blob: cefacbad153118620348bb1e3bc88838f75dcde7 [file] [log] [blame]
Jacob Panbb24c472009-09-02 07:37:17 -07001/*
2 * apb_timer.c: Driver for Langwell APB timers
3 *
4 * (C) Copyright 2009 Intel Corporation
5 * Author: Jacob Pan (jacob.jun.pan@intel.com)
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; version 2
10 * of the License.
11 *
12 * Note:
13 * Langwell is the south complex of Intel Moorestown MID platform. There are
14 * eight external timers in total that can be used by the operating system.
15 * The timer information, such as frequency and addresses, is provided to the
16 * OS via SFI tables.
17 * Timer interrupts are routed via FW/HW emulated IOAPIC independently via
18 * individual redirection table entries (RTE).
19 * Unlike HPET, there is no master counter, therefore one of the timers are
20 * used as clocksource. The overall allocation looks like:
21 * - timer 0 - NR_CPUs for per cpu timer
22 * - one timer for clocksource
23 * - one timer for watchdog driver.
24 * It is also worth notice that APB timer does not support true one-shot mode,
25 * free-running mode will be used here to emulate one-shot mode.
26 * APB timer can also be used as broadcast timer along with per cpu local APIC
27 * timer, but by default APB timer has higher rating than local APIC timers.
28 */
29
Jacob Panbb24c472009-09-02 07:37:17 -070030#include <linux/delay.h>
Jamie Iles06c3df42011-06-06 12:43:07 +010031#include <linux/dw_apb_timer.h>
Jacob Panbb24c472009-09-02 07:37:17 -070032#include <linux/errno.h>
33#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Jacob Panbb24c472009-09-02 07:37:17 -070035#include <linux/pm.h>
Jacob Panbb24c472009-09-02 07:37:17 -070036#include <linux/sfi.h>
37#include <linux/interrupt.h>
38#include <linux/cpu.h>
39#include <linux/irq.h>
40
41#include <asm/fixmap.h>
42#include <asm/apb_timer.h>
Kuppuswamy Sathyanarayanan05454c22013-10-17 15:35:27 -070043#include <asm/intel-mid.h>
Ralf Baechle16f871b2011-06-01 19:05:06 +010044#include <asm/time.h>
Jacob Panbb24c472009-09-02 07:37:17 -070045
Jacob Pana875c012010-05-19 12:01:25 -070046#define APBT_CLOCKEVENT_RATING 110
H. Peter Anvinc7bbf522010-03-03 13:38:48 -080047#define APBT_CLOCKSOURCE_RATING 250
Jacob Panbb24c472009-09-02 07:37:17 -070048
Jacob Panbb24c472009-09-02 07:37:17 -070049#define APBT_CLOCKEVENT0_NUM (0)
Jacob Panbb24c472009-09-02 07:37:17 -070050#define APBT_CLOCKSOURCE_NUM (2)
51
Jamie Iles06c3df42011-06-06 12:43:07 +010052static phys_addr_t apbt_address;
Jacob Panbb24c472009-09-02 07:37:17 -070053static int apb_timer_block_enabled;
54static void __iomem *apbt_virt_address;
Jacob Panbb24c472009-09-02 07:37:17 -070055
56/*
57 * Common DW APB timer info
58 */
Jamie Iles06c3df42011-06-06 12:43:07 +010059static unsigned long apbt_freq;
Jacob Panbb24c472009-09-02 07:37:17 -070060
61struct apbt_dev {
Jamie Iles06c3df42011-06-06 12:43:07 +010062 struct dw_apb_clock_event_device *timer;
63 unsigned int num;
64 int cpu;
65 unsigned int irq;
66 char name[10];
Jacob Panbb24c472009-09-02 07:37:17 -070067};
68
Jamie Iles06c3df42011-06-06 12:43:07 +010069static struct dw_apb_clocksource *clocksource_apbt;
70
71static inline void __iomem *adev_virt_addr(struct apbt_dev *adev)
72{
73 return apbt_virt_address + adev->num * APBTMRS_REG_SIZE;
74}
75
Jacob Pan30106732010-03-02 21:01:34 -080076static DEFINE_PER_CPU(struct apbt_dev, cpu_apbt_dev);
77
Jacob Panbb24c472009-09-02 07:37:17 -070078#ifdef CONFIG_SMP
79static unsigned int apbt_num_timers_used;
Jacob Panbb24c472009-09-02 07:37:17 -070080#endif
81
Jacob Panbb24c472009-09-02 07:37:17 -070082static inline void apbt_set_mapping(void)
83{
H. Peter Anvinc7bbf522010-03-03 13:38:48 -080084 struct sfi_timer_table_entry *mtmr;
Jamie Iles06c3df42011-06-06 12:43:07 +010085 int phy_cs_timer_id = 0;
Jacob Panbb24c472009-09-02 07:37:17 -070086
H. Peter Anvinc7bbf522010-03-03 13:38:48 -080087 if (apbt_virt_address) {
88 pr_debug("APBT base already mapped\n");
89 return;
90 }
91 mtmr = sfi_get_mtmr(APBT_CLOCKEVENT0_NUM);
92 if (mtmr == NULL) {
93 printk(KERN_ERR "Failed to get MTMR %d from SFI\n",
94 APBT_CLOCKEVENT0_NUM);
95 return;
96 }
Jamie Iles06c3df42011-06-06 12:43:07 +010097 apbt_address = (phys_addr_t)mtmr->phys_addr;
H. Peter Anvinc7bbf522010-03-03 13:38:48 -080098 if (!apbt_address) {
99 printk(KERN_WARNING "No timer base from SFI, use default\n");
100 apbt_address = APBT_DEFAULT_BASE;
101 }
102 apbt_virt_address = ioremap_nocache(apbt_address, APBT_MMAP_SIZE);
Jamie Iles06c3df42011-06-06 12:43:07 +0100103 if (!apbt_virt_address) {
104 pr_debug("Failed mapping APBT phy address at %lu\n",\
105 (unsigned long)apbt_address);
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800106 goto panic_noapbt;
107 }
Jamie Iles06c3df42011-06-06 12:43:07 +0100108 apbt_freq = mtmr->freq_hz;
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800109 sfi_free_mtmr(mtmr);
Jacob Panbb24c472009-09-02 07:37:17 -0700110
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800111 /* Now figure out the physical timer id for clocksource device */
112 mtmr = sfi_get_mtmr(APBT_CLOCKSOURCE_NUM);
113 if (mtmr == NULL)
114 goto panic_noapbt;
Jacob Panbb24c472009-09-02 07:37:17 -0700115
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800116 /* Now figure out the physical timer id */
Jamie Iles06c3df42011-06-06 12:43:07 +0100117 pr_debug("Use timer %d for clocksource\n",
118 (int)(mtmr->phys_addr & 0xff) / APBTMRS_REG_SIZE);
119 phy_cs_timer_id = (unsigned int)(mtmr->phys_addr & 0xff) /
120 APBTMRS_REG_SIZE;
121
122 clocksource_apbt = dw_apb_clocksource_init(APBT_CLOCKSOURCE_RATING,
123 "apbt0", apbt_virt_address + phy_cs_timer_id *
124 APBTMRS_REG_SIZE, apbt_freq);
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800125 return;
Jacob Panbb24c472009-09-02 07:37:17 -0700126
127panic_noapbt:
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800128 panic("Failed to setup APB system timer\n");
Jacob Panbb24c472009-09-02 07:37:17 -0700129
130}
131
132static inline void apbt_clear_mapping(void)
133{
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800134 iounmap(apbt_virt_address);
135 apbt_virt_address = NULL;
Jacob Panbb24c472009-09-02 07:37:17 -0700136}
137
Jacob Panbb24c472009-09-02 07:37:17 -0700138static int __init apbt_clockevent_register(void)
139{
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800140 struct sfi_timer_table_entry *mtmr;
Christoph Lameter89cbc762014-08-17 12:30:40 -0500141 struct apbt_dev *adev = this_cpu_ptr(&cpu_apbt_dev);
Jacob Panbb24c472009-09-02 07:37:17 -0700142
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800143 mtmr = sfi_get_mtmr(APBT_CLOCKEVENT0_NUM);
144 if (mtmr == NULL) {
145 printk(KERN_ERR "Failed to get MTMR %d from SFI\n",
146 APBT_CLOCKEVENT0_NUM);
147 return -ENODEV;
148 }
Jacob Panbb24c472009-09-02 07:37:17 -0700149
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800150 adev->num = smp_processor_id();
Jamie Iles06c3df42011-06-06 12:43:07 +0100151 adev->timer = dw_apb_clockevent_init(smp_processor_id(), "apbt0",
Kuppuswamy Sathyanarayanan712b6aa2013-10-17 15:35:29 -0700152 intel_mid_timer_options == INTEL_MID_TIMER_LAPIC_APBT ?
Jamie Iles06c3df42011-06-06 12:43:07 +0100153 APBT_CLOCKEVENT_RATING - 100 : APBT_CLOCKEVENT_RATING,
154 adev_virt_addr(adev), 0, apbt_freq);
155 /* Firmware does EOI handling for us. */
156 adev->timer->eoi = NULL;
Jacob Panbb24c472009-09-02 07:37:17 -0700157
Kuppuswamy Sathyanarayanan712b6aa2013-10-17 15:35:29 -0700158 if (intel_mid_timer_options == INTEL_MID_TIMER_LAPIC_APBT) {
Jamie Iles06c3df42011-06-06 12:43:07 +0100159 global_clock_event = &adev->timer->ced;
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800160 printk(KERN_DEBUG "%s clockevent registered as global\n",
161 global_clock_event->name);
162 }
Jacob Panbb24c472009-09-02 07:37:17 -0700163
Jamie Iles06c3df42011-06-06 12:43:07 +0100164 dw_apb_clockevent_register(adev->timer);
Jacob Panbb24c472009-09-02 07:37:17 -0700165
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800166 sfi_free_mtmr(mtmr);
167 return 0;
Jacob Panbb24c472009-09-02 07:37:17 -0700168}
169
170#ifdef CONFIG_SMP
Thomas Gleixnera5ef2e72010-09-28 11:11:10 +0200171
172static void apbt_setup_irq(struct apbt_dev *adev)
173{
Jacob Pan65509042011-01-13 16:06:44 -0800174 irq_modify_status(adev->irq, 0, IRQ_MOVE_PCNTXT);
175 irq_set_affinity(adev->irq, cpumask_of(adev->cpu));
Thomas Gleixnera5ef2e72010-09-28 11:11:10 +0200176}
177
Jacob Panbb24c472009-09-02 07:37:17 -0700178/* Should be called with per cpu */
179void apbt_setup_secondary_clock(void)
180{
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800181 struct apbt_dev *adev;
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800182 int cpu;
Jacob Panbb24c472009-09-02 07:37:17 -0700183
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800184 /* Don't register boot CPU clockevent */
185 cpu = smp_processor_id();
Robert Richterf6e9456c2010-07-21 19:03:58 +0200186 if (!cpu)
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800187 return;
Jacob Panbb24c472009-09-02 07:37:17 -0700188
Christoph Lameter89cbc762014-08-17 12:30:40 -0500189 adev = this_cpu_ptr(&cpu_apbt_dev);
Jamie Iles06c3df42011-06-06 12:43:07 +0100190 if (!adev->timer) {
191 adev->timer = dw_apb_clockevent_init(cpu, adev->name,
192 APBT_CLOCKEVENT_RATING, adev_virt_addr(adev),
193 adev->irq, apbt_freq);
194 adev->timer->eoi = NULL;
195 } else {
196 dw_apb_clockevent_resume(adev->timer);
197 }
Jacob Panbb24c472009-09-02 07:37:17 -0700198
Jamie Iles06c3df42011-06-06 12:43:07 +0100199 printk(KERN_INFO "Registering CPU %d clockevent device %s, cpu %08x\n",
200 cpu, adev->name, adev->cpu);
Jacob Panbb24c472009-09-02 07:37:17 -0700201
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800202 apbt_setup_irq(adev);
Jamie Iles06c3df42011-06-06 12:43:07 +0100203 dw_apb_clockevent_register(adev->timer);
Jacob Panbb24c472009-09-02 07:37:17 -0700204
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800205 return;
Jacob Panbb24c472009-09-02 07:37:17 -0700206}
207
208/*
209 * this notify handler process CPU hotplug events. in case of S0i3, nonboot
210 * cpus are disabled/enabled frequently, for performance reasons, we keep the
211 * per cpu timer irq registered so that we do need to do free_irq/request_irq.
212 *
213 * TODO: it might be more reliable to directly disable percpu clockevent device
214 * without the notifier chain. currently, cpu 0 may get interrupts from other
215 * cpu timers during the offline process due to the ordering of notification.
216 * the extra interrupt is harmless.
217 */
218static int apbt_cpuhp_notify(struct notifier_block *n,
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800219 unsigned long action, void *hcpu)
Jacob Panbb24c472009-09-02 07:37:17 -0700220{
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800221 unsigned long cpu = (unsigned long)hcpu;
222 struct apbt_dev *adev = &per_cpu(cpu_apbt_dev, cpu);
Jacob Panbb24c472009-09-02 07:37:17 -0700223
Thomas Gleixnera38f9872016-03-19 11:41:42 +0100224 switch (action & ~CPU_TASKS_FROZEN) {
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800225 case CPU_DEAD:
Jamie Iles06c3df42011-06-06 12:43:07 +0100226 dw_apb_clockevent_pause(adev->timer);
Thomas Gleixnera5ef2e72010-09-28 11:11:10 +0200227 if (system_state == SYSTEM_RUNNING) {
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800228 pr_debug("skipping APBT CPU %lu offline\n", cpu);
Cong Dingb9975da2013-01-14 22:39:18 +0100229 } else {
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800230 pr_debug("APBT clockevent for cpu %lu offline\n", cpu);
Jamie Iles06c3df42011-06-06 12:43:07 +0100231 dw_apb_clockevent_stop(adev->timer);
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800232 }
233 break;
234 default:
Joe Perchesd0ed0c32010-09-11 22:10:54 -0700235 pr_debug("APBT notified %lu, no action\n", action);
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800236 }
237 return NOTIFY_OK;
Jacob Panbb24c472009-09-02 07:37:17 -0700238}
239
240static __init int apbt_late_init(void)
241{
Kuppuswamy Sathyanarayanan712b6aa2013-10-17 15:35:29 -0700242 if (intel_mid_timer_options == INTEL_MID_TIMER_LAPIC_APBT ||
Jacob Pana875c012010-05-19 12:01:25 -0700243 !apb_timer_block_enabled)
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800244 return 0;
245 /* This notifier should be called after workqueue is ready */
246 hotcpu_notifier(apbt_cpuhp_notify, -20);
247 return 0;
Jacob Panbb24c472009-09-02 07:37:17 -0700248}
249fs_initcall(apbt_late_init);
250#else
251
252void apbt_setup_secondary_clock(void) {}
253
254#endif /* CONFIG_SMP */
255
Jacob Panbb24c472009-09-02 07:37:17 -0700256static int apbt_clocksource_register(void)
257{
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800258 u64 start, now;
259 cycle_t t1;
Jacob Panbb24c472009-09-02 07:37:17 -0700260
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800261 /* Start the counter, use timer 2 as source, timer 0/1 for event */
Jamie Iles06c3df42011-06-06 12:43:07 +0100262 dw_apb_clocksource_start(clocksource_apbt);
Jacob Panbb24c472009-09-02 07:37:17 -0700263
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800264 /* Verify whether apbt counter works */
Jamie Iles06c3df42011-06-06 12:43:07 +0100265 t1 = dw_apb_clocksource_read(clocksource_apbt);
Andy Lutomirski4ea16362015-06-25 18:44:07 +0200266 start = rdtsc();
Jacob Panbb24c472009-09-02 07:37:17 -0700267
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800268 /*
269 * We don't know the TSC frequency yet, but waiting for
270 * 200000 TSC cycles is safe:
271 * 4 GHz == 50us
272 * 1 GHz == 200us
273 */
274 do {
275 rep_nop();
Andy Lutomirski4ea16362015-06-25 18:44:07 +0200276 now = rdtsc();
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800277 } while ((now - start) < 200000UL);
Jacob Panbb24c472009-09-02 07:37:17 -0700278
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800279 /* APBT is the only always on clocksource, it has to work! */
Jamie Iles06c3df42011-06-06 12:43:07 +0100280 if (t1 == dw_apb_clocksource_read(clocksource_apbt))
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800281 panic("APBT counter not counting. APBT disabled\n");
Jacob Panbb24c472009-09-02 07:37:17 -0700282
Jamie Iles06c3df42011-06-06 12:43:07 +0100283 dw_apb_clocksource_register(clocksource_apbt);
Jacob Panbb24c472009-09-02 07:37:17 -0700284
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800285 return 0;
Jacob Panbb24c472009-09-02 07:37:17 -0700286}
287
288/*
289 * Early setup the APBT timer, only use timer 0 for booting then switch to
290 * per CPU timer if possible.
291 * returns 1 if per cpu apbt is setup
292 * returns 0 if no per cpu apbt is chosen
293 * panic if set up failed, this is the only platform timer on Moorestown.
294 */
295void __init apbt_time_init(void)
296{
297#ifdef CONFIG_SMP
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800298 int i;
299 struct sfi_timer_table_entry *p_mtmr;
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800300 struct apbt_dev *adev;
Jacob Panbb24c472009-09-02 07:37:17 -0700301#endif
302
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800303 if (apb_timer_block_enabled)
304 return;
305 apbt_set_mapping();
Jamie Iles06c3df42011-06-06 12:43:07 +0100306 if (!apbt_virt_address)
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800307 goto out_noapbt;
308 /*
309 * Read the frequency and check for a sane value, for ESL model
310 * we extend the possible clock range to allow time scaling.
311 */
Jacob Panbb24c472009-09-02 07:37:17 -0700312
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800313 if (apbt_freq < APBT_MIN_FREQ || apbt_freq > APBT_MAX_FREQ) {
Jamie Iles06c3df42011-06-06 12:43:07 +0100314 pr_debug("APBT has invalid freq 0x%lx\n", apbt_freq);
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800315 goto out_noapbt;
316 }
317 if (apbt_clocksource_register()) {
318 pr_debug("APBT has failed to register clocksource\n");
319 goto out_noapbt;
320 }
321 if (!apbt_clockevent_register())
322 apb_timer_block_enabled = 1;
323 else {
324 pr_debug("APBT has failed to register clockevent\n");
325 goto out_noapbt;
326 }
Jacob Panbb24c472009-09-02 07:37:17 -0700327#ifdef CONFIG_SMP
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800328 /* kernel cmdline disable apb timer, so we will use lapic timers */
Kuppuswamy Sathyanarayanan712b6aa2013-10-17 15:35:29 -0700329 if (intel_mid_timer_options == INTEL_MID_TIMER_LAPIC_APBT) {
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800330 printk(KERN_INFO "apbt: disabled per cpu timer\n");
331 return;
332 }
333 pr_debug("%s: %d CPUs online\n", __func__, num_online_cpus());
Sasha Levin8f170fa2012-12-20 14:11:36 -0500334 if (num_possible_cpus() <= sfi_mtimer_num)
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800335 apbt_num_timers_used = num_possible_cpus();
Sasha Levin8f170fa2012-12-20 14:11:36 -0500336 else
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800337 apbt_num_timers_used = 1;
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800338 pr_debug("%s: %d APB timers used\n", __func__, apbt_num_timers_used);
Jacob Panbb24c472009-09-02 07:37:17 -0700339
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800340 /* here we set up per CPU timer data structure */
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800341 for (i = 0; i < apbt_num_timers_used; i++) {
342 adev = &per_cpu(cpu_apbt_dev, i);
343 adev->num = i;
344 adev->cpu = i;
345 p_mtmr = sfi_get_mtmr(i);
Jamie Iles06c3df42011-06-06 12:43:07 +0100346 if (p_mtmr)
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800347 adev->irq = p_mtmr->irq;
Jamie Iles06c3df42011-06-06 12:43:07 +0100348 else
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800349 printk(KERN_ERR "Failed to get timer for cpu %d\n", i);
Jamie Iles06c3df42011-06-06 12:43:07 +0100350 snprintf(adev->name, sizeof(adev->name) - 1, "apbt%d", i);
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800351 }
Jacob Panbb24c472009-09-02 07:37:17 -0700352#endif
353
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800354 return;
Jacob Panbb24c472009-09-02 07:37:17 -0700355
356out_noapbt:
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800357 apbt_clear_mapping();
358 apb_timer_block_enabled = 0;
359 panic("failed to enable APB timer\n");
Jacob Panbb24c472009-09-02 07:37:17 -0700360}
361
Jacob Panbb24c472009-09-02 07:37:17 -0700362/* called before apb_timer_enable, use early map */
Jamie Iles06c3df42011-06-06 12:43:07 +0100363unsigned long apbt_quick_calibrate(void)
Jacob Panbb24c472009-09-02 07:37:17 -0700364{
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800365 int i, scale;
366 u64 old, new;
367 cycle_t t1, t2;
368 unsigned long khz = 0;
369 u32 loop, shift;
Jacob Panbb24c472009-09-02 07:37:17 -0700370
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800371 apbt_set_mapping();
Jamie Iles06c3df42011-06-06 12:43:07 +0100372 dw_apb_clocksource_start(clocksource_apbt);
Jacob Panbb24c472009-09-02 07:37:17 -0700373
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800374 /* check if the timer can count down, otherwise return */
Jamie Iles06c3df42011-06-06 12:43:07 +0100375 old = dw_apb_clocksource_read(clocksource_apbt);
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800376 i = 10000;
377 while (--i) {
Jamie Iles06c3df42011-06-06 12:43:07 +0100378 if (old != dw_apb_clocksource_read(clocksource_apbt))
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800379 break;
380 }
381 if (!i)
382 goto failed;
Jacob Panbb24c472009-09-02 07:37:17 -0700383
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800384 /* count 16 ms */
Jamie Iles06c3df42011-06-06 12:43:07 +0100385 loop = (apbt_freq / 1000) << 4;
Jacob Panbb24c472009-09-02 07:37:17 -0700386
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800387 /* restart the timer to ensure it won't get to 0 in the calibration */
Jamie Iles06c3df42011-06-06 12:43:07 +0100388 dw_apb_clocksource_start(clocksource_apbt);
Jacob Panbb24c472009-09-02 07:37:17 -0700389
Jamie Iles06c3df42011-06-06 12:43:07 +0100390 old = dw_apb_clocksource_read(clocksource_apbt);
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800391 old += loop;
Jacob Panbb24c472009-09-02 07:37:17 -0700392
Andy Lutomirski4ea16362015-06-25 18:44:07 +0200393 t1 = rdtsc();
Jacob Panbb24c472009-09-02 07:37:17 -0700394
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800395 do {
Jamie Iles06c3df42011-06-06 12:43:07 +0100396 new = dw_apb_clocksource_read(clocksource_apbt);
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800397 } while (new < old);
Jacob Panbb24c472009-09-02 07:37:17 -0700398
Andy Lutomirski4ea16362015-06-25 18:44:07 +0200399 t2 = rdtsc();
Jacob Panbb24c472009-09-02 07:37:17 -0700400
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800401 shift = 5;
402 if (unlikely(loop >> shift == 0)) {
403 printk(KERN_INFO
404 "APBT TSC calibration failed, not enough resolution\n");
405 return 0;
406 }
407 scale = (int)div_u64((t2 - t1), loop >> shift);
Jamie Iles06c3df42011-06-06 12:43:07 +0100408 khz = (scale * (apbt_freq / 1000)) >> shift;
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800409 printk(KERN_INFO "TSC freq calculated by APB timer is %lu khz\n", khz);
410 return khz;
Jacob Panbb24c472009-09-02 07:37:17 -0700411failed:
H. Peter Anvinc7bbf522010-03-03 13:38:48 -0800412 return 0;
Jacob Panbb24c472009-09-02 07:37:17 -0700413}