blob: 610f0253954dbb5151bb87215d3ab0d424e3ef0e [file] [log] [blame]
Andrew Isaacsonf137e462005-10-19 23:56:38 -07001/*
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 Baechlee9f874b2007-10-11 23:46:10 +010018#include <linux/clockchips.h>
Andrew Isaacsonf137e462005-10-19 23:56:38 -070019#include <linux/interrupt.h>
Ralf Baechled0453362007-10-22 10:38:44 +010020#include <linux/irq.h>
Ralf Baechled527eef2007-10-19 08:22:38 +010021#include <linux/percpu.h>
Andrew Isaacsonf137e462005-10-19 23:56:38 -070022#include <linux/spinlock.h>
Andrew Isaacsonf137e462005-10-19 23:56:38 -070023
Andrew Isaacsonf137e462005-10-19 23:56:38 -070024#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
40extern int bcm1480_steal_irq(int irq);
41
Ralf Baechlee9f874b2007-10-11 23:46:10 +010042/*
Ralf Baechled0453362007-10-22 10:38:44 +010043 * The general purpose timer ticks at 1MHz independent if
Ralf Baechlee9f874b2007-10-11 23:46:10 +010044 * the rest of the system
45 */
46static 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 Nemoto9ee5389c2007-10-18 00:57:07 +090053 timer_init = IOADDR(A_SCD_TIMER_REGISTER(cpu, R_SCD_TIMER_INIT));
Ralf Baechlee9f874b2007-10-11 23:46:10 +010054
55 switch (mode) {
56 case CLOCK_EVT_MODE_PERIODIC:
57 __raw_writeq(0, timer_cfg);
Ralf Baechled0453362007-10-22 10:38:44 +010058 __raw_writeq((V_SCD_TIMER_FREQ / HZ) - 1, timer_init);
Ralf Baechlee9f874b2007-10-11 23:46:10 +010059 __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 Baechled527eef2007-10-19 08:22:38 +010070 case CLOCK_EVT_MODE_RESUME:
Ralf Baechlee9f874b2007-10-11 23:46:10 +010071 ;
72 }
73}
74
Ralf Baechled527eef2007-10-19 08:22:38 +010075static 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 Baechlee9f874b2007-10-11 23:46:10 +010091static irqreturn_t sibyte_counter_handler(int irq, void *dev_id)
92{
Ralf Baechlee9f874b2007-10-11 23:46:10 +010093 unsigned int cpu = smp_processor_id();
Ralf Baechled0453362007-10-22 10:38:44 +010094 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 Baechlee9f874b2007-10-11 23:46:10 +010098
99 /* Reset the timer */
Ralf Baechled527eef2007-10-19 08:22:38 +0100100 __raw_writeq(M_SCD_TIMER_ENABLE | M_SCD_TIMER_MODE_CONTINUOUS,
Ralf Baechled0453362007-10-22 10:38:44 +0100101 timer_cfg);
Ralf Baechlee9f874b2007-10-11 23:46:10 +0100102 cd->event_handler(cd);
103
104 return IRQ_HANDLED;
105}
106
Ralf Baechled0453362007-10-22 10:38:44 +0100107static DEFINE_PER_CPU(struct clock_event_device, sibyte_hpt_clockevent);
108static DEFINE_PER_CPU(struct irqaction, sibyte_hpt_irqaction);
109static DEFINE_PER_CPU(char [18], sibyte_hpt_name);
Ralf Baechlee9f874b2007-10-11 23:46:10 +0100110
Ralf Baechled527eef2007-10-19 08:22:38 +0100111void __cpuinit sb1480_clockevent_init(void)
Ralf Baechlee9f874b2007-10-11 23:46:10 +0100112{
113 unsigned int cpu = smp_processor_id();
114 unsigned int irq = K_BCM1480_INT_TIMER_0 + cpu;
Ralf Baechled0453362007-10-22 10:38:44 +0100115 struct irqaction *action = &per_cpu(sibyte_hpt_irqaction, cpu);
Ralf Baechled527eef2007-10-19 08:22:38 +0100116 struct clock_event_device *cd = &per_cpu(sibyte_hpt_clockevent, cpu);
Ralf Baechled0453362007-10-22 10:38:44 +0100117 unsigned char *name = per_cpu(sibyte_hpt_name, cpu);
Ralf Baechled527eef2007-10-19 08:22:38 +0100118
Ralf Baechled0453362007-10-22 10:38:44 +0100119 BUG_ON(cpu > 3); /* Only have 4 general purpose timers */
120
121 sprintf(name, "bcm1480-counter %d", cpu);
122 cd->name = name;
Ralf Baechled527eef2007-10-19 08:22:38 +0100123 cd->features = CLOCK_EVT_FEAT_PERIODIC |
124 CLOCK_EVT_MODE_ONESHOT;
Ralf Baechled0453362007-10-22 10:38:44 +0100125 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 Baechled527eef2007-10-19 08:22:38 +0100131 cd->set_next_event = sibyte_next_event;
132 cd->set_mode = sibyte_set_mode;
Ralf Baechled0453362007-10-22 10:38:44 +0100133 clockevents_register_device(cd);
Ralf Baechlee9f874b2007-10-11 23:46:10 +0100134
Ralf Baechled0453362007-10-22 10:38:44 +0100135 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 Isaacsonf137e462005-10-19 23:56:38 -0700152}
153
Atsushi Nemoto00598562006-11-12 00:10:28 +0900154static cycle_t bcm1480_hpt_read(void)
Andrew Isaacsonf137e462005-10-19 23:56:38 -0700155{
Ralf Baechled0453362007-10-22 10:38:44 +0100156 return (cycle_t) __raw_readq(IOADDR(A_SCD_ZBBUS_CYCLE_COUNT));
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900157}
Andrew Isaacsonf137e462005-10-19 23:56:38 -0700158
Ralf Baechled527eef2007-10-19 08:22:38 +0100159struct clocksource bcm1480_clocksource = {
Ralf Baechled0453362007-10-22 10:38:44 +0100160 .name = "zbbus-cycles",
Ralf Baechled527eef2007-10-19 08:22:38 +0100161 .rating = 200,
162 .read = bcm1480_hpt_read,
Ralf Baechled0453362007-10-22 10:38:44 +0100163 .mask = CLOCKSOURCE_MASK(64),
Ralf Baechled527eef2007-10-19 08:22:38 +0100164 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
165};
166
167void __init sb1480_clocksource_init(void)
168{
169 struct clocksource *cs = &bcm1480_clocksource;
Ralf Baechled0453362007-10-22 10:38:44 +0100170 unsigned int plldiv;
171 unsigned long zbbus;
Ralf Baechled527eef2007-10-19 08:22:38 +0100172
Ralf Baechled0453362007-10-22 10:38:44 +0100173 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 Baechled527eef2007-10-19 08:22:38 +0100176 clocksource_register(cs);
177}
178
Ralf Baechled0453362007-10-22 10:38:44 +0100179void __init plat_time_init(void)
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900180{
Ralf Baechled527eef2007-10-19 08:22:38 +0100181 sb1480_clocksource_init();
Ralf Baechlee9f874b2007-10-11 23:46:10 +0100182 sb1480_clockevent_init();
Andrew Isaacsonf137e462005-10-19 23:56:38 -0700183}