pm.c 8.3 KB

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