b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * SMSC LAN9[12]1[567] Network driver |
| 3 | * |
| 4 | * (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de> |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <command.h> |
| 11 | #include <malloc.h> |
| 12 | #include <net.h> |
| 13 | #include <miiphy.h> |
| 14 | |
| 15 | #include "smc911x.h" |
| 16 | |
| 17 | u32 pkt_data_pull(struct eth_device *dev, u32 addr) \ |
| 18 | __attribute__ ((weak, alias ("smc911x_reg_read"))); |
| 19 | void pkt_data_push(struct eth_device *dev, u32 addr, u32 val) \ |
| 20 | __attribute__ ((weak, alias ("smc911x_reg_write"))); |
| 21 | |
| 22 | static void smc911x_handle_mac_address(struct eth_device *dev) |
| 23 | { |
| 24 | unsigned long addrh, addrl; |
| 25 | uchar *m = dev->enetaddr; |
| 26 | |
| 27 | addrl = m[0] | (m[1] << 8) | (m[2] << 16) | (m[3] << 24); |
| 28 | addrh = m[4] | (m[5] << 8); |
| 29 | smc911x_set_mac_csr(dev, ADDRL, addrl); |
| 30 | smc911x_set_mac_csr(dev, ADDRH, addrh); |
| 31 | |
| 32 | printf(DRIVERNAME ": MAC %pM\n", m); |
| 33 | } |
| 34 | |
| 35 | static int smc911x_eth_phy_read(struct eth_device *dev, |
| 36 | u8 phy, u8 reg, u16 *val) |
| 37 | { |
| 38 | while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY) |
| 39 | ; |
| 40 | |
| 41 | smc911x_set_mac_csr(dev, MII_ACC, phy << 11 | reg << 6 | |
| 42 | MII_ACC_MII_BUSY); |
| 43 | |
| 44 | while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY) |
| 45 | ; |
| 46 | |
| 47 | *val = smc911x_get_mac_csr(dev, MII_DATA); |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | static int smc911x_eth_phy_write(struct eth_device *dev, |
| 53 | u8 phy, u8 reg, u16 val) |
| 54 | { |
| 55 | while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY) |
| 56 | ; |
| 57 | |
| 58 | smc911x_set_mac_csr(dev, MII_DATA, val); |
| 59 | smc911x_set_mac_csr(dev, MII_ACC, |
| 60 | phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE); |
| 61 | |
| 62 | while (smc911x_get_mac_csr(dev, MII_ACC) & MII_ACC_MII_BUSY) |
| 63 | ; |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static int smc911x_phy_reset(struct eth_device *dev) |
| 68 | { |
| 69 | u32 reg; |
| 70 | |
| 71 | reg = smc911x_reg_read(dev, PMT_CTRL); |
| 72 | reg &= ~0xfffff030; |
| 73 | reg |= PMT_CTRL_PHY_RST; |
| 74 | smc911x_reg_write(dev, PMT_CTRL, reg); |
| 75 | |
| 76 | mdelay(100); |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static void smc911x_phy_configure(struct eth_device *dev) |
| 82 | { |
| 83 | int timeout; |
| 84 | u16 status; |
| 85 | |
| 86 | smc911x_phy_reset(dev); |
| 87 | |
| 88 | smc911x_eth_phy_write(dev, 1, MII_BMCR, BMCR_RESET); |
| 89 | mdelay(1); |
| 90 | smc911x_eth_phy_write(dev, 1, MII_ADVERTISE, 0x01e1); |
| 91 | smc911x_eth_phy_write(dev, 1, MII_BMCR, BMCR_ANENABLE | |
| 92 | BMCR_ANRESTART); |
| 93 | |
| 94 | timeout = 5000; |
| 95 | do { |
| 96 | mdelay(1); |
| 97 | if ((timeout--) == 0) |
| 98 | goto err_out; |
| 99 | |
| 100 | if (smc911x_eth_phy_read(dev, 1, MII_BMSR, &status) != 0) |
| 101 | goto err_out; |
| 102 | } while (!(status & BMSR_LSTATUS)); |
| 103 | |
| 104 | printf(DRIVERNAME ": phy initialized\n"); |
| 105 | |
| 106 | return; |
| 107 | |
| 108 | err_out: |
| 109 | printf(DRIVERNAME ": autonegotiation timed out\n"); |
| 110 | } |
| 111 | |
| 112 | static void smc911x_enable(struct eth_device *dev) |
| 113 | { |
| 114 | /* Enable TX */ |
| 115 | smc911x_reg_write(dev, HW_CFG, 8 << 16 | HW_CFG_SF); |
| 116 | |
| 117 | smc911x_reg_write(dev, GPT_CFG, GPT_CFG_TIMER_EN | 10000); |
| 118 | |
| 119 | smc911x_reg_write(dev, TX_CFG, TX_CFG_TX_ON); |
| 120 | |
| 121 | /* no padding to start of packets */ |
| 122 | smc911x_reg_write(dev, RX_CFG, 0); |
| 123 | |
| 124 | smc911x_set_mac_csr(dev, MAC_CR, MAC_CR_TXEN | MAC_CR_RXEN | |
| 125 | MAC_CR_HBDIS); |
| 126 | |
| 127 | } |
| 128 | |
| 129 | static int smc911x_init(struct eth_device *dev, bd_t * bd) |
| 130 | { |
| 131 | struct chip_id *id = dev->priv; |
| 132 | |
| 133 | printf(DRIVERNAME ": detected %s controller\n", id->name); |
| 134 | |
| 135 | smc911x_reset(dev); |
| 136 | |
| 137 | /* Configure the PHY, initialize the link state */ |
| 138 | smc911x_phy_configure(dev); |
| 139 | |
| 140 | smc911x_handle_mac_address(dev); |
| 141 | |
| 142 | /* Turn on Tx + Rx */ |
| 143 | smc911x_enable(dev); |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static int smc911x_send(struct eth_device *dev, void *packet, int length) |
| 149 | { |
| 150 | u32 *data = (u32*)packet; |
| 151 | u32 tmplen; |
| 152 | u32 status; |
| 153 | |
| 154 | smc911x_reg_write(dev, TX_DATA_FIFO, TX_CMD_A_INT_FIRST_SEG | |
| 155 | TX_CMD_A_INT_LAST_SEG | length); |
| 156 | smc911x_reg_write(dev, TX_DATA_FIFO, length); |
| 157 | |
| 158 | tmplen = (length + 3) / 4; |
| 159 | |
| 160 | while (tmplen--) |
| 161 | pkt_data_push(dev, TX_DATA_FIFO, *data++); |
| 162 | |
| 163 | /* wait for transmission */ |
| 164 | while (!((smc911x_reg_read(dev, TX_FIFO_INF) & |
| 165 | TX_FIFO_INF_TSUSED) >> 16)); |
| 166 | |
| 167 | /* get status. Ignore 'no carrier' error, it has no meaning for |
| 168 | * full duplex operation |
| 169 | */ |
| 170 | status = smc911x_reg_read(dev, TX_STATUS_FIFO) & |
| 171 | (TX_STS_LOC | TX_STS_LATE_COLL | TX_STS_MANY_COLL | |
| 172 | TX_STS_MANY_DEFER | TX_STS_UNDERRUN); |
| 173 | |
| 174 | if (!status) |
| 175 | return 0; |
| 176 | |
| 177 | printf(DRIVERNAME ": failed to send packet: %s%s%s%s%s\n", |
| 178 | status & TX_STS_LOC ? "TX_STS_LOC " : "", |
| 179 | status & TX_STS_LATE_COLL ? "TX_STS_LATE_COLL " : "", |
| 180 | status & TX_STS_MANY_COLL ? "TX_STS_MANY_COLL " : "", |
| 181 | status & TX_STS_MANY_DEFER ? "TX_STS_MANY_DEFER " : "", |
| 182 | status & TX_STS_UNDERRUN ? "TX_STS_UNDERRUN" : ""); |
| 183 | |
| 184 | return -1; |
| 185 | } |
| 186 | |
| 187 | static void smc911x_halt(struct eth_device *dev) |
| 188 | { |
| 189 | smc911x_reset(dev); |
| 190 | } |
| 191 | |
| 192 | static int smc911x_rx(struct eth_device *dev) |
| 193 | { |
| 194 | u32 *data = (u32 *)NetRxPackets[0]; |
| 195 | u32 pktlen, tmplen; |
| 196 | u32 status; |
| 197 | |
| 198 | if ((smc911x_reg_read(dev, RX_FIFO_INF) & RX_FIFO_INF_RXSUSED) >> 16) { |
| 199 | status = smc911x_reg_read(dev, RX_STATUS_FIFO); |
| 200 | pktlen = (status & RX_STS_PKT_LEN) >> 16; |
| 201 | |
| 202 | smc911x_reg_write(dev, RX_CFG, 0); |
| 203 | |
| 204 | tmplen = (pktlen + 3) / 4; |
| 205 | while (tmplen--) |
| 206 | *data++ = pkt_data_pull(dev, RX_DATA_FIFO); |
| 207 | |
| 208 | if (status & RX_STS_ES) |
| 209 | printf(DRIVERNAME |
| 210 | ": dropped bad packet. Status: 0x%08x\n", |
| 211 | status); |
| 212 | else |
| 213 | NetReceive(NetRxPackets[0], pktlen); |
| 214 | } |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
| 220 | /* wrapper for smc911x_eth_phy_read */ |
| 221 | static int smc911x_miiphy_read(const char *devname, u8 phy, u8 reg, u16 *val) |
| 222 | { |
| 223 | struct eth_device *dev = eth_get_dev_by_name(devname); |
| 224 | if (dev) |
| 225 | return smc911x_eth_phy_read(dev, phy, reg, val); |
| 226 | return -1; |
| 227 | } |
| 228 | /* wrapper for smc911x_eth_phy_write */ |
| 229 | static int smc911x_miiphy_write(const char *devname, u8 phy, u8 reg, u16 val) |
| 230 | { |
| 231 | struct eth_device *dev = eth_get_dev_by_name(devname); |
| 232 | if (dev) |
| 233 | return smc911x_eth_phy_write(dev, phy, reg, val); |
| 234 | return -1; |
| 235 | } |
| 236 | #endif |
| 237 | |
| 238 | int smc911x_initialize(u8 dev_num, int base_addr) |
| 239 | { |
| 240 | unsigned long addrl, addrh; |
| 241 | struct eth_device *dev; |
| 242 | |
| 243 | dev = malloc(sizeof(*dev)); |
| 244 | if (!dev) { |
| 245 | return -1; |
| 246 | } |
| 247 | memset(dev, 0, sizeof(*dev)); |
| 248 | |
| 249 | dev->iobase = base_addr; |
| 250 | |
| 251 | /* Try to detect chip. Will fail if not present. */ |
| 252 | if (smc911x_detect_chip(dev)) { |
| 253 | free(dev); |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | addrh = smc911x_get_mac_csr(dev, ADDRH); |
| 258 | addrl = smc911x_get_mac_csr(dev, ADDRL); |
| 259 | if (!(addrl == 0xffffffff && addrh == 0x0000ffff)) { |
| 260 | /* address is obtained from optional eeprom */ |
| 261 | dev->enetaddr[0] = addrl; |
| 262 | dev->enetaddr[1] = addrl >> 8; |
| 263 | dev->enetaddr[2] = addrl >> 16; |
| 264 | dev->enetaddr[3] = addrl >> 24; |
| 265 | dev->enetaddr[4] = addrh; |
| 266 | dev->enetaddr[5] = addrh >> 8; |
| 267 | } |
| 268 | |
| 269 | dev->init = smc911x_init; |
| 270 | dev->halt = smc911x_halt; |
| 271 | dev->send = smc911x_send; |
| 272 | dev->recv = smc911x_rx; |
| 273 | sprintf(dev->name, "%s-%hu", DRIVERNAME, dev_num); |
| 274 | |
| 275 | eth_register(dev); |
| 276 | |
| 277 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
| 278 | miiphy_register(dev->name, smc911x_miiphy_read, smc911x_miiphy_write); |
| 279 | #endif |
| 280 | |
| 281 | return 1; |
| 282 | } |