blob: 046e1a8aca6e6af41ac4d78fb5f30451b2139ad3 [file] [log] [blame]
Fabien Dessenneac0da892019-05-14 11:20:34 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) STMicroelectronics 2019 - All Rights Reserved
4 */
5
Patrick Delaunay20be9bb2020-11-06 19:01:49 +01006#define LOG_CATEGORY UCLASS_MAILBOX
7
Fabien Dessenneac0da892019-05-14 11:20:34 +02008#include <common.h>
9#include <clk.h>
10#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Fabien Dessenneac0da892019-05-14 11:20:34 +020012#include <mailbox-uclass.h>
Simon Glass9bc15642020-02-03 07:36:16 -070013#include <malloc.h>
Fabien Dessenneac0da892019-05-14 11:20:34 +020014#include <asm/io.h>
Simon Glass9bc15642020-02-03 07:36:16 -070015#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060016#include <linux/bitops.h>
Fabien Dessenneac0da892019-05-14 11:20:34 +020017
18/*
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.
22 */
23
24#define IPCC_PROC_OFFST 0x010
25
26#define IPCC_XSCR 0x008
27#define IPCC_XTOYSR 0x00c
28
29#define IPCC_HWCFGR 0x3f0
30#define IPCFGR_CHAN_MASK GENMASK(7, 0)
31
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))
35
36#define STM32_MAX_PROCS 2
37
38struct stm32_ipcc {
39 void __iomem *reg_base;
40 void __iomem *reg_proc;
41 u32 proc_id;
42 u32 n_chans;
43};
44
45static int stm32_ipcc_request(struct mbox_chan *chan)
46{
47 struct stm32_ipcc *ipcc = dev_get_priv(chan->dev);
48
Patrick Delaunay20be9bb2020-11-06 19:01:49 +010049 dev_dbg(chan->dev, "chan=%p\n", chan);
Fabien Dessenneac0da892019-05-14 11:20:34 +020050
51 if (chan->id >= ipcc->n_chans) {
Patrick Delaunay20be9bb2020-11-06 19:01:49 +010052 dev_dbg(chan->dev, "failed to request channel: %ld\n",
53 chan->id);
Fabien Dessenneac0da892019-05-14 11:20:34 +020054 return -EINVAL;
55 }
56
57 return 0;
58}
59
60static int stm32_ipcc_free(struct mbox_chan *chan)
61{
Patrick Delaunay20be9bb2020-11-06 19:01:49 +010062 dev_dbg(chan->dev, "chan=%p\n", chan);
Fabien Dessenneac0da892019-05-14 11:20:34 +020063
64 return 0;
65}
66
67static int stm32_ipcc_send(struct mbox_chan *chan, const void *data)
68{
69 struct stm32_ipcc *ipcc = dev_get_priv(chan->dev);
70
Patrick Delaunay20be9bb2020-11-06 19:01:49 +010071 dev_dbg(chan->dev, "chan=%p, data=%p\n", chan, data);
Fabien Dessenneac0da892019-05-14 11:20:34 +020072
73 if (readl(ipcc->reg_proc + IPCC_XTOYSR) & BIT(chan->id))
74 return -EBUSY;
75
76 /* set channel n occupied */
77 setbits_le32(ipcc->reg_proc + IPCC_XSCR, TX_BIT_CHAN(chan->id));
78
79 return 0;
80}
81
82static int stm32_ipcc_recv(struct mbox_chan *chan, void *data)
83{
84 struct stm32_ipcc *ipcc = dev_get_priv(chan->dev);
85 u32 val;
86 int proc_offset;
87
Patrick Delaunay20be9bb2020-11-06 19:01:49 +010088 dev_dbg(chan->dev, "chan=%p, data=%p\n", chan, data);
Fabien Dessenneac0da892019-05-14 11:20:34 +020089
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);
93
94 if (!(val & BIT(chan->id)))
95 return -ENODATA;
96
97 setbits_le32(ipcc->reg_proc + IPCC_XSCR, RX_BIT_CHAN(chan->id));
98
99 return 0;
100}
101
102static int stm32_ipcc_probe(struct udevice *dev)
103{
104 struct stm32_ipcc *ipcc = dev_get_priv(dev);
105 fdt_addr_t addr;
Fabien Dessenneac0da892019-05-14 11:20:34 +0200106 struct clk clk;
Patrick Delaunay6064c612020-09-09 17:48:15 +0200107 int ret;
Fabien Dessenneac0da892019-05-14 11:20:34 +0200108
Patrick Delaunay20be9bb2020-11-06 19:01:49 +0100109 dev_dbg(dev, "\n");
Fabien Dessenneac0da892019-05-14 11:20:34 +0200110
111 addr = dev_read_addr(dev);
112 if (addr == FDT_ADDR_T_NONE)
113 return -EINVAL;
114
115 ipcc->reg_base = (void __iomem *)addr;
116
117 /* proc_id */
Patrick Delaunay6064c612020-09-09 17:48:15 +0200118 ret = dev_read_u32_index(dev, "st,proc_id", 1, &ipcc->proc_id);
119 if (ret) {
Fabien Dessenneac0da892019-05-14 11:20:34 +0200120 dev_dbg(dev, "Missing st,proc_id\n");
121 return -EINVAL;
122 }
123
Fabien Dessenneac0da892019-05-14 11:20:34 +0200124 if (ipcc->proc_id >= STM32_MAX_PROCS) {
125 dev_err(dev, "Invalid proc_id (%d)\n", ipcc->proc_id);
126 return -EINVAL;
127 }
128
129 ipcc->reg_proc = ipcc->reg_base + ipcc->proc_id * IPCC_PROC_OFFST;
130
131 ret = clk_get_by_index(dev, 0, &clk);
132 if (ret)
133 return ret;
134
135 ret = clk_enable(&clk);
136 if (ret)
Sean Andersond318eb32023-12-16 14:38:42 -0500137 return ret;
Fabien Dessenneac0da892019-05-14 11:20:34 +0200138
139 /* get channel number */
140 ipcc->n_chans = readl(ipcc->reg_base + IPCC_HWCFGR);
141 ipcc->n_chans &= IPCFGR_CHAN_MASK;
142
143 return 0;
Fabien Dessenneac0da892019-05-14 11:20:34 +0200144}
145
146static const struct udevice_id stm32_ipcc_ids[] = {
147 { .compatible = "st,stm32mp1-ipcc" },
148 { }
149};
150
151struct mbox_ops stm32_ipcc_mbox_ops = {
152 .request = stm32_ipcc_request,
Simon Glass1ee48192020-02-03 07:35:50 -0700153 .rfree = stm32_ipcc_free,
Fabien Dessenneac0da892019-05-14 11:20:34 +0200154 .send = stm32_ipcc_send,
155 .recv = stm32_ipcc_recv,
156};
157
158U_BOOT_DRIVER(stm32_ipcc) = {
159 .name = "stm32_ipcc",
160 .id = UCLASS_MAILBOX,
161 .of_match = stm32_ipcc_ids,
162 .probe = stm32_ipcc_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700163 .priv_auto = sizeof(struct stm32_ipcc),
Fabien Dessenneac0da892019-05-14 11:20:34 +0200164 .ops = &stm32_ipcc_mbox_ops,
165};