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