time_32.c 8.8 KB

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