mpc52xx_gpt.c 23 KB

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