designware.c 21 KB

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