time.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. #include <linux/kernel.h>
  2. #include <linux/init.h>
  3. #include <linux/interrupt.h>
  4. #include <linux/irq.h>
  5. #include <linux/io.h>
  6. #include <linux/mm.h>
  7. #include <linux/clockchips.h>
  8. #include <linux/clocksource.h>
  9. #include <asm/memory.h>
  10. #include <asm/mach/map.h>
  11. #include <asm/mach/time.h>
  12. #include <asm/sched_clock.h>
  13. #include <plat/regops.h>
  14. #include <mach/map.h>
  15. #include <mach/hardware.h>
  16. #include <mach/reg_addr.h>
  17. /***********************************************************************
  18. * System timer
  19. **********************************************************************/
  20. #define TIMER_E_INPUT_BIT 8
  21. #define TIMER_D_INPUT_BIT 6
  22. #define TIMER_C_INPUT_BIT 4
  23. #define TIMER_B_INPUT_BIT 2
  24. #define TIMER_A_INPUT_BIT 0
  25. #define TIMER_E_INPUT_MASK (7UL << TIMER_E_INPUT_BIT)
  26. #define TIMER_D_INPUT_MASK (3UL << TIMER_D_INPUT_BIT)
  27. #define TIMER_C_INPUT_MASK (3UL << TIMER_C_INPUT_BIT)
  28. #define TIMER_B_INPUT_MASK (3UL << TIMER_B_INPUT_BIT)
  29. #define TIMER_A_INPUT_MASK (3UL << TIMER_A_INPUT_BIT)
  30. #define TIMER_UNIT_1us 0
  31. #define TIMER_UNIT_10us 1
  32. #define TIMER_UNIT_100us 2
  33. #define TIMER_UNIT_1ms 3
  34. #define TIMERE_UNIT_SYS 0
  35. #define TIMERE_UNIT_1us 1
  36. #define TIMERE_UNIT_10us 2
  37. #define TIMERE_UNIT_100us 3
  38. #define TIMERE_UNIT_1ms 4
  39. #define TIMER_A_ENABLE_BIT 16
  40. #define TIMER_E_ENABLE_BIT 20
  41. #define TIMER_A_PERIODIC_BIT 12
  42. /********** Clock Source Device, Timer-E *********/
  43. static cycle_t cycle_read_timerE(struct clocksource *cs)
  44. {
  45. return (cycles_t) aml_read_reg32(P_ISA_TIMERE);
  46. }
  47. static struct clocksource clocksource_timer_e = {
  48. .name = "Timer-E",
  49. .rating = 300,
  50. .read = cycle_read_timerE,
  51. .mask = CLOCKSOURCE_MASK(32),
  52. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  53. };
  54. #if CONFIG_HAVE_SCHED_CLOCK
  55. static DEFINE_CLOCK_DATA(cd);
  56. /*
  57. * sched_clock()
  58. */
  59. unsigned long long notrace sched_clock(void)
  60. {
  61. struct clocksource *cs = &clocksource_timer_e;
  62. u32 cyc = cycle_read_timerE(NULL);
  63. return cyc_to_fixed_sched_clock(&cd, cyc, (u32)~0, cs->mult, cs->shift);
  64. }
  65. static void notrace meson6_update_sched_clock(void)
  66. {
  67. u32 cyc = cycle_read_timerE(NULL);
  68. update_sched_clock(&cd, cyc, (u32)~0);
  69. }
  70. static void __init meson_clocksource_init(void)
  71. {
  72. aml_clr_reg32_mask(P_ISA_TIMER_MUX, TIMER_E_INPUT_MASK);
  73. aml_set_reg32_mask(P_ISA_TIMER_MUX, TIMERE_UNIT_1us << TIMER_E_INPUT_BIT);
  74. /// aml_write_reg32(P_ISA_TIMERE, 0);
  75. /**
  76. * (counter*mult)>>shift=xxx ns
  77. */
  78. /**
  79. * Constants generated by clocks_calc_mult_shift(m, s, 1MHz, NSEC_PER_SEC, 0).
  80. * This gives a resolution of about 1us and a wrap period of about 1h11min.
  81. */
  82. clocksource_timer_e.shift = 22;
  83. clocksource_timer_e.mult = 4194304000u;
  84. clocksource_register(&clocksource_timer_e);
  85. init_fixed_sched_clock(&cd, meson6_update_sched_clock, 32,
  86. USEC_PER_SEC,
  87. clocksource_timer_e.mult,
  88. clocksource_timer_e.shift);
  89. }
  90. #else
  91. static void __init meson_clocksource_init(void)
  92. {
  93. aml_clr_reg32_mask(P_ISA_TIMER_MUX, TIMER_E_INPUT_MASK);
  94. aml_set_reg32_mask(P_ISA_TIMER_MUX, TIMERE_UNIT_1us << TIMER_E_INPUT_BIT);
  95. /// aml_write_reg32(P_ISA_TIMERE, 0);
  96. /**
  97. * (counter*mult)>>shift=xxx ns
  98. */
  99. clocksource_timer_e.shift = 0;
  100. clocksource_timer_e.mult = 1000;
  101. clocksource_register(&clocksource_timer_e);
  102. }
  103. /*
  104. * sched_clock()
  105. */
  106. unsigned long long sched_clock(void)
  107. {
  108. static unsigned long last_timeE=0;
  109. static cycle_t cyc=0;
  110. struct clocksource *cs = &clocksource_timer_e;
  111. unsigned long cur;
  112. cur=cycle_read_timerE(NULL);
  113. cyc += cur - last_timeE;
  114. last_timeE=cur;
  115. return clocksource_cyc2ns(cyc, cs->mult, cs->shift);
  116. }
  117. #endif
  118. /********** Clock Event Device, Timer-ABCD *********/
  119. #define MESON_TIMERA 0
  120. #define MESON_TIMERB 1
  121. #define MESON_TIMERC 2
  122. #define MESON_TIMERD 3
  123. struct meson_clock {
  124. struct clock_event_device clockevent;
  125. struct irqaction irq;
  126. unsigned id;
  127. unsigned mux_reg;
  128. unsigned reg;
  129. };
  130. static struct meson_clock *clockevent_to_clock(struct clock_event_device *evt)
  131. {
  132. return container_of(evt, struct meson_clock, clockevent);
  133. }
  134. static void meson_clkevt_set_mode(enum clock_event_mode mode,
  135. struct clock_event_device *dev)
  136. {
  137. struct meson_clock * clk=clockevent_to_clock(dev);
  138. switch (mode) {
  139. case CLOCK_EVT_MODE_RESUME:
  140. printk("Resume timer%c\n",'A'+clk->id);
  141. aml_set_reg32_mask(clk->mux_reg, 0x1<<(TIMER_A_ENABLE_BIT+clk->id));
  142. break;
  143. case CLOCK_EVT_MODE_PERIODIC:
  144. aml_set_reg32_mask(clk->mux_reg, 0x1<<(TIMER_A_PERIODIC_BIT+clk->id));
  145. aml_set_reg32_mask(clk->mux_reg, 0x1<<(TIMER_A_ENABLE_BIT+clk->id));
  146. break;
  147. case CLOCK_EVT_MODE_ONESHOT:
  148. aml_clr_reg32_mask(clk->mux_reg, 0x1<<(TIMER_A_PERIODIC_BIT+clk->id));
  149. aml_set_reg32_mask(clk->mux_reg, 0x1<<(TIMER_A_ENABLE_BIT+clk->id));
  150. break;
  151. case CLOCK_EVT_MODE_SHUTDOWN:
  152. case CLOCK_EVT_MODE_UNUSED:
  153. printk("Disable timer%c\n",'A'+clk->id);
  154. aml_clr_reg32_mask(clk->mux_reg, 0x1<<(TIMER_A_ENABLE_BIT+clk->id));
  155. break;
  156. }
  157. }
  158. static int meson_set_next_event(unsigned long evt,
  159. struct clock_event_device *dev)
  160. {
  161. struct meson_clock * clk=clockevent_to_clock(dev);
  162. /* use a big number to clear previous trigger cleanly */
  163. aml_set_reg32_mask(clk->reg, evt & 0xffff);
  164. /* then set next event */
  165. aml_set_reg32_bits(clk->reg, evt, 0, 16);
  166. return 0;
  167. }
  168. #if (defined CONFIG_SMP)&& !(defined CONFIG_HAVE_ARM_TWD)
  169. #if (defined CONFIG_MESON_TIMERB ) || (defined CONFIG_MESON_TIMERD )
  170. #error "under smp build, if you deselect HAVE_ARM_TWD , you should not enable TIMERB and TIMERD"
  171. #endif
  172. static void meson_tick_set_mode(enum clock_event_mode mode,
  173. struct clock_event_device *dev);
  174. static int meson_tick_set_next_event(unsigned long evt,
  175. struct clock_event_device *dev);
  176. #define meson_tick_rating 450
  177. //{
  178. // int timer =2*smp_processor_id()+1;
  179. // meson_clkevt_set_mode(mode,&)
  180. //}
  181. #else
  182. #define meson_tick_set_mode meson_clkevt_set_mode
  183. #define meson_tick_set_next_event meson_set_next_event
  184. #define meson_tick_rating 300
  185. #endif
  186. /* Clock event timer interrupt handler */
  187. static irqreturn_t meson_timer_interrupt(int irq, void *dev_id);
  188. #if 0
  189. static struct clock_event_device clockevent_meson_1mhz = {
  190. .name = "TIMER-AC",
  191. .rating = 300, /* Reasonably fast and accurate clock event */
  192. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  193. .shift = 20,
  194. .set_next_event = meson_set_next_event,
  195. .set_mode = meson_clkevt_set_mode,
  196. };
  197. static struct irqaction meson_timer_irq = {
  198. .name = "Meson Timer Tick",
  199. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  200. .handler = meson_timer_interrupt,
  201. };
  202. #endif
  203. static struct meson_clock meson_timers[]={
  204. [MESON_TIMERA]={
  205. .clockevent={
  206. .name = "MESON TIMER-A",
  207. .rating = 400, /* Reasonably fast and accurate clock event */
  208. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  209. .shift = 20,
  210. .set_next_event = meson_set_next_event,
  211. .set_mode = meson_clkevt_set_mode,
  212. },
  213. .irq={
  214. .name = "MESON TIMER-A",
  215. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  216. .handler = meson_timer_interrupt,
  217. .irq =INT_TIMER_A,
  218. },
  219. .id=0,
  220. .mux_reg=P_ISA_TIMER_MUX,
  221. .reg=P_ISA_TIMERA
  222. },
  223. [MESON_TIMERB]=
  224. {
  225. .clockevent={
  226. .name = "MESON TIMER-B",
  227. .rating = meson_tick_rating, /* Reasonably fast and accurate clock event */
  228. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  229. .shift = 20,
  230. .set_next_event = meson_tick_set_next_event,
  231. .set_mode = meson_tick_set_mode,
  232. },
  233. .irq={
  234. .name = "MESON TIMER-B",
  235. .flags = IRQF_TIMER | IRQF_NOBALANCING,
  236. .handler = meson_timer_interrupt,
  237. .irq =INT_TIMER_B,
  238. },
  239. .id=1,
  240. .mux_reg=P_ISA_TIMER_MUX,
  241. .reg=P_ISA_TIMERB
  242. },
  243. [MESON_TIMERC]=
  244. {
  245. .clockevent={
  246. .name = "MESON TIMER-C",
  247. .rating = meson_tick_rating, /* Reasonably fast and accurate clock event */
  248. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  249. .shift = 20,
  250. .set_next_event = meson_set_next_event,
  251. .set_mode = meson_clkevt_set_mode,
  252. },
  253. .irq={
  254. .name = "MESON TIMER-C",
  255. .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  256. .handler = meson_timer_interrupt,
  257. .irq =INT_TIMER_C,
  258. },
  259. .id=2,
  260. .mux_reg=P_ISA_TIMER_MUX,
  261. .reg=P_ISA_TIMERC
  262. },
  263. [MESON_TIMERD]=
  264. {
  265. .clockevent={
  266. .name = "MESON TIMER-D",
  267. .rating = 400, /* Reasonably fast and accurate clock event */
  268. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  269. .shift = 20,
  270. .set_next_event = meson_tick_set_next_event,
  271. .set_mode = meson_tick_set_mode,
  272. },
  273. .irq={
  274. .name = "MESON TIMER-D",
  275. .flags = IRQF_TIMER | IRQF_NOBALANCING,
  276. .handler = meson_timer_interrupt,
  277. .irq =INT_TIMER_D,
  278. },
  279. .id=3,
  280. .mux_reg=P_ISA_TIMER_MUX,
  281. .reg=P_ISA_TIMERD
  282. }
  283. };
  284. static irqreturn_t meson_timer_interrupt(int irq, void *dev_id)
  285. {
  286. struct clock_event_device *evt = dev_id;
  287. if(evt==NULL||evt->event_handler==NULL)
  288. {
  289. WARN_ONCE(evt==NULL||evt->event_handler==NULL,"%p %s %p %d",evt,evt?evt->name:NULL,evt?evt->event_handler:NULL,irq);
  290. return IRQ_HANDLED;
  291. }
  292. evt->event_handler(evt);
  293. return IRQ_HANDLED;
  294. }
  295. static void __cpuinit meson_timer_init_device(struct clock_event_device *evt)
  296. {
  297. printk("%s %p\n",evt->name,evt);
  298. evt->mult=div_sc(1000000, NSEC_PER_SEC, 20);
  299. evt->max_delta_ns =
  300. clockevent_delta2ns(0xfffe, evt);
  301. evt->min_delta_ns=clockevent_delta2ns(1, evt);
  302. evt->cpumask = cpumask_of(smp_processor_id());
  303. }
  304. static void __init meson_timer_setup(struct clock_event_device *evt, unsigned i )
  305. {
  306. struct meson_clock * clk;
  307. clk=&meson_timers[i];
  308. /**
  309. * Enable Timer and setting the time base;
  310. */
  311. aml_set_reg32_mask(clk->mux_reg,
  312. (1<<(TIMER_A_ENABLE_BIT+clk->id))
  313. |(1<<(TIMER_A_PERIODIC_BIT+clk->id))
  314. |(TIMER_UNIT_1us << (TIMER_A_INPUT_BIT+clk->id*2))
  315. );
  316. aml_write_reg32(clk->reg, 9999);
  317. meson_timer_init_device(&clk->clockevent);
  318. clk->irq.dev_id=&clk->clockevent;
  319. clockevents_register_device(&(clk->clockevent));
  320. /* Set up the IRQ handler */
  321. setup_irq(clk->irq.irq, &clk->irq);
  322. }
  323. static void __init meson_clockevent_init(void)
  324. {
  325. int i;
  326. struct meson_clock * clk;
  327. /***
  328. * Disable Timer A~D, and clean the time base
  329. * Now all of the timer is 1us base
  330. */
  331. aml_clr_reg32_mask(P_ISA_TIMER_MUX,~(TIMER_E_INPUT_MASK));
  332. #ifdef CONFIG_MESON_TIMERA
  333. meson_timer_setup(NULL,MESON_TIMERA);
  334. #endif
  335. #ifdef CONFIG_MESON_TIMERB
  336. meson_timer_setup(NULL,MESON_TIMERB);
  337. #endif
  338. #ifdef CONFIG_MESON_TIMERC
  339. meson_timer_setup(NULL,MESON_TIMERC);
  340. #endif
  341. #ifdef CONFIG_MESON_TIMERD
  342. meson_timer_setup(NULL,MESON_TIMERD);
  343. #endif
  344. }
  345. #ifdef CONFIG_SMP
  346. /***********************************************************************
  347. * ARM TWD System timer
  348. **********************************************************************/
  349. #include <asm/localtimer.h>
  350. #ifdef CONFIG_HAVE_ARM_TWD
  351. #include <asm/smp_twd.h>
  352. int __cpuinit local_timer_setup(struct clock_event_device *evt)
  353. {
  354. evt->irq = 29;
  355. twd_timer_setup(evt);
  356. return 0;
  357. }
  358. static void meson_twd_private_timer_init(void)
  359. {
  360. twd_base=(void __iomem *)((IO_PERIPH_BASE+0x600));
  361. }
  362. #else///CONFIG_HAVE_ARM_TWD
  363. #if (defined CONFIG_MESON_TIMERB ) || (defined CONFIG_MESON_TIMERD )
  364. #error "under smp build, if you deselect HAVE_ARM_TWD , you should not enable TIMERB and TIMERD"
  365. #endif
  366. static void meson_tick_set_mode(enum clock_event_mode mode,
  367. struct clock_event_device *dev)
  368. {
  369. int timer =(2*smp_processor_id())+1;
  370. meson_clkevt_set_mode(mode,&meson_timers[timer].clockevent);
  371. }
  372. static int meson_tick_set_next_event(unsigned long evt,
  373. struct clock_event_device *dev)
  374. {
  375. int timer =2*smp_processor_id()+1;
  376. return meson_set_next_event(evt,&meson_timers[timer].clockevent);
  377. }
  378. int __cpuinit local_timer_setup(struct clock_event_device *evt)
  379. {
  380. int cpu=smp_processor_id();
  381. int timer=2*cpu+1;
  382. struct meson_clock * clk;
  383. clk=&meson_timers[timer];
  384. aml_set_reg32_mask(clk->mux_reg,
  385. (1<<(TIMER_A_ENABLE_BIT+clk->id))
  386. |(1<<(TIMER_A_PERIODIC_BIT+clk->id))
  387. |(TIMER_UNIT_1us << (TIMER_A_INPUT_BIT+clk->id*2))
  388. );
  389. aml_write_reg32(clk->reg, 9999);
  390. meson_timer_init_device(&(clk->clockevent));
  391. *evt=clk->clockevent;
  392. clk->irq.dev_id=evt;
  393. clockevents_register_device(evt);
  394. if(cpu)
  395. {
  396. irq_set_affinity(clk->irq.irq, cpumask_of(cpu));
  397. }
  398. /* Set up the IRQ handler */
  399. setup_irq(clk->irq.irq, &clk->irq);
  400. /**
  401. * Enable Timer and setting the time base;
  402. */
  403. return 0;
  404. }
  405. inline int local_timer_ack(void)
  406. {
  407. return 1;
  408. }
  409. #endif///CONFIG_HAVE_ARM_TWD
  410. #endif///CONFIG_SMP
  411. /*
  412. * This sets up the system timers, clock source and clock event.
  413. */
  414. static void __init meson_timer_init(void)
  415. {
  416. meson_clocksource_init();
  417. meson_clockevent_init();
  418. #ifdef CONFIG_SMP
  419. #ifdef CONFIG_HAVE_ARM_TWD
  420. meson_twd_private_timer_init();
  421. #endif
  422. #endif
  423. }
  424. struct sys_timer meson_sys_timer = {
  425. .init = meson_timer_init,
  426. };