sh_cmt.c 17 KB

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