ax88796.c 24 KB

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