designware.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. * (C) Copyright 2010
  3. * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * Designware ethernet IP driver for u-boot
  25. */
  26. #include <common.h>
  27. #include <miiphy.h>
  28. #include <malloc.h>
  29. #include <linux/err.h>
  30. #include <asm/io.h>
  31. #include "designware.h"
  32. static void tx_descs_init(struct eth_device *dev)
  33. {
  34. struct dw_eth_dev *priv = dev->priv;
  35. struct eth_dma_regs *dma_p = priv->dma_regs_p;
  36. struct dmamacdescr *desc_table_p = &priv->tx_mac_descrtable[0];
  37. char *txbuffs = &priv->txbuffs[0];
  38. struct dmamacdescr *desc_p;
  39. u32 idx;
  40. for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) {
  41. desc_p = &desc_table_p[idx];
  42. desc_p->dmamac_addr = &txbuffs[idx * CONFIG_ETH_BUFSIZE];
  43. desc_p->dmamac_next = &desc_table_p[idx + 1];
  44. #if defined(CONFIG_DW_ALTDESCRIPTOR)
  45. desc_p->txrx_status &= ~(DESC_TXSTS_TXINT | DESC_TXSTS_TXLAST |
  46. DESC_TXSTS_TXFIRST | DESC_TXSTS_TXCRCDIS | \
  47. DESC_TXSTS_TXCHECKINSCTRL | \
  48. DESC_TXSTS_TXRINGEND | DESC_TXSTS_TXPADDIS);
  49. desc_p->txrx_status |= DESC_TXSTS_TXCHAIN;
  50. desc_p->dmamac_cntl = 0;
  51. desc_p->txrx_status &= ~(DESC_TXSTS_MSK | DESC_TXSTS_OWNBYDMA);
  52. #else
  53. desc_p->dmamac_cntl = DESC_TXCTRL_TXCHAIN;
  54. desc_p->txrx_status = 0;
  55. #endif
  56. }
  57. /* Correcting the last pointer of the chain */
  58. desc_p->dmamac_next = &desc_table_p[0];
  59. writel((ulong)&desc_table_p[0], &dma_p->txdesclistaddr);
  60. }
  61. static void rx_descs_init(struct eth_device *dev)
  62. {
  63. struct dw_eth_dev *priv = dev->priv;
  64. struct eth_dma_regs *dma_p = priv->dma_regs_p;
  65. struct dmamacdescr *desc_table_p = &priv->rx_mac_descrtable[0];
  66. char *rxbuffs = &priv->rxbuffs[0];
  67. struct dmamacdescr *desc_p;
  68. u32 idx;
  69. for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
  70. desc_p = &desc_table_p[idx];
  71. desc_p->dmamac_addr = &rxbuffs[idx * CONFIG_ETH_BUFSIZE];
  72. desc_p->dmamac_next = &desc_table_p[idx + 1];
  73. desc_p->dmamac_cntl =
  74. (MAC_MAX_FRAME_SZ & DESC_RXCTRL_SIZE1MASK) | \
  75. DESC_RXCTRL_RXCHAIN;
  76. desc_p->txrx_status = DESC_RXSTS_OWNBYDMA;
  77. }
  78. /* Correcting the last pointer of the chain */
  79. desc_p->dmamac_next = &desc_table_p[0];
  80. writel((ulong)&desc_table_p[0], &dma_p->rxdesclistaddr);
  81. }
  82. static void descs_init(struct eth_device *dev)
  83. {
  84. tx_descs_init(dev);
  85. rx_descs_init(dev);
  86. }
  87. static int mac_reset(struct eth_device *dev)
  88. {
  89. struct dw_eth_dev *priv = dev->priv;
  90. struct eth_mac_regs *mac_p = priv->mac_regs_p;
  91. struct eth_dma_regs *dma_p = priv->dma_regs_p;
  92. int timeout = CONFIG_MACRESET_TIMEOUT;
  93. writel(DMAMAC_SRST, &dma_p->busmode);
  94. writel(MII_PORTSELECT, &mac_p->conf);
  95. do {
  96. if (!(readl(&dma_p->busmode) & DMAMAC_SRST))
  97. return 0;
  98. udelay(1000);
  99. } while (timeout--);
  100. return -1;
  101. }
  102. static int dw_write_hwaddr(struct eth_device *dev)
  103. {
  104. struct dw_eth_dev *priv = dev->priv;
  105. struct eth_mac_regs *mac_p = priv->mac_regs_p;
  106. u32 macid_lo, macid_hi;
  107. u8 *mac_id = &dev->enetaddr[0];
  108. macid_lo = mac_id[0] + (mac_id[1] << 8) + \
  109. (mac_id[2] << 16) + (mac_id[3] << 24);
  110. macid_hi = mac_id[4] + (mac_id[5] << 8);
  111. writel(macid_hi, &mac_p->macaddr0hi);
  112. writel(macid_lo, &mac_p->macaddr0lo);
  113. return 0;
  114. }
  115. static int dw_eth_init(struct eth_device *dev, bd_t *bis)
  116. {
  117. struct dw_eth_dev *priv = dev->priv;
  118. struct eth_mac_regs *mac_p = priv->mac_regs_p;
  119. struct eth_dma_regs *dma_p = priv->dma_regs_p;
  120. u32 conf;
  121. /* Reset ethernet hardware */
  122. if (mac_reset(dev) < 0)
  123. return -1;
  124. /* Resore the HW MAC address as it has been lost during MAC reset */
  125. dw_write_hwaddr(dev);
  126. writel(FIXEDBURST | PRIORXTX_41 | BURST_16,
  127. &dma_p->busmode);
  128. writel(FLUSHTXFIFO | readl(&dma_p->opmode), &dma_p->opmode);
  129. writel(STOREFORWARD | TXSECONDFRAME, &dma_p->opmode);
  130. conf = FRAMEBURSTENABLE | DISABLERXOWN;
  131. if (priv->speed != SPEED_1000M)
  132. conf |= MII_PORTSELECT;
  133. if (priv->duplex == FULL_DUPLEX)
  134. conf |= FULLDPLXMODE;
  135. writel(conf, &mac_p->conf);
  136. descs_init(dev);
  137. /*
  138. * Start/Enable xfer at dma as well as mac level
  139. */
  140. writel(readl(&dma_p->opmode) | RXSTART, &dma_p->opmode);
  141. writel(readl(&dma_p->opmode) | TXSTART, &dma_p->opmode);
  142. writel(readl(&mac_p->conf) | RXENABLE, &mac_p->conf);
  143. writel(readl(&mac_p->conf) | TXENABLE, &mac_p->conf);
  144. return 0;
  145. }
  146. static int dw_eth_send(struct eth_device *dev, volatile void *packet,
  147. int length)
  148. {
  149. struct dw_eth_dev *priv = dev->priv;
  150. struct eth_dma_regs *dma_p = priv->dma_regs_p;
  151. u32 desc_num = priv->tx_currdescnum;
  152. struct dmamacdescr *desc_p = &priv->tx_mac_descrtable[desc_num];
  153. /* Check if the descriptor is owned by CPU */
  154. if (desc_p->txrx_status & DESC_TXSTS_OWNBYDMA) {
  155. printf("CPU not owner of tx frame\n");
  156. return -1;
  157. }
  158. memcpy((void *)desc_p->dmamac_addr, (void *)packet, length);
  159. #if defined(CONFIG_DW_ALTDESCRIPTOR)
  160. desc_p->txrx_status |= DESC_TXSTS_TXFIRST | DESC_TXSTS_TXLAST;
  161. desc_p->dmamac_cntl |= (length << DESC_TXCTRL_SIZE1SHFT) & \
  162. DESC_TXCTRL_SIZE1MASK;
  163. desc_p->txrx_status &= ~(DESC_TXSTS_MSK);
  164. desc_p->txrx_status |= DESC_TXSTS_OWNBYDMA;
  165. #else
  166. desc_p->dmamac_cntl |= ((length << DESC_TXCTRL_SIZE1SHFT) & \
  167. DESC_TXCTRL_SIZE1MASK) | DESC_TXCTRL_TXLAST | \
  168. DESC_TXCTRL_TXFIRST;
  169. desc_p->txrx_status = DESC_TXSTS_OWNBYDMA;
  170. #endif
  171. /* Test the wrap-around condition. */
  172. if (++desc_num >= CONFIG_TX_DESCR_NUM)
  173. desc_num = 0;
  174. priv->tx_currdescnum = desc_num;
  175. /* Start the transmission */
  176. writel(POLL_DATA, &dma_p->txpolldemand);
  177. return 0;
  178. }
  179. static int dw_eth_recv(struct eth_device *dev)
  180. {
  181. struct dw_eth_dev *priv = dev->priv;
  182. u32 desc_num = priv->rx_currdescnum;
  183. struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
  184. u32 status = desc_p->txrx_status;
  185. int length = 0;
  186. /* Check if the owner is the CPU */
  187. if (!(status & DESC_RXSTS_OWNBYDMA)) {
  188. length = (status & DESC_RXSTS_FRMLENMSK) >> \
  189. DESC_RXSTS_FRMLENSHFT;
  190. NetReceive(desc_p->dmamac_addr, length);
  191. /*
  192. * Make the current descriptor valid again and go to
  193. * the next one
  194. */
  195. desc_p->txrx_status |= DESC_RXSTS_OWNBYDMA;
  196. /* Test the wrap-around condition. */
  197. if (++desc_num >= CONFIG_RX_DESCR_NUM)
  198. desc_num = 0;
  199. }
  200. priv->rx_currdescnum = desc_num;
  201. return length;
  202. }
  203. static void dw_eth_halt(struct eth_device *dev)
  204. {
  205. struct dw_eth_dev *priv = dev->priv;
  206. mac_reset(dev);
  207. priv->tx_currdescnum = priv->rx_currdescnum = 0;
  208. }
  209. static int eth_mdio_read(struct eth_device *dev, u8 addr, u8 reg, u16 *val)
  210. {
  211. struct dw_eth_dev *priv = dev->priv;
  212. struct eth_mac_regs *mac_p = priv->mac_regs_p;
  213. u32 miiaddr;
  214. int timeout = CONFIG_MDIO_TIMEOUT;
  215. miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) | \
  216. ((reg << MIIREGSHIFT) & MII_REGMSK);
  217. writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
  218. do {
  219. if (!(readl(&mac_p->miiaddr) & MII_BUSY)) {
  220. *val = readl(&mac_p->miidata);
  221. return 0;
  222. }
  223. udelay(1000);
  224. } while (timeout--);
  225. return -1;
  226. }
  227. static int eth_mdio_write(struct eth_device *dev, u8 addr, u8 reg, u16 val)
  228. {
  229. struct dw_eth_dev *priv = dev->priv;
  230. struct eth_mac_regs *mac_p = priv->mac_regs_p;
  231. u32 miiaddr;
  232. int ret = -1, timeout = CONFIG_MDIO_TIMEOUT;
  233. u16 value;
  234. writel(val, &mac_p->miidata);
  235. miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) | \
  236. ((reg << MIIREGSHIFT) & MII_REGMSK) | MII_WRITE;
  237. writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
  238. do {
  239. if (!(readl(&mac_p->miiaddr) & MII_BUSY)) {
  240. ret = 0;
  241. break;
  242. }
  243. udelay(1000);
  244. } while (timeout--);
  245. /* Needed as a fix for ST-Phy */
  246. eth_mdio_read(dev, addr, reg, &value);
  247. return ret;
  248. }
  249. #if defined(CONFIG_DW_SEARCH_PHY)
  250. static int find_phy(struct eth_device *dev)
  251. {
  252. int phy_addr = 0;
  253. u16 ctrl, oldctrl;
  254. do {
  255. eth_mdio_read(dev, phy_addr, MII_BMCR, &ctrl);
  256. oldctrl = ctrl & BMCR_ANENABLE;
  257. ctrl ^= BMCR_ANENABLE;
  258. eth_mdio_write(dev, phy_addr, MII_BMCR, ctrl);
  259. eth_mdio_read(dev, phy_addr, MII_BMCR, &ctrl);
  260. ctrl &= BMCR_ANENABLE;
  261. if (ctrl == oldctrl) {
  262. phy_addr++;
  263. } else {
  264. ctrl ^= BMCR_ANENABLE;
  265. eth_mdio_write(dev, phy_addr, MII_BMCR, ctrl);
  266. return phy_addr;
  267. }
  268. } while (phy_addr < 32);
  269. return -1;
  270. }
  271. #endif
  272. static int dw_reset_phy(struct eth_device *dev)
  273. {
  274. struct dw_eth_dev *priv = dev->priv;
  275. u16 ctrl;
  276. int timeout = CONFIG_PHYRESET_TIMEOUT;
  277. u32 phy_addr = priv->address;
  278. eth_mdio_write(dev, phy_addr, MII_BMCR, BMCR_RESET);
  279. do {
  280. eth_mdio_read(dev, phy_addr, MII_BMCR, &ctrl);
  281. if (!(ctrl & BMCR_RESET))
  282. break;
  283. udelay(1000);
  284. } while (timeout--);
  285. if (timeout < 0)
  286. return -1;
  287. #ifdef CONFIG_PHY_RESET_DELAY
  288. udelay(CONFIG_PHY_RESET_DELAY);
  289. #endif
  290. return 0;
  291. }
  292. static int configure_phy(struct eth_device *dev)
  293. {
  294. struct dw_eth_dev *priv = dev->priv;
  295. int phy_addr;
  296. u16 bmcr;
  297. #if defined(CONFIG_DW_AUTONEG)
  298. u16 bmsr;
  299. u32 timeout;
  300. u16 anlpar, btsr;
  301. #else
  302. u16 ctrl;
  303. #endif
  304. #if defined(CONFIG_DW_SEARCH_PHY)
  305. phy_addr = find_phy(dev);
  306. if (phy_addr > 0)
  307. priv->address = phy_addr;
  308. else
  309. return -1;
  310. #else
  311. phy_addr = priv->address;
  312. #endif
  313. if (dw_reset_phy(dev) < 0)
  314. return -1;
  315. #if defined(CONFIG_DW_AUTONEG)
  316. bmcr = BMCR_ANENABLE | BMCR_ANRESTART | BMCR_SPEED100 | \
  317. BMCR_FULLDPLX | BMCR_SPEED1000;
  318. #else
  319. bmcr = BMCR_SPEED100 | BMCR_FULLDPLX;
  320. #if defined(CONFIG_DW_SPEED10M)
  321. bmcr &= ~BMCR_SPEED100;
  322. #endif
  323. #if defined(CONFIG_DW_DUPLEXHALF)
  324. bmcr &= ~BMCR_FULLDPLX;
  325. #endif
  326. #endif
  327. if (eth_mdio_write(dev, phy_addr, MII_BMCR, bmcr) < 0)
  328. return -1;
  329. /* Read the phy status register and populate priv structure */
  330. #if defined(CONFIG_DW_AUTONEG)
  331. timeout = CONFIG_AUTONEG_TIMEOUT;
  332. do {
  333. eth_mdio_read(dev, phy_addr, MII_BMSR, &bmsr);
  334. if (bmsr & BMSR_ANEGCOMPLETE)
  335. break;
  336. udelay(1000);
  337. } while (timeout--);
  338. eth_mdio_read(dev, phy_addr, MII_LPA, &anlpar);
  339. eth_mdio_read(dev, phy_addr, MII_STAT1000, &btsr);
  340. if (btsr & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)) {
  341. priv->speed = SPEED_1000M;
  342. if (btsr & PHY_1000BTSR_1000FD)
  343. priv->duplex = FULL_DUPLEX;
  344. else
  345. priv->duplex = HALF_DUPLEX;
  346. } else {
  347. if (anlpar & LPA_100)
  348. priv->speed = SPEED_100M;
  349. else
  350. priv->speed = SPEED_10M;
  351. if (anlpar & (LPA_10FULL | LPA_100FULL))
  352. priv->duplex = FULL_DUPLEX;
  353. else
  354. priv->duplex = HALF_DUPLEX;
  355. }
  356. #else
  357. if (eth_mdio_read(dev, phy_addr, MII_BMCR, &ctrl) < 0)
  358. return -1;
  359. if (ctrl & BMCR_FULLDPLX)
  360. priv->duplex = FULL_DUPLEX;
  361. else
  362. priv->duplex = HALF_DUPLEX;
  363. if (ctrl & BMCR_SPEED1000)
  364. priv->speed = SPEED_1000M;
  365. else if (ctrl & BMCR_SPEED100)
  366. priv->speed = SPEED_100M;
  367. else
  368. priv->speed = SPEED_10M;
  369. #endif
  370. return 0;
  371. }
  372. #if defined(CONFIG_MII)
  373. static int dw_mii_read(const char *devname, u8 addr, u8 reg, u16 *val)
  374. {
  375. struct eth_device *dev;
  376. dev = eth_get_dev_by_name(devname);
  377. if (dev)
  378. eth_mdio_read(dev, addr, reg, val);
  379. return 0;
  380. }
  381. static int dw_mii_write(const char *devname, u8 addr, u8 reg, u16 val)
  382. {
  383. struct eth_device *dev;
  384. dev = eth_get_dev_by_name(devname);
  385. if (dev)
  386. eth_mdio_write(dev, addr, reg, val);
  387. return 0;
  388. }
  389. #endif
  390. int designware_initialize(u32 id, ulong base_addr, u32 phy_addr)
  391. {
  392. struct eth_device *dev;
  393. struct dw_eth_dev *priv;
  394. dev = (struct eth_device *) malloc(sizeof(struct eth_device));
  395. if (!dev)
  396. return -ENOMEM;
  397. /*
  398. * Since the priv structure contains the descriptors which need a strict
  399. * buswidth alignment, memalign is used to allocate memory
  400. */
  401. priv = (struct dw_eth_dev *) memalign(16, sizeof(struct dw_eth_dev));
  402. if (!priv) {
  403. free(dev);
  404. return -ENOMEM;
  405. }
  406. memset(dev, 0, sizeof(struct eth_device));
  407. memset(priv, 0, sizeof(struct dw_eth_dev));
  408. sprintf(dev->name, "mii%d", id);
  409. dev->iobase = (int)base_addr;
  410. dev->priv = priv;
  411. eth_getenv_enetaddr_by_index("eth", id, &dev->enetaddr[0]);
  412. priv->dev = dev;
  413. priv->mac_regs_p = (struct eth_mac_regs *)base_addr;
  414. priv->dma_regs_p = (struct eth_dma_regs *)(base_addr +
  415. DW_DMA_BASE_OFFSET);
  416. priv->address = phy_addr;
  417. if (mac_reset(dev) < 0)
  418. return -1;
  419. if (configure_phy(dev) < 0) {
  420. printf("Phy could not be configured\n");
  421. return -1;
  422. }
  423. dev->init = dw_eth_init;
  424. dev->send = dw_eth_send;
  425. dev->recv = dw_eth_recv;
  426. dev->halt = dw_eth_halt;
  427. dev->write_hwaddr = dw_write_hwaddr;
  428. eth_register(dev);
  429. #if defined(CONFIG_MII)
  430. miiphy_register(dev->name, dw_mii_read, dw_mii_write);
  431. #endif
  432. return 1;
  433. }