ax88796.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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 = 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 = 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 = 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 = 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. ret = -ENXIO;
  680. goto exit_mem;
  681. }
  682. dev->irq = res->start;
  683. ax->irqflags = res->flags & IRQF_TRIGGER_MASK;
  684. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  685. if (res == NULL) {
  686. dev_err(&pdev->dev, "no MEM specified\n");
  687. ret = -ENXIO;
  688. goto exit_mem;
  689. }
  690. size = (res->end - res->start) + 1;
  691. /* setup the register offsets from either the platform data
  692. * or by using the size of the resource provided */
  693. if (ax->plat->reg_offsets)
  694. ei_status.reg_offset = ax->plat->reg_offsets;
  695. else {
  696. ei_status.reg_offset = ax->reg_offsets;
  697. for (ret = 0; ret < 0x18; ret++)
  698. ax->reg_offsets[ret] = (size / 0x18) * ret;
  699. }
  700. ax->mem = request_mem_region(res->start, size, pdev->name);
  701. if (ax->mem == NULL) {
  702. dev_err(&pdev->dev, "cannot reserve registers\n");
  703. ret = -ENXIO;
  704. goto exit_mem;
  705. }
  706. ei_status.mem = ioremap(res->start, size);
  707. dev->base_addr = (unsigned long)ei_status.mem;
  708. if (ei_status.mem == NULL) {
  709. dev_err(&pdev->dev, "Cannot ioremap area (%08llx,%08llx)\n",
  710. (unsigned long long)res->start,
  711. (unsigned long long)res->end);
  712. ret = -ENXIO;
  713. goto exit_req;
  714. }
  715. /* look for reset area */
  716. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  717. if (res == NULL) {
  718. if (!ax->plat->reg_offsets) {
  719. for (ret = 0; ret < 0x20; ret++)
  720. ax->reg_offsets[ret] = (size / 0x20) * ret;
  721. }
  722. ax->map2 = NULL;
  723. } else {
  724. size = (res->end - res->start) + 1;
  725. ax->mem2 = request_mem_region(res->start, size, pdev->name);
  726. if (ax->mem2 == NULL) {
  727. dev_err(&pdev->dev, "cannot reserve registers\n");
  728. ret = -ENXIO;
  729. goto exit_mem1;
  730. }
  731. ax->map2 = ioremap(res->start, size);
  732. if (ax->map2 == NULL) {
  733. dev_err(&pdev->dev, "cannot map reset register\n");
  734. ret = -ENXIO;
  735. goto exit_mem2;
  736. }
  737. ei_status.reg_offset[0x1f] = ax->map2 - ei_status.mem;
  738. }
  739. /* got resources, now initialise and register device */
  740. ret = ax_init_dev(dev, 1);
  741. if (!ret)
  742. return 0;
  743. if (ax->map2 == NULL)
  744. goto exit_mem1;
  745. iounmap(ax->map2);
  746. exit_mem2:
  747. release_resource(ax->mem2);
  748. kfree(ax->mem2);
  749. exit_mem1:
  750. iounmap(ei_status.mem);
  751. exit_req:
  752. release_resource(ax->mem);
  753. kfree(ax->mem);
  754. exit_mem:
  755. free_netdev(dev);
  756. return ret;
  757. }
  758. /* suspend and resume */
  759. #ifdef CONFIG_PM
  760. static int ax_suspend(struct platform_device *dev, pm_message_t state)
  761. {
  762. struct net_device *ndev = platform_get_drvdata(dev);
  763. struct ax_device *ax = to_ax_dev(ndev);
  764. ax->resume_open = ax->running;
  765. netif_device_detach(ndev);
  766. ax_close(ndev);
  767. return 0;
  768. }
  769. static int ax_resume(struct platform_device *pdev)
  770. {
  771. struct net_device *ndev = platform_get_drvdata(pdev);
  772. struct ax_device *ax = to_ax_dev(ndev);
  773. ax_initial_setup(ndev, netdev_priv(ndev));
  774. ax_NS8390_init(ndev, ax->resume_open);
  775. netif_device_attach(ndev);
  776. if (ax->resume_open)
  777. ax_open(ndev);
  778. return 0;
  779. }
  780. #else
  781. #define ax_suspend NULL
  782. #define ax_resume NULL
  783. #endif
  784. static struct platform_driver axdrv = {
  785. .driver = {
  786. .name = "ax88796",
  787. .owner = THIS_MODULE,
  788. },
  789. .probe = ax_probe,
  790. .remove = ax_remove,
  791. .suspend = ax_suspend,
  792. .resume = ax_resume,
  793. };
  794. static int __init axdrv_init(void)
  795. {
  796. return platform_driver_register(&axdrv);
  797. }
  798. static void __exit axdrv_exit(void)
  799. {
  800. platform_driver_unregister(&axdrv);
  801. }
  802. module_init(axdrv_init);
  803. module_exit(axdrv_exit);
  804. MODULE_DESCRIPTION("AX88796 10/100 Ethernet platform driver");
  805. MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
  806. MODULE_LICENSE("GPL v2");
  807. MODULE_ALIAS("platform:ax88796");