time_32.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /* linux/arch/sparc/kernel/time.c
  2. *
  3. * Copyright (C) 1995 David S. Miller (davem@davemloft.net)
  4. * Copyright (C) 1996 Thomas K. Dyas (tdyas@eden.rutgers.edu)
  5. *
  6. * Chris Davis (cdavis@cois.on.ca) 03/27/1998
  7. * Added support for the intersil on the sun4/4200
  8. *
  9. * Gleb Raiko (rajko@mech.math.msu.su) 08/18/1998
  10. * Support for MicroSPARC-IIep, PCI CPU.
  11. *
  12. * This file handles the Sparc specific time handling details.
  13. *
  14. * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
  15. * "A Kernel Model for Precision Timekeeping" by Dave Mills
  16. */
  17. #include <linux/errno.h>
  18. #include <linux/module.h>
  19. #include <linux/sched.h>
  20. #include <linux/kernel.h>
  21. #include <linux/param.h>
  22. #include <linux/string.h>
  23. #include <linux/mm.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/time.h>
  26. #include <linux/rtc/m48t59.h>
  27. #include <linux/timex.h>
  28. #include <linux/clocksource.h>
  29. #include <linux/clockchips.h>
  30. #include <linux/init.h>
  31. #include <linux/pci.h>
  32. #include <linux/ioport.h>
  33. #include <linux/profile.h>
  34. #include <linux/of.h>
  35. #include <linux/of_device.h>
  36. #include <linux/platform_device.h>
  37. #include <asm/mc146818rtc.h>
  38. #include <asm/oplib.h>
  39. #include <asm/timex.h>
  40. #include <asm/timer.h>
  41. #include <asm/irq.h>
  42. #include <asm/io.h>
  43. #include <asm/idprom.h>
  44. #include <asm/page.h>
  45. #include <asm/pcic.h>
  46. #include <asm/irq_regs.h>
  47. #include <asm/setup.h>
  48. #include "kernel.h"
  49. #include "irq.h"
  50. static __cacheline_aligned_in_smp DEFINE_SEQLOCK(timer_cs_lock);
  51. static __volatile__ u64 timer_cs_internal_counter = 0;
  52. static char timer_cs_enabled = 0;
  53. static struct clock_event_device timer_ce;
  54. static char timer_ce_enabled = 0;
  55. #ifdef CONFIG_SMP
  56. DEFINE_PER_CPU(struct clock_event_device, sparc32_clockevent);
  57. #endif
  58. DEFINE_SPINLOCK(rtc_lock);
  59. EXPORT_SYMBOL(rtc_lock);
  60. unsigned long profile_pc(struct pt_regs *regs)
  61. {
  62. extern char __copy_user_begin[], __copy_user_end[];
  63. extern char __bzero_begin[], __bzero_end[];
  64. unsigned long pc = regs->pc;
  65. if (in_lock_functions(pc) ||
  66. (pc >= (unsigned long) __copy_user_begin &&
  67. pc < (unsigned long) __copy_user_end) ||
  68. (pc >= (unsigned long) __bzero_begin &&
  69. pc < (unsigned long) __bzero_end))
  70. pc = regs->u_regs[UREG_RETPC];
  71. return pc;
  72. }
  73. EXPORT_SYMBOL(profile_pc);
  74. volatile u32 __iomem *master_l10_counter;
  75. irqreturn_t notrace timer_interrupt(int dummy, void *dev_id)
  76. {
  77. if (timer_cs_enabled) {
  78. write_seqlock(&timer_cs_lock);
  79. timer_cs_internal_counter++;
  80. sparc_config.clear_clock_irq();
  81. write_sequnlock(&timer_cs_lock);
  82. } else {
  83. sparc_config.clear_clock_irq();
  84. }
  85. if (timer_ce_enabled)
  86. timer_ce.event_handler(&timer_ce);
  87. return IRQ_HANDLED;
  88. }
  89. static int timer_ce_shutdown(struct clock_event_device *evt)
  90. {
  91. timer_ce_enabled = 0;
  92. smp_mb();
  93. return 0;
  94. }
  95. static int timer_ce_set_periodic(struct clock_event_device *evt)
  96. {
  97. timer_ce_enabled = 1;
  98. smp_mb();
  99. return 0;
  100. }
  101. static __init void setup_timer_ce(void)
  102. {
  103. struct clock_event_device *ce = &timer_ce;
  104. BUG_ON(smp_processor_id() != boot_cpu_id);
  105. ce->name = "timer_ce";
  106. ce->rating = 100;
  107. ce->features = CLOCK_EVT_FEAT_PERIODIC;
  108. ce->set_state_shutdown = timer_ce_shutdown;
  109. ce->set_state_periodic = timer_ce_set_periodic;
  110. ce->tick_resume = timer_ce_set_periodic;
  111. ce->cpumask = cpu_possible_mask;
  112. ce->shift = 32;
  113. ce->mult = div_sc(sparc_config.clock_rate, NSEC_PER_SEC,
  114. ce->shift);
  115. clockevents_register_device(ce);
  116. }
  117. static unsigned int sbus_cycles_offset(void)
  118. {
  119. u32 val, offset;
  120. val = sbus_readl(master_l10_counter);
  121. offset = (val >> TIMER_VALUE_SHIFT) & TIMER_VALUE_MASK;
  122. /* Limit hit? */
  123. if (val & TIMER_LIMIT_BIT)
  124. offset += sparc_config.cs_period;
  125. return offset;
  126. }
  127. static cycle_t timer_cs_read(struct clocksource *cs)
  128. {
  129. unsigned int seq, offset;
  130. u64 cycles;
  131. do {
  132. seq = read_seqbegin(&timer_cs_lock);
  133. cycles = timer_cs_internal_counter;
  134. offset = sparc_config.get_cycles_offset();
  135. } while (read_seqretry(&timer_cs_lock, seq));
  136. /* Count absolute cycles */
  137. cycles *= sparc_config.cs_period;
  138. cycles += offset;
  139. return cycles;
  140. }
  141. static struct clocksource timer_cs = {
  142. .name = "timer_cs",
  143. .rating = 100,
  144. .read = timer_cs_read,
  145. .mask = CLOCKSOURCE_MASK(64),
  146. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  147. };
  148. static __init int setup_timer_cs(void)
  149. {
  150. timer_cs_enabled = 1;
  151. return clocksource_register_hz(&timer_cs, sparc_config.clock_rate);
  152. }
  153. #ifdef CONFIG_SMP
  154. static int percpu_ce_shutdown(struct clock_event_device *evt)
  155. {
  156. int cpu = cpumask_first(evt->cpumask);
  157. sparc_config.load_profile_irq(cpu, 0);
  158. return 0;
  159. }
  160. static int percpu_ce_set_periodic(struct clock_event_device *evt)
  161. {
  162. int cpu = cpumask_first(evt->cpumask);
  163. sparc_config.load_profile_irq(cpu, SBUS_CLOCK_RATE / HZ);
  164. return 0;
  165. }
  166. static int percpu_ce_set_next_event(unsigned long delta,
  167. struct clock_event_device *evt)
  168. {
  169. int cpu = cpumask_first(evt->cpumask);
  170. unsigned int next = (unsigned int)delta;
  171. sparc_config.load_profile_irq(cpu, next);
  172. return 0;
  173. }
  174. void register_percpu_ce(int cpu)
  175. {
  176. struct clock_event_device *ce = &per_cpu(sparc32_clockevent, cpu);
  177. unsigned int features = CLOCK_EVT_FEAT_PERIODIC;
  178. if (sparc_config.features & FEAT_L14_ONESHOT)
  179. features |= CLOCK_EVT_FEAT_ONESHOT;
  180. ce->name = "percpu_ce";
  181. ce->rating = 200;
  182. ce->features = features;
  183. ce->set_state_shutdown = percpu_ce_shutdown;
  184. ce->set_state_periodic = percpu_ce_set_periodic;
  185. ce->set_state_oneshot = percpu_ce_shutdown;
  186. ce->set_next_event = percpu_ce_set_next_event;
  187. ce->cpumask = cpumask_of(cpu);
  188. ce->shift = 32;
  189. ce->mult = div_sc(sparc_config.clock_rate, NSEC_PER_SEC,
  190. ce->shift);
  191. ce->max_delta_ns = clockevent_delta2ns(sparc_config.clock_rate, ce);
  192. ce->min_delta_ns = clockevent_delta2ns(100, ce);
  193. clockevents_register_device(ce);
  194. }
  195. #endif
  196. static unsigned char mostek_read_byte(struct device *dev, u32 ofs)
  197. {
  198. struct platform_device *pdev = to_platform_device(dev);
  199. struct m48t59_plat_data *pdata = pdev->dev.platform_data;
  200. return readb(pdata->ioaddr + ofs);
  201. }
  202. static void mostek_write_byte(struct device *dev, u32 ofs, u8 val)
  203. {
  204. struct platform_device *pdev = to_platform_device(dev);
  205. struct m48t59_plat_data *pdata = pdev->dev.platform_data;
  206. writeb(val, pdata->ioaddr + ofs);
  207. }
  208. static struct m48t59_plat_data m48t59_data = {
  209. .read_byte = mostek_read_byte,
  210. .write_byte = mostek_write_byte,
  211. };
  212. /* resource is set at runtime */
  213. static struct platform_device m48t59_rtc = {
  214. .name = "rtc-m48t59",
  215. .id = 0,
  216. .num_resources = 1,
  217. .dev = {
  218. .platform_data = &m48t59_data,
  219. },
  220. };
  221. static int clock_probe(struct platform_device *op)
  222. {
  223. struct device_node *dp = op->dev.of_node;
  224. const char *model = of_get_property(dp, "model", NULL);
  225. if (!model)
  226. return -ENODEV;
  227. /* Only the primary RTC has an address property */
  228. if (!of_find_property(dp, "address", NULL))
  229. return -ENODEV;
  230. m48t59_rtc.resource = &op->resource[0];
  231. if (!strcmp(model, "mk48t02")) {
  232. /* Map the clock register io area read-only */
  233. m48t59_data.ioaddr = of_ioremap(&op->resource[0], 0,
  234. 2048, "rtc-m48t59");
  235. m48t59_data.type = M48T59RTC_TYPE_M48T02;
  236. } else if (!strcmp(model, "mk48t08")) {
  237. m48t59_data.ioaddr = of_ioremap(&op->resource[0], 0,
  238. 8192, "rtc-m48t59");
  239. m48t59_data.type = M48T59RTC_TYPE_M48T08;
  240. } else
  241. return -ENODEV;
  242. if (platform_device_register(&m48t59_rtc) < 0)
  243. printk(KERN_ERR "Registering RTC device failed\n");
  244. return 0;
  245. }
  246. static struct of_device_id clock_match[] = {
  247. {
  248. .name = "eeprom",
  249. },
  250. {},
  251. };
  252. static struct platform_driver clock_driver = {
  253. .probe = clock_probe,
  254. .driver = {
  255. .name = "rtc",
  256. .of_match_table = clock_match,
  257. },
  258. };
  259. /* Probe for the mostek real time clock chip. */
  260. static int __init clock_init(void)
  261. {
  262. return platform_driver_register(&clock_driver);
  263. }
  264. /* Must be after subsys_initcall() so that busses are probed. Must
  265. * be before device_initcall() because things like the RTC driver
  266. * need to see the clock registers.
  267. */
  268. fs_initcall(clock_init);
  269. static void __init sparc32_late_time_init(void)
  270. {
  271. if (sparc_config.features & FEAT_L10_CLOCKEVENT)
  272. setup_timer_ce();
  273. if (sparc_config.features & FEAT_L10_CLOCKSOURCE)
  274. setup_timer_cs();
  275. #ifdef CONFIG_SMP
  276. register_percpu_ce(smp_processor_id());
  277. #endif
  278. }
  279. static void __init sbus_time_init(void)
  280. {
  281. sparc_config.get_cycles_offset = sbus_cycles_offset;
  282. sparc_config.init_timers();
  283. }
  284. void __init time_init(void)
  285. {
  286. sparc_config.features = 0;
  287. late_time_init = sparc32_late_time_init;
  288. if (pcic_present())
  289. pci_time_init();
  290. else
  291. sbus_time_init();
  292. }