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/pci.h>
13 #include <linux/platform_device.h>
14 #include <linux/reset.h>
15 #include <linux/resource.h>
16 #include <linux/types.h>
17 #include <linux/phy/phy.h>
18 #include <linux/mod_devicetable.h>
19 #include <linux/module.h>
21 #include "pcie-designware.h"
23 #define to_meson_pcie(x) dev_get_drvdata((x)->dev)
25 #define PCIE_CAP_MAX_PAYLOAD_SIZE(x) ((x) << 5)
26 #define PCIE_CAP_MAX_READ_REQ_SIZE(x) ((x) << 12)
28 /* PCIe specific config registers */
30 #define APP_LTSSM_ENABLE BIT(7)
32 #define PCIE_CFG_STATUS12 0x30
33 #define IS_SMLH_LINK_UP(x) ((x) & (1 << 6))
34 #define IS_RDLH_LINK_UP(x) ((x) & (1 << 16))
35 #define IS_LTSSM_UP(x) ((((x) >> 10) & 0x1f) == 0x11)
37 #define PCIE_CFG_STATUS17 0x44
38 #define PM_CURRENT_STATE(x) (((x) >> 7) & 0x1)
40 #define WAIT_LINKUP_TIMEOUT 4000
41 #define PORT_CLK_RATE 100000000UL
42 #define MAX_PAYLOAD_SIZE 256
43 #define MAX_READ_REQ_SIZE 256
44 #define PCIE_RESET_DELAY 500
45 #define PCIE_SHARED_RESET 1
46 #define PCIE_NORMAL_RESET 0
55 struct meson_pcie_clk_res {
58 struct clk *general_clk;
61 struct meson_pcie_rc_reset {
62 struct reset_control *port;
63 struct reset_control *apb;
68 void __iomem *cfg_base;
69 struct meson_pcie_clk_res clk_res;
70 struct meson_pcie_rc_reset mrst;
71 struct gpio_desc *reset_gpio;
75 static struct reset_control *meson_pcie_get_reset(struct meson_pcie *mp,
79 struct device *dev = mp->pci.dev;
80 struct reset_control *reset;
82 if (reset_type == PCIE_SHARED_RESET)
83 reset = devm_reset_control_get_shared(dev, id);
85 reset = devm_reset_control_get(dev, id);
90 static int meson_pcie_get_resets(struct meson_pcie *mp)
92 struct meson_pcie_rc_reset *mrst = &mp->mrst;
94 mrst->port = meson_pcie_get_reset(mp, "port", PCIE_NORMAL_RESET);
95 if (IS_ERR(mrst->port))
96 return PTR_ERR(mrst->port);
97 reset_control_deassert(mrst->port);
99 mrst->apb = meson_pcie_get_reset(mp, "apb", PCIE_SHARED_RESET);
100 if (IS_ERR(mrst->apb))
101 return PTR_ERR(mrst->apb);
102 reset_control_deassert(mrst->apb);
107 static int meson_pcie_get_mems(struct platform_device *pdev,
108 struct meson_pcie *mp)
110 struct dw_pcie *pci = &mp->pci;
112 pci->dbi_base = devm_platform_ioremap_resource_byname(pdev, "elbi");
113 if (IS_ERR(pci->dbi_base))
114 return PTR_ERR(pci->dbi_base);
116 mp->cfg_base = devm_platform_ioremap_resource_byname(pdev, "cfg");
117 if (IS_ERR(mp->cfg_base))
118 return PTR_ERR(mp->cfg_base);
123 static int meson_pcie_power_on(struct meson_pcie *mp)
127 ret = phy_init(mp->phy);
131 ret = phy_power_on(mp->phy);
140 static void meson_pcie_power_off(struct meson_pcie *mp)
142 phy_power_off(mp->phy);
146 static int meson_pcie_reset(struct meson_pcie *mp)
148 struct meson_pcie_rc_reset *mrst = &mp->mrst;
151 ret = phy_reset(mp->phy);
155 reset_control_assert(mrst->port);
156 reset_control_assert(mrst->apb);
157 udelay(PCIE_RESET_DELAY);
158 reset_control_deassert(mrst->port);
159 reset_control_deassert(mrst->apb);
160 udelay(PCIE_RESET_DELAY);
165 static inline void meson_pcie_disable_clock(void *data)
167 struct clk *clk = data;
169 clk_disable_unprepare(clk);
172 static inline struct clk *meson_pcie_probe_clock(struct device *dev,
173 const char *id, u64 rate)
178 clk = devm_clk_get(dev, id);
183 ret = clk_set_rate(clk, rate);
185 dev_err(dev, "set clk rate failed, ret = %d\n", ret);
190 ret = clk_prepare_enable(clk);
192 dev_err(dev, "couldn't enable clk\n");
196 devm_add_action_or_reset(dev, meson_pcie_disable_clock, clk);
201 static int meson_pcie_probe_clocks(struct meson_pcie *mp)
203 struct device *dev = mp->pci.dev;
204 struct meson_pcie_clk_res *res = &mp->clk_res;
206 res->port_clk = meson_pcie_probe_clock(dev, "port", PORT_CLK_RATE);
207 if (IS_ERR(res->port_clk))
208 return PTR_ERR(res->port_clk);
210 res->general_clk = meson_pcie_probe_clock(dev, "general", 0);
211 if (IS_ERR(res->general_clk))
212 return PTR_ERR(res->general_clk);
214 res->clk = meson_pcie_probe_clock(dev, "pclk", 0);
215 if (IS_ERR(res->clk))
216 return PTR_ERR(res->clk);
221 static inline u32 meson_cfg_readl(struct meson_pcie *mp, u32 reg)
223 return readl(mp->cfg_base + reg);
226 static inline void meson_cfg_writel(struct meson_pcie *mp, u32 val, u32 reg)
228 writel(val, mp->cfg_base + reg);
231 static void meson_pcie_assert_reset(struct meson_pcie *mp)
233 gpiod_set_value_cansleep(mp->reset_gpio, 1);
235 gpiod_set_value_cansleep(mp->reset_gpio, 0);
238 static void meson_pcie_ltssm_enable(struct meson_pcie *mp)
242 val = meson_cfg_readl(mp, PCIE_CFG0);
243 val |= APP_LTSSM_ENABLE;
244 meson_cfg_writel(mp, val, PCIE_CFG0);
247 static int meson_size_to_payload(struct meson_pcie *mp, int size)
249 struct device *dev = mp->pci.dev;
252 * dwc supports 2^(val+7) payload size, which val is 0~5 default to 1.
253 * So if input size is not 2^order alignment or less than 2^7 or bigger
254 * than 2^12, just set to default size 2^(1+7).
256 if (!is_power_of_2(size) || size < 128 || size > 4096) {
257 dev_warn(dev, "payload size %d, set to default 256\n", size);
261 return fls(size) - 8;
264 static void meson_set_max_payload(struct meson_pcie *mp, int size)
266 struct dw_pcie *pci = &mp->pci;
268 u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
269 int max_payload_size = meson_size_to_payload(mp, size);
271 val = dw_pcie_readl_dbi(pci, offset + PCI_EXP_DEVCTL);
272 val &= ~PCI_EXP_DEVCTL_PAYLOAD;
273 dw_pcie_writel_dbi(pci, offset + PCI_EXP_DEVCTL, val);
275 val = dw_pcie_readl_dbi(pci, offset + PCI_EXP_DEVCTL);
276 val |= PCIE_CAP_MAX_PAYLOAD_SIZE(max_payload_size);
277 dw_pcie_writel_dbi(pci, offset + PCI_EXP_DEVCTL, val);
280 static void meson_set_max_rd_req_size(struct meson_pcie *mp, int size)
282 struct dw_pcie *pci = &mp->pci;
284 u16 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
285 int max_rd_req_size = meson_size_to_payload(mp, size);
287 val = dw_pcie_readl_dbi(pci, offset + PCI_EXP_DEVCTL);
288 val &= ~PCI_EXP_DEVCTL_READRQ;
289 dw_pcie_writel_dbi(pci, offset + PCI_EXP_DEVCTL, val);
291 val = dw_pcie_readl_dbi(pci, offset + PCI_EXP_DEVCTL);
292 val |= PCIE_CAP_MAX_READ_REQ_SIZE(max_rd_req_size);
293 dw_pcie_writel_dbi(pci, offset + PCI_EXP_DEVCTL, val);
296 static int meson_pcie_start_link(struct dw_pcie *pci)
298 struct meson_pcie *mp = to_meson_pcie(pci);
300 meson_pcie_ltssm_enable(mp);
301 meson_pcie_assert_reset(mp);
306 static int meson_pcie_rd_own_conf(struct pci_bus *bus, u32 devfn,
307 int where, int size, u32 *val)
311 ret = pci_generic_config_read(bus, devfn, where, size, val);
312 if (ret != PCIBIOS_SUCCESSFUL)
316 * There is a bug in the MESON AXG PCIe controller whereby software
317 * cannot program the PCI_CLASS_DEVICE register, so we must fabricate
318 * the return value in the config accessors.
320 if ((where & ~3) == PCI_CLASS_REVISION) {
322 *val = (*val & ((1 << (size * 8)) - 1)) << (8 * (where & 3));
324 *val |= PCI_CLASS_BRIDGE_PCI_NORMAL << 8;
326 *val = (*val >> (8 * (where & 3))) & ((1 << (size * 8)) - 1);
329 return PCIBIOS_SUCCESSFUL;
332 static struct pci_ops meson_pci_ops = {
333 .map_bus = dw_pcie_own_conf_map_bus,
334 .read = meson_pcie_rd_own_conf,
335 .write = pci_generic_config_write,
338 static int meson_pcie_link_up(struct dw_pcie *pci)
340 struct meson_pcie *mp = to_meson_pcie(pci);
341 struct device *dev = pci->dev;
344 u32 state12, state17, smlh_up, ltssm_up, rdlh_up;
347 state12 = meson_cfg_readl(mp, PCIE_CFG_STATUS12);
348 state17 = meson_cfg_readl(mp, PCIE_CFG_STATUS17);
349 smlh_up = IS_SMLH_LINK_UP(state12);
350 rdlh_up = IS_RDLH_LINK_UP(state12);
351 ltssm_up = IS_LTSSM_UP(state12);
353 if (PM_CURRENT_STATE(state17) < PCIE_GEN3)
357 dev_dbg(dev, "smlh_link_up is on\n");
359 dev_dbg(dev, "rdlh_link_up is on\n");
361 dev_dbg(dev, "ltssm_up is on\n");
363 dev_dbg(dev, "speed_okay\n");
365 if (smlh_up && rdlh_up && ltssm_up && speed_okay)
371 } while (cnt < WAIT_LINKUP_TIMEOUT);
373 dev_err(dev, "error: wait linkup timeout\n");
377 static int meson_pcie_host_init(struct dw_pcie_rp *pp)
379 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
380 struct meson_pcie *mp = to_meson_pcie(pci);
382 pp->bridge->ops = &meson_pci_ops;
384 meson_set_max_payload(mp, MAX_PAYLOAD_SIZE);
385 meson_set_max_rd_req_size(mp, MAX_READ_REQ_SIZE);
390 static const struct dw_pcie_host_ops meson_pcie_host_ops = {
391 .init = meson_pcie_host_init,
394 static const struct dw_pcie_ops dw_pcie_ops = {
395 .link_up = meson_pcie_link_up,
396 .start_link = meson_pcie_start_link,
399 static int meson_pcie_probe(struct platform_device *pdev)
401 struct device *dev = &pdev->dev;
403 struct meson_pcie *mp;
406 mp = devm_kzalloc(dev, sizeof(*mp), GFP_KERNEL);
412 pci->ops = &dw_pcie_ops;
413 pci->pp.ops = &meson_pcie_host_ops;
416 mp->phy = devm_phy_get(dev, "pcie");
417 if (IS_ERR(mp->phy)) {
418 dev_err(dev, "get phy failed, %ld\n", PTR_ERR(mp->phy));
419 return PTR_ERR(mp->phy);
422 mp->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
423 if (IS_ERR(mp->reset_gpio)) {
424 dev_err(dev, "get reset gpio failed\n");
425 return PTR_ERR(mp->reset_gpio);
428 ret = meson_pcie_get_resets(mp);
430 dev_err(dev, "get reset resource failed, %d\n", ret);
434 ret = meson_pcie_get_mems(pdev, mp);
436 dev_err(dev, "get memory resource failed, %d\n", ret);
440 ret = meson_pcie_power_on(mp);
442 dev_err(dev, "phy power on failed, %d\n", ret);
446 ret = meson_pcie_reset(mp);
448 dev_err(dev, "reset failed, %d\n", ret);
452 ret = meson_pcie_probe_clocks(mp);
454 dev_err(dev, "init clock resources failed, %d\n", ret);
458 platform_set_drvdata(pdev, mp);
460 ret = dw_pcie_host_init(&pci->pp);
462 dev_err(dev, "Add PCIe port failed, %d\n", ret);
469 meson_pcie_power_off(mp);
473 static const struct of_device_id meson_pcie_of_match[] = {
475 .compatible = "amlogic,axg-pcie",
478 .compatible = "amlogic,g12a-pcie",
482 MODULE_DEVICE_TABLE(of, meson_pcie_of_match);
484 static struct platform_driver meson_pcie_driver = {
485 .probe = meson_pcie_probe,
487 .name = "meson-pcie",
488 .of_match_table = meson_pcie_of_match,
492 module_platform_driver(meson_pcie_driver);
494 MODULE_AUTHOR("Yue Wang <yue.wang@amlogic.com>");
495 MODULE_DESCRIPTION("Amlogic PCIe Controller driver");
496 MODULE_LICENSE("GPL v2");