sh_cmt.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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. int ret;
  342. struct sh_cmt_priv *p = cs_to_sh_cmt(cs);
  343. p->total_cycles = 0;
  344. ret = sh_cmt_start(p, FLAG_CLOCKSOURCE);
  345. if (!ret)
  346. __clocksource_updatefreq_hz(cs, p->rate);
  347. return ret;
  348. }
  349. static void sh_cmt_clocksource_disable(struct clocksource *cs)
  350. {
  351. sh_cmt_stop(cs_to_sh_cmt(cs), FLAG_CLOCKSOURCE);
  352. }
  353. static void sh_cmt_clocksource_resume(struct clocksource *cs)
  354. {
  355. sh_cmt_start(cs_to_sh_cmt(cs), FLAG_CLOCKSOURCE);
  356. }
  357. static int sh_cmt_register_clocksource(struct sh_cmt_priv *p,
  358. char *name, unsigned long rating)
  359. {
  360. struct clocksource *cs = &p->cs;
  361. cs->name = name;
  362. cs->rating = rating;
  363. cs->read = sh_cmt_clocksource_read;
  364. cs->enable = sh_cmt_clocksource_enable;
  365. cs->disable = sh_cmt_clocksource_disable;
  366. cs->suspend = sh_cmt_clocksource_disable;
  367. cs->resume = sh_cmt_clocksource_resume;
  368. cs->mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8);
  369. cs->flags = CLOCK_SOURCE_IS_CONTINUOUS;
  370. dev_info(&p->pdev->dev, "used as clock source\n");
  371. /* Register with dummy 1 Hz value, gets updated in ->enable() */
  372. clocksource_register_hz(cs, 1);
  373. return 0;
  374. }
  375. static struct sh_cmt_priv *ced_to_sh_cmt(struct clock_event_device *ced)
  376. {
  377. return container_of(ced, struct sh_cmt_priv, ced);
  378. }
  379. static void sh_cmt_clock_event_start(struct sh_cmt_priv *p, int periodic)
  380. {
  381. struct clock_event_device *ced = &p->ced;
  382. sh_cmt_start(p, FLAG_CLOCKEVENT);
  383. /* TODO: calculate good shift from rate and counter bit width */
  384. ced->shift = 32;
  385. ced->mult = div_sc(p->rate, NSEC_PER_SEC, ced->shift);
  386. ced->max_delta_ns = clockevent_delta2ns(p->max_match_value, ced);
  387. ced->min_delta_ns = clockevent_delta2ns(0x1f, ced);
  388. if (periodic)
  389. sh_cmt_set_next(p, ((p->rate + HZ/2) / HZ) - 1);
  390. else
  391. sh_cmt_set_next(p, p->max_match_value);
  392. }
  393. static void sh_cmt_clock_event_mode(enum clock_event_mode mode,
  394. struct clock_event_device *ced)
  395. {
  396. struct sh_cmt_priv *p = ced_to_sh_cmt(ced);
  397. /* deal with old setting first */
  398. switch (ced->mode) {
  399. case CLOCK_EVT_MODE_PERIODIC:
  400. case CLOCK_EVT_MODE_ONESHOT:
  401. sh_cmt_stop(p, FLAG_CLOCKEVENT);
  402. break;
  403. default:
  404. break;
  405. }
  406. switch (mode) {
  407. case CLOCK_EVT_MODE_PERIODIC:
  408. dev_info(&p->pdev->dev, "used for periodic clock events\n");
  409. sh_cmt_clock_event_start(p, 1);
  410. break;
  411. case CLOCK_EVT_MODE_ONESHOT:
  412. dev_info(&p->pdev->dev, "used for oneshot clock events\n");
  413. sh_cmt_clock_event_start(p, 0);
  414. break;
  415. case CLOCK_EVT_MODE_SHUTDOWN:
  416. case CLOCK_EVT_MODE_UNUSED:
  417. sh_cmt_stop(p, FLAG_CLOCKEVENT);
  418. break;
  419. default:
  420. break;
  421. }
  422. }
  423. static int sh_cmt_clock_event_next(unsigned long delta,
  424. struct clock_event_device *ced)
  425. {
  426. struct sh_cmt_priv *p = ced_to_sh_cmt(ced);
  427. BUG_ON(ced->mode != CLOCK_EVT_MODE_ONESHOT);
  428. if (likely(p->flags & FLAG_IRQCONTEXT))
  429. p->next_match_value = delta - 1;
  430. else
  431. sh_cmt_set_next(p, delta - 1);
  432. return 0;
  433. }
  434. static void sh_cmt_register_clockevent(struct sh_cmt_priv *p,
  435. char *name, unsigned long rating)
  436. {
  437. struct clock_event_device *ced = &p->ced;
  438. memset(ced, 0, sizeof(*ced));
  439. ced->name = name;
  440. ced->features = CLOCK_EVT_FEAT_PERIODIC;
  441. ced->features |= CLOCK_EVT_FEAT_ONESHOT;
  442. ced->rating = rating;
  443. ced->cpumask = cpumask_of(0);
  444. ced->set_next_event = sh_cmt_clock_event_next;
  445. ced->set_mode = sh_cmt_clock_event_mode;
  446. dev_info(&p->pdev->dev, "used for clock events\n");
  447. clockevents_register_device(ced);
  448. }
  449. static int sh_cmt_register(struct sh_cmt_priv *p, char *name,
  450. unsigned long clockevent_rating,
  451. unsigned long clocksource_rating)
  452. {
  453. if (p->width == (sizeof(p->max_match_value) * 8))
  454. p->max_match_value = ~0;
  455. else
  456. p->max_match_value = (1 << p->width) - 1;
  457. p->match_value = p->max_match_value;
  458. spin_lock_init(&p->lock);
  459. if (clockevent_rating)
  460. sh_cmt_register_clockevent(p, name, clockevent_rating);
  461. if (clocksource_rating)
  462. sh_cmt_register_clocksource(p, name, clocksource_rating);
  463. return 0;
  464. }
  465. static int sh_cmt_setup(struct sh_cmt_priv *p, struct platform_device *pdev)
  466. {
  467. struct sh_timer_config *cfg = pdev->dev.platform_data;
  468. struct resource *res;
  469. int irq, ret;
  470. ret = -ENXIO;
  471. memset(p, 0, sizeof(*p));
  472. p->pdev = pdev;
  473. if (!cfg) {
  474. dev_err(&p->pdev->dev, "missing platform data\n");
  475. goto err0;
  476. }
  477. platform_set_drvdata(pdev, p);
  478. res = platform_get_resource(p->pdev, IORESOURCE_MEM, 0);
  479. if (!res) {
  480. dev_err(&p->pdev->dev, "failed to get I/O memory\n");
  481. goto err0;
  482. }
  483. irq = platform_get_irq(p->pdev, 0);
  484. if (irq < 0) {
  485. dev_err(&p->pdev->dev, "failed to get irq\n");
  486. goto err0;
  487. }
  488. /* map memory, let mapbase point to our channel */
  489. p->mapbase = ioremap_nocache(res->start, resource_size(res));
  490. if (p->mapbase == NULL) {
  491. dev_err(&p->pdev->dev, "failed to remap I/O memory\n");
  492. goto err0;
  493. }
  494. /* request irq using setup_irq() (too early for request_irq()) */
  495. p->irqaction.name = dev_name(&p->pdev->dev);
  496. p->irqaction.handler = sh_cmt_interrupt;
  497. p->irqaction.dev_id = p;
  498. p->irqaction.flags = IRQF_DISABLED | IRQF_TIMER | \
  499. IRQF_IRQPOLL | IRQF_NOBALANCING;
  500. /* get hold of clock */
  501. p->clk = clk_get(&p->pdev->dev, "cmt_fck");
  502. if (IS_ERR(p->clk)) {
  503. dev_err(&p->pdev->dev, "cannot get clock\n");
  504. ret = PTR_ERR(p->clk);
  505. goto err1;
  506. }
  507. if (resource_size(res) == 6) {
  508. p->width = 16;
  509. p->overflow_bit = 0x80;
  510. p->clear_bits = ~0x80;
  511. } else {
  512. p->width = 32;
  513. p->overflow_bit = 0x8000;
  514. p->clear_bits = ~0xc000;
  515. }
  516. ret = sh_cmt_register(p, (char *)dev_name(&p->pdev->dev),
  517. cfg->clockevent_rating,
  518. cfg->clocksource_rating);
  519. if (ret) {
  520. dev_err(&p->pdev->dev, "registration failed\n");
  521. goto err1;
  522. }
  523. ret = setup_irq(irq, &p->irqaction);
  524. if (ret) {
  525. dev_err(&p->pdev->dev, "failed to request irq %d\n", irq);
  526. goto err1;
  527. }
  528. return 0;
  529. err1:
  530. iounmap(p->mapbase);
  531. err0:
  532. return ret;
  533. }
  534. static int __devinit sh_cmt_probe(struct platform_device *pdev)
  535. {
  536. struct sh_cmt_priv *p = platform_get_drvdata(pdev);
  537. int ret;
  538. if (p) {
  539. dev_info(&pdev->dev, "kept as earlytimer\n");
  540. return 0;
  541. }
  542. p = kmalloc(sizeof(*p), GFP_KERNEL);
  543. if (p == NULL) {
  544. dev_err(&pdev->dev, "failed to allocate driver data\n");
  545. return -ENOMEM;
  546. }
  547. ret = sh_cmt_setup(p, pdev);
  548. if (ret) {
  549. kfree(p);
  550. platform_set_drvdata(pdev, NULL);
  551. }
  552. return ret;
  553. }
  554. static int __devexit sh_cmt_remove(struct platform_device *pdev)
  555. {
  556. return -EBUSY; /* cannot unregister clockevent and clocksource */
  557. }
  558. static struct platform_driver sh_cmt_device_driver = {
  559. .probe = sh_cmt_probe,
  560. .remove = __devexit_p(sh_cmt_remove),
  561. .driver = {
  562. .name = "sh_cmt",
  563. }
  564. };
  565. static int __init sh_cmt_init(void)
  566. {
  567. return platform_driver_register(&sh_cmt_device_driver);
  568. }
  569. static void __exit sh_cmt_exit(void)
  570. {
  571. platform_driver_unregister(&sh_cmt_device_driver);
  572. }
  573. early_platform_init("earlytimer", &sh_cmt_device_driver);
  574. module_init(sh_cmt_init);
  575. module_exit(sh_cmt_exit);
  576. MODULE_AUTHOR("Magnus Damm");
  577. MODULE_DESCRIPTION("SuperH CMT Timer Driver");
  578. MODULE_LICENSE("GPL v2");