time_32.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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.h>
  27. #include <linux/rtc/m48t59.h>
  28. #include <linux/timex.h>
  29. #include <linux/clocksource.h>
  30. #include <linux/clockchips.h>
  31. #include <linux/init.h>
  32. #include <linux/pci.h>
  33. #include <linux/ioport.h>
  34. #include <linux/profile.h>
  35. #include <linux/of.h>
  36. #include <linux/of_device.h>
  37. #include <linux/platform_device.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 "irq.h"
  49. static __cacheline_aligned_in_smp DEFINE_SEQLOCK(timer_cs_lock);
  50. static __volatile__ u64 timer_cs_internal_counter = 0;
  51. static char timer_cs_enabled = 0;
  52. static struct clock_event_device timer_ce;
  53. static char timer_ce_enabled = 0;
  54. #ifdef CONFIG_SMP
  55. DEFINE_PER_CPU(struct clock_event_device, sparc32_clockevent);
  56. #endif
  57. DEFINE_SPINLOCK(rtc_lock);
  58. EXPORT_SYMBOL(rtc_lock);
  59. static int set_rtc_mmss(unsigned long);
  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__ unsigned int *master_l10_counter;
  75. int update_persistent_clock(struct timespec now)
  76. {
  77. return set_rtc_mmss(now.tv_sec);
  78. }
  79. irqreturn_t notrace timer_interrupt(int dummy, void *dev_id)
  80. {
  81. if (timer_cs_enabled) {
  82. write_seqlock(&timer_cs_lock);
  83. timer_cs_internal_counter++;
  84. sparc_config.clear_clock_irq();
  85. write_sequnlock(&timer_cs_lock);
  86. } else {
  87. sparc_config.clear_clock_irq();
  88. }
  89. if (timer_ce_enabled)
  90. timer_ce.event_handler(&timer_ce);
  91. return IRQ_HANDLED;
  92. }
  93. static void timer_ce_set_mode(enum clock_event_mode mode,
  94. struct clock_event_device *evt)
  95. {
  96. switch (mode) {
  97. case CLOCK_EVT_MODE_PERIODIC:
  98. case CLOCK_EVT_MODE_RESUME:
  99. timer_ce_enabled = 1;
  100. break;
  101. case CLOCK_EVT_MODE_SHUTDOWN:
  102. timer_ce_enabled = 0;
  103. break;
  104. default:
  105. break;
  106. }
  107. smp_mb();
  108. }
  109. static __init void setup_timer_ce(void)
  110. {
  111. struct clock_event_device *ce = &timer_ce;
  112. BUG_ON(smp_processor_id() != boot_cpu_id);
  113. ce->name = "timer_ce";
  114. ce->rating = 100;
  115. ce->features = CLOCK_EVT_FEAT_PERIODIC;
  116. ce->set_mode = timer_ce_set_mode;
  117. ce->cpumask = cpu_possible_mask;
  118. ce->shift = 32;
  119. ce->mult = div_sc(sparc_config.clock_rate, NSEC_PER_SEC,
  120. ce->shift);
  121. clockevents_register_device(ce);
  122. }
  123. static unsigned int sbus_cycles_offset(void)
  124. {
  125. unsigned int val, offset;
  126. val = *master_l10_counter;
  127. offset = (val >> TIMER_VALUE_SHIFT) & TIMER_VALUE_MASK;
  128. /* Limit hit? */
  129. if (val & TIMER_LIMIT_BIT)
  130. offset += sparc_config.cs_period;
  131. return offset;
  132. }
  133. static cycle_t timer_cs_read(struct clocksource *cs)
  134. {
  135. unsigned int seq, offset;
  136. u64 cycles;
  137. do {
  138. seq = read_seqbegin(&timer_cs_lock);
  139. cycles = timer_cs_internal_counter;
  140. offset = sparc_config.get_cycles_offset();
  141. } while (read_seqretry(&timer_cs_lock, seq));
  142. /* Count absolute cycles */
  143. cycles *= sparc_config.cs_period;
  144. cycles += offset;
  145. return cycles;
  146. }
  147. static struct clocksource timer_cs = {
  148. .name = "timer_cs",
  149. .rating = 100,
  150. .read = timer_cs_read,
  151. .mask = CLOCKSOURCE_MASK(64),
  152. .shift = 2,
  153. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  154. };
  155. static __init int setup_timer_cs(void)
  156. {
  157. timer_cs_enabled = 1;
  158. timer_cs.mult = clocksource_hz2mult(sparc_config.clock_rate,
  159. timer_cs.shift);
  160. return clocksource_register(&timer_cs);
  161. }
  162. #ifdef CONFIG_SMP
  163. static void percpu_ce_setup(enum clock_event_mode mode,
  164. struct clock_event_device *evt)
  165. {
  166. int cpu = __first_cpu(evt->cpumask);
  167. switch (mode) {
  168. case CLOCK_EVT_MODE_PERIODIC:
  169. sparc_config.load_profile_irq(cpu,
  170. SBUS_CLOCK_RATE / HZ);
  171. break;
  172. case CLOCK_EVT_MODE_ONESHOT:
  173. case CLOCK_EVT_MODE_SHUTDOWN:
  174. case CLOCK_EVT_MODE_UNUSED:
  175. sparc_config.load_profile_irq(cpu, 0);
  176. break;
  177. default:
  178. break;
  179. }
  180. }
  181. static int percpu_ce_set_next_event(unsigned long delta,
  182. struct clock_event_device *evt)
  183. {
  184. int cpu = __first_cpu(evt->cpumask);
  185. unsigned int next = (unsigned int)delta;
  186. sparc_config.load_profile_irq(cpu, next);
  187. return 0;
  188. }
  189. void register_percpu_ce(int cpu)
  190. {
  191. struct clock_event_device *ce = &per_cpu(sparc32_clockevent, cpu);
  192. unsigned int features = CLOCK_EVT_FEAT_PERIODIC;
  193. if (sparc_config.features & FEAT_L14_ONESHOT)
  194. features |= CLOCK_EVT_FEAT_ONESHOT;
  195. ce->name = "percpu_ce";
  196. ce->rating = 200;
  197. ce->features = features;
  198. ce->set_mode = percpu_ce_setup;
  199. ce->set_next_event = percpu_ce_set_next_event;
  200. ce->cpumask = cpumask_of(cpu);
  201. ce->shift = 32;
  202. ce->mult = div_sc(sparc_config.clock_rate, NSEC_PER_SEC,
  203. ce->shift);
  204. ce->max_delta_ns = clockevent_delta2ns(sparc_config.clock_rate, ce);
  205. ce->min_delta_ns = clockevent_delta2ns(100, ce);
  206. clockevents_register_device(ce);
  207. }
  208. #endif
  209. static unsigned char mostek_read_byte(struct device *dev, u32 ofs)
  210. {
  211. struct platform_device *pdev = to_platform_device(dev);
  212. struct m48t59_plat_data *pdata = pdev->dev.platform_data;
  213. return readb(pdata->ioaddr + ofs);
  214. }
  215. static void mostek_write_byte(struct device *dev, u32 ofs, u8 val)
  216. {
  217. struct platform_device *pdev = to_platform_device(dev);
  218. struct m48t59_plat_data *pdata = pdev->dev.platform_data;
  219. writeb(val, pdata->ioaddr + ofs);
  220. }
  221. static struct m48t59_plat_data m48t59_data = {
  222. .read_byte = mostek_read_byte,
  223. .write_byte = mostek_write_byte,
  224. };
  225. /* resource is set at runtime */
  226. static struct platform_device m48t59_rtc = {
  227. .name = "rtc-m48t59",
  228. .id = 0,
  229. .num_resources = 1,
  230. .dev = {
  231. .platform_data = &m48t59_data,
  232. },
  233. };
  234. static int __devinit clock_probe(struct platform_device *op)
  235. {
  236. struct device_node *dp = op->dev.of_node;
  237. const char *model = of_get_property(dp, "model", NULL);
  238. if (!model)
  239. return -ENODEV;
  240. /* Only the primary RTC has an address property */
  241. if (!of_find_property(dp, "address", NULL))
  242. return -ENODEV;
  243. m48t59_rtc.resource = &op->resource[0];
  244. if (!strcmp(model, "mk48t02")) {
  245. /* Map the clock register io area read-only */
  246. m48t59_data.ioaddr = of_ioremap(&op->resource[0], 0,
  247. 2048, "rtc-m48t59");
  248. m48t59_data.type = M48T59RTC_TYPE_M48T02;
  249. } else if (!strcmp(model, "mk48t08")) {
  250. m48t59_data.ioaddr = of_ioremap(&op->resource[0], 0,
  251. 8192, "rtc-m48t59");
  252. m48t59_data.type = M48T59RTC_TYPE_M48T08;
  253. } else
  254. return -ENODEV;
  255. if (platform_device_register(&m48t59_rtc) < 0)
  256. printk(KERN_ERR "Registering RTC device failed\n");
  257. return 0;
  258. }
  259. static struct of_device_id clock_match[] = {
  260. {
  261. .name = "eeprom",
  262. },
  263. {},
  264. };
  265. static struct platform_driver clock_driver = {
  266. .probe = clock_probe,
  267. .driver = {
  268. .name = "rtc",
  269. .owner = THIS_MODULE,
  270. .of_match_table = clock_match,
  271. },
  272. };
  273. /* Probe for the mostek real time clock chip. */
  274. static int __init clock_init(void)
  275. {
  276. return platform_driver_register(&clock_driver);
  277. }
  278. /* Must be after subsys_initcall() so that busses are probed. Must
  279. * be before device_initcall() because things like the RTC driver
  280. * need to see the clock registers.
  281. */
  282. fs_initcall(clock_init);
  283. static void __init sparc32_late_time_init(void)
  284. {
  285. if (sparc_config.features & FEAT_L10_CLOCKEVENT)
  286. setup_timer_ce();
  287. if (sparc_config.features & FEAT_L10_CLOCKSOURCE)
  288. setup_timer_cs();
  289. #ifdef CONFIG_SMP
  290. register_percpu_ce(smp_processor_id());
  291. #endif
  292. }
  293. static void __init sbus_time_init(void)
  294. {
  295. sparc_config.get_cycles_offset = sbus_cycles_offset;
  296. sparc_config.init_timers();
  297. }
  298. void __init time_init(void)
  299. {
  300. sparc_config.features = 0;
  301. late_time_init = sparc32_late_time_init;
  302. if (pcic_present())
  303. pci_time_init();
  304. else
  305. sbus_time_init();
  306. }
  307. static int set_rtc_mmss(unsigned long secs)
  308. {
  309. struct rtc_device *rtc = rtc_class_open("rtc0");
  310. int err = -1;
  311. if (rtc) {
  312. err = rtc_set_mmss(rtc, secs);
  313. rtc_class_close(rtc);
  314. }
  315. return err;
  316. }