blob: 060c4bd87153dad377fab54d0f231aea95d148cd [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
Roberto Cerati45a16932013-04-24 10:46:17 +080020#define RX_BUF_SIZE 2000
21
Roberto Cerati45a16932013-04-24 10:46:17 +080022/*
Roberto Cerati45a16932013-04-24 10:46:17 +080023 * struct ks_net - KS8851 driver private data
Roberto Cerati45a16932013-04-24 10:46:17 +080024 * @bus_width : i/o bus width.
Roberto Cerati45a16932013-04-24 10:46:17 +080025 * @sharedbus : Multipex(addr and data bus) mode indicator.
Marek Vasut63f22f52020-03-25 17:23:11 +010026 * @extra_byte : number of extra byte prepended rx pkt.
Roberto Cerati45a16932013-04-24 10:46:17 +080027 */
Roberto Cerati45a16932013-04-24 10:46:17 +080028struct ks_net {
Roberto Cerati45a16932013-04-24 10:46:17 +080029 int bus_width;
Roberto Cerati45a16932013-04-24 10:46:17 +080030 u16 sharedbus;
Roberto Cerati45a16932013-04-24 10:46:17 +080031 u8 extra_byte;
Roberto Cerati45a16932013-04-24 10:46:17 +080032} ks_str, *ks;
33
34#define BE3 0x8000 /* Byte Enable 3 */
35#define BE2 0x4000 /* Byte Enable 2 */
36#define BE1 0x2000 /* Byte Enable 1 */
37#define BE0 0x1000 /* Byte Enable 0 */
38
39static u8 ks_rdreg8(struct eth_device *dev, u16 offset)
40{
41 u8 shift_bit = offset & 0x03;
42 u8 shift_data = (offset & 1) << 3;
43
44 writew(offset | (BE0 << shift_bit), dev->iobase + 2);
45
46 return (u8)(readw(dev->iobase) >> shift_data);
47}
48
49static u16 ks_rdreg16(struct eth_device *dev, u16 offset)
50{
51 writew(offset | ((BE1 | BE0) << (offset & 0x02)), dev->iobase + 2);
52
53 return readw(dev->iobase);
54}
55
Roberto Cerati45a16932013-04-24 10:46:17 +080056static void ks_wrreg16(struct eth_device *dev, u16 offset, u16 val)
57{
58 writew(offset | ((BE1 | BE0) << (offset & 0x02)), dev->iobase + 2);
59 writew(val, dev->iobase);
60}
61
62/*
63 * ks_inblk - read a block of data from QMU. This is called after sudo DMA mode
64 * enabled.
65 * @ks: The chip state
66 * @wptr: buffer address to save data
67 * @len: length in byte to read
68 */
69static inline void ks_inblk(struct eth_device *dev, u16 *wptr, u32 len)
70{
71 len >>= 1;
72
73 while (len--)
74 *wptr++ = readw(dev->iobase);
75}
76
77/*
78 * ks_outblk - write data to QMU. This is called after sudo DMA mode enabled.
79 * @ks: The chip information
80 * @wptr: buffer address
81 * @len: length in byte to write
82 */
83static inline void ks_outblk(struct eth_device *dev, u16 *wptr, u32 len)
84{
85 len >>= 1;
86
87 while (len--)
88 writew(*wptr++, dev->iobase);
89}
90
91static void ks_enable_int(struct eth_device *dev)
92{
Marek Vasutb0435972020-03-25 17:18:55 +010093 ks_wrreg16(dev, KS_IER, IRQ_LCI | IRQ_TXI | IRQ_RXI);
Roberto Cerati45a16932013-04-24 10:46:17 +080094}
95
Marek Vasut8ec27b02020-03-25 17:25:29 +010096static void ks_set_powermode(struct eth_device *dev, unsigned int pwrmode)
Roberto Cerati45a16932013-04-24 10:46:17 +080097{
Marek Vasut8ec27b02020-03-25 17:25:29 +010098 unsigned int pmecr;
Roberto Cerati45a16932013-04-24 10:46:17 +080099
100 ks_rdreg16(dev, KS_GRR);
101 pmecr = ks_rdreg16(dev, KS_PMECR);
102 pmecr &= ~PMECR_PM_MASK;
103 pmecr |= pwrmode;
104
105 ks_wrreg16(dev, KS_PMECR, pmecr);
106}
107
108/*
109 * ks_read_config - read chip configuration of bus width.
110 * @ks: The chip information
111 */
112static void ks_read_config(struct eth_device *dev)
113{
114 u16 reg_data = 0;
115
116 /* Regardless of bus width, 8 bit read should always work. */
117 reg_data = ks_rdreg8(dev, KS_CCR) & 0x00FF;
118 reg_data |= ks_rdreg8(dev, KS_CCR + 1) << 8;
119
120 /* addr/data bus are multiplexed */
121 ks->sharedbus = (reg_data & CCR_SHARED) == CCR_SHARED;
122
123 /*
124 * There are garbage data when reading data from QMU,
125 * depending on bus-width.
126 */
127 if (reg_data & CCR_8BIT) {
128 ks->bus_width = ENUM_BUS_8BIT;
129 ks->extra_byte = 1;
130 } else if (reg_data & CCR_16BIT) {
131 ks->bus_width = ENUM_BUS_16BIT;
132 ks->extra_byte = 2;
133 } else {
134 ks->bus_width = ENUM_BUS_32BIT;
135 ks->extra_byte = 4;
136 }
137}
138
139/*
140 * ks_soft_reset - issue one of the soft reset to the device
141 * @ks: The device state.
142 * @op: The bit(s) to set in the GRR
143 *
144 * Issue the relevant soft-reset command to the device's GRR register
145 * specified by @op.
146 *
147 * Note, the delays are in there as a caution to ensure that the reset
148 * has time to take effect and then complete. Since the datasheet does
149 * not currently specify the exact sequence, we have chosen something
150 * that seems to work with our device.
151 */
Marek Vasut8ec27b02020-03-25 17:25:29 +0100152static void ks_soft_reset(struct eth_device *dev, unsigned int op)
Roberto Cerati45a16932013-04-24 10:46:17 +0800153{
154 /* Disable interrupt first */
155 ks_wrreg16(dev, KS_IER, 0x0000);
156 ks_wrreg16(dev, KS_GRR, op);
157 mdelay(10); /* wait a short time to effect reset */
158 ks_wrreg16(dev, KS_GRR, 0);
159 mdelay(1); /* wait for condition to clear */
160}
161
162void ks_enable_qmu(struct eth_device *dev)
163{
164 u16 w;
165
166 w = ks_rdreg16(dev, KS_TXCR);
167
168 /* Enables QMU Transmit (TXCR). */
169 ks_wrreg16(dev, KS_TXCR, w | TXCR_TXE);
170
171 /* Enable RX Frame Count Threshold and Auto-Dequeue RXQ Frame */
172 w = ks_rdreg16(dev, KS_RXQCR);
173 ks_wrreg16(dev, KS_RXQCR, w | RXQCR_RXFCTE);
174
175 /* Enables QMU Receive (RXCR1). */
176 w = ks_rdreg16(dev, KS_RXCR1);
177 ks_wrreg16(dev, KS_RXCR1, w | RXCR1_RXE);
178}
179
180static void ks_disable_qmu(struct eth_device *dev)
181{
182 u16 w;
183
184 w = ks_rdreg16(dev, KS_TXCR);
185
186 /* Disables QMU Transmit (TXCR). */
187 w &= ~TXCR_TXE;
188 ks_wrreg16(dev, KS_TXCR, w);
189
190 /* Disables QMU Receive (RXCR1). */
191 w = ks_rdreg16(dev, KS_RXCR1);
192 w &= ~RXCR1_RXE;
193 ks_wrreg16(dev, KS_RXCR1, w);
194}
195
196static inline void ks_read_qmu(struct eth_device *dev, u16 *buf, u32 len)
197{
198 u32 r = ks->extra_byte & 0x1;
199 u32 w = ks->extra_byte - r;
200
201 /* 1. set sudo DMA mode */
202 ks_wrreg16(dev, KS_RXFDPR, RXFDPR_RXFPAI);
Marek Vasut6a457312020-03-25 17:02:51 +0100203 ks_wrreg16(dev, KS_RXQCR, RXQCR_CMD_CNTL | RXQCR_SDA);
Roberto Cerati45a16932013-04-24 10:46:17 +0800204
205 /*
206 * 2. read prepend data
207 *
208 * read 4 + extra bytes and discard them.
209 * extra bytes for dummy, 2 for status, 2 for len
210 */
211
212 if (r)
213 ks_rdreg8(dev, 0);
214
215 ks_inblk(dev, buf, w + 2 + 2);
216
217 /* 3. read pkt data */
218 ks_inblk(dev, buf, ALIGN(len, 4));
219
220 /* 4. reset sudo DMA Mode */
Marek Vasut6a457312020-03-25 17:02:51 +0100221 ks_wrreg16(dev, KS_RXQCR, RXQCR_CMD_CNTL);
Roberto Cerati45a16932013-04-24 10:46:17 +0800222}
223
224static void ks_rcv(struct eth_device *dev, uchar **pv_data)
225{
Marek Vasutb0435972020-03-25 17:18:55 +0100226 unsigned int frame_cnt;
Marek Vasut63f22f52020-03-25 17:23:11 +0100227 u16 sts, len;
Roberto Cerati45a16932013-04-24 10:46:17 +0800228 int i;
229
Marek Vasutb0435972020-03-25 17:18:55 +0100230 frame_cnt = ks_rdreg16(dev, KS_RXFCTR) >> 8;
Roberto Cerati45a16932013-04-24 10:46:17 +0800231
232 /* read all header information */
Marek Vasutb0435972020-03-25 17:18:55 +0100233 for (i = 0; i < frame_cnt; i++) {
Roberto Cerati45a16932013-04-24 10:46:17 +0800234 /* Checking Received packet status */
Marek Vasut63f22f52020-03-25 17:23:11 +0100235 sts = ks_rdreg16(dev, KS_RXFHSR);
Roberto Cerati45a16932013-04-24 10:46:17 +0800236 /* Get packet len from hardware */
Marek Vasut63f22f52020-03-25 17:23:11 +0100237 len = ks_rdreg16(dev, KS_RXFHBCR);
Roberto Cerati45a16932013-04-24 10:46:17 +0800238
Marek Vasut63f22f52020-03-25 17:23:11 +0100239 if ((sts & RXFSHR_RXFV) && len && (len < RX_BUF_SIZE)) {
Roberto Cerati45a16932013-04-24 10:46:17 +0800240 /* read data block including CRC 4 bytes */
Marek Vasut63f22f52020-03-25 17:23:11 +0100241 ks_read_qmu(dev, (u16 *)(*pv_data), len);
Roberto Cerati45a16932013-04-24 10:46:17 +0800242
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500243 /* net_rx_packets buffer size is ok (*pv_data) */
Marek Vasut63f22f52020-03-25 17:23:11 +0100244 net_process_received_packet(*pv_data, len);
Roberto Cerati45a16932013-04-24 10:46:17 +0800245 pv_data++;
246 } else {
Marek Vasut8b41a162020-03-25 17:02:21 +0100247 ks_wrreg16(dev, KS_RXQCR, RXQCR_CMD_CNTL | RXQCR_RRXEF);
Roberto Cerati45a16932013-04-24 10:46:17 +0800248 printf(DRIVERNAME ": bad packet\n");
249 }
Roberto Cerati45a16932013-04-24 10:46:17 +0800250 }
251}
252
253/*
254 * ks_read_selftest - read the selftest memory info.
255 * @ks: The device state
256 *
257 * Read and check the TX/RX memory selftest information.
258 */
259static int ks_read_selftest(struct eth_device *dev)
260{
261 u16 both_done = MBIR_TXMBF | MBIR_RXMBF;
262 u16 mbir;
263 int ret = 0;
264
265 mbir = ks_rdreg16(dev, KS_MBIR);
266
267 if ((mbir & both_done) != both_done) {
268 printf(DRIVERNAME ": Memory selftest not finished\n");
269 return 0;
270 }
271
272 if (mbir & MBIR_TXMBFA) {
273 printf(DRIVERNAME ": TX memory selftest fails\n");
274 ret |= 1;
275 }
276
277 if (mbir & MBIR_RXMBFA) {
278 printf(DRIVERNAME ": RX memory selftest fails\n");
279 ret |= 2;
280 }
281
282 debug(DRIVERNAME ": the selftest passes\n");
283
284 return ret;
285}
286
287static void ks_setup(struct eth_device *dev)
288{
289 u16 w;
290
291 /* Setup Transmit Frame Data Pointer Auto-Increment (TXFDPR) */
292 ks_wrreg16(dev, KS_TXFDPR, TXFDPR_TXFPAI);
293
294 /* Setup Receive Frame Data Pointer Auto-Increment */
295 ks_wrreg16(dev, KS_RXFDPR, RXFDPR_RXFPAI);
296
297 /* Setup Receive Frame Threshold - 1 frame (RXFCTFC) */
298 ks_wrreg16(dev, KS_RXFCTR, 1 & RXFCTR_THRESHOLD_MASK);
299
300 /* Setup RxQ Command Control (RXQCR) */
Marek Vasut8b41a162020-03-25 17:02:21 +0100301 ks_wrreg16(dev, KS_RXQCR, RXQCR_CMD_CNTL);
Roberto Cerati45a16932013-04-24 10:46:17 +0800302
303 /*
304 * set the force mode to half duplex, default is full duplex
305 * because if the auto-negotiation fails, most switch uses
306 * half-duplex.
307 */
308 w = ks_rdreg16(dev, KS_P1MBCR);
309 w &= ~P1MBCR_FORCE_FDX;
310 ks_wrreg16(dev, KS_P1MBCR, w);
311
312 w = TXCR_TXFCE | TXCR_TXPE | TXCR_TXCRC | TXCR_TCGIP;
313 ks_wrreg16(dev, KS_TXCR, w);
314
315 w = RXCR1_RXFCE | RXCR1_RXBE | RXCR1_RXUE | RXCR1_RXME | RXCR1_RXIPFCC;
316
317 /* Normal mode */
318 w |= RXCR1_RXPAFMA;
319
320 ks_wrreg16(dev, KS_RXCR1, w);
321}
322
323static void ks_setup_int(struct eth_device *dev)
324{
Roberto Cerati45a16932013-04-24 10:46:17 +0800325 /* Clear the interrupts status of the hardware. */
326 ks_wrreg16(dev, KS_ISR, 0xffff);
Roberto Cerati45a16932013-04-24 10:46:17 +0800327}
328
329static int ks8851_mll_detect_chip(struct eth_device *dev)
330{
Marek Vasuteb69d8b2020-03-25 18:15:46 +0100331 unsigned short val;
Roberto Cerati45a16932013-04-24 10:46:17 +0800332
333 ks_read_config(dev);
334
335 val = ks_rdreg16(dev, KS_CIDER);
336
337 if (val == 0xffff) {
338 /* Special case -- no chip present */
339 printf(DRIVERNAME ": is chip mounted ?\n");
340 return -1;
341 } else if ((val & 0xfff0) != CIDER_ID) {
342 printf(DRIVERNAME ": Invalid chip id 0x%04x\n", val);
343 return -1;
344 }
345
346 debug("Read back KS8851 id 0x%x\n", val);
347
Marek Vasuteb69d8b2020-03-25 18:15:46 +0100348 if ((val & 0xfff0) != CIDER_ID) {
Roberto Cerati45a16932013-04-24 10:46:17 +0800349 printf(DRIVERNAME ": Unknown chip ID %04x\n", val);
350 return -1;
351 }
352
Roberto Cerati45a16932013-04-24 10:46:17 +0800353 return 0;
354}
355
356static void ks8851_mll_reset(struct eth_device *dev)
357{
358 /* wake up powermode to normal mode */
359 ks_set_powermode(dev, PMECR_PM_NORMAL);
360 mdelay(1); /* wait for normal mode to take effect */
361
362 /* Disable interrupt and reset */
363 ks_soft_reset(dev, GRR_GSR);
364
365 /* turn off the IRQs and ack any outstanding */
366 ks_wrreg16(dev, KS_IER, 0x0000);
367 ks_wrreg16(dev, KS_ISR, 0xffff);
368
369 /* shutdown RX/TX QMU */
370 ks_disable_qmu(dev);
371}
372
373static void ks8851_mll_phy_configure(struct eth_device *dev)
374{
375 u16 data;
376
377 ks_setup(dev);
378 ks_setup_int(dev);
379
380 /* Probing the phy */
381 data = ks_rdreg16(dev, KS_OBCR);
382 ks_wrreg16(dev, KS_OBCR, data | OBCR_ODS_16MA);
383
384 debug(DRIVERNAME ": phy initialized\n");
385}
386
387static void ks8851_mll_enable(struct eth_device *dev)
388{
389 ks_wrreg16(dev, KS_ISR, 0xffff);
390 ks_enable_int(dev);
391 ks_enable_qmu(dev);
392}
393
394static int ks8851_mll_init(struct eth_device *dev, bd_t *bd)
395{
Roberto Cerati45a16932013-04-24 10:46:17 +0800396 if (ks_read_selftest(dev)) {
397 printf(DRIVERNAME ": Selftest failed\n");
398 return -1;
399 }
400
401 ks8851_mll_reset(dev);
402
403 /* Configure the PHY, initialize the link state */
404 ks8851_mll_phy_configure(dev);
405
Roberto Cerati45a16932013-04-24 10:46:17 +0800406 /* Turn on Tx + Rx */
407 ks8851_mll_enable(dev);
408
409 return 0;
410}
411
412static void ks_write_qmu(struct eth_device *dev, u8 *pdata, u16 len)
413{
Marek Vasutb0435972020-03-25 17:18:55 +0100414 __le16 txw[2];
Roberto Cerati45a16932013-04-24 10:46:17 +0800415 /* start header at txb[0] to align txw entries */
Marek Vasutb0435972020-03-25 17:18:55 +0100416 txw[0] = 0;
417 txw[1] = cpu_to_le16(len);
Roberto Cerati45a16932013-04-24 10:46:17 +0800418
419 /* 1. set sudo-DMA mode */
420 ks_wrreg16(dev, KS_TXFDPR, TXFDPR_TXFPAI);
Marek Vasut6a457312020-03-25 17:02:51 +0100421 ks_wrreg16(dev, KS_RXQCR, RXQCR_CMD_CNTL | RXQCR_SDA);
Marek Vasut8ec27b02020-03-25 17:25:29 +0100422 /* 2. write status/length info */
Marek Vasutb0435972020-03-25 17:18:55 +0100423 ks_outblk(dev, txw, 4);
Roberto Cerati45a16932013-04-24 10:46:17 +0800424 /* 3. write pkt data */
425 ks_outblk(dev, (u16 *)pdata, ALIGN(len, 4));
426 /* 4. reset sudo-DMA mode */
Marek Vasut6a457312020-03-25 17:02:51 +0100427 ks_wrreg16(dev, KS_RXQCR, RXQCR_CMD_CNTL);
Roberto Cerati45a16932013-04-24 10:46:17 +0800428 /* 5. Enqueue Tx(move the pkt from TX buffer into TXQ) */
429 ks_wrreg16(dev, KS_TXQCR, TXQCR_METFE);
430 /* 6. wait until TXQCR_METFE is auto-cleared */
431 do { } while (ks_rdreg16(dev, KS_TXQCR) & TXQCR_METFE);
432}
433
434static int ks8851_mll_send(struct eth_device *dev, void *packet, int length)
435{
436 u8 *data = (u8 *)packet;
437 u16 tmplen = (u16)length;
438 u16 retv;
439
440 /*
441 * Extra space are required:
442 * 4 byte for alignment, 4 for status/length, 4 for CRC
443 */
444 retv = ks_rdreg16(dev, KS_TXMIR) & 0x1fff;
445 if (retv >= tmplen + 12) {
446 ks_write_qmu(dev, data, tmplen);
447 return 0;
Roberto Cerati45a16932013-04-24 10:46:17 +0800448 }
Marek Vasut8ec27b02020-03-25 17:25:29 +0100449
450 printf(DRIVERNAME ": failed to send packet: No buffer\n");
451 return -1;
Roberto Cerati45a16932013-04-24 10:46:17 +0800452}
453
454static void ks8851_mll_halt(struct eth_device *dev)
455{
456 ks8851_mll_reset(dev);
457}
458
459/*
460 * Maximum receive ring size; that is, the number of packets
461 * we can buffer before overflow happens. Basically, this just
462 * needs to be enough to prevent a packet being discarded while
463 * we are processing the previous one.
464 */
465static int ks8851_mll_recv(struct eth_device *dev)
466{
467 u16 status;
468
469 status = ks_rdreg16(dev, KS_ISR);
470
471 ks_wrreg16(dev, KS_ISR, status);
472
Marek Vasut8ec27b02020-03-25 17:25:29 +0100473 if (status & IRQ_RXI)
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500474 ks_rcv(dev, (uchar **)net_rx_packets);
Roberto Cerati45a16932013-04-24 10:46:17 +0800475
Marek Vasut8ec27b02020-03-25 17:25:29 +0100476 if (status & IRQ_LDI) {
Roberto Cerati45a16932013-04-24 10:46:17 +0800477 u16 pmecr = ks_rdreg16(dev, KS_PMECR);
Marek Vasut8ec27b02020-03-25 17:25:29 +0100478
Roberto Cerati45a16932013-04-24 10:46:17 +0800479 pmecr &= ~PMECR_WKEVT_MASK;
480 ks_wrreg16(dev, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
481 }
482
483 return 0;
484}
485
486static int ks8851_mll_write_hwaddr(struct eth_device *dev)
487{
488 u16 addrl, addrm, addrh;
489
490 addrh = (dev->enetaddr[0] << 8) | dev->enetaddr[1];
491 addrm = (dev->enetaddr[2] << 8) | dev->enetaddr[3];
492 addrl = (dev->enetaddr[4] << 8) | dev->enetaddr[5];
493
494 ks_wrreg16(dev, KS_MARH, addrh);
495 ks_wrreg16(dev, KS_MARM, addrm);
496 ks_wrreg16(dev, KS_MARL, addrl);
497
498 return 0;
499}
500
501int ks8851_mll_initialize(u8 dev_num, int base_addr)
502{
503 struct eth_device *dev;
504
Marek Vasute3b54cd2020-03-25 16:52:38 +0100505 dev = calloc(1, sizeof(*dev));
506 if (!dev)
507 return -ENOMEM;
Roberto Cerati45a16932013-04-24 10:46:17 +0800508
509 dev->iobase = base_addr;
510
511 ks = &ks_str;
512
513 /* Try to detect chip. Will fail if not present. */
514 if (ks8851_mll_detect_chip(dev)) {
515 free(dev);
516 return -1;
517 }
518
519 dev->init = ks8851_mll_init;
520 dev->halt = ks8851_mll_halt;
521 dev->send = ks8851_mll_send;
522 dev->recv = ks8851_mll_recv;
523 dev->write_hwaddr = ks8851_mll_write_hwaddr;
524 sprintf(dev->name, "%s-%hu", DRIVERNAME, dev_num);
525
526 eth_register(dev);
527
528 return 0;
529}