···11+/*22+ * Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.33+ *44+ * This program is free software; you can redistribute it and/or modify55+ * it under the terms of the GNU General Public License version 2 and66+ * only version 2 as published by the Free Software Foundation.77+ *88+ * This program is distributed in the hope that it will be useful,99+ * but WITHOUT ANY WARRANTY; without even the implied warranty of1010+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the1111+ * GNU General Public License for more details.1212+ *1313+ * You should have received a copy of the GNU General Public License1414+ * along with this program; if not, write to the Free Software1515+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA1616+ * 02110-1301, USA.1717+ */1818+1919+#ifndef ASM_TIME_H2020+#define ASM_TIME_H2121+2222+extern cycles_t pcycle_freq_mhz;2323+extern cycles_t thread_freq_mhz;2424+extern cycles_t sleep_clk_freq;2525+2626+void setup_percpu_clockdev(void);2727+void ipi_timer(void);2828+2929+#endif
+39
arch/hexagon/include/asm/timer-regs.h
···11+/*22+ * Timer support for Hexagon33+ *44+ * Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.55+ *66+ * This program is free software; you can redistribute it and/or modify77+ * it under the terms of the GNU General Public License version 2 and88+ * only version 2 as published by the Free Software Foundation.99+ *1010+ * This program is distributed in the hope that it will be useful,1111+ * but WITHOUT ANY WARRANTY; without even the implied warranty of1212+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the1313+ * GNU General Public License for more details.1414+ *1515+ * You should have received a copy of the GNU General Public License1616+ * along with this program; if not, write to the Free Software1717+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA1818+ * 02110-1301, USA.1919+ */2020+2121+#ifndef _ASM_TIMER_REGS_H2222+#define _ASM_TIMER_REGS_H2323+2424+/* This stuff should go into a platform specific file */2525+#define TCX0_CLK_RATE 192002626+#define TIMER_ENABLE 02727+#define TIMER_CLR_ON_MATCH 12828+2929+/*3030+ * 8x50 HDD Specs 5-8. Simulator co-sim not fixed until3131+ * release 1.1, and then it's "adjustable" and probably not defaulted.3232+ */3333+#define RTOS_TIMER_INT 33434+#ifdef CONFIG_HEXAGON_COMET3535+#define RTOS_TIMER_REGS_ADDR 0xAB000000UL3636+#endif3737+#define SLEEP_CLK_RATE 320003838+3939+#endif
+250
arch/hexagon/kernel/time.c
···11+/*22+ * Time related functions for Hexagon architecture33+ *44+ * Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.55+ *66+ * This program is free software; you can redistribute it and/or modify77+ * it under the terms of the GNU General Public License version 2 and88+ * only version 2 as published by the Free Software Foundation.99+ *1010+ * This program is distributed in the hope that it will be useful,1111+ * but WITHOUT ANY WARRANTY; without even the implied warranty of1212+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the1313+ * GNU General Public License for more details.1414+ *1515+ * You should have received a copy of the GNU General Public License1616+ * along with this program; if not, write to the Free Software1717+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA1818+ * 02110-1301, USA.1919+ */2020+2121+#include <linux/init.h>2222+#include <linux/clockchips.h>2323+#include <linux/clocksource.h>2424+#include <linux/interrupt.h>2525+#include <linux/err.h>2626+#include <linux/platform_device.h>2727+#include <linux/ioport.h>2828+#include <linux/of.h>2929+#include <linux/of_address.h>3030+#include <linux/of_irq.h>3131+3232+#include <asm/timer-regs.h>3333+#include <asm/hexagon_vm.h>3434+3535+/*3636+ * For the clocksource we need:3737+ * pcycle frequency (600MHz)3838+ * For the loops_per_jiffy we need:3939+ * thread/cpu frequency (100MHz)4040+ * And for the timer, we need:4141+ * sleep clock rate4242+ */4343+4444+cycles_t pcycle_freq_mhz;4545+cycles_t thread_freq_mhz;4646+cycles_t sleep_clk_freq;4747+4848+static struct resource rtos_timer_resources[] = {4949+ {5050+ .start = RTOS_TIMER_REGS_ADDR,5151+ .end = RTOS_TIMER_REGS_ADDR+PAGE_SIZE-1,5252+ .flags = IORESOURCE_MEM,5353+ },5454+};5555+5656+static struct platform_device rtos_timer_device = {5757+ .name = "rtos_timer",5858+ .id = -1,5959+ .num_resources = ARRAY_SIZE(rtos_timer_resources),6060+ .resource = rtos_timer_resources,6161+};6262+6363+/* A lot of this stuff should move into a platform specific section. */6464+struct adsp_hw_timer_struct {6565+ u32 match; /* Match value */6666+ u32 count;6767+ u32 enable; /* [1] - CLR_ON_MATCH_EN, [0] - EN */6868+ u32 clear; /* one-shot register that clears the count */6969+};7070+7171+/* Look for "TCX0" for related constants. */7272+static __iomem struct adsp_hw_timer_struct *rtos_timer;7373+7474+static cycle_t timer_get_cycles(struct clocksource *cs)7575+{7676+ return (cycle_t) __vmgettime();7777+}7878+7979+static struct clocksource hexagon_clocksource = {8080+ .name = "pcycles",8181+ .rating = 250,8282+ .read = timer_get_cycles,8383+ .mask = CLOCKSOURCE_MASK(64),8484+ .flags = CLOCK_SOURCE_IS_CONTINUOUS,8585+};8686+8787+static int set_next_event(unsigned long delta, struct clock_event_device *evt)8888+{8989+ /* Assuming the timer will be disabled when we enter here. */9090+9191+ iowrite32(1, &rtos_timer->clear);9292+ iowrite32(0, &rtos_timer->clear);9393+9494+ iowrite32(delta, &rtos_timer->match);9595+ iowrite32(1 << TIMER_ENABLE, &rtos_timer->enable);9696+ return 0;9797+}9898+9999+/*100100+ * Sets the mode (periodic, shutdown, oneshot, etc) of a timer.101101+ */102102+static void set_mode(enum clock_event_mode mode,103103+ struct clock_event_device *evt)104104+{105105+ switch (mode) {106106+ case CLOCK_EVT_MODE_SHUTDOWN:107107+ /* XXX implement me */108108+ default:109109+ break;110110+ }111111+}112112+113113+#ifdef CONFIG_SMP114114+/* Broadcast mechanism */115115+static void broadcast(const struct cpumask *mask)116116+{117117+ send_ipi(mask, IPI_TIMER);118118+}119119+#endif120120+121121+static struct clock_event_device hexagon_clockevent_dev = {122122+ .name = "clockevent",123123+ .features = CLOCK_EVT_FEAT_ONESHOT,124124+ .rating = 400,125125+ .irq = RTOS_TIMER_INT,126126+ .set_next_event = set_next_event,127127+ .set_mode = set_mode,128128+#ifdef CONFIG_SMP129129+ .broadcast = broadcast,130130+#endif131131+};132132+133133+#ifdef CONFIG_SMP134134+static DEFINE_PER_CPU(struct clock_event_device, clock_events);135135+136136+void setup_percpu_clockdev(void)137137+{138138+ int cpu = smp_processor_id();139139+ struct clock_event_device *ce_dev = &hexagon_clockevent_dev;140140+ struct clock_event_device *dummy_clock_dev =141141+ &per_cpu(clock_events, cpu);142142+143143+ memcpy(dummy_clock_dev, ce_dev, sizeof(*dummy_clock_dev));144144+ INIT_LIST_HEAD(&dummy_clock_dev->list);145145+146146+ dummy_clock_dev->features = CLOCK_EVT_FEAT_DUMMY;147147+ dummy_clock_dev->cpumask = cpumask_of(cpu);148148+ dummy_clock_dev->mode = CLOCK_EVT_MODE_UNUSED;149149+150150+ clockevents_register_device(dummy_clock_dev);151151+}152152+153153+/* Called from smp.c for each CPU's timer ipi call */154154+void ipi_timer(void)155155+{156156+ int cpu = smp_processor_id();157157+ struct clock_event_device *ce_dev = &per_cpu(clock_events, cpu);158158+159159+ ce_dev->event_handler(ce_dev);160160+}161161+#endif /* CONFIG_SMP */162162+163163+static irqreturn_t timer_interrupt(int irq, void *devid)164164+{165165+ struct clock_event_device *ce_dev = &hexagon_clockevent_dev;166166+167167+ iowrite32(0, &rtos_timer->enable);168168+ ce_dev->event_handler(ce_dev);169169+170170+ return IRQ_HANDLED;171171+}172172+173173+/* This should also be pulled from devtree */174174+static struct irqaction rtos_timer_intdesc = {175175+ .handler = timer_interrupt,176176+ .flags = IRQF_TIMER | IRQF_TRIGGER_RISING,177177+ .name = "rtos_timer"178178+};179179+180180+/*181181+ * time_init_deferred - called by start_kernel to set up timer/clock source182182+ *183183+ * Install the IRQ handler for the clock, setup timers.184184+ * This is done late, as that way, we can use ioremap().185185+ *186186+ * This runs just before the delay loop is calibrated, and187187+ * is used for delay calibration.188188+ */189189+void __init time_init_deferred(void)190190+{191191+ struct resource *resource = NULL;192192+ struct clock_event_device *ce_dev = &hexagon_clockevent_dev;193193+ struct device_node *dn;194194+ struct resource r;195195+ int err;196196+197197+ ce_dev->cpumask = cpu_all_mask;198198+199199+ if (!resource)200200+ resource = rtos_timer_device.resource;201201+202202+ /* ioremap here means this has to run later, after paging init */203203+ rtos_timer = ioremap(resource->start, resource->end204204+ - resource->start + 1);205205+206206+ if (!rtos_timer) {207207+ release_mem_region(resource->start, resource->end208208+ - resource->start + 1);209209+ }210210+ clocksource_register_khz(&hexagon_clocksource, pcycle_freq_mhz * 1000);211211+212212+ /* Note: the sim generic RTOS clock is apparently really 18750Hz */213213+214214+ /*215215+ * Last arg is some guaranteed seconds for which the conversion will216216+ * work without overflow.217217+ */218218+ clockevents_calc_mult_shift(ce_dev, sleep_clk_freq, 4);219219+220220+ ce_dev->max_delta_ns = clockevent_delta2ns(0x7fffffff, ce_dev);221221+ ce_dev->min_delta_ns = clockevent_delta2ns(0xf, ce_dev);222222+223223+#ifdef CONFIG_SMP224224+ setup_percpu_clockdev();225225+#endif226226+227227+ clockevents_register_device(ce_dev);228228+ setup_irq(ce_dev->irq, &rtos_timer_intdesc);229229+}230230+231231+void __init time_init(void)232232+{233233+ late_time_init = time_init_deferred;234234+}235235+236236+/*237237+ * This could become parametric or perhaps even computed at run-time,238238+ * but for now we take the observed simulator jitter.239239+ */240240+static long long fudgefactor = 350; /* Maybe lower if kernel optimized. */241241+242242+void __udelay(unsigned long usecs)243243+{244244+ unsigned long long start = __vmgettime();245245+ unsigned long long finish = (pcycle_freq_mhz * usecs) - fudgefactor;246246+247247+ while ((__vmgettime() - start) < finish)248248+ cpu_relax(); /* not sure how this improves readability */249249+}250250+EXPORT_SYMBOL(__udelay);