]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/net/ax88796.c
Merge git://git.infradead.org/battery-2.6
[linux-2.6-omap-h63xx.git] / drivers / net / ax88796.c
1 /* drivers/net/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
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>
28
29 #include <net/ax88796.h>
30
31 #include <asm/system.h>
32 #include <asm/io.h>
33
34 static int phy_debug = 0;
35
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
44
45 /* force unsigned long back to 'void __iomem *' */
46 #define ax_convert_addr(_a) ((void __force __iomem *)(_a))
47
48 #define ei_inb(_a)      readb(ax_convert_addr(_a))
49 #define ei_outb(_v, _a) writeb(_v, ax_convert_addr(_a))
50
51 #define ei_inb_p(_a)    ei_inb(_a)
52 #define ei_outb_p(_v, _a) ei_outb(_v, _a)
53
54 /* define EI_SHIFT() to take into account our register offsets */
55 #define EI_SHIFT(x)     (ei_local->reg_offset[(x)])
56
57 /* Ensure we have our RCR base value */
58 #define AX88796_PLATFORM
59
60 static unsigned char version[] = "ax88796.c: Copyright 2005,2007 Simtec Electronics\n";
61
62 #include "lib8390.c"
63
64 #define DRV_NAME "ax88796"
65 #define DRV_VERSION "1.00"
66
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)
71
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 */
76
77 /* device private data */
78
79 struct ax_device {
80         struct timer_list        mii_timer;
81         spinlock_t               mii_lock;
82         struct mii_if_info       mii;
83
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;
90
91         unsigned char            running;
92         unsigned char            resume_open;
93
94         u32                      reg_offsets[0x20];
95 };
96
97 static inline struct ax_device *to_ax_dev(struct net_device *dev)
98 {
99         struct ei_device *ei_local = netdev_priv(dev);
100         return (struct ax_device *)(ei_local+1);
101 }
102
103 /* ax_initial_check
104  *
105  * do an initial probe for the card to check wether it exists
106  * and is functional
107  */
108
109 static int ax_initial_check(struct net_device *dev)
110 {
111         struct ei_device *ei_local = netdev_priv(dev);
112         void __iomem *ioaddr = ei_local->mem;
113         int reg0;
114         int regd;
115
116         reg0 = ei_inb(ioaddr);
117         if (reg0 == 0xFF)
118                 return -ENODEV;
119
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;
129         }
130
131         return 0;
132 }
133
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. */
136
137 static void ax_reset_8390(struct net_device *dev)
138 {
139         struct ei_device *ei_local = netdev_priv(dev);
140         unsigned long reset_start_time = jiffies;
141         void __iomem *addr = (void __iomem *)dev->base_addr;
142
143         if (ei_debug > 1)
144                 printk(KERN_DEBUG "resetting the 8390 t=%ld...", jiffies);
145
146         ei_outb(ei_inb(addr + NE_RESET), addr + NE_RESET);
147
148         ei_status.txing = 0;
149         ei_status.dmaing = 0;
150
151         /* This check _should_not_ be necessary, omit eventually. */
152         while ((ei_inb(addr + EN0_ISR) & ENISR_RESET) == 0) {
153                 if (jiffies - reset_start_time > 2*HZ/100) {
154                         printk(KERN_WARNING "%s: %s did not complete.\n",
155                                __FUNCTION__, dev->name);
156                         break;
157                 }
158         }
159
160         ei_outb(ENISR_RESET, addr + EN0_ISR);   /* Ack intr. */
161 }
162
163
164 static void ax_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
165                             int ring_page)
166 {
167         struct ei_device *ei_local = netdev_priv(dev);
168         void __iomem *nic_base = ei_local->mem;
169
170         /* This *shouldn't* happen. If it does, it's the last thing you'll see */
171         if (ei_status.dmaing) {
172                 printk(KERN_EMERG "%s: DMAing conflict in %s [DMAstat:%d][irqlock:%d].\n",
173                         dev->name, __FUNCTION__,
174                        ei_status.dmaing, ei_status.irqlock);
175                 return;
176         }
177
178         ei_status.dmaing |= 0x01;
179         ei_outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
180         ei_outb(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
181         ei_outb(0, nic_base + EN0_RCNTHI);
182         ei_outb(0, nic_base + EN0_RSARLO);              /* On page boundary */
183         ei_outb(ring_page, nic_base + EN0_RSARHI);
184         ei_outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
185
186         if (ei_status.word16)
187                 readsw(nic_base + NE_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr)>>1);
188         else
189                 readsb(nic_base + NE_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr));
190
191         ei_outb(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
192         ei_status.dmaing &= ~0x01;
193
194         le16_to_cpus(&hdr->count);
195 }
196
197
198 /* Block input and output, similar to the Crynwr packet driver.  If you
199    are porting to a new ethercard, look at the packet driver source for hints.
200    The NEx000 doesn't share the on-board packet memory -- you have to put
201    the packet out through the "remote DMA" dataport using ei_outb. */
202
203 static void ax_block_input(struct net_device *dev, int count,
204                            struct sk_buff *skb, int ring_offset)
205 {
206         struct ei_device *ei_local = netdev_priv(dev);
207         void __iomem *nic_base = ei_local->mem;
208         char *buf = skb->data;
209
210         if (ei_status.dmaing) {
211                 printk(KERN_EMERG "%s: DMAing conflict in ax_block_input "
212                         "[DMAstat:%d][irqlock:%d].\n",
213                         dev->name, ei_status.dmaing, ei_status.irqlock);
214                 return;
215         }
216
217         ei_status.dmaing |= 0x01;
218
219         ei_outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD);
220         ei_outb(count & 0xff, nic_base + EN0_RCNTLO);
221         ei_outb(count >> 8, nic_base + EN0_RCNTHI);
222         ei_outb(ring_offset & 0xff, nic_base + EN0_RSARLO);
223         ei_outb(ring_offset >> 8, nic_base + EN0_RSARHI);
224         ei_outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
225
226         if (ei_status.word16) {
227                 readsw(nic_base + NE_DATAPORT, buf, count >> 1);
228                 if (count & 0x01)
229                         buf[count-1] = ei_inb(nic_base + NE_DATAPORT);
230
231         } else {
232                 readsb(nic_base + NE_DATAPORT, buf, count);
233         }
234
235         ei_status.dmaing &= ~1;
236 }
237
238 static void ax_block_output(struct net_device *dev, int count,
239                             const unsigned char *buf, const int start_page)
240 {
241         struct ei_device *ei_local = netdev_priv(dev);
242         void __iomem *nic_base = ei_local->mem;
243         unsigned long dma_start;
244
245         /* Round the count up for word writes.  Do we need to do this?
246            What effect will an odd byte count have on the 8390?
247            I should check someday. */
248
249         if (ei_status.word16 && (count & 0x01))
250                 count++;
251
252         /* This *shouldn't* happen. If it does, it's the last thing you'll see */
253         if (ei_status.dmaing) {
254                 printk(KERN_EMERG "%s: DMAing conflict in %s."
255                         "[DMAstat:%d][irqlock:%d]\n",
256                         dev->name, __FUNCTION__,
257                        ei_status.dmaing, ei_status.irqlock);
258                 return;
259         }
260
261         ei_status.dmaing |= 0x01;
262         /* We should already be in page 0, but to be safe... */
263         ei_outb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
264
265         ei_outb(ENISR_RDC, nic_base + EN0_ISR);
266
267         /* Now the normal output. */
268         ei_outb(count & 0xff, nic_base + EN0_RCNTLO);
269         ei_outb(count >> 8,   nic_base + EN0_RCNTHI);
270         ei_outb(0x00, nic_base + EN0_RSARLO);
271         ei_outb(start_page, nic_base + EN0_RSARHI);
272
273         ei_outb(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
274         if (ei_status.word16) {
275                 writesw(nic_base + NE_DATAPORT, buf, count>>1);
276         } else {
277                 writesb(nic_base + NE_DATAPORT, buf, count);
278         }
279
280         dma_start = jiffies;
281
282         while ((ei_inb(nic_base + EN0_ISR) & ENISR_RDC) == 0) {
283                 if (jiffies - dma_start > 2*HZ/100) {           /* 20ms */
284                         printk(KERN_WARNING "%s: timeout waiting for Tx RDC.\n", dev->name);
285                         ax_reset_8390(dev);
286                         ax_NS8390_init(dev,1);
287                         break;
288                 }
289         }
290
291         ei_outb(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
292         ei_status.dmaing &= ~0x01;
293         return;
294 }
295
296 /* definitions for accessing MII/EEPROM interface */
297
298 #define AX_MEMR                 EI_SHIFT(0x14)
299 #define AX_MEMR_MDC             (1<<0)
300 #define AX_MEMR_MDIR            (1<<1)
301 #define AX_MEMR_MDI             (1<<2)
302 #define AX_MEMR_MDO             (1<<3)
303 #define AX_MEMR_EECS            (1<<4)
304 #define AX_MEMR_EEI             (1<<5)
305 #define AX_MEMR_EEO             (1<<6)
306 #define AX_MEMR_EECLK           (1<<7)
307
308 /* ax_mii_ei_outbits
309  *
310  * write the specified set of bits to the phy
311 */
312
313 static void
314 ax_mii_ei_outbits(struct net_device *dev, unsigned int bits, int len)
315 {
316         struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
317         void __iomem *memr_addr = (void __iomem *)dev->base_addr + AX_MEMR;
318         unsigned int memr;
319
320         /* clock low, data to output mode */
321         memr = ei_inb(memr_addr);
322         memr &= ~(AX_MEMR_MDC | AX_MEMR_MDIR);
323         ei_outb(memr, memr_addr);
324
325         for (len--; len >= 0; len--) {
326                 if (bits & (1 << len))
327                         memr |= AX_MEMR_MDO;
328                 else
329                         memr &= ~AX_MEMR_MDO;
330
331                 ei_outb(memr, memr_addr);
332
333                 /* clock high */
334
335                 ei_outb(memr | AX_MEMR_MDC, memr_addr);
336                 udelay(1);
337
338                 /* clock low */
339                 ei_outb(memr, memr_addr);
340         }
341
342         /* leaves the clock line low, mdir input */
343         memr |= AX_MEMR_MDIR;
344         ei_outb(memr, (void __iomem *)dev->base_addr + AX_MEMR);
345 }
346
347 /* ax_phy_ei_inbits
348  *
349  * read a specified number of bits from the phy
350 */
351
352 static unsigned int
353 ax_phy_ei_inbits(struct net_device *dev, int no)
354 {
355         struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
356         void __iomem *memr_addr = (void __iomem *)dev->base_addr + AX_MEMR;
357         unsigned int memr;
358         unsigned int result = 0;
359
360         /* clock low, data to input mode */
361         memr = ei_inb(memr_addr);
362         memr &= ~AX_MEMR_MDC;
363         memr |= AX_MEMR_MDIR;
364         ei_outb(memr, memr_addr);
365
366         for (no--; no >= 0; no--) {
367                 ei_outb(memr | AX_MEMR_MDC, memr_addr);
368
369                 udelay(1);
370
371                 if (ei_inb(memr_addr) & AX_MEMR_MDI)
372                         result |= (1<<no);
373
374                 ei_outb(memr, memr_addr);
375         }
376
377         return result;
378 }
379
380 /* ax_phy_issueaddr
381  *
382  * use the low level bit shifting routines to send the address
383  * and command to the specified phy
384 */
385
386 static void
387 ax_phy_issueaddr(struct net_device *dev, int phy_addr, int reg, int opc)
388 {
389         if (phy_debug)
390                 pr_debug("%s: dev %p, %04x, %04x, %d\n",
391                         __FUNCTION__, dev, phy_addr, reg, opc);
392
393         ax_mii_ei_outbits(dev, 0x3f, 6);        /* pre-amble */
394         ax_mii_ei_outbits(dev, 1, 2);           /* frame-start */
395         ax_mii_ei_outbits(dev, opc, 2);         /* op code */
396         ax_mii_ei_outbits(dev, phy_addr, 5);    /* phy address */
397         ax_mii_ei_outbits(dev, reg, 5);         /* reg address */
398 }
399
400 static int
401 ax_phy_read(struct net_device *dev, int phy_addr, int reg)
402 {
403         struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev);
404         unsigned long flags;
405         unsigned int result;
406
407         spin_lock_irqsave(&ei_local->page_lock, flags);
408
409         ax_phy_issueaddr(dev, phy_addr, reg, 2);
410
411         result = ax_phy_ei_inbits(dev, 17);
412         result &= ~(3<<16);
413
414         spin_unlock_irqrestore(&ei_local->page_lock, flags);
415
416         if (phy_debug)
417                 pr_debug("%s: %04x.%04x => read %04x\n", __FUNCTION__,
418                          phy_addr, reg, result);
419
420         return result;
421 }
422
423 static void
424 ax_phy_write(struct net_device *dev, int phy_addr, int reg, int value)
425 {
426         struct ei_device *ei = (struct ei_device *) netdev_priv(dev);
427         unsigned long flags;
428
429         printk(KERN_DEBUG "%s: %p, %04x, %04x %04x\n",
430                __FUNCTION__, dev, phy_addr, reg, value);
431
432         spin_lock_irqsave(&ei->page_lock, flags);
433
434         ax_phy_issueaddr(dev, phy_addr, reg, 1);
435         ax_mii_ei_outbits(dev, 2, 2);           /* send TA */
436         ax_mii_ei_outbits(dev, value, 16);
437
438         spin_unlock_irqrestore(&ei->page_lock, flags);
439 }
440
441 static void ax_mii_expiry(unsigned long data)
442 {
443         struct net_device *dev = (struct net_device *)data;
444         struct ax_device  *ax = to_ax_dev(dev);
445         unsigned long flags;
446
447         spin_lock_irqsave(&ax->mii_lock, flags);
448         mii_check_media(&ax->mii, netif_msg_link(ax), 0);
449         spin_unlock_irqrestore(&ax->mii_lock, flags);
450
451         if (ax->running) {
452                 ax->mii_timer.expires = jiffies + HZ*2;
453                 add_timer(&ax->mii_timer);
454         }
455 }
456
457 static int ax_open(struct net_device *dev)
458 {
459         struct ax_device  *ax = to_ax_dev(dev);
460         struct ei_device *ei_local = netdev_priv(dev);
461         int ret;
462
463         dev_dbg(&ax->dev->dev, "%s: open\n", dev->name);
464
465         ret = request_irq(dev->irq, ax_ei_interrupt, 0, dev->name, dev);
466         if (ret)
467                 return ret;
468
469         ret = ax_ei_open(dev);
470         if (ret)
471                 return ret;
472
473         /* turn the phy on (if turned off) */
474
475         ei_outb(ax->plat->gpoc_val, ei_local->mem + EI_SHIFT(0x17));
476         ax->running = 1;
477
478         /* start the MII timer */
479
480         init_timer(&ax->mii_timer);
481
482         ax->mii_timer.expires  = jiffies+1;
483         ax->mii_timer.data     = (unsigned long) dev;
484         ax->mii_timer.function = ax_mii_expiry;
485
486         add_timer(&ax->mii_timer);
487
488         return 0;
489 }
490
491 static int ax_close(struct net_device *dev)
492 {
493         struct ax_device *ax = to_ax_dev(dev);
494         struct ei_device *ei_local = netdev_priv(dev);
495
496         dev_dbg(&ax->dev->dev, "%s: close\n", dev->name);
497
498         /* turn the phy off */
499
500         ei_outb(ax->plat->gpoc_val | (1<<6),
501                ei_local->mem + EI_SHIFT(0x17));
502
503         ax->running = 0;
504         wmb();
505
506         del_timer_sync(&ax->mii_timer);
507         ax_ei_close(dev);
508
509         free_irq(dev->irq, dev);
510         return 0;
511 }
512
513 static int ax_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
514 {
515         struct ax_device *ax = to_ax_dev(dev);
516         unsigned long flags;
517         int rc;
518
519         if (!netif_running(dev))
520                 return -EINVAL;
521
522         spin_lock_irqsave(&ax->mii_lock, flags);
523         rc = generic_mii_ioctl(&ax->mii, if_mii(req), cmd, NULL);
524         spin_unlock_irqrestore(&ax->mii_lock, flags);
525
526         return rc;
527 }
528
529 /* ethtool ops */
530
531 static void ax_get_drvinfo(struct net_device *dev,
532                            struct ethtool_drvinfo *info)
533 {
534         struct ax_device *ax = to_ax_dev(dev);
535
536         strcpy(info->driver, DRV_NAME);
537         strcpy(info->version, DRV_VERSION);
538         strcpy(info->bus_info, ax->dev->name);
539 }
540
541 static int ax_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
542 {
543         struct ax_device *ax = to_ax_dev(dev);
544         unsigned long flags;
545
546         spin_lock_irqsave(&ax->mii_lock, flags);
547         mii_ethtool_gset(&ax->mii, cmd);
548         spin_lock_irqsave(&ax->mii_lock, flags);
549
550         return 0;
551 }
552
553 static int ax_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
554 {
555         struct ax_device *ax = to_ax_dev(dev);
556         unsigned long flags;
557         int rc;
558
559         spin_lock_irqsave(&ax->mii_lock, flags);
560         rc = mii_ethtool_sset(&ax->mii, cmd);
561         spin_lock_irqsave(&ax->mii_lock, flags);
562
563         return rc;
564 }
565
566 static int ax_nway_reset(struct net_device *dev)
567 {
568         struct ax_device *ax = to_ax_dev(dev);
569         return mii_nway_restart(&ax->mii);
570 }
571
572 static u32 ax_get_link(struct net_device *dev)
573 {
574         struct ax_device *ax = to_ax_dev(dev);
575         return mii_link_ok(&ax->mii);
576 }
577
578 static const struct ethtool_ops ax_ethtool_ops = {
579         .get_drvinfo            = ax_get_drvinfo,
580         .get_settings           = ax_get_settings,
581         .set_settings           = ax_set_settings,
582         .nway_reset             = ax_nway_reset,
583         .get_link               = ax_get_link,
584 };
585
586 #ifdef CONFIG_AX88796_93CX6
587 static void ax_eeprom_register_read(struct eeprom_93cx6 *eeprom)
588 {
589         struct ei_device *ei_local = eeprom->data;
590         u8 reg = ei_inb(ei_local->mem + AX_MEMR);
591
592         eeprom->reg_data_in = reg & AX_MEMR_EEI;
593         eeprom->reg_data_out = reg & AX_MEMR_EEO; /* Input pin */
594         eeprom->reg_data_clock = reg & AX_MEMR_EECLK;
595         eeprom->reg_chip_select = reg & AX_MEMR_EECS;
596 }
597
598 static void ax_eeprom_register_write(struct eeprom_93cx6 *eeprom)
599 {
600         struct ei_device *ei_local = eeprom->data;
601         u8 reg = ei_inb(ei_local->mem + AX_MEMR);
602
603         reg &= ~(AX_MEMR_EEI | AX_MEMR_EECLK | AX_MEMR_EECS);
604
605         if (eeprom->reg_data_in)
606                 reg |= AX_MEMR_EEI;
607         if (eeprom->reg_data_clock)
608                 reg |= AX_MEMR_EECLK;
609         if (eeprom->reg_chip_select)
610                 reg |= AX_MEMR_EECS;
611
612         ei_outb(reg, ei_local->mem + AX_MEMR);
613         udelay(10);
614 }
615 #endif
616
617 /* setup code */
618
619 static void ax_initial_setup(struct net_device *dev, struct ei_device *ei_local)
620 {
621         void __iomem *ioaddr = ei_local->mem;
622         struct ax_device *ax = to_ax_dev(dev);
623
624         /* Select page 0*/
625         ei_outb(E8390_NODMA+E8390_PAGE0+E8390_STOP, ioaddr + E8390_CMD);
626
627         /* set to byte access */
628         ei_outb(ax->plat->dcr_val & ~1, ioaddr + EN0_DCFG);
629         ei_outb(ax->plat->gpoc_val, ioaddr + EI_SHIFT(0x17));
630 }
631
632 /* ax_init_dev
633  *
634  * initialise the specified device, taking care to note the MAC
635  * address it may already have (if configured), ensure
636  * the device is ready to be used by lib8390.c and registerd with
637  * the network layer.
638  */
639
640 static int ax_init_dev(struct net_device *dev, int first_init)
641 {
642         struct ei_device *ei_local = netdev_priv(dev);
643         struct ax_device *ax = to_ax_dev(dev);
644         void __iomem *ioaddr = ei_local->mem;
645         unsigned int start_page;
646         unsigned int stop_page;
647         int ret;
648         int i;
649
650         ret = ax_initial_check(dev);
651         if (ret)
652                 goto err_out;
653
654         /* setup goes here */
655
656         ax_initial_setup(dev, ei_local);
657
658         /* read the mac from the card prom if we need it */
659
660         if (first_init && ax->plat->flags & AXFLG_HAS_EEPROM) {
661                 unsigned char SA_prom[32];
662
663                 for(i = 0; i < sizeof(SA_prom); i+=2) {
664                         SA_prom[i] = ei_inb(ioaddr + NE_DATAPORT);
665                         SA_prom[i+1] = ei_inb(ioaddr + NE_DATAPORT);
666                 }
667
668                 if (ax->plat->wordlength == 2)
669                         for (i = 0; i < 16; i++)
670                                 SA_prom[i] = SA_prom[i+i];
671
672                 memcpy(dev->dev_addr,  SA_prom, 6);
673         }
674
675 #ifdef CONFIG_AX88796_93CX6
676         if (first_init && ax->plat->flags & AXFLG_HAS_93CX6) {
677                 unsigned char mac_addr[6];
678                 struct eeprom_93cx6 eeprom;
679
680                 eeprom.data = ei_local;
681                 eeprom.register_read = ax_eeprom_register_read;
682                 eeprom.register_write = ax_eeprom_register_write;
683                 eeprom.width = PCI_EEPROM_WIDTH_93C56;
684
685                 eeprom_93cx6_multiread(&eeprom, 0,
686                                        (__le16 __force *)mac_addr,
687                                        sizeof(mac_addr) >> 1);
688
689                 memcpy(dev->dev_addr,  mac_addr, 6);
690         }
691 #endif
692         if (ax->plat->wordlength == 2) {
693                 /* We must set the 8390 for word mode. */
694                 ei_outb(ax->plat->dcr_val, ei_local->mem + EN0_DCFG);
695                 start_page = NESM_START_PG;
696                 stop_page = NESM_STOP_PG;
697         } else {
698                 start_page = NE1SM_START_PG;
699                 stop_page = NE1SM_STOP_PG;
700         }
701
702         /* load the mac-address from the device if this is the
703          * first time we've initialised */
704
705         if (first_init && ax->plat->flags & AXFLG_MAC_FROMDEV) {
706                 ei_outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP,
707                         ei_local->mem + E8390_CMD); /* 0x61 */
708
709                 for (i = 0 ; i < ETHER_ADDR_LEN ; i++)
710                         dev->dev_addr[i] = ei_inb(ioaddr + EN1_PHYS_SHIFT(i));
711         }
712
713         ax_reset_8390(dev);
714
715         ei_status.name = "AX88796";
716         ei_status.tx_start_page = start_page;
717         ei_status.stop_page = stop_page;
718         ei_status.word16 = (ax->plat->wordlength == 2);
719         ei_status.rx_start_page = start_page + TX_PAGES;
720
721 #ifdef PACKETBUF_MEMSIZE
722          /* Allow the packet buffer size to be overridden by know-it-alls. */
723         ei_status.stop_page = ei_status.tx_start_page + PACKETBUF_MEMSIZE;
724 #endif
725
726         ei_status.reset_8390    = &ax_reset_8390;
727         ei_status.block_input   = &ax_block_input;
728         ei_status.block_output  = &ax_block_output;
729         ei_status.get_8390_hdr  = &ax_get_8390_hdr;
730         ei_status.priv = 0;
731
732         dev->open               = ax_open;
733         dev->stop               = ax_close;
734         dev->do_ioctl           = ax_ioctl;
735         dev->ethtool_ops        = &ax_ethtool_ops;
736
737         ax->msg_enable          = NETIF_MSG_LINK;
738         ax->mii.phy_id_mask     = 0x1f;
739         ax->mii.reg_num_mask    = 0x1f;
740         ax->mii.phy_id          = 0x10;         /* onboard phy */
741         ax->mii.force_media     = 0;
742         ax->mii.full_duplex     = 0;
743         ax->mii.mdio_read       = ax_phy_read;
744         ax->mii.mdio_write      = ax_phy_write;
745         ax->mii.dev             = dev;
746
747 #ifdef CONFIG_NET_POLL_CONTROLLER
748         dev->poll_controller = ax_ei_poll;
749 #endif
750         ax_NS8390_init(dev, 0);
751
752         if (first_init) {
753                 printk("AX88796: %dbit, irq %d, %lx, MAC: ",
754                        ei_status.word16 ? 16:8, dev->irq, dev->base_addr);
755
756                 for (i = 0; i < ETHER_ADDR_LEN; i++)
757                         printk("%2.2x%c", dev->dev_addr[i],
758                                (i < (ETHER_ADDR_LEN-1) ? ':' : ' '));
759
760                 printk("\n");
761         }
762
763         ret = register_netdev(dev);
764         if (ret)
765                 goto out_irq;
766
767         return 0;
768
769  out_irq:
770         /* cleanup irq */
771         free_irq(dev->irq, dev);
772  err_out:
773         return ret;
774 }
775
776 static int ax_remove(struct platform_device *_dev)
777 {
778         struct net_device *dev = platform_get_drvdata(_dev);
779         struct ax_device  *ax;
780
781         ax = to_ax_dev(dev);
782
783         unregister_netdev(dev);
784         free_irq(dev->irq, dev);
785
786         iounmap(ei_status.mem);
787         release_resource(ax->mem);
788         kfree(ax->mem);
789
790         if (ax->map2) {
791                 iounmap(ax->map2);
792                 release_resource(ax->mem2);
793                 kfree(ax->mem2);
794         }
795
796         free_netdev(dev);
797
798         return 0;
799 }
800
801 /* ax_probe
802  *
803  * This is the entry point when the platform device system uses to
804  * notify us of a new device to attach to. Allocate memory, find
805  * the resources and information passed, and map the necessary registers.
806 */
807
808 static int ax_probe(struct platform_device *pdev)
809 {
810         struct net_device *dev;
811         struct ax_device  *ax;
812         struct resource   *res;
813         size_t size;
814         int ret;
815
816         dev = ax__alloc_ei_netdev(sizeof(struct ax_device));
817         if (dev == NULL)
818                 return -ENOMEM;
819
820         /* ok, let's setup our device */
821         ax = to_ax_dev(dev);
822
823         memset(ax, 0, sizeof(struct ax_device));
824
825         spin_lock_init(&ax->mii_lock);
826
827         ax->dev = pdev;
828         ax->plat = pdev->dev.platform_data;
829         platform_set_drvdata(pdev, dev);
830
831         ei_status.rxcr_base  = ax->plat->rcr_val;
832
833         /* find the platform resources */
834
835         dev->irq  = platform_get_irq(pdev, 0);
836         if (dev->irq < 0) {
837                 dev_err(&pdev->dev, "no IRQ specified\n");
838                 ret = -ENXIO;
839                 goto exit_mem;
840         }
841
842         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
843         if (res == NULL) {
844                 dev_err(&pdev->dev, "no MEM specified\n");
845                 ret = -ENXIO;
846                 goto exit_mem;
847         }
848
849         size = (res->end - res->start) + 1;
850
851         /* setup the register offsets from either the platform data
852          * or by using the size of the resource provided */
853
854         if (ax->plat->reg_offsets)
855                 ei_status.reg_offset = ax->plat->reg_offsets;
856         else {
857                 ei_status.reg_offset = ax->reg_offsets;
858                 for (ret = 0; ret < 0x18; ret++)
859                         ax->reg_offsets[ret] = (size / 0x18) * ret;
860         }
861
862         ax->mem = request_mem_region(res->start, size, pdev->name);
863         if (ax->mem == NULL) {
864                 dev_err(&pdev->dev, "cannot reserve registers\n");
865                 ret = -ENXIO;
866                 goto exit_mem;
867         }
868
869         ei_status.mem = ioremap(res->start, size);
870         dev->base_addr = (unsigned long)ei_status.mem;
871
872         if (ei_status.mem == NULL) {
873                 dev_err(&pdev->dev, "Cannot ioremap area (%08llx,%08llx)\n",
874                         (unsigned long long)res->start,
875                         (unsigned long long)res->end);
876
877                 ret = -ENXIO;
878                 goto exit_req;
879         }
880
881         /* look for reset area */
882
883         res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
884         if (res == NULL) {
885                 if (!ax->plat->reg_offsets) {
886                         for (ret = 0; ret < 0x20; ret++)
887                                 ax->reg_offsets[ret] = (size / 0x20) * ret;
888                 }
889
890                 ax->map2 = NULL;
891         } else {
892                 size = (res->end - res->start) + 1;
893
894                 ax->mem2 = request_mem_region(res->start, size, pdev->name);
895                 if (ax->mem == NULL) {
896                         dev_err(&pdev->dev, "cannot reserve registers\n");
897                         ret = -ENXIO;
898                         goto exit_mem1;
899                 }
900
901                 ax->map2 = ioremap(res->start, size);
902                 if (ax->map2 == NULL) {
903                         dev_err(&pdev->dev, "cannot map reset register\n");
904                         ret = -ENXIO;
905                         goto exit_mem2;
906                 }
907
908                 ei_status.reg_offset[0x1f] = ax->map2 - ei_status.mem;
909         }
910
911         /* got resources, now initialise and register device */
912
913         ret = ax_init_dev(dev, 1);
914         if (!ret)
915                 return 0;
916
917         if (ax->map2 == NULL)
918                 goto exit_mem1;
919
920         iounmap(ax->map2);
921
922  exit_mem2:
923         release_resource(ax->mem2);
924         kfree(ax->mem2);
925
926  exit_mem1:
927         iounmap(ei_status.mem);
928
929  exit_req:
930         release_resource(ax->mem);
931         kfree(ax->mem);
932
933  exit_mem:
934         free_netdev(dev);
935
936         return ret;
937 }
938
939 /* suspend and resume */
940
941 #ifdef CONFIG_PM
942 static int ax_suspend(struct platform_device *dev, pm_message_t state)
943 {
944         struct net_device *ndev = platform_get_drvdata(dev);
945         struct ax_device  *ax = to_ax_dev(ndev);
946
947         ax->resume_open = ax->running;
948
949         netif_device_detach(ndev);
950         ax_close(ndev);
951
952         return 0;
953 }
954
955 static int ax_resume(struct platform_device *pdev)
956 {
957         struct net_device *ndev = platform_get_drvdata(pdev);
958         struct ax_device  *ax = to_ax_dev(ndev);
959
960         ax_initial_setup(ndev, netdev_priv(ndev));
961         ax_NS8390_init(ndev, ax->resume_open);
962         netif_device_attach(ndev);
963
964         if (ax->resume_open)
965                 ax_open(ndev);
966
967         return 0;
968 }
969
970 #else
971 #define ax_suspend NULL
972 #define ax_resume  NULL
973 #endif
974
975 static struct platform_driver axdrv = {
976         .driver = {
977                 .name           = "ax88796",
978                 .owner          = THIS_MODULE,
979         },
980         .probe          = ax_probe,
981         .remove         = ax_remove,
982         .suspend        = ax_suspend,
983         .resume         = ax_resume,
984 };
985
986 static int __init axdrv_init(void)
987 {
988         return platform_driver_register(&axdrv);
989 }
990
991 static void __exit axdrv_exit(void)
992 {
993         platform_driver_unregister(&axdrv);
994 }
995
996 module_init(axdrv_init);
997 module_exit(axdrv_exit);
998
999 MODULE_DESCRIPTION("AX88796 10/100 Ethernet platform driver");
1000 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
1001 MODULE_LICENSE("GPL v2");