macb.c 27 KB

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