1 /*
2  *	Real Time Clock interface for PPC64.
3  *
4  *	Based on rtc.c by Paul Gortmaker
5  *
6  *	This driver allows use of the real time clock
7  *	from user space. It exports the /dev/rtc
8  *	interface supporting various ioctl() and also the
9  *	/proc/driver/rtc pseudo-file for status information.
10  *
11  * 	Interface does not support RTC interrupts nor an alarm.
12  *
13  *	This program is free software; you can redistribute it and/or
14  *	modify it under the terms of the GNU General Public License
15  *	as published by the Free Software Foundation; either version
16  *	2 of the License, or (at your option) any later version.
17  *
18  *      1.0	Mike Corrigan:    IBM iSeries rtc support
19  *      1.1	Dave Engebretsen: IBM pSeries rtc support
20  */
21 
22 #define RTC_VERSION		"1.1"
23 
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/types.h>
27 #include <linux/miscdevice.h>
28 #include <linux/ioport.h>
29 #include <linux/fcntl.h>
30 #include <linux/mc146818rtc.h>
31 #include <linux/init.h>
32 #include <linux/poll.h>
33 #include <linux/proc_fs.h>
34 #include <linux/spinlock.h>
35 
36 #include <asm/hardirq.h>
37 #include <asm/io.h>
38 #include <asm/uaccess.h>
39 #include <asm/system.h>
40 #include <asm/time.h>
41 
42 #include <asm/iSeries/LparData.h>
43 #include <asm/iSeries/mf.h>
44 #include <asm/machdep.h>
45 #include <asm/iSeries/ItSpCommArea.h>
46 
47 extern int piranha_simulator;
48 
49 /*
50  *	We sponge a minor off of the misc major. No need slurping
51  *	up another valuable major dev number for this. If you add
52  *	an ioctl, make sure you don't conflict with SPARC's RTC
53  *	ioctls.
54  */
55 
56 static loff_t rtc_llseek(struct file *file, loff_t offset, int origin);
57 
58 static ssize_t rtc_read(struct file *file, char *buf,
59 			size_t count, loff_t *ppos);
60 
61 static int rtc_ioctl(struct inode *inode, struct file *file,
62 		     unsigned int cmd, unsigned long arg);
63 
64 static int rtc_read_proc(char *page, char **start, off_t off,
65                          int count, int *eof, void *data);
66 
67 /*
68  *	If this driver ever becomes modularised, it will be really nice
69  *	to make the epoch retain its value across module reload...
70  */
71 
72 static unsigned long epoch = 1900;	/* year corresponding to 0x00	*/
73 
74 static const unsigned char days_in_mo[] =
75 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
76 
77 /*
78  *	Now all the various file operations that we export.
79  */
80 
rtc_llseek(struct file * file,loff_t offset,int origin)81 static loff_t rtc_llseek(struct file *file, loff_t offset, int origin)
82 {
83 	return -ESPIPE;
84 }
85 
rtc_read(struct file * file,char * buf,size_t count,loff_t * ppos)86 static ssize_t rtc_read(struct file *file, char *buf,
87 			size_t count, loff_t *ppos)
88 {
89 	return -EIO;
90 }
91 
rtc_ioctl(struct inode * inode,struct file * file,unsigned int cmd,unsigned long arg)92 static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
93 		     unsigned long arg)
94 {
95 	struct rtc_time wtime;
96 
97 	switch (cmd) {
98 	case RTC_RD_TIME:	/* Read the time/date from RTC	*/
99 	{
100 		memset(&wtime, 0, sizeof(struct rtc_time));
101 		ppc_md.get_rtc_time(&wtime);
102 		break;
103 	}
104 	case RTC_SET_TIME:	/* Set the RTC */
105 	{
106 		struct rtc_time rtc_tm;
107 		unsigned char mon, day, hrs, min, sec, leap_yr;
108 		unsigned int yrs;
109 
110 		if (!capable(CAP_SYS_TIME))
111 			return -EACCES;
112 
113 		if (copy_from_user(&rtc_tm, (struct rtc_time*)arg,
114 				   sizeof(struct rtc_time)))
115 			return -EFAULT;
116 
117 		yrs = rtc_tm.tm_year;
118 		mon = rtc_tm.tm_mon + 1;   /* tm_mon starts at zero */
119 		day = rtc_tm.tm_mday;
120 		hrs = rtc_tm.tm_hour;
121 		min = rtc_tm.tm_min;
122 		sec = rtc_tm.tm_sec;
123 
124 		if (yrs < 70)
125 			return -EINVAL;
126 
127 		leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
128 
129 		if ((mon > 12) || (day == 0))
130 			return -EINVAL;
131 
132 		if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
133 			return -EINVAL;
134 
135 		if ((hrs >= 24) || (min >= 60) || (sec >= 60))
136 			return -EINVAL;
137 
138 		if ( yrs > 169 )
139 			return -EINVAL;
140 
141 		ppc_md.set_rtc_time(&rtc_tm);
142 
143 		return 0;
144 	}
145 	case RTC_EPOCH_READ:	/* Read the epoch.	*/
146 	{
147 		return put_user (epoch, (unsigned long *)arg);
148 	}
149 	case RTC_EPOCH_SET:	/* Set the epoch.	*/
150 	{
151 		/*
152 		 * There were no RTC clocks before 1900.
153 		 */
154 		if (arg < 1900)
155 			return -EINVAL;
156 
157 		if (!capable(CAP_SYS_TIME))
158 			return -EACCES;
159 
160 		epoch = arg;
161 		return 0;
162 	}
163 	default:
164 		return -EINVAL;
165 	}
166 	return copy_to_user((void *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
167 }
168 
rtc_open(struct inode * inode,struct file * file)169 static int rtc_open(struct inode *inode, struct file *file)
170 {
171 	return 0;
172 }
173 
rtc_release(struct inode * inode,struct file * file)174 static int rtc_release(struct inode *inode, struct file *file)
175 {
176 	return 0;
177 }
178 
179 /*
180  *	The various file operations we support.
181  */
182 static struct file_operations rtc_fops = {
183 	.owner =	THIS_MODULE,
184 	.llseek =	rtc_llseek,
185 	.read =		rtc_read,
186 	.ioctl =	rtc_ioctl,
187 	.open =		rtc_open,
188 	.release =	rtc_release,
189 };
190 
191 static struct miscdevice rtc_dev=
192 {
193 	RTC_MINOR,
194 	"rtc",
195 	&rtc_fops
196 };
197 
rtc_init(void)198 static int __init rtc_init(void)
199 {
200 	misc_register(&rtc_dev);
201 	create_proc_read_entry ("driver/rtc", 0, 0, rtc_read_proc, NULL);
202 
203 	printk(KERN_INFO "i/pSeries Real Time Clock Driver v" RTC_VERSION "\n");
204 
205 	return 0;
206 }
207 
rtc_exit(void)208 static void __exit rtc_exit (void)
209 {
210 	remove_proc_entry ("driver/rtc", NULL);
211 	misc_deregister(&rtc_dev);
212 }
213 
214 module_init(rtc_init);
215 module_exit(rtc_exit);
216 EXPORT_NO_SYMBOLS;
217 
218 /*
219  *	Info exported via "/proc/driver/rtc".
220  */
221 
rtc_proc_output(char * buf)222 static int rtc_proc_output (char *buf)
223 {
224 
225 	char *p;
226 	struct rtc_time tm;
227 
228 	p = buf;
229 
230 	ppc_md.get_rtc_time(&tm);
231 
232 	/*
233 	 * There is no way to tell if the luser has the RTC set for local
234 	 * time or for Universal Standard Time (GMT). Probably local though.
235 	 */
236 	p += sprintf(p,
237 		     "rtc_time\t: %02d:%02d:%02d\n"
238 		     "rtc_date\t: %04d-%02d-%02d\n"
239 	 	     "rtc_epoch\t: %04lu\n",
240 		     tm.tm_hour, tm.tm_min, tm.tm_sec,
241 		     tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch);
242 
243 	p += sprintf(p,
244 		     "DST_enable\t: no\n"
245 		     "BCD\t\t: yes\n"
246 		     "24hr\t\t: yes\n" );
247 
248 	return  p - buf;
249 }
250 
rtc_read_proc(char * page,char ** start,off_t off,int count,int * eof,void * data)251 static int rtc_read_proc(char *page, char **start, off_t off,
252                          int count, int *eof, void *data)
253 {
254         int len = rtc_proc_output (page);
255         if (len <= off+count) *eof = 1;
256         *start = page + off;
257         len -= off;
258         if (len>count) len = count;
259         if (len<0) len = 0;
260         return len;
261 }
262 
263 /*
264  * Get the RTC from the virtual service processor
265  * This requires flowing LpEvents to the primary partition
266  */
iSeries_get_rtc_time(struct rtc_time * rtc_tm)267 void iSeries_get_rtc_time(struct rtc_time *rtc_tm)
268 {
269 	if (piranha_simulator)
270 		return;
271 
272 	mf_getRtc(rtc_tm);
273 	rtc_tm->tm_mon--;
274 }
275 
276 
277 #define MAX_RTC_WAIT 5000	/* 5 sec */
278 #define RTAS_CLOCK_BUSY (-2)
pSeries_get_boot_time(struct rtc_time * rtc_tm)279 void pSeries_get_boot_time(struct rtc_time *rtc_tm)
280 {
281 	unsigned long ret[8];
282 	int error, wait_time;
283 	unsigned long max_wait_tb;
284 
285 	max_wait_tb = __get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT;
286 	do {
287 		error = rtas_call(rtas_token("get-time-of-day"), 0, 8, (void *)&ret);
288 		if (error == RTAS_CLOCK_BUSY || rtas_is_extended_busy(error)) {
289 			wait_time = rtas_extended_busy_delay_time(error);
290 			/* This is boot time so we spin. */
291 			udelay(wait_time*1000);
292 			error = RTAS_CLOCK_BUSY;
293 		}
294 	} while (error == RTAS_CLOCK_BUSY && (__get_tb() < max_wait_tb));
295 
296 	if (error != 0) {
297 		printk(KERN_WARNING "error: reading the clock failed (%d)\n",
298 			error);
299 		return;
300 	}
301 
302 	rtc_tm->tm_sec = ret[5];
303 	rtc_tm->tm_min = ret[4];
304 	rtc_tm->tm_hour = ret[3];
305 	rtc_tm->tm_mday = ret[2];
306 	rtc_tm->tm_mon = ret[1] - 1;
307 	rtc_tm->tm_year = ret[0] - 1900;
308 }
309 
310 /* NOTE: get_rtc_time will get an error if executed in interrupt context
311  * and if a delay is needed to read the clock.  In this case we just
312  * silently return without updating rtc_tm.
313  */
pSeries_get_rtc_time(struct rtc_time * rtc_tm)314 void pSeries_get_rtc_time(struct rtc_time *rtc_tm)
315 {
316         unsigned long ret[8];
317 	int error, wait_time;
318 	unsigned long max_wait_tb;
319 
320 	max_wait_tb = __get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT;
321 	do {
322 		error = rtas_call(rtas_token("get-time-of-day"), 0, 8, (void *)&ret);
323 		if (error == RTAS_CLOCK_BUSY || rtas_is_extended_busy(error)) {
324 			if (in_interrupt()) {
325 				printk(KERN_WARNING "error: reading clock would delay interrupt\n");
326 				return;	/* delay not allowed */
327 			}
328 			wait_time = rtas_extended_busy_delay_time(error);
329 			set_current_state(TASK_INTERRUPTIBLE);
330 			schedule_timeout(wait_time);
331 			error = RTAS_CLOCK_BUSY;
332 		}
333 	} while (error == RTAS_CLOCK_BUSY && (__get_tb() < max_wait_tb));
334 
335         if (error != 0) {
336                 printk(KERN_WARNING "error: reading the clock failed (%d)\n",
337 		       error);
338 		return;
339         }
340 
341 	rtc_tm->tm_sec = ret[5];
342 	rtc_tm->tm_min = ret[4];
343 	rtc_tm->tm_hour = ret[3];
344 	rtc_tm->tm_mday = ret[2];
345 	rtc_tm->tm_mon = ret[1] - 1;
346 	rtc_tm->tm_year = ret[0] - 1900;
347 }
348 
pSeries_set_rtc_time(struct rtc_time * tm)349 int pSeries_set_rtc_time(struct rtc_time *tm)
350 {
351 	int error, wait_time;
352 	unsigned long max_wait_tb;
353 
354 	max_wait_tb = __get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT;
355 	do {
356 	        error = rtas_call(rtas_token("set-time-of-day"), 7, 1, NULL,
357 				  tm->tm_year + 1900, tm->tm_mon + 1,
358 				  tm->tm_mday, tm->tm_hour, tm->tm_min,
359 				  tm->tm_sec, 0);
360 		if (error == RTAS_CLOCK_BUSY || rtas_is_extended_busy(error)) {
361 			if (in_interrupt())
362 				return 1;	/* probably decrementer */
363 			wait_time = rtas_extended_busy_delay_time(error);
364 			set_current_state(TASK_INTERRUPTIBLE);
365 			schedule_timeout(wait_time);
366 			error = RTAS_CLOCK_BUSY;
367 		}
368 	} while (error == RTAS_CLOCK_BUSY && (__get_tb() < max_wait_tb));
369 
370         if (error != 0)
371                 printk(KERN_WARNING "error: setting the clock failed (%d)\n",
372 		       error);
373 
374         return 0;
375 }
376 
377 /*
378  * Set the RTC in the virtual service processor
379  * This requires flowing LpEvents to the primary partition
380  */
iSeries_set_rtc_time(struct rtc_time * tm)381 int iSeries_set_rtc_time(struct rtc_time *tm)
382 {
383 	mf_setRtc(tm);
384 	return 0;
385 }
386 
iSeries_get_boot_time(struct rtc_time * tm)387 void iSeries_get_boot_time(struct rtc_time *tm)
388 {
389 	unsigned long time;
390 	static unsigned long lastsec = 1;
391 
392 	u32 dataWord1 = *((u32 *)(&xSpCommArea.xBcdTimeAtIplStart));
393 	u32 dataWord2 = *(((u32 *)&(xSpCommArea.xBcdTimeAtIplStart)) + 1);
394 	int year = 1970;
395 	int year1 = ( dataWord1 >> 24 ) & 0x000000FF;
396 	int year2 = ( dataWord1 >> 16 ) & 0x000000FF;
397 	int sec = ( dataWord1 >> 8 ) & 0x000000FF;
398 	int min = dataWord1 & 0x000000FF;
399 	int hour = ( dataWord2 >> 24 ) & 0x000000FF;
400 	int day = ( dataWord2 >> 8 ) & 0x000000FF;
401 	int mon = dataWord2 & 0x000000FF;
402 
403 	if ( piranha_simulator )
404 		return;
405 
406 	BCD_TO_BIN(sec);
407 	BCD_TO_BIN(min);
408 	BCD_TO_BIN(hour);
409 	BCD_TO_BIN(day);
410 	BCD_TO_BIN(mon);
411 	BCD_TO_BIN(year1);
412 	BCD_TO_BIN(year2);
413 	year = year1 * 100 + year2;
414 
415 	time = mktime(year, mon, day, hour, min, sec);
416 	time += ( jiffies / HZ );
417 
418 	/* Now THIS is a nasty hack!
419 	* It ensures that the first two calls get different answers.
420 	* That way the loop in init_time (time.c) will not think
421 	* the clock is stuck.
422 	*/
423 	if ( lastsec ) {
424 		time -= lastsec;
425 		--lastsec;
426 	}
427 
428 	to_tm(time, tm);
429 	tm->tm_year -= 1900;
430 	tm->tm_mon  -= 1;
431 }
432