lantiq_etop.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * Copyright (C) 2011 John Crispin <blogic@openwrt.org>
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/slab.h>
  8. #include <linux/errno.h>
  9. #include <linux/types.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/in.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/phy.h>
  16. #include <linux/ip.h>
  17. #include <linux/tcp.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/mm.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/ethtool.h>
  22. #include <linux/init.h>
  23. #include <linux/delay.h>
  24. #include <linux/io.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/module.h>
  27. #include <asm/checksum.h>
  28. #include <lantiq_soc.h>
  29. #include <xway_dma.h>
  30. #include <lantiq_platform.h>
  31. #define LTQ_ETOP_MDIO 0x11804
  32. #define MDIO_REQUEST 0x80000000
  33. #define MDIO_READ 0x40000000
  34. #define MDIO_ADDR_MASK 0x1f
  35. #define MDIO_ADDR_OFFSET 0x15
  36. #define MDIO_REG_MASK 0x1f
  37. #define MDIO_REG_OFFSET 0x10
  38. #define MDIO_VAL_MASK 0xffff
  39. #define PPE32_CGEN 0x800
  40. #define LQ_PPE32_ENET_MAC_CFG 0x1840
  41. #define LTQ_ETOP_ENETS0 0x11850
  42. #define LTQ_ETOP_MAC_DA0 0x1186C
  43. #define LTQ_ETOP_MAC_DA1 0x11870
  44. #define LTQ_ETOP_CFG 0x16020
  45. #define LTQ_ETOP_IGPLEN 0x16080
  46. #define MAX_DMA_CHAN 0x8
  47. #define MAX_DMA_CRC_LEN 0x4
  48. #define MAX_DMA_DATA_LEN 0x600
  49. #define ETOP_FTCU BIT(28)
  50. #define ETOP_MII_MASK 0xf
  51. #define ETOP_MII_NORMAL 0xd
  52. #define ETOP_MII_REVERSE 0xe
  53. #define ETOP_PLEN_UNDER 0x40
  54. #define ETOP_CGEN 0x800
  55. /* use 2 static channels for TX/RX */
  56. #define LTQ_ETOP_TX_CHANNEL 1
  57. #define LTQ_ETOP_RX_CHANNEL 6
  58. #define IS_TX(x) (x == LTQ_ETOP_TX_CHANNEL)
  59. #define IS_RX(x) (x == LTQ_ETOP_RX_CHANNEL)
  60. #define ltq_etop_r32(x) ltq_r32(ltq_etop_membase + (x))
  61. #define ltq_etop_w32(x, y) ltq_w32(x, ltq_etop_membase + (y))
  62. #define ltq_etop_w32_mask(x, y, z) \
  63. ltq_w32_mask(x, y, ltq_etop_membase + (z))
  64. #define DRV_VERSION "1.0"
  65. static void __iomem *ltq_etop_membase;
  66. struct ltq_etop_chan {
  67. int idx;
  68. int tx_free;
  69. struct net_device *netdev;
  70. struct napi_struct napi;
  71. struct ltq_dma_channel dma;
  72. struct sk_buff *skb[LTQ_DESC_NUM];
  73. };
  74. struct ltq_etop_priv {
  75. struct net_device *netdev;
  76. struct platform_device *pdev;
  77. struct ltq_eth_data *pldata;
  78. struct resource *res;
  79. struct mii_bus *mii_bus;
  80. struct ltq_etop_chan ch[MAX_DMA_CHAN];
  81. int tx_free[MAX_DMA_CHAN >> 1];
  82. spinlock_t lock;
  83. };
  84. static int
  85. ltq_etop_alloc_skb(struct ltq_etop_chan *ch)
  86. {
  87. struct ltq_etop_priv *priv = netdev_priv(ch->netdev);
  88. ch->skb[ch->dma.desc] = netdev_alloc_skb(ch->netdev, MAX_DMA_DATA_LEN);
  89. if (!ch->skb[ch->dma.desc])
  90. return -ENOMEM;
  91. ch->dma.desc_base[ch->dma.desc].addr = dma_map_single(&priv->pdev->dev,
  92. ch->skb[ch->dma.desc]->data, MAX_DMA_DATA_LEN,
  93. DMA_FROM_DEVICE);
  94. ch->dma.desc_base[ch->dma.desc].addr =
  95. CPHYSADDR(ch->skb[ch->dma.desc]->data);
  96. ch->dma.desc_base[ch->dma.desc].ctl =
  97. LTQ_DMA_OWN | LTQ_DMA_RX_OFFSET(NET_IP_ALIGN) |
  98. MAX_DMA_DATA_LEN;
  99. skb_reserve(ch->skb[ch->dma.desc], NET_IP_ALIGN);
  100. return 0;
  101. }
  102. static void
  103. ltq_etop_hw_receive(struct ltq_etop_chan *ch)
  104. {
  105. struct ltq_etop_priv *priv = netdev_priv(ch->netdev);
  106. struct ltq_dma_desc *desc = &ch->dma.desc_base[ch->dma.desc];
  107. struct sk_buff *skb = ch->skb[ch->dma.desc];
  108. int len = (desc->ctl & LTQ_DMA_SIZE_MASK) - MAX_DMA_CRC_LEN;
  109. unsigned long flags;
  110. spin_lock_irqsave(&priv->lock, flags);
  111. if (ltq_etop_alloc_skb(ch)) {
  112. netdev_err(ch->netdev,
  113. "failed to allocate new rx buffer, stopping DMA\n");
  114. ltq_dma_close(&ch->dma);
  115. }
  116. ch->dma.desc++;
  117. ch->dma.desc %= LTQ_DESC_NUM;
  118. spin_unlock_irqrestore(&priv->lock, flags);
  119. skb_put(skb, len);
  120. skb->protocol = eth_type_trans(skb, ch->netdev);
  121. netif_receive_skb(skb);
  122. }
  123. static int
  124. ltq_etop_poll_rx(struct napi_struct *napi, int budget)
  125. {
  126. struct ltq_etop_chan *ch = container_of(napi,
  127. struct ltq_etop_chan, napi);
  128. int work_done = 0;
  129. while (work_done < budget) {
  130. struct ltq_dma_desc *desc = &ch->dma.desc_base[ch->dma.desc];
  131. if ((desc->ctl & (LTQ_DMA_OWN | LTQ_DMA_C)) != LTQ_DMA_C)
  132. break;
  133. ltq_etop_hw_receive(ch);
  134. work_done++;
  135. }
  136. if (work_done < budget) {
  137. napi_complete_done(&ch->napi, work_done);
  138. ltq_dma_ack_irq(&ch->dma);
  139. }
  140. return work_done;
  141. }
  142. static int
  143. ltq_etop_poll_tx(struct napi_struct *napi, int budget)
  144. {
  145. struct ltq_etop_chan *ch =
  146. container_of(napi, struct ltq_etop_chan, napi);
  147. struct ltq_etop_priv *priv = netdev_priv(ch->netdev);
  148. struct netdev_queue *txq =
  149. netdev_get_tx_queue(ch->netdev, ch->idx >> 1);
  150. unsigned long flags;
  151. spin_lock_irqsave(&priv->lock, flags);
  152. while ((ch->dma.desc_base[ch->tx_free].ctl &
  153. (LTQ_DMA_OWN | LTQ_DMA_C)) == LTQ_DMA_C) {
  154. dev_kfree_skb_any(ch->skb[ch->tx_free]);
  155. ch->skb[ch->tx_free] = NULL;
  156. memset(&ch->dma.desc_base[ch->tx_free], 0,
  157. sizeof(struct ltq_dma_desc));
  158. ch->tx_free++;
  159. ch->tx_free %= LTQ_DESC_NUM;
  160. }
  161. spin_unlock_irqrestore(&priv->lock, flags);
  162. if (netif_tx_queue_stopped(txq))
  163. netif_tx_start_queue(txq);
  164. napi_complete(&ch->napi);
  165. ltq_dma_ack_irq(&ch->dma);
  166. return 1;
  167. }
  168. static irqreturn_t
  169. ltq_etop_dma_irq(int irq, void *_priv)
  170. {
  171. struct ltq_etop_priv *priv = _priv;
  172. int ch = irq - LTQ_DMA_CH0_INT;
  173. napi_schedule(&priv->ch[ch].napi);
  174. return IRQ_HANDLED;
  175. }
  176. static void
  177. ltq_etop_free_channel(struct net_device *dev, struct ltq_etop_chan *ch)
  178. {
  179. struct ltq_etop_priv *priv = netdev_priv(dev);
  180. ltq_dma_free(&ch->dma);
  181. if (ch->dma.irq)
  182. free_irq(ch->dma.irq, priv);
  183. if (IS_RX(ch->idx)) {
  184. int desc;
  185. for (desc = 0; desc < LTQ_DESC_NUM; desc++)
  186. dev_kfree_skb_any(ch->skb[ch->dma.desc]);
  187. }
  188. }
  189. static void
  190. ltq_etop_hw_exit(struct net_device *dev)
  191. {
  192. struct ltq_etop_priv *priv = netdev_priv(dev);
  193. int i;
  194. ltq_pmu_disable(PMU_PPE);
  195. for (i = 0; i < MAX_DMA_CHAN; i++)
  196. if (IS_TX(i) || IS_RX(i))
  197. ltq_etop_free_channel(dev, &priv->ch[i]);
  198. }
  199. static int
  200. ltq_etop_hw_init(struct net_device *dev)
  201. {
  202. struct ltq_etop_priv *priv = netdev_priv(dev);
  203. int i;
  204. ltq_pmu_enable(PMU_PPE);
  205. switch (priv->pldata->mii_mode) {
  206. case PHY_INTERFACE_MODE_RMII:
  207. ltq_etop_w32_mask(ETOP_MII_MASK,
  208. ETOP_MII_REVERSE, LTQ_ETOP_CFG);
  209. break;
  210. case PHY_INTERFACE_MODE_MII:
  211. ltq_etop_w32_mask(ETOP_MII_MASK,
  212. ETOP_MII_NORMAL, LTQ_ETOP_CFG);
  213. break;
  214. default:
  215. netdev_err(dev, "unknown mii mode %d\n",
  216. priv->pldata->mii_mode);
  217. return -ENOTSUPP;
  218. }
  219. /* enable crc generation */
  220. ltq_etop_w32(PPE32_CGEN, LQ_PPE32_ENET_MAC_CFG);
  221. ltq_dma_init_port(DMA_PORT_ETOP);
  222. for (i = 0; i < MAX_DMA_CHAN; i++) {
  223. int irq = LTQ_DMA_CH0_INT + i;
  224. struct ltq_etop_chan *ch = &priv->ch[i];
  225. ch->idx = ch->dma.nr = i;
  226. ch->dma.dev = &priv->pdev->dev;
  227. if (IS_TX(i)) {
  228. ltq_dma_alloc_tx(&ch->dma);
  229. request_irq(irq, ltq_etop_dma_irq, 0, "etop_tx", priv);
  230. } else if (IS_RX(i)) {
  231. ltq_dma_alloc_rx(&ch->dma);
  232. for (ch->dma.desc = 0; ch->dma.desc < LTQ_DESC_NUM;
  233. ch->dma.desc++)
  234. if (ltq_etop_alloc_skb(ch))
  235. return -ENOMEM;
  236. ch->dma.desc = 0;
  237. request_irq(irq, ltq_etop_dma_irq, 0, "etop_rx", priv);
  238. }
  239. ch->dma.irq = irq;
  240. }
  241. return 0;
  242. }
  243. static void
  244. ltq_etop_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  245. {
  246. strlcpy(info->driver, "Lantiq ETOP", sizeof(info->driver));
  247. strlcpy(info->bus_info, "internal", sizeof(info->bus_info));
  248. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  249. }
  250. static const struct ethtool_ops ltq_etop_ethtool_ops = {
  251. .get_drvinfo = ltq_etop_get_drvinfo,
  252. .nway_reset = phy_ethtool_nway_reset,
  253. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  254. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  255. };
  256. static int
  257. ltq_etop_mdio_wr(struct mii_bus *bus, int phy_addr, int phy_reg, u16 phy_data)
  258. {
  259. u32 val = MDIO_REQUEST |
  260. ((phy_addr & MDIO_ADDR_MASK) << MDIO_ADDR_OFFSET) |
  261. ((phy_reg & MDIO_REG_MASK) << MDIO_REG_OFFSET) |
  262. phy_data;
  263. while (ltq_etop_r32(LTQ_ETOP_MDIO) & MDIO_REQUEST)
  264. ;
  265. ltq_etop_w32(val, LTQ_ETOP_MDIO);
  266. return 0;
  267. }
  268. static int
  269. ltq_etop_mdio_rd(struct mii_bus *bus, int phy_addr, int phy_reg)
  270. {
  271. u32 val = MDIO_REQUEST | MDIO_READ |
  272. ((phy_addr & MDIO_ADDR_MASK) << MDIO_ADDR_OFFSET) |
  273. ((phy_reg & MDIO_REG_MASK) << MDIO_REG_OFFSET);
  274. while (ltq_etop_r32(LTQ_ETOP_MDIO) & MDIO_REQUEST)
  275. ;
  276. ltq_etop_w32(val, LTQ_ETOP_MDIO);
  277. while (ltq_etop_r32(LTQ_ETOP_MDIO) & MDIO_REQUEST)
  278. ;
  279. val = ltq_etop_r32(LTQ_ETOP_MDIO) & MDIO_VAL_MASK;
  280. return val;
  281. }
  282. static void
  283. ltq_etop_mdio_link(struct net_device *dev)
  284. {
  285. /* nothing to do */
  286. }
  287. static int
  288. ltq_etop_mdio_probe(struct net_device *dev)
  289. {
  290. struct ltq_etop_priv *priv = netdev_priv(dev);
  291. struct phy_device *phydev;
  292. phydev = phy_find_first(priv->mii_bus);
  293. if (!phydev) {
  294. netdev_err(dev, "no PHY found\n");
  295. return -ENODEV;
  296. }
  297. phydev = phy_connect(dev, phydev_name(phydev),
  298. &ltq_etop_mdio_link, priv->pldata->mii_mode);
  299. if (IS_ERR(phydev)) {
  300. netdev_err(dev, "Could not attach to PHY\n");
  301. return PTR_ERR(phydev);
  302. }
  303. phy_set_max_speed(phydev, SPEED_100);
  304. phy_attached_info(phydev);
  305. return 0;
  306. }
  307. static int
  308. ltq_etop_mdio_init(struct net_device *dev)
  309. {
  310. struct ltq_etop_priv *priv = netdev_priv(dev);
  311. int err;
  312. priv->mii_bus = mdiobus_alloc();
  313. if (!priv->mii_bus) {
  314. netdev_err(dev, "failed to allocate mii bus\n");
  315. err = -ENOMEM;
  316. goto err_out;
  317. }
  318. priv->mii_bus->priv = dev;
  319. priv->mii_bus->read = ltq_etop_mdio_rd;
  320. priv->mii_bus->write = ltq_etop_mdio_wr;
  321. priv->mii_bus->name = "ltq_mii";
  322. snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  323. priv->pdev->name, priv->pdev->id);
  324. if (mdiobus_register(priv->mii_bus)) {
  325. err = -ENXIO;
  326. goto err_out_free_mdiobus;
  327. }
  328. if (ltq_etop_mdio_probe(dev)) {
  329. err = -ENXIO;
  330. goto err_out_unregister_bus;
  331. }
  332. return 0;
  333. err_out_unregister_bus:
  334. mdiobus_unregister(priv->mii_bus);
  335. err_out_free_mdiobus:
  336. mdiobus_free(priv->mii_bus);
  337. err_out:
  338. return err;
  339. }
  340. static void
  341. ltq_etop_mdio_cleanup(struct net_device *dev)
  342. {
  343. struct ltq_etop_priv *priv = netdev_priv(dev);
  344. phy_disconnect(dev->phydev);
  345. mdiobus_unregister(priv->mii_bus);
  346. mdiobus_free(priv->mii_bus);
  347. }
  348. static int
  349. ltq_etop_open(struct net_device *dev)
  350. {
  351. struct ltq_etop_priv *priv = netdev_priv(dev);
  352. int i;
  353. for (i = 0; i < MAX_DMA_CHAN; i++) {
  354. struct ltq_etop_chan *ch = &priv->ch[i];
  355. if (!IS_TX(i) && (!IS_RX(i)))
  356. continue;
  357. ltq_dma_open(&ch->dma);
  358. ltq_dma_enable_irq(&ch->dma);
  359. napi_enable(&ch->napi);
  360. }
  361. phy_start(dev->phydev);
  362. netif_tx_start_all_queues(dev);
  363. return 0;
  364. }
  365. static int
  366. ltq_etop_stop(struct net_device *dev)
  367. {
  368. struct ltq_etop_priv *priv = netdev_priv(dev);
  369. int i;
  370. netif_tx_stop_all_queues(dev);
  371. phy_stop(dev->phydev);
  372. for (i = 0; i < MAX_DMA_CHAN; i++) {
  373. struct ltq_etop_chan *ch = &priv->ch[i];
  374. if (!IS_RX(i) && !IS_TX(i))
  375. continue;
  376. napi_disable(&ch->napi);
  377. ltq_dma_close(&ch->dma);
  378. }
  379. return 0;
  380. }
  381. static int
  382. ltq_etop_tx(struct sk_buff *skb, struct net_device *dev)
  383. {
  384. int queue = skb_get_queue_mapping(skb);
  385. struct netdev_queue *txq = netdev_get_tx_queue(dev, queue);
  386. struct ltq_etop_priv *priv = netdev_priv(dev);
  387. struct ltq_etop_chan *ch = &priv->ch[(queue << 1) | 1];
  388. struct ltq_dma_desc *desc = &ch->dma.desc_base[ch->dma.desc];
  389. int len;
  390. unsigned long flags;
  391. u32 byte_offset;
  392. len = skb->len < ETH_ZLEN ? ETH_ZLEN : skb->len;
  393. if ((desc->ctl & (LTQ_DMA_OWN | LTQ_DMA_C)) || ch->skb[ch->dma.desc]) {
  394. dev_kfree_skb_any(skb);
  395. netdev_err(dev, "tx ring full\n");
  396. netif_tx_stop_queue(txq);
  397. return NETDEV_TX_BUSY;
  398. }
  399. /* dma needs to start on a 16 byte aligned address */
  400. byte_offset = CPHYSADDR(skb->data) % 16;
  401. ch->skb[ch->dma.desc] = skb;
  402. netif_trans_update(dev);
  403. spin_lock_irqsave(&priv->lock, flags);
  404. desc->addr = ((unsigned int) dma_map_single(&priv->pdev->dev, skb->data, len,
  405. DMA_TO_DEVICE)) - byte_offset;
  406. wmb();
  407. desc->ctl = LTQ_DMA_OWN | LTQ_DMA_SOP | LTQ_DMA_EOP |
  408. LTQ_DMA_TX_OFFSET(byte_offset) | (len & LTQ_DMA_SIZE_MASK);
  409. ch->dma.desc++;
  410. ch->dma.desc %= LTQ_DESC_NUM;
  411. spin_unlock_irqrestore(&priv->lock, flags);
  412. if (ch->dma.desc_base[ch->dma.desc].ctl & LTQ_DMA_OWN)
  413. netif_tx_stop_queue(txq);
  414. return NETDEV_TX_OK;
  415. }
  416. static int
  417. ltq_etop_change_mtu(struct net_device *dev, int new_mtu)
  418. {
  419. struct ltq_etop_priv *priv = netdev_priv(dev);
  420. unsigned long flags;
  421. dev->mtu = new_mtu;
  422. spin_lock_irqsave(&priv->lock, flags);
  423. ltq_etop_w32((ETOP_PLEN_UNDER << 16) | new_mtu, LTQ_ETOP_IGPLEN);
  424. spin_unlock_irqrestore(&priv->lock, flags);
  425. return 0;
  426. }
  427. static int
  428. ltq_etop_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  429. {
  430. /* TODO: mii-toll reports "No MII transceiver present!." ?!*/
  431. return phy_mii_ioctl(dev->phydev, rq, cmd);
  432. }
  433. static int
  434. ltq_etop_set_mac_address(struct net_device *dev, void *p)
  435. {
  436. int ret = eth_mac_addr(dev, p);
  437. if (!ret) {
  438. struct ltq_etop_priv *priv = netdev_priv(dev);
  439. unsigned long flags;
  440. /* store the mac for the unicast filter */
  441. spin_lock_irqsave(&priv->lock, flags);
  442. ltq_etop_w32(*((u32 *)dev->dev_addr), LTQ_ETOP_MAC_DA0);
  443. ltq_etop_w32(*((u16 *)&dev->dev_addr[4]) << 16,
  444. LTQ_ETOP_MAC_DA1);
  445. spin_unlock_irqrestore(&priv->lock, flags);
  446. }
  447. return ret;
  448. }
  449. static void
  450. ltq_etop_set_multicast_list(struct net_device *dev)
  451. {
  452. struct ltq_etop_priv *priv = netdev_priv(dev);
  453. unsigned long flags;
  454. /* ensure that the unicast filter is not enabled in promiscious mode */
  455. spin_lock_irqsave(&priv->lock, flags);
  456. if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI))
  457. ltq_etop_w32_mask(ETOP_FTCU, 0, LTQ_ETOP_ENETS0);
  458. else
  459. ltq_etop_w32_mask(0, ETOP_FTCU, LTQ_ETOP_ENETS0);
  460. spin_unlock_irqrestore(&priv->lock, flags);
  461. }
  462. static int
  463. ltq_etop_init(struct net_device *dev)
  464. {
  465. struct ltq_etop_priv *priv = netdev_priv(dev);
  466. struct sockaddr mac;
  467. int err;
  468. bool random_mac = false;
  469. dev->watchdog_timeo = 10 * HZ;
  470. err = ltq_etop_hw_init(dev);
  471. if (err)
  472. goto err_hw;
  473. ltq_etop_change_mtu(dev, 1500);
  474. memcpy(&mac, &priv->pldata->mac, sizeof(struct sockaddr));
  475. if (!is_valid_ether_addr(mac.sa_data)) {
  476. pr_warn("etop: invalid MAC, using random\n");
  477. eth_random_addr(mac.sa_data);
  478. random_mac = true;
  479. }
  480. err = ltq_etop_set_mac_address(dev, &mac);
  481. if (err)
  482. goto err_netdev;
  483. /* Set addr_assign_type here, ltq_etop_set_mac_address would reset it. */
  484. if (random_mac)
  485. dev->addr_assign_type = NET_ADDR_RANDOM;
  486. ltq_etop_set_multicast_list(dev);
  487. err = ltq_etop_mdio_init(dev);
  488. if (err)
  489. goto err_netdev;
  490. return 0;
  491. err_netdev:
  492. unregister_netdev(dev);
  493. free_netdev(dev);
  494. err_hw:
  495. ltq_etop_hw_exit(dev);
  496. return err;
  497. }
  498. static void
  499. ltq_etop_tx_timeout(struct net_device *dev)
  500. {
  501. int err;
  502. ltq_etop_hw_exit(dev);
  503. err = ltq_etop_hw_init(dev);
  504. if (err)
  505. goto err_hw;
  506. netif_trans_update(dev);
  507. netif_wake_queue(dev);
  508. return;
  509. err_hw:
  510. ltq_etop_hw_exit(dev);
  511. netdev_err(dev, "failed to restart etop after TX timeout\n");
  512. }
  513. static const struct net_device_ops ltq_eth_netdev_ops = {
  514. .ndo_open = ltq_etop_open,
  515. .ndo_stop = ltq_etop_stop,
  516. .ndo_start_xmit = ltq_etop_tx,
  517. .ndo_change_mtu = ltq_etop_change_mtu,
  518. .ndo_do_ioctl = ltq_etop_ioctl,
  519. .ndo_set_mac_address = ltq_etop_set_mac_address,
  520. .ndo_validate_addr = eth_validate_addr,
  521. .ndo_set_rx_mode = ltq_etop_set_multicast_list,
  522. .ndo_select_queue = dev_pick_tx_zero,
  523. .ndo_init = ltq_etop_init,
  524. .ndo_tx_timeout = ltq_etop_tx_timeout,
  525. };
  526. static int __init
  527. ltq_etop_probe(struct platform_device *pdev)
  528. {
  529. struct net_device *dev;
  530. struct ltq_etop_priv *priv;
  531. struct resource *res;
  532. int err;
  533. int i;
  534. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  535. if (!res) {
  536. dev_err(&pdev->dev, "failed to get etop resource\n");
  537. err = -ENOENT;
  538. goto err_out;
  539. }
  540. res = devm_request_mem_region(&pdev->dev, res->start,
  541. resource_size(res), dev_name(&pdev->dev));
  542. if (!res) {
  543. dev_err(&pdev->dev, "failed to request etop resource\n");
  544. err = -EBUSY;
  545. goto err_out;
  546. }
  547. ltq_etop_membase = devm_ioremap_nocache(&pdev->dev,
  548. res->start, resource_size(res));
  549. if (!ltq_etop_membase) {
  550. dev_err(&pdev->dev, "failed to remap etop engine %d\n",
  551. pdev->id);
  552. err = -ENOMEM;
  553. goto err_out;
  554. }
  555. dev = alloc_etherdev_mq(sizeof(struct ltq_etop_priv), 4);
  556. if (!dev) {
  557. err = -ENOMEM;
  558. goto err_out;
  559. }
  560. strcpy(dev->name, "eth%d");
  561. dev->netdev_ops = &ltq_eth_netdev_ops;
  562. dev->ethtool_ops = &ltq_etop_ethtool_ops;
  563. priv = netdev_priv(dev);
  564. priv->res = res;
  565. priv->pdev = pdev;
  566. priv->pldata = dev_get_platdata(&pdev->dev);
  567. priv->netdev = dev;
  568. spin_lock_init(&priv->lock);
  569. SET_NETDEV_DEV(dev, &pdev->dev);
  570. for (i = 0; i < MAX_DMA_CHAN; i++) {
  571. if (IS_TX(i))
  572. netif_napi_add(dev, &priv->ch[i].napi,
  573. ltq_etop_poll_tx, 8);
  574. else if (IS_RX(i))
  575. netif_napi_add(dev, &priv->ch[i].napi,
  576. ltq_etop_poll_rx, 32);
  577. priv->ch[i].netdev = dev;
  578. }
  579. err = register_netdev(dev);
  580. if (err)
  581. goto err_free;
  582. platform_set_drvdata(pdev, dev);
  583. return 0;
  584. err_free:
  585. free_netdev(dev);
  586. err_out:
  587. return err;
  588. }
  589. static int
  590. ltq_etop_remove(struct platform_device *pdev)
  591. {
  592. struct net_device *dev = platform_get_drvdata(pdev);
  593. if (dev) {
  594. netif_tx_stop_all_queues(dev);
  595. ltq_etop_hw_exit(dev);
  596. ltq_etop_mdio_cleanup(dev);
  597. unregister_netdev(dev);
  598. }
  599. return 0;
  600. }
  601. static struct platform_driver ltq_mii_driver = {
  602. .remove = ltq_etop_remove,
  603. .driver = {
  604. .name = "ltq_etop",
  605. },
  606. };
  607. int __init
  608. init_ltq_etop(void)
  609. {
  610. int ret = platform_driver_probe(&ltq_mii_driver, ltq_etop_probe);
  611. if (ret)
  612. pr_err("ltq_etop: Error registering platform driver!");
  613. return ret;
  614. }
  615. static void __exit
  616. exit_ltq_etop(void)
  617. {
  618. platform_driver_unregister(&ltq_mii_driver);
  619. }
  620. module_init(init_ltq_etop);
  621. module_exit(exit_ltq_etop);
  622. MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
  623. MODULE_DESCRIPTION("Lantiq SoC ETOP");
  624. MODULE_LICENSE("GPL");