i2c.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * (C) Copyright 2004, Freescale, Inc
  3. * TsiChung Liew, Tsi-Chung.Liew@freescale.com.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #ifdef CONFIG_HARD_I2C
  25. #include <mpc8220.h>
  26. #include <i2c.h>
  27. typedef struct mpc8220_i2c {
  28. volatile u32 adr; /* I2Cn + 0x00 */
  29. volatile u32 fdr; /* I2Cn + 0x04 */
  30. volatile u32 cr; /* I2Cn + 0x08 */
  31. volatile u32 sr; /* I2Cn + 0x0C */
  32. volatile u32 dr; /* I2Cn + 0x10 */
  33. } i2c_t;
  34. /* I2Cn control register bits */
  35. #define I2C_EN 0x80
  36. #define I2C_IEN 0x40
  37. #define I2C_STA 0x20
  38. #define I2C_TX 0x10
  39. #define I2C_TXAK 0x08
  40. #define I2C_RSTA 0x04
  41. #define I2C_INIT_MASK (I2C_EN | I2C_STA | I2C_TX | I2C_RSTA)
  42. /* I2Cn status register bits */
  43. #define I2C_CF 0x80
  44. #define I2C_AAS 0x40
  45. #define I2C_BB 0x20
  46. #define I2C_AL 0x10
  47. #define I2C_SRW 0x04
  48. #define I2C_IF 0x02
  49. #define I2C_RXAK 0x01
  50. #define I2C_TIMEOUT 100
  51. #define I2C_RETRIES 1
  52. struct mpc8220_i2c_tap {
  53. int scl2tap;
  54. int tap2tap;
  55. };
  56. static int mpc_reg_in (volatile u32 * reg);
  57. static void mpc_reg_out (volatile u32 * reg, int val, int mask);
  58. static int wait_for_bb (void);
  59. static int wait_for_pin (int *status);
  60. static int do_address (uchar chip, char rdwr_flag);
  61. static int send_bytes (uchar chip, char *buf, int len);
  62. static int receive_bytes (uchar chip, char *buf, int len);
  63. static int mpc_get_fdr (int);
  64. static int mpc_reg_in (volatile u32 * reg)
  65. {
  66. int ret;
  67. ret = *reg >> 24;
  68. __asm__ __volatile__ ("eieio");
  69. return ret;
  70. }
  71. static void mpc_reg_out (volatile u32 * reg, int val, int mask)
  72. {
  73. int tmp;
  74. if (!mask) {
  75. *reg = val << 24;
  76. } else {
  77. tmp = mpc_reg_in (reg);
  78. *reg = ((tmp & ~mask) | (val & mask)) << 24;
  79. }
  80. __asm__ __volatile__ ("eieio");
  81. return;
  82. }
  83. static int wait_for_bb (void)
  84. {
  85. i2c_t *regs = (i2c_t *) MMAP_I2C;
  86. int timeout = I2C_TIMEOUT;
  87. int status;
  88. status = mpc_reg_in (&regs->sr);
  89. while (timeout-- && (status & I2C_BB)) {
  90. #if 1
  91. volatile int temp;
  92. mpc_reg_out (&regs->cr, I2C_STA, I2C_STA);
  93. temp = mpc_reg_in (&regs->dr);
  94. mpc_reg_out (&regs->cr, 0, I2C_STA);
  95. mpc_reg_out (&regs->cr, 0, 0);
  96. mpc_reg_out (&regs->cr, I2C_EN, 0);
  97. #endif
  98. udelay (1000);
  99. status = mpc_reg_in (&regs->sr);
  100. }
  101. return (status & I2C_BB);
  102. }
  103. static int wait_for_pin (int *status)
  104. {
  105. i2c_t *regs = (i2c_t *) MMAP_I2C;
  106. int timeout = I2C_TIMEOUT;
  107. *status = mpc_reg_in (&regs->sr);
  108. while (timeout-- && !(*status & I2C_IF)) {
  109. udelay (1000);
  110. *status = mpc_reg_in (&regs->sr);
  111. }
  112. if (!(*status & I2C_IF)) {
  113. return -1;
  114. }
  115. mpc_reg_out (&regs->sr, 0, I2C_IF);
  116. return 0;
  117. }
  118. static int do_address (uchar chip, char rdwr_flag)
  119. {
  120. i2c_t *regs = (i2c_t *) MMAP_I2C;
  121. int status;
  122. chip <<= 1;
  123. if (rdwr_flag)
  124. chip |= 1;
  125. mpc_reg_out (&regs->cr, I2C_TX, I2C_TX);
  126. mpc_reg_out (&regs->dr, chip, 0);
  127. if (wait_for_pin (&status))
  128. return -2;
  129. if (status & I2C_RXAK)
  130. return -3;
  131. return 0;
  132. }
  133. static int send_bytes (uchar chip, char *buf, int len)
  134. {
  135. i2c_t *regs = (i2c_t *) MMAP_I2C;
  136. int wrcount;
  137. int status;
  138. for (wrcount = 0; wrcount < len; ++wrcount) {
  139. mpc_reg_out (&regs->dr, buf[wrcount], 0);
  140. if (wait_for_pin (&status))
  141. break;
  142. if (status & I2C_RXAK)
  143. break;
  144. }
  145. return !(wrcount == len);
  146. return 0;
  147. }
  148. static int receive_bytes (uchar chip, char *buf, int len)
  149. {
  150. i2c_t *regs = (i2c_t *) MMAP_I2C;
  151. int dummy = 1;
  152. int rdcount = 0;
  153. int status;
  154. int i;
  155. mpc_reg_out (&regs->cr, 0, I2C_TX);
  156. for (i = 0; i < len; ++i) {
  157. buf[rdcount] = mpc_reg_in (&regs->dr);
  158. if (dummy)
  159. dummy = 0;
  160. else
  161. rdcount++;
  162. if (wait_for_pin (&status))
  163. return -4;
  164. }
  165. mpc_reg_out (&regs->cr, I2C_TXAK, I2C_TXAK);
  166. buf[rdcount++] = mpc_reg_in (&regs->dr);
  167. if (wait_for_pin (&status))
  168. return -5;
  169. mpc_reg_out (&regs->cr, 0, I2C_TXAK);
  170. return 0;
  171. }
  172. /**************** I2C API ****************/
  173. void i2c_init (int speed, int saddr)
  174. {
  175. i2c_t *regs = (i2c_t *) MMAP_I2C;
  176. mpc_reg_out (&regs->cr, 0, 0);
  177. mpc_reg_out (&regs->adr, saddr << 1, 0);
  178. /* Set clock
  179. */
  180. mpc_reg_out (&regs->fdr, mpc_get_fdr (speed), 0);
  181. /* Enable module
  182. */
  183. mpc_reg_out (&regs->cr, I2C_EN, I2C_INIT_MASK);
  184. mpc_reg_out (&regs->sr, 0, I2C_IF);
  185. return;
  186. }
  187. static int mpc_get_fdr (int speed)
  188. {
  189. DECLARE_GLOBAL_DATA_PTR;
  190. static int fdr = -1;
  191. if (fdr == -1) {
  192. ulong best_speed = 0;
  193. ulong divider;
  194. ulong ipb, scl;
  195. ulong bestmatch = 0xffffffffUL;
  196. int best_i = 0, best_j = 0, i, j;
  197. int SCL_Tap[] = { 9, 10, 12, 15, 5, 6, 7, 8 };
  198. struct mpc8220_i2c_tap scltap[] = {
  199. {4, 1},
  200. {4, 2},
  201. {6, 4},
  202. {6, 8},
  203. {14, 16},
  204. {30, 32},
  205. {62, 64},
  206. {126, 128}
  207. };
  208. ipb = gd->bus_clk;
  209. for (i = 7; i >= 0; i--) {
  210. for (j = 7; j >= 0; j--) {
  211. scl = 2 * (scltap[j].scl2tap +
  212. (SCL_Tap[i] -
  213. 1) * scltap[j].tap2tap + 2);
  214. if (ipb <= speed * scl) {
  215. if ((speed * scl - ipb) < bestmatch) {
  216. bestmatch = speed * scl - ipb;
  217. best_i = i;
  218. best_j = j;
  219. best_speed = ipb / scl;
  220. }
  221. }
  222. }
  223. }
  224. divider = (best_i & 3) | ((best_i & 4) << 3) | (best_j << 2);
  225. if (gd->flags & GD_FLG_RELOC) {
  226. fdr = divider;
  227. } else {
  228. printf ("%ld kHz, ", best_speed / 1000);
  229. return divider;
  230. }
  231. }
  232. return fdr;
  233. }
  234. int i2c_probe (uchar chip)
  235. {
  236. i2c_t *regs = (i2c_t *) MMAP_I2C;
  237. int i;
  238. for (i = 0; i < I2C_RETRIES; i++) {
  239. mpc_reg_out (&regs->cr, I2C_STA, I2C_STA);
  240. if (!do_address (chip, 0)) {
  241. mpc_reg_out (&regs->cr, 0, I2C_STA);
  242. break;
  243. }
  244. mpc_reg_out (&regs->cr, 0, I2C_STA);
  245. udelay (50);
  246. }
  247. return (i == I2C_RETRIES);
  248. }
  249. int i2c_read (uchar chip, uint addr, int alen, uchar * buf, int len)
  250. {
  251. uchar xaddr[4];
  252. i2c_t *regs = (i2c_t *) MMAP_I2C;
  253. int ret = -1;
  254. xaddr[0] = (addr >> 24) & 0xFF;
  255. xaddr[1] = (addr >> 16) & 0xFF;
  256. xaddr[2] = (addr >> 8) & 0xFF;
  257. xaddr[3] = addr & 0xFF;
  258. if (wait_for_bb ()) {
  259. printf ("i2c_read: bus is busy\n");
  260. goto Done;
  261. }
  262. mpc_reg_out (&regs->cr, I2C_STA, I2C_STA);
  263. if (do_address (chip, 0)) {
  264. printf ("i2c_read: failed to address chip\n");
  265. goto Done;
  266. }
  267. if (send_bytes (chip, (char *)&xaddr[4 - alen], alen)) {
  268. printf ("i2c_read: send_bytes failed\n");
  269. goto Done;
  270. }
  271. mpc_reg_out (&regs->cr, I2C_RSTA, I2C_RSTA);
  272. if (do_address (chip, 1)) {
  273. printf ("i2c_read: failed to address chip\n");
  274. goto Done;
  275. }
  276. if (receive_bytes (chip, (char *)buf, len)) {
  277. printf ("i2c_read: receive_bytes failed\n");
  278. goto Done;
  279. }
  280. ret = 0;
  281. Done:
  282. mpc_reg_out (&regs->cr, 0, I2C_STA);
  283. return ret;
  284. }
  285. int i2c_write (uchar chip, uint addr, int alen, uchar * buf, int len)
  286. {
  287. uchar xaddr[4];
  288. i2c_t *regs = (i2c_t *) MMAP_I2C;
  289. int ret = -1;
  290. xaddr[0] = (addr >> 24) & 0xFF;
  291. xaddr[1] = (addr >> 16) & 0xFF;
  292. xaddr[2] = (addr >> 8) & 0xFF;
  293. xaddr[3] = addr & 0xFF;
  294. if (wait_for_bb ()) {
  295. printf ("i2c_write: bus is busy\n");
  296. goto Done;
  297. }
  298. mpc_reg_out (&regs->cr, I2C_STA, I2C_STA);
  299. if (do_address (chip, 0)) {
  300. printf ("i2c_write: failed to address chip\n");
  301. goto Done;
  302. }
  303. if (send_bytes (chip, (char *)&xaddr[4 - alen], alen)) {
  304. printf ("i2c_write: send_bytes failed\n");
  305. goto Done;
  306. }
  307. if (send_bytes (chip, (char *)buf, len)) {
  308. printf ("i2c_write: send_bytes failed\n");
  309. goto Done;
  310. }
  311. ret = 0;
  312. Done:
  313. mpc_reg_out (&regs->cr, 0, I2C_STA);
  314. return ret;
  315. }
  316. uchar i2c_reg_read (uchar chip, uchar reg)
  317. {
  318. uchar buf;
  319. i2c_read (chip, reg, 1, &buf, 1);
  320. return buf;
  321. }
  322. void i2c_reg_write (uchar chip, uchar reg, uchar val)
  323. {
  324. i2c_write (chip, reg, 1, &val, 1);
  325. return;
  326. }
  327. #endif /* CONFIG_HARD_I2C */