ethaddr.c 3.8 KB

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