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