iic.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* Minimal support functions to read configuration from IIC EEPROMS
  2. * on MPC8xx boards. Originally written for RPGC RPX-Lite.
  3. * Dan Malek (dmalek@jlc.net).
  4. */
  5. #include <linux/types.h>
  6. #include <asm/uaccess.h>
  7. #include <asm/mpc8xx.h>
  8. #include <asm/commproc.h>
  9. /* IIC functions.
  10. * These are just the basic master read/write operations so we can
  11. * examine serial EEPROM.
  12. */
  13. void iic_read(uint devaddr, u_char *buf, uint offset, uint count);
  14. static int iic_init_done;
  15. static void
  16. iic_init(void)
  17. {
  18. volatile iic_t *iip;
  19. volatile i2c8xx_t *i2c;
  20. volatile cpm8xx_t *cp;
  21. volatile immap_t *immap;
  22. uint dpaddr;
  23. immap = (immap_t *)IMAP_ADDR;
  24. cp = (cpm8xx_t *)&(immap->im_cpm);
  25. /* Reset the CPM. This is necessary on the 860 processors
  26. * that may have started the SCC1 ethernet without relocating
  27. * the IIC.
  28. * This also stops the Ethernet in case we were loaded by a
  29. * BOOTP rom monitor.
  30. */
  31. cp->cp_cpcr = (CPM_CR_RST | CPM_CR_FLG);
  32. /* Wait for it.
  33. */
  34. while (cp->cp_cpcr & (CPM_CR_RST | CPM_CR_FLG));
  35. /* Remove any microcode patches. We will install our own
  36. * later.
  37. */
  38. cp->cp_cpmcr1 = 0;
  39. cp->cp_cpmcr2 = 0;
  40. cp->cp_cpmcr3 = 0;
  41. cp->cp_cpmcr4 = 0;
  42. cp->cp_rccr = 0;
  43. iip = (iic_t *)&cp->cp_dparam[PROFF_IIC];
  44. i2c = (i2c8xx_t *)&(immap->im_i2c);
  45. /* Initialize Port B IIC pins.
  46. */
  47. cp->cp_pbpar |= 0x00000030;
  48. cp->cp_pbdir |= 0x00000030;
  49. cp->cp_pbodr |= 0x00000030;
  50. /* Initialize the parameter ram.
  51. */
  52. /* Allocate space for a two transmit and one receive buffer
  53. * descriptor in the DP ram.
  54. * For now, this address seems OK, but it may have to
  55. * change with newer versions of the firmware.
  56. */
  57. dpaddr = 0x0840;
  58. /* Set up the IIC parameters in the parameter ram.
  59. */
  60. iip->iic_tbase = dpaddr;
  61. iip->iic_rbase = dpaddr + (2 * sizeof(cbd_t));
  62. iip->iic_tfcr = SMC_EB;
  63. iip->iic_rfcr = SMC_EB;
  64. /* This should really be done by the reader/writer.
  65. */
  66. iip->iic_mrblr = 128;
  67. /* Initialize Tx/Rx parameters.
  68. */
  69. cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_I2C, CPM_CR_INIT_TRX) | CPM_CR_FLG;
  70. while (cp->cp_cpcr & CPM_CR_FLG);
  71. /* Select an arbitrary address. Just make sure it is unique.
  72. */
  73. i2c->i2c_i2add = 0x34;
  74. /* Make clock run maximum slow.
  75. */
  76. i2c->i2c_i2brg = 7;
  77. /* Disable interrupts.
  78. */
  79. i2c->i2c_i2cmr = 0;
  80. i2c->i2c_i2cer = 0xff;
  81. /* Enable SDMA.
  82. */
  83. immap->im_siu_conf.sc_sdcr = 1;
  84. iic_init_done = 1;
  85. }
  86. /* Read from IIC.
  87. * Caller provides device address, memory buffer, and byte count.
  88. */
  89. static u_char iitemp[32];
  90. void
  91. iic_read(uint devaddr, u_char *buf, uint offset, uint count)
  92. {
  93. volatile iic_t *iip;
  94. volatile i2c8xx_t *i2c;
  95. volatile cbd_t *tbdf, *rbdf;
  96. volatile cpm8xx_t *cp;
  97. volatile immap_t *immap;
  98. u_char *tb;
  99. uint temp;
  100. /* If the interface has not been initialized, do that now.
  101. */
  102. if (!iic_init_done)
  103. iic_init();
  104. immap = (immap_t *)IMAP_ADDR;
  105. cp = (cpm8xx_t *)&(immap->im_cpm);
  106. iip = (iic_t *)&cp->cp_dparam[PROFF_IIC];
  107. i2c = (i2c8xx_t *)&(immap->im_i2c);
  108. tbdf = (cbd_t *)&cp->cp_dpmem[iip->iic_tbase];
  109. rbdf = (cbd_t *)&cp->cp_dpmem[iip->iic_rbase];
  110. /* Send a "dummy write" operation. This is a write request with
  111. * only the offset sent, followed by another start condition.
  112. * This will ensure we start reading from the first location
  113. * of the EEPROM.
  114. */
  115. tb = iitemp;
  116. tb = (u_char *)(((uint)tb + 15) & ~15);
  117. tbdf->cbd_bufaddr = (int)tb;
  118. *tb = devaddr & 0xfe; /* Device address */
  119. *(tb+1) = offset; /* Offset */
  120. tbdf->cbd_datlen = 2; /* Length */
  121. tbdf->cbd_sc =
  122. BD_SC_READY | BD_SC_LAST | BD_SC_WRAP | BD_IIC_START;
  123. i2c->i2c_i2mod = 1; /* Enable */
  124. i2c->i2c_i2cer = 0xff;
  125. i2c->i2c_i2com = 0x81; /* Start master */
  126. /* Wait for IIC transfer.
  127. */
  128. #if 0
  129. while ((i2c->i2c_i2cer & 3) == 0);
  130. if (tbdf->cbd_sc & BD_SC_READY)
  131. printf("IIC ra complete but tbuf ready\n");
  132. #else
  133. temp = 10000000;
  134. while ((tbdf->cbd_sc & BD_SC_READY) && (temp != 0))
  135. temp--;
  136. #if 0
  137. /* We can't do this...there is no serial port yet!
  138. */
  139. if (temp == 0) {
  140. printf("Timeout reading EEPROM\n");
  141. return;
  142. }
  143. #endif
  144. #endif
  145. /* Chip errata, clear enable.
  146. */
  147. i2c->i2c_i2mod = 0;
  148. /* To read, we need an empty buffer of the proper length.
  149. * All that is used is the first byte for address, the remainder
  150. * is just used for timing (and doesn't really have to exist).
  151. */
  152. tbdf->cbd_bufaddr = (int)tb;
  153. *tb = devaddr | 1; /* Device address */
  154. rbdf->cbd_bufaddr = (uint)buf; /* Desination buffer */
  155. tbdf->cbd_datlen = rbdf->cbd_datlen = count + 1; /* Length */
  156. tbdf->cbd_sc = BD_SC_READY | BD_SC_LAST | BD_SC_WRAP | BD_IIC_START;
  157. rbdf->cbd_sc = BD_SC_EMPTY | BD_SC_WRAP;
  158. /* Chip bug, set enable here.
  159. */
  160. i2c->i2c_i2mod = 1; /* Enable */
  161. i2c->i2c_i2cer = 0xff;
  162. i2c->i2c_i2com = 0x81; /* Start master */
  163. /* Wait for IIC transfer.
  164. */
  165. #if 0
  166. while ((i2c->i2c_i2cer & 1) == 0);
  167. if (rbdf->cbd_sc & BD_SC_EMPTY)
  168. printf("IIC read complete but rbuf empty\n");
  169. #else
  170. temp = 10000000;
  171. while ((tbdf->cbd_sc & BD_SC_READY) && (temp != 0))
  172. temp--;
  173. #endif
  174. /* Chip errata, clear enable.
  175. */
  176. i2c->i2c_i2mod = 0;
  177. }