Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / pci / host / pci-ftpci100.c
blobb9617d1c1d48be639eb0d486ee6b2b80011d7294
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Support for Faraday Technology FTPC100 PCI Controller
5 * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
7 * Based on the out-of-tree OpenWRT patch for Cortina Gemini:
8 * Copyright (C) 2009 Janos Laube <janos.dev@gmail.com>
9 * Copyright (C) 2009 Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
10 * Based on SL2312 PCI controller code
11 * Storlink (C) 2003
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/io.h>
17 #include <linux/kernel.h>
18 #include <linux/of_address.h>
19 #include <linux/of_device.h>
20 #include <linux/of_irq.h>
21 #include <linux/of_pci.h>
22 #include <linux/pci.h>
23 #include <linux/platform_device.h>
24 #include <linux/slab.h>
25 #include <linux/irqdomain.h>
26 #include <linux/irqchip/chained_irq.h>
27 #include <linux/bitops.h>
28 #include <linux/irq.h>
29 #include <linux/clk.h>
32 * Special configuration registers directly in the first few words
33 * in I/O space.
35 #define PCI_IOSIZE 0x00
36 #define PCI_PROT 0x04 /* AHB protection */
37 #define PCI_CTRL 0x08 /* PCI control signal */
38 #define PCI_SOFTRST 0x10 /* Soft reset counter and response error enable */
39 #define PCI_CONFIG 0x28 /* PCI configuration command register */
40 #define PCI_DATA 0x2C
42 #define FARADAY_PCI_STATUS_CMD 0x04 /* Status and command */
43 #define FARADAY_PCI_PMC 0x40 /* Power management control */
44 #define FARADAY_PCI_PMCSR 0x44 /* Power management status */
45 #define FARADAY_PCI_CTRL1 0x48 /* Control register 1 */
46 #define FARADAY_PCI_CTRL2 0x4C /* Control register 2 */
47 #define FARADAY_PCI_MEM1_BASE_SIZE 0x50 /* Memory base and size #1 */
48 #define FARADAY_PCI_MEM2_BASE_SIZE 0x54 /* Memory base and size #2 */
49 #define FARADAY_PCI_MEM3_BASE_SIZE 0x58 /* Memory base and size #3 */
51 #define PCI_STATUS_66MHZ_CAPABLE BIT(21)
53 /* Bits 31..28 gives INTD..INTA status */
54 #define PCI_CTRL2_INTSTS_SHIFT 28
55 #define PCI_CTRL2_INTMASK_CMDERR BIT(27)
56 #define PCI_CTRL2_INTMASK_PARERR BIT(26)
57 /* Bits 25..22 masks INTD..INTA */
58 #define PCI_CTRL2_INTMASK_SHIFT 22
59 #define PCI_CTRL2_INTMASK_MABRT_RX BIT(21)
60 #define PCI_CTRL2_INTMASK_TABRT_RX BIT(20)
61 #define PCI_CTRL2_INTMASK_TABRT_TX BIT(19)
62 #define PCI_CTRL2_INTMASK_RETRY4 BIT(18)
63 #define PCI_CTRL2_INTMASK_SERR_RX BIT(17)
64 #define PCI_CTRL2_INTMASK_PERR_RX BIT(16)
65 /* Bit 15 reserved */
66 #define PCI_CTRL2_MSTPRI_REQ6 BIT(14)
67 #define PCI_CTRL2_MSTPRI_REQ5 BIT(13)
68 #define PCI_CTRL2_MSTPRI_REQ4 BIT(12)
69 #define PCI_CTRL2_MSTPRI_REQ3 BIT(11)
70 #define PCI_CTRL2_MSTPRI_REQ2 BIT(10)
71 #define PCI_CTRL2_MSTPRI_REQ1 BIT(9)
72 #define PCI_CTRL2_MSTPRI_REQ0 BIT(8)
73 /* Bits 7..4 reserved */
74 /* Bits 3..0 TRDYW */
77 * Memory configs:
78 * Bit 31..20 defines the PCI side memory base
79 * Bit 19..16 (4 bits) defines the size per below
81 #define FARADAY_PCI_MEMBASE_MASK 0xfff00000
82 #define FARADAY_PCI_MEMSIZE_1MB 0x0
83 #define FARADAY_PCI_MEMSIZE_2MB 0x1
84 #define FARADAY_PCI_MEMSIZE_4MB 0x2
85 #define FARADAY_PCI_MEMSIZE_8MB 0x3
86 #define FARADAY_PCI_MEMSIZE_16MB 0x4
87 #define FARADAY_PCI_MEMSIZE_32MB 0x5
88 #define FARADAY_PCI_MEMSIZE_64MB 0x6
89 #define FARADAY_PCI_MEMSIZE_128MB 0x7
90 #define FARADAY_PCI_MEMSIZE_256MB 0x8
91 #define FARADAY_PCI_MEMSIZE_512MB 0x9
92 #define FARADAY_PCI_MEMSIZE_1GB 0xa
93 #define FARADAY_PCI_MEMSIZE_2GB 0xb
94 #define FARADAY_PCI_MEMSIZE_SHIFT 16
97 * The DMA base is set to 0x0 for all memory segments, it reflects the
98 * fact that the memory of the host system starts at 0x0.
100 #define FARADAY_PCI_DMA_MEM1_BASE 0x00000000
101 #define FARADAY_PCI_DMA_MEM2_BASE 0x00000000
102 #define FARADAY_PCI_DMA_MEM3_BASE 0x00000000
104 /* Defines for PCI configuration command register */
105 #define PCI_CONF_ENABLE BIT(31)
106 #define PCI_CONF_WHERE(r) ((r) & 0xFC)
107 #define PCI_CONF_BUS(b) (((b) & 0xFF) << 16)
108 #define PCI_CONF_DEVICE(d) (((d) & 0x1F) << 11)
109 #define PCI_CONF_FUNCTION(f) (((f) & 0x07) << 8)
112 * struct faraday_pci_variant - encodes IP block differences
113 * @cascaded_irq: this host has cascaded IRQs from an interrupt controller
114 * embedded in the host bridge.
116 struct faraday_pci_variant {
117 bool cascaded_irq;
120 struct faraday_pci {
121 struct device *dev;
122 void __iomem *base;
123 struct irq_domain *irqdomain;
124 struct pci_bus *bus;
125 struct clk *bus_clk;
128 static int faraday_res_to_memcfg(resource_size_t mem_base,
129 resource_size_t mem_size, u32 *val)
131 u32 outval;
133 switch (mem_size) {
134 case SZ_1M:
135 outval = FARADAY_PCI_MEMSIZE_1MB;
136 break;
137 case SZ_2M:
138 outval = FARADAY_PCI_MEMSIZE_2MB;
139 break;
140 case SZ_4M:
141 outval = FARADAY_PCI_MEMSIZE_4MB;
142 break;
143 case SZ_8M:
144 outval = FARADAY_PCI_MEMSIZE_8MB;
145 break;
146 case SZ_16M:
147 outval = FARADAY_PCI_MEMSIZE_16MB;
148 break;
149 case SZ_32M:
150 outval = FARADAY_PCI_MEMSIZE_32MB;
151 break;
152 case SZ_64M:
153 outval = FARADAY_PCI_MEMSIZE_64MB;
154 break;
155 case SZ_128M:
156 outval = FARADAY_PCI_MEMSIZE_128MB;
157 break;
158 case SZ_256M:
159 outval = FARADAY_PCI_MEMSIZE_256MB;
160 break;
161 case SZ_512M:
162 outval = FARADAY_PCI_MEMSIZE_512MB;
163 break;
164 case SZ_1G:
165 outval = FARADAY_PCI_MEMSIZE_1GB;
166 break;
167 case SZ_2G:
168 outval = FARADAY_PCI_MEMSIZE_2GB;
169 break;
170 default:
171 return -EINVAL;
173 outval <<= FARADAY_PCI_MEMSIZE_SHIFT;
175 /* This is probably not good */
176 if (mem_base & ~(FARADAY_PCI_MEMBASE_MASK))
177 pr_warn("truncated PCI memory base\n");
178 /* Translate to bridge side address space */
179 outval |= (mem_base & FARADAY_PCI_MEMBASE_MASK);
180 pr_debug("Translated pci base @%pap, size %pap to config %08x\n",
181 &mem_base, &mem_size, outval);
183 *val = outval;
184 return 0;
187 static int faraday_raw_pci_read_config(struct faraday_pci *p, int bus_number,
188 unsigned int fn, int config, int size,
189 u32 *value)
191 writel(PCI_CONF_BUS(bus_number) |
192 PCI_CONF_DEVICE(PCI_SLOT(fn)) |
193 PCI_CONF_FUNCTION(PCI_FUNC(fn)) |
194 PCI_CONF_WHERE(config) |
195 PCI_CONF_ENABLE,
196 p->base + PCI_CONFIG);
198 *value = readl(p->base + PCI_DATA);
200 if (size == 1)
201 *value = (*value >> (8 * (config & 3))) & 0xFF;
202 else if (size == 2)
203 *value = (*value >> (8 * (config & 3))) & 0xFFFF;
205 return PCIBIOS_SUCCESSFUL;
208 static int faraday_pci_read_config(struct pci_bus *bus, unsigned int fn,
209 int config, int size, u32 *value)
211 struct faraday_pci *p = bus->sysdata;
213 dev_dbg(&bus->dev,
214 "[read] slt: %.2d, fnc: %d, cnf: 0x%.2X, val (%d bytes): 0x%.8X\n",
215 PCI_SLOT(fn), PCI_FUNC(fn), config, size, *value);
217 return faraday_raw_pci_read_config(p, bus->number, fn, config, size, value);
220 static int faraday_raw_pci_write_config(struct faraday_pci *p, int bus_number,
221 unsigned int fn, int config, int size,
222 u32 value)
224 int ret = PCIBIOS_SUCCESSFUL;
226 writel(PCI_CONF_BUS(bus_number) |
227 PCI_CONF_DEVICE(PCI_SLOT(fn)) |
228 PCI_CONF_FUNCTION(PCI_FUNC(fn)) |
229 PCI_CONF_WHERE(config) |
230 PCI_CONF_ENABLE,
231 p->base + PCI_CONFIG);
233 switch (size) {
234 case 4:
235 writel(value, p->base + PCI_DATA);
236 break;
237 case 2:
238 writew(value, p->base + PCI_DATA + (config & 3));
239 break;
240 case 1:
241 writeb(value, p->base + PCI_DATA + (config & 3));
242 break;
243 default:
244 ret = PCIBIOS_BAD_REGISTER_NUMBER;
247 return ret;
250 static int faraday_pci_write_config(struct pci_bus *bus, unsigned int fn,
251 int config, int size, u32 value)
253 struct faraday_pci *p = bus->sysdata;
255 dev_dbg(&bus->dev,
256 "[write] slt: %.2d, fnc: %d, cnf: 0x%.2X, val (%d bytes): 0x%.8X\n",
257 PCI_SLOT(fn), PCI_FUNC(fn), config, size, value);
259 return faraday_raw_pci_write_config(p, bus->number, fn, config, size,
260 value);
263 static struct pci_ops faraday_pci_ops = {
264 .read = faraday_pci_read_config,
265 .write = faraday_pci_write_config,
268 static void faraday_pci_ack_irq(struct irq_data *d)
270 struct faraday_pci *p = irq_data_get_irq_chip_data(d);
271 unsigned int reg;
273 faraday_raw_pci_read_config(p, 0, 0, FARADAY_PCI_CTRL2, 4, &reg);
274 reg &= ~(0xF << PCI_CTRL2_INTSTS_SHIFT);
275 reg |= BIT(irqd_to_hwirq(d) + PCI_CTRL2_INTSTS_SHIFT);
276 faraday_raw_pci_write_config(p, 0, 0, FARADAY_PCI_CTRL2, 4, reg);
279 static void faraday_pci_mask_irq(struct irq_data *d)
281 struct faraday_pci *p = irq_data_get_irq_chip_data(d);
282 unsigned int reg;
284 faraday_raw_pci_read_config(p, 0, 0, FARADAY_PCI_CTRL2, 4, &reg);
285 reg &= ~((0xF << PCI_CTRL2_INTSTS_SHIFT)
286 | BIT(irqd_to_hwirq(d) + PCI_CTRL2_INTMASK_SHIFT));
287 faraday_raw_pci_write_config(p, 0, 0, FARADAY_PCI_CTRL2, 4, reg);
290 static void faraday_pci_unmask_irq(struct irq_data *d)
292 struct faraday_pci *p = irq_data_get_irq_chip_data(d);
293 unsigned int reg;
295 faraday_raw_pci_read_config(p, 0, 0, FARADAY_PCI_CTRL2, 4, &reg);
296 reg &= ~(0xF << PCI_CTRL2_INTSTS_SHIFT);
297 reg |= BIT(irqd_to_hwirq(d) + PCI_CTRL2_INTMASK_SHIFT);
298 faraday_raw_pci_write_config(p, 0, 0, FARADAY_PCI_CTRL2, 4, reg);
301 static void faraday_pci_irq_handler(struct irq_desc *desc)
303 struct faraday_pci *p = irq_desc_get_handler_data(desc);
304 struct irq_chip *irqchip = irq_desc_get_chip(desc);
305 unsigned int irq_stat, reg, i;
307 faraday_raw_pci_read_config(p, 0, 0, FARADAY_PCI_CTRL2, 4, &reg);
308 irq_stat = reg >> PCI_CTRL2_INTSTS_SHIFT;
310 chained_irq_enter(irqchip, desc);
312 for (i = 0; i < 4; i++) {
313 if ((irq_stat & BIT(i)) == 0)
314 continue;
315 generic_handle_irq(irq_find_mapping(p->irqdomain, i));
318 chained_irq_exit(irqchip, desc);
321 static struct irq_chip faraday_pci_irq_chip = {
322 .name = "PCI",
323 .irq_ack = faraday_pci_ack_irq,
324 .irq_mask = faraday_pci_mask_irq,
325 .irq_unmask = faraday_pci_unmask_irq,
328 static int faraday_pci_irq_map(struct irq_domain *domain, unsigned int irq,
329 irq_hw_number_t hwirq)
331 irq_set_chip_and_handler(irq, &faraday_pci_irq_chip, handle_level_irq);
332 irq_set_chip_data(irq, domain->host_data);
334 return 0;
337 static const struct irq_domain_ops faraday_pci_irqdomain_ops = {
338 .map = faraday_pci_irq_map,
341 static int faraday_pci_setup_cascaded_irq(struct faraday_pci *p)
343 struct device_node *intc = of_get_next_child(p->dev->of_node, NULL);
344 int irq;
345 int i;
347 if (!intc) {
348 dev_err(p->dev, "missing child interrupt-controller node\n");
349 return -EINVAL;
352 /* All PCI IRQs cascade off this one */
353 irq = of_irq_get(intc, 0);
354 if (irq <= 0) {
355 dev_err(p->dev, "failed to get parent IRQ\n");
356 return irq ?: -EINVAL;
359 p->irqdomain = irq_domain_add_linear(intc, PCI_NUM_INTX,
360 &faraday_pci_irqdomain_ops, p);
361 if (!p->irqdomain) {
362 dev_err(p->dev, "failed to create Gemini PCI IRQ domain\n");
363 return -EINVAL;
366 irq_set_chained_handler_and_data(irq, faraday_pci_irq_handler, p);
368 for (i = 0; i < 4; i++)
369 irq_create_mapping(p->irqdomain, i);
371 return 0;
374 static int faraday_pci_parse_map_dma_ranges(struct faraday_pci *p,
375 struct device_node *np)
377 struct of_pci_range range;
378 struct of_pci_range_parser parser;
379 struct device *dev = p->dev;
380 u32 confreg[3] = {
381 FARADAY_PCI_MEM1_BASE_SIZE,
382 FARADAY_PCI_MEM2_BASE_SIZE,
383 FARADAY_PCI_MEM3_BASE_SIZE,
385 int i = 0;
386 u32 val;
388 if (of_pci_dma_range_parser_init(&parser, np)) {
389 dev_err(dev, "missing dma-ranges property\n");
390 return -EINVAL;
394 * Get the dma-ranges from the device tree
396 for_each_of_pci_range(&parser, &range) {
397 u64 end = range.pci_addr + range.size - 1;
398 int ret;
400 ret = faraday_res_to_memcfg(range.pci_addr, range.size, &val);
401 if (ret) {
402 dev_err(dev,
403 "DMA range %d: illegal MEM resource size\n", i);
404 return -EINVAL;
407 dev_info(dev, "DMA MEM%d BASE: 0x%016llx -> 0x%016llx config %08x\n",
408 i + 1, range.pci_addr, end, val);
409 if (i <= 2) {
410 faraday_raw_pci_write_config(p, 0, 0, confreg[i],
411 4, val);
412 } else {
413 dev_err(dev, "ignore extraneous dma-range %d\n", i);
414 break;
417 i++;
420 return 0;
423 static int faraday_pci_probe(struct platform_device *pdev)
425 struct device *dev = &pdev->dev;
426 const struct faraday_pci_variant *variant =
427 of_device_get_match_data(dev);
428 struct resource *regs;
429 resource_size_t io_base;
430 struct resource_entry *win;
431 struct faraday_pci *p;
432 struct resource *mem;
433 struct resource *io;
434 struct pci_host_bridge *host;
435 struct clk *clk;
436 unsigned char max_bus_speed = PCI_SPEED_33MHz;
437 unsigned char cur_bus_speed = PCI_SPEED_33MHz;
438 int ret;
439 u32 val;
440 LIST_HEAD(res);
442 host = devm_pci_alloc_host_bridge(dev, sizeof(*p));
443 if (!host)
444 return -ENOMEM;
446 host->dev.parent = dev;
447 host->ops = &faraday_pci_ops;
448 host->busnr = 0;
449 host->msi = NULL;
450 host->map_irq = of_irq_parse_and_map_pci;
451 host->swizzle_irq = pci_common_swizzle;
452 p = pci_host_bridge_priv(host);
453 host->sysdata = p;
454 p->dev = dev;
456 /* Retrieve and enable optional clocks */
457 clk = devm_clk_get(dev, "PCLK");
458 if (IS_ERR(clk))
459 return PTR_ERR(clk);
460 ret = clk_prepare_enable(clk);
461 if (ret) {
462 dev_err(dev, "could not prepare PCLK\n");
463 return ret;
465 p->bus_clk = devm_clk_get(dev, "PCICLK");
466 if (IS_ERR(p->bus_clk))
467 return PTR_ERR(p->bus_clk);
468 ret = clk_prepare_enable(p->bus_clk);
469 if (ret) {
470 dev_err(dev, "could not prepare PCICLK\n");
471 return ret;
474 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
475 p->base = devm_ioremap_resource(dev, regs);
476 if (IS_ERR(p->base))
477 return PTR_ERR(p->base);
479 ret = of_pci_get_host_bridge_resources(dev->of_node, 0, 0xff,
480 &res, &io_base);
481 if (ret)
482 return ret;
484 ret = devm_request_pci_bus_resources(dev, &res);
485 if (ret)
486 return ret;
488 /* Get the I/O and memory ranges from DT */
489 resource_list_for_each_entry(win, &res) {
490 switch (resource_type(win->res)) {
491 case IORESOURCE_IO:
492 io = win->res;
493 io->name = "Gemini PCI I/O";
494 if (!faraday_res_to_memcfg(io->start - win->offset,
495 resource_size(io), &val)) {
496 /* setup I/O space size */
497 writel(val, p->base + PCI_IOSIZE);
498 } else {
499 dev_err(dev, "illegal IO mem size\n");
500 return -EINVAL;
502 ret = pci_remap_iospace(io, io_base);
503 if (ret) {
504 dev_warn(dev, "error %d: failed to map resource %pR\n",
505 ret, io);
506 continue;
508 break;
509 case IORESOURCE_MEM:
510 mem = win->res;
511 mem->name = "Gemini PCI MEM";
512 break;
513 case IORESOURCE_BUS:
514 break;
515 default:
516 break;
520 /* Setup hostbridge */
521 val = readl(p->base + PCI_CTRL);
522 val |= PCI_COMMAND_IO;
523 val |= PCI_COMMAND_MEMORY;
524 val |= PCI_COMMAND_MASTER;
525 writel(val, p->base + PCI_CTRL);
526 /* Mask and clear all interrupts */
527 faraday_raw_pci_write_config(p, 0, 0, FARADAY_PCI_CTRL2 + 2, 2, 0xF000);
528 if (variant->cascaded_irq) {
529 ret = faraday_pci_setup_cascaded_irq(p);
530 if (ret) {
531 dev_err(dev, "failed to setup cascaded IRQ\n");
532 return ret;
536 /* Check bus clock if we can gear up to 66 MHz */
537 if (!IS_ERR(p->bus_clk)) {
538 unsigned long rate;
539 u32 val;
541 faraday_raw_pci_read_config(p, 0, 0,
542 FARADAY_PCI_STATUS_CMD, 4, &val);
543 rate = clk_get_rate(p->bus_clk);
545 if ((rate == 33000000) && (val & PCI_STATUS_66MHZ_CAPABLE)) {
546 dev_info(dev, "33MHz bus is 66MHz capable\n");
547 max_bus_speed = PCI_SPEED_66MHz;
548 ret = clk_set_rate(p->bus_clk, 66000000);
549 if (ret)
550 dev_err(dev, "failed to set bus clock\n");
551 } else {
552 dev_info(dev, "33MHz only bus\n");
553 max_bus_speed = PCI_SPEED_33MHz;
556 /* Bumping the clock may fail so read back the rate */
557 rate = clk_get_rate(p->bus_clk);
558 if (rate == 33000000)
559 cur_bus_speed = PCI_SPEED_33MHz;
560 if (rate == 66000000)
561 cur_bus_speed = PCI_SPEED_66MHz;
564 ret = faraday_pci_parse_map_dma_ranges(p, dev->of_node);
565 if (ret)
566 return ret;
568 list_splice_init(&res, &host->windows);
569 ret = pci_scan_root_bus_bridge(host);
570 if (ret) {
571 dev_err(dev, "failed to scan host: %d\n", ret);
572 return ret;
574 p->bus = host->bus;
575 p->bus->max_bus_speed = max_bus_speed;
576 p->bus->cur_bus_speed = cur_bus_speed;
578 pci_bus_assign_resources(p->bus);
579 pci_bus_add_devices(p->bus);
580 pci_free_resource_list(&res);
582 return 0;
586 * We encode bridge variants here, we have at least two so it doesn't
587 * hurt to have infrastructure to encompass future variants as well.
589 const struct faraday_pci_variant faraday_regular = {
590 .cascaded_irq = true,
593 const struct faraday_pci_variant faraday_dual = {
594 .cascaded_irq = false,
597 static const struct of_device_id faraday_pci_of_match[] = {
599 .compatible = "faraday,ftpci100",
600 .data = &faraday_regular,
603 .compatible = "faraday,ftpci100-dual",
604 .data = &faraday_dual,
609 static struct platform_driver faraday_pci_driver = {
610 .driver = {
611 .name = "ftpci100",
612 .of_match_table = of_match_ptr(faraday_pci_of_match),
613 .suppress_bind_attrs = true,
615 .probe = faraday_pci_probe,
617 builtin_platform_driver(faraday_pci_driver);