lantiq_etop.c 19 KB

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