Gabor Juhos | e2dbdc4 | 2012-03-14 10:29:21 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Atheros AR71XX/AR724X specific PCI setup code |
| 3 | * |
| 4 | * Copyright (C) 2011 René Bolldorf <xsecute@googlemail.com> |
Gabor Juhos | e9b62e8 | 2012-03-14 10:36:14 +0100 | [diff] [blame] | 5 | * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org> |
| 6 | * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org> |
| 7 | * |
| 8 | * Parts of this file are based on Atheros' 2.6.15 BSP |
Gabor Juhos | e2dbdc4 | 2012-03-14 10:29:21 +0100 | [diff] [blame] | 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify it |
| 11 | * under the terms of the GNU General Public License version 2 as published |
| 12 | * by the Free Software Foundation. |
| 13 | */ |
| 14 | |
Gabor Juhos | d22ce25 | 2012-03-14 10:36:11 +0100 | [diff] [blame] | 15 | #include <linux/init.h> |
Gabor Juhos | e2dbdc4 | 2012-03-14 10:29:21 +0100 | [diff] [blame] | 16 | #include <linux/pci.h> |
Gabor Juhos | 9fc1ca5 | 2013-02-02 11:44:24 +0000 | [diff] [blame] | 17 | #include <linux/resource.h> |
| 18 | #include <linux/platform_device.h> |
Gabor Juhos | ec95025 | 2012-03-14 10:45:30 +0100 | [diff] [blame] | 19 | #include <asm/mach-ath79/ar71xx_regs.h> |
Gabor Juhos | 6335aef | 2012-03-14 10:29:24 +0100 | [diff] [blame] | 20 | #include <asm/mach-ath79/ath79.h> |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 21 | #include <asm/mach-ath79/irq.h> |
Gabor Juhos | 3a6208d | 2012-03-14 10:29:22 +0100 | [diff] [blame] | 22 | #include "pci.h" |
Gabor Juhos | e2dbdc4 | 2012-03-14 10:29:21 +0100 | [diff] [blame] | 23 | |
Gabor Juhos | a68ad4d | 2012-03-14 10:36:09 +0100 | [diff] [blame] | 24 | static int (*ath79_pci_plat_dev_init)(struct pci_dev *dev); |
Gabor Juhos | d22ce25 | 2012-03-14 10:36:11 +0100 | [diff] [blame] | 25 | static const struct ath79_pci_irq *ath79_pci_irq_map __initdata; |
| 26 | static unsigned ath79_pci_nr_irqs __initdata; |
Gabor Juhos | e2dbdc4 | 2012-03-14 10:29:21 +0100 | [diff] [blame] | 27 | |
Gabor Juhos | d22ce25 | 2012-03-14 10:36:11 +0100 | [diff] [blame] | 28 | static const struct ath79_pci_irq ar71xx_pci_irq_map[] __initconst = { |
| 29 | { |
| 30 | .slot = 17, |
| 31 | .pin = 1, |
| 32 | .irq = ATH79_PCI_IRQ(0), |
| 33 | }, { |
| 34 | .slot = 18, |
| 35 | .pin = 1, |
| 36 | .irq = ATH79_PCI_IRQ(1), |
| 37 | }, { |
| 38 | .slot = 19, |
| 39 | .pin = 1, |
| 40 | .irq = ATH79_PCI_IRQ(2), |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | static const struct ath79_pci_irq ar724x_pci_irq_map[] __initconst = { |
| 45 | { |
| 46 | .slot = 0, |
| 47 | .pin = 1, |
| 48 | .irq = ATH79_PCI_IRQ(0), |
| 49 | } |
| 50 | }; |
| 51 | |
Gabor Juhos | e2dbdc4 | 2012-03-14 10:29:21 +0100 | [diff] [blame] | 52 | int __init pcibios_map_irq(const struct pci_dev *dev, uint8_t slot, uint8_t pin) |
| 53 | { |
Gabor Juhos | e2dbdc4 | 2012-03-14 10:29:21 +0100 | [diff] [blame] | 54 | int irq = -1; |
Gabor Juhos | d22ce25 | 2012-03-14 10:36:11 +0100 | [diff] [blame] | 55 | int i; |
Gabor Juhos | e2dbdc4 | 2012-03-14 10:29:21 +0100 | [diff] [blame] | 56 | |
Gabor Juhos | d22ce25 | 2012-03-14 10:36:11 +0100 | [diff] [blame] | 57 | if (ath79_pci_nr_irqs == 0 || |
| 58 | ath79_pci_irq_map == NULL) { |
| 59 | if (soc_is_ar71xx()) { |
| 60 | ath79_pci_irq_map = ar71xx_pci_irq_map; |
| 61 | ath79_pci_nr_irqs = ARRAY_SIZE(ar71xx_pci_irq_map); |
Gabor Juhos | ec95025 | 2012-03-14 10:45:30 +0100 | [diff] [blame] | 62 | } else if (soc_is_ar724x() || |
| 63 | soc_is_ar9342() || |
| 64 | soc_is_ar9344()) { |
Gabor Juhos | d22ce25 | 2012-03-14 10:36:11 +0100 | [diff] [blame] | 65 | ath79_pci_irq_map = ar724x_pci_irq_map; |
| 66 | ath79_pci_nr_irqs = ARRAY_SIZE(ar724x_pci_irq_map); |
| 67 | } else { |
| 68 | pr_crit("pci %s: invalid irq map\n", |
| 69 | pci_name((struct pci_dev *) dev)); |
| 70 | return irq; |
| 71 | } |
| 72 | } |
Gabor Juhos | e2dbdc4 | 2012-03-14 10:29:21 +0100 | [diff] [blame] | 73 | |
Gabor Juhos | d22ce25 | 2012-03-14 10:36:11 +0100 | [diff] [blame] | 74 | for (i = 0; i < ath79_pci_nr_irqs; i++) { |
| 75 | const struct ath79_pci_irq *entry; |
| 76 | |
| 77 | entry = &ath79_pci_irq_map[i]; |
Gabor Juhos | 617fed4 | 2013-02-03 09:58:37 +0000 | [diff] [blame] | 78 | if (entry->bus == dev->bus->number && |
| 79 | entry->slot == slot && |
| 80 | entry->pin == pin) { |
Gabor Juhos | d22ce25 | 2012-03-14 10:36:11 +0100 | [diff] [blame] | 81 | irq = entry->irq; |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | if (irq < 0) |
| 87 | pr_crit("pci %s: no irq found for pin %u\n", |
| 88 | pci_name((struct pci_dev *) dev), pin); |
| 89 | else |
| 90 | pr_info("pci %s: using irq %d for pin %u\n", |
| 91 | pci_name((struct pci_dev *) dev), irq, pin); |
Gabor Juhos | e2dbdc4 | 2012-03-14 10:29:21 +0100 | [diff] [blame] | 92 | |
| 93 | return irq; |
| 94 | } |
| 95 | |
| 96 | int pcibios_plat_dev_init(struct pci_dev *dev) |
| 97 | { |
Gabor Juhos | a68ad4d | 2012-03-14 10:36:09 +0100 | [diff] [blame] | 98 | if (ath79_pci_plat_dev_init) |
| 99 | return ath79_pci_plat_dev_init(dev); |
Gabor Juhos | e2dbdc4 | 2012-03-14 10:29:21 +0100 | [diff] [blame] | 100 | |
Gabor Juhos | a68ad4d | 2012-03-14 10:36:09 +0100 | [diff] [blame] | 101 | return 0; |
| 102 | } |
Gabor Juhos | e2dbdc4 | 2012-03-14 10:29:21 +0100 | [diff] [blame] | 103 | |
Gabor Juhos | d22ce25 | 2012-03-14 10:36:11 +0100 | [diff] [blame] | 104 | void __init ath79_pci_set_irq_map(unsigned nr_irqs, |
| 105 | const struct ath79_pci_irq *map) |
| 106 | { |
| 107 | ath79_pci_nr_irqs = nr_irqs; |
| 108 | ath79_pci_irq_map = map; |
| 109 | } |
| 110 | |
Gabor Juhos | a68ad4d | 2012-03-14 10:36:09 +0100 | [diff] [blame] | 111 | void __init ath79_pci_set_plat_dev_init(int (*func)(struct pci_dev *dev)) |
| 112 | { |
| 113 | ath79_pci_plat_dev_init = func; |
Gabor Juhos | e2dbdc4 | 2012-03-14 10:29:21 +0100 | [diff] [blame] | 114 | } |
Gabor Juhos | 6335aef | 2012-03-14 10:29:24 +0100 | [diff] [blame] | 115 | |
Gabor Juhos | 9fc1ca5 | 2013-02-02 11:44:24 +0000 | [diff] [blame] | 116 | static struct platform_device * |
| 117 | ath79_register_pci_ar71xx(void) |
| 118 | { |
| 119 | struct platform_device *pdev; |
Gabor Juhos | 42cb60d | 2013-02-07 19:28:15 +0000 | [diff] [blame] | 120 | struct resource res[4]; |
Gabor Juhos | 9fc1ca5 | 2013-02-02 11:44:24 +0000 | [diff] [blame] | 121 | |
| 122 | memset(res, 0, sizeof(res)); |
| 123 | |
| 124 | res[0].name = "cfg_base"; |
| 125 | res[0].flags = IORESOURCE_MEM; |
| 126 | res[0].start = AR71XX_PCI_CFG_BASE; |
| 127 | res[0].end = AR71XX_PCI_CFG_BASE + AR71XX_PCI_CFG_SIZE - 1; |
| 128 | |
| 129 | res[1].flags = IORESOURCE_IRQ; |
Gabor Juhos | 7e69c10 | 2013-02-07 19:32:23 +0000 | [diff] [blame^] | 130 | res[1].start = ATH79_CPU_IRQ(2); |
| 131 | res[1].end = ATH79_CPU_IRQ(2); |
Gabor Juhos | 9fc1ca5 | 2013-02-02 11:44:24 +0000 | [diff] [blame] | 132 | |
Gabor Juhos | 42cb60d | 2013-02-07 19:28:15 +0000 | [diff] [blame] | 133 | res[2].name = "io_base"; |
| 134 | res[2].flags = IORESOURCE_IO; |
| 135 | res[2].start = 0; |
| 136 | res[2].end = 0; |
| 137 | |
| 138 | res[3].name = "mem_base"; |
| 139 | res[3].flags = IORESOURCE_MEM; |
| 140 | res[3].start = AR71XX_PCI_MEM_BASE; |
| 141 | res[3].end = AR71XX_PCI_MEM_BASE + AR71XX_PCI_MEM_SIZE - 1; |
| 142 | |
Gabor Juhos | 9fc1ca5 | 2013-02-02 11:44:24 +0000 | [diff] [blame] | 143 | pdev = platform_device_register_simple("ar71xx-pci", -1, |
| 144 | res, ARRAY_SIZE(res)); |
| 145 | return pdev; |
| 146 | } |
| 147 | |
| 148 | static struct platform_device * |
| 149 | ath79_register_pci_ar724x(int id, |
| 150 | unsigned long cfg_base, |
| 151 | unsigned long ctrl_base, |
Gabor Juhos | 12401fc | 2013-02-03 14:52:47 +0000 | [diff] [blame] | 152 | unsigned long crp_base, |
Gabor Juhos | 34b134a | 2013-02-03 09:59:45 +0000 | [diff] [blame] | 153 | unsigned long mem_base, |
| 154 | unsigned long mem_size, |
| 155 | unsigned long io_base, |
Gabor Juhos | 9fc1ca5 | 2013-02-02 11:44:24 +0000 | [diff] [blame] | 156 | int irq) |
| 157 | { |
| 158 | struct platform_device *pdev; |
Gabor Juhos | 12401fc | 2013-02-03 14:52:47 +0000 | [diff] [blame] | 159 | struct resource res[6]; |
Gabor Juhos | 9fc1ca5 | 2013-02-02 11:44:24 +0000 | [diff] [blame] | 160 | |
| 161 | memset(res, 0, sizeof(res)); |
| 162 | |
| 163 | res[0].name = "cfg_base"; |
| 164 | res[0].flags = IORESOURCE_MEM; |
| 165 | res[0].start = cfg_base; |
| 166 | res[0].end = cfg_base + AR724X_PCI_CFG_SIZE - 1; |
| 167 | |
| 168 | res[1].name = "ctrl_base"; |
| 169 | res[1].flags = IORESOURCE_MEM; |
| 170 | res[1].start = ctrl_base; |
| 171 | res[1].end = ctrl_base + AR724X_PCI_CTRL_SIZE - 1; |
| 172 | |
| 173 | res[2].flags = IORESOURCE_IRQ; |
| 174 | res[2].start = irq; |
| 175 | res[2].end = irq; |
| 176 | |
Gabor Juhos | 34b134a | 2013-02-03 09:59:45 +0000 | [diff] [blame] | 177 | res[3].name = "mem_base"; |
| 178 | res[3].flags = IORESOURCE_MEM; |
| 179 | res[3].start = mem_base; |
| 180 | res[3].end = mem_base + mem_size - 1; |
| 181 | |
| 182 | res[4].name = "io_base"; |
| 183 | res[4].flags = IORESOURCE_IO; |
| 184 | res[4].start = io_base; |
| 185 | res[4].end = io_base; |
| 186 | |
Gabor Juhos | 12401fc | 2013-02-03 14:52:47 +0000 | [diff] [blame] | 187 | res[5].name = "crp_base"; |
| 188 | res[5].flags = IORESOURCE_MEM; |
| 189 | res[5].start = crp_base; |
| 190 | res[5].end = crp_base + AR724X_PCI_CRP_SIZE - 1; |
| 191 | |
Gabor Juhos | 9fc1ca5 | 2013-02-02 11:44:24 +0000 | [diff] [blame] | 192 | pdev = platform_device_register_simple("ar724x-pci", id, |
| 193 | res, ARRAY_SIZE(res)); |
| 194 | return pdev; |
| 195 | } |
| 196 | |
Gabor Juhos | 6335aef | 2012-03-14 10:29:24 +0100 | [diff] [blame] | 197 | int __init ath79_register_pci(void) |
| 198 | { |
Gabor Juhos | 9fc1ca5 | 2013-02-02 11:44:24 +0000 | [diff] [blame] | 199 | struct platform_device *pdev = NULL; |
Gabor Juhos | d22ce25 | 2012-03-14 10:36:11 +0100 | [diff] [blame] | 200 | |
Gabor Juhos | 9fc1ca5 | 2013-02-02 11:44:24 +0000 | [diff] [blame] | 201 | if (soc_is_ar71xx()) { |
| 202 | pdev = ath79_register_pci_ar71xx(); |
| 203 | } else if (soc_is_ar724x()) { |
| 204 | pdev = ath79_register_pci_ar724x(-1, |
| 205 | AR724X_PCI_CFG_BASE, |
| 206 | AR724X_PCI_CTRL_BASE, |
Gabor Juhos | 12401fc | 2013-02-03 14:52:47 +0000 | [diff] [blame] | 207 | AR724X_PCI_CRP_BASE, |
Gabor Juhos | 34b134a | 2013-02-03 09:59:45 +0000 | [diff] [blame] | 208 | AR724X_PCI_MEM_BASE, |
| 209 | AR724X_PCI_MEM_SIZE, |
| 210 | 0, |
Gabor Juhos | 7e69c10 | 2013-02-07 19:32:23 +0000 | [diff] [blame^] | 211 | ATH79_CPU_IRQ(2)); |
Gabor Juhos | 9fc1ca5 | 2013-02-02 11:44:24 +0000 | [diff] [blame] | 212 | } else if (soc_is_ar9342() || |
| 213 | soc_is_ar9344()) { |
Gabor Juhos | ec95025 | 2012-03-14 10:45:30 +0100 | [diff] [blame] | 214 | u32 bootstrap; |
| 215 | |
| 216 | bootstrap = ath79_reset_rr(AR934X_RESET_REG_BOOTSTRAP); |
Gabor Juhos | 9fc1ca5 | 2013-02-02 11:44:24 +0000 | [diff] [blame] | 217 | if ((bootstrap & AR934X_BOOTSTRAP_PCIE_RC) == 0) |
| 218 | return -ENODEV; |
| 219 | |
| 220 | pdev = ath79_register_pci_ar724x(-1, |
| 221 | AR724X_PCI_CFG_BASE, |
| 222 | AR724X_PCI_CTRL_BASE, |
Gabor Juhos | 12401fc | 2013-02-03 14:52:47 +0000 | [diff] [blame] | 223 | AR724X_PCI_CRP_BASE, |
Gabor Juhos | 34b134a | 2013-02-03 09:59:45 +0000 | [diff] [blame] | 224 | AR724X_PCI_MEM_BASE, |
| 225 | AR724X_PCI_MEM_SIZE, |
| 226 | 0, |
Gabor Juhos | 9fc1ca5 | 2013-02-02 11:44:24 +0000 | [diff] [blame] | 227 | ATH79_IP2_IRQ(0)); |
| 228 | } else { |
| 229 | /* No PCI support */ |
| 230 | return -ENODEV; |
Gabor Juhos | ec95025 | 2012-03-14 10:45:30 +0100 | [diff] [blame] | 231 | } |
| 232 | |
Gabor Juhos | 9fc1ca5 | 2013-02-02 11:44:24 +0000 | [diff] [blame] | 233 | if (!pdev) |
| 234 | pr_err("unable to register PCI controller device\n"); |
| 235 | |
| 236 | return pdev ? 0 : -ENODEV; |
Gabor Juhos | 6335aef | 2012-03-14 10:29:24 +0100 | [diff] [blame] | 237 | } |