mpc52xx_gpt.c 23 KB

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