wait-for-power.patch
[u-boot-openmoko/mini2440.git] / drivers / net / plb2800_eth.c
blob0ae5d808a6f9d055ad73f2202737adf4c58f1dbb
1 /*
2 * PLB2800 internal switch ethernet driver.
4 * (C) Copyright 2003
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * See file CREDITS for list of people who contributed to this
8 * project.
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.
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.
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
26 #include <common.h>
28 #if defined(CONFIG_CMD_NET) \
29 && defined(CONFIG_NET_MULTI) && defined(CONFIG_PLB2800_ETHER)
31 #include <malloc.h>
32 #include <net.h>
33 #include <asm/addrspace.h>
36 #define NUM_RX_DESC PKTBUFSRX
37 #define TOUT_LOOP 1000000
39 #define LONG_REF(addr) (*((volatile unsigned long*)addr))
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)
47 #define MA_LEARN LONG_REF(0xb8008004)
48 #define DA_LOOKUP LONG_REF(0xb8008008)
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
60 #define MA_DEST_SHF 11
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
69 typedef volatile struct {
70 unsigned int stat;
71 unsigned int cmd;
72 unsigned int cnt;
73 unsigned int adr;
74 } mailbox_t;
76 #define MBOX_REG(mb) ((mailbox_t*)(0xb800c830+(mb<<4)))
78 typedef volatile struct {
79 unsigned int word0;
80 unsigned int word1;
81 unsigned int word2;
82 } mbhdr_t;
84 #define MBOX_MEM(mb) ((void*)(0xb800a000+((3-mb)<<11)))
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);
93 static void plb2800_set_mac_addr(struct eth_device *dev, unsigned char * addr);
94 static unsigned char * plb2800_get_mac_addr(void);
96 static int rx_new;
97 static int mac_addr_set = 0;
100 int plb2800_eth_initialize(bd_t * bis)
102 struct eth_device *dev;
103 ulong temp;
105 #ifdef DEBUG
106 printf("Entered plb2800_eth_initialize()\n");
107 #endif
109 if (!(dev = (struct eth_device *) malloc (sizeof *dev)))
111 printf("Failed to allocate memory\n");
112 return 0;
114 memset(dev, 0, sizeof(*dev));
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;
122 eth_register(dev);
124 /* bug fix */
125 *(ulong *)0xb800e800 = 0x838;
127 /* Set MBOX ownership */
128 temp = CMAC_CRIT << MBOX_STAT_ID_SHF;
129 MBOX_REG(0)->stat = temp;
130 MBOX_REG(1)->stat = temp;
132 temp = CMAC_NON_CRIT << MBOX_STAT_ID_SHF;
133 MBOX_REG(2)->stat = temp;
134 MBOX_REG(3)->stat = temp;
136 plb2800_set_mac_addr(dev, plb2800_get_mac_addr());
138 /* Disable all Mbox interrupt */
139 temp = MIPS_H_MASK;
140 temp &= ~ (SW_H_MBOX1_MASK | SW_H_MBOX2_MASK | SW_H_MBOX3_MASK | SW_H_MBOX4_MASK) ;
141 MIPS_H_MASK = temp;
143 #ifdef DEBUG
144 printf("Leaving plb2800_eth_initialize()\n");
145 #endif
147 return 1;
150 static int plb2800_eth_init(struct eth_device *dev, bd_t * bis)
152 #ifdef DEBUG
153 printf("Entering plb2800_eth_init()\n");
154 #endif
156 plb2800_set_mac_addr(dev, dev->enetaddr);
158 rx_new = 0;
160 #ifdef DEBUG
161 printf("Leaving plb2800_eth_init()\n");
162 #endif
164 return 0;
168 static int plb2800_eth_send(struct eth_device *dev, volatile void *packet,
169 int length)
171 int i;
172 int res = -1;
173 u32 temp;
174 mailbox_t * mb = MBOX_REG(0);
175 char * mem = MBOX_MEM(0);
177 #ifdef DEBUG
178 printf("Entered plb2800_eth_send()\n");
179 #endif
181 if (length <= 0)
183 printf ("%s: bad packet size: %d\n", dev->name, length);
184 goto Done;
187 if (length < 64)
189 length = 64;
192 temp = CMAC_CRX_CTRL_CG | ((length + 4) << CMAC_CRX_CTRL_PL_SHIFT);
194 #ifdef DEBUG
195 printf("0 mb->stat = 0x%x\n", mb->stat);
196 #endif
198 for(i = 0; mb->stat & (MBOX_STAT_CP | MBOX_STAT_MB); i++)
200 if (i >= TOUT_LOOP)
202 printf("%s: tx buffer not ready\n", dev->name);
203 printf("1 mb->stat = 0x%x\n", mb->stat);
204 goto Done;
208 /* For some strange reason, memcpy doesn't work, here!
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++)
217 *dst = *src;
218 dst++;
219 src++;
221 } while(0);
223 CMAC_CRX_CTRL = temp;
224 mb->cmd = MBOX_STAT_CP;
226 #ifdef DEBUG
227 printf("2 mb->stat = 0x%x\n", mb->stat);
228 #endif
230 res = length;
231 Done:
233 #ifdef DEBUG
234 printf("Leaving plb2800_eth_send()\n");
235 #endif
237 return res;
241 static int plb2800_eth_recv(struct eth_device *dev)
243 int length = 0;
244 mailbox_t * mbox = MBOX_REG(3);
245 unsigned char * hdr = MBOX_MEM(3);
246 unsigned int stat;
248 #ifdef DEBUG
249 printf("Entered plb2800_eth_recv()\n");
250 #endif
252 for (;;)
254 stat = mbox->stat;
256 if (!(stat & MBOX_STAT_CP))
258 break;
261 length = ((*(hdr + 6) & 0x3f) << 8) + *(hdr + 7);
262 memcpy((void *)NetRxPackets[rx_new], hdr + 12, length);
264 stat &= ~MBOX_STAT_CP;
265 mbox->stat = stat;
266 #ifdef DEBUG
268 int i;
269 for (i=0;i<length - 4;i++)
271 if (i % 16 == 0) printf("\n%04x: ", i);
272 printf("%02X ", NetRxPackets[rx_new][i]);
274 printf("\n");
276 #endif
278 if (length)
280 #ifdef DEBUG
281 printf("Received %d bytes\n", length);
282 #endif
283 NetReceive((void*)(NetRxPackets[rx_new]),
284 length - 4);
286 else
288 #if 1
289 printf("Zero length!!!\n");
290 #endif
293 rx_new = (rx_new + 1) % NUM_RX_DESC;
296 #ifdef DEBUG
297 printf("Leaving plb2800_eth_recv()\n");
298 #endif
300 return length;
304 static void plb2800_eth_halt(struct eth_device *dev)
306 #ifdef DEBUG
307 printf("Entered plb2800_eth_halt()\n");
308 #endif
310 #ifdef DEBUG
311 printf("Leaving plb2800_eth_halt()\n");
312 #endif
315 static void plb2800_set_mac_addr(struct eth_device *dev, unsigned char * addr)
317 char packet[60];
318 ulong temp;
319 int ix;
321 if (mac_addr_set ||
322 NULL == addr || memcmp(addr, "\0\0\0\0\0\0", 6) == 0)
324 return;
327 /* send one packet through CPU port
328 * in order to learn system MAC address
331 /* Set DA_LOOKUP register */
332 temp = EN_MA_LEARN | (0 << DA_STATE_SHF) | (63 << DA_DEST_SHF);
333 DA_LOOKUP = temp;
335 /* Set MA_LEARN register */
336 temp = 50 << MA_DEST_SHF; /* static entry */
337 MA_LEARN = temp;
339 /* set destination address */
340 for (ix=0;ix<6;ix++)
341 packet[ix] = 0xff;
343 /* set source address = system MAC address */
344 for (ix=0;ix<6;ix++)
345 packet[6+ix] = addr[ix];
347 /* set type field */
348 packet[12]=0xaa;
349 packet[13]=0x55;
351 /* set data field */
352 for(ix=14;ix<60;ix++)
353 packet[ix] = 0x00;
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
360 /* set one packet */
361 plb2800_eth_send(dev, packet, sizeof(packet));
363 /* delay for a while */
364 for(ix=0;ix<65535;ix++)
365 temp = ~temp;
367 /* Set CMAC_CTX_CTRL register */
368 temp = TSTAMP_MS; /* no autocast */
369 CMAC_CTX_CTRL = temp;
371 /* Set DA_LOOKUP register */
372 temp = EN_DA_LKUP;
373 DA_LOOKUP = temp;
375 mac_addr_set = 1;
378 static unsigned char * plb2800_get_mac_addr(void)
380 static unsigned char addr[6];
381 char *tmp, *end;
382 int i;
384 tmp = getenv ("ethaddr");
385 if (NULL == tmp) return NULL;
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;
393 return addr;
396 #endif /* CONFIG_PLB2800_ETHER */