mpc52xx_gpt.c 23 KB

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