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