Merge branch 'gup_flag-cleanups'
[cascardo/linux.git] / drivers / pci / host / pcie-iproc.c
1 /*
2  * Copyright (C) 2014 Hauke Mehrtens <hauke@hauke-m.de>
3  * Copyright (C) 2015 Broadcom Corporation
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation version 2.
8  *
9  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
10  * kind, whether express or implied; without even the implied warranty
11  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <linux/kernel.h>
16 #include <linux/pci.h>
17 #include <linux/msi.h>
18 #include <linux/clk.h>
19 #include <linux/module.h>
20 #include <linux/mbus.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/interrupt.h>
24 #include <linux/platform_device.h>
25 #include <linux/of_address.h>
26 #include <linux/of_pci.h>
27 #include <linux/of_irq.h>
28 #include <linux/of_platform.h>
29 #include <linux/phy/phy.h>
30
31 #include "pcie-iproc.h"
32
33 #define EP_PERST_SOURCE_SELECT_SHIFT 2
34 #define EP_PERST_SOURCE_SELECT       BIT(EP_PERST_SOURCE_SELECT_SHIFT)
35 #define EP_MODE_SURVIVE_PERST_SHIFT  1
36 #define EP_MODE_SURVIVE_PERST        BIT(EP_MODE_SURVIVE_PERST_SHIFT)
37 #define RC_PCIE_RST_OUTPUT_SHIFT     0
38 #define RC_PCIE_RST_OUTPUT           BIT(RC_PCIE_RST_OUTPUT_SHIFT)
39 #define PAXC_RESET_MASK              0x7f
40
41 #define CFG_IND_ADDR_MASK            0x00001ffc
42
43 #define CFG_ADDR_BUS_NUM_SHIFT       20
44 #define CFG_ADDR_BUS_NUM_MASK        0x0ff00000
45 #define CFG_ADDR_DEV_NUM_SHIFT       15
46 #define CFG_ADDR_DEV_NUM_MASK        0x000f8000
47 #define CFG_ADDR_FUNC_NUM_SHIFT      12
48 #define CFG_ADDR_FUNC_NUM_MASK       0x00007000
49 #define CFG_ADDR_REG_NUM_SHIFT       2
50 #define CFG_ADDR_REG_NUM_MASK        0x00000ffc
51 #define CFG_ADDR_CFG_TYPE_SHIFT      0
52 #define CFG_ADDR_CFG_TYPE_MASK       0x00000003
53
54 #define SYS_RC_INTX_MASK             0xf
55
56 #define PCIE_PHYLINKUP_SHIFT         3
57 #define PCIE_PHYLINKUP               BIT(PCIE_PHYLINKUP_SHIFT)
58 #define PCIE_DL_ACTIVE_SHIFT         2
59 #define PCIE_DL_ACTIVE               BIT(PCIE_DL_ACTIVE_SHIFT)
60
61 #define OARR_VALID_SHIFT             0
62 #define OARR_VALID                   BIT(OARR_VALID_SHIFT)
63 #define OARR_SIZE_CFG_SHIFT          1
64 #define OARR_SIZE_CFG                BIT(OARR_SIZE_CFG_SHIFT)
65
66 #define PCI_EXP_CAP                     0xac
67
68 #define MAX_NUM_OB_WINDOWS           2
69
70 #define IPROC_PCIE_REG_INVALID 0xffff
71
72 enum iproc_pcie_reg {
73         IPROC_PCIE_CLK_CTRL = 0,
74         IPROC_PCIE_CFG_IND_ADDR,
75         IPROC_PCIE_CFG_IND_DATA,
76         IPROC_PCIE_CFG_ADDR,
77         IPROC_PCIE_CFG_DATA,
78         IPROC_PCIE_INTX_EN,
79         IPROC_PCIE_OARR_LO,
80         IPROC_PCIE_OARR_HI,
81         IPROC_PCIE_OMAP_LO,
82         IPROC_PCIE_OMAP_HI,
83         IPROC_PCIE_LINK_STATUS,
84 };
85
86 /* iProc PCIe PAXB registers */
87 static const u16 iproc_pcie_reg_paxb[] = {
88         [IPROC_PCIE_CLK_CTRL]     = 0x000,
89         [IPROC_PCIE_CFG_IND_ADDR] = 0x120,
90         [IPROC_PCIE_CFG_IND_DATA] = 0x124,
91         [IPROC_PCIE_CFG_ADDR]     = 0x1f8,
92         [IPROC_PCIE_CFG_DATA]     = 0x1fc,
93         [IPROC_PCIE_INTX_EN]      = 0x330,
94         [IPROC_PCIE_OARR_LO]      = 0xd20,
95         [IPROC_PCIE_OARR_HI]      = 0xd24,
96         [IPROC_PCIE_OMAP_LO]      = 0xd40,
97         [IPROC_PCIE_OMAP_HI]      = 0xd44,
98         [IPROC_PCIE_LINK_STATUS]  = 0xf0c,
99 };
100
101 /* iProc PCIe PAXC v1 registers */
102 static const u16 iproc_pcie_reg_paxc[] = {
103         [IPROC_PCIE_CLK_CTRL]     = 0x000,
104         [IPROC_PCIE_CFG_IND_ADDR] = 0x1f0,
105         [IPROC_PCIE_CFG_IND_DATA] = 0x1f4,
106         [IPROC_PCIE_CFG_ADDR]     = 0x1f8,
107         [IPROC_PCIE_CFG_DATA]     = 0x1fc,
108         [IPROC_PCIE_INTX_EN]      = IPROC_PCIE_REG_INVALID,
109         [IPROC_PCIE_OARR_LO]      = IPROC_PCIE_REG_INVALID,
110         [IPROC_PCIE_OARR_HI]      = IPROC_PCIE_REG_INVALID,
111         [IPROC_PCIE_OMAP_LO]      = IPROC_PCIE_REG_INVALID,
112         [IPROC_PCIE_OMAP_HI]      = IPROC_PCIE_REG_INVALID,
113         [IPROC_PCIE_LINK_STATUS]  = IPROC_PCIE_REG_INVALID,
114 };
115
116 static inline struct iproc_pcie *iproc_data(struct pci_bus *bus)
117 {
118         struct iproc_pcie *pcie;
119 #ifdef CONFIG_ARM
120         struct pci_sys_data *sys = bus->sysdata;
121
122         pcie = sys->private_data;
123 #else
124         pcie = bus->sysdata;
125 #endif
126         return pcie;
127 }
128
129 static inline bool iproc_pcie_reg_is_invalid(u16 reg_offset)
130 {
131         return !!(reg_offset == IPROC_PCIE_REG_INVALID);
132 }
133
134 static inline u16 iproc_pcie_reg_offset(struct iproc_pcie *pcie,
135                                         enum iproc_pcie_reg reg)
136 {
137         return pcie->reg_offsets[reg];
138 }
139
140 static inline u32 iproc_pcie_read_reg(struct iproc_pcie *pcie,
141                                       enum iproc_pcie_reg reg)
142 {
143         u16 offset = iproc_pcie_reg_offset(pcie, reg);
144
145         if (iproc_pcie_reg_is_invalid(offset))
146                 return 0;
147
148         return readl(pcie->base + offset);
149 }
150
151 static inline void iproc_pcie_write_reg(struct iproc_pcie *pcie,
152                                         enum iproc_pcie_reg reg, u32 val)
153 {
154         u16 offset = iproc_pcie_reg_offset(pcie, reg);
155
156         if (iproc_pcie_reg_is_invalid(offset))
157                 return;
158
159         writel(val, pcie->base + offset);
160 }
161
162 static inline void iproc_pcie_ob_write(struct iproc_pcie *pcie,
163                                        enum iproc_pcie_reg reg,
164                                        unsigned window, u32 val)
165 {
166         u16 offset = iproc_pcie_reg_offset(pcie, reg);
167
168         if (iproc_pcie_reg_is_invalid(offset))
169                 return;
170
171         writel(val, pcie->base + offset + (window * 8));
172 }
173
174 /**
175  * Note access to the configuration registers are protected at the higher layer
176  * by 'pci_lock' in drivers/pci/access.c
177  */
178 static void __iomem *iproc_pcie_map_cfg_bus(struct pci_bus *bus,
179                                             unsigned int devfn,
180                                             int where)
181 {
182         struct iproc_pcie *pcie = iproc_data(bus);
183         unsigned slot = PCI_SLOT(devfn);
184         unsigned fn = PCI_FUNC(devfn);
185         unsigned busno = bus->number;
186         u32 val;
187         u16 offset;
188
189         /* root complex access */
190         if (busno == 0) {
191                 if (slot > 0 || fn > 0)
192                         return NULL;
193
194                 iproc_pcie_write_reg(pcie, IPROC_PCIE_CFG_IND_ADDR,
195                                      where & CFG_IND_ADDR_MASK);
196                 offset = iproc_pcie_reg_offset(pcie, IPROC_PCIE_CFG_IND_DATA);
197                 if (iproc_pcie_reg_is_invalid(offset))
198                         return NULL;
199                 else
200                         return (pcie->base + offset);
201         }
202
203         /*
204          * PAXC is connected to an internally emulated EP within the SoC.  It
205          * allows only one device.
206          */
207         if (pcie->type == IPROC_PCIE_PAXC)
208                 if (slot > 0)
209                         return NULL;
210
211         /* EP device access */
212         val = (busno << CFG_ADDR_BUS_NUM_SHIFT) |
213                 (slot << CFG_ADDR_DEV_NUM_SHIFT) |
214                 (fn << CFG_ADDR_FUNC_NUM_SHIFT) |
215                 (where & CFG_ADDR_REG_NUM_MASK) |
216                 (1 & CFG_ADDR_CFG_TYPE_MASK);
217         iproc_pcie_write_reg(pcie, IPROC_PCIE_CFG_ADDR, val);
218         offset = iproc_pcie_reg_offset(pcie, IPROC_PCIE_CFG_DATA);
219         if (iproc_pcie_reg_is_invalid(offset))
220                 return NULL;
221         else
222                 return (pcie->base + offset);
223 }
224
225 static struct pci_ops iproc_pcie_ops = {
226         .map_bus = iproc_pcie_map_cfg_bus,
227         .read = pci_generic_config_read32,
228         .write = pci_generic_config_write32,
229 };
230
231 static void iproc_pcie_reset(struct iproc_pcie *pcie)
232 {
233         u32 val;
234
235         if (pcie->type == IPROC_PCIE_PAXC) {
236                 val = iproc_pcie_read_reg(pcie, IPROC_PCIE_CLK_CTRL);
237                 val &= ~PAXC_RESET_MASK;
238                 iproc_pcie_write_reg(pcie, IPROC_PCIE_CLK_CTRL, val);
239                 udelay(100);
240                 val |= PAXC_RESET_MASK;
241                 iproc_pcie_write_reg(pcie, IPROC_PCIE_CLK_CTRL, val);
242                 udelay(100);
243                 return;
244         }
245
246         /*
247          * Select perst_b signal as reset source. Put the device into reset,
248          * and then bring it out of reset
249          */
250         val = iproc_pcie_read_reg(pcie, IPROC_PCIE_CLK_CTRL);
251         val &= ~EP_PERST_SOURCE_SELECT & ~EP_MODE_SURVIVE_PERST &
252                 ~RC_PCIE_RST_OUTPUT;
253         iproc_pcie_write_reg(pcie, IPROC_PCIE_CLK_CTRL, val);
254         udelay(250);
255
256         val |= RC_PCIE_RST_OUTPUT;
257         iproc_pcie_write_reg(pcie, IPROC_PCIE_CLK_CTRL, val);
258         msleep(100);
259 }
260
261 static int iproc_pcie_check_link(struct iproc_pcie *pcie, struct pci_bus *bus)
262 {
263         struct device *dev = pcie->dev;
264         u8 hdr_type;
265         u32 link_ctrl, class, val;
266         u16 pos = PCI_EXP_CAP, link_status;
267         bool link_is_active = false;
268
269         /*
270          * PAXC connects to emulated endpoint devices directly and does not
271          * have a Serdes.  Therefore skip the link detection logic here.
272          */
273         if (pcie->type == IPROC_PCIE_PAXC)
274                 return 0;
275
276         val = iproc_pcie_read_reg(pcie, IPROC_PCIE_LINK_STATUS);
277         if (!(val & PCIE_PHYLINKUP) || !(val & PCIE_DL_ACTIVE)) {
278                 dev_err(dev, "PHY or data link is INACTIVE!\n");
279                 return -ENODEV;
280         }
281
282         /* make sure we are not in EP mode */
283         pci_bus_read_config_byte(bus, 0, PCI_HEADER_TYPE, &hdr_type);
284         if ((hdr_type & 0x7f) != PCI_HEADER_TYPE_BRIDGE) {
285                 dev_err(dev, "in EP mode, hdr=%#02x\n", hdr_type);
286                 return -EFAULT;
287         }
288
289         /* force class to PCI_CLASS_BRIDGE_PCI (0x0604) */
290 #define PCI_BRIDGE_CTRL_REG_OFFSET 0x43c
291 #define PCI_CLASS_BRIDGE_MASK      0xffff00
292 #define PCI_CLASS_BRIDGE_SHIFT     8
293         pci_bus_read_config_dword(bus, 0, PCI_BRIDGE_CTRL_REG_OFFSET, &class);
294         class &= ~PCI_CLASS_BRIDGE_MASK;
295         class |= (PCI_CLASS_BRIDGE_PCI << PCI_CLASS_BRIDGE_SHIFT);
296         pci_bus_write_config_dword(bus, 0, PCI_BRIDGE_CTRL_REG_OFFSET, class);
297
298         /* check link status to see if link is active */
299         pci_bus_read_config_word(bus, 0, pos + PCI_EXP_LNKSTA, &link_status);
300         if (link_status & PCI_EXP_LNKSTA_NLW)
301                 link_is_active = true;
302
303         if (!link_is_active) {
304                 /* try GEN 1 link speed */
305 #define PCI_TARGET_LINK_SPEED_MASK    0xf
306 #define PCI_TARGET_LINK_SPEED_GEN2    0x2
307 #define PCI_TARGET_LINK_SPEED_GEN1    0x1
308                 pci_bus_read_config_dword(bus, 0,
309                                           pos + PCI_EXP_LNKCTL2,
310                                           &link_ctrl);
311                 if ((link_ctrl & PCI_TARGET_LINK_SPEED_MASK) ==
312                     PCI_TARGET_LINK_SPEED_GEN2) {
313                         link_ctrl &= ~PCI_TARGET_LINK_SPEED_MASK;
314                         link_ctrl |= PCI_TARGET_LINK_SPEED_GEN1;
315                         pci_bus_write_config_dword(bus, 0,
316                                            pos + PCI_EXP_LNKCTL2,
317                                            link_ctrl);
318                         msleep(100);
319
320                         pci_bus_read_config_word(bus, 0, pos + PCI_EXP_LNKSTA,
321                                                  &link_status);
322                         if (link_status & PCI_EXP_LNKSTA_NLW)
323                                 link_is_active = true;
324                 }
325         }
326
327         dev_info(dev, "link: %s\n", link_is_active ? "UP" : "DOWN");
328
329         return link_is_active ? 0 : -ENODEV;
330 }
331
332 static void iproc_pcie_enable(struct iproc_pcie *pcie)
333 {
334         iproc_pcie_write_reg(pcie, IPROC_PCIE_INTX_EN, SYS_RC_INTX_MASK);
335 }
336
337 /**
338  * Some iProc SoCs require the SW to configure the outbound address mapping
339  *
340  * Outbound address translation:
341  *
342  * iproc_pcie_address = axi_address - axi_offset
343  * OARR = iproc_pcie_address
344  * OMAP = pci_addr
345  *
346  * axi_addr -> iproc_pcie_address -> OARR -> OMAP -> pci_address
347  */
348 static int iproc_pcie_setup_ob(struct iproc_pcie *pcie, u64 axi_addr,
349                                u64 pci_addr, resource_size_t size)
350 {
351         struct iproc_pcie_ob *ob = &pcie->ob;
352         struct device *dev = pcie->dev;
353         unsigned i;
354         u64 max_size = (u64)ob->window_size * MAX_NUM_OB_WINDOWS;
355         u64 remainder;
356
357         if (size > max_size) {
358                 dev_err(dev,
359                         "res size %pap exceeds max supported size 0x%llx\n",
360                         &size, max_size);
361                 return -EINVAL;
362         }
363
364         div64_u64_rem(size, ob->window_size, &remainder);
365         if (remainder) {
366                 dev_err(dev,
367                         "res size %pap needs to be multiple of window size %pap\n",
368                         &size, &ob->window_size);
369                 return -EINVAL;
370         }
371
372         if (axi_addr < ob->axi_offset) {
373                 dev_err(dev, "axi address %pap less than offset %pap\n",
374                         &axi_addr, &ob->axi_offset);
375                 return -EINVAL;
376         }
377
378         /*
379          * Translate the AXI address to the internal address used by the iProc
380          * PCIe core before programming the OARR
381          */
382         axi_addr -= ob->axi_offset;
383
384         for (i = 0; i < MAX_NUM_OB_WINDOWS; i++) {
385                 iproc_pcie_ob_write(pcie, IPROC_PCIE_OARR_LO, i,
386                                     lower_32_bits(axi_addr) | OARR_VALID |
387                                     (ob->set_oarr_size ? 1 : 0));
388                 iproc_pcie_ob_write(pcie, IPROC_PCIE_OARR_HI, i,
389                                     upper_32_bits(axi_addr));
390                 iproc_pcie_ob_write(pcie, IPROC_PCIE_OMAP_LO, i,
391                                     lower_32_bits(pci_addr));
392                 iproc_pcie_ob_write(pcie, IPROC_PCIE_OMAP_HI, i,
393                                     upper_32_bits(pci_addr));
394
395                 size -= ob->window_size;
396                 if (size == 0)
397                         break;
398
399                 axi_addr += ob->window_size;
400                 pci_addr += ob->window_size;
401         }
402
403         return 0;
404 }
405
406 static int iproc_pcie_map_ranges(struct iproc_pcie *pcie,
407                                  struct list_head *resources)
408 {
409         struct device *dev = pcie->dev;
410         struct resource_entry *window;
411         int ret;
412
413         resource_list_for_each_entry(window, resources) {
414                 struct resource *res = window->res;
415                 u64 res_type = resource_type(res);
416
417                 switch (res_type) {
418                 case IORESOURCE_IO:
419                 case IORESOURCE_BUS:
420                         break;
421                 case IORESOURCE_MEM:
422                         ret = iproc_pcie_setup_ob(pcie, res->start,
423                                                   res->start - window->offset,
424                                                   resource_size(res));
425                         if (ret)
426                                 return ret;
427                         break;
428                 default:
429                         dev_err(dev, "invalid resource %pR\n", res);
430                         return -EINVAL;
431                 }
432         }
433
434         return 0;
435 }
436
437 static int iproc_pcie_msi_enable(struct iproc_pcie *pcie)
438 {
439         struct device_node *msi_node;
440
441         msi_node = of_parse_phandle(pcie->dev->of_node, "msi-parent", 0);
442         if (!msi_node)
443                 return -ENODEV;
444
445         /*
446          * If another MSI controller is being used, the call below should fail
447          * but that is okay
448          */
449         return iproc_msi_init(pcie, msi_node);
450 }
451
452 static void iproc_pcie_msi_disable(struct iproc_pcie *pcie)
453 {
454         iproc_msi_exit(pcie);
455 }
456
457 int iproc_pcie_setup(struct iproc_pcie *pcie, struct list_head *res)
458 {
459         struct device *dev;
460         int ret;
461         void *sysdata;
462         struct pci_bus *bus;
463
464         dev = pcie->dev;
465         ret = devm_request_pci_bus_resources(dev, res);
466         if (ret)
467                 return ret;
468
469         ret = phy_init(pcie->phy);
470         if (ret) {
471                 dev_err(dev, "unable to initialize PCIe PHY\n");
472                 return ret;
473         }
474
475         ret = phy_power_on(pcie->phy);
476         if (ret) {
477                 dev_err(dev, "unable to power on PCIe PHY\n");
478                 goto err_exit_phy;
479         }
480
481         switch (pcie->type) {
482         case IPROC_PCIE_PAXB:
483                 pcie->reg_offsets = iproc_pcie_reg_paxb;
484                 break;
485         case IPROC_PCIE_PAXC:
486                 pcie->reg_offsets = iproc_pcie_reg_paxc;
487                 break;
488         default:
489                 dev_err(dev, "incompatible iProc PCIe interface\n");
490                 ret = -EINVAL;
491                 goto err_power_off_phy;
492         }
493
494         iproc_pcie_reset(pcie);
495
496         if (pcie->need_ob_cfg) {
497                 ret = iproc_pcie_map_ranges(pcie, res);
498                 if (ret) {
499                         dev_err(dev, "map failed\n");
500                         goto err_power_off_phy;
501                 }
502         }
503
504 #ifdef CONFIG_ARM
505         pcie->sysdata.private_data = pcie;
506         sysdata = &pcie->sysdata;
507 #else
508         sysdata = pcie;
509 #endif
510
511         bus = pci_create_root_bus(dev, 0, &iproc_pcie_ops, sysdata, res);
512         if (!bus) {
513                 dev_err(dev, "unable to create PCI root bus\n");
514                 ret = -ENOMEM;
515                 goto err_power_off_phy;
516         }
517         pcie->root_bus = bus;
518
519         ret = iproc_pcie_check_link(pcie, bus);
520         if (ret) {
521                 dev_err(dev, "no PCIe EP device detected\n");
522                 goto err_rm_root_bus;
523         }
524
525         iproc_pcie_enable(pcie);
526
527         if (IS_ENABLED(CONFIG_PCI_MSI))
528                 if (iproc_pcie_msi_enable(pcie))
529                         dev_info(dev, "not using iProc MSI\n");
530
531         pci_scan_child_bus(bus);
532         pci_assign_unassigned_bus_resources(bus);
533         pci_fixup_irqs(pci_common_swizzle, pcie->map_irq);
534         pci_bus_add_devices(bus);
535
536         return 0;
537
538 err_rm_root_bus:
539         pci_stop_root_bus(bus);
540         pci_remove_root_bus(bus);
541
542 err_power_off_phy:
543         phy_power_off(pcie->phy);
544 err_exit_phy:
545         phy_exit(pcie->phy);
546         return ret;
547 }
548 EXPORT_SYMBOL(iproc_pcie_setup);
549
550 int iproc_pcie_remove(struct iproc_pcie *pcie)
551 {
552         pci_stop_root_bus(pcie->root_bus);
553         pci_remove_root_bus(pcie->root_bus);
554
555         iproc_pcie_msi_disable(pcie);
556
557         phy_power_off(pcie->phy);
558         phy_exit(pcie->phy);
559
560         return 0;
561 }
562 EXPORT_SYMBOL(iproc_pcie_remove);
563
564 MODULE_AUTHOR("Ray Jui <rjui@broadcom.com>");
565 MODULE_DESCRIPTION("Broadcom iPROC PCIe common driver");
566 MODULE_LICENSE("GPL v2");