sh_cmt.c 17 KB

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