18800e0faSSongjun Wu /* 28800e0faSSongjun Wu * Atmel I2C driver. 38800e0faSSongjun Wu * 48800e0faSSongjun Wu * (C) Copyright 2016 Songjun Wu <songjun.wu@atmel.com> 58800e0faSSongjun Wu * 68800e0faSSongjun Wu * SPDX-License-Identifier: GPL-2.0+ 78800e0faSSongjun Wu */ 88800e0faSSongjun Wu 98800e0faSSongjun Wu #include <asm/io.h> 108800e0faSSongjun Wu #include <common.h> 1176062b9cSWenyou Yang #include <clk.h> 128800e0faSSongjun Wu #include <dm.h> 138800e0faSSongjun Wu #include <errno.h> 148800e0faSSongjun Wu #include <fdtdec.h> 158800e0faSSongjun Wu #include <i2c.h> 168800e0faSSongjun Wu #include <linux/bitops.h> 178800e0faSSongjun Wu #include <mach/clk.h> 188800e0faSSongjun Wu 198800e0faSSongjun Wu #include "at91_i2c.h" 208800e0faSSongjun Wu 218800e0faSSongjun Wu DECLARE_GLOBAL_DATA_PTR; 228800e0faSSongjun Wu 238800e0faSSongjun Wu #define I2C_TIMEOUT_MS 100 248800e0faSSongjun Wu 258800e0faSSongjun Wu static int at91_wait_for_xfer(struct at91_i2c_bus *bus, u32 status) 268800e0faSSongjun Wu { 278800e0faSSongjun Wu struct at91_i2c_regs *reg = bus->regs; 288800e0faSSongjun Wu ulong start_time = get_timer(0); 298800e0faSSongjun Wu u32 sr; 308800e0faSSongjun Wu 318800e0faSSongjun Wu bus->status = 0; 328800e0faSSongjun Wu 338800e0faSSongjun Wu do { 348800e0faSSongjun Wu sr = readl(®->sr); 358800e0faSSongjun Wu bus->status |= sr; 368800e0faSSongjun Wu 378800e0faSSongjun Wu if (sr & TWI_SR_NACK) 388800e0faSSongjun Wu return -EREMOTEIO; 398800e0faSSongjun Wu else if (sr & status) 408800e0faSSongjun Wu return 0; 418800e0faSSongjun Wu } while (get_timer(start_time) < I2C_TIMEOUT_MS); 428800e0faSSongjun Wu 438800e0faSSongjun Wu return -ETIMEDOUT; 448800e0faSSongjun Wu } 458800e0faSSongjun Wu 468800e0faSSongjun Wu static int at91_i2c_xfer_msg(struct at91_i2c_bus *bus, struct i2c_msg *msg) 478800e0faSSongjun Wu { 488800e0faSSongjun Wu struct at91_i2c_regs *reg = bus->regs; 498800e0faSSongjun Wu bool is_read = msg->flags & I2C_M_RD; 508800e0faSSongjun Wu u32 i; 518800e0faSSongjun Wu int ret = 0; 528800e0faSSongjun Wu 538800e0faSSongjun Wu readl(®->sr); 548800e0faSSongjun Wu if (is_read) { 558800e0faSSongjun Wu writel(TWI_CR_START, ®->cr); 568800e0faSSongjun Wu 578800e0faSSongjun Wu for (i = 0; !ret && i < (msg->len - 1); i++) { 588800e0faSSongjun Wu ret = at91_wait_for_xfer(bus, TWI_SR_RXRDY); 598800e0faSSongjun Wu msg->buf[i] = readl(®->rhr); 608800e0faSSongjun Wu } 618800e0faSSongjun Wu 628800e0faSSongjun Wu if (ret) 638800e0faSSongjun Wu goto error; 648800e0faSSongjun Wu 658800e0faSSongjun Wu writel(TWI_CR_STOP, ®->cr); 668800e0faSSongjun Wu 678800e0faSSongjun Wu ret = at91_wait_for_xfer(bus, TWI_SR_RXRDY); 688800e0faSSongjun Wu if (ret) 698800e0faSSongjun Wu goto error; 708800e0faSSongjun Wu 718800e0faSSongjun Wu msg->buf[i] = readl(®->rhr); 728800e0faSSongjun Wu 738800e0faSSongjun Wu } else { 748800e0faSSongjun Wu writel(msg->buf[0], ®->thr); 75*0afbb0e1SAlan Ott ret = at91_wait_for_xfer(bus, TWI_SR_TXRDY); 76*0afbb0e1SAlan Ott 778800e0faSSongjun Wu for (i = 1; !ret && (i < msg->len); i++) { 788800e0faSSongjun Wu writel(msg->buf[i], ®->thr); 798800e0faSSongjun Wu ret = at91_wait_for_xfer(bus, TWI_SR_TXRDY); 808800e0faSSongjun Wu } 818800e0faSSongjun Wu 828800e0faSSongjun Wu if (ret) 838800e0faSSongjun Wu goto error; 848800e0faSSongjun Wu 858800e0faSSongjun Wu writel(TWI_CR_STOP, ®->cr); 868800e0faSSongjun Wu } 878800e0faSSongjun Wu 888800e0faSSongjun Wu if (!ret) 898800e0faSSongjun Wu ret = at91_wait_for_xfer(bus, TWI_SR_TXCOMP); 908800e0faSSongjun Wu 918800e0faSSongjun Wu if (ret) 928800e0faSSongjun Wu goto error; 938800e0faSSongjun Wu 948800e0faSSongjun Wu if (bus->status & (TWI_SR_OVRE | TWI_SR_UNRE | TWI_SR_LOCK)) { 958800e0faSSongjun Wu ret = -EIO; 968800e0faSSongjun Wu goto error; 978800e0faSSongjun Wu } 988800e0faSSongjun Wu 998800e0faSSongjun Wu return 0; 1008800e0faSSongjun Wu 1018800e0faSSongjun Wu error: 1028800e0faSSongjun Wu if (bus->status & TWI_SR_LOCK) 1038800e0faSSongjun Wu writel(TWI_CR_LOCKCLR, ®->cr); 1048800e0faSSongjun Wu 1058800e0faSSongjun Wu return ret; 1068800e0faSSongjun Wu } 1078800e0faSSongjun Wu 1088800e0faSSongjun Wu static int at91_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs) 1098800e0faSSongjun Wu { 1108800e0faSSongjun Wu struct at91_i2c_bus *bus = dev_get_priv(dev); 1118800e0faSSongjun Wu struct at91_i2c_regs *reg = bus->regs; 1128800e0faSSongjun Wu struct i2c_msg *m_start = msg; 1138800e0faSSongjun Wu bool is_read; 1148800e0faSSongjun Wu u32 int_addr_flag = 0; 1158800e0faSSongjun Wu int ret = 0; 1168800e0faSSongjun Wu 1178800e0faSSongjun Wu if (nmsgs == 2) { 1188800e0faSSongjun Wu int internal_address = 0; 1198800e0faSSongjun Wu int i; 1208800e0faSSongjun Wu 1218800e0faSSongjun Wu /* 1st msg is put into the internal address, start with 2nd */ 1228800e0faSSongjun Wu m_start = &msg[1]; 1238800e0faSSongjun Wu 1248800e0faSSongjun Wu /* the max length of internal address is 3 bytes */ 1258800e0faSSongjun Wu if (msg->len > 3) 1268800e0faSSongjun Wu return -EFAULT; 1278800e0faSSongjun Wu 1288800e0faSSongjun Wu for (i = 0; i < msg->len; ++i) { 1298800e0faSSongjun Wu const unsigned addr = msg->buf[msg->len - 1 - i]; 1308800e0faSSongjun Wu 1318800e0faSSongjun Wu internal_address |= addr << (8 * i); 1328800e0faSSongjun Wu int_addr_flag += TWI_MMR_IADRSZ_1; 1338800e0faSSongjun Wu } 1348800e0faSSongjun Wu 1358800e0faSSongjun Wu writel(internal_address, ®->iadr); 1368800e0faSSongjun Wu } 1378800e0faSSongjun Wu 1388800e0faSSongjun Wu is_read = m_start->flags & I2C_M_RD; 1398800e0faSSongjun Wu 1408800e0faSSongjun Wu writel((m_start->addr << 16) | int_addr_flag | 1418800e0faSSongjun Wu (is_read ? TWI_MMR_MREAD : 0), ®->mmr); 1428800e0faSSongjun Wu 1438800e0faSSongjun Wu ret = at91_i2c_xfer_msg(bus, m_start); 1448800e0faSSongjun Wu 1458800e0faSSongjun Wu return ret; 1468800e0faSSongjun Wu } 1478800e0faSSongjun Wu 1488800e0faSSongjun Wu /* 1498800e0faSSongjun Wu * Calculate symmetric clock as stated in datasheet: 1508800e0faSSongjun Wu * twi_clk = F_MAIN / (2 * (cdiv * (1 << ckdiv) + offset)) 1518800e0faSSongjun Wu */ 1528800e0faSSongjun Wu static void at91_calc_i2c_clock(struct udevice *dev, int i2c_clk) 1538800e0faSSongjun Wu { 1548800e0faSSongjun Wu struct at91_i2c_bus *bus = dev_get_priv(dev); 1558800e0faSSongjun Wu const struct at91_i2c_pdata *pdata = bus->pdata; 1568800e0faSSongjun Wu int offset = pdata->clk_offset; 1578800e0faSSongjun Wu int max_ckdiv = pdata->clk_max_div; 1588800e0faSSongjun Wu int ckdiv, cdiv, div; 1598800e0faSSongjun Wu unsigned long src_rate; 1608800e0faSSongjun Wu 1618800e0faSSongjun Wu src_rate = bus->bus_clk_rate; 1628800e0faSSongjun Wu 1638800e0faSSongjun Wu div = max(0, (int)DIV_ROUND_UP(src_rate, 2 * i2c_clk) - offset); 1648800e0faSSongjun Wu ckdiv = fls(div >> 8); 1658800e0faSSongjun Wu cdiv = div >> ckdiv; 1668800e0faSSongjun Wu 1678800e0faSSongjun Wu if (ckdiv > max_ckdiv) { 1688800e0faSSongjun Wu ckdiv = max_ckdiv; 1698800e0faSSongjun Wu cdiv = 255; 1708800e0faSSongjun Wu } 1718800e0faSSongjun Wu 1728800e0faSSongjun Wu bus->speed = DIV_ROUND_UP(src_rate, 1738800e0faSSongjun Wu (cdiv * (1 << ckdiv) + offset) * 2); 1748800e0faSSongjun Wu 1758800e0faSSongjun Wu bus->cwgr_val = (ckdiv << 16) | (cdiv << 8) | cdiv; 1768800e0faSSongjun Wu } 1778800e0faSSongjun Wu 1788800e0faSSongjun Wu static int at91_i2c_enable_clk(struct udevice *dev) 1798800e0faSSongjun Wu { 1808800e0faSSongjun Wu struct at91_i2c_bus *bus = dev_get_priv(dev); 1818800e0faSSongjun Wu struct clk clk; 1828800e0faSSongjun Wu ulong clk_rate; 1838800e0faSSongjun Wu int ret; 1848800e0faSSongjun Wu 1858800e0faSSongjun Wu ret = clk_get_by_index(dev, 0, &clk); 1868800e0faSSongjun Wu if (ret) 1878800e0faSSongjun Wu return -EINVAL; 1888800e0faSSongjun Wu 1898800e0faSSongjun Wu ret = clk_enable(&clk); 1908800e0faSSongjun Wu if (ret) 1918800e0faSSongjun Wu return ret; 1928800e0faSSongjun Wu 1938800e0faSSongjun Wu clk_rate = clk_get_rate(&clk); 1948800e0faSSongjun Wu if (!clk_rate) 19552f37333SWenyou Yang return -EINVAL; 1968800e0faSSongjun Wu 1978800e0faSSongjun Wu bus->bus_clk_rate = clk_rate; 1988800e0faSSongjun Wu 1998800e0faSSongjun Wu clk_free(&clk); 2008800e0faSSongjun Wu 2018800e0faSSongjun Wu return 0; 2028800e0faSSongjun Wu } 2038800e0faSSongjun Wu 2040bc8f640SWenyou.Yang@microchip.com static int at91_i2c_probe_chip(struct udevice *dev, uint chip, uint chip_flags) 2058800e0faSSongjun Wu { 2068800e0faSSongjun Wu struct at91_i2c_bus *bus = dev_get_priv(dev); 2078800e0faSSongjun Wu struct at91_i2c_regs *reg = bus->regs; 2088800e0faSSongjun Wu int ret; 2098800e0faSSongjun Wu 2108800e0faSSongjun Wu ret = at91_i2c_enable_clk(dev); 2118800e0faSSongjun Wu if (ret) 2128800e0faSSongjun Wu return ret; 2138800e0faSSongjun Wu 2148800e0faSSongjun Wu writel(TWI_CR_SWRST, ®->cr); 2158800e0faSSongjun Wu 2168800e0faSSongjun Wu at91_calc_i2c_clock(dev, bus->clock_frequency); 2178800e0faSSongjun Wu 2188800e0faSSongjun Wu writel(bus->cwgr_val, ®->cwgr); 2198800e0faSSongjun Wu writel(TWI_CR_MSEN, ®->cr); 2208800e0faSSongjun Wu writel(TWI_CR_SVDIS, ®->cr); 2218800e0faSSongjun Wu 2228800e0faSSongjun Wu return 0; 2238800e0faSSongjun Wu } 2248800e0faSSongjun Wu 2258800e0faSSongjun Wu static int at91_i2c_set_bus_speed(struct udevice *dev, unsigned int speed) 2268800e0faSSongjun Wu { 2278800e0faSSongjun Wu struct at91_i2c_bus *bus = dev_get_priv(dev); 2288800e0faSSongjun Wu 2298800e0faSSongjun Wu at91_calc_i2c_clock(dev, speed); 2308800e0faSSongjun Wu 2318800e0faSSongjun Wu writel(bus->cwgr_val, &bus->regs->cwgr); 2328800e0faSSongjun Wu 2338800e0faSSongjun Wu return 0; 2348800e0faSSongjun Wu } 2358800e0faSSongjun Wu 2368800e0faSSongjun Wu int at91_i2c_get_bus_speed(struct udevice *dev) 2378800e0faSSongjun Wu { 2388800e0faSSongjun Wu struct at91_i2c_bus *bus = dev_get_priv(dev); 2398800e0faSSongjun Wu 2408800e0faSSongjun Wu return bus->speed; 2418800e0faSSongjun Wu } 2428800e0faSSongjun Wu 2438800e0faSSongjun Wu static int at91_i2c_ofdata_to_platdata(struct udevice *dev) 2448800e0faSSongjun Wu { 2458800e0faSSongjun Wu const void *blob = gd->fdt_blob; 2468800e0faSSongjun Wu struct at91_i2c_bus *bus = dev_get_priv(dev); 247e160f7d4SSimon Glass int node = dev_of_offset(dev); 2488800e0faSSongjun Wu 249a821c4afSSimon Glass bus->regs = (struct at91_i2c_regs *)devfdt_get_addr(dev); 2508800e0faSSongjun Wu bus->pdata = (struct at91_i2c_pdata *)dev_get_driver_data(dev); 2518800e0faSSongjun Wu bus->clock_frequency = fdtdec_get_int(blob, node, 2528800e0faSSongjun Wu "clock-frequency", 100000); 2538800e0faSSongjun Wu 2548800e0faSSongjun Wu return 0; 2558800e0faSSongjun Wu } 2568800e0faSSongjun Wu 2578800e0faSSongjun Wu static const struct dm_i2c_ops at91_i2c_ops = { 2588800e0faSSongjun Wu .xfer = at91_i2c_xfer, 2590bc8f640SWenyou.Yang@microchip.com .probe_chip = at91_i2c_probe_chip, 2608800e0faSSongjun Wu .set_bus_speed = at91_i2c_set_bus_speed, 2618800e0faSSongjun Wu .get_bus_speed = at91_i2c_get_bus_speed, 2628800e0faSSongjun Wu }; 2638800e0faSSongjun Wu 2640bc8f640SWenyou.Yang@microchip.com static int at91_i2c_probe(struct udevice *dev) 2650bc8f640SWenyou.Yang@microchip.com { 2660bc8f640SWenyou.Yang@microchip.com struct at91_i2c_bus *bus = dev_get_priv(dev); 2670bc8f640SWenyou.Yang@microchip.com struct at91_i2c_regs *reg = bus->regs; 2680bc8f640SWenyou.Yang@microchip.com int ret; 2690bc8f640SWenyou.Yang@microchip.com 2700bc8f640SWenyou.Yang@microchip.com ret = at91_i2c_enable_clk(dev); 2710bc8f640SWenyou.Yang@microchip.com if (ret) 2720bc8f640SWenyou.Yang@microchip.com return ret; 2730bc8f640SWenyou.Yang@microchip.com 2740bc8f640SWenyou.Yang@microchip.com writel(TWI_CR_SWRST, ®->cr); 2750bc8f640SWenyou.Yang@microchip.com 2760bc8f640SWenyou.Yang@microchip.com at91_calc_i2c_clock(dev, bus->clock_frequency); 2770bc8f640SWenyou.Yang@microchip.com 2780bc8f640SWenyou.Yang@microchip.com writel(bus->cwgr_val, ®->cwgr); 2790bc8f640SWenyou.Yang@microchip.com writel(TWI_CR_MSEN, ®->cr); 2800bc8f640SWenyou.Yang@microchip.com writel(TWI_CR_SVDIS, ®->cr); 2810bc8f640SWenyou.Yang@microchip.com 2820bc8f640SWenyou.Yang@microchip.com return 0; 2830bc8f640SWenyou.Yang@microchip.com } 2840bc8f640SWenyou.Yang@microchip.com 2858800e0faSSongjun Wu static const struct at91_i2c_pdata at91rm9200_config = { 2868800e0faSSongjun Wu .clk_max_div = 5, 2878800e0faSSongjun Wu .clk_offset = 3, 2888800e0faSSongjun Wu }; 2898800e0faSSongjun Wu 2908800e0faSSongjun Wu static const struct at91_i2c_pdata at91sam9261_config = { 2918800e0faSSongjun Wu .clk_max_div = 5, 2928800e0faSSongjun Wu .clk_offset = 4, 2938800e0faSSongjun Wu }; 2948800e0faSSongjun Wu 2958800e0faSSongjun Wu static const struct at91_i2c_pdata at91sam9260_config = { 2968800e0faSSongjun Wu .clk_max_div = 7, 2978800e0faSSongjun Wu .clk_offset = 4, 2988800e0faSSongjun Wu }; 2998800e0faSSongjun Wu 3008800e0faSSongjun Wu static const struct at91_i2c_pdata at91sam9g20_config = { 3018800e0faSSongjun Wu .clk_max_div = 7, 3028800e0faSSongjun Wu .clk_offset = 4, 3038800e0faSSongjun Wu }; 3048800e0faSSongjun Wu 3058800e0faSSongjun Wu static const struct at91_i2c_pdata at91sam9g10_config = { 3068800e0faSSongjun Wu .clk_max_div = 7, 3078800e0faSSongjun Wu .clk_offset = 4, 3088800e0faSSongjun Wu }; 3098800e0faSSongjun Wu 3108800e0faSSongjun Wu static const struct at91_i2c_pdata at91sam9x5_config = { 3118800e0faSSongjun Wu .clk_max_div = 7, 3128800e0faSSongjun Wu .clk_offset = 4, 3138800e0faSSongjun Wu }; 3148800e0faSSongjun Wu 3158800e0faSSongjun Wu static const struct at91_i2c_pdata sama5d4_config = { 3168800e0faSSongjun Wu .clk_max_div = 7, 3178800e0faSSongjun Wu .clk_offset = 4, 3188800e0faSSongjun Wu }; 3198800e0faSSongjun Wu 3208800e0faSSongjun Wu static const struct at91_i2c_pdata sama5d2_config = { 3218800e0faSSongjun Wu .clk_max_div = 7, 3228800e0faSSongjun Wu .clk_offset = 3, 3238800e0faSSongjun Wu }; 3248800e0faSSongjun Wu 3258800e0faSSongjun Wu static const struct udevice_id at91_i2c_ids[] = { 3268800e0faSSongjun Wu { .compatible = "atmel,at91rm9200-i2c", .data = (long)&at91rm9200_config }, 3278800e0faSSongjun Wu { .compatible = "atmel,at91sam9260-i2c", .data = (long)&at91sam9260_config }, 3288800e0faSSongjun Wu { .compatible = "atmel,at91sam9261-i2c", .data = (long)&at91sam9261_config }, 3298800e0faSSongjun Wu { .compatible = "atmel,at91sam9g20-i2c", .data = (long)&at91sam9g20_config }, 3308800e0faSSongjun Wu { .compatible = "atmel,at91sam9g10-i2c", .data = (long)&at91sam9g10_config }, 3318800e0faSSongjun Wu { .compatible = "atmel,at91sam9x5-i2c", .data = (long)&at91sam9x5_config }, 3328800e0faSSongjun Wu { .compatible = "atmel,sama5d4-i2c", .data = (long)&sama5d4_config }, 3338800e0faSSongjun Wu { .compatible = "atmel,sama5d2-i2c", .data = (long)&sama5d2_config }, 3348800e0faSSongjun Wu { } 3358800e0faSSongjun Wu }; 3368800e0faSSongjun Wu 3378800e0faSSongjun Wu U_BOOT_DRIVER(i2c_at91) = { 3388800e0faSSongjun Wu .name = "i2c_at91", 3398800e0faSSongjun Wu .id = UCLASS_I2C, 3408800e0faSSongjun Wu .of_match = at91_i2c_ids, 3410bc8f640SWenyou.Yang@microchip.com .probe = at91_i2c_probe, 3428800e0faSSongjun Wu .ofdata_to_platdata = at91_i2c_ofdata_to_platdata, 3438800e0faSSongjun Wu .per_child_auto_alloc_size = sizeof(struct dm_i2c_chip), 3448800e0faSSongjun Wu .priv_auto_alloc_size = sizeof(struct at91_i2c_bus), 3458800e0faSSongjun Wu .ops = &at91_i2c_ops, 3468800e0faSSongjun Wu }; 347