sh_cmt.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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/platform_device.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/ioport.h>
  24. #include <linux/io.h>
  25. #include <linux/clk.h>
  26. #include <linux/irq.h>
  27. #include <linux/err.h>
  28. #include <linux/delay.h>
  29. #include <linux/clocksource.h>
  30. #include <linux/clockchips.h>
  31. #include <linux/sh_timer.h>
  32. #include <linux/slab.h>
  33. #include <linux/module.h>
  34. #include <linux/pm_domain.h>
  35. #include <linux/pm_runtime.h>
  36. struct sh_cmt_priv {
  37. void __iomem *mapbase;
  38. struct clk *clk;
  39. unsigned long width; /* 16 or 32 bit version of hardware block */
  40. unsigned long overflow_bit;
  41. unsigned long clear_bits;
  42. struct irqaction irqaction;
  43. struct platform_device *pdev;
  44. unsigned long flags;
  45. unsigned long match_value;
  46. unsigned long next_match_value;
  47. unsigned long max_match_value;
  48. unsigned long rate;
  49. raw_spinlock_t lock;
  50. struct clock_event_device ced;
  51. struct clocksource cs;
  52. unsigned long total_cycles;
  53. bool cs_enabled;
  54. /* callbacks for CMSTR and CMCSR access */
  55. unsigned long (*read_control)(void __iomem *base, unsigned long offs);
  56. void (*write_control)(void __iomem *base, unsigned long offs,
  57. unsigned long value);
  58. /* callbacks for CMCNT and CMCOR access */
  59. unsigned long (*read_count)(void __iomem *base, unsigned long offs);
  60. void (*write_count)(void __iomem *base, unsigned long offs,
  61. unsigned long value);
  62. };
  63. /* Examples of supported CMT timer register layouts and I/O access widths:
  64. *
  65. * "16-bit counter and 16-bit control" as found on sh7263:
  66. * CMSTR 0xfffec000 16-bit
  67. * CMCSR 0xfffec002 16-bit
  68. * CMCNT 0xfffec004 16-bit
  69. * CMCOR 0xfffec006 16-bit
  70. *
  71. * "32-bit counter and 16-bit control" as found on sh7372, sh73a0, r8a7740:
  72. * CMSTR 0xffca0000 16-bit
  73. * CMCSR 0xffca0060 16-bit
  74. * CMCNT 0xffca0064 32-bit
  75. * CMCOR 0xffca0068 32-bit
  76. */
  77. static unsigned long sh_cmt_read16(void __iomem *base, unsigned long offs)
  78. {
  79. return ioread16(base + (offs << 1));
  80. }
  81. static unsigned long sh_cmt_read32(void __iomem *base, unsigned long offs)
  82. {
  83. return ioread32(base + (offs << 2));
  84. }
  85. static void sh_cmt_write16(void __iomem *base, unsigned long offs,
  86. unsigned long value)
  87. {
  88. iowrite16(value, base + (offs << 1));
  89. }
  90. static void sh_cmt_write32(void __iomem *base, unsigned long offs,
  91. unsigned long value)
  92. {
  93. iowrite32(value, base + (offs << 2));
  94. }
  95. #define CMCSR 0 /* channel register */
  96. #define CMCNT 1 /* channel register */
  97. #define CMCOR 2 /* channel register */
  98. static inline unsigned long sh_cmt_read_cmstr(struct sh_cmt_priv *p)
  99. {
  100. struct sh_timer_config *cfg = p->pdev->dev.platform_data;
  101. return p->read_control(p->mapbase - cfg->channel_offset, 0);
  102. }
  103. static inline unsigned long sh_cmt_read_cmcsr(struct sh_cmt_priv *p)
  104. {
  105. return p->read_control(p->mapbase, CMCSR);
  106. }
  107. static inline unsigned long sh_cmt_read_cmcnt(struct sh_cmt_priv *p)
  108. {
  109. return p->read_count(p->mapbase, CMCNT);
  110. }
  111. static inline void sh_cmt_write_cmstr(struct sh_cmt_priv *p,
  112. unsigned long value)
  113. {
  114. struct sh_timer_config *cfg = p->pdev->dev.platform_data;
  115. p->write_control(p->mapbase - cfg->channel_offset, 0, value);
  116. }
  117. static inline void sh_cmt_write_cmcsr(struct sh_cmt_priv *p,
  118. unsigned long value)
  119. {
  120. p->write_control(p->mapbase, CMCSR, value);
  121. }
  122. static inline void sh_cmt_write_cmcnt(struct sh_cmt_priv *p,
  123. unsigned long value)
  124. {
  125. p->write_count(p->mapbase, CMCNT, value);
  126. }
  127. static inline void sh_cmt_write_cmcor(struct sh_cmt_priv *p,
  128. unsigned long value)
  129. {
  130. p->write_count(p->mapbase, CMCOR, value);
  131. }
  132. static unsigned long sh_cmt_get_counter(struct sh_cmt_priv *p,
  133. int *has_wrapped)
  134. {
  135. unsigned long v1, v2, v3;
  136. int o1, o2;
  137. o1 = sh_cmt_read_cmcsr(p) & p->overflow_bit;
  138. /* Make sure the timer value is stable. Stolen from acpi_pm.c */
  139. do {
  140. o2 = o1;
  141. v1 = sh_cmt_read_cmcnt(p);
  142. v2 = sh_cmt_read_cmcnt(p);
  143. v3 = sh_cmt_read_cmcnt(p);
  144. o1 = sh_cmt_read_cmcsr(p) & p->overflow_bit;
  145. } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
  146. || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
  147. *has_wrapped = o1;
  148. return v2;
  149. }
  150. static DEFINE_RAW_SPINLOCK(sh_cmt_lock);
  151. static void sh_cmt_start_stop_ch(struct sh_cmt_priv *p, int start)
  152. {
  153. struct sh_timer_config *cfg = p->pdev->dev.platform_data;
  154. unsigned long flags, value;
  155. /* start stop register shared by multiple timer channels */
  156. raw_spin_lock_irqsave(&sh_cmt_lock, flags);
  157. value = sh_cmt_read_cmstr(p);
  158. if (start)
  159. value |= 1 << cfg->timer_bit;
  160. else
  161. value &= ~(1 << cfg->timer_bit);
  162. sh_cmt_write_cmstr(p, value);
  163. raw_spin_unlock_irqrestore(&sh_cmt_lock, flags);
  164. }
  165. static int sh_cmt_enable(struct sh_cmt_priv *p, unsigned long *rate)
  166. {
  167. int k, ret;
  168. pm_runtime_get_sync(&p->pdev->dev);
  169. dev_pm_syscore_device(&p->pdev->dev, true);
  170. /* enable clock */
  171. ret = clk_enable(p->clk);
  172. if (ret) {
  173. dev_err(&p->pdev->dev, "cannot enable clock\n");
  174. goto err0;
  175. }
  176. /* make sure channel is disabled */
  177. sh_cmt_start_stop_ch(p, 0);
  178. /* configure channel, periodic mode and maximum timeout */
  179. if (p->width == 16) {
  180. *rate = clk_get_rate(p->clk) / 512;
  181. sh_cmt_write_cmcsr(p, 0x43);
  182. } else {
  183. *rate = clk_get_rate(p->clk) / 8;
  184. sh_cmt_write_cmcsr(p, 0x01a4);
  185. }
  186. sh_cmt_write_cmcor(p, 0xffffffff);
  187. sh_cmt_write_cmcnt(p, 0);
  188. /*
  189. * According to the sh73a0 user's manual, as CMCNT can be operated
  190. * only by the RCLK (Pseudo 32 KHz), there's one restriction on
  191. * modifying CMCNT register; two RCLK cycles are necessary before
  192. * this register is either read or any modification of the value
  193. * it holds is reflected in the LSI's actual operation.
  194. *
  195. * While at it, we're supposed to clear out the CMCNT as of this
  196. * moment, so make sure it's processed properly here. This will
  197. * take RCLKx2 at maximum.
  198. */
  199. for (k = 0; k < 100; k++) {
  200. if (!sh_cmt_read_cmcnt(p))
  201. break;
  202. udelay(1);
  203. }
  204. if (sh_cmt_read_cmcnt(p)) {
  205. dev_err(&p->pdev->dev, "cannot clear CMCNT\n");
  206. ret = -ETIMEDOUT;
  207. goto err1;
  208. }
  209. /* enable channel */
  210. sh_cmt_start_stop_ch(p, 1);
  211. return 0;
  212. err1:
  213. /* stop clock */
  214. clk_disable(p->clk);
  215. err0:
  216. return ret;
  217. }
  218. static void sh_cmt_disable(struct sh_cmt_priv *p)
  219. {
  220. /* disable channel */
  221. sh_cmt_start_stop_ch(p, 0);
  222. /* disable interrupts in CMT block */
  223. sh_cmt_write_cmcsr(p, 0);
  224. /* stop clock */
  225. clk_disable(p->clk);
  226. dev_pm_syscore_device(&p->pdev->dev, false);
  227. pm_runtime_put(&p->pdev->dev);
  228. }
  229. /* private flags */
  230. #define FLAG_CLOCKEVENT (1 << 0)
  231. #define FLAG_CLOCKSOURCE (1 << 1)
  232. #define FLAG_REPROGRAM (1 << 2)
  233. #define FLAG_SKIPEVENT (1 << 3)
  234. #define FLAG_IRQCONTEXT (1 << 4)
  235. static void sh_cmt_clock_event_program_verify(struct sh_cmt_priv *p,
  236. int absolute)
  237. {
  238. unsigned long new_match;
  239. unsigned long value = p->next_match_value;
  240. unsigned long delay = 0;
  241. unsigned long now = 0;
  242. int has_wrapped;
  243. now = sh_cmt_get_counter(p, &has_wrapped);
  244. p->flags |= FLAG_REPROGRAM; /* force reprogram */
  245. if (has_wrapped) {
  246. /* we're competing with the interrupt handler.
  247. * -> let the interrupt handler reprogram the timer.
  248. * -> interrupt number two handles the event.
  249. */
  250. p->flags |= FLAG_SKIPEVENT;
  251. return;
  252. }
  253. if (absolute)
  254. now = 0;
  255. do {
  256. /* reprogram the timer hardware,
  257. * but don't save the new match value yet.
  258. */
  259. new_match = now + value + delay;
  260. if (new_match > p->max_match_value)
  261. new_match = p->max_match_value;
  262. sh_cmt_write_cmcor(p, new_match);
  263. now = sh_cmt_get_counter(p, &has_wrapped);
  264. if (has_wrapped && (new_match > p->match_value)) {
  265. /* we are changing to a greater match value,
  266. * so this wrap must be caused by the counter
  267. * matching the old value.
  268. * -> first interrupt reprograms the timer.
  269. * -> interrupt number two handles the event.
  270. */
  271. p->flags |= FLAG_SKIPEVENT;
  272. break;
  273. }
  274. if (has_wrapped) {
  275. /* we are changing to a smaller match value,
  276. * so the wrap must be caused by the counter
  277. * matching the new value.
  278. * -> save programmed match value.
  279. * -> let isr handle the event.
  280. */
  281. p->match_value = new_match;
  282. break;
  283. }
  284. /* be safe: verify hardware settings */
  285. if (now < new_match) {
  286. /* timer value is below match value, all good.
  287. * this makes sure we won't miss any match events.
  288. * -> save programmed match value.
  289. * -> let isr handle the event.
  290. */
  291. p->match_value = new_match;
  292. break;
  293. }
  294. /* the counter has reached a value greater
  295. * than our new match value. and since the
  296. * has_wrapped flag isn't set we must have
  297. * programmed a too close event.
  298. * -> increase delay and retry.
  299. */
  300. if (delay)
  301. delay <<= 1;
  302. else
  303. delay = 1;
  304. if (!delay)
  305. dev_warn(&p->pdev->dev, "too long delay\n");
  306. } while (delay);
  307. }
  308. static void __sh_cmt_set_next(struct sh_cmt_priv *p, unsigned long delta)
  309. {
  310. if (delta > p->max_match_value)
  311. dev_warn(&p->pdev->dev, "delta out of range\n");
  312. p->next_match_value = delta;
  313. sh_cmt_clock_event_program_verify(p, 0);
  314. }
  315. static void sh_cmt_set_next(struct sh_cmt_priv *p, unsigned long delta)
  316. {
  317. unsigned long flags;
  318. raw_spin_lock_irqsave(&p->lock, flags);
  319. __sh_cmt_set_next(p, delta);
  320. raw_spin_unlock_irqrestore(&p->lock, flags);
  321. }
  322. static irqreturn_t sh_cmt_interrupt(int irq, void *dev_id)
  323. {
  324. struct sh_cmt_priv *p = dev_id;
  325. /* clear flags */
  326. sh_cmt_write_cmcsr(p, sh_cmt_read_cmcsr(p) & p->clear_bits);
  327. /* update clock source counter to begin with if enabled
  328. * the wrap flag should be cleared by the timer specific
  329. * isr before we end up here.
  330. */
  331. if (p->flags & FLAG_CLOCKSOURCE)
  332. p->total_cycles += p->match_value + 1;
  333. if (!(p->flags & FLAG_REPROGRAM))
  334. p->next_match_value = p->max_match_value;
  335. p->flags |= FLAG_IRQCONTEXT;
  336. if (p->flags & FLAG_CLOCKEVENT) {
  337. if (!(p->flags & FLAG_SKIPEVENT)) {
  338. if (p->ced.mode == CLOCK_EVT_MODE_ONESHOT) {
  339. p->next_match_value = p->max_match_value;
  340. p->flags |= FLAG_REPROGRAM;
  341. }
  342. p->ced.event_handler(&p->ced);
  343. }
  344. }
  345. p->flags &= ~FLAG_SKIPEVENT;
  346. if (p->flags & FLAG_REPROGRAM) {
  347. p->flags &= ~FLAG_REPROGRAM;
  348. sh_cmt_clock_event_program_verify(p, 1);
  349. if (p->flags & FLAG_CLOCKEVENT)
  350. if ((p->ced.mode == CLOCK_EVT_MODE_SHUTDOWN)
  351. || (p->match_value == p->next_match_value))
  352. p->flags &= ~FLAG_REPROGRAM;
  353. }
  354. p->flags &= ~FLAG_IRQCONTEXT;
  355. return IRQ_HANDLED;
  356. }
  357. static int sh_cmt_start(struct sh_cmt_priv *p, unsigned long flag)
  358. {
  359. int ret = 0;
  360. unsigned long flags;
  361. raw_spin_lock_irqsave(&p->lock, flags);
  362. if (!(p->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
  363. ret = sh_cmt_enable(p, &p->rate);
  364. if (ret)
  365. goto out;
  366. p->flags |= flag;
  367. /* setup timeout if no clockevent */
  368. if ((flag == FLAG_CLOCKSOURCE) && (!(p->flags & FLAG_CLOCKEVENT)))
  369. __sh_cmt_set_next(p, p->max_match_value);
  370. out:
  371. raw_spin_unlock_irqrestore(&p->lock, flags);
  372. return ret;
  373. }
  374. static void sh_cmt_stop(struct sh_cmt_priv *p, unsigned long flag)
  375. {
  376. unsigned long flags;
  377. unsigned long f;
  378. raw_spin_lock_irqsave(&p->lock, flags);
  379. f = p->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE);
  380. p->flags &= ~flag;
  381. if (f && !(p->flags & (FLAG_CLOCKEVENT | FLAG_CLOCKSOURCE)))
  382. sh_cmt_disable(p);
  383. /* adjust the timeout to maximum if only clocksource left */
  384. if ((flag == FLAG_CLOCKEVENT) && (p->flags & FLAG_CLOCKSOURCE))
  385. __sh_cmt_set_next(p, p->max_match_value);
  386. raw_spin_unlock_irqrestore(&p->lock, flags);
  387. }
  388. static struct sh_cmt_priv *cs_to_sh_cmt(struct clocksource *cs)
  389. {
  390. return container_of(cs, struct sh_cmt_priv, cs);
  391. }
  392. static cycle_t sh_cmt_clocksource_read(struct clocksource *cs)
  393. {
  394. struct sh_cmt_priv *p = cs_to_sh_cmt(cs);
  395. unsigned long flags, raw;
  396. unsigned long value;
  397. int has_wrapped;
  398. raw_spin_lock_irqsave(&p->lock, flags);
  399. value = p->total_cycles;
  400. raw = sh_cmt_get_counter(p, &has_wrapped);
  401. if (unlikely(has_wrapped))
  402. raw += p->match_value + 1;
  403. raw_spin_unlock_irqrestore(&p->lock, flags);
  404. return value + raw;
  405. }
  406. static int sh_cmt_clocksource_enable(struct clocksource *cs)
  407. {
  408. int ret;
  409. struct sh_cmt_priv *p = cs_to_sh_cmt(cs);
  410. WARN_ON(p->cs_enabled);
  411. p->total_cycles = 0;
  412. ret = sh_cmt_start(p, FLAG_CLOCKSOURCE);
  413. if (!ret) {
  414. __clocksource_updatefreq_hz(cs, p->rate);
  415. p->cs_enabled = true;
  416. }
  417. return ret;
  418. }
  419. static void sh_cmt_clocksource_disable(struct clocksource *cs)
  420. {
  421. struct sh_cmt_priv *p = cs_to_sh_cmt(cs);
  422. WARN_ON(!p->cs_enabled);
  423. sh_cmt_stop(p, FLAG_CLOCKSOURCE);
  424. p->cs_enabled = false;
  425. }
  426. static void sh_cmt_clocksource_suspend(struct clocksource *cs)
  427. {
  428. struct sh_cmt_priv *p = cs_to_sh_cmt(cs);
  429. sh_cmt_stop(p, FLAG_CLOCKSOURCE);
  430. pm_genpd_syscore_poweroff(&p->pdev->dev);
  431. }
  432. static void sh_cmt_clocksource_resume(struct clocksource *cs)
  433. {
  434. struct sh_cmt_priv *p = cs_to_sh_cmt(cs);
  435. pm_genpd_syscore_poweron(&p->pdev->dev);
  436. sh_cmt_start(p, FLAG_CLOCKSOURCE);
  437. }
  438. static int sh_cmt_register_clocksource(struct sh_cmt_priv *p,
  439. char *name, unsigned long rating)
  440. {
  441. struct clocksource *cs = &p->cs;
  442. cs->name = name;
  443. cs->rating = rating;
  444. cs->read = sh_cmt_clocksource_read;
  445. cs->enable = sh_cmt_clocksource_enable;
  446. cs->disable = sh_cmt_clocksource_disable;
  447. cs->suspend = sh_cmt_clocksource_suspend;
  448. cs->resume = sh_cmt_clocksource_resume;
  449. cs->mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8);
  450. cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
  451. dev_info(&p->pdev->dev, "used as clock source\n");
  452. /* Register with dummy 1 Hz value, gets updated in ->enable() */
  453. clocksource_register_hz(cs, 1);
  454. return 0;
  455. }
  456. static struct sh_cmt_priv *ced_to_sh_cmt(struct clock_event_device *ced)
  457. {
  458. return container_of(ced, struct sh_cmt_priv, ced);
  459. }
  460. static void sh_cmt_clock_event_start(struct sh_cmt_priv *p, int periodic)
  461. {
  462. struct clock_event_device *ced = &p->ced;
  463. sh_cmt_start(p, FLAG_CLOCKEVENT);
  464. /* TODO: calculate good shift from rate and counter bit width */
  465. ced->shift = 32;
  466. ced->mult = div_sc(p->rate, NSEC_PER_SEC, ced->shift);
  467. ced->max_delta_ns = clockevent_delta2ns(p->max_match_value, ced);
  468. ced->min_delta_ns = clockevent_delta2ns(0x1f, ced);
  469. if (periodic)
  470. sh_cmt_set_next(p, ((p->rate + HZ/2) / HZ) - 1);
  471. else
  472. sh_cmt_set_next(p, p->max_match_value);
  473. }
  474. static void sh_cmt_clock_event_mode(enum clock_event_mode mode,
  475. struct clock_event_device *ced)
  476. {
  477. struct sh_cmt_priv *p = ced_to_sh_cmt(ced);
  478. /* deal with old setting first */
  479. switch (ced->mode) {
  480. case CLOCK_EVT_MODE_PERIODIC:
  481. case CLOCK_EVT_MODE_ONESHOT:
  482. sh_cmt_stop(p, FLAG_CLOCKEVENT);
  483. break;
  484. default:
  485. break;
  486. }
  487. switch (mode) {
  488. case CLOCK_EVT_MODE_PERIODIC:
  489. dev_info(&p->pdev->dev, "used for periodic clock events\n");
  490. sh_cmt_clock_event_start(p, 1);
  491. break;
  492. case CLOCK_EVT_MODE_ONESHOT:
  493. dev_info(&p->pdev->dev, "used for oneshot clock events\n");
  494. sh_cmt_clock_event_start(p, 0);
  495. break;
  496. case CLOCK_EVT_MODE_SHUTDOWN:
  497. case CLOCK_EVT_MODE_UNUSED:
  498. sh_cmt_stop(p, FLAG_CLOCKEVENT);
  499. break;
  500. default:
  501. break;
  502. }
  503. }
  504. static int sh_cmt_clock_event_next(unsigned long delta,
  505. struct clock_event_device *ced)
  506. {
  507. struct sh_cmt_priv *p = ced_to_sh_cmt(ced);
  508. BUG_ON(ced->mode != CLOCK_EVT_MODE_ONESHOT);
  509. if (likely(p->flags & FLAG_IRQCONTEXT))
  510. p->next_match_value = delta - 1;
  511. else
  512. sh_cmt_set_next(p, delta - 1);
  513. return 0;
  514. }
  515. static void sh_cmt_clock_event_suspend(struct clock_event_device *ced)
  516. {
  517. pm_genpd_syscore_poweroff(&ced_to_sh_cmt(ced)->pdev->dev);
  518. }
  519. static void sh_cmt_clock_event_resume(struct clock_event_device *ced)
  520. {
  521. pm_genpd_syscore_poweron(&ced_to_sh_cmt(ced)->pdev->dev);
  522. }
  523. static void sh_cmt_register_clockevent(struct sh_cmt_priv *p,
  524. char *name, unsigned long rating)
  525. {
  526. struct clock_event_device *ced = &p->ced;
  527. memset(ced, 0, sizeof(*ced));
  528. ced->name = name;
  529. ced->features = CLOCK_EVT_FEAT_PERIODIC;
  530. ced->features |= CLOCK_EVT_FEAT_ONESHOT;
  531. ced->rating = rating;
  532. ced->cpumask = cpumask_of(0);
  533. ced->set_next_event = sh_cmt_clock_event_next;
  534. ced->set_mode = sh_cmt_clock_event_mode;
  535. ced->suspend = sh_cmt_clock_event_suspend;
  536. ced->resume = sh_cmt_clock_event_resume;
  537. dev_info(&p->pdev->dev, "used for clock events\n");
  538. clockevents_register_device(ced);
  539. }
  540. static int sh_cmt_register(struct sh_cmt_priv *p, char *name,
  541. unsigned long clockevent_rating,
  542. unsigned long clocksource_rating)
  543. {
  544. if (clockevent_rating)
  545. sh_cmt_register_clockevent(p, name, clockevent_rating);
  546. if (clocksource_rating)
  547. sh_cmt_register_clocksource(p, name, clocksource_rating);
  548. return 0;
  549. }
  550. static int sh_cmt_setup(struct sh_cmt_priv *p, struct platform_device *pdev)
  551. {
  552. struct sh_timer_config *cfg = pdev->dev.platform_data;
  553. struct resource *res;
  554. int irq, ret;
  555. ret = -ENXIO;
  556. memset(p, 0, sizeof(*p));
  557. p->pdev = pdev;
  558. if (!cfg) {
  559. dev_err(&p->pdev->dev, "missing platform data\n");
  560. goto err0;
  561. }
  562. res = platform_get_resource(p->pdev, IORESOURCE_MEM, 0);
  563. if (!res) {
  564. dev_err(&p->pdev->dev, "failed to get I/O memory\n");
  565. goto err0;
  566. }
  567. irq = platform_get_irq(p->pdev, 0);
  568. if (irq < 0) {
  569. dev_err(&p->pdev->dev, "failed to get irq\n");
  570. goto err0;
  571. }
  572. /* map memory, let mapbase point to our channel */
  573. p->mapbase = ioremap_nocache(res->start, resource_size(res));
  574. if (p->mapbase == NULL) {
  575. dev_err(&p->pdev->dev, "failed to remap I/O memory\n");
  576. goto err0;
  577. }
  578. /* request irq using setup_irq() (too early for request_irq()) */
  579. p->irqaction.name = dev_name(&p->pdev->dev);
  580. p->irqaction.handler = sh_cmt_interrupt;
  581. p->irqaction.dev_id = p;
  582. p->irqaction.flags = IRQF_DISABLED | IRQF_TIMER | \
  583. IRQF_IRQPOLL | IRQF_NOBALANCING;
  584. /* get hold of clock */
  585. p->clk = clk_get(&p->pdev->dev, "cmt_fck");
  586. if (IS_ERR(p->clk)) {
  587. dev_err(&p->pdev->dev, "cannot get clock\n");
  588. ret = PTR_ERR(p->clk);
  589. goto err1;
  590. }
  591. p->read_control = sh_cmt_read16;
  592. p->write_control = sh_cmt_write16;
  593. if (resource_size(res) == 6) {
  594. p->width = 16;
  595. p->read_count = sh_cmt_read16;
  596. p->write_count = sh_cmt_write16;
  597. p->overflow_bit = 0x80;
  598. p->clear_bits = ~0x80;
  599. } else {
  600. p->width = 32;
  601. p->read_count = sh_cmt_read32;
  602. p->write_count = sh_cmt_write32;
  603. p->overflow_bit = 0x8000;
  604. p->clear_bits = ~0xc000;
  605. }
  606. if (p->width == (sizeof(p->max_match_value) * 8))
  607. p->max_match_value = ~0;
  608. else
  609. p->max_match_value = (1 << p->width) - 1;
  610. p->match_value = p->max_match_value;
  611. raw_spin_lock_init(&p->lock);
  612. ret = sh_cmt_register(p, (char *)dev_name(&p->pdev->dev),
  613. cfg->clockevent_rating,
  614. cfg->clocksource_rating);
  615. if (ret) {
  616. dev_err(&p->pdev->dev, "registration failed\n");
  617. goto err2;
  618. }
  619. p->cs_enabled = false;
  620. ret = setup_irq(irq, &p->irqaction);
  621. if (ret) {
  622. dev_err(&p->pdev->dev, "failed to request irq %d\n", irq);
  623. goto err2;
  624. }
  625. platform_set_drvdata(pdev, p);
  626. return 0;
  627. err2:
  628. clk_put(p->clk);
  629. err1:
  630. iounmap(p->mapbase);
  631. err0:
  632. return ret;
  633. }
  634. static int sh_cmt_probe(struct platform_device *pdev)
  635. {
  636. struct sh_cmt_priv *p = platform_get_drvdata(pdev);
  637. struct sh_timer_config *cfg = pdev->dev.platform_data;
  638. int ret;
  639. if (!is_early_platform_device(pdev)) {
  640. pm_runtime_set_active(&pdev->dev);
  641. pm_runtime_enable(&pdev->dev);
  642. }
  643. if (p) {
  644. dev_info(&pdev->dev, "kept as earlytimer\n");
  645. goto out;
  646. }
  647. p = kmalloc(sizeof(*p), GFP_KERNEL);
  648. if (p == NULL) {
  649. dev_err(&pdev->dev, "failed to allocate driver data\n");
  650. return -ENOMEM;
  651. }
  652. ret = sh_cmt_setup(p, pdev);
  653. if (ret) {
  654. kfree(p);
  655. pm_runtime_idle(&pdev->dev);
  656. return ret;
  657. }
  658. if (is_early_platform_device(pdev))
  659. return 0;
  660. out:
  661. if (cfg->clockevent_rating || cfg->clocksource_rating)
  662. pm_runtime_irq_safe(&pdev->dev);
  663. else
  664. pm_runtime_idle(&pdev->dev);
  665. return 0;
  666. }
  667. static int sh_cmt_remove(struct platform_device *pdev)
  668. {
  669. return -EBUSY; /* cannot unregister clockevent and clocksource */
  670. }
  671. static struct platform_driver sh_cmt_device_driver = {
  672. .probe = sh_cmt_probe,
  673. .remove = sh_cmt_remove,
  674. .driver = {
  675. .name = "sh_cmt",
  676. }
  677. };
  678. static int __init sh_cmt_init(void)
  679. {
  680. return platform_driver_register(&sh_cmt_device_driver);
  681. }
  682. static void __exit sh_cmt_exit(void)
  683. {
  684. platform_driver_unregister(&sh_cmt_device_driver);
  685. }
  686. early_platform_init("earlytimer", &sh_cmt_device_driver);
  687. subsys_initcall(sh_cmt_init);
  688. module_exit(sh_cmt_exit);
  689. MODULE_AUTHOR("Magnus Damm");
  690. MODULE_DESCRIPTION("SuperH CMT Timer Driver");
  691. MODULE_LICENSE("GPL v2");