1 /* 2 * i2c driver for Freescale i.MX series 3 * 4 * (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de> 5 * (c) 2011 Marek Vasut <marek.vasut@gmail.com> 6 * 7 * Based on i2c-imx.c from linux kernel: 8 * Copyright (C) 2005 Torsten Koschorrek <koschorrek at synertronixx.de> 9 * Copyright (C) 2005 Matthias Blaschke <blaschke at synertronixx.de> 10 * Copyright (C) 2007 RightHand Technologies, Inc. 11 * Copyright (C) 2008 Darius Augulis <darius.augulis at teltonika.lt> 12 * 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 33 #include <common.h> 34 #include <asm/io.h> 35 36 #if defined(CONFIG_HARD_I2C) 37 38 #include <asm/arch/clock.h> 39 #include <asm/arch/imx-regs.h> 40 #include <i2c.h> 41 42 struct mxc_i2c_regs { 43 uint32_t iadr; 44 uint32_t ifdr; 45 uint32_t i2cr; 46 uint32_t i2sr; 47 uint32_t i2dr; 48 }; 49 50 #define I2CR_IEN (1 << 7) 51 #define I2CR_IIEN (1 << 6) 52 #define I2CR_MSTA (1 << 5) 53 #define I2CR_MTX (1 << 4) 54 #define I2CR_TX_NO_AK (1 << 3) 55 #define I2CR_RSTA (1 << 2) 56 57 #define I2SR_ICF (1 << 7) 58 #define I2SR_IBB (1 << 5) 59 #define I2SR_IIF (1 << 1) 60 #define I2SR_RX_NO_AK (1 << 0) 61 62 #ifdef CONFIG_SYS_I2C_BASE 63 #define I2C_BASE CONFIG_SYS_I2C_BASE 64 #else 65 #error "define CONFIG_SYS_I2C_BASE to use the mxc_i2c driver" 66 #endif 67 68 #define I2C_MAX_TIMEOUT 10000 69 70 static u16 i2c_clk_div[50][2] = { 71 { 22, 0x20 }, { 24, 0x21 }, { 26, 0x22 }, { 28, 0x23 }, 72 { 30, 0x00 }, { 32, 0x24 }, { 36, 0x25 }, { 40, 0x26 }, 73 { 42, 0x03 }, { 44, 0x27 }, { 48, 0x28 }, { 52, 0x05 }, 74 { 56, 0x29 }, { 60, 0x06 }, { 64, 0x2A }, { 72, 0x2B }, 75 { 80, 0x2C }, { 88, 0x09 }, { 96, 0x2D }, { 104, 0x0A }, 76 { 112, 0x2E }, { 128, 0x2F }, { 144, 0x0C }, { 160, 0x30 }, 77 { 192, 0x31 }, { 224, 0x32 }, { 240, 0x0F }, { 256, 0x33 }, 78 { 288, 0x10 }, { 320, 0x34 }, { 384, 0x35 }, { 448, 0x36 }, 79 { 480, 0x13 }, { 512, 0x37 }, { 576, 0x14 }, { 640, 0x38 }, 80 { 768, 0x39 }, { 896, 0x3A }, { 960, 0x17 }, { 1024, 0x3B }, 81 { 1152, 0x18 }, { 1280, 0x3C }, { 1536, 0x3D }, { 1792, 0x3E }, 82 { 1920, 0x1B }, { 2048, 0x3F }, { 2304, 0x1C }, { 2560, 0x1D }, 83 { 3072, 0x1E }, { 3840, 0x1F } 84 }; 85 86 /* 87 * Calculate and set proper clock divider 88 */ 89 static uint8_t i2c_imx_get_clk(unsigned int rate) 90 { 91 unsigned int i2c_clk_rate; 92 unsigned int div; 93 u8 clk_div; 94 95 #if defined(CONFIG_MX31) 96 struct clock_control_regs *sc_regs = 97 (struct clock_control_regs *)CCM_BASE; 98 99 /* start the required I2C clock */ 100 writel(readl(&sc_regs->cgr0) | (3 << CONFIG_SYS_I2C_CLK_OFFSET), 101 &sc_regs->cgr0); 102 #endif 103 104 /* Divider value calculation */ 105 i2c_clk_rate = mxc_get_clock(MXC_IPG_PERCLK); 106 div = (i2c_clk_rate + rate - 1) / rate; 107 if (div < i2c_clk_div[0][0]) 108 clk_div = 0; 109 else if (div > i2c_clk_div[ARRAY_SIZE(i2c_clk_div) - 1][0]) 110 clk_div = ARRAY_SIZE(i2c_clk_div) - 1; 111 else 112 for (clk_div = 0; i2c_clk_div[clk_div][0] < div; clk_div++) 113 ; 114 115 /* Store divider value */ 116 return clk_div; 117 } 118 119 /* 120 * Reset I2C Controller 121 */ 122 void i2c_reset(void) 123 { 124 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE; 125 126 writeb(0, &i2c_regs->i2cr); /* Reset module */ 127 writeb(0, &i2c_regs->i2sr); 128 } 129 130 /* 131 * Init I2C Bus 132 */ 133 void i2c_init(int speed, int unused) 134 { 135 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE; 136 u8 clk_idx = i2c_imx_get_clk(speed); 137 u8 idx = i2c_clk_div[clk_idx][1]; 138 139 /* Store divider value */ 140 writeb(idx, &i2c_regs->ifdr); 141 142 i2c_reset(); 143 } 144 145 /* 146 * Set I2C Speed 147 */ 148 int i2c_set_bus_speed(unsigned int speed) 149 { 150 i2c_init(speed, 0); 151 return 0; 152 } 153 154 /* 155 * Get I2C Speed 156 */ 157 unsigned int i2c_get_bus_speed(void) 158 { 159 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE; 160 u8 clk_idx = readb(&i2c_regs->ifdr); 161 u8 clk_div; 162 163 for (clk_div = 0; i2c_clk_div[clk_div][1] != clk_idx; clk_div++) 164 ; 165 166 return mxc_get_clock(MXC_IPG_PERCLK) / i2c_clk_div[clk_div][0]; 167 } 168 169 /* 170 * Wait for bus to be busy (or free if for_busy = 0) 171 * 172 * for_busy = 1: Wait for IBB to be asserted 173 * for_busy = 0: Wait for IBB to be de-asserted 174 */ 175 int i2c_imx_bus_busy(int for_busy) 176 { 177 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE; 178 unsigned int temp; 179 180 int timeout = I2C_MAX_TIMEOUT; 181 182 while (timeout--) { 183 temp = readb(&i2c_regs->i2sr); 184 185 if (for_busy && (temp & I2SR_IBB)) 186 return 0; 187 if (!for_busy && !(temp & I2SR_IBB)) 188 return 0; 189 190 udelay(1); 191 } 192 193 return 1; 194 } 195 196 /* 197 * Wait for transaction to complete 198 */ 199 int i2c_imx_trx_complete(void) 200 { 201 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE; 202 int timeout = I2C_MAX_TIMEOUT; 203 204 while (timeout--) { 205 if (readb(&i2c_regs->i2sr) & I2SR_IIF) { 206 writeb(0, &i2c_regs->i2sr); 207 return 0; 208 } 209 210 udelay(1); 211 } 212 213 return 1; 214 } 215 216 /* 217 * Check if the transaction was ACKed 218 */ 219 int i2c_imx_acked(void) 220 { 221 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE; 222 223 return readb(&i2c_regs->i2sr) & I2SR_RX_NO_AK; 224 } 225 226 /* 227 * Start the controller 228 */ 229 int i2c_imx_start(void) 230 { 231 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE; 232 unsigned int temp = 0; 233 int result; 234 int speed = i2c_get_bus_speed(); 235 u8 clk_idx = i2c_imx_get_clk(speed); 236 u8 idx = i2c_clk_div[clk_idx][1]; 237 238 /* Store divider value */ 239 writeb(idx, &i2c_regs->ifdr); 240 241 /* Enable I2C controller */ 242 writeb(0, &i2c_regs->i2sr); 243 writeb(I2CR_IEN, &i2c_regs->i2cr); 244 245 /* Wait controller to be stable */ 246 udelay(50); 247 248 /* Start I2C transaction */ 249 temp = readb(&i2c_regs->i2cr); 250 temp |= I2CR_MSTA; 251 writeb(temp, &i2c_regs->i2cr); 252 253 result = i2c_imx_bus_busy(1); 254 if (result) 255 return result; 256 257 temp |= I2CR_MTX | I2CR_TX_NO_AK; 258 writeb(temp, &i2c_regs->i2cr); 259 260 return 0; 261 } 262 263 /* 264 * Stop the controller 265 */ 266 void i2c_imx_stop(void) 267 { 268 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE; 269 unsigned int temp = 0; 270 271 /* Stop I2C transaction */ 272 temp = readb(&i2c_regs->i2cr); 273 temp |= ~(I2CR_MSTA | I2CR_MTX); 274 writeb(temp, &i2c_regs->i2cr); 275 276 i2c_imx_bus_busy(0); 277 278 /* Disable I2C controller */ 279 writeb(0, &i2c_regs->i2cr); 280 } 281 282 /* 283 * Set chip address and access mode 284 * 285 * read = 1: READ access 286 * read = 0: WRITE access 287 */ 288 int i2c_imx_set_chip_addr(uchar chip, int read) 289 { 290 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE; 291 int ret; 292 293 writeb((chip << 1) | read, &i2c_regs->i2dr); 294 295 ret = i2c_imx_trx_complete(); 296 if (ret) 297 return ret; 298 299 ret = i2c_imx_acked(); 300 if (ret) 301 return ret; 302 303 return ret; 304 } 305 306 /* 307 * Write register address 308 */ 309 int i2c_imx_set_reg_addr(uint addr, int alen) 310 { 311 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE; 312 int ret = 0; 313 314 while (alen--) { 315 writeb((addr >> (alen * 8)) & 0xff, &i2c_regs->i2dr); 316 317 ret = i2c_imx_trx_complete(); 318 if (ret) 319 break; 320 321 ret = i2c_imx_acked(); 322 if (ret) 323 break; 324 } 325 326 return ret; 327 } 328 329 /* 330 * Try if a chip add given address responds (probe the chip) 331 */ 332 int i2c_probe(uchar chip) 333 { 334 int ret; 335 336 ret = i2c_imx_start(); 337 if (ret) 338 return ret; 339 340 ret = i2c_imx_set_chip_addr(chip, 0); 341 if (ret) 342 return ret; 343 344 i2c_imx_stop(); 345 346 return ret; 347 } 348 349 /* 350 * Read data from I2C device 351 */ 352 int i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len) 353 { 354 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE; 355 int ret; 356 unsigned int temp; 357 int i; 358 359 ret = i2c_imx_start(); 360 if (ret) 361 return ret; 362 363 /* write slave address */ 364 ret = i2c_imx_set_chip_addr(chip, 0); 365 if (ret) 366 return ret; 367 368 ret = i2c_imx_set_reg_addr(addr, alen); 369 if (ret) 370 return ret; 371 372 temp = readb(&i2c_regs->i2cr); 373 temp |= I2CR_RSTA; 374 writeb(temp, &i2c_regs->i2cr); 375 376 ret = i2c_imx_set_chip_addr(chip, 1); 377 if (ret) 378 return ret; 379 380 /* setup bus to read data */ 381 temp = readb(&i2c_regs->i2cr); 382 temp &= ~(I2CR_MTX | I2CR_TX_NO_AK); 383 if (len == 1) 384 temp |= I2CR_TX_NO_AK; 385 writeb(temp, &i2c_regs->i2cr); 386 readb(&i2c_regs->i2dr); 387 388 /* read data */ 389 for (i = 0; i < len; i++) { 390 ret = i2c_imx_trx_complete(); 391 if (ret) 392 return ret; 393 394 /* 395 * It must generate STOP before read I2DR to prevent 396 * controller from generating another clock cycle 397 */ 398 if (i == (len - 1)) { 399 temp = readb(&i2c_regs->i2cr); 400 temp &= ~(I2CR_MSTA | I2CR_MTX); 401 writeb(temp, &i2c_regs->i2cr); 402 i2c_imx_bus_busy(0); 403 } else if (i == (len - 2)) { 404 temp = readb(&i2c_regs->i2cr); 405 temp |= I2CR_TX_NO_AK; 406 writeb(temp, &i2c_regs->i2cr); 407 } 408 409 buf[i] = readb(&i2c_regs->i2dr); 410 } 411 412 i2c_imx_stop(); 413 414 return ret; 415 } 416 417 /* 418 * Write data to I2C device 419 */ 420 int i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len) 421 { 422 struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE; 423 int ret; 424 int i; 425 426 ret = i2c_imx_start(); 427 if (ret) 428 return ret; 429 430 /* write slave address */ 431 ret = i2c_imx_set_chip_addr(chip, 0); 432 if (ret) 433 return ret; 434 435 ret = i2c_imx_set_reg_addr(addr, alen); 436 if (ret) 437 return ret; 438 439 for (i = 0; i < len; i++) { 440 writeb(buf[i], &i2c_regs->i2dr); 441 442 ret = i2c_imx_trx_complete(); 443 if (ret) 444 return ret; 445 446 ret = i2c_imx_acked(); 447 if (ret) 448 return ret; 449 } 450 451 i2c_imx_stop(); 452 453 return ret; 454 } 455 #endif /* CONFIG_HARD_I2C */ 456