1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/kernel.h>
17#include <linux/errno.h>
18#include <linux/list.h>
19#include <linux/io.h>
20#include <linux/of.h>
21#include <linux/spinlock.h>
22#include <linux/bitops.h>
23#include <asm/fsl_gtm.h>
24
25#define GTCFR_STP(x) ((x) & 1 ? 1 << 5 : 1 << 1)
26#define GTCFR_RST(x) ((x) & 1 ? 1 << 4 : 1 << 0)
27
28#define GTMDR_ICLK_MASK (3 << 1)
29#define GTMDR_ICLK_ICAS (0 << 1)
30#define GTMDR_ICLK_ICLK (1 << 1)
31#define GTMDR_ICLK_SLGO (2 << 1)
32#define GTMDR_FRR (1 << 3)
33#define GTMDR_ORI (1 << 4)
34#define GTMDR_SPS(x) ((x) << 8)
35
36struct gtm_timers_regs {
37 u8 gtcfr1;
38 u8 res0[0x3];
39 u8 gtcfr2;
40 u8 res1[0xB];
41 __be16 gtmdr1;
42 __be16 gtmdr2;
43 __be16 gtrfr1;
44 __be16 gtrfr2;
45 __be16 gtcpr1;
46 __be16 gtcpr2;
47 __be16 gtcnr1;
48 __be16 gtcnr2;
49 __be16 gtmdr3;
50 __be16 gtmdr4;
51 __be16 gtrfr3;
52 __be16 gtrfr4;
53 __be16 gtcpr3;
54 __be16 gtcpr4;
55 __be16 gtcnr3;
56 __be16 gtcnr4;
57 __be16 gtevr1;
58 __be16 gtevr2;
59 __be16 gtevr3;
60 __be16 gtevr4;
61 __be16 gtpsr1;
62 __be16 gtpsr2;
63 __be16 gtpsr3;
64 __be16 gtpsr4;
65 u8 res2[0x40];
66} __attribute__ ((packed));
67
68struct gtm {
69 unsigned int clock;
70 struct gtm_timers_regs __iomem *regs;
71 struct gtm_timer timers[4];
72 spinlock_t lock;
73 struct list_head list_node;
74};
75
76static LIST_HEAD(gtms);
77
78
79
80
81
82
83
84
85
86struct gtm_timer *gtm_get_timer16(void)
87{
88 struct gtm *gtm = NULL;
89 int i;
90
91 list_for_each_entry(gtm, >ms, list_node) {
92 spin_lock_irq(>m->lock);
93
94 for (i = 0; i < ARRAY_SIZE(gtm->timers); i++) {
95 if (!gtm->timers[i].requested) {
96 gtm->timers[i].requested = true;
97 spin_unlock_irq(>m->lock);
98 return >m->timers[i];
99 }
100 }
101
102 spin_unlock_irq(>m->lock);
103 }
104
105 if (gtm)
106 return ERR_PTR(-EBUSY);
107 return ERR_PTR(-ENODEV);
108}
109EXPORT_SYMBOL(gtm_get_timer16);
110
111
112
113
114
115
116
117
118
119
120
121struct gtm_timer *gtm_get_specific_timer16(struct gtm *gtm,
122 unsigned int timer)
123{
124 struct gtm_timer *ret = ERR_PTR(-EBUSY);
125
126 if (timer > 3)
127 return ERR_PTR(-EINVAL);
128
129 spin_lock_irq(>m->lock);
130
131 if (gtm->timers[timer].requested)
132 goto out;
133
134 ret = >m->timers[timer];
135 ret->requested = true;
136
137out:
138 spin_unlock_irq(>m->lock);
139 return ret;
140}
141EXPORT_SYMBOL(gtm_get_specific_timer16);
142
143
144
145
146
147
148
149
150void gtm_put_timer16(struct gtm_timer *tmr)
151{
152 gtm_stop_timer16(tmr);
153
154 spin_lock_irq(&tmr->gtm->lock);
155 tmr->requested = false;
156 spin_unlock_irq(&tmr->gtm->lock);
157}
158EXPORT_SYMBOL(gtm_put_timer16);
159
160
161
162
163
164static int gtm_set_ref_timer16(struct gtm_timer *tmr, int frequency,
165 int reference_value, bool free_run)
166{
167 struct gtm *gtm = tmr->gtm;
168 int num = tmr - >m->timers[0];
169 unsigned int prescaler;
170 u8 iclk = GTMDR_ICLK_ICLK;
171 u8 psr;
172 u8 sps;
173 unsigned long flags;
174 int max_prescaler = 256 * 256 * 16;
175
176
177 if (!tmr->gtpsr)
178 max_prescaler /= 256;
179
180 prescaler = gtm->clock / frequency;
181
182
183
184
185
186 if (prescaler > max_prescaler)
187 return -EINVAL;
188
189 if (prescaler > max_prescaler / 16) {
190 iclk = GTMDR_ICLK_SLGO;
191 prescaler /= 16;
192 }
193
194 if (prescaler <= 256) {
195 psr = 0;
196 sps = prescaler - 1;
197 } else {
198 psr = 256 - 1;
199 sps = prescaler / 256 - 1;
200 }
201
202 spin_lock_irqsave(>m->lock, flags);
203
204
205
206
207
208 clrsetbits_8(tmr->gtcfr, ~(GTCFR_STP(num) | GTCFR_RST(num)),
209 GTCFR_STP(num) | GTCFR_RST(num));
210
211 setbits8(tmr->gtcfr, GTCFR_STP(num));
212
213 if (tmr->gtpsr)
214 out_be16(tmr->gtpsr, psr);
215 clrsetbits_be16(tmr->gtmdr, 0xFFFF, iclk | GTMDR_SPS(sps) |
216 GTMDR_ORI | (free_run ? GTMDR_FRR : 0));
217 out_be16(tmr->gtcnr, 0);
218 out_be16(tmr->gtrfr, reference_value);
219 out_be16(tmr->gtevr, 0xFFFF);
220
221
222 clrbits8(tmr->gtcfr, GTCFR_STP(num));
223
224 spin_unlock_irqrestore(>m->lock, flags);
225
226 return 0;
227}
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242int gtm_set_timer16(struct gtm_timer *tmr, unsigned long usec, bool reload)
243{
244
245 int freq = 1000000;
246 unsigned int bit;
247
248 bit = fls_long(usec);
249 if (bit > 15) {
250 freq >>= bit - 15;
251 usec >>= bit - 15;
252 }
253
254 if (!freq)
255 return -EINVAL;
256
257 return gtm_set_ref_timer16(tmr, freq, usec, reload);
258}
259EXPORT_SYMBOL(gtm_set_timer16);
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278int gtm_set_exact_timer16(struct gtm_timer *tmr, u16 usec, bool reload)
279{
280
281 const int freq = 1000000;
282
283
284
285
286
287
288
289
290 return gtm_set_ref_timer16(tmr, freq, usec, reload);
291}
292EXPORT_SYMBOL(gtm_set_exact_timer16);
293
294
295
296
297
298
299
300
301void gtm_stop_timer16(struct gtm_timer *tmr)
302{
303 struct gtm *gtm = tmr->gtm;
304 int num = tmr - >m->timers[0];
305 unsigned long flags;
306
307 spin_lock_irqsave(>m->lock, flags);
308
309 setbits8(tmr->gtcfr, GTCFR_STP(num));
310 out_be16(tmr->gtevr, 0xFFFF);
311
312 spin_unlock_irqrestore(>m->lock, flags);
313}
314EXPORT_SYMBOL(gtm_stop_timer16);
315
316
317
318
319
320
321
322
323
324
325void gtm_ack_timer16(struct gtm_timer *tmr, u16 events)
326{
327 out_be16(tmr->gtevr, events);
328}
329EXPORT_SYMBOL(gtm_ack_timer16);
330
331static void __init gtm_set_shortcuts(struct device_node *np,
332 struct gtm_timer *timers,
333 struct gtm_timers_regs __iomem *regs)
334{
335
336
337
338
339
340
341 timers[0].gtcfr = ®s->gtcfr1;
342 timers[0].gtmdr = ®s->gtmdr1;
343 timers[0].gtcnr = ®s->gtcnr1;
344 timers[0].gtrfr = ®s->gtrfr1;
345 timers[0].gtevr = ®s->gtevr1;
346
347 timers[1].gtcfr = ®s->gtcfr1;
348 timers[1].gtmdr = ®s->gtmdr2;
349 timers[1].gtcnr = ®s->gtcnr2;
350 timers[1].gtrfr = ®s->gtrfr2;
351 timers[1].gtevr = ®s->gtevr2;
352
353 timers[2].gtcfr = ®s->gtcfr2;
354 timers[2].gtmdr = ®s->gtmdr3;
355 timers[2].gtcnr = ®s->gtcnr3;
356 timers[2].gtrfr = ®s->gtrfr3;
357 timers[2].gtevr = ®s->gtevr3;
358
359 timers[3].gtcfr = ®s->gtcfr2;
360 timers[3].gtmdr = ®s->gtmdr4;
361 timers[3].gtcnr = ®s->gtcnr4;
362 timers[3].gtrfr = ®s->gtrfr4;
363 timers[3].gtevr = ®s->gtevr4;
364
365
366 if (!of_device_is_compatible(np, "fsl,cpm2-gtm")) {
367 timers[0].gtpsr = ®s->gtpsr1;
368 timers[1].gtpsr = ®s->gtpsr2;
369 timers[2].gtpsr = ®s->gtpsr3;
370 timers[3].gtpsr = ®s->gtpsr4;
371 }
372}
373
374static int __init fsl_gtm_init(void)
375{
376 struct device_node *np;
377
378 for_each_compatible_node(np, NULL, "fsl,gtm") {
379 int i;
380 struct gtm *gtm;
381 const u32 *clock;
382 int size;
383
384 gtm = kzalloc(sizeof(*gtm), GFP_KERNEL);
385 if (!gtm) {
386 pr_err("%s: unable to allocate memory\n",
387 np->full_name);
388 continue;
389 }
390
391 spin_lock_init(>m->lock);
392
393 clock = of_get_property(np, "clock-frequency", &size);
394 if (!clock || size != sizeof(*clock)) {
395 pr_err("%s: no clock-frequency\n", np->full_name);
396 goto err;
397 }
398 gtm->clock = *clock;
399
400 for (i = 0; i < ARRAY_SIZE(gtm->timers); i++) {
401 int ret;
402 struct resource irq;
403
404 ret = of_irq_to_resource(np, i, &irq);
405 if (ret == NO_IRQ) {
406 pr_err("%s: not enough interrupts specified\n",
407 np->full_name);
408 goto err;
409 }
410 gtm->timers[i].irq = irq.start;
411 gtm->timers[i].gtm = gtm;
412 }
413
414 gtm->regs = of_iomap(np, 0);
415 if (!gtm->regs) {
416 pr_err("%s: unable to iomap registers\n",
417 np->full_name);
418 goto err;
419 }
420
421 gtm_set_shortcuts(np, gtm->timers, gtm->regs);
422 list_add(>m->list_node, >ms);
423
424
425 np->data = gtm;
426 of_node_get(np);
427
428 continue;
429err:
430 kfree(gtm);
431 }
432 return 0;
433}
434arch_initcall(fsl_gtm_init);
435