]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/ax88796.c
drivers/net: Remove unnecessary casts of netdev_priv
[net-next-2.6.git] / drivers / net / ax88796.c
CommitLineData
825a2ff1
BD
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>
89e536a1 27#include <linux/eeprom_93cx6.h>
5a0e3ad6 28#include <linux/slab.h>
825a2ff1
BD
29
30#include <net/ax88796.h>
31
32#include <asm/system.h>
33#include <asm/io.h>
34
35static 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
d57bc36e 41#define __ei_start_xmit ax_ei_start_xmit
825a2ff1 42#define __ei_tx_timeout ax_ei_tx_timeout
d57bc36e
MD
43#define __ei_get_stats ax_ei_get_stats
44#define __ei_set_multicast_list ax_ei_set_multicast_list
825a2ff1
BD
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
64static 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
83struct 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;
47cb0355 97 unsigned int irqflags;
825a2ff1
BD
98
99 u32 reg_offsets[0x20];
100};
101
102static 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
114static 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
142static void ax_reset_8390(struct net_device *dev)
143{
144 struct ei_device *ei_local = netdev_priv(dev);
237c5e8e 145 struct ax_device *ax = to_ax_dev(dev);
825a2ff1
BD
146 unsigned long reset_start_time = jiffies;
147 void __iomem *addr = (void __iomem *)dev->base_addr;
148
149 if (ei_debug > 1)
237c5e8e 150 dev_dbg(&ax->dev->dev, "resetting the 8390 t=%ld\n", jiffies);
825a2ff1
BD
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) {
237c5e8e 160 dev_warn(&ax->dev->dev, "%s: %s did not complete.\n",
b39d66a8 161 __func__, dev->name);
825a2ff1
BD
162 break;
163 }
164 }
165
166 ei_outb(ENISR_RESET, addr + EN0_ISR); /* Ack intr. */
167}
168
169
170static 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);
237c5e8e 174 struct ax_device *ax = to_ax_dev(dev);
825a2ff1
BD
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) {
237c5e8e
BD
179 dev_err(&ax->dev->dev, "%s: DMAing conflict in %s "
180 "[DMAstat:%d][irqlock:%d].\n",
b39d66a8 181 dev->name, __func__,
237c5e8e 182 ei_status.dmaing, ei_status.irqlock);
825a2ff1
BD
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
211static 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);
237c5e8e 215 struct ax_device *ax = to_ax_dev(dev);
825a2ff1
BD
216 void __iomem *nic_base = ei_local->mem;
217 char *buf = skb->data;
218
219 if (ei_status.dmaing) {
237c5e8e
BD
220 dev_err(&ax->dev->dev,
221 "%s: DMAing conflict in %s "
825a2ff1 222 "[DMAstat:%d][irqlock:%d].\n",
b39d66a8 223 dev->name, __func__,
237c5e8e 224 ei_status.dmaing, ei_status.irqlock);
825a2ff1
BD
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
249static 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);
237c5e8e 253 struct ax_device *ax = to_ax_dev(dev);
825a2ff1
BD
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) {
237c5e8e 266 dev_err(&ax->dev->dev, "%s: DMAing conflict in %s."
825a2ff1 267 "[DMAstat:%d][irqlock:%d]\n",
b39d66a8 268 dev->name, __func__,
825a2ff1
BD
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 */
237c5e8e
BD
296 dev_warn(&ax->dev->dev,
297 "%s: timeout waiting for Tx RDC.\n", dev->name);
825a2ff1
BD
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;
825a2ff1
BD
306}
307
308/* definitions for accessing MII/EEPROM interface */
309
310#define AX_MEMR EI_SHIFT(0x14)
311#define AX_MEMR_MDC (1<<0)
312#define AX_MEMR_MDIR (1<<1)
313#define AX_MEMR_MDI (1<<2)
314#define AX_MEMR_MDO (1<<3)
315#define AX_MEMR_EECS (1<<4)
316#define AX_MEMR_EEI (1<<5)
317#define AX_MEMR_EEO (1<<6)
318#define AX_MEMR_EECLK (1<<7)
319
320/* ax_mii_ei_outbits
321 *
322 * write the specified set of bits to the phy
323*/
324
325static void
326ax_mii_ei_outbits(struct net_device *dev, unsigned int bits, int len)
327{
ece49153 328 struct ei_device *ei_local = netdev_priv(dev);
825a2ff1
BD
329 void __iomem *memr_addr = (void __iomem *)dev->base_addr + AX_MEMR;
330 unsigned int memr;
331
332 /* clock low, data to output mode */
333 memr = ei_inb(memr_addr);
334 memr &= ~(AX_MEMR_MDC | AX_MEMR_MDIR);
335 ei_outb(memr, memr_addr);
336
337 for (len--; len >= 0; len--) {
338 if (bits & (1 << len))
339 memr |= AX_MEMR_MDO;
340 else
341 memr &= ~AX_MEMR_MDO;
342
343 ei_outb(memr, memr_addr);
344
345 /* clock high */
346
347 ei_outb(memr | AX_MEMR_MDC, memr_addr);
348 udelay(1);
349
350 /* clock low */
351 ei_outb(memr, memr_addr);
352 }
353
354 /* leaves the clock line low, mdir input */
355 memr |= AX_MEMR_MDIR;
356 ei_outb(memr, (void __iomem *)dev->base_addr + AX_MEMR);
357}
358
359/* ax_phy_ei_inbits
360 *
361 * read a specified number of bits from the phy
362*/
363
364static unsigned int
365ax_phy_ei_inbits(struct net_device *dev, int no)
366{
ece49153 367 struct ei_device *ei_local = netdev_priv(dev);
825a2ff1
BD
368 void __iomem *memr_addr = (void __iomem *)dev->base_addr + AX_MEMR;
369 unsigned int memr;
370 unsigned int result = 0;
371
372 /* clock low, data to input mode */
373 memr = ei_inb(memr_addr);
374 memr &= ~AX_MEMR_MDC;
375 memr |= AX_MEMR_MDIR;
376 ei_outb(memr, memr_addr);
377
378 for (no--; no >= 0; no--) {
379 ei_outb(memr | AX_MEMR_MDC, memr_addr);
380
381 udelay(1);
382
383 if (ei_inb(memr_addr) & AX_MEMR_MDI)
384 result |= (1<<no);
385
386 ei_outb(memr, memr_addr);
387 }
388
389 return result;
390}
391
392/* ax_phy_issueaddr
393 *
394 * use the low level bit shifting routines to send the address
395 * and command to the specified phy
396*/
397
398static void
399ax_phy_issueaddr(struct net_device *dev, int phy_addr, int reg, int opc)
400{
401 if (phy_debug)
402 pr_debug("%s: dev %p, %04x, %04x, %d\n",
b39d66a8 403 __func__, dev, phy_addr, reg, opc);
825a2ff1
BD
404
405 ax_mii_ei_outbits(dev, 0x3f, 6); /* pre-amble */
406 ax_mii_ei_outbits(dev, 1, 2); /* frame-start */
407 ax_mii_ei_outbits(dev, opc, 2); /* op code */
408 ax_mii_ei_outbits(dev, phy_addr, 5); /* phy address */
409 ax_mii_ei_outbits(dev, reg, 5); /* reg address */
410}
411
412static int
413ax_phy_read(struct net_device *dev, int phy_addr, int reg)
414{
ece49153 415 struct ei_device *ei_local = netdev_priv(dev);
825a2ff1
BD
416 unsigned long flags;
417 unsigned int result;
418
419 spin_lock_irqsave(&ei_local->page_lock, flags);
420
421 ax_phy_issueaddr(dev, phy_addr, reg, 2);
422
423 result = ax_phy_ei_inbits(dev, 17);
424 result &= ~(3<<16);
425
426 spin_unlock_irqrestore(&ei_local->page_lock, flags);
427
428 if (phy_debug)
b39d66a8 429 pr_debug("%s: %04x.%04x => read %04x\n", __func__,
825a2ff1
BD
430 phy_addr, reg, result);
431
432 return result;
433}
434
435static void
436ax_phy_write(struct net_device *dev, int phy_addr, int reg, int value)
437{
ece49153 438 struct ei_device *ei = netdev_priv(dev);
237c5e8e 439 struct ax_device *ax = to_ax_dev(dev);
825a2ff1
BD
440 unsigned long flags;
441
237c5e8e 442 dev_dbg(&ax->dev->dev, "%s: %p, %04x, %04x %04x\n",
b39d66a8 443 __func__, dev, phy_addr, reg, value);
825a2ff1
BD
444
445 spin_lock_irqsave(&ei->page_lock, flags);
446
447 ax_phy_issueaddr(dev, phy_addr, reg, 1);
448 ax_mii_ei_outbits(dev, 2, 2); /* send TA */
449 ax_mii_ei_outbits(dev, value, 16);
450
451 spin_unlock_irqrestore(&ei->page_lock, flags);
452}
453
454static void ax_mii_expiry(unsigned long data)
455{
456 struct net_device *dev = (struct net_device *)data;
457 struct ax_device *ax = to_ax_dev(dev);
458 unsigned long flags;
459
460 spin_lock_irqsave(&ax->mii_lock, flags);
461 mii_check_media(&ax->mii, netif_msg_link(ax), 0);
462 spin_unlock_irqrestore(&ax->mii_lock, flags);
463
464 if (ax->running) {
465 ax->mii_timer.expires = jiffies + HZ*2;
466 add_timer(&ax->mii_timer);
467 }
468}
469
470static int ax_open(struct net_device *dev)
471{
472 struct ax_device *ax = to_ax_dev(dev);
473 struct ei_device *ei_local = netdev_priv(dev);
474 int ret;
475
2832e856 476 dev_dbg(&ax->dev->dev, "%s: open\n", dev->name);
825a2ff1 477
47cb0355
DM
478 ret = request_irq(dev->irq, ax_ei_interrupt, ax->irqflags,
479 dev->name, dev);
825a2ff1
BD
480 if (ret)
481 return ret;
482
483 ret = ax_ei_open(dev);
9f1e7582
KV
484 if (ret) {
485 free_irq(dev->irq, dev);
825a2ff1 486 return ret;
9f1e7582 487 }
825a2ff1
BD
488
489 /* turn the phy on (if turned off) */
490
491 ei_outb(ax->plat->gpoc_val, ei_local->mem + EI_SHIFT(0x17));
492 ax->running = 1;
493
494 /* start the MII timer */
495
496 init_timer(&ax->mii_timer);
497
498 ax->mii_timer.expires = jiffies+1;
499 ax->mii_timer.data = (unsigned long) dev;
500 ax->mii_timer.function = ax_mii_expiry;
501
502 add_timer(&ax->mii_timer);
503
504 return 0;
505}
506
507static int ax_close(struct net_device *dev)
508{
509 struct ax_device *ax = to_ax_dev(dev);
510 struct ei_device *ei_local = netdev_priv(dev);
511
2832e856 512 dev_dbg(&ax->dev->dev, "%s: close\n", dev->name);
825a2ff1
BD
513
514 /* turn the phy off */
515
516 ei_outb(ax->plat->gpoc_val | (1<<6),
517 ei_local->mem + EI_SHIFT(0x17));
518
519 ax->running = 0;
520 wmb();
521
522 del_timer_sync(&ax->mii_timer);
523 ax_ei_close(dev);
524
525 free_irq(dev->irq, dev);
526 return 0;
527}
528
529static int ax_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
530{
531 struct ax_device *ax = to_ax_dev(dev);
532 unsigned long flags;
533 int rc;
534
535 if (!netif_running(dev))
536 return -EINVAL;
537
538 spin_lock_irqsave(&ax->mii_lock, flags);
539 rc = generic_mii_ioctl(&ax->mii, if_mii(req), cmd, NULL);
540 spin_unlock_irqrestore(&ax->mii_lock, flags);
541
542 return rc;
543}
544
545/* ethtool ops */
546
547static void ax_get_drvinfo(struct net_device *dev,
548 struct ethtool_drvinfo *info)
549{
550 struct ax_device *ax = to_ax_dev(dev);
551
552 strcpy(info->driver, DRV_NAME);
553 strcpy(info->version, DRV_VERSION);
554 strcpy(info->bus_info, ax->dev->name);
555}
556
557static int ax_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
558{
559 struct ax_device *ax = to_ax_dev(dev);
560 unsigned long flags;
561
562 spin_lock_irqsave(&ax->mii_lock, flags);
563 mii_ethtool_gset(&ax->mii, cmd);
c0912585 564 spin_unlock_irqrestore(&ax->mii_lock, flags);
825a2ff1
BD
565
566 return 0;
567}
568
569static int ax_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
570{
571 struct ax_device *ax = to_ax_dev(dev);
572 unsigned long flags;
573 int rc;
574
575 spin_lock_irqsave(&ax->mii_lock, flags);
576 rc = mii_ethtool_sset(&ax->mii, cmd);
c0912585 577 spin_unlock_irqrestore(&ax->mii_lock, flags);
825a2ff1
BD
578
579 return rc;
580}
581
582static int ax_nway_reset(struct net_device *dev)
583{
584 struct ax_device *ax = to_ax_dev(dev);
585 return mii_nway_restart(&ax->mii);
586}
587
588static u32 ax_get_link(struct net_device *dev)
589{
590 struct ax_device *ax = to_ax_dev(dev);
591 return mii_link_ok(&ax->mii);
592}
593
594static const struct ethtool_ops ax_ethtool_ops = {
595 .get_drvinfo = ax_get_drvinfo,
596 .get_settings = ax_get_settings,
597 .set_settings = ax_set_settings,
598 .nway_reset = ax_nway_reset,
599 .get_link = ax_get_link,
825a2ff1
BD
600};
601
89e536a1
MD
602#ifdef CONFIG_AX88796_93CX6
603static void ax_eeprom_register_read(struct eeprom_93cx6 *eeprom)
604{
605 struct ei_device *ei_local = eeprom->data;
606 u8 reg = ei_inb(ei_local->mem + AX_MEMR);
607
608 eeprom->reg_data_in = reg & AX_MEMR_EEI;
609 eeprom->reg_data_out = reg & AX_MEMR_EEO; /* Input pin */
610 eeprom->reg_data_clock = reg & AX_MEMR_EECLK;
611 eeprom->reg_chip_select = reg & AX_MEMR_EECS;
612}
613
614static void ax_eeprom_register_write(struct eeprom_93cx6 *eeprom)
615{
616 struct ei_device *ei_local = eeprom->data;
617 u8 reg = ei_inb(ei_local->mem + AX_MEMR);
618
619 reg &= ~(AX_MEMR_EEI | AX_MEMR_EECLK | AX_MEMR_EECS);
620
621 if (eeprom->reg_data_in)
622 reg |= AX_MEMR_EEI;
623 if (eeprom->reg_data_clock)
624 reg |= AX_MEMR_EECLK;
625 if (eeprom->reg_chip_select)
626 reg |= AX_MEMR_EECS;
627
628 ei_outb(reg, ei_local->mem + AX_MEMR);
629 udelay(10);
630}
631#endif
632
d57bc36e
MD
633static const struct net_device_ops ax_netdev_ops = {
634 .ndo_open = ax_open,
635 .ndo_stop = ax_close,
636 .ndo_do_ioctl = ax_ioctl,
637
638 .ndo_start_xmit = ax_ei_start_xmit,
639 .ndo_tx_timeout = ax_ei_tx_timeout,
640 .ndo_get_stats = ax_ei_get_stats,
641 .ndo_set_multicast_list = ax_ei_set_multicast_list,
642 .ndo_validate_addr = eth_validate_addr,
643 .ndo_set_mac_address = eth_mac_addr,
644 .ndo_change_mtu = eth_change_mtu,
645#ifdef CONFIG_NET_POLL_CONTROLLER
646 .ndo_poll_controller = ax_ei_poll,
647#endif
648};
649
825a2ff1
BD
650/* setup code */
651
652static void ax_initial_setup(struct net_device *dev, struct ei_device *ei_local)
653{
654 void __iomem *ioaddr = ei_local->mem;
655 struct ax_device *ax = to_ax_dev(dev);
656
657 /* Select page 0*/
658 ei_outb(E8390_NODMA+E8390_PAGE0+E8390_STOP, ioaddr + E8390_CMD);
659
660 /* set to byte access */
661 ei_outb(ax->plat->dcr_val & ~1, ioaddr + EN0_DCFG);
662 ei_outb(ax->plat->gpoc_val, ioaddr + EI_SHIFT(0x17));
663}
664
665/* ax_init_dev
666 *
667 * initialise the specified device, taking care to note the MAC
668 * address it may already have (if configured), ensure
669 * the device is ready to be used by lib8390.c and registerd with
670 * the network layer.
671 */
672
673static int ax_init_dev(struct net_device *dev, int first_init)
674{
675 struct ei_device *ei_local = netdev_priv(dev);
676 struct ax_device *ax = to_ax_dev(dev);
677 void __iomem *ioaddr = ei_local->mem;
678 unsigned int start_page;
679 unsigned int stop_page;
680 int ret;
681 int i;
682
683 ret = ax_initial_check(dev);
684 if (ret)
685 goto err_out;
686
687 /* setup goes here */
688
689 ax_initial_setup(dev, ei_local);
690
691 /* read the mac from the card prom if we need it */
692
693 if (first_init && ax->plat->flags & AXFLG_HAS_EEPROM) {
694 unsigned char SA_prom[32];
695
696 for(i = 0; i < sizeof(SA_prom); i+=2) {
697 SA_prom[i] = ei_inb(ioaddr + NE_DATAPORT);
698 SA_prom[i+1] = ei_inb(ioaddr + NE_DATAPORT);
699 }
700
701 if (ax->plat->wordlength == 2)
702 for (i = 0; i < 16; i++)
703 SA_prom[i] = SA_prom[i+i];
704
705 memcpy(dev->dev_addr, SA_prom, 6);
706 }
707
89e536a1
MD
708#ifdef CONFIG_AX88796_93CX6
709 if (first_init && ax->plat->flags & AXFLG_HAS_93CX6) {
710 unsigned char mac_addr[6];
711 struct eeprom_93cx6 eeprom;
712
713 eeprom.data = ei_local;
714 eeprom.register_read = ax_eeprom_register_read;
715 eeprom.register_write = ax_eeprom_register_write;
716 eeprom.width = PCI_EEPROM_WIDTH_93C56;
717
718 eeprom_93cx6_multiread(&eeprom, 0,
719 (__le16 __force *)mac_addr,
720 sizeof(mac_addr) >> 1);
721
722 memcpy(dev->dev_addr, mac_addr, 6);
723 }
724#endif
825a2ff1
BD
725 if (ax->plat->wordlength == 2) {
726 /* We must set the 8390 for word mode. */
727 ei_outb(ax->plat->dcr_val, ei_local->mem + EN0_DCFG);
728 start_page = NESM_START_PG;
729 stop_page = NESM_STOP_PG;
730 } else {
731 start_page = NE1SM_START_PG;
732 stop_page = NE1SM_STOP_PG;
733 }
734
735 /* load the mac-address from the device if this is the
736 * first time we've initialised */
737
67fca028
DM
738 if (first_init) {
739 if (ax->plat->flags & AXFLG_MAC_FROMDEV) {
740 ei_outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP,
741 ei_local->mem + E8390_CMD); /* 0x61 */
742 for (i = 0; i < ETHER_ADDR_LEN; i++)
743 dev->dev_addr[i] =
744 ei_inb(ioaddr + EN1_PHYS_SHIFT(i));
745 }
825a2ff1 746
67fca028
DM
747 if ((ax->plat->flags & AXFLG_MAC_FROMPLATFORM) &&
748 ax->plat->mac_addr)
749 memcpy(dev->dev_addr, ax->plat->mac_addr,
750 ETHER_ADDR_LEN);
825a2ff1
BD
751 }
752
753 ax_reset_8390(dev);
754
755 ei_status.name = "AX88796";
756 ei_status.tx_start_page = start_page;
757 ei_status.stop_page = stop_page;
758 ei_status.word16 = (ax->plat->wordlength == 2);
759 ei_status.rx_start_page = start_page + TX_PAGES;
760
761#ifdef PACKETBUF_MEMSIZE
762 /* Allow the packet buffer size to be overridden by know-it-alls. */
763 ei_status.stop_page = ei_status.tx_start_page + PACKETBUF_MEMSIZE;
764#endif
765
766 ei_status.reset_8390 = &ax_reset_8390;
767 ei_status.block_input = &ax_block_input;
768 ei_status.block_output = &ax_block_output;
769 ei_status.get_8390_hdr = &ax_get_8390_hdr;
770 ei_status.priv = 0;
771
d57bc36e 772 dev->netdev_ops = &ax_netdev_ops;
825a2ff1
BD
773 dev->ethtool_ops = &ax_ethtool_ops;
774
775 ax->msg_enable = NETIF_MSG_LINK;
776 ax->mii.phy_id_mask = 0x1f;
777 ax->mii.reg_num_mask = 0x1f;
778 ax->mii.phy_id = 0x10; /* onboard phy */
779 ax->mii.force_media = 0;
780 ax->mii.full_duplex = 0;
781 ax->mii.mdio_read = ax_phy_read;
782 ax->mii.mdio_write = ax_phy_write;
783 ax->mii.dev = dev;
784
825a2ff1
BD
785 ax_NS8390_init(dev, 0);
786
e174961c
JB
787 if (first_init)
788 dev_info(&ax->dev->dev, "%dbit, irq %d, %lx, MAC: %pM\n",
237c5e8e 789 ei_status.word16 ? 16:8, dev->irq, dev->base_addr,
e174961c 790 dev->dev_addr);
825a2ff1
BD
791
792 ret = register_netdev(dev);
793 if (ret)
794 goto out_irq;
795
796 return 0;
797
798 out_irq:
799 /* cleanup irq */
800 free_irq(dev->irq, dev);
801 err_out:
802 return ret;
803}
804
805static int ax_remove(struct platform_device *_dev)
806{
807 struct net_device *dev = platform_get_drvdata(_dev);
808 struct ax_device *ax;
809
810 ax = to_ax_dev(dev);
811
812 unregister_netdev(dev);
813 free_irq(dev->irq, dev);
814
815 iounmap(ei_status.mem);
816 release_resource(ax->mem);
817 kfree(ax->mem);
818
819 if (ax->map2) {
820 iounmap(ax->map2);
821 release_resource(ax->mem2);
822 kfree(ax->mem2);
823 }
824
825 free_netdev(dev);
826
827 return 0;
828}
829
830/* ax_probe
831 *
832 * This is the entry point when the platform device system uses to
833 * notify us of a new device to attach to. Allocate memory, find
834 * the resources and information passed, and map the necessary registers.
835*/
836
837static int ax_probe(struct platform_device *pdev)
838{
839 struct net_device *dev;
840 struct ax_device *ax;
841 struct resource *res;
842 size_t size;
47cb0355 843 int ret = 0;
825a2ff1
BD
844
845 dev = ax__alloc_ei_netdev(sizeof(struct ax_device));
846 if (dev == NULL)
847 return -ENOMEM;
848
849 /* ok, let's setup our device */
850 ax = to_ax_dev(dev);
851
852 memset(ax, 0, sizeof(struct ax_device));
853
854 spin_lock_init(&ax->mii_lock);
855
856 ax->dev = pdev;
857 ax->plat = pdev->dev.platform_data;
858 platform_set_drvdata(pdev, dev);
859
860 ei_status.rxcr_base = ax->plat->rcr_val;
861
862 /* find the platform resources */
863
47cb0355
DM
864 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
865 if (res == NULL) {
825a2ff1 866 dev_err(&pdev->dev, "no IRQ specified\n");
13eea192 867 ret = -ENXIO;
825a2ff1
BD
868 goto exit_mem;
869 }
47cb0355
DM
870
871 dev->irq = res->start;
872 ax->irqflags = res->flags & IRQF_TRIGGER_MASK;
825a2ff1
BD
873
874 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
875 if (res == NULL) {
876 dev_err(&pdev->dev, "no MEM specified\n");
877 ret = -ENXIO;
878 goto exit_mem;
879 }
880
881 size = (res->end - res->start) + 1;
882
883 /* setup the register offsets from either the platform data
884 * or by using the size of the resource provided */
885
886 if (ax->plat->reg_offsets)
887 ei_status.reg_offset = ax->plat->reg_offsets;
888 else {
889 ei_status.reg_offset = ax->reg_offsets;
890 for (ret = 0; ret < 0x18; ret++)
891 ax->reg_offsets[ret] = (size / 0x18) * ret;
892 }
893
894 ax->mem = 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_mem;
899 }
900
901 ei_status.mem = ioremap(res->start, size);
c7b17cb1 902 dev->base_addr = (unsigned long)ei_status.mem;
825a2ff1
BD
903
904 if (ei_status.mem == NULL) {
b4efe22c
AM
905 dev_err(&pdev->dev, "Cannot ioremap area (%08llx,%08llx)\n",
906 (unsigned long long)res->start,
907 (unsigned long long)res->end);
825a2ff1
BD
908
909 ret = -ENXIO;
910 goto exit_req;
911 }
912
913 /* look for reset area */
914
915 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
916 if (res == NULL) {
917 if (!ax->plat->reg_offsets) {
918 for (ret = 0; ret < 0x20; ret++)
919 ax->reg_offsets[ret] = (size / 0x20) * ret;
920 }
921
922 ax->map2 = NULL;
923 } else {
924 size = (res->end - res->start) + 1;
925
926 ax->mem2 = request_mem_region(res->start, size, pdev->name);
bcf4d812 927 if (ax->mem2 == NULL) {
825a2ff1
BD
928 dev_err(&pdev->dev, "cannot reserve registers\n");
929 ret = -ENXIO;
930 goto exit_mem1;
931 }
932
933 ax->map2 = ioremap(res->start, size);
934 if (ax->map2 == NULL) {
898eb71c 935 dev_err(&pdev->dev, "cannot map reset register\n");
825a2ff1
BD
936 ret = -ENXIO;
937 goto exit_mem2;
938 }
939
940 ei_status.reg_offset[0x1f] = ax->map2 - ei_status.mem;
941 }
942
943 /* got resources, now initialise and register device */
944
945 ret = ax_init_dev(dev, 1);
946 if (!ret)
947 return 0;
948
949 if (ax->map2 == NULL)
950 goto exit_mem1;
951
952 iounmap(ax->map2);
953
954 exit_mem2:
955 release_resource(ax->mem2);
956 kfree(ax->mem2);
957
958 exit_mem1:
959 iounmap(ei_status.mem);
960
961 exit_req:
962 release_resource(ax->mem);
963 kfree(ax->mem);
964
965 exit_mem:
966 free_netdev(dev);
967
968 return ret;
969}
970
971/* suspend and resume */
972
973#ifdef CONFIG_PM
974static int ax_suspend(struct platform_device *dev, pm_message_t state)
975{
976 struct net_device *ndev = platform_get_drvdata(dev);
977 struct ax_device *ax = to_ax_dev(ndev);
978
979 ax->resume_open = ax->running;
980
981 netif_device_detach(ndev);
982 ax_close(ndev);
983
984 return 0;
985}
986
987static int ax_resume(struct platform_device *pdev)
988{
989 struct net_device *ndev = platform_get_drvdata(pdev);
990 struct ax_device *ax = to_ax_dev(ndev);
991
992 ax_initial_setup(ndev, netdev_priv(ndev));
993 ax_NS8390_init(ndev, ax->resume_open);
994 netif_device_attach(ndev);
995
996 if (ax->resume_open)
997 ax_open(ndev);
998
999 return 0;
1000}
1001
1002#else
1003#define ax_suspend NULL
1004#define ax_resume NULL
1005#endif
1006
1007static struct platform_driver axdrv = {
1008 .driver = {
1009 .name = "ax88796",
1010 .owner = THIS_MODULE,
1011 },
1012 .probe = ax_probe,
1013 .remove = ax_remove,
1014 .suspend = ax_suspend,
1015 .resume = ax_resume,
1016};
1017
1018static int __init axdrv_init(void)
1019{
1020 return platform_driver_register(&axdrv);
1021}
1022
1023static void __exit axdrv_exit(void)
1024{
1025 platform_driver_unregister(&axdrv);
1026}
1027
1028module_init(axdrv_init);
1029module_exit(axdrv_exit);
1030
1031MODULE_DESCRIPTION("AX88796 10/100 Ethernet platform driver");
1032MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
1033MODULE_LICENSE("GPL v2");
72abb461 1034MODULE_ALIAS("platform:ax88796");