PCI: meson: Build as module by default
[linux-2.6-microblaze.git] / drivers / pci / controller / dwc / pci-meson.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * PCIe host controller driver for Amlogic MESON SoCs
4  *
5  * Copyright (c) 2018 Amlogic, inc.
6  * Author: Yue Wang <yue.wang@amlogic.com>
7  */
8
9 #include <linux/clk.h>
10 #include <linux/delay.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/of_device.h>
13 #include <linux/of_gpio.h>
14 #include <linux/pci.h>
15 #include <linux/platform_device.h>
16 #include <linux/reset.h>
17 #include <linux/resource.h>
18 #include <linux/types.h>
19 #include <linux/phy/phy.h>
20 #include <linux/module.h>
21
22 #include "pcie-designware.h"
23
24 #define to_meson_pcie(x) dev_get_drvdata((x)->dev)
25
26 /* External local bus interface registers */
27 #define PLR_OFFSET                      0x700
28 #define PCIE_PORT_LINK_CTRL_OFF         (PLR_OFFSET + 0x10)
29 #define FAST_LINK_MODE                  BIT(7)
30 #define LINK_CAPABLE_MASK               GENMASK(21, 16)
31 #define LINK_CAPABLE_X1                 BIT(16)
32
33 #define PCIE_GEN2_CTRL_OFF              (PLR_OFFSET + 0x10c)
34 #define NUM_OF_LANES_MASK               GENMASK(12, 8)
35 #define NUM_OF_LANES_X1                 BIT(8)
36 #define DIRECT_SPEED_CHANGE             BIT(17)
37
38 #define TYPE1_HDR_OFFSET                0x0
39 #define PCIE_STATUS_COMMAND             (TYPE1_HDR_OFFSET + 0x04)
40 #define PCI_IO_EN                       BIT(0)
41 #define PCI_MEM_SPACE_EN                BIT(1)
42 #define PCI_BUS_MASTER_EN               BIT(2)
43
44 #define PCIE_BASE_ADDR0                 (TYPE1_HDR_OFFSET + 0x10)
45 #define PCIE_BASE_ADDR1                 (TYPE1_HDR_OFFSET + 0x14)
46
47 #define PCIE_CAP_OFFSET                 0x70
48 #define PCIE_DEV_CTRL_DEV_STUS          (PCIE_CAP_OFFSET + 0x08)
49 #define PCIE_CAP_MAX_PAYLOAD_MASK       GENMASK(7, 5)
50 #define PCIE_CAP_MAX_PAYLOAD_SIZE(x)    ((x) << 5)
51 #define PCIE_CAP_MAX_READ_REQ_MASK      GENMASK(14, 12)
52 #define PCIE_CAP_MAX_READ_REQ_SIZE(x)   ((x) << 12)
53
54 /* PCIe specific config registers */
55 #define PCIE_CFG0                       0x0
56 #define APP_LTSSM_ENABLE                BIT(7)
57
58 #define PCIE_CFG_STATUS12               0x30
59 #define IS_SMLH_LINK_UP(x)              ((x) & (1 << 6))
60 #define IS_RDLH_LINK_UP(x)              ((x) & (1 << 16))
61 #define IS_LTSSM_UP(x)                  ((((x) >> 10) & 0x1f) == 0x11)
62
63 #define PCIE_CFG_STATUS17               0x44
64 #define PM_CURRENT_STATE(x)             (((x) >> 7) & 0x1)
65
66 #define WAIT_LINKUP_TIMEOUT             4000
67 #define PORT_CLK_RATE                   100000000UL
68 #define MAX_PAYLOAD_SIZE                256
69 #define MAX_READ_REQ_SIZE               256
70 #define PCIE_RESET_DELAY                500
71 #define PCIE_SHARED_RESET               1
72 #define PCIE_NORMAL_RESET               0
73
74 enum pcie_data_rate {
75         PCIE_GEN1,
76         PCIE_GEN2,
77         PCIE_GEN3,
78         PCIE_GEN4
79 };
80
81 struct meson_pcie_mem_res {
82         void __iomem *elbi_base;
83         void __iomem *cfg_base;
84 };
85
86 struct meson_pcie_clk_res {
87         struct clk *clk;
88         struct clk *port_clk;
89         struct clk *general_clk;
90 };
91
92 struct meson_pcie_rc_reset {
93         struct reset_control *port;
94         struct reset_control *apb;
95 };
96
97 struct meson_pcie {
98         struct dw_pcie pci;
99         struct meson_pcie_mem_res mem_res;
100         struct meson_pcie_clk_res clk_res;
101         struct meson_pcie_rc_reset mrst;
102         struct gpio_desc *reset_gpio;
103         struct phy *phy;
104 };
105
106 static struct reset_control *meson_pcie_get_reset(struct meson_pcie *mp,
107                                                   const char *id,
108                                                   u32 reset_type)
109 {
110         struct device *dev = mp->pci.dev;
111         struct reset_control *reset;
112
113         if (reset_type == PCIE_SHARED_RESET)
114                 reset = devm_reset_control_get_shared(dev, id);
115         else
116                 reset = devm_reset_control_get(dev, id);
117
118         return reset;
119 }
120
121 static int meson_pcie_get_resets(struct meson_pcie *mp)
122 {
123         struct meson_pcie_rc_reset *mrst = &mp->mrst;
124
125         mrst->port = meson_pcie_get_reset(mp, "port", PCIE_NORMAL_RESET);
126         if (IS_ERR(mrst->port))
127                 return PTR_ERR(mrst->port);
128         reset_control_deassert(mrst->port);
129
130         mrst->apb = meson_pcie_get_reset(mp, "apb", PCIE_SHARED_RESET);
131         if (IS_ERR(mrst->apb))
132                 return PTR_ERR(mrst->apb);
133         reset_control_deassert(mrst->apb);
134
135         return 0;
136 }
137
138 static void __iomem *meson_pcie_get_mem(struct platform_device *pdev,
139                                         struct meson_pcie *mp,
140                                         const char *id)
141 {
142         struct device *dev = mp->pci.dev;
143         struct resource *res;
144
145         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, id);
146
147         return devm_ioremap_resource(dev, res);
148 }
149
150 static int meson_pcie_get_mems(struct platform_device *pdev,
151                                struct meson_pcie *mp)
152 {
153         mp->mem_res.elbi_base = meson_pcie_get_mem(pdev, mp, "elbi");
154         if (IS_ERR(mp->mem_res.elbi_base))
155                 return PTR_ERR(mp->mem_res.elbi_base);
156
157         mp->mem_res.cfg_base = meson_pcie_get_mem(pdev, mp, "cfg");
158         if (IS_ERR(mp->mem_res.cfg_base))
159                 return PTR_ERR(mp->mem_res.cfg_base);
160
161         return 0;
162 }
163
164 static int meson_pcie_power_on(struct meson_pcie *mp)
165 {
166         int ret = 0;
167
168         ret = phy_init(mp->phy);
169         if (ret)
170                 return ret;
171
172         ret = phy_power_on(mp->phy);
173         if (ret) {
174                 phy_exit(mp->phy);
175                 return ret;
176         }
177
178         return 0;
179 }
180
181 static void meson_pcie_power_off(struct meson_pcie *mp)
182 {
183         phy_power_off(mp->phy);
184         phy_exit(mp->phy);
185 }
186
187 static int meson_pcie_reset(struct meson_pcie *mp)
188 {
189         struct meson_pcie_rc_reset *mrst = &mp->mrst;
190         int ret = 0;
191
192         ret = phy_reset(mp->phy);
193         if (ret)
194                 return ret;
195
196         reset_control_assert(mrst->port);
197         reset_control_assert(mrst->apb);
198         udelay(PCIE_RESET_DELAY);
199         reset_control_deassert(mrst->port);
200         reset_control_deassert(mrst->apb);
201         udelay(PCIE_RESET_DELAY);
202
203         return 0;
204 }
205
206 static inline struct clk *meson_pcie_probe_clock(struct device *dev,
207                                                  const char *id, u64 rate)
208 {
209         struct clk *clk;
210         int ret;
211
212         clk = devm_clk_get(dev, id);
213         if (IS_ERR(clk))
214                 return clk;
215
216         if (rate) {
217                 ret = clk_set_rate(clk, rate);
218                 if (ret) {
219                         dev_err(dev, "set clk rate failed, ret = %d\n", ret);
220                         return ERR_PTR(ret);
221                 }
222         }
223
224         ret = clk_prepare_enable(clk);
225         if (ret) {
226                 dev_err(dev, "couldn't enable clk\n");
227                 return ERR_PTR(ret);
228         }
229
230         devm_add_action_or_reset(dev,
231                                  (void (*) (void *))clk_disable_unprepare,
232                                  clk);
233
234         return clk;
235 }
236
237 static int meson_pcie_probe_clocks(struct meson_pcie *mp)
238 {
239         struct device *dev = mp->pci.dev;
240         struct meson_pcie_clk_res *res = &mp->clk_res;
241
242         res->port_clk = meson_pcie_probe_clock(dev, "port", PORT_CLK_RATE);
243         if (IS_ERR(res->port_clk))
244                 return PTR_ERR(res->port_clk);
245
246         res->general_clk = meson_pcie_probe_clock(dev, "general", 0);
247         if (IS_ERR(res->general_clk))
248                 return PTR_ERR(res->general_clk);
249
250         res->clk = meson_pcie_probe_clock(dev, "pclk", 0);
251         if (IS_ERR(res->clk))
252                 return PTR_ERR(res->clk);
253
254         return 0;
255 }
256
257 static inline void meson_elb_writel(struct meson_pcie *mp, u32 val, u32 reg)
258 {
259         writel(val, mp->mem_res.elbi_base + reg);
260 }
261
262 static inline u32 meson_elb_readl(struct meson_pcie *mp, u32 reg)
263 {
264         return readl(mp->mem_res.elbi_base + reg);
265 }
266
267 static inline u32 meson_cfg_readl(struct meson_pcie *mp, u32 reg)
268 {
269         return readl(mp->mem_res.cfg_base + reg);
270 }
271
272 static inline void meson_cfg_writel(struct meson_pcie *mp, u32 val, u32 reg)
273 {
274         writel(val, mp->mem_res.cfg_base + reg);
275 }
276
277 static void meson_pcie_assert_reset(struct meson_pcie *mp)
278 {
279         gpiod_set_value_cansleep(mp->reset_gpio, 1);
280         udelay(500);
281         gpiod_set_value_cansleep(mp->reset_gpio, 0);
282 }
283
284 static void meson_pcie_init_dw(struct meson_pcie *mp)
285 {
286         u32 val;
287
288         val = meson_cfg_readl(mp, PCIE_CFG0);
289         val |= APP_LTSSM_ENABLE;
290         meson_cfg_writel(mp, val, PCIE_CFG0);
291
292         val = meson_elb_readl(mp, PCIE_PORT_LINK_CTRL_OFF);
293         val &= ~(LINK_CAPABLE_MASK | FAST_LINK_MODE);
294         meson_elb_writel(mp, val, PCIE_PORT_LINK_CTRL_OFF);
295
296         val = meson_elb_readl(mp, PCIE_PORT_LINK_CTRL_OFF);
297         val |= LINK_CAPABLE_X1;
298         meson_elb_writel(mp, val, PCIE_PORT_LINK_CTRL_OFF);
299
300         val = meson_elb_readl(mp, PCIE_GEN2_CTRL_OFF);
301         val &= ~NUM_OF_LANES_MASK;
302         meson_elb_writel(mp, val, PCIE_GEN2_CTRL_OFF);
303
304         val = meson_elb_readl(mp, PCIE_GEN2_CTRL_OFF);
305         val |= NUM_OF_LANES_X1 | DIRECT_SPEED_CHANGE;
306         meson_elb_writel(mp, val, PCIE_GEN2_CTRL_OFF);
307
308         meson_elb_writel(mp, 0x0, PCIE_BASE_ADDR0);
309         meson_elb_writel(mp, 0x0, PCIE_BASE_ADDR1);
310 }
311
312 static int meson_size_to_payload(struct meson_pcie *mp, int size)
313 {
314         struct device *dev = mp->pci.dev;
315
316         /*
317          * dwc supports 2^(val+7) payload size, which val is 0~5 default to 1.
318          * So if input size is not 2^order alignment or less than 2^7 or bigger
319          * than 2^12, just set to default size 2^(1+7).
320          */
321         if (!is_power_of_2(size) || size < 128 || size > 4096) {
322                 dev_warn(dev, "payload size %d, set to default 256\n", size);
323                 return 1;
324         }
325
326         return fls(size) - 8;
327 }
328
329 static void meson_set_max_payload(struct meson_pcie *mp, int size)
330 {
331         u32 val;
332         int max_payload_size = meson_size_to_payload(mp, size);
333
334         val = meson_elb_readl(mp, PCIE_DEV_CTRL_DEV_STUS);
335         val &= ~PCIE_CAP_MAX_PAYLOAD_MASK;
336         meson_elb_writel(mp, val, PCIE_DEV_CTRL_DEV_STUS);
337
338         val = meson_elb_readl(mp, PCIE_DEV_CTRL_DEV_STUS);
339         val |= PCIE_CAP_MAX_PAYLOAD_SIZE(max_payload_size);
340         meson_elb_writel(mp, val, PCIE_DEV_CTRL_DEV_STUS);
341 }
342
343 static void meson_set_max_rd_req_size(struct meson_pcie *mp, int size)
344 {
345         u32 val;
346         int max_rd_req_size = meson_size_to_payload(mp, size);
347
348         val = meson_elb_readl(mp, PCIE_DEV_CTRL_DEV_STUS);
349         val &= ~PCIE_CAP_MAX_READ_REQ_MASK;
350         meson_elb_writel(mp, val, PCIE_DEV_CTRL_DEV_STUS);
351
352         val = meson_elb_readl(mp, PCIE_DEV_CTRL_DEV_STUS);
353         val |= PCIE_CAP_MAX_READ_REQ_SIZE(max_rd_req_size);
354         meson_elb_writel(mp, val, PCIE_DEV_CTRL_DEV_STUS);
355 }
356
357 static inline void meson_enable_memory_space(struct meson_pcie *mp)
358 {
359         /* Set the RC Bus Master, Memory Space and I/O Space enables */
360         meson_elb_writel(mp, PCI_IO_EN | PCI_MEM_SPACE_EN | PCI_BUS_MASTER_EN,
361                          PCIE_STATUS_COMMAND);
362 }
363
364 static int meson_pcie_establish_link(struct meson_pcie *mp)
365 {
366         struct dw_pcie *pci = &mp->pci;
367         struct pcie_port *pp = &pci->pp;
368
369         meson_pcie_init_dw(mp);
370         meson_set_max_payload(mp, MAX_PAYLOAD_SIZE);
371         meson_set_max_rd_req_size(mp, MAX_READ_REQ_SIZE);
372
373         dw_pcie_setup_rc(pp);
374         meson_enable_memory_space(mp);
375
376         meson_pcie_assert_reset(mp);
377
378         return dw_pcie_wait_for_link(pci);
379 }
380
381 static void meson_pcie_enable_interrupts(struct meson_pcie *mp)
382 {
383         if (IS_ENABLED(CONFIG_PCI_MSI))
384                 dw_pcie_msi_init(&mp->pci.pp);
385 }
386
387 static int meson_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
388                                   u32 *val)
389 {
390         struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
391         int ret;
392
393         ret = dw_pcie_read(pci->dbi_base + where, size, val);
394         if (ret != PCIBIOS_SUCCESSFUL)
395                 return ret;
396
397         /*
398          * There is a bug in the MESON AXG PCIe controller whereby software
399          * cannot program the PCI_CLASS_DEVICE register, so we must fabricate
400          * the return value in the config accessors.
401          */
402         if (where == PCI_CLASS_REVISION && size == 4)
403                 *val = (PCI_CLASS_BRIDGE_PCI << 16) | (*val & 0xffff);
404         else if (where == PCI_CLASS_DEVICE && size == 2)
405                 *val = PCI_CLASS_BRIDGE_PCI;
406         else if (where == PCI_CLASS_DEVICE && size == 1)
407                 *val = PCI_CLASS_BRIDGE_PCI & 0xff;
408         else if (where == PCI_CLASS_DEVICE + 1 && size == 1)
409                 *val = (PCI_CLASS_BRIDGE_PCI >> 8) & 0xff;
410
411         return PCIBIOS_SUCCESSFUL;
412 }
413
414 static int meson_pcie_wr_own_conf(struct pcie_port *pp, int where,
415                                   int size, u32 val)
416 {
417         struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
418
419         return dw_pcie_write(pci->dbi_base + where, size, val);
420 }
421
422 static int meson_pcie_link_up(struct dw_pcie *pci)
423 {
424         struct meson_pcie *mp = to_meson_pcie(pci);
425         struct device *dev = pci->dev;
426         u32 speed_okay = 0;
427         u32 cnt = 0;
428         u32 state12, state17, smlh_up, ltssm_up, rdlh_up;
429
430         do {
431                 state12 = meson_cfg_readl(mp, PCIE_CFG_STATUS12);
432                 state17 = meson_cfg_readl(mp, PCIE_CFG_STATUS17);
433                 smlh_up = IS_SMLH_LINK_UP(state12);
434                 rdlh_up = IS_RDLH_LINK_UP(state12);
435                 ltssm_up = IS_LTSSM_UP(state12);
436
437                 if (PM_CURRENT_STATE(state17) < PCIE_GEN3)
438                         speed_okay = 1;
439
440                 if (smlh_up)
441                         dev_dbg(dev, "smlh_link_up is on\n");
442                 if (rdlh_up)
443                         dev_dbg(dev, "rdlh_link_up is on\n");
444                 if (ltssm_up)
445                         dev_dbg(dev, "ltssm_up is on\n");
446                 if (speed_okay)
447                         dev_dbg(dev, "speed_okay\n");
448
449                 if (smlh_up && rdlh_up && ltssm_up && speed_okay)
450                         return 1;
451
452                 cnt++;
453
454                 udelay(10);
455         } while (cnt < WAIT_LINKUP_TIMEOUT);
456
457         dev_err(dev, "error: wait linkup timeout\n");
458         return 0;
459 }
460
461 static int meson_pcie_host_init(struct pcie_port *pp)
462 {
463         struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
464         struct meson_pcie *mp = to_meson_pcie(pci);
465         int ret;
466
467         ret = meson_pcie_establish_link(mp);
468         if (ret)
469                 return ret;
470
471         meson_pcie_enable_interrupts(mp);
472
473         return 0;
474 }
475
476 static const struct dw_pcie_host_ops meson_pcie_host_ops = {
477         .rd_own_conf = meson_pcie_rd_own_conf,
478         .wr_own_conf = meson_pcie_wr_own_conf,
479         .host_init = meson_pcie_host_init,
480 };
481
482 static int meson_add_pcie_port(struct meson_pcie *mp,
483                                struct platform_device *pdev)
484 {
485         struct dw_pcie *pci = &mp->pci;
486         struct pcie_port *pp = &pci->pp;
487         struct device *dev = &pdev->dev;
488         int ret;
489
490         if (IS_ENABLED(CONFIG_PCI_MSI)) {
491                 pp->msi_irq = platform_get_irq(pdev, 0);
492                 if (pp->msi_irq < 0)
493                         return pp->msi_irq;
494         }
495
496         pp->ops = &meson_pcie_host_ops;
497         pci->dbi_base = mp->mem_res.elbi_base;
498
499         ret = dw_pcie_host_init(pp);
500         if (ret) {
501                 dev_err(dev, "failed to initialize host\n");
502                 return ret;
503         }
504
505         return 0;
506 }
507
508 static const struct dw_pcie_ops dw_pcie_ops = {
509         .link_up = meson_pcie_link_up,
510 };
511
512 static int meson_pcie_probe(struct platform_device *pdev)
513 {
514         struct device *dev = &pdev->dev;
515         struct dw_pcie *pci;
516         struct meson_pcie *mp;
517         int ret;
518
519         mp = devm_kzalloc(dev, sizeof(*mp), GFP_KERNEL);
520         if (!mp)
521                 return -ENOMEM;
522
523         pci = &mp->pci;
524         pci->dev = dev;
525         pci->ops = &dw_pcie_ops;
526
527         mp->phy = devm_phy_get(dev, "pcie");
528         if (IS_ERR(mp->phy)) {
529                 dev_err(dev, "get phy failed, %ld\n", PTR_ERR(mp->phy));
530                 return PTR_ERR(mp->phy);
531         }
532
533         mp->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
534         if (IS_ERR(mp->reset_gpio)) {
535                 dev_err(dev, "get reset gpio failed\n");
536                 return PTR_ERR(mp->reset_gpio);
537         }
538
539         ret = meson_pcie_get_resets(mp);
540         if (ret) {
541                 dev_err(dev, "get reset resource failed, %d\n", ret);
542                 return ret;
543         }
544
545         ret = meson_pcie_get_mems(pdev, mp);
546         if (ret) {
547                 dev_err(dev, "get memory resource failed, %d\n", ret);
548                 return ret;
549         }
550
551         ret = meson_pcie_power_on(mp);
552         if (ret) {
553                 dev_err(dev, "phy power on failed, %d\n", ret);
554                 return ret;
555         }
556
557         ret = meson_pcie_reset(mp);
558         if (ret) {
559                 dev_err(dev, "reset failed, %d\n", ret);
560                 goto err_phy;
561         }
562
563         ret = meson_pcie_probe_clocks(mp);
564         if (ret) {
565                 dev_err(dev, "init clock resources failed, %d\n", ret);
566                 goto err_phy;
567         }
568
569         platform_set_drvdata(pdev, mp);
570
571         ret = meson_add_pcie_port(mp, pdev);
572         if (ret < 0) {
573                 dev_err(dev, "Add PCIe port failed, %d\n", ret);
574                 goto err_phy;
575         }
576
577         return 0;
578
579 err_phy:
580         meson_pcie_power_off(mp);
581         return ret;
582 }
583
584 static const struct of_device_id meson_pcie_of_match[] = {
585         {
586                 .compatible = "amlogic,axg-pcie",
587         },
588         {
589                 .compatible = "amlogic,g12a-pcie",
590         },
591         {},
592 };
593 MODULE_DEVICE_TABLE(of, meson_pcie_of_match);
594
595 static struct platform_driver meson_pcie_driver = {
596         .probe = meson_pcie_probe,
597         .driver = {
598                 .name = "meson-pcie",
599                 .of_match_table = meson_pcie_of_match,
600         },
601 };
602
603 module_platform_driver(meson_pcie_driver);
604
605 MODULE_AUTHOR("Yue Wang <yue.wang@amlogic.com>");
606 MODULE_DESCRIPTION("Amlogic PCIe Controller driver");
607 MODULE_LICENSE("GPL v2");