1 // SPDX-License-Identifier: GPL-2.0
3 * PCIe host controller driver for Amlogic MESON SoCs
5 * Copyright (c) 2018 Amlogic, inc.
6 * Author: Yue Wang <yue.wang@amlogic.com>
10 #include <linux/delay.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/of_gpio.h>
13 #include <linux/pci.h>
14 #include <linux/platform_device.h>
15 #include <linux/reset.h>
16 #include <linux/resource.h>
17 #include <linux/types.h>
18 #include <linux/phy/phy.h>
19 #include <linux/mod_devicetable.h>
20 #include <linux/module.h>
22 #include "pcie-designware.h"
24 #define to_meson_pcie(x) dev_get_drvdata((x)->dev)
26 #define PCIE_CAP_MAX_PAYLOAD_SIZE(x) ((x) << 5)
27 #define PCIE_CAP_MAX_READ_REQ_SIZE(x) ((x) << 12)
29 /* PCIe specific config registers */
31 #define APP_LTSSM_ENABLE BIT(7)
33 #define PCIE_CFG_STATUS12 0x30
34 #define IS_SMLH_LINK_UP(x) ((x) & (1 << 6))
35 #define IS_RDLH_LINK_UP(x) ((x) & (1 << 16))
36 #define IS_LTSSM_UP(x) ((((x) >> 10) & 0x1f) == 0x11)
38 #define PCIE_CFG_STATUS17 0x44
39 #define PM_CURRENT_STATE(x) (((x) >> 7) & 0x1)
41 #define WAIT_LINKUP_TIMEOUT 4000
42 #define PORT_CLK_RATE 100000000UL
43 #define MAX_PAYLOAD_SIZE 256
44 #define MAX_READ_REQ_SIZE 256
45 #define PCIE_RESET_DELAY 500
46 #define PCIE_SHARED_RESET 1
47 #define PCIE_NORMAL_RESET 0
56 struct meson_pcie_clk_res {
59 struct clk *general_clk;
62 struct meson_pcie_rc_reset {
63 struct reset_control *port;
64 struct reset_control *apb;
69 void __iomem *cfg_base;
70 struct meson_pcie_clk_res clk_res;
71 struct meson_pcie_rc_reset mrst;
72 struct gpio_desc *reset_gpio;
76 static struct reset_control *meson_pcie_get_reset(struct meson_pcie *mp,
80 struct device *dev = mp->pci.dev;
81 struct reset_control *reset;
83 if (reset_type == PCIE_SHARED_RESET)
84 reset = devm_reset_control_get_shared(dev, id);
86 reset = devm_reset_control_get(dev, id);
91 static int meson_pcie_get_resets(struct meson_pcie *mp)
93 struct meson_pcie_rc_reset *mrst = &mp->mrst;
95 mrst->port = meson_pcie_get_reset(mp, "port", PCIE_NORMAL_RESET);
96 if (IS_ERR(mrst->port))
97 return PTR_ERR(mrst->port);
98 reset_control_deassert(mrst->port);
100 mrst->apb = meson_pcie_get_reset(mp, "apb", PCIE_SHARED_RESET);
101 if (IS_ERR(mrst->apb))
102 return PTR_ERR(mrst->apb);
103 reset_control_deassert(mrst->apb);
108 static int meson_pcie_get_mems(struct platform_device *pdev,
109 struct meson_pcie *mp)
111 struct dw_pcie *pci = &mp->pci;
113 pci->dbi_base = devm_platform_ioremap_resource_byname(pdev, "elbi");
114 if (IS_ERR(pci->dbi_base))
115 return PTR_ERR(pci->dbi_base);
117 mp->cfg_base = devm_platform_ioremap_resource_byname(pdev, "cfg");
118 if (IS_ERR(mp->cfg_base))
119 return PTR_ERR(mp->cfg_base);
124 static int meson_pcie_power_on(struct meson_pcie *mp)
128 ret = phy_init(mp->phy);
132 ret = phy_power_on(mp->phy);
141 static void meson_pcie_power_off(struct meson_pcie *mp)
143 phy_power_off(mp->phy);
147 static int meson_pcie_reset(struct meson_pcie *mp)
149 struct meson_pcie_rc_reset *mrst = &mp->mrst;
152 ret = phy_reset(mp->phy);
156 reset_control_assert(mrst->port);
157 reset_control_assert(mrst->apb);
158 udelay(PCIE_RESET_DELAY);
159 reset_control_deassert(mrst->port);
160 reset_control_deassert(mrst->apb);
161 udelay(PCIE_RESET_DELAY);
166 static inline void meson_pcie_disable_clock(void *data)
168 struct clk *clk = data;
170 clk_disable_unprepare(clk);
173 static inline struct clk *meson_pcie_probe_clock(struct device *dev,
174 const char *id, u64 rate)
179 clk = devm_clk_get(dev, id);
184 ret = clk_set_rate(clk, rate);
186 dev_err(dev, "set clk rate failed, ret = %d\n", ret);
191 ret = clk_prepare_enable(clk);
193 dev_err(dev, "couldn't enable clk\n");
197 devm_add_action_or_reset(dev, meson_pcie_disable_clock, clk);
202 static int meson_pcie_probe_clocks(struct meson_pcie *mp)
204 struct device *dev = mp->pci.dev;
205 struct meson_pcie_clk_res *res = &mp->clk_res;
207 res->port_clk = meson_pcie_probe_clock(dev, "port", PORT_CLK_RATE);
208 if (IS_ERR(res->port_clk))
209 return PTR_ERR(res->port_clk);
211 res->general_clk = meson_pcie_probe_clock(dev, "general", 0);
212 if (IS_ERR(res->general_clk))
213 return PTR_ERR(res->general_clk);
215 res->clk = meson_pcie_probe_clock(dev, "pclk", 0);
216 if (IS_ERR(res->clk))
217 return PTR_ERR(res->clk);
222 static inline u32 meson_cfg_readl(struct meson_pcie *mp, u32 reg)
224 return readl(mp->cfg_base + reg);
227 static inline void meson_cfg_writel(struct meson_pcie *mp, u32 val, u32 reg)
229 writel(val, mp->cfg_base + reg);
232 static void meson_pcie_assert_reset(struct meson_pcie *mp)
234 gpiod_set_value_cansleep(mp->reset_gpio, 1);
236 gpiod_set_value_cansleep(mp->reset_gpio, 0);
239 static void meson_pcie_ltssm_enable(struct meson_pcie *mp)
243 val = meson_cfg_readl(mp, PCIE_CFG0);
244 val |= APP_LTSSM_ENABLE;
245 meson_cfg_writel(mp, val, PCIE_CFG0);
248 static int meson_size_to_payload(struct meson_pcie *mp, int size)
250 struct device *dev = mp->pci.dev;
253 * dwc supports 2^(val+7) payload size, which val is 0~5 default to 1.
254 * So if input size is not 2^order alignment or less than 2^7 or bigger
255 * than 2^12, just set to default size 2^(1+7).
257 if (!is_power_of_2(size) || size < 128 || size > 4096) {
258 dev_warn(dev, "payload size %d, set to default 256\n", size);
262 return fls(size) - 8;
265 static void meson_set_max_payload(struct meson_pcie *mp, int size)
267 struct dw_pcie *pci = &mp->pci;
269 u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
270 int max_payload_size = meson_size_to_payload(mp, size);
272 val = dw_pcie_readl_dbi(pci, offset + PCI_EXP_DEVCTL);
273 val &= ~PCI_EXP_DEVCTL_PAYLOAD;
274 dw_pcie_writel_dbi(pci, offset + PCI_EXP_DEVCTL, val);
276 val = dw_pcie_readl_dbi(pci, offset + PCI_EXP_DEVCTL);
277 val |= PCIE_CAP_MAX_PAYLOAD_SIZE(max_payload_size);
278 dw_pcie_writel_dbi(pci, offset + PCI_EXP_DEVCTL, val);
281 static void meson_set_max_rd_req_size(struct meson_pcie *mp, int size)
283 struct dw_pcie *pci = &mp->pci;
285 u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
286 int max_rd_req_size = meson_size_to_payload(mp, size);
288 val = dw_pcie_readl_dbi(pci, offset + PCI_EXP_DEVCTL);
289 val &= ~PCI_EXP_DEVCTL_READRQ;
290 dw_pcie_writel_dbi(pci, offset + PCI_EXP_DEVCTL, val);
292 val = dw_pcie_readl_dbi(pci, offset + PCI_EXP_DEVCTL);
293 val |= PCIE_CAP_MAX_READ_REQ_SIZE(max_rd_req_size);
294 dw_pcie_writel_dbi(pci, offset + PCI_EXP_DEVCTL, val);
297 static int meson_pcie_start_link(struct dw_pcie *pci)
299 struct meson_pcie *mp = to_meson_pcie(pci);
301 meson_pcie_ltssm_enable(mp);
302 meson_pcie_assert_reset(mp);
307 static int meson_pcie_rd_own_conf(struct pci_bus *bus, u32 devfn,
308 int where, int size, u32 *val)
312 ret = pci_generic_config_read(bus, devfn, where, size, val);
313 if (ret != PCIBIOS_SUCCESSFUL)
317 * There is a bug in the MESON AXG PCIe controller whereby software
318 * cannot program the PCI_CLASS_DEVICE register, so we must fabricate
319 * the return value in the config accessors.
321 if ((where & ~3) == PCI_CLASS_REVISION) {
323 *val = (*val & ((1 << (size * 8)) - 1)) << (8 * (where & 3));
325 *val |= PCI_CLASS_BRIDGE_PCI_NORMAL << 8;
327 *val = (*val >> (8 * (where & 3))) & ((1 << (size * 8)) - 1);
330 return PCIBIOS_SUCCESSFUL;
333 static struct pci_ops meson_pci_ops = {
334 .map_bus = dw_pcie_own_conf_map_bus,
335 .read = meson_pcie_rd_own_conf,
336 .write = pci_generic_config_write,
339 static int meson_pcie_link_up(struct dw_pcie *pci)
341 struct meson_pcie *mp = to_meson_pcie(pci);
342 struct device *dev = pci->dev;
345 u32 state12, state17, smlh_up, ltssm_up, rdlh_up;
348 state12 = meson_cfg_readl(mp, PCIE_CFG_STATUS12);
349 state17 = meson_cfg_readl(mp, PCIE_CFG_STATUS17);
350 smlh_up = IS_SMLH_LINK_UP(state12);
351 rdlh_up = IS_RDLH_LINK_UP(state12);
352 ltssm_up = IS_LTSSM_UP(state12);
354 if (PM_CURRENT_STATE(state17) < PCIE_GEN3)
358 dev_dbg(dev, "smlh_link_up is on\n");
360 dev_dbg(dev, "rdlh_link_up is on\n");
362 dev_dbg(dev, "ltssm_up is on\n");
364 dev_dbg(dev, "speed_okay\n");
366 if (smlh_up && rdlh_up && ltssm_up && speed_okay)
372 } while (cnt < WAIT_LINKUP_TIMEOUT);
374 dev_err(dev, "error: wait linkup timeout\n");
378 static int meson_pcie_host_init(struct dw_pcie_rp *pp)
380 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
381 struct meson_pcie *mp = to_meson_pcie(pci);
383 pp->bridge->ops = &meson_pci_ops;
385 meson_set_max_payload(mp, MAX_PAYLOAD_SIZE);
386 meson_set_max_rd_req_size(mp, MAX_READ_REQ_SIZE);
391 static const struct dw_pcie_host_ops meson_pcie_host_ops = {
392 .host_init = meson_pcie_host_init,
395 static const struct dw_pcie_ops dw_pcie_ops = {
396 .link_up = meson_pcie_link_up,
397 .start_link = meson_pcie_start_link,
400 static int meson_pcie_probe(struct platform_device *pdev)
402 struct device *dev = &pdev->dev;
404 struct meson_pcie *mp;
407 mp = devm_kzalloc(dev, sizeof(*mp), GFP_KERNEL);
413 pci->ops = &dw_pcie_ops;
414 pci->pp.ops = &meson_pcie_host_ops;
417 mp->phy = devm_phy_get(dev, "pcie");
418 if (IS_ERR(mp->phy)) {
419 dev_err(dev, "get phy failed, %ld\n", PTR_ERR(mp->phy));
420 return PTR_ERR(mp->phy);
423 mp->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
424 if (IS_ERR(mp->reset_gpio)) {
425 dev_err(dev, "get reset gpio failed\n");
426 return PTR_ERR(mp->reset_gpio);
429 ret = meson_pcie_get_resets(mp);
431 dev_err(dev, "get reset resource failed, %d\n", ret);
435 ret = meson_pcie_get_mems(pdev, mp);
437 dev_err(dev, "get memory resource failed, %d\n", ret);
441 ret = meson_pcie_power_on(mp);
443 dev_err(dev, "phy power on failed, %d\n", ret);
447 ret = meson_pcie_reset(mp);
449 dev_err(dev, "reset failed, %d\n", ret);
453 ret = meson_pcie_probe_clocks(mp);
455 dev_err(dev, "init clock resources failed, %d\n", ret);
459 platform_set_drvdata(pdev, mp);
461 ret = dw_pcie_host_init(&pci->pp);
463 dev_err(dev, "Add PCIe port failed, %d\n", ret);
470 meson_pcie_power_off(mp);
474 static const struct of_device_id meson_pcie_of_match[] = {
476 .compatible = "amlogic,axg-pcie",
479 .compatible = "amlogic,g12a-pcie",
483 MODULE_DEVICE_TABLE(of, meson_pcie_of_match);
485 static struct platform_driver meson_pcie_driver = {
486 .probe = meson_pcie_probe,
488 .name = "meson-pcie",
489 .of_match_table = meson_pcie_of_match,
493 module_platform_driver(meson_pcie_driver);
495 MODULE_AUTHOR("Yue Wang <yue.wang@amlogic.com>");
496 MODULE_DESCRIPTION("Amlogic PCIe Controller driver");
497 MODULE_LICENSE("GPL v2");