macb.c 25 KB

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