026-Add-RTC-driver-support-for-MCF5445x.patch 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. From 40563ab5aa698191dbd8a05fe6053aa790eee2a1 Mon Sep 17 00:00:00 2001
  2. From: Alison Wang <[email protected]>
  3. Date: Thu, 4 Aug 2011 09:59:46 +0800
  4. Subject: [PATCH 26/52] Add RTC driver support for MCF5445x
  5. On-chip RTC module support for MCF54451 and MCF54455.
  6. Using internal 32K clock to drive the rtc module.
  7. Signed-off-by: Alison Wang <[email protected]>
  8. ---
  9. drivers/rtc/Kconfig | 9 +
  10. drivers/rtc/Makefile | 1 +
  11. drivers/rtc/rtc-mcf.c | 583 +++++++++++++++++++++++++++++++++++++++++++++++++
  12. 3 files changed, 593 insertions(+), 0 deletions(-)
  13. create mode 100644 drivers/rtc/rtc-mcf.c
  14. --- a/drivers/rtc/Kconfig
  15. +++ b/drivers/rtc/Kconfig
  16. @@ -919,6 +919,15 @@ config RTC_DRV_MV
  17. This driver can also be built as a module. If so, the module
  18. will be called rtc-mv.
  19. +config RTC_MCF
  20. + tristate "Freescale Coldfire Real Time Clock"
  21. + depends on COLDFIRE
  22. + help
  23. + If you say yes here you will get support for the on-chip Coldfire
  24. + Real-Time Clock.
  25. +
  26. + If you build it as a module it will be call mcf-rtc.
  27. +
  28. config RTC_DRV_PS3
  29. tristate "PS3 RTC"
  30. depends on PPC_PS3
  31. --- a/drivers/rtc/Makefile
  32. +++ b/drivers/rtc/Makefile
  33. @@ -102,3 +102,4 @@ obj-$(CONFIG_RTC_DRV_VR41XX) += rtc-vr41
  34. obj-$(CONFIG_RTC_DRV_WM831X) += rtc-wm831x.o
  35. obj-$(CONFIG_RTC_DRV_WM8350) += rtc-wm8350.o
  36. obj-$(CONFIG_RTC_DRV_X1205) += rtc-x1205.o
  37. +obj-$(CONFIG_RTC_MCF) += rtc-mcf.o
  38. --- /dev/null
  39. +++ b/drivers/rtc/rtc-mcf.c
  40. @@ -0,0 +1,583 @@
  41. +/*
  42. + * Copyright (C) 2004-2011 Freescale Semiconductor, Inc. All Rights Reserved.
  43. + *
  44. + * Implementation based on rtc-mxc.c
  45. + * This file contains Real Time Clock interface for Linux.
  46. + *
  47. + * This is free software; you can redistribute it and/or modify
  48. + * it under the terms of the GNU General Public License as published by
  49. + * the Free Software Foundation; either version 2 of the License, or
  50. + * (at your option) any later version.
  51. + */
  52. +
  53. +#include <linux/rtc.h>
  54. +#include <linux/module.h>
  55. +#include <linux/fs.h>
  56. +#include <linux/init.h>
  57. +#include <linux/interrupt.h>
  58. +#include <linux/platform_device.h>
  59. +#include <linux/clk.h>
  60. +#include <linux/uaccess.h>
  61. +#include <asm/mcfsim.h>
  62. +#include <linux/slab.h>
  63. +#include <linux/io.h>
  64. +
  65. +#ifdef readl
  66. +#undef readl
  67. +#endif
  68. +
  69. +#ifdef writel
  70. +#undef writel
  71. +#endif
  72. +
  73. +#define readl(addr) in_be32(addr)
  74. +#define writel(val, addr) out_be32((addr), (val))
  75. +
  76. +#define RTC_INPUT_CLK_32768HZ 0x8000
  77. +#define RTC_INPUT_CLK_32000HZ 0x7D00
  78. +#define RTC_INPUT_CLK_38400HZ 0x9600
  79. +#define RTC_INPUT_CLK_48000HZ 0xBB80
  80. +
  81. +#define PIT_ALL_ON (MCF_RTC_ISR_2HZ | MCF_RTC_ISR_SAM0 | MCF_RTC_ISR_SAM1 | \
  82. + MCF_RTC_ISR_SAM2 | MCF_RTC_ISR_SAM3 | MCF_RTC_ISR_SAM4 | \
  83. + MCF_RTC_ISR_SAM5 | MCF_RTC_ISR_SAM6 | MCF_RTC_ISR_SAM7)
  84. +
  85. +#define MAX_PIE_NUM 9
  86. +#define MAX_PIE_FREQ 512
  87. +const u32 PIE_BIT_DEF[MAX_PIE_NUM][2] = {
  88. + {2, MCF_RTC_ISR_2HZ},
  89. + {4, MCF_RTC_ISR_SAM0},
  90. + {8, MCF_RTC_ISR_SAM1},
  91. + {16, MCF_RTC_ISR_SAM2},
  92. + {32, MCF_RTC_ISR_SAM3},
  93. + {64, MCF_RTC_ISR_SAM4},
  94. + {128, MCF_RTC_ISR_SAM5},
  95. + {256, MCF_RTC_ISR_SAM6},
  96. + {MAX_PIE_FREQ, MCF_RTC_ISR_SAM7},
  97. +};
  98. +
  99. +/* Those are the bits from a classic RTC we want to mimic */
  100. +#define RTC_IRQF 0x80 /* any of the following 3 is active */
  101. +#define RTC_PF 0x40 /* Periodic interrupt */
  102. +#define RTC_AF 0x20 /* Alarm interrupt */
  103. +#define RTC_UF 0x10 /* Update interrupt for 1Hz RTC */
  104. +
  105. +#define MCF_RTC_TIME 0
  106. +#define MCF_RTC_ALARM 1
  107. +
  108. +struct rtc_plat_data {
  109. + struct rtc_device *rtc;
  110. + int irq;
  111. + unsigned int irqen;
  112. + int alrm_sec;
  113. + int alrm_min;
  114. + int alrm_hour;
  115. + int alrm_mday;
  116. +};
  117. +
  118. +/*!
  119. + * @defgroup RTC Real Time Clock (RTC) Driver
  120. + */
  121. +/*!
  122. + * @file rtc-mcf.c
  123. + * @brief Real Time Clock interface
  124. + *
  125. + * This file contains Real Time Clock interface for Linux.
  126. + *
  127. + * @ingroup RTC
  128. + */
  129. +
  130. +#define RTC_VERSION "0.1"
  131. +
  132. +static u32 rtc_freq = 2; /* minimun value for PIE */
  133. +static unsigned long rtc_status;
  134. +
  135. +static struct rtc_time g_rtc_alarm = {
  136. + .tm_year = 0,
  137. + .tm_mon = 0,
  138. + .tm_mday = 0,
  139. + .tm_hour = 0,
  140. + .tm_mon = 0,
  141. + .tm_sec = 0,
  142. +};
  143. +
  144. +static DEFINE_SPINLOCK(rtc_lock);
  145. +
  146. +/*!
  147. + * This function is used to obtain the RTC time or the alarm value in
  148. + * second.
  149. + *
  150. + * @param time_alarm use MCF_RTC_TIME for RTC time value;
  151. + * MCF_RTC_ALARM for alarm value
  152. + *
  153. + * @return The RTC time or alarm time in second.
  154. + */
  155. +static u32 get_alarm_or_time(struct device *dev, int time_alarm)
  156. +{
  157. + u32 day, hr, min, sec, hr_min;
  158. +
  159. + if (time_alarm == MCF_RTC_TIME) {
  160. + day = MCF_RTC_DAYS_DAYS(readl(MCF_RTC_DAYS));
  161. + hr_min = readl(MCF_RTC_HOURMIN);
  162. + sec = MCF_RTC_SECONDS_SECONDS(readl(MCF_RTC_SECONDS));
  163. + } else if (time_alarm == MCF_RTC_ALARM) {
  164. + day = MCF_RTC_ALRM_DAY_DAYS(readl(MCF_RTC_ALRM_DAY));
  165. + hr_min = readl(MCF_RTC_ALRM_HM);
  166. + sec = MCF_RTC_ALRM_SEC_SECONDS(readl(MCF_RTC_ALRM_SEC));
  167. + } else {
  168. + panic("wrong value for time_alarm=%d\n", time_alarm);
  169. + }
  170. +
  171. + hr = (hr_min >> 8) & 0x001F;
  172. + min = hr_min & 0x003F;
  173. +
  174. + return (((day * 24 + hr) * 60) + min) * 60 + sec;
  175. +}
  176. +
  177. +/*!
  178. + * This function sets the RTC alarm value or the time value.
  179. + *
  180. + * @param time_alarm the new alarm value to be updated in the RTC
  181. + * @param time use MCF_RTC_TIME for RTC time value;
  182. + * MCF_RTC_ALARM for alarm value
  183. + */
  184. +static void set_alarm_or_time(struct device *dev, int time_alarm, u32 time)
  185. +{
  186. + u32 day, hr, min, sec, temp;
  187. +
  188. + day = time / 86400;
  189. + time -= day * 86400;
  190. + /* time is within a day now */
  191. + hr = time / 3600;
  192. + time -= hr * 3600;
  193. + /* time is within an hour now */
  194. + min = time / 60;
  195. + sec = time - min * 60;
  196. +
  197. + temp = (hr << 8) + min;
  198. +
  199. + if (time_alarm == MCF_RTC_TIME) {
  200. + writel(day, MCF_RTC_DAYS);
  201. + writel(sec, MCF_RTC_SECONDS);
  202. + writel(temp, MCF_RTC_HOURMIN);
  203. + } else if (time_alarm == MCF_RTC_ALARM) {
  204. + writel(day, MCF_RTC_ALRM_DAY);
  205. + writel(sec, MCF_RTC_ALRM_SEC);
  206. + writel(temp, MCF_RTC_ALRM_HM);
  207. + } else {
  208. + panic("wrong value for time_alarm=%d\n", time_alarm);
  209. + }
  210. +}
  211. +
  212. +/*!
  213. + * This function updates the RTC alarm registers and then clears all the
  214. + * interrupt status bits.
  215. + *
  216. + * @param alrm the new alarm value to be updated in the RTC
  217. + *
  218. + * @return 0 if successful; non-zero otherwise.
  219. + */
  220. +static int rtc_update_alarm(struct device *dev, struct rtc_time *alrm)
  221. +{
  222. + struct rtc_time alarm_tm, now_tm;
  223. + unsigned long now, time;
  224. + int ret;
  225. +
  226. + now = get_alarm_or_time(dev, MCF_RTC_TIME);
  227. + rtc_time_to_tm(now, &now_tm);
  228. + alarm_tm.tm_year = now_tm.tm_year;
  229. + alarm_tm.tm_mon = now_tm.tm_mon;
  230. + alarm_tm.tm_mday = now_tm.tm_mday;
  231. + alarm_tm.tm_hour = alrm->tm_hour;
  232. + alarm_tm.tm_min = alrm->tm_min;
  233. + alarm_tm.tm_sec = alrm->tm_sec;
  234. + rtc_tm_to_time(&now_tm, &now);
  235. + rtc_tm_to_time(&alarm_tm, &time);
  236. + if (time < now) {
  237. + time += 60 * 60 * 24;
  238. + rtc_time_to_tm(time, &alarm_tm);
  239. + }
  240. + ret = rtc_tm_to_time(&alarm_tm, &time);
  241. +
  242. + /* clear all the interrupt status bits */
  243. + writel(readl(MCF_RTC_ISR), MCF_RTC_ISR);
  244. +
  245. + set_alarm_or_time(dev, MCF_RTC_ALARM, time);
  246. +
  247. + return ret;
  248. +}
  249. +
  250. +/*!
  251. + * This function is the RTC interrupt service routine.
  252. + *
  253. + * @param irq RTC IRQ number
  254. + * @param dev_id device ID which is not used
  255. + *
  256. + * @return IRQ_HANDLED as defined in the include/linux/interrupt.h file.
  257. + */
  258. +static irqreturn_t mcf_rtc_interrupt(int irq, void *dev_id)
  259. +{
  260. + struct platform_device *pdev = dev_id;
  261. + struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  262. + u32 status, events = 0;
  263. +
  264. + spin_lock(&rtc_lock);
  265. +
  266. + /* clear interrupt sources */
  267. + status = readl(MCF_RTC_ISR) & readl(MCF_RTC_IER);
  268. + writel(status, MCF_RTC_ISR);
  269. +
  270. + /* clear alarm interrupt if it has occurred */
  271. + if (status & MCF_RTC_ISR_ALM)
  272. + status &= ~MCF_RTC_ISR_ALM;
  273. +
  274. + /* update irq data & counter */
  275. + if (status & MCF_RTC_ISR_ALM)
  276. + events |= (RTC_AF | RTC_IRQF);
  277. + if (status & MCF_RTC_ISR_1HZ)
  278. + events |= (RTC_UF | RTC_IRQF);
  279. + if (status & PIT_ALL_ON)
  280. + events |= (RTC_PF | RTC_IRQF);
  281. +
  282. + if ((status & MCF_RTC_ISR_ALM) && rtc_valid_tm(&g_rtc_alarm))
  283. + rtc_update_alarm(&pdev->dev, &g_rtc_alarm);
  284. +
  285. + spin_unlock(&rtc_lock);
  286. + rtc_update_irq(pdata->rtc, 1, events);
  287. + return IRQ_HANDLED;
  288. +}
  289. +
  290. +/*!
  291. + * clear all interrupts and release the IRQ
  292. + */
  293. +static void mcf_rtc_release(struct device *dev)
  294. +{
  295. + spin_lock_irq(&rtc_lock);
  296. + writel(0, MCF_RTC_IER); /* Disable all rtc interrupts */
  297. + writel(0x0000FFBF, MCF_RTC_ISR); /* Clear all interrupt status */
  298. + spin_unlock_irq(&rtc_lock);
  299. + rtc_status = 0;
  300. +}
  301. +
  302. +/*!
  303. + * This function is used to support some ioctl calls directly.
  304. + * Other ioctl calls are supported indirectly through the
  305. + * arm/common/rtctime.c file.
  306. + *
  307. + * @param cmd ioctl command as defined in include/linux/rtc.h
  308. + * @param arg value for the ioctl command
  309. + *
  310. + * @return 0 if successful or negative value otherwise.
  311. + */
  312. +static int mcf_rtc_ioctl(struct device *dev, unsigned int cmd,
  313. + unsigned long arg)
  314. +{
  315. + int i;
  316. +
  317. + switch (cmd) {
  318. + case RTC_PIE_OFF:
  319. + writel((readl(MCF_RTC_IER) & ~PIT_ALL_ON), MCF_RTC_IER);
  320. + return 0;
  321. + case RTC_IRQP_SET:
  322. + if (arg < 2 || arg > MAX_PIE_FREQ || (arg % 2) != 0)
  323. + return -EINVAL; /* Also make sure a power of 2Hz */
  324. + if ((arg > 64) && (!capable(CAP_SYS_RESOURCE)))
  325. + return -EACCES;
  326. + rtc_freq = arg;
  327. + return 0;
  328. + case RTC_IRQP_READ:
  329. + return put_user(rtc_freq, (u32 *) arg);
  330. + case RTC_PIE_ON:
  331. + for (i = 0; i < MAX_PIE_NUM; i++) {
  332. + if (PIE_BIT_DEF[i][0] == rtc_freq)
  333. + break;
  334. + }
  335. + if (i == MAX_PIE_NUM)
  336. + return -EACCES;
  337. + spin_lock_irq(&rtc_lock);
  338. + writel((readl(MCF_RTC_IER) | PIE_BIT_DEF[i][1]), MCF_RTC_IER);
  339. + spin_unlock_irq(&rtc_lock);
  340. + return 0;
  341. + case RTC_AIE_OFF:
  342. + spin_lock_irq(&rtc_lock);
  343. + writel((readl(MCF_RTC_IER) & ~MCF_RTC_ISR_ALM), MCF_RTC_IER);
  344. + spin_unlock_irq(&rtc_lock);
  345. + return 0;
  346. +
  347. + case RTC_AIE_ON:
  348. + spin_lock_irq(&rtc_lock);
  349. + writel((readl(MCF_RTC_IER) | MCF_RTC_ISR_ALM), MCF_RTC_IER);
  350. + spin_unlock_irq(&rtc_lock);
  351. + return 0;
  352. +
  353. + case RTC_UIE_OFF: /* UIE is for the 1Hz interrupt */
  354. + spin_lock_irq(&rtc_lock);
  355. + writel((readl(MCF_RTC_IER) & ~MCF_RTC_ISR_1HZ), MCF_RTC_IER);
  356. + spin_unlock_irq(&rtc_lock);
  357. + return 0;
  358. +
  359. + case RTC_UIE_ON:
  360. + spin_lock_irq(&rtc_lock);
  361. + writel((readl(MCF_RTC_IER) | MCF_RTC_ISR_1HZ), MCF_RTC_IER);
  362. + spin_unlock_irq(&rtc_lock);
  363. + return 0;
  364. + }
  365. + return -ENOIOCTLCMD;
  366. +}
  367. +
  368. +/*!
  369. + * This function reads the current RTC time into tm in Gregorian date.
  370. + *
  371. + * @param tm contains the RTC time value upon return
  372. + *
  373. + * @return 0 if successful; non-zero otherwise.
  374. + */
  375. +static int mcf_rtc_read_time(struct device *dev, struct rtc_time *tm)
  376. +{
  377. + u32 val;
  378. +
  379. + /* Avoid roll-over from reading the different registers */
  380. + do {
  381. + val = get_alarm_or_time(dev, MCF_RTC_TIME);
  382. + } while (val != get_alarm_or_time(dev, MCF_RTC_TIME));
  383. +
  384. + rtc_time_to_tm(val, tm);
  385. + return 0;
  386. +}
  387. +
  388. +/*!
  389. + * This function sets the internal RTC time based on tm in Gregorian date.
  390. + *
  391. + * @param tm the time value to be set in the RTC
  392. + *
  393. + * @return 0 if successful; non-zero otherwise.
  394. + */
  395. +static int mcf_rtc_set_time(struct device *dev, struct rtc_time *tm)
  396. +{
  397. + unsigned long time;
  398. + int ret;
  399. +
  400. + ret = rtc_tm_to_time(tm, &time);
  401. + if (ret != 0)
  402. + return ret;
  403. +
  404. + /* Avoid roll-over from reading the different registers */
  405. + do {
  406. + set_alarm_or_time(dev, MCF_RTC_TIME, time);
  407. + } while (time != get_alarm_or_time(dev, MCF_RTC_TIME));
  408. +
  409. + return ret;
  410. +}
  411. +
  412. +/*!
  413. + * This function reads the current alarm value into the passed in \b alrm
  414. + * argument. It updates the \b alrm's pending field value based on the whether
  415. + * an alarm interrupt occurs or not.
  416. + *
  417. + * @param alrm contains the RTC alarm value upon return
  418. + *
  419. + * @return 0 if successful; non-zero otherwise.
  420. + */
  421. +static int mcf_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  422. +{
  423. + rtc_time_to_tm(get_alarm_or_time(dev, MCF_RTC_ALARM), &alrm->time);
  424. + alrm->pending = ((readl(MCF_RTC_ISR) & MCF_RTC_ISR_ALM) != 0) ? 1 : 0;
  425. +
  426. + return 0;
  427. +}
  428. +
  429. +/*!
  430. + * This function sets the RTC alarm based on passed in alrm.
  431. + *
  432. + * @param alrm the alarm value to be set in the RTC
  433. + *
  434. + * @return 0 if successful; non-zero otherwise.
  435. + */
  436. +static int mcf_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  437. +{
  438. + int ret;
  439. +
  440. + spin_lock_irq(&rtc_lock);
  441. + if (rtc_valid_tm(&alrm->time)) {
  442. + if (alrm->time.tm_sec > 59 ||
  443. + alrm->time.tm_hour > 23 || alrm->time.tm_min > 59) {
  444. + ret = -EINVAL;
  445. + goto out;
  446. + }
  447. + ret = rtc_update_alarm(dev, &alrm->time);
  448. + } else {
  449. + ret = rtc_valid_tm(&alrm->time);
  450. + if (ret)
  451. + goto out;
  452. + ret = rtc_update_alarm(dev, &alrm->time);
  453. + }
  454. +
  455. + if (ret == 0) {
  456. + memcpy(&g_rtc_alarm, &alrm->time, sizeof(struct rtc_time));
  457. +
  458. + if (alrm->enabled) {
  459. + writel((readl(MCF_RTC_IER) | MCF_RTC_ISR_ALM),
  460. + MCF_RTC_IER);
  461. + } else {
  462. + writel((readl(MCF_RTC_IER) & ~MCF_RTC_ISR_ALM),
  463. + MCF_RTC_IER);
  464. + }
  465. + }
  466. +out:
  467. + spin_unlock_irq(&rtc_lock);
  468. +
  469. + return ret;
  470. +}
  471. +
  472. +/*!
  473. + * This function is used to provide the content for the /proc/driver/rtc
  474. + * file.
  475. + *
  476. + * @param buf the buffer to hold the information that the driver
  477. + * wants to write
  478. + *
  479. + * @return The number of bytes written into the rtc file.
  480. + */
  481. +static int mcf_rtc_proc(struct device *dev, struct seq_file *sq)
  482. +{
  483. + char *p = sq->buf;
  484. +
  485. + p += sprintf(p, "alarm_IRQ\t: %s\n",
  486. + (((readl(MCF_RTC_IER)) & MCF_RTC_ISR_ALM) !=
  487. + 0) ? "yes" : "no");
  488. + p += sprintf(p, "update_IRQ\t: %s\n",
  489. + (((readl(MCF_RTC_IER)) & MCF_RTC_ISR_1HZ) !=
  490. + 0) ? "yes" : "no");
  491. + p += sprintf(p, "periodic_IRQ\t: %s\n",
  492. + (((readl(MCF_RTC_IER)) & PIT_ALL_ON) !=
  493. + 0) ? "yes" : "no");
  494. + p += sprintf(p, "periodic_freq\t: %d\n", rtc_freq);
  495. +
  496. + return p - (sq->buf);
  497. +}
  498. +
  499. +/*!
  500. + * The RTC driver structure
  501. + */
  502. +static struct rtc_class_ops mcf_rtc_ops = {
  503. + .ioctl = mcf_rtc_ioctl,
  504. + .read_time = mcf_rtc_read_time,
  505. + .set_time = mcf_rtc_set_time,
  506. + .read_alarm = mcf_rtc_read_alarm,
  507. + .set_alarm = mcf_rtc_set_alarm,
  508. + .proc = mcf_rtc_proc,
  509. +};
  510. +
  511. +static int __devinit mcf_rtc_probe(struct platform_device *pdev)
  512. +{
  513. + struct timespec tv;
  514. + struct rtc_device *rtc;
  515. + struct rtc_plat_data *pdata = NULL;
  516. + u32 ret = 0;
  517. +
  518. + pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
  519. + if (!pdata)
  520. + return -ENOMEM;
  521. + /* External clock is hard wired to 32768Hz.
  522. + * Clock settings 32K, 38.4K and 48K are defined above. */
  523. +#if defined(CONFIG_M5227x) | defined(CONFIG_M5445X)
  524. + writel(0, MCF_RTC_GOCU);
  525. + writel(RTC_INPUT_CLK_32768HZ, MCF_RTC_GOCL);
  526. +#endif
  527. + /* Configure and enable the RTC */
  528. + pdata->irq = MCFINT_VECBASE + MCFINT_RTC;
  529. + if (request_irq(pdata->irq, mcf_rtc_interrupt, IRQF_DISABLED,
  530. + pdev->name, pdev) < 0) {
  531. + dev_warn(&pdev->dev, "interrupt not available.\n");
  532. + pdata->irq = -1;
  533. + }
  534. +
  535. + if (test_and_set_bit(1, &rtc_status))
  536. + return -EBUSY;
  537. +
  538. + rtc = rtc_device_register(pdev->name, &pdev->dev, &mcf_rtc_ops,
  539. + THIS_MODULE);
  540. + if (IS_ERR(rtc)) {
  541. + ret = PTR_ERR(rtc);
  542. + if (pdata->irq >= 0)
  543. + free_irq(pdata->irq, pdev);
  544. + kfree(pdata);
  545. + return ret;
  546. + }
  547. + pdata->rtc = rtc;
  548. + platform_set_drvdata(pdev, pdata);
  549. +
  550. + tv.tv_nsec = 0;
  551. + tv.tv_sec = get_alarm_or_time(&pdev->dev, MCF_RTC_TIME);
  552. +
  553. +#ifdef CONFIG_M5301x
  554. + writel(RTC_INPUT_CLK_32768HZ, MCF_RTC_GOC);
  555. + writel(0x08, MCF_RTC_OCEN);
  556. +#endif
  557. + writeb(4, MCFSIM_ICR_RTC);
  558. +
  559. + writel(MCF_RTC_IER_1HZ, MCF_RTC_IER); /* Unmask the 1Hz timer */
  560. +
  561. + writel(MCF_RTC_CR_EN, MCF_RTC_CR);
  562. + if ((readl(MCF_RTC_CR) & MCF_RTC_CR_EN) == 0) {
  563. + printk(KERN_ALERT "RTC Hardware couldn't be enabled!\n");
  564. + return -EPERM;
  565. + }
  566. +
  567. + printk(KERN_INFO "Real Time Clock Driver v%s\n", RTC_VERSION);
  568. + return ret;
  569. +}
  570. +
  571. +static int __devexit mcf_rtc_remove(struct platform_device *pdev)
  572. +{
  573. + struct rtc_plat_data *pdata = platform_get_drvdata(pdev);
  574. +
  575. + rtc_device_unregister(pdata->rtc);
  576. + if (pdata->irq >= 0)
  577. + free_irq(pdata->irq, pdev);
  578. + kfree(pdata);
  579. + mcf_rtc_release(NULL);
  580. + return 0;
  581. +}
  582. +
  583. +/*!
  584. + * Contains pointers to the power management callback functions.
  585. + */
  586. +MODULE_ALIAS("mcf-rtc");
  587. +static struct platform_driver mcf_rtc_driver = {
  588. + .driver = {
  589. + .name = "mcf-rtc",
  590. + .owner = THIS_MODULE,
  591. + },
  592. + .probe = mcf_rtc_probe,
  593. + .remove = __devexit_p(mcf_rtc_remove),
  594. +};
  595. +
  596. +/*!
  597. + * This function creates the /proc/driver/rtc file and registers the device RTC
  598. + * in the /dev/misc directory. It also reads the RTC value from external source
  599. + * and setup the internal RTC properly.
  600. + *
  601. + * @return -1 if RTC is failed to initialize; 0 is successful.
  602. + */
  603. +static int __init mcf_rtc_init(void)
  604. +{
  605. + return platform_driver_register(&mcf_rtc_driver);
  606. +}
  607. +
  608. +/*!
  609. + * This function removes the /proc/driver/rtc file and un-registers the
  610. + * device RTC from the /dev/misc directory.
  611. + */
  612. +static void __exit mcf_rtc_exit(void)
  613. +{
  614. + platform_driver_unregister(&mcf_rtc_driver);
  615. +
  616. +}
  617. +
  618. +module_init(mcf_rtc_init);
  619. +module_exit(mcf_rtc_exit);
  620. +
  621. +MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  622. +MODULE_DESCRIPTION("Real Time Clock Driver (MCF)");
  623. +MODULE_LICENSE("GPL");