lantiq_xrx200.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Lantiq / Intel PMAC driver for XRX200 SoCs
  4. *
  5. * Copyright (C) 2010 Lantiq Deutschland
  6. * Copyright (C) 2012 John Crispin <john@phrozen.org>
  7. * Copyright (C) 2017 - 2018 Hauke Mehrtens <hauke@hauke-m.de>
  8. */
  9. #include <linux/etherdevice.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/of_net.h>
  16. #include <linux/of_platform.h>
  17. #include <xway_dma.h>
  18. /* DMA */
  19. #define XRX200_DMA_DATA_LEN 0x600
  20. #define XRX200_DMA_RX 0
  21. #define XRX200_DMA_TX 1
  22. /* cpu port mac */
  23. #define PMAC_RX_IPG 0x0024
  24. #define PMAC_RX_IPG_MASK 0xf
  25. #define PMAC_HD_CTL 0x0000
  26. /* Add Ethernet header to packets from DMA to PMAC */
  27. #define PMAC_HD_CTL_ADD BIT(0)
  28. /* Add VLAN tag to Packets from DMA to PMAC */
  29. #define PMAC_HD_CTL_TAG BIT(1)
  30. /* Add CRC to packets from DMA to PMAC */
  31. #define PMAC_HD_CTL_AC BIT(2)
  32. /* Add status header to packets from PMAC to DMA */
  33. #define PMAC_HD_CTL_AS BIT(3)
  34. /* Remove CRC from packets from PMAC to DMA */
  35. #define PMAC_HD_CTL_RC BIT(4)
  36. /* Remove Layer-2 header from packets from PMAC to DMA */
  37. #define PMAC_HD_CTL_RL2 BIT(5)
  38. /* Status header is present from DMA to PMAC */
  39. #define PMAC_HD_CTL_RXSH BIT(6)
  40. /* Add special tag from PMAC to switch */
  41. #define PMAC_HD_CTL_AST BIT(7)
  42. /* Remove specail Tag from PMAC to DMA */
  43. #define PMAC_HD_CTL_RST BIT(8)
  44. /* Check CRC from DMA to PMAC */
  45. #define PMAC_HD_CTL_CCRC BIT(9)
  46. /* Enable reaction to Pause frames in the PMAC */
  47. #define PMAC_HD_CTL_FC BIT(10)
  48. struct xrx200_chan {
  49. int tx_free;
  50. struct napi_struct napi;
  51. struct ltq_dma_channel dma;
  52. struct sk_buff *skb[LTQ_DESC_NUM];
  53. struct xrx200_priv *priv;
  54. };
  55. struct xrx200_priv {
  56. struct clk *clk;
  57. struct xrx200_chan chan_tx;
  58. struct xrx200_chan chan_rx;
  59. struct net_device *net_dev;
  60. struct device *dev;
  61. __iomem void *pmac_reg;
  62. };
  63. static u32 xrx200_pmac_r32(struct xrx200_priv *priv, u32 offset)
  64. {
  65. return __raw_readl(priv->pmac_reg + offset);
  66. }
  67. static void xrx200_pmac_w32(struct xrx200_priv *priv, u32 val, u32 offset)
  68. {
  69. __raw_writel(val, priv->pmac_reg + offset);
  70. }
  71. static void xrx200_pmac_mask(struct xrx200_priv *priv, u32 clear, u32 set,
  72. u32 offset)
  73. {
  74. u32 val = xrx200_pmac_r32(priv, offset);
  75. val &= ~(clear);
  76. val |= set;
  77. xrx200_pmac_w32(priv, val, offset);
  78. }
  79. /* drop all the packets from the DMA ring */
  80. static void xrx200_flush_dma(struct xrx200_chan *ch)
  81. {
  82. int i;
  83. for (i = 0; i < LTQ_DESC_NUM; i++) {
  84. struct ltq_dma_desc *desc = &ch->dma.desc_base[ch->dma.desc];
  85. if ((desc->ctl & (LTQ_DMA_OWN | LTQ_DMA_C)) != LTQ_DMA_C)
  86. break;
  87. desc->ctl = LTQ_DMA_OWN | LTQ_DMA_RX_OFFSET(NET_IP_ALIGN) |
  88. XRX200_DMA_DATA_LEN;
  89. ch->dma.desc++;
  90. ch->dma.desc %= LTQ_DESC_NUM;
  91. }
  92. }
  93. static int xrx200_open(struct net_device *net_dev)
  94. {
  95. struct xrx200_priv *priv = netdev_priv(net_dev);
  96. napi_enable(&priv->chan_tx.napi);
  97. ltq_dma_open(&priv->chan_tx.dma);
  98. ltq_dma_enable_irq(&priv->chan_tx.dma);
  99. napi_enable(&priv->chan_rx.napi);
  100. ltq_dma_open(&priv->chan_rx.dma);
  101. /* The boot loader does not always deactivate the receiving of frames
  102. * on the ports and then some packets queue up in the PPE buffers.
  103. * They already passed the PMAC so they do not have the tags
  104. * configured here. Read the these packets here and drop them.
  105. * The HW should have written them into memory after 10us
  106. */
  107. usleep_range(20, 40);
  108. xrx200_flush_dma(&priv->chan_rx);
  109. ltq_dma_enable_irq(&priv->chan_rx.dma);
  110. netif_wake_queue(net_dev);
  111. return 0;
  112. }
  113. static int xrx200_close(struct net_device *net_dev)
  114. {
  115. struct xrx200_priv *priv = netdev_priv(net_dev);
  116. netif_stop_queue(net_dev);
  117. napi_disable(&priv->chan_rx.napi);
  118. ltq_dma_close(&priv->chan_rx.dma);
  119. napi_disable(&priv->chan_tx.napi);
  120. ltq_dma_close(&priv->chan_tx.dma);
  121. return 0;
  122. }
  123. static int xrx200_alloc_skb(struct xrx200_chan *ch)
  124. {
  125. struct sk_buff *skb = ch->skb[ch->dma.desc];
  126. dma_addr_t mapping;
  127. int ret = 0;
  128. ch->skb[ch->dma.desc] = netdev_alloc_skb_ip_align(ch->priv->net_dev,
  129. XRX200_DMA_DATA_LEN);
  130. if (!ch->skb[ch->dma.desc]) {
  131. ret = -ENOMEM;
  132. goto skip;
  133. }
  134. mapping = dma_map_single(ch->priv->dev, ch->skb[ch->dma.desc]->data,
  135. XRX200_DMA_DATA_LEN, DMA_FROM_DEVICE);
  136. if (unlikely(dma_mapping_error(ch->priv->dev, mapping))) {
  137. dev_kfree_skb_any(ch->skb[ch->dma.desc]);
  138. ch->skb[ch->dma.desc] = skb;
  139. ret = -ENOMEM;
  140. goto skip;
  141. }
  142. ch->dma.desc_base[ch->dma.desc].addr = mapping;
  143. /* Make sure the address is written before we give it to HW */
  144. wmb();
  145. skip:
  146. ch->dma.desc_base[ch->dma.desc].ctl =
  147. LTQ_DMA_OWN | LTQ_DMA_RX_OFFSET(NET_IP_ALIGN) |
  148. XRX200_DMA_DATA_LEN;
  149. return ret;
  150. }
  151. static int xrx200_hw_receive(struct xrx200_chan *ch)
  152. {
  153. struct xrx200_priv *priv = ch->priv;
  154. struct ltq_dma_desc *desc = &ch->dma.desc_base[ch->dma.desc];
  155. struct sk_buff *skb = ch->skb[ch->dma.desc];
  156. int len = (desc->ctl & LTQ_DMA_SIZE_MASK);
  157. struct net_device *net_dev = priv->net_dev;
  158. int ret;
  159. ret = xrx200_alloc_skb(ch);
  160. ch->dma.desc++;
  161. ch->dma.desc %= LTQ_DESC_NUM;
  162. if (ret) {
  163. net_dev->stats.rx_dropped++;
  164. netdev_err(net_dev, "failed to allocate new rx buffer\n");
  165. return ret;
  166. }
  167. skb_put(skb, len);
  168. skb->protocol = eth_type_trans(skb, net_dev);
  169. netif_receive_skb(skb);
  170. net_dev->stats.rx_packets++;
  171. net_dev->stats.rx_bytes += len - ETH_FCS_LEN;
  172. return 0;
  173. }
  174. static int xrx200_poll_rx(struct napi_struct *napi, int budget)
  175. {
  176. struct xrx200_chan *ch = container_of(napi,
  177. struct xrx200_chan, napi);
  178. int rx = 0;
  179. int ret;
  180. while (rx < budget) {
  181. struct ltq_dma_desc *desc = &ch->dma.desc_base[ch->dma.desc];
  182. if ((desc->ctl & (LTQ_DMA_OWN | LTQ_DMA_C)) == LTQ_DMA_C) {
  183. ret = xrx200_hw_receive(ch);
  184. if (ret)
  185. return ret;
  186. rx++;
  187. } else {
  188. break;
  189. }
  190. }
  191. if (rx < budget) {
  192. if (napi_complete_done(&ch->napi, rx))
  193. ltq_dma_enable_irq(&ch->dma);
  194. }
  195. return rx;
  196. }
  197. static int xrx200_tx_housekeeping(struct napi_struct *napi, int budget)
  198. {
  199. struct xrx200_chan *ch = container_of(napi,
  200. struct xrx200_chan, napi);
  201. struct net_device *net_dev = ch->priv->net_dev;
  202. int pkts = 0;
  203. int bytes = 0;
  204. netif_tx_lock(net_dev);
  205. while (pkts < budget) {
  206. struct ltq_dma_desc *desc = &ch->dma.desc_base[ch->tx_free];
  207. if ((desc->ctl & (LTQ_DMA_OWN | LTQ_DMA_C)) == LTQ_DMA_C) {
  208. struct sk_buff *skb = ch->skb[ch->tx_free];
  209. pkts++;
  210. bytes += skb->len;
  211. ch->skb[ch->tx_free] = NULL;
  212. consume_skb(skb);
  213. memset(&ch->dma.desc_base[ch->tx_free], 0,
  214. sizeof(struct ltq_dma_desc));
  215. ch->tx_free++;
  216. ch->tx_free %= LTQ_DESC_NUM;
  217. } else {
  218. break;
  219. }
  220. }
  221. net_dev->stats.tx_packets += pkts;
  222. net_dev->stats.tx_bytes += bytes;
  223. netdev_completed_queue(ch->priv->net_dev, pkts, bytes);
  224. netif_tx_unlock(net_dev);
  225. if (netif_queue_stopped(net_dev))
  226. netif_wake_queue(net_dev);
  227. if (pkts < budget) {
  228. if (napi_complete_done(&ch->napi, pkts))
  229. ltq_dma_enable_irq(&ch->dma);
  230. }
  231. return pkts;
  232. }
  233. static int xrx200_start_xmit(struct sk_buff *skb, struct net_device *net_dev)
  234. {
  235. struct xrx200_priv *priv = netdev_priv(net_dev);
  236. struct xrx200_chan *ch = &priv->chan_tx;
  237. struct ltq_dma_desc *desc = &ch->dma.desc_base[ch->dma.desc];
  238. u32 byte_offset;
  239. dma_addr_t mapping;
  240. int len;
  241. skb->dev = net_dev;
  242. if (skb_put_padto(skb, ETH_ZLEN)) {
  243. net_dev->stats.tx_dropped++;
  244. return NETDEV_TX_OK;
  245. }
  246. len = skb->len;
  247. if ((desc->ctl & (LTQ_DMA_OWN | LTQ_DMA_C)) || ch->skb[ch->dma.desc]) {
  248. netdev_err(net_dev, "tx ring full\n");
  249. netif_stop_queue(net_dev);
  250. return NETDEV_TX_BUSY;
  251. }
  252. ch->skb[ch->dma.desc] = skb;
  253. mapping = dma_map_single(priv->dev, skb->data, len, DMA_TO_DEVICE);
  254. if (unlikely(dma_mapping_error(priv->dev, mapping)))
  255. goto err_drop;
  256. /* dma needs to start on a 16 byte aligned address */
  257. byte_offset = mapping % 16;
  258. desc->addr = mapping - byte_offset;
  259. /* Make sure the address is written before we give it to HW */
  260. wmb();
  261. desc->ctl = LTQ_DMA_OWN | LTQ_DMA_SOP | LTQ_DMA_EOP |
  262. LTQ_DMA_TX_OFFSET(byte_offset) | (len & LTQ_DMA_SIZE_MASK);
  263. ch->dma.desc++;
  264. ch->dma.desc %= LTQ_DESC_NUM;
  265. if (ch->dma.desc == ch->tx_free)
  266. netif_stop_queue(net_dev);
  267. netdev_sent_queue(net_dev, len);
  268. return NETDEV_TX_OK;
  269. err_drop:
  270. dev_kfree_skb(skb);
  271. net_dev->stats.tx_dropped++;
  272. net_dev->stats.tx_errors++;
  273. return NETDEV_TX_OK;
  274. }
  275. static const struct net_device_ops xrx200_netdev_ops = {
  276. .ndo_open = xrx200_open,
  277. .ndo_stop = xrx200_close,
  278. .ndo_start_xmit = xrx200_start_xmit,
  279. .ndo_set_mac_address = eth_mac_addr,
  280. .ndo_validate_addr = eth_validate_addr,
  281. };
  282. static irqreturn_t xrx200_dma_irq(int irq, void *ptr)
  283. {
  284. struct xrx200_chan *ch = ptr;
  285. if (napi_schedule_prep(&ch->napi)) {
  286. ltq_dma_disable_irq(&ch->dma);
  287. __napi_schedule(&ch->napi);
  288. }
  289. ltq_dma_ack_irq(&ch->dma);
  290. return IRQ_HANDLED;
  291. }
  292. static int xrx200_dma_init(struct xrx200_priv *priv)
  293. {
  294. struct xrx200_chan *ch_rx = &priv->chan_rx;
  295. struct xrx200_chan *ch_tx = &priv->chan_tx;
  296. int ret = 0;
  297. int i;
  298. ltq_dma_init_port(DMA_PORT_ETOP);
  299. ch_rx->dma.nr = XRX200_DMA_RX;
  300. ch_rx->dma.dev = priv->dev;
  301. ch_rx->priv = priv;
  302. ltq_dma_alloc_rx(&ch_rx->dma);
  303. for (ch_rx->dma.desc = 0; ch_rx->dma.desc < LTQ_DESC_NUM;
  304. ch_rx->dma.desc++) {
  305. ret = xrx200_alloc_skb(ch_rx);
  306. if (ret)
  307. goto rx_free;
  308. }
  309. ch_rx->dma.desc = 0;
  310. ret = devm_request_irq(priv->dev, ch_rx->dma.irq, xrx200_dma_irq, 0,
  311. "xrx200_net_rx", &priv->chan_rx);
  312. if (ret) {
  313. dev_err(priv->dev, "failed to request RX irq %d\n",
  314. ch_rx->dma.irq);
  315. goto rx_ring_free;
  316. }
  317. ch_tx->dma.nr = XRX200_DMA_TX;
  318. ch_tx->dma.dev = priv->dev;
  319. ch_tx->priv = priv;
  320. ltq_dma_alloc_tx(&ch_tx->dma);
  321. ret = devm_request_irq(priv->dev, ch_tx->dma.irq, xrx200_dma_irq, 0,
  322. "xrx200_net_tx", &priv->chan_tx);
  323. if (ret) {
  324. dev_err(priv->dev, "failed to request TX irq %d\n",
  325. ch_tx->dma.irq);
  326. goto tx_free;
  327. }
  328. return ret;
  329. tx_free:
  330. ltq_dma_free(&ch_tx->dma);
  331. rx_ring_free:
  332. /* free the allocated RX ring */
  333. for (i = 0; i < LTQ_DESC_NUM; i++) {
  334. if (priv->chan_rx.skb[i])
  335. dev_kfree_skb_any(priv->chan_rx.skb[i]);
  336. }
  337. rx_free:
  338. ltq_dma_free(&ch_rx->dma);
  339. return ret;
  340. }
  341. static void xrx200_hw_cleanup(struct xrx200_priv *priv)
  342. {
  343. int i;
  344. ltq_dma_free(&priv->chan_tx.dma);
  345. ltq_dma_free(&priv->chan_rx.dma);
  346. /* free the allocated RX ring */
  347. for (i = 0; i < LTQ_DESC_NUM; i++)
  348. dev_kfree_skb_any(priv->chan_rx.skb[i]);
  349. }
  350. static int xrx200_probe(struct platform_device *pdev)
  351. {
  352. struct device *dev = &pdev->dev;
  353. struct device_node *np = dev->of_node;
  354. struct resource *res;
  355. struct xrx200_priv *priv;
  356. struct net_device *net_dev;
  357. const u8 *mac;
  358. int err;
  359. /* alloc the network device */
  360. net_dev = devm_alloc_etherdev(dev, sizeof(struct xrx200_priv));
  361. if (!net_dev)
  362. return -ENOMEM;
  363. priv = netdev_priv(net_dev);
  364. priv->net_dev = net_dev;
  365. priv->dev = dev;
  366. net_dev->netdev_ops = &xrx200_netdev_ops;
  367. SET_NETDEV_DEV(net_dev, dev);
  368. net_dev->min_mtu = ETH_ZLEN;
  369. net_dev->max_mtu = XRX200_DMA_DATA_LEN;
  370. /* load the memory ranges */
  371. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  372. if (!res) {
  373. dev_err(dev, "failed to get resources\n");
  374. return -ENOENT;
  375. }
  376. priv->pmac_reg = devm_ioremap_resource(dev, res);
  377. if (IS_ERR(priv->pmac_reg)) {
  378. dev_err(dev, "failed to request and remap io ranges\n");
  379. return PTR_ERR(priv->pmac_reg);
  380. }
  381. priv->chan_rx.dma.irq = platform_get_irq_byname(pdev, "rx");
  382. if (priv->chan_rx.dma.irq < 0)
  383. return -ENOENT;
  384. priv->chan_tx.dma.irq = platform_get_irq_byname(pdev, "tx");
  385. if (priv->chan_tx.dma.irq < 0)
  386. return -ENOENT;
  387. /* get the clock */
  388. priv->clk = devm_clk_get(dev, NULL);
  389. if (IS_ERR(priv->clk)) {
  390. dev_err(dev, "failed to get clock\n");
  391. return PTR_ERR(priv->clk);
  392. }
  393. mac = of_get_mac_address(np);
  394. if (!IS_ERR(mac))
  395. ether_addr_copy(net_dev->dev_addr, mac);
  396. else
  397. eth_hw_addr_random(net_dev);
  398. /* bring up the dma engine and IP core */
  399. err = xrx200_dma_init(priv);
  400. if (err)
  401. return err;
  402. /* enable clock gate */
  403. err = clk_prepare_enable(priv->clk);
  404. if (err)
  405. goto err_uninit_dma;
  406. /* set IPG to 12 */
  407. xrx200_pmac_mask(priv, PMAC_RX_IPG_MASK, 0xb, PMAC_RX_IPG);
  408. /* enable status header, enable CRC */
  409. xrx200_pmac_mask(priv, 0,
  410. PMAC_HD_CTL_RST | PMAC_HD_CTL_AST | PMAC_HD_CTL_RXSH |
  411. PMAC_HD_CTL_AS | PMAC_HD_CTL_AC | PMAC_HD_CTL_RC,
  412. PMAC_HD_CTL);
  413. /* setup NAPI */
  414. netif_napi_add(net_dev, &priv->chan_rx.napi, xrx200_poll_rx, 32);
  415. netif_tx_napi_add(net_dev, &priv->chan_tx.napi, xrx200_tx_housekeeping, 32);
  416. platform_set_drvdata(pdev, priv);
  417. err = register_netdev(net_dev);
  418. if (err)
  419. goto err_unprepare_clk;
  420. return 0;
  421. err_unprepare_clk:
  422. clk_disable_unprepare(priv->clk);
  423. err_uninit_dma:
  424. xrx200_hw_cleanup(priv);
  425. return err;
  426. }
  427. static int xrx200_remove(struct platform_device *pdev)
  428. {
  429. struct xrx200_priv *priv = platform_get_drvdata(pdev);
  430. struct net_device *net_dev = priv->net_dev;
  431. /* free stack related instances */
  432. netif_stop_queue(net_dev);
  433. netif_napi_del(&priv->chan_tx.napi);
  434. netif_napi_del(&priv->chan_rx.napi);
  435. /* remove the actual device */
  436. unregister_netdev(net_dev);
  437. /* release the clock */
  438. clk_disable_unprepare(priv->clk);
  439. /* shut down hardware */
  440. xrx200_hw_cleanup(priv);
  441. return 0;
  442. }
  443. static const struct of_device_id xrx200_match[] = {
  444. { .compatible = "lantiq,xrx200-net" },
  445. {},
  446. };
  447. MODULE_DEVICE_TABLE(of, xrx200_match);
  448. static struct platform_driver xrx200_driver = {
  449. .probe = xrx200_probe,
  450. .remove = xrx200_remove,
  451. .driver = {
  452. .name = "lantiq,xrx200-net",
  453. .of_match_table = xrx200_match,
  454. },
  455. };
  456. module_platform_driver(xrx200_driver);
  457. MODULE_AUTHOR("John Crispin <john@phrozen.org>");
  458. MODULE_DESCRIPTION("Lantiq SoC XRX200 ethernet");
  459. MODULE_LICENSE("GPL");