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