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