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