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