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