1 // SPDX-License-Identifier: GPL-2.0+
3 * Micrel KS8851_MLL 16bit Network driver
4 * Copyright (c) 2011 Roberto Cerati <roberto.cerati@bticino.it>
14 #include "ks8851_mll.h"
16 #define DRIVERNAME "ks8851_mll"
18 #define MAX_RECV_FRAMES 32
19 #define MAX_BUF_SIZE 2048
20 #define TX_BUF_SIZE 2000
21 #define RX_BUF_SIZE 2000
23 static const struct chip_id chip_ids[] = {
24 {CIDER_ID, "KSZ8851"},
29 * union ks_tx_hdr - tx header data
30 * @txb: The header as bytes
31 * @txw: The header as 16bit, little-endian words
33 * A dual representation of the tx header data to allow
34 * access to individual bytes, and to allow 16bit accesses
35 * with 16bit alignment.
43 * struct ks_net - KS8851 driver private data
44 * @net_device : The network device we're bound to
45 * @txh : temporaly buffer to save status/length.
46 * @frame_head_info : frame header information for multi-pkt rx.
47 * @statelock : Lock on this structure for tx list.
48 * @msg_enable : The message flags controlling driver output (see ethtool).
49 * @frame_cnt : number of frames received.
50 * @bus_width : i/o bus width.
51 * @irq : irq number assigned to this device.
52 * @rc_rxqcr : Cached copy of KS_RXQCR.
53 * @rc_txcr : Cached copy of KS_TXCR.
54 * @rc_ier : Cached copy of KS_IER.
55 * @sharedbus : Multipex(addr and data bus) mode indicator.
56 * @cmd_reg_cache : command register cached.
57 * @cmd_reg_cache_int : command register cached. Used in the irq handler.
58 * @promiscuous : promiscuous mode indicator.
59 * @all_mcast : mutlicast indicator.
60 * @mcast_lst_size : size of multicast list.
61 * @mcast_lst : multicast list.
62 * @mcast_bits : multicast enabed.
63 * @mac_addr : MAC address assigned to this device.
65 * @extra_byte : number of extra byte prepended rx pkt.
66 * @enabled : indicator this device works.
69 /* Receive multiplex framer header info */
70 struct type_frame_head {
71 u16 sts; /* Frame status */
72 u16 len; /* Byte count */
73 } fr_h_i[MAX_RECV_FRAMES];
76 struct net_device *netdev;
78 struct type_frame_head *frame_head_info;
88 u16 cmd_reg_cache_int;
92 u8 mcast_lst[MAX_MCAST_LST][MAC_ADDR_LEN];
93 u8 mcast_bits[HW_MCAST_SIZE];
100 #define BE3 0x8000 /* Byte Enable 3 */
101 #define BE2 0x4000 /* Byte Enable 2 */
102 #define BE1 0x2000 /* Byte Enable 1 */
103 #define BE0 0x1000 /* Byte Enable 0 */
105 static u8 ks_rdreg8(struct eth_device *dev, u16 offset)
107 u8 shift_bit = offset & 0x03;
108 u8 shift_data = (offset & 1) << 3;
110 writew(offset | (BE0 << shift_bit), dev->iobase + 2);
112 return (u8)(readw(dev->iobase) >> shift_data);
115 static u16 ks_rdreg16(struct eth_device *dev, u16 offset)
117 writew(offset | ((BE1 | BE0) << (offset & 0x02)), dev->iobase + 2);
119 return readw(dev->iobase);
122 static void ks_wrreg8(struct eth_device *dev, u16 offset, u8 val)
124 u8 shift_bit = (offset & 0x03);
125 u16 value_write = (u16)(val << ((offset & 1) << 3));
127 writew(offset | (BE0 << shift_bit), dev->iobase + 2);
128 writew(value_write, dev->iobase);
131 static void ks_wrreg16(struct eth_device *dev, u16 offset, u16 val)
133 writew(offset | ((BE1 | BE0) << (offset & 0x02)), dev->iobase + 2);
134 writew(val, dev->iobase);
138 * ks_inblk - read a block of data from QMU. This is called after sudo DMA mode
140 * @ks: The chip state
141 * @wptr: buffer address to save data
142 * @len: length in byte to read
144 static inline void ks_inblk(struct eth_device *dev, u16 *wptr, u32 len)
149 *wptr++ = readw(dev->iobase);
153 * ks_outblk - write data to QMU. This is called after sudo DMA mode enabled.
154 * @ks: The chip information
155 * @wptr: buffer address
156 * @len: length in byte to write
158 static inline void ks_outblk(struct eth_device *dev, u16 *wptr, u32 len)
163 writew(*wptr++, dev->iobase);
166 static void ks_enable_int(struct eth_device *dev)
168 ks_wrreg16(dev, KS_IER, ks->rc_ier);
171 static void ks_set_powermode(struct eth_device *dev, unsigned pwrmode)
175 ks_rdreg16(dev, KS_GRR);
176 pmecr = ks_rdreg16(dev, KS_PMECR);
177 pmecr &= ~PMECR_PM_MASK;
180 ks_wrreg16(dev, KS_PMECR, pmecr);
184 * ks_read_config - read chip configuration of bus width.
185 * @ks: The chip information
187 static void ks_read_config(struct eth_device *dev)
191 /* Regardless of bus width, 8 bit read should always work. */
192 reg_data = ks_rdreg8(dev, KS_CCR) & 0x00FF;
193 reg_data |= ks_rdreg8(dev, KS_CCR + 1) << 8;
195 /* addr/data bus are multiplexed */
196 ks->sharedbus = (reg_data & CCR_SHARED) == CCR_SHARED;
199 * There are garbage data when reading data from QMU,
200 * depending on bus-width.
202 if (reg_data & CCR_8BIT) {
203 ks->bus_width = ENUM_BUS_8BIT;
205 } else if (reg_data & CCR_16BIT) {
206 ks->bus_width = ENUM_BUS_16BIT;
209 ks->bus_width = ENUM_BUS_32BIT;
215 * ks_soft_reset - issue one of the soft reset to the device
216 * @ks: The device state.
217 * @op: The bit(s) to set in the GRR
219 * Issue the relevant soft-reset command to the device's GRR register
222 * Note, the delays are in there as a caution to ensure that the reset
223 * has time to take effect and then complete. Since the datasheet does
224 * not currently specify the exact sequence, we have chosen something
225 * that seems to work with our device.
227 static void ks_soft_reset(struct eth_device *dev, unsigned op)
229 /* Disable interrupt first */
230 ks_wrreg16(dev, KS_IER, 0x0000);
231 ks_wrreg16(dev, KS_GRR, op);
232 mdelay(10); /* wait a short time to effect reset */
233 ks_wrreg16(dev, KS_GRR, 0);
234 mdelay(1); /* wait for condition to clear */
237 void ks_enable_qmu(struct eth_device *dev)
241 w = ks_rdreg16(dev, KS_TXCR);
243 /* Enables QMU Transmit (TXCR). */
244 ks_wrreg16(dev, KS_TXCR, w | TXCR_TXE);
246 /* Enable RX Frame Count Threshold and Auto-Dequeue RXQ Frame */
247 w = ks_rdreg16(dev, KS_RXQCR);
248 ks_wrreg16(dev, KS_RXQCR, w | RXQCR_RXFCTE);
250 /* Enables QMU Receive (RXCR1). */
251 w = ks_rdreg16(dev, KS_RXCR1);
252 ks_wrreg16(dev, KS_RXCR1, w | RXCR1_RXE);
255 static void ks_disable_qmu(struct eth_device *dev)
259 w = ks_rdreg16(dev, KS_TXCR);
261 /* Disables QMU Transmit (TXCR). */
263 ks_wrreg16(dev, KS_TXCR, w);
265 /* Disables QMU Receive (RXCR1). */
266 w = ks_rdreg16(dev, KS_RXCR1);
268 ks_wrreg16(dev, KS_RXCR1, w);
271 static inline void ks_read_qmu(struct eth_device *dev, u16 *buf, u32 len)
273 u32 r = ks->extra_byte & 0x1;
274 u32 w = ks->extra_byte - r;
276 /* 1. set sudo DMA mode */
277 ks_wrreg16(dev, KS_RXFDPR, RXFDPR_RXFPAI);
278 ks_wrreg8(dev, KS_RXQCR, (ks->rc_rxqcr | RXQCR_SDA) & 0xff);
281 * 2. read prepend data
283 * read 4 + extra bytes and discard them.
284 * extra bytes for dummy, 2 for status, 2 for len
290 ks_inblk(dev, buf, w + 2 + 2);
292 /* 3. read pkt data */
293 ks_inblk(dev, buf, ALIGN(len, 4));
295 /* 4. reset sudo DMA Mode */
296 ks_wrreg8(dev, KS_RXQCR, (ks->rc_rxqcr & ~RXQCR_SDA) & 0xff);
299 static void ks_rcv(struct eth_device *dev, uchar **pv_data)
301 struct type_frame_head *frame_hdr = ks->frame_head_info;
304 ks->frame_cnt = ks_rdreg16(dev, KS_RXFCTR) >> 8;
306 /* read all header information */
307 for (i = 0; i < ks->frame_cnt; i++) {
308 /* Checking Received packet status */
309 frame_hdr->sts = ks_rdreg16(dev, KS_RXFHSR);
310 /* Get packet len from hardware */
311 frame_hdr->len = ks_rdreg16(dev, KS_RXFHBCR);
315 frame_hdr = ks->frame_head_info;
316 while (ks->frame_cnt--) {
317 if ((frame_hdr->sts & RXFSHR_RXFV) &&
318 (frame_hdr->len < RX_BUF_SIZE) &&
320 /* read data block including CRC 4 bytes */
321 ks_read_qmu(dev, (u16 *)(*pv_data), frame_hdr->len);
323 /* net_rx_packets buffer size is ok (*pv_data) */
324 net_process_received_packet(*pv_data, frame_hdr->len);
327 ks_wrreg16(dev, KS_RXQCR, (ks->rc_rxqcr | RXQCR_RRXEF));
328 printf(DRIVERNAME ": bad packet\n");
335 * ks_read_selftest - read the selftest memory info.
336 * @ks: The device state
338 * Read and check the TX/RX memory selftest information.
340 static int ks_read_selftest(struct eth_device *dev)
342 u16 both_done = MBIR_TXMBF | MBIR_RXMBF;
346 mbir = ks_rdreg16(dev, KS_MBIR);
348 if ((mbir & both_done) != both_done) {
349 printf(DRIVERNAME ": Memory selftest not finished\n");
353 if (mbir & MBIR_TXMBFA) {
354 printf(DRIVERNAME ": TX memory selftest fails\n");
358 if (mbir & MBIR_RXMBFA) {
359 printf(DRIVERNAME ": RX memory selftest fails\n");
363 debug(DRIVERNAME ": the selftest passes\n");
368 static void ks_setup(struct eth_device *dev)
372 /* Setup Transmit Frame Data Pointer Auto-Increment (TXFDPR) */
373 ks_wrreg16(dev, KS_TXFDPR, TXFDPR_TXFPAI);
375 /* Setup Receive Frame Data Pointer Auto-Increment */
376 ks_wrreg16(dev, KS_RXFDPR, RXFDPR_RXFPAI);
378 /* Setup Receive Frame Threshold - 1 frame (RXFCTFC) */
379 ks_wrreg16(dev, KS_RXFCTR, 1 & RXFCTR_THRESHOLD_MASK);
381 /* Setup RxQ Command Control (RXQCR) */
382 ks->rc_rxqcr = RXQCR_CMD_CNTL;
383 ks_wrreg16(dev, KS_RXQCR, ks->rc_rxqcr);
386 * set the force mode to half duplex, default is full duplex
387 * because if the auto-negotiation fails, most switch uses
390 w = ks_rdreg16(dev, KS_P1MBCR);
391 w &= ~P1MBCR_FORCE_FDX;
392 ks_wrreg16(dev, KS_P1MBCR, w);
394 w = TXCR_TXFCE | TXCR_TXPE | TXCR_TXCRC | TXCR_TCGIP;
395 ks_wrreg16(dev, KS_TXCR, w);
397 w = RXCR1_RXFCE | RXCR1_RXBE | RXCR1_RXUE | RXCR1_RXME | RXCR1_RXIPFCC;
402 ks_wrreg16(dev, KS_RXCR1, w);
405 static void ks_setup_int(struct eth_device *dev)
409 /* Clear the interrupts status of the hardware. */
410 ks_wrreg16(dev, KS_ISR, 0xffff);
412 /* Enables the interrupts of the hardware. */
413 ks->rc_ier = (IRQ_LCI | IRQ_TXI | IRQ_RXI);
416 static int ks8851_mll_detect_chip(struct eth_device *dev)
418 unsigned short val, i;
422 val = ks_rdreg16(dev, KS_CIDER);
425 /* Special case -- no chip present */
426 printf(DRIVERNAME ": is chip mounted ?\n");
428 } else if ((val & 0xfff0) != CIDER_ID) {
429 printf(DRIVERNAME ": Invalid chip id 0x%04x\n", val);
433 debug("Read back KS8851 id 0x%x\n", val);
435 /* only one entry in the table */
437 for (i = 0; chip_ids[i].id != 0; i++) {
438 if (chip_ids[i].id == val)
441 if (!chip_ids[i].id) {
442 printf(DRIVERNAME ": Unknown chip ID %04x\n", val);
446 dev->priv = (void *)&chip_ids[i];
451 static void ks8851_mll_reset(struct eth_device *dev)
453 /* wake up powermode to normal mode */
454 ks_set_powermode(dev, PMECR_PM_NORMAL);
455 mdelay(1); /* wait for normal mode to take effect */
457 /* Disable interrupt and reset */
458 ks_soft_reset(dev, GRR_GSR);
460 /* turn off the IRQs and ack any outstanding */
461 ks_wrreg16(dev, KS_IER, 0x0000);
462 ks_wrreg16(dev, KS_ISR, 0xffff);
464 /* shutdown RX/TX QMU */
468 static void ks8851_mll_phy_configure(struct eth_device *dev)
475 /* Probing the phy */
476 data = ks_rdreg16(dev, KS_OBCR);
477 ks_wrreg16(dev, KS_OBCR, data | OBCR_ODS_16MA);
479 debug(DRIVERNAME ": phy initialized\n");
482 static void ks8851_mll_enable(struct eth_device *dev)
484 ks_wrreg16(dev, KS_ISR, 0xffff);
489 static int ks8851_mll_init(struct eth_device *dev, bd_t *bd)
491 struct chip_id *id = dev->priv;
493 debug(DRIVERNAME ": detected %s controller\n", id->name);
495 if (ks_read_selftest(dev)) {
496 printf(DRIVERNAME ": Selftest failed\n");
500 ks8851_mll_reset(dev);
502 /* Configure the PHY, initialize the link state */
503 ks8851_mll_phy_configure(dev);
505 /* static allocation of private informations */
506 ks->frame_head_info = fr_h_i;
508 /* Turn on Tx + Rx */
509 ks8851_mll_enable(dev);
514 static void ks_write_qmu(struct eth_device *dev, u8 *pdata, u16 len)
516 /* start header at txb[0] to align txw entries */
518 ks->txh.txw[1] = cpu_to_le16(len);
520 /* 1. set sudo-DMA mode */
521 ks_wrreg16(dev, KS_TXFDPR, TXFDPR_TXFPAI);
522 ks_wrreg8(dev, KS_RXQCR, (ks->rc_rxqcr | RXQCR_SDA) & 0xff);
523 /* 2. write status/lenth info */
524 ks_outblk(dev, ks->txh.txw, 4);
525 /* 3. write pkt data */
526 ks_outblk(dev, (u16 *)pdata, ALIGN(len, 4));
527 /* 4. reset sudo-DMA mode */
528 ks_wrreg8(dev, KS_RXQCR, (ks->rc_rxqcr & ~RXQCR_SDA) & 0xff);
529 /* 5. Enqueue Tx(move the pkt from TX buffer into TXQ) */
530 ks_wrreg16(dev, KS_TXQCR, TXQCR_METFE);
531 /* 6. wait until TXQCR_METFE is auto-cleared */
532 do { } while (ks_rdreg16(dev, KS_TXQCR) & TXQCR_METFE);
535 static int ks8851_mll_send(struct eth_device *dev, void *packet, int length)
537 u8 *data = (u8 *)packet;
538 u16 tmplen = (u16)length;
542 * Extra space are required:
543 * 4 byte for alignment, 4 for status/length, 4 for CRC
545 retv = ks_rdreg16(dev, KS_TXMIR) & 0x1fff;
546 if (retv >= tmplen + 12) {
547 ks_write_qmu(dev, data, tmplen);
550 printf(DRIVERNAME ": failed to send packet: No buffer\n");
555 static void ks8851_mll_halt(struct eth_device *dev)
557 ks8851_mll_reset(dev);
561 * Maximum receive ring size; that is, the number of packets
562 * we can buffer before overflow happens. Basically, this just
563 * needs to be enough to prevent a packet being discarded while
564 * we are processing the previous one.
566 static int ks8851_mll_recv(struct eth_device *dev)
570 status = ks_rdreg16(dev, KS_ISR);
572 ks_wrreg16(dev, KS_ISR, status);
574 if ((status & IRQ_RXI))
575 ks_rcv(dev, (uchar **)net_rx_packets);
577 if ((status & IRQ_LDI)) {
578 u16 pmecr = ks_rdreg16(dev, KS_PMECR);
579 pmecr &= ~PMECR_WKEVT_MASK;
580 ks_wrreg16(dev, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
586 static int ks8851_mll_write_hwaddr(struct eth_device *dev)
588 u16 addrl, addrm, addrh;
590 addrh = (dev->enetaddr[0] << 8) | dev->enetaddr[1];
591 addrm = (dev->enetaddr[2] << 8) | dev->enetaddr[3];
592 addrl = (dev->enetaddr[4] << 8) | dev->enetaddr[5];
594 ks_wrreg16(dev, KS_MARH, addrh);
595 ks_wrreg16(dev, KS_MARM, addrm);
596 ks_wrreg16(dev, KS_MARL, addrl);
601 int ks8851_mll_initialize(u8 dev_num, int base_addr)
603 struct eth_device *dev;
605 dev = malloc(sizeof(*dev));
607 printf("Error: Failed to allocate memory\n");
610 memset(dev, 0, sizeof(*dev));
612 dev->iobase = base_addr;
616 /* Try to detect chip. Will fail if not present. */
617 if (ks8851_mll_detect_chip(dev)) {
622 dev->init = ks8851_mll_init;
623 dev->halt = ks8851_mll_halt;
624 dev->send = ks8851_mll_send;
625 dev->recv = ks8851_mll_recv;
626 dev->write_hwaddr = ks8851_mll_write_hwaddr;
627 sprintf(dev->name, "%s-%hu", DRIVERNAME, dev_num);