avionic design with actual uboot and tooling
submodule of avionic design uboot bootloader and with included tools to get you started , read readme.md and readme-tk1-loader.md
This commit is contained in:
49
u-boot/drivers/timer/Kconfig
Normal file
49
u-boot/drivers/timer/Kconfig
Normal file
@@ -0,0 +1,49 @@
|
||||
menu "Timer Support"
|
||||
|
||||
config TIMER
|
||||
bool "Enable driver model for timer drivers"
|
||||
depends on DM
|
||||
help
|
||||
Enable driver model for timer access. It uses the same API as
|
||||
lib/time.c, but now implemented by the uclass. The first timer
|
||||
will be used. The timer is usually a 32 bits free-running up
|
||||
counter. There may be no real tick, and no timer interrupt.
|
||||
|
||||
config TIMER_EARLY
|
||||
bool "Allow timer to be used early in U-Boot"
|
||||
depends on TIMER
|
||||
help
|
||||
In some cases the timer must be accessible before driver model is
|
||||
active. Examples include when using CONFIG_TRACE to trace U-Boot's
|
||||
execution before driver model is set up. Enable this option to
|
||||
use an early timer. These functions must be supported by your timer
|
||||
driver: timer_early_get_count() and timer_early_get_rate().
|
||||
|
||||
config ALTERA_TIMER
|
||||
bool "Altera timer support"
|
||||
depends on TIMER
|
||||
help
|
||||
Select this to enable a timer for Altera devices. Please find
|
||||
details on the "Embedded Peripherals IP User Guide" of Altera.
|
||||
|
||||
config SANDBOX_TIMER
|
||||
bool "Sandbox timer support"
|
||||
depends on SANDBOX && TIMER
|
||||
help
|
||||
Select this to enable an emulated timer for sandbox. It gets
|
||||
time from host os.
|
||||
|
||||
config X86_TSC_TIMER
|
||||
bool "x86 Time-Stamp Counter (TSC) timer support"
|
||||
depends on TIMER && X86
|
||||
default y if X86
|
||||
help
|
||||
Select this to enable Time-Stamp Counter (TSC) timer for x86.
|
||||
|
||||
config OMAP_TIMER
|
||||
bool "Omap timer support"
|
||||
depends on TIMER
|
||||
help
|
||||
Select this to enable an timer for Omap devices.
|
||||
|
||||
endmenu
|
||||
11
u-boot/drivers/timer/Makefile
Normal file
11
u-boot/drivers/timer/Makefile
Normal file
@@ -0,0 +1,11 @@
|
||||
#
|
||||
# Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw>
|
||||
#
|
||||
# SPDX-License-Identifier: GPL-2.0+
|
||||
#
|
||||
|
||||
obj-$(CONFIG_TIMER) += timer-uclass.o
|
||||
obj-$(CONFIG_ALTERA_TIMER) += altera_timer.o
|
||||
obj-$(CONFIG_SANDBOX_TIMER) += sandbox_timer.o
|
||||
obj-$(CONFIG_X86_TSC_TIMER) += tsc_timer.o
|
||||
obj-$(CONFIG_OMAP_TIMER) += omap-timer.o
|
||||
99
u-boot/drivers/timer/altera_timer.c
Normal file
99
u-boot/drivers/timer/altera_timer.c
Normal file
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* (C) Copyright 2000-2002
|
||||
* Wolfgang Denk, DENX Software Engineering, wd@denx.de.
|
||||
*
|
||||
* (C) Copyright 2004, Psyent Corporation <www.psyent.com>
|
||||
* Scott McNutt <smcnutt@psyent.com>
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0+
|
||||
*/
|
||||
|
||||
#include <common.h>
|
||||
#include <dm.h>
|
||||
#include <errno.h>
|
||||
#include <timer.h>
|
||||
#include <asm/io.h>
|
||||
|
||||
DECLARE_GLOBAL_DATA_PTR;
|
||||
|
||||
/* control register */
|
||||
#define ALTERA_TIMER_CONT BIT(1) /* Continuous mode */
|
||||
#define ALTERA_TIMER_START BIT(2) /* Start timer */
|
||||
#define ALTERA_TIMER_STOP BIT(3) /* Stop timer */
|
||||
|
||||
struct altera_timer_regs {
|
||||
u32 status; /* Timer status reg */
|
||||
u32 control; /* Timer control reg */
|
||||
u32 periodl; /* Timeout period low */
|
||||
u32 periodh; /* Timeout period high */
|
||||
u32 snapl; /* Snapshot low */
|
||||
u32 snaph; /* Snapshot high */
|
||||
};
|
||||
|
||||
struct altera_timer_platdata {
|
||||
struct altera_timer_regs *regs;
|
||||
};
|
||||
|
||||
static int altera_timer_get_count(struct udevice *dev, u64 *count)
|
||||
{
|
||||
struct altera_timer_platdata *plat = dev->platdata;
|
||||
struct altera_timer_regs *const regs = plat->regs;
|
||||
u32 val;
|
||||
|
||||
/* Trigger update */
|
||||
writel(0x0, ®s->snapl);
|
||||
|
||||
/* Read timer value */
|
||||
val = readl(®s->snapl) & 0xffff;
|
||||
val |= (readl(®s->snaph) & 0xffff) << 16;
|
||||
*count = timer_conv_64(~val);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int altera_timer_probe(struct udevice *dev)
|
||||
{
|
||||
struct altera_timer_platdata *plat = dev->platdata;
|
||||
struct altera_timer_regs *const regs = plat->regs;
|
||||
|
||||
writel(0, ®s->status);
|
||||
writel(0, ®s->control);
|
||||
writel(ALTERA_TIMER_STOP, ®s->control);
|
||||
|
||||
writel(0xffff, ®s->periodl);
|
||||
writel(0xffff, ®s->periodh);
|
||||
writel(ALTERA_TIMER_CONT | ALTERA_TIMER_START, ®s->control);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int altera_timer_ofdata_to_platdata(struct udevice *dev)
|
||||
{
|
||||
struct altera_timer_platdata *plat = dev_get_platdata(dev);
|
||||
|
||||
plat->regs = map_physmem(dev_get_addr(dev),
|
||||
sizeof(struct altera_timer_regs),
|
||||
MAP_NOCACHE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct timer_ops altera_timer_ops = {
|
||||
.get_count = altera_timer_get_count,
|
||||
};
|
||||
|
||||
static const struct udevice_id altera_timer_ids[] = {
|
||||
{ .compatible = "altr,timer-1.0" },
|
||||
{}
|
||||
};
|
||||
|
||||
U_BOOT_DRIVER(altera_timer) = {
|
||||
.name = "altera_timer",
|
||||
.id = UCLASS_TIMER,
|
||||
.of_match = altera_timer_ids,
|
||||
.ofdata_to_platdata = altera_timer_ofdata_to_platdata,
|
||||
.platdata_auto_alloc_size = sizeof(struct altera_timer_platdata),
|
||||
.probe = altera_timer_probe,
|
||||
.ops = &altera_timer_ops,
|
||||
.flags = DM_FLAG_PRE_RELOC,
|
||||
};
|
||||
109
u-boot/drivers/timer/omap-timer.c
Normal file
109
u-boot/drivers/timer/omap-timer.c
Normal file
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* TI OMAP timer driver
|
||||
*
|
||||
* Copyright (C) 2015, Texas Instruments, Incorporated
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0+
|
||||
*/
|
||||
|
||||
#include <common.h>
|
||||
#include <dm.h>
|
||||
#include <errno.h>
|
||||
#include <timer.h>
|
||||
#include <asm/io.h>
|
||||
#include <asm/arch/clock.h>
|
||||
|
||||
DECLARE_GLOBAL_DATA_PTR;
|
||||
|
||||
/* Timer register bits */
|
||||
#define TCLR_START BIT(0) /* Start=1 */
|
||||
#define TCLR_AUTO_RELOAD BIT(1) /* Auto reload */
|
||||
#define TCLR_PRE_EN BIT(5) /* Pre-scaler enable */
|
||||
#define TCLR_PTV_SHIFT (2) /* Pre-scaler shift value */
|
||||
|
||||
#define TIMER_CLOCK (V_SCLK / (2 << CONFIG_SYS_PTV))
|
||||
|
||||
struct omap_gptimer_regs {
|
||||
unsigned int tidr; /* offset 0x00 */
|
||||
unsigned char res1[12];
|
||||
unsigned int tiocp_cfg; /* offset 0x10 */
|
||||
unsigned char res2[12];
|
||||
unsigned int tier; /* offset 0x20 */
|
||||
unsigned int tistatr; /* offset 0x24 */
|
||||
unsigned int tistat; /* offset 0x28 */
|
||||
unsigned int tisr; /* offset 0x2c */
|
||||
unsigned int tcicr; /* offset 0x30 */
|
||||
unsigned int twer; /* offset 0x34 */
|
||||
unsigned int tclr; /* offset 0x38 */
|
||||
unsigned int tcrr; /* offset 0x3c */
|
||||
unsigned int tldr; /* offset 0x40 */
|
||||
unsigned int ttgr; /* offset 0x44 */
|
||||
unsigned int twpc; /* offset 0x48 */
|
||||
unsigned int tmar; /* offset 0x4c */
|
||||
unsigned int tcar1; /* offset 0x50 */
|
||||
unsigned int tscir; /* offset 0x54 */
|
||||
unsigned int tcar2; /* offset 0x58 */
|
||||
};
|
||||
|
||||
/* Omap Timer Priv */
|
||||
struct omap_timer_priv {
|
||||
struct omap_gptimer_regs *regs;
|
||||
};
|
||||
|
||||
static int omap_timer_get_count(struct udevice *dev, u64 *count)
|
||||
{
|
||||
struct omap_timer_priv *priv = dev_get_priv(dev);
|
||||
|
||||
*count = readl(&priv->regs->tcrr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int omap_timer_probe(struct udevice *dev)
|
||||
{
|
||||
struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
|
||||
struct omap_timer_priv *priv = dev_get_priv(dev);
|
||||
|
||||
uc_priv->clock_rate = TIMER_CLOCK;
|
||||
|
||||
/* start the counter ticking up, reload value on overflow */
|
||||
writel(0, &priv->regs->tldr);
|
||||
/* enable timer */
|
||||
writel((CONFIG_SYS_PTV << 2) | TCLR_PRE_EN | TCLR_AUTO_RELOAD |
|
||||
TCLR_START, &priv->regs->tclr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int omap_timer_ofdata_to_platdata(struct udevice *dev)
|
||||
{
|
||||
struct omap_timer_priv *priv = dev_get_priv(dev);
|
||||
|
||||
priv->regs = map_physmem(dev_get_addr(dev),
|
||||
sizeof(struct omap_gptimer_regs), MAP_NOCACHE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static const struct timer_ops omap_timer_ops = {
|
||||
.get_count = omap_timer_get_count,
|
||||
};
|
||||
|
||||
static const struct udevice_id omap_timer_ids[] = {
|
||||
{ .compatible = "ti,am335x-timer" },
|
||||
{ .compatible = "ti,am4372-timer" },
|
||||
{ .compatible = "ti,omap5430-timer" },
|
||||
{}
|
||||
};
|
||||
|
||||
U_BOOT_DRIVER(omap_timer) = {
|
||||
.name = "omap_timer",
|
||||
.id = UCLASS_TIMER,
|
||||
.of_match = omap_timer_ids,
|
||||
.ofdata_to_platdata = omap_timer_ofdata_to_platdata,
|
||||
.priv_auto_alloc_size = sizeof(struct omap_timer_priv),
|
||||
.probe = omap_timer_probe,
|
||||
.ops = &omap_timer_ops,
|
||||
.flags = DM_FLAG_PRE_RELOC,
|
||||
};
|
||||
71
u-boot/drivers/timer/sandbox_timer.c
Normal file
71
u-boot/drivers/timer/sandbox_timer.c
Normal file
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw>
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0+
|
||||
*/
|
||||
|
||||
#include <common.h>
|
||||
#include <dm.h>
|
||||
#include <errno.h>
|
||||
#include <timer.h>
|
||||
#include <os.h>
|
||||
|
||||
#define SANDBOX_TIMER_RATE 1000000
|
||||
|
||||
/* system timer offset in ms */
|
||||
static unsigned long sandbox_timer_offset;
|
||||
|
||||
void sandbox_timer_add_offset(unsigned long offset)
|
||||
{
|
||||
sandbox_timer_offset += offset;
|
||||
}
|
||||
|
||||
u64 notrace timer_early_get_count(void)
|
||||
{
|
||||
return os_get_nsec() / 1000 + sandbox_timer_offset * 1000;
|
||||
}
|
||||
|
||||
unsigned long notrace timer_early_get_rate(void)
|
||||
{
|
||||
return SANDBOX_TIMER_RATE;
|
||||
}
|
||||
|
||||
static notrace int sandbox_timer_get_count(struct udevice *dev, u64 *count)
|
||||
{
|
||||
*count = timer_early_get_count();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int sandbox_timer_probe(struct udevice *dev)
|
||||
{
|
||||
struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
|
||||
|
||||
if (!uc_priv->clock_rate)
|
||||
uc_priv->clock_rate = SANDBOX_TIMER_RATE;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct timer_ops sandbox_timer_ops = {
|
||||
.get_count = sandbox_timer_get_count,
|
||||
};
|
||||
|
||||
static const struct udevice_id sandbox_timer_ids[] = {
|
||||
{ .compatible = "sandbox,timer" },
|
||||
{ }
|
||||
};
|
||||
|
||||
U_BOOT_DRIVER(sandbox_timer) = {
|
||||
.name = "sandbox_timer",
|
||||
.id = UCLASS_TIMER,
|
||||
.of_match = sandbox_timer_ids,
|
||||
.probe = sandbox_timer_probe,
|
||||
.ops = &sandbox_timer_ops,
|
||||
.flags = DM_FLAG_PRE_RELOC,
|
||||
};
|
||||
|
||||
/* This is here in case we don't have a device tree */
|
||||
U_BOOT_DEVICE(sandbox_timer_non_fdt) = {
|
||||
.name = "sandbox_timer",
|
||||
};
|
||||
118
u-boot/drivers/timer/timer-uclass.c
Normal file
118
u-boot/drivers/timer/timer-uclass.c
Normal file
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw>
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0+
|
||||
*/
|
||||
|
||||
#include <common.h>
|
||||
#include <dm.h>
|
||||
#include <dm/lists.h>
|
||||
#include <dm/device-internal.h>
|
||||
#include <errno.h>
|
||||
#include <timer.h>
|
||||
|
||||
DECLARE_GLOBAL_DATA_PTR;
|
||||
|
||||
/*
|
||||
* Implement a timer uclass to work with lib/time.c. The timer is usually
|
||||
* a 32/64 bits free-running up counter. The get_rate() method is used to get
|
||||
* the input clock frequency of the timer. The get_count() method is used
|
||||
* to get the current 64 bits count value. If the hardware is counting down,
|
||||
* the value should be inversed inside the method. There may be no real
|
||||
* tick, and no timer interrupt.
|
||||
*/
|
||||
|
||||
int notrace timer_get_count(struct udevice *dev, u64 *count)
|
||||
{
|
||||
const struct timer_ops *ops = device_get_ops(dev);
|
||||
|
||||
if (!ops->get_count)
|
||||
return -ENOSYS;
|
||||
|
||||
return ops->get_count(dev, count);
|
||||
}
|
||||
|
||||
unsigned long notrace timer_get_rate(struct udevice *dev)
|
||||
{
|
||||
struct timer_dev_priv *uc_priv = dev->uclass_priv;
|
||||
|
||||
return uc_priv->clock_rate;
|
||||
}
|
||||
|
||||
static int timer_pre_probe(struct udevice *dev)
|
||||
{
|
||||
struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
|
||||
|
||||
uc_priv->clock_rate = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
|
||||
"clock-frequency", 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int timer_post_probe(struct udevice *dev)
|
||||
{
|
||||
struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
|
||||
|
||||
if (!uc_priv->clock_rate)
|
||||
return -EINVAL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
u64 timer_conv_64(u32 count)
|
||||
{
|
||||
/* increment tbh if tbl has rolled over */
|
||||
if (count < gd->timebase_l)
|
||||
gd->timebase_h++;
|
||||
gd->timebase_l = count;
|
||||
return ((u64)gd->timebase_h << 32) | gd->timebase_l;
|
||||
}
|
||||
|
||||
int notrace dm_timer_init(void)
|
||||
{
|
||||
const void *blob = gd->fdt_blob;
|
||||
struct udevice *dev = NULL;
|
||||
int node;
|
||||
int ret;
|
||||
|
||||
if (gd->timer)
|
||||
return 0;
|
||||
|
||||
/* Check for a chosen timer to be used for tick */
|
||||
node = fdtdec_get_chosen_node(blob, "tick-timer");
|
||||
if (node < 0) {
|
||||
/* No chosen timer, trying first available timer */
|
||||
ret = uclass_first_device_err(UCLASS_TIMER, &dev);
|
||||
if (ret)
|
||||
return ret;
|
||||
} else {
|
||||
if (uclass_get_device_by_of_offset(UCLASS_TIMER, node, &dev)) {
|
||||
/*
|
||||
* If the timer is not marked to be bound before
|
||||
* relocation, bind it anyway.
|
||||
*/
|
||||
if (node > 0 &&
|
||||
!lists_bind_fdt(gd->dm_root, blob, node, &dev)) {
|
||||
ret = device_probe(dev);
|
||||
if (ret)
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (dev) {
|
||||
gd->timer = dev;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
UCLASS_DRIVER(timer) = {
|
||||
.id = UCLASS_TIMER,
|
||||
.name = "timer",
|
||||
.pre_probe = timer_pre_probe,
|
||||
.flags = DM_UC_FLAG_SEQ_ALIAS,
|
||||
.post_probe = timer_post_probe,
|
||||
.per_device_auto_alloc_size = sizeof(struct timer_dev_priv),
|
||||
};
|
||||
379
u-boot/drivers/timer/tsc_timer.c
Normal file
379
u-boot/drivers/timer/tsc_timer.c
Normal file
@@ -0,0 +1,379 @@
|
||||
/*
|
||||
* Copyright (c) 2012 The Chromium OS Authors.
|
||||
*
|
||||
* TSC calibration codes are adapted from Linux kernel
|
||||
* arch/x86/kernel/tsc_msr.c and arch/x86/kernel/tsc.c
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0+
|
||||
*/
|
||||
|
||||
#include <common.h>
|
||||
#include <dm.h>
|
||||
#include <malloc.h>
|
||||
#include <timer.h>
|
||||
#include <asm/io.h>
|
||||
#include <asm/i8254.h>
|
||||
#include <asm/ibmpc.h>
|
||||
#include <asm/msr.h>
|
||||
#include <asm/u-boot-x86.h>
|
||||
|
||||
/* CPU reference clock frequency: in KHz */
|
||||
#define FREQ_83 83200
|
||||
#define FREQ_100 99840
|
||||
#define FREQ_133 133200
|
||||
#define FREQ_166 166400
|
||||
|
||||
#define MAX_NUM_FREQS 8
|
||||
|
||||
DECLARE_GLOBAL_DATA_PTR;
|
||||
|
||||
/*
|
||||
* According to Intel 64 and IA-32 System Programming Guide,
|
||||
* if MSR_PERF_STAT[31] is set, the maximum resolved bus ratio can be
|
||||
* read in MSR_PLATFORM_ID[12:8], otherwise in MSR_PERF_STAT[44:40].
|
||||
* Unfortunately some Intel Atom SoCs aren't quite compliant to this,
|
||||
* so we need manually differentiate SoC families. This is what the
|
||||
* field msr_plat does.
|
||||
*/
|
||||
struct freq_desc {
|
||||
u8 x86_family; /* CPU family */
|
||||
u8 x86_model; /* model */
|
||||
/* 2: use 100MHz, 1: use MSR_PLATFORM_INFO, 0: MSR_IA32_PERF_STATUS */
|
||||
u8 msr_plat;
|
||||
u32 freqs[MAX_NUM_FREQS];
|
||||
};
|
||||
|
||||
static struct freq_desc freq_desc_tables[] = {
|
||||
/* PNW */
|
||||
{ 6, 0x27, 0, { 0, 0, 0, 0, 0, FREQ_100, 0, FREQ_83 } },
|
||||
/* CLV+ */
|
||||
{ 6, 0x35, 0, { 0, FREQ_133, 0, 0, 0, FREQ_100, 0, FREQ_83 } },
|
||||
/* TNG */
|
||||
{ 6, 0x4a, 1, { 0, FREQ_100, FREQ_133, 0, 0, 0, 0, 0 } },
|
||||
/* VLV2 */
|
||||
{ 6, 0x37, 1, { FREQ_83, FREQ_100, FREQ_133, FREQ_166, 0, 0, 0, 0 } },
|
||||
/* Ivybridge */
|
||||
{ 6, 0x3a, 2, { 0, 0, 0, 0, 0, 0, 0, 0 } },
|
||||
/* ANN */
|
||||
{ 6, 0x5a, 1, { FREQ_83, FREQ_100, FREQ_133, FREQ_100, 0, 0, 0, 0 } },
|
||||
};
|
||||
|
||||
static int match_cpu(u8 family, u8 model)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < ARRAY_SIZE(freq_desc_tables); i++) {
|
||||
if ((family == freq_desc_tables[i].x86_family) &&
|
||||
(model == freq_desc_tables[i].x86_model))
|
||||
return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Map CPU reference clock freq ID(0-7) to CPU reference clock freq(KHz) */
|
||||
#define id_to_freq(cpu_index, freq_id) \
|
||||
(freq_desc_tables[cpu_index].freqs[freq_id])
|
||||
|
||||
/*
|
||||
* Do MSR calibration only for known/supported CPUs.
|
||||
*
|
||||
* Returns the calibration value or 0 if MSR calibration failed.
|
||||
*/
|
||||
static unsigned long __maybe_unused try_msr_calibrate_tsc(void)
|
||||
{
|
||||
u32 lo, hi, ratio, freq_id, freq;
|
||||
unsigned long res;
|
||||
int cpu_index;
|
||||
|
||||
cpu_index = match_cpu(gd->arch.x86, gd->arch.x86_model);
|
||||
if (cpu_index < 0)
|
||||
return 0;
|
||||
|
||||
if (freq_desc_tables[cpu_index].msr_plat) {
|
||||
rdmsr(MSR_PLATFORM_INFO, lo, hi);
|
||||
ratio = (lo >> 8) & 0x1f;
|
||||
} else {
|
||||
rdmsr(MSR_IA32_PERF_STATUS, lo, hi);
|
||||
ratio = (hi >> 8) & 0x1f;
|
||||
}
|
||||
debug("Maximum core-clock to bus-clock ratio: 0x%x\n", ratio);
|
||||
|
||||
if (!ratio)
|
||||
goto fail;
|
||||
|
||||
if (freq_desc_tables[cpu_index].msr_plat == 2) {
|
||||
/* TODO: Figure out how best to deal with this */
|
||||
freq = FREQ_100;
|
||||
debug("Using frequency: %u KHz\n", freq);
|
||||
} else {
|
||||
/* Get FSB FREQ ID */
|
||||
rdmsr(MSR_FSB_FREQ, lo, hi);
|
||||
freq_id = lo & 0x7;
|
||||
freq = id_to_freq(cpu_index, freq_id);
|
||||
debug("Resolved frequency ID: %u, frequency: %u KHz\n",
|
||||
freq_id, freq);
|
||||
}
|
||||
if (!freq)
|
||||
goto fail;
|
||||
|
||||
/* TSC frequency = maximum resolved freq * maximum resolved bus ratio */
|
||||
res = freq * ratio / 1000;
|
||||
debug("TSC runs at %lu MHz\n", res);
|
||||
|
||||
return res;
|
||||
|
||||
fail:
|
||||
debug("Fast TSC calibration using MSR failed\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* This reads the current MSB of the PIT counter, and
|
||||
* checks if we are running on sufficiently fast and
|
||||
* non-virtualized hardware.
|
||||
*
|
||||
* Our expectations are:
|
||||
*
|
||||
* - the PIT is running at roughly 1.19MHz
|
||||
*
|
||||
* - each IO is going to take about 1us on real hardware,
|
||||
* but we allow it to be much faster (by a factor of 10) or
|
||||
* _slightly_ slower (ie we allow up to a 2us read+counter
|
||||
* update - anything else implies a unacceptably slow CPU
|
||||
* or PIT for the fast calibration to work.
|
||||
*
|
||||
* - with 256 PIT ticks to read the value, we have 214us to
|
||||
* see the same MSB (and overhead like doing a single TSC
|
||||
* read per MSB value etc).
|
||||
*
|
||||
* - We're doing 2 reads per loop (LSB, MSB), and we expect
|
||||
* them each to take about a microsecond on real hardware.
|
||||
* So we expect a count value of around 100. But we'll be
|
||||
* generous, and accept anything over 50.
|
||||
*
|
||||
* - if the PIT is stuck, and we see *many* more reads, we
|
||||
* return early (and the next caller of pit_expect_msb()
|
||||
* then consider it a failure when they don't see the
|
||||
* next expected value).
|
||||
*
|
||||
* These expectations mean that we know that we have seen the
|
||||
* transition from one expected value to another with a fairly
|
||||
* high accuracy, and we didn't miss any events. We can thus
|
||||
* use the TSC value at the transitions to calculate a pretty
|
||||
* good value for the TSC frequencty.
|
||||
*/
|
||||
static inline int pit_verify_msb(unsigned char val)
|
||||
{
|
||||
/* Ignore LSB */
|
||||
inb(0x42);
|
||||
return inb(0x42) == val;
|
||||
}
|
||||
|
||||
static inline int pit_expect_msb(unsigned char val, u64 *tscp,
|
||||
unsigned long *deltap)
|
||||
{
|
||||
int count;
|
||||
u64 tsc = 0, prev_tsc = 0;
|
||||
|
||||
for (count = 0; count < 50000; count++) {
|
||||
if (!pit_verify_msb(val))
|
||||
break;
|
||||
prev_tsc = tsc;
|
||||
tsc = rdtsc();
|
||||
}
|
||||
*deltap = rdtsc() - prev_tsc;
|
||||
*tscp = tsc;
|
||||
|
||||
/*
|
||||
* We require _some_ success, but the quality control
|
||||
* will be based on the error terms on the TSC values.
|
||||
*/
|
||||
return count > 5;
|
||||
}
|
||||
|
||||
/*
|
||||
* How many MSB values do we want to see? We aim for
|
||||
* a maximum error rate of 500ppm (in practice the
|
||||
* real error is much smaller), but refuse to spend
|
||||
* more than 50ms on it.
|
||||
*/
|
||||
#define MAX_QUICK_PIT_MS 50
|
||||
#define MAX_QUICK_PIT_ITERATIONS (MAX_QUICK_PIT_MS * PIT_TICK_RATE / 1000 / 256)
|
||||
|
||||
static unsigned long __maybe_unused quick_pit_calibrate(void)
|
||||
{
|
||||
int i;
|
||||
u64 tsc, delta;
|
||||
unsigned long d1, d2;
|
||||
|
||||
/* Set the Gate high, disable speaker */
|
||||
outb((inb(0x61) & ~0x02) | 0x01, 0x61);
|
||||
|
||||
/*
|
||||
* Counter 2, mode 0 (one-shot), binary count
|
||||
*
|
||||
* NOTE! Mode 2 decrements by two (and then the
|
||||
* output is flipped each time, giving the same
|
||||
* final output frequency as a decrement-by-one),
|
||||
* so mode 0 is much better when looking at the
|
||||
* individual counts.
|
||||
*/
|
||||
outb(0xb0, 0x43);
|
||||
|
||||
/* Start at 0xffff */
|
||||
outb(0xff, 0x42);
|
||||
outb(0xff, 0x42);
|
||||
|
||||
/*
|
||||
* The PIT starts counting at the next edge, so we
|
||||
* need to delay for a microsecond. The easiest way
|
||||
* to do that is to just read back the 16-bit counter
|
||||
* once from the PIT.
|
||||
*/
|
||||
pit_verify_msb(0);
|
||||
|
||||
if (pit_expect_msb(0xff, &tsc, &d1)) {
|
||||
for (i = 1; i <= MAX_QUICK_PIT_ITERATIONS; i++) {
|
||||
if (!pit_expect_msb(0xff-i, &delta, &d2))
|
||||
break;
|
||||
|
||||
/*
|
||||
* Iterate until the error is less than 500 ppm
|
||||
*/
|
||||
delta -= tsc;
|
||||
if (d1+d2 >= delta >> 11)
|
||||
continue;
|
||||
|
||||
/*
|
||||
* Check the PIT one more time to verify that
|
||||
* all TSC reads were stable wrt the PIT.
|
||||
*
|
||||
* This also guarantees serialization of the
|
||||
* last cycle read ('d2') in pit_expect_msb.
|
||||
*/
|
||||
if (!pit_verify_msb(0xfe - i))
|
||||
break;
|
||||
goto success;
|
||||
}
|
||||
}
|
||||
debug("Fast TSC calibration failed\n");
|
||||
return 0;
|
||||
|
||||
success:
|
||||
/*
|
||||
* Ok, if we get here, then we've seen the
|
||||
* MSB of the PIT decrement 'i' times, and the
|
||||
* error has shrunk to less than 500 ppm.
|
||||
*
|
||||
* As a result, we can depend on there not being
|
||||
* any odd delays anywhere, and the TSC reads are
|
||||
* reliable (within the error).
|
||||
*
|
||||
* kHz = ticks / time-in-seconds / 1000;
|
||||
* kHz = (t2 - t1) / (I * 256 / PIT_TICK_RATE) / 1000
|
||||
* kHz = ((t2 - t1) * PIT_TICK_RATE) / (I * 256 * 1000)
|
||||
*/
|
||||
delta *= PIT_TICK_RATE;
|
||||
delta /= (i*256*1000);
|
||||
debug("Fast TSC calibration using PIT\n");
|
||||
return delta / 1000;
|
||||
}
|
||||
|
||||
/* Get the speed of the TSC timer in MHz */
|
||||
unsigned notrace long get_tbclk_mhz(void)
|
||||
{
|
||||
return get_tbclk() / 1000000;
|
||||
}
|
||||
|
||||
static ulong get_ms_timer(void)
|
||||
{
|
||||
return (get_ticks() * 1000) / get_tbclk();
|
||||
}
|
||||
|
||||
ulong get_timer(ulong base)
|
||||
{
|
||||
return get_ms_timer() - base;
|
||||
}
|
||||
|
||||
ulong notrace timer_get_us(void)
|
||||
{
|
||||
return get_ticks() / get_tbclk_mhz();
|
||||
}
|
||||
|
||||
ulong timer_get_boot_us(void)
|
||||
{
|
||||
return timer_get_us();
|
||||
}
|
||||
|
||||
void __udelay(unsigned long usec)
|
||||
{
|
||||
u64 now = get_ticks();
|
||||
u64 stop;
|
||||
|
||||
stop = now + usec * get_tbclk_mhz();
|
||||
|
||||
while ((int64_t)(stop - get_ticks()) > 0)
|
||||
#if defined(CONFIG_QEMU) && defined(CONFIG_SMP)
|
||||
/*
|
||||
* Add a 'pause' instruction on qemu target,
|
||||
* to give other VCPUs a chance to run.
|
||||
*/
|
||||
asm volatile("pause");
|
||||
#else
|
||||
;
|
||||
#endif
|
||||
}
|
||||
|
||||
static int tsc_timer_get_count(struct udevice *dev, u64 *count)
|
||||
{
|
||||
u64 now_tick = rdtsc();
|
||||
|
||||
*count = now_tick - gd->arch.tsc_base;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int tsc_timer_probe(struct udevice *dev)
|
||||
{
|
||||
struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
|
||||
|
||||
gd->arch.tsc_base = rdtsc();
|
||||
|
||||
/*
|
||||
* If there is no clock frequency specified in the device tree,
|
||||
* calibrate it by ourselves.
|
||||
*/
|
||||
if (!uc_priv->clock_rate) {
|
||||
unsigned long fast_calibrate;
|
||||
|
||||
fast_calibrate = try_msr_calibrate_tsc();
|
||||
if (!fast_calibrate) {
|
||||
fast_calibrate = quick_pit_calibrate();
|
||||
if (!fast_calibrate)
|
||||
panic("TSC frequency is ZERO");
|
||||
}
|
||||
|
||||
uc_priv->clock_rate = fast_calibrate * 1000000;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct timer_ops tsc_timer_ops = {
|
||||
.get_count = tsc_timer_get_count,
|
||||
};
|
||||
|
||||
static const struct udevice_id tsc_timer_ids[] = {
|
||||
{ .compatible = "x86,tsc-timer", },
|
||||
{ }
|
||||
};
|
||||
|
||||
U_BOOT_DRIVER(tsc_timer) = {
|
||||
.name = "tsc_timer",
|
||||
.id = UCLASS_TIMER,
|
||||
.of_match = tsc_timer_ids,
|
||||
.probe = tsc_timer_probe,
|
||||
.ops = &tsc_timer_ops,
|
||||
.flags = DM_FLAG_PRE_RELOC,
|
||||
};
|
||||
Reference in New Issue
Block a user