cpc710_init_ram.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * (C) Copyright 2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <config.h>
  24. #include <common.h>
  25. #include <asm/io.h>
  26. #include "pcippc2.h"
  27. #include "i2c.h"
  28. typedef struct cpc710_mem_org_s {
  29. u8 rows;
  30. u8 cols;
  31. u8 banks2;
  32. u8 org;
  33. } cpc710_mem_org_t;
  34. static int cpc710_compute_mcer (u32 * mcer,
  35. unsigned long *size, unsigned int sdram);
  36. static int cpc710_eeprom_checksum (unsigned int sdram);
  37. static u8 cpc710_eeprom_read (unsigned int sdram, unsigned int offset);
  38. static u32 cpc710_mcer_mem[] = {
  39. 0x000003f3, /* 18 lines, 4 Mb */
  40. 0x000003e3, /* 19 lines, 8 Mb */
  41. 0x000003c3, /* 20 lines, 16 Mb */
  42. 0x00000383, /* 21 lines, 32 Mb */
  43. 0x00000303, /* 22 lines, 64 Mb */
  44. 0x00000203, /* 23 lines, 128 Mb */
  45. 0x00000003, /* 24 lines, 256 Mb */
  46. 0x00000002, /* 25 lines, 512 Mb */
  47. 0x00000001 /* 26 lines, 1024 Mb */
  48. };
  49. static cpc710_mem_org_t cpc710_mem_org[] = {
  50. {0x0c, 0x09, 0x02, 0x00}, /* 0000: 12/ 9/2 */
  51. {0x0d, 0x09, 0x02, 0x00}, /* 0000: 13/ 9/2 */
  52. {0x0d, 0x0a, 0x02, 0x00}, /* 0000: 13/10/2 */
  53. {0x0d, 0x0b, 0x02, 0x00}, /* 0000: 13/11/2 */
  54. {0x0d, 0x0c, 0x02, 0x00}, /* 0000: 13/12/2 */
  55. {0x0e, 0x0c, 0x02, 0x00}, /* 0000: 14/12/2 */
  56. {0x0b, 0x08, 0x02, 0x01}, /* 0001: 11/ 8/2 */
  57. {0x0b, 0x09, 0x01, 0x02}, /* 0010: 11/ 9/1 */
  58. {0x0b, 0x0a, 0x01, 0x03}, /* 0011: 11/10/1 */
  59. {0x0c, 0x08, 0x02, 0x04}, /* 0100: 12/ 8/2 */
  60. {0x0c, 0x0a, 0x02, 0x05}, /* 0101: 12/10/2 */
  61. {0x0d, 0x08, 0x01, 0x06}, /* 0110: 13/ 8/1 */
  62. {0x0d, 0x08, 0x02, 0x07}, /* 0111: 13/ 8/2 */
  63. {0x0d, 0x09, 0x01, 0x08}, /* 1000: 13/ 9/1 */
  64. {0x0d, 0x0a, 0x01, 0x09}, /* 1001: 13/10/1 */
  65. {0x0b, 0x08, 0x01, 0x0a}, /* 1010: 11/ 8/1 */
  66. {0x0c, 0x08, 0x01, 0x0b}, /* 1011: 12/ 8/1 */
  67. {0x0c, 0x09, 0x01, 0x0c}, /* 1100: 12/ 9/1 */
  68. {0x0e, 0x09, 0x02, 0x0d}, /* 1101: 14/ 9/2 */
  69. {0x0e, 0x0a, 0x02, 0x0e}, /* 1110: 14/10/2 */
  70. {0x0e, 0x0b, 0x02, 0x0f} /* 1111: 14/11/2 */
  71. };
  72. unsigned long cpc710_ram_init (void)
  73. {
  74. unsigned long memsize = 0;
  75. unsigned long bank_size;
  76. u32 mcer;
  77. #ifndef CONFIG_SYS_RAMBOOT
  78. /* Clear memory banks
  79. */
  80. out32 (REG (SDRAM0, MCER0), 0);
  81. out32 (REG (SDRAM0, MCER1), 0);
  82. out32 (REG (SDRAM0, MCER2), 0);
  83. out32 (REG (SDRAM0, MCER3), 0);
  84. out32 (REG (SDRAM0, MCER4), 0);
  85. out32 (REG (SDRAM0, MCER5), 0);
  86. out32 (REG (SDRAM0, MCER6), 0);
  87. out32 (REG (SDRAM0, MCER7), 0);
  88. iobarrier_rw ();
  89. /* Disable memory
  90. */
  91. out32 (REG (SDRAM0, MCCR), 0x13b06000);
  92. iobarrier_rw ();
  93. #endif
  94. /* Only the first memory bank is initialised now
  95. */
  96. if (!cpc710_compute_mcer (&mcer, &bank_size, 0)) {
  97. puts ("Unsupported SDRAM type !\n");
  98. hang ();
  99. }
  100. memsize += bank_size;
  101. #ifndef CONFIG_SYS_RAMBOOT
  102. /* Enable bank, zero start
  103. */
  104. out32 (REG (SDRAM0, MCER0), mcer | 0x80000000);
  105. iobarrier_rw ();
  106. #endif
  107. #ifndef CONFIG_SYS_RAMBOOT
  108. /* Enable memory
  109. */
  110. out32 (REG (SDRAM0, MCCR), in32 (REG (SDRAM0, MCCR)) | 0x80000000);
  111. /* Wait until initialisation finished
  112. */
  113. while (!(in32 (REG (SDRAM0, MCCR)) & 0x20000000)) {
  114. iobarrier_rw ();
  115. }
  116. /* Clear Memory Error Status and Address registers
  117. */
  118. out32 (REG (SDRAM0, MESR), 0);
  119. out32 (REG (SDRAM0, MEAR), 0);
  120. iobarrier_rw ();
  121. /* ECC is not configured now
  122. */
  123. #endif
  124. /* Memory size counter
  125. */
  126. out32 (REG (CPC0, RGBAN1), memsize);
  127. return memsize;
  128. }
  129. static int cpc710_compute_mcer (u32 * mcer, unsigned long *size, unsigned int sdram)
  130. {
  131. u8 rows;
  132. u8 cols;
  133. u8 banks2;
  134. unsigned int lines;
  135. u32 mc = 0;
  136. unsigned int i;
  137. cpc710_mem_org_t *org = 0;
  138. if (!i2c_reset ()) {
  139. puts ("Can't reset I2C!\n");
  140. hang ();
  141. }
  142. if (!cpc710_eeprom_checksum (sdram)) {
  143. puts ("Invalid EEPROM checksum !\n");
  144. hang ();
  145. }
  146. rows = cpc710_eeprom_read (sdram, 3);
  147. cols = cpc710_eeprom_read (sdram, 4);
  148. /* Can be 2 or 4 banks; divide by 2
  149. */
  150. banks2 = cpc710_eeprom_read (sdram, 17) / 2;
  151. lines = rows + cols + banks2;
  152. if (lines < 18 || lines > 26) {
  153. /* Unsupported configuration
  154. */
  155. return 0;
  156. }
  157. mc |= cpc710_mcer_mem[lines - 18] << 6;
  158. for (i = 0; i < sizeof (cpc710_mem_org) / sizeof (cpc710_mem_org_t);
  159. i++) {
  160. cpc710_mem_org_t *corg = cpc710_mem_org + i;
  161. if (corg->rows == rows && corg->cols == cols
  162. && corg->banks2 == banks2) {
  163. org = corg;
  164. break;
  165. }
  166. }
  167. if (!org) {
  168. /* Unsupported configuration
  169. */
  170. return 0;
  171. }
  172. mc |= (u32) org->org << 2;
  173. /* Supported configuration
  174. */
  175. *mcer = mc;
  176. *size = 1l << (lines + 4);
  177. return 1;
  178. }
  179. static int cpc710_eeprom_checksum (unsigned int sdram)
  180. {
  181. u8 sum = 0;
  182. unsigned int i;
  183. for (i = 0; i < 63; i++) {
  184. sum += cpc710_eeprom_read (sdram, i);
  185. }
  186. return sum == cpc710_eeprom_read (sdram, 63);
  187. }
  188. static u8 cpc710_eeprom_read (unsigned int sdram, unsigned int offset)
  189. {
  190. u8 dev = (sdram << 1) | 0xa0;
  191. u8 data;
  192. if (!i2c_read_byte (&data, dev, offset)) {
  193. puts ("I2C error !\n");
  194. hang ();
  195. }
  196. return data;
  197. }