ax88796.c 25 KB

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