TPL: TM2: timer: Support for PMW timer (PWM4)
authorLukasz Majewski <l.majewski@samsung.com>
Thu, 12 May 2016 10:50:03 +0000 (12:50 +0200)
committerJaehoon Chung <jh80.chung@samsung.com>
Tue, 20 Oct 2020 01:28:23 +0000 (10:28 +0900)
Signed-off-by: Lukasz Majewski <l.majewski@samsung.com>
arch/arm/cpu/armv8/Makefile
arch/arm/cpu/armv8/exynos-common/Makefile [new file with mode: 0644]
arch/arm/cpu/armv8/exynos-common/cpu_info.c [new file with mode: 0644]
arch/arm/cpu/armv8/exynos-common/pwm.c [new file with mode: 0644]
arch/arm/cpu/armv8/exynos-common/timer.c [new file with mode: 0644]
arch/arm/include/asm/arch-exynos5433/cpu.h [new file with mode: 0644]
arch/arm/mach-exynos/Makefile

index 93d26f9..b6cfb29 100644 (file)
@@ -41,3 +41,4 @@ obj-$(CONFIG_ARMV8_PSCI) += psci.o
 obj-$(CONFIG_ARCH_SUNXI) += lowlevel_init.o
 obj-$(CONFIG_TARGET_BCMNS3) += bcmns3/
 obj-$(CONFIG_XEN) += xen/
+obj-$(CONFIG_TPL_TM2) += exynos-common/
diff --git a/arch/arm/cpu/armv8/exynos-common/Makefile b/arch/arm/cpu/armv8/exynos-common/Makefile
new file mode 100644 (file)
index 0000000..421838b
--- /dev/null
@@ -0,0 +1,12 @@
+#
+# Copyright (C) 2009 Samsung Electronics
+# Minkyu Kang <mk7.kang@samsung.com>
+#
+# SPDX-License-Identifier:     GPL-2.0+
+#
+
+obj-y          += cpu_info.o
+ifndef CONFIG_SPL_BUILD
+obj-y          += timer.o
+obj-$(CONFIG_PWM)      += pwm.o
+endif
diff --git a/arch/arm/cpu/armv8/exynos-common/cpu_info.c b/arch/arm/cpu/armv8/exynos-common/cpu_info.c
new file mode 100644 (file)
index 0000000..764c661
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2009 Samsung Electronics
+ * Minkyu Kang <mk7.kang@samsung.com>
+ *
+ * SPDX-License-Identifier:    GPL-2.0+
+ */
+#include <common.h>
+#include <fdtdec.h>
+#include <asm/io.h>
+#include <asm/arch/clk.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+/* Default is s5pc100 */
+unsigned int s5p_cpu_id = 0xC100;
+/* Default is EVT1 */
+unsigned int s5p_cpu_rev = 1;
+
+#ifdef CONFIG_ARCH_CPU_INIT
+int arch_cpu_init(void)
+{
+       s5p_set_cpu_id();
+
+       return 0;
+}
+#endif
+
+u32 get_device_type(void)
+{
+       return s5p_cpu_id;
+}
+
+#ifdef CONFIG_DISPLAY_CPUINFO
+int print_cpuinfo(void)
+{
+       const char *cpu_model;
+       int len;
+
+       /* For SoC with no real CPU ID in naming convention. */
+       cpu_model = fdt_getprop(gd->fdt_blob, 0, "cpu-model", &len);
+       if (cpu_model)
+               printf("CPU:   %.*s @ ", len, cpu_model);
+       else
+               printf("CPU:   %s%X @ ", s5p_get_cpu_name(), s5p_cpu_id);
+
+       print_freq(get_arm_clk(), "\n");
+
+       return 0;
+}
+#endif
diff --git a/arch/arm/cpu/armv8/exynos-common/pwm.c b/arch/arm/cpu/armv8/exynos-common/pwm.c
new file mode 100644 (file)
index 0000000..8984d30
--- /dev/null
@@ -0,0 +1,172 @@
+/*
+ * Copyright (C) 2011 Samsung Electronics
+ *
+ * Donghwa Lee <dh09.lee@samsung.com>
+ *
+ * SPDX-License-Identifier:    GPL-2.0+
+ */
+
+#include <common.h>
+#include <errno.h>
+#include <pwm.h>
+#include <asm/io.h>
+#include <asm/arch/pwm.h>
+#include <asm/arch/clk.h>
+#include <asm/arch/cpu.h>
+
+int pwm_enable(int pwm_id)
+{
+       const struct s5p_timer *pwm =
+                       (struct s5p_timer *)samsung_get_base_timer();
+       unsigned long tcon;
+
+       tcon = readl(&pwm->tcon);
+       tcon |= TCON_START(pwm_id);
+
+       writel(tcon, &pwm->tcon);
+
+       return 0;
+}
+
+void pwm_disable(int pwm_id)
+{
+       const struct s5p_timer *pwm =
+                       (struct s5p_timer *)samsung_get_base_timer();
+       unsigned long tcon;
+
+       tcon = readl(&pwm->tcon);
+       tcon &= ~TCON_START(pwm_id);
+
+       writel(tcon, &pwm->tcon);
+}
+
+static unsigned long pwm_calc_tin(int pwm_id, unsigned long freq)
+{
+       unsigned long tin_parent_rate;
+       unsigned int div;
+
+       tin_parent_rate = get_pwm_clk();
+
+       for (div = 2; div <= 16; div *= 2) {
+               if ((tin_parent_rate / (div << 16)) < freq)
+                       return tin_parent_rate / div;
+       }
+
+       return tin_parent_rate / 16;
+}
+
+#define NS_IN_SEC 1000000000UL
+
+int pwm_config(int pwm_id, int duty_ns, int period_ns)
+{
+       const struct s5p_timer *pwm =
+                       (struct s5p_timer *)samsung_get_base_timer();
+       unsigned int offset;
+       unsigned long tin_rate;
+       unsigned long tin_ns;
+       unsigned long frequency;
+       unsigned long tcon;
+       unsigned long tcnt;
+       unsigned long tcmp;
+
+       /*
+        * We currently avoid using 64bit arithmetic by using the
+        * fact that anything faster than 1GHz is easily representable
+        * by 32bits.
+        */
+       if (period_ns > NS_IN_SEC || duty_ns > NS_IN_SEC || period_ns == 0)
+               return -ERANGE;
+
+       if (duty_ns > period_ns)
+               return -EINVAL;
+
+       frequency = NS_IN_SEC / period_ns;
+
+       /* Check to see if we are changing the clock rate of the PWM */
+       tin_rate = pwm_calc_tin(pwm_id, frequency);
+
+       tin_ns = NS_IN_SEC / tin_rate;
+       tcnt = period_ns / tin_ns;
+
+       /* Note, counters count down */
+       tcmp = duty_ns / tin_ns;
+       tcmp = tcnt - tcmp;
+
+       /* Update the PWM register block. */
+       offset = pwm_id * 3;
+       if (pwm_id < 4) {
+               writel(tcnt, &pwm->tcntb0 + offset);
+               writel(tcmp, &pwm->tcmpb0 + offset);
+       }
+
+       tcon = readl(&pwm->tcon);
+       tcon |= TCON_UPDATE(pwm_id);
+       if (pwm_id < 4)
+               tcon |= TCON_AUTO_RELOAD(pwm_id);
+       else
+               tcon |= TCON4_AUTO_RELOAD;
+       writel(tcon, &pwm->tcon);
+
+       tcon &= ~TCON_UPDATE(pwm_id);
+       writel(tcon, &pwm->tcon);
+
+       return 0;
+}
+
+int pwm_init(int pwm_id, int div, int invert)
+{
+       u32 val;
+       const struct s5p_timer *pwm =
+                       (struct s5p_timer *)samsung_get_base_timer();
+       unsigned long ticks_per_period;
+       unsigned int offset, prescaler;
+
+       /*
+        * Timer Freq(HZ) =
+        *      PWM_CLK / { (prescaler_value + 1) * (divider_value) }
+        */
+
+       val = readl(&pwm->tcfg0);
+       if (pwm_id < 2) {
+               prescaler = PRESCALER_0;
+               val &= ~0xff;
+               val |= (prescaler & 0xff);
+       } else {
+               prescaler = PRESCALER_1;
+               val &= ~(0xff << 8);
+               val |= (prescaler & 0xff) << 8;
+       }
+       writel(val, &pwm->tcfg0);
+       val = readl(&pwm->tcfg1);
+       val &= ~(0xf << MUX_DIV_SHIFT(pwm_id));
+       val |= (div & 0xf) << MUX_DIV_SHIFT(pwm_id);
+       writel(val, &pwm->tcfg1);
+
+       if (pwm_id == 4) {
+               /*
+                * TODO(sjg): Use this as a countdown timer for now. We count
+                * down from the maximum value to 0, then reset.
+                */
+               ticks_per_period = -1UL;
+       } else {
+               const unsigned long pwm_hz = 1000;
+               unsigned long timer_rate_hz = get_pwm_clk() /
+                       ((prescaler + 1) * (1 << div));
+
+               ticks_per_period = timer_rate_hz / pwm_hz;
+       }
+
+       /* set count value */
+       offset = pwm_id * 3;
+
+       writel(ticks_per_period, &pwm->tcntb0 + offset);
+
+       val = readl(&pwm->tcon) & ~(0xf << TCON_OFFSET(pwm_id));
+       if (invert && (pwm_id < 4))
+               val |= TCON_INVERTER(pwm_id);
+       writel(val, &pwm->tcon);
+
+       pwm_enable(pwm_id);
+
+       return 0;
+}
diff --git a/arch/arm/cpu/armv8/exynos-common/timer.c b/arch/arm/cpu/armv8/exynos-common/timer.c
new file mode 100644 (file)
index 0000000..5cc51c0
--- /dev/null
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2009 Samsung Electronics
+ * Heungjun Kim <riverful.kim@samsung.com>
+ * Inki Dae <inki.dae@samsung.com>
+ * Minkyu Kang <mk7.kang@samsung.com>
+ *
+ * SPDX-License-Identifier:    GPL-2.0+
+ */
+
+#include <common.h>
+#include <div64.h>
+#include <asm/io.h>
+#include <asm/arch/pwm.h>
+#include <asm/arch/clk.h>
+#include <asm/arch/cpu.h>
+#include <pwm.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+unsigned long get_current_tick(void);
+
+/* macro to read the 16 bit timer */
+static inline struct s5p_timer *s5p_get_base_timer(void)
+{
+       return (struct s5p_timer *)samsung_get_base_timer();
+}
+
+/**
+ * Read the countdown timer.
+ *
+ * This operates at 1MHz and counts downwards. It will wrap about every
+ * hour (2^32 microseconds).
+ *
+ * @return current value of timer
+ */
+static unsigned long timer_get_us_down(void)
+{
+       struct s5p_timer *const timer = s5p_get_base_timer();
+
+       return readl(&timer->tcnto4);
+}
+
+int timer_init(void)
+{
+       /* PWM Timer 4 */
+       pwm_init(4, MUX_DIV_4, 0);
+       pwm_config(4, 100000, 100000);
+       pwm_enable(4);
+
+       /* Use this as the current monotonic time in us */
+       gd->arch.timer_reset_value = 0;
+
+       /* Use this as the last timer value we saw */
+       gd->arch.lastinc = timer_get_us_down();
+       reset_timer_masked();
+
+       return 0;
+}
+
+/*
+ * timer without interrupts
+ */
+unsigned long get_timer(unsigned long base)
+{
+       unsigned long long time_ms;
+
+       ulong now = timer_get_us_down();
+
+       /*
+        * Increment the time by the amount elapsed since the last read.
+        * The timer may have wrapped around, but it makes no difference to
+        * our arithmetic here.
+        */
+       gd->arch.timer_reset_value += gd->arch.lastinc - now;
+       gd->arch.lastinc = now;
+
+       /* Divide by 1000 to convert from us to ms */
+       time_ms = gd->arch.timer_reset_value;
+       do_div(time_ms, 1000);
+       return time_ms - base;
+}
+
+unsigned long __attribute__((no_instrument_function)) timer_get_us(void)
+{
+       static unsigned long base_time_us;
+
+       struct s5p_timer *const timer =
+               (struct s5p_timer *)samsung_get_base_timer();
+       unsigned long now_downward_us = readl(&timer->tcnto4);
+
+       if (!base_time_us)
+               base_time_us = now_downward_us;
+
+       /* Note that this timer counts downward. */
+       return base_time_us - now_downward_us;
+}
+
+/* delay x useconds */
+void __udelay(unsigned long usec)
+{
+       unsigned long count_value;
+
+       count_value = timer_get_us_down();
+       while ((int)(count_value - timer_get_us_down()) < (int)usec)
+               ;
+}
+
+void reset_timer_masked(void)
+{
+       struct s5p_timer *const timer = s5p_get_base_timer();
+
+       /* reset time */
+       gd->arch.lastinc = readl(&timer->tcnto4);
+       gd->arch.tbl = 0;
+}
diff --git a/arch/arm/include/asm/arch-exynos5433/cpu.h b/arch/arm/include/asm/arch-exynos5433/cpu.h
new file mode 100644 (file)
index 0000000..9eab21f
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * (C) Copyright 2016 Samsung Electronics
+ * Lukasz Majewski
+ *
+ * SPDX-License-Identifier:    GPL-2.0+
+ */
+
+#ifndef _EXYNOS64_CPU_H
+#define _EXYNOS64_CPU_H
+
+#define DEVICE_NOT_AVAILABLE           0
+
+#define EXYNOS_CPU_NAME                        "Exynos5433"
+
+/* EXYNOS5433 */
+#define EXYNOS5433_CLOCK_BASE          0x10040000
+#define EXYNOS5433_PWMTIMER_BASE       0x14DD0000
+
+
+#ifndef __ASSEMBLY__
+#include <asm/io.h>
+/* CPU detection macros */
+extern unsigned int s5p_cpu_id;
+extern unsigned int s5p_cpu_rev;
+
+static inline int s5p_get_cpu_rev(void)
+{
+       return s5p_cpu_rev;
+}
+
+static inline void s5p_set_cpu_id(void)
+{
+       return;
+}
+
+static inline char *s5p_get_cpu_name(void)
+{
+       return EXYNOS_CPU_NAME;
+}
+
+#define SAMSUNG_BASE(device, base)                             \
+static inline unsigned long __attribute__((no_instrument_function)) \
+       samsung_get_base_##device(void) \
+{                                                              \
+       return EXYNOS5433_##base;               \
+}
+
+SAMSUNG_BASE(clock, CLOCK_BASE)
+SAMSUNG_BASE(timer, PWMTIMER_BASE)
+#endif
+
+#endif /* _EXYNOS64_CPU_H */
index e895c13..a83029a 100644 (file)
@@ -3,7 +3,10 @@
 # Copyright (C) 2009 Samsung Electronics
 # Minkyu Kang <mk7.kang@samsung.com>
 
+ifndef CONFIG_TPL_TM2
 obj-y  += soc.o
+endif
+
 obj-$(CONFIG_CPU_V7A) += clock.o pinmux.o power.o system.o
 obj-$(CONFIG_ARM64)    += mmu-arm64.o