tqm834x.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * (C) Copyright 2005
  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. */
  24. #include <common.h>
  25. #include <ioports.h>
  26. #include <mpc83xx.h>
  27. #include <asm/mpc8349_pci.h>
  28. #include <i2c.h>
  29. #include <miiphy.h>
  30. #include <asm-ppc/mmu.h>
  31. #include <pci.h>
  32. DECLARE_GLOBAL_DATA_PTR;
  33. #define IOSYNC asm("eieio")
  34. #define ISYNC asm("isync")
  35. #define SYNC asm("sync")
  36. #define FPW FLASH_PORT_WIDTH
  37. #define FPWV FLASH_PORT_WIDTHV
  38. #define DDR_MAX_SIZE_PER_CS 0x20000000
  39. #if defined(DDR_CASLAT_20)
  40. #define TIMING_CASLAT TIMING_CFG1_CASLAT_20
  41. #define MODE_CASLAT DDR_MODE_CASLAT_20
  42. #else
  43. #define TIMING_CASLAT TIMING_CFG1_CASLAT_25
  44. #define MODE_CASLAT DDR_MODE_CASLAT_25
  45. #endif
  46. #define INITIAL_CS_CONFIG (CSCONFIG_EN | CSCONFIG_ROW_BIT_12 | \
  47. CSCONFIG_COL_BIT_9)
  48. /* Global variable used to store detected number of banks */
  49. int tqm834x_num_flash_banks;
  50. /* External definitions */
  51. ulong flash_get_size (ulong base, int banknum);
  52. extern flash_info_t flash_info[];
  53. /* Local functions */
  54. static int detect_num_flash_banks(void);
  55. static long int get_ddr_bank_size(short cs, volatile long *base);
  56. static void set_cs_bounds(short cs, long base, long size);
  57. static void set_cs_config(short cs, long config);
  58. static void set_ddr_config(void);
  59. /* Local variable */
  60. static volatile immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
  61. /**************************************************************************
  62. * Board initialzation after relocation to RAM. Used to detect the number
  63. * of Flash banks on TQM834x.
  64. */
  65. int board_early_init_r (void) {
  66. /* sanity check, IMMARBAR should be mirrored at offset zero of IMMR */
  67. if ((im->sysconf.immrbar & IMMRBAR_BASE_ADDR) != (u32)im)
  68. return 0;
  69. /* detect the number of Flash banks */
  70. return detect_num_flash_banks();
  71. }
  72. /**************************************************************************
  73. * DRAM initalization and size detection
  74. */
  75. phys_size_t initdram (int board_type)
  76. {
  77. long bank_size;
  78. long size;
  79. int cs;
  80. /* during size detection, set up the max DDRLAW size */
  81. im->sysconf.ddrlaw[0].bar = CONFIG_SYS_DDR_BASE;
  82. im->sysconf.ddrlaw[0].ar = (LAWAR_EN | LAWAR_SIZE_2G);
  83. /* set CS bounds to maximum size */
  84. for(cs = 0; cs < 4; ++cs) {
  85. set_cs_bounds(cs,
  86. CONFIG_SYS_DDR_BASE + (cs * DDR_MAX_SIZE_PER_CS),
  87. DDR_MAX_SIZE_PER_CS);
  88. set_cs_config(cs, INITIAL_CS_CONFIG);
  89. }
  90. /* configure ddr controller */
  91. set_ddr_config();
  92. udelay(200);
  93. /* enable DDR controller */
  94. im->ddr.sdram_cfg = (SDRAM_CFG_MEM_EN |
  95. SDRAM_CFG_SREN |
  96. SDRAM_CFG_SDRAM_TYPE_DDR1);
  97. SYNC;
  98. /* size detection */
  99. debug("\n");
  100. size = 0;
  101. for(cs = 0; cs < 4; ++cs) {
  102. debug("\nDetecting Bank%d\n", cs);
  103. bank_size = get_ddr_bank_size(cs,
  104. (volatile long*)(CONFIG_SYS_DDR_BASE + size));
  105. size += bank_size;
  106. debug("DDR Bank%d size: %d MiB\n\n", cs, bank_size >> 20);
  107. /* exit if less than one bank */
  108. if(size < DDR_MAX_SIZE_PER_CS) break;
  109. }
  110. return size;
  111. }
  112. /**************************************************************************
  113. * checkboard()
  114. */
  115. int checkboard (void)
  116. {
  117. puts("Board: TQM834x\n");
  118. #ifdef CONFIG_PCI
  119. volatile immap_t * immr;
  120. u32 w, f;
  121. immr = (immap_t *)CONFIG_SYS_IMMR;
  122. if (!(immr->reset.rcwh & HRCWH_PCI_HOST)) {
  123. printf("PCI: NOT in host mode..?!\n");
  124. return 0;
  125. }
  126. /* get bus width */
  127. w = 32;
  128. if (immr->reset.rcwh & HRCWH_64_BIT_PCI)
  129. w = 64;
  130. /* get clock */
  131. f = gd->pci_clk;
  132. printf("PCI1: %d bit, %d MHz\n", w, f / 1000000);
  133. #else
  134. printf("PCI: disabled\n");
  135. #endif
  136. return 0;
  137. }
  138. /**************************************************************************
  139. *
  140. * Local functions
  141. *
  142. *************************************************************************/
  143. /**************************************************************************
  144. * Detect the number of flash banks (1 or 2). Store it in
  145. * a global variable tqm834x_num_flash_banks.
  146. * Bank detection code based on the Monitor code.
  147. */
  148. static int detect_num_flash_banks(void)
  149. {
  150. typedef unsigned long FLASH_PORT_WIDTH;
  151. typedef volatile unsigned long FLASH_PORT_WIDTHV;
  152. FPWV *bank1_base;
  153. FPWV *bank2_base;
  154. FPW bank1_read;
  155. FPW bank2_read;
  156. ulong bank1_size;
  157. ulong bank2_size;
  158. ulong total_size;
  159. tqm834x_num_flash_banks = 2; /* assume two banks */
  160. /* Get bank 1 and 2 information */
  161. bank1_size = flash_get_size(CONFIG_SYS_FLASH_BASE, 0);
  162. debug("Bank1 size: %lu\n", bank1_size);
  163. bank2_size = flash_get_size(CONFIG_SYS_FLASH_BASE + bank1_size, 1);
  164. debug("Bank2 size: %lu\n", bank2_size);
  165. total_size = bank1_size + bank2_size;
  166. if (bank2_size > 0) {
  167. /* Seems like we've got bank 2, but maybe it's mirrored 1 */
  168. /* Set the base addresses */
  169. bank1_base = (FPWV *) (CONFIG_SYS_FLASH_BASE);
  170. bank2_base = (FPWV *) (CONFIG_SYS_FLASH_BASE + bank1_size);
  171. /* Put bank 2 into CFI command mode and read */
  172. bank2_base[0x55] = 0x00980098;
  173. IOSYNC;
  174. ISYNC;
  175. bank2_read = bank2_base[0x10];
  176. /* Read from bank 1 (it's in read mode) */
  177. bank1_read = bank1_base[0x10];
  178. /* Reset Flash */
  179. bank1_base[0] = 0x00F000F0;
  180. bank2_base[0] = 0x00F000F0;
  181. if (bank2_read == bank1_read) {
  182. /*
  183. * Looks like just one bank, but not sure yet. Let's
  184. * read from bank 2 in autosoelect mode.
  185. */
  186. bank2_base[0x0555] = 0x00AA00AA;
  187. bank2_base[0x02AA] = 0x00550055;
  188. bank2_base[0x0555] = 0x00900090;
  189. IOSYNC;
  190. ISYNC;
  191. bank2_read = bank2_base[0x10];
  192. /* Read from bank 1 (it's in read mode) */
  193. bank1_read = bank1_base[0x10];
  194. /* Reset Flash */
  195. bank1_base[0] = 0x00F000F0;
  196. bank2_base[0] = 0x00F000F0;
  197. if (bank2_read == bank1_read) {
  198. /*
  199. * In both CFI command and autoselect modes,
  200. * we got the some data reading from Flash.
  201. * There is only one mirrored bank.
  202. */
  203. tqm834x_num_flash_banks = 1;
  204. total_size = bank1_size;
  205. }
  206. }
  207. }
  208. debug("Number of flash banks detected: %d\n", tqm834x_num_flash_banks);
  209. /* set OR0 and BR0 */
  210. im->lbus.bank[0].or = CONFIG_SYS_OR_TIMING_FLASH |
  211. (-(total_size) & OR_GPCM_AM);
  212. im->lbus.bank[0].br = (CONFIG_SYS_FLASH_BASE & BR_BA) |
  213. (BR_MS_GPCM | BR_PS_32 | BR_V);
  214. return (0);
  215. }
  216. /*************************************************************************
  217. * Detect the size of a ddr bank. Sets CS bounds and CS config accordingly.
  218. */
  219. static long int get_ddr_bank_size(short cs, volatile long *base)
  220. {
  221. /* This array lists all valid DDR SDRAM configurations, with
  222. * Bank sizes in bytes. (Refer to Table 9-27 in the MPC8349E RM).
  223. * The last entry has to to have size equal 0 and is igonred during
  224. * autodection. Bank sizes must be in increasing order of size
  225. */
  226. struct {
  227. long row;
  228. long col;
  229. long size;
  230. } conf[] = {
  231. {CSCONFIG_ROW_BIT_12, CSCONFIG_COL_BIT_8, 32 << 20},
  232. {CSCONFIG_ROW_BIT_12, CSCONFIG_COL_BIT_9, 64 << 20},
  233. {CSCONFIG_ROW_BIT_12, CSCONFIG_COL_BIT_10, 128 << 20},
  234. {CSCONFIG_ROW_BIT_13, CSCONFIG_COL_BIT_9, 128 << 20},
  235. {CSCONFIG_ROW_BIT_13, CSCONFIG_COL_BIT_10, 256 << 20},
  236. {CSCONFIG_ROW_BIT_13, CSCONFIG_COL_BIT_11, 512 << 20},
  237. {CSCONFIG_ROW_BIT_14, CSCONFIG_COL_BIT_10, 512 << 20},
  238. {CSCONFIG_ROW_BIT_14, CSCONFIG_COL_BIT_11, 1024 << 20},
  239. {0, 0, 0}
  240. };
  241. int i;
  242. int detected;
  243. long size;
  244. detected = -1;
  245. for(i = 0; conf[i].size != 0; ++i) {
  246. /* set sdram bank configuration */
  247. set_cs_config(cs, CSCONFIG_EN | conf[i].col | conf[i].row);
  248. debug("Getting RAM size...\n");
  249. size = get_ram_size(base, DDR_MAX_SIZE_PER_CS);
  250. if((size == conf[i].size) && (i == detected + 1))
  251. detected = i;
  252. debug("Trying %ld x %ld (%ld MiB) at addr %p, detected: %ld MiB\n",
  253. conf[i].row,
  254. conf[i].col,
  255. conf[i].size >> 20,
  256. base,
  257. size >> 20);
  258. }
  259. if(detected == -1){
  260. /* disable empty cs */
  261. debug("\nNo valid configurations for CS%d, disabling...\n", cs);
  262. set_cs_config(cs, 0);
  263. return 0;
  264. }
  265. debug("\nDetected configuration %ld x %ld (%ld MiB) at addr %p\n",
  266. conf[detected].row, conf[detected].col, conf[detected].size >> 20, base);
  267. /* configure cs ro detected params */
  268. set_cs_config(cs, CSCONFIG_EN | conf[detected].row |
  269. conf[detected].col);
  270. set_cs_bounds(cs, (long)base, conf[detected].size);
  271. return(conf[detected].size);
  272. }
  273. /**************************************************************************
  274. * Sets DDR bank CS bounds.
  275. */
  276. static void set_cs_bounds(short cs, long base, long size)
  277. {
  278. debug("Setting bounds %08x, %08x for cs %d\n", base, size, cs);
  279. if(size == 0){
  280. im->ddr.csbnds[cs].csbnds = 0x00000000;
  281. } else {
  282. im->ddr.csbnds[cs].csbnds =
  283. ((base >> CSBNDS_SA_SHIFT) & CSBNDS_SA) |
  284. (((base + size - 1) >> CSBNDS_EA_SHIFT) &
  285. CSBNDS_EA);
  286. }
  287. SYNC;
  288. }
  289. /**************************************************************************
  290. * Sets DDR banks CS configuration.
  291. * config == 0x00000000 disables the CS.
  292. */
  293. static void set_cs_config(short cs, long config)
  294. {
  295. debug("Setting config %08x for cs %d\n", config, cs);
  296. im->ddr.cs_config[cs] = config;
  297. SYNC;
  298. }
  299. /**************************************************************************
  300. * Sets DDR clocks, timings and configuration.
  301. */
  302. static void set_ddr_config(void) {
  303. /* clock control */
  304. im->ddr.sdram_clk_cntl = DDR_SDRAM_CLK_CNTL_SS_EN |
  305. DDR_SDRAM_CLK_CNTL_CLK_ADJUST_05;
  306. SYNC;
  307. /* timing configuration */
  308. im->ddr.timing_cfg_1 =
  309. (4 << TIMING_CFG1_PRETOACT_SHIFT) |
  310. (7 << TIMING_CFG1_ACTTOPRE_SHIFT) |
  311. (4 << TIMING_CFG1_ACTTORW_SHIFT) |
  312. (5 << TIMING_CFG1_REFREC_SHIFT) |
  313. (3 << TIMING_CFG1_WRREC_SHIFT) |
  314. (3 << TIMING_CFG1_ACTTOACT_SHIFT) |
  315. (1 << TIMING_CFG1_WRTORD_SHIFT) |
  316. (TIMING_CFG1_CASLAT & TIMING_CASLAT);
  317. im->ddr.timing_cfg_2 =
  318. TIMING_CFG2_CPO_DEF |
  319. (2 << TIMING_CFG2_WR_DATA_DELAY_SHIFT);
  320. SYNC;
  321. /* don't enable DDR controller yet */
  322. im->ddr.sdram_cfg =
  323. SDRAM_CFG_SREN |
  324. SDRAM_CFG_SDRAM_TYPE_DDR1;
  325. SYNC;
  326. /* Set SDRAM mode */
  327. im->ddr.sdram_mode =
  328. ((DDR_MODE_EXT_MODEREG | DDR_MODE_WEAK) <<
  329. SDRAM_MODE_ESD_SHIFT) |
  330. ((DDR_MODE_MODEREG | DDR_MODE_BLEN_4) <<
  331. SDRAM_MODE_SD_SHIFT) |
  332. ((DDR_MODE_CASLAT << SDRAM_MODE_SD_SHIFT) &
  333. MODE_CASLAT);
  334. SYNC;
  335. /* Set fast SDRAM refresh rate */
  336. im->ddr.sdram_interval =
  337. (DDR_REFINT_166MHZ_7US << SDRAM_INTERVAL_REFINT_SHIFT) |
  338. (DDR_BSTOPRE << SDRAM_INTERVAL_BSTOPRE_SHIFT);
  339. SYNC;
  340. /* Workaround for DDR6 Erratum
  341. * see MPC8349E Device Errata Rev.8, 2/2006
  342. * This workaround influences the MPC internal "input enables"
  343. * dependent on CAS latency and MPC revision. According to errata
  344. * sheet the internal reserved registers for this workaround are
  345. * not available from revision 2.0 and up.
  346. */
  347. /* Get REVID from register SPRIDR. Skip workaround if rev >= 2.0
  348. * (0x200)
  349. */
  350. if ((im->sysconf.spridr & SPRIDR_REVID) < 0x200) {
  351. /* There is a internal reserved register at IMMRBAR+0x2F00
  352. * which has to be written with a certain value defined by
  353. * errata sheet.
  354. */
  355. u32 *reserved_p = (u32 *)((u8 *)im + 0x2f00);
  356. #if defined(DDR_CASLAT_20)
  357. *reserved_p = 0x201c0000;
  358. #else
  359. *reserved_p = 0x202c0000;
  360. #endif
  361. }
  362. }