Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[deliverable/linux.git] / drivers / macintosh / rack-meter.c
CommitLineData
3e00a5ae
BH
1/*
2 * RackMac vu-meter driver
3 *
4 * (c) Copyright 2006 Benjamin Herrenschmidt, IBM Corp.
5 * <benh@kernel.crashing.org>
6 *
7 * Released under the term of the GNU GPL v2.
8 *
9 * Support the CPU-meter LEDs of the Xserve G5
10 *
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 :-)
15 *
16 */
17#undef DEBUG
18
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>
27
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/dbdma.h>
34#include <asm/macio.h>
35#include <asm/keylargo.h>
36
37/* Number of samples in a sample buffer */
38#define SAMPLE_COUNT 256
39
40/* CPU meter sampling rate in ms */
41#define CPU_SAMPLING_RATE 250
42
43struct rackmeter_dma {
44 struct dbdma_cmd cmd[4] ____cacheline_aligned;
45 u32 mark ____cacheline_aligned;
46 u32 buf1[SAMPLE_COUNT] ____cacheline_aligned;
47 u32 buf2[SAMPLE_COUNT] ____cacheline_aligned;
48} ____cacheline_aligned;
49
50struct rackmeter_cpu {
51 struct work_struct sniffer;
52 cputime64_t prev_wall;
53 cputime64_t prev_idle;
54 int zero;
55} ____cacheline_aligned;
56
57struct 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;
70};
71
72/* To be set as a tunable */
73static int rackmeter_ignore_nice;
74
75/* This GPIO is whacked by the OS X driver when initializing */
76#define RACKMETER_MAGIC_GPIO 0x78
77
78/* This is copied from cpufreq_ondemand, maybe we should put it in
79 * a common header somewhere
80 */
81static inline cputime64_t get_cpu_idle_time(unsigned int cpu)
82{
83 cputime64_t retval;
84
85 retval = cputime64_add(kstat_cpu(cpu).cpustat.idle,
86 kstat_cpu(cpu).cpustat.iowait);
87
88 if (rackmeter_ignore_nice)
89 retval = cputime64_add(retval, kstat_cpu(cpu).cpustat.nice);
90
91 return retval;
92}
93
94static void rackmeter_setup_i2s(struct rackmeter *rm)
95{
96 struct macio_chip *macio = rm->mdev->bus->chip;
97
98 /* First whack magic GPIO */
99 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, RACKMETER_MAGIC_GPIO, 5);
100
101
102 /* Call feature code to enable the sound channel and the proper
103 * clock sources
104 */
105 pmac_call_feature(PMAC_FTR_SOUND_CHIP_ENABLE, rm->i2s, 0, 1);
106
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
110 */
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);
115
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
119 */
120 out_le32(rm->i2s_regs + 0x10, 0x01fa0000);
121 (void)in_le32(rm->i2s_regs + 0x10);
122 udelay(10);
123
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);
129}
130
131static void rackmeter_set_default_pattern(struct rackmeter *rm)
132{
133 int i;
134
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;
140 }
141}
142
143static void rackmeter_do_pause(struct rackmeter *rm, int pause)
144{
145 struct rackmeter_dma *rdma = rm->dma_buf_v;
146
147 pr_debug("rackmeter: %s\n", pause ? "paused" : "started");
148
149 rm->paused = pause;
150 if (pause) {
151 DBDMA_DO_STOP(rm->dma_regs);
152 return;
153 }
154 memset(rdma->buf1, 0, SAMPLE_COUNT & sizeof(u32));
155 memset(rdma->buf2, 0, SAMPLE_COUNT & sizeof(u32));
156
157 rm->dma_buf_v->mark = 0;
158
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);
163}
164
165static void rackmeter_setup_dbdma(struct rackmeter *rm)
166{
167 struct rackmeter_dma *db = rm->dma_buf_v;
168 struct dbdma_cmd *cmd = db->cmd;
169
170 /* Make sure dbdma is reset */
171 DBDMA_DO_RESET(rm->dma_regs);
172
173 pr_debug("rackmeter: mark offset=0x%lx\n",
174 offsetof(struct rackmeter_dma, mark));
175 pr_debug("rackmeter: buf1 offset=0x%lx\n",
176 offsetof(struct rackmeter_dma, buf1));
177 pr_debug("rackmeter: buf2 offset=0x%lx\n",
178 offsetof(struct rackmeter_dma, buf2));
179
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++;
188
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++;
194
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++;
201
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);
207
208 rackmeter_do_pause(rm, 0);
209}
210
211static void rackmeter_do_timer(void *data)
212{
213 struct rackmeter *rm = data;
214 unsigned int cpu = smp_processor_id();
215 struct rackmeter_cpu *rcpu = &rm->cpu[cpu];
216 cputime64_t cur_jiffies, total_idle_ticks;
217 unsigned int total_ticks, idle_ticks;
218 int i, offset, load, cumm, pause;
219
220 cur_jiffies = jiffies64_to_cputime64(get_jiffies_64());
221 total_ticks = (unsigned int)cputime64_sub(cur_jiffies,
222 rcpu->prev_wall);
223 rcpu->prev_wall = cur_jiffies;
224
225 total_idle_ticks = get_cpu_idle_time(cpu);
226 idle_ticks = (unsigned int) cputime64_sub(total_idle_ticks,
227 rcpu->prev_idle);
228 rcpu->prev_idle = total_idle_ticks;
229
230 /* We do a very dumb calculation to update the LEDs for now,
231 * we'll do better once we have actual PWM implemented
232 */
233 load = (9 * (total_ticks - idle_ticks)) / total_ticks;
234
235 offset = cpu << 3;
236 cumm = 0;
237 for (i = 0; i < 8; i++) {
238 u8 ub = (load > i) ? 0xff : 0;
239 rm->ubuf[i + offset] = ub;
240 cumm |= ub;
241 }
242 rcpu->zero = (cumm == 0);
243
244 /* Now check if LEDs are all 0, we can stop DMA */
245 pause = (rm->cpu[0].zero && rm->cpu[1].zero);
246 if (pause != rm->paused) {
247 mutex_lock(&rm->sem);
248 pause = (rm->cpu[0].zero && rm->cpu[1].zero);
249 rackmeter_do_pause(rm, pause);
250 mutex_unlock(&rm->sem);
251 }
252 schedule_delayed_work_on(cpu, &rcpu->sniffer,
253 msecs_to_jiffies(CPU_SAMPLING_RATE));
254}
255
256static void __devinit rackmeter_init_cpu_sniffer(struct rackmeter *rm)
257{
258 unsigned int cpu;
259
260 /* This driver works only with 1 or 2 CPUs numbered 0 and 1,
261 * but that's really all we have on Apple Xserve. It doesn't
262 * play very nice with CPU hotplug neither but we don't do that
263 * on those machines yet
264 */
265
266 INIT_WORK(&rm->cpu[0].sniffer, rackmeter_do_timer, rm);
267 INIT_WORK(&rm->cpu[1].sniffer, rackmeter_do_timer, rm);
268
269 for_each_online_cpu(cpu) {
270 struct rackmeter_cpu *rcpu;
271
272 if (cpu > 1)
273 continue;
274 rcpu = &rm->cpu[cpu];;
275 rcpu->prev_idle = get_cpu_idle_time(cpu);
276 rcpu->prev_wall = jiffies64_to_cputime64(get_jiffies_64());
277 schedule_delayed_work_on(cpu, &rm->cpu[cpu].sniffer,
278 msecs_to_jiffies(CPU_SAMPLING_RATE));
279 }
280}
281
282static void __devexit rackmeter_stop_cpu_sniffer(struct rackmeter *rm)
283{
284 cancel_rearming_delayed_work(&rm->cpu[0].sniffer);
285 cancel_rearming_delayed_work(&rm->cpu[1].sniffer);
286}
287
288static int rackmeter_setup(struct rackmeter *rm)
289{
290 pr_debug("rackmeter: setting up i2s..\n");
291 rackmeter_setup_i2s(rm);
292
293 pr_debug("rackmeter: setting up default pattern..\n");
294 rackmeter_set_default_pattern(rm);
295
296 pr_debug("rackmeter: setting up dbdma..\n");
297 rackmeter_setup_dbdma(rm);
298
299 pr_debug("rackmeter: start CPU measurements..\n");
300 rackmeter_init_cpu_sniffer(rm);
301
302 printk(KERN_INFO "RackMeter initialized\n");
303
304 return 0;
305}
306
307/* XXX FIXME: No PWM yet, this is 0/1 */
308static u32 rackmeter_calc_sample(struct rackmeter *rm, unsigned int index)
309{
310 int led;
311 u32 sample = 0;
312
313 for (led = 0; led < 16; led++) {
314 sample >>= 1;
315 sample |= ((rm->ubuf[led] >= 0x80) << 15);
316 }
317 return (sample << 17) | (sample >> 15);
318}
319
320static irqreturn_t rackmeter_irq(int irq, void *arg)
321{
322 struct rackmeter *rm = arg;
323 struct rackmeter_dma *db = rm->dma_buf_v;
324 unsigned int mark, i;
325 u32 *buf;
326
327 /* Flush PCI buffers with an MMIO read. Maybe we could actually
328 * check the status one day ... in case things go wrong, though
329 * this never happened to me
330 */
331 (void)in_le32(&rm->dma_regs->status);
332
333 /* Make sure the CPU gets us in order */
334 rmb();
335
336 /* Read mark */
337 mark = db->mark;
338 if (mark != 1 && mark != 2) {
339 printk(KERN_WARNING "rackmeter: Incorrect DMA mark 0x%08x\n",
340 mark);
341 /* We allow for 3 errors like that (stale DBDMA irqs) */
342 if (++rm->stale_irq > 3) {
343 printk(KERN_ERR "rackmeter: Too many errors,"
344 " stopping DMA\n");
345 DBDMA_DO_RESET(rm->dma_regs);
346 }
347 return IRQ_HANDLED;
348 }
349
350 /* Next buffer we need to fill is mark value */
351 buf = mark == 1 ? db->buf1 : db->buf2;
352
353 /* Fill it now. This routine converts the 8 bits depth sample array
354 * into the PWM bitmap for each LED.
355 */
356 for (i = 0; i < SAMPLE_COUNT; i++)
357 buf[i] = rackmeter_calc_sample(rm, i);
358
359
360 return IRQ_HANDLED;
361}
362
363static int __devinit rackmeter_probe(struct macio_dev* mdev,
364 const struct of_device_id *match)
365{
366 struct device_node *i2s = NULL, *np = NULL;
367 struct rackmeter *rm = NULL;
368 struct resource ri2s, rdma;
369 int rc = -ENODEV;
370
371 pr_debug("rackmeter_probe()\n");
372
373 /* Get i2s-a node */
374 while ((i2s = of_get_next_child(mdev->ofdev.node, i2s)) != NULL)
375 if (strcmp(i2s->name, "i2s-a") == 0)
376 break;
377 if (i2s == NULL) {
378 pr_debug(" i2s-a child not found\n");
379 goto bail;
380 }
381 /* Get lightshow or virtual sound */
382 while ((np = of_get_next_child(i2s, np)) != NULL) {
383 if (strcmp(np->name, "lightshow") == 0)
384 break;
385 if ((strcmp(np->name, "sound") == 0) &&
386 get_property(np, "virtual", NULL) != NULL)
387 break;
388 }
389 if (np == NULL) {
390 pr_debug(" lightshow or sound+virtual child not found\n");
391 goto bail;
392 }
393
394 /* Create and initialize our instance data */
395 rm = kzalloc(sizeof(struct rackmeter), GFP_KERNEL);
396 if (rm == NULL) {
397 printk(KERN_ERR "rackmeter: failed to allocate memory !\n");
398 rc = -ENOMEM;
399 goto bail_release;
400 }
401 rm->mdev = mdev;
402 rm->i2s = i2s;
403 mutex_init(&rm->sem);
404 dev_set_drvdata(&mdev->ofdev.dev, rm);
405 /* Check resources availability. We need at least resource 0 and 1 */
406#if 0 /* Use that when i2s-a is finally an mdev per-se */
407 if (macio_resource_count(mdev) < 2 || macio_irq_count(mdev) < 2) {
408 printk(KERN_ERR
409 "rackmeter: found match but lacks resources: %s"
410 " (%d resources, %d interrupts)\n",
411 mdev->ofdev.node->full_name);
412 rc = -ENXIO;
413 goto bail_free;
414 }
415 if (macio_request_resources(mdev, "rackmeter")) {
416 printk(KERN_ERR
417 "rackmeter: failed to request resources: %s\n",
418 mdev->ofdev.node->full_name);
419 rc = -EBUSY;
420 goto bail_free;
421 }
422 rm->irq = macio_irq(mdev, 1);
423#else
424 rm->irq = irq_of_parse_and_map(i2s, 1);
425 if (rm->irq == NO_IRQ ||
426 of_address_to_resource(i2s, 0, &ri2s) ||
427 of_address_to_resource(i2s, 1, &rdma)) {
428 printk(KERN_ERR
429 "rackmeter: found match but lacks resources: %s",
430 mdev->ofdev.node->full_name);
431 rc = -ENXIO;
432 goto bail_free;
433 }
434#endif
435
436 pr_debug(" i2s @0x%08x\n", (unsigned int)ri2s.start);
437 pr_debug(" dma @0x%08x\n", (unsigned int)rdma.start);
438 pr_debug(" irq %d\n", rm->irq);
439
440 rm->ubuf = (u8 *)__get_free_page(GFP_KERNEL);
441 if (rm->ubuf == NULL) {
442 printk(KERN_ERR
443 "rackmeter: failed to allocate samples page !\n");
444 rc = -ENOMEM;
445 goto bail_release;
446 }
447
448 rm->dma_buf_v = dma_alloc_coherent(&macio_get_pci_dev(mdev)->dev,
449 sizeof(struct rackmeter_dma),
450 &rm->dma_buf_p, GFP_KERNEL);
451 if (rm->dma_buf_v == NULL) {
452 printk(KERN_ERR
453 "rackmeter: failed to allocate dma buffer !\n");
454 rc = -ENOMEM;
455 goto bail_free_samples;
456 }
457#if 0
458 rm->i2s_regs = ioremap(macio_resource_start(mdev, 0), 0x1000);
459#else
460 rm->i2s_regs = ioremap(ri2s.start, 0x1000);
461#endif
462 if (rm->i2s_regs == NULL) {
463 printk(KERN_ERR
464 "rackmeter: failed to map i2s registers !\n");
465 rc = -ENXIO;
466 goto bail_free_dma;
467 }
468#if 0
469 rm->dma_regs = ioremap(macio_resource_start(mdev, 1), 0x100);
470#else
471 rm->dma_regs = ioremap(rdma.start, 0x100);
472#endif
473 if (rm->dma_regs == NULL) {
474 printk(KERN_ERR
475 "rackmeter: failed to map dma registers !\n");
476 rc = -ENXIO;
477 goto bail_unmap_i2s;
478 }
479
480 rc = rackmeter_setup(rm);
481 if (rc) {
482 printk(KERN_ERR
483 "rackmeter: failed to initialize !\n");
484 rc = -ENXIO;
485 goto bail_unmap_dma;
486 }
487
488 rc = request_irq(rm->irq, rackmeter_irq, 0, "rackmeter", rm);
489 if (rc != 0) {
490 printk(KERN_ERR
491 "rackmeter: failed to request interrupt !\n");
492 goto bail_stop_dma;
493 }
494 of_node_put(np);
495 return 0;
496
497 bail_stop_dma:
498 DBDMA_DO_RESET(rm->dma_regs);
499 bail_unmap_dma:
500 iounmap(rm->dma_regs);
501 bail_unmap_i2s:
502 iounmap(rm->i2s_regs);
503 bail_free_dma:
504 dma_free_coherent(&macio_get_pci_dev(mdev)->dev,
505 sizeof(struct rackmeter_dma),
506 rm->dma_buf_v, rm->dma_buf_p);
507 bail_free_samples:
508 free_page((unsigned long)rm->ubuf);
509 bail_release:
510#if 0
511 macio_release_resources(mdev);
512#endif
513 bail_free:
514 kfree(rm);
515 bail:
516 of_node_put(i2s);
517 of_node_put(np);
518 dev_set_drvdata(&mdev->ofdev.dev, NULL);
519 return rc;
520}
521
522static int __devexit rackmeter_remove(struct macio_dev* mdev)
523{
524 struct rackmeter *rm = dev_get_drvdata(&mdev->ofdev.dev);
525
526 /* Stop CPU sniffer timer & work queues */
527 rackmeter_stop_cpu_sniffer(rm);
528
529 /* Clear reference to private data */
530 dev_set_drvdata(&mdev->ofdev.dev, NULL);
531
532 /* Stop/reset dbdma */
533 DBDMA_DO_RESET(rm->dma_regs);
534
535 /* Release the IRQ */
536 free_irq(rm->irq, rm);
537
538 /* Unmap registers */
539 iounmap(rm->dma_regs);
540 iounmap(rm->i2s_regs);
541
542 /* Free DMA */
543 dma_free_coherent(&macio_get_pci_dev(mdev)->dev,
544 sizeof(struct rackmeter_dma),
545 rm->dma_buf_v, rm->dma_buf_p);
546
547 /* Free samples */
548 free_page((unsigned long)rm->ubuf);
549
550#if 0
551 /* Release resources */
552 macio_release_resources(mdev);
553#endif
554
555 /* Get rid of me */
556 kfree(rm);
557
558 return 0;
559}
560
561static int rackmeter_shutdown(struct macio_dev* mdev)
562{
563 struct rackmeter *rm = dev_get_drvdata(&mdev->ofdev.dev);
564
565 if (rm == NULL)
566 return -ENODEV;
567
568 /* Stop CPU sniffer timer & work queues */
569 rackmeter_stop_cpu_sniffer(rm);
570
571 /* Stop/reset dbdma */
572 DBDMA_DO_RESET(rm->dma_regs);
573
574 return 0;
575}
576
577static struct of_device_id rackmeter_match[] = {
578 { .name = "i2s" },
579 { }
580};
581
582static struct macio_driver rackmeter_drv = {
583 .name = "rackmeter",
584 .owner = THIS_MODULE,
585 .match_table = rackmeter_match,
586 .probe = rackmeter_probe,
587 .remove = rackmeter_remove,
588 .shutdown = rackmeter_shutdown,
589};
590
591
592static int __init rackmeter_init(void)
593{
594 pr_debug("rackmeter_init()\n");
595
596 return macio_register_driver(&rackmeter_drv);
597}
598
599static void __exit rackmeter_exit(void)
600{
601 pr_debug("rackmeter_exit()\n");
602
603 macio_unregister_driver(&rackmeter_drv);
604}
605
606module_init(rackmeter_init);
607module_exit(rackmeter_exit);
608
609
610MODULE_LICENSE("GPL");
611MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
612MODULE_DESCRIPTION("RackMeter: Support vu-meter on XServe front panel");
This page took 0.072047 seconds and 5 git commands to generate.