treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / mailbox / mailbox-sti.c
blob2baf69a0b81cc646ca3851558de33ba2635e8445
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * STi Mailbox
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>
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/of_device.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
24 #include "mailbox.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))
38 /**
39 * STi Mailbox device data
41 * An IP Mailbox is currently composed of 4 instances
42 * Each instance is currently composed of 32 channels
43 * This means that we have 128 channels per Mailbox
44 * A channel an be used for TX or RX
46 * @dev: Device to which it is attached
47 * @mbox: Representation of a communication channel controller
48 * @base: Base address of the register mapping region
49 * @name: Name of the mailbox
50 * @enabled: Local copy of enabled channels
51 * @lock: Mutex protecting enabled status
53 struct 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;
62 /**
63 * 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;
73 /**
74 * 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
80 struct sti_channel {
81 struct sti_mbox_device *mdev;
82 unsigned int instance;
83 unsigned int channel;
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);
96 static inline
97 struct mbox_chan *sti_mbox_to_channel(struct mbox_controller *mbox,
98 unsigned int instance,
99 unsigned int channel)
101 struct sti_channel *chan_info;
102 int i;
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];
112 dev_err(mbox->dev,
113 "Channel not registered: instance: %d channel: %d\n",
114 instance, channel);
116 return NULL;
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;
125 unsigned long flags;
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;
140 unsigned long flags;
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;
166 unsigned long bits;
167 void __iomem *base = MBOX_BASE(mdev, instance);
169 bits = readl_relaxed(base + STI_IRQ_VAL_OFFSET);
170 if (!bits)
171 /* No IRQs fired in specified instance */
172 return NULL;
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;
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;
188 return chan;
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++) {
199 keep_looking:
200 chan = sti_mbox_irq_to_channel(mdev, instance);
201 if (!chan)
202 continue;
204 mbox_chan_received_data(chan, NULL);
205 sti_mbox_clear_irq(chan);
206 sti_mbox_enable_channel(chan);
207 goto keep_looking;
210 return IRQ_HANDLED;
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;
220 int ret = IRQ_NONE;
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;
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]);
235 /* Only handle IRQ if no other valid IRQs were found */
236 if (ret == IRQ_NONE)
237 ret = IRQ_HANDLED;
238 continue;
241 sti_mbox_disable_channel(chan);
242 ret = IRQ_WAKE_THREAD;
245 if (ret == IRQ_NONE)
246 dev_err(mdev->dev, "Spurious IRQ - was a channel requested?\n");
248 return ret;
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);
262 return false;
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;
271 return true;
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);
285 dev_dbg(mdev->dev,
286 "Sent via Mailbox %s: instance: %d channel: %d\n",
287 mdev->name, instance, channel);
289 return 0;
292 static int sti_mbox_startup_chan(struct mbox_chan *chan)
294 sti_mbox_clear_irq(chan);
295 sti_mbox_enable_channel(chan);
297 return 0;
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;
304 int i;
306 for (i = 0; i < mbox->num_chans; i++)
307 if (chan == &mbox->chans[i])
308 break;
310 if (mbox->num_chans == i) {
311 dev_warn(mbox->dev, "Request to free non-existent channel\n");
312 return;
315 /* Reset channel */
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];
330 int i;
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);
340 for (i = 0; i < mbox->num_chans; i++) {
341 chan_info = mbox->chans[i].con_priv;
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) {
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];
361 if (!chan) {
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);
367 if (!chan_info)
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;
376 dev_info(mbox->dev,
377 "Mbox: %s: Created channel: instance: %d channel: %d\n",
378 mdev->name, instance, channel);
380 return chan;
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 = {
391 .num_inst = 4,
392 .num_chan = 32,
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;
411 struct resource *res;
412 int irq;
413 int ret;
415 match = of_match_device(sti_mailbox_match, &pdev->dev);
416 if (!match) {
417 dev_err(&pdev->dev, "No configuration found\n");
418 return -ENODEV;
420 pdev->dev.platform_data = (struct sti_mbox_pdata *) match->data;
422 mdev = devm_kzalloc(&pdev->dev, sizeof(*mdev), GFP_KERNEL);
423 if (!mdev)
424 return -ENOMEM;
426 platform_set_drvdata(pdev, mdev);
428 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
429 mdev->base = devm_ioremap_resource(&pdev->dev, res);
430 if (IS_ERR(mdev->base))
431 return PTR_ERR(mdev->base);
433 ret = of_property_read_string(np, "mbox-name", &mdev->name);
434 if (ret)
435 mdev->name = np->full_name;
437 mbox = devm_kzalloc(&pdev->dev, sizeof(*mbox), GFP_KERNEL);
438 if (!mbox)
439 return -ENOMEM;
441 chans = devm_kcalloc(&pdev->dev,
442 STI_MBOX_CHAN_MAX, sizeof(*chans), GFP_KERNEL);
443 if (!chans)
444 return -ENOMEM;
446 mdev->dev = &pdev->dev;
447 mdev->mbox = mbox;
449 spin_lock_init(&mdev->lock);
451 /* STi Mailbox does not have a Tx-Done or Tx-Ready IRQ */
452 mbox->txdone_irq = false;
453 mbox->txdone_poll = true;
454 mbox->txpoll_period = 100;
455 mbox->ops = &sti_mbox_ops;
456 mbox->dev = mdev->dev;
457 mbox->of_xlate = sti_mbox_xlate;
458 mbox->chans = chans;
459 mbox->num_chans = STI_MBOX_CHAN_MAX;
461 ret = devm_mbox_controller_register(&pdev->dev, mbox);
462 if (ret)
463 return ret;
465 /* It's okay for Tx Mailboxes to not supply IRQs */
466 irq = platform_get_irq(pdev, 0);
467 if (irq < 0) {
468 dev_info(&pdev->dev,
469 "%s: Registered Tx only Mailbox\n", mdev->name);
470 return 0;
473 ret = devm_request_threaded_irq(&pdev->dev, irq,
474 sti_mbox_irq_handler,
475 sti_mbox_thread_handler,
476 IRQF_ONESHOT, mdev->name, mdev);
477 if (ret) {
478 dev_err(&pdev->dev, "Can't claim IRQ %d\n", irq);
479 return -EINVAL;
482 dev_info(&pdev->dev, "%s: Registered Tx/Rx Mailbox\n", mdev->name);
484 return 0;
487 static struct platform_driver sti_mbox_driver = {
488 .probe = sti_mbox_probe,
489 .driver = {
490 .name = "sti-mailbox",
491 .of_match_table = sti_mailbox_match,
494 module_platform_driver(sti_mbox_driver);
496 MODULE_LICENSE("GPL");
497 MODULE_DESCRIPTION("STMicroelectronics Mailbox Controller");
498 MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org");
499 MODULE_ALIAS("platform:mailbox-sti");