macb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /*
  2. * Copyright (C) 2005-2006 Atmel Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <common.h>
  19. #if defined(CONFIG_MACB) && (CONFIG_COMMANDS & (CFG_CMD_NET | CFG_CMD_MII))
  20. /*
  21. * The u-boot networking stack is a little weird. It seems like the
  22. * networking core allocates receive buffers up front without any
  23. * regard to the hardware that's supposed to actually receive those
  24. * packets.
  25. *
  26. * The MACB receives packets into 128-byte receive buffers, so the
  27. * buffers allocated by the core isn't very practical to use. We'll
  28. * allocate our own, but we need one such buffer in case a packet
  29. * wraps around the DMA ring so that we have to copy it.
  30. *
  31. * Therefore, define CFG_RX_ETH_BUFFER to 1 in the board-specific
  32. * configuration header. This way, the core allocates one RX buffer
  33. * and one TX buffer, each of which can hold a ethernet packet of
  34. * maximum size.
  35. *
  36. * For some reason, the networking core unconditionally specifies a
  37. * 32-byte packet "alignment" (which really should be called
  38. * "padding"). MACB shouldn't need that, but we'll refrain from any
  39. * core modifications here...
  40. */
  41. #include <net.h>
  42. #include <malloc.h>
  43. #include <linux/mii.h>
  44. #include <asm/io.h>
  45. #include <asm/dma-mapping.h>
  46. #include <asm/arch/clk.h>
  47. #include "macb.h"
  48. #define barrier() asm volatile("" ::: "memory")
  49. #define CFG_MACB_RX_BUFFER_SIZE 4096
  50. #define CFG_MACB_RX_RING_SIZE (CFG_MACB_RX_BUFFER_SIZE / 128)
  51. #define CFG_MACB_TX_RING_SIZE 16
  52. #define CFG_MACB_TX_TIMEOUT 1000
  53. #define CFG_MACB_AUTONEG_TIMEOUT 5000000
  54. struct macb_dma_desc {
  55. u32 addr;
  56. u32 ctrl;
  57. };
  58. #define RXADDR_USED 0x00000001
  59. #define RXADDR_WRAP 0x00000002
  60. #define RXBUF_FRMLEN_MASK 0x00000fff
  61. #define RXBUF_FRAME_START 0x00004000
  62. #define RXBUF_FRAME_END 0x00008000
  63. #define RXBUF_TYPEID_MATCH 0x00400000
  64. #define RXBUF_ADDR4_MATCH 0x00800000
  65. #define RXBUF_ADDR3_MATCH 0x01000000
  66. #define RXBUF_ADDR2_MATCH 0x02000000
  67. #define RXBUF_ADDR1_MATCH 0x04000000
  68. #define RXBUF_BROADCAST 0x80000000
  69. #define TXBUF_FRMLEN_MASK 0x000007ff
  70. #define TXBUF_FRAME_END 0x00008000
  71. #define TXBUF_NOCRC 0x00010000
  72. #define TXBUF_EXHAUSTED 0x08000000
  73. #define TXBUF_UNDERRUN 0x10000000
  74. #define TXBUF_MAXRETRY 0x20000000
  75. #define TXBUF_WRAP 0x40000000
  76. #define TXBUF_USED 0x80000000
  77. struct macb_device {
  78. void *regs;
  79. unsigned int rx_tail;
  80. unsigned int tx_head;
  81. unsigned int tx_tail;
  82. void *rx_buffer;
  83. void *tx_buffer;
  84. struct macb_dma_desc *rx_ring;
  85. struct macb_dma_desc *tx_ring;
  86. unsigned long rx_buffer_dma;
  87. unsigned long rx_ring_dma;
  88. unsigned long tx_ring_dma;
  89. const struct device *dev;
  90. struct eth_device netdev;
  91. unsigned short phy_addr;
  92. };
  93. #define to_macb(_nd) container_of(_nd, struct macb_device, netdev)
  94. static void macb_mdio_write(struct macb_device *macb, u8 reg, u16 value)
  95. {
  96. unsigned long netctl;
  97. unsigned long netstat;
  98. unsigned long frame;
  99. netctl = macb_readl(macb, NCR);
  100. netctl |= MACB_BIT(MPE);
  101. macb_writel(macb, NCR, netctl);
  102. frame = (MACB_BF(SOF, 1)
  103. | MACB_BF(RW, 1)
  104. | MACB_BF(PHYA, macb->phy_addr)
  105. | MACB_BF(REGA, reg)
  106. | MACB_BF(CODE, 2)
  107. | MACB_BF(DATA, value));
  108. macb_writel(macb, MAN, frame);
  109. do {
  110. netstat = macb_readl(macb, NSR);
  111. } while (!(netstat & MACB_BIT(IDLE)));
  112. netctl = macb_readl(macb, NCR);
  113. netctl &= ~MACB_BIT(MPE);
  114. macb_writel(macb, NCR, netctl);
  115. }
  116. static u16 macb_mdio_read(struct macb_device *macb, u8 reg)
  117. {
  118. unsigned long netctl;
  119. unsigned long netstat;
  120. unsigned long frame;
  121. netctl = macb_readl(macb, NCR);
  122. netctl |= MACB_BIT(MPE);
  123. macb_writel(macb, NCR, netctl);
  124. frame = (MACB_BF(SOF, 1)
  125. | MACB_BF(RW, 2)
  126. | MACB_BF(PHYA, macb->phy_addr)
  127. | MACB_BF(REGA, reg)
  128. | MACB_BF(CODE, 2));
  129. macb_writel(macb, MAN, frame);
  130. do {
  131. netstat = macb_readl(macb, NSR);
  132. } while (!(netstat & MACB_BIT(IDLE)));
  133. frame = macb_readl(macb, MAN);
  134. netctl = macb_readl(macb, NCR);
  135. netctl &= ~MACB_BIT(MPE);
  136. macb_writel(macb, NCR, netctl);
  137. return MACB_BFEXT(DATA, frame);
  138. }
  139. #if (CONFIG_COMMANDS & CFG_CMD_NET)
  140. static int macb_send(struct eth_device *netdev, volatile void *packet,
  141. int length)
  142. {
  143. struct macb_device *macb = to_macb(netdev);
  144. unsigned long paddr, ctrl;
  145. unsigned int tx_head = macb->tx_head;
  146. int i;
  147. paddr = dma_map_single(packet, length, DMA_TO_DEVICE);
  148. ctrl = length & TXBUF_FRMLEN_MASK;
  149. ctrl |= TXBUF_FRAME_END;
  150. if (tx_head == (CFG_MACB_TX_RING_SIZE - 1)) {
  151. ctrl |= TXBUF_WRAP;
  152. macb->tx_head = 0;
  153. } else
  154. macb->tx_head++;
  155. macb->tx_ring[tx_head].ctrl = ctrl;
  156. macb->tx_ring[tx_head].addr = paddr;
  157. barrier();
  158. macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
  159. /*
  160. * I guess this is necessary because the networking core may
  161. * re-use the transmit buffer as soon as we return...
  162. */
  163. for (i = 0; i <= CFG_MACB_TX_TIMEOUT; i++) {
  164. barrier();
  165. ctrl = macb->tx_ring[tx_head].ctrl;
  166. if (ctrl & TXBUF_USED)
  167. break;
  168. udelay(1);
  169. }
  170. dma_unmap_single(packet, length, paddr);
  171. if (i <= CFG_MACB_TX_TIMEOUT) {
  172. if (ctrl & TXBUF_UNDERRUN)
  173. printf("%s: TX underrun\n", netdev->name);
  174. if (ctrl & TXBUF_EXHAUSTED)
  175. printf("%s: TX buffers exhausted in mid frame\n",
  176. netdev->name);
  177. } else {
  178. printf("%s: TX timeout\n", netdev->name);
  179. }
  180. /* No one cares anyway */
  181. return 0;
  182. }
  183. static void reclaim_rx_buffers(struct macb_device *macb,
  184. unsigned int new_tail)
  185. {
  186. unsigned int i;
  187. i = macb->rx_tail;
  188. while (i > new_tail) {
  189. macb->rx_ring[i].addr &= ~RXADDR_USED;
  190. i++;
  191. if (i > CFG_MACB_RX_RING_SIZE)
  192. i = 0;
  193. }
  194. while (i < new_tail) {
  195. macb->rx_ring[i].addr &= ~RXADDR_USED;
  196. i++;
  197. }
  198. barrier();
  199. macb->rx_tail = new_tail;
  200. }
  201. static int macb_recv(struct eth_device *netdev)
  202. {
  203. struct macb_device *macb = to_macb(netdev);
  204. unsigned int rx_tail = macb->rx_tail;
  205. void *buffer;
  206. int length;
  207. int wrapped = 0;
  208. u32 status;
  209. for (;;) {
  210. if (!(macb->rx_ring[rx_tail].addr & RXADDR_USED))
  211. return -1;
  212. status = macb->rx_ring[rx_tail].ctrl;
  213. if (status & RXBUF_FRAME_START) {
  214. if (rx_tail != macb->rx_tail)
  215. reclaim_rx_buffers(macb, rx_tail);
  216. wrapped = 0;
  217. }
  218. if (status & RXBUF_FRAME_END) {
  219. buffer = macb->rx_buffer + 128 * macb->rx_tail;
  220. length = status & RXBUF_FRMLEN_MASK;
  221. if (wrapped) {
  222. unsigned int headlen, taillen;
  223. headlen = 128 * (CFG_MACB_RX_RING_SIZE
  224. - macb->rx_tail);
  225. taillen = length - headlen;
  226. memcpy((void *)NetRxPackets[0],
  227. buffer, headlen);
  228. memcpy((void *)NetRxPackets[0] + headlen,
  229. macb->rx_buffer, taillen);
  230. buffer = (void *)NetRxPackets[0];
  231. }
  232. NetReceive(buffer, length);
  233. if (++rx_tail >= CFG_MACB_RX_RING_SIZE)
  234. rx_tail = 0;
  235. reclaim_rx_buffers(macb, rx_tail);
  236. } else {
  237. if (++rx_tail >= CFG_MACB_RX_RING_SIZE) {
  238. wrapped = 1;
  239. rx_tail = 0;
  240. }
  241. }
  242. barrier();
  243. }
  244. return 0;
  245. }
  246. static void macb_phy_reset(struct macb_device *macb)
  247. {
  248. struct eth_device *netdev = &macb->netdev;
  249. int i;
  250. u16 status, adv;
  251. adv = ADVERTISE_CSMA | ADVERTISE_ALL;
  252. macb_mdio_write(macb, MII_ADVERTISE, adv);
  253. printf("%s: Starting autonegotiation...\n", netdev->name);
  254. macb_mdio_write(macb, MII_BMCR, (BMCR_ANENABLE
  255. | BMCR_ANRESTART));
  256. for (i = 0; i < CFG_MACB_AUTONEG_TIMEOUT / 100; i++) {
  257. status = macb_mdio_read(macb, MII_BMSR);
  258. if (status & BMSR_ANEGCOMPLETE)
  259. break;
  260. udelay(100);
  261. }
  262. if (status & BMSR_ANEGCOMPLETE)
  263. printf("%s: Autonegotiation complete\n", netdev->name);
  264. else
  265. printf("%s: Autonegotiation timed out (status=0x%04x)\n",
  266. netdev->name, status);
  267. }
  268. static int macb_phy_init(struct macb_device *macb)
  269. {
  270. struct eth_device *netdev = &macb->netdev;
  271. u32 ncfgr;
  272. u16 phy_id, status, adv, lpa;
  273. int media, speed, duplex;
  274. int i;
  275. /* Check if the PHY is up to snuff... */
  276. phy_id = macb_mdio_read(macb, MII_PHYSID1);
  277. if (phy_id == 0xffff) {
  278. printf("%s: No PHY present\n", netdev->name);
  279. return 0;
  280. }
  281. status = macb_mdio_read(macb, MII_BMSR);
  282. if (!(status & BMSR_LSTATUS)) {
  283. /* Try to re-negotiate if we don't have link already. */
  284. macb_phy_reset(macb);
  285. for (i = 0; i < CFG_MACB_AUTONEG_TIMEOUT / 100; i++) {
  286. status = macb_mdio_read(macb, MII_BMSR);
  287. if (status & BMSR_LSTATUS)
  288. break;
  289. udelay(100);
  290. }
  291. }
  292. if (!(status & BMSR_LSTATUS)) {
  293. printf("%s: link down (status: 0x%04x)\n",
  294. netdev->name, status);
  295. return 0;
  296. } else {
  297. adv = macb_mdio_read(macb, MII_ADVERTISE);
  298. lpa = macb_mdio_read(macb, MII_LPA);
  299. media = mii_nway_result(lpa & adv);
  300. speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
  301. ? 1 : 0);
  302. duplex = (media & ADVERTISE_FULL) ? 1 : 0;
  303. printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
  304. netdev->name,
  305. speed ? "100" : "10",
  306. duplex ? "full" : "half",
  307. lpa);
  308. ncfgr = macb_readl(macb, NCFGR);
  309. ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
  310. if (speed)
  311. ncfgr |= MACB_BIT(SPD);
  312. if (duplex)
  313. ncfgr |= MACB_BIT(FD);
  314. macb_writel(macb, NCFGR, ncfgr);
  315. return 1;
  316. }
  317. }
  318. static int macb_init(struct eth_device *netdev, bd_t *bd)
  319. {
  320. struct macb_device *macb = to_macb(netdev);
  321. unsigned long paddr;
  322. u32 hwaddr_bottom;
  323. u16 hwaddr_top;
  324. int i;
  325. /*
  326. * macb_halt should have been called at some point before now,
  327. * so we'll assume the controller is idle.
  328. */
  329. /* initialize DMA descriptors */
  330. paddr = macb->rx_buffer_dma;
  331. for (i = 0; i < CFG_MACB_RX_RING_SIZE; i++) {
  332. if (i == (CFG_MACB_RX_RING_SIZE - 1))
  333. paddr |= RXADDR_WRAP;
  334. macb->rx_ring[i].addr = paddr;
  335. macb->rx_ring[i].ctrl = 0;
  336. paddr += 128;
  337. }
  338. for (i = 0; i < CFG_MACB_TX_RING_SIZE; i++) {
  339. macb->tx_ring[i].addr = 0;
  340. if (i == (CFG_MACB_TX_RING_SIZE - 1))
  341. macb->tx_ring[i].ctrl = TXBUF_USED | TXBUF_WRAP;
  342. else
  343. macb->tx_ring[i].ctrl = TXBUF_USED;
  344. }
  345. macb->rx_tail = macb->tx_head = macb->tx_tail = 0;
  346. macb_writel(macb, RBQP, macb->rx_ring_dma);
  347. macb_writel(macb, TBQP, macb->tx_ring_dma);
  348. /* set hardware address */
  349. hwaddr_bottom = cpu_to_le32(*((u32 *)netdev->enetaddr));
  350. macb_writel(macb, SA1B, hwaddr_bottom);
  351. hwaddr_top = cpu_to_le16(*((u16 *)(netdev->enetaddr + 4)));
  352. macb_writel(macb, SA1T, hwaddr_top);
  353. /* choose RMII or MII mode. This depends on the board */
  354. #ifdef CONFIG_RMII
  355. macb_writel(macb, USRIO, 0);
  356. #else
  357. macb_writel(macb, USRIO, MACB_BIT(MII));
  358. #endif
  359. if (!macb_phy_init(macb))
  360. return 0;
  361. /* Enable TX and RX */
  362. macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE));
  363. return 1;
  364. }
  365. static void macb_halt(struct eth_device *netdev)
  366. {
  367. struct macb_device *macb = to_macb(netdev);
  368. u32 ncr, tsr;
  369. /* Halt the controller and wait for any ongoing transmission to end. */
  370. ncr = macb_readl(macb, NCR);
  371. ncr |= MACB_BIT(THALT);
  372. macb_writel(macb, NCR, ncr);
  373. do {
  374. tsr = macb_readl(macb, TSR);
  375. } while (tsr & MACB_BIT(TGO));
  376. /* Disable TX and RX, and clear statistics */
  377. macb_writel(macb, NCR, MACB_BIT(CLRSTAT));
  378. }
  379. int macb_eth_initialize(int id, void *regs, unsigned int phy_addr)
  380. {
  381. struct macb_device *macb;
  382. struct eth_device *netdev;
  383. unsigned long macb_hz;
  384. u32 ncfgr;
  385. macb = malloc(sizeof(struct macb_device));
  386. if (!macb) {
  387. printf("Error: Failed to allocate memory for MACB%d\n", id);
  388. return -1;
  389. }
  390. memset(macb, 0, sizeof(struct macb_device));
  391. netdev = &macb->netdev;
  392. macb->rx_buffer = dma_alloc_coherent(CFG_MACB_RX_BUFFER_SIZE,
  393. &macb->rx_buffer_dma);
  394. macb->rx_ring = dma_alloc_coherent(CFG_MACB_RX_RING_SIZE
  395. * sizeof(struct macb_dma_desc),
  396. &macb->rx_ring_dma);
  397. macb->tx_ring = dma_alloc_coherent(CFG_MACB_TX_RING_SIZE
  398. * sizeof(struct macb_dma_desc),
  399. &macb->tx_ring_dma);
  400. macb->regs = regs;
  401. macb->phy_addr = phy_addr;
  402. sprintf(netdev->name, "macb%d", id);
  403. netdev->init = macb_init;
  404. netdev->halt = macb_halt;
  405. netdev->send = macb_send;
  406. netdev->recv = macb_recv;
  407. /*
  408. * Do some basic initialization so that we at least can talk
  409. * to the PHY
  410. */
  411. macb_hz = get_macb_pclk_rate(id);
  412. if (macb_hz < 20000000)
  413. ncfgr = MACB_BF(CLK, MACB_CLK_DIV8);
  414. else if (macb_hz < 40000000)
  415. ncfgr = MACB_BF(CLK, MACB_CLK_DIV16);
  416. else if (macb_hz < 80000000)
  417. ncfgr = MACB_BF(CLK, MACB_CLK_DIV32);
  418. else
  419. ncfgr = MACB_BF(CLK, MACB_CLK_DIV64);
  420. macb_writel(macb, NCFGR, ncfgr);
  421. eth_register(netdev);
  422. return 0;
  423. }
  424. #endif /* (CONFIG_COMMANDS & CFG_CMD_NET) */
  425. #if (CONFIG_COMMANDS & CFG_CMD_MII)
  426. int miiphy_read(unsigned char addr, unsigned char reg, unsigned short *value)
  427. {
  428. unsigned long netctl;
  429. unsigned long netstat;
  430. unsigned long frame;
  431. int iflag;
  432. iflag = disable_interrupts();
  433. netctl = macb_readl(&macb, EMACB_NCR);
  434. netctl |= MACB_BIT(MPE);
  435. macb_writel(&macb, EMACB_NCR, netctl);
  436. if (iflag)
  437. enable_interrupts();
  438. frame = (MACB_BF(SOF, 1)
  439. | MACB_BF(RW, 2)
  440. | MACB_BF(PHYA, addr)
  441. | MACB_BF(REGA, reg)
  442. | MACB_BF(CODE, 2));
  443. macb_writel(&macb, EMACB_MAN, frame);
  444. do {
  445. netstat = macb_readl(&macb, EMACB_NSR);
  446. } while (!(netstat & MACB_BIT(IDLE)));
  447. frame = macb_readl(&macb, EMACB_MAN);
  448. *value = MACB_BFEXT(DATA, frame);
  449. iflag = disable_interrupts();
  450. netctl = macb_readl(&macb, EMACB_NCR);
  451. netctl &= ~MACB_BIT(MPE);
  452. macb_writel(&macb, EMACB_NCR, netctl);
  453. if (iflag)
  454. enable_interrupts();
  455. return 0;
  456. }
  457. int miiphy_write(unsigned char addr, unsigned char reg, unsigned short value)
  458. {
  459. unsigned long netctl;
  460. unsigned long netstat;
  461. unsigned long frame;
  462. int iflag;
  463. iflag = disable_interrupts();
  464. netctl = macb_readl(&macb, EMACB_NCR);
  465. netctl |= MACB_BIT(MPE);
  466. macb_writel(&macb, EMACB_NCR, netctl);
  467. if (iflag)
  468. enable_interrupts();
  469. frame = (MACB_BF(SOF, 1)
  470. | MACB_BF(RW, 1)
  471. | MACB_BF(PHYA, addr)
  472. | MACB_BF(REGA, reg)
  473. | MACB_BF(CODE, 2)
  474. | MACB_BF(DATA, value));
  475. macb_writel(&macb, EMACB_MAN, frame);
  476. do {
  477. netstat = macb_readl(&macb, EMACB_NSR);
  478. } while (!(netstat & MACB_BIT(IDLE)));
  479. iflag = disable_interrupts();
  480. netctl = macb_readl(&macb, EMACB_NCR);
  481. netctl &= ~MACB_BIT(MPE);
  482. macb_writel(&macb, EMACB_NCR, netctl);
  483. if (iflag)
  484. enable_interrupts();
  485. return 0;
  486. }
  487. #endif /* (CONFIG_COMMANDS & CFG_CMD_MII) */
  488. #endif /* CONFIG_MACB */