blob: 0dea8f7a92301220dd64fc1343fc56457a5462e3 [file] [log] [blame]
Samuel Hollandb348efb2021-10-08 00:17:21 -05001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2014 Hans de Goede <hdegoede@redhat.com>
4 *
5 * Based on allwinner u-boot sources rsb code which is:
6 * (C) Copyright 2007-2013
7 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
8 * lixiang <lixiang@allwinnertech.com>
9 */
10
11#include <axp_pmic.h>
12#include <common.h>
13#include <dm.h>
14#include <errno.h>
15#include <i2c.h>
16#include <time.h>
17#include <asm/arch/cpu.h>
18#include <asm/arch/gpio.h>
19#include <asm/arch/prcm.h>
20#include <asm/arch/rsb.h>
21
22static int sun8i_rsb_await_trans(struct sunxi_rsb_reg *base)
23{
24 unsigned long tmo = timer_get_us() + 1000000;
25 u32 stat;
26 int ret;
27
28 while (1) {
29 stat = readl(&base->stat);
30 if (stat & RSB_STAT_LBSY_INT) {
31 ret = -EBUSY;
32 break;
33 }
34 if (stat & RSB_STAT_TERR_INT) {
35 ret = -EIO;
36 break;
37 }
38 if (stat & RSB_STAT_TOVER_INT) {
39 ret = 0;
40 break;
41 }
42 if (timer_get_us() > tmo) {
43 ret = -ETIME;
44 break;
45 }
46 }
47 writel(stat, &base->stat); /* Clear status bits */
48
49 return ret;
50}
51
52static int sun8i_rsb_do_trans(struct sunxi_rsb_reg *base)
53{
54 setbits_le32(&base->ctrl, RSB_CTRL_START_TRANS);
55
56 return sun8i_rsb_await_trans(base);
57}
58
59static int sun8i_rsb_read(struct sunxi_rsb_reg *base, u16 runtime_addr,
60 u8 reg_addr, u8 *data)
61{
62 int ret;
63
64 writel(RSB_DEVADDR_RUNTIME_ADDR(runtime_addr), &base->devaddr);
65 writel(reg_addr, &base->addr);
66 writel(RSB_CMD_BYTE_READ, &base->cmd);
67
68 ret = sun8i_rsb_do_trans(base);
69 if (ret)
70 return ret;
71
72 *data = readl(&base->data) & 0xff;
73
74 return 0;
75}
76
77static int sun8i_rsb_write(struct sunxi_rsb_reg *base, u16 runtime_addr,
78 u8 reg_addr, u8 data)
79{
80 writel(RSB_DEVADDR_RUNTIME_ADDR(runtime_addr), &base->devaddr);
81 writel(reg_addr, &base->addr);
82 writel(data, &base->data);
83 writel(RSB_CMD_BYTE_WRITE, &base->cmd);
84
85 return sun8i_rsb_do_trans(base);
86}
87
88static int sun8i_rsb_set_device_address(struct sunxi_rsb_reg *base,
89 u16 device_addr, u16 runtime_addr)
90{
91 writel(RSB_DEVADDR_RUNTIME_ADDR(runtime_addr) |
92 RSB_DEVADDR_DEVICE_ADDR(device_addr), &base->devaddr);
93 writel(RSB_CMD_SET_RTSADDR, &base->cmd);
94
95 return sun8i_rsb_do_trans(base);
96}
97
Samuel Hollandb348efb2021-10-08 00:17:21 -050098static void sun8i_rsb_set_clk(struct sunxi_rsb_reg *base)
99{
100 u32 div = 0;
101 u32 cd_odly = 0;
102
103 /* Source is Hosc24M, set RSB clk to 3Mhz */
104 div = 24000000 / 3000000 / 2 - 1;
105 cd_odly = div >> 1;
106 if (!cd_odly)
107 cd_odly = 1;
108
109 writel((cd_odly << 8) | div, &base->ccr);
110}
111
112static int sun8i_rsb_set_device_mode(struct sunxi_rsb_reg *base)
113{
114 unsigned long tmo = timer_get_us() + 1000000;
115
116 writel(RSB_DMCR_DEVICE_MODE_START | RSB_DMCR_DEVICE_MODE_DATA,
117 &base->dmcr);
118
119 while (readl(&base->dmcr) & RSB_DMCR_DEVICE_MODE_START) {
120 if (timer_get_us() > tmo)
121 return -ETIME;
122 }
123
124 return sun8i_rsb_await_trans(base);
125}
126
127static int sun8i_rsb_init(struct sunxi_rsb_reg *base)
128{
Samuel Hollandb348efb2021-10-08 00:17:21 -0500129 writel(RSB_CTRL_SOFT_RST, &base->ctrl);
130 sun8i_rsb_set_clk(base);
131
132 return sun8i_rsb_set_device_mode(base);
133}
134
135#if IS_ENABLED(CONFIG_AXP_PMIC_BUS)
136int rsb_read(const u16 runtime_addr, const u8 reg_addr, u8 *data)
137{
138 struct sunxi_rsb_reg *base = (struct sunxi_rsb_reg *)SUNXI_RSB_BASE;
139
140 return sun8i_rsb_read(base, runtime_addr, reg_addr, data);
141}
142
143int rsb_write(const u16 runtime_addr, const u8 reg_addr, u8 data)
144{
145 struct sunxi_rsb_reg *base = (struct sunxi_rsb_reg *)SUNXI_RSB_BASE;
146
147 return sun8i_rsb_write(base, runtime_addr, reg_addr, data);
148}
149
150int rsb_set_device_address(u16 device_addr, u16 runtime_addr)
151{
152 struct sunxi_rsb_reg *base = (struct sunxi_rsb_reg *)SUNXI_RSB_BASE;
153
154 return sun8i_rsb_set_device_address(base, device_addr, runtime_addr);
155}
156
157int rsb_init(void)
158{
159 struct sunxi_rsb_reg *base = (struct sunxi_rsb_reg *)SUNXI_RSB_BASE;
160
Samuel Hollandab85f7d2021-10-20 23:01:29 -0500161 /* Enable RSB and PIO clk, and de-assert their resets */
162 prcm_apb0_enable(PRCM_APB0_GATE_PIO | PRCM_APB0_GATE_RSB);
163
164 if (IS_ENABLED(CONFIG_MACH_SUN9I)) {
165 sunxi_gpio_set_cfgpin(SUNXI_GPN(0), SUN9I_GPN_R_RSB);
166 sunxi_gpio_set_cfgpin(SUNXI_GPN(1), SUN9I_GPN_R_RSB);
167 sunxi_gpio_set_pull(SUNXI_GPN(0), 1);
168 sunxi_gpio_set_pull(SUNXI_GPN(1), 1);
169 sunxi_gpio_set_drv(SUNXI_GPN(0), 2);
170 sunxi_gpio_set_drv(SUNXI_GPN(1), 2);
171 } else {
172 sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN8I_GPL_R_RSB);
173 sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN8I_GPL_R_RSB);
174 sunxi_gpio_set_pull(SUNXI_GPL(0), 1);
175 sunxi_gpio_set_pull(SUNXI_GPL(1), 1);
176 sunxi_gpio_set_drv(SUNXI_GPL(0), 2);
177 sunxi_gpio_set_drv(SUNXI_GPL(1), 2);
178 }
179
Samuel Hollandb348efb2021-10-08 00:17:21 -0500180 return sun8i_rsb_init(base);
181}
182#endif
183
184#if CONFIG_IS_ENABLED(DM_I2C)
185struct sun8i_rsb_priv {
186 struct sunxi_rsb_reg *base;
187};
188
189/*
190 * The mapping from hardware address to runtime address is fixed, and shared
191 * among all RSB drivers. See the comment in drivers/bus/sunxi-rsb.c in Linux.
192 */
193static int sun8i_rsb_get_runtime_address(u16 device_addr)
194{
195 if (device_addr == AXP_PMIC_PRI_DEVICE_ADDR)
196 return AXP_PMIC_PRI_RUNTIME_ADDR;
197 if (device_addr == AXP_PMIC_SEC_DEVICE_ADDR)
198 return AXP_PMIC_SEC_RUNTIME_ADDR;
199
200 return -ENXIO;
201}
202
203static int sun8i_rsb_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
204{
205 int runtime_addr = sun8i_rsb_get_runtime_address(msg->addr);
206 struct sun8i_rsb_priv *priv = dev_get_priv(bus);
207
208 if (runtime_addr < 0)
209 return runtime_addr;
210
211 /* The hardware only supports SMBus-style transfers. */
212 if (nmsgs == 2 && msg[1].flags == I2C_M_RD && msg[1].len == 1)
213 return sun8i_rsb_read(priv->base, runtime_addr,
214 msg[0].buf[0], &msg[1].buf[0]);
215
216 if (nmsgs == 1 && msg[0].len == 2)
217 return sun8i_rsb_write(priv->base, runtime_addr,
218 msg[0].buf[0], msg[0].buf[1]);
219
220 return -EINVAL;
221}
222
223static int sun8i_rsb_probe_chip(struct udevice *bus, uint chip_addr,
224 uint chip_flags)
225{
226 int runtime_addr = sun8i_rsb_get_runtime_address(chip_addr);
227 struct sun8i_rsb_priv *priv = dev_get_priv(bus);
228
229 if (runtime_addr < 0)
230 return runtime_addr;
231
232 return sun8i_rsb_set_device_address(priv->base, chip_addr, runtime_addr);
233}
234
235static int sun8i_rsb_probe(struct udevice *bus)
236{
237 struct sun8i_rsb_priv *priv = dev_get_priv(bus);
238
239 priv->base = dev_read_addr_ptr(bus);
240
241 return sun8i_rsb_init(priv->base);
242}
243
244static int sun8i_rsb_child_pre_probe(struct udevice *child)
245{
246 struct dm_i2c_chip *chip = dev_get_parent_plat(child);
247
248 /* Ensure each transfer is for a single register. */
249 chip->flags |= DM_I2C_CHIP_RD_ADDRESS | DM_I2C_CHIP_WR_ADDRESS;
250
251 return 0;
252}
253
254static const struct dm_i2c_ops sun8i_rsb_ops = {
255 .xfer = sun8i_rsb_xfer,
256 .probe_chip = sun8i_rsb_probe_chip,
257};
258
259static const struct udevice_id sun8i_rsb_ids[] = {
260 { .compatible = "allwinner,sun8i-a23-rsb" },
261 { /* sentinel */ }
262};
263
264U_BOOT_DRIVER(sun8i_rsb) = {
265 .name = "sun8i_rsb",
266 .id = UCLASS_I2C,
267 .of_match = sun8i_rsb_ids,
268 .probe = sun8i_rsb_probe,
269 .child_pre_probe = sun8i_rsb_child_pre_probe,
270 .priv_auto = sizeof(struct sun8i_rsb_priv),
271 .ops = &sun8i_rsb_ops,
272};
273#endif /* CONFIG_IS_ENABLED(DM_I2C) */