rtc-pl031.c 12 KB

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