at91_emac.c 13 KB

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