From e67606cf8e023de469c011ecb2a18a06d48a66a4 Mon Sep 17 00:00:00 2001 From: Jun Nie Date: Wed, 4 Jul 2018 15:51:20 +0800 Subject: [PATCH] drivers: imx: imx_gpt: Add general purpose timer API binding Add delay timer API so that it can be called by delay timer layer and used as delay timer globally. [bod: changed name from imx_delay_timer -> imx_gpt ] Signed-off-by: Jun Nie Reviewed-by: Bryan O'Donoghue Signed-off-by: Bryan O'Donoghue --- drivers/imx/timer/imx_gpt.c | 60 +++++++++++++++++++++++++++++++++++++ drivers/imx/timer/imx_gpt.h | 14 +++++++++ 2 files changed, 74 insertions(+) create mode 100644 drivers/imx/timer/imx_gpt.c create mode 100644 drivers/imx/timer/imx_gpt.h diff --git a/drivers/imx/timer/imx_gpt.c b/drivers/imx/timer/imx_gpt.c new file mode 100644 index 00000000..bd364eb3 --- /dev/null +++ b/drivers/imx/timer/imx_gpt.c @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include +#include +#include +#include + +#define GPTCR_SWR BIT(15) /* Software reset */ +#define GPTCR_24MEN BIT(10) /* Enable 24MHz clock input */ +#define GPTCR_CLKSOURCE_OSC (5 << 6) /* Clock source OSC */ +#define GPTCR_CLKSOURCE_MASK (0x7 << 6) +#define GPTCR_TEN 1 /* Timer enable */ + +#define GPTPR_PRESCL_24M_SHIFT 12 + +#define SYS_COUNTER_FREQ_IN_MHZ 3 + +#define GPTPR_TIMER_CTRL (imx_base_addr + 0x000) +#define GPTPR_TIMER_PRESCL (imx_base_addr + 0x004) +#define GPTPR_TIMER_CNTR (imx_base_addr + 0x024) + +static uintptr_t imx_base_addr; + +uint32_t imx_get_timer_value(void) +{ + return ~mmio_read_32(GPTPR_TIMER_CNTR); +} + +static const timer_ops_t imx_gpt_ops = { + .get_timer_value = imx_get_timer_value, + .clk_mult = 1, + .clk_div = SYS_COUNTER_FREQ_IN_MHZ, +}; + +void imx_gpt_ops_init(uintptr_t base_addr) +{ + int val; + + assert(base_addr != 0); + + imx_base_addr = base_addr; + + /* setup GP Timer */ + mmio_write_32(GPTPR_TIMER_CTRL, GPTCR_SWR); + mmio_write_32(GPTPR_TIMER_CTRL, 0); + + /* get 3MHz from 24MHz */ + mmio_write_32(GPTPR_TIMER_PRESCL, (7 << GPTPR_PRESCL_24M_SHIFT)); + + val = mmio_read_32(GPTPR_TIMER_CTRL); + val &= ~GPTCR_CLKSOURCE_MASK; + val |= GPTCR_24MEN | GPTCR_CLKSOURCE_OSC | GPTCR_TEN; + mmio_write_32(GPTPR_TIMER_CTRL, val); + + timer_init(&imx_gpt_ops); +} diff --git a/drivers/imx/timer/imx_gpt.h b/drivers/imx/timer/imx_gpt.h new file mode 100644 index 00000000..6416c3cb --- /dev/null +++ b/drivers/imx/timer/imx_gpt.h @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#ifndef __IMX_GPT_H__ +#define __IMX_GPT_H__ + +#include + +void imx_gpt_ops_init(uintptr_t reg_base); + +#endif /* __IMX_GPT_H__ */ -- 2.30.2