sdram_init.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * (C) Copyright 2001
  3. * Josh Huber <huber@mclx.com>, Mission Critical Linux, Inc.
  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. * adaption for the Marvell DB64460 Board
  25. * Ingo Assmus (ingo.assmus@keymile.com)
  26. *************************************************************************/
  27. /* sdram_init.c - automatic memory sizing */
  28. #include <common.h>
  29. #include <74xx_7xx.h>
  30. #include "../../Marvell/include/memory.h"
  31. #include "../../Marvell/include/pci.h"
  32. #include "../../Marvell/include/mv_gen_reg.h"
  33. #include <net.h>
  34. #include "eth.h"
  35. #include "mpsc.h"
  36. #include "../../Marvell/common/i2c.h"
  37. #include "64460.h"
  38. #include "mv_regs.h"
  39. DECLARE_GLOBAL_DATA_PTR;
  40. #undef DEBUG
  41. #define MAP_PCI
  42. #ifdef DEBUG
  43. #define DP(x) x
  44. #else
  45. #define DP(x)
  46. #endif
  47. int set_dfcdlInit (void); /* setup delay line of Mv64460 */
  48. int mvDmaIsChannelActive (int);
  49. int mvDmaSetMemorySpace (ulong, ulong, ulong, ulong, ulong);
  50. int mvDmaTransfer (int, ulong, ulong, ulong, ulong);
  51. #define D_CACHE_FLUSH_LINE(addr, offset) \
  52. { \
  53. __asm__ __volatile__ ("dcbf %0,%1" : : "r" (addr), "r" (offset)); \
  54. }
  55. int memory_map_bank (unsigned int bankNo,
  56. unsigned int bankBase, unsigned int bankLength)
  57. {
  58. #if defined (MAP_PCI) && defined (CONFIG_PCI)
  59. PCI_HOST host;
  60. #endif
  61. #ifdef DEBUG
  62. if (bankLength > 0) {
  63. printf ("mapping bank %d at %08x - %08x\n",
  64. bankNo, bankBase, bankBase + bankLength - 1);
  65. } else {
  66. printf ("unmapping bank %d\n", bankNo);
  67. }
  68. #endif
  69. memoryMapBank (bankNo, bankBase, bankLength);
  70. #if defined (MAP_PCI) && defined (CONFIG_PCI)
  71. for (host = PCI_HOST0; host <= PCI_HOST1; host++) {
  72. const int features =
  73. PREFETCH_ENABLE |
  74. DELAYED_READ_ENABLE |
  75. AGGRESSIVE_PREFETCH |
  76. READ_LINE_AGGRESSIVE_PREFETCH |
  77. READ_MULTI_AGGRESSIVE_PREFETCH |
  78. MAX_BURST_4 | PCI_NO_SWAP;
  79. pciMapMemoryBank (host, bankNo, bankBase, bankLength);
  80. pciSetRegionSnoopMode (host, bankNo, PCI_SNOOP_WB, bankBase,
  81. bankLength);
  82. pciSetRegionFeatures (host, bankNo, features, bankBase,
  83. bankLength);
  84. }
  85. #endif
  86. return 0;
  87. }
  88. /*
  89. * Check memory range for valid RAM. A simple memory test determines
  90. * the actually available RAM size between addresses `base' and
  91. * `base + maxsize'. Some (not all) hardware errors are detected:
  92. * - short between address lines
  93. * - short between data lines
  94. */
  95. long int dram_size (long int *base, long int maxsize)
  96. {
  97. volatile long int *addr, *b = base;
  98. long int cnt, val, save1, save2;
  99. #define STARTVAL (1<<20) /* start test at 1M */
  100. for (cnt = STARTVAL / sizeof (long); cnt < maxsize / sizeof (long);
  101. cnt <<= 1) {
  102. addr = base + cnt; /* pointer arith! */
  103. save1 = *addr; /* save contents of addr */
  104. save2 = *b; /* save contents of base */
  105. *addr = cnt; /* write cnt to addr */
  106. *b = 0; /* put null at base */
  107. /* check at base address */
  108. if ((*b) != 0) {
  109. *addr = save1; /* restore *addr */
  110. *b = save2; /* restore *b */
  111. return (0);
  112. }
  113. val = *addr; /* read *addr */
  114. val = *addr; /* read *addr */
  115. *addr = save1;
  116. *b = save2;
  117. if (val != cnt) {
  118. DP (printf
  119. ("Found %08x at Address %08x (failure)\n",
  120. (unsigned int) val, (unsigned int) addr));
  121. /* fix boundary condition.. STARTVAL means zero */
  122. if (cnt == STARTVAL / sizeof (long))
  123. cnt = 0;
  124. return (cnt * sizeof (long));
  125. }
  126. }
  127. return maxsize;
  128. }
  129. #define SDRAM_NORMAL 0x0
  130. #define SDRAM_PRECHARGE_ALL 0x1
  131. #define SDRAM_REFRESH_ALL 0x2
  132. #define SDRAM_MODE_REG_SETUP 0x3
  133. #define SDRAM_XTEN_MODE_REG_SETUP 0x4
  134. #define SDRAM_NOP 0x5
  135. #define SDRAM_SELF_REFRESH 0x7
  136. phys_size_t initdram (int board_type)
  137. {
  138. int tmp;
  139. int start;
  140. ulong size;
  141. ulong memSpaceAttr;
  142. ulong dest;
  143. /* first disable all banks */
  144. memory_map_bank(0, 0, 0);
  145. memory_map_bank(1, 0, 0);
  146. memory_map_bank(2, 0, 0);
  147. memory_map_bank(3, 0, 0);
  148. /* calibrate delay lines */
  149. set_dfcdlInit();
  150. GT_REG_WRITE(MV64460_SDRAM_OPERATION, SDRAM_NOP); /* 0x1418 */
  151. do {
  152. tmp = GTREGREAD(MV64460_SDRAM_OPERATION);
  153. } while(tmp != 0x0);
  154. /* SDRAM controller configuration */
  155. #ifdef CONFIG_MV64460_ECC
  156. GT_REG_WRITE(MV64460_SDRAM_CONFIG, 0x58201400); /* 0x1400 */
  157. #else
  158. GT_REG_WRITE(MV64460_SDRAM_CONFIG, 0x58200400); /* 0x1400 */
  159. #endif
  160. GT_REG_WRITE(MV64460_D_UNIT_CONTROL_LOW, 0xC3000540); /* 0x1404 */
  161. GT_REG_WRITE(MV64460_D_UNIT_CONTROL_HIGH, 0x0300F777); /* 0x1424 */
  162. GT_REG_WRITE(MV64460_SDRAM_TIMING_CONTROL_LOW, 0x01712220); /* 0x1408 */
  163. GT_REG_WRITE(MV64460_SDRAM_TIMING_CONTROL_HIGH, 0x0000005D); /* 0x140C */
  164. GT_REG_WRITE(MV64460_SDRAM_ADDR_CONTROL, 0x00000012); /* 0x1410 */
  165. GT_REG_WRITE(MV64460_SDRAM_OPEN_PAGES_CONTROL, 0x00000001); /* 0x1414 */
  166. /* SDRAM drive strength */
  167. GT_REG_WRITE(MV64460_SDRAM_ADDR_CTRL_PADS_CALIBRATION, 0x80000000); /* 0x14C0 */
  168. GT_REG_WRITE(MV64460_SDRAM_ADDR_CTRL_PADS_CALIBRATION, 0x80000008); /* 0x14C0 */
  169. GT_REG_WRITE(MV64460_SDRAM_DATA_PADS_CALIBRATION, 0x80000000); /* 0x14C4 */
  170. GT_REG_WRITE(MV64460_SDRAM_DATA_PADS_CALIBRATION, 0x80000008); /* 0x14C4 */
  171. /* setup SDRAM device registers */
  172. /* precharge all */
  173. GT_REG_WRITE(MV64460_SDRAM_OPERATION, SDRAM_PRECHARGE_ALL); /* 0x1418 */
  174. do {
  175. tmp = GTREGREAD(MV64460_SDRAM_OPERATION);
  176. } while(tmp != 0x0);
  177. /* enable DLL */
  178. GT_REG_WRITE(MV64460_EXTENDED_DRAM_MODE, 0x00000000); /* 0x1420 */
  179. GT_REG_WRITE(MV64460_SDRAM_OPERATION, SDRAM_XTEN_MODE_REG_SETUP); /* 0x1418 */
  180. do {
  181. tmp = GTREGREAD(MV64460_SDRAM_OPERATION);
  182. } while(tmp != 0x0);
  183. /* reset DLL */
  184. GT_REG_WRITE(MV64460_SDRAM_MODE, 0x00000132); /* 0x141C */
  185. GT_REG_WRITE(MV64460_SDRAM_OPERATION, SDRAM_MODE_REG_SETUP); /* 0x1418 */
  186. do {
  187. tmp = GTREGREAD(MV64460_SDRAM_OPERATION);
  188. } while(tmp != 0x0);
  189. /* precharge all */
  190. GT_REG_WRITE(MV64460_SDRAM_OPERATION, SDRAM_PRECHARGE_ALL); /* 0x1418 */
  191. do {
  192. tmp = GTREGREAD(MV64460_SDRAM_OPERATION);
  193. } while(tmp != 0x0);
  194. /* wait for 2 auto refresh commands */
  195. udelay(20);
  196. /* un-reset DLL */
  197. GT_REG_WRITE(MV64460_SDRAM_MODE, 0x00000032); /* 0x141C */
  198. GT_REG_WRITE(MV64460_SDRAM_OPERATION, SDRAM_MODE_REG_SETUP); /* 0x1418 */
  199. do {
  200. tmp = GTREGREAD(MV64460_SDRAM_OPERATION);
  201. } while(tmp != 0x0);
  202. /* wait 200 cycles */
  203. udelay(2); /* FIXME make this dynamic for the system clock */
  204. /* SDRAM init done */
  205. memory_map_bank(0, CONFIG_SYS_SDRAM_BASE, (256 << 20));
  206. #ifdef CONFIG_SYS_SDRAM1_BASE
  207. memory_map_bank(1, CONFIG_SYS_SDRAM1_BASE, (256 << 20));
  208. #endif
  209. /* DUNIT_MMASK: enable SnoopHitEn bit to avoid errata CPU-#4
  210. */
  211. tmp = GTREGREAD(MV64460_D_UNIT_MMASK); /* 0x14B0 */
  212. GT_REG_WRITE(MV64460_D_UNIT_MMASK, tmp | 0x2);
  213. start = (0 << 20);
  214. #ifdef CONFIG_P3M750
  215. size = (512 << 20);
  216. #elif defined (CONFIG_P3M7448)
  217. size = (128 << 20);
  218. #endif
  219. #ifdef CONFIG_MV64460_ECC
  220. memSpaceAttr = ((~(BIT0 << 0)) & 0xf) << 8;
  221. mvDmaSetMemorySpace (0, 0, memSpaceAttr, start, size);
  222. for (dest = start; dest < start + size; dest += _8M) {
  223. mvDmaTransfer (0, start, dest, _8M,
  224. BIT8 /*DMA_DTL_128BYTES */ |
  225. BIT3 /*DMA_HOLD_SOURCE_ADDR */ |
  226. BIT11 /*DMA_BLOCK_TRANSFER_MODE */ );
  227. while (mvDmaIsChannelActive (0));
  228. }
  229. #endif
  230. return (size);
  231. }
  232. void board_add_ram_info(int use_default)
  233. {
  234. u32 val;
  235. puts(" (CL=");
  236. switch ((GTREGREAD(MV64460_SDRAM_MODE) >> 4) & 0x7) {
  237. case 0x2:
  238. puts("2");
  239. break;
  240. case 0x3:
  241. puts("3");
  242. break;
  243. case 0x5:
  244. puts("1.5");
  245. break;
  246. case 0x6:
  247. puts("2.5");
  248. break;
  249. }
  250. val = GTREGREAD(MV64460_SDRAM_CONFIG);
  251. puts(", ECC ");
  252. if (val & 0x00001000)
  253. puts("enabled)");
  254. else
  255. puts("not enabled)");
  256. }
  257. /*
  258. * mvDmaIsChannelActive - Check if IDMA channel is active
  259. *
  260. * channel = IDMA channel number from 0 to 7
  261. */
  262. int mvDmaIsChannelActive (int channel)
  263. {
  264. ulong data;
  265. data = GTREGREAD (MV64460_DMA_CHANNEL0_CONTROL + 4 * channel);
  266. if (data & BIT14) /* activity status */
  267. return 1;
  268. return 0;
  269. }
  270. /*
  271. * mvDmaSetMemorySpace - Set a DMA memory window for the DMA's address decoding
  272. * map.
  273. *
  274. * memSpace = IDMA memory window number from 0 to 7
  275. * trg_if = Target interface:
  276. * 0x0 DRAM
  277. * 0x1 Device Bus
  278. * 0x2 Integrated SDRAM (or CPU bus 60x only)
  279. * 0x3 PCI0
  280. * 0x4 PCI1
  281. * attr = IDMA attributes (see MV datasheet)
  282. * base_addr = Sets up memory window for transfers
  283. *
  284. */
  285. int mvDmaSetMemorySpace (ulong memSpace,
  286. ulong trg_if,
  287. ulong attr, ulong base_addr, ulong size)
  288. {
  289. ulong temp;
  290. /* The base address must be aligned to the size. */
  291. if (base_addr % size != 0)
  292. return 0;
  293. if (size >= 0x10000) { /* 64K */
  294. size &= 0xffff0000;
  295. base_addr = (base_addr & 0xffff0000);
  296. /* Set the new attributes */
  297. GT_REG_WRITE (MV64460_DMA_BASE_ADDR_REG0 + memSpace * 8,
  298. (base_addr | trg_if | attr));
  299. GT_REG_WRITE ((MV64460_DMA_SIZE_REG0 + memSpace * 8),
  300. (size - 1) & 0xffff0000);
  301. temp = GTREGREAD (MV64460_DMA_BASE_ADDR_ENABLE_REG);
  302. GT_REG_WRITE (DMA_BASE_ADDR_ENABLE_REG,
  303. (temp & ~(BIT0 << memSpace)));
  304. return 1;
  305. }
  306. return 0;
  307. }
  308. /*
  309. * mvDmaTransfer - Transfer data from src_addr to dst_addr on one of the 4
  310. * DMA channels.
  311. *
  312. * channel = IDMA channel number from 0 to 3
  313. * destAddr = Destination address
  314. * sourceAddr = Source address
  315. * size = Size in bytes
  316. * command = See MV datasheet
  317. *
  318. */
  319. int mvDmaTransfer (int channel, ulong sourceAddr,
  320. ulong destAddr, ulong size, ulong command)
  321. {
  322. ulong engOffReg = 0; /* Engine Offset Register */
  323. if (size > 0xffff)
  324. command = command | BIT31; /* DMA_16M_DESCRIPTOR_MODE */
  325. command = command | ((command >> 6) & 0x7);
  326. engOffReg = channel * 4;
  327. GT_REG_WRITE (MV64460_DMA_CHANNEL0_BYTE_COUNT + engOffReg, size);
  328. GT_REG_WRITE (MV64460_DMA_CHANNEL0_SOURCE_ADDR + engOffReg, sourceAddr);
  329. GT_REG_WRITE (MV64460_DMA_CHANNEL0_DESTINATION_ADDR + engOffReg, destAddr);
  330. command = command |
  331. BIT12 | /* DMA_CHANNEL_ENABLE */
  332. BIT9; /* DMA_NON_CHAIN_MODE */
  333. /* Activate DMA channel By writting to mvDmaControlRegister */
  334. GT_REG_WRITE (MV64460_DMA_CHANNEL0_CONTROL + engOffReg, command);
  335. return 1;
  336. }
  337. /****************************************************************************************
  338. * SDRAM INIT *
  339. * This procedure detect all Sdram types: 64, 128, 256, 512 Mbit, 1Gbit and 2Gb *
  340. * This procedure fits only the Atlantis *
  341. * *
  342. ***************************************************************************************/
  343. /****************************************************************************************
  344. * DFCDL initialize MV643xx Design Considerations *
  345. * *
  346. ***************************************************************************************/
  347. int set_dfcdlInit (void)
  348. {
  349. int i;
  350. /* Values from MV64460 User Manual */
  351. unsigned int dfcdl_tbl[] = { 0x00000000, 0x00000001, 0x00000042, 0x00000083,
  352. 0x000000c4, 0x00000105, 0x00000146, 0x00000187,
  353. 0x000001c8, 0x00000209, 0x0000024a, 0x0000028b,
  354. 0x000002cc, 0x0000030d, 0x0000034e, 0x0000038f,
  355. 0x000003d0, 0x00000411, 0x00000452, 0x00000493,
  356. 0x000004d4, 0x00000515, 0x00000556, 0x00000597,
  357. 0x000005d8, 0x00000619, 0x0000065a, 0x0000069b,
  358. 0x000006dc, 0x0000071d, 0x0000075e, 0x0000079f,
  359. 0x000007e0, 0x00000821, 0x00000862, 0x000008a3,
  360. 0x000008e4, 0x00000925, 0x00000966, 0x000009a7,
  361. 0x000009e8, 0x00000a29, 0x00000a6a, 0x00000aab,
  362. 0x00000aec, 0x00000b2d, 0x00000b6e, 0x00000baf,
  363. 0x00000bf0, 0x00000c31, 0x00000c72, 0x00000cb3,
  364. 0x00000cf4, 0x00000d35, 0x00000d76, 0x00000db7,
  365. 0x00000df8, 0x00000e39, 0x00000e7a, 0x00000ebb,
  366. 0x00000efc, 0x00000f3d, 0x00000f7e, 0x00000fbf };
  367. for (i = 0; i < 64; i++)
  368. GT_REG_WRITE (SRAM_DATA0, dfcdl_tbl[i]);
  369. GT_REG_WRITE (DFCDL_CONFIG0, 0x00300000); /* enable dynamic delay line updating */
  370. return (0);
  371. }