183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+ 284ad6884SPeter Tyser /* 384ad6884SPeter Tyser * (C) Copyright 2009 484ad6884SPeter Tyser * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com. 584ad6884SPeter Tyser */ 684ad6884SPeter Tyser 784ad6884SPeter Tyser #include <common.h> 884ad6884SPeter Tyser #include <asm/io.h> 984ad6884SPeter Tyser #include <asm/arch/hardware.h> 1084ad6884SPeter Tyser #include <asm/arch/spr_gpt.h> 1184ad6884SPeter Tyser #include <asm/arch/spr_misc.h> 1284ad6884SPeter Tyser 1384ad6884SPeter Tyser #define GPT_RESOLUTION (CONFIG_SPEAR_HZ_CLOCK / CONFIG_SPEAR_HZ) 1484ad6884SPeter Tyser #define READ_TIMER() (readl(&gpt_regs_p->count) & GPT_FREE_RUNNING) 1584ad6884SPeter Tyser 1684ad6884SPeter Tyser static struct gpt_regs *const gpt_regs_p = 1784ad6884SPeter Tyser (struct gpt_regs *)CONFIG_SPEAR_TIMERBASE; 1884ad6884SPeter Tyser 1984ad6884SPeter Tyser static struct misc_regs *const misc_regs_p = 2084ad6884SPeter Tyser (struct misc_regs *)CONFIG_SPEAR_MISCBASE; 2184ad6884SPeter Tyser 22c9ac3ba1SHeiko Schocher DECLARE_GLOBAL_DATA_PTR; 23c9ac3ba1SHeiko Schocher 24*6180ea7eSPatrick Delaunay static ulong get_timer_masked(void); 25*6180ea7eSPatrick Delaunay 2666ee6923SSimon Glass #define timestamp gd->arch.tbl 27582601daSSimon Glass #define lastdec gd->arch.lastinc 2884ad6884SPeter Tyser timer_init(void)2984ad6884SPeter Tyserint timer_init(void) 3084ad6884SPeter Tyser { 3184ad6884SPeter Tyser u32 synth; 3284ad6884SPeter Tyser 3384ad6884SPeter Tyser /* Prescaler setting */ 3484ad6884SPeter Tyser #if defined(CONFIG_SPEAR3XX) 3584ad6884SPeter Tyser writel(MISC_PRSC_CFG, &misc_regs_p->prsc2_clk_cfg); 3684ad6884SPeter Tyser synth = MISC_GPT4SYNTH; 3784ad6884SPeter Tyser #elif defined(CONFIG_SPEAR600) 3884ad6884SPeter Tyser writel(MISC_PRSC_CFG, &misc_regs_p->prsc1_clk_cfg); 3984ad6884SPeter Tyser synth = MISC_GPT3SYNTH; 4084ad6884SPeter Tyser #else 416ef2e750SSimon Glass # error Incorrect config. Can only be SPEAR{600|300|310|320} 4284ad6884SPeter Tyser #endif 4384ad6884SPeter Tyser 4484ad6884SPeter Tyser writel(readl(&misc_regs_p->periph_clk_cfg) | synth, 4584ad6884SPeter Tyser &misc_regs_p->periph_clk_cfg); 4684ad6884SPeter Tyser 4784ad6884SPeter Tyser /* disable timers */ 4884ad6884SPeter Tyser writel(GPT_PRESCALER_1 | GPT_MODE_AUTO_RELOAD, &gpt_regs_p->control); 4984ad6884SPeter Tyser 5084ad6884SPeter Tyser /* load value for free running */ 5184ad6884SPeter Tyser writel(GPT_FREE_RUNNING, &gpt_regs_p->compare); 5284ad6884SPeter Tyser 5384ad6884SPeter Tyser /* auto reload, start timer */ 5484ad6884SPeter Tyser writel(readl(&gpt_regs_p->control) | GPT_ENABLE, &gpt_regs_p->control); 5584ad6884SPeter Tyser 5617659d7dSGraeme Russ /* Reset the timer */ 5717659d7dSGraeme Russ lastdec = READ_TIMER(); 5817659d7dSGraeme Russ timestamp = 0; 5984ad6884SPeter Tyser 6084ad6884SPeter Tyser return 0; 6184ad6884SPeter Tyser } 6284ad6884SPeter Tyser 6384ad6884SPeter Tyser /* 6484ad6884SPeter Tyser * timer without interrupts 6584ad6884SPeter Tyser */ get_timer(ulong base)6684ad6884SPeter Tyserulong get_timer(ulong base) 6784ad6884SPeter Tyser { 6884ad6884SPeter Tyser return (get_timer_masked() / GPT_RESOLUTION) - base; 6984ad6884SPeter Tyser } 7084ad6884SPeter Tyser __udelay(unsigned long usec)7184ad6884SPeter Tyservoid __udelay(unsigned long usec) 7284ad6884SPeter Tyser { 7384ad6884SPeter Tyser ulong tmo; 7484ad6884SPeter Tyser ulong start = get_timer_masked(); 7584ad6884SPeter Tyser ulong tenudelcnt = CONFIG_SPEAR_HZ_CLOCK / (1000 * 100); 7684ad6884SPeter Tyser ulong rndoff; 7784ad6884SPeter Tyser 7884ad6884SPeter Tyser rndoff = (usec % 10) ? 1 : 0; 7984ad6884SPeter Tyser 8084ad6884SPeter Tyser /* tenudelcnt timer tick gives 10 microsecconds delay */ 8184ad6884SPeter Tyser tmo = ((usec / 10) + rndoff) * tenudelcnt; 8284ad6884SPeter Tyser 8384ad6884SPeter Tyser while ((ulong) (get_timer_masked() - start) < tmo) 8484ad6884SPeter Tyser ; 8584ad6884SPeter Tyser } 8684ad6884SPeter Tyser get_timer_masked(void)87*6180ea7eSPatrick Delaunaystatic ulong get_timer_masked(void) 8884ad6884SPeter Tyser { 8984ad6884SPeter Tyser ulong now = READ_TIMER(); 9084ad6884SPeter Tyser 9184ad6884SPeter Tyser if (now >= lastdec) { 9284ad6884SPeter Tyser /* normal mode */ 9384ad6884SPeter Tyser timestamp += now - lastdec; 9484ad6884SPeter Tyser } else { 9584ad6884SPeter Tyser /* we have an overflow ... */ 9684ad6884SPeter Tyser timestamp += now + GPT_FREE_RUNNING - lastdec; 9784ad6884SPeter Tyser } 9884ad6884SPeter Tyser lastdec = now; 9984ad6884SPeter Tyser 10084ad6884SPeter Tyser return timestamp; 10184ad6884SPeter Tyser } 10284ad6884SPeter Tyser 10384ad6884SPeter Tyser /* 10484ad6884SPeter Tyser * This function is derived from PowerPC code (read timebase as long long). 10584ad6884SPeter Tyser * On ARM it just returns the timer value. 10684ad6884SPeter Tyser */ get_ticks(void)10784ad6884SPeter Tyserunsigned long long get_ticks(void) 10884ad6884SPeter Tyser { 10984ad6884SPeter Tyser return get_timer(0); 11084ad6884SPeter Tyser } 11184ad6884SPeter Tyser 11284ad6884SPeter Tyser /* 11384ad6884SPeter Tyser * This function is derived from PowerPC code (timebase clock frequency). 11484ad6884SPeter Tyser * On ARM it returns the number of timer ticks per second. 11584ad6884SPeter Tyser */ get_tbclk(void)11684ad6884SPeter Tyserulong get_tbclk(void) 11784ad6884SPeter Tyser { 11884ad6884SPeter Tyser return CONFIG_SPEAR_HZ; 11984ad6884SPeter Tyser } 120