1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2018 Arm Ltd.
4 * Author: Liviu Dudau <liviu.dudau@foss.arm.com>
16 #define I2C_CONTROL_REG 0x00
17 #define I2C_SET_REG 0x00
18 #define I2C_CLEAR_REG 0x04
23 struct versatile_i2c_priv {
28 static inline void versatile_sda_set(struct versatile_i2c_priv *priv, u8 state)
30 writel(SDA, priv->base + (state ? I2C_SET_REG : I2C_CLEAR_REG));
34 static inline int versatile_sda_get(struct versatile_i2c_priv *priv)
36 int v = !!(readl(priv->base + I2C_CONTROL_REG) & SDA);
42 static inline void versatile_scl_set(struct versatile_i2c_priv *priv, u8 state)
44 writel(SCL, priv->base + (state ? I2C_SET_REG : I2C_CLEAR_REG));
48 static inline int versatile_scl_get(struct versatile_i2c_priv *priv)
50 int v = !!(readl(priv->base + I2C_CONTROL_REG) & SCL);
56 /* start: SDA goes from high to low while SCL is high */
57 static void versatile_i2c_start(struct versatile_i2c_priv *priv)
60 versatile_sda_set(priv, 1);
61 versatile_scl_set(priv, 1);
62 versatile_sda_set(priv, 0);
65 /* stop: SDA goes from low to high while SCL is high */
66 static void versatile_i2c_stop(struct versatile_i2c_priv *priv)
68 versatile_scl_set(priv, 0);
69 versatile_sda_set(priv, 0);
70 versatile_scl_set(priv, 1);
71 versatile_sda_set(priv, 1);
74 /* read a bit from the SDA line (data or ACK/NACK) */
75 static u8 versatile_i2c_read_bit(struct versatile_i2c_priv *priv)
77 versatile_scl_set(priv, 0);
78 versatile_sda_set(priv, 1);
79 versatile_scl_set(priv, 1);
81 return (u8)versatile_sda_get(priv);
84 /* write a bit on the SDA line */
85 static void versatile_i2c_write_bit(struct versatile_i2c_priv *priv, u8 bit)
87 versatile_scl_set(priv, 0);
88 versatile_sda_set(priv, bit);
89 versatile_scl_set(priv, 1);
93 /* send a reset sequence of 9 clocks with SDA high */
94 static void versatile_i2c_reset_bus(struct versatile_i2c_priv *priv)
98 for (i = 0; i < 9; i++)
99 versatile_i2c_write_bit(priv, 1);
101 versatile_i2c_stop(priv);
104 /* write byte without start/stop sequence */
105 static int versatile_i2c_write_byte(struct versatile_i2c_priv *priv, u8 byte)
109 for (i = 0; i < 8; i++) {
110 versatile_i2c_write_bit(priv, byte & 0x80);
115 nak = versatile_i2c_read_bit(priv);
116 versatile_scl_set(priv, 0);
118 return nak; /* not a nack is an ack */
121 static int versatile_i2c_read_byte(struct versatile_i2c_priv *priv,
127 for (i = 0; i < 8; i++) {
129 *byte |= versatile_i2c_read_bit(priv);
132 versatile_i2c_write_bit(priv, ack);
137 static int versatile_i2c_send_slave_addr(struct versatile_i2c_priv *priv,
143 if (msg->flags & I2C_M_TEN) {
144 /* 10-bit address, send extended address code first */
145 addr = 0xf0 | ((msg->addr >> 7) & 0x06);
146 ret = versatile_i2c_write_byte(priv, addr);
148 versatile_i2c_stop(priv);
153 ret = versatile_i2c_write_byte(priv, msg->addr & 0xff);
155 versatile_i2c_stop(priv);
158 /* reads need to resend the addr */
159 if (msg->flags & I2C_M_RD) {
160 versatile_i2c_start(priv);
162 ret = versatile_i2c_write_byte(priv, addr);
164 versatile_i2c_stop(priv);
169 /* normal 7-bit address */
170 addr = msg->addr << 1;
171 if (msg->flags & I2C_M_RD)
173 ret = versatile_i2c_write_byte(priv, addr);
175 versatile_i2c_stop(priv);
183 static int versatile_i2c_message_xfer(struct versatile_i2c_priv *priv,
189 versatile_i2c_start(priv);
190 if (versatile_i2c_send_slave_addr(priv, msg))
193 for (i = 0; i < msg->len; i++) {
194 if (msg->flags & I2C_M_RD) {
195 ack = (msg->len - i - 1) == 0 ? 1 : 0;
196 ret = versatile_i2c_read_byte(priv, &msg->buf[i], ack);
198 ret = versatile_i2c_write_byte(priv, msg->buf[i]);
205 versatile_i2c_stop(priv);
210 static int versatile_i2c_xfer(struct udevice *bus,
211 struct i2c_msg *msg, int nmsgs)
213 struct versatile_i2c_priv *priv = dev_get_priv(bus);
216 for ( ; nmsgs > 0; nmsgs--, msg++) {
217 ret = versatile_i2c_message_xfer(priv, msg);
225 static int versatile_i2c_chip_probe(struct udevice *bus,
226 uint chip, uint chip_flags)
228 /* probe the presence of a slave by writing a 0-size message */
229 struct i2c_msg msg = { .addr = chip, .flags = chip_flags,
230 .len = 0, .buf = NULL };
231 struct versatile_i2c_priv *priv = dev_get_priv(bus);
233 return versatile_i2c_message_xfer(priv, &msg);
236 static int versatile_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
238 struct versatile_i2c_priv *priv = dev_get_priv(bus);
240 priv->delay = 1000000 / (speed << 2);
242 versatile_i2c_reset_bus(priv);
247 static int versatile_i2c_probe(struct udevice *dev)
249 struct versatile_i2c_priv *priv = dev_get_priv(dev);
251 priv->base = (phys_addr_t)dev_read_addr(dev);
252 priv->delay = 25; /* 25us * 4 = 100kHz */
254 * U-Boot still doesn't assign automatically
255 * sequence numbers to devices
262 static const struct dm_i2c_ops versatile_i2c_ops = {
263 .xfer = versatile_i2c_xfer,
264 .probe_chip = versatile_i2c_chip_probe,
265 .set_bus_speed = versatile_i2c_set_bus_speed,
268 static const struct udevice_id versatile_i2c_of_match[] = {
269 { .compatible = "arm,versatile-i2c" },
273 U_BOOT_DRIVER(versatile_i2c) = {
274 .name = "i2c-bus-versatile",
276 .of_match = versatile_i2c_of_match,
277 .probe = versatile_i2c_probe,
278 .priv_auto_alloc_size = sizeof(struct versatile_i2c_priv),
279 .ops = &versatile_i2c_ops,