dummy_ap.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. */
  10. /* MediaTek Inc. (C) 2016. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. */
  31. //#define CTP_ENV
  32. #ifndef CTP_ENV
  33. /* Note: Pleae enable DUMMY_AP option at rule.mk if hope to use this function */
  34. #include <platform/boot_mode.h>
  35. #include <debug.h>
  36. #include <dev/uart.h>
  37. #include <platform/mtk_key.h>
  38. #include <target/cust_key.h>
  39. #include <platform/mt_gpio.h>
  40. #include <sys/types.h>
  41. #include <debug.h>
  42. #include <err.h>
  43. #include <reg.h>
  44. #include <string.h>
  45. #include <platform/mt_typedefs.h>
  46. #include <platform/mt_reg_base.h>
  47. #include <platform/mt_irq.h>
  48. #include <platform/mt_pmic.h>
  49. #include <platform/timer.h>
  50. #include <sys/types.h>
  51. #include <arch/ops.h>
  52. #include <platform/mt_pmic.h>
  53. #include <platform/upmu_common.h>
  54. #include <platform/upmu_hw.h>
  55. #include <platform/spm.h>
  56. #else
  57. /*CTP environment*/
  58. #include <gpio.h>
  59. #include <barriers.h>
  60. #include <sync_write.h>
  61. #include <upmu_hw.h>
  62. #include <mt_spm_reg.h>
  63. #include <mt_spm_mtcmos.h>
  64. #include <efuse.h>
  65. #define dprintf(CRITICAL, fmt, args...) dbg_print("[MD]: "fmt, ##args)
  66. static void let_md_go(int md_id);
  67. static void config_md_boot_env(int md_id, int boot_mode);
  68. #endif /*CTP_ENV*/
  69. //------- feature option part ---------------------------------------
  70. //#define DEFAULT_META
  71. #define ENABLE_MD_RESET_SPM
  72. //#define ENABLE_MD_RESET_RGU
  73. //#define IGNORE_MD_WDT
  74. //#define IGNORE_MD1_WDT
  75. //#define IGNORE_MD2_WDT
  76. //#define NO_UNGATE_MD
  77. //------- enum and macro part ---------------------------------------
  78. enum {
  79. MD_SYS1 = 0,
  80. MD_SYS2,
  81. MD_SYS3,
  82. MD_SYS4,
  83. };
  84. enum {
  85. AP_ONLY = -1,
  86. MD1_ONLY = 0,
  87. MD2_ONLY,
  88. MD1_MD2,
  89. };
  90. #define ccci_write32(b, a, v) DRV_WriteReg32((b)+(a), (v))
  91. #define ccci_read32(b, a) DRV_Reg32((b)+(a))
  92. #define ccci_write16(b, a, v) DRV_WriteReg16((b)+(a), (v))
  93. #define ccci_read16(b, a) DRV_Reg16((b)+(a))
  94. #define ccci_write8(b, a, v) DRV_WriteReg8((b)+(a), (v))
  95. #define ccci_read8(b, a) DRV_Reg8((b)+(a))
  96. struct sram_cfg {
  97. unsigned int offset;
  98. unsigned int start_bit;
  99. unsigned int end_bit;
  100. };
  101. //------- IRQ ID part ---------------------------------------
  102. #define GIC_PRIVATE_SIGNALS (32)
  103. #define MT_MD_WDT1_IRQ_ID (205+GIC_PRIVATE_SIGNALS)
  104. //------- register part ---------------------------------------
  105. #define MD1_BUS_PROTECT_SET (0x100012A0)
  106. #define MD1_BUS_PROTECT_CLR (0x100012A4)
  107. #define MD1_BUS_PROTECT_STA (0x10001228)
  108. #define MD1_BUS_PROTECT1_SET (0x100012A8)
  109. #define MD1_BUS_PROTECT1_CLR (0x100012AC)
  110. #define MD1_BUS_PROTECT1_STA (0x10001258)
  111. #define PROTECTION_BITMASK ((0x1 << 3)|(0x1 << 4)|(0x1 << 7)) // bit 3,4,7
  112. #define PROTECTION1_BITMASK (0x1 << 6) // bit 6
  113. #define MD1_BOOT_VECTOR_EN (0x20000024)
  114. #define MD1_META_FLAG (0x20000010)
  115. #define TOPRGU_BASE (0x10007000)
  116. #define TOP_RGU_WDT_MODE (0x0)
  117. #define TOP_RGU_WDT_SWRST (0x14)
  118. #define TOP_RGU_WDT_SWSYSRST (0x18)
  119. #define TOP_RGU_WDT_NONRST_REG (0x20)
  120. #define TOP_RGU_LATCH_CONTROL (0x44)
  121. #define MD1_SYS (1 << 7)
  122. #define UNLOCK_KEY (0x88000000)
  123. #define MD_USB_INTERRUPT_MUX 0x10000280
  124. // MD RGU PCore
  125. #define BASE_ADDR_MDRSTCTL 0x200F0000
  126. #define MD_RGU_BASE (BASE_ADDR_MDRSTCTL + 0x100)
  127. #define WDT_MD_MODE (0x0)
  128. #define WDT_MD_MODE_KEY (0x55000030)
  129. #define PLL_TYPE (volatile kal_uint32 *)
  130. // AP view
  131. #define BASE_MADDR_APMIXEDSYS (0x1000C000)
  132. #define BASE_MADDR_MDTOP_PLLMIXED (0x20140000)
  133. #define BASE_MADDR_MDTOP_CLKSW (0x20150000)
  134. #define REG_APMIXEDSYS_AP_PLL_CON0 (PLL_TYPE(BASE_MADDR_APMIXEDSYS+0x0))
  135. #define REG_MDTOP_PLLMIXED_PLL_VERSION (PLL_TYPE(BASE_MADDR_MDTOP_PLLMIXED))
  136. #define REG_MDTOP_PLLMIXED_DCXO_PLL_SETTLE_CTL (PLL_TYPE(BASE_MADDR_MDTOP_PLLMIXED+0x4))
  137. #define REG_MDTOP_PLLMIXED_PLL_ON_CTL (PLL_TYPE(BASE_MADDR_MDTOP_PLLMIXED+0x10))
  138. #define REG_MDTOP_PLLMIXED_MDMCUPLL_CTL0 (PLL_TYPE(BASE_MADDR_MDTOP_PLLMIXED+0x40))
  139. #define REG_MDTOP_PLLMIXED_MDVDSPPLL_CTL0 (PLL_TYPE(BASE_MADDR_MDTOP_PLLMIXED+0x48))
  140. #define REG_MDTOP_PLLMIXED_MDBRPPLL_CTL0 (PLL_TYPE(BASE_MADDR_MDTOP_PLLMIXED+0x50))
  141. #define REG_MDTOP_PLLMIXED_MDTXPLL_CTL0 (PLL_TYPE(BASE_MADDR_MDTOP_PLLMIXED+0x58))
  142. #define REG_MDTOP_PLLMIXED_MDBPIPLL_CTL0 (PLL_TYPE(BASE_MADDR_MDTOP_PLLMIXED+0x60))
  143. #define REG_MDTOP_PLLMIXED_MDBPIPLL_CTL1 (PLL_TYPE(BASE_MADDR_MDTOP_PLLMIXED+0x64))
  144. #define REG_MDTOP_PLLMIXED_MDPLL_CTL0 (PLL_TYPE(BASE_MADDR_MDTOP_PLLMIXED+0x100))
  145. #define REG_MDTOP_PLLMIXED_MDPLL_CTL1 (PLL_TYPE(BASE_MADDR_MDTOP_PLLMIXED+0x104))
  146. #define REG_MDTOP_PLLMIXED_PLL_HP_RDY_IRQ (PLL_TYPE(BASE_MADDR_MDTOP_PLLMIXED+0x314))
  147. #define REG_MDTOP_PLLMIXED_PLL_HP_RDY_IRQ_MASK (PLL_TYPE(BASE_MADDR_MDTOP_PLLMIXED+0x318))
  148. #define REG_MDTOP_PLLMIXED_MDMCUPLL_STS (PLL_TYPE(BASE_MADDR_MDTOP_PLLMIXED+0xC00))
  149. #define REG_MDTOP_PLLMIXED_PLL_DUMMY (PLL_TYPE(BASE_MADDR_MDTOP_PLLMIXED+0xF00))
  150. #define REG_MDTOP_CLKSW_CLKON_CTL (PLL_TYPE(BASE_MADDR_MDTOP_CLKSW+0x20))
  151. #define REG_MDTOP_CLKSW_CLKSEL_CTL (PLL_TYPE(BASE_MADDR_MDTOP_CLKSW+0x24))
  152. #define REG_MDTOP_CLKSW_SDF_CK_CTL (PLL_TYPE(BASE_MADDR_MDTOP_CLKSW+0x28))
  153. #define REG_MDTOP_CLKSW_MDSYS_BUS_FLEXCKGEN_STS (PLL_TYPE(BASE_MADDR_MDTOP_CLKSW+0x84))
  154. #define MD_PLL_MAGIC_NUM 0x62930000
  155. #define APMIXED_BASE (BASE_MADDR_APMIXEDSYS)
  156. #define AP_PLL_CON0 (0x0)
  157. #define AP_PLL_CON1 (0x4)
  158. #define MDPLL_CON0 (0x3A0)
  159. #define MDPLL_CON3 (0x3AC)
  160. #define UINT32P (volatile unsigned int *)
  161. #define SLEEP_BASE (0x10006000)
  162. #define POWERON_CONFIG_EN (UINT32P (SLEEP_BASE+0x0))
  163. #define SPM_POWER_ON_VAL1 (UINT32P (SLEEP_BASE+0x8))
  164. #define INFRACFG_AO_BASE (0x10001000)
  165. #define INFRA_TOPAXI_PROTECTEN_1_SET (0x2A8)
  166. #define INFRA_TOPAXI_PROTECTEN_1_CLR (0x2AC)
  167. #define INFRA_MISC2 (0xF0C)
  168. //------- GPIO part ---------------------------------------
  169. #define GPIO_MAGIC (0x80000000)
  170. // -- UART
  171. #define NO_NEED_UART_CONFIG
  172. #ifndef NO_NEED_UART_CONFIG
  173. #define UART1_TX_GPIO_ID (GPIO96 | GPIO_MAGIC)
  174. #define UART1_RX_GPIO_ID (GPIO95 | GPIO_MAGIC)
  175. #define UART2_TX_GPIO_ID (GPIO21 | GPIO_MAGIC)
  176. #define UART2_RX_GPIO_ID (GPIO20 | GPIO_MAGIC)
  177. #define UART3_TX_GPIO_ID (GPIO151 | GPIO_MAGIC)
  178. #define UART3_RX_GPIO_ID (GPIO150 | GPIO_MAGIC)
  179. #define UART4_TX_GPIO_ID (GPIO28 | GPIO_MAGIC)
  180. #define UART4_RX_GPIO_ID (GPIO27 | GPIO_MAGIC)
  181. #endif
  182. //------- code part ---------------------------------------
  183. static unsigned int img_load_flag = 0;
  184. #ifndef CTP_ENV
  185. extern BOOT_ARGUMENT *g_boot_arg;
  186. extern char *ld_md_errno_to_str(int errno);
  187. static int meta_detection(void)
  188. {
  189. int boot_mode;
  190. #ifdef DEFAULT_META
  191. boot_mode = 1;
  192. return boot_mode;
  193. #endif
  194. boot_mode = 0;
  195. if (g_boot_arg->boot_mode != NORMAL_BOOT)
  196. boot_mode = 1;
  197. dprintf(CRITICAL, "Meta mode: %d, boot_mode: %d\n", boot_mode, g_boot_arg->boot_mode);
  198. return boot_mode;
  199. }
  200. void md_wdt_init(void)
  201. {
  202. if (img_load_flag & (1 << MD_SYS1)) {
  203. mt_irq_set_sens(MT_MD_WDT1_IRQ_ID, MT65xx_EDGE_SENSITIVE);
  204. mt_irq_set_polarity(MT_MD_WDT1_IRQ_ID, MT65xx_POLARITY_LOW);
  205. mt_irq_unmask(MT_MD_WDT1_IRQ_ID);
  206. }
  207. }
  208. #endif //#ifndef CTP_ENV
  209. void md_uart_config(int type_id, int boot_mode)
  210. {
  211. #ifndef NO_NEED_UART_CONFIG
  212. switch (type_id) {
  213. case AP_ONLY: // for AP only
  214. dprintf(CRITICAL, "md_uart_config:%d, UART1->AP_0, UART2->N/A, UART3->MD1_0, UART4->N/A\n", type_id);
  215. // same as dws initial setting
  216. mt_set_gpio_mode(UART1_TX_GPIO_ID, GPIO_MODE_01);
  217. mt_set_gpio_mode(UART1_RX_GPIO_ID, GPIO_MODE_01);
  218. mt_set_gpio_mode(UART3_TX_GPIO_ID, GPIO_MODE_06);
  219. mt_set_gpio_mode(UART3_RX_GPIO_ID, GPIO_MODE_06);
  220. break;
  221. case MD1_ONLY: // for AP & MD1
  222. case MD2_ONLY: // for AP & C2K
  223. case MD1_MD2: // for both MD1 and C2K
  224. if (boot_mode) {
  225. dprintf(CRITICAL, "md_uart_config:%d, UART3->MD1_0, UART1->AP_0, UART2->NA/A, UART4->N/A\n", type_id);
  226. mt_set_gpio_mode(UART1_TX_GPIO_ID, GPIO_MODE_01);
  227. mt_set_gpio_mode(UART1_RX_GPIO_ID, GPIO_MODE_01);
  228. mt_set_gpio_mode(UART3_TX_GPIO_ID, GPIO_MODE_05);
  229. mt_set_gpio_mode(UART3_RX_GPIO_ID, GPIO_MODE_05);
  230. }
  231. break;
  232. default:
  233. break;
  234. }
  235. #endif
  236. }
  237. void bus_protection_en(int md_id)
  238. {
  239. if (md_id == MD_SYS1) {
  240. /* enable protection for MD1 */
  241. dprintf(CRITICAL, "enable protection for md\n");
  242. DRV_WriteReg32(MD1_BUS_PROTECT_SET, PROTECTION_BITMASK);
  243. DRV_WriteReg32(MD1_BUS_PROTECT1_SET, PROTECTION1_BITMASK);
  244. /* poll protection ready */
  245. dprintf(CRITICAL, "wait protection ....\n");
  246. while ((DRV_Reg32(MD1_BUS_PROTECT_STA)&PROTECTION_BITMASK) != PROTECTION_BITMASK) {
  247. dprintf(CRITICAL, "0x%x\n", DRV_Reg32(MD1_BUS_PROTECT_STA));
  248. }
  249. dprintf(CRITICAL, "wait protection1 ....\n");
  250. while ((DRV_Reg32(MD1_BUS_PROTECT1_STA)&PROTECTION1_BITMASK) != PROTECTION1_BITMASK) {
  251. dprintf(CRITICAL, "0x%x\n", DRV_Reg32(MD1_BUS_PROTECT1_STA));
  252. }
  253. dprintf(CRITICAL, "protection enable done\n");
  254. return;
  255. }
  256. }
  257. void bus_protection_diable(int md_id)
  258. {
  259. if (md_id == MD_SYS1) {
  260. /* enable protection for MD1 */
  261. dprintf(CRITICAL, "disable protection for md\n");
  262. DRV_WriteReg32(MD1_BUS_PROTECT_CLR, PROTECTION_BITMASK);
  263. DRV_WriteReg32(MD1_BUS_PROTECT1_CLR, PROTECTION1_BITMASK);
  264. /* poll protection ready */
  265. dprintf(CRITICAL, "wait protection disable....\n");
  266. while ((DRV_Reg32(MD1_BUS_PROTECT_STA)&PROTECTION_BITMASK) != 0x00000000) {
  267. dprintf(CRITICAL, "0x%x\n", DRV_Reg32(MD1_BUS_PROTECT_STA));
  268. }
  269. dprintf(CRITICAL, "wait protection1 disable....\n");
  270. while ((DRV_Reg32(MD1_BUS_PROTECT1_STA)&PROTECTION1_BITMASK) != 0x00000000) {
  271. dprintf(CRITICAL, "0x%x\n", DRV_Reg32(MD1_BUS_PROTECT1_STA));
  272. }
  273. dprintf(CRITICAL, "protection disable done\n");
  274. return;
  275. }
  276. }
  277. #define INFRA_AO_BASE_LOCAL 0x10001000
  278. #define INFRA_AP2MD_DUMMY_REG 0x370
  279. #define INFRA_AP2MD_DUMMY_BIT 0
  280. #define INFRA_MD2PERI_PROT_EN 0x250
  281. #define INFRA_MD2PERI_PROT_RDY 0x258
  282. #define INFRA_MD2PERI_PROT_SET (0x2A8)
  283. #define INFRA_MD2PERI_PROT_CLR (0x2AC)
  284. #define INFRA_MD2PERI_PROT_BIT 6
  285. #define INFRA_PERI2MD_PROT_EN 0x220
  286. #define INFRA_PERI2MD_PROT_RDY 0x228
  287. #define INFRA_PERI2MD_PROT_SET (0x2A0)
  288. #define INFRA_PERI2MD_PROT_CLR (0x2A4)
  289. #define INFRA_PERI2MD_PROT_BIT 7
  290. static void md1_pre_access_md_reg(void)
  291. {
  292. unsigned int reg_val;
  293. unsigned int infra_ao_base = INFRA_AO_BASE_LOCAL;
  294. /*clear dummy reg flag to access modem reg*/
  295. reg_val = ccci_read32(infra_ao_base, INFRA_AP2MD_DUMMY_REG);
  296. reg_val &= (~(0x1 << INFRA_AP2MD_DUMMY_BIT));
  297. ccci_write32(infra_ao_base, INFRA_AP2MD_DUMMY_REG, reg_val);
  298. dprintf(CRITICAL, "pre: ap2md dummy reg 0x%X: 0x%X\n", INFRA_AO_BASE_LOCAL + INFRA_AP2MD_DUMMY_REG,
  299. ccci_read32(infra_ao_base, INFRA_AP2MD_DUMMY_REG));
  300. /*disable MD to AP*/
  301. ccci_write32(infra_ao_base, INFRA_MD2PERI_PROT_SET, (0x1 << INFRA_MD2PERI_PROT_BIT));
  302. while ((ccci_read32(infra_ao_base, INFRA_MD2PERI_PROT_RDY) & (0x1 << INFRA_MD2PERI_PROT_BIT))
  303. != (0x1 << INFRA_MD2PERI_PROT_BIT))
  304. ;
  305. dprintf(CRITICAL, "md2peri: en[0x%X], rdy[0x%X]\n",
  306. ccci_read32(infra_ao_base, INFRA_MD2PERI_PROT_EN),
  307. ccci_read32(infra_ao_base, INFRA_MD2PERI_PROT_RDY));
  308. }
  309. static void md1_post_access_md_reg(void)
  310. {
  311. unsigned int reg_val;
  312. unsigned int infra_ao_base = INFRA_AO_BASE_LOCAL;
  313. /*disable AP to MD*/
  314. ccci_write32(infra_ao_base, INFRA_PERI2MD_PROT_SET, (0x1 << INFRA_PERI2MD_PROT_BIT));
  315. while ((ccci_read32(infra_ao_base, INFRA_PERI2MD_PROT_RDY) & (0x1 << INFRA_PERI2MD_PROT_BIT))
  316. != (0x1 << INFRA_PERI2MD_PROT_BIT))
  317. ;
  318. dprintf(CRITICAL, "peri2md: en[0x%X], rdy[0x%X]\n",
  319. ccci_read32(infra_ao_base, INFRA_PERI2MD_PROT_EN),
  320. ccci_read32(infra_ao_base, INFRA_PERI2MD_PROT_RDY));
  321. /*enable MD to AP*/
  322. ccci_write32(infra_ao_base, INFRA_MD2PERI_PROT_CLR, (0x1 << INFRA_MD2PERI_PROT_BIT));
  323. while ((ccci_read32(infra_ao_base, INFRA_MD2PERI_PROT_RDY) & (0x1 << INFRA_MD2PERI_PROT_BIT)))
  324. ;
  325. dprintf(CRITICAL, "md2peri: en[0x%X], rdy[0x%X]\n",
  326. ccci_read32(infra_ao_base, INFRA_MD2PERI_PROT_EN),
  327. ccci_read32(infra_ao_base, INFRA_MD2PERI_PROT_RDY));
  328. /*set dummy reg flag and let md access AP*/
  329. reg_val = ccci_read32(infra_ao_base, INFRA_AP2MD_DUMMY_REG);
  330. reg_val |= (0x1 << INFRA_AP2MD_DUMMY_BIT);
  331. ccci_write32(infra_ao_base, INFRA_AP2MD_DUMMY_REG, reg_val);
  332. dprintf(CRITICAL, "post: ap2md dummy reg 0x%X: 0x%X\n", INFRA_AO_BASE_LOCAL + INFRA_AP2MD_DUMMY_REG,
  333. ccci_read32(infra_ao_base, INFRA_AP2MD_DUMMY_REG));
  334. }
  335. void pmic_init_sequence(void)
  336. {
  337. dprintf(CRITICAL, "pmic_init_sequence skipped!\n");
  338. }
  339. static void md1_pmic_setting(void)
  340. {
  341. // assume set in preloader
  342. dprintf(CRITICAL, "md1_pmic_setting skipped!\n");
  343. }
  344. int md_common_setting()
  345. {
  346. unsigned int reg_value;
  347. // MD srcclkena setting: [7:4]=4'h0010, [3:0]=4'h0001
  348. reg_value = ccci_read32(INFRACFG_AO_BASE, INFRA_MISC2);
  349. reg_value &= ~(0xFF);
  350. reg_value |= 0x21;
  351. ccci_write32(INFRACFG_AO_BASE, INFRA_MISC2, reg_value);
  352. dprintf(CRITICAL, "MD srcclkena setting:0x%x\n", ccci_read32(INFRACFG_AO_BASE, INFRA_MISC2));
  353. // clear MSDC0 interrupt as it's also connected to MD side
  354. //ccci_write32(0x1124000C, 0, 0xFFFFFFFF);
  355. pmic_init_sequence();
  356. return 0;
  357. }
  358. void md_common_pll_init(void)
  359. {
  360. // initial CLKSQ_LPF
  361. ccci_write32(APMIXED_BASE, AP_PLL_CON0, ccci_read32(APMIXED_BASE, AP_PLL_CON0) | (0x1 << 1));
  362. udelay(100);
  363. }
  364. void md1_pll_init(void)
  365. {
  366. md_common_pll_init();
  367. dprintf(CRITICAL, "Read MD PLL version:0x%x(@0x%x)\n", *REG_MDTOP_PLLMIXED_PLL_VERSION, (unsigned int)REG_MDTOP_PLLMIXED_PLL_VERSION);
  368. // Default md_srclkena_ack settle time = 136T 32K
  369. *REG_MDTOP_PLLMIXED_DCXO_PLL_SETTLE_CTL = 0x02020E88;
  370. *REG_MDTOP_PLLMIXED_MDBPIPLL_CTL0 = 0x801713B1; // fixed 600MHz(/4), 343MHz(/7), 267MHz(/9) /* Fvco = 2400M */
  371. *REG_MDTOP_PLLMIXED_MDTXPLL_CTL0 = 0x80171400; // 300MHz /* Fvco = 2400M */
  372. *REG_MDTOP_PLLMIXED_MDBRPPLL_CTL0 = 0x80229E00; // 400MHz /* Fvco = 3600M */
  373. *REG_MDTOP_PLLMIXED_MDVDSPPLL_CTL0 = 0x80204E00; // 672MHz /* Fvco = 3360M */
  374. *REG_MDTOP_PLLMIXED_MDMCUPLL_CTL0 = 0x80213C00; // 864MHz /* Fvco = 3456M */
  375. /*
  376. * Polling until MDMCUPLL complete frequency adjustment
  377. * Once MDMCUPLL complete, other PLL should complete too
  378. */
  379. while ((*REG_MDTOP_PLLMIXED_MDMCUPLL_STS >> 14) & 0x1) {};
  380. /* Default disable BPI /7 clock */
  381. *REG_MDTOP_PLLMIXED_MDBPIPLL_CTL1 = *REG_MDTOP_PLLMIXED_MDBPIPLL_CTL1 & (~(0x80));
  382. /*TINFO="MDSYS_INIT: Update ABB MDPLL control register default value"*/
  383. /* *REG_MDTOP_PLLMIXED_MDPLL_CTL1 = 0x4C43100; */
  384. /* Force TXPLL ON due to TOPSM couldn't enable this PLL by default, TXPLL would be disable after DVFS Init.
  385. other PLL ON controlled by HW" */
  386. *REG_MDTOP_PLLMIXED_PLL_ON_CTL = 0x100010;
  387. /*
  388. * Wait MD bus clock ready
  389. * Once MD bus ready, other clock should be ready too
  390. * In FPGA, the following status checking must be removed since there is no flex ck gen in FPGA.
  391. */
  392. while ((*REG_MDTOP_CLKSW_MDSYS_BUS_FLEXCKGEN_STS & 0x8000) != 0x8000) {};
  393. // Switch MDMCU & MD BUS clock to PLL frequency
  394. *REG_MDTOP_CLKSW_CLKSEL_CTL |= 0x3;
  395. // Switch all clock to PLL frequency
  396. *REG_MDTOP_CLKSW_CLKSEL_CTL |= 0x58103FC;
  397. // Switch SDF clock to PLL frequency
  398. *REG_MDTOP_CLKSW_SDF_CK_CTL |= 0x10;
  399. // Turn off all SW clock request, except ATB
  400. *REG_MDTOP_CLKSW_CLKON_CTL = 0x1;
  401. // Clear PLL ADJ RDY IRQ fired by initial period adjustment
  402. *REG_MDTOP_PLLMIXED_PLL_HP_RDY_IRQ = 0xFFFF;
  403. // Mask all PLL ADJ RDY IRQ
  404. *REG_MDTOP_PLLMIXED_PLL_HP_RDY_IRQ_MASK = 0xFFFF;
  405. /* Make a record that means MD pll has been initialized. */
  406. /* Note: We use "MD_PLL_MAGIC_NUM|0x1" to know PLL init in MD C code.
  407. If AP init PLL, it would be "MD_PLL_MAGIC_NUM". */
  408. *REG_MDTOP_PLLMIXED_PLL_DUMMY = (MD_PLL_MAGIC_NUM);
  409. dprintf(CRITICAL, "md1_pll_init done!\n");
  410. }
  411. void md1_boot(int boot_mode)
  412. {
  413. unsigned int reg_value;
  414. // step 1: configure modem related buck
  415. md1_pmic_setting();
  416. dprintf(CRITICAL, "Spare3 0x11C5004C val:0x%x\n", *((volatile unsigned int*)0x11C5004C) );
  417. // step 2: Power on MTCMOS
  418. spm_mtcmos_ctrl_md1(STA_POWER_ON);
  419. dprintf(CRITICAL, "MD1 MTCMOS power on done!\n");
  420. md1_pre_access_md_reg();
  421. // step 5: configure md_srclkena setting
  422. // INFRA_MISC2 already finished in md_common_setting()
  423. // SRCLKEN_O1 force on
  424. spm_write(POWERON_CONFIG_EN, 0x0B160001);
  425. spm_write(SPM_POWER_ON_VAL1, 0x00215830);
  426. dprintf(CRITICAL, "md_srclkena done!\n");
  427. // step 6: configure PLL setting
  428. md1_pll_init();
  429. // step 7: disabel MD WDT
  430. #if !defined(ENABLE_MD_RESET_SPM) && !defined(ENABLE_MD_RESET_RGU)
  431. ccci_write32(MD_RGU_BASE, WDT_MD_MODE, WDT_MD_MODE_KEY); // disable MD WDT & MD AUX_WDT
  432. #endif
  433. // step+: set META Register
  434. if (boot_mode) {
  435. reg_value = DRV_Reg32(MD1_META_FLAG);
  436. DRV_WriteReg32(MD1_META_FLAG, (reg_value | 0x1));
  437. }
  438. }
  439. static void config_md_boot_env(int md_id, int boot_mode)
  440. {
  441. switch (md_id) {
  442. case MD_SYS1:
  443. md1_boot(boot_mode);
  444. break;
  445. default:
  446. break;
  447. }
  448. }
  449. static void let_md_go(int md_id)
  450. {
  451. switch (md_id) {
  452. case MD_SYS1:
  453. /* step 8: trigger modem SW to run */
  454. #ifndef NO_UNGATE_MD
  455. ccci_write32(MD1_BOOT_VECTOR_EN, 0, 1);
  456. md1_post_access_md_reg();
  457. #else
  458. md1_post_access_md_reg();
  459. dprintf(CRITICAL, "do not let MD1 go\n");
  460. #endif
  461. break;
  462. default:
  463. break;
  464. }
  465. }
  466. void md_wdt_irq_handler(unsigned int irq)
  467. {
  468. //spm_mtcmos_ctrl_audio(STA_POWER_DOWN);
  469. //dprintf(CRITICAL, "turn off audio.\n");
  470. #if defined(ENABLE_MD_RESET_SPM) || defined(ENABLE_MD_RESET_RGU)
  471. unsigned int reg_value = 0;
  472. unsigned int cnt = ccci_read32(TOPRGU_BASE, TOP_RGU_WDT_NONRST_REG);
  473. // update counter
  474. ccci_write32(TOPRGU_BASE, TOP_RGU_WDT_NONRST_REG, cnt + 1);
  475. // reset UART config
  476. md_uart_config(AP_ONLY, 0);
  477. dprintf(CRITICAL, "\n\n\n\nCurrent wdt cnt:%d\n", cnt + 1);
  478. if (irq == MT_MD_WDT1_IRQ_ID) {
  479. #ifdef ENABLE_MD_RESET_SPM
  480. dprintf(CRITICAL, "MD1 power off\n");
  481. spm_mtcmos_ctrl_md1(STA_POWER_DOWN);
  482. mdelay(5);
  483. config_md_boot_env(MD_SYS1, 0);
  484. #endif
  485. #ifdef ENABLE_MD_RESET_RGU
  486. dprintf(CRITICAL, "MD1 reset\n");
  487. bus_protection_en(0);
  488. ccci_write32(TOPRGU_BASE, TOP_RGU_WDT_SWSYSRST,
  489. (ccci_read32(TOPRGU_BASE, TOP_RGU_WDT_SWSYSRST) | UNLOCK_KEY) | MD1_SYS);
  490. mdelay(5);
  491. ccci_write32(TOPRGU_BASE, TOP_RGU_WDT_SWSYSRST,
  492. (ccci_read32(TOPRGU_BASE, TOP_RGU_WDT_SWSYSRST) | UNLOCK_KEY) & (~MD1_SYS));
  493. bus_protection_diable(0);
  494. #endif
  495. let_md_go(MD_SYS1);
  496. }
  497. #if 1
  498. dprintf(CRITICAL, "Config UART after MD WDT! %d\n", cnt+1);
  499. if ((img_load_flag&((1 << MD_SYS1) | (1 << MD_SYS3))) == ((1 << MD_SYS1) | (1 << MD_SYS3))) {
  500. md_uart_config(MD1_MD2, 0);
  501. } else if (img_load_flag & (1 << MD_SYS1)) {
  502. md_uart_config(MD1_ONLY, 0);
  503. } else if (img_load_flag & (1 << MD_SYS3)) {
  504. md_uart_config(MD2_ONLY, 0);
  505. }
  506. #endif
  507. #else
  508. md_uart_config(AP_ONLY, 0);
  509. dprintf(CRITICAL, "Get MD WDT irq, STA:%x!!\n", ccci_read32(MD_RGU_BASE, 0xC));
  510. #ifdef IGNORE_MD_WDT
  511. dprintf(CRITICAL, "ignore MD WDT\n");
  512. #else
  513. dprintf(CRITICAL, "whole system reboot\n");
  514. ccci_write32(TOPRGU_BASE, TOP_RGU_LATCH_CONTROL, 0x95000000);
  515. ccci_write32(TOPRGU_BASE, TOP_RGU_WDT_MODE, 0x22000004);
  516. ccci_write32(TOPRGU_BASE, TOP_RGU_WDT_SWRST, 0x1209);
  517. while (1);
  518. #endif
  519. #endif
  520. //spm_mtcmos_ctrl_audio(STA_POWER_ON);
  521. //dprintf(CRITICAL, "turn on audio.\n");
  522. }
  523. int dummy_ap_irq_helper(unsigned int irq)
  524. {
  525. switch (irq) {
  526. case MT_MD_WDT1_IRQ_ID:
  527. if (img_load_flag &(1 << MD_SYS1)) {
  528. #ifndef IGNORE_MD1_WDT
  529. md_wdt_irq_handler(MT_MD_WDT1_IRQ_ID);
  530. #else
  531. dprintf(CRITICAL, "ignore MD1 WDT\n");
  532. #endif
  533. }
  534. return 1;
  535. default:
  536. break;
  537. }
  538. return 0;
  539. }
  540. void dummy_ap_boot_up_md(int md_ld_flag)
  541. {
  542. int boot_mode = 0;
  543. int i;
  544. int ret;
  545. img_load_flag = (unsigned int)md_ld_flag;
  546. // reinit UART, overwrite DWS setting
  547. md_uart_config(AP_ONLY, 0);
  548. // Disable AP WDT
  549. *(volatile unsigned int *)(TOPRGU_BASE) = 0x22000000;
  550. dprintf(CRITICAL, "Welcome to use dummy AP!\n");
  551. dprintf(CRITICAL, "load flag for dummy AP: %x\n", img_load_flag);
  552. #ifndef CTP_ENV
  553. if (img_load_flag == 0) {
  554. dprintf(CRITICAL, "no MD loaded for dummy AP\n");
  555. ret = get_md_err_from_lk_info(MD_SYS1);
  556. dprintf(CRITICAL, "hint for MD1 errno: %x, %s\n", ret, ld_md_errno_to_str(-ret));
  557. ret = get_md_err_from_lk_info(MD_SYS3);
  558. dprintf(CRITICAL, "hint for MD3 errno: %x, %s\n", ret, ld_md_errno_to_str(-ret));
  559. dprintf(CRITICAL, "stop.....\n");
  560. while (1);
  561. }
  562. if (img_load_flag & (1 << MD_SYS1)) {
  563. dprintf(CRITICAL, "MD1 loaded");
  564. ret = get_md_err_from_lk_info(MD_SYS1);
  565. if (ret < 0) {
  566. dprintf(CRITICAL, "MD1 load image has error, errno:%s", ld_md_errno_to_str(-ret));
  567. while (1);
  568. }
  569. }
  570. // 2, Check boot Mode
  571. boot_mode = meta_detection();
  572. dprintf(CRITICAL, "Get boot mode is %d\n", boot_mode);
  573. // 3, MD WDT ISR init
  574. dprintf(CRITICAL, "Init MD WDT\n");
  575. md_wdt_init();
  576. #endif
  577. // 4. Common setting for all MD
  578. md_common_setting();
  579. // 5. Setup per-MD env before boot up MD
  580. for (i=0; i<3; i++) {
  581. if (img_load_flag & (1 << i)) {
  582. dprintf(CRITICAL, "MD%d Enabled\n", i+1);
  583. config_md_boot_env(i, boot_mode);
  584. }
  585. }
  586. // 6. Switch UART
  587. dprintf(CRITICAL, "Switch UART!\n");
  588. if ((img_load_flag&((1 << MD_SYS1) | (1 << MD_SYS3))) == ((1 << MD_SYS1) | (1 << MD_SYS3))) {
  589. md_uart_config(MD1_MD2, boot_mode);
  590. } else if (img_load_flag & (1 << MD_SYS1)) {
  591. md_uart_config(MD1_ONLY, boot_mode);
  592. } else if (img_load_flag & (1 << MD_SYS3)) {
  593. md_uart_config(MD2_ONLY, boot_mode);
  594. }
  595. for (i=0; i<3; i++) {
  596. if (img_load_flag & (1 << i)) {
  597. dprintf(CRITICAL, "Trigger MD%d run\n", i+1);
  598. let_md_go(i);
  599. }
  600. }
  601. #ifndef CTP_ENV
  602. dprintf(CRITICAL, "enter while(1), ^O^!!!!!!!!!\n");
  603. while (1);
  604. #endif
  605. }