ax88796.c 24 KB

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