rtc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /*
  2. * Real Time Clock driver for WL-HDD
  3. *
  4. * Copyright (C) 2007 Andreas Engel
  5. *
  6. * Hacked together mostly by copying the relevant code parts from:
  7. * drivers/i2c/i2c-bcm5365.c
  8. * drivers/i2c/i2c-algo-bit.c
  9. * drivers/char/rtc.c
  10. *
  11. * Note 1:
  12. * This module uses the standard char device (10,135), while the Asus module
  13. * rtcdrv.o uses (12,0). So, both can coexist which might be handy during
  14. * development (but see the comment in rtc_open()).
  15. *
  16. * Note 2:
  17. * You might need to set the clock once after loading the driver the first
  18. * time because the driver switches the chip into 24h mode if it is running
  19. * in 12h mode.
  20. *
  21. * Usage:
  22. * For compatibility reasons with the original asus driver, the time can be
  23. * read and set via the /dev/rtc device entry. The only accepted data format
  24. * is "YYYY:MM:DD:W:HH:MM:SS\n". See OpenWrt wiki for a script which handles
  25. * this format.
  26. *
  27. * In addition, this driver supports the standard ioctl() calls for setting
  28. * and reading the hardware clock, so the ordinary hwclock utility can also
  29. * be used.
  30. *
  31. * This program is free software; you can redistribute it and/or
  32. * modify it under the terms of the GNU General Public License
  33. * as published by the Free Software Foundation; either version
  34. * 2 of the License, or (at your option) any later version.
  35. *
  36. * TODO:
  37. * - add a /proc/driver/rtc interface?
  38. * - make the battery failure bit available through the /proc interface?
  39. *
  40. * $Id: rtc.c 7 2007-05-25 19:37:01Z ae $
  41. */
  42. #include <linux/module.h>
  43. #include <linux/kmod.h>
  44. #include <linux/kernel.h>
  45. #include <linux/types.h>
  46. #include <linux/miscdevice.h>
  47. #include <linux/ioport.h>
  48. #include <linux/fcntl.h>
  49. #include <linux/mc146818rtc.h>
  50. #include <linux/init.h>
  51. #include <linux/spinlock.h>
  52. #include <linux/rtc.h>
  53. #include <linux/delay.h>
  54. #include <linux/version.h>
  55. #include <linux/gpio.h>
  56. #include <linux/uaccess.h>
  57. #include <asm/current.h>
  58. #if LINUX_VERSION_CODE < KERNEL_VERSION(3,4,0)
  59. #include <asm/system.h>
  60. #endif
  61. #include <bcm47xx.h>
  62. #include <linux/bcm47xx_nvram.h>
  63. #define RTC_IS_OPEN 0x01 /* Means /dev/rtc is in use. */
  64. /* Can be changed via a module parameter. */
  65. static int rtc_debug = 0;
  66. static unsigned long rtc_status = 0; /* Bitmapped status byte. */
  67. /* These settings are platform dependents. */
  68. unsigned int sda_index = 0;
  69. unsigned int scl_index = 0;
  70. #define I2C_READ_MASK 1
  71. #define I2C_WRITE_MASK 0
  72. #define I2C_ACK 1
  73. #define I2C_NAK 0
  74. #define RTC_EPOCH 1900
  75. #define RTC_I2C_ADDRESS (0x32 << 1)
  76. #define RTC_24HOUR_MODE_MASK 0x20
  77. #define RTC_PM_MASK 0x20
  78. #define RTC_VDET_MASK 0x40
  79. #define RTC_Y2K_MASK 0x80
  80. /*
  81. * Delay in microseconds for generating the pulses on the I2C bus. We use
  82. * a rather conservative setting here. See datasheet of the RTC chip.
  83. */
  84. #define ADAP_DELAY 50
  85. /* Avoid spurious compiler warnings. */
  86. #define UNUSED __attribute__((unused))
  87. MODULE_AUTHOR("Andreas Engel");
  88. MODULE_LICENSE("GPL");
  89. /* Test stolen from switch-adm.c. */
  90. module_param(rtc_debug, int, 0);
  91. static inline void sdalo(void)
  92. {
  93. gpio_direction_output(sda_index, 1);
  94. udelay(ADAP_DELAY);
  95. }
  96. static inline void sdahi(void)
  97. {
  98. gpio_direction_input(sda_index);
  99. udelay(ADAP_DELAY);
  100. }
  101. static inline void scllo(void)
  102. {
  103. gpio_direction_output(scl_index, 1);
  104. udelay(ADAP_DELAY);
  105. }
  106. static inline int getscl(void)
  107. {
  108. return (gpio_get_value(scl_index));
  109. }
  110. static inline int getsda(void)
  111. {
  112. return (gpio_get_value(sda_index));
  113. }
  114. /*
  115. * We shouldn't simply set the SCL pin to high. Like SDA, the SCL line is
  116. * bidirectional too. According to the I2C spec, the slave is allowed to
  117. * pull down the SCL line to slow down the clock, so we need to check this.
  118. * Generally, we'd need a timeout here, but in our case, we just check the
  119. * line, assuming the RTC chip behaves well.
  120. */
  121. static int sclhi(void)
  122. {
  123. gpio_direction_input(scl_index);
  124. udelay(ADAP_DELAY);
  125. if (!getscl()) {
  126. printk(KERN_ERR "SCL pin should be low\n");
  127. return -ETIMEDOUT;
  128. }
  129. return 0;
  130. }
  131. static void i2c_start(void)
  132. {
  133. sdalo();
  134. scllo();
  135. }
  136. static void i2c_stop(void)
  137. {
  138. sdalo();
  139. sclhi();
  140. sdahi();
  141. }
  142. static int i2c_outb(int c)
  143. {
  144. int i;
  145. int ack;
  146. /* assert: scl is low */
  147. for (i = 7; i >= 0; i--) {
  148. if (c & ( 1 << i )) {
  149. sdahi();
  150. } else {
  151. sdalo();
  152. }
  153. if (sclhi() < 0) { /* timed out */
  154. sdahi(); /* we don't want to block the net */
  155. return -ETIMEDOUT;
  156. };
  157. scllo();
  158. }
  159. sdahi();
  160. if (sclhi() < 0) {
  161. return -ETIMEDOUT;
  162. };
  163. /* read ack: SDA should be pulled down by slave */
  164. ack = getsda() == 0; /* ack: sda is pulled low ->success. */
  165. scllo();
  166. if (rtc_debug)
  167. printk(KERN_DEBUG "i2c_outb(0x%02x) -> %s\n",
  168. c, ack ? "ACK": "NAK");
  169. return ack; /* return 1 if device acked */
  170. /* assert: scl is low (sda undef) */
  171. }
  172. static int i2c_inb(int ack)
  173. {
  174. int i;
  175. unsigned int indata = 0;
  176. /* assert: scl is low */
  177. sdahi();
  178. for (i = 0; i < 8; i++) {
  179. if (sclhi() < 0) {
  180. return -ETIMEDOUT;
  181. };
  182. indata *= 2;
  183. if (getsda())
  184. indata |= 0x01;
  185. scllo();
  186. }
  187. if (ack) {
  188. sdalo();
  189. } else {
  190. sdahi();
  191. }
  192. if (sclhi() < 0) {
  193. sdahi();
  194. return -ETIMEDOUT;
  195. }
  196. scllo();
  197. sdahi();
  198. if (rtc_debug)
  199. printk(KERN_DEBUG "i2c_inb() -> 0x%02x\n", indata);
  200. /* assert: scl is low */
  201. return indata & 0xff;
  202. }
  203. static void i2c_init(void)
  204. {
  205. /* no gpio_control for EXTIF */
  206. // ssb_gpio_control(&ssb, sda_mask | scl_mask, 0);
  207. gpio_set_value(sda_index, 0);
  208. gpio_set_value(scl_index, 0);
  209. sdahi();
  210. sclhi();
  211. }
  212. static int rtc_open(UNUSED struct inode *inode, UNUSED struct file *filp)
  213. {
  214. spin_lock_irq(&rtc_lock);
  215. if (rtc_status & RTC_IS_OPEN) {
  216. spin_unlock_irq(&rtc_lock);
  217. return -EBUSY;
  218. }
  219. rtc_status |= RTC_IS_OPEN;
  220. /*
  221. * The following call is only necessary if we use both this driver and
  222. * the proprietary one from asus at the same time (which, b.t.w. only
  223. * makes sense during development). Otherwise, each access via the asus
  224. * driver will make access via this driver impossible.
  225. */
  226. i2c_init();
  227. spin_unlock_irq(&rtc_lock);
  228. return 0;
  229. }
  230. static int rtc_release(UNUSED struct inode *inode, UNUSED struct file *filp)
  231. {
  232. /* No need for locking here. */
  233. rtc_status &= ~RTC_IS_OPEN;
  234. return 0;
  235. }
  236. static int from_bcd(int bcdnum)
  237. {
  238. int fac, num = 0;
  239. for (fac = 1; bcdnum; fac *= 10) {
  240. num += (bcdnum % 16) * fac;
  241. bcdnum /= 16;
  242. }
  243. return num;
  244. }
  245. static int to_bcd(int decnum)
  246. {
  247. int fac, num = 0;
  248. for (fac = 1; decnum; fac *= 16) {
  249. num += (decnum % 10) * fac;
  250. decnum /= 10;
  251. }
  252. return num;
  253. }
  254. static void get_rtc_time(struct rtc_time *rtc_tm)
  255. {
  256. int cr2;
  257. /*
  258. * Read date and time from the RTC. We use read method (3).
  259. */
  260. spin_lock_irq(&rtc_lock);
  261. i2c_start();
  262. i2c_outb(RTC_I2C_ADDRESS | I2C_READ_MASK);
  263. cr2 = i2c_inb(I2C_ACK);
  264. rtc_tm->tm_sec = i2c_inb(I2C_ACK);
  265. rtc_tm->tm_min = i2c_inb(I2C_ACK);
  266. rtc_tm->tm_hour = i2c_inb(I2C_ACK);
  267. rtc_tm->tm_wday = i2c_inb(I2C_ACK);
  268. rtc_tm->tm_mday = i2c_inb(I2C_ACK);
  269. rtc_tm->tm_mon = i2c_inb(I2C_ACK);
  270. rtc_tm->tm_year = i2c_inb(I2C_NAK);
  271. i2c_stop();
  272. spin_unlock_irq(&rtc_lock);
  273. if (cr2 & RTC_VDET_MASK) {
  274. printk(KERN_WARNING "***RTC BATTERY FAILURE***\n");
  275. }
  276. /* Handle century bit */
  277. if (rtc_tm->tm_mon & RTC_Y2K_MASK) {
  278. rtc_tm->tm_mon &= ~RTC_Y2K_MASK;
  279. rtc_tm->tm_year += 0x100;
  280. }
  281. rtc_tm->tm_sec = from_bcd(rtc_tm->tm_sec);
  282. rtc_tm->tm_min = from_bcd(rtc_tm->tm_min);
  283. rtc_tm->tm_hour = from_bcd(rtc_tm->tm_hour);
  284. rtc_tm->tm_mday = from_bcd(rtc_tm->tm_mday);
  285. rtc_tm->tm_mon = from_bcd(rtc_tm->tm_mon) - 1;
  286. rtc_tm->tm_year = from_bcd(rtc_tm->tm_year);
  287. rtc_tm->tm_isdst = -1; /* DST not known */
  288. }
  289. static void set_rtc_time(struct rtc_time *rtc_tm)
  290. {
  291. rtc_tm->tm_sec = to_bcd(rtc_tm->tm_sec);
  292. rtc_tm->tm_min = to_bcd(rtc_tm->tm_min);
  293. rtc_tm->tm_hour = to_bcd(rtc_tm->tm_hour);
  294. rtc_tm->tm_mday = to_bcd(rtc_tm->tm_mday);
  295. rtc_tm->tm_mon = to_bcd(rtc_tm->tm_mon + 1);
  296. rtc_tm->tm_year = to_bcd(rtc_tm->tm_year);
  297. if (rtc_tm->tm_year >= 0x100) {
  298. rtc_tm->tm_year -= 0x100;
  299. rtc_tm->tm_mon |= RTC_Y2K_MASK;
  300. }
  301. spin_lock_irq(&rtc_lock);
  302. i2c_start();
  303. i2c_outb(RTC_I2C_ADDRESS | I2C_WRITE_MASK);
  304. i2c_outb(0x00); /* set starting register to 0 (=seconds) */
  305. i2c_outb(rtc_tm->tm_sec);
  306. i2c_outb(rtc_tm->tm_min);
  307. i2c_outb(rtc_tm->tm_hour);
  308. i2c_outb(rtc_tm->tm_wday);
  309. i2c_outb(rtc_tm->tm_mday);
  310. i2c_outb(rtc_tm->tm_mon);
  311. i2c_outb(rtc_tm->tm_year);
  312. i2c_stop();
  313. spin_unlock_irq(&rtc_lock);
  314. }
  315. static ssize_t rtc_write(UNUSED struct file *filp, const char *buf,
  316. size_t count, loff_t *ppos)
  317. {
  318. struct rtc_time rtc_tm;
  319. char buffer[23];
  320. char *p;
  321. if (!capable(CAP_SYS_TIME))
  322. return -EACCES;
  323. if (ppos != &filp->f_pos)
  324. return -ESPIPE;
  325. /*
  326. * For simplicity, the only acceptable format is:
  327. * YYYY:MM:DD:W:HH:MM:SS\n
  328. */
  329. if (count != 22)
  330. goto err_out;
  331. if (copy_from_user(buffer, buf, count))
  332. return -EFAULT;
  333. buffer[sizeof(buffer)-1] = '\0';
  334. p = &buffer[0];
  335. rtc_tm.tm_year = simple_strtoul(p, &p, 10);
  336. if (*p++ != ':') goto err_out;
  337. rtc_tm.tm_mon = simple_strtoul(p, &p, 10) - 1;
  338. if (*p++ != ':') goto err_out;
  339. rtc_tm.tm_mday = simple_strtoul(p, &p, 10);
  340. if (*p++ != ':') goto err_out;
  341. rtc_tm.tm_wday = simple_strtoul(p, &p, 10);
  342. if (*p++ != ':') goto err_out;
  343. rtc_tm.tm_hour = simple_strtoul(p, &p, 10);
  344. if (*p++ != ':') goto err_out;
  345. rtc_tm.tm_min = simple_strtoul(p, &p, 10);
  346. if (*p++ != ':') goto err_out;
  347. rtc_tm.tm_sec = simple_strtoul(p, &p, 10);
  348. if (*p != '\n') goto err_out;
  349. rtc_tm.tm_year -= RTC_EPOCH;
  350. set_rtc_time(&rtc_tm);
  351. *ppos += count;
  352. return count;
  353. err_out:
  354. printk(KERN_ERR "invalid format: use YYYY:MM:DD:W:HH:MM:SS\\n\n");
  355. return -EINVAL;
  356. }
  357. static ssize_t rtc_read(UNUSED struct file *filp, char *buf, size_t count,
  358. loff_t *ppos)
  359. {
  360. char wbuf[23];
  361. struct rtc_time tm;
  362. ssize_t len;
  363. if (count == 0 || *ppos != 0)
  364. return 0;
  365. get_rtc_time(&tm);
  366. len = sprintf(wbuf, "%04d:%02d:%02d:%d:%02d:%02d:%02d\n",
  367. tm.tm_year + RTC_EPOCH,
  368. tm.tm_mon + 1,
  369. tm.tm_mday,
  370. tm.tm_wday,
  371. tm.tm_hour,
  372. tm.tm_min,
  373. tm.tm_sec);
  374. if (len > (ssize_t)count)
  375. len = count;
  376. if (copy_to_user(buf, wbuf, len))
  377. return -EFAULT;
  378. *ppos += len;
  379. return len;
  380. }
  381. static int rtc_do_ioctl(unsigned int cmd, unsigned long arg)
  382. {
  383. struct rtc_time rtc_tm;
  384. switch (cmd) {
  385. case RTC_RD_TIME:
  386. memset(&rtc_tm, 0, sizeof(struct rtc_time));
  387. get_rtc_time(&rtc_tm);
  388. if (copy_to_user((void *)arg, &rtc_tm, sizeof(rtc_tm)))
  389. return -EFAULT;
  390. break;
  391. case RTC_SET_TIME:
  392. if (!capable(CAP_SYS_TIME))
  393. return -EACCES;
  394. if (copy_from_user(&rtc_tm, (struct rtc_time *)arg,
  395. sizeof(struct rtc_time)))
  396. return -EFAULT;
  397. set_rtc_time(&rtc_tm);
  398. break;
  399. default:
  400. return -ENOTTY;
  401. }
  402. return 0;
  403. }
  404. static long rtc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  405. {
  406. long ret;
  407. ret = rtc_do_ioctl(cmd, arg);
  408. return ret;
  409. }
  410. static const struct file_operations rtc_fops = {
  411. .owner = THIS_MODULE,
  412. .llseek = no_llseek,
  413. .read = rtc_read,
  414. .write = rtc_write,
  415. .unlocked_ioctl = rtc_ioctl,
  416. .open = rtc_open,
  417. .release = rtc_release,
  418. };
  419. static struct miscdevice rtc_dev = {
  420. .minor = RTC_MINOR,
  421. .name = "rtc",
  422. .fops = &rtc_fops,
  423. };
  424. /* Savagely ripped from diag.c. */
  425. static inline int startswith (char *source, char *cmp)
  426. {
  427. return !strncmp(source, cmp, strlen(cmp));
  428. }
  429. static void platform_detect(void)
  430. {
  431. char buf[20];
  432. int et0phyaddr, et1phyaddr;
  433. /* Based on "model_no". */
  434. if (bcm47xx_nvram_getenv("model_no", buf, sizeof(buf)) >= 0) {
  435. if (startswith(buf, "WL700")) { /* WL700* */
  436. sda_index = 2;
  437. scl_index = 5;
  438. return;
  439. }
  440. }
  441. if (bcm47xx_nvram_getenv("et0phyaddr", buf, sizeof(buf)) >= 0 )
  442. et0phyaddr = simple_strtoul(buf, NULL, 0);
  443. if (bcm47xx_nvram_getenv("et1phyaddr", buf, sizeof(buf)) >= 0 )
  444. et1phyaddr = simple_strtoul(buf, NULL, 0);
  445. if (bcm47xx_nvram_getenv("hardware_version", buf, sizeof(buf)) >= 0) {
  446. /* Either WL-300g or WL-HDD, do more extensive checks */
  447. if (startswith(buf, "WL300-") && et0phyaddr == 0 && et1phyaddr == 1) {
  448. sda_index = 4;
  449. scl_index = 5;
  450. return;
  451. }
  452. }
  453. /* not found */
  454. }
  455. static int __init rtc_init(void)
  456. {
  457. int cr1;
  458. platform_detect();
  459. if (sda_index == scl_index) {
  460. printk(KERN_ERR "RTC-RV5C386A: unrecognized platform!\n");
  461. return -ENODEV;
  462. }
  463. i2c_init();
  464. /*
  465. * Switch RTC to 24h mode
  466. */
  467. spin_lock_irq(&rtc_lock);
  468. i2c_start();
  469. i2c_outb(RTC_I2C_ADDRESS | I2C_WRITE_MASK);
  470. i2c_outb(0xE4); /* start at address 0xE, transmission mode 4 */
  471. cr1 = i2c_inb(I2C_NAK);
  472. i2c_stop();
  473. spin_unlock_irq(&rtc_lock);
  474. if ((cr1 & RTC_24HOUR_MODE_MASK) == 0) {
  475. /* RTC is running in 12h mode */
  476. printk(KERN_INFO "rtc.o: switching to 24h mode\n");
  477. spin_lock_irq(&rtc_lock);
  478. i2c_start();
  479. i2c_outb(RTC_I2C_ADDRESS | I2C_WRITE_MASK);
  480. i2c_outb(0xE0);
  481. i2c_outb(cr1 | RTC_24HOUR_MODE_MASK);
  482. i2c_stop();
  483. spin_unlock_irq(&rtc_lock);
  484. }
  485. misc_register(&rtc_dev);
  486. printk(KERN_INFO "RV5C386A Real Time Clock Driver loaded\n");
  487. return 0;
  488. }
  489. static void __exit rtc_exit (void)
  490. {
  491. misc_deregister(&rtc_dev);
  492. printk(KERN_INFO "Successfully removed RTC RV5C386A driver\n");
  493. }
  494. module_init(rtc_init);
  495. module_exit(rtc_exit);
  496. /*
  497. * Local Variables:
  498. * indent-tabs-mode:t
  499. * c-basic-offset:8
  500. * End:
  501. */