designware.c 14 KB

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