macb.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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 MACB_TX_DUMMY_DMA_DESC_SIZE (DMA_DESC_BYTES(1))
  51. #define RXADDR_USED 0x00000001
  52. #define RXADDR_WRAP 0x00000002
  53. #define RXBUF_FRMLEN_MASK 0x00000fff
  54. #define RXBUF_FRAME_START 0x00004000
  55. #define RXBUF_FRAME_END 0x00008000
  56. #define RXBUF_TYPEID_MATCH 0x00400000
  57. #define RXBUF_ADDR4_MATCH 0x00800000
  58. #define RXBUF_ADDR3_MATCH 0x01000000
  59. #define RXBUF_ADDR2_MATCH 0x02000000
  60. #define RXBUF_ADDR1_MATCH 0x04000000
  61. #define RXBUF_BROADCAST 0x80000000
  62. #define TXBUF_FRMLEN_MASK 0x000007ff
  63. #define TXBUF_FRAME_END 0x00008000
  64. #define TXBUF_NOCRC 0x00010000
  65. #define TXBUF_EXHAUSTED 0x08000000
  66. #define TXBUF_UNDERRUN 0x10000000
  67. #define TXBUF_MAXRETRY 0x20000000
  68. #define TXBUF_WRAP 0x40000000
  69. #define TXBUF_USED 0x80000000
  70. struct macb_device {
  71. void *regs;
  72. unsigned int rx_tail;
  73. unsigned int tx_head;
  74. unsigned int tx_tail;
  75. void *rx_buffer;
  76. void *tx_buffer;
  77. struct macb_dma_desc *rx_ring;
  78. struct macb_dma_desc *tx_ring;
  79. unsigned long rx_buffer_dma;
  80. unsigned long rx_ring_dma;
  81. unsigned long tx_ring_dma;
  82. struct macb_dma_desc *dummy_desc;
  83. unsigned long dummy_desc_dma;
  84. const struct device *dev;
  85. struct eth_device netdev;
  86. unsigned short phy_addr;
  87. struct mii_dev *bus;
  88. };
  89. #define to_macb(_nd) container_of(_nd, struct macb_device, netdev)
  90. static int macb_is_gem(struct macb_device *macb)
  91. {
  92. return MACB_BFEXT(IDNUM, macb_readl(macb, MID)) == 0x2;
  93. }
  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) || defined(CONFIG_PHYLIB)
  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. #define RX 1
  166. #define TX 0
  167. static inline void macb_invalidate_ring_desc(struct macb_device *macb, bool rx)
  168. {
  169. if (rx)
  170. invalidate_dcache_range(macb->rx_ring_dma, macb->rx_ring_dma +
  171. MACB_RX_DMA_DESC_SIZE);
  172. else
  173. invalidate_dcache_range(macb->tx_ring_dma, macb->tx_ring_dma +
  174. MACB_TX_DMA_DESC_SIZE);
  175. }
  176. static inline void macb_flush_ring_desc(struct macb_device *macb, bool rx)
  177. {
  178. if (rx)
  179. flush_dcache_range(macb->rx_ring_dma, macb->rx_ring_dma +
  180. MACB_RX_DMA_DESC_SIZE);
  181. else
  182. flush_dcache_range(macb->tx_ring_dma, macb->tx_ring_dma +
  183. MACB_TX_DMA_DESC_SIZE);
  184. }
  185. static inline void macb_flush_rx_buffer(struct macb_device *macb)
  186. {
  187. flush_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
  188. MACB_RX_BUFFER_SIZE);
  189. }
  190. static inline void macb_invalidate_rx_buffer(struct macb_device *macb)
  191. {
  192. invalidate_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
  193. MACB_RX_BUFFER_SIZE);
  194. }
  195. #if defined(CONFIG_CMD_NET)
  196. static int macb_send(struct eth_device *netdev, void *packet, int length)
  197. {
  198. struct macb_device *macb = to_macb(netdev);
  199. unsigned long paddr, ctrl;
  200. unsigned int tx_head = macb->tx_head;
  201. int i;
  202. paddr = dma_map_single(packet, length, DMA_TO_DEVICE);
  203. ctrl = length & TXBUF_FRMLEN_MASK;
  204. ctrl |= TXBUF_FRAME_END;
  205. if (tx_head == (MACB_TX_RING_SIZE - 1)) {
  206. ctrl |= TXBUF_WRAP;
  207. macb->tx_head = 0;
  208. } else {
  209. macb->tx_head++;
  210. }
  211. macb->tx_ring[tx_head].ctrl = ctrl;
  212. macb->tx_ring[tx_head].addr = paddr;
  213. barrier();
  214. macb_flush_ring_desc(macb, TX);
  215. /* Do we need check paddr and length is dcache line aligned? */
  216. flush_dcache_range(paddr, paddr + length);
  217. macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
  218. /*
  219. * I guess this is necessary because the networking core may
  220. * re-use the transmit buffer as soon as we return...
  221. */
  222. for (i = 0; i <= MACB_TX_TIMEOUT; i++) {
  223. barrier();
  224. macb_invalidate_ring_desc(macb, TX);
  225. ctrl = macb->tx_ring[tx_head].ctrl;
  226. if (ctrl & TXBUF_USED)
  227. break;
  228. udelay(1);
  229. }
  230. dma_unmap_single(packet, length, paddr);
  231. if (i <= MACB_TX_TIMEOUT) {
  232. if (ctrl & TXBUF_UNDERRUN)
  233. printf("%s: TX underrun\n", netdev->name);
  234. if (ctrl & TXBUF_EXHAUSTED)
  235. printf("%s: TX buffers exhausted in mid frame\n",
  236. netdev->name);
  237. } else {
  238. printf("%s: TX timeout\n", netdev->name);
  239. }
  240. /* No one cares anyway */
  241. return 0;
  242. }
  243. static void reclaim_rx_buffers(struct macb_device *macb,
  244. unsigned int new_tail)
  245. {
  246. unsigned int i;
  247. i = macb->rx_tail;
  248. macb_invalidate_ring_desc(macb, RX);
  249. while (i > new_tail) {
  250. macb->rx_ring[i].addr &= ~RXADDR_USED;
  251. i++;
  252. if (i > MACB_RX_RING_SIZE)
  253. i = 0;
  254. }
  255. while (i < new_tail) {
  256. macb->rx_ring[i].addr &= ~RXADDR_USED;
  257. i++;
  258. }
  259. barrier();
  260. macb_flush_ring_desc(macb, RX);
  261. macb->rx_tail = new_tail;
  262. }
  263. static int macb_recv(struct eth_device *netdev)
  264. {
  265. struct macb_device *macb = to_macb(netdev);
  266. unsigned int rx_tail = macb->rx_tail;
  267. void *buffer;
  268. int length;
  269. int wrapped = 0;
  270. u32 status;
  271. for (;;) {
  272. macb_invalidate_ring_desc(macb, RX);
  273. if (!(macb->rx_ring[rx_tail].addr & RXADDR_USED))
  274. return -1;
  275. status = macb->rx_ring[rx_tail].ctrl;
  276. if (status & RXBUF_FRAME_START) {
  277. if (rx_tail != macb->rx_tail)
  278. reclaim_rx_buffers(macb, rx_tail);
  279. wrapped = 0;
  280. }
  281. if (status & RXBUF_FRAME_END) {
  282. buffer = macb->rx_buffer + 128 * macb->rx_tail;
  283. length = status & RXBUF_FRMLEN_MASK;
  284. macb_invalidate_rx_buffer(macb);
  285. if (wrapped) {
  286. unsigned int headlen, taillen;
  287. headlen = 128 * (MACB_RX_RING_SIZE
  288. - macb->rx_tail);
  289. taillen = length - headlen;
  290. memcpy((void *)net_rx_packets[0],
  291. buffer, headlen);
  292. memcpy((void *)net_rx_packets[0] + headlen,
  293. macb->rx_buffer, taillen);
  294. buffer = (void *)net_rx_packets[0];
  295. }
  296. net_process_received_packet(buffer, length);
  297. if (++rx_tail >= MACB_RX_RING_SIZE)
  298. rx_tail = 0;
  299. reclaim_rx_buffers(macb, rx_tail);
  300. } else {
  301. if (++rx_tail >= MACB_RX_RING_SIZE) {
  302. wrapped = 1;
  303. rx_tail = 0;
  304. }
  305. }
  306. barrier();
  307. }
  308. return 0;
  309. }
  310. static void macb_phy_reset(struct macb_device *macb)
  311. {
  312. struct eth_device *netdev = &macb->netdev;
  313. int i;
  314. u16 status, adv;
  315. adv = ADVERTISE_CSMA | ADVERTISE_ALL;
  316. macb_mdio_write(macb, MII_ADVERTISE, adv);
  317. printf("%s: Starting autonegotiation...\n", netdev->name);
  318. macb_mdio_write(macb, MII_BMCR, (BMCR_ANENABLE
  319. | BMCR_ANRESTART));
  320. for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
  321. status = macb_mdio_read(macb, MII_BMSR);
  322. if (status & BMSR_ANEGCOMPLETE)
  323. break;
  324. udelay(100);
  325. }
  326. if (status & BMSR_ANEGCOMPLETE)
  327. printf("%s: Autonegotiation complete\n", netdev->name);
  328. else
  329. printf("%s: Autonegotiation timed out (status=0x%04x)\n",
  330. netdev->name, status);
  331. }
  332. #ifdef CONFIG_MACB_SEARCH_PHY
  333. static int macb_phy_find(struct macb_device *macb)
  334. {
  335. int i;
  336. u16 phy_id;
  337. /* Search for PHY... */
  338. for (i = 0; i < 32; i++) {
  339. macb->phy_addr = i;
  340. phy_id = macb_mdio_read(macb, MII_PHYSID1);
  341. if (phy_id != 0xffff) {
  342. printf("%s: PHY present at %d\n", macb->netdev.name, i);
  343. return 1;
  344. }
  345. }
  346. /* PHY isn't up to snuff */
  347. printf("%s: PHY not found\n", macb->netdev.name);
  348. return 0;
  349. }
  350. #endif /* CONFIG_MACB_SEARCH_PHY */
  351. static int macb_phy_init(struct macb_device *macb)
  352. {
  353. struct eth_device *netdev = &macb->netdev;
  354. #ifdef CONFIG_PHYLIB
  355. struct phy_device *phydev;
  356. #endif
  357. u32 ncfgr;
  358. u16 phy_id, status, adv, lpa;
  359. int media, speed, duplex;
  360. int i;
  361. arch_get_mdio_control(netdev->name);
  362. #ifdef CONFIG_MACB_SEARCH_PHY
  363. /* Auto-detect phy_addr */
  364. if (!macb_phy_find(macb))
  365. return 0;
  366. #endif /* CONFIG_MACB_SEARCH_PHY */
  367. /* Check if the PHY is up to snuff... */
  368. phy_id = macb_mdio_read(macb, MII_PHYSID1);
  369. if (phy_id == 0xffff) {
  370. printf("%s: No PHY present\n", netdev->name);
  371. return 0;
  372. }
  373. #ifdef CONFIG_PHYLIB
  374. /* need to consider other phy interface mode */
  375. phydev = phy_connect(macb->bus, macb->phy_addr, netdev,
  376. PHY_INTERFACE_MODE_RGMII);
  377. if (!phydev) {
  378. printf("phy_connect failed\n");
  379. return -ENODEV;
  380. }
  381. phy_config(phydev);
  382. #endif
  383. status = macb_mdio_read(macb, MII_BMSR);
  384. if (!(status & BMSR_LSTATUS)) {
  385. /* Try to re-negotiate if we don't have link already. */
  386. macb_phy_reset(macb);
  387. for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
  388. status = macb_mdio_read(macb, MII_BMSR);
  389. if (status & BMSR_LSTATUS)
  390. break;
  391. udelay(100);
  392. }
  393. }
  394. if (!(status & BMSR_LSTATUS)) {
  395. printf("%s: link down (status: 0x%04x)\n",
  396. netdev->name, status);
  397. return 0;
  398. }
  399. /* First check for GMAC */
  400. if (macb_is_gem(macb)) {
  401. lpa = macb_mdio_read(macb, MII_STAT1000);
  402. if (lpa & (LPA_1000FULL | LPA_1000HALF)) {
  403. duplex = ((lpa & LPA_1000FULL) ? 1 : 0);
  404. printf("%s: link up, 1000Mbps %s-duplex (lpa: 0x%04x)\n",
  405. netdev->name,
  406. duplex ? "full" : "half",
  407. lpa);
  408. ncfgr = macb_readl(macb, NCFGR);
  409. ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
  410. ncfgr |= GEM_BIT(GBE);
  411. if (duplex)
  412. ncfgr |= MACB_BIT(FD);
  413. macb_writel(macb, NCFGR, ncfgr);
  414. return 1;
  415. }
  416. }
  417. /* fall back for EMAC checking */
  418. adv = macb_mdio_read(macb, MII_ADVERTISE);
  419. lpa = macb_mdio_read(macb, MII_LPA);
  420. media = mii_nway_result(lpa & adv);
  421. speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
  422. ? 1 : 0);
  423. duplex = (media & ADVERTISE_FULL) ? 1 : 0;
  424. printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
  425. netdev->name,
  426. speed ? "100" : "10",
  427. duplex ? "full" : "half",
  428. lpa);
  429. ncfgr = macb_readl(macb, NCFGR);
  430. ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD) | GEM_BIT(GBE));
  431. if (speed)
  432. ncfgr |= MACB_BIT(SPD);
  433. if (duplex)
  434. ncfgr |= MACB_BIT(FD);
  435. macb_writel(macb, NCFGR, ncfgr);
  436. return 1;
  437. }
  438. static int gmac_init_multi_queues(struct macb_device *macb)
  439. {
  440. int i, num_queues = 1;
  441. u32 queue_mask;
  442. /* bit 0 is never set but queue 0 always exists */
  443. queue_mask = gem_readl(macb, DCFG6) & 0xff;
  444. queue_mask |= 0x1;
  445. for (i = 1; i < MACB_MAX_QUEUES; i++)
  446. if (queue_mask & (1 << i))
  447. num_queues++;
  448. macb->dummy_desc->ctrl = TXBUF_USED;
  449. macb->dummy_desc->addr = 0;
  450. flush_dcache_range(macb->dummy_desc_dma, macb->dummy_desc_dma +
  451. MACB_TX_DUMMY_DMA_DESC_SIZE);
  452. for (i = 1; i < num_queues; i++)
  453. gem_writel_queue_TBQP(macb, macb->dummy_desc_dma, i - 1);
  454. return 0;
  455. }
  456. static int macb_init(struct eth_device *netdev, bd_t *bd)
  457. {
  458. struct macb_device *macb = to_macb(netdev);
  459. unsigned long paddr;
  460. int i;
  461. /*
  462. * macb_halt should have been called at some point before now,
  463. * so we'll assume the controller is idle.
  464. */
  465. /* initialize DMA descriptors */
  466. paddr = macb->rx_buffer_dma;
  467. for (i = 0; i < MACB_RX_RING_SIZE; i++) {
  468. if (i == (MACB_RX_RING_SIZE - 1))
  469. paddr |= RXADDR_WRAP;
  470. macb->rx_ring[i].addr = paddr;
  471. macb->rx_ring[i].ctrl = 0;
  472. paddr += 128;
  473. }
  474. macb_flush_ring_desc(macb, RX);
  475. macb_flush_rx_buffer(macb);
  476. for (i = 0; i < MACB_TX_RING_SIZE; i++) {
  477. macb->tx_ring[i].addr = 0;
  478. if (i == (MACB_TX_RING_SIZE - 1))
  479. macb->tx_ring[i].ctrl = TXBUF_USED | TXBUF_WRAP;
  480. else
  481. macb->tx_ring[i].ctrl = TXBUF_USED;
  482. }
  483. macb_flush_ring_desc(macb, TX);
  484. macb->rx_tail = 0;
  485. macb->tx_head = 0;
  486. macb->tx_tail = 0;
  487. macb_writel(macb, RBQP, macb->rx_ring_dma);
  488. macb_writel(macb, TBQP, macb->tx_ring_dma);
  489. if (macb_is_gem(macb)) {
  490. /* Check the multi queue and initialize the queue for tx */
  491. gmac_init_multi_queues(macb);
  492. /*
  493. * When the GMAC IP with GE feature, this bit is used to
  494. * select interface between RGMII and GMII.
  495. * When the GMAC IP without GE feature, this bit is used
  496. * to select interface between RMII and MII.
  497. */
  498. #if defined(CONFIG_RGMII) || defined(CONFIG_RMII)
  499. gem_writel(macb, UR, GEM_BIT(RGMII));
  500. #else
  501. gem_writel(macb, UR, 0);
  502. #endif
  503. } else {
  504. /* choose RMII or MII mode. This depends on the board */
  505. #ifdef CONFIG_RMII
  506. #ifdef CONFIG_AT91FAMILY
  507. macb_writel(macb, USRIO, MACB_BIT(RMII) | MACB_BIT(CLKEN));
  508. #else
  509. macb_writel(macb, USRIO, 0);
  510. #endif
  511. #else
  512. #ifdef CONFIG_AT91FAMILY
  513. macb_writel(macb, USRIO, MACB_BIT(CLKEN));
  514. #else
  515. macb_writel(macb, USRIO, MACB_BIT(MII));
  516. #endif
  517. #endif /* CONFIG_RMII */
  518. }
  519. if (!macb_phy_init(macb))
  520. return -1;
  521. /* Enable TX and RX */
  522. macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE));
  523. return 0;
  524. }
  525. static void macb_halt(struct eth_device *netdev)
  526. {
  527. struct macb_device *macb = to_macb(netdev);
  528. u32 ncr, tsr;
  529. /* Halt the controller and wait for any ongoing transmission to end. */
  530. ncr = macb_readl(macb, NCR);
  531. ncr |= MACB_BIT(THALT);
  532. macb_writel(macb, NCR, ncr);
  533. do {
  534. tsr = macb_readl(macb, TSR);
  535. } while (tsr & MACB_BIT(TGO));
  536. /* Disable TX and RX, and clear statistics */
  537. macb_writel(macb, NCR, MACB_BIT(CLRSTAT));
  538. }
  539. static int macb_write_hwaddr(struct eth_device *dev)
  540. {
  541. struct macb_device *macb = to_macb(dev);
  542. u32 hwaddr_bottom;
  543. u16 hwaddr_top;
  544. /* set hardware address */
  545. hwaddr_bottom = dev->enetaddr[0] | dev->enetaddr[1] << 8 |
  546. dev->enetaddr[2] << 16 | dev->enetaddr[3] << 24;
  547. macb_writel(macb, SA1B, hwaddr_bottom);
  548. hwaddr_top = dev->enetaddr[4] | dev->enetaddr[5] << 8;
  549. macb_writel(macb, SA1T, hwaddr_top);
  550. return 0;
  551. }
  552. static u32 macb_mdc_clk_div(int id, struct macb_device *macb)
  553. {
  554. u32 config;
  555. unsigned long macb_hz = get_macb_pclk_rate(id);
  556. if (macb_hz < 20000000)
  557. config = MACB_BF(CLK, MACB_CLK_DIV8);
  558. else if (macb_hz < 40000000)
  559. config = MACB_BF(CLK, MACB_CLK_DIV16);
  560. else if (macb_hz < 80000000)
  561. config = MACB_BF(CLK, MACB_CLK_DIV32);
  562. else
  563. config = MACB_BF(CLK, MACB_CLK_DIV64);
  564. return config;
  565. }
  566. static u32 gem_mdc_clk_div(int id, struct macb_device *macb)
  567. {
  568. u32 config;
  569. unsigned long macb_hz = get_macb_pclk_rate(id);
  570. if (macb_hz < 20000000)
  571. config = GEM_BF(CLK, GEM_CLK_DIV8);
  572. else if (macb_hz < 40000000)
  573. config = GEM_BF(CLK, GEM_CLK_DIV16);
  574. else if (macb_hz < 80000000)
  575. config = GEM_BF(CLK, GEM_CLK_DIV32);
  576. else if (macb_hz < 120000000)
  577. config = GEM_BF(CLK, GEM_CLK_DIV48);
  578. else if (macb_hz < 160000000)
  579. config = GEM_BF(CLK, GEM_CLK_DIV64);
  580. else
  581. config = GEM_BF(CLK, GEM_CLK_DIV96);
  582. return config;
  583. }
  584. /*
  585. * Get the DMA bus width field of the network configuration register that we
  586. * should program. We find the width from decoding the design configuration
  587. * register to find the maximum supported data bus width.
  588. */
  589. static u32 macb_dbw(struct macb_device *macb)
  590. {
  591. switch (GEM_BFEXT(DBWDEF, gem_readl(macb, DCFG1))) {
  592. case 4:
  593. return GEM_BF(DBW, GEM_DBW128);
  594. case 2:
  595. return GEM_BF(DBW, GEM_DBW64);
  596. case 1:
  597. default:
  598. return GEM_BF(DBW, GEM_DBW32);
  599. }
  600. }
  601. int macb_eth_initialize(int id, void *regs, unsigned int phy_addr)
  602. {
  603. struct macb_device *macb;
  604. struct eth_device *netdev;
  605. u32 ncfgr;
  606. macb = malloc(sizeof(struct macb_device));
  607. if (!macb) {
  608. printf("Error: Failed to allocate memory for MACB%d\n", id);
  609. return -1;
  610. }
  611. memset(macb, 0, sizeof(struct macb_device));
  612. netdev = &macb->netdev;
  613. macb->rx_buffer = dma_alloc_coherent(MACB_RX_BUFFER_SIZE,
  614. &macb->rx_buffer_dma);
  615. macb->rx_ring = dma_alloc_coherent(MACB_RX_DMA_DESC_SIZE,
  616. &macb->rx_ring_dma);
  617. macb->tx_ring = dma_alloc_coherent(MACB_TX_DMA_DESC_SIZE,
  618. &macb->tx_ring_dma);
  619. macb->dummy_desc = dma_alloc_coherent(MACB_TX_DUMMY_DMA_DESC_SIZE,
  620. &macb->dummy_desc_dma);
  621. /* TODO: we need check the rx/tx_ring_dma is dcache line aligned */
  622. macb->regs = regs;
  623. macb->phy_addr = phy_addr;
  624. if (macb_is_gem(macb))
  625. sprintf(netdev->name, "gmac%d", id);
  626. else
  627. sprintf(netdev->name, "macb%d", id);
  628. netdev->init = macb_init;
  629. netdev->halt = macb_halt;
  630. netdev->send = macb_send;
  631. netdev->recv = macb_recv;
  632. netdev->write_hwaddr = macb_write_hwaddr;
  633. /*
  634. * Do some basic initialization so that we at least can talk
  635. * to the PHY
  636. */
  637. if (macb_is_gem(macb)) {
  638. ncfgr = gem_mdc_clk_div(id, macb);
  639. ncfgr |= macb_dbw(macb);
  640. } else {
  641. ncfgr = macb_mdc_clk_div(id, macb);
  642. }
  643. macb_writel(macb, NCFGR, ncfgr);
  644. eth_register(netdev);
  645. #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
  646. miiphy_register(netdev->name, macb_miiphy_read, macb_miiphy_write);
  647. macb->bus = miiphy_get_dev_by_name(netdev->name);
  648. #endif
  649. return 0;
  650. }
  651. #endif