1 // SPDX-License-Identifier: GPL-2.0+
9 #include <asm/global_data.h>
17 #include <linux/bitops.h>
22 DECLARE_GLOBAL_DATA_PTR;
24 #define I2C_TIMEOUT_MS 100
26 static int at91_wait_for_xfer(struct at91_i2c_bus *bus, u32 status)
28 struct at91_i2c_regs *reg = bus->regs;
29 ulong start_time = get_timer(0);
42 } while (get_timer(start_time) < I2C_TIMEOUT_MS);
47 static int at91_i2c_xfer_msg(struct at91_i2c_bus *bus, struct i2c_msg *msg)
49 struct at91_i2c_regs *reg = bus->regs;
50 bool is_read = msg->flags & I2C_M_RD;
56 writel(TWI_CR_START, ®->cr);
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(®->rhr);
66 writel(TWI_CR_STOP, ®->cr);
68 ret = at91_wait_for_xfer(bus, TWI_SR_RXRDY);
72 msg->buf[i] = readl(®->rhr);
75 writel(msg->buf[0], ®->thr);
76 ret = at91_wait_for_xfer(bus, TWI_SR_TXRDY);
78 for (i = 1; !ret && (i < msg->len); i++) {
79 writel(msg->buf[i], ®->thr);
80 ret = at91_wait_for_xfer(bus, TWI_SR_TXRDY);
86 writel(TWI_CR_STOP, ®->cr);
90 ret = at91_wait_for_xfer(bus, TWI_SR_TXCOMP);
95 if (bus->status & (TWI_SR_OVRE | TWI_SR_UNRE | TWI_SR_LOCK)) {
103 if (bus->status & TWI_SR_LOCK)
104 writel(TWI_CR_LOCKCLR, ®->cr);
109 static int at91_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
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;
115 u32 int_addr_flag = 0;
119 int internal_address = 0;
122 /* 1st msg is put into the internal address, start with 2nd */
125 /* the max length of internal address is 3 bytes */
129 for (i = 0; i < msg->len; ++i) {
130 const unsigned addr = msg->buf[msg->len - 1 - i];
132 internal_address |= addr << (8 * i);
133 int_addr_flag += TWI_MMR_IADRSZ_1;
136 writel(internal_address, ®->iadr);
139 is_read = m_start->flags & I2C_M_RD;
141 writel((m_start->addr << 16) | int_addr_flag |
142 (is_read ? TWI_MMR_MREAD : 0), ®->mmr);
144 ret = at91_i2c_xfer_msg(bus, m_start);
150 * Calculate symmetric clock as stated in datasheet:
151 * twi_clk = F_MAIN / (2 * (cdiv * (1 << ckdiv) + offset))
153 static void at91_calc_i2c_clock(struct udevice *dev, int i2c_clk)
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;
162 src_rate = bus->bus_clk_rate;
164 div = max(0, (int)DIV_ROUND_UP(src_rate, 2 * i2c_clk) - offset);
165 ckdiv = fls(div >> 8);
168 if (ckdiv > max_ckdiv) {
173 bus->speed = DIV_ROUND_UP(src_rate,
174 (cdiv * (1 << ckdiv) + offset) * 2);
176 bus->cwgr_val = (ckdiv << 16) | (cdiv << 8) | cdiv;
179 static int at91_i2c_enable_clk(struct udevice *dev)
181 struct at91_i2c_bus *bus = dev_get_priv(dev);
186 ret = clk_get_by_index(dev, 0, &clk);
190 ret = clk_enable(&clk);
194 clk_rate = clk_get_rate(&clk);
198 bus->bus_clk_rate = clk_rate;
205 static int at91_i2c_set_bus_speed(struct udevice *dev, unsigned int speed)
207 struct at91_i2c_bus *bus = dev_get_priv(dev);
209 at91_calc_i2c_clock(dev, speed);
211 writel(bus->cwgr_val, &bus->regs->cwgr);
216 int at91_i2c_get_bus_speed(struct udevice *dev)
218 struct at91_i2c_bus *bus = dev_get_priv(dev);
223 static int at91_i2c_of_to_plat(struct udevice *dev)
225 const void *blob = gd->fdt_blob;
226 struct at91_i2c_bus *bus = dev_get_priv(dev);
227 int node = dev_of_offset(dev);
229 bus->regs = dev_read_addr_ptr(dev);
230 bus->pdata = (struct at91_i2c_pdata *)dev_get_driver_data(dev);
231 bus->clock_frequency = fdtdec_get_int(blob, node,
232 "clock-frequency", 100000);
237 static const struct dm_i2c_ops at91_i2c_ops = {
238 .xfer = at91_i2c_xfer,
239 .set_bus_speed = at91_i2c_set_bus_speed,
240 .get_bus_speed = at91_i2c_get_bus_speed,
243 static int at91_i2c_probe(struct udevice *dev)
245 struct at91_i2c_bus *bus = dev_get_priv(dev);
246 struct at91_i2c_regs *reg = bus->regs;
249 ret = at91_i2c_enable_clk(dev);
253 writel(TWI_CR_SWRST, ®->cr);
255 at91_calc_i2c_clock(dev, bus->clock_frequency);
257 writel(bus->cwgr_val, ®->cwgr);
258 writel(TWI_CR_MSEN, ®->cr);
259 writel(TWI_CR_SVDIS, ®->cr);
264 static const struct at91_i2c_pdata at91rm9200_config = {
269 static const struct at91_i2c_pdata at91sam9261_config = {
274 static const struct at91_i2c_pdata at91sam9260_config = {
279 static const struct at91_i2c_pdata at91sam9g20_config = {
284 static const struct at91_i2c_pdata at91sam9g10_config = {
289 static const struct at91_i2c_pdata at91sam9x5_config = {
294 static const struct at91_i2c_pdata sama5d4_config = {
299 static const struct at91_i2c_pdata sama5d2_config = {
304 static 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 },
316 U_BOOT_DRIVER(i2c_at91) = {
319 .of_match = at91_i2c_ids,
320 .probe = at91_i2c_probe,
321 .of_to_plat = at91_i2c_of_to_plat,
322 .per_child_auto = sizeof(struct dm_i2c_chip),
323 .priv_auto = sizeof(struct at91_i2c_bus),
324 .ops = &at91_i2c_ops,