Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2000 |
| 4 | * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it |
| 5 | * |
| 6 | * (C) Copyright 2000 Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 7 | * Marius Groeger <mgroeger@sysgo.de> |
| 8 | * |
| 9 | * (C) Copyright 2003 Pengutronix e.K. |
| 10 | * Robert Schwebel <r.schwebel@pengutronix.de> |
| 11 | * |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 12 | * (C) Copyright 2011 Marvell Inc. |
| 13 | * Lei Wen <leiwen@marvell.com> |
| 14 | * |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 15 | * Back ported to the 8xx platform (from the 8260 platform) by |
| 16 | * Murray.Jensen@cmst.csiro.au, 27-Jan-01. |
| 17 | */ |
| 18 | |
Stefan Roese | 0c0f719 | 2016-09-16 15:07:52 +0200 | [diff] [blame] | 19 | #include <dm.h> |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 20 | #include <i2c.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 21 | #include <log.h> |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 22 | #include <asm/io.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 23 | #include <linux/delay.h> |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 24 | #include "mv_i2c.h" |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 25 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 26 | /* All transfers are described by this data structure */ |
Simon Glass | fffff72 | 2015-02-05 21:41:33 -0700 | [diff] [blame] | 27 | struct mv_i2c_msg { |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 28 | u8 condition; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 29 | u8 acknack; |
| 30 | u8 direction; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 31 | u8 data; |
| 32 | }; |
| 33 | |
Stefan Roese | 0c0f719 | 2016-09-16 15:07:52 +0200 | [diff] [blame] | 34 | #ifdef CONFIG_ARMADA_3700 |
| 35 | /* Armada 3700 has no padding between the registers */ |
| 36 | struct mv_i2c { |
| 37 | u32 ibmr; |
| 38 | u32 idbr; |
| 39 | u32 icr; |
| 40 | u32 isr; |
| 41 | u32 isar; |
| 42 | }; |
| 43 | #else |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 44 | struct mv_i2c { |
| 45 | u32 ibmr; |
| 46 | u32 pad0; |
| 47 | u32 idbr; |
| 48 | u32 pad1; |
| 49 | u32 icr; |
| 50 | u32 pad2; |
| 51 | u32 isr; |
| 52 | u32 pad3; |
| 53 | u32 isar; |
| 54 | }; |
Stefan Roese | 0c0f719 | 2016-09-16 15:07:52 +0200 | [diff] [blame] | 55 | #endif |
| 56 | |
| 57 | /* |
| 58 | * Dummy implementation that can be overwritten by a board |
| 59 | * specific function |
| 60 | */ |
| 61 | __weak void i2c_clk_enable(void) |
| 62 | { |
| 63 | } |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 64 | |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 65 | /* |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 66 | * i2c_reset: - reset the host controller |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 67 | * |
| 68 | */ |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 69 | static void i2c_reset(struct mv_i2c *base) |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 70 | { |
Stefan Roese | 9ad5a00 | 2016-09-16 15:07:53 +0200 | [diff] [blame] | 71 | u32 icr_mode; |
| 72 | |
| 73 | /* Save bus mode (standard or fast speed) for later use */ |
| 74 | icr_mode = readl(&base->icr) & ICR_MODE_MASK; |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 75 | writel(readl(&base->icr) & ~ICR_IUE, &base->icr); /* disable unit */ |
| 76 | writel(readl(&base->icr) | ICR_UR, &base->icr); /* reset the unit */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 77 | udelay(100); |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 78 | writel(readl(&base->icr) & ~ICR_IUE, &base->icr); /* disable unit */ |
| 79 | |
| 80 | i2c_clk_enable(); |
| 81 | |
Tom Rini | a5752f8 | 2021-08-18 23:12:32 -0400 | [diff] [blame] | 82 | writel(0x0, &base->isar); /* set our slave address */ |
Stefan Roese | 9ad5a00 | 2016-09-16 15:07:53 +0200 | [diff] [blame] | 83 | /* set control reg values */ |
| 84 | writel(I2C_ICR_INIT | icr_mode, &base->icr); |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 85 | writel(I2C_ISR_INIT, &base->isr); /* set clear interrupt bits */ |
| 86 | writel(readl(&base->icr) | ICR_IUE, &base->icr); /* enable unit */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 87 | udelay(100); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 90 | /* |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 91 | * i2c_isr_set_cleared: - wait until certain bits of the I2C status register |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 92 | * are set and cleared |
| 93 | * |
Markus Klotzbuecher | ba70d6a | 2006-03-24 12:23:27 +0100 | [diff] [blame] | 94 | * @return: 1 in case of success, 0 means timeout (no match within 10 ms). |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 95 | */ |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 96 | static int i2c_isr_set_cleared(struct mv_i2c *base, unsigned long set_mask, |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 97 | unsigned long cleared_mask) |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 98 | { |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 99 | int timeout = 1000, isr; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 100 | |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 101 | do { |
| 102 | isr = readl(&base->isr); |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 103 | udelay(10); |
| 104 | if (timeout-- < 0) |
| 105 | return 0; |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 106 | } while (((isr & set_mask) != set_mask) |
| 107 | || ((isr & cleared_mask) != 0)); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 108 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 109 | return 1; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 112 | /* |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 113 | * i2c_transfer: - Transfer one byte over the i2c bus |
| 114 | * |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 115 | * This function can tranfer a byte over the i2c bus in both directions. |
| 116 | * It is used by the public API functions. |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 117 | * |
| 118 | * @return: 0: transfer successful |
| 119 | * -1: message is empty |
| 120 | * -2: transmit timeout |
| 121 | * -3: ACK missing |
| 122 | * -4: receive timeout |
| 123 | * -5: illegal parameters |
| 124 | * -6: bus is busy and couldn't be aquired |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 125 | */ |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 126 | static int i2c_transfer(struct mv_i2c *base, struct mv_i2c_msg *msg) |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 127 | { |
| 128 | int ret; |
| 129 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 130 | if (!msg) |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 131 | goto transfer_error_msg_empty; |
| 132 | |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 133 | switch (msg->direction) { |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 134 | case I2C_WRITE: |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 135 | /* check if bus is not busy */ |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 136 | if (!i2c_isr_set_cleared(base, 0, ISR_IBB)) |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 137 | goto transfer_error_bus_busy; |
| 138 | |
| 139 | /* start transmission */ |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 140 | writel(readl(&base->icr) & ~ICR_START, &base->icr); |
| 141 | writel(readl(&base->icr) & ~ICR_STOP, &base->icr); |
| 142 | writel(msg->data, &base->idbr); |
Marek Vasut | 3ba8bf7 | 2010-09-09 09:50:39 +0200 | [diff] [blame] | 143 | if (msg->condition == I2C_COND_START) |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 144 | writel(readl(&base->icr) | ICR_START, &base->icr); |
Marek Vasut | 3ba8bf7 | 2010-09-09 09:50:39 +0200 | [diff] [blame] | 145 | if (msg->condition == I2C_COND_STOP) |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 146 | writel(readl(&base->icr) | ICR_STOP, &base->icr); |
Marek Vasut | 3ba8bf7 | 2010-09-09 09:50:39 +0200 | [diff] [blame] | 147 | if (msg->acknack == I2C_ACKNAK_SENDNAK) |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 148 | writel(readl(&base->icr) | ICR_ACKNAK, &base->icr); |
Marek Vasut | 3ba8bf7 | 2010-09-09 09:50:39 +0200 | [diff] [blame] | 149 | if (msg->acknack == I2C_ACKNAK_SENDACK) |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 150 | writel(readl(&base->icr) & ~ICR_ACKNAK, &base->icr); |
| 151 | writel(readl(&base->icr) & ~ICR_ALDIE, &base->icr); |
| 152 | writel(readl(&base->icr) | ICR_TB, &base->icr); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 153 | |
| 154 | /* transmit register empty? */ |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 155 | if (!i2c_isr_set_cleared(base, ISR_ITE, 0)) |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 156 | goto transfer_error_transmit_timeout; |
| 157 | |
| 158 | /* clear 'transmit empty' state */ |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 159 | writel(readl(&base->isr) | ISR_ITE, &base->isr); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 160 | |
| 161 | /* wait for ACK from slave */ |
| 162 | if (msg->acknack == I2C_ACKNAK_WAITACK) |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 163 | if (!i2c_isr_set_cleared(base, 0, ISR_ACKNAK)) |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 164 | goto transfer_error_ack_missing; |
| 165 | break; |
| 166 | |
| 167 | case I2C_READ: |
| 168 | |
| 169 | /* check if bus is not busy */ |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 170 | if (!i2c_isr_set_cleared(base, 0, ISR_IBB)) |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 171 | goto transfer_error_bus_busy; |
| 172 | |
| 173 | /* start receive */ |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 174 | writel(readl(&base->icr) & ~ICR_START, &base->icr); |
| 175 | writel(readl(&base->icr) & ~ICR_STOP, &base->icr); |
Marek Vasut | 3ba8bf7 | 2010-09-09 09:50:39 +0200 | [diff] [blame] | 176 | if (msg->condition == I2C_COND_START) |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 177 | writel(readl(&base->icr) | ICR_START, &base->icr); |
Marek Vasut | 3ba8bf7 | 2010-09-09 09:50:39 +0200 | [diff] [blame] | 178 | if (msg->condition == I2C_COND_STOP) |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 179 | writel(readl(&base->icr) | ICR_STOP, &base->icr); |
Marek Vasut | 3ba8bf7 | 2010-09-09 09:50:39 +0200 | [diff] [blame] | 180 | if (msg->acknack == I2C_ACKNAK_SENDNAK) |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 181 | writel(readl(&base->icr) | ICR_ACKNAK, &base->icr); |
Marek Vasut | 3ba8bf7 | 2010-09-09 09:50:39 +0200 | [diff] [blame] | 182 | if (msg->acknack == I2C_ACKNAK_SENDACK) |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 183 | writel(readl(&base->icr) & ~ICR_ACKNAK, &base->icr); |
| 184 | writel(readl(&base->icr) & ~ICR_ALDIE, &base->icr); |
| 185 | writel(readl(&base->icr) | ICR_TB, &base->icr); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 186 | |
| 187 | /* receive register full? */ |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 188 | if (!i2c_isr_set_cleared(base, ISR_IRF, 0)) |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 189 | goto transfer_error_receive_timeout; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 190 | |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 191 | msg->data = readl(&base->idbr); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 192 | |
| 193 | /* clear 'receive empty' state */ |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 194 | writel(readl(&base->isr) | ISR_IRF, &base->isr); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 195 | break; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 196 | default: |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 197 | goto transfer_error_illegal_param; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 198 | } |
| 199 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 200 | return 0; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 201 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 202 | transfer_error_msg_empty: |
Stefan Roese | 8eff909 | 2016-09-16 15:07:49 +0200 | [diff] [blame] | 203 | debug("i2c_transfer: error: 'msg' is empty\n"); |
| 204 | ret = -1; |
| 205 | goto i2c_transfer_finish; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 206 | |
| 207 | transfer_error_transmit_timeout: |
Stefan Roese | 8eff909 | 2016-09-16 15:07:49 +0200 | [diff] [blame] | 208 | debug("i2c_transfer: error: transmit timeout\n"); |
| 209 | ret = -2; |
| 210 | goto i2c_transfer_finish; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 211 | |
| 212 | transfer_error_ack_missing: |
Stefan Roese | 8eff909 | 2016-09-16 15:07:49 +0200 | [diff] [blame] | 213 | debug("i2c_transfer: error: ACK missing\n"); |
| 214 | ret = -3; |
| 215 | goto i2c_transfer_finish; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 216 | |
| 217 | transfer_error_receive_timeout: |
Stefan Roese | 8eff909 | 2016-09-16 15:07:49 +0200 | [diff] [blame] | 218 | debug("i2c_transfer: error: receive timeout\n"); |
| 219 | ret = -4; |
| 220 | goto i2c_transfer_finish; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 221 | |
| 222 | transfer_error_illegal_param: |
Stefan Roese | 8eff909 | 2016-09-16 15:07:49 +0200 | [diff] [blame] | 223 | debug("i2c_transfer: error: illegal parameters\n"); |
| 224 | ret = -5; |
| 225 | goto i2c_transfer_finish; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 226 | |
| 227 | transfer_error_bus_busy: |
Stefan Roese | 8eff909 | 2016-09-16 15:07:49 +0200 | [diff] [blame] | 228 | debug("i2c_transfer: error: bus is busy\n"); |
| 229 | ret = -6; |
| 230 | goto i2c_transfer_finish; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 231 | |
| 232 | i2c_transfer_finish: |
Stefan Roese | 8eff909 | 2016-09-16 15:07:49 +0200 | [diff] [blame] | 233 | debug("i2c_transfer: ISR: 0x%04x\n", readl(&base->isr)); |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 234 | i2c_reset(base); |
Stefan Roese | 8eff909 | 2016-09-16 15:07:49 +0200 | [diff] [blame] | 235 | return ret; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Stefan Roese | 0c0f719 | 2016-09-16 15:07:52 +0200 | [diff] [blame] | 238 | static int __i2c_read(struct mv_i2c *base, uchar chip, u8 *addr, int alen, |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 239 | uchar *buffer, int len) |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 240 | { |
Simon Glass | fffff72 | 2015-02-05 21:41:33 -0700 | [diff] [blame] | 241 | struct mv_i2c_msg msg; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 242 | |
Stefan Roese | 8eff909 | 2016-09-16 15:07:49 +0200 | [diff] [blame] | 243 | debug("i2c_read(chip=0x%02x, addr=0x%02x, alen=0x%02x, " |
Stefan Roese | 0c0f719 | 2016-09-16 15:07:52 +0200 | [diff] [blame] | 244 | "len=0x%02x)\n", chip, *addr, alen, len); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 245 | |
jinghua | 85f03f0 | 2016-09-16 15:07:54 +0200 | [diff] [blame] | 246 | if (len == 0) { |
| 247 | printf("reading zero byte is invalid\n"); |
| 248 | return -EINVAL; |
| 249 | } |
| 250 | |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 251 | i2c_reset(base); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 252 | |
| 253 | /* dummy chip address write */ |
Stefan Roese | 8eff909 | 2016-09-16 15:07:49 +0200 | [diff] [blame] | 254 | debug("i2c_read: dummy chip address write\n"); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 255 | msg.condition = I2C_COND_START; |
| 256 | msg.acknack = I2C_ACKNAK_WAITACK; |
| 257 | msg.direction = I2C_WRITE; |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 258 | msg.data = (chip << 1); |
| 259 | msg.data &= 0xFE; |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 260 | if (i2c_transfer(base, &msg)) |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 261 | return -1; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 262 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 263 | /* |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 264 | * send memory address bytes; |
| 265 | * alen defines how much bytes we have to send. |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 266 | */ |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 267 | while (--alen >= 0) { |
Stefan Roese | 0c0f719 | 2016-09-16 15:07:52 +0200 | [diff] [blame] | 268 | debug("i2c_read: send address byte %02x (alen=%d)\n", |
| 269 | *addr, alen); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 270 | msg.condition = I2C_COND_NORMAL; |
| 271 | msg.acknack = I2C_ACKNAK_WAITACK; |
| 272 | msg.direction = I2C_WRITE; |
Bradley Bolen | 7746626 | 2016-12-13 12:49:53 -0500 | [diff] [blame] | 273 | msg.data = addr[alen]; |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 274 | if (i2c_transfer(base, &msg)) |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 275 | return -1; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 276 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 277 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 278 | /* start read sequence */ |
Stefan Roese | 8eff909 | 2016-09-16 15:07:49 +0200 | [diff] [blame] | 279 | debug("i2c_read: start read sequence\n"); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 280 | msg.condition = I2C_COND_START; |
| 281 | msg.acknack = I2C_ACKNAK_WAITACK; |
| 282 | msg.direction = I2C_WRITE; |
| 283 | msg.data = (chip << 1); |
| 284 | msg.data |= 0x01; |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 285 | if (i2c_transfer(base, &msg)) |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 286 | return -1; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 287 | |
| 288 | /* read bytes; send NACK at last byte */ |
| 289 | while (len--) { |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 290 | if (len == 0) { |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 291 | msg.condition = I2C_COND_STOP; |
| 292 | msg.acknack = I2C_ACKNAK_SENDNAK; |
| 293 | } else { |
| 294 | msg.condition = I2C_COND_NORMAL; |
| 295 | msg.acknack = I2C_ACKNAK_SENDACK; |
| 296 | } |
| 297 | |
| 298 | msg.direction = I2C_READ; |
| 299 | msg.data = 0x00; |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 300 | if (i2c_transfer(base, &msg)) |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 301 | return -1; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 302 | |
Markus Klotzbuecher | ba70d6a | 2006-03-24 12:23:27 +0100 | [diff] [blame] | 303 | *buffer = msg.data; |
Stefan Roese | 0c0f719 | 2016-09-16 15:07:52 +0200 | [diff] [blame] | 304 | debug("i2c_read: reading byte (%p)=0x%02x\n", |
| 305 | buffer, *buffer); |
Markus Klotzbuecher | ba70d6a | 2006-03-24 12:23:27 +0100 | [diff] [blame] | 306 | buffer++; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 309 | i2c_reset(base); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
Stefan Roese | 0c0f719 | 2016-09-16 15:07:52 +0200 | [diff] [blame] | 314 | static int __i2c_write(struct mv_i2c *base, uchar chip, u8 *addr, int alen, |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 315 | uchar *buffer, int len) |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 316 | { |
Simon Glass | fffff72 | 2015-02-05 21:41:33 -0700 | [diff] [blame] | 317 | struct mv_i2c_msg msg; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 318 | |
Stefan Roese | 8eff909 | 2016-09-16 15:07:49 +0200 | [diff] [blame] | 319 | debug("i2c_write(chip=0x%02x, addr=0x%02x, alen=0x%02x, " |
Stefan Roese | 0c0f719 | 2016-09-16 15:07:52 +0200 | [diff] [blame] | 320 | "len=0x%02x)\n", chip, *addr, alen, len); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 321 | |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 322 | i2c_reset(base); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 323 | |
| 324 | /* chip address write */ |
Stefan Roese | 8eff909 | 2016-09-16 15:07:49 +0200 | [diff] [blame] | 325 | debug("i2c_write: chip address write\n"); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 326 | msg.condition = I2C_COND_START; |
| 327 | msg.acknack = I2C_ACKNAK_WAITACK; |
| 328 | msg.direction = I2C_WRITE; |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 329 | msg.data = (chip << 1); |
| 330 | msg.data &= 0xFE; |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 331 | if (i2c_transfer(base, &msg)) |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 332 | return -1; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 333 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 334 | /* |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 335 | * send memory address bytes; |
| 336 | * alen defines how much bytes we have to send. |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 337 | */ |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 338 | while (--alen >= 0) { |
Stefan Roese | 0c0f719 | 2016-09-16 15:07:52 +0200 | [diff] [blame] | 339 | debug("i2c_read: send address byte %02x (alen=%d)\n", |
| 340 | *addr, alen); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 341 | msg.condition = I2C_COND_NORMAL; |
| 342 | msg.acknack = I2C_ACKNAK_WAITACK; |
| 343 | msg.direction = I2C_WRITE; |
Bradley Bolen | 7746626 | 2016-12-13 12:49:53 -0500 | [diff] [blame] | 344 | msg.data = addr[alen]; |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 345 | if (i2c_transfer(base, &msg)) |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 346 | return -1; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 347 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 348 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 349 | /* write bytes; send NACK at last byte */ |
| 350 | while (len--) { |
Stefan Roese | 0c0f719 | 2016-09-16 15:07:52 +0200 | [diff] [blame] | 351 | debug("i2c_write: writing byte (%p)=0x%02x\n", |
| 352 | buffer, *buffer); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 353 | |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 354 | if (len == 0) |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 355 | msg.condition = I2C_COND_STOP; |
| 356 | else |
| 357 | msg.condition = I2C_COND_NORMAL; |
| 358 | |
| 359 | msg.acknack = I2C_ACKNAK_WAITACK; |
| 360 | msg.direction = I2C_WRITE; |
| 361 | msg.data = *(buffer++); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 362 | |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 363 | if (i2c_transfer(base, &msg)) |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 364 | return -1; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 367 | i2c_reset(base); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 368 | |
| 369 | return 0; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 370 | } |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 371 | |
Igor Opaniuk | 2147a16 | 2021-02-09 13:52:45 +0200 | [diff] [blame] | 372 | #if !CONFIG_IS_ENABLED(DM_I2C) |
Stefan Roese | 0c0f719 | 2016-09-16 15:07:52 +0200 | [diff] [blame] | 373 | |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 374 | static struct mv_i2c *base_glob; |
| 375 | |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 376 | /* API Functions */ |
| 377 | void i2c_init(int speed, int slaveaddr) |
| 378 | { |
Stefan Roese | 9ad5a00 | 2016-09-16 15:07:53 +0200 | [diff] [blame] | 379 | u32 val; |
| 380 | |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 381 | base_glob = (struct mv_i2c *)CONFIG_MV_I2C_REG; |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 382 | |
Simon Glass | f3d4615 | 2020-01-23 11:48:22 -0700 | [diff] [blame] | 383 | if (speed > I2C_SPEED_STANDARD_RATE) |
Stefan Roese | 9ad5a00 | 2016-09-16 15:07:53 +0200 | [diff] [blame] | 384 | val = ICR_FM; |
| 385 | else |
| 386 | val = ICR_SM; |
| 387 | clrsetbits_le32(&base_glob->icr, ICR_MODE_MASK, val); |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 388 | } |
| 389 | |
Stefan Roese | 0c0f719 | 2016-09-16 15:07:52 +0200 | [diff] [blame] | 390 | static int __i2c_probe_chip(struct mv_i2c *base, uchar chip) |
| 391 | { |
| 392 | struct mv_i2c_msg msg; |
| 393 | |
| 394 | i2c_reset(base); |
| 395 | |
| 396 | msg.condition = I2C_COND_START; |
| 397 | msg.acknack = I2C_ACKNAK_WAITACK; |
| 398 | msg.direction = I2C_WRITE; |
| 399 | msg.data = (chip << 1) + 1; |
| 400 | if (i2c_transfer(base, &msg)) |
| 401 | return -1; |
| 402 | |
| 403 | msg.condition = I2C_COND_STOP; |
| 404 | msg.acknack = I2C_ACKNAK_SENDNAK; |
| 405 | msg.direction = I2C_READ; |
| 406 | msg.data = 0x00; |
| 407 | if (i2c_transfer(base, &msg)) |
| 408 | return -1; |
| 409 | |
| 410 | return 0; |
| 411 | } |
| 412 | |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 413 | /* |
| 414 | * i2c_probe: - Test if a chip answers for a given i2c address |
| 415 | * |
| 416 | * @chip: address of the chip which is searched for |
| 417 | * @return: 0 if a chip was found, -1 otherwhise |
| 418 | */ |
| 419 | int i2c_probe(uchar chip) |
| 420 | { |
| 421 | return __i2c_probe_chip(base_glob, chip); |
| 422 | } |
| 423 | |
| 424 | /* |
| 425 | * i2c_read: - Read multiple bytes from an i2c device |
| 426 | * |
| 427 | * The higher level routines take into account that this function is only |
| 428 | * called with len < page length of the device (see configuration file) |
| 429 | * |
| 430 | * @chip: address of the chip which is to be read |
| 431 | * @addr: i2c data address within the chip |
| 432 | * @alen: length of the i2c data address (1..2 bytes) |
| 433 | * @buffer: where to write the data |
| 434 | * @len: how much byte do we want to read |
| 435 | * @return: 0 in case of success |
| 436 | */ |
| 437 | int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len) |
| 438 | { |
Stefan Roese | 0c0f719 | 2016-09-16 15:07:52 +0200 | [diff] [blame] | 439 | u8 addr_bytes[4]; |
| 440 | |
| 441 | addr_bytes[0] = (addr >> 0) & 0xFF; |
| 442 | addr_bytes[1] = (addr >> 8) & 0xFF; |
| 443 | addr_bytes[2] = (addr >> 16) & 0xFF; |
| 444 | addr_bytes[3] = (addr >> 24) & 0xFF; |
| 445 | |
| 446 | return __i2c_read(base_glob, chip, addr_bytes, alen, buffer, len); |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /* |
| 450 | * i2c_write: - Write multiple bytes to an i2c device |
| 451 | * |
| 452 | * The higher level routines take into account that this function is only |
| 453 | * called with len < page length of the device (see configuration file) |
| 454 | * |
| 455 | * @chip: address of the chip which is to be written |
| 456 | * @addr: i2c data address within the chip |
| 457 | * @alen: length of the i2c data address (1..2 bytes) |
| 458 | * @buffer: where to find the data to be written |
| 459 | * @len: how much byte do we want to read |
| 460 | * @return: 0 in case of success |
| 461 | */ |
| 462 | int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len) |
| 463 | { |
Stefan Roese | 0c0f719 | 2016-09-16 15:07:52 +0200 | [diff] [blame] | 464 | u8 addr_bytes[4]; |
| 465 | |
| 466 | addr_bytes[0] = (addr >> 0) & 0xFF; |
| 467 | addr_bytes[1] = (addr >> 8) & 0xFF; |
| 468 | addr_bytes[2] = (addr >> 16) & 0xFF; |
| 469 | addr_bytes[3] = (addr >> 24) & 0xFF; |
| 470 | |
| 471 | return __i2c_write(base_glob, chip, addr_bytes, alen, buffer, len); |
Stefan Roese | 7b46ee5 | 2016-09-16 15:07:51 +0200 | [diff] [blame] | 472 | } |
Stefan Roese | 0c0f719 | 2016-09-16 15:07:52 +0200 | [diff] [blame] | 473 | |
| 474 | #else /* CONFIG_DM_I2C */ |
| 475 | |
| 476 | struct mv_i2c_priv { |
| 477 | struct mv_i2c *base; |
| 478 | }; |
| 479 | |
| 480 | static int mv_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs) |
| 481 | { |
| 482 | struct mv_i2c_priv *i2c = dev_get_priv(bus); |
| 483 | struct i2c_msg *dmsg, *omsg, dummy; |
| 484 | |
| 485 | memset(&dummy, 0, sizeof(struct i2c_msg)); |
| 486 | |
| 487 | /* |
| 488 | * We expect either two messages (one with an offset and one with the |
| 489 | * actual data) or one message (just data or offset/data combined) |
| 490 | */ |
| 491 | if (nmsgs > 2 || nmsgs == 0) { |
| 492 | debug("%s: Only one or two messages are supported.", __func__); |
| 493 | return -1; |
| 494 | } |
| 495 | |
| 496 | omsg = nmsgs == 1 ? &dummy : msg; |
| 497 | dmsg = nmsgs == 1 ? msg : msg + 1; |
| 498 | |
| 499 | if (dmsg->flags & I2C_M_RD) |
| 500 | return __i2c_read(i2c->base, dmsg->addr, omsg->buf, |
| 501 | omsg->len, dmsg->buf, dmsg->len); |
| 502 | else |
| 503 | return __i2c_write(i2c->base, dmsg->addr, omsg->buf, |
| 504 | omsg->len, dmsg->buf, dmsg->len); |
| 505 | } |
| 506 | |
Stefan Roese | 9ad5a00 | 2016-09-16 15:07:53 +0200 | [diff] [blame] | 507 | static int mv_i2c_set_bus_speed(struct udevice *bus, unsigned int speed) |
| 508 | { |
| 509 | struct mv_i2c_priv *priv = dev_get_priv(bus); |
| 510 | u32 val; |
| 511 | |
Simon Glass | f3d4615 | 2020-01-23 11:48:22 -0700 | [diff] [blame] | 512 | if (speed > I2C_SPEED_STANDARD_RATE) |
Stefan Roese | 9ad5a00 | 2016-09-16 15:07:53 +0200 | [diff] [blame] | 513 | val = ICR_FM; |
| 514 | else |
| 515 | val = ICR_SM; |
| 516 | clrsetbits_le32(&priv->base->icr, ICR_MODE_MASK, val); |
| 517 | |
| 518 | return 0; |
| 519 | } |
| 520 | |
Stefan Roese | 0c0f719 | 2016-09-16 15:07:52 +0200 | [diff] [blame] | 521 | static int mv_i2c_probe(struct udevice *bus) |
| 522 | { |
| 523 | struct mv_i2c_priv *priv = dev_get_priv(bus); |
| 524 | |
Masahiro Yamada | 702e57e | 2020-08-04 14:14:43 +0900 | [diff] [blame] | 525 | priv->base = dev_read_addr_ptr(bus); |
Stefan Roese | 0c0f719 | 2016-09-16 15:07:52 +0200 | [diff] [blame] | 526 | |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | static const struct dm_i2c_ops mv_i2c_ops = { |
| 531 | .xfer = mv_i2c_xfer, |
Stefan Roese | 9ad5a00 | 2016-09-16 15:07:53 +0200 | [diff] [blame] | 532 | .set_bus_speed = mv_i2c_set_bus_speed, |
Stefan Roese | 0c0f719 | 2016-09-16 15:07:52 +0200 | [diff] [blame] | 533 | }; |
| 534 | |
| 535 | static const struct udevice_id mv_i2c_ids[] = { |
| 536 | { .compatible = "marvell,armada-3700-i2c" }, |
| 537 | { } |
| 538 | }; |
| 539 | |
| 540 | U_BOOT_DRIVER(i2c_mv) = { |
| 541 | .name = "i2c_mv", |
| 542 | .id = UCLASS_I2C, |
| 543 | .of_match = mv_i2c_ids, |
| 544 | .probe = mv_i2c_probe, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 545 | .priv_auto = sizeof(struct mv_i2c_priv), |
Stefan Roese | 0c0f719 | 2016-09-16 15:07:52 +0200 | [diff] [blame] | 546 | .ops = &mv_i2c_ops, |
| 547 | }; |
| 548 | #endif /* CONFIG_DM_I2C */ |