ax88796.c 25 KB

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