18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * J-Core SoC PIT/clocksource driver
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 2015-2016 Smart Energy Instruments, Inc.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public
78c2ecf20Sopenharmony_ci * License.  See the file "COPYING" in the main directory of this archive
88c2ecf20Sopenharmony_ci * for more details.
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/kernel.h>
128c2ecf20Sopenharmony_ci#include <linux/slab.h>
138c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
148c2ecf20Sopenharmony_ci#include <linux/clockchips.h>
158c2ecf20Sopenharmony_ci#include <linux/clocksource.h>
168c2ecf20Sopenharmony_ci#include <linux/sched_clock.h>
178c2ecf20Sopenharmony_ci#include <linux/cpu.h>
188c2ecf20Sopenharmony_ci#include <linux/cpuhotplug.h>
198c2ecf20Sopenharmony_ci#include <linux/of_address.h>
208c2ecf20Sopenharmony_ci#include <linux/of_irq.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define PIT_IRQ_SHIFT		12
238c2ecf20Sopenharmony_ci#define PIT_PRIO_SHIFT		20
248c2ecf20Sopenharmony_ci#define PIT_ENABLE_SHIFT	26
258c2ecf20Sopenharmony_ci#define PIT_PRIO_MASK		0xf
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define REG_PITEN		0x00
288c2ecf20Sopenharmony_ci#define REG_THROT		0x10
298c2ecf20Sopenharmony_ci#define REG_COUNT		0x14
308c2ecf20Sopenharmony_ci#define REG_BUSPD		0x18
318c2ecf20Sopenharmony_ci#define REG_SECHI		0x20
328c2ecf20Sopenharmony_ci#define REG_SECLO		0x24
338c2ecf20Sopenharmony_ci#define REG_NSEC		0x28
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistruct jcore_pit {
368c2ecf20Sopenharmony_ci	struct clock_event_device	ced;
378c2ecf20Sopenharmony_ci	void __iomem			*base;
388c2ecf20Sopenharmony_ci	unsigned long			periodic_delta;
398c2ecf20Sopenharmony_ci	u32				enable_val;
408c2ecf20Sopenharmony_ci};
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistatic void __iomem *jcore_pit_base;
438c2ecf20Sopenharmony_cistatic struct jcore_pit __percpu *jcore_pit_percpu;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic notrace u64 jcore_sched_clock_read(void)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	u32 seclo, nsec, seclo0;
488c2ecf20Sopenharmony_ci	__iomem void *base = jcore_pit_base;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	seclo = readl(base + REG_SECLO);
518c2ecf20Sopenharmony_ci	do {
528c2ecf20Sopenharmony_ci		seclo0 = seclo;
538c2ecf20Sopenharmony_ci		nsec  = readl(base + REG_NSEC);
548c2ecf20Sopenharmony_ci		seclo = readl(base + REG_SECLO);
558c2ecf20Sopenharmony_ci	} while (seclo0 != seclo);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	return seclo * NSEC_PER_SEC + nsec;
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic u64 jcore_clocksource_read(struct clocksource *cs)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	return jcore_sched_clock_read();
638c2ecf20Sopenharmony_ci}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic int jcore_pit_disable(struct jcore_pit *pit)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	writel(0, pit->base + REG_PITEN);
688c2ecf20Sopenharmony_ci	return 0;
698c2ecf20Sopenharmony_ci}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistatic int jcore_pit_set(unsigned long delta, struct jcore_pit *pit)
728c2ecf20Sopenharmony_ci{
738c2ecf20Sopenharmony_ci	jcore_pit_disable(pit);
748c2ecf20Sopenharmony_ci	writel(delta, pit->base + REG_THROT);
758c2ecf20Sopenharmony_ci	writel(pit->enable_val, pit->base + REG_PITEN);
768c2ecf20Sopenharmony_ci	return 0;
778c2ecf20Sopenharmony_ci}
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_cistatic int jcore_pit_set_state_shutdown(struct clock_event_device *ced)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	struct jcore_pit *pit = container_of(ced, struct jcore_pit, ced);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	return jcore_pit_disable(pit);
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic int jcore_pit_set_state_oneshot(struct clock_event_device *ced)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	struct jcore_pit *pit = container_of(ced, struct jcore_pit, ced);
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	return jcore_pit_disable(pit);
918c2ecf20Sopenharmony_ci}
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic int jcore_pit_set_state_periodic(struct clock_event_device *ced)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	struct jcore_pit *pit = container_of(ced, struct jcore_pit, ced);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	return jcore_pit_set(pit->periodic_delta, pit);
988c2ecf20Sopenharmony_ci}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic int jcore_pit_set_next_event(unsigned long delta,
1018c2ecf20Sopenharmony_ci				    struct clock_event_device *ced)
1028c2ecf20Sopenharmony_ci{
1038c2ecf20Sopenharmony_ci	struct jcore_pit *pit = container_of(ced, struct jcore_pit, ced);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	return jcore_pit_set(delta, pit);
1068c2ecf20Sopenharmony_ci}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic int jcore_pit_local_init(unsigned cpu)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	struct jcore_pit *pit = this_cpu_ptr(jcore_pit_percpu);
1118c2ecf20Sopenharmony_ci	unsigned buspd, freq;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	pr_info("Local J-Core PIT init on cpu %u\n", cpu);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	buspd = readl(pit->base + REG_BUSPD);
1168c2ecf20Sopenharmony_ci	freq = DIV_ROUND_CLOSEST(NSEC_PER_SEC, buspd);
1178c2ecf20Sopenharmony_ci	pit->periodic_delta = DIV_ROUND_CLOSEST(NSEC_PER_SEC, HZ * buspd);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	clockevents_config_and_register(&pit->ced, freq, 1, ULONG_MAX);
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	return 0;
1228c2ecf20Sopenharmony_ci}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistatic irqreturn_t jcore_timer_interrupt(int irq, void *dev_id)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	struct jcore_pit *pit = this_cpu_ptr(dev_id);
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	if (clockevent_state_oneshot(&pit->ced))
1298c2ecf20Sopenharmony_ci		jcore_pit_disable(pit);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	pit->ced.event_handler(&pit->ced);
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
1348c2ecf20Sopenharmony_ci}
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_cistatic int __init jcore_pit_init(struct device_node *node)
1378c2ecf20Sopenharmony_ci{
1388c2ecf20Sopenharmony_ci	int err;
1398c2ecf20Sopenharmony_ci	unsigned pit_irq, cpu;
1408c2ecf20Sopenharmony_ci	unsigned long hwirq;
1418c2ecf20Sopenharmony_ci	u32 irqprio, enable_val;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	jcore_pit_base = of_iomap(node, 0);
1448c2ecf20Sopenharmony_ci	if (!jcore_pit_base) {
1458c2ecf20Sopenharmony_ci		pr_err("Error: Cannot map base address for J-Core PIT\n");
1468c2ecf20Sopenharmony_ci		return -ENXIO;
1478c2ecf20Sopenharmony_ci	}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	pit_irq = irq_of_parse_and_map(node, 0);
1508c2ecf20Sopenharmony_ci	if (!pit_irq) {
1518c2ecf20Sopenharmony_ci		pr_err("Error: J-Core PIT has no IRQ\n");
1528c2ecf20Sopenharmony_ci		return -ENXIO;
1538c2ecf20Sopenharmony_ci	}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	pr_info("Initializing J-Core PIT at %p IRQ %d\n",
1568c2ecf20Sopenharmony_ci		jcore_pit_base, pit_irq);
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	err = clocksource_mmio_init(jcore_pit_base, "jcore_pit_cs",
1598c2ecf20Sopenharmony_ci				    NSEC_PER_SEC, 400, 32,
1608c2ecf20Sopenharmony_ci				    jcore_clocksource_read);
1618c2ecf20Sopenharmony_ci	if (err) {
1628c2ecf20Sopenharmony_ci		pr_err("Error registering clocksource device: %d\n", err);
1638c2ecf20Sopenharmony_ci		return err;
1648c2ecf20Sopenharmony_ci	}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	sched_clock_register(jcore_sched_clock_read, 32, NSEC_PER_SEC);
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	jcore_pit_percpu = alloc_percpu(struct jcore_pit);
1698c2ecf20Sopenharmony_ci	if (!jcore_pit_percpu) {
1708c2ecf20Sopenharmony_ci		pr_err("Failed to allocate memory for clock event device\n");
1718c2ecf20Sopenharmony_ci		return -ENOMEM;
1728c2ecf20Sopenharmony_ci	}
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	err = request_irq(pit_irq, jcore_timer_interrupt,
1758c2ecf20Sopenharmony_ci			  IRQF_TIMER | IRQF_PERCPU,
1768c2ecf20Sopenharmony_ci			  "jcore_pit", jcore_pit_percpu);
1778c2ecf20Sopenharmony_ci	if (err) {
1788c2ecf20Sopenharmony_ci		pr_err("pit irq request failed: %d\n", err);
1798c2ecf20Sopenharmony_ci		free_percpu(jcore_pit_percpu);
1808c2ecf20Sopenharmony_ci		return err;
1818c2ecf20Sopenharmony_ci	}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	/*
1848c2ecf20Sopenharmony_ci	 * The J-Core PIT is not hard-wired to a particular IRQ, but
1858c2ecf20Sopenharmony_ci	 * integrated with the interrupt controller such that the IRQ it
1868c2ecf20Sopenharmony_ci	 * generates is programmable, as follows:
1878c2ecf20Sopenharmony_ci	 *
1888c2ecf20Sopenharmony_ci	 * The bit layout of the PIT enable register is:
1898c2ecf20Sopenharmony_ci	 *
1908c2ecf20Sopenharmony_ci	 *	.....e..ppppiiiiiiii............
1918c2ecf20Sopenharmony_ci	 *
1928c2ecf20Sopenharmony_ci	 * where the .'s indicate unrelated/unused bits, e is enable,
1938c2ecf20Sopenharmony_ci	 * p is priority, and i is hard irq number.
1948c2ecf20Sopenharmony_ci	 *
1958c2ecf20Sopenharmony_ci	 * For the PIT included in AIC1 (obsolete but still in use),
1968c2ecf20Sopenharmony_ci	 * any hard irq (trap number) can be programmed via the 8
1978c2ecf20Sopenharmony_ci	 * iiiiiiii bits, and a priority (0-15) is programmable
1988c2ecf20Sopenharmony_ci	 * separately in the pppp bits.
1998c2ecf20Sopenharmony_ci	 *
2008c2ecf20Sopenharmony_ci	 * For the PIT included in AIC2 (current), the programming
2018c2ecf20Sopenharmony_ci	 * interface is equivalent modulo interrupt mapping. This is
2028c2ecf20Sopenharmony_ci	 * why a different compatible tag was not used. However only
2038c2ecf20Sopenharmony_ci	 * traps 64-127 (the ones actually intended to be used for
2048c2ecf20Sopenharmony_ci	 * interrupts, rather than syscalls/exceptions/etc.) can be
2058c2ecf20Sopenharmony_ci	 * programmed (the high 2 bits of i are ignored) and the
2068c2ecf20Sopenharmony_ci	 * priority pppp is <<2'd and or'd onto the irq number. This
2078c2ecf20Sopenharmony_ci	 * choice seems to have been made on the hardware engineering
2088c2ecf20Sopenharmony_ci	 * side under an assumption that preserving old AIC1 priority
2098c2ecf20Sopenharmony_ci	 * mappings was important. Future models will likely ignore
2108c2ecf20Sopenharmony_ci	 * the pppp field.
2118c2ecf20Sopenharmony_ci	 */
2128c2ecf20Sopenharmony_ci	hwirq = irq_get_irq_data(pit_irq)->hwirq;
2138c2ecf20Sopenharmony_ci	irqprio = (hwirq >> 2) & PIT_PRIO_MASK;
2148c2ecf20Sopenharmony_ci	enable_val = (1U << PIT_ENABLE_SHIFT)
2158c2ecf20Sopenharmony_ci		   | (hwirq << PIT_IRQ_SHIFT)
2168c2ecf20Sopenharmony_ci		   | (irqprio << PIT_PRIO_SHIFT);
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	for_each_present_cpu(cpu) {
2198c2ecf20Sopenharmony_ci		struct jcore_pit *pit = per_cpu_ptr(jcore_pit_percpu, cpu);
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci		pit->base = of_iomap(node, cpu);
2228c2ecf20Sopenharmony_ci		if (!pit->base) {
2238c2ecf20Sopenharmony_ci			pr_err("Unable to map PIT for cpu %u\n", cpu);
2248c2ecf20Sopenharmony_ci			continue;
2258c2ecf20Sopenharmony_ci		}
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci		pit->ced.name = "jcore_pit";
2288c2ecf20Sopenharmony_ci		pit->ced.features = CLOCK_EVT_FEAT_PERIODIC
2298c2ecf20Sopenharmony_ci				  | CLOCK_EVT_FEAT_ONESHOT
2308c2ecf20Sopenharmony_ci				  | CLOCK_EVT_FEAT_PERCPU;
2318c2ecf20Sopenharmony_ci		pit->ced.cpumask = cpumask_of(cpu);
2328c2ecf20Sopenharmony_ci		pit->ced.rating = 400;
2338c2ecf20Sopenharmony_ci		pit->ced.irq = pit_irq;
2348c2ecf20Sopenharmony_ci		pit->ced.set_state_shutdown = jcore_pit_set_state_shutdown;
2358c2ecf20Sopenharmony_ci		pit->ced.set_state_periodic = jcore_pit_set_state_periodic;
2368c2ecf20Sopenharmony_ci		pit->ced.set_state_oneshot = jcore_pit_set_state_oneshot;
2378c2ecf20Sopenharmony_ci		pit->ced.set_next_event = jcore_pit_set_next_event;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci		pit->enable_val = enable_val;
2408c2ecf20Sopenharmony_ci	}
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	cpuhp_setup_state(CPUHP_AP_JCORE_TIMER_STARTING,
2438c2ecf20Sopenharmony_ci			  "clockevents/jcore:starting",
2448c2ecf20Sopenharmony_ci			  jcore_pit_local_init, NULL);
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	return 0;
2478c2ecf20Sopenharmony_ci}
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ciTIMER_OF_DECLARE(jcore_pit, "jcore,pit", jcore_pit_init);
250