designware.c 17 KB

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