dnet.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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 = dev->phydev;
  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. /* find the first phy */
  211. phydev = phy_find_first(bp->mii_bus);
  212. if (!phydev) {
  213. printk(KERN_ERR "%s: no PHY found\n", dev->name);
  214. return -ENODEV;
  215. }
  216. /* TODO : add pin_irq */
  217. /* attach the mac to the phy */
  218. if (bp->capabilities & DNET_HAS_RMII) {
  219. phydev = phy_connect(dev, phydev_name(phydev),
  220. &dnet_handle_link_change,
  221. PHY_INTERFACE_MODE_RMII);
  222. } else {
  223. phydev = phy_connect(dev, phydev_name(phydev),
  224. &dnet_handle_link_change,
  225. PHY_INTERFACE_MODE_MII);
  226. }
  227. if (IS_ERR(phydev)) {
  228. printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
  229. return PTR_ERR(phydev);
  230. }
  231. /* mask with MAC supported features */
  232. if (bp->capabilities & DNET_HAS_GIGABIT)
  233. phydev->supported &= PHY_GBIT_FEATURES;
  234. else
  235. phydev->supported &= PHY_BASIC_FEATURES;
  236. phydev->supported |= SUPPORTED_Asym_Pause | SUPPORTED_Pause;
  237. phydev->advertising = phydev->supported;
  238. bp->link = 0;
  239. bp->speed = 0;
  240. bp->duplex = -1;
  241. return 0;
  242. }
  243. static int dnet_mii_init(struct dnet *bp)
  244. {
  245. int err;
  246. bp->mii_bus = mdiobus_alloc();
  247. if (bp->mii_bus == NULL)
  248. return -ENOMEM;
  249. bp->mii_bus->name = "dnet_mii_bus";
  250. bp->mii_bus->read = &dnet_mdio_read;
  251. bp->mii_bus->write = &dnet_mdio_write;
  252. snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  253. bp->pdev->name, bp->pdev->id);
  254. bp->mii_bus->priv = bp;
  255. if (mdiobus_register(bp->mii_bus)) {
  256. err = -ENXIO;
  257. goto err_out;
  258. }
  259. if (dnet_mii_probe(bp->dev) != 0) {
  260. err = -ENXIO;
  261. goto err_out_unregister_bus;
  262. }
  263. return 0;
  264. err_out_unregister_bus:
  265. mdiobus_unregister(bp->mii_bus);
  266. err_out:
  267. mdiobus_free(bp->mii_bus);
  268. return err;
  269. }
  270. /* For Neptune board: LINK1000 as Link LED and TX as activity LED */
  271. static int dnet_phy_marvell_fixup(struct phy_device *phydev)
  272. {
  273. return phy_write(phydev, 0x18, 0x4148);
  274. }
  275. static void dnet_update_stats(struct dnet *bp)
  276. {
  277. u32 __iomem *reg = bp->regs + DNET_RX_PKT_IGNR_CNT;
  278. u32 *p = &bp->hw_stats.rx_pkt_ignr;
  279. u32 *end = &bp->hw_stats.rx_byte + 1;
  280. WARN_ON((unsigned long)(end - p - 1) !=
  281. (DNET_RX_BYTE_CNT - DNET_RX_PKT_IGNR_CNT) / 4);
  282. for (; p < end; p++, reg++)
  283. *p += readl(reg);
  284. reg = bp->regs + DNET_TX_UNICAST_CNT;
  285. p = &bp->hw_stats.tx_unicast;
  286. end = &bp->hw_stats.tx_byte + 1;
  287. WARN_ON((unsigned long)(end - p - 1) !=
  288. (DNET_TX_BYTE_CNT - DNET_TX_UNICAST_CNT) / 4);
  289. for (; p < end; p++, reg++)
  290. *p += readl(reg);
  291. }
  292. static int dnet_poll(struct napi_struct *napi, int budget)
  293. {
  294. struct dnet *bp = container_of(napi, struct dnet, napi);
  295. struct net_device *dev = bp->dev;
  296. int npackets = 0;
  297. unsigned int pkt_len;
  298. struct sk_buff *skb;
  299. unsigned int *data_ptr;
  300. u32 int_enable;
  301. u32 cmd_word;
  302. int i;
  303. while (npackets < budget) {
  304. /*
  305. * break out of while loop if there are no more
  306. * packets waiting
  307. */
  308. if (!(dnet_readl(bp, RX_FIFO_WCNT) >> 16))
  309. break;
  310. cmd_word = dnet_readl(bp, RX_LEN_FIFO);
  311. pkt_len = cmd_word & 0xFFFF;
  312. if (cmd_word & 0xDF180000)
  313. printk(KERN_ERR "%s packet receive error %x\n",
  314. __func__, cmd_word);
  315. skb = netdev_alloc_skb(dev, pkt_len + 5);
  316. if (skb != NULL) {
  317. /* Align IP on 16 byte boundaries */
  318. skb_reserve(skb, 2);
  319. /*
  320. * 'skb_put()' points to the start of sk_buff
  321. * data area.
  322. */
  323. data_ptr = skb_put(skb, pkt_len);
  324. for (i = 0; i < (pkt_len + 3) >> 2; i++)
  325. *data_ptr++ = dnet_readl(bp, RX_DATA_FIFO);
  326. skb->protocol = eth_type_trans(skb, dev);
  327. netif_receive_skb(skb);
  328. npackets++;
  329. } else
  330. printk(KERN_NOTICE
  331. "%s: No memory to allocate a sk_buff of "
  332. "size %u.\n", dev->name, pkt_len);
  333. }
  334. if (npackets < budget) {
  335. /* We processed all packets available. Tell NAPI it can
  336. * stop polling then re-enable rx interrupts.
  337. */
  338. napi_complete_done(napi, npackets);
  339. int_enable = dnet_readl(bp, INTR_ENB);
  340. int_enable |= DNET_INTR_SRC_RX_CMDFIFOAF;
  341. dnet_writel(bp, int_enable, INTR_ENB);
  342. }
  343. return npackets;
  344. }
  345. static irqreturn_t dnet_interrupt(int irq, void *dev_id)
  346. {
  347. struct net_device *dev = dev_id;
  348. struct dnet *bp = netdev_priv(dev);
  349. u32 int_src, int_enable, int_current;
  350. unsigned long flags;
  351. unsigned int handled = 0;
  352. spin_lock_irqsave(&bp->lock, flags);
  353. /* read and clear the DNET irq (clear on read) */
  354. int_src = dnet_readl(bp, INTR_SRC);
  355. int_enable = dnet_readl(bp, INTR_ENB);
  356. int_current = int_src & int_enable;
  357. /* restart the queue if we had stopped it for TX fifo almost full */
  358. if (int_current & DNET_INTR_SRC_TX_FIFOAE) {
  359. int_enable = dnet_readl(bp, INTR_ENB);
  360. int_enable &= ~DNET_INTR_ENB_TX_FIFOAE;
  361. dnet_writel(bp, int_enable, INTR_ENB);
  362. netif_wake_queue(dev);
  363. handled = 1;
  364. }
  365. /* RX FIFO error checking */
  366. if (int_current &
  367. (DNET_INTR_SRC_RX_CMDFIFOFF | DNET_INTR_SRC_RX_DATAFIFOFF)) {
  368. printk(KERN_ERR "%s: RX fifo error %x, irq %x\n", __func__,
  369. dnet_readl(bp, RX_STATUS), int_current);
  370. /* we can only flush the RX FIFOs */
  371. dnet_writel(bp, DNET_SYS_CTL_RXFIFOFLUSH, SYS_CTL);
  372. ndelay(500);
  373. dnet_writel(bp, 0, SYS_CTL);
  374. handled = 1;
  375. }
  376. /* TX FIFO error checking */
  377. if (int_current &
  378. (DNET_INTR_SRC_TX_FIFOFULL | DNET_INTR_SRC_TX_DISCFRM)) {
  379. printk(KERN_ERR "%s: TX fifo error %x, irq %x\n", __func__,
  380. dnet_readl(bp, TX_STATUS), int_current);
  381. /* we can only flush the TX FIFOs */
  382. dnet_writel(bp, DNET_SYS_CTL_TXFIFOFLUSH, SYS_CTL);
  383. ndelay(500);
  384. dnet_writel(bp, 0, SYS_CTL);
  385. handled = 1;
  386. }
  387. if (int_current & DNET_INTR_SRC_RX_CMDFIFOAF) {
  388. if (napi_schedule_prep(&bp->napi)) {
  389. /*
  390. * There's no point taking any more interrupts
  391. * until we have processed the buffers
  392. */
  393. /* Disable Rx interrupts and schedule NAPI poll */
  394. int_enable = dnet_readl(bp, INTR_ENB);
  395. int_enable &= ~DNET_INTR_SRC_RX_CMDFIFOAF;
  396. dnet_writel(bp, int_enable, INTR_ENB);
  397. __napi_schedule(&bp->napi);
  398. }
  399. handled = 1;
  400. }
  401. if (!handled)
  402. pr_debug("%s: irq %x remains\n", __func__, int_current);
  403. spin_unlock_irqrestore(&bp->lock, flags);
  404. return IRQ_RETVAL(handled);
  405. }
  406. #ifdef DEBUG
  407. static inline void dnet_print_skb(struct sk_buff *skb)
  408. {
  409. int k;
  410. printk(KERN_DEBUG PFX "data:");
  411. for (k = 0; k < skb->len; k++)
  412. printk(" %02x", (unsigned int)skb->data[k]);
  413. printk("\n");
  414. }
  415. #else
  416. #define dnet_print_skb(skb) do {} while (0)
  417. #endif
  418. static netdev_tx_t dnet_start_xmit(struct sk_buff *skb, struct net_device *dev)
  419. {
  420. struct dnet *bp = netdev_priv(dev);
  421. u32 tx_status, irq_enable;
  422. unsigned int len, i, tx_cmd, wrsz;
  423. unsigned long flags;
  424. unsigned int *bufp;
  425. tx_status = dnet_readl(bp, TX_STATUS);
  426. pr_debug("start_xmit: len %u head %p data %p\n",
  427. skb->len, skb->head, skb->data);
  428. dnet_print_skb(skb);
  429. /* frame size (words) */
  430. len = (skb->len + 3) >> 2;
  431. spin_lock_irqsave(&bp->lock, flags);
  432. tx_status = dnet_readl(bp, TX_STATUS);
  433. bufp = (unsigned int *)(((unsigned long) skb->data) & ~0x3UL);
  434. wrsz = (u32) skb->len + 3;
  435. wrsz += ((unsigned long) skb->data) & 0x3;
  436. wrsz >>= 2;
  437. tx_cmd = ((((unsigned long)(skb->data)) & 0x03) << 16) | (u32) skb->len;
  438. /* check if there is enough room for the current frame */
  439. if (wrsz < (DNET_FIFO_SIZE - dnet_readl(bp, TX_FIFO_WCNT))) {
  440. for (i = 0; i < wrsz; i++)
  441. dnet_writel(bp, *bufp++, TX_DATA_FIFO);
  442. /*
  443. * inform MAC that a packet's written and ready to be
  444. * shipped out
  445. */
  446. dnet_writel(bp, tx_cmd, TX_LEN_FIFO);
  447. }
  448. if (dnet_readl(bp, TX_FIFO_WCNT) > DNET_FIFO_TX_DATA_AF_TH) {
  449. netif_stop_queue(dev);
  450. tx_status = dnet_readl(bp, INTR_SRC);
  451. irq_enable = dnet_readl(bp, INTR_ENB);
  452. irq_enable |= DNET_INTR_ENB_TX_FIFOAE;
  453. dnet_writel(bp, irq_enable, INTR_ENB);
  454. }
  455. skb_tx_timestamp(skb);
  456. /* free the buffer */
  457. dev_kfree_skb(skb);
  458. spin_unlock_irqrestore(&bp->lock, flags);
  459. return NETDEV_TX_OK;
  460. }
  461. static void dnet_reset_hw(struct dnet *bp)
  462. {
  463. /* put ts_mac in IDLE state i.e. disable rx/tx */
  464. dnet_writew_mac(bp, DNET_INTERNAL_MODE_REG, DNET_INTERNAL_MODE_FCEN);
  465. /*
  466. * RX FIFO almost full threshold: only cmd FIFO almost full is
  467. * implemented for RX side
  468. */
  469. dnet_writel(bp, DNET_FIFO_RX_CMD_AF_TH, RX_FIFO_TH);
  470. /*
  471. * TX FIFO almost empty threshold: only data FIFO almost empty
  472. * is implemented for TX side
  473. */
  474. dnet_writel(bp, DNET_FIFO_TX_DATA_AE_TH, TX_FIFO_TH);
  475. /* flush rx/tx fifos */
  476. dnet_writel(bp, DNET_SYS_CTL_RXFIFOFLUSH | DNET_SYS_CTL_TXFIFOFLUSH,
  477. SYS_CTL);
  478. msleep(1);
  479. dnet_writel(bp, 0, SYS_CTL);
  480. }
  481. static void dnet_init_hw(struct dnet *bp)
  482. {
  483. u32 config;
  484. dnet_reset_hw(bp);
  485. __dnet_set_hwaddr(bp);
  486. config = dnet_readw_mac(bp, DNET_INTERNAL_RXTX_CONTROL_REG);
  487. if (bp->dev->flags & IFF_PROMISC)
  488. /* Copy All Frames */
  489. config |= DNET_INTERNAL_RXTX_CONTROL_ENPROMISC;
  490. if (!(bp->dev->flags & IFF_BROADCAST))
  491. /* No BroadCast */
  492. config |= DNET_INTERNAL_RXTX_CONTROL_RXMULTICAST;
  493. config |= DNET_INTERNAL_RXTX_CONTROL_RXPAUSE |
  494. DNET_INTERNAL_RXTX_CONTROL_RXBROADCAST |
  495. DNET_INTERNAL_RXTX_CONTROL_DROPCONTROL |
  496. DNET_INTERNAL_RXTX_CONTROL_DISCFXFCS;
  497. dnet_writew_mac(bp, DNET_INTERNAL_RXTX_CONTROL_REG, config);
  498. /* clear irq before enabling them */
  499. config = dnet_readl(bp, INTR_SRC);
  500. /* enable RX/TX interrupt, recv packet ready interrupt */
  501. dnet_writel(bp, DNET_INTR_ENB_GLOBAL_ENABLE | DNET_INTR_ENB_RX_SUMMARY |
  502. DNET_INTR_ENB_TX_SUMMARY | DNET_INTR_ENB_RX_FIFOERR |
  503. DNET_INTR_ENB_RX_ERROR | DNET_INTR_ENB_RX_FIFOFULL |
  504. DNET_INTR_ENB_TX_FIFOFULL | DNET_INTR_ENB_TX_DISCFRM |
  505. DNET_INTR_ENB_RX_PKTRDY, INTR_ENB);
  506. }
  507. static int dnet_open(struct net_device *dev)
  508. {
  509. struct dnet *bp = netdev_priv(dev);
  510. /* if the phy is not yet register, retry later */
  511. if (!dev->phydev)
  512. return -EAGAIN;
  513. napi_enable(&bp->napi);
  514. dnet_init_hw(bp);
  515. phy_start_aneg(dev->phydev);
  516. /* schedule a link state check */
  517. phy_start(dev->phydev);
  518. netif_start_queue(dev);
  519. return 0;
  520. }
  521. static int dnet_close(struct net_device *dev)
  522. {
  523. struct dnet *bp = netdev_priv(dev);
  524. netif_stop_queue(dev);
  525. napi_disable(&bp->napi);
  526. if (dev->phydev)
  527. phy_stop(dev->phydev);
  528. dnet_reset_hw(bp);
  529. netif_carrier_off(dev);
  530. return 0;
  531. }
  532. static inline void dnet_print_pretty_hwstats(struct dnet_stats *hwstat)
  533. {
  534. pr_debug("%s\n", __func__);
  535. pr_debug("----------------------------- RX statistics "
  536. "-------------------------------\n");
  537. pr_debug("RX_PKT_IGNR_CNT %-8x\n", hwstat->rx_pkt_ignr);
  538. pr_debug("RX_LEN_CHK_ERR_CNT %-8x\n", hwstat->rx_len_chk_err);
  539. pr_debug("RX_LNG_FRM_CNT %-8x\n", hwstat->rx_lng_frm);
  540. pr_debug("RX_SHRT_FRM_CNT %-8x\n", hwstat->rx_shrt_frm);
  541. pr_debug("RX_IPG_VIOL_CNT %-8x\n", hwstat->rx_ipg_viol);
  542. pr_debug("RX_CRC_ERR_CNT %-8x\n", hwstat->rx_crc_err);
  543. pr_debug("RX_OK_PKT_CNT %-8x\n", hwstat->rx_ok_pkt);
  544. pr_debug("RX_CTL_FRM_CNT %-8x\n", hwstat->rx_ctl_frm);
  545. pr_debug("RX_PAUSE_FRM_CNT %-8x\n", hwstat->rx_pause_frm);
  546. pr_debug("RX_MULTICAST_CNT %-8x\n", hwstat->rx_multicast);
  547. pr_debug("RX_BROADCAST_CNT %-8x\n", hwstat->rx_broadcast);
  548. pr_debug("RX_VLAN_TAG_CNT %-8x\n", hwstat->rx_vlan_tag);
  549. pr_debug("RX_PRE_SHRINK_CNT %-8x\n", hwstat->rx_pre_shrink);
  550. pr_debug("RX_DRIB_NIB_CNT %-8x\n", hwstat->rx_drib_nib);
  551. pr_debug("RX_UNSUP_OPCD_CNT %-8x\n", hwstat->rx_unsup_opcd);
  552. pr_debug("RX_BYTE_CNT %-8x\n", hwstat->rx_byte);
  553. pr_debug("----------------------------- TX statistics "
  554. "-------------------------------\n");
  555. pr_debug("TX_UNICAST_CNT %-8x\n", hwstat->tx_unicast);
  556. pr_debug("TX_PAUSE_FRM_CNT %-8x\n", hwstat->tx_pause_frm);
  557. pr_debug("TX_MULTICAST_CNT %-8x\n", hwstat->tx_multicast);
  558. pr_debug("TX_BRDCAST_CNT %-8x\n", hwstat->tx_brdcast);
  559. pr_debug("TX_VLAN_TAG_CNT %-8x\n", hwstat->tx_vlan_tag);
  560. pr_debug("TX_BAD_FCS_CNT %-8x\n", hwstat->tx_bad_fcs);
  561. pr_debug("TX_JUMBO_CNT %-8x\n", hwstat->tx_jumbo);
  562. pr_debug("TX_BYTE_CNT %-8x\n", hwstat->tx_byte);
  563. }
  564. static struct net_device_stats *dnet_get_stats(struct net_device *dev)
  565. {
  566. struct dnet *bp = netdev_priv(dev);
  567. struct net_device_stats *nstat = &dev->stats;
  568. struct dnet_stats *hwstat = &bp->hw_stats;
  569. /* read stats from hardware */
  570. dnet_update_stats(bp);
  571. /* Convert HW stats into netdevice stats */
  572. nstat->rx_errors = (hwstat->rx_len_chk_err +
  573. hwstat->rx_lng_frm + hwstat->rx_shrt_frm +
  574. /* ignore IGP violation error
  575. hwstat->rx_ipg_viol + */
  576. hwstat->rx_crc_err +
  577. hwstat->rx_pre_shrink +
  578. hwstat->rx_drib_nib + hwstat->rx_unsup_opcd);
  579. nstat->tx_errors = hwstat->tx_bad_fcs;
  580. nstat->rx_length_errors = (hwstat->rx_len_chk_err +
  581. hwstat->rx_lng_frm +
  582. hwstat->rx_shrt_frm + hwstat->rx_pre_shrink);
  583. nstat->rx_crc_errors = hwstat->rx_crc_err;
  584. nstat->rx_frame_errors = hwstat->rx_pre_shrink + hwstat->rx_drib_nib;
  585. nstat->rx_packets = hwstat->rx_ok_pkt;
  586. nstat->tx_packets = (hwstat->tx_unicast +
  587. hwstat->tx_multicast + hwstat->tx_brdcast);
  588. nstat->rx_bytes = hwstat->rx_byte;
  589. nstat->tx_bytes = hwstat->tx_byte;
  590. nstat->multicast = hwstat->rx_multicast;
  591. nstat->rx_missed_errors = hwstat->rx_pkt_ignr;
  592. dnet_print_pretty_hwstats(hwstat);
  593. return nstat;
  594. }
  595. static int dnet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  596. {
  597. struct phy_device *phydev = dev->phydev;
  598. if (!netif_running(dev))
  599. return -EINVAL;
  600. if (!phydev)
  601. return -ENODEV;
  602. return phy_mii_ioctl(phydev, rq, cmd);
  603. }
  604. static void dnet_get_drvinfo(struct net_device *dev,
  605. struct ethtool_drvinfo *info)
  606. {
  607. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  608. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  609. strlcpy(info->bus_info, "0", sizeof(info->bus_info));
  610. }
  611. static const struct ethtool_ops dnet_ethtool_ops = {
  612. .get_drvinfo = dnet_get_drvinfo,
  613. .get_link = ethtool_op_get_link,
  614. .get_ts_info = ethtool_op_get_ts_info,
  615. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  616. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  617. };
  618. static const struct net_device_ops dnet_netdev_ops = {
  619. .ndo_open = dnet_open,
  620. .ndo_stop = dnet_close,
  621. .ndo_get_stats = dnet_get_stats,
  622. .ndo_start_xmit = dnet_start_xmit,
  623. .ndo_do_ioctl = dnet_ioctl,
  624. .ndo_set_mac_address = eth_mac_addr,
  625. .ndo_validate_addr = eth_validate_addr,
  626. };
  627. static int dnet_probe(struct platform_device *pdev)
  628. {
  629. struct resource *res;
  630. struct net_device *dev;
  631. struct dnet *bp;
  632. struct phy_device *phydev;
  633. int err;
  634. unsigned int irq;
  635. irq = platform_get_irq(pdev, 0);
  636. dev = alloc_etherdev(sizeof(*bp));
  637. if (!dev)
  638. return -ENOMEM;
  639. /* TODO: Actually, we have some interesting features... */
  640. dev->features |= 0;
  641. bp = netdev_priv(dev);
  642. bp->dev = dev;
  643. platform_set_drvdata(pdev, dev);
  644. SET_NETDEV_DEV(dev, &pdev->dev);
  645. spin_lock_init(&bp->lock);
  646. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  647. bp->regs = devm_ioremap_resource(&pdev->dev, res);
  648. if (IS_ERR(bp->regs)) {
  649. err = PTR_ERR(bp->regs);
  650. goto err_out_free_dev;
  651. }
  652. dev->irq = irq;
  653. err = request_irq(dev->irq, dnet_interrupt, 0, DRV_NAME, dev);
  654. if (err) {
  655. dev_err(&pdev->dev, "Unable to request IRQ %d (error %d)\n",
  656. irq, err);
  657. goto err_out_free_dev;
  658. }
  659. dev->netdev_ops = &dnet_netdev_ops;
  660. netif_napi_add(dev, &bp->napi, dnet_poll, 64);
  661. dev->ethtool_ops = &dnet_ethtool_ops;
  662. dev->base_addr = (unsigned long)bp->regs;
  663. bp->capabilities = dnet_readl(bp, VERCAPS) & DNET_CAPS_MASK;
  664. dnet_get_hwaddr(bp);
  665. if (!is_valid_ether_addr(dev->dev_addr)) {
  666. /* choose a random ethernet address */
  667. eth_hw_addr_random(dev);
  668. __dnet_set_hwaddr(bp);
  669. }
  670. err = register_netdev(dev);
  671. if (err) {
  672. dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
  673. goto err_out_free_irq;
  674. }
  675. /* register the PHY board fixup (for Marvell 88E1111) */
  676. err = phy_register_fixup_for_uid(0x01410cc0, 0xfffffff0,
  677. dnet_phy_marvell_fixup);
  678. /* we can live without it, so just issue a warning */
  679. if (err)
  680. dev_warn(&pdev->dev, "Cannot register PHY board fixup.\n");
  681. err = dnet_mii_init(bp);
  682. if (err)
  683. goto err_out_unregister_netdev;
  684. dev_info(&pdev->dev, "Dave DNET at 0x%p (0x%08x) irq %d %pM\n",
  685. bp->regs, (unsigned int)res->start, dev->irq, dev->dev_addr);
  686. dev_info(&pdev->dev, "has %smdio, %sirq, %sgigabit, %sdma\n",
  687. (bp->capabilities & DNET_HAS_MDIO) ? "" : "no ",
  688. (bp->capabilities & DNET_HAS_IRQ) ? "" : "no ",
  689. (bp->capabilities & DNET_HAS_GIGABIT) ? "" : "no ",
  690. (bp->capabilities & DNET_HAS_DMA) ? "" : "no ");
  691. phydev = dev->phydev;
  692. phy_attached_info(phydev);
  693. return 0;
  694. err_out_unregister_netdev:
  695. unregister_netdev(dev);
  696. err_out_free_irq:
  697. free_irq(dev->irq, dev);
  698. err_out_free_dev:
  699. free_netdev(dev);
  700. return err;
  701. }
  702. static int dnet_remove(struct platform_device *pdev)
  703. {
  704. struct net_device *dev;
  705. struct dnet *bp;
  706. dev = platform_get_drvdata(pdev);
  707. if (dev) {
  708. bp = netdev_priv(dev);
  709. if (dev->phydev)
  710. phy_disconnect(dev->phydev);
  711. mdiobus_unregister(bp->mii_bus);
  712. mdiobus_free(bp->mii_bus);
  713. unregister_netdev(dev);
  714. free_irq(dev->irq, dev);
  715. free_netdev(dev);
  716. }
  717. return 0;
  718. }
  719. static struct platform_driver dnet_driver = {
  720. .probe = dnet_probe,
  721. .remove = dnet_remove,
  722. .driver = {
  723. .name = "dnet",
  724. },
  725. };
  726. module_platform_driver(dnet_driver);
  727. MODULE_LICENSE("GPL");
  728. MODULE_DESCRIPTION("Dave DNET Ethernet driver");
  729. MODULE_AUTHOR("Ilya Yanok <yanok@emcraft.com>, "
  730. "Matteo Vit <matteo.vit@dave.eu>");