altera_tse.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /*
  2. * Altera 10/100/1000 triple speed ethernet mac driver
  3. *
  4. * Copyright (C) 2008 Altera Corporation.
  5. * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <common.h>
  12. #include <dm.h>
  13. #include <errno.h>
  14. #include <fdt_support.h>
  15. #include <memalign.h>
  16. #include <miiphy.h>
  17. #include <net.h>
  18. #include <asm/cache.h>
  19. #include <asm/dma-mapping.h>
  20. #include <asm/io.h>
  21. #include "altera_tse.h"
  22. DECLARE_GLOBAL_DATA_PTR;
  23. static inline void alt_sgdma_construct_descriptor(
  24. struct alt_sgdma_descriptor *desc,
  25. struct alt_sgdma_descriptor *next,
  26. void *read_addr,
  27. void *write_addr,
  28. unsigned short length_or_eop,
  29. int generate_eop,
  30. int read_fixed,
  31. int write_fixed_or_sop)
  32. {
  33. unsigned char val;
  34. /*
  35. * Mark the "next" descriptor as "not" owned by hardware. This prevents
  36. * The SGDMA controller from continuing to process the chain.
  37. */
  38. next->descriptor_control = next->descriptor_control &
  39. ~ALT_SGDMA_DESCRIPTOR_CONTROL_OWNED_BY_HW_MSK;
  40. memset(desc, 0, sizeof(struct alt_sgdma_descriptor));
  41. desc->source = virt_to_phys(read_addr);
  42. desc->destination = virt_to_phys(write_addr);
  43. desc->next = virt_to_phys(next);
  44. desc->bytes_to_transfer = length_or_eop;
  45. /*
  46. * Set the descriptor control block as follows:
  47. * - Set "owned by hardware" bit
  48. * - Optionally set "generate EOP" bit
  49. * - Optionally set the "read from fixed address" bit
  50. * - Optionally set the "write to fixed address bit (which serves
  51. * serves as a "generate SOP" control bit in memory-to-stream mode).
  52. * - Set the 4-bit atlantic channel, if specified
  53. *
  54. * Note this step is performed after all other descriptor information
  55. * has been filled out so that, if the controller already happens to be
  56. * pointing at this descriptor, it will not run (via the "owned by
  57. * hardware" bit) until all other descriptor has been set up.
  58. */
  59. val = ALT_SGDMA_DESCRIPTOR_CONTROL_OWNED_BY_HW_MSK;
  60. if (generate_eop)
  61. val |= ALT_SGDMA_DESCRIPTOR_CONTROL_GENERATE_EOP_MSK;
  62. if (read_fixed)
  63. val |= ALT_SGDMA_DESCRIPTOR_CONTROL_READ_FIXED_ADDRESS_MSK;
  64. if (write_fixed_or_sop)
  65. val |= ALT_SGDMA_DESCRIPTOR_CONTROL_WRITE_FIXED_ADDRESS_MSK;
  66. desc->descriptor_control = val;
  67. }
  68. static int alt_sgdma_wait_transfer(struct alt_sgdma_registers *regs)
  69. {
  70. int status;
  71. ulong ctime;
  72. /* Wait for the descriptor (chain) to complete */
  73. ctime = get_timer(0);
  74. while (1) {
  75. status = readl(&regs->status);
  76. if (!(status & ALT_SGDMA_STATUS_BUSY_MSK))
  77. break;
  78. if (get_timer(ctime) > ALT_TSE_SGDMA_BUSY_TIMEOUT) {
  79. status = -ETIMEDOUT;
  80. debug("sgdma timeout\n");
  81. break;
  82. }
  83. }
  84. /* Clear Run */
  85. writel(0, &regs->control);
  86. /* Clear status */
  87. writel(0xff, &regs->status);
  88. return status;
  89. }
  90. static int alt_sgdma_start_transfer(struct alt_sgdma_registers *regs,
  91. struct alt_sgdma_descriptor *desc)
  92. {
  93. unsigned int val;
  94. /* Point the controller at the descriptor */
  95. writel(virt_to_phys(desc), &regs->next_descriptor_pointer);
  96. /*
  97. * Set up SGDMA controller to:
  98. * - Disable interrupt generation
  99. * - Run once a valid descriptor is written to controller
  100. * - Stop on an error with any particular descriptor
  101. */
  102. val = ALT_SGDMA_CONTROL_RUN_MSK | ALT_SGDMA_CONTROL_STOP_DMA_ER_MSK;
  103. writel(val, &regs->control);
  104. return 0;
  105. }
  106. static void tse_adjust_link(struct altera_tse_priv *priv,
  107. struct phy_device *phydev)
  108. {
  109. struct alt_tse_mac *mac_dev = priv->mac_dev;
  110. unsigned int refvar;
  111. if (!phydev->link) {
  112. debug("%s: No link.\n", phydev->dev->name);
  113. return;
  114. }
  115. refvar = readl(&mac_dev->command_config);
  116. if (phydev->duplex)
  117. refvar |= ALTERA_TSE_CMD_HD_ENA_MSK;
  118. else
  119. refvar &= ~ALTERA_TSE_CMD_HD_ENA_MSK;
  120. switch (phydev->speed) {
  121. case 1000:
  122. refvar |= ALTERA_TSE_CMD_ETH_SPEED_MSK;
  123. refvar &= ~ALTERA_TSE_CMD_ENA_10_MSK;
  124. break;
  125. case 100:
  126. refvar &= ~ALTERA_TSE_CMD_ETH_SPEED_MSK;
  127. refvar &= ~ALTERA_TSE_CMD_ENA_10_MSK;
  128. break;
  129. case 10:
  130. refvar &= ~ALTERA_TSE_CMD_ETH_SPEED_MSK;
  131. refvar |= ALTERA_TSE_CMD_ENA_10_MSK;
  132. break;
  133. }
  134. writel(refvar, &mac_dev->command_config);
  135. }
  136. static int altera_tse_send(struct udevice *dev, void *packet, int length)
  137. {
  138. struct altera_tse_priv *priv = dev_get_priv(dev);
  139. struct alt_sgdma_descriptor *tx_desc = priv->tx_desc;
  140. unsigned long tx_buf = (unsigned long)packet;
  141. flush_dcache_range(tx_buf, tx_buf + length);
  142. alt_sgdma_construct_descriptor(
  143. tx_desc,
  144. tx_desc + 1,
  145. packet, /* read addr */
  146. NULL, /* write addr */
  147. length, /* length or EOP ,will change for each tx */
  148. 1, /* gen eop */
  149. 0, /* read fixed */
  150. 1 /* write fixed or sop */
  151. );
  152. /* send the packet */
  153. alt_sgdma_start_transfer(priv->sgdma_tx, tx_desc);
  154. alt_sgdma_wait_transfer(priv->sgdma_tx);
  155. debug("sent %d bytes\n", tx_desc->actual_bytes_transferred);
  156. return tx_desc->actual_bytes_transferred;
  157. }
  158. static int altera_tse_recv(struct udevice *dev, int flags, uchar **packetp)
  159. {
  160. struct altera_tse_priv *priv = dev_get_priv(dev);
  161. struct alt_sgdma_descriptor *rx_desc = priv->rx_desc;
  162. int packet_length;
  163. if (rx_desc->descriptor_status &
  164. ALT_SGDMA_DESCRIPTOR_STATUS_TERMINATED_BY_EOP_MSK) {
  165. packet_length = rx_desc->actual_bytes_transferred;
  166. debug("recv %d bytes\n", packet_length);
  167. *packetp = priv->rx_buf;
  168. return packet_length;
  169. }
  170. return -EAGAIN;
  171. }
  172. static int altera_tse_free_pkt(struct udevice *dev, uchar *packet,
  173. int length)
  174. {
  175. struct altera_tse_priv *priv = dev_get_priv(dev);
  176. struct alt_sgdma_descriptor *rx_desc = priv->rx_desc;
  177. unsigned long rx_buf = (unsigned long)priv->rx_buf;
  178. alt_sgdma_wait_transfer(priv->sgdma_rx);
  179. invalidate_dcache_range(rx_buf, rx_buf + PKTSIZE_ALIGN);
  180. alt_sgdma_construct_descriptor(
  181. rx_desc,
  182. rx_desc + 1,
  183. NULL, /* read addr */
  184. priv->rx_buf, /* write addr */
  185. 0, /* length or EOP */
  186. 0, /* gen eop */
  187. 0, /* read fixed */
  188. 0 /* write fixed or sop */
  189. );
  190. /* setup the sgdma */
  191. alt_sgdma_start_transfer(priv->sgdma_rx, rx_desc);
  192. debug("recv setup\n");
  193. return 0;
  194. }
  195. static void altera_tse_stop(struct udevice *dev)
  196. {
  197. struct altera_tse_priv *priv = dev_get_priv(dev);
  198. struct alt_tse_mac *mac_dev = priv->mac_dev;
  199. struct alt_sgdma_registers *rx_sgdma = priv->sgdma_rx;
  200. struct alt_sgdma_registers *tx_sgdma = priv->sgdma_tx;
  201. struct alt_sgdma_descriptor *rx_desc = priv->rx_desc;
  202. unsigned int status;
  203. int ret;
  204. ulong ctime;
  205. /* clear rx desc & wait for sgdma to complete */
  206. rx_desc->descriptor_control = 0;
  207. writel(0, &rx_sgdma->control);
  208. ret = alt_sgdma_wait_transfer(rx_sgdma);
  209. if (ret == -ETIMEDOUT)
  210. writel(ALT_SGDMA_CONTROL_SOFTWARERESET_MSK,
  211. &rx_sgdma->control);
  212. writel(0, &tx_sgdma->control);
  213. ret = alt_sgdma_wait_transfer(tx_sgdma);
  214. if (ret == -ETIMEDOUT)
  215. writel(ALT_SGDMA_CONTROL_SOFTWARERESET_MSK,
  216. &tx_sgdma->control);
  217. /* reset the mac */
  218. writel(ALTERA_TSE_CMD_SW_RESET_MSK, &mac_dev->command_config);
  219. ctime = get_timer(0);
  220. while (1) {
  221. status = readl(&mac_dev->command_config);
  222. if (!(status & ALTERA_TSE_CMD_SW_RESET_MSK))
  223. break;
  224. if (get_timer(ctime) > ALT_TSE_SW_RESET_TIMEOUT) {
  225. debug("Reset mac timeout\n");
  226. break;
  227. }
  228. }
  229. }
  230. static int tse_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
  231. {
  232. struct altera_tse_priv *priv = bus->priv;
  233. struct alt_tse_mac *mac_dev = priv->mac_dev;
  234. unsigned int value;
  235. /* set mdio address */
  236. writel(addr, &mac_dev->mdio_phy1_addr);
  237. /* get the data */
  238. value = readl(&mac_dev->mdio_phy1[reg]);
  239. return value & 0xffff;
  240. }
  241. static int tse_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
  242. u16 val)
  243. {
  244. struct altera_tse_priv *priv = bus->priv;
  245. struct alt_tse_mac *mac_dev = priv->mac_dev;
  246. /* set mdio address */
  247. writel(addr, &mac_dev->mdio_phy1_addr);
  248. /* set the data */
  249. writel(val, &mac_dev->mdio_phy1[reg]);
  250. return 0;
  251. }
  252. static int tse_mdio_init(const char *name, struct altera_tse_priv *priv)
  253. {
  254. struct mii_dev *bus = mdio_alloc();
  255. if (!bus) {
  256. printf("Failed to allocate MDIO bus\n");
  257. return -ENOMEM;
  258. }
  259. bus->read = tse_mdio_read;
  260. bus->write = tse_mdio_write;
  261. snprintf(bus->name, sizeof(bus->name), name);
  262. bus->priv = (void *)priv;
  263. return mdio_register(bus);
  264. }
  265. static int tse_phy_init(struct altera_tse_priv *priv, void *dev)
  266. {
  267. struct phy_device *phydev;
  268. unsigned int mask = 0xffffffff;
  269. if (priv->phyaddr)
  270. mask = 1 << priv->phyaddr;
  271. phydev = phy_find_by_mask(priv->bus, mask, priv->interface);
  272. if (!phydev)
  273. return -ENODEV;
  274. phy_connect_dev(phydev, dev);
  275. phydev->supported &= PHY_GBIT_FEATURES;
  276. phydev->advertising = phydev->supported;
  277. priv->phydev = phydev;
  278. phy_config(phydev);
  279. return 0;
  280. }
  281. static int altera_tse_write_hwaddr(struct udevice *dev)
  282. {
  283. struct altera_tse_priv *priv = dev_get_priv(dev);
  284. struct alt_tse_mac *mac_dev = priv->mac_dev;
  285. struct eth_pdata *pdata = dev_get_platdata(dev);
  286. u8 *hwaddr = pdata->enetaddr;
  287. unsigned int mac_lo, mac_hi;
  288. mac_lo = (hwaddr[3] << 24) | (hwaddr[2] << 16) |
  289. (hwaddr[1] << 8) | hwaddr[0];
  290. mac_hi = (hwaddr[5] << 8) | hwaddr[4];
  291. debug("Set MAC address to 0x%04x%08x\n", mac_hi, mac_lo);
  292. writel(mac_lo, &mac_dev->mac_addr_0);
  293. writel(mac_hi, &mac_dev->mac_addr_1);
  294. writel(mac_lo, &mac_dev->supp_mac_addr_0_0);
  295. writel(mac_hi, &mac_dev->supp_mac_addr_0_1);
  296. writel(mac_lo, &mac_dev->supp_mac_addr_1_0);
  297. writel(mac_hi, &mac_dev->supp_mac_addr_1_1);
  298. writel(mac_lo, &mac_dev->supp_mac_addr_2_0);
  299. writel(mac_hi, &mac_dev->supp_mac_addr_2_1);
  300. writel(mac_lo, &mac_dev->supp_mac_addr_3_0);
  301. writel(mac_hi, &mac_dev->supp_mac_addr_3_1);
  302. return 0;
  303. }
  304. static int altera_tse_start(struct udevice *dev)
  305. {
  306. struct altera_tse_priv *priv = dev_get_priv(dev);
  307. struct alt_tse_mac *mac_dev = priv->mac_dev;
  308. unsigned int val;
  309. int ret;
  310. /* need to create sgdma */
  311. debug("Configuring rx desc\n");
  312. altera_tse_free_pkt(dev, priv->rx_buf, PKTSIZE_ALIGN);
  313. /* start TSE */
  314. debug("Configuring TSE Mac\n");
  315. /* Initialize MAC registers */
  316. writel(PKTSIZE_ALIGN, &mac_dev->max_frame_length);
  317. writel(priv->rx_fifo_depth - 16, &mac_dev->rx_sel_empty_threshold);
  318. writel(0, &mac_dev->rx_sel_full_threshold);
  319. writel(priv->tx_fifo_depth - 16, &mac_dev->tx_sel_empty_threshold);
  320. writel(0, &mac_dev->tx_sel_full_threshold);
  321. writel(8, &mac_dev->rx_almost_empty_threshold);
  322. writel(8, &mac_dev->rx_almost_full_threshold);
  323. writel(8, &mac_dev->tx_almost_empty_threshold);
  324. writel(3, &mac_dev->tx_almost_full_threshold);
  325. /* NO Shift */
  326. writel(0, &mac_dev->rx_cmd_stat);
  327. writel(0, &mac_dev->tx_cmd_stat);
  328. /* enable MAC */
  329. val = ALTERA_TSE_CMD_TX_ENA_MSK | ALTERA_TSE_CMD_RX_ENA_MSK;
  330. writel(val, &mac_dev->command_config);
  331. /* Start up the PHY */
  332. ret = phy_startup(priv->phydev);
  333. if (ret) {
  334. debug("Could not initialize PHY %s\n",
  335. priv->phydev->dev->name);
  336. return ret;
  337. }
  338. tse_adjust_link(priv, priv->phydev);
  339. if (!priv->phydev->link)
  340. return -EIO;
  341. return 0;
  342. }
  343. static int altera_tse_probe(struct udevice *dev)
  344. {
  345. struct eth_pdata *pdata = dev_get_platdata(dev);
  346. struct altera_tse_priv *priv = dev_get_priv(dev);
  347. const void *blob = gd->fdt_blob;
  348. int node = dev->of_offset;
  349. const char *list, *end;
  350. const fdt32_t *cell;
  351. void *base, *desc_mem = NULL;
  352. unsigned long addr, size;
  353. int len, idx;
  354. int ret;
  355. /*
  356. * decode regs, assume address-cells and size-cells are both one.
  357. * there are multiple reg tuples, and they need to match with
  358. * reg-names.
  359. */
  360. list = fdt_getprop(blob, node, "reg-names", &len);
  361. if (!list)
  362. return -ENOENT;
  363. end = list + len;
  364. cell = fdt_getprop(blob, node, "reg", &len);
  365. if (!cell)
  366. return -ENOENT;
  367. idx = 0;
  368. while (list < end) {
  369. addr = fdt_translate_address((void *)blob,
  370. node, cell + idx);
  371. size = fdt_addr_to_cpu(cell[idx + 1]);
  372. base = ioremap(addr, size);
  373. len = strlen(list);
  374. if (strcmp(list, "control_port") == 0)
  375. priv->mac_dev = base;
  376. else if (strcmp(list, "rx_csr") == 0)
  377. priv->sgdma_rx = base;
  378. else if (strcmp(list, "tx_csr") == 0)
  379. priv->sgdma_tx = base;
  380. else if (strcmp(list, "s1") == 0)
  381. desc_mem = base;
  382. idx += 2;
  383. list += (len + 1);
  384. }
  385. /* decode fifo depth */
  386. priv->rx_fifo_depth = fdtdec_get_int(blob, node,
  387. "rx-fifo-depth", 0);
  388. priv->tx_fifo_depth = fdtdec_get_int(blob, node,
  389. "tx-fifo-depth", 0);
  390. /* decode phy */
  391. addr = fdtdec_get_int(blob, node,
  392. "phy-handle", 0);
  393. addr = fdt_node_offset_by_phandle(blob, addr);
  394. priv->phyaddr = fdtdec_get_int(blob, addr,
  395. "reg", 0);
  396. /* init desc */
  397. len = sizeof(struct alt_sgdma_descriptor) * 4;
  398. if (!desc_mem) {
  399. desc_mem = dma_alloc_coherent(len, &addr);
  400. if (!desc_mem)
  401. return -ENOMEM;
  402. }
  403. memset(desc_mem, 0, len);
  404. priv->tx_desc = desc_mem;
  405. priv->rx_desc = priv->tx_desc + 2;
  406. /* allocate recv packet buffer */
  407. priv->rx_buf = malloc_cache_aligned(PKTSIZE_ALIGN);
  408. if (!priv->rx_buf)
  409. return -ENOMEM;
  410. /* stop controller */
  411. debug("Reset TSE & SGDMAs\n");
  412. altera_tse_stop(dev);
  413. /* start the phy */
  414. priv->interface = pdata->phy_interface;
  415. tse_mdio_init(dev->name, priv);
  416. priv->bus = miiphy_get_dev_by_name(dev->name);
  417. ret = tse_phy_init(priv, dev);
  418. return ret;
  419. }
  420. static int altera_tse_ofdata_to_platdata(struct udevice *dev)
  421. {
  422. struct eth_pdata *pdata = dev_get_platdata(dev);
  423. const char *phy_mode;
  424. pdata->phy_interface = -1;
  425. phy_mode = fdt_getprop(gd->fdt_blob, dev->of_offset, "phy-mode", NULL);
  426. if (phy_mode)
  427. pdata->phy_interface = phy_get_interface_by_name(phy_mode);
  428. if (pdata->phy_interface == -1) {
  429. debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
  430. return -EINVAL;
  431. }
  432. return 0;
  433. }
  434. static const struct eth_ops altera_tse_ops = {
  435. .start = altera_tse_start,
  436. .send = altera_tse_send,
  437. .recv = altera_tse_recv,
  438. .free_pkt = altera_tse_free_pkt,
  439. .stop = altera_tse_stop,
  440. .write_hwaddr = altera_tse_write_hwaddr,
  441. };
  442. static const struct udevice_id altera_tse_ids[] = {
  443. { .compatible = "altr,tse-1.0", },
  444. { }
  445. };
  446. U_BOOT_DRIVER(altera_tse) = {
  447. .name = "altera_tse",
  448. .id = UCLASS_ETH,
  449. .of_match = altera_tse_ids,
  450. .ops = &altera_tse_ops,
  451. .ofdata_to_platdata = altera_tse_ofdata_to_platdata,
  452. .platdata_auto_alloc_size = sizeof(struct eth_pdata),
  453. .priv_auto_alloc_size = sizeof(struct altera_tse_priv),
  454. .probe = altera_tse_probe,
  455. };