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