blob: 634dad1c8b48ea024a66804b88ed783b59da48c1 [file] [log] [blame]
Richard Ršöjforsb07878e2009-06-04 03:35:55 +00001/*
Richard Röjforsa1aa8822010-04-21 16:33:29 -07002 * ks8842.c timberdale KS8842 ethernet driver
Richard Ršöjforsb07878e2009-06-04 03:35:55 +00003 * Copyright (c) 2009 Intel Corporation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19/* Supports:
20 * The Micrel KS8842 behind the timberdale FPGA
David J. Choi28bd6202010-07-13 10:09:19 -070021 * The genuine Micrel KS8841/42 device with ISA 16/32bit bus interface
Richard Ršöjforsb07878e2009-06-04 03:35:55 +000022 */
23
Joe Perches0dc7d2b2010-02-27 14:43:51 +000024#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
Richard Ršöjforsb07878e2009-06-04 03:35:55 +000026#include <linux/kernel.h>
27#include <linux/module.h>
28#include <linux/platform_device.h>
29#include <linux/netdevice.h>
30#include <linux/etherdevice.h>
31#include <linux/ethtool.h>
Richard Röjforsa1aa8822010-04-21 16:33:29 -070032#include <linux/ks8842.h>
Richard Ršöjforsb07878e2009-06-04 03:35:55 +000033
34#define DRV_NAME "ks8842"
35
36/* Timberdale specific Registers */
37#define REG_TIMB_RST 0x1c
38
39/* KS8842 registers */
40
41#define REG_SELECT_BANK 0x0e
42
43/* bank 0 registers */
44#define REG_QRFCR 0x04
45
46/* bank 2 registers */
47#define REG_MARL 0x00
48#define REG_MARM 0x02
49#define REG_MARH 0x04
50
51/* bank 3 registers */
52#define REG_GRR 0x06
53
54/* bank 16 registers */
55#define REG_TXCR 0x00
56#define REG_TXSR 0x02
57#define REG_RXCR 0x04
58#define REG_TXMIR 0x08
59#define REG_RXMIR 0x0A
60
61/* bank 17 registers */
62#define REG_TXQCR 0x00
63#define REG_RXQCR 0x02
64#define REG_TXFDPR 0x04
65#define REG_RXFDPR 0x06
66#define REG_QMU_DATA_LO 0x08
67#define REG_QMU_DATA_HI 0x0A
68
69/* bank 18 registers */
70#define REG_IER 0x00
71#define IRQ_LINK_CHANGE 0x8000
72#define IRQ_TX 0x4000
73#define IRQ_RX 0x2000
74#define IRQ_RX_OVERRUN 0x0800
75#define IRQ_TX_STOPPED 0x0200
76#define IRQ_RX_STOPPED 0x0100
77#define IRQ_RX_ERROR 0x0080
78#define ENABLED_IRQS (IRQ_LINK_CHANGE | IRQ_TX | IRQ_RX | IRQ_RX_STOPPED | \
79 IRQ_TX_STOPPED | IRQ_RX_OVERRUN | IRQ_RX_ERROR)
80#define REG_ISR 0x02
81#define REG_RXSR 0x04
82#define RXSR_VALID 0x8000
83#define RXSR_BROADCAST 0x80
84#define RXSR_MULTICAST 0x40
85#define RXSR_UNICAST 0x20
86#define RXSR_FRAMETYPE 0x08
87#define RXSR_TOO_LONG 0x04
88#define RXSR_RUNT 0x02
89#define RXSR_CRC_ERROR 0x01
90#define RXSR_ERROR (RXSR_TOO_LONG | RXSR_RUNT | RXSR_CRC_ERROR)
91
92/* bank 32 registers */
93#define REG_SW_ID_AND_ENABLE 0x00
94#define REG_SGCR1 0x02
95#define REG_SGCR2 0x04
96#define REG_SGCR3 0x06
97
98/* bank 39 registers */
99#define REG_MACAR1 0x00
100#define REG_MACAR2 0x02
101#define REG_MACAR3 0x04
102
103/* bank 45 registers */
104#define REG_P1MBCR 0x00
105#define REG_P1MBSR 0x02
106
107/* bank 46 registers */
108#define REG_P2MBCR 0x00
109#define REG_P2MBSR 0x02
110
111/* bank 48 registers */
112#define REG_P1CR2 0x02
113
114/* bank 49 registers */
115#define REG_P1CR4 0x02
116#define REG_P1SR 0x04
117
David J. Choi28bd6202010-07-13 10:09:19 -0700118/* flags passed by platform_device for configuration */
119#define MICREL_KS884X 0x01 /* 0=Timeberdale(FPGA), 1=Micrel */
120#define KS884X_16BIT 0x02 /* 1=16bit, 0=32bit */
121
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000122struct ks8842_adapter {
123 void __iomem *hw_addr;
124 int irq;
David J. Choi28bd6202010-07-13 10:09:19 -0700125 unsigned long conf_flags; /* copy of platform_device config */
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000126 struct tasklet_struct tasklet;
127 spinlock_t lock; /* spinlock to be interrupt safe */
Richard Röjforscc88e452010-07-08 23:36:30 -0700128 struct work_struct timeout_work;
129 struct net_device *netdev;
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000130};
131
132static inline void ks8842_select_bank(struct ks8842_adapter *adapter, u16 bank)
133{
134 iowrite16(bank, adapter->hw_addr + REG_SELECT_BANK);
135}
136
137static inline void ks8842_write8(struct ks8842_adapter *adapter, u16 bank,
138 u8 value, int offset)
139{
140 ks8842_select_bank(adapter, bank);
141 iowrite8(value, adapter->hw_addr + offset);
142}
143
144static inline void ks8842_write16(struct ks8842_adapter *adapter, u16 bank,
145 u16 value, int offset)
146{
147 ks8842_select_bank(adapter, bank);
148 iowrite16(value, adapter->hw_addr + offset);
149}
150
151static inline void ks8842_enable_bits(struct ks8842_adapter *adapter, u16 bank,
152 u16 bits, int offset)
153{
154 u16 reg;
155 ks8842_select_bank(adapter, bank);
156 reg = ioread16(adapter->hw_addr + offset);
157 reg |= bits;
158 iowrite16(reg, adapter->hw_addr + offset);
159}
160
161static inline void ks8842_clear_bits(struct ks8842_adapter *adapter, u16 bank,
162 u16 bits, int offset)
163{
164 u16 reg;
165 ks8842_select_bank(adapter, bank);
166 reg = ioread16(adapter->hw_addr + offset);
167 reg &= ~bits;
168 iowrite16(reg, adapter->hw_addr + offset);
169}
170
171static inline void ks8842_write32(struct ks8842_adapter *adapter, u16 bank,
172 u32 value, int offset)
173{
174 ks8842_select_bank(adapter, bank);
175 iowrite32(value, adapter->hw_addr + offset);
176}
177
178static inline u8 ks8842_read8(struct ks8842_adapter *adapter, u16 bank,
179 int offset)
180{
181 ks8842_select_bank(adapter, bank);
182 return ioread8(adapter->hw_addr + offset);
183}
184
185static inline u16 ks8842_read16(struct ks8842_adapter *adapter, u16 bank,
186 int offset)
187{
188 ks8842_select_bank(adapter, bank);
189 return ioread16(adapter->hw_addr + offset);
190}
191
192static inline u32 ks8842_read32(struct ks8842_adapter *adapter, u16 bank,
193 int offset)
194{
195 ks8842_select_bank(adapter, bank);
196 return ioread32(adapter->hw_addr + offset);
197}
198
199static void ks8842_reset(struct ks8842_adapter *adapter)
200{
David J. Choi28bd6202010-07-13 10:09:19 -0700201 if (adapter->conf_flags & MICREL_KS884X) {
202 ks8842_write16(adapter, 3, 1, REG_GRR);
203 msleep(10);
204 iowrite16(0, adapter->hw_addr + REG_GRR);
205 } else {
206 /* The KS8842 goes haywire when doing softare reset
207 * a work around in the timberdale IP is implemented to
208 * do a hardware reset instead
209 ks8842_write16(adapter, 3, 1, REG_GRR);
210 msleep(10);
211 iowrite16(0, adapter->hw_addr + REG_GRR);
212 */
213 iowrite32(0x1, adapter->hw_addr + REG_TIMB_RST);
214 msleep(20);
215 }
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000216}
217
218static void ks8842_update_link_status(struct net_device *netdev,
219 struct ks8842_adapter *adapter)
220{
221 /* check the status of the link */
222 if (ks8842_read16(adapter, 45, REG_P1MBSR) & 0x4) {
223 netif_carrier_on(netdev);
224 netif_wake_queue(netdev);
225 } else {
226 netif_stop_queue(netdev);
227 netif_carrier_off(netdev);
228 }
229}
230
231static void ks8842_enable_tx(struct ks8842_adapter *adapter)
232{
233 ks8842_enable_bits(adapter, 16, 0x01, REG_TXCR);
234}
235
236static void ks8842_disable_tx(struct ks8842_adapter *adapter)
237{
238 ks8842_clear_bits(adapter, 16, 0x01, REG_TXCR);
239}
240
241static void ks8842_enable_rx(struct ks8842_adapter *adapter)
242{
243 ks8842_enable_bits(adapter, 16, 0x01, REG_RXCR);
244}
245
246static void ks8842_disable_rx(struct ks8842_adapter *adapter)
247{
248 ks8842_clear_bits(adapter, 16, 0x01, REG_RXCR);
249}
250
251static void ks8842_reset_hw(struct ks8842_adapter *adapter)
252{
253 /* reset the HW */
254 ks8842_reset(adapter);
255
256 /* Enable QMU Transmit flow control / transmit padding / Transmit CRC */
257 ks8842_write16(adapter, 16, 0x000E, REG_TXCR);
258
259 /* enable the receiver, uni + multi + broadcast + flow ctrl
260 + crc strip */
261 ks8842_write16(adapter, 16, 0x8 | 0x20 | 0x40 | 0x80 | 0x400,
262 REG_RXCR);
263
264 /* TX frame pointer autoincrement */
265 ks8842_write16(adapter, 17, 0x4000, REG_TXFDPR);
266
267 /* RX frame pointer autoincrement */
268 ks8842_write16(adapter, 17, 0x4000, REG_RXFDPR);
269
270 /* RX 2 kb high watermark */
271 ks8842_write16(adapter, 0, 0x1000, REG_QRFCR);
272
273 /* aggresive back off in half duplex */
274 ks8842_enable_bits(adapter, 32, 1 << 8, REG_SGCR1);
275
276 /* enable no excessive collison drop */
277 ks8842_enable_bits(adapter, 32, 1 << 3, REG_SGCR2);
278
279 /* Enable port 1 force flow control / back pressure / transmit / recv */
280 ks8842_write16(adapter, 48, 0x1E07, REG_P1CR2);
281
282 /* restart port auto-negotiation */
283 ks8842_enable_bits(adapter, 49, 1 << 13, REG_P1CR4);
David J. Choi28bd6202010-07-13 10:09:19 -0700284
285 if (!(adapter->conf_flags & MICREL_KS884X))
286 /* only advertise 10Mbps */
287 ks8842_clear_bits(adapter, 49, 3 << 2, REG_P1CR4);
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000288
289 /* Enable the transmitter */
290 ks8842_enable_tx(adapter);
291
292 /* Enable the receiver */
293 ks8842_enable_rx(adapter);
294
295 /* clear all interrupts */
296 ks8842_write16(adapter, 18, 0xffff, REG_ISR);
297
298 /* enable interrupts */
299 ks8842_write16(adapter, 18, ENABLED_IRQS, REG_IER);
300
301 /* enable the switch */
302 ks8842_write16(adapter, 32, 0x1, REG_SW_ID_AND_ENABLE);
303}
304
305static void ks8842_read_mac_addr(struct ks8842_adapter *adapter, u8 *dest)
306{
307 int i;
308 u16 mac;
309
310 for (i = 0; i < ETH_ALEN; i++)
311 dest[ETH_ALEN - i - 1] = ks8842_read8(adapter, 2, REG_MARL + i);
312
David J. Choi28bd6202010-07-13 10:09:19 -0700313 if (adapter->conf_flags & MICREL_KS884X) {
314 /*
315 the sequence of saving mac addr between MAC and Switch is
316 different.
317 */
318
319 mac = ks8842_read16(adapter, 2, REG_MARL);
320 ks8842_write16(adapter, 39, mac, REG_MACAR3);
321 mac = ks8842_read16(adapter, 2, REG_MARM);
322 ks8842_write16(adapter, 39, mac, REG_MACAR2);
323 mac = ks8842_read16(adapter, 2, REG_MARH);
324 ks8842_write16(adapter, 39, mac, REG_MACAR1);
325 } else {
326
327 /* make sure the switch port uses the same MAC as the QMU */
328 mac = ks8842_read16(adapter, 2, REG_MARL);
329 ks8842_write16(adapter, 39, mac, REG_MACAR1);
330 mac = ks8842_read16(adapter, 2, REG_MARM);
331 ks8842_write16(adapter, 39, mac, REG_MACAR2);
332 mac = ks8842_read16(adapter, 2, REG_MARH);
333 ks8842_write16(adapter, 39, mac, REG_MACAR3);
334 }
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000335}
336
Richard Röjforsa1aa8822010-04-21 16:33:29 -0700337static void ks8842_write_mac_addr(struct ks8842_adapter *adapter, u8 *mac)
338{
339 unsigned long flags;
340 unsigned i;
341
342 spin_lock_irqsave(&adapter->lock, flags);
343 for (i = 0; i < ETH_ALEN; i++) {
344 ks8842_write8(adapter, 2, mac[ETH_ALEN - i - 1], REG_MARL + i);
David J. Choi28bd6202010-07-13 10:09:19 -0700345 if (!(adapter->conf_flags & MICREL_KS884X))
346 ks8842_write8(adapter, 39, mac[ETH_ALEN - i - 1],
347 REG_MACAR1 + i);
348 }
349
350 if (adapter->conf_flags & MICREL_KS884X) {
351 /*
352 the sequence of saving mac addr between MAC and Switch is
353 different.
354 */
355
356 u16 mac;
357
358 mac = ks8842_read16(adapter, 2, REG_MARL);
359 ks8842_write16(adapter, 39, mac, REG_MACAR3);
360 mac = ks8842_read16(adapter, 2, REG_MARM);
361 ks8842_write16(adapter, 39, mac, REG_MACAR2);
362 mac = ks8842_read16(adapter, 2, REG_MARH);
363 ks8842_write16(adapter, 39, mac, REG_MACAR1);
Richard Röjforsa1aa8822010-04-21 16:33:29 -0700364 }
365 spin_unlock_irqrestore(&adapter->lock, flags);
366}
367
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000368static inline u16 ks8842_tx_fifo_space(struct ks8842_adapter *adapter)
369{
370 return ks8842_read16(adapter, 16, REG_TXMIR) & 0x1fff;
371}
372
373static int ks8842_tx_frame(struct sk_buff *skb, struct net_device *netdev)
374{
375 struct ks8842_adapter *adapter = netdev_priv(netdev);
376 int len = skb->len;
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000377
Richard Röjforsa99db192010-07-04 13:26:39 +0000378 netdev_dbg(netdev, "%s: len %u head %p data %p tail %p end %p\n",
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000379 __func__, skb->len, skb->head, skb->data,
380 skb_tail_pointer(skb), skb_end_pointer(skb));
381
382 /* check FIFO buffer space, we need space for CRC and command bits */
383 if (ks8842_tx_fifo_space(adapter) < len + 8)
384 return NETDEV_TX_BUSY;
385
David J. Choi28bd6202010-07-13 10:09:19 -0700386 if (adapter->conf_flags & KS884X_16BIT) {
387 u16 *ptr16 = (u16 *)skb->data;
388 ks8842_write16(adapter, 17, 0x8000 | 0x100, REG_QMU_DATA_LO);
389 ks8842_write16(adapter, 17, (u16)len, REG_QMU_DATA_HI);
390 netdev->stats.tx_bytes += len;
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000391
David J. Choi28bd6202010-07-13 10:09:19 -0700392 /* copy buffer */
393 while (len > 0) {
394 iowrite16(*ptr16++, adapter->hw_addr + REG_QMU_DATA_LO);
395 iowrite16(*ptr16++, adapter->hw_addr + REG_QMU_DATA_HI);
396 len -= sizeof(u32);
397 }
398 } else {
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000399
David J. Choi28bd6202010-07-13 10:09:19 -0700400 u32 *ptr = (u32 *)skb->data;
401 u32 ctrl;
402 /* the control word, enable IRQ, port 1 and the length */
403 ctrl = 0x8000 | 0x100 | (len << 16);
404 ks8842_write32(adapter, 17, ctrl, REG_QMU_DATA_LO);
405
406 netdev->stats.tx_bytes += len;
407
408 /* copy buffer */
409 while (len > 0) {
410 iowrite32(*ptr, adapter->hw_addr + REG_QMU_DATA_LO);
411 len -= sizeof(u32);
412 ptr++;
413 }
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000414 }
415
416 /* enqueue packet */
417 ks8842_write16(adapter, 17, 1, REG_TXQCR);
418
419 dev_kfree_skb(skb);
420
421 return NETDEV_TX_OK;
422}
423
424static void ks8842_rx_frame(struct net_device *netdev,
425 struct ks8842_adapter *adapter)
426{
David J. Choi28bd6202010-07-13 10:09:19 -0700427 u32 status;
428 int len;
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000429
David J. Choi28bd6202010-07-13 10:09:19 -0700430 if (adapter->conf_flags & KS884X_16BIT) {
David S. Millere2df8b72010-07-19 15:25:04 -0700431 status = ks8842_read16(adapter, 17, REG_QMU_DATA_LO);
432 len = ks8842_read16(adapter, 17, REG_QMU_DATA_HI);
David J. Choi28bd6202010-07-13 10:09:19 -0700433 netdev_dbg(netdev, "%s - rx_data: status: %x\n",
David S. Millere2df8b72010-07-19 15:25:04 -0700434 __func__, status);
David J. Choi28bd6202010-07-13 10:09:19 -0700435 } else {
436 status = ks8842_read32(adapter, 17, REG_QMU_DATA_LO);
437 len = (status >> 16) & 0x7ff;
438 status &= 0xffff;
439 netdev_dbg(netdev, "%s - rx_data: status: %x\n",
440 __func__, status);
441 }
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000442
443 /* check the status */
444 if ((status & RXSR_VALID) && !(status & RXSR_ERROR)) {
Eric Dumazet89d71a62009-10-13 05:34:20 +0000445 struct sk_buff *skb = netdev_alloc_skb_ip_align(netdev, len);
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000446
Richard Röjforsa99db192010-07-04 13:26:39 +0000447 netdev_dbg(netdev, "%s, got package, len: %d\n", __func__, len);
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000448 if (skb) {
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000449
450 netdev->stats.rx_packets++;
451 netdev->stats.rx_bytes += len;
452 if (status & RXSR_MULTICAST)
453 netdev->stats.multicast++;
454
David J. Choi28bd6202010-07-13 10:09:19 -0700455 if (adapter->conf_flags & KS884X_16BIT) {
456 u16 *data16 = (u16 *)skb_put(skb, len);
457 ks8842_select_bank(adapter, 17);
458 while (len > 0) {
459 *data16++ = ioread16(adapter->hw_addr +
460 REG_QMU_DATA_LO);
461 *data16++ = ioread16(adapter->hw_addr +
462 REG_QMU_DATA_HI);
463 len -= sizeof(u32);
464 }
465 } else {
466 u32 *data = (u32 *)skb_put(skb, len);
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000467
David J. Choi28bd6202010-07-13 10:09:19 -0700468 ks8842_select_bank(adapter, 17);
469 while (len > 0) {
470 *data++ = ioread32(adapter->hw_addr +
471 REG_QMU_DATA_LO);
472 len -= sizeof(u32);
473 }
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000474 }
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000475 skb->protocol = eth_type_trans(skb, netdev);
476 netif_rx(skb);
477 } else
478 netdev->stats.rx_dropped++;
479 } else {
Richard Röjforsa99db192010-07-04 13:26:39 +0000480 netdev_dbg(netdev, "RX error, status: %x\n", status);
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000481 netdev->stats.rx_errors++;
482 if (status & RXSR_TOO_LONG)
483 netdev->stats.rx_length_errors++;
484 if (status & RXSR_CRC_ERROR)
485 netdev->stats.rx_crc_errors++;
486 if (status & RXSR_RUNT)
487 netdev->stats.rx_frame_errors++;
488 }
489
490 /* set high watermark to 3K */
491 ks8842_clear_bits(adapter, 0, 1 << 12, REG_QRFCR);
492
493 /* release the frame */
494 ks8842_write16(adapter, 17, 0x01, REG_RXQCR);
495
496 /* set high watermark to 2K */
497 ks8842_enable_bits(adapter, 0, 1 << 12, REG_QRFCR);
498}
499
500void ks8842_handle_rx(struct net_device *netdev, struct ks8842_adapter *adapter)
501{
502 u16 rx_data = ks8842_read16(adapter, 16, REG_RXMIR) & 0x1fff;
Richard Röjforsa99db192010-07-04 13:26:39 +0000503 netdev_dbg(netdev, "%s Entry - rx_data: %d\n", __func__, rx_data);
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000504 while (rx_data) {
505 ks8842_rx_frame(netdev, adapter);
506 rx_data = ks8842_read16(adapter, 16, REG_RXMIR) & 0x1fff;
507 }
508}
509
510void ks8842_handle_tx(struct net_device *netdev, struct ks8842_adapter *adapter)
511{
512 u16 sr = ks8842_read16(adapter, 16, REG_TXSR);
Richard Röjforsa99db192010-07-04 13:26:39 +0000513 netdev_dbg(netdev, "%s - entry, sr: %x\n", __func__, sr);
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000514 netdev->stats.tx_packets++;
515 if (netif_queue_stopped(netdev))
516 netif_wake_queue(netdev);
517}
518
519void ks8842_handle_rx_overrun(struct net_device *netdev,
520 struct ks8842_adapter *adapter)
521{
Richard Röjforsa99db192010-07-04 13:26:39 +0000522 netdev_dbg(netdev, "%s: entry\n", __func__);
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000523 netdev->stats.rx_errors++;
524 netdev->stats.rx_fifo_errors++;
525}
526
527void ks8842_tasklet(unsigned long arg)
528{
529 struct net_device *netdev = (struct net_device *)arg;
530 struct ks8842_adapter *adapter = netdev_priv(netdev);
531 u16 isr;
532 unsigned long flags;
533 u16 entry_bank;
534
535 /* read current bank to be able to set it back */
536 spin_lock_irqsave(&adapter->lock, flags);
537 entry_bank = ioread16(adapter->hw_addr + REG_SELECT_BANK);
538 spin_unlock_irqrestore(&adapter->lock, flags);
539
540 isr = ks8842_read16(adapter, 18, REG_ISR);
Richard Röjforsa99db192010-07-04 13:26:39 +0000541 netdev_dbg(netdev, "%s - ISR: 0x%x\n", __func__, isr);
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000542
543 /* Ack */
544 ks8842_write16(adapter, 18, isr, REG_ISR);
545
546 if (!netif_running(netdev))
547 return;
548
549 if (isr & IRQ_LINK_CHANGE)
550 ks8842_update_link_status(netdev, adapter);
551
552 if (isr & (IRQ_RX | IRQ_RX_ERROR))
553 ks8842_handle_rx(netdev, adapter);
554
555 if (isr & IRQ_TX)
556 ks8842_handle_tx(netdev, adapter);
557
558 if (isr & IRQ_RX_OVERRUN)
559 ks8842_handle_rx_overrun(netdev, adapter);
560
561 if (isr & IRQ_TX_STOPPED) {
562 ks8842_disable_tx(adapter);
563 ks8842_enable_tx(adapter);
564 }
565
566 if (isr & IRQ_RX_STOPPED) {
567 ks8842_disable_rx(adapter);
568 ks8842_enable_rx(adapter);
569 }
570
571 /* re-enable interrupts, put back the bank selection register */
572 spin_lock_irqsave(&adapter->lock, flags);
573 ks8842_write16(adapter, 18, ENABLED_IRQS, REG_IER);
574 iowrite16(entry_bank, adapter->hw_addr + REG_SELECT_BANK);
575 spin_unlock_irqrestore(&adapter->lock, flags);
576}
577
578static irqreturn_t ks8842_irq(int irq, void *devid)
579{
Richard Röjforsa99db192010-07-04 13:26:39 +0000580 struct net_device *netdev = devid;
581 struct ks8842_adapter *adapter = netdev_priv(netdev);
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000582 u16 isr;
583 u16 entry_bank = ioread16(adapter->hw_addr + REG_SELECT_BANK);
584 irqreturn_t ret = IRQ_NONE;
585
586 isr = ks8842_read16(adapter, 18, REG_ISR);
Richard Röjforsa99db192010-07-04 13:26:39 +0000587 netdev_dbg(netdev, "%s - ISR: 0x%x\n", __func__, isr);
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000588
589 if (isr) {
590 /* disable IRQ */
591 ks8842_write16(adapter, 18, 0x00, REG_IER);
592
593 /* schedule tasklet */
594 tasklet_schedule(&adapter->tasklet);
595
596 ret = IRQ_HANDLED;
597 }
598
599 iowrite16(entry_bank, adapter->hw_addr + REG_SELECT_BANK);
600
601 return ret;
602}
603
604
605/* Netdevice operations */
606
607static int ks8842_open(struct net_device *netdev)
608{
609 struct ks8842_adapter *adapter = netdev_priv(netdev);
610 int err;
611
Richard Röjforsa99db192010-07-04 13:26:39 +0000612 netdev_dbg(netdev, "%s - entry\n", __func__);
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000613
614 /* reset the HW */
615 ks8842_reset_hw(adapter);
616
Richard Röjforsa1aa8822010-04-21 16:33:29 -0700617 ks8842_write_mac_addr(adapter, netdev->dev_addr);
618
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000619 ks8842_update_link_status(netdev, adapter);
620
621 err = request_irq(adapter->irq, ks8842_irq, IRQF_SHARED, DRV_NAME,
Richard Röjforsa99db192010-07-04 13:26:39 +0000622 netdev);
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000623 if (err) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000624 pr_err("Failed to request IRQ: %d: %d\n", adapter->irq, err);
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000625 return err;
626 }
627
628 return 0;
629}
630
631static int ks8842_close(struct net_device *netdev)
632{
633 struct ks8842_adapter *adapter = netdev_priv(netdev);
634
Richard Röjforsa99db192010-07-04 13:26:39 +0000635 netdev_dbg(netdev, "%s - entry\n", __func__);
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000636
Richard Röjforscc88e452010-07-08 23:36:30 -0700637 cancel_work_sync(&adapter->timeout_work);
638
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000639 /* free the irq */
Richard Röjforsa99db192010-07-04 13:26:39 +0000640 free_irq(adapter->irq, netdev);
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000641
642 /* disable the switch */
643 ks8842_write16(adapter, 32, 0x0, REG_SW_ID_AND_ENABLE);
644
645 return 0;
646}
647
Stephen Hemminger613573252009-08-31 19:50:58 +0000648static netdev_tx_t ks8842_xmit_frame(struct sk_buff *skb,
649 struct net_device *netdev)
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000650{
651 int ret;
652 struct ks8842_adapter *adapter = netdev_priv(netdev);
653
Richard Röjforsa99db192010-07-04 13:26:39 +0000654 netdev_dbg(netdev, "%s: entry\n", __func__);
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000655
656 ret = ks8842_tx_frame(skb, netdev);
657
658 if (ks8842_tx_fifo_space(adapter) < netdev->mtu + 8)
659 netif_stop_queue(netdev);
660
661 return ret;
662}
663
664static int ks8842_set_mac(struct net_device *netdev, void *p)
665{
666 struct ks8842_adapter *adapter = netdev_priv(netdev);
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000667 struct sockaddr *addr = p;
668 char *mac = (u8 *)addr->sa_data;
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000669
Richard Röjforsa99db192010-07-04 13:26:39 +0000670 netdev_dbg(netdev, "%s: entry\n", __func__);
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000671
672 if (!is_valid_ether_addr(addr->sa_data))
673 return -EADDRNOTAVAIL;
674
675 memcpy(netdev->dev_addr, mac, netdev->addr_len);
676
Richard Röjforsa1aa8822010-04-21 16:33:29 -0700677 ks8842_write_mac_addr(adapter, mac);
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000678 return 0;
679}
680
Richard Röjforscc88e452010-07-08 23:36:30 -0700681static void ks8842_tx_timeout_work(struct work_struct *work)
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000682{
Richard Röjforscc88e452010-07-08 23:36:30 -0700683 struct ks8842_adapter *adapter =
684 container_of(work, struct ks8842_adapter, timeout_work);
685 struct net_device *netdev = adapter->netdev;
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000686 unsigned long flags;
687
Richard Röjforsa99db192010-07-04 13:26:39 +0000688 netdev_dbg(netdev, "%s: entry\n", __func__);
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000689
690 spin_lock_irqsave(&adapter->lock, flags);
691 /* disable interrupts */
692 ks8842_write16(adapter, 18, 0, REG_IER);
693 ks8842_write16(adapter, 18, 0xFFFF, REG_ISR);
Richard Röjforscc88e452010-07-08 23:36:30 -0700694
695 netif_stop_queue(netdev);
696
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000697 spin_unlock_irqrestore(&adapter->lock, flags);
698
699 ks8842_reset_hw(adapter);
700
Richard Röjforsa1aa8822010-04-21 16:33:29 -0700701 ks8842_write_mac_addr(adapter, netdev->dev_addr);
702
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000703 ks8842_update_link_status(netdev, adapter);
704}
705
Richard Röjforscc88e452010-07-08 23:36:30 -0700706static void ks8842_tx_timeout(struct net_device *netdev)
707{
708 struct ks8842_adapter *adapter = netdev_priv(netdev);
709
710 netdev_dbg(netdev, "%s: entry\n", __func__);
711
712 schedule_work(&adapter->timeout_work);
713}
714
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000715static const struct net_device_ops ks8842_netdev_ops = {
716 .ndo_open = ks8842_open,
717 .ndo_stop = ks8842_close,
718 .ndo_start_xmit = ks8842_xmit_frame,
719 .ndo_set_mac_address = ks8842_set_mac,
720 .ndo_tx_timeout = ks8842_tx_timeout,
721 .ndo_validate_addr = eth_validate_addr
722};
723
Stephen Hemminger0fc0b732009-09-02 01:03:33 -0700724static const struct ethtool_ops ks8842_ethtool_ops = {
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000725 .get_link = ethtool_op_get_link,
726};
727
728static int __devinit ks8842_probe(struct platform_device *pdev)
729{
730 int err = -ENOMEM;
731 struct resource *iomem;
732 struct net_device *netdev;
733 struct ks8842_adapter *adapter;
Richard Röjforsa1aa8822010-04-21 16:33:29 -0700734 struct ks8842_platform_data *pdata = pdev->dev.platform_data;
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000735 u16 id;
Richard Röjforsa1aa8822010-04-21 16:33:29 -0700736 unsigned i;
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000737
738 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
739 if (!request_mem_region(iomem->start, resource_size(iomem), DRV_NAME))
740 goto err_mem_region;
741
742 netdev = alloc_etherdev(sizeof(struct ks8842_adapter));
743 if (!netdev)
744 goto err_alloc_etherdev;
745
746 SET_NETDEV_DEV(netdev, &pdev->dev);
747
748 adapter = netdev_priv(netdev);
Richard Röjforscc88e452010-07-08 23:36:30 -0700749 adapter->netdev = netdev;
750 INIT_WORK(&adapter->timeout_work, ks8842_tx_timeout_work);
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000751 adapter->hw_addr = ioremap(iomem->start, resource_size(iomem));
David J. Choi28bd6202010-07-13 10:09:19 -0700752 adapter->conf_flags = iomem->flags;
753
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000754 if (!adapter->hw_addr)
755 goto err_ioremap;
756
757 adapter->irq = platform_get_irq(pdev, 0);
758 if (adapter->irq < 0) {
759 err = adapter->irq;
760 goto err_get_irq;
761 }
762
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000763 tasklet_init(&adapter->tasklet, ks8842_tasklet, (unsigned long)netdev);
764 spin_lock_init(&adapter->lock);
765
766 netdev->netdev_ops = &ks8842_netdev_ops;
767 netdev->ethtool_ops = &ks8842_ethtool_ops;
768
Richard Röjforsa1aa8822010-04-21 16:33:29 -0700769 /* Check if a mac address was given */
770 i = netdev->addr_len;
771 if (pdata) {
772 for (i = 0; i < netdev->addr_len; i++)
773 if (pdata->macaddr[i] != 0)
774 break;
775
776 if (i < netdev->addr_len)
777 /* an address was passed, use it */
778 memcpy(netdev->dev_addr, pdata->macaddr,
779 netdev->addr_len);
780 }
781
782 if (i == netdev->addr_len) {
783 ks8842_read_mac_addr(adapter, netdev->dev_addr);
784
785 if (!is_valid_ether_addr(netdev->dev_addr))
786 random_ether_addr(netdev->dev_addr);
787 }
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000788
789 id = ks8842_read16(adapter, 32, REG_SW_ID_AND_ENABLE);
790
791 strcpy(netdev->name, "eth%d");
792 err = register_netdev(netdev);
793 if (err)
794 goto err_register;
795
796 platform_set_drvdata(pdev, netdev);
797
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000798 pr_info("Found chip, family: 0x%x, id: 0x%x, rev: 0x%x\n",
Richard Ršöjforsb07878e2009-06-04 03:35:55 +0000799 (id >> 8) & 0xff, (id >> 4) & 0xf, (id >> 1) & 0x7);
800
801 return 0;
802
803err_register:
804err_get_irq:
805 iounmap(adapter->hw_addr);
806err_ioremap:
807 free_netdev(netdev);
808err_alloc_etherdev:
809 release_mem_region(iomem->start, resource_size(iomem));
810err_mem_region:
811 return err;
812}
813
814static int __devexit ks8842_remove(struct platform_device *pdev)
815{
816 struct net_device *netdev = platform_get_drvdata(pdev);
817 struct ks8842_adapter *adapter = netdev_priv(netdev);
818 struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
819
820 unregister_netdev(netdev);
821 tasklet_kill(&adapter->tasklet);
822 iounmap(adapter->hw_addr);
823 free_netdev(netdev);
824 release_mem_region(iomem->start, resource_size(iomem));
825 platform_set_drvdata(pdev, NULL);
826 return 0;
827}
828
829
830static struct platform_driver ks8842_platform_driver = {
831 .driver = {
832 .name = DRV_NAME,
833 .owner = THIS_MODULE,
834 },
835 .probe = ks8842_probe,
836 .remove = ks8842_remove,
837};
838
839static int __init ks8842_init(void)
840{
841 return platform_driver_register(&ks8842_platform_driver);
842}
843
844static void __exit ks8842_exit(void)
845{
846 platform_driver_unregister(&ks8842_platform_driver);
847}
848
849module_init(ks8842_init);
850module_exit(ks8842_exit);
851
852MODULE_DESCRIPTION("Timberdale KS8842 ethernet driver");
853MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>");
854MODULE_LICENSE("GPL v2");
855MODULE_ALIAS("platform:ks8842");
856