sh_cmt.c 17 KB

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