designware.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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 void dw_adjust_link(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;
  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. }
  207. static void _dw_eth_halt(struct dw_eth_dev *priv)
  208. {
  209. struct eth_mac_regs *mac_p = priv->mac_regs_p;
  210. struct eth_dma_regs *dma_p = priv->dma_regs_p;
  211. writel(readl(&mac_p->conf) & ~(RXENABLE | TXENABLE), &mac_p->conf);
  212. writel(readl(&dma_p->opmode) & ~(RXSTART | TXSTART), &dma_p->opmode);
  213. phy_shutdown(priv->phydev);
  214. }
  215. static int _dw_eth_init(struct dw_eth_dev *priv, u8 *enetaddr)
  216. {
  217. struct eth_mac_regs *mac_p = priv->mac_regs_p;
  218. struct eth_dma_regs *dma_p = priv->dma_regs_p;
  219. unsigned int start;
  220. int ret;
  221. writel(readl(&dma_p->busmode) | DMAMAC_SRST, &dma_p->busmode);
  222. start = get_timer(0);
  223. while (readl(&dma_p->busmode) & DMAMAC_SRST) {
  224. if (get_timer(start) >= CONFIG_MACRESET_TIMEOUT) {
  225. printf("DMA reset timeout\n");
  226. return -ETIMEDOUT;
  227. }
  228. mdelay(100);
  229. };
  230. /*
  231. * Soft reset above clears HW address registers.
  232. * So we have to set it here once again.
  233. */
  234. _dw_write_hwaddr(priv, enetaddr);
  235. rx_descs_init(priv);
  236. tx_descs_init(priv);
  237. writel(FIXEDBURST | PRIORXTX_41 | DMA_PBL, &dma_p->busmode);
  238. #ifndef CONFIG_DW_MAC_FORCE_THRESHOLD_MODE
  239. writel(readl(&dma_p->opmode) | FLUSHTXFIFO | STOREFORWARD,
  240. &dma_p->opmode);
  241. #else
  242. writel(readl(&dma_p->opmode) | FLUSHTXFIFO,
  243. &dma_p->opmode);
  244. #endif
  245. writel(readl(&dma_p->opmode) | RXSTART | TXSTART, &dma_p->opmode);
  246. #ifdef CONFIG_DW_AXI_BURST_LEN
  247. writel((CONFIG_DW_AXI_BURST_LEN & 0x1FF >> 1), &dma_p->axibus);
  248. #endif
  249. /* Start up the PHY */
  250. ret = phy_startup(priv->phydev);
  251. if (ret) {
  252. printf("Could not initialize PHY %s\n",
  253. priv->phydev->dev->name);
  254. return ret;
  255. }
  256. dw_adjust_link(mac_p, priv->phydev);
  257. if (!priv->phydev->link)
  258. return -EIO;
  259. writel(readl(&mac_p->conf) | RXENABLE | TXENABLE, &mac_p->conf);
  260. return 0;
  261. }
  262. static int _dw_eth_send(struct dw_eth_dev *priv, void *packet, int length)
  263. {
  264. struct eth_dma_regs *dma_p = priv->dma_regs_p;
  265. u32 desc_num = priv->tx_currdescnum;
  266. struct dmamacdescr *desc_p = &priv->tx_mac_descrtable[desc_num];
  267. ulong desc_start = (ulong)desc_p;
  268. ulong desc_end = desc_start +
  269. roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
  270. ulong data_start = desc_p->dmamac_addr;
  271. ulong data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
  272. /*
  273. * Strictly we only need to invalidate the "txrx_status" field
  274. * for the following check, but on some platforms we cannot
  275. * invalidate only 4 bytes, so we flush the entire descriptor,
  276. * which is 16 bytes in total. This is safe because the
  277. * individual descriptors in the array are each aligned to
  278. * ARCH_DMA_MINALIGN and padded appropriately.
  279. */
  280. invalidate_dcache_range(desc_start, desc_end);
  281. /* Check if the descriptor is owned by CPU */
  282. if (desc_p->txrx_status & DESC_TXSTS_OWNBYDMA) {
  283. printf("CPU not owner of tx frame\n");
  284. return -EPERM;
  285. }
  286. memcpy((void *)data_start, packet, length);
  287. /* Flush data to be sent */
  288. flush_dcache_range(data_start, data_end);
  289. #if defined(CONFIG_DW_ALTDESCRIPTOR)
  290. desc_p->txrx_status |= DESC_TXSTS_TXFIRST | DESC_TXSTS_TXLAST;
  291. desc_p->dmamac_cntl |= (length << DESC_TXCTRL_SIZE1SHFT) &
  292. DESC_TXCTRL_SIZE1MASK;
  293. desc_p->txrx_status &= ~(DESC_TXSTS_MSK);
  294. desc_p->txrx_status |= DESC_TXSTS_OWNBYDMA;
  295. #else
  296. desc_p->dmamac_cntl |= ((length << DESC_TXCTRL_SIZE1SHFT) &
  297. DESC_TXCTRL_SIZE1MASK) | DESC_TXCTRL_TXLAST |
  298. DESC_TXCTRL_TXFIRST;
  299. desc_p->txrx_status = DESC_TXSTS_OWNBYDMA;
  300. #endif
  301. /* Flush modified buffer descriptor */
  302. flush_dcache_range(desc_start, desc_end);
  303. /* Test the wrap-around condition. */
  304. if (++desc_num >= CONFIG_TX_DESCR_NUM)
  305. desc_num = 0;
  306. priv->tx_currdescnum = desc_num;
  307. /* Start the transmission */
  308. writel(POLL_DATA, &dma_p->txpolldemand);
  309. return 0;
  310. }
  311. static int _dw_eth_recv(struct dw_eth_dev *priv, uchar **packetp)
  312. {
  313. u32 status, desc_num = priv->rx_currdescnum;
  314. struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
  315. int length = -EAGAIN;
  316. ulong desc_start = (ulong)desc_p;
  317. ulong desc_end = desc_start +
  318. roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
  319. ulong data_start = desc_p->dmamac_addr;
  320. ulong data_end;
  321. /* Invalidate entire buffer descriptor */
  322. invalidate_dcache_range(desc_start, desc_end);
  323. status = desc_p->txrx_status;
  324. /* Check if the owner is the CPU */
  325. if (!(status & DESC_RXSTS_OWNBYDMA)) {
  326. length = (status & DESC_RXSTS_FRMLENMSK) >>
  327. DESC_RXSTS_FRMLENSHFT;
  328. /* Invalidate received data */
  329. data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
  330. invalidate_dcache_range(data_start, data_end);
  331. *packetp = (uchar *)(ulong)desc_p->dmamac_addr;
  332. }
  333. return length;
  334. }
  335. static int _dw_free_pkt(struct dw_eth_dev *priv)
  336. {
  337. u32 desc_num = priv->rx_currdescnum;
  338. struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
  339. ulong desc_start = (ulong)desc_p;
  340. ulong desc_end = desc_start +
  341. roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
  342. /*
  343. * Make the current descriptor valid again and go to
  344. * the next one
  345. */
  346. desc_p->txrx_status |= DESC_RXSTS_OWNBYDMA;
  347. /* Flush only status field - others weren't changed */
  348. flush_dcache_range(desc_start, desc_end);
  349. /* Test the wrap-around condition. */
  350. if (++desc_num >= CONFIG_RX_DESCR_NUM)
  351. desc_num = 0;
  352. priv->rx_currdescnum = desc_num;
  353. return 0;
  354. }
  355. static int dw_phy_init(struct dw_eth_dev *priv, void *dev)
  356. {
  357. struct phy_device *phydev;
  358. int mask = 0xffffffff, ret;
  359. #ifdef CONFIG_PHY_ADDR
  360. mask = 1 << CONFIG_PHY_ADDR;
  361. #endif
  362. phydev = phy_find_by_mask(priv->bus, mask, priv->interface);
  363. if (!phydev)
  364. return -ENODEV;
  365. phy_connect_dev(phydev, dev);
  366. phydev->supported &= PHY_GBIT_FEATURES;
  367. if (priv->max_speed) {
  368. ret = phy_set_supported(phydev, priv->max_speed);
  369. if (ret)
  370. return ret;
  371. }
  372. phydev->advertising = phydev->supported;
  373. priv->phydev = phydev;
  374. phy_config(phydev);
  375. return 0;
  376. }
  377. #ifndef CONFIG_DM_ETH
  378. static int dw_eth_init(struct eth_device *dev, bd_t *bis)
  379. {
  380. return _dw_eth_init(dev->priv, dev->enetaddr);
  381. }
  382. static int dw_eth_send(struct eth_device *dev, void *packet, int length)
  383. {
  384. return _dw_eth_send(dev->priv, packet, length);
  385. }
  386. static int dw_eth_recv(struct eth_device *dev)
  387. {
  388. uchar *packet;
  389. int length;
  390. length = _dw_eth_recv(dev->priv, &packet);
  391. if (length == -EAGAIN)
  392. return 0;
  393. net_process_received_packet(packet, length);
  394. _dw_free_pkt(dev->priv);
  395. return 0;
  396. }
  397. static void dw_eth_halt(struct eth_device *dev)
  398. {
  399. return _dw_eth_halt(dev->priv);
  400. }
  401. static int dw_write_hwaddr(struct eth_device *dev)
  402. {
  403. return _dw_write_hwaddr(dev->priv, dev->enetaddr);
  404. }
  405. int designware_initialize(ulong base_addr, u32 interface)
  406. {
  407. struct eth_device *dev;
  408. struct dw_eth_dev *priv;
  409. dev = (struct eth_device *) malloc(sizeof(struct eth_device));
  410. if (!dev)
  411. return -ENOMEM;
  412. /*
  413. * Since the priv structure contains the descriptors which need a strict
  414. * buswidth alignment, memalign is used to allocate memory
  415. */
  416. priv = (struct dw_eth_dev *) memalign(ARCH_DMA_MINALIGN,
  417. sizeof(struct dw_eth_dev));
  418. if (!priv) {
  419. free(dev);
  420. return -ENOMEM;
  421. }
  422. if ((phys_addr_t)priv + sizeof(*priv) > (1ULL << 32)) {
  423. printf("designware: buffers are outside DMA memory\n");
  424. return -EINVAL;
  425. }
  426. memset(dev, 0, sizeof(struct eth_device));
  427. memset(priv, 0, sizeof(struct dw_eth_dev));
  428. sprintf(dev->name, "dwmac.%lx", base_addr);
  429. dev->iobase = (int)base_addr;
  430. dev->priv = priv;
  431. priv->dev = dev;
  432. priv->mac_regs_p = (struct eth_mac_regs *)base_addr;
  433. priv->dma_regs_p = (struct eth_dma_regs *)(base_addr +
  434. DW_DMA_BASE_OFFSET);
  435. dev->init = dw_eth_init;
  436. dev->send = dw_eth_send;
  437. dev->recv = dw_eth_recv;
  438. dev->halt = dw_eth_halt;
  439. dev->write_hwaddr = dw_write_hwaddr;
  440. eth_register(dev);
  441. priv->interface = interface;
  442. dw_mdio_init(dev->name, priv->mac_regs_p);
  443. priv->bus = miiphy_get_dev_by_name(dev->name);
  444. return dw_phy_init(priv, dev);
  445. }
  446. #endif
  447. #ifdef CONFIG_DM_ETH
  448. static int designware_eth_start(struct udevice *dev)
  449. {
  450. struct eth_pdata *pdata = dev_get_platdata(dev);
  451. return _dw_eth_init(dev->priv, pdata->enetaddr);
  452. }
  453. static int designware_eth_send(struct udevice *dev, void *packet, int length)
  454. {
  455. struct dw_eth_dev *priv = dev_get_priv(dev);
  456. return _dw_eth_send(priv, packet, length);
  457. }
  458. static int designware_eth_recv(struct udevice *dev, int flags, uchar **packetp)
  459. {
  460. struct dw_eth_dev *priv = dev_get_priv(dev);
  461. return _dw_eth_recv(priv, packetp);
  462. }
  463. static int designware_eth_free_pkt(struct udevice *dev, uchar *packet,
  464. int length)
  465. {
  466. struct dw_eth_dev *priv = dev_get_priv(dev);
  467. return _dw_free_pkt(priv);
  468. }
  469. static void designware_eth_stop(struct udevice *dev)
  470. {
  471. struct dw_eth_dev *priv = dev_get_priv(dev);
  472. return _dw_eth_halt(priv);
  473. }
  474. static int designware_eth_write_hwaddr(struct udevice *dev)
  475. {
  476. struct eth_pdata *pdata = dev_get_platdata(dev);
  477. struct dw_eth_dev *priv = dev_get_priv(dev);
  478. return _dw_write_hwaddr(priv, pdata->enetaddr);
  479. }
  480. static int designware_eth_bind(struct udevice *dev)
  481. {
  482. #ifdef CONFIG_DM_PCI
  483. static int num_cards;
  484. char name[20];
  485. /* Create a unique device name for PCI type devices */
  486. if (device_is_on_pci_bus(dev)) {
  487. sprintf(name, "eth_designware#%u", num_cards++);
  488. device_set_name(dev, name);
  489. }
  490. #endif
  491. return 0;
  492. }
  493. static int designware_eth_probe(struct udevice *dev)
  494. {
  495. struct eth_pdata *pdata = dev_get_platdata(dev);
  496. struct dw_eth_dev *priv = dev_get_priv(dev);
  497. u32 iobase = pdata->iobase;
  498. ulong ioaddr;
  499. int ret;
  500. #ifdef CONFIG_DM_PCI
  501. /*
  502. * If we are on PCI bus, either directly attached to a PCI root port,
  503. * or via a PCI bridge, fill in platdata before we probe the hardware.
  504. */
  505. if (device_is_on_pci_bus(dev)) {
  506. dm_pci_read_config32(dev, PCI_BASE_ADDRESS_0, &iobase);
  507. iobase &= PCI_BASE_ADDRESS_MEM_MASK;
  508. iobase = dm_pci_mem_to_phys(dev, iobase);
  509. pdata->iobase = iobase;
  510. pdata->phy_interface = PHY_INTERFACE_MODE_RMII;
  511. }
  512. #endif
  513. debug("%s, iobase=%x, priv=%p\n", __func__, iobase, priv);
  514. ioaddr = iobase;
  515. priv->mac_regs_p = (struct eth_mac_regs *)ioaddr;
  516. priv->dma_regs_p = (struct eth_dma_regs *)(ioaddr + DW_DMA_BASE_OFFSET);
  517. priv->interface = pdata->phy_interface;
  518. priv->max_speed = pdata->max_speed;
  519. dw_mdio_init(dev->name, dev);
  520. priv->bus = miiphy_get_dev_by_name(dev->name);
  521. ret = dw_phy_init(priv, dev);
  522. debug("%s, ret=%d\n", __func__, ret);
  523. return ret;
  524. }
  525. static int designware_eth_remove(struct udevice *dev)
  526. {
  527. struct dw_eth_dev *priv = dev_get_priv(dev);
  528. free(priv->phydev);
  529. mdio_unregister(priv->bus);
  530. mdio_free(priv->bus);
  531. return 0;
  532. }
  533. static const struct eth_ops designware_eth_ops = {
  534. .start = designware_eth_start,
  535. .send = designware_eth_send,
  536. .recv = designware_eth_recv,
  537. .free_pkt = designware_eth_free_pkt,
  538. .stop = designware_eth_stop,
  539. .write_hwaddr = designware_eth_write_hwaddr,
  540. };
  541. static int designware_eth_ofdata_to_platdata(struct udevice *dev)
  542. {
  543. struct dw_eth_pdata *dw_pdata = dev_get_platdata(dev);
  544. #ifdef CONFIG_DM_GPIO
  545. struct dw_eth_dev *priv = dev_get_priv(dev);
  546. #endif
  547. struct eth_pdata *pdata = &dw_pdata->eth_pdata;
  548. const char *phy_mode;
  549. const fdt32_t *cell;
  550. #ifdef CONFIG_DM_GPIO
  551. int reset_flags = GPIOD_IS_OUT;
  552. #endif
  553. int ret = 0;
  554. pdata->iobase = dev_get_addr(dev);
  555. pdata->phy_interface = -1;
  556. phy_mode = fdt_getprop(gd->fdt_blob, dev->of_offset, "phy-mode", NULL);
  557. if (phy_mode)
  558. pdata->phy_interface = phy_get_interface_by_name(phy_mode);
  559. if (pdata->phy_interface == -1) {
  560. debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
  561. return -EINVAL;
  562. }
  563. pdata->max_speed = 0;
  564. cell = fdt_getprop(gd->fdt_blob, dev->of_offset, "max-speed", NULL);
  565. if (cell)
  566. pdata->max_speed = fdt32_to_cpu(*cell);
  567. #ifdef CONFIG_DM_GPIO
  568. if (fdtdec_get_bool(gd->fdt_blob, dev->of_offset,
  569. "snps,reset-active-low"))
  570. reset_flags |= GPIOD_ACTIVE_LOW;
  571. ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
  572. &priv->reset_gpio, reset_flags);
  573. if (ret == 0) {
  574. ret = fdtdec_get_int_array(gd->fdt_blob, dev->of_offset,
  575. "snps,reset-delays-us", dw_pdata->reset_delays, 3);
  576. } else if (ret == -ENOENT) {
  577. ret = 0;
  578. }
  579. #endif
  580. return ret;
  581. }
  582. static const struct udevice_id designware_eth_ids[] = {
  583. { .compatible = "allwinner,sun7i-a20-gmac" },
  584. { .compatible = "altr,socfpga-stmmac" },
  585. { .compatible = "amlogic,meson6-dwmac" },
  586. { }
  587. };
  588. U_BOOT_DRIVER(eth_designware) = {
  589. .name = "eth_designware",
  590. .id = UCLASS_ETH,
  591. .of_match = designware_eth_ids,
  592. .ofdata_to_platdata = designware_eth_ofdata_to_platdata,
  593. .bind = designware_eth_bind,
  594. .probe = designware_eth_probe,
  595. .remove = designware_eth_remove,
  596. .ops = &designware_eth_ops,
  597. .priv_auto_alloc_size = sizeof(struct dw_eth_dev),
  598. .platdata_auto_alloc_size = sizeof(struct dw_eth_pdata),
  599. .flags = DM_FLAG_ALLOC_PRIV_DMA,
  600. };
  601. static struct pci_device_id supported[] = {
  602. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_QRK_EMAC) },
  603. { }
  604. };
  605. U_BOOT_PCI_DEVICE(eth_designware, supported);
  606. #endif