2323#include <linux/mm.h>
2424#include <linux/interrupt.h>
2525#include <linux/time.h>
26+ #include <linux/rtc.h>
27+ #include <linux/rtc/m48t59.h>
2628#include <linux/timex.h>
2729#include <linux/init.h>
2830#include <linux/pci.h>
2931#include <linux/ioport.h>
3032#include <linux/profile.h>
3133#include <linux/of.h>
3234#include <linux/of_device.h>
35+ #include <linux/platform_device.h>
3336
3437#include <asm/oplib.h>
3538#include <asm/timer.h>
36- #include <asm/mostek.h>
3739#include <asm/system.h>
3840#include <asm/irq.h>
3941#include <asm/io.h>
4648#include "irq.h"
4749
4850DEFINE_SPINLOCK (rtc_lock );
49- static enum sparc_clock_type sp_clock_typ ;
50- DEFINE_SPINLOCK (mostek_lock );
51- void __iomem * mstk48t02_regs = NULL ;
52- static struct mostek48t08 __iomem * mstk48t08_regs = NULL ;
5351static int set_rtc_mmss (unsigned long );
5452static int sbus_do_settimeofday (struct timespec * tv );
5553
@@ -118,107 +116,55 @@ static irqreturn_t timer_interrupt(int dummy, void *dev_id)
118116 return IRQ_HANDLED ;
119117}
120118
121- /* Kick start a stopped clock (procedure from the Sun NVRAM/hostid FAQ). */
122- static void __devinit kick_start_clock (void )
119+ static unsigned char mostek_read_byte (struct device * dev , u32 ofs )
123120{
124- struct mostek48t02 * regs = (struct mostek48t02 * )mstk48t02_regs ;
125- unsigned char sec ;
126- int i , count ;
127-
128- prom_printf ("CLOCK: Clock was stopped. Kick start " );
129-
130- spin_lock_irq (& mostek_lock );
131-
132- /* Turn on the kick start bit to start the oscillator. */
133- regs -> creg |= MSTK_CREG_WRITE ;
134- regs -> sec &= ~MSTK_STOP ;
135- regs -> hour |= MSTK_KICK_START ;
136- regs -> creg &= ~MSTK_CREG_WRITE ;
137-
138- spin_unlock_irq (& mostek_lock );
139-
140- /* Delay to allow the clock oscillator to start. */
141- sec = MSTK_REG_SEC (regs );
142- for (i = 0 ; i < 3 ; i ++ ) {
143- while (sec == MSTK_REG_SEC (regs ))
144- for (count = 0 ; count < 100000 ; count ++ )
145- /* nothing */ ;
146- prom_printf ("." );
147- sec = regs -> sec ;
121+ struct platform_device * pdev = to_platform_device (dev );
122+ struct m48t59_plat_data * pdata = pdev -> dev .platform_data ;
123+ void __iomem * regs = pdata -> ioaddr ;
124+ unsigned char val = readb (regs + ofs );
125+
126+ /* the year 0 is 1968 */
127+ if (ofs == pdata -> offset + M48T59_YEAR ) {
128+ val += 0x68 ;
129+ if ((val & 0xf ) > 9 )
130+ val += 6 ;
148131 }
149- prom_printf ("\n" );
150-
151- spin_lock_irq (& mostek_lock );
152-
153- /* Turn off kick start and set a "valid" time and date. */
154- regs -> creg |= MSTK_CREG_WRITE ;
155- regs -> hour &= ~MSTK_KICK_START ;
156- MSTK_SET_REG_SEC (regs ,0 );
157- MSTK_SET_REG_MIN (regs ,0 );
158- MSTK_SET_REG_HOUR (regs ,0 );
159- MSTK_SET_REG_DOW (regs ,5 );
160- MSTK_SET_REG_DOM (regs ,1 );
161- MSTK_SET_REG_MONTH (regs ,8 );
162- MSTK_SET_REG_YEAR (regs ,1996 - MSTK_YEAR_ZERO );
163- regs -> creg &= ~MSTK_CREG_WRITE ;
164-
165- spin_unlock_irq (& mostek_lock );
166-
167- /* Ensure the kick start bit is off. If it isn't, turn it off. */
168- while (regs -> hour & MSTK_KICK_START ) {
169- prom_printf ("CLOCK: Kick start still on!\n" );
170-
171- spin_lock_irq (& mostek_lock );
172- regs -> creg |= MSTK_CREG_WRITE ;
173- regs -> hour &= ~MSTK_KICK_START ;
174- regs -> creg &= ~MSTK_CREG_WRITE ;
175- spin_unlock_irq (& mostek_lock );
176- }
177-
178- prom_printf ("CLOCK: Kick start procedure successful.\n" );
132+ return val ;
179133}
180134
181- /* Return nonzero if the clock chip battery is low. */
182- static inline int has_low_battery (void )
135+ static void mostek_write_byte (struct device * dev , u32 ofs , u8 val )
183136{
184- struct mostek48t02 * regs = (struct mostek48t02 * )mstk48t02_regs ;
185- unsigned char data1 , data2 ;
186-
187- spin_lock_irq (& mostek_lock );
188- data1 = regs -> eeprom [0 ]; /* Read some data. */
189- regs -> eeprom [0 ] = ~data1 ; /* Write back the complement. */
190- data2 = regs -> eeprom [0 ]; /* Read back the complement. */
191- regs -> eeprom [0 ] = data1 ; /* Restore the original value. */
192- spin_unlock_irq (& mostek_lock );
193-
194- return (data1 == data2 ); /* Was the write blocked? */
137+ struct platform_device * pdev = to_platform_device (dev );
138+ struct m48t59_plat_data * pdata = pdev -> dev .platform_data ;
139+ void __iomem * regs = pdata -> ioaddr ;
140+
141+ if (ofs == pdata -> offset + M48T59_YEAR ) {
142+ if (val < 0x68 )
143+ val += 0x32 ;
144+ else
145+ val -= 0x68 ;
146+ if ((val & 0xf ) > 9 )
147+ val += 6 ;
148+ if ((val & 0xf0 ) > 0x9A )
149+ val += 0x60 ;
150+ }
151+ writeb (val , regs + ofs );
195152}
196153
197- static void __devinit mostek_set_system_time (void )
198- {
199- unsigned int year , mon , day , hour , min , sec ;
200- struct mostek48t02 * mregs ;
201-
202- mregs = (struct mostek48t02 * )mstk48t02_regs ;
203- if (!mregs ) {
204- prom_printf ("Something wrong, clock regs not mapped yet.\n" );
205- prom_halt ();
206- }
207- spin_lock_irq (& mostek_lock );
208- mregs -> creg |= MSTK_CREG_READ ;
209- sec = MSTK_REG_SEC (mregs );
210- min = MSTK_REG_MIN (mregs );
211- hour = MSTK_REG_HOUR (mregs );
212- day = MSTK_REG_DOM (mregs );
213- mon = MSTK_REG_MONTH (mregs );
214- year = MSTK_CVT_YEAR ( MSTK_REG_YEAR (mregs ) );
215- xtime .tv_sec = mktime (year , mon , day , hour , min , sec );
216- xtime .tv_nsec = (INITIAL_JIFFIES % HZ ) * (NSEC_PER_SEC / HZ );
217- set_normalized_timespec (& wall_to_monotonic ,
218- - xtime .tv_sec , - xtime .tv_nsec );
219- mregs -> creg &= ~MSTK_CREG_READ ;
220- spin_unlock_irq (& mostek_lock );
221- }
154+ static struct m48t59_plat_data m48t59_data = {
155+ .read_byte = mostek_read_byte ,
156+ .write_byte = mostek_write_byte ,
157+ };
158+
159+ /* resource is set at runtime */
160+ static struct platform_device m48t59_rtc = {
161+ .name = "rtc-m48t59" ,
162+ .id = 0 ,
163+ .num_resources = 1 ,
164+ .dev = {
165+ .platform_data = & m48t59_data ,
166+ },
167+ };
222168
223169static int __devinit clock_probe (struct of_device * op , const struct of_device_id * match )
224170{
@@ -228,33 +174,21 @@ static int __devinit clock_probe(struct of_device *op, const struct of_device_id
228174 if (!model )
229175 return - ENODEV ;
230176
177+ m48t59_rtc .resource = & op -> resource [0 ];
231178 if (!strcmp (model , "mk48t02" )) {
232- sp_clock_typ = MSTK48T02 ;
233-
234179 /* Map the clock register io area read-only */
235- mstk48t02_regs = of_ioremap (& op -> resource [0 ], 0 ,
236- sizeof (struct mostek48t02 ),
237- "mk48t02" );
238- mstk48t08_regs = NULL ; /* To catch weirdness */
180+ m48t59_data .ioaddr = of_ioremap (& op -> resource [0 ], 0 ,
181+ 2048 , "rtc-m48t59" );
182+ m48t59_data .type = M48T59RTC_TYPE_M48T02 ;
239183 } else if (!strcmp (model , "mk48t08" )) {
240- sp_clock_typ = MSTK48T08 ;
241- mstk48t08_regs = of_ioremap (& op -> resource [0 ], 0 ,
242- sizeof (struct mostek48t08 ),
243- "mk48t08" );
244-
245- mstk48t02_regs = & mstk48t08_regs -> regs ;
184+ m48t59_data .ioaddr = of_ioremap (& op -> resource [0 ], 0 ,
185+ 8192 , "rtc-m48t59" );
186+ m48t59_data .type = M48T59RTC_TYPE_M48T08 ;
246187 } else
247188 return - ENODEV ;
248189
249- /* Report a low battery voltage condition. */
250- if (has_low_battery ())
251- printk (KERN_CRIT "NVRAM: Low battery voltage!\n" );
252-
253- /* Kick start the clock if it is completely stopped. */
254- if (mostek_read (mstk48t02_regs + MOSTEK_SEC ) & MSTK_STOP )
255- kick_start_clock ();
256-
257- mostek_set_system_time ();
190+ if (platform_device_register (& m48t59_rtc ) < 0 )
191+ printk (KERN_ERR "Registering RTC device failed\n" );
258192
259193 return 0 ;
260194}
@@ -270,7 +204,7 @@ static struct of_platform_driver clock_driver = {
270204 .match_table = clock_match ,
271205 .probe = clock_probe ,
272206 .driver = {
273- .name = "clock " ,
207+ .name = "rtc " ,
274208 },
275209};
276210
@@ -400,43 +334,12 @@ static int sbus_do_settimeofday(struct timespec *tv)
400334 return 0 ;
401335}
402336
403- /*
404- * BUG: This routine does not handle hour overflow properly; it just
405- * sets the minutes. Usually you won't notice until after reboot!
406- */
407- static int set_rtc_mmss (unsigned long nowtime )
337+ static int set_rtc_mmss (unsigned long secs )
408338{
409- int real_seconds , real_minutes , mostek_minutes ;
410- struct mostek48t02 * regs = (struct mostek48t02 * )mstk48t02_regs ;
411- unsigned long flags ;
339+ struct rtc_device * rtc = rtc_class_open ("rtc0" );
412340
413- spin_lock_irqsave (& mostek_lock , flags );
414- /* Read the current RTC minutes. */
415- regs -> creg |= MSTK_CREG_READ ;
416- mostek_minutes = MSTK_REG_MIN (regs );
417- regs -> creg &= ~MSTK_CREG_READ ;
341+ if (rtc )
342+ return rtc_set_mmss (rtc , secs );
418343
419- /*
420- * since we're only adjusting minutes and seconds,
421- * don't interfere with hour overflow. This avoids
422- * messing with unknown time zones but requires your
423- * RTC not to be off by more than 15 minutes
424- */
425- real_seconds = nowtime % 60 ;
426- real_minutes = nowtime / 60 ;
427- if (((abs (real_minutes - mostek_minutes ) + 15 )/30 ) & 1 )
428- real_minutes += 30 ; /* correct for half hour time zone */
429- real_minutes %= 60 ;
430-
431- if (abs (real_minutes - mostek_minutes ) < 30 ) {
432- regs -> creg |= MSTK_CREG_WRITE ;
433- MSTK_SET_REG_SEC (regs ,real_seconds );
434- MSTK_SET_REG_MIN (regs ,real_minutes );
435- regs -> creg &= ~MSTK_CREG_WRITE ;
436- spin_unlock_irqrestore (& mostek_lock , flags );
437- return 0 ;
438- } else {
439- spin_unlock_irqrestore (& mostek_lock , flags );
440- return -1 ;
441- }
344+ return -1 ;
442345}
0 commit comments