Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Atheros 724x PCI support |
| 3 | * |
| 4 | * Copyright (C) 2011 René Bolldorf <xsecute@googlemail.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License version 2 as published |
| 8 | * by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/pci.h> |
Gabor Juhos | 6015a85 | 2012-03-14 10:36:05 +0100 | [diff] [blame^] | 12 | #include <asm/mach-ath79/ath79.h> |
Gabor Juhos | 659243c | 2012-03-14 10:29:23 +0100 | [diff] [blame] | 13 | #include <asm/mach-ath79/pci.h> |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 14 | |
Gabor Juhos | c198441 | 2012-03-14 10:29:27 +0100 | [diff] [blame] | 15 | #define AR724X_PCI_CFG_BASE 0x14000000 |
| 16 | #define AR724X_PCI_CFG_SIZE 0x1000 |
Gabor Juhos | d624bd3 | 2012-03-14 10:29:26 +0100 | [diff] [blame] | 17 | #define AR724X_PCI_MEM_BASE 0x10000000 |
| 18 | #define AR724X_PCI_MEM_SIZE 0x08000000 |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 19 | |
Gabor Juhos | 6015a85 | 2012-03-14 10:36:05 +0100 | [diff] [blame^] | 20 | #define AR7240_BAR0_WAR_VALUE 0xffff |
| 21 | |
Gabor Juhos | d624bd3 | 2012-03-14 10:29:26 +0100 | [diff] [blame] | 22 | static DEFINE_SPINLOCK(ar724x_pci_lock); |
Gabor Juhos | c198441 | 2012-03-14 10:29:27 +0100 | [diff] [blame] | 23 | static void __iomem *ar724x_pci_devcfg_base; |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 24 | |
Gabor Juhos | 6015a85 | 2012-03-14 10:36:05 +0100 | [diff] [blame^] | 25 | static u32 ar724x_pci_bar0_value; |
| 26 | static bool ar724x_pci_bar0_is_cached; |
| 27 | |
Gabor Juhos | d624bd3 | 2012-03-14 10:29:26 +0100 | [diff] [blame] | 28 | 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] | 29 | int size, uint32_t *value) |
| 30 | { |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 31 | unsigned long flags; |
Gabor Juhos | c198441 | 2012-03-14 10:29:27 +0100 | [diff] [blame] | 32 | void __iomem *base; |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 33 | u32 data; |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 34 | |
| 35 | if (devfn) |
| 36 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 37 | |
Gabor Juhos | c198441 | 2012-03-14 10:29:27 +0100 | [diff] [blame] | 38 | base = ar724x_pci_devcfg_base; |
| 39 | |
Gabor Juhos | d624bd3 | 2012-03-14 10:29:26 +0100 | [diff] [blame] | 40 | spin_lock_irqsave(&ar724x_pci_lock, flags); |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 41 | data = __raw_readl(base + (where & ~3)); |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 42 | |
| 43 | switch (size) { |
| 44 | case 1: |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 45 | if (where & 1) |
| 46 | data >>= 8; |
| 47 | if (where & 2) |
| 48 | data >>= 16; |
| 49 | data &= 0xff; |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 50 | break; |
| 51 | case 2: |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 52 | if (where & 2) |
| 53 | data >>= 16; |
| 54 | data &= 0xffff; |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 55 | break; |
| 56 | case 4: |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 57 | break; |
| 58 | default: |
Gabor Juhos | d624bd3 | 2012-03-14 10:29:26 +0100 | [diff] [blame] | 59 | spin_unlock_irqrestore(&ar724x_pci_lock, flags); |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 60 | |
| 61 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 62 | } |
| 63 | |
Gabor Juhos | d624bd3 | 2012-03-14 10:29:26 +0100 | [diff] [blame] | 64 | spin_unlock_irqrestore(&ar724x_pci_lock, flags); |
Gabor Juhos | 6015a85 | 2012-03-14 10:36:05 +0100 | [diff] [blame^] | 65 | |
| 66 | if (where == PCI_BASE_ADDRESS_0 && size == 4 && |
| 67 | ar724x_pci_bar0_is_cached) { |
| 68 | /* use the cached value */ |
| 69 | *value = ar724x_pci_bar0_value; |
| 70 | } else { |
| 71 | *value = data; |
| 72 | } |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 73 | |
| 74 | return PCIBIOS_SUCCESSFUL; |
| 75 | } |
| 76 | |
Gabor Juhos | d624bd3 | 2012-03-14 10:29:26 +0100 | [diff] [blame] | 77 | 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] | 78 | int size, uint32_t value) |
| 79 | { |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 80 | unsigned long flags; |
Gabor Juhos | c198441 | 2012-03-14 10:29:27 +0100 | [diff] [blame] | 81 | void __iomem *base; |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 82 | u32 data; |
| 83 | int s; |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 84 | |
| 85 | if (devfn) |
| 86 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 87 | |
Gabor Juhos | 6015a85 | 2012-03-14 10:36:05 +0100 | [diff] [blame^] | 88 | if (soc_is_ar7240() && where == PCI_BASE_ADDRESS_0 && size == 4) { |
| 89 | if (value != 0xffffffff) { |
| 90 | /* |
| 91 | * WAR for a hw issue. If the BAR0 register of the |
| 92 | * device is set to the proper base address, the |
| 93 | * memory space of the device is not accessible. |
| 94 | * |
| 95 | * Cache the intended value so it can be read back, |
| 96 | * and write a SoC specific constant value to the |
| 97 | * BAR0 register in order to make the device memory |
| 98 | * accessible. |
| 99 | */ |
| 100 | ar724x_pci_bar0_is_cached = true; |
| 101 | ar724x_pci_bar0_value = value; |
| 102 | |
| 103 | value = AR7240_BAR0_WAR_VALUE; |
| 104 | } else { |
| 105 | ar724x_pci_bar0_is_cached = false; |
| 106 | } |
| 107 | } |
| 108 | |
Gabor Juhos | c198441 | 2012-03-14 10:29:27 +0100 | [diff] [blame] | 109 | base = ar724x_pci_devcfg_base; |
| 110 | |
Gabor Juhos | d624bd3 | 2012-03-14 10:29:26 +0100 | [diff] [blame] | 111 | spin_lock_irqsave(&ar724x_pci_lock, flags); |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 112 | data = __raw_readl(base + (where & ~3)); |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 113 | |
| 114 | switch (size) { |
| 115 | case 1: |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 116 | s = ((where & 3) * 8); |
| 117 | data &= ~(0xff << s); |
| 118 | data |= ((value & 0xff) << s); |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 119 | break; |
| 120 | case 2: |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 121 | s = ((where & 2) * 8); |
| 122 | data &= ~(0xffff << s); |
| 123 | data |= ((value & 0xffff) << s); |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 124 | break; |
| 125 | case 4: |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 126 | data = value; |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 127 | break; |
| 128 | default: |
Gabor Juhos | d624bd3 | 2012-03-14 10:29:26 +0100 | [diff] [blame] | 129 | spin_unlock_irqrestore(&ar724x_pci_lock, flags); |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 130 | |
| 131 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 132 | } |
| 133 | |
Gabor Juhos | 64adb6b | 2012-03-14 10:36:04 +0100 | [diff] [blame] | 134 | __raw_writel(data, base + (where & ~3)); |
| 135 | /* flush write */ |
| 136 | __raw_readl(base + (where & ~3)); |
Gabor Juhos | d624bd3 | 2012-03-14 10:29:26 +0100 | [diff] [blame] | 137 | spin_unlock_irqrestore(&ar724x_pci_lock, flags); |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 138 | |
| 139 | return PCIBIOS_SUCCESSFUL; |
| 140 | } |
| 141 | |
Gabor Juhos | d624bd3 | 2012-03-14 10:29:26 +0100 | [diff] [blame] | 142 | static struct pci_ops ar724x_pci_ops = { |
| 143 | .read = ar724x_pci_read, |
| 144 | .write = ar724x_pci_write, |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
Gabor Juhos | d624bd3 | 2012-03-14 10:29:26 +0100 | [diff] [blame] | 147 | static struct resource ar724x_io_resource = { |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 148 | .name = "PCI IO space", |
| 149 | .start = 0, |
| 150 | .end = 0, |
| 151 | .flags = IORESOURCE_IO, |
| 152 | }; |
| 153 | |
Gabor Juhos | d624bd3 | 2012-03-14 10:29:26 +0100 | [diff] [blame] | 154 | static struct resource ar724x_mem_resource = { |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 155 | .name = "PCI memory space", |
Gabor Juhos | d624bd3 | 2012-03-14 10:29:26 +0100 | [diff] [blame] | 156 | .start = AR724X_PCI_MEM_BASE, |
| 157 | .end = AR724X_PCI_MEM_BASE + AR724X_PCI_MEM_SIZE - 1, |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 158 | .flags = IORESOURCE_MEM, |
| 159 | }; |
| 160 | |
Gabor Juhos | d624bd3 | 2012-03-14 10:29:26 +0100 | [diff] [blame] | 161 | static struct pci_controller ar724x_pci_controller = { |
| 162 | .pci_ops = &ar724x_pci_ops, |
| 163 | .io_resource = &ar724x_io_resource, |
| 164 | .mem_resource = &ar724x_mem_resource, |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 165 | }; |
| 166 | |
Gabor Juhos | d624bd3 | 2012-03-14 10:29:26 +0100 | [diff] [blame] | 167 | int __init ar724x_pcibios_init(void) |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 168 | { |
Gabor Juhos | c198441 | 2012-03-14 10:29:27 +0100 | [diff] [blame] | 169 | ar724x_pci_devcfg_base = ioremap(AR724X_PCI_CFG_BASE, |
| 170 | AR724X_PCI_CFG_SIZE); |
| 171 | if (ar724x_pci_devcfg_base == NULL) |
| 172 | return -ENOMEM; |
| 173 | |
Gabor Juhos | d624bd3 | 2012-03-14 10:29:26 +0100 | [diff] [blame] | 174 | register_pci_controller(&ar724x_pci_controller); |
Rene Bolldorf | 4ff40d5 | 2011-11-17 14:25:09 +0000 | [diff] [blame] | 175 | |
| 176 | return PCIBIOS_SUCCESSFUL; |
| 177 | } |