162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Freescale General-purpose Timers Module
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) Freescale Semiconductor, Inc. 2006.
662306a36Sopenharmony_ci *               Shlomi Gridish <gridish@freescale.com>
762306a36Sopenharmony_ci *               Jerry Huang <Chang-Ming.Huang@freescale.com>
862306a36Sopenharmony_ci * Copyright (c) MontaVista Software, Inc. 2008.
962306a36Sopenharmony_ci *               Anton Vorontsov <avorontsov@ru.mvista.com>
1062306a36Sopenharmony_ci */
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#include <linux/kernel.h>
1362306a36Sopenharmony_ci#include <linux/err.h>
1462306a36Sopenharmony_ci#include <linux/errno.h>
1562306a36Sopenharmony_ci#include <linux/list.h>
1662306a36Sopenharmony_ci#include <linux/io.h>
1762306a36Sopenharmony_ci#include <linux/of.h>
1862306a36Sopenharmony_ci#include <linux/of_address.h>
1962306a36Sopenharmony_ci#include <linux/of_irq.h>
2062306a36Sopenharmony_ci#include <linux/spinlock.h>
2162306a36Sopenharmony_ci#include <linux/bitops.h>
2262306a36Sopenharmony_ci#include <linux/slab.h>
2362306a36Sopenharmony_ci#include <linux/export.h>
2462306a36Sopenharmony_ci#include <asm/fsl_gtm.h>
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#define GTCFR_STP(x)		((x) & 1 ? 1 << 5 : 1 << 1)
2762306a36Sopenharmony_ci#define GTCFR_RST(x)		((x) & 1 ? 1 << 4 : 1 << 0)
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci#define GTMDR_ICLK_MASK		(3 << 1)
3062306a36Sopenharmony_ci#define GTMDR_ICLK_ICAS		(0 << 1)
3162306a36Sopenharmony_ci#define GTMDR_ICLK_ICLK		(1 << 1)
3262306a36Sopenharmony_ci#define GTMDR_ICLK_SLGO		(2 << 1)
3362306a36Sopenharmony_ci#define GTMDR_FRR		(1 << 3)
3462306a36Sopenharmony_ci#define GTMDR_ORI		(1 << 4)
3562306a36Sopenharmony_ci#define GTMDR_SPS(x)		((x) << 8)
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_cistruct gtm_timers_regs {
3862306a36Sopenharmony_ci	u8	gtcfr1;		/* Timer 1, Timer 2 global config register */
3962306a36Sopenharmony_ci	u8	res0[0x3];
4062306a36Sopenharmony_ci	u8	gtcfr2;		/* Timer 3, timer 4 global config register */
4162306a36Sopenharmony_ci	u8	res1[0xB];
4262306a36Sopenharmony_ci	__be16	gtmdr1;		/* Timer 1 mode register */
4362306a36Sopenharmony_ci	__be16	gtmdr2;		/* Timer 2 mode register */
4462306a36Sopenharmony_ci	__be16	gtrfr1;		/* Timer 1 reference register */
4562306a36Sopenharmony_ci	__be16	gtrfr2;		/* Timer 2 reference register */
4662306a36Sopenharmony_ci	__be16	gtcpr1;		/* Timer 1 capture register */
4762306a36Sopenharmony_ci	__be16	gtcpr2;		/* Timer 2 capture register */
4862306a36Sopenharmony_ci	__be16	gtcnr1;		/* Timer 1 counter */
4962306a36Sopenharmony_ci	__be16	gtcnr2;		/* Timer 2 counter */
5062306a36Sopenharmony_ci	__be16	gtmdr3;		/* Timer 3 mode register */
5162306a36Sopenharmony_ci	__be16	gtmdr4;		/* Timer 4 mode register */
5262306a36Sopenharmony_ci	__be16	gtrfr3;		/* Timer 3 reference register */
5362306a36Sopenharmony_ci	__be16	gtrfr4;		/* Timer 4 reference register */
5462306a36Sopenharmony_ci	__be16	gtcpr3;		/* Timer 3 capture register */
5562306a36Sopenharmony_ci	__be16	gtcpr4;		/* Timer 4 capture register */
5662306a36Sopenharmony_ci	__be16	gtcnr3;		/* Timer 3 counter */
5762306a36Sopenharmony_ci	__be16	gtcnr4;		/* Timer 4 counter */
5862306a36Sopenharmony_ci	__be16	gtevr1;		/* Timer 1 event register */
5962306a36Sopenharmony_ci	__be16	gtevr2;		/* Timer 2 event register */
6062306a36Sopenharmony_ci	__be16	gtevr3;		/* Timer 3 event register */
6162306a36Sopenharmony_ci	__be16	gtevr4;		/* Timer 4 event register */
6262306a36Sopenharmony_ci	__be16	gtpsr1;		/* Timer 1 prescale register */
6362306a36Sopenharmony_ci	__be16	gtpsr2;		/* Timer 2 prescale register */
6462306a36Sopenharmony_ci	__be16	gtpsr3;		/* Timer 3 prescale register */
6562306a36Sopenharmony_ci	__be16	gtpsr4;		/* Timer 4 prescale register */
6662306a36Sopenharmony_ci	u8 res2[0x40];
6762306a36Sopenharmony_ci} __attribute__ ((packed));
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_cistruct gtm {
7062306a36Sopenharmony_ci	unsigned int clock;
7162306a36Sopenharmony_ci	struct gtm_timers_regs __iomem *regs;
7262306a36Sopenharmony_ci	struct gtm_timer timers[4];
7362306a36Sopenharmony_ci	spinlock_t lock;
7462306a36Sopenharmony_ci	struct list_head list_node;
7562306a36Sopenharmony_ci};
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_cistatic LIST_HEAD(gtms);
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci/**
8062306a36Sopenharmony_ci * gtm_get_timer - request GTM timer to use it with the rest of GTM API
8162306a36Sopenharmony_ci * Context:	non-IRQ
8262306a36Sopenharmony_ci *
8362306a36Sopenharmony_ci * This function reserves GTM timer for later use. It returns gtm_timer
8462306a36Sopenharmony_ci * structure to use with the rest of GTM API, you should use timer->irq
8562306a36Sopenharmony_ci * to manage timer interrupt.
8662306a36Sopenharmony_ci */
8762306a36Sopenharmony_cistruct gtm_timer *gtm_get_timer16(void)
8862306a36Sopenharmony_ci{
8962306a36Sopenharmony_ci	struct gtm *gtm;
9062306a36Sopenharmony_ci	int i;
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	list_for_each_entry(gtm, &gtms, list_node) {
9362306a36Sopenharmony_ci		spin_lock_irq(&gtm->lock);
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci		for (i = 0; i < ARRAY_SIZE(gtm->timers); i++) {
9662306a36Sopenharmony_ci			if (!gtm->timers[i].requested) {
9762306a36Sopenharmony_ci				gtm->timers[i].requested = true;
9862306a36Sopenharmony_ci				spin_unlock_irq(&gtm->lock);
9962306a36Sopenharmony_ci				return &gtm->timers[i];
10062306a36Sopenharmony_ci			}
10162306a36Sopenharmony_ci		}
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci		spin_unlock_irq(&gtm->lock);
10462306a36Sopenharmony_ci	}
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci	if (!list_empty(&gtms))
10762306a36Sopenharmony_ci		return ERR_PTR(-EBUSY);
10862306a36Sopenharmony_ci	return ERR_PTR(-ENODEV);
10962306a36Sopenharmony_ci}
11062306a36Sopenharmony_ciEXPORT_SYMBOL(gtm_get_timer16);
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci/**
11362306a36Sopenharmony_ci * gtm_get_specific_timer - request specific GTM timer
11462306a36Sopenharmony_ci * @gtm:	specific GTM, pass here GTM's device_node->data
11562306a36Sopenharmony_ci * @timer:	specific timer number, Timer1 is 0.
11662306a36Sopenharmony_ci * Context:	non-IRQ
11762306a36Sopenharmony_ci *
11862306a36Sopenharmony_ci * This function reserves GTM timer for later use. It returns gtm_timer
11962306a36Sopenharmony_ci * structure to use with the rest of GTM API, you should use timer->irq
12062306a36Sopenharmony_ci * to manage timer interrupt.
12162306a36Sopenharmony_ci */
12262306a36Sopenharmony_cistruct gtm_timer *gtm_get_specific_timer16(struct gtm *gtm,
12362306a36Sopenharmony_ci					   unsigned int timer)
12462306a36Sopenharmony_ci{
12562306a36Sopenharmony_ci	struct gtm_timer *ret = ERR_PTR(-EBUSY);
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci	if (timer > 3)
12862306a36Sopenharmony_ci		return ERR_PTR(-EINVAL);
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci	spin_lock_irq(&gtm->lock);
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci	if (gtm->timers[timer].requested)
13362306a36Sopenharmony_ci		goto out;
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci	ret = &gtm->timers[timer];
13662306a36Sopenharmony_ci	ret->requested = true;
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ciout:
13962306a36Sopenharmony_ci	spin_unlock_irq(&gtm->lock);
14062306a36Sopenharmony_ci	return ret;
14162306a36Sopenharmony_ci}
14262306a36Sopenharmony_ciEXPORT_SYMBOL(gtm_get_specific_timer16);
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci/**
14562306a36Sopenharmony_ci * gtm_put_timer16 - release 16 bits GTM timer
14662306a36Sopenharmony_ci * @tmr:	pointer to the gtm_timer structure obtained from gtm_get_timer
14762306a36Sopenharmony_ci * Context:	any
14862306a36Sopenharmony_ci *
14962306a36Sopenharmony_ci * This function releases GTM timer so others may request it.
15062306a36Sopenharmony_ci */
15162306a36Sopenharmony_civoid gtm_put_timer16(struct gtm_timer *tmr)
15262306a36Sopenharmony_ci{
15362306a36Sopenharmony_ci	gtm_stop_timer16(tmr);
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci	spin_lock_irq(&tmr->gtm->lock);
15662306a36Sopenharmony_ci	tmr->requested = false;
15762306a36Sopenharmony_ci	spin_unlock_irq(&tmr->gtm->lock);
15862306a36Sopenharmony_ci}
15962306a36Sopenharmony_ciEXPORT_SYMBOL(gtm_put_timer16);
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci/*
16262306a36Sopenharmony_ci * This is back-end for the exported functions, it's used to reset single
16362306a36Sopenharmony_ci * timer in reference mode.
16462306a36Sopenharmony_ci */
16562306a36Sopenharmony_cistatic int gtm_set_ref_timer16(struct gtm_timer *tmr, int frequency,
16662306a36Sopenharmony_ci			       int reference_value, bool free_run)
16762306a36Sopenharmony_ci{
16862306a36Sopenharmony_ci	struct gtm *gtm = tmr->gtm;
16962306a36Sopenharmony_ci	int num = tmr - &gtm->timers[0];
17062306a36Sopenharmony_ci	unsigned int prescaler;
17162306a36Sopenharmony_ci	u8 iclk = GTMDR_ICLK_ICLK;
17262306a36Sopenharmony_ci	u8 psr;
17362306a36Sopenharmony_ci	u8 sps;
17462306a36Sopenharmony_ci	unsigned long flags;
17562306a36Sopenharmony_ci	int max_prescaler = 256 * 256 * 16;
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci	/* CPM2 doesn't have primary prescaler */
17862306a36Sopenharmony_ci	if (!tmr->gtpsr)
17962306a36Sopenharmony_ci		max_prescaler /= 256;
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci	prescaler = gtm->clock / frequency;
18262306a36Sopenharmony_ci	/*
18362306a36Sopenharmony_ci	 * We have two 8 bit prescalers -- primary and secondary (psr, sps),
18462306a36Sopenharmony_ci	 * plus "slow go" mode (clk / 16). So, total prescale value is
18562306a36Sopenharmony_ci	 * 16 * (psr + 1) * (sps + 1). Though, for CPM2 GTMs we losing psr.
18662306a36Sopenharmony_ci	 */
18762306a36Sopenharmony_ci	if (prescaler > max_prescaler)
18862306a36Sopenharmony_ci		return -EINVAL;
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci	if (prescaler > max_prescaler / 16) {
19162306a36Sopenharmony_ci		iclk = GTMDR_ICLK_SLGO;
19262306a36Sopenharmony_ci		prescaler /= 16;
19362306a36Sopenharmony_ci	}
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ci	if (prescaler <= 256) {
19662306a36Sopenharmony_ci		psr = 0;
19762306a36Sopenharmony_ci		sps = prescaler - 1;
19862306a36Sopenharmony_ci	} else {
19962306a36Sopenharmony_ci		psr = 256 - 1;
20062306a36Sopenharmony_ci		sps = prescaler / 256 - 1;
20162306a36Sopenharmony_ci	}
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci	spin_lock_irqsave(&gtm->lock, flags);
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci	/*
20662306a36Sopenharmony_ci	 * Properly reset timers: stop, reset, set up prescalers, reference
20762306a36Sopenharmony_ci	 * value and clear event register.
20862306a36Sopenharmony_ci	 */
20962306a36Sopenharmony_ci	clrsetbits_8(tmr->gtcfr, ~(GTCFR_STP(num) | GTCFR_RST(num)),
21062306a36Sopenharmony_ci				 GTCFR_STP(num) | GTCFR_RST(num));
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ci	setbits8(tmr->gtcfr, GTCFR_STP(num));
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ci	if (tmr->gtpsr)
21562306a36Sopenharmony_ci		out_be16(tmr->gtpsr, psr);
21662306a36Sopenharmony_ci	clrsetbits_be16(tmr->gtmdr, 0xFFFF, iclk | GTMDR_SPS(sps) |
21762306a36Sopenharmony_ci			GTMDR_ORI | (free_run ? GTMDR_FRR : 0));
21862306a36Sopenharmony_ci	out_be16(tmr->gtcnr, 0);
21962306a36Sopenharmony_ci	out_be16(tmr->gtrfr, reference_value);
22062306a36Sopenharmony_ci	out_be16(tmr->gtevr, 0xFFFF);
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci	/* Let it be. */
22362306a36Sopenharmony_ci	clrbits8(tmr->gtcfr, GTCFR_STP(num));
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci	spin_unlock_irqrestore(&gtm->lock, flags);
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ci	return 0;
22862306a36Sopenharmony_ci}
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_ci/**
23162306a36Sopenharmony_ci * gtm_set_timer16 - (re)set 16 bit timer with arbitrary precision
23262306a36Sopenharmony_ci * @tmr:	pointer to the gtm_timer structure obtained from gtm_get_timer
23362306a36Sopenharmony_ci * @usec:	timer interval in microseconds
23462306a36Sopenharmony_ci * @reload:	if set, the timer will reset upon expiry rather than
23562306a36Sopenharmony_ci *         	continue running free.
23662306a36Sopenharmony_ci * Context:	any
23762306a36Sopenharmony_ci *
23862306a36Sopenharmony_ci * This function (re)sets the GTM timer so that it counts up to the requested
23962306a36Sopenharmony_ci * interval value, and fires the interrupt when the value is reached. This
24062306a36Sopenharmony_ci * function will reduce the precision of the timer as needed in order for the
24162306a36Sopenharmony_ci * requested timeout to fit in a 16-bit register.
24262306a36Sopenharmony_ci */
24362306a36Sopenharmony_ciint gtm_set_timer16(struct gtm_timer *tmr, unsigned long usec, bool reload)
24462306a36Sopenharmony_ci{
24562306a36Sopenharmony_ci	/* quite obvious, frequency which is enough for µSec precision */
24662306a36Sopenharmony_ci	int freq = 1000000;
24762306a36Sopenharmony_ci	unsigned int bit;
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci	bit = fls_long(usec);
25062306a36Sopenharmony_ci	if (bit > 15) {
25162306a36Sopenharmony_ci		freq >>= bit - 15;
25262306a36Sopenharmony_ci		usec >>= bit - 15;
25362306a36Sopenharmony_ci	}
25462306a36Sopenharmony_ci
25562306a36Sopenharmony_ci	if (!freq)
25662306a36Sopenharmony_ci		return -EINVAL;
25762306a36Sopenharmony_ci
25862306a36Sopenharmony_ci	return gtm_set_ref_timer16(tmr, freq, usec, reload);
25962306a36Sopenharmony_ci}
26062306a36Sopenharmony_ciEXPORT_SYMBOL(gtm_set_timer16);
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_ci/**
26362306a36Sopenharmony_ci * gtm_set_exact_utimer16 - (re)set 16 bits timer
26462306a36Sopenharmony_ci * @tmr:	pointer to the gtm_timer structure obtained from gtm_get_timer
26562306a36Sopenharmony_ci * @usec:	timer interval in microseconds
26662306a36Sopenharmony_ci * @reload:	if set, the timer will reset upon expiry rather than
26762306a36Sopenharmony_ci *         	continue running free.
26862306a36Sopenharmony_ci * Context:	any
26962306a36Sopenharmony_ci *
27062306a36Sopenharmony_ci * This function (re)sets GTM timer so that it counts up to the requested
27162306a36Sopenharmony_ci * interval value, and fires the interrupt when the value is reached. If reload
27262306a36Sopenharmony_ci * flag was set, timer will also reset itself upon reference value, otherwise
27362306a36Sopenharmony_ci * it continues to increment.
27462306a36Sopenharmony_ci *
27562306a36Sopenharmony_ci * The _exact_ bit in the function name states that this function will not
27662306a36Sopenharmony_ci * crop precision of the "usec" argument, thus usec is limited to 16 bits
27762306a36Sopenharmony_ci * (single timer width).
27862306a36Sopenharmony_ci */
27962306a36Sopenharmony_ciint gtm_set_exact_timer16(struct gtm_timer *tmr, u16 usec, bool reload)
28062306a36Sopenharmony_ci{
28162306a36Sopenharmony_ci	/* quite obvious, frequency which is enough for µSec precision */
28262306a36Sopenharmony_ci	const int freq = 1000000;
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ci	/*
28562306a36Sopenharmony_ci	 * We can lower the frequency (and probably power consumption) by
28662306a36Sopenharmony_ci	 * dividing both frequency and usec by 2 until there is no remainder.
28762306a36Sopenharmony_ci	 * But we won't bother with this unless savings are measured, so just
28862306a36Sopenharmony_ci	 * run the timer as is.
28962306a36Sopenharmony_ci	 */
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_ci	return gtm_set_ref_timer16(tmr, freq, usec, reload);
29262306a36Sopenharmony_ci}
29362306a36Sopenharmony_ciEXPORT_SYMBOL(gtm_set_exact_timer16);
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ci/**
29662306a36Sopenharmony_ci * gtm_stop_timer16 - stop single timer
29762306a36Sopenharmony_ci * @tmr:	pointer to the gtm_timer structure obtained from gtm_get_timer
29862306a36Sopenharmony_ci * Context:	any
29962306a36Sopenharmony_ci *
30062306a36Sopenharmony_ci * This function simply stops the GTM timer.
30162306a36Sopenharmony_ci */
30262306a36Sopenharmony_civoid gtm_stop_timer16(struct gtm_timer *tmr)
30362306a36Sopenharmony_ci{
30462306a36Sopenharmony_ci	struct gtm *gtm = tmr->gtm;
30562306a36Sopenharmony_ci	int num = tmr - &gtm->timers[0];
30662306a36Sopenharmony_ci	unsigned long flags;
30762306a36Sopenharmony_ci
30862306a36Sopenharmony_ci	spin_lock_irqsave(&gtm->lock, flags);
30962306a36Sopenharmony_ci
31062306a36Sopenharmony_ci	setbits8(tmr->gtcfr, GTCFR_STP(num));
31162306a36Sopenharmony_ci	out_be16(tmr->gtevr, 0xFFFF);
31262306a36Sopenharmony_ci
31362306a36Sopenharmony_ci	spin_unlock_irqrestore(&gtm->lock, flags);
31462306a36Sopenharmony_ci}
31562306a36Sopenharmony_ciEXPORT_SYMBOL(gtm_stop_timer16);
31662306a36Sopenharmony_ci
31762306a36Sopenharmony_ci/**
31862306a36Sopenharmony_ci * gtm_ack_timer16 - acknowledge timer event (free-run timers only)
31962306a36Sopenharmony_ci * @tmr:	pointer to the gtm_timer structure obtained from gtm_get_timer
32062306a36Sopenharmony_ci * @events:	events mask to ack
32162306a36Sopenharmony_ci * Context:	any
32262306a36Sopenharmony_ci *
32362306a36Sopenharmony_ci * Thus function used to acknowledge timer interrupt event, use it inside the
32462306a36Sopenharmony_ci * interrupt handler.
32562306a36Sopenharmony_ci */
32662306a36Sopenharmony_civoid gtm_ack_timer16(struct gtm_timer *tmr, u16 events)
32762306a36Sopenharmony_ci{
32862306a36Sopenharmony_ci	out_be16(tmr->gtevr, events);
32962306a36Sopenharmony_ci}
33062306a36Sopenharmony_ciEXPORT_SYMBOL(gtm_ack_timer16);
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_cistatic void __init gtm_set_shortcuts(struct device_node *np,
33362306a36Sopenharmony_ci				     struct gtm_timer *timers,
33462306a36Sopenharmony_ci				     struct gtm_timers_regs __iomem *regs)
33562306a36Sopenharmony_ci{
33662306a36Sopenharmony_ci	/*
33762306a36Sopenharmony_ci	 * Yeah, I don't like this either, but timers' registers a bit messed,
33862306a36Sopenharmony_ci	 * so we have to provide shortcuts to write timer independent code.
33962306a36Sopenharmony_ci	 * Alternative option is to create gt*() accessors, but that will be
34062306a36Sopenharmony_ci	 * even uglier and cryptic.
34162306a36Sopenharmony_ci	 */
34262306a36Sopenharmony_ci	timers[0].gtcfr = &regs->gtcfr1;
34362306a36Sopenharmony_ci	timers[0].gtmdr = &regs->gtmdr1;
34462306a36Sopenharmony_ci	timers[0].gtcnr = &regs->gtcnr1;
34562306a36Sopenharmony_ci	timers[0].gtrfr = &regs->gtrfr1;
34662306a36Sopenharmony_ci	timers[0].gtevr = &regs->gtevr1;
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_ci	timers[1].gtcfr = &regs->gtcfr1;
34962306a36Sopenharmony_ci	timers[1].gtmdr = &regs->gtmdr2;
35062306a36Sopenharmony_ci	timers[1].gtcnr = &regs->gtcnr2;
35162306a36Sopenharmony_ci	timers[1].gtrfr = &regs->gtrfr2;
35262306a36Sopenharmony_ci	timers[1].gtevr = &regs->gtevr2;
35362306a36Sopenharmony_ci
35462306a36Sopenharmony_ci	timers[2].gtcfr = &regs->gtcfr2;
35562306a36Sopenharmony_ci	timers[2].gtmdr = &regs->gtmdr3;
35662306a36Sopenharmony_ci	timers[2].gtcnr = &regs->gtcnr3;
35762306a36Sopenharmony_ci	timers[2].gtrfr = &regs->gtrfr3;
35862306a36Sopenharmony_ci	timers[2].gtevr = &regs->gtevr3;
35962306a36Sopenharmony_ci
36062306a36Sopenharmony_ci	timers[3].gtcfr = &regs->gtcfr2;
36162306a36Sopenharmony_ci	timers[3].gtmdr = &regs->gtmdr4;
36262306a36Sopenharmony_ci	timers[3].gtcnr = &regs->gtcnr4;
36362306a36Sopenharmony_ci	timers[3].gtrfr = &regs->gtrfr4;
36462306a36Sopenharmony_ci	timers[3].gtevr = &regs->gtevr4;
36562306a36Sopenharmony_ci
36662306a36Sopenharmony_ci	/* CPM2 doesn't have primary prescaler */
36762306a36Sopenharmony_ci	if (!of_device_is_compatible(np, "fsl,cpm2-gtm")) {
36862306a36Sopenharmony_ci		timers[0].gtpsr = &regs->gtpsr1;
36962306a36Sopenharmony_ci		timers[1].gtpsr = &regs->gtpsr2;
37062306a36Sopenharmony_ci		timers[2].gtpsr = &regs->gtpsr3;
37162306a36Sopenharmony_ci		timers[3].gtpsr = &regs->gtpsr4;
37262306a36Sopenharmony_ci	}
37362306a36Sopenharmony_ci}
37462306a36Sopenharmony_ci
37562306a36Sopenharmony_cistatic int __init fsl_gtm_init(void)
37662306a36Sopenharmony_ci{
37762306a36Sopenharmony_ci	struct device_node *np;
37862306a36Sopenharmony_ci
37962306a36Sopenharmony_ci	for_each_compatible_node(np, NULL, "fsl,gtm") {
38062306a36Sopenharmony_ci		int i;
38162306a36Sopenharmony_ci		struct gtm *gtm;
38262306a36Sopenharmony_ci		const u32 *clock;
38362306a36Sopenharmony_ci		int size;
38462306a36Sopenharmony_ci
38562306a36Sopenharmony_ci		gtm = kzalloc(sizeof(*gtm), GFP_KERNEL);
38662306a36Sopenharmony_ci		if (!gtm) {
38762306a36Sopenharmony_ci			pr_err("%pOF: unable to allocate memory\n",
38862306a36Sopenharmony_ci				np);
38962306a36Sopenharmony_ci			continue;
39062306a36Sopenharmony_ci		}
39162306a36Sopenharmony_ci
39262306a36Sopenharmony_ci		spin_lock_init(&gtm->lock);
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_ci		clock = of_get_property(np, "clock-frequency", &size);
39562306a36Sopenharmony_ci		if (!clock || size != sizeof(*clock)) {
39662306a36Sopenharmony_ci			pr_err("%pOF: no clock-frequency\n", np);
39762306a36Sopenharmony_ci			goto err;
39862306a36Sopenharmony_ci		}
39962306a36Sopenharmony_ci		gtm->clock = *clock;
40062306a36Sopenharmony_ci
40162306a36Sopenharmony_ci		for (i = 0; i < ARRAY_SIZE(gtm->timers); i++) {
40262306a36Sopenharmony_ci			unsigned int irq;
40362306a36Sopenharmony_ci
40462306a36Sopenharmony_ci			irq = irq_of_parse_and_map(np, i);
40562306a36Sopenharmony_ci			if (!irq) {
40662306a36Sopenharmony_ci				pr_err("%pOF: not enough interrupts specified\n",
40762306a36Sopenharmony_ci				       np);
40862306a36Sopenharmony_ci				goto err;
40962306a36Sopenharmony_ci			}
41062306a36Sopenharmony_ci			gtm->timers[i].irq = irq;
41162306a36Sopenharmony_ci			gtm->timers[i].gtm = gtm;
41262306a36Sopenharmony_ci		}
41362306a36Sopenharmony_ci
41462306a36Sopenharmony_ci		gtm->regs = of_iomap(np, 0);
41562306a36Sopenharmony_ci		if (!gtm->regs) {
41662306a36Sopenharmony_ci			pr_err("%pOF: unable to iomap registers\n",
41762306a36Sopenharmony_ci			       np);
41862306a36Sopenharmony_ci			goto err;
41962306a36Sopenharmony_ci		}
42062306a36Sopenharmony_ci
42162306a36Sopenharmony_ci		gtm_set_shortcuts(np, gtm->timers, gtm->regs);
42262306a36Sopenharmony_ci		list_add(&gtm->list_node, &gtms);
42362306a36Sopenharmony_ci
42462306a36Sopenharmony_ci		/* We don't want to lose the node and its ->data */
42562306a36Sopenharmony_ci		np->data = gtm;
42662306a36Sopenharmony_ci		of_node_get(np);
42762306a36Sopenharmony_ci
42862306a36Sopenharmony_ci		continue;
42962306a36Sopenharmony_cierr:
43062306a36Sopenharmony_ci		kfree(gtm);
43162306a36Sopenharmony_ci	}
43262306a36Sopenharmony_ci	return 0;
43362306a36Sopenharmony_ci}
43462306a36Sopenharmony_ciarch_initcall(fsl_gtm_init);
435