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