1 // SPDX-License-Identifier: GPL-2.0+
3 * Amlogic DesignWare based PCIe host controller driver
5 * Copyright (c) 2021 BayLibre, SAS
6 * Author: Neil Armstrong <narmstrong@baylibre.com>
8 * Based on pcie_dw_rockchip.c
9 * Copyright (c) 2021 Rockchip, Inc.
15 #include <generic-phy.h>
17 #include <power-domain.h>
20 #include <asm/global_data.h>
22 #include <asm-generic/gpio.h>
23 #include <dm/device_compat.h>
24 #include <linux/iopoll.h>
25 #include <linux/delay.h>
26 #include <linux/log2.h>
27 #include <linux/bitfield.h>
29 #include "pcie_dw_common.h"
31 DECLARE_GLOBAL_DATA_PTR;
34 * struct meson_pcie - Amlogic Meson DW PCIe controller state
36 * @pci: The common PCIe DW structure
37 * @meson_cfg_base: The base address of vendor regs
43 * @rst_gpio: The #PERST signal for slot
46 /* Must be first member of the struct */
51 struct clk clk_general;
53 struct reset_ctl_bulk rsts;
54 struct gpio_desc rst_gpio;
57 #define PCI_EXP_DEVCTL_PAYLOAD 0x00e0 /* Max_Payload_Size */
59 #define PCIE_CAP_MAX_PAYLOAD_SIZE(x) ((x) << 5)
60 #define PCIE_CAP_MAX_READ_REQ_SIZE(x) ((x) << 12)
62 /* PCIe specific config registers */
64 #define APP_LTSSM_ENABLE BIT(7)
66 #define PCIE_CFG_STATUS12 0x30
67 #define IS_SMLH_LINK_UP(x) ((x) & (1 << 6))
68 #define IS_RDLH_LINK_UP(x) ((x) & (1 << 16))
69 #define IS_LTSSM_UP(x) ((((x) >> 10) & 0x1f) == 0x11)
71 #define PCIE_CFG_STATUS17 0x44
72 #define PM_CURRENT_STATE(x) (((x) >> 7) & 0x1)
74 #define WAIT_LINKUP_TIMEOUT 4000
75 #define PORT_CLK_RATE 100000000UL
76 #define MAX_PAYLOAD_SIZE 256
77 #define MAX_READ_REQ_SIZE 256
78 #define PCIE_RESET_DELAY 500
79 #define PCIE_SHARED_RESET 1
80 #define PCIE_NORMAL_RESET 0
89 /* Parameters for the waiting for #perst signal */
90 #define PERST_WAIT_US 1000000
92 static inline u32 meson_cfg_readl(struct meson_pcie *priv, u32 reg)
94 return readl(priv->meson_cfg_base + reg);
97 static inline void meson_cfg_writel(struct meson_pcie *priv, u32 val, u32 reg)
99 writel(val, priv->meson_cfg_base + reg);
103 * meson_pcie_configure() - Configure link
105 * @meson_pcie: Pointer to the PCI controller state
107 * Configure the link mode and width
109 static void meson_pcie_configure(struct meson_pcie *priv)
113 dw_pcie_dbi_write_enable(&priv->dw, true);
115 val = readl(priv->dw.dbi_base + PCIE_PORT_LINK_CONTROL);
116 val &= ~PORT_LINK_FAST_LINK_MODE;
117 val |= PORT_LINK_DLL_LINK_EN;
118 val &= ~PORT_LINK_MODE_MASK;
119 val |= PORT_LINK_MODE_1_LANES;
120 writel(val, priv->dw.dbi_base + PCIE_PORT_LINK_CONTROL);
122 val = readl(priv->dw.dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
123 val &= ~PORT_LOGIC_LINK_WIDTH_MASK;
124 val |= PORT_LOGIC_LINK_WIDTH_1_LANES;
125 writel(val, priv->dw.dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
127 dw_pcie_dbi_write_enable(&priv->dw, false);
130 static inline void meson_pcie_enable_ltssm(struct meson_pcie *priv)
134 val = meson_cfg_readl(priv, PCIE_CFG0);
135 val |= APP_LTSSM_ENABLE;
136 meson_cfg_writel(priv, val, PCIE_CFG0);
139 static int meson_pcie_wait_link_up(struct meson_pcie *priv)
143 u32 state12, state17, smlh_up, ltssm_up, rdlh_up;
146 state12 = meson_cfg_readl(priv, PCIE_CFG_STATUS12);
147 state17 = meson_cfg_readl(priv, PCIE_CFG_STATUS17);
148 smlh_up = IS_SMLH_LINK_UP(state12);
149 rdlh_up = IS_RDLH_LINK_UP(state12);
150 ltssm_up = IS_LTSSM_UP(state12);
152 if (PM_CURRENT_STATE(state17) < PCIE_GEN3)
156 debug("%s: smlh_link_up is on\n", __func__);
158 debug("%s: rdlh_link_up is on\n", __func__);
160 debug("%s: ltssm_up is on\n", __func__);
162 debug("%s: speed_okay\n", __func__);
164 if (smlh_up && rdlh_up && ltssm_up && speed_okay)
170 } while (cnt < WAIT_LINKUP_TIMEOUT);
172 printf("%s: error: wait linkup timeout\n", __func__);
177 * meson_pcie_link_up() - Wait for the link to come up
179 * @meson_pcie: Pointer to the PCI controller state
180 * @cap_speed: Desired link speed
182 * Return: 1 (true) for active line and negative (false) for no link (timeout)
184 static int meson_pcie_link_up(struct meson_pcie *priv, u32 cap_speed)
186 /* DW link configurations */
187 meson_pcie_configure(priv);
189 /* Reset the device */
190 if (dm_gpio_is_valid(&priv->rst_gpio)) {
191 dm_gpio_set_value(&priv->rst_gpio, 1);
193 * Minimal is 100ms from spec but we see
194 * some wired devices need much more, such as 600ms.
195 * Add a enough delay to cover all cases.
197 udelay(PERST_WAIT_US);
198 dm_gpio_set_value(&priv->rst_gpio, 0);
202 meson_pcie_enable_ltssm(priv);
204 return meson_pcie_wait_link_up(priv);
207 static int meson_size_to_payload(int size)
210 * dwc supports 2^(val+7) payload size, which val is 0~5 default to 1.
211 * So if input size is not 2^order alignment or less than 2^7 or bigger
212 * than 2^12, just set to default size 2^(1+7).
214 if (!is_power_of_2(size) || size < 128 || size > 4096) {
215 debug("%s: payload size %d, set to default 256\n", __func__, size);
219 return fls(size) - 8;
222 static void meson_set_max_payload(struct meson_pcie *priv, int size)
225 u16 offset = dm_pci_find_capability(priv->dw.dev, PCI_CAP_ID_EXP);
226 int max_payload_size = meson_size_to_payload(size);
228 dw_pcie_dbi_write_enable(&priv->dw, true);
230 val = readl(priv->dw.dbi_base + offset + PCI_EXP_DEVCTL);
231 val &= ~PCI_EXP_DEVCTL_PAYLOAD;
232 writel(val, priv->dw.dbi_base + offset + PCI_EXP_DEVCTL);
234 val = readl(priv->dw.dbi_base + offset + PCI_EXP_DEVCTL);
235 val |= PCIE_CAP_MAX_PAYLOAD_SIZE(max_payload_size);
236 writel(val, priv->dw.dbi_base + PCI_EXP_DEVCTL);
238 dw_pcie_dbi_write_enable(&priv->dw, false);
241 static void meson_set_max_rd_req_size(struct meson_pcie *priv, int size)
244 u16 offset = dm_pci_find_capability(priv->dw.dev, PCI_CAP_ID_EXP);
245 int max_rd_req_size = meson_size_to_payload(size);
247 dw_pcie_dbi_write_enable(&priv->dw, true);
249 val = readl(priv->dw.dbi_base + offset + PCI_EXP_DEVCTL);
250 val &= ~PCI_EXP_DEVCTL_PAYLOAD;
251 writel(val, priv->dw.dbi_base + offset + PCI_EXP_DEVCTL);
253 val = readl(priv->dw.dbi_base + offset + PCI_EXP_DEVCTL);
254 val |= PCIE_CAP_MAX_READ_REQ_SIZE(max_rd_req_size);
255 writel(val, priv->dw.dbi_base + PCI_EXP_DEVCTL);
257 dw_pcie_dbi_write_enable(&priv->dw, false);
260 static int meson_pcie_init_port(struct udevice *dev)
263 struct meson_pcie *priv = dev_get_priv(dev);
265 ret = generic_phy_init(&priv->phy);
267 dev_err(dev, "failed to init phy (ret=%d)\n", ret);
271 ret = generic_phy_power_on(&priv->phy);
273 dev_err(dev, "failed to power on phy (ret=%d)\n", ret);
277 ret = generic_phy_reset(&priv->phy);
279 dev_err(dev, "failed to reset phy (ret=%d)\n", ret);
283 ret = reset_assert_bulk(&priv->rsts);
285 dev_err(dev, "failed to assert resets (ret=%d)\n", ret);
286 goto err_power_off_phy;
289 udelay(PCIE_RESET_DELAY);
291 ret = reset_deassert_bulk(&priv->rsts);
293 dev_err(dev, "failed to deassert resets (ret=%d)\n", ret);
294 goto err_power_off_phy;
297 udelay(PCIE_RESET_DELAY);
299 ret = clk_set_rate(&priv->clk_port, PORT_CLK_RATE);
301 dev_err(dev, "failed to set port clk rate (ret=%d)\n", ret);
302 goto err_deassert_bulk;
305 ret = clk_enable(&priv->clk_general);
307 dev_err(dev, "failed to enable clk general (ret=%d)\n", ret);
308 goto err_deassert_bulk;
311 ret = clk_enable(&priv->clk_pclk);
313 dev_err(dev, "failed to enable pclk (ret=%d)\n", ret);
314 goto err_deassert_bulk;
317 meson_set_max_payload(priv, MAX_PAYLOAD_SIZE);
318 meson_set_max_rd_req_size(priv, MAX_READ_REQ_SIZE);
320 pcie_dw_setup_host(&priv->dw);
322 meson_pcie_link_up(priv, LINK_SPEED_GEN_2);
326 reset_assert_bulk(&priv->rsts);
328 generic_phy_power_off(&priv->phy);
330 generic_phy_exit(&priv->phy);
335 static int meson_pcie_parse_dt(struct udevice *dev)
337 struct meson_pcie *priv = dev_get_priv(dev);
340 priv->dw.dbi_base = dev_read_addr_index_ptr(dev, 0);
341 if (!priv->dw.dbi_base)
344 dev_dbg(dev, "ELBI address is 0x%p\n", priv->dw.dbi_base);
346 priv->meson_cfg_base = dev_read_addr_index_ptr(dev, 1);
347 if (!priv->meson_cfg_base)
350 dev_dbg(dev, "CFG address is 0x%p\n", priv->meson_cfg_base);
352 ret = gpio_request_by_name(dev, "reset-gpios", 0,
353 &priv->rst_gpio, GPIOD_IS_OUT);
355 dev_err(dev, "failed to find reset-gpios property\n");
359 ret = reset_get_bulk(dev, &priv->rsts);
361 dev_err(dev, "Can't get reset: %d\n", ret);
365 ret = clk_get_by_name(dev, "port", &priv->clk_port);
367 dev_err(dev, "Can't get port clock: %d\n", ret);
371 ret = clk_get_by_name(dev, "general", &priv->clk_general);
373 dev_err(dev, "Can't get port clock: %d\n", ret);
377 ret = clk_get_by_name(dev, "pclk", &priv->clk_pclk);
379 dev_err(dev, "Can't get port clock: %d\n", ret);
383 ret = generic_phy_get_by_index(dev, 0, &priv->phy);
385 dev_err(dev, "failed to get pcie phy (ret=%d)\n", ret);
393 * meson_pcie_probe() - Probe the PCIe bus for active link
395 * @dev: A pointer to the device being operated on
397 * Probe for an active link on the PCIe bus and configure the controller
398 * to enable this port.
400 * Return: 0 on success, else -ENODEV
402 static int meson_pcie_probe(struct udevice *dev)
404 struct meson_pcie *priv = dev_get_priv(dev);
405 struct udevice *ctlr = pci_get_controller(dev);
406 struct pci_controller *hose = dev_get_uclass_priv(ctlr);
409 priv->dw.first_busno = dev_seq(dev);
412 ret = meson_pcie_parse_dt(dev);
416 ret = meson_pcie_init_port(dev);
418 dm_gpio_free(dev, &priv->rst_gpio);
422 printf("PCIE-%d: Link up (Gen%d-x%d, Bus%d)\n",
423 dev_seq(dev), pcie_dw_get_link_speed(&priv->dw),
424 pcie_dw_get_link_width(&priv->dw),
427 return pcie_dw_prog_outbound_atu_unroll(&priv->dw,
428 PCIE_ATU_REGION_INDEX0,
430 priv->dw.mem.phys_start,
431 priv->dw.mem.bus_start,
435 static const struct dm_pci_ops meson_pcie_ops = {
436 .read_config = pcie_dw_read_config,
437 .write_config = pcie_dw_write_config,
440 static const struct udevice_id meson_pcie_ids[] = {
441 { .compatible = "amlogic,axg-pcie" },
442 { .compatible = "amlogic,g12a-pcie" },
446 U_BOOT_DRIVER(meson_dw_pcie) = {
447 .name = "pcie_dw_meson",
449 .of_match = meson_pcie_ids,
450 .ops = &meson_pcie_ops,
451 .probe = meson_pcie_probe,
452 .priv_auto = sizeof(struct meson_pcie),