MERGE-master-patchset-edits
[linux-2.6/openmoko-kernel.git] / drivers / net / ax88796.c
blob337488ec707cfc2aad6b14ae7aec63315510529c
1 /* drivers/net/ax88796.c
3 * Copyright 2005,2007 Simtec Electronics
4 * Ben Dooks <ben@simtec.co.uk>
6 * Asix AX88796 10/100 Ethernet controller support
7 * Based on ne.c, by Donald Becker, et-al.
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.
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/isapnp.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.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/mii.h>
27 #include <linux/eeprom_93cx6.h>
29 #include <net/ax88796.h>
31 #include <asm/system.h>
32 #include <asm/io.h>
34 static int phy_debug = 0;
36 /* Rename the lib8390.c functions to show that they are in this driver */
37 #define __ei_open ax_ei_open
38 #define __ei_close ax_ei_close
39 #define __ei_poll ax_ei_poll
40 #define __ei_tx_timeout ax_ei_tx_timeout
41 #define __ei_interrupt ax_ei_interrupt
42 #define ____alloc_ei_netdev ax__alloc_ei_netdev
43 #define __NS8390_init ax_NS8390_init
45 /* force unsigned long back to 'void __iomem *' */
46 #define ax_convert_addr(_a) ((void __force __iomem *)(_a))
48 #define ei_inb(_a) readb(ax_convert_addr(_a))
49 #define ei_outb(_v, _a) writeb(_v, ax_convert_addr(_a))
51 #define ei_inb_p(_a) ei_inb(_a)
52 #define ei_outb_p(_v, _a) ei_outb(_v, _a)
54 /* define EI_SHIFT() to take into account our register offsets */
55 #define EI_SHIFT(x) (ei_local->reg_offset[(x)])
57 /* Ensure we have our RCR base value */
58 #define AX88796_PLATFORM
60 static unsigned char version[] = "ax88796.c: Copyright 2005,2007 Simtec Electronics\n";
62 #include "lib8390.c"
64 #define DRV_NAME "ax88796"
65 #define DRV_VERSION "1.00"
67 /* from ne.c */
68 #define NE_CMD EI_SHIFT(0x00)
69 #define NE_RESET EI_SHIFT(0x1f)
70 #define NE_DATAPORT EI_SHIFT(0x10)
72 #define NE1SM_START_PG 0x20 /* First page of TX buffer */
73 #define NE1SM_STOP_PG 0x40 /* Last page +1 of RX ring */
74 #define NESM_START_PG 0x40 /* First page of TX buffer */
75 #define NESM_STOP_PG 0x80 /* Last page +1 of RX ring */
77 /* device private data */
79 struct ax_device {
80 struct timer_list mii_timer;
81 spinlock_t mii_lock;
82 struct mii_if_info mii;
84 u32 msg_enable;
85 void __iomem *map2;
86 struct platform_device *dev;
87 struct resource *mem;
88 struct resource *mem2;
89 struct ax_plat_data *plat;
91 unsigned char running;
92 unsigned char resume_open;
94 u32 reg_offsets[0x20];
97 static inline struct ax_device *to_ax_dev(struct net_device *dev)
99 struct ei_device *ei_local = netdev_priv(dev);
100 return (struct ax_device *)(ei_local+1);
103 /* ax_initial_check
105 * do an initial probe for the card to check wether it exists
106 * and is functional
109 static int ax_initial_check(struct net_device *dev)
111 struct ei_device *ei_local = netdev_priv(dev);
112 void __iomem *ioaddr = ei_local->mem;
113 int reg0;
114 int regd;
116 reg0 = ei_inb(ioaddr);
117 if (reg0 == 0xFF)
118 return -ENODEV;
120 ei_outb(E8390_NODMA+E8390_PAGE1+E8390_STOP, ioaddr + E8390_CMD);
121 regd = ei_inb(ioaddr + 0x0d);
122 ei_outb(0xff, ioaddr + 0x0d);
123 ei_outb(E8390_NODMA+E8390_PAGE0, ioaddr + E8390_CMD);
124 ei_inb(ioaddr + EN0_COUNTER0); /* Clear the counter by reading. */
125 if (ei_inb(ioaddr + EN0_COUNTER0) != 0) {
126 ei_outb(reg0, ioaddr);
127 ei_outb(regd, ioaddr + 0x0d); /* Restore the old values. */
128 return -ENODEV;
131 return 0;
134 /* Hard reset the card. This used to pause for the same period that a
135 8390 reset command required, but that shouldn't be necessary. */
137 static void ax_reset_8390(struct net_device *dev)
139 struct ei_device *ei_local = netdev_priv(dev);
140 struct ax_device *ax = to_ax_dev(dev);
141 unsigned long reset_start_time = jiffies;
142 void __iomem *addr = (void __iomem *)dev->base_addr;
144 if (ei_debug > 1)
145 dev_dbg(&ax->dev->dev, "resetting the 8390 t=%ld\n", jiffies);
147 ei_outb(ei_inb(addr + NE_RESET), addr + NE_RESET);
149 ei_status.txing = 0;
150 ei_status.dmaing = 0;
152 /* This check _should_not_ be necessary, omit eventually. */
153 while ((ei_inb(addr + EN0_ISR) & ENISR_RESET) == 0) {
154 if (jiffies - reset_start_time > 2*HZ/100) {
155 dev_warn(&ax->dev->dev, "%s: %s did not complete.\n",
156 __func__, dev->name);
157 break;
161 ei_outb(ENISR_RESET, addr + EN0_ISR); /* Ack intr. */
165 static void ax_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
166 int ring_page)
168 struct ei_device *ei_local = netdev_priv(dev);
169 struct ax_device *ax = to_ax_dev(dev);
170 void __iomem *nic_base = ei_local->mem;
172 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
173 if (ei_status.dmaing) {
174 dev_err(&ax->dev->dev, "%s: DMAing conflict in %s "
175 "[DMAstat:%d][irqlock:%d].\n",
176 dev->name, __func__,
177 ei_status.dmaing, ei_status.irqlock);
178 return;
181 ei_status.dmaing |= 0x01;
182 ei_outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
183 ei_outb(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
184 ei_outb(0, nic_base + EN0_RCNTHI);
185 ei_outb(0, nic_base + EN0_RSARLO); /* On page boundary */
186 ei_outb(ring_page, nic_base + EN0_RSARHI);
187 ei_outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
189 if (ei_status.word16)
190 readsw(nic_base + NE_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr)>>1);
191 else
192 readsb(nic_base + NE_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr));
194 ei_outb(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
195 ei_status.dmaing &= ~0x01;
197 le16_to_cpus(&hdr->count);
201 /* Block input and output, similar to the Crynwr packet driver. If you
202 are porting to a new ethercard, look at the packet driver source for hints.
203 The NEx000 doesn't share the on-board packet memory -- you have to put
204 the packet out through the "remote DMA" dataport using ei_outb. */
206 static void ax_block_input(struct net_device *dev, int count,
207 struct sk_buff *skb, int ring_offset)
209 struct ei_device *ei_local = netdev_priv(dev);
210 struct ax_device *ax = to_ax_dev(dev);
211 void __iomem *nic_base = ei_local->mem;
212 char *buf = skb->data;
214 if (ei_status.dmaing) {
215 dev_err(&ax->dev->dev,
216 "%s: DMAing conflict in %s "
217 "[DMAstat:%d][irqlock:%d].\n",
218 dev->name, __func__,
219 ei_status.dmaing, ei_status.irqlock);
220 return;
223 ei_status.dmaing |= 0x01;
225 ei_outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
226 ei_outb(count & 0xff, nic_base + EN0_RCNTLO);
227 ei_outb(count >> 8, nic_base + EN0_RCNTHI);
228 ei_outb(ring_offset & 0xff, nic_base + EN0_RSARLO);
229 ei_outb(ring_offset >> 8, nic_base + EN0_RSARHI);
230 ei_outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
232 if (ei_status.word16) {
233 readsw(nic_base + NE_DATAPORT, buf, count >> 1);
234 if (count & 0x01)
235 buf[count-1] = ei_inb(nic_base + NE_DATAPORT);
237 } else {
238 readsb(nic_base + NE_DATAPORT, buf, count);
241 ei_status.dmaing &= ~1;
244 static void ax_block_output(struct net_device *dev, int count,
245 const unsigned char *buf, const int start_page)
247 struct ei_device *ei_local = netdev_priv(dev);
248 struct ax_device *ax = to_ax_dev(dev);
249 void __iomem *nic_base = ei_local->mem;
250 unsigned long dma_start;
252 /* Round the count up for word writes. Do we need to do this?
253 What effect will an odd byte count have on the 8390?
254 I should check someday. */
256 if (ei_status.word16 && (count & 0x01))
257 count++;
259 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
260 if (ei_status.dmaing) {
261 dev_err(&ax->dev->dev, "%s: DMAing conflict in %s."
262 "[DMAstat:%d][irqlock:%d]\n",
263 dev->name, __func__,
264 ei_status.dmaing, ei_status.irqlock);
265 return;
268 ei_status.dmaing |= 0x01;
269 /* We should already be in page 0, but to be safe... */
270 ei_outb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
272 ei_outb(ENISR_RDC, nic_base + EN0_ISR);
274 /* Now the normal output. */
275 ei_outb(count & 0xff, nic_base + EN0_RCNTLO);
276 ei_outb(count >> 8, nic_base + EN0_RCNTHI);
277 ei_outb(0x00, nic_base + EN0_RSARLO);
278 ei_outb(start_page, nic_base + EN0_RSARHI);
280 ei_outb(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
281 if (ei_status.word16) {
282 writesw(nic_base + NE_DATAPORT, buf, count>>1);
283 } else {
284 writesb(nic_base + NE_DATAPORT, buf, count);
287 dma_start = jiffies;
289 while ((ei_inb(nic_base + EN0_ISR) & ENISR_RDC) == 0) {
290 if (jiffies - dma_start > 2*HZ/100) { /* 20ms */
291 dev_warn(&ax->dev->dev,
292 "%s: timeout waiting for Tx RDC.\n", dev->name);
293 ax_reset_8390(dev);
294 ax_NS8390_init(dev,1);
295 break;
299 ei_outb(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
300 ei_status.dmaing &= ~0x01;
301 return;
304 /* definitions for accessing MII/EEPROM interface */
306 #define AX_MEMR EI_SHIFT(0x14)
307 #define AX_MEMR_MDC (1<<0)
308 #define AX_MEMR_MDIR (1<<1)
309 #define AX_MEMR_MDI (1<<2)
310 #define AX_MEMR_MDO (1<<3)
311 #define AX_MEMR_EECS (1<<4)
312 #define AX_MEMR_EEI (1<<5)
313 #define AX_MEMR_EEO (1<<6)
314 #define AX_MEMR_EECLK (1<<7)
316 /* ax_mii_ei_outbits
318 * write the specified set of bits to the phy
321 static void
322 ax_mii_ei_outbits(struct net_device *dev, unsigned int bits, int len)
324 struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
325 void __iomem *memr_addr = (void __iomem *)dev->base_addr + AX_MEMR;
326 unsigned int memr;
328 /* clock low, data to output mode */
329 memr = ei_inb(memr_addr);
330 memr &= ~(AX_MEMR_MDC | AX_MEMR_MDIR);
331 ei_outb(memr, memr_addr);
333 for (len--; len >= 0; len--) {
334 if (bits & (1 << len))
335 memr |= AX_MEMR_MDO;
336 else
337 memr &= ~AX_MEMR_MDO;
339 ei_outb(memr, memr_addr);
341 /* clock high */
343 ei_outb(memr | AX_MEMR_MDC, memr_addr);
344 udelay(1);
346 /* clock low */
347 ei_outb(memr, memr_addr);
350 /* leaves the clock line low, mdir input */
351 memr |= AX_MEMR_MDIR;
352 ei_outb(memr, (void __iomem *)dev->base_addr + AX_MEMR);
355 /* ax_phy_ei_inbits
357 * read a specified number of bits from the phy
360 static unsigned int
361 ax_phy_ei_inbits(struct net_device *dev, int no)
363 struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
364 void __iomem *memr_addr = (void __iomem *)dev->base_addr + AX_MEMR;
365 unsigned int memr;
366 unsigned int result = 0;
368 /* clock low, data to input mode */
369 memr = ei_inb(memr_addr);
370 memr &= ~AX_MEMR_MDC;
371 memr |= AX_MEMR_MDIR;
372 ei_outb(memr, memr_addr);
374 for (no--; no >= 0; no--) {
375 ei_outb(memr | AX_MEMR_MDC, memr_addr);
377 udelay(1);
379 if (ei_inb(memr_addr) & AX_MEMR_MDI)
380 result |= (1<<no);
382 ei_outb(memr, memr_addr);
385 return result;
388 /* ax_phy_issueaddr
390 * use the low level bit shifting routines to send the address
391 * and command to the specified phy
394 static void
395 ax_phy_issueaddr(struct net_device *dev, int phy_addr, int reg, int opc)
397 if (phy_debug)
398 pr_debug("%s: dev %p, %04x, %04x, %d\n",
399 __func__, dev, phy_addr, reg, opc);
401 ax_mii_ei_outbits(dev, 0x3f, 6); /* pre-amble */
402 ax_mii_ei_outbits(dev, 1, 2); /* frame-start */
403 ax_mii_ei_outbits(dev, opc, 2); /* op code */
404 ax_mii_ei_outbits(dev, phy_addr, 5); /* phy address */
405 ax_mii_ei_outbits(dev, reg, 5); /* reg address */
408 static int
409 ax_phy_read(struct net_device *dev, int phy_addr, int reg)
411 struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
412 unsigned long flags;
413 unsigned int result;
415 spin_lock_irqsave(&ei_local->page_lock, flags);
417 ax_phy_issueaddr(dev, phy_addr, reg, 2);
419 result = ax_phy_ei_inbits(dev, 17);
420 result &= ~(3<<16);
422 spin_unlock_irqrestore(&ei_local->page_lock, flags);
424 if (phy_debug)
425 pr_debug("%s: %04x.%04x => read %04x\n", __func__,
426 phy_addr, reg, result);
428 return result;
431 static void
432 ax_phy_write(struct net_device *dev, int phy_addr, int reg, int value)
434 struct ei_device *ei = (struct ei_device *) netdev_priv(dev);
435 struct ax_device *ax = to_ax_dev(dev);
436 unsigned long flags;
438 dev_dbg(&ax->dev->dev, "%s: %p, %04x, %04x %04x\n",
439 __func__, dev, phy_addr, reg, value);
441 spin_lock_irqsave(&ei->page_lock, flags);
443 ax_phy_issueaddr(dev, phy_addr, reg, 1);
444 ax_mii_ei_outbits(dev, 2, 2); /* send TA */
445 ax_mii_ei_outbits(dev, value, 16);
447 spin_unlock_irqrestore(&ei->page_lock, flags);
450 static void ax_mii_expiry(unsigned long data)
452 struct net_device *dev = (struct net_device *)data;
453 struct ax_device *ax = to_ax_dev(dev);
454 unsigned long flags;
456 spin_lock_irqsave(&ax->mii_lock, flags);
457 mii_check_media(&ax->mii, netif_msg_link(ax), 0);
458 spin_unlock_irqrestore(&ax->mii_lock, flags);
460 if (ax->running) {
461 ax->mii_timer.expires = jiffies + HZ*2;
462 add_timer(&ax->mii_timer);
466 static int ax_open(struct net_device *dev)
468 struct ax_device *ax = to_ax_dev(dev);
469 struct ei_device *ei_local = netdev_priv(dev);
470 int ret;
472 dev_dbg(&ax->dev->dev, "%s: open\n", dev->name);
474 ret = request_irq(dev->irq, ax_ei_interrupt, 0, dev->name, dev);
475 if (ret)
476 return ret;
478 ret = ax_ei_open(dev);
479 if (ret)
480 return ret;
482 /* turn the phy on (if turned off) */
484 ei_outb(ax->plat->gpoc_val, ei_local->mem + EI_SHIFT(0x17));
485 ax->running = 1;
487 /* start the MII timer */
489 init_timer(&ax->mii_timer);
491 ax->mii_timer.expires = jiffies+1;
492 ax->mii_timer.data = (unsigned long) dev;
493 ax->mii_timer.function = ax_mii_expiry;
495 add_timer(&ax->mii_timer);
497 return 0;
500 static int ax_close(struct net_device *dev)
502 struct ax_device *ax = to_ax_dev(dev);
503 struct ei_device *ei_local = netdev_priv(dev);
505 dev_dbg(&ax->dev->dev, "%s: close\n", dev->name);
507 /* turn the phy off */
509 ei_outb(ax->plat->gpoc_val | (1<<6),
510 ei_local->mem + EI_SHIFT(0x17));
512 ax->running = 0;
513 wmb();
515 del_timer_sync(&ax->mii_timer);
516 ax_ei_close(dev);
518 free_irq(dev->irq, dev);
519 return 0;
522 static int ax_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
524 struct ax_device *ax = to_ax_dev(dev);
525 unsigned long flags;
526 int rc;
528 if (!netif_running(dev))
529 return -EINVAL;
531 spin_lock_irqsave(&ax->mii_lock, flags);
532 rc = generic_mii_ioctl(&ax->mii, if_mii(req), cmd, NULL);
533 spin_unlock_irqrestore(&ax->mii_lock, flags);
535 return rc;
538 /* ethtool ops */
540 static void ax_get_drvinfo(struct net_device *dev,
541 struct ethtool_drvinfo *info)
543 struct ax_device *ax = to_ax_dev(dev);
545 strcpy(info->driver, DRV_NAME);
546 strcpy(info->version, DRV_VERSION);
547 strcpy(info->bus_info, ax->dev->name);
550 static int ax_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
552 struct ax_device *ax = to_ax_dev(dev);
553 unsigned long flags;
555 spin_lock_irqsave(&ax->mii_lock, flags);
556 mii_ethtool_gset(&ax->mii, cmd);
557 spin_unlock_irqrestore(&ax->mii_lock, flags);
559 return 0;
562 static int ax_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
564 struct ax_device *ax = to_ax_dev(dev);
565 unsigned long flags;
566 int rc;
568 spin_lock_irqsave(&ax->mii_lock, flags);
569 rc = mii_ethtool_sset(&ax->mii, cmd);
570 spin_unlock_irqrestore(&ax->mii_lock, flags);
572 return rc;
575 static int ax_nway_reset(struct net_device *dev)
577 struct ax_device *ax = to_ax_dev(dev);
578 return mii_nway_restart(&ax->mii);
581 static u32 ax_get_link(struct net_device *dev)
583 struct ax_device *ax = to_ax_dev(dev);
584 return mii_link_ok(&ax->mii);
587 static const struct ethtool_ops ax_ethtool_ops = {
588 .get_drvinfo = ax_get_drvinfo,
589 .get_settings = ax_get_settings,
590 .set_settings = ax_set_settings,
591 .nway_reset = ax_nway_reset,
592 .get_link = ax_get_link,
595 #ifdef CONFIG_AX88796_93CX6
596 static void ax_eeprom_register_read(struct eeprom_93cx6 *eeprom)
598 struct ei_device *ei_local = eeprom->data;
599 u8 reg = ei_inb(ei_local->mem + AX_MEMR);
601 eeprom->reg_data_in = reg & AX_MEMR_EEI;
602 eeprom->reg_data_out = reg & AX_MEMR_EEO; /* Input pin */
603 eeprom->reg_data_clock = reg & AX_MEMR_EECLK;
604 eeprom->reg_chip_select = reg & AX_MEMR_EECS;
607 static void ax_eeprom_register_write(struct eeprom_93cx6 *eeprom)
609 struct ei_device *ei_local = eeprom->data;
610 u8 reg = ei_inb(ei_local->mem + AX_MEMR);
612 reg &= ~(AX_MEMR_EEI | AX_MEMR_EECLK | AX_MEMR_EECS);
614 if (eeprom->reg_data_in)
615 reg |= AX_MEMR_EEI;
616 if (eeprom->reg_data_clock)
617 reg |= AX_MEMR_EECLK;
618 if (eeprom->reg_chip_select)
619 reg |= AX_MEMR_EECS;
621 ei_outb(reg, ei_local->mem + AX_MEMR);
622 udelay(10);
624 #endif
626 /* setup code */
628 static void ax_initial_setup(struct net_device *dev, struct ei_device *ei_local)
630 void __iomem *ioaddr = ei_local->mem;
631 struct ax_device *ax = to_ax_dev(dev);
633 /* Select page 0*/
634 ei_outb(E8390_NODMA+E8390_PAGE0+E8390_STOP, ioaddr + E8390_CMD);
636 /* set to byte access */
637 ei_outb(ax->plat->dcr_val & ~1, ioaddr + EN0_DCFG);
638 ei_outb(ax->plat->gpoc_val, ioaddr + EI_SHIFT(0x17));
641 /* ax_init_dev
643 * initialise the specified device, taking care to note the MAC
644 * address it may already have (if configured), ensure
645 * the device is ready to be used by lib8390.c and registerd with
646 * the network layer.
649 static int ax_init_dev(struct net_device *dev, int first_init)
651 struct ei_device *ei_local = netdev_priv(dev);
652 struct ax_device *ax = to_ax_dev(dev);
653 void __iomem *ioaddr = ei_local->mem;
654 unsigned int start_page;
655 unsigned int stop_page;
656 int ret;
657 int i;
659 ret = ax_initial_check(dev);
660 if (ret)
661 goto err_out;
663 /* setup goes here */
665 ax_initial_setup(dev, ei_local);
667 /* read the mac from the card prom if we need it */
669 if (first_init && ax->plat->flags & AXFLG_HAS_EEPROM) {
670 unsigned char SA_prom[32];
672 for(i = 0; i < sizeof(SA_prom); i+=2) {
673 SA_prom[i] = ei_inb(ioaddr + NE_DATAPORT);
674 SA_prom[i+1] = ei_inb(ioaddr + NE_DATAPORT);
677 if (ax->plat->wordlength == 2)
678 for (i = 0; i < 16; i++)
679 SA_prom[i] = SA_prom[i+i];
681 memcpy(dev->dev_addr, SA_prom, 6);
684 #ifdef CONFIG_AX88796_93CX6
685 if (first_init && ax->plat->flags & AXFLG_HAS_93CX6) {
686 unsigned char mac_addr[6];
687 struct eeprom_93cx6 eeprom;
689 eeprom.data = ei_local;
690 eeprom.register_read = ax_eeprom_register_read;
691 eeprom.register_write = ax_eeprom_register_write;
692 eeprom.width = PCI_EEPROM_WIDTH_93C56;
694 eeprom_93cx6_multiread(&eeprom, 0,
695 (__le16 __force *)mac_addr,
696 sizeof(mac_addr) >> 1);
698 memcpy(dev->dev_addr, mac_addr, 6);
700 #endif
701 if (ax->plat->wordlength == 2) {
702 /* We must set the 8390 for word mode. */
703 ei_outb(ax->plat->dcr_val, ei_local->mem + EN0_DCFG);
704 start_page = NESM_START_PG;
705 stop_page = NESM_STOP_PG;
706 } else {
707 start_page = NE1SM_START_PG;
708 stop_page = NE1SM_STOP_PG;
711 /* load the mac-address from the device if this is the
712 * first time we've initialised */
714 if (first_init && ax->plat->flags & AXFLG_MAC_FROMDEV) {
715 ei_outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP,
716 ei_local->mem + E8390_CMD); /* 0x61 */
718 for (i = 0 ; i < ETHER_ADDR_LEN ; i++)
719 dev->dev_addr[i] = ei_inb(ioaddr + EN1_PHYS_SHIFT(i));
722 ax_reset_8390(dev);
724 ei_status.name = "AX88796";
725 ei_status.tx_start_page = start_page;
726 ei_status.stop_page = stop_page;
727 ei_status.word16 = (ax->plat->wordlength == 2);
728 ei_status.rx_start_page = start_page + TX_PAGES;
730 #ifdef PACKETBUF_MEMSIZE
731 /* Allow the packet buffer size to be overridden by know-it-alls. */
732 ei_status.stop_page = ei_status.tx_start_page + PACKETBUF_MEMSIZE;
733 #endif
735 ei_status.reset_8390 = &ax_reset_8390;
736 ei_status.block_input = &ax_block_input;
737 ei_status.block_output = &ax_block_output;
738 ei_status.get_8390_hdr = &ax_get_8390_hdr;
739 ei_status.priv = 0;
741 dev->open = ax_open;
742 dev->stop = ax_close;
743 dev->do_ioctl = ax_ioctl;
744 dev->ethtool_ops = &ax_ethtool_ops;
746 ax->msg_enable = NETIF_MSG_LINK;
747 ax->mii.phy_id_mask = 0x1f;
748 ax->mii.reg_num_mask = 0x1f;
749 ax->mii.phy_id = 0x10; /* onboard phy */
750 ax->mii.force_media = 0;
751 ax->mii.full_duplex = 0;
752 ax->mii.mdio_read = ax_phy_read;
753 ax->mii.mdio_write = ax_phy_write;
754 ax->mii.dev = dev;
756 #ifdef CONFIG_NET_POLL_CONTROLLER
757 dev->poll_controller = ax_ei_poll;
758 #endif
759 ax_NS8390_init(dev, 0);
761 if (first_init)
762 dev_info(&ax->dev->dev, "%dbit, irq %d, %lx, MAC: %pM\n",
763 ei_status.word16 ? 16:8, dev->irq, dev->base_addr,
764 dev->dev_addr);
766 ret = register_netdev(dev);
767 if (ret)
768 goto out_irq;
770 return 0;
772 out_irq:
773 /* cleanup irq */
774 free_irq(dev->irq, dev);
775 err_out:
776 return ret;
779 static int ax_remove(struct platform_device *_dev)
781 struct net_device *dev = platform_get_drvdata(_dev);
782 struct ax_device *ax;
784 ax = to_ax_dev(dev);
786 unregister_netdev(dev);
787 free_irq(dev->irq, dev);
789 iounmap(ei_status.mem);
790 release_resource(ax->mem);
791 kfree(ax->mem);
793 if (ax->map2) {
794 iounmap(ax->map2);
795 release_resource(ax->mem2);
796 kfree(ax->mem2);
799 free_netdev(dev);
801 return 0;
804 /* ax_probe
806 * This is the entry point when the platform device system uses to
807 * notify us of a new device to attach to. Allocate memory, find
808 * the resources and information passed, and map the necessary registers.
811 static int ax_probe(struct platform_device *pdev)
813 struct net_device *dev;
814 struct ax_device *ax;
815 struct resource *res;
816 size_t size;
817 int ret;
819 dev = ax__alloc_ei_netdev(sizeof(struct ax_device));
820 if (dev == NULL)
821 return -ENOMEM;
823 /* ok, let's setup our device */
824 ax = to_ax_dev(dev);
826 memset(ax, 0, sizeof(struct ax_device));
828 spin_lock_init(&ax->mii_lock);
830 ax->dev = pdev;
831 ax->plat = pdev->dev.platform_data;
832 platform_set_drvdata(pdev, dev);
834 ei_status.rxcr_base = ax->plat->rcr_val;
836 /* find the platform resources */
838 ret = platform_get_irq(pdev, 0);
839 if (ret < 0) {
840 dev_err(&pdev->dev, "no IRQ specified\n");
841 goto exit_mem;
843 dev->irq = ret;
845 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
846 if (res == NULL) {
847 dev_err(&pdev->dev, "no MEM specified\n");
848 ret = -ENXIO;
849 goto exit_mem;
852 size = (res->end - res->start) + 1;
854 /* setup the register offsets from either the platform data
855 * or by using the size of the resource provided */
857 if (ax->plat->reg_offsets)
858 ei_status.reg_offset = ax->plat->reg_offsets;
859 else {
860 ei_status.reg_offset = ax->reg_offsets;
861 for (ret = 0; ret < 0x18; ret++)
862 ax->reg_offsets[ret] = (size / 0x18) * ret;
865 ax->mem = request_mem_region(res->start, size, pdev->name);
866 if (ax->mem == NULL) {
867 dev_err(&pdev->dev, "cannot reserve registers\n");
868 ret = -ENXIO;
869 goto exit_mem;
872 ei_status.mem = ioremap(res->start, size);
873 dev->base_addr = (unsigned long)ei_status.mem;
875 if (ei_status.mem == NULL) {
876 dev_err(&pdev->dev, "Cannot ioremap area (%08llx,%08llx)\n",
877 (unsigned long long)res->start,
878 (unsigned long long)res->end);
880 ret = -ENXIO;
881 goto exit_req;
884 /* look for reset area */
886 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
887 if (res == NULL) {
888 if (!ax->plat->reg_offsets) {
889 for (ret = 0; ret < 0x20; ret++)
890 ax->reg_offsets[ret] = (size / 0x20) * ret;
893 ax->map2 = NULL;
894 } else {
895 size = (res->end - res->start) + 1;
897 ax->mem2 = request_mem_region(res->start, size, pdev->name);
898 if (ax->mem == NULL) {
899 dev_err(&pdev->dev, "cannot reserve registers\n");
900 ret = -ENXIO;
901 goto exit_mem1;
904 ax->map2 = ioremap(res->start, size);
905 if (ax->map2 == NULL) {
906 dev_err(&pdev->dev, "cannot map reset register\n");
907 ret = -ENXIO;
908 goto exit_mem2;
911 ei_status.reg_offset[0x1f] = ax->map2 - ei_status.mem;
914 /* got resources, now initialise and register device */
916 ret = ax_init_dev(dev, 1);
917 if (!ret)
918 return 0;
920 if (ax->map2 == NULL)
921 goto exit_mem1;
923 iounmap(ax->map2);
925 exit_mem2:
926 release_resource(ax->mem2);
927 kfree(ax->mem2);
929 exit_mem1:
930 iounmap(ei_status.mem);
932 exit_req:
933 release_resource(ax->mem);
934 kfree(ax->mem);
936 exit_mem:
937 free_netdev(dev);
939 return ret;
942 /* suspend and resume */
944 #ifdef CONFIG_PM
945 static int ax_suspend(struct platform_device *dev, pm_message_t state)
947 struct net_device *ndev = platform_get_drvdata(dev);
948 struct ax_device *ax = to_ax_dev(ndev);
950 ax->resume_open = ax->running;
952 netif_device_detach(ndev);
953 ax_close(ndev);
955 return 0;
958 static int ax_resume(struct platform_device *pdev)
960 struct net_device *ndev = platform_get_drvdata(pdev);
961 struct ax_device *ax = to_ax_dev(ndev);
963 ax_initial_setup(ndev, netdev_priv(ndev));
964 ax_NS8390_init(ndev, ax->resume_open);
965 netif_device_attach(ndev);
967 if (ax->resume_open)
968 ax_open(ndev);
970 return 0;
973 #else
974 #define ax_suspend NULL
975 #define ax_resume NULL
976 #endif
978 static struct platform_driver axdrv = {
979 .driver = {
980 .name = "ax88796",
981 .owner = THIS_MODULE,
983 .probe = ax_probe,
984 .remove = ax_remove,
985 .suspend = ax_suspend,
986 .resume = ax_resume,
989 static int __init axdrv_init(void)
991 return platform_driver_register(&axdrv);
994 static void __exit axdrv_exit(void)
996 platform_driver_unregister(&axdrv);
999 module_init(axdrv_init);
1000 module_exit(axdrv_exit);
1002 MODULE_DESCRIPTION("AX88796 10/100 Ethernet platform driver");
1003 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
1004 MODULE_LICENSE("GPL v2");
1005 MODULE_ALIAS("platform:ax88796");