2 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/clk.h>
16 #include <linux/i2c.h>
17 #include <linux/iopoll.h>
18 #include <linux/interrupt.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
23 #define UNIPHIER_FI2C_CR 0x00 /* control register */
24 #define UNIPHIER_FI2C_CR_MST BIT(3) /* master mode */
25 #define UNIPHIER_FI2C_CR_STA BIT(2) /* start condition */
26 #define UNIPHIER_FI2C_CR_STO BIT(1) /* stop condition */
27 #define UNIPHIER_FI2C_CR_NACK BIT(0) /* do not return ACK */
28 #define UNIPHIER_FI2C_DTTX 0x04 /* TX FIFO */
29 #define UNIPHIER_FI2C_DTTX_CMD BIT(8) /* send command (slave addr) */
30 #define UNIPHIER_FI2C_DTTX_RD BIT(0) /* read transaction */
31 #define UNIPHIER_FI2C_DTRX 0x04 /* RX FIFO */
32 #define UNIPHIER_FI2C_SLAD 0x0c /* slave address */
33 #define UNIPHIER_FI2C_CYC 0x10 /* clock cycle control */
34 #define UNIPHIER_FI2C_LCTL 0x14 /* clock low period control */
35 #define UNIPHIER_FI2C_SSUT 0x18 /* restart/stop setup time control */
36 #define UNIPHIER_FI2C_DSUT 0x1c /* data setup time control */
37 #define UNIPHIER_FI2C_INT 0x20 /* interrupt status */
38 #define UNIPHIER_FI2C_IE 0x24 /* interrupt enable */
39 #define UNIPHIER_FI2C_IC 0x28 /* interrupt clear */
40 #define UNIPHIER_FI2C_INT_TE BIT(9) /* TX FIFO empty */
41 #define UNIPHIER_FI2C_INT_RF BIT(8) /* RX FIFO full */
42 #define UNIPHIER_FI2C_INT_TC BIT(7) /* send complete (STOP) */
43 #define UNIPHIER_FI2C_INT_RC BIT(6) /* receive complete (STOP) */
44 #define UNIPHIER_FI2C_INT_TB BIT(5) /* sent specified bytes */
45 #define UNIPHIER_FI2C_INT_RB BIT(4) /* received specified bytes */
46 #define UNIPHIER_FI2C_INT_NA BIT(2) /* no ACK */
47 #define UNIPHIER_FI2C_INT_AL BIT(1) /* arbitration lost */
48 #define UNIPHIER_FI2C_SR 0x2c /* status register */
49 #define UNIPHIER_FI2C_SR_DB BIT(12) /* device busy */
50 #define UNIPHIER_FI2C_SR_STS BIT(11) /* stop condition detected */
51 #define UNIPHIER_FI2C_SR_BB BIT(8) /* bus busy */
52 #define UNIPHIER_FI2C_SR_RFF BIT(3) /* RX FIFO full */
53 #define UNIPHIER_FI2C_SR_RNE BIT(2) /* RX FIFO not empty */
54 #define UNIPHIER_FI2C_SR_TNF BIT(1) /* TX FIFO not full */
55 #define UNIPHIER_FI2C_SR_TFE BIT(0) /* TX FIFO empty */
56 #define UNIPHIER_FI2C_RST 0x34 /* reset control */
57 #define UNIPHIER_FI2C_RST_TBRST BIT(2) /* clear TX FIFO */
58 #define UNIPHIER_FI2C_RST_RBRST BIT(1) /* clear RX FIFO */
59 #define UNIPHIER_FI2C_RST_RST BIT(0) /* forcible bus reset */
60 #define UNIPHIER_FI2C_BM 0x38 /* bus monitor */
61 #define UNIPHIER_FI2C_BM_SDAO BIT(3) /* output for SDA line */
62 #define UNIPHIER_FI2C_BM_SDAS BIT(2) /* readback of SDA line */
63 #define UNIPHIER_FI2C_BM_SCLO BIT(1) /* output for SCL line */
64 #define UNIPHIER_FI2C_BM_SCLS BIT(0) /* readback of SCL line */
65 #define UNIPHIER_FI2C_NOISE 0x3c /* noise filter control */
66 #define UNIPHIER_FI2C_TBC 0x40 /* TX byte count setting */
67 #define UNIPHIER_FI2C_RBC 0x44 /* RX byte count setting */
68 #define UNIPHIER_FI2C_TBCM 0x48 /* TX byte count monitor */
69 #define UNIPHIER_FI2C_RBCM 0x4c /* RX byte count monitor */
70 #define UNIPHIER_FI2C_BRST 0x50 /* bus reset */
71 #define UNIPHIER_FI2C_BRST_FOEN BIT(1) /* normal operation */
72 #define UNIPHIER_FI2C_BRST_RSCL BIT(0) /* release SCL */
74 #define UNIPHIER_FI2C_INT_FAULTS \
75 (UNIPHIER_FI2C_INT_NA | UNIPHIER_FI2C_INT_AL)
76 #define UNIPHIER_FI2C_INT_STOP \
77 (UNIPHIER_FI2C_INT_TC | UNIPHIER_FI2C_INT_RC)
79 #define UNIPHIER_FI2C_RD BIT(0)
80 #define UNIPHIER_FI2C_STOP BIT(1)
81 #define UNIPHIER_FI2C_MANUAL_NACK BIT(2)
82 #define UNIPHIER_FI2C_BYTE_WISE BIT(3)
83 #define UNIPHIER_FI2C_DEFER_STOP_COMP BIT(4)
85 #define UNIPHIER_FI2C_DEFAULT_SPEED 100000
86 #define UNIPHIER_FI2C_MAX_SPEED 400000
87 #define UNIPHIER_FI2C_FIFO_SIZE 8
89 struct uniphier_fi2c_priv {
90 struct completion comp;
91 struct i2c_adapter adap;
92 void __iomem *membase;
99 unsigned int busy_cnt;
100 unsigned int clk_cycle;
103 static void uniphier_fi2c_fill_txfifo(struct uniphier_fi2c_priv *priv,
106 int fifo_space = UNIPHIER_FI2C_FIFO_SIZE;
109 * TX-FIFO stores slave address in it for the first access.
110 * Decrement the counter.
116 if (fifo_space-- <= 0)
119 dev_dbg(&priv->adap.dev, "write data: %02x\n", *priv->buf);
120 writel(*priv->buf++, priv->membase + UNIPHIER_FI2C_DTTX);
125 static void uniphier_fi2c_drain_rxfifo(struct uniphier_fi2c_priv *priv)
127 int fifo_left = priv->flags & UNIPHIER_FI2C_BYTE_WISE ?
128 1 : UNIPHIER_FI2C_FIFO_SIZE;
131 if (fifo_left-- <= 0)
134 *priv->buf++ = readl(priv->membase + UNIPHIER_FI2C_DTRX);
135 dev_dbg(&priv->adap.dev, "read data: %02x\n", priv->buf[-1]);
140 static void uniphier_fi2c_set_irqs(struct uniphier_fi2c_priv *priv)
142 writel(priv->enabled_irqs, priv->membase + UNIPHIER_FI2C_IE);
145 static void uniphier_fi2c_clear_irqs(struct uniphier_fi2c_priv *priv)
147 writel(-1, priv->membase + UNIPHIER_FI2C_IC);
150 static void uniphier_fi2c_stop(struct uniphier_fi2c_priv *priv)
152 dev_dbg(&priv->adap.dev, "stop condition\n");
154 priv->enabled_irqs |= UNIPHIER_FI2C_INT_STOP;
155 uniphier_fi2c_set_irqs(priv);
156 writel(UNIPHIER_FI2C_CR_MST | UNIPHIER_FI2C_CR_STO,
157 priv->membase + UNIPHIER_FI2C_CR);
160 static irqreturn_t uniphier_fi2c_interrupt(int irq, void *dev_id)
162 struct uniphier_fi2c_priv *priv = dev_id;
165 irq_status = readl(priv->membase + UNIPHIER_FI2C_INT);
167 dev_dbg(&priv->adap.dev,
168 "interrupt: enabled_irqs=%04x, irq_status=%04x\n",
169 priv->enabled_irqs, irq_status);
171 if (irq_status & UNIPHIER_FI2C_INT_STOP)
174 if (unlikely(irq_status & UNIPHIER_FI2C_INT_AL)) {
175 dev_dbg(&priv->adap.dev, "arbitration lost\n");
176 priv->error = -EAGAIN;
180 if (unlikely(irq_status & UNIPHIER_FI2C_INT_NA)) {
181 dev_dbg(&priv->adap.dev, "could not get ACK\n");
182 priv->error = -ENXIO;
183 if (priv->flags & UNIPHIER_FI2C_RD) {
185 * work around a hardware bug:
186 * The receive-completed interrupt is never set even if
187 * STOP condition is detected after the address phase
188 * of read transaction fails to get ACK.
189 * To avoid time-out error, we issue STOP here,
190 * but do not wait for its completion.
191 * It should be checked after exiting this handler.
193 uniphier_fi2c_stop(priv);
194 priv->flags |= UNIPHIER_FI2C_DEFER_STOP_COMP;
200 if (irq_status & UNIPHIER_FI2C_INT_TE) {
204 uniphier_fi2c_fill_txfifo(priv, false);
208 if (irq_status & (UNIPHIER_FI2C_INT_RF | UNIPHIER_FI2C_INT_RB)) {
209 uniphier_fi2c_drain_rxfifo(priv);
213 if (unlikely(priv->flags & UNIPHIER_FI2C_MANUAL_NACK)) {
214 if (priv->len <= UNIPHIER_FI2C_FIFO_SIZE &&
215 !(priv->flags & UNIPHIER_FI2C_BYTE_WISE)) {
216 dev_dbg(&priv->adap.dev,
217 "enable read byte count IRQ\n");
218 priv->enabled_irqs |= UNIPHIER_FI2C_INT_RB;
219 uniphier_fi2c_set_irqs(priv);
220 priv->flags |= UNIPHIER_FI2C_BYTE_WISE;
222 if (priv->len <= 1) {
223 dev_dbg(&priv->adap.dev, "set NACK\n");
224 writel(UNIPHIER_FI2C_CR_MST |
225 UNIPHIER_FI2C_CR_NACK,
226 priv->membase + UNIPHIER_FI2C_CR);
236 if (priv->flags & UNIPHIER_FI2C_STOP) {
238 uniphier_fi2c_stop(priv);
241 priv->enabled_irqs = 0;
242 uniphier_fi2c_set_irqs(priv);
243 complete(&priv->comp);
247 uniphier_fi2c_clear_irqs(priv);
252 static void uniphier_fi2c_tx_init(struct uniphier_fi2c_priv *priv, u16 addr)
254 priv->enabled_irqs |= UNIPHIER_FI2C_INT_TE;
255 /* do not use TX byte counter */
256 writel(0, priv->membase + UNIPHIER_FI2C_TBC);
257 /* set slave address */
258 writel(UNIPHIER_FI2C_DTTX_CMD | addr << 1,
259 priv->membase + UNIPHIER_FI2C_DTTX);
260 /* first chunk of data */
261 uniphier_fi2c_fill_txfifo(priv, true);
264 static void uniphier_fi2c_rx_init(struct uniphier_fi2c_priv *priv, u16 addr)
266 priv->flags |= UNIPHIER_FI2C_RD;
268 if (likely(priv->len < 256)) {
270 * If possible, use RX byte counter.
271 * It can automatically handle NACK for the last byte.
273 writel(priv->len, priv->membase + UNIPHIER_FI2C_RBC);
274 priv->enabled_irqs |= UNIPHIER_FI2C_INT_RF |
275 UNIPHIER_FI2C_INT_RB;
278 * The byte counter can not count over 256. In this case,
279 * do not use it at all. Drain data when FIFO gets full,
280 * but treat the last portion as a special case.
282 writel(0, priv->membase + UNIPHIER_FI2C_RBC);
283 priv->flags |= UNIPHIER_FI2C_MANUAL_NACK;
284 priv->enabled_irqs |= UNIPHIER_FI2C_INT_RF;
287 /* set slave address with RD bit */
288 writel(UNIPHIER_FI2C_DTTX_CMD | UNIPHIER_FI2C_DTTX_RD | addr << 1,
289 priv->membase + UNIPHIER_FI2C_DTTX);
292 static void uniphier_fi2c_reset(struct uniphier_fi2c_priv *priv)
294 writel(UNIPHIER_FI2C_RST_RST, priv->membase + UNIPHIER_FI2C_RST);
297 static void uniphier_fi2c_prepare_operation(struct uniphier_fi2c_priv *priv)
299 writel(UNIPHIER_FI2C_BRST_FOEN | UNIPHIER_FI2C_BRST_RSCL,
300 priv->membase + UNIPHIER_FI2C_BRST);
303 static void uniphier_fi2c_recover(struct uniphier_fi2c_priv *priv)
305 uniphier_fi2c_reset(priv);
306 i2c_recover_bus(&priv->adap);
309 static int uniphier_fi2c_master_xfer_one(struct i2c_adapter *adap,
310 struct i2c_msg *msg, bool stop)
312 struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
313 bool is_read = msg->flags & I2C_M_RD;
314 unsigned long time_left;
316 dev_dbg(&adap->dev, "%s: addr=0x%02x, len=%d, stop=%d\n",
317 is_read ? "receive" : "transmit", msg->addr, msg->len, stop);
319 priv->len = msg->len;
320 priv->buf = msg->buf;
321 priv->enabled_irqs = UNIPHIER_FI2C_INT_FAULTS;
326 priv->flags |= UNIPHIER_FI2C_STOP;
328 reinit_completion(&priv->comp);
329 uniphier_fi2c_clear_irqs(priv);
330 writel(UNIPHIER_FI2C_RST_TBRST | UNIPHIER_FI2C_RST_RBRST,
331 priv->membase + UNIPHIER_FI2C_RST); /* reset TX/RX FIFO */
334 uniphier_fi2c_rx_init(priv, msg->addr);
336 uniphier_fi2c_tx_init(priv, msg->addr);
338 uniphier_fi2c_set_irqs(priv);
340 dev_dbg(&adap->dev, "start condition\n");
341 writel(UNIPHIER_FI2C_CR_MST | UNIPHIER_FI2C_CR_STA,
342 priv->membase + UNIPHIER_FI2C_CR);
344 time_left = wait_for_completion_timeout(&priv->comp, adap->timeout);
346 dev_err(&adap->dev, "transaction timeout.\n");
347 uniphier_fi2c_recover(priv);
350 dev_dbg(&adap->dev, "complete\n");
352 if (unlikely(priv->flags & UNIPHIER_FI2C_DEFER_STOP_COMP)) {
356 ret = readl_poll_timeout(priv->membase + UNIPHIER_FI2C_SR,
358 (status & UNIPHIER_FI2C_SR_STS) &&
359 !(status & UNIPHIER_FI2C_SR_BB),
363 "stop condition was not completed.\n");
364 uniphier_fi2c_recover(priv);
372 static int uniphier_fi2c_check_bus_busy(struct i2c_adapter *adap)
374 struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
376 if (readl(priv->membase + UNIPHIER_FI2C_SR) & UNIPHIER_FI2C_SR_DB) {
377 if (priv->busy_cnt++ > 3) {
379 * If bus busy continues too long, it is probably
380 * in a wrong state. Try bus recovery.
382 uniphier_fi2c_recover(priv);
393 static int uniphier_fi2c_master_xfer(struct i2c_adapter *adap,
394 struct i2c_msg *msgs, int num)
396 struct i2c_msg *msg, *emsg = msgs + num;
399 ret = uniphier_fi2c_check_bus_busy(adap);
403 for (msg = msgs; msg < emsg; msg++) {
404 /* If next message is read, skip the stop condition */
405 bool stop = !(msg + 1 < emsg && msg[1].flags & I2C_M_RD);
406 /* but, force it if I2C_M_STOP is set */
407 if (msg->flags & I2C_M_STOP)
410 ret = uniphier_fi2c_master_xfer_one(adap, msg, stop);
418 static u32 uniphier_fi2c_functionality(struct i2c_adapter *adap)
420 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
423 static const struct i2c_algorithm uniphier_fi2c_algo = {
424 .master_xfer = uniphier_fi2c_master_xfer,
425 .functionality = uniphier_fi2c_functionality,
428 static int uniphier_fi2c_get_scl(struct i2c_adapter *adap)
430 struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
432 return !!(readl(priv->membase + UNIPHIER_FI2C_BM) &
433 UNIPHIER_FI2C_BM_SCLS);
436 static void uniphier_fi2c_set_scl(struct i2c_adapter *adap, int val)
438 struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
440 writel(val ? UNIPHIER_FI2C_BRST_RSCL : 0,
441 priv->membase + UNIPHIER_FI2C_BRST);
444 static int uniphier_fi2c_get_sda(struct i2c_adapter *adap)
446 struct uniphier_fi2c_priv *priv = i2c_get_adapdata(adap);
448 return !!(readl(priv->membase + UNIPHIER_FI2C_BM) &
449 UNIPHIER_FI2C_BM_SDAS);
452 static void uniphier_fi2c_unprepare_recovery(struct i2c_adapter *adap)
454 uniphier_fi2c_prepare_operation(i2c_get_adapdata(adap));
457 static struct i2c_bus_recovery_info uniphier_fi2c_bus_recovery_info = {
458 .recover_bus = i2c_generic_scl_recovery,
459 .get_scl = uniphier_fi2c_get_scl,
460 .set_scl = uniphier_fi2c_set_scl,
461 .get_sda = uniphier_fi2c_get_sda,
462 .unprepare_recovery = uniphier_fi2c_unprepare_recovery,
465 static void uniphier_fi2c_hw_init(struct uniphier_fi2c_priv *priv)
467 unsigned int cyc = priv->clk_cycle;
470 tmp = readl(priv->membase + UNIPHIER_FI2C_CR);
471 tmp |= UNIPHIER_FI2C_CR_MST;
472 writel(tmp, priv->membase + UNIPHIER_FI2C_CR);
474 uniphier_fi2c_reset(priv);
476 writel(cyc, priv->membase + UNIPHIER_FI2C_CYC);
477 writel(cyc / 2, priv->membase + UNIPHIER_FI2C_LCTL);
478 writel(cyc / 2, priv->membase + UNIPHIER_FI2C_SSUT);
479 writel(cyc / 16, priv->membase + UNIPHIER_FI2C_DSUT);
481 uniphier_fi2c_prepare_operation(priv);
484 static int uniphier_fi2c_probe(struct platform_device *pdev)
486 struct device *dev = &pdev->dev;
487 struct uniphier_fi2c_priv *priv;
488 struct resource *regs;
490 unsigned long clk_rate;
493 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
497 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
498 priv->membase = devm_ioremap_resource(dev, regs);
499 if (IS_ERR(priv->membase))
500 return PTR_ERR(priv->membase);
502 irq = platform_get_irq(pdev, 0);
504 dev_err(dev, "failed to get IRQ number\n");
508 if (of_property_read_u32(dev->of_node, "clock-frequency", &bus_speed))
509 bus_speed = UNIPHIER_FI2C_DEFAULT_SPEED;
511 if (!bus_speed || bus_speed > UNIPHIER_FI2C_MAX_SPEED) {
512 dev_err(dev, "invalid clock-frequency %d\n", bus_speed);
516 priv->clk = devm_clk_get(dev, NULL);
517 if (IS_ERR(priv->clk)) {
518 dev_err(dev, "failed to get clock\n");
519 return PTR_ERR(priv->clk);
522 ret = clk_prepare_enable(priv->clk);
526 clk_rate = clk_get_rate(priv->clk);
528 dev_err(dev, "input clock rate should not be zero\n");
533 priv->clk_cycle = clk_rate / bus_speed;
534 init_completion(&priv->comp);
535 priv->adap.owner = THIS_MODULE;
536 priv->adap.algo = &uniphier_fi2c_algo;
537 priv->adap.dev.parent = dev;
538 priv->adap.dev.of_node = dev->of_node;
539 strlcpy(priv->adap.name, "UniPhier FI2C", sizeof(priv->adap.name));
540 priv->adap.bus_recovery_info = &uniphier_fi2c_bus_recovery_info;
541 i2c_set_adapdata(&priv->adap, priv);
542 platform_set_drvdata(pdev, priv);
544 uniphier_fi2c_hw_init(priv);
546 ret = devm_request_irq(dev, irq, uniphier_fi2c_interrupt, 0,
549 dev_err(dev, "failed to request irq %d\n", irq);
553 ret = i2c_add_adapter(&priv->adap);
556 clk_disable_unprepare(priv->clk);
561 static int uniphier_fi2c_remove(struct platform_device *pdev)
563 struct uniphier_fi2c_priv *priv = platform_get_drvdata(pdev);
565 i2c_del_adapter(&priv->adap);
566 clk_disable_unprepare(priv->clk);
571 static int __maybe_unused uniphier_fi2c_suspend(struct device *dev)
573 struct uniphier_fi2c_priv *priv = dev_get_drvdata(dev);
575 clk_disable_unprepare(priv->clk);
580 static int __maybe_unused uniphier_fi2c_resume(struct device *dev)
582 struct uniphier_fi2c_priv *priv = dev_get_drvdata(dev);
585 ret = clk_prepare_enable(priv->clk);
589 uniphier_fi2c_hw_init(priv);
594 static const struct dev_pm_ops uniphier_fi2c_pm_ops = {
595 SET_SYSTEM_SLEEP_PM_OPS(uniphier_fi2c_suspend, uniphier_fi2c_resume)
598 static const struct of_device_id uniphier_fi2c_match[] = {
599 { .compatible = "socionext,uniphier-fi2c" },
602 MODULE_DEVICE_TABLE(of, uniphier_fi2c_match);
604 static struct platform_driver uniphier_fi2c_drv = {
605 .probe = uniphier_fi2c_probe,
606 .remove = uniphier_fi2c_remove,
608 .name = "uniphier-fi2c",
609 .of_match_table = uniphier_fi2c_match,
610 .pm = &uniphier_fi2c_pm_ops,
613 module_platform_driver(uniphier_fi2c_drv);
615 MODULE_AUTHOR("Masahiro Yamada <yamada.masahiro@socionext.com>");
616 MODULE_DESCRIPTION("UniPhier FIFO-builtin I2C bus driver");
617 MODULE_LICENSE("GPL");