55 #include <linux/device.h>
59 #include <linux/list.h>
64 #include <linux/kernel.h>
65 #include <linux/slab.h>
67 #include <linux/watchdog.h>
70 #include <linux/module.h>
71 #include <asm/div64.h>
98 #if defined(CONFIG_GPIOLIB)
106 #define MPC52xx_GPT_MODE_MS_MASK (0x07)
107 #define MPC52xx_GPT_MODE_MS_IC (0x01)
108 #define MPC52xx_GPT_MODE_MS_OC (0x02)
109 #define MPC52xx_GPT_MODE_MS_PWM (0x03)
110 #define MPC52xx_GPT_MODE_MS_GPIO (0x04)
112 #define MPC52xx_GPT_MODE_GPIO_MASK (0x30)
113 #define MPC52xx_GPT_MODE_GPIO_OUT_LOW (0x20)
114 #define MPC52xx_GPT_MODE_GPIO_OUT_HIGH (0x30)
116 #define MPC52xx_GPT_MODE_COUNTER_ENABLE (0x1000)
117 #define MPC52xx_GPT_MODE_CONTINUOUS (0x0400)
118 #define MPC52xx_GPT_MODE_OPEN_DRAIN (0x0200)
119 #define MPC52xx_GPT_MODE_IRQ_EN (0x0100)
120 #define MPC52xx_GPT_MODE_WDT_EN (0x8000)
122 #define MPC52xx_GPT_MODE_ICT_MASK (0x030000)
123 #define MPC52xx_GPT_MODE_ICT_RISING (0x010000)
124 #define MPC52xx_GPT_MODE_ICT_FALLING (0x020000)
125 #define MPC52xx_GPT_MODE_ICT_TOGGLE (0x030000)
127 #define MPC52xx_GPT_MODE_WDT_PING (0xa5)
129 #define MPC52xx_GPT_STATUS_IRQMASK (0x000f)
131 #define MPC52xx_GPT_CAN_WDT (1 << 0)
132 #define MPC52xx_GPT_IS_WDT (1 << 1)
139 static void mpc52xx_gpt_irq_unmask(
struct irq_data *
d)
146 spin_unlock_irqrestore(&gpt->
lock, flags);
149 static void mpc52xx_gpt_irq_mask(
struct irq_data *
d)
156 spin_unlock_irqrestore(&gpt->
lock, flags);
159 static void mpc52xx_gpt_irq_ack(
struct irq_data *
d)
166 static int mpc52xx_gpt_irq_set_type(
struct irq_data *
d,
unsigned int flow_type)
172 dev_dbg(gpt->
dev,
"%s: virq=%i type=%x\n", __func__, d->
irq, flow_type);
181 spin_unlock_irqrestore(&gpt->
lock, flags);
186 static struct irq_chip mpc52xx_gpt_irq_chip = {
187 .name =
"MPC52xx GPT",
188 .irq_unmask = mpc52xx_gpt_irq_unmask,
189 .irq_mask = mpc52xx_gpt_irq_mask,
190 .irq_ack = mpc52xx_gpt_irq_ack,
191 .irq_set_type = mpc52xx_gpt_irq_set_type,
207 static int mpc52xx_gpt_irq_map(
struct irq_domain *
h,
unsigned int virq,
212 dev_dbg(gpt->
dev,
"%s: h=%p, virq=%i\n", __func__, h, virq);
214 irq_set_chip_and_handler(virq, &mpc52xx_gpt_irq_chip,
handle_edge_irq);
220 const u32 *intspec,
unsigned int intsize,
222 unsigned int *out_flags)
226 dev_dbg(gpt->
dev,
"%s: flags=%i\n", __func__, intspec[0]);
228 if ((intsize < 1) || (intspec[0] > 3)) {
234 *out_flags = intspec[0];
240 .map = mpc52xx_gpt_irq_map,
241 .xlate = mpc52xx_gpt_irq_xlate,
257 dev_err(gpt->
dev,
"irq_domain_add_linear() failed\n");
271 spin_unlock_irqrestore(&gpt->
lock, flags);
273 dev_dbg(gpt->
dev,
"%s() complete. virq=%i\n", __func__, cascade_virq);
280 #if defined(CONFIG_GPIOLIB)
286 static int mpc52xx_gpt_gpio_get(
struct gpio_chip *
gc,
unsigned int gpio)
294 mpc52xx_gpt_gpio_set(
struct gpio_chip *gc,
unsigned int gpio,
int v)
300 dev_dbg(gpt->
dev,
"%s: gpio:%d v:%d\n", __func__, gpio, v);
305 spin_unlock_irqrestore(&gpt->
lock, flags);
308 static int mpc52xx_gpt_gpio_dir_in(
struct gpio_chip *gc,
unsigned int gpio)
313 dev_dbg(gpt->
dev,
"%s: gpio:%d\n", __func__, gpio);
317 spin_unlock_irqrestore(&gpt->
lock, flags);
323 mpc52xx_gpt_gpio_dir_out(
struct gpio_chip *gc,
unsigned int gpio,
int val)
325 mpc52xx_gpt_gpio_set(gc, gpio, val);
340 if (!gpt->gc.label) {
346 gpt->gc.direction_input = mpc52xx_gpt_gpio_dir_in;
347 gpt->gc.direction_output = mpc52xx_gpt_gpio_dir_out;
348 gpt->gc.get = mpc52xx_gpt_gpio_get;
349 gpt->gc.set = mpc52xx_gpt_gpio_set;
351 gpt->gc.of_node =
node;
359 dev_err(gpt->
dev,
"gpiochip_add() failed; rc=%i\n", rc);
361 dev_dbg(gpt->
dev,
"%s() complete.\n", __func__);
409 }
else if (continuous)
417 do_div(clocks, 1000000000);
420 if (clocks > 0xffffffff)
435 prescale = (clocks >> 16) + 1;
437 if (clocks > 0xffff) {
438 pr_err(
"calculation error; prescale:%x clocks:%llx\n",
448 spin_unlock_irqrestore(&gpt->
lock, flags);
452 clrsetbits_be32(&gpt->
regs->mode, clear,
set);
453 spin_unlock_irqrestore(&gpt->
lock, flags);
469 return mpc52xx_gpt_do_start(gpt, period, continuous, 0);
486 spin_unlock_irqrestore(&gpt->
lock, flags);
491 spin_unlock_irqrestore(&gpt->
lock, flags);
510 spin_unlock_irqrestore(&gpt->
lock, flags);
512 prescale = period >> 16;
516 period = period * prescale * 1000000000ULL;
522 #if defined(CONFIG_MPC5200_WDT)
527 #define WDT_IDENTITY "mpc52xx watchdog on GPT0"
530 static unsigned long wdt_is_active;
542 spin_unlock_irqrestore(&gpt_wdt->
lock, flags);
547 size_t len, loff_t *ppos)
550 mpc52xx_gpt_wdt_ping(gpt_wdt);
556 .identity = WDT_IDENTITY,
559 static long mpc52xx_wdt_ioctl(
struct file *
file,
unsigned int cmd,
571 sizeof(mpc5200_wdt_info));
582 mpc52xx_gpt_wdt_ping(gpt_wdt);
589 real_timeout = (
u64) timeout * 1000000000ULL;
590 ret = mpc52xx_gpt_do_start(gpt_wdt, real_timeout, 0, 1);
604 do_div(real_timeout, 1000000000ULL);
605 timeout = (
int) real_timeout;
615 static int mpc52xx_wdt_open(
struct inode *
inode,
struct file *file)
620 if (!mpc52xx_gpt_wdt)
628 ret = mpc52xx_gpt_do_start(mpc52xx_gpt_wdt, 30ULL * 1000000000ULL,
639 static int mpc52xx_wdt_release(
struct inode *inode,
struct file *file)
642 #if !defined(CONFIG_WATCHDOG_NOWAYOUT)
647 clrbits32(&gpt_wdt->
regs->mode,
650 spin_unlock_irqrestore(&gpt_wdt->
lock, flags);
660 .write = mpc52xx_wdt_write,
661 .unlocked_ioctl = mpc52xx_wdt_ioctl,
662 .open = mpc52xx_wdt_open,
663 .release = mpc52xx_wdt_release,
666 static struct miscdevice mpc52xx_wdt_miscdev = {
669 .fops = &mpc52xx_wdt_fops,
672 static int __devinit mpc52xx_gpt_wdt_init(
void)
679 pr_err(
"%s: cannot register watchdog device\n", WDT_IDENTITY);
681 pr_info(
"%s: watchdog device registered\n", WDT_IDENTITY);
691 mpc52xx_gpt_wdt = gpt;
694 if (!period || *period == 0)
697 real_timeout = (
u64) *period * 1000000000ULL;
698 if (mpc52xx_gpt_do_start(gpt, real_timeout, 0, 1))
701 dev_info(gpt->
dev,
"watchdog set to %us timeout\n", *period);
707 static int __devinit mpc52xx_gpt_wdt_init(
void)
742 mpc52xx_gpt_gpio_setup(gpt, ofdev->
dev.of_node);
743 mpc52xx_gpt_irq_setup(gpt, ofdev->
dev.of_node);
746 list_add(&gpt->
list, &mpc52xx_gpt_list);
752 const u32 *on_boot_wdt;
756 "fsl,wdt-on-boot",
NULL);
762 mpc52xx_gpt_wdt_setup(gpt, on_boot_wdt);
773 static const struct of_device_id mpc52xx_gpt_match[] = {
774 { .compatible =
"fsl,mpc5200-gpt", },
777 { .compatible =
"fsl,mpc5200-gpt-gpio", },
778 { .compatible =
"mpc5200-gpt", },
784 .name =
"mpc52xx-gpt",
786 .of_match_table = mpc52xx_gpt_match,
788 .probe = mpc52xx_gpt_probe,
789 .remove = mpc52xx_gpt_remove,
792 static int __init mpc52xx_gpt_init(
void)