2 * Copyright (C) 2009 BuS Elektronik GmbH & Co. KG
3 * Jens Scharsig (esw@bus-elektronik.de)
6 * Author : Hamid Ikdoumi (Atmel)
8 * SPDX-License-Identifier: GPL-2.0+
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>
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>
30 #include <linux/mii.h>
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
40 #ifndef CONFIG_DRIVER_AT91EMAC_PHYADDR
41 #define CONFIG_DRIVER_AT91EMAC_PHYADDR 0
44 /* 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
52 #define HCLK_DIV AT91_EMAC_CFG_MCLK_8
56 #define DEBUG_AT91EMAC 1
58 #define DEBUG_AT91EMAC 0
62 #define DEBUG_AT91PHY 1
64 #define DEBUG_AT91PHY 0
67 #ifndef CONFIG_DRIVER_AT91EMAC_QUIET
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)
80 #define RBF_UNKNOWN (1<<27)
81 #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)
87 #define RBF_FRAMEMAX CONFIG_SYS_RX_ETH_BUFFER
88 #define RBF_FRAMELEN 0x600
91 unsigned long addr, size;
95 rbf_t rbfdt[RBF_FRAMEMAX];
96 unsigned long rbindex;
99 void at91emac_EnableMDIO(at91_emac_t *at91mac)
101 /* Mac CTRL reg set for MDIO enable */
102 writel(readl(&at91mac->ctl) | AT91_EMAC_CTL_MPE, &at91mac->ctl);
105 void at91emac_DisableMDIO(at91_emac_t *at91mac)
107 /* Mac CTRL reg set for MDIO disable */
108 writel(readl(&at91mac->ctl) & ~AT91_EMAC_CTL_MPE, &at91mac->ctl);
111 int at91emac_read(at91_emac_t *at91mac, unsigned char addr,
112 unsigned char reg, unsigned short *value)
114 unsigned long netstat;
115 at91emac_EnableMDIO(at91mac);
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),
123 netstat = readl(&at91mac->sr);
124 debug_cond(DEBUG_AT91PHY, "poll SR %08lx\n", netstat);
125 } while (!(netstat & AT91_EMAC_SR_IDLE));
127 *value = readl(&at91mac->man) & AT91_EMAC_MAN_DATA_MASK;
129 at91emac_DisableMDIO(at91mac);
131 debug_cond(DEBUG_AT91PHY,
132 "AT91PHY read %p REG(%d)=%x\n", at91mac, reg, *value);
137 int at91emac_write(at91_emac_t *at91mac, unsigned char addr,
138 unsigned char reg, unsigned short value)
140 unsigned long netstat;
141 debug_cond(DEBUG_AT91PHY,
142 "AT91PHY write %p REG(%d)=%p\n", at91mac, reg, &value);
144 at91emac_EnableMDIO(at91mac);
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),
152 netstat = readl(&at91mac->sr);
153 debug_cond(DEBUG_AT91PHY, "poll SR %08lx\n", netstat);
154 } while (!(netstat & AT91_EMAC_SR_IDLE));
156 at91emac_DisableMDIO(at91mac);
161 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
163 at91_emac_t *get_emacbase_by_name(const char *devname)
165 struct eth_device *netdev;
167 netdev = eth_get_dev_by_name(devname);
168 return (at91_emac_t *) netdev->iobase;
171 int at91emac_mii_read(const char *devname, unsigned char addr,
172 unsigned char reg, unsigned short *value)
176 emac = get_emacbase_by_name(devname);
177 at91emac_read(emac , addr, reg, value);
182 int at91emac_mii_write(const char *devname, unsigned char addr,
183 unsigned char reg, unsigned short value)
187 emac = get_emacbase_by_name(devname);
188 at91emac_write(emac, addr, reg, value);
194 static int at91emac_phy_reset(struct eth_device *netdev)
200 emac = (at91_emac_t *) netdev->iobase;
202 adv = ADVERTISE_CSMA | ADVERTISE_ALL;
203 at91emac_write(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
205 debug_cond(VERBOSEP, "%s: Starting autonegotiation...\n", netdev->name);
206 at91emac_write(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR, MII_BMCR,
207 (BMCR_ANENABLE | BMCR_ANRESTART));
209 for (i = 0; i < 30000; i++) {
210 at91emac_read(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
212 if (status & BMSR_ANEGCOMPLETE)
217 if (status & BMSR_ANEGCOMPLETE) {
219 "%s: Autonegotiation complete\n", netdev->name);
221 printf("%s: Autonegotiation timed out (status=0x%04x)\n",
222 netdev->name, status);
228 static int at91emac_phy_init(struct eth_device *netdev)
230 u16 phy_id, status, adv, lpa;
231 int media, speed, duplex;
235 emac = (at91_emac_t *) netdev->iobase;
237 /* Check if the PHY is up to snuff... */
238 at91emac_read(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
239 MII_PHYSID1, &phy_id);
240 if (phy_id == 0xffff) {
241 printf("%s: No PHY present\n", netdev->name);
245 at91emac_read(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
248 if (!(status & BMSR_LSTATUS)) {
249 /* Try to re-negotiate if we don't have link already. */
250 if (at91emac_phy_reset(netdev))
253 for (i = 0; i < 100000 / 100; i++) {
254 at91emac_read(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
256 if (status & BMSR_LSTATUS)
261 if (!(status & BMSR_LSTATUS)) {
262 debug_cond(VERBOSEP, "%s: link down\n", netdev->name);
265 at91emac_read(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
266 MII_ADVERTISE, &adv);
267 at91emac_read(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
269 media = mii_nway_result(lpa & adv);
270 speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
272 duplex = (media & ADVERTISE_FULL) ? 1 : 0;
273 debug_cond(VERBOSEP, "%s: link up, %sMbps %s-duplex\n",
275 speed ? "100" : "10",
276 duplex ? "full" : "half");
281 int at91emac_UpdateLinkSpeed(at91_emac_t *emac)
283 unsigned short stat1;
285 at91emac_read(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR, MII_BMSR, &stat1);
287 if (!(stat1 & BMSR_LSTATUS)) /* link status up? */
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,
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,
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,
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)),
326 static int at91emac_init(struct eth_device *netdev, bd_t *bd)
332 at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIO;
333 at91_pmc_t *pmc = (at91_pmc_t *) ATMEL_BASE_PMC;
335 emac = (at91_emac_t *) netdev->iobase;
336 dev = (emac_device *) netdev->priv;
338 /* PIO Disable Register */
339 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;
345 writel(value, &pio->pioa.pdr);
346 writel(value, &pio->pioa.asr);
349 value = ATMEL_PMX_BA_ERXCK;
351 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;
356 writel(value, &pio->piob.pdr);
357 writel(value, &pio->piob.bsr);
359 writel(1 << ATMEL_ID_EMAC, &pmc->pcer);
360 writel(readl(&emac->ctl) | AT91_EMAC_CTL_CSR, &emac->ctl);
362 /* 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;
367 dev->rbfdt[RBF_FRAMEMAX - 1].addr |= RBF_WRAP;
369 writel((u32) &(dev->rbfdt[0]), &emac->rbqp);
371 writel(readl(&emac->rsr) &
372 ~(AT91_EMAC_RSR_OVR | AT91_EMAC_RSR_REC | AT91_EMAC_RSR_BNA),
375 value = AT91_EMAC_CFG_CAF | AT91_EMAC_CFG_NBC |
378 value |= AT91_EMAC_CFG_RMII;
380 writel(value, &emac->cfg);
382 writel(readl(&emac->ctl) | AT91_EMAC_CTL_TE | AT91_EMAC_CTL_RE,
385 if (!at91emac_phy_init(netdev)) {
386 at91emac_UpdateLinkSpeed(emac);
392 static void at91emac_halt(struct eth_device *netdev)
396 emac = (at91_emac_t *) netdev->iobase;
397 writel(readl(&emac->ctl) & ~(AT91_EMAC_CTL_TE | AT91_EMAC_CTL_RE),
399 debug_cond(DEBUG_AT91EMAC, "halt MAC\n");
402 static int at91emac_send(struct eth_device *netdev, void *packet, int length)
406 emac = (at91_emac_t *) netdev->iobase;
408 while (!(readl(&emac->tsr) & AT91_EMAC_TSR_BNQ))
410 writel((u32) packet, &emac->tar);
411 writel(AT91_EMAC_TCR_LEN(length), &emac->tcr);
412 while (AT91_EMAC_TCR_LEN(readl(&emac->tcr)))
414 debug_cond(DEBUG_AT91EMAC, "Send %d\n", length);
415 writel(readl(&emac->tsr) | AT91_EMAC_TSR_COMP, &emac->tsr);
419 static int at91emac_recv(struct eth_device *netdev)
426 emac = (at91_emac_t *) netdev->iobase;
427 dev = (emac_device *) netdev->priv;
429 rbfp = &dev->rbfdt[dev->rbindex];
430 while (rbfp->addr & RBF_OWNER) {
431 size = rbfp->size & RBF_SIZE;
432 NetReceive(NetRxPackets[dev->rbindex], size);
434 debug_cond(DEBUG_AT91EMAC, "Recv[%ld]: %d bytes @ %lx\n",
435 dev->rbindex, size, rbfp->addr);
437 rbfp->addr &= ~RBF_OWNER;
439 if (dev->rbindex < (RBF_FRAMEMAX-1))
444 rbfp = &(dev->rbfdt[dev->rbindex]);
445 if (!(rbfp->addr & RBF_OWNER))
446 writel(readl(&emac->rsr) | AT91_EMAC_RSR_REC,
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);
455 printf("%s: reset receiver (EMAC dead lock bug)\n",
461 static int at91emac_write_hwaddr(struct eth_device *netdev)
464 at91_pmc_t *pmc = (at91_pmc_t *) ATMEL_BASE_PMC;
465 emac = (at91_emac_t *) netdev->iobase;
467 writel(1 << ATMEL_ID_EMAC, &pmc->pcer);
468 debug_cond(DEBUG_AT91EMAC,
469 "init MAC-ADDR %02x:%02x:%02x:%02x:%02x:%02x\n",
470 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),
475 writel((netdev->enetaddr[4] | netdev->enetaddr[5] << 8), &emac->sa2h);
476 debug_cond(DEBUG_AT91EMAC, "init MAC-ADDR %x%x\n",
477 readl(&emac->sa2h), readl(&emac->sa2l));
481 int at91emac_register(bd_t *bis, unsigned long iobase)
484 emac_device *emacfix;
485 struct eth_device *dev;
488 iobase = ATMEL_BASE_EMAC;
489 emac = malloc(sizeof(*emac)+512);
492 dev = malloc(sizeof(*dev));
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));
501 memset(dev, 0, sizeof(*dev));
502 sprintf(dev->name, "emac");
503 dev->iobase = iobase;
505 dev->init = at91emac_init;
506 dev->halt = at91emac_halt;
507 dev->send = at91emac_send;
508 dev->recv = at91emac_recv;
509 dev->write_hwaddr = at91emac_write_hwaddr;
513 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
514 miiphy_register(dev->name, at91emac_mii_read, at91emac_mii_write);