sh_cmt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /*
  2. * SuperH Timer Support - CMT
  3. *
  4. * Copyright (C) 2008 Magnus Damm
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/init.h>
  20. #include <linux/bootmem.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/ioport.h>
  25. #include <linux/io.h>
  26. #include <linux/clk.h>
  27. #include <linux/irq.h>
  28. #include <linux/err.h>
  29. #include <linux/clocksource.h>
  30. #include <linux/clockchips.h>
  31. #include <linux/sh_cmt.h>
  32. struct sh_cmt_priv {
  33. void __iomem *mapbase;
  34. struct clk *clk;
  35. unsigned long width; /* 16 or 32 bit version of hardware block */
  36. unsigned long overflow_bit;
  37. unsigned long clear_bits;
  38. struct irqaction irqaction;
  39. struct platform_device *pdev;
  40. unsigned long flags;
  41. unsigned long match_value;
  42. unsigned long next_match_value;
  43. unsigned long max_match_value;
  44. unsigned long rate;
  45. spinlock_t lock;
  46. struct clock_event_device ced;
  47. unsigned long total_cycles;
  48. };
  49. static DEFINE_SPINLOCK(sh_cmt_lock);
  50. #define CMSTR -1 /* shared register */
  51. #define CMCSR 0 /* channel register */
  52. #define CMCNT 1 /* channel register */
  53. #define CMCOR 2 /* channel register */
  54. static inline unsigned long sh_cmt_read(struct sh_cmt_priv *p, int reg_nr)
  55. {
  56. struct sh_cmt_config *cfg = p->pdev->dev.platform_data;
  57. void __iomem *base = p->mapbase;
  58. unsigned long offs;
  59. if (reg_nr == CMSTR) {
  60. offs = 0;
  61. base -= cfg->channel_offset;
  62. } else
  63. offs = reg_nr;
  64. if (p->width == 16)
  65. offs <<= 1;
  66. else {
  67. offs <<= 2;
  68. if ((reg_nr == CMCNT) || (reg_nr == CMCOR))
  69. return ioread32(base + offs);
  70. }
  71. return ioread16(base + offs);
  72. }
  73. static inline void sh_cmt_write(struct sh_cmt_priv *p, int reg_nr,
  74. unsigned long value)
  75. {
  76. struct sh_cmt_config *cfg = p->pdev->dev.platform_data;
  77. void __iomem *base = p->mapbase;
  78. unsigned long offs;
  79. if (reg_nr == CMSTR) {
  80. offs = 0;
  81. base -= cfg->channel_offset;
  82. } else
  83. offs = reg_nr;
  84. if (p->width == 16)
  85. offs <<= 1;
  86. else {
  87. offs <<= 2;
  88. if ((reg_nr == CMCNT) || (reg_nr == CMCOR)) {
  89. iowrite32(value, base + offs);
  90. return;
  91. }
  92. }
  93. iowrite16(value, base + offs);
  94. }
  95. static unsigned long sh_cmt_get_counter(struct sh_cmt_priv *p,
  96. int *has_wrapped)
  97. {
  98. unsigned long v1, v2, v3;
  99. /* Make sure the timer value is stable. Stolen from acpi_pm.c */
  100. do {
  101. v1 = sh_cmt_read(p, CMCNT);
  102. v2 = sh_cmt_read(p, CMCNT);
  103. v3 = sh_cmt_read(p, CMCNT);
  104. } while (unlikely((v1 > v2 && v1 < v3) || (v2 > v3 && v2 < v1)
  105. || (v3 > v1 && v3 < v2)));
  106. *has_wrapped = sh_cmt_read(p, CMCSR) & p->overflow_bit;
  107. return v2;
  108. }
  109. static void sh_cmt_start_stop_ch(struct sh_cmt_priv *p, int start)
  110. {
  111. struct sh_cmt_config *cfg = p->pdev->dev.platform_data;
  112. unsigned long flags, value;
  113. /* start stop register shared by multiple timer channels */
  114. spin_lock_irqsave(&sh_cmt_lock, flags);
  115. value = sh_cmt_read(p, CMSTR);
  116. if (start)
  117. value |= 1 << cfg->timer_bit;
  118. else
  119. value &= ~(1 << cfg->timer_bit);
  120. sh_cmt_write(p, CMSTR, value);
  121. spin_unlock_irqrestore(&sh_cmt_lock, flags);
  122. }
  123. static int sh_cmt_enable(struct sh_cmt_priv *p, unsigned long *rate)
  124. {
  125. struct sh_cmt_config *cfg = p->pdev->dev.platform_data;
  126. int ret;
  127. /* enable clock */
  128. ret = clk_enable(p->clk);
  129. if (ret) {
  130. pr_err("sh_cmt: cannot enable clock \"%s\"\n", cfg->clk);
  131. return ret;
  132. }
  133. *rate = clk_get_rate(p->clk) / 8;
  134. /* make sure channel is disabled */
  135. sh_cmt_start_stop_ch(p, 0);
  136. /* configure channel, periodic mode and maximum timeout */
  137. if (p->width == 16)
  138. sh_cmt_write(p, CMCSR, 0);
  139. else
  140. sh_cmt_write(p, CMCSR, 0x01a4);
  141. sh_cmt_write(p, CMCOR, 0xffffffff);
  142. sh_cmt_write(p, CMCNT, 0);
  143. /* enable channel */
  144. sh_cmt_start_stop_ch(p, 1);
  145. return 0;
  146. }
  147. static void sh_cmt_disable(struct sh_cmt_priv *p)
  148. {
  149. /* disable channel */
  150. sh_cmt_start_stop_ch(p, 0);
  151. /* stop clock */
  152. clk_disable(p->clk);
  153. }
  154. /* private flags */
  155. #define FLAG_CLOCKEVENT (1 << 0)
  156. #define FLAG_CLOCKSOURCE (1 << 1)
  157. #define FLAG_REPROGRAM (1 << 2)
  158. #define FLAG_SKIPEVENT (1 << 3)
  159. #define FLAG_IRQCONTEXT (1 << 4)
  160. static void sh_cmt_clock_event_program_verify(struct sh_cmt_priv *p,
  161. int absolute)
  162. {
  163. unsigned long new_match;
  164. unsigned long value = p->next_match_value;
  165. unsigned long delay = 0;
  166. unsigned long now = 0;
  167. int has_wrapped;
  168. now = sh_cmt_get_counter(p, &has_wrapped);
  169. p->flags |= FLAG_REPROGRAM; /* force reprogram */
  170. if (has_wrapped) {
  171. /* we're competing with the interrupt handler.
  172. * -> let the interrupt handler reprogram the timer.
  173. * -> interrupt number two handles the event.
  174. */
  175. p->flags |= FLAG_SKIPEVENT;
  176. return;
  177. }
  178. if (absolute)
  179. now = 0;
  180. do {
  181. /* reprogram the timer hardware,
  182. * but don't save the new match value yet.
  183. */
  184. new_match = now + value + delay;
  185. if (new_match > p->max_match_value)
  186. new_match = p->max_match_value;
  187. sh_cmt_write(p, CMCOR, new_match);
  188. now = sh_cmt_get_counter(p, &has_wrapped);
  189. if (has_wrapped && (new_match > p->match_value)) {
  190. /* we are changing to a greater match value,
  191. * so this wrap must be caused by the counter
  192. * matching the old value.
  193. * -> first interrupt reprograms the timer.
  194. * -> interrupt number two handles the event.
  195. */
  196. p->flags |= FLAG_SKIPEVENT;
  197. break;
  198. }
  199. if (has_wrapped) {
  200. /* we are changing to a smaller match value,
  201. * so the wrap must be caused by the counter
  202. * matching the new value.
  203. * -> save programmed match value.
  204. * -> let isr handle the event.
  205. */
  206. p->match_value = new_match;
  207. break;
  208. }
  209. /* be safe: verify hardware settings */
  210. if (now < new_match) {
  211. /* timer value is below match value, all good.
  212. * this makes sure we won't miss any match events.
  213. * -> save programmed match value.
  214. * -> let isr handle the event.
  215. */
  216. p->match_value = new_match;
  217. break;
  218. }
  219. /* the counter has reached a value greater
  220. * than our new match value. and since the
  221. * has_wrapped flag isn't set we must have
  222. * programmed a too close event.
  223. * -> increase delay and retry.
  224. */
  225. if (delay)
  226. delay <<= 1;
  227. else
  228. delay = 1;
  229. if (!delay)
  230. pr_warning("sh_cmt: too long delay\n");
  231. } while (delay);
  232. }
  233. static void sh_cmt_set_next(struct sh_cmt_priv *p, unsigned long delta)
  234. {
  235. unsigned long flags;
  236. if (delta > p->max_match_value)
  237. pr_warning("sh_cmt: delta out of range\n");
  238. spin_lock_irqsave(&p->lock, flags);
  239. p->next_match_value = delta;
  240. sh_cmt_clock_event_program_verify(p, 0);
  241. spin_unlock_irqrestore(&p->lock, flags);
  242. }
  243. static irqreturn_t sh_cmt_interrupt(int irq, void *dev_id)
  244. {
  245. struct sh_cmt_priv *p = dev_id;
  246. /* clear flags */
  247. sh_cmt_write(p, CMCSR, sh_cmt_read(p, CMCSR) & p->clear_bits);
  248. /* update clock source counter to begin with if enabled
  249. * the wrap flag should be cleared by the timer specific
  250. * isr before we end up here.
  251. */
  252. if (p->flags & FLAG_CLOCKSOURCE)
  253. p->total_cycles += p->match_value;
  254. if (!(p->flags & FLAG_REPROGRAM))
  255. p->next_match_value = p->max_match_value;
  256. p->flags |= FLAG_IRQCONTEXT;
  257. if (p->flags & FLAG_CLOCKEVENT) {
  258. if (!(p->flags & FLAG_SKIPEVENT)) {
  259. if (p->ced.mode == CLOCK_EVT_MODE_ONESHOT) {
  260. p->next_match_value = p->max_match_value;
  261. p->flags |= FLAG_REPROGRAM;
  262. }
  263. p->ced.event_handler(&p->ced);
  264. }
  265. }
  266. p->flags &= ~FLAG_SKIPEVENT;
  267. if (p->flags & FLAG_REPROGRAM) {
  268. p->flags &= ~FLAG_REPROGRAM;
  269. sh_cmt_clock_event_program_verify(p, 1);
  270. if (p->flags & FLAG_CLOCKEVENT)
  271. if ((p->ced.mode == CLOCK_EVT_MODE_SHUTDOWN)
  272. || (p->match_value == p->next_match_value))
  273. p->flags &= ~FLAG_REPROGRAM;
  274. }
  275. p->flags &= ~FLAG_IRQCONTEXT;
  276. return IRQ_HANDLED;
  277. }
  278. static int sh_cmt_start(struct sh_cmt_priv *p, unsigned long flag)
  279. {
  280. int ret = 0;
  281. unsigned long flags;
  282. spin_lock_irqsave(&p->lock, flags);
  283. if (!(p->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
  284. ret = sh_cmt_enable(p, &p->rate);
  285. if (ret)
  286. goto out;
  287. p->flags |= flag;
  288. /* setup timeout if no clockevent */
  289. if ((flag == FLAG_CLOCKSOURCE) && (!(p->flags & FLAG_CLOCKEVENT)))
  290. sh_cmt_set_next(p, p->max_match_value);
  291. out:
  292. spin_unlock_irqrestore(&p->lock, flags);
  293. return ret;
  294. }
  295. static void sh_cmt_stop(struct sh_cmt_priv *p, unsigned long flag)
  296. {
  297. unsigned long flags;
  298. unsigned long f;
  299. spin_lock_irqsave(&p->lock, flags);
  300. f = p->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE);
  301. p->flags &= ~flag;
  302. if (f && !(p->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
  303. sh_cmt_disable(p);
  304. /* adjust the timeout to maximum if only clocksource left */
  305. if ((flag == FLAG_CLOCKEVENT) && (p->flags & FLAG_CLOCKSOURCE))
  306. sh_cmt_set_next(p, p->max_match_value);
  307. spin_unlock_irqrestore(&p->lock, flags);
  308. }
  309. static struct sh_cmt_priv *ced_to_sh_cmt(struct clock_event_device *ced)
  310. {
  311. return container_of(ced, struct sh_cmt_priv, ced);
  312. }
  313. static void sh_cmt_clock_event_start(struct sh_cmt_priv *p, int periodic)
  314. {
  315. struct clock_event_device *ced = &p->ced;
  316. sh_cmt_start(p, FLAG_CLOCKEVENT);
  317. /* TODO: calculate good shift from rate and counter bit width */
  318. ced->shift = 32;
  319. ced->mult = div_sc(p->rate, NSEC_PER_SEC, ced->shift);
  320. ced->max_delta_ns = clockevent_delta2ns(p->max_match_value, ced);
  321. ced->min_delta_ns = clockevent_delta2ns(0x1f, ced);
  322. if (periodic)
  323. sh_cmt_set_next(p, (p->rate + HZ/2) / HZ);
  324. else
  325. sh_cmt_set_next(p, p->max_match_value);
  326. }
  327. static void sh_cmt_clock_event_mode(enum clock_event_mode mode,
  328. struct clock_event_device *ced)
  329. {
  330. struct sh_cmt_priv *p = ced_to_sh_cmt(ced);
  331. /* deal with old setting first */
  332. switch (ced->mode) {
  333. case CLOCK_EVT_MODE_PERIODIC:
  334. case CLOCK_EVT_MODE_ONESHOT:
  335. sh_cmt_stop(p, FLAG_CLOCKEVENT);
  336. break;
  337. default:
  338. break;
  339. }
  340. switch (mode) {
  341. case CLOCK_EVT_MODE_PERIODIC:
  342. pr_info("sh_cmt: %s used for periodic clock events\n",
  343. ced->name);
  344. sh_cmt_clock_event_start(p, 1);
  345. break;
  346. case CLOCK_EVT_MODE_ONESHOT:
  347. pr_info("sh_cmt: %s used for oneshot clock events\n",
  348. ced->name);
  349. sh_cmt_clock_event_start(p, 0);
  350. break;
  351. case CLOCK_EVT_MODE_SHUTDOWN:
  352. case CLOCK_EVT_MODE_UNUSED:
  353. sh_cmt_stop(p, FLAG_CLOCKEVENT);
  354. break;
  355. default:
  356. break;
  357. }
  358. }
  359. static int sh_cmt_clock_event_next(unsigned long delta,
  360. struct clock_event_device *ced)
  361. {
  362. struct sh_cmt_priv *p = ced_to_sh_cmt(ced);
  363. BUG_ON(ced->mode != CLOCK_EVT_MODE_ONESHOT);
  364. if (likely(p->flags & FLAG_IRQCONTEXT))
  365. p->next_match_value = delta;
  366. else
  367. sh_cmt_set_next(p, delta);
  368. return 0;
  369. }
  370. static void sh_cmt_register_clockevent(struct sh_cmt_priv *p,
  371. char *name, unsigned long rating)
  372. {
  373. struct clock_event_device *ced = &p->ced;
  374. memset(ced, 0, sizeof(*ced));
  375. ced->name = name;
  376. ced->features = CLOCK_EVT_FEAT_PERIODIC;
  377. ced->features |= CLOCK_EVT_FEAT_ONESHOT;
  378. ced->rating = rating;
  379. ced->cpumask = cpumask_of(0);
  380. ced->set_next_event = sh_cmt_clock_event_next;
  381. ced->set_mode = sh_cmt_clock_event_mode;
  382. pr_info("sh_cmt: %s used for clock events\n", ced->name);
  383. clockevents_register_device(ced);
  384. }
  385. int sh_cmt_register(struct sh_cmt_priv *p, char *name,
  386. unsigned long clockevent_rating,
  387. unsigned long clocksource_rating)
  388. {
  389. if (p->width == (sizeof(p->max_match_value) * 8))
  390. p->max_match_value = ~0;
  391. else
  392. p->max_match_value = (1 << p->width) - 1;
  393. p->match_value = p->max_match_value;
  394. spin_lock_init(&p->lock);
  395. if (clockevent_rating)
  396. sh_cmt_register_clockevent(p, name, clockevent_rating);
  397. return 0;
  398. }
  399. static int sh_cmt_setup(struct sh_cmt_priv *p, struct platform_device *pdev)
  400. {
  401. struct sh_cmt_config *cfg = pdev->dev.platform_data;
  402. struct resource *res;
  403. int irq, ret;
  404. ret = -ENXIO;
  405. memset(p, 0, sizeof(*p));
  406. p->pdev = pdev;
  407. if (!cfg) {
  408. dev_err(&p->pdev->dev, "missing platform data\n");
  409. goto err0;
  410. }
  411. platform_set_drvdata(pdev, p);
  412. res = platform_get_resource(p->pdev, IORESOURCE_MEM, 0);
  413. if (!res) {
  414. dev_err(&p->pdev->dev, "failed to get I/O memory\n");
  415. goto err0;
  416. }
  417. irq = platform_get_irq(p->pdev, 0);
  418. if (irq < 0) {
  419. dev_err(&p->pdev->dev, "failed to get irq\n");
  420. goto err0;
  421. }
  422. /* map memory, let mapbase point to our channel */
  423. p->mapbase = ioremap_nocache(res->start, resource_size(res));
  424. if (p->mapbase == NULL) {
  425. pr_err("sh_cmt: failed to remap I/O memory\n");
  426. goto err0;
  427. }
  428. /* request irq using setup_irq() (too early for request_irq()) */
  429. p->irqaction.name = cfg->name;
  430. p->irqaction.handler = sh_cmt_interrupt;
  431. p->irqaction.dev_id = p;
  432. p->irqaction.flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL;
  433. p->irqaction.mask = CPU_MASK_NONE;
  434. ret = setup_irq(irq, &p->irqaction);
  435. if (ret) {
  436. pr_err("sh_cmt: failed to request irq %d\n", irq);
  437. goto err1;
  438. }
  439. /* get hold of clock */
  440. p->clk = clk_get(&p->pdev->dev, cfg->clk);
  441. if (IS_ERR(p->clk)) {
  442. pr_err("sh_cmt: cannot get clock \"%s\"\n", cfg->clk);
  443. ret = PTR_ERR(p->clk);
  444. goto err2;
  445. }
  446. if (resource_size(res) == 6) {
  447. p->width = 16;
  448. p->overflow_bit = 0x80;
  449. p->clear_bits = ~0xc0;
  450. } else {
  451. p->width = 32;
  452. p->overflow_bit = 0x8000;
  453. p->clear_bits = ~0xc000;
  454. }
  455. return sh_cmt_register(p, cfg->name,
  456. cfg->clockevent_rating,
  457. cfg->clocksource_rating);
  458. err2:
  459. remove_irq(irq, &p->irqaction);
  460. err1:
  461. iounmap(p->mapbase);
  462. err0:
  463. return ret;
  464. }
  465. static int __devinit sh_cmt_probe(struct platform_device *pdev)
  466. {
  467. struct sh_cmt_priv *p = platform_get_drvdata(pdev);
  468. int ret;
  469. p = kmalloc(sizeof(*p), GFP_KERNEL);
  470. if (p == NULL) {
  471. dev_err(&pdev->dev, "failed to allocate driver data\n");
  472. return -ENOMEM;
  473. }
  474. ret = sh_cmt_setup(p, pdev);
  475. if (ret) {
  476. kfree(p);
  477. platform_set_drvdata(pdev, NULL);
  478. }
  479. return ret;
  480. }
  481. static int __devexit sh_cmt_remove(struct platform_device *pdev)
  482. {
  483. return -EBUSY; /* cannot unregister clockevent and clocksource */
  484. }
  485. static struct platform_driver sh_cmt_device_driver = {
  486. .probe = sh_cmt_probe,
  487. .remove = __devexit_p(sh_cmt_remove),
  488. .driver = {
  489. .name = "sh_cmt",
  490. }
  491. };
  492. static int __init sh_cmt_init(void)
  493. {
  494. return platform_driver_register(&sh_cmt_device_driver);
  495. }
  496. static void __exit sh_cmt_exit(void)
  497. {
  498. platform_driver_unregister(&sh_cmt_device_driver);
  499. }
  500. module_init(sh_cmt_init);
  501. module_exit(sh_cmt_exit);
  502. MODULE_AUTHOR("Magnus Damm");
  503. MODULE_DESCRIPTION("SuperH CMT Timer Driver");
  504. MODULE_LICENSE("GPL v2");