designware.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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 CONFIG_DM_ETH
  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. #ifdef CONFIG_DM_ETH
  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 = &txbuffs[idx * CONFIG_ETH_BUFSIZE];
  120. desc_p->dmamac_next = &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 = &desc_table_p[0];
  136. /* Flush all Tx buffer descriptors at once */
  137. flush_dcache_range((unsigned int)priv->tx_mac_descrtable,
  138. (unsigned int)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((unsigned int)rxbuffs, (unsigned int)rxbuffs +
  157. RX_TOTAL_BUFSIZE);
  158. for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
  159. desc_p = &desc_table_p[idx];
  160. desc_p->dmamac_addr = &rxbuffs[idx * CONFIG_ETH_BUFSIZE];
  161. desc_p->dmamac_next = &desc_table_p[idx + 1];
  162. desc_p->dmamac_cntl =
  163. (MAC_MAX_FRAME_SZ & DESC_RXCTRL_SIZE1MASK) |
  164. DESC_RXCTRL_RXCHAIN;
  165. desc_p->txrx_status = DESC_RXSTS_OWNBYDMA;
  166. }
  167. /* Correcting the last pointer of the chain */
  168. desc_p->dmamac_next = &desc_table_p[0];
  169. /* Flush all Rx buffer descriptors at once */
  170. flush_dcache_range((unsigned int)priv->rx_mac_descrtable,
  171. (unsigned int)priv->rx_mac_descrtable +
  172. sizeof(priv->rx_mac_descrtable));
  173. writel((ulong)&desc_table_p[0], &dma_p->rxdesclistaddr);
  174. priv->rx_currdescnum = 0;
  175. }
  176. static int _dw_write_hwaddr(struct dw_eth_dev *priv, u8 *mac_id)
  177. {
  178. struct eth_mac_regs *mac_p = priv->mac_regs_p;
  179. u32 macid_lo, macid_hi;
  180. macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
  181. (mac_id[3] << 24);
  182. macid_hi = mac_id[4] + (mac_id[5] << 8);
  183. writel(macid_hi, &mac_p->macaddr0hi);
  184. writel(macid_lo, &mac_p->macaddr0lo);
  185. return 0;
  186. }
  187. static void dw_adjust_link(struct eth_mac_regs *mac_p,
  188. struct phy_device *phydev)
  189. {
  190. u32 conf = readl(&mac_p->conf) | FRAMEBURSTENABLE | DISABLERXOWN;
  191. if (!phydev->link) {
  192. printf("%s: No link.\n", phydev->dev->name);
  193. return;
  194. }
  195. if (phydev->speed != 1000)
  196. conf |= MII_PORTSELECT;
  197. else
  198. conf &= ~MII_PORTSELECT;
  199. if (phydev->speed == 100)
  200. conf |= FES_100;
  201. if (phydev->duplex)
  202. conf |= FULLDPLXMODE;
  203. writel(conf, &mac_p->conf);
  204. printf("Speed: %d, %s duplex%s\n", phydev->speed,
  205. (phydev->duplex) ? "full" : "half",
  206. (phydev->port == PORT_FIBRE) ? ", fiber mode" : "");
  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. dw_adjust_link(mac_p, priv->phydev);
  258. if (!priv->phydev->link)
  259. return -EIO;
  260. writel(readl(&mac_p->conf) | RXENABLE | TXENABLE, &mac_p->conf);
  261. return 0;
  262. }
  263. static int _dw_eth_send(struct dw_eth_dev *priv, void *packet, int length)
  264. {
  265. struct eth_dma_regs *dma_p = priv->dma_regs_p;
  266. u32 desc_num = priv->tx_currdescnum;
  267. struct dmamacdescr *desc_p = &priv->tx_mac_descrtable[desc_num];
  268. uint32_t desc_start = (uint32_t)desc_p;
  269. uint32_t desc_end = desc_start +
  270. roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
  271. uint32_t data_start = (uint32_t)desc_p->dmamac_addr;
  272. uint32_t data_end = data_start +
  273. roundup(length, ARCH_DMA_MINALIGN);
  274. /*
  275. * Strictly we only need to invalidate the "txrx_status" field
  276. * for the following check, but on some platforms we cannot
  277. * invalidate only 4 bytes, so we flush the entire descriptor,
  278. * which is 16 bytes in total. This is safe because the
  279. * individual descriptors in the array are each aligned to
  280. * ARCH_DMA_MINALIGN and padded appropriately.
  281. */
  282. invalidate_dcache_range(desc_start, desc_end);
  283. /* Check if the descriptor is owned by CPU */
  284. if (desc_p->txrx_status & DESC_TXSTS_OWNBYDMA) {
  285. printf("CPU not owner of tx frame\n");
  286. return -EPERM;
  287. }
  288. memcpy(desc_p->dmamac_addr, packet, length);
  289. /* Flush data to be sent */
  290. flush_dcache_range(data_start, data_end);
  291. #if defined(CONFIG_DW_ALTDESCRIPTOR)
  292. desc_p->txrx_status |= DESC_TXSTS_TXFIRST | DESC_TXSTS_TXLAST;
  293. desc_p->dmamac_cntl |= (length << DESC_TXCTRL_SIZE1SHFT) &
  294. DESC_TXCTRL_SIZE1MASK;
  295. desc_p->txrx_status &= ~(DESC_TXSTS_MSK);
  296. desc_p->txrx_status |= DESC_TXSTS_OWNBYDMA;
  297. #else
  298. desc_p->dmamac_cntl |= ((length << DESC_TXCTRL_SIZE1SHFT) &
  299. DESC_TXCTRL_SIZE1MASK) | DESC_TXCTRL_TXLAST |
  300. DESC_TXCTRL_TXFIRST;
  301. desc_p->txrx_status = DESC_TXSTS_OWNBYDMA;
  302. #endif
  303. /* Flush modified buffer descriptor */
  304. flush_dcache_range(desc_start, desc_end);
  305. /* Test the wrap-around condition. */
  306. if (++desc_num >= CONFIG_TX_DESCR_NUM)
  307. desc_num = 0;
  308. priv->tx_currdescnum = desc_num;
  309. /* Start the transmission */
  310. writel(POLL_DATA, &dma_p->txpolldemand);
  311. return 0;
  312. }
  313. static int _dw_eth_recv(struct dw_eth_dev *priv, uchar **packetp)
  314. {
  315. u32 status, desc_num = priv->rx_currdescnum;
  316. struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
  317. int length = -EAGAIN;
  318. uint32_t desc_start = (uint32_t)desc_p;
  319. uint32_t desc_end = desc_start +
  320. roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
  321. uint32_t data_start = (uint32_t)desc_p->dmamac_addr;
  322. uint32_t data_end;
  323. /* Invalidate entire buffer descriptor */
  324. invalidate_dcache_range(desc_start, desc_end);
  325. status = desc_p->txrx_status;
  326. /* Check if the owner is the CPU */
  327. if (!(status & DESC_RXSTS_OWNBYDMA)) {
  328. length = (status & DESC_RXSTS_FRMLENMSK) >>
  329. DESC_RXSTS_FRMLENSHFT;
  330. /* Invalidate received data */
  331. data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
  332. invalidate_dcache_range(data_start, data_end);
  333. *packetp = desc_p->dmamac_addr;
  334. }
  335. return length;
  336. }
  337. static int _dw_free_pkt(struct dw_eth_dev *priv)
  338. {
  339. u32 desc_num = priv->rx_currdescnum;
  340. struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
  341. uint32_t desc_start = (uint32_t)desc_p;
  342. uint32_t desc_end = desc_start +
  343. roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
  344. /*
  345. * Make the current descriptor valid again and go to
  346. * the next one
  347. */
  348. desc_p->txrx_status |= DESC_RXSTS_OWNBYDMA;
  349. /* Flush only status field - others weren't changed */
  350. flush_dcache_range(desc_start, desc_end);
  351. /* Test the wrap-around condition. */
  352. if (++desc_num >= CONFIG_RX_DESCR_NUM)
  353. desc_num = 0;
  354. priv->rx_currdescnum = desc_num;
  355. return 0;
  356. }
  357. static int dw_phy_init(struct dw_eth_dev *priv, void *dev)
  358. {
  359. struct phy_device *phydev;
  360. int mask = 0xffffffff, ret;
  361. #ifdef CONFIG_PHY_ADDR
  362. mask = 1 << CONFIG_PHY_ADDR;
  363. #endif
  364. phydev = phy_find_by_mask(priv->bus, mask, priv->interface);
  365. if (!phydev)
  366. return -ENODEV;
  367. phy_connect_dev(phydev, dev);
  368. phydev->supported &= PHY_GBIT_FEATURES;
  369. if (priv->max_speed) {
  370. ret = phy_set_supported(phydev, priv->max_speed);
  371. if (ret)
  372. return ret;
  373. }
  374. phydev->advertising = phydev->supported;
  375. priv->phydev = phydev;
  376. phy_config(phydev);
  377. return 0;
  378. }
  379. #ifndef CONFIG_DM_ETH
  380. static int dw_eth_init(struct eth_device *dev, bd_t *bis)
  381. {
  382. return _dw_eth_init(dev->priv, dev->enetaddr);
  383. }
  384. static int dw_eth_send(struct eth_device *dev, void *packet, int length)
  385. {
  386. return _dw_eth_send(dev->priv, packet, length);
  387. }
  388. static int dw_eth_recv(struct eth_device *dev)
  389. {
  390. uchar *packet;
  391. int length;
  392. length = _dw_eth_recv(dev->priv, &packet);
  393. if (length == -EAGAIN)
  394. return 0;
  395. net_process_received_packet(packet, length);
  396. _dw_free_pkt(dev->priv);
  397. return 0;
  398. }
  399. static void dw_eth_halt(struct eth_device *dev)
  400. {
  401. return _dw_eth_halt(dev->priv);
  402. }
  403. static int dw_write_hwaddr(struct eth_device *dev)
  404. {
  405. return _dw_write_hwaddr(dev->priv, dev->enetaddr);
  406. }
  407. int designware_initialize(ulong base_addr, u32 interface)
  408. {
  409. struct eth_device *dev;
  410. struct dw_eth_dev *priv;
  411. dev = (struct eth_device *) malloc(sizeof(struct eth_device));
  412. if (!dev)
  413. return -ENOMEM;
  414. /*
  415. * Since the priv structure contains the descriptors which need a strict
  416. * buswidth alignment, memalign is used to allocate memory
  417. */
  418. priv = (struct dw_eth_dev *) memalign(ARCH_DMA_MINALIGN,
  419. sizeof(struct dw_eth_dev));
  420. if (!priv) {
  421. free(dev);
  422. return -ENOMEM;
  423. }
  424. memset(dev, 0, sizeof(struct eth_device));
  425. memset(priv, 0, sizeof(struct dw_eth_dev));
  426. sprintf(dev->name, "dwmac.%lx", base_addr);
  427. dev->iobase = (int)base_addr;
  428. dev->priv = priv;
  429. priv->dev = dev;
  430. priv->mac_regs_p = (struct eth_mac_regs *)base_addr;
  431. priv->dma_regs_p = (struct eth_dma_regs *)(base_addr +
  432. DW_DMA_BASE_OFFSET);
  433. dev->init = dw_eth_init;
  434. dev->send = dw_eth_send;
  435. dev->recv = dw_eth_recv;
  436. dev->halt = dw_eth_halt;
  437. dev->write_hwaddr = dw_write_hwaddr;
  438. eth_register(dev);
  439. priv->interface = interface;
  440. dw_mdio_init(dev->name, priv->mac_regs_p);
  441. priv->bus = miiphy_get_dev_by_name(dev->name);
  442. return dw_phy_init(priv, dev);
  443. }
  444. #endif
  445. #ifdef CONFIG_DM_ETH
  446. static int designware_eth_start(struct udevice *dev)
  447. {
  448. struct eth_pdata *pdata = dev_get_platdata(dev);
  449. return _dw_eth_init(dev->priv, pdata->enetaddr);
  450. }
  451. static int designware_eth_send(struct udevice *dev, void *packet, int length)
  452. {
  453. struct dw_eth_dev *priv = dev_get_priv(dev);
  454. return _dw_eth_send(priv, packet, length);
  455. }
  456. static int designware_eth_recv(struct udevice *dev, int flags, uchar **packetp)
  457. {
  458. struct dw_eth_dev *priv = dev_get_priv(dev);
  459. return _dw_eth_recv(priv, packetp);
  460. }
  461. static int designware_eth_free_pkt(struct udevice *dev, uchar *packet,
  462. int length)
  463. {
  464. struct dw_eth_dev *priv = dev_get_priv(dev);
  465. return _dw_free_pkt(priv);
  466. }
  467. static void designware_eth_stop(struct udevice *dev)
  468. {
  469. struct dw_eth_dev *priv = dev_get_priv(dev);
  470. return _dw_eth_halt(priv);
  471. }
  472. static int designware_eth_write_hwaddr(struct udevice *dev)
  473. {
  474. struct eth_pdata *pdata = dev_get_platdata(dev);
  475. struct dw_eth_dev *priv = dev_get_priv(dev);
  476. return _dw_write_hwaddr(priv, pdata->enetaddr);
  477. }
  478. static int designware_eth_bind(struct udevice *dev)
  479. {
  480. #ifdef CONFIG_DM_PCI
  481. static int num_cards;
  482. char name[20];
  483. /* Create a unique device name for PCI type devices */
  484. if (device_is_on_pci_bus(dev)) {
  485. sprintf(name, "eth_designware#%u", num_cards++);
  486. device_set_name(dev, name);
  487. }
  488. #endif
  489. return 0;
  490. }
  491. static int designware_eth_probe(struct udevice *dev)
  492. {
  493. struct eth_pdata *pdata = dev_get_platdata(dev);
  494. struct dw_eth_dev *priv = dev_get_priv(dev);
  495. u32 iobase = pdata->iobase;
  496. int ret;
  497. #ifdef CONFIG_DM_PCI
  498. /*
  499. * If we are on PCI bus, either directly attached to a PCI root port,
  500. * or via a PCI bridge, fill in platdata before we probe the hardware.
  501. */
  502. if (device_is_on_pci_bus(dev)) {
  503. dm_pci_read_config32(dev, PCI_BASE_ADDRESS_0, &iobase);
  504. iobase &= PCI_BASE_ADDRESS_MEM_MASK;
  505. iobase = dm_pci_mem_to_phys(dev, iobase);
  506. pdata->iobase = iobase;
  507. pdata->phy_interface = PHY_INTERFACE_MODE_RMII;
  508. }
  509. #endif
  510. debug("%s, iobase=%x, priv=%p\n", __func__, iobase, priv);
  511. priv->mac_regs_p = (struct eth_mac_regs *)iobase;
  512. priv->dma_regs_p = (struct eth_dma_regs *)(iobase + DW_DMA_BASE_OFFSET);
  513. priv->interface = pdata->phy_interface;
  514. priv->max_speed = pdata->max_speed;
  515. dw_mdio_init(dev->name, dev);
  516. priv->bus = miiphy_get_dev_by_name(dev->name);
  517. ret = dw_phy_init(priv, dev);
  518. debug("%s, ret=%d\n", __func__, ret);
  519. return ret;
  520. }
  521. static int designware_eth_remove(struct udevice *dev)
  522. {
  523. struct dw_eth_dev *priv = dev_get_priv(dev);
  524. free(priv->phydev);
  525. mdio_unregister(priv->bus);
  526. mdio_free(priv->bus);
  527. return 0;
  528. }
  529. static const struct eth_ops designware_eth_ops = {
  530. .start = designware_eth_start,
  531. .send = designware_eth_send,
  532. .recv = designware_eth_recv,
  533. .free_pkt = designware_eth_free_pkt,
  534. .stop = designware_eth_stop,
  535. .write_hwaddr = designware_eth_write_hwaddr,
  536. };
  537. static int designware_eth_ofdata_to_platdata(struct udevice *dev)
  538. {
  539. struct dw_eth_pdata *dw_pdata = dev_get_platdata(dev);
  540. struct dw_eth_dev *priv = dev_get_priv(dev);
  541. struct eth_pdata *pdata = &dw_pdata->eth_pdata;
  542. const char *phy_mode;
  543. const fdt32_t *cell;
  544. int reset_flags = GPIOD_IS_OUT;
  545. int ret = 0;
  546. pdata->iobase = dev_get_addr(dev);
  547. pdata->phy_interface = -1;
  548. phy_mode = fdt_getprop(gd->fdt_blob, dev->of_offset, "phy-mode", NULL);
  549. if (phy_mode)
  550. pdata->phy_interface = phy_get_interface_by_name(phy_mode);
  551. if (pdata->phy_interface == -1) {
  552. debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
  553. return -EINVAL;
  554. }
  555. pdata->max_speed = 0;
  556. cell = fdt_getprop(gd->fdt_blob, dev->of_offset, "max-speed", NULL);
  557. if (cell)
  558. pdata->max_speed = fdt32_to_cpu(*cell);
  559. if (fdtdec_get_bool(gd->fdt_blob, dev->of_offset,
  560. "snps,reset-active-low"))
  561. reset_flags |= GPIOD_ACTIVE_LOW;
  562. ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
  563. &priv->reset_gpio, reset_flags);
  564. if (ret == 0) {
  565. ret = fdtdec_get_int_array(gd->fdt_blob, dev->of_offset,
  566. "snps,reset-delays-us", dw_pdata->reset_delays, 3);
  567. } else if (ret == -ENOENT) {
  568. ret = 0;
  569. }
  570. return ret;
  571. }
  572. static const struct udevice_id designware_eth_ids[] = {
  573. { .compatible = "allwinner,sun7i-a20-gmac" },
  574. { .compatible = "altr,socfpga-stmmac" },
  575. { }
  576. };
  577. U_BOOT_DRIVER(eth_designware) = {
  578. .name = "eth_designware",
  579. .id = UCLASS_ETH,
  580. .of_match = designware_eth_ids,
  581. .ofdata_to_platdata = designware_eth_ofdata_to_platdata,
  582. .bind = designware_eth_bind,
  583. .probe = designware_eth_probe,
  584. .remove = designware_eth_remove,
  585. .ops = &designware_eth_ops,
  586. .priv_auto_alloc_size = sizeof(struct dw_eth_dev),
  587. .platdata_auto_alloc_size = sizeof(struct dw_eth_pdata),
  588. .flags = DM_FLAG_ALLOC_PRIV_DMA,
  589. };
  590. static struct pci_device_id supported[] = {
  591. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_QRK_EMAC) },
  592. { }
  593. };
  594. U_BOOT_PCI_DEVICE(eth_designware, supported);
  595. #endif