ax88796.c 25 KB

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