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