designware.c 14 KB

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