ax88796.c 24 KB

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