2 * LPC32xx I2C interface driver
4 * (C) Copyright 2014 DENX Software Engineering GmbH
5 * Written-by: Albert ARIBAUD - 3ADEV <albert.aribaud@3adev.fr>
7 * SPDX-License-Identifier: GPL-2.0+
13 #include <asm/errno.h>
14 #include <asm/arch/clk.h>
17 * Provide default speed and slave if target did not
20 #if !defined(CONFIG_SYS_I2C_LPC32XX_SPEED)
21 #define CONFIG_SYS_I2C_LPC32XX_SPEED 350000
24 #if !defined(CONFIG_SYS_I2C_LPC32XX_SLAVE)
25 #define CONFIG_SYS_I2C_LPC32XX_SLAVE 0
28 /* i2c register set */
29 struct lpc32xx_i2c_registers {
47 /* TX register fields */
48 #define LPC32XX_I2C_TX_START 0x00000100
49 #define LPC32XX_I2C_TX_STOP 0x00000200
51 /* Control register values */
52 #define LPC32XX_I2C_SOFT_RESET 0x00000100
54 /* Status register values */
55 #define LPC32XX_I2C_STAT_TFF 0x00000400
56 #define LPC32XX_I2C_STAT_RFE 0x00000200
57 #define LPC32XX_I2C_STAT_DRMI 0x00000008
58 #define LPC32XX_I2C_STAT_NAI 0x00000004
59 #define LPC32XX_I2C_STAT_TDI 0x00000001
61 static struct lpc32xx_i2c_registers *lpc32xx_i2c[] = {
62 (struct lpc32xx_i2c_registers *)I2C1_BASE,
63 (struct lpc32xx_i2c_registers *)I2C2_BASE
66 /* Set I2C bus speed */
67 static unsigned int lpc32xx_i2c_set_bus_speed(struct i2c_adapter *adap,
75 half_period = (105000000 / speed) / 2;
77 if ((half_period > 255) || (half_period < 0))
80 writel(half_period, &lpc32xx_i2c[adap->hwadapnr]->clk_hi);
81 writel(half_period, &lpc32xx_i2c[adap->hwadapnr]->clk_lo);
85 /* I2C init called by cmd_i2c when doing 'i2c reset'. */
86 static void _i2c_init(struct i2c_adapter *adap,
87 int requested_speed, int slaveadd)
89 struct lpc32xx_i2c_registers *i2c = lpc32xx_i2c[adap->hwadapnr];
91 /* soft reset (auto-clears) */
92 writel(LPC32XX_I2C_SOFT_RESET, &i2c->ctrl);
93 /* set HI and LO periods for about 350 kHz */
94 lpc32xx_i2c_set_bus_speed(adap, requested_speed);
97 /* I2C probe called by cmd_i2c when doing 'i2c probe'. */
98 static int lpc32xx_i2c_probe(struct i2c_adapter *adap, u8 dev)
100 struct lpc32xx_i2c_registers *i2c = lpc32xx_i2c[adap->hwadapnr];
103 /* Soft-reset the controller */
104 writel(LPC32XX_I2C_SOFT_RESET, &i2c->ctrl);
105 while (readl(&i2c->ctrl) & LPC32XX_I2C_SOFT_RESET)
107 /* Addre slave for write with start before and stop after */
108 writel((dev<<1) | LPC32XX_I2C_TX_START | LPC32XX_I2C_TX_STOP,
110 /* wait for end of transation */
111 while (!((stat = readl(&i2c->stat)) & LPC32XX_I2C_STAT_TDI))
113 /* was there no acknowledge? */
114 return (stat & LPC32XX_I2C_STAT_NAI) ? -1 : 0;
118 * I2C read called by cmd_i2c when doing 'i2c read' and by cmd_eeprom.c
119 * Begin write, send address byte(s), begin read, receive data bytes, end.
121 static int lpc32xx_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr,
122 int alen, u8 *data, int length)
124 struct lpc32xx_i2c_registers *i2c = lpc32xx_i2c[adap->hwadapnr];
127 /* Soft-reset the controller */
128 writel(LPC32XX_I2C_SOFT_RESET, &i2c->ctrl);
129 while (readl(&i2c->ctrl) & LPC32XX_I2C_SOFT_RESET)
131 /* do we need to write an address at all? */
133 /* Address slave in write mode */
134 writel((dev<<1) | LPC32XX_I2C_TX_START, &i2c->tx);
135 /* write address bytes */
137 /* compute address byte + stop for the last one */
138 int a = (addr >> (8 * alen)) & 0xff;
140 a |= LPC32XX_I2C_TX_STOP;
141 /* Send address byte */
144 /* wait for end of transation */
145 while (!((stat = readl(&i2c->stat)) & LPC32XX_I2C_STAT_TDI))
147 /* clear end-of-transaction flag */
148 writel(1, &i2c->stat);
150 /* do we have to read data at all? */
152 /* Address slave in read mode */
153 writel(1 | (dev<<1) | LPC32XX_I2C_TX_START, &i2c->tx);
156 while (length | wlen) {
157 /* read status for TFF and RFE */
158 stat = readl(&i2c->stat);
159 /* must we, can we write a trigger byte? */
161 & (!(stat & LPC32XX_I2C_STAT_TFF))) {
163 /* write trigger byte + stop if last */
165 LPC32XX_I2C_TX_STOP, &i2c->tx);
167 /* must we, can we read a data byte? */
169 & (!(stat & LPC32XX_I2C_STAT_RFE))) {
172 *(data++) = readl(&i2c->rx);
176 /* wait for end of transation */
177 while (!((stat = readl(&i2c->stat)) & LPC32XX_I2C_STAT_TDI))
179 /* clear end-of-transaction flag */
180 writel(1, &i2c->stat);
186 * I2C write called by cmd_i2c when doing 'i2c write' and by cmd_eeprom.c
187 * Begin write, send address byte(s), send data bytes, end.
189 static int lpc32xx_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr,
190 int alen, u8 *data, int length)
192 struct lpc32xx_i2c_registers *i2c = lpc32xx_i2c[adap->hwadapnr];
195 /* Soft-reset the controller */
196 writel(LPC32XX_I2C_SOFT_RESET, &i2c->ctrl);
197 while (readl(&i2c->ctrl) & LPC32XX_I2C_SOFT_RESET)
199 /* do we need to write anything at all? */
201 /* Address slave in write mode */
202 writel((dev<<1) | LPC32XX_I2C_TX_START, &i2c->tx);
203 /* write address bytes */
205 /* wait for transmit fifo not full */
206 stat = readl(&i2c->stat);
207 if (!(stat & LPC32XX_I2C_STAT_TFF)) {
209 int a = (addr >> (8 * alen)) & 0xff;
210 if (!(alen | length))
211 a |= LPC32XX_I2C_TX_STOP;
212 /* Send address byte */
217 /* wait for transmit fifo not full */
218 stat = readl(&i2c->stat);
219 if (!(stat & LPC32XX_I2C_STAT_TFF)) {
220 /* compute data byte, add stop if length==0 */
224 d |= LPC32XX_I2C_TX_STOP;
229 /* wait for end of transation */
230 while (!((stat = readl(&i2c->stat)) & LPC32XX_I2C_STAT_TDI))
232 /* clear end-of-transaction flag */
233 writel(1, &i2c->stat);
237 U_BOOT_I2C_ADAP_COMPLETE(lpc32xx_0, _i2c_init, lpc32xx_i2c_probe,
238 lpc32xx_i2c_read, lpc32xx_i2c_write,
239 lpc32xx_i2c_set_bus_speed,
240 CONFIG_SYS_I2C_LPC32XX_SPEED,
241 CONFIG_SYS_I2C_LPC32XX_SLAVE,
244 U_BOOT_I2C_ADAP_COMPLETE(lpc32xx_1, _i2c_init, lpc32xx_i2c_probe,
245 lpc32xx_i2c_read, lpc32xx_i2c_write,
246 lpc32xx_i2c_set_bus_speed,
247 CONFIG_SYS_I2C_LPC32XX_SPEED,
248 CONFIG_SYS_I2C_LPC32XX_SLAVE,