PCI: xgene: Add local struct device pointers
[cascardo/linux.git] / drivers / pci / host / pci-xgene.c
1 /**
2  * APM X-Gene PCIe Driver
3  *
4  * Copyright (c) 2014 Applied Micro Circuits Corporation.
5  *
6  * Author: Tanmay Inamdar <tinamdar@apm.com>.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; either version 2 of the License, or (at your
11  * option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  */
19 #include <linux/clk.h>
20 #include <linux/delay.h>
21 #include <linux/io.h>
22 #include <linux/jiffies.h>
23 #include <linux/memblock.h>
24 #include <linux/init.h>
25 #include <linux/of.h>
26 #include <linux/of_address.h>
27 #include <linux/of_irq.h>
28 #include <linux/of_pci.h>
29 #include <linux/pci.h>
30 #include <linux/platform_device.h>
31 #include <linux/slab.h>
32
33 #define PCIECORE_CTLANDSTATUS           0x50
34 #define PIM1_1L                         0x80
35 #define IBAR2                           0x98
36 #define IR2MSK                          0x9c
37 #define PIM2_1L                         0xa0
38 #define IBAR3L                          0xb4
39 #define IR3MSKL                         0xbc
40 #define PIM3_1L                         0xc4
41 #define OMR1BARL                        0x100
42 #define OMR2BARL                        0x118
43 #define OMR3BARL                        0x130
44 #define CFGBARL                         0x154
45 #define CFGBARH                         0x158
46 #define CFGCTL                          0x15c
47 #define RTDID                           0x160
48 #define BRIDGE_CFG_0                    0x2000
49 #define BRIDGE_CFG_4                    0x2010
50 #define BRIDGE_STATUS_0                 0x2600
51
52 #define LINK_UP_MASK                    0x00000100
53 #define AXI_EP_CFG_ACCESS               0x10000
54 #define EN_COHERENCY                    0xF0000000
55 #define EN_REG                          0x00000001
56 #define OB_LO_IO                        0x00000002
57 #define XGENE_PCIE_VENDORID             0x10E8
58 #define XGENE_PCIE_DEVICEID             0xE004
59 #define SZ_1T                           (SZ_1G*1024ULL)
60 #define PIPE_PHY_RATE_RD(src)           ((0xc000 & (u32)(src)) >> 0xe)
61
62 #define ROOT_CAP_AND_CTRL               0x5C
63
64 /* PCIe IP version */
65 #define XGENE_PCIE_IP_VER_UNKN          0
66 #define XGENE_PCIE_IP_VER_1             1
67
68 struct xgene_pcie_port {
69         struct device_node      *node;
70         struct device           *dev;
71         struct clk              *clk;
72         void __iomem            *csr_base;
73         void __iomem            *cfg_base;
74         unsigned long           cfg_addr;
75         bool                    link_up;
76         u32                     version;
77 };
78
79 static inline u32 pcie_bar_low_val(u32 addr, u32 flags)
80 {
81         return (addr & PCI_BASE_ADDRESS_MEM_MASK) | flags;
82 }
83
84 /*
85  * When the address bit [17:16] is 2'b01, the Configuration access will be
86  * treated as Type 1 and it will be forwarded to external PCIe device.
87  */
88 static void __iomem *xgene_pcie_get_cfg_base(struct pci_bus *bus)
89 {
90         struct xgene_pcie_port *port = bus->sysdata;
91
92         if (bus->number >= (bus->primary + 1))
93                 return port->cfg_base + AXI_EP_CFG_ACCESS;
94
95         return port->cfg_base;
96 }
97
98 /*
99  * For Configuration request, RTDID register is used as Bus Number,
100  * Device Number and Function number of the header fields.
101  */
102 static void xgene_pcie_set_rtdid_reg(struct pci_bus *bus, uint devfn)
103 {
104         struct xgene_pcie_port *port = bus->sysdata;
105         unsigned int b, d, f;
106         u32 rtdid_val = 0;
107
108         b = bus->number;
109         d = PCI_SLOT(devfn);
110         f = PCI_FUNC(devfn);
111
112         if (!pci_is_root_bus(bus))
113                 rtdid_val = (b << 8) | (d << 3) | f;
114
115         writel(rtdid_val, port->csr_base + RTDID);
116         /* read the register back to ensure flush */
117         readl(port->csr_base + RTDID);
118 }
119
120 /*
121  * X-Gene PCIe port uses BAR0-BAR1 of RC's configuration space as
122  * the translation from PCI bus to native BUS.  Entire DDR region
123  * is mapped into PCIe space using these registers, so it can be
124  * reached by DMA from EP devices.  The BAR0/1 of bridge should be
125  * hidden during enumeration to avoid the sizing and resource allocation
126  * by PCIe core.
127  */
128 static bool xgene_pcie_hide_rc_bars(struct pci_bus *bus, int offset)
129 {
130         if (pci_is_root_bus(bus) && ((offset == PCI_BASE_ADDRESS_0) ||
131                                      (offset == PCI_BASE_ADDRESS_1)))
132                 return true;
133
134         return false;
135 }
136
137 static void __iomem *xgene_pcie_map_bus(struct pci_bus *bus, unsigned int devfn,
138                               int offset)
139 {
140         if ((pci_is_root_bus(bus) && devfn != 0) ||
141             xgene_pcie_hide_rc_bars(bus, offset))
142                 return NULL;
143
144         xgene_pcie_set_rtdid_reg(bus, devfn);
145         return xgene_pcie_get_cfg_base(bus) + offset;
146 }
147
148 static int xgene_pcie_config_read32(struct pci_bus *bus, unsigned int devfn,
149                                     int where, int size, u32 *val)
150 {
151         struct xgene_pcie_port *port = bus->sysdata;
152
153         if (pci_generic_config_read32(bus, devfn, where & ~0x3, 4, val) !=
154             PCIBIOS_SUCCESSFUL)
155                 return PCIBIOS_DEVICE_NOT_FOUND;
156
157         /*
158          * The v1 controller has a bug in its Configuration Request
159          * Retry Status (CRS) logic: when CRS is enabled and we read the
160          * Vendor and Device ID of a non-existent device, the controller
161          * fabricates return data of 0xFFFF0001 ("device exists but is not
162          * ready") instead of 0xFFFFFFFF ("device does not exist").  This
163          * causes the PCI core to retry the read until it times out.
164          * Avoid this by not claiming to support CRS.
165          */
166         if (pci_is_root_bus(bus) && (port->version == XGENE_PCIE_IP_VER_1) &&
167             ((where & ~0x3) == ROOT_CAP_AND_CTRL))
168                 *val &= ~(PCI_EXP_RTCAP_CRSVIS << 16);
169
170         if (size <= 2)
171                 *val = (*val >> (8 * (where & 3))) & ((1 << (size * 8)) - 1);
172
173         return PCIBIOS_SUCCESSFUL;
174 }
175
176 static struct pci_ops xgene_pcie_ops = {
177         .map_bus = xgene_pcie_map_bus,
178         .read = xgene_pcie_config_read32,
179         .write = pci_generic_config_write32,
180 };
181
182 static u64 xgene_pcie_set_ib_mask(void __iomem *csr_base, u32 addr,
183                                   u32 flags, u64 size)
184 {
185         u64 mask = (~(size - 1) & PCI_BASE_ADDRESS_MEM_MASK) | flags;
186         u32 val32 = 0;
187         u32 val;
188
189         val32 = readl(csr_base + addr);
190         val = (val32 & 0x0000ffff) | (lower_32_bits(mask) << 16);
191         writel(val, csr_base + addr);
192
193         val32 = readl(csr_base + addr + 0x04);
194         val = (val32 & 0xffff0000) | (lower_32_bits(mask) >> 16);
195         writel(val, csr_base + addr + 0x04);
196
197         val32 = readl(csr_base + addr + 0x04);
198         val = (val32 & 0x0000ffff) | (upper_32_bits(mask) << 16);
199         writel(val, csr_base + addr + 0x04);
200
201         val32 = readl(csr_base + addr + 0x08);
202         val = (val32 & 0xffff0000) | (upper_32_bits(mask) >> 16);
203         writel(val, csr_base + addr + 0x08);
204
205         return mask;
206 }
207
208 static void xgene_pcie_linkup(struct xgene_pcie_port *port,
209                                    u32 *lanes, u32 *speed)
210 {
211         void __iomem *csr_base = port->csr_base;
212         u32 val32;
213
214         port->link_up = false;
215         val32 = readl(csr_base + PCIECORE_CTLANDSTATUS);
216         if (val32 & LINK_UP_MASK) {
217                 port->link_up = true;
218                 *speed = PIPE_PHY_RATE_RD(val32);
219                 val32 = readl(csr_base + BRIDGE_STATUS_0);
220                 *lanes = val32 >> 26;
221         }
222 }
223
224 static int xgene_pcie_init_port(struct xgene_pcie_port *port)
225 {
226         struct device *dev = port->dev;
227         int rc;
228
229         port->clk = clk_get(dev, NULL);
230         if (IS_ERR(port->clk)) {
231                 dev_err(dev, "clock not available\n");
232                 return -ENODEV;
233         }
234
235         rc = clk_prepare_enable(port->clk);
236         if (rc) {
237                 dev_err(dev, "clock enable failed\n");
238                 return rc;
239         }
240
241         return 0;
242 }
243
244 static int xgene_pcie_map_reg(struct xgene_pcie_port *port,
245                               struct platform_device *pdev)
246 {
247         struct device *dev = port->dev;
248         struct resource *res;
249
250         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "csr");
251         port->csr_base = devm_ioremap_resource(dev, res);
252         if (IS_ERR(port->csr_base))
253                 return PTR_ERR(port->csr_base);
254
255         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg");
256         port->cfg_base = devm_ioremap_resource(dev, res);
257         if (IS_ERR(port->cfg_base))
258                 return PTR_ERR(port->cfg_base);
259         port->cfg_addr = res->start;
260
261         return 0;
262 }
263
264 static void xgene_pcie_setup_ob_reg(struct xgene_pcie_port *port,
265                                     struct resource *res, u32 offset,
266                                     u64 cpu_addr, u64 pci_addr)
267 {
268         void __iomem *base = port->csr_base + offset;
269         struct device *dev = port->dev;
270         resource_size_t size = resource_size(res);
271         u64 restype = resource_type(res);
272         u64 mask = 0;
273         u32 min_size;
274         u32 flag = EN_REG;
275
276         if (restype == IORESOURCE_MEM) {
277                 min_size = SZ_128M;
278         } else {
279                 min_size = 128;
280                 flag |= OB_LO_IO;
281         }
282
283         if (size >= min_size)
284                 mask = ~(size - 1) | flag;
285         else
286                 dev_warn(dev, "res size 0x%llx less than minimum 0x%x\n",
287                          (u64)size, min_size);
288
289         writel(lower_32_bits(cpu_addr), base);
290         writel(upper_32_bits(cpu_addr), base + 0x04);
291         writel(lower_32_bits(mask), base + 0x08);
292         writel(upper_32_bits(mask), base + 0x0c);
293         writel(lower_32_bits(pci_addr), base + 0x10);
294         writel(upper_32_bits(pci_addr), base + 0x14);
295 }
296
297 static void xgene_pcie_setup_cfg_reg(void __iomem *csr_base, u64 addr)
298 {
299         writel(lower_32_bits(addr), csr_base + CFGBARL);
300         writel(upper_32_bits(addr), csr_base + CFGBARH);
301         writel(EN_REG, csr_base + CFGCTL);
302 }
303
304 static int xgene_pcie_map_ranges(struct xgene_pcie_port *port,
305                                  struct list_head *res,
306                                  resource_size_t io_base)
307 {
308         struct resource_entry *window;
309         struct device *dev = port->dev;
310         int ret;
311
312         resource_list_for_each_entry(window, res) {
313                 struct resource *res = window->res;
314                 u64 restype = resource_type(res);
315
316                 dev_dbg(dev, "%pR\n", res);
317
318                 switch (restype) {
319                 case IORESOURCE_IO:
320                         xgene_pcie_setup_ob_reg(port, res, OMR3BARL, io_base,
321                                                 res->start - window->offset);
322                         ret = pci_remap_iospace(res, io_base);
323                         if (ret < 0)
324                                 return ret;
325                         break;
326                 case IORESOURCE_MEM:
327                         if (res->flags & IORESOURCE_PREFETCH)
328                                 xgene_pcie_setup_ob_reg(port, res, OMR2BARL,
329                                                         res->start,
330                                                         res->start -
331                                                         window->offset);
332                         else
333                                 xgene_pcie_setup_ob_reg(port, res, OMR1BARL,
334                                                         res->start,
335                                                         res->start -
336                                                         window->offset);
337                         break;
338                 case IORESOURCE_BUS:
339                         break;
340                 default:
341                         dev_err(dev, "invalid resource %pR\n", res);
342                         return -EINVAL;
343                 }
344         }
345         xgene_pcie_setup_cfg_reg(port->csr_base, port->cfg_addr);
346
347         return 0;
348 }
349
350 static void xgene_pcie_setup_pims(void *addr, u64 pim, u64 size)
351 {
352         writel(lower_32_bits(pim), addr);
353         writel(upper_32_bits(pim) | EN_COHERENCY, addr + 0x04);
354         writel(lower_32_bits(size), addr + 0x10);
355         writel(upper_32_bits(size), addr + 0x14);
356 }
357
358 /*
359  * X-Gene PCIe support maximum 3 inbound memory regions
360  * This function helps to select a region based on size of region
361  */
362 static int xgene_pcie_select_ib_reg(u8 *ib_reg_mask, u64 size)
363 {
364         if ((size > 4) && (size < SZ_16M) && !(*ib_reg_mask & (1 << 1))) {
365                 *ib_reg_mask |= (1 << 1);
366                 return 1;
367         }
368
369         if ((size > SZ_1K) && (size < SZ_1T) && !(*ib_reg_mask & (1 << 0))) {
370                 *ib_reg_mask |= (1 << 0);
371                 return 0;
372         }
373
374         if ((size > SZ_1M) && (size < SZ_1T) && !(*ib_reg_mask & (1 << 2))) {
375                 *ib_reg_mask |= (1 << 2);
376                 return 2;
377         }
378
379         return -EINVAL;
380 }
381
382 static void xgene_pcie_setup_ib_reg(struct xgene_pcie_port *port,
383                                     struct of_pci_range *range, u8 *ib_reg_mask)
384 {
385         void __iomem *csr_base = port->csr_base;
386         void __iomem *cfg_base = port->cfg_base;
387         struct device *dev = port->dev;
388         void *bar_addr;
389         void *pim_addr;
390         u64 cpu_addr = range->cpu_addr;
391         u64 pci_addr = range->pci_addr;
392         u64 size = range->size;
393         u64 mask = ~(size - 1) | EN_REG;
394         u32 flags = PCI_BASE_ADDRESS_MEM_TYPE_64;
395         u32 bar_low;
396         int region;
397
398         region = xgene_pcie_select_ib_reg(ib_reg_mask, range->size);
399         if (region < 0) {
400                 dev_warn(dev, "invalid pcie dma-range config\n");
401                 return;
402         }
403
404         if (range->flags & IORESOURCE_PREFETCH)
405                 flags |= PCI_BASE_ADDRESS_MEM_PREFETCH;
406
407         bar_low = pcie_bar_low_val((u32)cpu_addr, flags);
408         switch (region) {
409         case 0:
410                 xgene_pcie_set_ib_mask(csr_base, BRIDGE_CFG_4, flags, size);
411                 bar_addr = cfg_base + PCI_BASE_ADDRESS_0;
412                 writel(bar_low, bar_addr);
413                 writel(upper_32_bits(cpu_addr), bar_addr + 0x4);
414                 pim_addr = csr_base + PIM1_1L;
415                 break;
416         case 1:
417                 bar_addr = csr_base + IBAR2;
418                 writel(bar_low, bar_addr);
419                 writel(lower_32_bits(mask), csr_base + IR2MSK);
420                 pim_addr = csr_base + PIM2_1L;
421                 break;
422         case 2:
423                 bar_addr = csr_base + IBAR3L;
424                 writel(bar_low, bar_addr);
425                 writel(upper_32_bits(cpu_addr), bar_addr + 0x4);
426                 writel(lower_32_bits(mask), csr_base + IR3MSKL);
427                 writel(upper_32_bits(mask), csr_base + IR3MSKL + 0x4);
428                 pim_addr = csr_base + PIM3_1L;
429                 break;
430         }
431
432         xgene_pcie_setup_pims(pim_addr, pci_addr, ~(size - 1));
433 }
434
435 static int pci_dma_range_parser_init(struct of_pci_range_parser *parser,
436                                      struct device_node *node)
437 {
438         const int na = 3, ns = 2;
439         int rlen;
440
441         parser->node = node;
442         parser->pna = of_n_addr_cells(node);
443         parser->np = parser->pna + na + ns;
444
445         parser->range = of_get_property(node, "dma-ranges", &rlen);
446         if (!parser->range)
447                 return -ENOENT;
448         parser->end = parser->range + rlen / sizeof(__be32);
449
450         return 0;
451 }
452
453 static int xgene_pcie_parse_map_dma_ranges(struct xgene_pcie_port *port)
454 {
455         struct device_node *np = port->node;
456         struct of_pci_range range;
457         struct of_pci_range_parser parser;
458         struct device *dev = port->dev;
459         u8 ib_reg_mask = 0;
460
461         if (pci_dma_range_parser_init(&parser, np)) {
462                 dev_err(dev, "missing dma-ranges property\n");
463                 return -EINVAL;
464         }
465
466         /* Get the dma-ranges from DT */
467         for_each_of_pci_range(&parser, &range) {
468                 u64 end = range.cpu_addr + range.size - 1;
469
470                 dev_dbg(dev, "0x%08x 0x%016llx..0x%016llx -> 0x%016llx\n",
471                         range.flags, range.cpu_addr, end, range.pci_addr);
472                 xgene_pcie_setup_ib_reg(port, &range, &ib_reg_mask);
473         }
474         return 0;
475 }
476
477 /* clear BAR configuration which was done by firmware */
478 static void xgene_pcie_clear_config(struct xgene_pcie_port *port)
479 {
480         int i;
481
482         for (i = PIM1_1L; i <= CFGCTL; i += 4)
483                 writel(0x0, port->csr_base + i);
484 }
485
486 static int xgene_pcie_setup(struct xgene_pcie_port *port,
487                             struct list_head *res,
488                             resource_size_t io_base)
489 {
490         struct device *dev = port->dev;
491         u32 val, lanes = 0, speed = 0;
492         int ret;
493
494         xgene_pcie_clear_config(port);
495
496         /* setup the vendor and device IDs correctly */
497         val = (XGENE_PCIE_DEVICEID << 16) | XGENE_PCIE_VENDORID;
498         writel(val, port->csr_base + BRIDGE_CFG_0);
499
500         ret = xgene_pcie_map_ranges(port, res, io_base);
501         if (ret)
502                 return ret;
503
504         ret = xgene_pcie_parse_map_dma_ranges(port);
505         if (ret)
506                 return ret;
507
508         xgene_pcie_linkup(port, &lanes, &speed);
509         if (!port->link_up)
510                 dev_info(dev, "(rc) link down\n");
511         else
512                 dev_info(dev, "(rc) x%d gen-%d link up\n", lanes, speed + 1);
513         return 0;
514 }
515
516 static int xgene_pcie_probe_bridge(struct platform_device *pdev)
517 {
518         struct device *dev = &pdev->dev;
519         struct device_node *dn = dev->of_node;
520         struct xgene_pcie_port *port;
521         resource_size_t iobase = 0;
522         struct pci_bus *bus;
523         int ret;
524         LIST_HEAD(res);
525
526         port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
527         if (!port)
528                 return -ENOMEM;
529
530         port->node = of_node_get(dn);
531         port->dev = dev;
532
533         port->version = XGENE_PCIE_IP_VER_UNKN;
534         if (of_device_is_compatible(port->node, "apm,xgene-pcie"))
535                 port->version = XGENE_PCIE_IP_VER_1;
536
537         ret = xgene_pcie_map_reg(port, pdev);
538         if (ret)
539                 return ret;
540
541         ret = xgene_pcie_init_port(port);
542         if (ret)
543                 return ret;
544
545         ret = of_pci_get_host_bridge_resources(dn, 0, 0xff, &res, &iobase);
546         if (ret)
547                 return ret;
548
549         ret = devm_request_pci_bus_resources(dev, &res);
550         if (ret)
551                 goto error;
552
553         ret = xgene_pcie_setup(port, &res, iobase);
554         if (ret)
555                 goto error;
556
557         bus = pci_create_root_bus(dev, 0, &xgene_pcie_ops, port, &res);
558         if (!bus) {
559                 ret = -ENOMEM;
560                 goto error;
561         }
562
563         pci_scan_child_bus(bus);
564         pci_assign_unassigned_bus_resources(bus);
565         pci_bus_add_devices(bus);
566
567         platform_set_drvdata(pdev, port);
568         return 0;
569
570 error:
571         pci_free_resource_list(&res);
572         return ret;
573 }
574
575 static const struct of_device_id xgene_pcie_match_table[] = {
576         {.compatible = "apm,xgene-pcie",},
577         {},
578 };
579
580 static struct platform_driver xgene_pcie_driver = {
581         .driver = {
582                    .name = "xgene-pcie",
583                    .of_match_table = of_match_ptr(xgene_pcie_match_table),
584         },
585         .probe = xgene_pcie_probe_bridge,
586 };
587 builtin_platform_driver(xgene_pcie_driver);