macb.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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. static int macb_phy_find(struct macb_device *macb, const char *name)
  379. {
  380. int i;
  381. u16 phy_id;
  382. /* Search for PHY... */
  383. for (i = 0; i < 32; i++) {
  384. macb->phy_addr = i;
  385. phy_id = macb_mdio_read(macb, MII_PHYSID1);
  386. if (phy_id != 0xffff) {
  387. printf("%s: PHY present at %d\n", name, i);
  388. return 1;
  389. }
  390. }
  391. /* PHY isn't up to snuff */
  392. printf("%s: PHY not found\n", name);
  393. return 0;
  394. }
  395. #ifdef CONFIG_DM_ETH
  396. static int macb_phy_init(struct udevice *dev, const char *name)
  397. #else
  398. static int macb_phy_init(struct macb_device *macb, const char *name)
  399. #endif
  400. {
  401. #ifdef CONFIG_DM_ETH
  402. struct macb_device *macb = dev_get_priv(dev);
  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. /* Auto-detect phy_addr */
  410. if (!macb_phy_find(macb, name))
  411. return 0;
  412. /* Check if the PHY is up to snuff... */
  413. phy_id = macb_mdio_read(macb, MII_PHYSID1);
  414. if (phy_id == 0xffff) {
  415. printf("%s: No PHY present\n", name);
  416. return 0;
  417. }
  418. #ifdef CONFIG_PHYLIB
  419. #ifdef CONFIG_DM_ETH
  420. macb->phydev = phy_connect(macb->bus, macb->phy_addr, dev,
  421. macb->phy_interface);
  422. #else
  423. /* need to consider other phy interface mode */
  424. macb->phydev = phy_connect(macb->bus, macb->phy_addr, &macb->netdev,
  425. PHY_INTERFACE_MODE_RGMII);
  426. #endif
  427. if (!macb->phydev) {
  428. printf("phy_connect failed\n");
  429. return -ENODEV;
  430. }
  431. phy_config(macb->phydev);
  432. #endif
  433. status = macb_mdio_read(macb, MII_BMSR);
  434. if (!(status & BMSR_LSTATUS)) {
  435. /* Try to re-negotiate if we don't have link already. */
  436. macb_phy_reset(macb, name);
  437. for (i = 0; i < MACB_AUTONEG_TIMEOUT / 100; i++) {
  438. status = macb_mdio_read(macb, MII_BMSR);
  439. if (status & BMSR_LSTATUS)
  440. break;
  441. udelay(100);
  442. }
  443. }
  444. if (!(status & BMSR_LSTATUS)) {
  445. printf("%s: link down (status: 0x%04x)\n",
  446. name, status);
  447. return 0;
  448. }
  449. /* First check for GMAC and that it is GiB capable */
  450. if (gem_is_gigabit_capable(macb)) {
  451. lpa = macb_mdio_read(macb, MII_STAT1000);
  452. if (lpa & (LPA_1000FULL | LPA_1000HALF)) {
  453. duplex = ((lpa & LPA_1000FULL) ? 1 : 0);
  454. printf("%s: link up, 1000Mbps %s-duplex (lpa: 0x%04x)\n",
  455. name,
  456. duplex ? "full" : "half",
  457. lpa);
  458. ncfgr = macb_readl(macb, NCFGR);
  459. ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
  460. ncfgr |= GEM_BIT(GBE);
  461. if (duplex)
  462. ncfgr |= MACB_BIT(FD);
  463. macb_writel(macb, NCFGR, ncfgr);
  464. return 1;
  465. }
  466. }
  467. /* fall back for EMAC checking */
  468. adv = macb_mdio_read(macb, MII_ADVERTISE);
  469. lpa = macb_mdio_read(macb, MII_LPA);
  470. media = mii_nway_result(lpa & adv);
  471. speed = (media & (ADVERTISE_100FULL | ADVERTISE_100HALF)
  472. ? 1 : 0);
  473. duplex = (media & ADVERTISE_FULL) ? 1 : 0;
  474. printf("%s: link up, %sMbps %s-duplex (lpa: 0x%04x)\n",
  475. name,
  476. speed ? "100" : "10",
  477. duplex ? "full" : "half",
  478. lpa);
  479. ncfgr = macb_readl(macb, NCFGR);
  480. ncfgr &= ~(MACB_BIT(SPD) | MACB_BIT(FD) | GEM_BIT(GBE));
  481. if (speed)
  482. ncfgr |= MACB_BIT(SPD);
  483. if (duplex)
  484. ncfgr |= MACB_BIT(FD);
  485. macb_writel(macb, NCFGR, ncfgr);
  486. return 1;
  487. }
  488. static int gmac_init_multi_queues(struct macb_device *macb)
  489. {
  490. int i, num_queues = 1;
  491. u32 queue_mask;
  492. /* bit 0 is never set but queue 0 always exists */
  493. queue_mask = gem_readl(macb, DCFG6) & 0xff;
  494. queue_mask |= 0x1;
  495. for (i = 1; i < MACB_MAX_QUEUES; i++)
  496. if (queue_mask & (1 << i))
  497. num_queues++;
  498. macb->dummy_desc->ctrl = TXBUF_USED;
  499. macb->dummy_desc->addr = 0;
  500. flush_dcache_range(macb->dummy_desc_dma, macb->dummy_desc_dma +
  501. ALIGN(MACB_TX_DUMMY_DMA_DESC_SIZE, PKTALIGN));
  502. for (i = 1; i < num_queues; i++)
  503. gem_writel_queue_TBQP(macb, macb->dummy_desc_dma, i - 1);
  504. return 0;
  505. }
  506. #ifdef CONFIG_DM_ETH
  507. static int _macb_init(struct udevice *dev, const char *name)
  508. #else
  509. static int _macb_init(struct macb_device *macb, const char *name)
  510. #endif
  511. {
  512. #ifdef CONFIG_DM_ETH
  513. struct macb_device *macb = dev_get_priv(dev);
  514. #endif
  515. unsigned long paddr;
  516. int i;
  517. /*
  518. * macb_halt should have been called at some point before now,
  519. * so we'll assume the controller is idle.
  520. */
  521. /* initialize DMA descriptors */
  522. paddr = macb->rx_buffer_dma;
  523. for (i = 0; i < MACB_RX_RING_SIZE; i++) {
  524. if (i == (MACB_RX_RING_SIZE - 1))
  525. paddr |= RXADDR_WRAP;
  526. macb->rx_ring[i].addr = paddr;
  527. macb->rx_ring[i].ctrl = 0;
  528. paddr += 128;
  529. }
  530. macb_flush_ring_desc(macb, RX);
  531. macb_flush_rx_buffer(macb);
  532. for (i = 0; i < MACB_TX_RING_SIZE; i++) {
  533. macb->tx_ring[i].addr = 0;
  534. if (i == (MACB_TX_RING_SIZE - 1))
  535. macb->tx_ring[i].ctrl = TXBUF_USED | TXBUF_WRAP;
  536. else
  537. macb->tx_ring[i].ctrl = TXBUF_USED;
  538. }
  539. macb_flush_ring_desc(macb, TX);
  540. macb->rx_tail = 0;
  541. macb->tx_head = 0;
  542. macb->tx_tail = 0;
  543. macb->next_rx_tail = 0;
  544. macb_writel(macb, RBQP, macb->rx_ring_dma);
  545. macb_writel(macb, TBQP, macb->tx_ring_dma);
  546. if (macb_is_gem(macb)) {
  547. /* Check the multi queue and initialize the queue for tx */
  548. gmac_init_multi_queues(macb);
  549. /*
  550. * When the GMAC IP with GE feature, this bit is used to
  551. * select interface between RGMII and GMII.
  552. * When the GMAC IP without GE feature, this bit is used
  553. * to select interface between RMII and MII.
  554. */
  555. #ifdef CONFIG_DM_ETH
  556. if ((macb->phy_interface == PHY_INTERFACE_MODE_RMII) ||
  557. (macb->phy_interface == PHY_INTERFACE_MODE_RGMII))
  558. gem_writel(macb, UR, GEM_BIT(RGMII));
  559. else
  560. gem_writel(macb, UR, 0);
  561. #else
  562. #if defined(CONFIG_RGMII) || defined(CONFIG_RMII)
  563. gem_writel(macb, UR, GEM_BIT(RGMII));
  564. #else
  565. gem_writel(macb, UR, 0);
  566. #endif
  567. #endif
  568. } else {
  569. /* choose RMII or MII mode. This depends on the board */
  570. #ifdef CONFIG_DM_ETH
  571. #ifdef CONFIG_AT91FAMILY
  572. if (macb->phy_interface == PHY_INTERFACE_MODE_RMII) {
  573. macb_writel(macb, USRIO,
  574. MACB_BIT(RMII) | MACB_BIT(CLKEN));
  575. } else {
  576. macb_writel(macb, USRIO, MACB_BIT(CLKEN));
  577. }
  578. #else
  579. if (macb->phy_interface == PHY_INTERFACE_MODE_RMII)
  580. macb_writel(macb, USRIO, 0);
  581. else
  582. macb_writel(macb, USRIO, MACB_BIT(MII));
  583. #endif
  584. #else
  585. #ifdef CONFIG_RMII
  586. #ifdef CONFIG_AT91FAMILY
  587. macb_writel(macb, USRIO, MACB_BIT(RMII) | MACB_BIT(CLKEN));
  588. #else
  589. macb_writel(macb, USRIO, 0);
  590. #endif
  591. #else
  592. #ifdef CONFIG_AT91FAMILY
  593. macb_writel(macb, USRIO, MACB_BIT(CLKEN));
  594. #else
  595. macb_writel(macb, USRIO, MACB_BIT(MII));
  596. #endif
  597. #endif /* CONFIG_RMII */
  598. #endif
  599. }
  600. #ifdef CONFIG_DM_ETH
  601. if (!macb_phy_init(dev, name))
  602. #else
  603. if (!macb_phy_init(macb, name))
  604. #endif
  605. return -1;
  606. /* Enable TX and RX */
  607. macb_writel(macb, NCR, MACB_BIT(TE) | MACB_BIT(RE));
  608. return 0;
  609. }
  610. static void _macb_halt(struct macb_device *macb)
  611. {
  612. u32 ncr, tsr;
  613. /* Halt the controller and wait for any ongoing transmission to end. */
  614. ncr = macb_readl(macb, NCR);
  615. ncr |= MACB_BIT(THALT);
  616. macb_writel(macb, NCR, ncr);
  617. do {
  618. tsr = macb_readl(macb, TSR);
  619. } while (tsr & MACB_BIT(TGO));
  620. /* Disable TX and RX, and clear statistics */
  621. macb_writel(macb, NCR, MACB_BIT(CLRSTAT));
  622. }
  623. static int _macb_write_hwaddr(struct macb_device *macb, unsigned char *enetaddr)
  624. {
  625. u32 hwaddr_bottom;
  626. u16 hwaddr_top;
  627. /* set hardware address */
  628. hwaddr_bottom = enetaddr[0] | enetaddr[1] << 8 |
  629. enetaddr[2] << 16 | enetaddr[3] << 24;
  630. macb_writel(macb, SA1B, hwaddr_bottom);
  631. hwaddr_top = enetaddr[4] | enetaddr[5] << 8;
  632. macb_writel(macb, SA1T, hwaddr_top);
  633. return 0;
  634. }
  635. static u32 macb_mdc_clk_div(int id, struct macb_device *macb)
  636. {
  637. u32 config;
  638. #if defined(CONFIG_DM_ETH) && defined(CONFIG_CLK)
  639. unsigned long macb_hz = macb->pclk_rate;
  640. #else
  641. unsigned long macb_hz = get_macb_pclk_rate(id);
  642. #endif
  643. if (macb_hz < 20000000)
  644. config = MACB_BF(CLK, MACB_CLK_DIV8);
  645. else if (macb_hz < 40000000)
  646. config = MACB_BF(CLK, MACB_CLK_DIV16);
  647. else if (macb_hz < 80000000)
  648. config = MACB_BF(CLK, MACB_CLK_DIV32);
  649. else
  650. config = MACB_BF(CLK, MACB_CLK_DIV64);
  651. return config;
  652. }
  653. static u32 gem_mdc_clk_div(int id, struct macb_device *macb)
  654. {
  655. u32 config;
  656. #if defined(CONFIG_DM_ETH) && defined(CONFIG_CLK)
  657. unsigned long macb_hz = macb->pclk_rate;
  658. #else
  659. unsigned long macb_hz = get_macb_pclk_rate(id);
  660. #endif
  661. if (macb_hz < 20000000)
  662. config = GEM_BF(CLK, GEM_CLK_DIV8);
  663. else if (macb_hz < 40000000)
  664. config = GEM_BF(CLK, GEM_CLK_DIV16);
  665. else if (macb_hz < 80000000)
  666. config = GEM_BF(CLK, GEM_CLK_DIV32);
  667. else if (macb_hz < 120000000)
  668. config = GEM_BF(CLK, GEM_CLK_DIV48);
  669. else if (macb_hz < 160000000)
  670. config = GEM_BF(CLK, GEM_CLK_DIV64);
  671. else
  672. config = GEM_BF(CLK, GEM_CLK_DIV96);
  673. return config;
  674. }
  675. /*
  676. * Get the DMA bus width field of the network configuration register that we
  677. * should program. We find the width from decoding the design configuration
  678. * register to find the maximum supported data bus width.
  679. */
  680. static u32 macb_dbw(struct macb_device *macb)
  681. {
  682. switch (GEM_BFEXT(DBWDEF, gem_readl(macb, DCFG1))) {
  683. case 4:
  684. return GEM_BF(DBW, GEM_DBW128);
  685. case 2:
  686. return GEM_BF(DBW, GEM_DBW64);
  687. case 1:
  688. default:
  689. return GEM_BF(DBW, GEM_DBW32);
  690. }
  691. }
  692. static void _macb_eth_initialize(struct macb_device *macb)
  693. {
  694. int id = 0; /* This is not used by functions we call */
  695. u32 ncfgr;
  696. /* TODO: we need check the rx/tx_ring_dma is dcache line aligned */
  697. macb->rx_buffer = dma_alloc_coherent(MACB_RX_BUFFER_SIZE,
  698. &macb->rx_buffer_dma);
  699. macb->rx_ring = dma_alloc_coherent(MACB_RX_DMA_DESC_SIZE,
  700. &macb->rx_ring_dma);
  701. macb->tx_ring = dma_alloc_coherent(MACB_TX_DMA_DESC_SIZE,
  702. &macb->tx_ring_dma);
  703. macb->dummy_desc = dma_alloc_coherent(MACB_TX_DUMMY_DMA_DESC_SIZE,
  704. &macb->dummy_desc_dma);
  705. /*
  706. * Do some basic initialization so that we at least can talk
  707. * to the PHY
  708. */
  709. if (macb_is_gem(macb)) {
  710. ncfgr = gem_mdc_clk_div(id, macb);
  711. ncfgr |= macb_dbw(macb);
  712. } else {
  713. ncfgr = macb_mdc_clk_div(id, macb);
  714. }
  715. macb_writel(macb, NCFGR, ncfgr);
  716. }
  717. #ifndef CONFIG_DM_ETH
  718. static int macb_send(struct eth_device *netdev, void *packet, int length)
  719. {
  720. struct macb_device *macb = to_macb(netdev);
  721. return _macb_send(macb, netdev->name, packet, length);
  722. }
  723. static int macb_recv(struct eth_device *netdev)
  724. {
  725. struct macb_device *macb = to_macb(netdev);
  726. uchar *packet;
  727. int length;
  728. macb->wrapped = false;
  729. for (;;) {
  730. macb->next_rx_tail = macb->rx_tail;
  731. length = _macb_recv(macb, &packet);
  732. if (length >= 0) {
  733. net_process_received_packet(packet, length);
  734. reclaim_rx_buffers(macb, macb->next_rx_tail);
  735. } else if (length < 0) {
  736. return length;
  737. }
  738. }
  739. }
  740. static int macb_init(struct eth_device *netdev, bd_t *bd)
  741. {
  742. struct macb_device *macb = to_macb(netdev);
  743. return _macb_init(macb, netdev->name);
  744. }
  745. static void macb_halt(struct eth_device *netdev)
  746. {
  747. struct macb_device *macb = to_macb(netdev);
  748. return _macb_halt(macb);
  749. }
  750. static int macb_write_hwaddr(struct eth_device *netdev)
  751. {
  752. struct macb_device *macb = to_macb(netdev);
  753. return _macb_write_hwaddr(macb, netdev->enetaddr);
  754. }
  755. int macb_eth_initialize(int id, void *regs, unsigned int phy_addr)
  756. {
  757. struct macb_device *macb;
  758. struct eth_device *netdev;
  759. macb = malloc(sizeof(struct macb_device));
  760. if (!macb) {
  761. printf("Error: Failed to allocate memory for MACB%d\n", id);
  762. return -1;
  763. }
  764. memset(macb, 0, sizeof(struct macb_device));
  765. netdev = &macb->netdev;
  766. macb->regs = regs;
  767. macb->phy_addr = phy_addr;
  768. if (macb_is_gem(macb))
  769. sprintf(netdev->name, "gmac%d", id);
  770. else
  771. sprintf(netdev->name, "macb%d", id);
  772. netdev->init = macb_init;
  773. netdev->halt = macb_halt;
  774. netdev->send = macb_send;
  775. netdev->recv = macb_recv;
  776. netdev->write_hwaddr = macb_write_hwaddr;
  777. _macb_eth_initialize(macb);
  778. eth_register(netdev);
  779. #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
  780. int retval;
  781. struct mii_dev *mdiodev = mdio_alloc();
  782. if (!mdiodev)
  783. return -ENOMEM;
  784. strncpy(mdiodev->name, netdev->name, MDIO_NAME_LEN);
  785. mdiodev->read = macb_miiphy_read;
  786. mdiodev->write = macb_miiphy_write;
  787. retval = mdio_register(mdiodev);
  788. if (retval < 0)
  789. return retval;
  790. macb->bus = miiphy_get_dev_by_name(netdev->name);
  791. #endif
  792. return 0;
  793. }
  794. #endif /* !CONFIG_DM_ETH */
  795. #ifdef CONFIG_DM_ETH
  796. static int macb_start(struct udevice *dev)
  797. {
  798. return _macb_init(dev, dev->name);
  799. }
  800. static int macb_send(struct udevice *dev, void *packet, int length)
  801. {
  802. struct macb_device *macb = dev_get_priv(dev);
  803. return _macb_send(macb, dev->name, packet, length);
  804. }
  805. static int macb_recv(struct udevice *dev, int flags, uchar **packetp)
  806. {
  807. struct macb_device *macb = dev_get_priv(dev);
  808. macb->next_rx_tail = macb->rx_tail;
  809. macb->wrapped = false;
  810. return _macb_recv(macb, packetp);
  811. }
  812. static int macb_free_pkt(struct udevice *dev, uchar *packet, int length)
  813. {
  814. struct macb_device *macb = dev_get_priv(dev);
  815. reclaim_rx_buffers(macb, macb->next_rx_tail);
  816. return 0;
  817. }
  818. static void macb_stop(struct udevice *dev)
  819. {
  820. struct macb_device *macb = dev_get_priv(dev);
  821. _macb_halt(macb);
  822. }
  823. static int macb_write_hwaddr(struct udevice *dev)
  824. {
  825. struct eth_pdata *plat = dev_get_platdata(dev);
  826. struct macb_device *macb = dev_get_priv(dev);
  827. return _macb_write_hwaddr(macb, plat->enetaddr);
  828. }
  829. static const struct eth_ops macb_eth_ops = {
  830. .start = macb_start,
  831. .send = macb_send,
  832. .recv = macb_recv,
  833. .stop = macb_stop,
  834. .free_pkt = macb_free_pkt,
  835. .write_hwaddr = macb_write_hwaddr,
  836. };
  837. #ifdef CONFIG_CLK
  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. #endif
  857. static int macb_eth_probe(struct udevice *dev)
  858. {
  859. struct eth_pdata *pdata = dev_get_platdata(dev);
  860. struct macb_device *macb = dev_get_priv(dev);
  861. const char *phy_mode;
  862. __maybe_unused int ret;
  863. phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
  864. NULL);
  865. if (phy_mode)
  866. macb->phy_interface = phy_get_interface_by_name(phy_mode);
  867. if (macb->phy_interface == -1) {
  868. debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
  869. return -EINVAL;
  870. }
  871. macb->regs = (void *)pdata->iobase;
  872. #ifdef CONFIG_CLK
  873. ret = macb_enable_clk(dev);
  874. if (ret)
  875. return ret;
  876. #endif
  877. _macb_eth_initialize(macb);
  878. #if defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
  879. macb->bus = mdio_alloc();
  880. if (!macb->bus)
  881. return -ENOMEM;
  882. strncpy(macb->bus->name, dev->name, MDIO_NAME_LEN);
  883. macb->bus->read = macb_miiphy_read;
  884. macb->bus->write = macb_miiphy_write;
  885. ret = mdio_register(macb->bus);
  886. if (ret < 0)
  887. return ret;
  888. macb->bus = miiphy_get_dev_by_name(dev->name);
  889. #endif
  890. return 0;
  891. }
  892. static int macb_eth_remove(struct udevice *dev)
  893. {
  894. struct macb_device *macb = dev_get_priv(dev);
  895. #ifdef CONFIG_PHYLIB
  896. free(macb->phydev);
  897. #endif
  898. mdio_unregister(macb->bus);
  899. mdio_free(macb->bus);
  900. return 0;
  901. }
  902. static int macb_eth_ofdata_to_platdata(struct udevice *dev)
  903. {
  904. struct eth_pdata *pdata = dev_get_platdata(dev);
  905. pdata->iobase = devfdt_get_addr(dev);
  906. return 0;
  907. }
  908. static const struct udevice_id macb_eth_ids[] = {
  909. { .compatible = "cdns,macb" },
  910. { .compatible = "cdns,at91sam9260-macb" },
  911. { .compatible = "atmel,sama5d2-gem" },
  912. { .compatible = "atmel,sama5d3-gem" },
  913. { .compatible = "atmel,sama5d4-gem" },
  914. { }
  915. };
  916. U_BOOT_DRIVER(eth_macb) = {
  917. .name = "eth_macb",
  918. .id = UCLASS_ETH,
  919. .of_match = macb_eth_ids,
  920. .ofdata_to_platdata = macb_eth_ofdata_to_platdata,
  921. .probe = macb_eth_probe,
  922. .remove = macb_eth_remove,
  923. .ops = &macb_eth_ops,
  924. .priv_auto_alloc_size = sizeof(struct macb_device),
  925. .platdata_auto_alloc_size = sizeof(struct eth_pdata),
  926. };
  927. #endif
  928. #endif