Path: blob/master/arch/powerpc/platforms/maple/time.c
10818 views
/*1* (c) Copyright 2004 Benjamin Herrenschmidt ([email protected]),2* IBM Corp.3*4* This program is free software; you can redistribute it and/or5* modify it under the terms of the GNU General Public License6* as published by the Free Software Foundation; either version7* 2 of the License, or (at your option) any later version.8*9*/1011#undef DEBUG1213#include <linux/errno.h>14#include <linux/sched.h>15#include <linux/kernel.h>16#include <linux/param.h>17#include <linux/string.h>18#include <linux/mm.h>19#include <linux/init.h>20#include <linux/time.h>21#include <linux/adb.h>22#include <linux/pmu.h>23#include <linux/interrupt.h>24#include <linux/mc146818rtc.h>25#include <linux/bcd.h>2627#include <asm/sections.h>28#include <asm/prom.h>29#include <asm/system.h>30#include <asm/io.h>31#include <asm/pgtable.h>32#include <asm/machdep.h>33#include <asm/time.h>3435#include "maple.h"3637#ifdef DEBUG38#define DBG(x...) printk(x)39#else40#define DBG(x...)41#endif4243static int maple_rtc_addr;4445static int maple_clock_read(int addr)46{47outb_p(addr, maple_rtc_addr);48return inb_p(maple_rtc_addr+1);49}5051static void maple_clock_write(unsigned long val, int addr)52{53outb_p(addr, maple_rtc_addr);54outb_p(val, maple_rtc_addr+1);55}5657void maple_get_rtc_time(struct rtc_time *tm)58{59do {60tm->tm_sec = maple_clock_read(RTC_SECONDS);61tm->tm_min = maple_clock_read(RTC_MINUTES);62tm->tm_hour = maple_clock_read(RTC_HOURS);63tm->tm_mday = maple_clock_read(RTC_DAY_OF_MONTH);64tm->tm_mon = maple_clock_read(RTC_MONTH);65tm->tm_year = maple_clock_read(RTC_YEAR);66} while (tm->tm_sec != maple_clock_read(RTC_SECONDS));6768if (!(maple_clock_read(RTC_CONTROL) & RTC_DM_BINARY)69|| RTC_ALWAYS_BCD) {70tm->tm_sec = bcd2bin(tm->tm_sec);71tm->tm_min = bcd2bin(tm->tm_min);72tm->tm_hour = bcd2bin(tm->tm_hour);73tm->tm_mday = bcd2bin(tm->tm_mday);74tm->tm_mon = bcd2bin(tm->tm_mon);75tm->tm_year = bcd2bin(tm->tm_year);76}77if ((tm->tm_year + 1900) < 1970)78tm->tm_year += 100;7980GregorianDay(tm);81}8283int maple_set_rtc_time(struct rtc_time *tm)84{85unsigned char save_control, save_freq_select;86int sec, min, hour, mon, mday, year;8788spin_lock(&rtc_lock);8990save_control = maple_clock_read(RTC_CONTROL); /* tell the clock it's being set */9192maple_clock_write((save_control|RTC_SET), RTC_CONTROL);9394save_freq_select = maple_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */9596maple_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);9798sec = tm->tm_sec;99min = tm->tm_min;100hour = tm->tm_hour;101mon = tm->tm_mon;102mday = tm->tm_mday;103year = tm->tm_year;104105if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {106sec = bin2bcd(sec);107min = bin2bcd(min);108hour = bin2bcd(hour);109mon = bin2bcd(mon);110mday = bin2bcd(mday);111year = bin2bcd(year);112}113maple_clock_write(sec, RTC_SECONDS);114maple_clock_write(min, RTC_MINUTES);115maple_clock_write(hour, RTC_HOURS);116maple_clock_write(mon, RTC_MONTH);117maple_clock_write(mday, RTC_DAY_OF_MONTH);118maple_clock_write(year, RTC_YEAR);119120/* The following flags have to be released exactly in this order,121* otherwise the DS12887 (popular MC146818A clone with integrated122* battery and quartz) will not reset the oscillator and will not123* update precisely 500 ms later. You won't find this mentioned in124* the Dallas Semiconductor data sheets, but who believes data125* sheets anyway ... -- Markus Kuhn126*/127maple_clock_write(save_control, RTC_CONTROL);128maple_clock_write(save_freq_select, RTC_FREQ_SELECT);129130spin_unlock(&rtc_lock);131132return 0;133}134135static struct resource rtc_iores = {136.name = "rtc",137.flags = IORESOURCE_BUSY,138};139140unsigned long __init maple_get_boot_time(void)141{142struct rtc_time tm;143struct device_node *rtcs;144145rtcs = of_find_compatible_node(NULL, "rtc", "pnpPNP,b00");146if (rtcs) {147struct resource r;148if (of_address_to_resource(rtcs, 0, &r)) {149printk(KERN_EMERG "Maple: Unable to translate RTC"150" address\n");151goto bail;152}153if (!(r.flags & IORESOURCE_IO)) {154printk(KERN_EMERG "Maple: RTC address isn't PIO!\n");155goto bail;156}157maple_rtc_addr = r.start;158printk(KERN_INFO "Maple: Found RTC at IO 0x%x\n",159maple_rtc_addr);160}161bail:162if (maple_rtc_addr == 0) {163maple_rtc_addr = RTC_PORT(0); /* legacy address */164printk(KERN_INFO "Maple: No device node for RTC, assuming "165"legacy address (0x%x)\n", maple_rtc_addr);166}167168rtc_iores.start = maple_rtc_addr;169rtc_iores.end = maple_rtc_addr + 7;170request_resource(&ioport_resource, &rtc_iores);171172maple_get_rtc_time(&tm);173return mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,174tm.tm_hour, tm.tm_min, tm.tm_sec);175}176177178179