18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * MPC5200 General Purpose Timer device driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2009 Secret Lab Technologies Ltd.
68c2ecf20Sopenharmony_ci * Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * This file is a driver for the the General Purpose Timer (gpt) devices
98c2ecf20Sopenharmony_ci * found on the MPC5200 SoC.  Each timer has an IO pin which can be used
108c2ecf20Sopenharmony_ci * for GPIO or can be used to raise interrupts.  The timer function can
118c2ecf20Sopenharmony_ci * be used independently from the IO pin, or it can be used to control
128c2ecf20Sopenharmony_ci * output signals or measure input signals.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * This driver supports the GPIO and IRQ controller functions of the GPT
158c2ecf20Sopenharmony_ci * device.  Timer functions are not yet supported.
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci * The timer gpt0 can be used as watchdog (wdt).  If the wdt mode is used,
188c2ecf20Sopenharmony_ci * this prevents the use of any gpt0 gpt function (i.e. they will fail with
198c2ecf20Sopenharmony_ci * -EBUSY).  Thus, the safety wdt function always has precedence over the gpt
208c2ecf20Sopenharmony_ci * function.  If the kernel has been compiled with CONFIG_WATCHDOG_NOWAYOUT,
218c2ecf20Sopenharmony_ci * this means that gpt0 is locked in wdt mode until the next reboot - this
228c2ecf20Sopenharmony_ci * may be a requirement in safety applications.
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci * To use the GPIO function, the following two properties must be added
258c2ecf20Sopenharmony_ci * to the device tree node for the gpt device (typically in the .dts file
268c2ecf20Sopenharmony_ci * for the board):
278c2ecf20Sopenharmony_ci * 	gpio-controller;
288c2ecf20Sopenharmony_ci * 	#gpio-cells = < 2 >;
298c2ecf20Sopenharmony_ci * This driver will register the GPIO pin if it finds the gpio-controller
308c2ecf20Sopenharmony_ci * property in the device tree.
318c2ecf20Sopenharmony_ci *
328c2ecf20Sopenharmony_ci * To use the IRQ controller function, the following two properties must
338c2ecf20Sopenharmony_ci * be added to the device tree node for the gpt device:
348c2ecf20Sopenharmony_ci * 	interrupt-controller;
358c2ecf20Sopenharmony_ci * 	#interrupt-cells = < 1 >;
368c2ecf20Sopenharmony_ci * The IRQ controller binding only uses one cell to specify the interrupt,
378c2ecf20Sopenharmony_ci * and the IRQ flags are encoded in the cell.  A cell is not used to encode
388c2ecf20Sopenharmony_ci * the IRQ number because the GPT only has a single IRQ source.  For flags,
398c2ecf20Sopenharmony_ci * a value of '1' means rising edge sensitive and '2' means falling edge.
408c2ecf20Sopenharmony_ci *
418c2ecf20Sopenharmony_ci * The GPIO and the IRQ controller functions can be used at the same time,
428c2ecf20Sopenharmony_ci * but in this use case the IO line will only work as an input.  Trying to
438c2ecf20Sopenharmony_ci * use it as a GPIO output will not work.
448c2ecf20Sopenharmony_ci *
458c2ecf20Sopenharmony_ci * When using the GPIO line as an output, it can either be driven as normal
468c2ecf20Sopenharmony_ci * IO, or it can be an Open Collector (OC) output.  At the moment it is the
478c2ecf20Sopenharmony_ci * responsibility of either the bootloader or the platform setup code to set
488c2ecf20Sopenharmony_ci * the output mode.  This driver does not change the output mode setting.
498c2ecf20Sopenharmony_ci */
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#include <linux/device.h>
528c2ecf20Sopenharmony_ci#include <linux/irq.h>
538c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
548c2ecf20Sopenharmony_ci#include <linux/io.h>
558c2ecf20Sopenharmony_ci#include <linux/list.h>
568c2ecf20Sopenharmony_ci#include <linux/mutex.h>
578c2ecf20Sopenharmony_ci#include <linux/of.h>
588c2ecf20Sopenharmony_ci#include <linux/of_platform.h>
598c2ecf20Sopenharmony_ci#include <linux/of_gpio.h>
608c2ecf20Sopenharmony_ci#include <linux/kernel.h>
618c2ecf20Sopenharmony_ci#include <linux/slab.h>
628c2ecf20Sopenharmony_ci#include <linux/fs.h>
638c2ecf20Sopenharmony_ci#include <linux/watchdog.h>
648c2ecf20Sopenharmony_ci#include <linux/miscdevice.h>
658c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
668c2ecf20Sopenharmony_ci#include <linux/module.h>
678c2ecf20Sopenharmony_ci#include <asm/div64.h>
688c2ecf20Sopenharmony_ci#include <asm/mpc52xx.h>
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Freescale MPC52xx gpt driver");
718c2ecf20Sopenharmony_ciMODULE_AUTHOR("Sascha Hauer, Grant Likely, Albrecht Dreß");
728c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci/**
758c2ecf20Sopenharmony_ci * struct mpc52xx_gpt - Private data structure for MPC52xx GPT driver
768c2ecf20Sopenharmony_ci * @dev: pointer to device structure
778c2ecf20Sopenharmony_ci * @regs: virtual address of GPT registers
788c2ecf20Sopenharmony_ci * @lock: spinlock to coordinate between different functions.
798c2ecf20Sopenharmony_ci * @gc: gpio_chip instance structure; used when GPIO is enabled
808c2ecf20Sopenharmony_ci * @irqhost: Pointer to irq_domain instance; used when IRQ mode is supported
818c2ecf20Sopenharmony_ci * @wdt_mode: only relevant for gpt0: bit 0 (MPC52xx_GPT_CAN_WDT) indicates
828c2ecf20Sopenharmony_ci *   if the gpt may be used as wdt, bit 1 (MPC52xx_GPT_IS_WDT) indicates
838c2ecf20Sopenharmony_ci *   if the timer is actively used as wdt which blocks gpt functions
848c2ecf20Sopenharmony_ci */
858c2ecf20Sopenharmony_cistruct mpc52xx_gpt_priv {
868c2ecf20Sopenharmony_ci	struct list_head list;		/* List of all GPT devices */
878c2ecf20Sopenharmony_ci	struct device *dev;
888c2ecf20Sopenharmony_ci	struct mpc52xx_gpt __iomem *regs;
898c2ecf20Sopenharmony_ci	raw_spinlock_t lock;
908c2ecf20Sopenharmony_ci	struct irq_domain *irqhost;
918c2ecf20Sopenharmony_ci	u32 ipb_freq;
928c2ecf20Sopenharmony_ci	u8 wdt_mode;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci#if defined(CONFIG_GPIOLIB)
958c2ecf20Sopenharmony_ci	struct gpio_chip gc;
968c2ecf20Sopenharmony_ci#endif
978c2ecf20Sopenharmony_ci};
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ciLIST_HEAD(mpc52xx_gpt_list);
1008c2ecf20Sopenharmony_ciDEFINE_MUTEX(mpc52xx_gpt_list_mutex);
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci#define MPC52xx_GPT_MODE_MS_MASK	(0x07)
1038c2ecf20Sopenharmony_ci#define MPC52xx_GPT_MODE_MS_IC		(0x01)
1048c2ecf20Sopenharmony_ci#define MPC52xx_GPT_MODE_MS_OC		(0x02)
1058c2ecf20Sopenharmony_ci#define MPC52xx_GPT_MODE_MS_PWM		(0x03)
1068c2ecf20Sopenharmony_ci#define MPC52xx_GPT_MODE_MS_GPIO	(0x04)
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci#define MPC52xx_GPT_MODE_GPIO_MASK	(0x30)
1098c2ecf20Sopenharmony_ci#define MPC52xx_GPT_MODE_GPIO_OUT_LOW	(0x20)
1108c2ecf20Sopenharmony_ci#define MPC52xx_GPT_MODE_GPIO_OUT_HIGH	(0x30)
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci#define MPC52xx_GPT_MODE_COUNTER_ENABLE	(0x1000)
1138c2ecf20Sopenharmony_ci#define MPC52xx_GPT_MODE_CONTINUOUS	(0x0400)
1148c2ecf20Sopenharmony_ci#define MPC52xx_GPT_MODE_OPEN_DRAIN	(0x0200)
1158c2ecf20Sopenharmony_ci#define MPC52xx_GPT_MODE_IRQ_EN		(0x0100)
1168c2ecf20Sopenharmony_ci#define MPC52xx_GPT_MODE_WDT_EN		(0x8000)
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci#define MPC52xx_GPT_MODE_ICT_MASK	(0x030000)
1198c2ecf20Sopenharmony_ci#define MPC52xx_GPT_MODE_ICT_RISING	(0x010000)
1208c2ecf20Sopenharmony_ci#define MPC52xx_GPT_MODE_ICT_FALLING	(0x020000)
1218c2ecf20Sopenharmony_ci#define MPC52xx_GPT_MODE_ICT_TOGGLE	(0x030000)
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci#define MPC52xx_GPT_MODE_WDT_PING	(0xa5)
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci#define MPC52xx_GPT_STATUS_IRQMASK	(0x000f)
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci#define MPC52xx_GPT_CAN_WDT		(1 << 0)
1288c2ecf20Sopenharmony_ci#define MPC52xx_GPT_IS_WDT		(1 << 1)
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci/* ---------------------------------------------------------------------
1328c2ecf20Sopenharmony_ci * Cascaded interrupt controller hooks
1338c2ecf20Sopenharmony_ci */
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistatic void mpc52xx_gpt_irq_unmask(struct irq_data *d)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	struct mpc52xx_gpt_priv *gpt = irq_data_get_irq_chip_data(d);
1388c2ecf20Sopenharmony_ci	unsigned long flags;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	raw_spin_lock_irqsave(&gpt->lock, flags);
1418c2ecf20Sopenharmony_ci	setbits32(&gpt->regs->mode, MPC52xx_GPT_MODE_IRQ_EN);
1428c2ecf20Sopenharmony_ci	raw_spin_unlock_irqrestore(&gpt->lock, flags);
1438c2ecf20Sopenharmony_ci}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_cistatic void mpc52xx_gpt_irq_mask(struct irq_data *d)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	struct mpc52xx_gpt_priv *gpt = irq_data_get_irq_chip_data(d);
1488c2ecf20Sopenharmony_ci	unsigned long flags;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	raw_spin_lock_irqsave(&gpt->lock, flags);
1518c2ecf20Sopenharmony_ci	clrbits32(&gpt->regs->mode, MPC52xx_GPT_MODE_IRQ_EN);
1528c2ecf20Sopenharmony_ci	raw_spin_unlock_irqrestore(&gpt->lock, flags);
1538c2ecf20Sopenharmony_ci}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_cistatic void mpc52xx_gpt_irq_ack(struct irq_data *d)
1568c2ecf20Sopenharmony_ci{
1578c2ecf20Sopenharmony_ci	struct mpc52xx_gpt_priv *gpt = irq_data_get_irq_chip_data(d);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	out_be32(&gpt->regs->status, MPC52xx_GPT_STATUS_IRQMASK);
1608c2ecf20Sopenharmony_ci}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cistatic int mpc52xx_gpt_irq_set_type(struct irq_data *d, unsigned int flow_type)
1638c2ecf20Sopenharmony_ci{
1648c2ecf20Sopenharmony_ci	struct mpc52xx_gpt_priv *gpt = irq_data_get_irq_chip_data(d);
1658c2ecf20Sopenharmony_ci	unsigned long flags;
1668c2ecf20Sopenharmony_ci	u32 reg;
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	dev_dbg(gpt->dev, "%s: virq=%i type=%x\n", __func__, d->irq, flow_type);
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	raw_spin_lock_irqsave(&gpt->lock, flags);
1718c2ecf20Sopenharmony_ci	reg = in_be32(&gpt->regs->mode) & ~MPC52xx_GPT_MODE_ICT_MASK;
1728c2ecf20Sopenharmony_ci	if (flow_type & IRQF_TRIGGER_RISING)
1738c2ecf20Sopenharmony_ci		reg |= MPC52xx_GPT_MODE_ICT_RISING;
1748c2ecf20Sopenharmony_ci	if (flow_type & IRQF_TRIGGER_FALLING)
1758c2ecf20Sopenharmony_ci		reg |= MPC52xx_GPT_MODE_ICT_FALLING;
1768c2ecf20Sopenharmony_ci	out_be32(&gpt->regs->mode, reg);
1778c2ecf20Sopenharmony_ci	raw_spin_unlock_irqrestore(&gpt->lock, flags);
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	return 0;
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_cistatic struct irq_chip mpc52xx_gpt_irq_chip = {
1838c2ecf20Sopenharmony_ci	.name = "MPC52xx GPT",
1848c2ecf20Sopenharmony_ci	.irq_unmask = mpc52xx_gpt_irq_unmask,
1858c2ecf20Sopenharmony_ci	.irq_mask = mpc52xx_gpt_irq_mask,
1868c2ecf20Sopenharmony_ci	.irq_ack = mpc52xx_gpt_irq_ack,
1878c2ecf20Sopenharmony_ci	.irq_set_type = mpc52xx_gpt_irq_set_type,
1888c2ecf20Sopenharmony_ci};
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cistatic void mpc52xx_gpt_irq_cascade(struct irq_desc *desc)
1918c2ecf20Sopenharmony_ci{
1928c2ecf20Sopenharmony_ci	struct mpc52xx_gpt_priv *gpt = irq_desc_get_handler_data(desc);
1938c2ecf20Sopenharmony_ci	int sub_virq;
1948c2ecf20Sopenharmony_ci	u32 status;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	status = in_be32(&gpt->regs->status) & MPC52xx_GPT_STATUS_IRQMASK;
1978c2ecf20Sopenharmony_ci	if (status) {
1988c2ecf20Sopenharmony_ci		sub_virq = irq_linear_revmap(gpt->irqhost, 0);
1998c2ecf20Sopenharmony_ci		generic_handle_irq(sub_virq);
2008c2ecf20Sopenharmony_ci	}
2018c2ecf20Sopenharmony_ci}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_cistatic int mpc52xx_gpt_irq_map(struct irq_domain *h, unsigned int virq,
2048c2ecf20Sopenharmony_ci			       irq_hw_number_t hw)
2058c2ecf20Sopenharmony_ci{
2068c2ecf20Sopenharmony_ci	struct mpc52xx_gpt_priv *gpt = h->host_data;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	dev_dbg(gpt->dev, "%s: h=%p, virq=%i\n", __func__, h, virq);
2098c2ecf20Sopenharmony_ci	irq_set_chip_data(virq, gpt);
2108c2ecf20Sopenharmony_ci	irq_set_chip_and_handler(virq, &mpc52xx_gpt_irq_chip, handle_edge_irq);
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	return 0;
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_cistatic int mpc52xx_gpt_irq_xlate(struct irq_domain *h, struct device_node *ct,
2168c2ecf20Sopenharmony_ci				 const u32 *intspec, unsigned int intsize,
2178c2ecf20Sopenharmony_ci				 irq_hw_number_t *out_hwirq,
2188c2ecf20Sopenharmony_ci				 unsigned int *out_flags)
2198c2ecf20Sopenharmony_ci{
2208c2ecf20Sopenharmony_ci	struct mpc52xx_gpt_priv *gpt = h->host_data;
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	dev_dbg(gpt->dev, "%s: flags=%i\n", __func__, intspec[0]);
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	if ((intsize < 1) || (intspec[0] > 3)) {
2258c2ecf20Sopenharmony_ci		dev_err(gpt->dev, "bad irq specifier in %pOF\n", ct);
2268c2ecf20Sopenharmony_ci		return -EINVAL;
2278c2ecf20Sopenharmony_ci	}
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	*out_hwirq = 0; /* The GPT only has 1 IRQ line */
2308c2ecf20Sopenharmony_ci	*out_flags = intspec[0];
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	return 0;
2338c2ecf20Sopenharmony_ci}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_cistatic const struct irq_domain_ops mpc52xx_gpt_irq_ops = {
2368c2ecf20Sopenharmony_ci	.map = mpc52xx_gpt_irq_map,
2378c2ecf20Sopenharmony_ci	.xlate = mpc52xx_gpt_irq_xlate,
2388c2ecf20Sopenharmony_ci};
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_cistatic void
2418c2ecf20Sopenharmony_cimpc52xx_gpt_irq_setup(struct mpc52xx_gpt_priv *gpt, struct device_node *node)
2428c2ecf20Sopenharmony_ci{
2438c2ecf20Sopenharmony_ci	int cascade_virq;
2448c2ecf20Sopenharmony_ci	unsigned long flags;
2458c2ecf20Sopenharmony_ci	u32 mode;
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	cascade_virq = irq_of_parse_and_map(node, 0);
2488c2ecf20Sopenharmony_ci	if (!cascade_virq)
2498c2ecf20Sopenharmony_ci		return;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	gpt->irqhost = irq_domain_add_linear(node, 1, &mpc52xx_gpt_irq_ops, gpt);
2528c2ecf20Sopenharmony_ci	if (!gpt->irqhost) {
2538c2ecf20Sopenharmony_ci		dev_err(gpt->dev, "irq_domain_add_linear() failed\n");
2548c2ecf20Sopenharmony_ci		return;
2558c2ecf20Sopenharmony_ci	}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	irq_set_handler_data(cascade_virq, gpt);
2588c2ecf20Sopenharmony_ci	irq_set_chained_handler(cascade_virq, mpc52xx_gpt_irq_cascade);
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	/* If the GPT is currently disabled, then change it to be in Input
2618c2ecf20Sopenharmony_ci	 * Capture mode.  If the mode is non-zero, then the pin could be
2628c2ecf20Sopenharmony_ci	 * already in use for something. */
2638c2ecf20Sopenharmony_ci	raw_spin_lock_irqsave(&gpt->lock, flags);
2648c2ecf20Sopenharmony_ci	mode = in_be32(&gpt->regs->mode);
2658c2ecf20Sopenharmony_ci	if ((mode & MPC52xx_GPT_MODE_MS_MASK) == 0)
2668c2ecf20Sopenharmony_ci		out_be32(&gpt->regs->mode, mode | MPC52xx_GPT_MODE_MS_IC);
2678c2ecf20Sopenharmony_ci	raw_spin_unlock_irqrestore(&gpt->lock, flags);
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	dev_dbg(gpt->dev, "%s() complete. virq=%i\n", __func__, cascade_virq);
2708c2ecf20Sopenharmony_ci}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci/* ---------------------------------------------------------------------
2748c2ecf20Sopenharmony_ci * GPIOLIB hooks
2758c2ecf20Sopenharmony_ci */
2768c2ecf20Sopenharmony_ci#if defined(CONFIG_GPIOLIB)
2778c2ecf20Sopenharmony_cistatic int mpc52xx_gpt_gpio_get(struct gpio_chip *gc, unsigned int gpio)
2788c2ecf20Sopenharmony_ci{
2798c2ecf20Sopenharmony_ci	struct mpc52xx_gpt_priv *gpt = gpiochip_get_data(gc);
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	return (in_be32(&gpt->regs->status) >> 8) & 1;
2828c2ecf20Sopenharmony_ci}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_cistatic void
2858c2ecf20Sopenharmony_cimpc52xx_gpt_gpio_set(struct gpio_chip *gc, unsigned int gpio, int v)
2868c2ecf20Sopenharmony_ci{
2878c2ecf20Sopenharmony_ci	struct mpc52xx_gpt_priv *gpt = gpiochip_get_data(gc);
2888c2ecf20Sopenharmony_ci	unsigned long flags;
2898c2ecf20Sopenharmony_ci	u32 r;
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	dev_dbg(gpt->dev, "%s: gpio:%d v:%d\n", __func__, gpio, v);
2928c2ecf20Sopenharmony_ci	r = v ? MPC52xx_GPT_MODE_GPIO_OUT_HIGH : MPC52xx_GPT_MODE_GPIO_OUT_LOW;
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	raw_spin_lock_irqsave(&gpt->lock, flags);
2958c2ecf20Sopenharmony_ci	clrsetbits_be32(&gpt->regs->mode, MPC52xx_GPT_MODE_GPIO_MASK, r);
2968c2ecf20Sopenharmony_ci	raw_spin_unlock_irqrestore(&gpt->lock, flags);
2978c2ecf20Sopenharmony_ci}
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_cistatic int mpc52xx_gpt_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
3008c2ecf20Sopenharmony_ci{
3018c2ecf20Sopenharmony_ci	struct mpc52xx_gpt_priv *gpt = gpiochip_get_data(gc);
3028c2ecf20Sopenharmony_ci	unsigned long flags;
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	dev_dbg(gpt->dev, "%s: gpio:%d\n", __func__, gpio);
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	raw_spin_lock_irqsave(&gpt->lock, flags);
3078c2ecf20Sopenharmony_ci	clrbits32(&gpt->regs->mode, MPC52xx_GPT_MODE_GPIO_MASK);
3088c2ecf20Sopenharmony_ci	raw_spin_unlock_irqrestore(&gpt->lock, flags);
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	return 0;
3118c2ecf20Sopenharmony_ci}
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_cistatic int
3148c2ecf20Sopenharmony_cimpc52xx_gpt_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
3158c2ecf20Sopenharmony_ci{
3168c2ecf20Sopenharmony_ci	mpc52xx_gpt_gpio_set(gc, gpio, val);
3178c2ecf20Sopenharmony_ci	return 0;
3188c2ecf20Sopenharmony_ci}
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_cistatic void
3218c2ecf20Sopenharmony_cimpc52xx_gpt_gpio_setup(struct mpc52xx_gpt_priv *gpt, struct device_node *node)
3228c2ecf20Sopenharmony_ci{
3238c2ecf20Sopenharmony_ci	int rc;
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	/* Only setup GPIO if the device tree claims the GPT is
3268c2ecf20Sopenharmony_ci	 * a GPIO controller */
3278c2ecf20Sopenharmony_ci	if (!of_find_property(node, "gpio-controller", NULL))
3288c2ecf20Sopenharmony_ci		return;
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	gpt->gc.label = kasprintf(GFP_KERNEL, "%pOF", node);
3318c2ecf20Sopenharmony_ci	if (!gpt->gc.label) {
3328c2ecf20Sopenharmony_ci		dev_err(gpt->dev, "out of memory\n");
3338c2ecf20Sopenharmony_ci		return;
3348c2ecf20Sopenharmony_ci	}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	gpt->gc.ngpio = 1;
3378c2ecf20Sopenharmony_ci	gpt->gc.direction_input  = mpc52xx_gpt_gpio_dir_in;
3388c2ecf20Sopenharmony_ci	gpt->gc.direction_output = mpc52xx_gpt_gpio_dir_out;
3398c2ecf20Sopenharmony_ci	gpt->gc.get = mpc52xx_gpt_gpio_get;
3408c2ecf20Sopenharmony_ci	gpt->gc.set = mpc52xx_gpt_gpio_set;
3418c2ecf20Sopenharmony_ci	gpt->gc.base = -1;
3428c2ecf20Sopenharmony_ci	gpt->gc.of_node = node;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	/* Setup external pin in GPIO mode */
3458c2ecf20Sopenharmony_ci	clrsetbits_be32(&gpt->regs->mode, MPC52xx_GPT_MODE_MS_MASK,
3468c2ecf20Sopenharmony_ci			MPC52xx_GPT_MODE_MS_GPIO);
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	rc = gpiochip_add_data(&gpt->gc, gpt);
3498c2ecf20Sopenharmony_ci	if (rc)
3508c2ecf20Sopenharmony_ci		dev_err(gpt->dev, "gpiochip_add_data() failed; rc=%i\n", rc);
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	dev_dbg(gpt->dev, "%s() complete.\n", __func__);
3538c2ecf20Sopenharmony_ci}
3548c2ecf20Sopenharmony_ci#else /* defined(CONFIG_GPIOLIB) */
3558c2ecf20Sopenharmony_cistatic void
3568c2ecf20Sopenharmony_cimpc52xx_gpt_gpio_setup(struct mpc52xx_gpt_priv *p, struct device_node *np) { }
3578c2ecf20Sopenharmony_ci#endif /* defined(CONFIG_GPIOLIB) */
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci/***********************************************************************
3608c2ecf20Sopenharmony_ci * Timer API
3618c2ecf20Sopenharmony_ci */
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci/**
3648c2ecf20Sopenharmony_ci * mpc52xx_gpt_from_irq - Return the GPT device associated with an IRQ number
3658c2ecf20Sopenharmony_ci * @irq: irq of timer.
3668c2ecf20Sopenharmony_ci */
3678c2ecf20Sopenharmony_cistruct mpc52xx_gpt_priv *mpc52xx_gpt_from_irq(int irq)
3688c2ecf20Sopenharmony_ci{
3698c2ecf20Sopenharmony_ci	struct mpc52xx_gpt_priv *gpt;
3708c2ecf20Sopenharmony_ci	struct list_head *pos;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	/* Iterate over the list of timers looking for a matching device */
3738c2ecf20Sopenharmony_ci	mutex_lock(&mpc52xx_gpt_list_mutex);
3748c2ecf20Sopenharmony_ci	list_for_each(pos, &mpc52xx_gpt_list) {
3758c2ecf20Sopenharmony_ci		gpt = container_of(pos, struct mpc52xx_gpt_priv, list);
3768c2ecf20Sopenharmony_ci		if (gpt->irqhost && irq == irq_linear_revmap(gpt->irqhost, 0)) {
3778c2ecf20Sopenharmony_ci			mutex_unlock(&mpc52xx_gpt_list_mutex);
3788c2ecf20Sopenharmony_ci			return gpt;
3798c2ecf20Sopenharmony_ci		}
3808c2ecf20Sopenharmony_ci	}
3818c2ecf20Sopenharmony_ci	mutex_unlock(&mpc52xx_gpt_list_mutex);
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	return NULL;
3848c2ecf20Sopenharmony_ci}
3858c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mpc52xx_gpt_from_irq);
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_cistatic int mpc52xx_gpt_do_start(struct mpc52xx_gpt_priv *gpt, u64 period,
3888c2ecf20Sopenharmony_ci				int continuous, int as_wdt)
3898c2ecf20Sopenharmony_ci{
3908c2ecf20Sopenharmony_ci	u32 clear, set;
3918c2ecf20Sopenharmony_ci	u64 clocks;
3928c2ecf20Sopenharmony_ci	u32 prescale;
3938c2ecf20Sopenharmony_ci	unsigned long flags;
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	clear = MPC52xx_GPT_MODE_MS_MASK | MPC52xx_GPT_MODE_CONTINUOUS;
3968c2ecf20Sopenharmony_ci	set = MPC52xx_GPT_MODE_MS_GPIO | MPC52xx_GPT_MODE_COUNTER_ENABLE;
3978c2ecf20Sopenharmony_ci	if (as_wdt) {
3988c2ecf20Sopenharmony_ci		clear |= MPC52xx_GPT_MODE_IRQ_EN;
3998c2ecf20Sopenharmony_ci		set |= MPC52xx_GPT_MODE_WDT_EN;
4008c2ecf20Sopenharmony_ci	} else if (continuous)
4018c2ecf20Sopenharmony_ci		set |= MPC52xx_GPT_MODE_CONTINUOUS;
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	/* Determine the number of clocks in the requested period.  64 bit
4048c2ecf20Sopenharmony_ci	 * arithmatic is done here to preserve the precision until the value
4058c2ecf20Sopenharmony_ci	 * is scaled back down into the u32 range.  Period is in 'ns', bus
4068c2ecf20Sopenharmony_ci	 * frequency is in Hz. */
4078c2ecf20Sopenharmony_ci	clocks = period * (u64)gpt->ipb_freq;
4088c2ecf20Sopenharmony_ci	do_div(clocks, 1000000000); /* Scale it down to ns range */
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	/* This device cannot handle a clock count greater than 32 bits */
4118c2ecf20Sopenharmony_ci	if (clocks > 0xffffffff)
4128c2ecf20Sopenharmony_ci		return -EINVAL;
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	/* Calculate the prescaler and count values from the clocks value.
4158c2ecf20Sopenharmony_ci	 * 'clocks' is the number of clock ticks in the period.  The timer
4168c2ecf20Sopenharmony_ci	 * has 16 bit precision and a 16 bit prescaler.  Prescaler is
4178c2ecf20Sopenharmony_ci	 * calculated by integer dividing the clocks by 0x10000 (shifting
4188c2ecf20Sopenharmony_ci	 * down 16 bits) to obtain the smallest possible divisor for clocks
4198c2ecf20Sopenharmony_ci	 * to get a 16 bit count value.
4208c2ecf20Sopenharmony_ci	 *
4218c2ecf20Sopenharmony_ci	 * Note: the prescale register is '1' based, not '0' based.  ie. a
4228c2ecf20Sopenharmony_ci	 * value of '1' means divide the clock by one.  0xffff divides the
4238c2ecf20Sopenharmony_ci	 * clock by 0xffff.  '0x0000' does not divide by zero, but wraps
4248c2ecf20Sopenharmony_ci	 * around and divides by 0x10000.  That is why prescale must be
4258c2ecf20Sopenharmony_ci	 * a u32 variable, not a u16, for this calculation. */
4268c2ecf20Sopenharmony_ci	prescale = (clocks >> 16) + 1;
4278c2ecf20Sopenharmony_ci	do_div(clocks, prescale);
4288c2ecf20Sopenharmony_ci	if (clocks > 0xffff) {
4298c2ecf20Sopenharmony_ci		pr_err("calculation error; prescale:%x clocks:%llx\n",
4308c2ecf20Sopenharmony_ci		       prescale, clocks);
4318c2ecf20Sopenharmony_ci		return -EINVAL;
4328c2ecf20Sopenharmony_ci	}
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	/* Set and enable the timer, reject an attempt to use a wdt as gpt */
4358c2ecf20Sopenharmony_ci	raw_spin_lock_irqsave(&gpt->lock, flags);
4368c2ecf20Sopenharmony_ci	if (as_wdt)
4378c2ecf20Sopenharmony_ci		gpt->wdt_mode |= MPC52xx_GPT_IS_WDT;
4388c2ecf20Sopenharmony_ci	else if ((gpt->wdt_mode & MPC52xx_GPT_IS_WDT) != 0) {
4398c2ecf20Sopenharmony_ci		raw_spin_unlock_irqrestore(&gpt->lock, flags);
4408c2ecf20Sopenharmony_ci		return -EBUSY;
4418c2ecf20Sopenharmony_ci	}
4428c2ecf20Sopenharmony_ci	out_be32(&gpt->regs->count, prescale << 16 | clocks);
4438c2ecf20Sopenharmony_ci	clrsetbits_be32(&gpt->regs->mode, clear, set);
4448c2ecf20Sopenharmony_ci	raw_spin_unlock_irqrestore(&gpt->lock, flags);
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	return 0;
4478c2ecf20Sopenharmony_ci}
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci/**
4508c2ecf20Sopenharmony_ci * mpc52xx_gpt_start_timer - Set and enable the GPT timer
4518c2ecf20Sopenharmony_ci * @gpt: Pointer to gpt private data structure
4528c2ecf20Sopenharmony_ci * @period: period of timer in ns; max. ~130s @ 33MHz IPB clock
4538c2ecf20Sopenharmony_ci * @continuous: set to 1 to make timer continuous free running
4548c2ecf20Sopenharmony_ci *
4558c2ecf20Sopenharmony_ci * An interrupt will be generated every time the timer fires
4568c2ecf20Sopenharmony_ci */
4578c2ecf20Sopenharmony_ciint mpc52xx_gpt_start_timer(struct mpc52xx_gpt_priv *gpt, u64 period,
4588c2ecf20Sopenharmony_ci                            int continuous)
4598c2ecf20Sopenharmony_ci{
4608c2ecf20Sopenharmony_ci	return mpc52xx_gpt_do_start(gpt, period, continuous, 0);
4618c2ecf20Sopenharmony_ci}
4628c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mpc52xx_gpt_start_timer);
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci/**
4658c2ecf20Sopenharmony_ci * mpc52xx_gpt_stop_timer - Stop a gpt
4668c2ecf20Sopenharmony_ci * @gpt: Pointer to gpt private data structure
4678c2ecf20Sopenharmony_ci *
4688c2ecf20Sopenharmony_ci * Returns an error if attempting to stop a wdt
4698c2ecf20Sopenharmony_ci */
4708c2ecf20Sopenharmony_ciint mpc52xx_gpt_stop_timer(struct mpc52xx_gpt_priv *gpt)
4718c2ecf20Sopenharmony_ci{
4728c2ecf20Sopenharmony_ci	unsigned long flags;
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	/* reject the operation if the timer is used as watchdog (gpt 0 only) */
4758c2ecf20Sopenharmony_ci	raw_spin_lock_irqsave(&gpt->lock, flags);
4768c2ecf20Sopenharmony_ci	if ((gpt->wdt_mode & MPC52xx_GPT_IS_WDT) != 0) {
4778c2ecf20Sopenharmony_ci		raw_spin_unlock_irqrestore(&gpt->lock, flags);
4788c2ecf20Sopenharmony_ci		return -EBUSY;
4798c2ecf20Sopenharmony_ci	}
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	clrbits32(&gpt->regs->mode, MPC52xx_GPT_MODE_COUNTER_ENABLE);
4828c2ecf20Sopenharmony_ci	raw_spin_unlock_irqrestore(&gpt->lock, flags);
4838c2ecf20Sopenharmony_ci	return 0;
4848c2ecf20Sopenharmony_ci}
4858c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mpc52xx_gpt_stop_timer);
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci/**
4888c2ecf20Sopenharmony_ci * mpc52xx_gpt_timer_period - Read the timer period
4898c2ecf20Sopenharmony_ci * @gpt: Pointer to gpt private data structure
4908c2ecf20Sopenharmony_ci *
4918c2ecf20Sopenharmony_ci * Returns the timer period in ns
4928c2ecf20Sopenharmony_ci */
4938c2ecf20Sopenharmony_ciu64 mpc52xx_gpt_timer_period(struct mpc52xx_gpt_priv *gpt)
4948c2ecf20Sopenharmony_ci{
4958c2ecf20Sopenharmony_ci	u64 period;
4968c2ecf20Sopenharmony_ci	u64 prescale;
4978c2ecf20Sopenharmony_ci	unsigned long flags;
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci	raw_spin_lock_irqsave(&gpt->lock, flags);
5008c2ecf20Sopenharmony_ci	period = in_be32(&gpt->regs->count);
5018c2ecf20Sopenharmony_ci	raw_spin_unlock_irqrestore(&gpt->lock, flags);
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	prescale = period >> 16;
5048c2ecf20Sopenharmony_ci	period &= 0xffff;
5058c2ecf20Sopenharmony_ci	if (prescale == 0)
5068c2ecf20Sopenharmony_ci		prescale = 0x10000;
5078c2ecf20Sopenharmony_ci	period = period * prescale * 1000000000ULL;
5088c2ecf20Sopenharmony_ci	do_div(period, (u64)gpt->ipb_freq);
5098c2ecf20Sopenharmony_ci	return period;
5108c2ecf20Sopenharmony_ci}
5118c2ecf20Sopenharmony_ciEXPORT_SYMBOL(mpc52xx_gpt_timer_period);
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci#if defined(CONFIG_MPC5200_WDT)
5148c2ecf20Sopenharmony_ci/***********************************************************************
5158c2ecf20Sopenharmony_ci * Watchdog API for gpt0
5168c2ecf20Sopenharmony_ci */
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci#define WDT_IDENTITY	    "mpc52xx watchdog on GPT0"
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci/* wdt_is_active stores whether or not the /dev/watchdog device is opened */
5218c2ecf20Sopenharmony_cistatic unsigned long wdt_is_active;
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci/* wdt-capable gpt */
5248c2ecf20Sopenharmony_cistatic struct mpc52xx_gpt_priv *mpc52xx_gpt_wdt;
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci/* low-level wdt functions */
5278c2ecf20Sopenharmony_cistatic inline void mpc52xx_gpt_wdt_ping(struct mpc52xx_gpt_priv *gpt_wdt)
5288c2ecf20Sopenharmony_ci{
5298c2ecf20Sopenharmony_ci	unsigned long flags;
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci	raw_spin_lock_irqsave(&gpt_wdt->lock, flags);
5328c2ecf20Sopenharmony_ci	out_8((u8 *) &gpt_wdt->regs->mode, MPC52xx_GPT_MODE_WDT_PING);
5338c2ecf20Sopenharmony_ci	raw_spin_unlock_irqrestore(&gpt_wdt->lock, flags);
5348c2ecf20Sopenharmony_ci}
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci/* wdt misc device api */
5378c2ecf20Sopenharmony_cistatic ssize_t mpc52xx_wdt_write(struct file *file, const char __user *data,
5388c2ecf20Sopenharmony_ci				 size_t len, loff_t *ppos)
5398c2ecf20Sopenharmony_ci{
5408c2ecf20Sopenharmony_ci	struct mpc52xx_gpt_priv *gpt_wdt = file->private_data;
5418c2ecf20Sopenharmony_ci	mpc52xx_gpt_wdt_ping(gpt_wdt);
5428c2ecf20Sopenharmony_ci	return 0;
5438c2ecf20Sopenharmony_ci}
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_cistatic const struct watchdog_info mpc5200_wdt_info = {
5468c2ecf20Sopenharmony_ci	.options	= WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
5478c2ecf20Sopenharmony_ci	.identity	= WDT_IDENTITY,
5488c2ecf20Sopenharmony_ci};
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_cistatic long mpc52xx_wdt_ioctl(struct file *file, unsigned int cmd,
5518c2ecf20Sopenharmony_ci			      unsigned long arg)
5528c2ecf20Sopenharmony_ci{
5538c2ecf20Sopenharmony_ci	struct mpc52xx_gpt_priv *gpt_wdt = file->private_data;
5548c2ecf20Sopenharmony_ci	int __user *data = (int __user *)arg;
5558c2ecf20Sopenharmony_ci	int timeout;
5568c2ecf20Sopenharmony_ci	u64 real_timeout;
5578c2ecf20Sopenharmony_ci	int ret = 0;
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci	switch (cmd) {
5608c2ecf20Sopenharmony_ci	case WDIOC_GETSUPPORT:
5618c2ecf20Sopenharmony_ci		ret = copy_to_user(data, &mpc5200_wdt_info,
5628c2ecf20Sopenharmony_ci				   sizeof(mpc5200_wdt_info));
5638c2ecf20Sopenharmony_ci		if (ret)
5648c2ecf20Sopenharmony_ci			ret = -EFAULT;
5658c2ecf20Sopenharmony_ci		break;
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	case WDIOC_GETSTATUS:
5688c2ecf20Sopenharmony_ci	case WDIOC_GETBOOTSTATUS:
5698c2ecf20Sopenharmony_ci		ret = put_user(0, data);
5708c2ecf20Sopenharmony_ci		break;
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci	case WDIOC_KEEPALIVE:
5738c2ecf20Sopenharmony_ci		mpc52xx_gpt_wdt_ping(gpt_wdt);
5748c2ecf20Sopenharmony_ci		break;
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci	case WDIOC_SETTIMEOUT:
5778c2ecf20Sopenharmony_ci		ret = get_user(timeout, data);
5788c2ecf20Sopenharmony_ci		if (ret)
5798c2ecf20Sopenharmony_ci			break;
5808c2ecf20Sopenharmony_ci		real_timeout = (u64) timeout * 1000000000ULL;
5818c2ecf20Sopenharmony_ci		ret = mpc52xx_gpt_do_start(gpt_wdt, real_timeout, 0, 1);
5828c2ecf20Sopenharmony_ci		if (ret)
5838c2ecf20Sopenharmony_ci			break;
5848c2ecf20Sopenharmony_ci		/* fall through and return the timeout */
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci	case WDIOC_GETTIMEOUT:
5878c2ecf20Sopenharmony_ci		/* we need to round here as to avoid e.g. the following
5888c2ecf20Sopenharmony_ci		 * situation:
5898c2ecf20Sopenharmony_ci		 * - timeout requested is 1 second;
5908c2ecf20Sopenharmony_ci		 * - real timeout @33MHz is 999997090ns
5918c2ecf20Sopenharmony_ci		 * - the int divide by 10^9 will return 0.
5928c2ecf20Sopenharmony_ci		 */
5938c2ecf20Sopenharmony_ci		real_timeout =
5948c2ecf20Sopenharmony_ci			mpc52xx_gpt_timer_period(gpt_wdt) + 500000000ULL;
5958c2ecf20Sopenharmony_ci		do_div(real_timeout, 1000000000ULL);
5968c2ecf20Sopenharmony_ci		timeout = (int) real_timeout;
5978c2ecf20Sopenharmony_ci		ret = put_user(timeout, data);
5988c2ecf20Sopenharmony_ci		break;
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci	default:
6018c2ecf20Sopenharmony_ci		ret = -ENOTTY;
6028c2ecf20Sopenharmony_ci	}
6038c2ecf20Sopenharmony_ci	return ret;
6048c2ecf20Sopenharmony_ci}
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_cistatic int mpc52xx_wdt_open(struct inode *inode, struct file *file)
6078c2ecf20Sopenharmony_ci{
6088c2ecf20Sopenharmony_ci	int ret;
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	/* sanity check */
6118c2ecf20Sopenharmony_ci	if (!mpc52xx_gpt_wdt)
6128c2ecf20Sopenharmony_ci		return -ENODEV;
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	/* /dev/watchdog can only be opened once */
6158c2ecf20Sopenharmony_ci	if (test_and_set_bit(0, &wdt_is_active))
6168c2ecf20Sopenharmony_ci		return -EBUSY;
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci	/* Set and activate the watchdog with 30 seconds timeout */
6198c2ecf20Sopenharmony_ci	ret = mpc52xx_gpt_do_start(mpc52xx_gpt_wdt, 30ULL * 1000000000ULL,
6208c2ecf20Sopenharmony_ci				   0, 1);
6218c2ecf20Sopenharmony_ci	if (ret) {
6228c2ecf20Sopenharmony_ci		clear_bit(0, &wdt_is_active);
6238c2ecf20Sopenharmony_ci		return ret;
6248c2ecf20Sopenharmony_ci	}
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	file->private_data = mpc52xx_gpt_wdt;
6278c2ecf20Sopenharmony_ci	return stream_open(inode, file);
6288c2ecf20Sopenharmony_ci}
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_cistatic int mpc52xx_wdt_release(struct inode *inode, struct file *file)
6318c2ecf20Sopenharmony_ci{
6328c2ecf20Sopenharmony_ci	/* note: releasing the wdt in NOWAYOUT-mode does not stop it */
6338c2ecf20Sopenharmony_ci#if !defined(CONFIG_WATCHDOG_NOWAYOUT)
6348c2ecf20Sopenharmony_ci	struct mpc52xx_gpt_priv *gpt_wdt = file->private_data;
6358c2ecf20Sopenharmony_ci	unsigned long flags;
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci	raw_spin_lock_irqsave(&gpt_wdt->lock, flags);
6388c2ecf20Sopenharmony_ci	clrbits32(&gpt_wdt->regs->mode,
6398c2ecf20Sopenharmony_ci		  MPC52xx_GPT_MODE_COUNTER_ENABLE | MPC52xx_GPT_MODE_WDT_EN);
6408c2ecf20Sopenharmony_ci	gpt_wdt->wdt_mode &= ~MPC52xx_GPT_IS_WDT;
6418c2ecf20Sopenharmony_ci	raw_spin_unlock_irqrestore(&gpt_wdt->lock, flags);
6428c2ecf20Sopenharmony_ci#endif
6438c2ecf20Sopenharmony_ci	clear_bit(0, &wdt_is_active);
6448c2ecf20Sopenharmony_ci	return 0;
6458c2ecf20Sopenharmony_ci}
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_cistatic const struct file_operations mpc52xx_wdt_fops = {
6498c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
6508c2ecf20Sopenharmony_ci	.llseek		= no_llseek,
6518c2ecf20Sopenharmony_ci	.write		= mpc52xx_wdt_write,
6528c2ecf20Sopenharmony_ci	.unlocked_ioctl = mpc52xx_wdt_ioctl,
6538c2ecf20Sopenharmony_ci	.compat_ioctl	= compat_ptr_ioctl,
6548c2ecf20Sopenharmony_ci	.open		= mpc52xx_wdt_open,
6558c2ecf20Sopenharmony_ci	.release	= mpc52xx_wdt_release,
6568c2ecf20Sopenharmony_ci};
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_cistatic struct miscdevice mpc52xx_wdt_miscdev = {
6598c2ecf20Sopenharmony_ci	.minor		= WATCHDOG_MINOR,
6608c2ecf20Sopenharmony_ci	.name		= "watchdog",
6618c2ecf20Sopenharmony_ci	.fops		= &mpc52xx_wdt_fops,
6628c2ecf20Sopenharmony_ci};
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_cistatic int mpc52xx_gpt_wdt_init(void)
6658c2ecf20Sopenharmony_ci{
6668c2ecf20Sopenharmony_ci	int err;
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci	/* try to register the watchdog misc device */
6698c2ecf20Sopenharmony_ci	err = misc_register(&mpc52xx_wdt_miscdev);
6708c2ecf20Sopenharmony_ci	if (err)
6718c2ecf20Sopenharmony_ci		pr_err("%s: cannot register watchdog device\n", WDT_IDENTITY);
6728c2ecf20Sopenharmony_ci	else
6738c2ecf20Sopenharmony_ci		pr_info("%s: watchdog device registered\n", WDT_IDENTITY);
6748c2ecf20Sopenharmony_ci	return err;
6758c2ecf20Sopenharmony_ci}
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_cistatic int mpc52xx_gpt_wdt_setup(struct mpc52xx_gpt_priv *gpt,
6788c2ecf20Sopenharmony_ci				 const u32 *period)
6798c2ecf20Sopenharmony_ci{
6808c2ecf20Sopenharmony_ci	u64 real_timeout;
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ci	/* remember the gpt for the wdt operation */
6838c2ecf20Sopenharmony_ci	mpc52xx_gpt_wdt = gpt;
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_ci	/* configure the wdt if the device tree contained a timeout */
6868c2ecf20Sopenharmony_ci	if (!period || *period == 0)
6878c2ecf20Sopenharmony_ci		return 0;
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci	real_timeout = (u64) *period * 1000000000ULL;
6908c2ecf20Sopenharmony_ci	if (mpc52xx_gpt_do_start(gpt, real_timeout, 0, 1))
6918c2ecf20Sopenharmony_ci		dev_warn(gpt->dev, "starting as wdt failed\n");
6928c2ecf20Sopenharmony_ci	else
6938c2ecf20Sopenharmony_ci		dev_info(gpt->dev, "watchdog set to %us timeout\n", *period);
6948c2ecf20Sopenharmony_ci	return 0;
6958c2ecf20Sopenharmony_ci}
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci#else
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_cistatic int mpc52xx_gpt_wdt_init(void)
7008c2ecf20Sopenharmony_ci{
7018c2ecf20Sopenharmony_ci	return 0;
7028c2ecf20Sopenharmony_ci}
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_cistatic inline int mpc52xx_gpt_wdt_setup(struct mpc52xx_gpt_priv *gpt,
7058c2ecf20Sopenharmony_ci					const u32 *period)
7068c2ecf20Sopenharmony_ci{
7078c2ecf20Sopenharmony_ci	return 0;
7088c2ecf20Sopenharmony_ci}
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci#endif	/*  CONFIG_MPC5200_WDT	*/
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci/* ---------------------------------------------------------------------
7138c2ecf20Sopenharmony_ci * of_platform bus binding code
7148c2ecf20Sopenharmony_ci */
7158c2ecf20Sopenharmony_cistatic int mpc52xx_gpt_probe(struct platform_device *ofdev)
7168c2ecf20Sopenharmony_ci{
7178c2ecf20Sopenharmony_ci	struct mpc52xx_gpt_priv *gpt;
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	gpt = devm_kzalloc(&ofdev->dev, sizeof *gpt, GFP_KERNEL);
7208c2ecf20Sopenharmony_ci	if (!gpt)
7218c2ecf20Sopenharmony_ci		return -ENOMEM;
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	raw_spin_lock_init(&gpt->lock);
7248c2ecf20Sopenharmony_ci	gpt->dev = &ofdev->dev;
7258c2ecf20Sopenharmony_ci	gpt->ipb_freq = mpc5xxx_get_bus_frequency(ofdev->dev.of_node);
7268c2ecf20Sopenharmony_ci	gpt->regs = of_iomap(ofdev->dev.of_node, 0);
7278c2ecf20Sopenharmony_ci	if (!gpt->regs)
7288c2ecf20Sopenharmony_ci		return -ENOMEM;
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_ci	dev_set_drvdata(&ofdev->dev, gpt);
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_ci	mpc52xx_gpt_gpio_setup(gpt, ofdev->dev.of_node);
7338c2ecf20Sopenharmony_ci	mpc52xx_gpt_irq_setup(gpt, ofdev->dev.of_node);
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci	mutex_lock(&mpc52xx_gpt_list_mutex);
7368c2ecf20Sopenharmony_ci	list_add(&gpt->list, &mpc52xx_gpt_list);
7378c2ecf20Sopenharmony_ci	mutex_unlock(&mpc52xx_gpt_list_mutex);
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	/* check if this device could be a watchdog */
7408c2ecf20Sopenharmony_ci	if (of_get_property(ofdev->dev.of_node, "fsl,has-wdt", NULL) ||
7418c2ecf20Sopenharmony_ci	    of_get_property(ofdev->dev.of_node, "has-wdt", NULL)) {
7428c2ecf20Sopenharmony_ci		const u32 *on_boot_wdt;
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci		gpt->wdt_mode = MPC52xx_GPT_CAN_WDT;
7458c2ecf20Sopenharmony_ci		on_boot_wdt = of_get_property(ofdev->dev.of_node,
7468c2ecf20Sopenharmony_ci					      "fsl,wdt-on-boot", NULL);
7478c2ecf20Sopenharmony_ci		if (on_boot_wdt) {
7488c2ecf20Sopenharmony_ci			dev_info(gpt->dev, "used as watchdog\n");
7498c2ecf20Sopenharmony_ci			gpt->wdt_mode |= MPC52xx_GPT_IS_WDT;
7508c2ecf20Sopenharmony_ci		} else
7518c2ecf20Sopenharmony_ci			dev_info(gpt->dev, "can function as watchdog\n");
7528c2ecf20Sopenharmony_ci		mpc52xx_gpt_wdt_setup(gpt, on_boot_wdt);
7538c2ecf20Sopenharmony_ci	}
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ci	return 0;
7568c2ecf20Sopenharmony_ci}
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_cistatic int mpc52xx_gpt_remove(struct platform_device *ofdev)
7598c2ecf20Sopenharmony_ci{
7608c2ecf20Sopenharmony_ci	return -EBUSY;
7618c2ecf20Sopenharmony_ci}
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_cistatic const struct of_device_id mpc52xx_gpt_match[] = {
7648c2ecf20Sopenharmony_ci	{ .compatible = "fsl,mpc5200-gpt", },
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci	/* Depreciated compatible values; don't use for new dts files */
7678c2ecf20Sopenharmony_ci	{ .compatible = "fsl,mpc5200-gpt-gpio", },
7688c2ecf20Sopenharmony_ci	{ .compatible = "mpc5200-gpt", },
7698c2ecf20Sopenharmony_ci	{}
7708c2ecf20Sopenharmony_ci};
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_cistatic struct platform_driver mpc52xx_gpt_driver = {
7738c2ecf20Sopenharmony_ci	.driver = {
7748c2ecf20Sopenharmony_ci		.name = "mpc52xx-gpt",
7758c2ecf20Sopenharmony_ci		.of_match_table = mpc52xx_gpt_match,
7768c2ecf20Sopenharmony_ci	},
7778c2ecf20Sopenharmony_ci	.probe = mpc52xx_gpt_probe,
7788c2ecf20Sopenharmony_ci	.remove = mpc52xx_gpt_remove,
7798c2ecf20Sopenharmony_ci};
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_cistatic int __init mpc52xx_gpt_init(void)
7828c2ecf20Sopenharmony_ci{
7838c2ecf20Sopenharmony_ci	return platform_driver_register(&mpc52xx_gpt_driver);
7848c2ecf20Sopenharmony_ci}
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci/* Make sure GPIOs and IRQs get set up before anyone tries to use them */
7878c2ecf20Sopenharmony_cisubsys_initcall(mpc52xx_gpt_init);
7888c2ecf20Sopenharmony_cidevice_initcall(mpc52xx_gpt_wdt_init);
789