Andrew Isaacson | f137e46 | 2005-10-19 23:56:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2000,2001,2004 Broadcom Corporation |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
Ralf Baechle | e9f874b | 2007-10-11 23:46:10 +0100 | [diff] [blame] | 18 | #include <linux/clockchips.h> |
Andrew Isaacson | f137e46 | 2005-10-19 23:56:38 -0700 | [diff] [blame] | 19 | #include <linux/interrupt.h> |
Ralf Baechle | d045336 | 2007-10-22 10:38:44 +0100 | [diff] [blame^] | 20 | #include <linux/irq.h> |
Ralf Baechle | d527eef | 2007-10-19 08:22:38 +0100 | [diff] [blame] | 21 | #include <linux/percpu.h> |
Andrew Isaacson | f137e46 | 2005-10-19 23:56:38 -0700 | [diff] [blame] | 22 | #include <linux/spinlock.h> |
Andrew Isaacson | f137e46 | 2005-10-19 23:56:38 -0700 | [diff] [blame] | 23 | |
Andrew Isaacson | f137e46 | 2005-10-19 23:56:38 -0700 | [diff] [blame] | 24 | #include <asm/addrspace.h> |
| 25 | #include <asm/time.h> |
| 26 | #include <asm/io.h> |
| 27 | |
| 28 | #include <asm/sibyte/bcm1480_regs.h> |
| 29 | #include <asm/sibyte/sb1250_regs.h> |
| 30 | #include <asm/sibyte/bcm1480_int.h> |
| 31 | #include <asm/sibyte/bcm1480_scd.h> |
| 32 | |
| 33 | #include <asm/sibyte/sb1250.h> |
| 34 | |
| 35 | |
| 36 | #define IMR_IP2_VAL K_BCM1480_INT_MAP_I0 |
| 37 | #define IMR_IP3_VAL K_BCM1480_INT_MAP_I1 |
| 38 | #define IMR_IP4_VAL K_BCM1480_INT_MAP_I2 |
| 39 | |
| 40 | extern int bcm1480_steal_irq(int irq); |
| 41 | |
Ralf Baechle | e9f874b | 2007-10-11 23:46:10 +0100 | [diff] [blame] | 42 | /* |
Ralf Baechle | d045336 | 2007-10-22 10:38:44 +0100 | [diff] [blame^] | 43 | * The general purpose timer ticks at 1MHz independent if |
Ralf Baechle | e9f874b | 2007-10-11 23:46:10 +0100 | [diff] [blame] | 44 | * the rest of the system |
| 45 | */ |
| 46 | static void sibyte_set_mode(enum clock_event_mode mode, |
| 47 | struct clock_event_device *evt) |
| 48 | { |
| 49 | unsigned int cpu = smp_processor_id(); |
| 50 | void __iomem *timer_cfg, *timer_init; |
| 51 | |
| 52 | timer_cfg = IOADDR(A_SCD_TIMER_REGISTER(cpu, R_SCD_TIMER_CFG)); |
Atsushi Nemoto | 9ee5389c | 2007-10-18 00:57:07 +0900 | [diff] [blame] | 53 | timer_init = IOADDR(A_SCD_TIMER_REGISTER(cpu, R_SCD_TIMER_INIT)); |
Ralf Baechle | e9f874b | 2007-10-11 23:46:10 +0100 | [diff] [blame] | 54 | |
| 55 | switch (mode) { |
| 56 | case CLOCK_EVT_MODE_PERIODIC: |
| 57 | __raw_writeq(0, timer_cfg); |
Ralf Baechle | d045336 | 2007-10-22 10:38:44 +0100 | [diff] [blame^] | 58 | __raw_writeq((V_SCD_TIMER_FREQ / HZ) - 1, timer_init); |
Ralf Baechle | e9f874b | 2007-10-11 23:46:10 +0100 | [diff] [blame] | 59 | __raw_writeq(M_SCD_TIMER_ENABLE | M_SCD_TIMER_MODE_CONTINUOUS, |
| 60 | timer_cfg); |
| 61 | break; |
| 62 | |
| 63 | case CLOCK_EVT_MODE_ONESHOT: |
| 64 | /* Stop the timer until we actually program a shot */ |
| 65 | case CLOCK_EVT_MODE_SHUTDOWN: |
| 66 | __raw_writeq(0, timer_cfg); |
| 67 | break; |
| 68 | |
| 69 | case CLOCK_EVT_MODE_UNUSED: /* shuddup gcc */ |
Ralf Baechle | d527eef | 2007-10-19 08:22:38 +0100 | [diff] [blame] | 70 | case CLOCK_EVT_MODE_RESUME: |
Ralf Baechle | e9f874b | 2007-10-11 23:46:10 +0100 | [diff] [blame] | 71 | ; |
| 72 | } |
| 73 | } |
| 74 | |
Ralf Baechle | d527eef | 2007-10-19 08:22:38 +0100 | [diff] [blame] | 75 | static int sibyte_next_event(unsigned long delta, struct clock_event_device *cd) |
| 76 | { |
| 77 | unsigned int cpu = smp_processor_id(); |
| 78 | void __iomem *timer_init; |
| 79 | unsigned int cnt; |
| 80 | int res; |
| 81 | |
| 82 | timer_init = IOADDR(A_SCD_TIMER_REGISTER(cpu, R_SCD_TIMER_INIT)); |
| 83 | cnt = __raw_readq(timer_init); |
| 84 | cnt += delta; |
| 85 | __raw_writeq(cnt, timer_init); |
| 86 | res = ((long)(__raw_readq(timer_init) - cnt ) > 0) ? -ETIME : 0; |
| 87 | |
| 88 | return res; |
| 89 | } |
| 90 | |
Ralf Baechle | e9f874b | 2007-10-11 23:46:10 +0100 | [diff] [blame] | 91 | static irqreturn_t sibyte_counter_handler(int irq, void *dev_id) |
| 92 | { |
Ralf Baechle | e9f874b | 2007-10-11 23:46:10 +0100 | [diff] [blame] | 93 | unsigned int cpu = smp_processor_id(); |
Ralf Baechle | d045336 | 2007-10-22 10:38:44 +0100 | [diff] [blame^] | 94 | struct clock_event_device *cd = dev_id; |
| 95 | void __iomem *timer_cfg; |
| 96 | |
| 97 | timer_cfg = IOADDR(A_SCD_TIMER_REGISTER(cpu, R_SCD_TIMER_CFG)); |
Ralf Baechle | e9f874b | 2007-10-11 23:46:10 +0100 | [diff] [blame] | 98 | |
| 99 | /* Reset the timer */ |
Ralf Baechle | d527eef | 2007-10-19 08:22:38 +0100 | [diff] [blame] | 100 | __raw_writeq(M_SCD_TIMER_ENABLE | M_SCD_TIMER_MODE_CONTINUOUS, |
Ralf Baechle | d045336 | 2007-10-22 10:38:44 +0100 | [diff] [blame^] | 101 | timer_cfg); |
Ralf Baechle | e9f874b | 2007-10-11 23:46:10 +0100 | [diff] [blame] | 102 | cd->event_handler(cd); |
| 103 | |
| 104 | return IRQ_HANDLED; |
| 105 | } |
| 106 | |
Ralf Baechle | d045336 | 2007-10-22 10:38:44 +0100 | [diff] [blame^] | 107 | static DEFINE_PER_CPU(struct clock_event_device, sibyte_hpt_clockevent); |
| 108 | static DEFINE_PER_CPU(struct irqaction, sibyte_hpt_irqaction); |
| 109 | static DEFINE_PER_CPU(char [18], sibyte_hpt_name); |
Ralf Baechle | e9f874b | 2007-10-11 23:46:10 +0100 | [diff] [blame] | 110 | |
Ralf Baechle | d527eef | 2007-10-19 08:22:38 +0100 | [diff] [blame] | 111 | void __cpuinit sb1480_clockevent_init(void) |
Ralf Baechle | e9f874b | 2007-10-11 23:46:10 +0100 | [diff] [blame] | 112 | { |
| 113 | unsigned int cpu = smp_processor_id(); |
| 114 | unsigned int irq = K_BCM1480_INT_TIMER_0 + cpu; |
Ralf Baechle | d045336 | 2007-10-22 10:38:44 +0100 | [diff] [blame^] | 115 | struct irqaction *action = &per_cpu(sibyte_hpt_irqaction, cpu); |
Ralf Baechle | d527eef | 2007-10-19 08:22:38 +0100 | [diff] [blame] | 116 | struct clock_event_device *cd = &per_cpu(sibyte_hpt_clockevent, cpu); |
Ralf Baechle | d045336 | 2007-10-22 10:38:44 +0100 | [diff] [blame^] | 117 | unsigned char *name = per_cpu(sibyte_hpt_name, cpu); |
Ralf Baechle | d527eef | 2007-10-19 08:22:38 +0100 | [diff] [blame] | 118 | |
Ralf Baechle | d045336 | 2007-10-22 10:38:44 +0100 | [diff] [blame^] | 119 | BUG_ON(cpu > 3); /* Only have 4 general purpose timers */ |
| 120 | |
| 121 | sprintf(name, "bcm1480-counter %d", cpu); |
| 122 | cd->name = name; |
Ralf Baechle | d527eef | 2007-10-19 08:22:38 +0100 | [diff] [blame] | 123 | cd->features = CLOCK_EVT_FEAT_PERIODIC | |
| 124 | CLOCK_EVT_MODE_ONESHOT; |
Ralf Baechle | d045336 | 2007-10-22 10:38:44 +0100 | [diff] [blame^] | 125 | clockevent_set_clock(cd, V_SCD_TIMER_FREQ); |
| 126 | cd->max_delta_ns = clockevent_delta2ns(0x7fffff, cd); |
| 127 | cd->min_delta_ns = clockevent_delta2ns(1, cd); |
| 128 | cd->rating = 200; |
| 129 | cd->irq = irq; |
| 130 | cd->cpumask = cpumask_of_cpu(cpu); |
Ralf Baechle | d527eef | 2007-10-19 08:22:38 +0100 | [diff] [blame] | 131 | cd->set_next_event = sibyte_next_event; |
| 132 | cd->set_mode = sibyte_set_mode; |
Ralf Baechle | d045336 | 2007-10-22 10:38:44 +0100 | [diff] [blame^] | 133 | clockevents_register_device(cd); |
Ralf Baechle | e9f874b | 2007-10-11 23:46:10 +0100 | [diff] [blame] | 134 | |
Ralf Baechle | d045336 | 2007-10-22 10:38:44 +0100 | [diff] [blame^] | 135 | bcm1480_mask_irq(cpu, irq); |
| 136 | |
| 137 | /* |
| 138 | * Map timer interrupt to IP[4] of this cpu |
| 139 | */ |
| 140 | __raw_writeq(IMR_IP4_VAL, |
| 141 | IOADDR(A_BCM1480_IMR_REGISTER(cpu, |
| 142 | R_BCM1480_IMR_INTERRUPT_MAP_BASE_H) + (irq << 3))); |
| 143 | |
| 144 | bcm1480_unmask_irq(cpu, irq); |
| 145 | bcm1480_steal_irq(irq); |
| 146 | |
| 147 | action->handler = sibyte_counter_handler; |
| 148 | action->flags = IRQF_DISABLED | IRQF_PERCPU; |
| 149 | action->name = name; |
| 150 | action->dev_id = cd; |
| 151 | setup_irq(irq, action); |
Andrew Isaacson | f137e46 | 2005-10-19 23:56:38 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Atsushi Nemoto | 0059856 | 2006-11-12 00:10:28 +0900 | [diff] [blame] | 154 | static cycle_t bcm1480_hpt_read(void) |
Andrew Isaacson | f137e46 | 2005-10-19 23:56:38 -0700 | [diff] [blame] | 155 | { |
Ralf Baechle | d045336 | 2007-10-22 10:38:44 +0100 | [diff] [blame^] | 156 | return (cycle_t) __raw_readq(IOADDR(A_SCD_ZBBUS_CYCLE_COUNT)); |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 157 | } |
Andrew Isaacson | f137e46 | 2005-10-19 23:56:38 -0700 | [diff] [blame] | 158 | |
Ralf Baechle | d527eef | 2007-10-19 08:22:38 +0100 | [diff] [blame] | 159 | struct clocksource bcm1480_clocksource = { |
Ralf Baechle | d045336 | 2007-10-22 10:38:44 +0100 | [diff] [blame^] | 160 | .name = "zbbus-cycles", |
Ralf Baechle | d527eef | 2007-10-19 08:22:38 +0100 | [diff] [blame] | 161 | .rating = 200, |
| 162 | .read = bcm1480_hpt_read, |
Ralf Baechle | d045336 | 2007-10-22 10:38:44 +0100 | [diff] [blame^] | 163 | .mask = CLOCKSOURCE_MASK(64), |
Ralf Baechle | d527eef | 2007-10-19 08:22:38 +0100 | [diff] [blame] | 164 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 165 | }; |
| 166 | |
| 167 | void __init sb1480_clocksource_init(void) |
| 168 | { |
| 169 | struct clocksource *cs = &bcm1480_clocksource; |
Ralf Baechle | d045336 | 2007-10-22 10:38:44 +0100 | [diff] [blame^] | 170 | unsigned int plldiv; |
| 171 | unsigned long zbbus; |
Ralf Baechle | d527eef | 2007-10-19 08:22:38 +0100 | [diff] [blame] | 172 | |
Ralf Baechle | d045336 | 2007-10-22 10:38:44 +0100 | [diff] [blame^] | 173 | plldiv = G_BCM1480_SYS_PLL_DIV(__raw_readq(IOADDR(A_SCD_SYSTEM_CFG))); |
| 174 | zbbus = ((plldiv >> 1) * 50000000) + ((plldiv & 1) * 25000000); |
| 175 | clocksource_set_clock(cs, zbbus); |
Ralf Baechle | d527eef | 2007-10-19 08:22:38 +0100 | [diff] [blame] | 176 | clocksource_register(cs); |
| 177 | } |
| 178 | |
Ralf Baechle | d045336 | 2007-10-22 10:38:44 +0100 | [diff] [blame^] | 179 | void __init plat_time_init(void) |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 180 | { |
Ralf Baechle | d527eef | 2007-10-19 08:22:38 +0100 | [diff] [blame] | 181 | sb1480_clocksource_init(); |
Ralf Baechle | e9f874b | 2007-10-11 23:46:10 +0100 | [diff] [blame] | 182 | sb1480_clockevent_init(); |
Andrew Isaacson | f137e46 | 2005-10-19 23:56:38 -0700 | [diff] [blame] | 183 | } |