at91_emac.c 12 KB

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