imx_rdc.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2019, NXP. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <errno.h>
  7. #include <lib/mmio.h>
  8. #include <imx_rdc.h>
  9. struct imx_uart {
  10. int index;
  11. unsigned int uart_base;
  12. };
  13. static const struct imx_uart imx8m_uart_info[] = {
  14. { /* UART 1 */
  15. .index = RDC_PDAP_UART1,
  16. .uart_base = IMX_UART1_BASE,
  17. }, { /* UART 2 */
  18. .index = RDC_PDAP_UART2,
  19. .uart_base = IMX_UART2_BASE,
  20. }, { /* UART 3 */
  21. .index = RDC_PDAP_UART3,
  22. .uart_base = IMX_UART3_BASE,
  23. }, { /* UART 4 */
  24. .index = RDC_PDAP_UART4,
  25. .uart_base = IMX_UART4_BASE,
  26. }
  27. };
  28. static int imx_rdc_uart_get_pdap_index(unsigned int uart_base)
  29. {
  30. size_t i;
  31. for (i = 0; i < ARRAY_SIZE(imx8m_uart_info); i++) {
  32. if (imx8m_uart_info[i].uart_base == uart_base) {
  33. return imx8m_uart_info[i].index;
  34. }
  35. }
  36. return -ENODEV;
  37. }
  38. static void imx_rdc_console_access_enable(struct imx_rdc_cfg *rdc_cfg,
  39. unsigned int console_base)
  40. {
  41. struct imx_rdc_cfg *rdc;
  42. int console_pdap_index;
  43. console_pdap_index = imx_rdc_uart_get_pdap_index(console_base);
  44. if (console_pdap_index < 0) {
  45. return;
  46. }
  47. for (rdc = rdc_cfg; rdc->type != RDC_INVALID; rdc++) {
  48. if (rdc->type != RDC_PDAP || rdc->index != console_pdap_index) {
  49. continue;
  50. }
  51. if (rdc->index == console_pdap_index &&
  52. rdc->setting.rdc_pdap == (D0R | D0W)) {
  53. return;
  54. }
  55. if (rdc->index == console_pdap_index) {
  56. rdc->setting.rdc_pdap = D0R | D0W;
  57. }
  58. }
  59. }
  60. void imx_rdc_init(struct imx_rdc_cfg *rdc_cfg, unsigned int console_base)
  61. {
  62. struct imx_rdc_cfg *rdc = rdc_cfg;
  63. imx_rdc_console_access_enable(rdc, console_base);
  64. while (rdc->type != RDC_INVALID) {
  65. switch (rdc->type) {
  66. case RDC_MDA:
  67. /* MDA config */
  68. mmio_write_32(MDAn(rdc->index), rdc->setting.rdc_mda);
  69. break;
  70. case RDC_PDAP:
  71. /* peripheral access permission config */
  72. mmio_write_32(PDAPn(rdc->index), rdc->setting.rdc_pdap);
  73. break;
  74. case RDC_MEM_REGION:
  75. /* memory region access permission config */
  76. mmio_write_32(MRSAn(rdc->index), rdc->setting.rdc_mem_region[0]);
  77. mmio_write_32(MREAn(rdc->index), rdc->setting.rdc_mem_region[1]);
  78. mmio_write_32(MRCn(rdc->index), rdc->setting.rdc_mem_region[2]);
  79. break;
  80. default:
  81. break;
  82. }
  83. rdc++;
  84. }
  85. }