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