at91_emac.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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(...) printf(__VA_ARGS__);
  66. #else
  67. #define DEBUG_AT91EMAC(...)
  68. #endif
  69. #ifdef MII_DEBUG
  70. #define DEBUG_AT91PHY(...) printf(__VA_ARGS__);
  71. #else
  72. #define DEBUG_AT91PHY(...)
  73. #endif
  74. #ifndef CONFIG_DRIVER_AT91EMAC_QUIET
  75. #define VERBOSEP(...) printf(__VA_ARGS__);
  76. #else
  77. #define VERBOSEP(...)
  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_UNKOWN (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. at91emac_EnableMDIO(at91mac);
  115. writel(AT91_EMAC_MAN_HIGH | AT91_EMAC_MAN_RW_R |
  116. AT91_EMAC_MAN_REGA(reg) | AT91_EMAC_MAN_CODE_802_3 |
  117. AT91_EMAC_MAN_PHYA(addr),
  118. &at91mac->man);
  119. udelay(10000);
  120. *value = readl(&at91mac->man) & AT91_EMAC_MAN_DATA_MASK;
  121. at91emac_DisableMDIO(at91mac);
  122. DEBUG_AT91PHY("AT91PHY read %x REG(%d)=%x\n", at91mac, reg, *value)
  123. return 0;
  124. }
  125. int at91emac_write(at91_emac_t *at91mac, unsigned char addr,
  126. unsigned char reg, unsigned short value)
  127. {
  128. DEBUG_AT91PHY("AT91PHY write %x REG(%d)=%x\n", at91mac, reg, &value)
  129. at91emac_EnableMDIO(at91mac);
  130. writel(AT91_EMAC_MAN_HIGH | AT91_EMAC_MAN_RW_W |
  131. AT91_EMAC_MAN_REGA(reg) | AT91_EMAC_MAN_CODE_802_3 |
  132. AT91_EMAC_MAN_PHYA(addr) | (value & AT91_EMAC_MAN_DATA_MASK),
  133. &at91mac->man);
  134. udelay(10000);
  135. at91emac_DisableMDIO(at91mac);
  136. return 0;
  137. }
  138. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  139. at91_emac_t *get_emacbase_by_name(const char *devname)
  140. {
  141. struct eth_device *netdev;
  142. netdev = eth_get_dev_by_name(devname);
  143. return (at91_emac_t *) netdev->iobase;
  144. }
  145. int at91emac_mii_read(const char *devname, unsigned char addr,
  146. unsigned char reg, unsigned short *value)
  147. {
  148. at91_emac_t *emac;
  149. emac = get_emacbase_by_name(devname);
  150. at91emac_read(emac , addr, reg, value);
  151. return 0;
  152. }
  153. int at91emac_mii_write(const char *devname, unsigned char addr,
  154. unsigned char reg, unsigned short value)
  155. {
  156. at91_emac_t *emac;
  157. emac = get_emacbase_by_name(devname);
  158. at91emac_write(emac, addr, reg, value);
  159. return 0;
  160. }
  161. #endif
  162. static int at91emac_phy_reset(struct eth_device *netdev)
  163. {
  164. int i;
  165. u16 status, adv;
  166. at91_emac_t *emac;
  167. emac = (at91_emac_t *) netdev->iobase;
  168. adv = ADVERTISE_CSMA | ADVERTISE_ALL;
  169. at91emac_write(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
  170. MII_ADVERTISE, adv);
  171. VERBOSEP("%s: Starting autonegotiation...\n", netdev->name);
  172. at91emac_write(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR, MII_BMCR,
  173. (BMCR_ANENABLE | BMCR_ANRESTART));
  174. for (i = 0; i < 100000 / 100; i++) {
  175. at91emac_read(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
  176. MII_BMSR, &status);
  177. if (status & BMSR_ANEGCOMPLETE)
  178. break;
  179. udelay(100);
  180. }
  181. if (status & BMSR_ANEGCOMPLETE) {
  182. VERBOSEP("%s: Autonegotiation complete\n", netdev->name);
  183. } else {
  184. printf("%s: Autonegotiation timed out (status=0x%04x)\n",
  185. netdev->name, status);
  186. return 1;
  187. }
  188. return 0;
  189. }
  190. static int at91emac_phy_init(struct eth_device *netdev)
  191. {
  192. u16 phy_id, status, adv, lpa;
  193. int media, speed, duplex;
  194. int i;
  195. at91_emac_t *emac;
  196. emac = (at91_emac_t *) netdev->iobase;
  197. /* Check if the PHY is up to snuff... */
  198. at91emac_read(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
  199. MII_PHYSID1, &phy_id);
  200. if (phy_id == 0xffff) {
  201. printf("%s: No PHY present\n", netdev->name);
  202. return 1;
  203. }
  204. at91emac_read(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
  205. MII_BMSR, &status);
  206. if (!(status & BMSR_LSTATUS)) {
  207. /* Try to re-negotiate if we don't have link already. */
  208. if (at91emac_phy_reset(netdev))
  209. return 2;
  210. for (i = 0; i < 100000 / 100; i++) {
  211. at91emac_read(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
  212. MII_BMSR, &status);
  213. if (status & BMSR_LSTATUS)
  214. break;
  215. udelay(100);
  216. }
  217. }
  218. if (!(status & BMSR_LSTATUS)) {
  219. VERBOSEP("%s: link down\n", netdev->name);
  220. return 3;
  221. } else {
  222. at91emac_read(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
  223. MII_ADVERTISE, &adv);
  224. at91emac_read(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR,
  225. MII_LPA, &lpa);
  226. media = mii_nway_result(lpa & adv);
  227. speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
  228. ? 1 : 0);
  229. duplex = (media & ADVERTISE_FULL) ? 1 : 0;
  230. VERBOSEP("%s: link up, %sMbps %s-duplex\n",
  231. netdev->name,
  232. speed ? "100" : "10",
  233. duplex ? "full" : "half");
  234. }
  235. return 0;
  236. }
  237. int at91emac_UpdateLinkSpeed(at91_emac_t *emac)
  238. {
  239. unsigned short stat1;
  240. at91emac_read(emac, CONFIG_DRIVER_AT91EMAC_PHYADDR, MII_BMSR, &stat1);
  241. if (!(stat1 & BMSR_LSTATUS)) /* link status up? */
  242. return 1;
  243. if (stat1 & BMSR_100FULL) {
  244. /*set Emac for 100BaseTX and Full Duplex */
  245. writel(readl(&emac->cfg) |
  246. AT91_EMAC_CFG_SPD | AT91_EMAC_CFG_FD,
  247. &emac->cfg);
  248. return 0;
  249. }
  250. if (stat1 & BMSR_10FULL) {
  251. /*set MII for 10BaseT and Full Duplex */
  252. writel((readl(&emac->cfg) &
  253. ~(AT91_EMAC_CFG_SPD | AT91_EMAC_CFG_FD)
  254. ) | AT91_EMAC_CFG_FD,
  255. &emac->cfg);
  256. return 0;
  257. }
  258. if (stat1 & BMSR_100HALF) {
  259. /*set MII for 100BaseTX and Half Duplex */
  260. writel((readl(&emac->cfg) &
  261. ~(AT91_EMAC_CFG_SPD | AT91_EMAC_CFG_FD)
  262. ) | AT91_EMAC_CFG_SPD,
  263. &emac->cfg);
  264. return 0;
  265. }
  266. if (stat1 & BMSR_10HALF) {
  267. /*set MII for 10BaseT and Half Duplex */
  268. writel((readl(&emac->cfg) &
  269. ~(AT91_EMAC_CFG_SPD | AT91_EMAC_CFG_FD)),
  270. &emac->cfg);
  271. return 0;
  272. }
  273. return 1;
  274. }
  275. static int at91emac_init(struct eth_device *netdev, bd_t *bd)
  276. {
  277. int i;
  278. u32 value;
  279. emac_device *dev;
  280. at91_emac_t *emac;
  281. at91_pio_t *pio = (at91_pio_t *) AT91_PIO_BASE;
  282. at91_pmc_t *pmc = (at91_pmc_t *) AT91_PMC_BASE;
  283. emac = (at91_emac_t *) netdev->iobase;
  284. dev = (emac_device *) netdev->priv;
  285. /* PIO Disable Register */
  286. value = AT91_PMX_AA_EMDIO | AT91_PMX_AA_EMDC |
  287. AT91_PMX_AA_ERXER | AT91_PMX_AA_ERX1 |
  288. AT91_PMX_AA_ERX0 | AT91_PMX_AA_ECRS |
  289. AT91_PMX_AA_ETX1 | AT91_PMX_AA_ETX0 |
  290. AT91_PMX_AA_ETXEN | AT91_PMX_AA_EREFCK;
  291. writel(value, &pio->pioa.pdr);
  292. writel(value, &pio->pioa.asr);
  293. #ifdef CONFIG_RMII
  294. value = AT91_PMX_BA_ERXCK;
  295. #else
  296. value = AT91_PMX_BA_ERXCK | AT91_PMX_BA_ECOL |
  297. AT91_PMX_BA_ERXDV | AT91_PMX_BA_ERX3 |
  298. AT91_PMX_BA_ERX2 | AT91_PMX_BA_ETXER |
  299. AT91_PMX_BA_ETX3 | AT91_PMX_BA_ETX2;
  300. #endif
  301. writel(value, &pio->piob.pdr);
  302. writel(value, &pio->piob.bsr);
  303. writel(1 << AT91_ID_EMAC, &pmc->pcer);
  304. writel(readl(&emac->ctl) | AT91_EMAC_CTL_CSR, &emac->ctl);
  305. /* Init Ethernet buffers */
  306. for (i = 0; i < RBF_FRAMEMAX; i++) {
  307. dev->rbfdt[i].addr = (unsigned long) NetRxPackets[i];
  308. dev->rbfdt[i].size = 0;
  309. }
  310. dev->rbfdt[RBF_FRAMEMAX - 1].addr |= RBF_WRAP;
  311. dev->rbindex = 0;
  312. writel((u32) &(dev->rbfdt[0]), &emac->rbqp);
  313. writel(readl(&emac->rsr) &
  314. ~(AT91_EMAC_RSR_OVR | AT91_EMAC_RSR_REC | AT91_EMAC_RSR_BNA),
  315. &emac->rsr);
  316. value = AT91_EMAC_CFG_CAF | AT91_EMAC_CFG_NBC |
  317. HCLK_DIV;
  318. #ifdef CONFIG_RMII
  319. value |= AT91_EMAC_CFG_RMII;
  320. #endif
  321. writel(value, &emac->cfg);
  322. writel(readl(&emac->ctl) | AT91_EMAC_CTL_TE | AT91_EMAC_CTL_RE,
  323. &emac->ctl);
  324. if (!at91emac_phy_init(netdev)) {
  325. at91emac_UpdateLinkSpeed(emac);
  326. return 0;
  327. }
  328. return 1;
  329. }
  330. static void at91emac_halt(struct eth_device *netdev)
  331. {
  332. at91_emac_t *emac;
  333. emac = (at91_emac_t *) netdev->iobase;
  334. writel(readl(&emac->ctl) & ~(AT91_EMAC_CTL_TE | AT91_EMAC_CTL_RE),
  335. &emac->ctl);
  336. DEBUG_AT91EMAC("halt MAC\n");
  337. }
  338. static int at91emac_send(struct eth_device *netdev, volatile void *packet,
  339. int length)
  340. {
  341. at91_emac_t *emac;
  342. emac = (at91_emac_t *) netdev->iobase;
  343. while (!(readl(&emac->tsr) & AT91_EMAC_TSR_BNQ))
  344. ;
  345. writel((u32) packet, &emac->tar);
  346. writel(AT91_EMAC_TCR_LEN(length), &emac->tcr);
  347. while (AT91_EMAC_TCR_LEN(readl(&emac->tcr)))
  348. ;
  349. DEBUG_AT91EMAC("Send %d \n", length);
  350. writel(readl(&emac->tsr) | AT91_EMAC_TSR_COMP, &emac->tsr);
  351. return 0;
  352. }
  353. static int at91emac_recv(struct eth_device *netdev)
  354. {
  355. emac_device *dev;
  356. at91_emac_t *emac;
  357. rbf_t *rbfp;
  358. int size;
  359. emac = (at91_emac_t *) netdev->iobase;
  360. dev = (emac_device *) netdev->priv;
  361. rbfp = &dev->rbfdt[dev->rbindex];
  362. while (rbfp->addr & RBF_OWNER) {
  363. size = rbfp->size & RBF_SIZE;
  364. NetReceive(NetRxPackets[dev->rbindex], size);
  365. DEBUG_AT91EMAC("Recv[%d]: %d bytes @ %x \n",
  366. dev->rbindex, size, rbfp->addr);
  367. rbfp->addr &= ~RBF_OWNER;
  368. rbfp->size = 0;
  369. if (dev->rbindex < (RBF_FRAMEMAX-1))
  370. dev->rbindex++;
  371. else
  372. dev->rbindex = 0;
  373. rbfp = &(dev->rbfdt[dev->rbindex]);
  374. if (!(rbfp->addr & RBF_OWNER))
  375. writel(readl(&emac->rsr) | AT91_EMAC_RSR_REC,
  376. &emac->rsr);
  377. }
  378. if (readl(&emac->isr) & AT91_EMAC_IxR_RBNA) {
  379. /* EMAC silicon bug 41.3.1 workaround 1 */
  380. writel(readl(&emac->ctl) & ~AT91_EMAC_CTL_RE, &emac->ctl);
  381. writel(readl(&emac->ctl) | AT91_EMAC_CTL_RE, &emac->ctl);
  382. dev->rbindex = 0;
  383. printf("%s: reset receiver (EMAC dead lock bug)\n",
  384. netdev->name);
  385. }
  386. return 0;
  387. }
  388. static int at91emac_write_hwaddr(struct eth_device *netdev)
  389. {
  390. emac_device *dev;
  391. at91_emac_t *emac;
  392. at91_pmc_t *pmc = (at91_pmc_t *) AT91_PMC_BASE;
  393. emac = (at91_emac_t *) netdev->iobase;
  394. dev = (emac_device *) netdev->priv;
  395. writel(1 << AT91_ID_EMAC, &pmc->pcer);
  396. DEBUG_AT91EMAC("init MAC-ADDR %x%x \n",
  397. cpu_to_le16(*((u16 *)(netdev->enetaddr + 4))),
  398. cpu_to_le32(*((u32 *)netdev->enetaddr)));
  399. writel(cpu_to_le32(*((u32 *)netdev->enetaddr)), &emac->sa2l);
  400. writel(cpu_to_le16(*((u16 *)(netdev->enetaddr + 4))), &emac->sa2h);
  401. DEBUG_AT91EMAC("init MAC-ADDR %x%x \n",
  402. readl(&emac->sa2h), readl(&emac->sa2l));
  403. return 0;
  404. }
  405. int at91emac_register(bd_t *bis, unsigned long iobase)
  406. {
  407. emac_device *emac;
  408. emac_device *emacfix;
  409. struct eth_device *dev;
  410. if (iobase == 0)
  411. iobase = AT91_EMAC_BASE;
  412. emac = malloc(sizeof(*emac)+512);
  413. if (emac == NULL)
  414. return 1;
  415. dev = malloc(sizeof(*dev));
  416. if (dev == NULL) {
  417. free(emac);
  418. return 1;
  419. }
  420. /* alignment as per Errata (64 bytes) is insufficient! */
  421. emacfix = (emac_device *) (((unsigned long) emac + 0x1ff) & 0xFFFFFE00);
  422. memset(emacfix, 0, sizeof(emac_device));
  423. memset(dev, 0, sizeof(*dev));
  424. #ifndef CONFIG_RMII
  425. sprintf(dev->name, "AT91 EMAC");
  426. #else
  427. sprintf(dev->name, "AT91 EMAC RMII");
  428. #endif
  429. dev->iobase = iobase;
  430. dev->priv = emacfix;
  431. dev->init = at91emac_init;
  432. dev->halt = at91emac_halt;
  433. dev->send = at91emac_send;
  434. dev->recv = at91emac_recv;
  435. dev->write_hwaddr = at91emac_write_hwaddr;
  436. eth_register(dev);
  437. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  438. miiphy_register(dev->name, at91emac_mii_read, at91emac_mii_write);
  439. #endif
  440. return 1;
  441. }