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