added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / drivers / net / arm / ks8695net.c
blobf3a127434897472110abaa8bd5e26b383e9a9767
1 /*
2 * Micrel KS8695 (Centaur) Ethernet.
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.
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.
14 * Copyright 2008 Simtec Electronics
15 * Daniel Silverstone <dsilvers@simtec.co.uk>
16 * Vincent Sanders <vince@simtec.co.uk>
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>
32 #include <linux/io.h>
34 #include <asm/irq.h>
36 #include <mach/regs-switch.h>
37 #include <mach/regs-misc.h>
39 #include "ks8695net.h"
41 #define MODULENAME "ks8695_ether"
42 #define MODULEVERSION "1.01"
45 * Transmit and device reset timeout, default 5 seconds.
47 static int watchdog = 5000;
49 /* Hardware structures */
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.
58 struct rx_ring_desc {
59 __le32 status;
60 __le32 length;
61 __le32 data_ptr;
62 __le32 next_desc;
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.
72 struct tx_ring_desc {
73 __le32 owner;
74 __le32 status;
75 __le32 data_ptr;
76 __le32 next_desc;
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
85 struct ks8695_skbuff {
86 struct sk_buff *skb;
87 dma_addr_t dma_ptr;
88 u32 length;
91 /* Private device structure */
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
98 #define MAX_RXBUF_SIZE 0x700
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)
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
110 enum ks8695_dtype {
111 KS8695_DTYPE_WAN,
112 KS8695_DTYPE_LAN,
113 KS8695_DTYPE_HPNA,
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
148 struct 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;
155 const char *rx_irq_name, *tx_irq_name, *link_irq_name;
156 int rx_irq, tx_irq, link_irq;
158 struct resource *regs_req, *phyiface_req;
159 void __iomem *phyiface_regs;
161 void *ring_base;
162 dma_addr_t ring_base_dma;
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;
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;
176 int msg_enable;
179 /* Register access */
182 * ks8695_readreg - Read from a KS8695 ethernet register
183 * @ksp: The device to read from
184 * @reg: The register to read
186 static inline u32
187 ks8695_readreg(struct ks8695_priv *ksp, int reg)
189 return readl(ksp->io_regs + reg);
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
198 static inline void
199 ks8695_writereg(struct ks8695_priv *ksp, int reg, u32 value)
201 writel(value, ksp->io_regs + reg);
204 /* Utility functions */
207 * ks8695_port_type - Retrieve port-type as user-friendly string
208 * @ksp: The device to return the type for
210 * Returns a string indicating which of the WAN, LAN or HPNA
211 * ports this device is likely to represent.
213 static const char *
214 ks8695_port_type(struct ks8695_priv *ksp)
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";
225 return "UNKNOWN";
229 * ks8695_update_mac - Update the MAC registers in the device
230 * @ksp: The device to update
232 * Updates the MAC registers in the KS8695 device from the address in the
233 * net_device structure associated with this interface.
235 static void
236 ks8695_update_mac(struct ks8695_priv *ksp)
238 /* Update the HW with the MAC from the net_device */
239 struct net_device *ndev = ksp->ndev;
240 u32 machigh, maclow;
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));
246 ks8695_writereg(ksp, KS8695_MAL, maclow);
247 ks8695_writereg(ksp, KS8695_MAH, machigh);
252 * ks8695_refill_rxbuffers - Re-fill the RX buffer ring
253 * @ksp: The device to refill
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.
260 static void
261 ks8695_refill_rxbuffers(struct ks8695_priv *ksp)
263 /* Run around the RX ring, filling in any missing sk_buff's */
264 int buff_n;
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;
271 ksp->rx_buffers[buff_n].skb = skb;
272 if (skb == NULL) {
273 /* Failed to allocate one, perhaps
274 * we'll try again later.
276 break;
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;
288 ksp->rx_buffers[buff_n].dma_ptr = mapping;
289 skb->dev = ksp->ndev;
290 ksp->rx_buffers[buff_n].length = MAX_RXBUF_SIZE;
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);
297 wmb();
299 /* And give ownership over to the hardware */
300 ksp->rx_ring[buff_n].status = cpu_to_le32(RDES_OWN);
305 /* Maximum number of multicast addresses which the KS8695 HW supports */
306 #define KS8695_NR_ADDRESSES 16
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
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.
318 static void
319 ks8695_init_partial_multicast(struct ks8695_priv *ksp,
320 struct dev_mc_list *addr,
321 int nr_addr)
323 u32 low, high;
324 int i;
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);
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]);
337 ks8695_writereg(ksp, KS8695_AAL_(i), low);
338 ks8695_writereg(ksp, KS8695_AAH_(i), AAH_E | high);
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);
348 /* Interrupt handling */
351 * ks8695_tx_irq - Transmit IRQ handler
352 * @irq: The IRQ which went off (ignored)
353 * @dev_id: The net_device for the interrupt
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.
359 static irqreturn_t
360 ks8695_tx_irq(int irq, void *dev_id)
362 struct net_device *ndev = (struct net_device *)dev_id;
363 struct ks8695_priv *ksp = netdev_priv(ndev);
364 int buff_n;
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;
375 /* Free the packet from the ring */
376 ksp->tx_ring[buff_n].data_ptr = 0;
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--;
389 netif_wake_queue(ndev);
391 return IRQ_HANDLED;
395 * ks8695_rx_irq - Receive IRQ handler
396 * @irq: The IRQ which went off (ignored)
397 * @dev_id: The net_device for the interrupt
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.
403 static irqreturn_t
404 ks8695_rx_irq(int irq, void *dev_id)
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;
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
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
430 goto rx_failure;
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++;
445 goto rx_failure;
448 pktlen = flags & RDES_FLEN;
449 pktlen -= 4; /* Drop the CRC */
451 /* Retrieve the sk_buff */
452 skb = ksp->rx_buffers[buff_n].skb;
454 /* Clear it from the ring */
455 ksp->rx_buffers[buff_n].skb = NULL;
456 ksp->rx_ring[buff_n].data_ptr = 0;
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);
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);
469 /* Record stats */
470 ndev->last_rx = jiffies;
471 ndev->stats.rx_packets++;
472 ndev->stats.rx_bytes += pktlen;
473 goto rx_finished;
475 rx_failure:
476 /* This ring entry is an error, but we can
477 * re-use the skb
479 /* Give the ring entry back to the hardware */
480 ksp->rx_ring[buff_n].status = cpu_to_le32(RDES_OWN);
481 rx_finished:
482 /* And note this as processed so we can start
483 * from here next time
485 last_rx_processed = buff_n;
486 } else {
487 /* Ran out of things to process, stop now */
488 break;
490 buff_n = (buff_n + 1) & MAX_RX_DESC_MASK;
491 } while (buff_n != ksp->next_rx_desc_read);
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;
498 /* And refill the buffers */
499 ks8695_refill_rxbuffers(ksp);
501 /* Kick the RX DMA engine, in case it became suspended */
502 ks8695_writereg(ksp, KS8695_DRSC, 0);
504 return IRQ_HANDLED;
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
512 * The WAN interface can generate an IRQ when the link changes,
513 * report this to the net layer and the user.
515 static irqreturn_t
516 ks8695_link_irq(int irq, void *dev_id)
518 struct net_device *ndev = (struct net_device *)dev_id;
519 struct ks8695_priv *ksp = netdev_priv(ndev);
520 u32 ctrl;
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);
538 return IRQ_HANDLED;
542 /* KS8695 Device functions */
545 * ks8695_reset - Reset a KS8695 ethernet interface
546 * @ksp: The interface to reset
548 * Perform an engine reset of the interface and re-program it
549 * with sensible defaults.
551 static void
552 ks8695_reset(struct ks8695_priv *ksp)
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);
563 if (reset_timeout < 0) {
564 dev_crit(ksp->dev,
565 "Timeout waiting for DMA engines to reset\n");
566 /* And blithely carry on */
569 /* Definitely wait long enough before attempting to program
570 * the engines
572 msleep(10);
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);
581 * ks8695_shutdown - Shut down a KS8695 ethernet interface
582 * @ksp: The interface to shut down
584 * This disables packet RX/TX, cleans up IRQs, drains the rings,
585 * and basically places the interface into a clean shutdown
586 * state.
588 static void
589 ks8695_shutdown(struct ks8695_priv *ksp)
591 u32 ctrl;
592 int buff_n;
594 /* Disable packet transmission */
595 ctrl = ks8695_readreg(ksp, KS8695_DTXC);
596 ks8695_writereg(ksp, KS8695_DTXC, ctrl & ~DTXC_TE);
598 /* Disable packet reception */
599 ctrl = ks8695_readreg(ksp, KS8695_DRXC);
600 ks8695_writereg(ksp, KS8695_DRXC, ctrl & ~DRXC_RE);
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);
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;
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;
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;
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;
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
652 * Return 0 on success.
654 static int
655 ks8695_setup_irq(int irq, const char *irq_name,
656 irq_handler_t handler, struct net_device *ndev)
658 int ret;
660 ret = request_irq(irq, handler, IRQF_SHARED, irq_name, ndev);
662 if (ret) {
663 dev_err(&ndev->dev, "failure to request IRQ %d\n", irq);
664 return ret;
667 return 0;
671 * ks8695_init_net - Initialise a KS8695 ethernet interface
672 * @ksp: The interface to initialise
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.
678 static int
679 ks8695_init_net(struct ks8695_priv *ksp)
681 int ret;
682 u32 ctrl;
684 ks8695_refill_rxbuffers(ksp);
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);
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;
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;
711 /* Bring up transmission */
712 ctrl = ks8695_readreg(ksp, KS8695_DTXC);
713 /* Enable packet transmission */
714 ks8695_writereg(ksp, KS8695_DTXC, ctrl | DTXC_TE);
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);
723 /* All done */
724 return 0;
728 * ks8695_release_device - HW resource release for KS8695 e-net
729 * @ksp: The device to be freed
731 * This unallocates io memory regions, dma-coherent regions etc
732 * which were allocated in ks8695_probe.
734 static void
735 ks8695_release_device(struct ks8695_priv *ksp)
737 /* Unmap the registers */
738 iounmap(ksp->io_regs);
739 if (ksp->phyiface_regs)
740 iounmap(ksp->phyiface_regs);
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);
750 /* Free the ring buffers */
751 dma_free_coherent(ksp->dev, RING_DMA_SIZE,
752 ksp->ring_base, ksp->ring_base_dma);
755 /* Ethtool support */
758 * ks8695_get_msglevel - Get the messages enabled for emission
759 * @ndev: The network device to read from
761 static u32
762 ks8695_get_msglevel(struct net_device *ndev)
764 struct ks8695_priv *ksp = netdev_priv(ndev);
766 return ksp->msg_enable;
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
774 static void
775 ks8695_set_msglevel(struct net_device *ndev, u32 value)
777 struct ks8695_priv *ksp = netdev_priv(ndev);
779 ksp->msg_enable = value;
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
787 static int
788 ks8695_get_settings(struct net_device *ndev, struct ethtool_cmd *cmd)
790 struct ks8695_priv *ksp = netdev_priv(ndev);
791 u32 ctrl;
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;
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;
806 /* BUG: Erm, dtype hpna implies no phy regs */
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;
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;
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;
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;
842 cmd->speed = (ctrl & WMC_WANF100) ?
843 SPEED_100 : SPEED_10;
844 cmd->duplex = (ctrl & WMC_WANFF) ?
845 DUPLEX_FULL : DUPLEX_HALF;
847 break;
848 case KS8695_DTYPE_LAN:
849 return -EOPNOTSUPP;
852 return 0;
856 * ks8695_set_settings - Set device-specific settings.
857 * @ndev: The network device to configure
858 * @cmd: The settings to configure
860 static int
861 ks8695_set_settings(struct net_device *ndev, struct ethtool_cmd *cmd)
863 struct ks8695_priv *ksp = netdev_priv(ndev);
864 u32 ctrl;
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;
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;
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);
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;
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;
911 } else {
912 switch (ksp->dtype) {
913 case KS8695_DTYPE_HPNA:
914 /* BUG: dtype_hpna implies no phy registers */
916 ctrl = __raw_readl(KS8695_MISC_VA + KS8695_HMC);
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;
924 __raw_writel(ctrl, KS8695_MISC_VA + KS8695_HMC);
926 return -EOPNOTSUPP;
927 case KS8695_DTYPE_WAN:
928 ctrl = readl(ksp->phyiface_regs + KS8695_WMC);
930 /* disable auto-negotiation */
931 ctrl |= WMC_WAND;
932 ctrl &= ~(WMC_WANF100 | WMC_WANFF);
934 if (cmd->speed == SPEED_100)
935 ctrl |= WMC_WANF100;
936 if (cmd->duplex == DUPLEX_FULL)
937 ctrl |= WMC_WANFF;
939 writel(ctrl, ksp->phyiface_regs + KS8695_WMC);
940 break;
941 case KS8695_DTYPE_LAN:
942 return -EOPNOTSUPP;
946 return 0;
950 * ks8695_nwayreset - Restart the autonegotiation on the port.
951 * @ndev: The network device to restart autoneotiation on
953 static int
954 ks8695_nwayreset(struct net_device *ndev)
956 struct ks8695_priv *ksp = netdev_priv(ndev);
957 u32 ctrl;
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);
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;
977 return 0;
981 * ks8695_get_link - Retrieve link status of network interface
982 * @ndev: The network interface to retrive the link status of.
984 static u32
985 ks8695_get_link(struct net_device *ndev)
987 struct ks8695_priv *ksp = netdev_priv(ndev);
988 u32 ctrl;
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;
1001 return 0;
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
1009 static void
1010 ks8695_get_pause(struct net_device *ndev, struct ethtool_pauseparam *param)
1012 struct ks8695_priv *ksp = netdev_priv(ndev);
1013 u32 ctrl;
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);
1022 /* advertise Pause */
1023 param->autoneg = (ctrl & WMC_WANAP);
1025 /* current Rx Flow-control */
1026 ctrl = ks8695_readreg(ksp, KS8695_DRXC);
1027 param->rx_pause = (ctrl & DRXC_RFCE);
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;
1040 * ks8695_set_pause - Configure pause/flow-control
1041 * @ndev: The device to configure
1042 * @param: The pause parameters to set
1044 * TODO: Implement this
1046 static int
1047 ks8695_set_pause(struct net_device *ndev, struct ethtool_pauseparam *param)
1049 return -EOPNOTSUPP;
1053 * ks8695_get_drvinfo - Retrieve driver information
1054 * @ndev: The network device to retrieve info about
1055 * @info: The info structure to fill out.
1057 static void
1058 ks8695_get_drvinfo(struct net_device *ndev, struct ethtool_drvinfo *info)
1060 strlcpy(info->driver, MODULENAME, sizeof(info->driver));
1061 strlcpy(info->version, MODULEVERSION, sizeof(info->version));
1062 strlcpy(info->bus_info, ndev->dev.parent->bus_id,
1063 sizeof(info->bus_info));
1066 static 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,
1078 /* Network device interface functions */
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
1085 static int
1086 ks8695_set_mac(struct net_device *ndev, void *addr)
1088 struct ks8695_priv *ksp = netdev_priv(ndev);
1089 struct sockaddr *address = addr;
1091 if (!is_valid_ether_addr(address->sa_data))
1092 return -EADDRNOTAVAIL;
1094 memcpy(ndev->dev_addr, address->sa_data, ndev->addr_len);
1096 ks8695_update_mac(ksp);
1098 dev_dbg(ksp->dev, "%s: Updated MAC address to %pM\n",
1099 ndev->name, ndev->dev_addr);
1101 return 0;
1105 * ks8695_set_multicast - Set up the multicast behaviour of the interface
1106 * @ndev: The net_device to configure
1108 * This routine, called by the net layer, configures promiscuity
1109 * and multicast reception behaviour for the interface.
1111 static void
1112 ks8695_set_multicast(struct net_device *ndev)
1114 struct ks8695_priv *ksp = netdev_priv(ndev);
1115 u32 ctrl;
1117 ctrl = ks8695_readreg(ksp, KS8695_DRXC);
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;
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
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);
1142 ks8695_writereg(ksp, KS8695_DRXC, ctrl);
1146 * ks8695_timeout - Handle a network tx/rx timeout.
1147 * @ndev: The net_device which timed out.
1149 * A network transaction timed out, reset the device.
1151 static void
1152 ks8695_timeout(struct net_device *ndev)
1154 struct ks8695_priv *ksp = netdev_priv(ndev);
1156 netif_stop_queue(ndev);
1157 ks8695_shutdown(ksp);
1159 ks8695_reset(ksp);
1161 ks8695_update_mac(ksp);
1163 /* We ignore the return from this since it managed to init
1164 * before it probably will be okay to init again.
1166 ks8695_init_net(ksp);
1168 /* Reconfigure promiscuity etc */
1169 ks8695_set_multicast(ndev);
1171 /* And start the TX queue once more */
1172 netif_start_queue(ndev);
1176 * ks8695_start_xmit - Start a packet transmission
1177 * @skb: The packet to transmit
1178 * @ndev: The network device to send the packet on
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.
1184 static int
1185 ks8695_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1187 struct ks8695_priv *ksp = netdev_priv(ndev);
1188 int buff_n;
1189 dma_addr_t dmap;
1191 spin_lock_irq(&ksp->txq_lock);
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;
1199 buff_n = ksp->tx_ring_next_slot;
1201 BUG_ON(ksp->tx_buffers[buff_n].skb);
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;
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;
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));
1224 wmb();
1226 /* Hand it over to the hardware */
1227 ksp->tx_ring[buff_n].owner = cpu_to_le32(TDES_OWN);
1229 if (++ksp->tx_ring_used == MAX_TX_DESC)
1230 netif_stop_queue(ndev);
1232 ndev->trans_start = jiffies;
1234 /* Kick the TX DMA in case it decided to go IDLE */
1235 ks8695_writereg(ksp, KS8695_DTSC, 0);
1237 /* And update the next ring slot */
1238 ksp->tx_ring_next_slot = (buff_n + 1) & MAX_TX_DESC_MASK;
1240 spin_unlock_irq(&ksp->txq_lock);
1241 return NETDEV_TX_OK;
1245 * ks8695_stop - Stop (shutdown) a KS8695 ethernet interface
1246 * @ndev: The net_device to stop
1248 * This disables the TX queue and cleans up a KS8695 ethernet
1249 * device.
1251 static int
1252 ks8695_stop(struct net_device *ndev)
1254 struct ks8695_priv *ksp = netdev_priv(ndev);
1256 netif_stop_queue(ndev);
1257 netif_carrier_off(ndev);
1259 ks8695_shutdown(ksp);
1261 return 0;
1265 * ks8695_open - Open (bring up) a KS8695 ethernet interface
1266 * @ndev: The net_device to open
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.
1272 static int
1273 ks8695_open(struct net_device *ndev)
1275 struct ks8695_priv *ksp = netdev_priv(ndev);
1276 int ret;
1278 if (!is_valid_ether_addr(ndev->dev_addr))
1279 return -EADDRNOTAVAIL;
1281 ks8695_reset(ksp);
1283 ks8695_update_mac(ksp);
1285 ret = ks8695_init_net(ksp);
1286 if (ret) {
1287 ks8695_shutdown(ksp);
1288 return ret;
1291 netif_start_queue(ndev);
1293 return 0;
1296 /* Platform device driver */
1299 * ks8695_init_switch - Init LAN switch to known good defaults.
1300 * @ksp: The device to initialise
1302 * This initialises the LAN switch in the KS8695 to a known-good
1303 * set of defaults.
1305 static void __devinit
1306 ks8695_init_switch(struct ks8695_priv *ksp)
1308 u32 ctrl;
1310 /* Default value for SEC0 according to datasheet */
1311 ctrl = 0x40819e00;
1313 /* LED0 = Speed LED1 = Link/Activity */
1314 ctrl &= ~(SEC0_LLED1S | SEC0_LLED0S);
1315 ctrl |= (LLED0S_LINK | LLED1S_LINK_ACTIVITY);
1317 /* Enable Switch */
1318 ctrl |= SEC0_ENABLE;
1320 writel(ctrl, ksp->phyiface_regs + KS8695_SEC0);
1322 /* Defaults for SEC1 */
1323 writel(0x9400100, ksp->phyiface_regs + KS8695_SEC1);
1327 * ks8695_init_wan_phy - Initialise the WAN PHY to sensible defaults
1328 * @ksp: The device to initialise
1330 * This initialises a KS8695's WAN phy to sensible values for
1331 * autonegotiation etc.
1333 static void __devinit
1334 ks8695_init_wan_phy(struct ks8695_priv *ksp)
1336 u32 ctrl;
1338 /* Support auto-negotiation */
1339 ctrl = (WMC_WANAP | WMC_WANA100F | WMC_WANA100H |
1340 WMC_WANA10F | WMC_WANA10H);
1342 /* LED0 = Activity , LED1 = Link */
1343 ctrl |= (WLED0S_ACTIVITY | WLED1S_LINK);
1345 /* Restart Auto-negotiation */
1346 ctrl |= WMC_WANR;
1348 writel(ctrl, ksp->phyiface_regs + KS8695_WMC);
1350 writel(0, ksp->phyiface_regs + KS8695_WPPM);
1351 writel(0, ksp->phyiface_regs + KS8695_PPS);
1354 static 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,
1360 .ndo_validate_addr = eth_validate_addr,
1361 .ndo_set_multicast_list = ks8695_set_multicast,
1365 * ks8695_probe - Probe and initialise a KS8695 ethernet interface
1366 * @pdev: The platform device to probe
1368 * Initialise a KS8695 ethernet device from platform data.
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.
1377 static int __devinit
1378 ks8695_probe(struct platform_device *pdev)
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;
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;
1395 SET_NETDEV_DEV(ndev, &pdev->dev);
1397 dev_dbg(&pdev->dev, "ks8695_probe() called\n");
1399 /* Configure our private structure a little */
1400 ksp = netdev_priv(ndev);
1401 memset(ksp, 0, sizeof(struct ks8695_priv));
1403 ksp->dev = &pdev->dev;
1404 ksp->ndev = ndev;
1405 ksp->msg_enable = NETIF_MSG_LINK;
1407 /* Retrieve resources */
1408 regs_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1409 phyiface_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
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);
1415 if (!(regs_res && rxirq_res && txirq_res)) {
1416 dev_err(ksp->dev, "insufficient resources\n");
1417 ret = -ENOENT;
1418 goto failure;
1421 ksp->regs_req = request_mem_region(regs_res->start,
1422 resource_size(regs_res),
1423 pdev->name);
1425 if (!ksp->regs_req) {
1426 dev_err(ksp->dev, "cannot claim register space\n");
1427 ret = -EIO;
1428 goto failure;
1431 ksp->io_regs = ioremap(regs_res->start, resource_size(regs_res));
1433 if (!ksp->io_regs) {
1434 dev_err(ksp->dev, "failed to ioremap registers\n");
1435 ret = -EINVAL;
1436 goto failure;
1439 if (phyiface_res) {
1440 ksp->phyiface_req =
1441 request_mem_region(phyiface_res->start,
1442 resource_size(phyiface_res),
1443 phyiface_res->name);
1445 if (!ksp->phyiface_req) {
1446 dev_err(ksp->dev,
1447 "cannot claim switch register space\n");
1448 ret = -EIO;
1449 goto failure;
1452 ksp->phyiface_regs = ioremap(phyiface_res->start,
1453 resource_size(phyiface_res));
1455 if (!ksp->phyiface_regs) {
1456 dev_err(ksp->dev,
1457 "failed to ioremap switch registers\n");
1458 ret = -EINVAL;
1459 goto failure;
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";
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);
1476 /* Retrieve the default MAC addr from the chip. */
1477 /* The bootloader should have left it in there for us. */
1479 machigh = ks8695_readreg(ksp, KS8695_MAH);
1480 maclow = ks8695_readreg(ksp, KS8695_MAL);
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;
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);
1493 /* In order to be efficient memory-wise, we allocate both
1494 * rings in one go.
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;
1503 /* Specify the TX DMA ring buffer */
1504 ksp->tx_ring = ksp->ring_base;
1505 ksp->tx_ring_dma = ksp->ring_base_dma;
1507 /* And initialise the queue's lock */
1508 spin_lock_init(&ksp->txq_lock);
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;
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);
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)));
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)));
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;
1545 /* And bring up the net_device with the net core */
1546 platform_set_drvdata(pdev, ndev);
1547 ret = register_netdev(ndev);
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;
1558 /* All is well */
1559 return 0;
1561 /* Error exit path */
1562 failure:
1563 ks8695_release_device(ksp);
1564 free_netdev(ndev);
1566 return ret;
1570 * ks8695_drv_suspend - Suspend a KS8695 ethernet platform device.
1571 * @pdev: The device to suspend
1572 * @state: The suspend state
1574 * This routine detaches and shuts down a KS8695 ethernet device.
1576 static int
1577 ks8695_drv_suspend(struct platform_device *pdev, pm_message_t state)
1579 struct net_device *ndev = platform_get_drvdata(pdev);
1580 struct ks8695_priv *ksp = netdev_priv(ndev);
1582 ksp->in_suspend = 1;
1584 if (netif_running(ndev)) {
1585 netif_device_detach(ndev);
1586 ks8695_shutdown(ksp);
1589 return 0;
1593 * ks8695_drv_resume - Resume a KS8695 ethernet platform device.
1594 * @pdev: The device to resume
1596 * This routine re-initialises and re-attaches a KS8695 ethernet
1597 * device.
1599 static int
1600 ks8695_drv_resume(struct platform_device *pdev)
1602 struct net_device *ndev = platform_get_drvdata(pdev);
1603 struct ks8695_priv *ksp = netdev_priv(ndev);
1605 if (netif_running(ndev)) {
1606 ks8695_reset(ksp);
1607 ks8695_init_net(ksp);
1608 ks8695_set_multicast(ndev);
1609 netif_device_attach(ndev);
1612 ksp->in_suspend = 0;
1614 return 0;
1618 * ks8695_drv_remove - Remove a KS8695 net device on driver unload.
1619 * @pdev: The platform device to remove
1621 * This unregisters and releases a KS8695 ethernet device.
1623 static int __devexit
1624 ks8695_drv_remove(struct platform_device *pdev)
1626 struct net_device *ndev = platform_get_drvdata(pdev);
1627 struct ks8695_priv *ksp = netdev_priv(ndev);
1629 platform_set_drvdata(pdev, NULL);
1631 unregister_netdev(ndev);
1632 ks8695_release_device(ksp);
1633 free_netdev(ndev);
1635 dev_dbg(&pdev->dev, "released and freed device\n");
1636 return 0;
1639 static struct platform_driver ks8695_driver = {
1640 .driver = {
1641 .name = MODULENAME,
1642 .owner = THIS_MODULE,
1644 .probe = ks8695_probe,
1645 .remove = __devexit_p(ks8695_drv_remove),
1646 .suspend = ks8695_drv_suspend,
1647 .resume = ks8695_drv_resume,
1650 /* Module interface */
1652 static int __init
1653 ks8695_init(void)
1655 printk(KERN_INFO "%s Ethernet driver, V%s\n",
1656 MODULENAME, MODULEVERSION);
1658 return platform_driver_register(&ks8695_driver);
1661 static void __exit
1662 ks8695_cleanup(void)
1664 platform_driver_unregister(&ks8695_driver);
1667 module_init(ks8695_init);
1668 module_exit(ks8695_cleanup);
1670 MODULE_AUTHOR("Simtec Electronics")
1671 MODULE_DESCRIPTION("Micrel KS8695 (Centaur) Ethernet driver");
1672 MODULE_LICENSE("GPL");
1673 MODULE_ALIAS("platform:" MODULENAME);
1675 module_param(watchdog, int, 0400);
1676 MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");