spram.c 4.7 KB

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