ax88796.c 23 KB

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