dnet.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. /*
  2. * Dave DNET Ethernet Controller driver
  3. *
  4. * Copyright (C) 2008 Dave S.r.l. <www.dave.eu>
  5. * Copyright (C) 2009 Ilya Yanok, Emcraft Systems Ltd, <yanok@emcraft.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/io.h>
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/kernel.h>
  15. #include <linux/types.h>
  16. #include <linux/slab.h>
  17. #include <linux/delay.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/etherdevice.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/phy.h>
  24. #include "dnet.h"
  25. #undef DEBUG
  26. /* function for reading internal MAC register */
  27. static u16 dnet_readw_mac(struct dnet *bp, u16 reg)
  28. {
  29. u16 data_read;
  30. /* issue a read */
  31. dnet_writel(bp, reg, MACREG_ADDR);
  32. /* since a read/write op to the MAC is very slow,
  33. * we must wait before reading the data */
  34. ndelay(500);
  35. /* read data read from the MAC register */
  36. data_read = dnet_readl(bp, MACREG_DATA);
  37. /* all done */
  38. return data_read;
  39. }
  40. /* function for writing internal MAC register */
  41. static void dnet_writew_mac(struct dnet *bp, u16 reg, u16 val)
  42. {
  43. /* load data to write */
  44. dnet_writel(bp, val, MACREG_DATA);
  45. /* issue a write */
  46. dnet_writel(bp, reg | DNET_INTERNAL_WRITE, MACREG_ADDR);
  47. /* since a read/write op to the MAC is very slow,
  48. * we must wait before exiting */
  49. ndelay(500);
  50. }
  51. static void __dnet_set_hwaddr(struct dnet *bp)
  52. {
  53. u16 tmp;
  54. tmp = be16_to_cpup((__be16 *)bp->dev->dev_addr);
  55. dnet_writew_mac(bp, DNET_INTERNAL_MAC_ADDR_0_REG, tmp);
  56. tmp = be16_to_cpup((__be16 *)(bp->dev->dev_addr + 2));
  57. dnet_writew_mac(bp, DNET_INTERNAL_MAC_ADDR_1_REG, tmp);
  58. tmp = be16_to_cpup((__be16 *)(bp->dev->dev_addr + 4));
  59. dnet_writew_mac(bp, DNET_INTERNAL_MAC_ADDR_2_REG, tmp);
  60. }
  61. static void dnet_get_hwaddr(struct dnet *bp)
  62. {
  63. u16 tmp;
  64. u8 addr[6];
  65. /*
  66. * from MAC docs:
  67. * "Note that the MAC address is stored in the registers in Hexadecimal
  68. * form. For example, to set the MAC Address to: AC-DE-48-00-00-80
  69. * would require writing 0xAC (octet 0) to address 0x0B (high byte of
  70. * Mac_addr[15:0]), 0xDE (octet 1) to address 0x0A (Low byte of
  71. * Mac_addr[15:0]), 0x48 (octet 2) to address 0x0D (high byte of
  72. * Mac_addr[15:0]), 0x00 (octet 3) to address 0x0C (Low byte of
  73. * Mac_addr[15:0]), 0x00 (octet 4) to address 0x0F (high byte of
  74. * Mac_addr[15:0]), and 0x80 (octet 5) to address * 0x0E (Low byte of
  75. * Mac_addr[15:0]).
  76. */
  77. tmp = dnet_readw_mac(bp, DNET_INTERNAL_MAC_ADDR_0_REG);
  78. *((__be16 *)addr) = cpu_to_be16(tmp);
  79. tmp = dnet_readw_mac(bp, DNET_INTERNAL_MAC_ADDR_1_REG);
  80. *((__be16 *)(addr + 2)) = cpu_to_be16(tmp);
  81. tmp = dnet_readw_mac(bp, DNET_INTERNAL_MAC_ADDR_2_REG);
  82. *((__be16 *)(addr + 4)) = cpu_to_be16(tmp);
  83. if (is_valid_ether_addr(addr))
  84. memcpy(bp->dev->dev_addr, addr, sizeof(addr));
  85. }
  86. static int dnet_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
  87. {
  88. struct dnet *bp = bus->priv;
  89. u16 value;
  90. while (!(dnet_readw_mac(bp, DNET_INTERNAL_GMII_MNG_CTL_REG)
  91. & DNET_INTERNAL_GMII_MNG_CMD_FIN))
  92. cpu_relax();
  93. /* only 5 bits allowed for phy-addr and reg_offset */
  94. mii_id &= 0x1f;
  95. regnum &= 0x1f;
  96. /* prepare reg_value for a read */
  97. value = (mii_id << 8);
  98. value |= regnum;
  99. /* write control word */
  100. dnet_writew_mac(bp, DNET_INTERNAL_GMII_MNG_CTL_REG, value);
  101. /* wait for end of transfer */
  102. while (!(dnet_readw_mac(bp, DNET_INTERNAL_GMII_MNG_CTL_REG)
  103. & DNET_INTERNAL_GMII_MNG_CMD_FIN))
  104. cpu_relax();
  105. value = dnet_readw_mac(bp, DNET_INTERNAL_GMII_MNG_DAT_REG);
  106. pr_debug("mdio_read %02x:%02x <- %04x\n", mii_id, regnum, value);
  107. return value;
  108. }
  109. static int dnet_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
  110. u16 value)
  111. {
  112. struct dnet *bp = bus->priv;
  113. u16 tmp;
  114. pr_debug("mdio_write %02x:%02x <- %04x\n", mii_id, regnum, value);
  115. while (!(dnet_readw_mac(bp, DNET_INTERNAL_GMII_MNG_CTL_REG)
  116. & DNET_INTERNAL_GMII_MNG_CMD_FIN))
  117. cpu_relax();
  118. /* prepare for a write operation */
  119. tmp = (1 << 13);
  120. /* only 5 bits allowed for phy-addr and reg_offset */
  121. mii_id &= 0x1f;
  122. regnum &= 0x1f;
  123. /* only 16 bits on data */
  124. value &= 0xffff;
  125. /* prepare reg_value for a write */
  126. tmp |= (mii_id << 8);
  127. tmp |= regnum;
  128. /* write data to write first */
  129. dnet_writew_mac(bp, DNET_INTERNAL_GMII_MNG_DAT_REG, value);
  130. /* write control word */
  131. dnet_writew_mac(bp, DNET_INTERNAL_GMII_MNG_CTL_REG, tmp);
  132. while (!(dnet_readw_mac(bp, DNET_INTERNAL_GMII_MNG_CTL_REG)
  133. & DNET_INTERNAL_GMII_MNG_CMD_FIN))
  134. cpu_relax();
  135. return 0;
  136. }
  137. static void dnet_handle_link_change(struct net_device *dev)
  138. {
  139. struct dnet *bp = netdev_priv(dev);
  140. struct phy_device *phydev = bp->phy_dev;
  141. unsigned long flags;
  142. u32 mode_reg, ctl_reg;
  143. int status_change = 0;
  144. spin_lock_irqsave(&bp->lock, flags);
  145. mode_reg = dnet_readw_mac(bp, DNET_INTERNAL_MODE_REG);
  146. ctl_reg = dnet_readw_mac(bp, DNET_INTERNAL_RXTX_CONTROL_REG);
  147. if (phydev->link) {
  148. if (bp->duplex != phydev->duplex) {
  149. if (phydev->duplex)
  150. ctl_reg &=
  151. ~(DNET_INTERNAL_RXTX_CONTROL_ENABLEHALFDUP);
  152. else
  153. ctl_reg |=
  154. DNET_INTERNAL_RXTX_CONTROL_ENABLEHALFDUP;
  155. bp->duplex = phydev->duplex;
  156. status_change = 1;
  157. }
  158. if (bp->speed != phydev->speed) {
  159. status_change = 1;
  160. switch (phydev->speed) {
  161. case 1000:
  162. mode_reg |= DNET_INTERNAL_MODE_GBITEN;
  163. break;
  164. case 100:
  165. case 10:
  166. mode_reg &= ~DNET_INTERNAL_MODE_GBITEN;
  167. break;
  168. default:
  169. printk(KERN_WARNING
  170. "%s: Ack! Speed (%d) is not "
  171. "10/100/1000!\n", dev->name,
  172. phydev->speed);
  173. break;
  174. }
  175. bp->speed = phydev->speed;
  176. }
  177. }
  178. if (phydev->link != bp->link) {
  179. if (phydev->link) {
  180. mode_reg |=
  181. (DNET_INTERNAL_MODE_RXEN | DNET_INTERNAL_MODE_TXEN);
  182. } else {
  183. mode_reg &=
  184. ~(DNET_INTERNAL_MODE_RXEN |
  185. DNET_INTERNAL_MODE_TXEN);
  186. bp->speed = 0;
  187. bp->duplex = -1;
  188. }
  189. bp->link = phydev->link;
  190. status_change = 1;
  191. }
  192. if (status_change) {
  193. dnet_writew_mac(bp, DNET_INTERNAL_RXTX_CONTROL_REG, ctl_reg);
  194. dnet_writew_mac(bp, DNET_INTERNAL_MODE_REG, mode_reg);
  195. }
  196. spin_unlock_irqrestore(&bp->lock, flags);
  197. if (status_change) {
  198. if (phydev->link)
  199. printk(KERN_INFO "%s: link up (%d/%s)\n",
  200. dev->name, phydev->speed,
  201. DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
  202. else
  203. printk(KERN_INFO "%s: link down\n", dev->name);
  204. }
  205. }
  206. static int dnet_mii_probe(struct net_device *dev)
  207. {
  208. struct dnet *bp = netdev_priv(dev);
  209. struct phy_device *phydev = NULL;
  210. int phy_addr;
  211. /* find the first phy */
  212. for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++) {
  213. if (bp->mii_bus->phy_map[phy_addr]) {
  214. phydev = bp->mii_bus->phy_map[phy_addr];
  215. break;
  216. }
  217. }
  218. if (!phydev) {
  219. printk(KERN_ERR "%s: no PHY found\n", dev->name);
  220. return -ENODEV;
  221. }
  222. /* TODO : add pin_irq */
  223. /* attach the mac to the phy */
  224. if (bp->capabilities & DNET_HAS_RMII) {
  225. phydev = phy_connect(dev, dev_name(&phydev->dev),
  226. &dnet_handle_link_change,
  227. PHY_INTERFACE_MODE_RMII);
  228. } else {
  229. phydev = phy_connect(dev, dev_name(&phydev->dev),
  230. &dnet_handle_link_change,
  231. PHY_INTERFACE_MODE_MII);
  232. }
  233. if (IS_ERR(phydev)) {
  234. printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
  235. return PTR_ERR(phydev);
  236. }
  237. /* mask with MAC supported features */
  238. if (bp->capabilities & DNET_HAS_GIGABIT)
  239. phydev->supported &= PHY_GBIT_FEATURES;
  240. else
  241. phydev->supported &= PHY_BASIC_FEATURES;
  242. phydev->supported |= SUPPORTED_Asym_Pause | SUPPORTED_Pause;
  243. phydev->advertising = phydev->supported;
  244. bp->link = 0;
  245. bp->speed = 0;
  246. bp->duplex = -1;
  247. bp->phy_dev = phydev;
  248. return 0;
  249. }
  250. static int dnet_mii_init(struct dnet *bp)
  251. {
  252. int err, i;
  253. bp->mii_bus = mdiobus_alloc();
  254. if (bp->mii_bus == NULL)
  255. return -ENOMEM;
  256. bp->mii_bus->name = "dnet_mii_bus";
  257. bp->mii_bus->read = &dnet_mdio_read;
  258. bp->mii_bus->write = &dnet_mdio_write;
  259. snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  260. bp->pdev->name, bp->pdev->id);
  261. bp->mii_bus->priv = bp;
  262. bp->mii_bus->irq = devm_kmalloc(&bp->pdev->dev,
  263. sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
  264. if (!bp->mii_bus->irq) {
  265. err = -ENOMEM;
  266. goto err_out;
  267. }
  268. for (i = 0; i < PHY_MAX_ADDR; i++)
  269. bp->mii_bus->irq[i] = PHY_POLL;
  270. if (mdiobus_register(bp->mii_bus)) {
  271. err = -ENXIO;
  272. goto err_out;
  273. }
  274. if (dnet_mii_probe(bp->dev) != 0) {
  275. err = -ENXIO;
  276. goto err_out_unregister_bus;
  277. }
  278. return 0;
  279. err_out_unregister_bus:
  280. mdiobus_unregister(bp->mii_bus);
  281. err_out:
  282. mdiobus_free(bp->mii_bus);
  283. return err;
  284. }
  285. /* For Neptune board: LINK1000 as Link LED and TX as activity LED */
  286. static int dnet_phy_marvell_fixup(struct phy_device *phydev)
  287. {
  288. return phy_write(phydev, 0x18, 0x4148);
  289. }
  290. static void dnet_update_stats(struct dnet *bp)
  291. {
  292. u32 __iomem *reg = bp->regs + DNET_RX_PKT_IGNR_CNT;
  293. u32 *p = &bp->hw_stats.rx_pkt_ignr;
  294. u32 *end = &bp->hw_stats.rx_byte + 1;
  295. WARN_ON((unsigned long)(end - p - 1) !=
  296. (DNET_RX_BYTE_CNT - DNET_RX_PKT_IGNR_CNT) / 4);
  297. for (; p < end; p++, reg++)
  298. *p += readl(reg);
  299. reg = bp->regs + DNET_TX_UNICAST_CNT;
  300. p = &bp->hw_stats.tx_unicast;
  301. end = &bp->hw_stats.tx_byte + 1;
  302. WARN_ON((unsigned long)(end - p - 1) !=
  303. (DNET_TX_BYTE_CNT - DNET_TX_UNICAST_CNT) / 4);
  304. for (; p < end; p++, reg++)
  305. *p += readl(reg);
  306. }
  307. static int dnet_poll(struct napi_struct *napi, int budget)
  308. {
  309. struct dnet *bp = container_of(napi, struct dnet, napi);
  310. struct net_device *dev = bp->dev;
  311. int npackets = 0;
  312. unsigned int pkt_len;
  313. struct sk_buff *skb;
  314. unsigned int *data_ptr;
  315. u32 int_enable;
  316. u32 cmd_word;
  317. int i;
  318. while (npackets < budget) {
  319. /*
  320. * break out of while loop if there are no more
  321. * packets waiting
  322. */
  323. if (!(dnet_readl(bp, RX_FIFO_WCNT) >> 16))
  324. break;
  325. cmd_word = dnet_readl(bp, RX_LEN_FIFO);
  326. pkt_len = cmd_word & 0xFFFF;
  327. if (cmd_word & 0xDF180000)
  328. printk(KERN_ERR "%s packet receive error %x\n",
  329. __func__, cmd_word);
  330. skb = netdev_alloc_skb(dev, pkt_len + 5);
  331. if (skb != NULL) {
  332. /* Align IP on 16 byte boundaries */
  333. skb_reserve(skb, 2);
  334. /*
  335. * 'skb_put()' points to the start of sk_buff
  336. * data area.
  337. */
  338. data_ptr = (unsigned int *)skb_put(skb, pkt_len);
  339. for (i = 0; i < (pkt_len + 3) >> 2; i++)
  340. *data_ptr++ = dnet_readl(bp, RX_DATA_FIFO);
  341. skb->protocol = eth_type_trans(skb, dev);
  342. netif_receive_skb(skb);
  343. npackets++;
  344. } else
  345. printk(KERN_NOTICE
  346. "%s: No memory to allocate a sk_buff of "
  347. "size %u.\n", dev->name, pkt_len);
  348. }
  349. if (npackets < budget) {
  350. /* We processed all packets available. Tell NAPI it can
  351. * stop polling then re-enable rx interrupts.
  352. */
  353. napi_complete(napi);
  354. int_enable = dnet_readl(bp, INTR_ENB);
  355. int_enable |= DNET_INTR_SRC_RX_CMDFIFOAF;
  356. dnet_writel(bp, int_enable, INTR_ENB);
  357. }
  358. return npackets;
  359. }
  360. static irqreturn_t dnet_interrupt(int irq, void *dev_id)
  361. {
  362. struct net_device *dev = dev_id;
  363. struct dnet *bp = netdev_priv(dev);
  364. u32 int_src, int_enable, int_current;
  365. unsigned long flags;
  366. unsigned int handled = 0;
  367. spin_lock_irqsave(&bp->lock, flags);
  368. /* read and clear the DNET irq (clear on read) */
  369. int_src = dnet_readl(bp, INTR_SRC);
  370. int_enable = dnet_readl(bp, INTR_ENB);
  371. int_current = int_src & int_enable;
  372. /* restart the queue if we had stopped it for TX fifo almost full */
  373. if (int_current & DNET_INTR_SRC_TX_FIFOAE) {
  374. int_enable = dnet_readl(bp, INTR_ENB);
  375. int_enable &= ~DNET_INTR_ENB_TX_FIFOAE;
  376. dnet_writel(bp, int_enable, INTR_ENB);
  377. netif_wake_queue(dev);
  378. handled = 1;
  379. }
  380. /* RX FIFO error checking */
  381. if (int_current &
  382. (DNET_INTR_SRC_RX_CMDFIFOFF | DNET_INTR_SRC_RX_DATAFIFOFF)) {
  383. printk(KERN_ERR "%s: RX fifo error %x, irq %x\n", __func__,
  384. dnet_readl(bp, RX_STATUS), int_current);
  385. /* we can only flush the RX FIFOs */
  386. dnet_writel(bp, DNET_SYS_CTL_RXFIFOFLUSH, SYS_CTL);
  387. ndelay(500);
  388. dnet_writel(bp, 0, SYS_CTL);
  389. handled = 1;
  390. }
  391. /* TX FIFO error checking */
  392. if (int_current &
  393. (DNET_INTR_SRC_TX_FIFOFULL | DNET_INTR_SRC_TX_DISCFRM)) {
  394. printk(KERN_ERR "%s: TX fifo error %x, irq %x\n", __func__,
  395. dnet_readl(bp, TX_STATUS), int_current);
  396. /* we can only flush the TX FIFOs */
  397. dnet_writel(bp, DNET_SYS_CTL_TXFIFOFLUSH, SYS_CTL);
  398. ndelay(500);
  399. dnet_writel(bp, 0, SYS_CTL);
  400. handled = 1;
  401. }
  402. if (int_current & DNET_INTR_SRC_RX_CMDFIFOAF) {
  403. if (napi_schedule_prep(&bp->napi)) {
  404. /*
  405. * There's no point taking any more interrupts
  406. * until we have processed the buffers
  407. */
  408. /* Disable Rx interrupts and schedule NAPI poll */
  409. int_enable = dnet_readl(bp, INTR_ENB);
  410. int_enable &= ~DNET_INTR_SRC_RX_CMDFIFOAF;
  411. dnet_writel(bp, int_enable, INTR_ENB);
  412. __napi_schedule(&bp->napi);
  413. }
  414. handled = 1;
  415. }
  416. if (!handled)
  417. pr_debug("%s: irq %x remains\n", __func__, int_current);
  418. spin_unlock_irqrestore(&bp->lock, flags);
  419. return IRQ_RETVAL(handled);
  420. }
  421. #ifdef DEBUG
  422. static inline void dnet_print_skb(struct sk_buff *skb)
  423. {
  424. int k;
  425. printk(KERN_DEBUG PFX "data:");
  426. for (k = 0; k < skb->len; k++)
  427. printk(" %02x", (unsigned int)skb->data[k]);
  428. printk("\n");
  429. }
  430. #else
  431. #define dnet_print_skb(skb) do {} while (0)
  432. #endif
  433. static netdev_tx_t dnet_start_xmit(struct sk_buff *skb, struct net_device *dev)
  434. {
  435. struct dnet *bp = netdev_priv(dev);
  436. u32 tx_status, irq_enable;
  437. unsigned int len, i, tx_cmd, wrsz;
  438. unsigned long flags;
  439. unsigned int *bufp;
  440. tx_status = dnet_readl(bp, TX_STATUS);
  441. pr_debug("start_xmit: len %u head %p data %p\n",
  442. skb->len, skb->head, skb->data);
  443. dnet_print_skb(skb);
  444. /* frame size (words) */
  445. len = (skb->len + 3) >> 2;
  446. spin_lock_irqsave(&bp->lock, flags);
  447. tx_status = dnet_readl(bp, TX_STATUS);
  448. bufp = (unsigned int *)(((unsigned long) skb->data) & ~0x3UL);
  449. wrsz = (u32) skb->len + 3;
  450. wrsz += ((unsigned long) skb->data) & 0x3;
  451. wrsz >>= 2;
  452. tx_cmd = ((((unsigned long)(skb->data)) & 0x03) << 16) | (u32) skb->len;
  453. /* check if there is enough room for the current frame */
  454. if (wrsz < (DNET_FIFO_SIZE - dnet_readl(bp, TX_FIFO_WCNT))) {
  455. for (i = 0; i < wrsz; i++)
  456. dnet_writel(bp, *bufp++, TX_DATA_FIFO);
  457. /*
  458. * inform MAC that a packet's written and ready to be
  459. * shipped out
  460. */
  461. dnet_writel(bp, tx_cmd, TX_LEN_FIFO);
  462. }
  463. if (dnet_readl(bp, TX_FIFO_WCNT) > DNET_FIFO_TX_DATA_AF_TH) {
  464. netif_stop_queue(dev);
  465. tx_status = dnet_readl(bp, INTR_SRC);
  466. irq_enable = dnet_readl(bp, INTR_ENB);
  467. irq_enable |= DNET_INTR_ENB_TX_FIFOAE;
  468. dnet_writel(bp, irq_enable, INTR_ENB);
  469. }
  470. skb_tx_timestamp(skb);
  471. /* free the buffer */
  472. dev_kfree_skb(skb);
  473. spin_unlock_irqrestore(&bp->lock, flags);
  474. return NETDEV_TX_OK;
  475. }
  476. static void dnet_reset_hw(struct dnet *bp)
  477. {
  478. /* put ts_mac in IDLE state i.e. disable rx/tx */
  479. dnet_writew_mac(bp, DNET_INTERNAL_MODE_REG, DNET_INTERNAL_MODE_FCEN);
  480. /*
  481. * RX FIFO almost full threshold: only cmd FIFO almost full is
  482. * implemented for RX side
  483. */
  484. dnet_writel(bp, DNET_FIFO_RX_CMD_AF_TH, RX_FIFO_TH);
  485. /*
  486. * TX FIFO almost empty threshold: only data FIFO almost empty
  487. * is implemented for TX side
  488. */
  489. dnet_writel(bp, DNET_FIFO_TX_DATA_AE_TH, TX_FIFO_TH);
  490. /* flush rx/tx fifos */
  491. dnet_writel(bp, DNET_SYS_CTL_RXFIFOFLUSH | DNET_SYS_CTL_TXFIFOFLUSH,
  492. SYS_CTL);
  493. msleep(1);
  494. dnet_writel(bp, 0, SYS_CTL);
  495. }
  496. static void dnet_init_hw(struct dnet *bp)
  497. {
  498. u32 config;
  499. dnet_reset_hw(bp);
  500. __dnet_set_hwaddr(bp);
  501. config = dnet_readw_mac(bp, DNET_INTERNAL_RXTX_CONTROL_REG);
  502. if (bp->dev->flags & IFF_PROMISC)
  503. /* Copy All Frames */
  504. config |= DNET_INTERNAL_RXTX_CONTROL_ENPROMISC;
  505. if (!(bp->dev->flags & IFF_BROADCAST))
  506. /* No BroadCast */
  507. config |= DNET_INTERNAL_RXTX_CONTROL_RXMULTICAST;
  508. config |= DNET_INTERNAL_RXTX_CONTROL_RXPAUSE |
  509. DNET_INTERNAL_RXTX_CONTROL_RXBROADCAST |
  510. DNET_INTERNAL_RXTX_CONTROL_DROPCONTROL |
  511. DNET_INTERNAL_RXTX_CONTROL_DISCFXFCS;
  512. dnet_writew_mac(bp, DNET_INTERNAL_RXTX_CONTROL_REG, config);
  513. /* clear irq before enabling them */
  514. config = dnet_readl(bp, INTR_SRC);
  515. /* enable RX/TX interrupt, recv packet ready interrupt */
  516. dnet_writel(bp, DNET_INTR_ENB_GLOBAL_ENABLE | DNET_INTR_ENB_RX_SUMMARY |
  517. DNET_INTR_ENB_TX_SUMMARY | DNET_INTR_ENB_RX_FIFOERR |
  518. DNET_INTR_ENB_RX_ERROR | DNET_INTR_ENB_RX_FIFOFULL |
  519. DNET_INTR_ENB_TX_FIFOFULL | DNET_INTR_ENB_TX_DISCFRM |
  520. DNET_INTR_ENB_RX_PKTRDY, INTR_ENB);
  521. }
  522. static int dnet_open(struct net_device *dev)
  523. {
  524. struct dnet *bp = netdev_priv(dev);
  525. /* if the phy is not yet register, retry later */
  526. if (!bp->phy_dev)
  527. return -EAGAIN;
  528. napi_enable(&bp->napi);
  529. dnet_init_hw(bp);
  530. phy_start_aneg(bp->phy_dev);
  531. /* schedule a link state check */
  532. phy_start(bp->phy_dev);
  533. netif_start_queue(dev);
  534. return 0;
  535. }
  536. static int dnet_close(struct net_device *dev)
  537. {
  538. struct dnet *bp = netdev_priv(dev);
  539. netif_stop_queue(dev);
  540. napi_disable(&bp->napi);
  541. if (bp->phy_dev)
  542. phy_stop(bp->phy_dev);
  543. dnet_reset_hw(bp);
  544. netif_carrier_off(dev);
  545. return 0;
  546. }
  547. static inline void dnet_print_pretty_hwstats(struct dnet_stats *hwstat)
  548. {
  549. pr_debug("%s\n", __func__);
  550. pr_debug("----------------------------- RX statistics "
  551. "-------------------------------\n");
  552. pr_debug("RX_PKT_IGNR_CNT %-8x\n", hwstat->rx_pkt_ignr);
  553. pr_debug("RX_LEN_CHK_ERR_CNT %-8x\n", hwstat->rx_len_chk_err);
  554. pr_debug("RX_LNG_FRM_CNT %-8x\n", hwstat->rx_lng_frm);
  555. pr_debug("RX_SHRT_FRM_CNT %-8x\n", hwstat->rx_shrt_frm);
  556. pr_debug("RX_IPG_VIOL_CNT %-8x\n", hwstat->rx_ipg_viol);
  557. pr_debug("RX_CRC_ERR_CNT %-8x\n", hwstat->rx_crc_err);
  558. pr_debug("RX_OK_PKT_CNT %-8x\n", hwstat->rx_ok_pkt);
  559. pr_debug("RX_CTL_FRM_CNT %-8x\n", hwstat->rx_ctl_frm);
  560. pr_debug("RX_PAUSE_FRM_CNT %-8x\n", hwstat->rx_pause_frm);
  561. pr_debug("RX_MULTICAST_CNT %-8x\n", hwstat->rx_multicast);
  562. pr_debug("RX_BROADCAST_CNT %-8x\n", hwstat->rx_broadcast);
  563. pr_debug("RX_VLAN_TAG_CNT %-8x\n", hwstat->rx_vlan_tag);
  564. pr_debug("RX_PRE_SHRINK_CNT %-8x\n", hwstat->rx_pre_shrink);
  565. pr_debug("RX_DRIB_NIB_CNT %-8x\n", hwstat->rx_drib_nib);
  566. pr_debug("RX_UNSUP_OPCD_CNT %-8x\n", hwstat->rx_unsup_opcd);
  567. pr_debug("RX_BYTE_CNT %-8x\n", hwstat->rx_byte);
  568. pr_debug("----------------------------- TX statistics "
  569. "-------------------------------\n");
  570. pr_debug("TX_UNICAST_CNT %-8x\n", hwstat->tx_unicast);
  571. pr_debug("TX_PAUSE_FRM_CNT %-8x\n", hwstat->tx_pause_frm);
  572. pr_debug("TX_MULTICAST_CNT %-8x\n", hwstat->tx_multicast);
  573. pr_debug("TX_BRDCAST_CNT %-8x\n", hwstat->tx_brdcast);
  574. pr_debug("TX_VLAN_TAG_CNT %-8x\n", hwstat->tx_vlan_tag);
  575. pr_debug("TX_BAD_FCS_CNT %-8x\n", hwstat->tx_bad_fcs);
  576. pr_debug("TX_JUMBO_CNT %-8x\n", hwstat->tx_jumbo);
  577. pr_debug("TX_BYTE_CNT %-8x\n", hwstat->tx_byte);
  578. }
  579. static struct net_device_stats *dnet_get_stats(struct net_device *dev)
  580. {
  581. struct dnet *bp = netdev_priv(dev);
  582. struct net_device_stats *nstat = &dev->stats;
  583. struct dnet_stats *hwstat = &bp->hw_stats;
  584. /* read stats from hardware */
  585. dnet_update_stats(bp);
  586. /* Convert HW stats into netdevice stats */
  587. nstat->rx_errors = (hwstat->rx_len_chk_err +
  588. hwstat->rx_lng_frm + hwstat->rx_shrt_frm +
  589. /* ignore IGP violation error
  590. hwstat->rx_ipg_viol + */
  591. hwstat->rx_crc_err +
  592. hwstat->rx_pre_shrink +
  593. hwstat->rx_drib_nib + hwstat->rx_unsup_opcd);
  594. nstat->tx_errors = hwstat->tx_bad_fcs;
  595. nstat->rx_length_errors = (hwstat->rx_len_chk_err +
  596. hwstat->rx_lng_frm +
  597. hwstat->rx_shrt_frm + hwstat->rx_pre_shrink);
  598. nstat->rx_crc_errors = hwstat->rx_crc_err;
  599. nstat->rx_frame_errors = hwstat->rx_pre_shrink + hwstat->rx_drib_nib;
  600. nstat->rx_packets = hwstat->rx_ok_pkt;
  601. nstat->tx_packets = (hwstat->tx_unicast +
  602. hwstat->tx_multicast + hwstat->tx_brdcast);
  603. nstat->rx_bytes = hwstat->rx_byte;
  604. nstat->tx_bytes = hwstat->tx_byte;
  605. nstat->multicast = hwstat->rx_multicast;
  606. nstat->rx_missed_errors = hwstat->rx_pkt_ignr;
  607. dnet_print_pretty_hwstats(hwstat);
  608. return nstat;
  609. }
  610. static int dnet_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  611. {
  612. struct dnet *bp = netdev_priv(dev);
  613. struct phy_device *phydev = bp->phy_dev;
  614. if (!phydev)
  615. return -ENODEV;
  616. return phy_ethtool_gset(phydev, cmd);
  617. }
  618. static int dnet_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  619. {
  620. struct dnet *bp = netdev_priv(dev);
  621. struct phy_device *phydev = bp->phy_dev;
  622. if (!phydev)
  623. return -ENODEV;
  624. return phy_ethtool_sset(phydev, cmd);
  625. }
  626. static int dnet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  627. {
  628. struct dnet *bp = netdev_priv(dev);
  629. struct phy_device *phydev = bp->phy_dev;
  630. if (!netif_running(dev))
  631. return -EINVAL;
  632. if (!phydev)
  633. return -ENODEV;
  634. return phy_mii_ioctl(phydev, rq, cmd);
  635. }
  636. static void dnet_get_drvinfo(struct net_device *dev,
  637. struct ethtool_drvinfo *info)
  638. {
  639. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  640. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  641. strlcpy(info->bus_info, "0", sizeof(info->bus_info));
  642. }
  643. static const struct ethtool_ops dnet_ethtool_ops = {
  644. .get_settings = dnet_get_settings,
  645. .set_settings = dnet_set_settings,
  646. .get_drvinfo = dnet_get_drvinfo,
  647. .get_link = ethtool_op_get_link,
  648. .get_ts_info = ethtool_op_get_ts_info,
  649. };
  650. static const struct net_device_ops dnet_netdev_ops = {
  651. .ndo_open = dnet_open,
  652. .ndo_stop = dnet_close,
  653. .ndo_get_stats = dnet_get_stats,
  654. .ndo_start_xmit = dnet_start_xmit,
  655. .ndo_do_ioctl = dnet_ioctl,
  656. .ndo_set_mac_address = eth_mac_addr,
  657. .ndo_validate_addr = eth_validate_addr,
  658. .ndo_change_mtu = eth_change_mtu,
  659. };
  660. static int dnet_probe(struct platform_device *pdev)
  661. {
  662. struct resource *res;
  663. struct net_device *dev;
  664. struct dnet *bp;
  665. struct phy_device *phydev;
  666. int err;
  667. unsigned int irq;
  668. irq = platform_get_irq(pdev, 0);
  669. dev = alloc_etherdev(sizeof(*bp));
  670. if (!dev)
  671. return -ENOMEM;
  672. /* TODO: Actually, we have some interesting features... */
  673. dev->features |= 0;
  674. bp = netdev_priv(dev);
  675. bp->dev = dev;
  676. platform_set_drvdata(pdev, dev);
  677. SET_NETDEV_DEV(dev, &pdev->dev);
  678. spin_lock_init(&bp->lock);
  679. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  680. bp->regs = devm_ioremap_resource(&pdev->dev, res);
  681. if (IS_ERR(bp->regs)) {
  682. err = PTR_ERR(bp->regs);
  683. goto err_out_free_dev;
  684. }
  685. dev->irq = irq;
  686. err = request_irq(dev->irq, dnet_interrupt, 0, DRV_NAME, dev);
  687. if (err) {
  688. dev_err(&pdev->dev, "Unable to request IRQ %d (error %d)\n",
  689. irq, err);
  690. goto err_out_free_dev;
  691. }
  692. dev->netdev_ops = &dnet_netdev_ops;
  693. netif_napi_add(dev, &bp->napi, dnet_poll, 64);
  694. dev->ethtool_ops = &dnet_ethtool_ops;
  695. dev->base_addr = (unsigned long)bp->regs;
  696. bp->capabilities = dnet_readl(bp, VERCAPS) & DNET_CAPS_MASK;
  697. dnet_get_hwaddr(bp);
  698. if (!is_valid_ether_addr(dev->dev_addr)) {
  699. /* choose a random ethernet address */
  700. eth_hw_addr_random(dev);
  701. __dnet_set_hwaddr(bp);
  702. }
  703. err = register_netdev(dev);
  704. if (err) {
  705. dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
  706. goto err_out_free_irq;
  707. }
  708. /* register the PHY board fixup (for Marvell 88E1111) */
  709. err = phy_register_fixup_for_uid(0x01410cc0, 0xfffffff0,
  710. dnet_phy_marvell_fixup);
  711. /* we can live without it, so just issue a warning */
  712. if (err)
  713. dev_warn(&pdev->dev, "Cannot register PHY board fixup.\n");
  714. err = dnet_mii_init(bp);
  715. if (err)
  716. goto err_out_unregister_netdev;
  717. dev_info(&pdev->dev, "Dave DNET at 0x%p (0x%08x) irq %d %pM\n",
  718. bp->regs, (unsigned int)res->start, dev->irq, dev->dev_addr);
  719. dev_info(&pdev->dev, "has %smdio, %sirq, %sgigabit, %sdma\n",
  720. (bp->capabilities & DNET_HAS_MDIO) ? "" : "no ",
  721. (bp->capabilities & DNET_HAS_IRQ) ? "" : "no ",
  722. (bp->capabilities & DNET_HAS_GIGABIT) ? "" : "no ",
  723. (bp->capabilities & DNET_HAS_DMA) ? "" : "no ");
  724. phydev = bp->phy_dev;
  725. dev_info(&pdev->dev, "attached PHY driver [%s] "
  726. "(mii_bus:phy_addr=%s, irq=%d)\n",
  727. phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
  728. return 0;
  729. err_out_unregister_netdev:
  730. unregister_netdev(dev);
  731. err_out_free_irq:
  732. free_irq(dev->irq, dev);
  733. err_out_free_dev:
  734. free_netdev(dev);
  735. return err;
  736. }
  737. static int dnet_remove(struct platform_device *pdev)
  738. {
  739. struct net_device *dev;
  740. struct dnet *bp;
  741. dev = platform_get_drvdata(pdev);
  742. if (dev) {
  743. bp = netdev_priv(dev);
  744. if (bp->phy_dev)
  745. phy_disconnect(bp->phy_dev);
  746. mdiobus_unregister(bp->mii_bus);
  747. mdiobus_free(bp->mii_bus);
  748. unregister_netdev(dev);
  749. free_irq(dev->irq, dev);
  750. free_netdev(dev);
  751. }
  752. return 0;
  753. }
  754. static struct platform_driver dnet_driver = {
  755. .probe = dnet_probe,
  756. .remove = dnet_remove,
  757. .driver = {
  758. .name = "dnet",
  759. },
  760. };
  761. module_platform_driver(dnet_driver);
  762. MODULE_LICENSE("GPL");
  763. MODULE_DESCRIPTION("Dave DNET Ethernet driver");
  764. MODULE_AUTHOR("Ilya Yanok <yanok@emcraft.com>, "
  765. "Matteo Vit <matteo.vit@dave.eu>");