i2c.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * (C) Copyright 2003 - 2007
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  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. * Based on the MPC5xxx code.
  24. */
  25. #include <common.h>
  26. DECLARE_GLOBAL_DATA_PTR;
  27. #ifdef CONFIG_HARD_I2C
  28. #include <mpc512x.h>
  29. #include <i2c.h>
  30. #define immr ((immap_t *)CFG_IMMR)
  31. /* by default set I2C bus 0 active */
  32. static unsigned int bus_num = 0;
  33. #define I2C_TIMEOUT 100
  34. #define I2C_RETRIES 3
  35. struct mpc512x_i2c_tap {
  36. int scl2tap;
  37. int tap2tap;
  38. };
  39. static int mpc_reg_in(volatile u32 *reg);
  40. static void mpc_reg_out(volatile u32 *reg, int val, int mask);
  41. static int wait_for_bb(void);
  42. static int wait_for_pin(int *status);
  43. static int do_address(uchar chip, char rdwr_flag);
  44. static int send_bytes(uchar chip, char *buf, int len);
  45. static int receive_bytes(uchar chip, char *buf, int len);
  46. static int mpc_get_fdr(int);
  47. static int mpc_reg_in (volatile u32 *reg)
  48. {
  49. int ret = *reg >> 24;
  50. __asm__ __volatile__ ("eieio");
  51. return ret;
  52. }
  53. static void mpc_reg_out (volatile u32 *reg, int val, int mask)
  54. {
  55. int tmp;
  56. if (!mask) {
  57. *reg = val << 24;
  58. } else {
  59. tmp = mpc_reg_in (reg);
  60. *reg = ((tmp & ~mask) | (val & mask)) << 24;
  61. }
  62. __asm__ __volatile__ ("eieio");
  63. return;
  64. }
  65. static int wait_for_bb (void)
  66. {
  67. i2c512x_dev_t *regs = &immr->i2c.dev[bus_num];
  68. int timeout = I2C_TIMEOUT;
  69. int status;
  70. status = mpc_reg_in (&regs->msr);
  71. while (timeout-- && (status & I2C_BB)) {
  72. volatile int temp;
  73. mpc_reg_out (&regs->mcr, I2C_STA, I2C_STA);
  74. temp = mpc_reg_in (&regs->mdr);
  75. mpc_reg_out (&regs->mcr, 0, I2C_STA);
  76. mpc_reg_out (&regs->mcr, 0, 0);
  77. mpc_reg_out (&regs->mcr, I2C_EN, 0);
  78. udelay (1000);
  79. status = mpc_reg_in (&regs->msr);
  80. }
  81. return (status & I2C_BB);
  82. }
  83. static int wait_for_pin (int *status)
  84. {
  85. i2c512x_dev_t *regs = &immr->i2c.dev[bus_num];
  86. int timeout = I2C_TIMEOUT;
  87. *status = mpc_reg_in (&regs->msr);
  88. while (timeout-- && !(*status & I2C_IF)) {
  89. udelay (1000);
  90. *status = mpc_reg_in (&regs->msr);
  91. }
  92. if (!(*status & I2C_IF)) {
  93. return -1;
  94. }
  95. mpc_reg_out (&regs->msr, 0, I2C_IF);
  96. return 0;
  97. }
  98. static int do_address (uchar chip, char rdwr_flag)
  99. {
  100. i2c512x_dev_t *regs = &immr->i2c.dev[bus_num];
  101. int status;
  102. chip <<= 1;
  103. if (rdwr_flag) {
  104. chip |= 1;
  105. }
  106. mpc_reg_out (&regs->mcr, I2C_TX, I2C_TX);
  107. mpc_reg_out (&regs->mdr, chip, 0);
  108. if (wait_for_pin (&status)) {
  109. return -2;
  110. }
  111. if (status & I2C_RXAK) {
  112. return -3;
  113. }
  114. return 0;
  115. }
  116. static int send_bytes (uchar chip, char *buf, int len)
  117. {
  118. i2c512x_dev_t *regs = &immr->i2c.dev[bus_num];
  119. int wrcount;
  120. int status;
  121. for (wrcount = 0; wrcount < len; ++wrcount) {
  122. mpc_reg_out (&regs->mdr, buf[wrcount], 0);
  123. if (wait_for_pin (&status)) {
  124. break;
  125. }
  126. if (status & I2C_RXAK) {
  127. break;
  128. }
  129. }
  130. return !(wrcount == len);
  131. }
  132. static int receive_bytes (uchar chip, char *buf, int len)
  133. {
  134. i2c512x_dev_t *regs = &immr->i2c.dev[bus_num];
  135. int dummy = 1;
  136. int rdcount = 0;
  137. int status;
  138. int i;
  139. mpc_reg_out (&regs->mcr, 0, I2C_TX);
  140. for (i = 0; i < len; ++i) {
  141. buf[rdcount] = mpc_reg_in (&regs->mdr);
  142. if (dummy) {
  143. dummy = 0;
  144. } else {
  145. rdcount++;
  146. }
  147. if (wait_for_pin (&status)) {
  148. return -4;
  149. }
  150. }
  151. mpc_reg_out (&regs->mcr, I2C_TXAK, I2C_TXAK);
  152. buf[rdcount++] = mpc_reg_in (&regs->mdr);
  153. if (wait_for_pin (&status)) {
  154. return -5;
  155. }
  156. mpc_reg_out (&regs->mcr, 0, I2C_TXAK);
  157. return 0;
  158. }
  159. /**************** I2C API ****************/
  160. void i2c_init (int speed, int saddr)
  161. {
  162. int i;
  163. for(i = 0; i < I2C_BUS_CNT; i++){
  164. i2c512x_dev_t *regs = &immr->i2c.dev[i];
  165. mpc_reg_out (&regs->mcr, 0, 0);
  166. /* Set clock */
  167. mpc_reg_out (&regs->mfdr, mpc_get_fdr (speed), 0);
  168. mpc_reg_out (&regs->madr, saddr << 1, 0);
  169. /* Enable module */
  170. mpc_reg_out (&regs->mcr, I2C_EN, I2C_INIT_MASK);
  171. mpc_reg_out (&regs->msr, 0, I2C_IF);
  172. }
  173. /* Disable interrupts */
  174. immr->i2c.icr = 0;
  175. /* Turn off filters */
  176. immr->i2c.mifr = 0;
  177. return;
  178. }
  179. static int mpc_get_fdr (int speed)
  180. {
  181. static int fdr = -1;
  182. if (fdr == -1) {
  183. ulong best_speed = 0;
  184. ulong divider;
  185. ulong ips, scl;
  186. ulong bestmatch = 0xffffffffUL;
  187. int best_i = 0, best_j = 0, i, j;
  188. int SCL_Tap[] = { 9, 10, 12, 15, 5, 6, 7, 8};
  189. struct mpc512x_i2c_tap scltap[] = {
  190. {4, 1},
  191. {4, 2},
  192. {6, 4},
  193. {6, 8},
  194. {14, 16},
  195. {30, 32},
  196. {62, 64},
  197. {126, 128}
  198. };
  199. ips = gd->ips_clk;
  200. for (i = 7; i >= 0; i--) {
  201. for (j = 7; j >= 0; j--) {
  202. scl = 2 * (scltap[j].scl2tap +
  203. (SCL_Tap[i] - 1) * scltap[j].tap2tap
  204. + 2);
  205. if (ips <= speed*scl) {
  206. if ((speed*scl - ips) < bestmatch) {
  207. bestmatch = speed*scl - ips;
  208. best_i = i;
  209. best_j = j;
  210. best_speed = ips/scl;
  211. }
  212. }
  213. }
  214. }
  215. divider = (best_i & 3) | ((best_i & 4) << 3) | (best_j << 2);
  216. if (gd->flags & GD_FLG_RELOC) {
  217. fdr = divider;
  218. } else {
  219. debug("%ld kHz, \n", best_speed / 1000);
  220. return divider;
  221. }
  222. }
  223. return fdr;
  224. }
  225. int i2c_probe (uchar chip)
  226. {
  227. i2c512x_dev_t *regs = &immr->i2c.dev[bus_num];
  228. int i;
  229. for (i = 0; i < I2C_RETRIES; i++) {
  230. mpc_reg_out (&regs->mcr, I2C_STA, I2C_STA);
  231. if (! do_address (chip, 0)) {
  232. mpc_reg_out (&regs->mcr, 0, I2C_STA);
  233. udelay (500);
  234. break;
  235. }
  236. mpc_reg_out (&regs->mcr, 0, I2C_STA);
  237. udelay (500);
  238. }
  239. return (i == I2C_RETRIES);
  240. }
  241. int i2c_read (uchar chip, uint addr, int alen, uchar *buf, int len)
  242. {
  243. char xaddr[4];
  244. i2c512x_dev_t *regs = &immr->i2c.dev[bus_num];
  245. int ret = -1;
  246. xaddr[0] = (addr >> 24) & 0xFF;
  247. xaddr[1] = (addr >> 16) & 0xFF;
  248. xaddr[2] = (addr >> 8) & 0xFF;
  249. xaddr[3] = addr & 0xFF;
  250. if (wait_for_bb ()) {
  251. printf ("i2c_read: bus is busy\n");
  252. goto Done;
  253. }
  254. mpc_reg_out (&regs->mcr, I2C_STA, I2C_STA);
  255. if (do_address (chip, 0)) {
  256. printf ("i2c_read: failed to address chip\n");
  257. goto Done;
  258. }
  259. if (send_bytes (chip, &xaddr[4-alen], alen)) {
  260. printf ("i2c_read: send_bytes failed\n");
  261. goto Done;
  262. }
  263. mpc_reg_out (&regs->mcr, I2C_RSTA, I2C_RSTA);
  264. if (do_address (chip, 1)) {
  265. printf ("i2c_read: failed to address chip\n");
  266. goto Done;
  267. }
  268. if (receive_bytes (chip, (char *)buf, len)) {
  269. printf ("i2c_read: receive_bytes failed\n");
  270. goto Done;
  271. }
  272. ret = 0;
  273. Done:
  274. mpc_reg_out (&regs->mcr, 0, I2C_STA);
  275. return ret;
  276. }
  277. int i2c_write (uchar chip, uint addr, int alen, uchar *buf, int len)
  278. {
  279. char xaddr[4];
  280. i2c512x_dev_t *regs = &immr->i2c.dev[bus_num];
  281. int ret = -1;
  282. xaddr[0] = (addr >> 24) & 0xFF;
  283. xaddr[1] = (addr >> 16) & 0xFF;
  284. xaddr[2] = (addr >> 8) & 0xFF;
  285. xaddr[3] = addr & 0xFF;
  286. if (wait_for_bb ()) {
  287. printf ("i2c_write: bus is busy\n");
  288. goto Done;
  289. }
  290. mpc_reg_out (&regs->mcr, I2C_STA, I2C_STA);
  291. if (do_address (chip, 0)) {
  292. printf ("i2c_write: failed to address chip\n");
  293. goto Done;
  294. }
  295. if (send_bytes (chip, &xaddr[4-alen], alen)) {
  296. printf ("i2c_write: send_bytes failed\n");
  297. goto Done;
  298. }
  299. if (send_bytes (chip, (char *)buf, len)) {
  300. printf ("i2c_write: send_bytes failed\n");
  301. goto Done;
  302. }
  303. ret = 0;
  304. Done:
  305. mpc_reg_out (&regs->mcr, 0, I2C_STA);
  306. return ret;
  307. }
  308. uchar i2c_reg_read (uchar chip, uchar reg)
  309. {
  310. uchar buf;
  311. i2c_read (chip, reg, 1, &buf, 1);
  312. return buf;
  313. }
  314. void i2c_reg_write (uchar chip, uchar reg, uchar val)
  315. {
  316. i2c_write (chip, reg, 1, &val, 1);
  317. return;
  318. }
  319. int i2c_set_bus_num (unsigned int bus)
  320. {
  321. if (bus >= I2C_BUS_CNT) {
  322. return -1;
  323. }
  324. bus_num = bus;
  325. return 0;
  326. }
  327. unsigned int i2c_get_bus_num (void)
  328. {
  329. return bus_num;
  330. }
  331. /* TODO */
  332. unsigned int i2c_get_bus_speed (void)
  333. {
  334. return -1;
  335. }
  336. int i2c_set_bus_speed (unsigned int speed)
  337. {
  338. if (speed != CFG_I2C_SPEED)
  339. return -1;
  340. return 0;
  341. }
  342. #endif /* CONFIG_HARD_I2C */