Path: blob/master/arch/mips/sibyte/swarm/rtc_m41t81.c
10818 views
/*1* Copyright (C) 2000, 2001 Broadcom Corporation2*3* Copyright (C) 2002 MontaVista Software Inc.4* Author: [email protected] or [email protected]5*6* This program is free software; you can redistribute it and/or modify it7* under the terms of the GNU General Public License as published by the8* Free Software Foundation; either version 2 of the License, or (at your9* option) any later version.10*11*/12#include <linux/bcd.h>13#include <linux/types.h>14#include <linux/time.h>1516#include <asm/time.h>17#include <asm/addrspace.h>18#include <asm/io.h>1920#include <asm/sibyte/sb1250.h>21#include <asm/sibyte/sb1250_regs.h>22#include <asm/sibyte/sb1250_smbus.h>232425/* M41T81 definitions */2627/*28* Register bits29*/3031#define M41T81REG_SC_ST 0x80 /* stop bit */32#define M41T81REG_HR_CB 0x40 /* century bit */33#define M41T81REG_HR_CEB 0x80 /* century enable bit */34#define M41T81REG_CTL_S 0x20 /* sign bit */35#define M41T81REG_CTL_FT 0x40 /* frequency test bit */36#define M41T81REG_CTL_OUT 0x80 /* output level */37#define M41T81REG_WD_RB0 0x01 /* watchdog resolution bit 0 */38#define M41T81REG_WD_RB1 0x02 /* watchdog resolution bit 1 */39#define M41T81REG_WD_BMB0 0x04 /* watchdog multiplier bit 0 */40#define M41T81REG_WD_BMB1 0x08 /* watchdog multiplier bit 1 */41#define M41T81REG_WD_BMB2 0x10 /* watchdog multiplier bit 2 */42#define M41T81REG_WD_BMB3 0x20 /* watchdog multiplier bit 3 */43#define M41T81REG_WD_BMB4 0x40 /* watchdog multiplier bit 4 */44#define M41T81REG_AMO_ABE 0x20 /* alarm in "battery back-up mode" enable bit */45#define M41T81REG_AMO_SQWE 0x40 /* square wave enable */46#define M41T81REG_AMO_AFE 0x80 /* alarm flag enable flag */47#define M41T81REG_ADT_RPT5 0x40 /* alarm repeat mode bit 5 */48#define M41T81REG_ADT_RPT4 0x80 /* alarm repeat mode bit 4 */49#define M41T81REG_AHR_RPT3 0x80 /* alarm repeat mode bit 3 */50#define M41T81REG_AHR_HT 0x40 /* halt update bit */51#define M41T81REG_AMN_RPT2 0x80 /* alarm repeat mode bit 2 */52#define M41T81REG_ASC_RPT1 0x80 /* alarm repeat mode bit 1 */53#define M41T81REG_FLG_AF 0x40 /* alarm flag (read only) */54#define M41T81REG_FLG_WDF 0x80 /* watchdog flag (read only) */55#define M41T81REG_SQW_RS0 0x10 /* sqw frequency bit 0 */56#define M41T81REG_SQW_RS1 0x20 /* sqw frequency bit 1 */57#define M41T81REG_SQW_RS2 0x40 /* sqw frequency bit 2 */58#define M41T81REG_SQW_RS3 0x80 /* sqw frequency bit 3 */596061/*62* Register numbers63*/6465#define M41T81REG_TSC 0x00 /* tenths/hundredths of second */66#define M41T81REG_SC 0x01 /* seconds */67#define M41T81REG_MN 0x02 /* minute */68#define M41T81REG_HR 0x03 /* hour/century */69#define M41T81REG_DY 0x04 /* day of week */70#define M41T81REG_DT 0x05 /* date of month */71#define M41T81REG_MO 0x06 /* month */72#define M41T81REG_YR 0x07 /* year */73#define M41T81REG_CTL 0x08 /* control */74#define M41T81REG_WD 0x09 /* watchdog */75#define M41T81REG_AMO 0x0A /* alarm: month */76#define M41T81REG_ADT 0x0B /* alarm: date */77#define M41T81REG_AHR 0x0C /* alarm: hour */78#define M41T81REG_AMN 0x0D /* alarm: minute */79#define M41T81REG_ASC 0x0E /* alarm: second */80#define M41T81REG_FLG 0x0F /* flags */81#define M41T81REG_SQW 0x13 /* square wave register */8283#define M41T81_CCR_ADDRESS 0x688485#define SMB_CSR(reg) IOADDR(A_SMB_REGISTER(1, reg))8687static int m41t81_read(uint8_t addr)88{89while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY)90;9192__raw_writeq(addr & 0xff, SMB_CSR(R_SMB_CMD));93__raw_writeq(V_SMB_ADDR(M41T81_CCR_ADDRESS) | V_SMB_TT_WR1BYTE,94SMB_CSR(R_SMB_START));9596while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY)97;9899__raw_writeq(V_SMB_ADDR(M41T81_CCR_ADDRESS) | V_SMB_TT_RD1BYTE,100SMB_CSR(R_SMB_START));101102while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY)103;104105if (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_ERROR) {106/* Clear error bit by writing a 1 */107__raw_writeq(M_SMB_ERROR, SMB_CSR(R_SMB_STATUS));108return -1;109}110111return (__raw_readq(SMB_CSR(R_SMB_DATA)) & 0xff);112}113114static int m41t81_write(uint8_t addr, int b)115{116while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY)117;118119__raw_writeq(addr & 0xff, SMB_CSR(R_SMB_CMD));120__raw_writeq(b & 0xff, SMB_CSR(R_SMB_DATA));121__raw_writeq(V_SMB_ADDR(M41T81_CCR_ADDRESS) | V_SMB_TT_WR2BYTE,122SMB_CSR(R_SMB_START));123124while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY)125;126127if (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_ERROR) {128/* Clear error bit by writing a 1 */129__raw_writeq(M_SMB_ERROR, SMB_CSR(R_SMB_STATUS));130return -1;131}132133/* read the same byte again to make sure it is written */134__raw_writeq(V_SMB_ADDR(M41T81_CCR_ADDRESS) | V_SMB_TT_RD1BYTE,135SMB_CSR(R_SMB_START));136137while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY)138;139140return 0;141}142143int m41t81_set_time(unsigned long t)144{145struct rtc_time tm;146unsigned long flags;147148/* Note we don't care about the century */149rtc_time_to_tm(t, &tm);150151/*152* Note the write order matters as it ensures the correctness.153* When we write sec, 10th sec is clear. It is reasonable to154* believe we should finish writing min within a second.155*/156157spin_lock_irqsave(&rtc_lock, flags);158tm.tm_sec = bin2bcd(tm.tm_sec);159m41t81_write(M41T81REG_SC, tm.tm_sec);160161tm.tm_min = bin2bcd(tm.tm_min);162m41t81_write(M41T81REG_MN, tm.tm_min);163164tm.tm_hour = bin2bcd(tm.tm_hour);165tm.tm_hour = (tm.tm_hour & 0x3f) | (m41t81_read(M41T81REG_HR) & 0xc0);166m41t81_write(M41T81REG_HR, tm.tm_hour);167168/* tm_wday starts from 0 to 6 */169if (tm.tm_wday == 0) tm.tm_wday = 7;170tm.tm_wday = bin2bcd(tm.tm_wday);171m41t81_write(M41T81REG_DY, tm.tm_wday);172173tm.tm_mday = bin2bcd(tm.tm_mday);174m41t81_write(M41T81REG_DT, tm.tm_mday);175176/* tm_mon starts from 0, *ick* */177tm.tm_mon ++;178tm.tm_mon = bin2bcd(tm.tm_mon);179m41t81_write(M41T81REG_MO, tm.tm_mon);180181/* we don't do century, everything is beyond 2000 */182tm.tm_year %= 100;183tm.tm_year = bin2bcd(tm.tm_year);184m41t81_write(M41T81REG_YR, tm.tm_year);185spin_unlock_irqrestore(&rtc_lock, flags);186187return 0;188}189190unsigned long m41t81_get_time(void)191{192unsigned int year, mon, day, hour, min, sec;193unsigned long flags;194195/*196* min is valid if two reads of sec are the same.197*/198for (;;) {199spin_lock_irqsave(&rtc_lock, flags);200sec = m41t81_read(M41T81REG_SC);201min = m41t81_read(M41T81REG_MN);202if (sec == m41t81_read(M41T81REG_SC)) break;203spin_unlock_irqrestore(&rtc_lock, flags);204}205hour = m41t81_read(M41T81REG_HR) & 0x3f;206day = m41t81_read(M41T81REG_DT);207mon = m41t81_read(M41T81REG_MO);208year = m41t81_read(M41T81REG_YR);209spin_unlock_irqrestore(&rtc_lock, flags);210211sec = bcd2bin(sec);212min = bcd2bin(min);213hour = bcd2bin(hour);214day = bcd2bin(day);215mon = bcd2bin(mon);216year = bcd2bin(year);217218year += 2000;219220return mktime(year, mon, day, hour, min, sec);221}222223int m41t81_probe(void)224{225unsigned int tmp;226227/* enable chip if it is not enabled yet */228tmp = m41t81_read(M41T81REG_SC);229m41t81_write(M41T81REG_SC, tmp & 0x7f);230231return (m41t81_read(M41T81REG_SC) != -1);232}233234235