pm.c 6.2 KB

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