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