sh_cmt.c 17 KB

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