blob: 35cd264abae70e6436e9e0e2a1288a7aa5484c89 [file] [log] [blame]
Daniel Silverstone7a3c66e2008-12-11 21:00:29 -08001/*
2 * Micrel KS8695 (Centaur) Ethernet.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * Copyright 2008 Simtec Electronics
15 * Daniel Silverstone <dsilvers@simtec.co.uk>
16 * Vincent Sanders <vince@simtec.co.uk>
17 */
18
19#include <linux/module.h>
20#include <linux/ioport.h>
21#include <linux/netdevice.h>
22#include <linux/etherdevice.h>
23#include <linux/init.h>
24#include <linux/skbuff.h>
25#include <linux/spinlock.h>
26#include <linux/crc32.h>
27#include <linux/mii.h>
28#include <linux/ethtool.h>
29#include <linux/delay.h>
30#include <linux/platform_device.h>
31#include <linux/irq.h>
Daniel Silverstone7a3c66e2008-12-11 21:00:29 -080032#include <linux/io.h>
33
34#include <asm/irq.h>
35
36#include <mach/regs-switch.h>
37#include <mach/regs-misc.h>
38
39#include "ks8695net.h"
40
41#define MODULENAME "ks8695_ether"
42#define MODULEVERSION "1.01"
43
44/*
45 * Transmit and device reset timeout, default 5 seconds.
46 */
47static int watchdog = 5000;
48
49/* Hardware structures */
50
51/**
52 * struct rx_ring_desc - Receive descriptor ring element
53 * @status: The status of the descriptor element (E.g. who owns it)
54 * @length: The number of bytes in the block pointed to by data_ptr
55 * @data_ptr: The physical address of the data block to receive into
56 * @next_desc: The physical address of the next descriptor element.
57 */
58struct rx_ring_desc {
59 __le32 status;
60 __le32 length;
61 __le32 data_ptr;
62 __le32 next_desc;
63};
64
65/**
66 * struct tx_ring_desc - Transmit descriptor ring element
67 * @owner: Who owns the descriptor
68 * @status: The number of bytes in the block pointed to by data_ptr
69 * @data_ptr: The physical address of the data block to receive into
70 * @next_desc: The physical address of the next descriptor element.
71 */
72struct tx_ring_desc {
73 __le32 owner;
74 __le32 status;
75 __le32 data_ptr;
76 __le32 next_desc;
77};
78
79/**
80 * struct ks8695_skbuff - sk_buff wrapper for rx/tx rings.
81 * @skb: The buffer in the ring
82 * @dma_ptr: The mapped DMA pointer of the buffer
83 * @length: The number of bytes mapped to dma_ptr
84 */
85struct ks8695_skbuff {
86 struct sk_buff *skb;
87 dma_addr_t dma_ptr;
88 u32 length;
89};
90
91/* Private device structure */
92
93#define MAX_TX_DESC 8
94#define MAX_TX_DESC_MASK 0x7
95#define MAX_RX_DESC 16
96#define MAX_RX_DESC_MASK 0xf
97
98#define MAX_RXBUF_SIZE 0x700
99
100#define TX_RING_DMA_SIZE (sizeof(struct tx_ring_desc) * MAX_TX_DESC)
101#define RX_RING_DMA_SIZE (sizeof(struct rx_ring_desc) * MAX_RX_DESC)
102#define RING_DMA_SIZE (TX_RING_DMA_SIZE + RX_RING_DMA_SIZE)
103
104/**
105 * enum ks8695_dtype - Device type
106 * @KS8695_DTYPE_WAN: This device is a WAN interface
107 * @KS8695_DTYPE_LAN: This device is a LAN interface
108 * @KS8695_DTYPE_HPNA: This device is an HPNA interface
109 */
110enum ks8695_dtype {
111 KS8695_DTYPE_WAN,
112 KS8695_DTYPE_LAN,
113 KS8695_DTYPE_HPNA,
114};
115
116/**
117 * struct ks8695_priv - Private data for the KS8695 Ethernet
118 * @in_suspend: Flag to indicate if we're suspending/resuming
119 * @ndev: The net_device for this interface
120 * @dev: The platform device object for this interface
121 * @dtype: The type of this device
122 * @io_regs: The ioremapped registers for this interface
123 * @rx_irq_name: The textual name of the RX IRQ from the platform data
124 * @tx_irq_name: The textual name of the TX IRQ from the platform data
125 * @link_irq_name: The textual name of the link IRQ from the
126 * platform data if available
127 * @rx_irq: The IRQ number for the RX IRQ
128 * @tx_irq: The IRQ number for the TX IRQ
129 * @link_irq: The IRQ number for the link IRQ if available
130 * @regs_req: The resource request for the registers region
131 * @phyiface_req: The resource request for the phy/switch region
132 * if available
133 * @phyiface_regs: The ioremapped registers for the phy/switch if available
134 * @ring_base: The base pointer of the dma coherent memory for the rings
135 * @ring_base_dma: The DMA mapped equivalent of ring_base
136 * @tx_ring: The pointer in ring_base of the TX ring
137 * @tx_ring_used: The number of slots in the TX ring which are occupied
138 * @tx_ring_next_slot: The next slot to fill in the TX ring
139 * @tx_ring_dma: The DMA mapped equivalent of tx_ring
140 * @tx_buffers: The sk_buff mappings for the TX ring
141 * @txq_lock: A lock to protect the tx_buffers tx_ring_used etc variables
142 * @rx_ring: The pointer in ring_base of the RX ring
143 * @rx_ring_dma: The DMA mapped equivalent of rx_ring
144 * @rx_buffers: The sk_buff mappings for the RX ring
145 * @next_rx_desc_read: The next RX descriptor to read from on IRQ
146 * @msg_enable: The flags for which messages to emit
147 */
148struct ks8695_priv {
149 int in_suspend;
150 struct net_device *ndev;
151 struct device *dev;
152 enum ks8695_dtype dtype;
153 void __iomem *io_regs;
154
155 const char *rx_irq_name, *tx_irq_name, *link_irq_name;
156 int rx_irq, tx_irq, link_irq;
157
158 struct resource *regs_req, *phyiface_req;
159 void __iomem *phyiface_regs;
160
161 void *ring_base;
162 dma_addr_t ring_base_dma;
163
164 struct tx_ring_desc *tx_ring;
165 int tx_ring_used;
166 int tx_ring_next_slot;
167 dma_addr_t tx_ring_dma;
168 struct ks8695_skbuff tx_buffers[MAX_TX_DESC];
169 spinlock_t txq_lock;
170
171 struct rx_ring_desc *rx_ring;
172 dma_addr_t rx_ring_dma;
173 struct ks8695_skbuff rx_buffers[MAX_RX_DESC];
174 int next_rx_desc_read;
175
176 int msg_enable;
177};
178
179/* Register access */
180
181/**
182 * ks8695_readreg - Read from a KS8695 ethernet register
183 * @ksp: The device to read from
184 * @reg: The register to read
185 */
186static inline u32
187ks8695_readreg(struct ks8695_priv *ksp, int reg)
188{
189 return readl(ksp->io_regs + reg);
190}
191
192/**
193 * ks8695_writereg - Write to a KS8695 ethernet register
194 * @ksp: The device to write to
195 * @reg: The register to write
196 * @value: The value to write to the register
197 */
198static inline void
199ks8695_writereg(struct ks8695_priv *ksp, int reg, u32 value)
200{
201 writel(value, ksp->io_regs + reg);
202}
203
204/* Utility functions */
205
206/**
207 * ks8695_port_type - Retrieve port-type as user-friendly string
208 * @ksp: The device to return the type for
209 *
210 * Returns a string indicating which of the WAN, LAN or HPNA
211 * ports this device is likely to represent.
212 */
213static const char *
214ks8695_port_type(struct ks8695_priv *ksp)
215{
216 switch (ksp->dtype) {
217 case KS8695_DTYPE_LAN:
218 return "LAN";
219 case KS8695_DTYPE_WAN:
220 return "WAN";
221 case KS8695_DTYPE_HPNA:
222 return "HPNA";
223 }
224
225 return "UNKNOWN";
226}
227
228/**
229 * ks8695_update_mac - Update the MAC registers in the device
230 * @ksp: The device to update
231 *
232 * Updates the MAC registers in the KS8695 device from the address in the
233 * net_device structure associated with this interface.
234 */
235static void
236ks8695_update_mac(struct ks8695_priv *ksp)
237{
238 /* Update the HW with the MAC from the net_device */
239 struct net_device *ndev = ksp->ndev;
240 u32 machigh, maclow;
241
242 maclow = ((ndev->dev_addr[2] << 24) | (ndev->dev_addr[3] << 16) |
243 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5] << 0));
244 machigh = ((ndev->dev_addr[0] << 8) | (ndev->dev_addr[1] << 0));
245
246 ks8695_writereg(ksp, KS8695_MAL, maclow);
247 ks8695_writereg(ksp, KS8695_MAH, machigh);
248
249}
250
251/**
252 * ks8695_refill_rxbuffers - Re-fill the RX buffer ring
253 * @ksp: The device to refill
254 *
255 * Iterates the RX ring of the device looking for empty slots.
256 * For each empty slot, we allocate and map a new SKB and give it
257 * to the hardware.
258 * This can be called from interrupt context safely.
259 */
260static void
261ks8695_refill_rxbuffers(struct ks8695_priv *ksp)
262{
263 /* Run around the RX ring, filling in any missing sk_buff's */
264 int buff_n;
265
266 for (buff_n = 0; buff_n < MAX_RX_DESC; ++buff_n) {
267 if (!ksp->rx_buffers[buff_n].skb) {
268 struct sk_buff *skb = dev_alloc_skb(MAX_RXBUF_SIZE);
269 dma_addr_t mapping;
270
271 ksp->rx_buffers[buff_n].skb = skb;
272 if (skb == NULL) {
273 /* Failed to allocate one, perhaps
274 * we'll try again later.
275 */
276 break;
277 }
278
279 mapping = dma_map_single(ksp->dev, skb->data,
280 MAX_RXBUF_SIZE,
281 DMA_FROM_DEVICE);
282 if (unlikely(dma_mapping_error(ksp->dev, mapping))) {
283 /* Failed to DMA map this SKB, try later */
284 dev_kfree_skb_irq(skb);
285 ksp->rx_buffers[buff_n].skb = NULL;
286 break;
287 }
288 ksp->rx_buffers[buff_n].dma_ptr = mapping;
289 skb->dev = ksp->ndev;
290 ksp->rx_buffers[buff_n].length = MAX_RXBUF_SIZE;
291
292 /* Record this into the DMA ring */
293 ksp->rx_ring[buff_n].data_ptr = cpu_to_le32(mapping);
294 ksp->rx_ring[buff_n].length =
295 cpu_to_le32(MAX_RXBUF_SIZE);
296
297 wmb();
298
299 /* And give ownership over to the hardware */
300 ksp->rx_ring[buff_n].status = cpu_to_le32(RDES_OWN);
301 }
302 }
303}
304
305/* Maximum number of multicast addresses which the KS8695 HW supports */
306#define KS8695_NR_ADDRESSES 16
307
308/**
309 * ks8695_init_partial_multicast - Init the mcast addr registers
310 * @ksp: The device to initialise
311 * @addr: The multicast address list to use
312 * @nr_addr: The number of addresses in the list
313 *
314 * This routine is a helper for ks8695_set_multicast - it writes
315 * the additional-address registers in the KS8695 ethernet device
316 * and cleans up any others left behind.
317 */
318static void
319ks8695_init_partial_multicast(struct ks8695_priv *ksp,
320 struct dev_mc_list *addr,
321 int nr_addr)
322{
323 u32 low, high;
324 int i;
325
326 for (i = 0; i < nr_addr; i++, addr = addr->next) {
327 /* Ran out of addresses? */
328 if (!addr)
329 break;
330 /* Ran out of space in chip? */
331 BUG_ON(i == KS8695_NR_ADDRESSES);
332
333 low = (addr->dmi_addr[2] << 24) | (addr->dmi_addr[3] << 16) |
334 (addr->dmi_addr[4] << 8) | (addr->dmi_addr[5]);
335 high = (addr->dmi_addr[0] << 8) | (addr->dmi_addr[1]);
336
337 ks8695_writereg(ksp, KS8695_AAL_(i), low);
338 ks8695_writereg(ksp, KS8695_AAH_(i), AAH_E | high);
339 }
340
341 /* Clear the remaining Additional Station Addresses */
342 for (; i < KS8695_NR_ADDRESSES; i++) {
343 ks8695_writereg(ksp, KS8695_AAL_(i), 0);
344 ks8695_writereg(ksp, KS8695_AAH_(i), 0);
345 }
346}
347
348/* Interrupt handling */
349
350/**
351 * ks8695_tx_irq - Transmit IRQ handler
352 * @irq: The IRQ which went off (ignored)
353 * @dev_id: The net_device for the interrupt
354 *
355 * Process the TX ring, clearing out any transmitted slots.
356 * Allows the net_device to pass us new packets once slots are
357 * freed.
358 */
359static irqreturn_t
360ks8695_tx_irq(int irq, void *dev_id)
361{
362 struct net_device *ndev = (struct net_device *)dev_id;
363 struct ks8695_priv *ksp = netdev_priv(ndev);
364 int buff_n;
365
366 for (buff_n = 0; buff_n < MAX_TX_DESC; ++buff_n) {
367 if (ksp->tx_buffers[buff_n].skb &&
368 !(ksp->tx_ring[buff_n].owner & cpu_to_le32(TDES_OWN))) {
369 rmb();
370 /* An SKB which is not owned by HW is present */
371 /* Update the stats for the net_device */
372 ndev->stats.tx_packets++;
373 ndev->stats.tx_bytes += ksp->tx_buffers[buff_n].length;
374
375 /* Free the packet from the ring */
376 ksp->tx_ring[buff_n].data_ptr = 0;
377
378 /* Free the sk_buff */
379 dma_unmap_single(ksp->dev,
380 ksp->tx_buffers[buff_n].dma_ptr,
381 ksp->tx_buffers[buff_n].length,
382 DMA_TO_DEVICE);
383 dev_kfree_skb_irq(ksp->tx_buffers[buff_n].skb);
384 ksp->tx_buffers[buff_n].skb = NULL;
385 ksp->tx_ring_used--;
386 }
387 }
388
389 netif_wake_queue(ndev);
390
391 return IRQ_HANDLED;
392}
393
394/**
395 * ks8695_rx_irq - Receive IRQ handler
396 * @irq: The IRQ which went off (ignored)
397 * @dev_id: The net_device for the interrupt
398 *
399 * Process the RX ring, passing any received packets up to the
400 * host. If we received anything other than errors, we then
401 * refill the ring.
402 */
403static irqreturn_t
404ks8695_rx_irq(int irq, void *dev_id)
405{
406 struct net_device *ndev = (struct net_device *)dev_id;
407 struct ks8695_priv *ksp = netdev_priv(ndev);
408 struct sk_buff *skb;
409 int buff_n;
410 u32 flags;
411 int pktlen;
412 int last_rx_processed = -1;
413
414 buff_n = ksp->next_rx_desc_read;
415 do {
416 if (ksp->rx_buffers[buff_n].skb &&
417 !(ksp->rx_ring[buff_n].status & cpu_to_le32(RDES_OWN))) {
418 rmb();
419 flags = le32_to_cpu(ksp->rx_ring[buff_n].status);
420 /* Found an SKB which we own, this means we
421 * received a packet
422 */
423 if ((flags & (RDES_FS | RDES_LS)) !=
424 (RDES_FS | RDES_LS)) {
425 /* This packet is not the first and
426 * the last segment. Therefore it is
427 * a "spanning" packet and we can't
428 * handle it
429 */
430 goto rx_failure;
431 }
432
433 if (flags & (RDES_ES | RDES_RE)) {
434 /* It's an error packet */
435 ndev->stats.rx_errors++;
436 if (flags & RDES_TL)
437 ndev->stats.rx_length_errors++;
438 if (flags & RDES_RF)
439 ndev->stats.rx_length_errors++;
440 if (flags & RDES_CE)
441 ndev->stats.rx_crc_errors++;
442 if (flags & RDES_RE)
443 ndev->stats.rx_missed_errors++;
444
445 goto rx_failure;
446 }
447
448 pktlen = flags & RDES_FLEN;
449 pktlen -= 4; /* Drop the CRC */
450
451 /* Retrieve the sk_buff */
452 skb = ksp->rx_buffers[buff_n].skb;
453
454 /* Clear it from the ring */
455 ksp->rx_buffers[buff_n].skb = NULL;
456 ksp->rx_ring[buff_n].data_ptr = 0;
457
458 /* Unmap the SKB */
459 dma_unmap_single(ksp->dev,
460 ksp->rx_buffers[buff_n].dma_ptr,
461 ksp->rx_buffers[buff_n].length,
462 DMA_FROM_DEVICE);
463
464 /* Relinquish the SKB to the network layer */
465 skb_put(skb, pktlen);
466 skb->protocol = eth_type_trans(skb, ndev);
467 netif_rx(skb);
468
469 /* Record stats */
470 ndev->last_rx = jiffies;
471 ndev->stats.rx_packets++;
472 ndev->stats.rx_bytes += pktlen;
473 goto rx_finished;
474
475rx_failure:
476 /* This ring entry is an error, but we can
477 * re-use the skb
478 */
479 /* Give the ring entry back to the hardware */
480 ksp->rx_ring[buff_n].status = cpu_to_le32(RDES_OWN);
481rx_finished:
482 /* And note this as processed so we can start
483 * from here next time
484 */
485 last_rx_processed = buff_n;
486 } else {
487 /* Ran out of things to process, stop now */
488 break;
489 }
490 buff_n = (buff_n + 1) & MAX_RX_DESC_MASK;
491 } while (buff_n != ksp->next_rx_desc_read);
492
493 /* And note which RX descriptor we last did anything with */
494 if (likely(last_rx_processed != -1))
495 ksp->next_rx_desc_read =
496 (last_rx_processed + 1) & MAX_RX_DESC_MASK;
497
498 /* And refill the buffers */
499 ks8695_refill_rxbuffers(ksp);
500
501 /* Kick the RX DMA engine, in case it became suspended */
502 ks8695_writereg(ksp, KS8695_DRSC, 0);
503
504 return IRQ_HANDLED;
505}
506
507/**
508 * ks8695_link_irq - Link change IRQ handler
509 * @irq: The IRQ which went off (ignored)
510 * @dev_id: The net_device for the interrupt
511 *
512 * The WAN interface can generate an IRQ when the link changes,
513 * report this to the net layer and the user.
514 */
515static irqreturn_t
516ks8695_link_irq(int irq, void *dev_id)
517{
518 struct net_device *ndev = (struct net_device *)dev_id;
519 struct ks8695_priv *ksp = netdev_priv(ndev);
520 u32 ctrl;
521
522 ctrl = readl(ksp->phyiface_regs + KS8695_WMC);
523 if (ctrl & WMC_WLS) {
524 netif_carrier_on(ndev);
525 if (netif_msg_link(ksp))
526 dev_info(ksp->dev,
527 "%s: Link is now up (10%sMbps/%s-duplex)\n",
528 ndev->name,
529 (ctrl & WMC_WSS) ? "0" : "",
530 (ctrl & WMC_WDS) ? "Full" : "Half");
531 } else {
532 netif_carrier_off(ndev);
533 if (netif_msg_link(ksp))
534 dev_info(ksp->dev, "%s: Link is now down.\n",
535 ndev->name);
536 }
537
538 return IRQ_HANDLED;
539}
540
541
542/* KS8695 Device functions */
543
544/**
545 * ks8695_reset - Reset a KS8695 ethernet interface
546 * @ksp: The interface to reset
547 *
548 * Perform an engine reset of the interface and re-program it
549 * with sensible defaults.
550 */
551static void
552ks8695_reset(struct ks8695_priv *ksp)
553{
554 int reset_timeout = watchdog;
555 /* Issue the reset via the TX DMA control register */
556 ks8695_writereg(ksp, KS8695_DTXC, DTXC_TRST);
557 while (reset_timeout--) {
558 if (!(ks8695_readreg(ksp, KS8695_DTXC) & DTXC_TRST))
559 break;
560 msleep(1);
561 }
562
Roel Kluin858b9ce2009-03-04 00:11:42 -0800563 if (reset_timeout < 0) {
Daniel Silverstone7a3c66e2008-12-11 21:00:29 -0800564 dev_crit(ksp->dev,
565 "Timeout waiting for DMA engines to reset\n");
566 /* And blithely carry on */
567 }
568
569 /* Definitely wait long enough before attempting to program
570 * the engines
571 */
572 msleep(10);
573
574 /* RX: unicast and broadcast */
575 ks8695_writereg(ksp, KS8695_DRXC, DRXC_RU | DRXC_RB);
576 /* TX: pad and add CRC */
577 ks8695_writereg(ksp, KS8695_DTXC, DTXC_TEP | DTXC_TAC);
578}
579
580/**
581 * ks8695_shutdown - Shut down a KS8695 ethernet interface
582 * @ksp: The interface to shut down
583 *
584 * This disables packet RX/TX, cleans up IRQs, drains the rings,
585 * and basically places the interface into a clean shutdown
586 * state.
587 */
588static void
589ks8695_shutdown(struct ks8695_priv *ksp)
590{
591 u32 ctrl;
592 int buff_n;
593
594 /* Disable packet transmission */
595 ctrl = ks8695_readreg(ksp, KS8695_DTXC);
596 ks8695_writereg(ksp, KS8695_DTXC, ctrl & ~DTXC_TE);
597
598 /* Disable packet reception */
599 ctrl = ks8695_readreg(ksp, KS8695_DRXC);
600 ks8695_writereg(ksp, KS8695_DRXC, ctrl & ~DRXC_RE);
601
602 /* Release the IRQs */
603 free_irq(ksp->rx_irq, ksp->ndev);
604 free_irq(ksp->tx_irq, ksp->ndev);
605 if (ksp->link_irq != -1)
606 free_irq(ksp->link_irq, ksp->ndev);
607
608 /* Throw away any pending TX packets */
609 for (buff_n = 0; buff_n < MAX_TX_DESC; ++buff_n) {
610 if (ksp->tx_buffers[buff_n].skb) {
611 /* Remove this SKB from the TX ring */
612 ksp->tx_ring[buff_n].owner = 0;
613 ksp->tx_ring[buff_n].status = 0;
614 ksp->tx_ring[buff_n].data_ptr = 0;
615
616 /* Unmap and bin this SKB */
617 dma_unmap_single(ksp->dev,
618 ksp->tx_buffers[buff_n].dma_ptr,
619 ksp->tx_buffers[buff_n].length,
620 DMA_TO_DEVICE);
621 dev_kfree_skb_irq(ksp->tx_buffers[buff_n].skb);
622 ksp->tx_buffers[buff_n].skb = NULL;
623 }
624 }
625
626 /* Purge the RX buffers */
627 for (buff_n = 0; buff_n < MAX_RX_DESC; ++buff_n) {
628 if (ksp->rx_buffers[buff_n].skb) {
629 /* Remove the SKB from the RX ring */
630 ksp->rx_ring[buff_n].status = 0;
631 ksp->rx_ring[buff_n].data_ptr = 0;
632
633 /* Unmap and bin the SKB */
634 dma_unmap_single(ksp->dev,
635 ksp->rx_buffers[buff_n].dma_ptr,
636 ksp->rx_buffers[buff_n].length,
637 DMA_FROM_DEVICE);
638 dev_kfree_skb_irq(ksp->rx_buffers[buff_n].skb);
639 ksp->rx_buffers[buff_n].skb = NULL;
640 }
641 }
642}
643
644
645/**
646 * ks8695_setup_irq - IRQ setup helper function
647 * @irq: The IRQ number to claim
648 * @irq_name: The name to give the IRQ claimant
649 * @handler: The function to call to handle the IRQ
650 * @ndev: The net_device to pass in as the dev_id argument to the handler
651 *
652 * Return 0 on success.
653 */
654static int
655ks8695_setup_irq(int irq, const char *irq_name,
656 irq_handler_t handler, struct net_device *ndev)
657{
658 int ret;
659
660 ret = request_irq(irq, handler, IRQF_SHARED, irq_name, ndev);
661
662 if (ret) {
663 dev_err(&ndev->dev, "failure to request IRQ %d\n", irq);
664 return ret;
665 }
666
667 return 0;
668}
669
670/**
671 * ks8695_init_net - Initialise a KS8695 ethernet interface
672 * @ksp: The interface to initialise
673 *
674 * This routine fills the RX ring, initialises the DMA engines,
675 * allocates the IRQs and then starts the packet TX and RX
676 * engines.
677 */
678static int
679ks8695_init_net(struct ks8695_priv *ksp)
680{
681 int ret;
682 u32 ctrl;
683
684 ks8695_refill_rxbuffers(ksp);
685
686 /* Initialise the DMA engines */
687 ks8695_writereg(ksp, KS8695_RDLB, (u32) ksp->rx_ring_dma);
688 ks8695_writereg(ksp, KS8695_TDLB, (u32) ksp->tx_ring_dma);
689
690 /* Request the IRQs */
691 ret = ks8695_setup_irq(ksp->rx_irq, ksp->rx_irq_name,
692 ks8695_rx_irq, ksp->ndev);
693 if (ret)
694 return ret;
695 ret = ks8695_setup_irq(ksp->tx_irq, ksp->tx_irq_name,
696 ks8695_tx_irq, ksp->ndev);
697 if (ret)
698 return ret;
699 if (ksp->link_irq != -1) {
700 ret = ks8695_setup_irq(ksp->link_irq, ksp->link_irq_name,
701 ks8695_link_irq, ksp->ndev);
702 if (ret)
703 return ret;
704 }
705
706 /* Set up the ring indices */
707 ksp->next_rx_desc_read = 0;
708 ksp->tx_ring_next_slot = 0;
709 ksp->tx_ring_used = 0;
710
711 /* Bring up transmission */
712 ctrl = ks8695_readreg(ksp, KS8695_DTXC);
713 /* Enable packet transmission */
714 ks8695_writereg(ksp, KS8695_DTXC, ctrl | DTXC_TE);
715
716 /* Bring up the reception */
717 ctrl = ks8695_readreg(ksp, KS8695_DRXC);
718 /* Enable packet reception */
719 ks8695_writereg(ksp, KS8695_DRXC, ctrl | DRXC_RE);
720 /* And start the DMA engine */
721 ks8695_writereg(ksp, KS8695_DRSC, 0);
722
723 /* All done */
724 return 0;
725}
726
727/**
728 * ks8695_release_device - HW resource release for KS8695 e-net
729 * @ksp: The device to be freed
730 *
731 * This unallocates io memory regions, dma-coherent regions etc
732 * which were allocated in ks8695_probe.
733 */
734static void
735ks8695_release_device(struct ks8695_priv *ksp)
736{
737 /* Unmap the registers */
738 iounmap(ksp->io_regs);
739 if (ksp->phyiface_regs)
740 iounmap(ksp->phyiface_regs);
741
742 /* And release the request */
743 release_resource(ksp->regs_req);
744 kfree(ksp->regs_req);
745 if (ksp->phyiface_req) {
746 release_resource(ksp->phyiface_req);
747 kfree(ksp->phyiface_req);
748 }
749
750 /* Free the ring buffers */
751 dma_free_coherent(ksp->dev, RING_DMA_SIZE,
752 ksp->ring_base, ksp->ring_base_dma);
753}
754
755/* Ethtool support */
756
757/**
758 * ks8695_get_msglevel - Get the messages enabled for emission
759 * @ndev: The network device to read from
760 */
761static u32
762ks8695_get_msglevel(struct net_device *ndev)
763{
764 struct ks8695_priv *ksp = netdev_priv(ndev);
765
766 return ksp->msg_enable;
767}
768
769/**
770 * ks8695_set_msglevel - Set the messages enabled for emission
771 * @ndev: The network device to configure
772 * @value: The messages to set for emission
773 */
774static void
775ks8695_set_msglevel(struct net_device *ndev, u32 value)
776{
777 struct ks8695_priv *ksp = netdev_priv(ndev);
778
779 ksp->msg_enable = value;
780}
781
782/**
783 * ks8695_get_settings - Get device-specific settings.
784 * @ndev: The network device to read settings from
785 * @cmd: The ethtool structure to read into
786 */
787static int
788ks8695_get_settings(struct net_device *ndev, struct ethtool_cmd *cmd)
789{
790 struct ks8695_priv *ksp = netdev_priv(ndev);
791 u32 ctrl;
792
793 /* All ports on the KS8695 support these... */
794 cmd->supported = (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
795 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
796 SUPPORTED_TP | SUPPORTED_MII);
797 cmd->transceiver = XCVR_INTERNAL;
798
799 /* Port specific extras */
800 switch (ksp->dtype) {
801 case KS8695_DTYPE_HPNA:
802 cmd->phy_address = 0;
803 /* not supported for HPNA */
804 cmd->autoneg = AUTONEG_DISABLE;
805
806 /* BUG: Erm, dtype hpna implies no phy regs */
807 /*
808 ctrl = readl(KS8695_MISC_VA + KS8695_HMC);
809 cmd->speed = (ctrl & HMC_HSS) ? SPEED_100 : SPEED_10;
810 cmd->duplex = (ctrl & HMC_HDS) ? DUPLEX_FULL : DUPLEX_HALF;
811 */
812 return -EOPNOTSUPP;
813 case KS8695_DTYPE_WAN:
814 cmd->advertising = ADVERTISED_TP | ADVERTISED_MII;
815 cmd->port = PORT_MII;
816 cmd->supported |= (SUPPORTED_Autoneg | SUPPORTED_Pause);
817 cmd->phy_address = 0;
818
819 ctrl = readl(ksp->phyiface_regs + KS8695_WMC);
820 if ((ctrl & WMC_WAND) == 0) {
821 /* auto-negotiation is enabled */
822 cmd->advertising |= ADVERTISED_Autoneg;
823 if (ctrl & WMC_WANA100F)
824 cmd->advertising |= ADVERTISED_100baseT_Full;
825 if (ctrl & WMC_WANA100H)
826 cmd->advertising |= ADVERTISED_100baseT_Half;
827 if (ctrl & WMC_WANA10F)
828 cmd->advertising |= ADVERTISED_10baseT_Full;
829 if (ctrl & WMC_WANA10H)
830 cmd->advertising |= ADVERTISED_10baseT_Half;
831 if (ctrl & WMC_WANAP)
832 cmd->advertising |= ADVERTISED_Pause;
833 cmd->autoneg = AUTONEG_ENABLE;
834
835 cmd->speed = (ctrl & WMC_WSS) ? SPEED_100 : SPEED_10;
836 cmd->duplex = (ctrl & WMC_WDS) ?
837 DUPLEX_FULL : DUPLEX_HALF;
838 } else {
839 /* auto-negotiation is disabled */
840 cmd->autoneg = AUTONEG_DISABLE;
841
842 cmd->speed = (ctrl & WMC_WANF100) ?
843 SPEED_100 : SPEED_10;
844 cmd->duplex = (ctrl & WMC_WANFF) ?
845 DUPLEX_FULL : DUPLEX_HALF;
846 }
847 break;
848 case KS8695_DTYPE_LAN:
849 return -EOPNOTSUPP;
850 }
851
852 return 0;
853}
854
855/**
856 * ks8695_set_settings - Set device-specific settings.
857 * @ndev: The network device to configure
858 * @cmd: The settings to configure
859 */
860static int
861ks8695_set_settings(struct net_device *ndev, struct ethtool_cmd *cmd)
862{
863 struct ks8695_priv *ksp = netdev_priv(ndev);
864 u32 ctrl;
865
866 if ((cmd->speed != SPEED_10) && (cmd->speed != SPEED_100))
867 return -EINVAL;
868 if ((cmd->duplex != DUPLEX_HALF) && (cmd->duplex != DUPLEX_FULL))
869 return -EINVAL;
870 if (cmd->port != PORT_MII)
871 return -EINVAL;
872 if (cmd->transceiver != XCVR_INTERNAL)
873 return -EINVAL;
874 if ((cmd->autoneg != AUTONEG_DISABLE) &&
875 (cmd->autoneg != AUTONEG_ENABLE))
876 return -EINVAL;
877
878 if (cmd->autoneg == AUTONEG_ENABLE) {
879 if ((cmd->advertising & (ADVERTISED_10baseT_Half |
880 ADVERTISED_10baseT_Full |
881 ADVERTISED_100baseT_Half |
882 ADVERTISED_100baseT_Full)) == 0)
883 return -EINVAL;
884
885 switch (ksp->dtype) {
886 case KS8695_DTYPE_HPNA:
887 /* HPNA does not support auto-negotiation. */
888 return -EINVAL;
889 case KS8695_DTYPE_WAN:
890 ctrl = readl(ksp->phyiface_regs + KS8695_WMC);
891
892 ctrl &= ~(WMC_WAND | WMC_WANA100F | WMC_WANA100H |
893 WMC_WANA10F | WMC_WANA10H);
894 if (cmd->advertising & ADVERTISED_100baseT_Full)
895 ctrl |= WMC_WANA100F;
896 if (cmd->advertising & ADVERTISED_100baseT_Half)
897 ctrl |= WMC_WANA100H;
898 if (cmd->advertising & ADVERTISED_10baseT_Full)
899 ctrl |= WMC_WANA10F;
900 if (cmd->advertising & ADVERTISED_10baseT_Half)
901 ctrl |= WMC_WANA10H;
902
903 /* force a re-negotiation */
904 ctrl |= WMC_WANR;
905 writel(ctrl, ksp->phyiface_regs + KS8695_WMC);
906 break;
907 case KS8695_DTYPE_LAN:
908 return -EOPNOTSUPP;
909 }
910
911 } else {
912 switch (ksp->dtype) {
913 case KS8695_DTYPE_HPNA:
914 /* BUG: dtype_hpna implies no phy registers */
915 /*
916 ctrl = __raw_readl(KS8695_MISC_VA + KS8695_HMC);
917
918 ctrl &= ~(HMC_HSS | HMC_HDS);
919 if (cmd->speed == SPEED_100)
920 ctrl |= HMC_HSS;
921 if (cmd->duplex == DUPLEX_FULL)
922 ctrl |= HMC_HDS;
923
924 __raw_writel(ctrl, KS8695_MISC_VA + KS8695_HMC);
925 */
926 return -EOPNOTSUPP;
927 case KS8695_DTYPE_WAN:
928 ctrl = readl(ksp->phyiface_regs + KS8695_WMC);
929
930 /* disable auto-negotiation */
931 ctrl |= WMC_WAND;
932 ctrl &= ~(WMC_WANF100 | WMC_WANFF);
933
934 if (cmd->speed == SPEED_100)
935 ctrl |= WMC_WANF100;
936 if (cmd->duplex == DUPLEX_FULL)
937 ctrl |= WMC_WANFF;
938
939 writel(ctrl, ksp->phyiface_regs + KS8695_WMC);
940 break;
941 case KS8695_DTYPE_LAN:
942 return -EOPNOTSUPP;
943 }
944 }
945
946 return 0;
947}
948
949/**
950 * ks8695_nwayreset - Restart the autonegotiation on the port.
951 * @ndev: The network device to restart autoneotiation on
952 */
953static int
954ks8695_nwayreset(struct net_device *ndev)
955{
956 struct ks8695_priv *ksp = netdev_priv(ndev);
957 u32 ctrl;
958
959 switch (ksp->dtype) {
960 case KS8695_DTYPE_HPNA:
961 /* No phy means no autonegotiation on hpna */
962 return -EINVAL;
963 case KS8695_DTYPE_WAN:
964 ctrl = readl(ksp->phyiface_regs + KS8695_WMC);
965
966 if ((ctrl & WMC_WAND) == 0)
967 writel(ctrl | WMC_WANR,
968 ksp->phyiface_regs + KS8695_WMC);
969 else
970 /* auto-negotiation not enabled */
971 return -EINVAL;
972 break;
973 case KS8695_DTYPE_LAN:
974 return -EOPNOTSUPP;
975 }
976
977 return 0;
978}
979
980/**
981 * ks8695_get_link - Retrieve link status of network interface
982 * @ndev: The network interface to retrive the link status of.
983 */
984static u32
985ks8695_get_link(struct net_device *ndev)
986{
987 struct ks8695_priv *ksp = netdev_priv(ndev);
988 u32 ctrl;
989
990 switch (ksp->dtype) {
991 case KS8695_DTYPE_HPNA:
992 /* HPNA always has link */
993 return 1;
994 case KS8695_DTYPE_WAN:
995 /* WAN we can read the PHY for */
996 ctrl = readl(ksp->phyiface_regs + KS8695_WMC);
997 return ctrl & WMC_WLS;
998 case KS8695_DTYPE_LAN:
999 return -EOPNOTSUPP;
1000 }
1001 return 0;
1002}
1003
1004/**
1005 * ks8695_get_pause - Retrieve network pause/flow-control advertising
1006 * @ndev: The device to retrieve settings from
1007 * @param: The structure to fill out with the information
1008 */
1009static void
1010ks8695_get_pause(struct net_device *ndev, struct ethtool_pauseparam *param)
1011{
1012 struct ks8695_priv *ksp = netdev_priv(ndev);
1013 u32 ctrl;
1014
1015 switch (ksp->dtype) {
1016 case KS8695_DTYPE_HPNA:
1017 /* No phy link on hpna to configure */
1018 return;
1019 case KS8695_DTYPE_WAN:
1020 ctrl = readl(ksp->phyiface_regs + KS8695_WMC);
1021
1022 /* advertise Pause */
1023 param->autoneg = (ctrl & WMC_WANAP);
1024
1025 /* current Rx Flow-control */
1026 ctrl = ks8695_readreg(ksp, KS8695_DRXC);
1027 param->rx_pause = (ctrl & DRXC_RFCE);
1028
1029 /* current Tx Flow-control */
1030 ctrl = ks8695_readreg(ksp, KS8695_DTXC);
1031 param->tx_pause = (ctrl & DTXC_TFCE);
1032 break;
1033 case KS8695_DTYPE_LAN:
1034 /* The LAN's "phy" is a direct-attached switch */
1035 return;
1036 }
1037}
1038
1039/**
1040 * ks8695_set_pause - Configure pause/flow-control
1041 * @ndev: The device to configure
1042 * @param: The pause parameters to set
1043 *
1044 * TODO: Implement this
1045 */
1046static int
1047ks8695_set_pause(struct net_device *ndev, struct ethtool_pauseparam *param)
1048{
1049 return -EOPNOTSUPP;
1050}
1051
1052/**
1053 * ks8695_get_drvinfo - Retrieve driver information
1054 * @ndev: The network device to retrieve info about
1055 * @info: The info structure to fill out.
1056 */
1057static void
1058ks8695_get_drvinfo(struct net_device *ndev, struct ethtool_drvinfo *info)
1059{
1060 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1061 strlcpy(info->version, MODULEVERSION, sizeof(info->version));
Kay Sieversc2313552009-03-24 16:38:22 -07001062 strlcpy(info->bus_info, dev_name(ndev->dev.parent),
Daniel Silverstone7a3c66e2008-12-11 21:00:29 -08001063 sizeof(info->bus_info));
1064}
1065
1066static struct ethtool_ops ks8695_ethtool_ops = {
1067 .get_msglevel = ks8695_get_msglevel,
1068 .set_msglevel = ks8695_set_msglevel,
1069 .get_settings = ks8695_get_settings,
1070 .set_settings = ks8695_set_settings,
1071 .nway_reset = ks8695_nwayreset,
1072 .get_link = ks8695_get_link,
1073 .get_pauseparam = ks8695_get_pause,
1074 .set_pauseparam = ks8695_set_pause,
1075 .get_drvinfo = ks8695_get_drvinfo,
1076};
1077
1078/* Network device interface functions */
1079
1080/**
1081 * ks8695_set_mac - Update MAC in net dev and HW
1082 * @ndev: The network device to update
1083 * @addr: The new MAC address to set
1084 */
1085static int
1086ks8695_set_mac(struct net_device *ndev, void *addr)
1087{
1088 struct ks8695_priv *ksp = netdev_priv(ndev);
1089 struct sockaddr *address = addr;
1090
1091 if (!is_valid_ether_addr(address->sa_data))
1092 return -EADDRNOTAVAIL;
1093
1094 memcpy(ndev->dev_addr, address->sa_data, ndev->addr_len);
1095
1096 ks8695_update_mac(ksp);
1097
1098 dev_dbg(ksp->dev, "%s: Updated MAC address to %pM\n",
1099 ndev->name, ndev->dev_addr);
1100
1101 return 0;
1102}
1103
1104/**
1105 * ks8695_set_multicast - Set up the multicast behaviour of the interface
1106 * @ndev: The net_device to configure
1107 *
1108 * This routine, called by the net layer, configures promiscuity
1109 * and multicast reception behaviour for the interface.
1110 */
1111static void
1112ks8695_set_multicast(struct net_device *ndev)
1113{
1114 struct ks8695_priv *ksp = netdev_priv(ndev);
1115 u32 ctrl;
1116
1117 ctrl = ks8695_readreg(ksp, KS8695_DRXC);
1118
1119 if (ndev->flags & IFF_PROMISC) {
1120 /* enable promiscuous mode */
1121 ctrl |= DRXC_RA;
1122 } else if (ndev->flags & ~IFF_PROMISC) {
1123 /* disable promiscuous mode */
1124 ctrl &= ~DRXC_RA;
1125 }
1126
1127 if (ndev->flags & IFF_ALLMULTI) {
1128 /* enable all multicast mode */
1129 ctrl |= DRXC_RM;
1130 } else if (ndev->mc_count > KS8695_NR_ADDRESSES) {
1131 /* more specific multicast addresses than can be
1132 * handled in hardware
1133 */
1134 ctrl |= DRXC_RM;
1135 } else {
1136 /* enable specific multicasts */
1137 ctrl &= ~DRXC_RM;
1138 ks8695_init_partial_multicast(ksp, ndev->mc_list,
1139 ndev->mc_count);
1140 }
1141
1142 ks8695_writereg(ksp, KS8695_DRXC, ctrl);
1143}
1144
1145/**
1146 * ks8695_timeout - Handle a network tx/rx timeout.
1147 * @ndev: The net_device which timed out.
1148 *
1149 * A network transaction timed out, reset the device.
1150 */
1151static void
1152ks8695_timeout(struct net_device *ndev)
1153{
1154 struct ks8695_priv *ksp = netdev_priv(ndev);
1155
1156 netif_stop_queue(ndev);
1157 ks8695_shutdown(ksp);
1158
1159 ks8695_reset(ksp);
1160
1161 ks8695_update_mac(ksp);
1162
1163 /* We ignore the return from this since it managed to init
1164 * before it probably will be okay to init again.
1165 */
1166 ks8695_init_net(ksp);
1167
1168 /* Reconfigure promiscuity etc */
1169 ks8695_set_multicast(ndev);
1170
1171 /* And start the TX queue once more */
1172 netif_start_queue(ndev);
1173}
1174
1175/**
1176 * ks8695_start_xmit - Start a packet transmission
1177 * @skb: The packet to transmit
1178 * @ndev: The network device to send the packet on
1179 *
1180 * This routine, called by the net layer, takes ownership of the
1181 * sk_buff and adds it to the TX ring. It then kicks the TX DMA
1182 * engine to ensure transmission begins.
1183 */
1184static int
1185ks8695_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1186{
1187 struct ks8695_priv *ksp = netdev_priv(ndev);
1188 int buff_n;
1189 dma_addr_t dmap;
1190
1191 spin_lock_irq(&ksp->txq_lock);
1192
1193 if (ksp->tx_ring_used == MAX_TX_DESC) {
1194 /* Somehow we got entered when we have no room */
1195 spin_unlock_irq(&ksp->txq_lock);
1196 return NETDEV_TX_BUSY;
1197 }
1198
1199 buff_n = ksp->tx_ring_next_slot;
1200
1201 BUG_ON(ksp->tx_buffers[buff_n].skb);
1202
1203 dmap = dma_map_single(ksp->dev, skb->data, skb->len, DMA_TO_DEVICE);
1204 if (unlikely(dma_mapping_error(ksp->dev, dmap))) {
1205 /* Failed to DMA map this SKB, give it back for now */
1206 spin_unlock_irq(&ksp->txq_lock);
1207 dev_dbg(ksp->dev, "%s: Could not map DMA memory for "\
1208 "transmission, trying later\n", ndev->name);
1209 return NETDEV_TX_BUSY;
1210 }
1211
1212 ksp->tx_buffers[buff_n].dma_ptr = dmap;
1213 /* Mapped okay, store the buffer pointer and length for later */
1214 ksp->tx_buffers[buff_n].skb = skb;
1215 ksp->tx_buffers[buff_n].length = skb->len;
1216
1217 /* Fill out the TX descriptor */
1218 ksp->tx_ring[buff_n].data_ptr =
1219 cpu_to_le32(ksp->tx_buffers[buff_n].dma_ptr);
1220 ksp->tx_ring[buff_n].status =
1221 cpu_to_le32(TDES_IC | TDES_FS | TDES_LS |
1222 (skb->len & TDES_TBS));
1223
1224 wmb();
1225
1226 /* Hand it over to the hardware */
1227 ksp->tx_ring[buff_n].owner = cpu_to_le32(TDES_OWN);
1228
1229 if (++ksp->tx_ring_used == MAX_TX_DESC)
1230 netif_stop_queue(ndev);
1231
1232 ndev->trans_start = jiffies;
1233
1234 /* Kick the TX DMA in case it decided to go IDLE */
1235 ks8695_writereg(ksp, KS8695_DTSC, 0);
1236
1237 /* And update the next ring slot */
1238 ksp->tx_ring_next_slot = (buff_n + 1) & MAX_TX_DESC_MASK;
1239
1240 spin_unlock_irq(&ksp->txq_lock);
1241 return NETDEV_TX_OK;
1242}
1243
1244/**
1245 * ks8695_stop - Stop (shutdown) a KS8695 ethernet interface
1246 * @ndev: The net_device to stop
1247 *
1248 * This disables the TX queue and cleans up a KS8695 ethernet
1249 * device.
1250 */
1251static int
1252ks8695_stop(struct net_device *ndev)
1253{
1254 struct ks8695_priv *ksp = netdev_priv(ndev);
1255
1256 netif_stop_queue(ndev);
1257 netif_carrier_off(ndev);
1258
1259 ks8695_shutdown(ksp);
1260
1261 return 0;
1262}
1263
1264/**
1265 * ks8695_open - Open (bring up) a KS8695 ethernet interface
1266 * @ndev: The net_device to open
1267 *
1268 * This resets, configures the MAC, initialises the RX ring and
1269 * DMA engines and starts the TX queue for a KS8695 ethernet
1270 * device.
1271 */
1272static int
1273ks8695_open(struct net_device *ndev)
1274{
1275 struct ks8695_priv *ksp = netdev_priv(ndev);
1276 int ret;
1277
1278 if (!is_valid_ether_addr(ndev->dev_addr))
1279 return -EADDRNOTAVAIL;
1280
1281 ks8695_reset(ksp);
1282
1283 ks8695_update_mac(ksp);
1284
1285 ret = ks8695_init_net(ksp);
1286 if (ret) {
1287 ks8695_shutdown(ksp);
1288 return ret;
1289 }
1290
1291 netif_start_queue(ndev);
1292
1293 return 0;
1294}
1295
1296/* Platform device driver */
1297
1298/**
1299 * ks8695_init_switch - Init LAN switch to known good defaults.
1300 * @ksp: The device to initialise
1301 *
1302 * This initialises the LAN switch in the KS8695 to a known-good
1303 * set of defaults.
1304 */
1305static void __devinit
1306ks8695_init_switch(struct ks8695_priv *ksp)
1307{
1308 u32 ctrl;
1309
1310 /* Default value for SEC0 according to datasheet */
1311 ctrl = 0x40819e00;
1312
1313 /* LED0 = Speed LED1 = Link/Activity */
1314 ctrl &= ~(SEC0_LLED1S | SEC0_LLED0S);
1315 ctrl |= (LLED0S_LINK | LLED1S_LINK_ACTIVITY);
1316
1317 /* Enable Switch */
1318 ctrl |= SEC0_ENABLE;
1319
1320 writel(ctrl, ksp->phyiface_regs + KS8695_SEC0);
1321
1322 /* Defaults for SEC1 */
1323 writel(0x9400100, ksp->phyiface_regs + KS8695_SEC1);
1324}
1325
1326/**
1327 * ks8695_init_wan_phy - Initialise the WAN PHY to sensible defaults
1328 * @ksp: The device to initialise
1329 *
1330 * This initialises a KS8695's WAN phy to sensible values for
1331 * autonegotiation etc.
1332 */
1333static void __devinit
1334ks8695_init_wan_phy(struct ks8695_priv *ksp)
1335{
1336 u32 ctrl;
1337
1338 /* Support auto-negotiation */
1339 ctrl = (WMC_WANAP | WMC_WANA100F | WMC_WANA100H |
1340 WMC_WANA10F | WMC_WANA10H);
1341
1342 /* LED0 = Activity , LED1 = Link */
1343 ctrl |= (WLED0S_ACTIVITY | WLED1S_LINK);
1344
1345 /* Restart Auto-negotiation */
1346 ctrl |= WMC_WANR;
1347
1348 writel(ctrl, ksp->phyiface_regs + KS8695_WMC);
1349
1350 writel(0, ksp->phyiface_regs + KS8695_WPPM);
1351 writel(0, ksp->phyiface_regs + KS8695_PPS);
1352}
1353
1354static const struct net_device_ops ks8695_netdev_ops = {
1355 .ndo_open = ks8695_open,
1356 .ndo_stop = ks8695_stop,
1357 .ndo_start_xmit = ks8695_start_xmit,
1358 .ndo_tx_timeout = ks8695_timeout,
1359 .ndo_set_mac_address = ks8695_set_mac,
Stephen Hemminger52255bb2009-01-09 10:45:37 +00001360 .ndo_validate_addr = eth_validate_addr,
Daniel Silverstone7a3c66e2008-12-11 21:00:29 -08001361 .ndo_set_multicast_list = ks8695_set_multicast,
1362};
1363
1364/**
1365 * ks8695_probe - Probe and initialise a KS8695 ethernet interface
1366 * @pdev: The platform device to probe
1367 *
1368 * Initialise a KS8695 ethernet device from platform data.
1369 *
1370 * This driver requires at least one IORESOURCE_MEM for the
1371 * registers and two IORESOURCE_IRQ for the RX and TX IRQs
1372 * respectively. It can optionally take an additional
1373 * IORESOURCE_MEM for the switch or phy in the case of the lan or
1374 * wan ports, and an IORESOURCE_IRQ for the link IRQ for the wan
1375 * port.
1376 */
1377static int __devinit
1378ks8695_probe(struct platform_device *pdev)
1379{
1380 struct ks8695_priv *ksp;
1381 struct net_device *ndev;
1382 struct resource *regs_res, *phyiface_res;
1383 struct resource *rxirq_res, *txirq_res, *linkirq_res;
1384 int ret = 0;
1385 int buff_n;
1386 u32 machigh, maclow;
1387
1388 /* Initialise a net_device */
1389 ndev = alloc_etherdev(sizeof(struct ks8695_priv));
1390 if (!ndev) {
1391 dev_err(&pdev->dev, "could not allocate device.\n");
1392 return -ENOMEM;
1393 }
1394
1395 SET_NETDEV_DEV(ndev, &pdev->dev);
1396
1397 dev_dbg(&pdev->dev, "ks8695_probe() called\n");
1398
1399 /* Configure our private structure a little */
1400 ksp = netdev_priv(ndev);
1401 memset(ksp, 0, sizeof(struct ks8695_priv));
1402
1403 ksp->dev = &pdev->dev;
1404 ksp->ndev = ndev;
1405 ksp->msg_enable = NETIF_MSG_LINK;
1406
1407 /* Retrieve resources */
1408 regs_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1409 phyiface_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1410
1411 rxirq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1412 txirq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
1413 linkirq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 2);
1414
1415 if (!(regs_res && rxirq_res && txirq_res)) {
1416 dev_err(ksp->dev, "insufficient resources\n");
1417 ret = -ENOENT;
1418 goto failure;
1419 }
1420
1421 ksp->regs_req = request_mem_region(regs_res->start,
1422 resource_size(regs_res),
1423 pdev->name);
1424
1425 if (!ksp->regs_req) {
1426 dev_err(ksp->dev, "cannot claim register space\n");
1427 ret = -EIO;
1428 goto failure;
1429 }
1430
1431 ksp->io_regs = ioremap(regs_res->start, resource_size(regs_res));
1432
1433 if (!ksp->io_regs) {
1434 dev_err(ksp->dev, "failed to ioremap registers\n");
1435 ret = -EINVAL;
1436 goto failure;
1437 }
1438
1439 if (phyiface_res) {
1440 ksp->phyiface_req =
1441 request_mem_region(phyiface_res->start,
1442 resource_size(phyiface_res),
1443 phyiface_res->name);
1444
1445 if (!ksp->phyiface_req) {
1446 dev_err(ksp->dev,
1447 "cannot claim switch register space\n");
1448 ret = -EIO;
1449 goto failure;
1450 }
1451
1452 ksp->phyiface_regs = ioremap(phyiface_res->start,
1453 resource_size(phyiface_res));
1454
1455 if (!ksp->phyiface_regs) {
1456 dev_err(ksp->dev,
1457 "failed to ioremap switch registers\n");
1458 ret = -EINVAL;
1459 goto failure;
1460 }
1461 }
1462
1463 ksp->rx_irq = rxirq_res->start;
1464 ksp->rx_irq_name = rxirq_res->name ? rxirq_res->name : "Ethernet RX";
1465 ksp->tx_irq = txirq_res->start;
1466 ksp->tx_irq_name = txirq_res->name ? txirq_res->name : "Ethernet TX";
1467 ksp->link_irq = (linkirq_res ? linkirq_res->start : -1);
1468 ksp->link_irq_name = (linkirq_res && linkirq_res->name) ?
1469 linkirq_res->name : "Ethernet Link";
1470
1471 /* driver system setup */
1472 ndev->netdev_ops = &ks8695_netdev_ops;
1473 SET_ETHTOOL_OPS(ndev, &ks8695_ethtool_ops);
1474 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
1475
1476 /* Retrieve the default MAC addr from the chip. */
1477 /* The bootloader should have left it in there for us. */
1478
1479 machigh = ks8695_readreg(ksp, KS8695_MAH);
1480 maclow = ks8695_readreg(ksp, KS8695_MAL);
1481
1482 ndev->dev_addr[0] = (machigh >> 8) & 0xFF;
1483 ndev->dev_addr[1] = machigh & 0xFF;
1484 ndev->dev_addr[2] = (maclow >> 24) & 0xFF;
1485 ndev->dev_addr[3] = (maclow >> 16) & 0xFF;
1486 ndev->dev_addr[4] = (maclow >> 8) & 0xFF;
1487 ndev->dev_addr[5] = maclow & 0xFF;
1488
1489 if (!is_valid_ether_addr(ndev->dev_addr))
1490 dev_warn(ksp->dev, "%s: Invalid ethernet MAC address. Please "
1491 "set using ifconfig\n", ndev->name);
1492
1493 /* In order to be efficient memory-wise, we allocate both
1494 * rings in one go.
1495 */
1496 ksp->ring_base = dma_alloc_coherent(&pdev->dev, RING_DMA_SIZE,
1497 &ksp->ring_base_dma, GFP_KERNEL);
1498 if (!ksp->ring_base) {
1499 ret = -ENOMEM;
1500 goto failure;
1501 }
1502
1503 /* Specify the TX DMA ring buffer */
1504 ksp->tx_ring = ksp->ring_base;
1505 ksp->tx_ring_dma = ksp->ring_base_dma;
1506
1507 /* And initialise the queue's lock */
1508 spin_lock_init(&ksp->txq_lock);
1509
1510 /* Specify the RX DMA ring buffer */
1511 ksp->rx_ring = ksp->ring_base + TX_RING_DMA_SIZE;
1512 ksp->rx_ring_dma = ksp->ring_base_dma + TX_RING_DMA_SIZE;
1513
1514 /* Zero the descriptor rings */
1515 memset(ksp->tx_ring, 0, TX_RING_DMA_SIZE);
1516 memset(ksp->rx_ring, 0, RX_RING_DMA_SIZE);
1517
1518 /* Build the rings */
1519 for (buff_n = 0; buff_n < MAX_TX_DESC; ++buff_n) {
1520 ksp->tx_ring[buff_n].next_desc =
1521 cpu_to_le32(ksp->tx_ring_dma +
1522 (sizeof(struct tx_ring_desc) *
1523 ((buff_n + 1) & MAX_TX_DESC_MASK)));
1524 }
1525
1526 for (buff_n = 0; buff_n < MAX_RX_DESC; ++buff_n) {
1527 ksp->rx_ring[buff_n].next_desc =
1528 cpu_to_le32(ksp->rx_ring_dma +
1529 (sizeof(struct rx_ring_desc) *
1530 ((buff_n + 1) & MAX_RX_DESC_MASK)));
1531 }
1532
1533 /* Initialise the port (physically) */
1534 if (ksp->phyiface_regs && ksp->link_irq == -1) {
1535 ks8695_init_switch(ksp);
1536 ksp->dtype = KS8695_DTYPE_LAN;
1537 } else if (ksp->phyiface_regs && ksp->link_irq != -1) {
1538 ks8695_init_wan_phy(ksp);
1539 ksp->dtype = KS8695_DTYPE_WAN;
1540 } else {
1541 /* No initialisation since HPNA does not have a PHY */
1542 ksp->dtype = KS8695_DTYPE_HPNA;
1543 }
1544
1545 /* And bring up the net_device with the net core */
1546 platform_set_drvdata(pdev, ndev);
1547 ret = register_netdev(ndev);
1548
1549 if (ret == 0) {
1550 dev_info(ksp->dev, "ks8695 ethernet (%s) MAC: %pM\n",
1551 ks8695_port_type(ksp), ndev->dev_addr);
1552 } else {
1553 /* Report the failure to register the net_device */
1554 dev_err(ksp->dev, "ks8695net: failed to register netdev.\n");
1555 goto failure;
1556 }
1557
1558 /* All is well */
1559 return 0;
1560
1561 /* Error exit path */
1562failure:
1563 ks8695_release_device(ksp);
1564 free_netdev(ndev);
1565
1566 return ret;
1567}
1568
1569/**
1570 * ks8695_drv_suspend - Suspend a KS8695 ethernet platform device.
1571 * @pdev: The device to suspend
1572 * @state: The suspend state
1573 *
1574 * This routine detaches and shuts down a KS8695 ethernet device.
1575 */
1576static int
1577ks8695_drv_suspend(struct platform_device *pdev, pm_message_t state)
1578{
1579 struct net_device *ndev = platform_get_drvdata(pdev);
1580 struct ks8695_priv *ksp = netdev_priv(ndev);
1581
1582 ksp->in_suspend = 1;
1583
1584 if (netif_running(ndev)) {
1585 netif_device_detach(ndev);
1586 ks8695_shutdown(ksp);
1587 }
1588
1589 return 0;
1590}
1591
1592/**
1593 * ks8695_drv_resume - Resume a KS8695 ethernet platform device.
1594 * @pdev: The device to resume
1595 *
1596 * This routine re-initialises and re-attaches a KS8695 ethernet
1597 * device.
1598 */
1599static int
1600ks8695_drv_resume(struct platform_device *pdev)
1601{
1602 struct net_device *ndev = platform_get_drvdata(pdev);
1603 struct ks8695_priv *ksp = netdev_priv(ndev);
1604
1605 if (netif_running(ndev)) {
1606 ks8695_reset(ksp);
1607 ks8695_init_net(ksp);
1608 ks8695_set_multicast(ndev);
1609 netif_device_attach(ndev);
1610 }
1611
1612 ksp->in_suspend = 0;
1613
1614 return 0;
1615}
1616
1617/**
1618 * ks8695_drv_remove - Remove a KS8695 net device on driver unload.
1619 * @pdev: The platform device to remove
1620 *
1621 * This unregisters and releases a KS8695 ethernet device.
1622 */
1623static int __devexit
1624ks8695_drv_remove(struct platform_device *pdev)
1625{
1626 struct net_device *ndev = platform_get_drvdata(pdev);
1627 struct ks8695_priv *ksp = netdev_priv(ndev);
1628
1629 platform_set_drvdata(pdev, NULL);
1630
1631 unregister_netdev(ndev);
1632 ks8695_release_device(ksp);
1633 free_netdev(ndev);
1634
1635 dev_dbg(&pdev->dev, "released and freed device\n");
1636 return 0;
1637}
1638
1639static struct platform_driver ks8695_driver = {
1640 .driver = {
1641 .name = MODULENAME,
1642 .owner = THIS_MODULE,
1643 },
1644 .probe = ks8695_probe,
1645 .remove = __devexit_p(ks8695_drv_remove),
1646 .suspend = ks8695_drv_suspend,
1647 .resume = ks8695_drv_resume,
1648};
1649
1650/* Module interface */
1651
1652static int __init
1653ks8695_init(void)
1654{
1655 printk(KERN_INFO "%s Ethernet driver, V%s\n",
1656 MODULENAME, MODULEVERSION);
1657
1658 return platform_driver_register(&ks8695_driver);
1659}
1660
1661static void __exit
1662ks8695_cleanup(void)
1663{
1664 platform_driver_unregister(&ks8695_driver);
1665}
1666
1667module_init(ks8695_init);
1668module_exit(ks8695_cleanup);
1669
1670MODULE_AUTHOR("Simtec Electronics")
1671MODULE_DESCRIPTION("Micrel KS8695 (Centaur) Ethernet driver");
1672MODULE_LICENSE("GPL");
1673MODULE_ALIAS("platform:" MODULENAME);
1674
1675module_param(watchdog, int, 0400);
1676MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");