pm.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 9899
  104. #define CTL_PM_SUSPEND 1
  105. #define CTL_PM_CMODE 2
  106. #define CTL_PM_P0 4
  107. #define CTL_PM_CM 5
  108. static int user_atoi(char __user *ubuf, size_t len)
  109. {
  110. char buf[16];
  111. unsigned long ret;
  112. if (len > 15)
  113. return -EINVAL;
  114. if (copy_from_user(buf, ubuf, len))
  115. return -EFAULT;
  116. buf[len] = 0;
  117. ret = simple_strtoul(buf, NULL, 0);
  118. if (ret > INT_MAX)
  119. return -ERANGE;
  120. return ret;
  121. }
  122. /*
  123. * Send us to sleep.
  124. */
  125. static int sysctl_pm_do_suspend(ctl_table *ctl, int write, struct file *filp,
  126. void __user *buffer, size_t *lenp, loff_t *fpos)
  127. {
  128. int retval, mode;
  129. if (*lenp <= 0)
  130. return -EIO;
  131. mode = user_atoi(buffer, *lenp);
  132. if ((mode != 1) && (mode != 5))
  133. return -EINVAL;
  134. retval = pm_send_all(PM_SUSPEND, (void *)3);
  135. if (retval == 0) {
  136. if (mode == 5)
  137. retval = pm_do_bus_sleep();
  138. else
  139. retval = pm_do_suspend();
  140. pm_send_all(PM_RESUME, (void *)0);
  141. }
  142. return retval;
  143. }
  144. static int try_set_cmode(int new_cmode)
  145. {
  146. if (new_cmode > 15)
  147. return -EINVAL;
  148. if (!(clock_cmodes_permitted & (1<<new_cmode)))
  149. return -EINVAL;
  150. /* tell all the drivers we're suspending */
  151. pm_send_all(PM_SUSPEND, (void *)3);
  152. /* now change cmode */
  153. local_irq_disable();
  154. frv_dma_pause_all();
  155. frv_change_cmode(new_cmode);
  156. determine_clocks(0);
  157. time_divisor_init();
  158. #ifdef DEBUG
  159. determine_clocks(1);
  160. #endif
  161. frv_dma_resume_all();
  162. local_irq_enable();
  163. /* tell all the drivers we're resuming */
  164. pm_send_all(PM_RESUME, (void *)0);
  165. return 0;
  166. }
  167. static int cmode_procctl(ctl_table *ctl, int write, struct file *filp,
  168. void __user *buffer, size_t *lenp, loff_t *fpos)
  169. {
  170. int new_cmode;
  171. if (!write)
  172. return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
  173. new_cmode = user_atoi(buffer, *lenp);
  174. return try_set_cmode(new_cmode)?:*lenp;
  175. }
  176. static int cmode_sysctl(ctl_table *table, int __user *name, int nlen,
  177. void __user *oldval, size_t __user *oldlenp,
  178. void __user *newval, size_t newlen, void **context)
  179. {
  180. if (oldval && oldlenp) {
  181. size_t oldlen;
  182. if (get_user(oldlen, oldlenp))
  183. return -EFAULT;
  184. if (oldlen != sizeof(int))
  185. return -EINVAL;
  186. if (put_user(clock_cmode_current, (unsigned __user *)oldval) ||
  187. put_user(sizeof(int), oldlenp))
  188. return -EFAULT;
  189. }
  190. if (newval && newlen) {
  191. int new_cmode;
  192. if (newlen != sizeof(int))
  193. return -EINVAL;
  194. if (get_user(new_cmode, (int __user *)newval))
  195. return -EFAULT;
  196. return try_set_cmode(new_cmode)?:1;
  197. }
  198. return 1;
  199. }
  200. static int try_set_p0(int new_p0)
  201. {
  202. unsigned long flags, clkc;
  203. if (new_p0 < 0 || new_p0 > 1)
  204. return -EINVAL;
  205. local_irq_save(flags);
  206. __set_PSR(flags & ~PSR_ET);
  207. frv_dma_pause_all();
  208. clkc = __get_CLKC();
  209. if (new_p0)
  210. clkc |= CLKC_P0;
  211. else
  212. clkc &= ~CLKC_P0;
  213. __set_CLKC(clkc);
  214. determine_clocks(0);
  215. time_divisor_init();
  216. #ifdef DEBUG
  217. determine_clocks(1);
  218. #endif
  219. frv_dma_resume_all();
  220. local_irq_restore(flags);
  221. return 0;
  222. }
  223. static int try_set_cm(int new_cm)
  224. {
  225. unsigned long flags, clkc;
  226. if (new_cm < 0 || new_cm > 1)
  227. return -EINVAL;
  228. local_irq_save(flags);
  229. __set_PSR(flags & ~PSR_ET);
  230. frv_dma_pause_all();
  231. clkc = __get_CLKC();
  232. clkc &= ~CLKC_CM;
  233. clkc |= new_cm;
  234. __set_CLKC(clkc);
  235. determine_clocks(0);
  236. time_divisor_init();
  237. #if 1 //def DEBUG
  238. determine_clocks(1);
  239. #endif
  240. frv_dma_resume_all();
  241. local_irq_restore(flags);
  242. return 0;
  243. }
  244. static int p0_procctl(ctl_table *ctl, int write, struct file *filp,
  245. void __user *buffer, size_t *lenp, loff_t *fpos)
  246. {
  247. int new_p0;
  248. if (!write)
  249. return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
  250. new_p0 = user_atoi(buffer, *lenp);
  251. return try_set_p0(new_p0)?:*lenp;
  252. }
  253. static int p0_sysctl(ctl_table *table, int __user *name, int nlen,
  254. void __user *oldval, size_t __user *oldlenp,
  255. void __user *newval, size_t newlen, void **context)
  256. {
  257. if (oldval && oldlenp) {
  258. size_t oldlen;
  259. if (get_user(oldlen, oldlenp))
  260. return -EFAULT;
  261. if (oldlen != sizeof(int))
  262. return -EINVAL;
  263. if (put_user(clock_p0_current, (unsigned __user *)oldval) ||
  264. put_user(sizeof(int), oldlenp))
  265. return -EFAULT;
  266. }
  267. if (newval && newlen) {
  268. int new_p0;
  269. if (newlen != sizeof(int))
  270. return -EINVAL;
  271. if (get_user(new_p0, (int __user *)newval))
  272. return -EFAULT;
  273. return try_set_p0(new_p0)?:1;
  274. }
  275. return 1;
  276. }
  277. static int cm_procctl(ctl_table *ctl, int write, struct file *filp,
  278. void __user *buffer, size_t *lenp, loff_t *fpos)
  279. {
  280. int new_cm;
  281. if (!write)
  282. return proc_dointvec(ctl, write, filp, buffer, lenp, fpos);
  283. new_cm = user_atoi(buffer, *lenp);
  284. return try_set_cm(new_cm)?:*lenp;
  285. }
  286. static int cm_sysctl(ctl_table *table, int __user *name, int nlen,
  287. void __user *oldval, size_t __user *oldlenp,
  288. void __user *newval, size_t newlen, void **context)
  289. {
  290. if (oldval && oldlenp) {
  291. size_t oldlen;
  292. if (get_user(oldlen, oldlenp))
  293. return -EFAULT;
  294. if (oldlen != sizeof(int))
  295. return -EINVAL;
  296. if (put_user(clock_cm_current, (unsigned __user *)oldval) ||
  297. put_user(sizeof(int), oldlenp))
  298. return -EFAULT;
  299. }
  300. if (newval && newlen) {
  301. int new_cm;
  302. if (newlen != sizeof(int))
  303. return -EINVAL;
  304. if (get_user(new_cm, (int __user *)newval))
  305. return -EFAULT;
  306. return try_set_cm(new_cm)?:1;
  307. }
  308. return 1;
  309. }
  310. static struct ctl_table pm_table[] =
  311. {
  312. {CTL_PM_SUSPEND, "suspend", NULL, 0, 0200, NULL, &sysctl_pm_do_suspend},
  313. {CTL_PM_CMODE, "cmode", &clock_cmode_current, sizeof(int), 0644, NULL, &cmode_procctl, &cmode_sysctl, NULL},
  314. {CTL_PM_P0, "p0", &clock_p0_current, sizeof(int), 0644, NULL, &p0_procctl, &p0_sysctl, NULL},
  315. {CTL_PM_CM, "cm", &clock_cm_current, sizeof(int), 0644, NULL, &cm_procctl, &cm_sysctl, NULL},
  316. {0}
  317. };
  318. static struct ctl_table pm_dir_table[] =
  319. {
  320. {CTL_PM, "pm", NULL, 0, 0555, pm_table},
  321. {0}
  322. };
  323. /*
  324. * Initialize power interface
  325. */
  326. static int __init pm_init(void)
  327. {
  328. register_sysctl_table(pm_dir_table, 1);
  329. return 0;
  330. }
  331. __initcall(pm_init);
  332. #endif