md: lock address when changing attributes of component devices
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / ax88796.c
blob194949afacd05142655f7d1a42e00b8b894f6697
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 __FUNCTION__, 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, __FUNCTION__,
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, __FUNCTION__,
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, __FUNCTION__,
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 __FUNCTION__, 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", __FUNCTION__,
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 __FUNCTION__, 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_lock_irqsave(&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_lock_irqsave(&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 DECLARE_MAC_BUF(mac);
764 dev_info(&ax->dev->dev, "%dbit, irq %d, %lx, MAC: %s\n",
765 ei_status.word16 ? 16:8, dev->irq, dev->base_addr,
766 print_mac(mac, dev->dev_addr));
769 ret = register_netdev(dev);
770 if (ret)
771 goto out_irq;
773 return 0;
775 out_irq:
776 /* cleanup irq */
777 free_irq(dev->irq, dev);
778 err_out:
779 return ret;
782 static int ax_remove(struct platform_device *_dev)
784 struct net_device *dev = platform_get_drvdata(_dev);
785 struct ax_device *ax;
787 ax = to_ax_dev(dev);
789 unregister_netdev(dev);
790 free_irq(dev->irq, dev);
792 iounmap(ei_status.mem);
793 release_resource(ax->mem);
794 kfree(ax->mem);
796 if (ax->map2) {
797 iounmap(ax->map2);
798 release_resource(ax->mem2);
799 kfree(ax->mem2);
802 free_netdev(dev);
804 return 0;
807 /* ax_probe
809 * This is the entry point when the platform device system uses to
810 * notify us of a new device to attach to. Allocate memory, find
811 * the resources and information passed, and map the necessary registers.
814 static int ax_probe(struct platform_device *pdev)
816 struct net_device *dev;
817 struct ax_device *ax;
818 struct resource *res;
819 size_t size;
820 int ret;
822 dev = ax__alloc_ei_netdev(sizeof(struct ax_device));
823 if (dev == NULL)
824 return -ENOMEM;
826 /* ok, let's setup our device */
827 ax = to_ax_dev(dev);
829 memset(ax, 0, sizeof(struct ax_device));
831 spin_lock_init(&ax->mii_lock);
833 ax->dev = pdev;
834 ax->plat = pdev->dev.platform_data;
835 platform_set_drvdata(pdev, dev);
837 ei_status.rxcr_base = ax->plat->rcr_val;
839 /* find the platform resources */
841 dev->irq = platform_get_irq(pdev, 0);
842 if (dev->irq < 0) {
843 dev_err(&pdev->dev, "no IRQ specified\n");
844 ret = -ENXIO;
845 goto exit_mem;
848 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
849 if (res == NULL) {
850 dev_err(&pdev->dev, "no MEM specified\n");
851 ret = -ENXIO;
852 goto exit_mem;
855 size = (res->end - res->start) + 1;
857 /* setup the register offsets from either the platform data
858 * or by using the size of the resource provided */
860 if (ax->plat->reg_offsets)
861 ei_status.reg_offset = ax->plat->reg_offsets;
862 else {
863 ei_status.reg_offset = ax->reg_offsets;
864 for (ret = 0; ret < 0x18; ret++)
865 ax->reg_offsets[ret] = (size / 0x18) * ret;
868 ax->mem = request_mem_region(res->start, size, pdev->name);
869 if (ax->mem == NULL) {
870 dev_err(&pdev->dev, "cannot reserve registers\n");
871 ret = -ENXIO;
872 goto exit_mem;
875 ei_status.mem = ioremap(res->start, size);
876 dev->base_addr = (unsigned long)ei_status.mem;
878 if (ei_status.mem == NULL) {
879 dev_err(&pdev->dev, "Cannot ioremap area (%08llx,%08llx)\n",
880 (unsigned long long)res->start,
881 (unsigned long long)res->end);
883 ret = -ENXIO;
884 goto exit_req;
887 /* look for reset area */
889 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
890 if (res == NULL) {
891 if (!ax->plat->reg_offsets) {
892 for (ret = 0; ret < 0x20; ret++)
893 ax->reg_offsets[ret] = (size / 0x20) * ret;
896 ax->map2 = NULL;
897 } else {
898 size = (res->end - res->start) + 1;
900 ax->mem2 = request_mem_region(res->start, size, pdev->name);
901 if (ax->mem == NULL) {
902 dev_err(&pdev->dev, "cannot reserve registers\n");
903 ret = -ENXIO;
904 goto exit_mem1;
907 ax->map2 = ioremap(res->start, size);
908 if (ax->map2 == NULL) {
909 dev_err(&pdev->dev, "cannot map reset register\n");
910 ret = -ENXIO;
911 goto exit_mem2;
914 ei_status.reg_offset[0x1f] = ax->map2 - ei_status.mem;
917 /* got resources, now initialise and register device */
919 ret = ax_init_dev(dev, 1);
920 if (!ret)
921 return 0;
923 if (ax->map2 == NULL)
924 goto exit_mem1;
926 iounmap(ax->map2);
928 exit_mem2:
929 release_resource(ax->mem2);
930 kfree(ax->mem2);
932 exit_mem1:
933 iounmap(ei_status.mem);
935 exit_req:
936 release_resource(ax->mem);
937 kfree(ax->mem);
939 exit_mem:
940 free_netdev(dev);
942 return ret;
945 /* suspend and resume */
947 #ifdef CONFIG_PM
948 static int ax_suspend(struct platform_device *dev, pm_message_t state)
950 struct net_device *ndev = platform_get_drvdata(dev);
951 struct ax_device *ax = to_ax_dev(ndev);
953 ax->resume_open = ax->running;
955 netif_device_detach(ndev);
956 ax_close(ndev);
958 return 0;
961 static int ax_resume(struct platform_device *pdev)
963 struct net_device *ndev = platform_get_drvdata(pdev);
964 struct ax_device *ax = to_ax_dev(ndev);
966 ax_initial_setup(ndev, netdev_priv(ndev));
967 ax_NS8390_init(ndev, ax->resume_open);
968 netif_device_attach(ndev);
970 if (ax->resume_open)
971 ax_open(ndev);
973 return 0;
976 #else
977 #define ax_suspend NULL
978 #define ax_resume NULL
979 #endif
981 static struct platform_driver axdrv = {
982 .driver = {
983 .name = "ax88796",
984 .owner = THIS_MODULE,
986 .probe = ax_probe,
987 .remove = ax_remove,
988 .suspend = ax_suspend,
989 .resume = ax_resume,
992 static int __init axdrv_init(void)
994 return platform_driver_register(&axdrv);
997 static void __exit axdrv_exit(void)
999 platform_driver_unregister(&axdrv);
1002 module_init(axdrv_init);
1003 module_exit(axdrv_exit);
1005 MODULE_DESCRIPTION("AX88796 10/100 Ethernet platform driver");
1006 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
1007 MODULE_LICENSE("GPL v2");