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