designware.c 17 KB

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