2 * HP i8042 SDC + MSM-58321 BBRTC driver.
4 * Copyright (c) 2001 Brian S. Julin
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification.
13 * 2. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * Alternatively, this software may be distributed under the terms of the
17 * GNU General Public License ("GPL").
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * System Device Controller Microprocessor Firmware Theory of Operation
31 * for Part Number 1820-4784 Revision B. Dwg No. A-1820-4784-2
32 * efirtc.c by Stephane Eranian/Hewlett Packard
36 #include <linux/hp_sdc.h>
37 #include <linux/errno.h>
38 #include <linux/types.h>
39 #include <linux/init.h>
40 #include <linux/module.h>
41 #include <linux/time.h>
42 #include <linux/miscdevice.h>
43 #include <linux/proc_fs.h>
44 #include <linux/seq_file.h>
45 #include <linux/poll.h>
46 #include <linux/rtc.h>
47 #include <linux/mutex.h>
48 #include <linux/semaphore.h>
50 MODULE_AUTHOR("Brian S. Julin <bri@calyx.com>");
51 MODULE_DESCRIPTION("HP i8042 SDC + MSM-58321 RTC Driver");
52 MODULE_LICENSE("Dual BSD/GPL");
54 #define RTC_VERSION "1.10d"
56 static unsigned long epoch
= 2000;
58 static struct semaphore i8042tregs
;
60 static void hp_sdc_rtc_isr (int irq
, void *dev_id
,
61 uint8_t status
, uint8_t data
)
66 static int hp_sdc_rtc_do_read_bbrtc (struct rtc_time
*rtctm
)
68 struct semaphore tsem
;
75 tseq
[i
++] = HP_SDC_ACT_DATAREG
|
76 HP_SDC_ACT_POSTCMD
| HP_SDC_ACT_DATAIN
;
77 tseq
[i
++] = 0x01; /* write i8042[0x70] */
78 tseq
[i
] = i
/ 7; /* BBRTC reg address */
80 tseq
[i
++] = HP_SDC_CMD_DO_RTCR
; /* Trigger command */
81 tseq
[i
++] = 2; /* expect 1 stat/dat pair back. */
82 i
++; i
++; /* buffer for stat/dat pair */
84 tseq
[84] |= HP_SDC_ACT_SEMAPHORE
;
87 t
.act
.semaphore
= &tsem
;
90 if (hp_sdc_enqueue_transaction(&t
)) return -1;
92 /* Put ourselves to sleep for results. */
93 if (WARN_ON(down_interruptible(&tsem
)))
96 /* Check for nonpresence of BBRTC */
97 if (!((tseq
[83] | tseq
[90] | tseq
[69] | tseq
[76] |
98 tseq
[55] | tseq
[62] | tseq
[34] | tseq
[41] |
99 tseq
[20] | tseq
[27] | tseq
[6] | tseq
[13]) & 0x0f))
102 memset(rtctm
, 0, sizeof(struct rtc_time
));
103 rtctm
->tm_year
= (tseq
[83] & 0x0f) + (tseq
[90] & 0x0f) * 10;
104 rtctm
->tm_mon
= (tseq
[69] & 0x0f) + (tseq
[76] & 0x0f) * 10;
105 rtctm
->tm_mday
= (tseq
[55] & 0x0f) + (tseq
[62] & 0x0f) * 10;
106 rtctm
->tm_wday
= (tseq
[48] & 0x0f);
107 rtctm
->tm_hour
= (tseq
[34] & 0x0f) + (tseq
[41] & 0x0f) * 10;
108 rtctm
->tm_min
= (tseq
[20] & 0x0f) + (tseq
[27] & 0x0f) * 10;
109 rtctm
->tm_sec
= (tseq
[6] & 0x0f) + (tseq
[13] & 0x0f) * 10;
114 static int hp_sdc_rtc_read_bbrtc (struct rtc_time
*rtctm
)
116 struct rtc_time tm
, tm_last
;
119 /* MSM-58321 has no read latch, so must read twice and compare. */
121 if (hp_sdc_rtc_do_read_bbrtc(&tm_last
)) return -1;
122 if (hp_sdc_rtc_do_read_bbrtc(&tm
)) return -1;
124 while (memcmp(&tm
, &tm_last
, sizeof(struct rtc_time
))) {
125 if (i
++ > 4) return -1;
126 memcpy(&tm_last
, &tm
, sizeof(struct rtc_time
));
127 if (hp_sdc_rtc_do_read_bbrtc(&tm
)) return -1;
130 memcpy(rtctm
, &tm
, sizeof(struct rtc_time
));
136 static int64_t hp_sdc_rtc_read_i8042timer (uint8_t loadcmd
, int numreg
)
138 hp_sdc_transaction t
;
140 HP_SDC_ACT_PRECMD
| HP_SDC_ACT_POSTCMD
| HP_SDC_ACT_DATAIN
,
142 HP_SDC_CMD_READ_T1
, 2, 0, 0,
143 HP_SDC_ACT_POSTCMD
| HP_SDC_ACT_DATAIN
,
144 HP_SDC_CMD_READ_T2
, 2, 0, 0,
145 HP_SDC_ACT_POSTCMD
| HP_SDC_ACT_DATAIN
,
146 HP_SDC_CMD_READ_T3
, 2, 0, 0,
147 HP_SDC_ACT_POSTCMD
| HP_SDC_ACT_DATAIN
,
148 HP_SDC_CMD_READ_T4
, 2, 0, 0,
149 HP_SDC_ACT_POSTCMD
| HP_SDC_ACT_DATAIN
,
150 HP_SDC_CMD_READ_T5
, 2, 0, 0
153 t
.endidx
= numreg
* 5;
156 tseq
[t
.endidx
- 4] |= HP_SDC_ACT_SEMAPHORE
; /* numreg assumed > 1 */
159 t
.act
.semaphore
= &i8042tregs
;
161 /* Sleep if output regs in use. */
162 if (WARN_ON(down_interruptible(&i8042tregs
)))
165 if (hp_sdc_enqueue_transaction(&t
)) {
170 /* Sleep until results come back. */
171 if (WARN_ON(down_interruptible(&i8042tregs
)))
177 ((uint64_t)(tseq
[10]) << 8) | ((uint64_t)(tseq
[15]) << 16) |
178 ((uint64_t)(tseq
[20]) << 24) | ((uint64_t)(tseq
[25]) << 32));
182 /* Read the i8042 real-time clock */
183 static inline int hp_sdc_rtc_read_rt(struct timespec64
*res
) {
188 raw
= hp_sdc_rtc_read_i8042timer(HP_SDC_CMD_LOAD_RT
, 5);
189 if (raw
< 0) return -1;
191 tenms
= (uint32_t)raw
& 0xffffff;
192 days
= (unsigned int)(raw
>> 24) & 0xffff;
194 res
->tv_nsec
= (long)(tenms
% 100) * 10000 * 1000;
195 res
->tv_sec
= (tenms
/ 100) + (time64_t
)days
* 86400;
201 /* Read the i8042 fast handshake timer */
202 static inline int hp_sdc_rtc_read_fhs(struct timespec64
*res
) {
206 raw
= hp_sdc_rtc_read_i8042timer(HP_SDC_CMD_LOAD_FHS
, 2);
207 if (raw
< 0) return -1;
209 tenms
= (unsigned int)raw
& 0xffff;
211 res
->tv_nsec
= (long)(tenms
% 100) * 10000 * 1000;
212 res
->tv_sec
= (time64_t
)(tenms
/ 100);
218 /* Read the i8042 match timer (a.k.a. alarm) */
219 static inline int hp_sdc_rtc_read_mt(struct timespec64
*res
) {
223 raw
= hp_sdc_rtc_read_i8042timer(HP_SDC_CMD_LOAD_MT
, 3);
224 if (raw
< 0) return -1;
226 tenms
= (uint32_t)raw
& 0xffffff;
228 res
->tv_nsec
= (long)(tenms
% 100) * 10000 * 1000;
229 res
->tv_sec
= (time64_t
)(tenms
/ 100);
235 /* Read the i8042 delay timer */
236 static inline int hp_sdc_rtc_read_dt(struct timespec64
*res
) {
240 raw
= hp_sdc_rtc_read_i8042timer(HP_SDC_CMD_LOAD_DT
, 3);
241 if (raw
< 0) return -1;
243 tenms
= (uint32_t)raw
& 0xffffff;
245 res
->tv_nsec
= (long)(tenms
% 100) * 10000 * 1000;
246 res
->tv_sec
= (time64_t
)(tenms
/ 100);
252 /* Read the i8042 cycle timer (a.k.a. periodic) */
253 static inline int hp_sdc_rtc_read_ct(struct timespec64
*res
) {
257 raw
= hp_sdc_rtc_read_i8042timer(HP_SDC_CMD_LOAD_CT
, 3);
258 if (raw
< 0) return -1;
260 tenms
= (uint32_t)raw
& 0xffffff;
262 res
->tv_nsec
= (long)(tenms
% 100) * 10000 * 1000;
263 res
->tv_sec
= (time64_t
)(tenms
/ 100);
268 static int hp_sdc_rtc_proc_show(struct seq_file
*m
, void *v
)
270 #define YN(bit) ("no")
271 #define NY(bit) ("yes")
273 struct timespec64 tv
;
275 memset(&tm
, 0, sizeof(struct rtc_time
));
277 if (hp_sdc_rtc_read_bbrtc(&tm
)) {
278 seq_puts(m
, "BBRTC\t\t: READ FAILED!\n");
281 "rtc_time\t: %ptRt\n"
282 "rtc_date\t: %ptRd\n"
283 "rtc_epoch\t: %04lu\n",
287 if (hp_sdc_rtc_read_rt(&tv
)) {
288 seq_puts(m
, "i8042 rtc\t: READ FAILED!\n");
290 seq_printf(m
, "i8042 rtc\t: %lld.%02ld seconds\n",
291 (s64
)tv
.tv_sec
, (long)tv
.tv_nsec
/1000000L);
294 if (hp_sdc_rtc_read_fhs(&tv
)) {
295 seq_puts(m
, "handshake\t: READ FAILED!\n");
297 seq_printf(m
, "handshake\t: %lld.%02ld seconds\n",
298 (s64
)tv
.tv_sec
, (long)tv
.tv_nsec
/1000000L);
301 if (hp_sdc_rtc_read_mt(&tv
)) {
302 seq_puts(m
, "alarm\t\t: READ FAILED!\n");
304 seq_printf(m
, "alarm\t\t: %lld.%02ld seconds\n",
305 (s64
)tv
.tv_sec
, (long)tv
.tv_nsec
/1000000L);
308 if (hp_sdc_rtc_read_dt(&tv
)) {
309 seq_puts(m
, "delay\t\t: READ FAILED!\n");
311 seq_printf(m
, "delay\t\t: %lld.%02ld seconds\n",
312 (s64
)tv
.tv_sec
, (long)tv
.tv_nsec
/1000000L);
315 if (hp_sdc_rtc_read_ct(&tv
)) {
316 seq_puts(m
, "periodic\t: READ FAILED!\n");
318 seq_printf(m
, "periodic\t: %lld.%02ld seconds\n",
319 (s64
)tv
.tv_sec
, (long)tv
.tv_nsec
/1000000L);
326 "square_wave\t: %s\n"
329 "periodic_IRQ\t: %s\n"
330 "periodic_freq\t: %ld\n"
331 "batt_status\t: %s\n",
340 1 ? "okay" : "dead");
347 static int __init
hp_sdc_rtc_init(void)
356 sema_init(&i8042tregs
, 1);
358 if ((ret
= hp_sdc_request_timer_irq(&hp_sdc_rtc_isr
)))
361 proc_create_single("driver/rtc", 0, NULL
, hp_sdc_rtc_proc_show
);
363 printk(KERN_INFO
"HP i8042 SDC + MSM-58321 RTC support loaded "
364 "(RTC v " RTC_VERSION
")\n");
369 static void __exit
hp_sdc_rtc_exit(void)
371 remove_proc_entry ("driver/rtc", NULL
);
372 hp_sdc_release_timer_irq(hp_sdc_rtc_isr
);
373 printk(KERN_INFO
"HP i8042 SDC + MSM-58321 RTC support unloaded\n");
376 module_init(hp_sdc_rtc_init
);
377 module_exit(hp_sdc_rtc_exit
);