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 | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 25 | #ifndef CONFIG_DM_ETH |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 26 | struct eth_device dev; |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 27 | #endif |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 28 | phys_addr_t iobase; |
| 29 | const struct chip_id *chipid; |
| 30 | unsigned char enetaddr[6]; |
Andre Przywara | 9d09b0a | 2021-06-28 14:30:31 +0100 | [diff] [blame] | 31 | bool use_32_bit_io; |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 32 | }; |
| 33 | |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 34 | static const struct chip_id chip_ids[] = { |
| 35 | { CHIP_89218, "LAN89218" }, |
| 36 | { CHIP_9115, "LAN9115" }, |
| 37 | { CHIP_9116, "LAN9116" }, |
| 38 | { CHIP_9117, "LAN9117" }, |
| 39 | { CHIP_9118, "LAN9118" }, |
| 40 | { CHIP_9211, "LAN9211" }, |
| 41 | { CHIP_9215, "LAN9215" }, |
| 42 | { CHIP_9216, "LAN9216" }, |
| 43 | { CHIP_9217, "LAN9217" }, |
| 44 | { CHIP_9218, "LAN9218" }, |
| 45 | { CHIP_9220, "LAN9220" }, |
| 46 | { CHIP_9221, "LAN9221" }, |
| 47 | { 0, NULL }, |
| 48 | }; |
| 49 | |
| 50 | #define DRIVERNAME "smc911x" |
| 51 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 52 | static u32 smc911x_reg_read(struct smc911x_priv *priv, u32 offset) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 53 | { |
Andre Przywara | 9d09b0a | 2021-06-28 14:30:31 +0100 | [diff] [blame] | 54 | if (priv->use_32_bit_io) |
| 55 | return readl(priv->iobase + offset); |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 56 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 57 | return (readw(priv->iobase + offset) & 0xffff) | |
| 58 | (readw(priv->iobase + offset + 2) << 16); |
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 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 61 | 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] | 62 | { |
Andre Przywara | 9d09b0a | 2021-06-28 14:30:31 +0100 | [diff] [blame] | 63 | if (priv->use_32_bit_io) { |
| 64 | writel(val, priv->iobase + offset); |
| 65 | } else { |
| 66 | writew(val & 0xffff, priv->iobase + offset); |
| 67 | writew(val >> 16, priv->iobase + offset + 2); |
| 68 | } |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 69 | } |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 70 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 71 | static u32 smc911x_get_mac_csr(struct smc911x_priv *priv, u8 reg) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 72 | { |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 73 | 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] | 74 | ; |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 75 | smc911x_reg_write(priv, MAC_CSR_CMD, |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 76 | MAC_CSR_CMD_CSR_BUSY | MAC_CSR_CMD_R_NOT_W | reg); |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 77 | 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] | 78 | ; |
| 79 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 80 | return smc911x_reg_read(priv, MAC_CSR_DATA); |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 83 | 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] | 84 | { |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 85 | 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] | 86 | ; |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 87 | smc911x_reg_write(priv, MAC_CSR_DATA, data); |
| 88 | smc911x_reg_write(priv, MAC_CSR_CMD, MAC_CSR_CMD_CSR_BUSY | reg); |
| 89 | 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] | 90 | ; |
| 91 | } |
| 92 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 93 | static int smc911x_detect_chip(struct smc911x_priv *priv) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 94 | { |
| 95 | unsigned long val, i; |
| 96 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 97 | val = smc911x_reg_read(priv, BYTE_TEST); |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 98 | if (val == 0xffffffff) { |
| 99 | /* Special case -- no chip present */ |
| 100 | return -1; |
| 101 | } else if (val != 0x87654321) { |
| 102 | printf(DRIVERNAME ": Invalid chip endian 0x%08lx\n", val); |
| 103 | return -1; |
| 104 | } |
| 105 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 106 | val = smc911x_reg_read(priv, ID_REV) >> 16; |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 107 | for (i = 0; chip_ids[i].id != 0; i++) { |
| 108 | if (chip_ids[i].id == val) break; |
| 109 | } |
| 110 | if (!chip_ids[i].id) { |
| 111 | printf(DRIVERNAME ": Unknown chip ID %04lx\n", val); |
| 112 | return -1; |
| 113 | } |
| 114 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 115 | priv->chipid = &chip_ids[i]; |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 120 | static void smc911x_reset(struct smc911x_priv *priv) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 121 | { |
| 122 | int timeout; |
| 123 | |
| 124 | /* |
| 125 | * Take out of PM setting first |
| 126 | * Device is already wake up if PMT_CTRL_READY bit is set |
| 127 | */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 128 | if ((smc911x_reg_read(priv, PMT_CTRL) & PMT_CTRL_READY) == 0) { |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 129 | /* Write to the bytetest will take out of powerdown */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 130 | smc911x_reg_write(priv, BYTE_TEST, 0x0); |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 131 | |
| 132 | timeout = 10; |
| 133 | |
| 134 | while (timeout-- && |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 135 | !(smc911x_reg_read(priv, PMT_CTRL) & PMT_CTRL_READY)) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 136 | udelay(10); |
| 137 | if (timeout < 0) { |
| 138 | printf(DRIVERNAME |
| 139 | ": timeout waiting for PM restore\n"); |
| 140 | return; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /* Disable interrupts */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 145 | smc911x_reg_write(priv, INT_EN, 0); |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 146 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 147 | smc911x_reg_write(priv, HW_CFG, HW_CFG_SRST); |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 148 | |
| 149 | timeout = 1000; |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 150 | while (timeout-- && smc911x_reg_read(priv, E2P_CMD) & E2P_CMD_EPC_BUSY) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 151 | udelay(10); |
| 152 | |
| 153 | if (timeout < 0) { |
| 154 | printf(DRIVERNAME ": reset timeout\n"); |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | /* Reset the FIFO level and flow control settings */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 159 | smc911x_set_mac_csr(priv, FLOW, FLOW_FCPT | FLOW_FCEN); |
| 160 | smc911x_reg_write(priv, AFC_CFG, 0x0050287F); |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 161 | |
| 162 | /* Set to LED outputs */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 163 | smc911x_reg_write(priv, GPIO_CFG, 0x70070000); |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 164 | } |
| 165 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 166 | static void smc911x_handle_mac_address(struct smc911x_priv *priv) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 167 | { |
| 168 | unsigned long addrh, addrl; |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 169 | unsigned char *m = priv->enetaddr; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 170 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 171 | addrl = m[0] | (m[1] << 8) | (m[2] << 16) | (m[3] << 24); |
| 172 | addrh = m[4] | (m[5] << 8); |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 173 | smc911x_set_mac_csr(priv, ADDRL, addrl); |
| 174 | smc911x_set_mac_csr(priv, ADDRH, addrh); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 175 | |
Mike Frysinger | 719b675 | 2009-02-11 19:09:54 -0500 | [diff] [blame] | 176 | printf(DRIVERNAME ": MAC %pM\n", m); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 177 | } |
| 178 | |
Andre Przywara | 240bfb1 | 2020-06-11 12:03:17 +0100 | [diff] [blame] | 179 | static bool smc911x_read_mac_address(struct smc911x_priv *priv) |
| 180 | { |
| 181 | u32 addrh, addrl; |
| 182 | |
| 183 | /* address is obtained from optional eeprom */ |
| 184 | addrh = smc911x_get_mac_csr(priv, ADDRH); |
| 185 | addrl = smc911x_get_mac_csr(priv, ADDRL); |
| 186 | if (addrl == 0xffffffff && addrh == 0x0000ffff) |
| 187 | return false; |
| 188 | |
| 189 | priv->enetaddr[0] = addrl; |
| 190 | priv->enetaddr[1] = addrl >> 8; |
| 191 | priv->enetaddr[2] = addrl >> 16; |
| 192 | priv->enetaddr[3] = addrl >> 24; |
| 193 | priv->enetaddr[4] = addrh; |
| 194 | priv->enetaddr[5] = addrh >> 8; |
| 195 | |
| 196 | return true; |
| 197 | } |
| 198 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 199 | static int smc911x_eth_phy_read(struct smc911x_priv *priv, |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 200 | u8 phy, u8 reg, u16 *val) |
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 | while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY) |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 203 | ; |
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 | smc911x_set_mac_csr(priv, MII_ACC, phy << 11 | reg << 6 | |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 206 | MII_ACC_MII_BUSY); |
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 | while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY) |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 209 | ; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 210 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 211 | *val = smc911x_get_mac_csr(priv, MII_DATA); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 216 | static int smc911x_eth_phy_write(struct smc911x_priv *priv, |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 217 | u8 phy, u8 reg, u16 val) |
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 | while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY) |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 220 | ; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 221 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 222 | smc911x_set_mac_csr(priv, MII_DATA, val); |
| 223 | smc911x_set_mac_csr(priv, MII_ACC, |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 224 | phy << 11 | reg << 6 | MII_ACC_MII_BUSY | MII_ACC_MII_WRITE); |
| 225 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 226 | while (smc911x_get_mac_csr(priv, MII_ACC) & MII_ACC_MII_BUSY) |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 227 | ; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 228 | return 0; |
| 229 | } |
| 230 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 231 | static int smc911x_phy_reset(struct smc911x_priv *priv) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 232 | { |
| 233 | u32 reg; |
| 234 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 235 | reg = smc911x_reg_read(priv, PMT_CTRL); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 236 | reg &= ~0xfffff030; |
| 237 | reg |= PMT_CTRL_PHY_RST; |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 238 | smc911x_reg_write(priv, PMT_CTRL, reg); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 239 | |
| 240 | mdelay(100); |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 245 | static void smc911x_phy_configure(struct smc911x_priv *priv) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 246 | { |
| 247 | int timeout; |
| 248 | u16 status; |
| 249 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 250 | smc911x_phy_reset(priv); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 251 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 252 | smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_RESET); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 253 | mdelay(1); |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 254 | smc911x_eth_phy_write(priv, 1, MII_ADVERTISE, 0x01e1); |
| 255 | smc911x_eth_phy_write(priv, 1, MII_BMCR, BMCR_ANENABLE | |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 256 | BMCR_ANRESTART); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 257 | |
| 258 | timeout = 5000; |
| 259 | do { |
| 260 | mdelay(1); |
| 261 | if ((timeout--) == 0) |
| 262 | goto err_out; |
| 263 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 264 | if (smc911x_eth_phy_read(priv, 1, MII_BMSR, &status) != 0) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 265 | goto err_out; |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 266 | } while (!(status & BMSR_LSTATUS)); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 267 | |
| 268 | printf(DRIVERNAME ": phy initialized\n"); |
| 269 | |
| 270 | return; |
| 271 | |
| 272 | err_out: |
| 273 | printf(DRIVERNAME ": autonegotiation timed out\n"); |
| 274 | } |
| 275 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 276 | static void smc911x_enable(struct smc911x_priv *priv) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 277 | { |
| 278 | /* Enable TX */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 279 | smc911x_reg_write(priv, HW_CFG, 8 << 16 | HW_CFG_SF); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 280 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 281 | smc911x_reg_write(priv, GPT_CFG, GPT_CFG_TIMER_EN | 10000); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 282 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 283 | smc911x_reg_write(priv, TX_CFG, TX_CFG_TX_ON); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 284 | |
| 285 | /* no padding to start of packets */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 286 | smc911x_reg_write(priv, RX_CFG, 0); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 287 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 288 | 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] | 289 | MAC_CR_HBDIS); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 290 | } |
| 291 | |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 292 | static int smc911x_init_common(struct smc911x_priv *priv) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 293 | { |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 294 | const struct chip_id *id = priv->chipid; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 295 | |
Wolfgang Denk | d61fbcc | 2009-10-28 00:49:47 +0100 | [diff] [blame] | 296 | printf(DRIVERNAME ": detected %s controller\n", id->name); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 297 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 298 | smc911x_reset(priv); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 299 | |
| 300 | /* Configure the PHY, initialize the link state */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 301 | smc911x_phy_configure(priv); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 302 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 303 | smc911x_handle_mac_address(priv); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 304 | |
| 305 | /* Turn on Tx + Rx */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 306 | smc911x_enable(priv); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 307 | |
| 308 | return 0; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 309 | } |
| 310 | |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 311 | static int smc911x_send_common(struct smc911x_priv *priv, |
| 312 | void *packet, int length) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 313 | { |
| 314 | u32 *data = (u32*)packet; |
| 315 | u32 tmplen; |
| 316 | u32 status; |
| 317 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 318 | 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] | 319 | TX_CMD_A_INT_LAST_SEG | length); |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 320 | smc911x_reg_write(priv, TX_DATA_FIFO, length); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 321 | |
| 322 | tmplen = (length + 3) / 4; |
| 323 | |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 324 | while (tmplen--) |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 325 | smc911x_reg_write(priv, TX_DATA_FIFO, *data++); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 326 | |
| 327 | /* wait for transmission */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 328 | while (!((smc911x_reg_read(priv, TX_FIFO_INF) & |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 329 | TX_FIFO_INF_TSUSED) >> 16)); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 330 | |
| 331 | /* get status. Ignore 'no carrier' error, it has no meaning for |
| 332 | * full duplex operation |
| 333 | */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 334 | status = smc911x_reg_read(priv, TX_STATUS_FIFO) & |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 335 | (TX_STS_LOC | TX_STS_LATE_COLL | TX_STS_MANY_COLL | |
| 336 | TX_STS_MANY_DEFER | TX_STS_UNDERRUN); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 337 | |
Guennadi Liakhovetski | 588bd2b | 2008-04-29 12:35:08 +0000 | [diff] [blame] | 338 | if (!status) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 339 | return 0; |
| 340 | |
| 341 | printf(DRIVERNAME ": failed to send packet: %s%s%s%s%s\n", |
| 342 | status & TX_STS_LOC ? "TX_STS_LOC " : "", |
| 343 | status & TX_STS_LATE_COLL ? "TX_STS_LATE_COLL " : "", |
| 344 | status & TX_STS_MANY_COLL ? "TX_STS_MANY_COLL " : "", |
| 345 | status & TX_STS_MANY_DEFER ? "TX_STS_MANY_DEFER " : "", |
| 346 | status & TX_STS_UNDERRUN ? "TX_STS_UNDERRUN" : ""); |
| 347 | |
| 348 | return -1; |
| 349 | } |
| 350 | |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 351 | static void smc911x_halt_common(struct smc911x_priv *priv) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 352 | { |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 353 | smc911x_reset(priv); |
| 354 | smc911x_handle_mac_address(priv); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 355 | } |
| 356 | |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 357 | static int smc911x_recv_common(struct smc911x_priv *priv, u32 *data) |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 358 | { |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 359 | u32 pktlen, tmplen; |
| 360 | u32 status; |
| 361 | |
Marek Vasut | c70631f | 2020-03-15 17:02:05 +0100 | [diff] [blame] | 362 | status = smc911x_reg_read(priv, RX_FIFO_INF); |
| 363 | if (!(status & RX_FIFO_INF_RXSUSED)) |
| 364 | return 0; |
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 | status = smc911x_reg_read(priv, RX_STATUS_FIFO); |
| 367 | pktlen = (status & RX_STS_PKT_LEN) >> 16; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 368 | |
Marek Vasut | c70631f | 2020-03-15 17:02:05 +0100 | [diff] [blame] | 369 | smc911x_reg_write(priv, RX_CFG, 0); |
| 370 | |
| 371 | tmplen = (pktlen + 3) / 4; |
| 372 | while (tmplen--) |
| 373 | *data++ = smc911x_reg_read(priv, RX_DATA_FIFO); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 374 | |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 375 | if (status & RX_STS_ES) { |
Marek Vasut | c70631f | 2020-03-15 17:02:05 +0100 | [diff] [blame] | 376 | printf(DRIVERNAME |
| 377 | ": dropped bad packet. Status: 0x%08x\n", |
| 378 | status); |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 379 | return 0; |
| 380 | } |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 381 | |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 382 | return pktlen; |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 385 | #ifndef CONFIG_DM_ETH |
| 386 | |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 387 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
| 388 | /* wrapper for smc911x_eth_phy_read */ |
Joe Hershberger | 1fbcbed | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 389 | static int smc911x_miiphy_read(struct mii_dev *bus, int phy, int devad, |
| 390 | int reg) |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 391 | { |
Joe Hershberger | 1fbcbed | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 392 | struct eth_device *dev = eth_get_dev_by_name(bus->name); |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 393 | struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev); |
Marek Vasut | 14330e5 | 2020-03-15 15:43:20 +0100 | [diff] [blame] | 394 | u16 val = 0; |
| 395 | int ret; |
| 396 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 397 | if (!dev || !priv) |
Marek Vasut | 14330e5 | 2020-03-15 15:43:20 +0100 | [diff] [blame] | 398 | return -ENODEV; |
| 399 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 400 | ret = smc911x_eth_phy_read(priv, phy, reg, &val); |
Marek Vasut | 14330e5 | 2020-03-15 15:43:20 +0100 | [diff] [blame] | 401 | if (ret < 0) |
| 402 | return ret; |
| 403 | |
| 404 | return val; |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 405 | } |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 406 | |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 407 | /* wrapper for smc911x_eth_phy_write */ |
Joe Hershberger | 1fbcbed | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 408 | static int smc911x_miiphy_write(struct mii_dev *bus, int phy, int devad, |
| 409 | int reg, u16 val) |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 410 | { |
Joe Hershberger | 1fbcbed | 2016-08-08 11:28:38 -0500 | [diff] [blame] | 411 | struct eth_device *dev = eth_get_dev_by_name(bus->name); |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 412 | struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev); |
Marek Vasut | 14330e5 | 2020-03-15 15:43:20 +0100 | [diff] [blame] | 413 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 414 | if (!dev || !priv) |
Marek Vasut | 14330e5 | 2020-03-15 15:43:20 +0100 | [diff] [blame] | 415 | return -ENODEV; |
| 416 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 417 | return smc911x_eth_phy_write(priv, phy, reg, val); |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 418 | } |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 419 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 420 | static int smc911x_initialize_mii(struct smc911x_priv *priv) |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 421 | { |
| 422 | struct mii_dev *mdiodev = mdio_alloc(); |
| 423 | int ret; |
| 424 | |
| 425 | if (!mdiodev) |
| 426 | return -ENOMEM; |
| 427 | |
Vladimir Oltean | f201344 | 2021-09-27 14:21:57 +0300 | [diff] [blame] | 428 | strlcpy(mdiodev->name, priv->dev.name, MDIO_NAME_LEN); |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 429 | mdiodev->read = smc911x_miiphy_read; |
| 430 | mdiodev->write = smc911x_miiphy_write; |
| 431 | |
| 432 | ret = mdio_register(mdiodev); |
| 433 | if (ret < 0) { |
| 434 | mdio_free(mdiodev); |
| 435 | return ret; |
| 436 | } |
| 437 | |
| 438 | return 0; |
| 439 | } |
| 440 | #else |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 441 | static int smc911x_initialize_mii(struct smc911x_priv *priv) |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 442 | { |
| 443 | return 0; |
| 444 | } |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 445 | #endif |
| 446 | |
Masahiro Yamada | 940a922 | 2020-06-26 15:13:34 +0900 | [diff] [blame] | 447 | static int smc911x_init(struct eth_device *dev, struct bd_info *bd) |
Marek Vasut | cc83018 | 2020-03-15 17:25:27 +0100 | [diff] [blame] | 448 | { |
| 449 | struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev); |
| 450 | |
| 451 | return smc911x_init_common(priv); |
| 452 | } |
| 453 | |
| 454 | static void smc911x_halt(struct eth_device *dev) |
| 455 | { |
| 456 | struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev); |
| 457 | |
| 458 | smc911x_halt_common(priv); |
| 459 | } |
| 460 | |
| 461 | static int smc911x_send(struct eth_device *dev, void *packet, int length) |
| 462 | { |
| 463 | struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev); |
| 464 | |
| 465 | return smc911x_send_common(priv, packet, length); |
| 466 | } |
| 467 | |
| 468 | static int smc911x_recv(struct eth_device *dev) |
| 469 | { |
| 470 | struct smc911x_priv *priv = container_of(dev, struct smc911x_priv, dev); |
| 471 | u32 *data = (u32 *)net_rx_packets[0]; |
| 472 | int ret; |
| 473 | |
| 474 | ret = smc911x_recv_common(priv, data); |
| 475 | if (ret) |
| 476 | net_process_received_packet(net_rx_packets[0], ret); |
| 477 | |
| 478 | return ret; |
| 479 | } |
| 480 | |
Peter Hoyes | c2e4ed1 | 2021-07-09 10:52:06 +0100 | [diff] [blame] | 481 | int smc911x_initialize(u8 dev_num, phys_addr_t base_addr) |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 482 | { |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 483 | struct smc911x_priv *priv; |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 484 | int ret; |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 485 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 486 | priv = calloc(1, sizeof(*priv)); |
| 487 | if (!priv) |
Marek Vasut | cf61359 | 2020-03-15 15:14:18 +0100 | [diff] [blame] | 488 | return -ENOMEM; |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 489 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 490 | priv->iobase = base_addr; |
| 491 | priv->dev.iobase = base_addr; |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 492 | |
Andre Przywara | 9d09b0a | 2021-06-28 14:30:31 +0100 | [diff] [blame] | 493 | priv->use_32_bit_io = CONFIG_IS_ENABLED(SMC911X_32_BIT); |
| 494 | |
Steve Sakoman | b3ec49d | 2009-10-20 18:21:18 +0200 | [diff] [blame] | 495 | /* Try to detect chip. Will fail if not present. */ |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 496 | ret = smc911x_detect_chip(priv); |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 497 | if (ret) { |
| 498 | ret = 0; /* Card not detected is not an error */ |
| 499 | goto err_detect; |
Steve Sakoman | b3ec49d | 2009-10-20 18:21:18 +0200 | [diff] [blame] | 500 | } |
| 501 | |
Andre Przywara | 240bfb1 | 2020-06-11 12:03:17 +0100 | [diff] [blame] | 502 | if (smc911x_read_mac_address(priv)) |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 503 | memcpy(priv->dev.enetaddr, priv->enetaddr, 6); |
Ben Warren | fbfdd3a | 2009-07-20 22:01:11 -0700 | [diff] [blame] | 504 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 505 | priv->dev.init = smc911x_init; |
| 506 | priv->dev.halt = smc911x_halt; |
| 507 | priv->dev.send = smc911x_send; |
| 508 | priv->dev.recv = smc911x_recv; |
| 509 | sprintf(priv->dev.name, "%s-%hu", DRIVERNAME, dev_num); |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 510 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 511 | eth_register(&priv->dev); |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 512 | |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 513 | ret = smc911x_initialize_mii(priv); |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 514 | if (ret) |
| 515 | goto err_mii; |
Helmut Raiger | be8f9c0 | 2011-06-29 00:12:14 +0000 | [diff] [blame] | 516 | |
Mike Rapoport | 907975a | 2009-11-12 15:35:08 +0200 | [diff] [blame] | 517 | return 1; |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 518 | |
| 519 | err_mii: |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 520 | eth_unregister(&priv->dev); |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 521 | err_detect: |
Marek Vasut | 5905147 | 2020-03-15 15:36:09 +0100 | [diff] [blame] | 522 | free(priv); |
Marek Vasut | 25eb468 | 2020-03-21 17:25:41 +0100 | [diff] [blame] | 523 | return ret; |
Sascha Hauer | f43a42d | 2008-04-15 00:08:20 -0400 | [diff] [blame] | 524 | } |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 525 | |
| 526 | #else /* ifdef CONFIG_DM_ETH */ |
| 527 | |
| 528 | static int smc911x_start(struct udevice *dev) |
| 529 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 530 | struct eth_pdata *plat = dev_get_plat(dev); |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 531 | struct smc911x_priv *priv = dev_get_priv(dev); |
| 532 | |
| 533 | memcpy(priv->enetaddr, plat->enetaddr, sizeof(plat->enetaddr)); |
| 534 | |
| 535 | return smc911x_init_common(priv); |
| 536 | } |
| 537 | |
| 538 | static void smc911x_stop(struct udevice *dev) |
| 539 | { |
| 540 | struct smc911x_priv *priv = dev_get_priv(dev); |
| 541 | |
| 542 | smc911x_halt_common(priv); |
| 543 | } |
| 544 | |
| 545 | static int smc911x_send(struct udevice *dev, void *packet, int length) |
| 546 | { |
| 547 | struct smc911x_priv *priv = dev_get_priv(dev); |
| 548 | int ret; |
| 549 | |
| 550 | ret = smc911x_send_common(priv, packet, length); |
| 551 | |
| 552 | return ret ? 0 : -ETIMEDOUT; |
| 553 | } |
| 554 | |
| 555 | static int smc911x_recv(struct udevice *dev, int flags, uchar **packetp) |
| 556 | { |
| 557 | struct smc911x_priv *priv = dev_get_priv(dev); |
| 558 | u32 *data = (u32 *)net_rx_packets[0]; |
| 559 | int ret; |
| 560 | |
| 561 | ret = smc911x_recv_common(priv, data); |
| 562 | if (ret) |
| 563 | *packetp = (void *)data; |
| 564 | |
| 565 | return ret ? ret : -EAGAIN; |
| 566 | } |
| 567 | |
Andre Przywara | 240bfb1 | 2020-06-11 12:03:17 +0100 | [diff] [blame] | 568 | static int smc911x_read_rom_hwaddr(struct udevice *dev) |
| 569 | { |
| 570 | struct smc911x_priv *priv = dev_get_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 571 | struct eth_pdata *pdata = dev_get_plat(dev); |
Andre Przywara | 240bfb1 | 2020-06-11 12:03:17 +0100 | [diff] [blame] | 572 | |
| 573 | if (!smc911x_read_mac_address(priv)) |
| 574 | return -ENODEV; |
| 575 | |
| 576 | memcpy(pdata->enetaddr, priv->enetaddr, sizeof(pdata->enetaddr)); |
| 577 | |
| 578 | return 0; |
| 579 | } |
| 580 | |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 581 | static int smc911x_bind(struct udevice *dev) |
| 582 | { |
| 583 | return device_set_name(dev, dev->name); |
| 584 | } |
| 585 | |
| 586 | static int smc911x_probe(struct udevice *dev) |
| 587 | { |
| 588 | struct smc911x_priv *priv = dev_get_priv(dev); |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 589 | int ret; |
| 590 | |
| 591 | /* Try to detect chip. Will fail if not present. */ |
| 592 | ret = smc911x_detect_chip(priv); |
| 593 | if (ret) |
| 594 | return ret; |
| 595 | |
Andre Przywara | 240bfb1 | 2020-06-11 12:03:17 +0100 | [diff] [blame] | 596 | smc911x_read_rom_hwaddr(dev); |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 597 | |
| 598 | return 0; |
| 599 | } |
| 600 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 601 | static int smc911x_of_to_plat(struct udevice *dev) |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 602 | { |
| 603 | struct smc911x_priv *priv = dev_get_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 604 | struct eth_pdata *pdata = dev_get_plat(dev); |
Andre Przywara | 9d09b0a | 2021-06-28 14:30:31 +0100 | [diff] [blame] | 605 | u32 io_width; |
| 606 | int ret; |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 607 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 608 | pdata->iobase = dev_read_addr(dev); |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 609 | priv->iobase = pdata->iobase; |
| 610 | |
Andre Przywara | 9d09b0a | 2021-06-28 14:30:31 +0100 | [diff] [blame] | 611 | ret = dev_read_u32(dev, "reg-io-width", &io_width); |
| 612 | if (!ret) |
| 613 | priv->use_32_bit_io = (io_width == 4); |
| 614 | else |
| 615 | priv->use_32_bit_io = CONFIG_IS_ENABLED(SMC911X_32_BIT); |
| 616 | |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | static const struct eth_ops smc911x_ops = { |
| 621 | .start = smc911x_start, |
| 622 | .send = smc911x_send, |
| 623 | .recv = smc911x_recv, |
| 624 | .stop = smc911x_stop, |
Andre Przywara | 240bfb1 | 2020-06-11 12:03:17 +0100 | [diff] [blame] | 625 | .read_rom_hwaddr = smc911x_read_rom_hwaddr, |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 626 | }; |
| 627 | |
| 628 | static const struct udevice_id smc911x_ids[] = { |
| 629 | { .compatible = "smsc,lan9115" }, |
| 630 | { } |
| 631 | }; |
| 632 | |
| 633 | U_BOOT_DRIVER(smc911x) = { |
| 634 | .name = "eth_smc911x", |
| 635 | .id = UCLASS_ETH, |
| 636 | .of_match = smc911x_ids, |
| 637 | .bind = smc911x_bind, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 638 | .of_to_plat = smc911x_of_to_plat, |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 639 | .probe = smc911x_probe, |
| 640 | .ops = &smc911x_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 641 | .priv_auto = sizeof(struct smc911x_priv), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 642 | .plat_auto = sizeof(struct eth_pdata), |
Marek Vasut | 5539944 | 2020-03-15 17:39:01 +0100 | [diff] [blame] | 643 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 644 | }; |
| 645 | #endif |