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