blob: 251a0292d5c46d45472ab46929284b692150453e [file] [log] [blame]
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +01001/*
2 * Copyright (C) 2005-2006 Atmel Corporation
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18#include <common.h>
19
Jon Loeligerd8c86302007-06-11 19:02:10 -050020#if defined(CONFIG_MACB) \
21 && ((CONFIG_COMMANDS & (CFG_CMD_NET | CFG_CMD_MII)) \
22 || (defined(CONFIG_CMD_NET) || defined(CONFIG_CMD_MII)))
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +010023
24/*
25 * The u-boot networking stack is a little weird. It seems like the
26 * networking core allocates receive buffers up front without any
27 * regard to the hardware that's supposed to actually receive those
28 * packets.
29 *
30 * The MACB receives packets into 128-byte receive buffers, so the
31 * buffers allocated by the core isn't very practical to use. We'll
32 * allocate our own, but we need one such buffer in case a packet
33 * wraps around the DMA ring so that we have to copy it.
34 *
35 * Therefore, define CFG_RX_ETH_BUFFER to 1 in the board-specific
36 * configuration header. This way, the core allocates one RX buffer
37 * and one TX buffer, each of which can hold a ethernet packet of
38 * maximum size.
39 *
40 * For some reason, the networking core unconditionally specifies a
41 * 32-byte packet "alignment" (which really should be called
42 * "padding"). MACB shouldn't need that, but we'll refrain from any
43 * core modifications here...
44 */
45
46#include <net.h>
47#include <malloc.h>
48
49#include <linux/mii.h>
50#include <asm/io.h>
51#include <asm/dma-mapping.h>
52#include <asm/arch/clk.h>
53
54#include "macb.h"
55
56#define CFG_MACB_RX_BUFFER_SIZE 4096
57#define CFG_MACB_RX_RING_SIZE (CFG_MACB_RX_BUFFER_SIZE / 128)
58#define CFG_MACB_TX_RING_SIZE 16
59#define CFG_MACB_TX_TIMEOUT 1000
60#define CFG_MACB_AUTONEG_TIMEOUT 5000000
61
62struct macb_dma_desc {
63 u32 addr;
64 u32 ctrl;
65};
66
67#define RXADDR_USED 0x00000001
68#define RXADDR_WRAP 0x00000002
69
70#define RXBUF_FRMLEN_MASK 0x00000fff
71#define RXBUF_FRAME_START 0x00004000
72#define RXBUF_FRAME_END 0x00008000
73#define RXBUF_TYPEID_MATCH 0x00400000
74#define RXBUF_ADDR4_MATCH 0x00800000
75#define RXBUF_ADDR3_MATCH 0x01000000
76#define RXBUF_ADDR2_MATCH 0x02000000
77#define RXBUF_ADDR1_MATCH 0x04000000
78#define RXBUF_BROADCAST 0x80000000
79
80#define TXBUF_FRMLEN_MASK 0x000007ff
81#define TXBUF_FRAME_END 0x00008000
82#define TXBUF_NOCRC 0x00010000
83#define TXBUF_EXHAUSTED 0x08000000
84#define TXBUF_UNDERRUN 0x10000000
85#define TXBUF_MAXRETRY 0x20000000
86#define TXBUF_WRAP 0x40000000
87#define TXBUF_USED 0x80000000
88
89struct macb_device {
90 void *regs;
91
92 unsigned int rx_tail;
93 unsigned int tx_head;
94 unsigned int tx_tail;
95
96 void *rx_buffer;
97 void *tx_buffer;
98 struct macb_dma_desc *rx_ring;
99 struct macb_dma_desc *tx_ring;
100
101 unsigned long rx_buffer_dma;
102 unsigned long rx_ring_dma;
103 unsigned long tx_ring_dma;
104
105 const struct device *dev;
106 struct eth_device netdev;
107 unsigned short phy_addr;
108};
109#define to_macb(_nd) container_of(_nd, struct macb_device, netdev)
110
111static void macb_mdio_write(struct macb_device *macb, u8 reg, u16 value)
112{
113 unsigned long netctl;
114 unsigned long netstat;
115 unsigned long frame;
116
117 netctl = macb_readl(macb, NCR);
118 netctl |= MACB_BIT(MPE);
119 macb_writel(macb, NCR, netctl);
120
121 frame = (MACB_BF(SOF, 1)
122 | MACB_BF(RW, 1)
123 | MACB_BF(PHYA, macb->phy_addr)
124 | MACB_BF(REGA, reg)
125 | MACB_BF(CODE, 2)
126 | MACB_BF(DATA, value));
127 macb_writel(macb, MAN, frame);
128
129 do {
130 netstat = macb_readl(macb, NSR);
131 } while (!(netstat & MACB_BIT(IDLE)));
132
133 netctl = macb_readl(macb, NCR);
134 netctl &= ~MACB_BIT(MPE);
135 macb_writel(macb, NCR, netctl);
136}
137
138static u16 macb_mdio_read(struct macb_device *macb, u8 reg)
139{
140 unsigned long netctl;
141 unsigned long netstat;
142 unsigned long frame;
143
144 netctl = macb_readl(macb, NCR);
145 netctl |= MACB_BIT(MPE);
146 macb_writel(macb, NCR, netctl);
147
148 frame = (MACB_BF(SOF, 1)
149 | MACB_BF(RW, 2)
150 | MACB_BF(PHYA, macb->phy_addr)
151 | MACB_BF(REGA, reg)
152 | MACB_BF(CODE, 2));
153 macb_writel(macb, MAN, frame);
154
155 do {
156 netstat = macb_readl(macb, NSR);
157 } while (!(netstat & MACB_BIT(IDLE)));
158
159 frame = macb_readl(macb, MAN);
160
161 netctl = macb_readl(macb, NCR);
162 netctl &= ~MACB_BIT(MPE);
163 macb_writel(macb, NCR, netctl);
164
165 return MACB_BFEXT(DATA, frame);
166}
167
Jon Loeligerd8c86302007-06-11 19:02:10 -0500168#if (CONFIG_COMMANDS & CFG_CMD_NET) || defined(CONFIG_CMD_NET)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100169
170static int macb_send(struct eth_device *netdev, volatile void *packet,
171 int length)
172{
173 struct macb_device *macb = to_macb(netdev);
174 unsigned long paddr, ctrl;
175 unsigned int tx_head = macb->tx_head;
176 int i;
177
178 paddr = dma_map_single(packet, length, DMA_TO_DEVICE);
179
180 ctrl = length & TXBUF_FRMLEN_MASK;
181 ctrl |= TXBUF_FRAME_END;
182 if (tx_head == (CFG_MACB_TX_RING_SIZE - 1)) {
183 ctrl |= TXBUF_WRAP;
184 macb->tx_head = 0;
185 } else
186 macb->tx_head++;
187
188 macb->tx_ring[tx_head].ctrl = ctrl;
189 macb->tx_ring[tx_head].addr = paddr;
190 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
191
192 /*
193 * I guess this is necessary because the networking core may
194 * re-use the transmit buffer as soon as we return...
195 */
196 i = 0;
197 while (!(macb->tx_ring[tx_head].ctrl & TXBUF_USED)) {
198 if (i > CFG_MACB_TX_TIMEOUT) {
199 printf("%s: TX timeout\n", netdev->name);
200 break;
201 }
202 udelay(1);
203 i++;
204 }
205
206 dma_unmap_single(packet, length, paddr);
207
208 if (i <= CFG_MACB_TX_TIMEOUT) {
209 ctrl = macb->tx_ring[tx_head].ctrl;
210 if (ctrl & TXBUF_UNDERRUN)
211 printf("%s: TX underrun\n", netdev->name);
212 if (ctrl & TXBUF_EXHAUSTED)
213 printf("%s: TX buffers exhausted in mid frame\n",
214 netdev->name);
215 }
216
217 /* No one cares anyway */
218 return 0;
219}
220
221static void reclaim_rx_buffers(struct macb_device *macb,
222 unsigned int new_tail)
223{
224 unsigned int i;
225
226 i = macb->rx_tail;
227 while (i > new_tail) {
228 macb->rx_ring[i].addr &= ~RXADDR_USED;
229 i++;
230 if (i > CFG_MACB_RX_RING_SIZE)
231 i = 0;
232 }
233
234 while (i < new_tail) {
235 macb->rx_ring[i].addr &= ~RXADDR_USED;
236 i++;
237 }
238
239 macb->rx_tail = new_tail;
240}
241
242static int macb_recv(struct eth_device *netdev)
243{
244 struct macb_device *macb = to_macb(netdev);
245 unsigned int rx_tail = macb->rx_tail;
246 void *buffer;
247 int length;
248 int wrapped = 0;
249 u32 status;
250
251 for (;;) {
252 if (!(macb->rx_ring[rx_tail].addr & RXADDR_USED))
253 return -1;
254
255 status = macb->rx_ring[rx_tail].ctrl;
256 if (status & RXBUF_FRAME_START) {
257 if (rx_tail != macb->rx_tail)
258 reclaim_rx_buffers(macb, rx_tail);
259 wrapped = 0;
260 }
261
262 if (status & RXBUF_FRAME_END) {
263 buffer = macb->rx_buffer + 128 * macb->rx_tail;
264 length = status & RXBUF_FRMLEN_MASK;
265 if (wrapped) {
266 unsigned int headlen, taillen;
267
268 headlen = 128 * (CFG_MACB_RX_RING_SIZE
269 - macb->rx_tail);
270 taillen = length - headlen;
271 memcpy((void *)NetRxPackets[0],
272 buffer, headlen);
273 memcpy((void *)NetRxPackets[0] + headlen,
274 macb->rx_buffer, taillen);
275 buffer = (void *)NetRxPackets[0];
276 }
277
278 NetReceive(buffer, length);
279 if (++rx_tail >= CFG_MACB_RX_RING_SIZE)
280 rx_tail = 0;
281 reclaim_rx_buffers(macb, rx_tail);
282 } else {
283 if (++rx_tail >= CFG_MACB_RX_RING_SIZE) {
284 wrapped = 1;
285 rx_tail = 0;
286 }
287 }
288 }
289
290 return 0;
291}
292
293static int macb_phy_init(struct macb_device *macb)
294{
295 struct eth_device *netdev = &macb->netdev;
296 u32 ncfgr;
297 u16 phy_id, status, adv, lpa;
298 int media, speed, duplex;
299 int i;
300
301 /* Check if the PHY is up to snuff... */
302 phy_id = macb_mdio_read(macb, MII_PHYSID1);
303 if (phy_id == 0xffff) {
304 printf("%s: No PHY present\n", netdev->name);
305 return 0;
306 }
307
308 adv = ADVERTISE_CSMA | ADVERTISE_ALL;
309 macb_mdio_write(macb, MII_ADVERTISE, adv);
310 printf("%s: Starting autonegotiation...\n", netdev->name);
311 macb_mdio_write(macb, MII_BMCR, (BMCR_ANENABLE
312 | BMCR_ANRESTART));
313
314#if 0
315 for (i = 0; i < 9; i++)
316 printf("mii%d: 0x%04x\n", i, macb_mdio_read(macb, i));
317#endif
318
319 for (i = 0; i < CFG_MACB_AUTONEG_TIMEOUT / 100; i++) {
320 status = macb_mdio_read(macb, MII_BMSR);
321 if (status & BMSR_ANEGCOMPLETE)
322 break;
323 udelay(100);
324 }
325
326 if (status & BMSR_ANEGCOMPLETE)
327 printf("%s: Autonegotiation complete\n", netdev->name);
328 else
329 printf("%s: Autonegotiation timed out (status=0x%04x)\n",
330 netdev->name, status);
331
332 if (!(status & BMSR_LSTATUS)) {
333 for (i = 0; i < CFG_MACB_AUTONEG_TIMEOUT / 100; i++) {
334 udelay(100);
335 status = macb_mdio_read(macb, MII_BMSR);
336 if (status & BMSR_LSTATUS)
337 break;
338 }
339 }
340
341 if (!(status & BMSR_LSTATUS)) {
342 printf("%s: link down (status: 0x%04x)\n",
343 netdev->name, status);
344 return 0;
345 } else {
346 lpa = macb_mdio_read(macb, MII_LPA);
347 media = mii_nway_result(lpa & adv);
348 speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
349 ? 1 : 0);
350 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
351 printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
352 netdev->name,
353 speed ? "100" : "10",
354 duplex ? "full" : "half",
355 lpa);
356
357 ncfgr = macb_readl(macb, NCFGR);
358 ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
359 if (speed)
360 ncfgr |= MACB_BIT(SPD);
361 if (duplex)
362 ncfgr |= MACB_BIT(FD);
363 macb_writel(macb, NCFGR, ncfgr);
364 return 1;
365 }
366}
367
368static int macb_init(struct eth_device *netdev, bd_t *bd)
369{
370 struct macb_device *macb = to_macb(netdev);
371 unsigned long paddr;
372 u32 hwaddr_bottom;
373 u16 hwaddr_top;
374 int i;
375
376 /*
377 * macb_halt should have been called at some point before now,
378 * so we'll assume the controller is idle.
379 */
380
381 /* initialize DMA descriptors */
382 paddr = macb->rx_buffer_dma;
383 for (i = 0; i < CFG_MACB_RX_RING_SIZE; i++) {
384 if (i == (CFG_MACB_RX_RING_SIZE - 1))
385 paddr |= RXADDR_WRAP;
386 macb->rx_ring[i].addr = paddr;
387 macb->rx_ring[i].ctrl = 0;
388 paddr += 128;
389 }
390 for (i = 0; i < CFG_MACB_TX_RING_SIZE; i++) {
391 macb->tx_ring[i].addr = 0;
392 if (i == (CFG_MACB_TX_RING_SIZE - 1))
393 macb->tx_ring[i].ctrl = TXBUF_USED | TXBUF_WRAP;
394 else
395 macb->tx_ring[i].ctrl = TXBUF_USED;
396 }
397 macb->rx_tail = macb->tx_head = macb->tx_tail = 0;
398
399 macb_writel(macb, RBQP, macb->rx_ring_dma);
400 macb_writel(macb, TBQP, macb->tx_ring_dma);
401
402 /* set hardware address */
403 hwaddr_bottom = cpu_to_le32(*((u32 *)netdev->enetaddr));
404 macb_writel(macb, SA1B, hwaddr_bottom);
405 hwaddr_top = cpu_to_le16(*((u16 *)(netdev->enetaddr + 4)));
406 macb_writel(macb, SA1T, hwaddr_top);
407
408 /* choose RMII or MII mode. This depends on the board */
409#ifdef CONFIG_RMII
410 macb_writel(macb, USRIO, 0);
411#else
412 macb_writel(macb, USRIO, MACB_BIT(MII));
413#endif
414
415 if (!macb_phy_init(macb))
416 return 0;
417
418 /* Enable TX and RX */
419 macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE));
420
421 return 1;
422}
423
424static void macb_halt(struct eth_device *netdev)
425{
426 struct macb_device *macb = to_macb(netdev);
427 u32 ncr, tsr;
428
429 /* Halt the controller and wait for any ongoing transmission to end. */
430 ncr = macb_readl(macb, NCR);
431 ncr |= MACB_BIT(THALT);
432 macb_writel(macb, NCR, ncr);
433
434 do {
435 tsr = macb_readl(macb, TSR);
436 } while (tsr & MACB_BIT(TGO));
437
438 /* Disable TX and RX, and clear statistics */
439 macb_writel(macb, NCR, MACB_BIT(CLRSTAT));
440}
441
442int macb_eth_initialize(int id, void *regs, unsigned int phy_addr)
443{
444 struct macb_device *macb;
445 struct eth_device *netdev;
446 unsigned long macb_hz;
447 u32 ncfgr;
448
449 macb = malloc(sizeof(struct macb_device));
450 if (!macb) {
451 printf("Error: Failed to allocate memory for MACB%d\n", id);
452 return -1;
453 }
454 memset(macb, 0, sizeof(struct macb_device));
455
456 netdev = &macb->netdev;
457
458 macb->rx_buffer = dma_alloc_coherent(CFG_MACB_RX_BUFFER_SIZE,
459 &macb->rx_buffer_dma);
460 macb->rx_ring = dma_alloc_coherent(CFG_MACB_RX_RING_SIZE
461 * sizeof(struct macb_dma_desc),
462 &macb->rx_ring_dma);
463 macb->tx_ring = dma_alloc_coherent(CFG_MACB_TX_RING_SIZE
464 * sizeof(struct macb_dma_desc),
465 &macb->tx_ring_dma);
466
467 macb->regs = regs;
468 macb->phy_addr = phy_addr;
469
470 sprintf(netdev->name, "macb%d", id);
471 netdev->init = macb_init;
472 netdev->halt = macb_halt;
473 netdev->send = macb_send;
474 netdev->recv = macb_recv;
475
476 /*
477 * Do some basic initialization so that we at least can talk
478 * to the PHY
479 */
480 macb_hz = get_macb_pclk_rate(id);
481 if (macb_hz < 20000000)
482 ncfgr = MACB_BF(CLK, MACB_CLK_DIV8);
483 else if (macb_hz < 40000000)
484 ncfgr = MACB_BF(CLK, MACB_CLK_DIV16);
485 else if (macb_hz < 80000000)
486 ncfgr = MACB_BF(CLK, MACB_CLK_DIV32);
487 else
488 ncfgr = MACB_BF(CLK, MACB_CLK_DIV64);
489
490 macb_writel(macb, NCFGR, ncfgr);
491
492 eth_register(netdev);
493
494 return 0;
495}
496
497#endif /* (CONFIG_COMMANDS & CFG_CMD_NET) */
498
Jon Loeligerd8c86302007-06-11 19:02:10 -0500499#if (CONFIG_COMMANDS & CFG_CMD_MII) || defined(CONFIG_CMD_MII)
Haavard Skinnemoen51c8f242006-01-20 10:03:34 +0100500
501int miiphy_read(unsigned char addr, unsigned char reg, unsigned short *value)
502{
503 unsigned long netctl;
504 unsigned long netstat;
505 unsigned long frame;
506 int iflag;
507
508 iflag = disable_interrupts();
509 netctl = macb_readl(&macb, EMACB_NCR);
510 netctl |= MACB_BIT(MPE);
511 macb_writel(&macb, EMACB_NCR, netctl);
512 if (iflag)
513 enable_interrupts();
514
515 frame = (MACB_BF(SOF, 1)
516 | MACB_BF(RW, 2)
517 | MACB_BF(PHYA, addr)
518 | MACB_BF(REGA, reg)
519 | MACB_BF(CODE, 2));
520 macb_writel(&macb, EMACB_MAN, frame);
521
522 do {
523 netstat = macb_readl(&macb, EMACB_NSR);
524 } while (!(netstat & MACB_BIT(IDLE)));
525
526 frame = macb_readl(&macb, EMACB_MAN);
527 *value = MACB_BFEXT(DATA, frame);
528
529 iflag = disable_interrupts();
530 netctl = macb_readl(&macb, EMACB_NCR);
531 netctl &= ~MACB_BIT(MPE);
532 macb_writel(&macb, EMACB_NCR, netctl);
533 if (iflag)
534 enable_interrupts();
535
536 return 0;
537}
538
539int miiphy_write(unsigned char addr, unsigned char reg, unsigned short value)
540{
541 unsigned long netctl;
542 unsigned long netstat;
543 unsigned long frame;
544 int iflag;
545
546 iflag = disable_interrupts();
547 netctl = macb_readl(&macb, EMACB_NCR);
548 netctl |= MACB_BIT(MPE);
549 macb_writel(&macb, EMACB_NCR, netctl);
550 if (iflag)
551 enable_interrupts();
552
553 frame = (MACB_BF(SOF, 1)
554 | MACB_BF(RW, 1)
555 | MACB_BF(PHYA, addr)
556 | MACB_BF(REGA, reg)
557 | MACB_BF(CODE, 2)
558 | MACB_BF(DATA, value));
559 macb_writel(&macb, EMACB_MAN, frame);
560
561 do {
562 netstat = macb_readl(&macb, EMACB_NSR);
563 } while (!(netstat & MACB_BIT(IDLE)));
564
565 iflag = disable_interrupts();
566 netctl = macb_readl(&macb, EMACB_NCR);
567 netctl &= ~MACB_BIT(MPE);
568 macb_writel(&macb, EMACB_NCR, netctl);
569 if (iflag)
570 enable_interrupts();
571
572 return 0;
573}
574
575#endif /* (CONFIG_COMMANDS & CFG_CMD_MII) */
576
577#endif /* CONFIG_MACB */