brintos

brintos / linux-shallow public Read only

0
0
Text · 12.8 KiB · b4b5bdd Raw
496 lines · c
1// SPDX-License-Identifier: GPL-2.0-or-later2/*3 * STi Mailbox4 *5 * Copyright (C) 2015 ST Microelectronics6 *7 * Author: Lee Jones <lee.jones@linaro.org> for ST Microelectronics8 *9 * Based on the original driver written by;10 *   Alexandre Torgue, Olivier Lebreton and Loic Pallardy11 */12 13#include <linux/err.h>14#include <linux/interrupt.h>15#include <linux/io.h>16#include <linux/kernel.h>17#include <linux/mailbox_controller.h>18#include <linux/module.h>19#include <linux/of.h>20#include <linux/platform_device.h>21#include <linux/property.h>22#include <linux/slab.h>23 24#include "mailbox.h"25 26#define STI_MBOX_INST_MAX	4      /* RAM saving: Max supported instances */27#define STI_MBOX_CHAN_MAX	20     /* RAM saving: Max supported channels  */28 29#define STI_IRQ_VAL_OFFSET	0x04   /* Read interrupt status	              */30#define STI_IRQ_SET_OFFSET	0x24   /* Generate a Tx channel interrupt     */31#define STI_IRQ_CLR_OFFSET	0x44   /* Clear pending Rx interrupts	      */32#define STI_ENA_VAL_OFFSET	0x64   /* Read enable status		      */33#define STI_ENA_SET_OFFSET	0x84   /* Enable a channel		      */34#define STI_ENA_CLR_OFFSET	0xa4   /* Disable a channel		      */35 36#define MBOX_BASE(mdev, inst)   ((mdev)->base + ((inst) * 4))37 38/**39 * struct sti_mbox_device - STi Mailbox device data40 *41 * @dev:	Device to which it is attached42 * @mbox:	Representation of a communication channel controller43 * @base:	Base address of the register mapping region44 * @name:	Name of the mailbox45 * @enabled:	Local copy of enabled channels46 * @lock:	Mutex protecting enabled status47 *48 * An IP Mailbox is currently composed of 4 instances49 * Each instance is currently composed of 32 channels50 * This means that we have 128 channels per Mailbox51 * A channel an be used for TX or RX52 */53struct sti_mbox_device {54	struct device		*dev;55	struct mbox_controller	*mbox;56	void __iomem		*base;57	const char		*name;58	u32			enabled[STI_MBOX_INST_MAX];59	spinlock_t		lock;60};61 62/**63 * struct sti_mbox_pdata - STi Mailbox platform specific configuration64 *65 * @num_inst:	Maximum number of instances in one HW Mailbox66 * @num_chan:	Maximum number of channel per instance67 */68struct sti_mbox_pdata {69	unsigned int		num_inst;70	unsigned int		num_chan;71};72 73/**74 * struct sti_channel - STi Mailbox allocated channel information75 *76 * @mdev:	Pointer to parent Mailbox device77 * @instance:	Instance number channel resides in78 * @channel:	Channel number pertaining to this container79 */80struct sti_channel {81	struct sti_mbox_device	*mdev;82	unsigned int		instance;83	unsigned int		channel;84};85 86static inline bool sti_mbox_channel_is_enabled(struct mbox_chan *chan)87{88	struct sti_channel *chan_info = chan->con_priv;89	struct sti_mbox_device *mdev = chan_info->mdev;90	unsigned int instance = chan_info->instance;91	unsigned int channel = chan_info->channel;92 93	return mdev->enabled[instance] & BIT(channel);94}95 96static inline97struct mbox_chan *sti_mbox_to_channel(struct mbox_controller *mbox,98				      unsigned int instance,99				      unsigned int channel)100{101	struct sti_channel *chan_info;102	int i;103 104	for (i = 0; i < mbox->num_chans; i++) {105		chan_info = mbox->chans[i].con_priv;106		if (chan_info &&107		    chan_info->instance == instance &&108		    chan_info->channel == channel)109			return &mbox->chans[i];110	}111 112	dev_err(mbox->dev,113		"Channel not registered: instance: %d channel: %d\n",114		instance, channel);115 116	return NULL;117}118 119static void sti_mbox_enable_channel(struct mbox_chan *chan)120{121	struct sti_channel *chan_info = chan->con_priv;122	struct sti_mbox_device *mdev = chan_info->mdev;123	unsigned int instance = chan_info->instance;124	unsigned int channel = chan_info->channel;125	unsigned long flags;126	void __iomem *base = MBOX_BASE(mdev, instance);127 128	spin_lock_irqsave(&mdev->lock, flags);129	mdev->enabled[instance] |= BIT(channel);130	writel_relaxed(BIT(channel), base + STI_ENA_SET_OFFSET);131	spin_unlock_irqrestore(&mdev->lock, flags);132}133 134static void sti_mbox_disable_channel(struct mbox_chan *chan)135{136	struct sti_channel *chan_info = chan->con_priv;137	struct sti_mbox_device *mdev = chan_info->mdev;138	unsigned int instance = chan_info->instance;139	unsigned int channel = chan_info->channel;140	unsigned long flags;141	void __iomem *base = MBOX_BASE(mdev, instance);142 143	spin_lock_irqsave(&mdev->lock, flags);144	mdev->enabled[instance] &= ~BIT(channel);145	writel_relaxed(BIT(channel), base + STI_ENA_CLR_OFFSET);146	spin_unlock_irqrestore(&mdev->lock, flags);147}148 149static void sti_mbox_clear_irq(struct mbox_chan *chan)150{151	struct sti_channel *chan_info = chan->con_priv;152	struct sti_mbox_device *mdev = chan_info->mdev;153	unsigned int instance = chan_info->instance;154	unsigned int channel = chan_info->channel;155	void __iomem *base = MBOX_BASE(mdev, instance);156 157	writel_relaxed(BIT(channel), base + STI_IRQ_CLR_OFFSET);158}159 160static struct mbox_chan *sti_mbox_irq_to_channel(struct sti_mbox_device *mdev,161						 unsigned int instance)162{163	struct mbox_controller *mbox = mdev->mbox;164	struct mbox_chan *chan = NULL;165	unsigned int channel;166	unsigned long bits;167	void __iomem *base = MBOX_BASE(mdev, instance);168 169	bits = readl_relaxed(base + STI_IRQ_VAL_OFFSET);170	if (!bits)171		/* No IRQs fired in specified instance */172		return NULL;173 174	/* An IRQ has fired, find the associated channel */175	for (channel = 0; bits; channel++) {176		if (!test_and_clear_bit(channel, &bits))177			continue;178 179		chan = sti_mbox_to_channel(mbox, instance, channel);180		if (chan) {181			dev_dbg(mbox->dev,182				"IRQ fired on instance: %d channel: %d\n",183				instance, channel);184			break;185		}186	}187 188	return chan;189}190 191static irqreturn_t sti_mbox_thread_handler(int irq, void *data)192{193	struct sti_mbox_device *mdev = data;194	struct sti_mbox_pdata *pdata = dev_get_platdata(mdev->dev);195	struct mbox_chan *chan;196	unsigned int instance;197 198	for (instance = 0; instance < pdata->num_inst; instance++) {199keep_looking:200		chan = sti_mbox_irq_to_channel(mdev, instance);201		if (!chan)202			continue;203 204		mbox_chan_received_data(chan, NULL);205		sti_mbox_clear_irq(chan);206		sti_mbox_enable_channel(chan);207		goto keep_looking;208	}209 210	return IRQ_HANDLED;211}212 213static irqreturn_t sti_mbox_irq_handler(int irq, void *data)214{215	struct sti_mbox_device *mdev = data;216	struct sti_mbox_pdata *pdata = dev_get_platdata(mdev->dev);217	struct sti_channel *chan_info;218	struct mbox_chan *chan;219	unsigned int instance;220	int ret = IRQ_NONE;221 222	for (instance = 0; instance < pdata->num_inst; instance++) {223		chan = sti_mbox_irq_to_channel(mdev, instance);224		if (!chan)225			continue;226		chan_info = chan->con_priv;227 228		if (!sti_mbox_channel_is_enabled(chan)) {229			dev_warn(mdev->dev,230				 "Unexpected IRQ: %s\n"231				 "  instance: %d: channel: %d [enabled: %x]\n",232				 mdev->name, chan_info->instance,233				 chan_info->channel, mdev->enabled[instance]);234 235			/* Only handle IRQ if no other valid IRQs were found */236			if (ret == IRQ_NONE)237				ret = IRQ_HANDLED;238			continue;239		}240 241		sti_mbox_disable_channel(chan);242		ret = IRQ_WAKE_THREAD;243	}244 245	if (ret == IRQ_NONE)246		dev_err(mdev->dev, "Spurious IRQ - was a channel requested?\n");247 248	return ret;249}250 251static bool sti_mbox_tx_is_ready(struct mbox_chan *chan)252{253	struct sti_channel *chan_info = chan->con_priv;254	struct sti_mbox_device *mdev = chan_info->mdev;255	unsigned int instance = chan_info->instance;256	unsigned int channel = chan_info->channel;257	void __iomem *base = MBOX_BASE(mdev, instance);258 259	if (!(readl_relaxed(base + STI_ENA_VAL_OFFSET) & BIT(channel))) {260		dev_dbg(mdev->dev, "Mbox: %s: inst: %d, chan: %d disabled\n",261			mdev->name, instance, channel);262		return false;263	}264 265	if (readl_relaxed(base + STI_IRQ_VAL_OFFSET) & BIT(channel)) {266		dev_dbg(mdev->dev, "Mbox: %s: inst: %d, chan: %d not ready\n",267			mdev->name, instance, channel);268		return false;269	}270 271	return true;272}273 274static int sti_mbox_send_data(struct mbox_chan *chan, void *data)275{276	struct sti_channel *chan_info = chan->con_priv;277	struct sti_mbox_device *mdev = chan_info->mdev;278	unsigned int instance = chan_info->instance;279	unsigned int channel = chan_info->channel;280	void __iomem *base = MBOX_BASE(mdev, instance);281 282	/* Send event to co-processor */283	writel_relaxed(BIT(channel), base + STI_IRQ_SET_OFFSET);284 285	dev_dbg(mdev->dev,286		"Sent via Mailbox %s: instance: %d channel: %d\n",287		mdev->name, instance, channel);288 289	return 0;290}291 292static int sti_mbox_startup_chan(struct mbox_chan *chan)293{294	sti_mbox_clear_irq(chan);295	sti_mbox_enable_channel(chan);296 297	return 0;298}299 300static void sti_mbox_shutdown_chan(struct mbox_chan *chan)301{302	struct sti_channel *chan_info = chan->con_priv;303	struct mbox_controller *mbox = chan_info->mdev->mbox;304	int i;305 306	for (i = 0; i < mbox->num_chans; i++)307		if (chan == &mbox->chans[i])308			break;309 310	if (mbox->num_chans == i) {311		dev_warn(mbox->dev, "Request to free non-existent channel\n");312		return;313	}314 315	/* Reset channel */316	sti_mbox_disable_channel(chan);317	sti_mbox_clear_irq(chan);318	chan->con_priv = NULL;319}320 321static struct mbox_chan *sti_mbox_xlate(struct mbox_controller *mbox,322					const struct of_phandle_args *spec)323{324	struct sti_mbox_device *mdev = dev_get_drvdata(mbox->dev);325	struct sti_mbox_pdata *pdata = dev_get_platdata(mdev->dev);326	struct sti_channel *chan_info;327	struct mbox_chan *chan = NULL;328	unsigned int instance  = spec->args[0];329	unsigned int channel   = spec->args[1];330	int i;331 332	/* Bounds checking */333	if (instance >= pdata->num_inst || channel  >= pdata->num_chan) {334		dev_err(mbox->dev,335			"Invalid channel requested instance: %d channel: %d\n",336			instance, channel);337		return ERR_PTR(-EINVAL);338	}339 340	for (i = 0; i < mbox->num_chans; i++) {341		chan_info = mbox->chans[i].con_priv;342 343		/* Is requested channel free? */344		if (chan_info &&345		    mbox->dev == chan_info->mdev->dev &&346		    instance == chan_info->instance &&347		    channel == chan_info->channel) {348 349			dev_err(mbox->dev, "Channel in use\n");350			return ERR_PTR(-EBUSY);351		}352 353		/*354		 * Find the first free slot, then continue checking355		 * to see if requested channel is in use356		 */357		if (!chan && !chan_info)358			chan = &mbox->chans[i];359	}360 361	if (!chan) {362		dev_err(mbox->dev, "No free channels left\n");363		return ERR_PTR(-EBUSY);364	}365 366	chan_info = devm_kzalloc(mbox->dev, sizeof(*chan_info), GFP_KERNEL);367	if (!chan_info)368		return ERR_PTR(-ENOMEM);369 370	chan_info->mdev		= mdev;371	chan_info->instance	= instance;372	chan_info->channel	= channel;373 374	chan->con_priv = chan_info;375 376	dev_info(mbox->dev,377		 "Mbox: %s: Created channel: instance: %d channel: %d\n",378		 mdev->name, instance, channel);379 380	return chan;381}382 383static const struct mbox_chan_ops sti_mbox_ops = {384	.startup	= sti_mbox_startup_chan,385	.shutdown	= sti_mbox_shutdown_chan,386	.send_data	= sti_mbox_send_data,387	.last_tx_done	= sti_mbox_tx_is_ready,388};389 390static const struct sti_mbox_pdata mbox_stih407_pdata = {391	.num_inst	= 4,392	.num_chan	= 32,393};394 395static const struct of_device_id sti_mailbox_match[] = {396	{397		.compatible = "st,stih407-mailbox",398		.data = (void *)&mbox_stih407_pdata399	},400	{ }401};402MODULE_DEVICE_TABLE(of, sti_mailbox_match);403 404static int sti_mbox_probe(struct platform_device *pdev)405{406	struct mbox_controller *mbox;407	struct sti_mbox_device *mdev;408	struct device_node *np = pdev->dev.of_node;409	struct mbox_chan *chans;410	int irq;411	int ret;412 413	pdev->dev.platform_data = (struct sti_mbox_pdata *)device_get_match_data(&pdev->dev);414	if (!pdev->dev.platform_data) {415		dev_err(&pdev->dev, "No configuration found\n");416		return -ENODEV;417	}418 419	mdev = devm_kzalloc(&pdev->dev, sizeof(*mdev), GFP_KERNEL);420	if (!mdev)421		return -ENOMEM;422 423	platform_set_drvdata(pdev, mdev);424 425	mdev->base = devm_platform_ioremap_resource(pdev, 0);426	if (IS_ERR(mdev->base))427		return PTR_ERR(mdev->base);428 429	ret = of_property_read_string(np, "mbox-name", &mdev->name);430	if (ret)431		mdev->name = np->full_name;432 433	mbox = devm_kzalloc(&pdev->dev, sizeof(*mbox), GFP_KERNEL);434	if (!mbox)435		return -ENOMEM;436 437	chans = devm_kcalloc(&pdev->dev,438			     STI_MBOX_CHAN_MAX, sizeof(*chans), GFP_KERNEL);439	if (!chans)440		return -ENOMEM;441 442	mdev->dev		= &pdev->dev;443	mdev->mbox		= mbox;444 445	spin_lock_init(&mdev->lock);446 447	/* STi Mailbox does not have a Tx-Done or Tx-Ready IRQ */448	mbox->txdone_irq	= false;449	mbox->txdone_poll	= true;450	mbox->txpoll_period	= 100;451	mbox->ops		= &sti_mbox_ops;452	mbox->dev		= mdev->dev;453	mbox->of_xlate		= sti_mbox_xlate;454	mbox->chans		= chans;455	mbox->num_chans		= STI_MBOX_CHAN_MAX;456 457	ret = devm_mbox_controller_register(&pdev->dev, mbox);458	if (ret)459		return ret;460 461	/* It's okay for Tx Mailboxes to not supply IRQs */462	irq = platform_get_irq(pdev, 0);463	if (irq < 0) {464		dev_info(&pdev->dev,465			 "%s: Registered Tx only Mailbox\n", mdev->name);466		return 0;467	}468 469	ret = devm_request_threaded_irq(&pdev->dev, irq,470					sti_mbox_irq_handler,471					sti_mbox_thread_handler,472					IRQF_ONESHOT, mdev->name, mdev);473	if (ret) {474		dev_err(&pdev->dev, "Can't claim IRQ %d\n", irq);475		return -EINVAL;476	}477 478	dev_info(&pdev->dev, "%s: Registered Tx/Rx Mailbox\n", mdev->name);479 480	return 0;481}482 483static struct platform_driver sti_mbox_driver = {484	.probe = sti_mbox_probe,485	.driver = {486		.name = "sti-mailbox",487		.of_match_table = sti_mailbox_match,488	},489};490module_platform_driver(sti_mbox_driver);491 492MODULE_LICENSE("GPL");493MODULE_DESCRIPTION("STMicroelectronics Mailbox Controller");494MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org");495MODULE_ALIAS("platform:mailbox-sti");496