rtc-vr41xx.c 8.8 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 *  Driver for NEC VR4100 series Real Time Clock unit.
L
Linus Torvalds 已提交
3
 *
4
 *  Copyright (C) 2003-2008  Yoichi Yuasa <yuasa@linux-mips.org>
L
Linus Torvalds 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
20
#include <linux/err.h>
L
Linus Torvalds 已提交
21 22
#include <linux/fs.h>
#include <linux/init.h>
23
#include <linux/io.h>
L
Linus Torvalds 已提交
24
#include <linux/ioport.h>
25
#include <linux/interrupt.h>
L
Linus Torvalds 已提交
26
#include <linux/module.h>
27
#include <linux/platform_device.h>
L
Linus Torvalds 已提交
28 29 30
#include <linux/rtc.h>
#include <linux/spinlock.h>
#include <linux/types.h>
31
#include <linux/uaccess.h>
32
#include <linux/log2.h>
L
Linus Torvalds 已提交
33 34 35

#include <asm/div64.h>

36
MODULE_AUTHOR("Yoichi Yuasa <yuasa@linux-mips.org>");
L
Linus Torvalds 已提交
37
MODULE_DESCRIPTION("NEC VR4100 series RTC driver");
38
MODULE_LICENSE("GPL v2");
L
Linus Torvalds 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

/* RTC 1 registers */
#define ETIMELREG		0x00
#define ETIMEMREG		0x02
#define ETIMEHREG		0x04
/* RFU */
#define ECMPLREG		0x08
#define ECMPMREG		0x0a
#define ECMPHREG		0x0c
/* RFU */
#define RTCL1LREG		0x10
#define RTCL1HREG		0x12
#define RTCL1CNTLREG		0x14
#define RTCL1CNTHREG		0x16
#define RTCL2LREG		0x18
#define RTCL2HREG		0x1a
#define RTCL2CNTLREG		0x1c
#define RTCL2CNTHREG		0x1e

/* RTC 2 registers */
#define TCLKLREG		0x00
#define TCLKHREG		0x02
#define TCLKCNTLREG		0x04
#define TCLKCNTHREG		0x06
/* RFU */
#define RTCINTREG		0x1e
 #define TCLOCK_INT		0x08
 #define RTCLONG2_INT		0x04
 #define RTCLONG1_INT		0x02
 #define ELAPSEDTIME_INT	0x01

#define RTC_FREQUENCY		32768
#define MAX_PERIODIC_RATE	6553

static void __iomem *rtc1_base;
static void __iomem *rtc2_base;

#define rtc1_read(offset)		readw(rtc1_base + (offset))
#define rtc1_write(offset, value)	writew((value), rtc1_base + (offset))

#define rtc2_read(offset)		readw(rtc2_base + (offset))
#define rtc2_write(offset, value)	writew((value), rtc2_base + (offset))

static unsigned long epoch = 1970;	/* Jan 1 1970 00:00:00 */

I
Ingo Molnar 已提交
84
static DEFINE_SPINLOCK(rtc_lock);
L
Linus Torvalds 已提交
85 86
static char rtc_name[] = "RTC";
static unsigned long periodic_count;
Y
Yoichi Yuasa 已提交
87
static unsigned int alarm_enabled;
88 89
static int aie_irq;
static int pie_irq;
L
Linus Torvalds 已提交
90 91 92

static inline unsigned long read_elapsed_second(void)
{
93

L
Linus Torvalds 已提交
94
	unsigned long first_low, first_mid, first_high;
95

L
Linus Torvalds 已提交
96 97 98 99 100 101 102 103 104 105
	unsigned long second_low, second_mid, second_high;

	do {
		first_low = rtc1_read(ETIMELREG);
		first_mid = rtc1_read(ETIMEMREG);
		first_high = rtc1_read(ETIMEHREG);
		second_low = rtc1_read(ETIMELREG);
		second_mid = rtc1_read(ETIMEMREG);
		second_high = rtc1_read(ETIMEHREG);
	} while (first_low != second_low || first_mid != second_mid ||
106
		 first_high != second_high);
L
Linus Torvalds 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

	return (first_high << 17) | (first_mid << 1) | (first_low >> 15);
}

static inline void write_elapsed_second(unsigned long sec)
{
	spin_lock_irq(&rtc_lock);

	rtc1_write(ETIMELREG, (uint16_t)(sec << 15));
	rtc1_write(ETIMEMREG, (uint16_t)(sec >> 1));
	rtc1_write(ETIMEHREG, (uint16_t)(sec >> 17));

	spin_unlock_irq(&rtc_lock);
}

122
static void vr41xx_rtc_release(struct device *dev)
L
Linus Torvalds 已提交
123 124 125 126
{

	spin_lock_irq(&rtc_lock);

127 128 129 130 131
	rtc1_write(ECMPLREG, 0);
	rtc1_write(ECMPMREG, 0);
	rtc1_write(ECMPHREG, 0);
	rtc1_write(RTCL1LREG, 0);
	rtc1_write(RTCL1HREG, 0);
L
Linus Torvalds 已提交
132 133 134

	spin_unlock_irq(&rtc_lock);

135 136
	disable_irq(aie_irq);
	disable_irq(pie_irq);
L
Linus Torvalds 已提交
137 138
}

139
static int vr41xx_rtc_read_time(struct device *dev, struct rtc_time *time)
L
Linus Torvalds 已提交
140 141 142 143 144 145
{
	unsigned long epoch_sec, elapsed_sec;

	epoch_sec = mktime(epoch, 1, 1, 0, 0, 0);
	elapsed_sec = read_elapsed_second();

146 147 148
	rtc_time_to_tm(epoch_sec + elapsed_sec, time);

	return 0;
L
Linus Torvalds 已提交
149 150
}

151
static int vr41xx_rtc_set_time(struct device *dev, struct rtc_time *time)
L
Linus Torvalds 已提交
152 153 154 155 156
{
	unsigned long epoch_sec, current_sec;

	epoch_sec = mktime(epoch, 1, 1, 0, 0, 0);
	current_sec = mktime(time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
157
			     time->tm_hour, time->tm_min, time->tm_sec);
L
Linus Torvalds 已提交
158 159

	write_elapsed_second(current_sec - epoch_sec);
160 161

	return 0;
L
Linus Torvalds 已提交
162 163
}

164
static int vr41xx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
L
Linus Torvalds 已提交
165
{
166 167
	unsigned long low, mid, high;
	struct rtc_time *time = &wkalrm->time;
L
Linus Torvalds 已提交
168

169
	spin_lock_irq(&rtc_lock);
L
Linus Torvalds 已提交
170

171 172 173
	low = rtc1_read(ECMPLREG);
	mid = rtc1_read(ECMPMREG);
	high = rtc1_read(ECMPHREG);
Y
Yoichi Yuasa 已提交
174
	wkalrm->enabled = alarm_enabled;
L
Linus Torvalds 已提交
175

176
	spin_unlock_irq(&rtc_lock);
L
Linus Torvalds 已提交
177

178
	rtc_time_to_tm((high << 17) | (mid << 1) | (low >> 15), time);
L
Linus Torvalds 已提交
179

180 181
	return 0;
}
L
Linus Torvalds 已提交
182

183 184 185 186
static int vr41xx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *wkalrm)
{
	unsigned long alarm_sec;
	struct rtc_time *time = &wkalrm->time;
L
Linus Torvalds 已提交
187

188
	alarm_sec = mktime(time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
189
			   time->tm_hour, time->tm_min, time->tm_sec);
L
Linus Torvalds 已提交
190

191
	spin_lock_irq(&rtc_lock);
L
Linus Torvalds 已提交
192

Y
Yoichi Yuasa 已提交
193
	if (alarm_enabled)
194
		disable_irq(aie_irq);
Y
Yoichi Yuasa 已提交
195

196 197 198
	rtc1_write(ECMPLREG, (uint16_t)(alarm_sec << 15));
	rtc1_write(ECMPMREG, (uint16_t)(alarm_sec >> 1));
	rtc1_write(ECMPHREG, (uint16_t)(alarm_sec >> 17));
L
Linus Torvalds 已提交
199

Y
Yoichi Yuasa 已提交
200
	if (wkalrm->enabled)
201
		enable_irq(aie_irq);
Y
Yoichi Yuasa 已提交
202 203 204

	alarm_enabled = wkalrm->enabled;

205
	spin_unlock_irq(&rtc_lock);
L
Linus Torvalds 已提交
206 207 208 209

	return 0;
}

210 211
static int vr41xx_rtc_ioctl(struct device *dev, unsigned int cmd, unsigned long arg)
{
L
Linus Torvalds 已提交
212 213 214 215 216 217 218 219 220 221
	switch (cmd) {
	case RTC_EPOCH_READ:
		return put_user(epoch, (unsigned long __user *)arg);
	case RTC_EPOCH_SET:
		/* Doesn't support before 1900 */
		if (arg < 1900)
			return -EINVAL;
		epoch = arg;
		break;
	default:
222
		return -ENOIOCTLCMD;
L
Linus Torvalds 已提交
223 224 225 226 227
	}

	return 0;
}

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
static int vr41xx_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
{
	spin_lock_irq(&rtc_lock);
	if (enabled) {
		if (!alarm_enabled) {
			enable_irq(aie_irq);
			alarm_enabled = 1;
		}
	} else {
		if (alarm_enabled) {
			disable_irq(aie_irq);
			alarm_enabled = 0;
		}
	}
	spin_unlock_irq(&rtc_lock);
	return 0;
}

246
static irqreturn_t elapsedtime_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
247
{
248 249
	struct platform_device *pdev = (struct platform_device *)dev_id;
	struct rtc_device *rtc = platform_get_drvdata(pdev);
L
Linus Torvalds 已提交
250

251
	rtc2_write(RTCINTREG, ELAPSEDTIME_INT);
L
Linus Torvalds 已提交
252

253
	rtc_update_irq(rtc, 1, RTC_AF);
L
Linus Torvalds 已提交
254 255 256 257

	return IRQ_HANDLED;
}

258
static irqreturn_t rtclong1_interrupt(int irq, void *dev_id)
L
Linus Torvalds 已提交
259
{
260 261
	struct platform_device *pdev = (struct platform_device *)dev_id;
	struct rtc_device *rtc = platform_get_drvdata(pdev);
L
Linus Torvalds 已提交
262 263 264 265 266 267 268
	unsigned long count = periodic_count;

	rtc2_write(RTCINTREG, RTCLONG1_INT);

	rtc1_write(RTCL1LREG, count);
	rtc1_write(RTCL1HREG, count >> 16);

269
	rtc_update_irq(rtc, 1, RTC_PF);
L
Linus Torvalds 已提交
270 271 272 273

	return IRQ_HANDLED;
}

274
static const struct rtc_class_ops vr41xx_rtc_ops = {
275 276 277 278 279 280
	.release	= vr41xx_rtc_release,
	.ioctl		= vr41xx_rtc_ioctl,
	.read_time	= vr41xx_rtc_read_time,
	.set_time	= vr41xx_rtc_set_time,
	.read_alarm	= vr41xx_rtc_read_alarm,
	.set_alarm	= vr41xx_rtc_set_alarm,
L
Linus Torvalds 已提交
281 282
};

283
static int rtc_probe(struct platform_device *pdev)
L
Linus Torvalds 已提交
284
{
285
	struct resource *res;
286
	struct rtc_device *rtc;
L
Linus Torvalds 已提交
287 288
	int retval;

289
	if (pdev->num_resources != 4)
L
Linus Torvalds 已提交
290 291
		return -EBUSY;

292 293
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res)
L
Linus Torvalds 已提交
294 295
		return -EBUSY;

296
	rtc1_base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
297
	if (!rtc1_base)
L
Linus Torvalds 已提交
298
		return -EBUSY;
299 300 301 302 303 304 305

	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
	if (!res) {
		retval = -EBUSY;
		goto err_rtc1_iounmap;
	}

306
	rtc2_base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
307 308 309
	if (!rtc2_base) {
		retval = -EBUSY;
		goto err_rtc1_iounmap;
L
Linus Torvalds 已提交
310 311
	}

312 313
	rtc = devm_rtc_device_register(&pdev->dev, rtc_name, &vr41xx_rtc_ops,
					THIS_MODULE);
314
	if (IS_ERR(rtc)) {
315 316
		retval = PTR_ERR(rtc);
		goto err_iounmap_all;
L
Linus Torvalds 已提交
317 318
	}

319 320
	rtc->max_user_freq = MAX_PERIODIC_RATE;

L
Linus Torvalds 已提交
321 322 323 324 325 326 327 328 329 330
	spin_lock_irq(&rtc_lock);

	rtc1_write(ECMPLREG, 0);
	rtc1_write(ECMPMREG, 0);
	rtc1_write(ECMPHREG, 0);
	rtc1_write(RTCL1LREG, 0);
	rtc1_write(RTCL1HREG, 0);

	spin_unlock_irq(&rtc_lock);

331
	aie_irq = platform_get_irq(pdev, 0);
332
	if (aie_irq <= 0) {
333
		retval = -EBUSY;
334
		goto err_iounmap_all;
L
Linus Torvalds 已提交
335 336
	}

337 338
	retval = devm_request_irq(&pdev->dev, aie_irq, elapsedtime_interrupt, 0,
				"elapsed_time", pdev);
339
	if (retval < 0)
340
		goto err_iounmap_all;
341 342

	pie_irq = platform_get_irq(pdev, 1);
343 344
	if (pie_irq <= 0) {
		retval = -EBUSY;
345
		goto err_iounmap_all;
346
	}
347

348 349
	retval = devm_request_irq(&pdev->dev, pie_irq, rtclong1_interrupt, 0,
				"rtclong1", pdev);
350
	if (retval < 0)
351
		goto err_iounmap_all;
L
Linus Torvalds 已提交
352

353 354
	platform_set_drvdata(pdev, rtc);

355 356
	disable_irq(aie_irq);
	disable_irq(pie_irq);
L
Linus Torvalds 已提交
357

358
	dev_info(&pdev->dev, "Real Time Clock of NEC VR4100 series\n");
L
Linus Torvalds 已提交
359 360

	return 0;
361 362 363 364 365 366 367 368

err_iounmap_all:
	rtc2_base = NULL;

err_rtc1_iounmap:
	rtc1_base = NULL;

	return retval;
L
Linus Torvalds 已提交
369 370
}

371 372 373
/* work with hotplug and coldplug */
MODULE_ALIAS("platform:RTC");

374
static struct platform_driver rtc_platform_driver = {
L
Linus Torvalds 已提交
375
	.probe		= rtc_probe,
376 377 378
	.driver		= {
		.name	= rtc_name,
	},
L
Linus Torvalds 已提交
379 380
};

381
module_platform_driver(rtc_platform_driver);