spram.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * MIPS SPRAM support
  4. *
  5. * Copyright (C) 2007, 2008 MIPS Technologies, Inc.
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/ptrace.h>
  9. #include <linux/stddef.h>
  10. #include <asm/fpu.h>
  11. #include <asm/mipsregs.h>
  12. #include <asm/r4kcache.h>
  13. #include <asm/hazards.h>
  14. #include <asm/spram.h>
  15. /*
  16. * These definitions are correct for the 24K/34K/74K SPRAM sample
  17. * implementation. The 4KS interpreted the tags differently...
  18. */
  19. #define SPRAM_TAG0_ENABLE 0x00000080
  20. #define SPRAM_TAG0_PA_MASK 0xfffff000
  21. #define SPRAM_TAG1_SIZE_MASK 0xfffff000
  22. #define SPRAM_TAG_STRIDE 8
  23. #define ERRCTL_SPRAM (1 << 28)
  24. /* errctl access */
  25. #define read_c0_errctl(x) read_c0_ecc(x)
  26. #define write_c0_errctl(x) write_c0_ecc(x)
  27. /*
  28. * Different semantics to the set_c0_* function built by __BUILD_SET_C0
  29. */
  30. static unsigned int bis_c0_errctl(unsigned int set)
  31. {
  32. unsigned int res;
  33. res = read_c0_errctl();
  34. write_c0_errctl(res | set);
  35. return res;
  36. }
  37. static void ispram_store_tag(unsigned int offset, unsigned int data)
  38. {
  39. unsigned int errctl;
  40. /* enable SPRAM tag access */
  41. errctl = bis_c0_errctl(ERRCTL_SPRAM);
  42. ehb();
  43. write_c0_taglo(data);
  44. ehb();
  45. cache_op(Index_Store_Tag_I, CKSEG0|offset);
  46. ehb();
  47. write_c0_errctl(errctl);
  48. ehb();
  49. }
  50. static unsigned int ispram_load_tag(unsigned int offset)
  51. {
  52. unsigned int data;
  53. unsigned int errctl;
  54. /* enable SPRAM tag access */
  55. errctl = bis_c0_errctl(ERRCTL_SPRAM);
  56. ehb();
  57. cache_op(Index_Load_Tag_I, CKSEG0 | offset);
  58. ehb();
  59. data = read_c0_taglo();
  60. ehb();
  61. write_c0_errctl(errctl);
  62. ehb();
  63. return data;
  64. }
  65. static void dspram_store_tag(unsigned int offset, unsigned int data)
  66. {
  67. unsigned int errctl;
  68. /* enable SPRAM tag access */
  69. errctl = bis_c0_errctl(ERRCTL_SPRAM);
  70. ehb();
  71. write_c0_dtaglo(data);
  72. ehb();
  73. cache_op(Index_Store_Tag_D, CKSEG0 | offset);
  74. ehb();
  75. write_c0_errctl(errctl);
  76. ehb();
  77. }
  78. static unsigned int dspram_load_tag(unsigned int offset)
  79. {
  80. unsigned int data;
  81. unsigned int errctl;
  82. errctl = bis_c0_errctl(ERRCTL_SPRAM);
  83. ehb();
  84. cache_op(Index_Load_Tag_D, CKSEG0 | offset);
  85. ehb();
  86. data = read_c0_dtaglo();
  87. ehb();
  88. write_c0_errctl(errctl);
  89. ehb();
  90. return data;
  91. }
  92. static void probe_spram(char *type,
  93. unsigned int base,
  94. unsigned int (*read)(unsigned int),
  95. void (*write)(unsigned int, unsigned int))
  96. {
  97. unsigned int firstsize = 0, lastsize = 0;
  98. unsigned int firstpa = 0, lastpa = 0, pa = 0;
  99. unsigned int offset = 0;
  100. unsigned int size, tag0, tag1;
  101. unsigned int enabled;
  102. int i;
  103. /*
  104. * The limit is arbitrary but avoids the loop running away if
  105. * the SPRAM tags are implemented differently
  106. */
  107. for (i = 0; i < 8; i++) {
  108. tag0 = read(offset);
  109. tag1 = read(offset+SPRAM_TAG_STRIDE);
  110. pr_debug("DBG %s%d: tag0=%08x tag1=%08x\n",
  111. type, i, tag0, tag1);
  112. size = tag1 & SPRAM_TAG1_SIZE_MASK;
  113. if (size == 0)
  114. break;
  115. if (i != 0) {
  116. /* tags may repeat... */
  117. if ((pa == firstpa && size == firstsize) ||
  118. (pa == lastpa && size == lastsize))
  119. break;
  120. }
  121. /* Align base with size */
  122. base = (base + size - 1) & ~(size-1);
  123. /* reprogram the base address base address and enable */
  124. tag0 = (base & SPRAM_TAG0_PA_MASK) | SPRAM_TAG0_ENABLE;
  125. write(offset, tag0);
  126. base += size;
  127. /* reread the tag */
  128. tag0 = read(offset);
  129. pa = tag0 & SPRAM_TAG0_PA_MASK;
  130. enabled = tag0 & SPRAM_TAG0_ENABLE;
  131. if (i == 0) {
  132. firstpa = pa;
  133. firstsize = size;
  134. }
  135. lastpa = pa;
  136. lastsize = size;
  137. if (strcmp(type, "DSPRAM") == 0) {
  138. unsigned int *vp = (unsigned int *)(CKSEG1 | pa);
  139. unsigned int v;
  140. #define TDAT 0x5a5aa5a5
  141. vp[0] = TDAT;
  142. vp[1] = ~TDAT;
  143. mb();
  144. v = vp[0];
  145. if (v != TDAT)
  146. printk(KERN_ERR "vp=%p wrote=%08x got=%08x\n",
  147. vp, TDAT, v);
  148. v = vp[1];
  149. if (v != ~TDAT)
  150. printk(KERN_ERR "vp=%p wrote=%08x got=%08x\n",
  151. vp+1, ~TDAT, v);
  152. }
  153. pr_info("%s%d: PA=%08x,Size=%08x%s\n",
  154. type, i, pa, size, enabled ? ",enabled" : "");
  155. offset += 2 * SPRAM_TAG_STRIDE;
  156. }
  157. }
  158. void spram_config(void)
  159. {
  160. unsigned int config0;
  161. switch (current_cpu_type()) {
  162. case CPU_24K:
  163. case CPU_34K:
  164. case CPU_74K:
  165. case CPU_1004K:
  166. case CPU_1074K:
  167. case CPU_INTERAPTIV:
  168. case CPU_PROAPTIV:
  169. case CPU_P5600:
  170. case CPU_QEMU_GENERIC:
  171. case CPU_I6400:
  172. case CPU_P6600:
  173. config0 = read_c0_config();
  174. /* FIXME: addresses are Malta specific */
  175. if (config0 & MIPS_CONF_ISP) {
  176. probe_spram("ISPRAM", 0x1c000000,
  177. &ispram_load_tag, &ispram_store_tag);
  178. }
  179. if (config0 & MIPS_CONF_DSP)
  180. probe_spram("DSPRAM", 0x1c100000,
  181. &dspram_load_tag, &dspram_store_tag);
  182. }
  183. }