Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Manuel Lauss | 7881446 | 2009-10-07 20:15:15 +0200 | [diff] [blame] | 2 | * Copyright (C) 2008-2009 Manuel Lauss <manuel.lauss@gmail.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame] | 4 | * Previous incarnations were: |
Sergei Shtylyov | 0167509 | 2008-03-24 23:15:50 +0300 | [diff] [blame] | 5 | * Copyright (C) 2001, 2006, 2008 MontaVista Software, <source@mvista.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame] | 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). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | */ |
| 33 | |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame] | 34 | #include <linux/clockchips.h> |
| 35 | #include <linux/clocksource.h> |
| 36 | #include <linux/interrupt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/spinlock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Ralf Baechle | bdc92d74 | 2013-05-21 16:59:19 +0200 | [diff] [blame] | 39 | #include <asm/idle.h> |
Manuel Lauss | 2882b0c | 2009-08-22 18:09:27 +0200 | [diff] [blame] | 40 | #include <asm/processor.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <asm/time.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <asm/mach-au1x00/au1000.h> |
| 43 | |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame] | 44 | /* 32kHz clock enabled and detected */ |
| 45 | #define CNTR_OK (SYS_CNTRL_E0 | SYS_CNTRL_32S) |
| 46 | |
Manuel Lauss | ad058e9 | 2009-04-22 08:01:48 +0200 | [diff] [blame] | 47 | static cycle_t au1x_counter1_read(struct clocksource *cs) |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame] | 48 | { |
Manuel Lauss | 1d09de7 | 2014-07-23 16:36:24 +0200 | [diff] [blame] | 49 | return alchemy_rdsys(AU1000_SYS_RTCREAD); |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static 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, |
Manuel Lauss | 8e0d737 | 2012-12-17 06:14:08 +0000 | [diff] [blame] | 57 | .rating = 1500, |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | static int au1x_rtcmatch2_set_next_event(unsigned long delta, |
| 61 | struct clock_event_device *cd) |
| 62 | { |
Manuel Lauss | 1d09de7 | 2014-07-23 16:36:24 +0200 | [diff] [blame] | 63 | delta += alchemy_rdsys(AU1000_SYS_RTCREAD); |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame] | 64 | /* wait for register access */ |
Manuel Lauss | 1d09de7 | 2014-07-23 16:36:24 +0200 | [diff] [blame] | 65 | while (alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_M21) |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame] | 66 | ; |
Manuel Lauss | 1d09de7 | 2014-07-23 16:36:24 +0200 | [diff] [blame] | 67 | alchemy_wrsys(delta, AU1000_SYS_RTCMATCH2); |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame] | 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame] | 72 | static 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 | |
| 79 | static struct clock_event_device au1x_rtcmatch2_clockdev = { |
| 80 | .name = "rtcmatch2", |
| 81 | .features = CLOCK_EVT_FEAT_ONESHOT, |
Manuel Lauss | 8e0d737 | 2012-12-17 06:14:08 +0000 | [diff] [blame] | 82 | .rating = 1500, |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 83 | .set_next_event = au1x_rtcmatch2_set_next_event, |
Rusty Russell | 51c870a | 2009-09-24 09:34:35 -0600 | [diff] [blame] | 84 | .cpumask = cpu_all_mask, |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | static struct irqaction au1x_rtcmatch2_irqaction = { |
| 88 | .handler = au1x_rtcmatch2_irq, |
Yong Zhang | 8b5690f | 2011-11-22 14:38:03 +0000 | [diff] [blame] | 89 | .flags = IRQF_TIMER, |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame] | 90 | .name = "timer", |
| 91 | .dev_id = &au1x_rtcmatch2_clockdev, |
| 92 | }; |
| 93 | |
Manuel Lauss | 7881446 | 2009-10-07 20:15:15 +0200 | [diff] [blame] | 94 | static int __init alchemy_time_init(unsigned int m2int) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | { |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame] | 96 | struct clock_event_device *cd = &au1x_rtcmatch2_clockdev; |
| 97 | unsigned long t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Manuel Lauss | 7881446 | 2009-10-07 20:15:15 +0200 | [diff] [blame] | 99 | au1x_rtcmatch2_clockdev.irq = m2int; |
| 100 | |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame] | 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). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | */ |
Manuel Lauss | 1d09de7 | 2014-07-23 16:36:24 +0200 | [diff] [blame] | 108 | if (CNTR_OK != (alchemy_rdsys(AU1000_SYS_CNTRCTRL) & CNTR_OK)) |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame] | 109 | goto cntr_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame] | 111 | /* |
| 112 | * setup counter 1 (RTC) to tick at full speed |
| 113 | */ |
| 114 | t = 0xffffff; |
Manuel Lauss | 1d09de7 | 2014-07-23 16:36:24 +0200 | [diff] [blame] | 115 | while ((alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_T1S) && --t) |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame] | 116 | asm volatile ("nop"); |
| 117 | if (!t) |
| 118 | goto cntr_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Manuel Lauss | 1d09de7 | 2014-07-23 16:36:24 +0200 | [diff] [blame] | 120 | alchemy_wrsys(0, AU1000_SYS_RTCTRIM); /* 32.768 kHz */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame] | 122 | t = 0xffffff; |
Manuel Lauss | 1d09de7 | 2014-07-23 16:36:24 +0200 | [diff] [blame] | 123 | while ((alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_C1S) && --t) |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame] | 124 | asm volatile ("nop"); |
| 125 | if (!t) |
| 126 | goto cntr_err; |
Manuel Lauss | 1d09de7 | 2014-07-23 16:36:24 +0200 | [diff] [blame] | 127 | alchemy_wrsys(0, AU1000_SYS_RTCWRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame] | 129 | t = 0xffffff; |
Manuel Lauss | 1d09de7 | 2014-07-23 16:36:24 +0200 | [diff] [blame] | 130 | while ((alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_C1S) && --t) |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame] | 131 | asm volatile ("nop"); |
| 132 | if (!t) |
| 133 | goto cntr_err; |
| 134 | |
| 135 | /* register counter1 clocksource and event device */ |
John Stultz | 75c4fd8 | 2010-04-26 20:23:11 -0700 | [diff] [blame] | 136 | clocksource_register_hz(&au1x_counter1_clocksource, 32768); |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame] | 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); |
Manuel Lauss | 8e36579 | 2011-12-20 17:37:29 +0100 | [diff] [blame] | 141 | cd->min_delta_ns = clockevent_delta2ns(9, cd); /* ~0.28ms */ |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame] | 142 | clockevents_register_device(cd); |
Manuel Lauss | 7881446 | 2009-10-07 20:15:15 +0200 | [diff] [blame] | 143 | setup_irq(m2int, &au1x_rtcmatch2_irqaction); |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame] | 144 | |
| 145 | printk(KERN_INFO "Alchemy clocksource installed\n"); |
| 146 | |
Manuel Lauss | 7881446 | 2009-10-07 20:15:15 +0200 | [diff] [blame] | 147 | return 0; |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame] | 148 | |
| 149 | cntr_err: |
Manuel Lauss | 7881446 | 2009-10-07 20:15:15 +0200 | [diff] [blame] | 150 | return -1; |
| 151 | } |
| 152 | |
Manuel Lauss | 7881446 | 2009-10-07 20:15:15 +0200 | [diff] [blame] | 153 | static 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, |
Manuel Lauss | 809f36c | 2011-11-01 20:03:30 +0100 | [diff] [blame] | 159 | AU1300_RTC_MATCH2_INT, |
Manuel Lauss | 7881446 | 2009-10-07 20:15:15 +0200 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | void __init plat_time_init(void) |
| 163 | { |
| 164 | int t; |
| 165 | |
| 166 | t = alchemy_get_cputype(); |
Manuel Lauss | 8e0d737 | 2012-12-17 06:14:08 +0000 | [diff] [blame] | 167 | if (t == ALCHEMY_CPU_UNKNOWN || |
| 168 | alchemy_time_init(alchemy_m2inttab[t])) |
| 169 | cpu_wait = NULL; /* wait doesn't work with r4k timer */ |
Manuel Lauss | 7881446 | 2009-10-07 20:15:15 +0200 | [diff] [blame] | 170 | } |