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 * (C) Copyright 2011 Marvell Inc. 12 * Lei Wen <leiwen@marvell.com> 13 * 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 36 #include <common.h> 37 #include <asm/io.h> 38 39 #ifdef CONFIG_HARD_I2C 40 #include <i2c.h> 41 #include "mv_i2c.h" 42 43 #ifdef DEBUG_I2C 44 #define PRINTD(x) printf x 45 #else 46 #define PRINTD(x) 47 #endif 48 49 /* All transfers are described by this data structure */ 50 struct i2c_msg { 51 u8 condition; 52 u8 acknack; 53 u8 direction; 54 u8 data; 55 }; 56 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 71 /* 72 * i2c_reset: - reset the host controller 73 * 74 */ 75 static void i2c_reset(void) 76 { 77 writel(readl(&base->icr) & ~ICR_IUE, &base->icr); /* disable unit */ 78 writel(readl(&base->icr) | ICR_UR, &base->icr); /* reset the unit */ 79 udelay(100); 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 */ 88 udelay(100); 89 } 90 91 /* 92 * i2c_isr_set_cleared: - wait until certain bits of the I2C status register 93 * are set and cleared 94 * 95 * @return: 1 in case of success, 0 means timeout (no match within 10 ms). 96 */ 97 static int i2c_isr_set_cleared(unsigned long set_mask, 98 unsigned long cleared_mask) 99 { 100 int timeout = 1000, isr; 101 102 do { 103 isr = readl(&base->isr); 104 udelay(10); 105 if (timeout-- < 0) 106 return 0; 107 } while (((isr & set_mask) != set_mask) 108 || ((isr & cleared_mask) != 0)); 109 110 return 1; 111 } 112 113 /* 114 * i2c_transfer: - Transfer one byte over the i2c bus 115 * 116 * This function can tranfer a byte over the i2c bus in both directions. 117 * It is used by the public API functions. 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 126 */ 127 int i2c_transfer(struct i2c_msg *msg) 128 { 129 int ret; 130 131 if (!msg) 132 goto transfer_error_msg_empty; 133 134 switch (msg->direction) { 135 case I2C_WRITE: 136 /* check if bus is not busy */ 137 if (!i2c_isr_set_cleared(0, ISR_IBB)) 138 goto transfer_error_bus_busy; 139 140 /* start transmission */ 141 writel(readl(&base->icr) & ~ICR_START, &base->icr); 142 writel(readl(&base->icr) & ~ICR_STOP, &base->icr); 143 writel(msg->data, &base->idbr); 144 if (msg->condition == I2C_COND_START) 145 writel(readl(&base->icr) | ICR_START, &base->icr); 146 if (msg->condition == I2C_COND_STOP) 147 writel(readl(&base->icr) | ICR_STOP, &base->icr); 148 if (msg->acknack == I2C_ACKNAK_SENDNAK) 149 writel(readl(&base->icr) | ICR_ACKNAK, &base->icr); 150 if (msg->acknack == I2C_ACKNAK_SENDACK) 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); 154 155 /* transmit register empty? */ 156 if (!i2c_isr_set_cleared(ISR_ITE, 0)) 157 goto transfer_error_transmit_timeout; 158 159 /* clear 'transmit empty' state */ 160 writel(readl(&base->isr) | ISR_ITE, &base->isr); 161 162 /* wait for ACK from slave */ 163 if (msg->acknack == I2C_ACKNAK_WAITACK) 164 if (!i2c_isr_set_cleared(0, ISR_ACKNAK)) 165 goto transfer_error_ack_missing; 166 break; 167 168 case I2C_READ: 169 170 /* check if bus is not busy */ 171 if (!i2c_isr_set_cleared(0, ISR_IBB)) 172 goto transfer_error_bus_busy; 173 174 /* start receive */ 175 writel(readl(&base->icr) & ~ICR_START, &base->icr); 176 writel(readl(&base->icr) & ~ICR_STOP, &base->icr); 177 if (msg->condition == I2C_COND_START) 178 writel(readl(&base->icr) | ICR_START, &base->icr); 179 if (msg->condition == I2C_COND_STOP) 180 writel(readl(&base->icr) | ICR_STOP, &base->icr); 181 if (msg->acknack == I2C_ACKNAK_SENDNAK) 182 writel(readl(&base->icr) | ICR_ACKNAK, &base->icr); 183 if (msg->acknack == I2C_ACKNAK_SENDACK) 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); 187 188 /* receive register full? */ 189 if (!i2c_isr_set_cleared(ISR_IRF, 0)) 190 goto transfer_error_receive_timeout; 191 192 msg->data = readl(&base->idbr); 193 194 /* clear 'receive empty' state */ 195 writel(readl(&base->isr) | ISR_IRF, &base->isr); 196 break; 197 default: 198 goto transfer_error_illegal_param; 199 } 200 201 return 0; 202 203 transfer_error_msg_empty: 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: 228 PRINTD(("i2c_transfer: ISR: 0x%04x\n", ISR)); 229 i2c_reset(); 230 return ret; 231 } 232 233 /* ------------------------------------------------------------------------ */ 234 /* API Functions */ 235 /* ------------------------------------------------------------------------ */ 236 void i2c_init(int speed, int slaveaddr) 237 { 238 #ifdef CONFIG_SYS_I2C_INIT_BOARD 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 251 i2c_init_board(); 252 253 writel(icr, &base->icr); 254 #endif 255 } 256 257 /* 258 * i2c_probe: - Test if a chip answers for a given i2c address 259 * 260 * @chip: address of the chip which is searched for 261 * @return: 0 if a chip was found, -1 otherwhise 262 */ 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; 273 if (i2c_transfer(&msg)) 274 return -1; 275 276 msg.condition = I2C_COND_STOP; 277 msg.acknack = I2C_ACKNAK_SENDNAK; 278 msg.direction = I2C_READ; 279 msg.data = 0x00; 280 if (i2c_transfer(&msg)) 281 return -1; 282 283 return 0; 284 } 285 286 /* 287 * i2c_read: - Read multiple bytes from an i2c device 288 * 289 * The higher level routines take into account that this function is only 290 * called with len < page length of the device (see configuration file) 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 */ 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 */ 303 304 PRINTD(("i2c_read(chip=0x%02x, addr=0x%02x, alen=0x%02x, " 305 "len=0x%02x)\n", chip, addr, alen, len)); 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; 314 msg.data = (chip << 1); 315 msg.data &= 0xFE; 316 if (i2c_transfer(&msg)) 317 return -1; 318 319 /* 320 * send memory address bytes; 321 * alen defines how much bytes we have to send. 322 */ 323 /*addr &= ((1 << CONFIG_SYS_EEPROM_PAGE_WRITE_BITS)-1); */ 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) { 329 PRINTD(("i2c_read: send memory word address byte %1d\n", alen)); 330 msg.condition = I2C_COND_NORMAL; 331 msg.acknack = I2C_ACKNAK_WAITACK; 332 msg.direction = I2C_WRITE; 333 msg.data = addr_bytes[alen]; 334 if (i2c_transfer(&msg)) 335 return -1; 336 } 337 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; 345 if (i2c_transfer(&msg)) 346 return -1; 347 348 /* read bytes; send NACK at last byte */ 349 while (len--) { 350 if (len == 0) { 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; 360 if (i2c_transfer(&msg)) 361 return -1; 362 363 *buffer = msg.data; 364 PRINTD(("i2c_read: reading byte (0x%08x)=0x%02x\n", 365 (unsigned int)buffer, *buffer)); 366 buffer++; 367 } 368 369 i2c_reset(); 370 371 return 0; 372 } 373 374 /* 375 * i2c_write: - Write multiple bytes to an i2c device 376 * 377 * The higher level routines take into account that this function is only 378 * called with len < page length of the device (see configuration file) 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) 383 * @buffer: where to find the data to be written 384 * @len: how much byte do we want to read 385 * @return: 0 in case of success 386 */ 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 392 PRINTD(("i2c_write(chip=0x%02x, addr=0x%02x, alen=0x%02x, " 393 "len=0x%02x)\n", chip, addr, alen, len)); 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; 402 msg.data = (chip << 1); 403 msg.data &= 0xFE; 404 if (i2c_transfer(&msg)) 405 return -1; 406 407 /* 408 * send memory address bytes; 409 * alen defines how much bytes we have to send. 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) { 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]; 421 if (i2c_transfer(&msg)) 422 return -1; 423 } 424 425 /* write bytes; send NACK at last byte */ 426 while (len--) { 427 PRINTD(("i2c_write: writing byte (0x%08x)=0x%02x\n", 428 (unsigned int)buffer, *buffer)); 429 430 if (len == 0) 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++); 438 439 if (i2c_transfer(&msg)) 440 return -1; 441 } 442 443 i2c_reset(); 444 445 return 0; 446 } 447 #endif /* CONFIG_HARD_I2C */ 448