blob: dda108735fce5877d85c3b9aa644fbca563cbcf5 [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 <clk.h>
9#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Fabien Dessenneac0da892019-05-14 11:20:34 +020011#include <mailbox-uclass.h>
Simon Glass9bc15642020-02-03 07:36:16 -070012#include <malloc.h>
Fabien Dessenneac0da892019-05-14 11:20:34 +020013#include <asm/io.h>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060015#include <linux/bitops.h>
Fabien Dessenneac0da892019-05-14 11:20:34 +020016
17/*
18 * IPCC has one set of registers per CPU
19 * IPCC_PROC_OFFST allows to define cpu registers set base address
20 * according to the assigned proc_id.
21 */
22
23#define IPCC_PROC_OFFST 0x010
24
25#define IPCC_XSCR 0x008
26#define IPCC_XTOYSR 0x00c
27
28#define IPCC_HWCFGR 0x3f0
29#define IPCFGR_CHAN_MASK GENMASK(7, 0)
30
31#define RX_BIT_CHAN(chan) BIT(chan)
32#define TX_BIT_SHIFT 16
33#define TX_BIT_CHAN(chan) BIT(TX_BIT_SHIFT + (chan))
34
35#define STM32_MAX_PROCS 2
36
37struct stm32_ipcc {
38 void __iomem *reg_base;
39 void __iomem *reg_proc;
40 u32 proc_id;
41 u32 n_chans;
42};
43
44static int stm32_ipcc_request(struct mbox_chan *chan)
45{
46 struct stm32_ipcc *ipcc = dev_get_priv(chan->dev);
47
Patrick Delaunay20be9bb2020-11-06 19:01:49 +010048 dev_dbg(chan->dev, "chan=%p\n", chan);
Fabien Dessenneac0da892019-05-14 11:20:34 +020049
50 if (chan->id >= ipcc->n_chans) {
Patrick Delaunay20be9bb2020-11-06 19:01:49 +010051 dev_dbg(chan->dev, "failed to request channel: %ld\n",
52 chan->id);
Fabien Dessenneac0da892019-05-14 11:20:34 +020053 return -EINVAL;
54 }
55
56 return 0;
57}
58
59static int stm32_ipcc_free(struct mbox_chan *chan)
60{
Patrick Delaunay20be9bb2020-11-06 19:01:49 +010061 dev_dbg(chan->dev, "chan=%p\n", chan);
Fabien Dessenneac0da892019-05-14 11:20:34 +020062
63 return 0;
64}
65
66static int stm32_ipcc_send(struct mbox_chan *chan, const void *data)
67{
68 struct stm32_ipcc *ipcc = dev_get_priv(chan->dev);
69
Patrick Delaunay20be9bb2020-11-06 19:01:49 +010070 dev_dbg(chan->dev, "chan=%p, data=%p\n", chan, data);
Fabien Dessenneac0da892019-05-14 11:20:34 +020071
72 if (readl(ipcc->reg_proc + IPCC_XTOYSR) & BIT(chan->id))
73 return -EBUSY;
74
75 /* set channel n occupied */
76 setbits_le32(ipcc->reg_proc + IPCC_XSCR, TX_BIT_CHAN(chan->id));
77
78 return 0;
79}
80
81static int stm32_ipcc_recv(struct mbox_chan *chan, void *data)
82{
83 struct stm32_ipcc *ipcc = dev_get_priv(chan->dev);
84 u32 val;
85 int proc_offset;
86
Patrick Delaunay20be9bb2020-11-06 19:01:49 +010087 dev_dbg(chan->dev, "chan=%p, data=%p\n", chan, data);
Fabien Dessenneac0da892019-05-14 11:20:34 +020088
89 /* read 'channel occupied' status from other proc */
90 proc_offset = ipcc->proc_id ? -IPCC_PROC_OFFST : IPCC_PROC_OFFST;
91 val = readl(ipcc->reg_proc + proc_offset + IPCC_XTOYSR);
92
93 if (!(val & BIT(chan->id)))
94 return -ENODATA;
95
96 setbits_le32(ipcc->reg_proc + IPCC_XSCR, RX_BIT_CHAN(chan->id));
97
98 return 0;
99}
100
101static int stm32_ipcc_probe(struct udevice *dev)
102{
103 struct stm32_ipcc *ipcc = dev_get_priv(dev);
104 fdt_addr_t addr;
Fabien Dessenneac0da892019-05-14 11:20:34 +0200105 struct clk clk;
Patrick Delaunay6064c612020-09-09 17:48:15 +0200106 int ret;
Fabien Dessenneac0da892019-05-14 11:20:34 +0200107
Patrick Delaunay20be9bb2020-11-06 19:01:49 +0100108 dev_dbg(dev, "\n");
Fabien Dessenneac0da892019-05-14 11:20:34 +0200109
110 addr = dev_read_addr(dev);
111 if (addr == FDT_ADDR_T_NONE)
112 return -EINVAL;
113
114 ipcc->reg_base = (void __iomem *)addr;
115
116 /* proc_id */
Patrick Delaunay6064c612020-09-09 17:48:15 +0200117 ret = dev_read_u32_index(dev, "st,proc_id", 1, &ipcc->proc_id);
118 if (ret) {
Fabien Dessenneac0da892019-05-14 11:20:34 +0200119 dev_dbg(dev, "Missing st,proc_id\n");
120 return -EINVAL;
121 }
122
Fabien Dessenneac0da892019-05-14 11:20:34 +0200123 if (ipcc->proc_id >= STM32_MAX_PROCS) {
124 dev_err(dev, "Invalid proc_id (%d)\n", ipcc->proc_id);
125 return -EINVAL;
126 }
127
128 ipcc->reg_proc = ipcc->reg_base + ipcc->proc_id * IPCC_PROC_OFFST;
129
130 ret = clk_get_by_index(dev, 0, &clk);
131 if (ret)
132 return ret;
133
134 ret = clk_enable(&clk);
135 if (ret)
Sean Andersond318eb32023-12-16 14:38:42 -0500136 return ret;
Fabien Dessenneac0da892019-05-14 11:20:34 +0200137
138 /* get channel number */
139 ipcc->n_chans = readl(ipcc->reg_base + IPCC_HWCFGR);
140 ipcc->n_chans &= IPCFGR_CHAN_MASK;
141
142 return 0;
Fabien Dessenneac0da892019-05-14 11:20:34 +0200143}
144
145static const struct udevice_id stm32_ipcc_ids[] = {
146 { .compatible = "st,stm32mp1-ipcc" },
147 { }
148};
149
150struct mbox_ops stm32_ipcc_mbox_ops = {
151 .request = stm32_ipcc_request,
Simon Glass1ee48192020-02-03 07:35:50 -0700152 .rfree = stm32_ipcc_free,
Fabien Dessenneac0da892019-05-14 11:20:34 +0200153 .send = stm32_ipcc_send,
154 .recv = stm32_ipcc_recv,
155};
156
157U_BOOT_DRIVER(stm32_ipcc) = {
158 .name = "stm32_ipcc",
159 .id = UCLASS_MAILBOX,
160 .of_match = stm32_ipcc_ids,
161 .probe = stm32_ipcc_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700162 .priv_auto = sizeof(struct stm32_ipcc),
Fabien Dessenneac0da892019-05-14 11:20:34 +0200163 .ops = &stm32_ipcc_mbox_ops,
164};