1 // SPDX-License-Identifier: GPL-2.0+
5 * (C) Copyright 2016 Songjun Wu <songjun.wu@atmel.com>
9 #include <asm/global_data.h>
16 #include <linux/bitops.h>
21 DECLARE_GLOBAL_DATA_PTR;
23 #define I2C_TIMEOUT_MS 100
25 static int at91_wait_for_xfer(struct at91_i2c_bus *bus, u32 status)
27 struct at91_i2c_regs *reg = bus->regs;
28 ulong start_time = get_timer(0);
41 } while (get_timer(start_time) < I2C_TIMEOUT_MS);
46 static int at91_i2c_xfer_msg(struct at91_i2c_bus *bus, struct i2c_msg *msg)
48 struct at91_i2c_regs *reg = bus->regs;
49 bool is_read = msg->flags & I2C_M_RD;
53 /* if there is no message to send/receive, just exit quietly */
59 writel(TWI_CR_START, ®->cr);
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(®->rhr);
69 writel(TWI_CR_STOP, ®->cr);
71 ret = at91_wait_for_xfer(bus, TWI_SR_RXRDY);
75 msg->buf[i] = readl(®->rhr);
78 writel(msg->buf[0], ®->thr);
79 ret = at91_wait_for_xfer(bus, TWI_SR_TXRDY);
81 for (i = 1; !ret && (i < msg->len); i++) {
82 writel(msg->buf[i], ®->thr);
83 ret = at91_wait_for_xfer(bus, TWI_SR_TXRDY);
89 writel(TWI_CR_STOP, ®->cr);
93 ret = at91_wait_for_xfer(bus, TWI_SR_TXCOMP);
98 if (bus->status & (TWI_SR_OVRE | TWI_SR_UNRE | TWI_SR_LOCK)) {
106 if (bus->status & TWI_SR_LOCK)
107 writel(TWI_CR_LOCKCLR, ®->cr);
112 static int at91_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
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;
118 u32 int_addr_flag = 0;
122 int internal_address = 0;
125 /* 1st msg is put into the internal address, start with 2nd */
128 /* the max length of internal address is 3 bytes */
132 for (i = 0; i < msg->len; ++i) {
133 const unsigned addr = msg->buf[msg->len - 1 - i];
135 internal_address |= addr << (8 * i);
136 int_addr_flag += TWI_MMR_IADRSZ_1;
139 writel(internal_address, ®->iadr);
142 is_read = m_start->flags & I2C_M_RD;
144 writel((m_start->addr << 16) | int_addr_flag |
145 (is_read ? TWI_MMR_MREAD : 0), ®->mmr);
147 ret = at91_i2c_xfer_msg(bus, m_start);
153 * Calculate symmetric clock as stated in datasheet:
154 * twi_clk = F_MAIN / (2 * (cdiv * (1 << ckdiv) + offset))
156 static void at91_calc_i2c_clock(struct udevice *dev, int i2c_clk)
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;
165 src_rate = bus->bus_clk_rate;
167 div = max(0, (int)DIV_ROUND_UP(src_rate, 2 * i2c_clk) - offset);
168 ckdiv = fls(div >> 8);
171 if (ckdiv > max_ckdiv) {
176 bus->speed = DIV_ROUND_UP(src_rate,
177 (cdiv * (1 << ckdiv) + offset) * 2);
179 bus->cwgr_val = (ckdiv << 16) | (cdiv << 8) | cdiv;
182 static int at91_i2c_enable_clk(struct udevice *dev)
184 struct at91_i2c_bus *bus = dev_get_priv(dev);
189 ret = clk_get_by_index(dev, 0, &clk);
193 ret = clk_enable(&clk);
197 clk_rate = clk_get_rate(&clk);
201 bus->bus_clk_rate = clk_rate;
206 static int at91_i2c_set_bus_speed(struct udevice *dev, unsigned int speed)
208 struct at91_i2c_bus *bus = dev_get_priv(dev);
210 at91_calc_i2c_clock(dev, speed);
212 writel(bus->cwgr_val, &bus->regs->cwgr);
217 int at91_i2c_get_bus_speed(struct udevice *dev)
219 struct at91_i2c_bus *bus = dev_get_priv(dev);
224 static int at91_i2c_of_to_plat(struct udevice *dev)
226 const void *blob = gd->fdt_blob;
227 struct at91_i2c_bus *bus = dev_get_priv(dev);
228 int node = dev_of_offset(dev);
230 bus->regs = dev_read_addr_ptr(dev);
231 bus->pdata = (struct at91_i2c_pdata *)dev_get_driver_data(dev);
232 bus->clock_frequency = fdtdec_get_int(blob, node,
233 "clock-frequency", 100000);
238 static const struct dm_i2c_ops at91_i2c_ops = {
239 .xfer = at91_i2c_xfer,
240 .set_bus_speed = at91_i2c_set_bus_speed,
241 .get_bus_speed = at91_i2c_get_bus_speed,
244 static int at91_i2c_probe(struct udevice *dev)
246 struct at91_i2c_bus *bus = dev_get_priv(dev);
247 struct at91_i2c_regs *reg = bus->regs;
250 ret = at91_i2c_enable_clk(dev);
254 writel(TWI_CR_SWRST, ®->cr);
256 at91_calc_i2c_clock(dev, bus->clock_frequency);
258 writel(bus->cwgr_val, ®->cwgr);
259 writel(TWI_CR_MSEN, ®->cr);
260 writel(TWI_CR_SVDIS, ®->cr);
265 static const struct at91_i2c_pdata at91rm9200_config = {
270 static const struct at91_i2c_pdata at91sam9261_config = {
275 static const struct at91_i2c_pdata at91sam9260_config = {
280 static const struct at91_i2c_pdata at91sam9g20_config = {
285 static const struct at91_i2c_pdata at91sam9g10_config = {
290 static const struct at91_i2c_pdata at91sam9x5_config = {
295 static const struct at91_i2c_pdata sama5d4_config = {
300 static const struct at91_i2c_pdata sama5d2_config = {
305 static const struct at91_i2c_pdata sam9x60_config = {
310 static 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 },
319 { .compatible = "microchip,sam9x60-i2c", .data = (long)&sam9x60_config },
323 U_BOOT_DRIVER(i2c_at91) = {
326 .of_match = at91_i2c_ids,
327 .probe = at91_i2c_probe,
328 .of_to_plat = at91_i2c_of_to_plat,
329 .per_child_auto = sizeof(struct dm_i2c_chip),
330 .priv_auto = sizeof(struct at91_i2c_bus),
331 .ops = &at91_i2c_ops,