pm.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. /*
  2. * Meson Power Management Routines
  3. *
  4. * Copyright (C) 2010 Amlogic, Inc. http://www.amlogic.com/
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/pm.h>
  11. #include <linux/suspend.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/clk.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/clk.h>
  17. #include <linux/fs.h>
  18. #include <asm/cacheflush.h>
  19. #include <asm/delay.h>
  20. #include <asm/uaccess.h>
  21. #include <mach/pm.h>
  22. #include <mach/am_regs.h>
  23. #include <plat/sram.h>
  24. #include <mach/power_gate.h>
  25. #include <mach/gpio.h>
  26. #include <mach/pctl.h>
  27. #include <mach/clock.h>
  28. #include <plat/regops.h>
  29. #include <plat/io.h>
  30. #ifdef CONFIG_WAKELOCK
  31. #include <linux/wakelock.h>
  32. #endif
  33. #include <mach/mod_gate.h>
  34. #ifdef CONFIG_HAS_EARLYSUSPEND
  35. #include <linux/earlysuspend.h>
  36. static struct early_suspend early_suspend;
  37. static int early_suspend_flag = 0;
  38. #endif
  39. #define ON 1
  40. #define OFF 0
  41. #include <mach/sleep.h>
  42. //#define EARLY_SUSPEND_USE_XTAL
  43. //#define MESON_SUSPEND_DEBUG
  44. static void (*meson_sram_suspend)(struct meson_pm_config *);
  45. static struct meson_pm_config *pdata;
  46. //static int mask_save_0[5];
  47. //static int mask_save_1[5];
  48. static void meson_sram_push(void *dest, void *src, unsigned int size)
  49. {
  50. int res = 0;
  51. memcpy(dest, src, size);
  52. flush_icache_range((unsigned long)dest, (unsigned long)(dest + size));
  53. res = memcmp(dest, src, size);
  54. printk("compare code in sram addr = 0x%x, size = 0x%x, result = %d", (unsigned)dest, size, res);
  55. }
  56. #define GATE_OFF(_MOD) do {power_gate_flag[GCLK_IDX_##_MOD] = IS_CLK_GATE_ON(_MOD);CLK_GATE_OFF(_MOD);} while(0)
  57. #define GATE_ON(_MOD) do {if (power_gate_flag[GCLK_IDX_##_MOD]) CLK_GATE_ON(_MOD);} while(0)
  58. #define GATE_SWITCH(flag, _MOD) do {if (flag) GATE_ON(_MOD); else GATE_OFF(_MOD);} while(0)
  59. static int power_gate_flag[GCLK_IDX_MAX];
  60. void power_init_off(void)
  61. {
  62. #if 0
  63. aml_clr_reg32_mask(P_HHI_DEMOD_CLK_CNTL, (1 << 8));
  64. aml_clr_reg32_mask(P_HHI_SATA_CLK_CNTL, (1 << 8));
  65. aml_clr_reg32_mask(P_HHI_ETH_CLK_CNTL, (1 << 8));
  66. aml_clr_reg32_mask(P_HHI_WIFI_CLK_CNTL, (1 << 0));
  67. aml_set_reg32_mask(P_HHI_DEMOD_PLL_CNTL, (1 << 15));
  68. #endif
  69. }
  70. void power_gate_switch(int flag)
  71. {
  72. //GATE_SWITCH(flag, DDR);
  73. GATE_SWITCH(flag, DOS);
  74. GATE_SWITCH(flag,MIPI_APB_CLK);
  75. GATE_SWITCH(flag,MIPI_SYS_CLK);
  76. //GATE_SWITCH(flag, AHB_BRIDGE);
  77. //GATE_SWITCH(flag, ISA);
  78. //GATE_SWITCH(flag, APB_CBUS);
  79. //GATE_SWITCH(flag, _1200XXX);
  80. GATE_SWITCH(flag, SPICC);
  81. GATE_SWITCH(flag, I2C);
  82. GATE_SWITCH(flag, SAR_ADC);
  83. GATE_SWITCH(flag, SMART_CARD_MPEG_DOMAIN);
  84. GATE_SWITCH(flag, RANDOM_NUM_GEN);
  85. GATE_SWITCH(flag, UART0);
  86. GATE_SWITCH(flag, SDHC);
  87. GATE_SWITCH(flag, STREAM);
  88. GATE_SWITCH(flag, ASYNC_FIFO);
  89. GATE_SWITCH(flag, SDIO);
  90. GATE_SWITCH(flag, AUD_BUF);
  91. //GATE_SWITCH(flag, HIU_PARSER);
  92. //GATE_SWITCH(flag, AMRISC);
  93. GATE_SWITCH(flag, BT656_IN);
  94. //GATE_SWITCH(flag, ASSIST_MISC);
  95. //GATE_SWITCH(flag, VI_CORE);
  96. GATE_SWITCH(flag, SPI2);
  97. //GATE_SWITCH(flag, MDEC_CLK_ASSIST);
  98. //GATE_SWITCH(flag, MDEC_CLK_PSC);
  99. GATE_SWITCH(flag, SPI1);
  100. //GATE_SWITCH(flag, AUD_IN);
  101. GATE_SWITCH(flag, ETHERNET);
  102. GATE_SWITCH(flag, AIU_AI_TOP_GLUE);
  103. GATE_SWITCH(flag, AIU_IEC958);
  104. GATE_SWITCH(flag, AIU_I2S_OUT);
  105. GATE_SWITCH(flag, AIU_AMCLK_MEASURE);
  106. GATE_SWITCH(flag, AIU_AIFIFO2);
  107. GATE_SWITCH(flag, AIU_AUD_MIXER);
  108. GATE_SWITCH(flag, AIU_MIXER_REG);
  109. GATE_SWITCH(flag, AIU_ADC);
  110. GATE_SWITCH(flag, BLK_MOV);
  111. //GATE_SWITCH(flag, UART1);
  112. GATE_SWITCH(flag, VGHL_PWM);
  113. GATE_SWITCH(flag, GE2D);
  114. GATE_SWITCH(flag, USB0);
  115. GATE_SWITCH(flag, USB1);
  116. //GATE_SWITCH(flag, RESET);
  117. GATE_SWITCH(flag, NAND);
  118. GATE_SWITCH(flag, HIU_PARSER_TOP);
  119. //GATE_SWITCH(flag, MDEC_CLK_DBLK);
  120. GATE_SWITCH(flag, MIPI_PHY);
  121. GATE_SWITCH(flag, VIDEO_IN);
  122. //GATE_SWITCH(flag, AHB_ARB0);
  123. GATE_SWITCH(flag, EFUSE);
  124. GATE_SWITCH(flag, ROM_CLK);
  125. //GATE_SWITCH(flag, AHB_DATA_BUS);
  126. //GATE_SWITCH(flag, AHB_CONTROL_BUS);
  127. GATE_SWITCH(flag, MISC_USB1_TO_DDR);
  128. GATE_SWITCH(flag, MISC_USB0_TO_DDR);
  129. //GATE_SWITCH(flag, AIU_PCLK);
  130. //GATE_SWITCH(flag, MMC_PCLK);
  131. GATE_SWITCH(flag, UART2);
  132. GATE_SWITCH(flag, DAC_CLK);
  133. GATE_SWITCH(flag, AIU_AOCLK);
  134. GATE_SWITCH(flag, AIU_AMCLK);
  135. GATE_SWITCH(flag, AIU_ICE958_AMCLK);
  136. GATE_SWITCH(flag, AIU_AUDIN_SCLK);
  137. GATE_SWITCH(flag, DEMUX);
  138. }
  139. EXPORT_SYMBOL(power_gate_switch);
  140. void early_power_gate_switch(int flag)
  141. {
  142. //GATE_SWITCH(flag, AMRISC);
  143. //GATE_SWITCH(flag, AUD_IN);
  144. GATE_SWITCH(flag, BLK_MOV);
  145. GATE_SWITCH(flag, VENC_I_TOP);
  146. GATE_SWITCH(flag, VENC_P_TOP);
  147. GATE_SWITCH(flag, VENC_T_TOP);
  148. GATE_SWITCH(flag, VENC_DAC);
  149. GATE_SWITCH(flag, HDMI_INTR_SYNC);
  150. GATE_SWITCH(flag, HDMI_PCLK);
  151. GATE_SWITCH(flag, MISC_DVIN);
  152. GATE_SWITCH(flag, MISC_RDMA);
  153. GATE_SWITCH(flag, VENCI_INT);
  154. GATE_SWITCH(flag, VIU2);
  155. GATE_SWITCH(flag, VENCP_INT);
  156. GATE_SWITCH(flag, VENCT_INT);
  157. GATE_SWITCH(flag, VENCL_INT);
  158. GATE_SWITCH(flag, VENC_L_TOP);
  159. GATE_SWITCH(flag, VCLK2_VENCI);
  160. GATE_SWITCH(flag, VCLK2_VENCI1);
  161. GATE_SWITCH(flag, VCLK2_VENCP);
  162. GATE_SWITCH(flag, VCLK2_VENCP1);
  163. GATE_SWITCH(flag, VCLK2_VENCT);
  164. GATE_SWITCH(flag, VCLK2_VENCT1);
  165. GATE_SWITCH(flag, VCLK2_OTHER);
  166. GATE_SWITCH(flag, VCLK2_ENCI);
  167. GATE_SWITCH(flag, VCLK2_ENCP);
  168. GATE_SWITCH(flag, VCLK1_HDMI);
  169. GATE_SWITCH(flag, ENC480P);
  170. GATE_SWITCH(flag, VCLK2_ENCT);
  171. GATE_SWITCH(flag, VCLK2_ENCL);
  172. GATE_SWITCH(flag, VCLK2_VENCL);
  173. GATE_SWITCH(flag, VCLK2_OTHER1);
  174. //GATE_SWITCH(flag, LED_PWM);
  175. //GATE_SWITCH(flag, GE2D);
  176. //GATE_SWITCH(flag, VIDEO_IN);
  177. GATE_SWITCH(flag, VI_CORE);
  178. }
  179. EXPORT_SYMBOL(early_power_gate_switch);
  180. #define CLK_COUNT 8
  181. static char clk_flag[CLK_COUNT];
  182. static unsigned clks[CLK_COUNT] = {
  183. P_HHI_ETH_CLK_CNTL,
  184. P_HHI_VID_CLK_CNTL,
  185. P_HHI_VIID_CLK_CNTL,
  186. P_HHI_AUD_CLK_CNTL,
  187. P_HHI_MALI_CLK_CNTL,
  188. P_HHI_HDMI_CLK_CNTL,
  189. P_HHI_MPEG_CLK_CNTL,
  190. P_HHI_VDEC_CLK_CNTL,
  191. };
  192. static char clks_name[CLK_COUNT][32] = {
  193. "HHI_ETH_CLK_CNTL",
  194. "HHI_VID_CLK_CNTL",
  195. "HHI_VIID_CLK_CNTL",
  196. "HHI_AUD_CLK_CNTL",
  197. "HHI_MALI_CLK_CNTL",
  198. "HHI_HDMI_CLK_CNTL",
  199. "HHI_MPEG_CLK_CNTL",
  200. "HHI_VDEC_CLK_CNTL",
  201. };
  202. #ifdef EARLY_SUSPEND_USE_XTAL
  203. #define EARLY_CLK_COUNT 3
  204. #else
  205. #define EARLY_CLK_COUNT 2
  206. #endif
  207. static char early_clk_flag[EARLY_CLK_COUNT];
  208. static unsigned early_clks[EARLY_CLK_COUNT] = {
  209. P_HHI_VID_CLK_CNTL,
  210. P_HHI_VIID_CLK_CNTL,
  211. #ifdef EARLY_SUSPEND_USE_XTAL
  212. P_HHI_MPEG_CLK_CNTL,
  213. #endif
  214. };
  215. static char early_clks_name[EARLY_CLK_COUNT][32] = {
  216. "HHI_VID_CLK_CNTL",
  217. "HHI_VIID_CLK_CNTL",
  218. #ifdef EARLY_SUSPEND_USE_XTAL
  219. "HHI_MPEG_CLK_CNTL",
  220. #endif
  221. };
  222. #if 1
  223. static void wait_uart_empty()
  224. {
  225. unsigned int count=0;
  226. do{
  227. if((aml_read_reg32(P_UART0_STATUS) & (1<<22)) == 0)
  228. udelay(4);
  229. else
  230. break;
  231. count++;
  232. }while(count<2000000);
  233. count=0;
  234. do{
  235. if((aml_read_reg32(P_UART1_STATUS) & (1<<22)) == 0)
  236. udelay(4);
  237. else
  238. break;
  239. count++;
  240. }while(count<2000000);
  241. count=0;
  242. do{
  243. if((aml_read_reg32(P_AO_UART_STATUS) & (1<<22)) == 0)
  244. udelay(4);
  245. else
  246. break;
  247. count++;
  248. }while(count<2000000);
  249. }
  250. #else
  251. static void wait_uart_empty()
  252. {
  253. do{
  254. if((aml_read_reg32(P_UART0_STATUS) & (1<<22)) == 0)
  255. udelay(4);
  256. else
  257. break;
  258. }while(1);
  259. do{
  260. if((aml_read_reg32(P_UART1_STATUS) & (1<<22)) == 0)
  261. udelay(4);
  262. else
  263. break;
  264. }while(1);
  265. do{
  266. if((aml_read_reg32(P_AO_UART_STATUS) & (1<<22)) == 0)
  267. udelay(4);
  268. else
  269. break;
  270. }while(1);
  271. }
  272. #endif
  273. static unsigned uart_rate_backup;
  274. static unsigned xtal_uart_rate_backup;
  275. void clk_switch(int flag)
  276. {
  277. int i;
  278. if (flag) {
  279. for (i = CLK_COUNT - 1; i >= 0; i--) {
  280. if (clk_flag[i]) {
  281. if ((clks[i] == P_HHI_VID_CLK_CNTL)||(clks[i] == P_HHI_VIID_CLK_CNTL)) {
  282. aml_set_reg32_bits(clks[i],clk_flag[i],19,2);
  283. } else if (clks[i] == P_HHI_MPEG_CLK_CNTL) {
  284. if(uart_rate_backup == 0){
  285. struct clk* sys_clk = clk_get_sys("clk81", NULL);
  286. sys_clk->rate = 0;
  287. uart_rate_backup = clk_get_rate(sys_clk);
  288. }
  289. wait_uart_empty();
  290. aml_set_reg32_mask(clks[i],(1<<7));//gate on pll
  291. udelay(10);
  292. aml_set_reg32_mask(clks[i],(1<<8));//switch to pll
  293. udelay(10);
  294. aml_clr_reg32_mask(P_UART0_CONTROL, (1 << 19) | 0xFFF);
  295. aml_set_reg32_mask(P_UART0_CONTROL, (((uart_rate_backup / (115200 * 4)) - 1) & 0xfff));
  296. aml_clr_reg32_mask(P_UART1_CONTROL, (1 << 19) | 0xFFF);
  297. aml_set_reg32_mask(P_UART1_CONTROL, (((uart_rate_backup / (115200 * 4)) - 1) & 0xfff));
  298. aml_clr_reg32_mask(P_AO_UART_CONTROL, (1 << 19) | 0xFFF);
  299. aml_set_reg32_bits(P_AO_UART_CONTROL, ((uart_rate_backup / (115200 * 4)) - 1) & 0xfff, 0, 12);
  300. } else {
  301. aml_set_reg32_mask(clks[i],(1<<8));
  302. }
  303. clk_flag[i] = 0;
  304. printk(KERN_INFO "clk %s(%x) on\n", clks_name[i], clks[i]);
  305. }
  306. }
  307. } else {
  308. for (i = 0; i < CLK_COUNT; i++) {
  309. if ((clks[i] == P_HHI_VID_CLK_CNTL)||(clks[i] == P_HHI_VIID_CLK_CNTL)) {
  310. clk_flag[i] = aml_get_reg32_bits(clks[i], 19, 2);
  311. if (clk_flag[i]) {
  312. aml_clr_reg32_mask(clks[i], (1<<19)|(1<<20));
  313. }
  314. } else if (clks[i] == P_HHI_MPEG_CLK_CNTL) {
  315. if (aml_read_reg32(clks[i]) & (1 << 8)) {
  316. if(xtal_uart_rate_backup == 0){//if no early suspend supported
  317. struct clk* sys_clk = clk_get_sys("xtal", NULL);
  318. xtal_uart_rate_backup = clk_get_rate(sys_clk);
  319. }
  320. wait_uart_empty();
  321. clk_flag[i] = 1;
  322. aml_clr_reg32_mask(clks[i], (1 << 8)); // 24M
  323. udelay(10);
  324. aml_clr_reg32_mask(clks[i], (1 << 7)); // 24M
  325. udelay(10);
  326. aml_clr_reg32_mask(P_UART0_CONTROL, (1 << 19) | 0xFFF);
  327. aml_set_reg32_mask(P_UART0_CONTROL, (((xtal_uart_rate_backup / (115200 * 4)) - 1) & 0xfff));
  328. aml_clr_reg32_mask(P_UART1_CONTROL, (1 << 19) | 0xFFF);
  329. aml_set_reg32_mask(P_UART1_CONTROL, (((xtal_uart_rate_backup / (115200 * 4)) - 1) & 0xfff));
  330. aml_clr_reg32_mask(P_AO_UART_CONTROL, (1 << 19) | 0xFFF);
  331. aml_set_reg32_bits(P_AO_UART_CONTROL, ((xtal_uart_rate_backup / (115200 * 4)) - 1) & 0xfff, 0, 12);
  332. }
  333. } else {
  334. clk_flag[i] = aml_get_reg32_bits(clks[i], 8, 1) ? 1 : 0;
  335. if (clk_flag[i]) {
  336. aml_clr_reg32_mask(clks[i], (1 << 8));
  337. }
  338. }
  339. if (clk_flag[i]) {
  340. printk(KERN_INFO "clk %s(%x) off\n", clks_name[i], clks[i]);
  341. wait_uart_empty();
  342. }
  343. }
  344. }
  345. }
  346. EXPORT_SYMBOL(clk_switch);
  347. void early_clk_switch(int flag)
  348. {
  349. int i;
  350. struct clk *sys_clk;
  351. if (flag) {
  352. for (i = EARLY_CLK_COUNT - 1; i >= 0; i--) {
  353. if (early_clk_flag[i]) {
  354. if ((early_clks[i] == P_HHI_VID_CLK_CNTL)||(early_clks[i] == P_HHI_VIID_CLK_CNTL)) {
  355. aml_set_reg32_bits(early_clks[i], early_clk_flag[i], 19, 2);
  356. }
  357. #ifdef EARLY_SUSPEND_USE_XTAL
  358. else if (early_clks[i] == P_HHI_MPEG_CLK_CNTL) {
  359. udelay(1000);
  360. aml_set_reg32_mask(early_clks[i], (1 << 8)); // clk81 back to normal
  361. aml_clr_reg32_mask(P_UART0_CONTROL, (1 << 19) | 0xFFF);
  362. aml_set_reg32_mask(P_UART0_CONTROL, (((uart_rate_backup / (115200 * 4)) - 1) & 0xfff));
  363. aml_clr_reg32_mask(P_UART1_CONTROL, (1 << 19) | 0xFFF);
  364. aml_set_reg32_mask(P_UART1_CONTROL, (((uart_rate_backup / (115200 * 4)) - 1) & 0xfff));
  365. aml_clr_reg32_mask(P_AO_UART_CONTROL, (1 << 19) | 0xFFF);
  366. aml_set_reg32_bits(P_AO_UART_CONTROL, ((uart_rate_backup / (115200 * 4)) - 1) & 0xfff, 0, 12);
  367. }
  368. #endif
  369. else {
  370. aml_set_reg32_mask(early_clks[i], (1 << 8));
  371. }
  372. printk(KERN_INFO "late clk %s(%x) on\n", early_clks_name[i], early_clks[i]);
  373. early_clk_flag[i] = 0;
  374. }
  375. }
  376. } else {
  377. sys_clk = clk_get_sys("clk81", NULL);
  378. sys_clk->rate = 0;
  379. uart_rate_backup = clk_get_rate(sys_clk);
  380. sys_clk = clk_get_sys("xtal", NULL);
  381. // xtal_uart_rate_backup = sys_clk->rate;
  382. xtal_uart_rate_backup = clk_get_rate(sys_clk);
  383. for (i = 0; i < EARLY_CLK_COUNT; i++) {
  384. if ((early_clks[i] == P_HHI_VID_CLK_CNTL)||(early_clks[i] == P_HHI_VIID_CLK_CNTL)) {
  385. early_clk_flag[i] = aml_get_reg32_bits(early_clks[i], 19, 2);
  386. if (early_clk_flag[i]) {
  387. aml_clr_reg32_mask(early_clks[i], (1<<19)|(1<<20));
  388. }
  389. }
  390. #ifdef EARLY_SUSPEND_USE_XTAL
  391. else if (early_clks[i] == P_HHI_MPEG_CLK_CNTL) {
  392. early_clk_flag[i] = 1;
  393. udelay(1000);
  394. aml_clr_reg32_mask(early_clks[i], (1 << 8)); // 24M
  395. aml_clr_reg32_mask(P_UART0_CONTROL, (1 << 19) | 0xFFF);
  396. aml_set_reg32_mask(P_UART0_CONTROL, (((xtal_uart_rate_backup / (115200 * 4)) - 1) & 0xfff));
  397. aml_clr_reg32_mask(P_UART1_CONTROL, (1 << 19) | 0xFFF);
  398. aml_set_reg32_mask(P_UART1_CONTROL, (((xtal_uart_rate_backup / (115200 * 4)) - 1) & 0xfff));
  399. aml_clr_reg32_mask(P_AO_UART_CONTROL, (1 << 19) | 0xFFF);
  400. aml_set_reg32_bits(P_AO_UART_CONTROL, ((xtal_uart_rate_backup / (115200 * 4)) - 1) & 0xfff, 0, 12);
  401. }
  402. #endif
  403. else {
  404. early_clk_flag[i] = aml_get_reg32_bits(early_clks[i], 8, 1) ? 1 : 0;
  405. if (early_clk_flag[i]) {
  406. aml_clr_reg32_mask(early_clks[i], (1 << 8));
  407. }
  408. }
  409. if (early_clk_flag[i]) {
  410. printk(KERN_INFO "early clk %s(%x) off\n", early_clks_name[i], early_clks[i]);
  411. }
  412. }
  413. }
  414. }
  415. EXPORT_SYMBOL(early_clk_switch);
  416. #define PLL_COUNT 3
  417. static char pll_flag[PLL_COUNT];
  418. static unsigned plls[PLL_COUNT] = {
  419. P_HHI_VID_PLL_CNTL,
  420. P_HHI_VIID_PLL_CNTL,
  421. // P_HHI_AUD_PLL_CNTL,
  422. P_HHI_MPLL_CNTL,
  423. };
  424. static char plls_name[PLL_COUNT][32] = {
  425. "HHI_VID_PLL_CNTL",
  426. "HHI_VIID_PLL_CNTL",
  427. // "HHI_AUD_PLL_CNTL",
  428. "HHI_MPLL_CNTL",
  429. };
  430. #define EARLY_PLL_COUNT 2
  431. static char early_pll_flag[EARLY_PLL_COUNT];
  432. static unsigned early_pll_settings[EARLY_PLL_COUNT][4];
  433. static unsigned early_plls[EARLY_PLL_COUNT] = {
  434. P_HHI_VID_PLL_CNTL,
  435. P_HHI_VIID_PLL_CNTL,
  436. };
  437. static char early_plls_name[EARLY_PLL_COUNT][32] = {
  438. "HHI_VID_PLL_CNTL",
  439. "HHI_VIID_PLL_CNTL",
  440. };
  441. /*
  442. void pll_switch(int flag)
  443. {
  444. int i;
  445. if (flag) {
  446. for (i = PLL_COUNT - 1; i >= 0; i--) {
  447. if (pll_flag[i]) {
  448. if(default_console_loglevel >= 7){
  449. printk(KERN_INFO "pll %s(%x) on\n", plls_name[i], plls[i]);
  450. udelay(2000);
  451. udelay(2000);
  452. }
  453. if ((plls[i]==P_HHI_VID_PLL_CNTL)||(plls[i]==P_HHI_VIID_PLL_CNTL)||(plls[i]==P_HHI_MPLL_CNTL)){
  454. aml_clr_reg32_mask(plls[i],(1<<30));
  455. pll_flag[i] = 0;
  456. }
  457. else{
  458. aml_clr_reg32_mask(plls[i],(1<<15));//bit15 PD:power down
  459. pll_flag[i] = 0;
  460. }
  461. udelay(10);
  462. }
  463. }
  464. udelay(1000);
  465. } else {
  466. for (i = 0; i < PLL_COUNT; i++) {
  467. if ((plls[i]==P_HHI_VID_PLL_CNTL)||(plls[i]==P_HHI_VIID_PLL_CNTL)||(plls[i]==P_HHI_MPLL_CNTL))
  468. pll_flag[i]=aml_get_reg32_bits(plls[i],30,1) ? 0:1;
  469. else
  470. pll_flag[i]=aml_get_reg32_bits(plls[i],15,1) ? 0:1;
  471. if (pll_flag[i]) {
  472. printk(KERN_INFO "pll %s(%x) off\n", plls_name[i], plls[i]);
  473. if ((plls[i]==P_HHI_VID_PLL_CNTL)||(plls[i]==P_HHI_VIID_PLL_CNTL)){
  474. aml_set_reg32_mask(plls[i],(1<<30));
  475. }
  476. else{
  477. aml_set_reg32_mask(plls[i],(1<<15));
  478. }
  479. }
  480. }
  481. }
  482. }
  483. EXPORT_SYMBOL(pll_switch);
  484. */
  485. void early_pll_switch(int flag)//for MX only
  486. {
  487. int i;
  488. if (flag) {
  489. for (i = EARLY_PLL_COUNT - 1; i >= 0; i--) {
  490. if (early_pll_flag[i]) {
  491. early_pll_flag[i] = 0;
  492. if (early_plls[i]==P_HHI_VID_PLL_CNTL)
  493. {
  494. do{
  495. aml_write_reg32(P_HHI_VID_PLL_CNTL,1<<29);
  496. aml_write_reg32(P_HHI_VID_PLL_CNTL2,0x814d3928);
  497. aml_write_reg32(P_HHI_VID_PLL_CNTL3,0x6d425012);
  498. aml_write_reg32(P_HHI_VID_PLL_CNTL4,0x110);
  499. aml_write_reg32(P_HHI_VID_PLL_CNTL,(early_pll_settings[i][0] & ~(1<<30))|1<<29);
  500. aml_write_reg32(P_HHI_VID_PLL_CNTL,early_pll_settings[i][0] & ~(3<<30));
  501. udelay(1000);
  502. }while((aml_read_reg32(P_HHI_VID_PLL_CNTL) & 1<<31) == 0);
  503. }
  504. else if(early_plls[i]==P_HHI_VIID_PLL_CNTL)
  505. {
  506. do{
  507. aml_write_reg32(P_HHI_VIID_PLL_CNTL,1<<29);
  508. aml_write_reg32(P_HHI_VIID_PLL_CNTL2,0x814d3928);
  509. aml_write_reg32(P_HHI_VIID_PLL_CNTL3,0x6d425012);
  510. aml_write_reg32(P_HHI_VIID_PLL_CNTL4,0x110);
  511. aml_write_reg32(P_HHI_VIID_PLL_CNTL,(early_pll_settings[i][0] & ~(1<<30))|1<<29);
  512. aml_write_reg32(P_HHI_VIID_PLL_CNTL,early_pll_settings[i][0] & ~(3<<30));
  513. udelay(1000);
  514. }while((aml_read_reg32(P_HHI_VIID_PLL_CNTL) & 1<<31) == 0);
  515. }
  516. else{
  517. printk("Error: not restore pll setting!!!\n");
  518. }
  519. printk(KERN_INFO "late pll %s(%x) on\n", early_plls_name[i], early_plls[i]);
  520. }
  521. }
  522. udelay(1000);
  523. } else {
  524. for (i = 0; i < EARLY_PLL_COUNT; i++) {
  525. if (early_plls[i]==P_HHI_VID_PLL_CNTL)
  526. {
  527. early_pll_flag[i] = aml_get_reg32_bits(early_plls[i],30,1) ? 0 : 1;
  528. early_pll_settings[i][0]=aml_read_reg32(P_HHI_VID_PLL_CNTL);
  529. early_pll_settings[i][1]=aml_read_reg32(P_HHI_VID_PLL_CNTL2);
  530. early_pll_settings[i][2]=aml_read_reg32(P_HHI_VID_PLL_CNTL3);
  531. early_pll_settings[i][3]=aml_read_reg32(P_HHI_VID_PLL_CNTL4);
  532. }
  533. else if(early_plls[i]==P_HHI_VIID_PLL_CNTL)
  534. {
  535. early_pll_flag[i] = aml_get_reg32_bits(early_plls[i],30,1) ? 0 : 1;
  536. early_pll_settings[i][0]=aml_read_reg32(P_HHI_VIID_PLL_CNTL);
  537. early_pll_settings[i][1]=aml_read_reg32(P_HHI_VIID_PLL_CNTL2);
  538. early_pll_settings[i][2]=aml_read_reg32(P_HHI_VIID_PLL_CNTL3);
  539. early_pll_settings[i][3]=aml_read_reg32(P_HHI_VIID_PLL_CNTL4);
  540. }
  541. else
  542. printk("Error: not store pll setting!\n");
  543. if (early_pll_flag[i]) {
  544. printk(KERN_INFO "early pll %s(%x) off\n", early_plls_name[i], early_plls[i]);
  545. if ((early_plls[i]==P_HHI_VID_PLL_CNTL)||(early_plls[i]==P_HHI_VIID_PLL_CNTL))
  546. aml_set_reg32_mask(early_plls[i], (1 << 30));
  547. }
  548. }
  549. }
  550. }
  551. EXPORT_SYMBOL(early_pll_switch);
  552. typedef struct {
  553. char name[32];
  554. unsigned reg_addr;
  555. unsigned set_bits;
  556. unsigned clear_bits;
  557. unsigned reg_value;
  558. unsigned enable; // 1:cbus 2:apb 3:ahb 0:disable
  559. } analog_t;
  560. #define ANALOG_COUNT 2
  561. static analog_t analog_regs[ANALOG_COUNT] = {
  562. {"SAR_ADC", P_SAR_ADC_REG3, 1 << 28, (1 << 30) | (1 << 21), 0, 1},
  563. #ifdef ADJUST_CORE_VOLTAGE
  564. {"LED_PWM_REG0", P_LED_PWM_REG0, 1 << 13, 1 << 12, 0, 0}, // needed for core voltage adjustment, so not off
  565. #else
  566. {"LED_PWM_REG0", P_LED_PWM_REG0, 1 << 13, 1 << 12, 0, 1},
  567. #endif
  568. //{"VGHL_PWM_REG0", P_VGHL_PWM_REG0, 1 << 13, 1 << 12, 0, 1},
  569. };
  570. void analog_switch(int flag)
  571. {
  572. int i;
  573. unsigned reg_value = 0;
  574. if (flag) {
  575. printk(KERN_INFO "analog on\n");
  576. aml_set_reg32_mask(P_AM_ANALOG_TOP_REG0, 1 << 1); // set 0x206e bit[1] 1 to power on top analog
  577. for (i = 0; i < ANALOG_COUNT; i++) {
  578. if (analog_regs[i].enable && (analog_regs[i].set_bits || analog_regs[i].clear_bits)) {
  579. if (analog_regs[i].enable == 1) {
  580. aml_write_reg32(analog_regs[i].reg_addr, analog_regs[i].reg_value);
  581. } else if (analog_regs[i].enable == 2) {
  582. aml_write_reg32(analog_regs[i].reg_addr, analog_regs[i].reg_value);
  583. } else if (analog_regs[i].enable == 3) {
  584. aml_write_reg32(analog_regs[i].reg_addr, analog_regs[i].reg_value);
  585. }
  586. }
  587. }
  588. } else {
  589. printk(KERN_INFO "analog off\n");
  590. for (i = 0; i < ANALOG_COUNT; i++) {
  591. if (analog_regs[i].enable && (analog_regs[i].set_bits || analog_regs[i].clear_bits)) {
  592. if (analog_regs[i].enable == 1) {
  593. analog_regs[i].reg_value = aml_read_reg32(analog_regs[i].reg_addr);
  594. printk("%s(0x%x):0x%x", analog_regs[i].name, analog_regs[i].reg_addr, analog_regs[i].reg_value);
  595. if (analog_regs[i].clear_bits) {
  596. aml_clr_reg32_mask(analog_regs[i].reg_addr, analog_regs[i].clear_bits);
  597. printk(" & ~0x%x", analog_regs[i].clear_bits);
  598. }
  599. if (analog_regs[i].set_bits) {
  600. aml_set_reg32_mask(analog_regs[i].reg_addr, analog_regs[i].set_bits);
  601. printk(" | 0x%x", analog_regs[i].set_bits);
  602. }
  603. reg_value = aml_read_reg32(analog_regs[i].reg_addr);
  604. printk(" = 0x%x\n", reg_value);
  605. } else if (analog_regs[i].enable == 2) {
  606. analog_regs[i].reg_value = aml_read_reg32(analog_regs[i].reg_addr);
  607. printk("%s(0x%x):0x%x", analog_regs[i].name, analog_regs[i].reg_addr, analog_regs[i].reg_value);
  608. if (analog_regs[i].clear_bits) {
  609. aml_clr_reg32_mask(analog_regs[i].reg_addr, analog_regs[i].clear_bits);
  610. printk(" & ~0x%x", analog_regs[i].clear_bits);
  611. }
  612. if (analog_regs[i].set_bits) {
  613. aml_set_reg32_mask(analog_regs[i].reg_addr, analog_regs[i].set_bits);
  614. printk(" | 0x%x", analog_regs[i].set_bits);
  615. }
  616. reg_value = aml_read_reg32(analog_regs[i].reg_addr);
  617. printk(" = 0x%x\n", reg_value);
  618. } else if (analog_regs[i].enable == 3) {
  619. analog_regs[i].reg_value = aml_read_reg32(analog_regs[i].reg_addr);
  620. printk("%s(0x%x):0x%x", analog_regs[i].name, analog_regs[i].reg_addr, analog_regs[i].reg_value);
  621. if (analog_regs[i].clear_bits) {
  622. aml_clr_reg32_mask(analog_regs[i].reg_addr, analog_regs[i].clear_bits);
  623. printk(" & ~0x%x", analog_regs[i].clear_bits);
  624. }
  625. if (analog_regs[i].set_bits) {
  626. aml_set_reg32_mask(analog_regs[i].reg_addr, analog_regs[i].set_bits);
  627. printk(" | 0x%x", analog_regs[i].set_bits);
  628. }
  629. reg_value = aml_read_reg32(analog_regs[i].reg_addr);
  630. printk(" = 0x%x\n", reg_value);
  631. }
  632. }
  633. }
  634. aml_clr_reg32_mask(P_AM_ANALOG_TOP_REG0, 1 << 1); // set 0x206e bit[1] 0 to shutdown top analog
  635. }
  636. }
  637. /*
  638. void usb_switch(int is_on, int ctrl)
  639. {
  640. int index, por;
  641. if (ctrl == 0) {
  642. index = USB_CTL_INDEX_A;
  643. } else {
  644. index = USB_CTL_INDEX_B;
  645. }
  646. if (is_on) {
  647. por = USB_CTL_POR_ON;
  648. } else {
  649. por = USB_CTL_POR_OFF;
  650. }
  651. set_usb_ctl_por(index, por);
  652. }
  653. */
  654. #ifdef CONFIG_HAS_EARLYSUSPEND
  655. static void meson_system_early_suspend(struct early_suspend *h)
  656. {
  657. if (!early_suspend_flag) {
  658. printk(KERN_INFO "sys_suspend\n");
  659. if (pdata->set_exgpio_early_suspend) {
  660. pdata->set_exgpio_early_suspend(OFF);
  661. }
  662. early_clk_switch(OFF);
  663. early_pll_switch(OFF);
  664. early_power_gate_switch(OFF);
  665. early_suspend_flag = 1;
  666. }
  667. }
  668. static void meson_system_late_resume(struct early_suspend *h)
  669. {
  670. if (early_suspend_flag) {
  671. early_power_gate_switch(ON);
  672. early_pll_switch(ON);
  673. early_clk_switch(ON);
  674. early_suspend_flag = 0;
  675. if (pdata->set_exgpio_early_suspend) {
  676. pdata->set_exgpio_early_suspend(ON);
  677. }
  678. printk(KERN_INFO "sys_resume\n");
  679. }
  680. #ifdef CONFIG_SUSPEND_WATCHDOG
  681. extern void reset_watchdog(void);
  682. reset_watchdog();
  683. #endif
  684. }
  685. #endif
  686. #ifdef CONFIG_SCREEN_ON_EARLY
  687. void vout_pll_resume_early(void)
  688. {
  689. #ifdef CONFIG_HAS_EARLYSUSPEND
  690. if (early_suspend_flag){
  691. early_power_gate_switch(ON);
  692. early_clk_switch(ON);
  693. early_suspend_flag = 0;
  694. if(pdata->set_exgpio_early_suspend){
  695. pdata->set_exgpio_early_suspend(ON);
  696. }
  697. printk(KERN_INFO "sys_resume\n");
  698. }
  699. #endif
  700. return;
  701. }
  702. EXPORT_SYMBOL(vout_pll_resume_early);
  703. #endif
  704. #define MODE_DELAYED_WAKE 0
  705. #define MODE_IRQ_DELAYED_WAKE 1
  706. #define MODE_IRQ_ONLY_WAKE 2
  707. static void auto_clk_gating_setup(
  708. unsigned long sleep_dly_tb, unsigned long mode, unsigned long clear_fiq, unsigned long clear_irq,
  709. unsigned long start_delay, unsigned long clock_gate_dly, unsigned long sleep_time, unsigned long enable_delay)
  710. {
  711. }
  712. static void meson_pm_suspend(void)
  713. {
  714. unsigned ddr_clk_N;
  715. #ifdef ADJUST_CORE_VOLTAGE
  716. unsigned vcck_backup = aml_get_reg32_bits(P_LED_PWM_REG0, 0, 4);
  717. printk(KERN_INFO "current vcck is 0x%x!\n", vcck_backup);
  718. #endif
  719. printk(KERN_INFO "enter meson_pm_suspend!\n");
  720. #ifdef CONFIG_SUSPEND_WATCHDOG
  721. extern void enable_watchdog(void);
  722. enable_watchdog();
  723. #endif
  724. // Disable MMC_LP_CTRL. Will be re-enabled at resume by kreboot.S
  725. //pr_debug("MMC_LP_CTRL1 before=%#x\n", aml_read_reg32(P_MMC_LP_CTRL1));
  726. //aml_write_reg32(P_MMC_LP_CTRL1, 0x60a80000);
  727. //pr_debug("MMC_LP_CTRL1 after=%#x\n", aml_read_reg32(P_MMC_LP_CTRL1));
  728. //done in bsp, early suspend
  729. pdata->ddr_clk = aml_read_reg32(P_HHI_DDR_PLL_CNTL);
  730. ddr_clk_N = (pdata->ddr_clk >> 9) & 0x1f;
  731. ddr_clk_N = ddr_clk_N * 4; // N*4
  732. if (ddr_clk_N > 0x1f) {
  733. ddr_clk_N = 0x1f;
  734. }
  735. pdata->ddr_clk &= ~(0x1f << 9);
  736. pdata->ddr_clk |= ddr_clk_N << 9;
  737. printk(KERN_INFO "target ddr clock 0x%x!\n", pdata->ddr_clk);
  738. analog_switch(OFF);
  739. //usb_switch(OFF, 0);
  740. //usb_switch(OFF, 1);
  741. if (pdata->set_vccx2) {
  742. pdata->set_vccx2(OFF);
  743. }
  744. clk_switch(OFF);
  745. // pll_switch(OFF);
  746. power_gate_switch(OFF);
  747. switch_mod_gate_by_type(MOD_MEDIA_CPU, 1);
  748. #ifndef CONFIG_MESON_SUSPEND
  749. printk("meson_sram_suspend params 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n",
  750. (unsigned)pdata->pctl_reg_base, (unsigned)pdata->mmc_reg_base, (unsigned)pdata->hiu_reg_base,
  751. (unsigned)pdata->power_key, (unsigned)pdata->ddr_clk, (unsigned)pdata->ddr_reg_backup);
  752. meson_sram_push(meson_sram_suspend, meson_cpu_suspend,
  753. meson_cpu_suspend_sz);
  754. #endif
  755. printk(KERN_INFO "sleep ...\n");
  756. #ifndef CONFIG_MESON_SUSPEND
  757. auto_clk_gating_setup(2, // select 100uS timebase
  758. MODE_IRQ_ONLY_WAKE, // Set interrupt wakeup only
  759. 0, // don't clear the FIQ global mask
  760. 0, // don't clear the IRQ global mask
  761. 1, // 1us start delay
  762. 1, // 1uS gate delay
  763. 1, // Set the delay wakeup time (1mS)
  764. 1); // 1uS enable delay
  765. #endif
  766. //switch A9 clock to xtal 24MHz
  767. aml_clr_reg32_mask(P_HHI_SYS_CPU_CLK_CNTL, 1 << 7);
  768. aml_set_reg32_mask(P_HHI_SYS_PLL_CNTL, 1 << 30);//power down sys pll
  769. #ifdef ADJUST_CORE_VOLTAGE
  770. aml_set_reg32_bits(P_LED_PWM_REG0,0,0,4);
  771. #endif
  772. #if 0
  773. //while ((READ_AOBUS_REG(AO_RTC_ADDR1) >> 2) & 1){
  774. while ((aml_read_reg32(AO_RTC_ADDR1) >> 2) & 1){
  775. udelay(10);
  776. }
  777. #else
  778. #ifdef CONFIG_MESON_SUSPEND
  779. extern int meson_power_suspend();
  780. meson_power_suspend();
  781. #else
  782. /**
  783. * @todo you should not enable irq with a directly register operation
  784. * Please replace it with setup_irq .
  785. */
  786. aml_set_reg32_mask(P_SYS_CPU_0_IRQ_IN2_INTR_MASK, pdata->power_key); //enable rtc interrupt only
  787. meson_sram_suspend(pdata);
  788. #endif
  789. #endif
  790. #ifdef ADJUST_CORE_VOLTAGE
  791. aml_set_reg32_bits(P_LED_PWM_REG0,vcck_backup, 0, 4);
  792. udelay(100);
  793. #endif
  794. aml_clr_reg32_mask(P_HHI_SYS_PLL_CNTL, (1 << 30)); //turn on sys pll
  795. printk(KERN_INFO "... wake up\n");
  796. if ((*(volatile unsigned *)(P_AO_RTC_ADDR1)) & (1<<12)) {
  797. // Woke from alarm, not power button. Set flag to inform key_input driver.
  798. WRITE_AOBUS_REG(AO_RTI_STATUS_REG2, 0x12345678);
  799. }
  800. // clear RTC interrupt
  801. *(volatile unsigned *)(P_AO_RTC_ADDR1)=(*(volatile unsigned *)(P_AO_RTC_ADDR1))|(0xf000);
  802. printk(KERN_INFO "RTCADD3=0x%x\n",*(volatile unsigned *)(P_AO_RTC_ADDR3));
  803. if((*(volatile unsigned *)(P_AO_RTC_ADDR3))|(1<<29))
  804. {
  805. *(volatile unsigned *)(P_AO_RTC_ADDR3)=(*(volatile unsigned *)(P_AO_RTC_ADDR3))&(~(1<<29));
  806. udelay(1000);
  807. }
  808. printk(KERN_INFO "RTCADD3=0x%x\n",*(volatile unsigned *)P_AO_RTC_ADDR3);
  809. if (pdata->set_vccx2) {
  810. pdata->set_vccx2(ON);
  811. }
  812. wait_uart_empty();
  813. aml_set_reg32_mask(P_HHI_SYS_CPU_CLK_CNTL , (1 << 7)); //a9 use pll
  814. switch_mod_gate_by_type(MOD_MEDIA_CPU, 0);
  815. power_gate_switch(ON);
  816. // pll_switch(ON);
  817. clk_switch(ON);
  818. //usb_switch(ON, 0);
  819. //usb_switch(ON, 1);
  820. analog_switch(ON);
  821. }
  822. static int meson_pm_prepare(void)
  823. {
  824. printk(KERN_INFO "enter meson_pm_prepare!\n");
  825. #if 0
  826. mask_save_0[0] = aml_read_reg32(P_SYS_CPU_0_IRQ_IN0_INTR_MASK);
  827. mask_save_0[1] = aml_read_reg32(P_SYS_CPU_0_IRQ_IN1_INTR_MASK);
  828. mask_save_0[2] = aml_read_reg32(P_SYS_CPU_0_IRQ_IN2_INTR_MASK);
  829. mask_save_0[3] = aml_read_reg32(P_SYS_CPU_0_IRQ_IN3_INTR_MASK);
  830. mask_save_0[4] = aml_read_reg32(P_SYS_CPU_0_IRQ_IN4_INTR_MASK);
  831. mask_save_1[0] = aml_read_reg32(P_SYS_CPU_1_IRQ_IN0_INTR_MASK);
  832. mask_save_1[1] = aml_read_reg32(P_SYS_CPU_1_IRQ_IN1_INTR_MASK);
  833. mask_save_1[2] = aml_read_reg32(P_SYS_CPU_1_IRQ_IN2_INTR_MASK);
  834. mask_save_1[3] = aml_read_reg32(P_SYS_CPU_1_IRQ_IN3_INTR_MASK);
  835. mask_save_1[4] = aml_read_reg32(P_SYS_CPU_1_IRQ_IN4_INTR_MASK);
  836. aml_write_reg32(P_SYS_CPU_0_IRQ_IN0_INTR_MASK, 0x0);
  837. aml_write_reg32(P_SYS_CPU_0_IRQ_IN1_INTR_MASK, 0x0);
  838. aml_write_reg32(P_SYS_CPU_0_IRQ_IN2_INTR_MASK, 0x0);
  839. aml_write_reg32(P_SYS_CPU_0_IRQ_IN3_INTR_MASK, 0x0);
  840. aml_write_reg32(P_SYS_CPU_0_IRQ_IN4_INTR_MASK, 0x0);
  841. aml_write_reg32(P_SYS_CPU_1_IRQ_IN0_INTR_MASK, 0x0);
  842. aml_write_reg32(P_SYS_CPU_1_IRQ_IN1_INTR_MASK, 0x0);
  843. aml_write_reg32(P_SYS_CPU_1_IRQ_IN2_INTR_MASK, 0x0);
  844. aml_write_reg32(P_SYS_CPU_1_IRQ_IN3_INTR_MASK, 0x0);
  845. aml_write_reg32(P_SYS_CPU_1_IRQ_IN4_INTR_MASK, 0x0);
  846. #endif
  847. #ifndef CONFIG_MESON_SUSPEND
  848. meson_sram_push(meson_sram_suspend, meson_cpu_suspend,
  849. meson_cpu_suspend_sz);
  850. #endif
  851. return 0;
  852. }
  853. static int meson_pm_enter(suspend_state_t state)
  854. {
  855. int ret = 0;
  856. switch (state) {
  857. case PM_SUSPEND_STANDBY:
  858. case PM_SUSPEND_MEM:
  859. meson_pm_suspend();
  860. break;
  861. default:
  862. ret = -EINVAL;
  863. }
  864. return ret;
  865. }
  866. static void meson_pm_finish(void)
  867. {
  868. printk(KERN_INFO "enter meson_pm_finish!\n");
  869. #if 0
  870. aml_write_reg32(P_SYS_CPU_0_IRQ_IN0_INTR_MASK, mask_save_0[0]);
  871. aml_write_reg32(P_SYS_CPU_0_IRQ_IN1_INTR_MASK, mask_save_0[1]);
  872. aml_write_reg32(P_SYS_CPU_0_IRQ_IN2_INTR_MASK, mask_save_0[2]);
  873. aml_write_reg32(P_SYS_CPU_0_IRQ_IN3_INTR_MASK, mask_save_0[3]);
  874. aml_write_reg32(P_SYS_CPU_0_IRQ_IN4_INTR_MASK, mask_save_0[4]);
  875. aml_write_reg32(P_SYS_CPU_1_IRQ_IN0_INTR_MASK, mask_save_1[0]);
  876. aml_write_reg32(P_SYS_CPU_1_IRQ_IN1_INTR_MASK, mask_save_1[1]);
  877. aml_write_reg32(P_SYS_CPU_1_IRQ_IN2_INTR_MASK, mask_save_1[2]);
  878. aml_write_reg32(P_SYS_CPU_1_IRQ_IN3_INTR_MASK, mask_save_1[3]);
  879. aml_write_reg32(P_SYS_CPU_1_IRQ_IN4_INTR_MASK, mask_save_1[4]);
  880. #endif
  881. #ifdef CONFIG_MESON_SUSPEND
  882. #ifdef MESON_SUSPEND_DEBUG // only use this when debug without android rootfs
  883. #ifdef CONFIG_EARLYSUSPEND
  884. extern void request_suspend_state(suspend_state_t new_state);
  885. request_suspend_state(0);
  886. #else
  887. extern int enter_state(suspend_state_t state);
  888. enter_state(0);
  889. #endif
  890. #endif
  891. #endif
  892. }
  893. static struct platform_suspend_ops meson_pm_ops = {
  894. .enter = meson_pm_enter,
  895. .prepare = meson_pm_prepare,
  896. .finish = meson_pm_finish,
  897. .valid = suspend_valid_only_mem,
  898. };
  899. static power_off_unused_pll(void)
  900. {
  901. aml_write_reg32(P_HHI_MPLL_CNTL7, 0x01082000); //turn off mp0
  902. aml_write_reg32(P_HHI_MPLL_CNTL8, 0x01082000); //turn off mp1
  903. }
  904. static int __init meson_pm_probe(struct platform_device *pdev)
  905. {
  906. printk(KERN_INFO "enter meson_pm_probe!\n");
  907. power_init_off();
  908. power_off_unused_pll();
  909. #ifdef CONFIG_HAS_EARLYSUSPEND
  910. early_suspend.level = EARLY_SUSPEND_LEVEL_DISABLE_FB;
  911. early_suspend.suspend = meson_system_early_suspend;
  912. early_suspend.resume = meson_system_late_resume;
  913. // early_suspend.param = pdev;
  914. register_early_suspend(&early_suspend);
  915. #endif
  916. pdata = pdev->dev.platform_data;
  917. if (!pdata) {
  918. dev_err(&pdev->dev, "cannot get platform data\n");
  919. return -ENOENT;
  920. }
  921. #ifndef CONFIG_MESON_SUSPEND
  922. pdata->ddr_reg_backup = sram_alloc(32 * 4);
  923. if (!pdata->ddr_reg_backup) {
  924. dev_err(&pdev->dev, "cannot allocate SRAM memory\n");
  925. return -ENOMEM;
  926. }
  927. meson_sram_suspend = sram_alloc(meson_cpu_suspend_sz);
  928. if (!meson_sram_suspend) {
  929. dev_err(&pdev->dev, "cannot allocate SRAM memory\n");
  930. return -ENOMEM;
  931. }
  932. meson_sram_push(meson_sram_suspend, meson_cpu_suspend,
  933. meson_cpu_suspend_sz);
  934. #endif
  935. suspend_set_ops(&meson_pm_ops);
  936. #ifndef CONFIG_MESON_SUSPEND
  937. printk(KERN_INFO "meson_pm_probe done 0x%x %d!\n", (unsigned)meson_sram_suspend, meson_cpu_suspend_sz);
  938. #else
  939. printk(KERN_INFO "meson_pm_probe done !\n");
  940. #endif
  941. return 0;
  942. }
  943. static int __exit meson_pm_remove(struct platform_device *pdev)
  944. {
  945. #ifdef CONFIG_HAS_EARLYSUSPEND
  946. unregister_early_suspend(&early_suspend);
  947. #endif
  948. return 0;
  949. }
  950. static struct platform_driver meson_pm_driver = {
  951. .driver = {
  952. .name = "pm-meson",
  953. .owner = THIS_MODULE,
  954. },
  955. .remove = __exit_p(meson_pm_remove),
  956. };
  957. static int __init meson_pm_init(void)
  958. {
  959. return platform_driver_probe(&meson_pm_driver, meson_pm_probe);
  960. }
  961. late_initcall(meson_pm_init);