sdram_init.c 12 KB

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