vpd.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * (C) Copyright 2000
  3. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  4. * Marius Groeger <mgroeger@sysgo.de>
  5. *
  6. * Code in faintly related to linux/arch/ppc/8xx_io:
  7. * MPC8xx CPM I2C interface. Copyright (c) 1999 Dan Malek (dmalek@jlc.net).
  8. *
  9. * This file implements functions to read the MBX's Vital Product Data
  10. * (VPD). I can't use the more general i2c code in mpc8xx/... since I need
  11. * the VPD at a time where there is no RAM available yet. Hence the VPD is
  12. * read into a special area in the DPRAM (see config_MBX.h::CFG_DPRAMVPD).
  13. *
  14. * -----------------------------------------------------------------
  15. * See file CREDITS for list of people who contributed to this
  16. * project.
  17. *
  18. * This program is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU General Public License as
  20. * published by the Free Software Foundation; either version 2 of
  21. * the License, or (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program; if not, write to the Free Software
  30. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  31. * MA 02111-1307 USA
  32. */
  33. #include <common.h>
  34. #ifdef CONFIG_8xx
  35. #include <commproc.h>
  36. #endif
  37. #include "vpd.h"
  38. /* Location of receive/transmit buffer descriptors
  39. * Allocate one transmit bd and one receive bd.
  40. * IIC_BD_FREE points to free bd space which we'll use as tx buffer.
  41. */
  42. #define IIC_BD_TX1 (BD_IIC_START + 0*sizeof(cbd_t))
  43. #define IIC_BD_TX2 (BD_IIC_START + 1*sizeof(cbd_t))
  44. #define IIC_BD_RX (BD_IIC_START + 2*sizeof(cbd_t))
  45. #define IIC_BD_FREE (BD_IIC_START + 3*sizeof(cbd_t))
  46. /* FIXME -- replace 0x2000 with offsetof */
  47. #define VPD_P ((vpd_t *)(CONFIG_SYS_IMMR + 0x2000 + CONFIG_SYS_DPRAMVPD))
  48. /* transmit/receive buffers */
  49. #define IIC_RX_LENGTH 128
  50. #define WITH_MICROCODE_PATCH
  51. vpd_packet_t * vpd_find_packet(u_char ident)
  52. {
  53. vpd_packet_t *packet;
  54. vpd_t *vpd = VPD_P;
  55. packet = (vpd_packet_t *)&vpd->packets;
  56. while ((packet->identifier != ident) && packet->identifier != 0xFF)
  57. {
  58. packet = (vpd_packet_t *)((char *)packet + packet->size + 2);
  59. }
  60. return packet;
  61. }
  62. void vpd_init(void)
  63. {
  64. volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
  65. volatile cpm8xx_t *cp = &(im->im_cpm);
  66. volatile i2c8xx_t *i2c = (i2c8xx_t *)&(im->im_i2c);
  67. volatile iic_t *iip;
  68. #ifdef WITH_MICROCODE_PATCH
  69. ulong reloc = 0;
  70. #endif
  71. iip = (iic_t *)&cp->cp_dparam[PROFF_IIC];
  72. /*
  73. * kludge: when running from flash, no microcode patch can be
  74. * installed. However, the DPMEM usually contains non-zero
  75. * garbage at the relocatable patch base location, so lets clear
  76. * it now. This way the rest of the code can support the microcode
  77. * patch dynamically.
  78. */
  79. if ((ulong)vpd_init & 0xff000000)
  80. iip->iic_rpbase = 0;
  81. #ifdef WITH_MICROCODE_PATCH
  82. /* Check for and use a microcode relocation patch. */
  83. if ((reloc = iip->iic_rpbase))
  84. iip = (iic_t *)&cp->cp_dpmem[iip->iic_rpbase];
  85. #endif
  86. /* Initialize Port B IIC pins */
  87. cp->cp_pbpar |= 0x00000030;
  88. cp->cp_pbdir |= 0x00000030;
  89. cp->cp_pbodr |= 0x00000030;
  90. i2c->i2c_i2mod = 0x04; /* filter clock */
  91. i2c->i2c_i2add = 0x34; /* select an arbitrary (unique) address */
  92. i2c->i2c_i2brg = 0x07; /* make clock run maximum slow */
  93. i2c->i2c_i2cmr = 0x00; /* disable interrupts */
  94. i2c->i2c_i2cer = 0x1f; /* clear events */
  95. i2c->i2c_i2com = 0x01; /* configure i2c to work as master */
  96. if (vpd_read(0xa4, (uchar*)VPD_P, VPD_EEPROM_SIZE, 0) != VPD_EEPROM_SIZE)
  97. {
  98. hang();
  99. }
  100. }
  101. /* Read from I2C.
  102. * This is a two step process. First, we send the "dummy" write
  103. * to set the device offset for the read. Second, we perform
  104. * the read operation.
  105. */
  106. int vpd_read(uint iic_device, uchar *buf, int count, int offset)
  107. {
  108. volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
  109. volatile cpm8xx_t *cp = &(im->im_cpm);
  110. volatile i2c8xx_t *i2c = (i2c8xx_t *)&(im->im_i2c);
  111. volatile iic_t *iip;
  112. volatile cbd_t *tbdf1, *tbdf2, *rbdf;
  113. uchar *tb;
  114. uchar event;
  115. #ifdef WITH_MICROCODE_PATCH
  116. ulong reloc = 0;
  117. #endif
  118. iip = (iic_t *)&cp->cp_dparam[PROFF_IIC];
  119. #ifdef WITH_MICROCODE_PATCH
  120. /* Check for and use a microcode relocation patch. */
  121. if ((reloc = iip->iic_rpbase))
  122. iip = (iic_t *)&cp->cp_dpmem[iip->iic_rpbase];
  123. #endif
  124. tbdf1 = (cbd_t *)&cp->cp_dpmem[IIC_BD_TX1];
  125. tbdf2 = (cbd_t *)&cp->cp_dpmem[IIC_BD_TX2];
  126. rbdf = (cbd_t *)&cp->cp_dpmem[IIC_BD_RX];
  127. /* Send a "dummy write" operation. This is a write request with
  128. * only the offset sent, followed by another start condition.
  129. * This will ensure we start reading from the first location
  130. * of the EEPROM.
  131. */
  132. tb = (uchar*)&cp->cp_dpmem[IIC_BD_FREE];
  133. tb[0] = iic_device & 0xfe; /* device address */
  134. tb[1] = offset; /* offset */
  135. tbdf1->cbd_bufaddr = (uint)tb;
  136. tbdf1->cbd_datlen = 2;
  137. tbdf1->cbd_sc = 0x8400;
  138. tb += 2;
  139. tb[0] = iic_device | 1; /* device address */
  140. tbdf2->cbd_bufaddr = (uint)tb;
  141. tbdf2->cbd_datlen = count+1;
  142. tbdf2->cbd_sc = 0xbc00;
  143. rbdf->cbd_bufaddr = (uint)buf;
  144. rbdf->cbd_datlen = 0;
  145. rbdf->cbd_sc = 0xb000;
  146. iip->iic_tbase = IIC_BD_TX1;
  147. iip->iic_tbptr = IIC_BD_TX1;
  148. iip->iic_rbase = IIC_BD_RX;
  149. iip->iic_rbptr = IIC_BD_RX;
  150. iip->iic_rfcr = 0x15;
  151. iip->iic_tfcr = 0x15;
  152. iip->iic_mrblr = count;
  153. iip->iic_rstate = 0;
  154. iip->iic_tstate = 0;
  155. i2c->i2c_i2cer = 0x1f; /* clear event mask */
  156. i2c->i2c_i2mod |= 1; /* enable iic operation */
  157. i2c->i2c_i2com |= 0x80; /* start master */
  158. /* wait for IIC transfer */
  159. do {
  160. __asm__ volatile ("eieio");
  161. event = i2c->i2c_i2cer;
  162. } while (event == 0);
  163. if ((event & 0x10) || (event & 0x04)) {
  164. count = -1;
  165. goto bailout;
  166. }
  167. bailout:
  168. i2c->i2c_i2mod &= ~1; /* turn off iic operation */
  169. i2c->i2c_i2cer = 0x1f; /* clear event mask */
  170. return count;
  171. }