Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Ralf Baechle | 217dd11 | 2007-11-01 01:57:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2000,2001,2004 Broadcom Corporation |
Ralf Baechle | 217dd11 | 2007-11-01 01:57:55 +0000 | [diff] [blame] | 4 | */ |
| 5 | #include <linux/clockchips.h> |
| 6 | #include <linux/interrupt.h> |
| 7 | #include <linux/percpu.h> |
Ralf Baechle | 631330f | 2009-06-19 14:05:26 +0100 | [diff] [blame] | 8 | #include <linux/smp.h> |
David Howells | ca4d3e67 | 2010-10-07 14:08:54 +0100 | [diff] [blame] | 9 | #include <linux/irq.h> |
Ralf Baechle | 217dd11 | 2007-11-01 01:57:55 +0000 | [diff] [blame] | 10 | |
| 11 | #include <asm/addrspace.h> |
| 12 | #include <asm/io.h> |
| 13 | #include <asm/time.h> |
| 14 | |
| 15 | #include <asm/sibyte/bcm1480_regs.h> |
| 16 | #include <asm/sibyte/sb1250_regs.h> |
| 17 | #include <asm/sibyte/bcm1480_int.h> |
| 18 | #include <asm/sibyte/bcm1480_scd.h> |
| 19 | |
| 20 | #include <asm/sibyte/sb1250.h> |
| 21 | |
| 22 | #define IMR_IP2_VAL K_BCM1480_INT_MAP_I0 |
| 23 | #define IMR_IP3_VAL K_BCM1480_INT_MAP_I1 |
| 24 | #define IMR_IP4_VAL K_BCM1480_INT_MAP_I2 |
| 25 | |
| 26 | /* |
| 27 | * The general purpose timer ticks at 1MHz independent if |
| 28 | * the rest of the system |
| 29 | */ |
Viresh Kumar | 1edf907 | 2015-07-06 16:41:55 +0530 | [diff] [blame] | 30 | |
| 31 | static int sibyte_set_periodic(struct clock_event_device *evt) |
Ralf Baechle | 217dd11 | 2007-11-01 01:57:55 +0000 | [diff] [blame] | 32 | { |
| 33 | unsigned int cpu = smp_processor_id(); |
| 34 | void __iomem *cfg, *init; |
| 35 | |
| 36 | cfg = IOADDR(A_SCD_TIMER_REGISTER(cpu, R_SCD_TIMER_CFG)); |
| 37 | init = IOADDR(A_SCD_TIMER_REGISTER(cpu, R_SCD_TIMER_INIT)); |
| 38 | |
Viresh Kumar | 1edf907 | 2015-07-06 16:41:55 +0530 | [diff] [blame] | 39 | __raw_writeq(0, cfg); |
| 40 | __raw_writeq((V_SCD_TIMER_FREQ / HZ) - 1, init); |
| 41 | __raw_writeq(M_SCD_TIMER_ENABLE | M_SCD_TIMER_MODE_CONTINUOUS, cfg); |
| 42 | return 0; |
| 43 | } |
Ralf Baechle | 217dd11 | 2007-11-01 01:57:55 +0000 | [diff] [blame] | 44 | |
Viresh Kumar | 1edf907 | 2015-07-06 16:41:55 +0530 | [diff] [blame] | 45 | static int sibyte_shutdown(struct clock_event_device *evt) |
| 46 | { |
| 47 | unsigned int cpu = smp_processor_id(); |
| 48 | void __iomem *cfg; |
Ralf Baechle | 217dd11 | 2007-11-01 01:57:55 +0000 | [diff] [blame] | 49 | |
Viresh Kumar | 1edf907 | 2015-07-06 16:41:55 +0530 | [diff] [blame] | 50 | cfg = IOADDR(A_SCD_TIMER_REGISTER(cpu, R_SCD_TIMER_CFG)); |
| 51 | |
| 52 | /* Stop the timer until we actually program a shot */ |
| 53 | __raw_writeq(0, cfg); |
| 54 | return 0; |
Ralf Baechle | 217dd11 | 2007-11-01 01:57:55 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static int sibyte_next_event(unsigned long delta, struct clock_event_device *cd) |
| 58 | { |
| 59 | unsigned int cpu = smp_processor_id(); |
| 60 | void __iomem *cfg, *init; |
| 61 | |
| 62 | cfg = IOADDR(A_SCD_TIMER_REGISTER(cpu, R_SCD_TIMER_CFG)); |
| 63 | init = IOADDR(A_SCD_TIMER_REGISTER(cpu, R_SCD_TIMER_INIT)); |
| 64 | |
Ralf Baechle | 8dfa741 | 2007-11-11 17:33:55 +0000 | [diff] [blame] | 65 | __raw_writeq(0, cfg); |
Ralf Baechle | 217dd11 | 2007-11-01 01:57:55 +0000 | [diff] [blame] | 66 | __raw_writeq(delta - 1, init); |
| 67 | __raw_writeq(M_SCD_TIMER_ENABLE, cfg); |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | static irqreturn_t sibyte_counter_handler(int irq, void *dev_id) |
| 73 | { |
| 74 | unsigned int cpu = smp_processor_id(); |
| 75 | struct clock_event_device *cd = dev_id; |
| 76 | void __iomem *cfg; |
| 77 | unsigned long tmode; |
| 78 | |
Viresh Kumar | 1edf907 | 2015-07-06 16:41:55 +0530 | [diff] [blame] | 79 | if (clockevent_state_periodic(cd)) |
Ralf Baechle | 217dd11 | 2007-11-01 01:57:55 +0000 | [diff] [blame] | 80 | tmode = M_SCD_TIMER_ENABLE | M_SCD_TIMER_MODE_CONTINUOUS; |
| 81 | else |
| 82 | tmode = 0; |
| 83 | |
| 84 | /* ACK interrupt */ |
| 85 | cfg = IOADDR(A_SCD_TIMER_REGISTER(cpu, R_SCD_TIMER_CFG)); |
| 86 | ____raw_writeq(tmode, cfg); |
| 87 | |
| 88 | cd->event_handler(cd); |
| 89 | |
| 90 | return IRQ_HANDLED; |
| 91 | } |
| 92 | |
| 93 | static DEFINE_PER_CPU(struct clock_event_device, sibyte_hpt_clockevent); |
| 94 | static DEFINE_PER_CPU(struct irqaction, sibyte_hpt_irqaction); |
| 95 | static DEFINE_PER_CPU(char [18], sibyte_hpt_name); |
| 96 | |
Paul Gortmaker | 078a55f | 2013-06-18 13:38:59 +0000 | [diff] [blame] | 97 | void sb1480_clockevent_init(void) |
Ralf Baechle | 217dd11 | 2007-11-01 01:57:55 +0000 | [diff] [blame] | 98 | { |
| 99 | unsigned int cpu = smp_processor_id(); |
| 100 | unsigned int irq = K_BCM1480_INT_TIMER_0 + cpu; |
| 101 | struct irqaction *action = &per_cpu(sibyte_hpt_irqaction, cpu); |
| 102 | struct clock_event_device *cd = &per_cpu(sibyte_hpt_clockevent, cpu); |
| 103 | unsigned char *name = per_cpu(sibyte_hpt_name, cpu); |
| 104 | |
| 105 | BUG_ON(cpu > 3); /* Only have 4 general purpose timers */ |
| 106 | |
| 107 | sprintf(name, "bcm1480-counter-%d", cpu); |
| 108 | cd->name = name; |
| 109 | cd->features = CLOCK_EVT_FEAT_PERIODIC | |
| 110 | CLOCK_EVT_FEAT_ONESHOT; |
| 111 | clockevent_set_clock(cd, V_SCD_TIMER_FREQ); |
| 112 | cd->max_delta_ns = clockevent_delta2ns(0x7fffff, cd); |
Nicolai Stange | e4db9253 | 2017-03-30 21:47:32 +0200 | [diff] [blame] | 113 | cd->max_delta_ticks = 0x7fffff; |
Ralf Baechle | 6224775 | 2007-11-11 17:24:46 +0000 | [diff] [blame] | 114 | cd->min_delta_ns = clockevent_delta2ns(2, cd); |
Nicolai Stange | e4db9253 | 2017-03-30 21:47:32 +0200 | [diff] [blame] | 115 | cd->min_delta_ticks = 2; |
Ralf Baechle | 217dd11 | 2007-11-01 01:57:55 +0000 | [diff] [blame] | 116 | cd->rating = 200; |
| 117 | cd->irq = irq; |
Rusty Russell | 320ab2b | 2008-12-13 21:20:26 +1030 | [diff] [blame] | 118 | cd->cpumask = cpumask_of(cpu); |
Ralf Baechle | 217dd11 | 2007-11-01 01:57:55 +0000 | [diff] [blame] | 119 | cd->set_next_event = sibyte_next_event; |
Viresh Kumar | 1edf907 | 2015-07-06 16:41:55 +0530 | [diff] [blame] | 120 | cd->set_state_shutdown = sibyte_shutdown; |
| 121 | cd->set_state_periodic = sibyte_set_periodic; |
| 122 | cd->set_state_oneshot = sibyte_shutdown; |
Ralf Baechle | 217dd11 | 2007-11-01 01:57:55 +0000 | [diff] [blame] | 123 | clockevents_register_device(cd); |
| 124 | |
| 125 | bcm1480_mask_irq(cpu, irq); |
| 126 | |
| 127 | /* |
| 128 | * Map the timer interrupt to IP[4] of this cpu |
| 129 | */ |
| 130 | __raw_writeq(IMR_IP4_VAL, |
| 131 | IOADDR(A_BCM1480_IMR_REGISTER(cpu, |
| 132 | R_BCM1480_IMR_INTERRUPT_MAP_BASE_H) + (irq << 3))); |
| 133 | |
| 134 | bcm1480_unmask_irq(cpu, irq); |
| 135 | |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 136 | action->handler = sibyte_counter_handler; |
Yong Zhang | 8b5690f | 2011-11-22 14:38:03 +0000 | [diff] [blame] | 137 | action->flags = IRQF_PERCPU | IRQF_TIMER; |
Ralf Baechle | 217dd11 | 2007-11-01 01:57:55 +0000 | [diff] [blame] | 138 | action->name = name; |
| 139 | action->dev_id = cd; |
Ralf Baechle | 07a80e4 | 2007-11-05 00:18:05 +0000 | [diff] [blame] | 140 | |
Rusty Russell | 0de2652 | 2008-12-13 21:20:26 +1030 | [diff] [blame] | 141 | irq_set_affinity(irq, cpumask_of(cpu)); |
Ralf Baechle | 217dd11 | 2007-11-01 01:57:55 +0000 | [diff] [blame] | 142 | setup_irq(irq, action); |
| 143 | } |