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