blob: 61345a18a6d16ae7bb691665aa9dd2f03a055b51 [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;
Pratyush Anand904d0e72013-10-09 21:32:12 +0900160 int i, pos, irq;
Jingoo Hanf342d942013-09-06 15:54:59 +0900161
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) {
Pratyush Anand904d0e72013-10-09 21:32:12 +0900168 irq = irq_find_mapping(pp->irq_domain,
169 i * 32 + pos);
170 generic_handle_irq(irq);
Jingoo Hanf342d942013-09-06 15:54:59 +0900171 pos++;
172 }
173 }
174 dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_STATUS + i * 12, 4, val);
175 }
176}
177
178void dw_pcie_msi_init(struct pcie_port *pp)
179{
180 pp->msi_data = __get_free_pages(GFP_KERNEL, 0);
181
182 /* program the msi_data */
183 dw_pcie_wr_own_conf(pp, PCIE_MSI_ADDR_LO, 4,
184 virt_to_phys((void *)pp->msi_data));
185 dw_pcie_wr_own_conf(pp, PCIE_MSI_ADDR_HI, 4, 0);
186}
187
188static int find_valid_pos0(struct pcie_port *pp, int msgvec, int pos, int *pos0)
189{
190 int flag = 1;
191
192 do {
193 pos = find_next_zero_bit(pp->msi_irq_in_use,
194 MAX_MSI_IRQS, pos);
195 /*if you have reached to the end then get out from here.*/
196 if (pos == MAX_MSI_IRQS)
197 return -ENOSPC;
198 /*
199 * Check if this position is at correct offset.nvec is always a
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700200 * power of two. pos0 must be nvec bit aligned.
Jingoo Hanf342d942013-09-06 15:54:59 +0900201 */
202 if (pos % msgvec)
203 pos += msgvec - (pos % msgvec);
204 else
205 flag = 0;
206 } while (flag);
207
208 *pos0 = pos;
209 return 0;
210}
211
Bjørn Erik Nilsenbe3f48c2013-11-29 14:35:24 +0100212static void clear_irq_range(struct pcie_port *pp, unsigned int irq_base,
213 unsigned int nvec, unsigned int pos)
214{
215 unsigned int i, res, bit, val;
216
217 i = 0;
218 while (i < nvec) {
219 irq_set_msi_desc_off(irq_base, i, NULL);
220 clear_bit(pos + i, pp->msi_irq_in_use);
221 /* Disable corresponding interrupt on MSI interrupt controller */
222 res = ((pos + i) / 32) * 12;
223 bit = (pos + i) % 32;
224 dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, &val);
225 val &= ~(1 << bit);
226 dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, val);
227 ++i;
228 }
229}
230
Jingoo Hanf342d942013-09-06 15:54:59 +0900231static int assign_irq(int no_irqs, struct msi_desc *desc, int *pos)
232{
233 int res, bit, irq, pos0, pos1, i;
234 u32 val;
235 struct pcie_port *pp = sys_to_pcie(desc->dev->bus->sysdata);
236
237 if (!pp) {
238 BUG();
239 return -EINVAL;
240 }
241
242 pos0 = find_first_zero_bit(pp->msi_irq_in_use,
243 MAX_MSI_IRQS);
244 if (pos0 % no_irqs) {
245 if (find_valid_pos0(pp, no_irqs, pos0, &pos0))
246 goto no_valid_irq;
247 }
248 if (no_irqs > 1) {
249 pos1 = find_next_bit(pp->msi_irq_in_use,
250 MAX_MSI_IRQS, pos0);
251 /* there must be nvec number of consecutive free bits */
252 while ((pos1 - pos0) < no_irqs) {
253 if (find_valid_pos0(pp, no_irqs, pos1, &pos0))
254 goto no_valid_irq;
255 pos1 = find_next_bit(pp->msi_irq_in_use,
256 MAX_MSI_IRQS, pos0);
257 }
258 }
259
Pratyush Anand904d0e72013-10-09 21:32:12 +0900260 irq = irq_find_mapping(pp->irq_domain, pos0);
261 if (!irq)
Jingoo Hanf342d942013-09-06 15:54:59 +0900262 goto no_valid_irq;
263
Bjørn Erik Nilsenbe3f48c2013-11-29 14:35:24 +0100264 /*
265 * irq_create_mapping (called from dw_pcie_host_init) pre-allocates
266 * descs so there is no need to allocate descs here. We can therefore
267 * assume that if irq_find_mapping above returns non-zero, then the
268 * descs are also successfully allocated.
269 */
270
Jingoo Hanf342d942013-09-06 15:54:59 +0900271 i = 0;
272 while (i < no_irqs) {
Bjørn Erik Nilsenbe3f48c2013-11-29 14:35:24 +0100273 if (irq_set_msi_desc_off(irq, i, desc) != 0) {
274 clear_irq_range(pp, irq, i, pos0);
275 goto no_valid_irq;
276 }
Jingoo Hanf342d942013-09-06 15:54:59 +0900277 set_bit(pos0 + i, pp->msi_irq_in_use);
Jingoo Hanf342d942013-09-06 15:54:59 +0900278 /*Enable corresponding interrupt in MSI interrupt controller */
279 res = ((pos0 + i) / 32) * 12;
280 bit = (pos0 + i) % 32;
281 dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, &val);
282 val |= 1 << bit;
283 dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, val);
284 i++;
285 }
286
287 *pos = pos0;
288 return irq;
289
290no_valid_irq:
291 *pos = pos0;
292 return -ENOSPC;
293}
294
295static void clear_irq(unsigned int irq)
296{
Bjørn Erik Nilsenbe3f48c2013-11-29 14:35:24 +0100297 unsigned int pos, nvec;
Jingoo Hanf342d942013-09-06 15:54:59 +0900298 struct irq_desc *desc;
299 struct msi_desc *msi;
300 struct pcie_port *pp;
Pratyush Anand904d0e72013-10-09 21:32:12 +0900301 struct irq_data *data = irq_get_irq_data(irq);
Jingoo Hanf342d942013-09-06 15:54:59 +0900302
303 /* get the port structure */
304 desc = irq_to_desc(irq);
305 msi = irq_desc_get_msi_desc(desc);
306 pp = sys_to_pcie(msi->dev->bus->sysdata);
307 if (!pp) {
308 BUG();
309 return;
310 }
311
Bjørn Erik Nilsenbe3f48c2013-11-29 14:35:24 +0100312 /* undo what was done in assign_irq */
Pratyush Anand904d0e72013-10-09 21:32:12 +0900313 pos = data->hwirq;
Bjørn Erik Nilsenbe3f48c2013-11-29 14:35:24 +0100314 nvec = 1 << msi->msi_attrib.multiple;
Jingoo Hanf342d942013-09-06 15:54:59 +0900315
Bjørn Erik Nilsenbe3f48c2013-11-29 14:35:24 +0100316 clear_irq_range(pp, irq, nvec, pos);
Jingoo Hanf342d942013-09-06 15:54:59 +0900317
Bjørn Erik Nilsenbe3f48c2013-11-29 14:35:24 +0100318 /* all irqs cleared; reset attributes */
319 msi->irq = 0;
320 msi->msi_attrib.multiple = 0;
Jingoo Hanf342d942013-09-06 15:54:59 +0900321}
322
323static int dw_msi_setup_irq(struct msi_chip *chip, struct pci_dev *pdev,
324 struct msi_desc *desc)
325{
326 int irq, pos, msgvec;
327 u16 msg_ctr;
328 struct msi_msg msg;
329 struct pcie_port *pp = sys_to_pcie(pdev->bus->sysdata);
330
331 if (!pp) {
332 BUG();
333 return -EINVAL;
334 }
335
336 pci_read_config_word(pdev, desc->msi_attrib.pos+PCI_MSI_FLAGS,
337 &msg_ctr);
338 msgvec = (msg_ctr&PCI_MSI_FLAGS_QSIZE) >> 4;
339 if (msgvec == 0)
340 msgvec = (msg_ctr & PCI_MSI_FLAGS_QMASK) >> 1;
341 if (msgvec > 5)
342 msgvec = 0;
343
344 irq = assign_irq((1 << msgvec), desc, &pos);
345 if (irq < 0)
346 return irq;
347
348 msg_ctr &= ~PCI_MSI_FLAGS_QSIZE;
349 msg_ctr |= msgvec << 4;
350 pci_write_config_word(pdev, desc->msi_attrib.pos + PCI_MSI_FLAGS,
351 msg_ctr);
352 desc->msi_attrib.multiple = msgvec;
353
354 msg.address_lo = virt_to_phys((void *)pp->msi_data);
355 msg.address_hi = 0x0;
356 msg.data = pos;
357 write_msi_msg(irq, &msg);
358
359 return 0;
360}
361
362static void dw_msi_teardown_irq(struct msi_chip *chip, unsigned int irq)
363{
364 clear_irq(irq);
365}
366
367static struct msi_chip dw_pcie_msi_chip = {
368 .setup_irq = dw_msi_setup_irq,
369 .teardown_irq = dw_msi_teardown_irq,
370};
371
Jingoo Han4b1ced82013-07-31 17:14:10 +0900372int dw_pcie_link_up(struct pcie_port *pp)
Jingoo Han340cba62013-06-21 16:24:54 +0900373{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900374 if (pp->ops->link_up)
375 return pp->ops->link_up(pp);
Jingoo Han340cba62013-06-21 16:24:54 +0900376 else
Jingoo Han340cba62013-06-21 16:24:54 +0900377 return 0;
Jingoo Han340cba62013-06-21 16:24:54 +0900378}
379
Jingoo Hanf342d942013-09-06 15:54:59 +0900380static int dw_pcie_msi_map(struct irq_domain *domain, unsigned int irq,
381 irq_hw_number_t hwirq)
382{
383 irq_set_chip_and_handler(irq, &dw_msi_irq_chip, handle_simple_irq);
384 irq_set_chip_data(irq, domain->host_data);
385 set_irq_flags(irq, IRQF_VALID);
386
387 return 0;
388}
389
390static const struct irq_domain_ops msi_domain_ops = {
391 .map = dw_pcie_msi_map,
392};
393
Jingoo Han4b1ced82013-07-31 17:14:10 +0900394int __init dw_pcie_host_init(struct pcie_port *pp)
Jingoo Han340cba62013-06-21 16:24:54 +0900395{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900396 struct device_node *np = pp->dev->of_node;
Jingoo Han340cba62013-06-21 16:24:54 +0900397 struct of_pci_range range;
398 struct of_pci_range_parser parser;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900399 u32 val;
Pratyush Anand904d0e72013-10-09 21:32:12 +0900400 int i;
Jingoo Hanf342d942013-09-06 15:54:59 +0900401
Jingoo Han340cba62013-06-21 16:24:54 +0900402 if (of_pci_range_parser_init(&parser, np)) {
Jingoo Han4b1ced82013-07-31 17:14:10 +0900403 dev_err(pp->dev, "missing ranges property\n");
Jingoo Han340cba62013-06-21 16:24:54 +0900404 return -EINVAL;
405 }
406
407 /* Get the I/O and memory ranges from DT */
408 for_each_of_pci_range(&parser, &range) {
409 unsigned long restype = range.flags & IORESOURCE_TYPE_BITS;
410 if (restype == IORESOURCE_IO) {
411 of_pci_range_to_resource(&range, np, &pp->io);
412 pp->io.name = "I/O";
413 pp->io.start = max_t(resource_size_t,
414 PCIBIOS_MIN_IO,
415 range.pci_addr + global_io_offset);
416 pp->io.end = min_t(resource_size_t,
417 IO_SPACE_LIMIT,
418 range.pci_addr + range.size
419 + global_io_offset);
420 pp->config.io_size = resource_size(&pp->io);
421 pp->config.io_bus_addr = range.pci_addr;
422 }
423 if (restype == IORESOURCE_MEM) {
424 of_pci_range_to_resource(&range, np, &pp->mem);
425 pp->mem.name = "MEM";
426 pp->config.mem_size = resource_size(&pp->mem);
427 pp->config.mem_bus_addr = range.pci_addr;
428 }
429 if (restype == 0) {
430 of_pci_range_to_resource(&range, np, &pp->cfg);
431 pp->config.cfg0_size = resource_size(&pp->cfg)/2;
432 pp->config.cfg1_size = resource_size(&pp->cfg)/2;
433 }
434 }
435
Jingoo Han4b1ced82013-07-31 17:14:10 +0900436 if (!pp->dbi_base) {
437 pp->dbi_base = devm_ioremap(pp->dev, pp->cfg.start,
438 resource_size(&pp->cfg));
439 if (!pp->dbi_base) {
440 dev_err(pp->dev, "error with ioremap\n");
441 return -ENOMEM;
442 }
Jingoo Han340cba62013-06-21 16:24:54 +0900443 }
Jingoo Han340cba62013-06-21 16:24:54 +0900444
Jingoo Han4b1ced82013-07-31 17:14:10 +0900445 pp->cfg0_base = pp->cfg.start;
446 pp->cfg1_base = pp->cfg.start + pp->config.cfg0_size;
447 pp->io_base = pp->io.start;
448 pp->mem_base = pp->mem.start;
449
450 pp->va_cfg0_base = devm_ioremap(pp->dev, pp->cfg0_base,
451 pp->config.cfg0_size);
452 if (!pp->va_cfg0_base) {
453 dev_err(pp->dev, "error with ioremap in function\n");
454 return -ENOMEM;
Jingoo Han340cba62013-06-21 16:24:54 +0900455 }
Jingoo Han4b1ced82013-07-31 17:14:10 +0900456 pp->va_cfg1_base = devm_ioremap(pp->dev, pp->cfg1_base,
457 pp->config.cfg1_size);
458 if (!pp->va_cfg1_base) {
459 dev_err(pp->dev, "error with ioremap\n");
460 return -ENOMEM;
461 }
Jingoo Han340cba62013-06-21 16:24:54 +0900462
Jingoo Han4b1ced82013-07-31 17:14:10 +0900463 if (of_property_read_u32(np, "num-lanes", &pp->lanes)) {
464 dev_err(pp->dev, "Failed to parse the number of lanes\n");
465 return -EINVAL;
466 }
Jingoo Han340cba62013-06-21 16:24:54 +0900467
Jingoo Hanf342d942013-09-06 15:54:59 +0900468 if (IS_ENABLED(CONFIG_PCI_MSI)) {
Pratyush Anand904d0e72013-10-09 21:32:12 +0900469 pp->irq_domain = irq_domain_add_linear(pp->dev->of_node,
Jingoo Hanf342d942013-09-06 15:54:59 +0900470 MAX_MSI_IRQS, &msi_domain_ops,
471 &dw_pcie_msi_chip);
Pratyush Anand904d0e72013-10-09 21:32:12 +0900472 if (!pp->irq_domain) {
Jingoo Hanf342d942013-09-06 15:54:59 +0900473 dev_err(pp->dev, "irq domain init failed\n");
474 return -ENXIO;
475 }
476
Pratyush Anand904d0e72013-10-09 21:32:12 +0900477 for (i = 0; i < MAX_MSI_IRQS; i++)
478 irq_create_mapping(pp->irq_domain, i);
Jingoo Hanf342d942013-09-06 15:54:59 +0900479 }
480
Jingoo Han4b1ced82013-07-31 17:14:10 +0900481 if (pp->ops->host_init)
482 pp->ops->host_init(pp);
Jingoo Han340cba62013-06-21 16:24:54 +0900483
Jingoo Han4b1ced82013-07-31 17:14:10 +0900484 dw_pcie_wr_own_conf(pp, PCI_BASE_ADDRESS_0, 4, 0);
485
486 /* program correct class for RC */
487 dw_pcie_wr_own_conf(pp, PCI_CLASS_DEVICE, 2, PCI_CLASS_BRIDGE_PCI);
488
489 dw_pcie_rd_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, &val);
490 val |= PORT_LOGIC_SPEED_CHANGE;
491 dw_pcie_wr_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, val);
492
493 dw_pci.nr_controllers = 1;
494 dw_pci.private_data = (void **)&pp;
495
496 pci_common_init(&dw_pci);
Jingoo Han340cba62013-06-21 16:24:54 +0900497 pci_assign_unassigned_resources();
498#ifdef CONFIG_PCI_DOMAINS
Jingoo Han4b1ced82013-07-31 17:14:10 +0900499 dw_pci.domain++;
Jingoo Han340cba62013-06-21 16:24:54 +0900500#endif
501
Jingoo Han340cba62013-06-21 16:24:54 +0900502 return 0;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900503}
Jingoo Han340cba62013-06-21 16:24:54 +0900504
Jingoo Han4b1ced82013-07-31 17:14:10 +0900505static void dw_pcie_prog_viewport_cfg0(struct pcie_port *pp, u32 busdev)
506{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900507 /* Program viewport 0 : OUTBOUND : CFG0 */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900508 dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0,
509 PCIE_ATU_VIEWPORT);
510 dw_pcie_writel_rc(pp, pp->cfg0_base, PCIE_ATU_LOWER_BASE);
511 dw_pcie_writel_rc(pp, (pp->cfg0_base >> 32), PCIE_ATU_UPPER_BASE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900512 dw_pcie_writel_rc(pp, pp->cfg0_base + pp->config.cfg0_size - 1,
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900513 PCIE_ATU_LIMIT);
514 dw_pcie_writel_rc(pp, busdev, PCIE_ATU_LOWER_TARGET);
515 dw_pcie_writel_rc(pp, 0, PCIE_ATU_UPPER_TARGET);
516 dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_CFG0, PCIE_ATU_CR1);
517 dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900518}
519
520static void dw_pcie_prog_viewport_cfg1(struct pcie_port *pp, u32 busdev)
521{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900522 /* Program viewport 1 : OUTBOUND : CFG1 */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900523 dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1,
524 PCIE_ATU_VIEWPORT);
525 dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_CFG1, PCIE_ATU_CR1);
526 dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2);
527 dw_pcie_writel_rc(pp, pp->cfg1_base, PCIE_ATU_LOWER_BASE);
528 dw_pcie_writel_rc(pp, (pp->cfg1_base >> 32), PCIE_ATU_UPPER_BASE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900529 dw_pcie_writel_rc(pp, pp->cfg1_base + pp->config.cfg1_size - 1,
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900530 PCIE_ATU_LIMIT);
531 dw_pcie_writel_rc(pp, busdev, PCIE_ATU_LOWER_TARGET);
532 dw_pcie_writel_rc(pp, 0, PCIE_ATU_UPPER_TARGET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900533}
534
535static void dw_pcie_prog_viewport_mem_outbound(struct pcie_port *pp)
536{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900537 /* Program viewport 0 : OUTBOUND : MEM */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900538 dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0,
539 PCIE_ATU_VIEWPORT);
540 dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_MEM, PCIE_ATU_CR1);
541 dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2);
542 dw_pcie_writel_rc(pp, pp->mem_base, PCIE_ATU_LOWER_BASE);
543 dw_pcie_writel_rc(pp, (pp->mem_base >> 32), PCIE_ATU_UPPER_BASE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900544 dw_pcie_writel_rc(pp, pp->mem_base + pp->config.mem_size - 1,
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900545 PCIE_ATU_LIMIT);
546 dw_pcie_writel_rc(pp, pp->config.mem_bus_addr, PCIE_ATU_LOWER_TARGET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900547 dw_pcie_writel_rc(pp, upper_32_bits(pp->config.mem_bus_addr),
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900548 PCIE_ATU_UPPER_TARGET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900549}
550
551static void dw_pcie_prog_viewport_io_outbound(struct pcie_port *pp)
552{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900553 /* Program viewport 1 : OUTBOUND : IO */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900554 dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1,
555 PCIE_ATU_VIEWPORT);
556 dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_IO, PCIE_ATU_CR1);
557 dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2);
558 dw_pcie_writel_rc(pp, pp->io_base, PCIE_ATU_LOWER_BASE);
559 dw_pcie_writel_rc(pp, (pp->io_base >> 32), PCIE_ATU_UPPER_BASE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900560 dw_pcie_writel_rc(pp, pp->io_base + pp->config.io_size - 1,
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900561 PCIE_ATU_LIMIT);
562 dw_pcie_writel_rc(pp, pp->config.io_bus_addr, PCIE_ATU_LOWER_TARGET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900563 dw_pcie_writel_rc(pp, upper_32_bits(pp->config.io_bus_addr),
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900564 PCIE_ATU_UPPER_TARGET);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900565}
566
567static int dw_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus,
568 u32 devfn, int where, int size, u32 *val)
569{
570 int ret = PCIBIOS_SUCCESSFUL;
571 u32 address, busdev;
572
573 busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
574 PCIE_ATU_FUNC(PCI_FUNC(devfn));
575 address = where & ~0x3;
576
577 if (bus->parent->number == pp->root_bus_nr) {
578 dw_pcie_prog_viewport_cfg0(pp, busdev);
579 ret = cfg_read(pp->va_cfg0_base + address, where, size, val);
580 dw_pcie_prog_viewport_mem_outbound(pp);
581 } else {
582 dw_pcie_prog_viewport_cfg1(pp, busdev);
583 ret = cfg_read(pp->va_cfg1_base + address, where, size, val);
584 dw_pcie_prog_viewport_io_outbound(pp);
585 }
586
Jingoo Han340cba62013-06-21 16:24:54 +0900587 return ret;
588}
589
Jingoo Han4b1ced82013-07-31 17:14:10 +0900590static int dw_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus,
591 u32 devfn, int where, int size, u32 val)
Jingoo Han340cba62013-06-21 16:24:54 +0900592{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900593 int ret = PCIBIOS_SUCCESSFUL;
594 u32 address, busdev;
Jingoo Han340cba62013-06-21 16:24:54 +0900595
Jingoo Han4b1ced82013-07-31 17:14:10 +0900596 busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
597 PCIE_ATU_FUNC(PCI_FUNC(devfn));
598 address = where & ~0x3;
Jingoo Han340cba62013-06-21 16:24:54 +0900599
Jingoo Han4b1ced82013-07-31 17:14:10 +0900600 if (bus->parent->number == pp->root_bus_nr) {
601 dw_pcie_prog_viewport_cfg0(pp, busdev);
602 ret = cfg_write(pp->va_cfg0_base + address, where, size, val);
603 dw_pcie_prog_viewport_mem_outbound(pp);
604 } else {
605 dw_pcie_prog_viewport_cfg1(pp, busdev);
606 ret = cfg_write(pp->va_cfg1_base + address, where, size, val);
607 dw_pcie_prog_viewport_io_outbound(pp);
608 }
609
610 return ret;
Jingoo Han340cba62013-06-21 16:24:54 +0900611}
612
Jingoo Han340cba62013-06-21 16:24:54 +0900613
Jingoo Han4b1ced82013-07-31 17:14:10 +0900614static int dw_pcie_valid_config(struct pcie_port *pp,
615 struct pci_bus *bus, int dev)
Jingoo Han340cba62013-06-21 16:24:54 +0900616{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900617 /* If there is no link, then there is no device */
618 if (bus->number != pp->root_bus_nr) {
619 if (!dw_pcie_link_up(pp))
620 return 0;
621 }
Jingoo Han340cba62013-06-21 16:24:54 +0900622
Jingoo Han4b1ced82013-07-31 17:14:10 +0900623 /* access only one slot on each root port */
624 if (bus->number == pp->root_bus_nr && dev > 0)
625 return 0;
Jingoo Han340cba62013-06-21 16:24:54 +0900626
627 /*
Jingoo Han4b1ced82013-07-31 17:14:10 +0900628 * do not read more than one device on the bus directly attached
629 * to RC's (Virtual Bridge's) DS side.
Jingoo Han340cba62013-06-21 16:24:54 +0900630 */
Jingoo Han4b1ced82013-07-31 17:14:10 +0900631 if (bus->primary == pp->root_bus_nr && dev > 0)
Jingoo Han340cba62013-06-21 16:24:54 +0900632 return 0;
Jingoo Han340cba62013-06-21 16:24:54 +0900633
634 return 1;
635}
636
Jingoo Han4b1ced82013-07-31 17:14:10 +0900637static int dw_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
638 int size, u32 *val)
Jingoo Han340cba62013-06-21 16:24:54 +0900639{
Jingoo Han4b1ced82013-07-31 17:14:10 +0900640 struct pcie_port *pp = sys_to_pcie(bus->sysdata);
641 unsigned long flags;
642 int ret;
Jingoo Han340cba62013-06-21 16:24:54 +0900643
Jingoo Han4b1ced82013-07-31 17:14:10 +0900644 if (!pp) {
645 BUG();
646 return -EINVAL;
647 }
Jingoo Han340cba62013-06-21 16:24:54 +0900648
Jingoo Han4b1ced82013-07-31 17:14:10 +0900649 if (dw_pcie_valid_config(pp, bus, PCI_SLOT(devfn)) == 0) {
650 *val = 0xffffffff;
651 return PCIBIOS_DEVICE_NOT_FOUND;
652 }
653
654 spin_lock_irqsave(&pp->conf_lock, flags);
655 if (bus->number != pp->root_bus_nr)
656 ret = dw_pcie_rd_other_conf(pp, bus, devfn,
657 where, size, val);
658 else
659 ret = dw_pcie_rd_own_conf(pp, where, size, val);
660 spin_unlock_irqrestore(&pp->conf_lock, flags);
661
662 return ret;
Jingoo Han340cba62013-06-21 16:24:54 +0900663}
Jingoo Han4b1ced82013-07-31 17:14:10 +0900664
665static int dw_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
666 int where, int size, u32 val)
667{
668 struct pcie_port *pp = sys_to_pcie(bus->sysdata);
669 unsigned long flags;
670 int ret;
671
672 if (!pp) {
673 BUG();
674 return -EINVAL;
675 }
676
677 if (dw_pcie_valid_config(pp, bus, PCI_SLOT(devfn)) == 0)
678 return PCIBIOS_DEVICE_NOT_FOUND;
679
680 spin_lock_irqsave(&pp->conf_lock, flags);
681 if (bus->number != pp->root_bus_nr)
682 ret = dw_pcie_wr_other_conf(pp, bus, devfn,
683 where, size, val);
684 else
685 ret = dw_pcie_wr_own_conf(pp, where, size, val);
686 spin_unlock_irqrestore(&pp->conf_lock, flags);
687
688 return ret;
689}
690
691static struct pci_ops dw_pcie_ops = {
692 .read = dw_pcie_rd_conf,
693 .write = dw_pcie_wr_conf,
694};
695
Bjorn Helgaas73e40852013-10-09 09:12:37 -0600696static int dw_pcie_setup(int nr, struct pci_sys_data *sys)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900697{
698 struct pcie_port *pp;
699
700 pp = sys_to_pcie(sys);
701
702 if (!pp)
703 return 0;
704
705 if (global_io_offset < SZ_1M && pp->config.io_size > 0) {
706 sys->io_offset = global_io_offset - pp->config.io_bus_addr;
707 pci_ioremap_io(sys->io_offset, pp->io.start);
708 global_io_offset += SZ_64K;
709 pci_add_resource_offset(&sys->resources, &pp->io,
710 sys->io_offset);
711 }
712
713 sys->mem_offset = pp->mem.start - pp->config.mem_bus_addr;
714 pci_add_resource_offset(&sys->resources, &pp->mem, sys->mem_offset);
715
716 return 1;
717}
718
Bjorn Helgaas73e40852013-10-09 09:12:37 -0600719static struct pci_bus *dw_pcie_scan_bus(int nr, struct pci_sys_data *sys)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900720{
721 struct pci_bus *bus;
722 struct pcie_port *pp = sys_to_pcie(sys);
723
724 if (pp) {
725 pp->root_bus_nr = sys->busnr;
726 bus = pci_scan_root_bus(NULL, sys->busnr, &dw_pcie_ops,
727 sys, &sys->resources);
728 } else {
729 bus = NULL;
730 BUG();
731 }
732
733 return bus;
734}
735
Bjorn Helgaas73e40852013-10-09 09:12:37 -0600736static int dw_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
Jingoo Han4b1ced82013-07-31 17:14:10 +0900737{
738 struct pcie_port *pp = sys_to_pcie(dev->bus->sysdata);
739
740 return pp->irq;
741}
742
Jingoo Hanf342d942013-09-06 15:54:59 +0900743static void dw_pcie_add_bus(struct pci_bus *bus)
744{
745 if (IS_ENABLED(CONFIG_PCI_MSI)) {
746 struct pcie_port *pp = sys_to_pcie(bus->sysdata);
747
748 dw_pcie_msi_chip.dev = pp->dev;
749 bus->msi = &dw_pcie_msi_chip;
750 }
751}
752
Jingoo Han4b1ced82013-07-31 17:14:10 +0900753static struct hw_pci dw_pci = {
754 .setup = dw_pcie_setup,
755 .scan = dw_pcie_scan_bus,
756 .map_irq = dw_pcie_map_irq,
Jingoo Hanf342d942013-09-06 15:54:59 +0900757 .add_bus = dw_pcie_add_bus,
Jingoo Han4b1ced82013-07-31 17:14:10 +0900758};
759
760void dw_pcie_setup_rc(struct pcie_port *pp)
761{
762 struct pcie_port_info *config = &pp->config;
Jingoo Han4b1ced82013-07-31 17:14:10 +0900763 u32 val;
764 u32 membase;
765 u32 memlimit;
766
767 /* set the number of lines as 4 */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900768 dw_pcie_readl_rc(pp, PCIE_PORT_LINK_CONTROL, &val);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900769 val &= ~PORT_LINK_MODE_MASK;
770 switch (pp->lanes) {
771 case 1:
772 val |= PORT_LINK_MODE_1_LANES;
773 break;
774 case 2:
775 val |= PORT_LINK_MODE_2_LANES;
776 break;
777 case 4:
778 val |= PORT_LINK_MODE_4_LANES;
779 break;
780 }
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900781 dw_pcie_writel_rc(pp, val, PCIE_PORT_LINK_CONTROL);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900782
783 /* set link width speed control register */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900784 dw_pcie_readl_rc(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, &val);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900785 val &= ~PORT_LOGIC_LINK_WIDTH_MASK;
786 switch (pp->lanes) {
787 case 1:
788 val |= PORT_LOGIC_LINK_WIDTH_1_LANES;
789 break;
790 case 2:
791 val |= PORT_LOGIC_LINK_WIDTH_2_LANES;
792 break;
793 case 4:
794 val |= PORT_LOGIC_LINK_WIDTH_4_LANES;
795 break;
796 }
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900797 dw_pcie_writel_rc(pp, val, PCIE_LINK_WIDTH_SPEED_CONTROL);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900798
799 /* setup RC BARs */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900800 dw_pcie_writel_rc(pp, 0x00000004, PCI_BASE_ADDRESS_0);
801 dw_pcie_writel_rc(pp, 0x00000004, PCI_BASE_ADDRESS_1);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900802
803 /* setup interrupt pins */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900804 dw_pcie_readl_rc(pp, PCI_INTERRUPT_LINE, &val);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900805 val &= 0xffff00ff;
806 val |= 0x00000100;
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900807 dw_pcie_writel_rc(pp, val, PCI_INTERRUPT_LINE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900808
809 /* setup bus numbers */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900810 dw_pcie_readl_rc(pp, PCI_PRIMARY_BUS, &val);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900811 val &= 0xff000000;
812 val |= 0x00010100;
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900813 dw_pcie_writel_rc(pp, val, PCI_PRIMARY_BUS);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900814
815 /* setup memory base, memory limit */
816 membase = ((u32)pp->mem_base & 0xfff00000) >> 16;
817 memlimit = (config->mem_size + (u32)pp->mem_base) & 0xfff00000;
818 val = memlimit | membase;
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900819 dw_pcie_writel_rc(pp, val, PCI_MEMORY_BASE);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900820
821 /* setup command register */
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900822 dw_pcie_readl_rc(pp, PCI_COMMAND, &val);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900823 val &= 0xffff0000;
824 val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
825 PCI_COMMAND_MASTER | PCI_COMMAND_SERR;
Seungwon Jeonf7b78682013-08-28 20:53:30 +0900826 dw_pcie_writel_rc(pp, val, PCI_COMMAND);
Jingoo Han4b1ced82013-07-31 17:14:10 +0900827}
Jingoo Han340cba62013-06-21 16:24:54 +0900828
829MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
Jingoo Han4b1ced82013-07-31 17:14:10 +0900830MODULE_DESCRIPTION("Designware PCIe host controller driver");
Jingoo Han340cba62013-06-21 16:24:54 +0900831MODULE_LICENSE("GPL v2");