Blame view

drivers/rtc/rtc-au1xxx.c 3.12 KB
45fd8a0c1   Manuel Lauss   rtc: Au1000 On-Ch...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  /*
   * Au1xxx counter0 (aka Time-Of-Year counter) RTC interface driver.
   *
   * Copyright (C) 2008 Manuel Lauss <mano@roarinelk.homelinux.net>
   *
   * This file is subject to the terms and conditions of the GNU General Public
   * License.  See the file "COPYING" in the main directory of this archive
   * for more details.
   */
  
  /* All current Au1xxx SoCs have 2 counters fed by an external 32.768 kHz
   * crystal. Counter 0, which keeps counting during sleep/powerdown, is
   * used to count seconds since the beginning of the unix epoch.
   *
   * The counters must be configured and enabled by bootloader/board code;
   * no checks as to whether they really get a proper 32.768kHz clock are
   * made as this would take far too long.
   */
  
  #include <linux/module.h>
  #include <linux/kernel.h>
  #include <linux/rtc.h>
  #include <linux/init.h>
  #include <linux/platform_device.h>
  #include <linux/io.h>
  #include <asm/mach-au1x00/au1000.h>
  
  /* 32kHz clock enabled and detected */
  #define CNTR_OK (SYS_CNTRL_E0 | SYS_CNTRL_32S)
  
  static int au1xtoy_rtc_read_time(struct device *dev, struct rtc_time *tm)
  {
  	unsigned long t;
1d09de7dc   Manuel Lauss   MIPS: Alchemy: in...
34
  	t = alchemy_rdsys(AU1000_SYS_TOYREAD);
45fd8a0c1   Manuel Lauss   rtc: Au1000 On-Ch...
35
36
37
38
39
40
41
42
43
44
45
  
  	rtc_time_to_tm(t, tm);
  
  	return rtc_valid_tm(tm);
  }
  
  static int au1xtoy_rtc_set_time(struct device *dev, struct rtc_time *tm)
  {
  	unsigned long t;
  
  	rtc_tm_to_time(tm, &t);
1d09de7dc   Manuel Lauss   MIPS: Alchemy: in...
46
  	alchemy_wrsys(t, AU1000_SYS_TOYWRITE);
45fd8a0c1   Manuel Lauss   rtc: Au1000 On-Ch...
47
48
49
50
  
  	/* wait for the pending register write to succeed.  This can
  	 * take up to 6 seconds...
  	 */
1d09de7dc   Manuel Lauss   MIPS: Alchemy: in...
51
  	while (alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_C0S)
45fd8a0c1   Manuel Lauss   rtc: Au1000 On-Ch...
52
53
54
55
56
57
58
59
60
  		msleep(1);
  
  	return 0;
  }
  
  static struct rtc_class_ops au1xtoy_rtc_ops = {
  	.read_time	= au1xtoy_rtc_read_time,
  	.set_time	= au1xtoy_rtc_set_time,
  };
5a167f454   Greg Kroah-Hartman   Drivers: rtc: rem...
61
  static int au1xtoy_rtc_probe(struct platform_device *pdev)
45fd8a0c1   Manuel Lauss   rtc: Au1000 On-Ch...
62
63
64
65
  {
  	struct rtc_device *rtcdev;
  	unsigned long t;
  	int ret;
1d09de7dc   Manuel Lauss   MIPS: Alchemy: in...
66
  	t = alchemy_rdsys(AU1000_SYS_CNTRCTRL);
45fd8a0c1   Manuel Lauss   rtc: Au1000 On-Ch...
67
68
69
70
71
72
73
74
75
76
  	if (!(t & CNTR_OK)) {
  		dev_err(&pdev->dev, "counters not working; aborting.
  ");
  		ret = -ENODEV;
  		goto out_err;
  	}
  
  	ret = -ETIMEDOUT;
  
  	/* set counter0 tickrate to 1Hz if necessary */
1d09de7dc   Manuel Lauss   MIPS: Alchemy: in...
77
  	if (alchemy_rdsys(AU1000_SYS_TOYTRIM) != 32767) {
45fd8a0c1   Manuel Lauss   rtc: Au1000 On-Ch...
78
79
  		/* wait until hardware gives access to TRIM register */
  		t = 0x00100000;
1d09de7dc   Manuel Lauss   MIPS: Alchemy: in...
80
  		while ((alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_T0S) && --t)
45fd8a0c1   Manuel Lauss   rtc: Au1000 On-Ch...
81
82
83
84
85
86
87
88
89
90
91
92
  			msleep(1);
  
  		if (!t) {
  			/* timed out waiting for register access; assume
  			 * counters are unusable.
  			 */
  			dev_err(&pdev->dev, "timeout waiting for access
  ");
  			goto out_err;
  		}
  
  		/* set 1Hz TOY tick rate */
1d09de7dc   Manuel Lauss   MIPS: Alchemy: in...
93
  		alchemy_wrsys(32767, AU1000_SYS_TOYTRIM);
45fd8a0c1   Manuel Lauss   rtc: Au1000 On-Ch...
94
95
96
  	}
  
  	/* wait until the hardware allows writes to the counter reg */
1d09de7dc   Manuel Lauss   MIPS: Alchemy: in...
97
  	while (alchemy_rdsys(AU1000_SYS_CNTRCTRL) & SYS_CNTRL_C0S)
45fd8a0c1   Manuel Lauss   rtc: Au1000 On-Ch...
98
  		msleep(1);
6c0686057   Jingoo Han   rtc: rtc-au1xxx: ...
99
  	rtcdev = devm_rtc_device_register(&pdev->dev, "rtc-au1xxx",
45fd8a0c1   Manuel Lauss   rtc: Au1000 On-Ch...
100
101
102
103
104
105
106
107
108
109
110
111
112
  				     &au1xtoy_rtc_ops, THIS_MODULE);
  	if (IS_ERR(rtcdev)) {
  		ret = PTR_ERR(rtcdev);
  		goto out_err;
  	}
  
  	platform_set_drvdata(pdev, rtcdev);
  
  	return 0;
  
  out_err:
  	return ret;
  }
45fd8a0c1   Manuel Lauss   rtc: Au1000 On-Ch...
113
114
115
  static struct platform_driver au1xrtc_driver = {
  	.driver		= {
  		.name	= "rtc-au1xxx",
45fd8a0c1   Manuel Lauss   rtc: Au1000 On-Ch...
116
  	},
45fd8a0c1   Manuel Lauss   rtc: Au1000 On-Ch...
117
  };
aaa834588   Jingoo Han   rtc: rtc-au1xxx: ...
118
  module_platform_driver_probe(au1xrtc_driver, au1xtoy_rtc_probe);
45fd8a0c1   Manuel Lauss   rtc: Au1000 On-Ch...
119
120
121
122
123
  
  MODULE_DESCRIPTION("Au1xxx TOY-counter-based RTC driver");
  MODULE_AUTHOR("Manuel Lauss <manuel.lauss@gmail.com>");
  MODULE_LICENSE("GPL");
  MODULE_ALIAS("platform:rtc-au1xxx");