15ad36c5fSErik Gilling /* 2938fa349SColin Cross * Copyright (C) 2011 Google, Inc. 35ad36c5fSErik Gilling * 45ad36c5fSErik Gilling * Author: 5938fa349SColin Cross * Colin Cross <ccross@android.com> 65ad36c5fSErik Gilling * 7*e307cc89SJoseph Lo * Copyright (C) 2010,2013, NVIDIA Corporation 8460907bcSGary King * 95ad36c5fSErik Gilling * This software is licensed under the terms of the GNU General Public 105ad36c5fSErik Gilling * License version 2, as published by the Free Software Foundation, and 115ad36c5fSErik Gilling * may be copied, distributed, and modified under those terms. 125ad36c5fSErik Gilling * 135ad36c5fSErik Gilling * This program is distributed in the hope that it will be useful, 145ad36c5fSErik Gilling * but WITHOUT ANY WARRANTY; without even the implied warranty of 155ad36c5fSErik Gilling * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 165ad36c5fSErik Gilling * GNU General Public License for more details. 175ad36c5fSErik Gilling * 185ad36c5fSErik Gilling */ 195ad36c5fSErik Gilling 205ad36c5fSErik Gilling #include <linux/kernel.h> 215ad36c5fSErik Gilling #include <linux/interrupt.h> 225ad36c5fSErik Gilling #include <linux/irq.h> 235ad36c5fSErik Gilling #include <linux/io.h> 240d4f7479Spdeschrijver@nvidia.com #include <linux/of.h> 25520f7bd7SRob Herring #include <linux/irqchip/arm-gic.h> 26*e307cc89SJoseph Lo #include <linux/syscore_ops.h> 275ad36c5fSErik Gilling 285ad36c5fSErik Gilling #include "board.h" 292be39c07SStephen Warren #include "iomap.h" 305ad36c5fSErik Gilling 31d1d8c666SColin Cross #define ICTLR_CPU_IEP_VFIQ 0x08 32d1d8c666SColin Cross #define ICTLR_CPU_IEP_FIR 0x14 33d1d8c666SColin Cross #define ICTLR_CPU_IEP_FIR_SET 0x18 34d1d8c666SColin Cross #define ICTLR_CPU_IEP_FIR_CLR 0x1c 35d1d8c666SColin Cross 36d1d8c666SColin Cross #define ICTLR_CPU_IER 0x20 37d1d8c666SColin Cross #define ICTLR_CPU_IER_SET 0x24 38d1d8c666SColin Cross #define ICTLR_CPU_IER_CLR 0x28 39d1d8c666SColin Cross #define ICTLR_CPU_IEP_CLASS 0x2C 40d1d8c666SColin Cross 41d1d8c666SColin Cross #define ICTLR_COP_IER 0x30 42d1d8c666SColin Cross #define ICTLR_COP_IER_SET 0x34 43d1d8c666SColin Cross #define ICTLR_COP_IER_CLR 0x38 44d1d8c666SColin Cross #define ICTLR_COP_IEP_CLASS 0x3c 45d1d8c666SColin Cross 46d1d8c666SColin Cross #define FIRST_LEGACY_IRQ 32 47*e307cc89SJoseph Lo #define TEGRA_MAX_NUM_ICTLRS 5 48d1d8c666SColin Cross 49d4b92fb2SJoseph Lo #define SGI_MASK 0xFFFF 50d4b92fb2SJoseph Lo 51caa4868eSPeter De Schrijver static int num_ictlrs; 52caa4868eSPeter De Schrijver 53d1d8c666SColin Cross static void __iomem *ictlr_reg_base[] = { 54d1d8c666SColin Cross IO_ADDRESS(TEGRA_PRIMARY_ICTLR_BASE), 55d1d8c666SColin Cross IO_ADDRESS(TEGRA_SECONDARY_ICTLR_BASE), 56d1d8c666SColin Cross IO_ADDRESS(TEGRA_TERTIARY_ICTLR_BASE), 57d1d8c666SColin Cross IO_ADDRESS(TEGRA_QUATERNARY_ICTLR_BASE), 58caa4868eSPeter De Schrijver IO_ADDRESS(TEGRA_QUINARY_ICTLR_BASE), 59d1d8c666SColin Cross }; 60d1d8c666SColin Cross 61*e307cc89SJoseph Lo #ifdef CONFIG_PM_SLEEP 62*e307cc89SJoseph Lo static u32 cop_ier[TEGRA_MAX_NUM_ICTLRS]; 63*e307cc89SJoseph Lo static u32 cop_iep[TEGRA_MAX_NUM_ICTLRS]; 64*e307cc89SJoseph Lo static u32 cpu_ier[TEGRA_MAX_NUM_ICTLRS]; 65*e307cc89SJoseph Lo static u32 cpu_iep[TEGRA_MAX_NUM_ICTLRS]; 66*e307cc89SJoseph Lo 67*e307cc89SJoseph Lo static u32 ictlr_wake_mask[TEGRA_MAX_NUM_ICTLRS]; 68*e307cc89SJoseph Lo #endif 69*e307cc89SJoseph Lo 70d4b92fb2SJoseph Lo bool tegra_pending_sgi(void) 71d4b92fb2SJoseph Lo { 72d4b92fb2SJoseph Lo u32 pending_set; 73d4b92fb2SJoseph Lo void __iomem *distbase = IO_ADDRESS(TEGRA_ARM_INT_DIST_BASE); 74d4b92fb2SJoseph Lo 75d4b92fb2SJoseph Lo pending_set = readl_relaxed(distbase + GIC_DIST_PENDING_SET); 76d4b92fb2SJoseph Lo 77d4b92fb2SJoseph Lo if (pending_set & SGI_MASK) 78d4b92fb2SJoseph Lo return true; 79d4b92fb2SJoseph Lo 80d4b92fb2SJoseph Lo return false; 81d4b92fb2SJoseph Lo } 82d4b92fb2SJoseph Lo 83d1d8c666SColin Cross static inline void tegra_irq_write_mask(unsigned int irq, unsigned long reg) 84d1d8c666SColin Cross { 85d1d8c666SColin Cross void __iomem *base; 86d1d8c666SColin Cross u32 mask; 87d1d8c666SColin Cross 88d1d8c666SColin Cross BUG_ON(irq < FIRST_LEGACY_IRQ || 89caa4868eSPeter De Schrijver irq >= FIRST_LEGACY_IRQ + num_ictlrs * 32); 90d1d8c666SColin Cross 91d1d8c666SColin Cross base = ictlr_reg_base[(irq - FIRST_LEGACY_IRQ) / 32]; 92d1d8c666SColin Cross mask = BIT((irq - FIRST_LEGACY_IRQ) % 32); 93d1d8c666SColin Cross 94d1d8c666SColin Cross __raw_writel(mask, base + reg); 95d1d8c666SColin Cross } 96d1d8c666SColin Cross 9737337a8dSLennert Buytenhek static void tegra_mask(struct irq_data *d) 98460907bcSGary King { 99d1d8c666SColin Cross if (d->irq < FIRST_LEGACY_IRQ) 100d1d8c666SColin Cross return; 101d1d8c666SColin Cross 102d1d8c666SColin Cross tegra_irq_write_mask(d->irq, ICTLR_CPU_IER_CLR); 103460907bcSGary King } 104460907bcSGary King 10537337a8dSLennert Buytenhek static void tegra_unmask(struct irq_data *d) 106460907bcSGary King { 107d1d8c666SColin Cross if (d->irq < FIRST_LEGACY_IRQ) 108d1d8c666SColin Cross return; 109d1d8c666SColin Cross 110d1d8c666SColin Cross tegra_irq_write_mask(d->irq, ICTLR_CPU_IER_SET); 111460907bcSGary King } 112460907bcSGary King 11326d902c0SColin Cross static void tegra_ack(struct irq_data *d) 11426d902c0SColin Cross { 115d1d8c666SColin Cross if (d->irq < FIRST_LEGACY_IRQ) 116d1d8c666SColin Cross return; 117d1d8c666SColin Cross 118d1d8c666SColin Cross tegra_irq_write_mask(d->irq, ICTLR_CPU_IEP_FIR_CLR); 11926d902c0SColin Cross } 12026d902c0SColin Cross 1214bd66cfdSColin Cross static void tegra_eoi(struct irq_data *d) 1224bd66cfdSColin Cross { 1234bd66cfdSColin Cross if (d->irq < FIRST_LEGACY_IRQ) 1244bd66cfdSColin Cross return; 1254bd66cfdSColin Cross 1264bd66cfdSColin Cross tegra_irq_write_mask(d->irq, ICTLR_CPU_IEP_FIR_CLR); 1274bd66cfdSColin Cross } 1284bd66cfdSColin Cross 12926d902c0SColin Cross static int tegra_retrigger(struct irq_data *d) 13026d902c0SColin Cross { 131d1d8c666SColin Cross if (d->irq < FIRST_LEGACY_IRQ) 132938fa349SColin Cross return 0; 133938fa349SColin Cross 134d1d8c666SColin Cross tegra_irq_write_mask(d->irq, ICTLR_CPU_IEP_FIR_SET); 135d1d8c666SColin Cross 13626d902c0SColin Cross return 1; 13726d902c0SColin Cross } 13826d902c0SColin Cross 139*e307cc89SJoseph Lo #ifdef CONFIG_PM_SLEEP 140*e307cc89SJoseph Lo static int tegra_set_wake(struct irq_data *d, unsigned int enable) 141*e307cc89SJoseph Lo { 142*e307cc89SJoseph Lo u32 irq = d->irq; 143*e307cc89SJoseph Lo u32 index, mask; 144*e307cc89SJoseph Lo 145*e307cc89SJoseph Lo if (irq < FIRST_LEGACY_IRQ || 146*e307cc89SJoseph Lo irq >= FIRST_LEGACY_IRQ + num_ictlrs * 32) 147*e307cc89SJoseph Lo return -EINVAL; 148*e307cc89SJoseph Lo 149*e307cc89SJoseph Lo index = ((irq - FIRST_LEGACY_IRQ) / 32); 150*e307cc89SJoseph Lo mask = BIT((irq - FIRST_LEGACY_IRQ) % 32); 151*e307cc89SJoseph Lo if (enable) 152*e307cc89SJoseph Lo ictlr_wake_mask[index] |= mask; 153*e307cc89SJoseph Lo else 154*e307cc89SJoseph Lo ictlr_wake_mask[index] &= ~mask; 155*e307cc89SJoseph Lo 156*e307cc89SJoseph Lo return 0; 157*e307cc89SJoseph Lo } 158*e307cc89SJoseph Lo 159*e307cc89SJoseph Lo static int tegra_legacy_irq_suspend(void) 160*e307cc89SJoseph Lo { 161*e307cc89SJoseph Lo unsigned long flags; 162*e307cc89SJoseph Lo int i; 163*e307cc89SJoseph Lo 164*e307cc89SJoseph Lo local_irq_save(flags); 165*e307cc89SJoseph Lo for (i = 0; i < num_ictlrs; i++) { 166*e307cc89SJoseph Lo void __iomem *ictlr = ictlr_reg_base[i]; 167*e307cc89SJoseph Lo /* Save interrupt state */ 168*e307cc89SJoseph Lo cpu_ier[i] = readl_relaxed(ictlr + ICTLR_CPU_IER); 169*e307cc89SJoseph Lo cpu_iep[i] = readl_relaxed(ictlr + ICTLR_CPU_IEP_CLASS); 170*e307cc89SJoseph Lo cop_ier[i] = readl_relaxed(ictlr + ICTLR_COP_IER); 171*e307cc89SJoseph Lo cop_iep[i] = readl_relaxed(ictlr + ICTLR_COP_IEP_CLASS); 172*e307cc89SJoseph Lo 173*e307cc89SJoseph Lo /* Disable COP interrupts */ 174*e307cc89SJoseph Lo writel_relaxed(~0ul, ictlr + ICTLR_COP_IER_CLR); 175*e307cc89SJoseph Lo 176*e307cc89SJoseph Lo /* Disable CPU interrupts */ 177*e307cc89SJoseph Lo writel_relaxed(~0ul, ictlr + ICTLR_CPU_IER_CLR); 178*e307cc89SJoseph Lo 179*e307cc89SJoseph Lo /* Enable the wakeup sources of ictlr */ 180*e307cc89SJoseph Lo writel_relaxed(ictlr_wake_mask[i], ictlr + ICTLR_CPU_IER_SET); 181*e307cc89SJoseph Lo } 182*e307cc89SJoseph Lo local_irq_restore(flags); 183*e307cc89SJoseph Lo 184*e307cc89SJoseph Lo return 0; 185*e307cc89SJoseph Lo } 186*e307cc89SJoseph Lo 187*e307cc89SJoseph Lo static void tegra_legacy_irq_resume(void) 188*e307cc89SJoseph Lo { 189*e307cc89SJoseph Lo unsigned long flags; 190*e307cc89SJoseph Lo int i; 191*e307cc89SJoseph Lo 192*e307cc89SJoseph Lo local_irq_save(flags); 193*e307cc89SJoseph Lo for (i = 0; i < num_ictlrs; i++) { 194*e307cc89SJoseph Lo void __iomem *ictlr = ictlr_reg_base[i]; 195*e307cc89SJoseph Lo writel_relaxed(cpu_iep[i], ictlr + ICTLR_CPU_IEP_CLASS); 196*e307cc89SJoseph Lo writel_relaxed(~0ul, ictlr + ICTLR_CPU_IER_CLR); 197*e307cc89SJoseph Lo writel_relaxed(cpu_ier[i], ictlr + ICTLR_CPU_IER_SET); 198*e307cc89SJoseph Lo writel_relaxed(cop_iep[i], ictlr + ICTLR_COP_IEP_CLASS); 199*e307cc89SJoseph Lo writel_relaxed(~0ul, ictlr + ICTLR_COP_IER_CLR); 200*e307cc89SJoseph Lo writel_relaxed(cop_ier[i], ictlr + ICTLR_COP_IER_SET); 201*e307cc89SJoseph Lo } 202*e307cc89SJoseph Lo local_irq_restore(flags); 203*e307cc89SJoseph Lo } 204*e307cc89SJoseph Lo 205*e307cc89SJoseph Lo static struct syscore_ops tegra_legacy_irq_syscore_ops = { 206*e307cc89SJoseph Lo .suspend = tegra_legacy_irq_suspend, 207*e307cc89SJoseph Lo .resume = tegra_legacy_irq_resume, 208*e307cc89SJoseph Lo }; 209*e307cc89SJoseph Lo 210*e307cc89SJoseph Lo int tegra_legacy_irq_syscore_init(void) 211*e307cc89SJoseph Lo { 212*e307cc89SJoseph Lo register_syscore_ops(&tegra_legacy_irq_syscore_ops); 213*e307cc89SJoseph Lo 214*e307cc89SJoseph Lo return 0; 215*e307cc89SJoseph Lo } 216*e307cc89SJoseph Lo #else 217*e307cc89SJoseph Lo #define tegra_set_wake NULL 218*e307cc89SJoseph Lo #endif 219*e307cc89SJoseph Lo 2205ad36c5fSErik Gilling void __init tegra_init_irq(void) 2215ad36c5fSErik Gilling { 222d1d8c666SColin Cross int i; 223caa4868eSPeter De Schrijver void __iomem *distbase; 224d1d8c666SColin Cross 225caa4868eSPeter De Schrijver distbase = IO_ADDRESS(TEGRA_ARM_INT_DIST_BASE); 226caa4868eSPeter De Schrijver num_ictlrs = readl_relaxed(distbase + GIC_DIST_CTR) & 0x1f; 227caa4868eSPeter De Schrijver 228caa4868eSPeter De Schrijver if (num_ictlrs > ARRAY_SIZE(ictlr_reg_base)) { 229caa4868eSPeter De Schrijver WARN(1, "Too many (%d) interrupt controllers found. Maximum is %d.", 230caa4868eSPeter De Schrijver num_ictlrs, ARRAY_SIZE(ictlr_reg_base)); 231caa4868eSPeter De Schrijver num_ictlrs = ARRAY_SIZE(ictlr_reg_base); 232caa4868eSPeter De Schrijver } 233caa4868eSPeter De Schrijver 234caa4868eSPeter De Schrijver for (i = 0; i < num_ictlrs; i++) { 235d1d8c666SColin Cross void __iomem *ictlr = ictlr_reg_base[i]; 236d1d8c666SColin Cross writel(~0, ictlr + ICTLR_CPU_IER_CLR); 237d1d8c666SColin Cross writel(0, ictlr + ICTLR_CPU_IEP_CLASS); 238d1d8c666SColin Cross } 239460907bcSGary King 240938fa349SColin Cross gic_arch_extn.irq_ack = tegra_ack; 2414bd66cfdSColin Cross gic_arch_extn.irq_eoi = tegra_eoi; 242938fa349SColin Cross gic_arch_extn.irq_mask = tegra_mask; 243938fa349SColin Cross gic_arch_extn.irq_unmask = tegra_unmask; 244938fa349SColin Cross gic_arch_extn.irq_retrigger = tegra_retrigger; 245*e307cc89SJoseph Lo gic_arch_extn.irq_set_wake = tegra_set_wake; 246*e307cc89SJoseph Lo gic_arch_extn.flags = IRQCHIP_MASK_ON_SUSPEND; 247938fa349SColin Cross 2480d4f7479Spdeschrijver@nvidia.com /* 2490d4f7479Spdeschrijver@nvidia.com * Check if there is a devicetree present, since the GIC will be 2500d4f7479Spdeschrijver@nvidia.com * initialized elsewhere under DT. 2510d4f7479Spdeschrijver@nvidia.com */ 2520d4f7479Spdeschrijver@nvidia.com if (!of_have_populated_dt()) 253caa4868eSPeter De Schrijver gic_init(0, 29, distbase, 254b580b899SRussell King IO_ADDRESS(TEGRA_ARM_PERIF_BASE + 0x100)); 255460907bcSGary King } 256