lantiq_etop.c 18 KB

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