ax88796.c 25 KB

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