blob: 17aa4fb88609745c524003690a07406e642506e9 [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/*
Roberto Cerati45a16932013-04-24 10:46:17 +080031 * struct ks_net - KS8851 driver private data
Roberto Cerati45a16932013-04-24 10:46:17 +080032 * @frame_head_info : frame header information for multi-pkt rx.
Roberto Cerati45a16932013-04-24 10:46:17 +080033 * @bus_width : i/o bus width.
Roberto Cerati45a16932013-04-24 10:46:17 +080034 * @sharedbus : Multipex(addr and data bus) mode indicator.
Roberto Cerati45a16932013-04-24 10:46:17 +080035 * @extra_byte : number of extra byte prepended rx pkt.
Roberto Cerati45a16932013-04-24 10:46:17 +080036 */
37
38/* Receive multiplex framer header info */
39struct type_frame_head {
40 u16 sts; /* Frame status */
41 u16 len; /* Byte count */
42} fr_h_i[MAX_RECV_FRAMES];
43
44struct ks_net {
Roberto Cerati45a16932013-04-24 10:46:17 +080045 struct type_frame_head *frame_head_info;
Roberto Cerati45a16932013-04-24 10:46:17 +080046 int bus_width;
Roberto Cerati45a16932013-04-24 10:46:17 +080047 u16 sharedbus;
Roberto Cerati45a16932013-04-24 10:46:17 +080048 u8 extra_byte;
Roberto Cerati45a16932013-04-24 10:46:17 +080049} ks_str, *ks;
50
51#define BE3 0x8000 /* Byte Enable 3 */
52#define BE2 0x4000 /* Byte Enable 2 */
53#define BE1 0x2000 /* Byte Enable 1 */
54#define BE0 0x1000 /* Byte Enable 0 */
55
56static u8 ks_rdreg8(struct eth_device *dev, u16 offset)
57{
58 u8 shift_bit = offset & 0x03;
59 u8 shift_data = (offset & 1) << 3;
60
61 writew(offset | (BE0 << shift_bit), dev->iobase + 2);
62
63 return (u8)(readw(dev->iobase) >> shift_data);
64}
65
66static u16 ks_rdreg16(struct eth_device *dev, u16 offset)
67{
68 writew(offset | ((BE1 | BE0) << (offset & 0x02)), dev->iobase + 2);
69
70 return readw(dev->iobase);
71}
72
Roberto Cerati45a16932013-04-24 10:46:17 +080073static void ks_wrreg16(struct eth_device *dev, u16 offset, u16 val)
74{
75 writew(offset | ((BE1 | BE0) << (offset & 0x02)), dev->iobase + 2);
76 writew(val, dev->iobase);
77}
78
79/*
80 * ks_inblk - read a block of data from QMU. This is called after sudo DMA mode
81 * enabled.
82 * @ks: The chip state
83 * @wptr: buffer address to save data
84 * @len: length in byte to read
85 */
86static inline void ks_inblk(struct eth_device *dev, u16 *wptr, u32 len)
87{
88 len >>= 1;
89
90 while (len--)
91 *wptr++ = readw(dev->iobase);
92}
93
94/*
95 * ks_outblk - write data to QMU. This is called after sudo DMA mode enabled.
96 * @ks: The chip information
97 * @wptr: buffer address
98 * @len: length in byte to write
99 */
100static inline void ks_outblk(struct eth_device *dev, u16 *wptr, u32 len)
101{
102 len >>= 1;
103
104 while (len--)
105 writew(*wptr++, dev->iobase);
106}
107
108static void ks_enable_int(struct eth_device *dev)
109{
Marek Vasutb0435972020-03-25 17:18:55 +0100110 ks_wrreg16(dev, KS_IER, IRQ_LCI | IRQ_TXI | IRQ_RXI);
Roberto Cerati45a16932013-04-24 10:46:17 +0800111}
112
113static void ks_set_powermode(struct eth_device *dev, unsigned pwrmode)
114{
115 unsigned pmecr;
116
117 ks_rdreg16(dev, KS_GRR);
118 pmecr = ks_rdreg16(dev, KS_PMECR);
119 pmecr &= ~PMECR_PM_MASK;
120 pmecr |= pwrmode;
121
122 ks_wrreg16(dev, KS_PMECR, pmecr);
123}
124
125/*
126 * ks_read_config - read chip configuration of bus width.
127 * @ks: The chip information
128 */
129static void ks_read_config(struct eth_device *dev)
130{
131 u16 reg_data = 0;
132
133 /* Regardless of bus width, 8 bit read should always work. */
134 reg_data = ks_rdreg8(dev, KS_CCR) & 0x00FF;
135 reg_data |= ks_rdreg8(dev, KS_CCR + 1) << 8;
136
137 /* addr/data bus are multiplexed */
138 ks->sharedbus = (reg_data & CCR_SHARED) == CCR_SHARED;
139
140 /*
141 * There are garbage data when reading data from QMU,
142 * depending on bus-width.
143 */
144 if (reg_data & CCR_8BIT) {
145 ks->bus_width = ENUM_BUS_8BIT;
146 ks->extra_byte = 1;
147 } else if (reg_data & CCR_16BIT) {
148 ks->bus_width = ENUM_BUS_16BIT;
149 ks->extra_byte = 2;
150 } else {
151 ks->bus_width = ENUM_BUS_32BIT;
152 ks->extra_byte = 4;
153 }
154}
155
156/*
157 * ks_soft_reset - issue one of the soft reset to the device
158 * @ks: The device state.
159 * @op: The bit(s) to set in the GRR
160 *
161 * Issue the relevant soft-reset command to the device's GRR register
162 * specified by @op.
163 *
164 * Note, the delays are in there as a caution to ensure that the reset
165 * has time to take effect and then complete. Since the datasheet does
166 * not currently specify the exact sequence, we have chosen something
167 * that seems to work with our device.
168 */
169static void ks_soft_reset(struct eth_device *dev, unsigned op)
170{
171 /* Disable interrupt first */
172 ks_wrreg16(dev, KS_IER, 0x0000);
173 ks_wrreg16(dev, KS_GRR, op);
174 mdelay(10); /* wait a short time to effect reset */
175 ks_wrreg16(dev, KS_GRR, 0);
176 mdelay(1); /* wait for condition to clear */
177}
178
179void ks_enable_qmu(struct eth_device *dev)
180{
181 u16 w;
182
183 w = ks_rdreg16(dev, KS_TXCR);
184
185 /* Enables QMU Transmit (TXCR). */
186 ks_wrreg16(dev, KS_TXCR, w | TXCR_TXE);
187
188 /* Enable RX Frame Count Threshold and Auto-Dequeue RXQ Frame */
189 w = ks_rdreg16(dev, KS_RXQCR);
190 ks_wrreg16(dev, KS_RXQCR, w | RXQCR_RXFCTE);
191
192 /* Enables QMU Receive (RXCR1). */
193 w = ks_rdreg16(dev, KS_RXCR1);
194 ks_wrreg16(dev, KS_RXCR1, w | RXCR1_RXE);
195}
196
197static void ks_disable_qmu(struct eth_device *dev)
198{
199 u16 w;
200
201 w = ks_rdreg16(dev, KS_TXCR);
202
203 /* Disables QMU Transmit (TXCR). */
204 w &= ~TXCR_TXE;
205 ks_wrreg16(dev, KS_TXCR, w);
206
207 /* Disables QMU Receive (RXCR1). */
208 w = ks_rdreg16(dev, KS_RXCR1);
209 w &= ~RXCR1_RXE;
210 ks_wrreg16(dev, KS_RXCR1, w);
211}
212
213static inline void ks_read_qmu(struct eth_device *dev, u16 *buf, u32 len)
214{
215 u32 r = ks->extra_byte & 0x1;
216 u32 w = ks->extra_byte - r;
217
218 /* 1. set sudo DMA mode */
219 ks_wrreg16(dev, KS_RXFDPR, RXFDPR_RXFPAI);
Marek Vasut6a457312020-03-25 17:02:51 +0100220 ks_wrreg16(dev, KS_RXQCR, RXQCR_CMD_CNTL | RXQCR_SDA);
Roberto Cerati45a16932013-04-24 10:46:17 +0800221
222 /*
223 * 2. read prepend data
224 *
225 * read 4 + extra bytes and discard them.
226 * extra bytes for dummy, 2 for status, 2 for len
227 */
228
229 if (r)
230 ks_rdreg8(dev, 0);
231
232 ks_inblk(dev, buf, w + 2 + 2);
233
234 /* 3. read pkt data */
235 ks_inblk(dev, buf, ALIGN(len, 4));
236
237 /* 4. reset sudo DMA Mode */
Marek Vasut6a457312020-03-25 17:02:51 +0100238 ks_wrreg16(dev, KS_RXQCR, RXQCR_CMD_CNTL);
Roberto Cerati45a16932013-04-24 10:46:17 +0800239}
240
241static void ks_rcv(struct eth_device *dev, uchar **pv_data)
242{
243 struct type_frame_head *frame_hdr = ks->frame_head_info;
Marek Vasutb0435972020-03-25 17:18:55 +0100244 unsigned int frame_cnt;
Roberto Cerati45a16932013-04-24 10:46:17 +0800245 int i;
246
Marek Vasutb0435972020-03-25 17:18:55 +0100247 frame_cnt = ks_rdreg16(dev, KS_RXFCTR) >> 8;
Roberto Cerati45a16932013-04-24 10:46:17 +0800248
249 /* read all header information */
Marek Vasutb0435972020-03-25 17:18:55 +0100250 for (i = 0; i < frame_cnt; i++) {
Roberto Cerati45a16932013-04-24 10:46:17 +0800251 /* Checking Received packet status */
252 frame_hdr->sts = ks_rdreg16(dev, KS_RXFHSR);
253 /* Get packet len from hardware */
254 frame_hdr->len = ks_rdreg16(dev, KS_RXFHBCR);
255 frame_hdr++;
256 }
257
258 frame_hdr = ks->frame_head_info;
Marek Vasutb0435972020-03-25 17:18:55 +0100259 while (frame_cnt--) {
Roberto Cerati45a16932013-04-24 10:46:17 +0800260 if ((frame_hdr->sts & RXFSHR_RXFV) &&
261 (frame_hdr->len < RX_BUF_SIZE) &&
262 frame_hdr->len) {
263 /* read data block including CRC 4 bytes */
264 ks_read_qmu(dev, (u16 *)(*pv_data), frame_hdr->len);
265
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500266 /* net_rx_packets buffer size is ok (*pv_data) */
267 net_process_received_packet(*pv_data, frame_hdr->len);
Roberto Cerati45a16932013-04-24 10:46:17 +0800268 pv_data++;
269 } else {
Marek Vasut8b41a162020-03-25 17:02:21 +0100270 ks_wrreg16(dev, KS_RXQCR, RXQCR_CMD_CNTL | RXQCR_RRXEF);
Roberto Cerati45a16932013-04-24 10:46:17 +0800271 printf(DRIVERNAME ": bad packet\n");
272 }
273 frame_hdr++;
274 }
275}
276
277/*
278 * ks_read_selftest - read the selftest memory info.
279 * @ks: The device state
280 *
281 * Read and check the TX/RX memory selftest information.
282 */
283static int ks_read_selftest(struct eth_device *dev)
284{
285 u16 both_done = MBIR_TXMBF | MBIR_RXMBF;
286 u16 mbir;
287 int ret = 0;
288
289 mbir = ks_rdreg16(dev, KS_MBIR);
290
291 if ((mbir & both_done) != both_done) {
292 printf(DRIVERNAME ": Memory selftest not finished\n");
293 return 0;
294 }
295
296 if (mbir & MBIR_TXMBFA) {
297 printf(DRIVERNAME ": TX memory selftest fails\n");
298 ret |= 1;
299 }
300
301 if (mbir & MBIR_RXMBFA) {
302 printf(DRIVERNAME ": RX memory selftest fails\n");
303 ret |= 2;
304 }
305
306 debug(DRIVERNAME ": the selftest passes\n");
307
308 return ret;
309}
310
311static void ks_setup(struct eth_device *dev)
312{
313 u16 w;
314
315 /* Setup Transmit Frame Data Pointer Auto-Increment (TXFDPR) */
316 ks_wrreg16(dev, KS_TXFDPR, TXFDPR_TXFPAI);
317
318 /* Setup Receive Frame Data Pointer Auto-Increment */
319 ks_wrreg16(dev, KS_RXFDPR, RXFDPR_RXFPAI);
320
321 /* Setup Receive Frame Threshold - 1 frame (RXFCTFC) */
322 ks_wrreg16(dev, KS_RXFCTR, 1 & RXFCTR_THRESHOLD_MASK);
323
324 /* Setup RxQ Command Control (RXQCR) */
Marek Vasut8b41a162020-03-25 17:02:21 +0100325 ks_wrreg16(dev, KS_RXQCR, RXQCR_CMD_CNTL);
Roberto Cerati45a16932013-04-24 10:46:17 +0800326
327 /*
328 * set the force mode to half duplex, default is full duplex
329 * because if the auto-negotiation fails, most switch uses
330 * half-duplex.
331 */
332 w = ks_rdreg16(dev, KS_P1MBCR);
333 w &= ~P1MBCR_FORCE_FDX;
334 ks_wrreg16(dev, KS_P1MBCR, w);
335
336 w = TXCR_TXFCE | TXCR_TXPE | TXCR_TXCRC | TXCR_TCGIP;
337 ks_wrreg16(dev, KS_TXCR, w);
338
339 w = RXCR1_RXFCE | RXCR1_RXBE | RXCR1_RXUE | RXCR1_RXME | RXCR1_RXIPFCC;
340
341 /* Normal mode */
342 w |= RXCR1_RXPAFMA;
343
344 ks_wrreg16(dev, KS_RXCR1, w);
345}
346
347static void ks_setup_int(struct eth_device *dev)
348{
Roberto Cerati45a16932013-04-24 10:46:17 +0800349 /* Clear the interrupts status of the hardware. */
350 ks_wrreg16(dev, KS_ISR, 0xffff);
Roberto Cerati45a16932013-04-24 10:46:17 +0800351}
352
353static int ks8851_mll_detect_chip(struct eth_device *dev)
354{
355 unsigned short val, i;
356
357 ks_read_config(dev);
358
359 val = ks_rdreg16(dev, KS_CIDER);
360
361 if (val == 0xffff) {
362 /* Special case -- no chip present */
363 printf(DRIVERNAME ": is chip mounted ?\n");
364 return -1;
365 } else if ((val & 0xfff0) != CIDER_ID) {
366 printf(DRIVERNAME ": Invalid chip id 0x%04x\n", val);
367 return -1;
368 }
369
370 debug("Read back KS8851 id 0x%x\n", val);
371
372 /* only one entry in the table */
373 val &= 0xfff0;
374 for (i = 0; chip_ids[i].id != 0; i++) {
375 if (chip_ids[i].id == val)
376 break;
377 }
378 if (!chip_ids[i].id) {
379 printf(DRIVERNAME ": Unknown chip ID %04x\n", val);
380 return -1;
381 }
382
383 dev->priv = (void *)&chip_ids[i];
384
385 return 0;
386}
387
388static void ks8851_mll_reset(struct eth_device *dev)
389{
390 /* wake up powermode to normal mode */
391 ks_set_powermode(dev, PMECR_PM_NORMAL);
392 mdelay(1); /* wait for normal mode to take effect */
393
394 /* Disable interrupt and reset */
395 ks_soft_reset(dev, GRR_GSR);
396
397 /* turn off the IRQs and ack any outstanding */
398 ks_wrreg16(dev, KS_IER, 0x0000);
399 ks_wrreg16(dev, KS_ISR, 0xffff);
400
401 /* shutdown RX/TX QMU */
402 ks_disable_qmu(dev);
403}
404
405static void ks8851_mll_phy_configure(struct eth_device *dev)
406{
407 u16 data;
408
409 ks_setup(dev);
410 ks_setup_int(dev);
411
412 /* Probing the phy */
413 data = ks_rdreg16(dev, KS_OBCR);
414 ks_wrreg16(dev, KS_OBCR, data | OBCR_ODS_16MA);
415
416 debug(DRIVERNAME ": phy initialized\n");
417}
418
419static void ks8851_mll_enable(struct eth_device *dev)
420{
421 ks_wrreg16(dev, KS_ISR, 0xffff);
422 ks_enable_int(dev);
423 ks_enable_qmu(dev);
424}
425
426static int ks8851_mll_init(struct eth_device *dev, bd_t *bd)
427{
428 struct chip_id *id = dev->priv;
429
430 debug(DRIVERNAME ": detected %s controller\n", id->name);
431
432 if (ks_read_selftest(dev)) {
433 printf(DRIVERNAME ": Selftest failed\n");
434 return -1;
435 }
436
437 ks8851_mll_reset(dev);
438
439 /* Configure the PHY, initialize the link state */
440 ks8851_mll_phy_configure(dev);
441
442 /* static allocation of private informations */
443 ks->frame_head_info = fr_h_i;
444
445 /* Turn on Tx + Rx */
446 ks8851_mll_enable(dev);
447
448 return 0;
449}
450
451static void ks_write_qmu(struct eth_device *dev, u8 *pdata, u16 len)
452{
Marek Vasutb0435972020-03-25 17:18:55 +0100453 __le16 txw[2];
Roberto Cerati45a16932013-04-24 10:46:17 +0800454 /* start header at txb[0] to align txw entries */
Marek Vasutb0435972020-03-25 17:18:55 +0100455 txw[0] = 0;
456 txw[1] = cpu_to_le16(len);
Roberto Cerati45a16932013-04-24 10:46:17 +0800457
458 /* 1. set sudo-DMA mode */
459 ks_wrreg16(dev, KS_TXFDPR, TXFDPR_TXFPAI);
Marek Vasut6a457312020-03-25 17:02:51 +0100460 ks_wrreg16(dev, KS_RXQCR, RXQCR_CMD_CNTL | RXQCR_SDA);
Roberto Cerati45a16932013-04-24 10:46:17 +0800461 /* 2. write status/lenth info */
Marek Vasutb0435972020-03-25 17:18:55 +0100462 ks_outblk(dev, txw, 4);
Roberto Cerati45a16932013-04-24 10:46:17 +0800463 /* 3. write pkt data */
464 ks_outblk(dev, (u16 *)pdata, ALIGN(len, 4));
465 /* 4. reset sudo-DMA mode */
Marek Vasut6a457312020-03-25 17:02:51 +0100466 ks_wrreg16(dev, KS_RXQCR, RXQCR_CMD_CNTL);
Roberto Cerati45a16932013-04-24 10:46:17 +0800467 /* 5. Enqueue Tx(move the pkt from TX buffer into TXQ) */
468 ks_wrreg16(dev, KS_TXQCR, TXQCR_METFE);
469 /* 6. wait until TXQCR_METFE is auto-cleared */
470 do { } while (ks_rdreg16(dev, KS_TXQCR) & TXQCR_METFE);
471}
472
473static int ks8851_mll_send(struct eth_device *dev, void *packet, int length)
474{
475 u8 *data = (u8 *)packet;
476 u16 tmplen = (u16)length;
477 u16 retv;
478
479 /*
480 * Extra space are required:
481 * 4 byte for alignment, 4 for status/length, 4 for CRC
482 */
483 retv = ks_rdreg16(dev, KS_TXMIR) & 0x1fff;
484 if (retv >= tmplen + 12) {
485 ks_write_qmu(dev, data, tmplen);
486 return 0;
487 } else {
488 printf(DRIVERNAME ": failed to send packet: No buffer\n");
489 return -1;
490 }
491}
492
493static void ks8851_mll_halt(struct eth_device *dev)
494{
495 ks8851_mll_reset(dev);
496}
497
498/*
499 * Maximum receive ring size; that is, the number of packets
500 * we can buffer before overflow happens. Basically, this just
501 * needs to be enough to prevent a packet being discarded while
502 * we are processing the previous one.
503 */
504static int ks8851_mll_recv(struct eth_device *dev)
505{
506 u16 status;
507
508 status = ks_rdreg16(dev, KS_ISR);
509
510 ks_wrreg16(dev, KS_ISR, status);
511
512 if ((status & IRQ_RXI))
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500513 ks_rcv(dev, (uchar **)net_rx_packets);
Roberto Cerati45a16932013-04-24 10:46:17 +0800514
515 if ((status & IRQ_LDI)) {
516 u16 pmecr = ks_rdreg16(dev, KS_PMECR);
517 pmecr &= ~PMECR_WKEVT_MASK;
518 ks_wrreg16(dev, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
519 }
520
521 return 0;
522}
523
524static int ks8851_mll_write_hwaddr(struct eth_device *dev)
525{
526 u16 addrl, addrm, addrh;
527
528 addrh = (dev->enetaddr[0] << 8) | dev->enetaddr[1];
529 addrm = (dev->enetaddr[2] << 8) | dev->enetaddr[3];
530 addrl = (dev->enetaddr[4] << 8) | dev->enetaddr[5];
531
532 ks_wrreg16(dev, KS_MARH, addrh);
533 ks_wrreg16(dev, KS_MARM, addrm);
534 ks_wrreg16(dev, KS_MARL, addrl);
535
536 return 0;
537}
538
539int ks8851_mll_initialize(u8 dev_num, int base_addr)
540{
541 struct eth_device *dev;
542
Marek Vasute3b54cd2020-03-25 16:52:38 +0100543 dev = calloc(1, sizeof(*dev));
544 if (!dev)
545 return -ENOMEM;
Roberto Cerati45a16932013-04-24 10:46:17 +0800546
547 dev->iobase = base_addr;
548
549 ks = &ks_str;
550
551 /* Try to detect chip. Will fail if not present. */
552 if (ks8851_mll_detect_chip(dev)) {
553 free(dev);
554 return -1;
555 }
556
557 dev->init = ks8851_mll_init;
558 dev->halt = ks8851_mll_halt;
559 dev->send = ks8851_mll_send;
560 dev->recv = ks8851_mll_recv;
561 dev->write_hwaddr = ks8851_mll_write_hwaddr;
562 sprintf(dev->name, "%s-%hu", DRIVERNAME, dev_num);
563
564 eth_register(dev);
565
566 return 0;
567}