ax88796.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* drivers/net/ethernet/8390/ax88796.c
  3. *
  4. * Copyright 2005,2007 Simtec Electronics
  5. * Ben Dooks <[email protected]>
  6. *
  7. * Asix AX88796 10/100 Ethernet controller support
  8. * Based on ne.c, by Donald Becker, et-al.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/errno.h>
  13. #include <linux/isapnp.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/io.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/delay.h>
  18. #include <linux/timer.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/etherdevice.h>
  21. #include <linux/ethtool.h>
  22. #include <linux/mdio-bitbang.h>
  23. #include <linux/phy.h>
  24. #include <linux/eeprom_93cx6.h>
  25. #include <linux/slab.h>
  26. #include <net/ax88796.h>
  27. /* Rename the lib8390.c functions to show that they are in this driver */
  28. #define __ei_open ax_ei_open
  29. #define __ei_close ax_ei_close
  30. #define __ei_poll ax_ei_poll
  31. #define __ei_start_xmit ax_ei_start_xmit
  32. #define __ei_tx_timeout ax_ei_tx_timeout
  33. #define __ei_get_stats ax_ei_get_stats
  34. #define __ei_set_multicast_list ax_ei_set_multicast_list
  35. #define __ei_interrupt ax_ei_interrupt
  36. #define ____alloc_ei_netdev ax__alloc_ei_netdev
  37. #define __NS8390_init ax_NS8390_init
  38. /* force unsigned long back to 'void __iomem *' */
  39. #define ax_convert_addr(_a) ((void __force __iomem *)(_a))
  40. #define ei_inb(_a) readb(ax_convert_addr(_a))
  41. #define ei_outb(_v, _a) writeb(_v, ax_convert_addr(_a))
  42. #define ei_inb_p(_a) ei_inb(_a)
  43. #define ei_outb_p(_v, _a) ei_outb(_v, _a)
  44. /* define EI_SHIFT() to take into account our register offsets */
  45. #define EI_SHIFT(x) (ei_local->reg_offset[(x)])
  46. /* Ensure we have our RCR base value */
  47. #define AX88796_PLATFORM
  48. static unsigned char version[] = "ax88796.c: Copyright 2005,2007 Simtec Electronics\n";
  49. #include "lib8390.c"
  50. #define DRV_NAME "ax88796"
  51. #define DRV_VERSION "1.00"
  52. /* from ne.c */
  53. #define NE_CMD EI_SHIFT(0x00)
  54. #define NE_RESET EI_SHIFT(0x1f)
  55. #define NE_DATAPORT EI_SHIFT(0x10)
  56. #define NE1SM_START_PG 0x20 /* First page of TX buffer */
  57. #define NE1SM_STOP_PG 0x40 /* Last page +1 of RX ring */
  58. #define NESM_START_PG 0x40 /* First page of TX buffer */
  59. #define NESM_STOP_PG 0x80 /* Last page +1 of RX ring */
  60. #define AX_GPOC_PPDSET BIT(6)
  61. /* device private data */
  62. struct ax_device {
  63. struct mii_bus *mii_bus;
  64. struct mdiobb_ctrl bb_ctrl;
  65. void __iomem *addr_memr;
  66. u8 reg_memr;
  67. int link;
  68. int speed;
  69. int duplex;
  70. void __iomem *map2;
  71. const struct ax_plat_data *plat;
  72. unsigned char running;
  73. unsigned char resume_open;
  74. unsigned int irqflags;
  75. u32 reg_offsets[0x20];
  76. };
  77. static inline struct ax_device *to_ax_dev(struct net_device *dev)
  78. {
  79. struct ei_device *ei_local = netdev_priv(dev);
  80. return (struct ax_device *)(ei_local + 1);
  81. }
  82. void ax_NS8390_reinit(struct net_device *dev)
  83. {
  84. ax_NS8390_init(dev, 1);
  85. }
  86. EXPORT_SYMBOL_GPL(ax_NS8390_reinit);
  87. /*
  88. * ax_initial_check
  89. *
  90. * do an initial probe for the card to check whether it exists
  91. * and is functional
  92. */
  93. static int ax_initial_check(struct net_device *dev)
  94. {
  95. struct ei_device *ei_local = netdev_priv(dev);
  96. void __iomem *ioaddr = ei_local->mem;
  97. int reg0;
  98. int regd;
  99. reg0 = ei_inb(ioaddr);
  100. if (reg0 == 0xFF)
  101. return -ENODEV;
  102. ei_outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP, ioaddr + E8390_CMD);
  103. regd = ei_inb(ioaddr + 0x0d);
  104. ei_outb(0xff, ioaddr + 0x0d);
  105. ei_outb(E8390_NODMA + E8390_PAGE0, ioaddr + E8390_CMD);
  106. ei_inb(ioaddr + EN0_COUNTER0); /* Clear the counter by reading. */
  107. if (ei_inb(ioaddr + EN0_COUNTER0) != 0) {
  108. ei_outb(reg0, ioaddr);
  109. ei_outb(regd, ioaddr + 0x0d); /* Restore the old values. */
  110. return -ENODEV;
  111. }
  112. return 0;
  113. }
  114. /*
  115. * Hard reset the card. This used to pause for the same period that a
  116. * 8390 reset command required, but that shouldn't be necessary.
  117. */
  118. static void ax_reset_8390(struct net_device *dev)
  119. {
  120. struct ei_device *ei_local = netdev_priv(dev);
  121. unsigned long reset_start_time = jiffies;
  122. void __iomem *addr = (void __iomem *)dev->base_addr;
  123. netif_dbg(ei_local, hw, dev, "resetting the 8390 t=%ld...\n", jiffies);
  124. ei_outb(ei_inb(addr + NE_RESET), addr + NE_RESET);
  125. ei_local->txing = 0;
  126. ei_local->dmaing = 0;
  127. /* This check _should_not_ be necessary, omit eventually. */
  128. while ((ei_inb(addr + EN0_ISR) & ENISR_RESET) == 0) {
  129. if (time_after(jiffies, reset_start_time + 2 * HZ / 100)) {
  130. netdev_warn(dev, "%s: did not complete.\n", __func__);
  131. break;
  132. }
  133. }
  134. ei_outb(ENISR_RESET, addr + EN0_ISR); /* Ack intr. */
  135. }
  136. /* Wrapper for __ei_interrupt for platforms that have a platform-specific
  137. * way to find out whether the interrupt request might be caused by
  138. * the ax88796 chip.
  139. */
  140. static irqreturn_t ax_ei_interrupt_filtered(int irq, void *dev_id)
  141. {
  142. struct net_device *dev = dev_id;
  143. struct ax_device *ax = to_ax_dev(dev);
  144. struct platform_device *pdev = to_platform_device(dev->dev.parent);
  145. if (!ax->plat->check_irq(pdev))
  146. return IRQ_NONE;
  147. return ax_ei_interrupt(irq, dev_id);
  148. }
  149. static void ax_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
  150. int ring_page)
  151. {
  152. struct ei_device *ei_local = netdev_priv(dev);
  153. void __iomem *nic_base = ei_local->mem;
  154. /* This *shouldn't* happen. If it does, it's the last thing you'll see */
  155. if (ei_local->dmaing) {
  156. netdev_err(dev, "DMAing conflict in %s "
  157. "[DMAstat:%d][irqlock:%d].\n",
  158. __func__,
  159. ei_local->dmaing, ei_local->irqlock);
  160. return;
  161. }
  162. ei_local->dmaing |= 0x01;
  163. ei_outb(E8390_NODMA + E8390_PAGE0 + E8390_START, nic_base + NE_CMD);
  164. ei_outb(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
  165. ei_outb(0, nic_base + EN0_RCNTHI);
  166. ei_outb(0, nic_base + EN0_RSARLO); /* On page boundary */
  167. ei_outb(ring_page, nic_base + EN0_RSARHI);
  168. ei_outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
  169. if (ei_local->word16)
  170. ioread16_rep(nic_base + NE_DATAPORT, hdr,
  171. sizeof(struct e8390_pkt_hdr) >> 1);
  172. else
  173. ioread8_rep(nic_base + NE_DATAPORT, hdr,
  174. sizeof(struct e8390_pkt_hdr));
  175. ei_outb(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
  176. ei_local->dmaing &= ~0x01;
  177. le16_to_cpus(&hdr->count);
  178. }
  179. /*
  180. * Block input and output, similar to the Crynwr packet driver. If
  181. * you are porting to a new ethercard, look at the packet driver
  182. * source for hints. The NEx000 doesn't share the on-board packet
  183. * memory -- you have to put the packet out through the "remote DMA"
  184. * dataport using ei_outb.
  185. */
  186. static void ax_block_input(struct net_device *dev, int count,
  187. struct sk_buff *skb, int ring_offset)
  188. {
  189. struct ei_device *ei_local = netdev_priv(dev);
  190. void __iomem *nic_base = ei_local->mem;
  191. char *buf = skb->data;
  192. if (ei_local->dmaing) {
  193. netdev_err(dev,
  194. "DMAing conflict in %s "
  195. "[DMAstat:%d][irqlock:%d].\n",
  196. __func__,
  197. ei_local->dmaing, ei_local->irqlock);
  198. return;
  199. }
  200. ei_local->dmaing |= 0x01;
  201. ei_outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base + NE_CMD);
  202. ei_outb(count & 0xff, nic_base + EN0_RCNTLO);
  203. ei_outb(count >> 8, nic_base + EN0_RCNTHI);
  204. ei_outb(ring_offset & 0xff, nic_base + EN0_RSARLO);
  205. ei_outb(ring_offset >> 8, nic_base + EN0_RSARHI);
  206. ei_outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
  207. if (ei_local->word16) {
  208. ioread16_rep(nic_base + NE_DATAPORT, buf, count >> 1);
  209. if (count & 0x01)
  210. buf[count-1] = ei_inb(nic_base + NE_DATAPORT);
  211. } else {
  212. ioread8_rep(nic_base + NE_DATAPORT, buf, count);
  213. }
  214. ei_local->dmaing &= ~1;
  215. }
  216. static void ax_block_output(struct net_device *dev, int count,
  217. const unsigned char *buf, const int start_page)
  218. {
  219. struct ei_device *ei_local = netdev_priv(dev);
  220. void __iomem *nic_base = ei_local->mem;
  221. unsigned long dma_start;
  222. /*
  223. * Round the count up for word writes. Do we need to do this?
  224. * What effect will an odd byte count have on the 8390? I
  225. * should check someday.
  226. */
  227. if (ei_local->word16 && (count & 0x01))
  228. count++;
  229. /* This *shouldn't* happen. If it does, it's the last thing you'll see */
  230. if (ei_local->dmaing) {
  231. netdev_err(dev, "DMAing conflict in %s."
  232. "[DMAstat:%d][irqlock:%d]\n",
  233. __func__,
  234. ei_local->dmaing, ei_local->irqlock);
  235. return;
  236. }
  237. ei_local->dmaing |= 0x01;
  238. /* We should already be in page 0, but to be safe... */
  239. ei_outb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
  240. ei_outb(ENISR_RDC, nic_base + EN0_ISR);
  241. /* Now the normal output. */
  242. ei_outb(count & 0xff, nic_base + EN0_RCNTLO);
  243. ei_outb(count >> 8, nic_base + EN0_RCNTHI);
  244. ei_outb(0x00, nic_base + EN0_RSARLO);
  245. ei_outb(start_page, nic_base + EN0_RSARHI);
  246. ei_outb(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
  247. if (ei_local->word16)
  248. iowrite16_rep(nic_base + NE_DATAPORT, buf, count >> 1);
  249. else
  250. iowrite8_rep(nic_base + NE_DATAPORT, buf, count);
  251. dma_start = jiffies;
  252. while ((ei_inb(nic_base + EN0_ISR) & ENISR_RDC) == 0) {
  253. if (time_after(jiffies, dma_start + 2 * HZ / 100)) { /* 20ms */
  254. netdev_warn(dev, "timeout waiting for Tx RDC.\n");
  255. ax_reset_8390(dev);
  256. ax_NS8390_init(dev, 1);
  257. break;
  258. }
  259. }
  260. ei_outb(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
  261. ei_local->dmaing &= ~0x01;
  262. }
  263. /* definitions for accessing MII/EEPROM interface */
  264. #define AX_MEMR EI_SHIFT(0x14)
  265. #define AX_MEMR_MDC BIT(0)
  266. #define AX_MEMR_MDIR BIT(1)
  267. #define AX_MEMR_MDI BIT(2)
  268. #define AX_MEMR_MDO BIT(3)
  269. #define AX_MEMR_EECS BIT(4)
  270. #define AX_MEMR_EEI BIT(5)
  271. #define AX_MEMR_EEO BIT(6)
  272. #define AX_MEMR_EECLK BIT(7)
  273. static void ax_handle_link_change(struct net_device *dev)
  274. {
  275. struct ax_device *ax = to_ax_dev(dev);
  276. struct phy_device *phy_dev = dev->phydev;
  277. int status_change = 0;
  278. if (phy_dev->link && ((ax->speed != phy_dev->speed) ||
  279. (ax->duplex != phy_dev->duplex))) {
  280. ax->speed = phy_dev->speed;
  281. ax->duplex = phy_dev->duplex;
  282. status_change = 1;
  283. }
  284. if (phy_dev->link != ax->link) {
  285. if (!phy_dev->link) {
  286. ax->speed = 0;
  287. ax->duplex = -1;
  288. }
  289. ax->link = phy_dev->link;
  290. status_change = 1;
  291. }
  292. if (status_change)
  293. phy_print_status(phy_dev);
  294. }
  295. static int ax_mii_probe(struct net_device *dev)
  296. {
  297. struct ax_device *ax = to_ax_dev(dev);
  298. struct phy_device *phy_dev = NULL;
  299. int ret;
  300. /* find the first phy */
  301. phy_dev = phy_find_first(ax->mii_bus);
  302. if (!phy_dev) {
  303. netdev_err(dev, "no PHY found\n");
  304. return -ENODEV;
  305. }
  306. ret = phy_connect_direct(dev, phy_dev, ax_handle_link_change,
  307. PHY_INTERFACE_MODE_MII);
  308. if (ret) {
  309. netdev_err(dev, "Could not attach to PHY\n");
  310. return ret;
  311. }
  312. phy_set_max_speed(phy_dev, SPEED_100);
  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. strscpy(info->driver, DRV_NAME, sizeof(info->driver));
  463. strscpy(info->version, DRV_VERSION, sizeof(info->version));
  464. strscpy(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_eth_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. eth_hw_addr_set(dev, SA_prom);
  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. eth_hw_addr_set(dev, mac_addr);
  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. u8 addr[ETH_ALEN];
  601. ei_outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP,
  602. ei_local->mem + E8390_CMD); /* 0x61 */
  603. for (i = 0; i < ETH_ALEN; i++)
  604. addr[i] = ei_inb(ioaddr + EN1_PHYS_SHIFT(i));
  605. eth_hw_addr_set(dev, addr);
  606. }
  607. if ((ax->plat->flags & AXFLG_MAC_FROMPLATFORM) &&
  608. ax->plat->mac_addr)
  609. eth_hw_addr_set(dev, ax->plat->mac_addr);
  610. if (!is_valid_ether_addr(dev->dev_addr)) {
  611. eth_hw_addr_random(dev);
  612. dev_info(&dev->dev, "Using random MAC address: %pM\n",
  613. dev->dev_addr);
  614. }
  615. ax_reset_8390(dev);
  616. ei_local->name = "AX88796";
  617. ei_local->tx_start_page = start_page;
  618. ei_local->stop_page = stop_page;
  619. ei_local->word16 = (ax->plat->wordlength == 2);
  620. ei_local->rx_start_page = start_page + TX_PAGES;
  621. #ifdef PACKETBUF_MEMSIZE
  622. /* Allow the packet buffer size to be overridden by know-it-alls. */
  623. ei_local->stop_page = ei_local->tx_start_page + PACKETBUF_MEMSIZE;
  624. #endif
  625. ei_local->reset_8390 = &ax_reset_8390;
  626. if (ax->plat->block_input)
  627. ei_local->block_input = ax->plat->block_input;
  628. else
  629. ei_local->block_input = &ax_block_input;
  630. if (ax->plat->block_output)
  631. ei_local->block_output = ax->plat->block_output;
  632. else
  633. ei_local->block_output = &ax_block_output;
  634. ei_local->get_8390_hdr = &ax_get_8390_hdr;
  635. ei_local->priv = 0;
  636. dev->netdev_ops = &ax_netdev_ops;
  637. dev->ethtool_ops = &ax_ethtool_ops;
  638. ax_NS8390_init(dev, 0);
  639. ret = register_netdev(dev);
  640. if (ret)
  641. goto err_out;
  642. netdev_info(dev, "%dbit, irq %d, %lx, MAC: %pM\n",
  643. ei_local->word16 ? 16 : 8, dev->irq, dev->base_addr,
  644. dev->dev_addr);
  645. return 0;
  646. err_out:
  647. return ret;
  648. }
  649. static int ax_remove(struct platform_device *pdev)
  650. {
  651. struct net_device *dev = platform_get_drvdata(pdev);
  652. struct ei_device *ei_local = netdev_priv(dev);
  653. struct ax_device *ax = to_ax_dev(dev);
  654. struct resource *mem;
  655. unregister_netdev(dev);
  656. iounmap(ei_local->mem);
  657. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  658. release_mem_region(mem->start, resource_size(mem));
  659. if (ax->map2) {
  660. iounmap(ax->map2);
  661. mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  662. release_mem_region(mem->start, resource_size(mem));
  663. }
  664. platform_set_drvdata(pdev, NULL);
  665. free_netdev(dev);
  666. return 0;
  667. }
  668. /*
  669. * ax_probe
  670. *
  671. * This is the entry point when the platform device system uses to
  672. * notify us of a new device to attach to. Allocate memory, find the
  673. * resources and information passed, and map the necessary registers.
  674. */
  675. static int ax_probe(struct platform_device *pdev)
  676. {
  677. struct net_device *dev;
  678. struct ei_device *ei_local;
  679. struct ax_device *ax;
  680. struct resource *irq, *mem, *mem2;
  681. unsigned long mem_size, mem2_size = 0;
  682. int ret = 0;
  683. dev = ax__alloc_ei_netdev(sizeof(struct ax_device));
  684. if (dev == NULL)
  685. return -ENOMEM;
  686. /* ok, let's setup our device */
  687. SET_NETDEV_DEV(dev, &pdev->dev);
  688. ei_local = netdev_priv(dev);
  689. ax = to_ax_dev(dev);
  690. ax->plat = dev_get_platdata(&pdev->dev);
  691. platform_set_drvdata(pdev, dev);
  692. ei_local->rxcr_base = ax->plat->rcr_val;
  693. /* find the platform resources */
  694. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  695. if (!irq) {
  696. dev_err(&pdev->dev, "no IRQ specified\n");
  697. ret = -ENXIO;
  698. goto exit_mem;
  699. }
  700. dev->irq = irq->start;
  701. ax->irqflags = irq->flags & IRQF_TRIGGER_MASK;
  702. if (irq->flags & IORESOURCE_IRQ_SHAREABLE)
  703. ax->irqflags |= IRQF_SHARED;
  704. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  705. if (!mem) {
  706. dev_err(&pdev->dev, "no MEM specified\n");
  707. ret = -ENXIO;
  708. goto exit_mem;
  709. }
  710. mem_size = resource_size(mem);
  711. /*
  712. * setup the register offsets from either the platform data or
  713. * by using the size of the resource provided
  714. */
  715. if (ax->plat->reg_offsets)
  716. ei_local->reg_offset = ax->plat->reg_offsets;
  717. else {
  718. ei_local->reg_offset = ax->reg_offsets;
  719. for (ret = 0; ret < 0x18; ret++)
  720. ax->reg_offsets[ret] = (mem_size / 0x18) * ret;
  721. }
  722. if (!request_mem_region(mem->start, mem_size, pdev->name)) {
  723. dev_err(&pdev->dev, "cannot reserve registers\n");
  724. ret = -ENXIO;
  725. goto exit_mem;
  726. }
  727. ei_local->mem = ioremap(mem->start, mem_size);
  728. dev->base_addr = (unsigned long)ei_local->mem;
  729. if (ei_local->mem == NULL) {
  730. dev_err(&pdev->dev, "Cannot ioremap area %pR\n", mem);
  731. ret = -ENXIO;
  732. goto exit_req;
  733. }
  734. /* look for reset area */
  735. mem2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  736. if (!mem2) {
  737. if (!ax->plat->reg_offsets) {
  738. for (ret = 0; ret < 0x20; ret++)
  739. ax->reg_offsets[ret] = (mem_size / 0x20) * ret;
  740. }
  741. } else {
  742. mem2_size = resource_size(mem2);
  743. if (!request_mem_region(mem2->start, mem2_size, pdev->name)) {
  744. dev_err(&pdev->dev, "cannot reserve registers\n");
  745. ret = -ENXIO;
  746. goto exit_mem1;
  747. }
  748. ax->map2 = ioremap(mem2->start, mem2_size);
  749. if (!ax->map2) {
  750. dev_err(&pdev->dev, "cannot map reset register\n");
  751. ret = -ENXIO;
  752. goto exit_mem2;
  753. }
  754. ei_local->reg_offset[0x1f] = ax->map2 - ei_local->mem;
  755. }
  756. /* got resources, now initialise and register device */
  757. ret = ax_init_dev(dev);
  758. if (!ret)
  759. return 0;
  760. if (!ax->map2)
  761. goto exit_mem1;
  762. iounmap(ax->map2);
  763. exit_mem2:
  764. if (mem2)
  765. release_mem_region(mem2->start, mem2_size);
  766. exit_mem1:
  767. iounmap(ei_local->mem);
  768. exit_req:
  769. release_mem_region(mem->start, mem_size);
  770. exit_mem:
  771. platform_set_drvdata(pdev, NULL);
  772. free_netdev(dev);
  773. return ret;
  774. }
  775. /* suspend and resume */
  776. #ifdef CONFIG_PM
  777. static int ax_suspend(struct platform_device *dev, pm_message_t state)
  778. {
  779. struct net_device *ndev = platform_get_drvdata(dev);
  780. struct ax_device *ax = to_ax_dev(ndev);
  781. ax->resume_open = ax->running;
  782. netif_device_detach(ndev);
  783. ax_close(ndev);
  784. return 0;
  785. }
  786. static int ax_resume(struct platform_device *pdev)
  787. {
  788. struct net_device *ndev = platform_get_drvdata(pdev);
  789. struct ax_device *ax = to_ax_dev(ndev);
  790. ax_initial_setup(ndev, netdev_priv(ndev));
  791. ax_NS8390_init(ndev, ax->resume_open);
  792. netif_device_attach(ndev);
  793. if (ax->resume_open)
  794. ax_open(ndev);
  795. return 0;
  796. }
  797. #else
  798. #define ax_suspend NULL
  799. #define ax_resume NULL
  800. #endif
  801. static struct platform_driver axdrv = {
  802. .driver = {
  803. .name = "ax88796",
  804. },
  805. .probe = ax_probe,
  806. .remove = ax_remove,
  807. .suspend = ax_suspend,
  808. .resume = ax_resume,
  809. };
  810. module_platform_driver(axdrv);
  811. MODULE_DESCRIPTION("AX88796 10/100 Ethernet platform driver");
  812. MODULE_AUTHOR("Ben Dooks, <[email protected]>");
  813. MODULE_LICENSE("GPL v2");
  814. MODULE_ALIAS("platform:ax88796");