]> bbs.cooldavid.org Git - net-next-2.6.git/blame - arch/powerpc/platforms/52xx/mpc52xx_gpt.c
of: Always use 'struct device.of_node' to get device node pointer.
[net-next-2.6.git] / arch / powerpc / platforms / 52xx / mpc52xx_gpt.c
CommitLineData
5496eab2
GL
1/*
2 * MPC5200 General Purpose Timer device driver
3 *
4 * Copyright (c) 2009 Secret Lab Technologies Ltd.
5 * Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
12 * This file is a driver for the the General Purpose Timer (gpt) devices
13 * found on the MPC5200 SoC. Each timer has an IO pin which can be used
14 * for GPIO or can be used to raise interrupts. The timer function can
15 * be used independently from the IO pin, or it can be used to control
16 * output signals or measure input signals.
17 *
18 * This driver supports the GPIO and IRQ controller functions of the GPT
eda43d16
AD
19 * device. Timer functions are not yet supported.
20 *
21 * The timer gpt0 can be used as watchdog (wdt). If the wdt mode is used,
22 * this prevents the use of any gpt0 gpt function (i.e. they will fail with
23 * -EBUSY). Thus, the safety wdt function always has precedence over the gpt
24 * function. If the kernel has been compiled with CONFIG_WATCHDOG_NOWAYOUT,
25 * this means that gpt0 is locked in wdt mode until the next reboot - this
26 * may be a requirement in safety applications.
5496eab2
GL
27 *
28 * To use the GPIO function, the following two properties must be added
29 * to the device tree node for the gpt device (typically in the .dts file
30 * for the board):
31 * gpio-controller;
32 * #gpio-cells = < 2 >;
33 * This driver will register the GPIO pin if it finds the gpio-controller
34 * property in the device tree.
35 *
36 * To use the IRQ controller function, the following two properties must
37 * be added to the device tree node for the gpt device:
38 * interrupt-controller;
39 * #interrupt-cells = < 1 >;
40 * The IRQ controller binding only uses one cell to specify the interrupt,
41 * and the IRQ flags are encoded in the cell. A cell is not used to encode
42 * the IRQ number because the GPT only has a single IRQ source. For flags,
43 * a value of '1' means rising edge sensitive and '2' means falling edge.
44 *
45 * The GPIO and the IRQ controller functions can be used at the same time,
46 * but in this use case the IO line will only work as an input. Trying to
47 * use it as a GPIO output will not work.
48 *
49 * When using the GPIO line as an output, it can either be driven as normal
50 * IO, or it can be an Open Collector (OC) output. At the moment it is the
51 * responsibility of either the bootloader or the platform setup code to set
52 * the output mode. This driver does not change the output mode setting.
53 */
54
4f59ecfa 55#include <linux/device.h>
5496eab2
GL
56#include <linux/irq.h>
57#include <linux/interrupt.h>
58#include <linux/io.h>
4f59ecfa
GL
59#include <linux/list.h>
60#include <linux/mutex.h>
5496eab2
GL
61#include <linux/of.h>
62#include <linux/of_platform.h>
63#include <linux/of_gpio.h>
64#include <linux/kernel.h>
5a0e3ad6 65#include <linux/slab.h>
eda43d16
AD
66#include <linux/watchdog.h>
67#include <linux/miscdevice.h>
68#include <linux/uaccess.h>
4f59ecfa 69#include <asm/div64.h>
5496eab2
GL
70#include <asm/mpc52xx.h>
71
72MODULE_DESCRIPTION("Freescale MPC52xx gpt driver");
eda43d16 73MODULE_AUTHOR("Sascha Hauer, Grant Likely, Albrecht Dreß");
5496eab2
GL
74MODULE_LICENSE("GPL");
75
76/**
77 * struct mpc52xx_gpt - Private data structure for MPC52xx GPT driver
78 * @dev: pointer to device structure
79 * @regs: virtual address of GPT registers
80 * @lock: spinlock to coordinate between different functions.
81 * @of_gc: of_gpio_chip instance structure; used when GPIO is enabled
82 * @irqhost: Pointer to irq_host instance; used when IRQ mode is supported
eda43d16
AD
83 * @wdt_mode: only relevant for gpt0: bit 0 (MPC52xx_GPT_CAN_WDT) indicates
84 * if the gpt may be used as wdt, bit 1 (MPC52xx_GPT_IS_WDT) indicates
85 * if the timer is actively used as wdt which blocks gpt functions
5496eab2
GL
86 */
87struct mpc52xx_gpt_priv {
4f59ecfa 88 struct list_head list; /* List of all GPT devices */
5496eab2
GL
89 struct device *dev;
90 struct mpc52xx_gpt __iomem *regs;
91 spinlock_t lock;
92 struct irq_host *irqhost;
4f59ecfa 93 u32 ipb_freq;
eda43d16 94 u8 wdt_mode;
5496eab2
GL
95
96#if defined(CONFIG_GPIOLIB)
97 struct of_gpio_chip of_gc;
98#endif
99};
100
4f59ecfa
GL
101LIST_HEAD(mpc52xx_gpt_list);
102DEFINE_MUTEX(mpc52xx_gpt_list_mutex);
103
5496eab2
GL
104#define MPC52xx_GPT_MODE_MS_MASK (0x07)
105#define MPC52xx_GPT_MODE_MS_IC (0x01)
106#define MPC52xx_GPT_MODE_MS_OC (0x02)
107#define MPC52xx_GPT_MODE_MS_PWM (0x03)
108#define MPC52xx_GPT_MODE_MS_GPIO (0x04)
109
110#define MPC52xx_GPT_MODE_GPIO_MASK (0x30)
111#define MPC52xx_GPT_MODE_GPIO_OUT_LOW (0x20)
112#define MPC52xx_GPT_MODE_GPIO_OUT_HIGH (0x30)
113
4f59ecfa
GL
114#define MPC52xx_GPT_MODE_COUNTER_ENABLE (0x1000)
115#define MPC52xx_GPT_MODE_CONTINUOUS (0x0400)
116#define MPC52xx_GPT_MODE_OPEN_DRAIN (0x0200)
5496eab2 117#define MPC52xx_GPT_MODE_IRQ_EN (0x0100)
eda43d16 118#define MPC52xx_GPT_MODE_WDT_EN (0x8000)
5496eab2
GL
119
120#define MPC52xx_GPT_MODE_ICT_MASK (0x030000)
121#define MPC52xx_GPT_MODE_ICT_RISING (0x010000)
122#define MPC52xx_GPT_MODE_ICT_FALLING (0x020000)
123#define MPC52xx_GPT_MODE_ICT_TOGGLE (0x030000)
124
eda43d16
AD
125#define MPC52xx_GPT_MODE_WDT_PING (0xa5)
126
5496eab2
GL
127#define MPC52xx_GPT_STATUS_IRQMASK (0x000f)
128
eda43d16
AD
129#define MPC52xx_GPT_CAN_WDT (1 << 0)
130#define MPC52xx_GPT_IS_WDT (1 << 1)
131
132
5496eab2
GL
133/* ---------------------------------------------------------------------
134 * Cascaded interrupt controller hooks
135 */
136
137static void mpc52xx_gpt_irq_unmask(unsigned int virq)
138{
139 struct mpc52xx_gpt_priv *gpt = get_irq_chip_data(virq);
140 unsigned long flags;
141
142 spin_lock_irqsave(&gpt->lock, flags);
143 setbits32(&gpt->regs->mode, MPC52xx_GPT_MODE_IRQ_EN);
144 spin_unlock_irqrestore(&gpt->lock, flags);
145}
146
147static void mpc52xx_gpt_irq_mask(unsigned int virq)
148{
149 struct mpc52xx_gpt_priv *gpt = get_irq_chip_data(virq);
150 unsigned long flags;
151
152 spin_lock_irqsave(&gpt->lock, flags);
153 clrbits32(&gpt->regs->mode, MPC52xx_GPT_MODE_IRQ_EN);
154 spin_unlock_irqrestore(&gpt->lock, flags);
155}
156
157static void mpc52xx_gpt_irq_ack(unsigned int virq)
158{
159 struct mpc52xx_gpt_priv *gpt = get_irq_chip_data(virq);
160
161 out_be32(&gpt->regs->status, MPC52xx_GPT_STATUS_IRQMASK);
162}
163
164static int mpc52xx_gpt_irq_set_type(unsigned int virq, unsigned int flow_type)
165{
166 struct mpc52xx_gpt_priv *gpt = get_irq_chip_data(virq);
167 unsigned long flags;
168 u32 reg;
169
170 dev_dbg(gpt->dev, "%s: virq=%i type=%x\n", __func__, virq, flow_type);
171
172 spin_lock_irqsave(&gpt->lock, flags);
173 reg = in_be32(&gpt->regs->mode) & ~MPC52xx_GPT_MODE_ICT_MASK;
174 if (flow_type & IRQF_TRIGGER_RISING)
175 reg |= MPC52xx_GPT_MODE_ICT_RISING;
176 if (flow_type & IRQF_TRIGGER_FALLING)
177 reg |= MPC52xx_GPT_MODE_ICT_FALLING;
178 out_be32(&gpt->regs->mode, reg);
179 spin_unlock_irqrestore(&gpt->lock, flags);
180
181 return 0;
182}
183
184static struct irq_chip mpc52xx_gpt_irq_chip = {
b27df672 185 .name = "MPC52xx GPT",
5496eab2
GL
186 .unmask = mpc52xx_gpt_irq_unmask,
187 .mask = mpc52xx_gpt_irq_mask,
188 .ack = mpc52xx_gpt_irq_ack,
189 .set_type = mpc52xx_gpt_irq_set_type,
190};
191
192void mpc52xx_gpt_irq_cascade(unsigned int virq, struct irq_desc *desc)
193{
194 struct mpc52xx_gpt_priv *gpt = get_irq_data(virq);
195 int sub_virq;
196 u32 status;
197
198 status = in_be32(&gpt->regs->status) & MPC52xx_GPT_STATUS_IRQMASK;
199 if (status) {
200 sub_virq = irq_linear_revmap(gpt->irqhost, 0);
201 generic_handle_irq(sub_virq);
202 }
203}
204
205static int mpc52xx_gpt_irq_map(struct irq_host *h, unsigned int virq,
206 irq_hw_number_t hw)
207{
208 struct mpc52xx_gpt_priv *gpt = h->host_data;
209
210 dev_dbg(gpt->dev, "%s: h=%p, virq=%i\n", __func__, h, virq);
211 set_irq_chip_data(virq, gpt);
212 set_irq_chip_and_handler(virq, &mpc52xx_gpt_irq_chip, handle_edge_irq);
213
214 return 0;
215}
216
217static int mpc52xx_gpt_irq_xlate(struct irq_host *h, struct device_node *ct,
40d50cf7 218 const u32 *intspec, unsigned int intsize,
5496eab2
GL
219 irq_hw_number_t *out_hwirq,
220 unsigned int *out_flags)
221{
222 struct mpc52xx_gpt_priv *gpt = h->host_data;
223
224 dev_dbg(gpt->dev, "%s: flags=%i\n", __func__, intspec[0]);
225
4f59ecfa 226 if ((intsize < 1) || (intspec[0] > 3)) {
5496eab2
GL
227 dev_err(gpt->dev, "bad irq specifier in %s\n", ct->full_name);
228 return -EINVAL;
229 }
230
231 *out_hwirq = 0; /* The GPT only has 1 IRQ line */
232 *out_flags = intspec[0];
233
234 return 0;
235}
236
237static struct irq_host_ops mpc52xx_gpt_irq_ops = {
238 .map = mpc52xx_gpt_irq_map,
239 .xlate = mpc52xx_gpt_irq_xlate,
240};
241
242static void
243mpc52xx_gpt_irq_setup(struct mpc52xx_gpt_priv *gpt, struct device_node *node)
244{
245 int cascade_virq;
246 unsigned long flags;
4f59ecfa 247 u32 mode;
5496eab2
GL
248
249 cascade_virq = irq_of_parse_and_map(node, 0);
4f59ecfa
GL
250 if (!cascade_virq)
251 return;
5496eab2
GL
252
253 gpt->irqhost = irq_alloc_host(node, IRQ_HOST_MAP_LINEAR, 1,
254 &mpc52xx_gpt_irq_ops, -1);
255 if (!gpt->irqhost) {
256 dev_err(gpt->dev, "irq_alloc_host() failed\n");
257 return;
258 }
259
260 gpt->irqhost->host_data = gpt;
5496eab2
GL
261 set_irq_data(cascade_virq, gpt);
262 set_irq_chained_handler(cascade_virq, mpc52xx_gpt_irq_cascade);
263
4f59ecfa
GL
264 /* If the GPT is currently disabled, then change it to be in Input
265 * Capture mode. If the mode is non-zero, then the pin could be
266 * already in use for something. */
5496eab2 267 spin_lock_irqsave(&gpt->lock, flags);
4f59ecfa
GL
268 mode = in_be32(&gpt->regs->mode);
269 if ((mode & MPC52xx_GPT_MODE_MS_MASK) == 0)
270 out_be32(&gpt->regs->mode, mode | MPC52xx_GPT_MODE_MS_IC);
5496eab2
GL
271 spin_unlock_irqrestore(&gpt->lock, flags);
272
273 dev_dbg(gpt->dev, "%s() complete. virq=%i\n", __func__, cascade_virq);
274}
275
276
277/* ---------------------------------------------------------------------
278 * GPIOLIB hooks
279 */
280#if defined(CONFIG_GPIOLIB)
281static inline struct mpc52xx_gpt_priv *gc_to_mpc52xx_gpt(struct gpio_chip *gc)
282{
283 return container_of(to_of_gpio_chip(gc), struct mpc52xx_gpt_priv,of_gc);
284}
285
286static int mpc52xx_gpt_gpio_get(struct gpio_chip *gc, unsigned int gpio)
287{
288 struct mpc52xx_gpt_priv *gpt = gc_to_mpc52xx_gpt(gc);
289
290 return (in_be32(&gpt->regs->status) >> 8) & 1;
291}
292
293static void
294mpc52xx_gpt_gpio_set(struct gpio_chip *gc, unsigned int gpio, int v)
295{
296 struct mpc52xx_gpt_priv *gpt = gc_to_mpc52xx_gpt(gc);
297 unsigned long flags;
298 u32 r;
299
300 dev_dbg(gpt->dev, "%s: gpio:%d v:%d\n", __func__, gpio, v);
301 r = v ? MPC52xx_GPT_MODE_GPIO_OUT_HIGH : MPC52xx_GPT_MODE_GPIO_OUT_LOW;
302
303 spin_lock_irqsave(&gpt->lock, flags);
304 clrsetbits_be32(&gpt->regs->mode, MPC52xx_GPT_MODE_GPIO_MASK, r);
305 spin_unlock_irqrestore(&gpt->lock, flags);
306}
307
308static int mpc52xx_gpt_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
309{
310 struct mpc52xx_gpt_priv *gpt = gc_to_mpc52xx_gpt(gc);
311 unsigned long flags;
312
313 dev_dbg(gpt->dev, "%s: gpio:%d\n", __func__, gpio);
314
315 spin_lock_irqsave(&gpt->lock, flags);
316 clrbits32(&gpt->regs->mode, MPC52xx_GPT_MODE_GPIO_MASK);
317 spin_unlock_irqrestore(&gpt->lock, flags);
318
319 return 0;
320}
321
322static int
323mpc52xx_gpt_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
324{
325 mpc52xx_gpt_gpio_set(gc, gpio, val);
326 return 0;
327}
328
329static void
330mpc52xx_gpt_gpio_setup(struct mpc52xx_gpt_priv *gpt, struct device_node *node)
331{
332 int rc;
333
334 /* Only setup GPIO if the device tree claims the GPT is
335 * a GPIO controller */
336 if (!of_find_property(node, "gpio-controller", NULL))
337 return;
338
339 gpt->of_gc.gc.label = kstrdup(node->full_name, GFP_KERNEL);
340 if (!gpt->of_gc.gc.label) {
341 dev_err(gpt->dev, "out of memory\n");
342 return;
343 }
344
345 gpt->of_gc.gpio_cells = 2;
346 gpt->of_gc.gc.ngpio = 1;
347 gpt->of_gc.gc.direction_input = mpc52xx_gpt_gpio_dir_in;
348 gpt->of_gc.gc.direction_output = mpc52xx_gpt_gpio_dir_out;
349 gpt->of_gc.gc.get = mpc52xx_gpt_gpio_get;
350 gpt->of_gc.gc.set = mpc52xx_gpt_gpio_set;
351 gpt->of_gc.gc.base = -1;
352 gpt->of_gc.xlate = of_gpio_simple_xlate;
353 node->data = &gpt->of_gc;
354 of_node_get(node);
355
356 /* Setup external pin in GPIO mode */
357 clrsetbits_be32(&gpt->regs->mode, MPC52xx_GPT_MODE_MS_MASK,
358 MPC52xx_GPT_MODE_MS_GPIO);
359
360 rc = gpiochip_add(&gpt->of_gc.gc);
361 if (rc)
362 dev_err(gpt->dev, "gpiochip_add() failed; rc=%i\n", rc);
363
364 dev_dbg(gpt->dev, "%s() complete.\n", __func__);
365}
366#else /* defined(CONFIG_GPIOLIB) */
367static void
368mpc52xx_gpt_gpio_setup(struct mpc52xx_gpt_priv *p, struct device_node *np) { }
369#endif /* defined(CONFIG_GPIOLIB) */
370
4f59ecfa
GL
371/***********************************************************************
372 * Timer API
373 */
374
375/**
376 * mpc52xx_gpt_from_irq - Return the GPT device associated with an IRQ number
377 * @irq: irq of timer.
378 */
379struct mpc52xx_gpt_priv *mpc52xx_gpt_from_irq(int irq)
380{
381 struct mpc52xx_gpt_priv *gpt;
382 struct list_head *pos;
383
384 /* Iterate over the list of timers looking for a matching device */
385 mutex_lock(&mpc52xx_gpt_list_mutex);
386 list_for_each(pos, &mpc52xx_gpt_list) {
387 gpt = container_of(pos, struct mpc52xx_gpt_priv, list);
388 if (gpt->irqhost && irq == irq_linear_revmap(gpt->irqhost, 0)) {
389 mutex_unlock(&mpc52xx_gpt_list_mutex);
390 return gpt;
391 }
392 }
393 mutex_unlock(&mpc52xx_gpt_list_mutex);
394
395 return NULL;
396}
397EXPORT_SYMBOL(mpc52xx_gpt_from_irq);
398
eda43d16
AD
399static int mpc52xx_gpt_do_start(struct mpc52xx_gpt_priv *gpt, u64 period,
400 int continuous, int as_wdt)
4f59ecfa
GL
401{
402 u32 clear, set;
403 u64 clocks;
404 u32 prescale;
405 unsigned long flags;
406
407 clear = MPC52xx_GPT_MODE_MS_MASK | MPC52xx_GPT_MODE_CONTINUOUS;
408 set = MPC52xx_GPT_MODE_MS_GPIO | MPC52xx_GPT_MODE_COUNTER_ENABLE;
eda43d16
AD
409 if (as_wdt) {
410 clear |= MPC52xx_GPT_MODE_IRQ_EN;
411 set |= MPC52xx_GPT_MODE_WDT_EN;
412 } else if (continuous)
4f59ecfa
GL
413 set |= MPC52xx_GPT_MODE_CONTINUOUS;
414
415 /* Determine the number of clocks in the requested period. 64 bit
416 * arithmatic is done here to preserve the precision until the value
417 * is scaled back down into the u32 range. Period is in 'ns', bus
418 * frequency is in Hz. */
690b846a 419 clocks = period * (u64)gpt->ipb_freq;
4f59ecfa
GL
420 do_div(clocks, 1000000000); /* Scale it down to ns range */
421
422 /* This device cannot handle a clock count greater than 32 bits */
423 if (clocks > 0xffffffff)
424 return -EINVAL;
425
426 /* Calculate the prescaler and count values from the clocks value.
427 * 'clocks' is the number of clock ticks in the period. The timer
428 * has 16 bit precision and a 16 bit prescaler. Prescaler is
429 * calculated by integer dividing the clocks by 0x10000 (shifting
430 * down 16 bits) to obtain the smallest possible divisor for clocks
431 * to get a 16 bit count value.
432 *
433 * Note: the prescale register is '1' based, not '0' based. ie. a
434 * value of '1' means divide the clock by one. 0xffff divides the
435 * clock by 0xffff. '0x0000' does not divide by zero, but wraps
436 * around and divides by 0x10000. That is why prescale must be
437 * a u32 variable, not a u16, for this calculation. */
438 prescale = (clocks >> 16) + 1;
439 do_div(clocks, prescale);
440 if (clocks > 0xffff) {
441 pr_err("calculation error; prescale:%x clocks:%llx\n",
442 prescale, clocks);
443 return -EINVAL;
444 }
445
eda43d16 446 /* Set and enable the timer, reject an attempt to use a wdt as gpt */
4f59ecfa 447 spin_lock_irqsave(&gpt->lock, flags);
eda43d16
AD
448 if (as_wdt)
449 gpt->wdt_mode |= MPC52xx_GPT_IS_WDT;
450 else if ((gpt->wdt_mode & MPC52xx_GPT_IS_WDT) != 0) {
451 spin_unlock_irqrestore(&gpt->lock, flags);
452 return -EBUSY;
453 }
4f59ecfa
GL
454 out_be32(&gpt->regs->count, prescale << 16 | clocks);
455 clrsetbits_be32(&gpt->regs->mode, clear, set);
456 spin_unlock_irqrestore(&gpt->lock, flags);
457
458 return 0;
459}
eda43d16
AD
460
461/**
462 * mpc52xx_gpt_start_timer - Set and enable the GPT timer
463 * @gpt: Pointer to gpt private data structure
464 * @period: period of timer in ns; max. ~130s @ 33MHz IPB clock
465 * @continuous: set to 1 to make timer continuous free running
466 *
467 * An interrupt will be generated every time the timer fires
468 */
469int mpc52xx_gpt_start_timer(struct mpc52xx_gpt_priv *gpt, u64 period,
470 int continuous)
471{
472 return mpc52xx_gpt_do_start(gpt, period, continuous, 0);
473}
4f59ecfa
GL
474EXPORT_SYMBOL(mpc52xx_gpt_start_timer);
475
eda43d16
AD
476/**
477 * mpc52xx_gpt_stop_timer - Stop a gpt
478 * @gpt: Pointer to gpt private data structure
479 *
480 * Returns an error if attempting to stop a wdt
481 */
482int mpc52xx_gpt_stop_timer(struct mpc52xx_gpt_priv *gpt)
4f59ecfa 483{
eda43d16
AD
484 unsigned long flags;
485
486 /* reject the operation if the timer is used as watchdog (gpt 0 only) */
487 spin_lock_irqsave(&gpt->lock, flags);
488 if ((gpt->wdt_mode & MPC52xx_GPT_IS_WDT) != 0) {
489 spin_unlock_irqrestore(&gpt->lock, flags);
490 return -EBUSY;
491 }
492
4f59ecfa 493 clrbits32(&gpt->regs->mode, MPC52xx_GPT_MODE_COUNTER_ENABLE);
eda43d16
AD
494 spin_unlock_irqrestore(&gpt->lock, flags);
495 return 0;
4f59ecfa
GL
496}
497EXPORT_SYMBOL(mpc52xx_gpt_stop_timer);
498
eda43d16
AD
499/**
500 * mpc52xx_gpt_timer_period - Read the timer period
501 * @gpt: Pointer to gpt private data structure
502 *
503 * Returns the timer period in ns
504 */
505u64 mpc52xx_gpt_timer_period(struct mpc52xx_gpt_priv *gpt)
506{
507 u64 period;
508 u64 prescale;
509 unsigned long flags;
510
511 spin_lock_irqsave(&gpt->lock, flags);
512 period = in_be32(&gpt->regs->count);
513 spin_unlock_irqrestore(&gpt->lock, flags);
514
515 prescale = period >> 16;
516 period &= 0xffff;
517 if (prescale == 0)
518 prescale = 0x10000;
519 period = period * prescale * 1000000000ULL;
520 do_div(period, (u64)gpt->ipb_freq);
521 return period;
522}
523EXPORT_SYMBOL(mpc52xx_gpt_timer_period);
524
525#if defined(CONFIG_MPC5200_WDT)
526/***********************************************************************
527 * Watchdog API for gpt0
528 */
529
530#define WDT_IDENTITY "mpc52xx watchdog on GPT0"
531
532/* wdt_is_active stores wether or not the /dev/watchdog device is opened */
533static unsigned long wdt_is_active;
534
535/* wdt-capable gpt */
536static struct mpc52xx_gpt_priv *mpc52xx_gpt_wdt;
537
538/* low-level wdt functions */
539static inline void mpc52xx_gpt_wdt_ping(struct mpc52xx_gpt_priv *gpt_wdt)
540{
541 unsigned long flags;
542
543 spin_lock_irqsave(&gpt_wdt->lock, flags);
544 out_8((u8 *) &gpt_wdt->regs->mode, MPC52xx_GPT_MODE_WDT_PING);
545 spin_unlock_irqrestore(&gpt_wdt->lock, flags);
546}
547
548/* wdt misc device api */
549static ssize_t mpc52xx_wdt_write(struct file *file, const char __user *data,
550 size_t len, loff_t *ppos)
551{
552 struct mpc52xx_gpt_priv *gpt_wdt = file->private_data;
553 mpc52xx_gpt_wdt_ping(gpt_wdt);
554 return 0;
555}
556
42747d71 557static const struct watchdog_info mpc5200_wdt_info = {
eda43d16
AD
558 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
559 .identity = WDT_IDENTITY,
560};
561
562static long mpc52xx_wdt_ioctl(struct file *file, unsigned int cmd,
563 unsigned long arg)
564{
565 struct mpc52xx_gpt_priv *gpt_wdt = file->private_data;
566 int __user *data = (int __user *)arg;
567 int timeout;
568 u64 real_timeout;
569 int ret = 0;
570
571 switch (cmd) {
572 case WDIOC_GETSUPPORT:
573 ret = copy_to_user(data, &mpc5200_wdt_info,
574 sizeof(mpc5200_wdt_info));
575 if (ret)
576 ret = -EFAULT;
577 break;
578
579 case WDIOC_GETSTATUS:
580 case WDIOC_GETBOOTSTATUS:
581 ret = put_user(0, data);
582 break;
583
584 case WDIOC_KEEPALIVE:
585 mpc52xx_gpt_wdt_ping(gpt_wdt);
586 break;
587
588 case WDIOC_SETTIMEOUT:
589 ret = get_user(timeout, data);
590 if (ret)
591 break;
592 real_timeout = (u64) timeout * 1000000000ULL;
593 ret = mpc52xx_gpt_do_start(gpt_wdt, real_timeout, 0, 1);
594 if (ret)
595 break;
596 /* fall through and return the timeout */
597
598 case WDIOC_GETTIMEOUT:
599 /* we need to round here as to avoid e.g. the following
600 * situation:
601 * - timeout requested is 1 second;
602 * - real timeout @33MHz is 999997090ns
603 * - the int divide by 10^9 will return 0.
604 */
605 real_timeout =
606 mpc52xx_gpt_timer_period(gpt_wdt) + 500000000ULL;
607 do_div(real_timeout, 1000000000ULL);
608 timeout = (int) real_timeout;
609 ret = put_user(timeout, data);
610 break;
611
612 default:
613 ret = -ENOTTY;
614 }
615 return ret;
616}
617
618static int mpc52xx_wdt_open(struct inode *inode, struct file *file)
619{
620 int ret;
621
622 /* sanity check */
623 if (!mpc52xx_gpt_wdt)
624 return -ENODEV;
625
626 /* /dev/watchdog can only be opened once */
627 if (test_and_set_bit(0, &wdt_is_active))
628 return -EBUSY;
629
630 /* Set and activate the watchdog with 30 seconds timeout */
631 ret = mpc52xx_gpt_do_start(mpc52xx_gpt_wdt, 30ULL * 1000000000ULL,
632 0, 1);
633 if (ret) {
634 clear_bit(0, &wdt_is_active);
635 return ret;
636 }
637
638 file->private_data = mpc52xx_gpt_wdt;
639 return nonseekable_open(inode, file);
640}
641
642static int mpc52xx_wdt_release(struct inode *inode, struct file *file)
643{
644 /* note: releasing the wdt in NOWAYOUT-mode does not stop it */
645#if !defined(CONFIG_WATCHDOG_NOWAYOUT)
646 struct mpc52xx_gpt_priv *gpt_wdt = file->private_data;
647 unsigned long flags;
648
649 spin_lock_irqsave(&gpt_wdt->lock, flags);
650 clrbits32(&gpt_wdt->regs->mode,
651 MPC52xx_GPT_MODE_COUNTER_ENABLE | MPC52xx_GPT_MODE_WDT_EN);
652 gpt_wdt->wdt_mode &= ~MPC52xx_GPT_IS_WDT;
653 spin_unlock_irqrestore(&gpt_wdt->lock, flags);
654#endif
655 clear_bit(0, &wdt_is_active);
656 return 0;
657}
658
659
660static const struct file_operations mpc52xx_wdt_fops = {
661 .owner = THIS_MODULE,
662 .llseek = no_llseek,
663 .write = mpc52xx_wdt_write,
664 .unlocked_ioctl = mpc52xx_wdt_ioctl,
665 .open = mpc52xx_wdt_open,
666 .release = mpc52xx_wdt_release,
667};
668
669static struct miscdevice mpc52xx_wdt_miscdev = {
670 .minor = WATCHDOG_MINOR,
671 .name = "watchdog",
672 .fops = &mpc52xx_wdt_fops,
673};
674
675static int __devinit mpc52xx_gpt_wdt_init(void)
676{
677 int err;
678
679 /* try to register the watchdog misc device */
680 err = misc_register(&mpc52xx_wdt_miscdev);
681 if (err)
682 pr_err("%s: cannot register watchdog device\n", WDT_IDENTITY);
683 else
684 pr_info("%s: watchdog device registered\n", WDT_IDENTITY);
685 return err;
686}
687
688static int mpc52xx_gpt_wdt_setup(struct mpc52xx_gpt_priv *gpt,
689 const u32 *period)
690{
691 u64 real_timeout;
692
693 /* remember the gpt for the wdt operation */
694 mpc52xx_gpt_wdt = gpt;
695
696 /* configure the wdt if the device tree contained a timeout */
697 if (!period || *period == 0)
698 return 0;
699
700 real_timeout = (u64) *period * 1000000000ULL;
701 if (mpc52xx_gpt_do_start(gpt, real_timeout, 0, 1))
702 dev_warn(gpt->dev, "starting as wdt failed\n");
703 else
704 dev_info(gpt->dev, "watchdog set to %us timeout\n", *period);
705 return 0;
706}
707
708#else
709
710static int __devinit mpc52xx_gpt_wdt_init(void)
711{
712 return 0;
713}
714
9205124c
GL
715static inline int mpc52xx_gpt_wdt_setup(struct mpc52xx_gpt_priv *gpt,
716 const u32 *period)
717{
718 return 0;
719}
eda43d16
AD
720
721#endif /* CONFIG_MPC5200_WDT */
722
5496eab2
GL
723/* ---------------------------------------------------------------------
724 * of_platform bus binding code
725 */
726static int __devinit mpc52xx_gpt_probe(struct of_device *ofdev,
727 const struct of_device_id *match)
728{
729 struct mpc52xx_gpt_priv *gpt;
730
731 gpt = kzalloc(sizeof *gpt, GFP_KERNEL);
732 if (!gpt)
733 return -ENOMEM;
734
735 spin_lock_init(&gpt->lock);
736 gpt->dev = &ofdev->dev;
61c7a080
GL
737 gpt->ipb_freq = mpc5xxx_get_bus_frequency(ofdev->dev.of_node);
738 gpt->regs = of_iomap(ofdev->dev.of_node, 0);
5496eab2
GL
739 if (!gpt->regs) {
740 kfree(gpt);
741 return -ENOMEM;
742 }
743
744 dev_set_drvdata(&ofdev->dev, gpt);
745
61c7a080
GL
746 mpc52xx_gpt_gpio_setup(gpt, ofdev->dev.of_node);
747 mpc52xx_gpt_irq_setup(gpt, ofdev->dev.of_node);
5496eab2 748
4f59ecfa
GL
749 mutex_lock(&mpc52xx_gpt_list_mutex);
750 list_add(&gpt->list, &mpc52xx_gpt_list);
751 mutex_unlock(&mpc52xx_gpt_list_mutex);
752
eda43d16 753 /* check if this device could be a watchdog */
61c7a080
GL
754 if (of_get_property(ofdev->dev.of_node, "fsl,has-wdt", NULL) ||
755 of_get_property(ofdev->dev.of_node, "has-wdt", NULL)) {
eda43d16
AD
756 const u32 *on_boot_wdt;
757
758 gpt->wdt_mode = MPC52xx_GPT_CAN_WDT;
61c7a080
GL
759 on_boot_wdt = of_get_property(ofdev->dev.of_node,
760 "fsl,wdt-on-boot", NULL);
eda43d16
AD
761 if (on_boot_wdt) {
762 dev_info(gpt->dev, "used as watchdog\n");
763 gpt->wdt_mode |= MPC52xx_GPT_IS_WDT;
764 } else
765 dev_info(gpt->dev, "can function as watchdog\n");
766 mpc52xx_gpt_wdt_setup(gpt, on_boot_wdt);
767 }
768
5496eab2
GL
769 return 0;
770}
771
772static int mpc52xx_gpt_remove(struct of_device *ofdev)
773{
774 return -EBUSY;
775}
776
777static const struct of_device_id mpc52xx_gpt_match[] = {
778 { .compatible = "fsl,mpc5200-gpt", },
779
780 /* Depreciated compatible values; don't use for new dts files */
781 { .compatible = "fsl,mpc5200-gpt-gpio", },
782 { .compatible = "mpc5200-gpt", },
783 {}
784};
785
786static struct of_platform_driver mpc52xx_gpt_driver = {
787 .name = "mpc52xx-gpt",
788 .match_table = mpc52xx_gpt_match,
789 .probe = mpc52xx_gpt_probe,
790 .remove = mpc52xx_gpt_remove,
791};
792
793static int __init mpc52xx_gpt_init(void)
794{
795 if (of_register_platform_driver(&mpc52xx_gpt_driver))
796 pr_err("error registering MPC52xx GPT driver\n");
797
798 return 0;
799}
800
801/* Make sure GPIOs and IRQs get set up before anyone tries to use them */
802subsys_initcall(mpc52xx_gpt_init);
eda43d16 803device_initcall(mpc52xx_gpt_wdt_init);