devapc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/interrupt.h>
  4. #include <linux/device.h>
  5. #include <linux/platform_device.h>
  6. #include <linux/mm.h>
  7. #include <linux/uaccess.h>
  8. #include <linux/slab.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/irq.h>
  11. #include <linux/sched.h>
  12. #include <linux/cdev.h>
  13. #include <linux/init.h>
  14. #include <linux/fs.h>
  15. #include <linux/device.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/of_address.h>
  20. #include <linux/types.h>
  21. #ifdef CONFIG_MTK_HIBERNATION
  22. #include <mtk_hibernate_dpm.h>
  23. #include <mach/irqs.h>
  24. #endif
  25. #ifdef CONFIG_MTK_CLKMGR
  26. /* mt_clkmgr */
  27. #include <mach/mt_clkmgr.h>
  28. #else
  29. /* CCF */
  30. #include <linux/clk.h>
  31. #endif
  32. #include "mt_device_apc.h"
  33. #include "mt_io.h"
  34. #include "sync_write.h"
  35. #include <mach/mt_secure_api.h>
  36. #include "devapc.h"
  37. /* Debug message event */
  38. #define DEVAPC_LOG_NONE 0x00000000
  39. #define DEVAPC_LOG_ERR 0x00000001
  40. #define DEVAPC_LOG_WARN 0x00000002
  41. #define DEVAPC_LOG_INFO 0x00000004
  42. #define DEVAPC_LOG_DBG 0x00000008
  43. #define DEVAPC_LOG_LEVEL (DEVAPC_LOG_ERR)
  44. #define DEVAPC_MSG(fmt, args...) \
  45. do { \
  46. if (DEVAPC_LOG_LEVEL & DEVAPC_LOG_DBG) { \
  47. pr_debug(fmt, ##args); \
  48. } else if (DEVAPC_LOG_LEVEL & DEVAPC_LOG_INFO) { \
  49. pr_info(fmt, ##args); \
  50. } else if (DEVAPC_LOG_LEVEL & DEVAPC_LOG_WARN) { \
  51. pr_warn(fmt, ##args); \
  52. } else if (DEVAPC_LOG_LEVEL & DEVAPC_LOG_ERR) { \
  53. pr_err(fmt, ##args); \
  54. } \
  55. } while (0)
  56. #ifndef CONFIG_MTK_CLKMGR
  57. /* CCF */
  58. static struct clk *dapc_clk;
  59. #endif
  60. static struct cdev *g_devapc_ctrl;
  61. static unsigned int devapc_irq;
  62. static void __iomem *devapc_ao_base;
  63. static void __iomem *devapc_pd_base;
  64. #if defined(CONFIG_ARCH_MT6735)
  65. static struct DEVICE_INFO devapc_devices[] = {
  66. /*0*/
  67. {"INFRA_AO_INFRASYS_CONFIG_REGS"},
  68. {"INFRA_AO_PMIC_WRAP_CONTROL_REG"},
  69. {"INFRA_AO_PERISYS_CONFIG_REGS"},
  70. {"INFRA_AO_KPAD_CONTROL_REG"},
  71. {"INFRA_AO_GPT"},
  72. {"INFRA_AO_APMCU_EINT_CONTROLLER"},
  73. {"INFRA_AO_TOP_LEVEL_SLP_MANAGER"},
  74. {"INFRA_AO_DEVICE_APC_AO"},
  75. {"INFRA_AO_SEJ"},
  76. {"INFRA_AO_RSVD"},
  77. /*10*/
  78. {"INFRA_AO_CLDMA_AO_TOP_AP"},
  79. {"INFRA_AO_CLDMA_AO_TOP_MD"},
  80. {"INFRASYS_MCUSYS_CONFIG_REG"},
  81. {"INFRASYS_CONTROL_REG"},
  82. {"INFRASYS_BOOTROM/SRAM"},
  83. {"INFRASYS_EMI_BUS_INTERFACE"},
  84. {"INFRASYS_SYSTEM_CIRQ"},
  85. {"INFRASYS_MM_IOMMU_CONFIGURATION"},
  86. {"INFRASYS_EFUSEC"},
  87. {"INFRASYS_DEVICE_APC_MONITOR"},
  88. /*20*/
  89. {"INFRASYS_MCU_BIU_CONFIGURATION"},
  90. {"INFRASYS_AP_MIXED_CONTROL_REG"},
  91. {"INFRASYS_CA7_AP_CCIF"},
  92. {"INFRASYS_CA7_MD_CCIF"},
  93. {"RSVD"},
  94. {"INFRASYS_GPIO1_CONTROLLER"},
  95. {"INFRASYS_MBIST_CONTROL_REG"},
  96. {"INFRASYS_TRNG"},
  97. {"INFRA_AO_TOP_LEVEL_CLOCK_GENERATOR"},
  98. {"INFRASYS_GPIO1_CONTROLLER"},
  99. /*30*/
  100. {"INFRA_AO_TOP_LEVEL_REST_GENERATOR"},
  101. {"INFRASYS_DDRPHY"},
  102. {"INFRASYS_DRAM_CONTROLLER"},
  103. {"INFRASYS_MIPI_RX_ANA"},
  104. {"INFRASYS_GCPU"},
  105. {"INFRASYS_GCE"},
  106. {"INFRASYS_CCIF_AP_1"},
  107. {"INFRASYS_CCIF_MD_1"},
  108. {"INFRASYS_CLDMA_PDN_AP"},
  109. {"INFRASYS_CLDMA_PDN_MD"},
  110. /*40*/
  111. {"INFRASYS_MD2MD_CCIF0"},
  112. {"INFRASYS_MD2MD_CCIF1"},
  113. {"INFRASYS_MDSYSINTF"},
  114. {"DEGBUGSYS"},
  115. {"DMA"},
  116. {"AUXADC"},
  117. {"UART0"},
  118. {"UART1"},
  119. {"UART2"},
  120. {"UART3"},
  121. /*50*/
  122. {"PWM"},
  123. {"I2C0"},
  124. {"I2C1"},
  125. {"I2C2"},
  126. {"SPI0"},
  127. {"PTP_THERMAL_CTL"},
  128. {"BTIF"},
  129. {"UART4"},
  130. {"DISP_PWM"},
  131. {"I2C3"},
  132. /*60*/
  133. {"IRDA"},
  134. {"IR_TX"},
  135. {"USB2.0"},
  136. {"USB2.0 SIF"},
  137. {"AUDIO"},
  138. {"MSDC0"},
  139. {"MSDC1"},
  140. {"MSDC2"},
  141. {"USB3.0"},
  142. {"WCN_AHB_SLAVE"},
  143. /*70*/
  144. {"MD2_PERIPHERALS"},
  145. {"MD3_PERIPHERALS"},
  146. {"G3D_CONFIG"},
  147. {"MALI"},
  148. {"MMSYS_CONFIG"},
  149. {"MDP_RDMA"},
  150. {"MDP_RSZ0"},
  151. {"MDP_RSZ1"},
  152. {"MDP_WDMA"},
  153. {"MDP_WROT"},
  154. /*80*/
  155. {"MDP_TDSHP"},
  156. {"DISP_OVL"},
  157. {"DISP_RDMA0"},
  158. {"DISP_RDMA1"},
  159. {"DISP_WDMA"},
  160. {"DISP_COLOR"},
  161. {"DISP_CCORR"},
  162. {"DISP_AAL"},
  163. {"DISP_GAMMA"},
  164. {"DISP_DITHER"},
  165. /*90*/
  166. {"Reserved"},
  167. {"DSI"},
  168. {"DPI"},
  169. {"Reserved"},
  170. {"MM_MUTEX"},
  171. {"SMI_LARB0"},
  172. {"SMI_COMMON"},
  173. {"MIPI_TX_CONFIG"},
  174. {"IMGSYS_CONFIG"},
  175. {"IMGSYS_SMI_LARB2"},
  176. /*100*/
  177. {"IMGSYS_CAM1"},
  178. {"IMGSYS_CAM2"},
  179. {"IMGSYS_CAM3"},
  180. {"IMGSYS_CAM4"},
  181. {"IMGSYS_SENINF"},
  182. {"IMGSYS_CAMSV"},
  183. {"IMGSYS_FDVT"},
  184. {"IMGSYS_CAM5"},
  185. {"IMGSYS_CAM6"},
  186. {"IMGSYS_CAM7"},
  187. /*110*/
  188. {"VDECSYS_GLOBAL_CONFIGURATION"},
  189. {"SMI_LARB1"},
  190. {"VDEC_FULL_TOP"},
  191. {"VENC_GLOBAL_CON"},
  192. {"SMI_LARB3"},
  193. {"VENC"},
  194. {"JPEG_ENC"},
  195. {"JPEG_DEC"},
  196. };
  197. #elif defined(CONFIG_ARCH_MT6735M)
  198. static struct DEVICE_INFO devapc_devices[] = {
  199. /* Slave */
  200. {"INFRA_AO_INFRASYS_CONFIG_REGS"},
  201. {"INFRA_AO_PMIC_WRAP_CONTROL_REG"},
  202. {"INFRA_AO_PERISYS_CONFIG_REGS"},
  203. {"INFRA_AO_KPAD_CONTROL_REG"},
  204. {"INFRA_AO_GPT"},
  205. {"INFRA_AO_APMCU_EINT_CONTROLLER"},
  206. {"INFRA_AO_TOP_LEVEL_SLP_MANAGER"},
  207. {"INFRA_AO_DEVICE_APC_AO"},
  208. {"INFRA_AO_SEJ"},
  209. {"INFRA_AO_RSVD"},
  210. /*10*/
  211. {"INFRA_AO_CLDMA_AO_TOP_AP"},
  212. {"INFRA_AO_CLDMA_AO_TOP_MD"},
  213. {"Reserved"},
  214. {"INFRASYS_MCUSYS_CONFIG_REG"},
  215. {"INFRASYS_CONTROL_REG"},
  216. {"INFRASYS_BOOTROM/SRAM"},
  217. {"INFRASYS_EMI_BUS_INTERFACE"},
  218. {"INFRASYS_SYSTEM_CIRQ"},
  219. {"INFRASYS_MM_IOMMU_CONFIGURATION"},
  220. {"INFRASYS_EFUSEC"},
  221. /*20*/
  222. {"INFRASYS_DEVICE_APC_MONITOR"},
  223. {"INFRASYS_MCU_BIU_CONFIGURATION"},
  224. {"INFRASYS_AP_MIXED_CONTROL_REG"},
  225. {"INFRASYS_CA7_AP_CCIF"},
  226. {"INFRASYS_CA7_MD_CCIF"},
  227. {"RSVD"},
  228. {"INFRASYS_GPIO1_CONTROLLER"},
  229. {"INFRASYS_MBIST_CONTROL_REG"},
  230. {"INFRASYS_TRNG"},
  231. {"INFRA_AO_TOP_LEVEL_CLOCK_GENERATOR"},
  232. /*30*/
  233. {"INFRASYS_GPIO1_CONTROLLER"},
  234. {"INFRA_AO_TOP_LEVEL_REST_GENERATOR"},
  235. {"INFRASYS_DDRPHY"},
  236. {"INFRASYS_DRAM_CONTROLLER"},
  237. {"INFRASYS_MIPI_RX_ANA"},
  238. {"INFRASYS_GCPU"},
  239. {"INFRASYS_GCE"},
  240. {"INFRASYS_CCIF_AP_1"},
  241. {"INFRASYS_CCIF_MD_1"},
  242. {"INFRASYS_CLDMA_PDN_AP"},
  243. /*40*/
  244. {"INFRASYS_CLDMA_PDN_MD"},
  245. {"INFRASYS_MDSYSINTF"},
  246. {"DEGBUGSYS"},
  247. {"DMA"},
  248. {"AUXADC"},
  249. {"UART0"},
  250. {"UART1"},
  251. {"UART2"},
  252. {"UART3"},
  253. {"PWM"},
  254. /*50*/
  255. {"I2C0"},
  256. {"I2C1"},
  257. {"I2C2"},
  258. {"SPI0"},
  259. {"PTP_THERMAL_CTL"},
  260. {"BTIF"},
  261. {"Reserved"},
  262. {"DISP_PWM"},
  263. {"I2C3"},
  264. {"IRDA"},
  265. /*60*/
  266. {"IR_TX"},
  267. {"USB2.0"},
  268. {"USB2.0 SIF"},
  269. {"AUDIO"},
  270. {"MSDC0"},
  271. {"MSDC1"},
  272. {"RESERVE"},
  273. {"RESERVE"},
  274. {"WCN_AHB_SLAVE"},
  275. {"MD_PERIPHERALS"},
  276. /*70*/
  277. {"RESERVE"},
  278. {"G3D_CONFIG"},
  279. {"MALI"},
  280. {"MMSYS_CONFIG"},
  281. {"MDP_RDMA"},
  282. {"MDP_RSZ0"},
  283. {"MDP_RSZ1"},
  284. {"MDP_WDMA"},
  285. {"MDP_WROT"},
  286. {"MDP_TDSHP"},
  287. /*80*/
  288. {"DISP_OVL0"},
  289. {"DISP_OVL1"},
  290. {"DISP_RDMA0"},
  291. {"DISP_RDMA1"},
  292. {"DISP_WDMA"},
  293. {"DISP_COLOR"},
  294. {"DISP_CCORR"},
  295. {"DISP_AAL"},
  296. {"DISP_GAMMA"},
  297. {"DISP_DITHER"},
  298. /*90*/
  299. {"Reserved"},
  300. {"DPI"},
  301. {"DSI"},
  302. {"Reserved"},
  303. {"MM_MUTEX"},
  304. {"SMI_LARB0"},
  305. {"SMI_COMMON"},
  306. {"MIPI_TX_CONFIG"},
  307. {"IMGSYS_CONFIG"},
  308. {"IMGSYS_SMI_LARB2"},
  309. /*100*/
  310. {"IMGSYS_CAM1"},
  311. {"IMGSYS_CAM2"},
  312. {"IMGSYS_SENINF"},
  313. {"VENC"},
  314. {"JPGENC"},
  315. {"VDEC"},
  316. {"VDEC_GLOBAL_CON"},
  317. {"SMI_LARB1"},
  318. {"VDEC_FULL_TOP"},
  319. };
  320. #elif defined(CONFIG_ARCH_MT6753)
  321. static struct DOMAIN_INFO domain_settings[] = {
  322. {"AP"},
  323. {"MD1"},
  324. {"CONN"},
  325. {"Reserved"},
  326. {"MM"},
  327. {"MD3"},
  328. {"MFG"},
  329. {"Reserved"},
  330. };
  331. static struct DEVICE_INFO devapc_devices[] = {
  332. {"INFRA_AO_INFRASYS_CONFIG_REGS"},
  333. {"INFRA_AO_PMIC_WRAP_CONTROL_REG"},
  334. {"INFRA_AO_PERISYS_CONFIG_REGS"},
  335. {"INFRA_AO_KPAD_CONTROL_REG"},
  336. {"INFRA_AO_GPT"},
  337. {"INFRA_AO_APMCU_EINT_CONTROLLER"},
  338. {"INFRA_AO_TOP_LEVEL_SLP_MANAGER"},
  339. {"INFRA_AO_DEVICE_APC_AO"},
  340. {"INFRA_AO_SEJ"},
  341. {"INFRA_AO_RSVD"},
  342. /*10*/
  343. {"INFRA_AO_CLDMA_AO_TOP_AP"},
  344. {"INFRA_AO_CLDMA_AO_TOP_MD"},
  345. {"INFRASYS_MCUSYS_CONFIG_REG"},
  346. {"INFRASYS_CONTROL_REG"},
  347. {"INFRASYS_BOOTROM/SRAM"},
  348. {"INFRASYS_EMI_BUS_INTERFACE"},
  349. {"INFRASYS_SYSTEM_CIRQ"},
  350. {"INFRASYS_MM_IOMMU_CONFIGURATION"},
  351. {"INFRASYS_EFUSEC"},
  352. {"INFRASYS_DEVICE_APC_MONITOR"},
  353. /*20*/
  354. {"INFRASYS_MCU_BIU_CONFIGURATION"},
  355. {"INFRASYS_AP_MIXED_CONTROL_REG"},
  356. {"INFRASYS_CA7_AP_CCIF"},
  357. {"INFRASYS_CA7_MD_CCIF"},
  358. {"RSVD"},
  359. {"INFRASYS_MBIST_CONTROL_REG"},
  360. {"INFRASYS_DRAM_CONTROLLER"},
  361. {"INFRASYS_TRNG"},
  362. {"INFRA_AO_TOP_LEVEL_CLOCK_GENERATOR"},
  363. {"INFRASYS_GPIO1_CONTROLLER"},
  364. /*30*/
  365. {"INFRA_AO_TOP_LEVEL_REST_GENERATOR"},
  366. {"INFRASYS_DDRPHY"},
  367. {"INFRASYS_DRAM_CONTROLLER"},
  368. {"INFRASYS_MIPI_RX_ANA"},
  369. {"INFRASYS_GCPU"},
  370. {"INFRASYS_GCE"},
  371. {"INFRASYS_CCIF_AP_1"},
  372. {"INFRASYS_CCIF_MD_1"},
  373. {"INFRASYS_CLDMA_PDN_AP"},
  374. {"INFRASYS_CLDMA_PDN_MD"},
  375. /*40*/
  376. {"INFRASYS_MD2MD_CCIF0"},
  377. {"INFRASYS_MD2MD_CCIF1"},
  378. {"INFRASYS_MDSYSINTF"},
  379. {"DEGBUGSYS"},
  380. {"DMA"},
  381. {"AUXADC"},
  382. {"UART0"},
  383. {"UART1"},
  384. {"UART2"},
  385. {"UART3"},
  386. /*50*/
  387. {"PWM"},
  388. {"I2C0"},
  389. {"I2C1"},
  390. {"I2C2"},
  391. {"SPI0"},
  392. {"PTP_THERMAL_CTL"},
  393. {"BTIF"},
  394. {"UART4"},
  395. {"DISP_PWM"},
  396. {"I2C3"},
  397. /*60*/
  398. {"IRDA"},
  399. {"IR_TX"},
  400. {"I2C4"},
  401. {"USB2.0"},
  402. {"USB2.0 SIF"},
  403. {"AUDIO"},
  404. {"MSDC0"},
  405. {"MSDC1"},
  406. {"MSDC2"},
  407. {"MSDC3"},
  408. /*70*/
  409. {"WCN_AHB_SLAVE"},
  410. {"MD_PERIPHERALS"},
  411. {"MD2_PERIPHERALS"},
  412. {"G3D_CONFIG"},
  413. {"MALI"},
  414. {"MMSYS_CONFIG"},
  415. {"MDP_RDMA"},
  416. {"MDP_RSZ0"},
  417. {"MDP_RSZ1"},
  418. {"MDP_WDMA"},
  419. /*80*/
  420. {"MDP_WROT"},
  421. {"MDP_TDSHP"},
  422. {"DISP_OVL0"},
  423. {"DISP_OVL1"},
  424. {"DISP_RDMA0"},
  425. {"DISP_RDMA1"},
  426. {"DISP_WDMA"},
  427. {"DISP_COLOR"},
  428. {"DISP_CCORR"},
  429. {"DISP_AAL"},
  430. /*90*/
  431. {"DISP_GAMMA"},
  432. {"DISP_DITHER"},
  433. {"Reserved"},
  434. {"DISP_OD"},
  435. {"DSI"},
  436. {"DPI"},
  437. {"MM_MUTEX"},
  438. {"SMI_LARB0"},
  439. {"SMI_COMMON"},
  440. {"IMGSYS_CONFIG"},
  441. /*100*/
  442. {"IMGSYS_SMI_LARB2"},
  443. {"IMGSYS_CAM1"},
  444. {"IMGSYS_CAM2"},
  445. {"IMGSYS_CAM3"},
  446. {"IMGSYS_CAM4"},
  447. {"IMGSYS_SENINF"},
  448. {"IMGSYS_CAMSV"},
  449. {"IMGSYS_FDVT"},
  450. {"IMGSYS_CAM5"},
  451. {"IMGSYS_CAM6"},
  452. /*110*/
  453. {"IMGSYS_CAM7"},
  454. {"VDECSYS_GLOBAL_CONFIGURATION"},
  455. {"SMI_LARB1"},
  456. {"VDEC_FULL_TOP"},
  457. {"VENC_GLOBAL_CON"},
  458. {"SMI_LARB3"},
  459. {"VENC"},
  460. {"JPEG_ENC"},
  461. {"JPEG_DEC"},
  462. };
  463. #else
  464. #error "Wrong Config type"
  465. #endif
  466. /*****************************************************************************
  467. *FUNCTION DEFINITION
  468. *****************************************************************************/
  469. static int clear_vio_status(unsigned int module);
  470. static int devapc_ioremap(void);
  471. /**************************************************************************
  472. *EXTERN FUNCTION
  473. **************************************************************************/
  474. int mt_devapc_check_emi_violation(void)
  475. {
  476. if ((readl(IOMEM(DEVAPC0_D0_VIO_STA_3)) & ABORT_EMI) == 0)
  477. return -1;
  478. pr_debug("EMI violation! It should be cleared by EMI MPU driver later!\n");
  479. return 0;
  480. }
  481. int mt_devapc_emi_initial(void)
  482. {
  483. DEVAPC_MSG("EMI_DAPC Init start\n");
  484. devapc_ioremap();
  485. if (NULL != devapc_ao_base) {
  486. mt_reg_sync_writel(readl(IOMEM(DEVAPC0_APC_CON)) & (0xFFFFFFFF ^ (1 << 2)), DEVAPC0_APC_CON);
  487. mt_reg_sync_writel(readl(IOMEM(DEVAPC0_PD_APC_CON)) & (0xFFFFFFFF ^ (1 << 2)), DEVAPC0_PD_APC_CON);
  488. mt_reg_sync_writel(ABORT_EMI, DEVAPC0_D0_VIO_STA_3);
  489. mt_reg_sync_writel(readl(IOMEM(DEVAPC0_D0_VIO_MASK_3)) & (0xFFFFFFFF ^ (ABORT_EMI)),
  490. DEVAPC0_D0_VIO_MASK_3);
  491. DEVAPC_MSG("EMI_DAPC Init done\n");
  492. return 0;
  493. }
  494. return -1;
  495. }
  496. int mt_devapc_clear_emi_violation(void)
  497. {
  498. if ((readl(IOMEM(DEVAPC0_D0_VIO_STA_3)) & ABORT_EMI) != 0)
  499. mt_reg_sync_writel(ABORT_EMI, DEVAPC0_D0_VIO_STA_3);
  500. return 0;
  501. }
  502. /*
  503. * mt_devapc_set_permission: set module permission on device apc.
  504. * @module: the moudle to specify permission
  505. * @domain_num: domain index number
  506. * @permission_control: specified permission
  507. * no return value.
  508. */
  509. int mt_devapc_set_permission(unsigned int module, E_MASK_DOM domain_num, APC_ATTR permission)
  510. {
  511. unsigned int *base;
  512. unsigned int clr_bit = 0x3 << ((module % 16) * 2);
  513. unsigned int set_bit = permission << ((module % 16) * 2);
  514. if (module >= DEVAPC_DEVICE_NUMBER) {
  515. DEVAPC_MSG("[DEVAPC] ERROR, device number %d exceeds the max number!\n", module);
  516. return -1;
  517. }
  518. if (DEVAPC_DOMAIN_AP == domain_num)
  519. base = DEVAPC0_D0_APC_0 + (module / 16) * 4;
  520. else if (DEVAPC_DOMAIN_MD1 == domain_num)
  521. base = DEVAPC0_D1_APC_0 + (module / 16) * 4;
  522. else if (DEVAPC_DOMAIN_CONN == domain_num)
  523. base = DEVAPC0_D2_APC_0 + (module / 16) * 4;
  524. else if (DEVAPC_DOMAIN_MD32 == domain_num)
  525. base = DEVAPC0_D3_APC_0 + (module / 16) * 4;
  526. #if defined(CONFIG_ARCH_MT6735)
  527. else if (DEVAPC_DOMAIN_MM == domain_num)
  528. base = DEVAPC0_D4_APC_0 + (module / 16) * 4;
  529. else if (DEVAPC_DOMAIN_MD3 == domain_num)
  530. base = DEVAPC0_D5_APC_0 + (module / 16) * 4;
  531. else if (DEVAPC_DOMAIN_MFG == domain_num)
  532. base = DEVAPC0_D6_APC_0 + (module / 16) * 4;
  533. #elif defined(CONFIG_ARCH_MT6735M)
  534. /* blank intentionally */
  535. #elif defined(CONFIG_ARCH_MT6753)
  536. else if (DEVAPC_DOMAIN_MM == domain_num)
  537. base = DEVAPC0_D4_APC_0 + (module / 16) * 4;
  538. else if (DEVAPC_DOMAIN_MD3 == domain_num)
  539. base = DEVAPC0_D5_APC_0 + (module / 16) * 4;
  540. else if (DEVAPC_DOMAIN_MFG == domain_num)
  541. base = DEVAPC0_D6_APC_0 + (module / 16) * 4;
  542. #else
  543. #error "Wrong Config type"
  544. #endif
  545. else {
  546. DEVAPC_MSG("[DEVAPC] ERROR, domain number %d exceeds the max number!\n", domain_num);
  547. return -2;
  548. }
  549. mt_reg_sync_writel(readl(base) & ~clr_bit, base);
  550. mt_reg_sync_writel(readl(base) | set_bit, base);
  551. return 0;
  552. }
  553. /**************************************************************************
  554. *STATIC FUNCTION
  555. **************************************************************************/
  556. static int devapc_ioremap(void)
  557. {
  558. struct device_node *node = NULL;
  559. /*IO remap*/
  560. node = of_find_compatible_node(NULL, NULL, "mediatek,DEVAPC_AO");
  561. if (node) {
  562. devapc_ao_base = of_iomap(node, 0);
  563. DEVAPC_MSG("[DEVAPC] AO_ADDRESS %p\n", devapc_ao_base);
  564. } else {
  565. pr_err("[DEVAPC] can't find DAPC_AO compatible node\n");
  566. return -1;
  567. }
  568. node = of_find_compatible_node(NULL, NULL, "mediatek,DEVAPC");
  569. if (node) {
  570. devapc_pd_base = of_iomap(node, 0);
  571. devapc_irq = irq_of_parse_and_map(node, 0);
  572. DEVAPC_MSG("[DEVAPC] PD_ADDRESS %p, IRD: %d\n", devapc_pd_base, devapc_irq);
  573. } else {
  574. pr_err("[DEVAPC] can't find DAPC_PD compatible node\n");
  575. return -1;
  576. }
  577. return 0;
  578. }
  579. #ifdef CONFIG_MTK_HIBERNATION
  580. static int devapc_pm_restore_noirq(struct device *device)
  581. {
  582. if (devapc_irq != 0) {
  583. mt_irq_set_sens(devapc_irq, MT_LEVEL_SENSITIVE);
  584. mt_irq_set_polarity(devapc_irq, MT_POLARITY_LOW);
  585. }
  586. return 0;
  587. }
  588. #endif
  589. #if defined(CONFIG_ARCH_MT6753)
  590. static int check_vio_status(unsigned int module)
  591. {
  592. unsigned int apc_index = 0;
  593. unsigned int apc_bit_index = 0;
  594. unsigned int vio_status = 0;
  595. apc_index = module / (MOD_NO_IN_1_DEVAPC*2);
  596. apc_bit_index = module % (MOD_NO_IN_1_DEVAPC*2);
  597. switch (apc_index) {
  598. case 0:
  599. vio_status = (*DEVAPC0_D0_VIO_STA_0 & (0x1 << apc_bit_index));
  600. break;
  601. case 1:
  602. vio_status = (*DEVAPC0_D0_VIO_STA_1 & (0x1 << apc_bit_index));
  603. break;
  604. case 2:
  605. vio_status = (*DEVAPC0_D0_VIO_STA_2 & (0x1 << apc_bit_index));
  606. break;
  607. case 3:
  608. vio_status = (*DEVAPC0_D0_VIO_STA_3 & (0x1 << apc_bit_index));
  609. break;
  610. case 4:
  611. vio_status = (*DEVAPC0_D0_VIO_STA_4 & (0x1 << apc_bit_index));
  612. break;
  613. default:
  614. break;
  615. }
  616. if (vio_status)
  617. return 1;
  618. return 0;
  619. }
  620. #endif
  621. #if defined(CONFIG_TRUSTONIC_TEE_SUPPORT) || defined(CONFIG_ARM_PSCI) || defined(CONFIG_MTK_PSCI)
  622. /* ATF or TEE runs this path */
  623. static void start_devapc(void)
  624. {
  625. mt_reg_sync_writel(readl(DEVAPC0_PD_APC_CON) & (0xFFFFFFFF ^ (1<<2)), DEVAPC0_PD_APC_CON);
  626. DEVAPC_MSG("[DEVAPC] Making SMC call to ATF.\n");
  627. /*Set DAPC in ATF*/
  628. mt_secure_call(MTK_SIP_KERNEL_DAPC_INIT, 0, 0, 0);
  629. }
  630. #else
  631. #error "Wrong Arch type (Kernel only)"
  632. #endif
  633. /*
  634. * clear_vio_status: clear violation status for each module.
  635. * @module: the moudle to clear violation status
  636. * @devapc_num: device apc index number (device apc 0 or 1)
  637. * @domain_num: domain index number (AP or MD domain)
  638. * no return value.
  639. */
  640. static int clear_vio_status(unsigned int module)
  641. {
  642. unsigned int apc_index = 0;
  643. unsigned int apc_bit_index = 0;
  644. apc_index = module / (MOD_NO_IN_1_DEVAPC*2);
  645. apc_bit_index = module % (MOD_NO_IN_1_DEVAPC*2);
  646. switch (apc_index) {
  647. case 0:
  648. *DEVAPC0_D0_VIO_STA_0 = (0x1 << apc_bit_index);
  649. break;
  650. case 1:
  651. *DEVAPC0_D0_VIO_STA_1 = (0x1 << apc_bit_index);
  652. break;
  653. case 2:
  654. *DEVAPC0_D0_VIO_STA_2 = (0x1 << apc_bit_index);
  655. break;
  656. case 3:
  657. *DEVAPC0_D0_VIO_STA_3 = (0x1 << apc_bit_index);
  658. break;
  659. case 4:
  660. *DEVAPC0_D0_VIO_STA_4 = (0x1 << apc_bit_index);
  661. break;
  662. default:
  663. break;
  664. }
  665. return 0;
  666. }
  667. static irqreturn_t devapc_violation_irq(int irq, void *dev_id)
  668. {
  669. unsigned int dbg0 = 0, dbg1 = 0;
  670. unsigned int master_id;
  671. unsigned int domain_id;
  672. unsigned int r_w_violation;
  673. int i;
  674. dbg0 = readl(DEVAPC0_VIO_DBG0);
  675. dbg1 = readl(DEVAPC0_VIO_DBG1);
  676. master_id = dbg0 & VIO_DBG_MSTID;
  677. domain_id = dbg0 & VIO_DBG_DMNID;
  678. r_w_violation = dbg0 & VIO_DBG_RW;
  679. #if defined(CONFIG_ARCH_MT6753)
  680. /* violation information improvement for Denali-3 */
  681. if ((domain_id >= 0) && (domain_id < ARRAY_SIZE(domain_settings))) {
  682. if (1 == r_w_violation) {
  683. pr_debug("[DEVAPC] Device Access Permission Write Violation - Process:%s PID:%i Vio Addr:0x%x , Bus ID:0x%x, Dom ID:0x%x (%s), VIO_DBG0:0x%x\n",
  684. current->comm, current->pid, dbg1, master_id, domain_id,
  685. domain_settings[domain_id].name, (*DEVAPC0_VIO_DBG0));
  686. } else {
  687. pr_debug("[DEVAPC] Device Access Permission Read Violation - Process:%s PID:%i Vio Addr:0x%x , Bus ID:0x%x, Dom ID:0x%x (%s), VIO_DBG0:0x%x\n",
  688. current->comm, current->pid, dbg1, master_id, domain_id,
  689. domain_settings[domain_id].name, (*DEVAPC0_VIO_DBG0));
  690. }
  691. } else {
  692. if (1 == r_w_violation) {
  693. pr_debug("[DEVAPC] Device Access Permission Write Violation - Process:%s PID:%i Vio Addr:0x%x , Bus ID:0x%x , Dom ID:0x%x, VIO_DBG0:0x%x\n",
  694. current->comm, current->pid, dbg1, master_id, domain_id, (*DEVAPC0_VIO_DBG0));
  695. } else {
  696. pr_debug("[DEVAPC] Device Access Permission Read Violation - Process:%s PID:%i Vio Addr:0x%x , Bus ID:0x%x , Dom ID:0x%x, VIO_DBG0:0x%x\n",
  697. current->comm, current->pid, dbg1, master_id, domain_id, (*DEVAPC0_VIO_DBG0));
  698. }
  699. }
  700. #else
  701. if (1 == r_w_violation) {
  702. pr_debug("[DEVAPC] Device Access Permission Write Violation - Process:%s PID:%i Vio Addr:0x%x , Bus ID:0x%x , Dom ID:0x%x\n",
  703. current->comm, current->pid, dbg1, master_id, domain_id);
  704. } else {
  705. pr_debug("[DEVAPC] Device Access Permission Read Violation - Process:%s PID:%i Vio Addr:0x%x , Bus ID:0x%x , Dom ID:0x%x\n",
  706. current->comm, current->pid, dbg1, master_id, domain_id);
  707. }
  708. #endif
  709. pr_debug("[DEVAPC] VIO_STA 0:0x%x, 1:0x%x, 2:0x%x, 3:0x%x, 4:0x%x\n",
  710. readl(DEVAPC0_D0_VIO_STA_0), readl(DEVAPC0_D0_VIO_STA_1), readl(DEVAPC0_D0_VIO_STA_2),
  711. readl(DEVAPC0_D0_VIO_STA_3), readl(DEVAPC0_D0_VIO_STA_4));
  712. for (i = 0; i < (ARRAY_SIZE(devapc_devices)); i++) {
  713. #if defined(CONFIG_ARCH_MT6753)
  714. /* violation information improvement */
  715. if (check_vio_status(i))
  716. pr_debug("[DEVAPC] Access Violation Slave: %s (index=%d)\n", devapc_devices[i].device, i);
  717. #endif
  718. clear_vio_status(i);
  719. }
  720. mt_reg_sync_writel(VIO_DBG_CLR, DEVAPC0_VIO_DBG0);
  721. dbg0 = readl(DEVAPC0_VIO_DBG0);
  722. dbg1 = readl(DEVAPC0_VIO_DBG1);
  723. if ((dbg0 != 0) || (dbg1 != 0)) {
  724. pr_debug("[DEVAPC] Multi-violation!\n");
  725. pr_debug("[DEVAPC] DBG0 = %x, DBG1 = %x\n", dbg0, dbg1);
  726. }
  727. return IRQ_HANDLED;
  728. }
  729. static int devapc_probe(struct platform_device *dev)
  730. {
  731. int ret;
  732. DEVAPC_MSG("[DEVAPC] module probe.\n");
  733. /*IO remap*/
  734. devapc_ioremap();
  735. /*
  736. * Interrupts of vilation (including SPC in SMI, or EMI MPU) are triggered by the device APC.
  737. * need to share the interrupt with the SPC driver.
  738. */
  739. ret = request_irq(devapc_irq, (irq_handler_t)devapc_violation_irq,
  740. IRQF_TRIGGER_LOW | IRQF_SHARED, "devapc", &g_devapc_ctrl);
  741. if (ret) {
  742. pr_err("[DEVAPC] Failed to request irq! (%d)\n", ret);
  743. return ret;
  744. }
  745. #ifdef CONFIG_MTK_CLKMGR
  746. /* mt_clkmgr */
  747. enable_clock(MT_CG_INFRA_DEVAPC, "DEVAPC");
  748. #else
  749. /* CCF */
  750. dapc_clk = devm_clk_get(&dev->dev, "devapc-main");
  751. if (IS_ERR(dapc_clk)) {
  752. pr_err("[DEVAPC] cannot get dapc clock.\n");
  753. return PTR_ERR(dapc_clk);
  754. }
  755. clk_prepare_enable(dapc_clk);
  756. #endif
  757. #ifdef CONFIG_MTK_HIBERNATION
  758. register_swsusp_restore_noirq_func(ID_M_DEVAPC, devapc_pm_restore_noirq, NULL);
  759. #endif
  760. start_devapc();
  761. return 0;
  762. }
  763. static int devapc_remove(struct platform_device *dev)
  764. {
  765. return 0;
  766. }
  767. static int devapc_suspend(struct platform_device *dev, pm_message_t state)
  768. {
  769. return 0;
  770. }
  771. static int devapc_resume(struct platform_device *dev)
  772. {
  773. DEVAPC_MSG("[DEVAPC] module resume.\n");
  774. return 0;
  775. }
  776. struct platform_device devapc_device = {
  777. .name = "devapc",
  778. .id = -1,
  779. };
  780. static const struct of_device_id mt_dapc_of_match[] = {
  781. { .compatible = "mediatek,DEVAPC", },
  782. {/* sentinel */},
  783. };
  784. MODULE_DEVICE_TABLE(of, mt_dapc_of_match);
  785. static struct platform_driver devapc_driver = {
  786. .probe = devapc_probe,
  787. .remove = devapc_remove,
  788. .suspend = devapc_suspend,
  789. .resume = devapc_resume,
  790. .driver = {
  791. .name = "dapc",
  792. .owner = THIS_MODULE,
  793. #ifdef CONFIG_OF
  794. .of_match_table = mt_dapc_of_match,
  795. #endif
  796. },
  797. };
  798. /*
  799. * devapc_init: module init function.
  800. */
  801. static int __init devapc_init(void)
  802. {
  803. int ret;
  804. DEVAPC_MSG("[DEVAPC] module init.\n");
  805. ret = platform_device_register(&devapc_device);
  806. if (ret) {
  807. pr_err("[DEVAPC] Unable to do device register(%d)\n", ret);
  808. return ret;
  809. }
  810. ret = platform_driver_register(&devapc_driver);
  811. if (ret) {
  812. pr_err("[DEVAPC] Unable to register driver (%d)\n", ret);
  813. platform_device_unregister(&devapc_device);
  814. return ret;
  815. }
  816. g_devapc_ctrl = cdev_alloc();
  817. if (!g_devapc_ctrl) {
  818. pr_err("[DEVAPC] Failed to add devapc device! (%d)\n", ret);
  819. platform_driver_unregister(&devapc_driver);
  820. platform_device_unregister(&devapc_device);
  821. return ret;
  822. }
  823. g_devapc_ctrl->owner = THIS_MODULE;
  824. return 0;
  825. }
  826. /*
  827. * devapc_exit: module exit function.
  828. */
  829. static void __exit devapc_exit(void)
  830. {
  831. DEVAPC_MSG("[DEVAPC] DEVAPC module exit\n");
  832. #ifdef CONFIG_MTK_HIBERNATION
  833. unregister_swsusp_restore_noirq_func(ID_M_DEVAPC);
  834. #endif
  835. }
  836. late_initcall(devapc_init);
  837. module_exit(devapc_exit);
  838. MODULE_LICENSE("GPL");