Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 2 | /* |
| 3 | * SMSC LAN9[12]1[567] Network driver |
| 4 | * |
Stelian Pop | 5049752 | 2008-05-08 22:52:09 +0200 | [diff] [blame] | 5 | * (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de> |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 9 | #include <command.h> |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 10 | #include <malloc.h> |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 11 | #include <net.h> |
| 12 | #include <miiphy.h> |
Marek Vasut | fa8efac | 2020-03-15 14:42:23 +0100 | [diff] [blame] | 13 | #include <linux/io.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 14 | #include <linux/delay.h> |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 15 | #include <linux/types.h> |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 16 | |
Mike Frysinger | ad8e4f4 | 2009-02-23 10:29:47 -0500 | [diff] [blame] | 17 | #include "smc911x.h" |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 18 | |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 19 | struct chip_id { |
| 20 | u16 id; |
| 21 | char *name; |
| 22 | }; |
| 23 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 24 | struct smc911x_priv { |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 25 | phys_addr_t iobase; |
| 26 | const struct chip_id *chipid; |
| 27 | unsigned char enetaddr[6]; |
Andre Przywara | 9d09b0a | 2021-06-28 14:30:31 +0100 | [diff] [blame] | 28 | bool use_32_bit_io; |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 29 | }; |
| 30 | |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 31 | static const struct chip_id chip_ids[] = { |
| 32 | { CHIP_89218, "LAN89218" }, |
| 33 | { CHIP_9115, "LAN9115" }, |
| 34 | { CHIP_9116, "LAN9116" }, |
| 35 | { CHIP_9117, "LAN9117" }, |
| 36 | { CHIP_9118, "LAN9118" }, |
| 37 | { CHIP_9211, "LAN9211" }, |
| 38 | { CHIP_9215, "LAN9215" }, |
| 39 | { CHIP_9216, "LAN9216" }, |
| 40 | { CHIP_9217, "LAN9217" }, |
| 41 | { CHIP_9218, "LAN9218" }, |
| 42 | { CHIP_9220, "LAN9220" }, |
| 43 | { CHIP_9221, "LAN9221" }, |
| 44 | { 0, NULL }, |
| 45 | }; |
| 46 | |
| 47 | #define DRIVERNAME "smc911x" |
| 48 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 49 | static u32 smc911x_reg_read(struct smc911x_priv *priv, u32 offset) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 50 | { |
Andre Przywara | 9d09b0a | 2021-06-28 14:30:31 +0100 | [diff] [blame] | 51 | if (priv->use_32_bit_io) |
| 52 | return readl(priv->iobase + offset); |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 53 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 54 | return (readw(priv->iobase + offset) & 0xffff) | |
| 55 | (readw(priv->iobase + offset + 2) << 16); |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 56 | } |
Andre Przywara | 9d09b0a | 2021-06-28 14:30:31 +0100 | [diff] [blame] | 57 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 58 | static void smc911x_reg_write(struct smc911x_priv *priv, u32 offset, u32 val) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 59 | { |
Andre Przywara | 9d09b0a | 2021-06-28 14:30:31 +0100 | [diff] [blame] | 60 | if (priv->use_32_bit_io) { |
| 61 | writel(val, priv->iobase + offset); |
| 62 | } else { |
| 63 | writew(val & 0xffff, priv->iobase + offset); |
| 64 | writew(val >> 16, priv->iobase + offset + 2); |
| 65 | } |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 66 | } |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 67 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 68 | static u32 smc911x_get_mac_csr(struct smc911x_priv *priv, u8 reg) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 69 | { |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 70 | while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 71 | ; |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 72 | smc911x_reg_write(priv, MAC_CSR_CMD, |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 73 | MAC_CSR_CMD_CSR_BUSY | MAC_CSR_CMD_R_NOT_W | reg); |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 74 | while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 75 | ; |
| 76 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 77 | return smc911x_reg_read(priv, MAC_CSR_DATA); |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 80 | static void smc911x_set_mac_csr(struct smc911x_priv *priv, u8 reg, u32 data) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 81 | { |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 82 | while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 83 | ; |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 84 | smc911x_reg_write(priv, MAC_CSR_DATA, data); |
| 85 | smc911x_reg_write(priv, MAC_CSR_CMD, MAC_CSR_CMD_CSR_BUSY | reg); |
| 86 | while (smc911x_reg_read(priv, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 87 | ; |
| 88 | } |
| 89 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 90 | static int smc911x_detect_chip(struct smc911x_priv *priv) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 91 | { |
| 92 | unsigned long val, i; |
| 93 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 94 | val = smc911x_reg_read(priv, BYTE_TEST); |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 95 | if (val == 0xffffffff) { |
| 96 | /* Special case -- no chip present */ |
| 97 | return -1; |
| 98 | } else if (val != 0x87654321) { |
| 99 | printf(DRIVERNAME ": Invalid chip endian 0x%08lx\n", val); |
| 100 | return -1; |
| 101 | } |
| 102 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 103 | val = smc911x_reg_read(priv, ID_REV) >> 16; |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 104 | for (i = 0; chip_ids[i].id != 0; i++) { |
| 105 | if (chip_ids[i].id == val) break; |
| 106 | } |
| 107 | if (!chip_ids[i].id) { |
| 108 | printf(DRIVERNAME ": Unknown chip ID %04lx\n", val); |
| 109 | return -1; |
| 110 | } |
| 111 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 112 | priv->chipid = &chip_ids[i]; |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 117 | static void smc911x_reset(struct smc911x_priv *priv) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 118 | { |
| 119 | int timeout; |
| 120 | |
| 121 | /* |
| 122 | * Take out of PM setting first |
| 123 | * Device is already wake up if PMT_CTRL_READY bit is set |
| 124 | */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 125 | if ((smc911x_reg_read(priv, PMT_CTRL) & PMT_CTRL_READY) == 0) { |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 126 | /* Write to the bytetest will take out of powerdown */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 127 | smc911x_reg_write(priv, BYTE_TEST, 0x0); |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 128 | |
| 129 | timeout = 10; |
| 130 | |
| 131 | while (timeout-- && |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 132 | !(smc911x_reg_read(priv, PMT_CTRL) & PMT_CTRL_READY)) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 133 | udelay(10); |
| 134 | if (timeout < 0) { |
| 135 | printf(DRIVERNAME |
| 136 | ": timeout waiting for PM restore\n"); |
| 137 | return; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /* Disable interrupts */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 142 | smc911x_reg_write(priv, INT_EN, 0); |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 143 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 144 | smc911x_reg_write(priv, HW_CFG, HW_CFG_SRST); |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 145 | |
| 146 | timeout = 1000; |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 147 | while (timeout-- && smc911x_reg_read(priv, E2P_CMD) & E2P_CMD_EPC_BUSY) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 148 | udelay(10); |
| 149 | |
| 150 | if (timeout < 0) { |
| 151 | printf(DRIVERNAME ": reset timeout\n"); |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | /* Reset the FIFO level and flow control settings */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 156 | smc911x_set_mac_csr(priv, FLOW, FLOW_FCPT | FLOW_FCEN); |
| 157 | smc911x_reg_write(priv, AFC_CFG, 0x0050287F); |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 158 | |
| 159 | /* Set to LED outputs */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 160 | smc911x_reg_write(priv, GPIO_CFG, 0x70070000); |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 163 | static void smc911x_handle_mac_address(struct smc911x_priv *priv) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 164 | { |
| 165 | unsigned long addrh, addrl; |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 166 | unsigned char *m = priv->enetaddr; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 167 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 168 | addrl = m[0] | (m[1] << 8) | (m[2] << 16) | (m[3] << 24); |
| 169 | addrh = m[4] | (m[5] << 8); |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 170 | smc911x_set_mac_csr(priv, ADDRL, addrl); |
| 171 | smc911x_set_mac_csr(priv, ADDRH, addrh); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 172 | |
Mike Frysinger | 719b675 | 2009-02-11 19:09:54 -0500 | [diff] [blame] | 173 | printf(DRIVERNAME ": MAC %pM\n", m); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 174 | } |
| 175 | |
Andre Przywara | 240bfb1 | 2020-06-11 12:03:17 +0100 | [diff] [blame] | 176 | static bool smc911x_read_mac_address(struct smc911x_priv *priv) |
| 177 | { |
| 178 | u32 addrh, addrl; |
| 179 | |
| 180 | /* address is obtained from optional eeprom */ |
| 181 | addrh = smc911x_get_mac_csr(priv, ADDRH); |
| 182 | addrl = smc911x_get_mac_csr(priv, ADDRL); |
| 183 | if (addrl == 0xffffffff && addrh == 0x0000ffff) |
| 184 | return false; |
| 185 | |
| 186 | priv->enetaddr[0] = addrl; |
| 187 | priv->enetaddr[1] = addrl >> 8; |
| 188 | priv->enetaddr[2] = addrl >> 16; |
| 189 | priv->enetaddr[3] = addrl >> 24; |
| 190 | priv->enetaddr[4] = addrh; |
| 191 | priv->enetaddr[5] = addrh >> 8; |
| 192 | |
| 193 | return true; |
| 194 | } |
| 195 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 196 | static int smc911x_eth_phy_read(struct smc911x_priv *priv, |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 197 | u8 phy, u8 reg, u16 *val) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 198 | { |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 199 | while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY) |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 200 | ; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 201 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 202 | smc911x_set_mac_csr(priv, MII_ACC, phy << 11 | reg << 6 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 203 | MII_ACC_MII_BUSY); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 204 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 205 | while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY) |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 206 | ; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 207 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 208 | *val = smc911x_get_mac_csr(priv, MII_DATA); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 213 | static int smc911x_eth_phy_write(struct smc911x_priv *priv, |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 214 | u8 phy, u8 reg, u16 val) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 215 | { |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 216 | while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY) |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 217 | ; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 218 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 219 | smc911x_set_mac_csr(priv, MII_DATA, val); |
| 220 | smc911x_set_mac_csr(priv, MII_ACC, |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 221 | phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE); |
| 222 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 223 | while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY) |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 224 | ; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 225 | return 0; |
| 226 | } |
| 227 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 228 | static int smc911x_phy_reset(struct smc911x_priv *priv) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 229 | { |
| 230 | u32 reg; |
| 231 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 232 | reg = smc911x_reg_read(priv, PMT_CTRL); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 233 | reg &= ~0xfffff030; |
| 234 | reg |= PMT_CTRL_PHY_RST; |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 235 | smc911x_reg_write(priv, PMT_CTRL, reg); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 236 | |
| 237 | mdelay(100); |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 242 | static void smc911x_phy_configure(struct smc911x_priv *priv) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 243 | { |
| 244 | int timeout; |
| 245 | u16 status; |
| 246 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 247 | smc911x_phy_reset(priv); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 248 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 249 | smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_RESET); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 250 | mdelay(1); |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 251 | smc911x_eth_phy_write(priv, 1, MII_ADVERTISE, 0x01e1); |
| 252 | smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_ANENABLE | |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 253 | BMCR_ANRESTART); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 254 | |
| 255 | timeout = 5000; |
| 256 | do { |
| 257 | mdelay(1); |
| 258 | if ((timeout--) == 0) |
| 259 | goto err_out; |
| 260 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 261 | if (smc911x_eth_phy_read(priv, 1, MII_BMSR, &status) != 0) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 262 | goto err_out; |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 263 | } while (!(status & BMSR_LSTATUS)); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 264 | |
| 265 | printf(DRIVERNAME ": phy initialized\n"); |
| 266 | |
| 267 | return; |
| 268 | |
| 269 | err_out: |
| 270 | printf(DRIVERNAME ": autonegotiation timed out\n"); |
| 271 | } |
| 272 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 273 | static void smc911x_enable(struct smc911x_priv *priv) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 274 | { |
| 275 | /* Enable TX */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 276 | smc911x_reg_write(priv, HW_CFG, 8 << 16 | HW_CFG_SF); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 277 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 278 | smc911x_reg_write(priv, GPT_CFG, GPT_CFG_TIMER_EN | 10000); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 279 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 280 | smc911x_reg_write(priv, TX_CFG, TX_CFG_TX_ON); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 281 | |
| 282 | /* no padding to start of packets */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 283 | smc911x_reg_write(priv, RX_CFG, 0); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 284 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 285 | smc911x_set_mac_csr(priv, MAC_CR, MAC_CR_TXEN | MAC_CR_RXEN | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 286 | MAC_CR_HBDIS); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 287 | } |
| 288 | |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 289 | static int smc911x_init_common(struct smc911x_priv *priv) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 290 | { |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 291 | const struct chip_id *id = priv->chipid; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 292 | |
Wolfgang Denk | d61fbcc | 2009-10-28 00:49:47 +0100 | [diff] [blame] | 293 | printf(DRIVERNAME ": detected %s controller\n", id->name); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 294 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 295 | smc911x_reset(priv); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 296 | |
| 297 | /* Configure the PHY, initialize the link state */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 298 | smc911x_phy_configure(priv); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 299 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 300 | smc911x_handle_mac_address(priv); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 301 | |
| 302 | /* Turn on Tx + Rx */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 303 | smc911x_enable(priv); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 304 | |
| 305 | return 0; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 306 | } |
| 307 | |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 308 | static int smc911x_send_common(struct smc911x_priv *priv, |
| 309 | void *packet, int length) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 310 | { |
| 311 | u32 *data = (u32*)packet; |
| 312 | u32 tmplen; |
| 313 | u32 status; |
| 314 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 315 | smc911x_reg_write(priv, TX_DATA_FIFO, TX_CMD_A_INT_FIRST_SEG | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 316 | TX_CMD_A_INT_LAST_SEG | length); |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 317 | smc911x_reg_write(priv, TX_DATA_FIFO, length); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 318 | |
| 319 | tmplen = (length + 3) / 4; |
| 320 | |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 321 | while (tmplen--) |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 322 | smc911x_reg_write(priv, TX_DATA_FIFO, *data++); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 323 | |
| 324 | /* wait for transmission */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 325 | while (!((smc911x_reg_read(priv, TX_FIFO_INF) & |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 326 | TX_FIFO_INF_TSUSED) >> 16)); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 327 | |
| 328 | /* get status. Ignore 'no carrier' error, it has no meaning for |
| 329 | * full duplex operation |
| 330 | */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 331 | status = smc911x_reg_read(priv, TX_STATUS_FIFO) & |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 332 | (TX_STS_LOC | TX_STS_LATE_COLL | TX_STS_MANY_COLL | |
| 333 | TX_STS_MANY_DEFER | TX_STS_UNDERRUN); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 334 | |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 335 | if (!status) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 336 | return 0; |
| 337 | |
| 338 | printf(DRIVERNAME ": failed to send packet: %s%s%s%s%s\n", |
| 339 | status & TX_STS_LOC ? "TX_STS_LOC " : "", |
| 340 | status & TX_STS_LATE_COLL ? "TX_STS_LATE_COLL " : "", |
| 341 | status & TX_STS_MANY_COLL ? "TX_STS_MANY_COLL " : "", |
| 342 | status & TX_STS_MANY_DEFER ? "TX_STS_MANY_DEFER " : "", |
| 343 | status & TX_STS_UNDERRUN ? "TX_STS_UNDERRUN" : ""); |
| 344 | |
| 345 | return -1; |
| 346 | } |
| 347 | |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 348 | static void smc911x_halt_common(struct smc911x_priv *priv) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 349 | { |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 350 | smc911x_reset(priv); |
| 351 | smc911x_handle_mac_address(priv); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 352 | } |
| 353 | |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 354 | static int smc911x_recv_common(struct smc911x_priv *priv, u32 *data) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 355 | { |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 356 | u32 pktlen, tmplen; |
| 357 | u32 status; |
| 358 | |
Marek Vasut | c70631f | 2020-03-15 17:02:05 +0100 | [diff] [blame] | 359 | status = smc911x_reg_read(priv, RX_FIFO_INF); |
| 360 | if (!(status & RX_FIFO_INF_RXSUSED)) |
| 361 | return 0; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 362 | |
Marek Vasut | c70631f | 2020-03-15 17:02:05 +0100 | [diff] [blame] | 363 | status = smc911x_reg_read(priv, RX_STATUS_FIFO); |
| 364 | pktlen = (status & RX_STS_PKT_LEN) >> 16; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 365 | |
Marek Vasut | c70631f | 2020-03-15 17:02:05 +0100 | [diff] [blame] | 366 | smc911x_reg_write(priv, RX_CFG, 0); |
| 367 | |
| 368 | tmplen = (pktlen + 3) / 4; |
| 369 | while (tmplen--) |
| 370 | *data++ = smc911x_reg_read(priv, RX_DATA_FIFO); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 371 | |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 372 | if (status & RX_STS_ES) { |
Marek Vasut | c70631f | 2020-03-15 17:02:05 +0100 | [diff] [blame] | 373 | printf(DRIVERNAME |
| 374 | ": dropped bad packet. Status: 0x%08x\n", |
| 375 | status); |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 376 | return 0; |
| 377 | } |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 378 | |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 379 | return pktlen; |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 382 | static int smc911x_start(struct udevice *dev) |
| 383 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 384 | struct eth_pdata *plat = dev_get_plat(dev); |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 385 | struct smc911x_priv *priv = dev_get_priv(dev); |
| 386 | |
| 387 | memcpy(priv->enetaddr, plat->enetaddr, sizeof(plat->enetaddr)); |
| 388 | |
| 389 | return smc911x_init_common(priv); |
| 390 | } |
| 391 | |
| 392 | static void smc911x_stop(struct udevice *dev) |
| 393 | { |
| 394 | struct smc911x_priv *priv = dev_get_priv(dev); |
| 395 | |
| 396 | smc911x_halt_common(priv); |
| 397 | } |
| 398 | |
| 399 | static int smc911x_send(struct udevice *dev, void *packet, int length) |
| 400 | { |
| 401 | struct smc911x_priv *priv = dev_get_priv(dev); |
| 402 | int ret; |
| 403 | |
| 404 | ret = smc911x_send_common(priv, packet, length); |
| 405 | |
Maxim Uvarov | f989147 | 2023-12-26 21:46:13 +0600 | [diff] [blame] | 406 | return ret ? -ETIMEDOUT : 0; |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | static int smc911x_recv(struct udevice *dev, int flags, uchar **packetp) |
| 410 | { |
| 411 | struct smc911x_priv *priv = dev_get_priv(dev); |
| 412 | u32 *data = (u32 *)net_rx_packets[0]; |
| 413 | int ret; |
| 414 | |
| 415 | ret = smc911x_recv_common(priv, data); |
| 416 | if (ret) |
| 417 | *packetp = (void *)data; |
| 418 | |
| 419 | return ret ? ret : -EAGAIN; |
| 420 | } |
| 421 | |
Andre Przywara | 240bfb1 | 2020-06-11 12:03:17 +0100 | [diff] [blame] | 422 | static int smc911x_read_rom_hwaddr(struct udevice *dev) |
| 423 | { |
| 424 | struct smc911x_priv *priv = dev_get_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 425 | struct eth_pdata *pdata = dev_get_plat(dev); |
Andre Przywara | 240bfb1 | 2020-06-11 12:03:17 +0100 | [diff] [blame] | 426 | |
| 427 | if (!smc911x_read_mac_address(priv)) |
| 428 | return -ENODEV; |
| 429 | |
| 430 | memcpy(pdata->enetaddr, priv->enetaddr, sizeof(pdata->enetaddr)); |
| 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 435 | static int smc911x_bind(struct udevice *dev) |
| 436 | { |
| 437 | return device_set_name(dev, dev->name); |
| 438 | } |
| 439 | |
| 440 | static int smc911x_probe(struct udevice *dev) |
| 441 | { |
| 442 | struct smc911x_priv *priv = dev_get_priv(dev); |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 443 | int ret; |
| 444 | |
| 445 | /* Try to detect chip. Will fail if not present. */ |
| 446 | ret = smc911x_detect_chip(priv); |
| 447 | if (ret) |
| 448 | return ret; |
| 449 | |
Andre Przywara | 240bfb1 | 2020-06-11 12:03:17 +0100 | [diff] [blame] | 450 | smc911x_read_rom_hwaddr(dev); |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 451 | |
| 452 | return 0; |
| 453 | } |
| 454 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 455 | static int smc911x_of_to_plat(struct udevice *dev) |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 456 | { |
| 457 | struct smc911x_priv *priv = dev_get_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 458 | struct eth_pdata *pdata = dev_get_plat(dev); |
Andre Przywara | 9d09b0a | 2021-06-28 14:30:31 +0100 | [diff] [blame] | 459 | u32 io_width; |
| 460 | int ret; |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 461 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 462 | pdata->iobase = dev_read_addr(dev); |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 463 | priv->iobase = pdata->iobase; |
| 464 | |
Andre Przywara | 9d09b0a | 2021-06-28 14:30:31 +0100 | [diff] [blame] | 465 | ret = dev_read_u32(dev, "reg-io-width", &io_width); |
| 466 | if (!ret) |
| 467 | priv->use_32_bit_io = (io_width == 4); |
| 468 | else |
| 469 | priv->use_32_bit_io = CONFIG_IS_ENABLED(SMC911X_32_BIT); |
| 470 | |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | static const struct eth_ops smc911x_ops = { |
| 475 | .start = smc911x_start, |
| 476 | .send = smc911x_send, |
| 477 | .recv = smc911x_recv, |
| 478 | .stop = smc911x_stop, |
Andre Przywara | 240bfb1 | 2020-06-11 12:03:17 +0100 | [diff] [blame] | 479 | .read_rom_hwaddr = smc911x_read_rom_hwaddr, |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 480 | }; |
| 481 | |
| 482 | static const struct udevice_id smc911x_ids[] = { |
| 483 | { .compatible = "smsc,lan9115" }, |
| 484 | { } |
| 485 | }; |
| 486 | |
| 487 | U_BOOT_DRIVER(smc911x) = { |
| 488 | .name = "eth_smc911x", |
| 489 | .id = UCLASS_ETH, |
| 490 | .of_match = smc911x_ids, |
| 491 | .bind = smc911x_bind, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 492 | .of_to_plat = smc911x_of_to_plat, |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 493 | .probe = smc911x_probe, |
| 494 | .ops = &smc911x_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 495 | .priv_auto = sizeof(struct smc911x_priv), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 496 | .plat_auto = sizeof(struct eth_pdata), |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 497 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 498 | }; |