blob: 11476ca95e93978b4e60e9108d3805a8e4797ab0 [file] [log] [blame]
Paul Gortmaker3396c782012-01-27 13:36:01 +00001/* drivers/net/ethernet/8390/ax88796.c
Ben Dooks825a2ff2007-07-03 16:53:09 +01002 *
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.
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010012 */
Ben Dooks825a2ff2007-07-03 16:53:09 +010013
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>
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010020#include <linux/io.h>
Ben Dooks825a2ff2007-07-03 16:53:09 +010021#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>
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +010027#include <linux/mdio-bitbang.h>
28#include <linux/phy.h>
Magnus Damm89e536a2007-09-28 22:42:16 -070029#include <linux/eeprom_93cx6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Ben Dooks825a2ff2007-07-03 16:53:09 +010031
32#include <net/ax88796.h>
33
Ben Dooks825a2ff2007-07-03 16:53:09 +010034
Ben Dooks825a2ff2007-07-03 16:53:09 +010035/* Rename the lib8390.c functions to show that they are in this driver */
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010036#define __ei_open ax_ei_open
37#define __ei_close ax_ei_close
38#define __ei_poll ax_ei_poll
Magnus Dammd57bc362009-01-14 21:05:55 -080039#define __ei_start_xmit ax_ei_start_xmit
Ben Dooks825a2ff2007-07-03 16:53:09 +010040#define __ei_tx_timeout ax_ei_tx_timeout
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010041#define __ei_get_stats ax_ei_get_stats
Magnus Dammd57bc362009-01-14 21:05:55 -080042#define __ei_set_multicast_list ax_ei_set_multicast_list
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010043#define __ei_interrupt ax_ei_interrupt
Ben Dooks825a2ff2007-07-03 16:53:09 +010044#define ____alloc_ei_netdev ax__alloc_ei_netdev
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010045#define __NS8390_init ax_NS8390_init
Ben Dooks825a2ff2007-07-03 16:53:09 +010046
47/* force unsigned long back to 'void __iomem *' */
48#define ax_convert_addr(_a) ((void __force __iomem *)(_a))
49
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010050#define ei_inb(_a) readb(ax_convert_addr(_a))
Ben Dooks825a2ff2007-07-03 16:53:09 +010051#define ei_outb(_v, _a) writeb(_v, ax_convert_addr(_a))
52
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010053#define ei_inb_p(_a) ei_inb(_a)
Ben Dooks825a2ff2007-07-03 16:53:09 +010054#define ei_outb_p(_v, _a) ei_outb(_v, _a)
55
56/* define EI_SHIFT() to take into account our register offsets */
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010057#define EI_SHIFT(x) (ei_local->reg_offset[(x)])
Ben Dooks825a2ff2007-07-03 16:53:09 +010058
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 */
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010075#define NE1SM_STOP_PG 0x40 /* Last page +1 of RX ring */
Ben Dooks825a2ff2007-07-03 16:53:09 +010076#define NESM_START_PG 0x40 /* First page of TX buffer */
77#define NESM_STOP_PG 0x80 /* Last page +1 of RX ring */
78
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +010079#define AX_GPOC_PPDSET BIT(6)
80
Ben Dooks825a2ff2007-07-03 16:53:09 +010081/* device private data */
82
83struct ax_device {
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +010084 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;
Ben Dooks825a2ff2007-07-03 16:53:09 +010092
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010093 void __iomem *map2;
Marc Kleine-Buddea54dc582011-02-21 10:43:31 +010094 const struct ax_plat_data *plat;
Ben Dooks825a2ff2007-07-03 16:53:09 +010095
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010096 unsigned char running;
97 unsigned char resume_open;
98 unsigned int irqflags;
Ben Dooks825a2ff2007-07-03 16:53:09 +010099
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100100 u32 reg_offsets[0x20];
Ben Dooks825a2ff2007-07-03 16:53:09 +0100101};
102
103static inline struct ax_device *to_ax_dev(struct net_device *dev)
104{
105 struct ei_device *ei_local = netdev_priv(dev);
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100106 return (struct ax_device *)(ei_local + 1);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100107}
108
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100109/*
110 * ax_initial_check
Ben Dooks825a2ff2007-07-03 16:53:09 +0100111 *
112 * do an initial probe for the card to check wether it exists
113 * and is functional
114 */
Ben Dooks825a2ff2007-07-03 16:53:09 +0100115static 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
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100126 ei_outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP, ioaddr + E8390_CMD);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100127 regd = ei_inb(ioaddr + 0x0d);
128 ei_outb(0xff, ioaddr + 0x0d);
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100129 ei_outb(E8390_NODMA + E8390_PAGE0, ioaddr + E8390_CMD);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100130 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
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100140/*
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 */
Ben Dooks825a2ff2007-07-03 16:53:09 +0100144static 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)
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100151 netdev_dbg(dev, "resetting the 8390 t=%ld\n", jiffies);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100152
153 ei_outb(ei_inb(addr + NE_RESET), addr + NE_RESET);
154
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100155 ei_local->txing = 0;
156 ei_local->dmaing = 0;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100157
158 /* This check _should_not_ be necessary, omit eventually. */
159 while ((ei_inb(addr + EN0_ISR) & ENISR_RESET) == 0) {
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100160 if (jiffies - reset_start_time > 2 * HZ / 100) {
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100161 netdev_warn(dev, "%s: did not complete.\n", __func__);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100162 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 */
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100177 if (ei_local->dmaing) {
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100178 netdev_err(dev, "DMAing conflict in %s "
Ben Dooks237c5e82008-01-31 11:25:31 +0000179 "[DMAstat:%d][irqlock:%d].\n",
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100180 __func__,
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100181 ei_local->dmaing, ei_local->irqlock);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100182 return;
183 }
184
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100185 ei_local->dmaing |= 0x01;
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100186 ei_outb(E8390_NODMA + E8390_PAGE0 + E8390_START, nic_base + NE_CMD);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100187 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
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100193 if (ei_local->word16)
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100194 readsw(nic_base + NE_DATAPORT, hdr,
195 sizeof(struct e8390_pkt_hdr) >> 1);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100196 else
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100197 readsb(nic_base + NE_DATAPORT, hdr,
198 sizeof(struct e8390_pkt_hdr));
Ben Dooks825a2ff2007-07-03 16:53:09 +0100199
200 ei_outb(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100201 ei_local->dmaing &= ~0x01;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100202
203 le16_to_cpus(&hdr->count);
204}
205
206
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100207/*
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 */
Ben Dooks825a2ff2007-07-03 16:53:09 +0100214static 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
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100221 if (ei_local->dmaing) {
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100222 netdev_err(dev,
223 "DMAing conflict in %s "
Ben Dooks825a2ff2007-07-03 16:53:09 +0100224 "[DMAstat:%d][irqlock:%d].\n",
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100225 __func__,
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100226 ei_local->dmaing, ei_local->irqlock);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100227 return;
228 }
229
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100230 ei_local->dmaing |= 0x01;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100231
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100232 ei_outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base + NE_CMD);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100233 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
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100239 if (ei_local->word16) {
Ben Dooks825a2ff2007-07-03 16:53:09 +0100240 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
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100248 ei_local->dmaing &= ~1;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100249}
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
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100258 /*
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 */
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100263 if (ei_local->word16 && (count & 0x01))
Ben Dooks825a2ff2007-07-03 16:53:09 +0100264 count++;
265
266 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100267 if (ei_local->dmaing) {
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100268 netdev_err(dev, "DMAing conflict in %s."
Ben Dooks825a2ff2007-07-03 16:53:09 +0100269 "[DMAstat:%d][irqlock:%d]\n",
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100270 __func__,
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100271 ei_local->dmaing, ei_local->irqlock);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100272 return;
273 }
274
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100275 ei_local->dmaing |= 0x01;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100276 /* 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);
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100283 ei_outb(count >> 8, nic_base + EN0_RCNTHI);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100284 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);
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100288 if (ei_local->word16)
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100289 writesw(nic_base + NE_DATAPORT, buf, count >> 1);
290 else
Ben Dooks825a2ff2007-07-03 16:53:09 +0100291 writesb(nic_base + NE_DATAPORT, buf, count);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100292
293 dma_start = jiffies;
294
295 while ((ei_inb(nic_base + EN0_ISR) & ENISR_RDC) == 0) {
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100296 if (jiffies - dma_start > 2 * HZ / 100) { /* 20ms */
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100297 netdev_warn(dev, "timeout waiting for Tx RDC.\n");
Ben Dooks825a2ff2007-07-03 16:53:09 +0100298 ax_reset_8390(dev);
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100299 ax_NS8390_init(dev, 1);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100300 break;
301 }
302 }
303
304 ei_outb(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100305 ei_local->dmaing &= ~0x01;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100306}
307
308/* definitions for accessing MII/EEPROM interface */
309
310#define AX_MEMR EI_SHIFT(0x14)
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100311#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)
Ben Dooks825a2ff2007-07-03 16:53:09 +0100319
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100320static void ax_handle_link_change(struct net_device *dev)
Ben Dooks825a2ff2007-07-03 16:53:09 +0100321{
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100322 struct ax_device *ax = to_ax_dev(dev);
323 struct phy_device *phy_dev = ax->phy_dev;
324 int status_change = 0;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100325
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100326 if (phy_dev->link && ((ax->speed != phy_dev->speed) ||
327 (ax->duplex != phy_dev->duplex))) {
Ben Dooks825a2ff2007-07-03 16:53:09 +0100328
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100329 ax->speed = phy_dev->speed;
330 ax->duplex = phy_dev->duplex;
331 status_change = 1;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100332 }
333
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100334 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;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100340
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100341 status_change = 1;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100342 }
343
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100344 if (status_change)
345 phy_print_status(phy_dev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100346}
347
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100348static int ax_mii_probe(struct net_device *dev)
Ben Dooks825a2ff2007-07-03 16:53:09 +0100349{
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100350 struct ax_device *ax = to_ax_dev(dev);
351 struct phy_device *phy_dev = NULL;
352 int ret;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100353
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100354 /* 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;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100378}
379
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100380static void ax_phy_switch(struct net_device *dev, int on)
Ben Dooks825a2ff2007-07-03 16:53:09 +0100381{
Joe Perchesece49152010-11-15 11:12:31 +0000382 struct ei_device *ei_local = netdev_priv(dev);
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100383 struct ax_device *ax = to_ax_dev(dev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100384
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100385 u8 reg_gpoc = ax->plat->gpoc_val;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100386
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100387 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));
Ben Dooks825a2ff2007-07-03 16:53:09 +0100393}
394
395static int ax_open(struct net_device *dev)
396{
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100397 struct ax_device *ax = to_ax_dev(dev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100398 int ret;
399
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100400 netdev_dbg(dev, "open\n");
Ben Dooks825a2ff2007-07-03 16:53:09 +0100401
Daniel Mack47cb0352009-03-24 23:31:22 -0700402 ret = request_irq(dev->irq, ax_ei_interrupt, ax->irqflags,
403 dev->name, dev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100404 if (ret)
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100405 goto failed_request_irq;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100406
407 /* turn the phy on (if turned off) */
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100408 ax_phy_switch(dev, 1);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100409
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100410 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
Ben Dooks825a2ff2007-07-03 16:53:09 +0100419 ax->running = 1;
420
Ben Dooks825a2ff2007-07-03 16:53:09 +0100421 return 0;
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100422
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;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100430}
431
432static int ax_close(struct net_device *dev)
433{
434 struct ax_device *ax = to_ax_dev(dev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100435
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100436 netdev_dbg(dev, "close\n");
Ben Dooks825a2ff2007-07-03 16:53:09 +0100437
Ben Dooks825a2ff2007-07-03 16:53:09 +0100438 ax->running = 0;
439 wmb();
440
Ben Dooks825a2ff2007-07-03 16:53:09 +0100441 ax_ei_close(dev);
442
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100443 /* turn the phy off */
444 ax_phy_switch(dev, 0);
445 phy_disconnect(ax->phy_dev);
446
Ben Dooks825a2ff2007-07-03 16:53:09 +0100447 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);
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100454 struct phy_device *phy_dev = ax->phy_dev;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100455
456 if (!netif_running(dev))
457 return -EINVAL;
458
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100459 if (!phy_dev)
460 return -ENODEV;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100461
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100462 return phy_mii_ioctl(phy_dev, req, cmd);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100463}
464
465/* ethtool ops */
466
467static void ax_get_drvinfo(struct net_device *dev,
468 struct ethtool_drvinfo *info)
469{
Marc Kleine-Budde0f442f52011-02-20 19:13:20 +0100470 struct platform_device *pdev = to_platform_device(dev->dev.parent);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100471
472 strcpy(info->driver, DRV_NAME);
473 strcpy(info->version, DRV_VERSION);
Marc Kleine-Budde0f442f52011-02-20 19:13:20 +0100474 strcpy(info->bus_info, pdev->name);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100475}
476
477static int ax_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
478{
479 struct ax_device *ax = to_ax_dev(dev);
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100480 struct phy_device *phy_dev = ax->phy_dev;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100481
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100482 if (!phy_dev)
483 return -ENODEV;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100484
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100485 return phy_ethtool_gset(phy_dev, cmd);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100486}
487
488static int ax_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
489{
490 struct ax_device *ax = to_ax_dev(dev);
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100491 struct phy_device *phy_dev = ax->phy_dev;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100492
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100493 if (!phy_dev)
494 return -ENODEV;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100495
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100496 return phy_ethtool_sset(phy_dev, cmd);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100497}
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,
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100503 .get_link = ethtool_op_get_link,
Ben Dooks825a2ff2007-07-03 16:53:09 +0100504};
505
Magnus Damm89e536a2007-09-28 22:42:16 -0700506#ifdef CONFIG_AX88796_93CX6
507static void ax_eeprom_register_read(struct eeprom_93cx6 *eeprom)
508{
509 struct ei_device *ei_local = eeprom->data;
510 u8 reg = ei_inb(ei_local->mem + AX_MEMR);
511
512 eeprom->reg_data_in = reg & AX_MEMR_EEI;
513 eeprom->reg_data_out = reg & AX_MEMR_EEO; /* Input pin */
514 eeprom->reg_data_clock = reg & AX_MEMR_EECLK;
515 eeprom->reg_chip_select = reg & AX_MEMR_EECS;
516}
517
518static void ax_eeprom_register_write(struct eeprom_93cx6 *eeprom)
519{
520 struct ei_device *ei_local = eeprom->data;
521 u8 reg = ei_inb(ei_local->mem + AX_MEMR);
522
523 reg &= ~(AX_MEMR_EEI | AX_MEMR_EECLK | AX_MEMR_EECS);
524
525 if (eeprom->reg_data_in)
526 reg |= AX_MEMR_EEI;
527 if (eeprom->reg_data_clock)
528 reg |= AX_MEMR_EECLK;
529 if (eeprom->reg_chip_select)
530 reg |= AX_MEMR_EECS;
531
532 ei_outb(reg, ei_local->mem + AX_MEMR);
533 udelay(10);
534}
535#endif
536
Magnus Dammd57bc362009-01-14 21:05:55 -0800537static const struct net_device_ops ax_netdev_ops = {
538 .ndo_open = ax_open,
539 .ndo_stop = ax_close,
540 .ndo_do_ioctl = ax_ioctl,
541
542 .ndo_start_xmit = ax_ei_start_xmit,
543 .ndo_tx_timeout = ax_ei_tx_timeout,
544 .ndo_get_stats = ax_ei_get_stats,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000545 .ndo_set_rx_mode = ax_ei_set_multicast_list,
Magnus Dammd57bc362009-01-14 21:05:55 -0800546 .ndo_validate_addr = eth_validate_addr,
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100547 .ndo_set_mac_address = eth_mac_addr,
Magnus Dammd57bc362009-01-14 21:05:55 -0800548 .ndo_change_mtu = eth_change_mtu,
549#ifdef CONFIG_NET_POLL_CONTROLLER
550 .ndo_poll_controller = ax_ei_poll,
551#endif
552};
553
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100554static void ax_bb_mdc(struct mdiobb_ctrl *ctrl, int level)
555{
556 struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
557
558 if (level)
559 ax->reg_memr |= AX_MEMR_MDC;
560 else
561 ax->reg_memr &= ~AX_MEMR_MDC;
562
563 ei_outb(ax->reg_memr, ax->addr_memr);
564}
565
566static void ax_bb_dir(struct mdiobb_ctrl *ctrl, int output)
567{
568 struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
569
570 if (output)
571 ax->reg_memr &= ~AX_MEMR_MDIR;
572 else
573 ax->reg_memr |= AX_MEMR_MDIR;
574
575 ei_outb(ax->reg_memr, ax->addr_memr);
576}
577
578static void ax_bb_set_data(struct mdiobb_ctrl *ctrl, int value)
579{
580 struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
581
582 if (value)
583 ax->reg_memr |= AX_MEMR_MDO;
584 else
585 ax->reg_memr &= ~AX_MEMR_MDO;
586
587 ei_outb(ax->reg_memr, ax->addr_memr);
588}
589
590static int ax_bb_get_data(struct mdiobb_ctrl *ctrl)
591{
592 struct ax_device *ax = container_of(ctrl, struct ax_device, bb_ctrl);
593 int reg_memr = ei_inb(ax->addr_memr);
594
595 return reg_memr & AX_MEMR_MDI ? 1 : 0;
596}
597
598static struct mdiobb_ops bb_ops = {
599 .owner = THIS_MODULE,
600 .set_mdc = ax_bb_mdc,
601 .set_mdio_dir = ax_bb_dir,
602 .set_mdio_data = ax_bb_set_data,
603 .get_mdio_data = ax_bb_get_data,
604};
605
Ben Dooks825a2ff2007-07-03 16:53:09 +0100606/* setup code */
607
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100608static int ax_mii_init(struct net_device *dev)
609{
610 struct platform_device *pdev = to_platform_device(dev->dev.parent);
611 struct ei_device *ei_local = netdev_priv(dev);
612 struct ax_device *ax = to_ax_dev(dev);
613 int err, i;
614
615 ax->bb_ctrl.ops = &bb_ops;
616 ax->addr_memr = ei_local->mem + AX_MEMR;
617 ax->mii_bus = alloc_mdio_bitbang(&ax->bb_ctrl);
618 if (!ax->mii_bus) {
619 err = -ENOMEM;
620 goto out;
621 }
622
623 ax->mii_bus->name = "ax88796_mii_bus";
624 ax->mii_bus->parent = dev->dev.parent;
Florian Fainellifa84e722012-01-09 23:59:07 +0000625 snprintf(ax->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
626 pdev->name, pdev->id);
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100627
628 ax->mii_bus->irq = kmalloc(sizeof(int) * PHY_MAX_ADDR, GFP_KERNEL);
629 if (!ax->mii_bus->irq) {
630 err = -ENOMEM;
631 goto out_free_mdio_bitbang;
632 }
633
634 for (i = 0; i < PHY_MAX_ADDR; i++)
635 ax->mii_bus->irq[i] = PHY_POLL;
636
637 err = mdiobus_register(ax->mii_bus);
638 if (err)
639 goto out_free_irq;
640
641 return 0;
642
643 out_free_irq:
644 kfree(ax->mii_bus->irq);
645 out_free_mdio_bitbang:
646 free_mdio_bitbang(ax->mii_bus);
647 out:
648 return err;
649}
650
Ben Dooks825a2ff2007-07-03 16:53:09 +0100651static void ax_initial_setup(struct net_device *dev, struct ei_device *ei_local)
652{
653 void __iomem *ioaddr = ei_local->mem;
654 struct ax_device *ax = to_ax_dev(dev);
655
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100656 /* Select page 0 */
657 ei_outb(E8390_NODMA + E8390_PAGE0 + E8390_STOP, ioaddr + E8390_CMD);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100658
659 /* set to byte access */
660 ei_outb(ax->plat->dcr_val & ~1, ioaddr + EN0_DCFG);
661 ei_outb(ax->plat->gpoc_val, ioaddr + EI_SHIFT(0x17));
662}
663
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100664/*
665 * ax_init_dev
Ben Dooks825a2ff2007-07-03 16:53:09 +0100666 *
667 * initialise the specified device, taking care to note the MAC
668 * address it may already have (if configured), ensure
669 * the device is ready to be used by lib8390.c and registerd with
670 * the network layer.
671 */
Marc Kleine-Budde1cbece32011-02-19 23:07:09 +0100672static int ax_init_dev(struct net_device *dev)
Ben Dooks825a2ff2007-07-03 16:53:09 +0100673{
674 struct ei_device *ei_local = netdev_priv(dev);
675 struct ax_device *ax = to_ax_dev(dev);
676 void __iomem *ioaddr = ei_local->mem;
677 unsigned int start_page;
678 unsigned int stop_page;
679 int ret;
680 int i;
681
682 ret = ax_initial_check(dev);
683 if (ret)
684 goto err_out;
685
686 /* setup goes here */
687
688 ax_initial_setup(dev, ei_local);
689
690 /* read the mac from the card prom if we need it */
691
Marc Kleine-Budde1cbece32011-02-19 23:07:09 +0100692 if (ax->plat->flags & AXFLG_HAS_EEPROM) {
Ben Dooks825a2ff2007-07-03 16:53:09 +0100693 unsigned char SA_prom[32];
694
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100695 for (i = 0; i < sizeof(SA_prom); i += 2) {
Ben Dooks825a2ff2007-07-03 16:53:09 +0100696 SA_prom[i] = ei_inb(ioaddr + NE_DATAPORT);
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100697 SA_prom[i + 1] = ei_inb(ioaddr + NE_DATAPORT);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100698 }
699
700 if (ax->plat->wordlength == 2)
701 for (i = 0; i < 16; i++)
702 SA_prom[i] = SA_prom[i+i];
703
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100704 memcpy(dev->dev_addr, SA_prom, 6);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100705 }
706
Magnus Damm89e536a2007-09-28 22:42:16 -0700707#ifdef CONFIG_AX88796_93CX6
Marc Kleine-Budde1cbece32011-02-19 23:07:09 +0100708 if (ax->plat->flags & AXFLG_HAS_93CX6) {
Magnus Damm89e536a2007-09-28 22:42:16 -0700709 unsigned char mac_addr[6];
710 struct eeprom_93cx6 eeprom;
711
712 eeprom.data = ei_local;
713 eeprom.register_read = ax_eeprom_register_read;
714 eeprom.register_write = ax_eeprom_register_write;
715 eeprom.width = PCI_EEPROM_WIDTH_93C56;
716
717 eeprom_93cx6_multiread(&eeprom, 0,
718 (__le16 __force *)mac_addr,
719 sizeof(mac_addr) >> 1);
720
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100721 memcpy(dev->dev_addr, mac_addr, 6);
Magnus Damm89e536a2007-09-28 22:42:16 -0700722 }
723#endif
Ben Dooks825a2ff2007-07-03 16:53:09 +0100724 if (ax->plat->wordlength == 2) {
725 /* We must set the 8390 for word mode. */
726 ei_outb(ax->plat->dcr_val, ei_local->mem + EN0_DCFG);
727 start_page = NESM_START_PG;
728 stop_page = NESM_STOP_PG;
729 } else {
730 start_page = NE1SM_START_PG;
731 stop_page = NE1SM_STOP_PG;
732 }
733
Marc Kleine-Budde1cbece32011-02-19 23:07:09 +0100734 /* load the mac-address from the device */
735 if (ax->plat->flags & AXFLG_MAC_FROMDEV) {
736 ei_outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP,
737 ei_local->mem + E8390_CMD); /* 0x61 */
Joe Perches104bf3f2011-11-16 09:38:03 +0000738 for (i = 0; i < ETH_ALEN; i++)
Marc Kleine-Budde1cbece32011-02-19 23:07:09 +0100739 dev->dev_addr[i] =
740 ei_inb(ioaddr + EN1_PHYS_SHIFT(i));
Ben Dooks825a2ff2007-07-03 16:53:09 +0100741 }
742
Marc Kleine-Budde1cbece32011-02-19 23:07:09 +0100743 if ((ax->plat->flags & AXFLG_MAC_FROMPLATFORM) &&
744 ax->plat->mac_addr)
Joe Perches104bf3f2011-11-16 09:38:03 +0000745 memcpy(dev->dev_addr, ax->plat->mac_addr, ETH_ALEN);
Marc Kleine-Budde1cbece32011-02-19 23:07:09 +0100746
Ben Dooks825a2ff2007-07-03 16:53:09 +0100747 ax_reset_8390(dev);
748
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100749 ei_local->name = "AX88796";
750 ei_local->tx_start_page = start_page;
751 ei_local->stop_page = stop_page;
752 ei_local->word16 = (ax->plat->wordlength == 2);
753 ei_local->rx_start_page = start_page + TX_PAGES;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100754
755#ifdef PACKETBUF_MEMSIZE
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100756 /* Allow the packet buffer size to be overridden by know-it-alls. */
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100757 ei_local->stop_page = ei_local->tx_start_page + PACKETBUF_MEMSIZE;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100758#endif
759
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100760 ei_local->reset_8390 = &ax_reset_8390;
761 ei_local->block_input = &ax_block_input;
762 ei_local->block_output = &ax_block_output;
763 ei_local->get_8390_hdr = &ax_get_8390_hdr;
764 ei_local->priv = 0;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100765
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100766 dev->netdev_ops = &ax_netdev_ops;
767 dev->ethtool_ops = &ax_ethtool_ops;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100768
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100769 ret = ax_mii_init(dev);
770 if (ret)
771 goto out_irq;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100772
Ben Dooks825a2ff2007-07-03 16:53:09 +0100773 ax_NS8390_init(dev, 0);
774
Ben Dooks825a2ff2007-07-03 16:53:09 +0100775 ret = register_netdev(dev);
776 if (ret)
777 goto out_irq;
778
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100779 netdev_info(dev, "%dbit, irq %d, %lx, MAC: %pM\n",
780 ei_local->word16 ? 16 : 8, dev->irq, dev->base_addr,
781 dev->dev_addr);
782
Ben Dooks825a2ff2007-07-03 16:53:09 +0100783 return 0;
784
785 out_irq:
786 /* cleanup irq */
787 free_irq(dev->irq, dev);
788 err_out:
789 return ret;
790}
791
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100792static int ax_remove(struct platform_device *pdev)
Ben Dooks825a2ff2007-07-03 16:53:09 +0100793{
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100794 struct net_device *dev = platform_get_drvdata(pdev);
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100795 struct ei_device *ei_local = netdev_priv(dev);
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100796 struct ax_device *ax = to_ax_dev(dev);
797 struct resource *mem;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100798
799 unregister_netdev(dev);
800 free_irq(dev->irq, dev);
801
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100802 iounmap(ei_local->mem);
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100803 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
804 release_mem_region(mem->start, resource_size(mem));
Ben Dooks825a2ff2007-07-03 16:53:09 +0100805
806 if (ax->map2) {
807 iounmap(ax->map2);
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100808 mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
809 release_mem_region(mem->start, resource_size(mem));
Ben Dooks825a2ff2007-07-03 16:53:09 +0100810 }
811
812 free_netdev(dev);
813
814 return 0;
815}
816
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100817/*
818 * ax_probe
Ben Dooks825a2ff2007-07-03 16:53:09 +0100819 *
820 * This is the entry point when the platform device system uses to
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100821 * notify us of a new device to attach to. Allocate memory, find the
822 * resources and information passed, and map the necessary registers.
823 */
Ben Dooks825a2ff2007-07-03 16:53:09 +0100824static int ax_probe(struct platform_device *pdev)
825{
826 struct net_device *dev;
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100827 struct ei_device *ei_local;
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100828 struct ax_device *ax;
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100829 struct resource *irq, *mem, *mem2;
830 resource_size_t mem_size, mem2_size = 0;
Daniel Mack47cb0352009-03-24 23:31:22 -0700831 int ret = 0;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100832
833 dev = ax__alloc_ei_netdev(sizeof(struct ax_device));
834 if (dev == NULL)
835 return -ENOMEM;
836
837 /* ok, let's setup our device */
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100838 SET_NETDEV_DEV(dev, &pdev->dev);
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100839 ei_local = netdev_priv(dev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100840 ax = to_ax_dev(dev);
841
Ben Dooks825a2ff2007-07-03 16:53:09 +0100842 ax->plat = pdev->dev.platform_data;
843 platform_set_drvdata(pdev, dev);
844
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100845 ei_local->rxcr_base = ax->plat->rcr_val;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100846
847 /* find the platform resources */
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100848 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
849 if (!irq) {
Ben Dooks825a2ff2007-07-03 16:53:09 +0100850 dev_err(&pdev->dev, "no IRQ specified\n");
Julia Lawall13eea192010-10-18 04:11:14 +0000851 ret = -ENXIO;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100852 goto exit_mem;
853 }
Daniel Mack47cb0352009-03-24 23:31:22 -0700854
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100855 dev->irq = irq->start;
856 ax->irqflags = irq->flags & IRQF_TRIGGER_MASK;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100857
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100858 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
859 if (!mem) {
Ben Dooks825a2ff2007-07-03 16:53:09 +0100860 dev_err(&pdev->dev, "no MEM specified\n");
861 ret = -ENXIO;
862 goto exit_mem;
863 }
864
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100865 mem_size = resource_size(mem);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100866
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100867 /*
868 * setup the register offsets from either the platform data or
869 * by using the size of the resource provided
870 */
Ben Dooks825a2ff2007-07-03 16:53:09 +0100871 if (ax->plat->reg_offsets)
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100872 ei_local->reg_offset = ax->plat->reg_offsets;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100873 else {
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100874 ei_local->reg_offset = ax->reg_offsets;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100875 for (ret = 0; ret < 0x18; ret++)
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100876 ax->reg_offsets[ret] = (mem_size / 0x18) * ret;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100877 }
878
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100879 if (!request_mem_region(mem->start, mem_size, pdev->name)) {
Ben Dooks825a2ff2007-07-03 16:53:09 +0100880 dev_err(&pdev->dev, "cannot reserve registers\n");
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100881 ret = -ENXIO;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100882 goto exit_mem;
883 }
884
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100885 ei_local->mem = ioremap(mem->start, mem_size);
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100886 dev->base_addr = (unsigned long)ei_local->mem;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100887
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100888 if (ei_local->mem == NULL) {
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100889 dev_err(&pdev->dev, "Cannot ioremap area %pR\n", mem);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100890
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100891 ret = -ENXIO;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100892 goto exit_req;
893 }
894
895 /* look for reset area */
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100896 mem2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
897 if (!mem2) {
Ben Dooks825a2ff2007-07-03 16:53:09 +0100898 if (!ax->plat->reg_offsets) {
899 for (ret = 0; ret < 0x20; ret++)
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100900 ax->reg_offsets[ret] = (mem_size / 0x20) * ret;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100901 }
Ben Dooks825a2ff2007-07-03 16:53:09 +0100902 } else {
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100903 mem2_size = resource_size(mem2);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100904
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100905 if (!request_mem_region(mem2->start, mem2_size, pdev->name)) {
Ben Dooks825a2ff2007-07-03 16:53:09 +0100906 dev_err(&pdev->dev, "cannot reserve registers\n");
907 ret = -ENXIO;
908 goto exit_mem1;
909 }
910
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100911 ax->map2 = ioremap(mem2->start, mem2_size);
912 if (!ax->map2) {
Joe Perches898eb712007-10-18 03:06:30 -0700913 dev_err(&pdev->dev, "cannot map reset register\n");
Ben Dooks825a2ff2007-07-03 16:53:09 +0100914 ret = -ENXIO;
915 goto exit_mem2;
916 }
917
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100918 ei_local->reg_offset[0x1f] = ax->map2 - ei_local->mem;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100919 }
920
921 /* got resources, now initialise and register device */
Marc Kleine-Budde1cbece32011-02-19 23:07:09 +0100922 ret = ax_init_dev(dev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100923 if (!ret)
924 return 0;
925
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100926 if (!ax->map2)
Ben Dooks825a2ff2007-07-03 16:53:09 +0100927 goto exit_mem1;
928
929 iounmap(ax->map2);
930
931 exit_mem2:
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100932 release_mem_region(mem2->start, mem2_size);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100933
934 exit_mem1:
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100935 iounmap(ei_local->mem);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100936
937 exit_req:
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100938 release_mem_region(mem->start, mem_size);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100939
940 exit_mem:
941 free_netdev(dev);
942
943 return ret;
944}
945
946/* suspend and resume */
947
948#ifdef CONFIG_PM
949static int ax_suspend(struct platform_device *dev, pm_message_t state)
950{
951 struct net_device *ndev = platform_get_drvdata(dev);
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100952 struct ax_device *ax = to_ax_dev(ndev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100953
954 ax->resume_open = ax->running;
955
956 netif_device_detach(ndev);
957 ax_close(ndev);
958
959 return 0;
960}
961
962static int ax_resume(struct platform_device *pdev)
963{
964 struct net_device *ndev = platform_get_drvdata(pdev);
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100965 struct ax_device *ax = to_ax_dev(ndev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100966
967 ax_initial_setup(ndev, netdev_priv(ndev));
968 ax_NS8390_init(ndev, ax->resume_open);
969 netif_device_attach(ndev);
970
971 if (ax->resume_open)
972 ax_open(ndev);
973
974 return 0;
975}
976
977#else
978#define ax_suspend NULL
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100979#define ax_resume NULL
Ben Dooks825a2ff2007-07-03 16:53:09 +0100980#endif
981
982static struct platform_driver axdrv = {
983 .driver = {
984 .name = "ax88796",
985 .owner = THIS_MODULE,
986 },
987 .probe = ax_probe,
988 .remove = ax_remove,
989 .suspend = ax_suspend,
990 .resume = ax_resume,
991};
992
Axel Lindb62f682011-11-27 16:44:17 +0000993module_platform_driver(axdrv);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100994
995MODULE_DESCRIPTION("AX88796 10/100 Ethernet platform driver");
996MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
997MODULE_LICENSE("GPL v2");
Kay Sievers72abb462008-04-18 13:50:44 -0700998MODULE_ALIAS("platform:ax88796");