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