lantiq_etop.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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 rx = 0;
  138. int complete = 0;
  139. while ((rx < budget) && !complete) {
  140. struct ltq_dma_desc *desc = &ch->dma.desc_base[ch->dma.desc];
  141. if ((desc->ctl & (LTQ_DMA_OWN | LTQ_DMA_C)) == LTQ_DMA_C) {
  142. ltq_etop_hw_receive(ch);
  143. rx++;
  144. } else {
  145. complete = 1;
  146. }
  147. }
  148. if (complete || !rx) {
  149. napi_complete(&ch->napi);
  150. ltq_dma_ack_irq(&ch->dma);
  151. }
  152. return rx;
  153. }
  154. static int
  155. ltq_etop_poll_tx(struct napi_struct *napi, int budget)
  156. {
  157. struct ltq_etop_chan *ch =
  158. container_of(napi, struct ltq_etop_chan, napi);
  159. struct ltq_etop_priv *priv = netdev_priv(ch->netdev);
  160. struct netdev_queue *txq =
  161. netdev_get_tx_queue(ch->netdev, ch->idx >> 1);
  162. unsigned long flags;
  163. spin_lock_irqsave(&priv->lock, flags);
  164. while ((ch->dma.desc_base[ch->tx_free].ctl &
  165. (LTQ_DMA_OWN | LTQ_DMA_C)) == LTQ_DMA_C) {
  166. dev_kfree_skb_any(ch->skb[ch->tx_free]);
  167. ch->skb[ch->tx_free] = NULL;
  168. memset(&ch->dma.desc_base[ch->tx_free], 0,
  169. sizeof(struct ltq_dma_desc));
  170. ch->tx_free++;
  171. ch->tx_free %= LTQ_DESC_NUM;
  172. }
  173. spin_unlock_irqrestore(&priv->lock, flags);
  174. if (netif_tx_queue_stopped(txq))
  175. netif_tx_start_queue(txq);
  176. napi_complete(&ch->napi);
  177. ltq_dma_ack_irq(&ch->dma);
  178. return 1;
  179. }
  180. static irqreturn_t
  181. ltq_etop_dma_irq(int irq, void *_priv)
  182. {
  183. struct ltq_etop_priv *priv = _priv;
  184. int ch = irq - LTQ_DMA_CH0_INT;
  185. napi_schedule(&priv->ch[ch].napi);
  186. return IRQ_HANDLED;
  187. }
  188. static void
  189. ltq_etop_free_channel(struct net_device *dev, struct ltq_etop_chan *ch)
  190. {
  191. struct ltq_etop_priv *priv = netdev_priv(dev);
  192. ltq_dma_free(&ch->dma);
  193. if (ch->dma.irq)
  194. free_irq(ch->dma.irq, priv);
  195. if (IS_RX(ch->idx)) {
  196. int desc;
  197. for (desc = 0; desc < LTQ_DESC_NUM; desc++)
  198. dev_kfree_skb_any(ch->skb[ch->dma.desc]);
  199. }
  200. }
  201. static void
  202. ltq_etop_hw_exit(struct net_device *dev)
  203. {
  204. struct ltq_etop_priv *priv = netdev_priv(dev);
  205. int i;
  206. ltq_pmu_disable(PMU_PPE);
  207. for (i = 0; i < MAX_DMA_CHAN; i++)
  208. if (IS_TX(i) || IS_RX(i))
  209. ltq_etop_free_channel(dev, &priv->ch[i]);
  210. }
  211. static int
  212. ltq_etop_hw_init(struct net_device *dev)
  213. {
  214. struct ltq_etop_priv *priv = netdev_priv(dev);
  215. int i;
  216. ltq_pmu_enable(PMU_PPE);
  217. switch (priv->pldata->mii_mode) {
  218. case PHY_INTERFACE_MODE_RMII:
  219. ltq_etop_w32_mask(ETOP_MII_MASK,
  220. ETOP_MII_REVERSE, LTQ_ETOP_CFG);
  221. break;
  222. case PHY_INTERFACE_MODE_MII:
  223. ltq_etop_w32_mask(ETOP_MII_MASK,
  224. ETOP_MII_NORMAL, LTQ_ETOP_CFG);
  225. break;
  226. default:
  227. netdev_err(dev, "unknown mii mode %d\n",
  228. priv->pldata->mii_mode);
  229. return -ENOTSUPP;
  230. }
  231. /* enable crc generation */
  232. ltq_etop_w32(PPE32_CGEN, LQ_PPE32_ENET_MAC_CFG);
  233. ltq_dma_init_port(DMA_PORT_ETOP);
  234. for (i = 0; i < MAX_DMA_CHAN; i++) {
  235. int irq = LTQ_DMA_CH0_INT + i;
  236. struct ltq_etop_chan *ch = &priv->ch[i];
  237. ch->idx = ch->dma.nr = i;
  238. if (IS_TX(i)) {
  239. ltq_dma_alloc_tx(&ch->dma);
  240. request_irq(irq, ltq_etop_dma_irq, 0, "etop_tx", priv);
  241. } else if (IS_RX(i)) {
  242. ltq_dma_alloc_rx(&ch->dma);
  243. for (ch->dma.desc = 0; ch->dma.desc < LTQ_DESC_NUM;
  244. ch->dma.desc++)
  245. if (ltq_etop_alloc_skb(ch))
  246. return -ENOMEM;
  247. ch->dma.desc = 0;
  248. request_irq(irq, ltq_etop_dma_irq, 0, "etop_rx", priv);
  249. }
  250. ch->dma.irq = irq;
  251. }
  252. return 0;
  253. }
  254. static void
  255. ltq_etop_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  256. {
  257. strlcpy(info->driver, "Lantiq ETOP", sizeof(info->driver));
  258. strlcpy(info->bus_info, "internal", sizeof(info->bus_info));
  259. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  260. }
  261. static int
  262. ltq_etop_nway_reset(struct net_device *dev)
  263. {
  264. return phy_start_aneg(dev->phydev);
  265. }
  266. static const struct ethtool_ops ltq_etop_ethtool_ops = {
  267. .get_drvinfo = ltq_etop_get_drvinfo,
  268. .nway_reset = ltq_etop_nway_reset,
  269. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  270. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  271. };
  272. static int
  273. ltq_etop_mdio_wr(struct mii_bus *bus, int phy_addr, int phy_reg, u16 phy_data)
  274. {
  275. u32 val = MDIO_REQUEST |
  276. ((phy_addr & MDIO_ADDR_MASK) << MDIO_ADDR_OFFSET) |
  277. ((phy_reg & MDIO_REG_MASK) << MDIO_REG_OFFSET) |
  278. phy_data;
  279. while (ltq_etop_r32(LTQ_ETOP_MDIO) & MDIO_REQUEST)
  280. ;
  281. ltq_etop_w32(val, LTQ_ETOP_MDIO);
  282. return 0;
  283. }
  284. static int
  285. ltq_etop_mdio_rd(struct mii_bus *bus, int phy_addr, int phy_reg)
  286. {
  287. u32 val = MDIO_REQUEST | MDIO_READ |
  288. ((phy_addr & MDIO_ADDR_MASK) << MDIO_ADDR_OFFSET) |
  289. ((phy_reg & MDIO_REG_MASK) << MDIO_REG_OFFSET);
  290. while (ltq_etop_r32(LTQ_ETOP_MDIO) & MDIO_REQUEST)
  291. ;
  292. ltq_etop_w32(val, LTQ_ETOP_MDIO);
  293. while (ltq_etop_r32(LTQ_ETOP_MDIO) & MDIO_REQUEST)
  294. ;
  295. val = ltq_etop_r32(LTQ_ETOP_MDIO) & MDIO_VAL_MASK;
  296. return val;
  297. }
  298. static void
  299. ltq_etop_mdio_link(struct net_device *dev)
  300. {
  301. /* nothing to do */
  302. }
  303. static int
  304. ltq_etop_mdio_probe(struct net_device *dev)
  305. {
  306. struct ltq_etop_priv *priv = netdev_priv(dev);
  307. struct phy_device *phydev;
  308. phydev = phy_find_first(priv->mii_bus);
  309. if (!phydev) {
  310. netdev_err(dev, "no PHY found\n");
  311. return -ENODEV;
  312. }
  313. phydev = phy_connect(dev, phydev_name(phydev),
  314. &ltq_etop_mdio_link, priv->pldata->mii_mode);
  315. if (IS_ERR(phydev)) {
  316. netdev_err(dev, "Could not attach to PHY\n");
  317. return PTR_ERR(phydev);
  318. }
  319. phydev->supported &= (SUPPORTED_10baseT_Half
  320. | SUPPORTED_10baseT_Full
  321. | SUPPORTED_100baseT_Half
  322. | SUPPORTED_100baseT_Full
  323. | SUPPORTED_Autoneg
  324. | SUPPORTED_MII
  325. | SUPPORTED_TP);
  326. phydev->advertising = phydev->supported;
  327. phy_attached_info(phydev);
  328. return 0;
  329. }
  330. static int
  331. ltq_etop_mdio_init(struct net_device *dev)
  332. {
  333. struct ltq_etop_priv *priv = netdev_priv(dev);
  334. int err;
  335. priv->mii_bus = mdiobus_alloc();
  336. if (!priv->mii_bus) {
  337. netdev_err(dev, "failed to allocate mii bus\n");
  338. err = -ENOMEM;
  339. goto err_out;
  340. }
  341. priv->mii_bus->priv = dev;
  342. priv->mii_bus->read = ltq_etop_mdio_rd;
  343. priv->mii_bus->write = ltq_etop_mdio_wr;
  344. priv->mii_bus->name = "ltq_mii";
  345. snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  346. priv->pdev->name, priv->pdev->id);
  347. if (mdiobus_register(priv->mii_bus)) {
  348. err = -ENXIO;
  349. goto err_out_free_mdiobus;
  350. }
  351. if (ltq_etop_mdio_probe(dev)) {
  352. err = -ENXIO;
  353. goto err_out_unregister_bus;
  354. }
  355. return 0;
  356. err_out_unregister_bus:
  357. mdiobus_unregister(priv->mii_bus);
  358. err_out_free_mdiobus:
  359. mdiobus_free(priv->mii_bus);
  360. err_out:
  361. return err;
  362. }
  363. static void
  364. ltq_etop_mdio_cleanup(struct net_device *dev)
  365. {
  366. struct ltq_etop_priv *priv = netdev_priv(dev);
  367. phy_disconnect(dev->phydev);
  368. mdiobus_unregister(priv->mii_bus);
  369. mdiobus_free(priv->mii_bus);
  370. }
  371. static int
  372. ltq_etop_open(struct net_device *dev)
  373. {
  374. struct ltq_etop_priv *priv = netdev_priv(dev);
  375. int i;
  376. for (i = 0; i < MAX_DMA_CHAN; i++) {
  377. struct ltq_etop_chan *ch = &priv->ch[i];
  378. if (!IS_TX(i) && (!IS_RX(i)))
  379. continue;
  380. ltq_dma_open(&ch->dma);
  381. napi_enable(&ch->napi);
  382. }
  383. phy_start(dev->phydev);
  384. netif_tx_start_all_queues(dev);
  385. return 0;
  386. }
  387. static int
  388. ltq_etop_stop(struct net_device *dev)
  389. {
  390. struct ltq_etop_priv *priv = netdev_priv(dev);
  391. int i;
  392. netif_tx_stop_all_queues(dev);
  393. phy_stop(dev->phydev);
  394. for (i = 0; i < MAX_DMA_CHAN; i++) {
  395. struct ltq_etop_chan *ch = &priv->ch[i];
  396. if (!IS_RX(i) && !IS_TX(i))
  397. continue;
  398. napi_disable(&ch->napi);
  399. ltq_dma_close(&ch->dma);
  400. }
  401. return 0;
  402. }
  403. static int
  404. ltq_etop_tx(struct sk_buff *skb, struct net_device *dev)
  405. {
  406. int queue = skb_get_queue_mapping(skb);
  407. struct netdev_queue *txq = netdev_get_tx_queue(dev, queue);
  408. struct ltq_etop_priv *priv = netdev_priv(dev);
  409. struct ltq_etop_chan *ch = &priv->ch[(queue << 1) | 1];
  410. struct ltq_dma_desc *desc = &ch->dma.desc_base[ch->dma.desc];
  411. int len;
  412. unsigned long flags;
  413. u32 byte_offset;
  414. len = skb->len < ETH_ZLEN ? ETH_ZLEN : skb->len;
  415. if ((desc->ctl & (LTQ_DMA_OWN | LTQ_DMA_C)) || ch->skb[ch->dma.desc]) {
  416. dev_kfree_skb_any(skb);
  417. netdev_err(dev, "tx ring full\n");
  418. netif_tx_stop_queue(txq);
  419. return NETDEV_TX_BUSY;
  420. }
  421. /* dma needs to start on a 16 byte aligned address */
  422. byte_offset = CPHYSADDR(skb->data) % 16;
  423. ch->skb[ch->dma.desc] = skb;
  424. netif_trans_update(dev);
  425. spin_lock_irqsave(&priv->lock, flags);
  426. desc->addr = ((unsigned int) dma_map_single(NULL, skb->data, len,
  427. DMA_TO_DEVICE)) - byte_offset;
  428. wmb();
  429. desc->ctl = LTQ_DMA_OWN | LTQ_DMA_SOP | LTQ_DMA_EOP |
  430. LTQ_DMA_TX_OFFSET(byte_offset) | (len & LTQ_DMA_SIZE_MASK);
  431. ch->dma.desc++;
  432. ch->dma.desc %= LTQ_DESC_NUM;
  433. spin_unlock_irqrestore(&priv->lock, flags);
  434. if (ch->dma.desc_base[ch->dma.desc].ctl & LTQ_DMA_OWN)
  435. netif_tx_stop_queue(txq);
  436. return NETDEV_TX_OK;
  437. }
  438. static int
  439. ltq_etop_change_mtu(struct net_device *dev, int new_mtu)
  440. {
  441. int ret = eth_change_mtu(dev, new_mtu);
  442. if (!ret) {
  443. struct ltq_etop_priv *priv = netdev_priv(dev);
  444. unsigned long flags;
  445. spin_lock_irqsave(&priv->lock, flags);
  446. ltq_etop_w32((ETOP_PLEN_UNDER << 16) | new_mtu,
  447. LTQ_ETOP_IGPLEN);
  448. spin_unlock_irqrestore(&priv->lock, flags);
  449. }
  450. return ret;
  451. }
  452. static int
  453. ltq_etop_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  454. {
  455. /* TODO: mii-toll reports "No MII transceiver present!." ?!*/
  456. return phy_mii_ioctl(dev->phydev, rq, cmd);
  457. }
  458. static int
  459. ltq_etop_set_mac_address(struct net_device *dev, void *p)
  460. {
  461. int ret = eth_mac_addr(dev, p);
  462. if (!ret) {
  463. struct ltq_etop_priv *priv = netdev_priv(dev);
  464. unsigned long flags;
  465. /* store the mac for the unicast filter */
  466. spin_lock_irqsave(&priv->lock, flags);
  467. ltq_etop_w32(*((u32 *)dev->dev_addr), LTQ_ETOP_MAC_DA0);
  468. ltq_etop_w32(*((u16 *)&dev->dev_addr[4]) << 16,
  469. LTQ_ETOP_MAC_DA1);
  470. spin_unlock_irqrestore(&priv->lock, flags);
  471. }
  472. return ret;
  473. }
  474. static void
  475. ltq_etop_set_multicast_list(struct net_device *dev)
  476. {
  477. struct ltq_etop_priv *priv = netdev_priv(dev);
  478. unsigned long flags;
  479. /* ensure that the unicast filter is not enabled in promiscious mode */
  480. spin_lock_irqsave(&priv->lock, flags);
  481. if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI))
  482. ltq_etop_w32_mask(ETOP_FTCU, 0, LTQ_ETOP_ENETS0);
  483. else
  484. ltq_etop_w32_mask(0, ETOP_FTCU, LTQ_ETOP_ENETS0);
  485. spin_unlock_irqrestore(&priv->lock, flags);
  486. }
  487. static u16
  488. ltq_etop_select_queue(struct net_device *dev, struct sk_buff *skb,
  489. void *accel_priv, select_queue_fallback_t fallback)
  490. {
  491. /* we are currently only using the first queue */
  492. return 0;
  493. }
  494. static int
  495. ltq_etop_init(struct net_device *dev)
  496. {
  497. struct ltq_etop_priv *priv = netdev_priv(dev);
  498. struct sockaddr mac;
  499. int err;
  500. bool random_mac = false;
  501. dev->watchdog_timeo = 10 * HZ;
  502. err = ltq_etop_hw_init(dev);
  503. if (err)
  504. goto err_hw;
  505. ltq_etop_change_mtu(dev, 1500);
  506. memcpy(&mac, &priv->pldata->mac, sizeof(struct sockaddr));
  507. if (!is_valid_ether_addr(mac.sa_data)) {
  508. pr_warn("etop: invalid MAC, using random\n");
  509. eth_random_addr(mac.sa_data);
  510. random_mac = true;
  511. }
  512. err = ltq_etop_set_mac_address(dev, &mac);
  513. if (err)
  514. goto err_netdev;
  515. /* Set addr_assign_type here, ltq_etop_set_mac_address would reset it. */
  516. if (random_mac)
  517. dev->addr_assign_type = NET_ADDR_RANDOM;
  518. ltq_etop_set_multicast_list(dev);
  519. err = ltq_etop_mdio_init(dev);
  520. if (err)
  521. goto err_netdev;
  522. return 0;
  523. err_netdev:
  524. unregister_netdev(dev);
  525. free_netdev(dev);
  526. err_hw:
  527. ltq_etop_hw_exit(dev);
  528. return err;
  529. }
  530. static void
  531. ltq_etop_tx_timeout(struct net_device *dev)
  532. {
  533. int err;
  534. ltq_etop_hw_exit(dev);
  535. err = ltq_etop_hw_init(dev);
  536. if (err)
  537. goto err_hw;
  538. netif_trans_update(dev);
  539. netif_wake_queue(dev);
  540. return;
  541. err_hw:
  542. ltq_etop_hw_exit(dev);
  543. netdev_err(dev, "failed to restart etop after TX timeout\n");
  544. }
  545. static const struct net_device_ops ltq_eth_netdev_ops = {
  546. .ndo_open = ltq_etop_open,
  547. .ndo_stop = ltq_etop_stop,
  548. .ndo_start_xmit = ltq_etop_tx,
  549. .ndo_change_mtu = ltq_etop_change_mtu,
  550. .ndo_do_ioctl = ltq_etop_ioctl,
  551. .ndo_set_mac_address = ltq_etop_set_mac_address,
  552. .ndo_validate_addr = eth_validate_addr,
  553. .ndo_set_rx_mode = ltq_etop_set_multicast_list,
  554. .ndo_select_queue = ltq_etop_select_queue,
  555. .ndo_init = ltq_etop_init,
  556. .ndo_tx_timeout = ltq_etop_tx_timeout,
  557. };
  558. static int __init
  559. ltq_etop_probe(struct platform_device *pdev)
  560. {
  561. struct net_device *dev;
  562. struct ltq_etop_priv *priv;
  563. struct resource *res;
  564. int err;
  565. int i;
  566. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  567. if (!res) {
  568. dev_err(&pdev->dev, "failed to get etop resource\n");
  569. err = -ENOENT;
  570. goto err_out;
  571. }
  572. res = devm_request_mem_region(&pdev->dev, res->start,
  573. resource_size(res), dev_name(&pdev->dev));
  574. if (!res) {
  575. dev_err(&pdev->dev, "failed to request etop resource\n");
  576. err = -EBUSY;
  577. goto err_out;
  578. }
  579. ltq_etop_membase = devm_ioremap_nocache(&pdev->dev,
  580. res->start, resource_size(res));
  581. if (!ltq_etop_membase) {
  582. dev_err(&pdev->dev, "failed to remap etop engine %d\n",
  583. pdev->id);
  584. err = -ENOMEM;
  585. goto err_out;
  586. }
  587. dev = alloc_etherdev_mq(sizeof(struct ltq_etop_priv), 4);
  588. if (!dev) {
  589. err = -ENOMEM;
  590. goto err_out;
  591. }
  592. strcpy(dev->name, "eth%d");
  593. dev->netdev_ops = &ltq_eth_netdev_ops;
  594. dev->ethtool_ops = &ltq_etop_ethtool_ops;
  595. priv = netdev_priv(dev);
  596. priv->res = res;
  597. priv->pdev = pdev;
  598. priv->pldata = dev_get_platdata(&pdev->dev);
  599. priv->netdev = dev;
  600. spin_lock_init(&priv->lock);
  601. SET_NETDEV_DEV(dev, &pdev->dev);
  602. for (i = 0; i < MAX_DMA_CHAN; i++) {
  603. if (IS_TX(i))
  604. netif_napi_add(dev, &priv->ch[i].napi,
  605. ltq_etop_poll_tx, 8);
  606. else if (IS_RX(i))
  607. netif_napi_add(dev, &priv->ch[i].napi,
  608. ltq_etop_poll_rx, 32);
  609. priv->ch[i].netdev = dev;
  610. }
  611. err = register_netdev(dev);
  612. if (err)
  613. goto err_free;
  614. platform_set_drvdata(pdev, dev);
  615. return 0;
  616. err_free:
  617. free_netdev(dev);
  618. err_out:
  619. return err;
  620. }
  621. static int
  622. ltq_etop_remove(struct platform_device *pdev)
  623. {
  624. struct net_device *dev = platform_get_drvdata(pdev);
  625. if (dev) {
  626. netif_tx_stop_all_queues(dev);
  627. ltq_etop_hw_exit(dev);
  628. ltq_etop_mdio_cleanup(dev);
  629. unregister_netdev(dev);
  630. }
  631. return 0;
  632. }
  633. static struct platform_driver ltq_mii_driver = {
  634. .remove = ltq_etop_remove,
  635. .driver = {
  636. .name = "ltq_etop",
  637. },
  638. };
  639. int __init
  640. init_ltq_etop(void)
  641. {
  642. int ret = platform_driver_probe(&ltq_mii_driver, ltq_etop_probe);
  643. if (ret)
  644. pr_err("ltq_etop: Error registering platform driver!");
  645. return ret;
  646. }
  647. static void __exit
  648. exit_ltq_etop(void)
  649. {
  650. platform_driver_unregister(&ltq_mii_driver);
  651. }
  652. module_init(init_ltq_etop);
  653. module_exit(exit_ltq_etop);
  654. MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
  655. MODULE_DESCRIPTION("Lantiq SoC ETOP");
  656. MODULE_LICENSE("GPL");