2 * Ralink RT3662/RT3883 SoC PCI support
6 * Parts of this file are based on Ralink's 2.6.21 BSP
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published
10 * by the Free Software Foundation.
13 #include <linux/types.h>
14 #include <linux/pci.h>
16 #include <linux/init.h>
17 #include <linux/delay.h>
18 #include <linux/interrupt.h>
20 #include <linux/of_irq.h>
21 #include <linux/of_pci.h>
22 #include <linux/platform_device.h>
24 #include <asm/mach-ralink/rt3883.h>
25 #include <asm/mach-ralink/ralink_regs.h>
27 #define RT3883_MEMORY_BASE 0x00000000
28 #define RT3883_MEMORY_SIZE 0x02000000
30 #define RT3883_PCI_REG_PCICFG 0x00
31 #define RT3883_PCICFG_P2P_BR_DEVNUM_M 0xf
32 #define RT3883_PCICFG_P2P_BR_DEVNUM_S 16
33 #define RT3883_PCICFG_PCIRST BIT(1)
34 #define RT3883_PCI_REG_PCIRAW 0x04
35 #define RT3883_PCI_REG_PCIINT 0x08
36 #define RT3883_PCI_REG_PCIENA 0x0c
38 #define RT3883_PCI_REG_CFGADDR 0x20
39 #define RT3883_PCI_REG_CFGDATA 0x24
40 #define RT3883_PCI_REG_MEMBASE 0x28
41 #define RT3883_PCI_REG_IOBASE 0x2c
42 #define RT3883_PCI_REG_ARBCTL 0x80
44 #define RT3883_PCI_REG_BASE(_x) (0x1000 + (_x) * 0x1000)
45 #define RT3883_PCI_REG_BAR0SETUP(_x) (RT3883_PCI_REG_BASE((_x)) + 0x10)
46 #define RT3883_PCI_REG_IMBASEBAR0(_x) (RT3883_PCI_REG_BASE((_x)) + 0x18)
47 #define RT3883_PCI_REG_ID(_x) (RT3883_PCI_REG_BASE((_x)) + 0x30)
48 #define RT3883_PCI_REG_CLASS(_x) (RT3883_PCI_REG_BASE((_x)) + 0x34)
49 #define RT3883_PCI_REG_SUBID(_x) (RT3883_PCI_REG_BASE((_x)) + 0x38)
50 #define RT3883_PCI_REG_STATUS(_x) (RT3883_PCI_REG_BASE((_x)) + 0x50)
52 #define RT3883_PCI_MODE_NONE 0
53 #define RT3883_PCI_MODE_PCI BIT(0)
54 #define RT3883_PCI_MODE_PCIE BIT(1)
55 #define RT3883_PCI_MODE_BOTH (RT3883_PCI_MODE_PCI | RT3883_PCI_MODE_PCIE)
57 #define RT3883_PCI_IRQ_COUNT 32
59 #define RT3883_P2P_BR_DEVNUM 1
61 struct rt3883_pci_controller {
64 struct device_node *intc_of_node;
65 struct irq_domain *irq_domain;
67 struct pci_controller pci_controller;
68 struct resource io_res;
69 struct resource mem_res;
74 static inline struct rt3883_pci_controller *
75 pci_bus_to_rt3883_controller(struct pci_bus *bus)
77 struct pci_controller *hose;
79 hose = (struct pci_controller *) bus->sysdata;
80 return container_of(hose, struct rt3883_pci_controller, pci_controller);
83 static inline u32 rt3883_pci_r32(struct rt3883_pci_controller *rpc,
86 return ioread32(rpc->base + reg);
89 static inline void rt3883_pci_w32(struct rt3883_pci_controller *rpc,
90 u32 val, unsigned reg)
92 iowrite32(val, rpc->base + reg);
95 static inline u32 rt3883_pci_get_cfgaddr(unsigned int bus, unsigned int slot,
96 unsigned int func, unsigned int where)
98 return (bus << 16) | (slot << 11) | (func << 8) | (where & 0xfc) |
102 static u32 rt3883_pci_read_cfg32(struct rt3883_pci_controller *rpc,
103 unsigned bus, unsigned slot,
104 unsigned func, unsigned reg)
110 address = rt3883_pci_get_cfgaddr(bus, slot, func, reg);
112 rt3883_pci_w32(rpc, address, RT3883_PCI_REG_CFGADDR);
113 ret = rt3883_pci_r32(rpc, RT3883_PCI_REG_CFGDATA);
118 static void rt3883_pci_write_cfg32(struct rt3883_pci_controller *rpc,
119 unsigned bus, unsigned slot,
120 unsigned func, unsigned reg, u32 val)
125 address = rt3883_pci_get_cfgaddr(bus, slot, func, reg);
127 rt3883_pci_w32(rpc, address, RT3883_PCI_REG_CFGADDR);
128 rt3883_pci_w32(rpc, val, RT3883_PCI_REG_CFGDATA);
131 static void rt3883_pci_irq_handler(struct irq_desc *desc)
133 struct rt3883_pci_controller *rpc;
136 rpc = irq_desc_get_handler_data(desc);
138 pending = rt3883_pci_r32(rpc, RT3883_PCI_REG_PCIINT) &
139 rt3883_pci_r32(rpc, RT3883_PCI_REG_PCIENA);
142 spurious_interrupt();
147 unsigned irq, bit = __ffs(pending);
149 irq = irq_find_mapping(rpc->irq_domain, bit);
150 generic_handle_irq(irq);
152 pending &= ~BIT(bit);
156 static void rt3883_pci_irq_unmask(struct irq_data *d)
158 struct rt3883_pci_controller *rpc;
161 rpc = irq_data_get_irq_chip_data(d);
163 t = rt3883_pci_r32(rpc, RT3883_PCI_REG_PCIENA);
164 rt3883_pci_w32(rpc, t | BIT(d->hwirq), RT3883_PCI_REG_PCIENA);
166 rt3883_pci_r32(rpc, RT3883_PCI_REG_PCIENA);
169 static void rt3883_pci_irq_mask(struct irq_data *d)
171 struct rt3883_pci_controller *rpc;
174 rpc = irq_data_get_irq_chip_data(d);
176 t = rt3883_pci_r32(rpc, RT3883_PCI_REG_PCIENA);
177 rt3883_pci_w32(rpc, t & ~BIT(d->hwirq), RT3883_PCI_REG_PCIENA);
179 rt3883_pci_r32(rpc, RT3883_PCI_REG_PCIENA);
182 static struct irq_chip rt3883_pci_irq_chip = {
183 .name = "RT3883 PCI",
184 .irq_mask = rt3883_pci_irq_mask,
185 .irq_unmask = rt3883_pci_irq_unmask,
186 .irq_mask_ack = rt3883_pci_irq_mask,
189 static int rt3883_pci_irq_map(struct irq_domain *d, unsigned int irq,
192 irq_set_chip_and_handler(irq, &rt3883_pci_irq_chip, handle_level_irq);
193 irq_set_chip_data(irq, d->host_data);
198 static const struct irq_domain_ops rt3883_pci_irq_domain_ops = {
199 .map = rt3883_pci_irq_map,
200 .xlate = irq_domain_xlate_onecell,
203 static int rt3883_pci_irq_init(struct device *dev,
204 struct rt3883_pci_controller *rpc)
208 irq = irq_of_parse_and_map(rpc->intc_of_node, 0);
210 dev_err(dev, "%s has no IRQ",
211 of_node_full_name(rpc->intc_of_node));
215 /* disable all interrupts */
216 rt3883_pci_w32(rpc, 0, RT3883_PCI_REG_PCIENA);
219 irq_domain_add_linear(rpc->intc_of_node, RT3883_PCI_IRQ_COUNT,
220 &rt3883_pci_irq_domain_ops,
222 if (!rpc->irq_domain) {
223 dev_err(dev, "unable to add IRQ domain\n");
227 irq_set_chained_handler_and_data(irq, rt3883_pci_irq_handler, rpc);
232 static int rt3883_pci_config_read(struct pci_bus *bus, unsigned int devfn,
233 int where, int size, u32 *val)
235 struct rt3883_pci_controller *rpc;
240 rpc = pci_bus_to_rt3883_controller(bus);
242 if (!rpc->pcie_ready && bus->number == 1)
243 return PCIBIOS_DEVICE_NOT_FOUND;
245 address = rt3883_pci_get_cfgaddr(bus->number, PCI_SLOT(devfn),
246 PCI_FUNC(devfn), where);
248 rt3883_pci_w32(rpc, address, RT3883_PCI_REG_CFGADDR);
249 data = rt3883_pci_r32(rpc, RT3883_PCI_REG_CFGDATA);
253 *val = (data >> ((where & 3) << 3)) & 0xff;
256 *val = (data >> ((where & 3) << 3)) & 0xffff;
263 return PCIBIOS_SUCCESSFUL;
266 static int rt3883_pci_config_write(struct pci_bus *bus, unsigned int devfn,
267 int where, int size, u32 val)
269 struct rt3883_pci_controller *rpc;
274 rpc = pci_bus_to_rt3883_controller(bus);
276 if (!rpc->pcie_ready && bus->number == 1)
277 return PCIBIOS_DEVICE_NOT_FOUND;
279 address = rt3883_pci_get_cfgaddr(bus->number, PCI_SLOT(devfn),
280 PCI_FUNC(devfn), where);
282 rt3883_pci_w32(rpc, address, RT3883_PCI_REG_CFGADDR);
283 data = rt3883_pci_r32(rpc, RT3883_PCI_REG_CFGDATA);
287 data = (data & ~(0xff << ((where & 3) << 3))) |
288 (val << ((where & 3) << 3));
291 data = (data & ~(0xffff << ((where & 3) << 3))) |
292 (val << ((where & 3) << 3));
299 rt3883_pci_w32(rpc, data, RT3883_PCI_REG_CFGDATA);
301 return PCIBIOS_SUCCESSFUL;
304 static struct pci_ops rt3883_pci_ops = {
305 .read = rt3883_pci_config_read,
306 .write = rt3883_pci_config_write,
309 static void rt3883_pci_preinit(struct rt3883_pci_controller *rpc, unsigned mode)
316 rstctrl = rt_sysc_r32(RT3883_SYSC_REG_RSTCTRL);
317 syscfg1 = rt_sysc_r32(RT3883_SYSC_REG_SYSCFG1);
318 clkcfg1 = rt_sysc_r32(RT3883_SYSC_REG_CLKCFG1);
320 if (mode & RT3883_PCI_MODE_PCIE) {
321 rstctrl |= RT3883_RSTCTRL_PCIE;
322 rt_sysc_w32(rstctrl, RT3883_SYSC_REG_RSTCTRL);
324 /* setup PCI PAD drive mode */
327 rt_sysc_w32(syscfg1, RT3883_SYSC_REG_SYSCFG1);
329 t = rt_sysc_r32(RT3883_SYSC_REG_PCIE_CLK_GEN0);
331 rt_sysc_w32(t, RT3883_SYSC_REG_PCIE_CLK_GEN0);
333 t = rt_sysc_r32(RT3883_SYSC_REG_PCIE_CLK_GEN1);
335 rt_sysc_w32(t, RT3883_SYSC_REG_PCIE_CLK_GEN1);
337 t = rt_sysc_r32(RT3883_SYSC_REG_PCIE_CLK_GEN1);
339 rt_sysc_w32(t, RT3883_SYSC_REG_PCIE_CLK_GEN1);
341 t = rt_sysc_r32(RT3883_SYSC_REG_PCIE_CLK_GEN0);
343 rt_sysc_w32(t, RT3883_SYSC_REG_PCIE_CLK_GEN0);
347 rstctrl &= ~RT3883_RSTCTRL_PCIE;
348 rt_sysc_w32(rstctrl, RT3883_SYSC_REG_RSTCTRL);
351 syscfg1 |= (RT3883_SYSCFG1_PCIE_RC_MODE | RT3883_SYSCFG1_PCI_HOST_MODE);
353 clkcfg1 &= ~(RT3883_CLKCFG1_PCI_CLK_EN | RT3883_CLKCFG1_PCIE_CLK_EN);
355 if (mode & RT3883_PCI_MODE_PCI) {
356 clkcfg1 |= RT3883_CLKCFG1_PCI_CLK_EN;
357 rstctrl &= ~RT3883_RSTCTRL_PCI;
360 if (mode & RT3883_PCI_MODE_PCIE) {
361 clkcfg1 |= RT3883_CLKCFG1_PCIE_CLK_EN;
362 rstctrl &= ~RT3883_RSTCTRL_PCIE;
365 rt_sysc_w32(syscfg1, RT3883_SYSC_REG_SYSCFG1);
366 rt_sysc_w32(rstctrl, RT3883_SYSC_REG_RSTCTRL);
367 rt_sysc_w32(clkcfg1, RT3883_SYSC_REG_CLKCFG1);
372 * setup the device number of the P2P bridge
373 * and de-assert the reset line
375 t = (RT3883_P2P_BR_DEVNUM << RT3883_PCICFG_P2P_BR_DEVNUM_S);
376 rt3883_pci_w32(rpc, t, RT3883_PCI_REG_PCICFG);
379 rt3883_pci_r32(rpc, RT3883_PCI_REG_PCICFG);
382 if (mode & RT3883_PCI_MODE_PCIE) {
385 t = rt3883_pci_r32(rpc, RT3883_PCI_REG_STATUS(1));
387 rpc->pcie_ready = t & BIT(0);
389 if (!rpc->pcie_ready) {
390 /* reset the PCIe block */
391 t = rt_sysc_r32(RT3883_SYSC_REG_RSTCTRL);
392 t |= RT3883_RSTCTRL_PCIE;
393 rt_sysc_w32(t, RT3883_SYSC_REG_RSTCTRL);
394 t &= ~RT3883_RSTCTRL_PCIE;
395 rt_sysc_w32(t, RT3883_SYSC_REG_RSTCTRL);
397 /* turn off PCIe clock */
398 t = rt_sysc_r32(RT3883_SYSC_REG_CLKCFG1);
399 t &= ~RT3883_CLKCFG1_PCIE_CLK_EN;
400 rt_sysc_w32(t, RT3883_SYSC_REG_CLKCFG1);
402 t = rt_sysc_r32(RT3883_SYSC_REG_PCIE_CLK_GEN0);
404 rt_sysc_w32(t, RT3883_SYSC_REG_PCIE_CLK_GEN0);
408 /* enable PCI arbiter */
409 rt3883_pci_w32(rpc, 0x79, RT3883_PCI_REG_ARBCTL);
412 static int rt3883_pci_probe(struct platform_device *pdev)
414 struct rt3883_pci_controller *rpc;
415 struct device *dev = &pdev->dev;
416 struct device_node *np = dev->of_node;
417 struct resource *res;
418 struct device_node *child;
423 rpc = devm_kzalloc(dev, sizeof(*rpc), GFP_KERNEL);
427 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
428 rpc->base = devm_ioremap_resource(dev, res);
429 if (IS_ERR(rpc->base))
430 return PTR_ERR(rpc->base);
432 /* find the interrupt controller child node */
433 for_each_child_of_node(np, child) {
434 if (of_get_property(child, "interrupt-controller", NULL)) {
435 rpc->intc_of_node = child;
440 if (!rpc->intc_of_node) {
441 dev_err(dev, "%s has no %s child node",
442 of_node_full_name(rpc->intc_of_node),
443 "interrupt controller");
447 /* find the PCI host bridge child node */
448 for_each_child_of_node(np, child) {
450 of_node_cmp(child->type, "pci") == 0) {
451 rpc->pci_controller.of_node = child;
456 if (!rpc->pci_controller.of_node) {
457 dev_err(dev, "%s has no %s child node",
458 of_node_full_name(rpc->intc_of_node),
461 goto err_put_intc_node;
464 mode = RT3883_PCI_MODE_NONE;
465 for_each_available_child_of_node(rpc->pci_controller.of_node, child) {
469 of_node_cmp(child->type, "pci") != 0)
472 devfn = of_pci_get_devfn(child);
476 switch (PCI_SLOT(devfn)) {
478 mode |= RT3883_PCI_MODE_PCIE;
483 mode |= RT3883_PCI_MODE_PCI;
488 if (mode == RT3883_PCI_MODE_NONE) {
489 dev_err(dev, "unable to determine PCI mode\n");
491 goto err_put_hb_node;
494 dev_info(dev, "mode:%s%s\n",
495 (mode & RT3883_PCI_MODE_PCI) ? " PCI" : "",
496 (mode & RT3883_PCI_MODE_PCIE) ? " PCIe" : "");
498 rt3883_pci_preinit(rpc, mode);
500 rpc->pci_controller.pci_ops = &rt3883_pci_ops;
501 rpc->pci_controller.io_resource = &rpc->io_res;
502 rpc->pci_controller.mem_resource = &rpc->mem_res;
504 /* Load PCI I/O and memory resources from DT */
505 pci_load_of_ranges(&rpc->pci_controller,
506 rpc->pci_controller.of_node);
508 rt3883_pci_w32(rpc, rpc->mem_res.start, RT3883_PCI_REG_MEMBASE);
509 rt3883_pci_w32(rpc, rpc->io_res.start, RT3883_PCI_REG_IOBASE);
511 ioport_resource.start = rpc->io_res.start;
512 ioport_resource.end = rpc->io_res.end;
515 rt3883_pci_w32(rpc, 0x03ff0000, RT3883_PCI_REG_BAR0SETUP(0));
516 rt3883_pci_w32(rpc, RT3883_MEMORY_BASE, RT3883_PCI_REG_IMBASEBAR0(0));
517 rt3883_pci_w32(rpc, 0x08021814, RT3883_PCI_REG_ID(0));
518 rt3883_pci_w32(rpc, 0x00800001, RT3883_PCI_REG_CLASS(0));
519 rt3883_pci_w32(rpc, 0x28801814, RT3883_PCI_REG_SUBID(0));
522 rt3883_pci_w32(rpc, 0x03ff0000, RT3883_PCI_REG_BAR0SETUP(1));
523 rt3883_pci_w32(rpc, RT3883_MEMORY_BASE, RT3883_PCI_REG_IMBASEBAR0(1));
524 rt3883_pci_w32(rpc, 0x08021814, RT3883_PCI_REG_ID(1));
525 rt3883_pci_w32(rpc, 0x06040001, RT3883_PCI_REG_CLASS(1));
526 rt3883_pci_w32(rpc, 0x28801814, RT3883_PCI_REG_SUBID(1));
528 err = rt3883_pci_irq_init(dev, rpc);
530 goto err_put_hb_node;
533 val = rt3883_pci_read_cfg32(rpc, 0, 0x01, 0, PCI_COMMAND);
534 val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
535 rt3883_pci_write_cfg32(rpc, 0, 0x01, 0, PCI_COMMAND, val);
538 val = rt3883_pci_read_cfg32(rpc, 0, 0x00, 0, PCI_COMMAND);
539 val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
540 rt3883_pci_write_cfg32(rpc, 0, 0x00, 0, PCI_COMMAND, val);
542 if (mode == RT3883_PCI_MODE_PCIE) {
543 rt3883_pci_w32(rpc, 0x03ff0001, RT3883_PCI_REG_BAR0SETUP(0));
544 rt3883_pci_w32(rpc, 0x03ff0001, RT3883_PCI_REG_BAR0SETUP(1));
546 rt3883_pci_write_cfg32(rpc, 0, RT3883_P2P_BR_DEVNUM, 0,
550 rt3883_pci_read_cfg32(rpc, 0, RT3883_P2P_BR_DEVNUM, 0,
553 rt3883_pci_write_cfg32(rpc, 0, RT3883_P2P_BR_DEVNUM, 0,
554 PCI_IO_BASE, 0x00000101);
557 register_pci_controller(&rpc->pci_controller);
562 of_node_put(rpc->pci_controller.of_node);
564 of_node_put(rpc->intc_of_node);
568 int __init pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
570 return of_irq_parse_and_map_pci(dev, slot, pin);
573 int pcibios_plat_dev_init(struct pci_dev *dev)
578 static const struct of_device_id rt3883_pci_ids[] = {
579 { .compatible = "ralink,rt3883-pci" },
583 static struct platform_driver rt3883_pci_driver = {
584 .probe = rt3883_pci_probe,
586 .name = "rt3883-pci",
587 .of_match_table = of_match_ptr(rt3883_pci_ids),
591 static int __init rt3883_pci_init(void)
593 return platform_driver_register(&rt3883_pci_driver);
596 postcore_initcall(rt3883_pci_init);