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