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