18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2000, 2001 Broadcom Corporation
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2002 MontaVista Software Inc.
68c2ecf20Sopenharmony_ci * Author: jsun@mvista.com or jsun@junsun.net
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci#include <linux/bcd.h>
98c2ecf20Sopenharmony_ci#include <linux/types.h>
108c2ecf20Sopenharmony_ci#include <linux/time.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <asm/time.h>
138c2ecf20Sopenharmony_ci#include <asm/addrspace.h>
148c2ecf20Sopenharmony_ci#include <asm/io.h>
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <asm/sibyte/sb1250.h>
178c2ecf20Sopenharmony_ci#include <asm/sibyte/sb1250_regs.h>
188c2ecf20Sopenharmony_ci#include <asm/sibyte/sb1250_smbus.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci/* M41T81 definitions */
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/*
248c2ecf20Sopenharmony_ci * Register bits
258c2ecf20Sopenharmony_ci */
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define M41T81REG_SC_ST		0x80		/* stop bit */
288c2ecf20Sopenharmony_ci#define M41T81REG_HR_CB		0x40		/* century bit */
298c2ecf20Sopenharmony_ci#define M41T81REG_HR_CEB	0x80		/* century enable bit */
308c2ecf20Sopenharmony_ci#define M41T81REG_CTL_S		0x20		/* sign bit */
318c2ecf20Sopenharmony_ci#define M41T81REG_CTL_FT	0x40		/* frequency test bit */
328c2ecf20Sopenharmony_ci#define M41T81REG_CTL_OUT	0x80		/* output level */
338c2ecf20Sopenharmony_ci#define M41T81REG_WD_RB0	0x01		/* watchdog resolution bit 0 */
348c2ecf20Sopenharmony_ci#define M41T81REG_WD_RB1	0x02		/* watchdog resolution bit 1 */
358c2ecf20Sopenharmony_ci#define M41T81REG_WD_BMB0	0x04		/* watchdog multiplier bit 0 */
368c2ecf20Sopenharmony_ci#define M41T81REG_WD_BMB1	0x08		/* watchdog multiplier bit 1 */
378c2ecf20Sopenharmony_ci#define M41T81REG_WD_BMB2	0x10		/* watchdog multiplier bit 2 */
388c2ecf20Sopenharmony_ci#define M41T81REG_WD_BMB3	0x20		/* watchdog multiplier bit 3 */
398c2ecf20Sopenharmony_ci#define M41T81REG_WD_BMB4	0x40		/* watchdog multiplier bit 4 */
408c2ecf20Sopenharmony_ci#define M41T81REG_AMO_ABE	0x20		/* alarm in "battery back-up mode" enable bit */
418c2ecf20Sopenharmony_ci#define M41T81REG_AMO_SQWE	0x40		/* square wave enable */
428c2ecf20Sopenharmony_ci#define M41T81REG_AMO_AFE	0x80		/* alarm flag enable flag */
438c2ecf20Sopenharmony_ci#define M41T81REG_ADT_RPT5	0x40		/* alarm repeat mode bit 5 */
448c2ecf20Sopenharmony_ci#define M41T81REG_ADT_RPT4	0x80		/* alarm repeat mode bit 4 */
458c2ecf20Sopenharmony_ci#define M41T81REG_AHR_RPT3	0x80		/* alarm repeat mode bit 3 */
468c2ecf20Sopenharmony_ci#define M41T81REG_AHR_HT	0x40		/* halt update bit */
478c2ecf20Sopenharmony_ci#define M41T81REG_AMN_RPT2	0x80		/* alarm repeat mode bit 2 */
488c2ecf20Sopenharmony_ci#define M41T81REG_ASC_RPT1	0x80		/* alarm repeat mode bit 1 */
498c2ecf20Sopenharmony_ci#define M41T81REG_FLG_AF	0x40		/* alarm flag (read only) */
508c2ecf20Sopenharmony_ci#define M41T81REG_FLG_WDF	0x80		/* watchdog flag (read only) */
518c2ecf20Sopenharmony_ci#define M41T81REG_SQW_RS0	0x10		/* sqw frequency bit 0 */
528c2ecf20Sopenharmony_ci#define M41T81REG_SQW_RS1	0x20		/* sqw frequency bit 1 */
538c2ecf20Sopenharmony_ci#define M41T81REG_SQW_RS2	0x40		/* sqw frequency bit 2 */
548c2ecf20Sopenharmony_ci#define M41T81REG_SQW_RS3	0x80		/* sqw frequency bit 3 */
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci/*
588c2ecf20Sopenharmony_ci * Register numbers
598c2ecf20Sopenharmony_ci */
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci#define M41T81REG_TSC	0x00		/* tenths/hundredths of second */
628c2ecf20Sopenharmony_ci#define M41T81REG_SC	0x01		/* seconds */
638c2ecf20Sopenharmony_ci#define M41T81REG_MN	0x02		/* minute */
648c2ecf20Sopenharmony_ci#define M41T81REG_HR	0x03		/* hour/century */
658c2ecf20Sopenharmony_ci#define M41T81REG_DY	0x04		/* day of week */
668c2ecf20Sopenharmony_ci#define M41T81REG_DT	0x05		/* date of month */
678c2ecf20Sopenharmony_ci#define M41T81REG_MO	0x06		/* month */
688c2ecf20Sopenharmony_ci#define M41T81REG_YR	0x07		/* year */
698c2ecf20Sopenharmony_ci#define M41T81REG_CTL	0x08		/* control */
708c2ecf20Sopenharmony_ci#define M41T81REG_WD	0x09		/* watchdog */
718c2ecf20Sopenharmony_ci#define M41T81REG_AMO	0x0A		/* alarm: month */
728c2ecf20Sopenharmony_ci#define M41T81REG_ADT	0x0B		/* alarm: date */
738c2ecf20Sopenharmony_ci#define M41T81REG_AHR	0x0C		/* alarm: hour */
748c2ecf20Sopenharmony_ci#define M41T81REG_AMN	0x0D		/* alarm: minute */
758c2ecf20Sopenharmony_ci#define M41T81REG_ASC	0x0E		/* alarm: second */
768c2ecf20Sopenharmony_ci#define M41T81REG_FLG	0x0F		/* flags */
778c2ecf20Sopenharmony_ci#define M41T81REG_SQW	0x13		/* square wave register */
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci#define M41T81_CCR_ADDRESS	0x68
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci#define SMB_CSR(reg)	IOADDR(A_SMB_REGISTER(1, reg))
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistatic int m41t81_read(uint8_t addr)
848c2ecf20Sopenharmony_ci{
858c2ecf20Sopenharmony_ci	while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY)
868c2ecf20Sopenharmony_ci		;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	__raw_writeq(addr & 0xff, SMB_CSR(R_SMB_CMD));
898c2ecf20Sopenharmony_ci	__raw_writeq(V_SMB_ADDR(M41T81_CCR_ADDRESS) | V_SMB_TT_WR1BYTE,
908c2ecf20Sopenharmony_ci		     SMB_CSR(R_SMB_START));
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY)
938c2ecf20Sopenharmony_ci		;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	__raw_writeq(V_SMB_ADDR(M41T81_CCR_ADDRESS) | V_SMB_TT_RD1BYTE,
968c2ecf20Sopenharmony_ci		     SMB_CSR(R_SMB_START));
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY)
998c2ecf20Sopenharmony_ci		;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	if (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_ERROR) {
1028c2ecf20Sopenharmony_ci		/* Clear error bit by writing a 1 */
1038c2ecf20Sopenharmony_ci		__raw_writeq(M_SMB_ERROR, SMB_CSR(R_SMB_STATUS));
1048c2ecf20Sopenharmony_ci		return -1;
1058c2ecf20Sopenharmony_ci	}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	return __raw_readq(SMB_CSR(R_SMB_DATA)) & 0xff;
1088c2ecf20Sopenharmony_ci}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_cistatic int m41t81_write(uint8_t addr, int b)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY)
1138c2ecf20Sopenharmony_ci		;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	__raw_writeq(addr & 0xff, SMB_CSR(R_SMB_CMD));
1168c2ecf20Sopenharmony_ci	__raw_writeq(b & 0xff, SMB_CSR(R_SMB_DATA));
1178c2ecf20Sopenharmony_ci	__raw_writeq(V_SMB_ADDR(M41T81_CCR_ADDRESS) | V_SMB_TT_WR2BYTE,
1188c2ecf20Sopenharmony_ci		     SMB_CSR(R_SMB_START));
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY)
1218c2ecf20Sopenharmony_ci		;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	if (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_ERROR) {
1248c2ecf20Sopenharmony_ci		/* Clear error bit by writing a 1 */
1258c2ecf20Sopenharmony_ci		__raw_writeq(M_SMB_ERROR, SMB_CSR(R_SMB_STATUS));
1268c2ecf20Sopenharmony_ci		return -1;
1278c2ecf20Sopenharmony_ci	}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	/* read the same byte again to make sure it is written */
1308c2ecf20Sopenharmony_ci	__raw_writeq(V_SMB_ADDR(M41T81_CCR_ADDRESS) | V_SMB_TT_RD1BYTE,
1318c2ecf20Sopenharmony_ci		     SMB_CSR(R_SMB_START));
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	while (__raw_readq(SMB_CSR(R_SMB_STATUS)) & M_SMB_BUSY)
1348c2ecf20Sopenharmony_ci		;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	return 0;
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ciint m41t81_set_time(time64_t t)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	struct rtc_time tm;
1428c2ecf20Sopenharmony_ci	unsigned long flags;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	/* Note we don't care about the century */
1458c2ecf20Sopenharmony_ci	rtc_time64_to_tm(t, &tm);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	/*
1488c2ecf20Sopenharmony_ci	 * Note the write order matters as it ensures the correctness.
1498c2ecf20Sopenharmony_ci	 * When we write sec, 10th sec is clear.  It is reasonable to
1508c2ecf20Sopenharmony_ci	 * believe we should finish writing min within a second.
1518c2ecf20Sopenharmony_ci	 */
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	spin_lock_irqsave(&rtc_lock, flags);
1548c2ecf20Sopenharmony_ci	tm.tm_sec = bin2bcd(tm.tm_sec);
1558c2ecf20Sopenharmony_ci	m41t81_write(M41T81REG_SC, tm.tm_sec);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	tm.tm_min = bin2bcd(tm.tm_min);
1588c2ecf20Sopenharmony_ci	m41t81_write(M41T81REG_MN, tm.tm_min);
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	tm.tm_hour = bin2bcd(tm.tm_hour);
1618c2ecf20Sopenharmony_ci	tm.tm_hour = (tm.tm_hour & 0x3f) | (m41t81_read(M41T81REG_HR) & 0xc0);
1628c2ecf20Sopenharmony_ci	m41t81_write(M41T81REG_HR, tm.tm_hour);
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	/* tm_wday starts from 0 to 6 */
1658c2ecf20Sopenharmony_ci	if (tm.tm_wday == 0) tm.tm_wday = 7;
1668c2ecf20Sopenharmony_ci	tm.tm_wday = bin2bcd(tm.tm_wday);
1678c2ecf20Sopenharmony_ci	m41t81_write(M41T81REG_DY, tm.tm_wday);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	tm.tm_mday = bin2bcd(tm.tm_mday);
1708c2ecf20Sopenharmony_ci	m41t81_write(M41T81REG_DT, tm.tm_mday);
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	/* tm_mon starts from 0, *ick* */
1738c2ecf20Sopenharmony_ci	tm.tm_mon ++;
1748c2ecf20Sopenharmony_ci	tm.tm_mon = bin2bcd(tm.tm_mon);
1758c2ecf20Sopenharmony_ci	m41t81_write(M41T81REG_MO, tm.tm_mon);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	/* we don't do century, everything is beyond 2000 */
1788c2ecf20Sopenharmony_ci	tm.tm_year %= 100;
1798c2ecf20Sopenharmony_ci	tm.tm_year = bin2bcd(tm.tm_year);
1808c2ecf20Sopenharmony_ci	m41t81_write(M41T81REG_YR, tm.tm_year);
1818c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&rtc_lock, flags);
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	return 0;
1848c2ecf20Sopenharmony_ci}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_citime64_t m41t81_get_time(void)
1878c2ecf20Sopenharmony_ci{
1888c2ecf20Sopenharmony_ci	unsigned int year, mon, day, hour, min, sec;
1898c2ecf20Sopenharmony_ci	unsigned long flags;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	/*
1928c2ecf20Sopenharmony_ci	 * min is valid if two reads of sec are the same.
1938c2ecf20Sopenharmony_ci	 */
1948c2ecf20Sopenharmony_ci	for (;;) {
1958c2ecf20Sopenharmony_ci		spin_lock_irqsave(&rtc_lock, flags);
1968c2ecf20Sopenharmony_ci		sec = m41t81_read(M41T81REG_SC);
1978c2ecf20Sopenharmony_ci		min = m41t81_read(M41T81REG_MN);
1988c2ecf20Sopenharmony_ci		if (sec == m41t81_read(M41T81REG_SC)) break;
1998c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&rtc_lock, flags);
2008c2ecf20Sopenharmony_ci	}
2018c2ecf20Sopenharmony_ci	hour = m41t81_read(M41T81REG_HR) & 0x3f;
2028c2ecf20Sopenharmony_ci	day = m41t81_read(M41T81REG_DT);
2038c2ecf20Sopenharmony_ci	mon = m41t81_read(M41T81REG_MO);
2048c2ecf20Sopenharmony_ci	year = m41t81_read(M41T81REG_YR);
2058c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&rtc_lock, flags);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	sec = bcd2bin(sec);
2088c2ecf20Sopenharmony_ci	min = bcd2bin(min);
2098c2ecf20Sopenharmony_ci	hour = bcd2bin(hour);
2108c2ecf20Sopenharmony_ci	day = bcd2bin(day);
2118c2ecf20Sopenharmony_ci	mon = bcd2bin(mon);
2128c2ecf20Sopenharmony_ci	year = bcd2bin(year);
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	year += 2000;
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	return mktime64(year, mon, day, hour, min, sec);
2178c2ecf20Sopenharmony_ci}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ciint m41t81_probe(void)
2208c2ecf20Sopenharmony_ci{
2218c2ecf20Sopenharmony_ci	unsigned int tmp;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	/* enable chip if it is not enabled yet */
2248c2ecf20Sopenharmony_ci	tmp = m41t81_read(M41T81REG_SC);
2258c2ecf20Sopenharmony_ci	m41t81_write(M41T81REG_SC, tmp & 0x7f);
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	return m41t81_read(M41T81REG_SC) != -1;
2288c2ecf20Sopenharmony_ci}
229