designware.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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 <miiphy.h>
  12. #include <malloc.h>
  13. #include <linux/compiler.h>
  14. #include <linux/err.h>
  15. #include <asm/io.h>
  16. #include "designware.h"
  17. #if !defined(CONFIG_PHYLIB)
  18. # error "DesignWare Ether MAC requires PHYLIB - missing CONFIG_PHYLIB"
  19. #endif
  20. static int dw_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
  21. {
  22. struct eth_mac_regs *mac_p = bus->priv;
  23. ulong start;
  24. u16 miiaddr;
  25. int timeout = CONFIG_MDIO_TIMEOUT;
  26. miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
  27. ((reg << MIIREGSHIFT) & MII_REGMSK);
  28. writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
  29. start = get_timer(0);
  30. while (get_timer(start) < timeout) {
  31. if (!(readl(&mac_p->miiaddr) & MII_BUSY))
  32. return readl(&mac_p->miidata);
  33. udelay(10);
  34. };
  35. return -1;
  36. }
  37. static int dw_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
  38. u16 val)
  39. {
  40. struct eth_mac_regs *mac_p = bus->priv;
  41. ulong start;
  42. u16 miiaddr;
  43. int ret = -1, timeout = CONFIG_MDIO_TIMEOUT;
  44. writel(val, &mac_p->miidata);
  45. miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
  46. ((reg << MIIREGSHIFT) & MII_REGMSK) | MII_WRITE;
  47. writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
  48. start = get_timer(0);
  49. while (get_timer(start) < timeout) {
  50. if (!(readl(&mac_p->miiaddr) & MII_BUSY)) {
  51. ret = 0;
  52. break;
  53. }
  54. udelay(10);
  55. };
  56. return ret;
  57. }
  58. static int dw_mdio_init(char *name, struct eth_mac_regs *mac_regs_p)
  59. {
  60. struct mii_dev *bus = mdio_alloc();
  61. if (!bus) {
  62. printf("Failed to allocate MDIO bus\n");
  63. return -1;
  64. }
  65. bus->read = dw_mdio_read;
  66. bus->write = dw_mdio_write;
  67. sprintf(bus->name, name);
  68. bus->priv = (void *)mac_regs_p;
  69. return mdio_register(bus);
  70. }
  71. static void tx_descs_init(struct eth_device *dev)
  72. {
  73. struct dw_eth_dev *priv = dev->priv;
  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 = &txbuffs[idx * CONFIG_ETH_BUFSIZE];
  82. desc_p->dmamac_next = &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 = &desc_table_p[0];
  98. /* Flush all Tx buffer descriptors at once */
  99. flush_dcache_range((unsigned int)priv->tx_mac_descrtable,
  100. (unsigned int)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 eth_device *dev)
  106. {
  107. struct dw_eth_dev *priv = dev->priv;
  108. struct eth_dma_regs *dma_p = priv->dma_regs_p;
  109. struct dmamacdescr *desc_table_p = &priv->rx_mac_descrtable[0];
  110. char *rxbuffs = &priv->rxbuffs[0];
  111. struct dmamacdescr *desc_p;
  112. u32 idx;
  113. /* Before passing buffers to GMAC we need to make sure zeros
  114. * written there right after "priv" structure allocation were
  115. * flushed into RAM.
  116. * Otherwise there's a chance to get some of them flushed in RAM when
  117. * GMAC is already pushing data to RAM via DMA. This way incoming from
  118. * GMAC data will be corrupted. */
  119. flush_dcache_range((unsigned int)rxbuffs, (unsigned int)rxbuffs +
  120. RX_TOTAL_BUFSIZE);
  121. for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
  122. desc_p = &desc_table_p[idx];
  123. desc_p->dmamac_addr = &rxbuffs[idx * CONFIG_ETH_BUFSIZE];
  124. desc_p->dmamac_next = &desc_table_p[idx + 1];
  125. desc_p->dmamac_cntl =
  126. (MAC_MAX_FRAME_SZ & DESC_RXCTRL_SIZE1MASK) | \
  127. DESC_RXCTRL_RXCHAIN;
  128. desc_p->txrx_status = DESC_RXSTS_OWNBYDMA;
  129. }
  130. /* Correcting the last pointer of the chain */
  131. desc_p->dmamac_next = &desc_table_p[0];
  132. /* Flush all Rx buffer descriptors at once */
  133. flush_dcache_range((unsigned int)priv->rx_mac_descrtable,
  134. (unsigned int)priv->rx_mac_descrtable +
  135. sizeof(priv->rx_mac_descrtable));
  136. writel((ulong)&desc_table_p[0], &dma_p->rxdesclistaddr);
  137. priv->rx_currdescnum = 0;
  138. }
  139. static int dw_write_hwaddr(struct eth_device *dev)
  140. {
  141. struct dw_eth_dev *priv = dev->priv;
  142. struct eth_mac_regs *mac_p = priv->mac_regs_p;
  143. u32 macid_lo, macid_hi;
  144. u8 *mac_id = &dev->enetaddr[0];
  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 eth_device *dev)
  172. {
  173. struct dw_eth_dev *priv = dev->priv;
  174. struct eth_mac_regs *mac_p = priv->mac_regs_p;
  175. struct eth_dma_regs *dma_p = priv->dma_regs_p;
  176. writel(readl(&mac_p->conf) & ~(RXENABLE | TXENABLE), &mac_p->conf);
  177. writel(readl(&dma_p->opmode) & ~(RXSTART | TXSTART), &dma_p->opmode);
  178. phy_shutdown(priv->phydev);
  179. }
  180. static int dw_eth_init(struct eth_device *dev, bd_t *bis)
  181. {
  182. struct dw_eth_dev *priv = dev->priv;
  183. struct eth_mac_regs *mac_p = priv->mac_regs_p;
  184. struct eth_dma_regs *dma_p = priv->dma_regs_p;
  185. unsigned int start;
  186. writel(readl(&dma_p->busmode) | DMAMAC_SRST, &dma_p->busmode);
  187. start = get_timer(0);
  188. while (readl(&dma_p->busmode) & DMAMAC_SRST) {
  189. if (get_timer(start) >= CONFIG_MACRESET_TIMEOUT)
  190. return -1;
  191. mdelay(100);
  192. };
  193. /* Soft reset above clears HW address registers.
  194. * So we have to set it here once again */
  195. dw_write_hwaddr(dev);
  196. rx_descs_init(dev);
  197. tx_descs_init(dev);
  198. writel(FIXEDBURST | PRIORXTX_41 | DMA_PBL, &dma_p->busmode);
  199. writel(readl(&dma_p->opmode) | FLUSHTXFIFO | STOREFORWARD,
  200. &dma_p->opmode);
  201. writel(readl(&dma_p->opmode) | RXSTART | TXSTART, &dma_p->opmode);
  202. /* Start up the PHY */
  203. if (phy_startup(priv->phydev)) {
  204. printf("Could not initialize PHY %s\n",
  205. priv->phydev->dev->name);
  206. return -1;
  207. }
  208. dw_adjust_link(mac_p, priv->phydev);
  209. if (!priv->phydev->link)
  210. return -1;
  211. writel(readl(&mac_p->conf) | RXENABLE | TXENABLE, &mac_p->conf);
  212. return 0;
  213. }
  214. static int dw_eth_send(struct eth_device *dev, void *packet, int length)
  215. {
  216. struct dw_eth_dev *priv = dev->priv;
  217. struct eth_dma_regs *dma_p = priv->dma_regs_p;
  218. u32 desc_num = priv->tx_currdescnum;
  219. struct dmamacdescr *desc_p = &priv->tx_mac_descrtable[desc_num];
  220. uint32_t desc_start = (uint32_t)desc_p;
  221. uint32_t desc_end = desc_start +
  222. roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
  223. uint32_t data_start = (uint32_t)desc_p->dmamac_addr;
  224. uint32_t data_end = data_start +
  225. roundup(length, ARCH_DMA_MINALIGN);
  226. /*
  227. * Strictly we only need to invalidate the "txrx_status" field
  228. * for the following check, but on some platforms we cannot
  229. * invalidate only 4 bytes, so we flush the entire descriptor,
  230. * which is 16 bytes in total. This is safe because the
  231. * individual descriptors in the array are each aligned to
  232. * ARCH_DMA_MINALIGN and padded appropriately.
  233. */
  234. invalidate_dcache_range(desc_start, desc_end);
  235. /* Check if the descriptor is owned by CPU */
  236. if (desc_p->txrx_status & DESC_TXSTS_OWNBYDMA) {
  237. printf("CPU not owner of tx frame\n");
  238. return -1;
  239. }
  240. memcpy(desc_p->dmamac_addr, packet, length);
  241. /* Flush data to be sent */
  242. flush_dcache_range(data_start, data_end);
  243. #if defined(CONFIG_DW_ALTDESCRIPTOR)
  244. desc_p->txrx_status |= DESC_TXSTS_TXFIRST | DESC_TXSTS_TXLAST;
  245. desc_p->dmamac_cntl |= (length << DESC_TXCTRL_SIZE1SHFT) & \
  246. DESC_TXCTRL_SIZE1MASK;
  247. desc_p->txrx_status &= ~(DESC_TXSTS_MSK);
  248. desc_p->txrx_status |= DESC_TXSTS_OWNBYDMA;
  249. #else
  250. desc_p->dmamac_cntl |= ((length << DESC_TXCTRL_SIZE1SHFT) & \
  251. DESC_TXCTRL_SIZE1MASK) | DESC_TXCTRL_TXLAST | \
  252. DESC_TXCTRL_TXFIRST;
  253. desc_p->txrx_status = DESC_TXSTS_OWNBYDMA;
  254. #endif
  255. /* Flush modified buffer descriptor */
  256. flush_dcache_range(desc_start, desc_end);
  257. /* Test the wrap-around condition. */
  258. if (++desc_num >= CONFIG_TX_DESCR_NUM)
  259. desc_num = 0;
  260. priv->tx_currdescnum = desc_num;
  261. /* Start the transmission */
  262. writel(POLL_DATA, &dma_p->txpolldemand);
  263. return 0;
  264. }
  265. static int dw_eth_recv(struct eth_device *dev)
  266. {
  267. struct dw_eth_dev *priv = dev->priv;
  268. u32 status, desc_num = priv->rx_currdescnum;
  269. struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
  270. int length = 0;
  271. uint32_t desc_start = (uint32_t)desc_p;
  272. uint32_t desc_end = desc_start +
  273. roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
  274. uint32_t data_start = (uint32_t)desc_p->dmamac_addr;
  275. uint32_t data_end;
  276. /* Invalidate entire buffer descriptor */
  277. invalidate_dcache_range(desc_start, desc_end);
  278. status = desc_p->txrx_status;
  279. /* Check if the owner is the CPU */
  280. if (!(status & DESC_RXSTS_OWNBYDMA)) {
  281. length = (status & DESC_RXSTS_FRMLENMSK) >> \
  282. DESC_RXSTS_FRMLENSHFT;
  283. /* Invalidate received data */
  284. data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
  285. invalidate_dcache_range(data_start, data_end);
  286. NetReceive(desc_p->dmamac_addr, length);
  287. /*
  288. * Make the current descriptor valid again and go to
  289. * the next one
  290. */
  291. desc_p->txrx_status |= DESC_RXSTS_OWNBYDMA;
  292. /* Flush only status field - others weren't changed */
  293. flush_dcache_range(desc_start, desc_end);
  294. /* Test the wrap-around condition. */
  295. if (++desc_num >= CONFIG_RX_DESCR_NUM)
  296. desc_num = 0;
  297. }
  298. priv->rx_currdescnum = desc_num;
  299. return length;
  300. }
  301. static int dw_phy_init(struct eth_device *dev)
  302. {
  303. struct dw_eth_dev *priv = dev->priv;
  304. struct phy_device *phydev;
  305. int mask = 0xffffffff;
  306. #ifdef CONFIG_PHY_ADDR
  307. mask = 1 << CONFIG_PHY_ADDR;
  308. #endif
  309. phydev = phy_find_by_mask(priv->bus, mask, priv->interface);
  310. if (!phydev)
  311. return -1;
  312. phy_connect_dev(phydev, dev);
  313. phydev->supported &= PHY_GBIT_FEATURES;
  314. phydev->advertising = phydev->supported;
  315. priv->phydev = phydev;
  316. phy_config(phydev);
  317. return 1;
  318. }
  319. int designware_initialize(ulong base_addr, u32 interface)
  320. {
  321. struct eth_device *dev;
  322. struct dw_eth_dev *priv;
  323. dev = (struct eth_device *) malloc(sizeof(struct eth_device));
  324. if (!dev)
  325. return -ENOMEM;
  326. /*
  327. * Since the priv structure contains the descriptors which need a strict
  328. * buswidth alignment, memalign is used to allocate memory
  329. */
  330. priv = (struct dw_eth_dev *) memalign(ARCH_DMA_MINALIGN,
  331. sizeof(struct dw_eth_dev));
  332. if (!priv) {
  333. free(dev);
  334. return -ENOMEM;
  335. }
  336. memset(dev, 0, sizeof(struct eth_device));
  337. memset(priv, 0, sizeof(struct dw_eth_dev));
  338. sprintf(dev->name, "dwmac.%lx", base_addr);
  339. dev->iobase = (int)base_addr;
  340. dev->priv = priv;
  341. priv->dev = dev;
  342. priv->mac_regs_p = (struct eth_mac_regs *)base_addr;
  343. priv->dma_regs_p = (struct eth_dma_regs *)(base_addr +
  344. DW_DMA_BASE_OFFSET);
  345. dev->init = dw_eth_init;
  346. dev->send = dw_eth_send;
  347. dev->recv = dw_eth_recv;
  348. dev->halt = dw_eth_halt;
  349. dev->write_hwaddr = dw_write_hwaddr;
  350. eth_register(dev);
  351. priv->interface = interface;
  352. dw_mdio_init(dev->name, priv->mac_regs_p);
  353. priv->bus = miiphy_get_dev_by_name(dev->name);
  354. return dw_phy_init(dev);
  355. }