esp_scsi: spellcheck 'driver'
[linux/fpc-iii.git] / drivers / macintosh / rack-meter.c
blob4192901cab406ca42580c13c7af4c3fcdbdd8fe1
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/slab.h>
22 #include <linux/device.h>
23 #include <linux/interrupt.h>
24 #include <linux/module.h>
25 #include <linux/pci.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/kernel_stat.h>
28 #include <linux/of_address.h>
29 #include <linux/of_irq.h>
31 #include <asm/io.h>
32 #include <asm/prom.h>
33 #include <asm/machdep.h>
34 #include <asm/pmac_feature.h>
35 #include <asm/dbdma.h>
36 #include <asm/macio.h>
37 #include <asm/keylargo.h>
39 /* Number of samples in a sample buffer */
40 #define SAMPLE_COUNT 256
42 /* CPU meter sampling rate in ms */
43 #define CPU_SAMPLING_RATE 250
45 struct rackmeter_dma {
46 struct dbdma_cmd cmd[4] ____cacheline_aligned;
47 u32 mark ____cacheline_aligned;
48 u32 buf1[SAMPLE_COUNT] ____cacheline_aligned;
49 u32 buf2[SAMPLE_COUNT] ____cacheline_aligned;
50 } ____cacheline_aligned;
52 struct rackmeter_cpu {
53 struct delayed_work sniffer;
54 struct rackmeter *rm;
55 cputime64_t prev_wall;
56 cputime64_t prev_idle;
57 int zero;
58 } ____cacheline_aligned;
60 struct rackmeter {
61 struct macio_dev *mdev;
62 unsigned int irq;
63 struct device_node *i2s;
64 u8 *ubuf;
65 struct dbdma_regs __iomem *dma_regs;
66 void __iomem *i2s_regs;
67 dma_addr_t dma_buf_p;
68 struct rackmeter_dma *dma_buf_v;
69 int stale_irq;
70 struct rackmeter_cpu cpu[2];
71 int paused;
72 struct mutex sem;
75 /* To be set as a tunable */
76 static int rackmeter_ignore_nice;
78 /* This GPIO is whacked by the OS X driver when initializing */
79 #define RACKMETER_MAGIC_GPIO 0x78
81 /* This is copied from cpufreq_ondemand, maybe we should put it in
82 * a common header somewhere
84 static inline cputime64_t get_cpu_idle_time(unsigned int cpu)
86 u64 retval;
88 retval = kcpustat_cpu(cpu).cpustat[CPUTIME_IDLE] +
89 kcpustat_cpu(cpu).cpustat[CPUTIME_IOWAIT];
91 if (rackmeter_ignore_nice)
92 retval += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
94 return retval;
97 static void rackmeter_setup_i2s(struct rackmeter *rm)
99 struct macio_chip *macio = rm->mdev->bus->chip;
101 /* First whack magic GPIO */
102 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, RACKMETER_MAGIC_GPIO, 5);
105 /* Call feature code to enable the sound channel and the proper
106 * clock sources
108 pmac_call_feature(PMAC_FTR_SOUND_CHIP_ENABLE, rm->i2s, 0, 1);
110 /* Power i2s and stop i2s clock. We whack MacIO FCRs directly for now.
111 * This is a bit racy, thus we should add new platform functions to
112 * handle that. snd-aoa needs that too
114 MACIO_BIS(KEYLARGO_FCR1, KL1_I2S0_ENABLE);
115 MACIO_BIC(KEYLARGO_FCR1, KL1_I2S0_CLK_ENABLE_BIT);
116 (void)MACIO_IN32(KEYLARGO_FCR1);
117 udelay(10);
119 /* Then setup i2s. For now, we use the same magic value that
120 * the OS X driver seems to use. We might want to play around
121 * with the clock divisors later
123 out_le32(rm->i2s_regs + 0x10, 0x01fa0000);
124 (void)in_le32(rm->i2s_regs + 0x10);
125 udelay(10);
127 /* Fully restart i2s*/
128 MACIO_BIS(KEYLARGO_FCR1, KL1_I2S0_CELL_ENABLE |
129 KL1_I2S0_CLK_ENABLE_BIT);
130 (void)MACIO_IN32(KEYLARGO_FCR1);
131 udelay(10);
134 static void rackmeter_set_default_pattern(struct rackmeter *rm)
136 int i;
138 for (i = 0; i < 16; i++) {
139 if (i < 8)
140 rm->ubuf[i] = (i & 1) * 255;
141 else
142 rm->ubuf[i] = ((~i) & 1) * 255;
146 static void rackmeter_do_pause(struct rackmeter *rm, int pause)
148 struct rackmeter_dma *rdma = rm->dma_buf_v;
150 pr_debug("rackmeter: %s\n", pause ? "paused" : "started");
152 rm->paused = pause;
153 if (pause) {
154 DBDMA_DO_STOP(rm->dma_regs);
155 return;
157 memset(rdma->buf1, 0, SAMPLE_COUNT & sizeof(u32));
158 memset(rdma->buf2, 0, SAMPLE_COUNT & sizeof(u32));
160 rm->dma_buf_v->mark = 0;
162 mb();
163 out_le32(&rm->dma_regs->cmdptr_hi, 0);
164 out_le32(&rm->dma_regs->cmdptr, rm->dma_buf_p);
165 out_le32(&rm->dma_regs->control, (RUN << 16) | RUN);
168 static void rackmeter_setup_dbdma(struct rackmeter *rm)
170 struct rackmeter_dma *db = rm->dma_buf_v;
171 struct dbdma_cmd *cmd = db->cmd;
173 /* Make sure dbdma is reset */
174 DBDMA_DO_RESET(rm->dma_regs);
176 pr_debug("rackmeter: mark offset=0x%zx\n",
177 offsetof(struct rackmeter_dma, mark));
178 pr_debug("rackmeter: buf1 offset=0x%zx\n",
179 offsetof(struct rackmeter_dma, buf1));
180 pr_debug("rackmeter: buf2 offset=0x%zx\n",
181 offsetof(struct rackmeter_dma, buf2));
183 /* Prepare 4 dbdma commands for the 2 buffers */
184 memset(cmd, 0, 4 * sizeof(struct dbdma_cmd));
185 st_le16(&cmd->req_count, 4);
186 st_le16(&cmd->command, STORE_WORD | INTR_ALWAYS | KEY_SYSTEM);
187 st_le32(&cmd->phy_addr, rm->dma_buf_p +
188 offsetof(struct rackmeter_dma, mark));
189 st_le32(&cmd->cmd_dep, 0x02000000);
190 cmd++;
192 st_le16(&cmd->req_count, SAMPLE_COUNT * 4);
193 st_le16(&cmd->command, OUTPUT_MORE);
194 st_le32(&cmd->phy_addr, rm->dma_buf_p +
195 offsetof(struct rackmeter_dma, buf1));
196 cmd++;
198 st_le16(&cmd->req_count, 4);
199 st_le16(&cmd->command, STORE_WORD | INTR_ALWAYS | KEY_SYSTEM);
200 st_le32(&cmd->phy_addr, rm->dma_buf_p +
201 offsetof(struct rackmeter_dma, mark));
202 st_le32(&cmd->cmd_dep, 0x01000000);
203 cmd++;
205 st_le16(&cmd->req_count, SAMPLE_COUNT * 4);
206 st_le16(&cmd->command, OUTPUT_MORE | BR_ALWAYS);
207 st_le32(&cmd->phy_addr, rm->dma_buf_p +
208 offsetof(struct rackmeter_dma, buf2));
209 st_le32(&cmd->cmd_dep, rm->dma_buf_p);
211 rackmeter_do_pause(rm, 0);
214 static void rackmeter_do_timer(struct work_struct *work)
216 struct rackmeter_cpu *rcpu =
217 container_of(work, struct rackmeter_cpu, sniffer.work);
218 struct rackmeter *rm = rcpu->rm;
219 unsigned int cpu = smp_processor_id();
220 cputime64_t cur_jiffies, total_idle_ticks;
221 unsigned int total_ticks, idle_ticks;
222 int i, offset, load, cumm, pause;
224 cur_jiffies = jiffies64_to_cputime64(get_jiffies_64());
225 total_ticks = (unsigned int) (cur_jiffies - rcpu->prev_wall);
226 rcpu->prev_wall = cur_jiffies;
228 total_idle_ticks = get_cpu_idle_time(cpu);
229 idle_ticks = (unsigned int) (total_idle_ticks - rcpu->prev_idle);
230 rcpu->prev_idle = total_idle_ticks;
232 /* We do a very dumb calculation to update the LEDs for now,
233 * we'll do better once we have actual PWM implemented
235 load = (9 * (total_ticks - idle_ticks)) / total_ticks;
237 offset = cpu << 3;
238 cumm = 0;
239 for (i = 0; i < 8; i++) {
240 u8 ub = (load > i) ? 0xff : 0;
241 rm->ubuf[i + offset] = ub;
242 cumm |= ub;
244 rcpu->zero = (cumm == 0);
246 /* Now check if LEDs are all 0, we can stop DMA */
247 pause = (rm->cpu[0].zero && rm->cpu[1].zero);
248 if (pause != rm->paused) {
249 mutex_lock(&rm->sem);
250 pause = (rm->cpu[0].zero && rm->cpu[1].zero);
251 rackmeter_do_pause(rm, pause);
252 mutex_unlock(&rm->sem);
254 schedule_delayed_work_on(cpu, &rcpu->sniffer,
255 msecs_to_jiffies(CPU_SAMPLING_RATE));
258 static void rackmeter_init_cpu_sniffer(struct rackmeter *rm)
260 unsigned int cpu;
262 /* This driver works only with 1 or 2 CPUs numbered 0 and 1,
263 * but that's really all we have on Apple Xserve. It doesn't
264 * play very nice with CPU hotplug neither but we don't do that
265 * on those machines yet
268 rm->cpu[0].rm = rm;
269 INIT_DELAYED_WORK(&rm->cpu[0].sniffer, rackmeter_do_timer);
270 rm->cpu[1].rm = rm;
271 INIT_DELAYED_WORK(&rm->cpu[1].sniffer, rackmeter_do_timer);
273 for_each_online_cpu(cpu) {
274 struct rackmeter_cpu *rcpu;
276 if (cpu > 1)
277 continue;
278 rcpu = &rm->cpu[cpu];
279 rcpu->prev_idle = get_cpu_idle_time(cpu);
280 rcpu->prev_wall = jiffies64_to_cputime64(get_jiffies_64());
281 schedule_delayed_work_on(cpu, &rm->cpu[cpu].sniffer,
282 msecs_to_jiffies(CPU_SAMPLING_RATE));
286 static void rackmeter_stop_cpu_sniffer(struct rackmeter *rm)
288 cancel_delayed_work_sync(&rm->cpu[0].sniffer);
289 cancel_delayed_work_sync(&rm->cpu[1].sniffer);
292 static int rackmeter_setup(struct rackmeter *rm)
294 pr_debug("rackmeter: setting up i2s..\n");
295 rackmeter_setup_i2s(rm);
297 pr_debug("rackmeter: setting up default pattern..\n");
298 rackmeter_set_default_pattern(rm);
300 pr_debug("rackmeter: setting up dbdma..\n");
301 rackmeter_setup_dbdma(rm);
303 pr_debug("rackmeter: start CPU measurements..\n");
304 rackmeter_init_cpu_sniffer(rm);
306 printk(KERN_INFO "RackMeter initialized\n");
308 return 0;
311 /* XXX FIXME: No PWM yet, this is 0/1 */
312 static u32 rackmeter_calc_sample(struct rackmeter *rm, unsigned int index)
314 int led;
315 u32 sample = 0;
317 for (led = 0; led < 16; led++) {
318 sample >>= 1;
319 sample |= ((rm->ubuf[led] >= 0x80) << 15);
321 return (sample << 17) | (sample >> 15);
324 static irqreturn_t rackmeter_irq(int irq, void *arg)
326 struct rackmeter *rm = arg;
327 struct rackmeter_dma *db = rm->dma_buf_v;
328 unsigned int mark, i;
329 u32 *buf;
331 /* Flush PCI buffers with an MMIO read. Maybe we could actually
332 * check the status one day ... in case things go wrong, though
333 * this never happened to me
335 (void)in_le32(&rm->dma_regs->status);
337 /* Make sure the CPU gets us in order */
338 rmb();
340 /* Read mark */
341 mark = db->mark;
342 if (mark != 1 && mark != 2) {
343 printk(KERN_WARNING "rackmeter: Incorrect DMA mark 0x%08x\n",
344 mark);
345 /* We allow for 3 errors like that (stale DBDMA irqs) */
346 if (++rm->stale_irq > 3) {
347 printk(KERN_ERR "rackmeter: Too many errors,"
348 " stopping DMA\n");
349 DBDMA_DO_RESET(rm->dma_regs);
351 return IRQ_HANDLED;
354 /* Next buffer we need to fill is mark value */
355 buf = mark == 1 ? db->buf1 : db->buf2;
357 /* Fill it now. This routine converts the 8 bits depth sample array
358 * into the PWM bitmap for each LED.
360 for (i = 0; i < SAMPLE_COUNT; i++)
361 buf[i] = rackmeter_calc_sample(rm, i);
364 return IRQ_HANDLED;
367 static int rackmeter_probe(struct macio_dev* mdev,
368 const struct of_device_id *match)
370 struct device_node *i2s = NULL, *np = NULL;
371 struct rackmeter *rm = NULL;
372 struct resource ri2s, rdma;
373 int rc = -ENODEV;
375 pr_debug("rackmeter_probe()\n");
377 /* Get i2s-a node */
378 while ((i2s = of_get_next_child(mdev->ofdev.dev.of_node, i2s)) != NULL)
379 if (strcmp(i2s->name, "i2s-a") == 0)
380 break;
381 if (i2s == NULL) {
382 pr_debug(" i2s-a child not found\n");
383 goto bail;
385 /* Get lightshow or virtual sound */
386 while ((np = of_get_next_child(i2s, np)) != NULL) {
387 if (strcmp(np->name, "lightshow") == 0)
388 break;
389 if ((strcmp(np->name, "sound") == 0) &&
390 of_get_property(np, "virtual", NULL) != NULL)
391 break;
393 if (np == NULL) {
394 pr_debug(" lightshow or sound+virtual child not found\n");
395 goto bail;
398 /* Create and initialize our instance data */
399 rm = kzalloc(sizeof(struct rackmeter), GFP_KERNEL);
400 if (rm == NULL) {
401 printk(KERN_ERR "rackmeter: failed to allocate memory !\n");
402 rc = -ENOMEM;
403 goto bail_release;
405 rm->mdev = mdev;
406 rm->i2s = i2s;
407 mutex_init(&rm->sem);
408 dev_set_drvdata(&mdev->ofdev.dev, rm);
409 /* Check resources availability. We need at least resource 0 and 1 */
410 #if 0 /* Use that when i2s-a is finally an mdev per-se */
411 if (macio_resource_count(mdev) < 2 || macio_irq_count(mdev) < 2) {
412 printk(KERN_ERR
413 "rackmeter: found match but lacks resources: %s"
414 " (%d resources, %d interrupts)\n",
415 mdev->ofdev.node->full_name);
416 rc = -ENXIO;
417 goto bail_free;
419 if (macio_request_resources(mdev, "rackmeter")) {
420 printk(KERN_ERR
421 "rackmeter: failed to request resources: %s\n",
422 mdev->ofdev.node->full_name);
423 rc = -EBUSY;
424 goto bail_free;
426 rm->irq = macio_irq(mdev, 1);
427 #else
428 rm->irq = irq_of_parse_and_map(i2s, 1);
429 if (rm->irq == NO_IRQ ||
430 of_address_to_resource(i2s, 0, &ri2s) ||
431 of_address_to_resource(i2s, 1, &rdma)) {
432 printk(KERN_ERR
433 "rackmeter: found match but lacks resources: %s",
434 mdev->ofdev.dev.of_node->full_name);
435 rc = -ENXIO;
436 goto bail_free;
438 #endif
440 pr_debug(" i2s @0x%08x\n", (unsigned int)ri2s.start);
441 pr_debug(" dma @0x%08x\n", (unsigned int)rdma.start);
442 pr_debug(" irq %d\n", rm->irq);
444 rm->ubuf = (u8 *)__get_free_page(GFP_KERNEL);
445 if (rm->ubuf == NULL) {
446 printk(KERN_ERR
447 "rackmeter: failed to allocate samples page !\n");
448 rc = -ENOMEM;
449 goto bail_release;
452 rm->dma_buf_v = dma_alloc_coherent(&macio_get_pci_dev(mdev)->dev,
453 sizeof(struct rackmeter_dma),
454 &rm->dma_buf_p, GFP_KERNEL);
455 if (rm->dma_buf_v == NULL) {
456 printk(KERN_ERR
457 "rackmeter: failed to allocate dma buffer !\n");
458 rc = -ENOMEM;
459 goto bail_free_samples;
461 #if 0
462 rm->i2s_regs = ioremap(macio_resource_start(mdev, 0), 0x1000);
463 #else
464 rm->i2s_regs = ioremap(ri2s.start, 0x1000);
465 #endif
466 if (rm->i2s_regs == NULL) {
467 printk(KERN_ERR
468 "rackmeter: failed to map i2s registers !\n");
469 rc = -ENXIO;
470 goto bail_free_dma;
472 #if 0
473 rm->dma_regs = ioremap(macio_resource_start(mdev, 1), 0x100);
474 #else
475 rm->dma_regs = ioremap(rdma.start, 0x100);
476 #endif
477 if (rm->dma_regs == NULL) {
478 printk(KERN_ERR
479 "rackmeter: failed to map dma registers !\n");
480 rc = -ENXIO;
481 goto bail_unmap_i2s;
484 rc = rackmeter_setup(rm);
485 if (rc) {
486 printk(KERN_ERR
487 "rackmeter: failed to initialize !\n");
488 rc = -ENXIO;
489 goto bail_unmap_dma;
492 rc = request_irq(rm->irq, rackmeter_irq, 0, "rackmeter", rm);
493 if (rc != 0) {
494 printk(KERN_ERR
495 "rackmeter: failed to request interrupt !\n");
496 goto bail_stop_dma;
498 of_node_put(np);
499 return 0;
501 bail_stop_dma:
502 DBDMA_DO_RESET(rm->dma_regs);
503 bail_unmap_dma:
504 iounmap(rm->dma_regs);
505 bail_unmap_i2s:
506 iounmap(rm->i2s_regs);
507 bail_free_dma:
508 dma_free_coherent(&macio_get_pci_dev(mdev)->dev,
509 sizeof(struct rackmeter_dma),
510 rm->dma_buf_v, rm->dma_buf_p);
511 bail_free_samples:
512 free_page((unsigned long)rm->ubuf);
513 bail_release:
514 #if 0
515 macio_release_resources(mdev);
516 #endif
517 bail_free:
518 kfree(rm);
519 bail:
520 of_node_put(i2s);
521 of_node_put(np);
522 dev_set_drvdata(&mdev->ofdev.dev, NULL);
523 return rc;
526 static int rackmeter_remove(struct macio_dev* mdev)
528 struct rackmeter *rm = dev_get_drvdata(&mdev->ofdev.dev);
530 /* Stop CPU sniffer timer & work queues */
531 rackmeter_stop_cpu_sniffer(rm);
533 /* Clear reference to private data */
534 dev_set_drvdata(&mdev->ofdev.dev, NULL);
536 /* Stop/reset dbdma */
537 DBDMA_DO_RESET(rm->dma_regs);
539 /* Release the IRQ */
540 free_irq(rm->irq, rm);
542 /* Unmap registers */
543 iounmap(rm->dma_regs);
544 iounmap(rm->i2s_regs);
546 /* Free DMA */
547 dma_free_coherent(&macio_get_pci_dev(mdev)->dev,
548 sizeof(struct rackmeter_dma),
549 rm->dma_buf_v, rm->dma_buf_p);
551 /* Free samples */
552 free_page((unsigned long)rm->ubuf);
554 #if 0
555 /* Release resources */
556 macio_release_resources(mdev);
557 #endif
559 /* Get rid of me */
560 kfree(rm);
562 return 0;
565 static int rackmeter_shutdown(struct macio_dev* mdev)
567 struct rackmeter *rm = dev_get_drvdata(&mdev->ofdev.dev);
569 if (rm == NULL)
570 return -ENODEV;
572 /* Stop CPU sniffer timer & work queues */
573 rackmeter_stop_cpu_sniffer(rm);
575 /* Stop/reset dbdma */
576 DBDMA_DO_RESET(rm->dma_regs);
578 return 0;
581 static struct of_device_id rackmeter_match[] = {
582 { .name = "i2s" },
586 static struct macio_driver rackmeter_driver = {
587 .driver = {
588 .name = "rackmeter",
589 .owner = THIS_MODULE,
590 .of_match_table = rackmeter_match,
592 .probe = rackmeter_probe,
593 .remove = rackmeter_remove,
594 .shutdown = rackmeter_shutdown,
598 static int __init rackmeter_init(void)
600 pr_debug("rackmeter_init()\n");
602 return macio_register_driver(&rackmeter_driver);
605 static void __exit rackmeter_exit(void)
607 pr_debug("rackmeter_exit()\n");
609 macio_unregister_driver(&rackmeter_driver);
612 module_init(rackmeter_init);
613 module_exit(rackmeter_exit);
616 MODULE_LICENSE("GPL");
617 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
618 MODULE_DESCRIPTION("RackMeter: Support vu-meter on XServe front panel");