macb.c 27 KB

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