macb.c 15 KB

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