blob: e5c8412c08c1da3633f687f6ecd716d3ea057591 [file] [log] [blame]
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001/**
Paul Gortmaker3396c782012-01-27 13:36:01 +00002 * drivers/net/ethernet/micrel/ks8851_mll.c
Choi, Davida55c0a0e2009-09-25 14:42:12 +00003 * Copyright (c) 2009 Micrel Inc.
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
Ben Hutchings1aa8b472012-07-10 10:56:59 +000019/* Supports:
Choi, Davida55c0a0e2009-09-25 14:42:12 +000020 * KS8851 16bit MLL chip from Micrel Inc.
21 */
22
Joe Perches0dc7d2b2010-02-27 14:43:51 +000023#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000025#include <linux/interrupt.h>
Choi, Davida55c0a0e2009-09-25 14:42:12 +000026#include <linux/module.h>
27#include <linux/kernel.h>
28#include <linux/netdevice.h>
29#include <linux/etherdevice.h>
30#include <linux/ethtool.h>
31#include <linux/cache.h>
32#include <linux/crc32.h>
Krzysztof Kozlowski5d258b42018-07-17 18:05:39 +020033#include <linux/crc32poly.h>
Choi, Davida55c0a0e2009-09-25 14:42:12 +000034#include <linux/mii.h>
35#include <linux/platform_device.h>
36#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Raffaele Recalcati29a6b6c2012-06-03 10:43:43 +000038#include <linux/ks8851_mll.h>
Jean-Christophe PLAGNIOL-VILLARD87227b82013-05-23 23:01:22 +000039#include <linux/of.h>
40#include <linux/of_device.h>
41#include <linux/of_net.h>
Choi, Davida55c0a0e2009-09-25 14:42:12 +000042
Lukas Wunneraae079a2019-03-20 15:02:00 +010043#include "ks8851.h"
44
Choi, Davida55c0a0e2009-09-25 14:42:12 +000045#define DRV_NAME "ks8851_mll"
46
47static u8 KS_DEFAULT_MAC_ADDRESS[] = { 0x00, 0x10, 0xA1, 0x86, 0x95, 0x11 };
Davide Ciminaghi8a9a0ea2012-04-13 04:48:25 +000048#define MAX_RECV_FRAMES 255
Choi, Davida55c0a0e2009-09-25 14:42:12 +000049#define MAX_BUF_SIZE 2048
50#define TX_BUF_SIZE 2000
51#define RX_BUF_SIZE 2000
52
Choi, Davida55c0a0e2009-09-25 14:42:12 +000053#define RXCR1_FILTER_MASK (RXCR1_RXINVF | RXCR1_RXAE | \
54 RXCR1_RXMAFMA | RXCR1_RXPAFMA)
Choi, Davida55c0a0e2009-09-25 14:42:12 +000055#define RXQCR_CMD_CNTL (RXQCR_RXFCTE|RXQCR_ADRFE)
56
Choi, Davida55c0a0e2009-09-25 14:42:12 +000057#define ENUM_BUS_NONE 0
58#define ENUM_BUS_8BIT 1
59#define ENUM_BUS_16BIT 2
60#define ENUM_BUS_32BIT 3
61
62#define MAX_MCAST_LST 32
63#define HW_MCAST_SIZE 8
Choi, Davida55c0a0e2009-09-25 14:42:12 +000064
65/**
66 * union ks_tx_hdr - tx header data
67 * @txb: The header as bytes
68 * @txw: The header as 16bit, little-endian words
69 *
70 * A dual representation of the tx header data to allow
71 * access to individual bytes, and to allow 16bit accesses
72 * with 16bit alignment.
73 */
74union ks_tx_hdr {
75 u8 txb[4];
76 __le16 txw[2];
77};
78
79/**
80 * struct ks_net - KS8851 driver private data
81 * @net_device : The network device we're bound to
82 * @hw_addr : start address of data register.
83 * @hw_addr_cmd : start address of command register.
84 * @txh : temporaly buffer to save status/length.
85 * @lock : Lock to ensure that the device is not accessed when busy.
86 * @pdev : Pointer to platform device.
87 * @mii : The MII state information for the mii calls.
88 * @frame_head_info : frame header information for multi-pkt rx.
89 * @statelock : Lock on this structure for tx list.
90 * @msg_enable : The message flags controlling driver output (see ethtool).
91 * @frame_cnt : number of frames received.
92 * @bus_width : i/o bus width.
Choi, Davida55c0a0e2009-09-25 14:42:12 +000093 * @rc_rxqcr : Cached copy of KS_RXQCR.
94 * @rc_txcr : Cached copy of KS_TXCR.
95 * @rc_ier : Cached copy of KS_IER.
96 * @sharedbus : Multipex(addr and data bus) mode indicator.
97 * @cmd_reg_cache : command register cached.
98 * @cmd_reg_cache_int : command register cached. Used in the irq handler.
99 * @promiscuous : promiscuous mode indicator.
100 * @all_mcast : mutlicast indicator.
101 * @mcast_lst_size : size of multicast list.
102 * @mcast_lst : multicast list.
103 * @mcast_bits : multicast enabed.
104 * @mac_addr : MAC address assigned to this device.
105 * @fid : frame id.
106 * @extra_byte : number of extra byte prepended rx pkt.
107 * @enabled : indicator this device works.
108 *
109 * The @lock ensures that the chip is protected when certain operations are
110 * in progress. When the read or write packet transfer is in progress, most
111 * of the chip registers are not accessible until the transfer is finished and
112 * the DMA has been de-asserted.
113 *
114 * The @statelock is used to protect information in the structure which may
115 * need to be accessed via several sources, such as the network driver layer
116 * or one of the work queues.
117 *
118 */
119
120/* Receive multiplex framer header info */
121struct type_frame_head {
122 u16 sts; /* Frame status */
123 u16 len; /* Byte count */
124};
125
126struct ks_net {
127 struct net_device *netdev;
128 void __iomem *hw_addr;
129 void __iomem *hw_addr_cmd;
130 union ks_tx_hdr txh ____cacheline_aligned;
131 struct mutex lock; /* spinlock to be interrupt safe */
132 struct platform_device *pdev;
133 struct mii_if_info mii;
134 struct type_frame_head *frame_head_info;
135 spinlock_t statelock;
136 u32 msg_enable;
137 u32 frame_cnt;
138 int bus_width;
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000139
140 u16 rc_rxqcr;
141 u16 rc_txcr;
142 u16 rc_ier;
143 u16 sharedbus;
144 u16 cmd_reg_cache;
145 u16 cmd_reg_cache_int;
146 u16 promiscuous;
147 u16 all_mcast;
148 u16 mcast_lst_size;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000149 u8 mcast_lst[MAX_MCAST_LST][ETH_ALEN];
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000150 u8 mcast_bits[HW_MCAST_SIZE];
151 u8 mac_addr[6];
152 u8 fid;
153 u8 extra_byte;
154 u8 enabled;
155};
156
157static int msg_enable;
158
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000159#define BE3 0x8000 /* Byte Enable 3 */
160#define BE2 0x4000 /* Byte Enable 2 */
161#define BE1 0x2000 /* Byte Enable 1 */
162#define BE0 0x1000 /* Byte Enable 0 */
163
Ben Hutchings1aa8b472012-07-10 10:56:59 +0000164/* register read/write calls.
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000165 *
166 * All these calls issue transactions to access the chip's registers. They
167 * all require that the necessary lock is held to prevent accesses when the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300168 * chip is busy transferring packet data (RX/TX FIFO accesses).
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000169 */
170
171/**
172 * ks_rdreg8 - read 8 bit register from device
173 * @ks : The chip information
174 * @offset: The register address
175 *
176 * Read a 8bit register from the chip, returning the result
177 */
178static u8 ks_rdreg8(struct ks_net *ks, int offset)
179{
180 u16 data;
181 u8 shift_bit = offset & 0x03;
182 u8 shift_data = (offset & 1) << 3;
183 ks->cmd_reg_cache = (u16) offset | (u16)(BE0 << shift_bit);
184 iowrite16(ks->cmd_reg_cache, ks->hw_addr_cmd);
185 data = ioread16(ks->hw_addr);
186 return (u8)(data >> shift_data);
187}
188
189/**
190 * ks_rdreg16 - read 16 bit register from device
191 * @ks : The chip information
192 * @offset: The register address
193 *
194 * Read a 16bit register from the chip, returning the result
195 */
196
197static u16 ks_rdreg16(struct ks_net *ks, int offset)
198{
199 ks->cmd_reg_cache = (u16)offset | ((BE1 | BE0) << (offset & 0x02));
200 iowrite16(ks->cmd_reg_cache, ks->hw_addr_cmd);
201 return ioread16(ks->hw_addr);
202}
203
204/**
205 * ks_wrreg8 - write 8bit register value to chip
206 * @ks: The chip information
207 * @offset: The register address
208 * @value: The value to write
209 *
210 */
211static void ks_wrreg8(struct ks_net *ks, int offset, u8 value)
212{
213 u8 shift_bit = (offset & 0x03);
214 u16 value_write = (u16)(value << ((offset & 1) << 3));
215 ks->cmd_reg_cache = (u16)offset | (BE0 << shift_bit);
216 iowrite16(ks->cmd_reg_cache, ks->hw_addr_cmd);
217 iowrite16(value_write, ks->hw_addr);
218}
219
220/**
221 * ks_wrreg16 - write 16bit register value to chip
222 * @ks: The chip information
223 * @offset: The register address
224 * @value: The value to write
225 *
226 */
227
228static void ks_wrreg16(struct ks_net *ks, int offset, u16 value)
229{
230 ks->cmd_reg_cache = (u16)offset | ((BE1 | BE0) << (offset & 0x02));
231 iowrite16(ks->cmd_reg_cache, ks->hw_addr_cmd);
232 iowrite16(value, ks->hw_addr);
233}
234
235/**
236 * ks_inblk - read a block of data from QMU. This is called after sudo DMA mode enabled.
237 * @ks: The chip state
238 * @wptr: buffer address to save data
239 * @len: length in byte to read
240 *
241 */
242static inline void ks_inblk(struct ks_net *ks, u16 *wptr, u32 len)
243{
244 len >>= 1;
245 while (len--)
246 *wptr++ = (u16)ioread16(ks->hw_addr);
247}
248
249/**
250 * ks_outblk - write data to QMU. This is called after sudo DMA mode enabled.
251 * @ks: The chip information
252 * @wptr: buffer address
253 * @len: length in byte to write
254 *
255 */
256static inline void ks_outblk(struct ks_net *ks, u16 *wptr, u32 len)
257{
258 len >>= 1;
259 while (len--)
260 iowrite16(*wptr++, ks->hw_addr);
261}
262
David J. Choi4a91ca42009-11-19 15:34:30 +0000263static void ks_disable_int(struct ks_net *ks)
264{
265 ks_wrreg16(ks, KS_IER, 0x0000);
266} /* ks_disable_int */
267
268static void ks_enable_int(struct ks_net *ks)
269{
270 ks_wrreg16(ks, KS_IER, ks->rc_ier);
271} /* ks_enable_int */
272
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000273/**
274 * ks_tx_fifo_space - return the available hardware buffer size.
275 * @ks: The chip information
276 *
277 */
278static inline u16 ks_tx_fifo_space(struct ks_net *ks)
279{
280 return ks_rdreg16(ks, KS_TXMIR) & 0x1fff;
281}
282
283/**
284 * ks_save_cmd_reg - save the command register from the cache.
285 * @ks: The chip information
286 *
287 */
288static inline void ks_save_cmd_reg(struct ks_net *ks)
289{
290 /*ks8851 MLL has a bug to read back the command register.
291 * So rely on software to save the content of command register.
292 */
293 ks->cmd_reg_cache_int = ks->cmd_reg_cache;
294}
295
296/**
297 * ks_restore_cmd_reg - restore the command register from the cache and
298 * write to hardware register.
299 * @ks: The chip information
300 *
301 */
302static inline void ks_restore_cmd_reg(struct ks_net *ks)
303{
304 ks->cmd_reg_cache = ks->cmd_reg_cache_int;
305 iowrite16(ks->cmd_reg_cache, ks->hw_addr_cmd);
306}
307
308/**
309 * ks_set_powermode - set power mode of the device
310 * @ks: The chip information
311 * @pwrmode: The power mode value to write to KS_PMECR.
312 *
313 * Change the power mode of the chip.
314 */
315static void ks_set_powermode(struct ks_net *ks, unsigned pwrmode)
316{
317 unsigned pmecr;
318
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000319 netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode);
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000320
321 ks_rdreg16(ks, KS_GRR);
322 pmecr = ks_rdreg16(ks, KS_PMECR);
323 pmecr &= ~PMECR_PM_MASK;
324 pmecr |= pwrmode;
325
326 ks_wrreg16(ks, KS_PMECR, pmecr);
327}
328
329/**
330 * ks_read_config - read chip configuration of bus width.
331 * @ks: The chip information
332 *
333 */
334static void ks_read_config(struct ks_net *ks)
335{
336 u16 reg_data = 0;
337
338 /* Regardless of bus width, 8 bit read should always work.*/
339 reg_data = ks_rdreg8(ks, KS_CCR) & 0x00FF;
340 reg_data |= ks_rdreg8(ks, KS_CCR+1) << 8;
341
342 /* addr/data bus are multiplexed */
343 ks->sharedbus = (reg_data & CCR_SHARED) == CCR_SHARED;
344
345 /* There are garbage data when reading data from QMU,
346 depending on bus-width.
347 */
348
349 if (reg_data & CCR_8BIT) {
350 ks->bus_width = ENUM_BUS_8BIT;
351 ks->extra_byte = 1;
352 } else if (reg_data & CCR_16BIT) {
353 ks->bus_width = ENUM_BUS_16BIT;
354 ks->extra_byte = 2;
355 } else {
356 ks->bus_width = ENUM_BUS_32BIT;
357 ks->extra_byte = 4;
358 }
359}
360
361/**
362 * ks_soft_reset - issue one of the soft reset to the device
363 * @ks: The device state.
364 * @op: The bit(s) to set in the GRR
365 *
366 * Issue the relevant soft-reset command to the device's GRR register
367 * specified by @op.
368 *
369 * Note, the delays are in there as a caution to ensure that the reset
370 * has time to take effect and then complete. Since the datasheet does
371 * not currently specify the exact sequence, we have chosen something
372 * that seems to work with our device.
373 */
374static void ks_soft_reset(struct ks_net *ks, unsigned op)
375{
376 /* Disable interrupt first */
377 ks_wrreg16(ks, KS_IER, 0x0000);
378 ks_wrreg16(ks, KS_GRR, op);
379 mdelay(10); /* wait a short time to effect reset */
380 ks_wrreg16(ks, KS_GRR, 0);
381 mdelay(1); /* wait for condition to clear */
382}
383
384
Jingoo Han51a700d2013-08-05 18:03:00 +0900385static void ks_enable_qmu(struct ks_net *ks)
David J. Choi4a91ca42009-11-19 15:34:30 +0000386{
387 u16 w;
388
389 w = ks_rdreg16(ks, KS_TXCR);
390 /* Enables QMU Transmit (TXCR). */
391 ks_wrreg16(ks, KS_TXCR, w | TXCR_TXE);
392
393 /*
394 * RX Frame Count Threshold Enable and Auto-Dequeue RXQ Frame
395 * Enable
396 */
397
398 w = ks_rdreg16(ks, KS_RXQCR);
399 ks_wrreg16(ks, KS_RXQCR, w | RXQCR_RXFCTE);
400
401 /* Enables QMU Receive (RXCR1). */
402 w = ks_rdreg16(ks, KS_RXCR1);
403 ks_wrreg16(ks, KS_RXCR1, w | RXCR1_RXE);
404 ks->enabled = true;
405} /* ks_enable_qmu */
406
407static void ks_disable_qmu(struct ks_net *ks)
408{
409 u16 w;
410
411 w = ks_rdreg16(ks, KS_TXCR);
412
413 /* Disables QMU Transmit (TXCR). */
414 w &= ~TXCR_TXE;
415 ks_wrreg16(ks, KS_TXCR, w);
416
417 /* Disables QMU Receive (RXCR1). */
418 w = ks_rdreg16(ks, KS_RXCR1);
419 w &= ~RXCR1_RXE ;
420 ks_wrreg16(ks, KS_RXCR1, w);
421
422 ks->enabled = false;
423
424} /* ks_disable_qmu */
425
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000426/**
427 * ks_read_qmu - read 1 pkt data from the QMU.
428 * @ks: The chip information
429 * @buf: buffer address to save 1 pkt
430 * @len: Pkt length
431 * Here is the sequence to read 1 pkt:
432 * 1. set sudo DMA mode
433 * 2. read prepend data
434 * 3. read pkt data
435 * 4. reset sudo DMA Mode
436 */
437static inline void ks_read_qmu(struct ks_net *ks, u16 *buf, u32 len)
438{
439 u32 r = ks->extra_byte & 0x1 ;
440 u32 w = ks->extra_byte - r;
441
442 /* 1. set sudo DMA mode */
443 ks_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI);
444 ks_wrreg8(ks, KS_RXQCR, (ks->rc_rxqcr | RXQCR_SDA) & 0xff);
445
446 /* 2. read prepend data */
447 /**
448 * read 4 + extra bytes and discard them.
449 * extra bytes for dummy, 2 for status, 2 for len
450 */
451
452 /* use likely(r) for 8 bit access for performance */
453 if (unlikely(r))
454 ioread8(ks->hw_addr);
455 ks_inblk(ks, buf, w + 2 + 2);
456
457 /* 3. read pkt data */
458 ks_inblk(ks, buf, ALIGN(len, 4));
459
460 /* 4. reset sudo DMA Mode */
461 ks_wrreg8(ks, KS_RXQCR, ks->rc_rxqcr);
462}
463
464/**
465 * ks_rcv - read multiple pkts data from the QMU.
466 * @ks: The chip information
467 * @netdev: The network device being opened.
468 *
469 * Read all of header information before reading pkt content.
470 * It is not allowed only port of pkts in QMU after issuing
471 * interrupt ack.
472 */
473static void ks_rcv(struct ks_net *ks, struct net_device *netdev)
474{
475 u32 i;
476 struct type_frame_head *frame_hdr = ks->frame_head_info;
477 struct sk_buff *skb;
478
479 ks->frame_cnt = ks_rdreg16(ks, KS_RXFCTR) >> 8;
480
481 /* read all header information */
482 for (i = 0; i < ks->frame_cnt; i++) {
483 /* Checking Received packet status */
484 frame_hdr->sts = ks_rdreg16(ks, KS_RXFHSR);
485 /* Get packet len from hardware */
486 frame_hdr->len = ks_rdreg16(ks, KS_RXFHBCR);
487 frame_hdr++;
488 }
489
490 frame_hdr = ks->frame_head_info;
491 while (ks->frame_cnt--) {
David J. Choic1ad32a2013-03-11 09:22:54 -0700492 if (unlikely(!(frame_hdr->sts & RXFSHR_RXFV) ||
493 frame_hdr->len >= RX_BUF_SIZE ||
494 frame_hdr->len <= 0)) {
495
496 /* discard an invalid packet */
497 ks_wrreg16(ks, KS_RXQCR, (ks->rc_rxqcr | RXQCR_RRXEF));
498 netdev->stats.rx_dropped++;
499 if (!(frame_hdr->sts & RXFSHR_RXFV))
500 netdev->stats.rx_frame_errors++;
501 else
502 netdev->stats.rx_length_errors++;
503 frame_hdr++;
504 continue;
505 }
506
Pradeep A Dalvic056b732012-02-05 02:50:38 +0000507 skb = netdev_alloc_skb(netdev, frame_hdr->len + 16);
David J. Choic1ad32a2013-03-11 09:22:54 -0700508 if (likely(skb)) {
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000509 skb_reserve(skb, 2);
510 /* read data block including CRC 4 bytes */
David J. Choi4a91ca42009-11-19 15:34:30 +0000511 ks_read_qmu(ks, (u16 *)skb->data, frame_hdr->len);
David J. Choic1ad32a2013-03-11 09:22:54 -0700512 skb_put(skb, frame_hdr->len - 4);
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000513 skb->protocol = eth_type_trans(skb, netdev);
514 netif_rx(skb);
David J. Choic1ad32a2013-03-11 09:22:54 -0700515 /* exclude CRC size */
516 netdev->stats.rx_bytes += frame_hdr->len - 4;
517 netdev->stats.rx_packets++;
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000518 } else {
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000519 ks_wrreg16(ks, KS_RXQCR, (ks->rc_rxqcr | RXQCR_RRXEF));
David J. Choic1ad32a2013-03-11 09:22:54 -0700520 netdev->stats.rx_dropped++;
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000521 }
522 frame_hdr++;
523 }
524}
525
526/**
527 * ks_update_link_status - link status update.
528 * @netdev: The network device being opened.
529 * @ks: The chip information
530 *
531 */
532
533static void ks_update_link_status(struct net_device *netdev, struct ks_net *ks)
534{
535 /* check the status of the link */
536 u32 link_up_status;
537 if (ks_rdreg16(ks, KS_P1SR) & P1SR_LINK_GOOD) {
538 netif_carrier_on(netdev);
539 link_up_status = true;
540 } else {
541 netif_carrier_off(netdev);
542 link_up_status = false;
543 }
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000544 netif_dbg(ks, link, ks->netdev,
545 "%s: %s\n", __func__, link_up_status ? "UP" : "DOWN");
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000546}
547
548/**
549 * ks_irq - device interrupt handler
Uwe Kleine-Königfafdbf82012-02-12 11:29:06 +0000550 * @irq: Interrupt number passed from the IRQ handler.
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000551 * @pw: The private word passed to register_irq(), our struct ks_net.
552 *
553 * This is the handler invoked to find out what happened
554 *
555 * Read the interrupt status, work out what needs to be done and then clear
556 * any of the interrupts that are not needed.
557 */
558
559static irqreturn_t ks_irq(int irq, void *pw)
560{
Choi, Davidaeedba82010-01-27 06:03:16 +0000561 struct net_device *netdev = pw;
562 struct ks_net *ks = netdev_priv(netdev);
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000563 u16 status;
564
565 /*this should be the first in IRQ handler */
566 ks_save_cmd_reg(ks);
567
568 status = ks_rdreg16(ks, KS_ISR);
569 if (unlikely(!status)) {
570 ks_restore_cmd_reg(ks);
571 return IRQ_NONE;
572 }
573
574 ks_wrreg16(ks, KS_ISR, status);
575
576 if (likely(status & IRQ_RXI))
577 ks_rcv(ks, netdev);
578
579 if (unlikely(status & IRQ_LCI))
580 ks_update_link_status(netdev, ks);
581
582 if (unlikely(status & IRQ_TXI))
583 netif_wake_queue(netdev);
584
585 if (unlikely(status & IRQ_LDI)) {
586
587 u16 pmecr = ks_rdreg16(ks, KS_PMECR);
588 pmecr &= ~PMECR_WKEVT_MASK;
589 ks_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
590 }
591
David J. Choic1ad32a2013-03-11 09:22:54 -0700592 if (unlikely(status & IRQ_RXOI))
593 ks->netdev->stats.rx_over_errors++;
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000594 /* this should be the last in IRQ handler*/
595 ks_restore_cmd_reg(ks);
596 return IRQ_HANDLED;
597}
598
599
600/**
601 * ks_net_open - open network device
602 * @netdev: The network device being opened.
603 *
604 * Called when the network device is marked active, such as a user executing
605 * 'ifconfig up' on the device.
606 */
607static int ks_net_open(struct net_device *netdev)
608{
609 struct ks_net *ks = netdev_priv(netdev);
610 int err;
611
Michael Opdenackercfb9a512013-09-13 06:10:10 +0200612#define KS_INT_FLAGS IRQF_TRIGGER_LOW
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000613 /* lock the card, even if we may not actually do anything
614 * else at the moment.
615 */
616
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000617 netif_dbg(ks, ifup, ks->netdev, "%s - entry\n", __func__);
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000618
619 /* reset the HW */
Jan Weitzel6c23e412012-02-14 21:35:15 +0000620 err = request_irq(netdev->irq, ks_irq, KS_INT_FLAGS, DRV_NAME, netdev);
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000621
622 if (err) {
Jan Weitzel6c23e412012-02-14 21:35:15 +0000623 pr_err("Failed to request IRQ: %d: %d\n", netdev->irq, err);
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000624 return err;
625 }
626
David J. Choi4a91ca42009-11-19 15:34:30 +0000627 /* wake up powermode to normal mode */
628 ks_set_powermode(ks, PMECR_PM_NORMAL);
629 mdelay(1); /* wait for normal mode to take effect */
630
631 ks_wrreg16(ks, KS_ISR, 0xffff);
632 ks_enable_int(ks);
633 ks_enable_qmu(ks);
634 netif_start_queue(ks->netdev);
635
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000636 netif_dbg(ks, ifup, ks->netdev, "network device up\n");
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000637
638 return 0;
639}
640
641/**
642 * ks_net_stop - close network device
643 * @netdev: The device being closed.
644 *
645 * Called to close down a network device which has been active. Cancell any
646 * work, shutdown the RX and TX process and then place the chip into a low
647 * power state whilst it is not being used.
648 */
649static int ks_net_stop(struct net_device *netdev)
650{
651 struct ks_net *ks = netdev_priv(netdev);
652
Joe Perches0dc7d2b2010-02-27 14:43:51 +0000653 netif_info(ks, ifdown, netdev, "shutting down\n");
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000654
655 netif_stop_queue(netdev);
656
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000657 mutex_lock(&ks->lock);
658
659 /* turn off the IRQs and ack any outstanding */
660 ks_wrreg16(ks, KS_IER, 0x0000);
661 ks_wrreg16(ks, KS_ISR, 0xffff);
662
David J. Choi4a91ca42009-11-19 15:34:30 +0000663 /* shutdown RX/TX QMU */
664 ks_disable_qmu(ks);
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000665
666 /* set powermode to soft power down to save power */
667 ks_set_powermode(ks, PMECR_PM_SOFTDOWN);
Jan Weitzel6c23e412012-02-14 21:35:15 +0000668 free_irq(netdev->irq, netdev);
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000669 mutex_unlock(&ks->lock);
670 return 0;
671}
672
673
674/**
675 * ks_write_qmu - write 1 pkt data to the QMU.
676 * @ks: The chip information
677 * @pdata: buffer address to save 1 pkt
678 * @len: Pkt length in byte
679 * Here is the sequence to write 1 pkt:
680 * 1. set sudo DMA mode
681 * 2. write status/length
682 * 3. write pkt data
683 * 4. reset sudo DMA Mode
684 * 5. reset sudo DMA mode
685 * 6. Wait until pkt is out
686 */
687static void ks_write_qmu(struct ks_net *ks, u8 *pdata, u16 len)
688{
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000689 /* start header at txb[0] to align txw entries */
David J. Choi4a91ca42009-11-19 15:34:30 +0000690 ks->txh.txw[0] = 0;
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000691 ks->txh.txw[1] = cpu_to_le16(len);
692
693 /* 1. set sudo-DMA mode */
694 ks_wrreg8(ks, KS_RXQCR, (ks->rc_rxqcr | RXQCR_SDA) & 0xff);
695 /* 2. write status/lenth info */
696 ks_outblk(ks, ks->txh.txw, 4);
697 /* 3. write pkt data */
698 ks_outblk(ks, (u16 *)pdata, ALIGN(len, 4));
699 /* 4. reset sudo-DMA mode */
700 ks_wrreg8(ks, KS_RXQCR, ks->rc_rxqcr);
701 /* 5. Enqueue Tx(move the pkt from TX buffer into TXQ) */
702 ks_wrreg16(ks, KS_TXQCR, TXQCR_METFE);
703 /* 6. wait until TXQCR_METFE is auto-cleared */
704 while (ks_rdreg16(ks, KS_TXQCR) & TXQCR_METFE)
705 ;
706}
707
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000708/**
709 * ks_start_xmit - transmit packet
710 * @skb : The buffer to transmit
711 * @netdev : The device used to transmit the packet.
712 *
713 * Called by the network layer to transmit the @skb.
714 * spin_lock_irqsave is required because tx and rx should be mutual exclusive.
715 * So while tx is in-progress, prevent IRQ interrupt from happenning.
716 */
YueHaibing2b49117a2018-09-21 10:42:15 +0800717static netdev_tx_t ks_start_xmit(struct sk_buff *skb, struct net_device *netdev)
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000718{
YueHaibing2b49117a2018-09-21 10:42:15 +0800719 netdev_tx_t retv = NETDEV_TX_OK;
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000720 struct ks_net *ks = netdev_priv(netdev);
721
722 disable_irq(netdev->irq);
723 ks_disable_int(ks);
724 spin_lock(&ks->statelock);
725
726 /* Extra space are required:
727 * 4 byte for alignment, 4 for status/length, 4 for CRC
728 */
729
730 if (likely(ks_tx_fifo_space(ks) >= skb->len + 12)) {
731 ks_write_qmu(ks, skb->data, skb->len);
David J. Choic1ad32a2013-03-11 09:22:54 -0700732 /* add tx statistics */
733 netdev->stats.tx_bytes += skb->len;
734 netdev->stats.tx_packets++;
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000735 dev_kfree_skb(skb);
736 } else
737 retv = NETDEV_TX_BUSY;
738 spin_unlock(&ks->statelock);
739 ks_enable_int(ks);
740 enable_irq(netdev->irq);
741 return retv;
742}
743
744/**
745 * ks_start_rx - ready to serve pkts
746 * @ks : The chip information
747 *
748 */
749static void ks_start_rx(struct ks_net *ks)
750{
751 u16 cntl;
752
753 /* Enables QMU Receive (RXCR1). */
754 cntl = ks_rdreg16(ks, KS_RXCR1);
755 cntl |= RXCR1_RXE ;
756 ks_wrreg16(ks, KS_RXCR1, cntl);
757} /* ks_start_rx */
758
759/**
760 * ks_stop_rx - stop to serve pkts
761 * @ks : The chip information
762 *
763 */
764static void ks_stop_rx(struct ks_net *ks)
765{
766 u16 cntl;
767
768 /* Disables QMU Receive (RXCR1). */
769 cntl = ks_rdreg16(ks, KS_RXCR1);
770 cntl &= ~RXCR1_RXE ;
771 ks_wrreg16(ks, KS_RXCR1, cntl);
772
773} /* ks_stop_rx */
774
Krzysztof Kozlowski5d258b42018-07-17 18:05:39 +0200775static unsigned long const ethernet_polynomial = CRC32_POLY_BE;
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000776
777static unsigned long ether_gen_crc(int length, u8 *data)
778{
779 long crc = -1;
780 while (--length >= 0) {
781 u8 current_octet = *data++;
782 int bit;
783
784 for (bit = 0; bit < 8; bit++, current_octet >>= 1) {
785 crc = (crc << 1) ^
786 ((crc < 0) ^ (current_octet & 1) ?
787 ethernet_polynomial : 0);
788 }
789 }
790 return (unsigned long)crc;
791} /* ether_gen_crc */
792
793/**
794* ks_set_grpaddr - set multicast information
795* @ks : The chip information
796*/
797
798static void ks_set_grpaddr(struct ks_net *ks)
799{
800 u8 i;
801 u32 index, position, value;
802
803 memset(ks->mcast_bits, 0, sizeof(u8) * HW_MCAST_SIZE);
804
805 for (i = 0; i < ks->mcast_lst_size; i++) {
806 position = (ether_gen_crc(6, ks->mcast_lst[i]) >> 26) & 0x3f;
807 index = position >> 3;
808 value = 1 << (position & 7);
809 ks->mcast_bits[index] |= (u8)value;
810 }
811
812 for (i = 0; i < HW_MCAST_SIZE; i++) {
813 if (i & 1) {
814 ks_wrreg16(ks, (u16)((KS_MAHTR0 + i) & ~1),
815 (ks->mcast_bits[i] << 8) |
816 ks->mcast_bits[i - 1]);
817 }
818 }
819} /* ks_set_grpaddr */
820
Ben Hutchings49ce9c22012-07-10 10:56:00 +0000821/**
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000822* ks_clear_mcast - clear multicast information
823*
824* @ks : The chip information
825* This routine removes all mcast addresses set in the hardware.
826*/
827
828static void ks_clear_mcast(struct ks_net *ks)
829{
830 u16 i, mcast_size;
831 for (i = 0; i < HW_MCAST_SIZE; i++)
832 ks->mcast_bits[i] = 0;
833
834 mcast_size = HW_MCAST_SIZE >> 2;
835 for (i = 0; i < mcast_size; i++)
836 ks_wrreg16(ks, KS_MAHTR0 + (2*i), 0);
837}
838
839static void ks_set_promis(struct ks_net *ks, u16 promiscuous_mode)
840{
841 u16 cntl;
842 ks->promiscuous = promiscuous_mode;
843 ks_stop_rx(ks); /* Stop receiving for reconfiguration */
844 cntl = ks_rdreg16(ks, KS_RXCR1);
845
846 cntl &= ~RXCR1_FILTER_MASK;
847 if (promiscuous_mode)
848 /* Enable Promiscuous mode */
849 cntl |= RXCR1_RXAE | RXCR1_RXINVF;
850 else
851 /* Disable Promiscuous mode (default normal mode) */
852 cntl |= RXCR1_RXPAFMA;
853
854 ks_wrreg16(ks, KS_RXCR1, cntl);
855
856 if (ks->enabled)
857 ks_start_rx(ks);
858
859} /* ks_set_promis */
860
861static void ks_set_mcast(struct ks_net *ks, u16 mcast)
862{
863 u16 cntl;
864
865 ks->all_mcast = mcast;
866 ks_stop_rx(ks); /* Stop receiving for reconfiguration */
867 cntl = ks_rdreg16(ks, KS_RXCR1);
868 cntl &= ~RXCR1_FILTER_MASK;
869 if (mcast)
870 /* Enable "Perfect with Multicast address passed mode" */
871 cntl |= (RXCR1_RXAE | RXCR1_RXMAFMA | RXCR1_RXPAFMA);
872 else
873 /**
874 * Disable "Perfect with Multicast address passed
875 * mode" (normal mode).
876 */
877 cntl |= RXCR1_RXPAFMA;
878
879 ks_wrreg16(ks, KS_RXCR1, cntl);
880
881 if (ks->enabled)
882 ks_start_rx(ks);
883} /* ks_set_mcast */
884
885static void ks_set_rx_mode(struct net_device *netdev)
886{
887 struct ks_net *ks = netdev_priv(netdev);
Jiri Pirko22bedad32010-04-01 21:22:57 +0000888 struct netdev_hw_addr *ha;
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000889
890 /* Turn on/off promiscuous mode. */
891 if ((netdev->flags & IFF_PROMISC) == IFF_PROMISC)
892 ks_set_promis(ks,
893 (u16)((netdev->flags & IFF_PROMISC) == IFF_PROMISC));
894 /* Turn on/off all mcast mode. */
895 else if ((netdev->flags & IFF_ALLMULTI) == IFF_ALLMULTI)
896 ks_set_mcast(ks,
897 (u16)((netdev->flags & IFF_ALLMULTI) == IFF_ALLMULTI));
898 else
899 ks_set_promis(ks, false);
900
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000901 if ((netdev->flags & IFF_MULTICAST) && netdev_mc_count(netdev)) {
902 if (netdev_mc_count(netdev) <= MAX_MCAST_LST) {
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000903 int i = 0;
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +0000904
Jiri Pirko22bedad32010-04-01 21:22:57 +0000905 netdev_for_each_mc_addr(ha, netdev) {
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000906 if (i >= MAX_MCAST_LST)
907 break;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000908 memcpy(ks->mcast_lst[i++], ha->addr, ETH_ALEN);
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000909 }
910 ks->mcast_lst_size = (u8)i;
911 ks_set_grpaddr(ks);
912 } else {
913 /**
914 * List too big to support so
915 * turn on all mcast mode.
916 */
917 ks->mcast_lst_size = MAX_MCAST_LST;
918 ks_set_mcast(ks, true);
919 }
920 } else {
921 ks->mcast_lst_size = 0;
922 ks_clear_mcast(ks);
923 }
924} /* ks_set_rx_mode */
925
926static void ks_set_mac(struct ks_net *ks, u8 *data)
927{
928 u16 *pw = (u16 *)data;
929 u16 w, u;
930
931 ks_stop_rx(ks); /* Stop receiving for reconfiguration */
932
933 u = *pw++;
934 w = ((u & 0xFF) << 8) | ((u >> 8) & 0xFF);
935 ks_wrreg16(ks, KS_MARH, w);
936
937 u = *pw++;
938 w = ((u & 0xFF) << 8) | ((u >> 8) & 0xFF);
939 ks_wrreg16(ks, KS_MARM, w);
940
941 u = *pw;
942 w = ((u & 0xFF) << 8) | ((u >> 8) & 0xFF);
943 ks_wrreg16(ks, KS_MARL, w);
944
Joe Perchesd458cdf2013-10-01 19:04:40 -0700945 memcpy(ks->mac_addr, data, ETH_ALEN);
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000946
947 if (ks->enabled)
948 ks_start_rx(ks);
949}
950
951static int ks_set_mac_address(struct net_device *netdev, void *paddr)
952{
953 struct ks_net *ks = netdev_priv(netdev);
954 struct sockaddr *addr = paddr;
955 u8 *da;
956
957 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
958
959 da = (u8 *)netdev->dev_addr;
960
961 ks_set_mac(ks, da);
962 return 0;
963}
964
965static int ks_net_ioctl(struct net_device *netdev, struct ifreq *req, int cmd)
966{
967 struct ks_net *ks = netdev_priv(netdev);
968
969 if (!netif_running(netdev))
970 return -EINVAL;
971
972 return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
973}
974
975static const struct net_device_ops ks_netdev_ops = {
976 .ndo_open = ks_net_open,
977 .ndo_stop = ks_net_stop,
978 .ndo_do_ioctl = ks_net_ioctl,
979 .ndo_start_xmit = ks_start_xmit,
980 .ndo_set_mac_address = ks_set_mac_address,
981 .ndo_set_rx_mode = ks_set_rx_mode,
Choi, Davida55c0a0e2009-09-25 14:42:12 +0000982 .ndo_validate_addr = eth_validate_addr,
983};
984
985/* ethtool support */
986
987static void ks_get_drvinfo(struct net_device *netdev,
988 struct ethtool_drvinfo *di)
989{
990 strlcpy(di->driver, DRV_NAME, sizeof(di->driver));
991 strlcpy(di->version, "1.00", sizeof(di->version));
992 strlcpy(di->bus_info, dev_name(netdev->dev.parent),
993 sizeof(di->bus_info));
994}
995
996static u32 ks_get_msglevel(struct net_device *netdev)
997{
998 struct ks_net *ks = netdev_priv(netdev);
999 return ks->msg_enable;
1000}
1001
1002static void ks_set_msglevel(struct net_device *netdev, u32 to)
1003{
1004 struct ks_net *ks = netdev_priv(netdev);
1005 ks->msg_enable = to;
1006}
1007
Philippe Reynesb6878ea2017-02-09 11:28:25 +01001008static int ks_get_link_ksettings(struct net_device *netdev,
1009 struct ethtool_link_ksettings *cmd)
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001010{
1011 struct ks_net *ks = netdev_priv(netdev);
yuval.shaia@oracle.com82c01a82017-06-04 20:22:00 +03001012
1013 mii_ethtool_get_link_ksettings(&ks->mii, cmd);
1014
1015 return 0;
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001016}
1017
Philippe Reynesb6878ea2017-02-09 11:28:25 +01001018static int ks_set_link_ksettings(struct net_device *netdev,
1019 const struct ethtool_link_ksettings *cmd)
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001020{
1021 struct ks_net *ks = netdev_priv(netdev);
Philippe Reynesb6878ea2017-02-09 11:28:25 +01001022 return mii_ethtool_set_link_ksettings(&ks->mii, cmd);
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001023}
1024
1025static u32 ks_get_link(struct net_device *netdev)
1026{
1027 struct ks_net *ks = netdev_priv(netdev);
1028 return mii_link_ok(&ks->mii);
1029}
1030
1031static int ks_nway_reset(struct net_device *netdev)
1032{
1033 struct ks_net *ks = netdev_priv(netdev);
1034 return mii_nway_restart(&ks->mii);
1035}
1036
1037static const struct ethtool_ops ks_ethtool_ops = {
1038 .get_drvinfo = ks_get_drvinfo,
1039 .get_msglevel = ks_get_msglevel,
1040 .set_msglevel = ks_set_msglevel,
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001041 .get_link = ks_get_link,
1042 .nway_reset = ks_nway_reset,
Philippe Reynesb6878ea2017-02-09 11:28:25 +01001043 .get_link_ksettings = ks_get_link_ksettings,
1044 .set_link_ksettings = ks_set_link_ksettings,
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001045};
1046
1047/* MII interface controls */
1048
1049/**
1050 * ks_phy_reg - convert MII register into a KS8851 register
1051 * @reg: MII register number.
1052 *
1053 * Return the KS8851 register number for the corresponding MII PHY register
1054 * if possible. Return zero if the MII register has no direct mapping to the
1055 * KS8851 register set.
1056 */
1057static int ks_phy_reg(int reg)
1058{
1059 switch (reg) {
1060 case MII_BMCR:
1061 return KS_P1MBCR;
1062 case MII_BMSR:
1063 return KS_P1MBSR;
1064 case MII_PHYSID1:
1065 return KS_PHY1ILR;
1066 case MII_PHYSID2:
1067 return KS_PHY1IHR;
1068 case MII_ADVERTISE:
1069 return KS_P1ANAR;
1070 case MII_LPA:
1071 return KS_P1ANLPR;
1072 }
1073
1074 return 0x0;
1075}
1076
1077/**
1078 * ks_phy_read - MII interface PHY register read.
1079 * @netdev: The network device the PHY is on.
1080 * @phy_addr: Address of PHY (ignored as we only have one)
1081 * @reg: The register to read.
1082 *
1083 * This call reads data from the PHY register specified in @reg. Since the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001084 * device does not support all the MII registers, the non-existent values
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001085 * are always returned as zero.
1086 *
1087 * We return zero for unsupported registers as the MII code does not check
1088 * the value returned for any error status, and simply returns it to the
1089 * caller. The mii-tool that the driver was tested with takes any -ve error
1090 * as real PHY capabilities, thus displaying incorrect data to the user.
1091 */
1092static int ks_phy_read(struct net_device *netdev, int phy_addr, int reg)
1093{
1094 struct ks_net *ks = netdev_priv(netdev);
1095 int ksreg;
1096 int result;
1097
1098 ksreg = ks_phy_reg(reg);
1099 if (!ksreg)
1100 return 0x0; /* no error return allowed, so use zero */
1101
1102 mutex_lock(&ks->lock);
1103 result = ks_rdreg16(ks, ksreg);
1104 mutex_unlock(&ks->lock);
1105
1106 return result;
1107}
1108
1109static void ks_phy_write(struct net_device *netdev,
1110 int phy, int reg, int value)
1111{
1112 struct ks_net *ks = netdev_priv(netdev);
1113 int ksreg;
1114
1115 ksreg = ks_phy_reg(reg);
1116 if (ksreg) {
1117 mutex_lock(&ks->lock);
1118 ks_wrreg16(ks, ksreg, value);
1119 mutex_unlock(&ks->lock);
1120 }
1121}
1122
1123/**
1124 * ks_read_selftest - read the selftest memory info.
1125 * @ks: The device state
1126 *
1127 * Read and check the TX/RX memory selftest information.
1128 */
1129static int ks_read_selftest(struct ks_net *ks)
1130{
1131 unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
1132 int ret = 0;
1133 unsigned rd;
1134
1135 rd = ks_rdreg16(ks, KS_MBIR);
1136
1137 if ((rd & both_done) != both_done) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001138 netdev_warn(ks->netdev, "Memory selftest not finished\n");
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001139 return 0;
1140 }
1141
1142 if (rd & MBIR_TXMBFA) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001143 netdev_err(ks->netdev, "TX memory selftest fails\n");
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001144 ret |= 1;
1145 }
1146
1147 if (rd & MBIR_RXMBFA) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001148 netdev_err(ks->netdev, "RX memory selftest fails\n");
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001149 ret |= 2;
1150 }
1151
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001152 netdev_info(ks->netdev, "the selftest passes\n");
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001153 return ret;
1154}
1155
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001156static void ks_setup(struct ks_net *ks)
1157{
1158 u16 w;
1159
1160 /**
1161 * Configure QMU Transmit
1162 */
1163
1164 /* Setup Transmit Frame Data Pointer Auto-Increment (TXFDPR) */
1165 ks_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
1166
1167 /* Setup Receive Frame Data Pointer Auto-Increment */
1168 ks_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI);
1169
1170 /* Setup Receive Frame Threshold - 1 frame (RXFCTFC) */
Lukas Wunneraae079a2019-03-20 15:02:00 +01001171 ks_wrreg16(ks, KS_RXFCTR, 1 & RXFCTR_RXFCT_MASK);
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001172
1173 /* Setup RxQ Command Control (RXQCR) */
1174 ks->rc_rxqcr = RXQCR_CMD_CNTL;
1175 ks_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
1176
1177 /**
1178 * set the force mode to half duplex, default is full duplex
1179 * because if the auto-negotiation fails, most switch uses
1180 * half-duplex.
1181 */
1182
1183 w = ks_rdreg16(ks, KS_P1MBCR);
Lukas Wunneraae079a2019-03-20 15:02:00 +01001184 w &= ~BMCR_FULLDPLX;
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001185 ks_wrreg16(ks, KS_P1MBCR, w);
1186
1187 w = TXCR_TXFCE | TXCR_TXPE | TXCR_TXCRC | TXCR_TCGIP;
1188 ks_wrreg16(ks, KS_TXCR, w);
1189
David J. Choi4a91ca42009-11-19 15:34:30 +00001190 w = RXCR1_RXFCE | RXCR1_RXBE | RXCR1_RXUE | RXCR1_RXME | RXCR1_RXIPFCC;
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001191
1192 if (ks->promiscuous) /* bPromiscuous */
1193 w |= (RXCR1_RXAE | RXCR1_RXINVF);
1194 else if (ks->all_mcast) /* Multicast address passed mode */
1195 w |= (RXCR1_RXAE | RXCR1_RXMAFMA | RXCR1_RXPAFMA);
1196 else /* Normal mode */
1197 w |= RXCR1_RXPAFMA;
1198
1199 ks_wrreg16(ks, KS_RXCR1, w);
1200} /*ks_setup */
1201
1202
1203static void ks_setup_int(struct ks_net *ks)
1204{
1205 ks->rc_ier = 0x00;
1206 /* Clear the interrupts status of the hardware. */
1207 ks_wrreg16(ks, KS_ISR, 0xffff);
1208
1209 /* Enables the interrupts of the hardware. */
1210 ks->rc_ier = (IRQ_LCI | IRQ_TXI | IRQ_RXI);
1211} /* ks_setup_int */
1212
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001213static int ks_hw_init(struct ks_net *ks)
1214{
1215#define MHEADER_SIZE (sizeof(struct type_frame_head) * MAX_RECV_FRAMES)
1216 ks->promiscuous = 0;
1217 ks->all_mcast = 0;
1218 ks->mcast_lst_size = 0;
1219
Himangi Saraogi54789982014-08-01 20:59:13 +05301220 ks->frame_head_info = devm_kmalloc(&ks->pdev->dev, MHEADER_SIZE,
1221 GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +00001222 if (!ks->frame_head_info)
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001223 return false;
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001224
1225 ks_set_mac(ks, KS_DEFAULT_MAC_ADDRESS);
1226 return true;
1227}
1228
Jean-Christophe PLAGNIOL-VILLARD87227b82013-05-23 23:01:22 +00001229#if defined(CONFIG_OF)
1230static const struct of_device_id ks8851_ml_dt_ids[] = {
1231 { .compatible = "micrel,ks8851-mll" },
1232 { /* sentinel */ }
1233};
1234MODULE_DEVICE_TABLE(of, ks8851_ml_dt_ids);
1235#endif
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001236
Bill Pemberton654b8c52012-12-03 09:23:59 -05001237static int ks8851_probe(struct platform_device *pdev)
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001238{
Himangi Saraogi54789982014-08-01 20:59:13 +05301239 int err;
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001240 struct resource *io_d, *io_c;
1241 struct net_device *netdev;
1242 struct ks_net *ks;
1243 u16 id, data;
Jean-Christophe PLAGNIOL-VILLARD87227b82013-05-23 23:01:22 +00001244 const char *mac;
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001245
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001246 netdev = alloc_etherdev(sizeof(struct ks_net));
1247 if (!netdev)
Himangi Saraogi54789982014-08-01 20:59:13 +05301248 return -ENOMEM;
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001249
1250 SET_NETDEV_DEV(netdev, &pdev->dev);
1251
1252 ks = netdev_priv(netdev);
1253 ks->netdev = netdev;
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001254
Himangi Saraogi54789982014-08-01 20:59:13 +05301255 io_d = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1256 ks->hw_addr = devm_ioremap_resource(&pdev->dev, io_d);
1257 if (IS_ERR(ks->hw_addr)) {
1258 err = PTR_ERR(ks->hw_addr);
1259 goto err_free;
1260 }
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001261
Himangi Saraogi54789982014-08-01 20:59:13 +05301262 io_c = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1263 ks->hw_addr_cmd = devm_ioremap_resource(&pdev->dev, io_c);
1264 if (IS_ERR(ks->hw_addr_cmd)) {
1265 err = PTR_ERR(ks->hw_addr_cmd);
1266 goto err_free;
1267 }
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001268
Jan Weitzel6c23e412012-02-14 21:35:15 +00001269 netdev->irq = platform_get_irq(pdev, 0);
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001270
Dan Carpenter32aa64f72012-02-16 20:44:33 +00001271 if ((int)netdev->irq < 0) {
Jan Weitzel6c23e412012-02-14 21:35:15 +00001272 err = netdev->irq;
Himangi Saraogi54789982014-08-01 20:59:13 +05301273 goto err_free;
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001274 }
1275
1276 ks->pdev = pdev;
1277
1278 mutex_init(&ks->lock);
1279 spin_lock_init(&ks->statelock);
1280
1281 netdev->netdev_ops = &ks_netdev_ops;
1282 netdev->ethtool_ops = &ks_ethtool_ops;
1283
1284 /* setup mii state */
1285 ks->mii.dev = netdev;
1286 ks->mii.phy_id = 1,
1287 ks->mii.phy_id_mask = 1;
1288 ks->mii.reg_num_mask = 0xf;
1289 ks->mii.mdio_read = ks_phy_read;
1290 ks->mii.mdio_write = ks_phy_write;
1291
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001292 netdev_info(netdev, "message enable is %d\n", msg_enable);
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001293 /* set the default message enable */
1294 ks->msg_enable = netif_msg_init(msg_enable, (NETIF_MSG_DRV |
1295 NETIF_MSG_PROBE |
1296 NETIF_MSG_LINK));
1297 ks_read_config(ks);
1298
1299 /* simple check for a valid chip being connected to the bus */
1300 if ((ks_rdreg16(ks, KS_CIDER) & ~CIDER_REV_MASK) != CIDER_ID) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001301 netdev_err(netdev, "failed to read device ID\n");
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001302 err = -ENODEV;
Himangi Saraogi54789982014-08-01 20:59:13 +05301303 goto err_free;
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001304 }
1305
1306 if (ks_read_selftest(ks)) {
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001307 netdev_err(netdev, "failed to read device ID\n");
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001308 err = -ENODEV;
Himangi Saraogi54789982014-08-01 20:59:13 +05301309 goto err_free;
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001310 }
1311
1312 err = register_netdev(netdev);
1313 if (err)
Himangi Saraogi54789982014-08-01 20:59:13 +05301314 goto err_free;
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001315
1316 platform_set_drvdata(pdev, netdev);
1317
1318 ks_soft_reset(ks, GRR_GSR);
1319 ks_hw_init(ks);
David J. Choi4a91ca42009-11-19 15:34:30 +00001320 ks_disable_qmu(ks);
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001321 ks_setup(ks);
1322 ks_setup_int(ks);
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001323
1324 data = ks_rdreg16(ks, KS_OBCR);
Lukas Wunneraae079a2019-03-20 15:02:00 +01001325 ks_wrreg16(ks, KS_OBCR, data | OBCR_ODS_16mA);
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001326
Raffaele Recalcati29a6b6c2012-06-03 10:43:43 +00001327 /* overwriting the default MAC address */
Jean-Christophe PLAGNIOL-VILLARD87227b82013-05-23 23:01:22 +00001328 if (pdev->dev.of_node) {
1329 mac = of_get_mac_address(pdev->dev.of_node);
Petr Štetiara51645f2019-05-06 23:27:04 +02001330 if (!IS_ERR(mac))
Petr Štetiar2d2924a2019-05-10 11:35:17 +02001331 ether_addr_copy(ks->mac_addr, mac);
Jean-Christophe PLAGNIOL-VILLARD87227b82013-05-23 23:01:22 +00001332 } else {
1333 struct ks8851_mll_platform_data *pdata;
1334
Jingoo Hanee946612013-08-30 13:59:01 +09001335 pdata = dev_get_platdata(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARD87227b82013-05-23 23:01:22 +00001336 if (!pdata) {
1337 netdev_err(netdev, "No platform data\n");
1338 err = -ENODEV;
1339 goto err_pdata;
1340 }
1341 memcpy(ks->mac_addr, pdata->mac_addr, ETH_ALEN);
Raffaele Recalcati29a6b6c2012-06-03 10:43:43 +00001342 }
Raffaele Recalcati29a6b6c2012-06-03 10:43:43 +00001343 if (!is_valid_ether_addr(ks->mac_addr)) {
1344 /* Use random MAC address if none passed */
Joe Perches7efd26d2012-07-12 19:33:06 +00001345 eth_random_addr(ks->mac_addr);
Raffaele Recalcati29a6b6c2012-06-03 10:43:43 +00001346 netdev_info(netdev, "Using random mac address\n");
1347 }
1348 netdev_info(netdev, "Mac address is: %pM\n", ks->mac_addr);
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001349
Joe Perchesd458cdf2013-10-01 19:04:40 -07001350 memcpy(netdev->dev_addr, ks->mac_addr, ETH_ALEN);
Raffaele Recalcati29a6b6c2012-06-03 10:43:43 +00001351
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001352 ks_set_mac(ks, netdev->dev_addr);
1353
1354 id = ks_rdreg16(ks, KS_CIDER);
1355
Joe Perches0dc7d2b2010-02-27 14:43:51 +00001356 netdev_info(netdev, "Found chip, family: 0x%x, id: 0x%x, rev: 0x%x\n",
1357 (id >> 8) & 0xff, (id >> 4) & 0xf, (id >> 1) & 0x7);
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001358 return 0;
1359
Dan Carpenterc3001b72012-06-05 20:31:29 +00001360err_pdata:
1361 unregister_netdev(netdev);
Himangi Saraogi54789982014-08-01 20:59:13 +05301362err_free:
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001363 free_netdev(netdev);
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001364 return err;
1365}
1366
Bill Pemberton654b8c52012-12-03 09:23:59 -05001367static int ks8851_remove(struct platform_device *pdev)
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001368{
1369 struct net_device *netdev = platform_get_drvdata(pdev);
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001370
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001371 unregister_netdev(netdev);
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001372 free_netdev(netdev);
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001373 return 0;
1374
1375}
1376
1377static struct platform_driver ks8851_platform_driver = {
1378 .driver = {
1379 .name = DRV_NAME,
Jean-Christophe PLAGNIOL-VILLARD87227b82013-05-23 23:01:22 +00001380 .of_match_table = of_match_ptr(ks8851_ml_dt_ids),
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001381 },
1382 .probe = ks8851_probe,
Bill Pemberton654b8c52012-12-03 09:23:59 -05001383 .remove = ks8851_remove,
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001384};
1385
Axel Lindb62f682011-11-27 16:44:17 +00001386module_platform_driver(ks8851_platform_driver);
Choi, Davida55c0a0e2009-09-25 14:42:12 +00001387
1388MODULE_DESCRIPTION("KS8851 MLL Network driver");
1389MODULE_AUTHOR("David Choi <david.choi@micrel.com>");
1390MODULE_LICENSE("GPL");
1391module_param_named(message, msg_enable, int, 0);
1392MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");
1393