blob: babcb09f87688bc467e1e17a97645af877110bf9 [file] [log] [blame]
Peng Fanef64e782018-10-18 14:28:11 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright 2018 NXP
4 *
5 * Peng Fan <peng.fan@nxp.com>
6 */
7
8#include <common.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Peng Fanef64e782018-10-18 14:28:11 +020010#include <asm/io.h>
11#include <dm.h>
12#include <dm/lists.h>
13#include <dm/root.h>
14#include <dm/device-internal.h>
15#include <asm/arch/sci/sci.h>
16#include <linux/iopoll.h>
17#include <misc.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20
21struct mu_type {
22 u32 tr[4];
23 u32 rr[4];
24 u32 sr;
25 u32 cr;
26};
27
28struct imx8_scu {
29 struct mu_type *base;
Peng Fanef64e782018-10-18 14:28:11 +020030};
31
32#define MU_CR_GIE_MASK 0xF0000000u
33#define MU_CR_RIE_MASK 0xF000000u
34#define MU_CR_GIR_MASK 0xF0000u
35#define MU_CR_TIE_MASK 0xF00000u
36#define MU_CR_F_MASK 0x7u
37#define MU_SR_TE0_MASK BIT(23)
38#define MU_SR_RF0_MASK BIT(27)
39#define MU_TR_COUNT 4
40#define MU_RR_COUNT 4
41
42static inline void mu_hal_init(struct mu_type *base)
43{
44 /* Clear GIEn, RIEn, TIEn, GIRn and ABFn. */
45 clrbits_le32(&base->cr, MU_CR_GIE_MASK | MU_CR_RIE_MASK |
46 MU_CR_TIE_MASK | MU_CR_GIR_MASK | MU_CR_F_MASK);
47}
48
49static int mu_hal_sendmsg(struct mu_type *base, u32 reg_index, u32 msg)
50{
51 u32 mask = MU_SR_TE0_MASK >> reg_index;
52 u32 val;
53 int ret;
54
55 assert(reg_index < MU_TR_COUNT);
56
57 /* Wait TX register to be empty. */
58 ret = readl_poll_timeout(&base->sr, val, val & mask, 10000);
59 if (ret < 0) {
60 printf("%s timeout\n", __func__);
61 return -ETIMEDOUT;
62 }
63
64 writel(msg, &base->tr[reg_index]);
65
66 return 0;
67}
68
69static int mu_hal_receivemsg(struct mu_type *base, u32 reg_index, u32 *msg)
70{
71 u32 mask = MU_SR_RF0_MASK >> reg_index;
72 u32 val;
73 int ret;
74
75 assert(reg_index < MU_TR_COUNT);
76
77 /* Wait RX register to be full. */
Ye Li77ed80c2020-05-03 22:31:47 +080078 ret = readl_poll_timeout(&base->sr, val, val & mask, 1000000);
Peng Fanef64e782018-10-18 14:28:11 +020079 if (ret < 0) {
80 printf("%s timeout\n", __func__);
81 return -ETIMEDOUT;
82 }
83
84 *msg = readl(&base->rr[reg_index]);
85
86 return 0;
87}
88
89static int sc_ipc_read(struct mu_type *base, void *data)
90{
91 struct sc_rpc_msg_s *msg = (struct sc_rpc_msg_s *)data;
92 int ret;
93 u8 count = 0;
94
95 if (!msg)
96 return -EINVAL;
97
98 /* Read first word */
99 ret = mu_hal_receivemsg(base, 0, (u32 *)msg);
100 if (ret)
101 return ret;
102 count++;
103
104 /* Check size */
105 if (msg->size > SC_RPC_MAX_MSG) {
106 *((u32 *)msg) = 0;
107 return -EINVAL;
108 }
109
110 /* Read remaining words */
111 while (count < msg->size) {
112 ret = mu_hal_receivemsg(base, count % MU_RR_COUNT,
113 &msg->DATA.u32[count - 1]);
114 if (ret)
115 return ret;
116 count++;
117 }
118
119 return 0;
120}
121
122static int sc_ipc_write(struct mu_type *base, void *data)
123{
124 struct sc_rpc_msg_s *msg = (struct sc_rpc_msg_s *)data;
125 int ret;
126 u8 count = 0;
127
128 if (!msg)
129 return -EINVAL;
130
131 /* Check size */
132 if (msg->size > SC_RPC_MAX_MSG)
133 return -EINVAL;
134
135 /* Write first word */
136 ret = mu_hal_sendmsg(base, 0, *((u32 *)msg));
137 if (ret)
138 return ret;
139 count++;
140
141 /* Write remaining words */
142 while (count < msg->size) {
143 ret = mu_hal_sendmsg(base, count % MU_TR_COUNT,
144 msg->DATA.u32[count - 1]);
145 if (ret)
146 return ret;
147 count++;
148 }
149
150 return 0;
151}
152
153/*
154 * Note the function prototype use msgid as the 2nd parameter, here
155 * we take it as no_resp.
156 */
157static int imx8_scu_call(struct udevice *dev, int no_resp, void *tx_msg,
158 int tx_size, void *rx_msg, int rx_size)
159{
Peng Fan026381f2018-12-15 12:19:52 +0000160 struct imx8_scu *plat = dev_get_platdata(dev);
Peng Fanef64e782018-10-18 14:28:11 +0200161 sc_err_t result;
162 int ret;
163
164 /* Expect tx_msg, rx_msg are the same value */
165 if (rx_msg && tx_msg != rx_msg)
166 printf("tx_msg %p, rx_msg %p\n", tx_msg, rx_msg);
167
Peng Fan026381f2018-12-15 12:19:52 +0000168 ret = sc_ipc_write(plat->base, tx_msg);
Peng Fanef64e782018-10-18 14:28:11 +0200169 if (ret)
170 return ret;
171 if (!no_resp) {
Peng Fan026381f2018-12-15 12:19:52 +0000172 ret = sc_ipc_read(plat->base, rx_msg);
Peng Fanef64e782018-10-18 14:28:11 +0200173 if (ret)
174 return ret;
175 }
176
177 result = RPC_R8((struct sc_rpc_msg_s *)tx_msg);
178
179 return sc_err_to_linux(result);
180}
181
182static int imx8_scu_probe(struct udevice *dev)
183{
Peng Fan026381f2018-12-15 12:19:52 +0000184 struct imx8_scu *plat = dev_get_platdata(dev);
Peng Fanef64e782018-10-18 14:28:11 +0200185 fdt_addr_t addr;
186
Peng Fan026381f2018-12-15 12:19:52 +0000187 debug("%s(dev=%p) (plat=%p)\n", __func__, dev, plat);
Peng Fanef64e782018-10-18 14:28:11 +0200188
189 addr = devfdt_get_addr(dev);
190 if (addr == FDT_ADDR_T_NONE)
191 return -EINVAL;
192
Peng Fan04b24962018-12-21 06:21:15 +0000193#ifdef CONFIG_SPL_BUILD
194 plat->base = (struct mu_type *)CONFIG_MU_BASE_SPL;
195#else
Peng Fan026381f2018-12-15 12:19:52 +0000196 plat->base = (struct mu_type *)addr;
Peng Fan04b24962018-12-21 06:21:15 +0000197#endif
Peng Fanef64e782018-10-18 14:28:11 +0200198
199 /* U-Boot not enable interrupts, so need to enable RX interrupts */
Peng Fan026381f2018-12-15 12:19:52 +0000200 mu_hal_init(plat->base);
Peng Fanef64e782018-10-18 14:28:11 +0200201
202 gd->arch.scu_dev = dev;
203
Peng Fanef64e782018-10-18 14:28:11 +0200204 return 0;
205}
206
207static int imx8_scu_remove(struct udevice *dev)
208{
209 return 0;
210}
211
212static int imx8_scu_bind(struct udevice *dev)
213{
Peng Fanef64e782018-10-18 14:28:11 +0200214 int ret;
215 struct udevice *child;
Peng Fan816d0932019-09-02 10:20:17 +0000216 ofnode node;
Peng Fanef64e782018-10-18 14:28:11 +0200217
218 debug("%s(dev=%p)\n", __func__, dev);
Peng Fan816d0932019-09-02 10:20:17 +0000219 ofnode_for_each_subnode(node, dev_ofnode(dev)) {
220 ret = lists_bind_fdt(dev, node, &child, true);
221 if (ret)
222 return ret;
223 debug("bind child dev %s\n", child->name);
224 }
Peng Fanef64e782018-10-18 14:28:11 +0200225
226 return 0;
227}
228
229static struct misc_ops imx8_scu_ops = {
230 .call = imx8_scu_call,
231};
232
233static const struct udevice_id imx8_scu_ids[] = {
234 { .compatible = "fsl,imx8qxp-mu" },
235 { .compatible = "fsl,imx8-mu" },
236 { }
237};
238
239U_BOOT_DRIVER(imx8_scu) = {
240 .name = "imx8_scu",
241 .id = UCLASS_MISC,
242 .of_match = imx8_scu_ids,
243 .probe = imx8_scu_probe,
244 .bind = imx8_scu_bind,
245 .remove = imx8_scu_remove,
246 .ops = &imx8_scu_ops,
Peng Fan026381f2018-12-15 12:19:52 +0000247 .platdata_auto_alloc_size = sizeof(struct imx8_scu),
Peng Fanef64e782018-10-18 14:28:11 +0200248 .flags = DM_FLAG_PRE_RELOC,
249};