Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 2 | /* |
Gabor Juhos | e9b62e8 | 2012-03-14 10:36:14 +0100 | [diff] [blame] | 3 | * Atheros AR724X PCI host controller driver |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2011 René Bolldorf <xsecute@googlemail.com> |
Gabor Juhos | e9b62e8 | 2012-03-14 10:36:14 +0100 | [diff] [blame] | 6 | * Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org> |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 9 | #include <linux/irq.h> |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 10 | #include <linux/pci.h> |
Paul Gortmaker | 2722090 | 2016-08-21 15:58:16 -0400 | [diff] [blame] | 11 | #include <linux/init.h> |
Mathias Kresin | 0316b05 | 2018-07-20 13:58:25 +0200 | [diff] [blame] | 12 | #include <linux/delay.h> |
Gabor Juhos | 58d2e9b | 2013-02-02 11:40:42 +0000 | [diff] [blame] | 13 | #include <linux/platform_device.h> |
Gabor Juhos | 6015a85 | 2012-03-14 10:36:05 +0100 | [diff] [blame] | 14 | #include <asm/mach-ath79/ath79.h> |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 15 | #include <asm/mach-ath79/ar71xx_regs.h> |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 16 | |
Mathias Kresin | 0316b05 | 2018-07-20 13:58:25 +0200 | [diff] [blame] | 17 | #define AR724X_PCI_REG_APP 0x00 |
Gabor Juhos | a1dca31 | 2012-08-23 15:35:26 +0200 | [diff] [blame] | 18 | #define AR724X_PCI_REG_RESET 0x18 |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 19 | #define AR724X_PCI_REG_INT_STATUS 0x4c |
| 20 | #define AR724X_PCI_REG_INT_MASK 0x50 |
| 21 | |
Mathias Kresin | 0316b05 | 2018-07-20 13:58:25 +0200 | [diff] [blame] | 22 | #define AR724X_PCI_APP_LTSSM_ENABLE BIT(0) |
| 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 | 12401fc | 2013-02-03 14:52:47 +0000 | [diff] [blame] | 32 | #define AR724X_PCI_CMD_INIT (PCI_COMMAND_MEMORY | \ |
| 33 | PCI_COMMAND_MASTER | \ |
| 34 | PCI_COMMAND_INVALIDATE | \ |
| 35 | PCI_COMMAND_PARITY | \ |
| 36 | PCI_COMMAND_SERR | \ |
| 37 | PCI_COMMAND_FAST_BACK) |
| 38 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 39 | struct ar724x_pci_controller { |
| 40 | void __iomem *devcfg_base; |
| 41 | void __iomem *ctrl_base; |
Gabor Juhos | 12401fc | 2013-02-03 14:52:47 +0000 | [diff] [blame] | 42 | void __iomem *crp_base; |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 43 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 44 | int irq; |
Gabor Juhos | 8b66d46 | 2013-02-03 10:00:16 +0000 | [diff] [blame] | 45 | int irq_base; |
Gabor Juhos | a1dca31 | 2012-08-23 15:35:26 +0200 | [diff] [blame] | 46 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 47 | bool link_up; |
| 48 | bool bar0_is_cached; |
| 49 | u32 bar0_value; |
| 50 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 51 | struct pci_controller pci_controller; |
Gabor Juhos | 34b134a | 2013-02-03 09:59:45 +0000 | [diff] [blame] | 52 | struct resource io_res; |
| 53 | struct resource mem_res; |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | static inline bool ar724x_pci_check_link(struct ar724x_pci_controller *apc) |
Gabor Juhos | a1dca31 | 2012-08-23 15:35:26 +0200 | [diff] [blame] | 57 | { |
| 58 | u32 reset; |
| 59 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 60 | reset = __raw_readl(apc->ctrl_base + AR724X_PCI_REG_RESET); |
Gabor Juhos | a1dca31 | 2012-08-23 15:35:26 +0200 | [diff] [blame] | 61 | return reset & AR724X_PCI_RESET_LINK_UP; |
| 62 | } |
Gabor Juhos | 6015a85 | 2012-03-14 10:36:05 +0100 | [diff] [blame] | 63 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 64 | static inline struct ar724x_pci_controller * |
| 65 | pci_bus_to_ar724x_controller(struct pci_bus *bus) |
| 66 | { |
| 67 | struct pci_controller *hose; |
| 68 | |
| 69 | hose = (struct pci_controller *) bus->sysdata; |
| 70 | return container_of(hose, struct ar724x_pci_controller, pci_controller); |
| 71 | } |
| 72 | |
Gabor Juhos | 12401fc | 2013-02-03 14:52:47 +0000 | [diff] [blame] | 73 | static int ar724x_pci_local_write(struct ar724x_pci_controller *apc, |
| 74 | int where, int size, u32 value) |
| 75 | { |
Gabor Juhos | 12401fc | 2013-02-03 14:52:47 +0000 | [diff] [blame] | 76 | void __iomem *base; |
| 77 | u32 data; |
| 78 | int s; |
| 79 | |
| 80 | WARN_ON(where & (size - 1)); |
| 81 | |
| 82 | if (!apc->link_up) |
| 83 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 84 | |
| 85 | base = apc->crp_base; |
Gabor Juhos | 12401fc | 2013-02-03 14:52:47 +0000 | [diff] [blame] | 86 | data = __raw_readl(base + (where & ~3)); |
| 87 | |
| 88 | switch (size) { |
| 89 | case 1: |
| 90 | s = ((where & 3) * 8); |
| 91 | data &= ~(0xff << s); |
| 92 | data |= ((value & 0xff) << s); |
| 93 | break; |
| 94 | case 2: |
| 95 | s = ((where & 2) * 8); |
| 96 | data &= ~(0xffff << s); |
| 97 | data |= ((value & 0xffff) << s); |
| 98 | break; |
| 99 | case 4: |
| 100 | data = value; |
| 101 | break; |
| 102 | default: |
Gabor Juhos | 12401fc | 2013-02-03 14:52:47 +0000 | [diff] [blame] | 103 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 104 | } |
| 105 | |
| 106 | __raw_writel(data, base + (where & ~3)); |
| 107 | /* flush write */ |
| 108 | __raw_readl(base + (where & ~3)); |
Gabor Juhos | 12401fc | 2013-02-03 14:52:47 +0000 | [diff] [blame] | 109 | |
| 110 | return PCIBIOS_SUCCESSFUL; |
| 111 | } |
| 112 | |
Gabor Juhos | d624bd3 | 2012-03-14 10:29:26 +0100 | [diff] [blame] | 113 | 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] | 114 | int size, uint32_t *value) |
| 115 | { |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 116 | struct ar724x_pci_controller *apc; |
Gabor Juhos | c198441 | 2012-03-14 10:29:27 +0100 | [diff] [blame] | 117 | void __iomem *base; |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 118 | u32 data; |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 119 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 120 | apc = pci_bus_to_ar724x_controller(bus); |
| 121 | if (!apc->link_up) |
Gabor Juhos | a1dca31 | 2012-08-23 15:35:26 +0200 | [diff] [blame] | 122 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 123 | |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 124 | if (devfn) |
| 125 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 126 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 127 | base = apc->devcfg_base; |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 128 | data = __raw_readl(base + (where & ~3)); |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 129 | |
| 130 | switch (size) { |
| 131 | case 1: |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 132 | if (where & 1) |
| 133 | data >>= 8; |
| 134 | if (where & 2) |
| 135 | data >>= 16; |
| 136 | data &= 0xff; |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 137 | break; |
| 138 | case 2: |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 139 | if (where & 2) |
| 140 | data >>= 16; |
| 141 | data &= 0xffff; |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 142 | break; |
| 143 | case 4: |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 144 | break; |
| 145 | default: |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 146 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 147 | } |
| 148 | |
Gabor Juhos | 6015a85 | 2012-03-14 10:36:05 +0100 | [diff] [blame] | 149 | if (where == PCI_BASE_ADDRESS_0 && size == 4 && |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 150 | apc->bar0_is_cached) { |
Gabor Juhos | 6015a85 | 2012-03-14 10:36:05 +0100 | [diff] [blame] | 151 | /* use the cached value */ |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 152 | *value = apc->bar0_value; |
Gabor Juhos | 6015a85 | 2012-03-14 10:36:05 +0100 | [diff] [blame] | 153 | } else { |
| 154 | *value = data; |
| 155 | } |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 156 | |
| 157 | return PCIBIOS_SUCCESSFUL; |
| 158 | } |
| 159 | |
Gabor Juhos | d624bd3 | 2012-03-14 10:29:26 +0100 | [diff] [blame] | 160 | 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] | 161 | int size, uint32_t value) |
| 162 | { |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 163 | struct ar724x_pci_controller *apc; |
Gabor Juhos | c198441 | 2012-03-14 10:29:27 +0100 | [diff] [blame] | 164 | void __iomem *base; |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 165 | u32 data; |
| 166 | int s; |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 167 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 168 | apc = pci_bus_to_ar724x_controller(bus); |
| 169 | if (!apc->link_up) |
Gabor Juhos | a1dca31 | 2012-08-23 15:35:26 +0200 | [diff] [blame] | 170 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 171 | |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 172 | if (devfn) |
| 173 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 174 | |
Gabor Juhos | 6015a85 | 2012-03-14 10:36:05 +0100 | [diff] [blame] | 175 | if (soc_is_ar7240() && where == PCI_BASE_ADDRESS_0 && size == 4) { |
| 176 | if (value != 0xffffffff) { |
| 177 | /* |
| 178 | * WAR for a hw issue. If the BAR0 register of the |
| 179 | * device is set to the proper base address, the |
| 180 | * memory space of the device is not accessible. |
| 181 | * |
| 182 | * Cache the intended value so it can be read back, |
| 183 | * and write a SoC specific constant value to the |
| 184 | * BAR0 register in order to make the device memory |
| 185 | * accessible. |
| 186 | */ |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 187 | apc->bar0_is_cached = true; |
| 188 | apc->bar0_value = value; |
Gabor Juhos | 6015a85 | 2012-03-14 10:36:05 +0100 | [diff] [blame] | 189 | |
| 190 | value = AR7240_BAR0_WAR_VALUE; |
| 191 | } else { |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 192 | apc->bar0_is_cached = false; |
Gabor Juhos | 6015a85 | 2012-03-14 10:36:05 +0100 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 196 | base = apc->devcfg_base; |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 197 | data = __raw_readl(base + (where & ~3)); |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 198 | |
| 199 | switch (size) { |
| 200 | case 1: |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 201 | s = ((where & 3) * 8); |
| 202 | data &= ~(0xff << s); |
| 203 | data |= ((value & 0xff) << s); |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 204 | break; |
| 205 | case 2: |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 206 | s = ((where & 2) * 8); |
| 207 | data &= ~(0xffff << s); |
| 208 | data |= ((value & 0xffff) << s); |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 209 | break; |
| 210 | case 4: |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 211 | data = value; |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 212 | break; |
| 213 | default: |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 214 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 215 | } |
| 216 | |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 217 | __raw_writel(data, base + (where & ~3)); |
| 218 | /* flush write */ |
| 219 | __raw_readl(base + (where & ~3)); |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 220 | |
| 221 | return PCIBIOS_SUCCESSFUL; |
| 222 | } |
| 223 | |
Gabor Juhos | d624bd3 | 2012-03-14 10:29:26 +0100 | [diff] [blame] | 224 | static struct pci_ops ar724x_pci_ops = { |
| 225 | .read = ar724x_pci_read, |
| 226 | .write = ar724x_pci_write, |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 227 | }; |
| 228 | |
Thomas Gleixner | bd0b9ac | 2015-09-14 10:42:37 +0200 | [diff] [blame] | 229 | static void ar724x_pci_irq_handler(struct irq_desc *desc) |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 230 | { |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 231 | struct ar724x_pci_controller *apc; |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 232 | void __iomem *base; |
| 233 | u32 pending; |
| 234 | |
Jiang Liu | 25aae56 | 2015-05-20 17:59:51 +0800 | [diff] [blame] | 235 | apc = irq_desc_get_handler_data(desc); |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 236 | base = apc->ctrl_base; |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 237 | |
| 238 | pending = __raw_readl(base + AR724X_PCI_REG_INT_STATUS) & |
| 239 | __raw_readl(base + AR724X_PCI_REG_INT_MASK); |
| 240 | |
| 241 | if (pending & AR724X_PCI_INT_DEV0) |
Gabor Juhos | 8b66d46 | 2013-02-03 10:00:16 +0000 | [diff] [blame] | 242 | generic_handle_irq(apc->irq_base + 0); |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 243 | |
| 244 | else |
| 245 | spurious_interrupt(); |
| 246 | } |
| 247 | |
| 248 | static void ar724x_pci_irq_unmask(struct irq_data *d) |
| 249 | { |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 250 | struct ar724x_pci_controller *apc; |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 251 | void __iomem *base; |
Gabor Juhos | 8b66d46 | 2013-02-03 10:00:16 +0000 | [diff] [blame] | 252 | int offset; |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 253 | u32 t; |
| 254 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 255 | apc = irq_data_get_irq_chip_data(d); |
| 256 | base = apc->ctrl_base; |
Gabor Juhos | 8b66d46 | 2013-02-03 10:00:16 +0000 | [diff] [blame] | 257 | offset = apc->irq_base - d->irq; |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 258 | |
Gabor Juhos | 8b66d46 | 2013-02-03 10:00:16 +0000 | [diff] [blame] | 259 | switch (offset) { |
| 260 | case 0: |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 261 | t = __raw_readl(base + AR724X_PCI_REG_INT_MASK); |
| 262 | __raw_writel(t | AR724X_PCI_INT_DEV0, |
| 263 | base + AR724X_PCI_REG_INT_MASK); |
| 264 | /* flush write */ |
| 265 | __raw_readl(base + AR724X_PCI_REG_INT_MASK); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | static void ar724x_pci_irq_mask(struct irq_data *d) |
| 270 | { |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 271 | struct ar724x_pci_controller *apc; |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 272 | void __iomem *base; |
Gabor Juhos | 8b66d46 | 2013-02-03 10:00:16 +0000 | [diff] [blame] | 273 | int offset; |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 274 | u32 t; |
| 275 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 276 | apc = irq_data_get_irq_chip_data(d); |
| 277 | base = apc->ctrl_base; |
Gabor Juhos | 8b66d46 | 2013-02-03 10:00:16 +0000 | [diff] [blame] | 278 | offset = apc->irq_base - d->irq; |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 279 | |
Gabor Juhos | 8b66d46 | 2013-02-03 10:00:16 +0000 | [diff] [blame] | 280 | switch (offset) { |
| 281 | case 0: |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 282 | t = __raw_readl(base + AR724X_PCI_REG_INT_MASK); |
| 283 | __raw_writel(t & ~AR724X_PCI_INT_DEV0, |
| 284 | base + AR724X_PCI_REG_INT_MASK); |
| 285 | |
| 286 | /* flush write */ |
| 287 | __raw_readl(base + AR724X_PCI_REG_INT_MASK); |
| 288 | |
| 289 | t = __raw_readl(base + AR724X_PCI_REG_INT_STATUS); |
| 290 | __raw_writel(t | AR724X_PCI_INT_DEV0, |
| 291 | base + AR724X_PCI_REG_INT_STATUS); |
| 292 | |
| 293 | /* flush write */ |
| 294 | __raw_readl(base + AR724X_PCI_REG_INT_STATUS); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | static struct irq_chip ar724x_pci_irq_chip = { |
| 299 | .name = "AR724X PCI ", |
| 300 | .irq_mask = ar724x_pci_irq_mask, |
| 301 | .irq_unmask = ar724x_pci_irq_unmask, |
| 302 | .irq_mask_ack = ar724x_pci_irq_mask, |
| 303 | }; |
| 304 | |
Gabor Juhos | 8b66d46 | 2013-02-03 10:00:16 +0000 | [diff] [blame] | 305 | static void ar724x_pci_irq_init(struct ar724x_pci_controller *apc, |
| 306 | int id) |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 307 | { |
| 308 | void __iomem *base; |
| 309 | int i; |
| 310 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 311 | base = apc->ctrl_base; |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 312 | |
| 313 | __raw_writel(0, base + AR724X_PCI_REG_INT_MASK); |
| 314 | __raw_writel(0, base + AR724X_PCI_REG_INT_STATUS); |
| 315 | |
Gabor Juhos | 8b66d46 | 2013-02-03 10:00:16 +0000 | [diff] [blame] | 316 | apc->irq_base = ATH79_PCI_IRQ_BASE + (id * AR724X_PCI_IRQ_COUNT); |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 317 | |
Gabor Juhos | 8b66d46 | 2013-02-03 10:00:16 +0000 | [diff] [blame] | 318 | for (i = apc->irq_base; |
| 319 | i < apc->irq_base + AR724X_PCI_IRQ_COUNT; i++) { |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 320 | irq_set_chip_and_handler(i, &ar724x_pci_irq_chip, |
| 321 | handle_level_irq); |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 322 | irq_set_chip_data(i, apc); |
| 323 | } |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 324 | |
Thomas Gleixner | 4d3f77d | 2015-07-13 20:45:56 +0000 | [diff] [blame] | 325 | irq_set_chained_handler_and_data(apc->irq, ar724x_pci_irq_handler, |
| 326 | apc); |
Gabor Juhos | 4c07c7d | 2012-03-14 10:36:07 +0100 | [diff] [blame] | 327 | } |
| 328 | |
Mathias Kresin | 0316b05 | 2018-07-20 13:58:25 +0200 | [diff] [blame] | 329 | static void ar724x_pci_hw_init(struct ar724x_pci_controller *apc) |
| 330 | { |
| 331 | u32 ppl, app; |
| 332 | int wait = 0; |
| 333 | |
| 334 | /* deassert PCIe host controller and PCIe PHY reset */ |
| 335 | ath79_device_reset_clear(AR724X_RESET_PCIE); |
| 336 | ath79_device_reset_clear(AR724X_RESET_PCIE_PHY); |
| 337 | |
| 338 | /* remove the reset of the PCIE PLL */ |
| 339 | ppl = ath79_pll_rr(AR724X_PLL_REG_PCIE_CONFIG); |
| 340 | ppl &= ~AR724X_PLL_REG_PCIE_CONFIG_PPL_RESET; |
| 341 | ath79_pll_wr(AR724X_PLL_REG_PCIE_CONFIG, ppl); |
| 342 | |
| 343 | /* deassert bypass for the PCIE PLL */ |
| 344 | ppl = ath79_pll_rr(AR724X_PLL_REG_PCIE_CONFIG); |
| 345 | ppl &= ~AR724X_PLL_REG_PCIE_CONFIG_PPL_BYPASS; |
| 346 | ath79_pll_wr(AR724X_PLL_REG_PCIE_CONFIG, ppl); |
| 347 | |
| 348 | /* set PCIE Application Control to ready */ |
| 349 | app = __raw_readl(apc->ctrl_base + AR724X_PCI_REG_APP); |
| 350 | app |= AR724X_PCI_APP_LTSSM_ENABLE; |
| 351 | __raw_writel(app, apc->ctrl_base + AR724X_PCI_REG_APP); |
| 352 | |
| 353 | /* wait up to 100ms for PHY link up */ |
| 354 | do { |
| 355 | mdelay(10); |
| 356 | wait++; |
| 357 | } while (wait < 10 && !ar724x_pci_check_link(apc)); |
| 358 | } |
| 359 | |
Gabor Juhos | 58d2e9b | 2013-02-02 11:40:42 +0000 | [diff] [blame] | 360 | static int ar724x_pci_probe(struct platform_device *pdev) |
| 361 | { |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 362 | struct ar724x_pci_controller *apc; |
Gabor Juhos | 58d2e9b | 2013-02-02 11:40:42 +0000 | [diff] [blame] | 363 | struct resource *res; |
Gabor Juhos | 8b66d46 | 2013-02-03 10:00:16 +0000 | [diff] [blame] | 364 | int id; |
| 365 | |
| 366 | id = pdev->id; |
| 367 | if (id == -1) |
| 368 | id = 0; |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 369 | |
| 370 | apc = devm_kzalloc(&pdev->dev, sizeof(struct ar724x_pci_controller), |
| 371 | GFP_KERNEL); |
| 372 | if (!apc) |
| 373 | return -ENOMEM; |
Gabor Juhos | 58d2e9b | 2013-02-02 11:40:42 +0000 | [diff] [blame] | 374 | |
| 375 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ctrl_base"); |
Silviu-Mihai Popescu | f560fab | 2013-03-12 10:30:05 +0000 | [diff] [blame] | 376 | apc->ctrl_base = devm_ioremap_resource(&pdev->dev, res); |
| 377 | if (IS_ERR(apc->ctrl_base)) |
| 378 | return PTR_ERR(apc->ctrl_base); |
Gabor Juhos | 58d2e9b | 2013-02-02 11:40:42 +0000 | [diff] [blame] | 379 | |
| 380 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg_base"); |
Silviu-Mihai Popescu | f560fab | 2013-03-12 10:30:05 +0000 | [diff] [blame] | 381 | apc->devcfg_base = devm_ioremap_resource(&pdev->dev, res); |
| 382 | if (IS_ERR(apc->devcfg_base)) |
| 383 | return PTR_ERR(apc->devcfg_base); |
Gabor Juhos | 58d2e9b | 2013-02-02 11:40:42 +0000 | [diff] [blame] | 384 | |
Gabor Juhos | 12401fc | 2013-02-03 14:52:47 +0000 | [diff] [blame] | 385 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "crp_base"); |
Silviu-Mihai Popescu | f560fab | 2013-03-12 10:30:05 +0000 | [diff] [blame] | 386 | apc->crp_base = devm_ioremap_resource(&pdev->dev, res); |
| 387 | if (IS_ERR(apc->crp_base)) |
| 388 | return PTR_ERR(apc->crp_base); |
Gabor Juhos | 12401fc | 2013-02-03 14:52:47 +0000 | [diff] [blame] | 389 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 390 | apc->irq = platform_get_irq(pdev, 0); |
| 391 | if (apc->irq < 0) |
Gabor Juhos | 58d2e9b | 2013-02-02 11:40:42 +0000 | [diff] [blame] | 392 | return -EINVAL; |
| 393 | |
Gabor Juhos | 34b134a | 2013-02-03 09:59:45 +0000 | [diff] [blame] | 394 | res = platform_get_resource_byname(pdev, IORESOURCE_IO, "io_base"); |
| 395 | if (!res) |
| 396 | return -EINVAL; |
| 397 | |
| 398 | apc->io_res.parent = res; |
| 399 | apc->io_res.name = "PCI IO space"; |
| 400 | apc->io_res.start = res->start; |
| 401 | apc->io_res.end = res->end; |
| 402 | apc->io_res.flags = IORESOURCE_IO; |
| 403 | |
| 404 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem_base"); |
| 405 | if (!res) |
| 406 | return -EINVAL; |
| 407 | |
| 408 | apc->mem_res.parent = res; |
| 409 | apc->mem_res.name = "PCI memory space"; |
| 410 | apc->mem_res.start = res->start; |
| 411 | apc->mem_res.end = res->end; |
| 412 | apc->mem_res.flags = IORESOURCE_MEM; |
| 413 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 414 | apc->pci_controller.pci_ops = &ar724x_pci_ops; |
Gabor Juhos | 34b134a | 2013-02-03 09:59:45 +0000 | [diff] [blame] | 415 | apc->pci_controller.io_resource = &apc->io_res; |
| 416 | apc->pci_controller.mem_resource = &apc->mem_res; |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 417 | |
Mathias Kresin | 0316b05 | 2018-07-20 13:58:25 +0200 | [diff] [blame] | 418 | /* |
| 419 | * Do the full PCIE Root Complex Initialization Sequence if the PCIe |
| 420 | * host controller is in reset. |
| 421 | */ |
| 422 | if (ath79_reset_rr(AR724X_RESET_REG_RESET_MODULE) & AR724X_RESET_PCIE) |
| 423 | ar724x_pci_hw_init(apc); |
| 424 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 425 | apc->link_up = ar724x_pci_check_link(apc); |
| 426 | if (!apc->link_up) |
Gabor Juhos | 58d2e9b | 2013-02-02 11:40:42 +0000 | [diff] [blame] | 427 | dev_warn(&pdev->dev, "PCIe link is down\n"); |
| 428 | |
Gabor Juhos | 8b66d46 | 2013-02-03 10:00:16 +0000 | [diff] [blame] | 429 | ar724x_pci_irq_init(apc, id); |
Gabor Juhos | 58d2e9b | 2013-02-02 11:40:42 +0000 | [diff] [blame] | 430 | |
Gabor Juhos | 12401fc | 2013-02-03 14:52:47 +0000 | [diff] [blame] | 431 | ar724x_pci_local_write(apc, PCI_COMMAND, 4, AR724X_PCI_CMD_INIT); |
| 432 | |
Gabor Juhos | 908339e | 2013-02-03 09:58:38 +0000 | [diff] [blame] | 433 | register_pci_controller(&apc->pci_controller); |
Gabor Juhos | 58d2e9b | 2013-02-02 11:40:42 +0000 | [diff] [blame] | 434 | |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | static struct platform_driver ar724x_pci_driver = { |
| 439 | .probe = ar724x_pci_probe, |
| 440 | .driver = { |
| 441 | .name = "ar724x-pci", |
Gabor Juhos | 58d2e9b | 2013-02-02 11:40:42 +0000 | [diff] [blame] | 442 | }, |
| 443 | }; |
| 444 | |
| 445 | static int __init ar724x_pci_init(void) |
| 446 | { |
| 447 | return platform_driver_register(&ar724x_pci_driver); |
| 448 | } |
| 449 | |
| 450 | postcore_initcall(ar724x_pci_init); |