]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/ethernet/8390/ax88796.c
Merge tag 'at91-ab-4.13-soc' of git://git.kernel.org/pub/scm/linux/kernel/git/abellon...
[mirror_ubuntu-bionic-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;
f6d7f2c6
MKB
87 void __iomem *addr_memr;
88 u8 reg_memr;
89 int link;
90 int speed;
91 int duplex;
9f072429 92
9f072429 93 void __iomem *map2;
a54dc585 94 const struct ax_plat_data *plat;
9f072429
MKB
95
96 unsigned char running;
97 unsigned char resume_open;
98 unsigned int irqflags;
99
100 u32 reg_offsets[0x20];
825a2ff1
BD
101};
102
103static inline struct ax_device *to_ax_dev(struct net_device *dev)
104{
105 struct ei_device *ei_local = netdev_priv(dev);
9f072429 106 return (struct ax_device *)(ei_local + 1);
825a2ff1
BD
107}
108
9f072429
MKB
109/*
110 * ax_initial_check
825a2ff1 111 *
48fc7f7e 112 * do an initial probe for the card to check whether it exists
825a2ff1
BD
113 * and is functional
114 */
825a2ff1
BD
115static int ax_initial_check(struct net_device *dev)
116{
117 struct ei_device *ei_local = netdev_priv(dev);
118 void __iomem *ioaddr = ei_local->mem;
119 int reg0;
120 int regd;
121
122 reg0 = ei_inb(ioaddr);
123 if (reg0 == 0xFF)
124 return -ENODEV;
125
9f072429 126 ei_outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP, ioaddr + E8390_CMD);
825a2ff1
BD
127 regd = ei_inb(ioaddr + 0x0d);
128 ei_outb(0xff, ioaddr + 0x0d);
9f072429 129 ei_outb(E8390_NODMA + E8390_PAGE0, ioaddr + E8390_CMD);
825a2ff1
BD
130 ei_inb(ioaddr + EN0_COUNTER0); /* Clear the counter by reading. */
131 if (ei_inb(ioaddr + EN0_COUNTER0) != 0) {
132 ei_outb(reg0, ioaddr);
133 ei_outb(regd, ioaddr + 0x0d); /* Restore the old values. */
134 return -ENODEV;
135 }
136
137 return 0;
138}
139
9f072429
MKB
140/*
141 * Hard reset the card. This used to pause for the same period that a
142 * 8390 reset command required, but that shouldn't be necessary.
143 */
825a2ff1
BD
144static void ax_reset_8390(struct net_device *dev)
145{
146 struct ei_device *ei_local = netdev_priv(dev);
147 unsigned long reset_start_time = jiffies;
148 void __iomem *addr = (void __iomem *)dev->base_addr;
149
c45f812f 150 netif_dbg(ei_local, hw, 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) {
03b8dcba 159 if (time_after(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)
b146ecd6
ML
193 ioread16_rep(nic_base + NE_DATAPORT, hdr,
194 sizeof(struct e8390_pkt_hdr) >> 1);
825a2ff1 195 else
b146ecd6
ML
196 ioread8_rep(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) {
b146ecd6 239 ioread16_rep(nic_base + NE_DATAPORT, buf, count >> 1);
825a2ff1
BD
240 if (count & 0x01)
241 buf[count-1] = ei_inb(nic_base + NE_DATAPORT);
242
243 } else {
b146ecd6 244 ioread8_rep(nic_base + NE_DATAPORT, buf, count);
825a2ff1
BD
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)
b146ecd6 288 iowrite16_rep(nic_base + NE_DATAPORT, buf, count >> 1);
9f072429 289 else
b146ecd6 290 iowrite8_rep(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) {
03b8dcba 295 if (time_after(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
f6d7f2c6 319static void ax_handle_link_change(struct net_device *dev)
825a2ff1 320{
f6d7f2c6 321 struct ax_device *ax = to_ax_dev(dev);
de0eabf8 322 struct phy_device *phy_dev = dev->phydev;
f6d7f2c6 323 int status_change = 0;
825a2ff1 324
f6d7f2c6
MKB
325 if (phy_dev->link && ((ax->speed != phy_dev->speed) ||
326 (ax->duplex != phy_dev->duplex))) {
825a2ff1 327
f6d7f2c6
MKB
328 ax->speed = phy_dev->speed;
329 ax->duplex = phy_dev->duplex;
330 status_change = 1;
825a2ff1
BD
331 }
332
f6d7f2c6
MKB
333 if (phy_dev->link != ax->link) {
334 if (!phy_dev->link) {
335 ax->speed = 0;
336 ax->duplex = -1;
337 }
338 ax->link = phy_dev->link;
825a2ff1 339
f6d7f2c6 340 status_change = 1;
825a2ff1
BD
341 }
342
f6d7f2c6
MKB
343 if (status_change)
344 phy_print_status(phy_dev);
825a2ff1
BD
345}
346
f6d7f2c6 347static int ax_mii_probe(struct net_device *dev)
825a2ff1 348{
f6d7f2c6
MKB
349 struct ax_device *ax = to_ax_dev(dev);
350 struct phy_device *phy_dev = NULL;
351 int ret;
825a2ff1 352
f6d7f2c6
MKB
353 /* find the first phy */
354 phy_dev = phy_find_first(ax->mii_bus);
355 if (!phy_dev) {
356 netdev_err(dev, "no PHY found\n");
357 return -ENODEV;
358 }
825a2ff1 359
f9a8f83b 360 ret = phy_connect_direct(dev, phy_dev, ax_handle_link_change,
f6d7f2c6
MKB
361 PHY_INTERFACE_MODE_MII);
362 if (ret) {
363 netdev_err(dev, "Could not attach to PHY\n");
364 return ret;
365 }
825a2ff1 366
f6d7f2c6
MKB
367 /* mask with MAC supported features */
368 phy_dev->supported &= PHY_BASIC_FEATURES;
369 phy_dev->advertising = phy_dev->supported;
825a2ff1 370
f6d7f2c6 371 netdev_info(dev, "PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
84eff6d1 372 phy_dev->drv->name, phydev_name(phy_dev), phy_dev->irq);
825a2ff1 373
f6d7f2c6 374 return 0;
825a2ff1
BD
375}
376
f6d7f2c6 377static void ax_phy_switch(struct net_device *dev, int on)
825a2ff1 378{
f6d7f2c6 379 struct ei_device *ei_local = netdev_priv(dev);
9f072429 380 struct ax_device *ax = to_ax_dev(dev);
825a2ff1 381
f6d7f2c6 382 u8 reg_gpoc = ax->plat->gpoc_val;
825a2ff1 383
f6d7f2c6
MKB
384 if (!!on)
385 reg_gpoc &= ~AX_GPOC_PPDSET;
386 else
387 reg_gpoc |= AX_GPOC_PPDSET;
388
389 ei_outb(reg_gpoc, ei_local->mem + EI_SHIFT(0x17));
825a2ff1
BD
390}
391
392static int ax_open(struct net_device *dev)
393{
9f072429 394 struct ax_device *ax = to_ax_dev(dev);
825a2ff1
BD
395 int ret;
396
85ac6162 397 netdev_dbg(dev, "open\n");
825a2ff1 398
47cb0355
DM
399 ret = request_irq(dev->irq, ax_ei_interrupt, ax->irqflags,
400 dev->name, dev);
825a2ff1 401 if (ret)
f6d7f2c6 402 goto failed_request_irq;
825a2ff1
BD
403
404 /* turn the phy on (if turned off) */
f6d7f2c6 405 ax_phy_switch(dev, 1);
825a2ff1 406
f6d7f2c6
MKB
407 ret = ax_mii_probe(dev);
408 if (ret)
409 goto failed_mii_probe;
de0eabf8 410 phy_start(dev->phydev);
825a2ff1 411
f6d7f2c6
MKB
412 ret = ax_ei_open(dev);
413 if (ret)
414 goto failed_ax_ei_open;
825a2ff1 415
f6d7f2c6 416 ax->running = 1;
825a2ff1
BD
417
418 return 0;
f6d7f2c6
MKB
419
420 failed_ax_ei_open:
de0eabf8 421 phy_disconnect(dev->phydev);
f6d7f2c6
MKB
422 failed_mii_probe:
423 ax_phy_switch(dev, 0);
424 free_irq(dev->irq, dev);
425 failed_request_irq:
426 return ret;
825a2ff1
BD
427}
428
429static int ax_close(struct net_device *dev)
430{
431 struct ax_device *ax = to_ax_dev(dev);
825a2ff1 432
85ac6162 433 netdev_dbg(dev, "close\n");
825a2ff1 434
825a2ff1
BD
435 ax->running = 0;
436 wmb();
437
825a2ff1
BD
438 ax_ei_close(dev);
439
f6d7f2c6
MKB
440 /* turn the phy off */
441 ax_phy_switch(dev, 0);
de0eabf8 442 phy_disconnect(dev->phydev);
f6d7f2c6 443
825a2ff1
BD
444 free_irq(dev->irq, dev);
445 return 0;
446}
447
448static int ax_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
449{
de0eabf8 450 struct phy_device *phy_dev = dev->phydev;
825a2ff1
BD
451
452 if (!netif_running(dev))
453 return -EINVAL;
454
f6d7f2c6
MKB
455 if (!phy_dev)
456 return -ENODEV;
825a2ff1 457
f6d7f2c6 458 return phy_mii_ioctl(phy_dev, req, cmd);
825a2ff1
BD
459}
460
461/* ethtool ops */
462
463static void ax_get_drvinfo(struct net_device *dev,
464 struct ethtool_drvinfo *info)
465{
0f442f5a 466 struct platform_device *pdev = to_platform_device(dev->dev.parent);
825a2ff1 467
7826d43f
JP
468 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
469 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
470 strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
825a2ff1
BD
471}
472
c45f812f
MW
473static u32 ax_get_msglevel(struct net_device *dev)
474{
475 struct ei_device *ei_local = netdev_priv(dev);
476
477 return ei_local->msg_enable;
478}
479
480static void ax_set_msglevel(struct net_device *dev, u32 v)
481{
482 struct ei_device *ei_local = netdev_priv(dev);
483
484 ei_local->msg_enable = v;
485}
486
825a2ff1
BD
487static const struct ethtool_ops ax_ethtool_ops = {
488 .get_drvinfo = ax_get_drvinfo,
f6d7f2c6 489 .get_link = ethtool_op_get_link,
2241b67b 490 .get_ts_info = ethtool_op_get_ts_info,
c45f812f
MW
491 .get_msglevel = ax_get_msglevel,
492 .set_msglevel = ax_set_msglevel,
d21cfb37
PR
493 .get_link_ksettings = phy_ethtool_get_link_ksettings,
494 .set_link_ksettings = phy_ethtool_set_link_ksettings,
825a2ff1
BD
495};
496
89e536a1
MD
497#ifdef CONFIG_AX88796_93CX6
498static void ax_eeprom_register_read(struct eeprom_93cx6 *eeprom)
499{
500 struct ei_device *ei_local = eeprom->data;
501 u8 reg = ei_inb(ei_local->mem + AX_MEMR);
502
503 eeprom->reg_data_in = reg & AX_MEMR_EEI;
504 eeprom->reg_data_out = reg & AX_MEMR_EEO; /* Input pin */
505 eeprom->reg_data_clock = reg & AX_MEMR_EECLK;
506 eeprom->reg_chip_select = reg & AX_MEMR_EECS;
507}
508
509static void ax_eeprom_register_write(struct eeprom_93cx6 *eeprom)
510{
511 struct ei_device *ei_local = eeprom->data;
512 u8 reg = ei_inb(ei_local->mem + AX_MEMR);
513
514 reg &= ~(AX_MEMR_EEI | AX_MEMR_EECLK | AX_MEMR_EECS);
515
516 if (eeprom->reg_data_in)
517 reg |= AX_MEMR_EEI;
518 if (eeprom->reg_data_clock)
519 reg |= AX_MEMR_EECLK;
520 if (eeprom->reg_chip_select)
521 reg |= AX_MEMR_EECS;
522
523 ei_outb(reg, ei_local->mem + AX_MEMR);
524 udelay(10);
525}
526#endif
527
d57bc36e
MD
528static const struct net_device_ops ax_netdev_ops = {
529 .ndo_open = ax_open,
530 .ndo_stop = ax_close,
531 .ndo_do_ioctl = ax_ioctl,
532
533 .ndo_start_xmit = ax_ei_start_xmit,
534 .ndo_tx_timeout = ax_ei_tx_timeout,
535 .ndo_get_stats = ax_ei_get_stats,
afc4b13d 536 .ndo_set_rx_mode = ax_ei_set_multicast_list,
d57bc36e 537 .ndo_validate_addr = eth_validate_addr,
9f072429 538 .ndo_set_mac_address = eth_mac_addr,
d57bc36e
MD
539#ifdef CONFIG_NET_POLL_CONTROLLER
540 .ndo_poll_controller = ax_ei_poll,
541#endif
542};
543
f6d7f2c6
MKB
544static void ax_bb_mdc(struct mdiobb_ctrl *ctrl, int level)
545{
546 struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
547
548 if (level)
549 ax->reg_memr |= AX_MEMR_MDC;
550 else
551 ax->reg_memr &= ~AX_MEMR_MDC;
552
553 ei_outb(ax->reg_memr, ax->addr_memr);
554}
555
556static void ax_bb_dir(struct mdiobb_ctrl *ctrl, int output)
557{
558 struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
559
560 if (output)
561 ax->reg_memr &= ~AX_MEMR_MDIR;
562 else
563 ax->reg_memr |= AX_MEMR_MDIR;
564
565 ei_outb(ax->reg_memr, ax->addr_memr);
566}
567
568static void ax_bb_set_data(struct mdiobb_ctrl *ctrl, int value)
569{
570 struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
571
572 if (value)
573 ax->reg_memr |= AX_MEMR_MDO;
574 else
575 ax->reg_memr &= ~AX_MEMR_MDO;
576
577 ei_outb(ax->reg_memr, ax->addr_memr);
578}
579
580static int ax_bb_get_data(struct mdiobb_ctrl *ctrl)
581{
582 struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
583 int reg_memr = ei_inb(ax->addr_memr);
584
585 return reg_memr & AX_MEMR_MDI ? 1 : 0;
586}
587
588static struct mdiobb_ops bb_ops = {
589 .owner = THIS_MODULE,
590 .set_mdc = ax_bb_mdc,
591 .set_mdio_dir = ax_bb_dir,
592 .set_mdio_data = ax_bb_set_data,
593 .get_mdio_data = ax_bb_get_data,
594};
595
825a2ff1
BD
596/* setup code */
597
f6d7f2c6
MKB
598static int ax_mii_init(struct net_device *dev)
599{
600 struct platform_device *pdev = to_platform_device(dev->dev.parent);
601 struct ei_device *ei_local = netdev_priv(dev);
602 struct ax_device *ax = to_ax_dev(dev);
e7f4dc35 603 int err;
f6d7f2c6
MKB
604
605 ax->bb_ctrl.ops = &bb_ops;
606 ax->addr_memr = ei_local->mem + AX_MEMR;
607 ax->mii_bus = alloc_mdio_bitbang(&ax->bb_ctrl);
608 if (!ax->mii_bus) {
609 err = -ENOMEM;
610 goto out;
611 }
612
613 ax->mii_bus->name = "ax88796_mii_bus";
614 ax->mii_bus->parent = dev->dev.parent;
fa84e720
FF
615 snprintf(ax->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
616 pdev->name, pdev->id);
f6d7f2c6 617
f6d7f2c6
MKB
618 err = mdiobus_register(ax->mii_bus);
619 if (err)
e7f4dc35 620 goto out_free_mdio_bitbang;
f6d7f2c6
MKB
621
622 return 0;
623
f6d7f2c6
MKB
624 out_free_mdio_bitbang:
625 free_mdio_bitbang(ax->mii_bus);
626 out:
627 return err;
628}
629
825a2ff1
BD
630static void ax_initial_setup(struct net_device *dev, struct ei_device *ei_local)
631{
632 void __iomem *ioaddr = ei_local->mem;
633 struct ax_device *ax = to_ax_dev(dev);
634
9f072429
MKB
635 /* Select page 0 */
636 ei_outb(E8390_NODMA + E8390_PAGE0 + E8390_STOP, ioaddr + E8390_CMD);
825a2ff1
BD
637
638 /* set to byte access */
639 ei_outb(ax->plat->dcr_val & ~1, ioaddr + EN0_DCFG);
640 ei_outb(ax->plat->gpoc_val, ioaddr + EI_SHIFT(0x17));
641}
642
9f072429
MKB
643/*
644 * ax_init_dev
825a2ff1
BD
645 *
646 * initialise the specified device, taking care to note the MAC
647 * address it may already have (if configured), ensure
648 * the device is ready to be used by lib8390.c and registerd with
649 * the network layer.
650 */
1cbece33 651static int ax_init_dev(struct net_device *dev)
825a2ff1
BD
652{
653 struct ei_device *ei_local = netdev_priv(dev);
654 struct ax_device *ax = to_ax_dev(dev);
655 void __iomem *ioaddr = ei_local->mem;
656 unsigned int start_page;
657 unsigned int stop_page;
658 int ret;
659 int i;
660
661 ret = ax_initial_check(dev);
662 if (ret)
663 goto err_out;
664
665 /* setup goes here */
666
667 ax_initial_setup(dev, ei_local);
668
669 /* read the mac from the card prom if we need it */
670
1cbece33 671 if (ax->plat->flags & AXFLG_HAS_EEPROM) {
825a2ff1
BD
672 unsigned char SA_prom[32];
673
9f072429 674 for (i = 0; i < sizeof(SA_prom); i += 2) {
825a2ff1 675 SA_prom[i] = ei_inb(ioaddr + NE_DATAPORT);
9f072429 676 SA_prom[i + 1] = ei_inb(ioaddr + NE_DATAPORT);
825a2ff1
BD
677 }
678
679 if (ax->plat->wordlength == 2)
680 for (i = 0; i < 16; i++)
681 SA_prom[i] = SA_prom[i+i];
682
d458cdf7 683 memcpy(dev->dev_addr, SA_prom, ETH_ALEN);
825a2ff1
BD
684 }
685
89e536a1 686#ifdef CONFIG_AX88796_93CX6
1cbece33 687 if (ax->plat->flags & AXFLG_HAS_93CX6) {
1409a932 688 unsigned char mac_addr[ETH_ALEN];
89e536a1
MD
689 struct eeprom_93cx6 eeprom;
690
691 eeprom.data = ei_local;
692 eeprom.register_read = ax_eeprom_register_read;
693 eeprom.register_write = ax_eeprom_register_write;
694 eeprom.width = PCI_EEPROM_WIDTH_93C56;
695
696 eeprom_93cx6_multiread(&eeprom, 0,
697 (__le16 __force *)mac_addr,
698 sizeof(mac_addr) >> 1);
699
1409a932 700 memcpy(dev->dev_addr, mac_addr, ETH_ALEN);
89e536a1
MD
701 }
702#endif
825a2ff1
BD
703 if (ax->plat->wordlength == 2) {
704 /* We must set the 8390 for word mode. */
705 ei_outb(ax->plat->dcr_val, ei_local->mem + EN0_DCFG);
706 start_page = NESM_START_PG;
707 stop_page = NESM_STOP_PG;
708 } else {
709 start_page = NE1SM_START_PG;
710 stop_page = NE1SM_STOP_PG;
711 }
712
1cbece33
MKB
713 /* load the mac-address from the device */
714 if (ax->plat->flags & AXFLG_MAC_FROMDEV) {
715 ei_outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP,
716 ei_local->mem + E8390_CMD); /* 0x61 */
104bf3fb 717 for (i = 0; i < ETH_ALEN; i++)
1cbece33
MKB
718 dev->dev_addr[i] =
719 ei_inb(ioaddr + EN1_PHYS_SHIFT(i));
825a2ff1
BD
720 }
721
1cbece33
MKB
722 if ((ax->plat->flags & AXFLG_MAC_FROMPLATFORM) &&
723 ax->plat->mac_addr)
104bf3fb 724 memcpy(dev->dev_addr, ax->plat->mac_addr, ETH_ALEN);
1cbece33 725
825a2ff1
BD
726 ax_reset_8390(dev);
727
ea5a43db
MKB
728 ei_local->name = "AX88796";
729 ei_local->tx_start_page = start_page;
730 ei_local->stop_page = stop_page;
731 ei_local->word16 = (ax->plat->wordlength == 2);
732 ei_local->rx_start_page = start_page + TX_PAGES;
825a2ff1
BD
733
734#ifdef PACKETBUF_MEMSIZE
9f072429 735 /* Allow the packet buffer size to be overridden by know-it-alls. */
ea5a43db 736 ei_local->stop_page = ei_local->tx_start_page + PACKETBUF_MEMSIZE;
825a2ff1
BD
737#endif
738
ea5a43db
MKB
739 ei_local->reset_8390 = &ax_reset_8390;
740 ei_local->block_input = &ax_block_input;
741 ei_local->block_output = &ax_block_output;
742 ei_local->get_8390_hdr = &ax_get_8390_hdr;
743 ei_local->priv = 0;
c45f812f 744 ei_local->msg_enable = ax_msg_enable;
825a2ff1 745
9f072429
MKB
746 dev->netdev_ops = &ax_netdev_ops;
747 dev->ethtool_ops = &ax_ethtool_ops;
825a2ff1 748
f6d7f2c6
MKB
749 ret = ax_mii_init(dev);
750 if (ret)
82533ad9 751 goto err_out;
825a2ff1 752
825a2ff1
BD
753 ax_NS8390_init(dev, 0);
754
825a2ff1
BD
755 ret = register_netdev(dev);
756 if (ret)
82533ad9 757 goto err_out;
825a2ff1 758
85ac6162
MKB
759 netdev_info(dev, "%dbit, irq %d, %lx, MAC: %pM\n",
760 ei_local->word16 ? 16 : 8, dev->irq, dev->base_addr,
761 dev->dev_addr);
762
825a2ff1
BD
763 return 0;
764
825a2ff1
BD
765 err_out:
766 return ret;
767}
768
c9218c3a 769static int ax_remove(struct platform_device *pdev)
825a2ff1 770{
c9218c3a 771 struct net_device *dev = platform_get_drvdata(pdev);
ea5a43db 772 struct ei_device *ei_local = netdev_priv(dev);
c9218c3a
MKB
773 struct ax_device *ax = to_ax_dev(dev);
774 struct resource *mem;
825a2ff1
BD
775
776 unregister_netdev(dev);
777 free_irq(dev->irq, dev);
778
ea5a43db 779 iounmap(ei_local->mem);
c9218c3a
MKB
780 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
781 release_mem_region(mem->start, resource_size(mem));
825a2ff1
BD
782
783 if (ax->map2) {
784 iounmap(ax->map2);
c9218c3a
MKB
785 mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
786 release_mem_region(mem->start, resource_size(mem));
825a2ff1
BD
787 }
788
789 free_netdev(dev);
790
791 return 0;
792}
793
9f072429
MKB
794/*
795 * ax_probe
825a2ff1
BD
796 *
797 * This is the entry point when the platform device system uses to
9f072429
MKB
798 * notify us of a new device to attach to. Allocate memory, find the
799 * resources and information passed, and map the necessary registers.
800 */
825a2ff1
BD
801static int ax_probe(struct platform_device *pdev)
802{
803 struct net_device *dev;
ea5a43db 804 struct ei_device *ei_local;
9f072429 805 struct ax_device *ax;
c9218c3a 806 struct resource *irq, *mem, *mem2;
b261c20f 807 unsigned long mem_size, mem2_size = 0;
47cb0355 808 int ret = 0;
825a2ff1
BD
809
810 dev = ax__alloc_ei_netdev(sizeof(struct ax_device));
811 if (dev == NULL)
812 return -ENOMEM;
813
814 /* ok, let's setup our device */
85ac6162 815 SET_NETDEV_DEV(dev, &pdev->dev);
ea5a43db 816 ei_local = netdev_priv(dev);
825a2ff1
BD
817 ax = to_ax_dev(dev);
818
a3ea2800 819 ax->plat = dev_get_platdata(&pdev->dev);
825a2ff1
BD
820 platform_set_drvdata(pdev, dev);
821
ea5a43db 822 ei_local->rxcr_base = ax->plat->rcr_val;
825a2ff1
BD
823
824 /* find the platform resources */
c9218c3a
MKB
825 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
826 if (!irq) {
825a2ff1 827 dev_err(&pdev->dev, "no IRQ specified\n");
13eea192 828 ret = -ENXIO;
825a2ff1
BD
829 goto exit_mem;
830 }
47cb0355 831
c9218c3a
MKB
832 dev->irq = irq->start;
833 ax->irqflags = irq->flags & IRQF_TRIGGER_MASK;
825a2ff1 834
c9218c3a
MKB
835 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
836 if (!mem) {
825a2ff1
BD
837 dev_err(&pdev->dev, "no MEM specified\n");
838 ret = -ENXIO;
839 goto exit_mem;
840 }
841
c9218c3a 842 mem_size = resource_size(mem);
825a2ff1 843
9f072429
MKB
844 /*
845 * setup the register offsets from either the platform data or
846 * by using the size of the resource provided
847 */
825a2ff1 848 if (ax->plat->reg_offsets)
ea5a43db 849 ei_local->reg_offset = ax->plat->reg_offsets;
825a2ff1 850 else {
ea5a43db 851 ei_local->reg_offset = ax->reg_offsets;
825a2ff1 852 for (ret = 0; ret < 0x18; ret++)
c9218c3a 853 ax->reg_offsets[ret] = (mem_size / 0x18) * ret;
825a2ff1
BD
854 }
855
c9218c3a 856 if (!request_mem_region(mem->start, mem_size, pdev->name)) {
825a2ff1 857 dev_err(&pdev->dev, "cannot reserve registers\n");
9f072429 858 ret = -ENXIO;
825a2ff1
BD
859 goto exit_mem;
860 }
861
c9218c3a 862 ei_local->mem = ioremap(mem->start, mem_size);
ea5a43db 863 dev->base_addr = (unsigned long)ei_local->mem;
825a2ff1 864
ea5a43db 865 if (ei_local->mem == NULL) {
c9218c3a 866 dev_err(&pdev->dev, "Cannot ioremap area %pR\n", mem);
825a2ff1 867
9f072429 868 ret = -ENXIO;
825a2ff1
BD
869 goto exit_req;
870 }
871
872 /* look for reset area */
c9218c3a
MKB
873 mem2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
874 if (!mem2) {
825a2ff1
BD
875 if (!ax->plat->reg_offsets) {
876 for (ret = 0; ret < 0x20; ret++)
c9218c3a 877 ax->reg_offsets[ret] = (mem_size / 0x20) * ret;
825a2ff1 878 }
825a2ff1 879 } else {
c9218c3a 880 mem2_size = resource_size(mem2);
825a2ff1 881
c9218c3a 882 if (!request_mem_region(mem2->start, mem2_size, pdev->name)) {
825a2ff1
BD
883 dev_err(&pdev->dev, "cannot reserve registers\n");
884 ret = -ENXIO;
885 goto exit_mem1;
886 }
887
c9218c3a
MKB
888 ax->map2 = ioremap(mem2->start, mem2_size);
889 if (!ax->map2) {
898eb71c 890 dev_err(&pdev->dev, "cannot map reset register\n");
825a2ff1
BD
891 ret = -ENXIO;
892 goto exit_mem2;
893 }
894
ea5a43db 895 ei_local->reg_offset[0x1f] = ax->map2 - ei_local->mem;
825a2ff1
BD
896 }
897
898 /* got resources, now initialise and register device */
1cbece33 899 ret = ax_init_dev(dev);
825a2ff1
BD
900 if (!ret)
901 return 0;
902
c9218c3a 903 if (!ax->map2)
825a2ff1
BD
904 goto exit_mem1;
905
906 iounmap(ax->map2);
907
908 exit_mem2:
09d306d7 909 if (mem2)
910 release_mem_region(mem2->start, mem2_size);
825a2ff1
BD
911
912 exit_mem1:
ea5a43db 913 iounmap(ei_local->mem);
825a2ff1
BD
914
915 exit_req:
c9218c3a 916 release_mem_region(mem->start, mem_size);
825a2ff1
BD
917
918 exit_mem:
919 free_netdev(dev);
920
921 return ret;
922}
923
924/* suspend and resume */
925
926#ifdef CONFIG_PM
927static int ax_suspend(struct platform_device *dev, pm_message_t state)
928{
929 struct net_device *ndev = platform_get_drvdata(dev);
9f072429 930 struct ax_device *ax = to_ax_dev(ndev);
825a2ff1
BD
931
932 ax->resume_open = ax->running;
933
934 netif_device_detach(ndev);
935 ax_close(ndev);
936
937 return 0;
938}
939
940static int ax_resume(struct platform_device *pdev)
941{
942 struct net_device *ndev = platform_get_drvdata(pdev);
9f072429 943 struct ax_device *ax = to_ax_dev(ndev);
825a2ff1
BD
944
945 ax_initial_setup(ndev, netdev_priv(ndev));
946 ax_NS8390_init(ndev, ax->resume_open);
947 netif_device_attach(ndev);
948
949 if (ax->resume_open)
950 ax_open(ndev);
951
952 return 0;
953}
954
955#else
956#define ax_suspend NULL
9f072429 957#define ax_resume NULL
825a2ff1
BD
958#endif
959
960static struct platform_driver axdrv = {
961 .driver = {
962 .name = "ax88796",
825a2ff1
BD
963 },
964 .probe = ax_probe,
965 .remove = ax_remove,
966 .suspend = ax_suspend,
967 .resume = ax_resume,
968};
969
db62f684 970module_platform_driver(axdrv);
825a2ff1
BD
971
972MODULE_DESCRIPTION("AX88796 10/100 Ethernet platform driver");
973MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
974MODULE_LICENSE("GPL v2");
72abb461 975MODULE_ALIAS("platform:ax88796");