Commit | Line | Data |
---|---|---|
825a2ff1 BD |
1 | /* 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. | |
12 | */ | |
13 | ||
14 | #include <linux/module.h> | |
15 | #include <linux/kernel.h> | |
16 | #include <linux/errno.h> | |
17 | #include <linux/isapnp.h> | |
18 | #include <linux/init.h> | |
19 | #include <linux/interrupt.h> | |
20 | #include <linux/platform_device.h> | |
21 | #include <linux/delay.h> | |
22 | #include <linux/timer.h> | |
23 | #include <linux/netdevice.h> | |
24 | #include <linux/etherdevice.h> | |
25 | #include <linux/ethtool.h> | |
26 | #include <linux/mii.h> | |
89e536a1 | 27 | #include <linux/eeprom_93cx6.h> |
825a2ff1 BD |
28 | |
29 | #include <net/ax88796.h> | |
30 | ||
31 | #include <asm/system.h> | |
32 | #include <asm/io.h> | |
33 | ||
34 | static int phy_debug = 0; | |
35 | ||
36 | /* Rename the lib8390.c functions to show that they are in this driver */ | |
37 | #define __ei_open ax_ei_open | |
38 | #define __ei_close ax_ei_close | |
39 | #define __ei_poll ax_ei_poll | |
d57bc36e | 40 | #define __ei_start_xmit ax_ei_start_xmit |
825a2ff1 | 41 | #define __ei_tx_timeout ax_ei_tx_timeout |
d57bc36e MD |
42 | #define __ei_get_stats ax_ei_get_stats |
43 | #define __ei_set_multicast_list ax_ei_set_multicast_list | |
825a2ff1 BD |
44 | #define __ei_interrupt ax_ei_interrupt |
45 | #define ____alloc_ei_netdev ax__alloc_ei_netdev | |
46 | #define __NS8390_init ax_NS8390_init | |
47 | ||
48 | /* force unsigned long back to 'void __iomem *' */ | |
49 | #define ax_convert_addr(_a) ((void __force __iomem *)(_a)) | |
50 | ||
51 | #define ei_inb(_a) readb(ax_convert_addr(_a)) | |
52 | #define ei_outb(_v, _a) writeb(_v, ax_convert_addr(_a)) | |
53 | ||
54 | #define ei_inb_p(_a) ei_inb(_a) | |
55 | #define ei_outb_p(_v, _a) ei_outb(_v, _a) | |
56 | ||
57 | /* define EI_SHIFT() to take into account our register offsets */ | |
58 | #define EI_SHIFT(x) (ei_local->reg_offset[(x)]) | |
59 | ||
60 | /* Ensure we have our RCR base value */ | |
61 | #define AX88796_PLATFORM | |
62 | ||
63 | static 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 */ | |
76 | #define NE1SM_STOP_PG 0x40 /* Last page +1 of RX ring */ | |
77 | #define NESM_START_PG 0x40 /* First page of TX buffer */ | |
78 | #define NESM_STOP_PG 0x80 /* Last page +1 of RX ring */ | |
79 | ||
80 | /* device private data */ | |
81 | ||
82 | struct ax_device { | |
83 | struct timer_list mii_timer; | |
84 | spinlock_t mii_lock; | |
85 | struct mii_if_info mii; | |
86 | ||
87 | u32 msg_enable; | |
88 | void __iomem *map2; | |
89 | struct platform_device *dev; | |
90 | struct resource *mem; | |
91 | struct resource *mem2; | |
92 | struct ax_plat_data *plat; | |
93 | ||
94 | unsigned char running; | |
95 | unsigned char resume_open; | |
47cb0355 | 96 | unsigned int irqflags; |
825a2ff1 BD |
97 | |
98 | u32 reg_offsets[0x20]; | |
99 | }; | |
100 | ||
101 | static inline struct ax_device *to_ax_dev(struct net_device *dev) | |
102 | { | |
103 | struct ei_device *ei_local = netdev_priv(dev); | |
104 | return (struct ax_device *)(ei_local+1); | |
105 | } | |
106 | ||
107 | /* ax_initial_check | |
108 | * | |
109 | * do an initial probe for the card to check wether it exists | |
110 | * and is functional | |
111 | */ | |
112 | ||
113 | static int ax_initial_check(struct net_device *dev) | |
114 | { | |
115 | struct ei_device *ei_local = netdev_priv(dev); | |
116 | void __iomem *ioaddr = ei_local->mem; | |
117 | int reg0; | |
118 | int regd; | |
119 | ||
120 | reg0 = ei_inb(ioaddr); | |
121 | if (reg0 == 0xFF) | |
122 | return -ENODEV; | |
123 | ||
124 | ei_outb(E8390_NODMA+E8390_PAGE1+E8390_STOP, ioaddr + E8390_CMD); | |
125 | regd = ei_inb(ioaddr + 0x0d); | |
126 | ei_outb(0xff, ioaddr + 0x0d); | |
127 | ei_outb(E8390_NODMA+E8390_PAGE0, ioaddr + E8390_CMD); | |
128 | ei_inb(ioaddr + EN0_COUNTER0); /* Clear the counter by reading. */ | |
129 | if (ei_inb(ioaddr + EN0_COUNTER0) != 0) { | |
130 | ei_outb(reg0, ioaddr); | |
131 | ei_outb(regd, ioaddr + 0x0d); /* Restore the old values. */ | |
132 | return -ENODEV; | |
133 | } | |
134 | ||
135 | return 0; | |
136 | } | |
137 | ||
138 | /* Hard reset the card. This used to pause for the same period that a | |
139 | 8390 reset command required, but that shouldn't be necessary. */ | |
140 | ||
141 | static void ax_reset_8390(struct net_device *dev) | |
142 | { | |
143 | struct ei_device *ei_local = netdev_priv(dev); | |
237c5e8e | 144 | struct ax_device *ax = to_ax_dev(dev); |
825a2ff1 BD |
145 | unsigned long reset_start_time = jiffies; |
146 | void __iomem *addr = (void __iomem *)dev->base_addr; | |
147 | ||
148 | if (ei_debug > 1) | |
237c5e8e | 149 | dev_dbg(&ax->dev->dev, "resetting the 8390 t=%ld\n", jiffies); |
825a2ff1 BD |
150 | |
151 | ei_outb(ei_inb(addr + NE_RESET), addr + NE_RESET); | |
152 | ||
153 | ei_status.txing = 0; | |
154 | ei_status.dmaing = 0; | |
155 | ||
156 | /* This check _should_not_ be necessary, omit eventually. */ | |
157 | while ((ei_inb(addr + EN0_ISR) & ENISR_RESET) == 0) { | |
158 | if (jiffies - reset_start_time > 2*HZ/100) { | |
237c5e8e | 159 | dev_warn(&ax->dev->dev, "%s: %s did not complete.\n", |
b39d66a8 | 160 | __func__, dev->name); |
825a2ff1 BD |
161 | break; |
162 | } | |
163 | } | |
164 | ||
165 | ei_outb(ENISR_RESET, addr + EN0_ISR); /* Ack intr. */ | |
166 | } | |
167 | ||
168 | ||
169 | static 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); | |
237c5e8e | 173 | struct ax_device *ax = to_ax_dev(dev); |
825a2ff1 BD |
174 | void __iomem *nic_base = ei_local->mem; |
175 | ||
176 | /* This *shouldn't* happen. If it does, it's the last thing you'll see */ | |
177 | if (ei_status.dmaing) { | |
237c5e8e BD |
178 | dev_err(&ax->dev->dev, "%s: DMAing conflict in %s " |
179 | "[DMAstat:%d][irqlock:%d].\n", | |
b39d66a8 | 180 | dev->name, __func__, |
237c5e8e | 181 | ei_status.dmaing, ei_status.irqlock); |
825a2ff1 BD |
182 | return; |
183 | } | |
184 | ||
185 | ei_status.dmaing |= 0x01; | |
186 | ei_outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD); | |
187 | ei_outb(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO); | |
188 | ei_outb(0, nic_base + EN0_RCNTHI); | |
189 | ei_outb(0, nic_base + EN0_RSARLO); /* On page boundary */ | |
190 | ei_outb(ring_page, nic_base + EN0_RSARHI); | |
191 | ei_outb(E8390_RREAD+E8390_START, nic_base + NE_CMD); | |
192 | ||
193 | if (ei_status.word16) | |
194 | readsw(nic_base + NE_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr)>>1); | |
195 | else | |
196 | readsb(nic_base + NE_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr)); | |
197 | ||
198 | ei_outb(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */ | |
199 | ei_status.dmaing &= ~0x01; | |
200 | ||
201 | le16_to_cpus(&hdr->count); | |
202 | } | |
203 | ||
204 | ||
205 | /* Block input and output, similar to the Crynwr packet driver. If you | |
206 | are porting to a new ethercard, look at the packet driver source for hints. | |
207 | The NEx000 doesn't share the on-board packet memory -- you have to put | |
208 | the packet out through the "remote DMA" dataport using ei_outb. */ | |
209 | ||
210 | static void ax_block_input(struct net_device *dev, int count, | |
211 | struct sk_buff *skb, int ring_offset) | |
212 | { | |
213 | struct ei_device *ei_local = netdev_priv(dev); | |
237c5e8e | 214 | struct ax_device *ax = to_ax_dev(dev); |
825a2ff1 BD |
215 | void __iomem *nic_base = ei_local->mem; |
216 | char *buf = skb->data; | |
217 | ||
218 | if (ei_status.dmaing) { | |
237c5e8e BD |
219 | dev_err(&ax->dev->dev, |
220 | "%s: DMAing conflict in %s " | |
825a2ff1 | 221 | "[DMAstat:%d][irqlock:%d].\n", |
b39d66a8 | 222 | dev->name, __func__, |
237c5e8e | 223 | ei_status.dmaing, ei_status.irqlock); |
825a2ff1 BD |
224 | return; |
225 | } | |
226 | ||
227 | ei_status.dmaing |= 0x01; | |
228 | ||
229 | ei_outb(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base+ NE_CMD); | |
230 | ei_outb(count & 0xff, nic_base + EN0_RCNTLO); | |
231 | ei_outb(count >> 8, nic_base + EN0_RCNTHI); | |
232 | ei_outb(ring_offset & 0xff, nic_base + EN0_RSARLO); | |
233 | ei_outb(ring_offset >> 8, nic_base + EN0_RSARHI); | |
234 | ei_outb(E8390_RREAD+E8390_START, nic_base + NE_CMD); | |
235 | ||
236 | if (ei_status.word16) { | |
237 | readsw(nic_base + NE_DATAPORT, buf, count >> 1); | |
238 | if (count & 0x01) | |
239 | buf[count-1] = ei_inb(nic_base + NE_DATAPORT); | |
240 | ||
241 | } else { | |
242 | readsb(nic_base + NE_DATAPORT, buf, count); | |
243 | } | |
244 | ||
245 | ei_status.dmaing &= ~1; | |
246 | } | |
247 | ||
248 | static void ax_block_output(struct net_device *dev, int count, | |
249 | const unsigned char *buf, const int start_page) | |
250 | { | |
251 | struct ei_device *ei_local = netdev_priv(dev); | |
237c5e8e | 252 | struct ax_device *ax = to_ax_dev(dev); |
825a2ff1 BD |
253 | void __iomem *nic_base = ei_local->mem; |
254 | unsigned long dma_start; | |
255 | ||
256 | /* Round the count up for word writes. Do we need to do this? | |
257 | What effect will an odd byte count have on the 8390? | |
258 | I should check someday. */ | |
259 | ||
260 | if (ei_status.word16 && (count & 0x01)) | |
261 | count++; | |
262 | ||
263 | /* This *shouldn't* happen. If it does, it's the last thing you'll see */ | |
264 | if (ei_status.dmaing) { | |
237c5e8e | 265 | dev_err(&ax->dev->dev, "%s: DMAing conflict in %s." |
825a2ff1 | 266 | "[DMAstat:%d][irqlock:%d]\n", |
b39d66a8 | 267 | dev->name, __func__, |
825a2ff1 BD |
268 | ei_status.dmaing, ei_status.irqlock); |
269 | return; | |
270 | } | |
271 | ||
272 | ei_status.dmaing |= 0x01; | |
273 | /* We should already be in page 0, but to be safe... */ | |
274 | ei_outb(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base + NE_CMD); | |
275 | ||
276 | ei_outb(ENISR_RDC, nic_base + EN0_ISR); | |
277 | ||
278 | /* Now the normal output. */ | |
279 | ei_outb(count & 0xff, nic_base + EN0_RCNTLO); | |
280 | ei_outb(count >> 8, nic_base + EN0_RCNTHI); | |
281 | ei_outb(0x00, nic_base + EN0_RSARLO); | |
282 | ei_outb(start_page, nic_base + EN0_RSARHI); | |
283 | ||
284 | ei_outb(E8390_RWRITE+E8390_START, nic_base + NE_CMD); | |
285 | if (ei_status.word16) { | |
286 | writesw(nic_base + NE_DATAPORT, buf, count>>1); | |
287 | } else { | |
288 | writesb(nic_base + NE_DATAPORT, buf, count); | |
289 | } | |
290 | ||
291 | dma_start = jiffies; | |
292 | ||
293 | while ((ei_inb(nic_base + EN0_ISR) & ENISR_RDC) == 0) { | |
294 | if (jiffies - dma_start > 2*HZ/100) { /* 20ms */ | |
237c5e8e BD |
295 | dev_warn(&ax->dev->dev, |
296 | "%s: timeout waiting for Tx RDC.\n", dev->name); | |
825a2ff1 BD |
297 | ax_reset_8390(dev); |
298 | ax_NS8390_init(dev,1); | |
299 | break; | |
300 | } | |
301 | } | |
302 | ||
303 | ei_outb(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */ | |
304 | ei_status.dmaing &= ~0x01; | |
305 | return; | |
306 | } | |
307 | ||
308 | /* definitions for accessing MII/EEPROM interface */ | |
309 | ||
310 | #define AX_MEMR EI_SHIFT(0x14) | |
311 | #define AX_MEMR_MDC (1<<0) | |
312 | #define AX_MEMR_MDIR (1<<1) | |
313 | #define AX_MEMR_MDI (1<<2) | |
314 | #define AX_MEMR_MDO (1<<3) | |
315 | #define AX_MEMR_EECS (1<<4) | |
316 | #define AX_MEMR_EEI (1<<5) | |
317 | #define AX_MEMR_EEO (1<<6) | |
318 | #define AX_MEMR_EECLK (1<<7) | |
319 | ||
320 | /* ax_mii_ei_outbits | |
321 | * | |
322 | * write the specified set of bits to the phy | |
323 | */ | |
324 | ||
325 | static void | |
326 | ax_mii_ei_outbits(struct net_device *dev, unsigned int bits, int len) | |
327 | { | |
328 | struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev); | |
329 | void __iomem *memr_addr = (void __iomem *)dev->base_addr + AX_MEMR; | |
330 | unsigned int memr; | |
331 | ||
332 | /* clock low, data to output mode */ | |
333 | memr = ei_inb(memr_addr); | |
334 | memr &= ~(AX_MEMR_MDC | AX_MEMR_MDIR); | |
335 | ei_outb(memr, memr_addr); | |
336 | ||
337 | for (len--; len >= 0; len--) { | |
338 | if (bits & (1 << len)) | |
339 | memr |= AX_MEMR_MDO; | |
340 | else | |
341 | memr &= ~AX_MEMR_MDO; | |
342 | ||
343 | ei_outb(memr, memr_addr); | |
344 | ||
345 | /* clock high */ | |
346 | ||
347 | ei_outb(memr | AX_MEMR_MDC, memr_addr); | |
348 | udelay(1); | |
349 | ||
350 | /* clock low */ | |
351 | ei_outb(memr, memr_addr); | |
352 | } | |
353 | ||
354 | /* leaves the clock line low, mdir input */ | |
355 | memr |= AX_MEMR_MDIR; | |
356 | ei_outb(memr, (void __iomem *)dev->base_addr + AX_MEMR); | |
357 | } | |
358 | ||
359 | /* ax_phy_ei_inbits | |
360 | * | |
361 | * read a specified number of bits from the phy | |
362 | */ | |
363 | ||
364 | static unsigned int | |
365 | ax_phy_ei_inbits(struct net_device *dev, int no) | |
366 | { | |
367 | struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev); | |
368 | void __iomem *memr_addr = (void __iomem *)dev->base_addr + AX_MEMR; | |
369 | unsigned int memr; | |
370 | unsigned int result = 0; | |
371 | ||
372 | /* clock low, data to input mode */ | |
373 | memr = ei_inb(memr_addr); | |
374 | memr &= ~AX_MEMR_MDC; | |
375 | memr |= AX_MEMR_MDIR; | |
376 | ei_outb(memr, memr_addr); | |
377 | ||
378 | for (no--; no >= 0; no--) { | |
379 | ei_outb(memr | AX_MEMR_MDC, memr_addr); | |
380 | ||
381 | udelay(1); | |
382 | ||
383 | if (ei_inb(memr_addr) & AX_MEMR_MDI) | |
384 | result |= (1<<no); | |
385 | ||
386 | ei_outb(memr, memr_addr); | |
387 | } | |
388 | ||
389 | return result; | |
390 | } | |
391 | ||
392 | /* ax_phy_issueaddr | |
393 | * | |
394 | * use the low level bit shifting routines to send the address | |
395 | * and command to the specified phy | |
396 | */ | |
397 | ||
398 | static void | |
399 | ax_phy_issueaddr(struct net_device *dev, int phy_addr, int reg, int opc) | |
400 | { | |
401 | if (phy_debug) | |
402 | pr_debug("%s: dev %p, %04x, %04x, %d\n", | |
b39d66a8 | 403 | __func__, dev, phy_addr, reg, opc); |
825a2ff1 BD |
404 | |
405 | ax_mii_ei_outbits(dev, 0x3f, 6); /* pre-amble */ | |
406 | ax_mii_ei_outbits(dev, 1, 2); /* frame-start */ | |
407 | ax_mii_ei_outbits(dev, opc, 2); /* op code */ | |
408 | ax_mii_ei_outbits(dev, phy_addr, 5); /* phy address */ | |
409 | ax_mii_ei_outbits(dev, reg, 5); /* reg address */ | |
410 | } | |
411 | ||
412 | static int | |
413 | ax_phy_read(struct net_device *dev, int phy_addr, int reg) | |
414 | { | |
415 | struct ei_device *ei_local = (struct ei_device *) netdev_priv(dev); | |
416 | unsigned long flags; | |
417 | unsigned int result; | |
418 | ||
419 | spin_lock_irqsave(&ei_local->page_lock, flags); | |
420 | ||
421 | ax_phy_issueaddr(dev, phy_addr, reg, 2); | |
422 | ||
423 | result = ax_phy_ei_inbits(dev, 17); | |
424 | result &= ~(3<<16); | |
425 | ||
426 | spin_unlock_irqrestore(&ei_local->page_lock, flags); | |
427 | ||
428 | if (phy_debug) | |
b39d66a8 | 429 | pr_debug("%s: %04x.%04x => read %04x\n", __func__, |
825a2ff1 BD |
430 | phy_addr, reg, result); |
431 | ||
432 | return result; | |
433 | } | |
434 | ||
435 | static void | |
436 | ax_phy_write(struct net_device *dev, int phy_addr, int reg, int value) | |
437 | { | |
438 | struct ei_device *ei = (struct ei_device *) netdev_priv(dev); | |
237c5e8e | 439 | struct ax_device *ax = to_ax_dev(dev); |
825a2ff1 BD |
440 | unsigned long flags; |
441 | ||
237c5e8e | 442 | dev_dbg(&ax->dev->dev, "%s: %p, %04x, %04x %04x\n", |
b39d66a8 | 443 | __func__, dev, phy_addr, reg, value); |
825a2ff1 BD |
444 | |
445 | spin_lock_irqsave(&ei->page_lock, flags); | |
446 | ||
447 | ax_phy_issueaddr(dev, phy_addr, reg, 1); | |
448 | ax_mii_ei_outbits(dev, 2, 2); /* send TA */ | |
449 | ax_mii_ei_outbits(dev, value, 16); | |
450 | ||
451 | spin_unlock_irqrestore(&ei->page_lock, flags); | |
452 | } | |
453 | ||
454 | static void ax_mii_expiry(unsigned long data) | |
455 | { | |
456 | struct net_device *dev = (struct net_device *)data; | |
457 | struct ax_device *ax = to_ax_dev(dev); | |
458 | unsigned long flags; | |
459 | ||
460 | spin_lock_irqsave(&ax->mii_lock, flags); | |
461 | mii_check_media(&ax->mii, netif_msg_link(ax), 0); | |
462 | spin_unlock_irqrestore(&ax->mii_lock, flags); | |
463 | ||
464 | if (ax->running) { | |
465 | ax->mii_timer.expires = jiffies + HZ*2; | |
466 | add_timer(&ax->mii_timer); | |
467 | } | |
468 | } | |
469 | ||
470 | static int ax_open(struct net_device *dev) | |
471 | { | |
472 | struct ax_device *ax = to_ax_dev(dev); | |
473 | struct ei_device *ei_local = netdev_priv(dev); | |
474 | int ret; | |
475 | ||
2832e856 | 476 | dev_dbg(&ax->dev->dev, "%s: open\n", dev->name); |
825a2ff1 | 477 | |
47cb0355 DM |
478 | ret = request_irq(dev->irq, ax_ei_interrupt, ax->irqflags, |
479 | dev->name, dev); | |
825a2ff1 BD |
480 | if (ret) |
481 | return ret; | |
482 | ||
483 | ret = ax_ei_open(dev); | |
484 | if (ret) | |
485 | return ret; | |
486 | ||
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 | ||
496 | ax->mii_timer.expires = jiffies+1; | |
497 | ax->mii_timer.data = (unsigned long) dev; | |
498 | ax->mii_timer.function = ax_mii_expiry; | |
499 | ||
500 | add_timer(&ax->mii_timer); | |
501 | ||
502 | return 0; | |
503 | } | |
504 | ||
505 | static 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 | ||
2832e856 | 510 | dev_dbg(&ax->dev->dev, "%s: close\n", dev->name); |
825a2ff1 BD |
511 | |
512 | /* turn the phy off */ | |
513 | ||
514 | ei_outb(ax->plat->gpoc_val | (1<<6), | |
515 | 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 | ||
527 | static 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 | ||
545 | static 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 | ||
555 | static 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); | |
c0912585 | 562 | spin_unlock_irqrestore(&ax->mii_lock, flags); |
825a2ff1 BD |
563 | |
564 | return 0; | |
565 | } | |
566 | ||
567 | static 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); | |
c0912585 | 575 | spin_unlock_irqrestore(&ax->mii_lock, flags); |
825a2ff1 BD |
576 | |
577 | return rc; | |
578 | } | |
579 | ||
580 | static 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 | ||
586 | static 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 | ||
592 | static 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, | |
825a2ff1 BD |
598 | }; |
599 | ||
89e536a1 MD |
600 | #ifdef CONFIG_AX88796_93CX6 |
601 | static 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 | ||
612 | static 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 | ||
d57bc36e MD |
631 | static 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, | |
641 | .ndo_set_mac_address = eth_mac_addr, | |
642 | .ndo_change_mtu = eth_change_mtu, | |
643 | #ifdef CONFIG_NET_POLL_CONTROLLER | |
644 | .ndo_poll_controller = ax_ei_poll, | |
645 | #endif | |
646 | }; | |
647 | ||
825a2ff1 BD |
648 | /* setup code */ |
649 | ||
650 | static 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 | ||
655 | /* Select page 0*/ | |
656 | ei_outb(E8390_NODMA+E8390_PAGE0+E8390_STOP, ioaddr + E8390_CMD); | |
657 | ||
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 | ||
663 | /* ax_init_dev | |
664 | * | |
665 | * initialise the specified device, taking care to note the MAC | |
666 | * address it may already have (if configured), ensure | |
667 | * the device is ready to be used by lib8390.c and registerd with | |
668 | * the network layer. | |
669 | */ | |
670 | ||
671 | static int ax_init_dev(struct net_device *dev, int first_init) | |
672 | { | |
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 | ||
691 | if (first_init && ax->plat->flags & AXFLG_HAS_EEPROM) { | |
692 | unsigned char SA_prom[32]; | |
693 | ||
694 | for(i = 0; i < sizeof(SA_prom); i+=2) { | |
695 | SA_prom[i] = ei_inb(ioaddr + NE_DATAPORT); | |
696 | SA_prom[i+1] = ei_inb(ioaddr + NE_DATAPORT); | |
697 | } | |
698 | ||
699 | if (ax->plat->wordlength == 2) | |
700 | for (i = 0; i < 16; i++) | |
701 | SA_prom[i] = SA_prom[i+i]; | |
702 | ||
703 | memcpy(dev->dev_addr, SA_prom, 6); | |
704 | } | |
705 | ||
89e536a1 MD |
706 | #ifdef CONFIG_AX88796_93CX6 |
707 | if (first_init && ax->plat->flags & AXFLG_HAS_93CX6) { | |
708 | 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 | ||
720 | memcpy(dev->dev_addr, mac_addr, 6); | |
721 | } | |
722 | #endif | |
825a2ff1 BD |
723 | 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 | ||
733 | /* load the mac-address from the device if this is the | |
734 | * first time we've initialised */ | |
735 | ||
67fca028 DM |
736 | if (first_init) { |
737 | if (ax->plat->flags & AXFLG_MAC_FROMDEV) { | |
738 | ei_outb(E8390_NODMA + E8390_PAGE1 + E8390_STOP, | |
739 | ei_local->mem + E8390_CMD); /* 0x61 */ | |
740 | for (i = 0; i < ETHER_ADDR_LEN; i++) | |
741 | dev->dev_addr[i] = | |
742 | ei_inb(ioaddr + EN1_PHYS_SHIFT(i)); | |
743 | } | |
825a2ff1 | 744 | |
67fca028 DM |
745 | if ((ax->plat->flags & AXFLG_MAC_FROMPLATFORM) && |
746 | ax->plat->mac_addr) | |
747 | memcpy(dev->dev_addr, ax->plat->mac_addr, | |
748 | ETHER_ADDR_LEN); | |
825a2ff1 BD |
749 | } |
750 | ||
751 | ax_reset_8390(dev); | |
752 | ||
753 | ei_status.name = "AX88796"; | |
754 | ei_status.tx_start_page = start_page; | |
755 | ei_status.stop_page = stop_page; | |
756 | ei_status.word16 = (ax->plat->wordlength == 2); | |
757 | ei_status.rx_start_page = start_page + TX_PAGES; | |
758 | ||
759 | #ifdef PACKETBUF_MEMSIZE | |
760 | /* Allow the packet buffer size to be overridden by know-it-alls. */ | |
761 | ei_status.stop_page = ei_status.tx_start_page + PACKETBUF_MEMSIZE; | |
762 | #endif | |
763 | ||
764 | ei_status.reset_8390 = &ax_reset_8390; | |
765 | ei_status.block_input = &ax_block_input; | |
766 | ei_status.block_output = &ax_block_output; | |
767 | ei_status.get_8390_hdr = &ax_get_8390_hdr; | |
768 | ei_status.priv = 0; | |
769 | ||
d57bc36e | 770 | dev->netdev_ops = &ax_netdev_ops; |
825a2ff1 BD |
771 | dev->ethtool_ops = &ax_ethtool_ops; |
772 | ||
773 | ax->msg_enable = NETIF_MSG_LINK; | |
774 | ax->mii.phy_id_mask = 0x1f; | |
775 | ax->mii.reg_num_mask = 0x1f; | |
776 | ax->mii.phy_id = 0x10; /* onboard phy */ | |
777 | ax->mii.force_media = 0; | |
778 | ax->mii.full_duplex = 0; | |
779 | ax->mii.mdio_read = ax_phy_read; | |
780 | ax->mii.mdio_write = ax_phy_write; | |
781 | ax->mii.dev = dev; | |
782 | ||
825a2ff1 BD |
783 | ax_NS8390_init(dev, 0); |
784 | ||
e174961c JB |
785 | if (first_init) |
786 | dev_info(&ax->dev->dev, "%dbit, irq %d, %lx, MAC: %pM\n", | |
237c5e8e | 787 | ei_status.word16 ? 16:8, dev->irq, dev->base_addr, |
e174961c | 788 | dev->dev_addr); |
825a2ff1 BD |
789 | |
790 | ret = register_netdev(dev); | |
791 | if (ret) | |
792 | goto out_irq; | |
793 | ||
794 | return 0; | |
795 | ||
796 | out_irq: | |
797 | /* cleanup irq */ | |
798 | free_irq(dev->irq, dev); | |
799 | err_out: | |
800 | return ret; | |
801 | } | |
802 | ||
803 | static int ax_remove(struct platform_device *_dev) | |
804 | { | |
805 | struct net_device *dev = platform_get_drvdata(_dev); | |
806 | struct ax_device *ax; | |
807 | ||
808 | ax = to_ax_dev(dev); | |
809 | ||
810 | unregister_netdev(dev); | |
811 | free_irq(dev->irq, dev); | |
812 | ||
813 | iounmap(ei_status.mem); | |
814 | release_resource(ax->mem); | |
815 | kfree(ax->mem); | |
816 | ||
817 | if (ax->map2) { | |
818 | iounmap(ax->map2); | |
819 | release_resource(ax->mem2); | |
820 | kfree(ax->mem2); | |
821 | } | |
822 | ||
823 | free_netdev(dev); | |
824 | ||
825 | return 0; | |
826 | } | |
827 | ||
828 | /* ax_probe | |
829 | * | |
830 | * This is the entry point when the platform device system uses to | |
831 | * notify us of a new device to attach to. Allocate memory, find | |
832 | * the resources and information passed, and map the necessary registers. | |
833 | */ | |
834 | ||
835 | static int ax_probe(struct platform_device *pdev) | |
836 | { | |
837 | struct net_device *dev; | |
838 | struct ax_device *ax; | |
839 | struct resource *res; | |
840 | size_t size; | |
47cb0355 | 841 | int ret = 0; |
825a2ff1 BD |
842 | |
843 | dev = ax__alloc_ei_netdev(sizeof(struct ax_device)); | |
844 | if (dev == NULL) | |
845 | return -ENOMEM; | |
846 | ||
847 | /* ok, let's setup our device */ | |
848 | ax = to_ax_dev(dev); | |
849 | ||
850 | memset(ax, 0, sizeof(struct ax_device)); | |
851 | ||
852 | spin_lock_init(&ax->mii_lock); | |
853 | ||
854 | ax->dev = pdev; | |
855 | ax->plat = pdev->dev.platform_data; | |
856 | platform_set_drvdata(pdev, dev); | |
857 | ||
858 | ei_status.rxcr_base = ax->plat->rcr_val; | |
859 | ||
860 | /* find the platform resources */ | |
861 | ||
47cb0355 DM |
862 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
863 | if (res == NULL) { | |
825a2ff1 | 864 | dev_err(&pdev->dev, "no IRQ specified\n"); |
825a2ff1 BD |
865 | goto exit_mem; |
866 | } | |
47cb0355 DM |
867 | |
868 | dev->irq = res->start; | |
869 | ax->irqflags = res->flags & IRQF_TRIGGER_MASK; | |
825a2ff1 BD |
870 | |
871 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
872 | if (res == NULL) { | |
873 | dev_err(&pdev->dev, "no MEM specified\n"); | |
874 | ret = -ENXIO; | |
875 | goto exit_mem; | |
876 | } | |
877 | ||
878 | size = (res->end - res->start) + 1; | |
879 | ||
880 | /* setup the register offsets from either the platform data | |
881 | * or by using the size of the resource provided */ | |
882 | ||
883 | if (ax->plat->reg_offsets) | |
884 | ei_status.reg_offset = ax->plat->reg_offsets; | |
885 | else { | |
886 | ei_status.reg_offset = ax->reg_offsets; | |
887 | for (ret = 0; ret < 0x18; ret++) | |
888 | ax->reg_offsets[ret] = (size / 0x18) * ret; | |
889 | } | |
890 | ||
891 | ax->mem = request_mem_region(res->start, size, pdev->name); | |
892 | if (ax->mem == NULL) { | |
893 | dev_err(&pdev->dev, "cannot reserve registers\n"); | |
894 | ret = -ENXIO; | |
895 | goto exit_mem; | |
896 | } | |
897 | ||
898 | ei_status.mem = ioremap(res->start, size); | |
c7b17cb1 | 899 | dev->base_addr = (unsigned long)ei_status.mem; |
825a2ff1 BD |
900 | |
901 | if (ei_status.mem == NULL) { | |
b4efe22c AM |
902 | dev_err(&pdev->dev, "Cannot ioremap area (%08llx,%08llx)\n", |
903 | (unsigned long long)res->start, | |
904 | (unsigned long long)res->end); | |
825a2ff1 BD |
905 | |
906 | ret = -ENXIO; | |
907 | goto exit_req; | |
908 | } | |
909 | ||
910 | /* look for reset area */ | |
911 | ||
912 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); | |
913 | if (res == NULL) { | |
914 | if (!ax->plat->reg_offsets) { | |
915 | for (ret = 0; ret < 0x20; ret++) | |
916 | ax->reg_offsets[ret] = (size / 0x20) * ret; | |
917 | } | |
918 | ||
919 | ax->map2 = NULL; | |
920 | } else { | |
921 | size = (res->end - res->start) + 1; | |
922 | ||
923 | ax->mem2 = request_mem_region(res->start, size, pdev->name); | |
924 | if (ax->mem == NULL) { | |
925 | dev_err(&pdev->dev, "cannot reserve registers\n"); | |
926 | ret = -ENXIO; | |
927 | goto exit_mem1; | |
928 | } | |
929 | ||
930 | ax->map2 = ioremap(res->start, size); | |
931 | if (ax->map2 == NULL) { | |
898eb71c | 932 | dev_err(&pdev->dev, "cannot map reset register\n"); |
825a2ff1 BD |
933 | ret = -ENXIO; |
934 | goto exit_mem2; | |
935 | } | |
936 | ||
937 | ei_status.reg_offset[0x1f] = ax->map2 - ei_status.mem; | |
938 | } | |
939 | ||
940 | /* got resources, now initialise and register device */ | |
941 | ||
942 | ret = ax_init_dev(dev, 1); | |
943 | if (!ret) | |
944 | return 0; | |
945 | ||
946 | if (ax->map2 == NULL) | |
947 | goto exit_mem1; | |
948 | ||
949 | iounmap(ax->map2); | |
950 | ||
951 | exit_mem2: | |
952 | release_resource(ax->mem2); | |
953 | kfree(ax->mem2); | |
954 | ||
955 | exit_mem1: | |
956 | iounmap(ei_status.mem); | |
957 | ||
958 | exit_req: | |
959 | release_resource(ax->mem); | |
960 | kfree(ax->mem); | |
961 | ||
962 | exit_mem: | |
963 | free_netdev(dev); | |
964 | ||
965 | return ret; | |
966 | } | |
967 | ||
968 | /* suspend and resume */ | |
969 | ||
970 | #ifdef CONFIG_PM | |
971 | static int ax_suspend(struct platform_device *dev, pm_message_t state) | |
972 | { | |
973 | struct net_device *ndev = platform_get_drvdata(dev); | |
974 | struct ax_device *ax = to_ax_dev(ndev); | |
975 | ||
976 | ax->resume_open = ax->running; | |
977 | ||
978 | netif_device_detach(ndev); | |
979 | ax_close(ndev); | |
980 | ||
981 | return 0; | |
982 | } | |
983 | ||
984 | static int ax_resume(struct platform_device *pdev) | |
985 | { | |
986 | struct net_device *ndev = platform_get_drvdata(pdev); | |
987 | struct ax_device *ax = to_ax_dev(ndev); | |
988 | ||
989 | ax_initial_setup(ndev, netdev_priv(ndev)); | |
990 | ax_NS8390_init(ndev, ax->resume_open); | |
991 | netif_device_attach(ndev); | |
992 | ||
993 | if (ax->resume_open) | |
994 | ax_open(ndev); | |
995 | ||
996 | return 0; | |
997 | } | |
998 | ||
999 | #else | |
1000 | #define ax_suspend NULL | |
1001 | #define ax_resume NULL | |
1002 | #endif | |
1003 | ||
1004 | static struct platform_driver axdrv = { | |
1005 | .driver = { | |
1006 | .name = "ax88796", | |
1007 | .owner = THIS_MODULE, | |
1008 | }, | |
1009 | .probe = ax_probe, | |
1010 | .remove = ax_remove, | |
1011 | .suspend = ax_suspend, | |
1012 | .resume = ax_resume, | |
1013 | }; | |
1014 | ||
1015 | static int __init axdrv_init(void) | |
1016 | { | |
1017 | return platform_driver_register(&axdrv); | |
1018 | } | |
1019 | ||
1020 | static void __exit axdrv_exit(void) | |
1021 | { | |
1022 | platform_driver_unregister(&axdrv); | |
1023 | } | |
1024 | ||
1025 | module_init(axdrv_init); | |
1026 | module_exit(axdrv_exit); | |
1027 | ||
1028 | MODULE_DESCRIPTION("AX88796 10/100 Ethernet platform driver"); | |
1029 | MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>"); | |
1030 | MODULE_LICENSE("GPL v2"); | |
72abb461 | 1031 | MODULE_ALIAS("platform:ax88796"); |