Richard Zhao | 4c19dee | 2011-09-02 10:09:29 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2011 Freescale Semiconductor, Inc. All Rights Reserved. |
| 3 | */ |
| 4 | |
| 5 | /* |
| 6 | * The code contained herein is licensed under the GNU General Public |
| 7 | * License. You may obtain a copy of the GNU General Public License |
| 8 | * Version 2 or later at the following locations: |
| 9 | * |
| 10 | * http://www.opensource.org/licenses/gpl-license.html |
| 11 | * http://www.gnu.org/copyleft/gpl.html |
| 12 | */ |
| 13 | /* |
| 14 | * Implementation based on rtc-ds1553.c |
| 15 | */ |
| 16 | |
| 17 | /*! |
| 18 | * @defgroup RTC Real Time Clock (RTC) Driver |
| 19 | */ |
| 20 | /*! |
| 21 | * @file rtc-mxc_v2.c |
| 22 | * @brief Real Time Clock interface |
| 23 | * |
| 24 | * This file contains Real Time Clock interface for Linux. |
| 25 | * |
| 26 | * @ingroup RTC |
| 27 | */ |
| 28 | |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/delay.h> |
| 31 | #include <linux/rtc.h> |
| 32 | #include <linux/module.h> |
| 33 | #include <linux/fs.h> |
| 34 | #include <linux/init.h> |
| 35 | #include <linux/interrupt.h> |
| 36 | #include <linux/platform_device.h> |
| 37 | #include <linux/clk.h> |
| 38 | #include <linux/uaccess.h> |
| 39 | #include <linux/io.h> |
| 40 | #include <linux/sched.h> |
| 41 | #include <linux/mxc_srtc.h> |
| 42 | |
| 43 | #define SRTC_LPSCLR_LLPSC_LSH 17 /* start bit for LSB time value */ |
| 44 | |
| 45 | #define SRTC_LPPDR_INIT 0x41736166 /* init for glitch detect */ |
| 46 | |
| 47 | #define SRTC_LPCR_SWR_LP (1 << 0) /* lp software reset */ |
| 48 | #define SRTC_LPCR_EN_LP (1 << 3) /* lp enable */ |
| 49 | #define SRTC_LPCR_WAE (1 << 4) /* lp wakeup alarm enable */ |
| 50 | #define SRTC_LPCR_SAE (1 << 5) /* lp security alarm enable */ |
| 51 | #define SRTC_LPCR_SI (1 << 6) /* lp security interrupt enable */ |
| 52 | #define SRTC_LPCR_ALP (1 << 7) /* lp alarm flag */ |
| 53 | #define SRTC_LPCR_LTC (1 << 8) /* lp lock time counter */ |
| 54 | #define SRTC_LPCR_LMC (1 << 9) /* lp lock monotonic counter */ |
| 55 | #define SRTC_LPCR_SV (1 << 10) /* lp security violation */ |
| 56 | #define SRTC_LPCR_NSA (1 << 11) /* lp non secure access */ |
| 57 | #define SRTC_LPCR_NVEIE (1 << 12) /* lp non valid state exit int en */ |
| 58 | #define SRTC_LPCR_IEIE (1 << 13) /* lp init state exit int enable */ |
| 59 | #define SRTC_LPCR_NVE (1 << 14) /* lp non valid state exit bit */ |
| 60 | #define SRTC_LPCR_IE (1 << 15) /* lp init state exit bit */ |
| 61 | |
| 62 | #define SRTC_LPCR_ALL_INT_EN (SRTC_LPCR_WAE | SRTC_LPCR_SAE | \ |
| 63 | SRTC_LPCR_SI | SRTC_LPCR_ALP | \ |
| 64 | SRTC_LPCR_NVEIE | SRTC_LPCR_IEIE) |
| 65 | |
| 66 | #define SRTC_LPSR_TRI (1 << 0) /* lp time read invalidate */ |
| 67 | #define SRTC_LPSR_PGD (1 << 1) /* lp power supply glitc detected */ |
| 68 | #define SRTC_LPSR_CTD (1 << 2) /* lp clock tampering detected */ |
| 69 | #define SRTC_LPSR_ALP (1 << 3) /* lp alarm flag */ |
| 70 | #define SRTC_LPSR_MR (1 << 4) /* lp monotonic counter rollover */ |
| 71 | #define SRTC_LPSR_TR (1 << 5) /* lp time rollover */ |
| 72 | #define SRTC_LPSR_EAD (1 << 6) /* lp external alarm detected */ |
| 73 | #define SRTC_LPSR_IT0 (1 << 7) /* lp IIM throttle */ |
| 74 | #define SRTC_LPSR_IT1 (1 << 8) |
| 75 | #define SRTC_LPSR_IT2 (1 << 9) |
| 76 | #define SRTC_LPSR_SM0 (1 << 10) /* lp security mode */ |
| 77 | #define SRTC_LPSR_SM1 (1 << 11) |
| 78 | #define SRTC_LPSR_STATE_LP0 (1 << 12) /* lp state */ |
| 79 | #define SRTC_LPSR_STATE_LP1 (1 << 13) |
| 80 | #define SRTC_LPSR_NVES (1 << 14) /* lp non-valid state exit status */ |
| 81 | #define SRTC_LPSR_IES (1 << 15) /* lp init state exit status */ |
| 82 | |
| 83 | #define MAX_PIE_NUM 15 |
| 84 | #define MAX_PIE_FREQ 32768 |
| 85 | #define MIN_PIE_FREQ 1 |
| 86 | |
| 87 | #define SRTC_PI0 (1 << 0) |
| 88 | #define SRTC_PI1 (1 << 1) |
| 89 | #define SRTC_PI2 (1 << 2) |
| 90 | #define SRTC_PI3 (1 << 3) |
| 91 | #define SRTC_PI4 (1 << 4) |
| 92 | #define SRTC_PI5 (1 << 5) |
| 93 | #define SRTC_PI6 (1 << 6) |
| 94 | #define SRTC_PI7 (1 << 7) |
| 95 | #define SRTC_PI8 (1 << 8) |
| 96 | #define SRTC_PI9 (1 << 9) |
| 97 | #define SRTC_PI10 (1 << 10) |
| 98 | #define SRTC_PI11 (1 << 11) |
| 99 | #define SRTC_PI12 (1 << 12) |
| 100 | #define SRTC_PI13 (1 << 13) |
| 101 | #define SRTC_PI14 (1 << 14) |
| 102 | #define SRTC_PI15 (1 << 15) |
| 103 | |
| 104 | #define PIT_ALL_ON (SRTC_PI1 | SRTC_PI2 | SRTC_PI3 | \ |
| 105 | SRTC_PI4 | SRTC_PI5 | SRTC_PI6 | SRTC_PI7 | \ |
| 106 | SRTC_PI8 | SRTC_PI9 | SRTC_PI10 | SRTC_PI11 | \ |
| 107 | SRTC_PI12 | SRTC_PI13 | SRTC_PI14 | SRTC_PI15) |
| 108 | |
| 109 | #define SRTC_SWR_HP (1 << 0) /* hp software reset */ |
| 110 | #define SRTC_EN_HP (1 << 3) /* hp enable */ |
| 111 | #define SRTC_TS (1 << 4) /* time syncronize hp with lp */ |
| 112 | |
| 113 | #define SRTC_IE_AHP (1 << 16) /* Alarm HP Interrupt Enable bit */ |
| 114 | #define SRTC_IE_WDHP (1 << 18) /* Write Done HP Interrupt Enable bit */ |
| 115 | #define SRTC_IE_WDLP (1 << 19) /* Write Done LP Interrupt Enable bit */ |
| 116 | |
| 117 | #define SRTC_ISR_AHP (1 << 16) /* interrupt status: alarm hp */ |
| 118 | #define SRTC_ISR_WDHP (1 << 18) /* interrupt status: write done hp */ |
| 119 | #define SRTC_ISR_WDLP (1 << 19) /* interrupt status: write done lp */ |
| 120 | #define SRTC_ISR_WPHP (1 << 20) /* interrupt status: write pending hp */ |
| 121 | #define SRTC_ISR_WPLP (1 << 21) /* interrupt status: write pending lp */ |
| 122 | |
| 123 | #define SRTC_LPSCMR 0x00 /* LP Secure Counter MSB Reg */ |
| 124 | #define SRTC_LPSCLR 0x04 /* LP Secure Counter LSB Reg */ |
| 125 | #define SRTC_LPSAR 0x08 /* LP Secure Alarm Reg */ |
| 126 | #define SRTC_LPSMCR 0x0C /* LP Secure Monotonic Counter Reg */ |
| 127 | #define SRTC_LPCR 0x10 /* LP Control Reg */ |
| 128 | #define SRTC_LPSR 0x14 /* LP Status Reg */ |
| 129 | #define SRTC_LPPDR 0x18 /* LP Power Supply Glitch Detector Reg */ |
| 130 | #define SRTC_LPGR 0x1C /* LP General Purpose Reg */ |
| 131 | #define SRTC_HPCMR 0x20 /* HP Counter MSB Reg */ |
| 132 | #define SRTC_HPCLR 0x24 /* HP Counter LSB Reg */ |
| 133 | #define SRTC_HPAMR 0x28 /* HP Alarm MSB Reg */ |
| 134 | #define SRTC_HPALR 0x2C /* HP Alarm LSB Reg */ |
| 135 | #define SRTC_HPCR 0x30 /* HP Control Reg */ |
| 136 | #define SRTC_HPISR 0x34 /* HP Interrupt Status Reg */ |
| 137 | #define SRTC_HPIENR 0x38 /* HP Interrupt Enable Reg */ |
| 138 | |
| 139 | #define SRTC_SECMODE_MASK 0x3 /* the mask of SRTC security mode */ |
| 140 | #define SRTC_SECMODE_LOW 0x0 /* Low Security */ |
| 141 | #define SRTC_SECMODE_MED 0x1 /* Medium Security */ |
| 142 | #define SRTC_SECMODE_HIGH 0x2 /* High Security */ |
| 143 | #define SRTC_SECMODE_RESERVED 0x3 /* Reserved */ |
| 144 | |
| 145 | struct rtc_drv_data { |
| 146 | struct rtc_device *rtc; |
| 147 | void __iomem *ioaddr; |
| 148 | unsigned long baseaddr; |
| 149 | int irq; |
| 150 | struct clk *clk; |
| 151 | bool irq_enable; |
| 152 | }; |
| 153 | |
| 154 | |
| 155 | /* completion event for implementing RTC_WAIT_FOR_TIME_SET ioctl */ |
| 156 | DECLARE_COMPLETION(srtc_completion); |
| 157 | /* global to save difference of 47-bit counter value */ |
| 158 | static int64_t time_diff; |
| 159 | |
| 160 | /*! |
| 161 | * @defgroup RTC Real Time Clock (RTC) Driver |
| 162 | */ |
| 163 | /*! |
| 164 | * @file rtc-mxc.c |
| 165 | * @brief Real Time Clock interface |
| 166 | * |
| 167 | * This file contains Real Time Clock interface for Linux. |
| 168 | * |
| 169 | * @ingroup RTC |
| 170 | */ |
| 171 | |
| 172 | static unsigned long rtc_status; |
| 173 | |
| 174 | static DEFINE_SPINLOCK(rtc_lock); |
| 175 | |
| 176 | /*! |
| 177 | * This function does write synchronization for writes to the lp srtc block. |
| 178 | * To take care of the asynchronous CKIL clock, all writes from the IP domain |
| 179 | * will be synchronized to the CKIL domain. |
| 180 | */ |
| 181 | static inline void rtc_write_sync_lp(void __iomem *ioaddr) |
| 182 | { |
| 183 | unsigned int i, count; |
| 184 | /* Wait for 3 CKIL cycles */ |
| 185 | for (i = 0; i < 3; i++) { |
| 186 | count = __raw_readl(ioaddr + SRTC_LPSCLR); |
| 187 | while |
| 188 | ((__raw_readl(ioaddr + SRTC_LPSCLR)) == count); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /*! |
| 193 | * This function updates the RTC alarm registers and then clears all the |
| 194 | * interrupt status bits. |
| 195 | * |
| 196 | * @param alrm the new alarm value to be updated in the RTC |
| 197 | * |
| 198 | * @return 0 if successful; non-zero otherwise. |
| 199 | */ |
| 200 | static int rtc_update_alarm(struct device *dev, struct rtc_time *alrm) |
| 201 | { |
| 202 | struct rtc_drv_data *pdata = dev_get_drvdata(dev); |
| 203 | void __iomem *ioaddr = pdata->ioaddr; |
| 204 | struct rtc_time alarm_tm, now_tm; |
| 205 | unsigned long now, time; |
| 206 | int ret; |
| 207 | |
| 208 | now = __raw_readl(ioaddr + SRTC_LPSCMR); |
| 209 | rtc_time_to_tm(now, &now_tm); |
| 210 | |
| 211 | alarm_tm.tm_year = now_tm.tm_year; |
| 212 | alarm_tm.tm_mon = now_tm.tm_mon; |
| 213 | alarm_tm.tm_mday = now_tm.tm_mday; |
| 214 | |
| 215 | alarm_tm.tm_hour = alrm->tm_hour; |
| 216 | alarm_tm.tm_min = alrm->tm_min; |
| 217 | alarm_tm.tm_sec = alrm->tm_sec; |
| 218 | |
| 219 | rtc_tm_to_time(&now_tm, &now); |
| 220 | rtc_tm_to_time(&alarm_tm, &time); |
| 221 | |
| 222 | if (time < now) { |
| 223 | time += 60 * 60 * 24; |
| 224 | rtc_time_to_tm(time, &alarm_tm); |
| 225 | } |
| 226 | ret = rtc_tm_to_time(&alarm_tm, &time); |
| 227 | |
| 228 | __raw_writel(time, ioaddr + SRTC_LPSAR); |
| 229 | |
| 230 | /* clear alarm interrupt status bit */ |
| 231 | __raw_writel(SRTC_LPSR_ALP, ioaddr + SRTC_LPSR); |
| 232 | |
| 233 | return ret; |
| 234 | } |
| 235 | |
| 236 | /*! |
| 237 | * This function is the RTC interrupt service routine. |
| 238 | * |
| 239 | * @param irq RTC IRQ number |
| 240 | * @param dev_id device ID which is not used |
| 241 | * |
| 242 | * @return IRQ_HANDLED as defined in the include/linux/interrupt.h file. |
| 243 | */ |
| 244 | static irqreturn_t mxc_rtc_interrupt(int irq, void *dev_id) |
| 245 | { |
| 246 | struct platform_device *pdev = dev_id; |
| 247 | struct rtc_drv_data *pdata = platform_get_drvdata(pdev); |
| 248 | void __iomem *ioaddr = pdata->ioaddr; |
| 249 | u32 lp_status, lp_cr; |
| 250 | u32 events = 0; |
| 251 | |
| 252 | lp_status = __raw_readl(ioaddr + SRTC_LPSR); |
| 253 | lp_cr = __raw_readl(ioaddr + SRTC_LPCR); |
| 254 | |
| 255 | /* update irq data & counter */ |
| 256 | if (lp_status & SRTC_LPSR_ALP) { |
| 257 | if (lp_cr & SRTC_LPCR_ALP) |
| 258 | events |= (RTC_AF | RTC_IRQF); |
| 259 | |
| 260 | /* disable further lp alarm interrupts */ |
| 261 | lp_cr &= ~(SRTC_LPCR_ALP | SRTC_LPCR_WAE); |
| 262 | } |
| 263 | |
| 264 | /* Update interrupt enables */ |
| 265 | __raw_writel(lp_cr, ioaddr + SRTC_LPCR); |
| 266 | |
| 267 | /* If no interrupts are enabled, turn off interrupts in kernel */ |
| 268 | if (((lp_cr & SRTC_LPCR_ALL_INT_EN) == 0) && (pdata->irq_enable)) { |
| 269 | disable_irq_nosync(pdata->irq); |
| 270 | pdata->irq_enable = false; |
| 271 | } |
| 272 | |
| 273 | /* clear interrupt status */ |
| 274 | __raw_writel(lp_status, ioaddr + SRTC_LPSR); |
| 275 | |
| 276 | rtc_write_sync_lp(ioaddr); |
| 277 | rtc_update_irq(pdata->rtc, 1, events); |
| 278 | return IRQ_HANDLED; |
| 279 | } |
| 280 | |
| 281 | /*! |
| 282 | * This function is used to open the RTC driver. |
| 283 | * |
| 284 | * @return 0 if successful; non-zero otherwise. |
| 285 | */ |
| 286 | static int mxc_rtc_open(struct device *dev) |
| 287 | { |
| 288 | if (test_and_set_bit(1, &rtc_status)) |
| 289 | return -EBUSY; |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | /*! |
| 294 | * clear all interrupts and release the IRQ |
| 295 | */ |
| 296 | static void mxc_rtc_release(struct device *dev) |
| 297 | { |
| 298 | rtc_status = 0; |
| 299 | } |
| 300 | |
| 301 | /*! |
| 302 | * This function is used to support some ioctl calls directly. |
| 303 | * Other ioctl calls are supported indirectly through the |
| 304 | * arm/common/rtctime.c file. |
| 305 | * |
| 306 | * @param cmd ioctl command as defined in include/linux/rtc.h |
| 307 | * @param arg value for the ioctl command |
| 308 | * |
| 309 | * @return 0 if successful or negative value otherwise. |
| 310 | */ |
| 311 | static int mxc_rtc_ioctl(struct device *dev, unsigned int cmd, |
| 312 | unsigned long arg) |
| 313 | { |
| 314 | struct rtc_drv_data *pdata = dev_get_drvdata(dev); |
| 315 | void __iomem *ioaddr = pdata->ioaddr; |
| 316 | u64 time_47bit; |
| 317 | int retVal; |
| 318 | |
| 319 | switch (cmd) { |
| 320 | case RTC_READ_TIME_47BIT: |
| 321 | time_47bit = (((u64) __raw_readl(ioaddr + SRTC_LPSCMR)) << 32 | |
| 322 | ((u64) __raw_readl(ioaddr + SRTC_LPSCLR))); |
| 323 | time_47bit >>= SRTC_LPSCLR_LLPSC_LSH; |
| 324 | |
| 325 | if (arg && copy_to_user((u64 *) arg, &time_47bit, sizeof(u64))) |
| 326 | return -EFAULT; |
| 327 | |
| 328 | return 0; |
| 329 | |
| 330 | /* This IOCTL to be used by processes to be notified of time changes */ |
| 331 | case RTC_WAIT_TIME_SET: |
| 332 | |
| 333 | /* don't block without releasing mutex first */ |
| 334 | mutex_unlock(&pdata->rtc->ops_lock); |
| 335 | |
| 336 | /* sleep until awakened by SRTC driver when LPSCMR is changed */ |
| 337 | wait_for_completion(&srtc_completion); |
| 338 | |
| 339 | /* relock mutex because rtc_dev_ioctl will unlock again */ |
| 340 | retVal = mutex_lock_interruptible(&pdata->rtc->ops_lock); |
| 341 | |
| 342 | /* copy the new time difference = new time - previous time |
| 343 | * to the user param. The difference is a signed value */ |
| 344 | if (arg && copy_to_user((int64_t *) arg, &time_diff, |
| 345 | sizeof(int64_t))) |
| 346 | return -EFAULT; |
| 347 | |
| 348 | return retVal; |
| 349 | |
| 350 | } |
| 351 | |
| 352 | return -ENOIOCTLCMD; |
| 353 | } |
| 354 | |
| 355 | /*! |
| 356 | * This function reads the current RTC time into tm in Gregorian date. |
| 357 | * |
| 358 | * @param tm contains the RTC time value upon return |
| 359 | * |
| 360 | * @return 0 if successful; non-zero otherwise. |
| 361 | */ |
| 362 | static int mxc_rtc_read_time(struct device *dev, struct rtc_time *tm) |
| 363 | { |
| 364 | struct rtc_drv_data *pdata = dev_get_drvdata(dev); |
| 365 | void __iomem *ioaddr = pdata->ioaddr; |
| 366 | |
| 367 | rtc_time_to_tm(__raw_readl(ioaddr + SRTC_LPSCMR), tm); |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | /*! |
| 372 | * This function sets the internal RTC time based on tm in Gregorian date. |
| 373 | * |
| 374 | * @param tm the time value to be set in the RTC |
| 375 | * |
| 376 | * @return 0 if successful; non-zero otherwise. |
| 377 | */ |
| 378 | static int mxc_rtc_set_time(struct device *dev, struct rtc_time *tm) |
| 379 | { |
| 380 | struct rtc_drv_data *pdata = dev_get_drvdata(dev); |
| 381 | void __iomem *ioaddr = pdata->ioaddr; |
| 382 | unsigned long time; |
| 383 | u64 old_time_47bit, new_time_47bit; |
| 384 | int ret; |
| 385 | ret = rtc_tm_to_time(tm, &time); |
| 386 | if (ret != 0) |
| 387 | return ret; |
| 388 | |
| 389 | old_time_47bit = (((u64) __raw_readl(ioaddr + SRTC_LPSCMR)) << 32 | |
| 390 | ((u64) __raw_readl(ioaddr + SRTC_LPSCLR))); |
| 391 | old_time_47bit >>= SRTC_LPSCLR_LLPSC_LSH; |
| 392 | |
| 393 | __raw_writel(time, ioaddr + SRTC_LPSCMR); |
| 394 | rtc_write_sync_lp(ioaddr); |
| 395 | |
| 396 | new_time_47bit = (((u64) __raw_readl(ioaddr + SRTC_LPSCMR)) << 32 | |
| 397 | ((u64) __raw_readl(ioaddr + SRTC_LPSCLR))); |
| 398 | new_time_47bit >>= SRTC_LPSCLR_LLPSC_LSH; |
| 399 | |
| 400 | /* update the difference between previous time and new time */ |
| 401 | time_diff = new_time_47bit - old_time_47bit; |
| 402 | |
| 403 | /* signal all waiting threads that time changed */ |
| 404 | complete_all(&srtc_completion); |
| 405 | |
| 406 | /* allow signalled threads to handle the time change notification */ |
| 407 | schedule(); |
| 408 | |
| 409 | /* reinitialize completion variable */ |
| 410 | INIT_COMPLETION(srtc_completion); |
| 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | /*! |
| 416 | * This function reads the current alarm value into the passed in \b alrm |
| 417 | * argument. It updates the \b alrm's pending field value based on the whether |
| 418 | * an alarm interrupt occurs or not. |
| 419 | * |
| 420 | * @param alrm contains the RTC alarm value upon return |
| 421 | * |
| 422 | * @return 0 if successful; non-zero otherwise. |
| 423 | */ |
| 424 | static int mxc_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 425 | { |
| 426 | struct rtc_drv_data *pdata = dev_get_drvdata(dev); |
| 427 | void __iomem *ioaddr = pdata->ioaddr; |
| 428 | |
| 429 | rtc_time_to_tm(__raw_readl(ioaddr + SRTC_LPSAR), &alrm->time); |
| 430 | alrm->pending = |
| 431 | ((__raw_readl(ioaddr + SRTC_LPSR) & SRTC_LPSR_ALP) != 0) ? 1 : 0; |
| 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | /*! |
| 437 | * This function sets the RTC alarm based on passed in alrm. |
| 438 | * |
| 439 | * @param alrm the alarm value to be set in the RTC |
| 440 | * |
| 441 | * @return 0 if successful; non-zero otherwise. |
| 442 | */ |
| 443 | static int mxc_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) |
| 444 | { |
| 445 | struct rtc_drv_data *pdata = dev_get_drvdata(dev); |
| 446 | void __iomem *ioaddr = pdata->ioaddr; |
| 447 | unsigned long lock_flags = 0; |
| 448 | u32 lp_cr; |
| 449 | int ret; |
| 450 | |
| 451 | if (rtc_valid_tm(&alrm->time)) { |
| 452 | if (alrm->time.tm_sec > 59 || |
| 453 | alrm->time.tm_hour > 23 || alrm->time.tm_min > 59) { |
| 454 | return -EINVAL; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | spin_lock_irqsave(&rtc_lock, lock_flags); |
| 459 | lp_cr = __raw_readl(ioaddr + SRTC_LPCR); |
| 460 | |
| 461 | ret = rtc_update_alarm(dev, &alrm->time); |
| 462 | if (ret) |
| 463 | goto out; |
| 464 | |
| 465 | if (alrm->enabled) |
| 466 | lp_cr |= (SRTC_LPCR_ALP | SRTC_LPCR_WAE); |
| 467 | else |
| 468 | lp_cr &= ~(SRTC_LPCR_ALP | SRTC_LPCR_WAE); |
| 469 | |
| 470 | if (lp_cr & SRTC_LPCR_ALL_INT_EN) { |
| 471 | if (!pdata->irq_enable) { |
| 472 | enable_irq(pdata->irq); |
| 473 | pdata->irq_enable = true; |
| 474 | } |
| 475 | } else { |
| 476 | if (pdata->irq_enable) { |
| 477 | disable_irq(pdata->irq); |
| 478 | pdata->irq_enable = false; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | __raw_writel(lp_cr, ioaddr + SRTC_LPCR); |
| 483 | |
| 484 | out: |
| 485 | rtc_write_sync_lp(ioaddr); |
| 486 | spin_unlock_irqrestore(&rtc_lock, lock_flags); |
| 487 | return ret; |
| 488 | } |
| 489 | |
| 490 | /*! |
| 491 | * This function is used to provide the content for the /proc/driver/rtc |
| 492 | * file. |
| 493 | * |
| 494 | * @param seq buffer to hold the information that the driver wants to write |
| 495 | * |
| 496 | * @return The number of bytes written into the rtc file. |
| 497 | */ |
| 498 | static int mxc_rtc_proc(struct device *dev, struct seq_file *seq) |
| 499 | { |
| 500 | struct rtc_drv_data *pdata = dev_get_drvdata(dev); |
| 501 | void __iomem *ioaddr = pdata->ioaddr; |
| 502 | |
| 503 | seq_printf(seq, "alarm_IRQ\t: %s\n", |
| 504 | (((__raw_readl(ioaddr + SRTC_LPCR)) & SRTC_LPCR_ALP) != |
| 505 | 0) ? "yes" : "no"); |
| 506 | |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | static int mxc_rtc_alarm_irq_enable(struct device *dev, unsigned int enable) |
| 511 | { |
| 512 | struct rtc_drv_data *pdata = dev_get_drvdata(dev); |
| 513 | void __iomem *ioaddr = pdata->ioaddr; |
| 514 | u32 lp_cr; |
| 515 | unsigned long lock_flags = 0; |
| 516 | |
| 517 | spin_lock_irqsave(&rtc_lock, lock_flags); |
| 518 | |
| 519 | if (enable) { |
| 520 | if (!pdata->irq_enable) { |
| 521 | enable_irq(pdata->irq); |
| 522 | pdata->irq_enable = true; |
| 523 | } |
| 524 | lp_cr = __raw_readl(ioaddr + SRTC_LPCR); |
| 525 | lp_cr |= SRTC_LPCR_ALP | SRTC_LPCR_WAE; |
| 526 | __raw_writel(lp_cr, ioaddr + SRTC_LPCR); |
| 527 | } else { |
| 528 | lp_cr = __raw_readl(ioaddr + SRTC_LPCR); |
| 529 | lp_cr &= ~(SRTC_LPCR_ALP | SRTC_LPCR_WAE); |
| 530 | if (((lp_cr & SRTC_LPCR_ALL_INT_EN) == 0) |
| 531 | && (pdata->irq_enable)) { |
| 532 | disable_irq(pdata->irq); |
| 533 | pdata->irq_enable = false; |
| 534 | } |
| 535 | __raw_writel(lp_cr, ioaddr + SRTC_LPCR); |
| 536 | } |
| 537 | |
| 538 | rtc_write_sync_lp(ioaddr); |
| 539 | spin_unlock_irqrestore(&rtc_lock, lock_flags); |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | /*! |
| 544 | * The RTC driver structure |
| 545 | */ |
| 546 | static struct rtc_class_ops mxc_rtc_ops = { |
| 547 | .open = mxc_rtc_open, |
| 548 | .release = mxc_rtc_release, |
| 549 | .ioctl = mxc_rtc_ioctl, |
| 550 | .read_time = mxc_rtc_read_time, |
| 551 | .set_time = mxc_rtc_set_time, |
| 552 | .read_alarm = mxc_rtc_read_alarm, |
| 553 | .set_alarm = mxc_rtc_set_alarm, |
| 554 | .proc = mxc_rtc_proc, |
| 555 | .alarm_irq_enable = mxc_rtc_alarm_irq_enable, |
| 556 | }; |
| 557 | |
| 558 | /*! MXC RTC Power management control */ |
| 559 | static int mxc_rtc_probe(struct platform_device *pdev) |
| 560 | { |
| 561 | struct clk *clk; |
| 562 | struct timespec tv; |
| 563 | struct resource *res; |
| 564 | struct rtc_device *rtc; |
| 565 | struct rtc_drv_data *pdata = NULL; |
| 566 | void __iomem *ioaddr; |
| 567 | int ret = 0; |
| 568 | |
| 569 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 570 | if (!res) |
| 571 | return -ENODEV; |
| 572 | |
| 573 | pdata = kzalloc(sizeof(*pdata), GFP_KERNEL); |
| 574 | if (!pdata) |
| 575 | return -ENOMEM; |
| 576 | |
| 577 | pdata->clk = clk_get(&pdev->dev, "rtc_clk"); |
| 578 | clk_enable(pdata->clk); |
| 579 | pdata->baseaddr = res->start; |
| 580 | pdata->ioaddr = ioremap(pdata->baseaddr, 0x40); |
| 581 | ioaddr = pdata->ioaddr; |
| 582 | |
| 583 | /* Configure and enable the RTC */ |
| 584 | pdata->irq = platform_get_irq(pdev, 0); |
| 585 | if (pdata->irq >= 0) { |
| 586 | if (request_irq(pdata->irq, mxc_rtc_interrupt, IRQF_SHARED, |
| 587 | pdev->name, pdev) < 0) { |
| 588 | dev_warn(&pdev->dev, "interrupt not available.\n"); |
| 589 | pdata->irq = -1; |
| 590 | } else { |
| 591 | disable_irq(pdata->irq); |
| 592 | pdata->irq_enable = false; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | clk = clk_get(&pdev->dev, "rtc_clk"); |
| 597 | if (clk_get_rate(clk) != 32768) { |
| 598 | printk(KERN_ALERT "rtc clock is not valid"); |
| 599 | ret = -EINVAL; |
| 600 | clk_put(clk); |
| 601 | goto err_out; |
| 602 | } |
| 603 | clk_put(clk); |
| 604 | |
| 605 | /* initialize glitch detect */ |
| 606 | __raw_writel(SRTC_LPPDR_INIT, ioaddr + SRTC_LPPDR); |
| 607 | udelay(100); |
| 608 | |
| 609 | /* clear lp interrupt status */ |
| 610 | __raw_writel(0xFFFFFFFF, ioaddr + SRTC_LPSR); |
| 611 | udelay(100); |
| 612 | |
| 613 | /* move out of init state */ |
| 614 | __raw_writel((SRTC_LPCR_IE | SRTC_LPCR_NSA), |
| 615 | ioaddr + SRTC_LPCR); |
| 616 | |
| 617 | udelay(100); |
| 618 | |
| 619 | while ((__raw_readl(ioaddr + SRTC_LPSR) & SRTC_LPSR_IES) == 0) |
| 620 | ; |
| 621 | |
| 622 | /* move out of non-valid state */ |
| 623 | __raw_writel((SRTC_LPCR_IE | SRTC_LPCR_NVE | SRTC_LPCR_NSA | |
| 624 | SRTC_LPCR_EN_LP), ioaddr + SRTC_LPCR); |
| 625 | |
| 626 | udelay(100); |
| 627 | |
| 628 | while ((__raw_readl(ioaddr + SRTC_LPSR) & SRTC_LPSR_NVES) == 0) |
| 629 | ; |
| 630 | |
| 631 | __raw_writel(0xFFFFFFFF, ioaddr + SRTC_LPSR); |
| 632 | udelay(100); |
| 633 | |
| 634 | platform_set_drvdata(pdev, pdata); |
| 635 | |
| 636 | rtc = rtc_device_register(pdev->name, &pdev->dev, |
| 637 | &mxc_rtc_ops, THIS_MODULE); |
| 638 | if (IS_ERR(rtc)) { |
| 639 | platform_set_drvdata(pdev, NULL); |
| 640 | ret = PTR_ERR(rtc); |
| 641 | goto err_out; |
| 642 | } |
| 643 | |
| 644 | pdata->rtc = rtc; |
| 645 | |
| 646 | tv.tv_nsec = 0; |
| 647 | tv.tv_sec = __raw_readl(ioaddr + SRTC_LPSCMR); |
| 648 | |
| 649 | /* By default, devices should wakeup if they can */ |
| 650 | /* So srtc is set as "should wakeup" as it can */ |
| 651 | device_init_wakeup(&pdev->dev, 1); |
| 652 | |
| 653 | return ret; |
| 654 | |
| 655 | err_out: |
| 656 | clk_disable(pdata->clk); |
| 657 | iounmap(ioaddr); |
| 658 | if (pdata->irq >= 0) |
| 659 | free_irq(pdata->irq, pdev); |
| 660 | kfree(pdata); |
| 661 | return ret; |
| 662 | } |
| 663 | |
| 664 | static int __exit mxc_rtc_remove(struct platform_device *pdev) |
| 665 | { |
| 666 | struct rtc_drv_data *pdata = platform_get_drvdata(pdev); |
| 667 | rtc_device_unregister(pdata->rtc); |
| 668 | if (pdata->irq >= 0) |
| 669 | free_irq(pdata->irq, pdev); |
| 670 | |
| 671 | clk_disable(pdata->clk); |
| 672 | clk_put(pdata->clk); |
| 673 | kfree(pdata); |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | /*! |
| 678 | * This function is called to save the system time delta relative to |
| 679 | * the MXC RTC when enterring a low power state. This time delta is |
| 680 | * then used on resume to adjust the system time to account for time |
| 681 | * loss while suspended. |
| 682 | * |
| 683 | * @param pdev not used |
| 684 | * @param state Power state to enter. |
| 685 | * |
| 686 | * @return The function always returns 0. |
| 687 | */ |
| 688 | static int mxc_rtc_suspend(struct platform_device *pdev, pm_message_t state) |
| 689 | { |
| 690 | struct rtc_drv_data *pdata = platform_get_drvdata(pdev); |
| 691 | |
| 692 | if (device_may_wakeup(&pdev->dev)) { |
| 693 | enable_irq_wake(pdata->irq); |
| 694 | } else { |
| 695 | if (pdata->irq_enable) |
| 696 | disable_irq(pdata->irq); |
| 697 | } |
| 698 | |
| 699 | return 0; |
| 700 | } |
| 701 | |
| 702 | /*! |
| 703 | * This function is called to correct the system time based on the |
| 704 | * current MXC RTC time relative to the time delta saved during |
| 705 | * suspend. |
| 706 | * |
| 707 | * @param pdev not used |
| 708 | * |
| 709 | * @return The function always returns 0. |
| 710 | */ |
| 711 | static int mxc_rtc_resume(struct platform_device *pdev) |
| 712 | { |
| 713 | struct rtc_drv_data *pdata = platform_get_drvdata(pdev); |
| 714 | |
| 715 | if (device_may_wakeup(&pdev->dev)) { |
| 716 | disable_irq_wake(pdata->irq); |
| 717 | } else { |
| 718 | if (pdata->irq_enable) |
| 719 | enable_irq(pdata->irq); |
| 720 | } |
| 721 | |
| 722 | return 0; |
| 723 | } |
| 724 | |
| 725 | /*! |
| 726 | * Contains pointers to the power management callback functions. |
| 727 | */ |
| 728 | static struct platform_driver mxc_rtc_driver = { |
| 729 | .driver = { |
| 730 | .name = "mxc_rtc", |
| 731 | }, |
| 732 | .probe = mxc_rtc_probe, |
| 733 | .remove = __exit_p(mxc_rtc_remove), |
| 734 | .suspend = mxc_rtc_suspend, |
| 735 | .resume = mxc_rtc_resume, |
| 736 | }; |
| 737 | |
| 738 | /*! |
| 739 | * This function creates the /proc/driver/rtc file and registers the device RTC |
| 740 | * in the /dev/misc directory. It also reads the RTC value from external source |
| 741 | * and setup the internal RTC properly. |
| 742 | * |
| 743 | * @return -1 if RTC is failed to initialize; 0 is successful. |
| 744 | */ |
| 745 | static int __init mxc_rtc_init(void) |
| 746 | { |
| 747 | return platform_driver_register(&mxc_rtc_driver); |
| 748 | } |
| 749 | |
| 750 | /*! |
| 751 | * This function removes the /proc/driver/rtc file and un-registers the |
| 752 | * device RTC from the /dev/misc directory. |
| 753 | */ |
| 754 | static void __exit mxc_rtc_exit(void) |
| 755 | { |
| 756 | platform_driver_unregister(&mxc_rtc_driver); |
| 757 | |
| 758 | } |
| 759 | |
| 760 | module_init(mxc_rtc_init); |
| 761 | module_exit(mxc_rtc_exit); |
| 762 | |
| 763 | MODULE_AUTHOR("Freescale Semiconductor, Inc."); |
| 764 | MODULE_DESCRIPTION("Realtime Clock Driver (RTC)"); |
| 765 | MODULE_LICENSE("GPL"); |