Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 1 | /* |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 2 | * Synopsys Designware PCIe host controller driver |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2013 Samsung Electronics Co., Ltd. |
| 5 | * http://www.samsung.com |
| 6 | * |
| 7 | * Author: Jingoo Han <jg1.han@samsung.com> |
| 8 | * |
| 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. |
| 12 | */ |
| 13 | |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 14 | #include <linux/irq.h> |
| 15 | #include <linux/irqdomain.h> |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 16 | #include <linux/kernel.h> |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 17 | #include <linux/module.h> |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 18 | #include <linux/msi.h> |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 19 | #include <linux/of_address.h> |
Lucas Stach | f86b3e3 | 2014-04-16 10:23:46 -0600 | [diff] [blame] | 20 | #include <linux/of_pci.h> |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 21 | #include <linux/pci.h> |
| 22 | #include <linux/pci_regs.h> |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 23 | #include <linux/types.h> |
| 24 | |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 25 | #include "pcie-designware.h" |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 26 | |
| 27 | /* Synopsis specific PCIE configuration registers */ |
| 28 | #define PCIE_PORT_LINK_CONTROL 0x710 |
| 29 | #define PORT_LINK_MODE_MASK (0x3f << 16) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 30 | #define PORT_LINK_MODE_1_LANES (0x1 << 16) |
| 31 | #define PORT_LINK_MODE_2_LANES (0x3 << 16) |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 32 | #define PORT_LINK_MODE_4_LANES (0x7 << 16) |
| 33 | |
| 34 | #define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C |
| 35 | #define PORT_LOGIC_SPEED_CHANGE (0x1 << 17) |
| 36 | #define PORT_LOGIC_LINK_WIDTH_MASK (0x1ff << 8) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 37 | #define PORT_LOGIC_LINK_WIDTH_1_LANES (0x1 << 8) |
| 38 | #define PORT_LOGIC_LINK_WIDTH_2_LANES (0x2 << 8) |
| 39 | #define PORT_LOGIC_LINK_WIDTH_4_LANES (0x4 << 8) |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 40 | |
| 41 | #define PCIE_MSI_ADDR_LO 0x820 |
| 42 | #define PCIE_MSI_ADDR_HI 0x824 |
| 43 | #define PCIE_MSI_INTR0_ENABLE 0x828 |
| 44 | #define PCIE_MSI_INTR0_MASK 0x82C |
| 45 | #define PCIE_MSI_INTR0_STATUS 0x830 |
| 46 | |
| 47 | #define PCIE_ATU_VIEWPORT 0x900 |
| 48 | #define PCIE_ATU_REGION_INBOUND (0x1 << 31) |
| 49 | #define PCIE_ATU_REGION_OUTBOUND (0x0 << 31) |
| 50 | #define PCIE_ATU_REGION_INDEX1 (0x1 << 0) |
| 51 | #define PCIE_ATU_REGION_INDEX0 (0x0 << 0) |
| 52 | #define PCIE_ATU_CR1 0x904 |
| 53 | #define PCIE_ATU_TYPE_MEM (0x0 << 0) |
| 54 | #define PCIE_ATU_TYPE_IO (0x2 << 0) |
| 55 | #define PCIE_ATU_TYPE_CFG0 (0x4 << 0) |
| 56 | #define PCIE_ATU_TYPE_CFG1 (0x5 << 0) |
| 57 | #define PCIE_ATU_CR2 0x908 |
| 58 | #define PCIE_ATU_ENABLE (0x1 << 31) |
| 59 | #define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30) |
| 60 | #define PCIE_ATU_LOWER_BASE 0x90C |
| 61 | #define PCIE_ATU_UPPER_BASE 0x910 |
| 62 | #define PCIE_ATU_LIMIT 0x914 |
| 63 | #define PCIE_ATU_LOWER_TARGET 0x918 |
| 64 | #define PCIE_ATU_BUS(x) (((x) & 0xff) << 24) |
| 65 | #define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19) |
| 66 | #define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16) |
| 67 | #define PCIE_ATU_UPPER_TARGET 0x91C |
| 68 | |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 69 | static struct hw_pci dw_pci; |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 70 | |
Bjorn Helgaas | 73e4085 | 2013-10-09 09:12:37 -0600 | [diff] [blame] | 71 | static unsigned long global_io_offset; |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 72 | |
| 73 | static inline struct pcie_port *sys_to_pcie(struct pci_sys_data *sys) |
| 74 | { |
| 75 | return sys->private_data; |
| 76 | } |
| 77 | |
Pratyush Anand | a01ef59 | 2013-12-11 15:08:32 +0530 | [diff] [blame] | 78 | int dw_pcie_cfg_read(void __iomem *addr, int where, int size, u32 *val) |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 79 | { |
| 80 | *val = readl(addr); |
| 81 | |
| 82 | if (size == 1) |
| 83 | *val = (*val >> (8 * (where & 3))) & 0xff; |
| 84 | else if (size == 2) |
| 85 | *val = (*val >> (8 * (where & 3))) & 0xffff; |
| 86 | else if (size != 4) |
| 87 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 88 | |
| 89 | return PCIBIOS_SUCCESSFUL; |
| 90 | } |
| 91 | |
Pratyush Anand | a01ef59 | 2013-12-11 15:08:32 +0530 | [diff] [blame] | 92 | int dw_pcie_cfg_write(void __iomem *addr, int where, int size, u32 val) |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 93 | { |
| 94 | if (size == 4) |
| 95 | writel(val, addr); |
| 96 | else if (size == 2) |
| 97 | writew(val, addr + (where & 2)); |
| 98 | else if (size == 1) |
| 99 | writeb(val, addr + (where & 3)); |
| 100 | else |
| 101 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 102 | |
| 103 | return PCIBIOS_SUCCESSFUL; |
| 104 | } |
| 105 | |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 106 | static inline void dw_pcie_readl_rc(struct pcie_port *pp, u32 reg, u32 *val) |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 107 | { |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 108 | if (pp->ops->readl_rc) |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 109 | pp->ops->readl_rc(pp, pp->dbi_base + reg, val); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 110 | else |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 111 | *val = readl(pp->dbi_base + reg); |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 112 | } |
| 113 | |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 114 | static inline void dw_pcie_writel_rc(struct pcie_port *pp, u32 val, u32 reg) |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 115 | { |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 116 | if (pp->ops->writel_rc) |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 117 | pp->ops->writel_rc(pp, val, pp->dbi_base + reg); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 118 | else |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 119 | writel(val, pp->dbi_base + reg); |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 120 | } |
| 121 | |
Bjorn Helgaas | 73e4085 | 2013-10-09 09:12:37 -0600 | [diff] [blame] | 122 | static int dw_pcie_rd_own_conf(struct pcie_port *pp, int where, int size, |
| 123 | u32 *val) |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 124 | { |
| 125 | int ret; |
| 126 | |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 127 | if (pp->ops->rd_own_conf) |
| 128 | ret = pp->ops->rd_own_conf(pp, where, size, val); |
| 129 | else |
Pratyush Anand | a01ef59 | 2013-12-11 15:08:32 +0530 | [diff] [blame] | 130 | ret = dw_pcie_cfg_read(pp->dbi_base + (where & ~0x3), where, |
| 131 | size, val); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 132 | |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 133 | return ret; |
| 134 | } |
| 135 | |
Bjorn Helgaas | 73e4085 | 2013-10-09 09:12:37 -0600 | [diff] [blame] | 136 | static int dw_pcie_wr_own_conf(struct pcie_port *pp, int where, int size, |
| 137 | u32 val) |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 138 | { |
| 139 | int ret; |
| 140 | |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 141 | if (pp->ops->wr_own_conf) |
| 142 | ret = pp->ops->wr_own_conf(pp, where, size, val); |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 143 | else |
Pratyush Anand | a01ef59 | 2013-12-11 15:08:32 +0530 | [diff] [blame] | 144 | ret = dw_pcie_cfg_write(pp->dbi_base + (where & ~0x3), where, |
| 145 | size, val); |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 146 | |
| 147 | return ret; |
| 148 | } |
| 149 | |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 150 | static struct irq_chip dw_msi_irq_chip = { |
| 151 | .name = "PCI-MSI", |
| 152 | .irq_enable = unmask_msi_irq, |
| 153 | .irq_disable = mask_msi_irq, |
| 154 | .irq_mask = mask_msi_irq, |
| 155 | .irq_unmask = unmask_msi_irq, |
| 156 | }; |
| 157 | |
| 158 | /* MSI int handler */ |
| 159 | void dw_handle_msi_irq(struct pcie_port *pp) |
| 160 | { |
| 161 | unsigned long val; |
Pratyush Anand | 904d0e7 | 2013-10-09 21:32:12 +0900 | [diff] [blame] | 162 | int i, pos, irq; |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 163 | |
| 164 | for (i = 0; i < MAX_MSI_CTRLS; i++) { |
| 165 | dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_STATUS + i * 12, 4, |
| 166 | (u32 *)&val); |
| 167 | if (val) { |
| 168 | pos = 0; |
| 169 | while ((pos = find_next_bit(&val, 32, pos)) != 32) { |
Pratyush Anand | 904d0e7 | 2013-10-09 21:32:12 +0900 | [diff] [blame] | 170 | irq = irq_find_mapping(pp->irq_domain, |
| 171 | i * 32 + pos); |
Harro Haan | ca16589 | 2013-12-12 19:29:03 +0100 | [diff] [blame] | 172 | dw_pcie_wr_own_conf(pp, |
| 173 | PCIE_MSI_INTR0_STATUS + i * 12, |
| 174 | 4, 1 << pos); |
Pratyush Anand | 904d0e7 | 2013-10-09 21:32:12 +0900 | [diff] [blame] | 175 | generic_handle_irq(irq); |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 176 | pos++; |
| 177 | } |
| 178 | } |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| 182 | void dw_pcie_msi_init(struct pcie_port *pp) |
| 183 | { |
| 184 | pp->msi_data = __get_free_pages(GFP_KERNEL, 0); |
| 185 | |
| 186 | /* program the msi_data */ |
| 187 | dw_pcie_wr_own_conf(pp, PCIE_MSI_ADDR_LO, 4, |
| 188 | virt_to_phys((void *)pp->msi_data)); |
| 189 | dw_pcie_wr_own_conf(pp, PCIE_MSI_ADDR_HI, 4, 0); |
| 190 | } |
| 191 | |
| 192 | static int find_valid_pos0(struct pcie_port *pp, int msgvec, int pos, int *pos0) |
| 193 | { |
| 194 | int flag = 1; |
| 195 | |
| 196 | do { |
| 197 | pos = find_next_zero_bit(pp->msi_irq_in_use, |
| 198 | MAX_MSI_IRQS, pos); |
| 199 | /*if you have reached to the end then get out from here.*/ |
| 200 | if (pos == MAX_MSI_IRQS) |
| 201 | return -ENOSPC; |
| 202 | /* |
| 203 | * Check if this position is at correct offset.nvec is always a |
Bjorn Helgaas | f762598 | 2013-11-14 11:28:18 -0700 | [diff] [blame] | 204 | * power of two. pos0 must be nvec bit aligned. |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 205 | */ |
| 206 | if (pos % msgvec) |
| 207 | pos += msgvec - (pos % msgvec); |
| 208 | else |
| 209 | flag = 0; |
| 210 | } while (flag); |
| 211 | |
| 212 | *pos0 = pos; |
| 213 | return 0; |
| 214 | } |
| 215 | |
Bjørn Erik Nilsen | be3f48c | 2013-11-29 14:35:24 +0100 | [diff] [blame] | 216 | static void clear_irq_range(struct pcie_port *pp, unsigned int irq_base, |
Jingoo Han | 58275f2f | 2013-12-27 09:30:25 +0900 | [diff] [blame] | 217 | unsigned int nvec, unsigned int pos) |
Bjørn Erik Nilsen | be3f48c | 2013-11-29 14:35:24 +0100 | [diff] [blame] | 218 | { |
| 219 | unsigned int i, res, bit, val; |
| 220 | |
Bjorn Helgaas | 0b8cfb6 | 2013-12-09 15:11:25 -0700 | [diff] [blame] | 221 | for (i = 0; i < nvec; i++) { |
Bjørn Erik Nilsen | be3f48c | 2013-11-29 14:35:24 +0100 | [diff] [blame] | 222 | irq_set_msi_desc_off(irq_base, i, NULL); |
| 223 | clear_bit(pos + i, pp->msi_irq_in_use); |
Jingoo Han | 58275f2f | 2013-12-27 09:30:25 +0900 | [diff] [blame] | 224 | /* Disable corresponding interrupt on MSI controller */ |
Bjørn Erik Nilsen | be3f48c | 2013-11-29 14:35:24 +0100 | [diff] [blame] | 225 | res = ((pos + i) / 32) * 12; |
| 226 | bit = (pos + i) % 32; |
| 227 | dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, &val); |
| 228 | val &= ~(1 << bit); |
| 229 | dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, val); |
Bjørn Erik Nilsen | be3f48c | 2013-11-29 14:35:24 +0100 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 233 | static int assign_irq(int no_irqs, struct msi_desc *desc, int *pos) |
| 234 | { |
| 235 | int res, bit, irq, pos0, pos1, i; |
| 236 | u32 val; |
| 237 | struct pcie_port *pp = sys_to_pcie(desc->dev->bus->sysdata); |
| 238 | |
| 239 | if (!pp) { |
| 240 | BUG(); |
| 241 | return -EINVAL; |
| 242 | } |
| 243 | |
| 244 | pos0 = find_first_zero_bit(pp->msi_irq_in_use, |
| 245 | MAX_MSI_IRQS); |
| 246 | if (pos0 % no_irqs) { |
| 247 | if (find_valid_pos0(pp, no_irqs, pos0, &pos0)) |
| 248 | goto no_valid_irq; |
| 249 | } |
| 250 | if (no_irqs > 1) { |
| 251 | pos1 = find_next_bit(pp->msi_irq_in_use, |
| 252 | MAX_MSI_IRQS, pos0); |
| 253 | /* there must be nvec number of consecutive free bits */ |
| 254 | while ((pos1 - pos0) < no_irqs) { |
| 255 | if (find_valid_pos0(pp, no_irqs, pos1, &pos0)) |
| 256 | goto no_valid_irq; |
| 257 | pos1 = find_next_bit(pp->msi_irq_in_use, |
| 258 | MAX_MSI_IRQS, pos0); |
| 259 | } |
| 260 | } |
| 261 | |
Pratyush Anand | 904d0e7 | 2013-10-09 21:32:12 +0900 | [diff] [blame] | 262 | irq = irq_find_mapping(pp->irq_domain, pos0); |
| 263 | if (!irq) |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 264 | goto no_valid_irq; |
| 265 | |
Bjørn Erik Nilsen | be3f48c | 2013-11-29 14:35:24 +0100 | [diff] [blame] | 266 | /* |
| 267 | * irq_create_mapping (called from dw_pcie_host_init) pre-allocates |
| 268 | * descs so there is no need to allocate descs here. We can therefore |
| 269 | * assume that if irq_find_mapping above returns non-zero, then the |
| 270 | * descs are also successfully allocated. |
| 271 | */ |
| 272 | |
Bjorn Helgaas | 0b8cfb6 | 2013-12-09 15:11:25 -0700 | [diff] [blame] | 273 | for (i = 0; i < no_irqs; i++) { |
Bjørn Erik Nilsen | be3f48c | 2013-11-29 14:35:24 +0100 | [diff] [blame] | 274 | if (irq_set_msi_desc_off(irq, i, desc) != 0) { |
| 275 | clear_irq_range(pp, irq, i, pos0); |
| 276 | goto no_valid_irq; |
| 277 | } |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 278 | set_bit(pos0 + i, pp->msi_irq_in_use); |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 279 | /*Enable corresponding interrupt in MSI interrupt controller */ |
| 280 | res = ((pos0 + i) / 32) * 12; |
| 281 | bit = (pos0 + i) % 32; |
| 282 | dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, &val); |
| 283 | val |= 1 << bit; |
| 284 | dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, val); |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | *pos = pos0; |
| 288 | return irq; |
| 289 | |
| 290 | no_valid_irq: |
| 291 | *pos = pos0; |
| 292 | return -ENOSPC; |
| 293 | } |
| 294 | |
| 295 | static void clear_irq(unsigned int irq) |
| 296 | { |
Bjørn Erik Nilsen | be3f48c | 2013-11-29 14:35:24 +0100 | [diff] [blame] | 297 | unsigned int pos, nvec; |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 298 | struct msi_desc *msi; |
| 299 | struct pcie_port *pp; |
Pratyush Anand | 904d0e7 | 2013-10-09 21:32:12 +0900 | [diff] [blame] | 300 | struct irq_data *data = irq_get_irq_data(irq); |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 301 | |
| 302 | /* get the port structure */ |
Thomas Gleixner | f7bfca6 | 2014-02-23 21:40:11 +0000 | [diff] [blame] | 303 | msi = irq_data_get_msi(data); |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 304 | pp = sys_to_pcie(msi->dev->bus->sysdata); |
| 305 | if (!pp) { |
| 306 | BUG(); |
| 307 | return; |
| 308 | } |
| 309 | |
Bjørn Erik Nilsen | be3f48c | 2013-11-29 14:35:24 +0100 | [diff] [blame] | 310 | /* undo what was done in assign_irq */ |
Pratyush Anand | 904d0e7 | 2013-10-09 21:32:12 +0900 | [diff] [blame] | 311 | pos = data->hwirq; |
Bjørn Erik Nilsen | be3f48c | 2013-11-29 14:35:24 +0100 | [diff] [blame] | 312 | nvec = 1 << msi->msi_attrib.multiple; |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 313 | |
Bjørn Erik Nilsen | be3f48c | 2013-11-29 14:35:24 +0100 | [diff] [blame] | 314 | clear_irq_range(pp, irq, nvec, pos); |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 315 | |
Bjørn Erik Nilsen | be3f48c | 2013-11-29 14:35:24 +0100 | [diff] [blame] | 316 | /* all irqs cleared; reset attributes */ |
| 317 | msi->irq = 0; |
| 318 | msi->msi_attrib.multiple = 0; |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | static int dw_msi_setup_irq(struct msi_chip *chip, struct pci_dev *pdev, |
| 322 | struct msi_desc *desc) |
| 323 | { |
| 324 | int irq, pos, msgvec; |
| 325 | u16 msg_ctr; |
| 326 | struct msi_msg msg; |
| 327 | struct pcie_port *pp = sys_to_pcie(pdev->bus->sysdata); |
| 328 | |
| 329 | if (!pp) { |
| 330 | BUG(); |
| 331 | return -EINVAL; |
| 332 | } |
| 333 | |
| 334 | pci_read_config_word(pdev, desc->msi_attrib.pos+PCI_MSI_FLAGS, |
| 335 | &msg_ctr); |
| 336 | msgvec = (msg_ctr&PCI_MSI_FLAGS_QSIZE) >> 4; |
| 337 | if (msgvec == 0) |
| 338 | msgvec = (msg_ctr & PCI_MSI_FLAGS_QMASK) >> 1; |
| 339 | if (msgvec > 5) |
| 340 | msgvec = 0; |
| 341 | |
| 342 | irq = assign_irq((1 << msgvec), desc, &pos); |
| 343 | if (irq < 0) |
| 344 | return irq; |
| 345 | |
Bjørn Erik Nilsen | 64989e7 | 2013-11-29 14:35:25 +0100 | [diff] [blame] | 346 | /* |
| 347 | * write_msi_msg() will update PCI_MSI_FLAGS so there is |
| 348 | * no need to explicitly call pci_write_config_word(). |
| 349 | */ |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 350 | desc->msi_attrib.multiple = msgvec; |
| 351 | |
| 352 | msg.address_lo = virt_to_phys((void *)pp->msi_data); |
| 353 | msg.address_hi = 0x0; |
| 354 | msg.data = pos; |
| 355 | write_msi_msg(irq, &msg); |
| 356 | |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | static void dw_msi_teardown_irq(struct msi_chip *chip, unsigned int irq) |
| 361 | { |
| 362 | clear_irq(irq); |
| 363 | } |
| 364 | |
| 365 | static struct msi_chip dw_pcie_msi_chip = { |
| 366 | .setup_irq = dw_msi_setup_irq, |
| 367 | .teardown_irq = dw_msi_teardown_irq, |
| 368 | }; |
| 369 | |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 370 | int dw_pcie_link_up(struct pcie_port *pp) |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 371 | { |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 372 | if (pp->ops->link_up) |
| 373 | return pp->ops->link_up(pp); |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 374 | else |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 375 | return 0; |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 376 | } |
| 377 | |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 378 | static int dw_pcie_msi_map(struct irq_domain *domain, unsigned int irq, |
| 379 | irq_hw_number_t hwirq) |
| 380 | { |
| 381 | irq_set_chip_and_handler(irq, &dw_msi_irq_chip, handle_simple_irq); |
| 382 | irq_set_chip_data(irq, domain->host_data); |
| 383 | set_irq_flags(irq, IRQF_VALID); |
| 384 | |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | static const struct irq_domain_ops msi_domain_ops = { |
| 389 | .map = dw_pcie_msi_map, |
| 390 | }; |
| 391 | |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 392 | int __init dw_pcie_host_init(struct pcie_port *pp) |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 393 | { |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 394 | struct device_node *np = pp->dev->of_node; |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 395 | struct of_pci_range range; |
| 396 | struct of_pci_range_parser parser; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 397 | u32 val; |
Pratyush Anand | 904d0e7 | 2013-10-09 21:32:12 +0900 | [diff] [blame] | 398 | int i; |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 399 | |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 400 | if (of_pci_range_parser_init(&parser, np)) { |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 401 | dev_err(pp->dev, "missing ranges property\n"); |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 402 | return -EINVAL; |
| 403 | } |
| 404 | |
| 405 | /* Get the I/O and memory ranges from DT */ |
| 406 | for_each_of_pci_range(&parser, &range) { |
| 407 | unsigned long restype = range.flags & IORESOURCE_TYPE_BITS; |
| 408 | if (restype == IORESOURCE_IO) { |
| 409 | of_pci_range_to_resource(&range, np, &pp->io); |
| 410 | pp->io.name = "I/O"; |
| 411 | pp->io.start = max_t(resource_size_t, |
| 412 | PCIBIOS_MIN_IO, |
| 413 | range.pci_addr + global_io_offset); |
| 414 | pp->io.end = min_t(resource_size_t, |
| 415 | IO_SPACE_LIMIT, |
| 416 | range.pci_addr + range.size |
| 417 | + global_io_offset); |
| 418 | pp->config.io_size = resource_size(&pp->io); |
| 419 | pp->config.io_bus_addr = range.pci_addr; |
Pratyush Anand | fce8591 | 2013-12-11 15:08:33 +0530 | [diff] [blame] | 420 | pp->io_base = range.cpu_addr; |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 421 | } |
| 422 | if (restype == IORESOURCE_MEM) { |
| 423 | of_pci_range_to_resource(&range, np, &pp->mem); |
| 424 | pp->mem.name = "MEM"; |
| 425 | pp->config.mem_size = resource_size(&pp->mem); |
| 426 | pp->config.mem_bus_addr = range.pci_addr; |
| 427 | } |
| 428 | if (restype == 0) { |
| 429 | of_pci_range_to_resource(&range, np, &pp->cfg); |
| 430 | pp->config.cfg0_size = resource_size(&pp->cfg)/2; |
| 431 | pp->config.cfg1_size = resource_size(&pp->cfg)/2; |
| 432 | } |
| 433 | } |
| 434 | |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 435 | if (!pp->dbi_base) { |
| 436 | pp->dbi_base = devm_ioremap(pp->dev, pp->cfg.start, |
| 437 | resource_size(&pp->cfg)); |
| 438 | if (!pp->dbi_base) { |
| 439 | dev_err(pp->dev, "error with ioremap\n"); |
| 440 | return -ENOMEM; |
| 441 | } |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 442 | } |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 443 | |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 444 | pp->cfg0_base = pp->cfg.start; |
| 445 | pp->cfg1_base = pp->cfg.start + pp->config.cfg0_size; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 446 | pp->mem_base = pp->mem.start; |
| 447 | |
| 448 | pp->va_cfg0_base = devm_ioremap(pp->dev, pp->cfg0_base, |
| 449 | pp->config.cfg0_size); |
| 450 | if (!pp->va_cfg0_base) { |
| 451 | dev_err(pp->dev, "error with ioremap in function\n"); |
| 452 | return -ENOMEM; |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 453 | } |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 454 | pp->va_cfg1_base = devm_ioremap(pp->dev, pp->cfg1_base, |
| 455 | pp->config.cfg1_size); |
| 456 | if (!pp->va_cfg1_base) { |
| 457 | dev_err(pp->dev, "error with ioremap\n"); |
| 458 | return -ENOMEM; |
| 459 | } |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 460 | |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 461 | if (of_property_read_u32(np, "num-lanes", &pp->lanes)) { |
| 462 | dev_err(pp->dev, "Failed to parse the number of lanes\n"); |
| 463 | return -EINVAL; |
| 464 | } |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 465 | |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 466 | if (IS_ENABLED(CONFIG_PCI_MSI)) { |
Pratyush Anand | 904d0e7 | 2013-10-09 21:32:12 +0900 | [diff] [blame] | 467 | pp->irq_domain = irq_domain_add_linear(pp->dev->of_node, |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 468 | MAX_MSI_IRQS, &msi_domain_ops, |
| 469 | &dw_pcie_msi_chip); |
Pratyush Anand | 904d0e7 | 2013-10-09 21:32:12 +0900 | [diff] [blame] | 470 | if (!pp->irq_domain) { |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 471 | dev_err(pp->dev, "irq domain init failed\n"); |
| 472 | return -ENXIO; |
| 473 | } |
| 474 | |
Pratyush Anand | 904d0e7 | 2013-10-09 21:32:12 +0900 | [diff] [blame] | 475 | for (i = 0; i < MAX_MSI_IRQS; i++) |
| 476 | irq_create_mapping(pp->irq_domain, i); |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 477 | } |
| 478 | |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 479 | if (pp->ops->host_init) |
| 480 | pp->ops->host_init(pp); |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 481 | |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 482 | dw_pcie_wr_own_conf(pp, PCI_BASE_ADDRESS_0, 4, 0); |
| 483 | |
| 484 | /* program correct class for RC */ |
| 485 | dw_pcie_wr_own_conf(pp, PCI_CLASS_DEVICE, 2, PCI_CLASS_BRIDGE_PCI); |
| 486 | |
| 487 | dw_pcie_rd_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, &val); |
| 488 | val |= PORT_LOGIC_SPEED_CHANGE; |
| 489 | dw_pcie_wr_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, val); |
| 490 | |
| 491 | dw_pci.nr_controllers = 1; |
| 492 | dw_pci.private_data = (void **)&pp; |
| 493 | |
Lucas Stach | f86b3e3 | 2014-04-16 10:23:46 -0600 | [diff] [blame] | 494 | pci_common_init_dev(pp->dev, &dw_pci); |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 495 | pci_assign_unassigned_resources(); |
| 496 | #ifdef CONFIG_PCI_DOMAINS |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 497 | dw_pci.domain++; |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 498 | #endif |
| 499 | |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 500 | return 0; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 501 | } |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 502 | |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 503 | static void dw_pcie_prog_viewport_cfg0(struct pcie_port *pp, u32 busdev) |
| 504 | { |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 505 | /* Program viewport 0 : OUTBOUND : CFG0 */ |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 506 | dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0, |
| 507 | PCIE_ATU_VIEWPORT); |
| 508 | dw_pcie_writel_rc(pp, pp->cfg0_base, PCIE_ATU_LOWER_BASE); |
| 509 | dw_pcie_writel_rc(pp, (pp->cfg0_base >> 32), PCIE_ATU_UPPER_BASE); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 510 | dw_pcie_writel_rc(pp, pp->cfg0_base + pp->config.cfg0_size - 1, |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 511 | PCIE_ATU_LIMIT); |
| 512 | dw_pcie_writel_rc(pp, busdev, PCIE_ATU_LOWER_TARGET); |
| 513 | dw_pcie_writel_rc(pp, 0, PCIE_ATU_UPPER_TARGET); |
| 514 | dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_CFG0, PCIE_ATU_CR1); |
| 515 | dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | static void dw_pcie_prog_viewport_cfg1(struct pcie_port *pp, u32 busdev) |
| 519 | { |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 520 | /* Program viewport 1 : OUTBOUND : CFG1 */ |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 521 | dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1, |
| 522 | PCIE_ATU_VIEWPORT); |
| 523 | dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_CFG1, PCIE_ATU_CR1); |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 524 | dw_pcie_writel_rc(pp, pp->cfg1_base, PCIE_ATU_LOWER_BASE); |
| 525 | dw_pcie_writel_rc(pp, (pp->cfg1_base >> 32), PCIE_ATU_UPPER_BASE); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 526 | dw_pcie_writel_rc(pp, pp->cfg1_base + pp->config.cfg1_size - 1, |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 527 | PCIE_ATU_LIMIT); |
| 528 | dw_pcie_writel_rc(pp, busdev, PCIE_ATU_LOWER_TARGET); |
| 529 | dw_pcie_writel_rc(pp, 0, PCIE_ATU_UPPER_TARGET); |
Mohit Kumar | 017fcdc | 2014-04-16 10:23:34 -0600 | [diff] [blame] | 530 | dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | static void dw_pcie_prog_viewport_mem_outbound(struct pcie_port *pp) |
| 534 | { |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 535 | /* Program viewport 0 : OUTBOUND : MEM */ |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 536 | dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0, |
| 537 | PCIE_ATU_VIEWPORT); |
| 538 | dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_MEM, PCIE_ATU_CR1); |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 539 | dw_pcie_writel_rc(pp, pp->mem_base, PCIE_ATU_LOWER_BASE); |
| 540 | dw_pcie_writel_rc(pp, (pp->mem_base >> 32), PCIE_ATU_UPPER_BASE); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 541 | dw_pcie_writel_rc(pp, pp->mem_base + pp->config.mem_size - 1, |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 542 | PCIE_ATU_LIMIT); |
| 543 | dw_pcie_writel_rc(pp, pp->config.mem_bus_addr, PCIE_ATU_LOWER_TARGET); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 544 | dw_pcie_writel_rc(pp, upper_32_bits(pp->config.mem_bus_addr), |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 545 | PCIE_ATU_UPPER_TARGET); |
Mohit Kumar | 017fcdc | 2014-04-16 10:23:34 -0600 | [diff] [blame] | 546 | dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | static void dw_pcie_prog_viewport_io_outbound(struct pcie_port *pp) |
| 550 | { |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 551 | /* Program viewport 1 : OUTBOUND : IO */ |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 552 | dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1, |
| 553 | PCIE_ATU_VIEWPORT); |
| 554 | dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_IO, PCIE_ATU_CR1); |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 555 | dw_pcie_writel_rc(pp, pp->io_base, PCIE_ATU_LOWER_BASE); |
| 556 | dw_pcie_writel_rc(pp, (pp->io_base >> 32), PCIE_ATU_UPPER_BASE); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 557 | dw_pcie_writel_rc(pp, pp->io_base + pp->config.io_size - 1, |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 558 | PCIE_ATU_LIMIT); |
| 559 | dw_pcie_writel_rc(pp, pp->config.io_bus_addr, PCIE_ATU_LOWER_TARGET); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 560 | dw_pcie_writel_rc(pp, upper_32_bits(pp->config.io_bus_addr), |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 561 | PCIE_ATU_UPPER_TARGET); |
Mohit Kumar | 017fcdc | 2014-04-16 10:23:34 -0600 | [diff] [blame] | 562 | dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | static int dw_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus, |
| 566 | u32 devfn, int where, int size, u32 *val) |
| 567 | { |
| 568 | int ret = PCIBIOS_SUCCESSFUL; |
| 569 | u32 address, busdev; |
| 570 | |
| 571 | busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) | |
| 572 | PCIE_ATU_FUNC(PCI_FUNC(devfn)); |
| 573 | address = where & ~0x3; |
| 574 | |
| 575 | if (bus->parent->number == pp->root_bus_nr) { |
| 576 | dw_pcie_prog_viewport_cfg0(pp, busdev); |
Pratyush Anand | a01ef59 | 2013-12-11 15:08:32 +0530 | [diff] [blame] | 577 | ret = dw_pcie_cfg_read(pp->va_cfg0_base + address, where, size, |
| 578 | val); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 579 | dw_pcie_prog_viewport_mem_outbound(pp); |
| 580 | } else { |
| 581 | dw_pcie_prog_viewport_cfg1(pp, busdev); |
Pratyush Anand | a01ef59 | 2013-12-11 15:08:32 +0530 | [diff] [blame] | 582 | ret = dw_pcie_cfg_read(pp->va_cfg1_base + address, where, size, |
| 583 | val); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 584 | dw_pcie_prog_viewport_io_outbound(pp); |
| 585 | } |
| 586 | |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 587 | return ret; |
| 588 | } |
| 589 | |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 590 | static int dw_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus, |
| 591 | u32 devfn, int where, int size, u32 val) |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 592 | { |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 593 | int ret = PCIBIOS_SUCCESSFUL; |
| 594 | u32 address, busdev; |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 595 | |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 596 | busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) | |
| 597 | PCIE_ATU_FUNC(PCI_FUNC(devfn)); |
| 598 | address = where & ~0x3; |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 599 | |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 600 | if (bus->parent->number == pp->root_bus_nr) { |
| 601 | dw_pcie_prog_viewport_cfg0(pp, busdev); |
Pratyush Anand | a01ef59 | 2013-12-11 15:08:32 +0530 | [diff] [blame] | 602 | ret = dw_pcie_cfg_write(pp->va_cfg0_base + address, where, size, |
| 603 | val); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 604 | dw_pcie_prog_viewport_mem_outbound(pp); |
| 605 | } else { |
| 606 | dw_pcie_prog_viewport_cfg1(pp, busdev); |
Pratyush Anand | a01ef59 | 2013-12-11 15:08:32 +0530 | [diff] [blame] | 607 | ret = dw_pcie_cfg_write(pp->va_cfg1_base + address, where, size, |
| 608 | val); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 609 | dw_pcie_prog_viewport_io_outbound(pp); |
| 610 | } |
| 611 | |
| 612 | return ret; |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 613 | } |
| 614 | |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 615 | static int dw_pcie_valid_config(struct pcie_port *pp, |
| 616 | struct pci_bus *bus, int dev) |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 617 | { |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 618 | /* If there is no link, then there is no device */ |
| 619 | if (bus->number != pp->root_bus_nr) { |
| 620 | if (!dw_pcie_link_up(pp)) |
| 621 | return 0; |
| 622 | } |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 623 | |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 624 | /* access only one slot on each root port */ |
| 625 | if (bus->number == pp->root_bus_nr && dev > 0) |
| 626 | return 0; |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 627 | |
| 628 | /* |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 629 | * do not read more than one device on the bus directly attached |
| 630 | * to RC's (Virtual Bridge's) DS side. |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 631 | */ |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 632 | if (bus->primary == pp->root_bus_nr && dev > 0) |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 633 | return 0; |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 634 | |
| 635 | return 1; |
| 636 | } |
| 637 | |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 638 | static int dw_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where, |
| 639 | int size, u32 *val) |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 640 | { |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 641 | struct pcie_port *pp = sys_to_pcie(bus->sysdata); |
| 642 | unsigned long flags; |
| 643 | int ret; |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 644 | |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 645 | if (!pp) { |
| 646 | BUG(); |
| 647 | return -EINVAL; |
| 648 | } |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 649 | |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 650 | if (dw_pcie_valid_config(pp, bus, PCI_SLOT(devfn)) == 0) { |
| 651 | *val = 0xffffffff; |
| 652 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 653 | } |
| 654 | |
| 655 | spin_lock_irqsave(&pp->conf_lock, flags); |
| 656 | if (bus->number != pp->root_bus_nr) |
| 657 | ret = dw_pcie_rd_other_conf(pp, bus, devfn, |
| 658 | where, size, val); |
| 659 | else |
| 660 | ret = dw_pcie_rd_own_conf(pp, where, size, val); |
| 661 | spin_unlock_irqrestore(&pp->conf_lock, flags); |
| 662 | |
| 663 | return ret; |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 664 | } |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 665 | |
| 666 | static int dw_pcie_wr_conf(struct pci_bus *bus, u32 devfn, |
| 667 | int where, int size, u32 val) |
| 668 | { |
| 669 | struct pcie_port *pp = sys_to_pcie(bus->sysdata); |
| 670 | unsigned long flags; |
| 671 | int ret; |
| 672 | |
| 673 | if (!pp) { |
| 674 | BUG(); |
| 675 | return -EINVAL; |
| 676 | } |
| 677 | |
| 678 | if (dw_pcie_valid_config(pp, bus, PCI_SLOT(devfn)) == 0) |
| 679 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 680 | |
| 681 | spin_lock_irqsave(&pp->conf_lock, flags); |
| 682 | if (bus->number != pp->root_bus_nr) |
| 683 | ret = dw_pcie_wr_other_conf(pp, bus, devfn, |
| 684 | where, size, val); |
| 685 | else |
| 686 | ret = dw_pcie_wr_own_conf(pp, where, size, val); |
| 687 | spin_unlock_irqrestore(&pp->conf_lock, flags); |
| 688 | |
| 689 | return ret; |
| 690 | } |
| 691 | |
| 692 | static struct pci_ops dw_pcie_ops = { |
| 693 | .read = dw_pcie_rd_conf, |
| 694 | .write = dw_pcie_wr_conf, |
| 695 | }; |
| 696 | |
Bjorn Helgaas | 73e4085 | 2013-10-09 09:12:37 -0600 | [diff] [blame] | 697 | static int dw_pcie_setup(int nr, struct pci_sys_data *sys) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 698 | { |
| 699 | struct pcie_port *pp; |
| 700 | |
| 701 | pp = sys_to_pcie(sys); |
| 702 | |
| 703 | if (!pp) |
| 704 | return 0; |
| 705 | |
| 706 | if (global_io_offset < SZ_1M && pp->config.io_size > 0) { |
| 707 | sys->io_offset = global_io_offset - pp->config.io_bus_addr; |
Pratyush Anand | fce8591 | 2013-12-11 15:08:33 +0530 | [diff] [blame] | 708 | pci_ioremap_io(global_io_offset, pp->io_base); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 709 | global_io_offset += SZ_64K; |
| 710 | pci_add_resource_offset(&sys->resources, &pp->io, |
| 711 | sys->io_offset); |
| 712 | } |
| 713 | |
| 714 | sys->mem_offset = pp->mem.start - pp->config.mem_bus_addr; |
| 715 | pci_add_resource_offset(&sys->resources, &pp->mem, sys->mem_offset); |
| 716 | |
| 717 | return 1; |
| 718 | } |
| 719 | |
Bjorn Helgaas | 73e4085 | 2013-10-09 09:12:37 -0600 | [diff] [blame] | 720 | static struct pci_bus *dw_pcie_scan_bus(int nr, struct pci_sys_data *sys) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 721 | { |
| 722 | struct pci_bus *bus; |
| 723 | struct pcie_port *pp = sys_to_pcie(sys); |
| 724 | |
| 725 | if (pp) { |
| 726 | pp->root_bus_nr = sys->busnr; |
Lucas Stach | f86b3e3 | 2014-04-16 10:23:46 -0600 | [diff] [blame] | 727 | bus = pci_scan_root_bus(pp->dev, sys->busnr, &dw_pcie_ops, |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 728 | sys, &sys->resources); |
| 729 | } else { |
| 730 | bus = NULL; |
| 731 | BUG(); |
| 732 | } |
| 733 | |
| 734 | return bus; |
| 735 | } |
| 736 | |
Bjorn Helgaas | 73e4085 | 2013-10-09 09:12:37 -0600 | [diff] [blame] | 737 | static int dw_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin) |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 738 | { |
| 739 | struct pcie_port *pp = sys_to_pcie(dev->bus->sysdata); |
Lucas Stach | f86b3e3 | 2014-04-16 10:23:46 -0600 | [diff] [blame] | 740 | int irq; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 741 | |
Lucas Stach | f86b3e3 | 2014-04-16 10:23:46 -0600 | [diff] [blame] | 742 | irq = of_irq_parse_and_map_pci(dev, slot, pin); |
| 743 | if (!irq) |
| 744 | irq = pp->irq; |
| 745 | |
| 746 | return irq; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 747 | } |
| 748 | |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 749 | static void dw_pcie_add_bus(struct pci_bus *bus) |
| 750 | { |
| 751 | if (IS_ENABLED(CONFIG_PCI_MSI)) { |
| 752 | struct pcie_port *pp = sys_to_pcie(bus->sysdata); |
| 753 | |
| 754 | dw_pcie_msi_chip.dev = pp->dev; |
| 755 | bus->msi = &dw_pcie_msi_chip; |
| 756 | } |
| 757 | } |
| 758 | |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 759 | static struct hw_pci dw_pci = { |
| 760 | .setup = dw_pcie_setup, |
| 761 | .scan = dw_pcie_scan_bus, |
| 762 | .map_irq = dw_pcie_map_irq, |
Jingoo Han | f342d94 | 2013-09-06 15:54:59 +0900 | [diff] [blame] | 763 | .add_bus = dw_pcie_add_bus, |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 764 | }; |
| 765 | |
| 766 | void dw_pcie_setup_rc(struct pcie_port *pp) |
| 767 | { |
| 768 | struct pcie_port_info *config = &pp->config; |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 769 | u32 val; |
| 770 | u32 membase; |
| 771 | u32 memlimit; |
| 772 | |
Mohit Kumar | c23fdc7 | 2014-04-16 10:23:28 -0600 | [diff] [blame] | 773 | /* set the number of lanes */ |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 774 | dw_pcie_readl_rc(pp, PCIE_PORT_LINK_CONTROL, &val); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 775 | val &= ~PORT_LINK_MODE_MASK; |
| 776 | switch (pp->lanes) { |
| 777 | case 1: |
| 778 | val |= PORT_LINK_MODE_1_LANES; |
| 779 | break; |
| 780 | case 2: |
| 781 | val |= PORT_LINK_MODE_2_LANES; |
| 782 | break; |
| 783 | case 4: |
| 784 | val |= PORT_LINK_MODE_4_LANES; |
| 785 | break; |
| 786 | } |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 787 | dw_pcie_writel_rc(pp, val, PCIE_PORT_LINK_CONTROL); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 788 | |
| 789 | /* set link width speed control register */ |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 790 | dw_pcie_readl_rc(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, &val); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 791 | val &= ~PORT_LOGIC_LINK_WIDTH_MASK; |
| 792 | switch (pp->lanes) { |
| 793 | case 1: |
| 794 | val |= PORT_LOGIC_LINK_WIDTH_1_LANES; |
| 795 | break; |
| 796 | case 2: |
| 797 | val |= PORT_LOGIC_LINK_WIDTH_2_LANES; |
| 798 | break; |
| 799 | case 4: |
| 800 | val |= PORT_LOGIC_LINK_WIDTH_4_LANES; |
| 801 | break; |
| 802 | } |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 803 | dw_pcie_writel_rc(pp, val, PCIE_LINK_WIDTH_SPEED_CONTROL); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 804 | |
| 805 | /* setup RC BARs */ |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 806 | dw_pcie_writel_rc(pp, 0x00000004, PCI_BASE_ADDRESS_0); |
Mohit Kumar | dbffdd6 | 2014-02-19 17:34:35 +0530 | [diff] [blame] | 807 | dw_pcie_writel_rc(pp, 0x00000000, PCI_BASE_ADDRESS_1); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 808 | |
| 809 | /* setup interrupt pins */ |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 810 | dw_pcie_readl_rc(pp, PCI_INTERRUPT_LINE, &val); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 811 | val &= 0xffff00ff; |
| 812 | val |= 0x00000100; |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 813 | dw_pcie_writel_rc(pp, val, PCI_INTERRUPT_LINE); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 814 | |
| 815 | /* setup bus numbers */ |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 816 | dw_pcie_readl_rc(pp, PCI_PRIMARY_BUS, &val); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 817 | val &= 0xff000000; |
| 818 | val |= 0x00010100; |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 819 | dw_pcie_writel_rc(pp, val, PCI_PRIMARY_BUS); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 820 | |
| 821 | /* setup memory base, memory limit */ |
| 822 | membase = ((u32)pp->mem_base & 0xfff00000) >> 16; |
| 823 | memlimit = (config->mem_size + (u32)pp->mem_base) & 0xfff00000; |
| 824 | val = memlimit | membase; |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 825 | dw_pcie_writel_rc(pp, val, PCI_MEMORY_BASE); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 826 | |
| 827 | /* setup command register */ |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 828 | dw_pcie_readl_rc(pp, PCI_COMMAND, &val); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 829 | val &= 0xffff0000; |
| 830 | val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY | |
| 831 | PCI_COMMAND_MASTER | PCI_COMMAND_SERR; |
Seungwon Jeon | f7b7868 | 2013-08-28 20:53:30 +0900 | [diff] [blame] | 832 | dw_pcie_writel_rc(pp, val, PCI_COMMAND); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 833 | } |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 834 | |
| 835 | MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>"); |
Jingoo Han | 4b1ced8 | 2013-07-31 17:14:10 +0900 | [diff] [blame] | 836 | MODULE_DESCRIPTION("Designware PCIe host controller driver"); |
Jingoo Han | 340cba6 | 2013-06-21 16:24:54 +0900 | [diff] [blame] | 837 | MODULE_LICENSE("GPL v2"); |