1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) STMicroelectronics 2018 - All Rights Reserved
4 * Authors: Ludovic Barre <ludovic.barre@st.com> for STMicroelectronics.
5 * Fabien Dessenne <fabien.dessenne@st.com> for STMicroelectronics.
8 #include <linux/bitfield.h>
10 #include <linux/interrupt.h>
12 #include <linux/mailbox_controller.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_wakeirq.h>
17 #define IPCC_XCR 0x000
18 #define XCR_RXOIE BIT(0)
19 #define XCR_TXOIE BIT(16)
21 #define IPCC_XMR 0x004
22 #define IPCC_XSCR 0x008
23 #define IPCC_XTOYSR 0x00c
25 #define IPCC_PROC_OFFST 0x010
27 #define IPCC_HWCFGR 0x3f0
28 #define IPCFGR_CHAN_MASK GENMASK(7, 0)
30 #define IPCC_VER 0x3f4
31 #define VER_MINREV_MASK GENMASK(3, 0)
32 #define VER_MAJREV_MASK GENMASK(7, 4)
34 #define RX_BIT_MASK GENMASK(15, 0)
35 #define RX_BIT_CHAN(chan) BIT(chan)
36 #define TX_BIT_SHIFT 16
37 #define TX_BIT_MASK GENMASK(31, 16)
38 #define TX_BIT_CHAN(chan) BIT(TX_BIT_SHIFT + (chan))
40 #define STM32_MAX_PROCS 2
49 struct mbox_controller controller
;
50 void __iomem
*reg_base
;
51 void __iomem
*reg_proc
;
53 spinlock_t lock
; /* protect access to IPCC registers */
54 int irqs
[IPCC_IRQ_NUM
];
62 static inline void stm32_ipcc_set_bits(spinlock_t
*lock
, void __iomem
*reg
,
67 spin_lock_irqsave(lock
, flags
);
68 writel_relaxed(readl_relaxed(reg
) | mask
, reg
);
69 spin_unlock_irqrestore(lock
, flags
);
72 static inline void stm32_ipcc_clr_bits(spinlock_t
*lock
, void __iomem
*reg
,
77 spin_lock_irqsave(lock
, flags
);
78 writel_relaxed(readl_relaxed(reg
) & ~mask
, reg
);
79 spin_unlock_irqrestore(lock
, flags
);
82 static irqreturn_t
stm32_ipcc_rx_irq(int irq
, void *data
)
84 struct stm32_ipcc
*ipcc
= data
;
85 struct device
*dev
= ipcc
->controller
.dev
;
86 u32 status
, mr
, tosr
, chan
;
87 irqreturn_t ret
= IRQ_NONE
;
90 /* read 'channel occupied' status from other proc */
91 proc_offset
= ipcc
->proc_id
? -IPCC_PROC_OFFST
: IPCC_PROC_OFFST
;
92 tosr
= readl_relaxed(ipcc
->reg_proc
+ proc_offset
+ IPCC_XTOYSR
);
93 mr
= readl_relaxed(ipcc
->reg_proc
+ IPCC_XMR
);
95 /* search for unmasked 'channel occupied' */
96 status
= tosr
& FIELD_GET(RX_BIT_MASK
, ~mr
);
98 for (chan
= 0; chan
< ipcc
->n_chans
; chan
++) {
99 if (!(status
& (1 << chan
)))
102 dev_dbg(dev
, "%s: chan:%d rx\n", __func__
, chan
);
104 mbox_chan_received_data(&ipcc
->controller
.chans
[chan
], NULL
);
106 stm32_ipcc_set_bits(&ipcc
->lock
, ipcc
->reg_proc
+ IPCC_XSCR
,
115 static irqreturn_t
stm32_ipcc_tx_irq(int irq
, void *data
)
117 struct stm32_ipcc
*ipcc
= data
;
118 struct device
*dev
= ipcc
->controller
.dev
;
119 u32 status
, mr
, tosr
, chan
;
120 irqreturn_t ret
= IRQ_NONE
;
122 tosr
= readl_relaxed(ipcc
->reg_proc
+ IPCC_XTOYSR
);
123 mr
= readl_relaxed(ipcc
->reg_proc
+ IPCC_XMR
);
125 /* search for unmasked 'channel free' */
126 status
= ~tosr
& FIELD_GET(TX_BIT_MASK
, ~mr
);
128 for (chan
= 0; chan
< ipcc
->n_chans
; chan
++) {
129 if (!(status
& (1 << chan
)))
132 dev_dbg(dev
, "%s: chan:%d tx\n", __func__
, chan
);
134 /* mask 'tx channel free' interrupt */
135 stm32_ipcc_set_bits(&ipcc
->lock
, ipcc
->reg_proc
+ IPCC_XMR
,
138 mbox_chan_txdone(&ipcc
->controller
.chans
[chan
], 0);
146 static int stm32_ipcc_send_data(struct mbox_chan
*link
, void *data
)
148 unsigned int chan
= (unsigned int)link
->con_priv
;
149 struct stm32_ipcc
*ipcc
= container_of(link
->mbox
, struct stm32_ipcc
,
152 dev_dbg(ipcc
->controller
.dev
, "%s: chan:%d\n", __func__
, chan
);
154 /* set channel n occupied */
155 stm32_ipcc_set_bits(&ipcc
->lock
, ipcc
->reg_proc
+ IPCC_XSCR
,
158 /* unmask 'tx channel free' interrupt */
159 stm32_ipcc_clr_bits(&ipcc
->lock
, ipcc
->reg_proc
+ IPCC_XMR
,
165 static int stm32_ipcc_startup(struct mbox_chan
*link
)
167 unsigned int chan
= (unsigned int)link
->con_priv
;
168 struct stm32_ipcc
*ipcc
= container_of(link
->mbox
, struct stm32_ipcc
,
172 ret
= clk_prepare_enable(ipcc
->clk
);
174 dev_err(ipcc
->controller
.dev
, "can not enable the clock\n");
178 /* unmask 'rx channel occupied' interrupt */
179 stm32_ipcc_clr_bits(&ipcc
->lock
, ipcc
->reg_proc
+ IPCC_XMR
,
185 static void stm32_ipcc_shutdown(struct mbox_chan
*link
)
187 unsigned int chan
= (unsigned int)link
->con_priv
;
188 struct stm32_ipcc
*ipcc
= container_of(link
->mbox
, struct stm32_ipcc
,
191 /* mask rx/tx interrupt */
192 stm32_ipcc_set_bits(&ipcc
->lock
, ipcc
->reg_proc
+ IPCC_XMR
,
193 RX_BIT_CHAN(chan
) | TX_BIT_CHAN(chan
));
195 clk_disable_unprepare(ipcc
->clk
);
198 static const struct mbox_chan_ops stm32_ipcc_ops
= {
199 .send_data
= stm32_ipcc_send_data
,
200 .startup
= stm32_ipcc_startup
,
201 .shutdown
= stm32_ipcc_shutdown
,
204 static int stm32_ipcc_probe(struct platform_device
*pdev
)
206 struct device
*dev
= &pdev
->dev
;
207 struct device_node
*np
= dev
->of_node
;
208 struct stm32_ipcc
*ipcc
;
209 struct resource
*res
;
213 static const char * const irq_name
[] = {"rx", "tx"};
214 irq_handler_t irq_thread
[] = {stm32_ipcc_rx_irq
, stm32_ipcc_tx_irq
};
217 dev_err(dev
, "No DT found\n");
221 ipcc
= devm_kzalloc(dev
, sizeof(*ipcc
), GFP_KERNEL
);
225 spin_lock_init(&ipcc
->lock
);
228 if (of_property_read_u32(np
, "st,proc-id", &ipcc
->proc_id
)) {
229 dev_err(dev
, "Missing st,proc-id\n");
233 if (ipcc
->proc_id
>= STM32_MAX_PROCS
) {
234 dev_err(dev
, "Invalid proc_id (%d)\n", ipcc
->proc_id
);
239 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
240 ipcc
->reg_base
= devm_ioremap_resource(dev
, res
);
241 if (IS_ERR(ipcc
->reg_base
))
242 return PTR_ERR(ipcc
->reg_base
);
244 ipcc
->reg_proc
= ipcc
->reg_base
+ ipcc
->proc_id
* IPCC_PROC_OFFST
;
247 ipcc
->clk
= devm_clk_get(dev
, NULL
);
248 if (IS_ERR(ipcc
->clk
))
249 return PTR_ERR(ipcc
->clk
);
251 ret
= clk_prepare_enable(ipcc
->clk
);
253 dev_err(dev
, "can not enable the clock\n");
258 for (i
= 0; i
< IPCC_IRQ_NUM
; i
++) {
259 ipcc
->irqs
[i
] = platform_get_irq_byname(pdev
, irq_name
[i
]);
260 if (ipcc
->irqs
[i
] < 0) {
261 if (ipcc
->irqs
[i
] != -EPROBE_DEFER
)
262 dev_err(dev
, "no IRQ specified %s\n",
268 ret
= devm_request_threaded_irq(dev
, ipcc
->irqs
[i
], NULL
,
269 irq_thread
[i
], IRQF_ONESHOT
,
270 dev_name(dev
), ipcc
);
272 dev_err(dev
, "failed to request irq %d (%d)\n", i
, ret
);
277 /* mask and enable rx/tx irq */
278 stm32_ipcc_set_bits(&ipcc
->lock
, ipcc
->reg_proc
+ IPCC_XMR
,
279 RX_BIT_MASK
| TX_BIT_MASK
);
280 stm32_ipcc_set_bits(&ipcc
->lock
, ipcc
->reg_proc
+ IPCC_XCR
,
281 XCR_RXOIE
| XCR_TXOIE
);
284 if (of_property_read_bool(np
, "wakeup-source")) {
285 ipcc
->wkp
= platform_get_irq_byname(pdev
, "wakeup");
287 if (ipcc
->wkp
!= -EPROBE_DEFER
)
288 dev_err(dev
, "could not get wakeup IRQ\n");
293 device_set_wakeup_capable(dev
, true);
294 ret
= dev_pm_set_dedicated_wake_irq(dev
, ipcc
->wkp
);
296 dev_err(dev
, "Failed to set wake up irq\n");
301 /* mailbox controller */
302 ipcc
->n_chans
= readl_relaxed(ipcc
->reg_base
+ IPCC_HWCFGR
);
303 ipcc
->n_chans
&= IPCFGR_CHAN_MASK
;
305 ipcc
->controller
.dev
= dev
;
306 ipcc
->controller
.txdone_irq
= true;
307 ipcc
->controller
.ops
= &stm32_ipcc_ops
;
308 ipcc
->controller
.num_chans
= ipcc
->n_chans
;
309 ipcc
->controller
.chans
= devm_kcalloc(dev
, ipcc
->controller
.num_chans
,
310 sizeof(*ipcc
->controller
.chans
),
312 if (!ipcc
->controller
.chans
) {
317 for (i
= 0; i
< ipcc
->controller
.num_chans
; i
++)
318 ipcc
->controller
.chans
[i
].con_priv
= (void *)i
;
320 ret
= devm_mbox_controller_register(dev
, &ipcc
->controller
);
324 platform_set_drvdata(pdev
, ipcc
);
326 ip_ver
= readl_relaxed(ipcc
->reg_base
+ IPCC_VER
);
328 dev_info(dev
, "ipcc rev:%ld.%ld enabled, %d chans, proc %d\n",
329 FIELD_GET(VER_MAJREV_MASK
, ip_ver
),
330 FIELD_GET(VER_MINREV_MASK
, ip_ver
),
331 ipcc
->controller
.num_chans
, ipcc
->proc_id
);
333 clk_disable_unprepare(ipcc
->clk
);
338 dev_pm_clear_wake_irq(dev
);
340 device_init_wakeup(dev
, false);
342 clk_disable_unprepare(ipcc
->clk
);
346 static int stm32_ipcc_remove(struct platform_device
*pdev
)
348 struct stm32_ipcc
*ipcc
= platform_get_drvdata(pdev
);
351 dev_pm_clear_wake_irq(&pdev
->dev
);
353 device_init_wakeup(&pdev
->dev
, false);
358 #ifdef CONFIG_PM_SLEEP
359 static void stm32_ipcc_set_irq_wake(struct device
*dev
, bool enable
)
361 struct stm32_ipcc
*ipcc
= dev_get_drvdata(dev
);
364 if (device_may_wakeup(dev
))
365 for (i
= 0; i
< IPCC_IRQ_NUM
; i
++)
366 irq_set_irq_wake(ipcc
->irqs
[i
], enable
);
369 static int stm32_ipcc_suspend(struct device
*dev
)
371 struct stm32_ipcc
*ipcc
= dev_get_drvdata(dev
);
373 ipcc
->xmr
= readl_relaxed(ipcc
->reg_proc
+ IPCC_XMR
);
374 ipcc
->xcr
= readl_relaxed(ipcc
->reg_proc
+ IPCC_XCR
);
376 stm32_ipcc_set_irq_wake(dev
, true);
381 static int stm32_ipcc_resume(struct device
*dev
)
383 struct stm32_ipcc
*ipcc
= dev_get_drvdata(dev
);
385 stm32_ipcc_set_irq_wake(dev
, false);
387 writel_relaxed(ipcc
->xmr
, ipcc
->reg_proc
+ IPCC_XMR
);
388 writel_relaxed(ipcc
->xcr
, ipcc
->reg_proc
+ IPCC_XCR
);
394 static SIMPLE_DEV_PM_OPS(stm32_ipcc_pm_ops
,
395 stm32_ipcc_suspend
, stm32_ipcc_resume
);
397 static const struct of_device_id stm32_ipcc_of_match
[] = {
398 { .compatible
= "st,stm32mp1-ipcc" },
401 MODULE_DEVICE_TABLE(of
, stm32_ipcc_of_match
);
403 static struct platform_driver stm32_ipcc_driver
= {
405 .name
= "stm32-ipcc",
406 .pm
= &stm32_ipcc_pm_ops
,
407 .of_match_table
= stm32_ipcc_of_match
,
409 .probe
= stm32_ipcc_probe
,
410 .remove
= stm32_ipcc_remove
,
413 module_platform_driver(stm32_ipcc_driver
);
415 MODULE_AUTHOR("Ludovic Barre <ludovic.barre@st.com>");
416 MODULE_AUTHOR("Fabien Dessenne <fabien.dessenne@st.com>");
417 MODULE_DESCRIPTION("STM32 IPCC driver");
418 MODULE_LICENSE("GPL v2");