blob: b4d1014b8d909f698a725c97df0eb4cc2a243764 [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>
Liam Beguin13232b92017-03-27 11:13:12 -040018#include <dm.h>
19#include <mapmem.h>
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +020020
21/*
22 * Provide default speed and slave if target did not
23 */
24
25#if !defined(CONFIG_SYS_I2C_LPC32XX_SPEED)
26#define CONFIG_SYS_I2C_LPC32XX_SPEED 350000
27#endif
28
29#if !defined(CONFIG_SYS_I2C_LPC32XX_SLAVE)
30#define CONFIG_SYS_I2C_LPC32XX_SLAVE 0
31#endif
32
33/* i2c register set */
Liam Beguin326998e2017-03-27 11:11:36 -040034struct lpc32xx_i2c_base {
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +020035 union {
36 u32 rx;
37 u32 tx;
38 };
39 u32 stat;
40 u32 ctrl;
41 u32 clk_hi;
42 u32 clk_lo;
43 u32 adr;
44 u32 rxfl;
45 u32 txfl;
46 u32 rxb;
47 u32 txb;
48 u32 stx;
49 u32 stxfl;
50};
51
Liam Beguin13232b92017-03-27 11:13:12 -040052#ifdef CONFIG_DM_I2C
53struct lpc32xx_i2c_dev {
54 struct lpc32xx_i2c_base *base;
55 int index;
56 uint speed;
57};
58#endif /* CONFIG_DM_I2C */
59
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +020060/* TX register fields */
61#define LPC32XX_I2C_TX_START 0x00000100
62#define LPC32XX_I2C_TX_STOP 0x00000200
63
64/* Control register values */
65#define LPC32XX_I2C_SOFT_RESET 0x00000100
66
67/* Status register values */
68#define LPC32XX_I2C_STAT_TFF 0x00000400
69#define LPC32XX_I2C_STAT_RFE 0x00000200
70#define LPC32XX_I2C_STAT_DRMI 0x00000008
71#define LPC32XX_I2C_STAT_NAI 0x00000004
72#define LPC32XX_I2C_STAT_TDI 0x00000001
73
Liam Beguin13232b92017-03-27 11:13:12 -040074#ifndef CONFIG_DM_I2C
Liam Beguin326998e2017-03-27 11:11:36 -040075static struct lpc32xx_i2c_base *lpc32xx_i2c[] = {
76 (struct lpc32xx_i2c_base *)I2C1_BASE,
77 (struct lpc32xx_i2c_base *)I2C2_BASE,
78 (struct lpc32xx_i2c_base *)(USB_BASE + 0x300)
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +020079};
Liam Beguin13232b92017-03-27 11:13:12 -040080#endif
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +020081
82/* Set I2C bus speed */
Liam Beguin326998e2017-03-27 11:11:36 -040083static unsigned int __i2c_set_bus_speed(struct lpc32xx_i2c_base *base,
84 unsigned int speed, unsigned int chip)
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +020085{
86 int half_period;
87
88 if (speed == 0)
89 return -EINVAL;
90
Vladimir Zapolskiy714642d2015-08-12 20:22:13 +030091 /* OTG I2C clock source and CLK registers are different */
Liam Beguin326998e2017-03-27 11:11:36 -040092 if (chip == 2) {
Vladimir Zapolskiy714642d2015-08-12 20:22:13 +030093 half_period = (get_periph_clk_rate() / speed) / 2;
94 if (half_period > 0xFF)
95 return -EINVAL;
96 } else {
97 half_period = (get_hclk_clk_rate() / speed) / 2;
98 if (half_period > 0x3FF)
99 return -EINVAL;
100 }
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200101
Liam Beguin326998e2017-03-27 11:11:36 -0400102 writel(half_period, &base->clk_hi);
103 writel(half_period, &base->clk_lo);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200104 return 0;
105}
106
107/* I2C init called by cmd_i2c when doing 'i2c reset'. */
Liam Beguin326998e2017-03-27 11:11:36 -0400108static void __i2c_init(struct lpc32xx_i2c_base *base,
109 int requested_speed, int slaveadd, unsigned int chip)
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200110{
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200111 /* soft reset (auto-clears) */
Liam Beguin326998e2017-03-27 11:11:36 -0400112 writel(LPC32XX_I2C_SOFT_RESET, &base->ctrl);
Vladimir Zapolskiy714642d2015-08-12 20:22:13 +0300113 /* set HI and LO periods for half of the default speed */
Liam Beguin326998e2017-03-27 11:11:36 -0400114 __i2c_set_bus_speed(base, requested_speed, chip);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200115}
116
117/* I2C probe called by cmd_i2c when doing 'i2c probe'. */
Liam Beguin326998e2017-03-27 11:11:36 -0400118static int __i2c_probe_chip(struct lpc32xx_i2c_base *base, u8 dev)
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200119{
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200120 int stat;
121
122 /* Soft-reset the controller */
Liam Beguin326998e2017-03-27 11:11:36 -0400123 writel(LPC32XX_I2C_SOFT_RESET, &base->ctrl);
124 while (readl(&base->ctrl) & LPC32XX_I2C_SOFT_RESET)
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200125 ;
126 /* Addre slave for write with start before and stop after */
127 writel((dev<<1) | LPC32XX_I2C_TX_START | LPC32XX_I2C_TX_STOP,
Liam Beguin326998e2017-03-27 11:11:36 -0400128 &base->tx);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200129 /* wait for end of transation */
Liam Beguin326998e2017-03-27 11:11:36 -0400130 while (!((stat = readl(&base->stat)) & LPC32XX_I2C_STAT_TDI))
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200131 ;
132 /* was there no acknowledge? */
133 return (stat & LPC32XX_I2C_STAT_NAI) ? -1 : 0;
134}
135
136/*
137 * I2C read called by cmd_i2c when doing 'i2c read' and by cmd_eeprom.c
138 * Begin write, send address byte(s), begin read, receive data bytes, end.
139 */
Liam Beguin326998e2017-03-27 11:11:36 -0400140static int __i2c_read(struct lpc32xx_i2c_base *base, u8 dev, uint addr,
141 int alen, u8 *data, int length)
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200142{
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200143 int stat, wlen;
144
145 /* Soft-reset the controller */
Liam Beguin326998e2017-03-27 11:11:36 -0400146 writel(LPC32XX_I2C_SOFT_RESET, &base->ctrl);
147 while (readl(&base->ctrl) & LPC32XX_I2C_SOFT_RESET)
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200148 ;
149 /* do we need to write an address at all? */
150 if (alen) {
151 /* Address slave in write mode */
Liam Beguin326998e2017-03-27 11:11:36 -0400152 writel((dev<<1) | LPC32XX_I2C_TX_START, &base->tx);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200153 /* write address bytes */
154 while (alen--) {
155 /* compute address byte + stop for the last one */
156 int a = (addr >> (8 * alen)) & 0xff;
157 if (!alen)
158 a |= LPC32XX_I2C_TX_STOP;
159 /* Send address byte */
Liam Beguin326998e2017-03-27 11:11:36 -0400160 writel(a, &base->tx);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200161 }
162 /* wait for end of transation */
Liam Beguin326998e2017-03-27 11:11:36 -0400163 while (!((stat = readl(&base->stat)) & LPC32XX_I2C_STAT_TDI))
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200164 ;
165 /* clear end-of-transaction flag */
Liam Beguin326998e2017-03-27 11:11:36 -0400166 writel(1, &base->stat);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200167 }
168 /* do we have to read data at all? */
169 if (length) {
170 /* Address slave in read mode */
Liam Beguin326998e2017-03-27 11:11:36 -0400171 writel(1 | (dev<<1) | LPC32XX_I2C_TX_START, &base->tx);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200172 wlen = length;
173 /* get data */
174 while (length | wlen) {
175 /* read status for TFF and RFE */
Liam Beguin326998e2017-03-27 11:11:36 -0400176 stat = readl(&base->stat);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200177 /* must we, can we write a trigger byte? */
178 if ((wlen > 0)
179 & (!(stat & LPC32XX_I2C_STAT_TFF))) {
180 wlen--;
181 /* write trigger byte + stop if last */
182 writel(wlen ? 0 :
Liam Beguin326998e2017-03-27 11:11:36 -0400183 LPC32XX_I2C_TX_STOP, &base->tx);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200184 }
185 /* must we, can we read a data byte? */
186 if ((length > 0)
187 & (!(stat & LPC32XX_I2C_STAT_RFE))) {
188 length--;
189 /* read byte */
Liam Beguin326998e2017-03-27 11:11:36 -0400190 *(data++) = readl(&base->rx);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200191 }
192 }
Sylvain Lemieuxdcdd9252015-07-27 13:37:38 -0400193 /* wait for end of transation */
Liam Beguin326998e2017-03-27 11:11:36 -0400194 while (!((stat = readl(&base->stat)) & LPC32XX_I2C_STAT_TDI))
Sylvain Lemieuxdcdd9252015-07-27 13:37:38 -0400195 ;
196 /* clear end-of-transaction flag */
Liam Beguin326998e2017-03-27 11:11:36 -0400197 writel(1, &base->stat);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200198 }
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200199 /* success */
200 return 0;
201}
202
203/*
204 * I2C write called by cmd_i2c when doing 'i2c write' and by cmd_eeprom.c
205 * Begin write, send address byte(s), send data bytes, end.
206 */
Liam Beguin326998e2017-03-27 11:11:36 -0400207static int __i2c_write(struct lpc32xx_i2c_base *base, u8 dev, uint addr,
208 int alen, u8 *data, int length)
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200209{
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200210 int stat;
211
212 /* Soft-reset the controller */
Liam Beguin326998e2017-03-27 11:11:36 -0400213 writel(LPC32XX_I2C_SOFT_RESET, &base->ctrl);
214 while (readl(&base->ctrl) & LPC32XX_I2C_SOFT_RESET)
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200215 ;
216 /* do we need to write anything at all? */
217 if (alen | length)
218 /* Address slave in write mode */
Liam Beguin326998e2017-03-27 11:11:36 -0400219 writel((dev<<1) | LPC32XX_I2C_TX_START, &base->tx);
Sylvain Lemieux2e6300f2015-07-27 13:37:39 -0400220 else
221 return 0;
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200222 /* write address bytes */
223 while (alen) {
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 alen--;
228 int a = (addr >> (8 * alen)) & 0xff;
229 if (!(alen | length))
230 a |= LPC32XX_I2C_TX_STOP;
231 /* Send address byte */
Liam Beguin326998e2017-03-27 11:11:36 -0400232 writel(a, &base->tx);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200233 }
234 }
235 while (length) {
236 /* wait for transmit fifo not full */
Liam Beguin326998e2017-03-27 11:11:36 -0400237 stat = readl(&base->stat);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200238 if (!(stat & LPC32XX_I2C_STAT_TFF)) {
239 /* compute data byte, add stop if length==0 */
240 length--;
241 int d = *(data++);
242 if (!length)
243 d |= LPC32XX_I2C_TX_STOP;
244 /* Send data byte */
Liam Beguin326998e2017-03-27 11:11:36 -0400245 writel(d, &base->tx);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200246 }
247 }
248 /* wait for end of transation */
Liam Beguin326998e2017-03-27 11:11:36 -0400249 while (!((stat = readl(&base->stat)) & LPC32XX_I2C_STAT_TDI))
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200250 ;
251 /* clear end-of-transaction flag */
Liam Beguin326998e2017-03-27 11:11:36 -0400252 writel(1, &base->stat);
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200253 return 0;
254}
255
Liam Beguin13232b92017-03-27 11:13:12 -0400256#ifndef CONFIG_DM_I2C
Liam Beguin3fe4a6c2017-03-14 11:24:40 -0400257static void lpc32xx_i2c_init(struct i2c_adapter *adap,
258 int requested_speed, int slaveadd)
259{
Liam Beguin326998e2017-03-27 11:11:36 -0400260 __i2c_init(lpc32xx_i2c[adap->hwadapnr], requested_speed, slaveadd,
261 adap->hwadapnr);
Liam Beguin3fe4a6c2017-03-14 11:24:40 -0400262}
263
264static int lpc32xx_i2c_probe_chip(struct i2c_adapter *adap, u8 dev)
265{
Liam Beguin326998e2017-03-27 11:11:36 -0400266 return __i2c_probe_chip(lpc32xx_i2c[adap->hwadapnr], dev);
Liam Beguin3fe4a6c2017-03-14 11:24:40 -0400267}
268
269static int lpc32xx_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr,
270 int alen, u8 *data, int length)
271{
Liam Beguin326998e2017-03-27 11:11:36 -0400272 return __i2c_read(lpc32xx_i2c[adap->hwadapnr], dev, addr,
273 alen, data, length);
Liam Beguin3fe4a6c2017-03-14 11:24:40 -0400274}
275
276static int lpc32xx_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr,
277 int alen, u8 *data, int length)
278{
Liam Beguin326998e2017-03-27 11:11:36 -0400279 return __i2c_write(lpc32xx_i2c[adap->hwadapnr], dev, addr,
280 alen, data, length);
Liam Beguin3fe4a6c2017-03-14 11:24:40 -0400281}
282
283static unsigned int lpc32xx_i2c_set_bus_speed(struct i2c_adapter *adap,
284 unsigned int speed)
285{
Liam Beguin326998e2017-03-27 11:11:36 -0400286 return __i2c_set_bus_speed(lpc32xx_i2c[adap->hwadapnr], speed,
287 adap->hwadapnr);
Liam Beguin3fe4a6c2017-03-14 11:24:40 -0400288}
289
290U_BOOT_I2C_ADAP_COMPLETE(lpc32xx_0, lpc32xx_i2c_init, lpc32xx_i2c_probe_chip,
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200291 lpc32xx_i2c_read, lpc32xx_i2c_write,
292 lpc32xx_i2c_set_bus_speed,
293 CONFIG_SYS_I2C_LPC32XX_SPEED,
294 CONFIG_SYS_I2C_LPC32XX_SLAVE,
295 0)
296
Liam Beguin3fe4a6c2017-03-14 11:24:40 -0400297U_BOOT_I2C_ADAP_COMPLETE(lpc32xx_1, lpc32xx_i2c_init, lpc32xx_i2c_probe_chip,
Albert ARIBAUD \(3ADEV\)b23324c2015-03-31 11:40:45 +0200298 lpc32xx_i2c_read, lpc32xx_i2c_write,
299 lpc32xx_i2c_set_bus_speed,
300 CONFIG_SYS_I2C_LPC32XX_SPEED,
301 CONFIG_SYS_I2C_LPC32XX_SLAVE,
302 1)
Sylvain Lemieuxfb51abb2015-08-04 17:04:41 -0400303
Liam Beguin3fe4a6c2017-03-14 11:24:40 -0400304U_BOOT_I2C_ADAP_COMPLETE(lpc32xx_2, lpc32xx_i2c_init, NULL,
Sylvain Lemieuxfb51abb2015-08-04 17:04:41 -0400305 lpc32xx_i2c_read, lpc32xx_i2c_write,
306 lpc32xx_i2c_set_bus_speed,
307 100000,
308 0,
309 2)
Liam Beguin13232b92017-03-27 11:13:12 -0400310#else /* CONFIG_DM_I2C */
311static int lpc32xx_i2c_probe(struct udevice *bus)
312{
313 struct lpc32xx_i2c_dev *dev = dev_get_platdata(bus);
314
315 __i2c_init(dev->base, dev->speed, 0, dev->index);
316 return 0;
317}
318
319static int lpc32xx_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
320 u32 chip_flags)
321{
322 struct lpc32xx_i2c_dev *dev = dev_get_platdata(bus);
323 return __i2c_probe_chip(dev->base, chip_addr);
324}
325
326static int lpc32xx_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
327 int nmsgs)
328{
329 struct lpc32xx_i2c_dev *dev = dev_get_platdata(bus);
330 struct i2c_msg *dmsg, *omsg, dummy;
331 uint i = 0, address = 0;
332
333 memset(&dummy, 0, sizeof(struct i2c_msg));
334
335 /* We expect either two messages (one with an offset and one with the
336 * actual data) or one message (just data)
337 */
338 if (nmsgs > 2 || nmsgs == 0) {
339 debug("%s: Only one or two messages are supported.", __func__);
340 return -1;
341 }
342
343 omsg = nmsgs == 1 ? &dummy : msg;
344 dmsg = nmsgs == 1 ? msg : msg + 1;
345
346 /* the address is expected to be a uint, not a array. */
347 address = omsg->buf[0];
348 for (i = 1; i < omsg->len; i++)
349 address = (address << 8) + omsg->buf[i];
350
351 if (dmsg->flags & I2C_M_RD)
352 return __i2c_read(dev->base, dmsg->addr, address,
353 omsg->len, dmsg->buf, dmsg->len);
354 else
355 return __i2c_write(dev->base, dmsg->addr, address,
356 omsg->len, dmsg->buf, dmsg->len);
357}
358
359static int lpc32xx_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
360{
361 struct lpc32xx_i2c_dev *dev = dev_get_platdata(bus);
362 return __i2c_set_bus_speed(dev->base, speed, dev->index);
363}
364
365static int lpc32xx_i2c_reset(struct udevice *bus)
366{
367 struct lpc32xx_i2c_dev *dev = dev_get_platdata(bus);
368
369 __i2c_init(dev->base, dev->speed, 0, dev->index);
370 return 0;
371}
372
373static const struct dm_i2c_ops lpc32xx_i2c_ops = {
374 .xfer = lpc32xx_i2c_xfer,
375 .probe_chip = lpc32xx_i2c_probe_chip,
376 .deblock = lpc32xx_i2c_reset,
377 .set_bus_speed = lpc32xx_i2c_set_bus_speed,
378};
379
380U_BOOT_DRIVER(i2c_lpc32xx) = {
381 .id = UCLASS_I2C,
382 .name = "i2c_lpc32xx",
383 .probe = lpc32xx_i2c_probe,
384 .ops = &lpc32xx_i2c_ops,
385};
386#endif /* CONFIG_DM_I2C */