0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/kernel.h>
0013 #include <linux/err.h>
0014 #include <linux/errno.h>
0015 #include <linux/list.h>
0016 #include <linux/io.h>
0017 #include <linux/of.h>
0018 #include <linux/of_address.h>
0019 #include <linux/of_irq.h>
0020 #include <linux/spinlock.h>
0021 #include <linux/bitops.h>
0022 #include <linux/slab.h>
0023 #include <linux/export.h>
0024 #include <asm/fsl_gtm.h>
0025
0026 #define GTCFR_STP(x) ((x) & 1 ? 1 << 5 : 1 << 1)
0027 #define GTCFR_RST(x) ((x) & 1 ? 1 << 4 : 1 << 0)
0028
0029 #define GTMDR_ICLK_MASK (3 << 1)
0030 #define GTMDR_ICLK_ICAS (0 << 1)
0031 #define GTMDR_ICLK_ICLK (1 << 1)
0032 #define GTMDR_ICLK_SLGO (2 << 1)
0033 #define GTMDR_FRR (1 << 3)
0034 #define GTMDR_ORI (1 << 4)
0035 #define GTMDR_SPS(x) ((x) << 8)
0036
0037 struct gtm_timers_regs {
0038 u8 gtcfr1;
0039 u8 res0[0x3];
0040 u8 gtcfr2;
0041 u8 res1[0xB];
0042 __be16 gtmdr1;
0043 __be16 gtmdr2;
0044 __be16 gtrfr1;
0045 __be16 gtrfr2;
0046 __be16 gtcpr1;
0047 __be16 gtcpr2;
0048 __be16 gtcnr1;
0049 __be16 gtcnr2;
0050 __be16 gtmdr3;
0051 __be16 gtmdr4;
0052 __be16 gtrfr3;
0053 __be16 gtrfr4;
0054 __be16 gtcpr3;
0055 __be16 gtcpr4;
0056 __be16 gtcnr3;
0057 __be16 gtcnr4;
0058 __be16 gtevr1;
0059 __be16 gtevr2;
0060 __be16 gtevr3;
0061 __be16 gtevr4;
0062 __be16 gtpsr1;
0063 __be16 gtpsr2;
0064 __be16 gtpsr3;
0065 __be16 gtpsr4;
0066 u8 res2[0x40];
0067 } __attribute__ ((packed));
0068
0069 struct gtm {
0070 unsigned int clock;
0071 struct gtm_timers_regs __iomem *regs;
0072 struct gtm_timer timers[4];
0073 spinlock_t lock;
0074 struct list_head list_node;
0075 };
0076
0077 static LIST_HEAD(gtms);
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087 struct gtm_timer *gtm_get_timer16(void)
0088 {
0089 struct gtm *gtm;
0090 int i;
0091
0092 list_for_each_entry(gtm, >ms, list_node) {
0093 spin_lock_irq(>m->lock);
0094
0095 for (i = 0; i < ARRAY_SIZE(gtm->timers); i++) {
0096 if (!gtm->timers[i].requested) {
0097 gtm->timers[i].requested = true;
0098 spin_unlock_irq(>m->lock);
0099 return >m->timers[i];
0100 }
0101 }
0102
0103 spin_unlock_irq(>m->lock);
0104 }
0105
0106 if (!list_empty(>ms))
0107 return ERR_PTR(-EBUSY);
0108 return ERR_PTR(-ENODEV);
0109 }
0110 EXPORT_SYMBOL(gtm_get_timer16);
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122 struct gtm_timer *gtm_get_specific_timer16(struct gtm *gtm,
0123 unsigned int timer)
0124 {
0125 struct gtm_timer *ret = ERR_PTR(-EBUSY);
0126
0127 if (timer > 3)
0128 return ERR_PTR(-EINVAL);
0129
0130 spin_lock_irq(>m->lock);
0131
0132 if (gtm->timers[timer].requested)
0133 goto out;
0134
0135 ret = >m->timers[timer];
0136 ret->requested = true;
0137
0138 out:
0139 spin_unlock_irq(>m->lock);
0140 return ret;
0141 }
0142 EXPORT_SYMBOL(gtm_get_specific_timer16);
0143
0144
0145
0146
0147
0148
0149
0150
0151 void gtm_put_timer16(struct gtm_timer *tmr)
0152 {
0153 gtm_stop_timer16(tmr);
0154
0155 spin_lock_irq(&tmr->gtm->lock);
0156 tmr->requested = false;
0157 spin_unlock_irq(&tmr->gtm->lock);
0158 }
0159 EXPORT_SYMBOL(gtm_put_timer16);
0160
0161
0162
0163
0164
0165 static int gtm_set_ref_timer16(struct gtm_timer *tmr, int frequency,
0166 int reference_value, bool free_run)
0167 {
0168 struct gtm *gtm = tmr->gtm;
0169 int num = tmr - >m->timers[0];
0170 unsigned int prescaler;
0171 u8 iclk = GTMDR_ICLK_ICLK;
0172 u8 psr;
0173 u8 sps;
0174 unsigned long flags;
0175 int max_prescaler = 256 * 256 * 16;
0176
0177
0178 if (!tmr->gtpsr)
0179 max_prescaler /= 256;
0180
0181 prescaler = gtm->clock / frequency;
0182
0183
0184
0185
0186
0187 if (prescaler > max_prescaler)
0188 return -EINVAL;
0189
0190 if (prescaler > max_prescaler / 16) {
0191 iclk = GTMDR_ICLK_SLGO;
0192 prescaler /= 16;
0193 }
0194
0195 if (prescaler <= 256) {
0196 psr = 0;
0197 sps = prescaler - 1;
0198 } else {
0199 psr = 256 - 1;
0200 sps = prescaler / 256 - 1;
0201 }
0202
0203 spin_lock_irqsave(>m->lock, flags);
0204
0205
0206
0207
0208
0209 clrsetbits_8(tmr->gtcfr, ~(GTCFR_STP(num) | GTCFR_RST(num)),
0210 GTCFR_STP(num) | GTCFR_RST(num));
0211
0212 setbits8(tmr->gtcfr, GTCFR_STP(num));
0213
0214 if (tmr->gtpsr)
0215 out_be16(tmr->gtpsr, psr);
0216 clrsetbits_be16(tmr->gtmdr, 0xFFFF, iclk | GTMDR_SPS(sps) |
0217 GTMDR_ORI | (free_run ? GTMDR_FRR : 0));
0218 out_be16(tmr->gtcnr, 0);
0219 out_be16(tmr->gtrfr, reference_value);
0220 out_be16(tmr->gtevr, 0xFFFF);
0221
0222
0223 clrbits8(tmr->gtcfr, GTCFR_STP(num));
0224
0225 spin_unlock_irqrestore(>m->lock, flags);
0226
0227 return 0;
0228 }
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243 int gtm_set_timer16(struct gtm_timer *tmr, unsigned long usec, bool reload)
0244 {
0245
0246 int freq = 1000000;
0247 unsigned int bit;
0248
0249 bit = fls_long(usec);
0250 if (bit > 15) {
0251 freq >>= bit - 15;
0252 usec >>= bit - 15;
0253 }
0254
0255 if (!freq)
0256 return -EINVAL;
0257
0258 return gtm_set_ref_timer16(tmr, freq, usec, reload);
0259 }
0260 EXPORT_SYMBOL(gtm_set_timer16);
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279 int gtm_set_exact_timer16(struct gtm_timer *tmr, u16 usec, bool reload)
0280 {
0281
0282 const int freq = 1000000;
0283
0284
0285
0286
0287
0288
0289
0290
0291 return gtm_set_ref_timer16(tmr, freq, usec, reload);
0292 }
0293 EXPORT_SYMBOL(gtm_set_exact_timer16);
0294
0295
0296
0297
0298
0299
0300
0301
0302 void gtm_stop_timer16(struct gtm_timer *tmr)
0303 {
0304 struct gtm *gtm = tmr->gtm;
0305 int num = tmr - >m->timers[0];
0306 unsigned long flags;
0307
0308 spin_lock_irqsave(>m->lock, flags);
0309
0310 setbits8(tmr->gtcfr, GTCFR_STP(num));
0311 out_be16(tmr->gtevr, 0xFFFF);
0312
0313 spin_unlock_irqrestore(>m->lock, flags);
0314 }
0315 EXPORT_SYMBOL(gtm_stop_timer16);
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326 void gtm_ack_timer16(struct gtm_timer *tmr, u16 events)
0327 {
0328 out_be16(tmr->gtevr, events);
0329 }
0330 EXPORT_SYMBOL(gtm_ack_timer16);
0331
0332 static void __init gtm_set_shortcuts(struct device_node *np,
0333 struct gtm_timer *timers,
0334 struct gtm_timers_regs __iomem *regs)
0335 {
0336
0337
0338
0339
0340
0341
0342 timers[0].gtcfr = ®s->gtcfr1;
0343 timers[0].gtmdr = ®s->gtmdr1;
0344 timers[0].gtcnr = ®s->gtcnr1;
0345 timers[0].gtrfr = ®s->gtrfr1;
0346 timers[0].gtevr = ®s->gtevr1;
0347
0348 timers[1].gtcfr = ®s->gtcfr1;
0349 timers[1].gtmdr = ®s->gtmdr2;
0350 timers[1].gtcnr = ®s->gtcnr2;
0351 timers[1].gtrfr = ®s->gtrfr2;
0352 timers[1].gtevr = ®s->gtevr2;
0353
0354 timers[2].gtcfr = ®s->gtcfr2;
0355 timers[2].gtmdr = ®s->gtmdr3;
0356 timers[2].gtcnr = ®s->gtcnr3;
0357 timers[2].gtrfr = ®s->gtrfr3;
0358 timers[2].gtevr = ®s->gtevr3;
0359
0360 timers[3].gtcfr = ®s->gtcfr2;
0361 timers[3].gtmdr = ®s->gtmdr4;
0362 timers[3].gtcnr = ®s->gtcnr4;
0363 timers[3].gtrfr = ®s->gtrfr4;
0364 timers[3].gtevr = ®s->gtevr4;
0365
0366
0367 if (!of_device_is_compatible(np, "fsl,cpm2-gtm")) {
0368 timers[0].gtpsr = ®s->gtpsr1;
0369 timers[1].gtpsr = ®s->gtpsr2;
0370 timers[2].gtpsr = ®s->gtpsr3;
0371 timers[3].gtpsr = ®s->gtpsr4;
0372 }
0373 }
0374
0375 static int __init fsl_gtm_init(void)
0376 {
0377 struct device_node *np;
0378
0379 for_each_compatible_node(np, NULL, "fsl,gtm") {
0380 int i;
0381 struct gtm *gtm;
0382 const u32 *clock;
0383 int size;
0384
0385 gtm = kzalloc(sizeof(*gtm), GFP_KERNEL);
0386 if (!gtm) {
0387 pr_err("%pOF: unable to allocate memory\n",
0388 np);
0389 continue;
0390 }
0391
0392 spin_lock_init(>m->lock);
0393
0394 clock = of_get_property(np, "clock-frequency", &size);
0395 if (!clock || size != sizeof(*clock)) {
0396 pr_err("%pOF: no clock-frequency\n", np);
0397 goto err;
0398 }
0399 gtm->clock = *clock;
0400
0401 for (i = 0; i < ARRAY_SIZE(gtm->timers); i++) {
0402 unsigned int irq;
0403
0404 irq = irq_of_parse_and_map(np, i);
0405 if (!irq) {
0406 pr_err("%pOF: not enough interrupts specified\n",
0407 np);
0408 goto err;
0409 }
0410 gtm->timers[i].irq = irq;
0411 gtm->timers[i].gtm = gtm;
0412 }
0413
0414 gtm->regs = of_iomap(np, 0);
0415 if (!gtm->regs) {
0416 pr_err("%pOF: unable to iomap registers\n",
0417 np);
0418 goto err;
0419 }
0420
0421 gtm_set_shortcuts(np, gtm->timers, gtm->regs);
0422 list_add(>m->list_node, >ms);
0423
0424
0425 np->data = gtm;
0426 of_node_get(np);
0427
0428 continue;
0429 err:
0430 kfree(gtm);
0431 }
0432 return 0;
0433 }
0434 arch_initcall(fsl_gtm_init);