blob: 518548e3bbcd2a10673e59ab432a07144e6b4821 [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 phys_addr_t iobase;
Roberto Cerati45a16932013-04-24 10:46:17 +080032 int bus_width;
Roberto Cerati45a16932013-04-24 10:46:17 +080033 u16 sharedbus;
Marek Vasut9c9f3fc2020-03-25 18:47:10 +010034 u16 rxfc;
Roberto Cerati45a16932013-04-24 10:46:17 +080035 u8 extra_byte;
Marek Vasutb7c6ae22020-03-25 17:35:00 +010036};
Roberto Cerati45a16932013-04-24 10:46:17 +080037
38#define BE3 0x8000 /* Byte Enable 3 */
39#define BE2 0x4000 /* Byte Enable 2 */
40#define BE1 0x2000 /* Byte Enable 1 */
41#define BE0 0x1000 /* Byte Enable 0 */
42
Marek Vasutb7c6ae22020-03-25 17:35:00 +010043static u8 ks_rdreg8(struct ks_net *ks, u16 offset)
Roberto Cerati45a16932013-04-24 10:46:17 +080044{
45 u8 shift_bit = offset & 0x03;
46 u8 shift_data = (offset & 1) << 3;
47
Marek Vasutb7c6ae22020-03-25 17:35:00 +010048 writew(offset | (BE0 << shift_bit), ks->iobase + 2);
Roberto Cerati45a16932013-04-24 10:46:17 +080049
Marek Vasutb7c6ae22020-03-25 17:35:00 +010050 return (u8)(readw(ks->iobase) >> shift_data);
Roberto Cerati45a16932013-04-24 10:46:17 +080051}
52
Marek Vasutb7c6ae22020-03-25 17:35:00 +010053static u16 ks_rdreg16(struct ks_net *ks, u16 offset)
Roberto Cerati45a16932013-04-24 10:46:17 +080054{
Marek Vasutb7c6ae22020-03-25 17:35:00 +010055 writew(offset | ((BE1 | BE0) << (offset & 0x02)), ks->iobase + 2);
Roberto Cerati45a16932013-04-24 10:46:17 +080056
Marek Vasutb7c6ae22020-03-25 17:35:00 +010057 return readw(ks->iobase);
Roberto Cerati45a16932013-04-24 10:46:17 +080058}
59
Marek Vasutb7c6ae22020-03-25 17:35:00 +010060static void ks_wrreg16(struct ks_net *ks, u16 offset, u16 val)
Roberto Cerati45a16932013-04-24 10:46:17 +080061{
Marek Vasutb7c6ae22020-03-25 17:35:00 +010062 writew(offset | ((BE1 | BE0) << (offset & 0x02)), ks->iobase + 2);
63 writew(val, ks->iobase);
Roberto Cerati45a16932013-04-24 10:46:17 +080064}
65
66/*
67 * ks_inblk - read a block of data from QMU. This is called after sudo DMA mode
68 * enabled.
69 * @ks: The chip state
70 * @wptr: buffer address to save data
71 * @len: length in byte to read
72 */
Marek Vasutb7c6ae22020-03-25 17:35:00 +010073static inline void ks_inblk(struct ks_net *ks, u16 *wptr, u32 len)
Roberto Cerati45a16932013-04-24 10:46:17 +080074{
75 len >>= 1;
76
77 while (len--)
Marek Vasutb7c6ae22020-03-25 17:35:00 +010078 *wptr++ = readw(ks->iobase);
Roberto Cerati45a16932013-04-24 10:46:17 +080079}
80
81/*
82 * ks_outblk - write data to QMU. This is called after sudo DMA mode enabled.
83 * @ks: The chip information
84 * @wptr: buffer address
85 * @len: length in byte to write
86 */
Marek Vasutb7c6ae22020-03-25 17:35:00 +010087static inline void ks_outblk(struct ks_net *ks, u16 *wptr, u32 len)
Roberto Cerati45a16932013-04-24 10:46:17 +080088{
89 len >>= 1;
90
91 while (len--)
Marek Vasutb7c6ae22020-03-25 17:35:00 +010092 writew(*wptr++, ks->iobase);
Roberto Cerati45a16932013-04-24 10:46:17 +080093}
94
Marek Vasutb7c6ae22020-03-25 17:35:00 +010095static void ks_enable_int(struct ks_net *ks)
Roberto Cerati45a16932013-04-24 10:46:17 +080096{
Marek Vasutb7c6ae22020-03-25 17:35:00 +010097 ks_wrreg16(ks, KS_IER, IRQ_LCI | IRQ_TXI | IRQ_RXI);
Roberto Cerati45a16932013-04-24 10:46:17 +080098}
99
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100100static void ks_set_powermode(struct ks_net *ks, unsigned int pwrmode)
Roberto Cerati45a16932013-04-24 10:46:17 +0800101{
Marek Vasut8ec27b02020-03-25 17:25:29 +0100102 unsigned int pmecr;
Roberto Cerati45a16932013-04-24 10:46:17 +0800103
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100104 ks_rdreg16(ks, KS_GRR);
105 pmecr = ks_rdreg16(ks, KS_PMECR);
Roberto Cerati45a16932013-04-24 10:46:17 +0800106 pmecr &= ~PMECR_PM_MASK;
107 pmecr |= pwrmode;
108
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100109 ks_wrreg16(ks, KS_PMECR, pmecr);
Roberto Cerati45a16932013-04-24 10:46:17 +0800110}
111
112/*
113 * ks_read_config - read chip configuration of bus width.
114 * @ks: The chip information
115 */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100116static void ks_read_config(struct ks_net *ks)
Roberto Cerati45a16932013-04-24 10:46:17 +0800117{
118 u16 reg_data = 0;
119
120 /* Regardless of bus width, 8 bit read should always work. */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100121 reg_data = ks_rdreg8(ks, KS_CCR) & 0x00FF;
122 reg_data |= ks_rdreg8(ks, KS_CCR + 1) << 8;
Roberto Cerati45a16932013-04-24 10:46:17 +0800123
124 /* addr/data bus are multiplexed */
125 ks->sharedbus = (reg_data & CCR_SHARED) == CCR_SHARED;
126
127 /*
128 * There are garbage data when reading data from QMU,
129 * depending on bus-width.
130 */
131 if (reg_data & CCR_8BIT) {
132 ks->bus_width = ENUM_BUS_8BIT;
133 ks->extra_byte = 1;
134 } else if (reg_data & CCR_16BIT) {
135 ks->bus_width = ENUM_BUS_16BIT;
136 ks->extra_byte = 2;
137 } else {
138 ks->bus_width = ENUM_BUS_32BIT;
139 ks->extra_byte = 4;
140 }
141}
142
143/*
144 * ks_soft_reset - issue one of the soft reset to the device
145 * @ks: The device state.
146 * @op: The bit(s) to set in the GRR
147 *
148 * Issue the relevant soft-reset command to the device's GRR register
149 * specified by @op.
150 *
151 * Note, the delays are in there as a caution to ensure that the reset
152 * has time to take effect and then complete. Since the datasheet does
153 * not currently specify the exact sequence, we have chosen something
154 * that seems to work with our device.
155 */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100156static void ks_soft_reset(struct ks_net *ks, unsigned int op)
Roberto Cerati45a16932013-04-24 10:46:17 +0800157{
158 /* Disable interrupt first */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100159 ks_wrreg16(ks, KS_IER, 0x0000);
160 ks_wrreg16(ks, KS_GRR, op);
Roberto Cerati45a16932013-04-24 10:46:17 +0800161 mdelay(10); /* wait a short time to effect reset */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100162 ks_wrreg16(ks, KS_GRR, 0);
Roberto Cerati45a16932013-04-24 10:46:17 +0800163 mdelay(1); /* wait for condition to clear */
164}
165
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100166void ks_enable_qmu(struct ks_net *ks)
Roberto Cerati45a16932013-04-24 10:46:17 +0800167{
168 u16 w;
169
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100170 w = ks_rdreg16(ks, KS_TXCR);
Roberto Cerati45a16932013-04-24 10:46:17 +0800171
172 /* Enables QMU Transmit (TXCR). */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100173 ks_wrreg16(ks, KS_TXCR, w | TXCR_TXE);
Roberto Cerati45a16932013-04-24 10:46:17 +0800174
175 /* Enable RX Frame Count Threshold and Auto-Dequeue RXQ Frame */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100176 w = ks_rdreg16(ks, KS_RXQCR);
177 ks_wrreg16(ks, KS_RXQCR, w | RXQCR_RXFCTE);
Roberto Cerati45a16932013-04-24 10:46:17 +0800178
179 /* Enables QMU Receive (RXCR1). */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100180 w = ks_rdreg16(ks, KS_RXCR1);
181 ks_wrreg16(ks, KS_RXCR1, w | RXCR1_RXE);
Roberto Cerati45a16932013-04-24 10:46:17 +0800182}
183
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100184static void ks_disable_qmu(struct ks_net *ks)
Roberto Cerati45a16932013-04-24 10:46:17 +0800185{
186 u16 w;
187
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100188 w = ks_rdreg16(ks, KS_TXCR);
Roberto Cerati45a16932013-04-24 10:46:17 +0800189
190 /* Disables QMU Transmit (TXCR). */
191 w &= ~TXCR_TXE;
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100192 ks_wrreg16(ks, KS_TXCR, w);
Roberto Cerati45a16932013-04-24 10:46:17 +0800193
194 /* Disables QMU Receive (RXCR1). */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100195 w = ks_rdreg16(ks, KS_RXCR1);
Roberto Cerati45a16932013-04-24 10:46:17 +0800196 w &= ~RXCR1_RXE;
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100197 ks_wrreg16(ks, KS_RXCR1, w);
Roberto Cerati45a16932013-04-24 10:46:17 +0800198}
199
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100200static inline void ks_read_qmu(struct ks_net *ks, u16 *buf, u32 len)
Roberto Cerati45a16932013-04-24 10:46:17 +0800201{
202 u32 r = ks->extra_byte & 0x1;
203 u32 w = ks->extra_byte - r;
204
205 /* 1. set sudo DMA mode */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100206 ks_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI);
207 ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL | RXQCR_SDA);
Roberto Cerati45a16932013-04-24 10:46:17 +0800208
209 /*
210 * 2. read prepend data
211 *
212 * read 4 + extra bytes and discard them.
213 * extra bytes for dummy, 2 for status, 2 for len
214 */
215
216 if (r)
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100217 ks_rdreg8(ks, 0);
Roberto Cerati45a16932013-04-24 10:46:17 +0800218
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100219 ks_inblk(ks, buf, w + 2 + 2);
Roberto Cerati45a16932013-04-24 10:46:17 +0800220
221 /* 3. read pkt data */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100222 ks_inblk(ks, buf, ALIGN(len, 4));
Roberto Cerati45a16932013-04-24 10:46:17 +0800223
224 /* 4. reset sudo DMA Mode */
Marek Vasutb7c6ae22020-03-25 17:35:00 +0100225 ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL);
Roberto Cerati45a16932013-04-24 10:46:17 +0800226}
227
Marek Vasut9c9f3fc2020-03-25 18:47:10 +0100228static int ks_rcv(struct ks_net *ks, uchar *data)
Roberto Cerati45a16932013-04-24 10:46:17 +0800229{
Marek Vasut63f22f52020-03-25 17:23:11 +0100230 u16 sts, len;
Roberto Cerati45a16932013-04-24 10:46:17 +0800231
Marek Vasut9c9f3fc2020-03-25 18:47:10 +0100232 if (!ks->rxfc)
233 ks->rxfc = ks_rdreg16(ks, KS_RXFCTR) >> 8;
Roberto Cerati45a16932013-04-24 10:46:17 +0800234
Marek Vasut9c9f3fc2020-03-25 18:47:10 +0100235 if (!ks->rxfc)
236 return 0;
Roberto Cerati45a16932013-04-24 10:46:17 +0800237
Marek Vasut9c9f3fc2020-03-25 18:47:10 +0100238 /* Checking Received packet status */
239 sts = ks_rdreg16(ks, KS_RXFHSR);
240 /* Get packet len from hardware */
241 len = ks_rdreg16(ks, KS_RXFHBCR);
Roberto Cerati45a16932013-04-24 10:46:17 +0800242
Marek Vasut9c9f3fc2020-03-25 18:47:10 +0100243 if ((sts & RXFSHR_RXFV) && len && (len < RX_BUF_SIZE)) {
244 /* read data block including CRC 4 bytes */
245 ks_read_qmu(ks, (u16 *)data, len);
246 ks->rxfc--;
247 return len - 4;
Roberto Cerati45a16932013-04-24 10:46:17 +0800248 }
Marek Vasut9c9f3fc2020-03-25 18:47:10 +0100249
250 ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL | RXQCR_RRXEF);
Marek Vasutdd70ff42021-01-06 15:16:01 +0100251 printf(DRIVERNAME ": bad packet (sts=0x%04x len=0x%04x)\n", sts, len);
252 ks->rxfc = 0;
Marek Vasut9c9f3fc2020-03-25 18:47:10 +0100253 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
Marek Vasut1d476de2020-03-25 18:00:35 +0100505static int ks8851_start(struct udevice *dev)
506{
507 struct ks_net *ks = dev_get_priv(dev);
508
509 return ks8851_mll_init_common(ks);
510}
511
512static void ks8851_stop(struct udevice *dev)
513{
514 struct ks_net *ks = dev_get_priv(dev);
515
516 ks8851_mll_halt_common(ks);
517}
518
519static int ks8851_send(struct udevice *dev, void *packet, int length)
520{
521 struct ks_net *ks = dev_get_priv(dev);
522 int ret;
523
524 ret = ks8851_mll_send_common(ks, packet, length);
525
526 return ret ? 0 : -ETIMEDOUT;
527}
528
529static int ks8851_recv(struct udevice *dev, int flags, uchar **packetp)
530{
531 struct ks_net *ks = dev_get_priv(dev);
532 uchar *data = net_rx_packets[0];
533 int ret;
534
535 ret = ks8851_mll_recv_common(ks, data);
536 if (ret)
537 *packetp = (void *)data;
538
539 return ret ? ret : -EAGAIN;
540}
541
542static int ks8851_write_hwaddr(struct udevice *dev)
543{
544 struct ks_net *ks = dev_get_priv(dev);
Simon Glassc69cda22020-12-03 16:55:20 -0700545 struct eth_pdata *pdata = dev_get_plat(dev);
Marek Vasut1d476de2020-03-25 18:00:35 +0100546
547 ks8851_mll_write_hwaddr_common(ks, pdata->enetaddr);
548
549 return 0;
550}
551
Marek Vasut68cbc632020-10-08 15:14:17 +0200552static int ks8851_read_rom_hwaddr(struct udevice *dev)
553{
554 struct ks_net *ks = dev_get_priv(dev);
Simon Glassc69cda22020-12-03 16:55:20 -0700555 struct eth_pdata *pdata = dev_get_plat(dev);
Marek Vasut68cbc632020-10-08 15:14:17 +0200556 u16 addrl, addrm, addrh;
557
558 /* No EEPROM means no valid MAC address. */
559 if (!(ks_rdreg16(ks, KS_CCR) & CCR_EEPROM))
560 return -EINVAL;
561
562 /*
563 * If the EEPROM contains valid MAC address, it is loaded into
564 * the NIC on power on. Read the MAC out of the NIC registers.
565 */
566 addrl = ks_rdreg16(ks, KS_MARL);
567 addrm = ks_rdreg16(ks, KS_MARM);
568 addrh = ks_rdreg16(ks, KS_MARH);
569
570 pdata->enetaddr[0] = (addrh >> 8) & 0xff;
571 pdata->enetaddr[1] = addrh & 0xff;
572 pdata->enetaddr[2] = (addrm >> 8) & 0xff;
573 pdata->enetaddr[3] = addrm & 0xff;
574 pdata->enetaddr[4] = (addrl >> 8) & 0xff;
575 pdata->enetaddr[5] = addrl & 0xff;
576
577 return !is_valid_ethaddr(pdata->enetaddr);
578}
579
Marek Vasut1d476de2020-03-25 18:00:35 +0100580static int ks8851_bind(struct udevice *dev)
581{
582 return device_set_name(dev, dev->name);
583}
584
585static int ks8851_probe(struct udevice *dev)
586{
587 struct ks_net *ks = dev_get_priv(dev);
588
589 /* Try to detect chip. Will fail if not present. */
590 ks8851_mll_detect_chip(ks);
591
592 return 0;
593}
594
Simon Glassd1998a92020-12-03 16:55:21 -0700595static int ks8851_of_to_plat(struct udevice *dev)
Marek Vasut1d476de2020-03-25 18:00:35 +0100596{
597 struct ks_net *ks = dev_get_priv(dev);
Simon Glassc69cda22020-12-03 16:55:20 -0700598 struct eth_pdata *pdata = dev_get_plat(dev);
Marek Vasut1d476de2020-03-25 18:00:35 +0100599
Masahiro Yamada25484932020-07-17 14:36:48 +0900600 pdata->iobase = dev_read_addr(dev);
Marek Vasut1d476de2020-03-25 18:00:35 +0100601 ks->iobase = pdata->iobase;
602
603 return 0;
604}
605
606static const struct eth_ops ks8851_ops = {
607 .start = ks8851_start,
608 .stop = ks8851_stop,
609 .send = ks8851_send,
610 .recv = ks8851_recv,
611 .write_hwaddr = ks8851_write_hwaddr,
Marek Vasut68cbc632020-10-08 15:14:17 +0200612 .read_rom_hwaddr = ks8851_read_rom_hwaddr,
Marek Vasut1d476de2020-03-25 18:00:35 +0100613};
614
615static const struct udevice_id ks8851_ids[] = {
616 { .compatible = "micrel,ks8851-mll" },
617 { }
618};
619
620U_BOOT_DRIVER(ks8851) = {
621 .name = "eth_ks8851",
622 .id = UCLASS_ETH,
623 .of_match = ks8851_ids,
624 .bind = ks8851_bind,
Simon Glassd1998a92020-12-03 16:55:21 -0700625 .of_to_plat = ks8851_of_to_plat,
Marek Vasut1d476de2020-03-25 18:00:35 +0100626 .probe = ks8851_probe,
627 .ops = &ks8851_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700628 .priv_auto = sizeof(struct ks_net),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700629 .plat_auto = sizeof(struct eth_pdata),
Marek Vasut1d476de2020-03-25 18:00:35 +0100630 .flags = DM_FLAG_ALLOC_PRIV_DMA,
631};