3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
11 /* For the V38B board the pin is GPIO_PSC_6 */
12 #define GPIO_PIN GPIO_PSC6_0
15 #define ERR_NO_NUMBER 1
16 #define ERR_BAD_NUMBER 2
18 static int is_high(void);
19 static int check_device(void);
20 static void io_out(int value);
21 static void io_input(void);
22 static void io_output(void);
23 static void init_gpio(void);
24 static void read_byte(unsigned char *data);
25 static void write_byte(unsigned char command);
27 void read_2501_memory(unsigned char *psernum, unsigned char *perr);
28 void board_get_enetaddr(uchar *enetaddr);
33 return (*((vu_long *) MPC5XXX_WU_GPIO_DATA_I) & GPIO_PIN);
36 static void io_out(int value)
39 *((vu_long *) MPC5XXX_WU_GPIO_DATA_O) |= GPIO_PIN;
41 *((vu_long *) MPC5XXX_WU_GPIO_DATA_O) &= ~GPIO_PIN;
44 static void io_input()
46 *((vu_long *) MPC5XXX_WU_GPIO_DIR) &= ~GPIO_PIN;
47 udelay(3); /* allow input to settle */
50 static void io_output()
52 *((vu_long *) MPC5XXX_WU_GPIO_DIR) |= GPIO_PIN;
55 static void init_gpio()
57 *((vu_long *) MPC5XXX_WU_GPIO_ENABLE) |= GPIO_PIN; /* Enable appropriate pin */
60 void read_2501_memory(unsigned char *psernum, unsigned char *perr)
63 unsigned char crcval, i;
64 unsigned char buf[NBYTES];
69 for (i = 0; i < NBYTES; i++)
73 *perr = ERR_NO_NUMBER;
76 write_byte(0xCC); /* skip ROM (0xCC) */
77 write_byte(0xF0); /* Read memory command 0xF0 */
78 write_byte(0x00); /* Address TA1=0, TA2=0 */
80 read_byte(&crcval); /* Read CRC of address and command */
82 for (i = 0; i < NBYTES; i++)
85 if (strncmp((const char *) &buf[11], "MAREL IEEE 802.3", 16)) {
86 *perr = ERR_BAD_NUMBER;
103 static int check_device()
109 udelay(500); /* must be at least 480 us low pulse */
114 found = (is_high() == 0) ? 1 : 0;
115 udelay(500); /* must be at least 480 us low pulse */
120 static void write_byte(unsigned char command)
124 for (i = 0; i < 8; i++) {
125 /* 1 us to 15 us low pulse starts bit slot */
126 /* Start with high pulse for 3 us */
134 if (command & 0x01) {
135 /* 60 us high for 1-bit */
139 /* 60 us low for 0-bit */
141 /* Leave pin as input */
144 command = command >> 1;
148 static void read_byte(unsigned char *data)
150 unsigned char i, rdat = 0;
152 for (i = 0; i < 8; i++) {
153 /* read one bit from one-wire device */
155 /* 1 - 15 us low starts bit slot */
160 /* allow line to be pulled high */
166 /* now sample input status */
168 rdat = (rdat >> 1) | 0x80;
172 udelay(60); /* at least 60 us */
174 /* copy the return value */
178 void board_get_enetaddr(uchar *enetaddr)
180 unsigned char sn[6], err = NO_ERROR;
184 read_2501_memory(sn, &err);
186 if (err == NO_ERROR) {
187 sprintf((char *)enetaddr, "%02x:%02x:%02x:%02x:%02x:%02x",
188 sn[0], sn[1], sn[2], sn[3], sn[4], sn[5]);
189 printf("MAC address: %s\n", enetaddr);
190 setenv("ethaddr", (char *)enetaddr);
192 sprintf((char *)enetaddr, "00:01:02:03:04:05");
193 printf("Error reading MAC address.\n");
194 printf("Setting default to %s\n", enetaddr);
195 setenv("ethaddr", (char *)enetaddr);