sh_cmt.c 18 KB

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