summaryrefslogtreecommitdiff
path: root/arch/arm/plat-mxc/epit.c
blob: 96f6a6b0c5d6719454b0dfd821553d73362abe24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
 * Copyright (C) 2005-2013 Freescale Semiconductor, Inc. All Rights Reserved.
 */
/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/err.h>
#include <linux/clk.h>
#include <linux/io.h>
#include <linux/fsl_devices.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/clockchips.h>
#include <mach/hardware.h>
#include <asm/mach/time.h>
#include <mach/common.h>
#include <mach/epit.h>

struct epit_device {
	struct list_head	node;
	struct platform_device *pdev;
	const char	*label;
	struct clk	*clk;
	int		clk_enabled;
	void __iomem	*mmio_base;
	unsigned int	use_count;
	unsigned int	id;
	unsigned int	irq;
	int				mode;
	void (*cb)(void *);
	void *cb_para;
};


static DEFINE_MUTEX(epit_lock);
static LIST_HEAD(epit_list);

static inline void epit_irq_disable(struct epit_device *epit)
{
	u32 val;

	val = readl(epit->mmio_base + EPITCR);
	val &= ~EPITCR_OCIEN;
	writel(val, epit->mmio_base + EPITCR);
}

static inline void epit_irq_enable(struct epit_device *epit)
{
	u32 val;

	val = readl(epit->mmio_base + EPITCR);
	val |= EPITCR_OCIEN;
	writel(val, epit->mmio_base + EPITCR);
}

static inline void epit_irq_acknowledge(struct epit_device *epit)
{
	writel(EPITSR_OCIF, epit->mmio_base + EPITSR);
}

static irqreturn_t epit_timer_interrupt(int irq, void *epit)
{
	struct epit_device *epit_dev = epit;
	u32 cr = 0;

    if (epit_dev) {
		/* stop EPIT timer */
		cr = readl(epit_dev->mmio_base + EPITCR);
		cr &= ~(EPITCR_EN);
		writel(cr, epit_dev->mmio_base + EPITCR);
		/* clear EPIT interrupt flag */
	    epit_irq_acknowledge(epit_dev);
		if (epit_dev->cb)
			epit_dev->cb(epit_dev->cb_para);
	}
	return IRQ_HANDLED;
}

int epit_config(struct epit_device *epit, int mode, void *cb, void *para)
{
	u32 cr;
	unsigned int rc;

	if (epit == NULL)
		return -EINVAL;

	epit->cb = cb;
	epit->cb_para = para;

	/*SW Reset EPIT */
	writel(EPITCR_SWR, epit->mmio_base + EPITCR);
	while (readl(epit->mmio_base + EPITCR) & EPITCR_SWR)
		;

	/* reset EPIT register */
	writel(0x0, epit->mmio_base + EPITCR);
	/* clear EPIT interrupt flag */
	writel(EPITSR_OCIF, epit->mmio_base + EPITSR);
	/* set EPIT mode and clk src */
	cr = (mode << 3) | EPITCR_CLKSRC_REF_HIGH;
	cr |= (EPITCR_WAITEN | EPITCR_OCIEN | EPITCR_RLD);
	writel(cr, epit->mmio_base + EPITCR);
	/* write load counter */
	writel(0xFFFFFFFF, epit->mmio_base + EPITLR);

	epit->mode = mode;

	if (!epit->clk_enabled) {
		rc = clk_enable(epit->clk);
		if (!rc)
			epit->clk_enabled = 1;
	}

	/* Enable EPIT IRQ */
	epit_irq_enable(epit);
	return 0;
}
EXPORT_SYMBOL(epit_config);

void epit_start(struct epit_device *epit, int time_ns)
{
	u32 compare_count = 0;
	unsigned long long int c;
	unsigned int cycles, prescale;
	u32 cr;

	c = clk_get_rate(epit->clk);
	c = c * time_ns;
	do_div(c, 1000000000);
	cycles = c;
	prescale = cycles / 0x10000 + 1;
	cycles /= (prescale + 1);
	/* select prescale */
	cr = readl(epit->mmio_base + EPITCR);
	cr &= ~(EPITCR_PRESC(0xFFF));
	cr |= EPITCR_PRESC(prescale) ;
	writel(cr, epit->mmio_base + EPITCR);
	/* write compare count */
	if (EPIT_FREE_RUN_MODE == epit->mode) {
		compare_count = (0xFFFFFFFF - cycles + 1); /* down counter */
	} else {
		cr = readl(epit->mmio_base + EPITLR);
		compare_count = cr - cycles + 1;
	}
	writel(compare_count, epit->mmio_base + EPITCMPR);
	/* set EPIT Timer Mode */
	cr = readl(epit->mmio_base + EPITCR);
	if (EPIT_FREE_RUN_MODE == epit->mode)
		cr |= EPITCR_ENMOD;

	writel(cr, epit->mmio_base + EPITCR);
	/* start EPIT timer */
	cr = readl(epit->mmio_base + EPITCR);
	cr |= EPITCR_EN;
	writel(cr, epit->mmio_base + EPITCR);
}
EXPORT_SYMBOL(epit_start);

void epit_stop(struct epit_device *epit)
{
	u32 cr = 0;

	epit_irq_disable(epit);
	cr = readl(epit->mmio_base + EPITCR);
	cr &= ~EPITCR_EN;
	writel(cr, epit->mmio_base + EPITCR);
	/* reset EPIT register */
	writel(EPITCR_SWR, epit->mmio_base + EPITCR);
	while (readl(epit->mmio_base + EPITCR) & EPITCR_SWR)
		;

	if (epit->clk_enabled) {
		clk_disable(epit->clk);
		epit->clk_enabled = 0;
	}
}
EXPORT_SYMBOL(epit_stop);

struct epit_device *epit_request(int epit_id, const char *label)
{
	struct epit_device *epit;
	int found = 0;

	mutex_lock(&epit_lock);
	list_for_each_entry(epit, &epit_list, node) {
		if (epit->id == epit_id) {
			found = 1;
			break;
		}
	}
	if (found) {
		if (epit->use_count == 0) {
			epit->use_count++;
			epit->label = label;
		} else {
			epit = ERR_PTR(-EBUSY);
		}
	} else {
		epit = ERR_PTR(-ENOENT);
	}

	mutex_unlock(&epit_lock);
	return epit;
}
EXPORT_SYMBOL(epit_request);

void epit_free(struct epit_device *epit)
{
	mutex_lock(&epit_lock);
	if (epit->use_count) {
		epit->use_count--;
		epit->label = NULL;
	} else {
		pr_warning("EPIT device already freed\n");
	}
	mutex_unlock(&epit_lock);
}
EXPORT_SYMBOL(epit_free);

static int __devinit mxc_epit_probe(struct platform_device *pdev)
{
	struct epit_device *epit;
	struct resource *r;
	struct mxc_epit_platform_data *plat_data = pdev->dev.platform_data;
	int ret = 0;

	epit = kzalloc(sizeof(struct epit_device), GFP_KERNEL);
	if (epit == NULL) {
		dev_err(&pdev->dev, "failed to allocate memory\n");
		return -ENOMEM;
	}
	epit->clk = clk_get(&pdev->dev, "epit");
	if (IS_ERR(epit->clk)) {
		ret = PTR_ERR(epit->clk);
		goto err_free;
	}

	epit->clk_enabled = 0;
	epit->use_count = 0;
	epit->id = pdev->id;
	epit->pdev = pdev;

	r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	epit->irq = (unsigned int)(r->start);
	if (!epit->irq) {
		dev_err(&pdev->dev, "no irq resource?\n");
		ret = -ENODEV;
		goto err_free_clk;
	}

	/* setup IRQ */
	if (request_irq(epit->irq, epit_timer_interrupt, IRQF_DISABLED, "epit",
					(void *)epit)) {
		printk(KERN_ERR "rtc: cannot register IRQ %d\n", epit->irq);
		ret = -EIO;
		goto err_free_clk;
	}

	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (r == NULL) {
		dev_err(&pdev->dev, "no memory resource defined\n");
		ret = -ENODEV;
		goto err_free;
	}

	r = request_mem_region(r->start, r->end - r->start + 1, pdev->name);
	if (r == NULL) {
		dev_err(&pdev->dev, "failed to request memory resource\n");
		ret = -EBUSY;
		goto err_free;
	}
	epit->mmio_base = ioremap(r->start, r->end - r->start + 1);
	if (epit->mmio_base == NULL) {
		dev_err(&pdev->dev, "failed to ioremap() registers\n");
		ret = -ENODEV;
		goto err_free_mem;
	}

	mutex_lock(&epit_lock);
	list_add_tail(&epit->node, &epit_list);
	mutex_unlock(&epit_lock);
	platform_set_drvdata(pdev, epit);
	return 0;
err_free_mem:
	release_mem_region(r->start, r->end - r->start + 1);
err_free_clk:
	clk_put(epit->clk);
err_free:
	kfree(epit);
	return ret;
}

static int __devexit mxc_epit_remove(struct platform_device *pdev)
{
	struct epit_device *epit;
	struct resource *r;

	epit = platform_get_drvdata(pdev);
	if (epit == NULL)
		return -ENODEV;

	mutex_lock(&epit_lock);
	list_del(&epit->node);
	mutex_unlock(&epit_lock);

	iounmap(epit->mmio_base);
	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	release_mem_region(r->start, r->end - r->start + 1);
	clk_put(epit->clk);
	kfree(epit);
	return 0;
}

static struct platform_driver mxc_epit_driver = {
	.driver = {
		.name = "mxc_epit",
	},
	.probe = mxc_epit_probe,
	.remove = __devexit_p(mxc_epit_remove),
};

static int __init mxc_epit_init(void)
{
	return platform_driver_register(&mxc_epit_driver);
}
arch_initcall(mxc_epit_init);

static void __exit mxc_epit_exit(void)
{
	platform_driver_unregister(&mxc_epit_driver);
}
module_exit(mxc_epit_exit);

MODULE_AUTHOR("Freescale Semiconductor, Inc.");
MODULE_DESCRIPTION("Enhance PIT driver");
MODULE_LICENSE("GPL");