pm.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * FR-V Power Management Routines
  3. *
  4. * Copyright (c) 2004 Red Hat, Inc.
  5. *
  6. * Based on SA1100 version:
  7. * Copyright (c) 2001 Cliff Brake <cbrake@accelent.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License.
  11. *
  12. */
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/pm.h>
  16. #include <linux/pm_legacy.h>
  17. #include <linux/sched.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/sysctl.h>
  20. #include <linux/errno.h>
  21. #include <linux/delay.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/mb86943a.h>
  24. #include "local.h"
  25. /*
  26. * Debug macros
  27. */
  28. #define DEBUG
  29. int pm_do_suspend(void)
  30. {
  31. local_irq_disable();
  32. __set_LEDS(0xb1);
  33. /* go zzz */
  34. frv_cpu_suspend(pdm_suspend_mode);
  35. __set_LEDS(0xb2);
  36. local_irq_enable();
  37. return 0;
  38. }
  39. static unsigned long __irq_mask;
  40. /*
  41. * Setup interrupt masks, etc to enable wakeup by power switch
  42. */
  43. static void __default_power_switch_setup(void)
  44. {
  45. /* default is to mask all interrupt sources. */
  46. __irq_mask = *(unsigned long *)0xfeff9820;
  47. *(unsigned long *)0xfeff9820 = 0xfffe0000;
  48. }
  49. /*
  50. * Cleanup interrupt masks, etc after wakeup by power switch
  51. */
  52. static void __default_power_switch_cleanup(void)
  53. {
  54. *(unsigned long *)0xfeff9820 = __irq_mask;
  55. }
  56. /*
  57. * Return non-zero if wakeup irq was caused by power switch
  58. */
  59. static int __default_power_switch_check(void)
  60. {
  61. return 1;
  62. }
  63. void (*__power_switch_wake_setup)(void) = __default_power_switch_setup;
  64. int (*__power_switch_wake_check)(void) = __default_power_switch_check;
  65. void (*__power_switch_wake_cleanup)(void) = __default_power_switch_cleanup;
  66. int pm_do_bus_sleep(void)
  67. {
  68. local_irq_disable();
  69. /*
  70. * Here is where we need some platform-dependent setup
  71. * of the interrupt state so that appropriate wakeup
  72. * sources are allowed and all others are masked.
  73. */
  74. __power_switch_wake_setup();
  75. __set_LEDS(0xa1);
  76. /* go zzz
  77. *
  78. * This is in a loop in case power switch shares an irq with other
  79. * devices. The wake_check() tells us if we need to finish waking
  80. * or go back to sleep.
  81. */
  82. do {
  83. frv_cpu_suspend(HSR0_PDM_BUS_SLEEP);
  84. } while (__power_switch_wake_check && !__power_switch_wake_check());
  85. __set_LEDS(0xa2);
  86. /*
  87. * Here is where we need some platform-dependent restore
  88. * of the interrupt state prior to being called.
  89. */
  90. __power_switch_wake_cleanup();
  91. local_irq_enable();
  92. return 0;
  93. }
  94. unsigned long sleep_phys_sp(void *sp)
  95. {
  96. return virt_to_phys(sp);
  97. }
  98. #ifdef CONFIG_SYSCTL
  99. /*
  100. * Use a temporary sysctl number. Horrid, but will be cleaned up in 2.6
  101. * when all the PM interfaces exist nicely.
  102. */
  103. #define CTL_PM_SUSPEND 1
  104. #define CTL_PM_CMODE 2
  105. #define CTL_PM_P0 4
  106. #define CTL_PM_CM 5
  107. static int user_atoi(char __user *ubuf, size_t len)
  108. {
  109. char buf[16];
  110. unsigned long ret;
  111. if (len > 15)
  112. return -EINVAL;
  113. if (copy_from_user(buf, ubuf, len))
  114. return -EFAULT;
  115. buf[len] = 0;
  116. ret = simple_strtoul(buf, NULL, 0);
  117. if (ret > INT_MAX)
  118. return -ERANGE;
  119. return ret;
  120. }
  121. /*
  122. * Send us to sleep.
  123. */
  124. static int sysctl_pm_do_suspend(ctl_table *ctl, int write, struct file *filp,
  125. void __user *buffer, size_t *lenp, loff_t *fpos)
  126. {
  127. int retval, mode;
  128. if (*lenp <= 0)
  129. return -EIO;
  130. mode = user_atoi(buffer, *lenp);
  131. if ((mode != 1) && (mode != 5))
  132. return -EINVAL;
  133. retval = pm_send_all(PM_SUSPEND, (void *)3);
  134. if (retval == 0) {
  135. if (mode == 5)
  136. retval = pm_do_bus_sleep();
  137. else
  138. retval = pm_do_suspend();
  139. pm_send_all(PM_RESUME, (void *)0);
  140. }
  141. return retval;
  142. }
  143. static int try_set_cmode(int new_cmode)
  144. {
  145. if (new_cmode > 15)
  146. return -EINVAL;
  147. if (!(clock_cmodes_permitted & (1<<new_cmode)))
  148. return -EINVAL;
  149. /* tell all the drivers we're suspending */
  150. pm_send_all(PM_SUSPEND, (void *)3);
  151. /* now change cmode */
  152. local_irq_disable();
  153. frv_dma_pause_all();
  154. frv_change_cmode(new_cmode);
  155. determine_clocks(0);
  156. time_divisor_init();
  157. #ifdef DEBUG
  158. determine_clocks(1);
  159. #endif
  160. frv_dma_resume_all();
  161. local_irq_enable();
  162. /* tell all the drivers we're resuming */
  163. pm_send_all(PM_RESUME, (void *)0);
  164. return 0;
  165. }
  166. static int cmode_procctl(ctl_table *ctl, int write, struct file *filp,
  167. void __user *buffer, size_t *lenp, loff_t *fpos)
  168. {
  169. int new_cmode;
  170. if (!write)
  171. return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
  172. new_cmode = user_atoi(buffer, *lenp);
  173. return try_set_cmode(new_cmode)?:*lenp;
  174. }
  175. static int cmode_sysctl(ctl_table *table, int __user *name, int nlen,
  176. void __user *oldval, size_t __user *oldlenp,
  177. void __user *newval, size_t newlen)
  178. {
  179. if (oldval && oldlenp) {
  180. size_t oldlen;
  181. if (get_user(oldlen, oldlenp))
  182. return -EFAULT;
  183. if (oldlen != sizeof(int))
  184. return -EINVAL;
  185. if (put_user(clock_cmode_current, (unsigned __user *)oldval) ||
  186. put_user(sizeof(int), oldlenp))
  187. return -EFAULT;
  188. }
  189. if (newval && newlen) {
  190. int new_cmode;
  191. if (newlen != sizeof(int))
  192. return -EINVAL;
  193. if (get_user(new_cmode, (int __user *)newval))
  194. return -EFAULT;
  195. return try_set_cmode(new_cmode)?:1;
  196. }
  197. return 1;
  198. }
  199. static int try_set_p0(int new_p0)
  200. {
  201. unsigned long flags, clkc;
  202. if (new_p0 < 0 || new_p0 > 1)
  203. return -EINVAL;
  204. local_irq_save(flags);
  205. __set_PSR(flags & ~PSR_ET);
  206. frv_dma_pause_all();
  207. clkc = __get_CLKC();
  208. if (new_p0)
  209. clkc |= CLKC_P0;
  210. else
  211. clkc &= ~CLKC_P0;
  212. __set_CLKC(clkc);
  213. determine_clocks(0);
  214. time_divisor_init();
  215. #ifdef DEBUG
  216. determine_clocks(1);
  217. #endif
  218. frv_dma_resume_all();
  219. local_irq_restore(flags);
  220. return 0;
  221. }
  222. static int try_set_cm(int new_cm)
  223. {
  224. unsigned long flags, clkc;
  225. if (new_cm < 0 || new_cm > 1)
  226. return -EINVAL;
  227. local_irq_save(flags);
  228. __set_PSR(flags & ~PSR_ET);
  229. frv_dma_pause_all();
  230. clkc = __get_CLKC();
  231. clkc &= ~CLKC_CM;
  232. clkc |= new_cm;
  233. __set_CLKC(clkc);
  234. determine_clocks(0);
  235. time_divisor_init();
  236. #if 1 //def DEBUG
  237. determine_clocks(1);
  238. #endif
  239. frv_dma_resume_all();
  240. local_irq_restore(flags);
  241. return 0;
  242. }
  243. static int p0_procctl(ctl_table *ctl, int write, struct file *filp,
  244. void __user *buffer, size_t *lenp, loff_t *fpos)
  245. {
  246. int new_p0;
  247. if (!write)
  248. return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
  249. new_p0 = user_atoi(buffer, *lenp);
  250. return try_set_p0(new_p0)?:*lenp;
  251. }
  252. static int p0_sysctl(ctl_table *table, int __user *name, int nlen,
  253. void __user *oldval, size_t __user *oldlenp,
  254. void __user *newval, size_t newlen)
  255. {
  256. if (oldval && oldlenp) {
  257. size_t oldlen;
  258. if (get_user(oldlen, oldlenp))
  259. return -EFAULT;
  260. if (oldlen != sizeof(int))
  261. return -EINVAL;
  262. if (put_user(clock_p0_current, (unsigned __user *)oldval) ||
  263. put_user(sizeof(int), oldlenp))
  264. return -EFAULT;
  265. }
  266. if (newval && newlen) {
  267. int new_p0;
  268. if (newlen != sizeof(int))
  269. return -EINVAL;
  270. if (get_user(new_p0, (int __user *)newval))
  271. return -EFAULT;
  272. return try_set_p0(new_p0)?:1;
  273. }
  274. return 1;
  275. }
  276. static int cm_procctl(ctl_table *ctl, int write, struct file *filp,
  277. void __user *buffer, size_t *lenp, loff_t *fpos)
  278. {
  279. int new_cm;
  280. if (!write)
  281. return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
  282. new_cm = user_atoi(buffer, *lenp);
  283. return try_set_cm(new_cm)?:*lenp;
  284. }
  285. static int cm_sysctl(ctl_table *table, int __user *name, int nlen,
  286. void __user *oldval, size_t __user *oldlenp,
  287. void __user *newval, size_t newlen)
  288. {
  289. if (oldval && oldlenp) {
  290. size_t oldlen;
  291. if (get_user(oldlen, oldlenp))
  292. return -EFAULT;
  293. if (oldlen != sizeof(int))
  294. return -EINVAL;
  295. if (put_user(clock_cm_current, (unsigned __user *)oldval) ||
  296. put_user(sizeof(int), oldlenp))
  297. return -EFAULT;
  298. }
  299. if (newval && newlen) {
  300. int new_cm;
  301. if (newlen != sizeof(int))
  302. return -EINVAL;
  303. if (get_user(new_cm, (int __user *)newval))
  304. return -EFAULT;
  305. return try_set_cm(new_cm)?:1;
  306. }
  307. return 1;
  308. }
  309. static struct ctl_table pm_table[] =
  310. {
  311. {
  312. .ctl_name = CTL_PM_SUSPEND,
  313. .procname = "suspend",
  314. .data = NULL,
  315. .maxlen = 0,
  316. .mode = 0200,
  317. .proc_handler = &sysctl_pm_do_suspend,
  318. },
  319. {
  320. .ctl_name = CTL_PM_CMODE,
  321. .procname = "cmode",
  322. .data = &clock_cmode_current,
  323. .maxlen = sizeof(int),
  324. .mode = 0644,
  325. .proc_handler = &cmode_procctl,
  326. .strategy = &cmode_sysctl,
  327. },
  328. {
  329. .ctl_name = CTL_PM_P0,
  330. .procname = "p0",
  331. .data = &clock_p0_current,
  332. .maxlen = sizeof(int),
  333. .mode = 0644,
  334. .proc_handler = &p0_procctl,
  335. .strategy = &p0_sysctl,
  336. },
  337. {
  338. .ctl_name = CTL_PM_CM,
  339. .procname = "cm",
  340. .data = &clock_cm_current,
  341. .maxlen = sizeof(int),
  342. .mode = 0644,
  343. .proc_handler = &cm_procctl,
  344. .strategy = &cm_sysctl,
  345. },
  346. { .ctl_name = 0}
  347. };
  348. static struct ctl_table pm_dir_table[] =
  349. {
  350. {
  351. .ctl_name = CTL_PM,
  352. .procname = "pm",
  353. .mode = 0555,
  354. .child = pm_table,
  355. },
  356. { .ctl_name = 0}
  357. };
  358. /*
  359. * Initialize power interface
  360. */
  361. static int __init pm_init(void)
  362. {
  363. register_sysctl_table(pm_dir_table);
  364. return 0;
  365. }
  366. __initcall(pm_init);
  367. #endif