macb.c 26 KB

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