blob: 4e2494eee5ce12ad5f1f7f04f05e507a6e03836a [file] [log] [blame]
Bartlomiej Sieka2dfa3d22006-10-13 21:09:09 +02001/*
Bartlomiej Sieka2dfa3d22006-10-13 21:09:09 +02002 * (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
Bartlomiej Sieka8cb2ae52006-11-01 01:45:46 +010027/* For the V38B board the pin is GPIO_PSC_6 */
28#define GPIO_PIN GPIO_PSC6_0
Bartlomiej Sieka2dfa3d22006-10-13 21:09:09 +020029
30#define NO_ERROR 0
31#define ERR_NO_NUMBER 1
32#define ERR_BAD_NUMBER 2
33
Bartlomiej Sieka2dfa3d22006-10-13 21:09:09 +020034static 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
Bartlomiej Sieka8cb2ae52006-11-01 01:45:46 +010046
Bartlomiej Sieka2dfa3d22006-10-13 21:09:09 +020047static int is_high()
48{
Bartlomiej Sieka8cb2ae52006-11-01 01:45:46 +010049 return (*((vu_long *) MPC5XXX_WU_GPIO_DATA_I) & GPIO_PIN);
Bartlomiej Sieka2dfa3d22006-10-13 21:09:09 +020050}
51
52static void io_out(int value)
53{
54 if (value)
Bartlomiej Sieka8cb2ae52006-11-01 01:45:46 +010055 *((vu_long *) MPC5XXX_WU_GPIO_DATA_O) |= GPIO_PIN;
Bartlomiej Sieka2dfa3d22006-10-13 21:09:09 +020056 else
Bartlomiej Sieka8cb2ae52006-11-01 01:45:46 +010057 *((vu_long *) MPC5XXX_WU_GPIO_DATA_O) &= ~GPIO_PIN;
Bartlomiej Sieka2dfa3d22006-10-13 21:09:09 +020058}
59
60static void io_input()
61{
Bartlomiej Sieka8cb2ae52006-11-01 01:45:46 +010062 *((vu_long *) MPC5XXX_WU_GPIO_DIR) &= ~GPIO_PIN;
Bartlomiej Sieka2dfa3d22006-10-13 21:09:09 +020063 udelay(3); /* allow input to settle */
64}
65
66static void io_output()
67{
Bartlomiej Sieka8cb2ae52006-11-01 01:45:46 +010068 *((vu_long *) MPC5XXX_WU_GPIO_DIR) |= GPIO_PIN;
Bartlomiej Sieka2dfa3d22006-10-13 21:09:09 +020069}
70
71static void init_gpio()
72{
Bartlomiej Sieka8cb2ae52006-11-01 01:45:46 +010073 *((vu_long *) MPC5XXX_WU_GPIO_ENABLE) |= GPIO_PIN; /* Enable appropriate pin */
Bartlomiej Sieka2dfa3d22006-10-13 21:09:09 +020074}
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
Bartlomiej Sieka8cb2ae52006-11-01 01:45:46 +010085 for (i = 0; i < NBYTES; i++)
86 buf[i] = 0;
Bartlomiej Sieka2dfa3d22006-10-13 21:09:09 +020087
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
Bartlomiej Sieka8cb2ae52006-11-01 01:45:46 +010098 for (i = 0; i < NBYTES; i++)
99 read_byte(&buf[i]);
Bartlomiej Sieka2dfa3d22006-10-13 21:09:09 +0200100 }
Bartlomiej Sieka8cb2ae52006-11-01 01:45:46 +0100101 if (strncmp((const char *) &buf[11], "MAREL IEEE 802.3", 16)) {
Bartlomiej Sieka2dfa3d22006-10-13 21:09:09 +0200102 *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;
Bartlomiej Sieka8cb2ae52006-11-01 01:45:46 +0100109 } else {
Bartlomiej Sieka2dfa3d22006-10-13 21:09:09 +0200110 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
Bartlomiej Sieka8cb2ae52006-11-01 01:45:46 +0100140 for (i = 0; i < 8; i++) {
Bartlomiej Sieka2dfa3d22006-10-13 21:09:09 +0200141 /* 1 us to 15 us low pulse starts bit slot */
142 /* Start with high pulse for 3 us */
143 io_input();
Bartlomiej Sieka2dfa3d22006-10-13 21:09:09 +0200144 udelay(3);
145
146 io_out(0);
147 io_output();
Bartlomiej Sieka2dfa3d22006-10-13 21:09:09 +0200148 udelay(3);
149
150 if (command & 0x01) {
151 /* 60 us high for 1-bit */
152 io_input();
153 udelay(60);
Bartlomiej Sieka8cb2ae52006-11-01 01:45:46 +0100154 } else
Bartlomiej Sieka2dfa3d22006-10-13 21:09:09 +0200155 /* 60 us low for 0-bit */
156 udelay(60);
Bartlomiej Sieka2dfa3d22006-10-13 21:09:09 +0200157 /* Leave pin as input */
158 io_input();
159
160 command = command >> 1;
161 }
162}
163
Bartlomiej Sieka8cb2ae52006-11-01 01:45:46 +0100164static void read_byte(unsigned char *data)
Bartlomiej Sieka2dfa3d22006-10-13 21:09:09 +0200165{
166 unsigned char i, rdat = 0;
167
Bartlomiej Sieka8cb2ae52006-11-01 01:45:46 +0100168 for (i = 0; i < 8; i++) {
Bartlomiej Sieka2dfa3d22006-10-13 21:09:09 +0200169 /* 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;
Wolfgang Denk45bd0212006-10-18 22:44:38 +0200187
Bartlomiej Sieka2dfa3d22006-10-13 21:09:09 +0200188 udelay(60); /* at least 60 us */
189 }
190 /* copy the return value */
191 *data = rdat;
192}
193
194void board_get_enetaddr(uchar *enetaddr)
195{
Bartlomiej Sieka8cb2ae52006-11-01 01:45:46 +0100196 unsigned char sn[6], err = NO_ERROR;
Bartlomiej Sieka2dfa3d22006-10-13 21:09:09 +0200197
198 init_gpio();
199
200 read_2501_memory(sn, &err);
201
202 if (err == NO_ERROR) {
Bartlomiej Sieka8cb2ae52006-11-01 01:45:46 +0100203 sprintf((char *)enetaddr, "%02x:%02x:%02x:%02x:%02x:%02x",
Bartlomiej Sieka2dfa3d22006-10-13 21:09:09 +0200204 sn[0], sn[1], sn[2], sn[3], sn[4], sn[5]);
205 printf("MAC address: %s\n", enetaddr);
Bartlomiej Sieka8cb2ae52006-11-01 01:45:46 +0100206 setenv("ethaddr", (char *)enetaddr);
207 } else {
208 sprintf((char *)enetaddr, "00:01:02:03:04:05");
Bartlomiej Sieka2dfa3d22006-10-13 21:09:09 +0200209 printf("Error reading MAC address.\n");
210 printf("Setting default to %s\n", enetaddr);
Bartlomiej Sieka8cb2ae52006-11-01 01:45:46 +0100211 setenv("ethaddr", (char *)enetaddr);
Bartlomiej Sieka2dfa3d22006-10-13 21:09:09 +0200212 }
213}