ax88796.c 24 KB

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