rtc-pl031.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. iounmap(ldata->base);
  259. kfree(ldata);
  260. amba_release_regions(adev);
  261. return 0;
  262. }
  263. static int pl031_probe(struct amba_device *adev, const struct amba_id *id)
  264. {
  265. int ret;
  266. struct pl031_local *ldata;
  267. struct pl031_vendor_data *vendor = id->data;
  268. struct rtc_class_ops *ops = &vendor->ops;
  269. unsigned long time, data;
  270. ret = amba_request_regions(adev, NULL);
  271. if (ret)
  272. goto err_req;
  273. ldata = kzalloc(sizeof(struct pl031_local), GFP_KERNEL);
  274. if (!ldata) {
  275. ret = -ENOMEM;
  276. goto out;
  277. }
  278. ldata->vendor = vendor;
  279. ldata->base = ioremap(adev->res.start, resource_size(&adev->res));
  280. if (!ldata->base) {
  281. ret = -ENOMEM;
  282. goto out_no_remap;
  283. }
  284. amba_set_drvdata(adev, ldata);
  285. dev_dbg(&adev->dev, "designer ID = 0x%02x\n", amba_manf(adev));
  286. dev_dbg(&adev->dev, "revision = 0x%01x\n", amba_rev(adev));
  287. data = readl(ldata->base + RTC_CR);
  288. /* Enable the clockwatch on ST Variants */
  289. if (vendor->clockwatch)
  290. data |= RTC_CR_CWEN;
  291. else
  292. data |= RTC_CR_EN;
  293. writel(data, ldata->base + RTC_CR);
  294. /*
  295. * On ST PL031 variants, the RTC reset value does not provide correct
  296. * weekday for 2000-01-01. Correct the erroneous sunday to saturday.
  297. */
  298. if (vendor->st_weekday) {
  299. if (readl(ldata->base + RTC_YDR) == 0x2000) {
  300. time = readl(ldata->base + RTC_DR);
  301. if ((time &
  302. (RTC_MON_MASK | RTC_MDAY_MASK | RTC_WDAY_MASK))
  303. == 0x02120000) {
  304. time = time | (0x7 << RTC_WDAY_SHIFT);
  305. writel(0x2000, ldata->base + RTC_YLR);
  306. writel(time, ldata->base + RTC_LR);
  307. }
  308. }
  309. }
  310. device_init_wakeup(&adev->dev, true);
  311. ldata->rtc = rtc_device_register("pl031", &adev->dev, ops,
  312. THIS_MODULE);
  313. if (IS_ERR(ldata->rtc)) {
  314. ret = PTR_ERR(ldata->rtc);
  315. goto out_no_rtc;
  316. }
  317. if (adev->irq[0]) {
  318. ret = request_irq(adev->irq[0], pl031_interrupt,
  319. vendor->irqflags, "rtc-pl031", ldata);
  320. if (ret)
  321. goto out_no_irq;
  322. dev_pm_set_wake_irq(&adev->dev, adev->irq[0]);
  323. }
  324. return 0;
  325. out_no_irq:
  326. rtc_device_unregister(ldata->rtc);
  327. out_no_rtc:
  328. iounmap(ldata->base);
  329. out_no_remap:
  330. kfree(ldata);
  331. out:
  332. amba_release_regions(adev);
  333. err_req:
  334. return ret;
  335. }
  336. /* Operations for the original ARM version */
  337. static struct pl031_vendor_data arm_pl031 = {
  338. .ops = {
  339. .read_time = pl031_read_time,
  340. .set_time = pl031_set_time,
  341. .read_alarm = pl031_read_alarm,
  342. .set_alarm = pl031_set_alarm,
  343. .alarm_irq_enable = pl031_alarm_irq_enable,
  344. },
  345. };
  346. /* The First ST derivative */
  347. static struct pl031_vendor_data stv1_pl031 = {
  348. .ops = {
  349. .read_time = pl031_read_time,
  350. .set_time = pl031_set_time,
  351. .read_alarm = pl031_read_alarm,
  352. .set_alarm = pl031_set_alarm,
  353. .alarm_irq_enable = pl031_alarm_irq_enable,
  354. },
  355. .clockwatch = true,
  356. .st_weekday = true,
  357. };
  358. /* And the second ST derivative */
  359. static struct pl031_vendor_data stv2_pl031 = {
  360. .ops = {
  361. .read_time = pl031_stv2_read_time,
  362. .set_time = pl031_stv2_set_time,
  363. .read_alarm = pl031_stv2_read_alarm,
  364. .set_alarm = pl031_stv2_set_alarm,
  365. .alarm_irq_enable = pl031_alarm_irq_enable,
  366. },
  367. .clockwatch = true,
  368. .st_weekday = true,
  369. /*
  370. * This variant shares the IRQ with another block and must not
  371. * suspend that IRQ line.
  372. * TODO check if it shares with IRQF_NO_SUSPEND user, else we can
  373. * remove IRQF_COND_SUSPEND
  374. */
  375. .irqflags = IRQF_SHARED | IRQF_COND_SUSPEND,
  376. };
  377. static struct amba_id pl031_ids[] = {
  378. {
  379. .id = 0x00041031,
  380. .mask = 0x000fffff,
  381. .data = &arm_pl031,
  382. },
  383. /* ST Micro variants */
  384. {
  385. .id = 0x00180031,
  386. .mask = 0x00ffffff,
  387. .data = &stv1_pl031,
  388. },
  389. {
  390. .id = 0x00280031,
  391. .mask = 0x00ffffff,
  392. .data = &stv2_pl031,
  393. },
  394. {0, 0},
  395. };
  396. MODULE_DEVICE_TABLE(amba, pl031_ids);
  397. static struct amba_driver pl031_driver = {
  398. .drv = {
  399. .name = "rtc-pl031",
  400. },
  401. .id_table = pl031_ids,
  402. .probe = pl031_probe,
  403. .remove = pl031_remove,
  404. };
  405. module_amba_driver(pl031_driver);
  406. MODULE_AUTHOR("Deepak Saxena <dsaxena@plexity.net>");
  407. MODULE_DESCRIPTION("ARM AMBA PL031 RTC Driver");
  408. MODULE_LICENSE("GPL");