2 * PCIe host controller driver for Samsung EXYNOS SoCs
4 * Copyright (C) 2013 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com
7 * Author: Jingoo Han <jg1.han@samsung.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/gpio.h>
17 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/list.h>
20 #include <linux/module.h>
22 #include <linux/of_address.h>
23 #include <linux/of_gpio.h>
24 #include <linux/of_pci.h>
25 #include <linux/pci.h>
26 #include <linux/pci_regs.h>
27 #include <linux/platform_device.h>
28 #include <linux/resource.h>
29 #include <linux/signal.h>
30 #include <linux/slab.h>
31 #include <linux/types.h>
33 struct pcie_port_info {
38 phys_addr_t io_bus_addr;
39 phys_addr_t mem_bus_addr;
46 void __iomem *dbi_base;
47 void __iomem *elbi_base;
48 void __iomem *phy_base;
49 void __iomem *purple_base;
51 void __iomem *va_cfg0_base;
53 void __iomem *va_cfg1_base;
60 struct pcie_port_info config;
68 * Exynos PCIe IP consists of Synopsys specific part and Exynos
69 * specific part. Only core block is a Synopsys designware part;
70 * other parts are Exynos specific.
73 /* Synopsis specific PCIE configuration registers */
74 #define PCIE_PORT_LINK_CONTROL 0x710
75 #define PORT_LINK_MODE_MASK (0x3f << 16)
76 #define PORT_LINK_MODE_4_LANES (0x7 << 16)
78 #define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C
79 #define PORT_LOGIC_SPEED_CHANGE (0x1 << 17)
80 #define PORT_LOGIC_LINK_WIDTH_MASK (0x1ff << 8)
81 #define PORT_LOGIC_LINK_WIDTH_4_LANES (0x7 << 8)
83 #define PCIE_MSI_ADDR_LO 0x820
84 #define PCIE_MSI_ADDR_HI 0x824
85 #define PCIE_MSI_INTR0_ENABLE 0x828
86 #define PCIE_MSI_INTR0_MASK 0x82C
87 #define PCIE_MSI_INTR0_STATUS 0x830
89 #define PCIE_ATU_VIEWPORT 0x900
90 #define PCIE_ATU_REGION_INBOUND (0x1 << 31)
91 #define PCIE_ATU_REGION_OUTBOUND (0x0 << 31)
92 #define PCIE_ATU_REGION_INDEX1 (0x1 << 0)
93 #define PCIE_ATU_REGION_INDEX0 (0x0 << 0)
94 #define PCIE_ATU_CR1 0x904
95 #define PCIE_ATU_TYPE_MEM (0x0 << 0)
96 #define PCIE_ATU_TYPE_IO (0x2 << 0)
97 #define PCIE_ATU_TYPE_CFG0 (0x4 << 0)
98 #define PCIE_ATU_TYPE_CFG1 (0x5 << 0)
99 #define PCIE_ATU_CR2 0x908
100 #define PCIE_ATU_ENABLE (0x1 << 31)
101 #define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30)
102 #define PCIE_ATU_LOWER_BASE 0x90C
103 #define PCIE_ATU_UPPER_BASE 0x910
104 #define PCIE_ATU_LIMIT 0x914
105 #define PCIE_ATU_LOWER_TARGET 0x918
106 #define PCIE_ATU_BUS(x) (((x) & 0xff) << 24)
107 #define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19)
108 #define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16)
109 #define PCIE_ATU_UPPER_TARGET 0x91C
111 /* Exynos specific PCIE configuration registers */
113 /* PCIe ELBI registers */
114 #define PCIE_IRQ_PULSE 0x000
115 #define IRQ_INTA_ASSERT (0x1 << 0)
116 #define IRQ_INTB_ASSERT (0x1 << 2)
117 #define IRQ_INTC_ASSERT (0x1 << 4)
118 #define IRQ_INTD_ASSERT (0x1 << 6)
119 #define PCIE_IRQ_LEVEL 0x004
120 #define PCIE_IRQ_SPECIAL 0x008
121 #define PCIE_IRQ_EN_PULSE 0x00c
122 #define PCIE_IRQ_EN_LEVEL 0x010
123 #define PCIE_IRQ_EN_SPECIAL 0x014
124 #define PCIE_PWR_RESET 0x018
125 #define PCIE_CORE_RESET 0x01c
126 #define PCIE_CORE_RESET_ENABLE (0x1 << 0)
127 #define PCIE_STICKY_RESET 0x020
128 #define PCIE_NONSTICKY_RESET 0x024
129 #define PCIE_APP_INIT_RESET 0x028
130 #define PCIE_APP_LTSSM_ENABLE 0x02c
131 #define PCIE_ELBI_RDLH_LINKUP 0x064
132 #define PCIE_ELBI_LTSSM_ENABLE 0x1
133 #define PCIE_ELBI_SLV_AWMISC 0x11c
134 #define PCIE_ELBI_SLV_ARMISC 0x120
135 #define PCIE_ELBI_SLV_DBI_ENABLE (0x1 << 21)
137 /* PCIe Purple registers */
138 #define PCIE_PHY_GLOBAL_RESET 0x000
139 #define PCIE_PHY_COMMON_RESET 0x004
140 #define PCIE_PHY_CMN_REG 0x008
141 #define PCIE_PHY_MAC_RESET 0x00c
142 #define PCIE_PHY_PLL_LOCKED 0x010
143 #define PCIE_PHY_TRSVREG_RESET 0x020
144 #define PCIE_PHY_TRSV_RESET 0x024
146 /* PCIe PHY registers */
147 #define PCIE_PHY_IMPEDANCE 0x004
148 #define PCIE_PHY_PLL_DIV_0 0x008
149 #define PCIE_PHY_PLL_BIAS 0x00c
150 #define PCIE_PHY_DCC_FEEDBACK 0x014
151 #define PCIE_PHY_PLL_DIV_1 0x05c
152 #define PCIE_PHY_TRSV0_EMP_LVL 0x084
153 #define PCIE_PHY_TRSV0_DRV_LVL 0x088
154 #define PCIE_PHY_TRSV0_RXCDR 0x0ac
155 #define PCIE_PHY_TRSV0_LVCC 0x0dc
156 #define PCIE_PHY_TRSV1_EMP_LVL 0x144
157 #define PCIE_PHY_TRSV1_RXCDR 0x16c
158 #define PCIE_PHY_TRSV1_LVCC 0x19c
159 #define PCIE_PHY_TRSV2_EMP_LVL 0x204
160 #define PCIE_PHY_TRSV2_RXCDR 0x22c
161 #define PCIE_PHY_TRSV2_LVCC 0x25c
162 #define PCIE_PHY_TRSV3_EMP_LVL 0x2c4
163 #define PCIE_PHY_TRSV3_RXCDR 0x2ec
164 #define PCIE_PHY_TRSV3_LVCC 0x31c
166 static struct hw_pci exynos_pci;
168 static inline struct pcie_port *sys_to_pcie(struct pci_sys_data *sys)
170 return sys->private_data;
173 static inline int cfg_read(void *addr, int where, int size, u32 *val)
178 *val = (*val >> (8 * (where & 3))) & 0xff;
180 *val = (*val >> (8 * (where & 3))) & 0xffff;
182 return PCIBIOS_BAD_REGISTER_NUMBER;
184 return PCIBIOS_SUCCESSFUL;
187 static inline int cfg_write(void *addr, int where, int size, u32 val)
192 writew(val, addr + (where & 2));
194 writeb(val, addr + (where & 3));
196 return PCIBIOS_BAD_REGISTER_NUMBER;
198 return PCIBIOS_SUCCESSFUL;
201 static void exynos_pcie_sideband_dbi_w_mode(struct pcie_port *pp, bool on)
206 val = readl(pp->elbi_base + PCIE_ELBI_SLV_AWMISC);
207 val |= PCIE_ELBI_SLV_DBI_ENABLE;
208 writel(val, pp->elbi_base + PCIE_ELBI_SLV_AWMISC);
210 val = readl(pp->elbi_base + PCIE_ELBI_SLV_AWMISC);
211 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
212 writel(val, pp->elbi_base + PCIE_ELBI_SLV_AWMISC);
216 static void exynos_pcie_sideband_dbi_r_mode(struct pcie_port *pp, bool on)
221 val = readl(pp->elbi_base + PCIE_ELBI_SLV_ARMISC);
222 val |= PCIE_ELBI_SLV_DBI_ENABLE;
223 writel(val, pp->elbi_base + PCIE_ELBI_SLV_ARMISC);
225 val = readl(pp->elbi_base + PCIE_ELBI_SLV_ARMISC);
226 val &= ~PCIE_ELBI_SLV_DBI_ENABLE;
227 writel(val, pp->elbi_base + PCIE_ELBI_SLV_ARMISC);
231 static inline void readl_rc(struct pcie_port *pp, void *dbi_base, u32 *val)
233 exynos_pcie_sideband_dbi_r_mode(pp, true);
234 *val = readl(dbi_base);
235 exynos_pcie_sideband_dbi_r_mode(pp, false);
239 static inline void writel_rc(struct pcie_port *pp, u32 val, void *dbi_base)
241 exynos_pcie_sideband_dbi_w_mode(pp, true);
242 writel(val, dbi_base);
243 exynos_pcie_sideband_dbi_w_mode(pp, false);
247 static int exynos_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
252 exynos_pcie_sideband_dbi_r_mode(pp, true);
253 ret = cfg_read(pp->dbi_base + (where & ~0x3), where, size, val);
254 exynos_pcie_sideband_dbi_r_mode(pp, false);
258 static int exynos_pcie_wr_own_conf(struct pcie_port *pp, int where, int size,
263 exynos_pcie_sideband_dbi_w_mode(pp, true);
264 ret = cfg_write(pp->dbi_base + (where & ~0x3), where, size, val);
265 exynos_pcie_sideband_dbi_w_mode(pp, false);
269 static void exynos_pcie_prog_viewport_cfg0(struct pcie_port *pp, u32 busdev)
272 void __iomem *dbi_base = pp->dbi_base;
274 /* Program viewport 0 : OUTBOUND : CFG0 */
275 val = PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0;
276 writel_rc(pp, val, dbi_base + PCIE_ATU_VIEWPORT);
277 writel_rc(pp, pp->cfg0_base, dbi_base + PCIE_ATU_LOWER_BASE);
278 writel_rc(pp, (pp->cfg0_base >> 32), dbi_base + PCIE_ATU_UPPER_BASE);
279 writel_rc(pp, pp->cfg0_base + pp->config.cfg0_size - 1,
280 dbi_base + PCIE_ATU_LIMIT);
281 writel_rc(pp, busdev, dbi_base + PCIE_ATU_LOWER_TARGET);
282 writel_rc(pp, 0, dbi_base + PCIE_ATU_UPPER_TARGET);
283 writel_rc(pp, PCIE_ATU_TYPE_CFG0, dbi_base + PCIE_ATU_CR1);
284 val = PCIE_ATU_ENABLE;
285 writel_rc(pp, val, dbi_base + PCIE_ATU_CR2);
288 static void exynos_pcie_prog_viewport_cfg1(struct pcie_port *pp, u32 busdev)
291 void __iomem *dbi_base = pp->dbi_base;
293 /* Program viewport 1 : OUTBOUND : CFG1 */
294 val = PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1;
295 writel_rc(pp, val, dbi_base + PCIE_ATU_VIEWPORT);
296 writel_rc(pp, PCIE_ATU_TYPE_CFG1, dbi_base + PCIE_ATU_CR1);
297 val = PCIE_ATU_ENABLE;
298 writel_rc(pp, val, dbi_base + PCIE_ATU_CR2);
299 writel_rc(pp, pp->cfg1_base, dbi_base + PCIE_ATU_LOWER_BASE);
300 writel_rc(pp, (pp->cfg1_base >> 32), dbi_base + PCIE_ATU_UPPER_BASE);
301 writel_rc(pp, pp->cfg1_base + pp->config.cfg1_size - 1,
302 dbi_base + PCIE_ATU_LIMIT);
303 writel_rc(pp, busdev, dbi_base + PCIE_ATU_LOWER_TARGET);
304 writel_rc(pp, 0, dbi_base + PCIE_ATU_UPPER_TARGET);
307 static void exynos_pcie_prog_viewport_mem_outbound(struct pcie_port *pp)
310 void __iomem *dbi_base = pp->dbi_base;
312 /* Program viewport 0 : OUTBOUND : MEM */
313 val = PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0;
314 writel_rc(pp, val, dbi_base + PCIE_ATU_VIEWPORT);
315 writel_rc(pp, PCIE_ATU_TYPE_MEM, dbi_base + PCIE_ATU_CR1);
316 val = PCIE_ATU_ENABLE;
317 writel_rc(pp, val, dbi_base + PCIE_ATU_CR2);
318 writel_rc(pp, pp->mem_base, dbi_base + PCIE_ATU_LOWER_BASE);
319 writel_rc(pp, (pp->mem_base >> 32), dbi_base + PCIE_ATU_UPPER_BASE);
320 writel_rc(pp, pp->mem_base + pp->config.mem_size - 1,
321 dbi_base + PCIE_ATU_LIMIT);
322 writel_rc(pp, pp->config.mem_bus_addr,
323 dbi_base + PCIE_ATU_LOWER_TARGET);
324 writel_rc(pp, upper_32_bits(pp->config.mem_bus_addr),
325 dbi_base + PCIE_ATU_UPPER_TARGET);
328 static void exynos_pcie_prog_viewport_io_outbound(struct pcie_port *pp)
331 void __iomem *dbi_base = pp->dbi_base;
333 /* Program viewport 1 : OUTBOUND : IO */
334 val = PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1;
335 writel_rc(pp, val, dbi_base + PCIE_ATU_VIEWPORT);
336 writel_rc(pp, PCIE_ATU_TYPE_IO, dbi_base + PCIE_ATU_CR1);
337 val = PCIE_ATU_ENABLE;
338 writel_rc(pp, val, dbi_base + PCIE_ATU_CR2);
339 writel_rc(pp, pp->io_base, dbi_base + PCIE_ATU_LOWER_BASE);
340 writel_rc(pp, (pp->io_base >> 32), dbi_base + PCIE_ATU_UPPER_BASE);
341 writel_rc(pp, pp->io_base + pp->config.io_size - 1,
342 dbi_base + PCIE_ATU_LIMIT);
343 writel_rc(pp, pp->config.io_bus_addr,
344 dbi_base + PCIE_ATU_LOWER_TARGET);
345 writel_rc(pp, upper_32_bits(pp->config.io_bus_addr),
346 dbi_base + PCIE_ATU_UPPER_TARGET);
349 static int exynos_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus,
350 u32 devfn, int where, int size, u32 *val)
352 int ret = PCIBIOS_SUCCESSFUL;
355 busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
356 PCIE_ATU_FUNC(PCI_FUNC(devfn));
357 address = where & ~0x3;
359 if (bus->parent->number == pp->root_bus_nr) {
360 exynos_pcie_prog_viewport_cfg0(pp, busdev);
361 ret = cfg_read(pp->va_cfg0_base + address, where, size, val);
362 exynos_pcie_prog_viewport_mem_outbound(pp);
364 exynos_pcie_prog_viewport_cfg1(pp, busdev);
365 ret = cfg_read(pp->va_cfg1_base + address, where, size, val);
366 exynos_pcie_prog_viewport_io_outbound(pp);
372 static int exynos_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus,
373 u32 devfn, int where, int size, u32 val)
375 int ret = PCIBIOS_SUCCESSFUL;
378 busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
379 PCIE_ATU_FUNC(PCI_FUNC(devfn));
380 address = where & ~0x3;
382 if (bus->parent->number == pp->root_bus_nr) {
383 exynos_pcie_prog_viewport_cfg0(pp, busdev);
384 ret = cfg_write(pp->va_cfg0_base + address, where, size, val);
385 exynos_pcie_prog_viewport_mem_outbound(pp);
387 exynos_pcie_prog_viewport_cfg1(pp, busdev);
388 ret = cfg_write(pp->va_cfg1_base + address, where, size, val);
389 exynos_pcie_prog_viewport_io_outbound(pp);
395 static unsigned long global_io_offset;
397 static int exynos_pcie_setup(int nr, struct pci_sys_data *sys)
399 struct pcie_port *pp;
401 pp = sys_to_pcie(sys);
406 if (global_io_offset < SZ_1M && pp->config.io_size > 0) {
407 sys->io_offset = global_io_offset - pp->config.io_bus_addr;
408 pci_ioremap_io(sys->io_offset, pp->io.start);
409 global_io_offset += SZ_64K;
410 pci_add_resource_offset(&sys->resources, &pp->io,
414 sys->mem_offset = pp->mem.start - pp->config.mem_bus_addr;
415 pci_add_resource_offset(&sys->resources, &pp->mem, sys->mem_offset);
420 static int exynos_pcie_link_up(struct pcie_port *pp)
422 u32 val = readl(pp->elbi_base + PCIE_ELBI_RDLH_LINKUP);
424 if (val == PCIE_ELBI_LTSSM_ENABLE)
430 static int exynos_pcie_valid_config(struct pcie_port *pp,
431 struct pci_bus *bus, int dev)
433 /* If there is no link, then there is no device */
434 if (bus->number != pp->root_bus_nr) {
435 if (!exynos_pcie_link_up(pp))
439 /* access only one slot on each root port */
440 if (bus->number == pp->root_bus_nr && dev > 0)
444 * do not read more than one device on the bus directly attached
445 * to RC's (Virtual Bridge's) DS side.
447 if (bus->primary == pp->root_bus_nr && dev > 0)
453 static int exynos_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
456 struct pcie_port *pp = sys_to_pcie(bus->sysdata);
465 if (exynos_pcie_valid_config(pp, bus, PCI_SLOT(devfn)) == 0) {
467 return PCIBIOS_DEVICE_NOT_FOUND;
470 spin_lock_irqsave(&pp->conf_lock, flags);
471 if (bus->number != pp->root_bus_nr)
472 ret = exynos_pcie_rd_other_conf(pp, bus, devfn,
475 ret = exynos_pcie_rd_own_conf(pp, where, size, val);
476 spin_unlock_irqrestore(&pp->conf_lock, flags);
481 static int exynos_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
482 int where, int size, u32 val)
484 struct pcie_port *pp = sys_to_pcie(bus->sysdata);
493 if (exynos_pcie_valid_config(pp, bus, PCI_SLOT(devfn)) == 0)
494 return PCIBIOS_DEVICE_NOT_FOUND;
496 spin_lock_irqsave(&pp->conf_lock, flags);
497 if (bus->number != pp->root_bus_nr)
498 ret = exynos_pcie_wr_other_conf(pp, bus, devfn,
501 ret = exynos_pcie_wr_own_conf(pp, where, size, val);
502 spin_unlock_irqrestore(&pp->conf_lock, flags);
507 static struct pci_ops exynos_pcie_ops = {
508 .read = exynos_pcie_rd_conf,
509 .write = exynos_pcie_wr_conf,
512 static struct pci_bus *exynos_pcie_scan_bus(int nr,
513 struct pci_sys_data *sys)
516 struct pcie_port *pp = sys_to_pcie(sys);
519 pp->root_bus_nr = sys->busnr;
520 bus = pci_scan_root_bus(NULL, sys->busnr, &exynos_pcie_ops,
521 sys, &sys->resources);
530 static int exynos_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
532 struct pcie_port *pp = sys_to_pcie(dev->bus->sysdata);
537 static struct hw_pci exynos_pci = {
538 .setup = exynos_pcie_setup,
539 .scan = exynos_pcie_scan_bus,
540 .map_irq = exynos_pcie_map_irq,
543 static void exynos_pcie_setup_rc(struct pcie_port *pp)
545 struct pcie_port_info *config = &pp->config;
546 void __iomem *dbi_base = pp->dbi_base;
551 /* set the number of lines as 4 */
552 readl_rc(pp, dbi_base + PCIE_PORT_LINK_CONTROL, &val);
553 val &= ~PORT_LINK_MODE_MASK;
554 val |= PORT_LINK_MODE_4_LANES;
555 writel_rc(pp, val, dbi_base + PCIE_PORT_LINK_CONTROL);
557 /* set link width speed control register */
558 readl_rc(pp, dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL, &val);
559 val &= ~PORT_LOGIC_LINK_WIDTH_MASK;
560 val |= PORT_LOGIC_LINK_WIDTH_4_LANES;
561 writel_rc(pp, val, dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
564 writel_rc(pp, 0x00000004, dbi_base + PCI_BASE_ADDRESS_0);
565 writel_rc(pp, 0x00000004, dbi_base + PCI_BASE_ADDRESS_1);
567 /* setup interrupt pins */
568 readl_rc(pp, dbi_base + PCI_INTERRUPT_LINE, &val);
571 writel_rc(pp, val, dbi_base + PCI_INTERRUPT_LINE);
573 /* setup bus numbers */
574 readl_rc(pp, dbi_base + PCI_PRIMARY_BUS, &val);
577 writel_rc(pp, val, dbi_base + PCI_PRIMARY_BUS);
579 /* setup memory base, memory limit */
580 membase = ((u32)pp->mem_base & 0xfff00000) >> 16;
581 memlimit = (config->mem_size + (u32)pp->mem_base) & 0xfff00000;
582 val = memlimit | membase;
583 writel_rc(pp, val, dbi_base + PCI_MEMORY_BASE);
585 /* setup command register */
586 readl_rc(pp, dbi_base + PCI_COMMAND, &val);
588 val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
589 PCI_COMMAND_MASTER | PCI_COMMAND_SERR;
590 writel_rc(pp, val, dbi_base + PCI_COMMAND);
593 static void exynos_pcie_assert_core_reset(struct pcie_port *pp)
596 void __iomem *elbi_base = pp->elbi_base;
598 val = readl(elbi_base + PCIE_CORE_RESET);
599 val &= ~PCIE_CORE_RESET_ENABLE;
600 writel(val, elbi_base + PCIE_CORE_RESET);
601 writel(0, elbi_base + PCIE_PWR_RESET);
602 writel(0, elbi_base + PCIE_STICKY_RESET);
603 writel(0, elbi_base + PCIE_NONSTICKY_RESET);
606 static void exynos_pcie_deassert_core_reset(struct pcie_port *pp)
609 void __iomem *elbi_base = pp->elbi_base;
610 void __iomem *purple_base = pp->purple_base;
612 val = readl(elbi_base + PCIE_CORE_RESET);
613 val |= PCIE_CORE_RESET_ENABLE;
614 writel(val, elbi_base + PCIE_CORE_RESET);
615 writel(1, elbi_base + PCIE_STICKY_RESET);
616 writel(1, elbi_base + PCIE_NONSTICKY_RESET);
617 writel(1, elbi_base + PCIE_APP_INIT_RESET);
618 writel(0, elbi_base + PCIE_APP_INIT_RESET);
619 writel(1, purple_base + PCIE_PHY_MAC_RESET);
622 static void exynos_pcie_assert_phy_reset(struct pcie_port *pp)
624 void __iomem *purple_base = pp->purple_base;
626 writel(0, purple_base + PCIE_PHY_MAC_RESET);
627 writel(1, purple_base + PCIE_PHY_GLOBAL_RESET);
630 static void exynos_pcie_deassert_phy_reset(struct pcie_port *pp)
632 void __iomem *elbi_base = pp->elbi_base;
633 void __iomem *purple_base = pp->purple_base;
635 writel(0, purple_base + PCIE_PHY_GLOBAL_RESET);
636 writel(1, elbi_base + PCIE_PWR_RESET);
637 writel(0, purple_base + PCIE_PHY_COMMON_RESET);
638 writel(0, purple_base + PCIE_PHY_CMN_REG);
639 writel(0, purple_base + PCIE_PHY_TRSVREG_RESET);
640 writel(0, purple_base + PCIE_PHY_TRSV_RESET);
643 static void exynos_pcie_init_phy(struct pcie_port *pp)
645 void __iomem *phy_base = pp->phy_base;
647 /* DCC feedback control off */
648 writel(0x29, phy_base + PCIE_PHY_DCC_FEEDBACK);
650 /* set TX/RX impedance */
651 writel(0xd5, phy_base + PCIE_PHY_IMPEDANCE);
653 /* set 50Mhz PHY clock */
654 writel(0x14, phy_base + PCIE_PHY_PLL_DIV_0);
655 writel(0x12, phy_base + PCIE_PHY_PLL_DIV_1);
657 /* set TX Differential output for lane 0 */
658 writel(0x7f, phy_base + PCIE_PHY_TRSV0_DRV_LVL);
660 /* set TX Pre-emphasis Level Control for lane 0 to minimum */
661 writel(0x0, phy_base + PCIE_PHY_TRSV0_EMP_LVL);
663 /* set RX clock and data recovery bandwidth */
664 writel(0xe7, phy_base + PCIE_PHY_PLL_BIAS);
665 writel(0x82, phy_base + PCIE_PHY_TRSV0_RXCDR);
666 writel(0x82, phy_base + PCIE_PHY_TRSV1_RXCDR);
667 writel(0x82, phy_base + PCIE_PHY_TRSV2_RXCDR);
668 writel(0x82, phy_base + PCIE_PHY_TRSV3_RXCDR);
670 /* change TX Pre-emphasis Level Control for lanes */
671 writel(0x39, phy_base + PCIE_PHY_TRSV0_EMP_LVL);
672 writel(0x39, phy_base + PCIE_PHY_TRSV1_EMP_LVL);
673 writel(0x39, phy_base + PCIE_PHY_TRSV2_EMP_LVL);
674 writel(0x39, phy_base + PCIE_PHY_TRSV3_EMP_LVL);
677 writel(0x20, phy_base + PCIE_PHY_TRSV0_LVCC);
678 writel(0xa0, phy_base + PCIE_PHY_TRSV1_LVCC);
679 writel(0xa0, phy_base + PCIE_PHY_TRSV2_LVCC);
680 writel(0xa0, phy_base + PCIE_PHY_TRSV3_LVCC);
683 static void exynos_pcie_assert_reset(struct pcie_port *pp)
685 if (pp->reset_gpio >= 0)
686 devm_gpio_request_one(pp->dev, pp->reset_gpio,
687 GPIOF_OUT_INIT_HIGH, "RESET");
691 static int exynos_pcie_establish_link(struct pcie_port *pp)
695 void __iomem *elbi_base = pp->elbi_base;
696 void __iomem *purple_base = pp->purple_base;
697 void __iomem *phy_base = pp->phy_base;
699 if (exynos_pcie_link_up(pp)) {
700 dev_err(pp->dev, "Link already up\n");
704 /* assert reset signals */
705 exynos_pcie_assert_core_reset(pp);
706 exynos_pcie_assert_phy_reset(pp);
708 /* de-assert phy reset */
709 exynos_pcie_deassert_phy_reset(pp);
712 exynos_pcie_init_phy(pp);
714 /* pulse for common reset */
715 writel(1, purple_base + PCIE_PHY_COMMON_RESET);
717 writel(0, purple_base + PCIE_PHY_COMMON_RESET);
719 /* de-assert core reset */
720 exynos_pcie_deassert_core_reset(pp);
722 /* setup root complex */
723 exynos_pcie_setup_rc(pp);
725 /* assert reset signal */
726 exynos_pcie_assert_reset(pp);
728 /* assert LTSSM enable */
729 writel(PCIE_ELBI_LTSSM_ENABLE, elbi_base + PCIE_APP_LTSSM_ENABLE);
731 /* check if the link is up or not */
732 while (!exynos_pcie_link_up(pp)) {
736 while (readl(phy_base + PCIE_PHY_PLL_LOCKED) == 0) {
737 val = readl(purple_base + PCIE_PHY_PLL_LOCKED);
738 dev_info(pp->dev, "PLL Locked: 0x%x\n", val);
740 dev_err(pp->dev, "PCIe Link Fail\n");
745 dev_info(pp->dev, "Link up\n");
750 static void exynos_pcie_clear_irq_pulse(struct pcie_port *pp)
753 void __iomem *elbi_base = pp->elbi_base;
755 val = readl(elbi_base + PCIE_IRQ_PULSE);
756 writel(val, elbi_base + PCIE_IRQ_PULSE);
760 static void exynos_pcie_enable_irq_pulse(struct pcie_port *pp)
763 void __iomem *elbi_base = pp->elbi_base;
765 /* enable INTX interrupt */
766 val = IRQ_INTA_ASSERT | IRQ_INTB_ASSERT |
767 IRQ_INTC_ASSERT | IRQ_INTD_ASSERT,
768 writel(val, elbi_base + PCIE_IRQ_EN_PULSE);
772 static irqreturn_t exynos_pcie_irq_handler(int irq, void *arg)
774 struct pcie_port *pp = arg;
776 exynos_pcie_clear_irq_pulse(pp);
780 static void exynos_pcie_enable_interrupts(struct pcie_port *pp)
782 exynos_pcie_enable_irq_pulse(pp);
786 static void exynos_pcie_host_init(struct pcie_port *pp)
788 struct pcie_port_info *config = &pp->config;
791 /* Keep first 64K for IO */
792 pp->cfg0_base = pp->cfg.start;
793 pp->cfg1_base = pp->cfg.start + config->cfg0_size;
794 pp->io_base = pp->io.start;
795 pp->mem_base = pp->mem.start;
798 exynos_pcie_establish_link(pp);
800 exynos_pcie_wr_own_conf(pp, PCI_BASE_ADDRESS_0, 4, 0);
802 /* program correct class for RC */
803 exynos_pcie_wr_own_conf(pp, PCI_CLASS_DEVICE, 2, PCI_CLASS_BRIDGE_PCI);
805 exynos_pcie_rd_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, &val);
806 val |= PORT_LOGIC_SPEED_CHANGE;
807 exynos_pcie_wr_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, val);
809 exynos_pcie_enable_interrupts(pp);
812 static int add_pcie_port(struct pcie_port *pp, struct platform_device *pdev)
814 struct resource *elbi_base;
815 struct resource *phy_base;
816 struct resource *purple_base;
819 elbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
821 dev_err(&pdev->dev, "couldn't get elbi base resource\n");
824 pp->elbi_base = devm_ioremap_resource(&pdev->dev, elbi_base);
825 if (IS_ERR(pp->elbi_base))
826 return PTR_ERR(pp->elbi_base);
828 phy_base = platform_get_resource(pdev, IORESOURCE_MEM, 1);
830 dev_err(&pdev->dev, "couldn't get phy base resource\n");
833 pp->phy_base = devm_ioremap_resource(&pdev->dev, phy_base);
834 if (IS_ERR(pp->phy_base))
835 return PTR_ERR(pp->phy_base);
837 purple_base = platform_get_resource(pdev, IORESOURCE_MEM, 2);
839 dev_err(&pdev->dev, "couldn't get purple base resource\n");
842 pp->purple_base = devm_ioremap_resource(&pdev->dev, purple_base);
843 if (IS_ERR(pp->purple_base))
844 return PTR_ERR(pp->purple_base);
846 pp->irq = platform_get_irq(pdev, 1);
848 dev_err(&pdev->dev, "failed to get irq\n");
851 ret = devm_request_irq(&pdev->dev, pp->irq, exynos_pcie_irq_handler,
852 IRQF_SHARED, "exynos-pcie", pp);
854 dev_err(&pdev->dev, "failed to request irq\n");
858 pp->dbi_base = devm_ioremap(&pdev->dev, pp->cfg.start,
859 resource_size(&pp->cfg));
861 dev_err(&pdev->dev, "error with ioremap\n");
865 pp->root_bus_nr = -1;
867 spin_lock_init(&pp->conf_lock);
868 exynos_pcie_host_init(pp);
869 pp->va_cfg0_base = devm_ioremap(&pdev->dev, pp->cfg0_base,
870 pp->config.cfg0_size);
871 if (!pp->va_cfg0_base) {
872 dev_err(pp->dev, "error with ioremap in function\n");
875 pp->va_cfg1_base = devm_ioremap(&pdev->dev, pp->cfg1_base,
876 pp->config.cfg1_size);
877 if (!pp->va_cfg1_base) {
878 dev_err(pp->dev, "error with ioremap\n");
885 static int __init exynos_pcie_probe(struct platform_device *pdev)
887 struct pcie_port *pp;
888 struct device_node *np = pdev->dev.of_node;
889 struct of_pci_range range;
890 struct of_pci_range_parser parser;
893 pp = devm_kzalloc(&pdev->dev, sizeof(*pp), GFP_KERNEL);
895 dev_err(&pdev->dev, "no memory for pcie port\n");
899 pp->dev = &pdev->dev;
901 if (of_pci_range_parser_init(&parser, np)) {
902 dev_err(&pdev->dev, "missing ranges property\n");
906 /* Get the I/O and memory ranges from DT */
907 for_each_of_pci_range(&parser, &range) {
908 unsigned long restype = range.flags & IORESOURCE_TYPE_BITS;
909 if (restype == IORESOURCE_IO) {
910 of_pci_range_to_resource(&range, np, &pp->io);
912 pp->io.start = max_t(resource_size_t,
914 range.pci_addr + global_io_offset);
915 pp->io.end = min_t(resource_size_t,
917 range.pci_addr + range.size
919 pp->config.io_size = resource_size(&pp->io);
920 pp->config.io_bus_addr = range.pci_addr;
922 if (restype == IORESOURCE_MEM) {
923 of_pci_range_to_resource(&range, np, &pp->mem);
924 pp->mem.name = "MEM";
925 pp->config.mem_size = resource_size(&pp->mem);
926 pp->config.mem_bus_addr = range.pci_addr;
929 of_pci_range_to_resource(&range, np, &pp->cfg);
930 pp->config.cfg0_size = resource_size(&pp->cfg)/2;
931 pp->config.cfg1_size = resource_size(&pp->cfg)/2;
935 pp->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
937 pp->clk = devm_clk_get(&pdev->dev, "pcie");
938 if (IS_ERR(pp->clk)) {
939 dev_err(&pdev->dev, "Failed to get pcie rc clock\n");
940 return PTR_ERR(pp->clk);
942 ret = clk_prepare_enable(pp->clk);
946 pp->bus_clk = devm_clk_get(&pdev->dev, "pcie_bus");
947 if (IS_ERR(pp->bus_clk)) {
948 dev_err(&pdev->dev, "Failed to get pcie bus clock\n");
949 ret = PTR_ERR(pp->bus_clk);
952 ret = clk_prepare_enable(pp->bus_clk);
956 ret = add_pcie_port(pp, pdev);
960 pp->controller = exynos_pci.nr_controllers;
961 exynos_pci.nr_controllers = 1;
962 exynos_pci.private_data = (void **)&pp;
964 pci_common_init(&exynos_pci);
965 pci_assign_unassigned_resources();
966 #ifdef CONFIG_PCI_DOMAINS
970 platform_set_drvdata(pdev, pp);
974 clk_disable_unprepare(pp->bus_clk);
976 clk_disable_unprepare(pp->clk);
980 static int __exit exynos_pcie_remove(struct platform_device *pdev)
982 struct pcie_port *pp = platform_get_drvdata(pdev);
984 clk_disable_unprepare(pp->bus_clk);
985 clk_disable_unprepare(pp->clk);
990 static const struct of_device_id exynos_pcie_of_match[] = {
991 { .compatible = "samsung,exynos5440-pcie", },
994 MODULE_DEVICE_TABLE(of, exynos_pcie_of_match);
996 static struct platform_driver exynos_pcie_driver = {
997 .remove = __exit_p(exynos_pcie_remove),
999 .name = "exynos-pcie",
1000 .owner = THIS_MODULE,
1001 .of_match_table = of_match_ptr(exynos_pcie_of_match),
1005 static int exynos_pcie_abort(unsigned long addr, unsigned int fsr,
1006 struct pt_regs *regs)
1008 unsigned long pc = instruction_pointer(regs);
1009 unsigned long instr = *(unsigned long *)pc;
1011 WARN_ONCE(1, "pcie abort\n");
1014 * If the instruction being executed was a read,
1015 * make it look like it read all-ones.
1017 if ((instr & 0x0c100000) == 0x04100000) {
1018 int reg = (instr >> 12) & 15;
1021 if (instr & 0x00400000)
1026 regs->uregs[reg] = val;
1031 if ((instr & 0x0e100090) == 0x00100090) {
1032 int reg = (instr >> 12) & 15;
1034 regs->uregs[reg] = -1;
1042 /* Exynos PCIe driver does not allow module unload */
1044 static int __init pcie_init(void)
1046 hook_fault_code(16 + 6, exynos_pcie_abort, SIGBUS, 0,
1047 "imprecise external abort");
1049 platform_driver_probe(&exynos_pcie_driver, exynos_pcie_probe);
1053 subsys_initcall(pcie_init);
1055 MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
1056 MODULE_DESCRIPTION("Samsung PCIe host controller driver");
1057 MODULE_LICENSE("GPL v2");