ax88796.c 23 KB

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