lantiq_etop.c 18 KB

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