OMAP3: PM: Prevented DVFS state switches when enabling off-mode
[linux-ginger.git] / drivers / macintosh / rack-meter.c
blob93fb32038b14cf2e822708763669dfc855f4e49d
1 /*
2 * RackMac vu-meter driver
4 * (c) Copyright 2006 Benjamin Herrenschmidt, IBM Corp.
5 * <benh@kernel.crashing.org>
7 * Released under the term of the GNU GPL v2.
9 * Support the CPU-meter LEDs of the Xserve G5
11 * TODO: Implement PWM to do variable intensity and provide userland
12 * interface for fun. Also, the CPU-meter could be made nicer by being
13 * a bit less "immediate" but giving instead a more average load over
14 * time. Patches welcome :-)
17 #undef DEBUG
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/device.h>
22 #include <linux/interrupt.h>
23 #include <linux/module.h>
24 #include <linux/pci.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/kernel_stat.h>
28 #include <asm/io.h>
29 #include <asm/prom.h>
30 #include <asm/machdep.h>
31 #include <asm/pmac_feature.h>
32 #include <asm/dbdma.h>
33 #include <asm/macio.h>
34 #include <asm/keylargo.h>
36 /* Number of samples in a sample buffer */
37 #define SAMPLE_COUNT 256
39 /* CPU meter sampling rate in ms */
40 #define CPU_SAMPLING_RATE 250
42 struct rackmeter_dma {
43 struct dbdma_cmd cmd[4] ____cacheline_aligned;
44 u32 mark ____cacheline_aligned;
45 u32 buf1[SAMPLE_COUNT] ____cacheline_aligned;
46 u32 buf2[SAMPLE_COUNT] ____cacheline_aligned;
47 } ____cacheline_aligned;
49 struct rackmeter_cpu {
50 struct delayed_work sniffer;
51 struct rackmeter *rm;
52 cputime64_t prev_wall;
53 cputime64_t prev_idle;
54 int zero;
55 } ____cacheline_aligned;
57 struct rackmeter {
58 struct macio_dev *mdev;
59 unsigned int irq;
60 struct device_node *i2s;
61 u8 *ubuf;
62 struct dbdma_regs __iomem *dma_regs;
63 void __iomem *i2s_regs;
64 dma_addr_t dma_buf_p;
65 struct rackmeter_dma *dma_buf_v;
66 int stale_irq;
67 struct rackmeter_cpu cpu[2];
68 int paused;
69 struct mutex sem;
72 /* To be set as a tunable */
73 static int rackmeter_ignore_nice;
75 /* This GPIO is whacked by the OS X driver when initializing */
76 #define RACKMETER_MAGIC_GPIO 0x78
78 /* This is copied from cpufreq_ondemand, maybe we should put it in
79 * a common header somewhere
81 static inline cputime64_t get_cpu_idle_time(unsigned int cpu)
83 cputime64_t retval;
85 retval = cputime64_add(kstat_cpu(cpu).cpustat.idle,
86 kstat_cpu(cpu).cpustat.iowait);
88 if (rackmeter_ignore_nice)
89 retval = cputime64_add(retval, kstat_cpu(cpu).cpustat.nice);
91 return retval;
94 static void rackmeter_setup_i2s(struct rackmeter *rm)
96 struct macio_chip *macio = rm->mdev->bus->chip;
98 /* First whack magic GPIO */
99 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, RACKMETER_MAGIC_GPIO, 5);
102 /* Call feature code to enable the sound channel and the proper
103 * clock sources
105 pmac_call_feature(PMAC_FTR_SOUND_CHIP_ENABLE, rm->i2s, 0, 1);
107 /* Power i2s and stop i2s clock. We whack MacIO FCRs directly for now.
108 * This is a bit racy, thus we should add new platform functions to
109 * handle that. snd-aoa needs that too
111 MACIO_BIS(KEYLARGO_FCR1, KL1_I2S0_ENABLE);
112 MACIO_BIC(KEYLARGO_FCR1, KL1_I2S0_CLK_ENABLE_BIT);
113 (void)MACIO_IN32(KEYLARGO_FCR1);
114 udelay(10);
116 /* Then setup i2s. For now, we use the same magic value that
117 * the OS X driver seems to use. We might want to play around
118 * with the clock divisors later
120 out_le32(rm->i2s_regs + 0x10, 0x01fa0000);
121 (void)in_le32(rm->i2s_regs + 0x10);
122 udelay(10);
124 /* Fully restart i2s*/
125 MACIO_BIS(KEYLARGO_FCR1, KL1_I2S0_CELL_ENABLE |
126 KL1_I2S0_CLK_ENABLE_BIT);
127 (void)MACIO_IN32(KEYLARGO_FCR1);
128 udelay(10);
131 static void rackmeter_set_default_pattern(struct rackmeter *rm)
133 int i;
135 for (i = 0; i < 16; i++) {
136 if (i < 8)
137 rm->ubuf[i] = (i & 1) * 255;
138 else
139 rm->ubuf[i] = ((~i) & 1) * 255;
143 static void rackmeter_do_pause(struct rackmeter *rm, int pause)
145 struct rackmeter_dma *rdma = rm->dma_buf_v;
147 pr_debug("rackmeter: %s\n", pause ? "paused" : "started");
149 rm->paused = pause;
150 if (pause) {
151 DBDMA_DO_STOP(rm->dma_regs);
152 return;
154 memset(rdma->buf1, 0, SAMPLE_COUNT & sizeof(u32));
155 memset(rdma->buf2, 0, SAMPLE_COUNT & sizeof(u32));
157 rm->dma_buf_v->mark = 0;
159 mb();
160 out_le32(&rm->dma_regs->cmdptr_hi, 0);
161 out_le32(&rm->dma_regs->cmdptr, rm->dma_buf_p);
162 out_le32(&rm->dma_regs->control, (RUN << 16) | RUN);
165 static void rackmeter_setup_dbdma(struct rackmeter *rm)
167 struct rackmeter_dma *db = rm->dma_buf_v;
168 struct dbdma_cmd *cmd = db->cmd;
170 /* Make sure dbdma is reset */
171 DBDMA_DO_RESET(rm->dma_regs);
173 pr_debug("rackmeter: mark offset=0x%zx\n",
174 offsetof(struct rackmeter_dma, mark));
175 pr_debug("rackmeter: buf1 offset=0x%zx\n",
176 offsetof(struct rackmeter_dma, buf1));
177 pr_debug("rackmeter: buf2 offset=0x%zx\n",
178 offsetof(struct rackmeter_dma, buf2));
180 /* Prepare 4 dbdma commands for the 2 buffers */
181 memset(cmd, 0, 4 * sizeof(struct dbdma_cmd));
182 st_le16(&cmd->req_count, 4);
183 st_le16(&cmd->command, STORE_WORD | INTR_ALWAYS | KEY_SYSTEM);
184 st_le32(&cmd->phy_addr, rm->dma_buf_p +
185 offsetof(struct rackmeter_dma, mark));
186 st_le32(&cmd->cmd_dep, 0x02000000);
187 cmd++;
189 st_le16(&cmd->req_count, SAMPLE_COUNT * 4);
190 st_le16(&cmd->command, OUTPUT_MORE);
191 st_le32(&cmd->phy_addr, rm->dma_buf_p +
192 offsetof(struct rackmeter_dma, buf1));
193 cmd++;
195 st_le16(&cmd->req_count, 4);
196 st_le16(&cmd->command, STORE_WORD | INTR_ALWAYS | KEY_SYSTEM);
197 st_le32(&cmd->phy_addr, rm->dma_buf_p +
198 offsetof(struct rackmeter_dma, mark));
199 st_le32(&cmd->cmd_dep, 0x01000000);
200 cmd++;
202 st_le16(&cmd->req_count, SAMPLE_COUNT * 4);
203 st_le16(&cmd->command, OUTPUT_MORE | BR_ALWAYS);
204 st_le32(&cmd->phy_addr, rm->dma_buf_p +
205 offsetof(struct rackmeter_dma, buf2));
206 st_le32(&cmd->cmd_dep, rm->dma_buf_p);
208 rackmeter_do_pause(rm, 0);
211 static void rackmeter_do_timer(struct work_struct *work)
213 struct rackmeter_cpu *rcpu =
214 container_of(work, struct rackmeter_cpu, sniffer.work);
215 struct rackmeter *rm = rcpu->rm;
216 unsigned int cpu = smp_processor_id();
217 cputime64_t cur_jiffies, total_idle_ticks;
218 unsigned int total_ticks, idle_ticks;
219 int i, offset, load, cumm, pause;
221 cur_jiffies = jiffies64_to_cputime64(get_jiffies_64());
222 total_ticks = (unsigned int)cputime64_sub(cur_jiffies,
223 rcpu->prev_wall);
224 rcpu->prev_wall = cur_jiffies;
226 total_idle_ticks = get_cpu_idle_time(cpu);
227 idle_ticks = (unsigned int) cputime64_sub(total_idle_ticks,
228 rcpu->prev_idle);
229 rcpu->prev_idle = total_idle_ticks;
231 /* We do a very dumb calculation to update the LEDs for now,
232 * we'll do better once we have actual PWM implemented
234 load = (9 * (total_ticks - idle_ticks)) / total_ticks;
236 offset = cpu << 3;
237 cumm = 0;
238 for (i = 0; i < 8; i++) {
239 u8 ub = (load > i) ? 0xff : 0;
240 rm->ubuf[i + offset] = ub;
241 cumm |= ub;
243 rcpu->zero = (cumm == 0);
245 /* Now check if LEDs are all 0, we can stop DMA */
246 pause = (rm->cpu[0].zero && rm->cpu[1].zero);
247 if (pause != rm->paused) {
248 mutex_lock(&rm->sem);
249 pause = (rm->cpu[0].zero && rm->cpu[1].zero);
250 rackmeter_do_pause(rm, pause);
251 mutex_unlock(&rm->sem);
253 schedule_delayed_work_on(cpu, &rcpu->sniffer,
254 msecs_to_jiffies(CPU_SAMPLING_RATE));
257 static void __devinit rackmeter_init_cpu_sniffer(struct rackmeter *rm)
259 unsigned int cpu;
261 /* This driver works only with 1 or 2 CPUs numbered 0 and 1,
262 * but that's really all we have on Apple Xserve. It doesn't
263 * play very nice with CPU hotplug neither but we don't do that
264 * on those machines yet
267 rm->cpu[0].rm = rm;
268 INIT_DELAYED_WORK(&rm->cpu[0].sniffer, rackmeter_do_timer);
269 rm->cpu[1].rm = rm;
270 INIT_DELAYED_WORK(&rm->cpu[1].sniffer, rackmeter_do_timer);
272 for_each_online_cpu(cpu) {
273 struct rackmeter_cpu *rcpu;
275 if (cpu > 1)
276 continue;
277 rcpu = &rm->cpu[cpu];
278 rcpu->prev_idle = get_cpu_idle_time(cpu);
279 rcpu->prev_wall = jiffies64_to_cputime64(get_jiffies_64());
280 schedule_delayed_work_on(cpu, &rm->cpu[cpu].sniffer,
281 msecs_to_jiffies(CPU_SAMPLING_RATE));
285 static void __devexit rackmeter_stop_cpu_sniffer(struct rackmeter *rm)
287 cancel_rearming_delayed_work(&rm->cpu[0].sniffer);
288 cancel_rearming_delayed_work(&rm->cpu[1].sniffer);
291 static int __devinit rackmeter_setup(struct rackmeter *rm)
293 pr_debug("rackmeter: setting up i2s..\n");
294 rackmeter_setup_i2s(rm);
296 pr_debug("rackmeter: setting up default pattern..\n");
297 rackmeter_set_default_pattern(rm);
299 pr_debug("rackmeter: setting up dbdma..\n");
300 rackmeter_setup_dbdma(rm);
302 pr_debug("rackmeter: start CPU measurements..\n");
303 rackmeter_init_cpu_sniffer(rm);
305 printk(KERN_INFO "RackMeter initialized\n");
307 return 0;
310 /* XXX FIXME: No PWM yet, this is 0/1 */
311 static u32 rackmeter_calc_sample(struct rackmeter *rm, unsigned int index)
313 int led;
314 u32 sample = 0;
316 for (led = 0; led < 16; led++) {
317 sample >>= 1;
318 sample |= ((rm->ubuf[led] >= 0x80) << 15);
320 return (sample << 17) | (sample >> 15);
323 static irqreturn_t rackmeter_irq(int irq, void *arg)
325 struct rackmeter *rm = arg;
326 struct rackmeter_dma *db = rm->dma_buf_v;
327 unsigned int mark, i;
328 u32 *buf;
330 /* Flush PCI buffers with an MMIO read. Maybe we could actually
331 * check the status one day ... in case things go wrong, though
332 * this never happened to me
334 (void)in_le32(&rm->dma_regs->status);
336 /* Make sure the CPU gets us in order */
337 rmb();
339 /* Read mark */
340 mark = db->mark;
341 if (mark != 1 && mark != 2) {
342 printk(KERN_WARNING "rackmeter: Incorrect DMA mark 0x%08x\n",
343 mark);
344 /* We allow for 3 errors like that (stale DBDMA irqs) */
345 if (++rm->stale_irq > 3) {
346 printk(KERN_ERR "rackmeter: Too many errors,"
347 " stopping DMA\n");
348 DBDMA_DO_RESET(rm->dma_regs);
350 return IRQ_HANDLED;
353 /* Next buffer we need to fill is mark value */
354 buf = mark == 1 ? db->buf1 : db->buf2;
356 /* Fill it now. This routine converts the 8 bits depth sample array
357 * into the PWM bitmap for each LED.
359 for (i = 0; i < SAMPLE_COUNT; i++)
360 buf[i] = rackmeter_calc_sample(rm, i);
363 return IRQ_HANDLED;
366 static int __devinit rackmeter_probe(struct macio_dev* mdev,
367 const struct of_device_id *match)
369 struct device_node *i2s = NULL, *np = NULL;
370 struct rackmeter *rm = NULL;
371 struct resource ri2s, rdma;
372 int rc = -ENODEV;
374 pr_debug("rackmeter_probe()\n");
376 /* Get i2s-a node */
377 while ((i2s = of_get_next_child(mdev->ofdev.node, i2s)) != NULL)
378 if (strcmp(i2s->name, "i2s-a") == 0)
379 break;
380 if (i2s == NULL) {
381 pr_debug(" i2s-a child not found\n");
382 goto bail;
384 /* Get lightshow or virtual sound */
385 while ((np = of_get_next_child(i2s, np)) != NULL) {
386 if (strcmp(np->name, "lightshow") == 0)
387 break;
388 if ((strcmp(np->name, "sound") == 0) &&
389 of_get_property(np, "virtual", NULL) != NULL)
390 break;
392 if (np == NULL) {
393 pr_debug(" lightshow or sound+virtual child not found\n");
394 goto bail;
397 /* Create and initialize our instance data */
398 rm = kzalloc(sizeof(struct rackmeter), GFP_KERNEL);
399 if (rm == NULL) {
400 printk(KERN_ERR "rackmeter: failed to allocate memory !\n");
401 rc = -ENOMEM;
402 goto bail_release;
404 rm->mdev = mdev;
405 rm->i2s = i2s;
406 mutex_init(&rm->sem);
407 dev_set_drvdata(&mdev->ofdev.dev, rm);
408 /* Check resources availability. We need at least resource 0 and 1 */
409 #if 0 /* Use that when i2s-a is finally an mdev per-se */
410 if (macio_resource_count(mdev) < 2 || macio_irq_count(mdev) < 2) {
411 printk(KERN_ERR
412 "rackmeter: found match but lacks resources: %s"
413 " (%d resources, %d interrupts)\n",
414 mdev->ofdev.node->full_name);
415 rc = -ENXIO;
416 goto bail_free;
418 if (macio_request_resources(mdev, "rackmeter")) {
419 printk(KERN_ERR
420 "rackmeter: failed to request resources: %s\n",
421 mdev->ofdev.node->full_name);
422 rc = -EBUSY;
423 goto bail_free;
425 rm->irq = macio_irq(mdev, 1);
426 #else
427 rm->irq = irq_of_parse_and_map(i2s, 1);
428 if (rm->irq == NO_IRQ ||
429 of_address_to_resource(i2s, 0, &ri2s) ||
430 of_address_to_resource(i2s, 1, &rdma)) {
431 printk(KERN_ERR
432 "rackmeter: found match but lacks resources: %s",
433 mdev->ofdev.node->full_name);
434 rc = -ENXIO;
435 goto bail_free;
437 #endif
439 pr_debug(" i2s @0x%08x\n", (unsigned int)ri2s.start);
440 pr_debug(" dma @0x%08x\n", (unsigned int)rdma.start);
441 pr_debug(" irq %d\n", rm->irq);
443 rm->ubuf = (u8 *)__get_free_page(GFP_KERNEL);
444 if (rm->ubuf == NULL) {
445 printk(KERN_ERR
446 "rackmeter: failed to allocate samples page !\n");
447 rc = -ENOMEM;
448 goto bail_release;
451 rm->dma_buf_v = dma_alloc_coherent(&macio_get_pci_dev(mdev)->dev,
452 sizeof(struct rackmeter_dma),
453 &rm->dma_buf_p, GFP_KERNEL);
454 if (rm->dma_buf_v == NULL) {
455 printk(KERN_ERR
456 "rackmeter: failed to allocate dma buffer !\n");
457 rc = -ENOMEM;
458 goto bail_free_samples;
460 #if 0
461 rm->i2s_regs = ioremap(macio_resource_start(mdev, 0), 0x1000);
462 #else
463 rm->i2s_regs = ioremap(ri2s.start, 0x1000);
464 #endif
465 if (rm->i2s_regs == NULL) {
466 printk(KERN_ERR
467 "rackmeter: failed to map i2s registers !\n");
468 rc = -ENXIO;
469 goto bail_free_dma;
471 #if 0
472 rm->dma_regs = ioremap(macio_resource_start(mdev, 1), 0x100);
473 #else
474 rm->dma_regs = ioremap(rdma.start, 0x100);
475 #endif
476 if (rm->dma_regs == NULL) {
477 printk(KERN_ERR
478 "rackmeter: failed to map dma registers !\n");
479 rc = -ENXIO;
480 goto bail_unmap_i2s;
483 rc = rackmeter_setup(rm);
484 if (rc) {
485 printk(KERN_ERR
486 "rackmeter: failed to initialize !\n");
487 rc = -ENXIO;
488 goto bail_unmap_dma;
491 rc = request_irq(rm->irq, rackmeter_irq, 0, "rackmeter", rm);
492 if (rc != 0) {
493 printk(KERN_ERR
494 "rackmeter: failed to request interrupt !\n");
495 goto bail_stop_dma;
497 of_node_put(np);
498 return 0;
500 bail_stop_dma:
501 DBDMA_DO_RESET(rm->dma_regs);
502 bail_unmap_dma:
503 iounmap(rm->dma_regs);
504 bail_unmap_i2s:
505 iounmap(rm->i2s_regs);
506 bail_free_dma:
507 dma_free_coherent(&macio_get_pci_dev(mdev)->dev,
508 sizeof(struct rackmeter_dma),
509 rm->dma_buf_v, rm->dma_buf_p);
510 bail_free_samples:
511 free_page((unsigned long)rm->ubuf);
512 bail_release:
513 #if 0
514 macio_release_resources(mdev);
515 #endif
516 bail_free:
517 kfree(rm);
518 bail:
519 of_node_put(i2s);
520 of_node_put(np);
521 dev_set_drvdata(&mdev->ofdev.dev, NULL);
522 return rc;
525 static int __devexit rackmeter_remove(struct macio_dev* mdev)
527 struct rackmeter *rm = dev_get_drvdata(&mdev->ofdev.dev);
529 /* Stop CPU sniffer timer & work queues */
530 rackmeter_stop_cpu_sniffer(rm);
532 /* Clear reference to private data */
533 dev_set_drvdata(&mdev->ofdev.dev, NULL);
535 /* Stop/reset dbdma */
536 DBDMA_DO_RESET(rm->dma_regs);
538 /* Release the IRQ */
539 free_irq(rm->irq, rm);
541 /* Unmap registers */
542 iounmap(rm->dma_regs);
543 iounmap(rm->i2s_regs);
545 /* Free DMA */
546 dma_free_coherent(&macio_get_pci_dev(mdev)->dev,
547 sizeof(struct rackmeter_dma),
548 rm->dma_buf_v, rm->dma_buf_p);
550 /* Free samples */
551 free_page((unsigned long)rm->ubuf);
553 #if 0
554 /* Release resources */
555 macio_release_resources(mdev);
556 #endif
558 /* Get rid of me */
559 kfree(rm);
561 return 0;
564 static int rackmeter_shutdown(struct macio_dev* mdev)
566 struct rackmeter *rm = dev_get_drvdata(&mdev->ofdev.dev);
568 if (rm == NULL)
569 return -ENODEV;
571 /* Stop CPU sniffer timer & work queues */
572 rackmeter_stop_cpu_sniffer(rm);
574 /* Stop/reset dbdma */
575 DBDMA_DO_RESET(rm->dma_regs);
577 return 0;
580 static struct of_device_id rackmeter_match[] = {
581 { .name = "i2s" },
585 static struct macio_driver rackmeter_driver = {
586 .name = "rackmeter",
587 .owner = THIS_MODULE,
588 .match_table = rackmeter_match,
589 .probe = rackmeter_probe,
590 .remove = __devexit_p(rackmeter_remove),
591 .shutdown = rackmeter_shutdown,
595 static int __init rackmeter_init(void)
597 pr_debug("rackmeter_init()\n");
599 return macio_register_driver(&rackmeter_driver);
602 static void __exit rackmeter_exit(void)
604 pr_debug("rackmeter_exit()\n");
606 macio_unregister_driver(&rackmeter_driver);
609 module_init(rackmeter_init);
610 module_exit(rackmeter_exit);
613 MODULE_LICENSE("GPL");
614 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
615 MODULE_DESCRIPTION("RackMeter: Support vu-meter on XServe front panel");