i2c.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * (C) Copyright 2006 Freescale Semiconductor, Inc.
  3. *
  4. * (C) Copyright 2003,Motorola Inc.
  5. * Xianghua Xiao <x.xiao@motorola.com>
  6. * Adapted for Motorola 85xx chip.
  7. *
  8. * (C) Copyright 2003
  9. * Gleb Natapov <gnatapov@mrv.com>
  10. * Some bits are taken from linux driver writen by adrian@humboldt.co.uk
  11. *
  12. * Hardware I2C driver for MPC107 PCI bridge.
  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. * Change log:
  33. *
  34. * 20050101: Eran Liberty (liberty@freescale.com)
  35. * Initial file creating (porting from 85XX & 8260)
  36. * 20060601: Dave Liu (daveliu@freescale.com)
  37. * Unified variable names for mpc83xx
  38. */
  39. #include <common.h>
  40. #include <command.h>
  41. #include <asm/io.h>
  42. #ifdef CONFIG_HARD_I2C
  43. #include <i2c.h>
  44. #include <asm/i2c.h>
  45. DECLARE_GLOBAL_DATA_PTR;
  46. /* Initialize the bus pointer to whatever one the SPD EEPROM is on.
  47. * Default is bus 1. This is necessary because the DDR initialization
  48. * runs from ROM, and we can't switch buses because we can't modify
  49. * the i2c_dev variable. Everything gets straightened out once i2c_init
  50. * is called from RAM. */
  51. #ifndef CFG_SPD_BUS_NUM
  52. #define CFG_SPD_BUS_NUM 1
  53. #endif
  54. static unsigned int i2c_bus_num = CFG_SPD_BUS_NUM;
  55. #if CFG_SPD_BUS_NUM == 1
  56. static volatile i2c_t *i2c_dev = I2C_1;
  57. #else
  58. static volatile i2c_t *i2c_dev = I2C_2;
  59. #endif
  60. static int i2c_bus_speed[2] = {0, 0};
  61. /*
  62. * Map the frequency divider to the FDR. This data is taken from table 17-5
  63. * of the MPC8349EA reference manual, with duplicates removed.
  64. */
  65. static struct {
  66. unsigned int divider;
  67. u8 fdr;
  68. } i2c_speed_map[] =
  69. {
  70. {0, 0x20},
  71. {256, 0x20},
  72. {288, 0x21},
  73. {320, 0x22},
  74. {352, 0x23},
  75. {384, 0x24},
  76. {416, 0x01},
  77. {448, 0x25},
  78. {480, 0x02},
  79. {512, 0x26},
  80. {576, 0x27},
  81. {640, 0x28},
  82. {704, 0x05},
  83. {768, 0x29},
  84. {832, 0x06},
  85. {896, 0x2A},
  86. {1024, 0x2B},
  87. {1152, 0x08},
  88. {1280, 0x2C},
  89. {1536, 0x2D},
  90. {1792, 0x2E},
  91. {1920, 0x0B},
  92. {2048, 0x2F},
  93. {2304, 0x0C},
  94. {2560, 0x30},
  95. {3072, 0x31},
  96. {3584, 0x32},
  97. {3840, 0x0F},
  98. {4096, 0x33},
  99. {4608, 0x10},
  100. {5120, 0x34},
  101. {6144, 0x35},
  102. {7168, 0x36},
  103. {7680, 0x13},
  104. {8192, 0x37},
  105. {9216, 0x14},
  106. {10240, 0x38},
  107. {12288, 0x39},
  108. {14336, 0x3A},
  109. {15360, 0x17},
  110. {16384, 0x3B},
  111. {18432, 0x18},
  112. {20480, 0x3C},
  113. {24576, 0x3D},
  114. {28672, 0x3E},
  115. {30720, 0x1B},
  116. {32768, 0x3F},
  117. {36864, 0x1C},
  118. {40960, 0x1D},
  119. {49152, 0x1E},
  120. {61440, 0x1F},
  121. {-1, 0x1F}
  122. };
  123. #define NUM_I2C_SPEEDS (sizeof(i2c_speed_map) / sizeof(i2c_speed_map[0]))
  124. static int set_speed(unsigned int speed)
  125. {
  126. unsigned long i2c_clk;
  127. unsigned int divider, i;
  128. u8 fdr = 0x3F;
  129. i2c_clk = (i2c_bus_num == 2) ? gd->i2c2_clk : gd->i2c1_clk;
  130. divider = i2c_clk / speed;
  131. /* Scan i2c_speed_map[] for the closest matching divider.*/
  132. for (i = 0; i < NUM_I2C_SPEEDS-1; i++) {
  133. /* Locate our divider in between two entries in i2c_speed_map[] */
  134. if ((divider >= i2c_speed_map[i].divider) &&
  135. (divider <= i2c_speed_map[i+1].divider)) {
  136. /* Which one is closer? */
  137. if ((divider - i2c_speed_map[i].divider) < (i2c_speed_map[i+1].divider - divider)) {
  138. fdr = i2c_speed_map[i].fdr;
  139. } else {
  140. fdr = i2c_speed_map[i+1].fdr;
  141. }
  142. break;
  143. }
  144. }
  145. writeb(fdr, &i2c_dev->fdr);
  146. i2c_bus_speed[i2c_bus_num - 1] = speed;
  147. return 0;
  148. }
  149. static void _i2c_init(int speed, int slaveadd)
  150. {
  151. /* stop I2C controller */
  152. writeb(0x00 , &i2c_dev->cr);
  153. /* set clock */
  154. set_speed(speed);
  155. /* set default filter */
  156. writeb(IC2_FDR,&i2c_dev->dfsrr);
  157. /* write slave address */
  158. writeb(slaveadd, &i2c_dev->adr);
  159. /* clear status register */
  160. writeb(I2C_CR_MTX, &i2c_dev->sr);
  161. /* start I2C controller */
  162. writeb(I2C_CR_MEN, &i2c_dev->cr);
  163. }
  164. void i2c_init(int speed, int slaveadd)
  165. {
  166. /* Set both interfaces to the same speed and slave address */
  167. /* Note: This function gets called twice - before and after
  168. * relocation to RAM. The first time it's called, we are unable
  169. * to change buses, so whichever one 'i2c_dev' was initialized to
  170. * gets set twice. When run from RAM both buses get set properly */
  171. i2c_set_bus_num(1);
  172. _i2c_init(speed, slaveadd);
  173. #ifdef CFG_I2C2_OFFSET
  174. i2c_set_bus_num(2);
  175. _i2c_init(speed, slaveadd);
  176. i2c_set_bus_num(1);
  177. #endif /* CFG_I2C2_OFFSET */
  178. }
  179. static __inline__ int
  180. i2c_wait4bus (void)
  181. {
  182. ulong timeval = get_timer (0);
  183. while (readb(&i2c_dev->sr) & I2C_SR_MBB) {
  184. if (get_timer (timeval) > I2C_TIMEOUT) {
  185. return -1;
  186. }
  187. }
  188. return 0;
  189. }
  190. static __inline__ int
  191. i2c_wait (int write)
  192. {
  193. u32 csr;
  194. ulong timeval = get_timer(0);
  195. do {
  196. csr = readb(&i2c_dev->sr);
  197. if (!(csr & I2C_SR_MIF))
  198. continue;
  199. writeb(0x0, &i2c_dev->sr);
  200. if (csr & I2C_SR_MAL) {
  201. debug("i2c_wait: MAL\n");
  202. return -1;
  203. }
  204. if (!(csr & I2C_SR_MCF)) {
  205. debug("i2c_wait: unfinished\n");
  206. return -1;
  207. }
  208. if (write == I2C_WRITE && (csr & I2C_SR_RXAK)) {
  209. debug("i2c_wait: No RXACK\n");
  210. return -1;
  211. }
  212. return 0;
  213. } while (get_timer (timeval) < I2C_TIMEOUT);
  214. debug("i2c_wait: timed out\n");
  215. return -1;
  216. }
  217. static __inline__ int
  218. i2c_write_addr (u8 dev, u8 dir, int rsta)
  219. {
  220. writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX |
  221. (rsta?I2C_CR_RSTA:0),
  222. &i2c_dev->cr);
  223. writeb((dev << 1) | dir, &i2c_dev->dr);
  224. if (i2c_wait (I2C_WRITE) < 0)
  225. return 0;
  226. return 1;
  227. }
  228. static __inline__ int
  229. __i2c_write (u8 *data, int length)
  230. {
  231. int i;
  232. writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
  233. &i2c_dev->cr);
  234. for (i=0; i < length; i++) {
  235. writeb(data[i], &i2c_dev->dr);
  236. if (i2c_wait (I2C_WRITE) < 0)
  237. break;
  238. }
  239. return i;
  240. }
  241. static __inline__ int
  242. __i2c_read (u8 *data, int length)
  243. {
  244. int i;
  245. writeb(I2C_CR_MEN | I2C_CR_MSTA |
  246. ((length == 1) ? I2C_CR_TXAK : 0),
  247. &i2c_dev->cr);
  248. /* dummy read */
  249. readb(&i2c_dev->dr);
  250. for (i=0; i < length; i++) {
  251. if (i2c_wait (I2C_READ) < 0)
  252. break;
  253. /* Generate ack on last next to last byte */
  254. if (i == length - 2)
  255. writeb(I2C_CR_MEN | I2C_CR_MSTA |
  256. I2C_CR_TXAK,
  257. &i2c_dev->cr);
  258. /* Generate stop on last byte */
  259. if (i == length - 1)
  260. writeb(I2C_CR_MEN | I2C_CR_TXAK, &i2c_dev->cr);
  261. data[i] = readb(&i2c_dev->dr);
  262. }
  263. return i;
  264. }
  265. int
  266. i2c_read (u8 dev, uint addr, int alen, u8 *data, int length)
  267. {
  268. int i = 0;
  269. u8 *a = (u8*)&addr;
  270. if (i2c_wait4bus () < 0)
  271. goto exit;
  272. if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
  273. goto exit;
  274. if (__i2c_write (&a[4 - alen], alen) != alen)
  275. goto exit;
  276. if (i2c_write_addr (dev, I2C_READ, 1) == 0)
  277. goto exit;
  278. i = __i2c_read (data, length);
  279. exit:
  280. writeb(I2C_CR_MEN, &i2c_dev->cr);
  281. return !(i == length);
  282. }
  283. int
  284. i2c_write (u8 dev, uint addr, int alen, u8 *data, int length)
  285. {
  286. int i = 0;
  287. u8 *a = (u8*)&addr;
  288. if (i2c_wait4bus () < 0)
  289. goto exit;
  290. if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
  291. goto exit;
  292. if (__i2c_write (&a[4 - alen], alen) != alen)
  293. goto exit;
  294. i = __i2c_write (data, length);
  295. exit:
  296. writeb(I2C_CR_MEN, &i2c_dev->cr);
  297. return !(i == length);
  298. }
  299. int i2c_probe (uchar chip)
  300. {
  301. int tmp;
  302. /*
  303. * Try to read the first location of the chip. The underlying
  304. * driver doesn't appear to support sending just the chip address
  305. * and looking for an <ACK> back.
  306. */
  307. udelay(10000);
  308. return i2c_read (chip, 0, 1, (uchar *)&tmp, 1);
  309. }
  310. uchar i2c_reg_read (uchar i2c_addr, uchar reg)
  311. {
  312. uchar buf[1];
  313. i2c_read (i2c_addr, reg, 1, buf, 1);
  314. return (buf[0]);
  315. }
  316. void i2c_reg_write (uchar i2c_addr, uchar reg, uchar val)
  317. {
  318. i2c_write (i2c_addr, reg, 1, &val, 1);
  319. }
  320. int i2c_set_bus_num(unsigned int bus)
  321. {
  322. if(bus == 1)
  323. {
  324. i2c_dev = I2C_1;
  325. }
  326. #ifdef CFG_I2C2_OFFSET
  327. else if(bus == 2)
  328. {
  329. i2c_dev = I2C_2;
  330. }
  331. #endif
  332. else
  333. {
  334. return -1;
  335. }
  336. i2c_bus_num = bus;
  337. return 0;
  338. }
  339. int i2c_set_bus_speed(unsigned int speed)
  340. {
  341. return set_speed(speed);
  342. }
  343. unsigned int i2c_get_bus_num(void)
  344. {
  345. return i2c_bus_num;
  346. }
  347. unsigned int i2c_get_bus_speed(void)
  348. {
  349. return i2c_bus_speed[i2c_bus_num - 1];
  350. }
  351. #endif /* CONFIG_HARD_I2C */