blob: ced606bf36fd179bd6f1352b3bf2fea0855a909f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +09002/*
Masahiro Yamadafa1f73f2016-07-19 21:56:13 +09003 * Copyright (C) 2014 Panasonic Corporation
4 * Copyright (C) 2015-2016 Socionext Inc.
5 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +09006 */
7
Masahiro Yamada8945b4f2017-10-13 19:21:58 +09008#include <linux/errno.h>
Masahiro Yamada663a23f2015-05-29 17:30:00 +09009#include <linux/io.h>
Masahiro Yamadaccd97022017-01-28 06:53:56 +090010#include <linux/iopoll.h>
Masahiro Yamada78752d62016-03-24 22:32:40 +090011#include <linux/sizes.h>
Masahiro Yamada8945b4f2017-10-13 19:21:58 +090012#include <linux/types.h>
13#include <dm.h>
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +090014#include <i2c.h>
15#include <fdtdec.h>
16
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +090017struct uniphier_fi2c_regs {
18 u32 cr; /* control register */
19#define I2C_CR_MST (1 << 3) /* master mode */
20#define I2C_CR_STA (1 << 2) /* start condition */
21#define I2C_CR_STO (1 << 1) /* stop condition */
22#define I2C_CR_NACK (1 << 0) /* not ACK */
23 u32 dttx; /* send FIFO (write-only) */
24#define dtrx dttx /* receive FIFO (read-only) */
25#define I2C_DTTX_CMD (1 << 8) /* send command (slave addr) */
26#define I2C_DTTX_RD (1 << 0) /* read */
27 u32 __reserved; /* no register at offset 0x08 */
28 u32 slad; /* slave address */
29 u32 cyc; /* clock cycle control */
30 u32 lctl; /* clock low period control */
31 u32 ssut; /* restart/stop setup time control */
32 u32 dsut; /* data setup time control */
33 u32 intr; /* interrupt status */
34 u32 ie; /* interrupt enable */
35 u32 ic; /* interrupt clear */
36#define I2C_INT_TE (1 << 9) /* TX FIFO empty */
37#define I2C_INT_RB (1 << 4) /* received specified bytes */
38#define I2C_INT_NA (1 << 2) /* no answer */
39#define I2C_INT_AL (1 << 1) /* arbitration lost */
40 u32 sr; /* status register */
41#define I2C_SR_DB (1 << 12) /* device busy */
42#define I2C_SR_BB (1 << 8) /* bus busy */
43#define I2C_SR_RFF (1 << 3) /* Rx FIFO full */
44#define I2C_SR_RNE (1 << 2) /* Rx FIFO not empty */
45#define I2C_SR_TNF (1 << 1) /* Tx FIFO not full */
46#define I2C_SR_TFE (1 << 0) /* Tx FIFO empty */
47 u32 __reserved2; /* no register at offset 0x30 */
48 u32 rst; /* reset control */
49#define I2C_RST_TBRST (1 << 2) /* clear Tx FIFO */
50#define I2C_RST_RBRST (1 << 1) /* clear Rx FIFO */
51#define I2C_RST_RST (1 << 0) /* forcible bus reset */
52 u32 bm; /* bus monitor */
53 u32 noise; /* noise filter control */
54 u32 tbc; /* Tx byte count setting */
55 u32 rbc; /* Rx byte count setting */
56 u32 tbcm; /* Tx byte count monitor */
57 u32 rbcm; /* Rx byte count monitor */
58 u32 brst; /* bus reset */
59#define I2C_BRST_FOEN (1 << 1) /* normal operation */
60#define I2C_BRST_RSCLO (1 << 0) /* release SCL low fixing */
61};
62
63#define FIOCLK 50000000
64
Masahiro Yamada8945b4f2017-10-13 19:21:58 +090065struct uniphier_fi2c_priv {
66 struct udevice *dev;
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +090067 struct uniphier_fi2c_regs __iomem *regs; /* register base */
68 unsigned long fioclk; /* internal operation clock */
69 unsigned long timeout; /* time out (us) */
70};
71
Masahiro Yamada8945b4f2017-10-13 19:21:58 +090072static void uniphier_fi2c_reset(struct uniphier_fi2c_priv *priv)
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +090073{
Masahiro Yamada8945b4f2017-10-13 19:21:58 +090074 writel(I2C_RST_RST, &priv->regs->rst);
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +090075}
76
Masahiro Yamada8945b4f2017-10-13 19:21:58 +090077static int uniphier_fi2c_check_bus_busy(struct uniphier_fi2c_priv *priv)
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +090078{
Masahiro Yamadaccd97022017-01-28 06:53:56 +090079 u32 val;
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +090080 int ret;
81
Masahiro Yamada8945b4f2017-10-13 19:21:58 +090082 ret = readl_poll_timeout(&priv->regs->sr, val, !(val & I2C_SR_DB), 100);
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +090083 if (ret < 0) {
Masahiro Yamada8945b4f2017-10-13 19:21:58 +090084 dev_dbg(priv->dev, "error: device busy too long. reset...\n");
85 uniphier_fi2c_reset(priv);
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +090086 }
87
88 return ret;
89}
90
91static int uniphier_fi2c_probe(struct udevice *dev)
92{
93 fdt_addr_t addr;
Masahiro Yamada8945b4f2017-10-13 19:21:58 +090094 struct uniphier_fi2c_priv *priv = dev_get_priv(dev);
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +090095
Simon Glassba1dea42017-05-17 17:18:05 -060096 addr = devfdt_get_addr(dev);
Masahiro Yamada78752d62016-03-24 22:32:40 +090097 if (addr == FDT_ADDR_T_NONE)
98 return -EINVAL;
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +090099
Masahiro Yamadafa1f73f2016-07-19 21:56:13 +0900100 priv->regs = devm_ioremap(dev, addr, SZ_128);
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900101 if (!priv->regs)
102 return -ENOMEM;
103
104 priv->fioclk = FIOCLK;
105
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900106 priv->dev = dev;
107
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900108 /* bus forcible reset */
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900109 uniphier_fi2c_reset(priv);
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900110
111 writel(I2C_BRST_FOEN | I2C_BRST_RSCLO, &priv->regs->brst);
112
113 return 0;
114}
115
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900116static int wait_for_irq(struct uniphier_fi2c_priv *priv, u32 flags,
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900117 bool *stop)
118{
119 u32 irq;
Masahiro Yamadaccd97022017-01-28 06:53:56 +0900120 int ret;
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900121
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900122 ret = readl_poll_timeout(&priv->regs->intr, irq, irq & flags,
123 priv->timeout);
Masahiro Yamadaccd97022017-01-28 06:53:56 +0900124 if (ret < 0) {
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900125 dev_dbg(priv->dev, "error: time out\n");
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900126 return ret;
127 }
128
129 if (irq & I2C_INT_AL) {
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900130 dev_dbg(priv->dev, "error: arbitration lost\n");
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900131 *stop = false;
132 return ret;
133 }
134
135 if (irq & I2C_INT_NA) {
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900136 dev_dbg(priv->dev, "error: no answer\n");
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900137 return ret;
138 }
139
140 return 0;
141}
142
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900143static int issue_stop(struct uniphier_fi2c_priv *priv, int old_ret)
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900144{
145 int ret;
146
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900147 dev_dbg(priv->dev, "stop condition\n");
148 writel(I2C_CR_MST | I2C_CR_STO, &priv->regs->cr);
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900149
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900150 ret = uniphier_fi2c_check_bus_busy(priv);
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900151 if (ret < 0)
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900152 dev_dbg(priv->dev, "error: device busy after operation\n");
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900153
154 return old_ret ? old_ret : ret;
155}
156
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900157static int uniphier_fi2c_transmit(struct uniphier_fi2c_priv *priv, uint addr,
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900158 uint len, const u8 *buf, bool *stop)
159{
160 int ret;
161 const u32 irq_flags = I2C_INT_TE | I2C_INT_NA | I2C_INT_AL;
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900162 struct uniphier_fi2c_regs __iomem *regs = priv->regs;
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900163
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900164 dev_dbg(priv->dev, "%s: addr = %x, len = %d\n", __func__, addr, len);
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900165
166 writel(I2C_DTTX_CMD | addr << 1, &regs->dttx);
167
168 writel(irq_flags, &regs->ie);
169 writel(irq_flags, &regs->ic);
170
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900171 dev_dbg(priv->dev, "start condition\n");
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900172 writel(I2C_CR_MST | I2C_CR_STA, &regs->cr);
173
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900174 ret = wait_for_irq(priv, irq_flags, stop);
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900175 if (ret < 0)
176 goto error;
177
178 while (len--) {
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900179 dev_dbg(priv->dev, "sending %x\n", *buf);
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900180 writel(*buf++, &regs->dttx);
181
182 writel(irq_flags, &regs->ic);
183
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900184 ret = wait_for_irq(priv, irq_flags, stop);
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900185 if (ret < 0)
186 goto error;
187 }
188
189error:
190 writel(irq_flags, &regs->ic);
191
192 if (*stop)
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900193 ret = issue_stop(priv, ret);
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900194
195 return ret;
196}
197
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900198static int uniphier_fi2c_receive(struct uniphier_fi2c_priv *priv, uint addr,
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900199 uint len, u8 *buf, bool *stop)
200{
201 int ret = 0;
202 const u32 irq_flags = I2C_INT_RB | I2C_INT_NA | I2C_INT_AL;
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900203 struct uniphier_fi2c_regs __iomem *regs = priv->regs;
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900204
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900205 dev_dbg(priv->dev, "%s: addr = %x, len = %d\n", __func__, addr, len);
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900206
207 /*
208 * In case 'len == 0', only the slave address should be sent
209 * for probing, which is covered by the transmit function.
210 */
211 if (len == 0)
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900212 return uniphier_fi2c_transmit(priv, addr, len, buf, stop);
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900213
214 writel(I2C_DTTX_CMD | I2C_DTTX_RD | addr << 1, &regs->dttx);
215
216 writel(0, &regs->rbc);
217 writel(irq_flags, &regs->ie);
218 writel(irq_flags, &regs->ic);
219
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900220 dev_dbg(priv->dev, "start condition\n");
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900221 writel(I2C_CR_MST | I2C_CR_STA | (len == 1 ? I2C_CR_NACK : 0),
222 &regs->cr);
223
224 while (len--) {
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900225 ret = wait_for_irq(priv, irq_flags, stop);
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900226 if (ret < 0)
227 goto error;
228
229 *buf++ = readl(&regs->dtrx);
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900230 dev_dbg(priv->dev, "received %x\n", *(buf - 1));
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900231
232 if (len == 1)
233 writel(I2C_CR_MST | I2C_CR_NACK, &regs->cr);
234
235 writel(irq_flags, &regs->ic);
236 }
237
238error:
239 writel(irq_flags, &regs->ic);
240
241 if (*stop)
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900242 ret = issue_stop(priv, ret);
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900243
244 return ret;
245}
246
247static int uniphier_fi2c_xfer(struct udevice *bus, struct i2c_msg *msg,
248 int nmsgs)
249{
250 int ret;
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900251 struct uniphier_fi2c_priv *priv = dev_get_priv(bus);
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900252 bool stop;
253
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900254 ret = uniphier_fi2c_check_bus_busy(priv);
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900255 if (ret < 0)
256 return ret;
257
258 for (; nmsgs > 0; nmsgs--, msg++) {
259 /* If next message is read, skip the stop condition */
260 stop = nmsgs > 1 && msg[1].flags & I2C_M_RD ? false : true;
261
262 if (msg->flags & I2C_M_RD)
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900263 ret = uniphier_fi2c_receive(priv, msg->addr, msg->len,
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900264 msg->buf, &stop);
265 else
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900266 ret = uniphier_fi2c_transmit(priv, msg->addr, msg->len,
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900267 msg->buf, &stop);
268
269 if (ret < 0)
270 break;
271 }
272
273 return ret;
274}
275
276static int uniphier_fi2c_set_bus_speed(struct udevice *bus, unsigned int speed)
277{
278 int ret;
279 unsigned int clk_count;
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900280 struct uniphier_fi2c_priv *priv = dev_get_priv(bus);
281 struct uniphier_fi2c_regs __iomem *regs = priv->regs;
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900282
283 /* max supported frequency is 400 kHz */
284 if (speed > 400000)
285 return -EINVAL;
286
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900287 ret = uniphier_fi2c_check_bus_busy(priv);
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900288 if (ret < 0)
289 return ret;
290
291 /* make sure the bus is idle when changing the frequency */
292 writel(I2C_BRST_RSCLO, &regs->brst);
293
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900294 clk_count = priv->fioclk / speed;
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900295
296 writel(clk_count, &regs->cyc);
297 writel(clk_count / 2, &regs->lctl);
298 writel(clk_count / 2, &regs->ssut);
299 writel(clk_count / 16, &regs->dsut);
300
301 writel(I2C_BRST_FOEN | I2C_BRST_RSCLO, &regs->brst);
302
303 /*
304 * Theoretically, each byte can be transferred in
305 * 1000000 * 9 / speed usec.
306 * This time out value is long enough.
307 */
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900308 priv->timeout = 100000000L / speed;
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900309
310 return 0;
311}
312
313static const struct dm_i2c_ops uniphier_fi2c_ops = {
314 .xfer = uniphier_fi2c_xfer,
315 .set_bus_speed = uniphier_fi2c_set_bus_speed,
316};
317
318static const struct udevice_id uniphier_fi2c_of_match[] = {
Masahiro Yamadad5f83a42015-03-11 15:54:46 +0900319 { .compatible = "socionext,uniphier-fi2c" },
320 { /* sentinel */ }
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900321};
322
323U_BOOT_DRIVER(uniphier_fi2c) = {
324 .name = "uniphier-fi2c",
325 .id = UCLASS_I2C,
326 .of_match = uniphier_fi2c_of_match,
327 .probe = uniphier_fi2c_probe,
Masahiro Yamada8945b4f2017-10-13 19:21:58 +0900328 .priv_auto_alloc_size = sizeof(struct uniphier_fi2c_priv),
Masahiro Yamada4e82e5e2015-01-13 12:44:37 +0900329 .ops = &uniphier_fi2c_ops,
330};