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