1 // SPDX-License-Identifier: GPL-2.0+
6 * (C) Copyright 2000 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
9 * (C) Copyright 2003 Pengutronix e.K.
12 * (C) Copyright 2011 Marvell Inc.
15 * Back ported to the 8xx platform (from the 8260 platform) by
24 #include <linux/delay.h>
27 /* All transfers are described by this data structure */
35 #ifdef CONFIG_ARMADA_3700
36 /* Armada 3700 has no padding between the registers */
59 * Dummy implementation that can be overwritten by a board
62 __weak void i2c_clk_enable(void)
67 * i2c_reset: - reset the host controller
70 static void i2c_reset(struct mv_i2c *base)
74 /* Save bus mode (standard or fast speed) for later use */
75 icr_mode = readl(&base->icr) & ICR_MODE_MASK;
76 writel(readl(&base->icr) & ~ICR_IUE, &base->icr); /* disable unit */
77 writel(readl(&base->icr) | ICR_UR, &base->icr); /* reset the unit */
79 writel(readl(&base->icr) & ~ICR_IUE, &base->icr); /* disable unit */
83 writel(0x0, &base->isar); /* set our slave address */
84 /* set control reg values */
85 writel(I2C_ICR_INIT | icr_mode, &base->icr);
86 writel(I2C_ISR_INIT, &base->isr); /* set clear interrupt bits */
87 writel(readl(&base->icr) | ICR_IUE, &base->icr); /* enable unit */
92 * i2c_isr_set_cleared: - wait until certain bits of the I2C status register
95 * @return: 1 in case of success, 0 means timeout (no match within 10 ms).
97 static int i2c_isr_set_cleared(struct mv_i2c *base, unsigned long set_mask,
98 unsigned long cleared_mask)
100 int timeout = 1000, isr;
103 isr = readl(&base->isr);
107 } while (((isr & set_mask) != set_mask)
108 || ((isr & cleared_mask) != 0));
114 * i2c_transfer: - Transfer one byte over the i2c bus
116 * This function can tranfer a byte over the i2c bus in both directions.
117 * It is used by the public API functions.
119 * @return: 0: transfer successful
120 * -1: message is empty
121 * -2: transmit timeout
123 * -4: receive timeout
124 * -5: illegal parameters
125 * -6: bus is busy and couldn't be aquired
127 static int i2c_transfer(struct mv_i2c *base, struct mv_i2c_msg *msg)
132 goto transfer_error_msg_empty;
134 switch (msg->direction) {
136 /* check if bus is not busy */
137 if (!i2c_isr_set_cleared(base, 0, ISR_IBB))
138 goto transfer_error_bus_busy;
140 /* start transmission */
141 writel(readl(&base->icr) & ~ICR_START, &base->icr);
142 writel(readl(&base->icr) & ~ICR_STOP, &base->icr);
143 writel(msg->data, &base->idbr);
144 if (msg->condition == I2C_COND_START)
145 writel(readl(&base->icr) | ICR_START, &base->icr);
146 if (msg->condition == I2C_COND_STOP)
147 writel(readl(&base->icr) | ICR_STOP, &base->icr);
148 if (msg->acknack == I2C_ACKNAK_SENDNAK)
149 writel(readl(&base->icr) | ICR_ACKNAK, &base->icr);
150 if (msg->acknack == I2C_ACKNAK_SENDACK)
151 writel(readl(&base->icr) & ~ICR_ACKNAK, &base->icr);
152 writel(readl(&base->icr) & ~ICR_ALDIE, &base->icr);
153 writel(readl(&base->icr) | ICR_TB, &base->icr);
155 /* transmit register empty? */
156 if (!i2c_isr_set_cleared(base, ISR_ITE, 0))
157 goto transfer_error_transmit_timeout;
159 /* clear 'transmit empty' state */
160 writel(readl(&base->isr) | ISR_ITE, &base->isr);
162 /* wait for ACK from slave */
163 if (msg->acknack == I2C_ACKNAK_WAITACK)
164 if (!i2c_isr_set_cleared(base, 0, ISR_ACKNAK))
165 goto transfer_error_ack_missing;
170 /* check if bus is not busy */
171 if (!i2c_isr_set_cleared(base, 0, ISR_IBB))
172 goto transfer_error_bus_busy;
175 writel(readl(&base->icr) & ~ICR_START, &base->icr);
176 writel(readl(&base->icr) & ~ICR_STOP, &base->icr);
177 if (msg->condition == I2C_COND_START)
178 writel(readl(&base->icr) | ICR_START, &base->icr);
179 if (msg->condition == I2C_COND_STOP)
180 writel(readl(&base->icr) | ICR_STOP, &base->icr);
181 if (msg->acknack == I2C_ACKNAK_SENDNAK)
182 writel(readl(&base->icr) | ICR_ACKNAK, &base->icr);
183 if (msg->acknack == I2C_ACKNAK_SENDACK)
184 writel(readl(&base->icr) & ~ICR_ACKNAK, &base->icr);
185 writel(readl(&base->icr) & ~ICR_ALDIE, &base->icr);
186 writel(readl(&base->icr) | ICR_TB, &base->icr);
188 /* receive register full? */
189 if (!i2c_isr_set_cleared(base, ISR_IRF, 0))
190 goto transfer_error_receive_timeout;
192 msg->data = readl(&base->idbr);
194 /* clear 'receive empty' state */
195 writel(readl(&base->isr) | ISR_IRF, &base->isr);
198 goto transfer_error_illegal_param;
203 transfer_error_msg_empty:
204 debug("i2c_transfer: error: 'msg' is empty\n");
206 goto i2c_transfer_finish;
208 transfer_error_transmit_timeout:
209 debug("i2c_transfer: error: transmit timeout\n");
211 goto i2c_transfer_finish;
213 transfer_error_ack_missing:
214 debug("i2c_transfer: error: ACK missing\n");
216 goto i2c_transfer_finish;
218 transfer_error_receive_timeout:
219 debug("i2c_transfer: error: receive timeout\n");
221 goto i2c_transfer_finish;
223 transfer_error_illegal_param:
224 debug("i2c_transfer: error: illegal parameters\n");
226 goto i2c_transfer_finish;
228 transfer_error_bus_busy:
229 debug("i2c_transfer: error: bus is busy\n");
231 goto i2c_transfer_finish;
234 debug("i2c_transfer: ISR: 0x%04x\n", readl(&base->isr));
239 static int __i2c_read(struct mv_i2c *base, uchar chip, u8 *addr, int alen,
240 uchar *buffer, int len)
242 struct mv_i2c_msg msg;
244 debug("i2c_read(chip=0x%02x, addr=0x%02x, alen=0x%02x, "
245 "len=0x%02x)\n", chip, *addr, alen, len);
248 printf("reading zero byte is invalid\n");
254 /* dummy chip address write */
255 debug("i2c_read: dummy chip address write\n");
256 msg.condition = I2C_COND_START;
257 msg.acknack = I2C_ACKNAK_WAITACK;
258 msg.direction = I2C_WRITE;
259 msg.data = (chip << 1);
261 if (i2c_transfer(base, &msg))
265 * send memory address bytes;
266 * alen defines how much bytes we have to send.
268 while (--alen >= 0) {
269 debug("i2c_read: send address byte %02x (alen=%d)\n",
271 msg.condition = I2C_COND_NORMAL;
272 msg.acknack = I2C_ACKNAK_WAITACK;
273 msg.direction = I2C_WRITE;
274 msg.data = addr[alen];
275 if (i2c_transfer(base, &msg))
279 /* start read sequence */
280 debug("i2c_read: start read sequence\n");
281 msg.condition = I2C_COND_START;
282 msg.acknack = I2C_ACKNAK_WAITACK;
283 msg.direction = I2C_WRITE;
284 msg.data = (chip << 1);
286 if (i2c_transfer(base, &msg))
289 /* read bytes; send NACK at last byte */
292 msg.condition = I2C_COND_STOP;
293 msg.acknack = I2C_ACKNAK_SENDNAK;
295 msg.condition = I2C_COND_NORMAL;
296 msg.acknack = I2C_ACKNAK_SENDACK;
299 msg.direction = I2C_READ;
301 if (i2c_transfer(base, &msg))
305 debug("i2c_read: reading byte (%p)=0x%02x\n",
315 static int __i2c_write(struct mv_i2c *base, uchar chip, u8 *addr, int alen,
316 uchar *buffer, int len)
318 struct mv_i2c_msg msg;
320 debug("i2c_write(chip=0x%02x, addr=0x%02x, alen=0x%02x, "
321 "len=0x%02x)\n", chip, *addr, alen, len);
325 /* chip address write */
326 debug("i2c_write: chip address write\n");
327 msg.condition = I2C_COND_START;
328 msg.acknack = I2C_ACKNAK_WAITACK;
329 msg.direction = I2C_WRITE;
330 msg.data = (chip << 1);
332 if (i2c_transfer(base, &msg))
336 * send memory address bytes;
337 * alen defines how much bytes we have to send.
339 while (--alen >= 0) {
340 debug("i2c_read: send address byte %02x (alen=%d)\n",
342 msg.condition = I2C_COND_NORMAL;
343 msg.acknack = I2C_ACKNAK_WAITACK;
344 msg.direction = I2C_WRITE;
345 msg.data = addr[alen];
346 if (i2c_transfer(base, &msg))
350 /* write bytes; send NACK at last byte */
352 debug("i2c_write: writing byte (%p)=0x%02x\n",
356 msg.condition = I2C_COND_STOP;
358 msg.condition = I2C_COND_NORMAL;
360 msg.acknack = I2C_ACKNAK_WAITACK;
361 msg.direction = I2C_WRITE;
362 msg.data = *(buffer++);
364 if (i2c_transfer(base, &msg))
373 #if !CONFIG_IS_ENABLED(DM_I2C)
375 static struct mv_i2c *base_glob;
378 void i2c_init(int speed, int slaveaddr)
382 base_glob = (struct mv_i2c *)CONFIG_MV_I2C_REG;
384 if (speed > I2C_SPEED_STANDARD_RATE)
388 clrsetbits_le32(&base_glob->icr, ICR_MODE_MASK, val);
391 static int __i2c_probe_chip(struct mv_i2c *base, uchar chip)
393 struct mv_i2c_msg msg;
397 msg.condition = I2C_COND_START;
398 msg.acknack = I2C_ACKNAK_WAITACK;
399 msg.direction = I2C_WRITE;
400 msg.data = (chip << 1) + 1;
401 if (i2c_transfer(base, &msg))
404 msg.condition = I2C_COND_STOP;
405 msg.acknack = I2C_ACKNAK_SENDNAK;
406 msg.direction = I2C_READ;
408 if (i2c_transfer(base, &msg))
415 * i2c_probe: - Test if a chip answers for a given i2c address
417 * @chip: address of the chip which is searched for
418 * @return: 0 if a chip was found, -1 otherwhise
420 int i2c_probe(uchar chip)
422 return __i2c_probe_chip(base_glob, chip);
426 * i2c_read: - Read multiple bytes from an i2c device
428 * The higher level routines take into account that this function is only
429 * called with len < page length of the device (see configuration file)
431 * @chip: address of the chip which is to be read
432 * @addr: i2c data address within the chip
433 * @alen: length of the i2c data address (1..2 bytes)
434 * @buffer: where to write the data
435 * @len: how much byte do we want to read
436 * @return: 0 in case of success
438 int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
442 addr_bytes[0] = (addr >> 0) & 0xFF;
443 addr_bytes[1] = (addr >> 8) & 0xFF;
444 addr_bytes[2] = (addr >> 16) & 0xFF;
445 addr_bytes[3] = (addr >> 24) & 0xFF;
447 return __i2c_read(base_glob, chip, addr_bytes, alen, buffer, len);
451 * i2c_write: - Write multiple bytes to an i2c device
453 * The higher level routines take into account that this function is only
454 * called with len < page length of the device (see configuration file)
456 * @chip: address of the chip which is to be written
457 * @addr: i2c data address within the chip
458 * @alen: length of the i2c data address (1..2 bytes)
459 * @buffer: where to find the data to be written
460 * @len: how much byte do we want to read
461 * @return: 0 in case of success
463 int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
467 addr_bytes[0] = (addr >> 0) & 0xFF;
468 addr_bytes[1] = (addr >> 8) & 0xFF;
469 addr_bytes[2] = (addr >> 16) & 0xFF;
470 addr_bytes[3] = (addr >> 24) & 0xFF;
472 return __i2c_write(base_glob, chip, addr_bytes, alen, buffer, len);
475 #else /* CONFIG_DM_I2C */
481 static int mv_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
483 struct mv_i2c_priv *i2c = dev_get_priv(bus);
484 struct i2c_msg *dmsg, *omsg, dummy;
486 memset(&dummy, 0, sizeof(struct i2c_msg));
489 * We expect either two messages (one with an offset and one with the
490 * actual data) or one message (just data or offset/data combined)
492 if (nmsgs > 2 || nmsgs == 0) {
493 debug("%s: Only one or two messages are supported.", __func__);
497 omsg = nmsgs == 1 ? &dummy : msg;
498 dmsg = nmsgs == 1 ? msg : msg + 1;
500 if (dmsg->flags & I2C_M_RD)
501 return __i2c_read(i2c->base, dmsg->addr, omsg->buf,
502 omsg->len, dmsg->buf, dmsg->len);
504 return __i2c_write(i2c->base, dmsg->addr, omsg->buf,
505 omsg->len, dmsg->buf, dmsg->len);
508 static int mv_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
510 struct mv_i2c_priv *priv = dev_get_priv(bus);
513 if (speed > I2C_SPEED_STANDARD_RATE)
517 clrsetbits_le32(&priv->base->icr, ICR_MODE_MASK, val);
522 static int mv_i2c_probe(struct udevice *bus)
524 struct mv_i2c_priv *priv = dev_get_priv(bus);
526 priv->base = dev_read_addr_ptr(bus);
531 static const struct dm_i2c_ops mv_i2c_ops = {
533 .set_bus_speed = mv_i2c_set_bus_speed,
536 static const struct udevice_id mv_i2c_ids[] = {
537 { .compatible = "marvell,armada-3700-i2c" },
541 U_BOOT_DRIVER(i2c_mv) = {
544 .of_match = mv_i2c_ids,
545 .probe = mv_i2c_probe,
546 .priv_auto = sizeof(struct mv_i2c_priv),
549 #endif /* CONFIG_DM_I2C */