Path: blob/master/arch/powerpc/platforms/52xx/mpc52xx_gpt.c
10818 views
/*1* MPC5200 General Purpose Timer device driver2*3* Copyright (c) 2009 Secret Lab Technologies Ltd.4* Copyright (c) 2008 Sascha Hauer <[email protected]>, Pengutronix5*6* This program is free software; you can redistribute it and/or modify it7* under the terms of the GNU General Public License as published by the8* Free Software Foundation; either version 2 of the License, or (at your9* option) any later version.10*11* This file is a driver for the the General Purpose Timer (gpt) devices12* found on the MPC5200 SoC. Each timer has an IO pin which can be used13* for GPIO or can be used to raise interrupts. The timer function can14* be used independently from the IO pin, or it can be used to control15* output signals or measure input signals.16*17* This driver supports the GPIO and IRQ controller functions of the GPT18* device. Timer functions are not yet supported.19*20* The timer gpt0 can be used as watchdog (wdt). If the wdt mode is used,21* this prevents the use of any gpt0 gpt function (i.e. they will fail with22* -EBUSY). Thus, the safety wdt function always has precedence over the gpt23* function. If the kernel has been compiled with CONFIG_WATCHDOG_NOWAYOUT,24* this means that gpt0 is locked in wdt mode until the next reboot - this25* may be a requirement in safety applications.26*27* To use the GPIO function, the following two properties must be added28* to the device tree node for the gpt device (typically in the .dts file29* for the board):30* gpio-controller;31* #gpio-cells = < 2 >;32* This driver will register the GPIO pin if it finds the gpio-controller33* property in the device tree.34*35* To use the IRQ controller function, the following two properties must36* be added to the device tree node for the gpt device:37* interrupt-controller;38* #interrupt-cells = < 1 >;39* The IRQ controller binding only uses one cell to specify the interrupt,40* and the IRQ flags are encoded in the cell. A cell is not used to encode41* the IRQ number because the GPT only has a single IRQ source. For flags,42* a value of '1' means rising edge sensitive and '2' means falling edge.43*44* The GPIO and the IRQ controller functions can be used at the same time,45* but in this use case the IO line will only work as an input. Trying to46* use it as a GPIO output will not work.47*48* When using the GPIO line as an output, it can either be driven as normal49* IO, or it can be an Open Collector (OC) output. At the moment it is the50* responsibility of either the bootloader or the platform setup code to set51* the output mode. This driver does not change the output mode setting.52*/5354#include <linux/device.h>55#include <linux/irq.h>56#include <linux/interrupt.h>57#include <linux/io.h>58#include <linux/list.h>59#include <linux/mutex.h>60#include <linux/of.h>61#include <linux/of_platform.h>62#include <linux/of_gpio.h>63#include <linux/kernel.h>64#include <linux/slab.h>65#include <linux/fs.h>66#include <linux/watchdog.h>67#include <linux/miscdevice.h>68#include <linux/uaccess.h>69#include <asm/div64.h>70#include <asm/mpc52xx.h>7172MODULE_DESCRIPTION("Freescale MPC52xx gpt driver");73MODULE_AUTHOR("Sascha Hauer, Grant Likely, Albrecht Dreß");74MODULE_LICENSE("GPL");7576/**77* struct mpc52xx_gpt - Private data structure for MPC52xx GPT driver78* @dev: pointer to device structure79* @regs: virtual address of GPT registers80* @lock: spinlock to coordinate between different functions.81* @gc: gpio_chip instance structure; used when GPIO is enabled82* @irqhost: Pointer to irq_host instance; used when IRQ mode is supported83* @wdt_mode: only relevant for gpt0: bit 0 (MPC52xx_GPT_CAN_WDT) indicates84* if the gpt may be used as wdt, bit 1 (MPC52xx_GPT_IS_WDT) indicates85* if the timer is actively used as wdt which blocks gpt functions86*/87struct mpc52xx_gpt_priv {88struct list_head list; /* List of all GPT devices */89struct device *dev;90struct mpc52xx_gpt __iomem *regs;91spinlock_t lock;92struct irq_host *irqhost;93u32 ipb_freq;94u8 wdt_mode;9596#if defined(CONFIG_GPIOLIB)97struct gpio_chip gc;98#endif99};100101LIST_HEAD(mpc52xx_gpt_list);102DEFINE_MUTEX(mpc52xx_gpt_list_mutex);103104#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)109110#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)113114#define MPC52xx_GPT_MODE_COUNTER_ENABLE (0x1000)115#define MPC52xx_GPT_MODE_CONTINUOUS (0x0400)116#define MPC52xx_GPT_MODE_OPEN_DRAIN (0x0200)117#define MPC52xx_GPT_MODE_IRQ_EN (0x0100)118#define MPC52xx_GPT_MODE_WDT_EN (0x8000)119120#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)124125#define MPC52xx_GPT_MODE_WDT_PING (0xa5)126127#define MPC52xx_GPT_STATUS_IRQMASK (0x000f)128129#define MPC52xx_GPT_CAN_WDT (1 << 0)130#define MPC52xx_GPT_IS_WDT (1 << 1)131132133/* ---------------------------------------------------------------------134* Cascaded interrupt controller hooks135*/136137static void mpc52xx_gpt_irq_unmask(struct irq_data *d)138{139struct mpc52xx_gpt_priv *gpt = irq_data_get_irq_chip_data(d);140unsigned long flags;141142spin_lock_irqsave(&gpt->lock, flags);143setbits32(&gpt->regs->mode, MPC52xx_GPT_MODE_IRQ_EN);144spin_unlock_irqrestore(&gpt->lock, flags);145}146147static void mpc52xx_gpt_irq_mask(struct irq_data *d)148{149struct mpc52xx_gpt_priv *gpt = irq_data_get_irq_chip_data(d);150unsigned long flags;151152spin_lock_irqsave(&gpt->lock, flags);153clrbits32(&gpt->regs->mode, MPC52xx_GPT_MODE_IRQ_EN);154spin_unlock_irqrestore(&gpt->lock, flags);155}156157static void mpc52xx_gpt_irq_ack(struct irq_data *d)158{159struct mpc52xx_gpt_priv *gpt = irq_data_get_irq_chip_data(d);160161out_be32(&gpt->regs->status, MPC52xx_GPT_STATUS_IRQMASK);162}163164static int mpc52xx_gpt_irq_set_type(struct irq_data *d, unsigned int flow_type)165{166struct mpc52xx_gpt_priv *gpt = irq_data_get_irq_chip_data(d);167unsigned long flags;168u32 reg;169170dev_dbg(gpt->dev, "%s: virq=%i type=%x\n", __func__, d->irq, flow_type);171172spin_lock_irqsave(&gpt->lock, flags);173reg = in_be32(&gpt->regs->mode) & ~MPC52xx_GPT_MODE_ICT_MASK;174if (flow_type & IRQF_TRIGGER_RISING)175reg |= MPC52xx_GPT_MODE_ICT_RISING;176if (flow_type & IRQF_TRIGGER_FALLING)177reg |= MPC52xx_GPT_MODE_ICT_FALLING;178out_be32(&gpt->regs->mode, reg);179spin_unlock_irqrestore(&gpt->lock, flags);180181return 0;182}183184static struct irq_chip mpc52xx_gpt_irq_chip = {185.name = "MPC52xx GPT",186.irq_unmask = mpc52xx_gpt_irq_unmask,187.irq_mask = mpc52xx_gpt_irq_mask,188.irq_ack = mpc52xx_gpt_irq_ack,189.irq_set_type = mpc52xx_gpt_irq_set_type,190};191192void mpc52xx_gpt_irq_cascade(unsigned int virq, struct irq_desc *desc)193{194struct mpc52xx_gpt_priv *gpt = irq_get_handler_data(virq);195int sub_virq;196u32 status;197198status = in_be32(&gpt->regs->status) & MPC52xx_GPT_STATUS_IRQMASK;199if (status) {200sub_virq = irq_linear_revmap(gpt->irqhost, 0);201generic_handle_irq(sub_virq);202}203}204205static int mpc52xx_gpt_irq_map(struct irq_host *h, unsigned int virq,206irq_hw_number_t hw)207{208struct mpc52xx_gpt_priv *gpt = h->host_data;209210dev_dbg(gpt->dev, "%s: h=%p, virq=%i\n", __func__, h, virq);211irq_set_chip_data(virq, gpt);212irq_set_chip_and_handler(virq, &mpc52xx_gpt_irq_chip, handle_edge_irq);213214return 0;215}216217static int mpc52xx_gpt_irq_xlate(struct irq_host *h, struct device_node *ct,218const u32 *intspec, unsigned int intsize,219irq_hw_number_t *out_hwirq,220unsigned int *out_flags)221{222struct mpc52xx_gpt_priv *gpt = h->host_data;223224dev_dbg(gpt->dev, "%s: flags=%i\n", __func__, intspec[0]);225226if ((intsize < 1) || (intspec[0] > 3)) {227dev_err(gpt->dev, "bad irq specifier in %s\n", ct->full_name);228return -EINVAL;229}230231*out_hwirq = 0; /* The GPT only has 1 IRQ line */232*out_flags = intspec[0];233234return 0;235}236237static struct irq_host_ops mpc52xx_gpt_irq_ops = {238.map = mpc52xx_gpt_irq_map,239.xlate = mpc52xx_gpt_irq_xlate,240};241242static void243mpc52xx_gpt_irq_setup(struct mpc52xx_gpt_priv *gpt, struct device_node *node)244{245int cascade_virq;246unsigned long flags;247u32 mode;248249cascade_virq = irq_of_parse_and_map(node, 0);250if (!cascade_virq)251return;252253gpt->irqhost = irq_alloc_host(node, IRQ_HOST_MAP_LINEAR, 1,254&mpc52xx_gpt_irq_ops, -1);255if (!gpt->irqhost) {256dev_err(gpt->dev, "irq_alloc_host() failed\n");257return;258}259260gpt->irqhost->host_data = gpt;261irq_set_handler_data(cascade_virq, gpt);262irq_set_chained_handler(cascade_virq, mpc52xx_gpt_irq_cascade);263264/* If the GPT is currently disabled, then change it to be in Input265* Capture mode. If the mode is non-zero, then the pin could be266* already in use for something. */267spin_lock_irqsave(&gpt->lock, flags);268mode = in_be32(&gpt->regs->mode);269if ((mode & MPC52xx_GPT_MODE_MS_MASK) == 0)270out_be32(&gpt->regs->mode, mode | MPC52xx_GPT_MODE_MS_IC);271spin_unlock_irqrestore(&gpt->lock, flags);272273dev_dbg(gpt->dev, "%s() complete. virq=%i\n", __func__, cascade_virq);274}275276277/* ---------------------------------------------------------------------278* GPIOLIB hooks279*/280#if defined(CONFIG_GPIOLIB)281static inline struct mpc52xx_gpt_priv *gc_to_mpc52xx_gpt(struct gpio_chip *gc)282{283return container_of(gc, struct mpc52xx_gpt_priv, gc);284}285286static int mpc52xx_gpt_gpio_get(struct gpio_chip *gc, unsigned int gpio)287{288struct mpc52xx_gpt_priv *gpt = gc_to_mpc52xx_gpt(gc);289290return (in_be32(&gpt->regs->status) >> 8) & 1;291}292293static void294mpc52xx_gpt_gpio_set(struct gpio_chip *gc, unsigned int gpio, int v)295{296struct mpc52xx_gpt_priv *gpt = gc_to_mpc52xx_gpt(gc);297unsigned long flags;298u32 r;299300dev_dbg(gpt->dev, "%s: gpio:%d v:%d\n", __func__, gpio, v);301r = v ? MPC52xx_GPT_MODE_GPIO_OUT_HIGH : MPC52xx_GPT_MODE_GPIO_OUT_LOW;302303spin_lock_irqsave(&gpt->lock, flags);304clrsetbits_be32(&gpt->regs->mode, MPC52xx_GPT_MODE_GPIO_MASK, r);305spin_unlock_irqrestore(&gpt->lock, flags);306}307308static int mpc52xx_gpt_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)309{310struct mpc52xx_gpt_priv *gpt = gc_to_mpc52xx_gpt(gc);311unsigned long flags;312313dev_dbg(gpt->dev, "%s: gpio:%d\n", __func__, gpio);314315spin_lock_irqsave(&gpt->lock, flags);316clrbits32(&gpt->regs->mode, MPC52xx_GPT_MODE_GPIO_MASK);317spin_unlock_irqrestore(&gpt->lock, flags);318319return 0;320}321322static int323mpc52xx_gpt_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)324{325mpc52xx_gpt_gpio_set(gc, gpio, val);326return 0;327}328329static void330mpc52xx_gpt_gpio_setup(struct mpc52xx_gpt_priv *gpt, struct device_node *node)331{332int rc;333334/* Only setup GPIO if the device tree claims the GPT is335* a GPIO controller */336if (!of_find_property(node, "gpio-controller", NULL))337return;338339gpt->gc.label = kstrdup(node->full_name, GFP_KERNEL);340if (!gpt->gc.label) {341dev_err(gpt->dev, "out of memory\n");342return;343}344345gpt->gc.ngpio = 1;346gpt->gc.direction_input = mpc52xx_gpt_gpio_dir_in;347gpt->gc.direction_output = mpc52xx_gpt_gpio_dir_out;348gpt->gc.get = mpc52xx_gpt_gpio_get;349gpt->gc.set = mpc52xx_gpt_gpio_set;350gpt->gc.base = -1;351gpt->gc.of_node = node;352353/* Setup external pin in GPIO mode */354clrsetbits_be32(&gpt->regs->mode, MPC52xx_GPT_MODE_MS_MASK,355MPC52xx_GPT_MODE_MS_GPIO);356357rc = gpiochip_add(&gpt->gc);358if (rc)359dev_err(gpt->dev, "gpiochip_add() failed; rc=%i\n", rc);360361dev_dbg(gpt->dev, "%s() complete.\n", __func__);362}363#else /* defined(CONFIG_GPIOLIB) */364static void365mpc52xx_gpt_gpio_setup(struct mpc52xx_gpt_priv *p, struct device_node *np) { }366#endif /* defined(CONFIG_GPIOLIB) */367368/***********************************************************************369* Timer API370*/371372/**373* mpc52xx_gpt_from_irq - Return the GPT device associated with an IRQ number374* @irq: irq of timer.375*/376struct mpc52xx_gpt_priv *mpc52xx_gpt_from_irq(int irq)377{378struct mpc52xx_gpt_priv *gpt;379struct list_head *pos;380381/* Iterate over the list of timers looking for a matching device */382mutex_lock(&mpc52xx_gpt_list_mutex);383list_for_each(pos, &mpc52xx_gpt_list) {384gpt = container_of(pos, struct mpc52xx_gpt_priv, list);385if (gpt->irqhost && irq == irq_linear_revmap(gpt->irqhost, 0)) {386mutex_unlock(&mpc52xx_gpt_list_mutex);387return gpt;388}389}390mutex_unlock(&mpc52xx_gpt_list_mutex);391392return NULL;393}394EXPORT_SYMBOL(mpc52xx_gpt_from_irq);395396static int mpc52xx_gpt_do_start(struct mpc52xx_gpt_priv *gpt, u64 period,397int continuous, int as_wdt)398{399u32 clear, set;400u64 clocks;401u32 prescale;402unsigned long flags;403404clear = MPC52xx_GPT_MODE_MS_MASK | MPC52xx_GPT_MODE_CONTINUOUS;405set = MPC52xx_GPT_MODE_MS_GPIO | MPC52xx_GPT_MODE_COUNTER_ENABLE;406if (as_wdt) {407clear |= MPC52xx_GPT_MODE_IRQ_EN;408set |= MPC52xx_GPT_MODE_WDT_EN;409} else if (continuous)410set |= MPC52xx_GPT_MODE_CONTINUOUS;411412/* Determine the number of clocks in the requested period. 64 bit413* arithmatic is done here to preserve the precision until the value414* is scaled back down into the u32 range. Period is in 'ns', bus415* frequency is in Hz. */416clocks = period * (u64)gpt->ipb_freq;417do_div(clocks, 1000000000); /* Scale it down to ns range */418419/* This device cannot handle a clock count greater than 32 bits */420if (clocks > 0xffffffff)421return -EINVAL;422423/* Calculate the prescaler and count values from the clocks value.424* 'clocks' is the number of clock ticks in the period. The timer425* has 16 bit precision and a 16 bit prescaler. Prescaler is426* calculated by integer dividing the clocks by 0x10000 (shifting427* down 16 bits) to obtain the smallest possible divisor for clocks428* to get a 16 bit count value.429*430* Note: the prescale register is '1' based, not '0' based. ie. a431* value of '1' means divide the clock by one. 0xffff divides the432* clock by 0xffff. '0x0000' does not divide by zero, but wraps433* around and divides by 0x10000. That is why prescale must be434* a u32 variable, not a u16, for this calculation. */435prescale = (clocks >> 16) + 1;436do_div(clocks, prescale);437if (clocks > 0xffff) {438pr_err("calculation error; prescale:%x clocks:%llx\n",439prescale, clocks);440return -EINVAL;441}442443/* Set and enable the timer, reject an attempt to use a wdt as gpt */444spin_lock_irqsave(&gpt->lock, flags);445if (as_wdt)446gpt->wdt_mode |= MPC52xx_GPT_IS_WDT;447else if ((gpt->wdt_mode & MPC52xx_GPT_IS_WDT) != 0) {448spin_unlock_irqrestore(&gpt->lock, flags);449return -EBUSY;450}451out_be32(&gpt->regs->count, prescale << 16 | clocks);452clrsetbits_be32(&gpt->regs->mode, clear, set);453spin_unlock_irqrestore(&gpt->lock, flags);454455return 0;456}457458/**459* mpc52xx_gpt_start_timer - Set and enable the GPT timer460* @gpt: Pointer to gpt private data structure461* @period: period of timer in ns; max. ~130s @ 33MHz IPB clock462* @continuous: set to 1 to make timer continuous free running463*464* An interrupt will be generated every time the timer fires465*/466int mpc52xx_gpt_start_timer(struct mpc52xx_gpt_priv *gpt, u64 period,467int continuous)468{469return mpc52xx_gpt_do_start(gpt, period, continuous, 0);470}471EXPORT_SYMBOL(mpc52xx_gpt_start_timer);472473/**474* mpc52xx_gpt_stop_timer - Stop a gpt475* @gpt: Pointer to gpt private data structure476*477* Returns an error if attempting to stop a wdt478*/479int mpc52xx_gpt_stop_timer(struct mpc52xx_gpt_priv *gpt)480{481unsigned long flags;482483/* reject the operation if the timer is used as watchdog (gpt 0 only) */484spin_lock_irqsave(&gpt->lock, flags);485if ((gpt->wdt_mode & MPC52xx_GPT_IS_WDT) != 0) {486spin_unlock_irqrestore(&gpt->lock, flags);487return -EBUSY;488}489490clrbits32(&gpt->regs->mode, MPC52xx_GPT_MODE_COUNTER_ENABLE);491spin_unlock_irqrestore(&gpt->lock, flags);492return 0;493}494EXPORT_SYMBOL(mpc52xx_gpt_stop_timer);495496/**497* mpc52xx_gpt_timer_period - Read the timer period498* @gpt: Pointer to gpt private data structure499*500* Returns the timer period in ns501*/502u64 mpc52xx_gpt_timer_period(struct mpc52xx_gpt_priv *gpt)503{504u64 period;505u64 prescale;506unsigned long flags;507508spin_lock_irqsave(&gpt->lock, flags);509period = in_be32(&gpt->regs->count);510spin_unlock_irqrestore(&gpt->lock, flags);511512prescale = period >> 16;513period &= 0xffff;514if (prescale == 0)515prescale = 0x10000;516period = period * prescale * 1000000000ULL;517do_div(period, (u64)gpt->ipb_freq);518return period;519}520EXPORT_SYMBOL(mpc52xx_gpt_timer_period);521522#if defined(CONFIG_MPC5200_WDT)523/***********************************************************************524* Watchdog API for gpt0525*/526527#define WDT_IDENTITY "mpc52xx watchdog on GPT0"528529/* wdt_is_active stores wether or not the /dev/watchdog device is opened */530static unsigned long wdt_is_active;531532/* wdt-capable gpt */533static struct mpc52xx_gpt_priv *mpc52xx_gpt_wdt;534535/* low-level wdt functions */536static inline void mpc52xx_gpt_wdt_ping(struct mpc52xx_gpt_priv *gpt_wdt)537{538unsigned long flags;539540spin_lock_irqsave(&gpt_wdt->lock, flags);541out_8((u8 *) &gpt_wdt->regs->mode, MPC52xx_GPT_MODE_WDT_PING);542spin_unlock_irqrestore(&gpt_wdt->lock, flags);543}544545/* wdt misc device api */546static ssize_t mpc52xx_wdt_write(struct file *file, const char __user *data,547size_t len, loff_t *ppos)548{549struct mpc52xx_gpt_priv *gpt_wdt = file->private_data;550mpc52xx_gpt_wdt_ping(gpt_wdt);551return 0;552}553554static const struct watchdog_info mpc5200_wdt_info = {555.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,556.identity = WDT_IDENTITY,557};558559static long mpc52xx_wdt_ioctl(struct file *file, unsigned int cmd,560unsigned long arg)561{562struct mpc52xx_gpt_priv *gpt_wdt = file->private_data;563int __user *data = (int __user *)arg;564int timeout;565u64 real_timeout;566int ret = 0;567568switch (cmd) {569case WDIOC_GETSUPPORT:570ret = copy_to_user(data, &mpc5200_wdt_info,571sizeof(mpc5200_wdt_info));572if (ret)573ret = -EFAULT;574break;575576case WDIOC_GETSTATUS:577case WDIOC_GETBOOTSTATUS:578ret = put_user(0, data);579break;580581case WDIOC_KEEPALIVE:582mpc52xx_gpt_wdt_ping(gpt_wdt);583break;584585case WDIOC_SETTIMEOUT:586ret = get_user(timeout, data);587if (ret)588break;589real_timeout = (u64) timeout * 1000000000ULL;590ret = mpc52xx_gpt_do_start(gpt_wdt, real_timeout, 0, 1);591if (ret)592break;593/* fall through and return the timeout */594595case WDIOC_GETTIMEOUT:596/* we need to round here as to avoid e.g. the following597* situation:598* - timeout requested is 1 second;599* - real timeout @33MHz is 999997090ns600* - the int divide by 10^9 will return 0.601*/602real_timeout =603mpc52xx_gpt_timer_period(gpt_wdt) + 500000000ULL;604do_div(real_timeout, 1000000000ULL);605timeout = (int) real_timeout;606ret = put_user(timeout, data);607break;608609default:610ret = -ENOTTY;611}612return ret;613}614615static int mpc52xx_wdt_open(struct inode *inode, struct file *file)616{617int ret;618619/* sanity check */620if (!mpc52xx_gpt_wdt)621return -ENODEV;622623/* /dev/watchdog can only be opened once */624if (test_and_set_bit(0, &wdt_is_active))625return -EBUSY;626627/* Set and activate the watchdog with 30 seconds timeout */628ret = mpc52xx_gpt_do_start(mpc52xx_gpt_wdt, 30ULL * 1000000000ULL,6290, 1);630if (ret) {631clear_bit(0, &wdt_is_active);632return ret;633}634635file->private_data = mpc52xx_gpt_wdt;636return nonseekable_open(inode, file);637}638639static int mpc52xx_wdt_release(struct inode *inode, struct file *file)640{641/* note: releasing the wdt in NOWAYOUT-mode does not stop it */642#if !defined(CONFIG_WATCHDOG_NOWAYOUT)643struct mpc52xx_gpt_priv *gpt_wdt = file->private_data;644unsigned long flags;645646spin_lock_irqsave(&gpt_wdt->lock, flags);647clrbits32(&gpt_wdt->regs->mode,648MPC52xx_GPT_MODE_COUNTER_ENABLE | MPC52xx_GPT_MODE_WDT_EN);649gpt_wdt->wdt_mode &= ~MPC52xx_GPT_IS_WDT;650spin_unlock_irqrestore(&gpt_wdt->lock, flags);651#endif652clear_bit(0, &wdt_is_active);653return 0;654}655656657static const struct file_operations mpc52xx_wdt_fops = {658.owner = THIS_MODULE,659.llseek = no_llseek,660.write = mpc52xx_wdt_write,661.unlocked_ioctl = mpc52xx_wdt_ioctl,662.open = mpc52xx_wdt_open,663.release = mpc52xx_wdt_release,664};665666static struct miscdevice mpc52xx_wdt_miscdev = {667.minor = WATCHDOG_MINOR,668.name = "watchdog",669.fops = &mpc52xx_wdt_fops,670};671672static int __devinit mpc52xx_gpt_wdt_init(void)673{674int err;675676/* try to register the watchdog misc device */677err = misc_register(&mpc52xx_wdt_miscdev);678if (err)679pr_err("%s: cannot register watchdog device\n", WDT_IDENTITY);680else681pr_info("%s: watchdog device registered\n", WDT_IDENTITY);682return err;683}684685static int mpc52xx_gpt_wdt_setup(struct mpc52xx_gpt_priv *gpt,686const u32 *period)687{688u64 real_timeout;689690/* remember the gpt for the wdt operation */691mpc52xx_gpt_wdt = gpt;692693/* configure the wdt if the device tree contained a timeout */694if (!period || *period == 0)695return 0;696697real_timeout = (u64) *period * 1000000000ULL;698if (mpc52xx_gpt_do_start(gpt, real_timeout, 0, 1))699dev_warn(gpt->dev, "starting as wdt failed\n");700else701dev_info(gpt->dev, "watchdog set to %us timeout\n", *period);702return 0;703}704705#else706707static int __devinit mpc52xx_gpt_wdt_init(void)708{709return 0;710}711712static inline int mpc52xx_gpt_wdt_setup(struct mpc52xx_gpt_priv *gpt,713const u32 *period)714{715return 0;716}717718#endif /* CONFIG_MPC5200_WDT */719720/* ---------------------------------------------------------------------721* of_platform bus binding code722*/723static int __devinit mpc52xx_gpt_probe(struct platform_device *ofdev)724{725struct mpc52xx_gpt_priv *gpt;726727gpt = kzalloc(sizeof *gpt, GFP_KERNEL);728if (!gpt)729return -ENOMEM;730731spin_lock_init(&gpt->lock);732gpt->dev = &ofdev->dev;733gpt->ipb_freq = mpc5xxx_get_bus_frequency(ofdev->dev.of_node);734gpt->regs = of_iomap(ofdev->dev.of_node, 0);735if (!gpt->regs) {736kfree(gpt);737return -ENOMEM;738}739740dev_set_drvdata(&ofdev->dev, gpt);741742mpc52xx_gpt_gpio_setup(gpt, ofdev->dev.of_node);743mpc52xx_gpt_irq_setup(gpt, ofdev->dev.of_node);744745mutex_lock(&mpc52xx_gpt_list_mutex);746list_add(&gpt->list, &mpc52xx_gpt_list);747mutex_unlock(&mpc52xx_gpt_list_mutex);748749/* check if this device could be a watchdog */750if (of_get_property(ofdev->dev.of_node, "fsl,has-wdt", NULL) ||751of_get_property(ofdev->dev.of_node, "has-wdt", NULL)) {752const u32 *on_boot_wdt;753754gpt->wdt_mode = MPC52xx_GPT_CAN_WDT;755on_boot_wdt = of_get_property(ofdev->dev.of_node,756"fsl,wdt-on-boot", NULL);757if (on_boot_wdt) {758dev_info(gpt->dev, "used as watchdog\n");759gpt->wdt_mode |= MPC52xx_GPT_IS_WDT;760} else761dev_info(gpt->dev, "can function as watchdog\n");762mpc52xx_gpt_wdt_setup(gpt, on_boot_wdt);763}764765return 0;766}767768static int mpc52xx_gpt_remove(struct platform_device *ofdev)769{770return -EBUSY;771}772773static const struct of_device_id mpc52xx_gpt_match[] = {774{ .compatible = "fsl,mpc5200-gpt", },775776/* Depreciated compatible values; don't use for new dts files */777{ .compatible = "fsl,mpc5200-gpt-gpio", },778{ .compatible = "mpc5200-gpt", },779{}780};781782static struct platform_driver mpc52xx_gpt_driver = {783.driver = {784.name = "mpc52xx-gpt",785.owner = THIS_MODULE,786.of_match_table = mpc52xx_gpt_match,787},788.probe = mpc52xx_gpt_probe,789.remove = mpc52xx_gpt_remove,790};791792static int __init mpc52xx_gpt_init(void)793{794return platform_driver_register(&mpc52xx_gpt_driver);795}796797/* Make sure GPIOs and IRQs get set up before anyone tries to use them */798subsys_initcall(mpc52xx_gpt_init);799device_initcall(mpc52xx_gpt_wdt_init);800801802