blob: 1370cac6d6c0cf9c07c582d10f3d5cb2f84c4808 [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>
27#include <linux/mii.h>
Magnus Damm89e536a2007-09-28 22:42:16 -070028#include <linux/eeprom_93cx6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Ben Dooks825a2ff2007-07-03 16:53:09 +010030
31#include <net/ax88796.h>
32
33#include <asm/system.h>
Ben Dooks825a2ff2007-07-03 16:53:09 +010034
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010035static int phy_debug;
Ben Dooks825a2ff2007-07-03 16:53:09 +010036
37/* Rename the lib8390.c functions to show that they are in this driver */
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010038#define __ei_open ax_ei_open
39#define __ei_close ax_ei_close
40#define __ei_poll ax_ei_poll
Magnus Dammd57bc362009-01-14 21:05:55 -080041#define __ei_start_xmit ax_ei_start_xmit
Ben Dooks825a2ff2007-07-03 16:53:09 +010042#define __ei_tx_timeout ax_ei_tx_timeout
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010043#define __ei_get_stats ax_ei_get_stats
Magnus Dammd57bc362009-01-14 21:05:55 -080044#define __ei_set_multicast_list ax_ei_set_multicast_list
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010045#define __ei_interrupt ax_ei_interrupt
Ben Dooks825a2ff2007-07-03 16:53:09 +010046#define ____alloc_ei_netdev ax__alloc_ei_netdev
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010047#define __NS8390_init ax_NS8390_init
Ben Dooks825a2ff2007-07-03 16:53:09 +010048
49/* force unsigned long back to 'void __iomem *' */
50#define ax_convert_addr(_a) ((void __force __iomem *)(_a))
51
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010052#define ei_inb(_a) readb(ax_convert_addr(_a))
Ben Dooks825a2ff2007-07-03 16:53:09 +010053#define ei_outb(_v, _a) writeb(_v, ax_convert_addr(_a))
54
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010055#define ei_inb_p(_a) ei_inb(_a)
Ben Dooks825a2ff2007-07-03 16:53:09 +010056#define ei_outb_p(_v, _a) ei_outb(_v, _a)
57
58/* define EI_SHIFT() to take into account our register offsets */
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010059#define EI_SHIFT(x) (ei_local->reg_offset[(x)])
Ben Dooks825a2ff2007-07-03 16:53:09 +010060
61/* Ensure we have our RCR base value */
62#define AX88796_PLATFORM
63
64static unsigned char version[] = "ax88796.c: Copyright 2005,2007 Simtec Electronics\n";
65
66#include "lib8390.c"
67
68#define DRV_NAME "ax88796"
69#define DRV_VERSION "1.00"
70
71/* from ne.c */
72#define NE_CMD EI_SHIFT(0x00)
73#define NE_RESET EI_SHIFT(0x1f)
74#define NE_DATAPORT EI_SHIFT(0x10)
75
76#define NE1SM_START_PG 0x20 /* First page of TX buffer */
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010077#define NE1SM_STOP_PG 0x40 /* Last page +1 of RX ring */
Ben Dooks825a2ff2007-07-03 16:53:09 +010078#define NESM_START_PG 0x40 /* First page of TX buffer */
79#define NESM_STOP_PG 0x80 /* Last page +1 of RX ring */
80
81/* device private data */
82
83struct ax_device {
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010084 struct timer_list mii_timer;
85 spinlock_t mii_lock;
86 struct mii_if_info mii;
Ben Dooks825a2ff2007-07-03 16:53:09 +010087
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010088 u32 msg_enable;
89 void __iomem *map2;
90 struct platform_device *dev;
91 struct resource *mem;
92 struct resource *mem2;
93 struct ax_plat_data *plat;
Ben Dooks825a2ff2007-07-03 16:53:09 +010094
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010095 unsigned char running;
96 unsigned char resume_open;
97 unsigned int irqflags;
Ben Dooks825a2ff2007-07-03 16:53:09 +010098
Marc Kleine-Budde9f072422011-02-19 17:07:40 +010099 u32 reg_offsets[0x20];
Ben Dooks825a2ff2007-07-03 16:53:09 +0100100};
101
102static inline struct ax_device *to_ax_dev(struct net_device *dev)
103{
104 struct ei_device *ei_local = netdev_priv(dev);
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100105 return (struct ax_device *)(ei_local + 1);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100106}
107
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100108/*
109 * ax_initial_check
Ben Dooks825a2ff2007-07-03 16:53:09 +0100110 *
111 * do an initial probe for the card to check wether it exists
112 * and is functional
113 */
Ben Dooks825a2ff2007-07-03 16:53:09 +0100114static int ax_initial_check(struct net_device *dev)
115{
116 struct ei_device *ei_local = netdev_priv(dev);
117 void __iomem *ioaddr = ei_local->mem;
118 int reg0;
119 int regd;
120
121 reg0 = ei_inb(ioaddr);
122 if (reg0 == 0xFF)
123 return -ENODEV;
124
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100125 ei_outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP, ioaddr + E8390_CMD);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100126 regd = ei_inb(ioaddr + 0x0d);
127 ei_outb(0xff, ioaddr + 0x0d);
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100128 ei_outb(E8390_NODMA + E8390_PAGE0, ioaddr + E8390_CMD);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100129 ei_inb(ioaddr + EN0_COUNTER0); /* Clear the counter by reading. */
130 if (ei_inb(ioaddr + EN0_COUNTER0) != 0) {
131 ei_outb(reg0, ioaddr);
132 ei_outb(regd, ioaddr + 0x0d); /* Restore the old values. */
133 return -ENODEV;
134 }
135
136 return 0;
137}
138
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100139/*
140 * Hard reset the card. This used to pause for the same period that a
141 * 8390 reset command required, but that shouldn't be necessary.
142 */
Ben Dooks825a2ff2007-07-03 16:53:09 +0100143static void ax_reset_8390(struct net_device *dev)
144{
145 struct ei_device *ei_local = netdev_priv(dev);
146 unsigned long reset_start_time = jiffies;
147 void __iomem *addr = (void __iomem *)dev->base_addr;
148
149 if (ei_debug > 1)
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100150 netdev_dbg(dev, "resetting the 8390 t=%ld\n", jiffies);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100151
152 ei_outb(ei_inb(addr + NE_RESET), addr + NE_RESET);
153
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100154 ei_local->txing = 0;
155 ei_local->dmaing = 0;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100156
157 /* This check _should_not_ be necessary, omit eventually. */
158 while ((ei_inb(addr + EN0_ISR) & ENISR_RESET) == 0) {
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100159 if (jiffies - reset_start_time > 2 * HZ / 100) {
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100160 netdev_warn(dev, "%s: did not complete.\n", __func__);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100161 break;
162 }
163 }
164
165 ei_outb(ENISR_RESET, addr + EN0_ISR); /* Ack intr. */
166}
167
168
169static void ax_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
170 int ring_page)
171{
172 struct ei_device *ei_local = netdev_priv(dev);
173 void __iomem *nic_base = ei_local->mem;
174
175 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100176 if (ei_local->dmaing) {
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100177 netdev_err(dev, "DMAing conflict in %s "
Ben Dooks237c5e82008-01-31 11:25:31 +0000178 "[DMAstat:%d][irqlock:%d].\n",
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100179 __func__,
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100180 ei_local->dmaing, ei_local->irqlock);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100181 return;
182 }
183
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100184 ei_local->dmaing |= 0x01;
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100185 ei_outb(E8390_NODMA + E8390_PAGE0 + E8390_START, nic_base + NE_CMD);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100186 ei_outb(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
187 ei_outb(0, nic_base + EN0_RCNTHI);
188 ei_outb(0, nic_base + EN0_RSARLO); /* On page boundary */
189 ei_outb(ring_page, nic_base + EN0_RSARHI);
190 ei_outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
191
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100192 if (ei_local->word16)
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100193 readsw(nic_base + NE_DATAPORT, hdr,
194 sizeof(struct e8390_pkt_hdr) >> 1);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100195 else
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100196 readsb(nic_base + NE_DATAPORT, hdr,
197 sizeof(struct e8390_pkt_hdr));
Ben Dooks825a2ff2007-07-03 16:53:09 +0100198
199 ei_outb(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100200 ei_local->dmaing &= ~0x01;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100201
202 le16_to_cpus(&hdr->count);
203}
204
205
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100206/*
207 * Block input and output, similar to the Crynwr packet driver. If
208 * you are porting to a new ethercard, look at the packet driver
209 * source for hints. The NEx000 doesn't share the on-board packet
210 * memory -- you have to put the packet out through the "remote DMA"
211 * dataport using ei_outb.
212 */
Ben Dooks825a2ff2007-07-03 16:53:09 +0100213static void ax_block_input(struct net_device *dev, int count,
214 struct sk_buff *skb, int ring_offset)
215{
216 struct ei_device *ei_local = netdev_priv(dev);
217 void __iomem *nic_base = ei_local->mem;
218 char *buf = skb->data;
219
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100220 if (ei_local->dmaing) {
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100221 netdev_err(dev,
222 "DMAing conflict in %s "
Ben Dooks825a2ff2007-07-03 16:53:09 +0100223 "[DMAstat:%d][irqlock:%d].\n",
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100224 __func__,
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100225 ei_local->dmaing, ei_local->irqlock);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100226 return;
227 }
228
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100229 ei_local->dmaing |= 0x01;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100230
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100231 ei_outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base + NE_CMD);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100232 ei_outb(count & 0xff, nic_base + EN0_RCNTLO);
233 ei_outb(count >> 8, nic_base + EN0_RCNTHI);
234 ei_outb(ring_offset & 0xff, nic_base + EN0_RSARLO);
235 ei_outb(ring_offset >> 8, nic_base + EN0_RSARHI);
236 ei_outb(E8390_RREAD+E8390_START, nic_base + NE_CMD);
237
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100238 if (ei_local->word16) {
Ben Dooks825a2ff2007-07-03 16:53:09 +0100239 readsw(nic_base + NE_DATAPORT, buf, count >> 1);
240 if (count & 0x01)
241 buf[count-1] = ei_inb(nic_base + NE_DATAPORT);
242
243 } else {
244 readsb(nic_base + NE_DATAPORT, buf, count);
245 }
246
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100247 ei_local->dmaing &= ~1;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100248}
249
250static void ax_block_output(struct net_device *dev, int count,
251 const unsigned char *buf, const int start_page)
252{
253 struct ei_device *ei_local = netdev_priv(dev);
254 void __iomem *nic_base = ei_local->mem;
255 unsigned long dma_start;
256
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100257 /*
258 * Round the count up for word writes. Do we need to do this?
259 * What effect will an odd byte count have on the 8390? I
260 * should check someday.
261 */
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100262 if (ei_local->word16 && (count & 0x01))
Ben Dooks825a2ff2007-07-03 16:53:09 +0100263 count++;
264
265 /* This *shouldn't* happen. If it does, it's the last thing you'll see */
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100266 if (ei_local->dmaing) {
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100267 netdev_err(dev, "DMAing conflict in %s."
Ben Dooks825a2ff2007-07-03 16:53:09 +0100268 "[DMAstat:%d][irqlock:%d]\n",
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100269 __func__,
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100270 ei_local->dmaing, ei_local->irqlock);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100271 return;
272 }
273
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100274 ei_local->dmaing |= 0x01;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100275 /* We should already be in page 0, but to be safe... */
276 ei_outb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD);
277
278 ei_outb(ENISR_RDC, nic_base + EN0_ISR);
279
280 /* Now the normal output. */
281 ei_outb(count & 0xff, nic_base + EN0_RCNTLO);
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100282 ei_outb(count >> 8, nic_base + EN0_RCNTHI);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100283 ei_outb(0x00, nic_base + EN0_RSARLO);
284 ei_outb(start_page, nic_base + EN0_RSARHI);
285
286 ei_outb(E8390_RWRITE+E8390_START, nic_base + NE_CMD);
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100287 if (ei_local->word16)
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100288 writesw(nic_base + NE_DATAPORT, buf, count >> 1);
289 else
Ben Dooks825a2ff2007-07-03 16:53:09 +0100290 writesb(nic_base + NE_DATAPORT, buf, count);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100291
292 dma_start = jiffies;
293
294 while ((ei_inb(nic_base + EN0_ISR) & ENISR_RDC) == 0) {
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100295 if (jiffies - dma_start > 2 * HZ / 100) { /* 20ms */
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100296 netdev_warn(dev, "timeout waiting for Tx RDC.\n");
Ben Dooks825a2ff2007-07-03 16:53:09 +0100297 ax_reset_8390(dev);
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100298 ax_NS8390_init(dev, 1);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100299 break;
300 }
301 }
302
303 ei_outb(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100304 ei_local->dmaing &= ~0x01;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100305}
306
307/* definitions for accessing MII/EEPROM interface */
308
309#define AX_MEMR EI_SHIFT(0x14)
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100310#define AX_MEMR_MDC BIT(0)
311#define AX_MEMR_MDIR BIT(1)
312#define AX_MEMR_MDI BIT(2)
313#define AX_MEMR_MDO BIT(3)
314#define AX_MEMR_EECS BIT(4)
315#define AX_MEMR_EEI BIT(5)
316#define AX_MEMR_EEO BIT(6)
317#define AX_MEMR_EECLK BIT(7)
Ben Dooks825a2ff2007-07-03 16:53:09 +0100318
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100319/*
320 * ax_mii_ei_outbits
Ben Dooks825a2ff2007-07-03 16:53:09 +0100321 *
322 * write the specified set of bits to the phy
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100323 */
Ben Dooks825a2ff2007-07-03 16:53:09 +0100324static void
325ax_mii_ei_outbits(struct net_device *dev, unsigned int bits, int len)
326{
Joe Perchesece49152010-11-15 11:12:31 +0000327 struct ei_device *ei_local = netdev_priv(dev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100328 void __iomem *memr_addr = (void __iomem *)dev->base_addr + AX_MEMR;
329 unsigned int memr;
330
331 /* clock low, data to output mode */
332 memr = ei_inb(memr_addr);
333 memr &= ~(AX_MEMR_MDC | AX_MEMR_MDIR);
334 ei_outb(memr, memr_addr);
335
336 for (len--; len >= 0; len--) {
337 if (bits & (1 << len))
338 memr |= AX_MEMR_MDO;
339 else
340 memr &= ~AX_MEMR_MDO;
341
342 ei_outb(memr, memr_addr);
343
344 /* clock high */
345
346 ei_outb(memr | AX_MEMR_MDC, memr_addr);
347 udelay(1);
348
349 /* clock low */
350 ei_outb(memr, memr_addr);
351 }
352
353 /* leaves the clock line low, mdir input */
354 memr |= AX_MEMR_MDIR;
355 ei_outb(memr, (void __iomem *)dev->base_addr + AX_MEMR);
356}
357
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100358/*
359 * ax_phy_ei_inbits
Ben Dooks825a2ff2007-07-03 16:53:09 +0100360 *
361 * read a specified number of bits from the phy
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100362 */
Ben Dooks825a2ff2007-07-03 16:53:09 +0100363static unsigned int
364ax_phy_ei_inbits(struct net_device *dev, int no)
365{
Joe Perchesece49152010-11-15 11:12:31 +0000366 struct ei_device *ei_local = netdev_priv(dev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100367 void __iomem *memr_addr = (void __iomem *)dev->base_addr + AX_MEMR;
368 unsigned int memr;
369 unsigned int result = 0;
370
371 /* clock low, data to input mode */
372 memr = ei_inb(memr_addr);
373 memr &= ~AX_MEMR_MDC;
374 memr |= AX_MEMR_MDIR;
375 ei_outb(memr, memr_addr);
376
377 for (no--; no >= 0; no--) {
378 ei_outb(memr | AX_MEMR_MDC, memr_addr);
379
380 udelay(1);
381
382 if (ei_inb(memr_addr) & AX_MEMR_MDI)
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100383 result |= (1 << no);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100384
385 ei_outb(memr, memr_addr);
386 }
387
388 return result;
389}
390
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100391/*
392 * ax_phy_issueaddr
Ben Dooks825a2ff2007-07-03 16:53:09 +0100393 *
394 * use the low level bit shifting routines to send the address
395 * and command to the specified phy
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100396 */
Ben Dooks825a2ff2007-07-03 16:53:09 +0100397static void
398ax_phy_issueaddr(struct net_device *dev, int phy_addr, int reg, int opc)
399{
400 if (phy_debug)
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100401 netdev_dbg(dev, "%s: dev %p, %04x, %04x, %d\n",
Harvey Harrisonb39d66a2008-08-20 16:52:04 -0700402 __func__, dev, phy_addr, reg, opc);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100403
404 ax_mii_ei_outbits(dev, 0x3f, 6); /* pre-amble */
405 ax_mii_ei_outbits(dev, 1, 2); /* frame-start */
406 ax_mii_ei_outbits(dev, opc, 2); /* op code */
407 ax_mii_ei_outbits(dev, phy_addr, 5); /* phy address */
408 ax_mii_ei_outbits(dev, reg, 5); /* reg address */
409}
410
411static int
412ax_phy_read(struct net_device *dev, int phy_addr, int reg)
413{
Joe Perchesece49152010-11-15 11:12:31 +0000414 struct ei_device *ei_local = netdev_priv(dev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100415 unsigned long flags;
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100416 unsigned int result;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100417
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100418 spin_lock_irqsave(&ei_local->page_lock, flags);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100419
420 ax_phy_issueaddr(dev, phy_addr, reg, 2);
421
422 result = ax_phy_ei_inbits(dev, 17);
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100423 result &= ~(3 << 16);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100424
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100425 spin_unlock_irqrestore(&ei_local->page_lock, flags);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100426
427 if (phy_debug)
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100428 netdev_dbg(dev, "%s: %04x.%04x => read %04x\n", __func__,
Ben Dooks825a2ff2007-07-03 16:53:09 +0100429 phy_addr, reg, result);
430
431 return result;
432}
433
434static void
435ax_phy_write(struct net_device *dev, int phy_addr, int reg, int value)
436{
Joe Perchesece49152010-11-15 11:12:31 +0000437 struct ei_device *ei = netdev_priv(dev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100438 unsigned long flags;
439
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100440 netdev_dbg(dev, "%s: %p, %04x, %04x %04x\n",
Harvey Harrisonb39d66a2008-08-20 16:52:04 -0700441 __func__, dev, phy_addr, reg, value);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100442
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100443 spin_lock_irqsave(&ei->page_lock, flags);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100444
445 ax_phy_issueaddr(dev, phy_addr, reg, 1);
446 ax_mii_ei_outbits(dev, 2, 2); /* send TA */
447 ax_mii_ei_outbits(dev, value, 16);
448
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100449 spin_unlock_irqrestore(&ei->page_lock, flags);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100450}
451
452static void ax_mii_expiry(unsigned long data)
453{
454 struct net_device *dev = (struct net_device *)data;
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100455 struct ax_device *ax = to_ax_dev(dev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100456 unsigned long flags;
457
458 spin_lock_irqsave(&ax->mii_lock, flags);
459 mii_check_media(&ax->mii, netif_msg_link(ax), 0);
460 spin_unlock_irqrestore(&ax->mii_lock, flags);
461
462 if (ax->running) {
463 ax->mii_timer.expires = jiffies + HZ*2;
464 add_timer(&ax->mii_timer);
465 }
466}
467
468static int ax_open(struct net_device *dev)
469{
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100470 struct ax_device *ax = to_ax_dev(dev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100471 struct ei_device *ei_local = netdev_priv(dev);
472 int ret;
473
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100474 netdev_dbg(dev, "open\n");
Ben Dooks825a2ff2007-07-03 16:53:09 +0100475
Daniel Mack47cb0352009-03-24 23:31:22 -0700476 ret = request_irq(dev->irq, ax_ei_interrupt, ax->irqflags,
477 dev->name, dev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100478 if (ret)
479 return ret;
480
481 ret = ax_ei_open(dev);
Kulikov Vasiliy9f1e7582010-07-08 23:42:40 -0700482 if (ret) {
483 free_irq(dev->irq, dev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100484 return ret;
Kulikov Vasiliy9f1e7582010-07-08 23:42:40 -0700485 }
Ben Dooks825a2ff2007-07-03 16:53:09 +0100486
487 /* turn the phy on (if turned off) */
488
489 ei_outb(ax->plat->gpoc_val, ei_local->mem + EI_SHIFT(0x17));
490 ax->running = 1;
491
492 /* start the MII timer */
493
494 init_timer(&ax->mii_timer);
495
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100496 ax->mii_timer.expires = jiffies + 1;
497 ax->mii_timer.data = (unsigned long) dev;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100498 ax->mii_timer.function = ax_mii_expiry;
499
500 add_timer(&ax->mii_timer);
501
502 return 0;
503}
504
505static int ax_close(struct net_device *dev)
506{
507 struct ax_device *ax = to_ax_dev(dev);
508 struct ei_device *ei_local = netdev_priv(dev);
509
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100510 netdev_dbg(dev, "close\n");
Ben Dooks825a2ff2007-07-03 16:53:09 +0100511
512 /* turn the phy off */
513
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100514 ei_outb(ax->plat->gpoc_val | (1 << 6),
Ben Dooks825a2ff2007-07-03 16:53:09 +0100515 ei_local->mem + EI_SHIFT(0x17));
516
517 ax->running = 0;
518 wmb();
519
520 del_timer_sync(&ax->mii_timer);
521 ax_ei_close(dev);
522
523 free_irq(dev->irq, dev);
524 return 0;
525}
526
527static int ax_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
528{
529 struct ax_device *ax = to_ax_dev(dev);
530 unsigned long flags;
531 int rc;
532
533 if (!netif_running(dev))
534 return -EINVAL;
535
536 spin_lock_irqsave(&ax->mii_lock, flags);
537 rc = generic_mii_ioctl(&ax->mii, if_mii(req), cmd, NULL);
538 spin_unlock_irqrestore(&ax->mii_lock, flags);
539
540 return rc;
541}
542
543/* ethtool ops */
544
545static void ax_get_drvinfo(struct net_device *dev,
546 struct ethtool_drvinfo *info)
547{
548 struct ax_device *ax = to_ax_dev(dev);
549
550 strcpy(info->driver, DRV_NAME);
551 strcpy(info->version, DRV_VERSION);
552 strcpy(info->bus_info, ax->dev->name);
553}
554
555static int ax_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
556{
557 struct ax_device *ax = to_ax_dev(dev);
558 unsigned long flags;
559
560 spin_lock_irqsave(&ax->mii_lock, flags);
561 mii_ethtool_gset(&ax->mii, cmd);
Ben Dooksc0912582008-08-07 17:21:09 +0100562 spin_unlock_irqrestore(&ax->mii_lock, flags);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100563
564 return 0;
565}
566
567static int ax_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
568{
569 struct ax_device *ax = to_ax_dev(dev);
570 unsigned long flags;
571 int rc;
572
573 spin_lock_irqsave(&ax->mii_lock, flags);
574 rc = mii_ethtool_sset(&ax->mii, cmd);
Ben Dooksc0912582008-08-07 17:21:09 +0100575 spin_unlock_irqrestore(&ax->mii_lock, flags);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100576
577 return rc;
578}
579
580static int ax_nway_reset(struct net_device *dev)
581{
582 struct ax_device *ax = to_ax_dev(dev);
583 return mii_nway_restart(&ax->mii);
584}
585
586static u32 ax_get_link(struct net_device *dev)
587{
588 struct ax_device *ax = to_ax_dev(dev);
589 return mii_link_ok(&ax->mii);
590}
591
592static const struct ethtool_ops ax_ethtool_ops = {
593 .get_drvinfo = ax_get_drvinfo,
594 .get_settings = ax_get_settings,
595 .set_settings = ax_set_settings,
596 .nway_reset = ax_nway_reset,
597 .get_link = ax_get_link,
Ben Dooks825a2ff2007-07-03 16:53:09 +0100598};
599
Magnus Damm89e536a2007-09-28 22:42:16 -0700600#ifdef CONFIG_AX88796_93CX6
601static void ax_eeprom_register_read(struct eeprom_93cx6 *eeprom)
602{
603 struct ei_device *ei_local = eeprom->data;
604 u8 reg = ei_inb(ei_local->mem + AX_MEMR);
605
606 eeprom->reg_data_in = reg & AX_MEMR_EEI;
607 eeprom->reg_data_out = reg & AX_MEMR_EEO; /* Input pin */
608 eeprom->reg_data_clock = reg & AX_MEMR_EECLK;
609 eeprom->reg_chip_select = reg & AX_MEMR_EECS;
610}
611
612static void ax_eeprom_register_write(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 reg &= ~(AX_MEMR_EEI | AX_MEMR_EECLK | AX_MEMR_EECS);
618
619 if (eeprom->reg_data_in)
620 reg |= AX_MEMR_EEI;
621 if (eeprom->reg_data_clock)
622 reg |= AX_MEMR_EECLK;
623 if (eeprom->reg_chip_select)
624 reg |= AX_MEMR_EECS;
625
626 ei_outb(reg, ei_local->mem + AX_MEMR);
627 udelay(10);
628}
629#endif
630
Magnus Dammd57bc362009-01-14 21:05:55 -0800631static const struct net_device_ops ax_netdev_ops = {
632 .ndo_open = ax_open,
633 .ndo_stop = ax_close,
634 .ndo_do_ioctl = ax_ioctl,
635
636 .ndo_start_xmit = ax_ei_start_xmit,
637 .ndo_tx_timeout = ax_ei_tx_timeout,
638 .ndo_get_stats = ax_ei_get_stats,
639 .ndo_set_multicast_list = ax_ei_set_multicast_list,
640 .ndo_validate_addr = eth_validate_addr,
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100641 .ndo_set_mac_address = eth_mac_addr,
Magnus Dammd57bc362009-01-14 21:05:55 -0800642 .ndo_change_mtu = eth_change_mtu,
643#ifdef CONFIG_NET_POLL_CONTROLLER
644 .ndo_poll_controller = ax_ei_poll,
645#endif
646};
647
Ben Dooks825a2ff2007-07-03 16:53:09 +0100648/* setup code */
649
650static void ax_initial_setup(struct net_device *dev, struct ei_device *ei_local)
651{
652 void __iomem *ioaddr = ei_local->mem;
653 struct ax_device *ax = to_ax_dev(dev);
654
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100655 /* Select page 0 */
656 ei_outb(E8390_NODMA + E8390_PAGE0 + E8390_STOP, ioaddr + E8390_CMD);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100657
658 /* set to byte access */
659 ei_outb(ax->plat->dcr_val & ~1, ioaddr + EN0_DCFG);
660 ei_outb(ax->plat->gpoc_val, ioaddr + EI_SHIFT(0x17));
661}
662
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100663/*
664 * ax_init_dev
Ben Dooks825a2ff2007-07-03 16:53:09 +0100665 *
666 * initialise the specified device, taking care to note the MAC
667 * address it may already have (if configured), ensure
668 * the device is ready to be used by lib8390.c and registerd with
669 * the network layer.
670 */
Marc Kleine-Budde1cbece32011-02-19 23:07:09 +0100671static int ax_init_dev(struct net_device *dev)
Ben Dooks825a2ff2007-07-03 16:53:09 +0100672{
673 struct ei_device *ei_local = netdev_priv(dev);
674 struct ax_device *ax = to_ax_dev(dev);
675 void __iomem *ioaddr = ei_local->mem;
676 unsigned int start_page;
677 unsigned int stop_page;
678 int ret;
679 int i;
680
681 ret = ax_initial_check(dev);
682 if (ret)
683 goto err_out;
684
685 /* setup goes here */
686
687 ax_initial_setup(dev, ei_local);
688
689 /* read the mac from the card prom if we need it */
690
Marc Kleine-Budde1cbece32011-02-19 23:07:09 +0100691 if (ax->plat->flags & AXFLG_HAS_EEPROM) {
Ben Dooks825a2ff2007-07-03 16:53:09 +0100692 unsigned char SA_prom[32];
693
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100694 for (i = 0; i < sizeof(SA_prom); i += 2) {
Ben Dooks825a2ff2007-07-03 16:53:09 +0100695 SA_prom[i] = ei_inb(ioaddr + NE_DATAPORT);
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100696 SA_prom[i + 1] = ei_inb(ioaddr + NE_DATAPORT);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100697 }
698
699 if (ax->plat->wordlength == 2)
700 for (i = 0; i < 16; i++)
701 SA_prom[i] = SA_prom[i+i];
702
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100703 memcpy(dev->dev_addr, SA_prom, 6);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100704 }
705
Magnus Damm89e536a2007-09-28 22:42:16 -0700706#ifdef CONFIG_AX88796_93CX6
Marc Kleine-Budde1cbece32011-02-19 23:07:09 +0100707 if (ax->plat->flags & AXFLG_HAS_93CX6) {
Magnus Damm89e536a2007-09-28 22:42:16 -0700708 unsigned char mac_addr[6];
709 struct eeprom_93cx6 eeprom;
710
711 eeprom.data = ei_local;
712 eeprom.register_read = ax_eeprom_register_read;
713 eeprom.register_write = ax_eeprom_register_write;
714 eeprom.width = PCI_EEPROM_WIDTH_93C56;
715
716 eeprom_93cx6_multiread(&eeprom, 0,
717 (__le16 __force *)mac_addr,
718 sizeof(mac_addr) >> 1);
719
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100720 memcpy(dev->dev_addr, mac_addr, 6);
Magnus Damm89e536a2007-09-28 22:42:16 -0700721 }
722#endif
Ben Dooks825a2ff2007-07-03 16:53:09 +0100723 if (ax->plat->wordlength == 2) {
724 /* We must set the 8390 for word mode. */
725 ei_outb(ax->plat->dcr_val, ei_local->mem + EN0_DCFG);
726 start_page = NESM_START_PG;
727 stop_page = NESM_STOP_PG;
728 } else {
729 start_page = NE1SM_START_PG;
730 stop_page = NE1SM_STOP_PG;
731 }
732
Marc Kleine-Budde1cbece32011-02-19 23:07:09 +0100733 /* load the mac-address from the device */
734 if (ax->plat->flags & AXFLG_MAC_FROMDEV) {
735 ei_outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP,
736 ei_local->mem + E8390_CMD); /* 0x61 */
737 for (i = 0; i < ETHER_ADDR_LEN; i++)
738 dev->dev_addr[i] =
739 ei_inb(ioaddr + EN1_PHYS_SHIFT(i));
Ben Dooks825a2ff2007-07-03 16:53:09 +0100740 }
741
Marc Kleine-Budde1cbece32011-02-19 23:07:09 +0100742 if ((ax->plat->flags & AXFLG_MAC_FROMPLATFORM) &&
743 ax->plat->mac_addr)
744 memcpy(dev->dev_addr, ax->plat->mac_addr,
745 ETHER_ADDR_LEN);
746
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
769 ax->msg_enable = NETIF_MSG_LINK;
770 ax->mii.phy_id_mask = 0x1f;
771 ax->mii.reg_num_mask = 0x1f;
772 ax->mii.phy_id = 0x10; /* onboard phy */
773 ax->mii.force_media = 0;
774 ax->mii.full_duplex = 0;
775 ax->mii.mdio_read = ax_phy_read;
776 ax->mii.mdio_write = ax_phy_write;
777 ax->mii.dev = dev;
778
Ben Dooks825a2ff2007-07-03 16:53:09 +0100779 ax_NS8390_init(dev, 0);
780
Ben Dooks825a2ff2007-07-03 16:53:09 +0100781 ret = register_netdev(dev);
782 if (ret)
783 goto out_irq;
784
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100785 netdev_info(dev, "%dbit, irq %d, %lx, MAC: %pM\n",
786 ei_local->word16 ? 16 : 8, dev->irq, dev->base_addr,
787 dev->dev_addr);
788
Ben Dooks825a2ff2007-07-03 16:53:09 +0100789 return 0;
790
791 out_irq:
792 /* cleanup irq */
793 free_irq(dev->irq, dev);
794 err_out:
795 return ret;
796}
797
798static int ax_remove(struct platform_device *_dev)
799{
800 struct net_device *dev = platform_get_drvdata(_dev);
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100801 struct ei_device *ei_local = netdev_priv(dev);
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100802 struct ax_device *ax;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100803
804 ax = to_ax_dev(dev);
805
806 unregister_netdev(dev);
807 free_irq(dev->irq, dev);
808
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100809 iounmap(ei_local->mem);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100810 release_resource(ax->mem);
811 kfree(ax->mem);
812
813 if (ax->map2) {
814 iounmap(ax->map2);
815 release_resource(ax->mem2);
816 kfree(ax->mem2);
817 }
818
819 free_netdev(dev);
820
821 return 0;
822}
823
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100824/*
825 * ax_probe
Ben Dooks825a2ff2007-07-03 16:53:09 +0100826 *
827 * This is the entry point when the platform device system uses to
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100828 * notify us of a new device to attach to. Allocate memory, find the
829 * resources and information passed, and map the necessary registers.
830 */
Ben Dooks825a2ff2007-07-03 16:53:09 +0100831static int ax_probe(struct platform_device *pdev)
832{
833 struct net_device *dev;
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100834 struct ei_device *ei_local;
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100835 struct ax_device *ax;
836 struct resource *res;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100837 size_t size;
Daniel Mack47cb0352009-03-24 23:31:22 -0700838 int ret = 0;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100839
840 dev = ax__alloc_ei_netdev(sizeof(struct ax_device));
841 if (dev == NULL)
842 return -ENOMEM;
843
844 /* ok, let's setup our device */
Marc Kleine-Budde85ac6162011-02-20 17:46:18 +0100845 SET_NETDEV_DEV(dev, &pdev->dev);
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100846 ei_local = netdev_priv(dev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100847 ax = to_ax_dev(dev);
848
Ben Dooks825a2ff2007-07-03 16:53:09 +0100849 spin_lock_init(&ax->mii_lock);
850
851 ax->dev = pdev;
852 ax->plat = pdev->dev.platform_data;
853 platform_set_drvdata(pdev, dev);
854
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100855 ei_local->rxcr_base = ax->plat->rcr_val;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100856
857 /* find the platform resources */
Daniel Mack47cb0352009-03-24 23:31:22 -0700858 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
859 if (res == NULL) {
Ben Dooks825a2ff2007-07-03 16:53:09 +0100860 dev_err(&pdev->dev, "no IRQ specified\n");
Julia Lawall13eea192010-10-18 04:11:14 +0000861 ret = -ENXIO;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100862 goto exit_mem;
863 }
Daniel Mack47cb0352009-03-24 23:31:22 -0700864
865 dev->irq = res->start;
866 ax->irqflags = res->flags & IRQF_TRIGGER_MASK;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100867
868 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
869 if (res == NULL) {
870 dev_err(&pdev->dev, "no MEM specified\n");
871 ret = -ENXIO;
872 goto exit_mem;
873 }
874
875 size = (res->end - res->start) + 1;
876
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100877 /*
878 * setup the register offsets from either the platform data or
879 * by using the size of the resource provided
880 */
Ben Dooks825a2ff2007-07-03 16:53:09 +0100881 if (ax->plat->reg_offsets)
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100882 ei_local->reg_offset = ax->plat->reg_offsets;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100883 else {
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100884 ei_local->reg_offset = ax->reg_offsets;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100885 for (ret = 0; ret < 0x18; ret++)
886 ax->reg_offsets[ret] = (size / 0x18) * ret;
887 }
888
889 ax->mem = request_mem_region(res->start, size, pdev->name);
890 if (ax->mem == NULL) {
891 dev_err(&pdev->dev, "cannot reserve registers\n");
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100892 ret = -ENXIO;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100893 goto exit_mem;
894 }
895
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100896 ei_local->mem = ioremap(res->start, size);
897 dev->base_addr = (unsigned long)ei_local->mem;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100898
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100899 if (ei_local->mem == NULL) {
Andrew Mortonb4efe222007-08-10 14:05:21 -0700900 dev_err(&pdev->dev, "Cannot ioremap area (%08llx,%08llx)\n",
901 (unsigned long long)res->start,
902 (unsigned long long)res->end);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100903
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100904 ret = -ENXIO;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100905 goto exit_req;
906 }
907
908 /* look for reset area */
909
910 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
911 if (res == NULL) {
912 if (!ax->plat->reg_offsets) {
913 for (ret = 0; ret < 0x20; ret++)
914 ax->reg_offsets[ret] = (size / 0x20) * ret;
915 }
916
917 ax->map2 = NULL;
918 } else {
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100919 size = (res->end - res->start) + 1;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100920
921 ax->mem2 = request_mem_region(res->start, size, pdev->name);
Julia Lawallbcf4d812010-02-08 22:44:18 -0800922 if (ax->mem2 == NULL) {
Ben Dooks825a2ff2007-07-03 16:53:09 +0100923 dev_err(&pdev->dev, "cannot reserve registers\n");
924 ret = -ENXIO;
925 goto exit_mem1;
926 }
927
928 ax->map2 = ioremap(res->start, size);
929 if (ax->map2 == NULL) {
Joe Perches898eb712007-10-18 03:06:30 -0700930 dev_err(&pdev->dev, "cannot map reset register\n");
Ben Dooks825a2ff2007-07-03 16:53:09 +0100931 ret = -ENXIO;
932 goto exit_mem2;
933 }
934
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100935 ei_local->reg_offset[0x1f] = ax->map2 - ei_local->mem;
Ben Dooks825a2ff2007-07-03 16:53:09 +0100936 }
937
938 /* got resources, now initialise and register device */
939
Marc Kleine-Budde1cbece32011-02-19 23:07:09 +0100940 ret = ax_init_dev(dev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100941 if (!ret)
942 return 0;
943
944 if (ax->map2 == NULL)
945 goto exit_mem1;
946
947 iounmap(ax->map2);
948
949 exit_mem2:
950 release_resource(ax->mem2);
951 kfree(ax->mem2);
952
953 exit_mem1:
Marc Kleine-Buddeea5a43d2011-02-19 22:08:33 +0100954 iounmap(ei_local->mem);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100955
956 exit_req:
957 release_resource(ax->mem);
958 kfree(ax->mem);
959
960 exit_mem:
961 free_netdev(dev);
962
963 return ret;
964}
965
966/* suspend and resume */
967
968#ifdef CONFIG_PM
969static int ax_suspend(struct platform_device *dev, pm_message_t state)
970{
971 struct net_device *ndev = platform_get_drvdata(dev);
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100972 struct ax_device *ax = to_ax_dev(ndev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100973
974 ax->resume_open = ax->running;
975
976 netif_device_detach(ndev);
977 ax_close(ndev);
978
979 return 0;
980}
981
982static int ax_resume(struct platform_device *pdev)
983{
984 struct net_device *ndev = platform_get_drvdata(pdev);
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100985 struct ax_device *ax = to_ax_dev(ndev);
Ben Dooks825a2ff2007-07-03 16:53:09 +0100986
987 ax_initial_setup(ndev, netdev_priv(ndev));
988 ax_NS8390_init(ndev, ax->resume_open);
989 netif_device_attach(ndev);
990
991 if (ax->resume_open)
992 ax_open(ndev);
993
994 return 0;
995}
996
997#else
998#define ax_suspend NULL
Marc Kleine-Budde9f072422011-02-19 17:07:40 +0100999#define ax_resume NULL
Ben Dooks825a2ff2007-07-03 16:53:09 +01001000#endif
1001
1002static struct platform_driver axdrv = {
1003 .driver = {
1004 .name = "ax88796",
1005 .owner = THIS_MODULE,
1006 },
1007 .probe = ax_probe,
1008 .remove = ax_remove,
1009 .suspend = ax_suspend,
1010 .resume = ax_resume,
1011};
1012
1013static int __init axdrv_init(void)
1014{
1015 return platform_driver_register(&axdrv);
1016}
1017
1018static void __exit axdrv_exit(void)
1019{
1020 platform_driver_unregister(&axdrv);
1021}
1022
1023module_init(axdrv_init);
1024module_exit(axdrv_exit);
1025
1026MODULE_DESCRIPTION("AX88796 10/100 Ethernet platform driver");
1027MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
1028MODULE_LICENSE("GPL v2");
Kay Sievers72abb462008-04-18 13:50:44 -07001029MODULE_ALIAS("platform:ax88796");