sh_cmt.c 17 KB

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