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