blob: f5225f330d04a4bfd5ded1115818b083917932db [file] [log] [blame]
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +02001/*
2 * LPC32xx I2C interface driver
3 *
Sylvain Lemieuxfb51abb2015-08-04 17:04:41 -04004 * (C) Copyright 2014-2015 DENX Software Engineering GmbH
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +02005 * Written-by: Albert ARIBAUD - 3ADEV <albert.aribaud@3adev.fr>
6 *
7 * SPDX-License-Identifier: GPL-2.0+
Simon Glasscb052ff2016-11-23 06:34:44 -07008 *
9 * NOTE: This driver should be converted to driver model before June 2017.
10 * Please see doc/driver-model/i2c-howto.txt for instructions.
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +020011 */
12
13#include <common.h>
14#include <asm/io.h>
15#include <i2c.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090016#include <linux/errno.h>
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +020017#include <asm/arch/clk.h>
18
19/*
20 * Provide default speed and slave if target did not
21 */
22
23#if !defined(CONFIG_SYS_I2C_LPC32XX_SPEED)
24#define CONFIG_SYS_I2C_LPC32XX_SPEED 350000
25#endif
26
27#if !defined(CONFIG_SYS_I2C_LPC32XX_SLAVE)
28#define CONFIG_SYS_I2C_LPC32XX_SLAVE 0
29#endif
30
31/* i2c register set */
Liam Beguin326998e2017-03-27 11:11:36 -040032struct lpc32xx_i2c_base {
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +020033 union {
34 u32 rx;
35 u32 tx;
36 };
37 u32 stat;
38 u32 ctrl;
39 u32 clk_hi;
40 u32 clk_lo;
41 u32 adr;
42 u32 rxfl;
43 u32 txfl;
44 u32 rxb;
45 u32 txb;
46 u32 stx;
47 u32 stxfl;
48};
49
50/* TX register fields */
51#define LPC32XX_I2C_TX_START 0x00000100
52#define LPC32XX_I2C_TX_STOP 0x00000200
53
54/* Control register values */
55#define LPC32XX_I2C_SOFT_RESET 0x00000100
56
57/* Status register values */
58#define LPC32XX_I2C_STAT_TFF 0x00000400
59#define LPC32XX_I2C_STAT_RFE 0x00000200
60#define LPC32XX_I2C_STAT_DRMI 0x00000008
61#define LPC32XX_I2C_STAT_NAI 0x00000004
62#define LPC32XX_I2C_STAT_TDI 0x00000001
63
Liam Beguin326998e2017-03-27 11:11:36 -040064static struct lpc32xx_i2c_base *lpc32xx_i2c[] = {
65 (struct lpc32xx_i2c_base *)I2C1_BASE,
66 (struct lpc32xx_i2c_base *)I2C2_BASE,
67 (struct lpc32xx_i2c_base *)(USB_BASE + 0x300)
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +020068};
69
70/* Set I2C bus speed */
Liam Beguin326998e2017-03-27 11:11:36 -040071static unsigned int __i2c_set_bus_speed(struct lpc32xx_i2c_base *base,
72 unsigned int speed, unsigned int chip)
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +020073{
74 int half_period;
75
76 if (speed == 0)
77 return -EINVAL;
78
Vladimir Zapolskiy714642d2015-08-12 20:22:13 +030079 /* OTG I2C clock source and CLK registers are different */
Liam Beguin326998e2017-03-27 11:11:36 -040080 if (chip == 2) {
Vladimir Zapolskiy714642d2015-08-12 20:22:13 +030081 half_period = (get_periph_clk_rate() / speed) / 2;
82 if (half_period > 0xFF)
83 return -EINVAL;
84 } else {
85 half_period = (get_hclk_clk_rate() / speed) / 2;
86 if (half_period > 0x3FF)
87 return -EINVAL;
88 }
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +020089
Liam Beguin326998e2017-03-27 11:11:36 -040090 writel(half_period, &base->clk_hi);
91 writel(half_period, &base->clk_lo);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +020092 return 0;
93}
94
95/* I2C init called by cmd_i2c when doing 'i2c reset'. */
Liam Beguin326998e2017-03-27 11:11:36 -040096static void __i2c_init(struct lpc32xx_i2c_base *base,
97 int requested_speed, int slaveadd, unsigned int chip)
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +020098{
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +020099 /* soft reset (auto-clears) */
Liam Beguin326998e2017-03-27 11:11:36 -0400100 writel(LPC32XX_I2C_SOFT_RESET, &base->ctrl);
Vladimir Zapolskiy714642d2015-08-12 20:22:13 +0300101 /* set HI and LO periods for half of the default speed */
Liam Beguin326998e2017-03-27 11:11:36 -0400102 __i2c_set_bus_speed(base, requested_speed, chip);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200103}
104
105/* I2C probe called by cmd_i2c when doing 'i2c probe'. */
Liam Beguin326998e2017-03-27 11:11:36 -0400106static int __i2c_probe_chip(struct lpc32xx_i2c_base *base, u8 dev)
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200107{
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200108 int stat;
109
110 /* Soft-reset the controller */
Liam Beguin326998e2017-03-27 11:11:36 -0400111 writel(LPC32XX_I2C_SOFT_RESET, &base->ctrl);
112 while (readl(&base->ctrl) & LPC32XX_I2C_SOFT_RESET)
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200113 ;
114 /* Addre slave for write with start before and stop after */
115 writel((dev<<1) | LPC32XX_I2C_TX_START | LPC32XX_I2C_TX_STOP,
Liam Beguin326998e2017-03-27 11:11:36 -0400116 &base->tx);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200117 /* wait for end of transation */
Liam Beguin326998e2017-03-27 11:11:36 -0400118 while (!((stat = readl(&base->stat)) & LPC32XX_I2C_STAT_TDI))
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200119 ;
120 /* was there no acknowledge? */
121 return (stat & LPC32XX_I2C_STAT_NAI) ? -1 : 0;
122}
123
124/*
125 * I2C read called by cmd_i2c when doing 'i2c read' and by cmd_eeprom.c
126 * Begin write, send address byte(s), begin read, receive data bytes, end.
127 */
Liam Beguin326998e2017-03-27 11:11:36 -0400128static int __i2c_read(struct lpc32xx_i2c_base *base, u8 dev, uint addr,
129 int alen, u8 *data, int length)
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200130{
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200131 int stat, wlen;
132
133 /* Soft-reset the controller */
Liam Beguin326998e2017-03-27 11:11:36 -0400134 writel(LPC32XX_I2C_SOFT_RESET, &base->ctrl);
135 while (readl(&base->ctrl) & LPC32XX_I2C_SOFT_RESET)
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200136 ;
137 /* do we need to write an address at all? */
138 if (alen) {
139 /* Address slave in write mode */
Liam Beguin326998e2017-03-27 11:11:36 -0400140 writel((dev<<1) | LPC32XX_I2C_TX_START, &base->tx);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200141 /* write address bytes */
142 while (alen--) {
143 /* compute address byte + stop for the last one */
144 int a = (addr >> (8 * alen)) & 0xff;
145 if (!alen)
146 a |= LPC32XX_I2C_TX_STOP;
147 /* Send address byte */
Liam Beguin326998e2017-03-27 11:11:36 -0400148 writel(a, &base->tx);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200149 }
150 /* wait for end of transation */
Liam Beguin326998e2017-03-27 11:11:36 -0400151 while (!((stat = readl(&base->stat)) & LPC32XX_I2C_STAT_TDI))
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200152 ;
153 /* clear end-of-transaction flag */
Liam Beguin326998e2017-03-27 11:11:36 -0400154 writel(1, &base->stat);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200155 }
156 /* do we have to read data at all? */
157 if (length) {
158 /* Address slave in read mode */
Liam Beguin326998e2017-03-27 11:11:36 -0400159 writel(1 | (dev<<1) | LPC32XX_I2C_TX_START, &base->tx);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200160 wlen = length;
161 /* get data */
162 while (length | wlen) {
163 /* read status for TFF and RFE */
Liam Beguin326998e2017-03-27 11:11:36 -0400164 stat = readl(&base->stat);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200165 /* must we, can we write a trigger byte? */
166 if ((wlen > 0)
167 & (!(stat & LPC32XX_I2C_STAT_TFF))) {
168 wlen--;
169 /* write trigger byte + stop if last */
170 writel(wlen ? 0 :
Liam Beguin326998e2017-03-27 11:11:36 -0400171 LPC32XX_I2C_TX_STOP, &base->tx);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200172 }
173 /* must we, can we read a data byte? */
174 if ((length > 0)
175 & (!(stat & LPC32XX_I2C_STAT_RFE))) {
176 length--;
177 /* read byte */
Liam Beguin326998e2017-03-27 11:11:36 -0400178 *(data++) = readl(&base->rx);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200179 }
180 }
Sylvain Lemieuxdcdd9252015-07-27 13:37:38 -0400181 /* wait for end of transation */
Liam Beguin326998e2017-03-27 11:11:36 -0400182 while (!((stat = readl(&base->stat)) & LPC32XX_I2C_STAT_TDI))
Sylvain Lemieuxdcdd9252015-07-27 13:37:38 -0400183 ;
184 /* clear end-of-transaction flag */
Liam Beguin326998e2017-03-27 11:11:36 -0400185 writel(1, &base->stat);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200186 }
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200187 /* success */
188 return 0;
189}
190
191/*
192 * I2C write called by cmd_i2c when doing 'i2c write' and by cmd_eeprom.c
193 * Begin write, send address byte(s), send data bytes, end.
194 */
Liam Beguin326998e2017-03-27 11:11:36 -0400195static int __i2c_write(struct lpc32xx_i2c_base *base, u8 dev, uint addr,
196 int alen, u8 *data, int length)
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200197{
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200198 int stat;
199
200 /* Soft-reset the controller */
Liam Beguin326998e2017-03-27 11:11:36 -0400201 writel(LPC32XX_I2C_SOFT_RESET, &base->ctrl);
202 while (readl(&base->ctrl) & LPC32XX_I2C_SOFT_RESET)
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200203 ;
204 /* do we need to write anything at all? */
205 if (alen | length)
206 /* Address slave in write mode */
Liam Beguin326998e2017-03-27 11:11:36 -0400207 writel((dev<<1) | LPC32XX_I2C_TX_START, &base->tx);
Sylvain Lemieux2e6300f2015-07-27 13:37:39 -0400208 else
209 return 0;
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200210 /* write address bytes */
211 while (alen) {
212 /* wait for transmit fifo not full */
Liam Beguin326998e2017-03-27 11:11:36 -0400213 stat = readl(&base->stat);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200214 if (!(stat & LPC32XX_I2C_STAT_TFF)) {
215 alen--;
216 int a = (addr >> (8 * alen)) & 0xff;
217 if (!(alen | length))
218 a |= LPC32XX_I2C_TX_STOP;
219 /* Send address byte */
Liam Beguin326998e2017-03-27 11:11:36 -0400220 writel(a, &base->tx);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200221 }
222 }
223 while (length) {
224 /* wait for transmit fifo not full */
Liam Beguin326998e2017-03-27 11:11:36 -0400225 stat = readl(&base->stat);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200226 if (!(stat & LPC32XX_I2C_STAT_TFF)) {
227 /* compute data byte, add stop if length==0 */
228 length--;
229 int d = *(data++);
230 if (!length)
231 d |= LPC32XX_I2C_TX_STOP;
232 /* Send data byte */
Liam Beguin326998e2017-03-27 11:11:36 -0400233 writel(d, &base->tx);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200234 }
235 }
236 /* wait for end of transation */
Liam Beguin326998e2017-03-27 11:11:36 -0400237 while (!((stat = readl(&base->stat)) & LPC32XX_I2C_STAT_TDI))
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200238 ;
239 /* clear end-of-transaction flag */
Liam Beguin326998e2017-03-27 11:11:36 -0400240 writel(1, &base->stat);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200241 return 0;
242}
243
Liam Beguin3fe4a6c2017-03-14 11:24:40 -0400244static void lpc32xx_i2c_init(struct i2c_adapter *adap,
245 int requested_speed, int slaveadd)
246{
Liam Beguin326998e2017-03-27 11:11:36 -0400247 __i2c_init(lpc32xx_i2c[adap->hwadapnr], requested_speed, slaveadd,
248 adap->hwadapnr);
Liam Beguin3fe4a6c2017-03-14 11:24:40 -0400249}
250
251static int lpc32xx_i2c_probe_chip(struct i2c_adapter *adap, u8 dev)
252{
Liam Beguin326998e2017-03-27 11:11:36 -0400253 return __i2c_probe_chip(lpc32xx_i2c[adap->hwadapnr], dev);
Liam Beguin3fe4a6c2017-03-14 11:24:40 -0400254}
255
256static int lpc32xx_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr,
257 int alen, u8 *data, int length)
258{
Liam Beguin326998e2017-03-27 11:11:36 -0400259 return __i2c_read(lpc32xx_i2c[adap->hwadapnr], dev, addr,
260 alen, data, length);
Liam Beguin3fe4a6c2017-03-14 11:24:40 -0400261}
262
263static int lpc32xx_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr,
264 int alen, u8 *data, int length)
265{
Liam Beguin326998e2017-03-27 11:11:36 -0400266 return __i2c_write(lpc32xx_i2c[adap->hwadapnr], dev, addr,
267 alen, data, length);
Liam Beguin3fe4a6c2017-03-14 11:24:40 -0400268}
269
270static unsigned int lpc32xx_i2c_set_bus_speed(struct i2c_adapter *adap,
271 unsigned int speed)
272{
Liam Beguin326998e2017-03-27 11:11:36 -0400273 return __i2c_set_bus_speed(lpc32xx_i2c[adap->hwadapnr], speed,
274 adap->hwadapnr);
Liam Beguin3fe4a6c2017-03-14 11:24:40 -0400275}
276
277U_BOOT_I2C_ADAP_COMPLETE(lpc32xx_0, lpc32xx_i2c_init, lpc32xx_i2c_probe_chip,
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200278 lpc32xx_i2c_read, lpc32xx_i2c_write,
279 lpc32xx_i2c_set_bus_speed,
280 CONFIG_SYS_I2C_LPC32XX_SPEED,
281 CONFIG_SYS_I2C_LPC32XX_SLAVE,
282 0)
283
Liam Beguin3fe4a6c2017-03-14 11:24:40 -0400284U_BOOT_I2C_ADAP_COMPLETE(lpc32xx_1, lpc32xx_i2c_init, lpc32xx_i2c_probe_chip,
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200285 lpc32xx_i2c_read, lpc32xx_i2c_write,
286 lpc32xx_i2c_set_bus_speed,
287 CONFIG_SYS_I2C_LPC32XX_SPEED,
288 CONFIG_SYS_I2C_LPC32XX_SLAVE,
289 1)
Sylvain Lemieuxfb51abb2015-08-04 17:04:41 -0400290
Liam Beguin3fe4a6c2017-03-14 11:24:40 -0400291U_BOOT_I2C_ADAP_COMPLETE(lpc32xx_2, lpc32xx_i2c_init, NULL,
Sylvain Lemieuxfb51abb2015-08-04 17:04:41 -0400292 lpc32xx_i2c_read, lpc32xx_i2c_write,
293 lpc32xx_i2c_set_bus_speed,
294 100000,
295 0,
296 2)