13ed7a2a7SSergey Ryazanov /* 23ed7a2a7SSergey Ryazanov * This program is free software; you can redistribute it and/or 33ed7a2a7SSergey Ryazanov * modify it under the terms of the GNU General Public License 43ed7a2a7SSergey Ryazanov * as published by the Free Software Foundation; either version 2 53ed7a2a7SSergey Ryazanov * of the License, or (at your option) any later version. 63ed7a2a7SSergey Ryazanov * 73ed7a2a7SSergey Ryazanov * This program is distributed in the hope that it will be useful, 83ed7a2a7SSergey Ryazanov * but WITHOUT ANY WARRANTY; without even the implied warranty of 93ed7a2a7SSergey Ryazanov * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 103ed7a2a7SSergey Ryazanov * GNU General Public License for more details. 113ed7a2a7SSergey Ryazanov * 123ed7a2a7SSergey Ryazanov * You should have received a copy of the GNU General Public License 133ed7a2a7SSergey Ryazanov * along with this program; if not, see <http://www.gnu.org/licenses/>. 143ed7a2a7SSergey Ryazanov */ 153ed7a2a7SSergey Ryazanov 163ed7a2a7SSergey Ryazanov /** 173ed7a2a7SSergey Ryazanov * Both AR2315 and AR2316 chips have PCI interface unit, which supports DMA 183ed7a2a7SSergey Ryazanov * and interrupt. PCI interface supports MMIO access method, but does not 193ed7a2a7SSergey Ryazanov * seem to support I/O ports. 203ed7a2a7SSergey Ryazanov * 213ed7a2a7SSergey Ryazanov * Read/write operation in the region 0x80000000-0xBFFFFFFF causes 223ed7a2a7SSergey Ryazanov * a memory read/write command on the PCI bus. 30 LSBs of address on 233ed7a2a7SSergey Ryazanov * the bus are taken from memory read/write request and 2 MSBs are 243ed7a2a7SSergey Ryazanov * determined by PCI unit configuration. 253ed7a2a7SSergey Ryazanov * 263ed7a2a7SSergey Ryazanov * To work with the configuration space instead of memory is necessary set 273ed7a2a7SSergey Ryazanov * the CFG_SEL bit in the PCI_MISC_CONFIG register. 283ed7a2a7SSergey Ryazanov * 293ed7a2a7SSergey Ryazanov * Devices on the bus can perform DMA requests via chip BAR1. PCI host 303ed7a2a7SSergey Ryazanov * controller BARs are programmend as if an external device is programmed. 313ed7a2a7SSergey Ryazanov * Which means that during configuration, IDSEL pin of the chip should be 323ed7a2a7SSergey Ryazanov * asserted. 333ed7a2a7SSergey Ryazanov * 343ed7a2a7SSergey Ryazanov * We know (and support) only one board that uses the PCI interface - 353ed7a2a7SSergey Ryazanov * Fonera 2.0g (FON2202). It has a USB EHCI controller connected to the 363ed7a2a7SSergey Ryazanov * AR2315 PCI bus. IDSEL pin of USB controller is connected to AD[13] line 373ed7a2a7SSergey Ryazanov * and IDSEL pin of AR2315 is connected to AD[16] line. 383ed7a2a7SSergey Ryazanov */ 393ed7a2a7SSergey Ryazanov 403ed7a2a7SSergey Ryazanov #include <linux/types.h> 413ed7a2a7SSergey Ryazanov #include <linux/pci.h> 423ed7a2a7SSergey Ryazanov #include <linux/platform_device.h> 433ed7a2a7SSergey Ryazanov #include <linux/kernel.h> 443ed7a2a7SSergey Ryazanov #include <linux/init.h> 453ed7a2a7SSergey Ryazanov #include <linux/mm.h> 463ed7a2a7SSergey Ryazanov #include <linux/delay.h> 473ed7a2a7SSergey Ryazanov #include <linux/bitops.h> 483ed7a2a7SSergey Ryazanov #include <linux/irq.h> 493ed7a2a7SSergey Ryazanov #include <linux/irqdomain.h> 503ed7a2a7SSergey Ryazanov #include <linux/io.h> 513ed7a2a7SSergey Ryazanov #include <asm/paccess.h> 523ed7a2a7SSergey Ryazanov 533ed7a2a7SSergey Ryazanov /* 543ed7a2a7SSergey Ryazanov * PCI Bus Interface Registers 553ed7a2a7SSergey Ryazanov */ 563ed7a2a7SSergey Ryazanov #define AR2315_PCI_1MS_REG 0x0008 573ed7a2a7SSergey Ryazanov 583ed7a2a7SSergey Ryazanov #define AR2315_PCI_1MS_MASK 0x3FFFF /* # of AHB clk cycles in 1ms */ 593ed7a2a7SSergey Ryazanov 603ed7a2a7SSergey Ryazanov #define AR2315_PCI_MISC_CONFIG 0x000c 613ed7a2a7SSergey Ryazanov 623ed7a2a7SSergey Ryazanov #define AR2315_PCIMISC_TXD_EN 0x00000001 /* Enable TXD for fragments */ 633ed7a2a7SSergey Ryazanov #define AR2315_PCIMISC_CFG_SEL 0x00000002 /* Mem or Config cycles */ 643ed7a2a7SSergey Ryazanov #define AR2315_PCIMISC_GIG_MASK 0x0000000C /* bits 31-30 for pci req */ 653ed7a2a7SSergey Ryazanov #define AR2315_PCIMISC_RST_MODE 0x00000030 663ed7a2a7SSergey Ryazanov #define AR2315_PCIRST_INPUT 0x00000000 /* 4:5=0 rst is input */ 673ed7a2a7SSergey Ryazanov #define AR2315_PCIRST_LOW 0x00000010 /* 4:5=1 rst to GND */ 683ed7a2a7SSergey Ryazanov #define AR2315_PCIRST_HIGH 0x00000020 /* 4:5=2 rst to VDD */ 693ed7a2a7SSergey Ryazanov #define AR2315_PCIGRANT_EN 0x00000000 /* 6:7=0 early grant en */ 703ed7a2a7SSergey Ryazanov #define AR2315_PCIGRANT_FRAME 0x00000040 /* 6:7=1 grant waits 4 frame */ 713ed7a2a7SSergey Ryazanov #define AR2315_PCIGRANT_IDLE 0x00000080 /* 6:7=2 grant waits 4 idle */ 723ed7a2a7SSergey Ryazanov #define AR2315_PCIGRANT_GAP 0x00000000 /* 6:7=2 grant waits 4 idle */ 733ed7a2a7SSergey Ryazanov #define AR2315_PCICACHE_DIS 0x00001000 /* PCI external access cache 743ed7a2a7SSergey Ryazanov * disable */ 753ed7a2a7SSergey Ryazanov 763ed7a2a7SSergey Ryazanov #define AR2315_PCI_OUT_TSTAMP 0x0010 773ed7a2a7SSergey Ryazanov 783ed7a2a7SSergey Ryazanov #define AR2315_PCI_UNCACHE_CFG 0x0014 793ed7a2a7SSergey Ryazanov 803ed7a2a7SSergey Ryazanov #define AR2315_PCI_IN_EN 0x0100 813ed7a2a7SSergey Ryazanov 823ed7a2a7SSergey Ryazanov #define AR2315_PCI_IN_EN0 0x01 /* Enable chain 0 */ 833ed7a2a7SSergey Ryazanov #define AR2315_PCI_IN_EN1 0x02 /* Enable chain 1 */ 843ed7a2a7SSergey Ryazanov #define AR2315_PCI_IN_EN2 0x04 /* Enable chain 2 */ 853ed7a2a7SSergey Ryazanov #define AR2315_PCI_IN_EN3 0x08 /* Enable chain 3 */ 863ed7a2a7SSergey Ryazanov 873ed7a2a7SSergey Ryazanov #define AR2315_PCI_IN_DIS 0x0104 883ed7a2a7SSergey Ryazanov 893ed7a2a7SSergey Ryazanov #define AR2315_PCI_IN_DIS0 0x01 /* Disable chain 0 */ 903ed7a2a7SSergey Ryazanov #define AR2315_PCI_IN_DIS1 0x02 /* Disable chain 1 */ 913ed7a2a7SSergey Ryazanov #define AR2315_PCI_IN_DIS2 0x04 /* Disable chain 2 */ 923ed7a2a7SSergey Ryazanov #define AR2315_PCI_IN_DIS3 0x08 /* Disable chain 3 */ 933ed7a2a7SSergey Ryazanov 943ed7a2a7SSergey Ryazanov #define AR2315_PCI_IN_PTR 0x0200 953ed7a2a7SSergey Ryazanov 963ed7a2a7SSergey Ryazanov #define AR2315_PCI_OUT_EN 0x0400 973ed7a2a7SSergey Ryazanov 983ed7a2a7SSergey Ryazanov #define AR2315_PCI_OUT_EN0 0x01 /* Enable chain 0 */ 993ed7a2a7SSergey Ryazanov 1003ed7a2a7SSergey Ryazanov #define AR2315_PCI_OUT_DIS 0x0404 1013ed7a2a7SSergey Ryazanov 1023ed7a2a7SSergey Ryazanov #define AR2315_PCI_OUT_DIS0 0x01 /* Disable chain 0 */ 1033ed7a2a7SSergey Ryazanov 1043ed7a2a7SSergey Ryazanov #define AR2315_PCI_OUT_PTR 0x0408 1053ed7a2a7SSergey Ryazanov 1063ed7a2a7SSergey Ryazanov /* PCI interrupt status (write one to clear) */ 1073ed7a2a7SSergey Ryazanov #define AR2315_PCI_ISR 0x0500 1083ed7a2a7SSergey Ryazanov 1093ed7a2a7SSergey Ryazanov #define AR2315_PCI_INT_TX 0x00000001 /* Desc In Completed */ 1103ed7a2a7SSergey Ryazanov #define AR2315_PCI_INT_TXOK 0x00000002 /* Desc In OK */ 1113ed7a2a7SSergey Ryazanov #define AR2315_PCI_INT_TXERR 0x00000004 /* Desc In ERR */ 1123ed7a2a7SSergey Ryazanov #define AR2315_PCI_INT_TXEOL 0x00000008 /* Desc In End-of-List */ 1133ed7a2a7SSergey Ryazanov #define AR2315_PCI_INT_RX 0x00000010 /* Desc Out Completed */ 1143ed7a2a7SSergey Ryazanov #define AR2315_PCI_INT_RXOK 0x00000020 /* Desc Out OK */ 1153ed7a2a7SSergey Ryazanov #define AR2315_PCI_INT_RXERR 0x00000040 /* Desc Out ERR */ 1163ed7a2a7SSergey Ryazanov #define AR2315_PCI_INT_RXEOL 0x00000080 /* Desc Out EOL */ 1173ed7a2a7SSergey Ryazanov #define AR2315_PCI_INT_TXOOD 0x00000200 /* Desc In Out-of-Desc */ 1183ed7a2a7SSergey Ryazanov #define AR2315_PCI_INT_DESCMASK 0x0000FFFF /* Desc Mask */ 1193ed7a2a7SSergey Ryazanov #define AR2315_PCI_INT_EXT 0x02000000 /* Extern PCI INTA */ 1203ed7a2a7SSergey Ryazanov #define AR2315_PCI_INT_ABORT 0x04000000 /* PCI bus abort event */ 1213ed7a2a7SSergey Ryazanov 1223ed7a2a7SSergey Ryazanov /* PCI interrupt mask */ 1233ed7a2a7SSergey Ryazanov #define AR2315_PCI_IMR 0x0504 1243ed7a2a7SSergey Ryazanov 1253ed7a2a7SSergey Ryazanov /* Global PCI interrupt enable */ 1263ed7a2a7SSergey Ryazanov #define AR2315_PCI_IER 0x0508 1273ed7a2a7SSergey Ryazanov 1283ed7a2a7SSergey Ryazanov #define AR2315_PCI_IER_DISABLE 0x00 /* disable pci interrupts */ 1293ed7a2a7SSergey Ryazanov #define AR2315_PCI_IER_ENABLE 0x01 /* enable pci interrupts */ 1303ed7a2a7SSergey Ryazanov 1313ed7a2a7SSergey Ryazanov #define AR2315_PCI_HOST_IN_EN 0x0800 1323ed7a2a7SSergey Ryazanov #define AR2315_PCI_HOST_IN_DIS 0x0804 1333ed7a2a7SSergey Ryazanov #define AR2315_PCI_HOST_IN_PTR 0x0810 1343ed7a2a7SSergey Ryazanov #define AR2315_PCI_HOST_OUT_EN 0x0900 1353ed7a2a7SSergey Ryazanov #define AR2315_PCI_HOST_OUT_DIS 0x0904 1363ed7a2a7SSergey Ryazanov #define AR2315_PCI_HOST_OUT_PTR 0x0908 1373ed7a2a7SSergey Ryazanov 1383ed7a2a7SSergey Ryazanov /* 1393ed7a2a7SSergey Ryazanov * PCI interrupts, which share IP5 1403ed7a2a7SSergey Ryazanov * Keep ordered according to AR2315_PCI_INT_XXX bits 1413ed7a2a7SSergey Ryazanov */ 1423ed7a2a7SSergey Ryazanov #define AR2315_PCI_IRQ_EXT 25 1433ed7a2a7SSergey Ryazanov #define AR2315_PCI_IRQ_ABORT 26 1443ed7a2a7SSergey Ryazanov #define AR2315_PCI_IRQ_COUNT 27 1453ed7a2a7SSergey Ryazanov 1463ed7a2a7SSergey Ryazanov /* Arbitrary size of memory region to access the configuration space */ 1473ed7a2a7SSergey Ryazanov #define AR2315_PCI_CFG_SIZE 0x00100000 1483ed7a2a7SSergey Ryazanov 1493ed7a2a7SSergey Ryazanov #define AR2315_PCI_HOST_SLOT 3 1503ed7a2a7SSergey Ryazanov #define AR2315_PCI_HOST_DEVID ((0xff18 << 16) | PCI_VENDOR_ID_ATHEROS) 1513ed7a2a7SSergey Ryazanov 152d1f2564aSChristoph Hellwig /* 153d1f2564aSChristoph Hellwig * We need some arbitrary non-zero value to be programmed to the BAR1 register 154d1f2564aSChristoph Hellwig * of PCI host controller to enable DMA. The same value should be used as the 155d1f2564aSChristoph Hellwig * offset to calculate the physical address of DMA buffer for PCI devices. 156d1f2564aSChristoph Hellwig */ 157d1f2564aSChristoph Hellwig #define AR2315_PCI_HOST_SDRAM_BASEADDR 0x20000000 158d1f2564aSChristoph Hellwig 1593ed7a2a7SSergey Ryazanov /* ??? access BAR */ 1603ed7a2a7SSergey Ryazanov #define AR2315_PCI_HOST_MBAR0 0x10000000 1613ed7a2a7SSergey Ryazanov /* RAM access BAR */ 1623ed7a2a7SSergey Ryazanov #define AR2315_PCI_HOST_MBAR1 AR2315_PCI_HOST_SDRAM_BASEADDR 1633ed7a2a7SSergey Ryazanov /* ??? access BAR */ 1643ed7a2a7SSergey Ryazanov #define AR2315_PCI_HOST_MBAR2 0x30000000 1653ed7a2a7SSergey Ryazanov 1663ed7a2a7SSergey Ryazanov struct ar2315_pci_ctrl { 1673ed7a2a7SSergey Ryazanov void __iomem *cfg_mem; 1683ed7a2a7SSergey Ryazanov void __iomem *mmr_mem; 1693ed7a2a7SSergey Ryazanov unsigned irq; 1703ed7a2a7SSergey Ryazanov unsigned irq_ext; 1713ed7a2a7SSergey Ryazanov struct irq_domain *domain; 1723ed7a2a7SSergey Ryazanov struct pci_controller pci_ctrl; 1733ed7a2a7SSergey Ryazanov struct resource mem_res; 1743ed7a2a7SSergey Ryazanov struct resource io_res; 1753ed7a2a7SSergey Ryazanov }; 1763ed7a2a7SSergey Ryazanov 177d1f2564aSChristoph Hellwig static inline dma_addr_t ar2315_dev_offset(struct device *dev) 178d1f2564aSChristoph Hellwig { 179d1f2564aSChristoph Hellwig if (dev && dev_is_pci(dev)) 180d1f2564aSChristoph Hellwig return AR2315_PCI_HOST_SDRAM_BASEADDR; 181d1f2564aSChristoph Hellwig return 0; 182d1f2564aSChristoph Hellwig } 183d1f2564aSChristoph Hellwig 184d1f2564aSChristoph Hellwig dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t paddr) 185d1f2564aSChristoph Hellwig { 186d1f2564aSChristoph Hellwig return paddr + ar2315_dev_offset(dev); 187d1f2564aSChristoph Hellwig } 188d1f2564aSChristoph Hellwig 189d1f2564aSChristoph Hellwig phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t dma_addr) 190d1f2564aSChristoph Hellwig { 191d1f2564aSChristoph Hellwig return dma_addr - ar2315_dev_offset(dev); 192d1f2564aSChristoph Hellwig } 193d1f2564aSChristoph Hellwig 1943ed7a2a7SSergey Ryazanov static inline struct ar2315_pci_ctrl *ar2315_pci_bus_to_apc(struct pci_bus *bus) 1953ed7a2a7SSergey Ryazanov { 1963ed7a2a7SSergey Ryazanov struct pci_controller *hose = bus->sysdata; 1973ed7a2a7SSergey Ryazanov 1983ed7a2a7SSergey Ryazanov return container_of(hose, struct ar2315_pci_ctrl, pci_ctrl); 1993ed7a2a7SSergey Ryazanov } 2003ed7a2a7SSergey Ryazanov 2013ed7a2a7SSergey Ryazanov static inline u32 ar2315_pci_reg_read(struct ar2315_pci_ctrl *apc, u32 reg) 2023ed7a2a7SSergey Ryazanov { 2033ed7a2a7SSergey Ryazanov return __raw_readl(apc->mmr_mem + reg); 2043ed7a2a7SSergey Ryazanov } 2053ed7a2a7SSergey Ryazanov 2063ed7a2a7SSergey Ryazanov static inline void ar2315_pci_reg_write(struct ar2315_pci_ctrl *apc, u32 reg, 2073ed7a2a7SSergey Ryazanov u32 val) 2083ed7a2a7SSergey Ryazanov { 2093ed7a2a7SSergey Ryazanov __raw_writel(val, apc->mmr_mem + reg); 2103ed7a2a7SSergey Ryazanov } 2113ed7a2a7SSergey Ryazanov 2123ed7a2a7SSergey Ryazanov static inline void ar2315_pci_reg_mask(struct ar2315_pci_ctrl *apc, u32 reg, 2133ed7a2a7SSergey Ryazanov u32 mask, u32 val) 2143ed7a2a7SSergey Ryazanov { 2153ed7a2a7SSergey Ryazanov u32 ret = ar2315_pci_reg_read(apc, reg); 2163ed7a2a7SSergey Ryazanov 2173ed7a2a7SSergey Ryazanov ret &= ~mask; 2183ed7a2a7SSergey Ryazanov ret |= val; 2193ed7a2a7SSergey Ryazanov ar2315_pci_reg_write(apc, reg, ret); 2203ed7a2a7SSergey Ryazanov } 2213ed7a2a7SSergey Ryazanov 2223ed7a2a7SSergey Ryazanov static int ar2315_pci_cfg_access(struct ar2315_pci_ctrl *apc, unsigned devfn, 2233ed7a2a7SSergey Ryazanov int where, int size, u32 *ptr, bool write) 2243ed7a2a7SSergey Ryazanov { 2253ed7a2a7SSergey Ryazanov int func = PCI_FUNC(devfn); 2263ed7a2a7SSergey Ryazanov int dev = PCI_SLOT(devfn); 2273ed7a2a7SSergey Ryazanov u32 addr = (1 << (13 + dev)) | (func << 8) | (where & ~3); 2283ed7a2a7SSergey Ryazanov u32 mask = 0xffffffff >> 8 * (4 - size); 2293ed7a2a7SSergey Ryazanov u32 sh = (where & 3) * 8; 2303ed7a2a7SSergey Ryazanov u32 value, isr; 2313ed7a2a7SSergey Ryazanov 2323ed7a2a7SSergey Ryazanov /* Prevent access past the remapped area */ 2333ed7a2a7SSergey Ryazanov if (addr >= AR2315_PCI_CFG_SIZE || dev > 18) 2343ed7a2a7SSergey Ryazanov return PCIBIOS_DEVICE_NOT_FOUND; 2353ed7a2a7SSergey Ryazanov 2363ed7a2a7SSergey Ryazanov /* Clear pending errors */ 2373ed7a2a7SSergey Ryazanov ar2315_pci_reg_write(apc, AR2315_PCI_ISR, AR2315_PCI_INT_ABORT); 2383ed7a2a7SSergey Ryazanov /* Select Configuration access */ 2393ed7a2a7SSergey Ryazanov ar2315_pci_reg_mask(apc, AR2315_PCI_MISC_CONFIG, 0, 2403ed7a2a7SSergey Ryazanov AR2315_PCIMISC_CFG_SEL); 2413ed7a2a7SSergey Ryazanov 2423ed7a2a7SSergey Ryazanov mb(); /* PCI must see space change before we begin */ 2433ed7a2a7SSergey Ryazanov 2443ed7a2a7SSergey Ryazanov value = __raw_readl(apc->cfg_mem + addr); 2453ed7a2a7SSergey Ryazanov 2463ed7a2a7SSergey Ryazanov isr = ar2315_pci_reg_read(apc, AR2315_PCI_ISR); 2473ed7a2a7SSergey Ryazanov 2483ed7a2a7SSergey Ryazanov if (isr & AR2315_PCI_INT_ABORT) 2493ed7a2a7SSergey Ryazanov goto exit_err; 2503ed7a2a7SSergey Ryazanov 2513ed7a2a7SSergey Ryazanov if (write) { 2523ed7a2a7SSergey Ryazanov value = (value & ~(mask << sh)) | *ptr << sh; 2533ed7a2a7SSergey Ryazanov __raw_writel(value, apc->cfg_mem + addr); 2543ed7a2a7SSergey Ryazanov isr = ar2315_pci_reg_read(apc, AR2315_PCI_ISR); 2553ed7a2a7SSergey Ryazanov if (isr & AR2315_PCI_INT_ABORT) 2563ed7a2a7SSergey Ryazanov goto exit_err; 2573ed7a2a7SSergey Ryazanov } else { 2583ed7a2a7SSergey Ryazanov *ptr = (value >> sh) & mask; 2593ed7a2a7SSergey Ryazanov } 2603ed7a2a7SSergey Ryazanov 2613ed7a2a7SSergey Ryazanov goto exit; 2623ed7a2a7SSergey Ryazanov 2633ed7a2a7SSergey Ryazanov exit_err: 2643ed7a2a7SSergey Ryazanov ar2315_pci_reg_write(apc, AR2315_PCI_ISR, AR2315_PCI_INT_ABORT); 2653ed7a2a7SSergey Ryazanov if (!write) 2663ed7a2a7SSergey Ryazanov *ptr = 0xffffffff; 2673ed7a2a7SSergey Ryazanov 2683ed7a2a7SSergey Ryazanov exit: 2693ed7a2a7SSergey Ryazanov /* Select Memory access */ 2703ed7a2a7SSergey Ryazanov ar2315_pci_reg_mask(apc, AR2315_PCI_MISC_CONFIG, AR2315_PCIMISC_CFG_SEL, 2713ed7a2a7SSergey Ryazanov 0); 2723ed7a2a7SSergey Ryazanov 2733ed7a2a7SSergey Ryazanov return isr & AR2315_PCI_INT_ABORT ? PCIBIOS_DEVICE_NOT_FOUND : 2743ed7a2a7SSergey Ryazanov PCIBIOS_SUCCESSFUL; 2753ed7a2a7SSergey Ryazanov } 2763ed7a2a7SSergey Ryazanov 2773ed7a2a7SSergey Ryazanov static inline int ar2315_pci_local_cfg_rd(struct ar2315_pci_ctrl *apc, 2783ed7a2a7SSergey Ryazanov unsigned devfn, int where, u32 *val) 2793ed7a2a7SSergey Ryazanov { 2803ed7a2a7SSergey Ryazanov return ar2315_pci_cfg_access(apc, devfn, where, sizeof(u32), val, 2813ed7a2a7SSergey Ryazanov false); 2823ed7a2a7SSergey Ryazanov } 2833ed7a2a7SSergey Ryazanov 2843ed7a2a7SSergey Ryazanov static inline int ar2315_pci_local_cfg_wr(struct ar2315_pci_ctrl *apc, 2853ed7a2a7SSergey Ryazanov unsigned devfn, int where, u32 val) 2863ed7a2a7SSergey Ryazanov { 2873ed7a2a7SSergey Ryazanov return ar2315_pci_cfg_access(apc, devfn, where, sizeof(u32), &val, 2883ed7a2a7SSergey Ryazanov true); 2893ed7a2a7SSergey Ryazanov } 2903ed7a2a7SSergey Ryazanov 2913ed7a2a7SSergey Ryazanov static int ar2315_pci_cfg_read(struct pci_bus *bus, unsigned devfn, int where, 2923ed7a2a7SSergey Ryazanov int size, u32 *value) 2933ed7a2a7SSergey Ryazanov { 2943ed7a2a7SSergey Ryazanov struct ar2315_pci_ctrl *apc = ar2315_pci_bus_to_apc(bus); 2953ed7a2a7SSergey Ryazanov 2963ed7a2a7SSergey Ryazanov if (PCI_SLOT(devfn) == AR2315_PCI_HOST_SLOT) 2973ed7a2a7SSergey Ryazanov return PCIBIOS_DEVICE_NOT_FOUND; 2983ed7a2a7SSergey Ryazanov 2993ed7a2a7SSergey Ryazanov return ar2315_pci_cfg_access(apc, devfn, where, size, value, false); 3003ed7a2a7SSergey Ryazanov } 3013ed7a2a7SSergey Ryazanov 3023ed7a2a7SSergey Ryazanov static int ar2315_pci_cfg_write(struct pci_bus *bus, unsigned devfn, int where, 3033ed7a2a7SSergey Ryazanov int size, u32 value) 3043ed7a2a7SSergey Ryazanov { 3053ed7a2a7SSergey Ryazanov struct ar2315_pci_ctrl *apc = ar2315_pci_bus_to_apc(bus); 3063ed7a2a7SSergey Ryazanov 3073ed7a2a7SSergey Ryazanov if (PCI_SLOT(devfn) == AR2315_PCI_HOST_SLOT) 3083ed7a2a7SSergey Ryazanov return PCIBIOS_DEVICE_NOT_FOUND; 3093ed7a2a7SSergey Ryazanov 3103ed7a2a7SSergey Ryazanov return ar2315_pci_cfg_access(apc, devfn, where, size, &value, true); 3113ed7a2a7SSergey Ryazanov } 3123ed7a2a7SSergey Ryazanov 3133ed7a2a7SSergey Ryazanov static struct pci_ops ar2315_pci_ops = { 3143ed7a2a7SSergey Ryazanov .read = ar2315_pci_cfg_read, 3153ed7a2a7SSergey Ryazanov .write = ar2315_pci_cfg_write, 3163ed7a2a7SSergey Ryazanov }; 3173ed7a2a7SSergey Ryazanov 3183ed7a2a7SSergey Ryazanov static int ar2315_pci_host_setup(struct ar2315_pci_ctrl *apc) 3193ed7a2a7SSergey Ryazanov { 3203ed7a2a7SSergey Ryazanov unsigned devfn = PCI_DEVFN(AR2315_PCI_HOST_SLOT, 0); 3213ed7a2a7SSergey Ryazanov int res; 3223ed7a2a7SSergey Ryazanov u32 id; 3233ed7a2a7SSergey Ryazanov 3243ed7a2a7SSergey Ryazanov res = ar2315_pci_local_cfg_rd(apc, devfn, PCI_VENDOR_ID, &id); 3253ed7a2a7SSergey Ryazanov if (res != PCIBIOS_SUCCESSFUL || id != AR2315_PCI_HOST_DEVID) 3263ed7a2a7SSergey Ryazanov return -ENODEV; 3273ed7a2a7SSergey Ryazanov 3283ed7a2a7SSergey Ryazanov /* Program MBARs */ 3293ed7a2a7SSergey Ryazanov ar2315_pci_local_cfg_wr(apc, devfn, PCI_BASE_ADDRESS_0, 3303ed7a2a7SSergey Ryazanov AR2315_PCI_HOST_MBAR0); 3313ed7a2a7SSergey Ryazanov ar2315_pci_local_cfg_wr(apc, devfn, PCI_BASE_ADDRESS_1, 3323ed7a2a7SSergey Ryazanov AR2315_PCI_HOST_MBAR1); 3333ed7a2a7SSergey Ryazanov ar2315_pci_local_cfg_wr(apc, devfn, PCI_BASE_ADDRESS_2, 3343ed7a2a7SSergey Ryazanov AR2315_PCI_HOST_MBAR2); 3353ed7a2a7SSergey Ryazanov 3363ed7a2a7SSergey Ryazanov /* Run */ 3373ed7a2a7SSergey Ryazanov ar2315_pci_local_cfg_wr(apc, devfn, PCI_COMMAND, PCI_COMMAND_MEMORY | 3383ed7a2a7SSergey Ryazanov PCI_COMMAND_MASTER | PCI_COMMAND_SPECIAL | 3393ed7a2a7SSergey Ryazanov PCI_COMMAND_INVALIDATE | PCI_COMMAND_PARITY | 3403ed7a2a7SSergey Ryazanov PCI_COMMAND_SERR | PCI_COMMAND_FAST_BACK); 3413ed7a2a7SSergey Ryazanov 3423ed7a2a7SSergey Ryazanov return 0; 3433ed7a2a7SSergey Ryazanov } 3443ed7a2a7SSergey Ryazanov 345bd0b9ac4SThomas Gleixner static void ar2315_pci_irq_handler(struct irq_desc *desc) 3463ed7a2a7SSergey Ryazanov { 34725aae561SJiang Liu struct ar2315_pci_ctrl *apc = irq_desc_get_handler_data(desc); 3483ed7a2a7SSergey Ryazanov u32 pending = ar2315_pci_reg_read(apc, AR2315_PCI_ISR) & 3493ed7a2a7SSergey Ryazanov ar2315_pci_reg_read(apc, AR2315_PCI_IMR); 3503ed7a2a7SSergey Ryazanov unsigned pci_irq = 0; 3513ed7a2a7SSergey Ryazanov 3523ed7a2a7SSergey Ryazanov if (pending) 3533ed7a2a7SSergey Ryazanov pci_irq = irq_find_mapping(apc->domain, __ffs(pending)); 3543ed7a2a7SSergey Ryazanov 3553ed7a2a7SSergey Ryazanov if (pci_irq) 3563ed7a2a7SSergey Ryazanov generic_handle_irq(pci_irq); 3573ed7a2a7SSergey Ryazanov else 3583ed7a2a7SSergey Ryazanov spurious_interrupt(); 3593ed7a2a7SSergey Ryazanov } 3603ed7a2a7SSergey Ryazanov 3613ed7a2a7SSergey Ryazanov static void ar2315_pci_irq_mask(struct irq_data *d) 3623ed7a2a7SSergey Ryazanov { 3633ed7a2a7SSergey Ryazanov struct ar2315_pci_ctrl *apc = irq_data_get_irq_chip_data(d); 3643ed7a2a7SSergey Ryazanov 3653ed7a2a7SSergey Ryazanov ar2315_pci_reg_mask(apc, AR2315_PCI_IMR, BIT(d->hwirq), 0); 3663ed7a2a7SSergey Ryazanov } 3673ed7a2a7SSergey Ryazanov 3683ed7a2a7SSergey Ryazanov static void ar2315_pci_irq_mask_ack(struct irq_data *d) 3693ed7a2a7SSergey Ryazanov { 3703ed7a2a7SSergey Ryazanov struct ar2315_pci_ctrl *apc = irq_data_get_irq_chip_data(d); 3713ed7a2a7SSergey Ryazanov u32 m = BIT(d->hwirq); 3723ed7a2a7SSergey Ryazanov 3733ed7a2a7SSergey Ryazanov ar2315_pci_reg_mask(apc, AR2315_PCI_IMR, m, 0); 3743ed7a2a7SSergey Ryazanov ar2315_pci_reg_write(apc, AR2315_PCI_ISR, m); 3753ed7a2a7SSergey Ryazanov } 3763ed7a2a7SSergey Ryazanov 3773ed7a2a7SSergey Ryazanov static void ar2315_pci_irq_unmask(struct irq_data *d) 3783ed7a2a7SSergey Ryazanov { 3793ed7a2a7SSergey Ryazanov struct ar2315_pci_ctrl *apc = irq_data_get_irq_chip_data(d); 3803ed7a2a7SSergey Ryazanov 3813ed7a2a7SSergey Ryazanov ar2315_pci_reg_mask(apc, AR2315_PCI_IMR, 0, BIT(d->hwirq)); 3823ed7a2a7SSergey Ryazanov } 3833ed7a2a7SSergey Ryazanov 3843ed7a2a7SSergey Ryazanov static struct irq_chip ar2315_pci_irq_chip = { 3853ed7a2a7SSergey Ryazanov .name = "AR2315-PCI", 3863ed7a2a7SSergey Ryazanov .irq_mask = ar2315_pci_irq_mask, 3873ed7a2a7SSergey Ryazanov .irq_mask_ack = ar2315_pci_irq_mask_ack, 3883ed7a2a7SSergey Ryazanov .irq_unmask = ar2315_pci_irq_unmask, 3893ed7a2a7SSergey Ryazanov }; 3903ed7a2a7SSergey Ryazanov 3913ed7a2a7SSergey Ryazanov static int ar2315_pci_irq_map(struct irq_domain *d, unsigned irq, 3923ed7a2a7SSergey Ryazanov irq_hw_number_t hw) 3933ed7a2a7SSergey Ryazanov { 3943ed7a2a7SSergey Ryazanov irq_set_chip_and_handler(irq, &ar2315_pci_irq_chip, handle_level_irq); 3953ed7a2a7SSergey Ryazanov irq_set_chip_data(irq, d->host_data); 3963ed7a2a7SSergey Ryazanov return 0; 3973ed7a2a7SSergey Ryazanov } 3983ed7a2a7SSergey Ryazanov 3993ed7a2a7SSergey Ryazanov static struct irq_domain_ops ar2315_pci_irq_domain_ops = { 4003ed7a2a7SSergey Ryazanov .map = ar2315_pci_irq_map, 4013ed7a2a7SSergey Ryazanov }; 4023ed7a2a7SSergey Ryazanov 4033ed7a2a7SSergey Ryazanov static void ar2315_pci_irq_init(struct ar2315_pci_ctrl *apc) 4043ed7a2a7SSergey Ryazanov { 4053ed7a2a7SSergey Ryazanov ar2315_pci_reg_mask(apc, AR2315_PCI_IER, AR2315_PCI_IER_ENABLE, 0); 4063ed7a2a7SSergey Ryazanov ar2315_pci_reg_mask(apc, AR2315_PCI_IMR, (AR2315_PCI_INT_ABORT | 4073ed7a2a7SSergey Ryazanov AR2315_PCI_INT_EXT), 0); 4083ed7a2a7SSergey Ryazanov 4093ed7a2a7SSergey Ryazanov apc->irq_ext = irq_create_mapping(apc->domain, AR2315_PCI_IRQ_EXT); 4103ed7a2a7SSergey Ryazanov 411746ad9a7SThomas Gleixner irq_set_chained_handler_and_data(apc->irq, ar2315_pci_irq_handler, 412746ad9a7SThomas Gleixner apc); 4133ed7a2a7SSergey Ryazanov 4143ed7a2a7SSergey Ryazanov /* Clear any pending Abort or external Interrupts 4153ed7a2a7SSergey Ryazanov * and enable interrupt processing */ 4163ed7a2a7SSergey Ryazanov ar2315_pci_reg_write(apc, AR2315_PCI_ISR, AR2315_PCI_INT_ABORT | 4173ed7a2a7SSergey Ryazanov AR2315_PCI_INT_EXT); 4183ed7a2a7SSergey Ryazanov ar2315_pci_reg_mask(apc, AR2315_PCI_IER, 0, AR2315_PCI_IER_ENABLE); 4193ed7a2a7SSergey Ryazanov } 4203ed7a2a7SSergey Ryazanov 4213ed7a2a7SSergey Ryazanov static int ar2315_pci_probe(struct platform_device *pdev) 4223ed7a2a7SSergey Ryazanov { 4233ed7a2a7SSergey Ryazanov struct ar2315_pci_ctrl *apc; 4243ed7a2a7SSergey Ryazanov struct device *dev = &pdev->dev; 4253ed7a2a7SSergey Ryazanov struct resource *res; 4263ed7a2a7SSergey Ryazanov int irq, err; 4273ed7a2a7SSergey Ryazanov 4283ed7a2a7SSergey Ryazanov apc = devm_kzalloc(dev, sizeof(*apc), GFP_KERNEL); 4293ed7a2a7SSergey Ryazanov if (!apc) 4303ed7a2a7SSergey Ryazanov return -ENOMEM; 4313ed7a2a7SSergey Ryazanov 4323ed7a2a7SSergey Ryazanov irq = platform_get_irq(pdev, 0); 4333ed7a2a7SSergey Ryazanov if (irq < 0) 4343ed7a2a7SSergey Ryazanov return -EINVAL; 4353ed7a2a7SSergey Ryazanov apc->irq = irq; 4363ed7a2a7SSergey Ryazanov 4373ed7a2a7SSergey Ryazanov res = platform_get_resource_byname(pdev, IORESOURCE_MEM, 4383ed7a2a7SSergey Ryazanov "ar2315-pci-ctrl"); 4393ed7a2a7SSergey Ryazanov apc->mmr_mem = devm_ioremap_resource(dev, res); 4403ed7a2a7SSergey Ryazanov if (IS_ERR(apc->mmr_mem)) 4413ed7a2a7SSergey Ryazanov return PTR_ERR(apc->mmr_mem); 4423ed7a2a7SSergey Ryazanov 4433ed7a2a7SSergey Ryazanov res = platform_get_resource_byname(pdev, IORESOURCE_MEM, 4443ed7a2a7SSergey Ryazanov "ar2315-pci-ext"); 4453ed7a2a7SSergey Ryazanov if (!res) 4463ed7a2a7SSergey Ryazanov return -EINVAL; 4473ed7a2a7SSergey Ryazanov 4483ed7a2a7SSergey Ryazanov apc->mem_res.name = "AR2315 PCI mem space"; 4493ed7a2a7SSergey Ryazanov apc->mem_res.parent = res; 4503ed7a2a7SSergey Ryazanov apc->mem_res.start = res->start; 4513ed7a2a7SSergey Ryazanov apc->mem_res.end = res->end; 4523ed7a2a7SSergey Ryazanov apc->mem_res.flags = IORESOURCE_MEM; 4533ed7a2a7SSergey Ryazanov 4543ed7a2a7SSergey Ryazanov /* Remap PCI config space */ 4553ed7a2a7SSergey Ryazanov apc->cfg_mem = devm_ioremap_nocache(dev, res->start, 4563ed7a2a7SSergey Ryazanov AR2315_PCI_CFG_SIZE); 4573ed7a2a7SSergey Ryazanov if (!apc->cfg_mem) { 4583ed7a2a7SSergey Ryazanov dev_err(dev, "failed to remap PCI config space\n"); 4593ed7a2a7SSergey Ryazanov return -ENOMEM; 4603ed7a2a7SSergey Ryazanov } 4613ed7a2a7SSergey Ryazanov 4623ed7a2a7SSergey Ryazanov /* Reset the PCI bus by setting bits 5-4 in PCI_MCFG */ 4633ed7a2a7SSergey Ryazanov ar2315_pci_reg_mask(apc, AR2315_PCI_MISC_CONFIG, 4643ed7a2a7SSergey Ryazanov AR2315_PCIMISC_RST_MODE, 4653ed7a2a7SSergey Ryazanov AR2315_PCIRST_LOW); 4663ed7a2a7SSergey Ryazanov msleep(100); 4673ed7a2a7SSergey Ryazanov 4683ed7a2a7SSergey Ryazanov /* Bring the PCI out of reset */ 4693ed7a2a7SSergey Ryazanov ar2315_pci_reg_mask(apc, AR2315_PCI_MISC_CONFIG, 4703ed7a2a7SSergey Ryazanov AR2315_PCIMISC_RST_MODE, 4713ed7a2a7SSergey Ryazanov AR2315_PCIRST_HIGH | AR2315_PCICACHE_DIS | 0x8); 4723ed7a2a7SSergey Ryazanov 4733ed7a2a7SSergey Ryazanov ar2315_pci_reg_write(apc, AR2315_PCI_UNCACHE_CFG, 4743ed7a2a7SSergey Ryazanov 0x1E | /* 1GB uncached */ 4753ed7a2a7SSergey Ryazanov (1 << 5) | /* Enable uncached */ 4763ed7a2a7SSergey Ryazanov (0x2 << 30) /* Base: 0x80000000 */); 4773ed7a2a7SSergey Ryazanov ar2315_pci_reg_read(apc, AR2315_PCI_UNCACHE_CFG); 4783ed7a2a7SSergey Ryazanov 4793ed7a2a7SSergey Ryazanov msleep(500); 4803ed7a2a7SSergey Ryazanov 4813ed7a2a7SSergey Ryazanov err = ar2315_pci_host_setup(apc); 4823ed7a2a7SSergey Ryazanov if (err) 4833ed7a2a7SSergey Ryazanov return err; 4843ed7a2a7SSergey Ryazanov 4853ed7a2a7SSergey Ryazanov apc->domain = irq_domain_add_linear(NULL, AR2315_PCI_IRQ_COUNT, 4863ed7a2a7SSergey Ryazanov &ar2315_pci_irq_domain_ops, apc); 4873ed7a2a7SSergey Ryazanov if (!apc->domain) { 4883ed7a2a7SSergey Ryazanov dev_err(dev, "failed to add IRQ domain\n"); 4893ed7a2a7SSergey Ryazanov return -ENOMEM; 4903ed7a2a7SSergey Ryazanov } 4913ed7a2a7SSergey Ryazanov 4923ed7a2a7SSergey Ryazanov ar2315_pci_irq_init(apc); 4933ed7a2a7SSergey Ryazanov 4943ed7a2a7SSergey Ryazanov /* PCI controller does not support I/O ports */ 4953ed7a2a7SSergey Ryazanov apc->io_res.name = "AR2315 IO space"; 4963ed7a2a7SSergey Ryazanov apc->io_res.start = 0; 4973ed7a2a7SSergey Ryazanov apc->io_res.end = 0; 4983ed7a2a7SSergey Ryazanov apc->io_res.flags = IORESOURCE_IO, 4993ed7a2a7SSergey Ryazanov 5003ed7a2a7SSergey Ryazanov apc->pci_ctrl.pci_ops = &ar2315_pci_ops; 5013ed7a2a7SSergey Ryazanov apc->pci_ctrl.mem_resource = &apc->mem_res, 5023ed7a2a7SSergey Ryazanov apc->pci_ctrl.io_resource = &apc->io_res, 5033ed7a2a7SSergey Ryazanov 5043ed7a2a7SSergey Ryazanov register_pci_controller(&apc->pci_ctrl); 5053ed7a2a7SSergey Ryazanov 5063ed7a2a7SSergey Ryazanov dev_info(dev, "register PCI controller\n"); 5073ed7a2a7SSergey Ryazanov 5083ed7a2a7SSergey Ryazanov return 0; 5093ed7a2a7SSergey Ryazanov } 5103ed7a2a7SSergey Ryazanov 5113ed7a2a7SSergey Ryazanov static struct platform_driver ar2315_pci_driver = { 5123ed7a2a7SSergey Ryazanov .probe = ar2315_pci_probe, 5133ed7a2a7SSergey Ryazanov .driver = { 5143ed7a2a7SSergey Ryazanov .name = "ar2315-pci", 5153ed7a2a7SSergey Ryazanov }, 5163ed7a2a7SSergey Ryazanov }; 5173ed7a2a7SSergey Ryazanov 5183ed7a2a7SSergey Ryazanov static int __init ar2315_pci_init(void) 5193ed7a2a7SSergey Ryazanov { 5203ed7a2a7SSergey Ryazanov return platform_driver_register(&ar2315_pci_driver); 5213ed7a2a7SSergey Ryazanov } 5223ed7a2a7SSergey Ryazanov arch_initcall(ar2315_pci_init); 5233ed7a2a7SSergey Ryazanov 5243ed7a2a7SSergey Ryazanov int pcibios_map_irq(const struct pci_dev *dev, u8 slot, u8 pin) 5253ed7a2a7SSergey Ryazanov { 5263ed7a2a7SSergey Ryazanov struct ar2315_pci_ctrl *apc = ar2315_pci_bus_to_apc(dev->bus); 5273ed7a2a7SSergey Ryazanov 5283ed7a2a7SSergey Ryazanov return slot ? 0 : apc->irq_ext; 5293ed7a2a7SSergey Ryazanov } 5303ed7a2a7SSergey Ryazanov 5313ed7a2a7SSergey Ryazanov int pcibios_plat_dev_init(struct pci_dev *dev) 5323ed7a2a7SSergey Ryazanov { 5333ed7a2a7SSergey Ryazanov return 0; 5343ed7a2a7SSergey Ryazanov } 535