| 
									
										
										
										
											2010-01-28 16:41:42 -08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (C) 2010 Google, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Author: | 
					
						
							|  |  |  |  *	Colin Cross <ccross@google.com> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This software is licensed under the terms of the GNU General Public | 
					
						
							|  |  |  |  * License version 2, as published by the Free Software Foundation, and | 
					
						
							|  |  |  |  * may be copied, distributed, and modified under those terms. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <linux/init.h>
 | 
					
						
							| 
									
										
										
										
											2011-02-21 17:04:37 -08:00
										 |  |  | #include <linux/err.h>
 | 
					
						
							| 
									
										
										
										
											2010-01-28 16:41:42 -08:00
										 |  |  | #include <linux/time.h>
 | 
					
						
							|  |  |  | #include <linux/interrupt.h>
 | 
					
						
							|  |  |  | #include <linux/irq.h>
 | 
					
						
							|  |  |  | #include <linux/clockchips.h>
 | 
					
						
							|  |  |  | #include <linux/clocksource.h>
 | 
					
						
							|  |  |  | #include <linux/clk.h>
 | 
					
						
							|  |  |  | #include <linux/io.h>
 | 
					
						
							| 
									
										
										
										
											2012-10-23 11:40:25 -06:00
										 |  |  | #include <linux/of_address.h>
 | 
					
						
							| 
									
										
										
										
											2012-09-19 13:13:33 -06:00
										 |  |  | #include <linux/of_irq.h>
 | 
					
						
							| 
									
										
										
										
											2013-06-01 23:39:40 -07:00
										 |  |  | #include <linux/sched_clock.h>
 | 
					
						
							| 
									
										
										
										
											2014-06-12 18:58:29 +03:00
										 |  |  | #include <linux/delay.h>
 | 
					
						
							| 
									
										
										
										
											2010-01-28 16:41:42 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <asm/mach/time.h>
 | 
					
						
							| 
									
										
										
										
											2012-01-10 19:44:19 +00:00
										 |  |  | #include <asm/smp_twd.h>
 | 
					
						
							| 
									
										
										
										
											2010-01-28 16:41:42 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-28 16:26:19 -08:00
										 |  |  | #define RTC_SECONDS            0x08
 | 
					
						
							|  |  |  | #define RTC_SHADOW_SECONDS     0x0c
 | 
					
						
							|  |  |  | #define RTC_MILLISECONDS       0x10
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-28 16:41:42 -08:00
										 |  |  | #define TIMERUS_CNTR_1US 0x10
 | 
					
						
							|  |  |  | #define TIMERUS_USEC_CFG 0x14
 | 
					
						
							|  |  |  | #define TIMERUS_CNTR_FREEZE 0x4c
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define TIMER1_BASE 0x0
 | 
					
						
							|  |  |  | #define TIMER2_BASE 0x8
 | 
					
						
							|  |  |  | #define TIMER3_BASE 0x50
 | 
					
						
							|  |  |  | #define TIMER4_BASE 0x58
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define TIMER_PTV 0x0
 | 
					
						
							|  |  |  | #define TIMER_PCR 0x4
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-23 11:40:25 -06:00
										 |  |  | static void __iomem *timer_reg_base; | 
					
						
							|  |  |  | static void __iomem *rtc_base; | 
					
						
							| 
									
										
										
										
											2010-11-28 16:26:19 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | static struct timespec persistent_ts; | 
					
						
							|  |  |  | static u64 persistent_ms, last_persistent_ms; | 
					
						
							| 
									
										
										
										
											2010-01-28 16:41:42 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-12 18:58:29 +03:00
										 |  |  | static struct delay_timer tegra_delay_timer; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-28 16:41:42 -08:00
										 |  |  | #define timer_writel(value, reg) \
 | 
					
						
							| 
									
										
										
										
											2011-09-08 17:49:13 -07:00
										 |  |  | 	__raw_writel(value, timer_reg_base + (reg)) | 
					
						
							| 
									
										
										
										
											2010-01-28 16:41:42 -08:00
										 |  |  | #define timer_readl(reg) \
 | 
					
						
							| 
									
										
										
										
											2011-09-08 17:49:13 -07:00
										 |  |  | 	__raw_readl(timer_reg_base + (reg)) | 
					
						
							| 
									
										
										
										
											2010-01-28 16:41:42 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | static int tegra_timer_set_next_event(unsigned long cycles, | 
					
						
							|  |  |  | 					 struct clock_event_device *evt) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	u32 reg; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	reg = 0x80000000 | ((cycles > 1) ? (cycles-1) : 0); | 
					
						
							|  |  |  | 	timer_writel(reg, TIMER3_BASE + TIMER_PTV); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void tegra_timer_set_mode(enum clock_event_mode mode, | 
					
						
							|  |  |  | 				    struct clock_event_device *evt) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	u32 reg; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	timer_writel(0, TIMER3_BASE + TIMER_PTV); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch (mode) { | 
					
						
							|  |  |  | 	case CLOCK_EVT_MODE_PERIODIC: | 
					
						
							|  |  |  | 		reg = 0xC0000000 | ((1000000/HZ)-1); | 
					
						
							|  |  |  | 		timer_writel(reg, TIMER3_BASE + TIMER_PTV); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case CLOCK_EVT_MODE_ONESHOT: | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case CLOCK_EVT_MODE_UNUSED: | 
					
						
							|  |  |  | 	case CLOCK_EVT_MODE_SHUTDOWN: | 
					
						
							|  |  |  | 	case CLOCK_EVT_MODE_RESUME: | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct clock_event_device tegra_clockevent = { | 
					
						
							|  |  |  | 	.name		= "timer0", | 
					
						
							|  |  |  | 	.rating		= 300, | 
					
						
							|  |  |  | 	.features	= CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC, | 
					
						
							|  |  |  | 	.set_next_event	= tegra_timer_set_next_event, | 
					
						
							|  |  |  | 	.set_mode	= tegra_timer_set_mode, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-18 16:21:26 -07:00
										 |  |  | static u64 notrace tegra_read_sched_clock(void) | 
					
						
							| 
									
										
										
										
											2010-12-15 21:49:42 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-15 12:19:23 +01:00
										 |  |  | 	return timer_readl(TIMERUS_CNTR_1US); | 
					
						
							| 
									
										
										
										
											2010-01-28 16:41:42 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-28 16:26:19 -08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * tegra_rtc_read - Reads the Tegra RTC registers | 
					
						
							|  |  |  |  * Care must be taken that this funciton is not called while the | 
					
						
							|  |  |  |  * tegra_rtc driver could be executing to avoid race conditions | 
					
						
							|  |  |  |  * on the RTC shadow register | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2011-09-08 17:50:03 -07:00
										 |  |  | static u64 tegra_rtc_read_ms(void) | 
					
						
							| 
									
										
										
										
											2010-11-28 16:26:19 -08:00
										 |  |  | { | 
					
						
							|  |  |  | 	u32 ms = readl(rtc_base + RTC_MILLISECONDS); | 
					
						
							|  |  |  | 	u32 s = readl(rtc_base + RTC_SHADOW_SECONDS); | 
					
						
							|  |  |  | 	return (u64)s * MSEC_PER_SEC + ms; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2012-05-05 19:28:44 +01:00
										 |  |  |  * tegra_read_persistent_clock -  Return time from a persistent clock. | 
					
						
							| 
									
										
										
										
											2010-11-28 16:26:19 -08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Reads the time from a source which isn't disabled during PM, the | 
					
						
							|  |  |  |  * 32k sync timer.  Convert the cycles elapsed since last read into | 
					
						
							|  |  |  |  * nsecs and adds to a monotonically increasing timespec. | 
					
						
							|  |  |  |  * Care must be taken that this funciton is not called while the | 
					
						
							|  |  |  |  * tegra_rtc driver could be executing to avoid race conditions | 
					
						
							|  |  |  |  * on the RTC shadow register | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2012-05-05 19:28:44 +01:00
										 |  |  | static void tegra_read_persistent_clock(struct timespec *ts) | 
					
						
							| 
									
										
										
										
											2010-11-28 16:26:19 -08:00
										 |  |  | { | 
					
						
							|  |  |  | 	u64 delta; | 
					
						
							|  |  |  | 	struct timespec *tsp = &persistent_ts; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	last_persistent_ms = persistent_ms; | 
					
						
							|  |  |  | 	persistent_ms = tegra_rtc_read_ms(); | 
					
						
							|  |  |  | 	delta = persistent_ms - last_persistent_ms; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	timespec_add_ns(tsp, delta * NSEC_PER_MSEC); | 
					
						
							|  |  |  | 	*ts = *tsp; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-12 18:58:29 +03:00
										 |  |  | static unsigned long tegra_delay_timer_read_counter_long(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return readl(timer_reg_base + TIMERUS_CNTR_1US); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-28 16:41:42 -08:00
										 |  |  | static irqreturn_t tegra_timer_interrupt(int irq, void *dev_id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct clock_event_device *evt = (struct clock_event_device *)dev_id; | 
					
						
							|  |  |  | 	timer_writel(1<<30, TIMER3_BASE + TIMER_PCR); | 
					
						
							|  |  |  | 	evt->event_handler(evt); | 
					
						
							|  |  |  | 	return IRQ_HANDLED; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct irqaction tegra_timer_irq = { | 
					
						
							|  |  |  | 	.name		= "timer0", | 
					
						
							| 
									
										
										
										
											2013-12-09 10:35:45 +01:00
										 |  |  | 	.flags		= IRQF_TIMER | IRQF_TRIGGER_HIGH, | 
					
						
							| 
									
										
										
										
											2010-01-28 16:41:42 -08:00
										 |  |  | 	.handler	= tegra_timer_interrupt, | 
					
						
							|  |  |  | 	.dev_id		= &tegra_clockevent, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-06 14:40:22 -06:00
										 |  |  | static void __init tegra20_init_timer(struct device_node *np) | 
					
						
							| 
									
										
										
										
											2010-01-28 16:41:42 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-02-21 17:04:37 -08:00
										 |  |  | 	struct clk *clk; | 
					
						
							| 
									
										
										
										
											2011-12-14 17:03:16 +02:00
										 |  |  | 	unsigned long rate; | 
					
						
							| 
									
										
										
										
											2010-01-28 16:41:42 -08:00
										 |  |  | 	int ret; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-23 11:40:25 -06:00
										 |  |  | 	timer_reg_base = of_iomap(np, 0); | 
					
						
							|  |  |  | 	if (!timer_reg_base) { | 
					
						
							| 
									
										
										
										
											2012-12-17 13:35:23 +02:00
										 |  |  | 		pr_err("Can't map timer registers\n"); | 
					
						
							| 
									
										
										
										
											2012-10-23 11:40:25 -06:00
										 |  |  | 		BUG(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-09-19 13:13:33 -06:00
										 |  |  | 	tegra_timer_irq.irq = irq_of_parse_and_map(np, 2); | 
					
						
							|  |  |  | 	if (tegra_timer_irq.irq <= 0) { | 
					
						
							|  |  |  | 		pr_err("Failed to map timer IRQ\n"); | 
					
						
							|  |  |  | 		BUG(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-04 15:40:30 +02:00
										 |  |  | 	clk = of_clk_get(np, 0); | 
					
						
							| 
									
										
										
										
											2011-12-14 17:03:16 +02:00
										 |  |  | 	if (IS_ERR(clk)) { | 
					
						
							| 
									
										
										
										
											2012-10-23 12:21:39 -06:00
										 |  |  | 		pr_warn("Unable to get timer clock. Assuming 12Mhz input clock.\n"); | 
					
						
							| 
									
										
										
										
											2011-12-14 17:03:16 +02:00
										 |  |  | 		rate = 12000000; | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2012-06-05 09:59:35 +05:30
										 |  |  | 		clk_prepare_enable(clk); | 
					
						
							| 
									
										
										
										
											2011-12-14 17:03:16 +02:00
										 |  |  | 		rate = clk_get_rate(clk); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-02-21 17:04:37 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-28 16:41:42 -08:00
										 |  |  | 	switch (rate) { | 
					
						
							|  |  |  | 	case 12000000: | 
					
						
							|  |  |  | 		timer_writel(0x000b, TIMERUS_USEC_CFG); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case 13000000: | 
					
						
							|  |  |  | 		timer_writel(0x000c, TIMERUS_USEC_CFG); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case 19200000: | 
					
						
							|  |  |  | 		timer_writel(0x045f, TIMERUS_USEC_CFG); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	case 26000000: | 
					
						
							|  |  |  | 		timer_writel(0x0019, TIMERUS_USEC_CFG); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		WARN(1, "Unknown clock rate"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-18 16:21:26 -07:00
										 |  |  | 	sched_clock_register(tegra_read_sched_clock, 32, 1000000); | 
					
						
							| 
									
										
										
										
											2010-12-15 21:49:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-08 14:09:47 +01:00
										 |  |  | 	if (clocksource_mmio_init(timer_reg_base + TIMERUS_CNTR_1US, | 
					
						
							|  |  |  | 		"timer_us", 1000000, 300, 32, clocksource_mmio_readl_up)) { | 
					
						
							| 
									
										
										
										
											2012-10-23 12:21:39 -06:00
										 |  |  | 		pr_err("Failed to register clocksource\n"); | 
					
						
							| 
									
										
										
										
											2010-01-28 16:41:42 -08:00
										 |  |  | 		BUG(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-12 18:58:29 +03:00
										 |  |  | 	tegra_delay_timer.read_current_timer = | 
					
						
							|  |  |  | 			tegra_delay_timer_read_counter_long; | 
					
						
							|  |  |  | 	tegra_delay_timer.freq = 1000000; | 
					
						
							|  |  |  | 	register_current_timer_delay(&tegra_delay_timer); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-28 16:41:42 -08:00
										 |  |  | 	ret = setup_irq(tegra_timer_irq.irq, &tegra_timer_irq); | 
					
						
							|  |  |  | 	if (ret) { | 
					
						
							| 
									
										
										
										
											2012-10-23 12:21:39 -06:00
										 |  |  | 		pr_err("Failed to register timer IRQ: %d\n", ret); | 
					
						
							| 
									
										
										
										
											2010-01-28 16:41:42 -08:00
										 |  |  | 		BUG(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tegra_clockevent.cpumask = cpu_all_mask; | 
					
						
							|  |  |  | 	tegra_clockevent.irq = tegra_timer_irq.irq; | 
					
						
							| 
									
										
										
										
											2013-01-12 11:50:05 +00:00
										 |  |  | 	clockevents_config_and_register(&tegra_clockevent, 1000000, | 
					
						
							|  |  |  | 					0x1, 0x1fffffff); | 
					
						
							| 
									
										
										
										
											2013-02-07 11:36:23 -06:00
										 |  |  | } | 
					
						
							|  |  |  | CLOCKSOURCE_OF_DECLARE(tegra20_timer, "nvidia,tegra20-timer", tegra20_init_timer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void __init tegra20_init_rtc(struct device_node *np) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct clk *clk; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rtc_base = of_iomap(np, 0); | 
					
						
							|  |  |  | 	if (!rtc_base) { | 
					
						
							|  |  |  | 		pr_err("Can't map RTC registers"); | 
					
						
							|  |  |  | 		BUG(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/*
 | 
					
						
							|  |  |  | 	 * rtc registers are used by read_persistent_clock, keep the rtc clock | 
					
						
							|  |  |  | 	 * enabled | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2013-04-09 15:27:52 +02:00
										 |  |  | 	clk = of_clk_get(np, 0); | 
					
						
							| 
									
										
										
										
											2013-02-07 11:36:23 -06:00
										 |  |  | 	if (IS_ERR(clk)) | 
					
						
							|  |  |  | 		pr_warn("Unable to get rtc-tegra clock\n"); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		clk_prepare_enable(clk); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-05 19:28:44 +01:00
										 |  |  | 	register_persistent_clock(NULL, tegra_read_persistent_clock); | 
					
						
							| 
									
										
										
										
											2010-01-28 16:41:42 -08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-02-07 11:36:23 -06:00
										 |  |  | CLOCKSOURCE_OF_DECLARE(tegra20_rtc, "nvidia,tegra20-rtc", tegra20_init_rtc); | 
					
						
							| 
									
										
										
										
											2010-01-28 16:41:42 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-28 16:26:19 -08:00
										 |  |  | #ifdef CONFIG_PM
 | 
					
						
							|  |  |  | static u32 usec_config; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void tegra_timer_suspend(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	usec_config = timer_readl(TIMERUS_USEC_CFG); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void tegra_timer_resume(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	timer_writel(usec_config, TIMERUS_USEC_CFG); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 |