x86/amd-iommu: Add per IOMMU reference counting
[linux/fpc-iii.git] / drivers / net / ks8851_mll.c
blob0be14d702bebcbb53aae6d5001ecd3ffa2a310e7
1 /**
2 * drivers/net/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 /**
20 * Supports:
21 * KS8851 16bit MLL chip from Micrel Inc.
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/netdevice.h>
27 #include <linux/etherdevice.h>
28 #include <linux/ethtool.h>
29 #include <linux/cache.h>
30 #include <linux/crc32.h>
31 #include <linux/mii.h>
32 #include <linux/platform_device.h>
33 #include <linux/delay.h>
35 #define DRV_NAME "ks8851_mll"
37 static u8 KS_DEFAULT_MAC_ADDRESS[] = { 0x00, 0x10, 0xA1, 0x86, 0x95, 0x11 };
38 #define MAX_RECV_FRAMES 32
39 #define MAX_BUF_SIZE 2048
40 #define TX_BUF_SIZE 2000
41 #define RX_BUF_SIZE 2000
43 #define KS_CCR 0x08
44 #define CCR_EEPROM (1 << 9)
45 #define CCR_SPI (1 << 8)
46 #define CCR_8BIT (1 << 7)
47 #define CCR_16BIT (1 << 6)
48 #define CCR_32BIT (1 << 5)
49 #define CCR_SHARED (1 << 4)
50 #define CCR_32PIN (1 << 0)
52 /* MAC address registers */
53 #define KS_MARL 0x10
54 #define KS_MARM 0x12
55 #define KS_MARH 0x14
57 #define KS_OBCR 0x20
58 #define OBCR_ODS_16MA (1 << 6)
60 #define KS_EEPCR 0x22
61 #define EEPCR_EESA (1 << 4)
62 #define EEPCR_EESB (1 << 3)
63 #define EEPCR_EEDO (1 << 2)
64 #define EEPCR_EESCK (1 << 1)
65 #define EEPCR_EECS (1 << 0)
67 #define KS_MBIR 0x24
68 #define MBIR_TXMBF (1 << 12)
69 #define MBIR_TXMBFA (1 << 11)
70 #define MBIR_RXMBF (1 << 4)
71 #define MBIR_RXMBFA (1 << 3)
73 #define KS_GRR 0x26
74 #define GRR_QMU (1 << 1)
75 #define GRR_GSR (1 << 0)
77 #define KS_WFCR 0x2A
78 #define WFCR_MPRXE (1 << 7)
79 #define WFCR_WF3E (1 << 3)
80 #define WFCR_WF2E (1 << 2)
81 #define WFCR_WF1E (1 << 1)
82 #define WFCR_WF0E (1 << 0)
84 #define KS_WF0CRC0 0x30
85 #define KS_WF0CRC1 0x32
86 #define KS_WF0BM0 0x34
87 #define KS_WF0BM1 0x36
88 #define KS_WF0BM2 0x38
89 #define KS_WF0BM3 0x3A
91 #define KS_WF1CRC0 0x40
92 #define KS_WF1CRC1 0x42
93 #define KS_WF1BM0 0x44
94 #define KS_WF1BM1 0x46
95 #define KS_WF1BM2 0x48
96 #define KS_WF1BM3 0x4A
98 #define KS_WF2CRC0 0x50
99 #define KS_WF2CRC1 0x52
100 #define KS_WF2BM0 0x54
101 #define KS_WF2BM1 0x56
102 #define KS_WF2BM2 0x58
103 #define KS_WF2BM3 0x5A
105 #define KS_WF3CRC0 0x60
106 #define KS_WF3CRC1 0x62
107 #define KS_WF3BM0 0x64
108 #define KS_WF3BM1 0x66
109 #define KS_WF3BM2 0x68
110 #define KS_WF3BM3 0x6A
112 #define KS_TXCR 0x70
113 #define TXCR_TCGICMP (1 << 8)
114 #define TXCR_TCGUDP (1 << 7)
115 #define TXCR_TCGTCP (1 << 6)
116 #define TXCR_TCGIP (1 << 5)
117 #define TXCR_FTXQ (1 << 4)
118 #define TXCR_TXFCE (1 << 3)
119 #define TXCR_TXPE (1 << 2)
120 #define TXCR_TXCRC (1 << 1)
121 #define TXCR_TXE (1 << 0)
123 #define KS_TXSR 0x72
124 #define TXSR_TXLC (1 << 13)
125 #define TXSR_TXMC (1 << 12)
126 #define TXSR_TXFID_MASK (0x3f << 0)
127 #define TXSR_TXFID_SHIFT (0)
128 #define TXSR_TXFID_GET(_v) (((_v) >> 0) & 0x3f)
131 #define KS_RXCR1 0x74
132 #define RXCR1_FRXQ (1 << 15)
133 #define RXCR1_RXUDPFCC (1 << 14)
134 #define RXCR1_RXTCPFCC (1 << 13)
135 #define RXCR1_RXIPFCC (1 << 12)
136 #define RXCR1_RXPAFMA (1 << 11)
137 #define RXCR1_RXFCE (1 << 10)
138 #define RXCR1_RXEFE (1 << 9)
139 #define RXCR1_RXMAFMA (1 << 8)
140 #define RXCR1_RXBE (1 << 7)
141 #define RXCR1_RXME (1 << 6)
142 #define RXCR1_RXUE (1 << 5)
143 #define RXCR1_RXAE (1 << 4)
144 #define RXCR1_RXINVF (1 << 1)
145 #define RXCR1_RXE (1 << 0)
146 #define RXCR1_FILTER_MASK (RXCR1_RXINVF | RXCR1_RXAE | \
147 RXCR1_RXMAFMA | RXCR1_RXPAFMA)
149 #define KS_RXCR2 0x76
150 #define RXCR2_SRDBL_MASK (0x7 << 5)
151 #define RXCR2_SRDBL_SHIFT (5)
152 #define RXCR2_SRDBL_4B (0x0 << 5)
153 #define RXCR2_SRDBL_8B (0x1 << 5)
154 #define RXCR2_SRDBL_16B (0x2 << 5)
155 #define RXCR2_SRDBL_32B (0x3 << 5)
156 /* #define RXCR2_SRDBL_FRAME (0x4 << 5) */
157 #define RXCR2_IUFFP (1 << 4)
158 #define RXCR2_RXIUFCEZ (1 << 3)
159 #define RXCR2_UDPLFE (1 << 2)
160 #define RXCR2_RXICMPFCC (1 << 1)
161 #define RXCR2_RXSAF (1 << 0)
163 #define KS_TXMIR 0x78
165 #define KS_RXFHSR 0x7C
166 #define RXFSHR_RXFV (1 << 15)
167 #define RXFSHR_RXICMPFCS (1 << 13)
168 #define RXFSHR_RXIPFCS (1 << 12)
169 #define RXFSHR_RXTCPFCS (1 << 11)
170 #define RXFSHR_RXUDPFCS (1 << 10)
171 #define RXFSHR_RXBF (1 << 7)
172 #define RXFSHR_RXMF (1 << 6)
173 #define RXFSHR_RXUF (1 << 5)
174 #define RXFSHR_RXMR (1 << 4)
175 #define RXFSHR_RXFT (1 << 3)
176 #define RXFSHR_RXFTL (1 << 2)
177 #define RXFSHR_RXRF (1 << 1)
178 #define RXFSHR_RXCE (1 << 0)
179 #define RXFSHR_ERR (RXFSHR_RXCE | RXFSHR_RXRF |\
180 RXFSHR_RXFTL | RXFSHR_RXMR |\
181 RXFSHR_RXICMPFCS | RXFSHR_RXIPFCS |\
182 RXFSHR_RXTCPFCS)
183 #define KS_RXFHBCR 0x7E
184 #define RXFHBCR_CNT_MASK 0x0FFF
186 #define KS_TXQCR 0x80
187 #define TXQCR_AETFE (1 << 2)
188 #define TXQCR_TXQMAM (1 << 1)
189 #define TXQCR_METFE (1 << 0)
191 #define KS_RXQCR 0x82
192 #define RXQCR_RXDTTS (1 << 12)
193 #define RXQCR_RXDBCTS (1 << 11)
194 #define RXQCR_RXFCTS (1 << 10)
195 #define RXQCR_RXIPHTOE (1 << 9)
196 #define RXQCR_RXDTTE (1 << 7)
197 #define RXQCR_RXDBCTE (1 << 6)
198 #define RXQCR_RXFCTE (1 << 5)
199 #define RXQCR_ADRFE (1 << 4)
200 #define RXQCR_SDA (1 << 3)
201 #define RXQCR_RRXEF (1 << 0)
202 #define RXQCR_CMD_CNTL (RXQCR_RXFCTE|RXQCR_ADRFE)
204 #define KS_TXFDPR 0x84
205 #define TXFDPR_TXFPAI (1 << 14)
206 #define TXFDPR_TXFP_MASK (0x7ff << 0)
207 #define TXFDPR_TXFP_SHIFT (0)
209 #define KS_RXFDPR 0x86
210 #define RXFDPR_RXFPAI (1 << 14)
212 #define KS_RXDTTR 0x8C
213 #define KS_RXDBCTR 0x8E
215 #define KS_IER 0x90
216 #define KS_ISR 0x92
217 #define IRQ_LCI (1 << 15)
218 #define IRQ_TXI (1 << 14)
219 #define IRQ_RXI (1 << 13)
220 #define IRQ_RXOI (1 << 11)
221 #define IRQ_TXPSI (1 << 9)
222 #define IRQ_RXPSI (1 << 8)
223 #define IRQ_TXSAI (1 << 6)
224 #define IRQ_RXWFDI (1 << 5)
225 #define IRQ_RXMPDI (1 << 4)
226 #define IRQ_LDI (1 << 3)
227 #define IRQ_EDI (1 << 2)
228 #define IRQ_SPIBEI (1 << 1)
229 #define IRQ_DEDI (1 << 0)
231 #define KS_RXFCTR 0x9C
232 #define RXFCTR_THRESHOLD_MASK 0x00FF
234 #define KS_RXFC 0x9D
235 #define RXFCTR_RXFC_MASK (0xff << 8)
236 #define RXFCTR_RXFC_SHIFT (8)
237 #define RXFCTR_RXFC_GET(_v) (((_v) >> 8) & 0xff)
238 #define RXFCTR_RXFCT_MASK (0xff << 0)
239 #define RXFCTR_RXFCT_SHIFT (0)
241 #define KS_TXNTFSR 0x9E
243 #define KS_MAHTR0 0xA0
244 #define KS_MAHTR1 0xA2
245 #define KS_MAHTR2 0xA4
246 #define KS_MAHTR3 0xA6
248 #define KS_FCLWR 0xB0
249 #define KS_FCHWR 0xB2
250 #define KS_FCOWR 0xB4
252 #define KS_CIDER 0xC0
253 #define CIDER_ID 0x8870
254 #define CIDER_REV_MASK (0x7 << 1)
255 #define CIDER_REV_SHIFT (1)
256 #define CIDER_REV_GET(_v) (((_v) >> 1) & 0x7)
258 #define KS_CGCR 0xC6
259 #define KS_IACR 0xC8
260 #define IACR_RDEN (1 << 12)
261 #define IACR_TSEL_MASK (0x3 << 10)
262 #define IACR_TSEL_SHIFT (10)
263 #define IACR_TSEL_MIB (0x3 << 10)
264 #define IACR_ADDR_MASK (0x1f << 0)
265 #define IACR_ADDR_SHIFT (0)
267 #define KS_IADLR 0xD0
268 #define KS_IAHDR 0xD2
270 #define KS_PMECR 0xD4
271 #define PMECR_PME_DELAY (1 << 14)
272 #define PMECR_PME_POL (1 << 12)
273 #define PMECR_WOL_WAKEUP (1 << 11)
274 #define PMECR_WOL_MAGICPKT (1 << 10)
275 #define PMECR_WOL_LINKUP (1 << 9)
276 #define PMECR_WOL_ENERGY (1 << 8)
277 #define PMECR_AUTO_WAKE_EN (1 << 7)
278 #define PMECR_WAKEUP_NORMAL (1 << 6)
279 #define PMECR_WKEVT_MASK (0xf << 2)
280 #define PMECR_WKEVT_SHIFT (2)
281 #define PMECR_WKEVT_GET(_v) (((_v) >> 2) & 0xf)
282 #define PMECR_WKEVT_ENERGY (0x1 << 2)
283 #define PMECR_WKEVT_LINK (0x2 << 2)
284 #define PMECR_WKEVT_MAGICPKT (0x4 << 2)
285 #define PMECR_WKEVT_FRAME (0x8 << 2)
286 #define PMECR_PM_MASK (0x3 << 0)
287 #define PMECR_PM_SHIFT (0)
288 #define PMECR_PM_NORMAL (0x0 << 0)
289 #define PMECR_PM_ENERGY (0x1 << 0)
290 #define PMECR_PM_SOFTDOWN (0x2 << 0)
291 #define PMECR_PM_POWERSAVE (0x3 << 0)
293 /* Standard MII PHY data */
294 #define KS_P1MBCR 0xE4
295 #define P1MBCR_FORCE_FDX (1 << 8)
297 #define KS_P1MBSR 0xE6
298 #define P1MBSR_AN_COMPLETE (1 << 5)
299 #define P1MBSR_AN_CAPABLE (1 << 3)
300 #define P1MBSR_LINK_UP (1 << 2)
302 #define KS_PHY1ILR 0xE8
303 #define KS_PHY1IHR 0xEA
304 #define KS_P1ANAR 0xEC
305 #define KS_P1ANLPR 0xEE
307 #define KS_P1SCLMD 0xF4
308 #define P1SCLMD_LEDOFF (1 << 15)
309 #define P1SCLMD_TXIDS (1 << 14)
310 #define P1SCLMD_RESTARTAN (1 << 13)
311 #define P1SCLMD_DISAUTOMDIX (1 << 10)
312 #define P1SCLMD_FORCEMDIX (1 << 9)
313 #define P1SCLMD_AUTONEGEN (1 << 7)
314 #define P1SCLMD_FORCE100 (1 << 6)
315 #define P1SCLMD_FORCEFDX (1 << 5)
316 #define P1SCLMD_ADV_FLOW (1 << 4)
317 #define P1SCLMD_ADV_100BT_FDX (1 << 3)
318 #define P1SCLMD_ADV_100BT_HDX (1 << 2)
319 #define P1SCLMD_ADV_10BT_FDX (1 << 1)
320 #define P1SCLMD_ADV_10BT_HDX (1 << 0)
322 #define KS_P1CR 0xF6
323 #define P1CR_HP_MDIX (1 << 15)
324 #define P1CR_REV_POL (1 << 13)
325 #define P1CR_OP_100M (1 << 10)
326 #define P1CR_OP_FDX (1 << 9)
327 #define P1CR_OP_MDI (1 << 7)
328 #define P1CR_AN_DONE (1 << 6)
329 #define P1CR_LINK_GOOD (1 << 5)
330 #define P1CR_PNTR_FLOW (1 << 4)
331 #define P1CR_PNTR_100BT_FDX (1 << 3)
332 #define P1CR_PNTR_100BT_HDX (1 << 2)
333 #define P1CR_PNTR_10BT_FDX (1 << 1)
334 #define P1CR_PNTR_10BT_HDX (1 << 0)
336 /* TX Frame control */
338 #define TXFR_TXIC (1 << 15)
339 #define TXFR_TXFID_MASK (0x3f << 0)
340 #define TXFR_TXFID_SHIFT (0)
342 #define KS_P1SR 0xF8
343 #define P1SR_HP_MDIX (1 << 15)
344 #define P1SR_REV_POL (1 << 13)
345 #define P1SR_OP_100M (1 << 10)
346 #define P1SR_OP_FDX (1 << 9)
347 #define P1SR_OP_MDI (1 << 7)
348 #define P1SR_AN_DONE (1 << 6)
349 #define P1SR_LINK_GOOD (1 << 5)
350 #define P1SR_PNTR_FLOW (1 << 4)
351 #define P1SR_PNTR_100BT_FDX (1 << 3)
352 #define P1SR_PNTR_100BT_HDX (1 << 2)
353 #define P1SR_PNTR_10BT_FDX (1 << 1)
354 #define P1SR_PNTR_10BT_HDX (1 << 0)
356 #define ENUM_BUS_NONE 0
357 #define ENUM_BUS_8BIT 1
358 #define ENUM_BUS_16BIT 2
359 #define ENUM_BUS_32BIT 3
361 #define MAX_MCAST_LST 32
362 #define HW_MCAST_SIZE 8
363 #define MAC_ADDR_LEN 6
366 * union ks_tx_hdr - tx header data
367 * @txb: The header as bytes
368 * @txw: The header as 16bit, little-endian words
370 * A dual representation of the tx header data to allow
371 * access to individual bytes, and to allow 16bit accesses
372 * with 16bit alignment.
374 union ks_tx_hdr {
375 u8 txb[4];
376 __le16 txw[2];
380 * struct ks_net - KS8851 driver private data
381 * @net_device : The network device we're bound to
382 * @hw_addr : start address of data register.
383 * @hw_addr_cmd : start address of command register.
384 * @txh : temporaly buffer to save status/length.
385 * @lock : Lock to ensure that the device is not accessed when busy.
386 * @pdev : Pointer to platform device.
387 * @mii : The MII state information for the mii calls.
388 * @frame_head_info : frame header information for multi-pkt rx.
389 * @statelock : Lock on this structure for tx list.
390 * @msg_enable : The message flags controlling driver output (see ethtool).
391 * @frame_cnt : number of frames received.
392 * @bus_width : i/o bus width.
393 * @irq : irq number assigned to this device.
394 * @rc_rxqcr : Cached copy of KS_RXQCR.
395 * @rc_txcr : Cached copy of KS_TXCR.
396 * @rc_ier : Cached copy of KS_IER.
397 * @sharedbus : Multipex(addr and data bus) mode indicator.
398 * @cmd_reg_cache : command register cached.
399 * @cmd_reg_cache_int : command register cached. Used in the irq handler.
400 * @promiscuous : promiscuous mode indicator.
401 * @all_mcast : mutlicast indicator.
402 * @mcast_lst_size : size of multicast list.
403 * @mcast_lst : multicast list.
404 * @mcast_bits : multicast enabed.
405 * @mac_addr : MAC address assigned to this device.
406 * @fid : frame id.
407 * @extra_byte : number of extra byte prepended rx pkt.
408 * @enabled : indicator this device works.
410 * The @lock ensures that the chip is protected when certain operations are
411 * in progress. When the read or write packet transfer is in progress, most
412 * of the chip registers are not accessible until the transfer is finished and
413 * the DMA has been de-asserted.
415 * The @statelock is used to protect information in the structure which may
416 * need to be accessed via several sources, such as the network driver layer
417 * or one of the work queues.
421 /* Receive multiplex framer header info */
422 struct type_frame_head {
423 u16 sts; /* Frame status */
424 u16 len; /* Byte count */
427 struct ks_net {
428 struct net_device *netdev;
429 void __iomem *hw_addr;
430 void __iomem *hw_addr_cmd;
431 union ks_tx_hdr txh ____cacheline_aligned;
432 struct mutex lock; /* spinlock to be interrupt safe */
433 struct platform_device *pdev;
434 struct mii_if_info mii;
435 struct type_frame_head *frame_head_info;
436 spinlock_t statelock;
437 u32 msg_enable;
438 u32 frame_cnt;
439 int bus_width;
440 int irq;
442 u16 rc_rxqcr;
443 u16 rc_txcr;
444 u16 rc_ier;
445 u16 sharedbus;
446 u16 cmd_reg_cache;
447 u16 cmd_reg_cache_int;
448 u16 promiscuous;
449 u16 all_mcast;
450 u16 mcast_lst_size;
451 u8 mcast_lst[MAX_MCAST_LST][MAC_ADDR_LEN];
452 u8 mcast_bits[HW_MCAST_SIZE];
453 u8 mac_addr[6];
454 u8 fid;
455 u8 extra_byte;
456 u8 enabled;
459 static int msg_enable;
461 #define ks_info(_ks, _msg...) dev_info(&(_ks)->pdev->dev, _msg)
462 #define ks_warn(_ks, _msg...) dev_warn(&(_ks)->pdev->dev, _msg)
463 #define ks_dbg(_ks, _msg...) dev_dbg(&(_ks)->pdev->dev, _msg)
464 #define ks_err(_ks, _msg...) dev_err(&(_ks)->pdev->dev, _msg)
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 */
472 * register read/write calls.
474 * All these calls issue transactions to access the chip's registers. They
475 * all require that the necessary lock is held to prevent accesses when the
476 * chip is busy transfering packet data (RX/TX FIFO accesses).
480 * ks_rdreg8 - read 8 bit register from device
481 * @ks : The chip information
482 * @offset: The register address
484 * Read a 8bit register from the chip, returning the result
486 static u8 ks_rdreg8(struct ks_net *ks, int offset)
488 u16 data;
489 u8 shift_bit = offset & 0x03;
490 u8 shift_data = (offset & 1) << 3;
491 ks->cmd_reg_cache = (u16) offset | (u16)(BE0 << shift_bit);
492 iowrite16(ks->cmd_reg_cache, ks->hw_addr_cmd);
493 data = ioread16(ks->hw_addr);
494 return (u8)(data >> shift_data);
498 * ks_rdreg16 - read 16 bit register from device
499 * @ks : The chip information
500 * @offset: The register address
502 * Read a 16bit register from the chip, returning the result
505 static u16 ks_rdreg16(struct ks_net *ks, int offset)
507 ks->cmd_reg_cache = (u16)offset | ((BE1 | BE0) << (offset & 0x02));
508 iowrite16(ks->cmd_reg_cache, ks->hw_addr_cmd);
509 return ioread16(ks->hw_addr);
513 * ks_wrreg8 - write 8bit register value to chip
514 * @ks: The chip information
515 * @offset: The register address
516 * @value: The value to write
519 static void ks_wrreg8(struct ks_net *ks, int offset, u8 value)
521 u8 shift_bit = (offset & 0x03);
522 u16 value_write = (u16)(value << ((offset & 1) << 3));
523 ks->cmd_reg_cache = (u16)offset | (BE0 << shift_bit);
524 iowrite16(ks->cmd_reg_cache, ks->hw_addr_cmd);
525 iowrite16(value_write, ks->hw_addr);
529 * ks_wrreg16 - write 16bit register value to chip
530 * @ks: The chip information
531 * @offset: The register address
532 * @value: The value to write
536 static void ks_wrreg16(struct ks_net *ks, int offset, u16 value)
538 ks->cmd_reg_cache = (u16)offset | ((BE1 | BE0) << (offset & 0x02));
539 iowrite16(ks->cmd_reg_cache, ks->hw_addr_cmd);
540 iowrite16(value, ks->hw_addr);
544 * ks_inblk - read a block of data from QMU. This is called after sudo DMA mode enabled.
545 * @ks: The chip state
546 * @wptr: buffer address to save data
547 * @len: length in byte to read
550 static inline void ks_inblk(struct ks_net *ks, u16 *wptr, u32 len)
552 len >>= 1;
553 while (len--)
554 *wptr++ = (u16)ioread16(ks->hw_addr);
558 * ks_outblk - write data to QMU. This is called after sudo DMA mode enabled.
559 * @ks: The chip information
560 * @wptr: buffer address
561 * @len: length in byte to write
564 static inline void ks_outblk(struct ks_net *ks, u16 *wptr, u32 len)
566 len >>= 1;
567 while (len--)
568 iowrite16(*wptr++, ks->hw_addr);
572 * ks_tx_fifo_space - return the available hardware buffer size.
573 * @ks: The chip information
576 static inline u16 ks_tx_fifo_space(struct ks_net *ks)
578 return ks_rdreg16(ks, KS_TXMIR) & 0x1fff;
582 * ks_save_cmd_reg - save the command register from the cache.
583 * @ks: The chip information
586 static inline void ks_save_cmd_reg(struct ks_net *ks)
588 /*ks8851 MLL has a bug to read back the command register.
589 * So rely on software to save the content of command register.
591 ks->cmd_reg_cache_int = ks->cmd_reg_cache;
595 * ks_restore_cmd_reg - restore the command register from the cache and
596 * write to hardware register.
597 * @ks: The chip information
600 static inline void ks_restore_cmd_reg(struct ks_net *ks)
602 ks->cmd_reg_cache = ks->cmd_reg_cache_int;
603 iowrite16(ks->cmd_reg_cache, ks->hw_addr_cmd);
607 * ks_set_powermode - set power mode of the device
608 * @ks: The chip information
609 * @pwrmode: The power mode value to write to KS_PMECR.
611 * Change the power mode of the chip.
613 static void ks_set_powermode(struct ks_net *ks, unsigned pwrmode)
615 unsigned pmecr;
617 if (netif_msg_hw(ks))
618 ks_dbg(ks, "setting power mode %d\n", pwrmode);
620 ks_rdreg16(ks, KS_GRR);
621 pmecr = ks_rdreg16(ks, KS_PMECR);
622 pmecr &= ~PMECR_PM_MASK;
623 pmecr |= pwrmode;
625 ks_wrreg16(ks, KS_PMECR, pmecr);
629 * ks_read_config - read chip configuration of bus width.
630 * @ks: The chip information
633 static void ks_read_config(struct ks_net *ks)
635 u16 reg_data = 0;
637 /* Regardless of bus width, 8 bit read should always work.*/
638 reg_data = ks_rdreg8(ks, KS_CCR) & 0x00FF;
639 reg_data |= ks_rdreg8(ks, KS_CCR+1) << 8;
641 /* addr/data bus are multiplexed */
642 ks->sharedbus = (reg_data & CCR_SHARED) == CCR_SHARED;
644 /* There are garbage data when reading data from QMU,
645 depending on bus-width.
648 if (reg_data & CCR_8BIT) {
649 ks->bus_width = ENUM_BUS_8BIT;
650 ks->extra_byte = 1;
651 } else if (reg_data & CCR_16BIT) {
652 ks->bus_width = ENUM_BUS_16BIT;
653 ks->extra_byte = 2;
654 } else {
655 ks->bus_width = ENUM_BUS_32BIT;
656 ks->extra_byte = 4;
661 * ks_soft_reset - issue one of the soft reset to the device
662 * @ks: The device state.
663 * @op: The bit(s) to set in the GRR
665 * Issue the relevant soft-reset command to the device's GRR register
666 * specified by @op.
668 * Note, the delays are in there as a caution to ensure that the reset
669 * has time to take effect and then complete. Since the datasheet does
670 * not currently specify the exact sequence, we have chosen something
671 * that seems to work with our device.
673 static void ks_soft_reset(struct ks_net *ks, unsigned op)
675 /* Disable interrupt first */
676 ks_wrreg16(ks, KS_IER, 0x0000);
677 ks_wrreg16(ks, KS_GRR, op);
678 mdelay(10); /* wait a short time to effect reset */
679 ks_wrreg16(ks, KS_GRR, 0);
680 mdelay(1); /* wait for condition to clear */
685 * ks_read_qmu - read 1 pkt data from the QMU.
686 * @ks: The chip information
687 * @buf: buffer address to save 1 pkt
688 * @len: Pkt length
689 * Here is the sequence to read 1 pkt:
690 * 1. set sudo DMA mode
691 * 2. read prepend data
692 * 3. read pkt data
693 * 4. reset sudo DMA Mode
695 static inline void ks_read_qmu(struct ks_net *ks, u16 *buf, u32 len)
697 u32 r = ks->extra_byte & 0x1 ;
698 u32 w = ks->extra_byte - r;
700 /* 1. set sudo DMA mode */
701 ks_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI);
702 ks_wrreg8(ks, KS_RXQCR, (ks->rc_rxqcr | RXQCR_SDA) & 0xff);
704 /* 2. read prepend data */
706 * read 4 + extra bytes and discard them.
707 * extra bytes for dummy, 2 for status, 2 for len
710 /* use likely(r) for 8 bit access for performance */
711 if (unlikely(r))
712 ioread8(ks->hw_addr);
713 ks_inblk(ks, buf, w + 2 + 2);
715 /* 3. read pkt data */
716 ks_inblk(ks, buf, ALIGN(len, 4));
718 /* 4. reset sudo DMA Mode */
719 ks_wrreg8(ks, KS_RXQCR, ks->rc_rxqcr);
723 * ks_rcv - read multiple pkts data from the QMU.
724 * @ks: The chip information
725 * @netdev: The network device being opened.
727 * Read all of header information before reading pkt content.
728 * It is not allowed only port of pkts in QMU after issuing
729 * interrupt ack.
731 static void ks_rcv(struct ks_net *ks, struct net_device *netdev)
733 u32 i;
734 struct type_frame_head *frame_hdr = ks->frame_head_info;
735 struct sk_buff *skb;
737 ks->frame_cnt = ks_rdreg16(ks, KS_RXFCTR) >> 8;
739 /* read all header information */
740 for (i = 0; i < ks->frame_cnt; i++) {
741 /* Checking Received packet status */
742 frame_hdr->sts = ks_rdreg16(ks, KS_RXFHSR);
743 /* Get packet len from hardware */
744 frame_hdr->len = ks_rdreg16(ks, KS_RXFHBCR);
745 frame_hdr++;
748 frame_hdr = ks->frame_head_info;
749 while (ks->frame_cnt--) {
750 skb = dev_alloc_skb(frame_hdr->len + 16);
751 if (likely(skb && (frame_hdr->sts & RXFSHR_RXFV) &&
752 (frame_hdr->len < RX_BUF_SIZE) && frame_hdr->len)) {
753 skb_reserve(skb, 2);
754 /* read data block including CRC 4 bytes */
755 ks_read_qmu(ks, (u16 *)skb->data, frame_hdr->len + 4);
756 skb_put(skb, frame_hdr->len);
757 skb->dev = netdev;
758 skb->protocol = eth_type_trans(skb, netdev);
759 netif_rx(skb);
760 } else {
761 printk(KERN_ERR "%s: err:skb alloc\n", __func__);
762 ks_wrreg16(ks, KS_RXQCR, (ks->rc_rxqcr | RXQCR_RRXEF));
763 if (skb)
764 dev_kfree_skb_irq(skb);
766 frame_hdr++;
771 * ks_update_link_status - link status update.
772 * @netdev: The network device being opened.
773 * @ks: The chip information
777 static void ks_update_link_status(struct net_device *netdev, struct ks_net *ks)
779 /* check the status of the link */
780 u32 link_up_status;
781 if (ks_rdreg16(ks, KS_P1SR) & P1SR_LINK_GOOD) {
782 netif_carrier_on(netdev);
783 link_up_status = true;
784 } else {
785 netif_carrier_off(netdev);
786 link_up_status = false;
788 if (netif_msg_link(ks))
789 ks_dbg(ks, "%s: %s\n",
790 __func__, link_up_status ? "UP" : "DOWN");
794 * ks_irq - device interrupt handler
795 * @irq: Interrupt number passed from the IRQ hnalder.
796 * @pw: The private word passed to register_irq(), our struct ks_net.
798 * This is the handler invoked to find out what happened
800 * Read the interrupt status, work out what needs to be done and then clear
801 * any of the interrupts that are not needed.
804 static irqreturn_t ks_irq(int irq, void *pw)
806 struct ks_net *ks = pw;
807 struct net_device *netdev = ks->netdev;
808 u16 status;
810 /*this should be the first in IRQ handler */
811 ks_save_cmd_reg(ks);
813 status = ks_rdreg16(ks, KS_ISR);
814 if (unlikely(!status)) {
815 ks_restore_cmd_reg(ks);
816 return IRQ_NONE;
819 ks_wrreg16(ks, KS_ISR, status);
821 if (likely(status & IRQ_RXI))
822 ks_rcv(ks, netdev);
824 if (unlikely(status & IRQ_LCI))
825 ks_update_link_status(netdev, ks);
827 if (unlikely(status & IRQ_TXI))
828 netif_wake_queue(netdev);
830 if (unlikely(status & IRQ_LDI)) {
832 u16 pmecr = ks_rdreg16(ks, KS_PMECR);
833 pmecr &= ~PMECR_WKEVT_MASK;
834 ks_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
837 /* this should be the last in IRQ handler*/
838 ks_restore_cmd_reg(ks);
839 return IRQ_HANDLED;
844 * ks_net_open - open network device
845 * @netdev: The network device being opened.
847 * Called when the network device is marked active, such as a user executing
848 * 'ifconfig up' on the device.
850 static int ks_net_open(struct net_device *netdev)
852 struct ks_net *ks = netdev_priv(netdev);
853 int err;
855 #define KS_INT_FLAGS (IRQF_DISABLED|IRQF_TRIGGER_LOW)
856 /* lock the card, even if we may not actually do anything
857 * else at the moment.
860 if (netif_msg_ifup(ks))
861 ks_dbg(ks, "%s - entry\n", __func__);
863 /* reset the HW */
864 err = request_irq(ks->irq, ks_irq, KS_INT_FLAGS, DRV_NAME, ks);
866 if (err) {
867 printk(KERN_ERR "Failed to request IRQ: %d: %d\n",
868 ks->irq, err);
869 return err;
872 if (netif_msg_ifup(ks))
873 ks_dbg(ks, "network device %s up\n", netdev->name);
875 return 0;
879 * ks_net_stop - close network device
880 * @netdev: The device being closed.
882 * Called to close down a network device which has been active. Cancell any
883 * work, shutdown the RX and TX process and then place the chip into a low
884 * power state whilst it is not being used.
886 static int ks_net_stop(struct net_device *netdev)
888 struct ks_net *ks = netdev_priv(netdev);
890 if (netif_msg_ifdown(ks))
891 ks_info(ks, "%s: shutting down\n", netdev->name);
893 netif_stop_queue(netdev);
895 kfree(ks->frame_head_info);
897 mutex_lock(&ks->lock);
899 /* turn off the IRQs and ack any outstanding */
900 ks_wrreg16(ks, KS_IER, 0x0000);
901 ks_wrreg16(ks, KS_ISR, 0xffff);
903 /* shutdown RX process */
904 ks_wrreg16(ks, KS_RXCR1, 0x0000);
906 /* shutdown TX process */
907 ks_wrreg16(ks, KS_TXCR, 0x0000);
909 /* set powermode to soft power down to save power */
910 ks_set_powermode(ks, PMECR_PM_SOFTDOWN);
911 free_irq(ks->irq, netdev);
912 mutex_unlock(&ks->lock);
913 return 0;
918 * ks_write_qmu - write 1 pkt data to the QMU.
919 * @ks: The chip information
920 * @pdata: buffer address to save 1 pkt
921 * @len: Pkt length in byte
922 * Here is the sequence to write 1 pkt:
923 * 1. set sudo DMA mode
924 * 2. write status/length
925 * 3. write pkt data
926 * 4. reset sudo DMA Mode
927 * 5. reset sudo DMA mode
928 * 6. Wait until pkt is out
930 static void ks_write_qmu(struct ks_net *ks, u8 *pdata, u16 len)
932 unsigned fid = ks->fid;
934 fid = ks->fid;
935 ks->fid = (ks->fid + 1) & TXFR_TXFID_MASK;
937 /* reduce the tx interrupt occurrances. */
938 if (!fid)
939 fid |= TXFR_TXIC; /* irq on completion */
941 /* start header at txb[0] to align txw entries */
942 ks->txh.txw[0] = cpu_to_le16(fid);
943 ks->txh.txw[1] = cpu_to_le16(len);
945 /* 1. set sudo-DMA mode */
946 ks_wrreg8(ks, KS_RXQCR, (ks->rc_rxqcr | RXQCR_SDA) & 0xff);
947 /* 2. write status/lenth info */
948 ks_outblk(ks, ks->txh.txw, 4);
949 /* 3. write pkt data */
950 ks_outblk(ks, (u16 *)pdata, ALIGN(len, 4));
951 /* 4. reset sudo-DMA mode */
952 ks_wrreg8(ks, KS_RXQCR, ks->rc_rxqcr);
953 /* 5. Enqueue Tx(move the pkt from TX buffer into TXQ) */
954 ks_wrreg16(ks, KS_TXQCR, TXQCR_METFE);
955 /* 6. wait until TXQCR_METFE is auto-cleared */
956 while (ks_rdreg16(ks, KS_TXQCR) & TXQCR_METFE)
960 static void ks_disable_int(struct ks_net *ks)
962 ks_wrreg16(ks, KS_IER, 0x0000);
963 } /* ks_disable_int */
965 static void ks_enable_int(struct ks_net *ks)
967 ks_wrreg16(ks, KS_IER, ks->rc_ier);
968 } /* ks_enable_int */
971 * ks_start_xmit - transmit packet
972 * @skb : The buffer to transmit
973 * @netdev : The device used to transmit the packet.
975 * Called by the network layer to transmit the @skb.
976 * spin_lock_irqsave is required because tx and rx should be mutual exclusive.
977 * So while tx is in-progress, prevent IRQ interrupt from happenning.
979 static int ks_start_xmit(struct sk_buff *skb, struct net_device *netdev)
981 int retv = NETDEV_TX_OK;
982 struct ks_net *ks = netdev_priv(netdev);
984 disable_irq(netdev->irq);
985 ks_disable_int(ks);
986 spin_lock(&ks->statelock);
988 /* Extra space are required:
989 * 4 byte for alignment, 4 for status/length, 4 for CRC
992 if (likely(ks_tx_fifo_space(ks) >= skb->len + 12)) {
993 ks_write_qmu(ks, skb->data, skb->len);
994 dev_kfree_skb(skb);
995 } else
996 retv = NETDEV_TX_BUSY;
997 spin_unlock(&ks->statelock);
998 ks_enable_int(ks);
999 enable_irq(netdev->irq);
1000 return retv;
1004 * ks_start_rx - ready to serve pkts
1005 * @ks : The chip information
1008 static void ks_start_rx(struct ks_net *ks)
1010 u16 cntl;
1012 /* Enables QMU Receive (RXCR1). */
1013 cntl = ks_rdreg16(ks, KS_RXCR1);
1014 cntl |= RXCR1_RXE ;
1015 ks_wrreg16(ks, KS_RXCR1, cntl);
1016 } /* ks_start_rx */
1019 * ks_stop_rx - stop to serve pkts
1020 * @ks : The chip information
1023 static void ks_stop_rx(struct ks_net *ks)
1025 u16 cntl;
1027 /* Disables QMU Receive (RXCR1). */
1028 cntl = ks_rdreg16(ks, KS_RXCR1);
1029 cntl &= ~RXCR1_RXE ;
1030 ks_wrreg16(ks, KS_RXCR1, cntl);
1032 } /* ks_stop_rx */
1034 static unsigned long const ethernet_polynomial = 0x04c11db7U;
1036 static unsigned long ether_gen_crc(int length, u8 *data)
1038 long crc = -1;
1039 while (--length >= 0) {
1040 u8 current_octet = *data++;
1041 int bit;
1043 for (bit = 0; bit < 8; bit++, current_octet >>= 1) {
1044 crc = (crc << 1) ^
1045 ((crc < 0) ^ (current_octet & 1) ?
1046 ethernet_polynomial : 0);
1049 return (unsigned long)crc;
1050 } /* ether_gen_crc */
1053 * ks_set_grpaddr - set multicast information
1054 * @ks : The chip information
1057 static void ks_set_grpaddr(struct ks_net *ks)
1059 u8 i;
1060 u32 index, position, value;
1062 memset(ks->mcast_bits, 0, sizeof(u8) * HW_MCAST_SIZE);
1064 for (i = 0; i < ks->mcast_lst_size; i++) {
1065 position = (ether_gen_crc(6, ks->mcast_lst[i]) >> 26) & 0x3f;
1066 index = position >> 3;
1067 value = 1 << (position & 7);
1068 ks->mcast_bits[index] |= (u8)value;
1071 for (i = 0; i < HW_MCAST_SIZE; i++) {
1072 if (i & 1) {
1073 ks_wrreg16(ks, (u16)((KS_MAHTR0 + i) & ~1),
1074 (ks->mcast_bits[i] << 8) |
1075 ks->mcast_bits[i - 1]);
1078 } /* ks_set_grpaddr */
1081 * ks_clear_mcast - clear multicast information
1083 * @ks : The chip information
1084 * This routine removes all mcast addresses set in the hardware.
1087 static void ks_clear_mcast(struct ks_net *ks)
1089 u16 i, mcast_size;
1090 for (i = 0; i < HW_MCAST_SIZE; i++)
1091 ks->mcast_bits[i] = 0;
1093 mcast_size = HW_MCAST_SIZE >> 2;
1094 for (i = 0; i < mcast_size; i++)
1095 ks_wrreg16(ks, KS_MAHTR0 + (2*i), 0);
1098 static void ks_set_promis(struct ks_net *ks, u16 promiscuous_mode)
1100 u16 cntl;
1101 ks->promiscuous = promiscuous_mode;
1102 ks_stop_rx(ks); /* Stop receiving for reconfiguration */
1103 cntl = ks_rdreg16(ks, KS_RXCR1);
1105 cntl &= ~RXCR1_FILTER_MASK;
1106 if (promiscuous_mode)
1107 /* Enable Promiscuous mode */
1108 cntl |= RXCR1_RXAE | RXCR1_RXINVF;
1109 else
1110 /* Disable Promiscuous mode (default normal mode) */
1111 cntl |= RXCR1_RXPAFMA;
1113 ks_wrreg16(ks, KS_RXCR1, cntl);
1115 if (ks->enabled)
1116 ks_start_rx(ks);
1118 } /* ks_set_promis */
1120 static void ks_set_mcast(struct ks_net *ks, u16 mcast)
1122 u16 cntl;
1124 ks->all_mcast = mcast;
1125 ks_stop_rx(ks); /* Stop receiving for reconfiguration */
1126 cntl = ks_rdreg16(ks, KS_RXCR1);
1127 cntl &= ~RXCR1_FILTER_MASK;
1128 if (mcast)
1129 /* Enable "Perfect with Multicast address passed mode" */
1130 cntl |= (RXCR1_RXAE | RXCR1_RXMAFMA | RXCR1_RXPAFMA);
1131 else
1133 * Disable "Perfect with Multicast address passed
1134 * mode" (normal mode).
1136 cntl |= RXCR1_RXPAFMA;
1138 ks_wrreg16(ks, KS_RXCR1, cntl);
1140 if (ks->enabled)
1141 ks_start_rx(ks);
1142 } /* ks_set_mcast */
1144 static void ks_set_rx_mode(struct net_device *netdev)
1146 struct ks_net *ks = netdev_priv(netdev);
1147 struct dev_mc_list *ptr;
1149 /* Turn on/off promiscuous mode. */
1150 if ((netdev->flags & IFF_PROMISC) == IFF_PROMISC)
1151 ks_set_promis(ks,
1152 (u16)((netdev->flags & IFF_PROMISC) == IFF_PROMISC));
1153 /* Turn on/off all mcast mode. */
1154 else if ((netdev->flags & IFF_ALLMULTI) == IFF_ALLMULTI)
1155 ks_set_mcast(ks,
1156 (u16)((netdev->flags & IFF_ALLMULTI) == IFF_ALLMULTI));
1157 else
1158 ks_set_promis(ks, false);
1160 if ((netdev->flags & IFF_MULTICAST) && netdev->mc_count) {
1161 if (netdev->mc_count <= MAX_MCAST_LST) {
1162 int i = 0;
1163 for (ptr = netdev->mc_list; ptr; ptr = ptr->next) {
1164 if (!(*ptr->dmi_addr & 1))
1165 continue;
1166 if (i >= MAX_MCAST_LST)
1167 break;
1168 memcpy(ks->mcast_lst[i++], ptr->dmi_addr,
1169 MAC_ADDR_LEN);
1171 ks->mcast_lst_size = (u8)i;
1172 ks_set_grpaddr(ks);
1173 } else {
1175 * List too big to support so
1176 * turn on all mcast mode.
1178 ks->mcast_lst_size = MAX_MCAST_LST;
1179 ks_set_mcast(ks, true);
1181 } else {
1182 ks->mcast_lst_size = 0;
1183 ks_clear_mcast(ks);
1185 } /* ks_set_rx_mode */
1187 static void ks_set_mac(struct ks_net *ks, u8 *data)
1189 u16 *pw = (u16 *)data;
1190 u16 w, u;
1192 ks_stop_rx(ks); /* Stop receiving for reconfiguration */
1194 u = *pw++;
1195 w = ((u & 0xFF) << 8) | ((u >> 8) & 0xFF);
1196 ks_wrreg16(ks, KS_MARH, w);
1198 u = *pw++;
1199 w = ((u & 0xFF) << 8) | ((u >> 8) & 0xFF);
1200 ks_wrreg16(ks, KS_MARM, w);
1202 u = *pw;
1203 w = ((u & 0xFF) << 8) | ((u >> 8) & 0xFF);
1204 ks_wrreg16(ks, KS_MARL, w);
1206 memcpy(ks->mac_addr, data, 6);
1208 if (ks->enabled)
1209 ks_start_rx(ks);
1212 static int ks_set_mac_address(struct net_device *netdev, void *paddr)
1214 struct ks_net *ks = netdev_priv(netdev);
1215 struct sockaddr *addr = paddr;
1216 u8 *da;
1218 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1220 da = (u8 *)netdev->dev_addr;
1222 ks_set_mac(ks, da);
1223 return 0;
1226 static int ks_net_ioctl(struct net_device *netdev, struct ifreq *req, int cmd)
1228 struct ks_net *ks = netdev_priv(netdev);
1230 if (!netif_running(netdev))
1231 return -EINVAL;
1233 return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
1236 static const struct net_device_ops ks_netdev_ops = {
1237 .ndo_open = ks_net_open,
1238 .ndo_stop = ks_net_stop,
1239 .ndo_do_ioctl = ks_net_ioctl,
1240 .ndo_start_xmit = ks_start_xmit,
1241 .ndo_set_mac_address = ks_set_mac_address,
1242 .ndo_set_rx_mode = ks_set_rx_mode,
1243 .ndo_change_mtu = eth_change_mtu,
1244 .ndo_validate_addr = eth_validate_addr,
1247 /* ethtool support */
1249 static void ks_get_drvinfo(struct net_device *netdev,
1250 struct ethtool_drvinfo *di)
1252 strlcpy(di->driver, DRV_NAME, sizeof(di->driver));
1253 strlcpy(di->version, "1.00", sizeof(di->version));
1254 strlcpy(di->bus_info, dev_name(netdev->dev.parent),
1255 sizeof(di->bus_info));
1258 static u32 ks_get_msglevel(struct net_device *netdev)
1260 struct ks_net *ks = netdev_priv(netdev);
1261 return ks->msg_enable;
1264 static void ks_set_msglevel(struct net_device *netdev, u32 to)
1266 struct ks_net *ks = netdev_priv(netdev);
1267 ks->msg_enable = to;
1270 static int ks_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
1272 struct ks_net *ks = netdev_priv(netdev);
1273 return mii_ethtool_gset(&ks->mii, cmd);
1276 static int ks_set_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
1278 struct ks_net *ks = netdev_priv(netdev);
1279 return mii_ethtool_sset(&ks->mii, cmd);
1282 static u32 ks_get_link(struct net_device *netdev)
1284 struct ks_net *ks = netdev_priv(netdev);
1285 return mii_link_ok(&ks->mii);
1288 static int ks_nway_reset(struct net_device *netdev)
1290 struct ks_net *ks = netdev_priv(netdev);
1291 return mii_nway_restart(&ks->mii);
1294 static const struct ethtool_ops ks_ethtool_ops = {
1295 .get_drvinfo = ks_get_drvinfo,
1296 .get_msglevel = ks_get_msglevel,
1297 .set_msglevel = ks_set_msglevel,
1298 .get_settings = ks_get_settings,
1299 .set_settings = ks_set_settings,
1300 .get_link = ks_get_link,
1301 .nway_reset = ks_nway_reset,
1304 /* MII interface controls */
1307 * ks_phy_reg - convert MII register into a KS8851 register
1308 * @reg: MII register number.
1310 * Return the KS8851 register number for the corresponding MII PHY register
1311 * if possible. Return zero if the MII register has no direct mapping to the
1312 * KS8851 register set.
1314 static int ks_phy_reg(int reg)
1316 switch (reg) {
1317 case MII_BMCR:
1318 return KS_P1MBCR;
1319 case MII_BMSR:
1320 return KS_P1MBSR;
1321 case MII_PHYSID1:
1322 return KS_PHY1ILR;
1323 case MII_PHYSID2:
1324 return KS_PHY1IHR;
1325 case MII_ADVERTISE:
1326 return KS_P1ANAR;
1327 case MII_LPA:
1328 return KS_P1ANLPR;
1331 return 0x0;
1335 * ks_phy_read - MII interface PHY register read.
1336 * @netdev: The network device the PHY is on.
1337 * @phy_addr: Address of PHY (ignored as we only have one)
1338 * @reg: The register to read.
1340 * This call reads data from the PHY register specified in @reg. Since the
1341 * device does not support all the MII registers, the non-existant values
1342 * are always returned as zero.
1344 * We return zero for unsupported registers as the MII code does not check
1345 * the value returned for any error status, and simply returns it to the
1346 * caller. The mii-tool that the driver was tested with takes any -ve error
1347 * as real PHY capabilities, thus displaying incorrect data to the user.
1349 static int ks_phy_read(struct net_device *netdev, int phy_addr, int reg)
1351 struct ks_net *ks = netdev_priv(netdev);
1352 int ksreg;
1353 int result;
1355 ksreg = ks_phy_reg(reg);
1356 if (!ksreg)
1357 return 0x0; /* no error return allowed, so use zero */
1359 mutex_lock(&ks->lock);
1360 result = ks_rdreg16(ks, ksreg);
1361 mutex_unlock(&ks->lock);
1363 return result;
1366 static void ks_phy_write(struct net_device *netdev,
1367 int phy, int reg, int value)
1369 struct ks_net *ks = netdev_priv(netdev);
1370 int ksreg;
1372 ksreg = ks_phy_reg(reg);
1373 if (ksreg) {
1374 mutex_lock(&ks->lock);
1375 ks_wrreg16(ks, ksreg, value);
1376 mutex_unlock(&ks->lock);
1381 * ks_read_selftest - read the selftest memory info.
1382 * @ks: The device state
1384 * Read and check the TX/RX memory selftest information.
1386 static int ks_read_selftest(struct ks_net *ks)
1388 unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
1389 int ret = 0;
1390 unsigned rd;
1392 rd = ks_rdreg16(ks, KS_MBIR);
1394 if ((rd & both_done) != both_done) {
1395 ks_warn(ks, "Memory selftest not finished\n");
1396 return 0;
1399 if (rd & MBIR_TXMBFA) {
1400 ks_err(ks, "TX memory selftest fails\n");
1401 ret |= 1;
1404 if (rd & MBIR_RXMBFA) {
1405 ks_err(ks, "RX memory selftest fails\n");
1406 ret |= 2;
1409 ks_info(ks, "the selftest passes\n");
1410 return ret;
1413 static void ks_disable(struct ks_net *ks)
1415 u16 w;
1417 w = ks_rdreg16(ks, KS_TXCR);
1419 /* Disables QMU Transmit (TXCR). */
1420 w &= ~TXCR_TXE;
1421 ks_wrreg16(ks, KS_TXCR, w);
1423 /* Disables QMU Receive (RXCR1). */
1424 w = ks_rdreg16(ks, KS_RXCR1);
1425 w &= ~RXCR1_RXE ;
1426 ks_wrreg16(ks, KS_RXCR1, w);
1428 ks->enabled = false;
1430 } /* ks_disable */
1432 static void ks_setup(struct ks_net *ks)
1434 u16 w;
1437 * Configure QMU Transmit
1440 /* Setup Transmit Frame Data Pointer Auto-Increment (TXFDPR) */
1441 ks_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
1443 /* Setup Receive Frame Data Pointer Auto-Increment */
1444 ks_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI);
1446 /* Setup Receive Frame Threshold - 1 frame (RXFCTFC) */
1447 ks_wrreg16(ks, KS_RXFCTR, 1 & RXFCTR_THRESHOLD_MASK);
1449 /* Setup RxQ Command Control (RXQCR) */
1450 ks->rc_rxqcr = RXQCR_CMD_CNTL;
1451 ks_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
1454 * set the force mode to half duplex, default is full duplex
1455 * because if the auto-negotiation fails, most switch uses
1456 * half-duplex.
1459 w = ks_rdreg16(ks, KS_P1MBCR);
1460 w &= ~P1MBCR_FORCE_FDX;
1461 ks_wrreg16(ks, KS_P1MBCR, w);
1463 w = TXCR_TXFCE | TXCR_TXPE | TXCR_TXCRC | TXCR_TCGIP;
1464 ks_wrreg16(ks, KS_TXCR, w);
1466 w = RXCR1_RXFCE | RXCR1_RXBE | RXCR1_RXUE;
1468 if (ks->promiscuous) /* bPromiscuous */
1469 w |= (RXCR1_RXAE | RXCR1_RXINVF);
1470 else if (ks->all_mcast) /* Multicast address passed mode */
1471 w |= (RXCR1_RXAE | RXCR1_RXMAFMA | RXCR1_RXPAFMA);
1472 else /* Normal mode */
1473 w |= RXCR1_RXPAFMA;
1475 ks_wrreg16(ks, KS_RXCR1, w);
1476 } /*ks_setup */
1479 static void ks_setup_int(struct ks_net *ks)
1481 ks->rc_ier = 0x00;
1482 /* Clear the interrupts status of the hardware. */
1483 ks_wrreg16(ks, KS_ISR, 0xffff);
1485 /* Enables the interrupts of the hardware. */
1486 ks->rc_ier = (IRQ_LCI | IRQ_TXI | IRQ_RXI);
1487 } /* ks_setup_int */
1489 void ks_enable(struct ks_net *ks)
1491 u16 w;
1493 w = ks_rdreg16(ks, KS_TXCR);
1494 /* Enables QMU Transmit (TXCR). */
1495 ks_wrreg16(ks, KS_TXCR, w | TXCR_TXE);
1498 * RX Frame Count Threshold Enable and Auto-Dequeue RXQ Frame
1499 * Enable
1502 w = ks_rdreg16(ks, KS_RXQCR);
1503 ks_wrreg16(ks, KS_RXQCR, w | RXQCR_RXFCTE);
1505 /* Enables QMU Receive (RXCR1). */
1506 w = ks_rdreg16(ks, KS_RXCR1);
1507 ks_wrreg16(ks, KS_RXCR1, w | RXCR1_RXE);
1508 ks->enabled = true;
1509 } /* ks_enable */
1511 static int ks_hw_init(struct ks_net *ks)
1513 #define MHEADER_SIZE (sizeof(struct type_frame_head) * MAX_RECV_FRAMES)
1514 ks->promiscuous = 0;
1515 ks->all_mcast = 0;
1516 ks->mcast_lst_size = 0;
1518 ks->frame_head_info = (struct type_frame_head *) \
1519 kmalloc(MHEADER_SIZE, GFP_KERNEL);
1520 if (!ks->frame_head_info) {
1521 printk(KERN_ERR "Error: Fail to allocate frame memory\n");
1522 return false;
1525 ks_set_mac(ks, KS_DEFAULT_MAC_ADDRESS);
1526 return true;
1530 static int __devinit ks8851_probe(struct platform_device *pdev)
1532 int err = -ENOMEM;
1533 struct resource *io_d, *io_c;
1534 struct net_device *netdev;
1535 struct ks_net *ks;
1536 u16 id, data;
1538 io_d = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1539 io_c = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1541 if (!request_mem_region(io_d->start, resource_size(io_d), DRV_NAME))
1542 goto err_mem_region;
1544 if (!request_mem_region(io_c->start, resource_size(io_c), DRV_NAME))
1545 goto err_mem_region1;
1547 netdev = alloc_etherdev(sizeof(struct ks_net));
1548 if (!netdev)
1549 goto err_alloc_etherdev;
1551 SET_NETDEV_DEV(netdev, &pdev->dev);
1553 ks = netdev_priv(netdev);
1554 ks->netdev = netdev;
1555 ks->hw_addr = ioremap(io_d->start, resource_size(io_d));
1557 if (!ks->hw_addr)
1558 goto err_ioremap;
1560 ks->hw_addr_cmd = ioremap(io_c->start, resource_size(io_c));
1561 if (!ks->hw_addr_cmd)
1562 goto err_ioremap1;
1564 ks->irq = platform_get_irq(pdev, 0);
1566 if (ks->irq < 0) {
1567 err = ks->irq;
1568 goto err_get_irq;
1571 ks->pdev = pdev;
1573 mutex_init(&ks->lock);
1574 spin_lock_init(&ks->statelock);
1576 netdev->netdev_ops = &ks_netdev_ops;
1577 netdev->ethtool_ops = &ks_ethtool_ops;
1579 /* setup mii state */
1580 ks->mii.dev = netdev;
1581 ks->mii.phy_id = 1,
1582 ks->mii.phy_id_mask = 1;
1583 ks->mii.reg_num_mask = 0xf;
1584 ks->mii.mdio_read = ks_phy_read;
1585 ks->mii.mdio_write = ks_phy_write;
1587 ks_info(ks, "message enable is %d\n", msg_enable);
1588 /* set the default message enable */
1589 ks->msg_enable = netif_msg_init(msg_enable, (NETIF_MSG_DRV |
1590 NETIF_MSG_PROBE |
1591 NETIF_MSG_LINK));
1592 ks_read_config(ks);
1594 /* simple check for a valid chip being connected to the bus */
1595 if ((ks_rdreg16(ks, KS_CIDER) & ~CIDER_REV_MASK) != CIDER_ID) {
1596 ks_err(ks, "failed to read device ID\n");
1597 err = -ENODEV;
1598 goto err_register;
1601 if (ks_read_selftest(ks)) {
1602 ks_err(ks, "failed to read device ID\n");
1603 err = -ENODEV;
1604 goto err_register;
1607 err = register_netdev(netdev);
1608 if (err)
1609 goto err_register;
1611 platform_set_drvdata(pdev, netdev);
1613 ks_soft_reset(ks, GRR_GSR);
1614 ks_hw_init(ks);
1615 ks_disable(ks);
1616 ks_setup(ks);
1617 ks_setup_int(ks);
1618 ks_enable_int(ks);
1619 ks_enable(ks);
1620 memcpy(netdev->dev_addr, ks->mac_addr, 6);
1622 data = ks_rdreg16(ks, KS_OBCR);
1623 ks_wrreg16(ks, KS_OBCR, data | OBCR_ODS_16MA);
1626 * If you want to use the default MAC addr,
1627 * comment out the 2 functions below.
1630 random_ether_addr(netdev->dev_addr);
1631 ks_set_mac(ks, netdev->dev_addr);
1633 id = ks_rdreg16(ks, KS_CIDER);
1635 printk(KERN_INFO DRV_NAME
1636 " Found chip, family: 0x%x, id: 0x%x, rev: 0x%x\n",
1637 (id >> 8) & 0xff, (id >> 4) & 0xf, (id >> 1) & 0x7);
1638 return 0;
1640 err_register:
1641 err_get_irq:
1642 iounmap(ks->hw_addr_cmd);
1643 err_ioremap1:
1644 iounmap(ks->hw_addr);
1645 err_ioremap:
1646 free_netdev(netdev);
1647 err_alloc_etherdev:
1648 release_mem_region(io_c->start, resource_size(io_c));
1649 err_mem_region1:
1650 release_mem_region(io_d->start, resource_size(io_d));
1651 err_mem_region:
1652 return err;
1655 static int __devexit ks8851_remove(struct platform_device *pdev)
1657 struct net_device *netdev = platform_get_drvdata(pdev);
1658 struct ks_net *ks = netdev_priv(netdev);
1659 struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1661 unregister_netdev(netdev);
1662 iounmap(ks->hw_addr);
1663 free_netdev(netdev);
1664 release_mem_region(iomem->start, resource_size(iomem));
1665 platform_set_drvdata(pdev, NULL);
1666 return 0;
1670 static struct platform_driver ks8851_platform_driver = {
1671 .driver = {
1672 .name = DRV_NAME,
1673 .owner = THIS_MODULE,
1675 .probe = ks8851_probe,
1676 .remove = __devexit_p(ks8851_remove),
1679 static int __init ks8851_init(void)
1681 return platform_driver_register(&ks8851_platform_driver);
1684 static void __exit ks8851_exit(void)
1686 platform_driver_unregister(&ks8851_platform_driver);
1689 module_init(ks8851_init);
1690 module_exit(ks8851_exit);
1692 MODULE_DESCRIPTION("KS8851 MLL Network driver");
1693 MODULE_AUTHOR("David Choi <david.choi@micrel.com>");
1694 MODULE_LICENSE("GPL");
1695 module_param_named(message, msg_enable, int, 0);
1696 MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");