Subject: RTC on SH7709
To: hpcsh development list <port-hpcsh@netbsd.org>
From: Adam Wysocki via ArcaBit <gophi@arcabit.pl>
List: port-hpcsh
Date: 12/23/2006 04:35:19
Hello,

Section 13 from SH7709 datasheet says that this CPU contains on-chip RTC. 
What's the reason to lack of it's support in hpcsh port? I've done some 
attempts to handle this RTC in userspace, but with no luck - am I doing 
something wrong, or maybe this RTC is broken in Jornada?

[gophi@hpcsh ~/src]$ sudo ./rtc
virtual=0x20434EDC physical=0xFFFFFEDC
virtual=0x20436EDE physical=0xFFFFFEDE
virtual=0x20437EC2 physical=0xFFFFFEC2
0# 0x17
1# 0x74
2# 0x08
3# 0x74
4# 0x08
5# 0xE0
6# 0xE0
7# 0x74
8# 0x0B
9# 0xE0

And the code (you need securelevel set to 0):

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>

typedef volatile unsigned char * volatile ptr_t;

ptr_t map_byte(int fd, off_t off)
{
	ptr_t p = mmap(NULL, sizeof(char), PROT_READ | PROT_WRITE, MAP_SHARED, fd, off);

	if (p == MAP_FAILED) {
		perror("mmap");
		exit(EXIT_FAILURE);
	}

	printf("virtual=0x%08X physical=0x%08X\n", p, off);

	return p;
}

void unmap_byte(ptr_t p)
{
	munmap((void *) p, sizeof(char));
}

int main(void)
{
	ptr_t rcr1, rcr2, rseccnt;
	int fd, i;

	fd = open("/dev/mem", O_RDWR);
	if (fd == -1) {
		perror("open");
		exit(EXIT_FAILURE);
	}

	rcr1 = map_byte(fd, 0xFFFFFEDC);
	rcr2 = map_byte(fd, 0xFFFFFEDE);
	rseccnt = map_byte(fd, 0xFFFFFEC2);

	*rcr2 |= (1 << 3) | (1 << 1);	// rtcen, reset
	*rseccnt = 0;
	*rcr2 = (1 << 0);		// start

	for (i = 0; i < 10; i++) {
		unsigned char ch;

		*rcr1 &= ~(1 << 4);	// cie
		*rcr1 &= ~(1 << 7);	// cf

		ch = *rseccnt;

		printf("%d# 0x%02X\n", i, ch);
		sleep(1);
	}

	unmap_byte(rseccnt);
	unmap_byte(rcr2);
	unmap_byte(rcr1);
	close(fd);

	return 0;
}

-- 
[ Adam Wysocki, +48 22 532 69 07, +48 514 710 213, www.chmurka.net ]
[ Software Research and Development Department, ArcaBit Sp. z o.o. ]
[ Ul. Fortuny 9, 01-339 Warszawa, Poland * http://www.arcabit.com/ ]