1
2
3
4
5
6#include <linux/clk.h>
7#include <linux/i2c.h>
8#include <linux/iopoll.h>
9#include <linux/interrupt.h>
10#include <linux/io.h>
11#include <linux/module.h>
12#include <linux/platform_device.h>
13
14#define UNIPHIER_FI2C_CR 0x00
15#define UNIPHIER_FI2C_CR_MST BIT(3)
16#define UNIPHIER_FI2C_CR_STA BIT(2)
17#define UNIPHIER_FI2C_CR_STO BIT(1)
18#define UNIPHIER_FI2C_CR_NACK BIT(0)
19#define UNIPHIER_FI2C_DTTX 0x04
20#define UNIPHIER_FI2C_DTTX_CMD BIT(8)
21#define UNIPHIER_FI2C_DTTX_RD BIT(0)
22#define UNIPHIER_FI2C_DTRX 0x04
23#define UNIPHIER_FI2C_SLAD 0x0c
24#define UNIPHIER_FI2C_CYC 0x10
25#define UNIPHIER_FI2C_LCTL 0x14
26#define UNIPHIER_FI2C_SSUT 0x18
27#define UNIPHIER_FI2C_DSUT 0x1c
28#define UNIPHIER_FI2C_INT 0x20
29#define UNIPHIER_FI2C_IE 0x24
30#define UNIPHIER_FI2C_IC 0x28
31#define UNIPHIER_FI2C_INT_TE BIT(9)
32#define UNIPHIER_FI2C_INT_RF BIT(8)
33#define UNIPHIER_FI2C_INT_TC BIT(7)
34#define UNIPHIER_FI2C_INT_RC BIT(6)
35#define UNIPHIER_FI2C_INT_TB BIT(5)
36#define UNIPHIER_FI2C_INT_RB BIT(4)
37#define UNIPHIER_FI2C_INT_NA BIT(2)
38#define UNIPHIER_FI2C_INT_AL BIT(1)
39#define UNIPHIER_FI2C_SR 0x2c
40#define UNIPHIER_FI2C_SR_DB BIT(12)
41#define UNIPHIER_FI2C_SR_STS BIT(11)
42#define UNIPHIER_FI2C_SR_BB BIT(8)
43#define UNIPHIER_FI2C_SR_RFF BIT(3)
44#define UNIPHIER_FI2C_SR_RNE BIT(2)
45#define UNIPHIER_FI2C_SR_TNF BIT(1)
46#define UNIPHIER_FI2C_SR_TFE BIT(0)
47#define UNIPHIER_FI2C_RST 0x34
48#define UNIPHIER_FI2C_RST_TBRST BIT(2)
49#define UNIPHIER_FI2C_RST_RBRST BIT(1)
50#define UNIPHIER_FI2C_RST_RST BIT(0)
51#define UNIPHIER_FI2C_BM 0x38
52#define UNIPHIER_FI2C_BM_SDAO BIT(3)
53#define UNIPHIER_FI2C_BM_SDAS BIT(2)
54#define UNIPHIER_FI2C_BM_SCLO BIT(1)
55#define UNIPHIER_FI2C_BM_SCLS BIT(0)
56#define UNIPHIER_FI2C_NOISE 0x3c
57#define UNIPHIER_FI2C_TBC 0x40
58#define UNIPHIER_FI2C_RBC 0x44
59#define UNIPHIER_FI2C_TBCM 0x48
60#define UNIPHIER_FI2C_RBCM 0x4c
61#define UNIPHIER_FI2C_BRST 0x50
62#define UNIPHIER_FI2C_BRST_FOEN BIT(1)
63#define UNIPHIER_FI2C_BRST_RSCL BIT(0)
64
65#define UNIPHIER_FI2C_INT_FAULTS \
66 (UNIPHIER_FI2C_INT_NA | UNIPHIER_FI2C_INT_AL)
67#define UNIPHIER_FI2C_INT_STOP \
68 (UNIPHIER_FI2C_INT_TC | UNIPHIER_FI2C_INT_RC)
69
70#define UNIPHIER_FI2C_RD BIT(0)
71#define UNIPHIER_FI2C_STOP BIT(1)
72#define UNIPHIER_FI2C_MANUAL_NACK BIT(2)
73#define UNIPHIER_FI2C_BYTE_WISE BIT(3)
74#define UNIPHIER_FI2C_DEFER_STOP_COMP BIT(4)
75
76#define UNIPHIER_FI2C_FIFO_SIZE 8
77
78struct uniphier_fi2c_priv {
79 struct completion comp;
80 struct i2c_adapter adap;
81 void __iomem *membase;
82 struct clk *clk;
83 unsigned int len;
84 u8 *buf;
85 u32 enabled_irqs;
86 int error;
87 unsigned int flags;
88 unsigned int busy_cnt;
89 unsigned int clk_cycle;
90 spinlock_t lock;
91};
92
93static void uniphier_fi2c_fill_txfifo(struct uniphier_fi2c_priv *priv,
94 bool first)
95{
96 int fifo_space = UNIPHIER_FI2C_FIFO_SIZE;
97
98
99
100
101
102 if (first)
103 fifo_space--;
104
105 while (priv->len) {
106 if (fifo_space-- <= 0)
107 break;
108
109 writel(*priv->buf++, priv->membase + UNIPHIER_FI2C_DTTX);
110 priv->len--;
111 }
112}
113
114static void uniphier_fi2c_drain_rxfifo(struct uniphier_fi2c_priv *priv)
115{
116 int fifo_left = priv->flags & UNIPHIER_FI2C_BYTE_WISE ?
117 1 : UNIPHIER_FI2C_FIFO_SIZE;
118
119 while (priv->len) {
120 if (fifo_left-- <= 0)
121 break;
122
123 *priv->buf++ = readl(priv->membase + UNIPHIER_FI2C_DTRX);
124 priv->len--;
125 }
126}
127
128static void uniphier_fi2c_set_irqs(struct uniphier_fi2c_priv *priv)
129{
130 writel(priv->enabled_irqs, priv->membase + UNIPHIER_FI2C_IE);
131}
132
133static void uniphier_fi2c_clear_irqs(struct uniphier_fi2c_priv *priv,
134 u32 mask)
135{
136 writel(mask, priv->membase + UNIPHIER_FI2C_IC);
137}
138
139static void uniphier_fi2c_stop(struct uniphier_fi2c_priv *priv)
140{
141 priv->enabled_irqs |= UNIPHIER_FI2C_INT_STOP;
142 uniphier_fi2c_set_irqs(priv);
143 writel(UNIPHIER_FI2C_CR_MST | UNIPHIER_FI2C_CR_STO,
144 priv->membase + UNIPHIER_FI2C_CR);
145}
146
147static irqreturn_t uniphier_fi2c_interrupt(int irq, void *dev_id)
148{
149 struct uniphier_fi2c_priv *priv = dev_id;
150 u32 irq_status;
151
152 spin_lock(&priv->lock);
153
154 irq_status = readl(priv->membase + UNIPHIER_FI2C_INT);
155 irq_status &= priv->enabled_irqs;
156
157 if (irq_status & UNIPHIER_FI2C_INT_STOP)
158 goto complete;
159
160 if (unlikely(irq_status & UNIPHIER_FI2C_INT_AL)) {
161 priv->error = -EAGAIN;
162 goto complete;
163 }
164
165 if (unlikely(irq_status & UNIPHIER_FI2C_INT_NA)) {
166 priv->error = -ENXIO;
167 if (priv->flags & UNIPHIER_FI2C_RD) {
168
169
170
171
172
173
174
175
176
177 uniphier_fi2c_stop(priv);
178 priv->flags |= UNIPHIER_FI2C_DEFER_STOP_COMP;
179 goto complete;
180 }
181 goto stop;
182 }
183
184 if (irq_status & UNIPHIER_FI2C_INT_TE) {
185 if (!priv->len)
186 goto data_done;
187
188 uniphier_fi2c_fill_txfifo(priv, false);
189 goto handled;
190 }
191
192 if (irq_status & (UNIPHIER_FI2C_INT_RF | UNIPHIER_FI2C_INT_RB)) {
193 uniphier_fi2c_drain_rxfifo(priv);
194
195
196
197
198
199
200 if (!priv->len && (irq_status & UNIPHIER_FI2C_INT_RB))
201 goto data_done;
202
203 if (unlikely(priv->flags & UNIPHIER_FI2C_MANUAL_NACK)) {
204 if (priv->len <= UNIPHIER_FI2C_FIFO_SIZE &&
205 !(priv->flags & UNIPHIER_FI2C_BYTE_WISE)) {
206 priv->enabled_irqs |= UNIPHIER_FI2C_INT_RB;
207 uniphier_fi2c_set_irqs(priv);
208 priv->flags |= UNIPHIER_FI2C_BYTE_WISE;
209 }
210 if (priv->len <= 1)
211 writel(UNIPHIER_FI2C_CR_MST |
212 UNIPHIER_FI2C_CR_NACK,
213 priv->membase + UNIPHIER_FI2C_CR);
214 }
215
216 goto handled;
217 }
218
219 spin_unlock(&priv->lock);
220
221 return IRQ_NONE;
222
223data_done:
224 if (priv->flags & UNIPHIER_FI2C_STOP) {
225stop:
226 uniphier_fi2c_stop(priv);
227 } else {
228complete:
229 priv->enabled_irqs = 0;
230 uniphier_fi2c_set_irqs(priv);
231 complete(&priv->comp);
232 }
233
234handled:
235
236
237
238
239
240 uniphier_fi2c_clear_irqs(priv, irq_status);
241
242 spin_unlock(&priv->lock);
243
244 return IRQ_HANDLED;
245}
246
247static void uniphier_fi2c_tx_init(struct uniphier_fi2c_priv *priv, u16 addr,
248 bool repeat)
249{
250 priv->enabled_irqs |= UNIPHIER_FI2C_INT_TE;
251 uniphier_fi2c_set_irqs(priv);
252
253
254 writel(0, priv->membase + UNIPHIER_FI2C_TBC);
255
256 writel(UNIPHIER_FI2C_DTTX_CMD | addr << 1,
257 priv->membase + UNIPHIER_FI2C_DTTX);
258
259
260
261
262 if (!repeat)
263 uniphier_fi2c_fill_txfifo(priv, true);
264}
265
266static void uniphier_fi2c_rx_init(struct uniphier_fi2c_priv *priv, u16 addr)
267{
268 priv->flags |= UNIPHIER_FI2C_RD;
269
270 if (likely(priv->len < 256)) {
271
272
273
274
275 writel(priv->len, priv->membase + UNIPHIER_FI2C_RBC);
276 priv->enabled_irqs |= UNIPHIER_FI2C_INT_RF |
277 UNIPHIER_FI2C_INT_RB;
278 } else {
279
280
281
282
283
284 writel(0, priv->membase + UNIPHIER_FI2C_RBC);
285 priv->flags |= UNIPHIER_FI2C_MANUAL_NACK;
286 priv->enabled_irqs |= UNIPHIER_FI2C_INT_RF;
287 }
288
289 uniphier_fi2c_set_irqs(priv);
290
291
292 writel(UNIPHIER_FI2C_DTTX_CMD | UNIPHIER_FI2C_DTTX_RD | addr << 1,
293 priv->membase + UNIPHIER_FI2C_DTTX);
294}
295
296static void uniphier_fi2c_reset(struct uniphier_fi2c_priv *priv)
297{
298 writel(UNIPHIER_FI2C_RST_RST, priv->membase + UNIPHIER_FI2C_RST);
299}
300
301static void uniphier_fi2c_prepare_operation(struct uniphier_fi2c_priv *priv)
302{
303 writel(UNIPHIER_FI2C_BRST_FOEN | UNIPHIER_FI2C_BRST_RSCL,
304 priv->membase + UNIPHIER_FI2C_BRST);
305}
306
307static void uniphier_fi2c_recover(struct uniphier_fi2c_priv *priv)
308{
309 uniphier_fi2c_reset(priv);
310 i2c_recover_bus(&priv->adap);
311}
312
313static int uniphier_fi2c_master_xfer_one(struct i2c_adapter *adap,
314 struct i2c_msg *msg, bool repeat,
315 bool stop)
316{
317 struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
318 bool is_read = msg->flags & I2C_M_RD;
319 unsigned long time_left, flags;
320
321 priv->len = msg->len;
322 priv->buf = msg->buf;
323 priv->enabled_irqs = UNIPHIER_FI2C_INT_FAULTS;
324 priv->error = 0;
325 priv->flags = 0;
326
327 if (stop)
328 priv->flags |= UNIPHIER_FI2C_STOP;
329
330 reinit_completion(&priv->comp);
331 uniphier_fi2c_clear_irqs(priv, U32_MAX);
332 writel(UNIPHIER_FI2C_RST_TBRST | UNIPHIER_FI2C_RST_RBRST,
333 priv->membase + UNIPHIER_FI2C_RST);
334
335 spin_lock_irqsave(&priv->lock, flags);
336
337 if (is_read)
338 uniphier_fi2c_rx_init(priv, msg->addr);
339 else
340 uniphier_fi2c_tx_init(priv, msg->addr, repeat);
341
342
343
344
345
346
347 if (!repeat)
348 writel(UNIPHIER_FI2C_CR_MST | UNIPHIER_FI2C_CR_STA,
349 priv->membase + UNIPHIER_FI2C_CR);
350
351 spin_unlock_irqrestore(&priv->lock, flags);
352
353 time_left = wait_for_completion_timeout(&priv->comp, adap->timeout);
354
355 spin_lock_irqsave(&priv->lock, flags);
356 priv->enabled_irqs = 0;
357 uniphier_fi2c_set_irqs(priv);
358 spin_unlock_irqrestore(&priv->lock, flags);
359
360 if (!time_left) {
361 dev_err(&adap->dev, "transaction timeout.\n");
362 uniphier_fi2c_recover(priv);
363 return -ETIMEDOUT;
364 }
365
366 if (unlikely(priv->flags & UNIPHIER_FI2C_DEFER_STOP_COMP)) {
367 u32 status;
368 int ret;
369
370 ret = readl_poll_timeout(priv->membase + UNIPHIER_FI2C_SR,
371 status,
372 (status & UNIPHIER_FI2C_SR_STS) &&
373 !(status & UNIPHIER_FI2C_SR_BB),
374 1, 20);
375 if (ret) {
376 dev_err(&adap->dev,
377 "stop condition was not completed.\n");
378 uniphier_fi2c_recover(priv);
379 return ret;
380 }
381 }
382
383 return priv->error;
384}
385
386static int uniphier_fi2c_check_bus_busy(struct i2c_adapter *adap)
387{
388 struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
389
390 if (readl(priv->membase + UNIPHIER_FI2C_SR) & UNIPHIER_FI2C_SR_DB) {
391 if (priv->busy_cnt++ > 3) {
392
393
394
395
396 uniphier_fi2c_recover(priv);
397 priv->busy_cnt = 0;
398 }
399
400 return -EAGAIN;
401 }
402
403 priv->busy_cnt = 0;
404 return 0;
405}
406
407static int uniphier_fi2c_master_xfer(struct i2c_adapter *adap,
408 struct i2c_msg *msgs, int num)
409{
410 struct i2c_msg *msg, *emsg = msgs + num;
411 bool repeat = false;
412 int ret;
413
414 ret = uniphier_fi2c_check_bus_busy(adap);
415 if (ret)
416 return ret;
417
418 for (msg = msgs; msg < emsg; msg++) {
419
420 bool stop = (msg + 1 == emsg) || (msg->flags & I2C_M_STOP);
421
422 ret = uniphier_fi2c_master_xfer_one(adap, msg, repeat, stop);
423 if (ret)
424 return ret;
425
426 repeat = !stop;
427 }
428
429 return num;
430}
431
432static u32 uniphier_fi2c_functionality(struct i2c_adapter *adap)
433{
434 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
435}
436
437static const struct i2c_algorithm uniphier_fi2c_algo = {
438 .master_xfer = uniphier_fi2c_master_xfer,
439 .functionality = uniphier_fi2c_functionality,
440};
441
442static int uniphier_fi2c_get_scl(struct i2c_adapter *adap)
443{
444 struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
445
446 return !!(readl(priv->membase + UNIPHIER_FI2C_BM) &
447 UNIPHIER_FI2C_BM_SCLS);
448}
449
450static void uniphier_fi2c_set_scl(struct i2c_adapter *adap, int val)
451{
452 struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
453
454 writel(val ? UNIPHIER_FI2C_BRST_RSCL : 0,
455 priv->membase + UNIPHIER_FI2C_BRST);
456}
457
458static int uniphier_fi2c_get_sda(struct i2c_adapter *adap)
459{
460 struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
461
462 return !!(readl(priv->membase + UNIPHIER_FI2C_BM) &
463 UNIPHIER_FI2C_BM_SDAS);
464}
465
466static void uniphier_fi2c_unprepare_recovery(struct i2c_adapter *adap)
467{
468 uniphier_fi2c_prepare_operation(i2c_get_adapdata(adap));
469}
470
471static struct i2c_bus_recovery_info uniphier_fi2c_bus_recovery_info = {
472 .recover_bus = i2c_generic_scl_recovery,
473 .get_scl = uniphier_fi2c_get_scl,
474 .set_scl = uniphier_fi2c_set_scl,
475 .get_sda = uniphier_fi2c_get_sda,
476 .unprepare_recovery = uniphier_fi2c_unprepare_recovery,
477};
478
479static void uniphier_fi2c_hw_init(struct uniphier_fi2c_priv *priv)
480{
481 unsigned int cyc = priv->clk_cycle;
482 u32 tmp;
483
484 tmp = readl(priv->membase + UNIPHIER_FI2C_CR);
485 tmp |= UNIPHIER_FI2C_CR_MST;
486 writel(tmp, priv->membase + UNIPHIER_FI2C_CR);
487
488 uniphier_fi2c_reset(priv);
489
490
491
492
493
494 writel(cyc, priv->membase + UNIPHIER_FI2C_CYC);
495
496
497
498
499
500 writel(cyc * 5 / 9, priv->membase + UNIPHIER_FI2C_LCTL);
501
502
503
504
505 writel(cyc / 2, priv->membase + UNIPHIER_FI2C_SSUT);
506
507
508
509
510 writel(cyc / 16, priv->membase + UNIPHIER_FI2C_DSUT);
511
512 uniphier_fi2c_prepare_operation(priv);
513}
514
515static int uniphier_fi2c_probe(struct platform_device *pdev)
516{
517 struct device *dev = &pdev->dev;
518 struct uniphier_fi2c_priv *priv;
519 u32 bus_speed;
520 unsigned long clk_rate;
521 int irq, ret;
522
523 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
524 if (!priv)
525 return -ENOMEM;
526
527 priv->membase = devm_platform_ioremap_resource(pdev, 0);
528 if (IS_ERR(priv->membase))
529 return PTR_ERR(priv->membase);
530
531 irq = platform_get_irq(pdev, 0);
532 if (irq < 0)
533 return irq;
534
535 if (of_property_read_u32(dev->of_node, "clock-frequency", &bus_speed))
536 bus_speed = I2C_MAX_STANDARD_MODE_FREQ;
537
538 if (!bus_speed || bus_speed > I2C_MAX_FAST_MODE_FREQ) {
539 dev_err(dev, "invalid clock-frequency %d\n", bus_speed);
540 return -EINVAL;
541 }
542
543 priv->clk = devm_clk_get(dev, NULL);
544 if (IS_ERR(priv->clk)) {
545 dev_err(dev, "failed to get clock\n");
546 return PTR_ERR(priv->clk);
547 }
548
549 ret = clk_prepare_enable(priv->clk);
550 if (ret)
551 return ret;
552
553 clk_rate = clk_get_rate(priv->clk);
554 if (!clk_rate) {
555 dev_err(dev, "input clock rate should not be zero\n");
556 ret = -EINVAL;
557 goto disable_clk;
558 }
559
560 priv->clk_cycle = clk_rate / bus_speed;
561 init_completion(&priv->comp);
562 spin_lock_init(&priv->lock);
563 priv->adap.owner = THIS_MODULE;
564 priv->adap.algo = &uniphier_fi2c_algo;
565 priv->adap.dev.parent = dev;
566 priv->adap.dev.of_node = dev->of_node;
567 strlcpy(priv->adap.name, "UniPhier FI2C", sizeof(priv->adap.name));
568 priv->adap.bus_recovery_info = &uniphier_fi2c_bus_recovery_info;
569 i2c_set_adapdata(&priv->adap, priv);
570 platform_set_drvdata(pdev, priv);
571
572 uniphier_fi2c_hw_init(priv);
573
574 ret = devm_request_irq(dev, irq, uniphier_fi2c_interrupt, 0,
575 pdev->name, priv);
576 if (ret) {
577 dev_err(dev, "failed to request irq %d\n", irq);
578 goto disable_clk;
579 }
580
581 ret = i2c_add_adapter(&priv->adap);
582disable_clk:
583 if (ret)
584 clk_disable_unprepare(priv->clk);
585
586 return ret;
587}
588
589static int uniphier_fi2c_remove(struct platform_device *pdev)
590{
591 struct uniphier_fi2c_priv *priv = platform_get_drvdata(pdev);
592
593 i2c_del_adapter(&priv->adap);
594 clk_disable_unprepare(priv->clk);
595
596 return 0;
597}
598
599static int __maybe_unused uniphier_fi2c_suspend(struct device *dev)
600{
601 struct uniphier_fi2c_priv *priv = dev_get_drvdata(dev);
602
603 clk_disable_unprepare(priv->clk);
604
605 return 0;
606}
607
608static int __maybe_unused uniphier_fi2c_resume(struct device *dev)
609{
610 struct uniphier_fi2c_priv *priv = dev_get_drvdata(dev);
611 int ret;
612
613 ret = clk_prepare_enable(priv->clk);
614 if (ret)
615 return ret;
616
617 uniphier_fi2c_hw_init(priv);
618
619 return 0;
620}
621
622static const struct dev_pm_ops uniphier_fi2c_pm_ops = {
623 SET_SYSTEM_SLEEP_PM_OPS(uniphier_fi2c_suspend, uniphier_fi2c_resume)
624};
625
626static const struct of_device_id uniphier_fi2c_match[] = {
627 { .compatible = "socionext,uniphier-fi2c" },
628 { }
629};
630MODULE_DEVICE_TABLE(of, uniphier_fi2c_match);
631
632static struct platform_driver uniphier_fi2c_drv = {
633 .probe = uniphier_fi2c_probe,
634 .remove = uniphier_fi2c_remove,
635 .driver = {
636 .name = "uniphier-fi2c",
637 .of_match_table = uniphier_fi2c_match,
638 .pm = &uniphier_fi2c_pm_ops,
639 },
640};
641module_platform_driver(uniphier_fi2c_drv);
642
643MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
644MODULE_DESCRIPTION("UniPhier FIFO-builtin I2C bus driver");
645MODULE_LICENSE("GPL");
646