Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Micrel KS8851_MLL 16bit Network driver |
| 4 | * Copyright (c) 2011 Roberto Cerati <roberto.cerati@bticino.it> |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 7 | #include <log.h> |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 8 | #include <asm/io.h> |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 9 | #include <command.h> |
| 10 | #include <malloc.h> |
| 11 | #include <net.h> |
| 12 | #include <miiphy.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 13 | #include <linux/delay.h> |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 14 | |
| 15 | #include "ks8851_mll.h" |
| 16 | |
| 17 | #define DRIVERNAME "ks8851_mll" |
| 18 | |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 19 | #define RX_BUF_SIZE 2000 |
| 20 | |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 21 | /* |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 22 | * struct ks_net - KS8851 driver private data |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 23 | * @dev : legacy non-DM ethernet device structure |
| 24 | * @iobase : register base |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 25 | * @bus_width : i/o bus width. |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 26 | * @sharedbus : Multipex(addr and data bus) mode indicator. |
Marek Vasut | 21bf578 | 2020-03-25 17:23:11 +0100 | [diff] [blame] | 27 | * @extra_byte : number of extra byte prepended rx pkt. |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 28 | */ |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 29 | struct ks_net { |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 30 | phys_addr_t iobase; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 31 | int bus_width; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 32 | u16 sharedbus; |
Marek Vasut | 5347ab6 | 2020-03-25 18:47:10 +0100 | [diff] [blame] | 33 | u16 rxfc; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 34 | u8 extra_byte; |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 35 | }; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 36 | |
| 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 Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 42 | static u8 ks_rdreg8(struct ks_net *ks, u16 offset) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 43 | { |
| 44 | u8 shift_bit = offset & 0x03; |
| 45 | u8 shift_data = (offset & 1) << 3; |
| 46 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 47 | writew(offset | (BE0 << shift_bit), ks->iobase + 2); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 48 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 49 | return (u8)(readw(ks->iobase) >> shift_data); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 50 | } |
| 51 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 52 | static u16 ks_rdreg16(struct ks_net *ks, u16 offset) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 53 | { |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 54 | writew(offset | ((BE1 | BE0) << (offset & 0x02)), ks->iobase + 2); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 55 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 56 | return readw(ks->iobase); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 57 | } |
| 58 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 59 | static void ks_wrreg16(struct ks_net *ks, u16 offset, u16 val) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 60 | { |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 61 | writew(offset | ((BE1 | BE0) << (offset & 0x02)), ks->iobase + 2); |
| 62 | writew(val, ks->iobase); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 63 | } |
| 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 Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 72 | static inline void ks_inblk(struct ks_net *ks, u16 *wptr, u32 len) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 73 | { |
| 74 | len >>= 1; |
| 75 | |
| 76 | while (len--) |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 77 | *wptr++ = readw(ks->iobase); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 78 | } |
| 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 Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 86 | static inline void ks_outblk(struct ks_net *ks, u16 *wptr, u32 len) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 87 | { |
| 88 | len >>= 1; |
| 89 | |
| 90 | while (len--) |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 91 | writew(*wptr++, ks->iobase); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 92 | } |
| 93 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 94 | static void ks_enable_int(struct ks_net *ks) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 95 | { |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 96 | ks_wrreg16(ks, KS_IER, IRQ_LCI | IRQ_TXI | IRQ_RXI); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 97 | } |
| 98 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 99 | static void ks_set_powermode(struct ks_net *ks, unsigned int pwrmode) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 100 | { |
Marek Vasut | e457cb1 | 2020-03-25 17:25:29 +0100 | [diff] [blame] | 101 | unsigned int pmecr; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 102 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 103 | ks_rdreg16(ks, KS_GRR); |
| 104 | pmecr = ks_rdreg16(ks, KS_PMECR); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 105 | pmecr &= ~PMECR_PM_MASK; |
| 106 | pmecr |= pwrmode; |
| 107 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 108 | ks_wrreg16(ks, KS_PMECR, pmecr); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /* |
| 112 | * ks_read_config - read chip configuration of bus width. |
| 113 | * @ks: The chip information |
| 114 | */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 115 | static void ks_read_config(struct ks_net *ks) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 116 | { |
| 117 | u16 reg_data = 0; |
| 118 | |
| 119 | /* Regardless of bus width, 8 bit read should always work. */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 120 | reg_data = ks_rdreg8(ks, KS_CCR) & 0x00FF; |
| 121 | reg_data |= ks_rdreg8(ks, KS_CCR + 1) << 8; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 122 | |
| 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 Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 155 | static void ks_soft_reset(struct ks_net *ks, unsigned int op) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 156 | { |
| 157 | /* Disable interrupt first */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 158 | ks_wrreg16(ks, KS_IER, 0x0000); |
| 159 | ks_wrreg16(ks, KS_GRR, op); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 160 | mdelay(10); /* wait a short time to effect reset */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 161 | ks_wrreg16(ks, KS_GRR, 0); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 162 | mdelay(1); /* wait for condition to clear */ |
| 163 | } |
| 164 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 165 | void ks_enable_qmu(struct ks_net *ks) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 166 | { |
| 167 | u16 w; |
| 168 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 169 | w = ks_rdreg16(ks, KS_TXCR); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 170 | |
| 171 | /* Enables QMU Transmit (TXCR). */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 172 | ks_wrreg16(ks, KS_TXCR, w | TXCR_TXE); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 173 | |
| 174 | /* Enable RX Frame Count Threshold and Auto-Dequeue RXQ Frame */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 175 | w = ks_rdreg16(ks, KS_RXQCR); |
| 176 | ks_wrreg16(ks, KS_RXQCR, w | RXQCR_RXFCTE); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 177 | |
| 178 | /* Enables QMU Receive (RXCR1). */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 179 | w = ks_rdreg16(ks, KS_RXCR1); |
| 180 | ks_wrreg16(ks, KS_RXCR1, w | RXCR1_RXE); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 181 | } |
| 182 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 183 | static void ks_disable_qmu(struct ks_net *ks) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 184 | { |
| 185 | u16 w; |
| 186 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 187 | w = ks_rdreg16(ks, KS_TXCR); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 188 | |
| 189 | /* Disables QMU Transmit (TXCR). */ |
| 190 | w &= ~TXCR_TXE; |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 191 | ks_wrreg16(ks, KS_TXCR, w); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 192 | |
| 193 | /* Disables QMU Receive (RXCR1). */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 194 | w = ks_rdreg16(ks, KS_RXCR1); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 195 | w &= ~RXCR1_RXE; |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 196 | ks_wrreg16(ks, KS_RXCR1, w); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 197 | } |
| 198 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 199 | static inline void ks_read_qmu(struct ks_net *ks, u16 *buf, u32 len) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 200 | { |
| 201 | u32 r = ks->extra_byte & 0x1; |
| 202 | u32 w = ks->extra_byte - r; |
| 203 | |
| 204 | /* 1. set sudo DMA mode */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 205 | ks_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI); |
| 206 | ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL | RXQCR_SDA); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 207 | |
| 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 Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 216 | ks_rdreg8(ks, 0); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 217 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 218 | ks_inblk(ks, buf, w + 2 + 2); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 219 | |
| 220 | /* 3. read pkt data */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 221 | ks_inblk(ks, buf, ALIGN(len, 4)); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 222 | |
| 223 | /* 4. reset sudo DMA Mode */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 224 | ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 225 | } |
| 226 | |
Marek Vasut | 5347ab6 | 2020-03-25 18:47:10 +0100 | [diff] [blame] | 227 | static int ks_rcv(struct ks_net *ks, uchar *data) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 228 | { |
Marek Vasut | 21bf578 | 2020-03-25 17:23:11 +0100 | [diff] [blame] | 229 | u16 sts, len; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 230 | |
Marek Vasut | 5347ab6 | 2020-03-25 18:47:10 +0100 | [diff] [blame] | 231 | if (!ks->rxfc) |
| 232 | ks->rxfc = ks_rdreg16(ks, KS_RXFCTR) >> 8; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 233 | |
Marek Vasut | 5347ab6 | 2020-03-25 18:47:10 +0100 | [diff] [blame] | 234 | if (!ks->rxfc) |
| 235 | return 0; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 236 | |
Marek Vasut | 5347ab6 | 2020-03-25 18:47:10 +0100 | [diff] [blame] | 237 | /* 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 Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 241 | |
Marek Vasut | 5347ab6 | 2020-03-25 18:47:10 +0100 | [diff] [blame] | 242 | 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 Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 247 | } |
Marek Vasut | 5347ab6 | 2020-03-25 18:47:10 +0100 | [diff] [blame] | 248 | |
| 249 | ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL | RXQCR_RRXEF); |
Marek Vasut | e034d88 | 2021-01-06 15:16:01 +0100 | [diff] [blame] | 250 | printf(DRIVERNAME ": bad packet (sts=0x%04x len=0x%04x)\n", sts, len); |
| 251 | ks->rxfc = 0; |
Marek Vasut | 5347ab6 | 2020-03-25 18:47:10 +0100 | [diff] [blame] | 252 | return 0; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 253 | } |
| 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 Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 261 | static int ks_read_selftest(struct ks_net *ks) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 262 | { |
| 263 | u16 both_done = MBIR_TXMBF | MBIR_RXMBF; |
| 264 | u16 mbir; |
| 265 | int ret = 0; |
| 266 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 267 | mbir = ks_rdreg16(ks, KS_MBIR); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 268 | |
| 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 Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 289 | static void ks_setup(struct ks_net *ks) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 290 | { |
| 291 | u16 w; |
| 292 | |
| 293 | /* Setup Transmit Frame Data Pointer Auto-Increment (TXFDPR) */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 294 | ks_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 295 | |
| 296 | /* Setup Receive Frame Data Pointer Auto-Increment */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 297 | ks_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 298 | |
| 299 | /* Setup Receive Frame Threshold - 1 frame (RXFCTFC) */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 300 | ks_wrreg16(ks, KS_RXFCTR, 1 & RXFCTR_THRESHOLD_MASK); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 301 | |
| 302 | /* Setup RxQ Command Control (RXQCR) */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 303 | ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 304 | |
| 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 Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 310 | w = ks_rdreg16(ks, KS_P1MBCR); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 311 | w &= ~P1MBCR_FORCE_FDX; |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 312 | ks_wrreg16(ks, KS_P1MBCR, w); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 313 | |
| 314 | w = TXCR_TXFCE | TXCR_TXPE | TXCR_TXCRC | TXCR_TCGIP; |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 315 | ks_wrreg16(ks, KS_TXCR, w); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 316 | |
| 317 | w = RXCR1_RXFCE | RXCR1_RXBE | RXCR1_RXUE | RXCR1_RXME | RXCR1_RXIPFCC; |
| 318 | |
| 319 | /* Normal mode */ |
| 320 | w |= RXCR1_RXPAFMA; |
| 321 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 322 | ks_wrreg16(ks, KS_RXCR1, w); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 323 | } |
| 324 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 325 | static void ks_setup_int(struct ks_net *ks) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 326 | { |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 327 | /* Clear the interrupts status of the hardware. */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 328 | ks_wrreg16(ks, KS_ISR, 0xffff); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 329 | } |
| 330 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 331 | static int ks8851_mll_detect_chip(struct ks_net *ks) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 332 | { |
Marek Vasut | b4ba60b | 2020-03-25 18:15:46 +0100 | [diff] [blame] | 333 | unsigned short val; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 334 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 335 | ks_read_config(ks); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 336 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 337 | val = ks_rdreg16(ks, KS_CIDER); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 338 | |
| 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 Vasut | b4ba60b | 2020-03-25 18:15:46 +0100 | [diff] [blame] | 350 | if ((val & 0xfff0) != CIDER_ID) { |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 351 | printf(DRIVERNAME ": Unknown chip ID %04x\n", val); |
| 352 | return -1; |
| 353 | } |
| 354 | |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 355 | return 0; |
| 356 | } |
| 357 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 358 | static void ks8851_mll_reset(struct ks_net *ks) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 359 | { |
| 360 | /* wake up powermode to normal mode */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 361 | ks_set_powermode(ks, PMECR_PM_NORMAL); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 362 | mdelay(1); /* wait for normal mode to take effect */ |
| 363 | |
| 364 | /* Disable interrupt and reset */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 365 | ks_soft_reset(ks, GRR_GSR); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 366 | |
| 367 | /* turn off the IRQs and ack any outstanding */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 368 | ks_wrreg16(ks, KS_IER, 0x0000); |
| 369 | ks_wrreg16(ks, KS_ISR, 0xffff); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 370 | |
| 371 | /* shutdown RX/TX QMU */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 372 | ks_disable_qmu(ks); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 373 | } |
| 374 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 375 | static void ks8851_mll_phy_configure(struct ks_net *ks) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 376 | { |
| 377 | u16 data; |
| 378 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 379 | ks_setup(ks); |
| 380 | ks_setup_int(ks); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 381 | |
| 382 | /* Probing the phy */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 383 | data = ks_rdreg16(ks, KS_OBCR); |
| 384 | ks_wrreg16(ks, KS_OBCR, data | OBCR_ODS_16MA); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 385 | |
| 386 | debug(DRIVERNAME ": phy initialized\n"); |
| 387 | } |
| 388 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 389 | static void ks8851_mll_enable(struct ks_net *ks) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 390 | { |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 391 | ks_wrreg16(ks, KS_ISR, 0xffff); |
| 392 | ks_enable_int(ks); |
| 393 | ks_enable_qmu(ks); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 394 | } |
| 395 | |
Marek Vasut | c0bb0d9 | 2020-03-25 17:54:45 +0100 | [diff] [blame] | 396 | static int ks8851_mll_init_common(struct ks_net *ks) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 397 | { |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 398 | if (ks_read_selftest(ks)) { |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 399 | printf(DRIVERNAME ": Selftest failed\n"); |
| 400 | return -1; |
| 401 | } |
| 402 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 403 | ks8851_mll_reset(ks); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 404 | |
| 405 | /* Configure the PHY, initialize the link state */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 406 | ks8851_mll_phy_configure(ks); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 407 | |
Marek Vasut | 5347ab6 | 2020-03-25 18:47:10 +0100 | [diff] [blame] | 408 | ks->rxfc = 0; |
| 409 | |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 410 | /* Turn on Tx + Rx */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 411 | ks8851_mll_enable(ks); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 412 | |
| 413 | return 0; |
| 414 | } |
| 415 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 416 | static void ks_write_qmu(struct ks_net *ks, u8 *pdata, u16 len) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 417 | { |
Marek Vasut | 1994c90 | 2020-03-25 17:18:55 +0100 | [diff] [blame] | 418 | __le16 txw[2]; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 419 | /* start header at txb[0] to align txw entries */ |
Marek Vasut | 1994c90 | 2020-03-25 17:18:55 +0100 | [diff] [blame] | 420 | txw[0] = 0; |
| 421 | txw[1] = cpu_to_le16(len); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 422 | |
| 423 | /* 1. set sudo-DMA mode */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 424 | ks_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI); |
| 425 | ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL | RXQCR_SDA); |
Marek Vasut | e457cb1 | 2020-03-25 17:25:29 +0100 | [diff] [blame] | 426 | /* 2. write status/length info */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 427 | ks_outblk(ks, txw, 4); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 428 | /* 3. write pkt data */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 429 | ks_outblk(ks, (u16 *)pdata, ALIGN(len, 4)); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 430 | /* 4. reset sudo-DMA mode */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 431 | ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 432 | /* 5. Enqueue Tx(move the pkt from TX buffer into TXQ) */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 433 | ks_wrreg16(ks, KS_TXQCR, TXQCR_METFE); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 434 | /* 6. wait until TXQCR_METFE is auto-cleared */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 435 | do { } while (ks_rdreg16(ks, KS_TXQCR) & TXQCR_METFE); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 436 | } |
| 437 | |
Marek Vasut | c0bb0d9 | 2020-03-25 17:54:45 +0100 | [diff] [blame] | 438 | static int ks8851_mll_send_common(struct ks_net *ks, void *packet, int length) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 439 | { |
| 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 Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 448 | retv = ks_rdreg16(ks, KS_TXMIR) & 0x1fff; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 449 | if (retv >= tmplen + 12) { |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 450 | ks_write_qmu(ks, data, tmplen); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 451 | return 0; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 452 | } |
Marek Vasut | e457cb1 | 2020-03-25 17:25:29 +0100 | [diff] [blame] | 453 | |
| 454 | printf(DRIVERNAME ": failed to send packet: No buffer\n"); |
| 455 | return -1; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 456 | } |
| 457 | |
Marek Vasut | c0bb0d9 | 2020-03-25 17:54:45 +0100 | [diff] [blame] | 458 | static void ks8851_mll_halt_common(struct ks_net *ks) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 459 | { |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 460 | ks8851_mll_reset(ks); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 461 | } |
| 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 Vasut | 5347ab6 | 2020-03-25 18:47:10 +0100 | [diff] [blame] | 469 | static int ks8851_mll_recv_common(struct ks_net *ks, uchar *data) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 470 | { |
| 471 | u16 status; |
Marek Vasut | 5347ab6 | 2020-03-25 18:47:10 +0100 | [diff] [blame] | 472 | int ret = 0; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 473 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 474 | status = ks_rdreg16(ks, KS_ISR); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 475 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 476 | ks_wrreg16(ks, KS_ISR, status); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 477 | |
Marek Vasut | 5347ab6 | 2020-03-25 18:47:10 +0100 | [diff] [blame] | 478 | if (ks->rxfc || (status & IRQ_RXI)) |
| 479 | ret = ks_rcv(ks, data); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 480 | |
Marek Vasut | e457cb1 | 2020-03-25 17:25:29 +0100 | [diff] [blame] | 481 | if (status & IRQ_LDI) { |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 482 | u16 pmecr = ks_rdreg16(ks, KS_PMECR); |
Marek Vasut | e457cb1 | 2020-03-25 17:25:29 +0100 | [diff] [blame] | 483 | |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 484 | pmecr &= ~PMECR_WKEVT_MASK; |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 485 | ks_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 486 | } |
| 487 | |
Marek Vasut | 5347ab6 | 2020-03-25 18:47:10 +0100 | [diff] [blame] | 488 | return ret; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 489 | } |
| 490 | |
Marek Vasut | c0bb0d9 | 2020-03-25 17:54:45 +0100 | [diff] [blame] | 491 | static void ks8851_mll_write_hwaddr_common(struct ks_net *ks, u8 enetaddr[6]) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 492 | { |
| 493 | u16 addrl, addrm, addrh; |
| 494 | |
Marek Vasut | c0bb0d9 | 2020-03-25 17:54:45 +0100 | [diff] [blame] | 495 | addrh = (enetaddr[0] << 8) | enetaddr[1]; |
| 496 | addrm = (enetaddr[2] << 8) | enetaddr[3]; |
| 497 | addrl = (enetaddr[4] << 8) | enetaddr[5]; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 498 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 499 | ks_wrreg16(ks, KS_MARH, addrh); |
| 500 | ks_wrreg16(ks, KS_MARM, addrm); |
| 501 | ks_wrreg16(ks, KS_MARL, addrl); |
Marek Vasut | c0bb0d9 | 2020-03-25 17:54:45 +0100 | [diff] [blame] | 502 | } |
| 503 | |
Marek Vasut | f5d054b | 2020-03-25 18:00:35 +0100 | [diff] [blame] | 504 | static 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 | |
| 511 | static 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 | |
| 518 | static 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 | |
| 528 | static 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 | |
| 541 | static int ks8851_write_hwaddr(struct udevice *dev) |
| 542 | { |
| 543 | struct ks_net *ks = dev_get_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 544 | struct eth_pdata *pdata = dev_get_plat(dev); |
Marek Vasut | f5d054b | 2020-03-25 18:00:35 +0100 | [diff] [blame] | 545 | |
| 546 | ks8851_mll_write_hwaddr_common(ks, pdata->enetaddr); |
| 547 | |
| 548 | return 0; |
| 549 | } |
| 550 | |
Marek Vasut | 8530bb4 | 2020-10-08 15:14:17 +0200 | [diff] [blame] | 551 | static int ks8851_read_rom_hwaddr(struct udevice *dev) |
| 552 | { |
| 553 | struct ks_net *ks = dev_get_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 554 | struct eth_pdata *pdata = dev_get_plat(dev); |
Marek Vasut | 8530bb4 | 2020-10-08 15:14:17 +0200 | [diff] [blame] | 555 | 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 Vasut | f5d054b | 2020-03-25 18:00:35 +0100 | [diff] [blame] | 579 | static int ks8851_bind(struct udevice *dev) |
| 580 | { |
| 581 | return device_set_name(dev, dev->name); |
| 582 | } |
| 583 | |
| 584 | static 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 Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 594 | static int ks8851_of_to_plat(struct udevice *dev) |
Marek Vasut | f5d054b | 2020-03-25 18:00:35 +0100 | [diff] [blame] | 595 | { |
| 596 | struct ks_net *ks = dev_get_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 597 | struct eth_pdata *pdata = dev_get_plat(dev); |
Marek Vasut | f5d054b | 2020-03-25 18:00:35 +0100 | [diff] [blame] | 598 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 599 | pdata->iobase = dev_read_addr(dev); |
Marek Vasut | f5d054b | 2020-03-25 18:00:35 +0100 | [diff] [blame] | 600 | ks->iobase = pdata->iobase; |
| 601 | |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | static 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 Vasut | 8530bb4 | 2020-10-08 15:14:17 +0200 | [diff] [blame] | 611 | .read_rom_hwaddr = ks8851_read_rom_hwaddr, |
Marek Vasut | f5d054b | 2020-03-25 18:00:35 +0100 | [diff] [blame] | 612 | }; |
| 613 | |
| 614 | static const struct udevice_id ks8851_ids[] = { |
| 615 | { .compatible = "micrel,ks8851-mll" }, |
| 616 | { } |
| 617 | }; |
| 618 | |
| 619 | U_BOOT_DRIVER(ks8851) = { |
| 620 | .name = "eth_ks8851", |
| 621 | .id = UCLASS_ETH, |
| 622 | .of_match = ks8851_ids, |
| 623 | .bind = ks8851_bind, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 624 | .of_to_plat = ks8851_of_to_plat, |
Marek Vasut | f5d054b | 2020-03-25 18:00:35 +0100 | [diff] [blame] | 625 | .probe = ks8851_probe, |
| 626 | .ops = &ks8851_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 627 | .priv_auto = sizeof(struct ks_net), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 628 | .plat_auto = sizeof(struct eth_pdata), |
Marek Vasut | f5d054b | 2020-03-25 18:00:35 +0100 | [diff] [blame] | 629 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 630 | }; |