blob: c927c0edf2504f42f12fc11c0830fd7ce64bd011 [file] [log] [blame]
Samuel Holland60d49282021-10-08 00:17:20 -05001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Sunxi A31 Power Management Unit
4 *
5 * (C) Copyright 2013 Oliver Schinagl <oliver@schinagl.nl>
6 * http://linux-sunxi.org
7 *
8 * Based on sun6i sources and earlier U-Boot Allwinner A10 SPL work
9 *
10 * (C) Copyright 2006-2013
11 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
12 * Berg Xing <bergxing@allwinnertech.com>
13 * Tom Cubie <tangliang@allwinnertech.com>
14 */
15
16#include <axp_pmic.h>
Samuel Hollande2802032022-03-17 23:52:34 -050017#include <clk.h>
Samuel Holland60d49282021-10-08 00:17:20 -050018#include <dm.h>
19#include <errno.h>
20#include <i2c.h>
Samuel Hollande2802032022-03-17 23:52:34 -050021#include <reset.h>
Andre Przywaraf944a612022-09-06 10:36:38 +010022#include <sunxi_gpio.h>
Samuel Holland60d49282021-10-08 00:17:20 -050023#include <time.h>
24#include <asm/io.h>
25#include <asm/arch/cpu.h>
Samuel Holland60d49282021-10-08 00:17:20 -050026#include <asm/arch/p2wi.h>
27#include <asm/arch/prcm.h>
28#include <asm/arch/sys_proto.h>
29
30static int sun6i_p2wi_await_trans(struct sunxi_p2wi_reg *base)
31{
32 unsigned long tmo = timer_get_us() + 1000000;
33 int ret;
34 u8 reg;
35
36 while (1) {
37 reg = readl(&base->status);
38 if (reg & P2WI_STAT_TRANS_ERR) {
39 ret = -EIO;
40 break;
41 }
42 if (reg & P2WI_STAT_TRANS_DONE) {
43 ret = 0;
44 break;
45 }
46 if (timer_get_us() > tmo) {
47 ret = -ETIME;
48 break;
49 }
50 }
51 writel(reg, &base->status); /* Clear status bits */
52
53 return ret;
54}
55
56static int sun6i_p2wi_read(struct sunxi_p2wi_reg *base, const u8 addr, u8 *data)
57{
58 int ret;
59
60 writel(P2WI_DATADDR_BYTE_1(addr), &base->dataddr0);
61 writel(P2WI_DATA_NUM_BYTES(1) |
62 P2WI_DATA_NUM_BYTES_READ, &base->numbytes);
63 writel(P2WI_STAT_TRANS_DONE, &base->status);
64 writel(P2WI_CTRL_TRANS_START, &base->ctrl);
65
66 ret = sun6i_p2wi_await_trans(base);
67
68 *data = readl(&base->data0) & P2WI_DATA_BYTE_1_MASK;
69
70 return ret;
71}
72
73static int sun6i_p2wi_write(struct sunxi_p2wi_reg *base, const u8 addr, u8 data)
74{
75 writel(P2WI_DATADDR_BYTE_1(addr), &base->dataddr0);
76 writel(P2WI_DATA_BYTE_1(data), &base->data0);
77 writel(P2WI_DATA_NUM_BYTES(1), &base->numbytes);
78 writel(P2WI_STAT_TRANS_DONE, &base->status);
79 writel(P2WI_CTRL_TRANS_START, &base->ctrl);
80
81 return sun6i_p2wi_await_trans(base);
82}
83
84static int sun6i_p2wi_change_to_p2wi_mode(struct sunxi_p2wi_reg *base,
85 u8 slave_addr, u8 ctrl_reg,
86 u8 init_data)
87{
88 unsigned long tmo = timer_get_us() + 1000000;
89
90 writel(P2WI_PM_DEV_ADDR(slave_addr) |
91 P2WI_PM_CTRL_ADDR(ctrl_reg) |
92 P2WI_PM_INIT_DATA(init_data) |
93 P2WI_PM_INIT_SEND,
94 &base->pm);
95
96 while ((readl(&base->pm) & P2WI_PM_INIT_SEND)) {
97 if (timer_get_us() > tmo)
98 return -ETIME;
99 }
100
101 return 0;
102}
103
104static void sun6i_p2wi_init(struct sunxi_p2wi_reg *base)
105{
Samuel Holland60d49282021-10-08 00:17:20 -0500106 /* Reset p2wi controller and set clock to CLKIN(12)/8 = 1.5 MHz */
107 writel(P2WI_CTRL_RESET, &base->ctrl);
108 sdelay(0x100);
109 writel(P2WI_CC_SDA_OUT_DELAY(1) | P2WI_CC_CLK_DIV(8),
110 &base->cc);
111}
112
113#if IS_ENABLED(CONFIG_AXP_PMIC_BUS)
114int p2wi_read(const u8 addr, u8 *data)
115{
116 struct sunxi_p2wi_reg *base = (struct sunxi_p2wi_reg *)SUN6I_P2WI_BASE;
117
118 return sun6i_p2wi_read(base, addr, data);
119}
120
121int p2wi_write(const u8 addr, u8 data)
122{
123 struct sunxi_p2wi_reg *base = (struct sunxi_p2wi_reg *)SUN6I_P2WI_BASE;
124
125 return sun6i_p2wi_write(base, addr, data);
126}
127
128int p2wi_change_to_p2wi_mode(u8 slave_addr, u8 ctrl_reg, u8 init_data)
129{
130 struct sunxi_p2wi_reg *base = (struct sunxi_p2wi_reg *)SUN6I_P2WI_BASE;
131
132 return sun6i_p2wi_change_to_p2wi_mode(base, slave_addr, ctrl_reg,
133 init_data);
134}
135
136void p2wi_init(void)
137{
138 struct sunxi_p2wi_reg *base = (struct sunxi_p2wi_reg *)SUN6I_P2WI_BASE;
139
Samuel Holland32f3b162021-10-20 23:01:29 -0500140 /* Enable p2wi and PIO clk, and de-assert their resets */
141 prcm_apb0_enable(PRCM_APB0_GATE_PIO | PRCM_APB0_GATE_P2WI);
142
143 sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN6I_GPL0_R_P2WI_SCK);
144 sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN6I_GPL1_R_P2WI_SDA);
145
Samuel Holland60d49282021-10-08 00:17:20 -0500146 sun6i_p2wi_init(base);
147}
148#endif
149
150#if CONFIG_IS_ENABLED(DM_I2C)
151struct sun6i_p2wi_priv {
152 struct sunxi_p2wi_reg *base;
153};
154
155static int sun6i_p2wi_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
156{
157 struct sun6i_p2wi_priv *priv = dev_get_priv(bus);
158
159 /* The hardware only supports SMBus-style transfers. */
160 if (nmsgs == 2 && msg[1].flags == I2C_M_RD && msg[1].len == 1)
161 return sun6i_p2wi_read(priv->base,
162 msg[0].buf[0], &msg[1].buf[0]);
163
164 if (nmsgs == 1 && msg[0].len == 2)
165 return sun6i_p2wi_write(priv->base,
166 msg[0].buf[0], msg[0].buf[1]);
167
168 return -EINVAL;
169}
170
171static int sun6i_p2wi_probe_chip(struct udevice *bus, uint chip_addr,
172 uint chip_flags)
173{
174 struct sun6i_p2wi_priv *priv = dev_get_priv(bus);
175
176 return sun6i_p2wi_change_to_p2wi_mode(priv->base, chip_addr,
177 AXP_PMIC_MODE_REG,
178 AXP_PMIC_MODE_P2WI);
179}
180
181static int sun6i_p2wi_probe(struct udevice *bus)
182{
183 struct sun6i_p2wi_priv *priv = dev_get_priv(bus);
Samuel Hollande2802032022-03-17 23:52:34 -0500184 struct reset_ctl *reset;
185 struct clk *clk;
Samuel Holland60d49282021-10-08 00:17:20 -0500186
187 priv->base = dev_read_addr_ptr(bus);
188
Samuel Hollande2802032022-03-17 23:52:34 -0500189 reset = devm_reset_control_get(bus, NULL);
190 if (!IS_ERR(reset))
191 reset_deassert(reset);
192
193 clk = devm_clk_get(bus, NULL);
194 if (!IS_ERR(clk))
195 clk_enable(clk);
196
Samuel Holland60d49282021-10-08 00:17:20 -0500197 sun6i_p2wi_init(priv->base);
198
199 return 0;
200}
201
202static int sun6i_p2wi_child_pre_probe(struct udevice *child)
203{
204 struct dm_i2c_chip *chip = dev_get_parent_plat(child);
Samuel Hollandcd5499b2022-03-17 23:52:33 -0500205 struct udevice *bus = child->parent;
Samuel Holland60d49282021-10-08 00:17:20 -0500206
207 /* Ensure each transfer is for a single register. */
208 chip->flags |= DM_I2C_CHIP_RD_ADDRESS | DM_I2C_CHIP_WR_ADDRESS;
209
Samuel Hollandcd5499b2022-03-17 23:52:33 -0500210 return sun6i_p2wi_probe_chip(bus, chip->chip_addr, 0);
Samuel Holland60d49282021-10-08 00:17:20 -0500211}
212
213static const struct dm_i2c_ops sun6i_p2wi_ops = {
214 .xfer = sun6i_p2wi_xfer,
215 .probe_chip = sun6i_p2wi_probe_chip,
216};
217
218static const struct udevice_id sun6i_p2wi_ids[] = {
219 { .compatible = "allwinner,sun6i-a31-p2wi" },
220 { /* sentinel */ }
221};
222
223U_BOOT_DRIVER(sun6i_p2wi) = {
224 .name = "sun6i_p2wi",
225 .id = UCLASS_I2C,
226 .of_match = sun6i_p2wi_ids,
227 .probe = sun6i_p2wi_probe,
228 .child_pre_probe = sun6i_p2wi_child_pre_probe,
229 .priv_auto = sizeof(struct sun6i_p2wi_priv),
230 .ops = &sun6i_p2wi_ops,
231};
232#endif /* CONFIG_IS_ENABLED(DM_I2C) */