18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2007-2009 ST-Ericsson AB
48c2ecf20Sopenharmony_ci * Timer COH 901 328, runs the OS timer interrupt.
58c2ecf20Sopenharmony_ci * Author: Linus Walleij <linus.walleij@stericsson.com>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
88c2ecf20Sopenharmony_ci#include <linux/time.h>
98c2ecf20Sopenharmony_ci#include <linux/timex.h>
108c2ecf20Sopenharmony_ci#include <linux/clockchips.h>
118c2ecf20Sopenharmony_ci#include <linux/clocksource.h>
128c2ecf20Sopenharmony_ci#include <linux/types.h>
138c2ecf20Sopenharmony_ci#include <linux/io.h>
148c2ecf20Sopenharmony_ci#include <linux/clk.h>
158c2ecf20Sopenharmony_ci#include <linux/err.h>
168c2ecf20Sopenharmony_ci#include <linux/irq.h>
178c2ecf20Sopenharmony_ci#include <linux/delay.h>
188c2ecf20Sopenharmony_ci#include <linux/of_address.h>
198c2ecf20Sopenharmony_ci#include <linux/of_irq.h>
208c2ecf20Sopenharmony_ci#include <linux/sched_clock.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/* Generic stuff */
238c2ecf20Sopenharmony_ci#include <asm/mach/map.h>
248c2ecf20Sopenharmony_ci#include <asm/mach/time.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci/*
278c2ecf20Sopenharmony_ci * APP side special timer registers
288c2ecf20Sopenharmony_ci * This timer contains four timers which can fire an interrupt each.
298c2ecf20Sopenharmony_ci * OS (operating system) timer @ 32768 Hz
308c2ecf20Sopenharmony_ci * DD (device driver) timer @ 1 kHz
318c2ecf20Sopenharmony_ci * GP1 (general purpose 1) timer @ 1MHz
328c2ecf20Sopenharmony_ci * GP2 (general purpose 2) timer @ 1MHz
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/* Reset OS Timer 32bit (-/W) */
368c2ecf20Sopenharmony_ci#define U300_TIMER_APP_ROST					(0x0000)
378c2ecf20Sopenharmony_ci#define U300_TIMER_APP_ROST_TIMER_RESET				(0x00000000)
388c2ecf20Sopenharmony_ci/* Enable OS Timer 32bit (-/W) */
398c2ecf20Sopenharmony_ci#define U300_TIMER_APP_EOST					(0x0004)
408c2ecf20Sopenharmony_ci#define U300_TIMER_APP_EOST_TIMER_ENABLE			(0x00000000)
418c2ecf20Sopenharmony_ci/* Disable OS Timer 32bit (-/W) */
428c2ecf20Sopenharmony_ci#define U300_TIMER_APP_DOST					(0x0008)
438c2ecf20Sopenharmony_ci#define U300_TIMER_APP_DOST_TIMER_DISABLE			(0x00000000)
448c2ecf20Sopenharmony_ci/* OS Timer Mode Register 32bit (-/W) */
458c2ecf20Sopenharmony_ci#define U300_TIMER_APP_SOSTM					(0x000c)
468c2ecf20Sopenharmony_ci#define U300_TIMER_APP_SOSTM_MODE_CONTINUOUS			(0x00000000)
478c2ecf20Sopenharmony_ci#define U300_TIMER_APP_SOSTM_MODE_ONE_SHOT			(0x00000001)
488c2ecf20Sopenharmony_ci/* OS Timer Status Register 32bit (R/-) */
498c2ecf20Sopenharmony_ci#define U300_TIMER_APP_OSTS					(0x0010)
508c2ecf20Sopenharmony_ci#define U300_TIMER_APP_OSTS_TIMER_STATE_MASK			(0x0000000F)
518c2ecf20Sopenharmony_ci#define U300_TIMER_APP_OSTS_TIMER_STATE_IDLE			(0x00000001)
528c2ecf20Sopenharmony_ci#define U300_TIMER_APP_OSTS_TIMER_STATE_ACTIVE			(0x00000002)
538c2ecf20Sopenharmony_ci#define U300_TIMER_APP_OSTS_ENABLE_IND				(0x00000010)
548c2ecf20Sopenharmony_ci#define U300_TIMER_APP_OSTS_MODE_MASK				(0x00000020)
558c2ecf20Sopenharmony_ci#define U300_TIMER_APP_OSTS_MODE_CONTINUOUS			(0x00000000)
568c2ecf20Sopenharmony_ci#define U300_TIMER_APP_OSTS_MODE_ONE_SHOT			(0x00000020)
578c2ecf20Sopenharmony_ci#define U300_TIMER_APP_OSTS_IRQ_ENABLED_IND			(0x00000040)
588c2ecf20Sopenharmony_ci#define U300_TIMER_APP_OSTS_IRQ_PENDING_IND			(0x00000080)
598c2ecf20Sopenharmony_ci/* OS Timer Current Count Register 32bit (R/-) */
608c2ecf20Sopenharmony_ci#define U300_TIMER_APP_OSTCC					(0x0014)
618c2ecf20Sopenharmony_ci/* OS Timer Terminal Count Register 32bit (R/W) */
628c2ecf20Sopenharmony_ci#define U300_TIMER_APP_OSTTC					(0x0018)
638c2ecf20Sopenharmony_ci/* OS Timer Interrupt Enable Register 32bit (-/W) */
648c2ecf20Sopenharmony_ci#define U300_TIMER_APP_OSTIE					(0x001c)
658c2ecf20Sopenharmony_ci#define U300_TIMER_APP_OSTIE_IRQ_DISABLE			(0x00000000)
668c2ecf20Sopenharmony_ci#define U300_TIMER_APP_OSTIE_IRQ_ENABLE				(0x00000001)
678c2ecf20Sopenharmony_ci/* OS Timer Interrupt Acknowledge Register 32bit (-/W) */
688c2ecf20Sopenharmony_ci#define U300_TIMER_APP_OSTIA					(0x0020)
698c2ecf20Sopenharmony_ci#define U300_TIMER_APP_OSTIA_IRQ_ACK				(0x00000080)
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci/* Reset DD Timer 32bit (-/W) */
728c2ecf20Sopenharmony_ci#define U300_TIMER_APP_RDDT					(0x0040)
738c2ecf20Sopenharmony_ci#define U300_TIMER_APP_RDDT_TIMER_RESET				(0x00000000)
748c2ecf20Sopenharmony_ci/* Enable DD Timer 32bit (-/W) */
758c2ecf20Sopenharmony_ci#define U300_TIMER_APP_EDDT					(0x0044)
768c2ecf20Sopenharmony_ci#define U300_TIMER_APP_EDDT_TIMER_ENABLE			(0x00000000)
778c2ecf20Sopenharmony_ci/* Disable DD Timer 32bit (-/W) */
788c2ecf20Sopenharmony_ci#define U300_TIMER_APP_DDDT					(0x0048)
798c2ecf20Sopenharmony_ci#define U300_TIMER_APP_DDDT_TIMER_DISABLE			(0x00000000)
808c2ecf20Sopenharmony_ci/* DD Timer Mode Register 32bit (-/W) */
818c2ecf20Sopenharmony_ci#define U300_TIMER_APP_SDDTM					(0x004c)
828c2ecf20Sopenharmony_ci#define U300_TIMER_APP_SDDTM_MODE_CONTINUOUS			(0x00000000)
838c2ecf20Sopenharmony_ci#define U300_TIMER_APP_SDDTM_MODE_ONE_SHOT			(0x00000001)
848c2ecf20Sopenharmony_ci/* DD Timer Status Register 32bit (R/-) */
858c2ecf20Sopenharmony_ci#define U300_TIMER_APP_DDTS					(0x0050)
868c2ecf20Sopenharmony_ci#define U300_TIMER_APP_DDTS_TIMER_STATE_MASK			(0x0000000F)
878c2ecf20Sopenharmony_ci#define U300_TIMER_APP_DDTS_TIMER_STATE_IDLE			(0x00000001)
888c2ecf20Sopenharmony_ci#define U300_TIMER_APP_DDTS_TIMER_STATE_ACTIVE			(0x00000002)
898c2ecf20Sopenharmony_ci#define U300_TIMER_APP_DDTS_ENABLE_IND				(0x00000010)
908c2ecf20Sopenharmony_ci#define U300_TIMER_APP_DDTS_MODE_MASK				(0x00000020)
918c2ecf20Sopenharmony_ci#define U300_TIMER_APP_DDTS_MODE_CONTINUOUS			(0x00000000)
928c2ecf20Sopenharmony_ci#define U300_TIMER_APP_DDTS_MODE_ONE_SHOT			(0x00000020)
938c2ecf20Sopenharmony_ci#define U300_TIMER_APP_DDTS_IRQ_ENABLED_IND			(0x00000040)
948c2ecf20Sopenharmony_ci#define U300_TIMER_APP_DDTS_IRQ_PENDING_IND			(0x00000080)
958c2ecf20Sopenharmony_ci/* DD Timer Current Count Register 32bit (R/-) */
968c2ecf20Sopenharmony_ci#define U300_TIMER_APP_DDTCC					(0x0054)
978c2ecf20Sopenharmony_ci/* DD Timer Terminal Count Register 32bit (R/W) */
988c2ecf20Sopenharmony_ci#define U300_TIMER_APP_DDTTC					(0x0058)
998c2ecf20Sopenharmony_ci/* DD Timer Interrupt Enable Register 32bit (-/W) */
1008c2ecf20Sopenharmony_ci#define U300_TIMER_APP_DDTIE					(0x005c)
1018c2ecf20Sopenharmony_ci#define U300_TIMER_APP_DDTIE_IRQ_DISABLE			(0x00000000)
1028c2ecf20Sopenharmony_ci#define U300_TIMER_APP_DDTIE_IRQ_ENABLE				(0x00000001)
1038c2ecf20Sopenharmony_ci/* DD Timer Interrupt Acknowledge Register 32bit (-/W) */
1048c2ecf20Sopenharmony_ci#define U300_TIMER_APP_DDTIA					(0x0060)
1058c2ecf20Sopenharmony_ci#define U300_TIMER_APP_DDTIA_IRQ_ACK				(0x00000080)
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci/* Reset GP1 Timer 32bit (-/W) */
1088c2ecf20Sopenharmony_ci#define U300_TIMER_APP_RGPT1					(0x0080)
1098c2ecf20Sopenharmony_ci#define U300_TIMER_APP_RGPT1_TIMER_RESET			(0x00000000)
1108c2ecf20Sopenharmony_ci/* Enable GP1 Timer 32bit (-/W) */
1118c2ecf20Sopenharmony_ci#define U300_TIMER_APP_EGPT1					(0x0084)
1128c2ecf20Sopenharmony_ci#define U300_TIMER_APP_EGPT1_TIMER_ENABLE			(0x00000000)
1138c2ecf20Sopenharmony_ci/* Disable GP1 Timer 32bit (-/W) */
1148c2ecf20Sopenharmony_ci#define U300_TIMER_APP_DGPT1					(0x0088)
1158c2ecf20Sopenharmony_ci#define U300_TIMER_APP_DGPT1_TIMER_DISABLE			(0x00000000)
1168c2ecf20Sopenharmony_ci/* GP1 Timer Mode Register 32bit (-/W) */
1178c2ecf20Sopenharmony_ci#define U300_TIMER_APP_SGPT1M					(0x008c)
1188c2ecf20Sopenharmony_ci#define U300_TIMER_APP_SGPT1M_MODE_CONTINUOUS			(0x00000000)
1198c2ecf20Sopenharmony_ci#define U300_TIMER_APP_SGPT1M_MODE_ONE_SHOT			(0x00000001)
1208c2ecf20Sopenharmony_ci/* GP1 Timer Status Register 32bit (R/-) */
1218c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT1S					(0x0090)
1228c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT1S_TIMER_STATE_MASK			(0x0000000F)
1238c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT1S_TIMER_STATE_IDLE			(0x00000001)
1248c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT1S_TIMER_STATE_ACTIVE			(0x00000002)
1258c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT1S_ENABLE_IND				(0x00000010)
1268c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT1S_MODE_MASK				(0x00000020)
1278c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT1S_MODE_CONTINUOUS			(0x00000000)
1288c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT1S_MODE_ONE_SHOT			(0x00000020)
1298c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT1S_IRQ_ENABLED_IND			(0x00000040)
1308c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT1S_IRQ_PENDING_IND			(0x00000080)
1318c2ecf20Sopenharmony_ci/* GP1 Timer Current Count Register 32bit (R/-) */
1328c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT1CC					(0x0094)
1338c2ecf20Sopenharmony_ci/* GP1 Timer Terminal Count Register 32bit (R/W) */
1348c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT1TC					(0x0098)
1358c2ecf20Sopenharmony_ci/* GP1 Timer Interrupt Enable Register 32bit (-/W) */
1368c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT1IE					(0x009c)
1378c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT1IE_IRQ_DISABLE			(0x00000000)
1388c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT1IE_IRQ_ENABLE			(0x00000001)
1398c2ecf20Sopenharmony_ci/* GP1 Timer Interrupt Acknowledge Register 32bit (-/W) */
1408c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT1IA					(0x00a0)
1418c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT1IA_IRQ_ACK				(0x00000080)
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci/* Reset GP2 Timer 32bit (-/W) */
1448c2ecf20Sopenharmony_ci#define U300_TIMER_APP_RGPT2					(0x00c0)
1458c2ecf20Sopenharmony_ci#define U300_TIMER_APP_RGPT2_TIMER_RESET			(0x00000000)
1468c2ecf20Sopenharmony_ci/* Enable GP2 Timer 32bit (-/W) */
1478c2ecf20Sopenharmony_ci#define U300_TIMER_APP_EGPT2					(0x00c4)
1488c2ecf20Sopenharmony_ci#define U300_TIMER_APP_EGPT2_TIMER_ENABLE			(0x00000000)
1498c2ecf20Sopenharmony_ci/* Disable GP2 Timer 32bit (-/W) */
1508c2ecf20Sopenharmony_ci#define U300_TIMER_APP_DGPT2					(0x00c8)
1518c2ecf20Sopenharmony_ci#define U300_TIMER_APP_DGPT2_TIMER_DISABLE			(0x00000000)
1528c2ecf20Sopenharmony_ci/* GP2 Timer Mode Register 32bit (-/W) */
1538c2ecf20Sopenharmony_ci#define U300_TIMER_APP_SGPT2M					(0x00cc)
1548c2ecf20Sopenharmony_ci#define U300_TIMER_APP_SGPT2M_MODE_CONTINUOUS			(0x00000000)
1558c2ecf20Sopenharmony_ci#define U300_TIMER_APP_SGPT2M_MODE_ONE_SHOT			(0x00000001)
1568c2ecf20Sopenharmony_ci/* GP2 Timer Status Register 32bit (R/-) */
1578c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT2S					(0x00d0)
1588c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT2S_TIMER_STATE_MASK			(0x0000000F)
1598c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT2S_TIMER_STATE_IDLE			(0x00000001)
1608c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT2S_TIMER_STATE_ACTIVE			(0x00000002)
1618c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT2S_ENABLE_IND				(0x00000010)
1628c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT2S_MODE_MASK				(0x00000020)
1638c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT2S_MODE_CONTINUOUS			(0x00000000)
1648c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT2S_MODE_ONE_SHOT			(0x00000020)
1658c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT2S_IRQ_ENABLED_IND			(0x00000040)
1668c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT2S_IRQ_PENDING_IND			(0x00000080)
1678c2ecf20Sopenharmony_ci/* GP2 Timer Current Count Register 32bit (R/-) */
1688c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT2CC					(0x00d4)
1698c2ecf20Sopenharmony_ci/* GP2 Timer Terminal Count Register 32bit (R/W) */
1708c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT2TC					(0x00d8)
1718c2ecf20Sopenharmony_ci/* GP2 Timer Interrupt Enable Register 32bit (-/W) */
1728c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT2IE					(0x00dc)
1738c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT2IE_IRQ_DISABLE			(0x00000000)
1748c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT2IE_IRQ_ENABLE			(0x00000001)
1758c2ecf20Sopenharmony_ci/* GP2 Timer Interrupt Acknowledge Register 32bit (-/W) */
1768c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT2IA					(0x00e0)
1778c2ecf20Sopenharmony_ci#define U300_TIMER_APP_GPT2IA_IRQ_ACK				(0x00000080)
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci/* Clock request control register - all four timers */
1808c2ecf20Sopenharmony_ci#define U300_TIMER_APP_CRC					(0x100)
1818c2ecf20Sopenharmony_ci#define U300_TIMER_APP_CRC_CLOCK_REQUEST_ENABLE			(0x00000001)
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_cistatic void __iomem *u300_timer_base;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_cistruct u300_clockevent_data {
1868c2ecf20Sopenharmony_ci	struct clock_event_device cevd;
1878c2ecf20Sopenharmony_ci	unsigned ticks_per_jiffy;
1888c2ecf20Sopenharmony_ci};
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cistatic int u300_shutdown(struct clock_event_device *evt)
1918c2ecf20Sopenharmony_ci{
1928c2ecf20Sopenharmony_ci	/* Disable interrupts on GP1 */
1938c2ecf20Sopenharmony_ci	writel(U300_TIMER_APP_GPT1IE_IRQ_DISABLE,
1948c2ecf20Sopenharmony_ci	       u300_timer_base + U300_TIMER_APP_GPT1IE);
1958c2ecf20Sopenharmony_ci	/* Disable GP1 */
1968c2ecf20Sopenharmony_ci	writel(U300_TIMER_APP_DGPT1_TIMER_DISABLE,
1978c2ecf20Sopenharmony_ci	       u300_timer_base + U300_TIMER_APP_DGPT1);
1988c2ecf20Sopenharmony_ci	return 0;
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci/*
2028c2ecf20Sopenharmony_ci * If we have oneshot timer active, the oneshot scheduling function
2038c2ecf20Sopenharmony_ci * u300_set_next_event() is called immediately after.
2048c2ecf20Sopenharmony_ci */
2058c2ecf20Sopenharmony_cistatic int u300_set_oneshot(struct clock_event_device *evt)
2068c2ecf20Sopenharmony_ci{
2078c2ecf20Sopenharmony_ci	/* Just return; here? */
2088c2ecf20Sopenharmony_ci	/*
2098c2ecf20Sopenharmony_ci	 * The actual event will be programmed by the next event hook,
2108c2ecf20Sopenharmony_ci	 * so we just set a dummy value somewhere at the end of the
2118c2ecf20Sopenharmony_ci	 * universe here.
2128c2ecf20Sopenharmony_ci	 */
2138c2ecf20Sopenharmony_ci	/* Disable interrupts on GPT1 */
2148c2ecf20Sopenharmony_ci	writel(U300_TIMER_APP_GPT1IE_IRQ_DISABLE,
2158c2ecf20Sopenharmony_ci	       u300_timer_base + U300_TIMER_APP_GPT1IE);
2168c2ecf20Sopenharmony_ci	/* Disable GP1 while we're reprogramming it. */
2178c2ecf20Sopenharmony_ci	writel(U300_TIMER_APP_DGPT1_TIMER_DISABLE,
2188c2ecf20Sopenharmony_ci	       u300_timer_base + U300_TIMER_APP_DGPT1);
2198c2ecf20Sopenharmony_ci	/*
2208c2ecf20Sopenharmony_ci	 * Expire far in the future, u300_set_next_event() will be
2218c2ecf20Sopenharmony_ci	 * called soon...
2228c2ecf20Sopenharmony_ci	 */
2238c2ecf20Sopenharmony_ci	writel(0xFFFFFFFF, u300_timer_base + U300_TIMER_APP_GPT1TC);
2248c2ecf20Sopenharmony_ci	/* We run one shot per tick here! */
2258c2ecf20Sopenharmony_ci	writel(U300_TIMER_APP_SGPT1M_MODE_ONE_SHOT,
2268c2ecf20Sopenharmony_ci	       u300_timer_base + U300_TIMER_APP_SGPT1M);
2278c2ecf20Sopenharmony_ci	/* Enable interrupts for this timer */
2288c2ecf20Sopenharmony_ci	writel(U300_TIMER_APP_GPT1IE_IRQ_ENABLE,
2298c2ecf20Sopenharmony_ci	       u300_timer_base + U300_TIMER_APP_GPT1IE);
2308c2ecf20Sopenharmony_ci	/* Enable timer */
2318c2ecf20Sopenharmony_ci	writel(U300_TIMER_APP_EGPT1_TIMER_ENABLE,
2328c2ecf20Sopenharmony_ci	       u300_timer_base + U300_TIMER_APP_EGPT1);
2338c2ecf20Sopenharmony_ci	return 0;
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_cistatic int u300_set_periodic(struct clock_event_device *evt)
2378c2ecf20Sopenharmony_ci{
2388c2ecf20Sopenharmony_ci	struct u300_clockevent_data *cevdata =
2398c2ecf20Sopenharmony_ci		container_of(evt, struct u300_clockevent_data, cevd);
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	/* Disable interrupts on GPT1 */
2428c2ecf20Sopenharmony_ci	writel(U300_TIMER_APP_GPT1IE_IRQ_DISABLE,
2438c2ecf20Sopenharmony_ci	       u300_timer_base + U300_TIMER_APP_GPT1IE);
2448c2ecf20Sopenharmony_ci	/* Disable GP1 while we're reprogramming it. */
2458c2ecf20Sopenharmony_ci	writel(U300_TIMER_APP_DGPT1_TIMER_DISABLE,
2468c2ecf20Sopenharmony_ci	       u300_timer_base + U300_TIMER_APP_DGPT1);
2478c2ecf20Sopenharmony_ci	/*
2488c2ecf20Sopenharmony_ci	 * Set the periodic mode to a certain number of ticks per
2498c2ecf20Sopenharmony_ci	 * jiffy.
2508c2ecf20Sopenharmony_ci	 */
2518c2ecf20Sopenharmony_ci	writel(cevdata->ticks_per_jiffy,
2528c2ecf20Sopenharmony_ci	       u300_timer_base + U300_TIMER_APP_GPT1TC);
2538c2ecf20Sopenharmony_ci	/*
2548c2ecf20Sopenharmony_ci	 * Set continuous mode, so the timer keeps triggering
2558c2ecf20Sopenharmony_ci	 * interrupts.
2568c2ecf20Sopenharmony_ci	 */
2578c2ecf20Sopenharmony_ci	writel(U300_TIMER_APP_SGPT1M_MODE_CONTINUOUS,
2588c2ecf20Sopenharmony_ci	       u300_timer_base + U300_TIMER_APP_SGPT1M);
2598c2ecf20Sopenharmony_ci	/* Enable timer interrupts */
2608c2ecf20Sopenharmony_ci	writel(U300_TIMER_APP_GPT1IE_IRQ_ENABLE,
2618c2ecf20Sopenharmony_ci	       u300_timer_base + U300_TIMER_APP_GPT1IE);
2628c2ecf20Sopenharmony_ci	/* Then enable the OS timer again */
2638c2ecf20Sopenharmony_ci	writel(U300_TIMER_APP_EGPT1_TIMER_ENABLE,
2648c2ecf20Sopenharmony_ci	       u300_timer_base + U300_TIMER_APP_EGPT1);
2658c2ecf20Sopenharmony_ci	return 0;
2668c2ecf20Sopenharmony_ci}
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci/*
2698c2ecf20Sopenharmony_ci * The app timer in one shot mode obviously has to be reprogrammed
2708c2ecf20Sopenharmony_ci * in EXACTLY this sequence to work properly. Do NOT try to e.g. replace
2718c2ecf20Sopenharmony_ci * the interrupt disable + timer disable commands with a reset command,
2728c2ecf20Sopenharmony_ci * it will fail miserably. Apparently (and I found this the hard way)
2738c2ecf20Sopenharmony_ci * the timer is very sensitive to the instruction order, though you don't
2748c2ecf20Sopenharmony_ci * get that impression from the data sheet.
2758c2ecf20Sopenharmony_ci */
2768c2ecf20Sopenharmony_cistatic int u300_set_next_event(unsigned long cycles,
2778c2ecf20Sopenharmony_ci			       struct clock_event_device *evt)
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci{
2808c2ecf20Sopenharmony_ci	/* Disable interrupts on GPT1 */
2818c2ecf20Sopenharmony_ci	writel(U300_TIMER_APP_GPT1IE_IRQ_DISABLE,
2828c2ecf20Sopenharmony_ci	       u300_timer_base + U300_TIMER_APP_GPT1IE);
2838c2ecf20Sopenharmony_ci	/* Disable GP1 while we're reprogramming it. */
2848c2ecf20Sopenharmony_ci	writel(U300_TIMER_APP_DGPT1_TIMER_DISABLE,
2858c2ecf20Sopenharmony_ci	       u300_timer_base + U300_TIMER_APP_DGPT1);
2868c2ecf20Sopenharmony_ci	/* Reset the General Purpose timer 1. */
2878c2ecf20Sopenharmony_ci	writel(U300_TIMER_APP_RGPT1_TIMER_RESET,
2888c2ecf20Sopenharmony_ci	       u300_timer_base + U300_TIMER_APP_RGPT1);
2898c2ecf20Sopenharmony_ci	/* IRQ in n * cycles */
2908c2ecf20Sopenharmony_ci	writel(cycles, u300_timer_base + U300_TIMER_APP_GPT1TC);
2918c2ecf20Sopenharmony_ci	/*
2928c2ecf20Sopenharmony_ci	 * We run one shot per tick here! (This is necessary to reconfigure,
2938c2ecf20Sopenharmony_ci	 * the timer will tilt if you don't!)
2948c2ecf20Sopenharmony_ci	 */
2958c2ecf20Sopenharmony_ci	writel(U300_TIMER_APP_SGPT1M_MODE_ONE_SHOT,
2968c2ecf20Sopenharmony_ci	       u300_timer_base + U300_TIMER_APP_SGPT1M);
2978c2ecf20Sopenharmony_ci	/* Enable timer interrupts */
2988c2ecf20Sopenharmony_ci	writel(U300_TIMER_APP_GPT1IE_IRQ_ENABLE,
2998c2ecf20Sopenharmony_ci	       u300_timer_base + U300_TIMER_APP_GPT1IE);
3008c2ecf20Sopenharmony_ci	/* Then enable the OS timer again */
3018c2ecf20Sopenharmony_ci	writel(U300_TIMER_APP_EGPT1_TIMER_ENABLE,
3028c2ecf20Sopenharmony_ci	       u300_timer_base + U300_TIMER_APP_EGPT1);
3038c2ecf20Sopenharmony_ci	return 0;
3048c2ecf20Sopenharmony_ci}
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_cistatic struct u300_clockevent_data u300_clockevent_data = {
3078c2ecf20Sopenharmony_ci	/* Use general purpose timer 1 as clock event */
3088c2ecf20Sopenharmony_ci	.cevd = {
3098c2ecf20Sopenharmony_ci		.name			= "GPT1",
3108c2ecf20Sopenharmony_ci		/* Reasonably fast and accurate clock event */
3118c2ecf20Sopenharmony_ci		.rating			= 300,
3128c2ecf20Sopenharmony_ci		.features		= CLOCK_EVT_FEAT_PERIODIC |
3138c2ecf20Sopenharmony_ci					  CLOCK_EVT_FEAT_ONESHOT,
3148c2ecf20Sopenharmony_ci		.set_next_event		= u300_set_next_event,
3158c2ecf20Sopenharmony_ci		.set_state_shutdown	= u300_shutdown,
3168c2ecf20Sopenharmony_ci		.set_state_periodic	= u300_set_periodic,
3178c2ecf20Sopenharmony_ci		.set_state_oneshot	= u300_set_oneshot,
3188c2ecf20Sopenharmony_ci	},
3198c2ecf20Sopenharmony_ci};
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci/* Clock event timer interrupt handler */
3228c2ecf20Sopenharmony_cistatic irqreturn_t u300_timer_interrupt(int irq, void *dev_id)
3238c2ecf20Sopenharmony_ci{
3248c2ecf20Sopenharmony_ci	struct clock_event_device *evt = &u300_clockevent_data.cevd;
3258c2ecf20Sopenharmony_ci	/* ACK/Clear timer IRQ for the APP GPT1 Timer */
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	writel(U300_TIMER_APP_GPT1IA_IRQ_ACK,
3288c2ecf20Sopenharmony_ci		u300_timer_base + U300_TIMER_APP_GPT1IA);
3298c2ecf20Sopenharmony_ci	evt->event_handler(evt);
3308c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
3318c2ecf20Sopenharmony_ci}
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci/*
3348c2ecf20Sopenharmony_ci * Override the global weak sched_clock symbol with this
3358c2ecf20Sopenharmony_ci * local implementation which uses the clocksource to get some
3368c2ecf20Sopenharmony_ci * better resolution when scheduling the kernel. We accept that
3378c2ecf20Sopenharmony_ci * this wraps around for now, since it is just a relative time
3388c2ecf20Sopenharmony_ci * stamp. (Inspired by OMAP implementation.)
3398c2ecf20Sopenharmony_ci */
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_cistatic u64 notrace u300_read_sched_clock(void)
3428c2ecf20Sopenharmony_ci{
3438c2ecf20Sopenharmony_ci	return readl(u300_timer_base + U300_TIMER_APP_GPT2CC);
3448c2ecf20Sopenharmony_ci}
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_cistatic unsigned long u300_read_current_timer(void)
3478c2ecf20Sopenharmony_ci{
3488c2ecf20Sopenharmony_ci	return readl(u300_timer_base + U300_TIMER_APP_GPT2CC);
3498c2ecf20Sopenharmony_ci}
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_cistatic struct delay_timer u300_delay_timer;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci/*
3548c2ecf20Sopenharmony_ci * This sets up the system timers, clock source and clock event.
3558c2ecf20Sopenharmony_ci */
3568c2ecf20Sopenharmony_cistatic int __init u300_timer_init_of(struct device_node *np)
3578c2ecf20Sopenharmony_ci{
3588c2ecf20Sopenharmony_ci	unsigned int irq;
3598c2ecf20Sopenharmony_ci	struct clk *clk;
3608c2ecf20Sopenharmony_ci	unsigned long rate;
3618c2ecf20Sopenharmony_ci	int ret;
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	u300_timer_base = of_iomap(np, 0);
3648c2ecf20Sopenharmony_ci	if (!u300_timer_base) {
3658c2ecf20Sopenharmony_ci		pr_err("could not ioremap system timer\n");
3668c2ecf20Sopenharmony_ci		return -ENXIO;
3678c2ecf20Sopenharmony_ci	}
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	/* Get the IRQ for the GP1 timer */
3708c2ecf20Sopenharmony_ci	irq = irq_of_parse_and_map(np, 2);
3718c2ecf20Sopenharmony_ci	if (!irq) {
3728c2ecf20Sopenharmony_ci		pr_err("no IRQ for system timer\n");
3738c2ecf20Sopenharmony_ci		return -EINVAL;
3748c2ecf20Sopenharmony_ci	}
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	pr_info("U300 GP1 timer @ base: %p, IRQ: %u\n", u300_timer_base, irq);
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	/* Clock the interrupt controller */
3798c2ecf20Sopenharmony_ci	clk = of_clk_get(np, 0);
3808c2ecf20Sopenharmony_ci	if (IS_ERR(clk))
3818c2ecf20Sopenharmony_ci		return PTR_ERR(clk);
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(clk);
3848c2ecf20Sopenharmony_ci	if (ret)
3858c2ecf20Sopenharmony_ci		return ret;
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	rate = clk_get_rate(clk);
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	u300_clockevent_data.ticks_per_jiffy = DIV_ROUND_CLOSEST(rate, HZ);
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	sched_clock_register(u300_read_sched_clock, 32, rate);
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	u300_delay_timer.read_current_timer = &u300_read_current_timer;
3948c2ecf20Sopenharmony_ci	u300_delay_timer.freq = rate;
3958c2ecf20Sopenharmony_ci	register_current_timer_delay(&u300_delay_timer);
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	/*
3988c2ecf20Sopenharmony_ci	 * Disable the "OS" and "DD" timers - these are designed for Symbian!
3998c2ecf20Sopenharmony_ci	 * Example usage in cnh1601578 cpu subsystem pd_timer_app.c
4008c2ecf20Sopenharmony_ci	 */
4018c2ecf20Sopenharmony_ci	writel(U300_TIMER_APP_CRC_CLOCK_REQUEST_ENABLE,
4028c2ecf20Sopenharmony_ci		u300_timer_base + U300_TIMER_APP_CRC);
4038c2ecf20Sopenharmony_ci	writel(U300_TIMER_APP_ROST_TIMER_RESET,
4048c2ecf20Sopenharmony_ci		u300_timer_base + U300_TIMER_APP_ROST);
4058c2ecf20Sopenharmony_ci	writel(U300_TIMER_APP_DOST_TIMER_DISABLE,
4068c2ecf20Sopenharmony_ci		u300_timer_base + U300_TIMER_APP_DOST);
4078c2ecf20Sopenharmony_ci	writel(U300_TIMER_APP_RDDT_TIMER_RESET,
4088c2ecf20Sopenharmony_ci		u300_timer_base + U300_TIMER_APP_RDDT);
4098c2ecf20Sopenharmony_ci	writel(U300_TIMER_APP_DDDT_TIMER_DISABLE,
4108c2ecf20Sopenharmony_ci		u300_timer_base + U300_TIMER_APP_DDDT);
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	/* Reset the General Purpose timer 1. */
4138c2ecf20Sopenharmony_ci	writel(U300_TIMER_APP_RGPT1_TIMER_RESET,
4148c2ecf20Sopenharmony_ci		u300_timer_base + U300_TIMER_APP_RGPT1);
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	/* Set up the IRQ handler */
4178c2ecf20Sopenharmony_ci	ret = request_irq(irq, u300_timer_interrupt,
4188c2ecf20Sopenharmony_ci			  IRQF_TIMER | IRQF_IRQPOLL, "U300 Timer Tick", NULL);
4198c2ecf20Sopenharmony_ci	if (ret)
4208c2ecf20Sopenharmony_ci		return ret;
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	/* Reset the General Purpose timer 2 */
4238c2ecf20Sopenharmony_ci	writel(U300_TIMER_APP_RGPT2_TIMER_RESET,
4248c2ecf20Sopenharmony_ci		u300_timer_base + U300_TIMER_APP_RGPT2);
4258c2ecf20Sopenharmony_ci	/* Set this timer to run around forever */
4268c2ecf20Sopenharmony_ci	writel(0xFFFFFFFFU, u300_timer_base + U300_TIMER_APP_GPT2TC);
4278c2ecf20Sopenharmony_ci	/* Set continuous mode so it wraps around */
4288c2ecf20Sopenharmony_ci	writel(U300_TIMER_APP_SGPT2M_MODE_CONTINUOUS,
4298c2ecf20Sopenharmony_ci	       u300_timer_base + U300_TIMER_APP_SGPT2M);
4308c2ecf20Sopenharmony_ci	/* Disable timer interrupts */
4318c2ecf20Sopenharmony_ci	writel(U300_TIMER_APP_GPT2IE_IRQ_DISABLE,
4328c2ecf20Sopenharmony_ci		u300_timer_base + U300_TIMER_APP_GPT2IE);
4338c2ecf20Sopenharmony_ci	/* Then enable the GP2 timer to use as a free running us counter */
4348c2ecf20Sopenharmony_ci	writel(U300_TIMER_APP_EGPT2_TIMER_ENABLE,
4358c2ecf20Sopenharmony_ci		u300_timer_base + U300_TIMER_APP_EGPT2);
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	/* Use general purpose timer 2 as clock source */
4388c2ecf20Sopenharmony_ci	ret = clocksource_mmio_init(u300_timer_base + U300_TIMER_APP_GPT2CC,
4398c2ecf20Sopenharmony_ci				    "GPT2", rate, 300, 32, clocksource_mmio_readl_up);
4408c2ecf20Sopenharmony_ci	if (ret) {
4418c2ecf20Sopenharmony_ci		pr_err("timer: failed to initialize U300 clock source\n");
4428c2ecf20Sopenharmony_ci		return ret;
4438c2ecf20Sopenharmony_ci	}
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci	/* Configure and register the clockevent */
4468c2ecf20Sopenharmony_ci	clockevents_config_and_register(&u300_clockevent_data.cevd, rate,
4478c2ecf20Sopenharmony_ci					1, 0xffffffff);
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	/*
4508c2ecf20Sopenharmony_ci	 * TODO: init and register the rest of the timers too, they can be
4518c2ecf20Sopenharmony_ci	 * used by hrtimers!
4528c2ecf20Sopenharmony_ci	 */
4538c2ecf20Sopenharmony_ci	return 0;
4548c2ecf20Sopenharmony_ci}
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ciTIMER_OF_DECLARE(u300_timer, "stericsson,u300-apptimer",
4578c2ecf20Sopenharmony_ci		       u300_timer_init_of);
458