dnet.c 25 KB

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