Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 2 | /* |
Masahiro Yamada | fa1f73f | 2016-07-19 21:56:13 +0900 | [diff] [blame] | 3 | * Copyright (C) 2014 Panasonic Corporation |
| 4 | * Copyright (C) 2015-2016 Socionext Inc. |
| 5 | * Author: Masahiro Yamada <yamada.masahiro@socionext.com> |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 6 | */ |
| 7 | |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 8 | #include <dm/device_compat.h> |
Masahiro Yamada | ec0aa7c | 2017-10-13 19:21:57 +0900 | [diff] [blame] | 9 | #include <linux/delay.h> |
| 10 | #include <linux/errno.h> |
Masahiro Yamada | 663a23f | 2015-05-29 17:30:00 +0900 | [diff] [blame] | 11 | #include <linux/io.h> |
Masahiro Yamada | 78752d6 | 2016-03-24 22:32:40 +0900 | [diff] [blame] | 12 | #include <linux/sizes.h> |
Masahiro Yamada | ec0aa7c | 2017-10-13 19:21:57 +0900 | [diff] [blame] | 13 | #include <linux/types.h> |
| 14 | #include <dm.h> |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 15 | #include <fdtdec.h> |
Masahiro Yamada | ec0aa7c | 2017-10-13 19:21:57 +0900 | [diff] [blame] | 16 | #include <i2c.h> |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 17 | |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 18 | struct uniphier_i2c_regs { |
| 19 | u32 dtrm; /* data transmission */ |
| 20 | #define I2C_DTRM_STA (1 << 10) |
| 21 | #define I2C_DTRM_STO (1 << 9) |
| 22 | #define I2C_DTRM_NACK (1 << 8) |
| 23 | #define I2C_DTRM_RD (1 << 0) |
| 24 | u32 drec; /* data reception */ |
| 25 | #define I2C_DREC_STS (1 << 12) |
| 26 | #define I2C_DREC_LRB (1 << 11) |
| 27 | #define I2C_DREC_LAB (1 << 9) |
| 28 | u32 myad; /* slave address */ |
| 29 | u32 clk; /* clock frequency control */ |
| 30 | u32 brst; /* bus reset */ |
| 31 | #define I2C_BRST_FOEN (1 << 1) |
| 32 | #define I2C_BRST_BRST (1 << 0) |
| 33 | u32 hold; /* hold time control */ |
| 34 | u32 bsts; /* bus status monitor */ |
| 35 | u32 noise; /* noise filter control */ |
| 36 | u32 setup; /* setup time control */ |
| 37 | }; |
| 38 | |
| 39 | #define IOBUS_FREQ 100000000 |
| 40 | |
Masahiro Yamada | ec0aa7c | 2017-10-13 19:21:57 +0900 | [diff] [blame] | 41 | struct uniphier_i2c_priv { |
| 42 | struct udevice *dev; |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 43 | struct uniphier_i2c_regs __iomem *regs; /* register base */ |
| 44 | unsigned long input_clk; /* master clock (Hz) */ |
| 45 | unsigned long wait_us; /* wait for every byte transfer (us) */ |
| 46 | }; |
| 47 | |
| 48 | static int uniphier_i2c_probe(struct udevice *dev) |
| 49 | { |
| 50 | fdt_addr_t addr; |
Masahiro Yamada | ec0aa7c | 2017-10-13 19:21:57 +0900 | [diff] [blame] | 51 | struct uniphier_i2c_priv *priv = dev_get_priv(dev); |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 52 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 53 | addr = dev_read_addr(dev); |
Masahiro Yamada | 78752d6 | 2016-03-24 22:32:40 +0900 | [diff] [blame] | 54 | if (addr == FDT_ADDR_T_NONE) |
| 55 | return -EINVAL; |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 56 | |
Masahiro Yamada | fa1f73f | 2016-07-19 21:56:13 +0900 | [diff] [blame] | 57 | priv->regs = devm_ioremap(dev, addr, SZ_64); |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 58 | if (!priv->regs) |
| 59 | return -ENOMEM; |
| 60 | |
| 61 | priv->input_clk = IOBUS_FREQ; |
| 62 | |
Masahiro Yamada | ec0aa7c | 2017-10-13 19:21:57 +0900 | [diff] [blame] | 63 | priv->dev = dev; |
| 64 | |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 65 | /* deassert reset */ |
| 66 | writel(0x3, &priv->regs->brst); |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |
Masahiro Yamada | ec0aa7c | 2017-10-13 19:21:57 +0900 | [diff] [blame] | 71 | static int send_and_recv_byte(struct uniphier_i2c_priv *priv, u32 dtrm) |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 72 | { |
Masahiro Yamada | ec0aa7c | 2017-10-13 19:21:57 +0900 | [diff] [blame] | 73 | writel(dtrm, &priv->regs->dtrm); |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 74 | |
| 75 | /* |
| 76 | * This controller only provides interruption to inform the completion |
| 77 | * of each byte transfer. (No status register to poll it.) |
| 78 | * Unfortunately, U-Boot does not have a good support of interrupt. |
| 79 | * Wait for a while. |
| 80 | */ |
Masahiro Yamada | ec0aa7c | 2017-10-13 19:21:57 +0900 | [diff] [blame] | 81 | udelay(priv->wait_us); |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 82 | |
Masahiro Yamada | ec0aa7c | 2017-10-13 19:21:57 +0900 | [diff] [blame] | 83 | return readl(&priv->regs->drec); |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 84 | } |
| 85 | |
Masahiro Yamada | ec0aa7c | 2017-10-13 19:21:57 +0900 | [diff] [blame] | 86 | static int send_byte(struct uniphier_i2c_priv *priv, u32 dtrm, bool *stop) |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 87 | { |
| 88 | int ret = 0; |
| 89 | u32 drec; |
| 90 | |
Masahiro Yamada | ec0aa7c | 2017-10-13 19:21:57 +0900 | [diff] [blame] | 91 | drec = send_and_recv_byte(priv, dtrm); |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 92 | |
| 93 | if (drec & I2C_DREC_LAB) { |
Masahiro Yamada | ec0aa7c | 2017-10-13 19:21:57 +0900 | [diff] [blame] | 94 | dev_dbg(priv->dev, "uniphier_i2c: bus arbitration failed\n"); |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 95 | *stop = false; |
| 96 | ret = -EREMOTEIO; |
| 97 | } |
| 98 | if (drec & I2C_DREC_LRB) { |
Masahiro Yamada | ec0aa7c | 2017-10-13 19:21:57 +0900 | [diff] [blame] | 99 | dev_dbg(priv->dev, "uniphier_i2c: slave did not return ACK\n"); |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 100 | ret = -EREMOTEIO; |
| 101 | } |
| 102 | return ret; |
| 103 | } |
| 104 | |
Masahiro Yamada | ec0aa7c | 2017-10-13 19:21:57 +0900 | [diff] [blame] | 105 | static int uniphier_i2c_transmit(struct uniphier_i2c_priv *priv, uint addr, |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 106 | uint len, const u8 *buf, bool *stop) |
| 107 | { |
| 108 | int ret; |
| 109 | |
Masahiro Yamada | ec0aa7c | 2017-10-13 19:21:57 +0900 | [diff] [blame] | 110 | dev_dbg(priv->dev, "%s: addr = %x, len = %d\n", __func__, addr, len); |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 111 | |
Masahiro Yamada | ec0aa7c | 2017-10-13 19:21:57 +0900 | [diff] [blame] | 112 | ret = send_byte(priv, I2C_DTRM_STA | I2C_DTRM_NACK | addr << 1, stop); |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 113 | if (ret < 0) |
| 114 | goto fail; |
| 115 | |
| 116 | while (len--) { |
Masahiro Yamada | ec0aa7c | 2017-10-13 19:21:57 +0900 | [diff] [blame] | 117 | ret = send_byte(priv, I2C_DTRM_NACK | *buf++, stop); |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 118 | if (ret < 0) |
| 119 | goto fail; |
| 120 | } |
| 121 | |
| 122 | fail: |
| 123 | if (*stop) |
Masahiro Yamada | ec0aa7c | 2017-10-13 19:21:57 +0900 | [diff] [blame] | 124 | writel(I2C_DTRM_STO | I2C_DTRM_NACK, &priv->regs->dtrm); |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 125 | |
| 126 | return ret; |
| 127 | } |
| 128 | |
Masahiro Yamada | ec0aa7c | 2017-10-13 19:21:57 +0900 | [diff] [blame] | 129 | static int uniphier_i2c_receive(struct uniphier_i2c_priv *priv, uint addr, |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 130 | uint len, u8 *buf, bool *stop) |
| 131 | { |
| 132 | int ret; |
| 133 | |
Masahiro Yamada | ec0aa7c | 2017-10-13 19:21:57 +0900 | [diff] [blame] | 134 | dev_dbg(priv->dev, "%s: addr = %x, len = %d\n", __func__, addr, len); |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 135 | |
Masahiro Yamada | ec0aa7c | 2017-10-13 19:21:57 +0900 | [diff] [blame] | 136 | ret = send_byte(priv, I2C_DTRM_STA | I2C_DTRM_NACK | |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 137 | I2C_DTRM_RD | addr << 1, stop); |
| 138 | if (ret < 0) |
| 139 | goto fail; |
| 140 | |
| 141 | while (len--) |
Masahiro Yamada | ec0aa7c | 2017-10-13 19:21:57 +0900 | [diff] [blame] | 142 | *buf++ = send_and_recv_byte(priv, len ? 0 : I2C_DTRM_NACK); |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 143 | |
| 144 | fail: |
| 145 | if (*stop) |
Masahiro Yamada | ec0aa7c | 2017-10-13 19:21:57 +0900 | [diff] [blame] | 146 | writel(I2C_DTRM_STO | I2C_DTRM_NACK, &priv->regs->dtrm); |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 147 | |
| 148 | return ret; |
| 149 | } |
| 150 | |
| 151 | static int uniphier_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, |
| 152 | int nmsgs) |
| 153 | { |
| 154 | int ret = 0; |
Masahiro Yamada | ec0aa7c | 2017-10-13 19:21:57 +0900 | [diff] [blame] | 155 | struct uniphier_i2c_priv *priv = dev_get_priv(bus); |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 156 | bool stop; |
| 157 | |
| 158 | for (; nmsgs > 0; nmsgs--, msg++) { |
| 159 | /* If next message is read, skip the stop condition */ |
| 160 | stop = nmsgs > 1 && msg[1].flags & I2C_M_RD ? false : true; |
| 161 | |
| 162 | if (msg->flags & I2C_M_RD) |
Masahiro Yamada | ec0aa7c | 2017-10-13 19:21:57 +0900 | [diff] [blame] | 163 | ret = uniphier_i2c_receive(priv, msg->addr, msg->len, |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 164 | msg->buf, &stop); |
| 165 | else |
Masahiro Yamada | ec0aa7c | 2017-10-13 19:21:57 +0900 | [diff] [blame] | 166 | ret = uniphier_i2c_transmit(priv, msg->addr, msg->len, |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 167 | msg->buf, &stop); |
| 168 | |
| 169 | if (ret < 0) |
| 170 | break; |
| 171 | } |
| 172 | |
| 173 | return ret; |
| 174 | } |
| 175 | |
| 176 | static int uniphier_i2c_set_bus_speed(struct udevice *bus, unsigned int speed) |
| 177 | { |
Masahiro Yamada | ec0aa7c | 2017-10-13 19:21:57 +0900 | [diff] [blame] | 178 | struct uniphier_i2c_priv *priv = dev_get_priv(bus); |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 179 | |
| 180 | /* max supported frequency is 400 kHz */ |
Simon Glass | f0c99c5 | 2020-01-23 11:48:22 -0700 | [diff] [blame] | 181 | if (speed > I2C_SPEED_FAST_RATE) |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 182 | return -EINVAL; |
| 183 | |
| 184 | /* bus reset: make sure the bus is idle when change the frequency */ |
| 185 | writel(0x1, &priv->regs->brst); |
| 186 | |
| 187 | writel((priv->input_clk / speed / 2 << 16) | (priv->input_clk / speed), |
| 188 | &priv->regs->clk); |
| 189 | |
| 190 | writel(0x3, &priv->regs->brst); |
| 191 | |
| 192 | /* |
| 193 | * Theoretically, each byte can be transferred in |
| 194 | * 1000000 * 9 / speed usec. For safety, wait more than double. |
| 195 | */ |
| 196 | priv->wait_us = 20000000 / speed; |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | static const struct dm_i2c_ops uniphier_i2c_ops = { |
| 203 | .xfer = uniphier_i2c_xfer, |
| 204 | .set_bus_speed = uniphier_i2c_set_bus_speed, |
| 205 | }; |
| 206 | |
| 207 | static const struct udevice_id uniphier_i2c_of_match[] = { |
Masahiro Yamada | d5f83a4 | 2015-03-11 15:54:46 +0900 | [diff] [blame] | 208 | { .compatible = "socionext,uniphier-i2c" }, |
| 209 | { /* sentinel */ } |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 210 | }; |
| 211 | |
| 212 | U_BOOT_DRIVER(uniphier_i2c) = { |
| 213 | .name = "uniphier-i2c", |
| 214 | .id = UCLASS_I2C, |
| 215 | .of_match = uniphier_i2c_of_match, |
| 216 | .probe = uniphier_i2c_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame^] | 217 | .priv_auto = sizeof(struct uniphier_i2c_priv), |
Masahiro Yamada | 96a42ed | 2015-01-13 12:44:36 +0900 | [diff] [blame] | 218 | .ops = &uniphier_i2c_ops, |
| 219 | }; |