1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * OMAP1 Dual-Mode Timers - platform device registration
4  *
5  * Contains first level initialization routines which internally
6  * generates timer device information and registers with linux
7  * device model. It also has a low level function to change the timer
8  * input clock source.
9  *
10  * Copyright (C) 2011 Texas Instruments Incorporated - https://www.ti.com/
11  * Tarun Kanti DebBarma <tarun.kanti@ti.com>
12  * Thara Gopinath <thara@ti.com>
13  */
14 
15 #include <linux/clk.h>
16 #include <linux/io.h>
17 #include <linux/err.h>
18 #include <linux/slab.h>
19 #include <linux/platform_device.h>
20 #include <linux/platform_data/dmtimer-omap.h>
21 #include <linux/soc/ti/omap1-io.h>
22 
23 #include <clocksource/timer-ti-dm.h>
24 
25 #include "soc.h"
26 
27 #define OMAP1610_GPTIMER1_BASE		0xfffb1400
28 #define OMAP1610_GPTIMER2_BASE		0xfffb1c00
29 #define OMAP1610_GPTIMER3_BASE		0xfffb2400
30 #define OMAP1610_GPTIMER4_BASE		0xfffb2c00
31 #define OMAP1610_GPTIMER5_BASE		0xfffb3400
32 #define OMAP1610_GPTIMER6_BASE		0xfffb3c00
33 #define OMAP1610_GPTIMER7_BASE		0xfffb7400
34 #define OMAP1610_GPTIMER8_BASE		0xfffbd400
35 
36 #define OMAP1_DM_TIMER_COUNT		8
37 
omap1_dm_timer_set_src(struct platform_device * pdev,int source)38 static int omap1_dm_timer_set_src(struct platform_device *pdev,
39 				int source)
40 {
41 	int n = (pdev->id - 1) << 1;
42 	u32 l;
43 
44 	l = omap_readl(MOD_CONF_CTRL_1) & ~(0x03 << n);
45 	l |= source << n;
46 	omap_writel(l, MOD_CONF_CTRL_1);
47 
48 	return 0;
49 }
50 
omap1_dm_timer_init(void)51 static int __init omap1_dm_timer_init(void)
52 {
53 	int i;
54 	int ret;
55 	struct dmtimer_platform_data *pdata;
56 	struct platform_device *pdev;
57 
58 	if (!cpu_is_omap16xx())
59 		return 0;
60 
61 	for (i = 1; i <= OMAP1_DM_TIMER_COUNT; i++) {
62 		struct resource res[2];
63 		u32 base, irq;
64 
65 		switch (i) {
66 		case 1:
67 			base = OMAP1610_GPTIMER1_BASE;
68 			irq = INT_1610_GPTIMER1;
69 			break;
70 		case 2:
71 			base = OMAP1610_GPTIMER2_BASE;
72 			irq = INT_1610_GPTIMER2;
73 			break;
74 		case 3:
75 			base = OMAP1610_GPTIMER3_BASE;
76 			irq = INT_1610_GPTIMER3;
77 			break;
78 		case 4:
79 			base = OMAP1610_GPTIMER4_BASE;
80 			irq = INT_1610_GPTIMER4;
81 			break;
82 		case 5:
83 			base = OMAP1610_GPTIMER5_BASE;
84 			irq = INT_1610_GPTIMER5;
85 			break;
86 		case 6:
87 			base = OMAP1610_GPTIMER6_BASE;
88 			irq = INT_1610_GPTIMER6;
89 			break;
90 		case 7:
91 			base = OMAP1610_GPTIMER7_BASE;
92 			irq = INT_1610_GPTIMER7;
93 			break;
94 		case 8:
95 			base = OMAP1610_GPTIMER8_BASE;
96 			irq = INT_1610_GPTIMER8;
97 			break;
98 		default:
99 			/*
100 			 * not supposed to reach here.
101 			 * this is to remove warning.
102 			 */
103 			return -EINVAL;
104 		}
105 
106 		pdev = platform_device_alloc("omap_timer", i);
107 		if (!pdev) {
108 			pr_err("%s: Failed to device alloc for dmtimer%d\n",
109 				__func__, i);
110 			return -ENOMEM;
111 		}
112 
113 		memset(res, 0, 2 * sizeof(struct resource));
114 		res[0].start = base;
115 		res[0].end = base + 0x46;
116 		res[0].flags = IORESOURCE_MEM;
117 		res[1].start = irq;
118 		res[1].end = irq;
119 		res[1].flags = IORESOURCE_IRQ;
120 		ret = platform_device_add_resources(pdev, res,
121 				ARRAY_SIZE(res));
122 		if (ret) {
123 			dev_err(&pdev->dev, "%s: Failed to add resources.\n",
124 				__func__);
125 			goto err_free_pdev;
126 		}
127 
128 		pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
129 		if (!pdata) {
130 			ret = -ENOMEM;
131 			goto err_free_pdata;
132 		}
133 
134 		pdata->set_timer_src = omap1_dm_timer_set_src;
135 		pdata->timer_capability = OMAP_TIMER_ALWON |
136 				OMAP_TIMER_NEEDS_RESET | OMAP_TIMER_HAS_DSP_IRQ;
137 
138 		ret = platform_device_add_data(pdev, pdata, sizeof(*pdata));
139 		if (ret) {
140 			dev_err(&pdev->dev, "%s: Failed to add platform data.\n",
141 				__func__);
142 			goto err_free_pdata;
143 		}
144 
145 		ret = platform_device_add(pdev);
146 		if (ret) {
147 			dev_err(&pdev->dev, "%s: Failed to add platform device.\n",
148 				__func__);
149 			goto err_free_pdata;
150 		}
151 
152 		dev_dbg(&pdev->dev, " Registered.\n");
153 	}
154 
155 	return 0;
156 
157 err_free_pdata:
158 	kfree(pdata);
159 
160 err_free_pdev:
161 	platform_device_put(pdev);
162 
163 	return ret;
164 }
165 arch_initcall(omap1_dm_timer_init);
166