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