1
2
3
4
5
6
7
8
9
10
11
12
13#include <common.h>
14#include <asm/io.h>
15#include <i2c.h>
16#include <linux/errno.h>
17#include <asm/arch/clk.h>
18
19
20
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
32struct lpc32xx_i2c_registers {
33 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
51#define LPC32XX_I2C_TX_START 0x00000100
52#define LPC32XX_I2C_TX_STOP 0x00000200
53
54
55#define LPC32XX_I2C_SOFT_RESET 0x00000100
56
57
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
64static struct lpc32xx_i2c_registers *lpc32xx_i2c[] = {
65 (struct lpc32xx_i2c_registers *)I2C1_BASE,
66 (struct lpc32xx_i2c_registers *)I2C2_BASE,
67 (struct lpc32xx_i2c_registers *)(USB_BASE + 0x300)
68};
69
70
71static unsigned int lpc32xx_i2c_set_bus_speed(struct i2c_adapter *adap,
72 unsigned int speed)
73{
74 int half_period;
75
76 if (speed == 0)
77 return -EINVAL;
78
79
80 if (adap->hwadapnr == 2) {
81 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 }
89
90 writel(half_period, &lpc32xx_i2c[adap->hwadapnr]->clk_hi);
91 writel(half_period, &lpc32xx_i2c[adap->hwadapnr]->clk_lo);
92 return 0;
93}
94
95
96static void _i2c_init(struct i2c_adapter *adap,
97 int requested_speed, int slaveadd)
98{
99 struct lpc32xx_i2c_registers *i2c = lpc32xx_i2c[adap->hwadapnr];
100
101
102 writel(LPC32XX_I2C_SOFT_RESET, &i2c->ctrl);
103
104 lpc32xx_i2c_set_bus_speed(adap, requested_speed);
105}
106
107
108static int lpc32xx_i2c_probe(struct i2c_adapter *adap, u8 dev)
109{
110 struct lpc32xx_i2c_registers *i2c = lpc32xx_i2c[adap->hwadapnr];
111 int stat;
112
113
114 writel(LPC32XX_I2C_SOFT_RESET, &i2c->ctrl);
115 while (readl(&i2c->ctrl) & LPC32XX_I2C_SOFT_RESET)
116 ;
117
118 writel((dev<<1) | LPC32XX_I2C_TX_START | LPC32XX_I2C_TX_STOP,
119 &i2c->tx);
120
121 while (!((stat = readl(&i2c->stat)) & LPC32XX_I2C_STAT_TDI))
122 ;
123
124 return (stat & LPC32XX_I2C_STAT_NAI) ? -1 : 0;
125}
126
127
128
129
130
131static int lpc32xx_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr,
132 int alen, u8 *data, int length)
133{
134 struct lpc32xx_i2c_registers *i2c = lpc32xx_i2c[adap->hwadapnr];
135 int stat, wlen;
136
137
138 writel(LPC32XX_I2C_SOFT_RESET, &i2c->ctrl);
139 while (readl(&i2c->ctrl) & LPC32XX_I2C_SOFT_RESET)
140 ;
141
142 if (alen) {
143
144 writel((dev<<1) | LPC32XX_I2C_TX_START, &i2c->tx);
145
146 while (alen--) {
147
148 int a = (addr >> (8 * alen)) & 0xff;
149 if (!alen)
150 a |= LPC32XX_I2C_TX_STOP;
151
152 writel(a, &i2c->tx);
153 }
154
155 while (!((stat = readl(&i2c->stat)) & LPC32XX_I2C_STAT_TDI))
156 ;
157
158 writel(1, &i2c->stat);
159 }
160
161 if (length) {
162
163 writel(1 | (dev<<1) | LPC32XX_I2C_TX_START, &i2c->tx);
164 wlen = length;
165
166 while (length | wlen) {
167
168 stat = readl(&i2c->stat);
169
170 if ((wlen > 0)
171 & (!(stat & LPC32XX_I2C_STAT_TFF))) {
172 wlen--;
173
174 writel(wlen ? 0 :
175 LPC32XX_I2C_TX_STOP, &i2c->tx);
176 }
177
178 if ((length > 0)
179 & (!(stat & LPC32XX_I2C_STAT_RFE))) {
180 length--;
181
182 *(data++) = readl(&i2c->rx);
183 }
184 }
185
186 while (!((stat = readl(&i2c->stat)) & LPC32XX_I2C_STAT_TDI))
187 ;
188
189 writel(1, &i2c->stat);
190 }
191
192 return 0;
193}
194
195
196
197
198
199static int lpc32xx_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr,
200 int alen, u8 *data, int length)
201{
202 struct lpc32xx_i2c_registers *i2c = lpc32xx_i2c[adap->hwadapnr];
203 int stat;
204
205
206 writel(LPC32XX_I2C_SOFT_RESET, &i2c->ctrl);
207 while (readl(&i2c->ctrl) & LPC32XX_I2C_SOFT_RESET)
208 ;
209
210 if (alen | length)
211
212 writel((dev<<1) | LPC32XX_I2C_TX_START, &i2c->tx);
213 else
214 return 0;
215
216 while (alen) {
217
218 stat = readl(&i2c->stat);
219 if (!(stat & LPC32XX_I2C_STAT_TFF)) {
220 alen--;
221 int a = (addr >> (8 * alen)) & 0xff;
222 if (!(alen | length))
223 a |= LPC32XX_I2C_TX_STOP;
224
225 writel(a, &i2c->tx);
226 }
227 }
228 while (length) {
229
230 stat = readl(&i2c->stat);
231 if (!(stat & LPC32XX_I2C_STAT_TFF)) {
232
233 length--;
234 int d = *(data++);
235 if (!length)
236 d |= LPC32XX_I2C_TX_STOP;
237
238 writel(d, &i2c->tx);
239 }
240 }
241
242 while (!((stat = readl(&i2c->stat)) & LPC32XX_I2C_STAT_TDI))
243 ;
244
245 writel(1, &i2c->stat);
246 return 0;
247}
248
249U_BOOT_I2C_ADAP_COMPLETE(lpc32xx_0, _i2c_init, lpc32xx_i2c_probe,
250 lpc32xx_i2c_read, lpc32xx_i2c_write,
251 lpc32xx_i2c_set_bus_speed,
252 CONFIG_SYS_I2C_LPC32XX_SPEED,
253 CONFIG_SYS_I2C_LPC32XX_SLAVE,
254 0)
255
256U_BOOT_I2C_ADAP_COMPLETE(lpc32xx_1, _i2c_init, lpc32xx_i2c_probe,
257 lpc32xx_i2c_read, lpc32xx_i2c_write,
258 lpc32xx_i2c_set_bus_speed,
259 CONFIG_SYS_I2C_LPC32XX_SPEED,
260 CONFIG_SYS_I2C_LPC32XX_SLAVE,
261 1)
262
263U_BOOT_I2C_ADAP_COMPLETE(lpc32xx_2, _i2c_init, NULL,
264 lpc32xx_i2c_read, lpc32xx_i2c_write,
265 lpc32xx_i2c_set_bus_speed,
266 100000,
267 0,
268 2)
269