1 // SPDX-License-Identifier: GPL-2.0+
3 * Micrel KS8851_MLL 16bit Network driver
14 #include <linux/delay.h>
16 #include "ks8851_mll.h"
18 #define DRIVERNAME "ks8851_mll"
20 #define RX_BUF_SIZE 2000
23 * struct ks_net - KS8851 driver private data
24 * @dev : legacy non-DM ethernet device structure
25 * @iobase : register base
26 * @bus_width : i/o bus width.
27 * @sharedbus : Multipex(addr and data bus) mode indicator.
28 * @extra_byte : number of extra byte prepended rx pkt.
32 struct eth_device dev;
41 #define BE3 0x8000 /* Byte Enable 3 */
42 #define BE2 0x4000 /* Byte Enable 2 */
43 #define BE1 0x2000 /* Byte Enable 1 */
44 #define BE0 0x1000 /* Byte Enable 0 */
46 static u8 ks_rdreg8(struct ks_net *ks, u16 offset)
48 u8 shift_bit = offset & 0x03;
49 u8 shift_data = (offset & 1) << 3;
51 writew(offset | (BE0 << shift_bit), ks->iobase + 2);
53 return (u8)(readw(ks->iobase) >> shift_data);
56 static u16 ks_rdreg16(struct ks_net *ks, u16 offset)
58 writew(offset | ((BE1 | BE0) << (offset & 0x02)), ks->iobase + 2);
60 return readw(ks->iobase);
63 static void ks_wrreg16(struct ks_net *ks, u16 offset, u16 val)
65 writew(offset | ((BE1 | BE0) << (offset & 0x02)), ks->iobase + 2);
66 writew(val, ks->iobase);
70 * ks_inblk - read a block of data from QMU. This is called after sudo DMA mode
73 * @wptr: buffer address to save data
74 * @len: length in byte to read
76 static inline void ks_inblk(struct ks_net *ks, u16 *wptr, u32 len)
81 *wptr++ = readw(ks->iobase);
85 * ks_outblk - write data to QMU. This is called after sudo DMA mode enabled.
86 * @ks: The chip information
87 * @wptr: buffer address
88 * @len: length in byte to write
90 static inline void ks_outblk(struct ks_net *ks, u16 *wptr, u32 len)
95 writew(*wptr++, ks->iobase);
98 static void ks_enable_int(struct ks_net *ks)
100 ks_wrreg16(ks, KS_IER, IRQ_LCI | IRQ_TXI | IRQ_RXI);
103 static void ks_set_powermode(struct ks_net *ks, unsigned int pwrmode)
107 ks_rdreg16(ks, KS_GRR);
108 pmecr = ks_rdreg16(ks, KS_PMECR);
109 pmecr &= ~PMECR_PM_MASK;
112 ks_wrreg16(ks, KS_PMECR, pmecr);
116 * ks_read_config - read chip configuration of bus width.
117 * @ks: The chip information
119 static void ks_read_config(struct ks_net *ks)
123 /* Regardless of bus width, 8 bit read should always work. */
124 reg_data = ks_rdreg8(ks, KS_CCR) & 0x00FF;
125 reg_data |= ks_rdreg8(ks, KS_CCR + 1) << 8;
127 /* addr/data bus are multiplexed */
128 ks->sharedbus = (reg_data & CCR_SHARED) == CCR_SHARED;
131 * There are garbage data when reading data from QMU,
132 * depending on bus-width.
134 if (reg_data & CCR_8BIT) {
135 ks->bus_width = ENUM_BUS_8BIT;
137 } else if (reg_data & CCR_16BIT) {
138 ks->bus_width = ENUM_BUS_16BIT;
141 ks->bus_width = ENUM_BUS_32BIT;
147 * ks_soft_reset - issue one of the soft reset to the device
148 * @ks: The device state.
149 * @op: The bit(s) to set in the GRR
151 * Issue the relevant soft-reset command to the device's GRR register
154 * Note, the delays are in there as a caution to ensure that the reset
155 * has time to take effect and then complete. Since the datasheet does
156 * not currently specify the exact sequence, we have chosen something
157 * that seems to work with our device.
159 static void ks_soft_reset(struct ks_net *ks, unsigned int op)
161 /* Disable interrupt first */
162 ks_wrreg16(ks, KS_IER, 0x0000);
163 ks_wrreg16(ks, KS_GRR, op);
164 mdelay(10); /* wait a short time to effect reset */
165 ks_wrreg16(ks, KS_GRR, 0);
166 mdelay(1); /* wait for condition to clear */
169 void ks_enable_qmu(struct ks_net *ks)
173 w = ks_rdreg16(ks, KS_TXCR);
175 /* Enables QMU Transmit (TXCR). */
176 ks_wrreg16(ks, KS_TXCR, w | TXCR_TXE);
178 /* Enable RX Frame Count Threshold and Auto-Dequeue RXQ Frame */
179 w = ks_rdreg16(ks, KS_RXQCR);
180 ks_wrreg16(ks, KS_RXQCR, w | RXQCR_RXFCTE);
182 /* Enables QMU Receive (RXCR1). */
183 w = ks_rdreg16(ks, KS_RXCR1);
184 ks_wrreg16(ks, KS_RXCR1, w | RXCR1_RXE);
187 static void ks_disable_qmu(struct ks_net *ks)
191 w = ks_rdreg16(ks, KS_TXCR);
193 /* Disables QMU Transmit (TXCR). */
195 ks_wrreg16(ks, KS_TXCR, w);
197 /* Disables QMU Receive (RXCR1). */
198 w = ks_rdreg16(ks, KS_RXCR1);
200 ks_wrreg16(ks, KS_RXCR1, w);
203 static inline void ks_read_qmu(struct ks_net *ks, u16 *buf, u32 len)
205 u32 r = ks->extra_byte & 0x1;
206 u32 w = ks->extra_byte - r;
208 /* 1. set sudo DMA mode */
209 ks_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI);
210 ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL | RXQCR_SDA);
213 * 2. read prepend data
215 * read 4 + extra bytes and discard them.
216 * extra bytes for dummy, 2 for status, 2 for len
222 ks_inblk(ks, buf, w + 2 + 2);
224 /* 3. read pkt data */
225 ks_inblk(ks, buf, ALIGN(len, 4));
227 /* 4. reset sudo DMA Mode */
228 ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL);
231 static int ks_rcv(struct ks_net *ks, uchar *data)
236 ks->rxfc = ks_rdreg16(ks, KS_RXFCTR) >> 8;
241 /* Checking Received packet status */
242 sts = ks_rdreg16(ks, KS_RXFHSR);
243 /* Get packet len from hardware */
244 len = ks_rdreg16(ks, KS_RXFHBCR);
246 if ((sts & RXFSHR_RXFV) && len && (len < RX_BUF_SIZE)) {
247 /* read data block including CRC 4 bytes */
248 ks_read_qmu(ks, (u16 *)data, len);
253 ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL | RXQCR_RRXEF);
254 printf(DRIVERNAME ": bad packet\n");
259 * ks_read_selftest - read the selftest memory info.
260 * @ks: The device state
262 * Read and check the TX/RX memory selftest information.
264 static int ks_read_selftest(struct ks_net *ks)
266 u16 both_done = MBIR_TXMBF | MBIR_RXMBF;
270 mbir = ks_rdreg16(ks, KS_MBIR);
272 if ((mbir & both_done) != both_done) {
273 printf(DRIVERNAME ": Memory selftest not finished\n");
277 if (mbir & MBIR_TXMBFA) {
278 printf(DRIVERNAME ": TX memory selftest fails\n");
282 if (mbir & MBIR_RXMBFA) {
283 printf(DRIVERNAME ": RX memory selftest fails\n");
287 debug(DRIVERNAME ": the selftest passes\n");
292 static void ks_setup(struct ks_net *ks)
296 /* Setup Transmit Frame Data Pointer Auto-Increment (TXFDPR) */
297 ks_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
299 /* Setup Receive Frame Data Pointer Auto-Increment */
300 ks_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI);
302 /* Setup Receive Frame Threshold - 1 frame (RXFCTFC) */
303 ks_wrreg16(ks, KS_RXFCTR, 1 & RXFCTR_THRESHOLD_MASK);
305 /* Setup RxQ Command Control (RXQCR) */
306 ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL);
309 * set the force mode to half duplex, default is full duplex
310 * because if the auto-negotiation fails, most switch uses
313 w = ks_rdreg16(ks, KS_P1MBCR);
314 w &= ~P1MBCR_FORCE_FDX;
315 ks_wrreg16(ks, KS_P1MBCR, w);
317 w = TXCR_TXFCE | TXCR_TXPE | TXCR_TXCRC | TXCR_TCGIP;
318 ks_wrreg16(ks, KS_TXCR, w);
320 w = RXCR1_RXFCE | RXCR1_RXBE | RXCR1_RXUE | RXCR1_RXME | RXCR1_RXIPFCC;
325 ks_wrreg16(ks, KS_RXCR1, w);
328 static void ks_setup_int(struct ks_net *ks)
330 /* Clear the interrupts status of the hardware. */
331 ks_wrreg16(ks, KS_ISR, 0xffff);
334 static int ks8851_mll_detect_chip(struct ks_net *ks)
340 val = ks_rdreg16(ks, KS_CIDER);
343 /* Special case -- no chip present */
344 printf(DRIVERNAME ": is chip mounted ?\n");
346 } else if ((val & 0xfff0) != CIDER_ID) {
347 printf(DRIVERNAME ": Invalid chip id 0x%04x\n", val);
351 debug("Read back KS8851 id 0x%x\n", val);
353 if ((val & 0xfff0) != CIDER_ID) {
354 printf(DRIVERNAME ": Unknown chip ID %04x\n", val);
361 static void ks8851_mll_reset(struct ks_net *ks)
363 /* wake up powermode to normal mode */
364 ks_set_powermode(ks, PMECR_PM_NORMAL);
365 mdelay(1); /* wait for normal mode to take effect */
367 /* Disable interrupt and reset */
368 ks_soft_reset(ks, GRR_GSR);
370 /* turn off the IRQs and ack any outstanding */
371 ks_wrreg16(ks, KS_IER, 0x0000);
372 ks_wrreg16(ks, KS_ISR, 0xffff);
374 /* shutdown RX/TX QMU */
378 static void ks8851_mll_phy_configure(struct ks_net *ks)
385 /* Probing the phy */
386 data = ks_rdreg16(ks, KS_OBCR);
387 ks_wrreg16(ks, KS_OBCR, data | OBCR_ODS_16MA);
389 debug(DRIVERNAME ": phy initialized\n");
392 static void ks8851_mll_enable(struct ks_net *ks)
394 ks_wrreg16(ks, KS_ISR, 0xffff);
399 static int ks8851_mll_init_common(struct ks_net *ks)
401 if (ks_read_selftest(ks)) {
402 printf(DRIVERNAME ": Selftest failed\n");
406 ks8851_mll_reset(ks);
408 /* Configure the PHY, initialize the link state */
409 ks8851_mll_phy_configure(ks);
413 /* Turn on Tx + Rx */
414 ks8851_mll_enable(ks);
419 static void ks_write_qmu(struct ks_net *ks, u8 *pdata, u16 len)
422 /* start header at txb[0] to align txw entries */
424 txw[1] = cpu_to_le16(len);
426 /* 1. set sudo-DMA mode */
427 ks_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
428 ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL | RXQCR_SDA);
429 /* 2. write status/length info */
430 ks_outblk(ks, txw, 4);
431 /* 3. write pkt data */
432 ks_outblk(ks, (u16 *)pdata, ALIGN(len, 4));
433 /* 4. reset sudo-DMA mode */
434 ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL);
435 /* 5. Enqueue Tx(move the pkt from TX buffer into TXQ) */
436 ks_wrreg16(ks, KS_TXQCR, TXQCR_METFE);
437 /* 6. wait until TXQCR_METFE is auto-cleared */
438 do { } while (ks_rdreg16(ks, KS_TXQCR) & TXQCR_METFE);
441 static int ks8851_mll_send_common(struct ks_net *ks, void *packet, int length)
443 u8 *data = (u8 *)packet;
444 u16 tmplen = (u16)length;
448 * Extra space are required:
449 * 4 byte for alignment, 4 for status/length, 4 for CRC
451 retv = ks_rdreg16(ks, KS_TXMIR) & 0x1fff;
452 if (retv >= tmplen + 12) {
453 ks_write_qmu(ks, data, tmplen);
457 printf(DRIVERNAME ": failed to send packet: No buffer\n");
461 static void ks8851_mll_halt_common(struct ks_net *ks)
463 ks8851_mll_reset(ks);
467 * Maximum receive ring size; that is, the number of packets
468 * we can buffer before overflow happens. Basically, this just
469 * needs to be enough to prevent a packet being discarded while
470 * we are processing the previous one.
472 static int ks8851_mll_recv_common(struct ks_net *ks, uchar *data)
477 status = ks_rdreg16(ks, KS_ISR);
479 ks_wrreg16(ks, KS_ISR, status);
481 if (ks->rxfc || (status & IRQ_RXI))
482 ret = ks_rcv(ks, data);
484 if (status & IRQ_LDI) {
485 u16 pmecr = ks_rdreg16(ks, KS_PMECR);
487 pmecr &= ~PMECR_WKEVT_MASK;
488 ks_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
494 static void ks8851_mll_write_hwaddr_common(struct ks_net *ks, u8 enetaddr[6])
496 u16 addrl, addrm, addrh;
498 addrh = (enetaddr[0] << 8) | enetaddr[1];
499 addrm = (enetaddr[2] << 8) | enetaddr[3];
500 addrl = (enetaddr[4] << 8) | enetaddr[5];
502 ks_wrreg16(ks, KS_MARH, addrh);
503 ks_wrreg16(ks, KS_MARM, addrm);
504 ks_wrreg16(ks, KS_MARL, addrl);
507 #ifndef CONFIG_DM_ETH
508 static int ks8851_mll_init(struct eth_device *dev, struct bd_info *bd)
510 struct ks_net *ks = container_of(dev, struct ks_net, dev);
512 return ks8851_mll_init_common(ks);
515 static void ks8851_mll_halt(struct eth_device *dev)
517 struct ks_net *ks = container_of(dev, struct ks_net, dev);
519 ks8851_mll_halt_common(ks);
522 static int ks8851_mll_send(struct eth_device *dev, void *packet, int length)
524 struct ks_net *ks = container_of(dev, struct ks_net, dev);
526 return ks8851_mll_send_common(ks, packet, length);
529 static int ks8851_mll_recv(struct eth_device *dev)
531 struct ks_net *ks = container_of(dev, struct ks_net, dev);
534 ret = ks8851_mll_recv_common(ks, net_rx_packets[0]);
536 net_process_received_packet(net_rx_packets[0], ret);
541 static int ks8851_mll_write_hwaddr(struct eth_device *dev)
543 struct ks_net *ks = container_of(dev, struct ks_net, dev);
545 ks8851_mll_write_hwaddr_common(ks, ks->dev.enetaddr);
550 int ks8851_mll_initialize(u8 dev_num, int base_addr)
554 ks = calloc(1, sizeof(*ks));
558 ks->iobase = base_addr;
560 /* Try to detect chip. Will fail if not present. */
561 if (ks8851_mll_detect_chip(ks)) {
566 ks->dev.init = ks8851_mll_init;
567 ks->dev.halt = ks8851_mll_halt;
568 ks->dev.send = ks8851_mll_send;
569 ks->dev.recv = ks8851_mll_recv;
570 ks->dev.write_hwaddr = ks8851_mll_write_hwaddr;
571 sprintf(ks->dev.name, "%s-%hu", DRIVERNAME, dev_num);
573 eth_register(&ks->dev);
577 #else /* ifdef CONFIG_DM_ETH */
578 static int ks8851_start(struct udevice *dev)
580 struct ks_net *ks = dev_get_priv(dev);
582 return ks8851_mll_init_common(ks);
585 static void ks8851_stop(struct udevice *dev)
587 struct ks_net *ks = dev_get_priv(dev);
589 ks8851_mll_halt_common(ks);
592 static int ks8851_send(struct udevice *dev, void *packet, int length)
594 struct ks_net *ks = dev_get_priv(dev);
597 ret = ks8851_mll_send_common(ks, packet, length);
599 return ret ? 0 : -ETIMEDOUT;
602 static int ks8851_recv(struct udevice *dev, int flags, uchar **packetp)
604 struct ks_net *ks = dev_get_priv(dev);
605 uchar *data = net_rx_packets[0];
608 ret = ks8851_mll_recv_common(ks, data);
610 *packetp = (void *)data;
612 return ret ? ret : -EAGAIN;
615 static int ks8851_write_hwaddr(struct udevice *dev)
617 struct ks_net *ks = dev_get_priv(dev);
618 struct eth_pdata *pdata = dev_get_platdata(dev);
620 ks8851_mll_write_hwaddr_common(ks, pdata->enetaddr);
625 static int ks8851_read_rom_hwaddr(struct udevice *dev)
627 struct ks_net *ks = dev_get_priv(dev);
628 struct eth_pdata *pdata = dev_get_platdata(dev);
629 u16 addrl, addrm, addrh;
631 /* No EEPROM means no valid MAC address. */
632 if (!(ks_rdreg16(ks, KS_CCR) & CCR_EEPROM))
636 * If the EEPROM contains valid MAC address, it is loaded into
637 * the NIC on power on. Read the MAC out of the NIC registers.
639 addrl = ks_rdreg16(ks, KS_MARL);
640 addrm = ks_rdreg16(ks, KS_MARM);
641 addrh = ks_rdreg16(ks, KS_MARH);
643 pdata->enetaddr[0] = (addrh >> 8) & 0xff;
644 pdata->enetaddr[1] = addrh & 0xff;
645 pdata->enetaddr[2] = (addrm >> 8) & 0xff;
646 pdata->enetaddr[3] = addrm & 0xff;
647 pdata->enetaddr[4] = (addrl >> 8) & 0xff;
648 pdata->enetaddr[5] = addrl & 0xff;
650 return !is_valid_ethaddr(pdata->enetaddr);
653 static int ks8851_bind(struct udevice *dev)
655 return device_set_name(dev, dev->name);
658 static int ks8851_probe(struct udevice *dev)
660 struct ks_net *ks = dev_get_priv(dev);
662 /* Try to detect chip. Will fail if not present. */
663 ks8851_mll_detect_chip(ks);
668 static int ks8851_ofdata_to_platdata(struct udevice *dev)
670 struct ks_net *ks = dev_get_priv(dev);
671 struct eth_pdata *pdata = dev_get_platdata(dev);
673 pdata->iobase = dev_read_addr(dev);
674 ks->iobase = pdata->iobase;
679 static const struct eth_ops ks8851_ops = {
680 .start = ks8851_start,
684 .write_hwaddr = ks8851_write_hwaddr,
685 .read_rom_hwaddr = ks8851_read_rom_hwaddr,
688 static const struct udevice_id ks8851_ids[] = {
689 { .compatible = "micrel,ks8851-mll" },
693 U_BOOT_DRIVER(ks8851) = {
694 .name = "eth_ks8851",
696 .of_match = ks8851_ids,
698 .ofdata_to_platdata = ks8851_ofdata_to_platdata,
699 .probe = ks8851_probe,
701 .priv_auto = sizeof(struct ks_net),
702 .platdata_auto = sizeof(struct eth_pdata),
703 .flags = DM_FLAG_ALLOC_PRIV_DMA,