Loading...
1/*
2 * Copyright (C) 2008-2009 Manuel Lauss <manuel.lauss@gmail.com>
3 *
4 * Previous incarnations were:
5 * Copyright (C) 2001, 2006, 2008 MontaVista Software, <source@mvista.com>
6 * Copied and modified Carsten Langgaard's time.c
7 *
8 * Carsten Langgaard, carstenl@mips.com
9 * Copyright (C) 1999,2000 MIPS Technologies, Inc. All rights reserved.
10 *
11 * ########################################################################
12 *
13 * This program is free software; you can distribute it and/or modify it
14 * under the terms of the GNU General Public License (Version 2) as
15 * published by the Free Software Foundation.
16 *
17 * This program is distributed in the hope it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
25 *
26 * ########################################################################
27 *
28 * Clocksource/event using the 32.768kHz-clocked Counter1 ('RTC' in the
29 * databooks). Firmware/Board init code must enable the counters in the
30 * counter control register, otherwise the CP0 counter clocksource/event
31 * will be installed instead (and use of 'wait' instruction is prohibited).
32 */
33
34#include <linux/clockchips.h>
35#include <linux/clocksource.h>
36#include <linux/interrupt.h>
37#include <linux/spinlock.h>
38
39#include <asm/idle.h>
40#include <asm/processor.h>
41#include <asm/time.h>
42#include <asm/mach-au1x00/au1000.h>
43
44/* 32kHz clock enabled and detected */
45#define CNTR_OK (SYS_CNTRL_E0 | SYS_CNTRL_32S)
46
47static u64 au1x_counter1_read(struct clocksource *cs)
48{
49 return alchemy_rdsys(AU1000_SYS_RTCREAD);
50}
51
52static struct clocksource au1x_counter1_clocksource = {
53 .name = "alchemy-counter1",
54 .read = au1x_counter1_read,
55 .mask = CLOCKSOURCE_MASK(32),
56 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
57 .rating = 1500,
58};
59
60static int au1x_rtcmatch2_set_next_event(unsigned long delta,
61 struct clock_event_device *cd)
62{
63 delta += alchemy_rdsys(AU1000_SYS_RTCREAD);
64 /* wait for register access */
65 while (alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_M21)
66 ;
67 alchemy_wrsys(delta, AU1000_SYS_RTCMATCH2);
68
69 return 0;
70}
71
72static irqreturn_t au1x_rtcmatch2_irq(int irq, void *dev_id)
73{
74 struct clock_event_device *cd = dev_id;
75 cd->event_handler(cd);
76 return IRQ_HANDLED;
77}
78
79static struct clock_event_device au1x_rtcmatch2_clockdev = {
80 .name = "rtcmatch2",
81 .features = CLOCK_EVT_FEAT_ONESHOT,
82 .rating = 1500,
83 .set_next_event = au1x_rtcmatch2_set_next_event,
84 .cpumask = cpu_all_mask,
85};
86
87static struct irqaction au1x_rtcmatch2_irqaction = {
88 .handler = au1x_rtcmatch2_irq,
89 .flags = IRQF_TIMER,
90 .name = "timer",
91 .dev_id = &au1x_rtcmatch2_clockdev,
92};
93
94static int __init alchemy_time_init(unsigned int m2int)
95{
96 struct clock_event_device *cd = &au1x_rtcmatch2_clockdev;
97 unsigned long t;
98
99 au1x_rtcmatch2_clockdev.irq = m2int;
100
101 /* Check if firmware (YAMON, ...) has enabled 32kHz and clock
102 * has been detected. If so install the rtcmatch2 clocksource,
103 * otherwise don't bother. Note that both bits being set is by
104 * no means a definite guarantee that the counters actually work
105 * (the 32S bit seems to be stuck set to 1 once a single clock-
106 * edge is detected, hence the timeouts).
107 */
108 if (CNTR_OK != (alchemy_rdsys(AU1000_SYS_CNTRCTRL) & CNTR_OK))
109 goto cntr_err;
110
111 /*
112 * setup counter 1 (RTC) to tick at full speed
113 */
114 t = 0xffffff;
115 while ((alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_T1S) && --t)
116 asm volatile ("nop");
117 if (!t)
118 goto cntr_err;
119
120 alchemy_wrsys(0, AU1000_SYS_RTCTRIM); /* 32.768 kHz */
121
122 t = 0xffffff;
123 while ((alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_C1S) && --t)
124 asm volatile ("nop");
125 if (!t)
126 goto cntr_err;
127 alchemy_wrsys(0, AU1000_SYS_RTCWRITE);
128
129 t = 0xffffff;
130 while ((alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_C1S) && --t)
131 asm volatile ("nop");
132 if (!t)
133 goto cntr_err;
134
135 /* register counter1 clocksource and event device */
136 clocksource_register_hz(&au1x_counter1_clocksource, 32768);
137
138 cd->shift = 32;
139 cd->mult = div_sc(32768, NSEC_PER_SEC, cd->shift);
140 cd->max_delta_ns = clockevent_delta2ns(0xffffffff, cd);
141 cd->min_delta_ns = clockevent_delta2ns(9, cd); /* ~0.28ms */
142 clockevents_register_device(cd);
143 setup_irq(m2int, &au1x_rtcmatch2_irqaction);
144
145 printk(KERN_INFO "Alchemy clocksource installed\n");
146
147 return 0;
148
149cntr_err:
150 return -1;
151}
152
153static int alchemy_m2inttab[] __initdata = {
154 AU1000_RTC_MATCH2_INT,
155 AU1500_RTC_MATCH2_INT,
156 AU1100_RTC_MATCH2_INT,
157 AU1550_RTC_MATCH2_INT,
158 AU1200_RTC_MATCH2_INT,
159 AU1300_RTC_MATCH2_INT,
160};
161
162void __init plat_time_init(void)
163{
164 int t;
165
166 t = alchemy_get_cputype();
167 if (t == ALCHEMY_CPU_UNKNOWN ||
168 alchemy_time_init(alchemy_m2inttab[t]))
169 cpu_wait = NULL; /* wait doesn't work with r4k timer */
170}
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2008-2009 Manuel Lauss <manuel.lauss@gmail.com>
4 *
5 * Previous incarnations were:
6 * Copyright (C) 2001, 2006, 2008 MontaVista Software, <source@mvista.com>
7 * Copied and modified Carsten Langgaard's time.c
8 *
9 * Carsten Langgaard, carstenl@mips.com
10 * Copyright (C) 1999,2000 MIPS Technologies, Inc. All rights reserved.
11 *
12 * ########################################################################
13 *
14 * ########################################################################
15 *
16 * Clocksource/event using the 32.768kHz-clocked Counter1 ('RTC' in the
17 * databooks). Firmware/Board init code must enable the counters in the
18 * counter control register, otherwise the CP0 counter clocksource/event
19 * will be installed instead (and use of 'wait' instruction is prohibited).
20 */
21
22#include <linux/clockchips.h>
23#include <linux/clocksource.h>
24#include <linux/interrupt.h>
25#include <linux/spinlock.h>
26
27#include <asm/idle.h>
28#include <asm/processor.h>
29#include <asm/time.h>
30#include <asm/mach-au1x00/au1000.h>
31
32/* 32kHz clock enabled and detected */
33#define CNTR_OK (SYS_CNTRL_E0 | SYS_CNTRL_32S)
34
35static u64 au1x_counter1_read(struct clocksource *cs)
36{
37 return alchemy_rdsys(AU1000_SYS_RTCREAD);
38}
39
40static struct clocksource au1x_counter1_clocksource = {
41 .name = "alchemy-counter1",
42 .read = au1x_counter1_read,
43 .mask = CLOCKSOURCE_MASK(32),
44 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
45 .rating = 1500,
46};
47
48static int au1x_rtcmatch2_set_next_event(unsigned long delta,
49 struct clock_event_device *cd)
50{
51 delta += alchemy_rdsys(AU1000_SYS_RTCREAD);
52 /* wait for register access */
53 while (alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_M21)
54 ;
55 alchemy_wrsys(delta, AU1000_SYS_RTCMATCH2);
56
57 return 0;
58}
59
60static irqreturn_t au1x_rtcmatch2_irq(int irq, void *dev_id)
61{
62 struct clock_event_device *cd = dev_id;
63 cd->event_handler(cd);
64 return IRQ_HANDLED;
65}
66
67static struct clock_event_device au1x_rtcmatch2_clockdev = {
68 .name = "rtcmatch2",
69 .features = CLOCK_EVT_FEAT_ONESHOT,
70 .rating = 1500,
71 .set_next_event = au1x_rtcmatch2_set_next_event,
72 .cpumask = cpu_possible_mask,
73};
74
75static int __init alchemy_time_init(unsigned int m2int)
76{
77 struct clock_event_device *cd = &au1x_rtcmatch2_clockdev;
78 unsigned long t;
79
80 au1x_rtcmatch2_clockdev.irq = m2int;
81
82 /* Check if firmware (YAMON, ...) has enabled 32kHz and clock
83 * has been detected. If so install the rtcmatch2 clocksource,
84 * otherwise don't bother. Note that both bits being set is by
85 * no means a definite guarantee that the counters actually work
86 * (the 32S bit seems to be stuck set to 1 once a single clock-
87 * edge is detected, hence the timeouts).
88 */
89 if (CNTR_OK != (alchemy_rdsys(AU1000_SYS_CNTRCTRL) & CNTR_OK))
90 goto cntr_err;
91
92 /*
93 * setup counter 1 (RTC) to tick at full speed
94 */
95 t = 0xffffff;
96 while ((alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_T1S) && --t)
97 asm volatile ("nop");
98 if (!t)
99 goto cntr_err;
100
101 alchemy_wrsys(0, AU1000_SYS_RTCTRIM); /* 32.768 kHz */
102
103 t = 0xffffff;
104 while ((alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_C1S) && --t)
105 asm volatile ("nop");
106 if (!t)
107 goto cntr_err;
108 alchemy_wrsys(0, AU1000_SYS_RTCWRITE);
109
110 t = 0xffffff;
111 while ((alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_C1S) && --t)
112 asm volatile ("nop");
113 if (!t)
114 goto cntr_err;
115
116 /* register counter1 clocksource and event device */
117 clocksource_register_hz(&au1x_counter1_clocksource, 32768);
118
119 cd->shift = 32;
120 cd->mult = div_sc(32768, NSEC_PER_SEC, cd->shift);
121 cd->max_delta_ns = clockevent_delta2ns(0xffffffff, cd);
122 cd->max_delta_ticks = 0xffffffff;
123 cd->min_delta_ns = clockevent_delta2ns(9, cd);
124 cd->min_delta_ticks = 9; /* ~0.28ms */
125 clockevents_register_device(cd);
126 if (request_irq(m2int, au1x_rtcmatch2_irq, IRQF_TIMER, "timer",
127 &au1x_rtcmatch2_clockdev))
128 pr_err("Failed to register timer interrupt\n");
129
130 printk(KERN_INFO "Alchemy clocksource installed\n");
131
132 return 0;
133
134cntr_err:
135 return -1;
136}
137
138static int alchemy_m2inttab[] __initdata = {
139 AU1000_RTC_MATCH2_INT,
140 AU1500_RTC_MATCH2_INT,
141 AU1100_RTC_MATCH2_INT,
142 AU1550_RTC_MATCH2_INT,
143 AU1200_RTC_MATCH2_INT,
144 AU1300_RTC_MATCH2_INT,
145};
146
147void __init plat_time_init(void)
148{
149 int t;
150
151 t = alchemy_get_cputype();
152 if (t == ALCHEMY_CPU_UNKNOWN ||
153 alchemy_time_init(alchemy_m2inttab[t]))
154 cpu_wait = NULL; /* wait doesn't work with r4k timer */
155}