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