ax88796.c 24 KB

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