blob: 0e9838aaf567d8b998695ee7ba332c7729481537 [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>
Lucas Stach804f57b2014-03-05 14:25:51 +010020#include <linux/of_pci.h>
Jingoo Han340cba62013-06-21 16:24:54 +090021#include <linux/pci.h>
22#include <linux/pci_regs.h>
Kishon Vijay Abraham I4dd964d2014-07-17 14:30:40 +053023#include <linux/platform_device.h>
Jingoo Han340cba62013-06-21 16:24:54 +090024#include <linux/types.h>
25
Jingoo Han4b1ced82013-07-31 17:14:10 +090026#include "pcie-designware.h"
Jingoo Han340cba62013-06-21 16:24:54 +090027
28/* Synopsis specific PCIE configuration registers */
29#define PCIE_PORT_LINK_CONTROL 0x710
30#define PORT_LINK_MODE_MASK (0x3f << 16)
Jingoo Han4b1ced82013-07-31 17:14:10 +090031#define PORT_LINK_MODE_1_LANES (0x1 << 16)
32#define PORT_LINK_MODE_2_LANES (0x3 << 16)
Jingoo Han340cba62013-06-21 16:24:54 +090033#define PORT_LINK_MODE_4_LANES (0x7 << 16)
34
35#define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C
36#define PORT_LOGIC_SPEED_CHANGE (0x1 << 17)
37#define PORT_LOGIC_LINK_WIDTH_MASK (0x1ff << 8)
Jingoo Han4b1ced82013-07-31 17:14:10 +090038#define PORT_LOGIC_LINK_WIDTH_1_LANES (0x1 << 8)
39#define PORT_LOGIC_LINK_WIDTH_2_LANES (0x2 << 8)
40#define PORT_LOGIC_LINK_WIDTH_4_LANES (0x4 << 8)
Jingoo Han340cba62013-06-21 16:24:54 +090041
42#define PCIE_MSI_ADDR_LO 0x820
43#define PCIE_MSI_ADDR_HI 0x824
44#define PCIE_MSI_INTR0_ENABLE 0x828
45#define PCIE_MSI_INTR0_MASK 0x82C
46#define PCIE_MSI_INTR0_STATUS 0x830
47
48#define PCIE_ATU_VIEWPORT 0x900
49#define PCIE_ATU_REGION_INBOUND (0x1 << 31)
50#define PCIE_ATU_REGION_OUTBOUND (0x0 << 31)
51#define PCIE_ATU_REGION_INDEX1 (0x1 << 0)
52#define PCIE_ATU_REGION_INDEX0 (0x0 << 0)
53#define PCIE_ATU_CR1 0x904
54#define PCIE_ATU_TYPE_MEM (0x0 << 0)
55#define PCIE_ATU_TYPE_IO (0x2 << 0)
56#define PCIE_ATU_TYPE_CFG0 (0x4 << 0)
57#define PCIE_ATU_TYPE_CFG1 (0x5 << 0)
58#define PCIE_ATU_CR2 0x908
59#define PCIE_ATU_ENABLE (0x1 << 31)
60#define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30)
61#define PCIE_ATU_LOWER_BASE 0x90C
62#define PCIE_ATU_UPPER_BASE 0x910
63#define PCIE_ATU_LIMIT 0x914
64#define PCIE_ATU_LOWER_TARGET 0x918
65#define PCIE_ATU_BUS(x) (((x) & 0xff) << 24)
66#define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19)
67#define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16)
68#define PCIE_ATU_UPPER_TARGET 0x91C
69
Jingoo Han4b1ced82013-07-31 17:14:10 +090070static struct hw_pci dw_pci;
Jingoo Han340cba62013-06-21 16:24:54 +090071
Bjorn Helgaas73e40852013-10-09 09:12:37 -060072static unsigned long global_io_offset;
Jingoo Han340cba62013-06-21 16:24:54 +090073
74static inline struct pcie_port *sys_to_pcie(struct pci_sys_data *sys)
75{
76 return sys->private_data;
77}
78
Pratyush Ananda01ef592013-12-11 15:08:32 +053079int dw_pcie_cfg_read(void __iomem *addr, int where, int size, u32 *val)
Jingoo Han340cba62013-06-21 16:24:54 +090080{
81 *val = readl(addr);
82
83 if (size == 1)
84 *val = (*val >> (8 * (where & 3))) & 0xff;
85 else if (size == 2)
86 *val = (*val >> (8 * (where & 3))) & 0xffff;
87 else if (size != 4)
88 return PCIBIOS_BAD_REGISTER_NUMBER;
89
90 return PCIBIOS_SUCCESSFUL;
91}
92
Pratyush Ananda01ef592013-12-11 15:08:32 +053093int dw_pcie_cfg_write(void __iomem *addr, int where, int size, u32 val)
Jingoo Han340cba62013-06-21 16:24:54 +090094{
95 if (size == 4)
96 writel(val, addr);
97 else if (size == 2)
98 writew(val, addr + (where & 2));
99 else if (size == 1)
100 writeb(val, addr + (where & 3));
101 else
102 return PCIBIOS_BAD_REGISTER_NUMBER;
103
104 return PCIBIOS_SUCCESSFUL;
105}
106
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900107static inline void dw_pcie_readl_rc(struct pcie_port *pp, u32 reg, u32 *val)
Jingoo Han340cba62013-06-21 16:24:54 +0900108{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900109 if (pp->ops->readl_rc)
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900110 pp->ops->readl_rc(pp, pp->dbi_base + reg, val);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900111 else
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900112 *val = readl(pp->dbi_base + reg);
Jingoo Han340cba62013-06-21 16:24:54 +0900113}
114
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900115static inline void dw_pcie_writel_rc(struct pcie_port *pp, u32 val, u32 reg)
Jingoo Han340cba62013-06-21 16:24:54 +0900116{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900117 if (pp->ops->writel_rc)
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900118 pp->ops->writel_rc(pp, val, pp->dbi_base + reg);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900119 else
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900120 writel(val, pp->dbi_base + reg);
Jingoo Han340cba62013-06-21 16:24:54 +0900121}
122
Bjorn Helgaas73e40852013-10-09 09:12:37 -0600123static int dw_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
124 u32 *val)
Jingoo Han340cba62013-06-21 16:24:54 +0900125{
126 int ret;
127
Jingoo Han4b1ced82013-07-31 17:14:10 +0900128 if (pp->ops->rd_own_conf)
129 ret = pp->ops->rd_own_conf(pp, where, size, val);
130 else
Pratyush Ananda01ef592013-12-11 15:08:32 +0530131 ret = dw_pcie_cfg_read(pp->dbi_base + (where & ~0x3), where,
132 size, val);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900133
Jingoo Han340cba62013-06-21 16:24:54 +0900134 return ret;
135}
136
Bjorn Helgaas73e40852013-10-09 09:12:37 -0600137static int dw_pcie_wr_own_conf(struct pcie_port *pp, int where, int size,
138 u32 val)
Jingoo Han340cba62013-06-21 16:24:54 +0900139{
140 int ret;
141
Jingoo Han4b1ced82013-07-31 17:14:10 +0900142 if (pp->ops->wr_own_conf)
143 ret = pp->ops->wr_own_conf(pp, where, size, val);
Jingoo Han340cba62013-06-21 16:24:54 +0900144 else
Pratyush Ananda01ef592013-12-11 15:08:32 +0530145 ret = dw_pcie_cfg_write(pp->dbi_base + (where & ~0x3), where,
146 size, val);
Jingoo Han340cba62013-06-21 16:24:54 +0900147
148 return ret;
149}
150
Jingoo Hanf342d942013-09-06 15:54:59 +0900151static struct irq_chip dw_msi_irq_chip = {
152 .name = "PCI-MSI",
153 .irq_enable = unmask_msi_irq,
154 .irq_disable = mask_msi_irq,
155 .irq_mask = mask_msi_irq,
156 .irq_unmask = unmask_msi_irq,
157};
158
159/* MSI int handler */
Lucas Stach7f4f16e2014-03-28 17:52:58 +0100160irqreturn_t dw_handle_msi_irq(struct pcie_port *pp)
Jingoo Hanf342d942013-09-06 15:54:59 +0900161{
162 unsigned long val;
Pratyush Anand904d0e72013-10-09 21:32:12 +0900163 int i, pos, irq;
Lucas Stach7f4f16e2014-03-28 17:52:58 +0100164 irqreturn_t ret = IRQ_NONE;
Jingoo Hanf342d942013-09-06 15:54:59 +0900165
166 for (i = 0; i < MAX_MSI_CTRLS; i++) {
167 dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_STATUS + i * 12, 4,
168 (u32 *)&val);
169 if (val) {
Lucas Stach7f4f16e2014-03-28 17:52:58 +0100170 ret = IRQ_HANDLED;
Jingoo Hanf342d942013-09-06 15:54:59 +0900171 pos = 0;
172 while ((pos = find_next_bit(&val, 32, pos)) != 32) {
Pratyush Anand904d0e72013-10-09 21:32:12 +0900173 irq = irq_find_mapping(pp->irq_domain,
174 i * 32 + pos);
Harro Haanca165892013-12-12 19:29:03 +0100175 dw_pcie_wr_own_conf(pp,
176 PCIE_MSI_INTR0_STATUS + i * 12,
177 4, 1 << pos);
Pratyush Anand904d0e72013-10-09 21:32:12 +0900178 generic_handle_irq(irq);
Jingoo Hanf342d942013-09-06 15:54:59 +0900179 pos++;
180 }
181 }
Jingoo Hanf342d942013-09-06 15:54:59 +0900182 }
Lucas Stach7f4f16e2014-03-28 17:52:58 +0100183
184 return ret;
Jingoo Hanf342d942013-09-06 15:54:59 +0900185}
186
187void dw_pcie_msi_init(struct pcie_port *pp)
188{
189 pp->msi_data = __get_free_pages(GFP_KERNEL, 0);
190
191 /* program the msi_data */
192 dw_pcie_wr_own_conf(pp, PCIE_MSI_ADDR_LO, 4,
193 virt_to_phys((void *)pp->msi_data));
194 dw_pcie_wr_own_conf(pp, PCIE_MSI_ADDR_HI, 4, 0);
195}
196
197static int find_valid_pos0(struct pcie_port *pp, int msgvec, int pos, int *pos0)
198{
199 int flag = 1;
200
201 do {
202 pos = find_next_zero_bit(pp->msi_irq_in_use,
203 MAX_MSI_IRQS, pos);
204 /*if you have reached to the end then get out from here.*/
205 if (pos == MAX_MSI_IRQS)
206 return -ENOSPC;
207 /*
208 * Check if this position is at correct offset.nvec is always a
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700209 * power of two. pos0 must be nvec bit aligned.
Jingoo Hanf342d942013-09-06 15:54:59 +0900210 */
211 if (pos % msgvec)
212 pos += msgvec - (pos % msgvec);
213 else
214 flag = 0;
215 } while (flag);
216
217 *pos0 = pos;
218 return 0;
219}
220
Bjørn Erik Nilsenbe3f48c2013-11-29 14:35:24 +0100221static void clear_irq_range(struct pcie_port *pp, unsigned int irq_base,
Jingoo Han58275f2f2013-12-27 09:30:25 +0900222 unsigned int nvec, unsigned int pos)
Bjørn Erik Nilsenbe3f48c2013-11-29 14:35:24 +0100223{
224 unsigned int i, res, bit, val;
225
Bjorn Helgaas0b8cfb62013-12-09 15:11:25 -0700226 for (i = 0; i < nvec; i++) {
Bjørn Erik Nilsenbe3f48c2013-11-29 14:35:24 +0100227 irq_set_msi_desc_off(irq_base, i, NULL);
228 clear_bit(pos + i, pp->msi_irq_in_use);
Jingoo Han58275f2f2013-12-27 09:30:25 +0900229 /* Disable corresponding interrupt on MSI controller */
Bjørn Erik Nilsenbe3f48c2013-11-29 14:35:24 +0100230 res = ((pos + i) / 32) * 12;
231 bit = (pos + i) % 32;
232 dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, &val);
233 val &= ~(1 << bit);
234 dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, val);
Bjørn Erik Nilsenbe3f48c2013-11-29 14:35:24 +0100235 }
236}
237
Jingoo Hanf342d942013-09-06 15:54:59 +0900238static int assign_irq(int no_irqs, struct msi_desc *desc, int *pos)
239{
240 int res, bit, irq, pos0, pos1, i;
241 u32 val;
242 struct pcie_port *pp = sys_to_pcie(desc->dev->bus->sysdata);
243
244 if (!pp) {
245 BUG();
246 return -EINVAL;
247 }
248
249 pos0 = find_first_zero_bit(pp->msi_irq_in_use,
250 MAX_MSI_IRQS);
251 if (pos0 % no_irqs) {
252 if (find_valid_pos0(pp, no_irqs, pos0, &pos0))
253 goto no_valid_irq;
254 }
255 if (no_irqs > 1) {
256 pos1 = find_next_bit(pp->msi_irq_in_use,
257 MAX_MSI_IRQS, pos0);
258 /* there must be nvec number of consecutive free bits */
259 while ((pos1 - pos0) < no_irqs) {
260 if (find_valid_pos0(pp, no_irqs, pos1, &pos0))
261 goto no_valid_irq;
262 pos1 = find_next_bit(pp->msi_irq_in_use,
263 MAX_MSI_IRQS, pos0);
264 }
265 }
266
Pratyush Anand904d0e72013-10-09 21:32:12 +0900267 irq = irq_find_mapping(pp->irq_domain, pos0);
268 if (!irq)
Jingoo Hanf342d942013-09-06 15:54:59 +0900269 goto no_valid_irq;
270
Bjørn Erik Nilsenbe3f48c2013-11-29 14:35:24 +0100271 /*
272 * irq_create_mapping (called from dw_pcie_host_init) pre-allocates
273 * descs so there is no need to allocate descs here. We can therefore
274 * assume that if irq_find_mapping above returns non-zero, then the
275 * descs are also successfully allocated.
276 */
277
Bjorn Helgaas0b8cfb62013-12-09 15:11:25 -0700278 for (i = 0; i < no_irqs; i++) {
Bjørn Erik Nilsenbe3f48c2013-11-29 14:35:24 +0100279 if (irq_set_msi_desc_off(irq, i, desc) != 0) {
280 clear_irq_range(pp, irq, i, pos0);
281 goto no_valid_irq;
282 }
Jingoo Hanf342d942013-09-06 15:54:59 +0900283 set_bit(pos0 + i, pp->msi_irq_in_use);
Jingoo Hanf342d942013-09-06 15:54:59 +0900284 /*Enable corresponding interrupt in MSI interrupt controller */
285 res = ((pos0 + i) / 32) * 12;
286 bit = (pos0 + i) % 32;
287 dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, &val);
288 val |= 1 << bit;
289 dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, val);
Jingoo Hanf342d942013-09-06 15:54:59 +0900290 }
291
292 *pos = pos0;
293 return irq;
294
295no_valid_irq:
296 *pos = pos0;
297 return -ENOSPC;
298}
299
300static void clear_irq(unsigned int irq)
301{
Bjørn Erik Nilsenbe3f48c2013-11-29 14:35:24 +0100302 unsigned int pos, nvec;
Jingoo Hanf342d942013-09-06 15:54:59 +0900303 struct msi_desc *msi;
304 struct pcie_port *pp;
Pratyush Anand904d0e72013-10-09 21:32:12 +0900305 struct irq_data *data = irq_get_irq_data(irq);
Jingoo Hanf342d942013-09-06 15:54:59 +0900306
307 /* get the port structure */
Thomas Gleixnerf7bfca62014-02-23 21:40:11 +0000308 msi = irq_data_get_msi(data);
Jingoo Hanf342d942013-09-06 15:54:59 +0900309 pp = sys_to_pcie(msi->dev->bus->sysdata);
310 if (!pp) {
311 BUG();
312 return;
313 }
314
Bjørn Erik Nilsenbe3f48c2013-11-29 14:35:24 +0100315 /* undo what was done in assign_irq */
Pratyush Anand904d0e72013-10-09 21:32:12 +0900316 pos = data->hwirq;
Bjørn Erik Nilsenbe3f48c2013-11-29 14:35:24 +0100317 nvec = 1 << msi->msi_attrib.multiple;
Jingoo Hanf342d942013-09-06 15:54:59 +0900318
Bjørn Erik Nilsenbe3f48c2013-11-29 14:35:24 +0100319 clear_irq_range(pp, irq, nvec, pos);
Jingoo Hanf342d942013-09-06 15:54:59 +0900320
Bjørn Erik Nilsenbe3f48c2013-11-29 14:35:24 +0100321 /* all irqs cleared; reset attributes */
322 msi->irq = 0;
323 msi->msi_attrib.multiple = 0;
Jingoo Hanf342d942013-09-06 15:54:59 +0900324}
325
326static int dw_msi_setup_irq(struct msi_chip *chip, struct pci_dev *pdev,
327 struct msi_desc *desc)
328{
329 int irq, pos, msgvec;
330 u16 msg_ctr;
331 struct msi_msg msg;
332 struct pcie_port *pp = sys_to_pcie(pdev->bus->sysdata);
333
334 if (!pp) {
335 BUG();
336 return -EINVAL;
337 }
338
339 pci_read_config_word(pdev, desc->msi_attrib.pos+PCI_MSI_FLAGS,
340 &msg_ctr);
341 msgvec = (msg_ctr&PCI_MSI_FLAGS_QSIZE) >> 4;
342 if (msgvec == 0)
343 msgvec = (msg_ctr & PCI_MSI_FLAGS_QMASK) >> 1;
344 if (msgvec > 5)
345 msgvec = 0;
346
347 irq = assign_irq((1 << msgvec), desc, &pos);
348 if (irq < 0)
349 return irq;
350
Bjørn Erik Nilsen64989e72013-11-29 14:35:25 +0100351 /*
352 * write_msi_msg() will update PCI_MSI_FLAGS so there is
353 * no need to explicitly call pci_write_config_word().
354 */
Jingoo Hanf342d942013-09-06 15:54:59 +0900355 desc->msi_attrib.multiple = msgvec;
356
357 msg.address_lo = virt_to_phys((void *)pp->msi_data);
358 msg.address_hi = 0x0;
359 msg.data = pos;
360 write_msi_msg(irq, &msg);
361
362 return 0;
363}
364
365static void dw_msi_teardown_irq(struct msi_chip *chip, unsigned int irq)
366{
367 clear_irq(irq);
368}
369
370static struct msi_chip dw_pcie_msi_chip = {
371 .setup_irq = dw_msi_setup_irq,
372 .teardown_irq = dw_msi_teardown_irq,
373};
374
Jingoo Han4b1ced82013-07-31 17:14:10 +0900375int dw_pcie_link_up(struct pcie_port *pp)
Jingoo Han340cba62013-06-21 16:24:54 +0900376{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900377 if (pp->ops->link_up)
378 return pp->ops->link_up(pp);
Jingoo Han340cba62013-06-21 16:24:54 +0900379 else
Jingoo Han340cba62013-06-21 16:24:54 +0900380 return 0;
Jingoo Han340cba62013-06-21 16:24:54 +0900381}
382
Jingoo Hanf342d942013-09-06 15:54:59 +0900383static int dw_pcie_msi_map(struct irq_domain *domain, unsigned int irq,
384 irq_hw_number_t hwirq)
385{
386 irq_set_chip_and_handler(irq, &dw_msi_irq_chip, handle_simple_irq);
387 irq_set_chip_data(irq, domain->host_data);
388 set_irq_flags(irq, IRQF_VALID);
389
390 return 0;
391}
392
393static const struct irq_domain_ops msi_domain_ops = {
394 .map = dw_pcie_msi_map,
395};
396
Jingoo Han4b1ced82013-07-31 17:14:10 +0900397int __init dw_pcie_host_init(struct pcie_port *pp)
Jingoo Han340cba62013-06-21 16:24:54 +0900398{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900399 struct device_node *np = pp->dev->of_node;
Kishon Vijay Abraham I4dd964d2014-07-17 14:30:40 +0530400 struct platform_device *pdev = to_platform_device(pp->dev);
Jingoo Han340cba62013-06-21 16:24:54 +0900401 struct of_pci_range range;
402 struct of_pci_range_parser parser;
Kishon Vijay Abraham I4dd964d2014-07-17 14:30:40 +0530403 struct resource *cfg_res;
Kishon Vijay Abraham If4c55c52014-07-17 14:30:41 +0530404 u32 val, na, ns;
405 const __be32 *addrp;
406 int i, index;
407
408 /* Find the address cell size and the number of cells in order to get
409 * the untranslated address.
410 */
411 of_property_read_u32(np, "#address-cells", &na);
412 ns = of_n_size_cells(np);
Jingoo Hanf342d942013-09-06 15:54:59 +0900413
Kishon Vijay Abraham I4dd964d2014-07-17 14:30:40 +0530414 cfg_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "config");
415 if (cfg_res) {
416 pp->config.cfg0_size = resource_size(cfg_res)/2;
417 pp->config.cfg1_size = resource_size(cfg_res)/2;
418 pp->cfg0_base = cfg_res->start;
419 pp->cfg1_base = cfg_res->start + pp->config.cfg0_size;
Kishon Vijay Abraham If4c55c52014-07-17 14:30:41 +0530420
421 /* Find the untranslated configuration space address */
422 index = of_property_match_string(np, "reg-names", "config");
423 addrp = of_get_address(np, index, false, false);
424 pp->cfg0_mod_base = of_read_number(addrp, ns);
425 pp->cfg1_mod_base = pp->cfg0_mod_base + pp->config.cfg0_size;
Kishon Vijay Abraham I4dd964d2014-07-17 14:30:40 +0530426 } else {
427 dev_err(pp->dev, "missing *config* reg space\n");
428 }
429
Jingoo Han340cba62013-06-21 16:24:54 +0900430 if (of_pci_range_parser_init(&parser, np)) {
Jingoo Han4b1ced82013-07-31 17:14:10 +0900431 dev_err(pp->dev, "missing ranges property\n");
Jingoo Han340cba62013-06-21 16:24:54 +0900432 return -EINVAL;
433 }
434
435 /* Get the I/O and memory ranges from DT */
436 for_each_of_pci_range(&parser, &range) {
437 unsigned long restype = range.flags & IORESOURCE_TYPE_BITS;
438 if (restype == IORESOURCE_IO) {
439 of_pci_range_to_resource(&range, np, &pp->io);
440 pp->io.name = "I/O";
441 pp->io.start = max_t(resource_size_t,
442 PCIBIOS_MIN_IO,
443 range.pci_addr + global_io_offset);
444 pp->io.end = min_t(resource_size_t,
445 IO_SPACE_LIMIT,
446 range.pci_addr + range.size
447 + global_io_offset);
448 pp->config.io_size = resource_size(&pp->io);
449 pp->config.io_bus_addr = range.pci_addr;
Pratyush Anandfce85912013-12-11 15:08:33 +0530450 pp->io_base = range.cpu_addr;
Kishon Vijay Abraham If4c55c52014-07-17 14:30:41 +0530451
452 /* Find the untranslated IO space address */
453 pp->io_mod_base = of_read_number(parser.range -
454 parser.np + na, ns);
Jingoo Han340cba62013-06-21 16:24:54 +0900455 }
456 if (restype == IORESOURCE_MEM) {
457 of_pci_range_to_resource(&range, np, &pp->mem);
458 pp->mem.name = "MEM";
459 pp->config.mem_size = resource_size(&pp->mem);
460 pp->config.mem_bus_addr = range.pci_addr;
Kishon Vijay Abraham If4c55c52014-07-17 14:30:41 +0530461
462 /* Find the untranslated MEM space address */
463 pp->mem_mod_base = of_read_number(parser.range -
464 parser.np + na, ns);
Jingoo Han340cba62013-06-21 16:24:54 +0900465 }
466 if (restype == 0) {
467 of_pci_range_to_resource(&range, np, &pp->cfg);
468 pp->config.cfg0_size = resource_size(&pp->cfg)/2;
469 pp->config.cfg1_size = resource_size(&pp->cfg)/2;
Kishon Vijay Abraham I4dd964d2014-07-17 14:30:40 +0530470 pp->cfg0_base = pp->cfg.start;
471 pp->cfg1_base = pp->cfg.start + pp->config.cfg0_size;
Kishon Vijay Abraham If4c55c52014-07-17 14:30:41 +0530472
473 /* Find the untranslated configuration space address */
474 pp->cfg0_mod_base = of_read_number(parser.range -
475 parser.np + na, ns);
476 pp->cfg1_mod_base = pp->cfg0_mod_base +
477 pp->config.cfg0_size;
Jingoo Han340cba62013-06-21 16:24:54 +0900478 }
479 }
480
Jingoo Han4b1ced82013-07-31 17:14:10 +0900481 if (!pp->dbi_base) {
482 pp->dbi_base = devm_ioremap(pp->dev, pp->cfg.start,
483 resource_size(&pp->cfg));
484 if (!pp->dbi_base) {
485 dev_err(pp->dev, "error with ioremap\n");
486 return -ENOMEM;
487 }
Jingoo Han340cba62013-06-21 16:24:54 +0900488 }
Jingoo Han340cba62013-06-21 16:24:54 +0900489
Jingoo Han4b1ced82013-07-31 17:14:10 +0900490 pp->mem_base = pp->mem.start;
491
492 pp->va_cfg0_base = devm_ioremap(pp->dev, pp->cfg0_base,
493 pp->config.cfg0_size);
494 if (!pp->va_cfg0_base) {
495 dev_err(pp->dev, "error with ioremap in function\n");
496 return -ENOMEM;
Jingoo Han340cba62013-06-21 16:24:54 +0900497 }
Jingoo Han4b1ced82013-07-31 17:14:10 +0900498 pp->va_cfg1_base = devm_ioremap(pp->dev, pp->cfg1_base,
499 pp->config.cfg1_size);
500 if (!pp->va_cfg1_base) {
501 dev_err(pp->dev, "error with ioremap\n");
502 return -ENOMEM;
503 }
Jingoo Han340cba62013-06-21 16:24:54 +0900504
Jingoo Han4b1ced82013-07-31 17:14:10 +0900505 if (of_property_read_u32(np, "num-lanes", &pp->lanes)) {
506 dev_err(pp->dev, "Failed to parse the number of lanes\n");
507 return -EINVAL;
508 }
Jingoo Han340cba62013-06-21 16:24:54 +0900509
Jingoo Hanf342d942013-09-06 15:54:59 +0900510 if (IS_ENABLED(CONFIG_PCI_MSI)) {
Pratyush Anand904d0e72013-10-09 21:32:12 +0900511 pp->irq_domain = irq_domain_add_linear(pp->dev->of_node,
Jingoo Hanf342d942013-09-06 15:54:59 +0900512 MAX_MSI_IRQS, &msi_domain_ops,
513 &dw_pcie_msi_chip);
Pratyush Anand904d0e72013-10-09 21:32:12 +0900514 if (!pp->irq_domain) {
Jingoo Hanf342d942013-09-06 15:54:59 +0900515 dev_err(pp->dev, "irq domain init failed\n");
516 return -ENXIO;
517 }
518
Pratyush Anand904d0e72013-10-09 21:32:12 +0900519 for (i = 0; i < MAX_MSI_IRQS; i++)
520 irq_create_mapping(pp->irq_domain, i);
Jingoo Hanf342d942013-09-06 15:54:59 +0900521 }
522
Jingoo Han4b1ced82013-07-31 17:14:10 +0900523 if (pp->ops->host_init)
524 pp->ops->host_init(pp);
Jingoo Han340cba62013-06-21 16:24:54 +0900525
Jingoo Han4b1ced82013-07-31 17:14:10 +0900526 dw_pcie_wr_own_conf(pp, PCI_BASE_ADDRESS_0, 4, 0);
527
528 /* program correct class for RC */
529 dw_pcie_wr_own_conf(pp, PCI_CLASS_DEVICE, 2, PCI_CLASS_BRIDGE_PCI);
530
531 dw_pcie_rd_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, &val);
532 val |= PORT_LOGIC_SPEED_CHANGE;
533 dw_pcie_wr_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, val);
534
535 dw_pci.nr_controllers = 1;
536 dw_pci.private_data = (void **)&pp;
537
Lucas Stach804f57b2014-03-05 14:25:51 +0100538 pci_common_init_dev(pp->dev, &dw_pci);
Jingoo Han340cba62013-06-21 16:24:54 +0900539 pci_assign_unassigned_resources();
540#ifdef CONFIG_PCI_DOMAINS
Jingoo Han4b1ced82013-07-31 17:14:10 +0900541 dw_pci.domain++;
Jingoo Han340cba62013-06-21 16:24:54 +0900542#endif
543
Jingoo Han340cba62013-06-21 16:24:54 +0900544 return 0;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900545}
Jingoo Han340cba62013-06-21 16:24:54 +0900546
Jingoo Han4b1ced82013-07-31 17:14:10 +0900547static void dw_pcie_prog_viewport_cfg0(struct pcie_port *pp, u32 busdev)
548{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900549 /* Program viewport 0 : OUTBOUND : CFG0 */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900550 dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0,
551 PCIE_ATU_VIEWPORT);
Kishon Vijay Abraham If4c55c52014-07-17 14:30:41 +0530552 dw_pcie_writel_rc(pp, pp->cfg0_mod_base, PCIE_ATU_LOWER_BASE);
553 dw_pcie_writel_rc(pp, (pp->cfg0_mod_base >> 32), PCIE_ATU_UPPER_BASE);
554 dw_pcie_writel_rc(pp, pp->cfg0_mod_base + pp->config.cfg0_size - 1,
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900555 PCIE_ATU_LIMIT);
556 dw_pcie_writel_rc(pp, busdev, PCIE_ATU_LOWER_TARGET);
557 dw_pcie_writel_rc(pp, 0, PCIE_ATU_UPPER_TARGET);
558 dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_CFG0, PCIE_ATU_CR1);
559 dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900560}
561
562static void dw_pcie_prog_viewport_cfg1(struct pcie_port *pp, u32 busdev)
563{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900564 /* Program viewport 1 : OUTBOUND : CFG1 */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900565 dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1,
566 PCIE_ATU_VIEWPORT);
567 dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_CFG1, PCIE_ATU_CR1);
Kishon Vijay Abraham If4c55c52014-07-17 14:30:41 +0530568 dw_pcie_writel_rc(pp, pp->cfg1_mod_base, PCIE_ATU_LOWER_BASE);
569 dw_pcie_writel_rc(pp, (pp->cfg1_mod_base >> 32), PCIE_ATU_UPPER_BASE);
570 dw_pcie_writel_rc(pp, pp->cfg1_mod_base + pp->config.cfg1_size - 1,
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900571 PCIE_ATU_LIMIT);
572 dw_pcie_writel_rc(pp, busdev, PCIE_ATU_LOWER_TARGET);
573 dw_pcie_writel_rc(pp, 0, PCIE_ATU_UPPER_TARGET);
Mohit Kumara19f88b2014-04-14 14:22:55 -0600574 dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900575}
576
577static void dw_pcie_prog_viewport_mem_outbound(struct pcie_port *pp)
578{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900579 /* Program viewport 0 : OUTBOUND : MEM */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900580 dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0,
581 PCIE_ATU_VIEWPORT);
582 dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_MEM, PCIE_ATU_CR1);
Kishon Vijay Abraham If4c55c52014-07-17 14:30:41 +0530583 dw_pcie_writel_rc(pp, pp->mem_mod_base, PCIE_ATU_LOWER_BASE);
584 dw_pcie_writel_rc(pp, (pp->mem_mod_base >> 32), PCIE_ATU_UPPER_BASE);
585 dw_pcie_writel_rc(pp, pp->mem_mod_base + pp->config.mem_size - 1,
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900586 PCIE_ATU_LIMIT);
587 dw_pcie_writel_rc(pp, pp->config.mem_bus_addr, PCIE_ATU_LOWER_TARGET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900588 dw_pcie_writel_rc(pp, upper_32_bits(pp->config.mem_bus_addr),
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900589 PCIE_ATU_UPPER_TARGET);
Mohit Kumara19f88b2014-04-14 14:22:55 -0600590 dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900591}
592
593static void dw_pcie_prog_viewport_io_outbound(struct pcie_port *pp)
594{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900595 /* Program viewport 1 : OUTBOUND : IO */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900596 dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1,
597 PCIE_ATU_VIEWPORT);
598 dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_IO, PCIE_ATU_CR1);
Kishon Vijay Abraham If4c55c52014-07-17 14:30:41 +0530599 dw_pcie_writel_rc(pp, pp->io_mod_base, PCIE_ATU_LOWER_BASE);
600 dw_pcie_writel_rc(pp, (pp->io_mod_base >> 32), PCIE_ATU_UPPER_BASE);
601 dw_pcie_writel_rc(pp, pp->io_mod_base + pp->config.io_size - 1,
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900602 PCIE_ATU_LIMIT);
603 dw_pcie_writel_rc(pp, pp->config.io_bus_addr, PCIE_ATU_LOWER_TARGET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900604 dw_pcie_writel_rc(pp, upper_32_bits(pp->config.io_bus_addr),
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900605 PCIE_ATU_UPPER_TARGET);
Mohit Kumara19f88b2014-04-14 14:22:55 -0600606 dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900607}
608
609static int dw_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus,
610 u32 devfn, int where, int size, u32 *val)
611{
612 int ret = PCIBIOS_SUCCESSFUL;
613 u32 address, busdev;
614
615 busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
616 PCIE_ATU_FUNC(PCI_FUNC(devfn));
617 address = where & ~0x3;
618
619 if (bus->parent->number == pp->root_bus_nr) {
620 dw_pcie_prog_viewport_cfg0(pp, busdev);
Pratyush Ananda01ef592013-12-11 15:08:32 +0530621 ret = dw_pcie_cfg_read(pp->va_cfg0_base + address, where, size,
622 val);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900623 dw_pcie_prog_viewport_mem_outbound(pp);
624 } else {
625 dw_pcie_prog_viewport_cfg1(pp, busdev);
Pratyush Ananda01ef592013-12-11 15:08:32 +0530626 ret = dw_pcie_cfg_read(pp->va_cfg1_base + address, where, size,
627 val);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900628 dw_pcie_prog_viewport_io_outbound(pp);
629 }
630
Jingoo Han340cba62013-06-21 16:24:54 +0900631 return ret;
632}
633
Jingoo Han4b1ced82013-07-31 17:14:10 +0900634static int dw_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus,
635 u32 devfn, int where, int size, u32 val)
Jingoo Han340cba62013-06-21 16:24:54 +0900636{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900637 int ret = PCIBIOS_SUCCESSFUL;
638 u32 address, busdev;
Jingoo Han340cba62013-06-21 16:24:54 +0900639
Jingoo Han4b1ced82013-07-31 17:14:10 +0900640 busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
641 PCIE_ATU_FUNC(PCI_FUNC(devfn));
642 address = where & ~0x3;
Jingoo Han340cba62013-06-21 16:24:54 +0900643
Jingoo Han4b1ced82013-07-31 17:14:10 +0900644 if (bus->parent->number == pp->root_bus_nr) {
645 dw_pcie_prog_viewport_cfg0(pp, busdev);
Pratyush Ananda01ef592013-12-11 15:08:32 +0530646 ret = dw_pcie_cfg_write(pp->va_cfg0_base + address, where, size,
647 val);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900648 dw_pcie_prog_viewport_mem_outbound(pp);
649 } else {
650 dw_pcie_prog_viewport_cfg1(pp, busdev);
Pratyush Ananda01ef592013-12-11 15:08:32 +0530651 ret = dw_pcie_cfg_write(pp->va_cfg1_base + address, where, size,
652 val);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900653 dw_pcie_prog_viewport_io_outbound(pp);
654 }
655
656 return ret;
Jingoo Han340cba62013-06-21 16:24:54 +0900657}
658
Jingoo Han4b1ced82013-07-31 17:14:10 +0900659static int dw_pcie_valid_config(struct pcie_port *pp,
660 struct pci_bus *bus, int dev)
Jingoo Han340cba62013-06-21 16:24:54 +0900661{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900662 /* If there is no link, then there is no device */
663 if (bus->number != pp->root_bus_nr) {
664 if (!dw_pcie_link_up(pp))
665 return 0;
666 }
Jingoo Han340cba62013-06-21 16:24:54 +0900667
Jingoo Han4b1ced82013-07-31 17:14:10 +0900668 /* access only one slot on each root port */
669 if (bus->number == pp->root_bus_nr && dev > 0)
670 return 0;
Jingoo Han340cba62013-06-21 16:24:54 +0900671
672 /*
Jingoo Han4b1ced82013-07-31 17:14:10 +0900673 * do not read more than one device on the bus directly attached
674 * to RC's (Virtual Bridge's) DS side.
Jingoo Han340cba62013-06-21 16:24:54 +0900675 */
Jingoo Han4b1ced82013-07-31 17:14:10 +0900676 if (bus->primary == pp->root_bus_nr && dev > 0)
Jingoo Han340cba62013-06-21 16:24:54 +0900677 return 0;
Jingoo Han340cba62013-06-21 16:24:54 +0900678
679 return 1;
680}
681
Jingoo Han4b1ced82013-07-31 17:14:10 +0900682static int dw_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
683 int size, u32 *val)
Jingoo Han340cba62013-06-21 16:24:54 +0900684{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900685 struct pcie_port *pp = sys_to_pcie(bus->sysdata);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900686 int ret;
Jingoo Han340cba62013-06-21 16:24:54 +0900687
Jingoo Han4b1ced82013-07-31 17:14:10 +0900688 if (!pp) {
689 BUG();
690 return -EINVAL;
691 }
Jingoo Han340cba62013-06-21 16:24:54 +0900692
Jingoo Han4b1ced82013-07-31 17:14:10 +0900693 if (dw_pcie_valid_config(pp, bus, PCI_SLOT(devfn)) == 0) {
694 *val = 0xffffffff;
695 return PCIBIOS_DEVICE_NOT_FOUND;
696 }
697
Jingoo Han4b1ced82013-07-31 17:14:10 +0900698 if (bus->number != pp->root_bus_nr)
Murali Karicheria1c0ae92014-07-21 12:58:41 -0400699 if (pp->ops->rd_other_conf)
700 ret = pp->ops->rd_other_conf(pp, bus, devfn,
701 where, size, val);
702 else
703 ret = dw_pcie_rd_other_conf(pp, bus, devfn,
Jingoo Han4b1ced82013-07-31 17:14:10 +0900704 where, size, val);
705 else
706 ret = dw_pcie_rd_own_conf(pp, where, size, val);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900707
708 return ret;
Jingoo Han340cba62013-06-21 16:24:54 +0900709}
Jingoo Han4b1ced82013-07-31 17:14:10 +0900710
711static int dw_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
712 int where, int size, u32 val)
713{
714 struct pcie_port *pp = sys_to_pcie(bus->sysdata);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900715 int ret;
716
717 if (!pp) {
718 BUG();
719 return -EINVAL;
720 }
721
722 if (dw_pcie_valid_config(pp, bus, PCI_SLOT(devfn)) == 0)
723 return PCIBIOS_DEVICE_NOT_FOUND;
724
Jingoo Han4b1ced82013-07-31 17:14:10 +0900725 if (bus->number != pp->root_bus_nr)
Murali Karicheria1c0ae92014-07-21 12:58:41 -0400726 if (pp->ops->wr_other_conf)
727 ret = pp->ops->wr_other_conf(pp, bus, devfn,
728 where, size, val);
729 else
730 ret = dw_pcie_wr_other_conf(pp, bus, devfn,
Jingoo Han4b1ced82013-07-31 17:14:10 +0900731 where, size, val);
732 else
733 ret = dw_pcie_wr_own_conf(pp, where, size, val);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900734
735 return ret;
736}
737
738static struct pci_ops dw_pcie_ops = {
739 .read = dw_pcie_rd_conf,
740 .write = dw_pcie_wr_conf,
741};
742
Bjorn Helgaas73e40852013-10-09 09:12:37 -0600743static int dw_pcie_setup(int nr, struct pci_sys_data *sys)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900744{
745 struct pcie_port *pp;
746
747 pp = sys_to_pcie(sys);
748
749 if (!pp)
750 return 0;
751
752 if (global_io_offset < SZ_1M && pp->config.io_size > 0) {
753 sys->io_offset = global_io_offset - pp->config.io_bus_addr;
Pratyush Anandfce85912013-12-11 15:08:33 +0530754 pci_ioremap_io(global_io_offset, pp->io_base);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900755 global_io_offset += SZ_64K;
756 pci_add_resource_offset(&sys->resources, &pp->io,
757 sys->io_offset);
758 }
759
760 sys->mem_offset = pp->mem.start - pp->config.mem_bus_addr;
761 pci_add_resource_offset(&sys->resources, &pp->mem, sys->mem_offset);
762
763 return 1;
764}
765
Bjorn Helgaas73e40852013-10-09 09:12:37 -0600766static struct pci_bus *dw_pcie_scan_bus(int nr, struct pci_sys_data *sys)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900767{
768 struct pci_bus *bus;
769 struct pcie_port *pp = sys_to_pcie(sys);
770
771 if (pp) {
772 pp->root_bus_nr = sys->busnr;
Lucas Stach804f57b2014-03-05 14:25:51 +0100773 bus = pci_scan_root_bus(pp->dev, sys->busnr, &dw_pcie_ops,
Jingoo Han4b1ced82013-07-31 17:14:10 +0900774 sys, &sys->resources);
775 } else {
776 bus = NULL;
777 BUG();
778 }
779
780 return bus;
781}
782
Bjorn Helgaas73e40852013-10-09 09:12:37 -0600783static int dw_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900784{
785 struct pcie_port *pp = sys_to_pcie(dev->bus->sysdata);
Lucas Stach804f57b2014-03-05 14:25:51 +0100786 int irq;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900787
Lucas Stach804f57b2014-03-05 14:25:51 +0100788 irq = of_irq_parse_and_map_pci(dev, slot, pin);
789 if (!irq)
790 irq = pp->irq;
791
792 return irq;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900793}
794
Jingoo Hanf342d942013-09-06 15:54:59 +0900795static void dw_pcie_add_bus(struct pci_bus *bus)
796{
797 if (IS_ENABLED(CONFIG_PCI_MSI)) {
798 struct pcie_port *pp = sys_to_pcie(bus->sysdata);
799
800 dw_pcie_msi_chip.dev = pp->dev;
801 bus->msi = &dw_pcie_msi_chip;
802 }
803}
804
Jingoo Han4b1ced82013-07-31 17:14:10 +0900805static struct hw_pci dw_pci = {
806 .setup = dw_pcie_setup,
807 .scan = dw_pcie_scan_bus,
808 .map_irq = dw_pcie_map_irq,
Jingoo Hanf342d942013-09-06 15:54:59 +0900809 .add_bus = dw_pcie_add_bus,
Jingoo Han4b1ced82013-07-31 17:14:10 +0900810};
811
812void dw_pcie_setup_rc(struct pcie_port *pp)
813{
814 struct pcie_port_info *config = &pp->config;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900815 u32 val;
816 u32 membase;
817 u32 memlimit;
818
Mohit Kumar66c5c342014-04-14 14:22:54 -0600819 /* set the number of lanes */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900820 dw_pcie_readl_rc(pp, PCIE_PORT_LINK_CONTROL, &val);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900821 val &= ~PORT_LINK_MODE_MASK;
822 switch (pp->lanes) {
823 case 1:
824 val |= PORT_LINK_MODE_1_LANES;
825 break;
826 case 2:
827 val |= PORT_LINK_MODE_2_LANES;
828 break;
829 case 4:
830 val |= PORT_LINK_MODE_4_LANES;
831 break;
832 }
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900833 dw_pcie_writel_rc(pp, val, PCIE_PORT_LINK_CONTROL);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900834
835 /* set link width speed control register */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900836 dw_pcie_readl_rc(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, &val);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900837 val &= ~PORT_LOGIC_LINK_WIDTH_MASK;
838 switch (pp->lanes) {
839 case 1:
840 val |= PORT_LOGIC_LINK_WIDTH_1_LANES;
841 break;
842 case 2:
843 val |= PORT_LOGIC_LINK_WIDTH_2_LANES;
844 break;
845 case 4:
846 val |= PORT_LOGIC_LINK_WIDTH_4_LANES;
847 break;
848 }
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900849 dw_pcie_writel_rc(pp, val, PCIE_LINK_WIDTH_SPEED_CONTROL);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900850
851 /* setup RC BARs */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900852 dw_pcie_writel_rc(pp, 0x00000004, PCI_BASE_ADDRESS_0);
Mohit Kumardbffdd62014-02-19 17:34:35 +0530853 dw_pcie_writel_rc(pp, 0x00000000, PCI_BASE_ADDRESS_1);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900854
855 /* setup interrupt pins */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900856 dw_pcie_readl_rc(pp, PCI_INTERRUPT_LINE, &val);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900857 val &= 0xffff00ff;
858 val |= 0x00000100;
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900859 dw_pcie_writel_rc(pp, val, PCI_INTERRUPT_LINE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900860
861 /* setup bus numbers */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900862 dw_pcie_readl_rc(pp, PCI_PRIMARY_BUS, &val);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900863 val &= 0xff000000;
864 val |= 0x00010100;
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900865 dw_pcie_writel_rc(pp, val, PCI_PRIMARY_BUS);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900866
867 /* setup memory base, memory limit */
868 membase = ((u32)pp->mem_base & 0xfff00000) >> 16;
869 memlimit = (config->mem_size + (u32)pp->mem_base) & 0xfff00000;
870 val = memlimit | membase;
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900871 dw_pcie_writel_rc(pp, val, PCI_MEMORY_BASE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900872
873 /* setup command register */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900874 dw_pcie_readl_rc(pp, PCI_COMMAND, &val);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900875 val &= 0xffff0000;
876 val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
877 PCI_COMMAND_MASTER | PCI_COMMAND_SERR;
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900878 dw_pcie_writel_rc(pp, val, PCI_COMMAND);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900879}
Jingoo Han340cba62013-06-21 16:24:54 +0900880
881MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
Jingoo Han4b1ced82013-07-31 17:14:10 +0900882MODULE_DESCRIPTION("Designware PCIe host controller driver");
Jingoo Han340cba62013-06-21 16:24:54 +0900883MODULE_LICENSE("GPL v2");