blob: e7cb8c8b9776441a58040c1e2b8ec9bb17cd1f21 [file] [log] [blame]
Ben Dooks825a2ff2007-07-03 16:53:09 +01001/* drivers/net/ax88796.c
2 *
3 * Copyright 2005,2007 Simtec Electronics
4 * Ben Dooks <ben@simtec.co.uk>
5 *
6 * Asix AX88796 10/100 Ethernet controller support
7 * Based on ne.c, by Donald Becker, et-al.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
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
34#include <asm/system.h>
Ben Dooks825a2ff2007-07-03 16:53:09 +010035
Ben Dooks825a2ff2007-07-03 16:53:09 +010036/* Rename the lib8390.c functions to show that they are in this driver */
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010037#define __ei_open ax_ei_open
38#define __ei_close ax_ei_close
39#define __ei_poll ax_ei_poll
Magnus Dammd57bc362009-01-14 21:05:55 -080040#define __ei_start_xmit ax_ei_start_xmit
Ben Dooks825a2ff2007-07-03 16:53:09 +010041#define __ei_tx_timeout ax_ei_tx_timeout
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010042#define __ei_get_stats ax_ei_get_stats
Magnus Dammd57bc362009-01-14 21:05:55 -080043#define __ei_set_multicast_list ax_ei_set_multicast_list
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010044#define __ei_interrupt ax_ei_interrupt
Ben Dooks825a2ff2007-07-03 16:53:09 +010045#define ____alloc_ei_netdev ax__alloc_ei_netdev
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010046#define __NS8390_init ax_NS8390_init
Ben Dooks825a2ff2007-07-03 16:53:09 +010047
48/* force unsigned long back to 'void __iomem *' */
49#define ax_convert_addr(_a) ((void __force __iomem *)(_a))
50
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010051#define ei_inb(_a) readb(ax_convert_addr(_a))
Ben Dooks825a2ff2007-07-03 16:53:09 +010052#define ei_outb(_v, _a) writeb(_v, ax_convert_addr(_a))
53
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010054#define ei_inb_p(_a) ei_inb(_a)
Ben Dooks825a2ff2007-07-03 16:53:09 +010055#define ei_outb_p(_v, _a) ei_outb(_v, _a)
56
57/* define EI_SHIFT() to take into account our register offsets */
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010058#define EI_SHIFT(x) (ei_local->reg_offset[(x)])
Ben Dooks825a2ff2007-07-03 16:53:09 +010059
60/* Ensure we have our RCR base value */
61#define AX88796_PLATFORM
62
63static unsigned char version[] = "ax88796.c: Copyright 2005,2007 Simtec Electronics\n";
64
65#include "lib8390.c"
66
67#define DRV_NAME "ax88796"
68#define DRV_VERSION "1.00"
69
70/* from ne.c */
71#define NE_CMD EI_SHIFT(0x00)
72#define NE_RESET EI_SHIFT(0x1f)
73#define NE_DATAPORT EI_SHIFT(0x10)
74
75#define NE1SM_START_PG 0x20 /* First page of TX buffer */
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010076#define NE1SM_STOP_PG 0x40 /* Last page +1 of RX ring */
Ben Dooks825a2ff2007-07-03 16:53:09 +010077#define NESM_START_PG 0x40 /* First page of TX buffer */
78#define NESM_STOP_PG 0x80 /* Last page +1 of RX ring */
79
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +010080#define AX_GPOC_PPDSET BIT(6)
81
Ben Dooks825a2ff2007-07-03 16:53:09 +010082/* device private data */
83
84struct ax_device {
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +010085 struct mii_bus *mii_bus;
86 struct mdiobb_ctrl bb_ctrl;
87 struct phy_device *phy_dev;
88 void __iomem *addr_memr;
89 u8 reg_memr;
90 int link;
91 int speed;
92 int duplex;
Ben Dooks825a2ff2007-07-03 16:53:09 +010093
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010094 void __iomem *map2;
Marc Kleine-Buddea54dc582011-02-21 10:43:31 +010095 const struct ax_plat_data *plat;
Ben Dooks825a2ff2007-07-03 16:53:09 +010096
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010097 unsigned char running;
98 unsigned char resume_open;
99 unsigned int irqflags;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100100
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100101 u32 reg_offsets[0x20];
Ben Dooks825a2ff2007-07-03 16:53:09 +0100102};
103
104static inline struct ax_device *to_ax_dev(struct net_device *dev)
105{
106 struct ei_device *ei_local = netdev_priv(dev);
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100107 return (struct ax_device *)(ei_local + 1);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100108}
109
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100110/*
111 * ax_initial_check
Ben Dooks825a2ff2007-07-03 16:53:09 +0100112 *
113 * do an initial probe for the card to check wether it exists
114 * and is functional
115 */
Ben Dooks825a2ff2007-07-03 16:53:09 +0100116static int ax_initial_check(struct net_device *dev)
117{
118 struct ei_device *ei_local = netdev_priv(dev);
119 void __iomem *ioaddr = ei_local->mem;
120 int reg0;
121 int regd;
122
123 reg0 = ei_inb(ioaddr);
124 if (reg0 == 0xFF)
125 return -ENODEV;
126
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100127 ei_outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP, ioaddr + E8390_CMD);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100128 regd = ei_inb(ioaddr + 0x0d);
129 ei_outb(0xff, ioaddr + 0x0d);
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100130 ei_outb(E8390_NODMA + E8390_PAGE0, ioaddr + E8390_CMD);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100131 ei_inb(ioaddr + EN0_COUNTER0); /* Clear the counter by reading. */
132 if (ei_inb(ioaddr + EN0_COUNTER0) != 0) {
133 ei_outb(reg0, ioaddr);
134 ei_outb(regd, ioaddr + 0x0d); /* Restore the old values. */
135 return -ENODEV;
136 }
137
138 return 0;
139}
140
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100141/*
142 * Hard reset the card. This used to pause for the same period that a
143 * 8390 reset command required, but that shouldn't be necessary.
144 */
Ben Dooks825a2ff2007-07-03 16:53:09 +0100145static void ax_reset_8390(struct net_device *dev)
146{
147 struct ei_device *ei_local = netdev_priv(dev);
148 unsigned long reset_start_time = jiffies;
149 void __iomem *addr = (void __iomem *)dev->base_addr;
150
151 if (ei_debug > 1)
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100152 netdev_dbg(dev, "resetting the 8390 t=%ld\n", jiffies);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100153
154 ei_outb(ei_inb(addr + NE_RESET), addr + NE_RESET);
155
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100156 ei_local->txing = 0;
157 ei_local->dmaing = 0;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100158
159 /* This check _should_not_ be necessary, omit eventually. */
160 while ((ei_inb(addr + EN0_ISR) & ENISR_RESET) == 0) {
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100161 if (jiffies - reset_start_time > 2 * HZ / 100) {
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100162 netdev_warn(dev, "%s: did not complete.\n", __func__);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100163 break;
164 }
165 }
166
167 ei_outb(ENISR_RESET, addr + EN0_ISR); /* Ack intr. */
168}
169
170
171static void ax_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
172 int ring_page)
173{
174 struct ei_device *ei_local = netdev_priv(dev);
175 void __iomem *nic_base = ei_local->mem;
176
177 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100178 if (ei_local->dmaing) {
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100179 netdev_err(dev, "DMAing conflict in %s "
Ben Dooks237c5e82008-01-31 11:25:31 +0000180 "[DMAstat:%d][irqlock:%d].\n",
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100181 __func__,
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100182 ei_local->dmaing, ei_local->irqlock);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100183 return;
184 }
185
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100186 ei_local->dmaing |= 0x01;
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100187 ei_outb(E8390_NODMA + E8390_PAGE0 + E8390_START, nic_base + NE_CMD);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100188 ei_outb(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
189 ei_outb(0, nic_base + EN0_RCNTHI);
190 ei_outb(0, nic_base + EN0_RSARLO); /* On page boundary */
191 ei_outb(ring_page, nic_base + EN0_RSARHI);
192 ei_outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
193
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100194 if (ei_local->word16)
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100195 readsw(nic_base + NE_DATAPORT, hdr,
196 sizeof(struct e8390_pkt_hdr) >> 1);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100197 else
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100198 readsb(nic_base + NE_DATAPORT, hdr,
199 sizeof(struct e8390_pkt_hdr));
Ben Dooks825a2ff2007-07-03 16:53:09 +0100200
201 ei_outb(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100202 ei_local->dmaing &= ~0x01;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100203
204 le16_to_cpus(&hdr->count);
205}
206
207
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100208/*
209 * Block input and output, similar to the Crynwr packet driver. If
210 * you are porting to a new ethercard, look at the packet driver
211 * source for hints. The NEx000 doesn't share the on-board packet
212 * memory -- you have to put the packet out through the "remote DMA"
213 * dataport using ei_outb.
214 */
Ben Dooks825a2ff2007-07-03 16:53:09 +0100215static void ax_block_input(struct net_device *dev, int count,
216 struct sk_buff *skb, int ring_offset)
217{
218 struct ei_device *ei_local = netdev_priv(dev);
219 void __iomem *nic_base = ei_local->mem;
220 char *buf = skb->data;
221
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100222 if (ei_local->dmaing) {
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100223 netdev_err(dev,
224 "DMAing conflict in %s "
Ben Dooks825a2ff2007-07-03 16:53:09 +0100225 "[DMAstat:%d][irqlock:%d].\n",
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100226 __func__,
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100227 ei_local->dmaing, ei_local->irqlock);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100228 return;
229 }
230
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100231 ei_local->dmaing |= 0x01;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100232
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100233 ei_outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base + NE_CMD);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100234 ei_outb(count & 0xff, nic_base + EN0_RCNTLO);
235 ei_outb(count >> 8, nic_base + EN0_RCNTHI);
236 ei_outb(ring_offset & 0xff, nic_base + EN0_RSARLO);
237 ei_outb(ring_offset >> 8, nic_base + EN0_RSARHI);
238 ei_outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
239
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100240 if (ei_local->word16) {
Ben Dooks825a2ff2007-07-03 16:53:09 +0100241 readsw(nic_base + NE_DATAPORT, buf, count >> 1);
242 if (count & 0x01)
243 buf[count-1] = ei_inb(nic_base + NE_DATAPORT);
244
245 } else {
246 readsb(nic_base + NE_DATAPORT, buf, count);
247 }
248
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100249 ei_local->dmaing &= ~1;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100250}
251
252static void ax_block_output(struct net_device *dev, int count,
253 const unsigned char *buf, const int start_page)
254{
255 struct ei_device *ei_local = netdev_priv(dev);
256 void __iomem *nic_base = ei_local->mem;
257 unsigned long dma_start;
258
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100259 /*
260 * Round the count up for word writes. Do we need to do this?
261 * What effect will an odd byte count have on the 8390? I
262 * should check someday.
263 */
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100264 if (ei_local->word16 && (count & 0x01))
Ben Dooks825a2ff2007-07-03 16:53:09 +0100265 count++;
266
267 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100268 if (ei_local->dmaing) {
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100269 netdev_err(dev, "DMAing conflict in %s."
Ben Dooks825a2ff2007-07-03 16:53:09 +0100270 "[DMAstat:%d][irqlock:%d]\n",
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100271 __func__,
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100272 ei_local->dmaing, ei_local->irqlock);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100273 return;
274 }
275
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100276 ei_local->dmaing |= 0x01;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100277 /* We should already be in page 0, but to be safe... */
278 ei_outb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
279
280 ei_outb(ENISR_RDC, nic_base + EN0_ISR);
281
282 /* Now the normal output. */
283 ei_outb(count & 0xff, nic_base + EN0_RCNTLO);
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100284 ei_outb(count >> 8, nic_base + EN0_RCNTHI);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100285 ei_outb(0x00, nic_base + EN0_RSARLO);
286 ei_outb(start_page, nic_base + EN0_RSARHI);
287
288 ei_outb(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100289 if (ei_local->word16)
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100290 writesw(nic_base + NE_DATAPORT, buf, count >> 1);
291 else
Ben Dooks825a2ff2007-07-03 16:53:09 +0100292 writesb(nic_base + NE_DATAPORT, buf, count);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100293
294 dma_start = jiffies;
295
296 while ((ei_inb(nic_base + EN0_ISR) & ENISR_RDC) == 0) {
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100297 if (jiffies - dma_start > 2 * HZ / 100) { /* 20ms */
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100298 netdev_warn(dev, "timeout waiting for Tx RDC.\n");
Ben Dooks825a2ff2007-07-03 16:53:09 +0100299 ax_reset_8390(dev);
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100300 ax_NS8390_init(dev, 1);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100301 break;
302 }
303 }
304
305 ei_outb(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100306 ei_local->dmaing &= ~0x01;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100307}
308
309/* definitions for accessing MII/EEPROM interface */
310
311#define AX_MEMR EI_SHIFT(0x14)
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100312#define AX_MEMR_MDC BIT(0)
313#define AX_MEMR_MDIR BIT(1)
314#define AX_MEMR_MDI BIT(2)
315#define AX_MEMR_MDO BIT(3)
316#define AX_MEMR_EECS BIT(4)
317#define AX_MEMR_EEI BIT(5)
318#define AX_MEMR_EEO BIT(6)
319#define AX_MEMR_EECLK BIT(7)
Ben Dooks825a2ff2007-07-03 16:53:09 +0100320
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100321static void ax_handle_link_change(struct net_device *dev)
Ben Dooks825a2ff2007-07-03 16:53:09 +0100322{
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100323 struct ax_device *ax = to_ax_dev(dev);
324 struct phy_device *phy_dev = ax->phy_dev;
325 int status_change = 0;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100326
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100327 if (phy_dev->link && ((ax->speed != phy_dev->speed) ||
328 (ax->duplex != phy_dev->duplex))) {
Ben Dooks825a2ff2007-07-03 16:53:09 +0100329
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100330 ax->speed = phy_dev->speed;
331 ax->duplex = phy_dev->duplex;
332 status_change = 1;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100333 }
334
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100335 if (phy_dev->link != ax->link) {
336 if (!phy_dev->link) {
337 ax->speed = 0;
338 ax->duplex = -1;
339 }
340 ax->link = phy_dev->link;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100341
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100342 status_change = 1;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100343 }
344
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100345 if (status_change)
346 phy_print_status(phy_dev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100347}
348
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100349static int ax_mii_probe(struct net_device *dev)
Ben Dooks825a2ff2007-07-03 16:53:09 +0100350{
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100351 struct ax_device *ax = to_ax_dev(dev);
352 struct phy_device *phy_dev = NULL;
353 int ret;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100354
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100355 /* find the first phy */
356 phy_dev = phy_find_first(ax->mii_bus);
357 if (!phy_dev) {
358 netdev_err(dev, "no PHY found\n");
359 return -ENODEV;
360 }
361
362 ret = phy_connect_direct(dev, phy_dev, ax_handle_link_change, 0,
363 PHY_INTERFACE_MODE_MII);
364 if (ret) {
365 netdev_err(dev, "Could not attach to PHY\n");
366 return ret;
367 }
368
369 /* mask with MAC supported features */
370 phy_dev->supported &= PHY_BASIC_FEATURES;
371 phy_dev->advertising = phy_dev->supported;
372
373 ax->phy_dev = phy_dev;
374
375 netdev_info(dev, "PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
376 phy_dev->drv->name, dev_name(&phy_dev->dev), phy_dev->irq);
377
378 return 0;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100379}
380
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100381static void ax_phy_switch(struct net_device *dev, int on)
Ben Dooks825a2ff2007-07-03 16:53:09 +0100382{
Joe Perchesece49152010-11-15 11:12:31 +0000383 struct ei_device *ei_local = netdev_priv(dev);
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100384 struct ax_device *ax = to_ax_dev(dev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100385
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100386 u8 reg_gpoc = ax->plat->gpoc_val;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100387
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100388 if (!!on)
389 reg_gpoc &= ~AX_GPOC_PPDSET;
390 else
391 reg_gpoc |= AX_GPOC_PPDSET;
392
393 ei_outb(reg_gpoc, ei_local->mem + EI_SHIFT(0x17));
Ben Dooks825a2ff2007-07-03 16:53:09 +0100394}
395
396static int ax_open(struct net_device *dev)
397{
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100398 struct ax_device *ax = to_ax_dev(dev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100399 int ret;
400
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100401 netdev_dbg(dev, "open\n");
Ben Dooks825a2ff2007-07-03 16:53:09 +0100402
Daniel Mack47cb0352009-03-24 23:31:22 -0700403 ret = request_irq(dev->irq, ax_ei_interrupt, ax->irqflags,
404 dev->name, dev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100405 if (ret)
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100406 goto failed_request_irq;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100407
408 /* turn the phy on (if turned off) */
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100409 ax_phy_switch(dev, 1);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100410
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100411 ret = ax_mii_probe(dev);
412 if (ret)
413 goto failed_mii_probe;
414 phy_start(ax->phy_dev);
415
416 ret = ax_ei_open(dev);
417 if (ret)
418 goto failed_ax_ei_open;
419
Ben Dooks825a2ff2007-07-03 16:53:09 +0100420 ax->running = 1;
421
Ben Dooks825a2ff2007-07-03 16:53:09 +0100422 return 0;
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100423
424 failed_ax_ei_open:
425 phy_disconnect(ax->phy_dev);
426 failed_mii_probe:
427 ax_phy_switch(dev, 0);
428 free_irq(dev->irq, dev);
429 failed_request_irq:
430 return ret;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100431}
432
433static int ax_close(struct net_device *dev)
434{
435 struct ax_device *ax = to_ax_dev(dev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100436
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100437 netdev_dbg(dev, "close\n");
Ben Dooks825a2ff2007-07-03 16:53:09 +0100438
Ben Dooks825a2ff2007-07-03 16:53:09 +0100439 ax->running = 0;
440 wmb();
441
Ben Dooks825a2ff2007-07-03 16:53:09 +0100442 ax_ei_close(dev);
443
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100444 /* turn the phy off */
445 ax_phy_switch(dev, 0);
446 phy_disconnect(ax->phy_dev);
447
Ben Dooks825a2ff2007-07-03 16:53:09 +0100448 free_irq(dev->irq, dev);
449 return 0;
450}
451
452static int ax_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
453{
454 struct ax_device *ax = to_ax_dev(dev);
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100455 struct phy_device *phy_dev = ax->phy_dev;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100456
457 if (!netif_running(dev))
458 return -EINVAL;
459
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100460 if (!phy_dev)
461 return -ENODEV;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100462
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100463 return phy_mii_ioctl(phy_dev, req, cmd);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100464}
465
466/* ethtool ops */
467
468static void ax_get_drvinfo(struct net_device *dev,
469 struct ethtool_drvinfo *info)
470{
Marc Kleine-Budde0f442f52011-02-20 19:13:20 +0100471 struct platform_device *pdev = to_platform_device(dev->dev.parent);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100472
473 strcpy(info->driver, DRV_NAME);
474 strcpy(info->version, DRV_VERSION);
Marc Kleine-Budde0f442f52011-02-20 19:13:20 +0100475 strcpy(info->bus_info, pdev->name);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100476}
477
478static int ax_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
479{
480 struct ax_device *ax = to_ax_dev(dev);
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100481 struct phy_device *phy_dev = ax->phy_dev;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100482
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100483 if (!phy_dev)
484 return -ENODEV;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100485
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100486 return phy_ethtool_gset(phy_dev, cmd);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100487}
488
489static int ax_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
490{
491 struct ax_device *ax = to_ax_dev(dev);
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100492 struct phy_device *phy_dev = ax->phy_dev;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100493
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100494 if (!phy_dev)
495 return -ENODEV;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100496
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100497 return phy_ethtool_sset(phy_dev, cmd);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100498}
499
500static const struct ethtool_ops ax_ethtool_ops = {
501 .get_drvinfo = ax_get_drvinfo,
502 .get_settings = ax_get_settings,
503 .set_settings = ax_set_settings,
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100504 .get_link = ethtool_op_get_link,
Ben Dooks825a2ff2007-07-03 16:53:09 +0100505};
506
Magnus Damm89e536a2007-09-28 22:42:16 -0700507#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
Magnus Dammd57bc362009-01-14 21:05:55 -0800538static 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_multicast_list = ax_ei_set_multicast_list,
547 .ndo_validate_addr = eth_validate_addr,
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100548 .ndo_set_mac_address = eth_mac_addr,
Magnus Dammd57bc362009-01-14 21:05:55 -0800549 .ndo_change_mtu = eth_change_mtu,
550#ifdef CONFIG_NET_POLL_CONTROLLER
551 .ndo_poll_controller = ax_ei_poll,
552#endif
553};
554
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100555static 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
Ben Dooks825a2ff2007-07-03 16:53:09 +0100607/* setup code */
608
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100609static 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, "%x", pdev->id);
627
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 */
738 for (i = 0; i < ETHER_ADDR_LEN; i++)
739 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)
745 memcpy(dev->dev_addr, ax->plat->mac_addr,
746 ETHER_ADDR_LEN);
747
Ben Dooks825a2ff2007-07-03 16:53:09 +0100748 ax_reset_8390(dev);
749
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100750 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;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100755
756#ifdef PACKETBUF_MEMSIZE
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100757 /* Allow the packet buffer size to be overridden by know-it-alls. */
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100758 ei_local->stop_page = ei_local->tx_start_page + PACKETBUF_MEMSIZE;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100759#endif
760
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100761 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;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100766
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100767 dev->netdev_ops = &ax_netdev_ops;
768 dev->ethtool_ops = &ax_ethtool_ops;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100769
Marc Kleine-Buddef6d7f2c2011-02-21 12:41:55 +0100770 ret = ax_mii_init(dev);
771 if (ret)
772 goto out_irq;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100773
Ben Dooks825a2ff2007-07-03 16:53:09 +0100774 ax_NS8390_init(dev, 0);
775
Ben Dooks825a2ff2007-07-03 16:53:09 +0100776 ret = register_netdev(dev);
777 if (ret)
778 goto out_irq;
779
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100780 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
Ben Dooks825a2ff2007-07-03 16:53:09 +0100784 return 0;
785
786 out_irq:
787 /* cleanup irq */
788 free_irq(dev->irq, dev);
789 err_out:
790 return ret;
791}
792
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100793static int ax_remove(struct platform_device *pdev)
Ben Dooks825a2ff2007-07-03 16:53:09 +0100794{
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100795 struct net_device *dev = platform_get_drvdata(pdev);
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100796 struct ei_device *ei_local = netdev_priv(dev);
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100797 struct ax_device *ax = to_ax_dev(dev);
798 struct resource *mem;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100799
800 unregister_netdev(dev);
801 free_irq(dev->irq, dev);
802
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100803 iounmap(ei_local->mem);
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100804 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
805 release_mem_region(mem->start, resource_size(mem));
Ben Dooks825a2ff2007-07-03 16:53:09 +0100806
807 if (ax->map2) {
808 iounmap(ax->map2);
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100809 mem = platform_get_resource(pdev, IORESOURCE_MEM, 1);
810 release_mem_region(mem->start, resource_size(mem));
Ben Dooks825a2ff2007-07-03 16:53:09 +0100811 }
812
813 free_netdev(dev);
814
815 return 0;
816}
817
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100818/*
819 * ax_probe
Ben Dooks825a2ff2007-07-03 16:53:09 +0100820 *
821 * This is the entry point when the platform device system uses to
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100822 * notify us of a new device to attach to. Allocate memory, find the
823 * resources and information passed, and map the necessary registers.
824 */
Ben Dooks825a2ff2007-07-03 16:53:09 +0100825static int ax_probe(struct platform_device *pdev)
826{
827 struct net_device *dev;
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100828 struct ei_device *ei_local;
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100829 struct ax_device *ax;
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100830 struct resource *irq, *mem, *mem2;
831 resource_size_t mem_size, mem2_size = 0;
Daniel Mack47cb0352009-03-24 23:31:22 -0700832 int ret = 0;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100833
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 */
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100839 SET_NETDEV_DEV(dev, &pdev->dev);
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100840 ei_local = netdev_priv(dev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100841 ax = to_ax_dev(dev);
842
Ben Dooks825a2ff2007-07-03 16:53:09 +0100843 ax->plat = pdev->dev.platform_data;
844 platform_set_drvdata(pdev, dev);
845
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100846 ei_local->rxcr_base = ax->plat->rcr_val;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100847
848 /* find the platform resources */
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100849 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
850 if (!irq) {
Ben Dooks825a2ff2007-07-03 16:53:09 +0100851 dev_err(&pdev->dev, "no IRQ specified\n");
Julia Lawall13eea192010-10-18 04:11:14 +0000852 ret = -ENXIO;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100853 goto exit_mem;
854 }
Daniel Mack47cb0352009-03-24 23:31:22 -0700855
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100856 dev->irq = irq->start;
857 ax->irqflags = irq->flags & IRQF_TRIGGER_MASK;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100858
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100859 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
860 if (!mem) {
Ben Dooks825a2ff2007-07-03 16:53:09 +0100861 dev_err(&pdev->dev, "no MEM specified\n");
862 ret = -ENXIO;
863 goto exit_mem;
864 }
865
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100866 mem_size = resource_size(mem);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100867
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100868 /*
869 * setup the register offsets from either the platform data or
870 * by using the size of the resource provided
871 */
Ben Dooks825a2ff2007-07-03 16:53:09 +0100872 if (ax->plat->reg_offsets)
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100873 ei_local->reg_offset = ax->plat->reg_offsets;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100874 else {
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100875 ei_local->reg_offset = ax->reg_offsets;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100876 for (ret = 0; ret < 0x18; ret++)
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100877 ax->reg_offsets[ret] = (mem_size / 0x18) * ret;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100878 }
879
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100880 if (!request_mem_region(mem->start, mem_size, pdev->name)) {
Ben Dooks825a2ff2007-07-03 16:53:09 +0100881 dev_err(&pdev->dev, "cannot reserve registers\n");
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100882 ret = -ENXIO;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100883 goto exit_mem;
884 }
885
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100886 ei_local->mem = ioremap(mem->start, mem_size);
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100887 dev->base_addr = (unsigned long)ei_local->mem;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100888
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100889 if (ei_local->mem == NULL) {
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100890 dev_err(&pdev->dev, "Cannot ioremap area %pR\n", mem);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100891
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100892 ret = -ENXIO;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100893 goto exit_req;
894 }
895
896 /* look for reset area */
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100897 mem2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
898 if (!mem2) {
Ben Dooks825a2ff2007-07-03 16:53:09 +0100899 if (!ax->plat->reg_offsets) {
900 for (ret = 0; ret < 0x20; ret++)
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100901 ax->reg_offsets[ret] = (mem_size / 0x20) * ret;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100902 }
Ben Dooks825a2ff2007-07-03 16:53:09 +0100903 } else {
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100904 mem2_size = resource_size(mem2);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100905
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100906 if (!request_mem_region(mem2->start, mem2_size, pdev->name)) {
Ben Dooks825a2ff2007-07-03 16:53:09 +0100907 dev_err(&pdev->dev, "cannot reserve registers\n");
908 ret = -ENXIO;
909 goto exit_mem1;
910 }
911
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100912 ax->map2 = ioremap(mem2->start, mem2_size);
913 if (!ax->map2) {
Joe Perches898eb712007-10-18 03:06:30 -0700914 dev_err(&pdev->dev, "cannot map reset register\n");
Ben Dooks825a2ff2007-07-03 16:53:09 +0100915 ret = -ENXIO;
916 goto exit_mem2;
917 }
918
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100919 ei_local->reg_offset[0x1f] = ax->map2 - ei_local->mem;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100920 }
921
922 /* got resources, now initialise and register device */
Marc Kleine-Budde1cbece32011-02-19 23:07:09 +0100923 ret = ax_init_dev(dev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100924 if (!ret)
925 return 0;
926
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100927 if (!ax->map2)
Ben Dooks825a2ff2007-07-03 16:53:09 +0100928 goto exit_mem1;
929
930 iounmap(ax->map2);
931
932 exit_mem2:
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100933 release_mem_region(mem2->start, mem2_size);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100934
935 exit_mem1:
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100936 iounmap(ei_local->mem);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100937
938 exit_req:
Marc Kleine-Buddec9218c32011-02-21 11:24:39 +0100939 release_mem_region(mem->start, mem_size);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100940
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);
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100953 struct ax_device *ax = to_ax_dev(ndev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100954
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);
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100966 struct ax_device *ax = to_ax_dev(ndev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100967
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
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100980#define ax_resume NULL
Ben Dooks825a2ff2007-07-03 16:53:09 +0100981#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
994static int __init axdrv_init(void)
995{
996 return platform_driver_register(&axdrv);
997}
998
999static void __exit axdrv_exit(void)
1000{
1001 platform_driver_unregister(&axdrv);
1002}
1003
1004module_init(axdrv_init);
1005module_exit(axdrv_exit);
1006
1007MODULE_DESCRIPTION("AX88796 10/100 Ethernet platform driver");
1008MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
1009MODULE_LICENSE("GPL v2");
Kay Sievers72abb462008-04-18 13:50:44 -07001010MODULE_ALIAS("platform:ax88796");