macb.c 23 KB

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