6b13f685e
김민수
BSP 최초 추가
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
#include <common.h>
#include <linux/types.h>
#include <asm/io.h>
#include <asm/errno.h>
#include <dm/device.h>
#include <dm/root.h>
#include <i2c.h>
#include <fdtdec.h>
DECLARE_GLOBAL_DATA_PTR;
struct uniphier_fi2c_regs {
u32 cr;
#define I2C_CR_MST (1 << 3) /* master mode */
#define I2C_CR_STA (1 << 2) /* start condition */
#define I2C_CR_STO (1 << 1) /* stop condition */
#define I2C_CR_NACK (1 << 0) /* not ACK */
u32 dttx;
#define dtrx dttx /* receive FIFO (read-only) */
#define I2C_DTTX_CMD (1 << 8) /* send command (slave addr) */
#define I2C_DTTX_RD (1 << 0) /* read */
u32 __reserved;
u32 slad;
u32 cyc;
u32 lctl;
u32 ssut;
u32 dsut;
u32 intr;
u32 ie;
u32 ic;
#define I2C_INT_TE (1 << 9) /* TX FIFO empty */
#define I2C_INT_RB (1 << 4) /* received specified bytes */
#define I2C_INT_NA (1 << 2) /* no answer */
#define I2C_INT_AL (1 << 1) /* arbitration lost */
u32 sr;
#define I2C_SR_DB (1 << 12) /* device busy */
#define I2C_SR_BB (1 << 8) /* bus busy */
#define I2C_SR_RFF (1 << 3) /* Rx FIFO full */
#define I2C_SR_RNE (1 << 2) /* Rx FIFO not empty */
#define I2C_SR_TNF (1 << 1) /* Tx FIFO not full */
#define I2C_SR_TFE (1 << 0) /* Tx FIFO empty */
u32 __reserved2;
u32 rst;
#define I2C_RST_TBRST (1 << 2) /* clear Tx FIFO */
#define I2C_RST_RBRST (1 << 1) /* clear Rx FIFO */
#define I2C_RST_RST (1 << 0) /* forcible bus reset */
u32 bm;
u32 noise;
u32 tbc;
u32 rbc;
u32 tbcm;
u32 rbcm;
u32 brst;
#define I2C_BRST_FOEN (1 << 1) /* normal operation */
#define I2C_BRST_RSCLO (1 << 0) /* release SCL low fixing */
};
#define FIOCLK 50000000
struct uniphier_fi2c_dev {
struct uniphier_fi2c_regs __iomem *regs;
unsigned long fioclk;
unsigned long timeout;
};
static int poll_status(u32 __iomem *reg, u32 flag)
{
int wait = 1000000;
while (readl(reg) & flag) {
if (wait-- < 0)
return -EREMOTEIO;
udelay(1);
}
return 0;
}
static int reset_bus(struct uniphier_fi2c_regs __iomem *regs)
{
int ret;
writel(I2C_RST_RST, ®s->rst);
ret = poll_status(®s->rst, I2C_RST_RST);
if (ret < 0)
debug("error: fail to reset I2C controller
");
return ret;
}
static int check_device_busy(struct uniphier_fi2c_regs __iomem *regs)
{
int ret;
ret = poll_status(®s->sr, I2C_SR_DB);
if (ret < 0) {
debug("error: device busy too long. reset...
");
ret = reset_bus(regs);
}
return ret;
}
static int uniphier_fi2c_probe(struct udevice *dev)
{
fdt_addr_t addr;
fdt_size_t size;
struct uniphier_fi2c_dev *priv = dev_get_priv(dev);
int ret;
addr = fdtdec_get_addr_size(gd->fdt_blob, dev->of_offset, "reg",
&size);
priv->regs = map_sysmem(addr, size);
if (!priv->regs)
return -ENOMEM;
priv->fioclk = FIOCLK;
ret = reset_bus(priv->regs);
if (ret < 0)
return ret;
writel(I2C_BRST_FOEN | I2C_BRST_RSCLO, &priv->regs->brst);
return 0;
}
static int uniphier_fi2c_remove(struct udevice *dev)
{
struct uniphier_fi2c_dev *priv = dev_get_priv(dev);
unmap_sysmem(priv->regs);
return 0;
}
static int wait_for_irq(struct uniphier_fi2c_dev *dev, u32 flags,
bool *stop)
{
u32 irq;
unsigned long wait = dev->timeout;
int ret = -EREMOTEIO;
do {
udelay(1);
irq = readl(&dev->regs->intr);
} while (!(irq & flags) && wait--);
if (wait < 0) {
debug("error: time out
");
return ret;
}
if (irq & I2C_INT_AL) {
debug("error: arbitration lost
");
*stop = false;
return ret;
}
if (irq & I2C_INT_NA) {
debug("error: no answer
");
return ret;
}
return 0;
}
static int issue_stop(struct uniphier_fi2c_dev *dev, int old_ret)
{
int ret;
debug("stop condition
");
writel(I2C_CR_MST | I2C_CR_STO, &dev->regs->cr);
ret = poll_status(&dev->regs->sr, I2C_SR_DB);
if (ret < 0)
debug("error: device busy after operation
");
return old_ret ? old_ret : ret;
}
static int uniphier_fi2c_transmit(struct uniphier_fi2c_dev *dev, uint addr,
uint len, const u8 *buf, bool *stop)
{
int ret;
const u32 irq_flags = I2C_INT_TE | I2C_INT_NA | I2C_INT_AL;
struct uniphier_fi2c_regs __iomem *regs = dev->regs;
debug("%s: addr = %x, len = %d
", __func__, addr, len);
writel(I2C_DTTX_CMD | addr << 1, ®s->dttx);
writel(irq_flags, ®s->ie);
writel(irq_flags, ®s->ic);
debug("start condition
");
writel(I2C_CR_MST | I2C_CR_STA, ®s->cr);
ret = wait_for_irq(dev, irq_flags, stop);
if (ret < 0)
goto error;
while (len--) {
debug("sending %x
", *buf);
writel(*buf++, ®s->dttx);
writel(irq_flags, ®s->ic);
ret = wait_for_irq(dev, irq_flags, stop);
if (ret < 0)
goto error;
}
error:
writel(irq_flags, ®s->ic);
if (*stop)
ret = issue_stop(dev, ret);
return ret;
}
static int uniphier_fi2c_receive(struct uniphier_fi2c_dev *dev, uint addr,
uint len, u8 *buf, bool *stop)
{
int ret = 0;
const u32 irq_flags = I2C_INT_RB | I2C_INT_NA | I2C_INT_AL;
struct uniphier_fi2c_regs __iomem *regs = dev->regs;
debug("%s: addr = %x, len = %d
", __func__, addr, len);
if (len == 0)
return uniphier_fi2c_transmit(dev, addr, len, buf, stop);
writel(I2C_DTTX_CMD | I2C_DTTX_RD | addr << 1, ®s->dttx);
writel(0, ®s->rbc);
writel(irq_flags, ®s->ie);
writel(irq_flags, ®s->ic);
debug("start condition
");
writel(I2C_CR_MST | I2C_CR_STA | (len == 1 ? I2C_CR_NACK : 0),
®s->cr);
while (len--) {
ret = wait_for_irq(dev, irq_flags, stop);
if (ret < 0)
goto error;
*buf++ = readl(®s->dtrx);
debug("received %x
", *(buf - 1));
if (len == 1)
writel(I2C_CR_MST | I2C_CR_NACK, ®s->cr);
writel(irq_flags, ®s->ic);
}
error:
writel(irq_flags, ®s->ic);
if (*stop)
ret = issue_stop(dev, ret);
return ret;
}
static int uniphier_fi2c_xfer(struct udevice *bus, struct i2c_msg *msg,
int nmsgs)
{
int ret;
struct uniphier_fi2c_dev *dev = dev_get_priv(bus);
bool stop;
ret = check_device_busy(dev->regs);
if (ret < 0)
return ret;
for (; nmsgs > 0; nmsgs--, msg++) {
stop = nmsgs > 1 && msg[1].flags & I2C_M_RD ? false : true;
if (msg->flags & I2C_M_RD)
ret = uniphier_fi2c_receive(dev, msg->addr, msg->len,
msg->buf, &stop);
else
ret = uniphier_fi2c_transmit(dev, msg->addr, msg->len,
msg->buf, &stop);
if (ret < 0)
break;
}
return ret;
}
static int uniphier_fi2c_set_bus_speed(struct udevice *bus, unsigned int speed)
{
int ret;
unsigned int clk_count;
struct uniphier_fi2c_dev *dev = dev_get_priv(bus);
struct uniphier_fi2c_regs __iomem *regs = dev->regs;
if (speed > 400000)
return -EINVAL;
ret = check_device_busy(dev->regs);
if (ret < 0)
return ret;
writel(I2C_BRST_RSCLO, ®s->brst);
clk_count = dev->fioclk / speed;
writel(clk_count, ®s->cyc);
writel(clk_count / 2, ®s->lctl);
writel(clk_count / 2, ®s->ssut);
writel(clk_count / 16, ®s->dsut);
writel(I2C_BRST_FOEN | I2C_BRST_RSCLO, ®s->brst);
dev->timeout = 100000000L / speed;
return 0;
}
static const struct dm_i2c_ops uniphier_fi2c_ops = {
.xfer = uniphier_fi2c_xfer,
.set_bus_speed = uniphier_fi2c_set_bus_speed,
};
static const struct udevice_id uniphier_fi2c_of_match[] = {
{ .compatible = "socionext,uniphier-fi2c" },
{ }
};
U_BOOT_DRIVER(uniphier_fi2c) = {
.name = "uniphier-fi2c",
.id = UCLASS_I2C,
.of_match = uniphier_fi2c_of_match,
.probe = uniphier_fi2c_probe,
.remove = uniphier_fi2c_remove,
.priv_auto_alloc_size = sizeof(struct uniphier_fi2c_dev),
.ops = &uniphier_fi2c_ops,
};
|