blob: 60311e8d6240d8bfa02f0a710e9eba4b8c9702e3 [file] [log] [blame]
Thomas Gleixner2c162f92019-06-03 07:45:02 +02001// SPDX-License-Identifier: GPL-2.0-only
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +11002/*
3 * RackMac vu-meter driver
4 *
5 * (c) Copyright 2006 Benjamin Herrenschmidt, IBM Corp.
6 * <benh@kernel.crashing.org>
7 *
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +11008 * Support the CPU-meter LEDs of the Xserve G5
9 *
10 * TODO: Implement PWM to do variable intensity and provide userland
11 * interface for fun. Also, the CPU-meter could be made nicer by being
12 * a bit less "immediate" but giving instead a more average load over
13 * time. Patches welcome :-)
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +110014 */
15#undef DEBUG
16
17#include <linux/types.h>
18#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +110020#include <linux/device.h>
21#include <linux/interrupt.h>
22#include <linux/module.h>
23#include <linux/pci.h>
24#include <linux/dma-mapping.h>
25#include <linux/kernel_stat.h>
Rob Herring5af50732013-09-17 14:28:33 -050026#include <linux/of_address.h>
27#include <linux/of_irq.h>
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +110028
29#include <asm/io.h>
30#include <asm/prom.h>
31#include <asm/machdep.h>
32#include <asm/pmac_feature.h>
33#include <asm/dbdma.h>
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +110034#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 {
David Howells6d5aefb2006-12-05 19:36:26 +000051 struct delayed_work sniffer;
52 struct rackmeter *rm;
Frederic Weisbecker564b7332017-01-31 04:09:20 +010053 u64 prev_wall;
54 u64 prev_idle;
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +110055 int zero;
56} ____cacheline_aligned;
57
58struct rackmeter {
59 struct macio_dev *mdev;
60 unsigned int irq;
61 struct device_node *i2s;
62 u8 *ubuf;
63 struct dbdma_regs __iomem *dma_regs;
64 void __iomem *i2s_regs;
65 dma_addr_t dma_buf_p;
66 struct rackmeter_dma *dma_buf_v;
67 int stale_irq;
68 struct rackmeter_cpu cpu[2];
69 int paused;
70 struct mutex sem;
71};
72
73/* To be set as a tunable */
74static int rackmeter_ignore_nice;
75
76/* This GPIO is whacked by the OS X driver when initializing */
77#define RACKMETER_MAGIC_GPIO 0x78
78
79/* This is copied from cpufreq_ondemand, maybe we should put it in
80 * a common header somewhere
81 */
Frederic Weisbecker564b7332017-01-31 04:09:20 +010082static inline u64 get_cpu_idle_time(unsigned int cpu)
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +110083{
Frederic Weisbecker8392853e2019-11-21 03:44:30 +010084 struct kernel_cpustat *kcpustat = &kcpustat_cpu(cpu);
Glauber Costa3292beb2011-11-28 14:45:17 -020085 u64 retval;
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +110086
Frederic Weisbecker8392853e2019-11-21 03:44:30 +010087 retval = kcpustat->cpustat[CPUTIME_IDLE] +
88 kcpustat->cpustat[CPUTIME_IOWAIT];
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +110089
90 if (rackmeter_ignore_nice)
Frederic Weisbecker8392853e2019-11-21 03:44:30 +010091 retval += kcpustat_field(kcpustat, CPUTIME_NICE, cpu);
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +110092
Frederic Weisbecker564b7332017-01-31 04:09:20 +010093 return retval;
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +110094}
95
96static void rackmeter_setup_i2s(struct rackmeter *rm)
97{
98 struct macio_chip *macio = rm->mdev->bus->chip;
99
100 /* First whack magic GPIO */
101 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, RACKMETER_MAGIC_GPIO, 5);
102
103
104 /* Call feature code to enable the sound channel and the proper
105 * clock sources
106 */
107 pmac_call_feature(PMAC_FTR_SOUND_CHIP_ENABLE, rm->i2s, 0, 1);
108
109 /* Power i2s and stop i2s clock. We whack MacIO FCRs directly for now.
110 * This is a bit racy, thus we should add new platform functions to
111 * handle that. snd-aoa needs that too
112 */
113 MACIO_BIS(KEYLARGO_FCR1, KL1_I2S0_ENABLE);
114 MACIO_BIC(KEYLARGO_FCR1, KL1_I2S0_CLK_ENABLE_BIT);
115 (void)MACIO_IN32(KEYLARGO_FCR1);
116 udelay(10);
117
118 /* Then setup i2s. For now, we use the same magic value that
119 * the OS X driver seems to use. We might want to play around
120 * with the clock divisors later
121 */
122 out_le32(rm->i2s_regs + 0x10, 0x01fa0000);
123 (void)in_le32(rm->i2s_regs + 0x10);
124 udelay(10);
125
126 /* Fully restart i2s*/
127 MACIO_BIS(KEYLARGO_FCR1, KL1_I2S0_CELL_ENABLE |
128 KL1_I2S0_CLK_ENABLE_BIT);
129 (void)MACIO_IN32(KEYLARGO_FCR1);
130 udelay(10);
131}
132
133static void rackmeter_set_default_pattern(struct rackmeter *rm)
134{
135 int i;
136
137 for (i = 0; i < 16; i++) {
138 if (i < 8)
139 rm->ubuf[i] = (i & 1) * 255;
140 else
141 rm->ubuf[i] = ((~i) & 1) * 255;
142 }
143}
144
145static void rackmeter_do_pause(struct rackmeter *rm, int pause)
146{
147 struct rackmeter_dma *rdma = rm->dma_buf_v;
148
149 pr_debug("rackmeter: %s\n", pause ? "paused" : "started");
150
151 rm->paused = pause;
152 if (pause) {
153 DBDMA_DO_STOP(rm->dma_regs);
154 return;
155 }
Aaro Koskinene2836552018-03-16 22:17:28 +0200156 memset(rdma->buf1, 0, sizeof(rdma->buf1));
157 memset(rdma->buf2, 0, sizeof(rdma->buf2));
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100158
159 rm->dma_buf_v->mark = 0;
160
161 mb();
162 out_le32(&rm->dma_regs->cmdptr_hi, 0);
163 out_le32(&rm->dma_regs->cmdptr, rm->dma_buf_p);
164 out_le32(&rm->dma_regs->control, (RUN << 16) | RUN);
165}
166
167static void rackmeter_setup_dbdma(struct rackmeter *rm)
168{
169 struct rackmeter_dma *db = rm->dma_buf_v;
170 struct dbdma_cmd *cmd = db->cmd;
171
172 /* Make sure dbdma is reset */
173 DBDMA_DO_RESET(rm->dma_regs);
174
Al Viro193d0732007-02-09 16:39:05 +0000175 pr_debug("rackmeter: mark offset=0x%zx\n",
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100176 offsetof(struct rackmeter_dma, mark));
Al Viro193d0732007-02-09 16:39:05 +0000177 pr_debug("rackmeter: buf1 offset=0x%zx\n",
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100178 offsetof(struct rackmeter_dma, buf1));
Al Viro193d0732007-02-09 16:39:05 +0000179 pr_debug("rackmeter: buf2 offset=0x%zx\n",
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100180 offsetof(struct rackmeter_dma, buf2));
181
182 /* Prepare 4 dbdma commands for the 2 buffers */
183 memset(cmd, 0, 4 * sizeof(struct dbdma_cmd));
David Gibsonf5718722015-02-03 16:36:21 +1100184 cmd->req_count = cpu_to_le16(4);
185 cmd->command = cpu_to_le16(STORE_WORD | INTR_ALWAYS | KEY_SYSTEM);
186 cmd->phy_addr = cpu_to_le32(rm->dma_buf_p +
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100187 offsetof(struct rackmeter_dma, mark));
David Gibsonf5718722015-02-03 16:36:21 +1100188 cmd->cmd_dep = cpu_to_le32(0x02000000);
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100189 cmd++;
190
David Gibsonf5718722015-02-03 16:36:21 +1100191 cmd->req_count = cpu_to_le16(SAMPLE_COUNT * 4);
192 cmd->command = cpu_to_le16(OUTPUT_MORE);
193 cmd->phy_addr = cpu_to_le32(rm->dma_buf_p +
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100194 offsetof(struct rackmeter_dma, buf1));
195 cmd++;
196
David Gibsonf5718722015-02-03 16:36:21 +1100197 cmd->req_count = cpu_to_le16(4);
198 cmd->command = cpu_to_le16(STORE_WORD | INTR_ALWAYS | KEY_SYSTEM);
199 cmd->phy_addr = cpu_to_le32(rm->dma_buf_p +
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100200 offsetof(struct rackmeter_dma, mark));
David Gibsonf5718722015-02-03 16:36:21 +1100201 cmd->cmd_dep = cpu_to_le32(0x01000000);
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100202 cmd++;
203
David Gibsonf5718722015-02-03 16:36:21 +1100204 cmd->req_count = cpu_to_le16(SAMPLE_COUNT * 4);
205 cmd->command = cpu_to_le16(OUTPUT_MORE | BR_ALWAYS);
206 cmd->phy_addr = cpu_to_le32(rm->dma_buf_p +
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100207 offsetof(struct rackmeter_dma, buf2));
David Gibsonf5718722015-02-03 16:36:21 +1100208 cmd->cmd_dep = cpu_to_le32(rm->dma_buf_p);
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100209
210 rackmeter_do_pause(rm, 0);
211}
212
David Howells6d5aefb2006-12-05 19:36:26 +0000213static void rackmeter_do_timer(struct work_struct *work)
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100214{
David Howells6d5aefb2006-12-05 19:36:26 +0000215 struct rackmeter_cpu *rcpu =
216 container_of(work, struct rackmeter_cpu, sniffer.work);
217 struct rackmeter *rm = rcpu->rm;
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100218 unsigned int cpu = smp_processor_id();
Frederic Weisbecker564b7332017-01-31 04:09:20 +0100219 u64 cur_nsecs, total_idle_nsecs;
220 u64 total_nsecs, idle_nsecs;
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100221 int i, offset, load, cumm, pause;
222
Frederic Weisbecker564b7332017-01-31 04:09:20 +0100223 cur_nsecs = jiffies64_to_nsecs(get_jiffies_64());
224 total_nsecs = cur_nsecs - rcpu->prev_wall;
225 rcpu->prev_wall = cur_nsecs;
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100226
Frederic Weisbecker564b7332017-01-31 04:09:20 +0100227 total_idle_nsecs = get_cpu_idle_time(cpu);
228 idle_nsecs = total_idle_nsecs - rcpu->prev_idle;
229 idle_nsecs = min(idle_nsecs, total_nsecs);
230 rcpu->prev_idle = total_idle_nsecs;
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100231
232 /* We do a very dumb calculation to update the LEDs for now,
233 * we'll do better once we have actual PWM implemented
234 */
Frederic Weisbecker564b7332017-01-31 04:09:20 +0100235 load = div64_u64(9 * (total_nsecs - idle_nsecs), total_nsecs);
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100236
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;
243 }
244 rcpu->zero = (cumm == 0);
245
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);
253 }
254 schedule_delayed_work_on(cpu, &rcpu->sniffer,
255 msecs_to_jiffies(CPU_SAMPLING_RATE));
256}
257
Greg Kroah-Hartman1da42fb2012-12-21 15:03:50 -0800258static void rackmeter_init_cpu_sniffer(struct rackmeter *rm)
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100259{
260 unsigned int cpu;
261
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
266 */
267
David Howells6d5aefb2006-12-05 19:36:26 +0000268 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);
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100272
273 for_each_online_cpu(cpu) {
274 struct rackmeter_cpu *rcpu;
275
276 if (cpu > 1)
277 continue;
Joe Perchesa419aef2009-08-18 11:18:35 -0700278 rcpu = &rm->cpu[cpu];
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100279 rcpu->prev_idle = get_cpu_idle_time(cpu);
Frederic Weisbecker564b7332017-01-31 04:09:20 +0100280 rcpu->prev_wall = jiffies64_to_nsecs(get_jiffies_64());
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100281 schedule_delayed_work_on(cpu, &rm->cpu[cpu].sniffer,
282 msecs_to_jiffies(CPU_SAMPLING_RATE));
283 }
284}
285
Grant Likelye1f4dea2011-02-28 09:59:31 +0000286static void rackmeter_stop_cpu_sniffer(struct rackmeter *rm)
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100287{
Tejun Heoafe2c512010-12-14 16:21:17 +0100288 cancel_delayed_work_sync(&rm->cpu[0].sniffer);
289 cancel_delayed_work_sync(&rm->cpu[1].sniffer);
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100290}
291
Greg Kroah-Hartman1da42fb2012-12-21 15:03:50 -0800292static int rackmeter_setup(struct rackmeter *rm)
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100293{
294 pr_debug("rackmeter: setting up i2s..\n");
295 rackmeter_setup_i2s(rm);
296
297 pr_debug("rackmeter: setting up default pattern..\n");
298 rackmeter_set_default_pattern(rm);
299
300 pr_debug("rackmeter: setting up dbdma..\n");
301 rackmeter_setup_dbdma(rm);
302
303 pr_debug("rackmeter: start CPU measurements..\n");
304 rackmeter_init_cpu_sniffer(rm);
305
306 printk(KERN_INFO "RackMeter initialized\n");
307
308 return 0;
309}
310
311/* XXX FIXME: No PWM yet, this is 0/1 */
312static u32 rackmeter_calc_sample(struct rackmeter *rm, unsigned int index)
313{
314 int led;
315 u32 sample = 0;
316
317 for (led = 0; led < 16; led++) {
318 sample >>= 1;
319 sample |= ((rm->ubuf[led] >= 0x80) << 15);
320 }
321 return (sample << 17) | (sample >> 15);
322}
323
324static irqreturn_t rackmeter_irq(int irq, void *arg)
325{
326 struct rackmeter *rm = arg;
327 struct rackmeter_dma *db = rm->dma_buf_v;
328 unsigned int mark, i;
329 u32 *buf;
330
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
334 */
335 (void)in_le32(&rm->dma_regs->status);
336
337 /* Make sure the CPU gets us in order */
338 rmb();
339
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);
350 }
351 return IRQ_HANDLED;
352 }
353
354 /* Next buffer we need to fill is mark value */
355 buf = mark == 1 ? db->buf1 : db->buf2;
356
357 /* Fill it now. This routine converts the 8 bits depth sample array
358 * into the PWM bitmap for each LED.
359 */
360 for (i = 0; i < SAMPLE_COUNT; i++)
361 buf[i] = rackmeter_calc_sample(rm, i);
362
363
364 return IRQ_HANDLED;
365}
366
Greg Kroah-Hartman1da42fb2012-12-21 15:03:50 -0800367static int rackmeter_probe(struct macio_dev* mdev,
368 const struct of_device_id *match)
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100369{
370 struct device_node *i2s = NULL, *np = NULL;
371 struct rackmeter *rm = NULL;
372 struct resource ri2s, rdma;
373 int rc = -ENODEV;
374
375 pr_debug("rackmeter_probe()\n");
376
377 /* Get i2s-a node */
Rob Herringf1e0add2018-12-05 13:50:28 -0600378 for_each_child_of_node(mdev->ofdev.dev.of_node, i2s)
379 if (of_node_name_eq(i2s, "i2s-a"))
380 break;
381
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100382 if (i2s == NULL) {
383 pr_debug(" i2s-a child not found\n");
384 goto bail;
385 }
386 /* Get lightshow or virtual sound */
Rob Herringf1e0add2018-12-05 13:50:28 -0600387 for_each_child_of_node(i2s, np) {
388 if (of_node_name_eq(np, "lightshow"))
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100389 break;
Rob Herringf1e0add2018-12-05 13:50:28 -0600390 if (of_node_name_eq(np, "sound") &&
Stephen Rothwell01b27262007-04-27 13:41:15 +1000391 of_get_property(np, "virtual", NULL) != NULL)
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100392 break;
393 }
394 if (np == NULL) {
395 pr_debug(" lightshow or sound+virtual child not found\n");
396 goto bail;
397 }
398
399 /* Create and initialize our instance data */
Markus Elfringa0828cf2017-01-19 17:15:30 +0100400 rm = kzalloc(sizeof(*rm), GFP_KERNEL);
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100401 if (rm == NULL) {
402 printk(KERN_ERR "rackmeter: failed to allocate memory !\n");
403 rc = -ENOMEM;
404 goto bail_release;
405 }
406 rm->mdev = mdev;
407 rm->i2s = i2s;
408 mutex_init(&rm->sem);
409 dev_set_drvdata(&mdev->ofdev.dev, rm);
410 /* Check resources availability. We need at least resource 0 and 1 */
411#if 0 /* Use that when i2s-a is finally an mdev per-se */
412 if (macio_resource_count(mdev) < 2 || macio_irq_count(mdev) < 2) {
413 printk(KERN_ERR
Rob Herringb6a945a2017-07-18 16:43:12 -0500414 "rackmeter: found match but lacks resources: %pOF"
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100415 " (%d resources, %d interrupts)\n",
Rob Herringb6a945a2017-07-18 16:43:12 -0500416 mdev->ofdev.dev.of_node);
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100417 rc = -ENXIO;
418 goto bail_free;
419 }
420 if (macio_request_resources(mdev, "rackmeter")) {
421 printk(KERN_ERR
Rob Herringb6a945a2017-07-18 16:43:12 -0500422 "rackmeter: failed to request resources: %pOF\n",
423 mdev->ofdev.dev.of_node);
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100424 rc = -EBUSY;
425 goto bail_free;
426 }
427 rm->irq = macio_irq(mdev, 1);
428#else
429 rm->irq = irq_of_parse_and_map(i2s, 1);
Michael Ellermanef24ba72016-09-06 21:53:24 +1000430 if (!rm->irq ||
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100431 of_address_to_resource(i2s, 0, &ri2s) ||
432 of_address_to_resource(i2s, 1, &rdma)) {
433 printk(KERN_ERR
Rob Herringb6a945a2017-07-18 16:43:12 -0500434 "rackmeter: found match but lacks resources: %pOF",
435 mdev->ofdev.dev.of_node);
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100436 rc = -ENXIO;
437 goto bail_free;
438 }
439#endif
440
441 pr_debug(" i2s @0x%08x\n", (unsigned int)ri2s.start);
442 pr_debug(" dma @0x%08x\n", (unsigned int)rdma.start);
443 pr_debug(" irq %d\n", rm->irq);
444
445 rm->ubuf = (u8 *)__get_free_page(GFP_KERNEL);
446 if (rm->ubuf == NULL) {
447 printk(KERN_ERR
448 "rackmeter: failed to allocate samples page !\n");
449 rc = -ENOMEM;
450 goto bail_release;
451 }
452
453 rm->dma_buf_v = dma_alloc_coherent(&macio_get_pci_dev(mdev)->dev,
454 sizeof(struct rackmeter_dma),
455 &rm->dma_buf_p, GFP_KERNEL);
456 if (rm->dma_buf_v == NULL) {
457 printk(KERN_ERR
458 "rackmeter: failed to allocate dma buffer !\n");
459 rc = -ENOMEM;
460 goto bail_free_samples;
461 }
462#if 0
463 rm->i2s_regs = ioremap(macio_resource_start(mdev, 0), 0x1000);
464#else
465 rm->i2s_regs = ioremap(ri2s.start, 0x1000);
466#endif
467 if (rm->i2s_regs == NULL) {
468 printk(KERN_ERR
469 "rackmeter: failed to map i2s registers !\n");
470 rc = -ENXIO;
471 goto bail_free_dma;
472 }
473#if 0
474 rm->dma_regs = ioremap(macio_resource_start(mdev, 1), 0x100);
475#else
476 rm->dma_regs = ioremap(rdma.start, 0x100);
477#endif
478 if (rm->dma_regs == NULL) {
479 printk(KERN_ERR
480 "rackmeter: failed to map dma registers !\n");
481 rc = -ENXIO;
482 goto bail_unmap_i2s;
483 }
484
485 rc = rackmeter_setup(rm);
486 if (rc) {
487 printk(KERN_ERR
488 "rackmeter: failed to initialize !\n");
489 rc = -ENXIO;
490 goto bail_unmap_dma;
491 }
492
493 rc = request_irq(rm->irq, rackmeter_irq, 0, "rackmeter", rm);
494 if (rc != 0) {
495 printk(KERN_ERR
496 "rackmeter: failed to request interrupt !\n");
497 goto bail_stop_dma;
498 }
499 of_node_put(np);
500 return 0;
501
502 bail_stop_dma:
503 DBDMA_DO_RESET(rm->dma_regs);
504 bail_unmap_dma:
505 iounmap(rm->dma_regs);
506 bail_unmap_i2s:
507 iounmap(rm->i2s_regs);
508 bail_free_dma:
509 dma_free_coherent(&macio_get_pci_dev(mdev)->dev,
510 sizeof(struct rackmeter_dma),
511 rm->dma_buf_v, rm->dma_buf_p);
512 bail_free_samples:
513 free_page((unsigned long)rm->ubuf);
514 bail_release:
515#if 0
516 macio_release_resources(mdev);
517#endif
518 bail_free:
519 kfree(rm);
520 bail:
521 of_node_put(i2s);
522 of_node_put(np);
523 dev_set_drvdata(&mdev->ofdev.dev, NULL);
524 return rc;
525}
526
Greg Kroah-Hartman1da42fb2012-12-21 15:03:50 -0800527static int rackmeter_remove(struct macio_dev* mdev)
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100528{
529 struct rackmeter *rm = dev_get_drvdata(&mdev->ofdev.dev);
530
531 /* Stop CPU sniffer timer & work queues */
532 rackmeter_stop_cpu_sniffer(rm);
533
534 /* Clear reference to private data */
535 dev_set_drvdata(&mdev->ofdev.dev, NULL);
536
537 /* Stop/reset dbdma */
538 DBDMA_DO_RESET(rm->dma_regs);
539
540 /* Release the IRQ */
541 free_irq(rm->irq, rm);
542
543 /* Unmap registers */
544 iounmap(rm->dma_regs);
545 iounmap(rm->i2s_regs);
546
547 /* Free DMA */
548 dma_free_coherent(&macio_get_pci_dev(mdev)->dev,
549 sizeof(struct rackmeter_dma),
550 rm->dma_buf_v, rm->dma_buf_p);
551
552 /* Free samples */
553 free_page((unsigned long)rm->ubuf);
554
555#if 0
556 /* Release resources */
557 macio_release_resources(mdev);
558#endif
559
560 /* Get rid of me */
561 kfree(rm);
562
563 return 0;
564}
565
566static int rackmeter_shutdown(struct macio_dev* mdev)
567{
568 struct rackmeter *rm = dev_get_drvdata(&mdev->ofdev.dev);
569
570 if (rm == NULL)
571 return -ENODEV;
572
573 /* Stop CPU sniffer timer & work queues */
574 rackmeter_stop_cpu_sniffer(rm);
575
576 /* Stop/reset dbdma */
577 DBDMA_DO_RESET(rm->dma_regs);
578
579 return 0;
580}
581
Arvind Yadav516fa8d2017-06-19 11:14:25 +0530582static const struct of_device_id rackmeter_match[] = {
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100583 { .name = "i2s" },
584 { }
585};
Luis de Bethencourt6735b2e2015-10-20 16:13:53 +0100586MODULE_DEVICE_TABLE(of, rackmeter_match);
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100587
Al Viro31421a62008-11-22 17:35:24 +0000588static struct macio_driver rackmeter_driver = {
Benjamin Herrenschmidtc2cdf6a2010-06-02 17:09:18 +1000589 .driver = {
590 .name = "rackmeter",
591 .owner = THIS_MODULE,
592 .of_match_table = rackmeter_match,
593 },
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100594 .probe = rackmeter_probe,
Greg Kroah-Hartman1da42fb2012-12-21 15:03:50 -0800595 .remove = rackmeter_remove,
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100596 .shutdown = rackmeter_shutdown,
597};
598
599
600static int __init rackmeter_init(void)
601{
602 pr_debug("rackmeter_init()\n");
603
Al Viro31421a62008-11-22 17:35:24 +0000604 return macio_register_driver(&rackmeter_driver);
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100605}
606
607static void __exit rackmeter_exit(void)
608{
609 pr_debug("rackmeter_exit()\n");
610
Al Viro31421a62008-11-22 17:35:24 +0000611 macio_unregister_driver(&rackmeter_driver);
Benjamin Herrenschmidt3e00a5a2006-11-16 14:03:33 +1100612}
613
614module_init(rackmeter_init);
615module_exit(rackmeter_exit);
616
617
618MODULE_LICENSE("GPL");
619MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
620MODULE_DESCRIPTION("RackMeter: Support vu-meter on XServe front panel");