xref: /openbmc/u-boot/drivers/i2c/at91_i2c.c (revision 76062b9cdbe756eff75b99beaf6e94e8bb059431)
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>
11*76062b9cSWenyou 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 udevice *dev_clk;
1808800e0faSSongjun Wu 	struct clk clk;
1818800e0faSSongjun Wu 	ulong clk_rate;
1828800e0faSSongjun Wu 	int periph;
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 	periph = fdtdec_get_uint(gd->fdt_blob, clk.dev->of_offset, "reg", -1);
1908800e0faSSongjun Wu 	if (periph < 0)
1918800e0faSSongjun Wu 		return -EINVAL;
1928800e0faSSongjun Wu 
1938800e0faSSongjun Wu 	dev_clk = dev_get_parent(clk.dev);
1948800e0faSSongjun Wu 	ret = clk_request(dev_clk, &clk);
1958800e0faSSongjun Wu 	if (ret)
1968800e0faSSongjun Wu 		return ret;
1978800e0faSSongjun Wu 
1988800e0faSSongjun Wu 	clk.id = periph;
1998800e0faSSongjun Wu 	ret = clk_enable(&clk);
2008800e0faSSongjun Wu 	if (ret)
2018800e0faSSongjun Wu 		return ret;
2028800e0faSSongjun Wu 
2038800e0faSSongjun Wu 	ret = clk_get_by_index(dev_clk, 0, &clk);
2048800e0faSSongjun Wu 	if (ret)
2058800e0faSSongjun Wu 		return ret;
2068800e0faSSongjun Wu 
2078800e0faSSongjun Wu 	clk_rate = clk_get_rate(&clk);
2088800e0faSSongjun Wu 	if (!clk_rate)
2098800e0faSSongjun Wu 		return -ENODEV;
2108800e0faSSongjun Wu 
2118800e0faSSongjun Wu 	bus->bus_clk_rate = clk_rate;
2128800e0faSSongjun Wu 
2138800e0faSSongjun Wu 	clk_free(&clk);
2148800e0faSSongjun Wu 
2158800e0faSSongjun Wu 	return 0;
2168800e0faSSongjun Wu }
2178800e0faSSongjun Wu 
2188800e0faSSongjun Wu static int at91_i2c_probe(struct udevice *dev, uint chip, uint chip_flags)
2198800e0faSSongjun Wu {
2208800e0faSSongjun Wu 	struct at91_i2c_bus *bus = dev_get_priv(dev);
2218800e0faSSongjun Wu 	struct at91_i2c_regs *reg = bus->regs;
2228800e0faSSongjun Wu 	int ret;
2238800e0faSSongjun Wu 
2248800e0faSSongjun Wu 	ret = at91_i2c_enable_clk(dev);
2258800e0faSSongjun Wu 	if (ret)
2268800e0faSSongjun Wu 		return ret;
2278800e0faSSongjun Wu 
2288800e0faSSongjun Wu 	writel(TWI_CR_SWRST, &reg->cr);
2298800e0faSSongjun Wu 
2308800e0faSSongjun Wu 	at91_calc_i2c_clock(dev, bus->clock_frequency);
2318800e0faSSongjun Wu 
2328800e0faSSongjun Wu 	writel(bus->cwgr_val, &reg->cwgr);
2338800e0faSSongjun Wu 	writel(TWI_CR_MSEN, &reg->cr);
2348800e0faSSongjun Wu 	writel(TWI_CR_SVDIS, &reg->cr);
2358800e0faSSongjun Wu 
2368800e0faSSongjun Wu 	return 0;
2378800e0faSSongjun Wu }
2388800e0faSSongjun Wu 
2398800e0faSSongjun Wu static int at91_i2c_set_bus_speed(struct udevice *dev, unsigned int speed)
2408800e0faSSongjun Wu {
2418800e0faSSongjun Wu 	struct at91_i2c_bus *bus = dev_get_priv(dev);
2428800e0faSSongjun Wu 
2438800e0faSSongjun Wu 	at91_calc_i2c_clock(dev, speed);
2448800e0faSSongjun Wu 
2458800e0faSSongjun Wu 	writel(bus->cwgr_val, &bus->regs->cwgr);
2468800e0faSSongjun Wu 
2478800e0faSSongjun Wu 	return 0;
2488800e0faSSongjun Wu }
2498800e0faSSongjun Wu 
2508800e0faSSongjun Wu int at91_i2c_get_bus_speed(struct udevice *dev)
2518800e0faSSongjun Wu {
2528800e0faSSongjun Wu 	struct at91_i2c_bus *bus = dev_get_priv(dev);
2538800e0faSSongjun Wu 
2548800e0faSSongjun Wu 	return bus->speed;
2558800e0faSSongjun Wu }
2568800e0faSSongjun Wu 
2578800e0faSSongjun Wu static int at91_i2c_ofdata_to_platdata(struct udevice *dev)
2588800e0faSSongjun Wu {
2598800e0faSSongjun Wu 	const void *blob = gd->fdt_blob;
2608800e0faSSongjun Wu 	struct at91_i2c_bus *bus = dev_get_priv(dev);
2618800e0faSSongjun Wu 	int node = dev->of_offset;
2628800e0faSSongjun Wu 
2638800e0faSSongjun Wu 	bus->regs = (struct at91_i2c_regs *)dev_get_addr(dev);
2648800e0faSSongjun Wu 	bus->pdata = (struct at91_i2c_pdata *)dev_get_driver_data(dev);
2658800e0faSSongjun Wu 	bus->clock_frequency = fdtdec_get_int(blob, node,
2668800e0faSSongjun Wu 					      "clock-frequency", 100000);
2678800e0faSSongjun Wu 
2688800e0faSSongjun Wu 	return 0;
2698800e0faSSongjun Wu }
2708800e0faSSongjun Wu 
2718800e0faSSongjun Wu static const struct dm_i2c_ops at91_i2c_ops = {
2728800e0faSSongjun Wu 	.xfer		= at91_i2c_xfer,
2738800e0faSSongjun Wu 	.probe_chip	= at91_i2c_probe,
2748800e0faSSongjun Wu 	.set_bus_speed	= at91_i2c_set_bus_speed,
2758800e0faSSongjun Wu 	.get_bus_speed	= at91_i2c_get_bus_speed,
2768800e0faSSongjun Wu };
2778800e0faSSongjun Wu 
2788800e0faSSongjun Wu static const struct at91_i2c_pdata at91rm9200_config = {
2798800e0faSSongjun Wu 	.clk_max_div = 5,
2808800e0faSSongjun Wu 	.clk_offset = 3,
2818800e0faSSongjun Wu };
2828800e0faSSongjun Wu 
2838800e0faSSongjun Wu static const struct at91_i2c_pdata at91sam9261_config = {
2848800e0faSSongjun Wu 	.clk_max_div = 5,
2858800e0faSSongjun Wu 	.clk_offset = 4,
2868800e0faSSongjun Wu };
2878800e0faSSongjun Wu 
2888800e0faSSongjun Wu static const struct at91_i2c_pdata at91sam9260_config = {
2898800e0faSSongjun Wu 	.clk_max_div = 7,
2908800e0faSSongjun Wu 	.clk_offset = 4,
2918800e0faSSongjun Wu };
2928800e0faSSongjun Wu 
2938800e0faSSongjun Wu static const struct at91_i2c_pdata at91sam9g20_config = {
2948800e0faSSongjun Wu 	.clk_max_div = 7,
2958800e0faSSongjun Wu 	.clk_offset = 4,
2968800e0faSSongjun Wu };
2978800e0faSSongjun Wu 
2988800e0faSSongjun Wu static const struct at91_i2c_pdata at91sam9g10_config = {
2998800e0faSSongjun Wu 	.clk_max_div = 7,
3008800e0faSSongjun Wu 	.clk_offset = 4,
3018800e0faSSongjun Wu };
3028800e0faSSongjun Wu 
3038800e0faSSongjun Wu static const struct at91_i2c_pdata at91sam9x5_config = {
3048800e0faSSongjun Wu 	.clk_max_div = 7,
3058800e0faSSongjun Wu 	.clk_offset = 4,
3068800e0faSSongjun Wu };
3078800e0faSSongjun Wu 
3088800e0faSSongjun Wu static const struct at91_i2c_pdata sama5d4_config = {
3098800e0faSSongjun Wu 	.clk_max_div = 7,
3108800e0faSSongjun Wu 	.clk_offset = 4,
3118800e0faSSongjun Wu };
3128800e0faSSongjun Wu 
3138800e0faSSongjun Wu static const struct at91_i2c_pdata sama5d2_config = {
3148800e0faSSongjun Wu 	.clk_max_div = 7,
3158800e0faSSongjun Wu 	.clk_offset = 3,
3168800e0faSSongjun Wu };
3178800e0faSSongjun Wu 
3188800e0faSSongjun Wu static const struct udevice_id at91_i2c_ids[] = {
3198800e0faSSongjun Wu { .compatible = "atmel,at91rm9200-i2c", .data = (long)&at91rm9200_config },
3208800e0faSSongjun Wu { .compatible = "atmel,at91sam9260-i2c", .data = (long)&at91sam9260_config },
3218800e0faSSongjun Wu { .compatible = "atmel,at91sam9261-i2c", .data = (long)&at91sam9261_config },
3228800e0faSSongjun Wu { .compatible = "atmel,at91sam9g20-i2c", .data = (long)&at91sam9g20_config },
3238800e0faSSongjun Wu { .compatible = "atmel,at91sam9g10-i2c", .data = (long)&at91sam9g10_config },
3248800e0faSSongjun Wu { .compatible = "atmel,at91sam9x5-i2c", .data = (long)&at91sam9x5_config },
3258800e0faSSongjun Wu { .compatible = "atmel,sama5d4-i2c", .data = (long)&sama5d4_config },
3268800e0faSSongjun Wu { .compatible = "atmel,sama5d2-i2c", .data = (long)&sama5d2_config },
3278800e0faSSongjun Wu { }
3288800e0faSSongjun Wu };
3298800e0faSSongjun Wu 
3308800e0faSSongjun Wu U_BOOT_DRIVER(i2c_at91) = {
3318800e0faSSongjun Wu 	.name	= "i2c_at91",
3328800e0faSSongjun Wu 	.id	= UCLASS_I2C,
3338800e0faSSongjun Wu 	.of_match = at91_i2c_ids,
3348800e0faSSongjun Wu 	.ofdata_to_platdata = at91_i2c_ofdata_to_platdata,
3358800e0faSSongjun Wu 	.per_child_auto_alloc_size = sizeof(struct dm_i2c_chip),
3368800e0faSSongjun Wu 	.priv_auto_alloc_size = sizeof(struct at91_i2c_bus),
3378800e0faSSongjun Wu 	.ops	= &at91_i2c_ops,
3388800e0faSSongjun Wu };
339