ax88796.c 25 KB

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