1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
28800e0faSSongjun Wu /*
38800e0faSSongjun Wu * Atmel I2C driver.
48800e0faSSongjun Wu *
58800e0faSSongjun Wu * (C) Copyright 2016 Songjun Wu <songjun.wu@atmel.com>
68800e0faSSongjun Wu */
78800e0faSSongjun Wu
88800e0faSSongjun Wu #include <asm/io.h>
98800e0faSSongjun Wu #include <common.h>
1076062b9cSWenyou Yang #include <clk.h>
118800e0faSSongjun Wu #include <dm.h>
128800e0faSSongjun Wu #include <errno.h>
138800e0faSSongjun Wu #include <fdtdec.h>
148800e0faSSongjun Wu #include <i2c.h>
158800e0faSSongjun Wu #include <linux/bitops.h>
168800e0faSSongjun Wu #include <mach/clk.h>
178800e0faSSongjun Wu
188800e0faSSongjun Wu #include "at91_i2c.h"
198800e0faSSongjun Wu
208800e0faSSongjun Wu DECLARE_GLOBAL_DATA_PTR;
218800e0faSSongjun Wu
228800e0faSSongjun Wu #define I2C_TIMEOUT_MS 100
238800e0faSSongjun Wu
at91_wait_for_xfer(struct at91_i2c_bus * bus,u32 status)248800e0faSSongjun Wu static int at91_wait_for_xfer(struct at91_i2c_bus *bus, u32 status)
258800e0faSSongjun Wu {
268800e0faSSongjun Wu struct at91_i2c_regs *reg = bus->regs;
278800e0faSSongjun Wu ulong start_time = get_timer(0);
288800e0faSSongjun Wu u32 sr;
298800e0faSSongjun Wu
308800e0faSSongjun Wu bus->status = 0;
318800e0faSSongjun Wu
328800e0faSSongjun Wu do {
338800e0faSSongjun Wu sr = readl(®->sr);
348800e0faSSongjun Wu bus->status |= sr;
358800e0faSSongjun Wu
368800e0faSSongjun Wu if (sr & TWI_SR_NACK)
378800e0faSSongjun Wu return -EREMOTEIO;
388800e0faSSongjun Wu else if (sr & status)
398800e0faSSongjun Wu return 0;
408800e0faSSongjun Wu } while (get_timer(start_time) < I2C_TIMEOUT_MS);
418800e0faSSongjun Wu
428800e0faSSongjun Wu return -ETIMEDOUT;
438800e0faSSongjun Wu }
448800e0faSSongjun Wu
at91_i2c_xfer_msg(struct at91_i2c_bus * bus,struct i2c_msg * msg)458800e0faSSongjun Wu static int at91_i2c_xfer_msg(struct at91_i2c_bus *bus, struct i2c_msg *msg)
468800e0faSSongjun Wu {
478800e0faSSongjun Wu struct at91_i2c_regs *reg = bus->regs;
488800e0faSSongjun Wu bool is_read = msg->flags & I2C_M_RD;
498800e0faSSongjun Wu u32 i;
508800e0faSSongjun Wu int ret = 0;
518800e0faSSongjun Wu
528800e0faSSongjun Wu readl(®->sr);
538800e0faSSongjun Wu if (is_read) {
548800e0faSSongjun Wu writel(TWI_CR_START, ®->cr);
558800e0faSSongjun Wu
568800e0faSSongjun Wu for (i = 0; !ret && i < (msg->len - 1); i++) {
578800e0faSSongjun Wu ret = at91_wait_for_xfer(bus, TWI_SR_RXRDY);
588800e0faSSongjun Wu msg->buf[i] = readl(®->rhr);
598800e0faSSongjun Wu }
608800e0faSSongjun Wu
618800e0faSSongjun Wu if (ret)
628800e0faSSongjun Wu goto error;
638800e0faSSongjun Wu
648800e0faSSongjun Wu writel(TWI_CR_STOP, ®->cr);
658800e0faSSongjun Wu
668800e0faSSongjun Wu ret = at91_wait_for_xfer(bus, TWI_SR_RXRDY);
678800e0faSSongjun Wu if (ret)
688800e0faSSongjun Wu goto error;
698800e0faSSongjun Wu
708800e0faSSongjun Wu msg->buf[i] = readl(®->rhr);
718800e0faSSongjun Wu
728800e0faSSongjun Wu } else {
738800e0faSSongjun Wu writel(msg->buf[0], ®->thr);
740afbb0e1SAlan Ott ret = at91_wait_for_xfer(bus, TWI_SR_TXRDY);
750afbb0e1SAlan Ott
768800e0faSSongjun Wu for (i = 1; !ret && (i < msg->len); i++) {
778800e0faSSongjun Wu writel(msg->buf[i], ®->thr);
788800e0faSSongjun Wu ret = at91_wait_for_xfer(bus, TWI_SR_TXRDY);
798800e0faSSongjun Wu }
808800e0faSSongjun Wu
818800e0faSSongjun Wu if (ret)
828800e0faSSongjun Wu goto error;
838800e0faSSongjun Wu
848800e0faSSongjun Wu writel(TWI_CR_STOP, ®->cr);
858800e0faSSongjun Wu }
868800e0faSSongjun Wu
878800e0faSSongjun Wu if (!ret)
888800e0faSSongjun Wu ret = at91_wait_for_xfer(bus, TWI_SR_TXCOMP);
898800e0faSSongjun Wu
908800e0faSSongjun Wu if (ret)
918800e0faSSongjun Wu goto error;
928800e0faSSongjun Wu
938800e0faSSongjun Wu if (bus->status & (TWI_SR_OVRE | TWI_SR_UNRE | TWI_SR_LOCK)) {
948800e0faSSongjun Wu ret = -EIO;
958800e0faSSongjun Wu goto error;
968800e0faSSongjun Wu }
978800e0faSSongjun Wu
988800e0faSSongjun Wu return 0;
998800e0faSSongjun Wu
1008800e0faSSongjun Wu error:
1018800e0faSSongjun Wu if (bus->status & TWI_SR_LOCK)
1028800e0faSSongjun Wu writel(TWI_CR_LOCKCLR, ®->cr);
1038800e0faSSongjun Wu
1048800e0faSSongjun Wu return ret;
1058800e0faSSongjun Wu }
1068800e0faSSongjun Wu
at91_i2c_xfer(struct udevice * dev,struct i2c_msg * msg,int nmsgs)1078800e0faSSongjun Wu static int at91_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
1088800e0faSSongjun Wu {
1098800e0faSSongjun Wu struct at91_i2c_bus *bus = dev_get_priv(dev);
1108800e0faSSongjun Wu struct at91_i2c_regs *reg = bus->regs;
1118800e0faSSongjun Wu struct i2c_msg *m_start = msg;
1128800e0faSSongjun Wu bool is_read;
1138800e0faSSongjun Wu u32 int_addr_flag = 0;
1148800e0faSSongjun Wu int ret = 0;
1158800e0faSSongjun Wu
1168800e0faSSongjun Wu if (nmsgs == 2) {
1178800e0faSSongjun Wu int internal_address = 0;
1188800e0faSSongjun Wu int i;
1198800e0faSSongjun Wu
1208800e0faSSongjun Wu /* 1st msg is put into the internal address, start with 2nd */
1218800e0faSSongjun Wu m_start = &msg[1];
1228800e0faSSongjun Wu
1238800e0faSSongjun Wu /* the max length of internal address is 3 bytes */
1248800e0faSSongjun Wu if (msg->len > 3)
1258800e0faSSongjun Wu return -EFAULT;
1268800e0faSSongjun Wu
1278800e0faSSongjun Wu for (i = 0; i < msg->len; ++i) {
1288800e0faSSongjun Wu const unsigned addr = msg->buf[msg->len - 1 - i];
1298800e0faSSongjun Wu
1308800e0faSSongjun Wu internal_address |= addr << (8 * i);
1318800e0faSSongjun Wu int_addr_flag += TWI_MMR_IADRSZ_1;
1328800e0faSSongjun Wu }
1338800e0faSSongjun Wu
1348800e0faSSongjun Wu writel(internal_address, ®->iadr);
1358800e0faSSongjun Wu }
1368800e0faSSongjun Wu
1378800e0faSSongjun Wu is_read = m_start->flags & I2C_M_RD;
1388800e0faSSongjun Wu
1398800e0faSSongjun Wu writel((m_start->addr << 16) | int_addr_flag |
1408800e0faSSongjun Wu (is_read ? TWI_MMR_MREAD : 0), ®->mmr);
1418800e0faSSongjun Wu
1428800e0faSSongjun Wu ret = at91_i2c_xfer_msg(bus, m_start);
1438800e0faSSongjun Wu
1448800e0faSSongjun Wu return ret;
1458800e0faSSongjun Wu }
1468800e0faSSongjun Wu
1478800e0faSSongjun Wu /*
1488800e0faSSongjun Wu * Calculate symmetric clock as stated in datasheet:
1498800e0faSSongjun Wu * twi_clk = F_MAIN / (2 * (cdiv * (1 << ckdiv) + offset))
1508800e0faSSongjun Wu */
at91_calc_i2c_clock(struct udevice * dev,int i2c_clk)1518800e0faSSongjun Wu static void at91_calc_i2c_clock(struct udevice *dev, int i2c_clk)
1528800e0faSSongjun Wu {
1538800e0faSSongjun Wu struct at91_i2c_bus *bus = dev_get_priv(dev);
1548800e0faSSongjun Wu const struct at91_i2c_pdata *pdata = bus->pdata;
1558800e0faSSongjun Wu int offset = pdata->clk_offset;
1568800e0faSSongjun Wu int max_ckdiv = pdata->clk_max_div;
1578800e0faSSongjun Wu int ckdiv, cdiv, div;
1588800e0faSSongjun Wu unsigned long src_rate;
1598800e0faSSongjun Wu
1608800e0faSSongjun Wu src_rate = bus->bus_clk_rate;
1618800e0faSSongjun Wu
1628800e0faSSongjun Wu div = max(0, (int)DIV_ROUND_UP(src_rate, 2 * i2c_clk) - offset);
1638800e0faSSongjun Wu ckdiv = fls(div >> 8);
1648800e0faSSongjun Wu cdiv = div >> ckdiv;
1658800e0faSSongjun Wu
1668800e0faSSongjun Wu if (ckdiv > max_ckdiv) {
1678800e0faSSongjun Wu ckdiv = max_ckdiv;
1688800e0faSSongjun Wu cdiv = 255;
1698800e0faSSongjun Wu }
1708800e0faSSongjun Wu
1718800e0faSSongjun Wu bus->speed = DIV_ROUND_UP(src_rate,
1728800e0faSSongjun Wu (cdiv * (1 << ckdiv) + offset) * 2);
1738800e0faSSongjun Wu
1748800e0faSSongjun Wu bus->cwgr_val = (ckdiv << 16) | (cdiv << 8) | cdiv;
1758800e0faSSongjun Wu }
1768800e0faSSongjun Wu
at91_i2c_enable_clk(struct udevice * dev)1778800e0faSSongjun Wu static int at91_i2c_enable_clk(struct udevice *dev)
1788800e0faSSongjun Wu {
1798800e0faSSongjun Wu struct at91_i2c_bus *bus = dev_get_priv(dev);
1808800e0faSSongjun Wu struct clk clk;
1818800e0faSSongjun Wu ulong clk_rate;
1828800e0faSSongjun Wu int ret;
1838800e0faSSongjun Wu
1848800e0faSSongjun Wu ret = clk_get_by_index(dev, 0, &clk);
1858800e0faSSongjun Wu if (ret)
1868800e0faSSongjun Wu return -EINVAL;
1878800e0faSSongjun Wu
1888800e0faSSongjun Wu ret = clk_enable(&clk);
1898800e0faSSongjun Wu if (ret)
1908800e0faSSongjun Wu return ret;
1918800e0faSSongjun Wu
1928800e0faSSongjun Wu clk_rate = clk_get_rate(&clk);
1938800e0faSSongjun Wu if (!clk_rate)
19452f37333SWenyou Yang return -EINVAL;
1958800e0faSSongjun Wu
1968800e0faSSongjun Wu bus->bus_clk_rate = clk_rate;
1978800e0faSSongjun Wu
1988800e0faSSongjun Wu clk_free(&clk);
1998800e0faSSongjun Wu
2008800e0faSSongjun Wu return 0;
2018800e0faSSongjun Wu }
2028800e0faSSongjun Wu
at91_i2c_set_bus_speed(struct udevice * dev,unsigned int speed)2038800e0faSSongjun Wu static int at91_i2c_set_bus_speed(struct udevice *dev, unsigned int speed)
2048800e0faSSongjun Wu {
2058800e0faSSongjun Wu struct at91_i2c_bus *bus = dev_get_priv(dev);
2068800e0faSSongjun Wu
2078800e0faSSongjun Wu at91_calc_i2c_clock(dev, speed);
2088800e0faSSongjun Wu
2098800e0faSSongjun Wu writel(bus->cwgr_val, &bus->regs->cwgr);
2108800e0faSSongjun Wu
2118800e0faSSongjun Wu return 0;
2128800e0faSSongjun Wu }
2138800e0faSSongjun Wu
at91_i2c_get_bus_speed(struct udevice * dev)2148800e0faSSongjun Wu int at91_i2c_get_bus_speed(struct udevice *dev)
2158800e0faSSongjun Wu {
2168800e0faSSongjun Wu struct at91_i2c_bus *bus = dev_get_priv(dev);
2178800e0faSSongjun Wu
2188800e0faSSongjun Wu return bus->speed;
2198800e0faSSongjun Wu }
2208800e0faSSongjun Wu
at91_i2c_ofdata_to_platdata(struct udevice * dev)2218800e0faSSongjun Wu static int at91_i2c_ofdata_to_platdata(struct udevice *dev)
2228800e0faSSongjun Wu {
2238800e0faSSongjun Wu const void *blob = gd->fdt_blob;
2248800e0faSSongjun Wu struct at91_i2c_bus *bus = dev_get_priv(dev);
225e160f7d4SSimon Glass int node = dev_of_offset(dev);
2268800e0faSSongjun Wu
227a821c4afSSimon Glass bus->regs = (struct at91_i2c_regs *)devfdt_get_addr(dev);
2288800e0faSSongjun Wu bus->pdata = (struct at91_i2c_pdata *)dev_get_driver_data(dev);
2298800e0faSSongjun Wu bus->clock_frequency = fdtdec_get_int(blob, node,
2308800e0faSSongjun Wu "clock-frequency", 100000);
2318800e0faSSongjun Wu
2328800e0faSSongjun Wu return 0;
2338800e0faSSongjun Wu }
2348800e0faSSongjun Wu
2358800e0faSSongjun Wu static const struct dm_i2c_ops at91_i2c_ops = {
2368800e0faSSongjun Wu .xfer = at91_i2c_xfer,
2378800e0faSSongjun Wu .set_bus_speed = at91_i2c_set_bus_speed,
2388800e0faSSongjun Wu .get_bus_speed = at91_i2c_get_bus_speed,
2398800e0faSSongjun Wu };
2408800e0faSSongjun Wu
at91_i2c_probe(struct udevice * dev)2410bc8f640SWenyou.Yang@microchip.com static int at91_i2c_probe(struct udevice *dev)
2420bc8f640SWenyou.Yang@microchip.com {
2430bc8f640SWenyou.Yang@microchip.com struct at91_i2c_bus *bus = dev_get_priv(dev);
2440bc8f640SWenyou.Yang@microchip.com struct at91_i2c_regs *reg = bus->regs;
2450bc8f640SWenyou.Yang@microchip.com int ret;
2460bc8f640SWenyou.Yang@microchip.com
2470bc8f640SWenyou.Yang@microchip.com ret = at91_i2c_enable_clk(dev);
2480bc8f640SWenyou.Yang@microchip.com if (ret)
2490bc8f640SWenyou.Yang@microchip.com return ret;
2500bc8f640SWenyou.Yang@microchip.com
2510bc8f640SWenyou.Yang@microchip.com writel(TWI_CR_SWRST, ®->cr);
2520bc8f640SWenyou.Yang@microchip.com
2530bc8f640SWenyou.Yang@microchip.com at91_calc_i2c_clock(dev, bus->clock_frequency);
2540bc8f640SWenyou.Yang@microchip.com
2550bc8f640SWenyou.Yang@microchip.com writel(bus->cwgr_val, ®->cwgr);
2560bc8f640SWenyou.Yang@microchip.com writel(TWI_CR_MSEN, ®->cr);
2570bc8f640SWenyou.Yang@microchip.com writel(TWI_CR_SVDIS, ®->cr);
2580bc8f640SWenyou.Yang@microchip.com
2590bc8f640SWenyou.Yang@microchip.com return 0;
2600bc8f640SWenyou.Yang@microchip.com }
2610bc8f640SWenyou.Yang@microchip.com
2628800e0faSSongjun Wu static const struct at91_i2c_pdata at91rm9200_config = {
2638800e0faSSongjun Wu .clk_max_div = 5,
2648800e0faSSongjun Wu .clk_offset = 3,
2658800e0faSSongjun Wu };
2668800e0faSSongjun Wu
2678800e0faSSongjun Wu static const struct at91_i2c_pdata at91sam9261_config = {
2688800e0faSSongjun Wu .clk_max_div = 5,
2698800e0faSSongjun Wu .clk_offset = 4,
2708800e0faSSongjun Wu };
2718800e0faSSongjun Wu
2728800e0faSSongjun Wu static const struct at91_i2c_pdata at91sam9260_config = {
2738800e0faSSongjun Wu .clk_max_div = 7,
2748800e0faSSongjun Wu .clk_offset = 4,
2758800e0faSSongjun Wu };
2768800e0faSSongjun Wu
2778800e0faSSongjun Wu static const struct at91_i2c_pdata at91sam9g20_config = {
2788800e0faSSongjun Wu .clk_max_div = 7,
2798800e0faSSongjun Wu .clk_offset = 4,
2808800e0faSSongjun Wu };
2818800e0faSSongjun Wu
2828800e0faSSongjun Wu static const struct at91_i2c_pdata at91sam9g10_config = {
2838800e0faSSongjun Wu .clk_max_div = 7,
2848800e0faSSongjun Wu .clk_offset = 4,
2858800e0faSSongjun Wu };
2868800e0faSSongjun Wu
2878800e0faSSongjun Wu static const struct at91_i2c_pdata at91sam9x5_config = {
2888800e0faSSongjun Wu .clk_max_div = 7,
2898800e0faSSongjun Wu .clk_offset = 4,
2908800e0faSSongjun Wu };
2918800e0faSSongjun Wu
2928800e0faSSongjun Wu static const struct at91_i2c_pdata sama5d4_config = {
2938800e0faSSongjun Wu .clk_max_div = 7,
2948800e0faSSongjun Wu .clk_offset = 4,
2958800e0faSSongjun Wu };
2968800e0faSSongjun Wu
2978800e0faSSongjun Wu static const struct at91_i2c_pdata sama5d2_config = {
2988800e0faSSongjun Wu .clk_max_div = 7,
2998800e0faSSongjun Wu .clk_offset = 3,
3008800e0faSSongjun Wu };
3018800e0faSSongjun Wu
3028800e0faSSongjun Wu static const struct udevice_id at91_i2c_ids[] = {
3038800e0faSSongjun Wu { .compatible = "atmel,at91rm9200-i2c", .data = (long)&at91rm9200_config },
3048800e0faSSongjun Wu { .compatible = "atmel,at91sam9260-i2c", .data = (long)&at91sam9260_config },
3058800e0faSSongjun Wu { .compatible = "atmel,at91sam9261-i2c", .data = (long)&at91sam9261_config },
3068800e0faSSongjun Wu { .compatible = "atmel,at91sam9g20-i2c", .data = (long)&at91sam9g20_config },
3078800e0faSSongjun Wu { .compatible = "atmel,at91sam9g10-i2c", .data = (long)&at91sam9g10_config },
3088800e0faSSongjun Wu { .compatible = "atmel,at91sam9x5-i2c", .data = (long)&at91sam9x5_config },
3098800e0faSSongjun Wu { .compatible = "atmel,sama5d4-i2c", .data = (long)&sama5d4_config },
3108800e0faSSongjun Wu { .compatible = "atmel,sama5d2-i2c", .data = (long)&sama5d2_config },
3118800e0faSSongjun Wu { }
3128800e0faSSongjun Wu };
3138800e0faSSongjun Wu
3148800e0faSSongjun Wu U_BOOT_DRIVER(i2c_at91) = {
3158800e0faSSongjun Wu .name = "i2c_at91",
3168800e0faSSongjun Wu .id = UCLASS_I2C,
3178800e0faSSongjun Wu .of_match = at91_i2c_ids,
3180bc8f640SWenyou.Yang@microchip.com .probe = at91_i2c_probe,
3198800e0faSSongjun Wu .ofdata_to_platdata = at91_i2c_ofdata_to_platdata,
3208800e0faSSongjun Wu .per_child_auto_alloc_size = sizeof(struct dm_i2c_chip),
3218800e0faSSongjun Wu .priv_auto_alloc_size = sizeof(struct at91_i2c_bus),
3228800e0faSSongjun Wu .ops = &at91_i2c_ops,
3238800e0faSSongjun Wu };
324