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