Yang Xiwen | d971711 | 2023-08-23 01:03:42 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Hisilicon Fast Ethernet MAC Driver |
| 4 | * Adapted from linux |
| 5 | * |
| 6 | * Copyright (c) 2016 HiSilicon Technologies Co., Ltd. |
| 7 | * Copyright (c) 2023 Yang Xiwen <forbidden405@outlook.com> |
| 8 | */ |
| 9 | |
| 10 | #include <dm.h> |
| 11 | #include <clk.h> |
| 12 | #include <miiphy.h> |
| 13 | #include <net.h> |
| 14 | #include <reset.h> |
| 15 | #include <wait_bit.h> |
| 16 | #include <asm/io.h> |
| 17 | #include <dm/device_compat.h> |
Yang Xiwen | 7579ad1 | 2024-01-22 22:33:22 +0800 | [diff] [blame] | 18 | #include <dm/lists.h> |
Yang Xiwen | f9db9d7 | 2024-01-22 22:33:23 +0800 | [diff] [blame] | 19 | #include <linux/bitfield.h> |
| 20 | #include <linux/ethtool.h> |
Yang Xiwen | d971711 | 2023-08-23 01:03:42 +0800 | [diff] [blame] | 21 | #include <linux/delay.h> |
| 22 | #include <linux/kernel.h> |
| 23 | |
| 24 | /* MAC control register list */ |
| 25 | #define MAC_PORTSEL 0x0200 |
| 26 | #define MAC_PORTSEL_STAT_CPU BIT(0) |
| 27 | #define MAC_PORTSEL_RMII BIT(1) |
| 28 | #define MAC_PORTSET 0x0208 |
| 29 | #define MAC_PORTSET_DUPLEX_FULL BIT(0) |
| 30 | #define MAC_PORTSET_LINKED BIT(1) |
| 31 | #define MAC_PORTSET_SPEED_100M BIT(2) |
| 32 | #define MAC_SET 0x0210 |
| 33 | #define MAX_FRAME_SIZE 1600 |
| 34 | #define MAX_FRAME_SIZE_MASK GENMASK(10, 0) |
| 35 | #define BIT_PAUSE_EN BIT(18) |
| 36 | #define RX_COALESCE_SET 0x0340 |
| 37 | #define RX_COALESCED_FRAME_OFFSET 24 |
| 38 | #define RX_COALESCED_FRAMES 8 |
| 39 | #define RX_COALESCED_TIMER 0x74 |
| 40 | #define QLEN_SET 0x0344 |
| 41 | #define RX_DEPTH_OFFSET 8 |
| 42 | #define MAX_HW_FIFO_DEPTH 64 |
| 43 | #define HW_TX_FIFO_DEPTH 1 |
| 44 | #define MAX_HW_RX_FIFO_DEPTH (MAX_HW_FIFO_DEPTH - HW_TX_FIFO_DEPTH) |
| 45 | #define HW_RX_FIFO_DEPTH min(PKTBUFSRX, MAX_HW_RX_FIFO_DEPTH) |
| 46 | #define IQFRM_DES 0x0354 |
| 47 | #define RX_FRAME_LEN_MASK GENMASK(11, 0) |
| 48 | #define RX_FRAME_IN_INDEX_MASK GENMASK(17, 12) |
| 49 | #define IQ_ADDR 0x0358 |
| 50 | #define EQ_ADDR 0x0360 |
| 51 | #define EQFRM_LEN 0x0364 |
| 52 | #define ADDRQ_STAT 0x036C |
| 53 | #define TX_CNT_INUSE_MASK GENMASK(5, 0) |
| 54 | #define BIT_TX_READY BIT(24) |
| 55 | #define BIT_RX_READY BIT(25) |
| 56 | /* global control register list */ |
| 57 | #define GLB_HOSTMAC_L32 0x0000 |
| 58 | #define GLB_HOSTMAC_H16 0x0004 |
| 59 | #define GLB_SOFT_RESET 0x0008 |
| 60 | #define SOFT_RESET_ALL BIT(0) |
| 61 | #define GLB_FWCTRL 0x0010 |
| 62 | #define FWCTRL_VLAN_ENABLE BIT(0) |
| 63 | #define FWCTRL_FW2CPU_ENA BIT(5) |
| 64 | #define FWCTRL_FWALL2CPU BIT(7) |
| 65 | #define GLB_MACTCTRL 0x0014 |
| 66 | #define MACTCTRL_UNI2CPU BIT(1) |
| 67 | #define MACTCTRL_MULTI2CPU BIT(3) |
| 68 | #define MACTCTRL_BROAD2CPU BIT(5) |
| 69 | #define MACTCTRL_MACT_ENA BIT(7) |
| 70 | #define GLB_IRQ_STAT 0x0030 |
| 71 | #define GLB_IRQ_ENA 0x0034 |
| 72 | #define IRQ_ENA_PORT0_MASK GENMASK(7, 0) |
| 73 | #define IRQ_ENA_PORT0 BIT(18) |
| 74 | #define IRQ_ENA_ALL BIT(19) |
| 75 | #define GLB_IRQ_RAW 0x0038 |
| 76 | #define IRQ_INT_RX_RDY BIT(0) |
| 77 | #define IRQ_INT_TX_PER_PACKET BIT(1) |
| 78 | #define IRQ_INT_TX_FIFO_EMPTY BIT(6) |
| 79 | #define IRQ_INT_MULTI_RXRDY BIT(7) |
| 80 | #define DEF_INT_MASK (IRQ_INT_MULTI_RXRDY | \ |
| 81 | IRQ_INT_TX_PER_PACKET | \ |
| 82 | IRQ_INT_TX_FIFO_EMPTY) |
| 83 | #define GLB_MAC_L32_BASE 0x0100 |
| 84 | #define GLB_MAC_H16_BASE 0x0104 |
| 85 | #define MACFLT_HI16_MASK GENMASK(15, 0) |
| 86 | #define BIT_MACFLT_ENA BIT(17) |
| 87 | #define BIT_MACFLT_FW2CPU BIT(21) |
| 88 | #define GLB_MAC_H16(reg) (GLB_MAC_H16_BASE + ((reg) * 0x8)) |
| 89 | #define GLB_MAC_L32(reg) (GLB_MAC_L32_BASE + ((reg) * 0x8)) |
| 90 | #define MAX_MAC_FILTER_NUM 8 |
| 91 | #define MAX_UNICAST_ADDRESSES 2 |
| 92 | #define MAX_MULTICAST_ADDRESSES (MAX_MAC_FILTER_NUM - \ |
| 93 | MAX_UNICAST_ADDRESSES) |
| 94 | /* software tx and rx queue number, should be power of 2 */ |
| 95 | #define TXQ_NUM 64 |
| 96 | #define RXQ_NUM 128 |
| 97 | |
| 98 | #define PHY_RESET_DELAYS_PROPERTY "hisilicon,phy-reset-delays-us" |
| 99 | #define MAC_RESET_DELAY_PROPERTY "hisilicon,mac-reset-delay-us" |
| 100 | #define MAC_RESET_ASSERT_PERIOD 200000 |
| 101 | |
| 102 | enum phy_reset_delays { |
| 103 | PRE_DELAY, |
| 104 | PULSE, |
| 105 | POST_DELAY, |
| 106 | DELAYS_NUM, |
| 107 | }; |
| 108 | |
| 109 | enum clk_type { |
| 110 | CLK_MAC, |
| 111 | CLK_BUS, |
| 112 | CLK_PHY, |
| 113 | CLK_NUM, |
| 114 | }; |
| 115 | |
| 116 | struct hisi_femac_priv { |
| 117 | void __iomem *port_base; |
| 118 | void __iomem *glb_base; |
| 119 | struct clk *clks[CLK_NUM]; |
| 120 | struct reset_ctl *mac_rst; |
| 121 | struct reset_ctl *phy_rst; |
| 122 | u32 phy_reset_delays[DELAYS_NUM]; |
| 123 | u32 mac_reset_delay; |
| 124 | |
| 125 | struct phy_device *phy; |
| 126 | |
| 127 | u32 link_status; |
| 128 | }; |
| 129 | |
Yang Xiwen | f9db9d7 | 2024-01-22 22:33:23 +0800 | [diff] [blame] | 130 | struct hisi_femac_stat_entry { |
| 131 | const char *name; |
| 132 | u32 offset; |
| 133 | u32 mask; |
| 134 | }; |
| 135 | |
| 136 | /* please refer to the datasheet for the description of these entries */ |
| 137 | static const struct hisi_femac_stat_entry hisi_femac_stats_table[] = { |
| 138 | { "rxsof_cnt", 0x584, GENMASK(31, 28) }, |
| 139 | { "rxeof_cnt", 0x584, GENMASK(27, 24) }, |
| 140 | { "rxcrcok_cnt", 0x584, GENMASK(23, 20) }, |
| 141 | { "rxcrcbad_cnt", 0x584, GENMASK(19, 16) }, |
| 142 | { "txsof_cnt", 0x584, GENMASK(15, 12) }, |
| 143 | { "txeof_cnt", 0x584, GENMASK(11, 8) }, |
| 144 | { "txcrcok_cnt", 0x584, GENMASK(7, 4) }, |
| 145 | { "txcrcbad_cnt", 0x584, GENMASK(3, 0) }, |
| 146 | { "pkts_cpu", 0x5a0, GENMASK(15, 0) }, |
| 147 | { "addr_cpu", 0x5a4, GENMASK(15, 0) }, |
| 148 | { "pkts_port", 0x5a8, GENMASK(15, 0) }, |
| 149 | { "pkts_cpu2tx", 0x5ac, GENMASK(15, 0) }, |
| 150 | { "rxdvrise", 0x600, GENMASK(31, 0) }, |
| 151 | { "ifinoctets", 0x604, GENMASK(31, 0) }, |
| 152 | { "octets_rx", 0x608, GENMASK(31, 0) }, |
| 153 | { "local_mac_match", 0x60c, GENMASK(31, 0) }, |
| 154 | { "pkts", 0x610, GENMASK(31, 0) }, |
| 155 | { "broadcastpkts", 0x614, GENMASK(31, 0) }, |
| 156 | { "multicastpkts", 0x618, GENMASK(31, 0) }, |
| 157 | { "ifinucastpkts", 0x61c, GENMASK(31, 0) }, |
| 158 | { "ifinerrors", 0x620, GENMASK(31, 0) }, |
| 159 | { "crcerr", 0x624, GENMASK(31, 0) }, |
| 160 | { "abnormalsizepkts", 0x628, GENMASK(31, 0) }, |
| 161 | { "dot3alignmenterr", 0x62c, GENMASK(31, 0) }, |
| 162 | { "dot3pause", 0x630, GENMASK(31, 0) }, |
| 163 | { "dropevents", 0x634, GENMASK(31, 0) }, |
| 164 | { "flux_frame_cnt", 0x638, GENMASK(31, 0) }, |
| 165 | { "flux_drop_cnt", 0x63c, GENMASK(31, 0) }, |
| 166 | { "mac_not2cpu_pkts", 0x64c, GENMASK(31, 0) }, |
| 167 | { "pkts_tx", 0x780, GENMASK(31, 0) }, |
| 168 | { "broadcastpkts_tx", 0x784, GENMASK(31, 0) }, |
| 169 | { "multicastpkts_tx", 0x788, GENMASK(31, 0) }, |
| 170 | { "ifoutucastpkts_tx", 0x78c, GENMASK(31, 0) }, |
| 171 | { "octets_tx", 0x790, GENMASK(31, 0) }, |
| 172 | { "dot3pause", 0x794, GENMASK(31, 0) }, |
| 173 | { "retry_times_tx", 0x798, GENMASK(31, 0) }, |
| 174 | { "collisions", 0x79c, GENMASK(31, 0) }, |
| 175 | { "dot3latecol", 0x7a0, GENMASK(31, 0) }, |
| 176 | { "dot3colok", 0x7a4, GENMASK(31, 0) }, |
| 177 | { "dot3excessivecol", 0x7a8, GENMASK(31, 0) }, |
| 178 | { "dot3colcnt", 0x7ac, GENMASK(31, 0) }, |
| 179 | }; |
| 180 | |
Yang Xiwen | d971711 | 2023-08-23 01:03:42 +0800 | [diff] [blame] | 181 | static void hisi_femac_irq_enable(struct hisi_femac_priv *priv, int irqs) |
| 182 | { |
| 183 | u32 val; |
| 184 | |
| 185 | val = readl(priv->glb_base + GLB_IRQ_ENA); |
| 186 | writel(val | irqs, priv->glb_base + GLB_IRQ_ENA); |
| 187 | } |
| 188 | |
| 189 | static void hisi_femac_irq_disable(struct hisi_femac_priv *priv, int irqs) |
| 190 | { |
| 191 | u32 val; |
| 192 | |
| 193 | val = readl(priv->glb_base + GLB_IRQ_ENA); |
| 194 | writel(val & (~irqs), priv->glb_base + GLB_IRQ_ENA); |
| 195 | } |
| 196 | |
| 197 | static void hisi_femac_port_init(struct hisi_femac_priv *priv) |
| 198 | { |
| 199 | u32 val; |
| 200 | |
| 201 | /* MAC gets link status info and phy mode by software config */ |
| 202 | val = MAC_PORTSEL_STAT_CPU; |
| 203 | if (priv->phy->interface == PHY_INTERFACE_MODE_RMII) |
| 204 | val |= MAC_PORTSEL_RMII; |
| 205 | writel(val, priv->port_base + MAC_PORTSEL); |
| 206 | |
| 207 | /*clear all interrupt status */ |
| 208 | writel(IRQ_ENA_PORT0_MASK, priv->glb_base + GLB_IRQ_RAW); |
| 209 | hisi_femac_irq_disable(priv, IRQ_ENA_PORT0_MASK | IRQ_ENA_PORT0); |
| 210 | |
| 211 | val = readl(priv->glb_base + GLB_FWCTRL); |
| 212 | val &= ~(FWCTRL_VLAN_ENABLE | FWCTRL_FWALL2CPU); |
| 213 | val |= FWCTRL_FW2CPU_ENA; |
| 214 | writel(val, priv->glb_base + GLB_FWCTRL); |
| 215 | |
| 216 | val = readl(priv->glb_base + GLB_MACTCTRL); |
| 217 | val |= (MACTCTRL_BROAD2CPU | MACTCTRL_MACT_ENA); |
| 218 | writel(val, priv->glb_base + GLB_MACTCTRL); |
| 219 | |
| 220 | val = readl(priv->port_base + MAC_SET); |
| 221 | val &= ~MAX_FRAME_SIZE_MASK; |
| 222 | val |= MAX_FRAME_SIZE; |
| 223 | writel(val, priv->port_base + MAC_SET); |
| 224 | |
| 225 | val = RX_COALESCED_TIMER | |
| 226 | (RX_COALESCED_FRAMES << RX_COALESCED_FRAME_OFFSET); |
| 227 | writel(val, priv->port_base + RX_COALESCE_SET); |
| 228 | |
| 229 | val = (HW_RX_FIFO_DEPTH << RX_DEPTH_OFFSET) | HW_TX_FIFO_DEPTH; |
| 230 | writel(val, priv->port_base + QLEN_SET); |
| 231 | } |
| 232 | |
| 233 | static void hisi_femac_rx_refill(struct hisi_femac_priv *priv) |
| 234 | { |
| 235 | int i; |
| 236 | ulong addr; |
| 237 | |
| 238 | for (i = 0; i < HW_RX_FIFO_DEPTH; i++) { |
| 239 | addr = (ulong)net_rx_packets[i]; |
| 240 | writel(addr, priv->port_base + IQ_ADDR); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | static void hisi_femac_adjust_link(struct udevice *dev) |
| 245 | { |
| 246 | struct hisi_femac_priv *priv = dev_get_priv(dev); |
| 247 | struct phy_device *phy = priv->phy; |
| 248 | u32 status = 0; |
| 249 | |
| 250 | if (phy->link) |
| 251 | status |= MAC_PORTSET_LINKED; |
| 252 | if (phy->duplex == DUPLEX_FULL) |
| 253 | status |= MAC_PORTSET_DUPLEX_FULL; |
| 254 | if (phy->speed == SPEED_100) |
| 255 | status |= MAC_PORTSET_SPEED_100M; |
| 256 | |
| 257 | writel(status, priv->port_base + MAC_PORTSET); |
| 258 | } |
| 259 | |
| 260 | static int hisi_femac_port_reset(struct hisi_femac_priv *priv) |
| 261 | { |
| 262 | u32 val; |
| 263 | |
| 264 | val = readl(priv->glb_base + GLB_SOFT_RESET); |
| 265 | val |= SOFT_RESET_ALL; |
| 266 | writel(val, priv->glb_base + GLB_SOFT_RESET); |
| 267 | |
| 268 | udelay(800); |
| 269 | |
| 270 | val &= ~SOFT_RESET_ALL; |
| 271 | writel(val, priv->glb_base + GLB_SOFT_RESET); |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | static int hisi_femac_set_hw_mac_addr(struct udevice *dev) |
| 277 | { |
| 278 | struct hisi_femac_priv *priv = dev_get_priv(dev); |
| 279 | struct eth_pdata *plat = dev_get_plat(dev); |
| 280 | unsigned char *mac = plat->enetaddr; |
| 281 | u32 reg; |
| 282 | |
| 283 | reg = mac[1] | (mac[0] << 8); |
| 284 | writel(reg, priv->glb_base + GLB_HOSTMAC_H16); |
| 285 | |
| 286 | reg = mac[5] | (mac[4] << 8) | (mac[3] << 16) | (mac[2] << 24); |
| 287 | writel(reg, priv->glb_base + GLB_HOSTMAC_L32); |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | static int hisi_femac_start(struct udevice *dev) |
| 293 | { |
| 294 | int ret; |
| 295 | struct hisi_femac_priv *priv = dev_get_priv(dev); |
| 296 | |
| 297 | hisi_femac_port_reset(priv); |
| 298 | hisi_femac_set_hw_mac_addr(dev); |
| 299 | hisi_femac_rx_refill(priv); |
| 300 | |
| 301 | ret = phy_startup(priv->phy); |
Yang Xiwen | f56b07b | 2024-01-22 22:33:21 +0800 | [diff] [blame] | 302 | if (ret) { |
| 303 | dev_err(dev, "Failed to startup phy: %d\n", ret); |
| 304 | return log_msg_ret("phy", ret); |
| 305 | } |
Yang Xiwen | d971711 | 2023-08-23 01:03:42 +0800 | [diff] [blame] | 306 | |
| 307 | if (!priv->phy->link) { |
| 308 | debug("%s: link down\n", __func__); |
| 309 | return -ENODEV; |
| 310 | } |
| 311 | |
| 312 | hisi_femac_adjust_link(dev); |
| 313 | |
| 314 | writel(IRQ_ENA_PORT0_MASK, priv->glb_base + GLB_IRQ_RAW); |
| 315 | hisi_femac_irq_enable(priv, IRQ_ENA_ALL | IRQ_ENA_PORT0 | DEF_INT_MASK); |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static int hisi_femac_send(struct udevice *dev, void *packet, int length) |
| 321 | { |
| 322 | struct hisi_femac_priv *priv = dev_get_priv(dev); |
| 323 | ulong addr = (ulong)packet; |
| 324 | int ret; |
| 325 | |
| 326 | // clear previous irq |
| 327 | writel(IRQ_INT_TX_PER_PACKET, priv->glb_base + GLB_IRQ_RAW); |
| 328 | |
| 329 | // flush cache |
| 330 | flush_cache(addr, length + ETH_FCS_LEN); |
| 331 | |
| 332 | // write packet address |
| 333 | writel(addr, priv->port_base + EQ_ADDR); |
| 334 | |
| 335 | // write packet length (and send it) |
| 336 | writel(length + ETH_FCS_LEN, priv->port_base + EQFRM_LEN); |
| 337 | |
| 338 | // wait until FIFO is empty |
| 339 | ret = wait_for_bit_le32(priv->glb_base + GLB_IRQ_RAW, IRQ_INT_TX_PER_PACKET, true, 50, false); |
Yang Xiwen | f56b07b | 2024-01-22 22:33:21 +0800 | [diff] [blame] | 340 | if (ret == -ETIMEDOUT) { |
| 341 | dev_err(dev, "FIFO timeout\n"); |
| 342 | return log_msg_ret("net", ret); |
| 343 | } |
Yang Xiwen | d971711 | 2023-08-23 01:03:42 +0800 | [diff] [blame] | 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | static int hisi_femac_recv(struct udevice *dev, int flags, uchar **packetp) |
| 349 | { |
| 350 | struct hisi_femac_priv *priv = dev_get_priv(dev); |
| 351 | int val, index, length; |
| 352 | |
| 353 | val = readl(priv->glb_base + GLB_IRQ_RAW); |
| 354 | if (!(val & IRQ_INT_RX_RDY)) |
| 355 | return -EAGAIN; |
| 356 | |
| 357 | val = readl(priv->port_base + IQFRM_DES); |
| 358 | index = (val & RX_FRAME_IN_INDEX_MASK) >> 12; |
| 359 | length = val & RX_FRAME_LEN_MASK; |
| 360 | |
| 361 | // invalidate cache |
| 362 | invalidate_dcache_range((ulong)net_rx_packets[index], (ulong)net_rx_packets[index] + length); |
| 363 | *packetp = net_rx_packets[index]; |
| 364 | |
| 365 | // Tell hardware we will process the packet |
| 366 | writel(IRQ_INT_RX_RDY, priv->glb_base + GLB_IRQ_RAW); |
| 367 | |
| 368 | return length; |
| 369 | } |
| 370 | |
| 371 | static int hisi_femac_free_pkt(struct udevice *dev, uchar *packet, int length) |
| 372 | { |
| 373 | struct hisi_femac_priv *priv = dev_get_priv(dev); |
| 374 | ulong addr = (ulong)packet; |
| 375 | |
| 376 | // Tell hardware the packet can be reused |
| 377 | writel(addr, priv->port_base + IQ_ADDR); |
| 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | static void hisi_femac_stop(struct udevice *dev) |
| 383 | { |
| 384 | struct hisi_femac_priv *priv = dev_get_priv(dev); |
| 385 | |
| 386 | // assert internal reset |
| 387 | writel(SOFT_RESET_ALL, priv->glb_base + GLB_SOFT_RESET); |
| 388 | } |
| 389 | |
Yang Xiwen | f9db9d7 | 2024-01-22 22:33:23 +0800 | [diff] [blame] | 390 | static int hisi_femac_get_sset_count(struct udevice *dev) |
| 391 | { |
| 392 | return ARRAY_SIZE(hisi_femac_stats_table); |
| 393 | } |
| 394 | |
| 395 | static void hisi_femac_get_strings(struct udevice *dev, u8 *data) |
| 396 | { |
| 397 | int i; |
| 398 | |
| 399 | for (i = 0; i < ARRAY_SIZE(hisi_femac_stats_table); i++) |
| 400 | strcpy(data + i * ETH_GSTRING_LEN, hisi_femac_stats_table[i].name); |
| 401 | } |
| 402 | |
| 403 | /* Non-constant mask variant of FIELD_GET/FIELD_PREP */ |
| 404 | #define field_get(_mask, _reg) (((_reg) & (_mask)) >> (ffs(_mask) - 1)) |
| 405 | |
| 406 | static void hisi_femac_get_stats(struct udevice *dev, u64 *data) |
| 407 | { |
| 408 | int i; |
| 409 | u32 mask, reg; |
| 410 | struct hisi_femac_priv *priv = dev_get_priv(dev); |
| 411 | void __iomem *port_base = priv->port_base; |
| 412 | |
| 413 | for (i = 0; i < ARRAY_SIZE(hisi_femac_stats_table); i++) { |
| 414 | mask = hisi_femac_stats_table[i].mask; |
| 415 | reg = readl(port_base + hisi_femac_stats_table[i].offset); |
| 416 | |
| 417 | data[i] = field_get(mask, reg); |
| 418 | } |
| 419 | } |
| 420 | |
Yang Xiwen | 581b2f5 | 2024-01-22 22:33:24 +0800 | [diff] [blame] | 421 | static int hisi_femac_of_to_plat(struct udevice *dev) |
Yang Xiwen | d971711 | 2023-08-23 01:03:42 +0800 | [diff] [blame] | 422 | { |
| 423 | int ret, i; |
| 424 | struct hisi_femac_priv *priv = dev_get_priv(dev); |
Yang Xiwen | 7579ad1 | 2024-01-22 22:33:22 +0800 | [diff] [blame] | 425 | ofnode mdio_node; |
| 426 | bool mdio_registered = false; |
Yang Xiwen | d971711 | 2023-08-23 01:03:42 +0800 | [diff] [blame] | 427 | static const char * const clk_strs[] = { |
| 428 | [CLK_MAC] = "mac", |
| 429 | [CLK_BUS] = "bus", |
| 430 | [CLK_PHY] = "phy", |
| 431 | }; |
| 432 | |
| 433 | priv->port_base = dev_remap_addr_name(dev, "port"); |
Yang Xiwen | f56b07b | 2024-01-22 22:33:21 +0800 | [diff] [blame] | 434 | if (!priv->port_base) { |
| 435 | dev_err(dev, "Failed to remap port address space\n"); |
| 436 | return log_msg_ret("net", -EINVAL); |
| 437 | } |
Yang Xiwen | d971711 | 2023-08-23 01:03:42 +0800 | [diff] [blame] | 438 | |
| 439 | priv->glb_base = dev_remap_addr_name(dev, "glb"); |
Yang Xiwen | f56b07b | 2024-01-22 22:33:21 +0800 | [diff] [blame] | 440 | if (IS_ERR(priv->glb_base)) { |
| 441 | dev_err(dev, "Failed to remap global address space\n"); |
| 442 | return log_msg_ret("net", -EINVAL); |
| 443 | } |
Yang Xiwen | d971711 | 2023-08-23 01:03:42 +0800 | [diff] [blame] | 444 | |
| 445 | for (i = 0; i < ARRAY_SIZE(clk_strs); i++) { |
| 446 | priv->clks[i] = devm_clk_get(dev, clk_strs[i]); |
| 447 | if (IS_ERR(priv->clks[i])) { |
| 448 | dev_err(dev, "Error getting clock %s\n", clk_strs[i]); |
Yang Xiwen | f56b07b | 2024-01-22 22:33:21 +0800 | [diff] [blame] | 449 | return log_msg_ret("clk", PTR_ERR(priv->clks[i])); |
Yang Xiwen | d971711 | 2023-08-23 01:03:42 +0800 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
| 453 | priv->mac_rst = devm_reset_control_get(dev, "mac"); |
Yang Xiwen | f56b07b | 2024-01-22 22:33:21 +0800 | [diff] [blame] | 454 | if (IS_ERR(priv->mac_rst)) { |
| 455 | dev_err(dev, "Failed to get MAC reset %ld\n", PTR_ERR(priv->mac_rst)); |
| 456 | return log_msg_ret("rst", PTR_ERR(priv->mac_rst)); |
| 457 | } |
Yang Xiwen | d971711 | 2023-08-23 01:03:42 +0800 | [diff] [blame] | 458 | |
| 459 | priv->phy_rst = devm_reset_control_get(dev, "phy"); |
Yang Xiwen | f56b07b | 2024-01-22 22:33:21 +0800 | [diff] [blame] | 460 | if (IS_ERR(priv->phy_rst)) { |
| 461 | dev_err(dev, "Failed to get PHY reset %ld\n", PTR_ERR(priv->phy_rst)); |
| 462 | return log_msg_ret("rst", PTR_ERR(priv->phy_rst)); |
| 463 | } |
Yang Xiwen | d971711 | 2023-08-23 01:03:42 +0800 | [diff] [blame] | 464 | |
| 465 | ret = dev_read_u32_array(dev, |
| 466 | PHY_RESET_DELAYS_PROPERTY, |
| 467 | priv->phy_reset_delays, |
| 468 | DELAYS_NUM); |
Yang Xiwen | f56b07b | 2024-01-22 22:33:21 +0800 | [diff] [blame] | 469 | if (ret < 0) { |
| 470 | dev_err(dev, "Failed to get PHY reset delays %d\n", ret); |
| 471 | return log_msg_ret("rst", ret); |
| 472 | } |
Yang Xiwen | d971711 | 2023-08-23 01:03:42 +0800 | [diff] [blame] | 473 | |
| 474 | priv->mac_reset_delay = dev_read_u32_default(dev, |
| 475 | MAC_RESET_DELAY_PROPERTY, |
| 476 | MAC_RESET_ASSERT_PERIOD); |
| 477 | |
Yang Xiwen | 7579ad1 | 2024-01-22 22:33:22 +0800 | [diff] [blame] | 478 | /* Create MDIO bus */ |
| 479 | ofnode_for_each_subnode(mdio_node, dev_ofnode(dev)) { |
| 480 | const char *subnode_name = ofnode_get_name(mdio_node); |
| 481 | struct udevice *mdiodev; |
| 482 | |
| 483 | // Skip subnodes not starting with "mdio" |
| 484 | if (strncmp(subnode_name, "mdio", 4)) |
| 485 | continue; |
| 486 | |
| 487 | ret = device_bind_driver_to_node(dev, "hisi-femac-mdio", |
| 488 | subnode_name, mdio_node, &mdiodev); |
| 489 | if (ret) { |
| 490 | dev_err(dev, "Failed to register MDIO bus device %d\n", ret); |
| 491 | return log_msg_ret("net", ret); |
| 492 | } |
| 493 | |
| 494 | mdio_registered = true; |
| 495 | break; |
| 496 | } |
| 497 | |
| 498 | if (!mdio_registered) { |
| 499 | dev_err(dev, "No MDIO subnode is found!\n"); |
| 500 | return log_msg_ret("mdio", -ENODATA); |
| 501 | } |
| 502 | |
Yang Xiwen | d971711 | 2023-08-23 01:03:42 +0800 | [diff] [blame] | 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | static int hisi_femac_phy_reset(struct hisi_femac_priv *priv) |
| 507 | { |
| 508 | struct reset_ctl *rst = priv->phy_rst; |
| 509 | u32 *delays = priv->phy_reset_delays; |
| 510 | int ret; |
| 511 | |
| 512 | // Disable MAC clk before phy reset |
| 513 | ret = clk_disable(priv->clks[CLK_MAC]); |
Yang Xiwen | f56b07b | 2024-01-22 22:33:21 +0800 | [diff] [blame] | 514 | if (ret < 0) { |
| 515 | pr_err("%s: Failed to disable MAC clock %d\n", __func__, ret); |
| 516 | return log_msg_ret("clk", ret); |
| 517 | } |
Yang Xiwen | d971711 | 2023-08-23 01:03:42 +0800 | [diff] [blame] | 518 | ret = clk_disable(priv->clks[CLK_BUS]); |
Yang Xiwen | f56b07b | 2024-01-22 22:33:21 +0800 | [diff] [blame] | 519 | if (ret < 0) { |
| 520 | pr_err("%s: Failed to disable bus clock %d\n", __func__, ret); |
| 521 | return log_msg_ret("clk", ret); |
| 522 | } |
Yang Xiwen | d971711 | 2023-08-23 01:03:42 +0800 | [diff] [blame] | 523 | |
| 524 | udelay(delays[PRE_DELAY]); |
| 525 | |
| 526 | ret = reset_assert(rst); |
Yang Xiwen | f56b07b | 2024-01-22 22:33:21 +0800 | [diff] [blame] | 527 | if (ret < 0) { |
| 528 | pr_err("%s: Failed to assert reset %d\n", __func__, ret); |
| 529 | return log_msg_ret("rst", ret); |
| 530 | } |
Yang Xiwen | d971711 | 2023-08-23 01:03:42 +0800 | [diff] [blame] | 531 | |
| 532 | udelay(delays[PULSE]); |
| 533 | |
| 534 | ret = reset_deassert(rst); |
Yang Xiwen | f56b07b | 2024-01-22 22:33:21 +0800 | [diff] [blame] | 535 | if (ret < 0) { |
| 536 | pr_err("%s: Failed to deassert reset %d\n", __func__, ret); |
| 537 | return log_msg_ret("rst", ret); |
| 538 | } |
Yang Xiwen | d971711 | 2023-08-23 01:03:42 +0800 | [diff] [blame] | 539 | |
| 540 | udelay(delays[POST_DELAY]); |
| 541 | |
| 542 | ret = clk_enable(priv->clks[CLK_MAC]); |
Yang Xiwen | f56b07b | 2024-01-22 22:33:21 +0800 | [diff] [blame] | 543 | if (ret < 0) { |
| 544 | pr_err("%s: Failed to enable MAC clock %d\n", __func__, ret); |
| 545 | return log_msg_ret("clk", ret); |
| 546 | } |
Yang Xiwen | d971711 | 2023-08-23 01:03:42 +0800 | [diff] [blame] | 547 | ret = clk_enable(priv->clks[CLK_BUS]); |
Yang Xiwen | f56b07b | 2024-01-22 22:33:21 +0800 | [diff] [blame] | 548 | if (ret < 0) { |
| 549 | pr_err("%s: Failed to enable MAC bus clock %d\n", __func__, ret); |
| 550 | return log_msg_ret("clk", ret); |
| 551 | } |
Yang Xiwen | d971711 | 2023-08-23 01:03:42 +0800 | [diff] [blame] | 552 | |
| 553 | return 0; |
| 554 | } |
| 555 | |
Yang Xiwen | 581b2f5 | 2024-01-22 22:33:24 +0800 | [diff] [blame] | 556 | static int hisi_femac_probe(struct udevice *dev) |
Yang Xiwen | d971711 | 2023-08-23 01:03:42 +0800 | [diff] [blame] | 557 | { |
| 558 | struct hisi_femac_priv *priv = dev_get_priv(dev); |
| 559 | int ret, i; |
| 560 | |
| 561 | // Enable clocks |
| 562 | for (i = 0; i < CLK_NUM; i++) { |
| 563 | ret = clk_prepare_enable(priv->clks[i]); |
Yang Xiwen | f56b07b | 2024-01-22 22:33:21 +0800 | [diff] [blame] | 564 | if (ret < 0) { |
| 565 | dev_err(dev, "Failed to enable clk %d: %d\n", i, ret); |
| 566 | return log_msg_ret("clk", ret); |
| 567 | } |
Yang Xiwen | d971711 | 2023-08-23 01:03:42 +0800 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | // Reset MAC |
| 571 | ret = reset_assert(priv->mac_rst); |
Yang Xiwen | f56b07b | 2024-01-22 22:33:21 +0800 | [diff] [blame] | 572 | if (ret < 0) { |
| 573 | dev_err(dev, "Failed to assert MAC reset: %d\n", ret); |
| 574 | return log_msg_ret("net", ret); |
| 575 | } |
Yang Xiwen | d971711 | 2023-08-23 01:03:42 +0800 | [diff] [blame] | 576 | |
| 577 | udelay(priv->mac_reset_delay); |
| 578 | |
| 579 | ret = reset_deassert(priv->mac_rst); |
Yang Xiwen | f56b07b | 2024-01-22 22:33:21 +0800 | [diff] [blame] | 580 | if (ret < 0) { |
| 581 | dev_err(dev, "Failed to deassert MAC reset: %d\n", ret); |
| 582 | return log_msg_ret("net", ret); |
| 583 | } |
Yang Xiwen | d971711 | 2023-08-23 01:03:42 +0800 | [diff] [blame] | 584 | |
| 585 | // Reset PHY |
| 586 | ret = hisi_femac_phy_reset(priv); |
Yang Xiwen | f56b07b | 2024-01-22 22:33:21 +0800 | [diff] [blame] | 587 | if (ret < 0) { |
| 588 | dev_err(dev, "Failed to reset PHY: %d\n", ret); |
| 589 | return log_msg_ret("net", ret); |
| 590 | } |
Yang Xiwen | d971711 | 2023-08-23 01:03:42 +0800 | [diff] [blame] | 591 | |
| 592 | // Connect to PHY |
| 593 | priv->phy = dm_eth_phy_connect(dev); |
Yang Xiwen | f56b07b | 2024-01-22 22:33:21 +0800 | [diff] [blame] | 594 | if (!priv->phy) { |
| 595 | dev_err(dev, "Failed to connect to phy\n"); |
| 596 | return log_msg_ret("phy", -EINVAL); |
| 597 | } |
Yang Xiwen | d971711 | 2023-08-23 01:03:42 +0800 | [diff] [blame] | 598 | |
| 599 | hisi_femac_port_init(priv); |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | static const struct eth_ops hisi_femac_ops = { |
| 604 | .start = hisi_femac_start, |
| 605 | .send = hisi_femac_send, |
| 606 | .recv = hisi_femac_recv, |
| 607 | .free_pkt = hisi_femac_free_pkt, |
| 608 | .stop = hisi_femac_stop, |
| 609 | .write_hwaddr = hisi_femac_set_hw_mac_addr, |
Yang Xiwen | f9db9d7 | 2024-01-22 22:33:23 +0800 | [diff] [blame] | 610 | .get_sset_count = hisi_femac_get_sset_count, |
| 611 | .get_strings = hisi_femac_get_strings, |
| 612 | .get_stats = hisi_femac_get_stats, |
Yang Xiwen | d971711 | 2023-08-23 01:03:42 +0800 | [diff] [blame] | 613 | }; |
| 614 | |
| 615 | static const struct udevice_id hisi_femac_ids[] = { |
| 616 | {.compatible = "hisilicon,hisi-femac-v1",}, |
| 617 | {.compatible = "hisilicon,hisi-femac-v2",}, |
| 618 | {.compatible = "hisilicon,hi3516cv300-femac",}, |
| 619 | {.compatible = "hisilicon,hi3798mv200-femac",}, |
| 620 | {}, |
| 621 | }; |
| 622 | |
| 623 | U_BOOT_DRIVER(hisi_femac_driver) = { |
| 624 | .name = "eth_hisi_femac", |
| 625 | .id = UCLASS_ETH, |
| 626 | .of_match = of_match_ptr(hisi_femac_ids), |
| 627 | .of_to_plat = hisi_femac_of_to_plat, |
| 628 | .ops = &hisi_femac_ops, |
| 629 | .probe = hisi_femac_probe, |
| 630 | .plat_auto = sizeof(struct eth_pdata), |
| 631 | .priv_auto = sizeof(struct hisi_femac_priv), |
| 632 | }; |