ax88796.c 24 KB

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