wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Cirrus Logic CS8900A Ethernet |
| 3 | * |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 4 | * (C) 2003 Wolfgang Denk, wd@denx.de |
| 5 | * Extension to synchronize ethaddr environment variable |
| 6 | * against value in EEPROM |
| 7 | * |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 8 | * (C) Copyright 2002 |
| 9 | * Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 10 | * Marius Groeger <mgroeger@sysgo.de> |
| 11 | * |
| 12 | * Copyright (C) 1999 Ben Williamson <benw@pobox.com> |
| 13 | * |
| 14 | * See file CREDITS for list of people who contributed to this |
| 15 | * project. |
| 16 | * |
| 17 | * This program is loaded into SRAM in bootstrap mode, where it waits |
| 18 | * for commands on UART1 to read and write memory, jump to code etc. |
| 19 | * A design goal for this program is to be entirely independent of the |
| 20 | * target board. Anything with a CL-PS7111 or EP7211 should be able to run |
| 21 | * this code in bootstrap mode. All the board specifics can be handled on |
| 22 | * the host. |
| 23 | * |
| 24 | * This program is free software; you can redistribute it and/or modify |
| 25 | * it under the terms of the GNU General Public License as published by |
| 26 | * the Free Software Foundation; either version 2 of the License, or |
| 27 | * (at your option) any later version. |
| 28 | * |
| 29 | * This program is distributed in the hope that it will be useful, |
| 30 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 31 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 32 | * GNU General Public License for more details. |
| 33 | * |
| 34 | * You should have received a copy of the GNU General Public License |
| 35 | * along with this program; if not, write to the Free Software |
| 36 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 37 | */ |
| 38 | |
| 39 | #include <common.h> |
| 40 | #include <command.h> |
| 41 | #include "cs8900.h" |
| 42 | #include <net.h> |
| 43 | |
| 44 | #ifdef CONFIG_DRIVER_CS8900 |
| 45 | |
| 46 | #if (CONFIG_COMMANDS & CFG_CMD_NET) |
| 47 | |
| 48 | |
| 49 | /* packet page register access functions */ |
| 50 | |
| 51 | #ifdef CS8900_BUS32 |
| 52 | /* we don't need 16 bit initialisation on 32 bit bus */ |
| 53 | #define get_reg_init_bus(x) get_reg((x)) |
| 54 | #else |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 55 | static unsigned short get_reg_init_bus (int regno) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 56 | { |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 57 | /* force 16 bit busmode */ |
| 58 | volatile unsigned char c; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 59 | |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 60 | c = CS8900_BUS16_0; |
| 61 | c = CS8900_BUS16_1; |
| 62 | c = CS8900_BUS16_0; |
| 63 | c = CS8900_BUS16_1; |
| 64 | c = CS8900_BUS16_0; |
| 65 | |
| 66 | CS8900_PPTR = regno; |
| 67 | return (unsigned short) CS8900_PDATA; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 68 | } |
| 69 | #endif |
| 70 | |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 71 | static unsigned short get_reg (int regno) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 72 | { |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 73 | CS8900_PPTR = regno; |
| 74 | return (unsigned short) CS8900_PDATA; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 78 | static void put_reg (int regno, unsigned short val) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 79 | { |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 80 | CS8900_PPTR = regno; |
| 81 | CS8900_PDATA = val; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 82 | } |
| 83 | |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 84 | static void eth_reset (void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 85 | { |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 86 | int tmo; |
| 87 | unsigned short us; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 88 | |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 89 | /* reset NIC */ |
| 90 | put_reg (PP_SelfCTL, get_reg (PP_SelfCTL) | PP_SelfCTL_Reset); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 91 | |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 92 | /* wait for 200ms */ |
| 93 | udelay (200000); |
| 94 | /* Wait until the chip is reset */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 95 | |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 96 | tmo = get_timer (0) + 1 * CFG_HZ; |
| 97 | while ((((us = get_reg_init_bus (PP_SelfSTAT)) & PP_SelfSTAT_InitD) == 0) |
| 98 | && tmo < get_timer (0)) |
| 99 | /*NOP*/; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 100 | } |
| 101 | |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 102 | void cs8900_get_enetaddr (uchar * addr) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 103 | { |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 104 | int i; |
| 105 | unsigned char env_enetaddr[6]; |
| 106 | char *tmp = getenv ("ethaddr"); |
| 107 | char *end; |
| 108 | |
| 109 | for (i=0; i<6; i++) { |
| 110 | env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0; |
| 111 | if (tmp) |
| 112 | tmp = (*end) ? end+1 : end; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 113 | } |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 114 | |
| 115 | /* verify chip id */ |
| 116 | if (get_reg_init_bus (PP_ChipID) != 0x630e) |
| 117 | return; |
| 118 | eth_reset (); |
| 119 | if ((get_reg (PP_SelfST) & (PP_SelfSTAT_EEPROM | PP_SelfSTAT_EEPROM_OK)) == |
| 120 | (PP_SelfSTAT_EEPROM | PP_SelfSTAT_EEPROM_OK)) { |
| 121 | |
| 122 | /* Load the MAC from EEPROM */ |
| 123 | for (i = 0; i < 6 / 2; i++) { |
| 124 | unsigned int Addr; |
| 125 | |
| 126 | Addr = get_reg (PP_IA + i * 2); |
| 127 | addr[i * 2] = Addr & 0xFF; |
| 128 | addr[i * 2 + 1] = Addr >> 8; |
| 129 | } |
| 130 | |
| 131 | if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6) != 0 && |
| 132 | memcmp(env_enetaddr, addr, 6) != 0) { |
| 133 | printf ("\nWarning: MAC addresses don't match:\n"); |
| 134 | printf ("\tHW MAC address: " |
| 135 | "%02X:%02X:%02X:%02X:%02X:%02X\n", |
| 136 | addr[0], addr[1], |
| 137 | addr[2], addr[3], |
| 138 | addr[4], addr[5] ); |
| 139 | printf ("\t\"ethaddr\" value: " |
| 140 | "%02X:%02X:%02X:%02X:%02X:%02X\n", |
| 141 | env_enetaddr[0], env_enetaddr[1], |
| 142 | env_enetaddr[2], env_enetaddr[3], |
| 143 | env_enetaddr[4], env_enetaddr[5]) ; |
| 144 | debug ("### Set MAC addr from environment\n"); |
| 145 | memcpy (addr, env_enetaddr, 6); |
| 146 | } |
| 147 | if (!tmp) { |
| 148 | char ethaddr[20]; |
| 149 | sprintf (ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 150 | addr[0], addr[1], |
| 151 | addr[2], addr[3], |
| 152 | addr[4], addr[5]) ; |
| 153 | debug ("### Set environment from HW MAC addr = \"%s\"\n", ethaddr); |
| 154 | setenv ("ethaddr", ethaddr); |
| 155 | } |
| 156 | |
| 157 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 158 | } |
| 159 | |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 160 | void eth_halt (void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 161 | { |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 162 | /* disable transmitter/receiver mode */ |
| 163 | put_reg (PP_LineCTL, 0); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 164 | |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 165 | /* "shutdown" to show ChipID or kernel wouldn't find he cs8900 ... */ |
| 166 | get_reg_init_bus (PP_ChipID); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 167 | } |
| 168 | |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 169 | int eth_init (bd_t * bd) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 170 | { |
| 171 | |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 172 | /* verify chip id */ |
| 173 | if (get_reg_init_bus (PP_ChipID) != 0x630e) { |
| 174 | printf ("CS8900 Ethernet chip not found?!\n"); |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | eth_reset (); |
| 179 | /* set the ethernet address */ |
| 180 | put_reg (PP_IA + 0, bd->bi_enetaddr[0] | (bd->bi_enetaddr[1] << 8)); |
| 181 | put_reg (PP_IA + 2, bd->bi_enetaddr[2] | (bd->bi_enetaddr[3] << 8)); |
| 182 | put_reg (PP_IA + 4, bd->bi_enetaddr[4] | (bd->bi_enetaddr[5] << 8)); |
| 183 | |
| 184 | /* receive only error free packets addressed to this card */ |
| 185 | put_reg (PP_RxCTL, PP_RxCTL_IA | PP_RxCTL_Broadcast | PP_RxCTL_RxOK); |
| 186 | |
| 187 | /* do not generate any interrupts on receive operations */ |
| 188 | put_reg (PP_RxCFG, 0); |
| 189 | |
| 190 | /* do not generate any interrupts on transmit operations */ |
| 191 | put_reg (PP_TxCFG, 0); |
| 192 | |
| 193 | /* do not generate any interrupts on buffer operations */ |
| 194 | put_reg (PP_BufCFG, 0); |
| 195 | |
| 196 | /* enable transmitter/receiver mode */ |
| 197 | put_reg (PP_LineCTL, PP_LineCTL_Rx | PP_LineCTL_Tx); |
| 198 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 199 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /* Get a data block via Ethernet */ |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 203 | extern int eth_rx (void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 204 | { |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 205 | int i; |
| 206 | unsigned short rxlen; |
| 207 | unsigned short *addr; |
| 208 | unsigned short status; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 209 | |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 210 | status = get_reg (PP_RER); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 211 | |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 212 | if ((status & PP_RER_RxOK) == 0) |
| 213 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 214 | |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 215 | status = CS8900_RTDATA; /* stat */ |
| 216 | rxlen = CS8900_RTDATA; /* len */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 217 | |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 218 | if (rxlen > PKTSIZE_ALIGN + PKTALIGN) |
| 219 | printf ("packet too big!\n"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 220 | |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 221 | for (addr = (unsigned short *) NetRxPackets[0], i = rxlen >> 1; i > 0; |
| 222 | i--) |
| 223 | *addr++ = CS8900_RTDATA; |
| 224 | if (rxlen & 1) |
| 225 | *addr++ = CS8900_RTDATA; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 226 | |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 227 | /* Pass the packet up to the protocol layers. */ |
| 228 | NetReceive (NetRxPackets[0], rxlen); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 229 | |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 230 | return rxlen; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /* Send a data block via Ethernet. */ |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 234 | extern int eth_send (volatile void *packet, int length) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 235 | { |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 236 | volatile unsigned short *addr; |
| 237 | int tmo; |
| 238 | unsigned short s; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 239 | |
| 240 | retry: |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 241 | /* initiate a transmit sequence */ |
| 242 | CS8900_TxCMD = PP_TxCmd_TxStart_Full; |
| 243 | CS8900_TxLEN = length; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 244 | |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 245 | /* Test to see if the chip has allocated memory for the packet */ |
| 246 | if ((get_reg (PP_BusSTAT) & PP_BusSTAT_TxRDY) == 0) { |
| 247 | /* Oops... this should not happen! */ |
| 248 | printf ("cs: unable to send packet; retrying...\n"); |
| 249 | for (tmo = get_timer (0) + 5 * CFG_HZ; get_timer (0) < tmo;) |
| 250 | /*NOP*/; |
| 251 | eth_reset (); |
| 252 | goto retry; |
| 253 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 254 | |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 255 | /* Write the contents of the packet */ |
| 256 | /* assume even number of bytes */ |
| 257 | for (addr = packet; length > 0; length -= 2) |
| 258 | CS8900_RTDATA = *addr++; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 259 | |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 260 | /* wait for transfer to succeed */ |
| 261 | tmo = get_timer (0) + 5 * CFG_HZ; |
| 262 | while ((s = get_reg (PP_TER) & ~0x1F) == 0) { |
| 263 | if (get_timer (0) >= tmo) |
| 264 | break; |
| 265 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 266 | |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 267 | /* nothing */ ; |
| 268 | if ((s & (PP_TER_CRS | PP_TER_TxOK)) != PP_TER_TxOK) { |
| 269 | printf ("\ntransmission error %#x\n", s); |
| 270 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 271 | |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 272 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 273 | } |
| 274 | |
wdenk | 1cb8e98 | 2003-03-06 21:55:29 +0000 | [diff] [blame^] | 275 | static void cs8900_e2prom_ready(void) |
| 276 | { |
| 277 | while(get_reg(PP_SelfST) & SI_BUSY); |
| 278 | } |
| 279 | |
| 280 | /***********************************************************/ |
| 281 | /* read a 16-bit word out of the EEPROM */ |
| 282 | /***********************************************************/ |
| 283 | |
| 284 | int cs8900_e2prom_read(unsigned char addr, unsigned short *value) |
| 285 | { |
| 286 | cs8900_e2prom_ready(); |
| 287 | put_reg(PP_EECMD, EEPROM_READ_CMD | addr); |
| 288 | cs8900_e2prom_ready(); |
| 289 | *value = get_reg(PP_EEData); |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | |
| 295 | /***********************************************************/ |
| 296 | /* write a 16-bit word into the EEPROM */ |
| 297 | /***********************************************************/ |
| 298 | |
| 299 | void cs8900_e2prom_write(unsigned char addr, unsigned short value) |
| 300 | { |
| 301 | cs8900_e2prom_ready(); |
| 302 | put_reg(PP_EECMD, EEPROM_WRITE_EN); |
| 303 | cs8900_e2prom_ready(); |
| 304 | put_reg(PP_EEData, value); |
| 305 | put_reg(PP_EECMD, EEPROM_WRITE_CMD | addr); |
| 306 | cs8900_e2prom_ready(); |
| 307 | put_reg(PP_EECMD, EEPROM_WRITE_DIS); |
| 308 | cs8900_e2prom_ready(); |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 313 | #endif /* COMMANDS & CFG_NET */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 314 | |
wdenk | 6069ff2 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 315 | #endif /* CONFIG_DRIVER_CS8900 */ |