sh_cmt.c 24 KB

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