ax88796.c 23 KB

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