// SPDX-License-Identifier: GPL-2.0-or-later1/*2* Freescale General-purpose Timers Module3*4* Copyright (c) Freescale Semiconductor, Inc. 2006.5* Shlomi Gridish <[email protected]>6* Jerry Huang <[email protected]>7* Copyright (c) MontaVista Software, Inc. 2008.8* Anton Vorontsov <[email protected]>9*/1011#include <linux/kernel.h>12#include <linux/err.h>13#include <linux/errno.h>14#include <linux/list.h>15#include <linux/io.h>16#include <linux/of.h>17#include <linux/of_address.h>18#include <linux/of_irq.h>19#include <linux/spinlock.h>20#include <linux/bitops.h>21#include <linux/slab.h>22#include <linux/export.h>23#include <asm/fsl_gtm.h>2425#define GTCFR_STP(x) ((x) & 1 ? 1 << 5 : 1 << 1)26#define GTCFR_RST(x) ((x) & 1 ? 1 << 4 : 1 << 0)2728#define GTMDR_ICLK_MASK (3 << 1)29#define GTMDR_ICLK_ICAS (0 << 1)30#define GTMDR_ICLK_ICLK (1 << 1)31#define GTMDR_ICLK_SLGO (2 << 1)32#define GTMDR_FRR (1 << 3)33#define GTMDR_ORI (1 << 4)34#define GTMDR_SPS(x) ((x) << 8)3536struct gtm_timers_regs {37u8 gtcfr1; /* Timer 1, Timer 2 global config register */38u8 res0[0x3];39u8 gtcfr2; /* Timer 3, timer 4 global config register */40u8 res1[0xB];41__be16 gtmdr1; /* Timer 1 mode register */42__be16 gtmdr2; /* Timer 2 mode register */43__be16 gtrfr1; /* Timer 1 reference register */44__be16 gtrfr2; /* Timer 2 reference register */45__be16 gtcpr1; /* Timer 1 capture register */46__be16 gtcpr2; /* Timer 2 capture register */47__be16 gtcnr1; /* Timer 1 counter */48__be16 gtcnr2; /* Timer 2 counter */49__be16 gtmdr3; /* Timer 3 mode register */50__be16 gtmdr4; /* Timer 4 mode register */51__be16 gtrfr3; /* Timer 3 reference register */52__be16 gtrfr4; /* Timer 4 reference register */53__be16 gtcpr3; /* Timer 3 capture register */54__be16 gtcpr4; /* Timer 4 capture register */55__be16 gtcnr3; /* Timer 3 counter */56__be16 gtcnr4; /* Timer 4 counter */57__be16 gtevr1; /* Timer 1 event register */58__be16 gtevr2; /* Timer 2 event register */59__be16 gtevr3; /* Timer 3 event register */60__be16 gtevr4; /* Timer 4 event register */61__be16 gtpsr1; /* Timer 1 prescale register */62__be16 gtpsr2; /* Timer 2 prescale register */63__be16 gtpsr3; /* Timer 3 prescale register */64__be16 gtpsr4; /* Timer 4 prescale register */65u8 res2[0x40];66} __attribute__ ((packed));6768struct gtm {69unsigned int clock;70struct gtm_timers_regs __iomem *regs;71struct gtm_timer timers[4];72spinlock_t lock;73struct list_head list_node;74};7576static LIST_HEAD(gtms);7778/**79* gtm_get_timer16 - request GTM timer to use it with the rest of GTM API80* Context: non-IRQ81*82* This function reserves GTM timer for later use. It returns gtm_timer83* structure to use with the rest of GTM API, you should use timer->irq84* to manage timer interrupt.85*/86struct gtm_timer *gtm_get_timer16(void)87{88struct gtm *gtm;89int i;9091list_for_each_entry(gtm, >ms, list_node) {92spin_lock_irq(>m->lock);9394for (i = 0; i < ARRAY_SIZE(gtm->timers); i++) {95if (!gtm->timers[i].requested) {96gtm->timers[i].requested = true;97spin_unlock_irq(>m->lock);98return >m->timers[i];99}100}101102spin_unlock_irq(>m->lock);103}104105if (!list_empty(>ms))106return ERR_PTR(-EBUSY);107return ERR_PTR(-ENODEV);108}109EXPORT_SYMBOL(gtm_get_timer16);110111/**112* gtm_get_specific_timer16 - request specific GTM timer113* @gtm: specific GTM, pass here GTM's device_node->data114* @timer: specific timer number, Timer1 is 0.115* Context: non-IRQ116*117* This function reserves GTM timer for later use. It returns gtm_timer118* structure to use with the rest of GTM API, you should use timer->irq119* to manage timer interrupt.120*/121struct gtm_timer *gtm_get_specific_timer16(struct gtm *gtm,122unsigned int timer)123{124struct gtm_timer *ret = ERR_PTR(-EBUSY);125126if (timer > 3)127return ERR_PTR(-EINVAL);128129spin_lock_irq(>m->lock);130131if (gtm->timers[timer].requested)132goto out;133134ret = >m->timers[timer];135ret->requested = true;136137out:138spin_unlock_irq(>m->lock);139return ret;140}141EXPORT_SYMBOL(gtm_get_specific_timer16);142143/**144* gtm_put_timer16 - release 16 bits GTM timer145* @tmr: pointer to the gtm_timer structure obtained from gtm_get_timer146* Context: any147*148* This function releases GTM timer so others may request it.149*/150void gtm_put_timer16(struct gtm_timer *tmr)151{152gtm_stop_timer16(tmr);153154spin_lock_irq(&tmr->gtm->lock);155tmr->requested = false;156spin_unlock_irq(&tmr->gtm->lock);157}158EXPORT_SYMBOL(gtm_put_timer16);159160/*161* This is back-end for the exported functions, it's used to reset single162* timer in reference mode.163*/164static int gtm_set_ref_timer16(struct gtm_timer *tmr, int frequency,165int reference_value, bool free_run)166{167struct gtm *gtm = tmr->gtm;168int num = tmr - >m->timers[0];169unsigned int prescaler;170u8 iclk = GTMDR_ICLK_ICLK;171u8 psr;172u8 sps;173unsigned long flags;174int max_prescaler = 256 * 256 * 16;175176/* CPM2 doesn't have primary prescaler */177if (!tmr->gtpsr)178max_prescaler /= 256;179180prescaler = gtm->clock / frequency;181/*182* We have two 8 bit prescalers -- primary and secondary (psr, sps),183* plus "slow go" mode (clk / 16). So, total prescale value is184* 16 * (psr + 1) * (sps + 1). Though, for CPM2 GTMs we losing psr.185*/186if (prescaler > max_prescaler)187return -EINVAL;188189if (prescaler > max_prescaler / 16) {190iclk = GTMDR_ICLK_SLGO;191prescaler /= 16;192}193194if (prescaler <= 256) {195psr = 0;196sps = prescaler - 1;197} else {198psr = 256 - 1;199sps = prescaler / 256 - 1;200}201202spin_lock_irqsave(>m->lock, flags);203204/*205* Properly reset timers: stop, reset, set up prescalers, reference206* value and clear event register.207*/208clrsetbits_8(tmr->gtcfr, ~(GTCFR_STP(num) | GTCFR_RST(num)),209GTCFR_STP(num) | GTCFR_RST(num));210211setbits8(tmr->gtcfr, GTCFR_STP(num));212213if (tmr->gtpsr)214out_be16(tmr->gtpsr, psr);215clrsetbits_be16(tmr->gtmdr, 0xFFFF, iclk | GTMDR_SPS(sps) |216GTMDR_ORI | (free_run ? GTMDR_FRR : 0));217out_be16(tmr->gtcnr, 0);218out_be16(tmr->gtrfr, reference_value);219out_be16(tmr->gtevr, 0xFFFF);220221/* Let it be. */222clrbits8(tmr->gtcfr, GTCFR_STP(num));223224spin_unlock_irqrestore(>m->lock, flags);225226return 0;227}228229/**230* gtm_set_timer16 - (re)set 16 bit timer with arbitrary precision231* @tmr: pointer to the gtm_timer structure obtained from gtm_get_timer232* @usec: timer interval in microseconds233* @reload: if set, the timer will reset upon expiry rather than234* continue running free.235* Context: any236*237* This function (re)sets the GTM timer so that it counts up to the requested238* interval value, and fires the interrupt when the value is reached. This239* function will reduce the precision of the timer as needed in order for the240* requested timeout to fit in a 16-bit register.241*/242int gtm_set_timer16(struct gtm_timer *tmr, unsigned long usec, bool reload)243{244/* quite obvious, frequency which is enough for µSec precision */245int freq = 1000000;246unsigned int bit;247248bit = fls_long(usec);249if (bit > 15) {250freq >>= bit - 15;251usec >>= bit - 15;252}253254if (!freq)255return -EINVAL;256257return gtm_set_ref_timer16(tmr, freq, usec, reload);258}259EXPORT_SYMBOL(gtm_set_timer16);260261/**262* gtm_set_exact_timer16 - (re)set 16 bits timer263* @tmr: pointer to the gtm_timer structure obtained from gtm_get_timer264* @usec: timer interval in microseconds265* @reload: if set, the timer will reset upon expiry rather than266* continue running free.267* Context: any268*269* This function (re)sets GTM timer so that it counts up to the requested270* interval value, and fires the interrupt when the value is reached. If reload271* flag was set, timer will also reset itself upon reference value, otherwise272* it continues to increment.273*274* The _exact_ bit in the function name states that this function will not275* crop precision of the "usec" argument, thus usec is limited to 16 bits276* (single timer width).277*/278int gtm_set_exact_timer16(struct gtm_timer *tmr, u16 usec, bool reload)279{280/* quite obvious, frequency which is enough for µSec precision */281const int freq = 1000000;282283/*284* We can lower the frequency (and probably power consumption) by285* dividing both frequency and usec by 2 until there is no remainder.286* But we won't bother with this unless savings are measured, so just287* run the timer as is.288*/289290return gtm_set_ref_timer16(tmr, freq, usec, reload);291}292EXPORT_SYMBOL(gtm_set_exact_timer16);293294/**295* gtm_stop_timer16 - stop single timer296* @tmr: pointer to the gtm_timer structure obtained from gtm_get_timer297* Context: any298*299* This function simply stops the GTM timer.300*/301void gtm_stop_timer16(struct gtm_timer *tmr)302{303struct gtm *gtm = tmr->gtm;304int num = tmr - >m->timers[0];305unsigned long flags;306307spin_lock_irqsave(>m->lock, flags);308309setbits8(tmr->gtcfr, GTCFR_STP(num));310out_be16(tmr->gtevr, 0xFFFF);311312spin_unlock_irqrestore(>m->lock, flags);313}314EXPORT_SYMBOL(gtm_stop_timer16);315316/**317* gtm_ack_timer16 - acknowledge timer event (free-run timers only)318* @tmr: pointer to the gtm_timer structure obtained from gtm_get_timer319* @events: events mask to ack320* Context: any321*322* Thus function used to acknowledge timer interrupt event, use it inside the323* interrupt handler.324*/325void gtm_ack_timer16(struct gtm_timer *tmr, u16 events)326{327out_be16(tmr->gtevr, events);328}329EXPORT_SYMBOL(gtm_ack_timer16);330331static void __init gtm_set_shortcuts(struct device_node *np,332struct gtm_timer *timers,333struct gtm_timers_regs __iomem *regs)334{335/*336* Yeah, I don't like this either, but timers' registers a bit messed,337* so we have to provide shortcuts to write timer independent code.338* Alternative option is to create gt*() accessors, but that will be339* even uglier and cryptic.340*/341timers[0].gtcfr = ®s->gtcfr1;342timers[0].gtmdr = ®s->gtmdr1;343timers[0].gtcnr = ®s->gtcnr1;344timers[0].gtrfr = ®s->gtrfr1;345timers[0].gtevr = ®s->gtevr1;346347timers[1].gtcfr = ®s->gtcfr1;348timers[1].gtmdr = ®s->gtmdr2;349timers[1].gtcnr = ®s->gtcnr2;350timers[1].gtrfr = ®s->gtrfr2;351timers[1].gtevr = ®s->gtevr2;352353timers[2].gtcfr = ®s->gtcfr2;354timers[2].gtmdr = ®s->gtmdr3;355timers[2].gtcnr = ®s->gtcnr3;356timers[2].gtrfr = ®s->gtrfr3;357timers[2].gtevr = ®s->gtevr3;358359timers[3].gtcfr = ®s->gtcfr2;360timers[3].gtmdr = ®s->gtmdr4;361timers[3].gtcnr = ®s->gtcnr4;362timers[3].gtrfr = ®s->gtrfr4;363timers[3].gtevr = ®s->gtevr4;364365/* CPM2 doesn't have primary prescaler */366if (!of_device_is_compatible(np, "fsl,cpm2-gtm")) {367timers[0].gtpsr = ®s->gtpsr1;368timers[1].gtpsr = ®s->gtpsr2;369timers[2].gtpsr = ®s->gtpsr3;370timers[3].gtpsr = ®s->gtpsr4;371}372}373374static int __init fsl_gtm_init(void)375{376struct device_node *np;377378for_each_compatible_node(np, NULL, "fsl,gtm") {379int i;380struct gtm *gtm;381const u32 *clock;382int size;383384gtm = kzalloc(sizeof(*gtm), GFP_KERNEL);385if (!gtm) {386pr_err("%pOF: unable to allocate memory\n",387np);388continue;389}390391spin_lock_init(>m->lock);392393clock = of_get_property(np, "clock-frequency", &size);394if (!clock || size != sizeof(*clock)) {395pr_err("%pOF: no clock-frequency\n", np);396goto err;397}398gtm->clock = *clock;399400for (i = 0; i < ARRAY_SIZE(gtm->timers); i++) {401unsigned int irq;402403irq = irq_of_parse_and_map(np, i);404if (!irq) {405pr_err("%pOF: not enough interrupts specified\n",406np);407goto err;408}409gtm->timers[i].irq = irq;410gtm->timers[i].gtm = gtm;411}412413gtm->regs = of_iomap(np, 0);414if (!gtm->regs) {415pr_err("%pOF: unable to iomap registers\n",416np);417goto err;418}419420gtm_set_shortcuts(np, gtm->timers, gtm->regs);421list_add(>m->list_node, >ms);422423/* We don't want to lose the node and its ->data */424np->data = gtm;425of_node_get(np);426427continue;428err:429kfree(gtm);430}431return 0;432}433arch_initcall(fsl_gtm_init);434435436