Peng Fan | 5721a82 | 2022-07-26 16:41:15 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright 2022 NXP |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <clk.h> |
| 8 | #include <cpu_func.h> |
| 9 | #include <dm.h> |
Marek Vasut | 6a9b7bf | 2023-03-06 15:53:47 +0100 | [diff] [blame] | 10 | #include <dm/device_compat.h> |
Peng Fan | 5721a82 | 2022-07-26 16:41:15 +0800 | [diff] [blame] | 11 | #include <errno.h> |
| 12 | #include <eth_phy.h> |
| 13 | #include <log.h> |
| 14 | #include <malloc.h> |
| 15 | #include <memalign.h> |
| 16 | #include <miiphy.h> |
| 17 | #include <net.h> |
| 18 | #include <netdev.h> |
| 19 | #include <phy.h> |
| 20 | #include <reset.h> |
| 21 | #include <wait_bit.h> |
| 22 | #include <asm/arch/clock.h> |
| 23 | #include <asm/cache.h> |
| 24 | #include <asm/gpio.h> |
| 25 | #include <asm/io.h> |
| 26 | #include <asm/mach-imx/sys_proto.h> |
| 27 | #include <linux/delay.h> |
| 28 | |
| 29 | #include "dwc_eth_qos.h" |
| 30 | |
| 31 | __weak u32 imx_get_eqos_csr_clk(void) |
| 32 | { |
| 33 | return 100 * 1000000; |
| 34 | } |
| 35 | |
Peng Fan | 5721a82 | 2022-07-26 16:41:15 +0800 | [diff] [blame] | 36 | static ulong eqos_get_tick_clk_rate_imx(struct udevice *dev) |
| 37 | { |
Marek Vasut | 6a9b7bf | 2023-03-06 15:53:47 +0100 | [diff] [blame] | 38 | struct eqos_priv *eqos = dev_get_priv(dev); |
| 39 | |
| 40 | return clk_get_rate(&eqos->clk_master_bus); |
Peng Fan | 5721a82 | 2022-07-26 16:41:15 +0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | static int eqos_probe_resources_imx(struct udevice *dev) |
| 44 | { |
| 45 | struct eqos_priv *eqos = dev_get_priv(dev); |
| 46 | phy_interface_t interface; |
Marek Vasut | 6a9b7bf | 2023-03-06 15:53:47 +0100 | [diff] [blame] | 47 | int ret; |
Peng Fan | 5721a82 | 2022-07-26 16:41:15 +0800 | [diff] [blame] | 48 | |
| 49 | debug("%s(dev=%p):\n", __func__, dev); |
| 50 | |
| 51 | interface = eqos->config->interface(dev); |
| 52 | |
| 53 | if (interface == PHY_INTERFACE_MODE_NA) { |
| 54 | pr_err("Invalid PHY interface\n"); |
| 55 | return -EINVAL; |
| 56 | } |
| 57 | |
Marek Vasut | 6a9b7bf | 2023-03-06 15:53:47 +0100 | [diff] [blame] | 58 | eqos->max_speed = dev_read_u32_default(dev, "max-speed", 0); |
| 59 | |
| 60 | ret = clk_get_by_name(dev, "stmmaceth", &eqos->clk_master_bus); |
| 61 | if (ret) { |
| 62 | dev_dbg(dev, "clk_get_by_name(master_bus) failed: %d", ret); |
| 63 | goto err_probe; |
| 64 | } |
| 65 | |
| 66 | ret = clk_get_by_name(dev, "ptp_ref", &eqos->clk_ptp_ref); |
| 67 | if (ret) { |
| 68 | dev_dbg(dev, "clk_get_by_name(ptp_ref) failed: %d", ret); |
| 69 | goto err_free_clk_master_bus; |
| 70 | } |
| 71 | |
| 72 | ret = clk_get_by_name(dev, "tx", &eqos->clk_tx); |
| 73 | if (ret) { |
| 74 | dev_dbg(dev, "clk_get_by_name(tx) failed: %d", ret); |
| 75 | goto err_free_clk_ptp_ref; |
| 76 | } |
| 77 | |
| 78 | ret = clk_get_by_name(dev, "pclk", &eqos->clk_ck); |
| 79 | if (ret) { |
| 80 | dev_dbg(dev, "clk_get_by_name(pclk) failed: %d", ret); |
| 81 | goto err_free_clk_tx; |
| 82 | } |
| 83 | |
Peng Fan | 5721a82 | 2022-07-26 16:41:15 +0800 | [diff] [blame] | 84 | debug("%s: OK\n", __func__); |
| 85 | return 0; |
Marek Vasut | 6a9b7bf | 2023-03-06 15:53:47 +0100 | [diff] [blame] | 86 | |
| 87 | err_free_clk_tx: |
| 88 | clk_free(&eqos->clk_tx); |
| 89 | err_free_clk_ptp_ref: |
| 90 | clk_free(&eqos->clk_ptp_ref); |
| 91 | err_free_clk_master_bus: |
| 92 | clk_free(&eqos->clk_master_bus); |
| 93 | err_probe: |
| 94 | |
| 95 | debug("%s: returns %d\n", __func__, ret); |
| 96 | return ret; |
| 97 | } |
| 98 | |
| 99 | static int eqos_remove_resources_imx(struct udevice *dev) |
| 100 | { |
| 101 | struct eqos_priv *eqos = dev_get_priv(dev); |
| 102 | |
| 103 | debug("%s(dev=%p):\n", __func__, dev); |
| 104 | |
| 105 | clk_free(&eqos->clk_ck); |
| 106 | clk_free(&eqos->clk_tx); |
| 107 | clk_free(&eqos->clk_ptp_ref); |
| 108 | clk_free(&eqos->clk_master_bus); |
| 109 | |
| 110 | debug("%s: OK\n", __func__); |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static int eqos_start_clks_imx(struct udevice *dev) |
| 115 | { |
| 116 | struct eqos_priv *eqos = dev_get_priv(dev); |
| 117 | int ret; |
| 118 | |
| 119 | debug("%s(dev=%p):\n", __func__, dev); |
| 120 | |
| 121 | ret = clk_enable(&eqos->clk_master_bus); |
| 122 | if (ret < 0) { |
| 123 | dev_dbg(dev, "clk_enable(clk_master_bus) failed: %d", ret); |
| 124 | goto err; |
| 125 | } |
| 126 | |
| 127 | ret = clk_enable(&eqos->clk_ptp_ref); |
| 128 | if (ret < 0) { |
| 129 | dev_dbg(dev, "clk_enable(clk_ptp_ref) failed: %d", ret); |
| 130 | goto err_disable_clk_master_bus; |
| 131 | } |
| 132 | |
| 133 | ret = clk_enable(&eqos->clk_tx); |
| 134 | if (ret < 0) { |
| 135 | dev_dbg(dev, "clk_enable(clk_tx) failed: %d", ret); |
| 136 | goto err_disable_clk_ptp_ref; |
| 137 | } |
| 138 | |
| 139 | ret = clk_enable(&eqos->clk_ck); |
| 140 | if (ret < 0) { |
| 141 | dev_dbg(dev, "clk_enable(clk_ck) failed: %d", ret); |
| 142 | goto err_disable_clk_tx; |
| 143 | } |
| 144 | |
| 145 | debug("%s: OK\n", __func__); |
| 146 | return 0; |
| 147 | |
| 148 | err_disable_clk_tx: |
| 149 | clk_disable(&eqos->clk_tx); |
| 150 | err_disable_clk_ptp_ref: |
| 151 | clk_disable(&eqos->clk_ptp_ref); |
| 152 | err_disable_clk_master_bus: |
| 153 | clk_disable(&eqos->clk_master_bus); |
| 154 | err: |
| 155 | debug("%s: FAILED: %d\n", __func__, ret); |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | static int eqos_stop_clks_imx(struct udevice *dev) |
| 160 | { |
| 161 | struct eqos_priv *eqos = dev_get_priv(dev); |
| 162 | |
| 163 | debug("%s(dev=%p):\n", __func__, dev); |
| 164 | |
| 165 | clk_disable(&eqos->clk_ck); |
| 166 | clk_disable(&eqos->clk_tx); |
| 167 | clk_disable(&eqos->clk_ptp_ref); |
| 168 | clk_disable(&eqos->clk_master_bus); |
| 169 | |
| 170 | debug("%s: OK\n", __func__); |
| 171 | return 0; |
Peng Fan | 5721a82 | 2022-07-26 16:41:15 +0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | static int eqos_set_tx_clk_speed_imx(struct udevice *dev) |
| 175 | { |
| 176 | struct eqos_priv *eqos = dev_get_priv(dev); |
| 177 | ulong rate; |
| 178 | int ret; |
| 179 | |
| 180 | debug("%s(dev=%p):\n", __func__, dev); |
| 181 | |
Marek Vasut | 03705e1 | 2023-03-06 15:53:48 +0100 | [diff] [blame^] | 182 | if (eqos->phy->interface == PHY_INTERFACE_MODE_RMII) |
| 183 | rate = 5000; /* 5000 kHz = 5 MHz */ |
| 184 | else |
| 185 | rate = 2500; /* 2500 kHz = 2.5 MHz */ |
| 186 | |
| 187 | if (eqos->phy->speed == SPEED_1000 && |
| 188 | (eqos->phy->interface == PHY_INTERFACE_MODE_RGMII || |
| 189 | eqos->phy->interface == PHY_INTERFACE_MODE_RGMII_ID || |
| 190 | eqos->phy->interface == PHY_INTERFACE_MODE_RGMII_RXID || |
| 191 | eqos->phy->interface == PHY_INTERFACE_MODE_RGMII_TXID)) { |
| 192 | rate *= 50; /* Use 50x base rate i.e. 125 MHz */ |
| 193 | } else if (eqos->phy->speed == SPEED_100) { |
| 194 | rate *= 10; /* Use 10x base rate */ |
| 195 | } else if (eqos->phy->speed == SPEED_10) { |
| 196 | rate *= 1; /* Use base rate */ |
| 197 | } else { |
Peng Fan | 5721a82 | 2022-07-26 16:41:15 +0800 | [diff] [blame] | 198 | pr_err("invalid speed %d", eqos->phy->speed); |
| 199 | return -EINVAL; |
| 200 | } |
| 201 | |
Marek Vasut | 03705e1 | 2023-03-06 15:53:48 +0100 | [diff] [blame^] | 202 | rate *= 1000; /* clk_set_rate() operates in Hz */ |
| 203 | |
Marek Vasut | 6a9b7bf | 2023-03-06 15:53:47 +0100 | [diff] [blame] | 204 | ret = clk_set_rate(&eqos->clk_tx, rate); |
Peng Fan | 5721a82 | 2022-07-26 16:41:15 +0800 | [diff] [blame] | 205 | if (ret < 0) { |
| 206 | pr_err("imx (tx_clk, %lu) failed: %d", rate, ret); |
| 207 | return ret; |
| 208 | } |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
Peng Fan | bf69a7b9 | 2022-07-26 16:41:17 +0800 | [diff] [blame] | 213 | static int eqos_get_enetaddr_imx(struct udevice *dev) |
| 214 | { |
| 215 | struct eth_pdata *pdata = dev_get_plat(dev); |
| 216 | |
| 217 | imx_get_mac_from_fuse(dev_seq(dev), pdata->enetaddr); |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
Peng Fan | 5721a82 | 2022-07-26 16:41:15 +0800 | [diff] [blame] | 222 | static struct eqos_ops eqos_imx_ops = { |
| 223 | .eqos_inval_desc = eqos_inval_desc_generic, |
| 224 | .eqos_flush_desc = eqos_flush_desc_generic, |
| 225 | .eqos_inval_buffer = eqos_inval_buffer_generic, |
| 226 | .eqos_flush_buffer = eqos_flush_buffer_generic, |
| 227 | .eqos_probe_resources = eqos_probe_resources_imx, |
Marek Vasut | 6a9b7bf | 2023-03-06 15:53:47 +0100 | [diff] [blame] | 228 | .eqos_remove_resources = eqos_remove_resources_imx, |
Peng Fan | 5721a82 | 2022-07-26 16:41:15 +0800 | [diff] [blame] | 229 | .eqos_stop_resets = eqos_null_ops, |
| 230 | .eqos_start_resets = eqos_null_ops, |
Marek Vasut | 6a9b7bf | 2023-03-06 15:53:47 +0100 | [diff] [blame] | 231 | .eqos_stop_clks = eqos_stop_clks_imx, |
| 232 | .eqos_start_clks = eqos_start_clks_imx, |
Peng Fan | 5721a82 | 2022-07-26 16:41:15 +0800 | [diff] [blame] | 233 | .eqos_calibrate_pads = eqos_null_ops, |
| 234 | .eqos_disable_calibration = eqos_null_ops, |
| 235 | .eqos_set_tx_clk_speed = eqos_set_tx_clk_speed_imx, |
Peng Fan | bf69a7b9 | 2022-07-26 16:41:17 +0800 | [diff] [blame] | 236 | .eqos_get_enetaddr = eqos_get_enetaddr_imx, |
| 237 | .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_imx, |
Peng Fan | 5721a82 | 2022-07-26 16:41:15 +0800 | [diff] [blame] | 238 | }; |
| 239 | |
| 240 | struct eqos_config __maybe_unused eqos_imx_config = { |
| 241 | .reg_access_always_ok = false, |
| 242 | .mdio_wait = 10, |
| 243 | .swr_wait = 50, |
| 244 | .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB, |
| 245 | .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_250_300, |
| 246 | .axi_bus_width = EQOS_AXI_WIDTH_64, |
| 247 | .interface = dev_read_phy_mode, |
| 248 | .ops = &eqos_imx_ops |
| 249 | }; |