xref: /linux/arch/powerpc/sysdev/fsl_gtm.c (revision ef24ba7091517d2bbf9ba2cb4256c0dccd51d248)
183ff9dcfSAnton Vorontsov /*
283ff9dcfSAnton Vorontsov  * Freescale General-purpose Timers Module
383ff9dcfSAnton Vorontsov  *
48a56e1eeSYang Li  * Copyright (c) Freescale Semiconductor, Inc. 2006.
583ff9dcfSAnton Vorontsov  *               Shlomi Gridish <gridish@freescale.com>
683ff9dcfSAnton Vorontsov  *               Jerry Huang <Chang-Ming.Huang@freescale.com>
783ff9dcfSAnton Vorontsov  * Copyright (c) MontaVista Software, Inc. 2008.
883ff9dcfSAnton Vorontsov  *               Anton Vorontsov <avorontsov@ru.mvista.com>
983ff9dcfSAnton Vorontsov  *
1083ff9dcfSAnton Vorontsov  * This program is free software; you can redistribute  it and/or modify it
1183ff9dcfSAnton Vorontsov  * under  the terms of  the GNU General  Public License as published by the
1283ff9dcfSAnton Vorontsov  * Free Software Foundation;  either version 2 of the  License, or (at your
1383ff9dcfSAnton Vorontsov  * option) any later version.
1483ff9dcfSAnton Vorontsov  */
1583ff9dcfSAnton Vorontsov 
1683ff9dcfSAnton Vorontsov #include <linux/kernel.h>
1722ae782fSGrant Likely #include <linux/err.h>
1883ff9dcfSAnton Vorontsov #include <linux/errno.h>
1983ff9dcfSAnton Vorontsov #include <linux/list.h>
2083ff9dcfSAnton Vorontsov #include <linux/io.h>
2183ff9dcfSAnton Vorontsov #include <linux/of.h>
2226a2056eSRob Herring #include <linux/of_address.h>
2326a2056eSRob Herring #include <linux/of_irq.h>
2483ff9dcfSAnton Vorontsov #include <linux/spinlock.h>
2583ff9dcfSAnton Vorontsov #include <linux/bitops.h>
265a0e3ad6STejun Heo #include <linux/slab.h>
2766b15db6SPaul Gortmaker #include <linux/export.h>
2883ff9dcfSAnton Vorontsov #include <asm/fsl_gtm.h>
2983ff9dcfSAnton Vorontsov 
3083ff9dcfSAnton Vorontsov #define GTCFR_STP(x)		((x) & 1 ? 1 << 5 : 1 << 1)
3183ff9dcfSAnton Vorontsov #define GTCFR_RST(x)		((x) & 1 ? 1 << 4 : 1 << 0)
3283ff9dcfSAnton Vorontsov 
3383ff9dcfSAnton Vorontsov #define GTMDR_ICLK_MASK		(3 << 1)
3483ff9dcfSAnton Vorontsov #define GTMDR_ICLK_ICAS		(0 << 1)
3583ff9dcfSAnton Vorontsov #define GTMDR_ICLK_ICLK		(1 << 1)
3683ff9dcfSAnton Vorontsov #define GTMDR_ICLK_SLGO		(2 << 1)
3783ff9dcfSAnton Vorontsov #define GTMDR_FRR		(1 << 3)
3883ff9dcfSAnton Vorontsov #define GTMDR_ORI		(1 << 4)
3983ff9dcfSAnton Vorontsov #define GTMDR_SPS(x)		((x) << 8)
4083ff9dcfSAnton Vorontsov 
4183ff9dcfSAnton Vorontsov struct gtm_timers_regs {
4283ff9dcfSAnton Vorontsov 	u8	gtcfr1;		/* Timer 1, Timer 2 global config register */
4383ff9dcfSAnton Vorontsov 	u8	res0[0x3];
4483ff9dcfSAnton Vorontsov 	u8	gtcfr2;		/* Timer 3, timer 4 global config register */
4583ff9dcfSAnton Vorontsov 	u8	res1[0xB];
4683ff9dcfSAnton Vorontsov 	__be16	gtmdr1;		/* Timer 1 mode register */
4783ff9dcfSAnton Vorontsov 	__be16	gtmdr2;		/* Timer 2 mode register */
4883ff9dcfSAnton Vorontsov 	__be16	gtrfr1;		/* Timer 1 reference register */
4983ff9dcfSAnton Vorontsov 	__be16	gtrfr2;		/* Timer 2 reference register */
5083ff9dcfSAnton Vorontsov 	__be16	gtcpr1;		/* Timer 1 capture register */
5183ff9dcfSAnton Vorontsov 	__be16	gtcpr2;		/* Timer 2 capture register */
5283ff9dcfSAnton Vorontsov 	__be16	gtcnr1;		/* Timer 1 counter */
5383ff9dcfSAnton Vorontsov 	__be16	gtcnr2;		/* Timer 2 counter */
5483ff9dcfSAnton Vorontsov 	__be16	gtmdr3;		/* Timer 3 mode register */
5583ff9dcfSAnton Vorontsov 	__be16	gtmdr4;		/* Timer 4 mode register */
5683ff9dcfSAnton Vorontsov 	__be16	gtrfr3;		/* Timer 3 reference register */
5783ff9dcfSAnton Vorontsov 	__be16	gtrfr4;		/* Timer 4 reference register */
5883ff9dcfSAnton Vorontsov 	__be16	gtcpr3;		/* Timer 3 capture register */
5983ff9dcfSAnton Vorontsov 	__be16	gtcpr4;		/* Timer 4 capture register */
6083ff9dcfSAnton Vorontsov 	__be16	gtcnr3;		/* Timer 3 counter */
6183ff9dcfSAnton Vorontsov 	__be16	gtcnr4;		/* Timer 4 counter */
6283ff9dcfSAnton Vorontsov 	__be16	gtevr1;		/* Timer 1 event register */
6383ff9dcfSAnton Vorontsov 	__be16	gtevr2;		/* Timer 2 event register */
6483ff9dcfSAnton Vorontsov 	__be16	gtevr3;		/* Timer 3 event register */
6583ff9dcfSAnton Vorontsov 	__be16	gtevr4;		/* Timer 4 event register */
6683ff9dcfSAnton Vorontsov 	__be16	gtpsr1;		/* Timer 1 prescale register */
6783ff9dcfSAnton Vorontsov 	__be16	gtpsr2;		/* Timer 2 prescale register */
6883ff9dcfSAnton Vorontsov 	__be16	gtpsr3;		/* Timer 3 prescale register */
6983ff9dcfSAnton Vorontsov 	__be16	gtpsr4;		/* Timer 4 prescale register */
7083ff9dcfSAnton Vorontsov 	u8 res2[0x40];
7183ff9dcfSAnton Vorontsov } __attribute__ ((packed));
7283ff9dcfSAnton Vorontsov 
7383ff9dcfSAnton Vorontsov struct gtm {
7483ff9dcfSAnton Vorontsov 	unsigned int clock;
7583ff9dcfSAnton Vorontsov 	struct gtm_timers_regs __iomem *regs;
7683ff9dcfSAnton Vorontsov 	struct gtm_timer timers[4];
7783ff9dcfSAnton Vorontsov 	spinlock_t lock;
7883ff9dcfSAnton Vorontsov 	struct list_head list_node;
7983ff9dcfSAnton Vorontsov };
8083ff9dcfSAnton Vorontsov 
8183ff9dcfSAnton Vorontsov static LIST_HEAD(gtms);
8283ff9dcfSAnton Vorontsov 
8383ff9dcfSAnton Vorontsov /**
8483ff9dcfSAnton Vorontsov  * gtm_get_timer - request GTM timer to use it with the rest of GTM API
8583ff9dcfSAnton Vorontsov  * Context:	non-IRQ
8683ff9dcfSAnton Vorontsov  *
8783ff9dcfSAnton Vorontsov  * This function reserves GTM timer for later use. It returns gtm_timer
8883ff9dcfSAnton Vorontsov  * structure to use with the rest of GTM API, you should use timer->irq
8983ff9dcfSAnton Vorontsov  * to manage timer interrupt.
9083ff9dcfSAnton Vorontsov  */
9183ff9dcfSAnton Vorontsov struct gtm_timer *gtm_get_timer16(void)
9283ff9dcfSAnton Vorontsov {
9383ff9dcfSAnton Vorontsov 	struct gtm *gtm = NULL;
9483ff9dcfSAnton Vorontsov 	int i;
9583ff9dcfSAnton Vorontsov 
9683ff9dcfSAnton Vorontsov 	list_for_each_entry(gtm, &gtms, list_node) {
9783ff9dcfSAnton Vorontsov 		spin_lock_irq(&gtm->lock);
9883ff9dcfSAnton Vorontsov 
9983ff9dcfSAnton Vorontsov 		for (i = 0; i < ARRAY_SIZE(gtm->timers); i++) {
10083ff9dcfSAnton Vorontsov 			if (!gtm->timers[i].requested) {
10183ff9dcfSAnton Vorontsov 				gtm->timers[i].requested = true;
10283ff9dcfSAnton Vorontsov 				spin_unlock_irq(&gtm->lock);
10383ff9dcfSAnton Vorontsov 				return &gtm->timers[i];
10483ff9dcfSAnton Vorontsov 			}
10583ff9dcfSAnton Vorontsov 		}
10683ff9dcfSAnton Vorontsov 
10783ff9dcfSAnton Vorontsov 		spin_unlock_irq(&gtm->lock);
10883ff9dcfSAnton Vorontsov 	}
10983ff9dcfSAnton Vorontsov 
11083ff9dcfSAnton Vorontsov 	if (gtm)
11183ff9dcfSAnton Vorontsov 		return ERR_PTR(-EBUSY);
11283ff9dcfSAnton Vorontsov 	return ERR_PTR(-ENODEV);
11383ff9dcfSAnton Vorontsov }
11483ff9dcfSAnton Vorontsov EXPORT_SYMBOL(gtm_get_timer16);
11583ff9dcfSAnton Vorontsov 
11683ff9dcfSAnton Vorontsov /**
11783ff9dcfSAnton Vorontsov  * gtm_get_specific_timer - request specific GTM timer
11883ff9dcfSAnton Vorontsov  * @gtm:	specific GTM, pass here GTM's device_node->data
11983ff9dcfSAnton Vorontsov  * @timer:	specific timer number, Timer1 is 0.
12083ff9dcfSAnton Vorontsov  * Context:	non-IRQ
12183ff9dcfSAnton Vorontsov  *
12283ff9dcfSAnton Vorontsov  * This function reserves GTM timer for later use. It returns gtm_timer
12383ff9dcfSAnton Vorontsov  * structure to use with the rest of GTM API, you should use timer->irq
12483ff9dcfSAnton Vorontsov  * to manage timer interrupt.
12583ff9dcfSAnton Vorontsov  */
12683ff9dcfSAnton Vorontsov struct gtm_timer *gtm_get_specific_timer16(struct gtm *gtm,
12783ff9dcfSAnton Vorontsov 					   unsigned int timer)
12883ff9dcfSAnton Vorontsov {
12983ff9dcfSAnton Vorontsov 	struct gtm_timer *ret = ERR_PTR(-EBUSY);
13083ff9dcfSAnton Vorontsov 
13183ff9dcfSAnton Vorontsov 	if (timer > 3)
13283ff9dcfSAnton Vorontsov 		return ERR_PTR(-EINVAL);
13383ff9dcfSAnton Vorontsov 
13483ff9dcfSAnton Vorontsov 	spin_lock_irq(&gtm->lock);
13583ff9dcfSAnton Vorontsov 
13683ff9dcfSAnton Vorontsov 	if (gtm->timers[timer].requested)
13783ff9dcfSAnton Vorontsov 		goto out;
13883ff9dcfSAnton Vorontsov 
13983ff9dcfSAnton Vorontsov 	ret = &gtm->timers[timer];
14083ff9dcfSAnton Vorontsov 	ret->requested = true;
14183ff9dcfSAnton Vorontsov 
14283ff9dcfSAnton Vorontsov out:
14383ff9dcfSAnton Vorontsov 	spin_unlock_irq(&gtm->lock);
14483ff9dcfSAnton Vorontsov 	return ret;
14583ff9dcfSAnton Vorontsov }
14683ff9dcfSAnton Vorontsov EXPORT_SYMBOL(gtm_get_specific_timer16);
14783ff9dcfSAnton Vorontsov 
14883ff9dcfSAnton Vorontsov /**
14983ff9dcfSAnton Vorontsov  * gtm_put_timer16 - release 16 bits GTM timer
15083ff9dcfSAnton Vorontsov  * @tmr:	pointer to the gtm_timer structure obtained from gtm_get_timer
15183ff9dcfSAnton Vorontsov  * Context:	any
15283ff9dcfSAnton Vorontsov  *
15383ff9dcfSAnton Vorontsov  * This function releases GTM timer so others may request it.
15483ff9dcfSAnton Vorontsov  */
15583ff9dcfSAnton Vorontsov void gtm_put_timer16(struct gtm_timer *tmr)
15683ff9dcfSAnton Vorontsov {
15783ff9dcfSAnton Vorontsov 	gtm_stop_timer16(tmr);
15883ff9dcfSAnton Vorontsov 
15983ff9dcfSAnton Vorontsov 	spin_lock_irq(&tmr->gtm->lock);
16083ff9dcfSAnton Vorontsov 	tmr->requested = false;
16183ff9dcfSAnton Vorontsov 	spin_unlock_irq(&tmr->gtm->lock);
16283ff9dcfSAnton Vorontsov }
16383ff9dcfSAnton Vorontsov EXPORT_SYMBOL(gtm_put_timer16);
16483ff9dcfSAnton Vorontsov 
16583ff9dcfSAnton Vorontsov /*
16683ff9dcfSAnton Vorontsov  * This is back-end for the exported functions, it's used to reset single
16783ff9dcfSAnton Vorontsov  * timer in reference mode.
16883ff9dcfSAnton Vorontsov  */
16983ff9dcfSAnton Vorontsov static int gtm_set_ref_timer16(struct gtm_timer *tmr, int frequency,
17083ff9dcfSAnton Vorontsov 			       int reference_value, bool free_run)
17183ff9dcfSAnton Vorontsov {
17283ff9dcfSAnton Vorontsov 	struct gtm *gtm = tmr->gtm;
17383ff9dcfSAnton Vorontsov 	int num = tmr - &gtm->timers[0];
17483ff9dcfSAnton Vorontsov 	unsigned int prescaler;
17583ff9dcfSAnton Vorontsov 	u8 iclk = GTMDR_ICLK_ICLK;
17683ff9dcfSAnton Vorontsov 	u8 psr;
17783ff9dcfSAnton Vorontsov 	u8 sps;
17883ff9dcfSAnton Vorontsov 	unsigned long flags;
17983ff9dcfSAnton Vorontsov 	int max_prescaler = 256 * 256 * 16;
18083ff9dcfSAnton Vorontsov 
18183ff9dcfSAnton Vorontsov 	/* CPM2 doesn't have primary prescaler */
18283ff9dcfSAnton Vorontsov 	if (!tmr->gtpsr)
18383ff9dcfSAnton Vorontsov 		max_prescaler /= 256;
18483ff9dcfSAnton Vorontsov 
18583ff9dcfSAnton Vorontsov 	prescaler = gtm->clock / frequency;
18683ff9dcfSAnton Vorontsov 	/*
18783ff9dcfSAnton Vorontsov 	 * We have two 8 bit prescalers -- primary and secondary (psr, sps),
18883ff9dcfSAnton Vorontsov 	 * plus "slow go" mode (clk / 16). So, total prescale value is
18983ff9dcfSAnton Vorontsov 	 * 16 * (psr + 1) * (sps + 1). Though, for CPM2 GTMs we losing psr.
19083ff9dcfSAnton Vorontsov 	 */
19183ff9dcfSAnton Vorontsov 	if (prescaler > max_prescaler)
19283ff9dcfSAnton Vorontsov 		return -EINVAL;
19383ff9dcfSAnton Vorontsov 
19483ff9dcfSAnton Vorontsov 	if (prescaler > max_prescaler / 16) {
19583ff9dcfSAnton Vorontsov 		iclk = GTMDR_ICLK_SLGO;
19683ff9dcfSAnton Vorontsov 		prescaler /= 16;
19783ff9dcfSAnton Vorontsov 	}
19883ff9dcfSAnton Vorontsov 
19983ff9dcfSAnton Vorontsov 	if (prescaler <= 256) {
20083ff9dcfSAnton Vorontsov 		psr = 0;
20183ff9dcfSAnton Vorontsov 		sps = prescaler - 1;
20283ff9dcfSAnton Vorontsov 	} else {
20383ff9dcfSAnton Vorontsov 		psr = 256 - 1;
20483ff9dcfSAnton Vorontsov 		sps = prescaler / 256 - 1;
20583ff9dcfSAnton Vorontsov 	}
20683ff9dcfSAnton Vorontsov 
20783ff9dcfSAnton Vorontsov 	spin_lock_irqsave(&gtm->lock, flags);
20883ff9dcfSAnton Vorontsov 
20983ff9dcfSAnton Vorontsov 	/*
21083ff9dcfSAnton Vorontsov 	 * Properly reset timers: stop, reset, set up prescalers, reference
21183ff9dcfSAnton Vorontsov 	 * value and clear event register.
21283ff9dcfSAnton Vorontsov 	 */
21383ff9dcfSAnton Vorontsov 	clrsetbits_8(tmr->gtcfr, ~(GTCFR_STP(num) | GTCFR_RST(num)),
21483ff9dcfSAnton Vorontsov 				 GTCFR_STP(num) | GTCFR_RST(num));
21583ff9dcfSAnton Vorontsov 
21683ff9dcfSAnton Vorontsov 	setbits8(tmr->gtcfr, GTCFR_STP(num));
21783ff9dcfSAnton Vorontsov 
21883ff9dcfSAnton Vorontsov 	if (tmr->gtpsr)
21983ff9dcfSAnton Vorontsov 		out_be16(tmr->gtpsr, psr);
22083ff9dcfSAnton Vorontsov 	clrsetbits_be16(tmr->gtmdr, 0xFFFF, iclk | GTMDR_SPS(sps) |
22183ff9dcfSAnton Vorontsov 			GTMDR_ORI | (free_run ? GTMDR_FRR : 0));
22283ff9dcfSAnton Vorontsov 	out_be16(tmr->gtcnr, 0);
22383ff9dcfSAnton Vorontsov 	out_be16(tmr->gtrfr, reference_value);
22483ff9dcfSAnton Vorontsov 	out_be16(tmr->gtevr, 0xFFFF);
22583ff9dcfSAnton Vorontsov 
22683ff9dcfSAnton Vorontsov 	/* Let it be. */
22783ff9dcfSAnton Vorontsov 	clrbits8(tmr->gtcfr, GTCFR_STP(num));
22883ff9dcfSAnton Vorontsov 
22983ff9dcfSAnton Vorontsov 	spin_unlock_irqrestore(&gtm->lock, flags);
23083ff9dcfSAnton Vorontsov 
23183ff9dcfSAnton Vorontsov 	return 0;
23283ff9dcfSAnton Vorontsov }
23383ff9dcfSAnton Vorontsov 
23483ff9dcfSAnton Vorontsov /**
23583ff9dcfSAnton Vorontsov  * gtm_set_timer16 - (re)set 16 bit timer with arbitrary precision
23683ff9dcfSAnton Vorontsov  * @tmr:	pointer to the gtm_timer structure obtained from gtm_get_timer
23783ff9dcfSAnton Vorontsov  * @usec:	timer interval in microseconds
23883ff9dcfSAnton Vorontsov  * @reload:	if set, the timer will reset upon expiry rather than
23983ff9dcfSAnton Vorontsov  *         	continue running free.
24083ff9dcfSAnton Vorontsov  * Context:	any
24183ff9dcfSAnton Vorontsov  *
24283ff9dcfSAnton Vorontsov  * This function (re)sets the GTM timer so that it counts up to the requested
24383ff9dcfSAnton Vorontsov  * interval value, and fires the interrupt when the value is reached. This
24483ff9dcfSAnton Vorontsov  * function will reduce the precision of the timer as needed in order for the
24583ff9dcfSAnton Vorontsov  * requested timeout to fit in a 16-bit register.
24683ff9dcfSAnton Vorontsov  */
24783ff9dcfSAnton Vorontsov int gtm_set_timer16(struct gtm_timer *tmr, unsigned long usec, bool reload)
24883ff9dcfSAnton Vorontsov {
24983ff9dcfSAnton Vorontsov 	/* quite obvious, frequency which is enough for µSec precision */
25083ff9dcfSAnton Vorontsov 	int freq = 1000000;
25183ff9dcfSAnton Vorontsov 	unsigned int bit;
25283ff9dcfSAnton Vorontsov 
25383ff9dcfSAnton Vorontsov 	bit = fls_long(usec);
25483ff9dcfSAnton Vorontsov 	if (bit > 15) {
25583ff9dcfSAnton Vorontsov 		freq >>= bit - 15;
25683ff9dcfSAnton Vorontsov 		usec >>= bit - 15;
25783ff9dcfSAnton Vorontsov 	}
25883ff9dcfSAnton Vorontsov 
25983ff9dcfSAnton Vorontsov 	if (!freq)
26083ff9dcfSAnton Vorontsov 		return -EINVAL;
26183ff9dcfSAnton Vorontsov 
26283ff9dcfSAnton Vorontsov 	return gtm_set_ref_timer16(tmr, freq, usec, reload);
26383ff9dcfSAnton Vorontsov }
26483ff9dcfSAnton Vorontsov EXPORT_SYMBOL(gtm_set_timer16);
26583ff9dcfSAnton Vorontsov 
26683ff9dcfSAnton Vorontsov /**
26783ff9dcfSAnton Vorontsov  * gtm_set_exact_utimer16 - (re)set 16 bits timer
26883ff9dcfSAnton Vorontsov  * @tmr:	pointer to the gtm_timer structure obtained from gtm_get_timer
26983ff9dcfSAnton Vorontsov  * @usec:	timer interval in microseconds
27083ff9dcfSAnton Vorontsov  * @reload:	if set, the timer will reset upon expiry rather than
27183ff9dcfSAnton Vorontsov  *         	continue running free.
27283ff9dcfSAnton Vorontsov  * Context:	any
27383ff9dcfSAnton Vorontsov  *
27483ff9dcfSAnton Vorontsov  * This function (re)sets GTM timer so that it counts up to the requested
27583ff9dcfSAnton Vorontsov  * interval value, and fires the interrupt when the value is reached. If reload
27683ff9dcfSAnton Vorontsov  * flag was set, timer will also reset itself upon reference value, otherwise
27783ff9dcfSAnton Vorontsov  * it continues to increment.
27883ff9dcfSAnton Vorontsov  *
27983ff9dcfSAnton Vorontsov  * The _exact_ bit in the function name states that this function will not
28083ff9dcfSAnton Vorontsov  * crop precision of the "usec" argument, thus usec is limited to 16 bits
28183ff9dcfSAnton Vorontsov  * (single timer width).
28283ff9dcfSAnton Vorontsov  */
28383ff9dcfSAnton Vorontsov int gtm_set_exact_timer16(struct gtm_timer *tmr, u16 usec, bool reload)
28483ff9dcfSAnton Vorontsov {
28583ff9dcfSAnton Vorontsov 	/* quite obvious, frequency which is enough for µSec precision */
28683ff9dcfSAnton Vorontsov 	const int freq = 1000000;
28783ff9dcfSAnton Vorontsov 
28883ff9dcfSAnton Vorontsov 	/*
28983ff9dcfSAnton Vorontsov 	 * We can lower the frequency (and probably power consumption) by
29083ff9dcfSAnton Vorontsov 	 * dividing both frequency and usec by 2 until there is no remainder.
29183ff9dcfSAnton Vorontsov 	 * But we won't bother with this unless savings are measured, so just
29283ff9dcfSAnton Vorontsov 	 * run the timer as is.
29383ff9dcfSAnton Vorontsov 	 */
29483ff9dcfSAnton Vorontsov 
29583ff9dcfSAnton Vorontsov 	return gtm_set_ref_timer16(tmr, freq, usec, reload);
29683ff9dcfSAnton Vorontsov }
29783ff9dcfSAnton Vorontsov EXPORT_SYMBOL(gtm_set_exact_timer16);
29883ff9dcfSAnton Vorontsov 
29983ff9dcfSAnton Vorontsov /**
30083ff9dcfSAnton Vorontsov  * gtm_stop_timer16 - stop single timer
30183ff9dcfSAnton Vorontsov  * @tmr:	pointer to the gtm_timer structure obtained from gtm_get_timer
30283ff9dcfSAnton Vorontsov  * Context:	any
30383ff9dcfSAnton Vorontsov  *
30483ff9dcfSAnton Vorontsov  * This function simply stops the GTM timer.
30583ff9dcfSAnton Vorontsov  */
30683ff9dcfSAnton Vorontsov void gtm_stop_timer16(struct gtm_timer *tmr)
30783ff9dcfSAnton Vorontsov {
30883ff9dcfSAnton Vorontsov 	struct gtm *gtm = tmr->gtm;
30983ff9dcfSAnton Vorontsov 	int num = tmr - &gtm->timers[0];
31083ff9dcfSAnton Vorontsov 	unsigned long flags;
31183ff9dcfSAnton Vorontsov 
31283ff9dcfSAnton Vorontsov 	spin_lock_irqsave(&gtm->lock, flags);
31383ff9dcfSAnton Vorontsov 
31483ff9dcfSAnton Vorontsov 	setbits8(tmr->gtcfr, GTCFR_STP(num));
31583ff9dcfSAnton Vorontsov 	out_be16(tmr->gtevr, 0xFFFF);
31683ff9dcfSAnton Vorontsov 
31783ff9dcfSAnton Vorontsov 	spin_unlock_irqrestore(&gtm->lock, flags);
31883ff9dcfSAnton Vorontsov }
31983ff9dcfSAnton Vorontsov EXPORT_SYMBOL(gtm_stop_timer16);
32083ff9dcfSAnton Vorontsov 
32183ff9dcfSAnton Vorontsov /**
32283ff9dcfSAnton Vorontsov  * gtm_ack_timer16 - acknowledge timer event (free-run timers only)
32383ff9dcfSAnton Vorontsov  * @tmr:	pointer to the gtm_timer structure obtained from gtm_get_timer
32483ff9dcfSAnton Vorontsov  * @events:	events mask to ack
32583ff9dcfSAnton Vorontsov  * Context:	any
32683ff9dcfSAnton Vorontsov  *
32783ff9dcfSAnton Vorontsov  * Thus function used to acknowledge timer interrupt event, use it inside the
32883ff9dcfSAnton Vorontsov  * interrupt handler.
32983ff9dcfSAnton Vorontsov  */
33083ff9dcfSAnton Vorontsov void gtm_ack_timer16(struct gtm_timer *tmr, u16 events)
33183ff9dcfSAnton Vorontsov {
33283ff9dcfSAnton Vorontsov 	out_be16(tmr->gtevr, events);
33383ff9dcfSAnton Vorontsov }
33483ff9dcfSAnton Vorontsov EXPORT_SYMBOL(gtm_ack_timer16);
33583ff9dcfSAnton Vorontsov 
33683ff9dcfSAnton Vorontsov static void __init gtm_set_shortcuts(struct device_node *np,
33783ff9dcfSAnton Vorontsov 				     struct gtm_timer *timers,
33883ff9dcfSAnton Vorontsov 				     struct gtm_timers_regs __iomem *regs)
33983ff9dcfSAnton Vorontsov {
34083ff9dcfSAnton Vorontsov 	/*
34183ff9dcfSAnton Vorontsov 	 * Yeah, I don't like this either, but timers' registers a bit messed,
34283ff9dcfSAnton Vorontsov 	 * so we have to provide shortcuts to write timer independent code.
34383ff9dcfSAnton Vorontsov 	 * Alternative option is to create gt*() accessors, but that will be
34483ff9dcfSAnton Vorontsov 	 * even uglier and cryptic.
34583ff9dcfSAnton Vorontsov 	 */
34683ff9dcfSAnton Vorontsov 	timers[0].gtcfr = &regs->gtcfr1;
34783ff9dcfSAnton Vorontsov 	timers[0].gtmdr = &regs->gtmdr1;
34883ff9dcfSAnton Vorontsov 	timers[0].gtcnr = &regs->gtcnr1;
34983ff9dcfSAnton Vorontsov 	timers[0].gtrfr = &regs->gtrfr1;
35083ff9dcfSAnton Vorontsov 	timers[0].gtevr = &regs->gtevr1;
35183ff9dcfSAnton Vorontsov 
35283ff9dcfSAnton Vorontsov 	timers[1].gtcfr = &regs->gtcfr1;
35383ff9dcfSAnton Vorontsov 	timers[1].gtmdr = &regs->gtmdr2;
35483ff9dcfSAnton Vorontsov 	timers[1].gtcnr = &regs->gtcnr2;
35583ff9dcfSAnton Vorontsov 	timers[1].gtrfr = &regs->gtrfr2;
35683ff9dcfSAnton Vorontsov 	timers[1].gtevr = &regs->gtevr2;
35783ff9dcfSAnton Vorontsov 
35883ff9dcfSAnton Vorontsov 	timers[2].gtcfr = &regs->gtcfr2;
35983ff9dcfSAnton Vorontsov 	timers[2].gtmdr = &regs->gtmdr3;
36083ff9dcfSAnton Vorontsov 	timers[2].gtcnr = &regs->gtcnr3;
36183ff9dcfSAnton Vorontsov 	timers[2].gtrfr = &regs->gtrfr3;
36283ff9dcfSAnton Vorontsov 	timers[2].gtevr = &regs->gtevr3;
36383ff9dcfSAnton Vorontsov 
36483ff9dcfSAnton Vorontsov 	timers[3].gtcfr = &regs->gtcfr2;
36583ff9dcfSAnton Vorontsov 	timers[3].gtmdr = &regs->gtmdr4;
36683ff9dcfSAnton Vorontsov 	timers[3].gtcnr = &regs->gtcnr4;
36783ff9dcfSAnton Vorontsov 	timers[3].gtrfr = &regs->gtrfr4;
36883ff9dcfSAnton Vorontsov 	timers[3].gtevr = &regs->gtevr4;
36983ff9dcfSAnton Vorontsov 
37083ff9dcfSAnton Vorontsov 	/* CPM2 doesn't have primary prescaler */
37183ff9dcfSAnton Vorontsov 	if (!of_device_is_compatible(np, "fsl,cpm2-gtm")) {
37283ff9dcfSAnton Vorontsov 		timers[0].gtpsr = &regs->gtpsr1;
37383ff9dcfSAnton Vorontsov 		timers[1].gtpsr = &regs->gtpsr2;
37483ff9dcfSAnton Vorontsov 		timers[2].gtpsr = &regs->gtpsr3;
37583ff9dcfSAnton Vorontsov 		timers[3].gtpsr = &regs->gtpsr4;
37683ff9dcfSAnton Vorontsov 	}
37783ff9dcfSAnton Vorontsov }
37883ff9dcfSAnton Vorontsov 
37983ff9dcfSAnton Vorontsov static int __init fsl_gtm_init(void)
38083ff9dcfSAnton Vorontsov {
38183ff9dcfSAnton Vorontsov 	struct device_node *np;
38283ff9dcfSAnton Vorontsov 
38383ff9dcfSAnton Vorontsov 	for_each_compatible_node(np, NULL, "fsl,gtm") {
38483ff9dcfSAnton Vorontsov 		int i;
38583ff9dcfSAnton Vorontsov 		struct gtm *gtm;
38683ff9dcfSAnton Vorontsov 		const u32 *clock;
38783ff9dcfSAnton Vorontsov 		int size;
38883ff9dcfSAnton Vorontsov 
38983ff9dcfSAnton Vorontsov 		gtm = kzalloc(sizeof(*gtm), GFP_KERNEL);
39083ff9dcfSAnton Vorontsov 		if (!gtm) {
39183ff9dcfSAnton Vorontsov 			pr_err("%s: unable to allocate memory\n",
39283ff9dcfSAnton Vorontsov 				np->full_name);
39383ff9dcfSAnton Vorontsov 			continue;
39483ff9dcfSAnton Vorontsov 		}
39583ff9dcfSAnton Vorontsov 
39683ff9dcfSAnton Vorontsov 		spin_lock_init(&gtm->lock);
39783ff9dcfSAnton Vorontsov 
39883ff9dcfSAnton Vorontsov 		clock = of_get_property(np, "clock-frequency", &size);
39983ff9dcfSAnton Vorontsov 		if (!clock || size != sizeof(*clock)) {
40083ff9dcfSAnton Vorontsov 			pr_err("%s: no clock-frequency\n", np->full_name);
40183ff9dcfSAnton Vorontsov 			goto err;
40283ff9dcfSAnton Vorontsov 		}
40383ff9dcfSAnton Vorontsov 		gtm->clock = *clock;
40483ff9dcfSAnton Vorontsov 
40583ff9dcfSAnton Vorontsov 		for (i = 0; i < ARRAY_SIZE(gtm->timers); i++) {
406f7578496SThierry Reding 			unsigned int irq;
40783ff9dcfSAnton Vorontsov 
408f7578496SThierry Reding 			irq = irq_of_parse_and_map(np, i);
409*ef24ba70SMichael Ellerman 			if (!irq) {
41083ff9dcfSAnton Vorontsov 				pr_err("%s: not enough interrupts specified\n",
41183ff9dcfSAnton Vorontsov 				       np->full_name);
41283ff9dcfSAnton Vorontsov 				goto err;
41383ff9dcfSAnton Vorontsov 			}
414f7578496SThierry Reding 			gtm->timers[i].irq = irq;
41583ff9dcfSAnton Vorontsov 			gtm->timers[i].gtm = gtm;
41683ff9dcfSAnton Vorontsov 		}
41783ff9dcfSAnton Vorontsov 
41883ff9dcfSAnton Vorontsov 		gtm->regs = of_iomap(np, 0);
41983ff9dcfSAnton Vorontsov 		if (!gtm->regs) {
42083ff9dcfSAnton Vorontsov 			pr_err("%s: unable to iomap registers\n",
42183ff9dcfSAnton Vorontsov 			       np->full_name);
42283ff9dcfSAnton Vorontsov 			goto err;
42383ff9dcfSAnton Vorontsov 		}
42483ff9dcfSAnton Vorontsov 
42583ff9dcfSAnton Vorontsov 		gtm_set_shortcuts(np, gtm->timers, gtm->regs);
42683ff9dcfSAnton Vorontsov 		list_add(&gtm->list_node, &gtms);
42783ff9dcfSAnton Vorontsov 
42883ff9dcfSAnton Vorontsov 		/* We don't want to lose the node and its ->data */
42983ff9dcfSAnton Vorontsov 		np->data = gtm;
43083ff9dcfSAnton Vorontsov 		of_node_get(np);
43183ff9dcfSAnton Vorontsov 
43283ff9dcfSAnton Vorontsov 		continue;
43383ff9dcfSAnton Vorontsov err:
43483ff9dcfSAnton Vorontsov 		kfree(gtm);
43583ff9dcfSAnton Vorontsov 	}
43683ff9dcfSAnton Vorontsov 	return 0;
43783ff9dcfSAnton Vorontsov }
43883ff9dcfSAnton Vorontsov arch_initcall(fsl_gtm_init);
439