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