Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame^] | 2 | * Copyright (C) 2008 Manuel Lauss <mano@roarinelk.homelinux.net> |
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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <asm/time.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <asm/mach-au1x00/au1000.h> |
| 41 | |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame^] | 42 | /* 32kHz clock enabled and detected */ |
| 43 | #define CNTR_OK (SYS_CNTRL_E0 | SYS_CNTRL_32S) |
| 44 | |
Pete Popov | fe359bf | 2005-04-08 08:34:43 +0000 | [diff] [blame] | 45 | extern int allow_au1k_wait; /* default off for CP0 Counter */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | static DEFINE_SPINLOCK(time_lock); |
| 48 | |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 49 | /* |
| 50 | * I haven't found anyone that doesn't use a 12 MHz source clock, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | * but just in case..... |
| 52 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #define AU1000_SRC_CLK 12000000 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | /* |
| 56 | * We read the real processor speed from the PLL. This is important |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 57 | * because it is more accurate than computing it from the 32 KHz |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | * counter, if it exists. If we don't have an accurate processor |
| 59 | * speed, all of the peripherals that derive their clocks based on |
| 60 | * this advertised speed will introduce error and sometimes not work |
| 61 | * properly. This function is futher convoluted to still allow configurations |
| 62 | * to do that in case they have really, really old silicon with a |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame^] | 63 | * write-only PLL register. -- Dan |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | */ |
Sergei Shtylyov | eba8291 | 2008-03-27 22:05:57 +0300 | [diff] [blame] | 65 | unsigned long calc_clock(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | unsigned long cpu_speed; |
| 68 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | spin_lock_irqsave(&time_lock, flags); |
| 71 | |
Sergei Shtylyov | 758e285 | 2008-03-27 16:09:31 +0300 | [diff] [blame] | 72 | /* |
| 73 | * On early Au1000, sys_cpupll was write-only. Since these |
| 74 | * silicon versions of Au1000 are not sold by AMD, we don't bend |
| 75 | * over backwards trying to determine the frequency. |
| 76 | */ |
Manuel Lauss | 074cf65 | 2008-12-21 09:26:21 +0100 | [diff] [blame] | 77 | if (au1xxx_cpu_has_pll_wo()) |
Sergei Shtylyov | 758e285 | 2008-03-27 16:09:31 +0300 | [diff] [blame] | 78 | #ifdef CONFIG_SOC_AU1000_FREQUENCY |
| 79 | cpu_speed = CONFIG_SOC_AU1000_FREQUENCY; |
| 80 | #else |
| 81 | cpu_speed = 396000000; |
| 82 | #endif |
| 83 | else |
| 84 | cpu_speed = (au_readl(SYS_CPUPLL) & 0x0000003f) * AU1000_SRC_CLK; |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 85 | /* On Alchemy CPU:counter ratio is 1:1 */ |
Sergei Shtylyov | 53c1b19 | 2006-09-03 22:17:10 +0400 | [diff] [blame] | 86 | mips_hpt_frequency = cpu_speed; |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 87 | /* Equation: Baudrate = CPU / (SD * 2 * CLKDIV * 16) */ |
| 88 | set_au1x00_uart_baud_base(cpu_speed / (2 * ((int)(au_readl(SYS_POWERCTRL) |
| 89 | & 0x03) + 2) * 16)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | spin_unlock_irqrestore(&time_lock, flags); |
Sergei Shtylyov | eba8291 | 2008-03-27 22:05:57 +0300 | [diff] [blame] | 91 | return cpu_speed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame^] | 94 | static cycle_t au1x_counter1_read(void) |
| 95 | { |
| 96 | return au_readl(SYS_RTCREAD); |
| 97 | } |
| 98 | |
| 99 | static struct clocksource au1x_counter1_clocksource = { |
| 100 | .name = "alchemy-counter1", |
| 101 | .read = au1x_counter1_read, |
| 102 | .mask = CLOCKSOURCE_MASK(32), |
| 103 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 104 | .rating = 100, |
| 105 | }; |
| 106 | |
| 107 | static int au1x_rtcmatch2_set_next_event(unsigned long delta, |
| 108 | struct clock_event_device *cd) |
| 109 | { |
| 110 | delta += au_readl(SYS_RTCREAD); |
| 111 | /* wait for register access */ |
| 112 | while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M21) |
| 113 | ; |
| 114 | au_writel(delta, SYS_RTCMATCH2); |
| 115 | au_sync(); |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static void au1x_rtcmatch2_set_mode(enum clock_event_mode mode, |
| 121 | struct clock_event_device *cd) |
| 122 | { |
| 123 | } |
| 124 | |
| 125 | static irqreturn_t au1x_rtcmatch2_irq(int irq, void *dev_id) |
| 126 | { |
| 127 | struct clock_event_device *cd = dev_id; |
| 128 | cd->event_handler(cd); |
| 129 | return IRQ_HANDLED; |
| 130 | } |
| 131 | |
| 132 | static struct clock_event_device au1x_rtcmatch2_clockdev = { |
| 133 | .name = "rtcmatch2", |
| 134 | .features = CLOCK_EVT_FEAT_ONESHOT, |
| 135 | .rating = 100, |
| 136 | .irq = AU1000_RTC_MATCH2_INT, |
| 137 | .set_next_event = au1x_rtcmatch2_set_next_event, |
| 138 | .set_mode = au1x_rtcmatch2_set_mode, |
| 139 | .cpumask = CPU_MASK_ALL, |
| 140 | }; |
| 141 | |
| 142 | static struct irqaction au1x_rtcmatch2_irqaction = { |
| 143 | .handler = au1x_rtcmatch2_irq, |
| 144 | .flags = IRQF_DISABLED | IRQF_TIMER, |
| 145 | .name = "timer", |
| 146 | .dev_id = &au1x_rtcmatch2_clockdev, |
| 147 | }; |
| 148 | |
Ralf Baechle | bc2f2a2 | 2007-10-26 12:58:02 +0100 | [diff] [blame] | 149 | void __init plat_time_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | { |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame^] | 151 | struct clock_event_device *cd = &au1x_rtcmatch2_clockdev; |
| 152 | unsigned long t; |
Sergei Shtylyov | eba8291 | 2008-03-27 22:05:57 +0300 | [diff] [blame] | 153 | unsigned int est_freq = calc_clock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | est_freq += 5000; /* round */ |
| 156 | est_freq -= est_freq%10000; |
Manuel Lauss | 074cf65 | 2008-12-21 09:26:21 +0100 | [diff] [blame] | 157 | printk(KERN_INFO "(PRId %08x) @ %u.%02u MHz\n", read_c0_prid(), |
Sergei Shtylyov | c1dcb14 | 2008-04-30 23:18:41 +0400 | [diff] [blame] | 158 | est_freq / 1000000, ((est_freq % 1000000) * 100) / 1000000); |
| 159 | set_au1x00_speed(est_freq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame^] | 161 | /* Check if firmware (YAMON, ...) has enabled 32kHz and clock |
| 162 | * has been detected. If so install the rtcmatch2 clocksource, |
| 163 | * otherwise don't bother. Note that both bits being set is by |
| 164 | * no means a definite guarantee that the counters actually work |
| 165 | * (the 32S bit seems to be stuck set to 1 once a single clock- |
| 166 | * edge is detected, hence the timeouts). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | */ |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame^] | 168 | if (CNTR_OK != (au_readl(SYS_COUNTER_CNTRL) & CNTR_OK)) |
| 169 | goto cntr_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame^] | 171 | /* |
| 172 | * setup counter 1 (RTC) to tick at full speed |
| 173 | */ |
| 174 | t = 0xffffff; |
| 175 | while ((au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T1S) && t--) |
| 176 | asm volatile ("nop"); |
| 177 | if (!t) |
| 178 | goto cntr_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame^] | 180 | au_writel(0, SYS_RTCTRIM); /* 32.768 kHz */ |
| 181 | au_sync(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame^] | 183 | t = 0xffffff; |
| 184 | while ((au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S) && t--) |
| 185 | asm volatile ("nop"); |
| 186 | if (!t) |
| 187 | goto cntr_err; |
| 188 | au_writel(0, SYS_RTCWRITE); |
| 189 | au_sync(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
Manuel Lauss | 0c694de | 2008-12-21 09:26:23 +0100 | [diff] [blame^] | 191 | t = 0xffffff; |
| 192 | while ((au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S) && t--) |
| 193 | asm volatile ("nop"); |
| 194 | if (!t) |
| 195 | goto cntr_err; |
| 196 | |
| 197 | /* register counter1 clocksource and event device */ |
| 198 | clocksource_set_clock(&au1x_counter1_clocksource, 32768); |
| 199 | clocksource_register(&au1x_counter1_clocksource); |
| 200 | |
| 201 | cd->shift = 32; |
| 202 | cd->mult = div_sc(32768, NSEC_PER_SEC, cd->shift); |
| 203 | cd->max_delta_ns = clockevent_delta2ns(0xffffffff, cd); |
| 204 | cd->min_delta_ns = clockevent_delta2ns(8, cd); /* ~0.25ms */ |
| 205 | clockevents_register_device(cd); |
| 206 | setup_irq(AU1000_RTC_MATCH2_INT, &au1x_rtcmatch2_irqaction); |
| 207 | |
| 208 | printk(KERN_INFO "Alchemy clocksource installed\n"); |
| 209 | |
| 210 | /* can now use 'wait' */ |
| 211 | allow_au1k_wait = 1; |
| 212 | return; |
| 213 | |
| 214 | cntr_err: |
| 215 | /* counters unusable, use C0 counter */ |
| 216 | r4k_clockevent_init(); |
| 217 | init_r4k_clocksource(); |
| 218 | allow_au1k_wait = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | } |