ax88796.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. /* drivers/net/ethernet/8390/ax88796.c
  2. *
  3. * Copyright 2005,2007 Simtec Electronics
  4. * Ben Dooks <ben@simtec.co.uk>
  5. *
  6. * Asix AX88796 10/100 Ethernet controller support
  7. * Based on ne.c, by Donald Becker, et-al.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/errno.h>
  16. #include <linux/isapnp.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/delay.h>
  21. #include <linux/timer.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/ethtool.h>
  25. #include <linux/mdio-bitbang.h>
  26. #include <linux/phy.h>
  27. #include <linux/eeprom_93cx6.h>
  28. #include <linux/slab.h>
  29. #include <net/ax88796.h>
  30. /* Rename the lib8390.c functions to show that they are in this driver */
  31. #define __ei_open ax_ei_open
  32. #define __ei_close ax_ei_close
  33. #define __ei_poll ax_ei_poll
  34. #define __ei_start_xmit ax_ei_start_xmit
  35. #define __ei_tx_timeout ax_ei_tx_timeout
  36. #define __ei_get_stats ax_ei_get_stats
  37. #define __ei_set_multicast_list ax_ei_set_multicast_list
  38. #define __ei_interrupt ax_ei_interrupt
  39. #define ____alloc_ei_netdev ax__alloc_ei_netdev
  40. #define __NS8390_init ax_NS8390_init
  41. /* force unsigned long back to 'void __iomem *' */
  42. #define ax_convert_addr(_a) ((void __force __iomem *)(_a))
  43. #define ei_inb(_a) readb(ax_convert_addr(_a))
  44. #define ei_outb(_v, _a) writeb(_v, ax_convert_addr(_a))
  45. #define ei_inb_p(_a) ei_inb(_a)
  46. #define ei_outb_p(_v, _a) ei_outb(_v, _a)
  47. /* define EI_SHIFT() to take into account our register offsets */
  48. #define EI_SHIFT(x) (ei_local->reg_offset[(x)])
  49. /* Ensure we have our RCR base value */
  50. #define AX88796_PLATFORM
  51. static unsigned char version[] = "ax88796.c: Copyright 2005,2007 Simtec Electronics\n";
  52. #include "lib8390.c"
  53. #define DRV_NAME "ax88796"
  54. #define DRV_VERSION "1.00"
  55. /* from ne.c */
  56. #define NE_CMD EI_SHIFT(0x00)
  57. #define NE_RESET EI_SHIFT(0x1f)
  58. #define NE_DATAPORT EI_SHIFT(0x10)
  59. #define NE1SM_START_PG 0x20 /* First page of TX buffer */
  60. #define NE1SM_STOP_PG 0x40 /* Last page +1 of RX ring */
  61. #define NESM_START_PG 0x40 /* First page of TX buffer */
  62. #define NESM_STOP_PG 0x80 /* Last page +1 of RX ring */
  63. #define AX_GPOC_PPDSET BIT(6)
  64. /* device private data */
  65. struct ax_device {
  66. struct mii_bus *mii_bus;
  67. struct mdiobb_ctrl bb_ctrl;
  68. void __iomem *addr_memr;
  69. u8 reg_memr;
  70. int link;
  71. int speed;
  72. int duplex;
  73. void __iomem *map2;
  74. const struct ax_plat_data *plat;
  75. unsigned char running;
  76. unsigned char resume_open;
  77. unsigned int irqflags;
  78. u32 reg_offsets[0x20];
  79. };
  80. static inline struct ax_device *to_ax_dev(struct net_device *dev)
  81. {
  82. struct ei_device *ei_local = netdev_priv(dev);
  83. return (struct ax_device *)(ei_local + 1);
  84. }
  85. /*
  86. * ax_initial_check
  87. *
  88. * do an initial probe for the card to check whether it exists
  89. * and is functional
  90. */
  91. static int ax_initial_check(struct net_device *dev)
  92. {
  93. struct ei_device *ei_local = netdev_priv(dev);
  94. void __iomem *ioaddr = ei_local->mem;
  95. int reg0;
  96. int regd;
  97. reg0 = ei_inb(ioaddr);
  98. if (reg0 == 0xFF)
  99. return -ENODEV;
  100. ei_outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP, ioaddr + E8390_CMD);
  101. regd = ei_inb(ioaddr + 0x0d);
  102. ei_outb(0xff, ioaddr + 0x0d);
  103. ei_outb(E8390_NODMA + E8390_PAGE0, ioaddr + E8390_CMD);
  104. ei_inb(ioaddr + EN0_COUNTER0); /* Clear the counter by reading. */
  105. if (ei_inb(ioaddr + EN0_COUNTER0) != 0) {
  106. ei_outb(reg0, ioaddr);
  107. ei_outb(regd, ioaddr + 0x0d); /* Restore the old values. */
  108. return -ENODEV;
  109. }
  110. return 0;
  111. }
  112. /*
  113. * Hard reset the card. This used to pause for the same period that a
  114. * 8390 reset command required, but that shouldn't be necessary.
  115. */
  116. static void ax_reset_8390(struct net_device *dev)
  117. {
  118. struct ei_device *ei_local = netdev_priv(dev);
  119. unsigned long reset_start_time = jiffies;
  120. void __iomem *addr = (void __iomem *)dev->base_addr;
  121. netif_dbg(ei_local, hw, dev, "resetting the 8390 t=%ld...\n", jiffies);
  122. ei_outb(ei_inb(addr + NE_RESET), addr + NE_RESET);
  123. ei_local->txing = 0;
  124. ei_local->dmaing = 0;
  125. /* This check _should_not_ be necessary, omit eventually. */
  126. while ((ei_inb(addr + EN0_ISR) & ENISR_RESET) == 0) {
  127. if (time_after(jiffies, reset_start_time + 2 * HZ / 100)) {
  128. netdev_warn(dev, "%s: did not complete.\n", __func__);
  129. break;
  130. }
  131. }
  132. ei_outb(ENISR_RESET, addr + EN0_ISR); /* Ack intr. */
  133. }
  134. /* Wrapper for __ei_interrupt for platforms that have a platform-specific
  135. * way to find out whether the interrupt request might be caused by
  136. * the ax88796 chip.
  137. */
  138. static irqreturn_t ax_ei_interrupt_filtered(int irq, void *dev_id)
  139. {
  140. struct net_device *dev = dev_id;
  141. struct ax_device *ax = to_ax_dev(dev);
  142. struct platform_device *pdev = to_platform_device(dev->dev.parent);
  143. if (!ax->plat->check_irq(pdev))
  144. return IRQ_NONE;
  145. return ax_ei_interrupt(irq, dev_id);
  146. }
  147. static void ax_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
  148. int ring_page)
  149. {
  150. struct ei_device *ei_local = netdev_priv(dev);
  151. void __iomem *nic_base = ei_local->mem;
  152. /* This *shouldn't* happen. If it does, it's the last thing you'll see */
  153. if (ei_local->dmaing) {
  154. netdev_err(dev, "DMAing conflict in %s "
  155. "[DMAstat:%d][irqlock:%d].\n",
  156. __func__,
  157. ei_local->dmaing, ei_local->irqlock);
  158. return;
  159. }
  160. ei_local->dmaing |= 0x01;
  161. ei_outb(E8390_NODMA + E8390_PAGE0 + E8390_START, nic_base + NE_CMD);
  162. ei_outb(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
  163. ei_outb(0, nic_base + EN0_RCNTHI);
  164. ei_outb(0, nic_base + EN0_RSARLO); /* On page boundary */
  165. ei_outb(ring_page, nic_base + EN0_RSARHI);
  166. ei_outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
  167. if (ei_local->word16)
  168. ioread16_rep(nic_base + NE_DATAPORT, hdr,
  169. sizeof(struct e8390_pkt_hdr) >> 1);
  170. else
  171. ioread8_rep(nic_base + NE_DATAPORT, hdr,
  172. sizeof(struct e8390_pkt_hdr));
  173. ei_outb(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
  174. ei_local->dmaing &= ~0x01;
  175. le16_to_cpus(&hdr->count);
  176. }
  177. /*
  178. * Block input and output, similar to the Crynwr packet driver. If
  179. * you are porting to a new ethercard, look at the packet driver
  180. * source for hints. The NEx000 doesn't share the on-board packet
  181. * memory -- you have to put the packet out through the "remote DMA"
  182. * dataport using ei_outb.
  183. */
  184. static void ax_block_input(struct net_device *dev, int count,
  185. struct sk_buff *skb, int ring_offset)
  186. {
  187. struct ei_device *ei_local = netdev_priv(dev);
  188. void __iomem *nic_base = ei_local->mem;
  189. char *buf = skb->data;
  190. if (ei_local->dmaing) {
  191. netdev_err(dev,
  192. "DMAing conflict in %s "
  193. "[DMAstat:%d][irqlock:%d].\n",
  194. __func__,
  195. ei_local->dmaing, ei_local->irqlock);
  196. return;
  197. }
  198. ei_local->dmaing |= 0x01;
  199. ei_outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base + NE_CMD);
  200. ei_outb(count & 0xff, nic_base + EN0_RCNTLO);
  201. ei_outb(count >> 8, nic_base + EN0_RCNTHI);
  202. ei_outb(ring_offset & 0xff, nic_base + EN0_RSARLO);
  203. ei_outb(ring_offset >> 8, nic_base + EN0_RSARHI);
  204. ei_outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
  205. if (ei_local->word16) {
  206. ioread16_rep(nic_base + NE_DATAPORT, buf, count >> 1);
  207. if (count & 0x01)
  208. buf[count-1] = ei_inb(nic_base + NE_DATAPORT);
  209. } else {
  210. ioread8_rep(nic_base + NE_DATAPORT, buf, count);
  211. }
  212. ei_local->dmaing &= ~1;
  213. }
  214. static void ax_block_output(struct net_device *dev, int count,
  215. const unsigned char *buf, const int start_page)
  216. {
  217. struct ei_device *ei_local = netdev_priv(dev);
  218. void __iomem *nic_base = ei_local->mem;
  219. unsigned long dma_start;
  220. /*
  221. * Round the count up for word writes. Do we need to do this?
  222. * What effect will an odd byte count have on the 8390? I
  223. * should check someday.
  224. */
  225. if (ei_local->word16 && (count & 0x01))
  226. count++;
  227. /* This *shouldn't* happen. If it does, it's the last thing you'll see */
  228. if (ei_local->dmaing) {
  229. netdev_err(dev, "DMAing conflict in %s."
  230. "[DMAstat:%d][irqlock:%d]\n",
  231. __func__,
  232. ei_local->dmaing, ei_local->irqlock);
  233. return;
  234. }
  235. ei_local->dmaing |= 0x01;
  236. /* We should already be in page 0, but to be safe... */
  237. ei_outb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
  238. ei_outb(ENISR_RDC, nic_base + EN0_ISR);
  239. /* Now the normal output. */
  240. ei_outb(count & 0xff, nic_base + EN0_RCNTLO);
  241. ei_outb(count >> 8, nic_base + EN0_RCNTHI);
  242. ei_outb(0x00, nic_base + EN0_RSARLO);
  243. ei_outb(start_page, nic_base + EN0_RSARHI);
  244. ei_outb(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
  245. if (ei_local->word16)
  246. iowrite16_rep(nic_base + NE_DATAPORT, buf, count >> 1);
  247. else
  248. iowrite8_rep(nic_base + NE_DATAPORT, buf, count);
  249. dma_start = jiffies;
  250. while ((ei_inb(nic_base + EN0_ISR) & ENISR_RDC) == 0) {
  251. if (time_after(jiffies, dma_start + 2 * HZ / 100)) { /* 20ms */
  252. netdev_warn(dev, "timeout waiting for Tx RDC.\n");
  253. ax_reset_8390(dev);
  254. ax_NS8390_init(dev, 1);
  255. break;
  256. }
  257. }
  258. ei_outb(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
  259. ei_local->dmaing &= ~0x01;
  260. }
  261. /* definitions for accessing MII/EEPROM interface */
  262. #define AX_MEMR EI_SHIFT(0x14)
  263. #define AX_MEMR_MDC BIT(0)
  264. #define AX_MEMR_MDIR BIT(1)
  265. #define AX_MEMR_MDI BIT(2)
  266. #define AX_MEMR_MDO BIT(3)
  267. #define AX_MEMR_EECS BIT(4)
  268. #define AX_MEMR_EEI BIT(5)
  269. #define AX_MEMR_EEO BIT(6)
  270. #define AX_MEMR_EECLK BIT(7)
  271. static void ax_handle_link_change(struct net_device *dev)
  272. {
  273. struct ax_device *ax = to_ax_dev(dev);
  274. struct phy_device *phy_dev = dev->phydev;
  275. int status_change = 0;
  276. if (phy_dev->link && ((ax->speed != phy_dev->speed) ||
  277. (ax->duplex != phy_dev->duplex))) {
  278. ax->speed = phy_dev->speed;
  279. ax->duplex = phy_dev->duplex;
  280. status_change = 1;
  281. }
  282. if (phy_dev->link != ax->link) {
  283. if (!phy_dev->link) {
  284. ax->speed = 0;
  285. ax->duplex = -1;
  286. }
  287. ax->link = phy_dev->link;
  288. status_change = 1;
  289. }
  290. if (status_change)
  291. phy_print_status(phy_dev);
  292. }
  293. static int ax_mii_probe(struct net_device *dev)
  294. {
  295. struct ax_device *ax = to_ax_dev(dev);
  296. struct phy_device *phy_dev = NULL;
  297. int ret;
  298. /* find the first phy */
  299. phy_dev = phy_find_first(ax->mii_bus);
  300. if (!phy_dev) {
  301. netdev_err(dev, "no PHY found\n");
  302. return -ENODEV;
  303. }
  304. ret = phy_connect_direct(dev, phy_dev, ax_handle_link_change,
  305. PHY_INTERFACE_MODE_MII);
  306. if (ret) {
  307. netdev_err(dev, "Could not attach to PHY\n");
  308. return ret;
  309. }
  310. /* mask with MAC supported features */
  311. phy_dev->supported &= PHY_BASIC_FEATURES;
  312. phy_dev->advertising = phy_dev->supported;
  313. netdev_info(dev, "PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
  314. phy_dev->drv->name, phydev_name(phy_dev), phy_dev->irq);
  315. return 0;
  316. }
  317. static void ax_phy_switch(struct net_device *dev, int on)
  318. {
  319. struct ei_device *ei_local = netdev_priv(dev);
  320. struct ax_device *ax = to_ax_dev(dev);
  321. u8 reg_gpoc = ax->plat->gpoc_val;
  322. if (!!on)
  323. reg_gpoc &= ~AX_GPOC_PPDSET;
  324. else
  325. reg_gpoc |= AX_GPOC_PPDSET;
  326. ei_outb(reg_gpoc, ei_local->mem + EI_SHIFT(0x17));
  327. }
  328. static void ax_bb_mdc(struct mdiobb_ctrl *ctrl, int level)
  329. {
  330. struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
  331. if (level)
  332. ax->reg_memr |= AX_MEMR_MDC;
  333. else
  334. ax->reg_memr &= ~AX_MEMR_MDC;
  335. ei_outb(ax->reg_memr, ax->addr_memr);
  336. }
  337. static void ax_bb_dir(struct mdiobb_ctrl *ctrl, int output)
  338. {
  339. struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
  340. if (output)
  341. ax->reg_memr &= ~AX_MEMR_MDIR;
  342. else
  343. ax->reg_memr |= AX_MEMR_MDIR;
  344. ei_outb(ax->reg_memr, ax->addr_memr);
  345. }
  346. static void ax_bb_set_data(struct mdiobb_ctrl *ctrl, int value)
  347. {
  348. struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
  349. if (value)
  350. ax->reg_memr |= AX_MEMR_MDO;
  351. else
  352. ax->reg_memr &= ~AX_MEMR_MDO;
  353. ei_outb(ax->reg_memr, ax->addr_memr);
  354. }
  355. static int ax_bb_get_data(struct mdiobb_ctrl *ctrl)
  356. {
  357. struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
  358. int reg_memr = ei_inb(ax->addr_memr);
  359. return reg_memr & AX_MEMR_MDI ? 1 : 0;
  360. }
  361. static const struct mdiobb_ops bb_ops = {
  362. .owner = THIS_MODULE,
  363. .set_mdc = ax_bb_mdc,
  364. .set_mdio_dir = ax_bb_dir,
  365. .set_mdio_data = ax_bb_set_data,
  366. .get_mdio_data = ax_bb_get_data,
  367. };
  368. static int ax_mii_init(struct net_device *dev)
  369. {
  370. struct platform_device *pdev = to_platform_device(dev->dev.parent);
  371. struct ei_device *ei_local = netdev_priv(dev);
  372. struct ax_device *ax = to_ax_dev(dev);
  373. int err;
  374. ax->bb_ctrl.ops = &bb_ops;
  375. ax->addr_memr = ei_local->mem + AX_MEMR;
  376. ax->mii_bus = alloc_mdio_bitbang(&ax->bb_ctrl);
  377. if (!ax->mii_bus) {
  378. err = -ENOMEM;
  379. goto out;
  380. }
  381. ax->mii_bus->name = "ax88796_mii_bus";
  382. ax->mii_bus->parent = dev->dev.parent;
  383. snprintf(ax->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  384. pdev->name, pdev->id);
  385. err = mdiobus_register(ax->mii_bus);
  386. if (err)
  387. goto out_free_mdio_bitbang;
  388. return 0;
  389. out_free_mdio_bitbang:
  390. free_mdio_bitbang(ax->mii_bus);
  391. out:
  392. return err;
  393. }
  394. static int ax_open(struct net_device *dev)
  395. {
  396. struct ax_device *ax = to_ax_dev(dev);
  397. int ret;
  398. netdev_dbg(dev, "open\n");
  399. ret = ax_mii_init(dev);
  400. if (ret)
  401. goto failed_mii;
  402. if (ax->plat->check_irq)
  403. ret = request_irq(dev->irq, ax_ei_interrupt_filtered,
  404. ax->irqflags, dev->name, dev);
  405. else
  406. ret = request_irq(dev->irq, ax_ei_interrupt, ax->irqflags,
  407. dev->name, dev);
  408. if (ret)
  409. goto failed_request_irq;
  410. /* turn the phy on (if turned off) */
  411. ax_phy_switch(dev, 1);
  412. ret = ax_mii_probe(dev);
  413. if (ret)
  414. goto failed_mii_probe;
  415. phy_start(dev->phydev);
  416. ret = ax_ei_open(dev);
  417. if (ret)
  418. goto failed_ax_ei_open;
  419. ax->running = 1;
  420. return 0;
  421. failed_ax_ei_open:
  422. phy_disconnect(dev->phydev);
  423. failed_mii_probe:
  424. ax_phy_switch(dev, 0);
  425. free_irq(dev->irq, dev);
  426. failed_request_irq:
  427. /* unregister mdiobus */
  428. mdiobus_unregister(ax->mii_bus);
  429. free_mdio_bitbang(ax->mii_bus);
  430. failed_mii:
  431. return ret;
  432. }
  433. static int ax_close(struct net_device *dev)
  434. {
  435. struct ax_device *ax = to_ax_dev(dev);
  436. netdev_dbg(dev, "close\n");
  437. ax->running = 0;
  438. wmb();
  439. ax_ei_close(dev);
  440. /* turn the phy off */
  441. ax_phy_switch(dev, 0);
  442. phy_disconnect(dev->phydev);
  443. free_irq(dev->irq, dev);
  444. mdiobus_unregister(ax->mii_bus);
  445. free_mdio_bitbang(ax->mii_bus);
  446. return 0;
  447. }
  448. static int ax_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
  449. {
  450. struct phy_device *phy_dev = dev->phydev;
  451. if (!netif_running(dev))
  452. return -EINVAL;
  453. if (!phy_dev)
  454. return -ENODEV;
  455. return phy_mii_ioctl(phy_dev, req, cmd);
  456. }
  457. /* ethtool ops */
  458. static void ax_get_drvinfo(struct net_device *dev,
  459. struct ethtool_drvinfo *info)
  460. {
  461. struct platform_device *pdev = to_platform_device(dev->dev.parent);
  462. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  463. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  464. strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
  465. }
  466. static u32 ax_get_msglevel(struct net_device *dev)
  467. {
  468. struct ei_device *ei_local = netdev_priv(dev);
  469. return ei_local->msg_enable;
  470. }
  471. static void ax_set_msglevel(struct net_device *dev, u32 v)
  472. {
  473. struct ei_device *ei_local = netdev_priv(dev);
  474. ei_local->msg_enable = v;
  475. }
  476. static const struct ethtool_ops ax_ethtool_ops = {
  477. .get_drvinfo = ax_get_drvinfo,
  478. .get_link = ethtool_op_get_link,
  479. .get_ts_info = ethtool_op_get_ts_info,
  480. .get_msglevel = ax_get_msglevel,
  481. .set_msglevel = ax_set_msglevel,
  482. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  483. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  484. };
  485. #ifdef CONFIG_AX88796_93CX6
  486. static void ax_eeprom_register_read(struct eeprom_93cx6 *eeprom)
  487. {
  488. struct ei_device *ei_local = eeprom->data;
  489. u8 reg = ei_inb(ei_local->mem + AX_MEMR);
  490. eeprom->reg_data_in = reg & AX_MEMR_EEI;
  491. eeprom->reg_data_out = reg & AX_MEMR_EEO; /* Input pin */
  492. eeprom->reg_data_clock = reg & AX_MEMR_EECLK;
  493. eeprom->reg_chip_select = reg & AX_MEMR_EECS;
  494. }
  495. static void ax_eeprom_register_write(struct eeprom_93cx6 *eeprom)
  496. {
  497. struct ei_device *ei_local = eeprom->data;
  498. u8 reg = ei_inb(ei_local->mem + AX_MEMR);
  499. reg &= ~(AX_MEMR_EEI | AX_MEMR_EECLK | AX_MEMR_EECS);
  500. if (eeprom->reg_data_in)
  501. reg |= AX_MEMR_EEI;
  502. if (eeprom->reg_data_clock)
  503. reg |= AX_MEMR_EECLK;
  504. if (eeprom->reg_chip_select)
  505. reg |= AX_MEMR_EECS;
  506. ei_outb(reg, ei_local->mem + AX_MEMR);
  507. udelay(10);
  508. }
  509. #endif
  510. static const struct net_device_ops ax_netdev_ops = {
  511. .ndo_open = ax_open,
  512. .ndo_stop = ax_close,
  513. .ndo_do_ioctl = ax_ioctl,
  514. .ndo_start_xmit = ax_ei_start_xmit,
  515. .ndo_tx_timeout = ax_ei_tx_timeout,
  516. .ndo_get_stats = ax_ei_get_stats,
  517. .ndo_set_rx_mode = ax_ei_set_multicast_list,
  518. .ndo_validate_addr = eth_validate_addr,
  519. .ndo_set_mac_address = eth_mac_addr,
  520. #ifdef CONFIG_NET_POLL_CONTROLLER
  521. .ndo_poll_controller = ax_ei_poll,
  522. #endif
  523. };
  524. /* setup code */
  525. static void ax_initial_setup(struct net_device *dev, struct ei_device *ei_local)
  526. {
  527. void __iomem *ioaddr = ei_local->mem;
  528. struct ax_device *ax = to_ax_dev(dev);
  529. /* Select page 0 */
  530. ei_outb(E8390_NODMA + E8390_PAGE0 + E8390_STOP, ioaddr + E8390_CMD);
  531. /* set to byte access */
  532. ei_outb(ax->plat->dcr_val & ~1, ioaddr + EN0_DCFG);
  533. ei_outb(ax->plat->gpoc_val, ioaddr + EI_SHIFT(0x17));
  534. }
  535. /*
  536. * ax_init_dev
  537. *
  538. * initialise the specified device, taking care to note the MAC
  539. * address it may already have (if configured), ensure
  540. * the device is ready to be used by lib8390.c and registerd with
  541. * the network layer.
  542. */
  543. static int ax_init_dev(struct net_device *dev)
  544. {
  545. struct ei_device *ei_local = netdev_priv(dev);
  546. struct ax_device *ax = to_ax_dev(dev);
  547. void __iomem *ioaddr = ei_local->mem;
  548. unsigned int start_page;
  549. unsigned int stop_page;
  550. int ret;
  551. int i;
  552. ret = ax_initial_check(dev);
  553. if (ret)
  554. goto err_out;
  555. /* setup goes here */
  556. ax_initial_setup(dev, ei_local);
  557. /* read the mac from the card prom if we need it */
  558. if (ax->plat->flags & AXFLG_HAS_EEPROM) {
  559. unsigned char SA_prom[32];
  560. ei_outb(6, ioaddr + EN0_RCNTLO);
  561. ei_outb(0, ioaddr + EN0_RCNTHI);
  562. ei_outb(0, ioaddr + EN0_RSARLO);
  563. ei_outb(0, ioaddr + EN0_RSARHI);
  564. ei_outb(E8390_RREAD + E8390_START, ioaddr + NE_CMD);
  565. for (i = 0; i < sizeof(SA_prom); i += 2) {
  566. SA_prom[i] = ei_inb(ioaddr + NE_DATAPORT);
  567. SA_prom[i + 1] = ei_inb(ioaddr + NE_DATAPORT);
  568. }
  569. ei_outb(ENISR_RDC, ioaddr + EN0_ISR); /* Ack intr. */
  570. if (ax->plat->wordlength == 2)
  571. for (i = 0; i < 16; i++)
  572. SA_prom[i] = SA_prom[i+i];
  573. memcpy(dev->dev_addr, SA_prom, ETH_ALEN);
  574. }
  575. #ifdef CONFIG_AX88796_93CX6
  576. if (ax->plat->flags & AXFLG_HAS_93CX6) {
  577. unsigned char mac_addr[ETH_ALEN];
  578. struct eeprom_93cx6 eeprom;
  579. eeprom.data = ei_local;
  580. eeprom.register_read = ax_eeprom_register_read;
  581. eeprom.register_write = ax_eeprom_register_write;
  582. eeprom.width = PCI_EEPROM_WIDTH_93C56;
  583. eeprom_93cx6_multiread(&eeprom, 0,
  584. (__le16 __force *)mac_addr,
  585. sizeof(mac_addr) >> 1);
  586. memcpy(dev->dev_addr, mac_addr, ETH_ALEN);
  587. }
  588. #endif
  589. if (ax->plat->wordlength == 2) {
  590. /* We must set the 8390 for word mode. */
  591. ei_outb(ax->plat->dcr_val, ei_local->mem + EN0_DCFG);
  592. start_page = NESM_START_PG;
  593. stop_page = NESM_STOP_PG;
  594. } else {
  595. start_page = NE1SM_START_PG;
  596. stop_page = NE1SM_STOP_PG;
  597. }
  598. /* load the mac-address from the device */
  599. if (ax->plat->flags & AXFLG_MAC_FROMDEV) {
  600. ei_outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP,
  601. ei_local->mem + E8390_CMD); /* 0x61 */
  602. for (i = 0; i < ETH_ALEN; i++)
  603. dev->dev_addr[i] =
  604. ei_inb(ioaddr + EN1_PHYS_SHIFT(i));
  605. }
  606. if ((ax->plat->flags & AXFLG_MAC_FROMPLATFORM) &&
  607. ax->plat->mac_addr)
  608. memcpy(dev->dev_addr, ax->plat->mac_addr, ETH_ALEN);
  609. if (!is_valid_ether_addr(dev->dev_addr)) {
  610. eth_hw_addr_random(dev);
  611. dev_info(&dev->dev, "Using random MAC address: %pM\n",
  612. dev->dev_addr);
  613. }
  614. ax_reset_8390(dev);
  615. ei_local->name = "AX88796";
  616. ei_local->tx_start_page = start_page;
  617. ei_local->stop_page = stop_page;
  618. ei_local->word16 = (ax->plat->wordlength == 2);
  619. ei_local->rx_start_page = start_page + TX_PAGES;
  620. #ifdef PACKETBUF_MEMSIZE
  621. /* Allow the packet buffer size to be overridden by know-it-alls. */
  622. ei_local->stop_page = ei_local->tx_start_page + PACKETBUF_MEMSIZE;
  623. #endif
  624. ei_local->reset_8390 = &ax_reset_8390;
  625. if (ax->plat->block_input)
  626. ei_local->block_input = ax->plat->block_input;
  627. else
  628. ei_local->block_input = &ax_block_input;
  629. if (ax->plat->block_output)
  630. ei_local->block_output = ax->plat->block_output;
  631. else
  632. ei_local->block_output = &ax_block_output;
  633. ei_local->get_8390_hdr = &ax_get_8390_hdr;
  634. ei_local->priv = 0;
  635. dev->netdev_ops = &ax_netdev_ops;
  636. dev->ethtool_ops = &ax_ethtool_ops;
  637. ax_NS8390_init(dev, 0);
  638. ret = register_netdev(dev);
  639. if (ret)
  640. goto err_out;
  641. netdev_info(dev, "%dbit, irq %d, %lx, MAC: %pM\n",
  642. ei_local->word16 ? 16 : 8, dev->irq, dev->base_addr,
  643. dev->dev_addr);
  644. return 0;
  645. err_out:
  646. return ret;
  647. }
  648. static int ax_remove(struct platform_device *pdev)
  649. {
  650. struct net_device *dev = platform_get_drvdata(pdev);
  651. struct ei_device *ei_local = netdev_priv(dev);
  652. struct ax_device *ax = to_ax_dev(dev);
  653. struct resource *mem;
  654. unregister_netdev(dev);
  655. iounmap(ei_local->mem);
  656. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  657. release_mem_region(mem->start, resource_size(mem));
  658. if (ax->map2) {
  659. iounmap(ax->map2);
  660. mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  661. release_mem_region(mem->start, resource_size(mem));
  662. }
  663. platform_set_drvdata(pdev, NULL);
  664. free_netdev(dev);
  665. return 0;
  666. }
  667. /*
  668. * ax_probe
  669. *
  670. * This is the entry point when the platform device system uses to
  671. * notify us of a new device to attach to. Allocate memory, find the
  672. * resources and information passed, and map the necessary registers.
  673. */
  674. static int ax_probe(struct platform_device *pdev)
  675. {
  676. struct net_device *dev;
  677. struct ei_device *ei_local;
  678. struct ax_device *ax;
  679. struct resource *irq, *mem, *mem2;
  680. unsigned long mem_size, mem2_size = 0;
  681. int ret = 0;
  682. dev = ax__alloc_ei_netdev(sizeof(struct ax_device));
  683. if (dev == NULL)
  684. return -ENOMEM;
  685. /* ok, let's setup our device */
  686. SET_NETDEV_DEV(dev, &pdev->dev);
  687. ei_local = netdev_priv(dev);
  688. ax = to_ax_dev(dev);
  689. ax->plat = dev_get_platdata(&pdev->dev);
  690. platform_set_drvdata(pdev, dev);
  691. ei_local->rxcr_base = ax->plat->rcr_val;
  692. /* find the platform resources */
  693. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  694. if (!irq) {
  695. dev_err(&pdev->dev, "no IRQ specified\n");
  696. ret = -ENXIO;
  697. goto exit_mem;
  698. }
  699. dev->irq = irq->start;
  700. ax->irqflags = irq->flags & IRQF_TRIGGER_MASK;
  701. if (irq->flags & IORESOURCE_IRQ_SHAREABLE)
  702. ax->irqflags |= IRQF_SHARED;
  703. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  704. if (!mem) {
  705. dev_err(&pdev->dev, "no MEM specified\n");
  706. ret = -ENXIO;
  707. goto exit_mem;
  708. }
  709. mem_size = resource_size(mem);
  710. /*
  711. * setup the register offsets from either the platform data or
  712. * by using the size of the resource provided
  713. */
  714. if (ax->plat->reg_offsets)
  715. ei_local->reg_offset = ax->plat->reg_offsets;
  716. else {
  717. ei_local->reg_offset = ax->reg_offsets;
  718. for (ret = 0; ret < 0x18; ret++)
  719. ax->reg_offsets[ret] = (mem_size / 0x18) * ret;
  720. }
  721. if (!request_mem_region(mem->start, mem_size, pdev->name)) {
  722. dev_err(&pdev->dev, "cannot reserve registers\n");
  723. ret = -ENXIO;
  724. goto exit_mem;
  725. }
  726. ei_local->mem = ioremap(mem->start, mem_size);
  727. dev->base_addr = (unsigned long)ei_local->mem;
  728. if (ei_local->mem == NULL) {
  729. dev_err(&pdev->dev, "Cannot ioremap area %pR\n", mem);
  730. ret = -ENXIO;
  731. goto exit_req;
  732. }
  733. /* look for reset area */
  734. mem2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  735. if (!mem2) {
  736. if (!ax->plat->reg_offsets) {
  737. for (ret = 0; ret < 0x20; ret++)
  738. ax->reg_offsets[ret] = (mem_size / 0x20) * ret;
  739. }
  740. } else {
  741. mem2_size = resource_size(mem2);
  742. if (!request_mem_region(mem2->start, mem2_size, pdev->name)) {
  743. dev_err(&pdev->dev, "cannot reserve registers\n");
  744. ret = -ENXIO;
  745. goto exit_mem1;
  746. }
  747. ax->map2 = ioremap(mem2->start, mem2_size);
  748. if (!ax->map2) {
  749. dev_err(&pdev->dev, "cannot map reset register\n");
  750. ret = -ENXIO;
  751. goto exit_mem2;
  752. }
  753. ei_local->reg_offset[0x1f] = ax->map2 - ei_local->mem;
  754. }
  755. /* got resources, now initialise and register device */
  756. ret = ax_init_dev(dev);
  757. if (!ret)
  758. return 0;
  759. if (!ax->map2)
  760. goto exit_mem1;
  761. iounmap(ax->map2);
  762. exit_mem2:
  763. if (mem2)
  764. release_mem_region(mem2->start, mem2_size);
  765. exit_mem1:
  766. iounmap(ei_local->mem);
  767. exit_req:
  768. release_mem_region(mem->start, mem_size);
  769. exit_mem:
  770. platform_set_drvdata(pdev, NULL);
  771. free_netdev(dev);
  772. return ret;
  773. }
  774. /* suspend and resume */
  775. #ifdef CONFIG_PM
  776. static int ax_suspend(struct platform_device *dev, pm_message_t state)
  777. {
  778. struct net_device *ndev = platform_get_drvdata(dev);
  779. struct ax_device *ax = to_ax_dev(ndev);
  780. ax->resume_open = ax->running;
  781. netif_device_detach(ndev);
  782. ax_close(ndev);
  783. return 0;
  784. }
  785. static int ax_resume(struct platform_device *pdev)
  786. {
  787. struct net_device *ndev = platform_get_drvdata(pdev);
  788. struct ax_device *ax = to_ax_dev(ndev);
  789. ax_initial_setup(ndev, netdev_priv(ndev));
  790. ax_NS8390_init(ndev, ax->resume_open);
  791. netif_device_attach(ndev);
  792. if (ax->resume_open)
  793. ax_open(ndev);
  794. return 0;
  795. }
  796. #else
  797. #define ax_suspend NULL
  798. #define ax_resume NULL
  799. #endif
  800. static struct platform_driver axdrv = {
  801. .driver = {
  802. .name = "ax88796",
  803. },
  804. .probe = ax_probe,
  805. .remove = ax_remove,
  806. .suspend = ax_suspend,
  807. .resume = ax_resume,
  808. };
  809. module_platform_driver(axdrv);
  810. MODULE_DESCRIPTION("AX88796 10/100 Ethernet platform driver");
  811. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  812. MODULE_LICENSE("GPL v2");
  813. MODULE_ALIAS("platform:ax88796");