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