blob: a2e69afabe6fc7d035156f2c3672f5b0d657cb6e [file] [log] [blame]
Jingoo Han340cba62013-06-21 16:24:54 +09001/*
Jingoo Han4b1ced82013-07-31 17:14:10 +09002 * Synopsys Designware PCIe host controller driver
Jingoo Han340cba62013-06-21 16:24:54 +09003 *
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 Hanf342d942013-09-06 15:54:59 +090014#include <linux/irq.h>
15#include <linux/irqdomain.h>
Jingoo Han340cba62013-06-21 16:24:54 +090016#include <linux/kernel.h>
Jingoo Han340cba62013-06-21 16:24:54 +090017#include <linux/module.h>
Jingoo Hanf342d942013-09-06 15:54:59 +090018#include <linux/msi.h>
Jingoo Han340cba62013-06-21 16:24:54 +090019#include <linux/of_address.h>
Jingoo Han340cba62013-06-21 16:24:54 +090020#include <linux/pci.h>
21#include <linux/pci_regs.h>
Jingoo Han340cba62013-06-21 16:24:54 +090022#include <linux/types.h>
23
Jingoo Han4b1ced82013-07-31 17:14:10 +090024#include "pcie-designware.h"
Jingoo Han340cba62013-06-21 16:24:54 +090025
26/* Synopsis specific PCIE configuration registers */
27#define PCIE_PORT_LINK_CONTROL 0x710
28#define PORT_LINK_MODE_MASK (0x3f << 16)
Jingoo Han4b1ced82013-07-31 17:14:10 +090029#define PORT_LINK_MODE_1_LANES (0x1 << 16)
30#define PORT_LINK_MODE_2_LANES (0x3 << 16)
Jingoo Han340cba62013-06-21 16:24:54 +090031#define PORT_LINK_MODE_4_LANES (0x7 << 16)
32
33#define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C
34#define PORT_LOGIC_SPEED_CHANGE (0x1 << 17)
35#define PORT_LOGIC_LINK_WIDTH_MASK (0x1ff << 8)
Jingoo Han4b1ced82013-07-31 17:14:10 +090036#define PORT_LOGIC_LINK_WIDTH_1_LANES (0x1 << 8)
37#define PORT_LOGIC_LINK_WIDTH_2_LANES (0x2 << 8)
38#define PORT_LOGIC_LINK_WIDTH_4_LANES (0x4 << 8)
Jingoo Han340cba62013-06-21 16:24:54 +090039
40#define PCIE_MSI_ADDR_LO 0x820
41#define PCIE_MSI_ADDR_HI 0x824
42#define PCIE_MSI_INTR0_ENABLE 0x828
43#define PCIE_MSI_INTR0_MASK 0x82C
44#define PCIE_MSI_INTR0_STATUS 0x830
45
46#define PCIE_ATU_VIEWPORT 0x900
47#define PCIE_ATU_REGION_INBOUND (0x1 << 31)
48#define PCIE_ATU_REGION_OUTBOUND (0x0 << 31)
49#define PCIE_ATU_REGION_INDEX1 (0x1 << 0)
50#define PCIE_ATU_REGION_INDEX0 (0x0 << 0)
51#define PCIE_ATU_CR1 0x904
52#define PCIE_ATU_TYPE_MEM (0x0 << 0)
53#define PCIE_ATU_TYPE_IO (0x2 << 0)
54#define PCIE_ATU_TYPE_CFG0 (0x4 << 0)
55#define PCIE_ATU_TYPE_CFG1 (0x5 << 0)
56#define PCIE_ATU_CR2 0x908
57#define PCIE_ATU_ENABLE (0x1 << 31)
58#define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30)
59#define PCIE_ATU_LOWER_BASE 0x90C
60#define PCIE_ATU_UPPER_BASE 0x910
61#define PCIE_ATU_LIMIT 0x914
62#define PCIE_ATU_LOWER_TARGET 0x918
63#define PCIE_ATU_BUS(x) (((x) & 0xff) << 24)
64#define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19)
65#define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16)
66#define PCIE_ATU_UPPER_TARGET 0x91C
67
Jingoo Han4b1ced82013-07-31 17:14:10 +090068static struct hw_pci dw_pci;
Jingoo Han340cba62013-06-21 16:24:54 +090069
Bjorn Helgaas73e40852013-10-09 09:12:37 -060070static unsigned long global_io_offset;
Jingoo Han340cba62013-06-21 16:24:54 +090071
72static inline struct pcie_port *sys_to_pcie(struct pci_sys_data *sys)
73{
74 return sys->private_data;
75}
76
Jingoo Han4b1ced82013-07-31 17:14:10 +090077int cfg_read(void __iomem *addr, int where, int size, u32 *val)
Jingoo Han340cba62013-06-21 16:24:54 +090078{
79 *val = readl(addr);
80
81 if (size == 1)
82 *val = (*val >> (8 * (where & 3))) & 0xff;
83 else if (size == 2)
84 *val = (*val >> (8 * (where & 3))) & 0xffff;
85 else if (size != 4)
86 return PCIBIOS_BAD_REGISTER_NUMBER;
87
88 return PCIBIOS_SUCCESSFUL;
89}
90
Jingoo Han4b1ced82013-07-31 17:14:10 +090091int cfg_write(void __iomem *addr, int where, int size, u32 val)
Jingoo Han340cba62013-06-21 16:24:54 +090092{
93 if (size == 4)
94 writel(val, addr);
95 else if (size == 2)
96 writew(val, addr + (where & 2));
97 else if (size == 1)
98 writeb(val, addr + (where & 3));
99 else
100 return PCIBIOS_BAD_REGISTER_NUMBER;
101
102 return PCIBIOS_SUCCESSFUL;
103}
104
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900105static inline void dw_pcie_readl_rc(struct pcie_port *pp, u32 reg, u32 *val)
Jingoo Han340cba62013-06-21 16:24:54 +0900106{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900107 if (pp->ops->readl_rc)
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900108 pp->ops->readl_rc(pp, pp->dbi_base + reg, val);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900109 else
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900110 *val = readl(pp->dbi_base + reg);
Jingoo Han340cba62013-06-21 16:24:54 +0900111}
112
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900113static inline void dw_pcie_writel_rc(struct pcie_port *pp, u32 val, u32 reg)
Jingoo Han340cba62013-06-21 16:24:54 +0900114{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900115 if (pp->ops->writel_rc)
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900116 pp->ops->writel_rc(pp, val, pp->dbi_base + reg);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900117 else
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900118 writel(val, pp->dbi_base + reg);
Jingoo Han340cba62013-06-21 16:24:54 +0900119}
120
Bjorn Helgaas73e40852013-10-09 09:12:37 -0600121static int dw_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
122 u32 *val)
Jingoo Han340cba62013-06-21 16:24:54 +0900123{
124 int ret;
125
Jingoo Han4b1ced82013-07-31 17:14:10 +0900126 if (pp->ops->rd_own_conf)
127 ret = pp->ops->rd_own_conf(pp, where, size, val);
128 else
129 ret = cfg_read(pp->dbi_base + (where & ~0x3), where, size, val);
130
Jingoo Han340cba62013-06-21 16:24:54 +0900131 return ret;
132}
133
Bjorn Helgaas73e40852013-10-09 09:12:37 -0600134static int dw_pcie_wr_own_conf(struct pcie_port *pp, int where, int size,
135 u32 val)
Jingoo Han340cba62013-06-21 16:24:54 +0900136{
137 int ret;
138
Jingoo Han4b1ced82013-07-31 17:14:10 +0900139 if (pp->ops->wr_own_conf)
140 ret = pp->ops->wr_own_conf(pp, where, size, val);
Jingoo Han340cba62013-06-21 16:24:54 +0900141 else
Jingoo Han4b1ced82013-07-31 17:14:10 +0900142 ret = cfg_write(pp->dbi_base + (where & ~0x3), where, size,
143 val);
Jingoo Han340cba62013-06-21 16:24:54 +0900144
145 return ret;
146}
147
Jingoo Hanf342d942013-09-06 15:54:59 +0900148static struct irq_chip dw_msi_irq_chip = {
149 .name = "PCI-MSI",
150 .irq_enable = unmask_msi_irq,
151 .irq_disable = mask_msi_irq,
152 .irq_mask = mask_msi_irq,
153 .irq_unmask = unmask_msi_irq,
154};
155
156/* MSI int handler */
157void dw_handle_msi_irq(struct pcie_port *pp)
158{
159 unsigned long val;
160 int i, pos;
161
162 for (i = 0; i < MAX_MSI_CTRLS; i++) {
163 dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_STATUS + i * 12, 4,
164 (u32 *)&val);
165 if (val) {
166 pos = 0;
167 while ((pos = find_next_bit(&val, 32, pos)) != 32) {
168 generic_handle_irq(pp->msi_irq_start
169 + (i * 32) + pos);
170 pos++;
171 }
172 }
173 dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_STATUS + i * 12, 4, val);
174 }
175}
176
177void dw_pcie_msi_init(struct pcie_port *pp)
178{
179 pp->msi_data = __get_free_pages(GFP_KERNEL, 0);
180
181 /* program the msi_data */
182 dw_pcie_wr_own_conf(pp, PCIE_MSI_ADDR_LO, 4,
183 virt_to_phys((void *)pp->msi_data));
184 dw_pcie_wr_own_conf(pp, PCIE_MSI_ADDR_HI, 4, 0);
185}
186
187static int find_valid_pos0(struct pcie_port *pp, int msgvec, int pos, int *pos0)
188{
189 int flag = 1;
190
191 do {
192 pos = find_next_zero_bit(pp->msi_irq_in_use,
193 MAX_MSI_IRQS, pos);
194 /*if you have reached to the end then get out from here.*/
195 if (pos == MAX_MSI_IRQS)
196 return -ENOSPC;
197 /*
198 * Check if this position is at correct offset.nvec is always a
199 * power of two. pos0 must be nvec bit alligned.
200 */
201 if (pos % msgvec)
202 pos += msgvec - (pos % msgvec);
203 else
204 flag = 0;
205 } while (flag);
206
207 *pos0 = pos;
208 return 0;
209}
210
211static int assign_irq(int no_irqs, struct msi_desc *desc, int *pos)
212{
213 int res, bit, irq, pos0, pos1, i;
214 u32 val;
215 struct pcie_port *pp = sys_to_pcie(desc->dev->bus->sysdata);
216
217 if (!pp) {
218 BUG();
219 return -EINVAL;
220 }
221
222 pos0 = find_first_zero_bit(pp->msi_irq_in_use,
223 MAX_MSI_IRQS);
224 if (pos0 % no_irqs) {
225 if (find_valid_pos0(pp, no_irqs, pos0, &pos0))
226 goto no_valid_irq;
227 }
228 if (no_irqs > 1) {
229 pos1 = find_next_bit(pp->msi_irq_in_use,
230 MAX_MSI_IRQS, pos0);
231 /* there must be nvec number of consecutive free bits */
232 while ((pos1 - pos0) < no_irqs) {
233 if (find_valid_pos0(pp, no_irqs, pos1, &pos0))
234 goto no_valid_irq;
235 pos1 = find_next_bit(pp->msi_irq_in_use,
236 MAX_MSI_IRQS, pos0);
237 }
238 }
239
240 irq = (pp->msi_irq_start + pos0);
241
242 if ((irq + no_irqs) > (pp->msi_irq_start + MAX_MSI_IRQS-1))
243 goto no_valid_irq;
244
245 i = 0;
246 while (i < no_irqs) {
247 set_bit(pos0 + i, pp->msi_irq_in_use);
248 irq_alloc_descs((irq + i), (irq + i), 1, 0);
249 irq_set_msi_desc(irq + i, desc);
250 /*Enable corresponding interrupt in MSI interrupt controller */
251 res = ((pos0 + i) / 32) * 12;
252 bit = (pos0 + i) % 32;
253 dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, &val);
254 val |= 1 << bit;
255 dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, val);
256 i++;
257 }
258
259 *pos = pos0;
260 return irq;
261
262no_valid_irq:
263 *pos = pos0;
264 return -ENOSPC;
265}
266
267static void clear_irq(unsigned int irq)
268{
269 int res, bit, val, pos;
270 struct irq_desc *desc;
271 struct msi_desc *msi;
272 struct pcie_port *pp;
273
274 /* get the port structure */
275 desc = irq_to_desc(irq);
276 msi = irq_desc_get_msi_desc(desc);
277 pp = sys_to_pcie(msi->dev->bus->sysdata);
278 if (!pp) {
279 BUG();
280 return;
281 }
282
283 pos = irq - pp->msi_irq_start;
284
285 irq_free_desc(irq);
286
287 clear_bit(pos, pp->msi_irq_in_use);
288
289 /* Disable corresponding interrupt on MSI interrupt controller */
290 res = (pos / 32) * 12;
291 bit = pos % 32;
292 dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, &val);
293 val &= ~(1 << bit);
294 dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, val);
295}
296
297static int dw_msi_setup_irq(struct msi_chip *chip, struct pci_dev *pdev,
298 struct msi_desc *desc)
299{
300 int irq, pos, msgvec;
301 u16 msg_ctr;
302 struct msi_msg msg;
303 struct pcie_port *pp = sys_to_pcie(pdev->bus->sysdata);
304
305 if (!pp) {
306 BUG();
307 return -EINVAL;
308 }
309
310 pci_read_config_word(pdev, desc->msi_attrib.pos+PCI_MSI_FLAGS,
311 &msg_ctr);
312 msgvec = (msg_ctr&PCI_MSI_FLAGS_QSIZE) >> 4;
313 if (msgvec == 0)
314 msgvec = (msg_ctr & PCI_MSI_FLAGS_QMASK) >> 1;
315 if (msgvec > 5)
316 msgvec = 0;
317
318 irq = assign_irq((1 << msgvec), desc, &pos);
319 if (irq < 0)
320 return irq;
321
322 msg_ctr &= ~PCI_MSI_FLAGS_QSIZE;
323 msg_ctr |= msgvec << 4;
324 pci_write_config_word(pdev, desc->msi_attrib.pos + PCI_MSI_FLAGS,
325 msg_ctr);
326 desc->msi_attrib.multiple = msgvec;
327
328 msg.address_lo = virt_to_phys((void *)pp->msi_data);
329 msg.address_hi = 0x0;
330 msg.data = pos;
331 write_msi_msg(irq, &msg);
332
333 return 0;
334}
335
336static void dw_msi_teardown_irq(struct msi_chip *chip, unsigned int irq)
337{
338 clear_irq(irq);
339}
340
341static struct msi_chip dw_pcie_msi_chip = {
342 .setup_irq = dw_msi_setup_irq,
343 .teardown_irq = dw_msi_teardown_irq,
344};
345
Jingoo Han4b1ced82013-07-31 17:14:10 +0900346int dw_pcie_link_up(struct pcie_port *pp)
Jingoo Han340cba62013-06-21 16:24:54 +0900347{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900348 if (pp->ops->link_up)
349 return pp->ops->link_up(pp);
Jingoo Han340cba62013-06-21 16:24:54 +0900350 else
Jingoo Han340cba62013-06-21 16:24:54 +0900351 return 0;
Jingoo Han340cba62013-06-21 16:24:54 +0900352}
353
Jingoo Hanf342d942013-09-06 15:54:59 +0900354static int dw_pcie_msi_map(struct irq_domain *domain, unsigned int irq,
355 irq_hw_number_t hwirq)
356{
357 irq_set_chip_and_handler(irq, &dw_msi_irq_chip, handle_simple_irq);
358 irq_set_chip_data(irq, domain->host_data);
359 set_irq_flags(irq, IRQF_VALID);
360
361 return 0;
362}
363
364static const struct irq_domain_ops msi_domain_ops = {
365 .map = dw_pcie_msi_map,
366};
367
Jingoo Han4b1ced82013-07-31 17:14:10 +0900368int __init dw_pcie_host_init(struct pcie_port *pp)
Jingoo Han340cba62013-06-21 16:24:54 +0900369{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900370 struct device_node *np = pp->dev->of_node;
Jingoo Han340cba62013-06-21 16:24:54 +0900371 struct of_pci_range range;
372 struct of_pci_range_parser parser;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900373 u32 val;
Jingoo Han340cba62013-06-21 16:24:54 +0900374
Jingoo Hanf342d942013-09-06 15:54:59 +0900375 struct irq_domain *irq_domain;
376
Jingoo Han340cba62013-06-21 16:24:54 +0900377 if (of_pci_range_parser_init(&parser, np)) {
Jingoo Han4b1ced82013-07-31 17:14:10 +0900378 dev_err(pp->dev, "missing ranges property\n");
Jingoo Han340cba62013-06-21 16:24:54 +0900379 return -EINVAL;
380 }
381
382 /* Get the I/O and memory ranges from DT */
383 for_each_of_pci_range(&parser, &range) {
384 unsigned long restype = range.flags & IORESOURCE_TYPE_BITS;
385 if (restype == IORESOURCE_IO) {
386 of_pci_range_to_resource(&range, np, &pp->io);
387 pp->io.name = "I/O";
388 pp->io.start = max_t(resource_size_t,
389 PCIBIOS_MIN_IO,
390 range.pci_addr + global_io_offset);
391 pp->io.end = min_t(resource_size_t,
392 IO_SPACE_LIMIT,
393 range.pci_addr + range.size
394 + global_io_offset);
395 pp->config.io_size = resource_size(&pp->io);
396 pp->config.io_bus_addr = range.pci_addr;
397 }
398 if (restype == IORESOURCE_MEM) {
399 of_pci_range_to_resource(&range, np, &pp->mem);
400 pp->mem.name = "MEM";
401 pp->config.mem_size = resource_size(&pp->mem);
402 pp->config.mem_bus_addr = range.pci_addr;
403 }
404 if (restype == 0) {
405 of_pci_range_to_resource(&range, np, &pp->cfg);
406 pp->config.cfg0_size = resource_size(&pp->cfg)/2;
407 pp->config.cfg1_size = resource_size(&pp->cfg)/2;
408 }
409 }
410
Jingoo Han4b1ced82013-07-31 17:14:10 +0900411 if (!pp->dbi_base) {
412 pp->dbi_base = devm_ioremap(pp->dev, pp->cfg.start,
413 resource_size(&pp->cfg));
414 if (!pp->dbi_base) {
415 dev_err(pp->dev, "error with ioremap\n");
416 return -ENOMEM;
417 }
Jingoo Han340cba62013-06-21 16:24:54 +0900418 }
Jingoo Han340cba62013-06-21 16:24:54 +0900419
Jingoo Han4b1ced82013-07-31 17:14:10 +0900420 pp->cfg0_base = pp->cfg.start;
421 pp->cfg1_base = pp->cfg.start + pp->config.cfg0_size;
422 pp->io_base = pp->io.start;
423 pp->mem_base = pp->mem.start;
424
425 pp->va_cfg0_base = devm_ioremap(pp->dev, pp->cfg0_base,
426 pp->config.cfg0_size);
427 if (!pp->va_cfg0_base) {
428 dev_err(pp->dev, "error with ioremap in function\n");
429 return -ENOMEM;
Jingoo Han340cba62013-06-21 16:24:54 +0900430 }
Jingoo Han4b1ced82013-07-31 17:14:10 +0900431 pp->va_cfg1_base = devm_ioremap(pp->dev, pp->cfg1_base,
432 pp->config.cfg1_size);
433 if (!pp->va_cfg1_base) {
434 dev_err(pp->dev, "error with ioremap\n");
435 return -ENOMEM;
436 }
Jingoo Han340cba62013-06-21 16:24:54 +0900437
Jingoo Han4b1ced82013-07-31 17:14:10 +0900438 if (of_property_read_u32(np, "num-lanes", &pp->lanes)) {
439 dev_err(pp->dev, "Failed to parse the number of lanes\n");
440 return -EINVAL;
441 }
Jingoo Han340cba62013-06-21 16:24:54 +0900442
Jingoo Hanf342d942013-09-06 15:54:59 +0900443 if (IS_ENABLED(CONFIG_PCI_MSI)) {
444 irq_domain = irq_domain_add_linear(pp->dev->of_node,
445 MAX_MSI_IRQS, &msi_domain_ops,
446 &dw_pcie_msi_chip);
447 if (!irq_domain) {
448 dev_err(pp->dev, "irq domain init failed\n");
449 return -ENXIO;
450 }
451
452 pp->msi_irq_start = irq_find_mapping(irq_domain, 0);
453 }
454
Jingoo Han4b1ced82013-07-31 17:14:10 +0900455 if (pp->ops->host_init)
456 pp->ops->host_init(pp);
Jingoo Han340cba62013-06-21 16:24:54 +0900457
Jingoo Han4b1ced82013-07-31 17:14:10 +0900458 dw_pcie_wr_own_conf(pp, PCI_BASE_ADDRESS_0, 4, 0);
459
460 /* program correct class for RC */
461 dw_pcie_wr_own_conf(pp, PCI_CLASS_DEVICE, 2, PCI_CLASS_BRIDGE_PCI);
462
463 dw_pcie_rd_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, &val);
464 val |= PORT_LOGIC_SPEED_CHANGE;
465 dw_pcie_wr_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, val);
466
467 dw_pci.nr_controllers = 1;
468 dw_pci.private_data = (void **)&pp;
469
470 pci_common_init(&dw_pci);
Jingoo Han340cba62013-06-21 16:24:54 +0900471 pci_assign_unassigned_resources();
472#ifdef CONFIG_PCI_DOMAINS
Jingoo Han4b1ced82013-07-31 17:14:10 +0900473 dw_pci.domain++;
Jingoo Han340cba62013-06-21 16:24:54 +0900474#endif
475
Jingoo Han340cba62013-06-21 16:24:54 +0900476 return 0;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900477}
Jingoo Han340cba62013-06-21 16:24:54 +0900478
Jingoo Han4b1ced82013-07-31 17:14:10 +0900479static void dw_pcie_prog_viewport_cfg0(struct pcie_port *pp, u32 busdev)
480{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900481 /* Program viewport 0 : OUTBOUND : CFG0 */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900482 dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0,
483 PCIE_ATU_VIEWPORT);
484 dw_pcie_writel_rc(pp, pp->cfg0_base, PCIE_ATU_LOWER_BASE);
485 dw_pcie_writel_rc(pp, (pp->cfg0_base >> 32), PCIE_ATU_UPPER_BASE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900486 dw_pcie_writel_rc(pp, pp->cfg0_base + pp->config.cfg0_size - 1,
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900487 PCIE_ATU_LIMIT);
488 dw_pcie_writel_rc(pp, busdev, PCIE_ATU_LOWER_TARGET);
489 dw_pcie_writel_rc(pp, 0, PCIE_ATU_UPPER_TARGET);
490 dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_CFG0, PCIE_ATU_CR1);
491 dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900492}
493
494static void dw_pcie_prog_viewport_cfg1(struct pcie_port *pp, u32 busdev)
495{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900496 /* Program viewport 1 : OUTBOUND : CFG1 */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900497 dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1,
498 PCIE_ATU_VIEWPORT);
499 dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_CFG1, PCIE_ATU_CR1);
500 dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2);
501 dw_pcie_writel_rc(pp, pp->cfg1_base, PCIE_ATU_LOWER_BASE);
502 dw_pcie_writel_rc(pp, (pp->cfg1_base >> 32), PCIE_ATU_UPPER_BASE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900503 dw_pcie_writel_rc(pp, pp->cfg1_base + pp->config.cfg1_size - 1,
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900504 PCIE_ATU_LIMIT);
505 dw_pcie_writel_rc(pp, busdev, PCIE_ATU_LOWER_TARGET);
506 dw_pcie_writel_rc(pp, 0, PCIE_ATU_UPPER_TARGET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900507}
508
509static void dw_pcie_prog_viewport_mem_outbound(struct pcie_port *pp)
510{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900511 /* Program viewport 0 : OUTBOUND : MEM */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900512 dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0,
513 PCIE_ATU_VIEWPORT);
514 dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_MEM, PCIE_ATU_CR1);
515 dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2);
516 dw_pcie_writel_rc(pp, pp->mem_base, PCIE_ATU_LOWER_BASE);
517 dw_pcie_writel_rc(pp, (pp->mem_base >> 32), PCIE_ATU_UPPER_BASE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900518 dw_pcie_writel_rc(pp, pp->mem_base + pp->config.mem_size - 1,
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900519 PCIE_ATU_LIMIT);
520 dw_pcie_writel_rc(pp, pp->config.mem_bus_addr, PCIE_ATU_LOWER_TARGET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900521 dw_pcie_writel_rc(pp, upper_32_bits(pp->config.mem_bus_addr),
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900522 PCIE_ATU_UPPER_TARGET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900523}
524
525static void dw_pcie_prog_viewport_io_outbound(struct pcie_port *pp)
526{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900527 /* Program viewport 1 : OUTBOUND : IO */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900528 dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1,
529 PCIE_ATU_VIEWPORT);
530 dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_IO, PCIE_ATU_CR1);
531 dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2);
532 dw_pcie_writel_rc(pp, pp->io_base, PCIE_ATU_LOWER_BASE);
533 dw_pcie_writel_rc(pp, (pp->io_base >> 32), PCIE_ATU_UPPER_BASE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900534 dw_pcie_writel_rc(pp, pp->io_base + pp->config.io_size - 1,
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900535 PCIE_ATU_LIMIT);
536 dw_pcie_writel_rc(pp, pp->config.io_bus_addr, PCIE_ATU_LOWER_TARGET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900537 dw_pcie_writel_rc(pp, upper_32_bits(pp->config.io_bus_addr),
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900538 PCIE_ATU_UPPER_TARGET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900539}
540
541static int dw_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus,
542 u32 devfn, int where, int size, u32 *val)
543{
544 int ret = PCIBIOS_SUCCESSFUL;
545 u32 address, busdev;
546
547 busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
548 PCIE_ATU_FUNC(PCI_FUNC(devfn));
549 address = where & ~0x3;
550
551 if (bus->parent->number == pp->root_bus_nr) {
552 dw_pcie_prog_viewport_cfg0(pp, busdev);
553 ret = cfg_read(pp->va_cfg0_base + address, where, size, val);
554 dw_pcie_prog_viewport_mem_outbound(pp);
555 } else {
556 dw_pcie_prog_viewport_cfg1(pp, busdev);
557 ret = cfg_read(pp->va_cfg1_base + address, where, size, val);
558 dw_pcie_prog_viewport_io_outbound(pp);
559 }
560
Jingoo Han340cba62013-06-21 16:24:54 +0900561 return ret;
562}
563
Jingoo Han4b1ced82013-07-31 17:14:10 +0900564static int dw_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus,
565 u32 devfn, int where, int size, u32 val)
Jingoo Han340cba62013-06-21 16:24:54 +0900566{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900567 int ret = PCIBIOS_SUCCESSFUL;
568 u32 address, busdev;
Jingoo Han340cba62013-06-21 16:24:54 +0900569
Jingoo Han4b1ced82013-07-31 17:14:10 +0900570 busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
571 PCIE_ATU_FUNC(PCI_FUNC(devfn));
572 address = where & ~0x3;
Jingoo Han340cba62013-06-21 16:24:54 +0900573
Jingoo Han4b1ced82013-07-31 17:14:10 +0900574 if (bus->parent->number == pp->root_bus_nr) {
575 dw_pcie_prog_viewport_cfg0(pp, busdev);
576 ret = cfg_write(pp->va_cfg0_base + address, where, size, val);
577 dw_pcie_prog_viewport_mem_outbound(pp);
578 } else {
579 dw_pcie_prog_viewport_cfg1(pp, busdev);
580 ret = cfg_write(pp->va_cfg1_base + address, where, size, val);
581 dw_pcie_prog_viewport_io_outbound(pp);
582 }
583
584 return ret;
Jingoo Han340cba62013-06-21 16:24:54 +0900585}
586
Jingoo Han340cba62013-06-21 16:24:54 +0900587
Jingoo Han4b1ced82013-07-31 17:14:10 +0900588static int dw_pcie_valid_config(struct pcie_port *pp,
589 struct pci_bus *bus, int dev)
Jingoo Han340cba62013-06-21 16:24:54 +0900590{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900591 /* If there is no link, then there is no device */
592 if (bus->number != pp->root_bus_nr) {
593 if (!dw_pcie_link_up(pp))
594 return 0;
595 }
Jingoo Han340cba62013-06-21 16:24:54 +0900596
Jingoo Han4b1ced82013-07-31 17:14:10 +0900597 /* access only one slot on each root port */
598 if (bus->number == pp->root_bus_nr && dev > 0)
599 return 0;
Jingoo Han340cba62013-06-21 16:24:54 +0900600
601 /*
Jingoo Han4b1ced82013-07-31 17:14:10 +0900602 * do not read more than one device on the bus directly attached
603 * to RC's (Virtual Bridge's) DS side.
Jingoo Han340cba62013-06-21 16:24:54 +0900604 */
Jingoo Han4b1ced82013-07-31 17:14:10 +0900605 if (bus->primary == pp->root_bus_nr && dev > 0)
Jingoo Han340cba62013-06-21 16:24:54 +0900606 return 0;
Jingoo Han340cba62013-06-21 16:24:54 +0900607
608 return 1;
609}
610
Jingoo Han4b1ced82013-07-31 17:14:10 +0900611static int dw_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
612 int size, u32 *val)
Jingoo Han340cba62013-06-21 16:24:54 +0900613{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900614 struct pcie_port *pp = sys_to_pcie(bus->sysdata);
615 unsigned long flags;
616 int ret;
Jingoo Han340cba62013-06-21 16:24:54 +0900617
Jingoo Han4b1ced82013-07-31 17:14:10 +0900618 if (!pp) {
619 BUG();
620 return -EINVAL;
621 }
Jingoo Han340cba62013-06-21 16:24:54 +0900622
Jingoo Han4b1ced82013-07-31 17:14:10 +0900623 if (dw_pcie_valid_config(pp, bus, PCI_SLOT(devfn)) == 0) {
624 *val = 0xffffffff;
625 return PCIBIOS_DEVICE_NOT_FOUND;
626 }
627
628 spin_lock_irqsave(&pp->conf_lock, flags);
629 if (bus->number != pp->root_bus_nr)
630 ret = dw_pcie_rd_other_conf(pp, bus, devfn,
631 where, size, val);
632 else
633 ret = dw_pcie_rd_own_conf(pp, where, size, val);
634 spin_unlock_irqrestore(&pp->conf_lock, flags);
635
636 return ret;
Jingoo Han340cba62013-06-21 16:24:54 +0900637}
Jingoo Han4b1ced82013-07-31 17:14:10 +0900638
639static int dw_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
640 int where, int size, u32 val)
641{
642 struct pcie_port *pp = sys_to_pcie(bus->sysdata);
643 unsigned long flags;
644 int ret;
645
646 if (!pp) {
647 BUG();
648 return -EINVAL;
649 }
650
651 if (dw_pcie_valid_config(pp, bus, PCI_SLOT(devfn)) == 0)
652 return PCIBIOS_DEVICE_NOT_FOUND;
653
654 spin_lock_irqsave(&pp->conf_lock, flags);
655 if (bus->number != pp->root_bus_nr)
656 ret = dw_pcie_wr_other_conf(pp, bus, devfn,
657 where, size, val);
658 else
659 ret = dw_pcie_wr_own_conf(pp, where, size, val);
660 spin_unlock_irqrestore(&pp->conf_lock, flags);
661
662 return ret;
663}
664
665static struct pci_ops dw_pcie_ops = {
666 .read = dw_pcie_rd_conf,
667 .write = dw_pcie_wr_conf,
668};
669
Bjorn Helgaas73e40852013-10-09 09:12:37 -0600670static int dw_pcie_setup(int nr, struct pci_sys_data *sys)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900671{
672 struct pcie_port *pp;
673
674 pp = sys_to_pcie(sys);
675
676 if (!pp)
677 return 0;
678
679 if (global_io_offset < SZ_1M && pp->config.io_size > 0) {
680 sys->io_offset = global_io_offset - pp->config.io_bus_addr;
681 pci_ioremap_io(sys->io_offset, pp->io.start);
682 global_io_offset += SZ_64K;
683 pci_add_resource_offset(&sys->resources, &pp->io,
684 sys->io_offset);
685 }
686
687 sys->mem_offset = pp->mem.start - pp->config.mem_bus_addr;
688 pci_add_resource_offset(&sys->resources, &pp->mem, sys->mem_offset);
689
690 return 1;
691}
692
Bjorn Helgaas73e40852013-10-09 09:12:37 -0600693static struct pci_bus *dw_pcie_scan_bus(int nr, struct pci_sys_data *sys)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900694{
695 struct pci_bus *bus;
696 struct pcie_port *pp = sys_to_pcie(sys);
697
698 if (pp) {
699 pp->root_bus_nr = sys->busnr;
700 bus = pci_scan_root_bus(NULL, sys->busnr, &dw_pcie_ops,
701 sys, &sys->resources);
702 } else {
703 bus = NULL;
704 BUG();
705 }
706
707 return bus;
708}
709
Bjorn Helgaas73e40852013-10-09 09:12:37 -0600710static int dw_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900711{
712 struct pcie_port *pp = sys_to_pcie(dev->bus->sysdata);
713
714 return pp->irq;
715}
716
Jingoo Hanf342d942013-09-06 15:54:59 +0900717static void dw_pcie_add_bus(struct pci_bus *bus)
718{
719 if (IS_ENABLED(CONFIG_PCI_MSI)) {
720 struct pcie_port *pp = sys_to_pcie(bus->sysdata);
721
722 dw_pcie_msi_chip.dev = pp->dev;
723 bus->msi = &dw_pcie_msi_chip;
724 }
725}
726
Jingoo Han4b1ced82013-07-31 17:14:10 +0900727static struct hw_pci dw_pci = {
728 .setup = dw_pcie_setup,
729 .scan = dw_pcie_scan_bus,
730 .map_irq = dw_pcie_map_irq,
Jingoo Hanf342d942013-09-06 15:54:59 +0900731 .add_bus = dw_pcie_add_bus,
Jingoo Han4b1ced82013-07-31 17:14:10 +0900732};
733
734void dw_pcie_setup_rc(struct pcie_port *pp)
735{
736 struct pcie_port_info *config = &pp->config;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900737 u32 val;
738 u32 membase;
739 u32 memlimit;
740
741 /* set the number of lines as 4 */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900742 dw_pcie_readl_rc(pp, PCIE_PORT_LINK_CONTROL, &val);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900743 val &= ~PORT_LINK_MODE_MASK;
744 switch (pp->lanes) {
745 case 1:
746 val |= PORT_LINK_MODE_1_LANES;
747 break;
748 case 2:
749 val |= PORT_LINK_MODE_2_LANES;
750 break;
751 case 4:
752 val |= PORT_LINK_MODE_4_LANES;
753 break;
754 }
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900755 dw_pcie_writel_rc(pp, val, PCIE_PORT_LINK_CONTROL);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900756
757 /* set link width speed control register */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900758 dw_pcie_readl_rc(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, &val);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900759 val &= ~PORT_LOGIC_LINK_WIDTH_MASK;
760 switch (pp->lanes) {
761 case 1:
762 val |= PORT_LOGIC_LINK_WIDTH_1_LANES;
763 break;
764 case 2:
765 val |= PORT_LOGIC_LINK_WIDTH_2_LANES;
766 break;
767 case 4:
768 val |= PORT_LOGIC_LINK_WIDTH_4_LANES;
769 break;
770 }
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900771 dw_pcie_writel_rc(pp, val, PCIE_LINK_WIDTH_SPEED_CONTROL);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900772
773 /* setup RC BARs */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900774 dw_pcie_writel_rc(pp, 0x00000004, PCI_BASE_ADDRESS_0);
775 dw_pcie_writel_rc(pp, 0x00000004, PCI_BASE_ADDRESS_1);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900776
777 /* setup interrupt pins */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900778 dw_pcie_readl_rc(pp, PCI_INTERRUPT_LINE, &val);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900779 val &= 0xffff00ff;
780 val |= 0x00000100;
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900781 dw_pcie_writel_rc(pp, val, PCI_INTERRUPT_LINE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900782
783 /* setup bus numbers */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900784 dw_pcie_readl_rc(pp, PCI_PRIMARY_BUS, &val);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900785 val &= 0xff000000;
786 val |= 0x00010100;
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900787 dw_pcie_writel_rc(pp, val, PCI_PRIMARY_BUS);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900788
789 /* setup memory base, memory limit */
790 membase = ((u32)pp->mem_base & 0xfff00000) >> 16;
791 memlimit = (config->mem_size + (u32)pp->mem_base) & 0xfff00000;
792 val = memlimit | membase;
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900793 dw_pcie_writel_rc(pp, val, PCI_MEMORY_BASE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900794
795 /* setup command register */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900796 dw_pcie_readl_rc(pp, PCI_COMMAND, &val);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900797 val &= 0xffff0000;
798 val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
799 PCI_COMMAND_MASTER | PCI_COMMAND_SERR;
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900800 dw_pcie_writel_rc(pp, val, PCI_COMMAND);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900801}
Jingoo Han340cba62013-06-21 16:24:54 +0900802
803MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
Jingoo Han4b1ced82013-07-31 17:14:10 +0900804MODULE_DESCRIPTION("Designware PCIe host controller driver");
Jingoo Han340cba62013-06-21 16:24:54 +0900805MODULE_LICENSE("GPL v2");