macb.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /*
  2. * Copyright (C) 2005-2006 Atmel Corporation
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. /*
  8. * The u-boot networking stack is a little weird. It seems like the
  9. * networking core allocates receive buffers up front without any
  10. * regard to the hardware that's supposed to actually receive those
  11. * packets.
  12. *
  13. * The MACB receives packets into 128-byte receive buffers, so the
  14. * buffers allocated by the core isn't very practical to use. We'll
  15. * allocate our own, but we need one such buffer in case a packet
  16. * wraps around the DMA ring so that we have to copy it.
  17. *
  18. * Therefore, define CONFIG_SYS_RX_ETH_BUFFER to 1 in the board-specific
  19. * configuration header. This way, the core allocates one RX buffer
  20. * and one TX buffer, each of which can hold a ethernet packet of
  21. * maximum size.
  22. *
  23. * For some reason, the networking core unconditionally specifies a
  24. * 32-byte packet "alignment" (which really should be called
  25. * "padding"). MACB shouldn't need that, but we'll refrain from any
  26. * core modifications here...
  27. */
  28. #include <net.h>
  29. #include <netdev.h>
  30. #include <malloc.h>
  31. #include <miiphy.h>
  32. #include <linux/mii.h>
  33. #include <asm/io.h>
  34. #include <asm/dma-mapping.h>
  35. #include <asm/arch/clk.h>
  36. #include <asm-generic/errno.h>
  37. #include "macb.h"
  38. #define MACB_RX_BUFFER_SIZE 4096
  39. #define MACB_RX_RING_SIZE (MACB_RX_BUFFER_SIZE / 128)
  40. #define MACB_TX_RING_SIZE 16
  41. #define MACB_TX_TIMEOUT 1000
  42. #define MACB_AUTONEG_TIMEOUT 5000000
  43. struct macb_dma_desc {
  44. u32 addr;
  45. u32 ctrl;
  46. };
  47. #define DMA_DESC_BYTES(n) (n * sizeof(struct macb_dma_desc))
  48. #define MACB_TX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_TX_RING_SIZE))
  49. #define MACB_RX_DMA_DESC_SIZE (DMA_DESC_BYTES(MACB_RX_RING_SIZE))
  50. #define RXADDR_USED 0x00000001
  51. #define RXADDR_WRAP 0x00000002
  52. #define RXBUF_FRMLEN_MASK 0x00000fff
  53. #define RXBUF_FRAME_START 0x00004000
  54. #define RXBUF_FRAME_END 0x00008000
  55. #define RXBUF_TYPEID_MATCH 0x00400000
  56. #define RXBUF_ADDR4_MATCH 0x00800000
  57. #define RXBUF_ADDR3_MATCH 0x01000000
  58. #define RXBUF_ADDR2_MATCH 0x02000000
  59. #define RXBUF_ADDR1_MATCH 0x04000000
  60. #define RXBUF_BROADCAST 0x80000000
  61. #define TXBUF_FRMLEN_MASK 0x000007ff
  62. #define TXBUF_FRAME_END 0x00008000
  63. #define TXBUF_NOCRC 0x00010000
  64. #define TXBUF_EXHAUSTED 0x08000000
  65. #define TXBUF_UNDERRUN 0x10000000
  66. #define TXBUF_MAXRETRY 0x20000000
  67. #define TXBUF_WRAP 0x40000000
  68. #define TXBUF_USED 0x80000000
  69. struct macb_device {
  70. void *regs;
  71. unsigned int rx_tail;
  72. unsigned int tx_head;
  73. unsigned int tx_tail;
  74. void *rx_buffer;
  75. void *tx_buffer;
  76. struct macb_dma_desc *rx_ring;
  77. struct macb_dma_desc *tx_ring;
  78. unsigned long rx_buffer_dma;
  79. unsigned long rx_ring_dma;
  80. unsigned long tx_ring_dma;
  81. const struct device *dev;
  82. struct eth_device netdev;
  83. unsigned short phy_addr;
  84. struct mii_dev *bus;
  85. };
  86. #define to_macb(_nd) container_of(_nd, struct macb_device, netdev)
  87. static int macb_is_gem(struct macb_device *macb)
  88. {
  89. return MACB_BFEXT(IDNUM, macb_readl(macb, MID)) == 0x2;
  90. }
  91. static void macb_mdio_write(struct macb_device *macb, u8 reg, u16 value)
  92. {
  93. unsigned long netctl;
  94. unsigned long netstat;
  95. unsigned long frame;
  96. netctl = macb_readl(macb, NCR);
  97. netctl |= MACB_BIT(MPE);
  98. macb_writel(macb, NCR, netctl);
  99. frame = (MACB_BF(SOF, 1)
  100. | MACB_BF(RW, 1)
  101. | MACB_BF(PHYA, macb->phy_addr)
  102. | MACB_BF(REGA, reg)
  103. | MACB_BF(CODE, 2)
  104. | MACB_BF(DATA, value));
  105. macb_writel(macb, MAN, frame);
  106. do {
  107. netstat = macb_readl(macb, NSR);
  108. } while (!(netstat & MACB_BIT(IDLE)));
  109. netctl = macb_readl(macb, NCR);
  110. netctl &= ~MACB_BIT(MPE);
  111. macb_writel(macb, NCR, netctl);
  112. }
  113. static u16 macb_mdio_read(struct macb_device *macb, u8 reg)
  114. {
  115. unsigned long netctl;
  116. unsigned long netstat;
  117. unsigned long frame;
  118. netctl = macb_readl(macb, NCR);
  119. netctl |= MACB_BIT(MPE);
  120. macb_writel(macb, NCR, netctl);
  121. frame = (MACB_BF(SOF, 1)
  122. | MACB_BF(RW, 2)
  123. | MACB_BF(PHYA, macb->phy_addr)
  124. | MACB_BF(REGA, reg)
  125. | MACB_BF(CODE, 2));
  126. macb_writel(macb, MAN, frame);
  127. do {
  128. netstat = macb_readl(macb, NSR);
  129. } while (!(netstat & MACB_BIT(IDLE)));
  130. frame = macb_readl(macb, MAN);
  131. netctl = macb_readl(macb, NCR);
  132. netctl &= ~MACB_BIT(MPE);
  133. macb_writel(macb, NCR, netctl);
  134. return MACB_BFEXT(DATA, frame);
  135. }
  136. void __weak arch_get_mdio_control(const char *name)
  137. {
  138. return;
  139. }
  140. #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
  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. arch_get_mdio_control(devname);
  148. *value = macb_mdio_read(macb, reg);
  149. return 0;
  150. }
  151. int macb_miiphy_write(const char *devname, u8 phy_adr, u8 reg, u16 value)
  152. {
  153. struct eth_device *dev = eth_get_dev_by_name(devname);
  154. struct macb_device *macb = to_macb(dev);
  155. if (macb->phy_addr != phy_adr)
  156. return -1;
  157. arch_get_mdio_control(devname);
  158. macb_mdio_write(macb, reg, value);
  159. return 0;
  160. }
  161. #endif
  162. #define RX 1
  163. #define TX 0
  164. static inline void macb_invalidate_ring_desc(struct macb_device *macb, bool rx)
  165. {
  166. if (rx)
  167. invalidate_dcache_range(macb->rx_ring_dma, macb->rx_ring_dma +
  168. MACB_RX_DMA_DESC_SIZE);
  169. else
  170. invalidate_dcache_range(macb->tx_ring_dma, macb->tx_ring_dma +
  171. MACB_TX_DMA_DESC_SIZE);
  172. }
  173. static inline void macb_flush_ring_desc(struct macb_device *macb, bool rx)
  174. {
  175. if (rx)
  176. flush_dcache_range(macb->rx_ring_dma, macb->rx_ring_dma +
  177. MACB_RX_DMA_DESC_SIZE);
  178. else
  179. flush_dcache_range(macb->tx_ring_dma, macb->tx_ring_dma +
  180. MACB_TX_DMA_DESC_SIZE);
  181. }
  182. static inline void macb_flush_rx_buffer(struct macb_device *macb)
  183. {
  184. flush_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
  185. MACB_RX_BUFFER_SIZE);
  186. }
  187. static inline void macb_invalidate_rx_buffer(struct macb_device *macb)
  188. {
  189. invalidate_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
  190. MACB_RX_BUFFER_SIZE);
  191. }
  192. #if defined(CONFIG_CMD_NET)
  193. static int macb_send(struct eth_device *netdev, void *packet, int length)
  194. {
  195. struct macb_device *macb = to_macb(netdev);
  196. unsigned long paddr, ctrl;
  197. unsigned int tx_head = macb->tx_head;
  198. int i;
  199. paddr = dma_map_single(packet, length, DMA_TO_DEVICE);
  200. ctrl = length & TXBUF_FRMLEN_MASK;
  201. ctrl |= TXBUF_FRAME_END;
  202. if (tx_head == (MACB_TX_RING_SIZE - 1)) {
  203. ctrl |= TXBUF_WRAP;
  204. macb->tx_head = 0;
  205. } else {
  206. macb->tx_head++;
  207. }
  208. macb->tx_ring[tx_head].ctrl = ctrl;
  209. macb->tx_ring[tx_head].addr = paddr;
  210. barrier();
  211. macb_flush_ring_desc(macb, TX);
  212. /* Do we need check paddr and length is dcache line aligned? */
  213. flush_dcache_range(paddr, paddr + length);
  214. macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
  215. /*
  216. * I guess this is necessary because the networking core may
  217. * re-use the transmit buffer as soon as we return...
  218. */
  219. for (i = 0; i <= MACB_TX_TIMEOUT; i++) {
  220. barrier();
  221. macb_invalidate_ring_desc(macb, TX);
  222. ctrl = macb->tx_ring[tx_head].ctrl;
  223. if (ctrl & TXBUF_USED)
  224. break;
  225. udelay(1);
  226. }
  227. dma_unmap_single(packet, length, paddr);
  228. if (i <= MACB_TX_TIMEOUT) {
  229. if (ctrl & TXBUF_UNDERRUN)
  230. printf("%s: TX underrun\n", netdev->name);
  231. if (ctrl & TXBUF_EXHAUSTED)
  232. printf("%s: TX buffers exhausted in mid frame\n",
  233. netdev->name);
  234. } else {
  235. printf("%s: TX timeout\n", netdev->name);
  236. }
  237. /* No one cares anyway */
  238. return 0;
  239. }
  240. static void reclaim_rx_buffers(struct macb_device *macb,
  241. unsigned int new_tail)
  242. {
  243. unsigned int i;
  244. i = macb->rx_tail;
  245. macb_invalidate_ring_desc(macb, RX);
  246. while (i > new_tail) {
  247. macb->rx_ring[i].addr &= ~RXADDR_USED;
  248. i++;
  249. if (i > MACB_RX_RING_SIZE)
  250. i = 0;
  251. }
  252. while (i < new_tail) {
  253. macb->rx_ring[i].addr &= ~RXADDR_USED;
  254. i++;
  255. }
  256. barrier();
  257. macb_flush_ring_desc(macb, RX);
  258. macb->rx_tail = new_tail;
  259. }
  260. static int macb_recv(struct eth_device *netdev)
  261. {
  262. struct macb_device *macb = to_macb(netdev);
  263. unsigned int rx_tail = macb->rx_tail;
  264. void *buffer;
  265. int length;
  266. int wrapped = 0;
  267. u32 status;
  268. for (;;) {
  269. macb_invalidate_ring_desc(macb, RX);
  270. if (!(macb->rx_ring[rx_tail].addr & RXADDR_USED))
  271. return -1;
  272. status = macb->rx_ring[rx_tail].ctrl;
  273. if (status & RXBUF_FRAME_START) {
  274. if (rx_tail != macb->rx_tail)
  275. reclaim_rx_buffers(macb, rx_tail);
  276. wrapped = 0;
  277. }
  278. if (status & RXBUF_FRAME_END) {
  279. buffer = macb->rx_buffer + 128 * macb->rx_tail;
  280. length = status & RXBUF_FRMLEN_MASK;
  281. macb_invalidate_rx_buffer(macb);
  282. if (wrapped) {
  283. unsigned int headlen, taillen;
  284. headlen = 128 * (MACB_RX_RING_SIZE
  285. - macb->rx_tail);
  286. taillen = length - headlen;
  287. memcpy((void *)NetRxPackets[0],
  288. buffer, headlen);
  289. memcpy((void *)NetRxPackets[0] + headlen,
  290. macb->rx_buffer, taillen);
  291. buffer = (void *)NetRxPackets[0];
  292. }
  293. NetReceive(buffer, length);
  294. if (++rx_tail >= MACB_RX_RING_SIZE)
  295. rx_tail = 0;
  296. reclaim_rx_buffers(macb, rx_tail);
  297. } else {
  298. if (++rx_tail >= MACB_RX_RING_SIZE) {
  299. wrapped = 1;
  300. rx_tail = 0;
  301. }
  302. }
  303. barrier();
  304. }
  305. return 0;
  306. }
  307. static void macb_phy_reset(struct macb_device *macb)
  308. {
  309. struct eth_device *netdev = &macb->netdev;
  310. int i;
  311. u16 status, adv;
  312. adv = ADVERTISE_CSMA | ADVERTISE_ALL;
  313. macb_mdio_write(macb, MII_ADVERTISE, adv);
  314. printf("%s: Starting autonegotiation...\n", netdev->name);
  315. macb_mdio_write(macb, MII_BMCR, (BMCR_ANENABLE
  316. | BMCR_ANRESTART));
  317. for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
  318. status = macb_mdio_read(macb, MII_BMSR);
  319. if (status & BMSR_ANEGCOMPLETE)
  320. break;
  321. udelay(100);
  322. }
  323. if (status & BMSR_ANEGCOMPLETE)
  324. printf("%s: Autonegotiation complete\n", netdev->name);
  325. else
  326. printf("%s: Autonegotiation timed out (status=0x%04x)\n",
  327. netdev->name, status);
  328. }
  329. #ifdef CONFIG_MACB_SEARCH_PHY
  330. static int macb_phy_find(struct macb_device *macb)
  331. {
  332. int i;
  333. u16 phy_id;
  334. /* Search for PHY... */
  335. for (i = 0; i < 32; i++) {
  336. macb->phy_addr = i;
  337. phy_id = macb_mdio_read(macb, MII_PHYSID1);
  338. if (phy_id != 0xffff) {
  339. printf("%s: PHY present at %d\n", macb->netdev.name, i);
  340. return 1;
  341. }
  342. }
  343. /* PHY isn't up to snuff */
  344. printf("%s: PHY not found\n", macb->netdev.name);
  345. return 0;
  346. }
  347. #endif /* CONFIG_MACB_SEARCH_PHY */
  348. static int macb_phy_init(struct macb_device *macb)
  349. {
  350. struct eth_device *netdev = &macb->netdev;
  351. #ifdef CONFIG_PHYLIB
  352. struct phy_device *phydev;
  353. #endif
  354. u32 ncfgr;
  355. u16 phy_id, status, adv, lpa;
  356. int media, speed, duplex;
  357. int i;
  358. arch_get_mdio_control(netdev->name);
  359. #ifdef CONFIG_MACB_SEARCH_PHY
  360. /* Auto-detect phy_addr */
  361. if (!macb_phy_find(macb))
  362. return 0;
  363. #endif /* CONFIG_MACB_SEARCH_PHY */
  364. /* Check if the PHY is up to snuff... */
  365. phy_id = macb_mdio_read(macb, MII_PHYSID1);
  366. if (phy_id == 0xffff) {
  367. printf("%s: No PHY present\n", netdev->name);
  368. return 0;
  369. }
  370. #ifdef CONFIG_PHYLIB
  371. /* need to consider other phy interface mode */
  372. phydev = phy_connect(macb->bus, macb->phy_addr, netdev,
  373. PHY_INTERFACE_MODE_RGMII);
  374. if (!phydev) {
  375. printf("phy_connect failed\n");
  376. return -ENODEV;
  377. }
  378. phy_config(phydev);
  379. #endif
  380. status = macb_mdio_read(macb, MII_BMSR);
  381. if (!(status & BMSR_LSTATUS)) {
  382. /* Try to re-negotiate if we don't have link already. */
  383. macb_phy_reset(macb);
  384. for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
  385. status = macb_mdio_read(macb, MII_BMSR);
  386. if (status & BMSR_LSTATUS)
  387. break;
  388. udelay(100);
  389. }
  390. }
  391. if (!(status & BMSR_LSTATUS)) {
  392. printf("%s: link down (status: 0x%04x)\n",
  393. netdev->name, status);
  394. return 0;
  395. }
  396. /* First check for GMAC */
  397. if (macb_is_gem(macb)) {
  398. lpa = macb_mdio_read(macb, MII_STAT1000);
  399. if (lpa & (LPA_1000FULL | LPA_1000HALF)) {
  400. duplex = ((lpa & LPA_1000FULL) ? 1 : 0);
  401. printf("%s: link up, 1000Mbps %s-duplex (lpa: 0x%04x)\n",
  402. netdev->name,
  403. duplex ? "full" : "half",
  404. lpa);
  405. ncfgr = macb_readl(macb, NCFGR);
  406. ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
  407. ncfgr |= GEM_BIT(GBE);
  408. if (duplex)
  409. ncfgr |= MACB_BIT(FD);
  410. macb_writel(macb, NCFGR, ncfgr);
  411. return 1;
  412. }
  413. }
  414. /* fall back for EMAC checking */
  415. adv = macb_mdio_read(macb, MII_ADVERTISE);
  416. lpa = macb_mdio_read(macb, MII_LPA);
  417. media = mii_nway_result(lpa & adv);
  418. speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
  419. ? 1 : 0);
  420. duplex = (media & ADVERTISE_FULL) ? 1 : 0;
  421. printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
  422. netdev->name,
  423. speed ? "100" : "10",
  424. duplex ? "full" : "half",
  425. lpa);
  426. ncfgr = macb_readl(macb, NCFGR);
  427. ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD) | GEM_BIT(GBE));
  428. if (speed)
  429. ncfgr |= MACB_BIT(SPD);
  430. if (duplex)
  431. ncfgr |= MACB_BIT(FD);
  432. macb_writel(macb, NCFGR, ncfgr);
  433. return 1;
  434. }
  435. static int macb_write_hwaddr(struct eth_device *dev);
  436. static int macb_init(struct eth_device *netdev, bd_t *bd)
  437. {
  438. struct macb_device *macb = to_macb(netdev);
  439. unsigned long paddr;
  440. int i;
  441. /*
  442. * macb_halt should have been called at some point before now,
  443. * so we'll assume the controller is idle.
  444. */
  445. /* initialize DMA descriptors */
  446. paddr = macb->rx_buffer_dma;
  447. for (i = 0; i < MACB_RX_RING_SIZE; i++) {
  448. if (i == (MACB_RX_RING_SIZE - 1))
  449. paddr |= RXADDR_WRAP;
  450. macb->rx_ring[i].addr = paddr;
  451. macb->rx_ring[i].ctrl = 0;
  452. paddr += 128;
  453. }
  454. macb_flush_ring_desc(macb, RX);
  455. macb_flush_rx_buffer(macb);
  456. for (i = 0; i < MACB_TX_RING_SIZE; i++) {
  457. macb->tx_ring[i].addr = 0;
  458. if (i == (MACB_TX_RING_SIZE - 1))
  459. macb->tx_ring[i].ctrl = TXBUF_USED | TXBUF_WRAP;
  460. else
  461. macb->tx_ring[i].ctrl = TXBUF_USED;
  462. }
  463. macb_flush_ring_desc(macb, TX);
  464. macb->rx_tail = 0;
  465. macb->tx_head = 0;
  466. macb->tx_tail = 0;
  467. macb_writel(macb, RBQP, macb->rx_ring_dma);
  468. macb_writel(macb, TBQP, macb->tx_ring_dma);
  469. if (macb_is_gem(macb)) {
  470. /*
  471. * When the GMAC IP with GE feature, this bit is used to
  472. * select interface between RGMII and GMII.
  473. * When the GMAC IP without GE feature, this bit is used
  474. * to select interface between RMII and MII.
  475. */
  476. #if defined(CONFIG_RGMII) || defined(CONFIG_RMII)
  477. gem_writel(macb, UR, GEM_BIT(RGMII));
  478. #else
  479. gem_writel(macb, UR, 0);
  480. #endif
  481. } else {
  482. /* choose RMII or MII mode. This depends on the board */
  483. #ifdef CONFIG_RMII
  484. #ifdef CONFIG_AT91FAMILY
  485. macb_writel(macb, USRIO, MACB_BIT(RMII) | MACB_BIT(CLKEN));
  486. #else
  487. macb_writel(macb, USRIO, 0);
  488. #endif
  489. #else
  490. #ifdef CONFIG_AT91FAMILY
  491. macb_writel(macb, USRIO, MACB_BIT(CLKEN));
  492. #else
  493. macb_writel(macb, USRIO, MACB_BIT(MII));
  494. #endif
  495. #endif /* CONFIG_RMII */
  496. }
  497. /* update the ethaddr */
  498. if (is_valid_ether_addr(netdev->enetaddr)) {
  499. macb_write_hwaddr(netdev);
  500. } else {
  501. printf("%s: mac address is not valid\n", netdev->name);
  502. return -1;
  503. }
  504. if (!macb_phy_init(macb))
  505. return -1;
  506. /* Enable TX and RX */
  507. macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE));
  508. return 0;
  509. }
  510. static void macb_halt(struct eth_device *netdev)
  511. {
  512. struct macb_device *macb = to_macb(netdev);
  513. u32 ncr, tsr;
  514. /* Halt the controller and wait for any ongoing transmission to end. */
  515. ncr = macb_readl(macb, NCR);
  516. ncr |= MACB_BIT(THALT);
  517. macb_writel(macb, NCR, ncr);
  518. do {
  519. tsr = macb_readl(macb, TSR);
  520. } while (tsr & MACB_BIT(TGO));
  521. /* Disable TX and RX, and clear statistics */
  522. macb_writel(macb, NCR, MACB_BIT(CLRSTAT));
  523. }
  524. static int macb_write_hwaddr(struct eth_device *dev)
  525. {
  526. struct macb_device *macb = to_macb(dev);
  527. u32 hwaddr_bottom;
  528. u16 hwaddr_top;
  529. /* set hardware address */
  530. hwaddr_bottom = dev->enetaddr[0] | dev->enetaddr[1] << 8 |
  531. dev->enetaddr[2] << 16 | dev->enetaddr[3] << 24;
  532. macb_writel(macb, SA1B, hwaddr_bottom);
  533. hwaddr_top = dev->enetaddr[4] | dev->enetaddr[5] << 8;
  534. macb_writel(macb, SA1T, hwaddr_top);
  535. return 0;
  536. }
  537. static u32 macb_mdc_clk_div(int id, struct macb_device *macb)
  538. {
  539. u32 config;
  540. unsigned long macb_hz = get_macb_pclk_rate(id);
  541. if (macb_hz < 20000000)
  542. config = MACB_BF(CLK, MACB_CLK_DIV8);
  543. else if (macb_hz < 40000000)
  544. config = MACB_BF(CLK, MACB_CLK_DIV16);
  545. else if (macb_hz < 80000000)
  546. config = MACB_BF(CLK, MACB_CLK_DIV32);
  547. else
  548. config = MACB_BF(CLK, MACB_CLK_DIV64);
  549. return config;
  550. }
  551. static u32 gem_mdc_clk_div(int id, struct macb_device *macb)
  552. {
  553. u32 config;
  554. unsigned long macb_hz = get_macb_pclk_rate(id);
  555. if (macb_hz < 20000000)
  556. config = GEM_BF(CLK, GEM_CLK_DIV8);
  557. else if (macb_hz < 40000000)
  558. config = GEM_BF(CLK, GEM_CLK_DIV16);
  559. else if (macb_hz < 80000000)
  560. config = GEM_BF(CLK, GEM_CLK_DIV32);
  561. else if (macb_hz < 120000000)
  562. config = GEM_BF(CLK, GEM_CLK_DIV48);
  563. else if (macb_hz < 160000000)
  564. config = GEM_BF(CLK, GEM_CLK_DIV64);
  565. else
  566. config = GEM_BF(CLK, GEM_CLK_DIV96);
  567. return config;
  568. }
  569. /*
  570. * Get the DMA bus width field of the network configuration register that we
  571. * should program. We find the width from decoding the design configuration
  572. * register to find the maximum supported data bus width.
  573. */
  574. static u32 macb_dbw(struct macb_device *macb)
  575. {
  576. switch (GEM_BFEXT(DBWDEF, gem_readl(macb, DCFG1))) {
  577. case 4:
  578. return GEM_BF(DBW, GEM_DBW128);
  579. case 2:
  580. return GEM_BF(DBW, GEM_DBW64);
  581. case 1:
  582. default:
  583. return GEM_BF(DBW, GEM_DBW32);
  584. }
  585. }
  586. int macb_eth_initialize(int id, void *regs, unsigned int phy_addr)
  587. {
  588. struct macb_device *macb;
  589. struct eth_device *netdev;
  590. u32 ncfgr;
  591. macb = malloc(sizeof(struct macb_device));
  592. if (!macb) {
  593. printf("Error: Failed to allocate memory for MACB%d\n", id);
  594. return -1;
  595. }
  596. memset(macb, 0, sizeof(struct macb_device));
  597. netdev = &macb->netdev;
  598. macb->rx_buffer = dma_alloc_coherent(MACB_RX_BUFFER_SIZE,
  599. &macb->rx_buffer_dma);
  600. macb->rx_ring = dma_alloc_coherent(MACB_RX_DMA_DESC_SIZE,
  601. &macb->rx_ring_dma);
  602. macb->tx_ring = dma_alloc_coherent(MACB_TX_DMA_DESC_SIZE,
  603. &macb->tx_ring_dma);
  604. /* TODO: we need check the rx/tx_ring_dma is dcache line aligned */
  605. macb->regs = regs;
  606. macb->phy_addr = phy_addr;
  607. if (macb_is_gem(macb))
  608. sprintf(netdev->name, "gmac%d", id);
  609. else
  610. sprintf(netdev->name, "macb%d", id);
  611. netdev->init = macb_init;
  612. netdev->halt = macb_halt;
  613. netdev->send = macb_send;
  614. netdev->recv = macb_recv;
  615. netdev->write_hwaddr = macb_write_hwaddr;
  616. /*
  617. * Do some basic initialization so that we at least can talk
  618. * to the PHY
  619. */
  620. if (macb_is_gem(macb)) {
  621. ncfgr = gem_mdc_clk_div(id, macb);
  622. ncfgr |= macb_dbw(macb);
  623. } else {
  624. ncfgr = macb_mdc_clk_div(id, macb);
  625. }
  626. macb_writel(macb, NCFGR, ncfgr);
  627. eth_register(netdev);
  628. #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
  629. miiphy_register(netdev->name, macb_miiphy_read, macb_miiphy_write);
  630. macb->bus = miiphy_get_dev_by_name(netdev->name);
  631. #endif
  632. return 0;
  633. }
  634. #endif