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/init.h>
30 #include <linux/pci.h>
31 #include <linux/ioport.h>
32 #include <linux/profile.h>
33 #include <linux/of.h>
34 #include <linux/of_device.h>
35 #include <linux/platform_device.h>
36 
37 #include <asm/oplib.h>
38 #include <asm/timex.h>
39 #include <asm/timer.h>
40 #include <asm/irq.h>
41 #include <asm/io.h>
42 #include <asm/idprom.h>
43 #include <asm/machines.h>
44 #include <asm/page.h>
45 #include <asm/pcic.h>
46 #include <asm/irq_regs.h>
47 
48 #include "irq.h"
49 
50 DEFINE_SPINLOCK(rtc_lock);
51 EXPORT_SYMBOL(rtc_lock);
52 
53 static int set_rtc_mmss(unsigned long);
54 
profile_pc(struct pt_regs * regs)55 unsigned long profile_pc(struct pt_regs *regs)
56 {
57 	extern char __copy_user_begin[], __copy_user_end[];
58 	extern char __atomic_begin[], __atomic_end[];
59 	extern char __bzero_begin[], __bzero_end[];
60 
61 	unsigned long pc = regs->pc;
62 
63 	if (in_lock_functions(pc) ||
64 	    (pc >= (unsigned long) __copy_user_begin &&
65 	     pc < (unsigned long) __copy_user_end) ||
66 	    (pc >= (unsigned long) __atomic_begin &&
67 	     pc < (unsigned long) __atomic_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 
74 EXPORT_SYMBOL(profile_pc);
75 
76 __volatile__ unsigned int *master_l10_counter;
77 
78 u32 (*do_arch_gettimeoffset)(void);
79 
update_persistent_clock(struct timespec now)80 int update_persistent_clock(struct timespec now)
81 {
82 	return set_rtc_mmss(now.tv_sec);
83 }
84 
85 /*
86  * timer_interrupt() needs to keep up the real-time clock,
87  * as well as call the "xtime_update()" routine every clocktick
88  */
89 
90 #define TICK_SIZE (tick_nsec / 1000)
91 
timer_interrupt(int dummy,void * dev_id)92 static irqreturn_t timer_interrupt(int dummy, void *dev_id)
93 {
94 #ifndef CONFIG_SMP
95 	profile_tick(CPU_PROFILING);
96 #endif
97 
98 	clear_clock_irq();
99 
100 	xtime_update(1);
101 
102 #ifndef CONFIG_SMP
103 	update_process_times(user_mode(get_irq_regs()));
104 #endif
105 	return IRQ_HANDLED;
106 }
107 
mostek_read_byte(struct device * dev,u32 ofs)108 static unsigned char mostek_read_byte(struct device *dev, u32 ofs)
109 {
110 	struct platform_device *pdev = to_platform_device(dev);
111 	struct m48t59_plat_data *pdata = pdev->dev.platform_data;
112 
113 	return readb(pdata->ioaddr + ofs);
114 }
115 
mostek_write_byte(struct device * dev,u32 ofs,u8 val)116 static void mostek_write_byte(struct device *dev, u32 ofs, u8 val)
117 {
118 	struct platform_device *pdev = to_platform_device(dev);
119 	struct m48t59_plat_data *pdata = pdev->dev.platform_data;
120 
121 	writeb(val, pdata->ioaddr + ofs);
122 }
123 
124 static struct m48t59_plat_data m48t59_data = {
125 	.read_byte = mostek_read_byte,
126 	.write_byte = mostek_write_byte,
127 };
128 
129 /* resource is set at runtime */
130 static struct platform_device m48t59_rtc = {
131 	.name		= "rtc-m48t59",
132 	.id		= 0,
133 	.num_resources	= 1,
134 	.dev	= {
135 		.platform_data = &m48t59_data,
136 	},
137 };
138 
clock_probe(struct platform_device * op)139 static int __devinit clock_probe(struct platform_device *op)
140 {
141 	struct device_node *dp = op->dev.of_node;
142 	const char *model = of_get_property(dp, "model", NULL);
143 
144 	if (!model)
145 		return -ENODEV;
146 
147 	/* Only the primary RTC has an address property */
148 	if (!of_find_property(dp, "address", NULL))
149 		return -ENODEV;
150 
151 	m48t59_rtc.resource = &op->resource[0];
152 	if (!strcmp(model, "mk48t02")) {
153 		/* Map the clock register io area read-only */
154 		m48t59_data.ioaddr = of_ioremap(&op->resource[0], 0,
155 						2048, "rtc-m48t59");
156 		m48t59_data.type = M48T59RTC_TYPE_M48T02;
157 	} else if (!strcmp(model, "mk48t08")) {
158 		m48t59_data.ioaddr = of_ioremap(&op->resource[0], 0,
159 						8192, "rtc-m48t59");
160 		m48t59_data.type = M48T59RTC_TYPE_M48T08;
161 	} else
162 		return -ENODEV;
163 
164 	if (platform_device_register(&m48t59_rtc) < 0)
165 		printk(KERN_ERR "Registering RTC device failed\n");
166 
167 	return 0;
168 }
169 
170 static struct of_device_id clock_match[] = {
171 	{
172 		.name = "eeprom",
173 	},
174 	{},
175 };
176 
177 static struct platform_driver clock_driver = {
178 	.probe		= clock_probe,
179 	.driver = {
180 		.name = "rtc",
181 		.owner = THIS_MODULE,
182 		.of_match_table = clock_match,
183 	},
184 };
185 
186 
187 /* Probe for the mostek real time clock chip. */
clock_init(void)188 static int __init clock_init(void)
189 {
190 	return platform_driver_register(&clock_driver);
191 }
192 /* Must be after subsys_initcall() so that busses are probed.  Must
193  * be before device_initcall() because things like the RTC driver
194  * need to see the clock registers.
195  */
196 fs_initcall(clock_init);
197 
198 
sbus_do_gettimeoffset(void)199 u32 sbus_do_gettimeoffset(void)
200 {
201 	unsigned long val = *master_l10_counter;
202 	unsigned long usec = (val >> 10) & 0x1fffff;
203 
204 	/* Limit hit?  */
205 	if (val & 0x80000000)
206 		usec += 1000000 / HZ;
207 
208 	return usec * 1000;
209 }
210 
211 
arch_gettimeoffset(void)212 u32 arch_gettimeoffset(void)
213 {
214 	if (unlikely(!do_arch_gettimeoffset))
215 		return 0;
216 	return do_arch_gettimeoffset();
217 }
218 
sbus_time_init(void)219 static void __init sbus_time_init(void)
220 {
221 	do_arch_gettimeoffset = sbus_do_gettimeoffset;
222 
223 	btfixup();
224 
225 	sparc_irq_config.init_timers(timer_interrupt);
226 }
227 
time_init(void)228 void __init time_init(void)
229 {
230 	if (pcic_present())
231 		pci_time_init();
232 	else
233 		sbus_time_init();
234 }
235 
236 
set_rtc_mmss(unsigned long secs)237 static int set_rtc_mmss(unsigned long secs)
238 {
239 	struct rtc_device *rtc = rtc_class_open("rtc0");
240 	int err = -1;
241 
242 	if (rtc) {
243 		err = rtc_set_mmss(rtc, secs);
244 		rtc_class_close(rtc);
245 	}
246 
247 	return err;
248 }
249