designware.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. /*
  2. * (C) Copyright 2010
  3. * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /*
  8. * Designware ethernet IP driver for U-Boot
  9. */
  10. #include <common.h>
  11. #include <dm.h>
  12. #include <errno.h>
  13. #include <miiphy.h>
  14. #include <malloc.h>
  15. #include <pci.h>
  16. #include <linux/compiler.h>
  17. #include <linux/err.h>
  18. #include <asm/io.h>
  19. #include "designware.h"
  20. DECLARE_GLOBAL_DATA_PTR;
  21. static int dw_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
  22. {
  23. #ifdef CONFIG_DM_ETH
  24. struct dw_eth_dev *priv = dev_get_priv((struct udevice *)bus->priv);
  25. struct eth_mac_regs *mac_p = priv->mac_regs_p;
  26. #else
  27. struct eth_mac_regs *mac_p = bus->priv;
  28. #endif
  29. ulong start;
  30. u16 miiaddr;
  31. int timeout = CONFIG_MDIO_TIMEOUT;
  32. miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
  33. ((reg << MIIREGSHIFT) & MII_REGMSK);
  34. writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
  35. start = get_timer(0);
  36. while (get_timer(start) < timeout) {
  37. if (!(readl(&mac_p->miiaddr) & MII_BUSY))
  38. return readl(&mac_p->miidata);
  39. udelay(10);
  40. };
  41. return -ETIMEDOUT;
  42. }
  43. static int dw_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
  44. u16 val)
  45. {
  46. #ifdef CONFIG_DM_ETH
  47. struct dw_eth_dev *priv = dev_get_priv((struct udevice *)bus->priv);
  48. struct eth_mac_regs *mac_p = priv->mac_regs_p;
  49. #else
  50. struct eth_mac_regs *mac_p = bus->priv;
  51. #endif
  52. ulong start;
  53. u16 miiaddr;
  54. int ret = -ETIMEDOUT, timeout = CONFIG_MDIO_TIMEOUT;
  55. writel(val, &mac_p->miidata);
  56. miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
  57. ((reg << MIIREGSHIFT) & MII_REGMSK) | MII_WRITE;
  58. writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
  59. start = get_timer(0);
  60. while (get_timer(start) < timeout) {
  61. if (!(readl(&mac_p->miiaddr) & MII_BUSY)) {
  62. ret = 0;
  63. break;
  64. }
  65. udelay(10);
  66. };
  67. return ret;
  68. }
  69. #if defined(CONFIG_DM_ETH) && defined(CONFIG_DM_GPIO)
  70. static int dw_mdio_reset(struct mii_dev *bus)
  71. {
  72. struct udevice *dev = bus->priv;
  73. struct dw_eth_dev *priv = dev_get_priv(dev);
  74. struct dw_eth_pdata *pdata = dev_get_platdata(dev);
  75. int ret;
  76. if (!dm_gpio_is_valid(&priv->reset_gpio))
  77. return 0;
  78. /* reset the phy */
  79. ret = dm_gpio_set_value(&priv->reset_gpio, 0);
  80. if (ret)
  81. return ret;
  82. udelay(pdata->reset_delays[0]);
  83. ret = dm_gpio_set_value(&priv->reset_gpio, 1);
  84. if (ret)
  85. return ret;
  86. udelay(pdata->reset_delays[1]);
  87. ret = dm_gpio_set_value(&priv->reset_gpio, 0);
  88. if (ret)
  89. return ret;
  90. udelay(pdata->reset_delays[2]);
  91. return 0;
  92. }
  93. #endif
  94. static int dw_mdio_init(const char *name, void *priv)
  95. {
  96. struct mii_dev *bus = mdio_alloc();
  97. if (!bus) {
  98. printf("Failed to allocate MDIO bus\n");
  99. return -ENOMEM;
  100. }
  101. bus->read = dw_mdio_read;
  102. bus->write = dw_mdio_write;
  103. snprintf(bus->name, sizeof(bus->name), "%s", name);
  104. #if defined(CONFIG_DM_ETH) && defined(CONFIG_DM_GPIO)
  105. bus->reset = dw_mdio_reset;
  106. #endif
  107. bus->priv = priv;
  108. return mdio_register(bus);
  109. }
  110. static void tx_descs_init(struct dw_eth_dev *priv)
  111. {
  112. struct eth_dma_regs *dma_p = priv->dma_regs_p;
  113. struct dmamacdescr *desc_table_p = &priv->tx_mac_descrtable[0];
  114. char *txbuffs = &priv->txbuffs[0];
  115. struct dmamacdescr *desc_p;
  116. u32 idx;
  117. for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) {
  118. desc_p = &desc_table_p[idx];
  119. desc_p->dmamac_addr = (ulong)&txbuffs[idx * CONFIG_ETH_BUFSIZE];
  120. desc_p->dmamac_next = (ulong)&desc_table_p[idx + 1];
  121. #if defined(CONFIG_DW_ALTDESCRIPTOR)
  122. desc_p->txrx_status &= ~(DESC_TXSTS_TXINT | DESC_TXSTS_TXLAST |
  123. DESC_TXSTS_TXFIRST | DESC_TXSTS_TXCRCDIS |
  124. DESC_TXSTS_TXCHECKINSCTRL |
  125. DESC_TXSTS_TXRINGEND | DESC_TXSTS_TXPADDIS);
  126. desc_p->txrx_status |= DESC_TXSTS_TXCHAIN;
  127. desc_p->dmamac_cntl = 0;
  128. desc_p->txrx_status &= ~(DESC_TXSTS_MSK | DESC_TXSTS_OWNBYDMA);
  129. #else
  130. desc_p->dmamac_cntl = DESC_TXCTRL_TXCHAIN;
  131. desc_p->txrx_status = 0;
  132. #endif
  133. }
  134. /* Correcting the last pointer of the chain */
  135. desc_p->dmamac_next = (ulong)&desc_table_p[0];
  136. /* Flush all Tx buffer descriptors at once */
  137. flush_dcache_range((ulong)priv->tx_mac_descrtable,
  138. (ulong)priv->tx_mac_descrtable +
  139. sizeof(priv->tx_mac_descrtable));
  140. writel((ulong)&desc_table_p[0], &dma_p->txdesclistaddr);
  141. priv->tx_currdescnum = 0;
  142. }
  143. static void rx_descs_init(struct dw_eth_dev *priv)
  144. {
  145. struct eth_dma_regs *dma_p = priv->dma_regs_p;
  146. struct dmamacdescr *desc_table_p = &priv->rx_mac_descrtable[0];
  147. char *rxbuffs = &priv->rxbuffs[0];
  148. struct dmamacdescr *desc_p;
  149. u32 idx;
  150. /* Before passing buffers to GMAC we need to make sure zeros
  151. * written there right after "priv" structure allocation were
  152. * flushed into RAM.
  153. * Otherwise there's a chance to get some of them flushed in RAM when
  154. * GMAC is already pushing data to RAM via DMA. This way incoming from
  155. * GMAC data will be corrupted. */
  156. flush_dcache_range((ulong)rxbuffs, (ulong)rxbuffs + RX_TOTAL_BUFSIZE);
  157. for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
  158. desc_p = &desc_table_p[idx];
  159. desc_p->dmamac_addr = (ulong)&rxbuffs[idx * CONFIG_ETH_BUFSIZE];
  160. desc_p->dmamac_next = (ulong)&desc_table_p[idx + 1];
  161. desc_p->dmamac_cntl =
  162. (MAC_MAX_FRAME_SZ & DESC_RXCTRL_SIZE1MASK) |
  163. DESC_RXCTRL_RXCHAIN;
  164. desc_p->txrx_status = DESC_RXSTS_OWNBYDMA;
  165. }
  166. /* Correcting the last pointer of the chain */
  167. desc_p->dmamac_next = (ulong)&desc_table_p[0];
  168. /* Flush all Rx buffer descriptors at once */
  169. flush_dcache_range((ulong)priv->rx_mac_descrtable,
  170. (ulong)priv->rx_mac_descrtable +
  171. sizeof(priv->rx_mac_descrtable));
  172. writel((ulong)&desc_table_p[0], &dma_p->rxdesclistaddr);
  173. priv->rx_currdescnum = 0;
  174. }
  175. static int _dw_write_hwaddr(struct dw_eth_dev *priv, u8 *mac_id)
  176. {
  177. struct eth_mac_regs *mac_p = priv->mac_regs_p;
  178. u32 macid_lo, macid_hi;
  179. macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
  180. (mac_id[3] << 24);
  181. macid_hi = mac_id[4] + (mac_id[5] << 8);
  182. writel(macid_hi, &mac_p->macaddr0hi);
  183. writel(macid_lo, &mac_p->macaddr0lo);
  184. return 0;
  185. }
  186. static int dw_adjust_link(struct dw_eth_dev *priv, struct eth_mac_regs *mac_p,
  187. struct phy_device *phydev)
  188. {
  189. u32 conf = readl(&mac_p->conf) | FRAMEBURSTENABLE | DISABLERXOWN;
  190. if (!phydev->link) {
  191. printf("%s: No link.\n", phydev->dev->name);
  192. return 0;
  193. }
  194. if (phydev->speed != 1000)
  195. conf |= MII_PORTSELECT;
  196. else
  197. conf &= ~MII_PORTSELECT;
  198. if (phydev->speed == 100)
  199. conf |= FES_100;
  200. if (phydev->duplex)
  201. conf |= FULLDPLXMODE;
  202. writel(conf, &mac_p->conf);
  203. printf("Speed: %d, %s duplex%s\n", phydev->speed,
  204. (phydev->duplex) ? "full" : "half",
  205. (phydev->port == PORT_FIBRE) ? ", fiber mode" : "");
  206. return 0;
  207. }
  208. static void _dw_eth_halt(struct dw_eth_dev *priv)
  209. {
  210. struct eth_mac_regs *mac_p = priv->mac_regs_p;
  211. struct eth_dma_regs *dma_p = priv->dma_regs_p;
  212. writel(readl(&mac_p->conf) & ~(RXENABLE | TXENABLE), &mac_p->conf);
  213. writel(readl(&dma_p->opmode) & ~(RXSTART | TXSTART), &dma_p->opmode);
  214. phy_shutdown(priv->phydev);
  215. }
  216. static int _dw_eth_init(struct dw_eth_dev *priv, u8 *enetaddr)
  217. {
  218. struct eth_mac_regs *mac_p = priv->mac_regs_p;
  219. struct eth_dma_regs *dma_p = priv->dma_regs_p;
  220. unsigned int start;
  221. int ret;
  222. writel(readl(&dma_p->busmode) | DMAMAC_SRST, &dma_p->busmode);
  223. start = get_timer(0);
  224. while (readl(&dma_p->busmode) & DMAMAC_SRST) {
  225. if (get_timer(start) >= CONFIG_MACRESET_TIMEOUT) {
  226. printf("DMA reset timeout\n");
  227. return -ETIMEDOUT;
  228. }
  229. mdelay(100);
  230. };
  231. /*
  232. * Soft reset above clears HW address registers.
  233. * So we have to set it here once again.
  234. */
  235. _dw_write_hwaddr(priv, enetaddr);
  236. rx_descs_init(priv);
  237. tx_descs_init(priv);
  238. writel(FIXEDBURST | PRIORXTX_41 | DMA_PBL, &dma_p->busmode);
  239. #ifndef CONFIG_DW_MAC_FORCE_THRESHOLD_MODE
  240. writel(readl(&dma_p->opmode) | FLUSHTXFIFO | STOREFORWARD,
  241. &dma_p->opmode);
  242. #else
  243. writel(readl(&dma_p->opmode) | FLUSHTXFIFO,
  244. &dma_p->opmode);
  245. #endif
  246. writel(readl(&dma_p->opmode) | RXSTART | TXSTART, &dma_p->opmode);
  247. #ifdef CONFIG_DW_AXI_BURST_LEN
  248. writel((CONFIG_DW_AXI_BURST_LEN & 0x1FF >> 1), &dma_p->axibus);
  249. #endif
  250. /* Start up the PHY */
  251. ret = phy_startup(priv->phydev);
  252. if (ret) {
  253. printf("Could not initialize PHY %s\n",
  254. priv->phydev->dev->name);
  255. return ret;
  256. }
  257. ret = dw_adjust_link(priv, mac_p, priv->phydev);
  258. if (ret)
  259. return ret;
  260. if (!priv->phydev->link)
  261. return -EIO;
  262. writel(readl(&mac_p->conf) | RXENABLE | TXENABLE, &mac_p->conf);
  263. return 0;
  264. }
  265. static int _dw_eth_send(struct dw_eth_dev *priv, void *packet, int length)
  266. {
  267. struct eth_dma_regs *dma_p = priv->dma_regs_p;
  268. u32 desc_num = priv->tx_currdescnum;
  269. struct dmamacdescr *desc_p = &priv->tx_mac_descrtable[desc_num];
  270. ulong desc_start = (ulong)desc_p;
  271. ulong desc_end = desc_start +
  272. roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
  273. ulong data_start = desc_p->dmamac_addr;
  274. ulong data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
  275. /*
  276. * Strictly we only need to invalidate the "txrx_status" field
  277. * for the following check, but on some platforms we cannot
  278. * invalidate only 4 bytes, so we flush the entire descriptor,
  279. * which is 16 bytes in total. This is safe because the
  280. * individual descriptors in the array are each aligned to
  281. * ARCH_DMA_MINALIGN and padded appropriately.
  282. */
  283. invalidate_dcache_range(desc_start, desc_end);
  284. /* Check if the descriptor is owned by CPU */
  285. if (desc_p->txrx_status & DESC_TXSTS_OWNBYDMA) {
  286. printf("CPU not owner of tx frame\n");
  287. return -EPERM;
  288. }
  289. memcpy((void *)data_start, packet, length);
  290. /* Flush data to be sent */
  291. flush_dcache_range(data_start, data_end);
  292. #if defined(CONFIG_DW_ALTDESCRIPTOR)
  293. desc_p->txrx_status |= DESC_TXSTS_TXFIRST | DESC_TXSTS_TXLAST;
  294. desc_p->dmamac_cntl |= (length << DESC_TXCTRL_SIZE1SHFT) &
  295. DESC_TXCTRL_SIZE1MASK;
  296. desc_p->txrx_status &= ~(DESC_TXSTS_MSK);
  297. desc_p->txrx_status |= DESC_TXSTS_OWNBYDMA;
  298. #else
  299. desc_p->dmamac_cntl |= ((length << DESC_TXCTRL_SIZE1SHFT) &
  300. DESC_TXCTRL_SIZE1MASK) | DESC_TXCTRL_TXLAST |
  301. DESC_TXCTRL_TXFIRST;
  302. desc_p->txrx_status = DESC_TXSTS_OWNBYDMA;
  303. #endif
  304. /* Flush modified buffer descriptor */
  305. flush_dcache_range(desc_start, desc_end);
  306. /* Test the wrap-around condition. */
  307. if (++desc_num >= CONFIG_TX_DESCR_NUM)
  308. desc_num = 0;
  309. priv->tx_currdescnum = desc_num;
  310. /* Start the transmission */
  311. writel(POLL_DATA, &dma_p->txpolldemand);
  312. return 0;
  313. }
  314. static int _dw_eth_recv(struct dw_eth_dev *priv, uchar **packetp)
  315. {
  316. u32 status, desc_num = priv->rx_currdescnum;
  317. struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
  318. int length = -EAGAIN;
  319. ulong desc_start = (ulong)desc_p;
  320. ulong desc_end = desc_start +
  321. roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
  322. ulong data_start = desc_p->dmamac_addr;
  323. ulong data_end;
  324. /* Invalidate entire buffer descriptor */
  325. invalidate_dcache_range(desc_start, desc_end);
  326. status = desc_p->txrx_status;
  327. /* Check if the owner is the CPU */
  328. if (!(status & DESC_RXSTS_OWNBYDMA)) {
  329. length = (status & DESC_RXSTS_FRMLENMSK) >>
  330. DESC_RXSTS_FRMLENSHFT;
  331. /* Invalidate received data */
  332. data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
  333. invalidate_dcache_range(data_start, data_end);
  334. *packetp = (uchar *)(ulong)desc_p->dmamac_addr;
  335. }
  336. return length;
  337. }
  338. static int _dw_free_pkt(struct dw_eth_dev *priv)
  339. {
  340. u32 desc_num = priv->rx_currdescnum;
  341. struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
  342. ulong desc_start = (ulong)desc_p;
  343. ulong desc_end = desc_start +
  344. roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
  345. /*
  346. * Make the current descriptor valid again and go to
  347. * the next one
  348. */
  349. desc_p->txrx_status |= DESC_RXSTS_OWNBYDMA;
  350. /* Flush only status field - others weren't changed */
  351. flush_dcache_range(desc_start, desc_end);
  352. /* Test the wrap-around condition. */
  353. if (++desc_num >= CONFIG_RX_DESCR_NUM)
  354. desc_num = 0;
  355. priv->rx_currdescnum = desc_num;
  356. return 0;
  357. }
  358. static int dw_phy_init(struct dw_eth_dev *priv, void *dev)
  359. {
  360. struct phy_device *phydev;
  361. int mask = 0xffffffff, ret;
  362. #ifdef CONFIG_PHY_ADDR
  363. mask = 1 << CONFIG_PHY_ADDR;
  364. #endif
  365. phydev = phy_find_by_mask(priv->bus, mask, priv->interface);
  366. if (!phydev)
  367. return -ENODEV;
  368. phy_connect_dev(phydev, dev);
  369. phydev->supported &= PHY_GBIT_FEATURES;
  370. if (priv->max_speed) {
  371. ret = phy_set_supported(phydev, priv->max_speed);
  372. if (ret)
  373. return ret;
  374. }
  375. phydev->advertising = phydev->supported;
  376. priv->phydev = phydev;
  377. phy_config(phydev);
  378. return 0;
  379. }
  380. #ifndef CONFIG_DM_ETH
  381. static int dw_eth_init(struct eth_device *dev, bd_t *bis)
  382. {
  383. return _dw_eth_init(dev->priv, dev->enetaddr);
  384. }
  385. static int dw_eth_send(struct eth_device *dev, void *packet, int length)
  386. {
  387. return _dw_eth_send(dev->priv, packet, length);
  388. }
  389. static int dw_eth_recv(struct eth_device *dev)
  390. {
  391. uchar *packet;
  392. int length;
  393. length = _dw_eth_recv(dev->priv, &packet);
  394. if (length == -EAGAIN)
  395. return 0;
  396. net_process_received_packet(packet, length);
  397. _dw_free_pkt(dev->priv);
  398. return 0;
  399. }
  400. static void dw_eth_halt(struct eth_device *dev)
  401. {
  402. return _dw_eth_halt(dev->priv);
  403. }
  404. static int dw_write_hwaddr(struct eth_device *dev)
  405. {
  406. return _dw_write_hwaddr(dev->priv, dev->enetaddr);
  407. }
  408. int designware_initialize(ulong base_addr, u32 interface)
  409. {
  410. struct eth_device *dev;
  411. struct dw_eth_dev *priv;
  412. dev = (struct eth_device *) malloc(sizeof(struct eth_device));
  413. if (!dev)
  414. return -ENOMEM;
  415. /*
  416. * Since the priv structure contains the descriptors which need a strict
  417. * buswidth alignment, memalign is used to allocate memory
  418. */
  419. priv = (struct dw_eth_dev *) memalign(ARCH_DMA_MINALIGN,
  420. sizeof(struct dw_eth_dev));
  421. if (!priv) {
  422. free(dev);
  423. return -ENOMEM;
  424. }
  425. if ((phys_addr_t)priv + sizeof(*priv) > (1ULL << 32)) {
  426. printf("designware: buffers are outside DMA memory\n");
  427. return -EINVAL;
  428. }
  429. memset(dev, 0, sizeof(struct eth_device));
  430. memset(priv, 0, sizeof(struct dw_eth_dev));
  431. sprintf(dev->name, "dwmac.%lx", base_addr);
  432. dev->iobase = (int)base_addr;
  433. dev->priv = priv;
  434. priv->dev = dev;
  435. priv->mac_regs_p = (struct eth_mac_regs *)base_addr;
  436. priv->dma_regs_p = (struct eth_dma_regs *)(base_addr +
  437. DW_DMA_BASE_OFFSET);
  438. dev->init = dw_eth_init;
  439. dev->send = dw_eth_send;
  440. dev->recv = dw_eth_recv;
  441. dev->halt = dw_eth_halt;
  442. dev->write_hwaddr = dw_write_hwaddr;
  443. eth_register(dev);
  444. priv->interface = interface;
  445. dw_mdio_init(dev->name, priv->mac_regs_p);
  446. priv->bus = miiphy_get_dev_by_name(dev->name);
  447. return dw_phy_init(priv, dev);
  448. }
  449. #endif
  450. #ifdef CONFIG_DM_ETH
  451. static int designware_eth_start(struct udevice *dev)
  452. {
  453. struct eth_pdata *pdata = dev_get_platdata(dev);
  454. return _dw_eth_init(dev->priv, pdata->enetaddr);
  455. }
  456. static int designware_eth_send(struct udevice *dev, void *packet, int length)
  457. {
  458. struct dw_eth_dev *priv = dev_get_priv(dev);
  459. return _dw_eth_send(priv, packet, length);
  460. }
  461. static int designware_eth_recv(struct udevice *dev, int flags, uchar **packetp)
  462. {
  463. struct dw_eth_dev *priv = dev_get_priv(dev);
  464. return _dw_eth_recv(priv, packetp);
  465. }
  466. static int designware_eth_free_pkt(struct udevice *dev, uchar *packet,
  467. int length)
  468. {
  469. struct dw_eth_dev *priv = dev_get_priv(dev);
  470. return _dw_free_pkt(priv);
  471. }
  472. static void designware_eth_stop(struct udevice *dev)
  473. {
  474. struct dw_eth_dev *priv = dev_get_priv(dev);
  475. return _dw_eth_halt(priv);
  476. }
  477. static int designware_eth_write_hwaddr(struct udevice *dev)
  478. {
  479. struct eth_pdata *pdata = dev_get_platdata(dev);
  480. struct dw_eth_dev *priv = dev_get_priv(dev);
  481. return _dw_write_hwaddr(priv, pdata->enetaddr);
  482. }
  483. static int designware_eth_bind(struct udevice *dev)
  484. {
  485. #ifdef CONFIG_DM_PCI
  486. static int num_cards;
  487. char name[20];
  488. /* Create a unique device name for PCI type devices */
  489. if (device_is_on_pci_bus(dev)) {
  490. sprintf(name, "eth_designware#%u", num_cards++);
  491. device_set_name(dev, name);
  492. }
  493. #endif
  494. return 0;
  495. }
  496. int designware_eth_probe(struct udevice *dev)
  497. {
  498. struct eth_pdata *pdata = dev_get_platdata(dev);
  499. struct dw_eth_dev *priv = dev_get_priv(dev);
  500. u32 iobase = pdata->iobase;
  501. ulong ioaddr;
  502. int ret;
  503. #ifdef CONFIG_DM_PCI
  504. /*
  505. * If we are on PCI bus, either directly attached to a PCI root port,
  506. * or via a PCI bridge, fill in platdata before we probe the hardware.
  507. */
  508. if (device_is_on_pci_bus(dev)) {
  509. dm_pci_read_config32(dev, PCI_BASE_ADDRESS_0, &iobase);
  510. iobase &= PCI_BASE_ADDRESS_MEM_MASK;
  511. iobase = dm_pci_mem_to_phys(dev, iobase);
  512. pdata->iobase = iobase;
  513. pdata->phy_interface = PHY_INTERFACE_MODE_RMII;
  514. }
  515. #endif
  516. debug("%s, iobase=%x, priv=%p\n", __func__, iobase, priv);
  517. ioaddr = iobase;
  518. priv->mac_regs_p = (struct eth_mac_regs *)ioaddr;
  519. priv->dma_regs_p = (struct eth_dma_regs *)(ioaddr + DW_DMA_BASE_OFFSET);
  520. priv->interface = pdata->phy_interface;
  521. priv->max_speed = pdata->max_speed;
  522. dw_mdio_init(dev->name, dev);
  523. priv->bus = miiphy_get_dev_by_name(dev->name);
  524. ret = dw_phy_init(priv, dev);
  525. debug("%s, ret=%d\n", __func__, ret);
  526. return ret;
  527. }
  528. static int designware_eth_remove(struct udevice *dev)
  529. {
  530. struct dw_eth_dev *priv = dev_get_priv(dev);
  531. free(priv->phydev);
  532. mdio_unregister(priv->bus);
  533. mdio_free(priv->bus);
  534. return 0;
  535. }
  536. const struct eth_ops designware_eth_ops = {
  537. .start = designware_eth_start,
  538. .send = designware_eth_send,
  539. .recv = designware_eth_recv,
  540. .free_pkt = designware_eth_free_pkt,
  541. .stop = designware_eth_stop,
  542. .write_hwaddr = designware_eth_write_hwaddr,
  543. };
  544. int designware_eth_ofdata_to_platdata(struct udevice *dev)
  545. {
  546. struct dw_eth_pdata *dw_pdata = dev_get_platdata(dev);
  547. #ifdef CONFIG_DM_GPIO
  548. struct dw_eth_dev *priv = dev_get_priv(dev);
  549. #endif
  550. struct eth_pdata *pdata = &dw_pdata->eth_pdata;
  551. const char *phy_mode;
  552. const fdt32_t *cell;
  553. #ifdef CONFIG_DM_GPIO
  554. int reset_flags = GPIOD_IS_OUT;
  555. #endif
  556. int ret = 0;
  557. pdata->iobase = dev_get_addr(dev);
  558. pdata->phy_interface = -1;
  559. phy_mode = fdt_getprop(gd->fdt_blob, dev->of_offset, "phy-mode", NULL);
  560. if (phy_mode)
  561. pdata->phy_interface = phy_get_interface_by_name(phy_mode);
  562. if (pdata->phy_interface == -1) {
  563. debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
  564. return -EINVAL;
  565. }
  566. pdata->max_speed = 0;
  567. cell = fdt_getprop(gd->fdt_blob, dev->of_offset, "max-speed", NULL);
  568. if (cell)
  569. pdata->max_speed = fdt32_to_cpu(*cell);
  570. #ifdef CONFIG_DM_GPIO
  571. if (fdtdec_get_bool(gd->fdt_blob, dev->of_offset,
  572. "snps,reset-active-low"))
  573. reset_flags |= GPIOD_ACTIVE_LOW;
  574. ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
  575. &priv->reset_gpio, reset_flags);
  576. if (ret == 0) {
  577. ret = fdtdec_get_int_array(gd->fdt_blob, dev->of_offset,
  578. "snps,reset-delays-us", dw_pdata->reset_delays, 3);
  579. } else if (ret == -ENOENT) {
  580. ret = 0;
  581. }
  582. #endif
  583. return ret;
  584. }
  585. static const struct udevice_id designware_eth_ids[] = {
  586. { .compatible = "allwinner,sun7i-a20-gmac" },
  587. { .compatible = "altr,socfpga-stmmac" },
  588. { .compatible = "amlogic,meson6-dwmac" },
  589. { }
  590. };
  591. U_BOOT_DRIVER(eth_designware) = {
  592. .name = "eth_designware",
  593. .id = UCLASS_ETH,
  594. .of_match = designware_eth_ids,
  595. .ofdata_to_platdata = designware_eth_ofdata_to_platdata,
  596. .bind = designware_eth_bind,
  597. .probe = designware_eth_probe,
  598. .remove = designware_eth_remove,
  599. .ops = &designware_eth_ops,
  600. .priv_auto_alloc_size = sizeof(struct dw_eth_dev),
  601. .platdata_auto_alloc_size = sizeof(struct dw_eth_pdata),
  602. .flags = DM_FLAG_ALLOC_PRIV_DMA,
  603. };
  604. static struct pci_device_id supported[] = {
  605. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_QRK_EMAC) },
  606. { }
  607. };
  608. U_BOOT_PCI_DEVICE(eth_designware, supported);
  609. #endif