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