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>
36 #include <mach/regs-switch.h>
37 #include <mach/regs-misc.h>
38 #include <asm/mach/irq.h>
39 #include <mach/regs-irq.h>
41 #include "ks8695net.h"
43 #define MODULENAME "ks8695_ether"
44 #define MODULEVERSION "1.02"
47 * Transmit and device reset timeout, default 5 seconds.
49 static int watchdog
= 5000;
51 /* Hardware structures */
54 * struct rx_ring_desc - Receive descriptor ring element
55 * @status: The status of the descriptor element (E.g. who owns it)
56 * @length: The number of bytes in the block pointed to by data_ptr
57 * @data_ptr: The physical address of the data block to receive into
58 * @next_desc: The physical address of the next descriptor element.
68 * struct tx_ring_desc - Transmit descriptor ring element
69 * @owner: Who owns the descriptor
70 * @status: The number of bytes in the block pointed to by data_ptr
71 * @data_ptr: The physical address of the data block to receive into
72 * @next_desc: The physical address of the next descriptor element.
82 * struct ks8695_skbuff - sk_buff wrapper for rx/tx rings.
83 * @skb: The buffer in the ring
84 * @dma_ptr: The mapped DMA pointer of the buffer
85 * @length: The number of bytes mapped to dma_ptr
87 struct ks8695_skbuff
{
93 /* Private device structure */
96 #define MAX_TX_DESC_MASK 0x7
97 #define MAX_RX_DESC 16
98 #define MAX_RX_DESC_MASK 0xf
100 /*napi_weight have better more than rx DMA buffers*/
101 #define NAPI_WEIGHT 64
103 #define MAX_RXBUF_SIZE 0x700
105 #define TX_RING_DMA_SIZE (sizeof(struct tx_ring_desc) * MAX_TX_DESC)
106 #define RX_RING_DMA_SIZE (sizeof(struct rx_ring_desc) * MAX_RX_DESC)
107 #define RING_DMA_SIZE (TX_RING_DMA_SIZE + RX_RING_DMA_SIZE)
110 * enum ks8695_dtype - Device type
111 * @KS8695_DTYPE_WAN: This device is a WAN interface
112 * @KS8695_DTYPE_LAN: This device is a LAN interface
113 * @KS8695_DTYPE_HPNA: This device is an HPNA interface
122 * struct ks8695_priv - Private data for the KS8695 Ethernet
123 * @in_suspend: Flag to indicate if we're suspending/resuming
124 * @ndev: The net_device for this interface
125 * @dev: The platform device object for this interface
126 * @dtype: The type of this device
127 * @io_regs: The ioremapped registers for this interface
128 * @napi : Add support NAPI for Rx
129 * @rx_irq_name: The textual name of the RX IRQ from the platform data
130 * @tx_irq_name: The textual name of the TX IRQ from the platform data
131 * @link_irq_name: The textual name of the link IRQ from the
132 * platform data if available
133 * @rx_irq: The IRQ number for the RX IRQ
134 * @tx_irq: The IRQ number for the TX IRQ
135 * @link_irq: The IRQ number for the link IRQ if available
136 * @regs_req: The resource request for the registers region
137 * @phyiface_req: The resource request for the phy/switch region
139 * @phyiface_regs: The ioremapped registers for the phy/switch if available
140 * @ring_base: The base pointer of the dma coherent memory for the rings
141 * @ring_base_dma: The DMA mapped equivalent of ring_base
142 * @tx_ring: The pointer in ring_base of the TX ring
143 * @tx_ring_used: The number of slots in the TX ring which are occupied
144 * @tx_ring_next_slot: The next slot to fill in the TX ring
145 * @tx_ring_dma: The DMA mapped equivalent of tx_ring
146 * @tx_buffers: The sk_buff mappings for the TX ring
147 * @txq_lock: A lock to protect the tx_buffers tx_ring_used etc variables
148 * @rx_ring: The pointer in ring_base of the RX ring
149 * @rx_ring_dma: The DMA mapped equivalent of rx_ring
150 * @rx_buffers: The sk_buff mappings for the RX ring
151 * @next_rx_desc_read: The next RX descriptor to read from on IRQ
152 * @rx_lock: A lock to protect Rx irq function
153 * @msg_enable: The flags for which messages to emit
157 struct net_device
*ndev
;
159 enum ks8695_dtype dtype
;
160 void __iomem
*io_regs
;
162 struct napi_struct napi
;
164 const char *rx_irq_name
, *tx_irq_name
, *link_irq_name
;
165 int rx_irq
, tx_irq
, link_irq
;
167 struct resource
*regs_req
, *phyiface_req
;
168 void __iomem
*phyiface_regs
;
171 dma_addr_t ring_base_dma
;
173 struct tx_ring_desc
*tx_ring
;
175 int tx_ring_next_slot
;
176 dma_addr_t tx_ring_dma
;
177 struct ks8695_skbuff tx_buffers
[MAX_TX_DESC
];
180 struct rx_ring_desc
*rx_ring
;
181 dma_addr_t rx_ring_dma
;
182 struct ks8695_skbuff rx_buffers
[MAX_RX_DESC
];
183 int next_rx_desc_read
;
189 /* Register access */
192 * ks8695_readreg - Read from a KS8695 ethernet register
193 * @ksp: The device to read from
194 * @reg: The register to read
197 ks8695_readreg(struct ks8695_priv
*ksp
, int reg
)
199 return readl(ksp
->io_regs
+ reg
);
203 * ks8695_writereg - Write to a KS8695 ethernet register
204 * @ksp: The device to write to
205 * @reg: The register to write
206 * @value: The value to write to the register
209 ks8695_writereg(struct ks8695_priv
*ksp
, int reg
, u32 value
)
211 writel(value
, ksp
->io_regs
+ reg
);
214 /* Utility functions */
217 * ks8695_port_type - Retrieve port-type as user-friendly string
218 * @ksp: The device to return the type for
220 * Returns a string indicating which of the WAN, LAN or HPNA
221 * ports this device is likely to represent.
224 ks8695_port_type(struct ks8695_priv
*ksp
)
226 switch (ksp
->dtype
) {
227 case KS8695_DTYPE_LAN
:
229 case KS8695_DTYPE_WAN
:
231 case KS8695_DTYPE_HPNA
:
239 * ks8695_update_mac - Update the MAC registers in the device
240 * @ksp: The device to update
242 * Updates the MAC registers in the KS8695 device from the address in the
243 * net_device structure associated with this interface.
246 ks8695_update_mac(struct ks8695_priv
*ksp
)
248 /* Update the HW with the MAC from the net_device */
249 struct net_device
*ndev
= ksp
->ndev
;
252 maclow
= ((ndev
->dev_addr
[2] << 24) | (ndev
->dev_addr
[3] << 16) |
253 (ndev
->dev_addr
[4] << 8) | (ndev
->dev_addr
[5] << 0));
254 machigh
= ((ndev
->dev_addr
[0] << 8) | (ndev
->dev_addr
[1] << 0));
256 ks8695_writereg(ksp
, KS8695_MAL
, maclow
);
257 ks8695_writereg(ksp
, KS8695_MAH
, machigh
);
262 * ks8695_refill_rxbuffers - Re-fill the RX buffer ring
263 * @ksp: The device to refill
265 * Iterates the RX ring of the device looking for empty slots.
266 * For each empty slot, we allocate and map a new SKB and give it
268 * This can be called from interrupt context safely.
271 ks8695_refill_rxbuffers(struct ks8695_priv
*ksp
)
273 /* Run around the RX ring, filling in any missing sk_buff's */
276 for (buff_n
= 0; buff_n
< MAX_RX_DESC
; ++buff_n
) {
277 if (!ksp
->rx_buffers
[buff_n
].skb
) {
278 struct sk_buff
*skb
= dev_alloc_skb(MAX_RXBUF_SIZE
);
281 ksp
->rx_buffers
[buff_n
].skb
= skb
;
283 /* Failed to allocate one, perhaps
284 * we'll try again later.
289 mapping
= dma_map_single(ksp
->dev
, skb
->data
,
292 if (unlikely(dma_mapping_error(ksp
->dev
, mapping
))) {
293 /* Failed to DMA map this SKB, try later */
294 dev_kfree_skb_irq(skb
);
295 ksp
->rx_buffers
[buff_n
].skb
= NULL
;
298 ksp
->rx_buffers
[buff_n
].dma_ptr
= mapping
;
299 skb
->dev
= ksp
->ndev
;
300 ksp
->rx_buffers
[buff_n
].length
= MAX_RXBUF_SIZE
;
302 /* Record this into the DMA ring */
303 ksp
->rx_ring
[buff_n
].data_ptr
= cpu_to_le32(mapping
);
304 ksp
->rx_ring
[buff_n
].length
=
305 cpu_to_le32(MAX_RXBUF_SIZE
);
309 /* And give ownership over to the hardware */
310 ksp
->rx_ring
[buff_n
].status
= cpu_to_le32(RDES_OWN
);
315 /* Maximum number of multicast addresses which the KS8695 HW supports */
316 #define KS8695_NR_ADDRESSES 16
319 * ks8695_init_partial_multicast - Init the mcast addr registers
320 * @ksp: The device to initialise
321 * @addr: The multicast address list to use
322 * @nr_addr: The number of addresses in the list
324 * This routine is a helper for ks8695_set_multicast - it writes
325 * the additional-address registers in the KS8695 ethernet device
326 * and cleans up any others left behind.
329 ks8695_init_partial_multicast(struct ks8695_priv
*ksp
,
330 struct net_device
*ndev
)
334 struct dev_mc_list
*dmi
;
337 netdev_for_each_mc_addr(dmi
, ndev
) {
338 /* Ran out of space in chip? */
339 BUG_ON(i
== KS8695_NR_ADDRESSES
);
341 low
= (dmi
->dmi_addr
[2] << 24) | (dmi
->dmi_addr
[3] << 16) |
342 (dmi
->dmi_addr
[4] << 8) | (dmi
->dmi_addr
[5]);
343 high
= (dmi
->dmi_addr
[0] << 8) | (dmi
->dmi_addr
[1]);
345 ks8695_writereg(ksp
, KS8695_AAL_(i
), low
);
346 ks8695_writereg(ksp
, KS8695_AAH_(i
), AAH_E
| high
);
350 /* Clear the remaining Additional Station Addresses */
351 for (; i
< KS8695_NR_ADDRESSES
; i
++) {
352 ks8695_writereg(ksp
, KS8695_AAL_(i
), 0);
353 ks8695_writereg(ksp
, KS8695_AAH_(i
), 0);
357 /* Interrupt handling */
360 * ks8695_tx_irq - Transmit IRQ handler
361 * @irq: The IRQ which went off (ignored)
362 * @dev_id: The net_device for the interrupt
364 * Process the TX ring, clearing out any transmitted slots.
365 * Allows the net_device to pass us new packets once slots are
369 ks8695_tx_irq(int irq
, void *dev_id
)
371 struct net_device
*ndev
= (struct net_device
*)dev_id
;
372 struct ks8695_priv
*ksp
= netdev_priv(ndev
);
375 for (buff_n
= 0; buff_n
< MAX_TX_DESC
; ++buff_n
) {
376 if (ksp
->tx_buffers
[buff_n
].skb
&&
377 !(ksp
->tx_ring
[buff_n
].owner
& cpu_to_le32(TDES_OWN
))) {
379 /* An SKB which is not owned by HW is present */
380 /* Update the stats for the net_device */
381 ndev
->stats
.tx_packets
++;
382 ndev
->stats
.tx_bytes
+= ksp
->tx_buffers
[buff_n
].length
;
384 /* Free the packet from the ring */
385 ksp
->tx_ring
[buff_n
].data_ptr
= 0;
387 /* Free the sk_buff */
388 dma_unmap_single(ksp
->dev
,
389 ksp
->tx_buffers
[buff_n
].dma_ptr
,
390 ksp
->tx_buffers
[buff_n
].length
,
392 dev_kfree_skb_irq(ksp
->tx_buffers
[buff_n
].skb
);
393 ksp
->tx_buffers
[buff_n
].skb
= NULL
;
398 netif_wake_queue(ndev
);
404 * ks8695_get_rx_enable_bit - Get rx interrupt enable/status bit
405 * @ksp: Private data for the KS8695 Ethernet
407 * For KS8695 document:
408 * Interrupt Enable Register (offset 0xE204)
409 * Bit29 : WAN MAC Receive Interrupt Enable
410 * Bit16 : LAN MAC Receive Interrupt Enable
411 * Interrupt Status Register (Offset 0xF208)
412 * Bit29: WAN MAC Receive Status
413 * Bit16: LAN MAC Receive Status
414 * So, this Rx interrrupt enable/status bit number is equal
417 static inline u32
ks8695_get_rx_enable_bit(struct ks8695_priv
*ksp
)
423 * ks8695_rx_irq - Receive IRQ handler
424 * @irq: The IRQ which went off (ignored)
425 * @dev_id: The net_device for the interrupt
427 * Inform NAPI that packet reception needs to be scheduled
431 ks8695_rx_irq(int irq
, void *dev_id
)
433 struct net_device
*ndev
= (struct net_device
*)dev_id
;
434 struct ks8695_priv
*ksp
= netdev_priv(ndev
);
436 spin_lock(&ksp
->rx_lock
);
438 if (napi_schedule_prep(&ksp
->napi
)) {
439 unsigned long status
= readl(KS8695_IRQ_VA
+ KS8695_INTEN
);
440 unsigned long mask_bit
= 1 << ks8695_get_rx_enable_bit(ksp
);
441 /*disable rx interrupt*/
443 writel(status
, KS8695_IRQ_VA
+ KS8695_INTEN
);
444 __napi_schedule(&ksp
->napi
);
447 spin_unlock(&ksp
->rx_lock
);
452 * ks8695_rx - Receive packets called by NAPI poll method
453 * @ksp: Private data for the KS8695 Ethernet
454 * @budget: Number of packets allowed to process
456 static int ks8695_rx(struct ks8695_priv
*ksp
, int budget
)
458 struct net_device
*ndev
= ksp
->ndev
;
465 buff_n
= ksp
->next_rx_desc_read
;
466 while (received
< budget
467 && ksp
->rx_buffers
[buff_n
].skb
468 && (!(ksp
->rx_ring
[buff_n
].status
&
469 cpu_to_le32(RDES_OWN
)))) {
471 flags
= le32_to_cpu(ksp
->rx_ring
[buff_n
].status
);
473 /* Found an SKB which we own, this means we
476 if ((flags
& (RDES_FS
| RDES_LS
)) !=
477 (RDES_FS
| RDES_LS
)) {
478 /* This packet is not the first and
479 * the last segment. Therefore it is
480 * a "spanning" packet and we can't
486 if (flags
& (RDES_ES
| RDES_RE
)) {
487 /* It's an error packet */
488 ndev
->stats
.rx_errors
++;
490 ndev
->stats
.rx_length_errors
++;
492 ndev
->stats
.rx_length_errors
++;
494 ndev
->stats
.rx_crc_errors
++;
496 ndev
->stats
.rx_missed_errors
++;
501 pktlen
= flags
& RDES_FLEN
;
502 pktlen
-= 4; /* Drop the CRC */
504 /* Retrieve the sk_buff */
505 skb
= ksp
->rx_buffers
[buff_n
].skb
;
507 /* Clear it from the ring */
508 ksp
->rx_buffers
[buff_n
].skb
= NULL
;
509 ksp
->rx_ring
[buff_n
].data_ptr
= 0;
512 dma_unmap_single(ksp
->dev
,
513 ksp
->rx_buffers
[buff_n
].dma_ptr
,
514 ksp
->rx_buffers
[buff_n
].length
,
517 /* Relinquish the SKB to the network layer */
518 skb_put(skb
, pktlen
);
519 skb
->protocol
= eth_type_trans(skb
, ndev
);
520 netif_receive_skb(skb
);
523 ndev
->stats
.rx_packets
++;
524 ndev
->stats
.rx_bytes
+= pktlen
;
528 /* This ring entry is an error, but we can
531 /* Give the ring entry back to the hardware */
532 ksp
->rx_ring
[buff_n
].status
= cpu_to_le32(RDES_OWN
);
535 buff_n
= (buff_n
+ 1) & MAX_RX_DESC_MASK
;
538 /* And note which RX descriptor we last did */
539 ksp
->next_rx_desc_read
= buff_n
;
541 /* And refill the buffers */
542 ks8695_refill_rxbuffers(ksp
);
544 /* Kick the RX DMA engine, in case it became suspended */
545 ks8695_writereg(ksp
, KS8695_DRSC
, 0);
552 * ks8695_poll - Receive packet by NAPI poll method
553 * @ksp: Private data for the KS8695 Ethernet
554 * @budget: The remaining number packets for network subsystem
556 * Invoked by the network core when it requests for new
557 * packets from the driver
559 static int ks8695_poll(struct napi_struct
*napi
, int budget
)
561 struct ks8695_priv
*ksp
= container_of(napi
, struct ks8695_priv
, napi
);
562 unsigned long work_done
;
564 unsigned long isr
= readl(KS8695_IRQ_VA
+ KS8695_INTEN
);
565 unsigned long mask_bit
= 1 << ks8695_get_rx_enable_bit(ksp
);
567 work_done
= ks8695_rx(ksp
, budget
);
569 if (work_done
< budget
) {
571 spin_lock_irqsave(&ksp
->rx_lock
, flags
);
572 __napi_complete(napi
);
573 /*enable rx interrupt*/
574 writel(isr
| mask_bit
, KS8695_IRQ_VA
+ KS8695_INTEN
);
575 spin_unlock_irqrestore(&ksp
->rx_lock
, flags
);
581 * ks8695_link_irq - Link change IRQ handler
582 * @irq: The IRQ which went off (ignored)
583 * @dev_id: The net_device for the interrupt
585 * The WAN interface can generate an IRQ when the link changes,
586 * report this to the net layer and the user.
589 ks8695_link_irq(int irq
, void *dev_id
)
591 struct net_device
*ndev
= (struct net_device
*)dev_id
;
592 struct ks8695_priv
*ksp
= netdev_priv(ndev
);
595 ctrl
= readl(ksp
->phyiface_regs
+ KS8695_WMC
);
596 if (ctrl
& WMC_WLS
) {
597 netif_carrier_on(ndev
);
598 if (netif_msg_link(ksp
))
600 "%s: Link is now up (10%sMbps/%s-duplex)\n",
602 (ctrl
& WMC_WSS
) ? "0" : "",
603 (ctrl
& WMC_WDS
) ? "Full" : "Half");
605 netif_carrier_off(ndev
);
606 if (netif_msg_link(ksp
))
607 dev_info(ksp
->dev
, "%s: Link is now down.\n",
615 /* KS8695 Device functions */
618 * ks8695_reset - Reset a KS8695 ethernet interface
619 * @ksp: The interface to reset
621 * Perform an engine reset of the interface and re-program it
622 * with sensible defaults.
625 ks8695_reset(struct ks8695_priv
*ksp
)
627 int reset_timeout
= watchdog
;
628 /* Issue the reset via the TX DMA control register */
629 ks8695_writereg(ksp
, KS8695_DTXC
, DTXC_TRST
);
630 while (reset_timeout
--) {
631 if (!(ks8695_readreg(ksp
, KS8695_DTXC
) & DTXC_TRST
))
636 if (reset_timeout
< 0) {
638 "Timeout waiting for DMA engines to reset\n");
639 /* And blithely carry on */
642 /* Definitely wait long enough before attempting to program
647 /* RX: unicast and broadcast */
648 ks8695_writereg(ksp
, KS8695_DRXC
, DRXC_RU
| DRXC_RB
);
649 /* TX: pad and add CRC */
650 ks8695_writereg(ksp
, KS8695_DTXC
, DTXC_TEP
| DTXC_TAC
);
654 * ks8695_shutdown - Shut down a KS8695 ethernet interface
655 * @ksp: The interface to shut down
657 * This disables packet RX/TX, cleans up IRQs, drains the rings,
658 * and basically places the interface into a clean shutdown
662 ks8695_shutdown(struct ks8695_priv
*ksp
)
667 /* Disable packet transmission */
668 ctrl
= ks8695_readreg(ksp
, KS8695_DTXC
);
669 ks8695_writereg(ksp
, KS8695_DTXC
, ctrl
& ~DTXC_TE
);
671 /* Disable packet reception */
672 ctrl
= ks8695_readreg(ksp
, KS8695_DRXC
);
673 ks8695_writereg(ksp
, KS8695_DRXC
, ctrl
& ~DRXC_RE
);
675 /* Release the IRQs */
676 free_irq(ksp
->rx_irq
, ksp
->ndev
);
677 free_irq(ksp
->tx_irq
, ksp
->ndev
);
678 if (ksp
->link_irq
!= -1)
679 free_irq(ksp
->link_irq
, ksp
->ndev
);
681 /* Throw away any pending TX packets */
682 for (buff_n
= 0; buff_n
< MAX_TX_DESC
; ++buff_n
) {
683 if (ksp
->tx_buffers
[buff_n
].skb
) {
684 /* Remove this SKB from the TX ring */
685 ksp
->tx_ring
[buff_n
].owner
= 0;
686 ksp
->tx_ring
[buff_n
].status
= 0;
687 ksp
->tx_ring
[buff_n
].data_ptr
= 0;
689 /* Unmap and bin this SKB */
690 dma_unmap_single(ksp
->dev
,
691 ksp
->tx_buffers
[buff_n
].dma_ptr
,
692 ksp
->tx_buffers
[buff_n
].length
,
694 dev_kfree_skb_irq(ksp
->tx_buffers
[buff_n
].skb
);
695 ksp
->tx_buffers
[buff_n
].skb
= NULL
;
699 /* Purge the RX buffers */
700 for (buff_n
= 0; buff_n
< MAX_RX_DESC
; ++buff_n
) {
701 if (ksp
->rx_buffers
[buff_n
].skb
) {
702 /* Remove the SKB from the RX ring */
703 ksp
->rx_ring
[buff_n
].status
= 0;
704 ksp
->rx_ring
[buff_n
].data_ptr
= 0;
706 /* Unmap and bin the SKB */
707 dma_unmap_single(ksp
->dev
,
708 ksp
->rx_buffers
[buff_n
].dma_ptr
,
709 ksp
->rx_buffers
[buff_n
].length
,
711 dev_kfree_skb_irq(ksp
->rx_buffers
[buff_n
].skb
);
712 ksp
->rx_buffers
[buff_n
].skb
= NULL
;
719 * ks8695_setup_irq - IRQ setup helper function
720 * @irq: The IRQ number to claim
721 * @irq_name: The name to give the IRQ claimant
722 * @handler: The function to call to handle the IRQ
723 * @ndev: The net_device to pass in as the dev_id argument to the handler
725 * Return 0 on success.
728 ks8695_setup_irq(int irq
, const char *irq_name
,
729 irq_handler_t handler
, struct net_device
*ndev
)
733 ret
= request_irq(irq
, handler
, IRQF_SHARED
, irq_name
, ndev
);
736 dev_err(&ndev
->dev
, "failure to request IRQ %d\n", irq
);
744 * ks8695_init_net - Initialise a KS8695 ethernet interface
745 * @ksp: The interface to initialise
747 * This routine fills the RX ring, initialises the DMA engines,
748 * allocates the IRQs and then starts the packet TX and RX
752 ks8695_init_net(struct ks8695_priv
*ksp
)
757 ks8695_refill_rxbuffers(ksp
);
759 /* Initialise the DMA engines */
760 ks8695_writereg(ksp
, KS8695_RDLB
, (u32
) ksp
->rx_ring_dma
);
761 ks8695_writereg(ksp
, KS8695_TDLB
, (u32
) ksp
->tx_ring_dma
);
763 /* Request the IRQs */
764 ret
= ks8695_setup_irq(ksp
->rx_irq
, ksp
->rx_irq_name
,
765 ks8695_rx_irq
, ksp
->ndev
);
768 ret
= ks8695_setup_irq(ksp
->tx_irq
, ksp
->tx_irq_name
,
769 ks8695_tx_irq
, ksp
->ndev
);
772 if (ksp
->link_irq
!= -1) {
773 ret
= ks8695_setup_irq(ksp
->link_irq
, ksp
->link_irq_name
,
774 ks8695_link_irq
, ksp
->ndev
);
779 /* Set up the ring indices */
780 ksp
->next_rx_desc_read
= 0;
781 ksp
->tx_ring_next_slot
= 0;
782 ksp
->tx_ring_used
= 0;
784 /* Bring up transmission */
785 ctrl
= ks8695_readreg(ksp
, KS8695_DTXC
);
786 /* Enable packet transmission */
787 ks8695_writereg(ksp
, KS8695_DTXC
, ctrl
| DTXC_TE
);
789 /* Bring up the reception */
790 ctrl
= ks8695_readreg(ksp
, KS8695_DRXC
);
791 /* Enable packet reception */
792 ks8695_writereg(ksp
, KS8695_DRXC
, ctrl
| DRXC_RE
);
793 /* And start the DMA engine */
794 ks8695_writereg(ksp
, KS8695_DRSC
, 0);
801 * ks8695_release_device - HW resource release for KS8695 e-net
802 * @ksp: The device to be freed
804 * This unallocates io memory regions, dma-coherent regions etc
805 * which were allocated in ks8695_probe.
808 ks8695_release_device(struct ks8695_priv
*ksp
)
810 /* Unmap the registers */
811 iounmap(ksp
->io_regs
);
812 if (ksp
->phyiface_regs
)
813 iounmap(ksp
->phyiface_regs
);
815 /* And release the request */
816 release_resource(ksp
->regs_req
);
817 kfree(ksp
->regs_req
);
818 if (ksp
->phyiface_req
) {
819 release_resource(ksp
->phyiface_req
);
820 kfree(ksp
->phyiface_req
);
823 /* Free the ring buffers */
824 dma_free_coherent(ksp
->dev
, RING_DMA_SIZE
,
825 ksp
->ring_base
, ksp
->ring_base_dma
);
828 /* Ethtool support */
831 * ks8695_get_msglevel - Get the messages enabled for emission
832 * @ndev: The network device to read from
835 ks8695_get_msglevel(struct net_device
*ndev
)
837 struct ks8695_priv
*ksp
= netdev_priv(ndev
);
839 return ksp
->msg_enable
;
843 * ks8695_set_msglevel - Set the messages enabled for emission
844 * @ndev: The network device to configure
845 * @value: The messages to set for emission
848 ks8695_set_msglevel(struct net_device
*ndev
, u32 value
)
850 struct ks8695_priv
*ksp
= netdev_priv(ndev
);
852 ksp
->msg_enable
= value
;
856 * ks8695_get_settings - Get device-specific settings.
857 * @ndev: The network device to read settings from
858 * @cmd: The ethtool structure to read into
861 ks8695_get_settings(struct net_device
*ndev
, struct ethtool_cmd
*cmd
)
863 struct ks8695_priv
*ksp
= netdev_priv(ndev
);
866 /* All ports on the KS8695 support these... */
867 cmd
->supported
= (SUPPORTED_10baseT_Half
| SUPPORTED_10baseT_Full
|
868 SUPPORTED_100baseT_Half
| SUPPORTED_100baseT_Full
|
869 SUPPORTED_TP
| SUPPORTED_MII
);
870 cmd
->transceiver
= XCVR_INTERNAL
;
872 /* Port specific extras */
873 switch (ksp
->dtype
) {
874 case KS8695_DTYPE_HPNA
:
875 cmd
->phy_address
= 0;
876 /* not supported for HPNA */
877 cmd
->autoneg
= AUTONEG_DISABLE
;
879 /* BUG: Erm, dtype hpna implies no phy regs */
881 ctrl = readl(KS8695_MISC_VA + KS8695_HMC);
882 cmd->speed = (ctrl & HMC_HSS) ? SPEED_100 : SPEED_10;
883 cmd->duplex = (ctrl & HMC_HDS) ? DUPLEX_FULL : DUPLEX_HALF;
886 case KS8695_DTYPE_WAN
:
887 cmd
->advertising
= ADVERTISED_TP
| ADVERTISED_MII
;
888 cmd
->port
= PORT_MII
;
889 cmd
->supported
|= (SUPPORTED_Autoneg
| SUPPORTED_Pause
);
890 cmd
->phy_address
= 0;
892 ctrl
= readl(ksp
->phyiface_regs
+ KS8695_WMC
);
893 if ((ctrl
& WMC_WAND
) == 0) {
894 /* auto-negotiation is enabled */
895 cmd
->advertising
|= ADVERTISED_Autoneg
;
896 if (ctrl
& WMC_WANA100F
)
897 cmd
->advertising
|= ADVERTISED_100baseT_Full
;
898 if (ctrl
& WMC_WANA100H
)
899 cmd
->advertising
|= ADVERTISED_100baseT_Half
;
900 if (ctrl
& WMC_WANA10F
)
901 cmd
->advertising
|= ADVERTISED_10baseT_Full
;
902 if (ctrl
& WMC_WANA10H
)
903 cmd
->advertising
|= ADVERTISED_10baseT_Half
;
904 if (ctrl
& WMC_WANAP
)
905 cmd
->advertising
|= ADVERTISED_Pause
;
906 cmd
->autoneg
= AUTONEG_ENABLE
;
908 cmd
->speed
= (ctrl
& WMC_WSS
) ? SPEED_100
: SPEED_10
;
909 cmd
->duplex
= (ctrl
& WMC_WDS
) ?
910 DUPLEX_FULL
: DUPLEX_HALF
;
912 /* auto-negotiation is disabled */
913 cmd
->autoneg
= AUTONEG_DISABLE
;
915 cmd
->speed
= (ctrl
& WMC_WANF100
) ?
916 SPEED_100
: SPEED_10
;
917 cmd
->duplex
= (ctrl
& WMC_WANFF
) ?
918 DUPLEX_FULL
: DUPLEX_HALF
;
921 case KS8695_DTYPE_LAN
:
929 * ks8695_set_settings - Set device-specific settings.
930 * @ndev: The network device to configure
931 * @cmd: The settings to configure
934 ks8695_set_settings(struct net_device
*ndev
, struct ethtool_cmd
*cmd
)
936 struct ks8695_priv
*ksp
= netdev_priv(ndev
);
939 if ((cmd
->speed
!= SPEED_10
) && (cmd
->speed
!= SPEED_100
))
941 if ((cmd
->duplex
!= DUPLEX_HALF
) && (cmd
->duplex
!= DUPLEX_FULL
))
943 if (cmd
->port
!= PORT_MII
)
945 if (cmd
->transceiver
!= XCVR_INTERNAL
)
947 if ((cmd
->autoneg
!= AUTONEG_DISABLE
) &&
948 (cmd
->autoneg
!= AUTONEG_ENABLE
))
951 if (cmd
->autoneg
== AUTONEG_ENABLE
) {
952 if ((cmd
->advertising
& (ADVERTISED_10baseT_Half
|
953 ADVERTISED_10baseT_Full
|
954 ADVERTISED_100baseT_Half
|
955 ADVERTISED_100baseT_Full
)) == 0)
958 switch (ksp
->dtype
) {
959 case KS8695_DTYPE_HPNA
:
960 /* HPNA does not support auto-negotiation. */
962 case KS8695_DTYPE_WAN
:
963 ctrl
= readl(ksp
->phyiface_regs
+ KS8695_WMC
);
965 ctrl
&= ~(WMC_WAND
| WMC_WANA100F
| WMC_WANA100H
|
966 WMC_WANA10F
| WMC_WANA10H
);
967 if (cmd
->advertising
& ADVERTISED_100baseT_Full
)
968 ctrl
|= WMC_WANA100F
;
969 if (cmd
->advertising
& ADVERTISED_100baseT_Half
)
970 ctrl
|= WMC_WANA100H
;
971 if (cmd
->advertising
& ADVERTISED_10baseT_Full
)
973 if (cmd
->advertising
& ADVERTISED_10baseT_Half
)
976 /* force a re-negotiation */
978 writel(ctrl
, ksp
->phyiface_regs
+ KS8695_WMC
);
980 case KS8695_DTYPE_LAN
:
985 switch (ksp
->dtype
) {
986 case KS8695_DTYPE_HPNA
:
987 /* BUG: dtype_hpna implies no phy registers */
989 ctrl = __raw_readl(KS8695_MISC_VA + KS8695_HMC);
991 ctrl &= ~(HMC_HSS | HMC_HDS);
992 if (cmd->speed == SPEED_100)
994 if (cmd->duplex == DUPLEX_FULL)
997 __raw_writel(ctrl, KS8695_MISC_VA + KS8695_HMC);
1000 case KS8695_DTYPE_WAN
:
1001 ctrl
= readl(ksp
->phyiface_regs
+ KS8695_WMC
);
1003 /* disable auto-negotiation */
1005 ctrl
&= ~(WMC_WANF100
| WMC_WANFF
);
1007 if (cmd
->speed
== SPEED_100
)
1008 ctrl
|= WMC_WANF100
;
1009 if (cmd
->duplex
== DUPLEX_FULL
)
1012 writel(ctrl
, ksp
->phyiface_regs
+ KS8695_WMC
);
1014 case KS8695_DTYPE_LAN
:
1023 * ks8695_nwayreset - Restart the autonegotiation on the port.
1024 * @ndev: The network device to restart autoneotiation on
1027 ks8695_nwayreset(struct net_device
*ndev
)
1029 struct ks8695_priv
*ksp
= netdev_priv(ndev
);
1032 switch (ksp
->dtype
) {
1033 case KS8695_DTYPE_HPNA
:
1034 /* No phy means no autonegotiation on hpna */
1036 case KS8695_DTYPE_WAN
:
1037 ctrl
= readl(ksp
->phyiface_regs
+ KS8695_WMC
);
1039 if ((ctrl
& WMC_WAND
) == 0)
1040 writel(ctrl
| WMC_WANR
,
1041 ksp
->phyiface_regs
+ KS8695_WMC
);
1043 /* auto-negotiation not enabled */
1046 case KS8695_DTYPE_LAN
:
1054 * ks8695_get_link - Retrieve link status of network interface
1055 * @ndev: The network interface to retrive the link status of.
1058 ks8695_get_link(struct net_device
*ndev
)
1060 struct ks8695_priv
*ksp
= netdev_priv(ndev
);
1063 switch (ksp
->dtype
) {
1064 case KS8695_DTYPE_HPNA
:
1065 /* HPNA always has link */
1067 case KS8695_DTYPE_WAN
:
1068 /* WAN we can read the PHY for */
1069 ctrl
= readl(ksp
->phyiface_regs
+ KS8695_WMC
);
1070 return ctrl
& WMC_WLS
;
1071 case KS8695_DTYPE_LAN
:
1078 * ks8695_get_pause - Retrieve network pause/flow-control advertising
1079 * @ndev: The device to retrieve settings from
1080 * @param: The structure to fill out with the information
1083 ks8695_get_pause(struct net_device
*ndev
, struct ethtool_pauseparam
*param
)
1085 struct ks8695_priv
*ksp
= netdev_priv(ndev
);
1088 switch (ksp
->dtype
) {
1089 case KS8695_DTYPE_HPNA
:
1090 /* No phy link on hpna to configure */
1092 case KS8695_DTYPE_WAN
:
1093 ctrl
= readl(ksp
->phyiface_regs
+ KS8695_WMC
);
1095 /* advertise Pause */
1096 param
->autoneg
= (ctrl
& WMC_WANAP
);
1098 /* current Rx Flow-control */
1099 ctrl
= ks8695_readreg(ksp
, KS8695_DRXC
);
1100 param
->rx_pause
= (ctrl
& DRXC_RFCE
);
1102 /* current Tx Flow-control */
1103 ctrl
= ks8695_readreg(ksp
, KS8695_DTXC
);
1104 param
->tx_pause
= (ctrl
& DTXC_TFCE
);
1106 case KS8695_DTYPE_LAN
:
1107 /* The LAN's "phy" is a direct-attached switch */
1113 * ks8695_set_pause - Configure pause/flow-control
1114 * @ndev: The device to configure
1115 * @param: The pause parameters to set
1117 * TODO: Implement this
1120 ks8695_set_pause(struct net_device
*ndev
, struct ethtool_pauseparam
*param
)
1126 * ks8695_get_drvinfo - Retrieve driver information
1127 * @ndev: The network device to retrieve info about
1128 * @info: The info structure to fill out.
1131 ks8695_get_drvinfo(struct net_device
*ndev
, struct ethtool_drvinfo
*info
)
1133 strlcpy(info
->driver
, MODULENAME
, sizeof(info
->driver
));
1134 strlcpy(info
->version
, MODULEVERSION
, sizeof(info
->version
));
1135 strlcpy(info
->bus_info
, dev_name(ndev
->dev
.parent
),
1136 sizeof(info
->bus_info
));
1139 static const struct ethtool_ops ks8695_ethtool_ops
= {
1140 .get_msglevel
= ks8695_get_msglevel
,
1141 .set_msglevel
= ks8695_set_msglevel
,
1142 .get_settings
= ks8695_get_settings
,
1143 .set_settings
= ks8695_set_settings
,
1144 .nway_reset
= ks8695_nwayreset
,
1145 .get_link
= ks8695_get_link
,
1146 .get_pauseparam
= ks8695_get_pause
,
1147 .set_pauseparam
= ks8695_set_pause
,
1148 .get_drvinfo
= ks8695_get_drvinfo
,
1151 /* Network device interface functions */
1154 * ks8695_set_mac - Update MAC in net dev and HW
1155 * @ndev: The network device to update
1156 * @addr: The new MAC address to set
1159 ks8695_set_mac(struct net_device
*ndev
, void *addr
)
1161 struct ks8695_priv
*ksp
= netdev_priv(ndev
);
1162 struct sockaddr
*address
= addr
;
1164 if (!is_valid_ether_addr(address
->sa_data
))
1165 return -EADDRNOTAVAIL
;
1167 memcpy(ndev
->dev_addr
, address
->sa_data
, ndev
->addr_len
);
1169 ks8695_update_mac(ksp
);
1171 dev_dbg(ksp
->dev
, "%s: Updated MAC address to %pM\n",
1172 ndev
->name
, ndev
->dev_addr
);
1178 * ks8695_set_multicast - Set up the multicast behaviour of the interface
1179 * @ndev: The net_device to configure
1181 * This routine, called by the net layer, configures promiscuity
1182 * and multicast reception behaviour for the interface.
1185 ks8695_set_multicast(struct net_device
*ndev
)
1187 struct ks8695_priv
*ksp
= netdev_priv(ndev
);
1190 ctrl
= ks8695_readreg(ksp
, KS8695_DRXC
);
1192 if (ndev
->flags
& IFF_PROMISC
) {
1193 /* enable promiscuous mode */
1195 } else if (ndev
->flags
& ~IFF_PROMISC
) {
1196 /* disable promiscuous mode */
1200 if (ndev
->flags
& IFF_ALLMULTI
) {
1201 /* enable all multicast mode */
1203 } else if (netdev_mc_count(ndev
) > KS8695_NR_ADDRESSES
) {
1204 /* more specific multicast addresses than can be
1205 * handled in hardware
1209 /* enable specific multicasts */
1211 ks8695_init_partial_multicast(ksp
, ndev
);
1214 ks8695_writereg(ksp
, KS8695_DRXC
, ctrl
);
1218 * ks8695_timeout - Handle a network tx/rx timeout.
1219 * @ndev: The net_device which timed out.
1221 * A network transaction timed out, reset the device.
1224 ks8695_timeout(struct net_device
*ndev
)
1226 struct ks8695_priv
*ksp
= netdev_priv(ndev
);
1228 netif_stop_queue(ndev
);
1229 ks8695_shutdown(ksp
);
1233 ks8695_update_mac(ksp
);
1235 /* We ignore the return from this since it managed to init
1236 * before it probably will be okay to init again.
1238 ks8695_init_net(ksp
);
1240 /* Reconfigure promiscuity etc */
1241 ks8695_set_multicast(ndev
);
1243 /* And start the TX queue once more */
1244 netif_start_queue(ndev
);
1248 * ks8695_start_xmit - Start a packet transmission
1249 * @skb: The packet to transmit
1250 * @ndev: The network device to send the packet on
1252 * This routine, called by the net layer, takes ownership of the
1253 * sk_buff and adds it to the TX ring. It then kicks the TX DMA
1254 * engine to ensure transmission begins.
1257 ks8695_start_xmit(struct sk_buff
*skb
, struct net_device
*ndev
)
1259 struct ks8695_priv
*ksp
= netdev_priv(ndev
);
1263 spin_lock_irq(&ksp
->txq_lock
);
1265 if (ksp
->tx_ring_used
== MAX_TX_DESC
) {
1266 /* Somehow we got entered when we have no room */
1267 spin_unlock_irq(&ksp
->txq_lock
);
1268 return NETDEV_TX_BUSY
;
1271 buff_n
= ksp
->tx_ring_next_slot
;
1273 BUG_ON(ksp
->tx_buffers
[buff_n
].skb
);
1275 dmap
= dma_map_single(ksp
->dev
, skb
->data
, skb
->len
, DMA_TO_DEVICE
);
1276 if (unlikely(dma_mapping_error(ksp
->dev
, dmap
))) {
1277 /* Failed to DMA map this SKB, give it back for now */
1278 spin_unlock_irq(&ksp
->txq_lock
);
1279 dev_dbg(ksp
->dev
, "%s: Could not map DMA memory for "\
1280 "transmission, trying later\n", ndev
->name
);
1281 return NETDEV_TX_BUSY
;
1284 ksp
->tx_buffers
[buff_n
].dma_ptr
= dmap
;
1285 /* Mapped okay, store the buffer pointer and length for later */
1286 ksp
->tx_buffers
[buff_n
].skb
= skb
;
1287 ksp
->tx_buffers
[buff_n
].length
= skb
->len
;
1289 /* Fill out the TX descriptor */
1290 ksp
->tx_ring
[buff_n
].data_ptr
=
1291 cpu_to_le32(ksp
->tx_buffers
[buff_n
].dma_ptr
);
1292 ksp
->tx_ring
[buff_n
].status
=
1293 cpu_to_le32(TDES_IC
| TDES_FS
| TDES_LS
|
1294 (skb
->len
& TDES_TBS
));
1298 /* Hand it over to the hardware */
1299 ksp
->tx_ring
[buff_n
].owner
= cpu_to_le32(TDES_OWN
);
1301 if (++ksp
->tx_ring_used
== MAX_TX_DESC
)
1302 netif_stop_queue(ndev
);
1304 ndev
->trans_start
= jiffies
;
1306 /* Kick the TX DMA in case it decided to go IDLE */
1307 ks8695_writereg(ksp
, KS8695_DTSC
, 0);
1309 /* And update the next ring slot */
1310 ksp
->tx_ring_next_slot
= (buff_n
+ 1) & MAX_TX_DESC_MASK
;
1312 spin_unlock_irq(&ksp
->txq_lock
);
1313 return NETDEV_TX_OK
;
1317 * ks8695_stop - Stop (shutdown) a KS8695 ethernet interface
1318 * @ndev: The net_device to stop
1320 * This disables the TX queue and cleans up a KS8695 ethernet
1324 ks8695_stop(struct net_device
*ndev
)
1326 struct ks8695_priv
*ksp
= netdev_priv(ndev
);
1328 netif_stop_queue(ndev
);
1329 napi_disable(&ksp
->napi
);
1331 ks8695_shutdown(ksp
);
1337 * ks8695_open - Open (bring up) a KS8695 ethernet interface
1338 * @ndev: The net_device to open
1340 * This resets, configures the MAC, initialises the RX ring and
1341 * DMA engines and starts the TX queue for a KS8695 ethernet
1345 ks8695_open(struct net_device
*ndev
)
1347 struct ks8695_priv
*ksp
= netdev_priv(ndev
);
1350 if (!is_valid_ether_addr(ndev
->dev_addr
))
1351 return -EADDRNOTAVAIL
;
1355 ks8695_update_mac(ksp
);
1357 ret
= ks8695_init_net(ksp
);
1359 ks8695_shutdown(ksp
);
1363 napi_enable(&ksp
->napi
);
1364 netif_start_queue(ndev
);
1369 /* Platform device driver */
1372 * ks8695_init_switch - Init LAN switch to known good defaults.
1373 * @ksp: The device to initialise
1375 * This initialises the LAN switch in the KS8695 to a known-good
1378 static void __devinit
1379 ks8695_init_switch(struct ks8695_priv
*ksp
)
1383 /* Default value for SEC0 according to datasheet */
1386 /* LED0 = Speed LED1 = Link/Activity */
1387 ctrl
&= ~(SEC0_LLED1S
| SEC0_LLED0S
);
1388 ctrl
|= (LLED0S_LINK
| LLED1S_LINK_ACTIVITY
);
1391 ctrl
|= SEC0_ENABLE
;
1393 writel(ctrl
, ksp
->phyiface_regs
+ KS8695_SEC0
);
1395 /* Defaults for SEC1 */
1396 writel(0x9400100, ksp
->phyiface_regs
+ KS8695_SEC1
);
1400 * ks8695_init_wan_phy - Initialise the WAN PHY to sensible defaults
1401 * @ksp: The device to initialise
1403 * This initialises a KS8695's WAN phy to sensible values for
1404 * autonegotiation etc.
1406 static void __devinit
1407 ks8695_init_wan_phy(struct ks8695_priv
*ksp
)
1411 /* Support auto-negotiation */
1412 ctrl
= (WMC_WANAP
| WMC_WANA100F
| WMC_WANA100H
|
1413 WMC_WANA10F
| WMC_WANA10H
);
1415 /* LED0 = Activity , LED1 = Link */
1416 ctrl
|= (WLED0S_ACTIVITY
| WLED1S_LINK
);
1418 /* Restart Auto-negotiation */
1421 writel(ctrl
, ksp
->phyiface_regs
+ KS8695_WMC
);
1423 writel(0, ksp
->phyiface_regs
+ KS8695_WPPM
);
1424 writel(0, ksp
->phyiface_regs
+ KS8695_PPS
);
1427 static const struct net_device_ops ks8695_netdev_ops
= {
1428 .ndo_open
= ks8695_open
,
1429 .ndo_stop
= ks8695_stop
,
1430 .ndo_start_xmit
= ks8695_start_xmit
,
1431 .ndo_tx_timeout
= ks8695_timeout
,
1432 .ndo_set_mac_address
= ks8695_set_mac
,
1433 .ndo_validate_addr
= eth_validate_addr
,
1434 .ndo_set_multicast_list
= ks8695_set_multicast
,
1438 * ks8695_probe - Probe and initialise a KS8695 ethernet interface
1439 * @pdev: The platform device to probe
1441 * Initialise a KS8695 ethernet device from platform data.
1443 * This driver requires at least one IORESOURCE_MEM for the
1444 * registers and two IORESOURCE_IRQ for the RX and TX IRQs
1445 * respectively. It can optionally take an additional
1446 * IORESOURCE_MEM for the switch or phy in the case of the lan or
1447 * wan ports, and an IORESOURCE_IRQ for the link IRQ for the wan
1450 static int __devinit
1451 ks8695_probe(struct platform_device
*pdev
)
1453 struct ks8695_priv
*ksp
;
1454 struct net_device
*ndev
;
1455 struct resource
*regs_res
, *phyiface_res
;
1456 struct resource
*rxirq_res
, *txirq_res
, *linkirq_res
;
1459 u32 machigh
, maclow
;
1461 /* Initialise a net_device */
1462 ndev
= alloc_etherdev(sizeof(struct ks8695_priv
));
1464 dev_err(&pdev
->dev
, "could not allocate device.\n");
1468 SET_NETDEV_DEV(ndev
, &pdev
->dev
);
1470 dev_dbg(&pdev
->dev
, "ks8695_probe() called\n");
1472 /* Configure our private structure a little */
1473 ksp
= netdev_priv(ndev
);
1474 memset(ksp
, 0, sizeof(struct ks8695_priv
));
1476 ksp
->dev
= &pdev
->dev
;
1478 ksp
->msg_enable
= NETIF_MSG_LINK
;
1480 /* Retrieve resources */
1481 regs_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1482 phyiface_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1484 rxirq_res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1485 txirq_res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 1);
1486 linkirq_res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 2);
1488 if (!(regs_res
&& rxirq_res
&& txirq_res
)) {
1489 dev_err(ksp
->dev
, "insufficient resources\n");
1494 ksp
->regs_req
= request_mem_region(regs_res
->start
,
1495 resource_size(regs_res
),
1498 if (!ksp
->regs_req
) {
1499 dev_err(ksp
->dev
, "cannot claim register space\n");
1504 ksp
->io_regs
= ioremap(regs_res
->start
, resource_size(regs_res
));
1506 if (!ksp
->io_regs
) {
1507 dev_err(ksp
->dev
, "failed to ioremap registers\n");
1514 request_mem_region(phyiface_res
->start
,
1515 resource_size(phyiface_res
),
1516 phyiface_res
->name
);
1518 if (!ksp
->phyiface_req
) {
1520 "cannot claim switch register space\n");
1525 ksp
->phyiface_regs
= ioremap(phyiface_res
->start
,
1526 resource_size(phyiface_res
));
1528 if (!ksp
->phyiface_regs
) {
1530 "failed to ioremap switch registers\n");
1536 ksp
->rx_irq
= rxirq_res
->start
;
1537 ksp
->rx_irq_name
= rxirq_res
->name
? rxirq_res
->name
: "Ethernet RX";
1538 ksp
->tx_irq
= txirq_res
->start
;
1539 ksp
->tx_irq_name
= txirq_res
->name
? txirq_res
->name
: "Ethernet TX";
1540 ksp
->link_irq
= (linkirq_res
? linkirq_res
->start
: -1);
1541 ksp
->link_irq_name
= (linkirq_res
&& linkirq_res
->name
) ?
1542 linkirq_res
->name
: "Ethernet Link";
1544 /* driver system setup */
1545 ndev
->netdev_ops
= &ks8695_netdev_ops
;
1546 SET_ETHTOOL_OPS(ndev
, &ks8695_ethtool_ops
);
1547 ndev
->watchdog_timeo
= msecs_to_jiffies(watchdog
);
1549 netif_napi_add(ndev
, &ksp
->napi
, ks8695_poll
, NAPI_WEIGHT
);
1551 /* Retrieve the default MAC addr from the chip. */
1552 /* The bootloader should have left it in there for us. */
1554 machigh
= ks8695_readreg(ksp
, KS8695_MAH
);
1555 maclow
= ks8695_readreg(ksp
, KS8695_MAL
);
1557 ndev
->dev_addr
[0] = (machigh
>> 8) & 0xFF;
1558 ndev
->dev_addr
[1] = machigh
& 0xFF;
1559 ndev
->dev_addr
[2] = (maclow
>> 24) & 0xFF;
1560 ndev
->dev_addr
[3] = (maclow
>> 16) & 0xFF;
1561 ndev
->dev_addr
[4] = (maclow
>> 8) & 0xFF;
1562 ndev
->dev_addr
[5] = maclow
& 0xFF;
1564 if (!is_valid_ether_addr(ndev
->dev_addr
))
1565 dev_warn(ksp
->dev
, "%s: Invalid ethernet MAC address. Please "
1566 "set using ifconfig\n", ndev
->name
);
1568 /* In order to be efficient memory-wise, we allocate both
1571 ksp
->ring_base
= dma_alloc_coherent(&pdev
->dev
, RING_DMA_SIZE
,
1572 &ksp
->ring_base_dma
, GFP_KERNEL
);
1573 if (!ksp
->ring_base
) {
1578 /* Specify the TX DMA ring buffer */
1579 ksp
->tx_ring
= ksp
->ring_base
;
1580 ksp
->tx_ring_dma
= ksp
->ring_base_dma
;
1582 /* And initialise the queue's lock */
1583 spin_lock_init(&ksp
->txq_lock
);
1584 spin_lock_init(&ksp
->rx_lock
);
1586 /* Specify the RX DMA ring buffer */
1587 ksp
->rx_ring
= ksp
->ring_base
+ TX_RING_DMA_SIZE
;
1588 ksp
->rx_ring_dma
= ksp
->ring_base_dma
+ TX_RING_DMA_SIZE
;
1590 /* Zero the descriptor rings */
1591 memset(ksp
->tx_ring
, 0, TX_RING_DMA_SIZE
);
1592 memset(ksp
->rx_ring
, 0, RX_RING_DMA_SIZE
);
1594 /* Build the rings */
1595 for (buff_n
= 0; buff_n
< MAX_TX_DESC
; ++buff_n
) {
1596 ksp
->tx_ring
[buff_n
].next_desc
=
1597 cpu_to_le32(ksp
->tx_ring_dma
+
1598 (sizeof(struct tx_ring_desc
) *
1599 ((buff_n
+ 1) & MAX_TX_DESC_MASK
)));
1602 for (buff_n
= 0; buff_n
< MAX_RX_DESC
; ++buff_n
) {
1603 ksp
->rx_ring
[buff_n
].next_desc
=
1604 cpu_to_le32(ksp
->rx_ring_dma
+
1605 (sizeof(struct rx_ring_desc
) *
1606 ((buff_n
+ 1) & MAX_RX_DESC_MASK
)));
1609 /* Initialise the port (physically) */
1610 if (ksp
->phyiface_regs
&& ksp
->link_irq
== -1) {
1611 ks8695_init_switch(ksp
);
1612 ksp
->dtype
= KS8695_DTYPE_LAN
;
1613 } else if (ksp
->phyiface_regs
&& ksp
->link_irq
!= -1) {
1614 ks8695_init_wan_phy(ksp
);
1615 ksp
->dtype
= KS8695_DTYPE_WAN
;
1617 /* No initialisation since HPNA does not have a PHY */
1618 ksp
->dtype
= KS8695_DTYPE_HPNA
;
1621 /* And bring up the net_device with the net core */
1622 platform_set_drvdata(pdev
, ndev
);
1623 ret
= register_netdev(ndev
);
1626 dev_info(ksp
->dev
, "ks8695 ethernet (%s) MAC: %pM\n",
1627 ks8695_port_type(ksp
), ndev
->dev_addr
);
1629 /* Report the failure to register the net_device */
1630 dev_err(ksp
->dev
, "ks8695net: failed to register netdev.\n");
1637 /* Error exit path */
1639 ks8695_release_device(ksp
);
1646 * ks8695_drv_suspend - Suspend a KS8695 ethernet platform device.
1647 * @pdev: The device to suspend
1648 * @state: The suspend state
1650 * This routine detaches and shuts down a KS8695 ethernet device.
1653 ks8695_drv_suspend(struct platform_device
*pdev
, pm_message_t state
)
1655 struct net_device
*ndev
= platform_get_drvdata(pdev
);
1656 struct ks8695_priv
*ksp
= netdev_priv(ndev
);
1658 ksp
->in_suspend
= 1;
1660 if (netif_running(ndev
)) {
1661 netif_device_detach(ndev
);
1662 ks8695_shutdown(ksp
);
1669 * ks8695_drv_resume - Resume a KS8695 ethernet platform device.
1670 * @pdev: The device to resume
1672 * This routine re-initialises and re-attaches a KS8695 ethernet
1676 ks8695_drv_resume(struct platform_device
*pdev
)
1678 struct net_device
*ndev
= platform_get_drvdata(pdev
);
1679 struct ks8695_priv
*ksp
= netdev_priv(ndev
);
1681 if (netif_running(ndev
)) {
1683 ks8695_init_net(ksp
);
1684 ks8695_set_multicast(ndev
);
1685 netif_device_attach(ndev
);
1688 ksp
->in_suspend
= 0;
1694 * ks8695_drv_remove - Remove a KS8695 net device on driver unload.
1695 * @pdev: The platform device to remove
1697 * This unregisters and releases a KS8695 ethernet device.
1699 static int __devexit
1700 ks8695_drv_remove(struct platform_device
*pdev
)
1702 struct net_device
*ndev
= platform_get_drvdata(pdev
);
1703 struct ks8695_priv
*ksp
= netdev_priv(ndev
);
1705 platform_set_drvdata(pdev
, NULL
);
1706 netif_napi_del(&ksp
->napi
);
1708 unregister_netdev(ndev
);
1709 ks8695_release_device(ksp
);
1712 dev_dbg(&pdev
->dev
, "released and freed device\n");
1716 static struct platform_driver ks8695_driver
= {
1719 .owner
= THIS_MODULE
,
1721 .probe
= ks8695_probe
,
1722 .remove
= __devexit_p(ks8695_drv_remove
),
1723 .suspend
= ks8695_drv_suspend
,
1724 .resume
= ks8695_drv_resume
,
1727 /* Module interface */
1732 printk(KERN_INFO
"%s Ethernet driver, V%s\n",
1733 MODULENAME
, MODULEVERSION
);
1735 return platform_driver_register(&ks8695_driver
);
1739 ks8695_cleanup(void)
1741 platform_driver_unregister(&ks8695_driver
);
1744 module_init(ks8695_init
);
1745 module_exit(ks8695_cleanup
);
1747 MODULE_AUTHOR("Simtec Electronics")
1748 MODULE_DESCRIPTION("Micrel KS8695 (Centaur) Ethernet driver");
1749 MODULE_LICENSE("GPL");
1750 MODULE_ALIAS("platform:" MODULENAME
);
1752 module_param(watchdog
, int, 0400);
1753 MODULE_PARM_DESC(watchdog
, "transmit timeout in milliseconds");