blob: cc2e826257a919686b24c1cdf50609dd89a4fc94 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Roberto Ceratib1eee652013-04-24 10:46:17 +08002/*
3 * Micrel KS8851_MLL 16bit Network driver
4 * Copyright (c) 2011 Roberto Cerati <roberto.cerati@bticino.it>
Roberto Ceratib1eee652013-04-24 10:46:17 +08005 */
6
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.h>
Roberto Ceratib1eee652013-04-24 10:46:17 +08008#include <asm/io.h>
Roberto Ceratib1eee652013-04-24 10:46:17 +08009#include <command.h>
10#include <malloc.h>
11#include <net.h>
12#include <miiphy.h>
Simon Glassdbd79542020-05-10 11:40:11 -060013#include <linux/delay.h>
Roberto Ceratib1eee652013-04-24 10:46:17 +080014
15#include "ks8851_mll.h"
16
17#define DRIVERNAME "ks8851_mll"
18
Roberto Ceratib1eee652013-04-24 10:46:17 +080019#define RX_BUF_SIZE 2000
20
Roberto Ceratib1eee652013-04-24 10:46:17 +080021/*
Roberto Ceratib1eee652013-04-24 10:46:17 +080022 * struct ks_net - KS8851 driver private data
Marek Vasut1e1693e2020-03-25 17:35:00 +010023 * @dev : legacy non-DM ethernet device structure
24 * @iobase : register base
Roberto Ceratib1eee652013-04-24 10:46:17 +080025 * @bus_width : i/o bus width.
Roberto Ceratib1eee652013-04-24 10:46:17 +080026 * @sharedbus : Multipex(addr and data bus) mode indicator.
Marek Vasut21bf5782020-03-25 17:23:11 +010027 * @extra_byte : number of extra byte prepended rx pkt.
Roberto Ceratib1eee652013-04-24 10:46:17 +080028 */
Roberto Ceratib1eee652013-04-24 10:46:17 +080029struct ks_net {
Marek Vasut1e1693e2020-03-25 17:35:00 +010030 phys_addr_t iobase;
Roberto Ceratib1eee652013-04-24 10:46:17 +080031 int bus_width;
Roberto Ceratib1eee652013-04-24 10:46:17 +080032 u16 sharedbus;
Marek Vasut5347ab62020-03-25 18:47:10 +010033 u16 rxfc;
Roberto Ceratib1eee652013-04-24 10:46:17 +080034 u8 extra_byte;
Marek Vasut1e1693e2020-03-25 17:35:00 +010035};
Roberto Ceratib1eee652013-04-24 10:46:17 +080036
37#define BE3 0x8000 /* Byte Enable 3 */
38#define BE2 0x4000 /* Byte Enable 2 */
39#define BE1 0x2000 /* Byte Enable 1 */
40#define BE0 0x1000 /* Byte Enable 0 */
41
Marek Vasut1e1693e2020-03-25 17:35:00 +010042static u8 ks_rdreg8(struct ks_net *ks, u16 offset)
Roberto Ceratib1eee652013-04-24 10:46:17 +080043{
44 u8 shift_bit = offset & 0x03;
45 u8 shift_data = (offset & 1) << 3;
46
Marek Vasut1e1693e2020-03-25 17:35:00 +010047 writew(offset | (BE0 << shift_bit), ks->iobase + 2);
Roberto Ceratib1eee652013-04-24 10:46:17 +080048
Marek Vasut1e1693e2020-03-25 17:35:00 +010049 return (u8)(readw(ks->iobase) >> shift_data);
Roberto Ceratib1eee652013-04-24 10:46:17 +080050}
51
Marek Vasut1e1693e2020-03-25 17:35:00 +010052static u16 ks_rdreg16(struct ks_net *ks, u16 offset)
Roberto Ceratib1eee652013-04-24 10:46:17 +080053{
Marek Vasut1e1693e2020-03-25 17:35:00 +010054 writew(offset | ((BE1 | BE0) << (offset & 0x02)), ks->iobase + 2);
Roberto Ceratib1eee652013-04-24 10:46:17 +080055
Marek Vasut1e1693e2020-03-25 17:35:00 +010056 return readw(ks->iobase);
Roberto Ceratib1eee652013-04-24 10:46:17 +080057}
58
Marek Vasut1e1693e2020-03-25 17:35:00 +010059static void ks_wrreg16(struct ks_net *ks, u16 offset, u16 val)
Roberto Ceratib1eee652013-04-24 10:46:17 +080060{
Marek Vasut1e1693e2020-03-25 17:35:00 +010061 writew(offset | ((BE1 | BE0) << (offset & 0x02)), ks->iobase + 2);
62 writew(val, ks->iobase);
Roberto Ceratib1eee652013-04-24 10:46:17 +080063}
64
65/*
66 * ks_inblk - read a block of data from QMU. This is called after sudo DMA mode
67 * enabled.
68 * @ks: The chip state
69 * @wptr: buffer address to save data
70 * @len: length in byte to read
71 */
Marek Vasut1e1693e2020-03-25 17:35:00 +010072static inline void ks_inblk(struct ks_net *ks, u16 *wptr, u32 len)
Roberto Ceratib1eee652013-04-24 10:46:17 +080073{
74 len >>= 1;
75
76 while (len--)
Marek Vasut1e1693e2020-03-25 17:35:00 +010077 *wptr++ = readw(ks->iobase);
Roberto Ceratib1eee652013-04-24 10:46:17 +080078}
79
80/*
81 * ks_outblk - write data to QMU. This is called after sudo DMA mode enabled.
82 * @ks: The chip information
83 * @wptr: buffer address
84 * @len: length in byte to write
85 */
Marek Vasut1e1693e2020-03-25 17:35:00 +010086static inline void ks_outblk(struct ks_net *ks, u16 *wptr, u32 len)
Roberto Ceratib1eee652013-04-24 10:46:17 +080087{
88 len >>= 1;
89
90 while (len--)
Marek Vasut1e1693e2020-03-25 17:35:00 +010091 writew(*wptr++, ks->iobase);
Roberto Ceratib1eee652013-04-24 10:46:17 +080092}
93
Marek Vasut1e1693e2020-03-25 17:35:00 +010094static void ks_enable_int(struct ks_net *ks)
Roberto Ceratib1eee652013-04-24 10:46:17 +080095{
Marek Vasut1e1693e2020-03-25 17:35:00 +010096 ks_wrreg16(ks, KS_IER, IRQ_LCI | IRQ_TXI | IRQ_RXI);
Roberto Ceratib1eee652013-04-24 10:46:17 +080097}
98
Marek Vasut1e1693e2020-03-25 17:35:00 +010099static void ks_set_powermode(struct ks_net *ks, unsigned int pwrmode)
Roberto Ceratib1eee652013-04-24 10:46:17 +0800100{
Marek Vasute457cb12020-03-25 17:25:29 +0100101 unsigned int pmecr;
Roberto Ceratib1eee652013-04-24 10:46:17 +0800102
Marek Vasut1e1693e2020-03-25 17:35:00 +0100103 ks_rdreg16(ks, KS_GRR);
104 pmecr = ks_rdreg16(ks, KS_PMECR);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800105 pmecr &= ~PMECR_PM_MASK;
106 pmecr |= pwrmode;
107
Marek Vasut1e1693e2020-03-25 17:35:00 +0100108 ks_wrreg16(ks, KS_PMECR, pmecr);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800109}
110
111/*
112 * ks_read_config - read chip configuration of bus width.
113 * @ks: The chip information
114 */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100115static void ks_read_config(struct ks_net *ks)
Roberto Ceratib1eee652013-04-24 10:46:17 +0800116{
117 u16 reg_data = 0;
118
119 /* Regardless of bus width, 8 bit read should always work. */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100120 reg_data = ks_rdreg8(ks, KS_CCR) & 0x00FF;
121 reg_data |= ks_rdreg8(ks, KS_CCR + 1) << 8;
Roberto Ceratib1eee652013-04-24 10:46:17 +0800122
123 /* addr/data bus are multiplexed */
124 ks->sharedbus = (reg_data & CCR_SHARED) == CCR_SHARED;
125
126 /*
127 * There are garbage data when reading data from QMU,
128 * depending on bus-width.
129 */
130 if (reg_data & CCR_8BIT) {
131 ks->bus_width = ENUM_BUS_8BIT;
132 ks->extra_byte = 1;
133 } else if (reg_data & CCR_16BIT) {
134 ks->bus_width = ENUM_BUS_16BIT;
135 ks->extra_byte = 2;
136 } else {
137 ks->bus_width = ENUM_BUS_32BIT;
138 ks->extra_byte = 4;
139 }
140}
141
142/*
143 * ks_soft_reset - issue one of the soft reset to the device
144 * @ks: The device state.
145 * @op: The bit(s) to set in the GRR
146 *
147 * Issue the relevant soft-reset command to the device's GRR register
148 * specified by @op.
149 *
150 * Note, the delays are in there as a caution to ensure that the reset
151 * has time to take effect and then complete. Since the datasheet does
152 * not currently specify the exact sequence, we have chosen something
153 * that seems to work with our device.
154 */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100155static void ks_soft_reset(struct ks_net *ks, unsigned int op)
Roberto Ceratib1eee652013-04-24 10:46:17 +0800156{
157 /* Disable interrupt first */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100158 ks_wrreg16(ks, KS_IER, 0x0000);
159 ks_wrreg16(ks, KS_GRR, op);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800160 mdelay(10); /* wait a short time to effect reset */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100161 ks_wrreg16(ks, KS_GRR, 0);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800162 mdelay(1); /* wait for condition to clear */
163}
164
Marek Vasut1e1693e2020-03-25 17:35:00 +0100165void ks_enable_qmu(struct ks_net *ks)
Roberto Ceratib1eee652013-04-24 10:46:17 +0800166{
167 u16 w;
168
Marek Vasut1e1693e2020-03-25 17:35:00 +0100169 w = ks_rdreg16(ks, KS_TXCR);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800170
171 /* Enables QMU Transmit (TXCR). */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100172 ks_wrreg16(ks, KS_TXCR, w | TXCR_TXE);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800173
174 /* Enable RX Frame Count Threshold and Auto-Dequeue RXQ Frame */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100175 w = ks_rdreg16(ks, KS_RXQCR);
176 ks_wrreg16(ks, KS_RXQCR, w | RXQCR_RXFCTE);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800177
178 /* Enables QMU Receive (RXCR1). */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100179 w = ks_rdreg16(ks, KS_RXCR1);
180 ks_wrreg16(ks, KS_RXCR1, w | RXCR1_RXE);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800181}
182
Marek Vasut1e1693e2020-03-25 17:35:00 +0100183static void ks_disable_qmu(struct ks_net *ks)
Roberto Ceratib1eee652013-04-24 10:46:17 +0800184{
185 u16 w;
186
Marek Vasut1e1693e2020-03-25 17:35:00 +0100187 w = ks_rdreg16(ks, KS_TXCR);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800188
189 /* Disables QMU Transmit (TXCR). */
190 w &= ~TXCR_TXE;
Marek Vasut1e1693e2020-03-25 17:35:00 +0100191 ks_wrreg16(ks, KS_TXCR, w);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800192
193 /* Disables QMU Receive (RXCR1). */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100194 w = ks_rdreg16(ks, KS_RXCR1);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800195 w &= ~RXCR1_RXE;
Marek Vasut1e1693e2020-03-25 17:35:00 +0100196 ks_wrreg16(ks, KS_RXCR1, w);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800197}
198
Marek Vasut1e1693e2020-03-25 17:35:00 +0100199static inline void ks_read_qmu(struct ks_net *ks, u16 *buf, u32 len)
Roberto Ceratib1eee652013-04-24 10:46:17 +0800200{
201 u32 r = ks->extra_byte & 0x1;
202 u32 w = ks->extra_byte - r;
203
204 /* 1. set sudo DMA mode */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100205 ks_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI);
206 ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL | RXQCR_SDA);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800207
208 /*
209 * 2. read prepend data
210 *
211 * read 4 + extra bytes and discard them.
212 * extra bytes for dummy, 2 for status, 2 for len
213 */
214
215 if (r)
Marek Vasut1e1693e2020-03-25 17:35:00 +0100216 ks_rdreg8(ks, 0);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800217
Marek Vasut1e1693e2020-03-25 17:35:00 +0100218 ks_inblk(ks, buf, w + 2 + 2);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800219
220 /* 3. read pkt data */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100221 ks_inblk(ks, buf, ALIGN(len, 4));
Roberto Ceratib1eee652013-04-24 10:46:17 +0800222
223 /* 4. reset sudo DMA Mode */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100224 ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800225}
226
Marek Vasut5347ab62020-03-25 18:47:10 +0100227static int ks_rcv(struct ks_net *ks, uchar *data)
Roberto Ceratib1eee652013-04-24 10:46:17 +0800228{
Marek Vasut21bf5782020-03-25 17:23:11 +0100229 u16 sts, len;
Roberto Ceratib1eee652013-04-24 10:46:17 +0800230
Marek Vasut5347ab62020-03-25 18:47:10 +0100231 if (!ks->rxfc)
232 ks->rxfc = ks_rdreg16(ks, KS_RXFCTR) >> 8;
Roberto Ceratib1eee652013-04-24 10:46:17 +0800233
Marek Vasut5347ab62020-03-25 18:47:10 +0100234 if (!ks->rxfc)
235 return 0;
Roberto Ceratib1eee652013-04-24 10:46:17 +0800236
Marek Vasut5347ab62020-03-25 18:47:10 +0100237 /* Checking Received packet status */
238 sts = ks_rdreg16(ks, KS_RXFHSR);
239 /* Get packet len from hardware */
240 len = ks_rdreg16(ks, KS_RXFHBCR);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800241
Marek Vasut5347ab62020-03-25 18:47:10 +0100242 if ((sts & RXFSHR_RXFV) && len && (len < RX_BUF_SIZE)) {
243 /* read data block including CRC 4 bytes */
244 ks_read_qmu(ks, (u16 *)data, len);
245 ks->rxfc--;
246 return len - 4;
Roberto Ceratib1eee652013-04-24 10:46:17 +0800247 }
Marek Vasut5347ab62020-03-25 18:47:10 +0100248
249 ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL | RXQCR_RRXEF);
Marek Vasute034d882021-01-06 15:16:01 +0100250 printf(DRIVERNAME ": bad packet (sts=0x%04x len=0x%04x)\n", sts, len);
251 ks->rxfc = 0;
Marek Vasut5347ab62020-03-25 18:47:10 +0100252 return 0;
Roberto Ceratib1eee652013-04-24 10:46:17 +0800253}
254
255/*
256 * ks_read_selftest - read the selftest memory info.
257 * @ks: The device state
258 *
259 * Read and check the TX/RX memory selftest information.
260 */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100261static int ks_read_selftest(struct ks_net *ks)
Roberto Ceratib1eee652013-04-24 10:46:17 +0800262{
263 u16 both_done = MBIR_TXMBF | MBIR_RXMBF;
264 u16 mbir;
265 int ret = 0;
266
Marek Vasut1e1693e2020-03-25 17:35:00 +0100267 mbir = ks_rdreg16(ks, KS_MBIR);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800268
269 if ((mbir & both_done) != both_done) {
270 printf(DRIVERNAME ": Memory selftest not finished\n");
271 return 0;
272 }
273
274 if (mbir & MBIR_TXMBFA) {
275 printf(DRIVERNAME ": TX memory selftest fails\n");
276 ret |= 1;
277 }
278
279 if (mbir & MBIR_RXMBFA) {
280 printf(DRIVERNAME ": RX memory selftest fails\n");
281 ret |= 2;
282 }
283
284 debug(DRIVERNAME ": the selftest passes\n");
285
286 return ret;
287}
288
Marek Vasut1e1693e2020-03-25 17:35:00 +0100289static void ks_setup(struct ks_net *ks)
Roberto Ceratib1eee652013-04-24 10:46:17 +0800290{
291 u16 w;
292
293 /* Setup Transmit Frame Data Pointer Auto-Increment (TXFDPR) */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100294 ks_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800295
296 /* Setup Receive Frame Data Pointer Auto-Increment */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100297 ks_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800298
299 /* Setup Receive Frame Threshold - 1 frame (RXFCTFC) */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100300 ks_wrreg16(ks, KS_RXFCTR, 1 & RXFCTR_THRESHOLD_MASK);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800301
302 /* Setup RxQ Command Control (RXQCR) */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100303 ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800304
305 /*
306 * set the force mode to half duplex, default is full duplex
307 * because if the auto-negotiation fails, most switch uses
308 * half-duplex.
309 */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100310 w = ks_rdreg16(ks, KS_P1MBCR);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800311 w &= ~P1MBCR_FORCE_FDX;
Marek Vasut1e1693e2020-03-25 17:35:00 +0100312 ks_wrreg16(ks, KS_P1MBCR, w);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800313
314 w = TXCR_TXFCE | TXCR_TXPE | TXCR_TXCRC | TXCR_TCGIP;
Marek Vasut1e1693e2020-03-25 17:35:00 +0100315 ks_wrreg16(ks, KS_TXCR, w);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800316
317 w = RXCR1_RXFCE | RXCR1_RXBE | RXCR1_RXUE | RXCR1_RXME | RXCR1_RXIPFCC;
318
319 /* Normal mode */
320 w |= RXCR1_RXPAFMA;
321
Marek Vasut1e1693e2020-03-25 17:35:00 +0100322 ks_wrreg16(ks, KS_RXCR1, w);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800323}
324
Marek Vasut1e1693e2020-03-25 17:35:00 +0100325static void ks_setup_int(struct ks_net *ks)
Roberto Ceratib1eee652013-04-24 10:46:17 +0800326{
Roberto Ceratib1eee652013-04-24 10:46:17 +0800327 /* Clear the interrupts status of the hardware. */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100328 ks_wrreg16(ks, KS_ISR, 0xffff);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800329}
330
Marek Vasut1e1693e2020-03-25 17:35:00 +0100331static int ks8851_mll_detect_chip(struct ks_net *ks)
Roberto Ceratib1eee652013-04-24 10:46:17 +0800332{
Marek Vasutb4ba60b2020-03-25 18:15:46 +0100333 unsigned short val;
Roberto Ceratib1eee652013-04-24 10:46:17 +0800334
Marek Vasut1e1693e2020-03-25 17:35:00 +0100335 ks_read_config(ks);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800336
Marek Vasut1e1693e2020-03-25 17:35:00 +0100337 val = ks_rdreg16(ks, KS_CIDER);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800338
339 if (val == 0xffff) {
340 /* Special case -- no chip present */
341 printf(DRIVERNAME ": is chip mounted ?\n");
342 return -1;
343 } else if ((val & 0xfff0) != CIDER_ID) {
344 printf(DRIVERNAME ": Invalid chip id 0x%04x\n", val);
345 return -1;
346 }
347
348 debug("Read back KS8851 id 0x%x\n", val);
349
Marek Vasutb4ba60b2020-03-25 18:15:46 +0100350 if ((val & 0xfff0) != CIDER_ID) {
Roberto Ceratib1eee652013-04-24 10:46:17 +0800351 printf(DRIVERNAME ": Unknown chip ID %04x\n", val);
352 return -1;
353 }
354
Roberto Ceratib1eee652013-04-24 10:46:17 +0800355 return 0;
356}
357
Marek Vasut1e1693e2020-03-25 17:35:00 +0100358static void ks8851_mll_reset(struct ks_net *ks)
Roberto Ceratib1eee652013-04-24 10:46:17 +0800359{
360 /* wake up powermode to normal mode */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100361 ks_set_powermode(ks, PMECR_PM_NORMAL);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800362 mdelay(1); /* wait for normal mode to take effect */
363
364 /* Disable interrupt and reset */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100365 ks_soft_reset(ks, GRR_GSR);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800366
367 /* turn off the IRQs and ack any outstanding */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100368 ks_wrreg16(ks, KS_IER, 0x0000);
369 ks_wrreg16(ks, KS_ISR, 0xffff);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800370
371 /* shutdown RX/TX QMU */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100372 ks_disable_qmu(ks);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800373}
374
Marek Vasut1e1693e2020-03-25 17:35:00 +0100375static void ks8851_mll_phy_configure(struct ks_net *ks)
Roberto Ceratib1eee652013-04-24 10:46:17 +0800376{
377 u16 data;
378
Marek Vasut1e1693e2020-03-25 17:35:00 +0100379 ks_setup(ks);
380 ks_setup_int(ks);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800381
382 /* Probing the phy */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100383 data = ks_rdreg16(ks, KS_OBCR);
384 ks_wrreg16(ks, KS_OBCR, data | OBCR_ODS_16MA);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800385
386 debug(DRIVERNAME ": phy initialized\n");
387}
388
Marek Vasut1e1693e2020-03-25 17:35:00 +0100389static void ks8851_mll_enable(struct ks_net *ks)
Roberto Ceratib1eee652013-04-24 10:46:17 +0800390{
Marek Vasut1e1693e2020-03-25 17:35:00 +0100391 ks_wrreg16(ks, KS_ISR, 0xffff);
392 ks_enable_int(ks);
393 ks_enable_qmu(ks);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800394}
395
Marek Vasutc0bb0d92020-03-25 17:54:45 +0100396static int ks8851_mll_init_common(struct ks_net *ks)
Roberto Ceratib1eee652013-04-24 10:46:17 +0800397{
Marek Vasut1e1693e2020-03-25 17:35:00 +0100398 if (ks_read_selftest(ks)) {
Roberto Ceratib1eee652013-04-24 10:46:17 +0800399 printf(DRIVERNAME ": Selftest failed\n");
400 return -1;
401 }
402
Marek Vasut1e1693e2020-03-25 17:35:00 +0100403 ks8851_mll_reset(ks);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800404
405 /* Configure the PHY, initialize the link state */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100406 ks8851_mll_phy_configure(ks);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800407
Marek Vasut5347ab62020-03-25 18:47:10 +0100408 ks->rxfc = 0;
409
Roberto Ceratib1eee652013-04-24 10:46:17 +0800410 /* Turn on Tx + Rx */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100411 ks8851_mll_enable(ks);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800412
413 return 0;
414}
415
Marek Vasut1e1693e2020-03-25 17:35:00 +0100416static void ks_write_qmu(struct ks_net *ks, u8 *pdata, u16 len)
Roberto Ceratib1eee652013-04-24 10:46:17 +0800417{
Marek Vasut1994c902020-03-25 17:18:55 +0100418 __le16 txw[2];
Roberto Ceratib1eee652013-04-24 10:46:17 +0800419 /* start header at txb[0] to align txw entries */
Marek Vasut1994c902020-03-25 17:18:55 +0100420 txw[0] = 0;
421 txw[1] = cpu_to_le16(len);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800422
423 /* 1. set sudo-DMA mode */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100424 ks_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
425 ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL | RXQCR_SDA);
Marek Vasute457cb12020-03-25 17:25:29 +0100426 /* 2. write status/length info */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100427 ks_outblk(ks, txw, 4);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800428 /* 3. write pkt data */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100429 ks_outblk(ks, (u16 *)pdata, ALIGN(len, 4));
Roberto Ceratib1eee652013-04-24 10:46:17 +0800430 /* 4. reset sudo-DMA mode */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100431 ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800432 /* 5. Enqueue Tx(move the pkt from TX buffer into TXQ) */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100433 ks_wrreg16(ks, KS_TXQCR, TXQCR_METFE);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800434 /* 6. wait until TXQCR_METFE is auto-cleared */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100435 do { } while (ks_rdreg16(ks, KS_TXQCR) & TXQCR_METFE);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800436}
437
Marek Vasutc0bb0d92020-03-25 17:54:45 +0100438static int ks8851_mll_send_common(struct ks_net *ks, void *packet, int length)
Roberto Ceratib1eee652013-04-24 10:46:17 +0800439{
440 u8 *data = (u8 *)packet;
441 u16 tmplen = (u16)length;
442 u16 retv;
443
444 /*
445 * Extra space are required:
446 * 4 byte for alignment, 4 for status/length, 4 for CRC
447 */
Marek Vasut1e1693e2020-03-25 17:35:00 +0100448 retv = ks_rdreg16(ks, KS_TXMIR) & 0x1fff;
Roberto Ceratib1eee652013-04-24 10:46:17 +0800449 if (retv >= tmplen + 12) {
Marek Vasut1e1693e2020-03-25 17:35:00 +0100450 ks_write_qmu(ks, data, tmplen);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800451 return 0;
Roberto Ceratib1eee652013-04-24 10:46:17 +0800452 }
Marek Vasute457cb12020-03-25 17:25:29 +0100453
454 printf(DRIVERNAME ": failed to send packet: No buffer\n");
455 return -1;
Roberto Ceratib1eee652013-04-24 10:46:17 +0800456}
457
Marek Vasutc0bb0d92020-03-25 17:54:45 +0100458static void ks8851_mll_halt_common(struct ks_net *ks)
Roberto Ceratib1eee652013-04-24 10:46:17 +0800459{
Marek Vasut1e1693e2020-03-25 17:35:00 +0100460 ks8851_mll_reset(ks);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800461}
462
463/*
464 * Maximum receive ring size; that is, the number of packets
465 * we can buffer before overflow happens. Basically, this just
466 * needs to be enough to prevent a packet being discarded while
467 * we are processing the previous one.
468 */
Marek Vasut5347ab62020-03-25 18:47:10 +0100469static int ks8851_mll_recv_common(struct ks_net *ks, uchar *data)
Roberto Ceratib1eee652013-04-24 10:46:17 +0800470{
471 u16 status;
Marek Vasut5347ab62020-03-25 18:47:10 +0100472 int ret = 0;
Roberto Ceratib1eee652013-04-24 10:46:17 +0800473
Marek Vasut1e1693e2020-03-25 17:35:00 +0100474 status = ks_rdreg16(ks, KS_ISR);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800475
Marek Vasut1e1693e2020-03-25 17:35:00 +0100476 ks_wrreg16(ks, KS_ISR, status);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800477
Marek Vasut5347ab62020-03-25 18:47:10 +0100478 if (ks->rxfc || (status & IRQ_RXI))
479 ret = ks_rcv(ks, data);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800480
Marek Vasute457cb12020-03-25 17:25:29 +0100481 if (status & IRQ_LDI) {
Marek Vasut1e1693e2020-03-25 17:35:00 +0100482 u16 pmecr = ks_rdreg16(ks, KS_PMECR);
Marek Vasute457cb12020-03-25 17:25:29 +0100483
Roberto Ceratib1eee652013-04-24 10:46:17 +0800484 pmecr &= ~PMECR_WKEVT_MASK;
Marek Vasut1e1693e2020-03-25 17:35:00 +0100485 ks_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
Roberto Ceratib1eee652013-04-24 10:46:17 +0800486 }
487
Marek Vasut5347ab62020-03-25 18:47:10 +0100488 return ret;
Roberto Ceratib1eee652013-04-24 10:46:17 +0800489}
490
Marek Vasutc0bb0d92020-03-25 17:54:45 +0100491static void ks8851_mll_write_hwaddr_common(struct ks_net *ks, u8 enetaddr[6])
Roberto Ceratib1eee652013-04-24 10:46:17 +0800492{
493 u16 addrl, addrm, addrh;
494
Marek Vasutc0bb0d92020-03-25 17:54:45 +0100495 addrh = (enetaddr[0] << 8) | enetaddr[1];
496 addrm = (enetaddr[2] << 8) | enetaddr[3];
497 addrl = (enetaddr[4] << 8) | enetaddr[5];
Roberto Ceratib1eee652013-04-24 10:46:17 +0800498
Marek Vasut1e1693e2020-03-25 17:35:00 +0100499 ks_wrreg16(ks, KS_MARH, addrh);
500 ks_wrreg16(ks, KS_MARM, addrm);
501 ks_wrreg16(ks, KS_MARL, addrl);
Marek Vasutc0bb0d92020-03-25 17:54:45 +0100502}
503
Marek Vasutf5d054b2020-03-25 18:00:35 +0100504static int ks8851_start(struct udevice *dev)
505{
506 struct ks_net *ks = dev_get_priv(dev);
507
508 return ks8851_mll_init_common(ks);
509}
510
511static void ks8851_stop(struct udevice *dev)
512{
513 struct ks_net *ks = dev_get_priv(dev);
514
515 ks8851_mll_halt_common(ks);
516}
517
518static int ks8851_send(struct udevice *dev, void *packet, int length)
519{
520 struct ks_net *ks = dev_get_priv(dev);
521 int ret;
522
523 ret = ks8851_mll_send_common(ks, packet, length);
524
525 return ret ? 0 : -ETIMEDOUT;
526}
527
528static int ks8851_recv(struct udevice *dev, int flags, uchar **packetp)
529{
530 struct ks_net *ks = dev_get_priv(dev);
531 uchar *data = net_rx_packets[0];
532 int ret;
533
534 ret = ks8851_mll_recv_common(ks, data);
535 if (ret)
536 *packetp = (void *)data;
537
538 return ret ? ret : -EAGAIN;
539}
540
541static int ks8851_write_hwaddr(struct udevice *dev)
542{
543 struct ks_net *ks = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700544 struct eth_pdata *pdata = dev_get_plat(dev);
Marek Vasutf5d054b2020-03-25 18:00:35 +0100545
546 ks8851_mll_write_hwaddr_common(ks, pdata->enetaddr);
547
548 return 0;
549}
550
Marek Vasut8530bb42020-10-08 15:14:17 +0200551static int ks8851_read_rom_hwaddr(struct udevice *dev)
552{
553 struct ks_net *ks = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700554 struct eth_pdata *pdata = dev_get_plat(dev);
Marek Vasut8530bb42020-10-08 15:14:17 +0200555 u16 addrl, addrm, addrh;
556
557 /* No EEPROM means no valid MAC address. */
558 if (!(ks_rdreg16(ks, KS_CCR) & CCR_EEPROM))
559 return -EINVAL;
560
561 /*
562 * If the EEPROM contains valid MAC address, it is loaded into
563 * the NIC on power on. Read the MAC out of the NIC registers.
564 */
565 addrl = ks_rdreg16(ks, KS_MARL);
566 addrm = ks_rdreg16(ks, KS_MARM);
567 addrh = ks_rdreg16(ks, KS_MARH);
568
569 pdata->enetaddr[0] = (addrh >> 8) & 0xff;
570 pdata->enetaddr[1] = addrh & 0xff;
571 pdata->enetaddr[2] = (addrm >> 8) & 0xff;
572 pdata->enetaddr[3] = addrm & 0xff;
573 pdata->enetaddr[4] = (addrl >> 8) & 0xff;
574 pdata->enetaddr[5] = addrl & 0xff;
575
576 return !is_valid_ethaddr(pdata->enetaddr);
577}
578
Marek Vasutf5d054b2020-03-25 18:00:35 +0100579static int ks8851_bind(struct udevice *dev)
580{
581 return device_set_name(dev, dev->name);
582}
583
584static int ks8851_probe(struct udevice *dev)
585{
586 struct ks_net *ks = dev_get_priv(dev);
587
588 /* Try to detect chip. Will fail if not present. */
589 ks8851_mll_detect_chip(ks);
590
591 return 0;
592}
593
Simon Glassaad29ae2020-12-03 16:55:21 -0700594static int ks8851_of_to_plat(struct udevice *dev)
Marek Vasutf5d054b2020-03-25 18:00:35 +0100595{
596 struct ks_net *ks = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700597 struct eth_pdata *pdata = dev_get_plat(dev);
Marek Vasutf5d054b2020-03-25 18:00:35 +0100598
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900599 pdata->iobase = dev_read_addr(dev);
Marek Vasutf5d054b2020-03-25 18:00:35 +0100600 ks->iobase = pdata->iobase;
601
602 return 0;
603}
604
605static const struct eth_ops ks8851_ops = {
606 .start = ks8851_start,
607 .stop = ks8851_stop,
608 .send = ks8851_send,
609 .recv = ks8851_recv,
610 .write_hwaddr = ks8851_write_hwaddr,
Marek Vasut8530bb42020-10-08 15:14:17 +0200611 .read_rom_hwaddr = ks8851_read_rom_hwaddr,
Marek Vasutf5d054b2020-03-25 18:00:35 +0100612};
613
614static const struct udevice_id ks8851_ids[] = {
615 { .compatible = "micrel,ks8851-mll" },
616 { }
617};
618
619U_BOOT_DRIVER(ks8851) = {
620 .name = "eth_ks8851",
621 .id = UCLASS_ETH,
622 .of_match = ks8851_ids,
623 .bind = ks8851_bind,
Simon Glassaad29ae2020-12-03 16:55:21 -0700624 .of_to_plat = ks8851_of_to_plat,
Marek Vasutf5d054b2020-03-25 18:00:35 +0100625 .probe = ks8851_probe,
626 .ops = &ks8851_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700627 .priv_auto = sizeof(struct ks_net),
Simon Glass71fa5b42020-12-03 16:55:18 -0700628 .plat_auto = sizeof(struct eth_pdata),
Marek Vasutf5d054b2020-03-25 18:00:35 +0100629 .flags = DM_FLAG_ALLOC_PRIV_DMA,
630};