wdenk | a6db71d | 2003-04-08 23:25:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * PLB2800 internal switch ethernet driver. |
| 3 | * |
| 4 | * (C) Copyright 2003 |
| 5 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 6 | * |
| 7 | * See file CREDITS for list of people who contributed to this |
| 8 | * project. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 of |
| 13 | * the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 23 | * MA 02111-1307 USA |
| 24 | */ |
| 25 | |
| 26 | #include <common.h> |
| 27 | |
Jon Loeliger | d8c8630 | 2007-06-11 19:02:10 -0500 | [diff] [blame] | 28 | #if ((CONFIG_COMMANDS & CFG_CMD_NET) || defined(CONFIG_CMD_NET)) \ |
| 29 | && defined(CONFIG_NET_MULTI) && defined(CONFIG_PLB2800_ETHER) |
wdenk | a6db71d | 2003-04-08 23:25:21 +0000 | [diff] [blame] | 30 | |
| 31 | #include <malloc.h> |
| 32 | #include <net.h> |
| 33 | #include <asm/addrspace.h> |
| 34 | |
| 35 | |
| 36 | #define NUM_RX_DESC PKTBUFSRX |
| 37 | #define TOUT_LOOP 1000000 |
| 38 | |
| 39 | #define LONG_REF(addr) (*((volatile unsigned long*)addr)) |
| 40 | |
| 41 | #define CMAC_CRX_CTRL LONG_REF(0xb800c870) |
| 42 | #define CMAC_CTX_CTRL LONG_REF(0xb800c874) |
| 43 | #define SYS_MAC_ADDR_0 LONG_REF(0xb800c878) |
| 44 | #define SYS_MAC_ADDR_1 LONG_REF(0xb800c87c) |
| 45 | #define MIPS_H_MASK LONG_REF(0xB800C810) |
| 46 | |
| 47 | #define MA_LEARN LONG_REF(0xb8008004) |
| 48 | #define DA_LOOKUP LONG_REF(0xb8008008) |
| 49 | |
| 50 | #define CMAC_CRX_CTRL_PD 0x00000001 |
| 51 | #define CMAC_CRX_CTRL_CG 0x00000002 |
| 52 | #define CMAC_CRX_CTRL_PL_SHIFT 2 |
| 53 | #define CMAC_CRIT 0x0 |
| 54 | #define CMAC_NON_CRIT 0x1 |
| 55 | #define MBOX_STAT_ID_SHF 28 |
| 56 | #define MBOX_STAT_CP 0x80000000 |
| 57 | #define MBOX_STAT_MB 0x00000001 |
| 58 | #define EN_MA_LEARN 0x02000000 |
| 59 | #define EN_DA_LKUP 0x01000000 |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 60 | #define MA_DEST_SHF 11 |
wdenk | a6db71d | 2003-04-08 23:25:21 +0000 | [diff] [blame] | 61 | #define DA_DEST_SHF 11 |
| 62 | #define DA_STATE_SHF 19 |
| 63 | #define TSTAMP_MS 0x00000000 |
| 64 | #define SW_H_MBOX4_MASK 0x08000000 |
| 65 | #define SW_H_MBOX3_MASK 0x04000000 |
| 66 | #define SW_H_MBOX2_MASK 0x02000000 |
| 67 | #define SW_H_MBOX1_MASK 0x01000000 |
| 68 | |
| 69 | typedef volatile struct { |
| 70 | unsigned int stat; |
| 71 | unsigned int cmd; |
| 72 | unsigned int cnt; |
| 73 | unsigned int adr; |
| 74 | } mailbox_t; |
| 75 | |
| 76 | #define MBOX_REG(mb) ((mailbox_t*)(0xb800c830+(mb<<4))) |
| 77 | |
| 78 | typedef volatile struct { |
| 79 | unsigned int word0; |
| 80 | unsigned int word1; |
| 81 | unsigned int word2; |
| 82 | } mbhdr_t; |
| 83 | |
| 84 | #define MBOX_MEM(mb) ((void*)(0xb800a000+((3-mb)<<11))) |
| 85 | |
| 86 | |
| 87 | static int plb2800_eth_init(struct eth_device *dev, bd_t * bis); |
| 88 | static int plb2800_eth_send(struct eth_device *dev, volatile void *packet, |
| 89 | int length); |
| 90 | static int plb2800_eth_recv(struct eth_device *dev); |
| 91 | static void plb2800_eth_halt(struct eth_device *dev); |
| 92 | |
| 93 | static void plb2800_set_mac_addr(struct eth_device *dev, unsigned char * addr); |
| 94 | static unsigned char * plb2800_get_mac_addr(void); |
| 95 | |
| 96 | static int rx_new; |
| 97 | static int mac_addr_set = 0; |
| 98 | |
| 99 | |
| 100 | int plb2800_eth_initialize(bd_t * bis) |
| 101 | { |
| 102 | struct eth_device *dev; |
| 103 | ulong temp; |
| 104 | |
| 105 | #ifdef DEBUG |
| 106 | printf("Entered plb2800_eth_initialize()\n"); |
| 107 | #endif |
| 108 | |
| 109 | if (!(dev = (struct eth_device *) malloc (sizeof *dev))) |
| 110 | { |
| 111 | printf("Failed to allocate memory\n"); |
| 112 | return 0; |
| 113 | } |
| 114 | memset(dev, 0, sizeof(*dev)); |
| 115 | |
| 116 | sprintf(dev->name, "PLB2800 Switch"); |
| 117 | dev->init = plb2800_eth_init; |
| 118 | dev->halt = plb2800_eth_halt; |
| 119 | dev->send = plb2800_eth_send; |
| 120 | dev->recv = plb2800_eth_recv; |
| 121 | |
| 122 | eth_register(dev); |
| 123 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 124 | /* bug fix */ |
| 125 | *(ulong *)0xb800e800 = 0x838; |
wdenk | a6db71d | 2003-04-08 23:25:21 +0000 | [diff] [blame] | 126 | |
| 127 | /* Set MBOX ownership */ |
| 128 | temp = CMAC_CRIT << MBOX_STAT_ID_SHF; |
| 129 | MBOX_REG(0)->stat = temp; |
| 130 | MBOX_REG(1)->stat = temp; |
| 131 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 132 | temp = CMAC_NON_CRIT << MBOX_STAT_ID_SHF; |
wdenk | a6db71d | 2003-04-08 23:25:21 +0000 | [diff] [blame] | 133 | MBOX_REG(2)->stat = temp; |
| 134 | MBOX_REG(3)->stat = temp; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 135 | |
wdenk | a6db71d | 2003-04-08 23:25:21 +0000 | [diff] [blame] | 136 | plb2800_set_mac_addr(dev, plb2800_get_mac_addr()); |
| 137 | |
| 138 | /* Disable all Mbox interrupt */ |
| 139 | temp = MIPS_H_MASK; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 140 | temp &= ~ (SW_H_MBOX1_MASK | SW_H_MBOX2_MASK | SW_H_MBOX3_MASK | SW_H_MBOX4_MASK) ; |
wdenk | a6db71d | 2003-04-08 23:25:21 +0000 | [diff] [blame] | 141 | MIPS_H_MASK = temp; |
| 142 | |
| 143 | #ifdef DEBUG |
| 144 | printf("Leaving plb2800_eth_initialize()\n"); |
| 145 | #endif |
| 146 | |
| 147 | return 1; |
| 148 | } |
| 149 | |
| 150 | static int plb2800_eth_init(struct eth_device *dev, bd_t * bis) |
| 151 | { |
| 152 | #ifdef DEBUG |
| 153 | printf("Entering plb2800_eth_init()\n"); |
| 154 | #endif |
| 155 | |
| 156 | plb2800_set_mac_addr(dev, dev->enetaddr); |
| 157 | |
| 158 | rx_new = 0; |
| 159 | |
| 160 | #ifdef DEBUG |
| 161 | printf("Leaving plb2800_eth_init()\n"); |
| 162 | #endif |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | static int plb2800_eth_send(struct eth_device *dev, volatile void *packet, |
| 169 | int length) |
| 170 | { |
| 171 | int i; |
| 172 | int res = -1; |
| 173 | u32 temp; |
| 174 | mailbox_t * mb = MBOX_REG(0); |
| 175 | char * mem = MBOX_MEM(0); |
| 176 | |
| 177 | #ifdef DEBUG |
| 178 | printf("Entered plb2800_eth_send()\n"); |
| 179 | #endif |
| 180 | |
| 181 | if (length <= 0) |
| 182 | { |
| 183 | printf ("%s: bad packet size: %d\n", dev->name, length); |
| 184 | goto Done; |
| 185 | } |
| 186 | |
| 187 | if (length < 64) |
| 188 | { |
| 189 | length = 64; |
| 190 | } |
| 191 | |
| 192 | temp = CMAC_CRX_CTRL_CG | ((length + 4) << CMAC_CRX_CTRL_PL_SHIFT); |
| 193 | |
| 194 | #ifdef DEBUG |
| 195 | printf("0 mb->stat = 0x%x\n", mb->stat); |
| 196 | #endif |
| 197 | |
| 198 | for(i = 0; mb->stat & (MBOX_STAT_CP | MBOX_STAT_MB); i++) |
| 199 | { |
| 200 | if (i >= TOUT_LOOP) |
| 201 | { |
| 202 | printf("%s: tx buffer not ready\n", dev->name); |
| 203 | printf("1 mb->stat = 0x%x\n", mb->stat); |
| 204 | goto Done; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /* For some strange reason, memcpy doesn't work, here! |
| 209 | */ |
| 210 | do |
| 211 | { |
| 212 | int words = (length >> 2) + 1; |
| 213 | unsigned int* dst = (unsigned int*)(mem); |
| 214 | unsigned int* src = (unsigned int*)(packet); |
| 215 | for (i = 0; i < words; i++) |
| 216 | { |
| 217 | *dst = *src; |
| 218 | dst++; |
| 219 | src++; |
| 220 | }; |
| 221 | } while(0); |
| 222 | |
| 223 | CMAC_CRX_CTRL = temp; |
| 224 | mb->cmd = MBOX_STAT_CP; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 225 | |
wdenk | a6db71d | 2003-04-08 23:25:21 +0000 | [diff] [blame] | 226 | #ifdef DEBUG |
| 227 | printf("2 mb->stat = 0x%x\n", mb->stat); |
| 228 | #endif |
| 229 | |
| 230 | res = length; |
| 231 | Done: |
| 232 | |
| 233 | #ifdef DEBUG |
| 234 | printf("Leaving plb2800_eth_send()\n"); |
| 235 | #endif |
| 236 | |
| 237 | return res; |
| 238 | } |
| 239 | |
| 240 | |
| 241 | static int plb2800_eth_recv(struct eth_device *dev) |
| 242 | { |
| 243 | int length = 0; |
| 244 | mailbox_t * mbox = MBOX_REG(3); |
| 245 | unsigned char * hdr = MBOX_MEM(3); |
| 246 | unsigned int stat; |
| 247 | |
| 248 | #ifdef DEBUG |
| 249 | printf("Entered plb2800_eth_recv()\n"); |
| 250 | #endif |
| 251 | |
| 252 | for (;;) |
| 253 | { |
| 254 | stat = mbox->stat; |
| 255 | |
| 256 | if (!(stat & MBOX_STAT_CP)) |
| 257 | { |
| 258 | break; |
| 259 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 260 | |
wdenk | a6db71d | 2003-04-08 23:25:21 +0000 | [diff] [blame] | 261 | length = ((*(hdr + 6) & 0x3f) << 8) + *(hdr + 7); |
| 262 | memcpy((void *)NetRxPackets[rx_new], hdr + 12, length); |
| 263 | |
| 264 | stat &= ~MBOX_STAT_CP; |
| 265 | mbox->stat = stat; |
| 266 | #ifdef DEBUG |
| 267 | { |
| 268 | int i; |
| 269 | for (i=0;i<length - 4;i++) |
| 270 | { |
| 271 | if (i % 16 == 0) printf("\n%04x: ", i); |
| 272 | printf("%02X ", NetRxPackets[rx_new][i]); |
| 273 | } |
| 274 | printf("\n"); |
| 275 | } |
| 276 | #endif |
| 277 | |
| 278 | if (length) |
| 279 | { |
| 280 | #ifdef DEBUG |
| 281 | printf("Received %d bytes\n", length); |
| 282 | #endif |
| 283 | NetReceive((void*)(NetRxPackets[rx_new]), |
| 284 | length - 4); |
| 285 | } |
| 286 | else |
| 287 | { |
| 288 | #if 1 |
| 289 | printf("Zero length!!!\n"); |
| 290 | #endif |
| 291 | } |
| 292 | |
| 293 | rx_new = (rx_new + 1) % NUM_RX_DESC; |
| 294 | } |
| 295 | |
| 296 | #ifdef DEBUG |
| 297 | printf("Leaving plb2800_eth_recv()\n"); |
| 298 | #endif |
| 299 | |
| 300 | return length; |
| 301 | } |
| 302 | |
| 303 | |
| 304 | static void plb2800_eth_halt(struct eth_device *dev) |
| 305 | { |
| 306 | #ifdef DEBUG |
| 307 | printf("Entered plb2800_eth_halt()\n"); |
| 308 | #endif |
| 309 | |
| 310 | #ifdef DEBUG |
| 311 | printf("Leaving plb2800_eth_halt()\n"); |
| 312 | #endif |
| 313 | } |
| 314 | |
| 315 | static void plb2800_set_mac_addr(struct eth_device *dev, unsigned char * addr) |
| 316 | { |
| 317 | char packet[60]; |
| 318 | ulong temp; |
| 319 | int ix; |
| 320 | |
| 321 | if (mac_addr_set || |
| 322 | NULL == addr || memcmp(addr, "\0\0\0\0\0\0", 6) == 0) |
| 323 | { |
| 324 | return; |
| 325 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 326 | |
wdenk | a6db71d | 2003-04-08 23:25:21 +0000 | [diff] [blame] | 327 | /* send one packet through CPU port |
| 328 | * in order to learn system MAC address |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 329 | */ |
wdenk | a6db71d | 2003-04-08 23:25:21 +0000 | [diff] [blame] | 330 | |
| 331 | /* Set DA_LOOKUP register */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 332 | temp = EN_MA_LEARN | (0 << DA_STATE_SHF) | (63 << DA_DEST_SHF); |
wdenk | a6db71d | 2003-04-08 23:25:21 +0000 | [diff] [blame] | 333 | DA_LOOKUP = temp; |
| 334 | |
| 335 | /* Set MA_LEARN register */ |
| 336 | temp = 50 << MA_DEST_SHF; /* static entry */ |
| 337 | MA_LEARN = temp; |
| 338 | |
| 339 | /* set destination address */ |
| 340 | for (ix=0;ix<6;ix++) |
| 341 | packet[ix] = 0xff; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 342 | |
wdenk | a6db71d | 2003-04-08 23:25:21 +0000 | [diff] [blame] | 343 | /* set source address = system MAC address */ |
| 344 | for (ix=0;ix<6;ix++) |
| 345 | packet[6+ix] = addr[ix]; |
| 346 | |
| 347 | /* set type field */ |
| 348 | packet[12]=0xaa; |
| 349 | packet[13]=0x55; |
| 350 | |
| 351 | /* set data field */ |
| 352 | for(ix=14;ix<60;ix++) |
| 353 | packet[ix] = 0x00; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 354 | |
wdenk | a6db71d | 2003-04-08 23:25:21 +0000 | [diff] [blame] | 355 | #ifdef DEBUG |
| 356 | for (ix=0;ix<6;ix++) |
| 357 | printf("mac_addr[%d]=%02X\n", ix, (unsigned char)packet[6+ix]); |
| 358 | #endif |
| 359 | |
| 360 | /* set one packet */ |
| 361 | plb2800_eth_send(dev, packet, sizeof(packet)); |
| 362 | |
| 363 | /* delay for a while */ |
| 364 | for(ix=0;ix<65535;ix++) |
| 365 | temp = ~temp; |
| 366 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 367 | /* Set CMAC_CTX_CTRL register */ |
wdenk | a6db71d | 2003-04-08 23:25:21 +0000 | [diff] [blame] | 368 | temp = TSTAMP_MS; /* no autocast */ |
| 369 | CMAC_CTX_CTRL = temp; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 370 | |
wdenk | a6db71d | 2003-04-08 23:25:21 +0000 | [diff] [blame] | 371 | /* Set DA_LOOKUP register */ |
| 372 | temp = EN_DA_LKUP; |
| 373 | DA_LOOKUP = temp; |
| 374 | |
| 375 | mac_addr_set = 1; |
| 376 | } |
| 377 | |
| 378 | static unsigned char * plb2800_get_mac_addr(void) |
| 379 | { |
| 380 | static unsigned char addr[6]; |
| 381 | char *tmp, *end; |
| 382 | int i; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 383 | |
wdenk | a6db71d | 2003-04-08 23:25:21 +0000 | [diff] [blame] | 384 | tmp = getenv ("ethaddr"); |
| 385 | if (NULL == tmp) return NULL; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 386 | |
wdenk | a6db71d | 2003-04-08 23:25:21 +0000 | [diff] [blame] | 387 | for (i=0; i<6; i++) { |
| 388 | addr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0; |
| 389 | if (tmp) |
| 390 | tmp = (*end) ? end+1 : end; |
| 391 | } |
| 392 | |
| 393 | return addr; |
| 394 | } |
| 395 | |
| 396 | #endif /* CONFIG_PLB2800_ETHER */ |