Bartlomiej Sieka | 2dfa3d2 | 2006-10-13 21:09:09 +0200 | [diff] [blame] | 1 | /* |
Bartlomiej Sieka | 2dfa3d2 | 2006-10-13 21:09:09 +0200 | [diff] [blame] | 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 | |
Bartlomiej Sieka | 8cb2ae5 | 2006-11-01 01:45:46 +0100 | [diff] [blame] | 27 | /* For the V38B board the pin is GPIO_PSC_6 */ |
| 28 | #define GPIO_PIN GPIO_PSC6_0 |
Bartlomiej Sieka | 2dfa3d2 | 2006-10-13 21:09:09 +0200 | [diff] [blame] | 29 | |
| 30 | #define NO_ERROR 0 |
| 31 | #define ERR_NO_NUMBER 1 |
| 32 | #define ERR_BAD_NUMBER 2 |
| 33 | |
Bartlomiej Sieka | 2dfa3d2 | 2006-10-13 21:09:09 +0200 | [diff] [blame] | 34 | static int is_high(void); |
| 35 | static int check_device(void); |
| 36 | static void io_out(int value); |
| 37 | static void io_input(void); |
| 38 | static void io_output(void); |
| 39 | static void init_gpio(void); |
| 40 | static void read_byte(unsigned char *data); |
| 41 | static void write_byte(unsigned char command); |
| 42 | |
| 43 | void read_2501_memory(unsigned char *psernum, unsigned char *perr); |
| 44 | void board_get_enetaddr(uchar *enetaddr); |
| 45 | |
Bartlomiej Sieka | 8cb2ae5 | 2006-11-01 01:45:46 +0100 | [diff] [blame] | 46 | |
Bartlomiej Sieka | 2dfa3d2 | 2006-10-13 21:09:09 +0200 | [diff] [blame] | 47 | static int is_high() |
| 48 | { |
Bartlomiej Sieka | 8cb2ae5 | 2006-11-01 01:45:46 +0100 | [diff] [blame] | 49 | return (*((vu_long *) MPC5XXX_WU_GPIO_DATA_I) & GPIO_PIN); |
Bartlomiej Sieka | 2dfa3d2 | 2006-10-13 21:09:09 +0200 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static void io_out(int value) |
| 53 | { |
| 54 | if (value) |
Bartlomiej Sieka | 8cb2ae5 | 2006-11-01 01:45:46 +0100 | [diff] [blame] | 55 | *((vu_long *) MPC5XXX_WU_GPIO_DATA_O) |= GPIO_PIN; |
Bartlomiej Sieka | 2dfa3d2 | 2006-10-13 21:09:09 +0200 | [diff] [blame] | 56 | else |
Bartlomiej Sieka | 8cb2ae5 | 2006-11-01 01:45:46 +0100 | [diff] [blame] | 57 | *((vu_long *) MPC5XXX_WU_GPIO_DATA_O) &= ~GPIO_PIN; |
Bartlomiej Sieka | 2dfa3d2 | 2006-10-13 21:09:09 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static void io_input() |
| 61 | { |
Bartlomiej Sieka | 8cb2ae5 | 2006-11-01 01:45:46 +0100 | [diff] [blame] | 62 | *((vu_long *) MPC5XXX_WU_GPIO_DIR) &= ~GPIO_PIN; |
Bartlomiej Sieka | 2dfa3d2 | 2006-10-13 21:09:09 +0200 | [diff] [blame] | 63 | udelay(3); /* allow input to settle */ |
| 64 | } |
| 65 | |
| 66 | static void io_output() |
| 67 | { |
Bartlomiej Sieka | 8cb2ae5 | 2006-11-01 01:45:46 +0100 | [diff] [blame] | 68 | *((vu_long *) MPC5XXX_WU_GPIO_DIR) |= GPIO_PIN; |
Bartlomiej Sieka | 2dfa3d2 | 2006-10-13 21:09:09 +0200 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static void init_gpio() |
| 72 | { |
Bartlomiej Sieka | 8cb2ae5 | 2006-11-01 01:45:46 +0100 | [diff] [blame] | 73 | *((vu_long *) MPC5XXX_WU_GPIO_ENABLE) |= GPIO_PIN; /* Enable appropriate pin */ |
Bartlomiej Sieka | 2dfa3d2 | 2006-10-13 21:09:09 +0200 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void 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 Sieka | 8cb2ae5 | 2006-11-01 01:45:46 +0100 | [diff] [blame] | 85 | for (i = 0; i < NBYTES; i++) |
| 86 | buf[i] = 0; |
Bartlomiej Sieka | 2dfa3d2 | 2006-10-13 21:09:09 +0200 | [diff] [blame] | 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 | |
Bartlomiej Sieka | 8cb2ae5 | 2006-11-01 01:45:46 +0100 | [diff] [blame] | 98 | for (i = 0; i < NBYTES; i++) |
| 99 | read_byte(&buf[i]); |
Bartlomiej Sieka | 2dfa3d2 | 2006-10-13 21:09:09 +0200 | [diff] [blame] | 100 | } |
Bartlomiej Sieka | 8cb2ae5 | 2006-11-01 01:45:46 +0100 | [diff] [blame] | 101 | if (strncmp((const char *) &buf[11], "MAREL IEEE 802.3", 16)) { |
Bartlomiej Sieka | 2dfa3d2 | 2006-10-13 21:09:09 +0200 | [diff] [blame] | 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; |
Bartlomiej Sieka | 8cb2ae5 | 2006-11-01 01:45:46 +0100 | [diff] [blame] | 109 | } else { |
Bartlomiej Sieka | 2dfa3d2 | 2006-10-13 21:09:09 +0200 | [diff] [blame] | 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 | |
| 119 | static 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 | |
| 136 | static void write_byte(unsigned char command) |
| 137 | { |
| 138 | char i; |
| 139 | |
Bartlomiej Sieka | 8cb2ae5 | 2006-11-01 01:45:46 +0100 | [diff] [blame] | 140 | for (i = 0; i < 8; i++) { |
Bartlomiej Sieka | 2dfa3d2 | 2006-10-13 21:09:09 +0200 | [diff] [blame] | 141 | /* 1 us to 15 us low pulse starts bit slot */ |
| 142 | /* Start with high pulse for 3 us */ |
| 143 | io_input(); |
Bartlomiej Sieka | 2dfa3d2 | 2006-10-13 21:09:09 +0200 | [diff] [blame] | 144 | udelay(3); |
| 145 | |
| 146 | io_out(0); |
| 147 | io_output(); |
Bartlomiej Sieka | 2dfa3d2 | 2006-10-13 21:09:09 +0200 | [diff] [blame] | 148 | udelay(3); |
| 149 | |
| 150 | if (command & 0x01) { |
| 151 | /* 60 us high for 1-bit */ |
| 152 | io_input(); |
| 153 | udelay(60); |
Bartlomiej Sieka | 8cb2ae5 | 2006-11-01 01:45:46 +0100 | [diff] [blame] | 154 | } else |
Bartlomiej Sieka | 2dfa3d2 | 2006-10-13 21:09:09 +0200 | [diff] [blame] | 155 | /* 60 us low for 0-bit */ |
| 156 | udelay(60); |
Bartlomiej Sieka | 2dfa3d2 | 2006-10-13 21:09:09 +0200 | [diff] [blame] | 157 | /* Leave pin as input */ |
| 158 | io_input(); |
| 159 | |
| 160 | command = command >> 1; |
| 161 | } |
| 162 | } |
| 163 | |
Bartlomiej Sieka | 8cb2ae5 | 2006-11-01 01:45:46 +0100 | [diff] [blame] | 164 | static void read_byte(unsigned char *data) |
Bartlomiej Sieka | 2dfa3d2 | 2006-10-13 21:09:09 +0200 | [diff] [blame] | 165 | { |
| 166 | unsigned char i, rdat = 0; |
| 167 | |
Bartlomiej Sieka | 8cb2ae5 | 2006-11-01 01:45:46 +0100 | [diff] [blame] | 168 | for (i = 0; i < 8; i++) { |
Bartlomiej Sieka | 2dfa3d2 | 2006-10-13 21:09:09 +0200 | [diff] [blame] | 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; |
Wolfgang Denk | 45bd021 | 2006-10-18 22:44:38 +0200 | [diff] [blame] | 187 | |
Bartlomiej Sieka | 2dfa3d2 | 2006-10-13 21:09:09 +0200 | [diff] [blame] | 188 | udelay(60); /* at least 60 us */ |
| 189 | } |
| 190 | /* copy the return value */ |
| 191 | *data = rdat; |
| 192 | } |
| 193 | |
| 194 | void board_get_enetaddr(uchar *enetaddr) |
| 195 | { |
Bartlomiej Sieka | 8cb2ae5 | 2006-11-01 01:45:46 +0100 | [diff] [blame] | 196 | unsigned char sn[6], err = NO_ERROR; |
Bartlomiej Sieka | 2dfa3d2 | 2006-10-13 21:09:09 +0200 | [diff] [blame] | 197 | |
| 198 | init_gpio(); |
| 199 | |
| 200 | read_2501_memory(sn, &err); |
| 201 | |
| 202 | if (err == NO_ERROR) { |
Bartlomiej Sieka | 8cb2ae5 | 2006-11-01 01:45:46 +0100 | [diff] [blame] | 203 | sprintf((char *)enetaddr, "%02x:%02x:%02x:%02x:%02x:%02x", |
Bartlomiej Sieka | 2dfa3d2 | 2006-10-13 21:09:09 +0200 | [diff] [blame] | 204 | sn[0], sn[1], sn[2], sn[3], sn[4], sn[5]); |
| 205 | printf("MAC address: %s\n", enetaddr); |
Bartlomiej Sieka | 8cb2ae5 | 2006-11-01 01:45:46 +0100 | [diff] [blame] | 206 | setenv("ethaddr", (char *)enetaddr); |
| 207 | } else { |
| 208 | sprintf((char *)enetaddr, "00:01:02:03:04:05"); |
Bartlomiej Sieka | 2dfa3d2 | 2006-10-13 21:09:09 +0200 | [diff] [blame] | 209 | printf("Error reading MAC address.\n"); |
| 210 | printf("Setting default to %s\n", enetaddr); |
Bartlomiej Sieka | 8cb2ae5 | 2006-11-01 01:45:46 +0100 | [diff] [blame] | 211 | setenv("ethaddr", (char *)enetaddr); |
Bartlomiej Sieka | 2dfa3d2 | 2006-10-13 21:09:09 +0200 | [diff] [blame] | 212 | } |
| 213 | } |