blob: 2424e4297e47ea95f5a1b4b510f14abe326a2706 [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
Marek Vasutb7c6ae22020-03-25 17:35:00 +010024 * @dev : legacy non-DM ethernet device structure
25 * @iobase : register base
Roberto Cerati45a16932013-04-24 10:46:17 +080026 * @bus_width : i/o bus width.
Roberto Cerati45a16932013-04-24 10:46:17 +080027 * @sharedbus : Multipex(addr and data bus) mode indicator.
Marek Vasut63f22f52020-03-25 17:23:11 +010028 * @extra_byte : number of extra byte prepended rx pkt.
Roberto Cerati45a16932013-04-24 10:46:17 +080029 */
Roberto Cerati45a16932013-04-24 10:46:17 +080030struct ks_net {
Marek Vasutb7c6ae22020-03-25 17:35:00 +010031 struct eth_device dev;
32 phys_addr_t iobase;
Roberto Cerati45a16932013-04-24 10:46:17 +080033 int bus_width;
Roberto Cerati45a16932013-04-24 10:46:17 +080034 u16 sharedbus;
Marek Vasut9c9f3fc2020-03-25 18:47:10 +010035 u16 rxfc;
Roberto Cerati45a16932013-04-24 10:46:17 +080036 u8 extra_byte;
Marek Vasutb7c6ae22020-03-25 17:35:00 +010037};
Roberto Cerati45a16932013-04-24 10:46:17 +080038
39#define BE3 0x8000 /* Byte Enable 3 */
40#define BE2 0x4000 /* Byte Enable 2 */
41#define BE1 0x2000 /* Byte Enable 1 */
42#define BE0 0x1000 /* Byte Enable 0 */
43
Marek Vasutb7c6ae22020-03-25 17:35:00 +010044static u8 ks_rdreg8(struct ks_net *ks, u16 offset)
Roberto Cerati45a16932013-04-24 10:46:17 +080045{
46 u8 shift_bit = offset & 0x03;
47 u8 shift_data = (offset & 1) << 3;
48
Marek Vasutb7c6ae22020-03-25 17:35:00 +010049 writew(offset | (BE0 << shift_bit), ks->iobase + 2);
Roberto Cerati45a16932013-04-24 10:46:17 +080050
Marek Vasutb7c6ae22020-03-25 17:35:00 +010051 return (u8)(readw(ks->iobase) >> shift_data);
Roberto Cerati45a16932013-04-24 10:46:17 +080052}
53
Marek Vasutb7c6ae22020-03-25 17:35:00 +010054static u16 ks_rdreg16(struct ks_net *ks, u16 offset)
Roberto Cerati45a16932013-04-24 10:46:17 +080055{
Marek Vasutb7c6ae22020-03-25 17:35:00 +010056 writew(offset | ((BE1 | BE0) << (offset & 0x02)), ks->iobase + 2);
Roberto Cerati45a16932013-04-24 10:46:17 +080057
Marek Vasutb7c6ae22020-03-25 17:35:00 +010058 return readw(ks->iobase);
Roberto Cerati45a16932013-04-24 10:46:17 +080059}
60
Marek Vasutb7c6ae22020-03-25 17:35:00 +010061static void ks_wrreg16(struct ks_net *ks, u16 offset, u16 val)
Roberto Cerati45a16932013-04-24 10:46:17 +080062{
Marek Vasutb7c6ae22020-03-25 17:35:00 +010063 writew(offset | ((BE1 | BE0) << (offset & 0x02)), ks->iobase + 2);
64 writew(val, ks->iobase);
Roberto Cerati45a16932013-04-24 10:46:17 +080065}
66
67/*
68 * ks_inblk - read a block of data from QMU. This is called after sudo DMA mode
69 * enabled.
70 * @ks: The chip state
71 * @wptr: buffer address to save data
72 * @len: length in byte to read
73 */
Marek Vasutb7c6ae22020-03-25 17:35:00 +010074static inline void ks_inblk(struct ks_net *ks, u16 *wptr, u32 len)
Roberto Cerati45a16932013-04-24 10:46:17 +080075{
76 len >>= 1;
77
78 while (len--)
Marek Vasutb7c6ae22020-03-25 17:35:00 +010079 *wptr++ = readw(ks->iobase);
Roberto Cerati45a16932013-04-24 10:46:17 +080080}
81
82/*
83 * ks_outblk - write data to QMU. This is called after sudo DMA mode enabled.
84 * @ks: The chip information
85 * @wptr: buffer address
86 * @len: length in byte to write
87 */
Marek Vasutb7c6ae22020-03-25 17:35:00 +010088static inline void ks_outblk(struct ks_net *ks, u16 *wptr, u32 len)
Roberto Cerati45a16932013-04-24 10:46:17 +080089{
90 len >>= 1;
91
92 while (len--)
Marek Vasutb7c6ae22020-03-25 17:35:00 +010093 writew(*wptr++, ks->iobase);
Roberto Cerati45a16932013-04-24 10:46:17 +080094}
95
Marek Vasutb7c6ae22020-03-25 17:35:00 +010096static void ks_enable_int(struct ks_net *ks)
Roberto Cerati45a16932013-04-24 10:46:17 +080097{
Marek Vasutb7c6ae22020-03-25 17:35:00 +010098 ks_wrreg16(ks, KS_IER, IRQ_LCI | IRQ_TXI | IRQ_RXI);
Roberto Cerati45a16932013-04-24 10:46:17 +080099}
100
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100101static void ks_set_powermode(struct ks_net *ks, unsigned int pwrmode)
Roberto Cerati45a16932013-04-24 10:46:17 +0800102{
Marek Vasut8ec27b02020-03-25 17:25:29 +0100103 unsigned int pmecr;
Roberto Cerati45a16932013-04-24 10:46:17 +0800104
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100105 ks_rdreg16(ks, KS_GRR);
106 pmecr = ks_rdreg16(ks, KS_PMECR);
Roberto Cerati45a16932013-04-24 10:46:17 +0800107 pmecr &= ~PMECR_PM_MASK;
108 pmecr |= pwrmode;
109
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100110 ks_wrreg16(ks, KS_PMECR, pmecr);
Roberto Cerati45a16932013-04-24 10:46:17 +0800111}
112
113/*
114 * ks_read_config - read chip configuration of bus width.
115 * @ks: The chip information
116 */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100117static void ks_read_config(struct ks_net *ks)
Roberto Cerati45a16932013-04-24 10:46:17 +0800118{
119 u16 reg_data = 0;
120
121 /* Regardless of bus width, 8 bit read should always work. */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100122 reg_data = ks_rdreg8(ks, KS_CCR) & 0x00FF;
123 reg_data |= ks_rdreg8(ks, KS_CCR + 1) << 8;
Roberto Cerati45a16932013-04-24 10:46:17 +0800124
125 /* addr/data bus are multiplexed */
126 ks->sharedbus = (reg_data & CCR_SHARED) == CCR_SHARED;
127
128 /*
129 * There are garbage data when reading data from QMU,
130 * depending on bus-width.
131 */
132 if (reg_data & CCR_8BIT) {
133 ks->bus_width = ENUM_BUS_8BIT;
134 ks->extra_byte = 1;
135 } else if (reg_data & CCR_16BIT) {
136 ks->bus_width = ENUM_BUS_16BIT;
137 ks->extra_byte = 2;
138 } else {
139 ks->bus_width = ENUM_BUS_32BIT;
140 ks->extra_byte = 4;
141 }
142}
143
144/*
145 * ks_soft_reset - issue one of the soft reset to the device
146 * @ks: The device state.
147 * @op: The bit(s) to set in the GRR
148 *
149 * Issue the relevant soft-reset command to the device's GRR register
150 * specified by @op.
151 *
152 * Note, the delays are in there as a caution to ensure that the reset
153 * has time to take effect and then complete. Since the datasheet does
154 * not currently specify the exact sequence, we have chosen something
155 * that seems to work with our device.
156 */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100157static void ks_soft_reset(struct ks_net *ks, unsigned int op)
Roberto Cerati45a16932013-04-24 10:46:17 +0800158{
159 /* Disable interrupt first */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100160 ks_wrreg16(ks, KS_IER, 0x0000);
161 ks_wrreg16(ks, KS_GRR, op);
Roberto Cerati45a16932013-04-24 10:46:17 +0800162 mdelay(10); /* wait a short time to effect reset */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100163 ks_wrreg16(ks, KS_GRR, 0);
Roberto Cerati45a16932013-04-24 10:46:17 +0800164 mdelay(1); /* wait for condition to clear */
165}
166
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100167void ks_enable_qmu(struct ks_net *ks)
Roberto Cerati45a16932013-04-24 10:46:17 +0800168{
169 u16 w;
170
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100171 w = ks_rdreg16(ks, KS_TXCR);
Roberto Cerati45a16932013-04-24 10:46:17 +0800172
173 /* Enables QMU Transmit (TXCR). */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100174 ks_wrreg16(ks, KS_TXCR, w | TXCR_TXE);
Roberto Cerati45a16932013-04-24 10:46:17 +0800175
176 /* Enable RX Frame Count Threshold and Auto-Dequeue RXQ Frame */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100177 w = ks_rdreg16(ks, KS_RXQCR);
178 ks_wrreg16(ks, KS_RXQCR, w | RXQCR_RXFCTE);
Roberto Cerati45a16932013-04-24 10:46:17 +0800179
180 /* Enables QMU Receive (RXCR1). */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100181 w = ks_rdreg16(ks, KS_RXCR1);
182 ks_wrreg16(ks, KS_RXCR1, w | RXCR1_RXE);
Roberto Cerati45a16932013-04-24 10:46:17 +0800183}
184
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100185static void ks_disable_qmu(struct ks_net *ks)
Roberto Cerati45a16932013-04-24 10:46:17 +0800186{
187 u16 w;
188
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100189 w = ks_rdreg16(ks, KS_TXCR);
Roberto Cerati45a16932013-04-24 10:46:17 +0800190
191 /* Disables QMU Transmit (TXCR). */
192 w &= ~TXCR_TXE;
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100193 ks_wrreg16(ks, KS_TXCR, w);
Roberto Cerati45a16932013-04-24 10:46:17 +0800194
195 /* Disables QMU Receive (RXCR1). */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100196 w = ks_rdreg16(ks, KS_RXCR1);
Roberto Cerati45a16932013-04-24 10:46:17 +0800197 w &= ~RXCR1_RXE;
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100198 ks_wrreg16(ks, KS_RXCR1, w);
Roberto Cerati45a16932013-04-24 10:46:17 +0800199}
200
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100201static inline void ks_read_qmu(struct ks_net *ks, u16 *buf, u32 len)
Roberto Cerati45a16932013-04-24 10:46:17 +0800202{
203 u32 r = ks->extra_byte & 0x1;
204 u32 w = ks->extra_byte - r;
205
206 /* 1. set sudo DMA mode */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100207 ks_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI);
208 ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL | RXQCR_SDA);
Roberto Cerati45a16932013-04-24 10:46:17 +0800209
210 /*
211 * 2. read prepend data
212 *
213 * read 4 + extra bytes and discard them.
214 * extra bytes for dummy, 2 for status, 2 for len
215 */
216
217 if (r)
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100218 ks_rdreg8(ks, 0);
Roberto Cerati45a16932013-04-24 10:46:17 +0800219
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100220 ks_inblk(ks, buf, w + 2 + 2);
Roberto Cerati45a16932013-04-24 10:46:17 +0800221
222 /* 3. read pkt data */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100223 ks_inblk(ks, buf, ALIGN(len, 4));
Roberto Cerati45a16932013-04-24 10:46:17 +0800224
225 /* 4. reset sudo DMA Mode */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100226 ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL);
Roberto Cerati45a16932013-04-24 10:46:17 +0800227}
228
Marek Vasut9c9f3fc2020-03-25 18:47:10 +0100229static int ks_rcv(struct ks_net *ks, uchar *data)
Roberto Cerati45a16932013-04-24 10:46:17 +0800230{
Marek Vasut63f22f52020-03-25 17:23:11 +0100231 u16 sts, len;
Roberto Cerati45a16932013-04-24 10:46:17 +0800232
Marek Vasut9c9f3fc2020-03-25 18:47:10 +0100233 if (!ks->rxfc)
234 ks->rxfc = ks_rdreg16(ks, KS_RXFCTR) >> 8;
Roberto Cerati45a16932013-04-24 10:46:17 +0800235
Marek Vasut9c9f3fc2020-03-25 18:47:10 +0100236 if (!ks->rxfc)
237 return 0;
Roberto Cerati45a16932013-04-24 10:46:17 +0800238
Marek Vasut9c9f3fc2020-03-25 18:47:10 +0100239 /* Checking Received packet status */
240 sts = ks_rdreg16(ks, KS_RXFHSR);
241 /* Get packet len from hardware */
242 len = ks_rdreg16(ks, KS_RXFHBCR);
Roberto Cerati45a16932013-04-24 10:46:17 +0800243
Marek Vasut9c9f3fc2020-03-25 18:47:10 +0100244 if ((sts & RXFSHR_RXFV) && len && (len < RX_BUF_SIZE)) {
245 /* read data block including CRC 4 bytes */
246 ks_read_qmu(ks, (u16 *)data, len);
247 ks->rxfc--;
248 return len - 4;
Roberto Cerati45a16932013-04-24 10:46:17 +0800249 }
Marek Vasut9c9f3fc2020-03-25 18:47:10 +0100250
251 ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL | RXQCR_RRXEF);
252 printf(DRIVERNAME ": bad packet\n");
253 return 0;
Roberto Cerati45a16932013-04-24 10:46:17 +0800254}
255
256/*
257 * ks_read_selftest - read the selftest memory info.
258 * @ks: The device state
259 *
260 * Read and check the TX/RX memory selftest information.
261 */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100262static int ks_read_selftest(struct ks_net *ks)
Roberto Cerati45a16932013-04-24 10:46:17 +0800263{
264 u16 both_done = MBIR_TXMBF | MBIR_RXMBF;
265 u16 mbir;
266 int ret = 0;
267
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100268 mbir = ks_rdreg16(ks, KS_MBIR);
Roberto Cerati45a16932013-04-24 10:46:17 +0800269
270 if ((mbir & both_done) != both_done) {
271 printf(DRIVERNAME ": Memory selftest not finished\n");
272 return 0;
273 }
274
275 if (mbir & MBIR_TXMBFA) {
276 printf(DRIVERNAME ": TX memory selftest fails\n");
277 ret |= 1;
278 }
279
280 if (mbir & MBIR_RXMBFA) {
281 printf(DRIVERNAME ": RX memory selftest fails\n");
282 ret |= 2;
283 }
284
285 debug(DRIVERNAME ": the selftest passes\n");
286
287 return ret;
288}
289
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100290static void ks_setup(struct ks_net *ks)
Roberto Cerati45a16932013-04-24 10:46:17 +0800291{
292 u16 w;
293
294 /* Setup Transmit Frame Data Pointer Auto-Increment (TXFDPR) */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100295 ks_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
Roberto Cerati45a16932013-04-24 10:46:17 +0800296
297 /* Setup Receive Frame Data Pointer Auto-Increment */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100298 ks_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI);
Roberto Cerati45a16932013-04-24 10:46:17 +0800299
300 /* Setup Receive Frame Threshold - 1 frame (RXFCTFC) */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100301 ks_wrreg16(ks, KS_RXFCTR, 1 & RXFCTR_THRESHOLD_MASK);
Roberto Cerati45a16932013-04-24 10:46:17 +0800302
303 /* Setup RxQ Command Control (RXQCR) */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100304 ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL);
Roberto Cerati45a16932013-04-24 10:46:17 +0800305
306 /*
307 * set the force mode to half duplex, default is full duplex
308 * because if the auto-negotiation fails, most switch uses
309 * half-duplex.
310 */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100311 w = ks_rdreg16(ks, KS_P1MBCR);
Roberto Cerati45a16932013-04-24 10:46:17 +0800312 w &= ~P1MBCR_FORCE_FDX;
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100313 ks_wrreg16(ks, KS_P1MBCR, w);
Roberto Cerati45a16932013-04-24 10:46:17 +0800314
315 w = TXCR_TXFCE | TXCR_TXPE | TXCR_TXCRC | TXCR_TCGIP;
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100316 ks_wrreg16(ks, KS_TXCR, w);
Roberto Cerati45a16932013-04-24 10:46:17 +0800317
318 w = RXCR1_RXFCE | RXCR1_RXBE | RXCR1_RXUE | RXCR1_RXME | RXCR1_RXIPFCC;
319
320 /* Normal mode */
321 w |= RXCR1_RXPAFMA;
322
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100323 ks_wrreg16(ks, KS_RXCR1, w);
Roberto Cerati45a16932013-04-24 10:46:17 +0800324}
325
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100326static void ks_setup_int(struct ks_net *ks)
Roberto Cerati45a16932013-04-24 10:46:17 +0800327{
Roberto Cerati45a16932013-04-24 10:46:17 +0800328 /* Clear the interrupts status of the hardware. */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100329 ks_wrreg16(ks, KS_ISR, 0xffff);
Roberto Cerati45a16932013-04-24 10:46:17 +0800330}
331
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100332static int ks8851_mll_detect_chip(struct ks_net *ks)
Roberto Cerati45a16932013-04-24 10:46:17 +0800333{
Marek Vasuteb69d8b2020-03-25 18:15:46 +0100334 unsigned short val;
Roberto Cerati45a16932013-04-24 10:46:17 +0800335
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100336 ks_read_config(ks);
Roberto Cerati45a16932013-04-24 10:46:17 +0800337
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100338 val = ks_rdreg16(ks, KS_CIDER);
Roberto Cerati45a16932013-04-24 10:46:17 +0800339
340 if (val == 0xffff) {
341 /* Special case -- no chip present */
342 printf(DRIVERNAME ": is chip mounted ?\n");
343 return -1;
344 } else if ((val & 0xfff0) != CIDER_ID) {
345 printf(DRIVERNAME ": Invalid chip id 0x%04x\n", val);
346 return -1;
347 }
348
349 debug("Read back KS8851 id 0x%x\n", val);
350
Marek Vasuteb69d8b2020-03-25 18:15:46 +0100351 if ((val & 0xfff0) != CIDER_ID) {
Roberto Cerati45a16932013-04-24 10:46:17 +0800352 printf(DRIVERNAME ": Unknown chip ID %04x\n", val);
353 return -1;
354 }
355
Roberto Cerati45a16932013-04-24 10:46:17 +0800356 return 0;
357}
358
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100359static void ks8851_mll_reset(struct ks_net *ks)
Roberto Cerati45a16932013-04-24 10:46:17 +0800360{
361 /* wake up powermode to normal mode */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100362 ks_set_powermode(ks, PMECR_PM_NORMAL);
Roberto Cerati45a16932013-04-24 10:46:17 +0800363 mdelay(1); /* wait for normal mode to take effect */
364
365 /* Disable interrupt and reset */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100366 ks_soft_reset(ks, GRR_GSR);
Roberto Cerati45a16932013-04-24 10:46:17 +0800367
368 /* turn off the IRQs and ack any outstanding */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100369 ks_wrreg16(ks, KS_IER, 0x0000);
370 ks_wrreg16(ks, KS_ISR, 0xffff);
Roberto Cerati45a16932013-04-24 10:46:17 +0800371
372 /* shutdown RX/TX QMU */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100373 ks_disable_qmu(ks);
Roberto Cerati45a16932013-04-24 10:46:17 +0800374}
375
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100376static void ks8851_mll_phy_configure(struct ks_net *ks)
Roberto Cerati45a16932013-04-24 10:46:17 +0800377{
378 u16 data;
379
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100380 ks_setup(ks);
381 ks_setup_int(ks);
Roberto Cerati45a16932013-04-24 10:46:17 +0800382
383 /* Probing the phy */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100384 data = ks_rdreg16(ks, KS_OBCR);
385 ks_wrreg16(ks, KS_OBCR, data | OBCR_ODS_16MA);
Roberto Cerati45a16932013-04-24 10:46:17 +0800386
387 debug(DRIVERNAME ": phy initialized\n");
388}
389
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100390static void ks8851_mll_enable(struct ks_net *ks)
Roberto Cerati45a16932013-04-24 10:46:17 +0800391{
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100392 ks_wrreg16(ks, KS_ISR, 0xffff);
393 ks_enable_int(ks);
394 ks_enable_qmu(ks);
Roberto Cerati45a16932013-04-24 10:46:17 +0800395}
396
Marek Vasutf7259122020-03-25 17:54:45 +0100397static int ks8851_mll_init_common(struct ks_net *ks)
Roberto Cerati45a16932013-04-24 10:46:17 +0800398{
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100399 if (ks_read_selftest(ks)) {
Roberto Cerati45a16932013-04-24 10:46:17 +0800400 printf(DRIVERNAME ": Selftest failed\n");
401 return -1;
402 }
403
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100404 ks8851_mll_reset(ks);
Roberto Cerati45a16932013-04-24 10:46:17 +0800405
406 /* Configure the PHY, initialize the link state */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100407 ks8851_mll_phy_configure(ks);
Roberto Cerati45a16932013-04-24 10:46:17 +0800408
Marek Vasut9c9f3fc2020-03-25 18:47:10 +0100409 ks->rxfc = 0;
410
Roberto Cerati45a16932013-04-24 10:46:17 +0800411 /* Turn on Tx + Rx */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100412 ks8851_mll_enable(ks);
Roberto Cerati45a16932013-04-24 10:46:17 +0800413
414 return 0;
415}
416
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100417static void ks_write_qmu(struct ks_net *ks, u8 *pdata, u16 len)
Roberto Cerati45a16932013-04-24 10:46:17 +0800418{
Marek Vasutb0435972020-03-25 17:18:55 +0100419 __le16 txw[2];
Roberto Cerati45a16932013-04-24 10:46:17 +0800420 /* start header at txb[0] to align txw entries */
Marek Vasutb0435972020-03-25 17:18:55 +0100421 txw[0] = 0;
422 txw[1] = cpu_to_le16(len);
Roberto Cerati45a16932013-04-24 10:46:17 +0800423
424 /* 1. set sudo-DMA mode */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100425 ks_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
426 ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL | RXQCR_SDA);
Marek Vasut8ec27b02020-03-25 17:25:29 +0100427 /* 2. write status/length info */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100428 ks_outblk(ks, txw, 4);
Roberto Cerati45a16932013-04-24 10:46:17 +0800429 /* 3. write pkt data */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100430 ks_outblk(ks, (u16 *)pdata, ALIGN(len, 4));
Roberto Cerati45a16932013-04-24 10:46:17 +0800431 /* 4. reset sudo-DMA mode */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100432 ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL);
Roberto Cerati45a16932013-04-24 10:46:17 +0800433 /* 5. Enqueue Tx(move the pkt from TX buffer into TXQ) */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100434 ks_wrreg16(ks, KS_TXQCR, TXQCR_METFE);
Roberto Cerati45a16932013-04-24 10:46:17 +0800435 /* 6. wait until TXQCR_METFE is auto-cleared */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100436 do { } while (ks_rdreg16(ks, KS_TXQCR) & TXQCR_METFE);
Roberto Cerati45a16932013-04-24 10:46:17 +0800437}
438
Marek Vasutf7259122020-03-25 17:54:45 +0100439static int ks8851_mll_send_common(struct ks_net *ks, void *packet, int length)
Roberto Cerati45a16932013-04-24 10:46:17 +0800440{
441 u8 *data = (u8 *)packet;
442 u16 tmplen = (u16)length;
443 u16 retv;
444
445 /*
446 * Extra space are required:
447 * 4 byte for alignment, 4 for status/length, 4 for CRC
448 */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100449 retv = ks_rdreg16(ks, KS_TXMIR) & 0x1fff;
Roberto Cerati45a16932013-04-24 10:46:17 +0800450 if (retv >= tmplen + 12) {
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100451 ks_write_qmu(ks, data, tmplen);
Roberto Cerati45a16932013-04-24 10:46:17 +0800452 return 0;
Roberto Cerati45a16932013-04-24 10:46:17 +0800453 }
Marek Vasut8ec27b02020-03-25 17:25:29 +0100454
455 printf(DRIVERNAME ": failed to send packet: No buffer\n");
456 return -1;
Roberto Cerati45a16932013-04-24 10:46:17 +0800457}
458
Marek Vasutf7259122020-03-25 17:54:45 +0100459static void ks8851_mll_halt_common(struct ks_net *ks)
Roberto Cerati45a16932013-04-24 10:46:17 +0800460{
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100461 ks8851_mll_reset(ks);
Roberto Cerati45a16932013-04-24 10:46:17 +0800462}
463
464/*
465 * Maximum receive ring size; that is, the number of packets
466 * we can buffer before overflow happens. Basically, this just
467 * needs to be enough to prevent a packet being discarded while
468 * we are processing the previous one.
469 */
Marek Vasut9c9f3fc2020-03-25 18:47:10 +0100470static int ks8851_mll_recv_common(struct ks_net *ks, uchar *data)
Roberto Cerati45a16932013-04-24 10:46:17 +0800471{
472 u16 status;
Marek Vasut9c9f3fc2020-03-25 18:47:10 +0100473 int ret = 0;
Roberto Cerati45a16932013-04-24 10:46:17 +0800474
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100475 status = ks_rdreg16(ks, KS_ISR);
Roberto Cerati45a16932013-04-24 10:46:17 +0800476
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100477 ks_wrreg16(ks, KS_ISR, status);
Roberto Cerati45a16932013-04-24 10:46:17 +0800478
Marek Vasut9c9f3fc2020-03-25 18:47:10 +0100479 if (ks->rxfc || (status & IRQ_RXI))
480 ret = ks_rcv(ks, data);
Roberto Cerati45a16932013-04-24 10:46:17 +0800481
Marek Vasut8ec27b02020-03-25 17:25:29 +0100482 if (status & IRQ_LDI) {
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100483 u16 pmecr = ks_rdreg16(ks, KS_PMECR);
Marek Vasut8ec27b02020-03-25 17:25:29 +0100484
Roberto Cerati45a16932013-04-24 10:46:17 +0800485 pmecr &= ~PMECR_WKEVT_MASK;
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100486 ks_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
Roberto Cerati45a16932013-04-24 10:46:17 +0800487 }
488
Marek Vasut9c9f3fc2020-03-25 18:47:10 +0100489 return ret;
Roberto Cerati45a16932013-04-24 10:46:17 +0800490}
491
Marek Vasutf7259122020-03-25 17:54:45 +0100492static void ks8851_mll_write_hwaddr_common(struct ks_net *ks, u8 enetaddr[6])
Roberto Cerati45a16932013-04-24 10:46:17 +0800493{
494 u16 addrl, addrm, addrh;
495
Marek Vasutf7259122020-03-25 17:54:45 +0100496 addrh = (enetaddr[0] << 8) | enetaddr[1];
497 addrm = (enetaddr[2] << 8) | enetaddr[3];
498 addrl = (enetaddr[4] << 8) | enetaddr[5];
Roberto Cerati45a16932013-04-24 10:46:17 +0800499
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100500 ks_wrreg16(ks, KS_MARH, addrh);
501 ks_wrreg16(ks, KS_MARM, addrm);
502 ks_wrreg16(ks, KS_MARL, addrl);
Marek Vasutf7259122020-03-25 17:54:45 +0100503}
504
505static int ks8851_mll_init(struct eth_device *dev, bd_t *bd)
506{
507 struct ks_net *ks = container_of(dev, struct ks_net, dev);
508
509 return ks8851_mll_init_common(ks);
510}
511
512static void ks8851_mll_halt(struct eth_device *dev)
513{
514 struct ks_net *ks = container_of(dev, struct ks_net, dev);
515
516 ks8851_mll_halt_common(ks);
517}
518
519static int ks8851_mll_send(struct eth_device *dev, void *packet, int length)
520{
521 struct ks_net *ks = container_of(dev, struct ks_net, dev);
522
523 return ks8851_mll_send_common(ks, packet, length);
524}
525
526static int ks8851_mll_recv(struct eth_device *dev)
527{
528 struct ks_net *ks = container_of(dev, struct ks_net, dev);
Marek Vasut9c9f3fc2020-03-25 18:47:10 +0100529 int ret;
Marek Vasutf7259122020-03-25 17:54:45 +0100530
Marek Vasut9c9f3fc2020-03-25 18:47:10 +0100531 ret = ks8851_mll_recv_common(ks, net_rx_packets[0]);
532 if (ret)
533 net_process_received_packet(net_rx_packets[0], ret);
534
535 return ret;
Marek Vasutf7259122020-03-25 17:54:45 +0100536}
537
538static int ks8851_mll_write_hwaddr(struct eth_device *dev)
539{
540 struct ks_net *ks = container_of(dev, struct ks_net, dev);
541
542 ks8851_mll_write_hwaddr_common(ks, ks->dev.enetaddr);
Roberto Cerati45a16932013-04-24 10:46:17 +0800543
544 return 0;
545}
546
547int ks8851_mll_initialize(u8 dev_num, int base_addr)
548{
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100549 struct ks_net *ks;
Roberto Cerati45a16932013-04-24 10:46:17 +0800550
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100551 ks = calloc(1, sizeof(*ks));
552 if (!ks)
Marek Vasute3b54cd2020-03-25 16:52:38 +0100553 return -ENOMEM;
Roberto Cerati45a16932013-04-24 10:46:17 +0800554
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100555 ks->iobase = base_addr;
Roberto Cerati45a16932013-04-24 10:46:17 +0800556
557 /* Try to detect chip. Will fail if not present. */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100558 if (ks8851_mll_detect_chip(ks)) {
559 free(ks);
Roberto Cerati45a16932013-04-24 10:46:17 +0800560 return -1;
561 }
562
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100563 ks->dev.init = ks8851_mll_init;
564 ks->dev.halt = ks8851_mll_halt;
565 ks->dev.send = ks8851_mll_send;
566 ks->dev.recv = ks8851_mll_recv;
567 ks->dev.write_hwaddr = ks8851_mll_write_hwaddr;
568 sprintf(ks->dev.name, "%s-%hu", DRIVERNAME, dev_num);
Roberto Cerati45a16932013-04-24 10:46:17 +0800569
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100570 eth_register(&ks->dev);
Roberto Cerati45a16932013-04-24 10:46:17 +0800571
572 return 0;
573}