1b841b6e9Srick /*
2b841b6e9Srick * Andestech ATCPIT100 timer driver
3b841b6e9Srick *
4b841b6e9Srick * (C) Copyright 2016
5b841b6e9Srick * Rick Chen, NDS32 Software Engineering, rick@andestech.com
6b841b6e9Srick *
7b841b6e9Srick * SPDX-License-Identifier: GPL-2.0+
8b841b6e9Srick */
9b841b6e9Srick #include <common.h>
10b841b6e9Srick #include <dm.h>
11b841b6e9Srick #include <errno.h>
12b841b6e9Srick #include <timer.h>
13b841b6e9Srick #include <linux/io.h>
14b841b6e9Srick
15b841b6e9Srick DECLARE_GLOBAL_DATA_PTR;
16b841b6e9Srick
17b841b6e9Srick #define REG32_TMR(x) (*(unsigned long *) ((plat->regs) + (x>>2)))
18b841b6e9Srick
19b841b6e9Srick /*
20b841b6e9Srick * Definition of register offsets
21b841b6e9Srick */
22b841b6e9Srick
23b841b6e9Srick /* ID and Revision Register */
24b841b6e9Srick #define ID_REV 0x0
25b841b6e9Srick
26b841b6e9Srick /* Configuration Register */
27b841b6e9Srick #define CFG 0x10
28b841b6e9Srick
29b841b6e9Srick /* Interrupt Enable Register */
30b841b6e9Srick #define INT_EN 0x14
31b841b6e9Srick #define CH_INT_EN(c , i) ((1<<i)<<(4*c))
32b841b6e9Srick
33b841b6e9Srick /* Interrupt Status Register */
34b841b6e9Srick #define INT_STA 0x18
35b841b6e9Srick #define CH_INT_STA(c , i) ((1<<i)<<(4*c))
36b841b6e9Srick
37b841b6e9Srick /* Channel Enable Register */
38b841b6e9Srick #define CH_EN 0x1C
39b841b6e9Srick #define CH_TMR_EN(c , t) ((1<<t)<<(4*c))
40b841b6e9Srick
41b841b6e9Srick /* Ch n Control REgister */
42b841b6e9Srick #define CH_CTL(n) (0x20+0x10*n)
43b841b6e9Srick /* Channel clock source , bit 3 , 0:External clock , 1:APB clock */
44b841b6e9Srick #define APB_CLK (1<<3)
45b841b6e9Srick /* Channel mode , bit 0~2 */
46b841b6e9Srick #define TMR_32 1
47b841b6e9Srick #define TMR_16 2
48b841b6e9Srick #define TMR_8 3
49b841b6e9Srick #define PWM 4
50b841b6e9Srick
51b841b6e9Srick #define CH_REL(n) (0x24+0x10*n)
52b841b6e9Srick #define CH_CNT(n) (0x28+0x10*n)
53b841b6e9Srick
54b841b6e9Srick struct atctmr_timer_regs {
55b841b6e9Srick u32 id_rev; /* 0x00 */
56b841b6e9Srick u32 reservd[3]; /* 0x04 ~ 0x0c */
57b841b6e9Srick u32 cfg; /* 0x10 */
58b841b6e9Srick u32 int_en; /* 0x14 */
59b841b6e9Srick u32 int_st; /* 0x18 */
60b841b6e9Srick u32 ch_en; /* 0x1c */
61b841b6e9Srick u32 ch0_ctrl; /* 0x20 */
62b841b6e9Srick u32 ch0_reload; /* 0x24 */
63b841b6e9Srick u32 ch0_cntr; /* 0x28 */
64b841b6e9Srick u32 reservd1; /* 0x2c */
65b841b6e9Srick u32 ch1_ctrl; /* 0x30 */
66b841b6e9Srick u32 ch1_reload; /* 0x34 */
67b841b6e9Srick u32 int_mask; /* 0x38 */
68b841b6e9Srick };
69b841b6e9Srick
70b841b6e9Srick struct atftmr_timer_platdata {
71b841b6e9Srick unsigned long *regs;
72b841b6e9Srick };
73b841b6e9Srick
atftmr_timer_get_count(struct udevice * dev,u64 * count)74b841b6e9Srick static int atftmr_timer_get_count(struct udevice *dev, u64 *count)
75b841b6e9Srick {
76b841b6e9Srick struct atftmr_timer_platdata *plat = dev->platdata;
77b841b6e9Srick u32 val;
78b841b6e9Srick val = ~(REG32_TMR(CH_CNT(1))+0xffffffff);
79b841b6e9Srick *count = timer_conv_64(val);
80b841b6e9Srick return 0;
81b841b6e9Srick }
82b841b6e9Srick
atctmr_timer_probe(struct udevice * dev)83b841b6e9Srick static int atctmr_timer_probe(struct udevice *dev)
84b841b6e9Srick {
85b841b6e9Srick struct atftmr_timer_platdata *plat = dev->platdata;
86b841b6e9Srick REG32_TMR(CH_REL(1)) = 0xffffffff;
87b841b6e9Srick REG32_TMR(CH_CTL(1)) = APB_CLK|TMR_32;
88b841b6e9Srick REG32_TMR(CH_EN) |= CH_TMR_EN(1 , 0);
89b841b6e9Srick return 0;
90b841b6e9Srick }
91b841b6e9Srick
atctme_timer_ofdata_to_platdata(struct udevice * dev)92b841b6e9Srick static int atctme_timer_ofdata_to_platdata(struct udevice *dev)
93b841b6e9Srick {
94b841b6e9Srick struct atftmr_timer_platdata *plat = dev_get_platdata(dev);
95*a821c4afSSimon Glass plat->regs = map_physmem(devfdt_get_addr(dev) , 0x100 , MAP_NOCACHE);
96b841b6e9Srick return 0;
97b841b6e9Srick }
98b841b6e9Srick
99b841b6e9Srick static const struct timer_ops ag101p_timer_ops = {
100b841b6e9Srick .get_count = atftmr_timer_get_count,
101b841b6e9Srick };
102b841b6e9Srick
103b841b6e9Srick static const struct udevice_id ag101p_timer_ids[] = {
104b841b6e9Srick { .compatible = "andestech,atcpit100" },
105b841b6e9Srick {}
106b841b6e9Srick };
107b841b6e9Srick
108b841b6e9Srick U_BOOT_DRIVER(altera_timer) = {
109b841b6e9Srick .name = "ae3xx_timer",
110b841b6e9Srick .id = UCLASS_TIMER,
111b841b6e9Srick .of_match = ag101p_timer_ids,
112b841b6e9Srick .ofdata_to_platdata = atctme_timer_ofdata_to_platdata,
113b841b6e9Srick .platdata_auto_alloc_size = sizeof(struct atftmr_timer_platdata),
114b841b6e9Srick .probe = atctmr_timer_probe,
115b841b6e9Srick .ops = &ag101p_timer_ops,
116b841b6e9Srick .flags = DM_FLAG_PRE_RELOC,
117b841b6e9Srick };
118