ethaddr.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * (C) Copyright 2006
  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. #include <common.h>
  24. #include <mpc5xxx.h>
  25. /* For the V38B board the pin is GPIO_PSC_6 */
  26. #define GPIO_PIN GPIO_PSC6_0
  27. #define NO_ERROR 0
  28. #define ERR_NO_NUMBER 1
  29. #define ERR_BAD_NUMBER 2
  30. static int is_high(void);
  31. static int check_device(void);
  32. static void io_out(int value);
  33. static void io_input(void);
  34. static void io_output(void);
  35. static void init_gpio(void);
  36. static void read_byte(unsigned char *data);
  37. static void write_byte(unsigned char command);
  38. void read_2501_memory(unsigned char *psernum, unsigned char *perr);
  39. void board_get_enetaddr(uchar *enetaddr);
  40. static int is_high()
  41. {
  42. return (*((vu_long *) MPC5XXX_WU_GPIO_DATA_I) & GPIO_PIN);
  43. }
  44. static void io_out(int value)
  45. {
  46. if (value)
  47. *((vu_long *) MPC5XXX_WU_GPIO_DATA_O) |= GPIO_PIN;
  48. else
  49. *((vu_long *) MPC5XXX_WU_GPIO_DATA_O) &= ~GPIO_PIN;
  50. }
  51. static void io_input()
  52. {
  53. *((vu_long *) MPC5XXX_WU_GPIO_DIR) &= ~GPIO_PIN;
  54. udelay(3); /* allow input to settle */
  55. }
  56. static void io_output()
  57. {
  58. *((vu_long *) MPC5XXX_WU_GPIO_DIR) |= GPIO_PIN;
  59. }
  60. static void init_gpio()
  61. {
  62. *((vu_long *) MPC5XXX_WU_GPIO_ENABLE) |= GPIO_PIN; /* Enable appropriate pin */
  63. }
  64. void read_2501_memory(unsigned char *psernum, unsigned char *perr)
  65. {
  66. #define NBYTES 28
  67. unsigned char crcval, i;
  68. unsigned char buf[NBYTES];
  69. *perr = 0;
  70. crcval = 0;
  71. for (i = 0; i < NBYTES; i++)
  72. buf[i] = 0;
  73. if (!check_device())
  74. *perr = ERR_NO_NUMBER;
  75. else {
  76. *perr = NO_ERROR;
  77. write_byte(0xCC); /* skip ROM (0xCC) */
  78. write_byte(0xF0); /* Read memory command 0xF0 */
  79. write_byte(0x00); /* Address TA1=0, TA2=0 */
  80. write_byte(0x00);
  81. read_byte(&crcval); /* Read CRC of address and command */
  82. for (i = 0; i < NBYTES; i++)
  83. read_byte(&buf[i]);
  84. }
  85. if (strncmp((const char *) &buf[11], "MAREL IEEE 802.3", 16)) {
  86. *perr = ERR_BAD_NUMBER;
  87. psernum[0] = 0x00;
  88. psernum[1] = 0xE0;
  89. psernum[2] = 0xEE;
  90. psernum[3] = 0xFF;
  91. psernum[4] = 0xFF;
  92. psernum[5] = 0xFF;
  93. } else {
  94. psernum[0] = 0x00;
  95. psernum[1] = 0xE0;
  96. psernum[2] = 0xEE;
  97. psernum[3] = buf[7];
  98. psernum[4] = buf[6];
  99. psernum[5] = buf[5];
  100. }
  101. }
  102. static int check_device()
  103. {
  104. int found;
  105. io_output();
  106. io_out(0);
  107. udelay(500); /* must be at least 480 us low pulse */
  108. io_input();
  109. udelay(60);
  110. found = (is_high() == 0) ? 1 : 0;
  111. udelay(500); /* must be at least 480 us low pulse */
  112. return found;
  113. }
  114. static void write_byte(unsigned char command)
  115. {
  116. char i;
  117. for (i = 0; i < 8; i++) {
  118. /* 1 us to 15 us low pulse starts bit slot */
  119. /* Start with high pulse for 3 us */
  120. io_input();
  121. udelay(3);
  122. io_out(0);
  123. io_output();
  124. udelay(3);
  125. if (command & 0x01) {
  126. /* 60 us high for 1-bit */
  127. io_input();
  128. udelay(60);
  129. } else
  130. /* 60 us low for 0-bit */
  131. udelay(60);
  132. /* Leave pin as input */
  133. io_input();
  134. command = command >> 1;
  135. }
  136. }
  137. static void read_byte(unsigned char *data)
  138. {
  139. unsigned char i, rdat = 0;
  140. for (i = 0; i < 8; i++) {
  141. /* read one bit from one-wire device */
  142. /* 1 - 15 us low starts bit slot */
  143. io_out(0);
  144. io_output();
  145. udelay(0);
  146. /* allow line to be pulled high */
  147. io_input();
  148. /* delay 10 us */
  149. udelay(10);
  150. /* now sample input status */
  151. if (is_high())
  152. rdat = (rdat >> 1) | 0x80;
  153. else
  154. rdat = rdat >> 1;
  155. udelay(60); /* at least 60 us */
  156. }
  157. /* copy the return value */
  158. *data = rdat;
  159. }
  160. void board_get_enetaddr(uchar *enetaddr)
  161. {
  162. unsigned char sn[6], err = NO_ERROR;
  163. init_gpio();
  164. read_2501_memory(sn, &err);
  165. if (err == NO_ERROR) {
  166. sprintf((char *)enetaddr, "%02x:%02x:%02x:%02x:%02x:%02x",
  167. sn[0], sn[1], sn[2], sn[3], sn[4], sn[5]);
  168. printf("MAC address: %s\n", enetaddr);
  169. setenv("ethaddr", (char *)enetaddr);
  170. } else {
  171. sprintf((char *)enetaddr, "00:01:02:03:04:05");
  172. printf("Error reading MAC address.\n");
  173. printf("Setting default to %s\n", enetaddr);
  174. setenv("ethaddr", (char *)enetaddr);
  175. }
  176. }