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