macb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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. /*
  20. * The u-boot networking stack is a little weird. It seems like the
  21. * networking core allocates receive buffers up front without any
  22. * regard to the hardware that's supposed to actually receive those
  23. * packets.
  24. *
  25. * The MACB receives packets into 128-byte receive buffers, so the
  26. * buffers allocated by the core isn't very practical to use. We'll
  27. * allocate our own, but we need one such buffer in case a packet
  28. * wraps around the DMA ring so that we have to copy it.
  29. *
  30. * Therefore, define CONFIG_SYS_RX_ETH_BUFFER to 1 in the board-specific
  31. * configuration header. This way, the core allocates one RX buffer
  32. * and one TX buffer, each of which can hold a ethernet packet of
  33. * maximum size.
  34. *
  35. * For some reason, the networking core unconditionally specifies a
  36. * 32-byte packet "alignment" (which really should be called
  37. * "padding"). MACB shouldn't need that, but we'll refrain from any
  38. * core modifications here...
  39. */
  40. #include <net.h>
  41. #include <netdev.h>
  42. #include <malloc.h>
  43. #include <miiphy.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 CONFIG_SYS_MACB_RX_BUFFER_SIZE 4096
  51. #define CONFIG_SYS_MACB_RX_RING_SIZE (CONFIG_SYS_MACB_RX_BUFFER_SIZE / 128)
  52. #define CONFIG_SYS_MACB_TX_RING_SIZE 16
  53. #define CONFIG_SYS_MACB_TX_TIMEOUT 1000
  54. #define CONFIG_SYS_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_MII)
  141. int macb_miiphy_read(const char *devname, u8 phy_adr, u8 reg, u16 *value)
  142. {
  143. struct eth_device *dev = eth_get_dev_by_name(devname);
  144. struct macb_device *macb = to_macb(dev);
  145. if ( macb->phy_addr != phy_adr )
  146. return -1;
  147. *value = macb_mdio_read(macb, reg);
  148. return 0;
  149. }
  150. int macb_miiphy_write(const char *devname, u8 phy_adr, u8 reg, u16 value)
  151. {
  152. struct eth_device *dev = eth_get_dev_by_name(devname);
  153. struct macb_device *macb = to_macb(dev);
  154. if ( macb->phy_addr != phy_adr )
  155. return -1;
  156. macb_mdio_write(macb, reg, value);
  157. return 0;
  158. }
  159. #endif
  160. #if defined(CONFIG_CMD_NET)
  161. static int macb_send(struct eth_device *netdev, void *packet, int length)
  162. {
  163. struct macb_device *macb = to_macb(netdev);
  164. unsigned long paddr, ctrl;
  165. unsigned int tx_head = macb->tx_head;
  166. int i;
  167. paddr = dma_map_single(packet, length, DMA_TO_DEVICE);
  168. ctrl = length & TXBUF_FRMLEN_MASK;
  169. ctrl |= TXBUF_FRAME_END;
  170. if (tx_head == (CONFIG_SYS_MACB_TX_RING_SIZE - 1)) {
  171. ctrl |= TXBUF_WRAP;
  172. macb->tx_head = 0;
  173. } else
  174. macb->tx_head++;
  175. macb->tx_ring[tx_head].ctrl = ctrl;
  176. macb->tx_ring[tx_head].addr = paddr;
  177. barrier();
  178. macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
  179. /*
  180. * I guess this is necessary because the networking core may
  181. * re-use the transmit buffer as soon as we return...
  182. */
  183. for (i = 0; i <= CONFIG_SYS_MACB_TX_TIMEOUT; i++) {
  184. barrier();
  185. ctrl = macb->tx_ring[tx_head].ctrl;
  186. if (ctrl & TXBUF_USED)
  187. break;
  188. udelay(1);
  189. }
  190. dma_unmap_single(packet, length, paddr);
  191. if (i <= CONFIG_SYS_MACB_TX_TIMEOUT) {
  192. if (ctrl & TXBUF_UNDERRUN)
  193. printf("%s: TX underrun\n", netdev->name);
  194. if (ctrl & TXBUF_EXHAUSTED)
  195. printf("%s: TX buffers exhausted in mid frame\n",
  196. netdev->name);
  197. } else {
  198. printf("%s: TX timeout\n", netdev->name);
  199. }
  200. /* No one cares anyway */
  201. return 0;
  202. }
  203. static void reclaim_rx_buffers(struct macb_device *macb,
  204. unsigned int new_tail)
  205. {
  206. unsigned int i;
  207. i = macb->rx_tail;
  208. while (i > new_tail) {
  209. macb->rx_ring[i].addr &= ~RXADDR_USED;
  210. i++;
  211. if (i > CONFIG_SYS_MACB_RX_RING_SIZE)
  212. i = 0;
  213. }
  214. while (i < new_tail) {
  215. macb->rx_ring[i].addr &= ~RXADDR_USED;
  216. i++;
  217. }
  218. barrier();
  219. macb->rx_tail = new_tail;
  220. }
  221. static int macb_recv(struct eth_device *netdev)
  222. {
  223. struct macb_device *macb = to_macb(netdev);
  224. unsigned int rx_tail = macb->rx_tail;
  225. void *buffer;
  226. int length;
  227. int wrapped = 0;
  228. u32 status;
  229. for (;;) {
  230. if (!(macb->rx_ring[rx_tail].addr & RXADDR_USED))
  231. return -1;
  232. status = macb->rx_ring[rx_tail].ctrl;
  233. if (status & RXBUF_FRAME_START) {
  234. if (rx_tail != macb->rx_tail)
  235. reclaim_rx_buffers(macb, rx_tail);
  236. wrapped = 0;
  237. }
  238. if (status & RXBUF_FRAME_END) {
  239. buffer = macb->rx_buffer + 128 * macb->rx_tail;
  240. length = status & RXBUF_FRMLEN_MASK;
  241. if (wrapped) {
  242. unsigned int headlen, taillen;
  243. headlen = 128 * (CONFIG_SYS_MACB_RX_RING_SIZE
  244. - macb->rx_tail);
  245. taillen = length - headlen;
  246. memcpy((void *)NetRxPackets[0],
  247. buffer, headlen);
  248. memcpy((void *)NetRxPackets[0] + headlen,
  249. macb->rx_buffer, taillen);
  250. buffer = (void *)NetRxPackets[0];
  251. }
  252. NetReceive(buffer, length);
  253. if (++rx_tail >= CONFIG_SYS_MACB_RX_RING_SIZE)
  254. rx_tail = 0;
  255. reclaim_rx_buffers(macb, rx_tail);
  256. } else {
  257. if (++rx_tail >= CONFIG_SYS_MACB_RX_RING_SIZE) {
  258. wrapped = 1;
  259. rx_tail = 0;
  260. }
  261. }
  262. barrier();
  263. }
  264. return 0;
  265. }
  266. static void macb_phy_reset(struct macb_device *macb)
  267. {
  268. struct eth_device *netdev = &macb->netdev;
  269. int i;
  270. u16 status, adv;
  271. adv = ADVERTISE_CSMA | ADVERTISE_ALL;
  272. macb_mdio_write(macb, MII_ADVERTISE, adv);
  273. printf("%s: Starting autonegotiation...\n", netdev->name);
  274. macb_mdio_write(macb, MII_BMCR, (BMCR_ANENABLE
  275. | BMCR_ANRESTART));
  276. for (i = 0; i < CONFIG_SYS_MACB_AUTONEG_TIMEOUT / 100; i++) {
  277. status = macb_mdio_read(macb, MII_BMSR);
  278. if (status & BMSR_ANEGCOMPLETE)
  279. break;
  280. udelay(100);
  281. }
  282. if (status & BMSR_ANEGCOMPLETE)
  283. printf("%s: Autonegotiation complete\n", netdev->name);
  284. else
  285. printf("%s: Autonegotiation timed out (status=0x%04x)\n",
  286. netdev->name, status);
  287. }
  288. #ifdef CONFIG_MACB_SEARCH_PHY
  289. static int macb_phy_find(struct macb_device *macb)
  290. {
  291. int i;
  292. u16 phy_id;
  293. /* Search for PHY... */
  294. for (i = 0; i < 32; i++) {
  295. macb->phy_addr = i;
  296. phy_id = macb_mdio_read(macb, MII_PHYSID1);
  297. if (phy_id != 0xffff) {
  298. printf("%s: PHY present at %d\n", macb->netdev.name, i);
  299. return 1;
  300. }
  301. }
  302. /* PHY isn't up to snuff */
  303. printf("%s: PHY not found", macb->netdev.name);
  304. return 0;
  305. }
  306. #endif /* CONFIG_MACB_SEARCH_PHY */
  307. static int macb_phy_init(struct macb_device *macb)
  308. {
  309. struct eth_device *netdev = &macb->netdev;
  310. u32 ncfgr;
  311. u16 phy_id, status, adv, lpa;
  312. int media, speed, duplex;
  313. int i;
  314. #ifdef CONFIG_MACB_SEARCH_PHY
  315. /* Auto-detect phy_addr */
  316. if (!macb_phy_find(macb)) {
  317. return 0;
  318. }
  319. #endif /* CONFIG_MACB_SEARCH_PHY */
  320. /* Check if the PHY is up to snuff... */
  321. phy_id = macb_mdio_read(macb, MII_PHYSID1);
  322. if (phy_id == 0xffff) {
  323. printf("%s: No PHY present\n", netdev->name);
  324. return 0;
  325. }
  326. status = macb_mdio_read(macb, MII_BMSR);
  327. if (!(status & BMSR_LSTATUS)) {
  328. /* Try to re-negotiate if we don't have link already. */
  329. macb_phy_reset(macb);
  330. for (i = 0; i < CONFIG_SYS_MACB_AUTONEG_TIMEOUT / 100; i++) {
  331. status = macb_mdio_read(macb, MII_BMSR);
  332. if (status & BMSR_LSTATUS)
  333. break;
  334. udelay(100);
  335. }
  336. }
  337. if (!(status & BMSR_LSTATUS)) {
  338. printf("%s: link down (status: 0x%04x)\n",
  339. netdev->name, status);
  340. return 0;
  341. } else {
  342. adv = macb_mdio_read(macb, MII_ADVERTISE);
  343. lpa = macb_mdio_read(macb, MII_LPA);
  344. media = mii_nway_result(lpa & adv);
  345. speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
  346. ? 1 : 0);
  347. duplex = (media & ADVERTISE_FULL) ? 1 : 0;
  348. printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
  349. netdev->name,
  350. speed ? "100" : "10",
  351. duplex ? "full" : "half",
  352. lpa);
  353. ncfgr = macb_readl(macb, NCFGR);
  354. ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
  355. if (speed)
  356. ncfgr |= MACB_BIT(SPD);
  357. if (duplex)
  358. ncfgr |= MACB_BIT(FD);
  359. macb_writel(macb, NCFGR, ncfgr);
  360. return 1;
  361. }
  362. }
  363. static int macb_init(struct eth_device *netdev, bd_t *bd)
  364. {
  365. struct macb_device *macb = to_macb(netdev);
  366. unsigned long paddr;
  367. int i;
  368. /*
  369. * macb_halt should have been called at some point before now,
  370. * so we'll assume the controller is idle.
  371. */
  372. /* initialize DMA descriptors */
  373. paddr = macb->rx_buffer_dma;
  374. for (i = 0; i < CONFIG_SYS_MACB_RX_RING_SIZE; i++) {
  375. if (i == (CONFIG_SYS_MACB_RX_RING_SIZE - 1))
  376. paddr |= RXADDR_WRAP;
  377. macb->rx_ring[i].addr = paddr;
  378. macb->rx_ring[i].ctrl = 0;
  379. paddr += 128;
  380. }
  381. for (i = 0; i < CONFIG_SYS_MACB_TX_RING_SIZE; i++) {
  382. macb->tx_ring[i].addr = 0;
  383. if (i == (CONFIG_SYS_MACB_TX_RING_SIZE - 1))
  384. macb->tx_ring[i].ctrl = TXBUF_USED | TXBUF_WRAP;
  385. else
  386. macb->tx_ring[i].ctrl = TXBUF_USED;
  387. }
  388. macb->rx_tail = macb->tx_head = macb->tx_tail = 0;
  389. macb_writel(macb, RBQP, macb->rx_ring_dma);
  390. macb_writel(macb, TBQP, macb->tx_ring_dma);
  391. /* choose RMII or MII mode. This depends on the board */
  392. #ifdef CONFIG_RMII
  393. #if defined(CONFIG_AT91CAP9) || defined(CONFIG_AT91SAM9260) || \
  394. defined(CONFIG_AT91SAM9263) || defined(CONFIG_AT91SAM9G20) || \
  395. defined(CONFIG_AT91SAM9G45) || defined(CONFIG_AT91SAM9M10G45) || \
  396. defined(CONFIG_AT91SAM9XE) || defined(CONFIG_AT91SAM9X5)
  397. macb_writel(macb, USRIO, MACB_BIT(RMII) | MACB_BIT(CLKEN));
  398. #else
  399. macb_writel(macb, USRIO, 0);
  400. #endif
  401. #else
  402. #if defined(CONFIG_AT91CAP9) || defined(CONFIG_AT91SAM9260) || \
  403. defined(CONFIG_AT91SAM9263) || defined(CONFIG_AT91SAM9G20) || \
  404. defined(CONFIG_AT91SAM9G45) || defined(CONFIG_AT91SAM9M10G45) || \
  405. defined(CONFIG_AT91SAM9XE) || defined(CONFIG_AT91SAM9X5)
  406. macb_writel(macb, USRIO, MACB_BIT(CLKEN));
  407. #else
  408. macb_writel(macb, USRIO, MACB_BIT(MII));
  409. #endif
  410. #endif /* CONFIG_RMII */
  411. if (!macb_phy_init(macb))
  412. return -1;
  413. /* Enable TX and RX */
  414. macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE));
  415. return 0;
  416. }
  417. static void macb_halt(struct eth_device *netdev)
  418. {
  419. struct macb_device *macb = to_macb(netdev);
  420. u32 ncr, tsr;
  421. /* Halt the controller and wait for any ongoing transmission to end. */
  422. ncr = macb_readl(macb, NCR);
  423. ncr |= MACB_BIT(THALT);
  424. macb_writel(macb, NCR, ncr);
  425. do {
  426. tsr = macb_readl(macb, TSR);
  427. } while (tsr & MACB_BIT(TGO));
  428. /* Disable TX and RX, and clear statistics */
  429. macb_writel(macb, NCR, MACB_BIT(CLRSTAT));
  430. }
  431. static int macb_write_hwaddr(struct eth_device *dev)
  432. {
  433. struct macb_device *macb = to_macb(dev);
  434. u32 hwaddr_bottom;
  435. u16 hwaddr_top;
  436. /* set hardware address */
  437. hwaddr_bottom = dev->enetaddr[0] | dev->enetaddr[1] << 8 |
  438. dev->enetaddr[2] << 16 | dev->enetaddr[3] << 24;
  439. macb_writel(macb, SA1B, hwaddr_bottom);
  440. hwaddr_top = dev->enetaddr[4] | dev->enetaddr[5] << 8;
  441. macb_writel(macb, SA1T, hwaddr_top);
  442. return 0;
  443. }
  444. int macb_eth_initialize(int id, void *regs, unsigned int phy_addr)
  445. {
  446. struct macb_device *macb;
  447. struct eth_device *netdev;
  448. unsigned long macb_hz;
  449. u32 ncfgr;
  450. macb = malloc(sizeof(struct macb_device));
  451. if (!macb) {
  452. printf("Error: Failed to allocate memory for MACB%d\n", id);
  453. return -1;
  454. }
  455. memset(macb, 0, sizeof(struct macb_device));
  456. netdev = &macb->netdev;
  457. macb->rx_buffer = dma_alloc_coherent(CONFIG_SYS_MACB_RX_BUFFER_SIZE,
  458. &macb->rx_buffer_dma);
  459. macb->rx_ring = dma_alloc_coherent(CONFIG_SYS_MACB_RX_RING_SIZE
  460. * sizeof(struct macb_dma_desc),
  461. &macb->rx_ring_dma);
  462. macb->tx_ring = dma_alloc_coherent(CONFIG_SYS_MACB_TX_RING_SIZE
  463. * sizeof(struct macb_dma_desc),
  464. &macb->tx_ring_dma);
  465. macb->regs = regs;
  466. macb->phy_addr = phy_addr;
  467. sprintf(netdev->name, "macb%d", id);
  468. netdev->init = macb_init;
  469. netdev->halt = macb_halt;
  470. netdev->send = macb_send;
  471. netdev->recv = macb_recv;
  472. netdev->write_hwaddr = macb_write_hwaddr;
  473. /*
  474. * Do some basic initialization so that we at least can talk
  475. * to the PHY
  476. */
  477. macb_hz = get_macb_pclk_rate(id);
  478. if (macb_hz < 20000000)
  479. ncfgr = MACB_BF(CLK, MACB_CLK_DIV8);
  480. else if (macb_hz < 40000000)
  481. ncfgr = MACB_BF(CLK, MACB_CLK_DIV16);
  482. else if (macb_hz < 80000000)
  483. ncfgr = MACB_BF(CLK, MACB_CLK_DIV32);
  484. else
  485. ncfgr = MACB_BF(CLK, MACB_CLK_DIV64);
  486. macb_writel(macb, NCFGR, ncfgr);
  487. eth_register(netdev);
  488. #if defined(CONFIG_CMD_MII)
  489. miiphy_register(netdev->name, macb_miiphy_read, macb_miiphy_write);
  490. #endif
  491. return 0;
  492. }
  493. #endif