Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Roberto Cerati | 45a1693 | 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 | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 7 | #include <log.h> |
Roberto Cerati | 45a1693 | 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 | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 14 | #include <linux/delay.h> |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 15 | |
| 16 | #include "ks8851_mll.h" |
| 17 | |
| 18 | #define DRIVERNAME "ks8851_mll" |
| 19 | |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 20 | #define RX_BUF_SIZE 2000 |
| 21 | |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 22 | /* |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 23 | * struct ks_net - KS8851 driver private data |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 24 | * @bus_width : i/o bus width. |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 25 | * @sharedbus : Multipex(addr and data bus) mode indicator. |
Marek Vasut | 63f22f5 | 2020-03-25 17:23:11 +0100 | [diff] [blame] | 26 | * @extra_byte : number of extra byte prepended rx pkt. |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 27 | */ |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 28 | struct ks_net { |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 29 | int bus_width; |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 30 | u16 sharedbus; |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 31 | u8 extra_byte; |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 32 | } ks_str, *ks; |
| 33 | |
| 34 | #define BE3 0x8000 /* Byte Enable 3 */ |
| 35 | #define BE2 0x4000 /* Byte Enable 2 */ |
| 36 | #define BE1 0x2000 /* Byte Enable 1 */ |
| 37 | #define BE0 0x1000 /* Byte Enable 0 */ |
| 38 | |
| 39 | static u8 ks_rdreg8(struct eth_device *dev, u16 offset) |
| 40 | { |
| 41 | u8 shift_bit = offset & 0x03; |
| 42 | u8 shift_data = (offset & 1) << 3; |
| 43 | |
| 44 | writew(offset | (BE0 << shift_bit), dev->iobase + 2); |
| 45 | |
| 46 | return (u8)(readw(dev->iobase) >> shift_data); |
| 47 | } |
| 48 | |
| 49 | static u16 ks_rdreg16(struct eth_device *dev, u16 offset) |
| 50 | { |
| 51 | writew(offset | ((BE1 | BE0) << (offset & 0x02)), dev->iobase + 2); |
| 52 | |
| 53 | return readw(dev->iobase); |
| 54 | } |
| 55 | |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 56 | static void ks_wrreg16(struct eth_device *dev, u16 offset, u16 val) |
| 57 | { |
| 58 | writew(offset | ((BE1 | BE0) << (offset & 0x02)), dev->iobase + 2); |
| 59 | writew(val, dev->iobase); |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | * ks_inblk - read a block of data from QMU. This is called after sudo DMA mode |
| 64 | * enabled. |
| 65 | * @ks: The chip state |
| 66 | * @wptr: buffer address to save data |
| 67 | * @len: length in byte to read |
| 68 | */ |
| 69 | static inline void ks_inblk(struct eth_device *dev, u16 *wptr, u32 len) |
| 70 | { |
| 71 | len >>= 1; |
| 72 | |
| 73 | while (len--) |
| 74 | *wptr++ = readw(dev->iobase); |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * ks_outblk - write data to QMU. This is called after sudo DMA mode enabled. |
| 79 | * @ks: The chip information |
| 80 | * @wptr: buffer address |
| 81 | * @len: length in byte to write |
| 82 | */ |
| 83 | static inline void ks_outblk(struct eth_device *dev, u16 *wptr, u32 len) |
| 84 | { |
| 85 | len >>= 1; |
| 86 | |
| 87 | while (len--) |
| 88 | writew(*wptr++, dev->iobase); |
| 89 | } |
| 90 | |
| 91 | static void ks_enable_int(struct eth_device *dev) |
| 92 | { |
Marek Vasut | b043597 | 2020-03-25 17:18:55 +0100 | [diff] [blame] | 93 | ks_wrreg16(dev, KS_IER, IRQ_LCI | IRQ_TXI | IRQ_RXI); |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 94 | } |
| 95 | |
Marek Vasut | 8ec27b0 | 2020-03-25 17:25:29 +0100 | [diff] [blame^] | 96 | static void ks_set_powermode(struct eth_device *dev, unsigned int pwrmode) |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 97 | { |
Marek Vasut | 8ec27b0 | 2020-03-25 17:25:29 +0100 | [diff] [blame^] | 98 | unsigned int pmecr; |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 99 | |
| 100 | ks_rdreg16(dev, KS_GRR); |
| 101 | pmecr = ks_rdreg16(dev, KS_PMECR); |
| 102 | pmecr &= ~PMECR_PM_MASK; |
| 103 | pmecr |= pwrmode; |
| 104 | |
| 105 | ks_wrreg16(dev, KS_PMECR, pmecr); |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * ks_read_config - read chip configuration of bus width. |
| 110 | * @ks: The chip information |
| 111 | */ |
| 112 | static void ks_read_config(struct eth_device *dev) |
| 113 | { |
| 114 | u16 reg_data = 0; |
| 115 | |
| 116 | /* Regardless of bus width, 8 bit read should always work. */ |
| 117 | reg_data = ks_rdreg8(dev, KS_CCR) & 0x00FF; |
| 118 | reg_data |= ks_rdreg8(dev, KS_CCR + 1) << 8; |
| 119 | |
| 120 | /* addr/data bus are multiplexed */ |
| 121 | ks->sharedbus = (reg_data & CCR_SHARED) == CCR_SHARED; |
| 122 | |
| 123 | /* |
| 124 | * There are garbage data when reading data from QMU, |
| 125 | * depending on bus-width. |
| 126 | */ |
| 127 | if (reg_data & CCR_8BIT) { |
| 128 | ks->bus_width = ENUM_BUS_8BIT; |
| 129 | ks->extra_byte = 1; |
| 130 | } else if (reg_data & CCR_16BIT) { |
| 131 | ks->bus_width = ENUM_BUS_16BIT; |
| 132 | ks->extra_byte = 2; |
| 133 | } else { |
| 134 | ks->bus_width = ENUM_BUS_32BIT; |
| 135 | ks->extra_byte = 4; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * ks_soft_reset - issue one of the soft reset to the device |
| 141 | * @ks: The device state. |
| 142 | * @op: The bit(s) to set in the GRR |
| 143 | * |
| 144 | * Issue the relevant soft-reset command to the device's GRR register |
| 145 | * specified by @op. |
| 146 | * |
| 147 | * Note, the delays are in there as a caution to ensure that the reset |
| 148 | * has time to take effect and then complete. Since the datasheet does |
| 149 | * not currently specify the exact sequence, we have chosen something |
| 150 | * that seems to work with our device. |
| 151 | */ |
Marek Vasut | 8ec27b0 | 2020-03-25 17:25:29 +0100 | [diff] [blame^] | 152 | static void ks_soft_reset(struct eth_device *dev, unsigned int op) |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 153 | { |
| 154 | /* Disable interrupt first */ |
| 155 | ks_wrreg16(dev, KS_IER, 0x0000); |
| 156 | ks_wrreg16(dev, KS_GRR, op); |
| 157 | mdelay(10); /* wait a short time to effect reset */ |
| 158 | ks_wrreg16(dev, KS_GRR, 0); |
| 159 | mdelay(1); /* wait for condition to clear */ |
| 160 | } |
| 161 | |
| 162 | void ks_enable_qmu(struct eth_device *dev) |
| 163 | { |
| 164 | u16 w; |
| 165 | |
| 166 | w = ks_rdreg16(dev, KS_TXCR); |
| 167 | |
| 168 | /* Enables QMU Transmit (TXCR). */ |
| 169 | ks_wrreg16(dev, KS_TXCR, w | TXCR_TXE); |
| 170 | |
| 171 | /* Enable RX Frame Count Threshold and Auto-Dequeue RXQ Frame */ |
| 172 | w = ks_rdreg16(dev, KS_RXQCR); |
| 173 | ks_wrreg16(dev, KS_RXQCR, w | RXQCR_RXFCTE); |
| 174 | |
| 175 | /* Enables QMU Receive (RXCR1). */ |
| 176 | w = ks_rdreg16(dev, KS_RXCR1); |
| 177 | ks_wrreg16(dev, KS_RXCR1, w | RXCR1_RXE); |
| 178 | } |
| 179 | |
| 180 | static void ks_disable_qmu(struct eth_device *dev) |
| 181 | { |
| 182 | u16 w; |
| 183 | |
| 184 | w = ks_rdreg16(dev, KS_TXCR); |
| 185 | |
| 186 | /* Disables QMU Transmit (TXCR). */ |
| 187 | w &= ~TXCR_TXE; |
| 188 | ks_wrreg16(dev, KS_TXCR, w); |
| 189 | |
| 190 | /* Disables QMU Receive (RXCR1). */ |
| 191 | w = ks_rdreg16(dev, KS_RXCR1); |
| 192 | w &= ~RXCR1_RXE; |
| 193 | ks_wrreg16(dev, KS_RXCR1, w); |
| 194 | } |
| 195 | |
| 196 | static inline void ks_read_qmu(struct eth_device *dev, u16 *buf, u32 len) |
| 197 | { |
| 198 | u32 r = ks->extra_byte & 0x1; |
| 199 | u32 w = ks->extra_byte - r; |
| 200 | |
| 201 | /* 1. set sudo DMA mode */ |
| 202 | ks_wrreg16(dev, KS_RXFDPR, RXFDPR_RXFPAI); |
Marek Vasut | 6a45731 | 2020-03-25 17:02:51 +0100 | [diff] [blame] | 203 | ks_wrreg16(dev, KS_RXQCR, RXQCR_CMD_CNTL | RXQCR_SDA); |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 204 | |
| 205 | /* |
| 206 | * 2. read prepend data |
| 207 | * |
| 208 | * read 4 + extra bytes and discard them. |
| 209 | * extra bytes for dummy, 2 for status, 2 for len |
| 210 | */ |
| 211 | |
| 212 | if (r) |
| 213 | ks_rdreg8(dev, 0); |
| 214 | |
| 215 | ks_inblk(dev, buf, w + 2 + 2); |
| 216 | |
| 217 | /* 3. read pkt data */ |
| 218 | ks_inblk(dev, buf, ALIGN(len, 4)); |
| 219 | |
| 220 | /* 4. reset sudo DMA Mode */ |
Marek Vasut | 6a45731 | 2020-03-25 17:02:51 +0100 | [diff] [blame] | 221 | ks_wrreg16(dev, KS_RXQCR, RXQCR_CMD_CNTL); |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | static void ks_rcv(struct eth_device *dev, uchar **pv_data) |
| 225 | { |
Marek Vasut | b043597 | 2020-03-25 17:18:55 +0100 | [diff] [blame] | 226 | unsigned int frame_cnt; |
Marek Vasut | 63f22f5 | 2020-03-25 17:23:11 +0100 | [diff] [blame] | 227 | u16 sts, len; |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 228 | int i; |
| 229 | |
Marek Vasut | b043597 | 2020-03-25 17:18:55 +0100 | [diff] [blame] | 230 | frame_cnt = ks_rdreg16(dev, KS_RXFCTR) >> 8; |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 231 | |
| 232 | /* read all header information */ |
Marek Vasut | b043597 | 2020-03-25 17:18:55 +0100 | [diff] [blame] | 233 | for (i = 0; i < frame_cnt; i++) { |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 234 | /* Checking Received packet status */ |
Marek Vasut | 63f22f5 | 2020-03-25 17:23:11 +0100 | [diff] [blame] | 235 | sts = ks_rdreg16(dev, KS_RXFHSR); |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 236 | /* Get packet len from hardware */ |
Marek Vasut | 63f22f5 | 2020-03-25 17:23:11 +0100 | [diff] [blame] | 237 | len = ks_rdreg16(dev, KS_RXFHBCR); |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 238 | |
Marek Vasut | 63f22f5 | 2020-03-25 17:23:11 +0100 | [diff] [blame] | 239 | if ((sts & RXFSHR_RXFV) && len && (len < RX_BUF_SIZE)) { |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 240 | /* read data block including CRC 4 bytes */ |
Marek Vasut | 63f22f5 | 2020-03-25 17:23:11 +0100 | [diff] [blame] | 241 | ks_read_qmu(dev, (u16 *)(*pv_data), len); |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 242 | |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 243 | /* net_rx_packets buffer size is ok (*pv_data) */ |
Marek Vasut | 63f22f5 | 2020-03-25 17:23:11 +0100 | [diff] [blame] | 244 | net_process_received_packet(*pv_data, len); |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 245 | pv_data++; |
| 246 | } else { |
Marek Vasut | 8b41a16 | 2020-03-25 17:02:21 +0100 | [diff] [blame] | 247 | ks_wrreg16(dev, KS_RXQCR, RXQCR_CMD_CNTL | RXQCR_RRXEF); |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 248 | printf(DRIVERNAME ": bad packet\n"); |
| 249 | } |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * ks_read_selftest - read the selftest memory info. |
| 255 | * @ks: The device state |
| 256 | * |
| 257 | * Read and check the TX/RX memory selftest information. |
| 258 | */ |
| 259 | static int ks_read_selftest(struct eth_device *dev) |
| 260 | { |
| 261 | u16 both_done = MBIR_TXMBF | MBIR_RXMBF; |
| 262 | u16 mbir; |
| 263 | int ret = 0; |
| 264 | |
| 265 | mbir = ks_rdreg16(dev, KS_MBIR); |
| 266 | |
| 267 | if ((mbir & both_done) != both_done) { |
| 268 | printf(DRIVERNAME ": Memory selftest not finished\n"); |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | if (mbir & MBIR_TXMBFA) { |
| 273 | printf(DRIVERNAME ": TX memory selftest fails\n"); |
| 274 | ret |= 1; |
| 275 | } |
| 276 | |
| 277 | if (mbir & MBIR_RXMBFA) { |
| 278 | printf(DRIVERNAME ": RX memory selftest fails\n"); |
| 279 | ret |= 2; |
| 280 | } |
| 281 | |
| 282 | debug(DRIVERNAME ": the selftest passes\n"); |
| 283 | |
| 284 | return ret; |
| 285 | } |
| 286 | |
| 287 | static void ks_setup(struct eth_device *dev) |
| 288 | { |
| 289 | u16 w; |
| 290 | |
| 291 | /* Setup Transmit Frame Data Pointer Auto-Increment (TXFDPR) */ |
| 292 | ks_wrreg16(dev, KS_TXFDPR, TXFDPR_TXFPAI); |
| 293 | |
| 294 | /* Setup Receive Frame Data Pointer Auto-Increment */ |
| 295 | ks_wrreg16(dev, KS_RXFDPR, RXFDPR_RXFPAI); |
| 296 | |
| 297 | /* Setup Receive Frame Threshold - 1 frame (RXFCTFC) */ |
| 298 | ks_wrreg16(dev, KS_RXFCTR, 1 & RXFCTR_THRESHOLD_MASK); |
| 299 | |
| 300 | /* Setup RxQ Command Control (RXQCR) */ |
Marek Vasut | 8b41a16 | 2020-03-25 17:02:21 +0100 | [diff] [blame] | 301 | ks_wrreg16(dev, KS_RXQCR, RXQCR_CMD_CNTL); |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 302 | |
| 303 | /* |
| 304 | * set the force mode to half duplex, default is full duplex |
| 305 | * because if the auto-negotiation fails, most switch uses |
| 306 | * half-duplex. |
| 307 | */ |
| 308 | w = ks_rdreg16(dev, KS_P1MBCR); |
| 309 | w &= ~P1MBCR_FORCE_FDX; |
| 310 | ks_wrreg16(dev, KS_P1MBCR, w); |
| 311 | |
| 312 | w = TXCR_TXFCE | TXCR_TXPE | TXCR_TXCRC | TXCR_TCGIP; |
| 313 | ks_wrreg16(dev, KS_TXCR, w); |
| 314 | |
| 315 | w = RXCR1_RXFCE | RXCR1_RXBE | RXCR1_RXUE | RXCR1_RXME | RXCR1_RXIPFCC; |
| 316 | |
| 317 | /* Normal mode */ |
| 318 | w |= RXCR1_RXPAFMA; |
| 319 | |
| 320 | ks_wrreg16(dev, KS_RXCR1, w); |
| 321 | } |
| 322 | |
| 323 | static void ks_setup_int(struct eth_device *dev) |
| 324 | { |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 325 | /* Clear the interrupts status of the hardware. */ |
| 326 | ks_wrreg16(dev, KS_ISR, 0xffff); |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | static int ks8851_mll_detect_chip(struct eth_device *dev) |
| 330 | { |
Marek Vasut | eb69d8b | 2020-03-25 18:15:46 +0100 | [diff] [blame] | 331 | unsigned short val; |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 332 | |
| 333 | ks_read_config(dev); |
| 334 | |
| 335 | val = ks_rdreg16(dev, KS_CIDER); |
| 336 | |
| 337 | if (val == 0xffff) { |
| 338 | /* Special case -- no chip present */ |
| 339 | printf(DRIVERNAME ": is chip mounted ?\n"); |
| 340 | return -1; |
| 341 | } else if ((val & 0xfff0) != CIDER_ID) { |
| 342 | printf(DRIVERNAME ": Invalid chip id 0x%04x\n", val); |
| 343 | return -1; |
| 344 | } |
| 345 | |
| 346 | debug("Read back KS8851 id 0x%x\n", val); |
| 347 | |
Marek Vasut | eb69d8b | 2020-03-25 18:15:46 +0100 | [diff] [blame] | 348 | if ((val & 0xfff0) != CIDER_ID) { |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 349 | printf(DRIVERNAME ": Unknown chip ID %04x\n", val); |
| 350 | return -1; |
| 351 | } |
| 352 | |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | static void ks8851_mll_reset(struct eth_device *dev) |
| 357 | { |
| 358 | /* wake up powermode to normal mode */ |
| 359 | ks_set_powermode(dev, PMECR_PM_NORMAL); |
| 360 | mdelay(1); /* wait for normal mode to take effect */ |
| 361 | |
| 362 | /* Disable interrupt and reset */ |
| 363 | ks_soft_reset(dev, GRR_GSR); |
| 364 | |
| 365 | /* turn off the IRQs and ack any outstanding */ |
| 366 | ks_wrreg16(dev, KS_IER, 0x0000); |
| 367 | ks_wrreg16(dev, KS_ISR, 0xffff); |
| 368 | |
| 369 | /* shutdown RX/TX QMU */ |
| 370 | ks_disable_qmu(dev); |
| 371 | } |
| 372 | |
| 373 | static void ks8851_mll_phy_configure(struct eth_device *dev) |
| 374 | { |
| 375 | u16 data; |
| 376 | |
| 377 | ks_setup(dev); |
| 378 | ks_setup_int(dev); |
| 379 | |
| 380 | /* Probing the phy */ |
| 381 | data = ks_rdreg16(dev, KS_OBCR); |
| 382 | ks_wrreg16(dev, KS_OBCR, data | OBCR_ODS_16MA); |
| 383 | |
| 384 | debug(DRIVERNAME ": phy initialized\n"); |
| 385 | } |
| 386 | |
| 387 | static void ks8851_mll_enable(struct eth_device *dev) |
| 388 | { |
| 389 | ks_wrreg16(dev, KS_ISR, 0xffff); |
| 390 | ks_enable_int(dev); |
| 391 | ks_enable_qmu(dev); |
| 392 | } |
| 393 | |
| 394 | static int ks8851_mll_init(struct eth_device *dev, bd_t *bd) |
| 395 | { |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 396 | if (ks_read_selftest(dev)) { |
| 397 | printf(DRIVERNAME ": Selftest failed\n"); |
| 398 | return -1; |
| 399 | } |
| 400 | |
| 401 | ks8851_mll_reset(dev); |
| 402 | |
| 403 | /* Configure the PHY, initialize the link state */ |
| 404 | ks8851_mll_phy_configure(dev); |
| 405 | |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 406 | /* Turn on Tx + Rx */ |
| 407 | ks8851_mll_enable(dev); |
| 408 | |
| 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | static void ks_write_qmu(struct eth_device *dev, u8 *pdata, u16 len) |
| 413 | { |
Marek Vasut | b043597 | 2020-03-25 17:18:55 +0100 | [diff] [blame] | 414 | __le16 txw[2]; |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 415 | /* start header at txb[0] to align txw entries */ |
Marek Vasut | b043597 | 2020-03-25 17:18:55 +0100 | [diff] [blame] | 416 | txw[0] = 0; |
| 417 | txw[1] = cpu_to_le16(len); |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 418 | |
| 419 | /* 1. set sudo-DMA mode */ |
| 420 | ks_wrreg16(dev, KS_TXFDPR, TXFDPR_TXFPAI); |
Marek Vasut | 6a45731 | 2020-03-25 17:02:51 +0100 | [diff] [blame] | 421 | ks_wrreg16(dev, KS_RXQCR, RXQCR_CMD_CNTL | RXQCR_SDA); |
Marek Vasut | 8ec27b0 | 2020-03-25 17:25:29 +0100 | [diff] [blame^] | 422 | /* 2. write status/length info */ |
Marek Vasut | b043597 | 2020-03-25 17:18:55 +0100 | [diff] [blame] | 423 | ks_outblk(dev, txw, 4); |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 424 | /* 3. write pkt data */ |
| 425 | ks_outblk(dev, (u16 *)pdata, ALIGN(len, 4)); |
| 426 | /* 4. reset sudo-DMA mode */ |
Marek Vasut | 6a45731 | 2020-03-25 17:02:51 +0100 | [diff] [blame] | 427 | ks_wrreg16(dev, KS_RXQCR, RXQCR_CMD_CNTL); |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 428 | /* 5. Enqueue Tx(move the pkt from TX buffer into TXQ) */ |
| 429 | ks_wrreg16(dev, KS_TXQCR, TXQCR_METFE); |
| 430 | /* 6. wait until TXQCR_METFE is auto-cleared */ |
| 431 | do { } while (ks_rdreg16(dev, KS_TXQCR) & TXQCR_METFE); |
| 432 | } |
| 433 | |
| 434 | static int ks8851_mll_send(struct eth_device *dev, void *packet, int length) |
| 435 | { |
| 436 | u8 *data = (u8 *)packet; |
| 437 | u16 tmplen = (u16)length; |
| 438 | u16 retv; |
| 439 | |
| 440 | /* |
| 441 | * Extra space are required: |
| 442 | * 4 byte for alignment, 4 for status/length, 4 for CRC |
| 443 | */ |
| 444 | retv = ks_rdreg16(dev, KS_TXMIR) & 0x1fff; |
| 445 | if (retv >= tmplen + 12) { |
| 446 | ks_write_qmu(dev, data, tmplen); |
| 447 | return 0; |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 448 | } |
Marek Vasut | 8ec27b0 | 2020-03-25 17:25:29 +0100 | [diff] [blame^] | 449 | |
| 450 | printf(DRIVERNAME ": failed to send packet: No buffer\n"); |
| 451 | return -1; |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | static void ks8851_mll_halt(struct eth_device *dev) |
| 455 | { |
| 456 | ks8851_mll_reset(dev); |
| 457 | } |
| 458 | |
| 459 | /* |
| 460 | * Maximum receive ring size; that is, the number of packets |
| 461 | * we can buffer before overflow happens. Basically, this just |
| 462 | * needs to be enough to prevent a packet being discarded while |
| 463 | * we are processing the previous one. |
| 464 | */ |
| 465 | static int ks8851_mll_recv(struct eth_device *dev) |
| 466 | { |
| 467 | u16 status; |
| 468 | |
| 469 | status = ks_rdreg16(dev, KS_ISR); |
| 470 | |
| 471 | ks_wrreg16(dev, KS_ISR, status); |
| 472 | |
Marek Vasut | 8ec27b0 | 2020-03-25 17:25:29 +0100 | [diff] [blame^] | 473 | if (status & IRQ_RXI) |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 474 | ks_rcv(dev, (uchar **)net_rx_packets); |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 475 | |
Marek Vasut | 8ec27b0 | 2020-03-25 17:25:29 +0100 | [diff] [blame^] | 476 | if (status & IRQ_LDI) { |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 477 | u16 pmecr = ks_rdreg16(dev, KS_PMECR); |
Marek Vasut | 8ec27b0 | 2020-03-25 17:25:29 +0100 | [diff] [blame^] | 478 | |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 479 | pmecr &= ~PMECR_WKEVT_MASK; |
| 480 | ks_wrreg16(dev, KS_PMECR, pmecr | PMECR_WKEVT_LINK); |
| 481 | } |
| 482 | |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | static int ks8851_mll_write_hwaddr(struct eth_device *dev) |
| 487 | { |
| 488 | u16 addrl, addrm, addrh; |
| 489 | |
| 490 | addrh = (dev->enetaddr[0] << 8) | dev->enetaddr[1]; |
| 491 | addrm = (dev->enetaddr[2] << 8) | dev->enetaddr[3]; |
| 492 | addrl = (dev->enetaddr[4] << 8) | dev->enetaddr[5]; |
| 493 | |
| 494 | ks_wrreg16(dev, KS_MARH, addrh); |
| 495 | ks_wrreg16(dev, KS_MARM, addrm); |
| 496 | ks_wrreg16(dev, KS_MARL, addrl); |
| 497 | |
| 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | int ks8851_mll_initialize(u8 dev_num, int base_addr) |
| 502 | { |
| 503 | struct eth_device *dev; |
| 504 | |
Marek Vasut | e3b54cd | 2020-03-25 16:52:38 +0100 | [diff] [blame] | 505 | dev = calloc(1, sizeof(*dev)); |
| 506 | if (!dev) |
| 507 | return -ENOMEM; |
Roberto Cerati | 45a1693 | 2013-04-24 10:46:17 +0800 | [diff] [blame] | 508 | |
| 509 | dev->iobase = base_addr; |
| 510 | |
| 511 | ks = &ks_str; |
| 512 | |
| 513 | /* Try to detect chip. Will fail if not present. */ |
| 514 | if (ks8851_mll_detect_chip(dev)) { |
| 515 | free(dev); |
| 516 | return -1; |
| 517 | } |
| 518 | |
| 519 | dev->init = ks8851_mll_init; |
| 520 | dev->halt = ks8851_mll_halt; |
| 521 | dev->send = ks8851_mll_send; |
| 522 | dev->recv = ks8851_mll_recv; |
| 523 | dev->write_hwaddr = ks8851_mll_write_hwaddr; |
| 524 | sprintf(dev->name, "%s-%hu", DRIVERNAME, dev_num); |
| 525 | |
| 526 | eth_register(dev); |
| 527 | |
| 528 | return 0; |
| 529 | } |