macb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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 CFG_MACB_RX_BUFFER_SIZE 4096
  49. #define CFG_MACB_RX_RING_SIZE (CFG_MACB_RX_BUFFER_SIZE / 128)
  50. #define CFG_MACB_TX_RING_SIZE 16
  51. #define CFG_MACB_TX_TIMEOUT 1000
  52. #define CFG_MACB_AUTONEG_TIMEOUT 5000000
  53. struct macb_dma_desc {
  54. u32 addr;
  55. u32 ctrl;
  56. };
  57. #define RXADDR_USED 0x00000001
  58. #define RXADDR_WRAP 0x00000002
  59. #define RXBUF_FRMLEN_MASK 0x00000fff
  60. #define RXBUF_FRAME_START 0x00004000
  61. #define RXBUF_FRAME_END 0x00008000
  62. #define RXBUF_TYPEID_MATCH 0x00400000
  63. #define RXBUF_ADDR4_MATCH 0x00800000
  64. #define RXBUF_ADDR3_MATCH 0x01000000
  65. #define RXBUF_ADDR2_MATCH 0x02000000
  66. #define RXBUF_ADDR1_MATCH 0x04000000
  67. #define RXBUF_BROADCAST 0x80000000
  68. #define TXBUF_FRMLEN_MASK 0x000007ff
  69. #define TXBUF_FRAME_END 0x00008000
  70. #define TXBUF_NOCRC 0x00010000
  71. #define TXBUF_EXHAUSTED 0x08000000
  72. #define TXBUF_UNDERRUN 0x10000000
  73. #define TXBUF_MAXRETRY 0x20000000
  74. #define TXBUF_WRAP 0x40000000
  75. #define TXBUF_USED 0x80000000
  76. struct macb_device {
  77. void *regs;
  78. unsigned int rx_tail;
  79. unsigned int tx_head;
  80. unsigned int tx_tail;
  81. void *rx_buffer;
  82. void *tx_buffer;
  83. struct macb_dma_desc *rx_ring;
  84. struct macb_dma_desc *tx_ring;
  85. unsigned long rx_buffer_dma;
  86. unsigned long rx_ring_dma;
  87. unsigned long tx_ring_dma;
  88. const struct device *dev;
  89. struct eth_device netdev;
  90. unsigned short phy_addr;
  91. };
  92. #define to_macb(_nd) container_of(_nd, struct macb_device, netdev)
  93. static void macb_mdio_write(struct macb_device *macb, u8 reg, u16 value)
  94. {
  95. unsigned long netctl;
  96. unsigned long netstat;
  97. unsigned long frame;
  98. netctl = macb_readl(macb, NCR);
  99. netctl |= MACB_BIT(MPE);
  100. macb_writel(macb, NCR, netctl);
  101. frame = (MACB_BF(SOF, 1)
  102. | MACB_BF(RW, 1)
  103. | MACB_BF(PHYA, macb->phy_addr)
  104. | MACB_BF(REGA, reg)
  105. | MACB_BF(CODE, 2)
  106. | MACB_BF(DATA, value));
  107. macb_writel(macb, MAN, frame);
  108. do {
  109. netstat = macb_readl(macb, NSR);
  110. } while (!(netstat & MACB_BIT(IDLE)));
  111. netctl = macb_readl(macb, NCR);
  112. netctl &= ~MACB_BIT(MPE);
  113. macb_writel(macb, NCR, netctl);
  114. }
  115. static u16 macb_mdio_read(struct macb_device *macb, u8 reg)
  116. {
  117. unsigned long netctl;
  118. unsigned long netstat;
  119. unsigned long frame;
  120. netctl = macb_readl(macb, NCR);
  121. netctl |= MACB_BIT(MPE);
  122. macb_writel(macb, NCR, netctl);
  123. frame = (MACB_BF(SOF, 1)
  124. | MACB_BF(RW, 2)
  125. | MACB_BF(PHYA, macb->phy_addr)
  126. | MACB_BF(REGA, reg)
  127. | MACB_BF(CODE, 2));
  128. macb_writel(macb, MAN, frame);
  129. do {
  130. netstat = macb_readl(macb, NSR);
  131. } while (!(netstat & MACB_BIT(IDLE)));
  132. frame = macb_readl(macb, MAN);
  133. netctl = macb_readl(macb, NCR);
  134. netctl &= ~MACB_BIT(MPE);
  135. macb_writel(macb, NCR, netctl);
  136. return MACB_BFEXT(DATA, frame);
  137. }
  138. #if (CONFIG_COMMANDS & CFG_CMD_NET)
  139. static int macb_send(struct eth_device *netdev, volatile void *packet,
  140. int length)
  141. {
  142. struct macb_device *macb = to_macb(netdev);
  143. unsigned long paddr, ctrl;
  144. unsigned int tx_head = macb->tx_head;
  145. int i;
  146. paddr = dma_map_single(packet, length, DMA_TO_DEVICE);
  147. ctrl = length & TXBUF_FRMLEN_MASK;
  148. ctrl |= TXBUF_FRAME_END;
  149. if (tx_head == (CFG_MACB_TX_RING_SIZE - 1)) {
  150. ctrl |= TXBUF_WRAP;
  151. macb->tx_head = 0;
  152. } else
  153. macb->tx_head++;
  154. macb->tx_ring[tx_head].ctrl = ctrl;
  155. macb->tx_ring[tx_head].addr = paddr;
  156. macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
  157. /*
  158. * I guess this is necessary because the networking core may
  159. * re-use the transmit buffer as soon as we return...
  160. */
  161. i = 0;
  162. while (!(macb->tx_ring[tx_head].ctrl & TXBUF_USED)) {
  163. if (i > CFG_MACB_TX_TIMEOUT) {
  164. printf("%s: TX timeout\n", netdev->name);
  165. break;
  166. }
  167. udelay(1);
  168. i++;
  169. }
  170. dma_unmap_single(packet, length, paddr);
  171. if (i <= CFG_MACB_TX_TIMEOUT) {
  172. ctrl = macb->tx_ring[tx_head].ctrl;
  173. if (ctrl & TXBUF_UNDERRUN)
  174. printf("%s: TX underrun\n", netdev->name);
  175. if (ctrl & TXBUF_EXHAUSTED)
  176. printf("%s: TX buffers exhausted in mid frame\n",
  177. netdev->name);
  178. }
  179. /* No one cares anyway */
  180. return 0;
  181. }
  182. static void reclaim_rx_buffers(struct macb_device *macb,
  183. unsigned int new_tail)
  184. {
  185. unsigned int i;
  186. i = macb->rx_tail;
  187. while (i > new_tail) {
  188. macb->rx_ring[i].addr &= ~RXADDR_USED;
  189. i++;
  190. if (i > CFG_MACB_RX_RING_SIZE)
  191. i = 0;
  192. }
  193. while (i < new_tail) {
  194. macb->rx_ring[i].addr &= ~RXADDR_USED;
  195. i++;
  196. }
  197. macb->rx_tail = new_tail;
  198. }
  199. static int macb_recv(struct eth_device *netdev)
  200. {
  201. struct macb_device *macb = to_macb(netdev);
  202. unsigned int rx_tail = macb->rx_tail;
  203. void *buffer;
  204. int length;
  205. int wrapped = 0;
  206. u32 status;
  207. for (;;) {
  208. if (!(macb->rx_ring[rx_tail].addr & RXADDR_USED))
  209. return -1;
  210. status = macb->rx_ring[rx_tail].ctrl;
  211. if (status & RXBUF_FRAME_START) {
  212. if (rx_tail != macb->rx_tail)
  213. reclaim_rx_buffers(macb, rx_tail);
  214. wrapped = 0;
  215. }
  216. if (status & RXBUF_FRAME_END) {
  217. buffer = macb->rx_buffer + 128 * macb->rx_tail;
  218. length = status & RXBUF_FRMLEN_MASK;
  219. if (wrapped) {
  220. unsigned int headlen, taillen;
  221. headlen = 128 * (CFG_MACB_RX_RING_SIZE
  222. - macb->rx_tail);
  223. taillen = length - headlen;
  224. memcpy((void *)NetRxPackets[0],
  225. buffer, headlen);
  226. memcpy((void *)NetRxPackets[0] + headlen,
  227. macb->rx_buffer, taillen);
  228. buffer = (void *)NetRxPackets[0];
  229. }
  230. NetReceive(buffer, length);
  231. if (++rx_tail >= CFG_MACB_RX_RING_SIZE)
  232. rx_tail = 0;
  233. reclaim_rx_buffers(macb, rx_tail);
  234. } else {
  235. if (++rx_tail >= CFG_MACB_RX_RING_SIZE) {
  236. wrapped = 1;
  237. rx_tail = 0;
  238. }
  239. }
  240. }
  241. return 0;
  242. }
  243. static int macb_phy_init(struct macb_device *macb)
  244. {
  245. struct eth_device *netdev = &macb->netdev;
  246. u32 ncfgr;
  247. u16 phy_id, status, adv, lpa;
  248. int media, speed, duplex;
  249. int i;
  250. /* Check if the PHY is up to snuff... */
  251. phy_id = macb_mdio_read(macb, MII_PHYSID1);
  252. if (phy_id == 0xffff) {
  253. printf("%s: No PHY present\n", netdev->name);
  254. return 0;
  255. }
  256. adv = ADVERTISE_CSMA | ADVERTISE_ALL;
  257. macb_mdio_write(macb, MII_ADVERTISE, adv);
  258. printf("%s: Starting autonegotiation...\n", netdev->name);
  259. macb_mdio_write(macb, MII_BMCR, (BMCR_ANENABLE
  260. | BMCR_ANRESTART));
  261. #if 0
  262. for (i = 0; i < 9; i++)
  263. printf("mii%d: 0x%04x\n", i, macb_mdio_read(macb, i));
  264. #endif
  265. for (i = 0; i < CFG_MACB_AUTONEG_TIMEOUT / 100; i++) {
  266. status = macb_mdio_read(macb, MII_BMSR);
  267. if (status & BMSR_ANEGCOMPLETE)
  268. break;
  269. udelay(100);
  270. }
  271. if (status & BMSR_ANEGCOMPLETE)
  272. printf("%s: Autonegotiation complete\n", netdev->name);
  273. else
  274. printf("%s: Autonegotiation timed out (status=0x%04x)\n",
  275. netdev->name, status);
  276. if (!(status & BMSR_LSTATUS)) {
  277. for (i = 0; i < CFG_MACB_AUTONEG_TIMEOUT / 100; i++) {
  278. udelay(100);
  279. status = macb_mdio_read(macb, MII_BMSR);
  280. if (status & BMSR_LSTATUS)
  281. break;
  282. }
  283. }
  284. if (!(status & BMSR_LSTATUS)) {
  285. printf("%s: link down (status: 0x%04x)\n",
  286. netdev->name, status);
  287. return 0;
  288. } else {
  289. lpa = macb_mdio_read(macb, MII_LPA);
  290. media = mii_nway_result(lpa & adv);
  291. speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
  292. ? 1 : 0);
  293. duplex = (media & ADVERTISE_FULL) ? 1 : 0;
  294. printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
  295. netdev->name,
  296. speed ? "100" : "10",
  297. duplex ? "full" : "half",
  298. lpa);
  299. ncfgr = macb_readl(macb, NCFGR);
  300. ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
  301. if (speed)
  302. ncfgr |= MACB_BIT(SPD);
  303. if (duplex)
  304. ncfgr |= MACB_BIT(FD);
  305. macb_writel(macb, NCFGR, ncfgr);
  306. return 1;
  307. }
  308. }
  309. static int macb_init(struct eth_device *netdev, bd_t *bd)
  310. {
  311. struct macb_device *macb = to_macb(netdev);
  312. unsigned long paddr;
  313. u32 hwaddr_bottom;
  314. u16 hwaddr_top;
  315. int i;
  316. /*
  317. * macb_halt should have been called at some point before now,
  318. * so we'll assume the controller is idle.
  319. */
  320. /* initialize DMA descriptors */
  321. paddr = macb->rx_buffer_dma;
  322. for (i = 0; i < CFG_MACB_RX_RING_SIZE; i++) {
  323. if (i == (CFG_MACB_RX_RING_SIZE - 1))
  324. paddr |= RXADDR_WRAP;
  325. macb->rx_ring[i].addr = paddr;
  326. macb->rx_ring[i].ctrl = 0;
  327. paddr += 128;
  328. }
  329. for (i = 0; i < CFG_MACB_TX_RING_SIZE; i++) {
  330. macb->tx_ring[i].addr = 0;
  331. if (i == (CFG_MACB_TX_RING_SIZE - 1))
  332. macb->tx_ring[i].ctrl = TXBUF_USED | TXBUF_WRAP;
  333. else
  334. macb->tx_ring[i].ctrl = TXBUF_USED;
  335. }
  336. macb->rx_tail = macb->tx_head = macb->tx_tail = 0;
  337. macb_writel(macb, RBQP, macb->rx_ring_dma);
  338. macb_writel(macb, TBQP, macb->tx_ring_dma);
  339. /* set hardware address */
  340. hwaddr_bottom = cpu_to_le32(*((u32 *)netdev->enetaddr));
  341. macb_writel(macb, SA1B, hwaddr_bottom);
  342. hwaddr_top = cpu_to_le16(*((u16 *)(netdev->enetaddr + 4)));
  343. macb_writel(macb, SA1T, hwaddr_top);
  344. /* choose RMII or MII mode. This depends on the board */
  345. #ifdef CONFIG_RMII
  346. macb_writel(macb, USRIO, 0);
  347. #else
  348. macb_writel(macb, USRIO, MACB_BIT(MII));
  349. #endif
  350. if (!macb_phy_init(macb))
  351. return 0;
  352. /* Enable TX and RX */
  353. macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE));
  354. return 1;
  355. }
  356. static void macb_halt(struct eth_device *netdev)
  357. {
  358. struct macb_device *macb = to_macb(netdev);
  359. u32 ncr, tsr;
  360. /* Halt the controller and wait for any ongoing transmission to end. */
  361. ncr = macb_readl(macb, NCR);
  362. ncr |= MACB_BIT(THALT);
  363. macb_writel(macb, NCR, ncr);
  364. do {
  365. tsr = macb_readl(macb, TSR);
  366. } while (tsr & MACB_BIT(TGO));
  367. /* Disable TX and RX, and clear statistics */
  368. macb_writel(macb, NCR, MACB_BIT(CLRSTAT));
  369. }
  370. int macb_eth_initialize(int id, void *regs, unsigned int phy_addr)
  371. {
  372. struct macb_device *macb;
  373. struct eth_device *netdev;
  374. unsigned long macb_hz;
  375. u32 ncfgr;
  376. macb = malloc(sizeof(struct macb_device));
  377. if (!macb) {
  378. printf("Error: Failed to allocate memory for MACB%d\n", id);
  379. return -1;
  380. }
  381. memset(macb, 0, sizeof(struct macb_device));
  382. netdev = &macb->netdev;
  383. macb->rx_buffer = dma_alloc_coherent(CFG_MACB_RX_BUFFER_SIZE,
  384. &macb->rx_buffer_dma);
  385. macb->rx_ring = dma_alloc_coherent(CFG_MACB_RX_RING_SIZE
  386. * sizeof(struct macb_dma_desc),
  387. &macb->rx_ring_dma);
  388. macb->tx_ring = dma_alloc_coherent(CFG_MACB_TX_RING_SIZE
  389. * sizeof(struct macb_dma_desc),
  390. &macb->tx_ring_dma);
  391. macb->regs = regs;
  392. macb->phy_addr = phy_addr;
  393. sprintf(netdev->name, "macb%d", id);
  394. netdev->init = macb_init;
  395. netdev->halt = macb_halt;
  396. netdev->send = macb_send;
  397. netdev->recv = macb_recv;
  398. /*
  399. * Do some basic initialization so that we at least can talk
  400. * to the PHY
  401. */
  402. macb_hz = get_macb_pclk_rate(id);
  403. if (macb_hz < 20000000)
  404. ncfgr = MACB_BF(CLK, MACB_CLK_DIV8);
  405. else if (macb_hz < 40000000)
  406. ncfgr = MACB_BF(CLK, MACB_CLK_DIV16);
  407. else if (macb_hz < 80000000)
  408. ncfgr = MACB_BF(CLK, MACB_CLK_DIV32);
  409. else
  410. ncfgr = MACB_BF(CLK, MACB_CLK_DIV64);
  411. macb_writel(macb, NCFGR, ncfgr);
  412. eth_register(netdev);
  413. return 0;
  414. }
  415. #endif /* (CONFIG_COMMANDS & CFG_CMD_NET) */
  416. #if (CONFIG_COMMANDS & CFG_CMD_MII)
  417. int miiphy_read(unsigned char addr, unsigned char reg, unsigned short *value)
  418. {
  419. unsigned long netctl;
  420. unsigned long netstat;
  421. unsigned long frame;
  422. int iflag;
  423. iflag = disable_interrupts();
  424. netctl = macb_readl(&macb, EMACB_NCR);
  425. netctl |= MACB_BIT(MPE);
  426. macb_writel(&macb, EMACB_NCR, netctl);
  427. if (iflag)
  428. enable_interrupts();
  429. frame = (MACB_BF(SOF, 1)
  430. | MACB_BF(RW, 2)
  431. | MACB_BF(PHYA, addr)
  432. | MACB_BF(REGA, reg)
  433. | MACB_BF(CODE, 2));
  434. macb_writel(&macb, EMACB_MAN, frame);
  435. do {
  436. netstat = macb_readl(&macb, EMACB_NSR);
  437. } while (!(netstat & MACB_BIT(IDLE)));
  438. frame = macb_readl(&macb, EMACB_MAN);
  439. *value = MACB_BFEXT(DATA, frame);
  440. iflag = disable_interrupts();
  441. netctl = macb_readl(&macb, EMACB_NCR);
  442. netctl &= ~MACB_BIT(MPE);
  443. macb_writel(&macb, EMACB_NCR, netctl);
  444. if (iflag)
  445. enable_interrupts();
  446. return 0;
  447. }
  448. int miiphy_write(unsigned char addr, unsigned char reg, unsigned short value)
  449. {
  450. unsigned long netctl;
  451. unsigned long netstat;
  452. unsigned long frame;
  453. int iflag;
  454. iflag = disable_interrupts();
  455. netctl = macb_readl(&macb, EMACB_NCR);
  456. netctl |= MACB_BIT(MPE);
  457. macb_writel(&macb, EMACB_NCR, netctl);
  458. if (iflag)
  459. enable_interrupts();
  460. frame = (MACB_BF(SOF, 1)
  461. | MACB_BF(RW, 1)
  462. | MACB_BF(PHYA, addr)
  463. | MACB_BF(REGA, reg)
  464. | MACB_BF(CODE, 2)
  465. | MACB_BF(DATA, value));
  466. macb_writel(&macb, EMACB_MAN, frame);
  467. do {
  468. netstat = macb_readl(&macb, EMACB_NSR);
  469. } while (!(netstat & MACB_BIT(IDLE)));
  470. iflag = disable_interrupts();
  471. netctl = macb_readl(&macb, EMACB_NCR);
  472. netctl &= ~MACB_BIT(MPE);
  473. macb_writel(&macb, EMACB_NCR, netctl);
  474. if (iflag)
  475. enable_interrupts();
  476. return 0;
  477. }
  478. #endif /* (CONFIG_COMMANDS & CFG_CMD_MII) */
  479. #endif /* CONFIG_MACB */