macb.c 25 KB

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