1f5076f86Srick /*
2f5076f86Srick * Andestech ATFTMR010 timer driver
3f5076f86Srick *
4f5076f86Srick * (C) Copyright 2016
5f5076f86Srick * Rick Chen, NDS32 Software Engineering, rick@andestech.com
6f5076f86Srick *
7f5076f86Srick * SPDX-License-Identifier: GPL-2.0+
8f5076f86Srick */
9f5076f86Srick #include <common.h>
10f5076f86Srick #include <dm.h>
11f5076f86Srick #include <errno.h>
12f5076f86Srick #include <timer.h>
13f5076f86Srick #include <linux/io.h>
14f5076f86Srick
15f5076f86Srick DECLARE_GLOBAL_DATA_PTR;
16f5076f86Srick
17f5076f86Srick /*
18f5076f86Srick * Timer Control Register
19f5076f86Srick */
20f5076f86Srick #define T3_UPDOWN (1 << 11)
21f5076f86Srick #define T2_UPDOWN (1 << 10)
22f5076f86Srick #define T1_UPDOWN (1 << 9)
23f5076f86Srick #define T3_OFENABLE (1 << 8)
24f5076f86Srick #define T3_CLOCK (1 << 7)
25f5076f86Srick #define T3_ENABLE (1 << 6)
26f5076f86Srick #define T2_OFENABLE (1 << 5)
27f5076f86Srick #define T2_CLOCK (1 << 4)
28f5076f86Srick #define T2_ENABLE (1 << 3)
29f5076f86Srick #define T1_OFENABLE (1 << 2)
30f5076f86Srick #define T1_CLOCK (1 << 1)
31f5076f86Srick #define T1_ENABLE (1 << 0)
32f5076f86Srick
33f5076f86Srick /*
34f5076f86Srick * Timer Interrupt State & Mask Registers
35f5076f86Srick */
36f5076f86Srick #define T3_OVERFLOW (1 << 8)
37f5076f86Srick #define T3_MATCH2 (1 << 7)
38f5076f86Srick #define T3_MATCH1 (1 << 6)
39f5076f86Srick #define T2_OVERFLOW (1 << 5)
40f5076f86Srick #define T2_MATCH2 (1 << 4)
41f5076f86Srick #define T2_MATCH1 (1 << 3)
42f5076f86Srick #define T1_OVERFLOW (1 << 2)
43f5076f86Srick #define T1_MATCH2 (1 << 1)
44f5076f86Srick #define T1_MATCH1 (1 << 0)
45f5076f86Srick
46f5076f86Srick struct atftmr_timer_regs {
47f5076f86Srick u32 t1_counter; /* 0x00 */
48f5076f86Srick u32 t1_load; /* 0x04 */
49f5076f86Srick u32 t1_match1; /* 0x08 */
50f5076f86Srick u32 t1_match2; /* 0x0c */
51f5076f86Srick u32 t2_counter; /* 0x10 */
52f5076f86Srick u32 t2_load; /* 0x14 */
53f5076f86Srick u32 t2_match1; /* 0x18 */
54f5076f86Srick u32 t2_match2; /* 0x1c */
55f5076f86Srick u32 t3_counter; /* 0x20 */
56f5076f86Srick u32 t3_load; /* 0x24 */
57f5076f86Srick u32 t3_match1; /* 0x28 */
58f5076f86Srick u32 t3_match2; /* 0x2c */
59f5076f86Srick u32 cr; /* 0x30 */
60f5076f86Srick u32 int_state; /* 0x34 */
61f5076f86Srick u32 int_mask; /* 0x38 */
62f5076f86Srick };
63f5076f86Srick
64f5076f86Srick struct atftmr_timer_platdata {
65f5076f86Srick struct atftmr_timer_regs *regs;
66f5076f86Srick };
67f5076f86Srick
atftmr_timer_get_count(struct udevice * dev,u64 * count)68f5076f86Srick static int atftmr_timer_get_count(struct udevice *dev, u64 *count)
69f5076f86Srick {
70f5076f86Srick struct atftmr_timer_platdata *plat = dev->platdata;
71f5076f86Srick struct atftmr_timer_regs *const regs = plat->regs;
72f5076f86Srick u32 val;
73f5076f86Srick val = readl(®s->t3_counter);
74f5076f86Srick *count = timer_conv_64(val);
75f5076f86Srick return 0;
76f5076f86Srick }
77f5076f86Srick
atftmr_timer_probe(struct udevice * dev)78f5076f86Srick static int atftmr_timer_probe(struct udevice *dev)
79f5076f86Srick {
80f5076f86Srick struct atftmr_timer_platdata *plat = dev->platdata;
81f5076f86Srick struct atftmr_timer_regs *const regs = plat->regs;
82f5076f86Srick u32 cr;
83f5076f86Srick writel(0, ®s->t3_load);
84f5076f86Srick writel(0, ®s->t3_counter);
85f5076f86Srick writel(TIMER_LOAD_VAL, ®s->t3_match1);
86f5076f86Srick writel(TIMER_LOAD_VAL, ®s->t3_match2);
87f5076f86Srick /* disable interrupts */
88f5076f86Srick writel(T3_MATCH1|T3_MATCH2|T3_OVERFLOW , ®s->int_mask);
89f5076f86Srick cr = readl(®s->cr);
90f5076f86Srick cr |= (T3_ENABLE|T3_UPDOWN);
91f5076f86Srick writel(cr, ®s->cr);
92f5076f86Srick return 0;
93f5076f86Srick }
94f5076f86Srick
atftme_timer_ofdata_to_platdata(struct udevice * dev)95f5076f86Srick static int atftme_timer_ofdata_to_platdata(struct udevice *dev)
96f5076f86Srick {
97f5076f86Srick struct atftmr_timer_platdata *plat = dev_get_platdata(dev);
98*a821c4afSSimon Glass plat->regs = map_physmem(devfdt_get_addr(dev),
99f5076f86Srick sizeof(struct atftmr_timer_regs),
100f5076f86Srick MAP_NOCACHE);
101f5076f86Srick return 0;
102f5076f86Srick }
103f5076f86Srick
104f5076f86Srick static const struct timer_ops ag101p_timer_ops = {
105f5076f86Srick .get_count = atftmr_timer_get_count,
106f5076f86Srick };
107f5076f86Srick
108f5076f86Srick static const struct udevice_id ag101p_timer_ids[] = {
109f5076f86Srick { .compatible = "andestech,attmr010" },
110f5076f86Srick {}
111f5076f86Srick };
112f5076f86Srick
113f5076f86Srick U_BOOT_DRIVER(altera_timer) = {
114f5076f86Srick .name = "ag101p_timer",
115f5076f86Srick .id = UCLASS_TIMER,
116f5076f86Srick .of_match = ag101p_timer_ids,
117f5076f86Srick .ofdata_to_platdata = atftme_timer_ofdata_to_platdata,
118f5076f86Srick .platdata_auto_alloc_size = sizeof(struct atftmr_timer_platdata),
119f5076f86Srick .probe = atftmr_timer_probe,
120f5076f86Srick .ops = &ag101p_timer_ops,
121f5076f86Srick .flags = DM_FLAG_PRE_RELOC,
122f5076f86Srick };
123