blob: 0778eb52253429fe1d693750396395c153a4aaf3 [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
6#include <common.h>
7#include <clk.h>
8#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Fabien Dessenneac0da892019-05-14 11:20:34 +020010#include <mailbox-uclass.h>
Simon Glass9bc15642020-02-03 07:36:16 -070011#include <malloc.h>
Fabien Dessenneac0da892019-05-14 11:20:34 +020012#include <asm/io.h>
Simon Glass9bc15642020-02-03 07:36:16 -070013#include <dm/device_compat.h>
Fabien Dessenneac0da892019-05-14 11:20:34 +020014
15/*
16 * IPCC has one set of registers per CPU
17 * IPCC_PROC_OFFST allows to define cpu registers set base address
18 * according to the assigned proc_id.
19 */
20
21#define IPCC_PROC_OFFST 0x010
22
23#define IPCC_XSCR 0x008
24#define IPCC_XTOYSR 0x00c
25
26#define IPCC_HWCFGR 0x3f0
27#define IPCFGR_CHAN_MASK GENMASK(7, 0)
28
29#define RX_BIT_CHAN(chan) BIT(chan)
30#define TX_BIT_SHIFT 16
31#define TX_BIT_CHAN(chan) BIT(TX_BIT_SHIFT + (chan))
32
33#define STM32_MAX_PROCS 2
34
35struct stm32_ipcc {
36 void __iomem *reg_base;
37 void __iomem *reg_proc;
38 u32 proc_id;
39 u32 n_chans;
40};
41
42static int stm32_ipcc_request(struct mbox_chan *chan)
43{
44 struct stm32_ipcc *ipcc = dev_get_priv(chan->dev);
45
46 debug("%s(chan=%p)\n", __func__, chan);
47
48 if (chan->id >= ipcc->n_chans) {
49 debug("%s failed to request channel: %ld\n",
50 __func__, chan->id);
51 return -EINVAL;
52 }
53
54 return 0;
55}
56
57static int stm32_ipcc_free(struct mbox_chan *chan)
58{
59 debug("%s(chan=%p)\n", __func__, chan);
60
61 return 0;
62}
63
64static int stm32_ipcc_send(struct mbox_chan *chan, const void *data)
65{
66 struct stm32_ipcc *ipcc = dev_get_priv(chan->dev);
67
68 debug("%s(chan=%p, data=%p)\n", __func__, chan, data);
69
70 if (readl(ipcc->reg_proc + IPCC_XTOYSR) & BIT(chan->id))
71 return -EBUSY;
72
73 /* set channel n occupied */
74 setbits_le32(ipcc->reg_proc + IPCC_XSCR, TX_BIT_CHAN(chan->id));
75
76 return 0;
77}
78
79static int stm32_ipcc_recv(struct mbox_chan *chan, void *data)
80{
81 struct stm32_ipcc *ipcc = dev_get_priv(chan->dev);
82 u32 val;
83 int proc_offset;
84
85 debug("%s(chan=%p, data=%p)\n", __func__, chan, data);
86
87 /* read 'channel occupied' status from other proc */
88 proc_offset = ipcc->proc_id ? -IPCC_PROC_OFFST : IPCC_PROC_OFFST;
89 val = readl(ipcc->reg_proc + proc_offset + IPCC_XTOYSR);
90
91 if (!(val & BIT(chan->id)))
92 return -ENODATA;
93
94 setbits_le32(ipcc->reg_proc + IPCC_XSCR, RX_BIT_CHAN(chan->id));
95
96 return 0;
97}
98
99static int stm32_ipcc_probe(struct udevice *dev)
100{
101 struct stm32_ipcc *ipcc = dev_get_priv(dev);
102 fdt_addr_t addr;
103 const fdt32_t *cell;
104 struct clk clk;
105 int len, ret;
106
107 debug("%s(dev=%p)\n", __func__, dev);
108
109 addr = dev_read_addr(dev);
110 if (addr == FDT_ADDR_T_NONE)
111 return -EINVAL;
112
113 ipcc->reg_base = (void __iomem *)addr;
114
115 /* proc_id */
116 cell = dev_read_prop(dev, "st,proc_id", &len);
117 if (len < sizeof(fdt32_t)) {
118 dev_dbg(dev, "Missing st,proc_id\n");
119 return -EINVAL;
120 }
121
122 ipcc->proc_id = fdtdec_get_number(cell, 1);
123
124 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)
137 goto clk_free;
138
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;
144
145clk_free:
146 clk_free(&clk);
147
148 return ret;
149}
150
151static const struct udevice_id stm32_ipcc_ids[] = {
152 { .compatible = "st,stm32mp1-ipcc" },
153 { }
154};
155
156struct mbox_ops stm32_ipcc_mbox_ops = {
157 .request = stm32_ipcc_request,
Simon Glass1ee48192020-02-03 07:35:50 -0700158 .rfree = stm32_ipcc_free,
Fabien Dessenneac0da892019-05-14 11:20:34 +0200159 .send = stm32_ipcc_send,
160 .recv = stm32_ipcc_recv,
161};
162
163U_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_alloc_size = sizeof(struct stm32_ipcc),
169 .ops = &stm32_ipcc_mbox_ops,
170};