1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright (C) 2015 ST Microelectronics
7 * Author: Lee Jones <lee.jones@linaro.org> for ST Microelectronics
9 * Based on the original driver written by;
10 * Alexandre Torgue, Olivier Lebreton and Loic Pallardy
13 #include <linux/err.h>
14 #include <linux/interrupt.h>
16 #include <linux/kernel.h>
17 #include <linux/mailbox_controller.h>
18 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
26 #define STI_MBOX_INST_MAX 4 /* RAM saving: Max supported instances */
27 #define STI_MBOX_CHAN_MAX 20 /* RAM saving: Max supported channels */
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 */
36 #define MBOX_BASE(mdev, inst) ((mdev)->base + ((inst) * 4))
39 * struct sti_mbox_device - STi Mailbox device data
41 * @dev: Device to which it is attached
42 * @mbox: Representation of a communication channel controller
43 * @base: Base address of the register mapping region
44 * @name: Name of the mailbox
45 * @enabled: Local copy of enabled channels
46 * @lock: Mutex protecting enabled status
48 * An IP Mailbox is currently composed of 4 instances
49 * Each instance is currently composed of 32 channels
50 * This means that we have 128 channels per Mailbox
51 * A channel an be used for TX or RX
53 struct sti_mbox_device {
55 struct mbox_controller *mbox;
58 u32 enabled[STI_MBOX_INST_MAX];
63 * struct sti_mbox_pdata - STi Mailbox platform specific configuration
65 * @num_inst: Maximum number of instances in one HW Mailbox
66 * @num_chan: Maximum number of channel per instance
68 struct sti_mbox_pdata {
69 unsigned int num_inst;
70 unsigned int num_chan;
74 * struct sti_channel - STi Mailbox allocated channel information
76 * @mdev: Pointer to parent Mailbox device
77 * @instance: Instance number channel resides in
78 * @channel: Channel number pertaining to this container
81 struct sti_mbox_device *mdev;
82 unsigned int instance;
86 static inline bool sti_mbox_channel_is_enabled(struct mbox_chan *chan)
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;
93 return mdev->enabled[instance] & BIT(channel);
97 struct mbox_chan *sti_mbox_to_channel(struct mbox_controller *mbox,
98 unsigned int instance,
101 struct sti_channel *chan_info;
104 for (i = 0; i < mbox->num_chans; i++) {
105 chan_info = mbox->chans[i].con_priv;
107 chan_info->instance == instance &&
108 chan_info->channel == channel)
109 return &mbox->chans[i];
113 "Channel not registered: instance: %d channel: %d\n",
119 static void sti_mbox_enable_channel(struct mbox_chan *chan)
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;
126 void __iomem *base = MBOX_BASE(mdev, instance);
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);
134 static void sti_mbox_disable_channel(struct mbox_chan *chan)
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;
141 void __iomem *base = MBOX_BASE(mdev, instance);
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);
149 static void sti_mbox_clear_irq(struct mbox_chan *chan)
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);
157 writel_relaxed(BIT(channel), base + STI_IRQ_CLR_OFFSET);
160 static struct mbox_chan *sti_mbox_irq_to_channel(struct sti_mbox_device *mdev,
161 unsigned int instance)
163 struct mbox_controller *mbox = mdev->mbox;
164 struct mbox_chan *chan = NULL;
165 unsigned int channel;
167 void __iomem *base = MBOX_BASE(mdev, instance);
169 bits = readl_relaxed(base + STI_IRQ_VAL_OFFSET);
171 /* No IRQs fired in specified instance */
174 /* An IRQ has fired, find the associated channel */
175 for (channel = 0; bits; channel++) {
176 if (!test_and_clear_bit(channel, &bits))
179 chan = sti_mbox_to_channel(mbox, instance, channel);
182 "IRQ fired on instance: %d channel: %d\n",
191 static irqreturn_t sti_mbox_thread_handler(int irq, void *data)
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;
198 for (instance = 0; instance < pdata->num_inst; instance++) {
200 chan = sti_mbox_irq_to_channel(mdev, instance);
204 mbox_chan_received_data(chan, NULL);
205 sti_mbox_clear_irq(chan);
206 sti_mbox_enable_channel(chan);
213 static irqreturn_t sti_mbox_irq_handler(int irq, void *data)
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;
222 for (instance = 0; instance < pdata->num_inst; instance++) {
223 chan = sti_mbox_irq_to_channel(mdev, instance);
226 chan_info = chan->con_priv;
228 if (!sti_mbox_channel_is_enabled(chan)) {
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]);
235 /* Only handle IRQ if no other valid IRQs were found */
241 sti_mbox_disable_channel(chan);
242 ret = IRQ_WAKE_THREAD;
246 dev_err(mdev->dev, "Spurious IRQ - was a channel requested?\n");
251 static bool sti_mbox_tx_is_ready(struct mbox_chan *chan)
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);
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);
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);
274 static int sti_mbox_send_data(struct mbox_chan *chan, void *data)
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);
282 /* Send event to co-processor */
283 writel_relaxed(BIT(channel), base + STI_IRQ_SET_OFFSET);
286 "Sent via Mailbox %s: instance: %d channel: %d\n",
287 mdev->name, instance, channel);
292 static int sti_mbox_startup_chan(struct mbox_chan *chan)
294 sti_mbox_clear_irq(chan);
295 sti_mbox_enable_channel(chan);
300 static void sti_mbox_shutdown_chan(struct mbox_chan *chan)
302 struct sti_channel *chan_info = chan->con_priv;
303 struct mbox_controller *mbox = chan_info->mdev->mbox;
306 for (i = 0; i < mbox->num_chans; i++)
307 if (chan == &mbox->chans[i])
310 if (mbox->num_chans == i) {
311 dev_warn(mbox->dev, "Request to free non-existent channel\n");
316 sti_mbox_disable_channel(chan);
317 sti_mbox_clear_irq(chan);
318 chan->con_priv = NULL;
321 static struct mbox_chan *sti_mbox_xlate(struct mbox_controller *mbox,
322 const struct of_phandle_args *spec)
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];
332 /* Bounds checking */
333 if (instance >= pdata->num_inst || channel >= pdata->num_chan) {
335 "Invalid channel requested instance: %d channel: %d\n",
337 return ERR_PTR(-EINVAL);
340 for (i = 0; i < mbox->num_chans; i++) {
341 chan_info = mbox->chans[i].con_priv;
343 /* Is requested channel free? */
345 mbox->dev == chan_info->mdev->dev &&
346 instance == chan_info->instance &&
347 channel == chan_info->channel) {
349 dev_err(mbox->dev, "Channel in use\n");
350 return ERR_PTR(-EBUSY);
354 * Find the first free slot, then continue checking
355 * to see if requested channel is in use
357 if (!chan && !chan_info)
358 chan = &mbox->chans[i];
362 dev_err(mbox->dev, "No free channels left\n");
363 return ERR_PTR(-EBUSY);
366 chan_info = devm_kzalloc(mbox->dev, sizeof(*chan_info), GFP_KERNEL);
368 return ERR_PTR(-ENOMEM);
370 chan_info->mdev = mdev;
371 chan_info->instance = instance;
372 chan_info->channel = channel;
374 chan->con_priv = chan_info;
377 "Mbox: %s: Created channel: instance: %d channel: %d\n",
378 mdev->name, instance, channel);
383 static 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,
390 static const struct sti_mbox_pdata mbox_stih407_pdata = {
395 static const struct of_device_id sti_mailbox_match[] = {
397 .compatible = "st,stih407-mailbox",
398 .data = (void *)&mbox_stih407_pdata
402 MODULE_DEVICE_TABLE(of, sti_mailbox_match);
404 static int sti_mbox_probe(struct platform_device *pdev)
406 const struct of_device_id *match;
407 struct mbox_controller *mbox;
408 struct sti_mbox_device *mdev;
409 struct device_node *np = pdev->dev.of_node;
410 struct mbox_chan *chans;
414 match = of_match_device(sti_mailbox_match, &pdev->dev);
416 dev_err(&pdev->dev, "No configuration found\n");
419 pdev->dev.platform_data = (struct sti_mbox_pdata *) match->data;
421 mdev = devm_kzalloc(&pdev->dev, sizeof(*mdev), GFP_KERNEL);
425 platform_set_drvdata(pdev, mdev);
427 mdev->base = devm_platform_ioremap_resource(pdev, 0);
428 if (IS_ERR(mdev->base))
429 return PTR_ERR(mdev->base);
431 ret = of_property_read_string(np, "mbox-name", &mdev->name);
433 mdev->name = np->full_name;
435 mbox = devm_kzalloc(&pdev->dev, sizeof(*mbox), GFP_KERNEL);
439 chans = devm_kcalloc(&pdev->dev,
440 STI_MBOX_CHAN_MAX, sizeof(*chans), GFP_KERNEL);
444 mdev->dev = &pdev->dev;
447 spin_lock_init(&mdev->lock);
449 /* STi Mailbox does not have a Tx-Done or Tx-Ready IRQ */
450 mbox->txdone_irq = false;
451 mbox->txdone_poll = true;
452 mbox->txpoll_period = 100;
453 mbox->ops = &sti_mbox_ops;
454 mbox->dev = mdev->dev;
455 mbox->of_xlate = sti_mbox_xlate;
457 mbox->num_chans = STI_MBOX_CHAN_MAX;
459 ret = devm_mbox_controller_register(&pdev->dev, mbox);
463 /* It's okay for Tx Mailboxes to not supply IRQs */
464 irq = platform_get_irq(pdev, 0);
467 "%s: Registered Tx only Mailbox\n", mdev->name);
471 ret = devm_request_threaded_irq(&pdev->dev, irq,
472 sti_mbox_irq_handler,
473 sti_mbox_thread_handler,
474 IRQF_ONESHOT, mdev->name, mdev);
476 dev_err(&pdev->dev, "Can't claim IRQ %d\n", irq);
480 dev_info(&pdev->dev, "%s: Registered Tx/Rx Mailbox\n", mdev->name);
485 static struct platform_driver sti_mbox_driver = {
486 .probe = sti_mbox_probe,
488 .name = "sti-mailbox",
489 .of_match_table = sti_mailbox_match,
492 module_platform_driver(sti_mbox_driver);
494 MODULE_LICENSE("GPL");
495 MODULE_DESCRIPTION("STMicroelectronics Mailbox Controller");
496 MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org");
497 MODULE_ALIAS("platform:mailbox-sti");