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
407 static int sti_mbox_probe(struct platform_device
*pdev
)
409 const struct of_device_id
*match
;
410 struct mbox_controller
*mbox
;
411 struct sti_mbox_device
*mdev
;
412 struct device_node
*np
= pdev
->dev
.of_node
;
413 struct mbox_chan
*chans
;
414 struct resource
*res
;
418 match
= of_match_device(sti_mailbox_match
, &pdev
->dev
);
420 dev_err(&pdev
->dev
, "No configuration found\n");
423 pdev
->dev
.platform_data
= (struct sti_mbox_pdata
*) match
->data
;
425 mdev
= devm_kzalloc(&pdev
->dev
, sizeof(*mdev
), GFP_KERNEL
);
429 platform_set_drvdata(pdev
, mdev
);
431 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
432 mdev
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
433 if (IS_ERR(mdev
->base
))
434 return PTR_ERR(mdev
->base
);
436 ret
= of_property_read_string(np
, "mbox-name", &mdev
->name
);
438 mdev
->name
= np
->full_name
;
440 mbox
= devm_kzalloc(&pdev
->dev
, sizeof(*mbox
), GFP_KERNEL
);
444 chans
= devm_kzalloc(&pdev
->dev
,
445 sizeof(*chans
) * STI_MBOX_CHAN_MAX
, GFP_KERNEL
);
449 mdev
->dev
= &pdev
->dev
;
452 spin_lock_init(&mdev
->lock
);
454 /* STi Mailbox does not have a Tx-Done or Tx-Ready IRQ */
455 mbox
->txdone_irq
= false;
456 mbox
->txdone_poll
= true;
457 mbox
->txpoll_period
= 100;
458 mbox
->ops
= &sti_mbox_ops
;
459 mbox
->dev
= mdev
->dev
;
460 mbox
->of_xlate
= sti_mbox_xlate
;
462 mbox
->num_chans
= STI_MBOX_CHAN_MAX
;
464 ret
= mbox_controller_register(mbox
);
468 /* It's okay for Tx Mailboxes to not supply IRQs */
469 irq
= platform_get_irq(pdev
, 0);
472 "%s: Registered Tx only Mailbox\n", mdev
->name
);
476 ret
= devm_request_threaded_irq(&pdev
->dev
, irq
,
477 sti_mbox_irq_handler
,
478 sti_mbox_thread_handler
,
479 IRQF_ONESHOT
, mdev
->name
, mdev
);
481 dev_err(&pdev
->dev
, "Can't claim IRQ %d\n", irq
);
482 mbox_controller_unregister(mbox
);
486 dev_info(&pdev
->dev
, "%s: Registered Tx/Rx Mailbox\n", mdev
->name
);
491 static int sti_mbox_remove(struct platform_device
*pdev
)
493 struct sti_mbox_device
*mdev
= platform_get_drvdata(pdev
);
495 mbox_controller_unregister(mdev
->mbox
);
500 static struct platform_driver sti_mbox_driver
= {
501 .probe
= sti_mbox_probe
,
502 .remove
= sti_mbox_remove
,
504 .name
= "sti-mailbox",
505 .of_match_table
= sti_mailbox_match
,
508 module_platform_driver(sti_mbox_driver
);
510 MODULE_LICENSE("GPL");
511 MODULE_DESCRIPTION("STMicroelectronics Mailbox Controller");
512 MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org");
513 MODULE_ALIAS("platform:mailbox-sti");