designware.c 13 KB

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