macb.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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_DM_ETH
  98. #ifdef CONFIG_CLK
  99. unsigned long pclk_rate;
  100. #endif
  101. phy_interface_t phy_interface;
  102. #endif
  103. };
  104. #ifndef CONFIG_DM_ETH
  105. #define to_macb(_nd) container_of(_nd, struct macb_device, netdev)
  106. #endif
  107. static int macb_is_gem(struct macb_device *macb)
  108. {
  109. return MACB_BFEXT(IDNUM, macb_readl(macb, MID)) == 0x2;
  110. }
  111. #ifndef cpu_is_sama5d2
  112. #define cpu_is_sama5d2() 0
  113. #endif
  114. #ifndef cpu_is_sama5d4
  115. #define cpu_is_sama5d4() 0
  116. #endif
  117. static int gem_is_gigabit_capable(struct macb_device *macb)
  118. {
  119. /*
  120. * The GEM controllers embedded in SAMA5D2 and SAMA5D4 are
  121. * configured to support only 10/100.
  122. */
  123. return macb_is_gem(macb) && !cpu_is_sama5d2() && !cpu_is_sama5d4();
  124. }
  125. static void macb_mdio_write(struct macb_device *macb, u8 reg, u16 value)
  126. {
  127. unsigned long netctl;
  128. unsigned long netstat;
  129. unsigned long frame;
  130. netctl = macb_readl(macb, NCR);
  131. netctl |= MACB_BIT(MPE);
  132. macb_writel(macb, NCR, netctl);
  133. frame = (MACB_BF(SOF, 1)
  134. | MACB_BF(RW, 1)
  135. | MACB_BF(PHYA, macb->phy_addr)
  136. | MACB_BF(REGA, reg)
  137. | MACB_BF(CODE, 2)
  138. | MACB_BF(DATA, value));
  139. macb_writel(macb, MAN, frame);
  140. do {
  141. netstat = macb_readl(macb, NSR);
  142. } while (!(netstat & MACB_BIT(IDLE)));
  143. netctl = macb_readl(macb, NCR);
  144. netctl &= ~MACB_BIT(MPE);
  145. macb_writel(macb, NCR, netctl);
  146. }
  147. static u16 macb_mdio_read(struct macb_device *macb, u8 reg)
  148. {
  149. unsigned long netctl;
  150. unsigned long netstat;
  151. unsigned long frame;
  152. netctl = macb_readl(macb, NCR);
  153. netctl |= MACB_BIT(MPE);
  154. macb_writel(macb, NCR, netctl);
  155. frame = (MACB_BF(SOF, 1)
  156. | MACB_BF(RW, 2)
  157. | MACB_BF(PHYA, macb->phy_addr)
  158. | MACB_BF(REGA, reg)
  159. | MACB_BF(CODE, 2));
  160. macb_writel(macb, MAN, frame);
  161. do {
  162. netstat = macb_readl(macb, NSR);
  163. } while (!(netstat & MACB_BIT(IDLE)));
  164. frame = macb_readl(macb, MAN);
  165. netctl = macb_readl(macb, NCR);
  166. netctl &= ~MACB_BIT(MPE);
  167. macb_writel(macb, NCR, netctl);
  168. return MACB_BFEXT(DATA, frame);
  169. }
  170. void __weak arch_get_mdio_control(const char *name)
  171. {
  172. return;
  173. }
  174. #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
  175. int macb_miiphy_read(struct mii_dev *bus, int phy_adr, int devad, int reg)
  176. {
  177. u16 value = 0;
  178. #ifdef CONFIG_DM_ETH
  179. struct udevice *dev = eth_get_dev_by_name(bus->name);
  180. struct macb_device *macb = dev_get_priv(dev);
  181. #else
  182. struct eth_device *dev = eth_get_dev_by_name(bus->name);
  183. struct macb_device *macb = to_macb(dev);
  184. #endif
  185. if (macb->phy_addr != phy_adr)
  186. return -1;
  187. arch_get_mdio_control(bus->name);
  188. value = macb_mdio_read(macb, reg);
  189. return value;
  190. }
  191. int macb_miiphy_write(struct mii_dev *bus, int phy_adr, int devad, int reg,
  192. u16 value)
  193. {
  194. #ifdef CONFIG_DM_ETH
  195. struct udevice *dev = eth_get_dev_by_name(bus->name);
  196. struct macb_device *macb = dev_get_priv(dev);
  197. #else
  198. struct eth_device *dev = eth_get_dev_by_name(bus->name);
  199. struct macb_device *macb = to_macb(dev);
  200. #endif
  201. if (macb->phy_addr != phy_adr)
  202. return -1;
  203. arch_get_mdio_control(bus->name);
  204. macb_mdio_write(macb, reg, value);
  205. return 0;
  206. }
  207. #endif
  208. #define RX 1
  209. #define TX 0
  210. static inline void macb_invalidate_ring_desc(struct macb_device *macb, bool rx)
  211. {
  212. if (rx)
  213. invalidate_dcache_range(macb->rx_ring_dma,
  214. ALIGN(macb->rx_ring_dma + MACB_RX_DMA_DESC_SIZE,
  215. PKTALIGN));
  216. else
  217. invalidate_dcache_range(macb->tx_ring_dma,
  218. ALIGN(macb->tx_ring_dma + MACB_TX_DMA_DESC_SIZE,
  219. PKTALIGN));
  220. }
  221. static inline void macb_flush_ring_desc(struct macb_device *macb, bool rx)
  222. {
  223. if (rx)
  224. flush_dcache_range(macb->rx_ring_dma, macb->rx_ring_dma +
  225. ALIGN(MACB_RX_DMA_DESC_SIZE, PKTALIGN));
  226. else
  227. flush_dcache_range(macb->tx_ring_dma, macb->tx_ring_dma +
  228. ALIGN(MACB_TX_DMA_DESC_SIZE, PKTALIGN));
  229. }
  230. static inline void macb_flush_rx_buffer(struct macb_device *macb)
  231. {
  232. flush_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
  233. ALIGN(MACB_RX_BUFFER_SIZE, PKTALIGN));
  234. }
  235. static inline void macb_invalidate_rx_buffer(struct macb_device *macb)
  236. {
  237. invalidate_dcache_range(macb->rx_buffer_dma, macb->rx_buffer_dma +
  238. ALIGN(MACB_RX_BUFFER_SIZE, PKTALIGN));
  239. }
  240. #if defined(CONFIG_CMD_NET)
  241. static int _macb_send(struct macb_device *macb, const char *name, void *packet,
  242. int length)
  243. {
  244. unsigned long paddr, ctrl;
  245. unsigned int tx_head = macb->tx_head;
  246. int i;
  247. paddr = dma_map_single(packet, length, DMA_TO_DEVICE);
  248. ctrl = length & TXBUF_FRMLEN_MASK;
  249. ctrl |= TXBUF_FRAME_END;
  250. if (tx_head == (MACB_TX_RING_SIZE - 1)) {
  251. ctrl |= TXBUF_WRAP;
  252. macb->tx_head = 0;
  253. } else {
  254. macb->tx_head++;
  255. }
  256. macb->tx_ring[tx_head].ctrl = ctrl;
  257. macb->tx_ring[tx_head].addr = paddr;
  258. barrier();
  259. macb_flush_ring_desc(macb, TX);
  260. /* Do we need check paddr and length is dcache line aligned? */
  261. flush_dcache_range(paddr, paddr + ALIGN(length, ARCH_DMA_MINALIGN));
  262. macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE) | MACB_BIT(TSTART));
  263. /*
  264. * I guess this is necessary because the networking core may
  265. * re-use the transmit buffer as soon as we return...
  266. */
  267. for (i = 0; i <= MACB_TX_TIMEOUT; i++) {
  268. barrier();
  269. macb_invalidate_ring_desc(macb, TX);
  270. ctrl = macb->tx_ring[tx_head].ctrl;
  271. if (ctrl & TXBUF_USED)
  272. break;
  273. udelay(1);
  274. }
  275. dma_unmap_single(packet, length, paddr);
  276. if (i <= MACB_TX_TIMEOUT) {
  277. if (ctrl & TXBUF_UNDERRUN)
  278. printf("%s: TX underrun\n", name);
  279. if (ctrl & TXBUF_EXHAUSTED)
  280. printf("%s: TX buffers exhausted in mid frame\n", name);
  281. } else {
  282. printf("%s: TX timeout\n", name);
  283. }
  284. /* No one cares anyway */
  285. return 0;
  286. }
  287. static void reclaim_rx_buffers(struct macb_device *macb,
  288. unsigned int new_tail)
  289. {
  290. unsigned int i;
  291. i = macb->rx_tail;
  292. macb_invalidate_ring_desc(macb, RX);
  293. while (i > new_tail) {
  294. macb->rx_ring[i].addr &= ~RXADDR_USED;
  295. i++;
  296. if (i > MACB_RX_RING_SIZE)
  297. i = 0;
  298. }
  299. while (i < new_tail) {
  300. macb->rx_ring[i].addr &= ~RXADDR_USED;
  301. i++;
  302. }
  303. barrier();
  304. macb_flush_ring_desc(macb, RX);
  305. macb->rx_tail = new_tail;
  306. }
  307. static int _macb_recv(struct macb_device *macb, uchar **packetp)
  308. {
  309. unsigned int next_rx_tail = macb->next_rx_tail;
  310. void *buffer;
  311. int length;
  312. u32 status;
  313. macb->wrapped = false;
  314. for (;;) {
  315. macb_invalidate_ring_desc(macb, RX);
  316. if (!(macb->rx_ring[next_rx_tail].addr & RXADDR_USED))
  317. return -EAGAIN;
  318. status = macb->rx_ring[next_rx_tail].ctrl;
  319. if (status & RXBUF_FRAME_START) {
  320. if (next_rx_tail != macb->rx_tail)
  321. reclaim_rx_buffers(macb, next_rx_tail);
  322. macb->wrapped = false;
  323. }
  324. if (status & RXBUF_FRAME_END) {
  325. buffer = macb->rx_buffer + 128 * macb->rx_tail;
  326. length = status & RXBUF_FRMLEN_MASK;
  327. macb_invalidate_rx_buffer(macb);
  328. if (macb->wrapped) {
  329. unsigned int headlen, taillen;
  330. headlen = 128 * (MACB_RX_RING_SIZE
  331. - macb->rx_tail);
  332. taillen = length - headlen;
  333. memcpy((void *)net_rx_packets[0],
  334. buffer, headlen);
  335. memcpy((void *)net_rx_packets[0] + headlen,
  336. macb->rx_buffer, taillen);
  337. *packetp = (void *)net_rx_packets[0];
  338. } else {
  339. *packetp = buffer;
  340. }
  341. if (++next_rx_tail >= MACB_RX_RING_SIZE)
  342. next_rx_tail = 0;
  343. macb->next_rx_tail = next_rx_tail;
  344. return length;
  345. } else {
  346. if (++next_rx_tail >= MACB_RX_RING_SIZE) {
  347. macb->wrapped = true;
  348. next_rx_tail = 0;
  349. }
  350. }
  351. barrier();
  352. }
  353. }
  354. static void macb_phy_reset(struct macb_device *macb, const char *name)
  355. {
  356. int i;
  357. u16 status, adv;
  358. adv = ADVERTISE_CSMA | ADVERTISE_ALL;
  359. macb_mdio_write(macb, MII_ADVERTISE, adv);
  360. printf("%s: Starting autonegotiation...\n", name);
  361. macb_mdio_write(macb, MII_BMCR, (BMCR_ANENABLE
  362. | BMCR_ANRESTART));
  363. for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
  364. status = macb_mdio_read(macb, MII_BMSR);
  365. if (status & BMSR_ANEGCOMPLETE)
  366. break;
  367. udelay(100);
  368. }
  369. if (status & BMSR_ANEGCOMPLETE)
  370. printf("%s: Autonegotiation complete\n", name);
  371. else
  372. printf("%s: Autonegotiation timed out (status=0x%04x)\n",
  373. name, status);
  374. }
  375. #ifdef CONFIG_MACB_SEARCH_PHY
  376. static int macb_phy_find(struct macb_device *macb, const char *name)
  377. {
  378. int i;
  379. u16 phy_id;
  380. /* Search for PHY... */
  381. for (i = 0; i < 32; i++) {
  382. macb->phy_addr = i;
  383. phy_id = macb_mdio_read(macb, MII_PHYSID1);
  384. if (phy_id != 0xffff) {
  385. printf("%s: PHY present at %d\n", name, i);
  386. return 1;
  387. }
  388. }
  389. /* PHY isn't up to snuff */
  390. printf("%s: PHY not found\n", name);
  391. return 0;
  392. }
  393. #endif /* CONFIG_MACB_SEARCH_PHY */
  394. #ifdef CONFIG_DM_ETH
  395. static int macb_phy_init(struct udevice *dev, const char *name)
  396. #else
  397. static int macb_phy_init(struct macb_device *macb, const char *name)
  398. #endif
  399. {
  400. #ifdef CONFIG_DM_ETH
  401. struct macb_device *macb = dev_get_priv(dev);
  402. #endif
  403. #ifdef CONFIG_PHYLIB
  404. struct phy_device *phydev;
  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. phydev = phy_connect(macb->bus, macb->phy_addr, dev,
  425. macb->phy_interface);
  426. #else
  427. /* need to consider other phy interface mode */
  428. phydev = phy_connect(macb->bus, macb->phy_addr, &macb->netdev,
  429. PHY_INTERFACE_MODE_RGMII);
  430. #endif
  431. if (!phydev) {
  432. printf("phy_connect failed\n");
  433. return -ENODEV;
  434. }
  435. phy_config(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. phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
  866. NULL);
  867. if (phy_mode)
  868. macb->phy_interface = phy_get_interface_by_name(phy_mode);
  869. if (macb->phy_interface == -1) {
  870. debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
  871. return -EINVAL;
  872. }
  873. macb->regs = (void *)pdata->iobase;
  874. #ifdef CONFIG_CLK
  875. int ret = macb_enable_clk(dev);
  876. if (ret)
  877. return ret;
  878. #endif
  879. _macb_eth_initialize(macb);
  880. #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
  881. int retval;
  882. struct mii_dev *mdiodev = mdio_alloc();
  883. if (!mdiodev)
  884. return -ENOMEM;
  885. strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
  886. mdiodev->read = macb_miiphy_read;
  887. mdiodev->write = macb_miiphy_write;
  888. retval = mdio_register(mdiodev);
  889. if (retval < 0)
  890. return retval;
  891. macb->bus = miiphy_get_dev_by_name(dev->name);
  892. #endif
  893. return 0;
  894. }
  895. static int macb_eth_ofdata_to_platdata(struct udevice *dev)
  896. {
  897. struct eth_pdata *pdata = dev_get_platdata(dev);
  898. pdata->iobase = dev_get_addr(dev);
  899. return 0;
  900. }
  901. static const struct udevice_id macb_eth_ids[] = {
  902. { .compatible = "cdns,macb" },
  903. { }
  904. };
  905. U_BOOT_DRIVER(eth_macb) = {
  906. .name = "eth_macb",
  907. .id = UCLASS_ETH,
  908. .of_match = macb_eth_ids,
  909. .ofdata_to_platdata = macb_eth_ofdata_to_platdata,
  910. .probe = macb_eth_probe,
  911. .ops = &macb_eth_ops,
  912. .priv_auto_alloc_size = sizeof(struct macb_device),
  913. .platdata_auto_alloc_size = sizeof(struct eth_pdata),
  914. };
  915. #endif
  916. #endif