blob: aca8de9436908394966286646ebff1679b208a66 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Songjun Wu8800e0f2016-06-20 13:22:38 +08002/*
3 * Atmel I2C driver.
4 *
5 * (C) Copyright 2016 Songjun Wu <songjun.wu@atmel.com>
Songjun Wu8800e0f2016-06-20 13:22:38 +08006 */
7
Simon Glass336d4612020-02-03 07:36:16 -07008#include <malloc.h>
Simon Glass401d1c42020-10-30 21:38:53 -06009#include <asm/global_data.h>
Songjun Wu8800e0f2016-06-20 13:22:38 +080010#include <asm/io.h>
11#include <common.h>
Wenyou Yang76062b92016-09-13 10:40:31 +080012#include <clk.h>
Songjun Wu8800e0f2016-06-20 13:22:38 +080013#include <dm.h>
14#include <errno.h>
15#include <fdtdec.h>
16#include <i2c.h>
17#include <linux/bitops.h>
18#include <mach/clk.h>
19
20#include "at91_i2c.h"
21
22DECLARE_GLOBAL_DATA_PTR;
23
24#define I2C_TIMEOUT_MS 100
25
26static int at91_wait_for_xfer(struct at91_i2c_bus *bus, u32 status)
27{
28 struct at91_i2c_regs *reg = bus->regs;
29 ulong start_time = get_timer(0);
30 u32 sr;
31
32 bus->status = 0;
33
34 do {
35 sr = readl(&reg->sr);
36 bus->status |= sr;
37
38 if (sr & TWI_SR_NACK)
39 return -EREMOTEIO;
40 else if (sr & status)
41 return 0;
42 } while (get_timer(start_time) < I2C_TIMEOUT_MS);
43
44 return -ETIMEDOUT;
45}
46
47static int at91_i2c_xfer_msg(struct at91_i2c_bus *bus, struct i2c_msg *msg)
48{
49 struct at91_i2c_regs *reg = bus->regs;
50 bool is_read = msg->flags & I2C_M_RD;
51 u32 i;
52 int ret = 0;
53
54 readl(&reg->sr);
55 if (is_read) {
56 writel(TWI_CR_START, &reg->cr);
57
58 for (i = 0; !ret && i < (msg->len - 1); i++) {
59 ret = at91_wait_for_xfer(bus, TWI_SR_RXRDY);
60 msg->buf[i] = readl(&reg->rhr);
61 }
62
63 if (ret)
64 goto error;
65
66 writel(TWI_CR_STOP, &reg->cr);
67
68 ret = at91_wait_for_xfer(bus, TWI_SR_RXRDY);
69 if (ret)
70 goto error;
71
72 msg->buf[i] = readl(&reg->rhr);
73
74 } else {
75 writel(msg->buf[0], &reg->thr);
Alan Ott0afbb0e2017-11-28 22:25:23 -050076 ret = at91_wait_for_xfer(bus, TWI_SR_TXRDY);
77
Songjun Wu8800e0f2016-06-20 13:22:38 +080078 for (i = 1; !ret && (i < msg->len); i++) {
79 writel(msg->buf[i], &reg->thr);
80 ret = at91_wait_for_xfer(bus, TWI_SR_TXRDY);
81 }
82
83 if (ret)
84 goto error;
85
86 writel(TWI_CR_STOP, &reg->cr);
87 }
88
89 if (!ret)
90 ret = at91_wait_for_xfer(bus, TWI_SR_TXCOMP);
91
92 if (ret)
93 goto error;
94
95 if (bus->status & (TWI_SR_OVRE | TWI_SR_UNRE | TWI_SR_LOCK)) {
96 ret = -EIO;
97 goto error;
98 }
99
100 return 0;
101
102error:
103 if (bus->status & TWI_SR_LOCK)
104 writel(TWI_CR_LOCKCLR, &reg->cr);
105
106 return ret;
107}
108
109static int at91_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
110{
111 struct at91_i2c_bus *bus = dev_get_priv(dev);
112 struct at91_i2c_regs *reg = bus->regs;
113 struct i2c_msg *m_start = msg;
114 bool is_read;
115 u32 int_addr_flag = 0;
116 int ret = 0;
117
118 if (nmsgs == 2) {
119 int internal_address = 0;
120 int i;
121
122 /* 1st msg is put into the internal address, start with 2nd */
123 m_start = &msg[1];
124
125 /* the max length of internal address is 3 bytes */
126 if (msg->len > 3)
127 return -EFAULT;
128
129 for (i = 0; i < msg->len; ++i) {
130 const unsigned addr = msg->buf[msg->len - 1 - i];
131
132 internal_address |= addr << (8 * i);
133 int_addr_flag += TWI_MMR_IADRSZ_1;
134 }
135
136 writel(internal_address, &reg->iadr);
137 }
138
139 is_read = m_start->flags & I2C_M_RD;
140
141 writel((m_start->addr << 16) | int_addr_flag |
142 (is_read ? TWI_MMR_MREAD : 0), &reg->mmr);
143
144 ret = at91_i2c_xfer_msg(bus, m_start);
145
146 return ret;
147}
148
149/*
150 * Calculate symmetric clock as stated in datasheet:
151 * twi_clk = F_MAIN / (2 * (cdiv * (1 << ckdiv) + offset))
152 */
153static void at91_calc_i2c_clock(struct udevice *dev, int i2c_clk)
154{
155 struct at91_i2c_bus *bus = dev_get_priv(dev);
156 const struct at91_i2c_pdata *pdata = bus->pdata;
157 int offset = pdata->clk_offset;
158 int max_ckdiv = pdata->clk_max_div;
159 int ckdiv, cdiv, div;
160 unsigned long src_rate;
161
162 src_rate = bus->bus_clk_rate;
163
164 div = max(0, (int)DIV_ROUND_UP(src_rate, 2 * i2c_clk) - offset);
165 ckdiv = fls(div >> 8);
166 cdiv = div >> ckdiv;
167
168 if (ckdiv > max_ckdiv) {
169 ckdiv = max_ckdiv;
170 cdiv = 255;
171 }
172
173 bus->speed = DIV_ROUND_UP(src_rate,
174 (cdiv * (1 << ckdiv) + offset) * 2);
175
176 bus->cwgr_val = (ckdiv << 16) | (cdiv << 8) | cdiv;
177}
178
179static int at91_i2c_enable_clk(struct udevice *dev)
180{
181 struct at91_i2c_bus *bus = dev_get_priv(dev);
Songjun Wu8800e0f2016-06-20 13:22:38 +0800182 struct clk clk;
183 ulong clk_rate;
Songjun Wu8800e0f2016-06-20 13:22:38 +0800184 int ret;
185
186 ret = clk_get_by_index(dev, 0, &clk);
187 if (ret)
188 return -EINVAL;
189
Songjun Wu8800e0f2016-06-20 13:22:38 +0800190 ret = clk_enable(&clk);
191 if (ret)
192 return ret;
193
Songjun Wu8800e0f2016-06-20 13:22:38 +0800194 clk_rate = clk_get_rate(&clk);
195 if (!clk_rate)
Wenyou Yang52f37332016-09-27 11:00:32 +0800196 return -EINVAL;
Songjun Wu8800e0f2016-06-20 13:22:38 +0800197
198 bus->bus_clk_rate = clk_rate;
199
200 clk_free(&clk);
201
202 return 0;
203}
204
Songjun Wu8800e0f2016-06-20 13:22:38 +0800205static int at91_i2c_set_bus_speed(struct udevice *dev, unsigned int speed)
206{
207 struct at91_i2c_bus *bus = dev_get_priv(dev);
208
209 at91_calc_i2c_clock(dev, speed);
210
211 writel(bus->cwgr_val, &bus->regs->cwgr);
212
213 return 0;
214}
215
216int at91_i2c_get_bus_speed(struct udevice *dev)
217{
218 struct at91_i2c_bus *bus = dev_get_priv(dev);
219
220 return bus->speed;
221}
222
Simon Glassd1998a92020-12-03 16:55:21 -0700223static int at91_i2c_of_to_plat(struct udevice *dev)
Songjun Wu8800e0f2016-06-20 13:22:38 +0800224{
225 const void *blob = gd->fdt_blob;
226 struct at91_i2c_bus *bus = dev_get_priv(dev);
Simon Glasse160f7d2017-01-17 16:52:55 -0700227 int node = dev_of_offset(dev);
Songjun Wu8800e0f2016-06-20 13:22:38 +0800228
Masahiro Yamada8613c8d2020-07-17 14:36:46 +0900229 bus->regs = dev_read_addr_ptr(dev);
Songjun Wu8800e0f2016-06-20 13:22:38 +0800230 bus->pdata = (struct at91_i2c_pdata *)dev_get_driver_data(dev);
231 bus->clock_frequency = fdtdec_get_int(blob, node,
232 "clock-frequency", 100000);
233
234 return 0;
235}
236
237static const struct dm_i2c_ops at91_i2c_ops = {
238 .xfer = at91_i2c_xfer,
Songjun Wu8800e0f2016-06-20 13:22:38 +0800239 .set_bus_speed = at91_i2c_set_bus_speed,
240 .get_bus_speed = at91_i2c_get_bus_speed,
241};
242
Wenyou.Yang@microchip.com0bc8f642017-07-31 09:56:27 +0800243static int at91_i2c_probe(struct udevice *dev)
244{
245 struct at91_i2c_bus *bus = dev_get_priv(dev);
246 struct at91_i2c_regs *reg = bus->regs;
247 int ret;
248
249 ret = at91_i2c_enable_clk(dev);
250 if (ret)
251 return ret;
252
253 writel(TWI_CR_SWRST, &reg->cr);
254
255 at91_calc_i2c_clock(dev, bus->clock_frequency);
256
257 writel(bus->cwgr_val, &reg->cwgr);
258 writel(TWI_CR_MSEN, &reg->cr);
259 writel(TWI_CR_SVDIS, &reg->cr);
260
261 return 0;
262}
263
Songjun Wu8800e0f2016-06-20 13:22:38 +0800264static const struct at91_i2c_pdata at91rm9200_config = {
265 .clk_max_div = 5,
266 .clk_offset = 3,
267};
268
269static const struct at91_i2c_pdata at91sam9261_config = {
270 .clk_max_div = 5,
271 .clk_offset = 4,
272};
273
274static const struct at91_i2c_pdata at91sam9260_config = {
275 .clk_max_div = 7,
276 .clk_offset = 4,
277};
278
279static const struct at91_i2c_pdata at91sam9g20_config = {
280 .clk_max_div = 7,
281 .clk_offset = 4,
282};
283
284static const struct at91_i2c_pdata at91sam9g10_config = {
285 .clk_max_div = 7,
286 .clk_offset = 4,
287};
288
289static const struct at91_i2c_pdata at91sam9x5_config = {
290 .clk_max_div = 7,
291 .clk_offset = 4,
292};
293
294static const struct at91_i2c_pdata sama5d4_config = {
295 .clk_max_div = 7,
296 .clk_offset = 4,
297};
298
299static const struct at91_i2c_pdata sama5d2_config = {
300 .clk_max_div = 7,
301 .clk_offset = 3,
302};
303
304static const struct udevice_id at91_i2c_ids[] = {
305{ .compatible = "atmel,at91rm9200-i2c", .data = (long)&at91rm9200_config },
306{ .compatible = "atmel,at91sam9260-i2c", .data = (long)&at91sam9260_config },
307{ .compatible = "atmel,at91sam9261-i2c", .data = (long)&at91sam9261_config },
308{ .compatible = "atmel,at91sam9g20-i2c", .data = (long)&at91sam9g20_config },
309{ .compatible = "atmel,at91sam9g10-i2c", .data = (long)&at91sam9g10_config },
310{ .compatible = "atmel,at91sam9x5-i2c", .data = (long)&at91sam9x5_config },
311{ .compatible = "atmel,sama5d4-i2c", .data = (long)&sama5d4_config },
312{ .compatible = "atmel,sama5d2-i2c", .data = (long)&sama5d2_config },
313{ }
314};
315
316U_BOOT_DRIVER(i2c_at91) = {
317 .name = "i2c_at91",
318 .id = UCLASS_I2C,
319 .of_match = at91_i2c_ids,
Wenyou.Yang@microchip.com0bc8f642017-07-31 09:56:27 +0800320 .probe = at91_i2c_probe,
Simon Glassd1998a92020-12-03 16:55:21 -0700321 .of_to_plat = at91_i2c_of_to_plat,
Simon Glass41575d82020-12-03 16:55:17 -0700322 .per_child_auto = sizeof(struct dm_i2c_chip),
323 .priv_auto = sizeof(struct at91_i2c_bus),
Songjun Wu8800e0f2016-06-20 13:22:38 +0800324 .ops = &at91_i2c_ops,
325};