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