at91_emac.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  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. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  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.
  14. *
  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.
  19. *
  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
  24. */
  25. #include <common.h>
  26. #include <asm/io.h>
  27. #ifndef CONFIG_AT91_LEGACY
  28. #include <asm/arch/hardware.h>
  29. #include <asm/arch/at91_emac.h>
  30. #include <asm/arch/at91_pmc.h>
  31. #include <asm/arch/at91_pio.h>
  32. #else
  33. /* remove next 5 lines, if all RM9200 boards convert to at91 arch */
  34. #include <asm/arch-at91/at91rm9200.h>
  35. #include <asm/arch-at91/hardware.h>
  36. #include <asm/arch-at91/at91_emac.h>
  37. #include <asm/arch-at91/at91_pmc.h>
  38. #include <asm/arch-at91/at91_pio.h>
  39. #endif
  40. #include <net.h>
  41. #include <netdev.h>
  42. #include <malloc.h>
  43. #include <miiphy.h>
  44. #include <linux/mii.h>
  45. #undef MII_DEBUG
  46. #undef ET_DEBUG
  47. #if (CONFIG_SYS_RX_ETH_BUFFER > 1024)
  48. #error AT91 EMAC supports max 1024 RX buffers. \
  49. Please decrease the CONFIG_SYS_RX_ETH_BUFFER value
  50. #endif
  51. #ifndef CONFIG_DRIVER_AT91EMAC_PHYADDR
  52. #define CONFIG_DRIVER_AT91EMAC_PHYADDR 0
  53. #endif
  54. /* MDIO clock must not exceed 2.5 MHz, so enable MCK divider */
  55. #if (AT91C_MASTER_CLOCK > 80000000)
  56. #define HCLK_DIV AT91_EMAC_CFG_MCLK_64
  57. #elif (AT91C_MASTER_CLOCK > 40000000)
  58. #define HCLK_DIV AT91_EMAC_CFG_MCLK_32
  59. #elif (AT91C_MASTER_CLOCK > 20000000)
  60. #define HCLK_DIV AT91_EMAC_CFG_MCLK_16
  61. #else
  62. #define HCLK_DIV AT91_EMAC_CFG_MCLK_8
  63. #endif
  64. #ifdef ET_DEBUG
  65. #define DEBUG_AT91EMAC 1
  66. #else
  67. #define DEBUG_AT91EMAC 0
  68. #endif
  69. #ifdef MII_DEBUG
  70. #define DEBUG_AT91PHY 1
  71. #else
  72. #define DEBUG_AT91PHY 0
  73. #endif
  74. #ifndef CONFIG_DRIVER_AT91EMAC_QUIET
  75. #define VERBOSEP 1
  76. #else
  77. #define VERBOSEP 0
  78. #endif
  79. #define RBF_ADDR 0xfffffffc
  80. #define RBF_OWNER (1<<0)
  81. #define RBF_WRAP (1<<1)
  82. #define RBF_BROADCAST (1<<31)
  83. #define RBF_MULTICAST (1<<30)
  84. #define RBF_UNICAST (1<<29)
  85. #define RBF_EXTERNAL (1<<28)
  86. #define RBF_UNKNOWN (1<<27)
  87. #define RBF_SIZE 0x07ff
  88. #define RBF_LOCAL4 (1<<26)
  89. #define RBF_LOCAL3 (1<<25)
  90. #define RBF_LOCAL2 (1<<24)
  91. #define RBF_LOCAL1 (1<<23)
  92. #define RBF_FRAMEMAX CONFIG_SYS_RX_ETH_BUFFER
  93. #define RBF_FRAMELEN 0x600
  94. typedef struct {
  95. unsigned long addr, size;
  96. } rbf_t;
  97. typedef struct {
  98. rbf_t rbfdt[RBF_FRAMEMAX];
  99. unsigned long rbindex;
  100. } emac_device;
  101. void at91emac_EnableMDIO(at91_emac_t *at91mac)
  102. {
  103. /* Mac CTRL reg set for MDIO enable */
  104. writel(readl(&at91mac->ctl) | AT91_EMAC_CTL_MPE, &at91mac->ctl);
  105. }
  106. void at91emac_DisableMDIO(at91_emac_t *at91mac)
  107. {
  108. /* Mac CTRL reg set for MDIO disable */
  109. writel(readl(&at91mac->ctl) & ~AT91_EMAC_CTL_MPE, &at91mac->ctl);
  110. }
  111. int at91emac_read(at91_emac_t *at91mac, unsigned char addr,
  112. unsigned char reg, unsigned short *value)
  113. {
  114. unsigned long netstat;
  115. at91emac_EnableMDIO(at91mac);
  116. writel(AT91_EMAC_MAN_HIGH | AT91_EMAC_MAN_RW_R |
  117. AT91_EMAC_MAN_REGA(reg) | AT91_EMAC_MAN_CODE_802_3 |
  118. AT91_EMAC_MAN_PHYA(addr),
  119. &at91mac->man);
  120. do {
  121. netstat = readl(&at91mac->sr);
  122. debug_cond(DEBUG_AT91PHY, "poll SR %08lx\n", netstat);
  123. } while (!(netstat & AT91_EMAC_SR_IDLE));
  124. *value = readl(&at91mac->man) & AT91_EMAC_MAN_DATA_MASK;
  125. at91emac_DisableMDIO(at91mac);
  126. debug_cond(DEBUG_AT91PHY,
  127. "AT91PHY read %p REG(%d)=%x\n", at91mac, reg, *value);
  128. return 0;
  129. }
  130. int at91emac_write(at91_emac_t *at91mac, unsigned char addr,
  131. unsigned char reg, unsigned short value)
  132. {
  133. unsigned long netstat;
  134. debug_cond(DEBUG_AT91PHY,
  135. "AT91PHY write %p REG(%d)=%p\n", at91mac, reg, &value);
  136. at91emac_EnableMDIO(at91mac);
  137. writel(AT91_EMAC_MAN_HIGH | AT91_EMAC_MAN_RW_W |
  138. AT91_EMAC_MAN_REGA(reg) | AT91_EMAC_MAN_CODE_802_3 |
  139. AT91_EMAC_MAN_PHYA(addr) | (value & AT91_EMAC_MAN_DATA_MASK),
  140. &at91mac->man);
  141. do {
  142. netstat = readl(&at91mac->sr);
  143. debug_cond(DEBUG_AT91PHY, "poll SR %08lx\n", netstat);
  144. } while (!(netstat & AT91_EMAC_SR_IDLE));
  145. at91emac_DisableMDIO(at91mac);
  146. return 0;
  147. }
  148. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  149. at91_emac_t *get_emacbase_by_name(const char *devname)
  150. {
  151. struct eth_device *netdev;
  152. netdev = eth_get_dev_by_name(devname);
  153. return (at91_emac_t *) netdev->iobase;
  154. }
  155. int at91emac_mii_read(const char *devname, unsigned char addr,
  156. unsigned char reg, unsigned short *value)
  157. {
  158. at91_emac_t *emac;
  159. emac = get_emacbase_by_name(devname);
  160. at91emac_read(emac , addr, reg, value);
  161. return 0;
  162. }
  163. int at91emac_mii_write(const char *devname, unsigned char addr,
  164. unsigned char reg, unsigned short value)
  165. {
  166. at91_emac_t *emac;
  167. emac = get_emacbase_by_name(devname);
  168. at91emac_write(emac, addr, reg, value);
  169. return 0;
  170. }
  171. #endif
  172. static int at91emac_phy_reset(struct eth_device *netdev)
  173. {
  174. int i;
  175. u16 status, adv;
  176. at91_emac_t *emac;
  177. emac = (at91_emac_t *) netdev->iobase;
  178. adv = ADVERTISE_CSMA | ADVERTISE_ALL;
  179. at91emac_write(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
  180. MII_ADVERTISE, adv);
  181. debug_cond(VERBOSEP, "%s: Starting autonegotiation...\n", netdev->name);
  182. at91emac_write(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR, MII_BMCR,
  183. (BMCR_ANENABLE | BMCR_ANRESTART));
  184. for (i = 0; i < 30000; i++) {
  185. at91emac_read(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
  186. MII_BMSR, &status);
  187. if (status & BMSR_ANEGCOMPLETE)
  188. break;
  189. udelay(100);
  190. }
  191. if (status & BMSR_ANEGCOMPLETE) {
  192. debug_cond(VERBOSEP,
  193. "%s: Autonegotiation complete\n", netdev->name);
  194. } else {
  195. printf("%s: Autonegotiation timed out (status=0x%04x)\n",
  196. netdev->name, status);
  197. return -1;
  198. }
  199. return 0;
  200. }
  201. static int at91emac_phy_init(struct eth_device *netdev)
  202. {
  203. u16 phy_id, status, adv, lpa;
  204. int media, speed, duplex;
  205. int i;
  206. at91_emac_t *emac;
  207. emac = (at91_emac_t *) netdev->iobase;
  208. /* Check if the PHY is up to snuff... */
  209. at91emac_read(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
  210. MII_PHYSID1, &phy_id);
  211. if (phy_id == 0xffff) {
  212. printf("%s: No PHY present\n", netdev->name);
  213. return -1;
  214. }
  215. at91emac_read(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
  216. MII_BMSR, &status);
  217. if (!(status & BMSR_LSTATUS)) {
  218. /* Try to re-negotiate if we don't have link already. */
  219. if (at91emac_phy_reset(netdev))
  220. return -2;
  221. for (i = 0; i < 100000 / 100; i++) {
  222. at91emac_read(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
  223. MII_BMSR, &status);
  224. if (status & BMSR_LSTATUS)
  225. break;
  226. udelay(100);
  227. }
  228. }
  229. if (!(status & BMSR_LSTATUS)) {
  230. debug_cond(VERBOSEP, "%s: link down\n", netdev->name);
  231. return -3;
  232. } else {
  233. at91emac_read(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
  234. MII_ADVERTISE, &adv);
  235. at91emac_read(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
  236. MII_LPA, &lpa);
  237. media = mii_nway_result(lpa & adv);
  238. speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
  239. ? 1 : 0);
  240. duplex = (media & ADVERTISE_FULL) ? 1 : 0;
  241. debug_cond(VERBOSEP, "%s: link up, %sMbps %s-duplex\n",
  242. netdev->name,
  243. speed ? "100" : "10",
  244. duplex ? "full" : "half");
  245. }
  246. return 0;
  247. }
  248. int at91emac_UpdateLinkSpeed(at91_emac_t *emac)
  249. {
  250. unsigned short stat1;
  251. at91emac_read(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR, MII_BMSR, &stat1);
  252. if (!(stat1 & BMSR_LSTATUS)) /* link status up? */
  253. return -1;
  254. if (stat1 & BMSR_100FULL) {
  255. /*set Emac for 100BaseTX and Full Duplex */
  256. writel(readl(&emac->cfg) |
  257. AT91_EMAC_CFG_SPD | AT91_EMAC_CFG_FD,
  258. &emac->cfg);
  259. return 0;
  260. }
  261. if (stat1 & BMSR_10FULL) {
  262. /*set MII for 10BaseT and Full Duplex */
  263. writel((readl(&emac->cfg) &
  264. ~(AT91_EMAC_CFG_SPD | AT91_EMAC_CFG_FD)
  265. ) | AT91_EMAC_CFG_FD,
  266. &emac->cfg);
  267. return 0;
  268. }
  269. if (stat1 & BMSR_100HALF) {
  270. /*set MII for 100BaseTX and Half Duplex */
  271. writel((readl(&emac->cfg) &
  272. ~(AT91_EMAC_CFG_SPD | AT91_EMAC_CFG_FD)
  273. ) | AT91_EMAC_CFG_SPD,
  274. &emac->cfg);
  275. return 0;
  276. }
  277. if (stat1 & BMSR_10HALF) {
  278. /*set MII for 10BaseT and Half Duplex */
  279. writel((readl(&emac->cfg) &
  280. ~(AT91_EMAC_CFG_SPD | AT91_EMAC_CFG_FD)),
  281. &emac->cfg);
  282. return 0;
  283. }
  284. return 0;
  285. }
  286. static int at91emac_init(struct eth_device *netdev, bd_t *bd)
  287. {
  288. int i;
  289. u32 value;
  290. emac_device *dev;
  291. at91_emac_t *emac;
  292. at91_pio_t *pio = (at91_pio_t *) ATMEL_BASE_PIO;
  293. at91_pmc_t *pmc = (at91_pmc_t *) ATMEL_BASE_PMC;
  294. emac = (at91_emac_t *) netdev->iobase;
  295. dev = (emac_device *) netdev->priv;
  296. /* PIO Disable Register */
  297. value = ATMEL_PMX_AA_EMDIO | ATMEL_PMX_AA_EMDC |
  298. ATMEL_PMX_AA_ERXER | ATMEL_PMX_AA_ERX1 |
  299. ATMEL_PMX_AA_ERX0 | ATMEL_PMX_AA_ECRS |
  300. ATMEL_PMX_AA_ETX1 | ATMEL_PMX_AA_ETX0 |
  301. ATMEL_PMX_AA_ETXEN | ATMEL_PMX_AA_EREFCK;
  302. writel(value, &pio->pioa.pdr);
  303. writel(value, &pio->pioa.asr);
  304. #ifdef CONFIG_RMII
  305. value = ATMEL_PMX_BA_ERXCK;
  306. #else
  307. value = ATMEL_PMX_BA_ERXCK | ATMEL_PMX_BA_ECOL |
  308. ATMEL_PMX_BA_ERXDV | ATMEL_PMX_BA_ERX3 |
  309. ATMEL_PMX_BA_ERX2 | ATMEL_PMX_BA_ETXER |
  310. ATMEL_PMX_BA_ETX3 | ATMEL_PMX_BA_ETX2;
  311. #endif
  312. writel(value, &pio->piob.pdr);
  313. writel(value, &pio->piob.bsr);
  314. writel(1 << ATMEL_ID_EMAC, &pmc->pcer);
  315. writel(readl(&emac->ctl) | AT91_EMAC_CTL_CSR, &emac->ctl);
  316. /* Init Ethernet buffers */
  317. for (i = 0; i < RBF_FRAMEMAX; i++) {
  318. dev->rbfdt[i].addr = (unsigned long) NetRxPackets[i];
  319. dev->rbfdt[i].size = 0;
  320. }
  321. dev->rbfdt[RBF_FRAMEMAX - 1].addr |= RBF_WRAP;
  322. dev->rbindex = 0;
  323. writel((u32) &(dev->rbfdt[0]), &emac->rbqp);
  324. writel(readl(&emac->rsr) &
  325. ~(AT91_EMAC_RSR_OVR | AT91_EMAC_RSR_REC | AT91_EMAC_RSR_BNA),
  326. &emac->rsr);
  327. value = AT91_EMAC_CFG_CAF | AT91_EMAC_CFG_NBC |
  328. HCLK_DIV;
  329. #ifdef CONFIG_RMII
  330. value |= AT91_EMAC_CFG_RMII;
  331. #endif
  332. writel(value, &emac->cfg);
  333. writel(readl(&emac->ctl) | AT91_EMAC_CTL_TE | AT91_EMAC_CTL_RE,
  334. &emac->ctl);
  335. if (!at91emac_phy_init(netdev)) {
  336. at91emac_UpdateLinkSpeed(emac);
  337. return 0;
  338. }
  339. return -1;
  340. }
  341. static void at91emac_halt(struct eth_device *netdev)
  342. {
  343. at91_emac_t *emac;
  344. emac = (at91_emac_t *) netdev->iobase;
  345. writel(readl(&emac->ctl) & ~(AT91_EMAC_CTL_TE | AT91_EMAC_CTL_RE),
  346. &emac->ctl);
  347. debug_cond(DEBUG_AT91EMAC, "halt MAC\n");
  348. }
  349. static int at91emac_send(struct eth_device *netdev, void *packet, int length)
  350. {
  351. at91_emac_t *emac;
  352. emac = (at91_emac_t *) netdev->iobase;
  353. while (!(readl(&emac->tsr) & AT91_EMAC_TSR_BNQ))
  354. ;
  355. writel((u32) packet, &emac->tar);
  356. writel(AT91_EMAC_TCR_LEN(length), &emac->tcr);
  357. while (AT91_EMAC_TCR_LEN(readl(&emac->tcr)))
  358. ;
  359. debug_cond(DEBUG_AT91EMAC, "Send %d\n", length);
  360. writel(readl(&emac->tsr) | AT91_EMAC_TSR_COMP, &emac->tsr);
  361. return 0;
  362. }
  363. static int at91emac_recv(struct eth_device *netdev)
  364. {
  365. emac_device *dev;
  366. at91_emac_t *emac;
  367. rbf_t *rbfp;
  368. int size;
  369. emac = (at91_emac_t *) netdev->iobase;
  370. dev = (emac_device *) netdev->priv;
  371. rbfp = &dev->rbfdt[dev->rbindex];
  372. while (rbfp->addr & RBF_OWNER) {
  373. size = rbfp->size & RBF_SIZE;
  374. NetReceive(NetRxPackets[dev->rbindex], size);
  375. debug_cond(DEBUG_AT91EMAC, "Recv[%ld]: %d bytes @ %lx\n",
  376. dev->rbindex, size, rbfp->addr);
  377. rbfp->addr &= ~RBF_OWNER;
  378. rbfp->size = 0;
  379. if (dev->rbindex < (RBF_FRAMEMAX-1))
  380. dev->rbindex++;
  381. else
  382. dev->rbindex = 0;
  383. rbfp = &(dev->rbfdt[dev->rbindex]);
  384. if (!(rbfp->addr & RBF_OWNER))
  385. writel(readl(&emac->rsr) | AT91_EMAC_RSR_REC,
  386. &emac->rsr);
  387. }
  388. if (readl(&emac->isr) & AT91_EMAC_IxR_RBNA) {
  389. /* EMAC silicon bug 41.3.1 workaround 1 */
  390. writel(readl(&emac->ctl) & ~AT91_EMAC_CTL_RE, &emac->ctl);
  391. writel(readl(&emac->ctl) | AT91_EMAC_CTL_RE, &emac->ctl);
  392. dev->rbindex = 0;
  393. printf("%s: reset receiver (EMAC dead lock bug)\n",
  394. netdev->name);
  395. }
  396. return 0;
  397. }
  398. static int at91emac_write_hwaddr(struct eth_device *netdev)
  399. {
  400. at91_emac_t *emac;
  401. at91_pmc_t *pmc = (at91_pmc_t *) ATMEL_BASE_PMC;
  402. emac = (at91_emac_t *) netdev->iobase;
  403. writel(1 << ATMEL_ID_EMAC, &pmc->pcer);
  404. debug_cond(DEBUG_AT91EMAC,
  405. "init MAC-ADDR %02x:%02x:%02x:%02x:%02x:%02x\n",
  406. netdev->enetaddr[5], netdev->enetaddr[4], netdev->enetaddr[3],
  407. netdev->enetaddr[2], netdev->enetaddr[1], netdev->enetaddr[0]);
  408. writel( (netdev->enetaddr[0] | netdev->enetaddr[1] << 8 |
  409. netdev->enetaddr[2] << 16 | netdev->enetaddr[3] << 24),
  410. &emac->sa2l);
  411. writel((netdev->enetaddr[4] | netdev->enetaddr[5] << 8), &emac->sa2h);
  412. debug_cond(DEBUG_AT91EMAC, "init MAC-ADDR %x%x\n",
  413. readl(&emac->sa2h), readl(&emac->sa2l));
  414. return 0;
  415. }
  416. int at91emac_register(bd_t *bis, unsigned long iobase)
  417. {
  418. emac_device *emac;
  419. emac_device *emacfix;
  420. struct eth_device *dev;
  421. if (iobase == 0)
  422. iobase = ATMEL_BASE_EMAC;
  423. emac = malloc(sizeof(*emac)+512);
  424. if (emac == NULL)
  425. return -1;
  426. dev = malloc(sizeof(*dev));
  427. if (dev == NULL) {
  428. free(emac);
  429. return -1;
  430. }
  431. /* alignment as per Errata (64 bytes) is insufficient! */
  432. emacfix = (emac_device *) (((unsigned long) emac + 0x1ff) & 0xFFFFFE00);
  433. memset(emacfix, 0, sizeof(emac_device));
  434. memset(dev, 0, sizeof(*dev));
  435. sprintf(dev->name, "emac");
  436. dev->iobase = iobase;
  437. dev->priv = emacfix;
  438. dev->init = at91emac_init;
  439. dev->halt = at91emac_halt;
  440. dev->send = at91emac_send;
  441. dev->recv = at91emac_recv;
  442. dev->write_hwaddr = at91emac_write_hwaddr;
  443. eth_register(dev);
  444. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  445. miiphy_register(dev->name, at91emac_mii_read, at91emac_mii_write);
  446. #endif
  447. return 1;
  448. }