xref: /openbmc/u-boot/drivers/i2c/at91_i2c.c (revision e160f7d430f163bc42757aff3bf2bcac0a459f02)
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(&reg->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(&reg->sr);
548800e0faSSongjun Wu 	if (is_read) {
558800e0faSSongjun Wu 		writel(TWI_CR_START, &reg->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(&reg->rhr);
608800e0faSSongjun Wu 		}
618800e0faSSongjun Wu 
628800e0faSSongjun Wu 		if (ret)
638800e0faSSongjun Wu 			goto error;
648800e0faSSongjun Wu 
658800e0faSSongjun Wu 		writel(TWI_CR_STOP, &reg->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(&reg->rhr);
728800e0faSSongjun Wu 
738800e0faSSongjun Wu 	} else {
748800e0faSSongjun Wu 		writel(msg->buf[0], &reg->thr);
758800e0faSSongjun Wu 		for (i = 1; !ret && (i < msg->len); i++) {
768800e0faSSongjun Wu 			writel(msg->buf[i], &reg->thr);
778800e0faSSongjun Wu 			ret = at91_wait_for_xfer(bus, TWI_SR_TXRDY);
788800e0faSSongjun Wu 		}
798800e0faSSongjun Wu 
808800e0faSSongjun Wu 		if (ret)
818800e0faSSongjun Wu 			goto error;
828800e0faSSongjun Wu 
838800e0faSSongjun Wu 		writel(TWI_CR_STOP, &reg->cr);
848800e0faSSongjun Wu 	}
858800e0faSSongjun Wu 
868800e0faSSongjun Wu 	if (!ret)
878800e0faSSongjun Wu 		ret = at91_wait_for_xfer(bus, TWI_SR_TXCOMP);
888800e0faSSongjun Wu 
898800e0faSSongjun Wu 	if (ret)
908800e0faSSongjun Wu 		goto error;
918800e0faSSongjun Wu 
928800e0faSSongjun Wu 	if (bus->status & (TWI_SR_OVRE | TWI_SR_UNRE | TWI_SR_LOCK)) {
938800e0faSSongjun Wu 		ret = -EIO;
948800e0faSSongjun Wu 		goto error;
958800e0faSSongjun Wu 	}
968800e0faSSongjun Wu 
978800e0faSSongjun Wu 	return 0;
988800e0faSSongjun Wu 
998800e0faSSongjun Wu error:
1008800e0faSSongjun Wu 	if (bus->status & TWI_SR_LOCK)
1018800e0faSSongjun Wu 		writel(TWI_CR_LOCKCLR, &reg->cr);
1028800e0faSSongjun Wu 
1038800e0faSSongjun Wu 	return ret;
1048800e0faSSongjun Wu }
1058800e0faSSongjun Wu 
1068800e0faSSongjun Wu static int at91_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
1078800e0faSSongjun Wu {
1088800e0faSSongjun Wu 	struct at91_i2c_bus *bus = dev_get_priv(dev);
1098800e0faSSongjun Wu 	struct at91_i2c_regs *reg = bus->regs;
1108800e0faSSongjun Wu 	struct i2c_msg *m_start = msg;
1118800e0faSSongjun Wu 	bool is_read;
1128800e0faSSongjun Wu 	u32 int_addr_flag = 0;
1138800e0faSSongjun Wu 	int ret = 0;
1148800e0faSSongjun Wu 
1158800e0faSSongjun Wu 	if (nmsgs == 2) {
1168800e0faSSongjun Wu 		int internal_address = 0;
1178800e0faSSongjun Wu 		int i;
1188800e0faSSongjun Wu 
1198800e0faSSongjun Wu 		/* 1st msg is put into the internal address, start with 2nd */
1208800e0faSSongjun Wu 		m_start = &msg[1];
1218800e0faSSongjun Wu 
1228800e0faSSongjun Wu 		/* the max length of internal address is 3 bytes */
1238800e0faSSongjun Wu 		if (msg->len > 3)
1248800e0faSSongjun Wu 			return -EFAULT;
1258800e0faSSongjun Wu 
1268800e0faSSongjun Wu 		for (i = 0; i < msg->len; ++i) {
1278800e0faSSongjun Wu 			const unsigned addr = msg->buf[msg->len - 1 - i];
1288800e0faSSongjun Wu 
1298800e0faSSongjun Wu 			internal_address |= addr << (8 * i);
1308800e0faSSongjun Wu 			int_addr_flag += TWI_MMR_IADRSZ_1;
1318800e0faSSongjun Wu 		}
1328800e0faSSongjun Wu 
1338800e0faSSongjun Wu 		writel(internal_address, &reg->iadr);
1348800e0faSSongjun Wu 	}
1358800e0faSSongjun Wu 
1368800e0faSSongjun Wu 	is_read = m_start->flags & I2C_M_RD;
1378800e0faSSongjun Wu 
1388800e0faSSongjun Wu 	writel((m_start->addr << 16) | int_addr_flag |
1398800e0faSSongjun Wu 	       (is_read ? TWI_MMR_MREAD : 0), &reg->mmr);
1408800e0faSSongjun Wu 
1418800e0faSSongjun Wu 	ret = at91_i2c_xfer_msg(bus, m_start);
1428800e0faSSongjun Wu 
1438800e0faSSongjun Wu 	return ret;
1448800e0faSSongjun Wu }
1458800e0faSSongjun Wu 
1468800e0faSSongjun Wu /*
1478800e0faSSongjun Wu  * Calculate symmetric clock as stated in datasheet:
1488800e0faSSongjun Wu  * twi_clk = F_MAIN / (2 * (cdiv * (1 << ckdiv) + offset))
1498800e0faSSongjun Wu  */
1508800e0faSSongjun Wu static void at91_calc_i2c_clock(struct udevice *dev, int i2c_clk)
1518800e0faSSongjun Wu {
1528800e0faSSongjun Wu 	struct at91_i2c_bus *bus = dev_get_priv(dev);
1538800e0faSSongjun Wu 	const struct at91_i2c_pdata *pdata = bus->pdata;
1548800e0faSSongjun Wu 	int offset = pdata->clk_offset;
1558800e0faSSongjun Wu 	int max_ckdiv = pdata->clk_max_div;
1568800e0faSSongjun Wu 	int ckdiv, cdiv, div;
1578800e0faSSongjun Wu 	unsigned long src_rate;
1588800e0faSSongjun Wu 
1598800e0faSSongjun Wu 	src_rate = bus->bus_clk_rate;
1608800e0faSSongjun Wu 
1618800e0faSSongjun Wu 	div = max(0, (int)DIV_ROUND_UP(src_rate, 2 * i2c_clk) - offset);
1628800e0faSSongjun Wu 	ckdiv = fls(div >> 8);
1638800e0faSSongjun Wu 	cdiv = div >> ckdiv;
1648800e0faSSongjun Wu 
1658800e0faSSongjun Wu 	if (ckdiv > max_ckdiv) {
1668800e0faSSongjun Wu 		ckdiv = max_ckdiv;
1678800e0faSSongjun Wu 		cdiv = 255;
1688800e0faSSongjun Wu 	}
1698800e0faSSongjun Wu 
1708800e0faSSongjun Wu 	bus->speed = DIV_ROUND_UP(src_rate,
1718800e0faSSongjun Wu 				  (cdiv * (1 << ckdiv) + offset) * 2);
1728800e0faSSongjun Wu 
1738800e0faSSongjun Wu 	bus->cwgr_val = (ckdiv << 16) | (cdiv << 8) | cdiv;
1748800e0faSSongjun Wu }
1758800e0faSSongjun Wu 
1768800e0faSSongjun Wu static int at91_i2c_enable_clk(struct udevice *dev)
1778800e0faSSongjun Wu {
1788800e0faSSongjun Wu 	struct at91_i2c_bus *bus = dev_get_priv(dev);
1798800e0faSSongjun Wu 	struct clk clk;
1808800e0faSSongjun Wu 	ulong clk_rate;
1818800e0faSSongjun Wu 	int ret;
1828800e0faSSongjun Wu 
1838800e0faSSongjun Wu 	ret = clk_get_by_index(dev, 0, &clk);
1848800e0faSSongjun Wu 	if (ret)
1858800e0faSSongjun Wu 		return -EINVAL;
1868800e0faSSongjun Wu 
1878800e0faSSongjun Wu 	ret = clk_enable(&clk);
1888800e0faSSongjun Wu 	if (ret)
1898800e0faSSongjun Wu 		return ret;
1908800e0faSSongjun Wu 
1918800e0faSSongjun Wu 	clk_rate = clk_get_rate(&clk);
1928800e0faSSongjun Wu 	if (!clk_rate)
19352f37333SWenyou Yang 		return -EINVAL;
1948800e0faSSongjun Wu 
1958800e0faSSongjun Wu 	bus->bus_clk_rate = clk_rate;
1968800e0faSSongjun Wu 
1978800e0faSSongjun Wu 	clk_free(&clk);
1988800e0faSSongjun Wu 
1998800e0faSSongjun Wu 	return 0;
2008800e0faSSongjun Wu }
2018800e0faSSongjun Wu 
2028800e0faSSongjun Wu static int at91_i2c_probe(struct udevice *dev, uint chip, uint chip_flags)
2038800e0faSSongjun Wu {
2048800e0faSSongjun Wu 	struct at91_i2c_bus *bus = dev_get_priv(dev);
2058800e0faSSongjun Wu 	struct at91_i2c_regs *reg = bus->regs;
2068800e0faSSongjun Wu 	int ret;
2078800e0faSSongjun Wu 
2088800e0faSSongjun Wu 	ret = at91_i2c_enable_clk(dev);
2098800e0faSSongjun Wu 	if (ret)
2108800e0faSSongjun Wu 		return ret;
2118800e0faSSongjun Wu 
2128800e0faSSongjun Wu 	writel(TWI_CR_SWRST, &reg->cr);
2138800e0faSSongjun Wu 
2148800e0faSSongjun Wu 	at91_calc_i2c_clock(dev, bus->clock_frequency);
2158800e0faSSongjun Wu 
2168800e0faSSongjun Wu 	writel(bus->cwgr_val, &reg->cwgr);
2178800e0faSSongjun Wu 	writel(TWI_CR_MSEN, &reg->cr);
2188800e0faSSongjun Wu 	writel(TWI_CR_SVDIS, &reg->cr);
2198800e0faSSongjun Wu 
2208800e0faSSongjun Wu 	return 0;
2218800e0faSSongjun Wu }
2228800e0faSSongjun Wu 
2238800e0faSSongjun Wu static int at91_i2c_set_bus_speed(struct udevice *dev, unsigned int speed)
2248800e0faSSongjun Wu {
2258800e0faSSongjun Wu 	struct at91_i2c_bus *bus = dev_get_priv(dev);
2268800e0faSSongjun Wu 
2278800e0faSSongjun Wu 	at91_calc_i2c_clock(dev, speed);
2288800e0faSSongjun Wu 
2298800e0faSSongjun Wu 	writel(bus->cwgr_val, &bus->regs->cwgr);
2308800e0faSSongjun Wu 
2318800e0faSSongjun Wu 	return 0;
2328800e0faSSongjun Wu }
2338800e0faSSongjun Wu 
2348800e0faSSongjun Wu int at91_i2c_get_bus_speed(struct udevice *dev)
2358800e0faSSongjun Wu {
2368800e0faSSongjun Wu 	struct at91_i2c_bus *bus = dev_get_priv(dev);
2378800e0faSSongjun Wu 
2388800e0faSSongjun Wu 	return bus->speed;
2398800e0faSSongjun Wu }
2408800e0faSSongjun Wu 
2418800e0faSSongjun Wu static int at91_i2c_ofdata_to_platdata(struct udevice *dev)
2428800e0faSSongjun Wu {
2438800e0faSSongjun Wu 	const void *blob = gd->fdt_blob;
2448800e0faSSongjun Wu 	struct at91_i2c_bus *bus = dev_get_priv(dev);
245*e160f7d4SSimon Glass 	int node = dev_of_offset(dev);
2468800e0faSSongjun Wu 
2478800e0faSSongjun Wu 	bus->regs = (struct at91_i2c_regs *)dev_get_addr(dev);
2488800e0faSSongjun Wu 	bus->pdata = (struct at91_i2c_pdata *)dev_get_driver_data(dev);
2498800e0faSSongjun Wu 	bus->clock_frequency = fdtdec_get_int(blob, node,
2508800e0faSSongjun Wu 					      "clock-frequency", 100000);
2518800e0faSSongjun Wu 
2528800e0faSSongjun Wu 	return 0;
2538800e0faSSongjun Wu }
2548800e0faSSongjun Wu 
2558800e0faSSongjun Wu static const struct dm_i2c_ops at91_i2c_ops = {
2568800e0faSSongjun Wu 	.xfer		= at91_i2c_xfer,
2578800e0faSSongjun Wu 	.probe_chip	= at91_i2c_probe,
2588800e0faSSongjun Wu 	.set_bus_speed	= at91_i2c_set_bus_speed,
2598800e0faSSongjun Wu 	.get_bus_speed	= at91_i2c_get_bus_speed,
2608800e0faSSongjun Wu };
2618800e0faSSongjun Wu 
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,
3188800e0faSSongjun Wu 	.ofdata_to_platdata = at91_i2c_ofdata_to_platdata,
3198800e0faSSongjun Wu 	.per_child_auto_alloc_size = sizeof(struct dm_i2c_chip),
3208800e0faSSongjun Wu 	.priv_auto_alloc_size = sizeof(struct at91_i2c_bus),
3218800e0faSSongjun Wu 	.ops	= &at91_i2c_ops,
3228800e0faSSongjun Wu };
323