mpc52xx_gpt.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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 int mpc52xx_gpt_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  238. {
  239. struct mpc52xx_gpt_priv *gpt = gpiochip_get_data(gc);
  240. return (in_be32(&gpt->regs->status) >> 8) & 1;
  241. }
  242. static void
  243. mpc52xx_gpt_gpio_set(struct gpio_chip *gc, unsigned int gpio, int v)
  244. {
  245. struct mpc52xx_gpt_priv *gpt = gpiochip_get_data(gc);
  246. unsigned long flags;
  247. u32 r;
  248. dev_dbg(gpt->dev, "%s: gpio:%d v:%d\n", __func__, gpio, v);
  249. r = v ? MPC52xx_GPT_MODE_GPIO_OUT_HIGH : MPC52xx_GPT_MODE_GPIO_OUT_LOW;
  250. spin_lock_irqsave(&gpt->lock, flags);
  251. clrsetbits_be32(&gpt->regs->mode, MPC52xx_GPT_MODE_GPIO_MASK, r);
  252. spin_unlock_irqrestore(&gpt->lock, flags);
  253. }
  254. static int mpc52xx_gpt_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  255. {
  256. struct mpc52xx_gpt_priv *gpt = gpiochip_get_data(gc);
  257. unsigned long flags;
  258. dev_dbg(gpt->dev, "%s: gpio:%d\n", __func__, gpio);
  259. spin_lock_irqsave(&gpt->lock, flags);
  260. clrbits32(&gpt->regs->mode, MPC52xx_GPT_MODE_GPIO_MASK);
  261. spin_unlock_irqrestore(&gpt->lock, flags);
  262. return 0;
  263. }
  264. static int
  265. mpc52xx_gpt_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  266. {
  267. mpc52xx_gpt_gpio_set(gc, gpio, val);
  268. return 0;
  269. }
  270. static void
  271. mpc52xx_gpt_gpio_setup(struct mpc52xx_gpt_priv *gpt, struct device_node *node)
  272. {
  273. int rc;
  274. /* Only setup GPIO if the device tree claims the GPT is
  275. * a GPIO controller */
  276. if (!of_find_property(node, "gpio-controller", NULL))
  277. return;
  278. gpt->gc.label = kstrdup(node->full_name, GFP_KERNEL);
  279. if (!gpt->gc.label) {
  280. dev_err(gpt->dev, "out of memory\n");
  281. return;
  282. }
  283. gpt->gc.ngpio = 1;
  284. gpt->gc.direction_input = mpc52xx_gpt_gpio_dir_in;
  285. gpt->gc.direction_output = mpc52xx_gpt_gpio_dir_out;
  286. gpt->gc.get = mpc52xx_gpt_gpio_get;
  287. gpt->gc.set = mpc52xx_gpt_gpio_set;
  288. gpt->gc.base = -1;
  289. gpt->gc.of_node = node;
  290. /* Setup external pin in GPIO mode */
  291. clrsetbits_be32(&gpt->regs->mode, MPC52xx_GPT_MODE_MS_MASK,
  292. MPC52xx_GPT_MODE_MS_GPIO);
  293. rc = gpiochip_add_data(&gpt->gc, gpt);
  294. if (rc)
  295. dev_err(gpt->dev, "gpiochip_add_data() failed; rc=%i\n", rc);
  296. dev_dbg(gpt->dev, "%s() complete.\n", __func__);
  297. }
  298. #else /* defined(CONFIG_GPIOLIB) */
  299. static void
  300. mpc52xx_gpt_gpio_setup(struct mpc52xx_gpt_priv *p, struct device_node *np) { }
  301. #endif /* defined(CONFIG_GPIOLIB) */
  302. /***********************************************************************
  303. * Timer API
  304. */
  305. /**
  306. * mpc52xx_gpt_from_irq - Return the GPT device associated with an IRQ number
  307. * @irq: irq of timer.
  308. */
  309. struct mpc52xx_gpt_priv *mpc52xx_gpt_from_irq(int irq)
  310. {
  311. struct mpc52xx_gpt_priv *gpt;
  312. struct list_head *pos;
  313. /* Iterate over the list of timers looking for a matching device */
  314. mutex_lock(&mpc52xx_gpt_list_mutex);
  315. list_for_each(pos, &mpc52xx_gpt_list) {
  316. gpt = container_of(pos, struct mpc52xx_gpt_priv, list);
  317. if (gpt->irqhost && irq == irq_linear_revmap(gpt->irqhost, 0)) {
  318. mutex_unlock(&mpc52xx_gpt_list_mutex);
  319. return gpt;
  320. }
  321. }
  322. mutex_unlock(&mpc52xx_gpt_list_mutex);
  323. return NULL;
  324. }
  325. EXPORT_SYMBOL(mpc52xx_gpt_from_irq);
  326. static int mpc52xx_gpt_do_start(struct mpc52xx_gpt_priv *gpt, u64 period,
  327. int continuous, int as_wdt)
  328. {
  329. u32 clear, set;
  330. u64 clocks;
  331. u32 prescale;
  332. unsigned long flags;
  333. clear = MPC52xx_GPT_MODE_MS_MASK | MPC52xx_GPT_MODE_CONTINUOUS;
  334. set = MPC52xx_GPT_MODE_MS_GPIO | MPC52xx_GPT_MODE_COUNTER_ENABLE;
  335. if (as_wdt) {
  336. clear |= MPC52xx_GPT_MODE_IRQ_EN;
  337. set |= MPC52xx_GPT_MODE_WDT_EN;
  338. } else if (continuous)
  339. set |= MPC52xx_GPT_MODE_CONTINUOUS;
  340. /* Determine the number of clocks in the requested period. 64 bit
  341. * arithmatic is done here to preserve the precision until the value
  342. * is scaled back down into the u32 range. Period is in 'ns', bus
  343. * frequency is in Hz. */
  344. clocks = period * (u64)gpt->ipb_freq;
  345. do_div(clocks, 1000000000); /* Scale it down to ns range */
  346. /* This device cannot handle a clock count greater than 32 bits */
  347. if (clocks > 0xffffffff)
  348. return -EINVAL;
  349. /* Calculate the prescaler and count values from the clocks value.
  350. * 'clocks' is the number of clock ticks in the period. The timer
  351. * has 16 bit precision and a 16 bit prescaler. Prescaler is
  352. * calculated by integer dividing the clocks by 0x10000 (shifting
  353. * down 16 bits) to obtain the smallest possible divisor for clocks
  354. * to get a 16 bit count value.
  355. *
  356. * Note: the prescale register is '1' based, not '0' based. ie. a
  357. * value of '1' means divide the clock by one. 0xffff divides the
  358. * clock by 0xffff. '0x0000' does not divide by zero, but wraps
  359. * around and divides by 0x10000. That is why prescale must be
  360. * a u32 variable, not a u16, for this calculation. */
  361. prescale = (clocks >> 16) + 1;
  362. do_div(clocks, prescale);
  363. if (clocks > 0xffff) {
  364. pr_err("calculation error; prescale:%x clocks:%llx\n",
  365. prescale, clocks);
  366. return -EINVAL;
  367. }
  368. /* Set and enable the timer, reject an attempt to use a wdt as gpt */
  369. spin_lock_irqsave(&gpt->lock, flags);
  370. if (as_wdt)
  371. gpt->wdt_mode |= MPC52xx_GPT_IS_WDT;
  372. else if ((gpt->wdt_mode & MPC52xx_GPT_IS_WDT) != 0) {
  373. spin_unlock_irqrestore(&gpt->lock, flags);
  374. return -EBUSY;
  375. }
  376. out_be32(&gpt->regs->count, prescale << 16 | clocks);
  377. clrsetbits_be32(&gpt->regs->mode, clear, set);
  378. spin_unlock_irqrestore(&gpt->lock, flags);
  379. return 0;
  380. }
  381. /**
  382. * mpc52xx_gpt_start_timer - Set and enable the GPT timer
  383. * @gpt: Pointer to gpt private data structure
  384. * @period: period of timer in ns; max. ~130s @ 33MHz IPB clock
  385. * @continuous: set to 1 to make timer continuous free running
  386. *
  387. * An interrupt will be generated every time the timer fires
  388. */
  389. int mpc52xx_gpt_start_timer(struct mpc52xx_gpt_priv *gpt, u64 period,
  390. int continuous)
  391. {
  392. return mpc52xx_gpt_do_start(gpt, period, continuous, 0);
  393. }
  394. EXPORT_SYMBOL(mpc52xx_gpt_start_timer);
  395. /**
  396. * mpc52xx_gpt_stop_timer - Stop a gpt
  397. * @gpt: Pointer to gpt private data structure
  398. *
  399. * Returns an error if attempting to stop a wdt
  400. */
  401. int mpc52xx_gpt_stop_timer(struct mpc52xx_gpt_priv *gpt)
  402. {
  403. unsigned long flags;
  404. /* reject the operation if the timer is used as watchdog (gpt 0 only) */
  405. spin_lock_irqsave(&gpt->lock, flags);
  406. if ((gpt->wdt_mode & MPC52xx_GPT_IS_WDT) != 0) {
  407. spin_unlock_irqrestore(&gpt->lock, flags);
  408. return -EBUSY;
  409. }
  410. clrbits32(&gpt->regs->mode, MPC52xx_GPT_MODE_COUNTER_ENABLE);
  411. spin_unlock_irqrestore(&gpt->lock, flags);
  412. return 0;
  413. }
  414. EXPORT_SYMBOL(mpc52xx_gpt_stop_timer);
  415. /**
  416. * mpc52xx_gpt_timer_period - Read the timer period
  417. * @gpt: Pointer to gpt private data structure
  418. *
  419. * Returns the timer period in ns
  420. */
  421. u64 mpc52xx_gpt_timer_period(struct mpc52xx_gpt_priv *gpt)
  422. {
  423. u64 period;
  424. u64 prescale;
  425. unsigned long flags;
  426. spin_lock_irqsave(&gpt->lock, flags);
  427. period = in_be32(&gpt->regs->count);
  428. spin_unlock_irqrestore(&gpt->lock, flags);
  429. prescale = period >> 16;
  430. period &= 0xffff;
  431. if (prescale == 0)
  432. prescale = 0x10000;
  433. period = period * prescale * 1000000000ULL;
  434. do_div(period, (u64)gpt->ipb_freq);
  435. return period;
  436. }
  437. EXPORT_SYMBOL(mpc52xx_gpt_timer_period);
  438. #if defined(CONFIG_MPC5200_WDT)
  439. /***********************************************************************
  440. * Watchdog API for gpt0
  441. */
  442. #define WDT_IDENTITY "mpc52xx watchdog on GPT0"
  443. /* wdt_is_active stores whether or not the /dev/watchdog device is opened */
  444. static unsigned long wdt_is_active;
  445. /* wdt-capable gpt */
  446. static struct mpc52xx_gpt_priv *mpc52xx_gpt_wdt;
  447. /* low-level wdt functions */
  448. static inline void mpc52xx_gpt_wdt_ping(struct mpc52xx_gpt_priv *gpt_wdt)
  449. {
  450. unsigned long flags;
  451. spin_lock_irqsave(&gpt_wdt->lock, flags);
  452. out_8((u8 *) &gpt_wdt->regs->mode, MPC52xx_GPT_MODE_WDT_PING);
  453. spin_unlock_irqrestore(&gpt_wdt->lock, flags);
  454. }
  455. /* wdt misc device api */
  456. static ssize_t mpc52xx_wdt_write(struct file *file, const char __user *data,
  457. size_t len, loff_t *ppos)
  458. {
  459. struct mpc52xx_gpt_priv *gpt_wdt = file->private_data;
  460. mpc52xx_gpt_wdt_ping(gpt_wdt);
  461. return 0;
  462. }
  463. static const struct watchdog_info mpc5200_wdt_info = {
  464. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  465. .identity = WDT_IDENTITY,
  466. };
  467. static long mpc52xx_wdt_ioctl(struct file *file, unsigned int cmd,
  468. unsigned long arg)
  469. {
  470. struct mpc52xx_gpt_priv *gpt_wdt = file->private_data;
  471. int __user *data = (int __user *)arg;
  472. int timeout;
  473. u64 real_timeout;
  474. int ret = 0;
  475. switch (cmd) {
  476. case WDIOC_GETSUPPORT:
  477. ret = copy_to_user(data, &mpc5200_wdt_info,
  478. sizeof(mpc5200_wdt_info));
  479. if (ret)
  480. ret = -EFAULT;
  481. break;
  482. case WDIOC_GETSTATUS:
  483. case WDIOC_GETBOOTSTATUS:
  484. ret = put_user(0, data);
  485. break;
  486. case WDIOC_KEEPALIVE:
  487. mpc52xx_gpt_wdt_ping(gpt_wdt);
  488. break;
  489. case WDIOC_SETTIMEOUT:
  490. ret = get_user(timeout, data);
  491. if (ret)
  492. break;
  493. real_timeout = (u64) timeout * 1000000000ULL;
  494. ret = mpc52xx_gpt_do_start(gpt_wdt, real_timeout, 0, 1);
  495. if (ret)
  496. break;
  497. /* fall through and return the timeout */
  498. case WDIOC_GETTIMEOUT:
  499. /* we need to round here as to avoid e.g. the following
  500. * situation:
  501. * - timeout requested is 1 second;
  502. * - real timeout @33MHz is 999997090ns
  503. * - the int divide by 10^9 will return 0.
  504. */
  505. real_timeout =
  506. mpc52xx_gpt_timer_period(gpt_wdt) + 500000000ULL;
  507. do_div(real_timeout, 1000000000ULL);
  508. timeout = (int) real_timeout;
  509. ret = put_user(timeout, data);
  510. break;
  511. default:
  512. ret = -ENOTTY;
  513. }
  514. return ret;
  515. }
  516. static int mpc52xx_wdt_open(struct inode *inode, struct file *file)
  517. {
  518. int ret;
  519. /* sanity check */
  520. if (!mpc52xx_gpt_wdt)
  521. return -ENODEV;
  522. /* /dev/watchdog can only be opened once */
  523. if (test_and_set_bit(0, &wdt_is_active))
  524. return -EBUSY;
  525. /* Set and activate the watchdog with 30 seconds timeout */
  526. ret = mpc52xx_gpt_do_start(mpc52xx_gpt_wdt, 30ULL * 1000000000ULL,
  527. 0, 1);
  528. if (ret) {
  529. clear_bit(0, &wdt_is_active);
  530. return ret;
  531. }
  532. file->private_data = mpc52xx_gpt_wdt;
  533. return nonseekable_open(inode, file);
  534. }
  535. static int mpc52xx_wdt_release(struct inode *inode, struct file *file)
  536. {
  537. /* note: releasing the wdt in NOWAYOUT-mode does not stop it */
  538. #if !defined(CONFIG_WATCHDOG_NOWAYOUT)
  539. struct mpc52xx_gpt_priv *gpt_wdt = file->private_data;
  540. unsigned long flags;
  541. spin_lock_irqsave(&gpt_wdt->lock, flags);
  542. clrbits32(&gpt_wdt->regs->mode,
  543. MPC52xx_GPT_MODE_COUNTER_ENABLE | MPC52xx_GPT_MODE_WDT_EN);
  544. gpt_wdt->wdt_mode &= ~MPC52xx_GPT_IS_WDT;
  545. spin_unlock_irqrestore(&gpt_wdt->lock, flags);
  546. #endif
  547. clear_bit(0, &wdt_is_active);
  548. return 0;
  549. }
  550. static const struct file_operations mpc52xx_wdt_fops = {
  551. .owner = THIS_MODULE,
  552. .llseek = no_llseek,
  553. .write = mpc52xx_wdt_write,
  554. .unlocked_ioctl = mpc52xx_wdt_ioctl,
  555. .open = mpc52xx_wdt_open,
  556. .release = mpc52xx_wdt_release,
  557. };
  558. static struct miscdevice mpc52xx_wdt_miscdev = {
  559. .minor = WATCHDOG_MINOR,
  560. .name = "watchdog",
  561. .fops = &mpc52xx_wdt_fops,
  562. };
  563. static int mpc52xx_gpt_wdt_init(void)
  564. {
  565. int err;
  566. /* try to register the watchdog misc device */
  567. err = misc_register(&mpc52xx_wdt_miscdev);
  568. if (err)
  569. pr_err("%s: cannot register watchdog device\n", WDT_IDENTITY);
  570. else
  571. pr_info("%s: watchdog device registered\n", WDT_IDENTITY);
  572. return err;
  573. }
  574. static int mpc52xx_gpt_wdt_setup(struct mpc52xx_gpt_priv *gpt,
  575. const u32 *period)
  576. {
  577. u64 real_timeout;
  578. /* remember the gpt for the wdt operation */
  579. mpc52xx_gpt_wdt = gpt;
  580. /* configure the wdt if the device tree contained a timeout */
  581. if (!period || *period == 0)
  582. return 0;
  583. real_timeout = (u64) *period * 1000000000ULL;
  584. if (mpc52xx_gpt_do_start(gpt, real_timeout, 0, 1))
  585. dev_warn(gpt->dev, "starting as wdt failed\n");
  586. else
  587. dev_info(gpt->dev, "watchdog set to %us timeout\n", *period);
  588. return 0;
  589. }
  590. #else
  591. static int mpc52xx_gpt_wdt_init(void)
  592. {
  593. return 0;
  594. }
  595. static inline int mpc52xx_gpt_wdt_setup(struct mpc52xx_gpt_priv *gpt,
  596. const u32 *period)
  597. {
  598. return 0;
  599. }
  600. #endif /* CONFIG_MPC5200_WDT */
  601. /* ---------------------------------------------------------------------
  602. * of_platform bus binding code
  603. */
  604. static int mpc52xx_gpt_probe(struct platform_device *ofdev)
  605. {
  606. struct mpc52xx_gpt_priv *gpt;
  607. gpt = devm_kzalloc(&ofdev->dev, sizeof *gpt, GFP_KERNEL);
  608. if (!gpt)
  609. return -ENOMEM;
  610. spin_lock_init(&gpt->lock);
  611. gpt->dev = &ofdev->dev;
  612. gpt->ipb_freq = mpc5xxx_get_bus_frequency(ofdev->dev.of_node);
  613. gpt->regs = of_iomap(ofdev->dev.of_node, 0);
  614. if (!gpt->regs)
  615. return -ENOMEM;
  616. dev_set_drvdata(&ofdev->dev, gpt);
  617. mpc52xx_gpt_gpio_setup(gpt, ofdev->dev.of_node);
  618. mpc52xx_gpt_irq_setup(gpt, ofdev->dev.of_node);
  619. mutex_lock(&mpc52xx_gpt_list_mutex);
  620. list_add(&gpt->list, &mpc52xx_gpt_list);
  621. mutex_unlock(&mpc52xx_gpt_list_mutex);
  622. /* check if this device could be a watchdog */
  623. if (of_get_property(ofdev->dev.of_node, "fsl,has-wdt", NULL) ||
  624. of_get_property(ofdev->dev.of_node, "has-wdt", NULL)) {
  625. const u32 *on_boot_wdt;
  626. gpt->wdt_mode = MPC52xx_GPT_CAN_WDT;
  627. on_boot_wdt = of_get_property(ofdev->dev.of_node,
  628. "fsl,wdt-on-boot", NULL);
  629. if (on_boot_wdt) {
  630. dev_info(gpt->dev, "used as watchdog\n");
  631. gpt->wdt_mode |= MPC52xx_GPT_IS_WDT;
  632. } else
  633. dev_info(gpt->dev, "can function as watchdog\n");
  634. mpc52xx_gpt_wdt_setup(gpt, on_boot_wdt);
  635. }
  636. return 0;
  637. }
  638. static int mpc52xx_gpt_remove(struct platform_device *ofdev)
  639. {
  640. return -EBUSY;
  641. }
  642. static const struct of_device_id mpc52xx_gpt_match[] = {
  643. { .compatible = "fsl,mpc5200-gpt", },
  644. /* Depreciated compatible values; don't use for new dts files */
  645. { .compatible = "fsl,mpc5200-gpt-gpio", },
  646. { .compatible = "mpc5200-gpt", },
  647. {}
  648. };
  649. static struct platform_driver mpc52xx_gpt_driver = {
  650. .driver = {
  651. .name = "mpc52xx-gpt",
  652. .of_match_table = mpc52xx_gpt_match,
  653. },
  654. .probe = mpc52xx_gpt_probe,
  655. .remove = mpc52xx_gpt_remove,
  656. };
  657. static int __init mpc52xx_gpt_init(void)
  658. {
  659. return platform_driver_register(&mpc52xx_gpt_driver);
  660. }
  661. /* Make sure GPIOs and IRQs get set up before anyone tries to use them */
  662. subsys_initcall(mpc52xx_gpt_init);
  663. device_initcall(mpc52xx_gpt_wdt_init);