Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 1 | /* |
Gabor Juhos | e9b62e8 | 2012-03-14 10:36:14 +0100 | [diff] [blame] | 2 | * Atheros AR724X PCI host controller driver |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 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) 2009-2011 Gabor Juhos <juhosg@openwrt.org> |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License version 2 as published |
| 9 | * by the Free Software Foundation. |
| 10 | */ |
| 11 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 12 | #include <linux/spinlock.h> |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 13 | #include <linux/irq.h> |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 14 | #include <linux/pci.h> |
Gabor Juhos | 58d2e9b | 2013-02-02 11:40:42 +0000 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/platform_device.h> |
Gabor Juhos | 6015a85 | 2012-03-14 10:36:05 +0100 | [diff] [blame] | 17 | #include <asm/mach-ath79/ath79.h> |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 18 | #include <asm/mach-ath79/ar71xx_regs.h> |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 19 | |
Gabor Juhos | a1dca31 | 2012-08-23 15:35:26 +0200 | [diff] [blame] | 20 | #define AR724X_PCI_REG_RESET 0x18 |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 21 | #define AR724X_PCI_REG_INT_STATUS 0x4c |
| 22 | #define AR724X_PCI_REG_INT_MASK 0x50 |
| 23 | |
Gabor Juhos | a1dca31 | 2012-08-23 15:35:26 +0200 | [diff] [blame] | 24 | #define AR724X_PCI_RESET_LINK_UP BIT(0) |
| 25 | |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 26 | #define AR724X_PCI_INT_DEV0 BIT(14) |
| 27 | |
| 28 | #define AR724X_PCI_IRQ_COUNT 1 |
| 29 | |
Gabor Juhos | 6015a85 | 2012-03-14 10:36:05 +0100 | [diff] [blame] | 30 | #define AR7240_BAR0_WAR_VALUE 0xffff |
| 31 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 32 | struct ar724x_pci_controller { |
| 33 | void __iomem *devcfg_base; |
| 34 | void __iomem *ctrl_base; |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 35 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 36 | int irq; |
Gabor Juhos | a1dca31 | 2012-08-23 15:35:26 +0200 | [diff] [blame] | 37 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 38 | bool link_up; |
| 39 | bool bar0_is_cached; |
| 40 | u32 bar0_value; |
| 41 | |
| 42 | spinlock_t lock; |
| 43 | |
| 44 | struct pci_controller pci_controller; |
Gabor Juhos | 34b134a | 2013-02-03 09:59:45 +0000 | [diff] [blame^] | 45 | struct resource io_res; |
| 46 | struct resource mem_res; |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | static inline bool ar724x_pci_check_link(struct ar724x_pci_controller *apc) |
Gabor Juhos | a1dca31 | 2012-08-23 15:35:26 +0200 | [diff] [blame] | 50 | { |
| 51 | u32 reset; |
| 52 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 53 | reset = __raw_readl(apc->ctrl_base + AR724X_PCI_REG_RESET); |
Gabor Juhos | a1dca31 | 2012-08-23 15:35:26 +0200 | [diff] [blame] | 54 | return reset & AR724X_PCI_RESET_LINK_UP; |
| 55 | } |
Gabor Juhos | 6015a85 | 2012-03-14 10:36:05 +0100 | [diff] [blame] | 56 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 57 | static inline struct ar724x_pci_controller * |
| 58 | pci_bus_to_ar724x_controller(struct pci_bus *bus) |
| 59 | { |
| 60 | struct pci_controller *hose; |
| 61 | |
| 62 | hose = (struct pci_controller *) bus->sysdata; |
| 63 | return container_of(hose, struct ar724x_pci_controller, pci_controller); |
| 64 | } |
| 65 | |
Gabor Juhos | d624bd3 | 2012-03-14 10:29:26 +0100 | [diff] [blame] | 66 | static int ar724x_pci_read(struct pci_bus *bus, unsigned int devfn, int where, |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 67 | int size, uint32_t *value) |
| 68 | { |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 69 | struct ar724x_pci_controller *apc; |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 70 | unsigned long flags; |
Gabor Juhos | c198441 | 2012-03-14 10:29:27 +0100 | [diff] [blame] | 71 | void __iomem *base; |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 72 | u32 data; |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 73 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 74 | apc = pci_bus_to_ar724x_controller(bus); |
| 75 | if (!apc->link_up) |
Gabor Juhos | a1dca31 | 2012-08-23 15:35:26 +0200 | [diff] [blame] | 76 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 77 | |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 78 | if (devfn) |
| 79 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 80 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 81 | base = apc->devcfg_base; |
Gabor Juhos | c198441 | 2012-03-14 10:29:27 +0100 | [diff] [blame] | 82 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 83 | spin_lock_irqsave(&apc->lock, flags); |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 84 | data = __raw_readl(base + (where & ~3)); |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 85 | |
| 86 | switch (size) { |
| 87 | case 1: |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 88 | if (where & 1) |
| 89 | data >>= 8; |
| 90 | if (where & 2) |
| 91 | data >>= 16; |
| 92 | data &= 0xff; |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 93 | break; |
| 94 | case 2: |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 95 | if (where & 2) |
| 96 | data >>= 16; |
| 97 | data &= 0xffff; |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 98 | break; |
| 99 | case 4: |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 100 | break; |
| 101 | default: |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 102 | spin_unlock_irqrestore(&apc->lock, flags); |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 103 | |
| 104 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 105 | } |
| 106 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 107 | spin_unlock_irqrestore(&apc->lock, flags); |
Gabor Juhos | 6015a85 | 2012-03-14 10:36:05 +0100 | [diff] [blame] | 108 | |
| 109 | if (where == PCI_BASE_ADDRESS_0 && size == 4 && |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 110 | apc->bar0_is_cached) { |
Gabor Juhos | 6015a85 | 2012-03-14 10:36:05 +0100 | [diff] [blame] | 111 | /* use the cached value */ |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 112 | *value = apc->bar0_value; |
Gabor Juhos | 6015a85 | 2012-03-14 10:36:05 +0100 | [diff] [blame] | 113 | } else { |
| 114 | *value = data; |
| 115 | } |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 116 | |
| 117 | return PCIBIOS_SUCCESSFUL; |
| 118 | } |
| 119 | |
Gabor Juhos | d624bd3 | 2012-03-14 10:29:26 +0100 | [diff] [blame] | 120 | static int ar724x_pci_write(struct pci_bus *bus, unsigned int devfn, int where, |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 121 | int size, uint32_t value) |
| 122 | { |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 123 | struct ar724x_pci_controller *apc; |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 124 | unsigned long flags; |
Gabor Juhos | c198441 | 2012-03-14 10:29:27 +0100 | [diff] [blame] | 125 | void __iomem *base; |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 126 | u32 data; |
| 127 | int s; |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 128 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 129 | apc = pci_bus_to_ar724x_controller(bus); |
| 130 | if (!apc->link_up) |
Gabor Juhos | a1dca31 | 2012-08-23 15:35:26 +0200 | [diff] [blame] | 131 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 132 | |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 133 | if (devfn) |
| 134 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 135 | |
Gabor Juhos | 6015a85 | 2012-03-14 10:36:05 +0100 | [diff] [blame] | 136 | if (soc_is_ar7240() && where == PCI_BASE_ADDRESS_0 && size == 4) { |
| 137 | if (value != 0xffffffff) { |
| 138 | /* |
| 139 | * WAR for a hw issue. If the BAR0 register of the |
| 140 | * device is set to the proper base address, the |
| 141 | * memory space of the device is not accessible. |
| 142 | * |
| 143 | * Cache the intended value so it can be read back, |
| 144 | * and write a SoC specific constant value to the |
| 145 | * BAR0 register in order to make the device memory |
| 146 | * accessible. |
| 147 | */ |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 148 | apc->bar0_is_cached = true; |
| 149 | apc->bar0_value = value; |
Gabor Juhos | 6015a85 | 2012-03-14 10:36:05 +0100 | [diff] [blame] | 150 | |
| 151 | value = AR7240_BAR0_WAR_VALUE; |
| 152 | } else { |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 153 | apc->bar0_is_cached = false; |
Gabor Juhos | 6015a85 | 2012-03-14 10:36:05 +0100 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 157 | base = apc->devcfg_base; |
Gabor Juhos | c198441 | 2012-03-14 10:29:27 +0100 | [diff] [blame] | 158 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 159 | spin_lock_irqsave(&apc->lock, flags); |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 160 | data = __raw_readl(base + (where & ~3)); |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 161 | |
| 162 | switch (size) { |
| 163 | case 1: |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 164 | s = ((where & 3) * 8); |
| 165 | data &= ~(0xff << s); |
| 166 | data |= ((value & 0xff) << s); |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 167 | break; |
| 168 | case 2: |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 169 | s = ((where & 2) * 8); |
| 170 | data &= ~(0xffff << s); |
| 171 | data |= ((value & 0xffff) << s); |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 172 | break; |
| 173 | case 4: |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 174 | data = value; |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 175 | break; |
| 176 | default: |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 177 | spin_unlock_irqrestore(&apc->lock, flags); |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 178 | |
| 179 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 180 | } |
| 181 | |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 182 | __raw_writel(data, base + (where & ~3)); |
| 183 | /* flush write */ |
| 184 | __raw_readl(base + (where & ~3)); |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 185 | spin_unlock_irqrestore(&apc->lock, flags); |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 186 | |
| 187 | return PCIBIOS_SUCCESSFUL; |
| 188 | } |
| 189 | |
Gabor Juhos | d624bd3 | 2012-03-14 10:29:26 +0100 | [diff] [blame] | 190 | static struct pci_ops ar724x_pci_ops = { |
| 191 | .read = ar724x_pci_read, |
| 192 | .write = ar724x_pci_write, |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 193 | }; |
| 194 | |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 195 | static void ar724x_pci_irq_handler(unsigned int irq, struct irq_desc *desc) |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 196 | { |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 197 | struct ar724x_pci_controller *apc; |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 198 | void __iomem *base; |
| 199 | u32 pending; |
| 200 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 201 | apc = irq_get_handler_data(irq); |
| 202 | base = apc->ctrl_base; |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 203 | |
| 204 | pending = __raw_readl(base + AR724X_PCI_REG_INT_STATUS) & |
| 205 | __raw_readl(base + AR724X_PCI_REG_INT_MASK); |
| 206 | |
| 207 | if (pending & AR724X_PCI_INT_DEV0) |
| 208 | generic_handle_irq(ATH79_PCI_IRQ(0)); |
| 209 | |
| 210 | else |
| 211 | spurious_interrupt(); |
| 212 | } |
| 213 | |
| 214 | static void ar724x_pci_irq_unmask(struct irq_data *d) |
| 215 | { |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 216 | struct ar724x_pci_controller *apc; |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 217 | void __iomem *base; |
| 218 | u32 t; |
| 219 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 220 | apc = irq_data_get_irq_chip_data(d); |
| 221 | base = apc->ctrl_base; |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 222 | |
| 223 | switch (d->irq) { |
| 224 | case ATH79_PCI_IRQ(0): |
| 225 | t = __raw_readl(base + AR724X_PCI_REG_INT_MASK); |
| 226 | __raw_writel(t | AR724X_PCI_INT_DEV0, |
| 227 | base + AR724X_PCI_REG_INT_MASK); |
| 228 | /* flush write */ |
| 229 | __raw_readl(base + AR724X_PCI_REG_INT_MASK); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | static void ar724x_pci_irq_mask(struct irq_data *d) |
| 234 | { |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 235 | struct ar724x_pci_controller *apc; |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 236 | void __iomem *base; |
| 237 | u32 t; |
| 238 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 239 | apc = irq_data_get_irq_chip_data(d); |
| 240 | base = apc->ctrl_base; |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 241 | |
| 242 | switch (d->irq) { |
| 243 | case ATH79_PCI_IRQ(0): |
| 244 | t = __raw_readl(base + AR724X_PCI_REG_INT_MASK); |
| 245 | __raw_writel(t & ~AR724X_PCI_INT_DEV0, |
| 246 | base + AR724X_PCI_REG_INT_MASK); |
| 247 | |
| 248 | /* flush write */ |
| 249 | __raw_readl(base + AR724X_PCI_REG_INT_MASK); |
| 250 | |
| 251 | t = __raw_readl(base + AR724X_PCI_REG_INT_STATUS); |
| 252 | __raw_writel(t | AR724X_PCI_INT_DEV0, |
| 253 | base + AR724X_PCI_REG_INT_STATUS); |
| 254 | |
| 255 | /* flush write */ |
| 256 | __raw_readl(base + AR724X_PCI_REG_INT_STATUS); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | static struct irq_chip ar724x_pci_irq_chip = { |
| 261 | .name = "AR724X PCI ", |
| 262 | .irq_mask = ar724x_pci_irq_mask, |
| 263 | .irq_unmask = ar724x_pci_irq_unmask, |
| 264 | .irq_mask_ack = ar724x_pci_irq_mask, |
| 265 | }; |
| 266 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 267 | static void ar724x_pci_irq_init(struct ar724x_pci_controller *apc) |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 268 | { |
| 269 | void __iomem *base; |
| 270 | int i; |
| 271 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 272 | base = apc->ctrl_base; |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 273 | |
| 274 | __raw_writel(0, base + AR724X_PCI_REG_INT_MASK); |
| 275 | __raw_writel(0, base + AR724X_PCI_REG_INT_STATUS); |
| 276 | |
| 277 | BUILD_BUG_ON(ATH79_PCI_IRQ_COUNT < AR724X_PCI_IRQ_COUNT); |
| 278 | |
| 279 | for (i = ATH79_PCI_IRQ_BASE; |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 280 | i < ATH79_PCI_IRQ_BASE + AR724X_PCI_IRQ_COUNT; i++) { |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 281 | irq_set_chip_and_handler(i, &ar724x_pci_irq_chip, |
| 282 | handle_level_irq); |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 283 | irq_set_chip_data(i, apc); |
| 284 | } |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 285 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 286 | irq_set_handler_data(apc->irq, apc); |
| 287 | irq_set_chained_handler(apc->irq, ar724x_pci_irq_handler); |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 288 | } |
| 289 | |
Gabor Juhos | 58d2e9b | 2013-02-02 11:40:42 +0000 | [diff] [blame] | 290 | static int ar724x_pci_probe(struct platform_device *pdev) |
| 291 | { |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 292 | struct ar724x_pci_controller *apc; |
Gabor Juhos | 58d2e9b | 2013-02-02 11:40:42 +0000 | [diff] [blame] | 293 | struct resource *res; |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 294 | |
| 295 | apc = devm_kzalloc(&pdev->dev, sizeof(struct ar724x_pci_controller), |
| 296 | GFP_KERNEL); |
| 297 | if (!apc) |
| 298 | return -ENOMEM; |
Gabor Juhos | 58d2e9b | 2013-02-02 11:40:42 +0000 | [diff] [blame] | 299 | |
| 300 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ctrl_base"); |
| 301 | if (!res) |
| 302 | return -EINVAL; |
| 303 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 304 | apc->ctrl_base = devm_request_and_ioremap(&pdev->dev, res); |
| 305 | if (apc->ctrl_base == NULL) |
Gabor Juhos | 58d2e9b | 2013-02-02 11:40:42 +0000 | [diff] [blame] | 306 | return -EBUSY; |
| 307 | |
| 308 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg_base"); |
| 309 | if (!res) |
| 310 | return -EINVAL; |
| 311 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 312 | apc->devcfg_base = devm_request_and_ioremap(&pdev->dev, res); |
| 313 | if (!apc->devcfg_base) |
Gabor Juhos | 58d2e9b | 2013-02-02 11:40:42 +0000 | [diff] [blame] | 314 | return -EBUSY; |
| 315 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 316 | apc->irq = platform_get_irq(pdev, 0); |
| 317 | if (apc->irq < 0) |
Gabor Juhos | 58d2e9b | 2013-02-02 11:40:42 +0000 | [diff] [blame] | 318 | return -EINVAL; |
| 319 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 320 | spin_lock_init(&apc->lock); |
| 321 | |
Gabor Juhos | 34b134a | 2013-02-03 09:59:45 +0000 | [diff] [blame^] | 322 | res = platform_get_resource_byname(pdev, IORESOURCE_IO, "io_base"); |
| 323 | if (!res) |
| 324 | return -EINVAL; |
| 325 | |
| 326 | apc->io_res.parent = res; |
| 327 | apc->io_res.name = "PCI IO space"; |
| 328 | apc->io_res.start = res->start; |
| 329 | apc->io_res.end = res->end; |
| 330 | apc->io_res.flags = IORESOURCE_IO; |
| 331 | |
| 332 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem_base"); |
| 333 | if (!res) |
| 334 | return -EINVAL; |
| 335 | |
| 336 | apc->mem_res.parent = res; |
| 337 | apc->mem_res.name = "PCI memory space"; |
| 338 | apc->mem_res.start = res->start; |
| 339 | apc->mem_res.end = res->end; |
| 340 | apc->mem_res.flags = IORESOURCE_MEM; |
| 341 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 342 | apc->pci_controller.pci_ops = &ar724x_pci_ops; |
Gabor Juhos | 34b134a | 2013-02-03 09:59:45 +0000 | [diff] [blame^] | 343 | apc->pci_controller.io_resource = &apc->io_res; |
| 344 | apc->pci_controller.mem_resource = &apc->mem_res; |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 345 | |
| 346 | apc->link_up = ar724x_pci_check_link(apc); |
| 347 | if (!apc->link_up) |
Gabor Juhos | 58d2e9b | 2013-02-02 11:40:42 +0000 | [diff] [blame] | 348 | dev_warn(&pdev->dev, "PCIe link is down\n"); |
| 349 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 350 | ar724x_pci_irq_init(apc); |
Gabor Juhos | 58d2e9b | 2013-02-02 11:40:42 +0000 | [diff] [blame] | 351 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 352 | register_pci_controller(&apc->pci_controller); |
Gabor Juhos | 58d2e9b | 2013-02-02 11:40:42 +0000 | [diff] [blame] | 353 | |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | static struct platform_driver ar724x_pci_driver = { |
| 358 | .probe = ar724x_pci_probe, |
| 359 | .driver = { |
| 360 | .name = "ar724x-pci", |
| 361 | .owner = THIS_MODULE, |
| 362 | }, |
| 363 | }; |
| 364 | |
| 365 | static int __init ar724x_pci_init(void) |
| 366 | { |
| 367 | return platform_driver_register(&ar724x_pci_driver); |
| 368 | } |
| 369 | |
| 370 | postcore_initcall(ar724x_pci_init); |