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