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