oxnas: switch to kernel 5.4 and remove kernel 4.14
[oweals/openwrt.git] / target / linux / oxnas / files / drivers / pci / controller / pcie-oxnas.c
1 /*
2  * PCIe driver for PLX NAS782X SoCs
3  *
4  * Author: Ma Haijun <mahaijuns@gmail.com>
5  *
6  * This file is licensed under the terms of the GNU General Public
7  * License version 2.  This program is licensed "as is" without any
8  * warranty of any kind, whether express or implied.
9  */
10
11 #include <linux/kernel.h>
12 #include <linux/pci.h>
13 #include <linux/clk.h>
14 #include <linux/module.h>
15 #include <linux/mbus.h>
16 #include <linux/mfd/syscon.h>
17 #include <linux/slab.h>
18 #include <linux/platform_device.h>
19 #include <linux/of_address.h>
20 #include <linux/of_device.h>
21 #include <linux/of_gpio.h>
22 #include <linux/of_irq.h>
23 #include <linux/of_pci.h>
24 #include <linux/of_platform.h>
25 #include <linux/gpio.h>
26 #include <linux/delay.h>
27 #include <linux/clk.h>
28 #include <linux/phy.h>
29 #include <linux/phy/phy.h>
30 #include <linux/regmap.h>
31 #include <linux/reset.h>
32 #include <linux/io.h>
33 #include <linux/sizes.h>
34
35 #include "../pci.h"
36
37 #define SYS_CTRL_HCSL_CTRL_REGOFFSET    0x114
38
39 static inline void oxnas_register_clear_mask(void __iomem *p, unsigned mask)
40 {
41         u32 val = readl_relaxed(p);
42
43         val &= ~mask;
44         writel_relaxed(val, p);
45 }
46
47 static inline void oxnas_register_set_mask(void __iomem *p, unsigned mask)
48 {
49         u32 val = readl_relaxed(p);
50
51         val |= mask;
52         writel_relaxed(val, p);
53 }
54
55 static inline void oxnas_register_value_mask(void __iomem *p,
56                                              unsigned mask, unsigned new_value)
57 {
58         /* TODO sanity check mask & new_value = new_value */
59         u32 val = readl_relaxed(p);
60
61         val &= ~mask;
62         val |= new_value;
63         writel_relaxed(val, p);
64 }
65
66 #define VERSION_ID_MAGIC                0x082510b5
67 #define LINK_UP_TIMEOUT_SECONDS         1
68 #define NUM_CONTROLLERS                 1
69
70 enum {
71         PCIE_DEVICE_TYPE_MASK = 0x0F,
72         PCIE_DEVICE_TYPE_ENDPOINT = 0,
73         PCIE_DEVICE_TYPE_LEGACY_ENDPOINT = 1,
74         PCIE_DEVICE_TYPE_ROOT = 4,
75
76         PCIE_LTSSM = BIT(4),
77         PCIE_READY_ENTR_L23 = BIT(9),
78         PCIE_LINK_UP = BIT(11),
79         PCIE_OBTRANS = BIT(12),
80 };
81
82 /* core config registers */
83 enum {
84         PCI_CONFIG_VERSION_DEVICEID = 0,
85         PCI_CONFIG_COMMAND_STATUS = 4,
86 };
87
88 /* inbound config registers */
89 enum {
90         IB_ADDR_XLATE_ENABLE = 0xFC,
91
92         /* bits */
93         ENABLE_IN_ADDR_TRANS = BIT(0),
94 };
95
96 /* outbound config registers, offset relative to PCIE_POM0_MEM_ADDR */
97 enum {
98         PCIE_POM0_MEM_ADDR      = 0,
99         PCIE_POM1_MEM_ADDR      = 4,
100         PCIE_IN0_MEM_ADDR       = 8,
101         PCIE_IN1_MEM_ADDR       = 12,
102         PCIE_IN_IO_ADDR         = 16,
103         PCIE_IN_CFG0_ADDR       = 20,
104         PCIE_IN_CFG1_ADDR       = 24,
105         PCIE_IN_MSG_ADDR        = 28,
106         PCIE_IN0_MEM_LIMIT      = 32,
107         PCIE_IN1_MEM_LIMIT      = 36,
108         PCIE_IN_IO_LIMIT        = 40,
109         PCIE_IN_CFG0_LIMIT      = 44,
110         PCIE_IN_CFG1_LIMIT      = 48,
111         PCIE_IN_MSG_LIMIT       = 52,
112         PCIE_AHB_SLAVE_CTRL     = 56,
113
114         PCIE_SLAVE_BE_SHIFT     = 22,
115 };
116
117 #define PCIE_SLAVE_BE(val)      ((val) << PCIE_SLAVE_BE_SHIFT)
118 #define PCIE_SLAVE_BE_MASK      PCIE_SLAVE_BE(0xF)
119
120 struct oxnas_pcie_shared {
121         /* seems all access are serialized, no lock required */
122         int refcount;
123 };
124
125 /* Structure representing one PCIe interfaces */
126 struct oxnas_pcie {
127         void __iomem *cfgbase;
128         void __iomem *base;
129         void __iomem *inbound;
130         struct regmap *sys_ctrl;
131         unsigned int outbound_offset;
132         unsigned int pcie_ctrl_offset;
133         struct phy *phy;
134         int haslink;
135         struct platform_device *pdev;
136         struct resource io;
137         struct resource cfg;
138         struct resource pre_mem;        /* prefetchable */
139         struct resource non_mem;        /* non-prefetchable */
140         struct resource busn;           /* max available bus numbers */
141         int card_reset;                 /* gpio pin, optional */
142         unsigned hcsl_en;               /* hcsl pci enable bit */
143         struct clk *clk;
144         struct clk *busclk;             /* for pcie bus, actually the PLLB */
145         void *private_data[1];
146         spinlock_t lock;
147 };
148
149 static struct oxnas_pcie_shared pcie_shared = {
150         .refcount = 0,
151 };
152
153 static inline struct oxnas_pcie *sys_to_pcie(struct pci_sys_data *sys)
154 {
155         return sys->private_data;
156 }
157
158
159 static inline void set_out_lanes(struct oxnas_pcie *pcie, unsigned lanes)
160 {
161         regmap_update_bits(pcie->sys_ctrl, pcie->outbound_offset + PCIE_AHB_SLAVE_CTRL,
162                                   PCIE_SLAVE_BE_MASK, PCIE_SLAVE_BE(lanes));
163         wmb();
164 }
165
166 static int oxnas_pcie_link_up(struct oxnas_pcie *pcie)
167 {
168         unsigned long end;
169         unsigned int val;
170
171         /* Poll for PCIE link up */
172         end = jiffies + (LINK_UP_TIMEOUT_SECONDS * HZ);
173         while (!time_after(jiffies, end)) {
174                 regmap_read(pcie->sys_ctrl, pcie->pcie_ctrl_offset, &val);
175                 if (val & PCIE_LINK_UP)
176                         return 1;
177         }
178         return 0;
179 }
180
181 static void oxnas_pcie_setup_hw(struct oxnas_pcie *pcie)
182 {
183         /* We won't have any inbound address translation. This allows PCI
184          * devices to access anywhere in the AHB address map. Might be regarded
185          * as a bit dangerous, but let's get things working before we worry
186          * about that
187          */
188         oxnas_register_clear_mask(pcie->inbound + IB_ADDR_XLATE_ENABLE,
189                                   ENABLE_IN_ADDR_TRANS);
190         wmb();
191
192         /*
193          * Program outbound translation windows
194          *
195          * Outbound window is what is referred to as "PCI client" region in HRM
196          *
197          * Could use the larger alternative address space to get >>64M regions
198          * for graphics cards etc., but will not bother at this point.
199          *
200          * IP bug means that AMBA window size must be a power of 2
201          *
202          * Set mem0 window for first 16MB of outbound window non-prefetchable
203          * Set mem1 window for second 16MB of outbound window prefetchable
204          * Set io window for next 16MB of outbound window
205          * Set cfg0 for final 1MB of outbound window
206          *
207          * Ignore mem1, cfg1 and msg windows for now as no obvious use cases for
208          * 820 that would need them
209          *
210          * Probably ideally want no offset between mem0 window start as seen by
211          * ARM and as seen on PCI bus and get Linux to assign memory regions to
212          * PCI devices using the same "PCI client" region start address as seen
213          * by ARM
214          */
215
216         /* Set PCIeA mem0 region to be 1st 16MB of the 64MB PCIeA window */
217         regmap_write(pcie->sys_ctrl, pcie->outbound_offset + PCIE_IN0_MEM_ADDR, pcie->non_mem.start);
218         regmap_write(pcie->sys_ctrl, pcie->outbound_offset + PCIE_IN0_MEM_LIMIT, pcie->non_mem.end);
219         regmap_write(pcie->sys_ctrl, pcie->outbound_offset + PCIE_POM0_MEM_ADDR, pcie->non_mem.start);
220
221         /* Set PCIeA mem1 region to be 2nd 16MB of the 64MB PCIeA window */
222         regmap_write(pcie->sys_ctrl, pcie->outbound_offset + PCIE_IN1_MEM_ADDR, pcie->pre_mem.start);
223         regmap_write(pcie->sys_ctrl, pcie->outbound_offset + PCIE_IN1_MEM_LIMIT, pcie->pre_mem.end);
224         regmap_write(pcie->sys_ctrl, pcie->outbound_offset + PCIE_POM1_MEM_ADDR, pcie->pre_mem.start);
225
226         /* Set PCIeA io to be third 16M region of the 64MB PCIeA window*/
227         regmap_write(pcie->sys_ctrl, pcie->outbound_offset + PCIE_IN_IO_ADDR, pcie->io.start);
228         regmap_write(pcie->sys_ctrl, pcie->outbound_offset + PCIE_IN_IO_LIMIT, pcie->io.end);
229
230
231         /* Set PCIeA cgf0 to be last 16M region of the 64MB PCIeA window*/
232         regmap_write(pcie->sys_ctrl, pcie->outbound_offset + PCIE_IN_CFG0_ADDR, pcie->cfg.start);
233         regmap_write(pcie->sys_ctrl, pcie->outbound_offset + PCIE_IN_CFG0_LIMIT, pcie->cfg.end);
234         wmb();
235
236         /* Enable outbound address translation */
237         regmap_write_bits(pcie->sys_ctrl, pcie->pcie_ctrl_offset, PCIE_OBTRANS, PCIE_OBTRANS);
238         wmb();
239
240         /*
241          * Program PCIe command register for core to:
242          *  enable memory space
243          *  enable bus master
244          *  enable io
245          */
246         writel_relaxed(7, pcie->base + PCI_CONFIG_COMMAND_STATUS);
247         /* which is which */
248         wmb();
249 }
250
251 static unsigned oxnas_pcie_cfg_to_offset(
252         struct pci_sys_data *sys,
253         unsigned char bus_number,
254         unsigned int devfn,
255         int where)
256 {
257         unsigned int function = PCI_FUNC(devfn);
258         unsigned int slot = PCI_SLOT(devfn);
259         unsigned char bus_number_offset;
260
261         bus_number_offset = bus_number - sys->busnr;
262
263         /*
264          * We'll assume for now that the offset, function, slot, bus encoding
265          * should map onto linear, contiguous addresses in PCIe config space,
266          * albeit that the majority will be unused as only slot 0 is valid for
267          * any PCIe bus and most devices have only function 0
268          *
269          * Could be that PCIe in fact works by not encoding the slot number into
270          * the config space address as it's known that only slot 0 is valid.
271          * We'll have to experiment if/when we get a PCIe switch connected to
272          * the PCIe host
273          */
274         return (bus_number_offset << 20) | (slot << 15) | (function << 12) |
275                 (where & ~3);
276 }
277
278 /* PCI configuration space write function */
279 static int oxnas_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
280                               int where, int size, u32 val)
281 {
282         unsigned long flags;
283         struct oxnas_pcie *pcie = sys_to_pcie(bus->sysdata);
284         unsigned offset;
285         u32 value;
286         u32 lanes;
287
288         /* Only a single device per bus for PCIe point-to-point links */
289         if (PCI_SLOT(devfn) > 0)
290                 return PCIBIOS_DEVICE_NOT_FOUND;
291
292         if (!pcie->haslink)
293                 return PCIBIOS_DEVICE_NOT_FOUND;
294
295         offset = oxnas_pcie_cfg_to_offset(bus->sysdata, bus->number, devfn,
296                                           where);
297
298         value = val << (8 * (where & 3));
299         lanes = (0xf >> (4-size)) << (where & 3);
300         /* it race with mem and io write, but the possibility is low, normally
301          * all config writes happens at driver initialize stage, wont interleave
302          * with others.
303          * and many pcie cards use dword (4bytes) access mem/io access only,
304          * so not bother to copy that ugly work-around now. */
305         spin_lock_irqsave(&pcie->lock, flags);
306         set_out_lanes(pcie, lanes);
307         writel_relaxed(value, pcie->cfgbase + offset);
308         set_out_lanes(pcie, 0xf);
309         spin_unlock_irqrestore(&pcie->lock, flags);
310
311         return PCIBIOS_SUCCESSFUL;
312 }
313
314 /* PCI configuration space read function */
315 static int oxnas_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
316                               int size, u32 *val)
317 {
318         struct oxnas_pcie *pcie = sys_to_pcie(bus->sysdata);
319         unsigned offset;
320         u32 value;
321         u32 left_bytes, right_bytes;
322
323         /* Only a single device per bus for PCIe point-to-point links */
324         if (PCI_SLOT(devfn) > 0) {
325                 *val = 0xffffffff;
326                 return PCIBIOS_DEVICE_NOT_FOUND;
327         }
328
329         if (!pcie->haslink) {
330                 *val = 0xffffffff;
331                 return PCIBIOS_DEVICE_NOT_FOUND;
332         }
333
334         offset = oxnas_pcie_cfg_to_offset(bus->sysdata, bus->number, devfn,
335                                           where);
336         value = readl_relaxed(pcie->cfgbase + offset);
337         left_bytes = where & 3;
338         right_bytes = 4 - left_bytes - size;
339         value <<= right_bytes * 8;
340         value >>= (left_bytes + right_bytes) * 8;
341         *val = value;
342
343         return PCIBIOS_SUCCESSFUL;
344 }
345
346 static struct pci_ops oxnas_pcie_ops = {
347         .read = oxnas_pcie_rd_conf,
348         .write = oxnas_pcie_wr_conf,
349 };
350
351 static int oxnas_pcie_setup(int nr, struct pci_sys_data *sys)
352 {
353         struct oxnas_pcie *pcie = sys_to_pcie(sys);
354
355         pci_add_resource_offset(&sys->resources, &pcie->non_mem, sys->mem_offset);
356         pci_add_resource_offset(&sys->resources, &pcie->pre_mem, sys->mem_offset);
357         pci_add_resource_offset(&sys->resources, &pcie->io, sys->io_offset);
358         pci_add_resource(&sys->resources, &pcie->busn);
359         if (sys->busnr == 0) { /* default one */
360                 sys->busnr = pcie->busn.start;
361         }
362         /* do not use devm_ioremap_resource, it does not like cfg resource */
363         pcie->cfgbase = devm_ioremap(&pcie->pdev->dev, pcie->cfg.start,
364                                      resource_size(&pcie->cfg));
365         if (!pcie->cfgbase)
366                 return -ENOMEM;
367
368         oxnas_pcie_setup_hw(pcie);
369
370         return 1;
371 }
372
373 static void oxnas_pcie_enable(struct device *dev, struct oxnas_pcie *pcie)
374 {
375         struct hw_pci hw;
376         int i;
377
378         memset(&hw, 0, sizeof(hw));
379         for (i = 0; i < NUM_CONTROLLERS; i++)
380                 pcie->private_data[i] = pcie;
381
382         hw.nr_controllers = NUM_CONTROLLERS;
383 /* I think use stack pointer is a bad idea though it is valid in this case */
384         hw.private_data   = pcie->private_data;
385         hw.setup          = oxnas_pcie_setup;
386         hw.map_irq        = of_irq_parse_and_map_pci;
387         hw.ops            = &oxnas_pcie_ops;
388
389         /* pass dev to maintain of tree, interrupt mapping rely on this */
390         pci_common_init_dev(dev, &hw);
391 }
392
393 static int oxnas_pcie_shared_init(struct platform_device *pdev, struct oxnas_pcie *pcie)
394 {
395         if (++pcie_shared.refcount == 1) {
396                 phy_init(pcie->phy);
397                 phy_power_on(pcie->phy);
398                 return 0;
399         } else {
400                 return 0;
401         }
402 }
403
404 #if 0
405 /* maybe we will call it when enter low power state */
406 static void oxnas_pcie_shared_deinit(struct platform_device *pdev)
407 {
408         if (--pcie_shared.refcount == 0) {
409                 /* no cleanup needed */;
410         }
411 }
412 #endif
413
414 static int
415 oxnas_pcie_map_registers(struct platform_device *pdev,
416                          struct device_node *np,
417                          struct oxnas_pcie *pcie)
418 {
419         struct resource regs;
420         int ret = 0;
421         u32 outbound_ctrl_offset;
422         u32 pcie_ctrl_offset;
423
424         ret = of_address_to_resource(np, 0, &regs);
425         if (ret) {
426                 dev_err(&pdev->dev, "failed to parse base register space\n");
427                 return -EINVAL;
428         }
429
430         pcie->base = devm_ioremap_resource(&pdev->dev, &regs);
431         if (!pcie->base) {
432                 dev_err(&pdev->dev, "failed to map base register space\n");
433                 return -ENOMEM;
434         }
435
436         ret = of_address_to_resource(np, 1, &regs);
437         if (ret) {
438                 dev_err(&pdev->dev, "failed to parse inbound register space\n");
439                 return -EINVAL;
440         }
441
442         pcie->inbound = devm_ioremap_resource(&pdev->dev, &regs);
443         if (!pcie->inbound) {
444                 dev_err(&pdev->dev, "failed to map inbound register space\n");
445                 return -ENOMEM;
446         }
447
448         pcie->phy = devm_of_phy_get(&pdev->dev, np, NULL);
449         if (IS_ERR(pcie->phy)) {
450                 if (PTR_ERR(pcie->phy) == -EPROBE_DEFER) {
451                         dev_err(&pdev->dev, "failed to probe phy\n");
452                         return PTR_ERR(pcie->phy);
453                 }
454                 dev_warn(&pdev->dev, "phy not attached\n");
455                 pcie->phy = NULL;
456         }
457
458         if (of_property_read_u32(np, "plxtech,pcie-outbound-offset",
459                                  &outbound_ctrl_offset)) {
460                 dev_err(&pdev->dev, "failed to parse outbound register offset\n");
461                 return -EINVAL;
462         }
463         pcie->outbound_offset = outbound_ctrl_offset;
464
465         if (of_property_read_u32(np, "plxtech,pcie-ctrl-offset",
466                                  &pcie_ctrl_offset)) {
467                 dev_err(&pdev->dev, "failed to parse pcie-ctrl register offset\n");
468                 return -EINVAL;
469         }
470         pcie->pcie_ctrl_offset = pcie_ctrl_offset;
471
472         return 0;
473 }
474
475 static int oxnas_pcie_init_res(struct platform_device *pdev,
476                                       struct oxnas_pcie *pcie,
477                                       struct device_node *np)
478 {
479         struct of_pci_range range;
480         struct of_pci_range_parser parser;
481         int ret;
482
483         if (of_pci_range_parser_init(&parser, np))
484                 return -EINVAL;
485
486         /* Get the I/O and memory ranges from DT */
487         for_each_of_pci_range(&parser, &range) {
488
489                 unsigned long restype = range.flags & IORESOURCE_TYPE_BITS;
490                 if (restype == IORESOURCE_IO) {
491                         of_pci_range_to_resource(&range, np, &pcie->io);
492                         pcie->io.name = "I/O";
493                 }
494                 if (restype == IORESOURCE_MEM) {
495                         if (range.flags & IORESOURCE_PREFETCH) {
496                                 of_pci_range_to_resource(&range, np, &pcie->pre_mem);
497                                 pcie->pre_mem.name = "PRE MEM";
498                         } else {
499                                 of_pci_range_to_resource(&range, np, &pcie->non_mem);
500                                 pcie->non_mem.name = "NON MEM";
501                         }
502
503                 }
504                 if (restype == 0)
505                         of_pci_range_to_resource(&range, np, &pcie->cfg);
506         }
507
508         /* Get the bus range */
509         ret = of_pci_parse_bus_range(np, &pcie->busn);
510
511         if (ret) {
512                 dev_err(&pdev->dev, "failed to parse bus-range property: %d\n",
513                         ret);
514                 return ret;
515         }
516
517         pcie->card_reset = of_get_gpio(np, 0);
518         if (pcie->card_reset < 0)
519                 dev_info(&pdev->dev, "card reset gpio pin not exists\n");
520
521         if (of_property_read_u32(np, "plxtech,pcie-hcsl-bit", &pcie->hcsl_en))
522                 return -EINVAL;
523
524         pcie->clk = of_clk_get_by_name(np, "pcie");
525         if (IS_ERR(pcie->clk)) {
526                 return PTR_ERR(pcie->clk);
527         }
528
529         pcie->busclk = of_clk_get_by_name(np, "busclk");
530         if (IS_ERR(pcie->busclk)) {
531                 clk_put(pcie->clk);
532                 return PTR_ERR(pcie->busclk);
533         }
534
535         return 0;
536 }
537
538 static void oxnas_pcie_init_hw(struct platform_device *pdev,
539                                struct oxnas_pcie *pcie)
540 {
541         u32 version_id;
542         int ret;
543
544         clk_prepare_enable(pcie->busclk);
545
546         /* reset PCIe cards use hard-wired gpio pin */
547         if (pcie->card_reset >= 0 &&
548             !gpio_direction_output(pcie->card_reset, 0)) {
549                 wmb();
550                 mdelay(10);
551                 /* must tri-state the pin to pull it up */
552                 gpio_direction_input(pcie->card_reset);
553                 wmb();
554                 mdelay(100);
555         }
556
557         /* ToDo: use phy power-on port... */
558         regmap_update_bits(pcie->sys_ctrl, SYS_CTRL_HCSL_CTRL_REGOFFSET,
559                            BIT(pcie->hcsl_en), BIT(pcie->hcsl_en));
560
561         /* core */
562         ret = device_reset(&pdev->dev);
563         if (ret) {
564                 dev_err(&pdev->dev, "core reset failed %d\n", ret);
565                 return;
566         }
567
568         /* Start PCIe core clocks */
569         clk_prepare_enable(pcie->clk);
570
571         version_id = readl_relaxed(pcie->base + PCI_CONFIG_VERSION_DEVICEID);
572         dev_info(&pdev->dev, "PCIe version/deviceID 0x%x\n", version_id);
573
574         if (version_id != VERSION_ID_MAGIC) {
575                 dev_info(&pdev->dev, "PCIe controller not found\n");
576                 pcie->haslink = 0;
577                 return;
578         }
579
580         /* allow entry to L23 state */
581         regmap_write_bits(pcie->sys_ctrl, pcie->pcie_ctrl_offset,
582                           PCIE_READY_ENTR_L23, PCIE_READY_ENTR_L23);
583
584         /* Set PCIe core into RootCore mode */
585         regmap_write_bits(pcie->sys_ctrl, pcie->pcie_ctrl_offset,
586                           PCIE_DEVICE_TYPE_MASK, PCIE_DEVICE_TYPE_ROOT);
587         wmb();
588
589         /* Bring up the PCI core */
590         regmap_write_bits(pcie->sys_ctrl, pcie->pcie_ctrl_offset,
591                           PCIE_LTSSM, PCIE_LTSSM);
592         wmb();
593 }
594
595 static int oxnas_pcie_probe(struct platform_device *pdev)
596 {
597         struct oxnas_pcie *pcie;
598         struct device_node *np = pdev->dev.of_node;
599         int ret;
600
601         pcie = devm_kzalloc(&pdev->dev, sizeof(struct oxnas_pcie),
602                             GFP_KERNEL);
603         if (!pcie)
604                 return -ENOMEM;
605
606         pcie->pdev = pdev;
607         pcie->haslink = 1;
608         spin_lock_init(&pcie->lock);
609
610         pcie->sys_ctrl = syscon_regmap_lookup_by_compatible("oxsemi,ox820-sys-ctrl");
611         if (IS_ERR(pcie->sys_ctrl))
612                 return PTR_ERR(pcie->sys_ctrl);
613
614         ret = oxnas_pcie_init_res(pdev, pcie, np);
615         if (ret)
616                 return ret;
617         if (pcie->card_reset >= 0) {
618                 ret = gpio_request_one(pcie->card_reset, GPIOF_DIR_IN,
619                                        dev_name(&pdev->dev));
620                 if (ret) {
621                         dev_err(&pdev->dev, "cannot request gpio pin %d\n",
622                                 pcie->card_reset);
623                         return ret;
624                 }
625         }
626
627         ret = oxnas_pcie_map_registers(pdev, np, pcie);
628         if (ret) {
629                 dev_err(&pdev->dev, "cannot map registers\n");
630                 goto err_free_gpio;
631         }
632
633         ret = oxnas_pcie_shared_init(pdev, pcie);
634         if (ret)
635                 goto err_free_gpio;
636
637         /* if hw not found, haslink cleared */
638         oxnas_pcie_init_hw(pdev, pcie);
639
640         if (pcie->haslink && oxnas_pcie_link_up(pcie)) {
641                 pcie->haslink = 1;
642                 dev_info(&pdev->dev, "link up\n");
643         } else {
644                 pcie->haslink = 0;
645                 dev_info(&pdev->dev, "link down\n");
646         }
647         /* should we register our controller even when pcie->haslink is 0 ? */
648         /* register the controller with framework */
649         oxnas_pcie_enable(&pdev->dev, pcie);
650
651         return 0;
652
653 err_free_gpio:
654         if (pcie->card_reset)
655                 gpio_free(pcie->card_reset);
656
657         return ret;
658 }
659
660 static const struct of_device_id oxnas_pcie_of_match_table[] = {
661         { .compatible = "plxtech,nas782x-pcie", },
662         {},
663 };
664
665 static struct platform_driver oxnas_pcie_driver = {
666         .driver = {
667                 .name = "oxnas-pcie",
668                 .suppress_bind_attrs = true,
669                 .of_match_table = oxnas_pcie_of_match_table,
670         },
671         .probe = oxnas_pcie_probe,
672 };
673
674 builtin_platform_driver(oxnas_pcie_driver);