blob: 6643d1e9c134a6db3cfb807bb80fd8b523120a94 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Roberto Cerati45a16932013-04-24 10:46:17 +08002/*
3 * Micrel KS8851_MLL 16bit Network driver
4 * Copyright (c) 2011 Roberto Cerati <roberto.cerati@bticino.it>
Roberto Cerati45a16932013-04-24 10:46:17 +08005 */
6
Simon Glassf7ae49f2020-05-10 11:40:05 -06007#include <log.h>
Roberto Cerati45a16932013-04-24 10:46:17 +08008#include <asm/io.h>
9#include <common.h>
10#include <command.h>
11#include <malloc.h>
12#include <net.h>
13#include <miiphy.h>
Simon Glassc05ed002020-05-10 11:40:11 -060014#include <linux/delay.h>
Roberto Cerati45a16932013-04-24 10:46:17 +080015
16#include "ks8851_mll.h"
17
18#define DRIVERNAME "ks8851_mll"
19
20#define MAX_RECV_FRAMES 32
21#define MAX_BUF_SIZE 2048
22#define TX_BUF_SIZE 2000
23#define RX_BUF_SIZE 2000
24
25static const struct chip_id chip_ids[] = {
26 {CIDER_ID, "KSZ8851"},
27 {0, NULL},
28};
29
30/*
31 * union ks_tx_hdr - tx header data
32 * @txb: The header as bytes
33 * @txw: The header as 16bit, little-endian words
34 *
35 * A dual representation of the tx header data to allow
36 * access to individual bytes, and to allow 16bit accesses
37 * with 16bit alignment.
38 */
39union ks_tx_hdr {
40 u8 txb[4];
41 __le16 txw[2];
42};
43
44/*
45 * struct ks_net - KS8851 driver private data
46 * @net_device : The network device we're bound to
47 * @txh : temporaly buffer to save status/length.
48 * @frame_head_info : frame header information for multi-pkt rx.
49 * @statelock : Lock on this structure for tx list.
50 * @msg_enable : The message flags controlling driver output (see ethtool).
51 * @frame_cnt : number of frames received.
52 * @bus_width : i/o bus width.
53 * @irq : irq number assigned to this device.
54 * @rc_rxqcr : Cached copy of KS_RXQCR.
55 * @rc_txcr : Cached copy of KS_TXCR.
56 * @rc_ier : Cached copy of KS_IER.
57 * @sharedbus : Multipex(addr and data bus) mode indicator.
58 * @cmd_reg_cache : command register cached.
59 * @cmd_reg_cache_int : command register cached. Used in the irq handler.
60 * @promiscuous : promiscuous mode indicator.
61 * @all_mcast : mutlicast indicator.
62 * @mcast_lst_size : size of multicast list.
63 * @mcast_lst : multicast list.
64 * @mcast_bits : multicast enabed.
65 * @mac_addr : MAC address assigned to this device.
66 * @fid : frame id.
67 * @extra_byte : number of extra byte prepended rx pkt.
68 * @enabled : indicator this device works.
69 */
70
71/* Receive multiplex framer header info */
72struct type_frame_head {
73 u16 sts; /* Frame status */
74 u16 len; /* Byte count */
75} fr_h_i[MAX_RECV_FRAMES];
76
77struct ks_net {
78 struct net_device *netdev;
79 union ks_tx_hdr txh;
80 struct type_frame_head *frame_head_info;
81 u32 msg_enable;
82 u32 frame_cnt;
83 int bus_width;
84 int irq;
85 u16 rc_rxqcr;
86 u16 rc_txcr;
87 u16 rc_ier;
88 u16 sharedbus;
89 u16 cmd_reg_cache;
90 u16 cmd_reg_cache_int;
91 u16 promiscuous;
92 u16 all_mcast;
93 u16 mcast_lst_size;
94 u8 mcast_lst[MAX_MCAST_LST][MAC_ADDR_LEN];
95 u8 mcast_bits[HW_MCAST_SIZE];
96 u8 mac_addr[6];
97 u8 fid;
98 u8 extra_byte;
99 u8 enabled;
100} ks_str, *ks;
101
102#define BE3 0x8000 /* Byte Enable 3 */
103#define BE2 0x4000 /* Byte Enable 2 */
104#define BE1 0x2000 /* Byte Enable 1 */
105#define BE0 0x1000 /* Byte Enable 0 */
106
107static u8 ks_rdreg8(struct eth_device *dev, u16 offset)
108{
109 u8 shift_bit = offset & 0x03;
110 u8 shift_data = (offset & 1) << 3;
111
112 writew(offset | (BE0 << shift_bit), dev->iobase + 2);
113
114 return (u8)(readw(dev->iobase) >> shift_data);
115}
116
117static u16 ks_rdreg16(struct eth_device *dev, u16 offset)
118{
119 writew(offset | ((BE1 | BE0) << (offset & 0x02)), dev->iobase + 2);
120
121 return readw(dev->iobase);
122}
123
124static void ks_wrreg8(struct eth_device *dev, u16 offset, u8 val)
125{
126 u8 shift_bit = (offset & 0x03);
127 u16 value_write = (u16)(val << ((offset & 1) << 3));
128
129 writew(offset | (BE0 << shift_bit), dev->iobase + 2);
130 writew(value_write, dev->iobase);
131}
132
133static void ks_wrreg16(struct eth_device *dev, u16 offset, u16 val)
134{
135 writew(offset | ((BE1 | BE0) << (offset & 0x02)), dev->iobase + 2);
136 writew(val, dev->iobase);
137}
138
139/*
140 * ks_inblk - read a block of data from QMU. This is called after sudo DMA mode
141 * enabled.
142 * @ks: The chip state
143 * @wptr: buffer address to save data
144 * @len: length in byte to read
145 */
146static inline void ks_inblk(struct eth_device *dev, u16 *wptr, u32 len)
147{
148 len >>= 1;
149
150 while (len--)
151 *wptr++ = readw(dev->iobase);
152}
153
154/*
155 * ks_outblk - write data to QMU. This is called after sudo DMA mode enabled.
156 * @ks: The chip information
157 * @wptr: buffer address
158 * @len: length in byte to write
159 */
160static inline void ks_outblk(struct eth_device *dev, u16 *wptr, u32 len)
161{
162 len >>= 1;
163
164 while (len--)
165 writew(*wptr++, dev->iobase);
166}
167
168static void ks_enable_int(struct eth_device *dev)
169{
170 ks_wrreg16(dev, KS_IER, ks->rc_ier);
171}
172
173static void ks_set_powermode(struct eth_device *dev, unsigned pwrmode)
174{
175 unsigned pmecr;
176
177 ks_rdreg16(dev, KS_GRR);
178 pmecr = ks_rdreg16(dev, KS_PMECR);
179 pmecr &= ~PMECR_PM_MASK;
180 pmecr |= pwrmode;
181
182 ks_wrreg16(dev, KS_PMECR, pmecr);
183}
184
185/*
186 * ks_read_config - read chip configuration of bus width.
187 * @ks: The chip information
188 */
189static void ks_read_config(struct eth_device *dev)
190{
191 u16 reg_data = 0;
192
193 /* Regardless of bus width, 8 bit read should always work. */
194 reg_data = ks_rdreg8(dev, KS_CCR) & 0x00FF;
195 reg_data |= ks_rdreg8(dev, KS_CCR + 1) << 8;
196
197 /* addr/data bus are multiplexed */
198 ks->sharedbus = (reg_data & CCR_SHARED) == CCR_SHARED;
199
200 /*
201 * There are garbage data when reading data from QMU,
202 * depending on bus-width.
203 */
204 if (reg_data & CCR_8BIT) {
205 ks->bus_width = ENUM_BUS_8BIT;
206 ks->extra_byte = 1;
207 } else if (reg_data & CCR_16BIT) {
208 ks->bus_width = ENUM_BUS_16BIT;
209 ks->extra_byte = 2;
210 } else {
211 ks->bus_width = ENUM_BUS_32BIT;
212 ks->extra_byte = 4;
213 }
214}
215
216/*
217 * ks_soft_reset - issue one of the soft reset to the device
218 * @ks: The device state.
219 * @op: The bit(s) to set in the GRR
220 *
221 * Issue the relevant soft-reset command to the device's GRR register
222 * specified by @op.
223 *
224 * Note, the delays are in there as a caution to ensure that the reset
225 * has time to take effect and then complete. Since the datasheet does
226 * not currently specify the exact sequence, we have chosen something
227 * that seems to work with our device.
228 */
229static void ks_soft_reset(struct eth_device *dev, unsigned op)
230{
231 /* Disable interrupt first */
232 ks_wrreg16(dev, KS_IER, 0x0000);
233 ks_wrreg16(dev, KS_GRR, op);
234 mdelay(10); /* wait a short time to effect reset */
235 ks_wrreg16(dev, KS_GRR, 0);
236 mdelay(1); /* wait for condition to clear */
237}
238
239void ks_enable_qmu(struct eth_device *dev)
240{
241 u16 w;
242
243 w = ks_rdreg16(dev, KS_TXCR);
244
245 /* Enables QMU Transmit (TXCR). */
246 ks_wrreg16(dev, KS_TXCR, w | TXCR_TXE);
247
248 /* Enable RX Frame Count Threshold and Auto-Dequeue RXQ Frame */
249 w = ks_rdreg16(dev, KS_RXQCR);
250 ks_wrreg16(dev, KS_RXQCR, w | RXQCR_RXFCTE);
251
252 /* Enables QMU Receive (RXCR1). */
253 w = ks_rdreg16(dev, KS_RXCR1);
254 ks_wrreg16(dev, KS_RXCR1, w | RXCR1_RXE);
255}
256
257static void ks_disable_qmu(struct eth_device *dev)
258{
259 u16 w;
260
261 w = ks_rdreg16(dev, KS_TXCR);
262
263 /* Disables QMU Transmit (TXCR). */
264 w &= ~TXCR_TXE;
265 ks_wrreg16(dev, KS_TXCR, w);
266
267 /* Disables QMU Receive (RXCR1). */
268 w = ks_rdreg16(dev, KS_RXCR1);
269 w &= ~RXCR1_RXE;
270 ks_wrreg16(dev, KS_RXCR1, w);
271}
272
273static inline void ks_read_qmu(struct eth_device *dev, u16 *buf, u32 len)
274{
275 u32 r = ks->extra_byte & 0x1;
276 u32 w = ks->extra_byte - r;
277
278 /* 1. set sudo DMA mode */
279 ks_wrreg16(dev, KS_RXFDPR, RXFDPR_RXFPAI);
280 ks_wrreg8(dev, KS_RXQCR, (ks->rc_rxqcr | RXQCR_SDA) & 0xff);
281
282 /*
283 * 2. read prepend data
284 *
285 * read 4 + extra bytes and discard them.
286 * extra bytes for dummy, 2 for status, 2 for len
287 */
288
289 if (r)
290 ks_rdreg8(dev, 0);
291
292 ks_inblk(dev, buf, w + 2 + 2);
293
294 /* 3. read pkt data */
295 ks_inblk(dev, buf, ALIGN(len, 4));
296
297 /* 4. reset sudo DMA Mode */
298 ks_wrreg8(dev, KS_RXQCR, (ks->rc_rxqcr & ~RXQCR_SDA) & 0xff);
299}
300
301static void ks_rcv(struct eth_device *dev, uchar **pv_data)
302{
303 struct type_frame_head *frame_hdr = ks->frame_head_info;
304 int i;
305
306 ks->frame_cnt = ks_rdreg16(dev, KS_RXFCTR) >> 8;
307
308 /* read all header information */
309 for (i = 0; i < ks->frame_cnt; i++) {
310 /* Checking Received packet status */
311 frame_hdr->sts = ks_rdreg16(dev, KS_RXFHSR);
312 /* Get packet len from hardware */
313 frame_hdr->len = ks_rdreg16(dev, KS_RXFHBCR);
314 frame_hdr++;
315 }
316
317 frame_hdr = ks->frame_head_info;
318 while (ks->frame_cnt--) {
319 if ((frame_hdr->sts & RXFSHR_RXFV) &&
320 (frame_hdr->len < RX_BUF_SIZE) &&
321 frame_hdr->len) {
322 /* read data block including CRC 4 bytes */
323 ks_read_qmu(dev, (u16 *)(*pv_data), frame_hdr->len);
324
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500325 /* net_rx_packets buffer size is ok (*pv_data) */
326 net_process_received_packet(*pv_data, frame_hdr->len);
Roberto Cerati45a16932013-04-24 10:46:17 +0800327 pv_data++;
328 } else {
329 ks_wrreg16(dev, KS_RXQCR, (ks->rc_rxqcr | RXQCR_RRXEF));
330 printf(DRIVERNAME ": bad packet\n");
331 }
332 frame_hdr++;
333 }
334}
335
336/*
337 * ks_read_selftest - read the selftest memory info.
338 * @ks: The device state
339 *
340 * Read and check the TX/RX memory selftest information.
341 */
342static int ks_read_selftest(struct eth_device *dev)
343{
344 u16 both_done = MBIR_TXMBF | MBIR_RXMBF;
345 u16 mbir;
346 int ret = 0;
347
348 mbir = ks_rdreg16(dev, KS_MBIR);
349
350 if ((mbir & both_done) != both_done) {
351 printf(DRIVERNAME ": Memory selftest not finished\n");
352 return 0;
353 }
354
355 if (mbir & MBIR_TXMBFA) {
356 printf(DRIVERNAME ": TX memory selftest fails\n");
357 ret |= 1;
358 }
359
360 if (mbir & MBIR_RXMBFA) {
361 printf(DRIVERNAME ": RX memory selftest fails\n");
362 ret |= 2;
363 }
364
365 debug(DRIVERNAME ": the selftest passes\n");
366
367 return ret;
368}
369
370static void ks_setup(struct eth_device *dev)
371{
372 u16 w;
373
374 /* Setup Transmit Frame Data Pointer Auto-Increment (TXFDPR) */
375 ks_wrreg16(dev, KS_TXFDPR, TXFDPR_TXFPAI);
376
377 /* Setup Receive Frame Data Pointer Auto-Increment */
378 ks_wrreg16(dev, KS_RXFDPR, RXFDPR_RXFPAI);
379
380 /* Setup Receive Frame Threshold - 1 frame (RXFCTFC) */
381 ks_wrreg16(dev, KS_RXFCTR, 1 & RXFCTR_THRESHOLD_MASK);
382
383 /* Setup RxQ Command Control (RXQCR) */
384 ks->rc_rxqcr = RXQCR_CMD_CNTL;
385 ks_wrreg16(dev, KS_RXQCR, ks->rc_rxqcr);
386
387 /*
388 * set the force mode to half duplex, default is full duplex
389 * because if the auto-negotiation fails, most switch uses
390 * half-duplex.
391 */
392 w = ks_rdreg16(dev, KS_P1MBCR);
393 w &= ~P1MBCR_FORCE_FDX;
394 ks_wrreg16(dev, KS_P1MBCR, w);
395
396 w = TXCR_TXFCE | TXCR_TXPE | TXCR_TXCRC | TXCR_TCGIP;
397 ks_wrreg16(dev, KS_TXCR, w);
398
399 w = RXCR1_RXFCE | RXCR1_RXBE | RXCR1_RXUE | RXCR1_RXME | RXCR1_RXIPFCC;
400
401 /* Normal mode */
402 w |= RXCR1_RXPAFMA;
403
404 ks_wrreg16(dev, KS_RXCR1, w);
405}
406
407static void ks_setup_int(struct eth_device *dev)
408{
409 ks->rc_ier = 0x00;
410
411 /* Clear the interrupts status of the hardware. */
412 ks_wrreg16(dev, KS_ISR, 0xffff);
413
414 /* Enables the interrupts of the hardware. */
415 ks->rc_ier = (IRQ_LCI | IRQ_TXI | IRQ_RXI);
416}
417
418static int ks8851_mll_detect_chip(struct eth_device *dev)
419{
420 unsigned short val, i;
421
422 ks_read_config(dev);
423
424 val = ks_rdreg16(dev, KS_CIDER);
425
426 if (val == 0xffff) {
427 /* Special case -- no chip present */
428 printf(DRIVERNAME ": is chip mounted ?\n");
429 return -1;
430 } else if ((val & 0xfff0) != CIDER_ID) {
431 printf(DRIVERNAME ": Invalid chip id 0x%04x\n", val);
432 return -1;
433 }
434
435 debug("Read back KS8851 id 0x%x\n", val);
436
437 /* only one entry in the table */
438 val &= 0xfff0;
439 for (i = 0; chip_ids[i].id != 0; i++) {
440 if (chip_ids[i].id == val)
441 break;
442 }
443 if (!chip_ids[i].id) {
444 printf(DRIVERNAME ": Unknown chip ID %04x\n", val);
445 return -1;
446 }
447
448 dev->priv = (void *)&chip_ids[i];
449
450 return 0;
451}
452
453static void ks8851_mll_reset(struct eth_device *dev)
454{
455 /* wake up powermode to normal mode */
456 ks_set_powermode(dev, PMECR_PM_NORMAL);
457 mdelay(1); /* wait for normal mode to take effect */
458
459 /* Disable interrupt and reset */
460 ks_soft_reset(dev, GRR_GSR);
461
462 /* turn off the IRQs and ack any outstanding */
463 ks_wrreg16(dev, KS_IER, 0x0000);
464 ks_wrreg16(dev, KS_ISR, 0xffff);
465
466 /* shutdown RX/TX QMU */
467 ks_disable_qmu(dev);
468}
469
470static void ks8851_mll_phy_configure(struct eth_device *dev)
471{
472 u16 data;
473
474 ks_setup(dev);
475 ks_setup_int(dev);
476
477 /* Probing the phy */
478 data = ks_rdreg16(dev, KS_OBCR);
479 ks_wrreg16(dev, KS_OBCR, data | OBCR_ODS_16MA);
480
481 debug(DRIVERNAME ": phy initialized\n");
482}
483
484static void ks8851_mll_enable(struct eth_device *dev)
485{
486 ks_wrreg16(dev, KS_ISR, 0xffff);
487 ks_enable_int(dev);
488 ks_enable_qmu(dev);
489}
490
491static int ks8851_mll_init(struct eth_device *dev, bd_t *bd)
492{
493 struct chip_id *id = dev->priv;
494
495 debug(DRIVERNAME ": detected %s controller\n", id->name);
496
497 if (ks_read_selftest(dev)) {
498 printf(DRIVERNAME ": Selftest failed\n");
499 return -1;
500 }
501
502 ks8851_mll_reset(dev);
503
504 /* Configure the PHY, initialize the link state */
505 ks8851_mll_phy_configure(dev);
506
507 /* static allocation of private informations */
508 ks->frame_head_info = fr_h_i;
509
510 /* Turn on Tx + Rx */
511 ks8851_mll_enable(dev);
512
513 return 0;
514}
515
516static void ks_write_qmu(struct eth_device *dev, u8 *pdata, u16 len)
517{
518 /* start header at txb[0] to align txw entries */
519 ks->txh.txw[0] = 0;
520 ks->txh.txw[1] = cpu_to_le16(len);
521
522 /* 1. set sudo-DMA mode */
523 ks_wrreg16(dev, KS_TXFDPR, TXFDPR_TXFPAI);
524 ks_wrreg8(dev, KS_RXQCR, (ks->rc_rxqcr | RXQCR_SDA) & 0xff);
525 /* 2. write status/lenth info */
526 ks_outblk(dev, ks->txh.txw, 4);
527 /* 3. write pkt data */
528 ks_outblk(dev, (u16 *)pdata, ALIGN(len, 4));
529 /* 4. reset sudo-DMA mode */
530 ks_wrreg8(dev, KS_RXQCR, (ks->rc_rxqcr & ~RXQCR_SDA) & 0xff);
531 /* 5. Enqueue Tx(move the pkt from TX buffer into TXQ) */
532 ks_wrreg16(dev, KS_TXQCR, TXQCR_METFE);
533 /* 6. wait until TXQCR_METFE is auto-cleared */
534 do { } while (ks_rdreg16(dev, KS_TXQCR) & TXQCR_METFE);
535}
536
537static int ks8851_mll_send(struct eth_device *dev, void *packet, int length)
538{
539 u8 *data = (u8 *)packet;
540 u16 tmplen = (u16)length;
541 u16 retv;
542
543 /*
544 * Extra space are required:
545 * 4 byte for alignment, 4 for status/length, 4 for CRC
546 */
547 retv = ks_rdreg16(dev, KS_TXMIR) & 0x1fff;
548 if (retv >= tmplen + 12) {
549 ks_write_qmu(dev, data, tmplen);
550 return 0;
551 } else {
552 printf(DRIVERNAME ": failed to send packet: No buffer\n");
553 return -1;
554 }
555}
556
557static void ks8851_mll_halt(struct eth_device *dev)
558{
559 ks8851_mll_reset(dev);
560}
561
562/*
563 * Maximum receive ring size; that is, the number of packets
564 * we can buffer before overflow happens. Basically, this just
565 * needs to be enough to prevent a packet being discarded while
566 * we are processing the previous one.
567 */
568static int ks8851_mll_recv(struct eth_device *dev)
569{
570 u16 status;
571
572 status = ks_rdreg16(dev, KS_ISR);
573
574 ks_wrreg16(dev, KS_ISR, status);
575
576 if ((status & IRQ_RXI))
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500577 ks_rcv(dev, (uchar **)net_rx_packets);
Roberto Cerati45a16932013-04-24 10:46:17 +0800578
579 if ((status & IRQ_LDI)) {
580 u16 pmecr = ks_rdreg16(dev, KS_PMECR);
581 pmecr &= ~PMECR_WKEVT_MASK;
582 ks_wrreg16(dev, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
583 }
584
585 return 0;
586}
587
588static int ks8851_mll_write_hwaddr(struct eth_device *dev)
589{
590 u16 addrl, addrm, addrh;
591
592 addrh = (dev->enetaddr[0] << 8) | dev->enetaddr[1];
593 addrm = (dev->enetaddr[2] << 8) | dev->enetaddr[3];
594 addrl = (dev->enetaddr[4] << 8) | dev->enetaddr[5];
595
596 ks_wrreg16(dev, KS_MARH, addrh);
597 ks_wrreg16(dev, KS_MARM, addrm);
598 ks_wrreg16(dev, KS_MARL, addrl);
599
600 return 0;
601}
602
603int ks8851_mll_initialize(u8 dev_num, int base_addr)
604{
605 struct eth_device *dev;
606
607 dev = malloc(sizeof(*dev));
608 if (!dev) {
609 printf("Error: Failed to allocate memory\n");
610 return -1;
611 }
612 memset(dev, 0, sizeof(*dev));
613
614 dev->iobase = base_addr;
615
616 ks = &ks_str;
617
618 /* Try to detect chip. Will fail if not present. */
619 if (ks8851_mll_detect_chip(dev)) {
620 free(dev);
621 return -1;
622 }
623
624 dev->init = ks8851_mll_init;
625 dev->halt = ks8851_mll_halt;
626 dev->send = ks8851_mll_send;
627 dev->recv = ks8851_mll_recv;
628 dev->write_hwaddr = ks8851_mll_write_hwaddr;
629 sprintf(dev->name, "%s-%hu", DRIVERNAME, dev_num);
630
631 eth_register(dev);
632
633 return 0;
634}