blob: 73612ea069108eab4e25ae8bcd10cee951d18318 [file] [log] [blame]
Jens Scharsigc041e9d2010-01-23 12:03:45 +01001/*
2 * Copyright (C) 2009 BuS Elektronik GmbH & Co. KG
3 * Jens Scharsig (esw@bus-elektronik.de)
4 *
5 * (C) Copyright 2003
6 * Author : Hamid Ikdoumi (Atmel)
7
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Jens Scharsigc041e9d2010-01-23 12:03:45 +01009 */
10
11#include <common.h>
12#include <asm/io.h>
13#ifndef CONFIG_AT91_LEGACY
14#include <asm/arch/hardware.h>
15#include <asm/arch/at91_emac.h>
16#include <asm/arch/at91_pmc.h>
17#include <asm/arch/at91_pio.h>
18#else
19/* remove next 5 lines, if all RM9200 boards convert to at91 arch */
20#include <asm/arch-at91/at91rm9200.h>
21#include <asm/arch-at91/hardware.h>
22#include <asm/arch-at91/at91_emac.h>
23#include <asm/arch-at91/at91_pmc.h>
24#include <asm/arch-at91/at91_pio.h>
25#endif
26#include <net.h>
27#include <netdev.h>
28#include <malloc.h>
29#include <miiphy.h>
30#include <linux/mii.h>
31
32#undef MII_DEBUG
33#undef ET_DEBUG
34
35#if (CONFIG_SYS_RX_ETH_BUFFER > 1024)
36#error AT91 EMAC supports max 1024 RX buffers. \
37 Please decrease the CONFIG_SYS_RX_ETH_BUFFER value
38#endif
39
Eric Bénard836cd452010-06-21 09:40:43 +020040#ifndef CONFIG_DRIVER_AT91EMAC_PHYADDR
41#define CONFIG_DRIVER_AT91EMAC_PHYADDR 0
42#endif
43
Jens Scharsigc041e9d2010-01-23 12:03:45 +010044/* MDIO clock must not exceed 2.5 MHz, so enable MCK divider */
45#if (AT91C_MASTER_CLOCK > 80000000)
46 #define HCLK_DIV AT91_EMAC_CFG_MCLK_64
47#elif (AT91C_MASTER_CLOCK > 40000000)
48 #define HCLK_DIV AT91_EMAC_CFG_MCLK_32
49#elif (AT91C_MASTER_CLOCK > 20000000)
50 #define HCLK_DIV AT91_EMAC_CFG_MCLK_16
51#else
52 #define HCLK_DIV AT91_EMAC_CFG_MCLK_8
53#endif
54
55#ifdef ET_DEBUG
Wolfgang Denkf4962062011-12-09 12:14:21 +010056#define DEBUG_AT91EMAC 1
Jens Scharsigc041e9d2010-01-23 12:03:45 +010057#else
Wolfgang Denkf4962062011-12-09 12:14:21 +010058#define DEBUG_AT91EMAC 0
Jens Scharsigc041e9d2010-01-23 12:03:45 +010059#endif
60
61#ifdef MII_DEBUG
Wolfgang Denkf4962062011-12-09 12:14:21 +010062#define DEBUG_AT91PHY 1
Jens Scharsigc041e9d2010-01-23 12:03:45 +010063#else
Wolfgang Denkf4962062011-12-09 12:14:21 +010064#define DEBUG_AT91PHY 0
Jens Scharsigc041e9d2010-01-23 12:03:45 +010065#endif
66
67#ifndef CONFIG_DRIVER_AT91EMAC_QUIET
Wolfgang Denkf4962062011-12-09 12:14:21 +010068#define VERBOSEP 1
Jens Scharsigc041e9d2010-01-23 12:03:45 +010069#else
Wolfgang Denkf4962062011-12-09 12:14:21 +010070#define VERBOSEP 0
Jens Scharsigc041e9d2010-01-23 12:03:45 +010071#endif
72
73#define RBF_ADDR 0xfffffffc
74#define RBF_OWNER (1<<0)
75#define RBF_WRAP (1<<1)
76#define RBF_BROADCAST (1<<31)
77#define RBF_MULTICAST (1<<30)
78#define RBF_UNICAST (1<<29)
79#define RBF_EXTERNAL (1<<28)
Loïc Minier6052cba2011-02-03 22:04:26 +010080#define RBF_UNKNOWN (1<<27)
Jens Scharsigc041e9d2010-01-23 12:03:45 +010081#define RBF_SIZE 0x07ff
82#define RBF_LOCAL4 (1<<26)
83#define RBF_LOCAL3 (1<<25)
84#define RBF_LOCAL2 (1<<24)
85#define RBF_LOCAL1 (1<<23)
86
87#define RBF_FRAMEMAX CONFIG_SYS_RX_ETH_BUFFER
88#define RBF_FRAMELEN 0x600
89
90typedef struct {
91 unsigned long addr, size;
92} rbf_t;
93
94typedef struct {
95 rbf_t rbfdt[RBF_FRAMEMAX];
96 unsigned long rbindex;
97} emac_device;
98
99void at91emac_EnableMDIO(at91_emac_t *at91mac)
100{
101 /* Mac CTRL reg set for MDIO enable */
102 writel(readl(&at91mac->ctl) | AT91_EMAC_CTL_MPE, &at91mac->ctl);
103}
104
105void at91emac_DisableMDIO(at91_emac_t *at91mac)
106{
107 /* Mac CTRL reg set for MDIO disable */
108 writel(readl(&at91mac->ctl) & ~AT91_EMAC_CTL_MPE, &at91mac->ctl);
109}
110
111int at91emac_read(at91_emac_t *at91mac, unsigned char addr,
112 unsigned char reg, unsigned short *value)
113{
Andreas Bießmann38bda012010-09-07 19:10:34 +0200114 unsigned long netstat;
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100115 at91emac_EnableMDIO(at91mac);
116
117 writel(AT91_EMAC_MAN_HIGH | AT91_EMAC_MAN_RW_R |
118 AT91_EMAC_MAN_REGA(reg) | AT91_EMAC_MAN_CODE_802_3 |
119 AT91_EMAC_MAN_PHYA(addr),
120 &at91mac->man);
Andreas Bießmann38bda012010-09-07 19:10:34 +0200121
122 do {
123 netstat = readl(&at91mac->sr);
Wolfgang Denkf4962062011-12-09 12:14:21 +0100124 debug_cond(DEBUG_AT91PHY, "poll SR %08lx\n", netstat);
Andreas Bießmann38bda012010-09-07 19:10:34 +0200125 } while (!(netstat & AT91_EMAC_SR_IDLE));
126
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100127 *value = readl(&at91mac->man) & AT91_EMAC_MAN_DATA_MASK;
128
129 at91emac_DisableMDIO(at91mac);
130
Wolfgang Denkf4962062011-12-09 12:14:21 +0100131 debug_cond(DEBUG_AT91PHY,
132 "AT91PHY read %p REG(%d)=%x\n", at91mac, reg, *value);
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100133
134 return 0;
135}
136
137int at91emac_write(at91_emac_t *at91mac, unsigned char addr,
138 unsigned char reg, unsigned short value)
139{
Andreas Bießmann38bda012010-09-07 19:10:34 +0200140 unsigned long netstat;
Wolfgang Denkf4962062011-12-09 12:14:21 +0100141 debug_cond(DEBUG_AT91PHY,
142 "AT91PHY write %p REG(%d)=%p\n", at91mac, reg, &value);
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100143
144 at91emac_EnableMDIO(at91mac);
145
146 writel(AT91_EMAC_MAN_HIGH | AT91_EMAC_MAN_RW_W |
147 AT91_EMAC_MAN_REGA(reg) | AT91_EMAC_MAN_CODE_802_3 |
148 AT91_EMAC_MAN_PHYA(addr) | (value & AT91_EMAC_MAN_DATA_MASK),
149 &at91mac->man);
Andreas Bießmann38bda012010-09-07 19:10:34 +0200150
151 do {
152 netstat = readl(&at91mac->sr);
Wolfgang Denkf4962062011-12-09 12:14:21 +0100153 debug_cond(DEBUG_AT91PHY, "poll SR %08lx\n", netstat);
Andreas Bießmann38bda012010-09-07 19:10:34 +0200154 } while (!(netstat & AT91_EMAC_SR_IDLE));
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100155
156 at91emac_DisableMDIO(at91mac);
Andreas Bießmann38bda012010-09-07 19:10:34 +0200157
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100158 return 0;
159}
160
161#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
162
Ben Warrend7fb9bc2010-07-29 12:56:11 -0700163at91_emac_t *get_emacbase_by_name(const char *devname)
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100164{
165 struct eth_device *netdev;
166
167 netdev = eth_get_dev_by_name(devname);
168 return (at91_emac_t *) netdev->iobase;
169}
170
Mike Frysinger5700bb62010-07-27 18:35:08 -0400171int at91emac_mii_read(const char *devname, unsigned char addr,
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100172 unsigned char reg, unsigned short *value)
173{
174 at91_emac_t *emac;
175
176 emac = get_emacbase_by_name(devname);
177 at91emac_read(emac , addr, reg, value);
178 return 0;
179}
180
181
Mike Frysinger5700bb62010-07-27 18:35:08 -0400182int at91emac_mii_write(const char *devname, unsigned char addr,
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100183 unsigned char reg, unsigned short value)
184{
185 at91_emac_t *emac;
186
187 emac = get_emacbase_by_name(devname);
188 at91emac_write(emac, addr, reg, value);
189 return 0;
190}
191
192#endif
193
194static int at91emac_phy_reset(struct eth_device *netdev)
195{
196 int i;
197 u16 status, adv;
198 at91_emac_t *emac;
199
200 emac = (at91_emac_t *) netdev->iobase;
201
202 adv = ADVERTISE_CSMA | ADVERTISE_ALL;
Eric Bénard836cd452010-06-21 09:40:43 +0200203 at91emac_write(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
204 MII_ADVERTISE, adv);
Wolfgang Denkf4962062011-12-09 12:14:21 +0100205 debug_cond(VERBOSEP, "%s: Starting autonegotiation...\n", netdev->name);
Eric Bénard836cd452010-06-21 09:40:43 +0200206 at91emac_write(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR, MII_BMCR,
207 (BMCR_ANENABLE | BMCR_ANRESTART));
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100208
Andreas Bießmanne63ac4c2010-10-07 09:44:46 +0200209 for (i = 0; i < 30000; i++) {
Eric Bénard836cd452010-06-21 09:40:43 +0200210 at91emac_read(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
211 MII_BMSR, &status);
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100212 if (status & BMSR_ANEGCOMPLETE)
213 break;
214 udelay(100);
215 }
216
217 if (status & BMSR_ANEGCOMPLETE) {
Wolfgang Denkf4962062011-12-09 12:14:21 +0100218 debug_cond(VERBOSEP,
219 "%s: Autonegotiation complete\n", netdev->name);
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100220 } else {
221 printf("%s: Autonegotiation timed out (status=0x%04x)\n",
222 netdev->name, status);
Andreas Bießmann77179062010-10-07 09:44:47 +0200223 return -1;
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100224 }
225 return 0;
226}
227
228static int at91emac_phy_init(struct eth_device *netdev)
229{
230 u16 phy_id, status, adv, lpa;
231 int media, speed, duplex;
232 int i;
233 at91_emac_t *emac;
234
235 emac = (at91_emac_t *) netdev->iobase;
236
237 /* Check if the PHY is up to snuff... */
Eric Bénard836cd452010-06-21 09:40:43 +0200238 at91emac_read(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
239 MII_PHYSID1, &phy_id);
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100240 if (phy_id == 0xffff) {
241 printf("%s: No PHY present\n", netdev->name);
Andreas Bießmann77179062010-10-07 09:44:47 +0200242 return -1;
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100243 }
244
Eric Bénard836cd452010-06-21 09:40:43 +0200245 at91emac_read(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
246 MII_BMSR, &status);
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100247
248 if (!(status & BMSR_LSTATUS)) {
249 /* Try to re-negotiate if we don't have link already. */
250 if (at91emac_phy_reset(netdev))
Andreas Bießmann77179062010-10-07 09:44:47 +0200251 return -2;
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100252
253 for (i = 0; i < 100000 / 100; i++) {
Eric Bénard836cd452010-06-21 09:40:43 +0200254 at91emac_read(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
255 MII_BMSR, &status);
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100256 if (status & BMSR_LSTATUS)
257 break;
258 udelay(100);
259 }
260 }
261 if (!(status & BMSR_LSTATUS)) {
Wolfgang Denkf4962062011-12-09 12:14:21 +0100262 debug_cond(VERBOSEP, "%s: link down\n", netdev->name);
Andreas Bießmann77179062010-10-07 09:44:47 +0200263 return -3;
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100264 } else {
Eric Bénard836cd452010-06-21 09:40:43 +0200265 at91emac_read(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
266 MII_ADVERTISE, &adv);
267 at91emac_read(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
268 MII_LPA, &lpa);
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100269 media = mii_nway_result(lpa & adv);
270 speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
271 ? 1 : 0);
272 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
Wolfgang Denkf4962062011-12-09 12:14:21 +0100273 debug_cond(VERBOSEP, "%s: link up, %sMbps %s-duplex\n",
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100274 netdev->name,
275 speed ? "100" : "10",
276 duplex ? "full" : "half");
277 }
278 return 0;
279}
280
281int at91emac_UpdateLinkSpeed(at91_emac_t *emac)
282{
283 unsigned short stat1;
284
Eric Bénard836cd452010-06-21 09:40:43 +0200285 at91emac_read(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR, MII_BMSR, &stat1);
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100286
287 if (!(stat1 & BMSR_LSTATUS)) /* link status up? */
Andreas Bießmann77179062010-10-07 09:44:47 +0200288 return -1;
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100289
290 if (stat1 & BMSR_100FULL) {
291 /*set Emac for 100BaseTX and Full Duplex */
292 writel(readl(&emac->cfg) |
293 AT91_EMAC_CFG_SPD | AT91_EMAC_CFG_FD,
294 &emac->cfg);
295 return 0;
296 }
297
298 if (stat1 & BMSR_10FULL) {
299 /*set MII for 10BaseT and Full Duplex */
300 writel((readl(&emac->cfg) &
301 ~(AT91_EMAC_CFG_SPD | AT91_EMAC_CFG_FD)
302 ) | AT91_EMAC_CFG_FD,
303 &emac->cfg);
304 return 0;
305 }
306
307 if (stat1 & BMSR_100HALF) {
308 /*set MII for 100BaseTX and Half Duplex */
309 writel((readl(&emac->cfg) &
310 ~(AT91_EMAC_CFG_SPD | AT91_EMAC_CFG_FD)
311 ) | AT91_EMAC_CFG_SPD,
312 &emac->cfg);
313 return 0;
314 }
315
316 if (stat1 & BMSR_10HALF) {
317 /*set MII for 10BaseT and Half Duplex */
318 writel((readl(&emac->cfg) &
319 ~(AT91_EMAC_CFG_SPD | AT91_EMAC_CFG_FD)),
320 &emac->cfg);
321 return 0;
322 }
Andreas Bießmann77179062010-10-07 09:44:47 +0200323 return 0;
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100324}
325
326static int at91emac_init(struct eth_device *netdev, bd_t *bd)
327{
328 int i;
329 u32 value;
330 emac_device *dev;
331 at91_emac_t *emac;
Jens Scharsig80733992011-02-19 06:17:02 +0000332 at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIO;
333 at91_pmc_t *pmc = (at91_pmc_t *) ATMEL_BASE_PMC;
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100334
335 emac = (at91_emac_t *) netdev->iobase;
336 dev = (emac_device *) netdev->priv;
337
338 /* PIO Disable Register */
Jens Scharsig80733992011-02-19 06:17:02 +0000339 value = ATMEL_PMX_AA_EMDIO | ATMEL_PMX_AA_EMDC |
340 ATMEL_PMX_AA_ERXER | ATMEL_PMX_AA_ERX1 |
341 ATMEL_PMX_AA_ERX0 | ATMEL_PMX_AA_ECRS |
342 ATMEL_PMX_AA_ETX1 | ATMEL_PMX_AA_ETX0 |
343 ATMEL_PMX_AA_ETXEN | ATMEL_PMX_AA_EREFCK;
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100344
345 writel(value, &pio->pioa.pdr);
346 writel(value, &pio->pioa.asr);
347
348#ifdef CONFIG_RMII
Jens Scharsig80733992011-02-19 06:17:02 +0000349 value = ATMEL_PMX_BA_ERXCK;
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100350#else
Jens Scharsig80733992011-02-19 06:17:02 +0000351 value = ATMEL_PMX_BA_ERXCK | ATMEL_PMX_BA_ECOL |
352 ATMEL_PMX_BA_ERXDV | ATMEL_PMX_BA_ERX3 |
353 ATMEL_PMX_BA_ERX2 | ATMEL_PMX_BA_ETXER |
354 ATMEL_PMX_BA_ETX3 | ATMEL_PMX_BA_ETX2;
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100355#endif
356 writel(value, &pio->piob.pdr);
357 writel(value, &pio->piob.bsr);
358
Jens Scharsig80733992011-02-19 06:17:02 +0000359 writel(1 << ATMEL_ID_EMAC, &pmc->pcer);
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100360 writel(readl(&emac->ctl) | AT91_EMAC_CTL_CSR, &emac->ctl);
361
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100362 /* Init Ethernet buffers */
363 for (i = 0; i < RBF_FRAMEMAX; i++) {
364 dev->rbfdt[i].addr = (unsigned long) NetRxPackets[i];
365 dev->rbfdt[i].size = 0;
366 }
367 dev->rbfdt[RBF_FRAMEMAX - 1].addr |= RBF_WRAP;
368 dev->rbindex = 0;
369 writel((u32) &(dev->rbfdt[0]), &emac->rbqp);
370
371 writel(readl(&emac->rsr) &
372 ~(AT91_EMAC_RSR_OVR | AT91_EMAC_RSR_REC | AT91_EMAC_RSR_BNA),
373 &emac->rsr);
374
375 value = AT91_EMAC_CFG_CAF | AT91_EMAC_CFG_NBC |
376 HCLK_DIV;
377#ifdef CONFIG_RMII
Eric Bénard836cd452010-06-21 09:40:43 +0200378 value |= AT91_EMAC_CFG_RMII;
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100379#endif
380 writel(value, &emac->cfg);
381
382 writel(readl(&emac->ctl) | AT91_EMAC_CTL_TE | AT91_EMAC_CTL_RE,
383 &emac->ctl);
384
385 if (!at91emac_phy_init(netdev)) {
386 at91emac_UpdateLinkSpeed(emac);
387 return 0;
388 }
Andreas Bießmann77179062010-10-07 09:44:47 +0200389 return -1;
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100390}
391
392static void at91emac_halt(struct eth_device *netdev)
393{
394 at91_emac_t *emac;
395
396 emac = (at91_emac_t *) netdev->iobase;
397 writel(readl(&emac->ctl) & ~(AT91_EMAC_CTL_TE | AT91_EMAC_CTL_RE),
398 &emac->ctl);
Wolfgang Denkf4962062011-12-09 12:14:21 +0100399 debug_cond(DEBUG_AT91EMAC, "halt MAC\n");
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100400}
401
Joe Hershberger95775012012-05-21 14:45:19 +0000402static int at91emac_send(struct eth_device *netdev, void *packet, int length)
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100403{
404 at91_emac_t *emac;
405
406 emac = (at91_emac_t *) netdev->iobase;
407
408 while (!(readl(&emac->tsr) & AT91_EMAC_TSR_BNQ))
409 ;
410 writel((u32) packet, &emac->tar);
411 writel(AT91_EMAC_TCR_LEN(length), &emac->tcr);
412 while (AT91_EMAC_TCR_LEN(readl(&emac->tcr)))
413 ;
Wolfgang Denkf4962062011-12-09 12:14:21 +0100414 debug_cond(DEBUG_AT91EMAC, "Send %d\n", length);
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100415 writel(readl(&emac->tsr) | AT91_EMAC_TSR_COMP, &emac->tsr);
416 return 0;
417}
418
419static int at91emac_recv(struct eth_device *netdev)
420{
421 emac_device *dev;
422 at91_emac_t *emac;
423 rbf_t *rbfp;
424 int size;
425
426 emac = (at91_emac_t *) netdev->iobase;
427 dev = (emac_device *) netdev->priv;
428
429 rbfp = &dev->rbfdt[dev->rbindex];
430 while (rbfp->addr & RBF_OWNER) {
431 size = rbfp->size & RBF_SIZE;
432 NetReceive(NetRxPackets[dev->rbindex], size);
433
Wolfgang Denkf4962062011-12-09 12:14:21 +0100434 debug_cond(DEBUG_AT91EMAC, "Recv[%ld]: %d bytes @ %lx\n",
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100435 dev->rbindex, size, rbfp->addr);
436
437 rbfp->addr &= ~RBF_OWNER;
438 rbfp->size = 0;
439 if (dev->rbindex < (RBF_FRAMEMAX-1))
440 dev->rbindex++;
441 else
442 dev->rbindex = 0;
443
444 rbfp = &(dev->rbfdt[dev->rbindex]);
445 if (!(rbfp->addr & RBF_OWNER))
446 writel(readl(&emac->rsr) | AT91_EMAC_RSR_REC,
447 &emac->rsr);
448 }
449
450 if (readl(&emac->isr) & AT91_EMAC_IxR_RBNA) {
451 /* EMAC silicon bug 41.3.1 workaround 1 */
452 writel(readl(&emac->ctl) & ~AT91_EMAC_CTL_RE, &emac->ctl);
453 writel(readl(&emac->ctl) | AT91_EMAC_CTL_RE, &emac->ctl);
454 dev->rbindex = 0;
455 printf("%s: reset receiver (EMAC dead lock bug)\n",
456 netdev->name);
457 }
458 return 0;
459}
460
Eric Bénard409943a2010-06-21 09:41:16 +0200461static int at91emac_write_hwaddr(struct eth_device *netdev)
462{
Eric Bénard409943a2010-06-21 09:41:16 +0200463 at91_emac_t *emac;
Jens Scharsig80733992011-02-19 06:17:02 +0000464 at91_pmc_t *pmc = (at91_pmc_t *) ATMEL_BASE_PMC;
Eric Bénard409943a2010-06-21 09:41:16 +0200465 emac = (at91_emac_t *) netdev->iobase;
Eric Bénard409943a2010-06-21 09:41:16 +0200466
Jens Scharsig80733992011-02-19 06:17:02 +0000467 writel(1 << ATMEL_ID_EMAC, &pmc->pcer);
Wolfgang Denkf4962062011-12-09 12:14:21 +0100468 debug_cond(DEBUG_AT91EMAC,
469 "init MAC-ADDR %02x:%02x:%02x:%02x:%02x:%02x\n",
andreas.devel@googlemail.com2321bfe2011-06-09 00:22:54 +0000470 netdev->enetaddr[5], netdev->enetaddr[4], netdev->enetaddr[3],
471 netdev->enetaddr[2], netdev->enetaddr[1], netdev->enetaddr[0]);
472 writel( (netdev->enetaddr[0] | netdev->enetaddr[1] << 8 |
473 netdev->enetaddr[2] << 16 | netdev->enetaddr[3] << 24),
474 &emac->sa2l);
475 writel((netdev->enetaddr[4] | netdev->enetaddr[5] << 8), &emac->sa2h);
Wolfgang Denkf4962062011-12-09 12:14:21 +0100476 debug_cond(DEBUG_AT91EMAC, "init MAC-ADDR %x%x\n",
Eric Bénard409943a2010-06-21 09:41:16 +0200477 readl(&emac->sa2h), readl(&emac->sa2l));
478 return 0;
479}
480
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100481int at91emac_register(bd_t *bis, unsigned long iobase)
482{
483 emac_device *emac;
484 emac_device *emacfix;
485 struct eth_device *dev;
486
487 if (iobase == 0)
Jens Scharsig80733992011-02-19 06:17:02 +0000488 iobase = ATMEL_BASE_EMAC;
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100489 emac = malloc(sizeof(*emac)+512);
490 if (emac == NULL)
Andreas Bießmann77179062010-10-07 09:44:47 +0200491 return -1;
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100492 dev = malloc(sizeof(*dev));
493 if (dev == NULL) {
494 free(emac);
Andreas Bießmann77179062010-10-07 09:44:47 +0200495 return -1;
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100496 }
497 /* alignment as per Errata (64 bytes) is insufficient! */
498 emacfix = (emac_device *) (((unsigned long) emac + 0x1ff) & 0xFFFFFE00);
499 memset(emacfix, 0, sizeof(emac_device));
500
501 memset(dev, 0, sizeof(*dev));
Andreas Bießmann4b8d77b2010-09-07 19:10:33 +0200502 sprintf(dev->name, "emac");
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100503 dev->iobase = iobase;
504 dev->priv = emacfix;
505 dev->init = at91emac_init;
506 dev->halt = at91emac_halt;
507 dev->send = at91emac_send;
508 dev->recv = at91emac_recv;
Eric Bénard409943a2010-06-21 09:41:16 +0200509 dev->write_hwaddr = at91emac_write_hwaddr;
Jens Scharsigc041e9d2010-01-23 12:03:45 +0100510
511 eth_register(dev);
512
513#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
514 miiphy_register(dev->name, at91emac_mii_read, at91emac_mii_write);
515#endif
516 return 1;
517}