4 * Copyright (C) 2015 ST Microelectronics
6 * Author: Lee Jones <lee.jones@linaro.org> for ST Microelectronics
8 * Based on the original driver written by;
9 * Alexandre Torgue, Olivier Lebreton and Loic Pallardy
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
17 #include <linux/err.h>
18 #include <linux/interrupt.h>
20 #include <linux/kernel.h>
21 #include <linux/mailbox_controller.h>
22 #include <linux/module.h>
24 #include <linux/of_device.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
30 #define STI_MBOX_INST_MAX 4 /* RAM saving: Max supported instances */
31 #define STI_MBOX_CHAN_MAX 20 /* RAM saving: Max supported channels */
33 #define STI_IRQ_VAL_OFFSET 0x04 /* Read interrupt status */
34 #define STI_IRQ_SET_OFFSET 0x24 /* Generate a Tx channel interrupt */
35 #define STI_IRQ_CLR_OFFSET 0x44 /* Clear pending Rx interrupts */
36 #define STI_ENA_VAL_OFFSET 0x64 /* Read enable status */
37 #define STI_ENA_SET_OFFSET 0x84 /* Enable a channel */
38 #define STI_ENA_CLR_OFFSET 0xa4 /* Disable a channel */
40 #define MBOX_BASE(mdev, inst) ((mdev)->base + ((inst) * 4))
43 * STi Mailbox device data
45 * An IP Mailbox is currently composed of 4 instances
46 * Each instance is currently composed of 32 channels
47 * This means that we have 128 channels per Mailbox
48 * A channel an be used for TX or RX
50 * @dev: Device to which it is attached
51 * @mbox: Representation of a communication channel controller
52 * @base: Base address of the register mapping region
53 * @name: Name of the mailbox
54 * @enabled: Local copy of enabled channels
55 * @lock: Mutex protecting enabled status
57 struct sti_mbox_device {
59 struct mbox_controller *mbox;
62 u32 enabled[STI_MBOX_INST_MAX];
67 * STi Mailbox platform specific configuration
69 * @num_inst: Maximum number of instances in one HW Mailbox
70 * @num_chan: Maximum number of channel per instance
72 struct sti_mbox_pdata {
73 unsigned int num_inst;
74 unsigned int num_chan;
78 * STi Mailbox allocated channel information
80 * @mdev: Pointer to parent Mailbox device
81 * @instance: Instance number channel resides in
82 * @channel: Channel number pertaining to this container
85 struct sti_mbox_device *mdev;
86 unsigned int instance;
90 static inline bool sti_mbox_channel_is_enabled(struct mbox_chan *chan)
92 struct sti_channel *chan_info = chan->con_priv;
93 struct sti_mbox_device *mdev = chan_info->mdev;
94 unsigned int instance = chan_info->instance;
95 unsigned int channel = chan_info->channel;
97 return mdev->enabled[instance] & BIT(channel);
101 struct mbox_chan *sti_mbox_to_channel(struct mbox_controller *mbox,
102 unsigned int instance,
103 unsigned int channel)
105 struct sti_channel *chan_info;
108 for (i = 0; i < mbox->num_chans; i++) {
109 chan_info = mbox->chans[i].con_priv;
111 chan_info->instance == instance &&
112 chan_info->channel == channel)
113 return &mbox->chans[i];
117 "Channel not registered: instance: %d channel: %d\n",
123 static void sti_mbox_enable_channel(struct mbox_chan *chan)
125 struct sti_channel *chan_info = chan->con_priv;
126 struct sti_mbox_device *mdev = chan_info->mdev;
127 unsigned int instance = chan_info->instance;
128 unsigned int channel = chan_info->channel;
130 void __iomem *base = MBOX_BASE(mdev, instance);
132 spin_lock_irqsave(&mdev->lock, flags);
133 mdev->enabled[instance] |= BIT(channel);
134 writel_relaxed(BIT(channel), base + STI_ENA_SET_OFFSET);
135 spin_unlock_irqrestore(&mdev->lock, flags);
138 static void sti_mbox_disable_channel(struct mbox_chan *chan)
140 struct sti_channel *chan_info = chan->con_priv;
141 struct sti_mbox_device *mdev = chan_info->mdev;
142 unsigned int instance = chan_info->instance;
143 unsigned int channel = chan_info->channel;
145 void __iomem *base = MBOX_BASE(mdev, instance);
147 spin_lock_irqsave(&mdev->lock, flags);
148 mdev->enabled[instance] &= ~BIT(channel);
149 writel_relaxed(BIT(channel), base + STI_ENA_CLR_OFFSET);
150 spin_unlock_irqrestore(&mdev->lock, flags);
153 static void sti_mbox_clear_irq(struct mbox_chan *chan)
155 struct sti_channel *chan_info = chan->con_priv;
156 struct sti_mbox_device *mdev = chan_info->mdev;
157 unsigned int instance = chan_info->instance;
158 unsigned int channel = chan_info->channel;
159 void __iomem *base = MBOX_BASE(mdev, instance);
161 writel_relaxed(BIT(channel), base + STI_IRQ_CLR_OFFSET);
164 static struct mbox_chan *sti_mbox_irq_to_channel(struct sti_mbox_device *mdev,
165 unsigned int instance)
167 struct mbox_controller *mbox = mdev->mbox;
168 struct mbox_chan *chan = NULL;
169 unsigned int channel;
171 void __iomem *base = MBOX_BASE(mdev, instance);
173 bits = readl_relaxed(base + STI_IRQ_VAL_OFFSET);
175 /* No IRQs fired in specified instance */
178 /* An IRQ has fired, find the associated channel */
179 for (channel = 0; bits; channel++) {
180 if (!test_and_clear_bit(channel, &bits))
183 chan = sti_mbox_to_channel(mbox, instance, channel);
186 "IRQ fired on instance: %d channel: %d\n",
195 static irqreturn_t sti_mbox_thread_handler(int irq, void *data)
197 struct sti_mbox_device *mdev = data;
198 struct sti_mbox_pdata *pdata = dev_get_platdata(mdev->dev);
199 struct mbox_chan *chan;
200 unsigned int instance;
202 for (instance = 0; instance < pdata->num_inst; instance++) {
204 chan = sti_mbox_irq_to_channel(mdev, instance);
208 mbox_chan_received_data(chan, NULL);
209 sti_mbox_clear_irq(chan);
210 sti_mbox_enable_channel(chan);
217 static irqreturn_t sti_mbox_irq_handler(int irq, void *data)
219 struct sti_mbox_device *mdev = data;
220 struct sti_mbox_pdata *pdata = dev_get_platdata(mdev->dev);
221 struct sti_channel *chan_info;
222 struct mbox_chan *chan;
223 unsigned int instance;
226 for (instance = 0; instance < pdata->num_inst; instance++) {
227 chan = sti_mbox_irq_to_channel(mdev, instance);
230 chan_info = chan->con_priv;
232 if (!sti_mbox_channel_is_enabled(chan)) {
234 "Unexpected IRQ: %s\n"
235 " instance: %d: channel: %d [enabled: %x]\n",
236 mdev->name, chan_info->instance,
237 chan_info->channel, mdev->enabled[instance]);
239 /* Only handle IRQ if no other valid IRQs were found */
245 sti_mbox_disable_channel(chan);
246 ret = IRQ_WAKE_THREAD;
250 dev_err(mdev->dev, "Spurious IRQ - was a channel requested?\n");
255 static bool sti_mbox_tx_is_ready(struct mbox_chan *chan)
257 struct sti_channel *chan_info = chan->con_priv;
258 struct sti_mbox_device *mdev = chan_info->mdev;
259 unsigned int instance = chan_info->instance;
260 unsigned int channel = chan_info->channel;
261 void __iomem *base = MBOX_BASE(mdev, instance);
263 if (!(readl_relaxed(base + STI_ENA_VAL_OFFSET) & BIT(channel))) {
264 dev_dbg(mdev->dev, "Mbox: %s: inst: %d, chan: %d disabled\n",
265 mdev->name, instance, channel);
269 if (readl_relaxed(base + STI_IRQ_VAL_OFFSET) & BIT(channel)) {
270 dev_dbg(mdev->dev, "Mbox: %s: inst: %d, chan: %d not ready\n",
271 mdev->name, instance, channel);
278 static int sti_mbox_send_data(struct mbox_chan *chan, void *data)
280 struct sti_channel *chan_info = chan->con_priv;
281 struct sti_mbox_device *mdev = chan_info->mdev;
282 unsigned int instance = chan_info->instance;
283 unsigned int channel = chan_info->channel;
284 void __iomem *base = MBOX_BASE(mdev, instance);
286 /* Send event to co-processor */
287 writel_relaxed(BIT(channel), base + STI_IRQ_SET_OFFSET);
290 "Sent via Mailbox %s: instance: %d channel: %d\n",
291 mdev->name, instance, channel);
296 static int sti_mbox_startup_chan(struct mbox_chan *chan)
298 sti_mbox_clear_irq(chan);
299 sti_mbox_enable_channel(chan);
304 static void sti_mbox_shutdown_chan(struct mbox_chan *chan)
306 struct sti_channel *chan_info = chan->con_priv;
307 struct mbox_controller *mbox = chan_info->mdev->mbox;
310 for (i = 0; i < mbox->num_chans; i++)
311 if (chan == &mbox->chans[i])
314 if (mbox->num_chans == i) {
315 dev_warn(mbox->dev, "Request to free non-existent channel\n");
320 sti_mbox_disable_channel(chan);
321 sti_mbox_clear_irq(chan);
322 chan->con_priv = NULL;
325 static struct mbox_chan *sti_mbox_xlate(struct mbox_controller *mbox,
326 const struct of_phandle_args *spec)
328 struct sti_mbox_device *mdev = dev_get_drvdata(mbox->dev);
329 struct sti_mbox_pdata *pdata = dev_get_platdata(mdev->dev);
330 struct sti_channel *chan_info;
331 struct mbox_chan *chan = NULL;
332 unsigned int instance = spec->args[0];
333 unsigned int channel = spec->args[1];
336 /* Bounds checking */
337 if (instance >= pdata->num_inst || channel >= pdata->num_chan) {
339 "Invalid channel requested instance: %d channel: %d\n",
341 return ERR_PTR(-EINVAL);
344 for (i = 0; i < mbox->num_chans; i++) {
345 chan_info = mbox->chans[i].con_priv;
347 /* Is requested channel free? */
349 mbox->dev == chan_info->mdev->dev &&
350 instance == chan_info->instance &&
351 channel == chan_info->channel) {
353 dev_err(mbox->dev, "Channel in use\n");
354 return ERR_PTR(-EBUSY);
358 * Find the first free slot, then continue checking
359 * to see if requested channel is in use
361 if (!chan && !chan_info)
362 chan = &mbox->chans[i];
366 dev_err(mbox->dev, "No free channels left\n");
367 return ERR_PTR(-EBUSY);
370 chan_info = devm_kzalloc(mbox->dev, sizeof(*chan_info), GFP_KERNEL);
372 return ERR_PTR(-ENOMEM);
374 chan_info->mdev = mdev;
375 chan_info->instance = instance;
376 chan_info->channel = channel;
378 chan->con_priv = chan_info;
381 "Mbox: %s: Created channel: instance: %d channel: %d\n",
382 mdev->name, instance, channel);
387 static const struct mbox_chan_ops sti_mbox_ops = {
388 .startup = sti_mbox_startup_chan,
389 .shutdown = sti_mbox_shutdown_chan,
390 .send_data = sti_mbox_send_data,
391 .last_tx_done = sti_mbox_tx_is_ready,
394 static const struct sti_mbox_pdata mbox_stih407_pdata = {
399 static const struct of_device_id sti_mailbox_match[] = {
401 .compatible = "st,stih407-mailbox",
402 .data = (void *)&mbox_stih407_pdata
406 MODULE_DEVICE_TABLE(of, sti_mailbox_match);
408 static int sti_mbox_probe(struct platform_device *pdev)
410 const struct of_device_id *match;
411 struct mbox_controller *mbox;
412 struct sti_mbox_device *mdev;
413 struct device_node *np = pdev->dev.of_node;
414 struct mbox_chan *chans;
415 struct resource *res;
419 match = of_match_device(sti_mailbox_match, &pdev->dev);
421 dev_err(&pdev->dev, "No configuration found\n");
424 pdev->dev.platform_data = (struct sti_mbox_pdata *) match->data;
426 mdev = devm_kzalloc(&pdev->dev, sizeof(*mdev), GFP_KERNEL);
430 platform_set_drvdata(pdev, mdev);
432 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
433 mdev->base = devm_ioremap_resource(&pdev->dev, res);
434 if (IS_ERR(mdev->base))
435 return PTR_ERR(mdev->base);
437 ret = of_property_read_string(np, "mbox-name", &mdev->name);
439 mdev->name = np->full_name;
441 mbox = devm_kzalloc(&pdev->dev, sizeof(*mbox), GFP_KERNEL);
445 chans = devm_kzalloc(&pdev->dev,
446 sizeof(*chans) * STI_MBOX_CHAN_MAX, GFP_KERNEL);
450 mdev->dev = &pdev->dev;
453 spin_lock_init(&mdev->lock);
455 /* STi Mailbox does not have a Tx-Done or Tx-Ready IRQ */
456 mbox->txdone_irq = false;
457 mbox->txdone_poll = true;
458 mbox->txpoll_period = 100;
459 mbox->ops = &sti_mbox_ops;
460 mbox->dev = mdev->dev;
461 mbox->of_xlate = sti_mbox_xlate;
463 mbox->num_chans = STI_MBOX_CHAN_MAX;
465 ret = mbox_controller_register(mbox);
469 /* It's okay for Tx Mailboxes to not supply IRQs */
470 irq = platform_get_irq(pdev, 0);
473 "%s: Registered Tx only Mailbox\n", mdev->name);
477 ret = devm_request_threaded_irq(&pdev->dev, irq,
478 sti_mbox_irq_handler,
479 sti_mbox_thread_handler,
480 IRQF_ONESHOT, mdev->name, mdev);
482 dev_err(&pdev->dev, "Can't claim IRQ %d\n", irq);
483 mbox_controller_unregister(mbox);
487 dev_info(&pdev->dev, "%s: Registered Tx/Rx Mailbox\n", mdev->name);
492 static int sti_mbox_remove(struct platform_device *pdev)
494 struct sti_mbox_device *mdev = platform_get_drvdata(pdev);
496 mbox_controller_unregister(mdev->mbox);
501 static struct platform_driver sti_mbox_driver = {
502 .probe = sti_mbox_probe,
503 .remove = sti_mbox_remove,
505 .name = "sti-mailbox",
506 .of_match_table = sti_mailbox_match,
509 module_platform_driver(sti_mbox_driver);
511 MODULE_LICENSE("GPL");
512 MODULE_DESCRIPTION("STMicroelectronics Mailbox Controller");
513 MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org");
514 MODULE_ALIAS("platform:mailbox-sti");