i2c.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. /* Three I2C bus speeds are supported here (50kHz, 100kHz
  47. * and 400kHz). It should be easy to add more. Note that
  48. * the maximum bus speed for I2C bus 1 is CSB/3, while I2C
  49. * bus 2 can go as high as CSB.
  50. * Typical values for CSB are 266MHz and 200MHz. */
  51. /* 50kH 100kHz 400kHz */
  52. static const uchar speed_map_266[][3] =
  53. {{0x2e, 0x2a, 0x20}, /* base 88MHz */
  54. {0x34, 0x30, 0x28}}; /* base 266 MHz */
  55. static const uchar speed_map_200[][3] =
  56. {{0x2c, 0x28, 0x20}, /* base 66 MHz */
  57. {0x33, 0x2f, 0x26}}; /* base 200 MHz */
  58. /* Initialize the bus pointer to whatever one the SPD EEPROM is on.
  59. * Default is bus 1. This is necessary because the DDR initialization
  60. * runs from ROM, and we can't switch buses because we can't modify
  61. * the i2c_dev variable. Everything gets straightened out once i2c_init
  62. * is called from RAM. */
  63. #if defined CFG_SPD_BUS_NUM
  64. static i2c_t *i2c_dev = CFG_SPD_BUS_NUM;
  65. #else
  66. static i2c_t *i2c_dev = I2C_1;
  67. #endif
  68. static uchar busNum = I2C_BUS_1 ;
  69. static int bus_speed[2] = {0, 0};
  70. static int set_speed(int speed)
  71. {
  72. uchar value;
  73. const uchar *spdPtr;
  74. /* Global data contains maximum I2C bus 1 speed, which is CSB/3 */
  75. if(gd->i2c_clk == 88000000)
  76. {
  77. spdPtr = speed_map_266[busNum];
  78. }
  79. else if(gd->i2c_clk == 66000000)
  80. {
  81. spdPtr = speed_map_200[busNum];
  82. }
  83. else
  84. {
  85. printf("Max I2C bus speed %d not supported\n", gd->i2c_clk);
  86. return -1;
  87. }
  88. switch(speed)
  89. {
  90. case 50000:
  91. value = *(spdPtr + 0);
  92. break;
  93. case 100000:
  94. value = *(spdPtr + 1);
  95. break;
  96. case 400000:
  97. value = *(spdPtr + 2);
  98. break;
  99. default:
  100. printf("I2C bus speed %d not supported\n", speed);
  101. return -2;
  102. }
  103. /* set clock */
  104. writeb(value, &i2c_dev->fdr);
  105. bus_speed[busNum] = speed;
  106. return 0;
  107. }
  108. static void _i2c_init(int speed, int slaveadd)
  109. {
  110. /* stop I2C controller */
  111. writeb(0x00 , &i2c_dev->cr);
  112. /* set clock */
  113. writeb(speed, &i2c_dev->fdr);
  114. /* set default filter */
  115. writeb(0x10,&i2c_dev->dfsrr);
  116. /* write slave address */
  117. writeb(slaveadd, &i2c_dev->adr);
  118. /* clear status register */
  119. writeb(0x00, &i2c_dev->sr);
  120. /* start I2C controller */
  121. writeb(I2C_CR_MEN, &i2c_dev->cr);
  122. }
  123. void i2c_init(int speed, int slaveadd)
  124. {
  125. /* Set both interfaces to the same speed and slave address */
  126. /* Note: This function gets called twice - before and after
  127. * relocation to RAM. The first time it's called, we are unable
  128. * to change buses, so whichever one 'i2c_dev' was initialized to
  129. * gets set twice. When run from RAM both buses get set properly */
  130. i2c_set_bus_num(I2C_BUS_1);
  131. _i2c_init(speed, slaveadd);
  132. #ifdef CFG_I2C2_OFFSET
  133. i2c_set_bus_num(I2C_BUS_2);
  134. _i2c_init(speed, slaveadd);
  135. i2c_set_bus_num(I2C_BUS_1);
  136. #endif /* CFG_I2C2_OFFSET */
  137. }
  138. static __inline__ int
  139. i2c_wait4bus (void)
  140. {
  141. ulong timeval = get_timer (0);
  142. while (readb(&i2c_dev->sr) & I2C_SR_MBB) {
  143. if (get_timer (timeval) > I2C_TIMEOUT) {
  144. return -1;
  145. }
  146. }
  147. return 0;
  148. }
  149. static __inline__ int
  150. i2c_wait (int write)
  151. {
  152. u32 csr;
  153. ulong timeval = get_timer(0);
  154. do {
  155. csr = readb(&i2c_dev->sr);
  156. if (!(csr & I2C_SR_MIF))
  157. continue;
  158. writeb(0x0, &i2c_dev->sr);
  159. if (csr & I2C_SR_MAL) {
  160. debug("i2c_wait: MAL\n");
  161. return -1;
  162. }
  163. if (!(csr & I2C_SR_MCF)) {
  164. debug("i2c_wait: unfinished\n");
  165. return -1;
  166. }
  167. if (write == I2C_WRITE && (csr & I2C_SR_RXAK)) {
  168. debug("i2c_wait: No RXACK\n");
  169. return -1;
  170. }
  171. return 0;
  172. } while (get_timer (timeval) < I2C_TIMEOUT);
  173. debug("i2c_wait: timed out\n");
  174. return -1;
  175. }
  176. static __inline__ int
  177. i2c_write_addr (u8 dev, u8 dir, int rsta)
  178. {
  179. writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX |
  180. (rsta?I2C_CR_RSTA:0),
  181. &i2c_dev->cr);
  182. writeb((dev << 1) | dir, &i2c_dev->dr);
  183. if (i2c_wait (I2C_WRITE) < 0)
  184. return 0;
  185. return 1;
  186. }
  187. static __inline__ int
  188. __i2c_write (u8 *data, int length)
  189. {
  190. int i;
  191. writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
  192. &i2c_dev->cr);
  193. for (i=0; i < length; i++) {
  194. writeb(data[i], &i2c_dev->dr);
  195. if (i2c_wait (I2C_WRITE) < 0)
  196. break;
  197. }
  198. return i;
  199. }
  200. static __inline__ int
  201. __i2c_read (u8 *data, int length)
  202. {
  203. int i;
  204. writeb(I2C_CR_MEN | I2C_CR_MSTA |
  205. ((length == 1) ? I2C_CR_TXAK : 0),
  206. &i2c_dev->cr);
  207. /* dummy read */
  208. readb(&i2c_dev->dr);
  209. for (i=0; i < length; i++) {
  210. if (i2c_wait (I2C_READ) < 0)
  211. break;
  212. /* Generate ack on last next to last byte */
  213. if (i == length - 2)
  214. writeb(I2C_CR_MEN | I2C_CR_MSTA |
  215. I2C_CR_TXAK,
  216. &i2c_dev->cr);
  217. /* Generate stop on last byte */
  218. if (i == length - 1)
  219. writeb(I2C_CR_MEN | I2C_CR_TXAK, &i2c_dev->cr);
  220. data[i] = readb(&i2c_dev->dr);
  221. }
  222. return i;
  223. }
  224. int
  225. i2c_read (u8 dev, uint addr, int alen, u8 *data, int length)
  226. {
  227. int i = 0;
  228. u8 *a = (u8*)&addr;
  229. if (i2c_wait4bus () < 0)
  230. goto exit;
  231. if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
  232. goto exit;
  233. if (__i2c_write (&a[4 - alen], alen) != alen)
  234. goto exit;
  235. if (i2c_write_addr (dev, I2C_READ, 1) == 0)
  236. goto exit;
  237. i = __i2c_read (data, length);
  238. exit:
  239. writeb(I2C_CR_MEN, &i2c_dev->cr);
  240. return !(i == length);
  241. }
  242. int
  243. i2c_write (u8 dev, uint addr, int alen, u8 *data, int length)
  244. {
  245. int i = 0;
  246. u8 *a = (u8*)&addr;
  247. if (i2c_wait4bus () < 0)
  248. goto exit;
  249. if (i2c_write_addr (dev, I2C_WRITE, 0) == 0)
  250. goto exit;
  251. if (__i2c_write (&a[4 - alen], alen) != alen)
  252. goto exit;
  253. i = __i2c_write (data, length);
  254. exit:
  255. writeb(I2C_CR_MEN, &i2c_dev->cr);
  256. return !(i == length);
  257. }
  258. int i2c_probe (uchar chip)
  259. {
  260. int tmp;
  261. /*
  262. * Try to read the first location of the chip. The underlying
  263. * driver doesn't appear to support sending just the chip address
  264. * and looking for an <ACK> back.
  265. */
  266. udelay(10000);
  267. return i2c_read (chip, 0, 1, (uchar *)&tmp, 1);
  268. }
  269. uchar i2c_reg_read (uchar i2c_addr, uchar reg)
  270. {
  271. uchar buf[1];
  272. i2c_read (i2c_addr, reg, 1, buf, 1);
  273. return (buf[0]);
  274. }
  275. void i2c_reg_write (uchar i2c_addr, uchar reg, uchar val)
  276. {
  277. i2c_write (i2c_addr, reg, 1, &val, 1);
  278. }
  279. int i2c_set_bus_num(uchar bus)
  280. {
  281. if(bus == I2C_BUS_1)
  282. {
  283. i2c_dev = I2C_1;
  284. }
  285. #ifdef CFG_I2C2_OFFSET
  286. else if(bus == I2C_BUS_2)
  287. {
  288. i2c_dev = I2C_2;
  289. }
  290. #endif /* CFG_I2C2_OFFSET */
  291. else
  292. {
  293. return -1;
  294. }
  295. busNum = bus;
  296. return 0;
  297. }
  298. int i2c_set_bus_speed(int speed)
  299. {
  300. return set_speed(speed);
  301. }
  302. uchar i2c_get_bus_num(void)
  303. {
  304. return busNum;
  305. }
  306. int i2c_get_bus_speed(void)
  307. {
  308. return bus_speed[busNum];
  309. }
  310. #endif /* CONFIG_HARD_I2C */