mpc52xx_gpt.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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/watchdog.h>
  66. #include <linux/miscdevice.h>
  67. #include <linux/uaccess.h>
  68. #include <asm/div64.h>
  69. #include <asm/mpc52xx.h>
  70. MODULE_DESCRIPTION("Freescale MPC52xx gpt driver");
  71. MODULE_AUTHOR("Sascha Hauer, Grant Likely, Albrecht Dreß");
  72. MODULE_LICENSE("GPL");
  73. /**
  74. * struct mpc52xx_gpt - Private data structure for MPC52xx GPT driver
  75. * @dev: pointer to device structure
  76. * @regs: virtual address of GPT registers
  77. * @lock: spinlock to coordinate between different functions.
  78. * @of_gc: of_gpio_chip instance structure; used when GPIO is enabled
  79. * @irqhost: Pointer to irq_host instance; used when IRQ mode is supported
  80. * @wdt_mode: only relevant for gpt0: bit 0 (MPC52xx_GPT_CAN_WDT) indicates
  81. * if the gpt may be used as wdt, bit 1 (MPC52xx_GPT_IS_WDT) indicates
  82. * if the timer is actively used as wdt which blocks gpt functions
  83. */
  84. struct mpc52xx_gpt_priv {
  85. struct list_head list; /* List of all GPT devices */
  86. struct device *dev;
  87. struct mpc52xx_gpt __iomem *regs;
  88. spinlock_t lock;
  89. struct irq_host *irqhost;
  90. u32 ipb_freq;
  91. u8 wdt_mode;
  92. #if defined(CONFIG_GPIOLIB)
  93. struct of_gpio_chip of_gc;
  94. #endif
  95. };
  96. LIST_HEAD(mpc52xx_gpt_list);
  97. DEFINE_MUTEX(mpc52xx_gpt_list_mutex);
  98. #define MPC52xx_GPT_MODE_MS_MASK (0x07)
  99. #define MPC52xx_GPT_MODE_MS_IC (0x01)
  100. #define MPC52xx_GPT_MODE_MS_OC (0x02)
  101. #define MPC52xx_GPT_MODE_MS_PWM (0x03)
  102. #define MPC52xx_GPT_MODE_MS_GPIO (0x04)
  103. #define MPC52xx_GPT_MODE_GPIO_MASK (0x30)
  104. #define MPC52xx_GPT_MODE_GPIO_OUT_LOW (0x20)
  105. #define MPC52xx_GPT_MODE_GPIO_OUT_HIGH (0x30)
  106. #define MPC52xx_GPT_MODE_COUNTER_ENABLE (0x1000)
  107. #define MPC52xx_GPT_MODE_CONTINUOUS (0x0400)
  108. #define MPC52xx_GPT_MODE_OPEN_DRAIN (0x0200)
  109. #define MPC52xx_GPT_MODE_IRQ_EN (0x0100)
  110. #define MPC52xx_GPT_MODE_WDT_EN (0x8000)
  111. #define MPC52xx_GPT_MODE_ICT_MASK (0x030000)
  112. #define MPC52xx_GPT_MODE_ICT_RISING (0x010000)
  113. #define MPC52xx_GPT_MODE_ICT_FALLING (0x020000)
  114. #define MPC52xx_GPT_MODE_ICT_TOGGLE (0x030000)
  115. #define MPC52xx_GPT_MODE_WDT_PING (0xa5)
  116. #define MPC52xx_GPT_STATUS_IRQMASK (0x000f)
  117. #define MPC52xx_GPT_CAN_WDT (1 << 0)
  118. #define MPC52xx_GPT_IS_WDT (1 << 1)
  119. /* ---------------------------------------------------------------------
  120. * Cascaded interrupt controller hooks
  121. */
  122. static void mpc52xx_gpt_irq_unmask(unsigned int virq)
  123. {
  124. struct mpc52xx_gpt_priv *gpt = get_irq_chip_data(virq);
  125. unsigned long flags;
  126. spin_lock_irqsave(&gpt->lock, flags);
  127. setbits32(&gpt->regs->mode, MPC52xx_GPT_MODE_IRQ_EN);
  128. spin_unlock_irqrestore(&gpt->lock, flags);
  129. }
  130. static void mpc52xx_gpt_irq_mask(unsigned int virq)
  131. {
  132. struct mpc52xx_gpt_priv *gpt = get_irq_chip_data(virq);
  133. unsigned long flags;
  134. spin_lock_irqsave(&gpt->lock, flags);
  135. clrbits32(&gpt->regs->mode, MPC52xx_GPT_MODE_IRQ_EN);
  136. spin_unlock_irqrestore(&gpt->lock, flags);
  137. }
  138. static void mpc52xx_gpt_irq_ack(unsigned int virq)
  139. {
  140. struct mpc52xx_gpt_priv *gpt = get_irq_chip_data(virq);
  141. out_be32(&gpt->regs->status, MPC52xx_GPT_STATUS_IRQMASK);
  142. }
  143. static int mpc52xx_gpt_irq_set_type(unsigned int virq, unsigned int flow_type)
  144. {
  145. struct mpc52xx_gpt_priv *gpt = get_irq_chip_data(virq);
  146. unsigned long flags;
  147. u32 reg;
  148. dev_dbg(gpt->dev, "%s: virq=%i type=%x\n", __func__, virq, flow_type);
  149. spin_lock_irqsave(&gpt->lock, flags);
  150. reg = in_be32(&gpt->regs->mode) & ~MPC52xx_GPT_MODE_ICT_MASK;
  151. if (flow_type & IRQF_TRIGGER_RISING)
  152. reg |= MPC52xx_GPT_MODE_ICT_RISING;
  153. if (flow_type & IRQF_TRIGGER_FALLING)
  154. reg |= MPC52xx_GPT_MODE_ICT_FALLING;
  155. out_be32(&gpt->regs->mode, reg);
  156. spin_unlock_irqrestore(&gpt->lock, flags);
  157. return 0;
  158. }
  159. static struct irq_chip mpc52xx_gpt_irq_chip = {
  160. .name = "MPC52xx GPT",
  161. .unmask = mpc52xx_gpt_irq_unmask,
  162. .mask = mpc52xx_gpt_irq_mask,
  163. .ack = mpc52xx_gpt_irq_ack,
  164. .set_type = mpc52xx_gpt_irq_set_type,
  165. };
  166. void mpc52xx_gpt_irq_cascade(unsigned int virq, struct irq_desc *desc)
  167. {
  168. struct mpc52xx_gpt_priv *gpt = get_irq_data(virq);
  169. int sub_virq;
  170. u32 status;
  171. status = in_be32(&gpt->regs->status) & MPC52xx_GPT_STATUS_IRQMASK;
  172. if (status) {
  173. sub_virq = irq_linear_revmap(gpt->irqhost, 0);
  174. generic_handle_irq(sub_virq);
  175. }
  176. }
  177. static int mpc52xx_gpt_irq_map(struct irq_host *h, unsigned int virq,
  178. irq_hw_number_t hw)
  179. {
  180. struct mpc52xx_gpt_priv *gpt = h->host_data;
  181. dev_dbg(gpt->dev, "%s: h=%p, virq=%i\n", __func__, h, virq);
  182. set_irq_chip_data(virq, gpt);
  183. set_irq_chip_and_handler(virq, &mpc52xx_gpt_irq_chip, handle_edge_irq);
  184. return 0;
  185. }
  186. static int mpc52xx_gpt_irq_xlate(struct irq_host *h, struct device_node *ct,
  187. const u32 *intspec, unsigned int intsize,
  188. irq_hw_number_t *out_hwirq,
  189. unsigned int *out_flags)
  190. {
  191. struct mpc52xx_gpt_priv *gpt = h->host_data;
  192. dev_dbg(gpt->dev, "%s: flags=%i\n", __func__, intspec[0]);
  193. if ((intsize < 1) || (intspec[0] > 3)) {
  194. dev_err(gpt->dev, "bad irq specifier in %s\n", ct->full_name);
  195. return -EINVAL;
  196. }
  197. *out_hwirq = 0; /* The GPT only has 1 IRQ line */
  198. *out_flags = intspec[0];
  199. return 0;
  200. }
  201. static struct irq_host_ops mpc52xx_gpt_irq_ops = {
  202. .map = mpc52xx_gpt_irq_map,
  203. .xlate = mpc52xx_gpt_irq_xlate,
  204. };
  205. static void
  206. mpc52xx_gpt_irq_setup(struct mpc52xx_gpt_priv *gpt, struct device_node *node)
  207. {
  208. int cascade_virq;
  209. unsigned long flags;
  210. u32 mode;
  211. cascade_virq = irq_of_parse_and_map(node, 0);
  212. if (!cascade_virq)
  213. return;
  214. gpt->irqhost = irq_alloc_host(node, IRQ_HOST_MAP_LINEAR, 1,
  215. &mpc52xx_gpt_irq_ops, -1);
  216. if (!gpt->irqhost) {
  217. dev_err(gpt->dev, "irq_alloc_host() failed\n");
  218. return;
  219. }
  220. gpt->irqhost->host_data = gpt;
  221. set_irq_data(cascade_virq, gpt);
  222. set_irq_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(to_of_gpio_chip(gc), struct mpc52xx_gpt_priv,of_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->of_gc.gc.label = kstrdup(node->full_name, GFP_KERNEL);
  283. if (!gpt->of_gc.gc.label) {
  284. dev_err(gpt->dev, "out of memory\n");
  285. return;
  286. }
  287. gpt->of_gc.gpio_cells = 2;
  288. gpt->of_gc.gc.ngpio = 1;
  289. gpt->of_gc.gc.direction_input = mpc52xx_gpt_gpio_dir_in;
  290. gpt->of_gc.gc.direction_output = mpc52xx_gpt_gpio_dir_out;
  291. gpt->of_gc.gc.get = mpc52xx_gpt_gpio_get;
  292. gpt->of_gc.gc.set = mpc52xx_gpt_gpio_set;
  293. gpt->of_gc.gc.base = -1;
  294. gpt->of_gc.xlate = of_gpio_simple_xlate;
  295. node->data = &gpt->of_gc;
  296. of_node_get(node);
  297. /* Setup external pin in GPIO mode */
  298. clrsetbits_be32(&gpt->regs->mode, MPC52xx_GPT_MODE_MS_MASK,
  299. MPC52xx_GPT_MODE_MS_GPIO);
  300. rc = gpiochip_add(&gpt->of_gc.gc);
  301. if (rc)
  302. dev_err(gpt->dev, "gpiochip_add() failed; rc=%i\n", rc);
  303. dev_dbg(gpt->dev, "%s() complete.\n", __func__);
  304. }
  305. #else /* defined(CONFIG_GPIOLIB) */
  306. static void
  307. mpc52xx_gpt_gpio_setup(struct mpc52xx_gpt_priv *p, struct device_node *np) { }
  308. #endif /* defined(CONFIG_GPIOLIB) */
  309. /***********************************************************************
  310. * Timer API
  311. */
  312. /**
  313. * mpc52xx_gpt_from_irq - Return the GPT device associated with an IRQ number
  314. * @irq: irq of timer.
  315. */
  316. struct mpc52xx_gpt_priv *mpc52xx_gpt_from_irq(int irq)
  317. {
  318. struct mpc52xx_gpt_priv *gpt;
  319. struct list_head *pos;
  320. /* Iterate over the list of timers looking for a matching device */
  321. mutex_lock(&mpc52xx_gpt_list_mutex);
  322. list_for_each(pos, &mpc52xx_gpt_list) {
  323. gpt = container_of(pos, struct mpc52xx_gpt_priv, list);
  324. if (gpt->irqhost && irq == irq_linear_revmap(gpt->irqhost, 0)) {
  325. mutex_unlock(&mpc52xx_gpt_list_mutex);
  326. return gpt;
  327. }
  328. }
  329. mutex_unlock(&mpc52xx_gpt_list_mutex);
  330. return NULL;
  331. }
  332. EXPORT_SYMBOL(mpc52xx_gpt_from_irq);
  333. static int mpc52xx_gpt_do_start(struct mpc52xx_gpt_priv *gpt, u64 period,
  334. int continuous, int as_wdt)
  335. {
  336. u32 clear, set;
  337. u64 clocks;
  338. u32 prescale;
  339. unsigned long flags;
  340. clear = MPC52xx_GPT_MODE_MS_MASK | MPC52xx_GPT_MODE_CONTINUOUS;
  341. set = MPC52xx_GPT_MODE_MS_GPIO | MPC52xx_GPT_MODE_COUNTER_ENABLE;
  342. if (as_wdt) {
  343. clear |= MPC52xx_GPT_MODE_IRQ_EN;
  344. set |= MPC52xx_GPT_MODE_WDT_EN;
  345. } else if (continuous)
  346. set |= MPC52xx_GPT_MODE_CONTINUOUS;
  347. /* Determine the number of clocks in the requested period. 64 bit
  348. * arithmatic is done here to preserve the precision until the value
  349. * is scaled back down into the u32 range. Period is in 'ns', bus
  350. * frequency is in Hz. */
  351. clocks = period * (u64)gpt->ipb_freq;
  352. do_div(clocks, 1000000000); /* Scale it down to ns range */
  353. /* This device cannot handle a clock count greater than 32 bits */
  354. if (clocks > 0xffffffff)
  355. return -EINVAL;
  356. /* Calculate the prescaler and count values from the clocks value.
  357. * 'clocks' is the number of clock ticks in the period. The timer
  358. * has 16 bit precision and a 16 bit prescaler. Prescaler is
  359. * calculated by integer dividing the clocks by 0x10000 (shifting
  360. * down 16 bits) to obtain the smallest possible divisor for clocks
  361. * to get a 16 bit count value.
  362. *
  363. * Note: the prescale register is '1' based, not '0' based. ie. a
  364. * value of '1' means divide the clock by one. 0xffff divides the
  365. * clock by 0xffff. '0x0000' does not divide by zero, but wraps
  366. * around and divides by 0x10000. That is why prescale must be
  367. * a u32 variable, not a u16, for this calculation. */
  368. prescale = (clocks >> 16) + 1;
  369. do_div(clocks, prescale);
  370. if (clocks > 0xffff) {
  371. pr_err("calculation error; prescale:%x clocks:%llx\n",
  372. prescale, clocks);
  373. return -EINVAL;
  374. }
  375. /* Set and enable the timer, reject an attempt to use a wdt as gpt */
  376. spin_lock_irqsave(&gpt->lock, flags);
  377. if (as_wdt)
  378. gpt->wdt_mode |= MPC52xx_GPT_IS_WDT;
  379. else if ((gpt->wdt_mode & MPC52xx_GPT_IS_WDT) != 0) {
  380. spin_unlock_irqrestore(&gpt->lock, flags);
  381. return -EBUSY;
  382. }
  383. out_be32(&gpt->regs->count, prescale << 16 | clocks);
  384. clrsetbits_be32(&gpt->regs->mode, clear, set);
  385. spin_unlock_irqrestore(&gpt->lock, flags);
  386. return 0;
  387. }
  388. /**
  389. * mpc52xx_gpt_start_timer - Set and enable the GPT timer
  390. * @gpt: Pointer to gpt private data structure
  391. * @period: period of timer in ns; max. ~130s @ 33MHz IPB clock
  392. * @continuous: set to 1 to make timer continuous free running
  393. *
  394. * An interrupt will be generated every time the timer fires
  395. */
  396. int mpc52xx_gpt_start_timer(struct mpc52xx_gpt_priv *gpt, u64 period,
  397. int continuous)
  398. {
  399. return mpc52xx_gpt_do_start(gpt, period, continuous, 0);
  400. }
  401. EXPORT_SYMBOL(mpc52xx_gpt_start_timer);
  402. /**
  403. * mpc52xx_gpt_stop_timer - Stop a gpt
  404. * @gpt: Pointer to gpt private data structure
  405. *
  406. * Returns an error if attempting to stop a wdt
  407. */
  408. int mpc52xx_gpt_stop_timer(struct mpc52xx_gpt_priv *gpt)
  409. {
  410. unsigned long flags;
  411. /* reject the operation if the timer is used as watchdog (gpt 0 only) */
  412. spin_lock_irqsave(&gpt->lock, flags);
  413. if ((gpt->wdt_mode & MPC52xx_GPT_IS_WDT) != 0) {
  414. spin_unlock_irqrestore(&gpt->lock, flags);
  415. return -EBUSY;
  416. }
  417. clrbits32(&gpt->regs->mode, MPC52xx_GPT_MODE_COUNTER_ENABLE);
  418. spin_unlock_irqrestore(&gpt->lock, flags);
  419. return 0;
  420. }
  421. EXPORT_SYMBOL(mpc52xx_gpt_stop_timer);
  422. /**
  423. * mpc52xx_gpt_timer_period - Read the timer period
  424. * @gpt: Pointer to gpt private data structure
  425. *
  426. * Returns the timer period in ns
  427. */
  428. u64 mpc52xx_gpt_timer_period(struct mpc52xx_gpt_priv *gpt)
  429. {
  430. u64 period;
  431. u64 prescale;
  432. unsigned long flags;
  433. spin_lock_irqsave(&gpt->lock, flags);
  434. period = in_be32(&gpt->regs->count);
  435. spin_unlock_irqrestore(&gpt->lock, flags);
  436. prescale = period >> 16;
  437. period &= 0xffff;
  438. if (prescale == 0)
  439. prescale = 0x10000;
  440. period = period * prescale * 1000000000ULL;
  441. do_div(period, (u64)gpt->ipb_freq);
  442. return period;
  443. }
  444. EXPORT_SYMBOL(mpc52xx_gpt_timer_period);
  445. #if defined(CONFIG_MPC5200_WDT)
  446. /***********************************************************************
  447. * Watchdog API for gpt0
  448. */
  449. #define WDT_IDENTITY "mpc52xx watchdog on GPT0"
  450. /* wdt_is_active stores wether or not the /dev/watchdog device is opened */
  451. static unsigned long wdt_is_active;
  452. /* wdt-capable gpt */
  453. static struct mpc52xx_gpt_priv *mpc52xx_gpt_wdt;
  454. /* low-level wdt functions */
  455. static inline void mpc52xx_gpt_wdt_ping(struct mpc52xx_gpt_priv *gpt_wdt)
  456. {
  457. unsigned long flags;
  458. spin_lock_irqsave(&gpt_wdt->lock, flags);
  459. out_8((u8 *) &gpt_wdt->regs->mode, MPC52xx_GPT_MODE_WDT_PING);
  460. spin_unlock_irqrestore(&gpt_wdt->lock, flags);
  461. }
  462. /* wdt misc device api */
  463. static ssize_t mpc52xx_wdt_write(struct file *file, const char __user *data,
  464. size_t len, loff_t *ppos)
  465. {
  466. struct mpc52xx_gpt_priv *gpt_wdt = file->private_data;
  467. mpc52xx_gpt_wdt_ping(gpt_wdt);
  468. return 0;
  469. }
  470. static const struct watchdog_info mpc5200_wdt_info = {
  471. .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
  472. .identity = WDT_IDENTITY,
  473. };
  474. static long mpc52xx_wdt_ioctl(struct file *file, unsigned int cmd,
  475. unsigned long arg)
  476. {
  477. struct mpc52xx_gpt_priv *gpt_wdt = file->private_data;
  478. int __user *data = (int __user *)arg;
  479. int timeout;
  480. u64 real_timeout;
  481. int ret = 0;
  482. switch (cmd) {
  483. case WDIOC_GETSUPPORT:
  484. ret = copy_to_user(data, &mpc5200_wdt_info,
  485. sizeof(mpc5200_wdt_info));
  486. if (ret)
  487. ret = -EFAULT;
  488. break;
  489. case WDIOC_GETSTATUS:
  490. case WDIOC_GETBOOTSTATUS:
  491. ret = put_user(0, data);
  492. break;
  493. case WDIOC_KEEPALIVE:
  494. mpc52xx_gpt_wdt_ping(gpt_wdt);
  495. break;
  496. case WDIOC_SETTIMEOUT:
  497. ret = get_user(timeout, data);
  498. if (ret)
  499. break;
  500. real_timeout = (u64) timeout * 1000000000ULL;
  501. ret = mpc52xx_gpt_do_start(gpt_wdt, real_timeout, 0, 1);
  502. if (ret)
  503. break;
  504. /* fall through and return the timeout */
  505. case WDIOC_GETTIMEOUT:
  506. /* we need to round here as to avoid e.g. the following
  507. * situation:
  508. * - timeout requested is 1 second;
  509. * - real timeout @33MHz is 999997090ns
  510. * - the int divide by 10^9 will return 0.
  511. */
  512. real_timeout =
  513. mpc52xx_gpt_timer_period(gpt_wdt) + 500000000ULL;
  514. do_div(real_timeout, 1000000000ULL);
  515. timeout = (int) real_timeout;
  516. ret = put_user(timeout, data);
  517. break;
  518. default:
  519. ret = -ENOTTY;
  520. }
  521. return ret;
  522. }
  523. static int mpc52xx_wdt_open(struct inode *inode, struct file *file)
  524. {
  525. int ret;
  526. /* sanity check */
  527. if (!mpc52xx_gpt_wdt)
  528. return -ENODEV;
  529. /* /dev/watchdog can only be opened once */
  530. if (test_and_set_bit(0, &wdt_is_active))
  531. return -EBUSY;
  532. /* Set and activate the watchdog with 30 seconds timeout */
  533. ret = mpc52xx_gpt_do_start(mpc52xx_gpt_wdt, 30ULL * 1000000000ULL,
  534. 0, 1);
  535. if (ret) {
  536. clear_bit(0, &wdt_is_active);
  537. return ret;
  538. }
  539. file->private_data = mpc52xx_gpt_wdt;
  540. return nonseekable_open(inode, file);
  541. }
  542. static int mpc52xx_wdt_release(struct inode *inode, struct file *file)
  543. {
  544. /* note: releasing the wdt in NOWAYOUT-mode does not stop it */
  545. #if !defined(CONFIG_WATCHDOG_NOWAYOUT)
  546. struct mpc52xx_gpt_priv *gpt_wdt = file->private_data;
  547. unsigned long flags;
  548. spin_lock_irqsave(&gpt_wdt->lock, flags);
  549. clrbits32(&gpt_wdt->regs->mode,
  550. MPC52xx_GPT_MODE_COUNTER_ENABLE | MPC52xx_GPT_MODE_WDT_EN);
  551. gpt_wdt->wdt_mode &= ~MPC52xx_GPT_IS_WDT;
  552. spin_unlock_irqrestore(&gpt_wdt->lock, flags);
  553. #endif
  554. clear_bit(0, &wdt_is_active);
  555. return 0;
  556. }
  557. static const struct file_operations mpc52xx_wdt_fops = {
  558. .owner = THIS_MODULE,
  559. .llseek = no_llseek,
  560. .write = mpc52xx_wdt_write,
  561. .unlocked_ioctl = mpc52xx_wdt_ioctl,
  562. .open = mpc52xx_wdt_open,
  563. .release = mpc52xx_wdt_release,
  564. };
  565. static struct miscdevice mpc52xx_wdt_miscdev = {
  566. .minor = WATCHDOG_MINOR,
  567. .name = "watchdog",
  568. .fops = &mpc52xx_wdt_fops,
  569. };
  570. static int __devinit mpc52xx_gpt_wdt_init(void)
  571. {
  572. int err;
  573. /* try to register the watchdog misc device */
  574. err = misc_register(&mpc52xx_wdt_miscdev);
  575. if (err)
  576. pr_err("%s: cannot register watchdog device\n", WDT_IDENTITY);
  577. else
  578. pr_info("%s: watchdog device registered\n", WDT_IDENTITY);
  579. return err;
  580. }
  581. static int mpc52xx_gpt_wdt_setup(struct mpc52xx_gpt_priv *gpt,
  582. const u32 *period)
  583. {
  584. u64 real_timeout;
  585. /* remember the gpt for the wdt operation */
  586. mpc52xx_gpt_wdt = gpt;
  587. /* configure the wdt if the device tree contained a timeout */
  588. if (!period || *period == 0)
  589. return 0;
  590. real_timeout = (u64) *period * 1000000000ULL;
  591. if (mpc52xx_gpt_do_start(gpt, real_timeout, 0, 1))
  592. dev_warn(gpt->dev, "starting as wdt failed\n");
  593. else
  594. dev_info(gpt->dev, "watchdog set to %us timeout\n", *period);
  595. return 0;
  596. }
  597. #else
  598. static int __devinit mpc52xx_gpt_wdt_init(void)
  599. {
  600. return 0;
  601. }
  602. static inline int mpc52xx_gpt_wdt_setup(struct mpc52xx_gpt_priv *gpt,
  603. const u32 *period)
  604. {
  605. return 0;
  606. }
  607. #endif /* CONFIG_MPC5200_WDT */
  608. /* ---------------------------------------------------------------------
  609. * of_platform bus binding code
  610. */
  611. static int __devinit mpc52xx_gpt_probe(struct of_device *ofdev,
  612. const struct of_device_id *match)
  613. {
  614. struct mpc52xx_gpt_priv *gpt;
  615. gpt = kzalloc(sizeof *gpt, GFP_KERNEL);
  616. if (!gpt)
  617. return -ENOMEM;
  618. spin_lock_init(&gpt->lock);
  619. gpt->dev = &ofdev->dev;
  620. gpt->ipb_freq = mpc5xxx_get_bus_frequency(ofdev->dev.of_node);
  621. gpt->regs = of_iomap(ofdev->dev.of_node, 0);
  622. if (!gpt->regs) {
  623. kfree(gpt);
  624. return -ENOMEM;
  625. }
  626. dev_set_drvdata(&ofdev->dev, gpt);
  627. mpc52xx_gpt_gpio_setup(gpt, ofdev->dev.of_node);
  628. mpc52xx_gpt_irq_setup(gpt, ofdev->dev.of_node);
  629. mutex_lock(&mpc52xx_gpt_list_mutex);
  630. list_add(&gpt->list, &mpc52xx_gpt_list);
  631. mutex_unlock(&mpc52xx_gpt_list_mutex);
  632. /* check if this device could be a watchdog */
  633. if (of_get_property(ofdev->dev.of_node, "fsl,has-wdt", NULL) ||
  634. of_get_property(ofdev->dev.of_node, "has-wdt", NULL)) {
  635. const u32 *on_boot_wdt;
  636. gpt->wdt_mode = MPC52xx_GPT_CAN_WDT;
  637. on_boot_wdt = of_get_property(ofdev->dev.of_node,
  638. "fsl,wdt-on-boot", NULL);
  639. if (on_boot_wdt) {
  640. dev_info(gpt->dev, "used as watchdog\n");
  641. gpt->wdt_mode |= MPC52xx_GPT_IS_WDT;
  642. } else
  643. dev_info(gpt->dev, "can function as watchdog\n");
  644. mpc52xx_gpt_wdt_setup(gpt, on_boot_wdt);
  645. }
  646. return 0;
  647. }
  648. static int mpc52xx_gpt_remove(struct of_device *ofdev)
  649. {
  650. return -EBUSY;
  651. }
  652. static const struct of_device_id mpc52xx_gpt_match[] = {
  653. { .compatible = "fsl,mpc5200-gpt", },
  654. /* Depreciated compatible values; don't use for new dts files */
  655. { .compatible = "fsl,mpc5200-gpt-gpio", },
  656. { .compatible = "mpc5200-gpt", },
  657. {}
  658. };
  659. static struct of_platform_driver mpc52xx_gpt_driver = {
  660. .driver = {
  661. .name = "mpc52xx-gpt",
  662. .owner = THIS_MODULE,
  663. .of_match_table = mpc52xx_gpt_match,
  664. },
  665. .probe = mpc52xx_gpt_probe,
  666. .remove = mpc52xx_gpt_remove,
  667. };
  668. static int __init mpc52xx_gpt_init(void)
  669. {
  670. if (of_register_platform_driver(&mpc52xx_gpt_driver))
  671. pr_err("error registering MPC52xx GPT driver\n");
  672. return 0;
  673. }
  674. /* Make sure GPIOs and IRQs get set up before anyone tries to use them */
  675. subsys_initcall(mpc52xx_gpt_init);
  676. device_initcall(mpc52xx_gpt_wdt_init);