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> |
| 9 | #include <common.h> |
| 10 | #include <command.h> |
| 11 | #include <malloc.h> |
| 12 | #include <net.h> |
| 13 | #include <miiphy.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 14 | #include <linux/delay.h> |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 15 | |
| 16 | #include "ks8851_mll.h" |
| 17 | |
| 18 | #define DRIVERNAME "ks8851_mll" |
| 19 | |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 20 | #define RX_BUF_SIZE 2000 |
| 21 | |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 22 | /* |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 23 | * struct ks_net - KS8851 driver private data |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 24 | * @dev : legacy non-DM ethernet device structure |
| 25 | * @iobase : register base |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 26 | * @bus_width : i/o bus width. |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 27 | * @sharedbus : Multipex(addr and data bus) mode indicator. |
Marek Vasut | 21bf578 | 2020-03-25 17:23:11 +0100 | [diff] [blame] | 28 | * @extra_byte : number of extra byte prepended rx pkt. |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 29 | */ |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 30 | struct ks_net { |
Marek Vasut | f5d054b | 2020-03-25 18:00:35 +0100 | [diff] [blame] | 31 | #ifndef CONFIG_DM_ETH |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 32 | struct eth_device dev; |
Marek Vasut | f5d054b | 2020-03-25 18:00:35 +0100 | [diff] [blame] | 33 | #endif |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 34 | phys_addr_t iobase; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 35 | int bus_width; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 36 | u16 sharedbus; |
Marek Vasut | 5347ab6 | 2020-03-25 18:47:10 +0100 | [diff] [blame] | 37 | u16 rxfc; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 38 | u8 extra_byte; |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 39 | }; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 40 | |
| 41 | #define BE3 0x8000 /* Byte Enable 3 */ |
| 42 | #define BE2 0x4000 /* Byte Enable 2 */ |
| 43 | #define BE1 0x2000 /* Byte Enable 1 */ |
| 44 | #define BE0 0x1000 /* Byte Enable 0 */ |
| 45 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 46 | static u8 ks_rdreg8(struct ks_net *ks, u16 offset) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 47 | { |
| 48 | u8 shift_bit = offset & 0x03; |
| 49 | u8 shift_data = (offset & 1) << 3; |
| 50 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 51 | writew(offset | (BE0 << shift_bit), ks->iobase + 2); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 52 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 53 | return (u8)(readw(ks->iobase) >> shift_data); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 54 | } |
| 55 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 56 | static u16 ks_rdreg16(struct ks_net *ks, u16 offset) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 57 | { |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 58 | writew(offset | ((BE1 | BE0) << (offset & 0x02)), ks->iobase + 2); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 59 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 60 | return readw(ks->iobase); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 61 | } |
| 62 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 63 | static void ks_wrreg16(struct ks_net *ks, u16 offset, u16 val) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 64 | { |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 65 | writew(offset | ((BE1 | BE0) << (offset & 0x02)), ks->iobase + 2); |
| 66 | writew(val, ks->iobase); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /* |
| 70 | * ks_inblk - read a block of data from QMU. This is called after sudo DMA mode |
| 71 | * enabled. |
| 72 | * @ks: The chip state |
| 73 | * @wptr: buffer address to save data |
| 74 | * @len: length in byte to read |
| 75 | */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 76 | 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] | 77 | { |
| 78 | len >>= 1; |
| 79 | |
| 80 | while (len--) |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 81 | *wptr++ = readw(ks->iobase); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /* |
| 85 | * ks_outblk - write data to QMU. This is called after sudo DMA mode enabled. |
| 86 | * @ks: The chip information |
| 87 | * @wptr: buffer address |
| 88 | * @len: length in byte to write |
| 89 | */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 90 | 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] | 91 | { |
| 92 | len >>= 1; |
| 93 | |
| 94 | while (len--) |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 95 | writew(*wptr++, ks->iobase); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 96 | } |
| 97 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 98 | static void ks_enable_int(struct ks_net *ks) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 99 | { |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 100 | ks_wrreg16(ks, KS_IER, IRQ_LCI | IRQ_TXI | IRQ_RXI); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 101 | } |
| 102 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 103 | static void ks_set_powermode(struct ks_net *ks, unsigned int pwrmode) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 104 | { |
Marek Vasut | e457cb1 | 2020-03-25 17:25:29 +0100 | [diff] [blame] | 105 | unsigned int pmecr; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 106 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 107 | ks_rdreg16(ks, KS_GRR); |
| 108 | pmecr = ks_rdreg16(ks, KS_PMECR); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 109 | pmecr &= ~PMECR_PM_MASK; |
| 110 | pmecr |= pwrmode; |
| 111 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 112 | ks_wrreg16(ks, KS_PMECR, pmecr); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /* |
| 116 | * ks_read_config - read chip configuration of bus width. |
| 117 | * @ks: The chip information |
| 118 | */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 119 | static void ks_read_config(struct ks_net *ks) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 120 | { |
| 121 | u16 reg_data = 0; |
| 122 | |
| 123 | /* Regardless of bus width, 8 bit read should always work. */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 124 | reg_data = ks_rdreg8(ks, KS_CCR) & 0x00FF; |
| 125 | reg_data |= ks_rdreg8(ks, KS_CCR + 1) << 8; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 126 | |
| 127 | /* addr/data bus are multiplexed */ |
| 128 | ks->sharedbus = (reg_data & CCR_SHARED) == CCR_SHARED; |
| 129 | |
| 130 | /* |
| 131 | * There are garbage data when reading data from QMU, |
| 132 | * depending on bus-width. |
| 133 | */ |
| 134 | if (reg_data & CCR_8BIT) { |
| 135 | ks->bus_width = ENUM_BUS_8BIT; |
| 136 | ks->extra_byte = 1; |
| 137 | } else if (reg_data & CCR_16BIT) { |
| 138 | ks->bus_width = ENUM_BUS_16BIT; |
| 139 | ks->extra_byte = 2; |
| 140 | } else { |
| 141 | ks->bus_width = ENUM_BUS_32BIT; |
| 142 | ks->extra_byte = 4; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * ks_soft_reset - issue one of the soft reset to the device |
| 148 | * @ks: The device state. |
| 149 | * @op: The bit(s) to set in the GRR |
| 150 | * |
| 151 | * Issue the relevant soft-reset command to the device's GRR register |
| 152 | * specified by @op. |
| 153 | * |
| 154 | * Note, the delays are in there as a caution to ensure that the reset |
| 155 | * has time to take effect and then complete. Since the datasheet does |
| 156 | * not currently specify the exact sequence, we have chosen something |
| 157 | * that seems to work with our device. |
| 158 | */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 159 | static void ks_soft_reset(struct ks_net *ks, unsigned int op) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 160 | { |
| 161 | /* Disable interrupt first */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 162 | ks_wrreg16(ks, KS_IER, 0x0000); |
| 163 | ks_wrreg16(ks, KS_GRR, op); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 164 | mdelay(10); /* wait a short time to effect reset */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 165 | ks_wrreg16(ks, KS_GRR, 0); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 166 | mdelay(1); /* wait for condition to clear */ |
| 167 | } |
| 168 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 169 | void ks_enable_qmu(struct ks_net *ks) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 170 | { |
| 171 | u16 w; |
| 172 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 173 | w = ks_rdreg16(ks, KS_TXCR); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 174 | |
| 175 | /* Enables QMU Transmit (TXCR). */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 176 | ks_wrreg16(ks, KS_TXCR, w | TXCR_TXE); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 177 | |
| 178 | /* Enable RX Frame Count Threshold and Auto-Dequeue RXQ Frame */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 179 | w = ks_rdreg16(ks, KS_RXQCR); |
| 180 | ks_wrreg16(ks, KS_RXQCR, w | RXQCR_RXFCTE); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 181 | |
| 182 | /* Enables QMU Receive (RXCR1). */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 183 | w = ks_rdreg16(ks, KS_RXCR1); |
| 184 | ks_wrreg16(ks, KS_RXCR1, w | RXCR1_RXE); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 185 | } |
| 186 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 187 | static void ks_disable_qmu(struct ks_net *ks) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 188 | { |
| 189 | u16 w; |
| 190 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 191 | w = ks_rdreg16(ks, KS_TXCR); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 192 | |
| 193 | /* Disables QMU Transmit (TXCR). */ |
| 194 | w &= ~TXCR_TXE; |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 195 | ks_wrreg16(ks, KS_TXCR, w); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 196 | |
| 197 | /* Disables QMU Receive (RXCR1). */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 198 | w = ks_rdreg16(ks, KS_RXCR1); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 199 | w &= ~RXCR1_RXE; |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 200 | ks_wrreg16(ks, KS_RXCR1, w); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 201 | } |
| 202 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 203 | 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] | 204 | { |
| 205 | u32 r = ks->extra_byte & 0x1; |
| 206 | u32 w = ks->extra_byte - r; |
| 207 | |
| 208 | /* 1. set sudo DMA mode */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 209 | ks_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI); |
| 210 | ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL | RXQCR_SDA); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 211 | |
| 212 | /* |
| 213 | * 2. read prepend data |
| 214 | * |
| 215 | * read 4 + extra bytes and discard them. |
| 216 | * extra bytes for dummy, 2 for status, 2 for len |
| 217 | */ |
| 218 | |
| 219 | if (r) |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 220 | ks_rdreg8(ks, 0); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 221 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 222 | ks_inblk(ks, buf, w + 2 + 2); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 223 | |
| 224 | /* 3. read pkt data */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 225 | ks_inblk(ks, buf, ALIGN(len, 4)); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 226 | |
| 227 | /* 4. reset sudo DMA Mode */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 228 | ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 229 | } |
| 230 | |
Marek Vasut | 5347ab6 | 2020-03-25 18:47:10 +0100 | [diff] [blame] | 231 | static int ks_rcv(struct ks_net *ks, uchar *data) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 232 | { |
Marek Vasut | 21bf578 | 2020-03-25 17:23:11 +0100 | [diff] [blame] | 233 | u16 sts, len; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 234 | |
Marek Vasut | 5347ab6 | 2020-03-25 18:47:10 +0100 | [diff] [blame] | 235 | if (!ks->rxfc) |
| 236 | ks->rxfc = ks_rdreg16(ks, KS_RXFCTR) >> 8; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 237 | |
Marek Vasut | 5347ab6 | 2020-03-25 18:47:10 +0100 | [diff] [blame] | 238 | if (!ks->rxfc) |
| 239 | return 0; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 240 | |
Marek Vasut | 5347ab6 | 2020-03-25 18:47:10 +0100 | [diff] [blame] | 241 | /* Checking Received packet status */ |
| 242 | sts = ks_rdreg16(ks, KS_RXFHSR); |
| 243 | /* Get packet len from hardware */ |
| 244 | len = ks_rdreg16(ks, KS_RXFHBCR); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 245 | |
Marek Vasut | 5347ab6 | 2020-03-25 18:47:10 +0100 | [diff] [blame] | 246 | if ((sts & RXFSHR_RXFV) && len && (len < RX_BUF_SIZE)) { |
| 247 | /* read data block including CRC 4 bytes */ |
| 248 | ks_read_qmu(ks, (u16 *)data, len); |
| 249 | ks->rxfc--; |
| 250 | return len - 4; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 251 | } |
Marek Vasut | 5347ab6 | 2020-03-25 18:47:10 +0100 | [diff] [blame] | 252 | |
| 253 | ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL | RXQCR_RRXEF); |
Marek Vasut | e034d88 | 2021-01-06 15:16:01 +0100 | [diff] [blame] | 254 | printf(DRIVERNAME ": bad packet (sts=0x%04x len=0x%04x)\n", sts, len); |
| 255 | ks->rxfc = 0; |
Marek Vasut | 5347ab6 | 2020-03-25 18:47:10 +0100 | [diff] [blame] | 256 | return 0; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /* |
| 260 | * ks_read_selftest - read the selftest memory info. |
| 261 | * @ks: The device state |
| 262 | * |
| 263 | * Read and check the TX/RX memory selftest information. |
| 264 | */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 265 | static int ks_read_selftest(struct ks_net *ks) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 266 | { |
| 267 | u16 both_done = MBIR_TXMBF | MBIR_RXMBF; |
| 268 | u16 mbir; |
| 269 | int ret = 0; |
| 270 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 271 | mbir = ks_rdreg16(ks, KS_MBIR); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 272 | |
| 273 | if ((mbir & both_done) != both_done) { |
| 274 | printf(DRIVERNAME ": Memory selftest not finished\n"); |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | if (mbir & MBIR_TXMBFA) { |
| 279 | printf(DRIVERNAME ": TX memory selftest fails\n"); |
| 280 | ret |= 1; |
| 281 | } |
| 282 | |
| 283 | if (mbir & MBIR_RXMBFA) { |
| 284 | printf(DRIVERNAME ": RX memory selftest fails\n"); |
| 285 | ret |= 2; |
| 286 | } |
| 287 | |
| 288 | debug(DRIVERNAME ": the selftest passes\n"); |
| 289 | |
| 290 | return ret; |
| 291 | } |
| 292 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 293 | static void ks_setup(struct ks_net *ks) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 294 | { |
| 295 | u16 w; |
| 296 | |
| 297 | /* Setup Transmit Frame Data Pointer Auto-Increment (TXFDPR) */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 298 | ks_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 299 | |
| 300 | /* Setup Receive Frame Data Pointer Auto-Increment */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 301 | ks_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 302 | |
| 303 | /* Setup Receive Frame Threshold - 1 frame (RXFCTFC) */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 304 | ks_wrreg16(ks, KS_RXFCTR, 1 & RXFCTR_THRESHOLD_MASK); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 305 | |
| 306 | /* Setup RxQ Command Control (RXQCR) */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 307 | ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 308 | |
| 309 | /* |
| 310 | * set the force mode to half duplex, default is full duplex |
| 311 | * because if the auto-negotiation fails, most switch uses |
| 312 | * half-duplex. |
| 313 | */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 314 | w = ks_rdreg16(ks, KS_P1MBCR); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 315 | w &= ~P1MBCR_FORCE_FDX; |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 316 | ks_wrreg16(ks, KS_P1MBCR, w); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 317 | |
| 318 | w = TXCR_TXFCE | TXCR_TXPE | TXCR_TXCRC | TXCR_TCGIP; |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 319 | ks_wrreg16(ks, KS_TXCR, w); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 320 | |
| 321 | w = RXCR1_RXFCE | RXCR1_RXBE | RXCR1_RXUE | RXCR1_RXME | RXCR1_RXIPFCC; |
| 322 | |
| 323 | /* Normal mode */ |
| 324 | w |= RXCR1_RXPAFMA; |
| 325 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 326 | ks_wrreg16(ks, KS_RXCR1, w); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 327 | } |
| 328 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 329 | static void ks_setup_int(struct ks_net *ks) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 330 | { |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 331 | /* Clear the interrupts status of the hardware. */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 332 | ks_wrreg16(ks, KS_ISR, 0xffff); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 333 | } |
| 334 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 335 | static int ks8851_mll_detect_chip(struct ks_net *ks) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 336 | { |
Marek Vasut | b4ba60b | 2020-03-25 18:15:46 +0100 | [diff] [blame] | 337 | unsigned short val; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 338 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 339 | ks_read_config(ks); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 340 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 341 | val = ks_rdreg16(ks, KS_CIDER); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 342 | |
| 343 | if (val == 0xffff) { |
| 344 | /* Special case -- no chip present */ |
| 345 | printf(DRIVERNAME ": is chip mounted ?\n"); |
| 346 | return -1; |
| 347 | } else if ((val & 0xfff0) != CIDER_ID) { |
| 348 | printf(DRIVERNAME ": Invalid chip id 0x%04x\n", val); |
| 349 | return -1; |
| 350 | } |
| 351 | |
| 352 | debug("Read back KS8851 id 0x%x\n", val); |
| 353 | |
Marek Vasut | b4ba60b | 2020-03-25 18:15:46 +0100 | [diff] [blame] | 354 | if ((val & 0xfff0) != CIDER_ID) { |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 355 | printf(DRIVERNAME ": Unknown chip ID %04x\n", val); |
| 356 | return -1; |
| 357 | } |
| 358 | |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 359 | return 0; |
| 360 | } |
| 361 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 362 | static void ks8851_mll_reset(struct ks_net *ks) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 363 | { |
| 364 | /* wake up powermode to normal mode */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 365 | ks_set_powermode(ks, PMECR_PM_NORMAL); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 366 | mdelay(1); /* wait for normal mode to take effect */ |
| 367 | |
| 368 | /* Disable interrupt and reset */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 369 | ks_soft_reset(ks, GRR_GSR); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 370 | |
| 371 | /* turn off the IRQs and ack any outstanding */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 372 | ks_wrreg16(ks, KS_IER, 0x0000); |
| 373 | ks_wrreg16(ks, KS_ISR, 0xffff); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 374 | |
| 375 | /* shutdown RX/TX QMU */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 376 | ks_disable_qmu(ks); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 377 | } |
| 378 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 379 | static void ks8851_mll_phy_configure(struct ks_net *ks) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 380 | { |
| 381 | u16 data; |
| 382 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 383 | ks_setup(ks); |
| 384 | ks_setup_int(ks); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 385 | |
| 386 | /* Probing the phy */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 387 | data = ks_rdreg16(ks, KS_OBCR); |
| 388 | ks_wrreg16(ks, KS_OBCR, data | OBCR_ODS_16MA); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 389 | |
| 390 | debug(DRIVERNAME ": phy initialized\n"); |
| 391 | } |
| 392 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 393 | static void ks8851_mll_enable(struct ks_net *ks) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 394 | { |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 395 | ks_wrreg16(ks, KS_ISR, 0xffff); |
| 396 | ks_enable_int(ks); |
| 397 | ks_enable_qmu(ks); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 398 | } |
| 399 | |
Marek Vasut | c0bb0d9 | 2020-03-25 17:54:45 +0100 | [diff] [blame] | 400 | static int ks8851_mll_init_common(struct ks_net *ks) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 401 | { |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 402 | if (ks_read_selftest(ks)) { |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 403 | printf(DRIVERNAME ": Selftest failed\n"); |
| 404 | return -1; |
| 405 | } |
| 406 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 407 | ks8851_mll_reset(ks); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 408 | |
| 409 | /* Configure the PHY, initialize the link state */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 410 | ks8851_mll_phy_configure(ks); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 411 | |
Marek Vasut | 5347ab6 | 2020-03-25 18:47:10 +0100 | [diff] [blame] | 412 | ks->rxfc = 0; |
| 413 | |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 414 | /* Turn on Tx + Rx */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 415 | ks8851_mll_enable(ks); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 420 | 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] | 421 | { |
Marek Vasut | 1994c90 | 2020-03-25 17:18:55 +0100 | [diff] [blame] | 422 | __le16 txw[2]; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 423 | /* start header at txb[0] to align txw entries */ |
Marek Vasut | 1994c90 | 2020-03-25 17:18:55 +0100 | [diff] [blame] | 424 | txw[0] = 0; |
| 425 | txw[1] = cpu_to_le16(len); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 426 | |
| 427 | /* 1. set sudo-DMA mode */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 428 | ks_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI); |
| 429 | ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL | RXQCR_SDA); |
Marek Vasut | e457cb1 | 2020-03-25 17:25:29 +0100 | [diff] [blame] | 430 | /* 2. write status/length info */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 431 | ks_outblk(ks, txw, 4); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 432 | /* 3. write pkt data */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 433 | ks_outblk(ks, (u16 *)pdata, ALIGN(len, 4)); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 434 | /* 4. reset sudo-DMA mode */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 435 | ks_wrreg16(ks, KS_RXQCR, RXQCR_CMD_CNTL); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 436 | /* 5. Enqueue Tx(move the pkt from TX buffer into TXQ) */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 437 | ks_wrreg16(ks, KS_TXQCR, TXQCR_METFE); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 438 | /* 6. wait until TXQCR_METFE is auto-cleared */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 439 | do { } while (ks_rdreg16(ks, KS_TXQCR) & TXQCR_METFE); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 440 | } |
| 441 | |
Marek Vasut | c0bb0d9 | 2020-03-25 17:54:45 +0100 | [diff] [blame] | 442 | 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] | 443 | { |
| 444 | u8 *data = (u8 *)packet; |
| 445 | u16 tmplen = (u16)length; |
| 446 | u16 retv; |
| 447 | |
| 448 | /* |
| 449 | * Extra space are required: |
| 450 | * 4 byte for alignment, 4 for status/length, 4 for CRC |
| 451 | */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 452 | retv = ks_rdreg16(ks, KS_TXMIR) & 0x1fff; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 453 | if (retv >= tmplen + 12) { |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 454 | ks_write_qmu(ks, data, tmplen); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 455 | return 0; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 456 | } |
Marek Vasut | e457cb1 | 2020-03-25 17:25:29 +0100 | [diff] [blame] | 457 | |
| 458 | printf(DRIVERNAME ": failed to send packet: No buffer\n"); |
| 459 | return -1; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 460 | } |
| 461 | |
Marek Vasut | c0bb0d9 | 2020-03-25 17:54:45 +0100 | [diff] [blame] | 462 | static void ks8851_mll_halt_common(struct ks_net *ks) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 463 | { |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 464 | ks8851_mll_reset(ks); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | /* |
| 468 | * Maximum receive ring size; that is, the number of packets |
| 469 | * we can buffer before overflow happens. Basically, this just |
| 470 | * needs to be enough to prevent a packet being discarded while |
| 471 | * we are processing the previous one. |
| 472 | */ |
Marek Vasut | 5347ab6 | 2020-03-25 18:47:10 +0100 | [diff] [blame] | 473 | static int ks8851_mll_recv_common(struct ks_net *ks, uchar *data) |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 474 | { |
| 475 | u16 status; |
Marek Vasut | 5347ab6 | 2020-03-25 18:47:10 +0100 | [diff] [blame] | 476 | int ret = 0; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 477 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 478 | status = ks_rdreg16(ks, KS_ISR); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 479 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 480 | ks_wrreg16(ks, KS_ISR, status); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 481 | |
Marek Vasut | 5347ab6 | 2020-03-25 18:47:10 +0100 | [diff] [blame] | 482 | if (ks->rxfc || (status & IRQ_RXI)) |
| 483 | ret = ks_rcv(ks, data); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 484 | |
Marek Vasut | e457cb1 | 2020-03-25 17:25:29 +0100 | [diff] [blame] | 485 | if (status & IRQ_LDI) { |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 486 | u16 pmecr = ks_rdreg16(ks, KS_PMECR); |
Marek Vasut | e457cb1 | 2020-03-25 17:25:29 +0100 | [diff] [blame] | 487 | |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 488 | pmecr &= ~PMECR_WKEVT_MASK; |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 489 | ks_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 490 | } |
| 491 | |
Marek Vasut | 5347ab6 | 2020-03-25 18:47:10 +0100 | [diff] [blame] | 492 | return ret; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 493 | } |
| 494 | |
Marek Vasut | c0bb0d9 | 2020-03-25 17:54:45 +0100 | [diff] [blame] | 495 | 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] | 496 | { |
| 497 | u16 addrl, addrm, addrh; |
| 498 | |
Marek Vasut | c0bb0d9 | 2020-03-25 17:54:45 +0100 | [diff] [blame] | 499 | addrh = (enetaddr[0] << 8) | enetaddr[1]; |
| 500 | addrm = (enetaddr[2] << 8) | enetaddr[3]; |
| 501 | addrl = (enetaddr[4] << 8) | enetaddr[5]; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 502 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 503 | ks_wrreg16(ks, KS_MARH, addrh); |
| 504 | ks_wrreg16(ks, KS_MARM, addrm); |
| 505 | ks_wrreg16(ks, KS_MARL, addrl); |
Marek Vasut | c0bb0d9 | 2020-03-25 17:54:45 +0100 | [diff] [blame] | 506 | } |
| 507 | |
Marek Vasut | f5d054b | 2020-03-25 18:00:35 +0100 | [diff] [blame] | 508 | #ifndef CONFIG_DM_ETH |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 509 | static int ks8851_mll_init(struct eth_device *dev, struct bd_info *bd) |
Marek Vasut | c0bb0d9 | 2020-03-25 17:54:45 +0100 | [diff] [blame] | 510 | { |
| 511 | struct ks_net *ks = container_of(dev, struct ks_net, dev); |
| 512 | |
| 513 | return ks8851_mll_init_common(ks); |
| 514 | } |
| 515 | |
| 516 | static void ks8851_mll_halt(struct eth_device *dev) |
| 517 | { |
| 518 | struct ks_net *ks = container_of(dev, struct ks_net, dev); |
| 519 | |
| 520 | ks8851_mll_halt_common(ks); |
| 521 | } |
| 522 | |
| 523 | static int ks8851_mll_send(struct eth_device *dev, void *packet, int length) |
| 524 | { |
| 525 | struct ks_net *ks = container_of(dev, struct ks_net, dev); |
| 526 | |
| 527 | return ks8851_mll_send_common(ks, packet, length); |
| 528 | } |
| 529 | |
| 530 | static int ks8851_mll_recv(struct eth_device *dev) |
| 531 | { |
| 532 | struct ks_net *ks = container_of(dev, struct ks_net, dev); |
Marek Vasut | 5347ab6 | 2020-03-25 18:47:10 +0100 | [diff] [blame] | 533 | int ret; |
Marek Vasut | c0bb0d9 | 2020-03-25 17:54:45 +0100 | [diff] [blame] | 534 | |
Marek Vasut | 5347ab6 | 2020-03-25 18:47:10 +0100 | [diff] [blame] | 535 | ret = ks8851_mll_recv_common(ks, net_rx_packets[0]); |
| 536 | if (ret) |
| 537 | net_process_received_packet(net_rx_packets[0], ret); |
| 538 | |
| 539 | return ret; |
Marek Vasut | c0bb0d9 | 2020-03-25 17:54:45 +0100 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | static int ks8851_mll_write_hwaddr(struct eth_device *dev) |
| 543 | { |
| 544 | struct ks_net *ks = container_of(dev, struct ks_net, dev); |
| 545 | |
| 546 | ks8851_mll_write_hwaddr_common(ks, ks->dev.enetaddr); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 547 | |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | int ks8851_mll_initialize(u8 dev_num, int base_addr) |
| 552 | { |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 553 | struct ks_net *ks; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 554 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 555 | ks = calloc(1, sizeof(*ks)); |
| 556 | if (!ks) |
Marek Vasut | 033a879 | 2020-03-25 16:52:38 +0100 | [diff] [blame] | 557 | return -ENOMEM; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 558 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 559 | ks->iobase = base_addr; |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 560 | |
| 561 | /* Try to detect chip. Will fail if not present. */ |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 562 | if (ks8851_mll_detect_chip(ks)) { |
| 563 | free(ks); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 564 | return -1; |
| 565 | } |
| 566 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 567 | ks->dev.init = ks8851_mll_init; |
| 568 | ks->dev.halt = ks8851_mll_halt; |
| 569 | ks->dev.send = ks8851_mll_send; |
| 570 | ks->dev.recv = ks8851_mll_recv; |
| 571 | ks->dev.write_hwaddr = ks8851_mll_write_hwaddr; |
| 572 | sprintf(ks->dev.name, "%s-%hu", DRIVERNAME, dev_num); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 573 | |
Marek Vasut | 1e1693e | 2020-03-25 17:35:00 +0100 | [diff] [blame] | 574 | eth_register(&ks->dev); |
Roberto Cerati | b1eee65 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 575 | |
| 576 | return 0; |
| 577 | } |
Marek Vasut | f5d054b | 2020-03-25 18:00:35 +0100 | [diff] [blame] | 578 | #else /* ifdef CONFIG_DM_ETH */ |
| 579 | static int ks8851_start(struct udevice *dev) |
| 580 | { |
| 581 | struct ks_net *ks = dev_get_priv(dev); |
| 582 | |
| 583 | return ks8851_mll_init_common(ks); |
| 584 | } |
| 585 | |
| 586 | static void ks8851_stop(struct udevice *dev) |
| 587 | { |
| 588 | struct ks_net *ks = dev_get_priv(dev); |
| 589 | |
| 590 | ks8851_mll_halt_common(ks); |
| 591 | } |
| 592 | |
| 593 | static int ks8851_send(struct udevice *dev, void *packet, int length) |
| 594 | { |
| 595 | struct ks_net *ks = dev_get_priv(dev); |
| 596 | int ret; |
| 597 | |
| 598 | ret = ks8851_mll_send_common(ks, packet, length); |
| 599 | |
| 600 | return ret ? 0 : -ETIMEDOUT; |
| 601 | } |
| 602 | |
| 603 | static int ks8851_recv(struct udevice *dev, int flags, uchar **packetp) |
| 604 | { |
| 605 | struct ks_net *ks = dev_get_priv(dev); |
| 606 | uchar *data = net_rx_packets[0]; |
| 607 | int ret; |
| 608 | |
| 609 | ret = ks8851_mll_recv_common(ks, data); |
| 610 | if (ret) |
| 611 | *packetp = (void *)data; |
| 612 | |
| 613 | return ret ? ret : -EAGAIN; |
| 614 | } |
| 615 | |
| 616 | static int ks8851_write_hwaddr(struct udevice *dev) |
| 617 | { |
| 618 | struct ks_net *ks = dev_get_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 619 | struct eth_pdata *pdata = dev_get_plat(dev); |
Marek Vasut | f5d054b | 2020-03-25 18:00:35 +0100 | [diff] [blame] | 620 | |
| 621 | ks8851_mll_write_hwaddr_common(ks, pdata->enetaddr); |
| 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | |
Marek Vasut | 8530bb4 | 2020-10-08 15:14:17 +0200 | [diff] [blame] | 626 | static int ks8851_read_rom_hwaddr(struct udevice *dev) |
| 627 | { |
| 628 | struct ks_net *ks = dev_get_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 629 | struct eth_pdata *pdata = dev_get_plat(dev); |
Marek Vasut | 8530bb4 | 2020-10-08 15:14:17 +0200 | [diff] [blame] | 630 | u16 addrl, addrm, addrh; |
| 631 | |
| 632 | /* No EEPROM means no valid MAC address. */ |
| 633 | if (!(ks_rdreg16(ks, KS_CCR) & CCR_EEPROM)) |
| 634 | return -EINVAL; |
| 635 | |
| 636 | /* |
| 637 | * If the EEPROM contains valid MAC address, it is loaded into |
| 638 | * the NIC on power on. Read the MAC out of the NIC registers. |
| 639 | */ |
| 640 | addrl = ks_rdreg16(ks, KS_MARL); |
| 641 | addrm = ks_rdreg16(ks, KS_MARM); |
| 642 | addrh = ks_rdreg16(ks, KS_MARH); |
| 643 | |
| 644 | pdata->enetaddr[0] = (addrh >> 8) & 0xff; |
| 645 | pdata->enetaddr[1] = addrh & 0xff; |
| 646 | pdata->enetaddr[2] = (addrm >> 8) & 0xff; |
| 647 | pdata->enetaddr[3] = addrm & 0xff; |
| 648 | pdata->enetaddr[4] = (addrl >> 8) & 0xff; |
| 649 | pdata->enetaddr[5] = addrl & 0xff; |
| 650 | |
| 651 | return !is_valid_ethaddr(pdata->enetaddr); |
| 652 | } |
| 653 | |
Marek Vasut | f5d054b | 2020-03-25 18:00:35 +0100 | [diff] [blame] | 654 | static int ks8851_bind(struct udevice *dev) |
| 655 | { |
| 656 | return device_set_name(dev, dev->name); |
| 657 | } |
| 658 | |
| 659 | static int ks8851_probe(struct udevice *dev) |
| 660 | { |
| 661 | struct ks_net *ks = dev_get_priv(dev); |
| 662 | |
| 663 | /* Try to detect chip. Will fail if not present. */ |
| 664 | ks8851_mll_detect_chip(ks); |
| 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 669 | static int ks8851_of_to_plat(struct udevice *dev) |
Marek Vasut | f5d054b | 2020-03-25 18:00:35 +0100 | [diff] [blame] | 670 | { |
| 671 | struct ks_net *ks = dev_get_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 672 | struct eth_pdata *pdata = dev_get_plat(dev); |
Marek Vasut | f5d054b | 2020-03-25 18:00:35 +0100 | [diff] [blame] | 673 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 674 | pdata->iobase = dev_read_addr(dev); |
Marek Vasut | f5d054b | 2020-03-25 18:00:35 +0100 | [diff] [blame] | 675 | ks->iobase = pdata->iobase; |
| 676 | |
| 677 | return 0; |
| 678 | } |
| 679 | |
| 680 | static const struct eth_ops ks8851_ops = { |
| 681 | .start = ks8851_start, |
| 682 | .stop = ks8851_stop, |
| 683 | .send = ks8851_send, |
| 684 | .recv = ks8851_recv, |
| 685 | .write_hwaddr = ks8851_write_hwaddr, |
Marek Vasut | 8530bb4 | 2020-10-08 15:14:17 +0200 | [diff] [blame] | 686 | .read_rom_hwaddr = ks8851_read_rom_hwaddr, |
Marek Vasut | f5d054b | 2020-03-25 18:00:35 +0100 | [diff] [blame] | 687 | }; |
| 688 | |
| 689 | static const struct udevice_id ks8851_ids[] = { |
| 690 | { .compatible = "micrel,ks8851-mll" }, |
| 691 | { } |
| 692 | }; |
| 693 | |
| 694 | U_BOOT_DRIVER(ks8851) = { |
| 695 | .name = "eth_ks8851", |
| 696 | .id = UCLASS_ETH, |
| 697 | .of_match = ks8851_ids, |
| 698 | .bind = ks8851_bind, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 699 | .of_to_plat = ks8851_of_to_plat, |
Marek Vasut | f5d054b | 2020-03-25 18:00:35 +0100 | [diff] [blame] | 700 | .probe = ks8851_probe, |
| 701 | .ops = &ks8851_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 702 | .priv_auto = sizeof(struct ks_net), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 703 | .plat_auto = sizeof(struct eth_pdata), |
Marek Vasut | f5d054b | 2020-03-25 18:00:35 +0100 | [diff] [blame] | 704 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 705 | }; |
| 706 | #endif |