| 
									
										
										
										
											2006-06-19 15:27:53 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * arch/arm/mach-netx/time.c | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (c) 2005 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU General Public License version 2 | 
					
						
							|  |  |  |  * as published by the Free Software Foundation. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software | 
					
						
							|  |  |  |  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <linux/init.h>
 | 
					
						
							|  |  |  | #include <linux/interrupt.h>
 | 
					
						
							| 
									
										
										
										
											2006-12-12 09:23:45 +01:00
										 |  |  | #include <linux/irq.h>
 | 
					
						
							|  |  |  | #include <linux/clocksource.h>
 | 
					
						
							| 
									
										
										
										
											2008-12-09 21:57:24 +01:00
										 |  |  | #include <linux/clockchips.h>
 | 
					
						
							| 
									
										
										
										
											2008-09-06 12:10:45 +01:00
										 |  |  | #include <linux/io.h>
 | 
					
						
							| 
									
										
										
										
											2006-06-19 15:27:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-05 16:14:15 +01:00
										 |  |  | #include <mach/hardware.h>
 | 
					
						
							| 
									
										
										
										
											2006-06-19 15:27:53 +01:00
										 |  |  | #include <asm/mach/time.h>
 | 
					
						
							| 
									
										
										
										
											2008-08-05 16:14:15 +01:00
										 |  |  | #include <mach/netx-regs.h>
 | 
					
						
							| 
									
										
										
										
											2006-06-19 15:27:53 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-12 20:56:02 +01:00
										 |  |  | #define NETX_CLOCK_FREQ 100000000
 | 
					
						
							|  |  |  | #define NETX_LATCH DIV_ROUND_CLOSEST(NETX_CLOCK_FREQ, HZ)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-09 21:57:24 +01:00
										 |  |  | #define TIMER_CLOCKEVENT 0
 | 
					
						
							| 
									
										
										
										
											2008-12-09 21:57:22 +01:00
										 |  |  | #define TIMER_CLOCKSOURCE 1
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-09 21:57:24 +01:00
										 |  |  | static void netx_set_mode(enum clock_event_mode mode, | 
					
						
							|  |  |  | 		struct clock_event_device *clk) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	u32 tmode; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* disable timer */ | 
					
						
							|  |  |  | 	writel(0, NETX_GPIO_COUNTER_CTRL(TIMER_CLOCKEVENT)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch (mode) { | 
					
						
							|  |  |  | 	case CLOCK_EVT_MODE_PERIODIC: | 
					
						
							| 
									
										
										
										
											2013-11-12 20:56:02 +01:00
										 |  |  | 		writel(NETX_LATCH, NETX_GPIO_COUNTER_MAX(TIMER_CLOCKEVENT)); | 
					
						
							| 
									
										
										
										
											2008-12-09 21:57:24 +01:00
										 |  |  | 		tmode = NETX_GPIO_COUNTER_CTRL_RST_EN | | 
					
						
							|  |  |  | 			NETX_GPIO_COUNTER_CTRL_IRQ_EN | | 
					
						
							|  |  |  | 			NETX_GPIO_COUNTER_CTRL_RUN; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	case CLOCK_EVT_MODE_ONESHOT: | 
					
						
							|  |  |  | 		writel(0, NETX_GPIO_COUNTER_MAX(TIMER_CLOCKEVENT)); | 
					
						
							|  |  |  | 		tmode = NETX_GPIO_COUNTER_CTRL_IRQ_EN | | 
					
						
							|  |  |  | 			NETX_GPIO_COUNTER_CTRL_RUN; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		WARN(1, "%s: unhandled mode %d\n", __func__, mode); | 
					
						
							|  |  |  | 		/* fall through */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	case CLOCK_EVT_MODE_SHUTDOWN: | 
					
						
							|  |  |  | 	case CLOCK_EVT_MODE_UNUSED: | 
					
						
							|  |  |  | 	case CLOCK_EVT_MODE_RESUME: | 
					
						
							|  |  |  | 		tmode = 0; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	writel(tmode, NETX_GPIO_COUNTER_CTRL(TIMER_CLOCKEVENT)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int netx_set_next_event(unsigned long evt, | 
					
						
							|  |  |  | 		struct clock_event_device *clk) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	writel(0 - evt, NETX_GPIO_COUNTER_CURRENT(TIMER_CLOCKEVENT)); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct clock_event_device netx_clockevent = { | 
					
						
							|  |  |  | 	.name = "netx-timer" __stringify(TIMER_CLOCKEVENT), | 
					
						
							|  |  |  | 	.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT, | 
					
						
							|  |  |  | 	.set_next_event = netx_set_next_event, | 
					
						
							|  |  |  | 	.set_mode = netx_set_mode, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-19 15:27:53 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * IRQ handler for the timer | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static irqreturn_t | 
					
						
							| 
									
										
										
										
											2006-10-06 10:53:39 -07:00
										 |  |  | netx_timer_interrupt(int irq, void *dev_id) | 
					
						
							| 
									
										
										
										
											2006-06-19 15:27:53 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-12-09 21:57:24 +01:00
										 |  |  | 	struct clock_event_device *evt = &netx_clockevent; | 
					
						
							| 
									
										
										
										
											2006-12-12 09:23:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-19 15:27:53 +01:00
										 |  |  | 	/* acknowledge interrupt */ | 
					
						
							|  |  |  | 	writel(COUNTER_BIT(0), NETX_GPIO_IRQ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-09 21:57:24 +01:00
										 |  |  | 	evt->event_handler(evt); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-19 15:27:53 +01:00
										 |  |  | 	return IRQ_HANDLED; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct irqaction netx_timer_irq = { | 
					
						
							| 
									
										
										
										
											2008-12-09 21:57:21 +01:00
										 |  |  | 	.name		= "NetX Timer Tick", | 
					
						
							| 
									
										
										
										
											2014-03-04 22:04:50 +01:00
										 |  |  | 	.flags		= IRQF_TIMER | IRQF_IRQPOLL, | 
					
						
							| 
									
										
										
										
											2008-12-09 21:57:21 +01:00
										 |  |  | 	.handler	= netx_timer_interrupt, | 
					
						
							| 
									
										
										
										
											2006-06-19 15:27:53 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Set up timer interrupt | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2012-11-08 12:40:59 -07:00
										 |  |  | void __init netx_timer_init(void) | 
					
						
							| 
									
										
										
										
											2006-06-19 15:27:53 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	/* disable timer initially */ | 
					
						
							|  |  |  | 	writel(0, NETX_GPIO_COUNTER_CTRL(0)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Reset the timer value to zero */ | 
					
						
							|  |  |  | 	writel(0, NETX_GPIO_COUNTER_CURRENT(0)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-12 20:56:02 +01:00
										 |  |  | 	writel(NETX_LATCH, NETX_GPIO_COUNTER_MAX(0)); | 
					
						
							| 
									
										
										
										
											2006-06-19 15:27:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* acknowledge interrupt */ | 
					
						
							|  |  |  | 	writel(COUNTER_BIT(0), NETX_GPIO_IRQ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-09 21:57:21 +01:00
										 |  |  | 	/* Enable the interrupt in the specific timer
 | 
					
						
							|  |  |  | 	 * register and start timer | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2006-06-19 15:27:53 +01:00
										 |  |  | 	writel(COUNTER_BIT(0), NETX_GPIO_IRQ_ENABLE); | 
					
						
							|  |  |  | 	writel(NETX_GPIO_COUNTER_CTRL_IRQ_EN | NETX_GPIO_COUNTER_CTRL_RUN, | 
					
						
							| 
									
										
										
										
											2008-12-09 21:57:21 +01:00
										 |  |  | 			NETX_GPIO_COUNTER_CTRL(0)); | 
					
						
							| 
									
										
										
										
											2006-06-19 15:27:53 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	setup_irq(NETX_IRQ_TIMER0, &netx_timer_irq); | 
					
						
							| 
									
										
										
										
											2006-12-12 09:23:45 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* Setup timer one for clocksource */ | 
					
						
							| 
									
										
										
										
											2008-12-09 21:57:22 +01:00
										 |  |  | 	writel(0, NETX_GPIO_COUNTER_CTRL(TIMER_CLOCKSOURCE)); | 
					
						
							|  |  |  | 	writel(0, NETX_GPIO_COUNTER_CURRENT(TIMER_CLOCKSOURCE)); | 
					
						
							|  |  |  | 	writel(0xffffffff, NETX_GPIO_COUNTER_MAX(TIMER_CLOCKSOURCE)); | 
					
						
							| 
									
										
										
										
											2006-12-12 09:23:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-09 21:57:21 +01:00
										 |  |  | 	writel(NETX_GPIO_COUNTER_CTRL_RUN, | 
					
						
							| 
									
										
										
										
											2008-12-09 21:57:22 +01:00
										 |  |  | 			NETX_GPIO_COUNTER_CTRL(TIMER_CLOCKSOURCE)); | 
					
						
							| 
									
										
										
										
											2006-12-12 09:23:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-08 14:09:47 +01:00
										 |  |  | 	clocksource_mmio_init(NETX_GPIO_COUNTER_CURRENT(TIMER_CLOCKSOURCE), | 
					
						
							| 
									
										
										
										
											2013-11-12 20:56:02 +01:00
										 |  |  | 		"netx_timer", NETX_CLOCK_FREQ, 200, 32, clocksource_mmio_readl_up); | 
					
						
							| 
									
										
										
										
											2008-12-09 21:57:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* with max_delta_ns >= delta2ns(0x800) the system currently runs fine.
 | 
					
						
							|  |  |  | 	 * Adding some safety ... */ | 
					
						
							| 
									
										
										
										
											2009-01-08 12:02:27 +00:00
										 |  |  | 	netx_clockevent.cpumask = cpumask_of(0); | 
					
						
							| 
									
										
										
										
											2013-11-12 20:56:02 +01:00
										 |  |  | 	clockevents_config_and_register(&netx_clockevent, NETX_CLOCK_FREQ, | 
					
						
							| 
									
										
										
										
											2013-01-12 11:50:05 +00:00
										 |  |  | 					0xa00, 0xfffffffe); | 
					
						
							| 
									
										
										
										
											2006-06-19 15:27:53 +01:00
										 |  |  | } |