designware.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. printf("DMA reset timeout\n");
  191. return -1;
  192. }
  193. mdelay(100);
  194. };
  195. /* Soft reset above clears HW address registers.
  196. * So we have to set it here once again */
  197. dw_write_hwaddr(dev);
  198. rx_descs_init(dev);
  199. tx_descs_init(dev);
  200. writel(FIXEDBURST | PRIORXTX_41 | DMA_PBL, &dma_p->busmode);
  201. writel(readl(&dma_p->opmode) | FLUSHTXFIFO | STOREFORWARD,
  202. &dma_p->opmode);
  203. writel(readl(&dma_p->opmode) | RXSTART | TXSTART, &dma_p->opmode);
  204. /* Start up the PHY */
  205. if (phy_startup(priv->phydev)) {
  206. printf("Could not initialize PHY %s\n",
  207. priv->phydev->dev->name);
  208. return -1;
  209. }
  210. dw_adjust_link(mac_p, priv->phydev);
  211. if (!priv->phydev->link)
  212. return -1;
  213. writel(readl(&mac_p->conf) | RXENABLE | TXENABLE, &mac_p->conf);
  214. return 0;
  215. }
  216. static int dw_eth_send(struct eth_device *dev, void *packet, int length)
  217. {
  218. struct dw_eth_dev *priv = dev->priv;
  219. struct eth_dma_regs *dma_p = priv->dma_regs_p;
  220. u32 desc_num = priv->tx_currdescnum;
  221. struct dmamacdescr *desc_p = &priv->tx_mac_descrtable[desc_num];
  222. uint32_t desc_start = (uint32_t)desc_p;
  223. uint32_t desc_end = desc_start +
  224. roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
  225. uint32_t data_start = (uint32_t)desc_p->dmamac_addr;
  226. uint32_t data_end = data_start +
  227. roundup(length, ARCH_DMA_MINALIGN);
  228. /*
  229. * Strictly we only need to invalidate the "txrx_status" field
  230. * for the following check, but on some platforms we cannot
  231. * invalidate only 4 bytes, so we flush the entire descriptor,
  232. * which is 16 bytes in total. This is safe because the
  233. * individual descriptors in the array are each aligned to
  234. * ARCH_DMA_MINALIGN and padded appropriately.
  235. */
  236. invalidate_dcache_range(desc_start, desc_end);
  237. /* Check if the descriptor is owned by CPU */
  238. if (desc_p->txrx_status & DESC_TXSTS_OWNBYDMA) {
  239. printf("CPU not owner of tx frame\n");
  240. return -1;
  241. }
  242. memcpy(desc_p->dmamac_addr, packet, length);
  243. /* Flush data to be sent */
  244. flush_dcache_range(data_start, data_end);
  245. #if defined(CONFIG_DW_ALTDESCRIPTOR)
  246. desc_p->txrx_status |= DESC_TXSTS_TXFIRST | DESC_TXSTS_TXLAST;
  247. desc_p->dmamac_cntl |= (length << DESC_TXCTRL_SIZE1SHFT) & \
  248. DESC_TXCTRL_SIZE1MASK;
  249. desc_p->txrx_status &= ~(DESC_TXSTS_MSK);
  250. desc_p->txrx_status |= DESC_TXSTS_OWNBYDMA;
  251. #else
  252. desc_p->dmamac_cntl |= ((length << DESC_TXCTRL_SIZE1SHFT) & \
  253. DESC_TXCTRL_SIZE1MASK) | DESC_TXCTRL_TXLAST | \
  254. DESC_TXCTRL_TXFIRST;
  255. desc_p->txrx_status = DESC_TXSTS_OWNBYDMA;
  256. #endif
  257. /* Flush modified buffer descriptor */
  258. flush_dcache_range(desc_start, desc_end);
  259. /* Test the wrap-around condition. */
  260. if (++desc_num >= CONFIG_TX_DESCR_NUM)
  261. desc_num = 0;
  262. priv->tx_currdescnum = desc_num;
  263. /* Start the transmission */
  264. writel(POLL_DATA, &dma_p->txpolldemand);
  265. return 0;
  266. }
  267. static int dw_eth_recv(struct eth_device *dev)
  268. {
  269. struct dw_eth_dev *priv = dev->priv;
  270. u32 status, desc_num = priv->rx_currdescnum;
  271. struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
  272. int length = 0;
  273. uint32_t desc_start = (uint32_t)desc_p;
  274. uint32_t desc_end = desc_start +
  275. roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
  276. uint32_t data_start = (uint32_t)desc_p->dmamac_addr;
  277. uint32_t data_end;
  278. /* Invalidate entire buffer descriptor */
  279. invalidate_dcache_range(desc_start, desc_end);
  280. status = desc_p->txrx_status;
  281. /* Check if the owner is the CPU */
  282. if (!(status & DESC_RXSTS_OWNBYDMA)) {
  283. length = (status & DESC_RXSTS_FRMLENMSK) >> \
  284. DESC_RXSTS_FRMLENSHFT;
  285. /* Invalidate received data */
  286. data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
  287. invalidate_dcache_range(data_start, data_end);
  288. NetReceive(desc_p->dmamac_addr, length);
  289. /*
  290. * Make the current descriptor valid again and go to
  291. * the next one
  292. */
  293. desc_p->txrx_status |= DESC_RXSTS_OWNBYDMA;
  294. /* Flush only status field - others weren't changed */
  295. flush_dcache_range(desc_start, desc_end);
  296. /* Test the wrap-around condition. */
  297. if (++desc_num >= CONFIG_RX_DESCR_NUM)
  298. desc_num = 0;
  299. }
  300. priv->rx_currdescnum = desc_num;
  301. return length;
  302. }
  303. static int dw_phy_init(struct eth_device *dev)
  304. {
  305. struct dw_eth_dev *priv = dev->priv;
  306. struct phy_device *phydev;
  307. int mask = 0xffffffff;
  308. #ifdef CONFIG_PHY_ADDR
  309. mask = 1 << CONFIG_PHY_ADDR;
  310. #endif
  311. phydev = phy_find_by_mask(priv->bus, mask, priv->interface);
  312. if (!phydev)
  313. return -1;
  314. phy_connect_dev(phydev, dev);
  315. phydev->supported &= PHY_GBIT_FEATURES;
  316. phydev->advertising = phydev->supported;
  317. priv->phydev = phydev;
  318. phy_config(phydev);
  319. return 1;
  320. }
  321. int designware_initialize(ulong base_addr, u32 interface)
  322. {
  323. struct eth_device *dev;
  324. struct dw_eth_dev *priv;
  325. dev = (struct eth_device *) malloc(sizeof(struct eth_device));
  326. if (!dev)
  327. return -ENOMEM;
  328. /*
  329. * Since the priv structure contains the descriptors which need a strict
  330. * buswidth alignment, memalign is used to allocate memory
  331. */
  332. priv = (struct dw_eth_dev *) memalign(ARCH_DMA_MINALIGN,
  333. sizeof(struct dw_eth_dev));
  334. if (!priv) {
  335. free(dev);
  336. return -ENOMEM;
  337. }
  338. memset(dev, 0, sizeof(struct eth_device));
  339. memset(priv, 0, sizeof(struct dw_eth_dev));
  340. sprintf(dev->name, "dwmac.%lx", base_addr);
  341. dev->iobase = (int)base_addr;
  342. dev->priv = priv;
  343. priv->dev = dev;
  344. priv->mac_regs_p = (struct eth_mac_regs *)base_addr;
  345. priv->dma_regs_p = (struct eth_dma_regs *)(base_addr +
  346. DW_DMA_BASE_OFFSET);
  347. dev->init = dw_eth_init;
  348. dev->send = dw_eth_send;
  349. dev->recv = dw_eth_recv;
  350. dev->halt = dw_eth_halt;
  351. dev->write_hwaddr = dw_write_hwaddr;
  352. eth_register(dev);
  353. priv->interface = interface;
  354. dw_mdio_init(dev->name, priv->mac_regs_p);
  355. priv->bus = miiphy_get_dev_by_name(dev->name);
  356. return dw_phy_init(dev);
  357. }