rtc-pl031.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * drivers/rtc/rtc-pl031.c
  3. *
  4. * Real Time Clock interface for ARM AMBA PrimeCell 031 RTC
  5. *
  6. * Author: Deepak Saxena <dsaxena@plexity.net>
  7. *
  8. * Copyright 2006 (c) MontaVista Software, Inc.
  9. *
  10. * Author: Mian Yousaf Kaukab <mian.yousaf.kaukab@stericsson.com>
  11. * Copyright 2010 (c) ST-Ericsson AB
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version
  16. * 2 of the License, or (at your option) any later version.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/rtc.h>
  20. #include <linux/init.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/amba/bus.h>
  23. #include <linux/io.h>
  24. #include <linux/bcd.h>
  25. #include <linux/delay.h>
  26. #include <linux/pm_wakeirq.h>
  27. #include <linux/slab.h>
  28. /*
  29. * Register definitions
  30. */
  31. #define RTC_DR 0x00 /* Data read register */
  32. #define RTC_MR 0x04 /* Match register */
  33. #define RTC_LR 0x08 /* Data load register */
  34. #define RTC_CR 0x0c /* Control register */
  35. #define RTC_IMSC 0x10 /* Interrupt mask and set register */
  36. #define RTC_RIS 0x14 /* Raw interrupt status register */
  37. #define RTC_MIS 0x18 /* Masked interrupt status register */
  38. #define RTC_ICR 0x1c /* Interrupt clear register */
  39. /* ST variants have additional timer functionality */
  40. #define RTC_TDR 0x20 /* Timer data read register */
  41. #define RTC_TLR 0x24 /* Timer data load register */
  42. #define RTC_TCR 0x28 /* Timer control register */
  43. #define RTC_YDR 0x30 /* Year data read register */
  44. #define RTC_YMR 0x34 /* Year match register */
  45. #define RTC_YLR 0x38 /* Year data load register */
  46. #define RTC_CR_EN (1 << 0) /* counter enable bit */
  47. #define RTC_CR_CWEN (1 << 26) /* Clockwatch enable bit */
  48. #define RTC_TCR_EN (1 << 1) /* Periodic timer enable bit */
  49. /* Common bit definitions for Interrupt status and control registers */
  50. #define RTC_BIT_AI (1 << 0) /* Alarm interrupt bit */
  51. #define RTC_BIT_PI (1 << 1) /* Periodic interrupt bit. ST variants only. */
  52. /* Common bit definations for ST v2 for reading/writing time */
  53. #define RTC_SEC_SHIFT 0
  54. #define RTC_SEC_MASK (0x3F << RTC_SEC_SHIFT) /* Second [0-59] */
  55. #define RTC_MIN_SHIFT 6
  56. #define RTC_MIN_MASK (0x3F << RTC_MIN_SHIFT) /* Minute [0-59] */
  57. #define RTC_HOUR_SHIFT 12
  58. #define RTC_HOUR_MASK (0x1F << RTC_HOUR_SHIFT) /* Hour [0-23] */
  59. #define RTC_WDAY_SHIFT 17
  60. #define RTC_WDAY_MASK (0x7 << RTC_WDAY_SHIFT) /* Day of Week [1-7] 1=Sunday */
  61. #define RTC_MDAY_SHIFT 20
  62. #define RTC_MDAY_MASK (0x1F << RTC_MDAY_SHIFT) /* Day of Month [1-31] */
  63. #define RTC_MON_SHIFT 25
  64. #define RTC_MON_MASK (0xF << RTC_MON_SHIFT) /* Month [1-12] 1=January */
  65. #define RTC_TIMER_FREQ 32768
  66. /**
  67. * struct pl031_vendor_data - per-vendor variations
  68. * @ops: the vendor-specific operations used on this silicon version
  69. * @clockwatch: if this is an ST Microelectronics silicon version with a
  70. * clockwatch function
  71. * @st_weekday: if this is an ST Microelectronics silicon version that need
  72. * the weekday fix
  73. * @irqflags: special IRQ flags per variant
  74. */
  75. struct pl031_vendor_data {
  76. struct rtc_class_ops ops;
  77. bool clockwatch;
  78. bool st_weekday;
  79. unsigned long irqflags;
  80. };
  81. struct pl031_local {
  82. struct pl031_vendor_data *vendor;
  83. struct rtc_device *rtc;
  84. void __iomem *base;
  85. };
  86. static int pl031_alarm_irq_enable(struct device *dev,
  87. unsigned int enabled)
  88. {
  89. struct pl031_local *ldata = dev_get_drvdata(dev);
  90. unsigned long imsc;
  91. /* Clear any pending alarm interrupts. */
  92. writel(RTC_BIT_AI, ldata->base + RTC_ICR);
  93. imsc = readl(ldata->base + RTC_IMSC);
  94. if (enabled == 1)
  95. writel(imsc | RTC_BIT_AI, ldata->base + RTC_IMSC);
  96. else
  97. writel(imsc & ~RTC_BIT_AI, ldata->base + RTC_IMSC);
  98. return 0;
  99. }
  100. /*
  101. * Convert Gregorian date to ST v2 RTC format.
  102. */
  103. static int pl031_stv2_tm_to_time(struct device *dev,
  104. struct rtc_time *tm, unsigned long *st_time,
  105. unsigned long *bcd_year)
  106. {
  107. int year = tm->tm_year + 1900;
  108. int wday = tm->tm_wday;
  109. /* wday masking is not working in hardware so wday must be valid */
  110. if (wday < -1 || wday > 6) {
  111. dev_err(dev, "invalid wday value %d\n", tm->tm_wday);
  112. return -EINVAL;
  113. } else if (wday == -1) {
  114. /* wday is not provided, calculate it here */
  115. unsigned long time;
  116. struct rtc_time calc_tm;
  117. rtc_tm_to_time(tm, &time);
  118. rtc_time_to_tm(time, &calc_tm);
  119. wday = calc_tm.tm_wday;
  120. }
  121. *bcd_year = (bin2bcd(year % 100) | bin2bcd(year / 100) << 8);
  122. *st_time = ((tm->tm_mon + 1) << RTC_MON_SHIFT)
  123. | (tm->tm_mday << RTC_MDAY_SHIFT)
  124. | ((wday + 1) << RTC_WDAY_SHIFT)
  125. | (tm->tm_hour << RTC_HOUR_SHIFT)
  126. | (tm->tm_min << RTC_MIN_SHIFT)
  127. | (tm->tm_sec << RTC_SEC_SHIFT);
  128. return 0;
  129. }
  130. /*
  131. * Convert ST v2 RTC format to Gregorian date.
  132. */
  133. static int pl031_stv2_time_to_tm(unsigned long st_time, unsigned long bcd_year,
  134. struct rtc_time *tm)
  135. {
  136. tm->tm_year = bcd2bin(bcd_year) + (bcd2bin(bcd_year >> 8) * 100);
  137. tm->tm_mon = ((st_time & RTC_MON_MASK) >> RTC_MON_SHIFT) - 1;
  138. tm->tm_mday = ((st_time & RTC_MDAY_MASK) >> RTC_MDAY_SHIFT);
  139. tm->tm_wday = ((st_time & RTC_WDAY_MASK) >> RTC_WDAY_SHIFT) - 1;
  140. tm->tm_hour = ((st_time & RTC_HOUR_MASK) >> RTC_HOUR_SHIFT);
  141. tm->tm_min = ((st_time & RTC_MIN_MASK) >> RTC_MIN_SHIFT);
  142. tm->tm_sec = ((st_time & RTC_SEC_MASK) >> RTC_SEC_SHIFT);
  143. tm->tm_yday = rtc_year_days(tm->tm_mday, tm->tm_mon, tm->tm_year);
  144. tm->tm_year -= 1900;
  145. return 0;
  146. }
  147. static int pl031_stv2_read_time(struct device *dev, struct rtc_time *tm)
  148. {
  149. struct pl031_local *ldata = dev_get_drvdata(dev);
  150. pl031_stv2_time_to_tm(readl(ldata->base + RTC_DR),
  151. readl(ldata->base + RTC_YDR), tm);
  152. return 0;
  153. }
  154. static int pl031_stv2_set_time(struct device *dev, struct rtc_time *tm)
  155. {
  156. unsigned long time;
  157. unsigned long bcd_year;
  158. struct pl031_local *ldata = dev_get_drvdata(dev);
  159. int ret;
  160. ret = pl031_stv2_tm_to_time(dev, tm, &time, &bcd_year);
  161. if (ret == 0) {
  162. writel(bcd_year, ldata->base + RTC_YLR);
  163. writel(time, ldata->base + RTC_LR);
  164. }
  165. return ret;
  166. }
  167. static int pl031_stv2_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  168. {
  169. struct pl031_local *ldata = dev_get_drvdata(dev);
  170. int ret;
  171. ret = pl031_stv2_time_to_tm(readl(ldata->base + RTC_MR),
  172. readl(ldata->base + RTC_YMR), &alarm->time);
  173. alarm->pending = readl(ldata->base + RTC_RIS) & RTC_BIT_AI;
  174. alarm->enabled = readl(ldata->base + RTC_IMSC) & RTC_BIT_AI;
  175. return ret;
  176. }
  177. static int pl031_stv2_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  178. {
  179. struct pl031_local *ldata = dev_get_drvdata(dev);
  180. unsigned long time;
  181. unsigned long bcd_year;
  182. int ret;
  183. /* At the moment, we can only deal with non-wildcarded alarm times. */
  184. ret = rtc_valid_tm(&alarm->time);
  185. if (ret == 0) {
  186. ret = pl031_stv2_tm_to_time(dev, &alarm->time,
  187. &time, &bcd_year);
  188. if (ret == 0) {
  189. writel(bcd_year, ldata->base + RTC_YMR);
  190. writel(time, ldata->base + RTC_MR);
  191. pl031_alarm_irq_enable(dev, alarm->enabled);
  192. }
  193. }
  194. return ret;
  195. }
  196. static irqreturn_t pl031_interrupt(int irq, void *dev_id)
  197. {
  198. struct pl031_local *ldata = dev_id;
  199. unsigned long rtcmis;
  200. unsigned long events = 0;
  201. rtcmis = readl(ldata->base + RTC_MIS);
  202. if (rtcmis & RTC_BIT_AI) {
  203. writel(RTC_BIT_AI, ldata->base + RTC_ICR);
  204. events |= (RTC_AF | RTC_IRQF);
  205. rtc_update_irq(ldata->rtc, 1, events);
  206. return IRQ_HANDLED;
  207. }
  208. return IRQ_NONE;
  209. }
  210. static int pl031_read_time(struct device *dev, struct rtc_time *tm)
  211. {
  212. struct pl031_local *ldata = dev_get_drvdata(dev);
  213. rtc_time_to_tm(readl(ldata->base + RTC_DR), tm);
  214. return 0;
  215. }
  216. static int pl031_set_time(struct device *dev, struct rtc_time *tm)
  217. {
  218. unsigned long time;
  219. struct pl031_local *ldata = dev_get_drvdata(dev);
  220. int ret;
  221. ret = rtc_tm_to_time(tm, &time);
  222. if (ret == 0)
  223. writel(time, ldata->base + RTC_LR);
  224. return ret;
  225. }
  226. static int pl031_read_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  227. {
  228. struct pl031_local *ldata = dev_get_drvdata(dev);
  229. rtc_time_to_tm(readl(ldata->base + RTC_MR), &alarm->time);
  230. alarm->pending = readl(ldata->base + RTC_RIS) & RTC_BIT_AI;
  231. alarm->enabled = readl(ldata->base + RTC_IMSC) & RTC_BIT_AI;
  232. return 0;
  233. }
  234. static int pl031_set_alarm(struct device *dev, struct rtc_wkalrm *alarm)
  235. {
  236. struct pl031_local *ldata = dev_get_drvdata(dev);
  237. unsigned long time;
  238. int ret;
  239. /* At the moment, we can only deal with non-wildcarded alarm times. */
  240. ret = rtc_valid_tm(&alarm->time);
  241. if (ret == 0) {
  242. ret = rtc_tm_to_time(&alarm->time, &time);
  243. if (ret == 0) {
  244. writel(time, ldata->base + RTC_MR);
  245. pl031_alarm_irq_enable(dev, alarm->enabled);
  246. }
  247. }
  248. return ret;
  249. }
  250. static int pl031_remove(struct amba_device *adev)
  251. {
  252. struct pl031_local *ldata = dev_get_drvdata(&adev->dev);
  253. dev_pm_clear_wake_irq(&adev->dev);
  254. device_init_wakeup(&adev->dev, false);
  255. if (adev->irq[0])
  256. free_irq(adev->irq[0], ldata);
  257. rtc_device_unregister(ldata->rtc);
  258. amba_release_regions(adev);
  259. return 0;
  260. }
  261. static int pl031_probe(struct amba_device *adev, const struct amba_id *id)
  262. {
  263. int ret;
  264. struct pl031_local *ldata;
  265. struct pl031_vendor_data *vendor = id->data;
  266. struct rtc_class_ops *ops;
  267. unsigned long time, data;
  268. ret = amba_request_regions(adev, NULL);
  269. if (ret)
  270. goto err_req;
  271. ldata = devm_kzalloc(&adev->dev, sizeof(struct pl031_local),
  272. GFP_KERNEL);
  273. ops = devm_kmemdup(&adev->dev, &vendor->ops, sizeof(vendor->ops),
  274. GFP_KERNEL);
  275. if (!ldata || !ops) {
  276. ret = -ENOMEM;
  277. goto out;
  278. }
  279. ldata->vendor = vendor;
  280. ldata->base = devm_ioremap(&adev->dev, adev->res.start,
  281. resource_size(&adev->res));
  282. if (!ldata->base) {
  283. ret = -ENOMEM;
  284. goto out;
  285. }
  286. amba_set_drvdata(adev, ldata);
  287. dev_dbg(&adev->dev, "designer ID = 0x%02x\n", amba_manf(adev));
  288. dev_dbg(&adev->dev, "revision = 0x%01x\n", amba_rev(adev));
  289. data = readl(ldata->base + RTC_CR);
  290. /* Enable the clockwatch on ST Variants */
  291. if (vendor->clockwatch)
  292. data |= RTC_CR_CWEN;
  293. else
  294. data |= RTC_CR_EN;
  295. writel(data, ldata->base + RTC_CR);
  296. /*
  297. * On ST PL031 variants, the RTC reset value does not provide correct
  298. * weekday for 2000-01-01. Correct the erroneous sunday to saturday.
  299. */
  300. if (vendor->st_weekday) {
  301. if (readl(ldata->base + RTC_YDR) == 0x2000) {
  302. time = readl(ldata->base + RTC_DR);
  303. if ((time &
  304. (RTC_MON_MASK | RTC_MDAY_MASK | RTC_WDAY_MASK))
  305. == 0x02120000) {
  306. time = time | (0x7 << RTC_WDAY_SHIFT);
  307. writel(0x2000, ldata->base + RTC_YLR);
  308. writel(time, ldata->base + RTC_LR);
  309. }
  310. }
  311. }
  312. if (!adev->irq[0]) {
  313. /* When there's no interrupt, no point in exposing the alarm */
  314. ops->read_alarm = NULL;
  315. ops->set_alarm = NULL;
  316. ops->alarm_irq_enable = NULL;
  317. }
  318. device_init_wakeup(&adev->dev, true);
  319. ldata->rtc = rtc_device_register("pl031", &adev->dev, ops,
  320. THIS_MODULE);
  321. if (IS_ERR(ldata->rtc)) {
  322. ret = PTR_ERR(ldata->rtc);
  323. goto out;
  324. }
  325. if (adev->irq[0]) {
  326. ret = request_irq(adev->irq[0], pl031_interrupt,
  327. vendor->irqflags, "rtc-pl031", ldata);
  328. if (ret)
  329. goto out_no_irq;
  330. dev_pm_set_wake_irq(&adev->dev, adev->irq[0]);
  331. }
  332. return 0;
  333. out_no_irq:
  334. rtc_device_unregister(ldata->rtc);
  335. out:
  336. amba_release_regions(adev);
  337. err_req:
  338. return ret;
  339. }
  340. /* Operations for the original ARM version */
  341. static struct pl031_vendor_data arm_pl031 = {
  342. .ops = {
  343. .read_time = pl031_read_time,
  344. .set_time = pl031_set_time,
  345. .read_alarm = pl031_read_alarm,
  346. .set_alarm = pl031_set_alarm,
  347. .alarm_irq_enable = pl031_alarm_irq_enable,
  348. },
  349. };
  350. /* The First ST derivative */
  351. static struct pl031_vendor_data stv1_pl031 = {
  352. .ops = {
  353. .read_time = pl031_read_time,
  354. .set_time = pl031_set_time,
  355. .read_alarm = pl031_read_alarm,
  356. .set_alarm = pl031_set_alarm,
  357. .alarm_irq_enable = pl031_alarm_irq_enable,
  358. },
  359. .clockwatch = true,
  360. .st_weekday = true,
  361. };
  362. /* And the second ST derivative */
  363. static struct pl031_vendor_data stv2_pl031 = {
  364. .ops = {
  365. .read_time = pl031_stv2_read_time,
  366. .set_time = pl031_stv2_set_time,
  367. .read_alarm = pl031_stv2_read_alarm,
  368. .set_alarm = pl031_stv2_set_alarm,
  369. .alarm_irq_enable = pl031_alarm_irq_enable,
  370. },
  371. .clockwatch = true,
  372. .st_weekday = true,
  373. /*
  374. * This variant shares the IRQ with another block and must not
  375. * suspend that IRQ line.
  376. * TODO check if it shares with IRQF_NO_SUSPEND user, else we can
  377. * remove IRQF_COND_SUSPEND
  378. */
  379. .irqflags = IRQF_SHARED | IRQF_COND_SUSPEND,
  380. };
  381. static const struct amba_id pl031_ids[] = {
  382. {
  383. .id = 0x00041031,
  384. .mask = 0x000fffff,
  385. .data = &arm_pl031,
  386. },
  387. /* ST Micro variants */
  388. {
  389. .id = 0x00180031,
  390. .mask = 0x00ffffff,
  391. .data = &stv1_pl031,
  392. },
  393. {
  394. .id = 0x00280031,
  395. .mask = 0x00ffffff,
  396. .data = &stv2_pl031,
  397. },
  398. {0, 0},
  399. };
  400. MODULE_DEVICE_TABLE(amba, pl031_ids);
  401. static struct amba_driver pl031_driver = {
  402. .drv = {
  403. .name = "rtc-pl031",
  404. },
  405. .id_table = pl031_ids,
  406. .probe = pl031_probe,
  407. .remove = pl031_remove,
  408. };
  409. module_amba_driver(pl031_driver);
  410. MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net>");
  411. MODULE_DESCRIPTION("ARM AMBA PL031 RTC Driver");
  412. MODULE_LICENSE("GPL");