1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) STMicroelectronics 2019 - All Rights Reserved
6 #define LOG_CATEGORY UCLASS_MAILBOX
12 #include <mailbox-uclass.h>
15 #include <dm/device_compat.h>
16 #include <linux/bitops.h>
19 * IPCC has one set of registers per CPU
20 * IPCC_PROC_OFFST allows to define cpu registers set base address
21 * according to the assigned proc_id.
24 #define IPCC_PROC_OFFST 0x010
26 #define IPCC_XSCR 0x008
27 #define IPCC_XTOYSR 0x00c
29 #define IPCC_HWCFGR 0x3f0
30 #define IPCFGR_CHAN_MASK GENMASK(7, 0)
32 #define RX_BIT_CHAN(chan) BIT(chan)
33 #define TX_BIT_SHIFT 16
34 #define TX_BIT_CHAN(chan) BIT(TX_BIT_SHIFT + (chan))
36 #define STM32_MAX_PROCS 2
39 void __iomem *reg_base;
40 void __iomem *reg_proc;
45 static int stm32_ipcc_request(struct mbox_chan *chan)
47 struct stm32_ipcc *ipcc = dev_get_priv(chan->dev);
49 dev_dbg(chan->dev, "chan=%p\n", chan);
51 if (chan->id >= ipcc->n_chans) {
52 dev_dbg(chan->dev, "failed to request channel: %ld\n",
60 static int stm32_ipcc_free(struct mbox_chan *chan)
62 dev_dbg(chan->dev, "chan=%p\n", chan);
67 static int stm32_ipcc_send(struct mbox_chan *chan, const void *data)
69 struct stm32_ipcc *ipcc = dev_get_priv(chan->dev);
71 dev_dbg(chan->dev, "chan=%p, data=%p\n", chan, data);
73 if (readl(ipcc->reg_proc + IPCC_XTOYSR) & BIT(chan->id))
76 /* set channel n occupied */
77 setbits_le32(ipcc->reg_proc + IPCC_XSCR, TX_BIT_CHAN(chan->id));
82 static int stm32_ipcc_recv(struct mbox_chan *chan, void *data)
84 struct stm32_ipcc *ipcc = dev_get_priv(chan->dev);
88 dev_dbg(chan->dev, "chan=%p, data=%p\n", chan, data);
90 /* read 'channel occupied' status from other proc */
91 proc_offset = ipcc->proc_id ? -IPCC_PROC_OFFST : IPCC_PROC_OFFST;
92 val = readl(ipcc->reg_proc + proc_offset + IPCC_XTOYSR);
94 if (!(val & BIT(chan->id)))
97 setbits_le32(ipcc->reg_proc + IPCC_XSCR, RX_BIT_CHAN(chan->id));
102 static int stm32_ipcc_probe(struct udevice *dev)
104 struct stm32_ipcc *ipcc = dev_get_priv(dev);
111 addr = dev_read_addr(dev);
112 if (addr == FDT_ADDR_T_NONE)
115 ipcc->reg_base = (void __iomem *)addr;
118 ret = dev_read_u32_index(dev, "st,proc_id", 1, &ipcc->proc_id);
120 dev_dbg(dev, "Missing st,proc_id\n");
124 if (ipcc->proc_id >= STM32_MAX_PROCS) {
125 dev_err(dev, "Invalid proc_id (%d)\n", ipcc->proc_id);
129 ipcc->reg_proc = ipcc->reg_base + ipcc->proc_id * IPCC_PROC_OFFST;
131 ret = clk_get_by_index(dev, 0, &clk);
135 ret = clk_enable(&clk);
139 /* get channel number */
140 ipcc->n_chans = readl(ipcc->reg_base + IPCC_HWCFGR);
141 ipcc->n_chans &= IPCFGR_CHAN_MASK;
151 static const struct udevice_id stm32_ipcc_ids[] = {
152 { .compatible = "st,stm32mp1-ipcc" },
156 struct mbox_ops stm32_ipcc_mbox_ops = {
157 .request = stm32_ipcc_request,
158 .rfree = stm32_ipcc_free,
159 .send = stm32_ipcc_send,
160 .recv = stm32_ipcc_recv,
163 U_BOOT_DRIVER(stm32_ipcc) = {
164 .name = "stm32_ipcc",
165 .id = UCLASS_MAILBOX,
166 .of_match = stm32_ipcc_ids,
167 .probe = stm32_ipcc_probe,
168 .priv_auto = sizeof(struct stm32_ipcc),
169 .ops = &stm32_ipcc_mbox_ops,