lantiq_etop.c 19 KB

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