wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000 |
| 3 | * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it |
| 4 | * |
| 5 | * (C) Copyright 2000 Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 6 | * Marius Groeger <mgroeger@sysgo.de> |
| 7 | * |
| 8 | * (C) Copyright 2003 Pengutronix e.K. |
| 9 | * Robert Schwebel <r.schwebel@pengutronix.de> |
| 10 | * |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 11 | * (C) Copyright 2011 Marvell Inc. |
| 12 | * Lei Wen <leiwen@marvell.com> |
| 13 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 14 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 15 | * |
| 16 | * Back ported to the 8xx platform (from the 8260 platform) by |
| 17 | * Murray.Jensen@cmst.csiro.au, 27-Jan-01. |
| 18 | */ |
| 19 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 20 | #include <common.h> |
Marek Vasut | 3ba8bf7 | 2010-09-09 09:50:39 +0200 | [diff] [blame] | 21 | #include <asm/io.h> |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 22 | |
| 23 | #ifdef CONFIG_HARD_I2C |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 24 | #include <i2c.h> |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 25 | #include "mv_i2c.h" |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 26 | |
| 27 | #ifdef DEBUG_I2C |
| 28 | #define PRINTD(x) printf x |
| 29 | #else |
| 30 | #define PRINTD(x) |
| 31 | #endif |
| 32 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 33 | /* All transfers are described by this data structure */ |
Simon Glass | fffff72 | 2015-02-05 21:41:33 -0700 | [diff] [blame] | 34 | struct mv_i2c_msg { |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 35 | u8 condition; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 36 | u8 acknack; |
| 37 | u8 direction; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 38 | u8 data; |
| 39 | }; |
| 40 | |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 41 | struct mv_i2c { |
| 42 | u32 ibmr; |
| 43 | u32 pad0; |
| 44 | u32 idbr; |
| 45 | u32 pad1; |
| 46 | u32 icr; |
| 47 | u32 pad2; |
| 48 | u32 isr; |
| 49 | u32 pad3; |
| 50 | u32 isar; |
| 51 | }; |
| 52 | |
Lei Wen | adb00bb | 2011-04-13 23:48:39 +0530 | [diff] [blame] | 53 | static struct mv_i2c *base; |
Lei Wen | d308c9d | 2011-09-30 05:44:12 +0000 | [diff] [blame] | 54 | static void i2c_board_init(struct mv_i2c *base) |
| 55 | { |
| 56 | #ifdef CONFIG_SYS_I2C_INIT_BOARD |
| 57 | u32 icr; |
| 58 | /* |
| 59 | * call board specific i2c bus reset routine before accessing the |
| 60 | * environment, which might be in a chip on that bus. For details |
| 61 | * about this problem see doc/I2C_Edge_Conditions. |
| 62 | * |
| 63 | * disable I2C controller first, otherwhise it thinks we want to |
| 64 | * talk to the slave port... |
| 65 | */ |
| 66 | icr = readl(&base->icr); |
| 67 | writel(readl(&base->icr) & ~(ICR_SCLE | ICR_IUE), &base->icr); |
| 68 | |
| 69 | i2c_init_board(); |
| 70 | |
| 71 | writel(icr, &base->icr); |
| 72 | #endif |
| 73 | } |
| 74 | |
Lei Wen | adb00bb | 2011-04-13 23:48:39 +0530 | [diff] [blame] | 75 | #ifdef CONFIG_I2C_MULTI_BUS |
Rob Herring | e6fbc3e | 2015-03-17 15:46:36 -0500 | [diff] [blame] | 76 | static unsigned long i2c_regs[CONFIG_MV_I2C_NUM] = CONFIG_MV_I2C_REG; |
Lei Wen | adb00bb | 2011-04-13 23:48:39 +0530 | [diff] [blame] | 77 | static unsigned int bus_initialized[CONFIG_MV_I2C_NUM]; |
| 78 | static unsigned int current_bus; |
| 79 | |
| 80 | int i2c_set_bus_num(unsigned int bus) |
| 81 | { |
| 82 | if ((bus < 0) || (bus >= CONFIG_MV_I2C_NUM)) { |
| 83 | printf("Bad bus: %d\n", bus); |
| 84 | return -1; |
| 85 | } |
| 86 | |
| 87 | base = (struct mv_i2c *)i2c_regs[bus]; |
| 88 | current_bus = bus; |
| 89 | |
| 90 | if (!bus_initialized[current_bus]) { |
Lei Wen | d308c9d | 2011-09-30 05:44:12 +0000 | [diff] [blame] | 91 | i2c_board_init(base); |
Lei Wen | adb00bb | 2011-04-13 23:48:39 +0530 | [diff] [blame] | 92 | bus_initialized[current_bus] = 1; |
| 93 | } |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | unsigned int i2c_get_bus_num(void) |
| 99 | { |
| 100 | return current_bus; |
| 101 | } |
| 102 | #endif |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 103 | |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 104 | /* |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 105 | * i2c_reset: - reset the host controller |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 106 | * |
| 107 | */ |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 108 | static void i2c_reset(void) |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 109 | { |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 110 | writel(readl(&base->icr) & ~ICR_IUE, &base->icr); /* disable unit */ |
| 111 | writel(readl(&base->icr) | ICR_UR, &base->icr); /* reset the unit */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 112 | udelay(100); |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 113 | writel(readl(&base->icr) & ~ICR_IUE, &base->icr); /* disable unit */ |
| 114 | |
| 115 | i2c_clk_enable(); |
| 116 | |
| 117 | writel(CONFIG_SYS_I2C_SLAVE, &base->isar); /* set our slave address */ |
| 118 | writel(I2C_ICR_INIT, &base->icr); /* set control reg values */ |
| 119 | writel(I2C_ISR_INIT, &base->isr); /* set clear interrupt bits */ |
| 120 | writel(readl(&base->icr) | ICR_IUE, &base->icr); /* enable unit */ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 121 | udelay(100); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 124 | /* |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 125 | * i2c_isr_set_cleared: - wait until certain bits of the I2C status register |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 126 | * are set and cleared |
| 127 | * |
Markus Klotzbuecher | ba70d6a | 2006-03-24 12:23:27 +0100 | [diff] [blame] | 128 | * @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] | 129 | */ |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 130 | static int i2c_isr_set_cleared(unsigned long set_mask, |
| 131 | unsigned long cleared_mask) |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 132 | { |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 133 | int timeout = 1000, isr; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 134 | |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 135 | do { |
| 136 | isr = readl(&base->isr); |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 137 | udelay(10); |
| 138 | if (timeout-- < 0) |
| 139 | return 0; |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 140 | } while (((isr & set_mask) != set_mask) |
| 141 | || ((isr & cleared_mask) != 0)); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 142 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 143 | return 1; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 146 | /* |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 147 | * i2c_transfer: - Transfer one byte over the i2c bus |
| 148 | * |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 149 | * This function can tranfer a byte over the i2c bus in both directions. |
| 150 | * It is used by the public API functions. |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 151 | * |
| 152 | * @return: 0: transfer successful |
| 153 | * -1: message is empty |
| 154 | * -2: transmit timeout |
| 155 | * -3: ACK missing |
| 156 | * -4: receive timeout |
| 157 | * -5: illegal parameters |
| 158 | * -6: bus is busy and couldn't be aquired |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 159 | */ |
Simon Glass | fffff72 | 2015-02-05 21:41:33 -0700 | [diff] [blame] | 160 | int i2c_transfer(struct mv_i2c_msg *msg) |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 161 | { |
| 162 | int ret; |
| 163 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 164 | if (!msg) |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 165 | goto transfer_error_msg_empty; |
| 166 | |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 167 | switch (msg->direction) { |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 168 | case I2C_WRITE: |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 169 | /* check if bus is not busy */ |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 170 | if (!i2c_isr_set_cleared(0, ISR_IBB)) |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 171 | goto transfer_error_bus_busy; |
| 172 | |
| 173 | /* start transmission */ |
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); |
| 176 | writel(msg->data, &base->idbr); |
Marek Vasut | 3ba8bf7 | 2010-09-09 09:50:39 +0200 | [diff] [blame] | 177 | if (msg->condition == I2C_COND_START) |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 178 | writel(readl(&base->icr) | ICR_START, &base->icr); |
Marek Vasut | 3ba8bf7 | 2010-09-09 09:50:39 +0200 | [diff] [blame] | 179 | if (msg->condition == I2C_COND_STOP) |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 180 | writel(readl(&base->icr) | ICR_STOP, &base->icr); |
Marek Vasut | 3ba8bf7 | 2010-09-09 09:50:39 +0200 | [diff] [blame] | 181 | if (msg->acknack == I2C_ACKNAK_SENDNAK) |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 182 | writel(readl(&base->icr) | ICR_ACKNAK, &base->icr); |
Marek Vasut | 3ba8bf7 | 2010-09-09 09:50:39 +0200 | [diff] [blame] | 183 | if (msg->acknack == I2C_ACKNAK_SENDACK) |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 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); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 187 | |
| 188 | /* transmit register empty? */ |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 189 | if (!i2c_isr_set_cleared(ISR_ITE, 0)) |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 190 | goto transfer_error_transmit_timeout; |
| 191 | |
| 192 | /* clear 'transmit empty' state */ |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 193 | writel(readl(&base->isr) | ISR_ITE, &base->isr); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 194 | |
| 195 | /* wait for ACK from slave */ |
| 196 | if (msg->acknack == I2C_ACKNAK_WAITACK) |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 197 | if (!i2c_isr_set_cleared(0, ISR_ACKNAK)) |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 198 | goto transfer_error_ack_missing; |
| 199 | break; |
| 200 | |
| 201 | case I2C_READ: |
| 202 | |
| 203 | /* check if bus is not busy */ |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 204 | if (!i2c_isr_set_cleared(0, ISR_IBB)) |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 205 | goto transfer_error_bus_busy; |
| 206 | |
| 207 | /* start receive */ |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 208 | writel(readl(&base->icr) & ~ICR_START, &base->icr); |
| 209 | writel(readl(&base->icr) & ~ICR_STOP, &base->icr); |
Marek Vasut | 3ba8bf7 | 2010-09-09 09:50:39 +0200 | [diff] [blame] | 210 | if (msg->condition == I2C_COND_START) |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 211 | writel(readl(&base->icr) | ICR_START, &base->icr); |
Marek Vasut | 3ba8bf7 | 2010-09-09 09:50:39 +0200 | [diff] [blame] | 212 | if (msg->condition == I2C_COND_STOP) |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 213 | writel(readl(&base->icr) | ICR_STOP, &base->icr); |
Marek Vasut | 3ba8bf7 | 2010-09-09 09:50:39 +0200 | [diff] [blame] | 214 | if (msg->acknack == I2C_ACKNAK_SENDNAK) |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 215 | writel(readl(&base->icr) | ICR_ACKNAK, &base->icr); |
Marek Vasut | 3ba8bf7 | 2010-09-09 09:50:39 +0200 | [diff] [blame] | 216 | if (msg->acknack == I2C_ACKNAK_SENDACK) |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 217 | writel(readl(&base->icr) & ~ICR_ACKNAK, &base->icr); |
| 218 | writel(readl(&base->icr) & ~ICR_ALDIE, &base->icr); |
| 219 | writel(readl(&base->icr) | ICR_TB, &base->icr); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 220 | |
| 221 | /* receive register full? */ |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 222 | if (!i2c_isr_set_cleared(ISR_IRF, 0)) |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 223 | goto transfer_error_receive_timeout; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 224 | |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 225 | msg->data = readl(&base->idbr); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 226 | |
| 227 | /* clear 'receive empty' state */ |
Lei Wen | 3df619e | 2011-04-13 23:48:31 +0530 | [diff] [blame] | 228 | writel(readl(&base->isr) | ISR_IRF, &base->isr); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 229 | break; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 230 | default: |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 231 | goto transfer_error_illegal_param; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 232 | } |
| 233 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 234 | return 0; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 235 | |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 236 | transfer_error_msg_empty: |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 237 | PRINTD(("i2c_transfer: error: 'msg' is empty\n")); |
| 238 | ret = -1; goto i2c_transfer_finish; |
| 239 | |
| 240 | transfer_error_transmit_timeout: |
| 241 | PRINTD(("i2c_transfer: error: transmit timeout\n")); |
| 242 | ret = -2; goto i2c_transfer_finish; |
| 243 | |
| 244 | transfer_error_ack_missing: |
| 245 | PRINTD(("i2c_transfer: error: ACK missing\n")); |
| 246 | ret = -3; goto i2c_transfer_finish; |
| 247 | |
| 248 | transfer_error_receive_timeout: |
| 249 | PRINTD(("i2c_transfer: error: receive timeout\n")); |
| 250 | ret = -4; goto i2c_transfer_finish; |
| 251 | |
| 252 | transfer_error_illegal_param: |
| 253 | PRINTD(("i2c_transfer: error: illegal parameters\n")); |
| 254 | ret = -5; goto i2c_transfer_finish; |
| 255 | |
| 256 | transfer_error_bus_busy: |
| 257 | PRINTD(("i2c_transfer: error: bus is busy\n")); |
| 258 | ret = -6; goto i2c_transfer_finish; |
| 259 | |
| 260 | i2c_transfer_finish: |
Lei Wen | 2be1ed3 | 2011-09-30 05:43:48 +0000 | [diff] [blame] | 261 | PRINTD(("i2c_transfer: ISR: 0x%04x\n", readl(&base->isr))); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 262 | i2c_reset(); |
| 263 | return ret; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /* ------------------------------------------------------------------------ */ |
| 267 | /* API Functions */ |
| 268 | /* ------------------------------------------------------------------------ */ |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 269 | void i2c_init(int speed, int slaveaddr) |
| 270 | { |
Lei Wen | adb00bb | 2011-04-13 23:48:39 +0530 | [diff] [blame] | 271 | #ifdef CONFIG_I2C_MULTI_BUS |
Lei Wen | d308c9d | 2011-09-30 05:44:12 +0000 | [diff] [blame] | 272 | current_bus = 0; |
Lei Wen | adb00bb | 2011-04-13 23:48:39 +0530 | [diff] [blame] | 273 | base = (struct mv_i2c *)i2c_regs[current_bus]; |
| 274 | #else |
| 275 | base = (struct mv_i2c *)CONFIG_MV_I2C_REG; |
| 276 | #endif |
| 277 | |
Lei Wen | d308c9d | 2011-09-30 05:44:12 +0000 | [diff] [blame] | 278 | i2c_board_init(base); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 281 | /* |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 282 | * i2c_probe: - Test if a chip answers for a given i2c address |
| 283 | * |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 284 | * @chip: address of the chip which is searched for |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 285 | * @return: 0 if a chip was found, -1 otherwhise |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 286 | */ |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 287 | int i2c_probe(uchar chip) |
| 288 | { |
Simon Glass | fffff72 | 2015-02-05 21:41:33 -0700 | [diff] [blame] | 289 | struct mv_i2c_msg msg; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 290 | |
| 291 | i2c_reset(); |
| 292 | |
| 293 | msg.condition = I2C_COND_START; |
| 294 | msg.acknack = I2C_ACKNAK_WAITACK; |
| 295 | msg.direction = I2C_WRITE; |
| 296 | msg.data = (chip << 1) + 1; |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 297 | if (i2c_transfer(&msg)) |
| 298 | return -1; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 299 | |
| 300 | msg.condition = I2C_COND_STOP; |
| 301 | msg.acknack = I2C_ACKNAK_SENDNAK; |
| 302 | msg.direction = I2C_READ; |
| 303 | msg.data = 0x00; |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 304 | if (i2c_transfer(&msg)) |
| 305 | return -1; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 310 | /* |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 311 | * i2c_read: - Read multiple bytes from an i2c device |
| 312 | * |
| 313 | * The higher level routines take into account that this function is only |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 314 | * called with len < page length of the device (see configuration file) |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 315 | * |
| 316 | * @chip: address of the chip which is to be read |
| 317 | * @addr: i2c data address within the chip |
| 318 | * @alen: length of the i2c data address (1..2 bytes) |
| 319 | * @buffer: where to write the data |
| 320 | * @len: how much byte do we want to read |
| 321 | * @return: 0 in case of success |
| 322 | */ |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 323 | int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len) |
| 324 | { |
Simon Glass | fffff72 | 2015-02-05 21:41:33 -0700 | [diff] [blame] | 325 | struct mv_i2c_msg msg; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 326 | u8 addr_bytes[3]; /* lowest...highest byte of data address */ |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 327 | |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 328 | PRINTD(("i2c_read(chip=0x%02x, addr=0x%02x, alen=0x%02x, " |
| 329 | "len=0x%02x)\n", chip, addr, alen, len)); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 330 | |
| 331 | i2c_reset(); |
| 332 | |
| 333 | /* dummy chip address write */ |
| 334 | PRINTD(("i2c_read: dummy chip address write\n")); |
| 335 | msg.condition = I2C_COND_START; |
| 336 | msg.acknack = I2C_ACKNAK_WAITACK; |
| 337 | msg.direction = I2C_WRITE; |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 338 | msg.data = (chip << 1); |
| 339 | msg.data &= 0xFE; |
| 340 | if (i2c_transfer(&msg)) |
| 341 | return -1; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 342 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 343 | /* |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 344 | * send memory address bytes; |
| 345 | * alen defines how much bytes we have to send. |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 346 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 347 | /*addr &= ((1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)-1); */ |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 348 | addr_bytes[0] = (u8)((addr >> 0) & 0x000000FF); |
| 349 | addr_bytes[1] = (u8)((addr >> 8) & 0x000000FF); |
| 350 | addr_bytes[2] = (u8)((addr >> 16) & 0x000000FF); |
| 351 | |
| 352 | while (--alen >= 0) { |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 353 | PRINTD(("i2c_read: send memory word address byte %1d\n", alen)); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 354 | msg.condition = I2C_COND_NORMAL; |
| 355 | msg.acknack = I2C_ACKNAK_WAITACK; |
| 356 | msg.direction = I2C_WRITE; |
| 357 | msg.data = addr_bytes[alen]; |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 358 | if (i2c_transfer(&msg)) |
| 359 | return -1; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 360 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 361 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 362 | /* start read sequence */ |
| 363 | PRINTD(("i2c_read: start read sequence\n")); |
| 364 | msg.condition = I2C_COND_START; |
| 365 | msg.acknack = I2C_ACKNAK_WAITACK; |
| 366 | msg.direction = I2C_WRITE; |
| 367 | msg.data = (chip << 1); |
| 368 | msg.data |= 0x01; |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 369 | if (i2c_transfer(&msg)) |
| 370 | return -1; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 371 | |
| 372 | /* read bytes; send NACK at last byte */ |
| 373 | while (len--) { |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 374 | if (len == 0) { |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 375 | msg.condition = I2C_COND_STOP; |
| 376 | msg.acknack = I2C_ACKNAK_SENDNAK; |
| 377 | } else { |
| 378 | msg.condition = I2C_COND_NORMAL; |
| 379 | msg.acknack = I2C_ACKNAK_SENDACK; |
| 380 | } |
| 381 | |
| 382 | msg.direction = I2C_READ; |
| 383 | msg.data = 0x00; |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 384 | if (i2c_transfer(&msg)) |
| 385 | return -1; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 386 | |
Markus Klotzbuecher | ba70d6a | 2006-03-24 12:23:27 +0100 | [diff] [blame] | 387 | *buffer = msg.data; |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 388 | PRINTD(("i2c_read: reading byte (0x%08x)=0x%02x\n", |
| 389 | (unsigned int)buffer, *buffer)); |
Markus Klotzbuecher | ba70d6a | 2006-03-24 12:23:27 +0100 | [diff] [blame] | 390 | buffer++; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | i2c_reset(); |
| 394 | |
| 395 | return 0; |
| 396 | } |
| 397 | |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 398 | /* |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 399 | * i2c_write: - Write multiple bytes to an i2c device |
| 400 | * |
| 401 | * The higher level routines take into account that this function is only |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 402 | * called with len < page length of the device (see configuration file) |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 403 | * |
| 404 | * @chip: address of the chip which is to be written |
| 405 | * @addr: i2c data address within the chip |
| 406 | * @alen: length of the i2c data address (1..2 bytes) |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 407 | * @buffer: where to find the data to be written |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 408 | * @len: how much byte do we want to read |
| 409 | * @return: 0 in case of success |
| 410 | */ |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 411 | int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len) |
| 412 | { |
Simon Glass | fffff72 | 2015-02-05 21:41:33 -0700 | [diff] [blame] | 413 | struct mv_i2c_msg msg; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 414 | u8 addr_bytes[3]; /* lowest...highest byte of data address */ |
| 415 | |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 416 | PRINTD(("i2c_write(chip=0x%02x, addr=0x%02x, alen=0x%02x, " |
| 417 | "len=0x%02x)\n", chip, addr, alen, len)); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 418 | |
| 419 | i2c_reset(); |
| 420 | |
| 421 | /* chip address write */ |
| 422 | PRINTD(("i2c_write: chip address write\n")); |
| 423 | msg.condition = I2C_COND_START; |
| 424 | msg.acknack = I2C_ACKNAK_WAITACK; |
| 425 | msg.direction = I2C_WRITE; |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 426 | msg.data = (chip << 1); |
| 427 | msg.data &= 0xFE; |
| 428 | if (i2c_transfer(&msg)) |
| 429 | return -1; |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 430 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 431 | /* |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 432 | * send memory address bytes; |
| 433 | * alen defines how much bytes we have to send. |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 434 | */ |
| 435 | addr_bytes[0] = (u8)((addr >> 0) & 0x000000FF); |
| 436 | addr_bytes[1] = (u8)((addr >> 8) & 0x000000FF); |
| 437 | addr_bytes[2] = (u8)((addr >> 16) & 0x000000FF); |
| 438 | |
| 439 | while (--alen >= 0) { |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 440 | PRINTD(("i2c_write: send memory word address\n")); |
| 441 | msg.condition = I2C_COND_NORMAL; |
| 442 | msg.acknack = I2C_ACKNAK_WAITACK; |
| 443 | msg.direction = I2C_WRITE; |
| 444 | msg.data = addr_bytes[alen]; |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 445 | if (i2c_transfer(&msg)) |
| 446 | return -1; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 447 | } |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 448 | |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 449 | /* write bytes; send NACK at last byte */ |
| 450 | while (len--) { |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 451 | PRINTD(("i2c_write: writing byte (0x%08x)=0x%02x\n", |
| 452 | (unsigned int)buffer, *buffer)); |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 453 | |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 454 | if (len == 0) |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 455 | msg.condition = I2C_COND_STOP; |
| 456 | else |
| 457 | msg.condition = I2C_COND_NORMAL; |
| 458 | |
| 459 | msg.acknack = I2C_ACKNAK_WAITACK; |
| 460 | msg.direction = I2C_WRITE; |
| 461 | msg.data = *(buffer++); |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 462 | |
Lei Wen | 68432c2 | 2011-04-13 23:48:16 +0530 | [diff] [blame] | 463 | if (i2c_transfer(&msg)) |
| 464 | return -1; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | i2c_reset(); |
| 468 | |
| 469 | return 0; |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 470 | } |
wdenk | 43d9616 | 2003-03-06 00:02:04 +0000 | [diff] [blame] | 471 | #endif /* CONFIG_HARD_I2C */ |