altera_tse.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. u16 length_or_eop,
  29. int generate_eop,
  30. int read_fixed,
  31. int write_fixed_or_sop)
  32. {
  33. u8 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. u32 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. u32 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_sgdma(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. alt_sgdma_construct_descriptor(
  141. tx_desc,
  142. tx_desc + 1,
  143. packet, /* read addr */
  144. NULL, /* write addr */
  145. length, /* length or EOP ,will change for each tx */
  146. 1, /* gen eop */
  147. 0, /* read fixed */
  148. 1 /* write fixed or sop */
  149. );
  150. /* send the packet */
  151. alt_sgdma_start_transfer(priv->sgdma_tx, tx_desc);
  152. alt_sgdma_wait_transfer(priv->sgdma_tx);
  153. debug("sent %d bytes\n", tx_desc->actual_bytes_transferred);
  154. return tx_desc->actual_bytes_transferred;
  155. }
  156. static int altera_tse_recv_sgdma(struct udevice *dev, int flags,
  157. uchar **packetp)
  158. {
  159. struct altera_tse_priv *priv = dev_get_priv(dev);
  160. struct alt_sgdma_descriptor *rx_desc = priv->rx_desc;
  161. int packet_length;
  162. if (rx_desc->descriptor_status &
  163. ALT_SGDMA_DESCRIPTOR_STATUS_TERMINATED_BY_EOP_MSK) {
  164. alt_sgdma_wait_transfer(priv->sgdma_rx);
  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_sgdma(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. alt_sgdma_construct_descriptor(
  178. rx_desc,
  179. rx_desc + 1,
  180. NULL, /* read addr */
  181. priv->rx_buf, /* write addr */
  182. 0, /* length or EOP */
  183. 0, /* gen eop */
  184. 0, /* read fixed */
  185. 0 /* write fixed or sop */
  186. );
  187. /* setup the sgdma */
  188. alt_sgdma_start_transfer(priv->sgdma_rx, rx_desc);
  189. debug("recv setup\n");
  190. return 0;
  191. }
  192. static void altera_tse_stop_mac(struct altera_tse_priv *priv)
  193. {
  194. struct alt_tse_mac *mac_dev = priv->mac_dev;
  195. u32 status;
  196. ulong ctime;
  197. /* reset the mac */
  198. writel(ALTERA_TSE_CMD_SW_RESET_MSK, &mac_dev->command_config);
  199. ctime = get_timer(0);
  200. while (1) {
  201. status = readl(&mac_dev->command_config);
  202. if (!(status & ALTERA_TSE_CMD_SW_RESET_MSK))
  203. break;
  204. if (get_timer(ctime) > ALT_TSE_SW_RESET_TIMEOUT) {
  205. debug("Reset mac timeout\n");
  206. break;
  207. }
  208. }
  209. }
  210. static void altera_tse_stop_sgdma(struct udevice *dev)
  211. {
  212. struct altera_tse_priv *priv = dev_get_priv(dev);
  213. struct alt_sgdma_registers *rx_sgdma = priv->sgdma_rx;
  214. struct alt_sgdma_registers *tx_sgdma = priv->sgdma_tx;
  215. struct alt_sgdma_descriptor *rx_desc = priv->rx_desc;
  216. int ret;
  217. /* clear rx desc & wait for sgdma to complete */
  218. rx_desc->descriptor_control = 0;
  219. writel(0, &rx_sgdma->control);
  220. ret = alt_sgdma_wait_transfer(rx_sgdma);
  221. if (ret == -ETIMEDOUT)
  222. writel(ALT_SGDMA_CONTROL_SOFTWARERESET_MSK,
  223. &rx_sgdma->control);
  224. writel(0, &tx_sgdma->control);
  225. ret = alt_sgdma_wait_transfer(tx_sgdma);
  226. if (ret == -ETIMEDOUT)
  227. writel(ALT_SGDMA_CONTROL_SOFTWARERESET_MSK,
  228. &tx_sgdma->control);
  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. u32 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. u32 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_send(struct udevice *dev, void *packet, int length)
  305. {
  306. struct altera_tse_priv *priv = dev_get_priv(dev);
  307. unsigned long tx_buf = (unsigned long)packet;
  308. flush_dcache_range(tx_buf, tx_buf + length);
  309. return priv->ops->send(dev, packet, length);
  310. }
  311. static int altera_tse_recv(struct udevice *dev, int flags, uchar **packetp)
  312. {
  313. struct altera_tse_priv *priv = dev_get_priv(dev);
  314. return priv->ops->recv(dev, flags, packetp);
  315. }
  316. static int altera_tse_free_pkt(struct udevice *dev, uchar *packet,
  317. int length)
  318. {
  319. struct altera_tse_priv *priv = dev_get_priv(dev);
  320. unsigned long rx_buf = (unsigned long)priv->rx_buf;
  321. invalidate_dcache_range(rx_buf, rx_buf + PKTSIZE_ALIGN);
  322. return priv->ops->free_pkt(dev, packet, length);
  323. }
  324. static void altera_tse_stop(struct udevice *dev)
  325. {
  326. struct altera_tse_priv *priv = dev_get_priv(dev);
  327. priv->ops->stop(dev);
  328. altera_tse_stop_mac(priv);
  329. }
  330. static int altera_tse_start(struct udevice *dev)
  331. {
  332. struct altera_tse_priv *priv = dev_get_priv(dev);
  333. struct alt_tse_mac *mac_dev = priv->mac_dev;
  334. u32 val;
  335. int ret;
  336. /* need to create sgdma */
  337. debug("Configuring rx desc\n");
  338. altera_tse_free_pkt(dev, priv->rx_buf, PKTSIZE_ALIGN);
  339. /* start TSE */
  340. debug("Configuring TSE Mac\n");
  341. /* Initialize MAC registers */
  342. writel(PKTSIZE_ALIGN, &mac_dev->max_frame_length);
  343. writel(priv->rx_fifo_depth - 16, &mac_dev->rx_sel_empty_threshold);
  344. writel(0, &mac_dev->rx_sel_full_threshold);
  345. writel(priv->tx_fifo_depth - 16, &mac_dev->tx_sel_empty_threshold);
  346. writel(0, &mac_dev->tx_sel_full_threshold);
  347. writel(8, &mac_dev->rx_almost_empty_threshold);
  348. writel(8, &mac_dev->rx_almost_full_threshold);
  349. writel(8, &mac_dev->tx_almost_empty_threshold);
  350. writel(3, &mac_dev->tx_almost_full_threshold);
  351. /* NO Shift */
  352. writel(0, &mac_dev->rx_cmd_stat);
  353. writel(0, &mac_dev->tx_cmd_stat);
  354. /* enable MAC */
  355. val = ALTERA_TSE_CMD_TX_ENA_MSK | ALTERA_TSE_CMD_RX_ENA_MSK;
  356. writel(val, &mac_dev->command_config);
  357. /* Start up the PHY */
  358. ret = phy_startup(priv->phydev);
  359. if (ret) {
  360. debug("Could not initialize PHY %s\n",
  361. priv->phydev->dev->name);
  362. return ret;
  363. }
  364. tse_adjust_link(priv, priv->phydev);
  365. if (!priv->phydev->link)
  366. return -EIO;
  367. return 0;
  368. }
  369. static const struct tse_ops tse_sgdma_ops = {
  370. .send = altera_tse_send_sgdma,
  371. .recv = altera_tse_recv_sgdma,
  372. .free_pkt = altera_tse_free_pkt_sgdma,
  373. .stop = altera_tse_stop_sgdma,
  374. };
  375. static int altera_tse_probe(struct udevice *dev)
  376. {
  377. struct eth_pdata *pdata = dev_get_platdata(dev);
  378. struct altera_tse_priv *priv = dev_get_priv(dev);
  379. void *blob = (void *)gd->fdt_blob;
  380. int node = dev->of_offset;
  381. const char *list, *end;
  382. const fdt32_t *cell;
  383. void *base, *desc_mem = NULL;
  384. unsigned long addr, size;
  385. int parent, addrc, sizec;
  386. int len, idx;
  387. int ret;
  388. priv->dma_type = dev_get_driver_data(dev);
  389. if (priv->dma_type == ALT_SGDMA)
  390. priv->ops = &tse_sgdma_ops;
  391. /*
  392. * decode regs. there are multiple reg tuples, and they need to
  393. * match with reg-names.
  394. */
  395. parent = fdt_parent_offset(blob, node);
  396. of_bus_default_count_cells(blob, parent, &addrc, &sizec);
  397. list = fdt_getprop(blob, node, "reg-names", &len);
  398. if (!list)
  399. return -ENOENT;
  400. end = list + len;
  401. cell = fdt_getprop(blob, node, "reg", &len);
  402. if (!cell)
  403. return -ENOENT;
  404. idx = 0;
  405. while (list < end) {
  406. addr = fdt_translate_address((void *)blob,
  407. node, cell + idx);
  408. size = fdt_addr_to_cpu(cell[idx + addrc]);
  409. base = ioremap(addr, size);
  410. len = strlen(list);
  411. if (strcmp(list, "control_port") == 0)
  412. priv->mac_dev = base;
  413. else if (strcmp(list, "rx_csr") == 0)
  414. priv->sgdma_rx = base;
  415. else if (strcmp(list, "tx_csr") == 0)
  416. priv->sgdma_tx = base;
  417. else if (strcmp(list, "s1") == 0)
  418. desc_mem = base;
  419. idx += addrc + sizec;
  420. list += (len + 1);
  421. }
  422. /* decode fifo depth */
  423. priv->rx_fifo_depth = fdtdec_get_int(blob, node,
  424. "rx-fifo-depth", 0);
  425. priv->tx_fifo_depth = fdtdec_get_int(blob, node,
  426. "tx-fifo-depth", 0);
  427. /* decode phy */
  428. addr = fdtdec_get_int(blob, node,
  429. "phy-handle", 0);
  430. addr = fdt_node_offset_by_phandle(blob, addr);
  431. priv->phyaddr = fdtdec_get_int(blob, addr,
  432. "reg", 0);
  433. /* init desc */
  434. if (priv->dma_type == ALT_SGDMA) {
  435. len = sizeof(struct alt_sgdma_descriptor) * 4;
  436. if (!desc_mem) {
  437. desc_mem = dma_alloc_coherent(len, &addr);
  438. if (!desc_mem)
  439. return -ENOMEM;
  440. }
  441. memset(desc_mem, 0, len);
  442. priv->tx_desc = desc_mem;
  443. priv->rx_desc = priv->tx_desc +
  444. 2 * sizeof(struct alt_sgdma_descriptor);
  445. }
  446. /* allocate recv packet buffer */
  447. priv->rx_buf = malloc_cache_aligned(PKTSIZE_ALIGN);
  448. if (!priv->rx_buf)
  449. return -ENOMEM;
  450. /* stop controller */
  451. debug("Reset TSE & SGDMAs\n");
  452. altera_tse_stop(dev);
  453. /* start the phy */
  454. priv->interface = pdata->phy_interface;
  455. tse_mdio_init(dev->name, priv);
  456. priv->bus = miiphy_get_dev_by_name(dev->name);
  457. ret = tse_phy_init(priv, dev);
  458. return ret;
  459. }
  460. static int altera_tse_ofdata_to_platdata(struct udevice *dev)
  461. {
  462. struct eth_pdata *pdata = dev_get_platdata(dev);
  463. const char *phy_mode;
  464. pdata->phy_interface = -1;
  465. phy_mode = fdt_getprop(gd->fdt_blob, dev->of_offset, "phy-mode", NULL);
  466. if (phy_mode)
  467. pdata->phy_interface = phy_get_interface_by_name(phy_mode);
  468. if (pdata->phy_interface == -1) {
  469. debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
  470. return -EINVAL;
  471. }
  472. return 0;
  473. }
  474. static const struct eth_ops altera_tse_ops = {
  475. .start = altera_tse_start,
  476. .send = altera_tse_send,
  477. .recv = altera_tse_recv,
  478. .free_pkt = altera_tse_free_pkt,
  479. .stop = altera_tse_stop,
  480. .write_hwaddr = altera_tse_write_hwaddr,
  481. };
  482. static const struct udevice_id altera_tse_ids[] = {
  483. { .compatible = "altr,tse-1.0", .data = ALT_SGDMA },
  484. {}
  485. };
  486. U_BOOT_DRIVER(altera_tse) = {
  487. .name = "altera_tse",
  488. .id = UCLASS_ETH,
  489. .of_match = altera_tse_ids,
  490. .ops = &altera_tse_ops,
  491. .ofdata_to_platdata = altera_tse_ofdata_to_platdata,
  492. .platdata_auto_alloc_size = sizeof(struct eth_pdata),
  493. .priv_auto_alloc_size = sizeof(struct altera_tse_priv),
  494. .probe = altera_tse_probe,
  495. };