Path: blob/master/arch/powerpc/platforms/52xx/mpc52xx_gpt.c
26481 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* MPC5200 General Purpose Timer device driver3*4* Copyright (c) 2009 Secret Lab Technologies Ltd.5* Copyright (c) 2008 Sascha Hauer <[email protected]>, Pengutronix6*7* This file is a driver for the General Purpose Timer (gpt) devices8* found on the MPC5200 SoC. Each timer has an IO pin which can be used9* for GPIO or can be used to raise interrupts. The timer function can10* be used independently from the IO pin, or it can be used to control11* output signals or measure input signals.12*13* This driver supports the GPIO and IRQ controller functions of the GPT14* device. Timer functions are not yet supported.15*16* The timer gpt0 can be used as watchdog (wdt). If the wdt mode is used,17* this prevents the use of any gpt0 gpt function (i.e. they will fail with18* -EBUSY). Thus, the safety wdt function always has precedence over the gpt19* function. If the kernel has been compiled with CONFIG_WATCHDOG_NOWAYOUT,20* this means that gpt0 is locked in wdt mode until the next reboot - this21* may be a requirement in safety applications.22*23* To use the GPIO function, the following two properties must be added24* to the device tree node for the gpt device (typically in the .dts file25* for the board):26* gpio-controller;27* #gpio-cells = < 2 >;28* This driver will register the GPIO pin if it finds the gpio-controller29* property in the device tree.30*31* To use the IRQ controller function, the following two properties must32* be added to the device tree node for the gpt device:33* interrupt-controller;34* #interrupt-cells = < 1 >;35* The IRQ controller binding only uses one cell to specify the interrupt,36* and the IRQ flags are encoded in the cell. A cell is not used to encode37* the IRQ number because the GPT only has a single IRQ source. For flags,38* a value of '1' means rising edge sensitive and '2' means falling edge.39*40* The GPIO and the IRQ controller functions can be used at the same time,41* but in this use case the IO line will only work as an input. Trying to42* use it as a GPIO output will not work.43*44* When using the GPIO line as an output, it can either be driven as normal45* IO, or it can be an Open Collector (OC) output. At the moment it is the46* responsibility of either the bootloader or the platform setup code to set47* the output mode. This driver does not change the output mode setting.48*/4950#include <linux/gpio/driver.h>51#include <linux/irq.h>52#include <linux/interrupt.h>53#include <linux/io.h>54#include <linux/list.h>55#include <linux/mutex.h>56#include <linux/of.h>57#include <linux/of_address.h>58#include <linux/of_irq.h>59#include <linux/platform_device.h>60#include <linux/kernel.h>61#include <linux/property.h>62#include <linux/slab.h>63#include <linux/fs.h>64#include <linux/watchdog.h>65#include <linux/miscdevice.h>66#include <linux/uaccess.h>67#include <linux/module.h>68#include <asm/div64.h>69#include <asm/mpc52xx.h>7071MODULE_DESCRIPTION("Freescale MPC52xx gpt driver");72MODULE_AUTHOR("Sascha Hauer, Grant Likely, Albrecht Dreß");73MODULE_LICENSE("GPL");7475/**76* struct mpc52xx_gpt - Private data structure for MPC52xx GPT driver77* @dev: pointer to device structure78* @regs: virtual address of GPT registers79* @lock: spinlock to coordinate between different functions.80* @gc: gpio_chip instance structure; used when GPIO is enabled81* @irqhost: Pointer to irq_domain instance; used when IRQ mode is supported82* @wdt_mode: only relevant for gpt0: bit 0 (MPC52xx_GPT_CAN_WDT) indicates83* if the gpt may be used as wdt, bit 1 (MPC52xx_GPT_IS_WDT) indicates84* if the timer is actively used as wdt which blocks gpt functions85*/86struct mpc52xx_gpt_priv {87struct list_head list; /* List of all GPT devices */88struct device *dev;89struct mpc52xx_gpt __iomem *regs;90raw_spinlock_t lock;91struct irq_domain *irqhost;92u32 ipb_freq;93u8 wdt_mode;9495#if defined(CONFIG_GPIOLIB)96struct gpio_chip gc;97#endif98};99100LIST_HEAD(mpc52xx_gpt_list);101DEFINE_MUTEX(mpc52xx_gpt_list_mutex);102103#define MPC52xx_GPT_MODE_MS_MASK (0x07)104#define MPC52xx_GPT_MODE_MS_IC (0x01)105#define MPC52xx_GPT_MODE_MS_OC (0x02)106#define MPC52xx_GPT_MODE_MS_PWM (0x03)107#define MPC52xx_GPT_MODE_MS_GPIO (0x04)108109#define MPC52xx_GPT_MODE_GPIO_MASK (0x30)110#define MPC52xx_GPT_MODE_GPIO_OUT_LOW (0x20)111#define MPC52xx_GPT_MODE_GPIO_OUT_HIGH (0x30)112113#define MPC52xx_GPT_MODE_COUNTER_ENABLE (0x1000)114#define MPC52xx_GPT_MODE_CONTINUOUS (0x0400)115#define MPC52xx_GPT_MODE_OPEN_DRAIN (0x0200)116#define MPC52xx_GPT_MODE_IRQ_EN (0x0100)117#define MPC52xx_GPT_MODE_WDT_EN (0x8000)118119#define MPC52xx_GPT_MODE_ICT_MASK (0x030000)120#define MPC52xx_GPT_MODE_ICT_RISING (0x010000)121#define MPC52xx_GPT_MODE_ICT_FALLING (0x020000)122#define MPC52xx_GPT_MODE_ICT_TOGGLE (0x030000)123124#define MPC52xx_GPT_MODE_WDT_PING (0xa5)125126#define MPC52xx_GPT_STATUS_IRQMASK (0x000f)127128#define MPC52xx_GPT_CAN_WDT (1 << 0)129#define MPC52xx_GPT_IS_WDT (1 << 1)130131132/* ---------------------------------------------------------------------133* Cascaded interrupt controller hooks134*/135136static void mpc52xx_gpt_irq_unmask(struct irq_data *d)137{138struct mpc52xx_gpt_priv *gpt = irq_data_get_irq_chip_data(d);139unsigned long flags;140141raw_spin_lock_irqsave(&gpt->lock, flags);142setbits32(&gpt->regs->mode, MPC52xx_GPT_MODE_IRQ_EN);143raw_spin_unlock_irqrestore(&gpt->lock, flags);144}145146static void mpc52xx_gpt_irq_mask(struct irq_data *d)147{148struct mpc52xx_gpt_priv *gpt = irq_data_get_irq_chip_data(d);149unsigned long flags;150151raw_spin_lock_irqsave(&gpt->lock, flags);152clrbits32(&gpt->regs->mode, MPC52xx_GPT_MODE_IRQ_EN);153raw_spin_unlock_irqrestore(&gpt->lock, flags);154}155156static void mpc52xx_gpt_irq_ack(struct irq_data *d)157{158struct mpc52xx_gpt_priv *gpt = irq_data_get_irq_chip_data(d);159160out_be32(&gpt->regs->status, MPC52xx_GPT_STATUS_IRQMASK);161}162163static int mpc52xx_gpt_irq_set_type(struct irq_data *d, unsigned int flow_type)164{165struct mpc52xx_gpt_priv *gpt = irq_data_get_irq_chip_data(d);166unsigned long flags;167u32 reg;168169dev_dbg(gpt->dev, "%s: virq=%i type=%x\n", __func__, d->irq, flow_type);170171raw_spin_lock_irqsave(&gpt->lock, flags);172reg = in_be32(&gpt->regs->mode) & ~MPC52xx_GPT_MODE_ICT_MASK;173if (flow_type & IRQF_TRIGGER_RISING)174reg |= MPC52xx_GPT_MODE_ICT_RISING;175if (flow_type & IRQF_TRIGGER_FALLING)176reg |= MPC52xx_GPT_MODE_ICT_FALLING;177out_be32(&gpt->regs->mode, reg);178raw_spin_unlock_irqrestore(&gpt->lock, flags);179180return 0;181}182183static struct irq_chip mpc52xx_gpt_irq_chip = {184.name = "MPC52xx GPT",185.irq_unmask = mpc52xx_gpt_irq_unmask,186.irq_mask = mpc52xx_gpt_irq_mask,187.irq_ack = mpc52xx_gpt_irq_ack,188.irq_set_type = mpc52xx_gpt_irq_set_type,189};190191static void mpc52xx_gpt_irq_cascade(struct irq_desc *desc)192{193struct mpc52xx_gpt_priv *gpt = irq_desc_get_handler_data(desc);194u32 status;195196status = in_be32(&gpt->regs->status) & MPC52xx_GPT_STATUS_IRQMASK;197if (status)198generic_handle_domain_irq(gpt->irqhost, 0);199}200201static int mpc52xx_gpt_irq_map(struct irq_domain *h, unsigned int virq,202irq_hw_number_t hw)203{204struct mpc52xx_gpt_priv *gpt = h->host_data;205206dev_dbg(gpt->dev, "%s: h=%p, virq=%i\n", __func__, h, virq);207irq_set_chip_data(virq, gpt);208irq_set_chip_and_handler(virq, &mpc52xx_gpt_irq_chip, handle_edge_irq);209210return 0;211}212213static int mpc52xx_gpt_irq_xlate(struct irq_domain *h, struct device_node *ct,214const u32 *intspec, unsigned int intsize,215irq_hw_number_t *out_hwirq,216unsigned int *out_flags)217{218struct mpc52xx_gpt_priv *gpt = h->host_data;219220dev_dbg(gpt->dev, "%s: flags=%i\n", __func__, intspec[0]);221222if ((intsize < 1) || (intspec[0] > 3)) {223dev_err(gpt->dev, "bad irq specifier in %pOF\n", ct);224return -EINVAL;225}226227*out_hwirq = 0; /* The GPT only has 1 IRQ line */228*out_flags = intspec[0];229230return 0;231}232233static const struct irq_domain_ops mpc52xx_gpt_irq_ops = {234.map = mpc52xx_gpt_irq_map,235.xlate = mpc52xx_gpt_irq_xlate,236};237238static void239mpc52xx_gpt_irq_setup(struct mpc52xx_gpt_priv *gpt, struct device_node *node)240{241int cascade_virq;242unsigned long flags;243u32 mode;244245cascade_virq = irq_of_parse_and_map(node, 0);246if (!cascade_virq)247return;248249gpt->irqhost = irq_domain_create_linear(of_fwnode_handle(node), 1, &mpc52xx_gpt_irq_ops, gpt);250if (!gpt->irqhost) {251dev_err(gpt->dev, "irq_domain_create_linear() failed\n");252return;253}254255irq_set_handler_data(cascade_virq, gpt);256irq_set_chained_handler(cascade_virq, mpc52xx_gpt_irq_cascade);257258/* If the GPT is currently disabled, then change it to be in Input259* Capture mode. If the mode is non-zero, then the pin could be260* already in use for something. */261raw_spin_lock_irqsave(&gpt->lock, flags);262mode = in_be32(&gpt->regs->mode);263if ((mode & MPC52xx_GPT_MODE_MS_MASK) == 0)264out_be32(&gpt->regs->mode, mode | MPC52xx_GPT_MODE_MS_IC);265raw_spin_unlock_irqrestore(&gpt->lock, flags);266267dev_dbg(gpt->dev, "%s() complete. virq=%i\n", __func__, cascade_virq);268}269270271/* ---------------------------------------------------------------------272* GPIOLIB hooks273*/274#if defined(CONFIG_GPIOLIB)275static int mpc52xx_gpt_gpio_get(struct gpio_chip *gc, unsigned int gpio)276{277struct mpc52xx_gpt_priv *gpt = gpiochip_get_data(gc);278279return (in_be32(&gpt->regs->status) >> 8) & 1;280}281282static int283mpc52xx_gpt_gpio_set(struct gpio_chip *gc, unsigned int gpio, int v)284{285struct mpc52xx_gpt_priv *gpt = gpiochip_get_data(gc);286unsigned long flags;287u32 r;288289dev_dbg(gpt->dev, "%s: gpio:%d v:%d\n", __func__, gpio, v);290r = v ? MPC52xx_GPT_MODE_GPIO_OUT_HIGH : MPC52xx_GPT_MODE_GPIO_OUT_LOW;291292raw_spin_lock_irqsave(&gpt->lock, flags);293clrsetbits_be32(&gpt->regs->mode, MPC52xx_GPT_MODE_GPIO_MASK, r);294raw_spin_unlock_irqrestore(&gpt->lock, flags);295296return 0;297}298299static int mpc52xx_gpt_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)300{301struct mpc52xx_gpt_priv *gpt = gpiochip_get_data(gc);302unsigned long flags;303304dev_dbg(gpt->dev, "%s: gpio:%d\n", __func__, gpio);305306raw_spin_lock_irqsave(&gpt->lock, flags);307clrbits32(&gpt->regs->mode, MPC52xx_GPT_MODE_GPIO_MASK);308raw_spin_unlock_irqrestore(&gpt->lock, flags);309310return 0;311}312313static int314mpc52xx_gpt_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)315{316mpc52xx_gpt_gpio_set(gc, gpio, val);317return 0;318}319320static void mpc52xx_gpt_gpio_setup(struct mpc52xx_gpt_priv *gpt)321{322int rc;323324/* Only setup GPIO if the device claims the GPT is a GPIO controller */325if (!device_property_present(gpt->dev, "gpio-controller"))326return;327328gpt->gc.label = kasprintf(GFP_KERNEL, "%pfw", dev_fwnode(gpt->dev));329if (!gpt->gc.label) {330dev_err(gpt->dev, "out of memory\n");331return;332}333334gpt->gc.ngpio = 1;335gpt->gc.direction_input = mpc52xx_gpt_gpio_dir_in;336gpt->gc.direction_output = mpc52xx_gpt_gpio_dir_out;337gpt->gc.get = mpc52xx_gpt_gpio_get;338gpt->gc.set = mpc52xx_gpt_gpio_set;339gpt->gc.base = -1;340gpt->gc.parent = gpt->dev;341342/* Setup external pin in GPIO mode */343clrsetbits_be32(&gpt->regs->mode, MPC52xx_GPT_MODE_MS_MASK,344MPC52xx_GPT_MODE_MS_GPIO);345346rc = gpiochip_add_data(&gpt->gc, gpt);347if (rc)348dev_err(gpt->dev, "gpiochip_add_data() failed; rc=%i\n", rc);349350dev_dbg(gpt->dev, "%s() complete.\n", __func__);351}352#else /* defined(CONFIG_GPIOLIB) */353static void mpc52xx_gpt_gpio_setup(struct mpc52xx_gpt_priv *gpt) { }354#endif /* defined(CONFIG_GPIOLIB) */355356/***********************************************************************357* Timer API358*/359360/**361* mpc52xx_gpt_from_irq - Return the GPT device associated with an IRQ number362* @irq: irq of timer.363*/364struct mpc52xx_gpt_priv *mpc52xx_gpt_from_irq(int irq)365{366struct mpc52xx_gpt_priv *gpt;367struct list_head *pos;368369/* Iterate over the list of timers looking for a matching device */370mutex_lock(&mpc52xx_gpt_list_mutex);371list_for_each(pos, &mpc52xx_gpt_list) {372gpt = container_of(pos, struct mpc52xx_gpt_priv, list);373if (gpt->irqhost && irq == irq_find_mapping(gpt->irqhost, 0)) {374mutex_unlock(&mpc52xx_gpt_list_mutex);375return gpt;376}377}378mutex_unlock(&mpc52xx_gpt_list_mutex);379380return NULL;381}382EXPORT_SYMBOL(mpc52xx_gpt_from_irq);383384static int mpc52xx_gpt_do_start(struct mpc52xx_gpt_priv *gpt, u64 period,385int continuous, int as_wdt)386{387u32 clear, set;388u64 clocks;389u32 prescale;390unsigned long flags;391392clear = MPC52xx_GPT_MODE_MS_MASK | MPC52xx_GPT_MODE_CONTINUOUS;393set = MPC52xx_GPT_MODE_MS_GPIO | MPC52xx_GPT_MODE_COUNTER_ENABLE;394if (as_wdt) {395clear |= MPC52xx_GPT_MODE_IRQ_EN;396set |= MPC52xx_GPT_MODE_WDT_EN;397} else if (continuous)398set |= MPC52xx_GPT_MODE_CONTINUOUS;399400/* Determine the number of clocks in the requested period. 64 bit401* arithmetic is done here to preserve the precision until the value402* is scaled back down into the u32 range. Period is in 'ns', bus403* frequency is in Hz. */404clocks = period * (u64)gpt->ipb_freq;405do_div(clocks, 1000000000); /* Scale it down to ns range */406407/* This device cannot handle a clock count greater than 32 bits */408if (clocks > 0xffffffff)409return -EINVAL;410411/* Calculate the prescaler and count values from the clocks value.412* 'clocks' is the number of clock ticks in the period. The timer413* has 16 bit precision and a 16 bit prescaler. Prescaler is414* calculated by integer dividing the clocks by 0x10000 (shifting415* down 16 bits) to obtain the smallest possible divisor for clocks416* to get a 16 bit count value.417*418* Note: the prescale register is '1' based, not '0' based. ie. a419* value of '1' means divide the clock by one. 0xffff divides the420* clock by 0xffff. '0x0000' does not divide by zero, but wraps421* around and divides by 0x10000. That is why prescale must be422* a u32 variable, not a u16, for this calculation. */423prescale = (clocks >> 16) + 1;424do_div(clocks, prescale);425if (clocks > 0xffff) {426pr_err("calculation error; prescale:%x clocks:%llx\n",427prescale, clocks);428return -EINVAL;429}430431/* Set and enable the timer, reject an attempt to use a wdt as gpt */432raw_spin_lock_irqsave(&gpt->lock, flags);433if (as_wdt)434gpt->wdt_mode |= MPC52xx_GPT_IS_WDT;435else if ((gpt->wdt_mode & MPC52xx_GPT_IS_WDT) != 0) {436raw_spin_unlock_irqrestore(&gpt->lock, flags);437return -EBUSY;438}439out_be32(&gpt->regs->count, prescale << 16 | clocks);440clrsetbits_be32(&gpt->regs->mode, clear, set);441raw_spin_unlock_irqrestore(&gpt->lock, flags);442443return 0;444}445446/**447* mpc52xx_gpt_start_timer - Set and enable the GPT timer448* @gpt: Pointer to gpt private data structure449* @period: period of timer in ns; max. ~130s @ 33MHz IPB clock450* @continuous: set to 1 to make timer continuous free running451*452* An interrupt will be generated every time the timer fires453*/454int mpc52xx_gpt_start_timer(struct mpc52xx_gpt_priv *gpt, u64 period,455int continuous)456{457return mpc52xx_gpt_do_start(gpt, period, continuous, 0);458}459EXPORT_SYMBOL(mpc52xx_gpt_start_timer);460461/**462* mpc52xx_gpt_stop_timer - Stop a gpt463* @gpt: Pointer to gpt private data structure464*465* Returns an error if attempting to stop a wdt466*/467int mpc52xx_gpt_stop_timer(struct mpc52xx_gpt_priv *gpt)468{469unsigned long flags;470471/* reject the operation if the timer is used as watchdog (gpt 0 only) */472raw_spin_lock_irqsave(&gpt->lock, flags);473if ((gpt->wdt_mode & MPC52xx_GPT_IS_WDT) != 0) {474raw_spin_unlock_irqrestore(&gpt->lock, flags);475return -EBUSY;476}477478clrbits32(&gpt->regs->mode, MPC52xx_GPT_MODE_COUNTER_ENABLE);479raw_spin_unlock_irqrestore(&gpt->lock, flags);480return 0;481}482EXPORT_SYMBOL(mpc52xx_gpt_stop_timer);483484/**485* mpc52xx_gpt_timer_period - Read the timer period486* @gpt: Pointer to gpt private data structure487*488* Returns the timer period in ns489*/490u64 mpc52xx_gpt_timer_period(struct mpc52xx_gpt_priv *gpt)491{492u64 period;493u64 prescale;494unsigned long flags;495496raw_spin_lock_irqsave(&gpt->lock, flags);497period = in_be32(&gpt->regs->count);498raw_spin_unlock_irqrestore(&gpt->lock, flags);499500prescale = period >> 16;501period &= 0xffff;502if (prescale == 0)503prescale = 0x10000;504period = period * prescale * 1000000000ULL;505do_div(period, gpt->ipb_freq);506return period;507}508EXPORT_SYMBOL(mpc52xx_gpt_timer_period);509510#if defined(CONFIG_MPC5200_WDT)511/***********************************************************************512* Watchdog API for gpt0513*/514515#define WDT_IDENTITY "mpc52xx watchdog on GPT0"516517/* wdt_is_active stores whether or not the /dev/watchdog device is opened */518static unsigned long wdt_is_active;519520/* wdt-capable gpt */521static struct mpc52xx_gpt_priv *mpc52xx_gpt_wdt;522523/* low-level wdt functions */524static inline void mpc52xx_gpt_wdt_ping(struct mpc52xx_gpt_priv *gpt_wdt)525{526unsigned long flags;527528raw_spin_lock_irqsave(&gpt_wdt->lock, flags);529out_8((u8 *) &gpt_wdt->regs->mode, MPC52xx_GPT_MODE_WDT_PING);530raw_spin_unlock_irqrestore(&gpt_wdt->lock, flags);531}532533/* wdt misc device api */534static ssize_t mpc52xx_wdt_write(struct file *file, const char __user *data,535size_t len, loff_t *ppos)536{537struct mpc52xx_gpt_priv *gpt_wdt = file->private_data;538mpc52xx_gpt_wdt_ping(gpt_wdt);539return 0;540}541542static const struct watchdog_info mpc5200_wdt_info = {543.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,544.identity = WDT_IDENTITY,545};546547static long mpc52xx_wdt_ioctl(struct file *file, unsigned int cmd,548unsigned long arg)549{550struct mpc52xx_gpt_priv *gpt_wdt = file->private_data;551int __user *data = (int __user *)arg;552int timeout;553u64 real_timeout;554int ret = 0;555556switch (cmd) {557case WDIOC_GETSUPPORT:558ret = copy_to_user(data, &mpc5200_wdt_info,559sizeof(mpc5200_wdt_info));560if (ret)561ret = -EFAULT;562break;563564case WDIOC_GETSTATUS:565case WDIOC_GETBOOTSTATUS:566ret = put_user(0, data);567break;568569case WDIOC_KEEPALIVE:570mpc52xx_gpt_wdt_ping(gpt_wdt);571break;572573case WDIOC_SETTIMEOUT:574ret = get_user(timeout, data);575if (ret)576break;577real_timeout = (u64) timeout * 1000000000ULL;578ret = mpc52xx_gpt_do_start(gpt_wdt, real_timeout, 0, 1);579if (ret)580break;581/* fall through and return the timeout */582fallthrough;583584case WDIOC_GETTIMEOUT:585/* we need to round here as to avoid e.g. the following586* situation:587* - timeout requested is 1 second;588* - real timeout @33MHz is 999997090ns589* - the int divide by 10^9 will return 0.590*/591real_timeout =592mpc52xx_gpt_timer_period(gpt_wdt) + 500000000ULL;593do_div(real_timeout, 1000000000ULL);594timeout = (int) real_timeout;595ret = put_user(timeout, data);596break;597598default:599ret = -ENOTTY;600}601return ret;602}603604static int mpc52xx_wdt_open(struct inode *inode, struct file *file)605{606int ret;607608/* sanity check */609if (!mpc52xx_gpt_wdt)610return -ENODEV;611612/* /dev/watchdog can only be opened once */613if (test_and_set_bit(0, &wdt_is_active))614return -EBUSY;615616/* Set and activate the watchdog with 30 seconds timeout */617ret = mpc52xx_gpt_do_start(mpc52xx_gpt_wdt, 30ULL * 1000000000ULL,6180, 1);619if (ret) {620clear_bit(0, &wdt_is_active);621return ret;622}623624file->private_data = mpc52xx_gpt_wdt;625return stream_open(inode, file);626}627628static int mpc52xx_wdt_release(struct inode *inode, struct file *file)629{630/* note: releasing the wdt in NOWAYOUT-mode does not stop it */631#if !defined(CONFIG_WATCHDOG_NOWAYOUT)632struct mpc52xx_gpt_priv *gpt_wdt = file->private_data;633unsigned long flags;634635raw_spin_lock_irqsave(&gpt_wdt->lock, flags);636clrbits32(&gpt_wdt->regs->mode,637MPC52xx_GPT_MODE_COUNTER_ENABLE | MPC52xx_GPT_MODE_WDT_EN);638gpt_wdt->wdt_mode &= ~MPC52xx_GPT_IS_WDT;639raw_spin_unlock_irqrestore(&gpt_wdt->lock, flags);640#endif641clear_bit(0, &wdt_is_active);642return 0;643}644645646static const struct file_operations mpc52xx_wdt_fops = {647.owner = THIS_MODULE,648.write = mpc52xx_wdt_write,649.unlocked_ioctl = mpc52xx_wdt_ioctl,650.compat_ioctl = compat_ptr_ioctl,651.open = mpc52xx_wdt_open,652.release = mpc52xx_wdt_release,653};654655static struct miscdevice mpc52xx_wdt_miscdev = {656.minor = WATCHDOG_MINOR,657.name = "watchdog",658.fops = &mpc52xx_wdt_fops,659};660661static int mpc52xx_gpt_wdt_init(void)662{663int err;664665/* try to register the watchdog misc device */666err = misc_register(&mpc52xx_wdt_miscdev);667if (err)668pr_err("%s: cannot register watchdog device\n", WDT_IDENTITY);669else670pr_info("%s: watchdog device registered\n", WDT_IDENTITY);671return err;672}673674static int mpc52xx_gpt_wdt_setup(struct mpc52xx_gpt_priv *gpt,675const u32 *period)676{677u64 real_timeout;678679/* remember the gpt for the wdt operation */680mpc52xx_gpt_wdt = gpt;681682/* configure the wdt if the device tree contained a timeout */683if (!period || *period == 0)684return 0;685686real_timeout = (u64) *period * 1000000000ULL;687if (mpc52xx_gpt_do_start(gpt, real_timeout, 0, 1))688dev_warn(gpt->dev, "starting as wdt failed\n");689else690dev_info(gpt->dev, "watchdog set to %us timeout\n", *period);691return 0;692}693694#else695696static int mpc52xx_gpt_wdt_init(void)697{698return 0;699}700701static inline int mpc52xx_gpt_wdt_setup(struct mpc52xx_gpt_priv *gpt,702const u32 *period)703{704return 0;705}706707#endif /* CONFIG_MPC5200_WDT */708709/* ---------------------------------------------------------------------710* of_platform bus binding code711*/712static int mpc52xx_gpt_probe(struct platform_device *ofdev)713{714struct mpc52xx_gpt_priv *gpt;715716gpt = devm_kzalloc(&ofdev->dev, sizeof *gpt, GFP_KERNEL);717if (!gpt)718return -ENOMEM;719720raw_spin_lock_init(&gpt->lock);721gpt->dev = &ofdev->dev;722gpt->ipb_freq = mpc5xxx_get_bus_frequency(&ofdev->dev);723gpt->regs = of_iomap(ofdev->dev.of_node, 0);724if (!gpt->regs)725return -ENOMEM;726727dev_set_drvdata(&ofdev->dev, gpt);728729mpc52xx_gpt_gpio_setup(gpt);730mpc52xx_gpt_irq_setup(gpt, ofdev->dev.of_node);731732mutex_lock(&mpc52xx_gpt_list_mutex);733list_add(&gpt->list, &mpc52xx_gpt_list);734mutex_unlock(&mpc52xx_gpt_list_mutex);735736/* check if this device could be a watchdog */737if (of_property_read_bool(ofdev->dev.of_node, "fsl,has-wdt") ||738of_property_read_bool(ofdev->dev.of_node, "has-wdt")) {739const u32 *on_boot_wdt;740741gpt->wdt_mode = MPC52xx_GPT_CAN_WDT;742on_boot_wdt = of_get_property(ofdev->dev.of_node,743"fsl,wdt-on-boot", NULL);744if (on_boot_wdt) {745dev_info(gpt->dev, "used as watchdog\n");746gpt->wdt_mode |= MPC52xx_GPT_IS_WDT;747} else748dev_info(gpt->dev, "can function as watchdog\n");749mpc52xx_gpt_wdt_setup(gpt, on_boot_wdt);750}751752return 0;753}754755static const struct of_device_id mpc52xx_gpt_match[] = {756{ .compatible = "fsl,mpc5200-gpt", },757758/* Depreciated compatible values; don't use for new dts files */759{ .compatible = "fsl,mpc5200-gpt-gpio", },760{ .compatible = "mpc5200-gpt", },761{}762};763764static struct platform_driver mpc52xx_gpt_driver = {765.driver = {766.name = "mpc52xx-gpt",767.suppress_bind_attrs = true,768.of_match_table = mpc52xx_gpt_match,769},770.probe = mpc52xx_gpt_probe,771};772773static int __init mpc52xx_gpt_init(void)774{775return platform_driver_register(&mpc52xx_gpt_driver);776}777778/* Make sure GPIOs and IRQs get set up before anyone tries to use them */779subsys_initcall(mpc52xx_gpt_init);780device_initcall(mpc52xx_gpt_wdt_init);781782783