blob: a254e295cbfc95bc77e15c97d9e177ab2affe07d [file] [log] [blame]
Andre Przywara31ab0fd2022-10-20 23:10:23 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * ARM PrimeCell Dual-Timer Module (SP804) driver
4 * Copyright (C) 2022 Arm Ltd.
5 */
6
Andre Przywara31ab0fd2022-10-20 23:10:23 +01007#include <clk.h>
8#include <dm.h>
9#include <init.h>
10#include <log.h>
11#include <asm/global_data.h>
12#include <dm/ofnode.h>
13#include <mapmem.h>
14#include <dt-structs.h>
15#include <timer.h>
16#include <asm/io.h>
17
18DECLARE_GLOBAL_DATA_PTR;
19
20#define SP804_TIMERX_LOAD 0x00
21#define SP804_TIMERX_VALUE 0x04
22#define SP804_TIMERX_CONTROL 0x08
23
24#define SP804_CTRL_TIMER_ENABLE (1U << 7)
25#define SP804_CTRL_TIMER_PERIODIC (1U << 6)
26#define SP804_CTRL_INT_ENABLE (1U << 5)
27#define SP804_CTRL_TIMER_PRESCALE_SHIFT 2
28#define SP804_CTRL_TIMER_PRESCALE_MASK (3U << SP804_CTRL_TIMER_PRESCALE_SHIFT)
29#define SP804_CTRL_TIMER_32BIT (1U << 1)
30#define SP804_CTRL_ONESHOT (1U << 0)
31
32
33struct sp804_timer_plat {
34 uintptr_t base;
35};
36
37static u64 sp804_timer_get_count(struct udevice *dev)
38{
39 struct sp804_timer_plat *plat = dev_get_plat(dev);
40 uint32_t cntr = readl(plat->base + SP804_TIMERX_VALUE);
41
42 /* timers are down-counting */
43 return ~0u - cntr;
44}
45
46static int sp804_clk_of_to_plat(struct udevice *dev)
47{
48 struct sp804_timer_plat *plat = dev_get_plat(dev);
49
50 plat->base = dev_read_addr(dev);
51 if (!plat->base)
52 return -ENOENT;
53
54 return 0;
55}
56
57static int sp804_timer_probe(struct udevice *dev)
58{
59 struct sp804_timer_plat *plat = dev_get_plat(dev);
60 struct timer_dev_priv *uc_priv = dev_get_uclass_priv(dev);
61 struct clk base_clk;
62 unsigned int divider = 1;
63 uint32_t ctlr;
64 int ret;
65
66 ctlr = readl(plat->base + SP804_TIMERX_CONTROL);
67 ctlr &= SP804_CTRL_TIMER_PRESCALE_MASK;
68 switch (ctlr >> SP804_CTRL_TIMER_PRESCALE_SHIFT) {
69 case 0x0: divider = 1; break;
70 case 0x1: divider = 16; break;
71 case 0x2: divider = 256; break;
72 case 0x3: printf("illegal!\n"); break;
73 }
74
75 ret = clk_get_by_index(dev, 0, &base_clk);
76 if (ret) {
77 printf("could not find SP804 timer base clock in DT\n");
78 return ret;
79 }
80
81 uc_priv->clock_rate = clk_get_rate(&base_clk) / divider;
82
83 /* keep divider, free-running, wrapping, no IRQs, 32-bit mode */
84 ctlr |= SP804_CTRL_TIMER_32BIT | SP804_CTRL_TIMER_ENABLE;
85 writel(ctlr, plat->base + SP804_TIMERX_CONTROL);
86
87 return 0;
88}
89
90static const struct timer_ops sp804_timer_ops = {
91 .get_count = sp804_timer_get_count,
92};
93
94static const struct udevice_id sp804_timer_ids[] = {
95 { .compatible = "arm,sp804" },
96 {}
97};
98
99U_BOOT_DRIVER(arm_sp804_timer) = {
100 .name = "arm_sp804_timer",
101 .id = UCLASS_TIMER,
102 .of_match = sp804_timer_ids,
103 .probe = sp804_timer_probe,
104 .ops = &sp804_timer_ops,
105 .plat_auto = sizeof(struct sp804_timer_plat),
106 .of_to_plat = sp804_clk_of_to_plat,
107};