mpsc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * MPSC/UART driver for the Marvell mv64360, mv64460, ...
  3. *
  4. * Author: Mark A. Greer <mgreer@mvista.com>
  5. *
  6. * 2007 (c) MontaVista Software, Inc. This file is licensed under
  7. * the terms of the GNU General Public License version 2. This program
  8. * is licensed "as is" without any warranty of any kind, whether express
  9. * or implied.
  10. */
  11. #include <stdarg.h>
  12. #include <stddef.h>
  13. #include "types.h"
  14. #include "string.h"
  15. #include "stdio.h"
  16. #include "io.h"
  17. #include "ops.h"
  18. #define MPSC_CHR_1 0x000c
  19. #define MPSC_CHR_2 0x0010
  20. #define MPSC_CHR_2_TA (1<<7)
  21. #define MPSC_CHR_2_TCS (1<<9)
  22. #define MPSC_CHR_2_RA (1<<23)
  23. #define MPSC_CHR_2_CRD (1<<25)
  24. #define MPSC_CHR_2_EH (1<<31)
  25. #define MPSC_CHR_4 0x0018
  26. #define MPSC_CHR_4_Z (1<<29)
  27. #define MPSC_CHR_5 0x001c
  28. #define MPSC_CHR_5_CTL1_INTR (1<<12)
  29. #define MPSC_CHR_5_CTL1_VALID (1<<15)
  30. #define MPSC_CHR_10 0x0030
  31. #define MPSC_INTR_CAUSE 0x0000
  32. #define MPSC_INTR_CAUSE_RCC (1<<6)
  33. #define MPSC_INTR_MASK 0x0080
  34. #define SDMA_SDCM 0x0008
  35. #define SDMA_SDCM_AR (1<<15)
  36. #define SDMA_SDCM_AT (1<<31)
  37. static volatile char *mpsc_base;
  38. static volatile char *mpscintr_base;
  39. static u32 chr1, chr2;
  40. static int mpsc_open(void)
  41. {
  42. chr1 = in_le32((u32 *)(mpsc_base + MPSC_CHR_1)) & 0x00ff0000;
  43. chr2 = in_le32((u32 *)(mpsc_base + MPSC_CHR_2)) & ~(MPSC_CHR_2_TA
  44. | MPSC_CHR_2_TCS | MPSC_CHR_2_RA | MPSC_CHR_2_CRD
  45. | MPSC_CHR_2_EH);
  46. out_le32((u32 *)(mpsc_base + MPSC_CHR_4), MPSC_CHR_4_Z);
  47. out_le32((u32 *)(mpsc_base + MPSC_CHR_5),
  48. MPSC_CHR_5_CTL1_INTR | MPSC_CHR_5_CTL1_VALID);
  49. out_le32((u32 *)(mpsc_base + MPSC_CHR_2), chr2 | MPSC_CHR_2_EH);
  50. return 0;
  51. }
  52. static void mpsc_putc(unsigned char c)
  53. {
  54. while (in_le32((u32 *)(mpsc_base + MPSC_CHR_2)) & MPSC_CHR_2_TCS);
  55. out_le32((u32 *)(mpsc_base + MPSC_CHR_1), chr1 | c);
  56. out_le32((u32 *)(mpsc_base + MPSC_CHR_2), chr2 | MPSC_CHR_2_TCS);
  57. }
  58. static unsigned char mpsc_getc(void)
  59. {
  60. u32 cause = 0;
  61. unsigned char c;
  62. while (!(cause & MPSC_INTR_CAUSE_RCC))
  63. cause = in_le32((u32 *)(mpscintr_base + MPSC_INTR_CAUSE));
  64. c = in_8((u8 *)(mpsc_base + MPSC_CHR_10 + 2));
  65. out_8((u8 *)(mpsc_base + MPSC_CHR_10 + 2), c);
  66. out_le32((u32 *)(mpscintr_base + MPSC_INTR_CAUSE),
  67. cause & ~MPSC_INTR_CAUSE_RCC);
  68. return c;
  69. }
  70. static u8 mpsc_tstc(void)
  71. {
  72. return (u8)((in_le32((u32 *)(mpscintr_base + MPSC_INTR_CAUSE))
  73. & MPSC_INTR_CAUSE_RCC) != 0);
  74. }
  75. static void mpsc_stop_dma(volatile char *sdma_base)
  76. {
  77. out_le32((u32 *)(mpsc_base + MPSC_CHR_2),MPSC_CHR_2_TA | MPSC_CHR_2_RA);
  78. out_le32((u32 *)(sdma_base + SDMA_SDCM), SDMA_SDCM_AR | SDMA_SDCM_AT);
  79. while ((in_le32((u32 *)(sdma_base + SDMA_SDCM))
  80. & (SDMA_SDCM_AR | SDMA_SDCM_AT)) != 0)
  81. udelay(100);
  82. }
  83. static volatile char *mpsc_get_virtreg_of_phandle(void *devp, char *prop)
  84. {
  85. void *v;
  86. int n;
  87. n = getprop(devp, prop, &v, sizeof(v));
  88. if (n != sizeof(v))
  89. goto err_out;
  90. devp = find_node_by_linuxphandle((u32)v);
  91. if (devp == NULL)
  92. goto err_out;
  93. n = getprop(devp, "virtual-reg", &v, sizeof(v));
  94. if (n == sizeof(v))
  95. return v;
  96. err_out:
  97. return NULL;
  98. }
  99. int mpsc_console_init(void *devp, struct serial_console_data *scdp)
  100. {
  101. void *v;
  102. int n, reg_set;
  103. volatile char *sdma_base;
  104. n = getprop(devp, "virtual-reg", &v, sizeof(v));
  105. if (n != sizeof(v))
  106. goto err_out;
  107. mpsc_base = v;
  108. sdma_base = mpsc_get_virtreg_of_phandle(devp, "sdma");
  109. if (sdma_base == NULL)
  110. goto err_out;
  111. mpscintr_base = mpsc_get_virtreg_of_phandle(devp, "mpscintr");
  112. if (mpscintr_base == NULL)
  113. goto err_out;
  114. n = getprop(devp, "cell-index", &v, sizeof(v));
  115. if (n != sizeof(v))
  116. goto err_out;
  117. reg_set = (int)v;
  118. mpscintr_base += (reg_set == 0) ? 0x4 : 0xc;
  119. /* Make sure the mpsc ctlrs are shutdown */
  120. out_le32((u32 *)(mpscintr_base + MPSC_INTR_CAUSE), 0);
  121. out_le32((u32 *)(mpscintr_base + MPSC_INTR_CAUSE), 0);
  122. out_le32((u32 *)(mpscintr_base + MPSC_INTR_MASK), 0);
  123. out_le32((u32 *)(mpscintr_base + MPSC_INTR_MASK), 0);
  124. mpsc_stop_dma(sdma_base);
  125. scdp->open = mpsc_open;
  126. scdp->putc = mpsc_putc;
  127. scdp->getc = mpsc_getc;
  128. scdp->tstc = mpsc_tstc;
  129. scdp->close = NULL;
  130. return 0;
  131. err_out:
  132. return -1;
  133. }