Linux 4.19.133
[linux/fpc-iii.git] / drivers / net / ethernet / micrel / ks8851_mll.c
blob68af0d8cf61371f4cf96c7643a92a73a220e4b89
1 /**
2 * drivers/net/ethernet/micrel/ks8851_mll.c
3 * Copyright (c) 2009 Micrel Inc.
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.
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.
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.
19 /* Supports:
20 * KS8851 16bit MLL chip from Micrel Inc.
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/interrupt.h>
26 #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>
33 #include <linux/crc32poly.h>
34 #include <linux/mii.h>
35 #include <linux/platform_device.h>
36 #include <linux/delay.h>
37 #include <linux/slab.h>
38 #include <linux/ks8851_mll.h>
39 #include <linux/of.h>
40 #include <linux/of_device.h>
41 #include <linux/of_net.h>
43 #define DRV_NAME "ks8851_mll"
45 static u8 KS_DEFAULT_MAC_ADDRESS[] = { 0x00, 0x10, 0xA1, 0x86, 0x95, 0x11 };
46 #define MAX_RECV_FRAMES 255
47 #define MAX_BUF_SIZE 2048
48 #define TX_BUF_SIZE 2000
49 #define RX_BUF_SIZE 2000
51 #define KS_CCR 0x08
52 #define CCR_EEPROM (1 << 9)
53 #define CCR_SPI (1 << 8)
54 #define CCR_8BIT (1 << 7)
55 #define CCR_16BIT (1 << 6)
56 #define CCR_32BIT (1 << 5)
57 #define CCR_SHARED (1 << 4)
58 #define CCR_32PIN (1 << 0)
60 /* MAC address registers */
61 #define KS_MARL 0x10
62 #define KS_MARM 0x12
63 #define KS_MARH 0x14
65 #define KS_OBCR 0x20
66 #define OBCR_ODS_16MA (1 << 6)
68 #define KS_EEPCR 0x22
69 #define EEPCR_EESA (1 << 4)
70 #define EEPCR_EESB (1 << 3)
71 #define EEPCR_EEDO (1 << 2)
72 #define EEPCR_EESCK (1 << 1)
73 #define EEPCR_EECS (1 << 0)
75 #define KS_MBIR 0x24
76 #define MBIR_TXMBF (1 << 12)
77 #define MBIR_TXMBFA (1 << 11)
78 #define MBIR_RXMBF (1 << 4)
79 #define MBIR_RXMBFA (1 << 3)
81 #define KS_GRR 0x26
82 #define GRR_QMU (1 << 1)
83 #define GRR_GSR (1 << 0)
85 #define KS_WFCR 0x2A
86 #define WFCR_MPRXE (1 << 7)
87 #define WFCR_WF3E (1 << 3)
88 #define WFCR_WF2E (1 << 2)
89 #define WFCR_WF1E (1 << 1)
90 #define WFCR_WF0E (1 << 0)
92 #define KS_WF0CRC0 0x30
93 #define KS_WF0CRC1 0x32
94 #define KS_WF0BM0 0x34
95 #define KS_WF0BM1 0x36
96 #define KS_WF0BM2 0x38
97 #define KS_WF0BM3 0x3A
99 #define KS_WF1CRC0 0x40
100 #define KS_WF1CRC1 0x42
101 #define KS_WF1BM0 0x44
102 #define KS_WF1BM1 0x46
103 #define KS_WF1BM2 0x48
104 #define KS_WF1BM3 0x4A
106 #define KS_WF2CRC0 0x50
107 #define KS_WF2CRC1 0x52
108 #define KS_WF2BM0 0x54
109 #define KS_WF2BM1 0x56
110 #define KS_WF2BM2 0x58
111 #define KS_WF2BM3 0x5A
113 #define KS_WF3CRC0 0x60
114 #define KS_WF3CRC1 0x62
115 #define KS_WF3BM0 0x64
116 #define KS_WF3BM1 0x66
117 #define KS_WF3BM2 0x68
118 #define KS_WF3BM3 0x6A
120 #define KS_TXCR 0x70
121 #define TXCR_TCGICMP (1 << 8)
122 #define TXCR_TCGUDP (1 << 7)
123 #define TXCR_TCGTCP (1 << 6)
124 #define TXCR_TCGIP (1 << 5)
125 #define TXCR_FTXQ (1 << 4)
126 #define TXCR_TXFCE (1 << 3)
127 #define TXCR_TXPE (1 << 2)
128 #define TXCR_TXCRC (1 << 1)
129 #define TXCR_TXE (1 << 0)
131 #define KS_TXSR 0x72
132 #define TXSR_TXLC (1 << 13)
133 #define TXSR_TXMC (1 << 12)
134 #define TXSR_TXFID_MASK (0x3f << 0)
135 #define TXSR_TXFID_SHIFT (0)
136 #define TXSR_TXFID_GET(_v) (((_v) >> 0) & 0x3f)
139 #define KS_RXCR1 0x74
140 #define RXCR1_FRXQ (1 << 15)
141 #define RXCR1_RXUDPFCC (1 << 14)
142 #define RXCR1_RXTCPFCC (1 << 13)
143 #define RXCR1_RXIPFCC (1 << 12)
144 #define RXCR1_RXPAFMA (1 << 11)
145 #define RXCR1_RXFCE (1 << 10)
146 #define RXCR1_RXEFE (1 << 9)
147 #define RXCR1_RXMAFMA (1 << 8)
148 #define RXCR1_RXBE (1 << 7)
149 #define RXCR1_RXME (1 << 6)
150 #define RXCR1_RXUE (1 << 5)
151 #define RXCR1_RXAE (1 << 4)
152 #define RXCR1_RXINVF (1 << 1)
153 #define RXCR1_RXE (1 << 0)
154 #define RXCR1_FILTER_MASK (RXCR1_RXINVF | RXCR1_RXAE | \
155 RXCR1_RXMAFMA | RXCR1_RXPAFMA)
157 #define KS_RXCR2 0x76
158 #define RXCR2_SRDBL_MASK (0x7 << 5)
159 #define RXCR2_SRDBL_SHIFT (5)
160 #define RXCR2_SRDBL_4B (0x0 << 5)
161 #define RXCR2_SRDBL_8B (0x1 << 5)
162 #define RXCR2_SRDBL_16B (0x2 << 5)
163 #define RXCR2_SRDBL_32B (0x3 << 5)
164 /* #define RXCR2_SRDBL_FRAME (0x4 << 5) */
165 #define RXCR2_IUFFP (1 << 4)
166 #define RXCR2_RXIUFCEZ (1 << 3)
167 #define RXCR2_UDPLFE (1 << 2)
168 #define RXCR2_RXICMPFCC (1 << 1)
169 #define RXCR2_RXSAF (1 << 0)
171 #define KS_TXMIR 0x78
173 #define KS_RXFHSR 0x7C
174 #define RXFSHR_RXFV (1 << 15)
175 #define RXFSHR_RXICMPFCS (1 << 13)
176 #define RXFSHR_RXIPFCS (1 << 12)
177 #define RXFSHR_RXTCPFCS (1 << 11)
178 #define RXFSHR_RXUDPFCS (1 << 10)
179 #define RXFSHR_RXBF (1 << 7)
180 #define RXFSHR_RXMF (1 << 6)
181 #define RXFSHR_RXUF (1 << 5)
182 #define RXFSHR_RXMR (1 << 4)
183 #define RXFSHR_RXFT (1 << 3)
184 #define RXFSHR_RXFTL (1 << 2)
185 #define RXFSHR_RXRF (1 << 1)
186 #define RXFSHR_RXCE (1 << 0)
187 #define RXFSHR_ERR (RXFSHR_RXCE | RXFSHR_RXRF |\
188 RXFSHR_RXFTL | RXFSHR_RXMR |\
189 RXFSHR_RXICMPFCS | RXFSHR_RXIPFCS |\
190 RXFSHR_RXTCPFCS)
191 #define KS_RXFHBCR 0x7E
192 #define RXFHBCR_CNT_MASK 0x0FFF
194 #define KS_TXQCR 0x80
195 #define TXQCR_AETFE (1 << 2)
196 #define TXQCR_TXQMAM (1 << 1)
197 #define TXQCR_METFE (1 << 0)
199 #define KS_RXQCR 0x82
200 #define RXQCR_RXDTTS (1 << 12)
201 #define RXQCR_RXDBCTS (1 << 11)
202 #define RXQCR_RXFCTS (1 << 10)
203 #define RXQCR_RXIPHTOE (1 << 9)
204 #define RXQCR_RXDTTE (1 << 7)
205 #define RXQCR_RXDBCTE (1 << 6)
206 #define RXQCR_RXFCTE (1 << 5)
207 #define RXQCR_ADRFE (1 << 4)
208 #define RXQCR_SDA (1 << 3)
209 #define RXQCR_RRXEF (1 << 0)
210 #define RXQCR_CMD_CNTL (RXQCR_RXFCTE|RXQCR_ADRFE)
212 #define KS_TXFDPR 0x84
213 #define TXFDPR_TXFPAI (1 << 14)
214 #define TXFDPR_TXFP_MASK (0x7ff << 0)
215 #define TXFDPR_TXFP_SHIFT (0)
217 #define KS_RXFDPR 0x86
218 #define RXFDPR_RXFPAI (1 << 14)
220 #define KS_RXDTTR 0x8C
221 #define KS_RXDBCTR 0x8E
223 #define KS_IER 0x90
224 #define KS_ISR 0x92
225 #define IRQ_LCI (1 << 15)
226 #define IRQ_TXI (1 << 14)
227 #define IRQ_RXI (1 << 13)
228 #define IRQ_RXOI (1 << 11)
229 #define IRQ_TXPSI (1 << 9)
230 #define IRQ_RXPSI (1 << 8)
231 #define IRQ_TXSAI (1 << 6)
232 #define IRQ_RXWFDI (1 << 5)
233 #define IRQ_RXMPDI (1 << 4)
234 #define IRQ_LDI (1 << 3)
235 #define IRQ_EDI (1 << 2)
236 #define IRQ_SPIBEI (1 << 1)
237 #define IRQ_DEDI (1 << 0)
239 #define KS_RXFCTR 0x9C
240 #define RXFCTR_THRESHOLD_MASK 0x00FF
242 #define KS_RXFC 0x9D
243 #define RXFCTR_RXFC_MASK (0xff << 8)
244 #define RXFCTR_RXFC_SHIFT (8)
245 #define RXFCTR_RXFC_GET(_v) (((_v) >> 8) & 0xff)
246 #define RXFCTR_RXFCT_MASK (0xff << 0)
247 #define RXFCTR_RXFCT_SHIFT (0)
249 #define KS_TXNTFSR 0x9E
251 #define KS_MAHTR0 0xA0
252 #define KS_MAHTR1 0xA2
253 #define KS_MAHTR2 0xA4
254 #define KS_MAHTR3 0xA6
256 #define KS_FCLWR 0xB0
257 #define KS_FCHWR 0xB2
258 #define KS_FCOWR 0xB4
260 #define KS_CIDER 0xC0
261 #define CIDER_ID 0x8870
262 #define CIDER_REV_MASK (0x7 << 1)
263 #define CIDER_REV_SHIFT (1)
264 #define CIDER_REV_GET(_v) (((_v) >> 1) & 0x7)
266 #define KS_CGCR 0xC6
267 #define KS_IACR 0xC8
268 #define IACR_RDEN (1 << 12)
269 #define IACR_TSEL_MASK (0x3 << 10)
270 #define IACR_TSEL_SHIFT (10)
271 #define IACR_TSEL_MIB (0x3 << 10)
272 #define IACR_ADDR_MASK (0x1f << 0)
273 #define IACR_ADDR_SHIFT (0)
275 #define KS_IADLR 0xD0
276 #define KS_IAHDR 0xD2
278 #define KS_PMECR 0xD4
279 #define PMECR_PME_DELAY (1 << 14)
280 #define PMECR_PME_POL (1 << 12)
281 #define PMECR_WOL_WAKEUP (1 << 11)
282 #define PMECR_WOL_MAGICPKT (1 << 10)
283 #define PMECR_WOL_LINKUP (1 << 9)
284 #define PMECR_WOL_ENERGY (1 << 8)
285 #define PMECR_AUTO_WAKE_EN (1 << 7)
286 #define PMECR_WAKEUP_NORMAL (1 << 6)
287 #define PMECR_WKEVT_MASK (0xf << 2)
288 #define PMECR_WKEVT_SHIFT (2)
289 #define PMECR_WKEVT_GET(_v) (((_v) >> 2) & 0xf)
290 #define PMECR_WKEVT_ENERGY (0x1 << 2)
291 #define PMECR_WKEVT_LINK (0x2 << 2)
292 #define PMECR_WKEVT_MAGICPKT (0x4 << 2)
293 #define PMECR_WKEVT_FRAME (0x8 << 2)
294 #define PMECR_PM_MASK (0x3 << 0)
295 #define PMECR_PM_SHIFT (0)
296 #define PMECR_PM_NORMAL (0x0 << 0)
297 #define PMECR_PM_ENERGY (0x1 << 0)
298 #define PMECR_PM_SOFTDOWN (0x2 << 0)
299 #define PMECR_PM_POWERSAVE (0x3 << 0)
301 /* Standard MII PHY data */
302 #define KS_P1MBCR 0xE4
303 #define P1MBCR_FORCE_FDX (1 << 8)
305 #define KS_P1MBSR 0xE6
306 #define P1MBSR_AN_COMPLETE (1 << 5)
307 #define P1MBSR_AN_CAPABLE (1 << 3)
308 #define P1MBSR_LINK_UP (1 << 2)
310 #define KS_PHY1ILR 0xE8
311 #define KS_PHY1IHR 0xEA
312 #define KS_P1ANAR 0xEC
313 #define KS_P1ANLPR 0xEE
315 #define KS_P1SCLMD 0xF4
316 #define P1SCLMD_LEDOFF (1 << 15)
317 #define P1SCLMD_TXIDS (1 << 14)
318 #define P1SCLMD_RESTARTAN (1 << 13)
319 #define P1SCLMD_DISAUTOMDIX (1 << 10)
320 #define P1SCLMD_FORCEMDIX (1 << 9)
321 #define P1SCLMD_AUTONEGEN (1 << 7)
322 #define P1SCLMD_FORCE100 (1 << 6)
323 #define P1SCLMD_FORCEFDX (1 << 5)
324 #define P1SCLMD_ADV_FLOW (1 << 4)
325 #define P1SCLMD_ADV_100BT_FDX (1 << 3)
326 #define P1SCLMD_ADV_100BT_HDX (1 << 2)
327 #define P1SCLMD_ADV_10BT_FDX (1 << 1)
328 #define P1SCLMD_ADV_10BT_HDX (1 << 0)
330 #define KS_P1CR 0xF6
331 #define P1CR_HP_MDIX (1 << 15)
332 #define P1CR_REV_POL (1 << 13)
333 #define P1CR_OP_100M (1 << 10)
334 #define P1CR_OP_FDX (1 << 9)
335 #define P1CR_OP_MDI (1 << 7)
336 #define P1CR_AN_DONE (1 << 6)
337 #define P1CR_LINK_GOOD (1 << 5)
338 #define P1CR_PNTR_FLOW (1 << 4)
339 #define P1CR_PNTR_100BT_FDX (1 << 3)
340 #define P1CR_PNTR_100BT_HDX (1 << 2)
341 #define P1CR_PNTR_10BT_FDX (1 << 1)
342 #define P1CR_PNTR_10BT_HDX (1 << 0)
344 /* TX Frame control */
346 #define TXFR_TXIC (1 << 15)
347 #define TXFR_TXFID_MASK (0x3f << 0)
348 #define TXFR_TXFID_SHIFT (0)
350 #define KS_P1SR 0xF8
351 #define P1SR_HP_MDIX (1 << 15)
352 #define P1SR_REV_POL (1 << 13)
353 #define P1SR_OP_100M (1 << 10)
354 #define P1SR_OP_FDX (1 << 9)
355 #define P1SR_OP_MDI (1 << 7)
356 #define P1SR_AN_DONE (1 << 6)
357 #define P1SR_LINK_GOOD (1 << 5)
358 #define P1SR_PNTR_FLOW (1 << 4)
359 #define P1SR_PNTR_100BT_FDX (1 << 3)
360 #define P1SR_PNTR_100BT_HDX (1 << 2)
361 #define P1SR_PNTR_10BT_FDX (1 << 1)
362 #define P1SR_PNTR_10BT_HDX (1 << 0)
364 #define ENUM_BUS_NONE 0
365 #define ENUM_BUS_8BIT 1
366 #define ENUM_BUS_16BIT 2
367 #define ENUM_BUS_32BIT 3
369 #define MAX_MCAST_LST 32
370 #define HW_MCAST_SIZE 8
373 * union ks_tx_hdr - tx header data
374 * @txb: The header as bytes
375 * @txw: The header as 16bit, little-endian words
377 * A dual representation of the tx header data to allow
378 * access to individual bytes, and to allow 16bit accesses
379 * with 16bit alignment.
381 union ks_tx_hdr {
382 u8 txb[4];
383 __le16 txw[2];
387 * struct ks_net - KS8851 driver private data
388 * @net_device : The network device we're bound to
389 * @hw_addr : start address of data register.
390 * @hw_addr_cmd : start address of command register.
391 * @txh : temporaly buffer to save status/length.
392 * @lock : Lock to ensure that the device is not accessed when busy.
393 * @pdev : Pointer to platform device.
394 * @mii : The MII state information for the mii calls.
395 * @frame_head_info : frame header information for multi-pkt rx.
396 * @statelock : Lock on this structure for tx list.
397 * @msg_enable : The message flags controlling driver output (see ethtool).
398 * @frame_cnt : number of frames received.
399 * @bus_width : i/o bus width.
400 * @rc_rxqcr : Cached copy of KS_RXQCR.
401 * @rc_txcr : Cached copy of KS_TXCR.
402 * @rc_ier : Cached copy of KS_IER.
403 * @sharedbus : Multipex(addr and data bus) mode indicator.
404 * @cmd_reg_cache : command register cached.
405 * @cmd_reg_cache_int : command register cached. Used in the irq handler.
406 * @promiscuous : promiscuous mode indicator.
407 * @all_mcast : mutlicast indicator.
408 * @mcast_lst_size : size of multicast list.
409 * @mcast_lst : multicast list.
410 * @mcast_bits : multicast enabed.
411 * @mac_addr : MAC address assigned to this device.
412 * @fid : frame id.
413 * @extra_byte : number of extra byte prepended rx pkt.
414 * @enabled : indicator this device works.
416 * The @lock ensures that the chip is protected when certain operations are
417 * in progress. When the read or write packet transfer is in progress, most
418 * of the chip registers are not accessible until the transfer is finished and
419 * the DMA has been de-asserted.
421 * The @statelock is used to protect information in the structure which may
422 * need to be accessed via several sources, such as the network driver layer
423 * or one of the work queues.
427 /* Receive multiplex framer header info */
428 struct type_frame_head {
429 u16 sts; /* Frame status */
430 u16 len; /* Byte count */
433 struct ks_net {
434 struct net_device *netdev;
435 void __iomem *hw_addr;
436 void __iomem *hw_addr_cmd;
437 union ks_tx_hdr txh ____cacheline_aligned;
438 struct mutex lock; /* spinlock to be interrupt safe */
439 struct platform_device *pdev;
440 struct mii_if_info mii;
441 struct type_frame_head *frame_head_info;
442 spinlock_t statelock;
443 u32 msg_enable;
444 u32 frame_cnt;
445 int bus_width;
447 u16 rc_rxqcr;
448 u16 rc_txcr;
449 u16 rc_ier;
450 u16 sharedbus;
451 u16 cmd_reg_cache;
452 u16 cmd_reg_cache_int;
453 u16 promiscuous;
454 u16 all_mcast;
455 u16 mcast_lst_size;
456 u8 mcast_lst[MAX_MCAST_LST][ETH_ALEN];
457 u8 mcast_bits[HW_MCAST_SIZE];
458 u8 mac_addr[6];
459 u8 fid;
460 u8 extra_byte;
461 u8 enabled;
464 static int msg_enable;
466 #define BE3 0x8000 /* Byte Enable 3 */
467 #define BE2 0x4000 /* Byte Enable 2 */
468 #define BE1 0x2000 /* Byte Enable 1 */
469 #define BE0 0x1000 /* Byte Enable 0 */
471 /* register read/write calls.
473 * All these calls issue transactions to access the chip's registers. They
474 * all require that the necessary lock is held to prevent accesses when the
475 * chip is busy transferring packet data (RX/TX FIFO accesses).
479 * ks_check_endian - Check whether endianness of the bus is correct
480 * @ks : The chip information
482 * The KS8851-16MLL EESK pin allows selecting the endianness of the 16bit
483 * bus. To maintain optimum performance, the bus endianness should be set
484 * such that it matches the endianness of the CPU.
487 static int ks_check_endian(struct ks_net *ks)
489 u16 cider;
492 * Read CIDER register first, however read it the "wrong" way around.
493 * If the endian strap on the KS8851-16MLL in incorrect and the chip
494 * is operating in different endianness than the CPU, then the meaning
495 * of BE[3:0] byte-enable bits is also swapped such that:
496 * BE[3,2,1,0] becomes BE[1,0,3,2]
498 * Luckily for us, the byte-enable bits are the top four MSbits of
499 * the address register and the CIDER register is at offset 0xc0.
500 * Hence, by reading address 0xc0c0, which is not impacted by endian
501 * swapping, we assert either BE[3:2] or BE[1:0] while reading the
502 * CIDER register.
504 * If the bus configuration is correct, reading 0xc0c0 asserts
505 * BE[3:2] and this read returns 0x0000, because to read register
506 * with bottom two LSbits of address set to 0, BE[1:0] must be
507 * asserted.
509 * If the bus configuration is NOT correct, reading 0xc0c0 asserts
510 * BE[1:0] and this read returns non-zero 0x8872 value.
512 iowrite16(BE3 | BE2 | KS_CIDER, ks->hw_addr_cmd);
513 cider = ioread16(ks->hw_addr);
514 if (!cider)
515 return 0;
517 netdev_err(ks->netdev, "incorrect EESK endian strap setting\n");
519 return -EINVAL;
523 * ks_rdreg16 - read 16 bit register from device
524 * @ks : The chip information
525 * @offset: The register address
527 * Read a 16bit register from the chip, returning the result
530 static u16 ks_rdreg16(struct ks_net *ks, int offset)
532 ks->cmd_reg_cache = (u16)offset | ((BE1 | BE0) << (offset & 0x02));
533 iowrite16(ks->cmd_reg_cache, ks->hw_addr_cmd);
534 return ioread16(ks->hw_addr);
538 * ks_wrreg16 - write 16bit register value to chip
539 * @ks: The chip information
540 * @offset: The register address
541 * @value: The value to write
545 static void ks_wrreg16(struct ks_net *ks, int offset, u16 value)
547 ks->cmd_reg_cache = (u16)offset | ((BE1 | BE0) << (offset & 0x02));
548 iowrite16(ks->cmd_reg_cache, ks->hw_addr_cmd);
549 iowrite16(value, ks->hw_addr);
553 * ks_inblk - read a block of data from QMU. This is called after sudo DMA mode enabled.
554 * @ks: The chip state
555 * @wptr: buffer address to save data
556 * @len: length in byte to read
559 static inline void ks_inblk(struct ks_net *ks, u16 *wptr, u32 len)
561 len >>= 1;
562 while (len--)
563 *wptr++ = (u16)ioread16(ks->hw_addr);
567 * ks_outblk - write data to QMU. This is called after sudo DMA mode enabled.
568 * @ks: The chip information
569 * @wptr: buffer address
570 * @len: length in byte to write
573 static inline void ks_outblk(struct ks_net *ks, u16 *wptr, u32 len)
575 len >>= 1;
576 while (len--)
577 iowrite16(*wptr++, ks->hw_addr);
580 static void ks_disable_int(struct ks_net *ks)
582 ks_wrreg16(ks, KS_IER, 0x0000);
583 } /* ks_disable_int */
585 static void ks_enable_int(struct ks_net *ks)
587 ks_wrreg16(ks, KS_IER, ks->rc_ier);
588 } /* ks_enable_int */
591 * ks_tx_fifo_space - return the available hardware buffer size.
592 * @ks: The chip information
595 static inline u16 ks_tx_fifo_space(struct ks_net *ks)
597 return ks_rdreg16(ks, KS_TXMIR) & 0x1fff;
601 * ks_save_cmd_reg - save the command register from the cache.
602 * @ks: The chip information
605 static inline void ks_save_cmd_reg(struct ks_net *ks)
607 /*ks8851 MLL has a bug to read back the command register.
608 * So rely on software to save the content of command register.
610 ks->cmd_reg_cache_int = ks->cmd_reg_cache;
614 * ks_restore_cmd_reg - restore the command register from the cache and
615 * write to hardware register.
616 * @ks: The chip information
619 static inline void ks_restore_cmd_reg(struct ks_net *ks)
621 ks->cmd_reg_cache = ks->cmd_reg_cache_int;
622 iowrite16(ks->cmd_reg_cache, ks->hw_addr_cmd);
626 * ks_set_powermode - set power mode of the device
627 * @ks: The chip information
628 * @pwrmode: The power mode value to write to KS_PMECR.
630 * Change the power mode of the chip.
632 static void ks_set_powermode(struct ks_net *ks, unsigned pwrmode)
634 unsigned pmecr;
636 netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode);
638 ks_rdreg16(ks, KS_GRR);
639 pmecr = ks_rdreg16(ks, KS_PMECR);
640 pmecr &= ~PMECR_PM_MASK;
641 pmecr |= pwrmode;
643 ks_wrreg16(ks, KS_PMECR, pmecr);
647 * ks_read_config - read chip configuration of bus width.
648 * @ks: The chip information
651 static void ks_read_config(struct ks_net *ks)
653 u16 reg_data = 0;
655 /* Regardless of bus width, 8 bit read should always work.*/
656 reg_data = ks_rdreg16(ks, KS_CCR);
658 /* addr/data bus are multiplexed */
659 ks->sharedbus = (reg_data & CCR_SHARED) == CCR_SHARED;
661 /* There are garbage data when reading data from QMU,
662 depending on bus-width.
665 if (reg_data & CCR_8BIT) {
666 ks->bus_width = ENUM_BUS_8BIT;
667 ks->extra_byte = 1;
668 } else if (reg_data & CCR_16BIT) {
669 ks->bus_width = ENUM_BUS_16BIT;
670 ks->extra_byte = 2;
671 } else {
672 ks->bus_width = ENUM_BUS_32BIT;
673 ks->extra_byte = 4;
678 * ks_soft_reset - issue one of the soft reset to the device
679 * @ks: The device state.
680 * @op: The bit(s) to set in the GRR
682 * Issue the relevant soft-reset command to the device's GRR register
683 * specified by @op.
685 * Note, the delays are in there as a caution to ensure that the reset
686 * has time to take effect and then complete. Since the datasheet does
687 * not currently specify the exact sequence, we have chosen something
688 * that seems to work with our device.
690 static void ks_soft_reset(struct ks_net *ks, unsigned op)
692 /* Disable interrupt first */
693 ks_wrreg16(ks, KS_IER, 0x0000);
694 ks_wrreg16(ks, KS_GRR, op);
695 mdelay(10); /* wait a short time to effect reset */
696 ks_wrreg16(ks, KS_GRR, 0);
697 mdelay(1); /* wait for condition to clear */
701 static void ks_enable_qmu(struct ks_net *ks)
703 u16 w;
705 w = ks_rdreg16(ks, KS_TXCR);
706 /* Enables QMU Transmit (TXCR). */
707 ks_wrreg16(ks, KS_TXCR, w | TXCR_TXE);
710 * RX Frame Count Threshold Enable and Auto-Dequeue RXQ Frame
711 * Enable
714 w = ks_rdreg16(ks, KS_RXQCR);
715 ks_wrreg16(ks, KS_RXQCR, w | RXQCR_RXFCTE);
717 /* Enables QMU Receive (RXCR1). */
718 w = ks_rdreg16(ks, KS_RXCR1);
719 ks_wrreg16(ks, KS_RXCR1, w | RXCR1_RXE);
720 ks->enabled = true;
721 } /* ks_enable_qmu */
723 static void ks_disable_qmu(struct ks_net *ks)
725 u16 w;
727 w = ks_rdreg16(ks, KS_TXCR);
729 /* Disables QMU Transmit (TXCR). */
730 w &= ~TXCR_TXE;
731 ks_wrreg16(ks, KS_TXCR, w);
733 /* Disables QMU Receive (RXCR1). */
734 w = ks_rdreg16(ks, KS_RXCR1);
735 w &= ~RXCR1_RXE ;
736 ks_wrreg16(ks, KS_RXCR1, w);
738 ks->enabled = false;
740 } /* ks_disable_qmu */
743 * ks_read_qmu - read 1 pkt data from the QMU.
744 * @ks: The chip information
745 * @buf: buffer address to save 1 pkt
746 * @len: Pkt length
747 * Here is the sequence to read 1 pkt:
748 * 1. set sudo DMA mode
749 * 2. read prepend data
750 * 3. read pkt data
751 * 4. reset sudo DMA Mode
753 static inline void ks_read_qmu(struct ks_net *ks, u16 *buf, u32 len)
755 u32 r = ks->extra_byte & 0x1 ;
756 u32 w = ks->extra_byte - r;
758 /* 1. set sudo DMA mode */
759 ks_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI);
760 ks_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
762 /* 2. read prepend data */
764 * read 4 + extra bytes and discard them.
765 * extra bytes for dummy, 2 for status, 2 for len
768 /* use likely(r) for 8 bit access for performance */
769 if (unlikely(r))
770 ioread8(ks->hw_addr);
771 ks_inblk(ks, buf, w + 2 + 2);
773 /* 3. read pkt data */
774 ks_inblk(ks, buf, ALIGN(len, 4));
776 /* 4. reset sudo DMA Mode */
777 ks_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
781 * ks_rcv - read multiple pkts data from the QMU.
782 * @ks: The chip information
783 * @netdev: The network device being opened.
785 * Read all of header information before reading pkt content.
786 * It is not allowed only port of pkts in QMU after issuing
787 * interrupt ack.
789 static void ks_rcv(struct ks_net *ks, struct net_device *netdev)
791 u32 i;
792 struct type_frame_head *frame_hdr = ks->frame_head_info;
793 struct sk_buff *skb;
795 ks->frame_cnt = ks_rdreg16(ks, KS_RXFCTR) >> 8;
797 /* read all header information */
798 for (i = 0; i < ks->frame_cnt; i++) {
799 /* Checking Received packet status */
800 frame_hdr->sts = ks_rdreg16(ks, KS_RXFHSR);
801 /* Get packet len from hardware */
802 frame_hdr->len = ks_rdreg16(ks, KS_RXFHBCR);
803 frame_hdr++;
806 frame_hdr = ks->frame_head_info;
807 while (ks->frame_cnt--) {
808 if (unlikely(!(frame_hdr->sts & RXFSHR_RXFV) ||
809 frame_hdr->len >= RX_BUF_SIZE ||
810 frame_hdr->len <= 0)) {
812 /* discard an invalid packet */
813 ks_wrreg16(ks, KS_RXQCR, (ks->rc_rxqcr | RXQCR_RRXEF));
814 netdev->stats.rx_dropped++;
815 if (!(frame_hdr->sts & RXFSHR_RXFV))
816 netdev->stats.rx_frame_errors++;
817 else
818 netdev->stats.rx_length_errors++;
819 frame_hdr++;
820 continue;
823 skb = netdev_alloc_skb(netdev, frame_hdr->len + 16);
824 if (likely(skb)) {
825 skb_reserve(skb, 2);
826 /* read data block including CRC 4 bytes */
827 ks_read_qmu(ks, (u16 *)skb->data, frame_hdr->len);
828 skb_put(skb, frame_hdr->len - 4);
829 skb->protocol = eth_type_trans(skb, netdev);
830 netif_rx(skb);
831 /* exclude CRC size */
832 netdev->stats.rx_bytes += frame_hdr->len - 4;
833 netdev->stats.rx_packets++;
834 } else {
835 ks_wrreg16(ks, KS_RXQCR, (ks->rc_rxqcr | RXQCR_RRXEF));
836 netdev->stats.rx_dropped++;
838 frame_hdr++;
843 * ks_update_link_status - link status update.
844 * @netdev: The network device being opened.
845 * @ks: The chip information
849 static void ks_update_link_status(struct net_device *netdev, struct ks_net *ks)
851 /* check the status of the link */
852 u32 link_up_status;
853 if (ks_rdreg16(ks, KS_P1SR) & P1SR_LINK_GOOD) {
854 netif_carrier_on(netdev);
855 link_up_status = true;
856 } else {
857 netif_carrier_off(netdev);
858 link_up_status = false;
860 netif_dbg(ks, link, ks->netdev,
861 "%s: %s\n", __func__, link_up_status ? "UP" : "DOWN");
865 * ks_irq - device interrupt handler
866 * @irq: Interrupt number passed from the IRQ handler.
867 * @pw: The private word passed to register_irq(), our struct ks_net.
869 * This is the handler invoked to find out what happened
871 * Read the interrupt status, work out what needs to be done and then clear
872 * any of the interrupts that are not needed.
875 static irqreturn_t ks_irq(int irq, void *pw)
877 struct net_device *netdev = pw;
878 struct ks_net *ks = netdev_priv(netdev);
879 unsigned long flags;
880 u16 status;
882 spin_lock_irqsave(&ks->statelock, flags);
883 /*this should be the first in IRQ handler */
884 ks_save_cmd_reg(ks);
886 status = ks_rdreg16(ks, KS_ISR);
887 if (unlikely(!status)) {
888 ks_restore_cmd_reg(ks);
889 spin_unlock_irqrestore(&ks->statelock, flags);
890 return IRQ_NONE;
893 ks_wrreg16(ks, KS_ISR, status);
895 if (likely(status & IRQ_RXI))
896 ks_rcv(ks, netdev);
898 if (unlikely(status & IRQ_LCI))
899 ks_update_link_status(netdev, ks);
901 if (unlikely(status & IRQ_TXI))
902 netif_wake_queue(netdev);
904 if (unlikely(status & IRQ_LDI)) {
906 u16 pmecr = ks_rdreg16(ks, KS_PMECR);
907 pmecr &= ~PMECR_WKEVT_MASK;
908 ks_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
911 if (unlikely(status & IRQ_RXOI))
912 ks->netdev->stats.rx_over_errors++;
913 /* this should be the last in IRQ handler*/
914 ks_restore_cmd_reg(ks);
915 spin_unlock_irqrestore(&ks->statelock, flags);
916 return IRQ_HANDLED;
921 * ks_net_open - open network device
922 * @netdev: The network device being opened.
924 * Called when the network device is marked active, such as a user executing
925 * 'ifconfig up' on the device.
927 static int ks_net_open(struct net_device *netdev)
929 struct ks_net *ks = netdev_priv(netdev);
930 int err;
932 #define KS_INT_FLAGS IRQF_TRIGGER_LOW
933 /* lock the card, even if we may not actually do anything
934 * else at the moment.
937 netif_dbg(ks, ifup, ks->netdev, "%s - entry\n", __func__);
939 /* reset the HW */
940 err = request_irq(netdev->irq, ks_irq, KS_INT_FLAGS, DRV_NAME, netdev);
942 if (err) {
943 pr_err("Failed to request IRQ: %d: %d\n", netdev->irq, err);
944 return err;
947 /* wake up powermode to normal mode */
948 ks_set_powermode(ks, PMECR_PM_NORMAL);
949 mdelay(1); /* wait for normal mode to take effect */
951 ks_wrreg16(ks, KS_ISR, 0xffff);
952 ks_enable_int(ks);
953 ks_enable_qmu(ks);
954 netif_start_queue(ks->netdev);
956 netif_dbg(ks, ifup, ks->netdev, "network device up\n");
958 return 0;
962 * ks_net_stop - close network device
963 * @netdev: The device being closed.
965 * Called to close down a network device which has been active. Cancell any
966 * work, shutdown the RX and TX process and then place the chip into a low
967 * power state whilst it is not being used.
969 static int ks_net_stop(struct net_device *netdev)
971 struct ks_net *ks = netdev_priv(netdev);
973 netif_info(ks, ifdown, netdev, "shutting down\n");
975 netif_stop_queue(netdev);
977 mutex_lock(&ks->lock);
979 /* turn off the IRQs and ack any outstanding */
980 ks_wrreg16(ks, KS_IER, 0x0000);
981 ks_wrreg16(ks, KS_ISR, 0xffff);
983 /* shutdown RX/TX QMU */
984 ks_disable_qmu(ks);
985 ks_disable_int(ks);
987 /* set powermode to soft power down to save power */
988 ks_set_powermode(ks, PMECR_PM_SOFTDOWN);
989 free_irq(netdev->irq, netdev);
990 mutex_unlock(&ks->lock);
991 return 0;
996 * ks_write_qmu - write 1 pkt data to the QMU.
997 * @ks: The chip information
998 * @pdata: buffer address to save 1 pkt
999 * @len: Pkt length in byte
1000 * Here is the sequence to write 1 pkt:
1001 * 1. set sudo DMA mode
1002 * 2. write status/length
1003 * 3. write pkt data
1004 * 4. reset sudo DMA Mode
1005 * 5. reset sudo DMA mode
1006 * 6. Wait until pkt is out
1008 static void ks_write_qmu(struct ks_net *ks, u8 *pdata, u16 len)
1010 /* start header at txb[0] to align txw entries */
1011 ks->txh.txw[0] = 0;
1012 ks->txh.txw[1] = cpu_to_le16(len);
1014 /* 1. set sudo-DMA mode */
1015 ks_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
1016 /* 2. write status/lenth info */
1017 ks_outblk(ks, ks->txh.txw, 4);
1018 /* 3. write pkt data */
1019 ks_outblk(ks, (u16 *)pdata, ALIGN(len, 4));
1020 /* 4. reset sudo-DMA mode */
1021 ks_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
1022 /* 5. Enqueue Tx(move the pkt from TX buffer into TXQ) */
1023 ks_wrreg16(ks, KS_TXQCR, TXQCR_METFE);
1024 /* 6. wait until TXQCR_METFE is auto-cleared */
1025 while (ks_rdreg16(ks, KS_TXQCR) & TXQCR_METFE)
1030 * ks_start_xmit - transmit packet
1031 * @skb : The buffer to transmit
1032 * @netdev : The device used to transmit the packet.
1034 * Called by the network layer to transmit the @skb.
1035 * spin_lock_irqsave is required because tx and rx should be mutual exclusive.
1036 * So while tx is in-progress, prevent IRQ interrupt from happenning.
1038 static netdev_tx_t ks_start_xmit(struct sk_buff *skb, struct net_device *netdev)
1040 netdev_tx_t retv = NETDEV_TX_OK;
1041 struct ks_net *ks = netdev_priv(netdev);
1042 unsigned long flags;
1044 spin_lock_irqsave(&ks->statelock, flags);
1046 /* Extra space are required:
1047 * 4 byte for alignment, 4 for status/length, 4 for CRC
1050 if (likely(ks_tx_fifo_space(ks) >= skb->len + 12)) {
1051 ks_write_qmu(ks, skb->data, skb->len);
1052 /* add tx statistics */
1053 netdev->stats.tx_bytes += skb->len;
1054 netdev->stats.tx_packets++;
1055 dev_kfree_skb(skb);
1056 } else
1057 retv = NETDEV_TX_BUSY;
1058 spin_unlock_irqrestore(&ks->statelock, flags);
1059 return retv;
1063 * ks_start_rx - ready to serve pkts
1064 * @ks : The chip information
1067 static void ks_start_rx(struct ks_net *ks)
1069 u16 cntl;
1071 /* Enables QMU Receive (RXCR1). */
1072 cntl = ks_rdreg16(ks, KS_RXCR1);
1073 cntl |= RXCR1_RXE ;
1074 ks_wrreg16(ks, KS_RXCR1, cntl);
1075 } /* ks_start_rx */
1078 * ks_stop_rx - stop to serve pkts
1079 * @ks : The chip information
1082 static void ks_stop_rx(struct ks_net *ks)
1084 u16 cntl;
1086 /* Disables QMU Receive (RXCR1). */
1087 cntl = ks_rdreg16(ks, KS_RXCR1);
1088 cntl &= ~RXCR1_RXE ;
1089 ks_wrreg16(ks, KS_RXCR1, cntl);
1091 } /* ks_stop_rx */
1093 static unsigned long const ethernet_polynomial = CRC32_POLY_BE;
1095 static unsigned long ether_gen_crc(int length, u8 *data)
1097 long crc = -1;
1098 while (--length >= 0) {
1099 u8 current_octet = *data++;
1100 int bit;
1102 for (bit = 0; bit < 8; bit++, current_octet >>= 1) {
1103 crc = (crc << 1) ^
1104 ((crc < 0) ^ (current_octet & 1) ?
1105 ethernet_polynomial : 0);
1108 return (unsigned long)crc;
1109 } /* ether_gen_crc */
1112 * ks_set_grpaddr - set multicast information
1113 * @ks : The chip information
1116 static void ks_set_grpaddr(struct ks_net *ks)
1118 u8 i;
1119 u32 index, position, value;
1121 memset(ks->mcast_bits, 0, sizeof(u8) * HW_MCAST_SIZE);
1123 for (i = 0; i < ks->mcast_lst_size; i++) {
1124 position = (ether_gen_crc(6, ks->mcast_lst[i]) >> 26) & 0x3f;
1125 index = position >> 3;
1126 value = 1 << (position & 7);
1127 ks->mcast_bits[index] |= (u8)value;
1130 for (i = 0; i < HW_MCAST_SIZE; i++) {
1131 if (i & 1) {
1132 ks_wrreg16(ks, (u16)((KS_MAHTR0 + i) & ~1),
1133 (ks->mcast_bits[i] << 8) |
1134 ks->mcast_bits[i - 1]);
1137 } /* ks_set_grpaddr */
1140 * ks_clear_mcast - clear multicast information
1142 * @ks : The chip information
1143 * This routine removes all mcast addresses set in the hardware.
1146 static void ks_clear_mcast(struct ks_net *ks)
1148 u16 i, mcast_size;
1149 for (i = 0; i < HW_MCAST_SIZE; i++)
1150 ks->mcast_bits[i] = 0;
1152 mcast_size = HW_MCAST_SIZE >> 2;
1153 for (i = 0; i < mcast_size; i++)
1154 ks_wrreg16(ks, KS_MAHTR0 + (2*i), 0);
1157 static void ks_set_promis(struct ks_net *ks, u16 promiscuous_mode)
1159 u16 cntl;
1160 ks->promiscuous = promiscuous_mode;
1161 ks_stop_rx(ks); /* Stop receiving for reconfiguration */
1162 cntl = ks_rdreg16(ks, KS_RXCR1);
1164 cntl &= ~RXCR1_FILTER_MASK;
1165 if (promiscuous_mode)
1166 /* Enable Promiscuous mode */
1167 cntl |= RXCR1_RXAE | RXCR1_RXINVF;
1168 else
1169 /* Disable Promiscuous mode (default normal mode) */
1170 cntl |= RXCR1_RXPAFMA;
1172 ks_wrreg16(ks, KS_RXCR1, cntl);
1174 if (ks->enabled)
1175 ks_start_rx(ks);
1177 } /* ks_set_promis */
1179 static void ks_set_mcast(struct ks_net *ks, u16 mcast)
1181 u16 cntl;
1183 ks->all_mcast = mcast;
1184 ks_stop_rx(ks); /* Stop receiving for reconfiguration */
1185 cntl = ks_rdreg16(ks, KS_RXCR1);
1186 cntl &= ~RXCR1_FILTER_MASK;
1187 if (mcast)
1188 /* Enable "Perfect with Multicast address passed mode" */
1189 cntl |= (RXCR1_RXAE | RXCR1_RXMAFMA | RXCR1_RXPAFMA);
1190 else
1192 * Disable "Perfect with Multicast address passed
1193 * mode" (normal mode).
1195 cntl |= RXCR1_RXPAFMA;
1197 ks_wrreg16(ks, KS_RXCR1, cntl);
1199 if (ks->enabled)
1200 ks_start_rx(ks);
1201 } /* ks_set_mcast */
1203 static void ks_set_rx_mode(struct net_device *netdev)
1205 struct ks_net *ks = netdev_priv(netdev);
1206 struct netdev_hw_addr *ha;
1208 /* Turn on/off promiscuous mode. */
1209 if ((netdev->flags & IFF_PROMISC) == IFF_PROMISC)
1210 ks_set_promis(ks,
1211 (u16)((netdev->flags & IFF_PROMISC) == IFF_PROMISC));
1212 /* Turn on/off all mcast mode. */
1213 else if ((netdev->flags & IFF_ALLMULTI) == IFF_ALLMULTI)
1214 ks_set_mcast(ks,
1215 (u16)((netdev->flags & IFF_ALLMULTI) == IFF_ALLMULTI));
1216 else
1217 ks_set_promis(ks, false);
1219 if ((netdev->flags & IFF_MULTICAST) && netdev_mc_count(netdev)) {
1220 if (netdev_mc_count(netdev) <= MAX_MCAST_LST) {
1221 int i = 0;
1223 netdev_for_each_mc_addr(ha, netdev) {
1224 if (i >= MAX_MCAST_LST)
1225 break;
1226 memcpy(ks->mcast_lst[i++], ha->addr, ETH_ALEN);
1228 ks->mcast_lst_size = (u8)i;
1229 ks_set_grpaddr(ks);
1230 } else {
1232 * List too big to support so
1233 * turn on all mcast mode.
1235 ks->mcast_lst_size = MAX_MCAST_LST;
1236 ks_set_mcast(ks, true);
1238 } else {
1239 ks->mcast_lst_size = 0;
1240 ks_clear_mcast(ks);
1242 } /* ks_set_rx_mode */
1244 static void ks_set_mac(struct ks_net *ks, u8 *data)
1246 u16 *pw = (u16 *)data;
1247 u16 w, u;
1249 ks_stop_rx(ks); /* Stop receiving for reconfiguration */
1251 u = *pw++;
1252 w = ((u & 0xFF) << 8) | ((u >> 8) & 0xFF);
1253 ks_wrreg16(ks, KS_MARH, w);
1255 u = *pw++;
1256 w = ((u & 0xFF) << 8) | ((u >> 8) & 0xFF);
1257 ks_wrreg16(ks, KS_MARM, w);
1259 u = *pw;
1260 w = ((u & 0xFF) << 8) | ((u >> 8) & 0xFF);
1261 ks_wrreg16(ks, KS_MARL, w);
1263 memcpy(ks->mac_addr, data, ETH_ALEN);
1265 if (ks->enabled)
1266 ks_start_rx(ks);
1269 static int ks_set_mac_address(struct net_device *netdev, void *paddr)
1271 struct ks_net *ks = netdev_priv(netdev);
1272 struct sockaddr *addr = paddr;
1273 u8 *da;
1275 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1277 da = (u8 *)netdev->dev_addr;
1279 ks_set_mac(ks, da);
1280 return 0;
1283 static int ks_net_ioctl(struct net_device *netdev, struct ifreq *req, int cmd)
1285 struct ks_net *ks = netdev_priv(netdev);
1287 if (!netif_running(netdev))
1288 return -EINVAL;
1290 return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
1293 static const struct net_device_ops ks_netdev_ops = {
1294 .ndo_open = ks_net_open,
1295 .ndo_stop = ks_net_stop,
1296 .ndo_do_ioctl = ks_net_ioctl,
1297 .ndo_start_xmit = ks_start_xmit,
1298 .ndo_set_mac_address = ks_set_mac_address,
1299 .ndo_set_rx_mode = ks_set_rx_mode,
1300 .ndo_validate_addr = eth_validate_addr,
1303 /* ethtool support */
1305 static void ks_get_drvinfo(struct net_device *netdev,
1306 struct ethtool_drvinfo *di)
1308 strlcpy(di->driver, DRV_NAME, sizeof(di->driver));
1309 strlcpy(di->version, "1.00", sizeof(di->version));
1310 strlcpy(di->bus_info, dev_name(netdev->dev.parent),
1311 sizeof(di->bus_info));
1314 static u32 ks_get_msglevel(struct net_device *netdev)
1316 struct ks_net *ks = netdev_priv(netdev);
1317 return ks->msg_enable;
1320 static void ks_set_msglevel(struct net_device *netdev, u32 to)
1322 struct ks_net *ks = netdev_priv(netdev);
1323 ks->msg_enable = to;
1326 static int ks_get_link_ksettings(struct net_device *netdev,
1327 struct ethtool_link_ksettings *cmd)
1329 struct ks_net *ks = netdev_priv(netdev);
1331 mii_ethtool_get_link_ksettings(&ks->mii, cmd);
1333 return 0;
1336 static int ks_set_link_ksettings(struct net_device *netdev,
1337 const struct ethtool_link_ksettings *cmd)
1339 struct ks_net *ks = netdev_priv(netdev);
1340 return mii_ethtool_set_link_ksettings(&ks->mii, cmd);
1343 static u32 ks_get_link(struct net_device *netdev)
1345 struct ks_net *ks = netdev_priv(netdev);
1346 return mii_link_ok(&ks->mii);
1349 static int ks_nway_reset(struct net_device *netdev)
1351 struct ks_net *ks = netdev_priv(netdev);
1352 return mii_nway_restart(&ks->mii);
1355 static const struct ethtool_ops ks_ethtool_ops = {
1356 .get_drvinfo = ks_get_drvinfo,
1357 .get_msglevel = ks_get_msglevel,
1358 .set_msglevel = ks_set_msglevel,
1359 .get_link = ks_get_link,
1360 .nway_reset = ks_nway_reset,
1361 .get_link_ksettings = ks_get_link_ksettings,
1362 .set_link_ksettings = ks_set_link_ksettings,
1365 /* MII interface controls */
1368 * ks_phy_reg - convert MII register into a KS8851 register
1369 * @reg: MII register number.
1371 * Return the KS8851 register number for the corresponding MII PHY register
1372 * if possible. Return zero if the MII register has no direct mapping to the
1373 * KS8851 register set.
1375 static int ks_phy_reg(int reg)
1377 switch (reg) {
1378 case MII_BMCR:
1379 return KS_P1MBCR;
1380 case MII_BMSR:
1381 return KS_P1MBSR;
1382 case MII_PHYSID1:
1383 return KS_PHY1ILR;
1384 case MII_PHYSID2:
1385 return KS_PHY1IHR;
1386 case MII_ADVERTISE:
1387 return KS_P1ANAR;
1388 case MII_LPA:
1389 return KS_P1ANLPR;
1392 return 0x0;
1396 * ks_phy_read - MII interface PHY register read.
1397 * @netdev: The network device the PHY is on.
1398 * @phy_addr: Address of PHY (ignored as we only have one)
1399 * @reg: The register to read.
1401 * This call reads data from the PHY register specified in @reg. Since the
1402 * device does not support all the MII registers, the non-existent values
1403 * are always returned as zero.
1405 * We return zero for unsupported registers as the MII code does not check
1406 * the value returned for any error status, and simply returns it to the
1407 * caller. The mii-tool that the driver was tested with takes any -ve error
1408 * as real PHY capabilities, thus displaying incorrect data to the user.
1410 static int ks_phy_read(struct net_device *netdev, int phy_addr, int reg)
1412 struct ks_net *ks = netdev_priv(netdev);
1413 int ksreg;
1414 int result;
1416 ksreg = ks_phy_reg(reg);
1417 if (!ksreg)
1418 return 0x0; /* no error return allowed, so use zero */
1420 mutex_lock(&ks->lock);
1421 result = ks_rdreg16(ks, ksreg);
1422 mutex_unlock(&ks->lock);
1424 return result;
1427 static void ks_phy_write(struct net_device *netdev,
1428 int phy, int reg, int value)
1430 struct ks_net *ks = netdev_priv(netdev);
1431 int ksreg;
1433 ksreg = ks_phy_reg(reg);
1434 if (ksreg) {
1435 mutex_lock(&ks->lock);
1436 ks_wrreg16(ks, ksreg, value);
1437 mutex_unlock(&ks->lock);
1442 * ks_read_selftest - read the selftest memory info.
1443 * @ks: The device state
1445 * Read and check the TX/RX memory selftest information.
1447 static int ks_read_selftest(struct ks_net *ks)
1449 unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
1450 int ret = 0;
1451 unsigned rd;
1453 rd = ks_rdreg16(ks, KS_MBIR);
1455 if ((rd & both_done) != both_done) {
1456 netdev_warn(ks->netdev, "Memory selftest not finished\n");
1457 return 0;
1460 if (rd & MBIR_TXMBFA) {
1461 netdev_err(ks->netdev, "TX memory selftest fails\n");
1462 ret |= 1;
1465 if (rd & MBIR_RXMBFA) {
1466 netdev_err(ks->netdev, "RX memory selftest fails\n");
1467 ret |= 2;
1470 netdev_info(ks->netdev, "the selftest passes\n");
1471 return ret;
1474 static void ks_setup(struct ks_net *ks)
1476 u16 w;
1479 * Configure QMU Transmit
1482 /* Setup Transmit Frame Data Pointer Auto-Increment (TXFDPR) */
1483 ks_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
1485 /* Setup Receive Frame Data Pointer Auto-Increment */
1486 ks_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI);
1488 /* Setup Receive Frame Threshold - 1 frame (RXFCTFC) */
1489 ks_wrreg16(ks, KS_RXFCTR, 1 & RXFCTR_THRESHOLD_MASK);
1491 /* Setup RxQ Command Control (RXQCR) */
1492 ks->rc_rxqcr = RXQCR_CMD_CNTL;
1493 ks_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
1496 * set the force mode to half duplex, default is full duplex
1497 * because if the auto-negotiation fails, most switch uses
1498 * half-duplex.
1501 w = ks_rdreg16(ks, KS_P1MBCR);
1502 w &= ~P1MBCR_FORCE_FDX;
1503 ks_wrreg16(ks, KS_P1MBCR, w);
1505 w = TXCR_TXFCE | TXCR_TXPE | TXCR_TXCRC | TXCR_TCGIP;
1506 ks_wrreg16(ks, KS_TXCR, w);
1508 w = RXCR1_RXFCE | RXCR1_RXBE | RXCR1_RXUE | RXCR1_RXME | RXCR1_RXIPFCC;
1510 if (ks->promiscuous) /* bPromiscuous */
1511 w |= (RXCR1_RXAE | RXCR1_RXINVF);
1512 else if (ks->all_mcast) /* Multicast address passed mode */
1513 w |= (RXCR1_RXAE | RXCR1_RXMAFMA | RXCR1_RXPAFMA);
1514 else /* Normal mode */
1515 w |= RXCR1_RXPAFMA;
1517 ks_wrreg16(ks, KS_RXCR1, w);
1518 } /*ks_setup */
1521 static void ks_setup_int(struct ks_net *ks)
1523 ks->rc_ier = 0x00;
1524 /* Clear the interrupts status of the hardware. */
1525 ks_wrreg16(ks, KS_ISR, 0xffff);
1527 /* Enables the interrupts of the hardware. */
1528 ks->rc_ier = (IRQ_LCI | IRQ_TXI | IRQ_RXI);
1529 } /* ks_setup_int */
1531 static int ks_hw_init(struct ks_net *ks)
1533 #define MHEADER_SIZE (sizeof(struct type_frame_head) * MAX_RECV_FRAMES)
1534 ks->promiscuous = 0;
1535 ks->all_mcast = 0;
1536 ks->mcast_lst_size = 0;
1538 ks->frame_head_info = devm_kmalloc(&ks->pdev->dev, MHEADER_SIZE,
1539 GFP_KERNEL);
1540 if (!ks->frame_head_info)
1541 return false;
1543 ks_set_mac(ks, KS_DEFAULT_MAC_ADDRESS);
1544 return true;
1547 #if defined(CONFIG_OF)
1548 static const struct of_device_id ks8851_ml_dt_ids[] = {
1549 { .compatible = "micrel,ks8851-mll" },
1550 { /* sentinel */ }
1552 MODULE_DEVICE_TABLE(of, ks8851_ml_dt_ids);
1553 #endif
1555 static int ks8851_probe(struct platform_device *pdev)
1557 int err;
1558 struct resource *io_d, *io_c;
1559 struct net_device *netdev;
1560 struct ks_net *ks;
1561 u16 id, data;
1562 const char *mac;
1564 netdev = alloc_etherdev(sizeof(struct ks_net));
1565 if (!netdev)
1566 return -ENOMEM;
1568 SET_NETDEV_DEV(netdev, &pdev->dev);
1570 ks = netdev_priv(netdev);
1571 ks->netdev = netdev;
1573 io_d = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1574 ks->hw_addr = devm_ioremap_resource(&pdev->dev, io_d);
1575 if (IS_ERR(ks->hw_addr)) {
1576 err = PTR_ERR(ks->hw_addr);
1577 goto err_free;
1580 io_c = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1581 ks->hw_addr_cmd = devm_ioremap_resource(&pdev->dev, io_c);
1582 if (IS_ERR(ks->hw_addr_cmd)) {
1583 err = PTR_ERR(ks->hw_addr_cmd);
1584 goto err_free;
1587 err = ks_check_endian(ks);
1588 if (err)
1589 goto err_free;
1591 netdev->irq = platform_get_irq(pdev, 0);
1593 if ((int)netdev->irq < 0) {
1594 err = netdev->irq;
1595 goto err_free;
1598 ks->pdev = pdev;
1600 mutex_init(&ks->lock);
1601 spin_lock_init(&ks->statelock);
1603 netdev->netdev_ops = &ks_netdev_ops;
1604 netdev->ethtool_ops = &ks_ethtool_ops;
1606 /* setup mii state */
1607 ks->mii.dev = netdev;
1608 ks->mii.phy_id = 1,
1609 ks->mii.phy_id_mask = 1;
1610 ks->mii.reg_num_mask = 0xf;
1611 ks->mii.mdio_read = ks_phy_read;
1612 ks->mii.mdio_write = ks_phy_write;
1614 netdev_info(netdev, "message enable is %d\n", msg_enable);
1615 /* set the default message enable */
1616 ks->msg_enable = netif_msg_init(msg_enable, (NETIF_MSG_DRV |
1617 NETIF_MSG_PROBE |
1618 NETIF_MSG_LINK));
1619 ks_read_config(ks);
1621 /* simple check for a valid chip being connected to the bus */
1622 if ((ks_rdreg16(ks, KS_CIDER) & ~CIDER_REV_MASK) != CIDER_ID) {
1623 netdev_err(netdev, "failed to read device ID\n");
1624 err = -ENODEV;
1625 goto err_free;
1628 if (ks_read_selftest(ks)) {
1629 netdev_err(netdev, "failed to read device ID\n");
1630 err = -ENODEV;
1631 goto err_free;
1634 err = register_netdev(netdev);
1635 if (err)
1636 goto err_free;
1638 platform_set_drvdata(pdev, netdev);
1640 ks_soft_reset(ks, GRR_GSR);
1641 ks_hw_init(ks);
1642 ks_disable_qmu(ks);
1643 ks_setup(ks);
1644 ks_setup_int(ks);
1646 data = ks_rdreg16(ks, KS_OBCR);
1647 ks_wrreg16(ks, KS_OBCR, data | OBCR_ODS_16MA);
1649 /* overwriting the default MAC address */
1650 if (pdev->dev.of_node) {
1651 mac = of_get_mac_address(pdev->dev.of_node);
1652 if (mac)
1653 memcpy(ks->mac_addr, mac, ETH_ALEN);
1654 } else {
1655 struct ks8851_mll_platform_data *pdata;
1657 pdata = dev_get_platdata(&pdev->dev);
1658 if (!pdata) {
1659 netdev_err(netdev, "No platform data\n");
1660 err = -ENODEV;
1661 goto err_pdata;
1663 memcpy(ks->mac_addr, pdata->mac_addr, ETH_ALEN);
1665 if (!is_valid_ether_addr(ks->mac_addr)) {
1666 /* Use random MAC address if none passed */
1667 eth_random_addr(ks->mac_addr);
1668 netdev_info(netdev, "Using random mac address\n");
1670 netdev_info(netdev, "Mac address is: %pM\n", ks->mac_addr);
1672 memcpy(netdev->dev_addr, ks->mac_addr, ETH_ALEN);
1674 ks_set_mac(ks, netdev->dev_addr);
1676 id = ks_rdreg16(ks, KS_CIDER);
1678 netdev_info(netdev, "Found chip, family: 0x%x, id: 0x%x, rev: 0x%x\n",
1679 (id >> 8) & 0xff, (id >> 4) & 0xf, (id >> 1) & 0x7);
1680 return 0;
1682 err_pdata:
1683 unregister_netdev(netdev);
1684 err_free:
1685 free_netdev(netdev);
1686 return err;
1689 static int ks8851_remove(struct platform_device *pdev)
1691 struct net_device *netdev = platform_get_drvdata(pdev);
1693 unregister_netdev(netdev);
1694 free_netdev(netdev);
1695 return 0;
1699 static struct platform_driver ks8851_platform_driver = {
1700 .driver = {
1701 .name = DRV_NAME,
1702 .of_match_table = of_match_ptr(ks8851_ml_dt_ids),
1704 .probe = ks8851_probe,
1705 .remove = ks8851_remove,
1708 module_platform_driver(ks8851_platform_driver);
1710 MODULE_DESCRIPTION("KS8851 MLL Network driver");
1711 MODULE_AUTHOR("David Choi <david.choi@micrel.com>");
1712 MODULE_LICENSE("GPL");
1713 module_param_named(message, msg_enable, int, 0);
1714 MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");