44x_spd_ddr2.c 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129
  1. /*
  2. * arch/powerpc/cpu/ppc4xx/44x_spd_ddr2.c
  3. * This SPD SDRAM detection code supports AMCC PPC44x cpu's with a
  4. * DDR2 controller (non Denali Core). Those currently are:
  5. *
  6. * 405: 405EX(r)
  7. * 440/460: 440SP/440SPe/460EX/460GT
  8. *
  9. * Copyright (c) 2008 Nuovation System Designs, LLC
  10. * Grant Erickson <gerickson@nuovations.com>
  11. * (C) Copyright 2007-2009
  12. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  13. *
  14. * COPYRIGHT AMCC CORPORATION 2004
  15. *
  16. * SPDX-License-Identifier: GPL-2.0+
  17. */
  18. /* define DEBUG for debugging output (obviously ;-)) */
  19. #if 0
  20. #define DEBUG
  21. #endif
  22. #include <common.h>
  23. #include <command.h>
  24. #include <asm/ppc4xx.h>
  25. #include <i2c.h>
  26. #include <asm/io.h>
  27. #include <asm/processor.h>
  28. #include <asm/mmu.h>
  29. #include <asm/cache.h>
  30. #include "ecc.h"
  31. #define PPC4xx_IBM_DDR2_DUMP_REGISTER(mnemonic) \
  32. do { \
  33. u32 data; \
  34. mfsdram(SDRAM_##mnemonic, data); \
  35. printf("%20s[%02x] = 0x%08X\n", \
  36. "SDRAM_" #mnemonic, SDRAM_##mnemonic, data); \
  37. } while (0)
  38. #define PPC4xx_IBM_DDR2_DUMP_MQ_REGISTER(mnemonic) \
  39. do { \
  40. u32 data; \
  41. data = mfdcr(SDRAM_##mnemonic); \
  42. printf("%20s[%02x] = 0x%08X\n", \
  43. "SDRAM_" #mnemonic, SDRAM_##mnemonic, data); \
  44. } while (0)
  45. static void update_rdcc(void)
  46. {
  47. u32 val;
  48. /*
  49. * Complete RDSS configuration as mentioned on page 7 of the AMCC
  50. * PowerPC440SP/SPe DDR2 application note:
  51. * "DDR1/DDR2 Initialization Sequence and Dynamic Tuning"
  52. *
  53. * Or item #10 "10. Complete RDSS configuration" in chapter
  54. * "22.2.9 SDRAM Initialization" of AMCC PPC460EX/EXr/GT users
  55. * manual.
  56. */
  57. mfsdram(SDRAM_RTSR, val);
  58. if ((val & SDRAM_RTSR_TRK1SM_MASK) == SDRAM_RTSR_TRK1SM_ATPLS1) {
  59. mfsdram(SDRAM_RDCC, val);
  60. if ((val & SDRAM_RDCC_RDSS_MASK) != SDRAM_RDCC_RDSS_T4) {
  61. val += 0x40000000;
  62. mtsdram(SDRAM_RDCC, val);
  63. }
  64. }
  65. }
  66. #if defined(CONFIG_440)
  67. /*
  68. * This DDR2 setup code can dynamically setup the TLB entries for the DDR2
  69. * memory region. Right now the cache should still be disabled in U-Boot
  70. * because of the EMAC driver, that need its buffer descriptor to be located
  71. * in non cached memory.
  72. *
  73. * If at some time this restriction doesn't apply anymore, just define
  74. * CONFIG_4xx_DCACHE in the board config file and this code should setup
  75. * everything correctly.
  76. */
  77. #ifdef CONFIG_4xx_DCACHE
  78. /* enable caching on SDRAM */
  79. #define MY_TLB_WORD2_I_ENABLE 0
  80. #else
  81. /* disable caching on SDRAM */
  82. #define MY_TLB_WORD2_I_ENABLE TLB_WORD2_I_ENABLE
  83. #endif /* CONFIG_4xx_DCACHE */
  84. void dcbz_area(u32 start_address, u32 num_bytes);
  85. #endif /* CONFIG_440 */
  86. #define MAXRANKS 4
  87. #define MAXBXCF 4
  88. #define MULDIV64(m1, m2, d) (u32)(((u64)(m1) * (u64)(m2)) / (u64)(d))
  89. /*-----------------------------------------------------------------------------+
  90. * sdram_memsize
  91. *-----------------------------------------------------------------------------*/
  92. phys_size_t sdram_memsize(void)
  93. {
  94. phys_size_t mem_size;
  95. unsigned long mcopt2;
  96. unsigned long mcstat;
  97. unsigned long mb0cf;
  98. unsigned long sdsz;
  99. unsigned long i;
  100. mem_size = 0;
  101. mfsdram(SDRAM_MCOPT2, mcopt2);
  102. mfsdram(SDRAM_MCSTAT, mcstat);
  103. /* DDR controller must be enabled and not in self-refresh. */
  104. /* Otherwise memsize is zero. */
  105. if (((mcopt2 & SDRAM_MCOPT2_DCEN_MASK) == SDRAM_MCOPT2_DCEN_ENABLE)
  106. && ((mcopt2 & SDRAM_MCOPT2_SREN_MASK) == SDRAM_MCOPT2_SREN_EXIT)
  107. && ((mcstat & (SDRAM_MCSTAT_MIC_MASK | SDRAM_MCSTAT_SRMS_MASK))
  108. == (SDRAM_MCSTAT_MIC_COMP | SDRAM_MCSTAT_SRMS_NOT_SF))) {
  109. for (i = 0; i < MAXBXCF; i++) {
  110. mfsdram(SDRAM_MB0CF + (i << 2), mb0cf);
  111. /* Banks enabled */
  112. if ((mb0cf & SDRAM_BXCF_M_BE_MASK) == SDRAM_BXCF_M_BE_ENABLE) {
  113. #if defined(CONFIG_440)
  114. sdsz = mfdcr_any(SDRAM_R0BAS + i) & SDRAM_RXBAS_SDSZ_MASK;
  115. #else
  116. sdsz = mb0cf & SDRAM_RXBAS_SDSZ_MASK;
  117. #endif
  118. switch(sdsz) {
  119. case SDRAM_RXBAS_SDSZ_8:
  120. mem_size+=8;
  121. break;
  122. case SDRAM_RXBAS_SDSZ_16:
  123. mem_size+=16;
  124. break;
  125. case SDRAM_RXBAS_SDSZ_32:
  126. mem_size+=32;
  127. break;
  128. case SDRAM_RXBAS_SDSZ_64:
  129. mem_size+=64;
  130. break;
  131. case SDRAM_RXBAS_SDSZ_128:
  132. mem_size+=128;
  133. break;
  134. case SDRAM_RXBAS_SDSZ_256:
  135. mem_size+=256;
  136. break;
  137. case SDRAM_RXBAS_SDSZ_512:
  138. mem_size+=512;
  139. break;
  140. case SDRAM_RXBAS_SDSZ_1024:
  141. mem_size+=1024;
  142. break;
  143. case SDRAM_RXBAS_SDSZ_2048:
  144. mem_size+=2048;
  145. break;
  146. case SDRAM_RXBAS_SDSZ_4096:
  147. mem_size+=4096;
  148. break;
  149. default:
  150. printf("WARNING: Unsupported bank size (SDSZ=0x%lx)!\n"
  151. , sdsz);
  152. mem_size=0;
  153. break;
  154. }
  155. }
  156. }
  157. }
  158. return mem_size << 20;
  159. }
  160. /*-----------------------------------------------------------------------------+
  161. * is_ecc_enabled
  162. *-----------------------------------------------------------------------------*/
  163. static unsigned long is_ecc_enabled(void)
  164. {
  165. unsigned long val;
  166. mfsdram(SDRAM_MCOPT1, val);
  167. return SDRAM_MCOPT1_MCHK_CHK_DECODE(val);
  168. }
  169. /*-----------------------------------------------------------------------------+
  170. * board_add_ram_info
  171. *-----------------------------------------------------------------------------*/
  172. void board_add_ram_info(int use_default)
  173. {
  174. PPC4xx_SYS_INFO board_cfg;
  175. u32 val;
  176. if (is_ecc_enabled())
  177. puts(" (ECC");
  178. else
  179. puts(" (ECC not");
  180. get_sys_info(&board_cfg);
  181. #if defined(CONFIG_405EX)
  182. val = board_cfg.freqPLB;
  183. #else
  184. mfsdr(SDR0_DDR0, val);
  185. val = MULDIV64((board_cfg.freqPLB), SDR0_DDR0_DDRM_DECODE(val), 1);
  186. #endif
  187. printf(" enabled, %d MHz", (val * 2) / 1000000);
  188. mfsdram(SDRAM_MMODE, val);
  189. val = (val & SDRAM_MMODE_DCL_MASK) >> 4;
  190. printf(", CL%d)", val);
  191. }
  192. #if defined(CONFIG_SPD_EEPROM)
  193. /*-----------------------------------------------------------------------------+
  194. * Defines
  195. *-----------------------------------------------------------------------------*/
  196. #define SDRAM_DDR1 1
  197. #define SDRAM_DDR2 2
  198. #define SDRAM_NONE 0
  199. #define MAXDIMMS 2
  200. #define MAX_SPD_BYTES 256 /* Max number of bytes on the DIMM's SPD EEPROM */
  201. #define ONE_BILLION 1000000000
  202. #define CMD_NOP (7 << 19)
  203. #define CMD_PRECHARGE (2 << 19)
  204. #define CMD_REFRESH (1 << 19)
  205. #define CMD_EMR (0 << 19)
  206. #define CMD_READ (5 << 19)
  207. #define CMD_WRITE (4 << 19)
  208. #define SELECT_MR (0 << 16)
  209. #define SELECT_EMR (1 << 16)
  210. #define SELECT_EMR2 (2 << 16)
  211. #define SELECT_EMR3 (3 << 16)
  212. /* MR */
  213. #define DLL_RESET 0x00000100
  214. #define WRITE_RECOV_2 (1 << 9)
  215. #define WRITE_RECOV_3 (2 << 9)
  216. #define WRITE_RECOV_4 (3 << 9)
  217. #define WRITE_RECOV_5 (4 << 9)
  218. #define WRITE_RECOV_6 (5 << 9)
  219. #define BURST_LEN_4 0x00000002
  220. /* EMR */
  221. #define ODT_0_OHM 0x00000000
  222. #define ODT_50_OHM 0x00000044
  223. #define ODT_75_OHM 0x00000004
  224. #define ODT_150_OHM 0x00000040
  225. #define ODS_FULL 0x00000000
  226. #define ODS_REDUCED 0x00000002
  227. #define OCD_CALIB_DEF 0x00000380
  228. /* defines for ODT (On Die Termination) of the 440SP(e) DDR2 controller */
  229. #define ODT_EB0R (0x80000000 >> 8)
  230. #define ODT_EB0W (0x80000000 >> 7)
  231. #define CALC_ODT_R(n) (ODT_EB0R << (n << 1))
  232. #define CALC_ODT_W(n) (ODT_EB0W << (n << 1))
  233. #define CALC_ODT_RW(n) (CALC_ODT_R(n) | CALC_ODT_W(n))
  234. /* Defines for the Read Cycle Delay test */
  235. #define NUMMEMTESTS 8
  236. #define NUMMEMWORDS 8
  237. #define NUMLOOPS 64 /* memory test loops */
  238. /*
  239. * Newer PPC's like 440SPe, 460EX/GT can be equipped with more than 2GB of SDRAM.
  240. * To support such configurations, we "only" map the first 2GB via the TLB's. We
  241. * need some free virtual address space for the remaining peripherals like, SoC
  242. * devices, FLASH etc.
  243. *
  244. * Note that ECC is currently not supported on configurations with more than 2GB
  245. * SDRAM. This is because we only map the first 2GB on such systems, and therefore
  246. * the ECC parity byte of the remaining area can't be written.
  247. */
  248. /*
  249. * Board-specific Platform code can reimplement spd_ddr_init_hang () if needed
  250. */
  251. void __spd_ddr_init_hang (void)
  252. {
  253. hang ();
  254. }
  255. void spd_ddr_init_hang (void) __attribute__((weak, alias("__spd_ddr_init_hang")));
  256. /*
  257. * To provide an interface for board specific config values in this common
  258. * DDR setup code, we implement he "weak" default functions here. They return
  259. * the default value back to the caller.
  260. *
  261. * Please see include/configs/yucca.h for an example fora board specific
  262. * implementation.
  263. */
  264. u32 __ddr_wrdtr(u32 default_val)
  265. {
  266. return default_val;
  267. }
  268. u32 ddr_wrdtr(u32) __attribute__((weak, alias("__ddr_wrdtr")));
  269. u32 __ddr_clktr(u32 default_val)
  270. {
  271. return default_val;
  272. }
  273. u32 ddr_clktr(u32) __attribute__((weak, alias("__ddr_clktr")));
  274. /* Private Structure Definitions */
  275. /* enum only to ease code for cas latency setting */
  276. typedef enum ddr_cas_id {
  277. DDR_CAS_2 = 20,
  278. DDR_CAS_2_5 = 25,
  279. DDR_CAS_3 = 30,
  280. DDR_CAS_4 = 40,
  281. DDR_CAS_5 = 50
  282. } ddr_cas_id_t;
  283. /*-----------------------------------------------------------------------------+
  284. * Prototypes
  285. *-----------------------------------------------------------------------------*/
  286. static void get_spd_info(unsigned long *dimm_populated,
  287. unsigned char *iic0_dimm_addr,
  288. unsigned long num_dimm_banks);
  289. static void check_mem_type(unsigned long *dimm_populated,
  290. unsigned char *iic0_dimm_addr,
  291. unsigned long num_dimm_banks);
  292. static void check_frequency(unsigned long *dimm_populated,
  293. unsigned char *iic0_dimm_addr,
  294. unsigned long num_dimm_banks);
  295. static void check_rank_number(unsigned long *dimm_populated,
  296. unsigned char *iic0_dimm_addr,
  297. unsigned long num_dimm_banks);
  298. static void check_voltage_type(unsigned long *dimm_populated,
  299. unsigned char *iic0_dimm_addr,
  300. unsigned long num_dimm_banks);
  301. static void program_memory_queue(unsigned long *dimm_populated,
  302. unsigned char *iic0_dimm_addr,
  303. unsigned long num_dimm_banks);
  304. static void program_codt(unsigned long *dimm_populated,
  305. unsigned char *iic0_dimm_addr,
  306. unsigned long num_dimm_banks);
  307. static void program_mode(unsigned long *dimm_populated,
  308. unsigned char *iic0_dimm_addr,
  309. unsigned long num_dimm_banks,
  310. ddr_cas_id_t *selected_cas,
  311. int *write_recovery);
  312. static void program_tr(unsigned long *dimm_populated,
  313. unsigned char *iic0_dimm_addr,
  314. unsigned long num_dimm_banks);
  315. static void program_rtr(unsigned long *dimm_populated,
  316. unsigned char *iic0_dimm_addr,
  317. unsigned long num_dimm_banks);
  318. static void program_bxcf(unsigned long *dimm_populated,
  319. unsigned char *iic0_dimm_addr,
  320. unsigned long num_dimm_banks);
  321. static void program_copt1(unsigned long *dimm_populated,
  322. unsigned char *iic0_dimm_addr,
  323. unsigned long num_dimm_banks);
  324. static void program_initplr(unsigned long *dimm_populated,
  325. unsigned char *iic0_dimm_addr,
  326. unsigned long num_dimm_banks,
  327. ddr_cas_id_t selected_cas,
  328. int write_recovery);
  329. #ifdef CONFIG_DDR_ECC
  330. static void program_ecc(unsigned long *dimm_populated,
  331. unsigned char *iic0_dimm_addr,
  332. unsigned long num_dimm_banks,
  333. unsigned long tlb_word2_i_value);
  334. #endif
  335. #if !defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION)
  336. static void program_DQS_calibration(unsigned long *dimm_populated,
  337. unsigned char *iic0_dimm_addr,
  338. unsigned long num_dimm_banks);
  339. #ifdef HARD_CODED_DQS /* calibration test with hardvalues */
  340. static void test(void);
  341. #else
  342. static void DQS_calibration_process(void);
  343. #endif
  344. #endif
  345. static unsigned char spd_read(uchar chip, uint addr)
  346. {
  347. unsigned char data[2];
  348. if (i2c_probe(chip) == 0)
  349. if (i2c_read(chip, addr, 1, data, 1) == 0)
  350. return data[0];
  351. return 0;
  352. }
  353. /*-----------------------------------------------------------------------------+
  354. * initdram. Initializes the 440SP Memory Queue and DDR SDRAM controller.
  355. * Note: This routine runs from flash with a stack set up in the chip's
  356. * sram space. It is important that the routine does not require .sbss, .bss or
  357. * .data sections. It also cannot call routines that require these sections.
  358. *-----------------------------------------------------------------------------*/
  359. /*-----------------------------------------------------------------------------
  360. * Function: initdram
  361. * Description: Configures SDRAM memory banks for DDR operation.
  362. * Auto Memory Configuration option reads the DDR SDRAM EEPROMs
  363. * via the IIC bus and then configures the DDR SDRAM memory
  364. * banks appropriately. If Auto Memory Configuration is
  365. * not used, it is assumed that no DIMM is plugged
  366. *-----------------------------------------------------------------------------*/
  367. phys_size_t initdram(int board_type)
  368. {
  369. unsigned char iic0_dimm_addr[] = SPD_EEPROM_ADDRESS;
  370. unsigned long dimm_populated[MAXDIMMS] = {SDRAM_NONE, SDRAM_NONE};
  371. unsigned long num_dimm_banks; /* on board dimm banks */
  372. unsigned long val;
  373. ddr_cas_id_t selected_cas = DDR_CAS_5; /* preset to silence compiler */
  374. int write_recovery;
  375. phys_size_t dram_size = 0;
  376. num_dimm_banks = sizeof(iic0_dimm_addr);
  377. /*------------------------------------------------------------------
  378. * Reset the DDR-SDRAM controller.
  379. *-----------------------------------------------------------------*/
  380. mtsdr(SDR0_SRST, SDR0_SRST0_DMC);
  381. mtsdr(SDR0_SRST, 0x00000000);
  382. /*
  383. * Make sure I2C controller is initialized
  384. * before continuing.
  385. */
  386. /* switch to correct I2C bus */
  387. i2c_set_bus_num(CONFIG_SYS_SPD_BUS_NUM);
  388. /*------------------------------------------------------------------
  389. * Clear out the serial presence detect buffers.
  390. * Perform IIC reads from the dimm. Fill in the spds.
  391. * Check to see if the dimm slots are populated
  392. *-----------------------------------------------------------------*/
  393. get_spd_info(dimm_populated, iic0_dimm_addr, num_dimm_banks);
  394. /*------------------------------------------------------------------
  395. * Check the memory type for the dimms plugged.
  396. *-----------------------------------------------------------------*/
  397. check_mem_type(dimm_populated, iic0_dimm_addr, num_dimm_banks);
  398. /*------------------------------------------------------------------
  399. * Check the frequency supported for the dimms plugged.
  400. *-----------------------------------------------------------------*/
  401. check_frequency(dimm_populated, iic0_dimm_addr, num_dimm_banks);
  402. /*------------------------------------------------------------------
  403. * Check the total rank number.
  404. *-----------------------------------------------------------------*/
  405. check_rank_number(dimm_populated, iic0_dimm_addr, num_dimm_banks);
  406. /*------------------------------------------------------------------
  407. * Check the voltage type for the dimms plugged.
  408. *-----------------------------------------------------------------*/
  409. check_voltage_type(dimm_populated, iic0_dimm_addr, num_dimm_banks);
  410. /*------------------------------------------------------------------
  411. * Program SDRAM controller options 2 register
  412. * Except Enabling of the memory controller.
  413. *-----------------------------------------------------------------*/
  414. mfsdram(SDRAM_MCOPT2, val);
  415. mtsdram(SDRAM_MCOPT2,
  416. (val &
  417. ~(SDRAM_MCOPT2_SREN_MASK | SDRAM_MCOPT2_PMEN_MASK |
  418. SDRAM_MCOPT2_IPTR_MASK | SDRAM_MCOPT2_XSRP_MASK |
  419. SDRAM_MCOPT2_ISIE_MASK))
  420. | (SDRAM_MCOPT2_SREN_ENTER | SDRAM_MCOPT2_PMEN_DISABLE |
  421. SDRAM_MCOPT2_IPTR_IDLE | SDRAM_MCOPT2_XSRP_ALLOW |
  422. SDRAM_MCOPT2_ISIE_ENABLE));
  423. /*------------------------------------------------------------------
  424. * Program SDRAM controller options 1 register
  425. * Note: Does not enable the memory controller.
  426. *-----------------------------------------------------------------*/
  427. program_copt1(dimm_populated, iic0_dimm_addr, num_dimm_banks);
  428. /*------------------------------------------------------------------
  429. * Set the SDRAM Controller On Die Termination Register
  430. *-----------------------------------------------------------------*/
  431. program_codt(dimm_populated, iic0_dimm_addr, num_dimm_banks);
  432. /*------------------------------------------------------------------
  433. * Program SDRAM refresh register.
  434. *-----------------------------------------------------------------*/
  435. program_rtr(dimm_populated, iic0_dimm_addr, num_dimm_banks);
  436. /*------------------------------------------------------------------
  437. * Program SDRAM mode register.
  438. *-----------------------------------------------------------------*/
  439. program_mode(dimm_populated, iic0_dimm_addr, num_dimm_banks,
  440. &selected_cas, &write_recovery);
  441. /*------------------------------------------------------------------
  442. * Set the SDRAM Write Data/DM/DQS Clock Timing Reg
  443. *-----------------------------------------------------------------*/
  444. mfsdram(SDRAM_WRDTR, val);
  445. mtsdram(SDRAM_WRDTR, (val & ~(SDRAM_WRDTR_LLWP_MASK | SDRAM_WRDTR_WTR_MASK)) |
  446. ddr_wrdtr(SDRAM_WRDTR_LLWP_1_CYC | SDRAM_WRDTR_WTR_90_DEG_ADV));
  447. /*------------------------------------------------------------------
  448. * Set the SDRAM Clock Timing Register
  449. *-----------------------------------------------------------------*/
  450. mfsdram(SDRAM_CLKTR, val);
  451. mtsdram(SDRAM_CLKTR, (val & ~SDRAM_CLKTR_CLKP_MASK) |
  452. ddr_clktr(SDRAM_CLKTR_CLKP_0_DEG));
  453. /*------------------------------------------------------------------
  454. * Program the BxCF registers.
  455. *-----------------------------------------------------------------*/
  456. program_bxcf(dimm_populated, iic0_dimm_addr, num_dimm_banks);
  457. /*------------------------------------------------------------------
  458. * Program SDRAM timing registers.
  459. *-----------------------------------------------------------------*/
  460. program_tr(dimm_populated, iic0_dimm_addr, num_dimm_banks);
  461. /*------------------------------------------------------------------
  462. * Set the Extended Mode register
  463. *-----------------------------------------------------------------*/
  464. mfsdram(SDRAM_MEMODE, val);
  465. mtsdram(SDRAM_MEMODE,
  466. (val & ~(SDRAM_MEMODE_DIC_MASK | SDRAM_MEMODE_DLL_MASK |
  467. SDRAM_MEMODE_RTT_MASK | SDRAM_MEMODE_DQS_MASK)) |
  468. (SDRAM_MEMODE_DIC_NORMAL | SDRAM_MEMODE_DLL_ENABLE
  469. | SDRAM_MEMODE_RTT_150OHM | SDRAM_MEMODE_DQS_ENABLE));
  470. /*------------------------------------------------------------------
  471. * Program Initialization preload registers.
  472. *-----------------------------------------------------------------*/
  473. program_initplr(dimm_populated, iic0_dimm_addr, num_dimm_banks,
  474. selected_cas, write_recovery);
  475. /*------------------------------------------------------------------
  476. * Delay to ensure 200usec have elapsed since reset.
  477. *-----------------------------------------------------------------*/
  478. udelay(400);
  479. /*------------------------------------------------------------------
  480. * Set the memory queue core base addr.
  481. *-----------------------------------------------------------------*/
  482. program_memory_queue(dimm_populated, iic0_dimm_addr, num_dimm_banks);
  483. /*------------------------------------------------------------------
  484. * Program SDRAM controller options 2 register
  485. * Enable the memory controller.
  486. *-----------------------------------------------------------------*/
  487. mfsdram(SDRAM_MCOPT2, val);
  488. mtsdram(SDRAM_MCOPT2,
  489. (val & ~(SDRAM_MCOPT2_SREN_MASK | SDRAM_MCOPT2_DCEN_MASK |
  490. SDRAM_MCOPT2_IPTR_MASK | SDRAM_MCOPT2_ISIE_MASK)) |
  491. SDRAM_MCOPT2_IPTR_EXECUTE);
  492. /*------------------------------------------------------------------
  493. * Wait for IPTR_EXECUTE init sequence to complete.
  494. *-----------------------------------------------------------------*/
  495. do {
  496. mfsdram(SDRAM_MCSTAT, val);
  497. } while ((val & SDRAM_MCSTAT_MIC_MASK) == SDRAM_MCSTAT_MIC_NOTCOMP);
  498. /* enable the controller only after init sequence completes */
  499. mfsdram(SDRAM_MCOPT2, val);
  500. mtsdram(SDRAM_MCOPT2, (val | SDRAM_MCOPT2_DCEN_ENABLE));
  501. /* Make sure delay-line calibration is done before proceeding */
  502. do {
  503. mfsdram(SDRAM_DLCR, val);
  504. } while (!(val & SDRAM_DLCR_DLCS_COMPLETE));
  505. /* get installed memory size */
  506. dram_size = sdram_memsize();
  507. /*
  508. * Limit size to 2GB
  509. */
  510. if (dram_size > CONFIG_MAX_MEM_MAPPED)
  511. dram_size = CONFIG_MAX_MEM_MAPPED;
  512. /* and program tlb entries for this size (dynamic) */
  513. /*
  514. * Program TLB entries with caches enabled, for best performace
  515. * while auto-calibrating and ECC generation
  516. */
  517. program_tlb(0, 0, dram_size, 0);
  518. /*------------------------------------------------------------------
  519. * DQS calibration.
  520. *-----------------------------------------------------------------*/
  521. #if defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION)
  522. DQS_autocalibration();
  523. #else
  524. program_DQS_calibration(dimm_populated, iic0_dimm_addr, num_dimm_banks);
  525. #endif
  526. /*
  527. * Now complete RDSS configuration as mentioned on page 7 of the AMCC
  528. * PowerPC440SP/SPe DDR2 application note:
  529. * "DDR1/DDR2 Initialization Sequence and Dynamic Tuning"
  530. */
  531. update_rdcc();
  532. #ifdef CONFIG_DDR_ECC
  533. /*------------------------------------------------------------------
  534. * If ecc is enabled, initialize the parity bits.
  535. *-----------------------------------------------------------------*/
  536. program_ecc(dimm_populated, iic0_dimm_addr, num_dimm_banks, 0);
  537. #endif
  538. /*
  539. * Flush the dcache before removing the TLB with caches
  540. * enabled. Otherwise this might lead to problems later on,
  541. * e.g. while booting Linux (as seen on ICON-440SPe).
  542. */
  543. flush_dcache();
  544. /*
  545. * Now after initialization (auto-calibration and ECC generation)
  546. * remove the TLB entries with caches enabled and program again with
  547. * desired cache functionality
  548. */
  549. remove_tlb(0, dram_size);
  550. program_tlb(0, 0, dram_size, MY_TLB_WORD2_I_ENABLE);
  551. ppc4xx_ibm_ddr2_register_dump();
  552. /*
  553. * Clear potential errors resulting from auto-calibration.
  554. * If not done, then we could get an interrupt later on when
  555. * exceptions are enabled.
  556. */
  557. set_mcsr(get_mcsr());
  558. return sdram_memsize();
  559. }
  560. static void get_spd_info(unsigned long *dimm_populated,
  561. unsigned char *iic0_dimm_addr,
  562. unsigned long num_dimm_banks)
  563. {
  564. unsigned long dimm_num;
  565. unsigned long dimm_found;
  566. unsigned char num_of_bytes;
  567. unsigned char total_size;
  568. dimm_found = false;
  569. for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
  570. num_of_bytes = 0;
  571. total_size = 0;
  572. num_of_bytes = spd_read(iic0_dimm_addr[dimm_num], 0);
  573. debug("\nspd_read(0x%x) returned %d\n",
  574. iic0_dimm_addr[dimm_num], num_of_bytes);
  575. total_size = spd_read(iic0_dimm_addr[dimm_num], 1);
  576. debug("spd_read(0x%x) returned %d\n",
  577. iic0_dimm_addr[dimm_num], total_size);
  578. if ((num_of_bytes != 0) && (total_size != 0)) {
  579. dimm_populated[dimm_num] = true;
  580. dimm_found = true;
  581. debug("DIMM slot %lu: populated\n", dimm_num);
  582. } else {
  583. dimm_populated[dimm_num] = false;
  584. debug("DIMM slot %lu: Not populated\n", dimm_num);
  585. }
  586. }
  587. if (dimm_found == false) {
  588. printf("ERROR - No memory installed. Install a DDR-SDRAM DIMM.\n\n");
  589. spd_ddr_init_hang ();
  590. }
  591. }
  592. /*------------------------------------------------------------------
  593. * For the memory DIMMs installed, this routine verifies that they
  594. * really are DDR specific DIMMs.
  595. *-----------------------------------------------------------------*/
  596. static void check_mem_type(unsigned long *dimm_populated,
  597. unsigned char *iic0_dimm_addr,
  598. unsigned long num_dimm_banks)
  599. {
  600. unsigned long dimm_num;
  601. unsigned long dimm_type;
  602. for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
  603. if (dimm_populated[dimm_num] == true) {
  604. dimm_type = spd_read(iic0_dimm_addr[dimm_num], 2);
  605. switch (dimm_type) {
  606. case 1:
  607. printf("ERROR: Standard Fast Page Mode DRAM DIMM detected in "
  608. "slot %d.\n", (unsigned int)dimm_num);
  609. printf("Only DDR and DDR2 SDRAM DIMMs are supported.\n");
  610. printf("Replace the DIMM module with a supported DIMM.\n\n");
  611. spd_ddr_init_hang ();
  612. break;
  613. case 2:
  614. printf("ERROR: EDO DIMM detected in slot %d.\n",
  615. (unsigned int)dimm_num);
  616. printf("Only DDR and DDR2 SDRAM DIMMs are supported.\n");
  617. printf("Replace the DIMM module with a supported DIMM.\n\n");
  618. spd_ddr_init_hang ();
  619. break;
  620. case 3:
  621. printf("ERROR: Pipelined Nibble DIMM detected in slot %d.\n",
  622. (unsigned int)dimm_num);
  623. printf("Only DDR and DDR2 SDRAM DIMMs are supported.\n");
  624. printf("Replace the DIMM module with a supported DIMM.\n\n");
  625. spd_ddr_init_hang ();
  626. break;
  627. case 4:
  628. printf("ERROR: SDRAM DIMM detected in slot %d.\n",
  629. (unsigned int)dimm_num);
  630. printf("Only DDR and DDR2 SDRAM DIMMs are supported.\n");
  631. printf("Replace the DIMM module with a supported DIMM.\n\n");
  632. spd_ddr_init_hang ();
  633. break;
  634. case 5:
  635. printf("ERROR: Multiplexed ROM DIMM detected in slot %d.\n",
  636. (unsigned int)dimm_num);
  637. printf("Only DDR and DDR2 SDRAM DIMMs are supported.\n");
  638. printf("Replace the DIMM module with a supported DIMM.\n\n");
  639. spd_ddr_init_hang ();
  640. break;
  641. case 6:
  642. printf("ERROR: SGRAM DIMM detected in slot %d.\n",
  643. (unsigned int)dimm_num);
  644. printf("Only DDR and DDR2 SDRAM DIMMs are supported.\n");
  645. printf("Replace the DIMM module with a supported DIMM.\n\n");
  646. spd_ddr_init_hang ();
  647. break;
  648. case 7:
  649. debug("DIMM slot %lu: DDR1 SDRAM detected\n", dimm_num);
  650. dimm_populated[dimm_num] = SDRAM_DDR1;
  651. break;
  652. case 8:
  653. debug("DIMM slot %lu: DDR2 SDRAM detected\n", dimm_num);
  654. dimm_populated[dimm_num] = SDRAM_DDR2;
  655. break;
  656. default:
  657. printf("ERROR: Unknown DIMM detected in slot %d.\n",
  658. (unsigned int)dimm_num);
  659. printf("Only DDR1 and DDR2 SDRAM DIMMs are supported.\n");
  660. printf("Replace the DIMM module with a supported DIMM.\n\n");
  661. spd_ddr_init_hang ();
  662. break;
  663. }
  664. }
  665. }
  666. for (dimm_num = 1; dimm_num < num_dimm_banks; dimm_num++) {
  667. if ((dimm_populated[dimm_num-1] != SDRAM_NONE)
  668. && (dimm_populated[dimm_num] != SDRAM_NONE)
  669. && (dimm_populated[dimm_num-1] != dimm_populated[dimm_num])) {
  670. printf("ERROR: DIMM's DDR1 and DDR2 type can not be mixed.\n");
  671. spd_ddr_init_hang ();
  672. }
  673. }
  674. }
  675. /*------------------------------------------------------------------
  676. * For the memory DIMMs installed, this routine verifies that
  677. * frequency previously calculated is supported.
  678. *-----------------------------------------------------------------*/
  679. static void check_frequency(unsigned long *dimm_populated,
  680. unsigned char *iic0_dimm_addr,
  681. unsigned long num_dimm_banks)
  682. {
  683. unsigned long dimm_num;
  684. unsigned long tcyc_reg;
  685. unsigned long cycle_time;
  686. unsigned long calc_cycle_time;
  687. unsigned long sdram_freq;
  688. unsigned long sdr_ddrpll;
  689. PPC4xx_SYS_INFO board_cfg;
  690. /*------------------------------------------------------------------
  691. * Get the board configuration info.
  692. *-----------------------------------------------------------------*/
  693. get_sys_info(&board_cfg);
  694. mfsdr(SDR0_DDR0, sdr_ddrpll);
  695. sdram_freq = ((board_cfg.freqPLB) * SDR0_DDR0_DDRM_DECODE(sdr_ddrpll));
  696. /*
  697. * calc_cycle_time is calculated from DDR frequency set by board/chip
  698. * and is expressed in multiple of 10 picoseconds
  699. * to match the way DIMM cycle time is calculated below.
  700. */
  701. calc_cycle_time = MULDIV64(ONE_BILLION, 100, sdram_freq);
  702. for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
  703. if (dimm_populated[dimm_num] != SDRAM_NONE) {
  704. tcyc_reg = spd_read(iic0_dimm_addr[dimm_num], 9);
  705. /*
  706. * Byte 9, Cycle time for CAS Latency=X, is split into two nibbles:
  707. * the higher order nibble (bits 4-7) designates the cycle time
  708. * to a granularity of 1ns;
  709. * the value presented by the lower order nibble (bits 0-3)
  710. * has a granularity of .1ns and is added to the value designated
  711. * by the higher nibble. In addition, four lines of the lower order
  712. * nibble are assigned to support +.25,+.33, +.66 and +.75.
  713. */
  714. /* Convert from hex to decimal */
  715. if ((tcyc_reg & 0x0F) == 0x0D)
  716. cycle_time = (((tcyc_reg & 0xF0) >> 4) * 100) + 75;
  717. else if ((tcyc_reg & 0x0F) == 0x0C)
  718. cycle_time = (((tcyc_reg & 0xF0) >> 4) * 100) + 66;
  719. else if ((tcyc_reg & 0x0F) == 0x0B)
  720. cycle_time = (((tcyc_reg & 0xF0) >> 4) * 100) + 33;
  721. else if ((tcyc_reg & 0x0F) == 0x0A)
  722. cycle_time = (((tcyc_reg & 0xF0) >> 4) * 100) + 25;
  723. else
  724. cycle_time = (((tcyc_reg & 0xF0) >> 4) * 100) +
  725. ((tcyc_reg & 0x0F)*10);
  726. debug("cycle_time=%lu [10 picoseconds]\n", cycle_time);
  727. if (cycle_time > (calc_cycle_time + 10)) {
  728. /*
  729. * the provided sdram cycle_time is too small
  730. * for the available DIMM cycle_time.
  731. * The additionnal 100ps is here to accept a small incertainty.
  732. */
  733. printf("ERROR: DRAM DIMM detected with cycle_time %d ps in "
  734. "slot %d \n while calculated cycle time is %d ps.\n",
  735. (unsigned int)(cycle_time*10),
  736. (unsigned int)dimm_num,
  737. (unsigned int)(calc_cycle_time*10));
  738. printf("Replace the DIMM, or change DDR frequency via "
  739. "strapping bits.\n\n");
  740. spd_ddr_init_hang ();
  741. }
  742. }
  743. }
  744. }
  745. /*------------------------------------------------------------------
  746. * For the memory DIMMs installed, this routine verifies two
  747. * ranks/banks maximum are availables.
  748. *-----------------------------------------------------------------*/
  749. static void check_rank_number(unsigned long *dimm_populated,
  750. unsigned char *iic0_dimm_addr,
  751. unsigned long num_dimm_banks)
  752. {
  753. unsigned long dimm_num;
  754. unsigned long dimm_rank;
  755. unsigned long total_rank = 0;
  756. for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
  757. if (dimm_populated[dimm_num] != SDRAM_NONE) {
  758. dimm_rank = spd_read(iic0_dimm_addr[dimm_num], 5);
  759. if (((unsigned long)spd_read(iic0_dimm_addr[dimm_num], 2)) == 0x08)
  760. dimm_rank = (dimm_rank & 0x0F) +1;
  761. else
  762. dimm_rank = dimm_rank & 0x0F;
  763. if (dimm_rank > MAXRANKS) {
  764. printf("ERROR: DRAM DIMM detected with %lu ranks in "
  765. "slot %lu is not supported.\n", dimm_rank, dimm_num);
  766. printf("Only %d ranks are supported for all DIMM.\n", MAXRANKS);
  767. printf("Replace the DIMM module with a supported DIMM.\n\n");
  768. spd_ddr_init_hang ();
  769. } else
  770. total_rank += dimm_rank;
  771. }
  772. if (total_rank > MAXRANKS) {
  773. printf("ERROR: DRAM DIMM detected with a total of %d ranks "
  774. "for all slots.\n", (unsigned int)total_rank);
  775. printf("Only %d ranks are supported for all DIMM.\n", MAXRANKS);
  776. printf("Remove one of the DIMM modules.\n\n");
  777. spd_ddr_init_hang ();
  778. }
  779. }
  780. }
  781. /*------------------------------------------------------------------
  782. * only support 2.5V modules.
  783. * This routine verifies this.
  784. *-----------------------------------------------------------------*/
  785. static void check_voltage_type(unsigned long *dimm_populated,
  786. unsigned char *iic0_dimm_addr,
  787. unsigned long num_dimm_banks)
  788. {
  789. unsigned long dimm_num;
  790. unsigned long voltage_type;
  791. for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
  792. if (dimm_populated[dimm_num] != SDRAM_NONE) {
  793. voltage_type = spd_read(iic0_dimm_addr[dimm_num], 8);
  794. switch (voltage_type) {
  795. case 0x00:
  796. printf("ERROR: Only DIMMs DDR 2.5V or DDR2 1.8V are supported.\n");
  797. printf("This DIMM is 5.0 Volt/TTL.\n");
  798. printf("Replace the DIMM module in slot %d with a supported DIMM.\n\n",
  799. (unsigned int)dimm_num);
  800. spd_ddr_init_hang ();
  801. break;
  802. case 0x01:
  803. printf("ERROR: Only DIMMs DDR 2.5V or DDR2 1.8V are supported.\n");
  804. printf("This DIMM is LVTTL.\n");
  805. printf("Replace the DIMM module in slot %d with a supported DIMM.\n\n",
  806. (unsigned int)dimm_num);
  807. spd_ddr_init_hang ();
  808. break;
  809. case 0x02:
  810. printf("ERROR: Only DIMMs DDR 2.5V or DDR2 1.8V are supported.\n");
  811. printf("This DIMM is 1.5 Volt.\n");
  812. printf("Replace the DIMM module in slot %d with a supported DIMM.\n\n",
  813. (unsigned int)dimm_num);
  814. spd_ddr_init_hang ();
  815. break;
  816. case 0x03:
  817. printf("ERROR: Only DIMMs DDR 2.5V or DDR2 1.8V are supported.\n");
  818. printf("This DIMM is 3.3 Volt/TTL.\n");
  819. printf("Replace the DIMM module in slot %d with a supported DIMM.\n\n",
  820. (unsigned int)dimm_num);
  821. spd_ddr_init_hang ();
  822. break;
  823. case 0x04:
  824. /* 2.5 Voltage only for DDR1 */
  825. break;
  826. case 0x05:
  827. /* 1.8 Voltage only for DDR2 */
  828. break;
  829. default:
  830. printf("ERROR: Only DIMMs DDR 2.5V or DDR2 1.8V are supported.\n");
  831. printf("Replace the DIMM module in slot %d with a supported DIMM.\n\n",
  832. (unsigned int)dimm_num);
  833. spd_ddr_init_hang ();
  834. break;
  835. }
  836. }
  837. }
  838. }
  839. /*-----------------------------------------------------------------------------+
  840. * program_copt1.
  841. *-----------------------------------------------------------------------------*/
  842. static void program_copt1(unsigned long *dimm_populated,
  843. unsigned char *iic0_dimm_addr,
  844. unsigned long num_dimm_banks)
  845. {
  846. unsigned long dimm_num;
  847. unsigned long mcopt1;
  848. unsigned long ecc_enabled;
  849. unsigned long ecc = 0;
  850. unsigned long data_width = 0;
  851. unsigned long dimm_32bit;
  852. unsigned long dimm_64bit;
  853. unsigned long registered = 0;
  854. unsigned long attribute = 0;
  855. unsigned long buf0, buf1; /* TODO: code to be changed for IOP1.6 to support 4 DIMMs */
  856. unsigned long bankcount;
  857. unsigned long val;
  858. #ifdef CONFIG_DDR_ECC
  859. ecc_enabled = true;
  860. #else
  861. ecc_enabled = false;
  862. #endif
  863. dimm_32bit = false;
  864. dimm_64bit = false;
  865. buf0 = false;
  866. buf1 = false;
  867. /*------------------------------------------------------------------
  868. * Set memory controller options reg 1, SDRAM_MCOPT1.
  869. *-----------------------------------------------------------------*/
  870. mfsdram(SDRAM_MCOPT1, val);
  871. mcopt1 = val & ~(SDRAM_MCOPT1_MCHK_MASK | SDRAM_MCOPT1_RDEN_MASK |
  872. SDRAM_MCOPT1_PMU_MASK | SDRAM_MCOPT1_DMWD_MASK |
  873. SDRAM_MCOPT1_UIOS_MASK | SDRAM_MCOPT1_BCNT_MASK |
  874. SDRAM_MCOPT1_DDR_TYPE_MASK | SDRAM_MCOPT1_RWOO_MASK |
  875. SDRAM_MCOPT1_WOOO_MASK | SDRAM_MCOPT1_DCOO_MASK |
  876. SDRAM_MCOPT1_DREF_MASK);
  877. mcopt1 |= SDRAM_MCOPT1_QDEP;
  878. mcopt1 |= SDRAM_MCOPT1_PMU_OPEN;
  879. mcopt1 |= SDRAM_MCOPT1_RWOO_DISABLED;
  880. mcopt1 |= SDRAM_MCOPT1_WOOO_DISABLED;
  881. mcopt1 |= SDRAM_MCOPT1_DCOO_DISABLED;
  882. mcopt1 |= SDRAM_MCOPT1_DREF_NORMAL;
  883. for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
  884. if (dimm_populated[dimm_num] != SDRAM_NONE) {
  885. /* test ecc support */
  886. ecc = (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 11);
  887. if (ecc != 0x02) /* ecc not supported */
  888. ecc_enabled = false;
  889. /* test bank count */
  890. bankcount = (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 17);
  891. if (bankcount == 0x04) /* bank count = 4 */
  892. mcopt1 |= SDRAM_MCOPT1_4_BANKS;
  893. else /* bank count = 8 */
  894. mcopt1 |= SDRAM_MCOPT1_8_BANKS;
  895. /* test for buffered/unbuffered, registered, differential clocks */
  896. registered = (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 20);
  897. attribute = (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 21);
  898. /* TODO: code to be changed for IOP1.6 to support 4 DIMMs */
  899. if (dimm_num == 0) {
  900. if (dimm_populated[dimm_num] == SDRAM_DDR1) /* DDR1 type */
  901. mcopt1 |= SDRAM_MCOPT1_DDR1_TYPE;
  902. if (dimm_populated[dimm_num] == SDRAM_DDR2) /* DDR2 type */
  903. mcopt1 |= SDRAM_MCOPT1_DDR2_TYPE;
  904. if (registered == 1) { /* DDR2 always buffered */
  905. /* TODO: what about above comments ? */
  906. mcopt1 |= SDRAM_MCOPT1_RDEN;
  907. buf0 = true;
  908. } else {
  909. /* TODO: the mask 0x02 doesn't match Samsung def for byte 21. */
  910. if ((attribute & 0x02) == 0x00) {
  911. /* buffered not supported */
  912. buf0 = false;
  913. } else {
  914. mcopt1 |= SDRAM_MCOPT1_RDEN;
  915. buf0 = true;
  916. }
  917. }
  918. }
  919. else if (dimm_num == 1) {
  920. if (dimm_populated[dimm_num] == SDRAM_DDR1) /* DDR1 type */
  921. mcopt1 |= SDRAM_MCOPT1_DDR1_TYPE;
  922. if (dimm_populated[dimm_num] == SDRAM_DDR2) /* DDR2 type */
  923. mcopt1 |= SDRAM_MCOPT1_DDR2_TYPE;
  924. if (registered == 1) {
  925. /* DDR2 always buffered */
  926. mcopt1 |= SDRAM_MCOPT1_RDEN;
  927. buf1 = true;
  928. } else {
  929. if ((attribute & 0x02) == 0x00) {
  930. /* buffered not supported */
  931. buf1 = false;
  932. } else {
  933. mcopt1 |= SDRAM_MCOPT1_RDEN;
  934. buf1 = true;
  935. }
  936. }
  937. }
  938. /* Note that for DDR2 the byte 7 is reserved, but OK to keep code as is. */
  939. data_width = (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 6) +
  940. (((unsigned long)spd_read(iic0_dimm_addr[dimm_num], 7)) << 8);
  941. switch (data_width) {
  942. case 72:
  943. case 64:
  944. dimm_64bit = true;
  945. break;
  946. case 40:
  947. case 32:
  948. dimm_32bit = true;
  949. break;
  950. default:
  951. printf("WARNING: Detected a DIMM with a data width of %lu bits.\n",
  952. data_width);
  953. printf("Only DIMMs with 32 or 64 bit DDR-SDRAM widths are supported.\n");
  954. break;
  955. }
  956. }
  957. }
  958. /* verify matching properties */
  959. if ((dimm_populated[0] != SDRAM_NONE) && (dimm_populated[1] != SDRAM_NONE)) {
  960. if (buf0 != buf1) {
  961. printf("ERROR: DIMM's buffered/unbuffered, registered, clocking don't match.\n");
  962. spd_ddr_init_hang ();
  963. }
  964. }
  965. if ((dimm_64bit == true) && (dimm_32bit == true)) {
  966. printf("ERROR: Cannot mix 32 bit and 64 bit DDR-SDRAM DIMMs together.\n");
  967. spd_ddr_init_hang ();
  968. } else if ((dimm_64bit == true) && (dimm_32bit == false)) {
  969. mcopt1 |= SDRAM_MCOPT1_DMWD_64;
  970. } else if ((dimm_64bit == false) && (dimm_32bit == true)) {
  971. mcopt1 |= SDRAM_MCOPT1_DMWD_32;
  972. } else {
  973. printf("ERROR: Please install only 32 or 64 bit DDR-SDRAM DIMMs.\n\n");
  974. spd_ddr_init_hang ();
  975. }
  976. if (ecc_enabled == true)
  977. mcopt1 |= SDRAM_MCOPT1_MCHK_GEN;
  978. else
  979. mcopt1 |= SDRAM_MCOPT1_MCHK_NON;
  980. mtsdram(SDRAM_MCOPT1, mcopt1);
  981. }
  982. /*-----------------------------------------------------------------------------+
  983. * program_codt.
  984. *-----------------------------------------------------------------------------*/
  985. static void program_codt(unsigned long *dimm_populated,
  986. unsigned char *iic0_dimm_addr,
  987. unsigned long num_dimm_banks)
  988. {
  989. unsigned long codt;
  990. unsigned long modt0 = 0;
  991. unsigned long modt1 = 0;
  992. unsigned long modt2 = 0;
  993. unsigned long modt3 = 0;
  994. unsigned char dimm_num;
  995. unsigned char dimm_rank;
  996. unsigned char total_rank = 0;
  997. unsigned char total_dimm = 0;
  998. unsigned char dimm_type = 0;
  999. unsigned char firstSlot = 0;
  1000. /*------------------------------------------------------------------
  1001. * Set the SDRAM Controller On Die Termination Register
  1002. *-----------------------------------------------------------------*/
  1003. mfsdram(SDRAM_CODT, codt);
  1004. codt &= ~(SDRAM_CODT_DQS_SINGLE_END | SDRAM_CODT_CKSE_SINGLE_END);
  1005. codt |= SDRAM_CODT_IO_NMODE;
  1006. for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
  1007. if (dimm_populated[dimm_num] != SDRAM_NONE) {
  1008. dimm_rank = (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 5);
  1009. if (((unsigned long)spd_read(iic0_dimm_addr[dimm_num], 2)) == 0x08) {
  1010. dimm_rank = (dimm_rank & 0x0F) + 1;
  1011. dimm_type = SDRAM_DDR2;
  1012. } else {
  1013. dimm_rank = dimm_rank & 0x0F;
  1014. dimm_type = SDRAM_DDR1;
  1015. }
  1016. total_rank += dimm_rank;
  1017. total_dimm++;
  1018. if ((dimm_num == 0) && (total_dimm == 1))
  1019. firstSlot = true;
  1020. else
  1021. firstSlot = false;
  1022. }
  1023. }
  1024. if (dimm_type == SDRAM_DDR2) {
  1025. codt |= SDRAM_CODT_DQS_1_8_V_DDR2;
  1026. if ((total_dimm == 1) && (firstSlot == true)) {
  1027. if (total_rank == 1) { /* PUUU */
  1028. codt |= CALC_ODT_R(0);
  1029. modt0 = CALC_ODT_W(0);
  1030. modt1 = 0x00000000;
  1031. modt2 = 0x00000000;
  1032. modt3 = 0x00000000;
  1033. }
  1034. if (total_rank == 2) { /* PPUU */
  1035. codt |= CALC_ODT_R(0) | CALC_ODT_R(1);
  1036. modt0 = CALC_ODT_W(0) | CALC_ODT_W(1);
  1037. modt1 = 0x00000000;
  1038. modt2 = 0x00000000;
  1039. modt3 = 0x00000000;
  1040. }
  1041. } else if ((total_dimm == 1) && (firstSlot != true)) {
  1042. if (total_rank == 1) { /* UUPU */
  1043. codt |= CALC_ODT_R(2);
  1044. modt0 = 0x00000000;
  1045. modt1 = 0x00000000;
  1046. modt2 = CALC_ODT_W(2);
  1047. modt3 = 0x00000000;
  1048. }
  1049. if (total_rank == 2) { /* UUPP */
  1050. codt |= CALC_ODT_R(2) | CALC_ODT_R(3);
  1051. modt0 = 0x00000000;
  1052. modt1 = 0x00000000;
  1053. modt2 = CALC_ODT_W(2) | CALC_ODT_W(3);
  1054. modt3 = 0x00000000;
  1055. }
  1056. }
  1057. if (total_dimm == 2) {
  1058. if (total_rank == 2) { /* PUPU */
  1059. codt |= CALC_ODT_R(0) | CALC_ODT_R(2);
  1060. modt0 = CALC_ODT_RW(2);
  1061. modt1 = 0x00000000;
  1062. modt2 = CALC_ODT_RW(0);
  1063. modt3 = 0x00000000;
  1064. }
  1065. if (total_rank == 4) { /* PPPP */
  1066. codt |= CALC_ODT_R(0) | CALC_ODT_R(1) |
  1067. CALC_ODT_R(2) | CALC_ODT_R(3);
  1068. modt0 = CALC_ODT_RW(2) | CALC_ODT_RW(3);
  1069. modt1 = 0x00000000;
  1070. modt2 = CALC_ODT_RW(0) | CALC_ODT_RW(1);
  1071. modt3 = 0x00000000;
  1072. }
  1073. }
  1074. } else {
  1075. codt |= SDRAM_CODT_DQS_2_5_V_DDR1;
  1076. modt0 = 0x00000000;
  1077. modt1 = 0x00000000;
  1078. modt2 = 0x00000000;
  1079. modt3 = 0x00000000;
  1080. if (total_dimm == 1) {
  1081. if (total_rank == 1)
  1082. codt |= 0x00800000;
  1083. if (total_rank == 2)
  1084. codt |= 0x02800000;
  1085. }
  1086. if (total_dimm == 2) {
  1087. if (total_rank == 2)
  1088. codt |= 0x08800000;
  1089. if (total_rank == 4)
  1090. codt |= 0x2a800000;
  1091. }
  1092. }
  1093. debug("nb of dimm %d\n", total_dimm);
  1094. debug("nb of rank %d\n", total_rank);
  1095. if (total_dimm == 1)
  1096. debug("dimm in slot %d\n", firstSlot);
  1097. mtsdram(SDRAM_CODT, codt);
  1098. mtsdram(SDRAM_MODT0, modt0);
  1099. mtsdram(SDRAM_MODT1, modt1);
  1100. mtsdram(SDRAM_MODT2, modt2);
  1101. mtsdram(SDRAM_MODT3, modt3);
  1102. }
  1103. /*-----------------------------------------------------------------------------+
  1104. * program_initplr.
  1105. *-----------------------------------------------------------------------------*/
  1106. static void program_initplr(unsigned long *dimm_populated,
  1107. unsigned char *iic0_dimm_addr,
  1108. unsigned long num_dimm_banks,
  1109. ddr_cas_id_t selected_cas,
  1110. int write_recovery)
  1111. {
  1112. u32 cas = 0;
  1113. u32 odt = 0;
  1114. u32 ods = 0;
  1115. u32 mr;
  1116. u32 wr;
  1117. u32 emr;
  1118. u32 emr2;
  1119. u32 emr3;
  1120. int dimm_num;
  1121. int total_dimm = 0;
  1122. /******************************************************
  1123. ** Assumption: if more than one DIMM, all DIMMs are the same
  1124. ** as already checked in check_memory_type
  1125. ******************************************************/
  1126. if ((dimm_populated[0] == SDRAM_DDR1) || (dimm_populated[1] == SDRAM_DDR1)) {
  1127. mtsdram(SDRAM_INITPLR0, 0x81B80000);
  1128. mtsdram(SDRAM_INITPLR1, 0x81900400);
  1129. mtsdram(SDRAM_INITPLR2, 0x81810000);
  1130. mtsdram(SDRAM_INITPLR3, 0xff800162);
  1131. mtsdram(SDRAM_INITPLR4, 0x81900400);
  1132. mtsdram(SDRAM_INITPLR5, 0x86080000);
  1133. mtsdram(SDRAM_INITPLR6, 0x86080000);
  1134. mtsdram(SDRAM_INITPLR7, 0x81000062);
  1135. } else if ((dimm_populated[0] == SDRAM_DDR2) || (dimm_populated[1] == SDRAM_DDR2)) {
  1136. switch (selected_cas) {
  1137. case DDR_CAS_3:
  1138. cas = 3 << 4;
  1139. break;
  1140. case DDR_CAS_4:
  1141. cas = 4 << 4;
  1142. break;
  1143. case DDR_CAS_5:
  1144. cas = 5 << 4;
  1145. break;
  1146. default:
  1147. printf("ERROR: ucode error on selected_cas value %d", selected_cas);
  1148. spd_ddr_init_hang ();
  1149. break;
  1150. }
  1151. #if 0
  1152. /*
  1153. * ToDo - Still a problem with the write recovery:
  1154. * On the Corsair CM2X512-5400C4 module, setting write recovery
  1155. * in the INITPLR reg to the value calculated in program_mode()
  1156. * results in not correctly working DDR2 memory (crash after
  1157. * relocation).
  1158. *
  1159. * So for now, set the write recovery to 3. This seems to work
  1160. * on the Corair module too.
  1161. *
  1162. * 2007-03-01, sr
  1163. */
  1164. switch (write_recovery) {
  1165. case 3:
  1166. wr = WRITE_RECOV_3;
  1167. break;
  1168. case 4:
  1169. wr = WRITE_RECOV_4;
  1170. break;
  1171. case 5:
  1172. wr = WRITE_RECOV_5;
  1173. break;
  1174. case 6:
  1175. wr = WRITE_RECOV_6;
  1176. break;
  1177. default:
  1178. printf("ERROR: write recovery not support (%d)", write_recovery);
  1179. spd_ddr_init_hang ();
  1180. break;
  1181. }
  1182. #else
  1183. wr = WRITE_RECOV_3; /* test-only, see description above */
  1184. #endif
  1185. for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++)
  1186. if (dimm_populated[dimm_num] != SDRAM_NONE)
  1187. total_dimm++;
  1188. if (total_dimm == 1) {
  1189. odt = ODT_150_OHM;
  1190. ods = ODS_FULL;
  1191. } else if (total_dimm == 2) {
  1192. odt = ODT_75_OHM;
  1193. ods = ODS_REDUCED;
  1194. } else {
  1195. printf("ERROR: Unsupported number of DIMM's (%d)", total_dimm);
  1196. spd_ddr_init_hang ();
  1197. }
  1198. mr = CMD_EMR | SELECT_MR | BURST_LEN_4 | wr | cas;
  1199. emr = CMD_EMR | SELECT_EMR | odt | ods;
  1200. emr2 = CMD_EMR | SELECT_EMR2;
  1201. emr3 = CMD_EMR | SELECT_EMR3;
  1202. /* NOP - Wait 106 MemClk cycles */
  1203. mtsdram(SDRAM_INITPLR0, SDRAM_INITPLR_ENABLE | CMD_NOP |
  1204. SDRAM_INITPLR_IMWT_ENCODE(106));
  1205. udelay(1000);
  1206. /* precharge 4 MemClk cycles */
  1207. mtsdram(SDRAM_INITPLR1, SDRAM_INITPLR_ENABLE | CMD_PRECHARGE |
  1208. SDRAM_INITPLR_IMWT_ENCODE(4));
  1209. /* EMR2 - Wait tMRD (2 MemClk cycles) */
  1210. mtsdram(SDRAM_INITPLR2, SDRAM_INITPLR_ENABLE | emr2 |
  1211. SDRAM_INITPLR_IMWT_ENCODE(2));
  1212. /* EMR3 - Wait tMRD (2 MemClk cycles) */
  1213. mtsdram(SDRAM_INITPLR3, SDRAM_INITPLR_ENABLE | emr3 |
  1214. SDRAM_INITPLR_IMWT_ENCODE(2));
  1215. /* EMR DLL ENABLE - Wait tMRD (2 MemClk cycles) */
  1216. mtsdram(SDRAM_INITPLR4, SDRAM_INITPLR_ENABLE | emr |
  1217. SDRAM_INITPLR_IMWT_ENCODE(2));
  1218. /* MR w/ DLL reset - 200 cycle wait for DLL reset */
  1219. mtsdram(SDRAM_INITPLR5, SDRAM_INITPLR_ENABLE | mr | DLL_RESET |
  1220. SDRAM_INITPLR_IMWT_ENCODE(200));
  1221. udelay(1000);
  1222. /* precharge 4 MemClk cycles */
  1223. mtsdram(SDRAM_INITPLR6, SDRAM_INITPLR_ENABLE | CMD_PRECHARGE |
  1224. SDRAM_INITPLR_IMWT_ENCODE(4));
  1225. /* Refresh 25 MemClk cycles */
  1226. mtsdram(SDRAM_INITPLR7, SDRAM_INITPLR_ENABLE | CMD_REFRESH |
  1227. SDRAM_INITPLR_IMWT_ENCODE(25));
  1228. /* Refresh 25 MemClk cycles */
  1229. mtsdram(SDRAM_INITPLR8, SDRAM_INITPLR_ENABLE | CMD_REFRESH |
  1230. SDRAM_INITPLR_IMWT_ENCODE(25));
  1231. /* Refresh 25 MemClk cycles */
  1232. mtsdram(SDRAM_INITPLR9, SDRAM_INITPLR_ENABLE | CMD_REFRESH |
  1233. SDRAM_INITPLR_IMWT_ENCODE(25));
  1234. /* Refresh 25 MemClk cycles */
  1235. mtsdram(SDRAM_INITPLR10, SDRAM_INITPLR_ENABLE | CMD_REFRESH |
  1236. SDRAM_INITPLR_IMWT_ENCODE(25));
  1237. /* MR w/o DLL reset - Wait tMRD (2 MemClk cycles) */
  1238. mtsdram(SDRAM_INITPLR11, SDRAM_INITPLR_ENABLE | mr |
  1239. SDRAM_INITPLR_IMWT_ENCODE(2));
  1240. /* EMR OCD Default - Wait tMRD (2 MemClk cycles) */
  1241. mtsdram(SDRAM_INITPLR12, SDRAM_INITPLR_ENABLE | OCD_CALIB_DEF |
  1242. SDRAM_INITPLR_IMWT_ENCODE(2) | emr);
  1243. /* EMR OCD Exit */
  1244. mtsdram(SDRAM_INITPLR13, SDRAM_INITPLR_ENABLE | emr |
  1245. SDRAM_INITPLR_IMWT_ENCODE(2));
  1246. } else {
  1247. printf("ERROR: ucode error as unknown DDR type in program_initplr");
  1248. spd_ddr_init_hang ();
  1249. }
  1250. }
  1251. /*------------------------------------------------------------------
  1252. * This routine programs the SDRAM_MMODE register.
  1253. * the selected_cas is an output parameter, that will be passed
  1254. * by caller to call the above program_initplr( )
  1255. *-----------------------------------------------------------------*/
  1256. static void program_mode(unsigned long *dimm_populated,
  1257. unsigned char *iic0_dimm_addr,
  1258. unsigned long num_dimm_banks,
  1259. ddr_cas_id_t *selected_cas,
  1260. int *write_recovery)
  1261. {
  1262. unsigned long dimm_num;
  1263. unsigned long sdram_ddr1;
  1264. unsigned long t_wr_ns;
  1265. unsigned long t_wr_clk;
  1266. unsigned long cas_bit;
  1267. unsigned long cas_index;
  1268. unsigned long sdram_freq;
  1269. unsigned long ddr_check;
  1270. unsigned long mmode;
  1271. unsigned long tcyc_reg;
  1272. unsigned long cycle_2_0_clk;
  1273. unsigned long cycle_2_5_clk;
  1274. unsigned long cycle_3_0_clk;
  1275. unsigned long cycle_4_0_clk;
  1276. unsigned long cycle_5_0_clk;
  1277. unsigned long max_2_0_tcyc_ns_x_100;
  1278. unsigned long max_2_5_tcyc_ns_x_100;
  1279. unsigned long max_3_0_tcyc_ns_x_100;
  1280. unsigned long max_4_0_tcyc_ns_x_100;
  1281. unsigned long max_5_0_tcyc_ns_x_100;
  1282. unsigned long cycle_time_ns_x_100[3];
  1283. PPC4xx_SYS_INFO board_cfg;
  1284. unsigned char cas_2_0_available;
  1285. unsigned char cas_2_5_available;
  1286. unsigned char cas_3_0_available;
  1287. unsigned char cas_4_0_available;
  1288. unsigned char cas_5_0_available;
  1289. unsigned long sdr_ddrpll;
  1290. /*------------------------------------------------------------------
  1291. * Get the board configuration info.
  1292. *-----------------------------------------------------------------*/
  1293. get_sys_info(&board_cfg);
  1294. mfsdr(SDR0_DDR0, sdr_ddrpll);
  1295. sdram_freq = MULDIV64((board_cfg.freqPLB), SDR0_DDR0_DDRM_DECODE(sdr_ddrpll), 1);
  1296. debug("sdram_freq=%lu\n", sdram_freq);
  1297. /*------------------------------------------------------------------
  1298. * Handle the timing. We need to find the worst case timing of all
  1299. * the dimm modules installed.
  1300. *-----------------------------------------------------------------*/
  1301. t_wr_ns = 0;
  1302. cas_2_0_available = true;
  1303. cas_2_5_available = true;
  1304. cas_3_0_available = true;
  1305. cas_4_0_available = true;
  1306. cas_5_0_available = true;
  1307. max_2_0_tcyc_ns_x_100 = 10;
  1308. max_2_5_tcyc_ns_x_100 = 10;
  1309. max_3_0_tcyc_ns_x_100 = 10;
  1310. max_4_0_tcyc_ns_x_100 = 10;
  1311. max_5_0_tcyc_ns_x_100 = 10;
  1312. sdram_ddr1 = true;
  1313. /* loop through all the DIMM slots on the board */
  1314. for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
  1315. /* If a dimm is installed in a particular slot ... */
  1316. if (dimm_populated[dimm_num] != SDRAM_NONE) {
  1317. if (dimm_populated[dimm_num] == SDRAM_DDR1)
  1318. sdram_ddr1 = true;
  1319. else
  1320. sdram_ddr1 = false;
  1321. cas_bit = spd_read(iic0_dimm_addr[dimm_num], 18);
  1322. debug("cas_bit[SPD byte 18]=%02lx\n", cas_bit);
  1323. /* For a particular DIMM, grab the three CAS values it supports */
  1324. for (cas_index = 0; cas_index < 3; cas_index++) {
  1325. switch (cas_index) {
  1326. case 0:
  1327. tcyc_reg = spd_read(iic0_dimm_addr[dimm_num], 9);
  1328. break;
  1329. case 1:
  1330. tcyc_reg = spd_read(iic0_dimm_addr[dimm_num], 23);
  1331. break;
  1332. default:
  1333. tcyc_reg = spd_read(iic0_dimm_addr[dimm_num], 25);
  1334. break;
  1335. }
  1336. if ((tcyc_reg & 0x0F) >= 10) {
  1337. if ((tcyc_reg & 0x0F) == 0x0D) {
  1338. /* Convert from hex to decimal */
  1339. cycle_time_ns_x_100[cas_index] =
  1340. (((tcyc_reg & 0xF0) >> 4) * 100) + 75;
  1341. } else {
  1342. printf("ERROR: SPD reported Tcyc is incorrect for DIMM "
  1343. "in slot %d\n", (unsigned int)dimm_num);
  1344. spd_ddr_init_hang ();
  1345. }
  1346. } else {
  1347. /* Convert from hex to decimal */
  1348. cycle_time_ns_x_100[cas_index] =
  1349. (((tcyc_reg & 0xF0) >> 4) * 100) +
  1350. ((tcyc_reg & 0x0F)*10);
  1351. }
  1352. debug("cas_index=%lu: cycle_time_ns_x_100=%lu\n", cas_index,
  1353. cycle_time_ns_x_100[cas_index]);
  1354. }
  1355. /* The rest of this routine determines if CAS 2.0, 2.5, 3.0, 4.0 and 5.0 are */
  1356. /* supported for a particular DIMM. */
  1357. cas_index = 0;
  1358. if (sdram_ddr1) {
  1359. /*
  1360. * DDR devices use the following bitmask for CAS latency:
  1361. * Bit 7 6 5 4 3 2 1 0
  1362. * TBD 4.0 3.5 3.0 2.5 2.0 1.5 1.0
  1363. */
  1364. if (((cas_bit & 0x40) == 0x40) && (cas_index < 3) &&
  1365. (cycle_time_ns_x_100[cas_index] != 0)) {
  1366. max_4_0_tcyc_ns_x_100 = max(max_4_0_tcyc_ns_x_100,
  1367. cycle_time_ns_x_100[cas_index]);
  1368. cas_index++;
  1369. } else {
  1370. if (cas_index != 0)
  1371. cas_index++;
  1372. cas_4_0_available = false;
  1373. }
  1374. if (((cas_bit & 0x10) == 0x10) && (cas_index < 3) &&
  1375. (cycle_time_ns_x_100[cas_index] != 0)) {
  1376. max_3_0_tcyc_ns_x_100 = max(max_3_0_tcyc_ns_x_100,
  1377. cycle_time_ns_x_100[cas_index]);
  1378. cas_index++;
  1379. } else {
  1380. if (cas_index != 0)
  1381. cas_index++;
  1382. cas_3_0_available = false;
  1383. }
  1384. if (((cas_bit & 0x08) == 0x08) && (cas_index < 3) &&
  1385. (cycle_time_ns_x_100[cas_index] != 0)) {
  1386. max_2_5_tcyc_ns_x_100 = max(max_2_5_tcyc_ns_x_100,
  1387. cycle_time_ns_x_100[cas_index]);
  1388. cas_index++;
  1389. } else {
  1390. if (cas_index != 0)
  1391. cas_index++;
  1392. cas_2_5_available = false;
  1393. }
  1394. if (((cas_bit & 0x04) == 0x04) && (cas_index < 3) &&
  1395. (cycle_time_ns_x_100[cas_index] != 0)) {
  1396. max_2_0_tcyc_ns_x_100 = max(max_2_0_tcyc_ns_x_100,
  1397. cycle_time_ns_x_100[cas_index]);
  1398. cas_index++;
  1399. } else {
  1400. if (cas_index != 0)
  1401. cas_index++;
  1402. cas_2_0_available = false;
  1403. }
  1404. } else {
  1405. /*
  1406. * DDR2 devices use the following bitmask for CAS latency:
  1407. * Bit 7 6 5 4 3 2 1 0
  1408. * TBD 6.0 5.0 4.0 3.0 2.0 TBD TBD
  1409. */
  1410. if (((cas_bit & 0x20) == 0x20) && (cas_index < 3) &&
  1411. (cycle_time_ns_x_100[cas_index] != 0)) {
  1412. max_5_0_tcyc_ns_x_100 = max(max_5_0_tcyc_ns_x_100,
  1413. cycle_time_ns_x_100[cas_index]);
  1414. cas_index++;
  1415. } else {
  1416. if (cas_index != 0)
  1417. cas_index++;
  1418. cas_5_0_available = false;
  1419. }
  1420. if (((cas_bit & 0x10) == 0x10) && (cas_index < 3) &&
  1421. (cycle_time_ns_x_100[cas_index] != 0)) {
  1422. max_4_0_tcyc_ns_x_100 = max(max_4_0_tcyc_ns_x_100,
  1423. cycle_time_ns_x_100[cas_index]);
  1424. cas_index++;
  1425. } else {
  1426. if (cas_index != 0)
  1427. cas_index++;
  1428. cas_4_0_available = false;
  1429. }
  1430. if (((cas_bit & 0x08) == 0x08) && (cas_index < 3) &&
  1431. (cycle_time_ns_x_100[cas_index] != 0)) {
  1432. max_3_0_tcyc_ns_x_100 = max(max_3_0_tcyc_ns_x_100,
  1433. cycle_time_ns_x_100[cas_index]);
  1434. cas_index++;
  1435. } else {
  1436. if (cas_index != 0)
  1437. cas_index++;
  1438. cas_3_0_available = false;
  1439. }
  1440. }
  1441. }
  1442. }
  1443. /*------------------------------------------------------------------
  1444. * Set the SDRAM mode, SDRAM_MMODE
  1445. *-----------------------------------------------------------------*/
  1446. mfsdram(SDRAM_MMODE, mmode);
  1447. mmode = mmode & ~(SDRAM_MMODE_WR_MASK | SDRAM_MMODE_DCL_MASK);
  1448. /* add 10 here because of rounding problems */
  1449. cycle_2_0_clk = MULDIV64(ONE_BILLION, 100, max_2_0_tcyc_ns_x_100) + 10;
  1450. cycle_2_5_clk = MULDIV64(ONE_BILLION, 100, max_2_5_tcyc_ns_x_100) + 10;
  1451. cycle_3_0_clk = MULDIV64(ONE_BILLION, 100, max_3_0_tcyc_ns_x_100) + 10;
  1452. cycle_4_0_clk = MULDIV64(ONE_BILLION, 100, max_4_0_tcyc_ns_x_100) + 10;
  1453. cycle_5_0_clk = MULDIV64(ONE_BILLION, 100, max_5_0_tcyc_ns_x_100) + 10;
  1454. debug("cycle_3_0_clk=%lu\n", cycle_3_0_clk);
  1455. debug("cycle_4_0_clk=%lu\n", cycle_4_0_clk);
  1456. debug("cycle_5_0_clk=%lu\n", cycle_5_0_clk);
  1457. if (sdram_ddr1 == true) { /* DDR1 */
  1458. if ((cas_2_0_available == true) &&
  1459. (sdram_freq <= cycle_2_0_clk)) {
  1460. mmode |= SDRAM_MMODE_DCL_DDR1_2_0_CLK;
  1461. *selected_cas = DDR_CAS_2;
  1462. } else if ((cas_2_5_available == true) &&
  1463. (sdram_freq <= cycle_2_5_clk)) {
  1464. mmode |= SDRAM_MMODE_DCL_DDR1_2_5_CLK;
  1465. *selected_cas = DDR_CAS_2_5;
  1466. } else if ((cas_3_0_available == true) &&
  1467. (sdram_freq <= cycle_3_0_clk)) {
  1468. mmode |= SDRAM_MMODE_DCL_DDR1_3_0_CLK;
  1469. *selected_cas = DDR_CAS_3;
  1470. } else {
  1471. printf("ERROR: Cannot find a supported CAS latency with the installed DIMMs.\n");
  1472. printf("Only DIMMs DDR1 with CAS latencies of 2.0, 2.5, and 3.0 are supported.\n");
  1473. printf("Make sure the PLB speed is within the supported range of the DIMMs.\n\n");
  1474. spd_ddr_init_hang ();
  1475. }
  1476. } else { /* DDR2 */
  1477. debug("cas_3_0_available=%d\n", cas_3_0_available);
  1478. debug("cas_4_0_available=%d\n", cas_4_0_available);
  1479. debug("cas_5_0_available=%d\n", cas_5_0_available);
  1480. if ((cas_3_0_available == true) &&
  1481. (sdram_freq <= cycle_3_0_clk)) {
  1482. mmode |= SDRAM_MMODE_DCL_DDR2_3_0_CLK;
  1483. *selected_cas = DDR_CAS_3;
  1484. } else if ((cas_4_0_available == true) &&
  1485. (sdram_freq <= cycle_4_0_clk)) {
  1486. mmode |= SDRAM_MMODE_DCL_DDR2_4_0_CLK;
  1487. *selected_cas = DDR_CAS_4;
  1488. } else if ((cas_5_0_available == true) &&
  1489. (sdram_freq <= cycle_5_0_clk)) {
  1490. mmode |= SDRAM_MMODE_DCL_DDR2_5_0_CLK;
  1491. *selected_cas = DDR_CAS_5;
  1492. } else {
  1493. printf("ERROR: Cannot find a supported CAS latency with the installed DIMMs.\n");
  1494. printf("Only DIMMs DDR2 with CAS latencies of 3.0, 4.0, and 5.0 are supported.\n");
  1495. printf("Make sure the PLB speed is within the supported range of the DIMMs.\n");
  1496. printf("cas3=%d cas4=%d cas5=%d\n",
  1497. cas_3_0_available, cas_4_0_available, cas_5_0_available);
  1498. printf("sdram_freq=%lu cycle3=%lu cycle4=%lu cycle5=%lu\n\n",
  1499. sdram_freq, cycle_3_0_clk, cycle_4_0_clk, cycle_5_0_clk);
  1500. spd_ddr_init_hang ();
  1501. }
  1502. }
  1503. if (sdram_ddr1 == true)
  1504. mmode |= SDRAM_MMODE_WR_DDR1;
  1505. else {
  1506. /* loop through all the DIMM slots on the board */
  1507. for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
  1508. /* If a dimm is installed in a particular slot ... */
  1509. if (dimm_populated[dimm_num] != SDRAM_NONE)
  1510. t_wr_ns = max(t_wr_ns,
  1511. spd_read(iic0_dimm_addr[dimm_num], 36) >> 2);
  1512. }
  1513. /*
  1514. * convert from nanoseconds to ddr clocks
  1515. * round up if necessary
  1516. */
  1517. t_wr_clk = MULDIV64(sdram_freq, t_wr_ns, ONE_BILLION);
  1518. ddr_check = MULDIV64(ONE_BILLION, t_wr_clk, t_wr_ns);
  1519. if (sdram_freq != ddr_check)
  1520. t_wr_clk++;
  1521. switch (t_wr_clk) {
  1522. case 0:
  1523. case 1:
  1524. case 2:
  1525. case 3:
  1526. mmode |= SDRAM_MMODE_WR_DDR2_3_CYC;
  1527. break;
  1528. case 4:
  1529. mmode |= SDRAM_MMODE_WR_DDR2_4_CYC;
  1530. break;
  1531. case 5:
  1532. mmode |= SDRAM_MMODE_WR_DDR2_5_CYC;
  1533. break;
  1534. default:
  1535. mmode |= SDRAM_MMODE_WR_DDR2_6_CYC;
  1536. break;
  1537. }
  1538. *write_recovery = t_wr_clk;
  1539. }
  1540. debug("CAS latency = %d\n", *selected_cas);
  1541. debug("Write recovery = %d\n", *write_recovery);
  1542. mtsdram(SDRAM_MMODE, mmode);
  1543. }
  1544. /*-----------------------------------------------------------------------------+
  1545. * program_rtr.
  1546. *-----------------------------------------------------------------------------*/
  1547. static void program_rtr(unsigned long *dimm_populated,
  1548. unsigned char *iic0_dimm_addr,
  1549. unsigned long num_dimm_banks)
  1550. {
  1551. PPC4xx_SYS_INFO board_cfg;
  1552. unsigned long max_refresh_rate;
  1553. unsigned long dimm_num;
  1554. unsigned long refresh_rate_type;
  1555. unsigned long refresh_rate;
  1556. unsigned long rint;
  1557. unsigned long sdram_freq;
  1558. unsigned long sdr_ddrpll;
  1559. unsigned long val;
  1560. /*------------------------------------------------------------------
  1561. * Get the board configuration info.
  1562. *-----------------------------------------------------------------*/
  1563. get_sys_info(&board_cfg);
  1564. /*------------------------------------------------------------------
  1565. * Set the SDRAM Refresh Timing Register, SDRAM_RTR
  1566. *-----------------------------------------------------------------*/
  1567. mfsdr(SDR0_DDR0, sdr_ddrpll);
  1568. sdram_freq = ((board_cfg.freqPLB) * SDR0_DDR0_DDRM_DECODE(sdr_ddrpll));
  1569. max_refresh_rate = 0;
  1570. for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
  1571. if (dimm_populated[dimm_num] != SDRAM_NONE) {
  1572. refresh_rate_type = spd_read(iic0_dimm_addr[dimm_num], 12);
  1573. refresh_rate_type &= 0x7F;
  1574. switch (refresh_rate_type) {
  1575. case 0:
  1576. refresh_rate = 15625;
  1577. break;
  1578. case 1:
  1579. refresh_rate = 3906;
  1580. break;
  1581. case 2:
  1582. refresh_rate = 7812;
  1583. break;
  1584. case 3:
  1585. refresh_rate = 31250;
  1586. break;
  1587. case 4:
  1588. refresh_rate = 62500;
  1589. break;
  1590. case 5:
  1591. refresh_rate = 125000;
  1592. break;
  1593. default:
  1594. refresh_rate = 0;
  1595. printf("ERROR: DIMM %d unsupported refresh rate/type.\n",
  1596. (unsigned int)dimm_num);
  1597. printf("Replace the DIMM module with a supported DIMM.\n\n");
  1598. spd_ddr_init_hang ();
  1599. break;
  1600. }
  1601. max_refresh_rate = max(max_refresh_rate, refresh_rate);
  1602. }
  1603. }
  1604. rint = MULDIV64(sdram_freq, max_refresh_rate, ONE_BILLION);
  1605. mfsdram(SDRAM_RTR, val);
  1606. mtsdram(SDRAM_RTR, (val & ~SDRAM_RTR_RINT_MASK) |
  1607. (SDRAM_RTR_RINT_ENCODE(rint)));
  1608. }
  1609. /*------------------------------------------------------------------
  1610. * This routine programs the SDRAM_TRx registers.
  1611. *-----------------------------------------------------------------*/
  1612. static void program_tr(unsigned long *dimm_populated,
  1613. unsigned char *iic0_dimm_addr,
  1614. unsigned long num_dimm_banks)
  1615. {
  1616. unsigned long dimm_num;
  1617. unsigned long sdram_ddr1;
  1618. unsigned long t_rp_ns;
  1619. unsigned long t_rcd_ns;
  1620. unsigned long t_rrd_ns;
  1621. unsigned long t_ras_ns;
  1622. unsigned long t_rc_ns;
  1623. unsigned long t_rfc_ns;
  1624. unsigned long t_wpc_ns;
  1625. unsigned long t_wtr_ns;
  1626. unsigned long t_rpc_ns;
  1627. unsigned long t_rp_clk;
  1628. unsigned long t_rcd_clk;
  1629. unsigned long t_rrd_clk;
  1630. unsigned long t_ras_clk;
  1631. unsigned long t_rc_clk;
  1632. unsigned long t_rfc_clk;
  1633. unsigned long t_wpc_clk;
  1634. unsigned long t_wtr_clk;
  1635. unsigned long t_rpc_clk;
  1636. unsigned long sdtr1, sdtr2, sdtr3;
  1637. unsigned long ddr_check;
  1638. unsigned long sdram_freq;
  1639. unsigned long sdr_ddrpll;
  1640. PPC4xx_SYS_INFO board_cfg;
  1641. /*------------------------------------------------------------------
  1642. * Get the board configuration info.
  1643. *-----------------------------------------------------------------*/
  1644. get_sys_info(&board_cfg);
  1645. mfsdr(SDR0_DDR0, sdr_ddrpll);
  1646. sdram_freq = ((board_cfg.freqPLB) * SDR0_DDR0_DDRM_DECODE(sdr_ddrpll));
  1647. /*------------------------------------------------------------------
  1648. * Handle the timing. We need to find the worst case timing of all
  1649. * the dimm modules installed.
  1650. *-----------------------------------------------------------------*/
  1651. t_rp_ns = 0;
  1652. t_rrd_ns = 0;
  1653. t_rcd_ns = 0;
  1654. t_ras_ns = 0;
  1655. t_rc_ns = 0;
  1656. t_rfc_ns = 0;
  1657. t_wpc_ns = 0;
  1658. t_wtr_ns = 0;
  1659. t_rpc_ns = 0;
  1660. sdram_ddr1 = true;
  1661. /* loop through all the DIMM slots on the board */
  1662. for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
  1663. /* If a dimm is installed in a particular slot ... */
  1664. if (dimm_populated[dimm_num] != SDRAM_NONE) {
  1665. if (dimm_populated[dimm_num] == SDRAM_DDR2)
  1666. sdram_ddr1 = true;
  1667. else
  1668. sdram_ddr1 = false;
  1669. t_rcd_ns = max(t_rcd_ns, spd_read(iic0_dimm_addr[dimm_num], 29) >> 2);
  1670. t_rrd_ns = max(t_rrd_ns, spd_read(iic0_dimm_addr[dimm_num], 28) >> 2);
  1671. t_rp_ns = max(t_rp_ns, spd_read(iic0_dimm_addr[dimm_num], 27) >> 2);
  1672. t_ras_ns = max(t_ras_ns, spd_read(iic0_dimm_addr[dimm_num], 30));
  1673. t_rc_ns = max(t_rc_ns, spd_read(iic0_dimm_addr[dimm_num], 41));
  1674. t_rfc_ns = max(t_rfc_ns, spd_read(iic0_dimm_addr[dimm_num], 42));
  1675. }
  1676. }
  1677. /*------------------------------------------------------------------
  1678. * Set the SDRAM Timing Reg 1, SDRAM_TR1
  1679. *-----------------------------------------------------------------*/
  1680. mfsdram(SDRAM_SDTR1, sdtr1);
  1681. sdtr1 &= ~(SDRAM_SDTR1_LDOF_MASK | SDRAM_SDTR1_RTW_MASK |
  1682. SDRAM_SDTR1_WTWO_MASK | SDRAM_SDTR1_RTRO_MASK);
  1683. /* default values */
  1684. sdtr1 |= SDRAM_SDTR1_LDOF_2_CLK;
  1685. sdtr1 |= SDRAM_SDTR1_RTW_2_CLK;
  1686. /* normal operations */
  1687. sdtr1 |= SDRAM_SDTR1_WTWO_0_CLK;
  1688. sdtr1 |= SDRAM_SDTR1_RTRO_1_CLK;
  1689. mtsdram(SDRAM_SDTR1, sdtr1);
  1690. /*------------------------------------------------------------------
  1691. * Set the SDRAM Timing Reg 2, SDRAM_TR2
  1692. *-----------------------------------------------------------------*/
  1693. mfsdram(SDRAM_SDTR2, sdtr2);
  1694. sdtr2 &= ~(SDRAM_SDTR2_RCD_MASK | SDRAM_SDTR2_WTR_MASK |
  1695. SDRAM_SDTR2_XSNR_MASK | SDRAM_SDTR2_WPC_MASK |
  1696. SDRAM_SDTR2_RPC_MASK | SDRAM_SDTR2_RP_MASK |
  1697. SDRAM_SDTR2_RRD_MASK);
  1698. /*
  1699. * convert t_rcd from nanoseconds to ddr clocks
  1700. * round up if necessary
  1701. */
  1702. t_rcd_clk = MULDIV64(sdram_freq, t_rcd_ns, ONE_BILLION);
  1703. ddr_check = MULDIV64(ONE_BILLION, t_rcd_clk, t_rcd_ns);
  1704. if (sdram_freq != ddr_check)
  1705. t_rcd_clk++;
  1706. switch (t_rcd_clk) {
  1707. case 0:
  1708. case 1:
  1709. sdtr2 |= SDRAM_SDTR2_RCD_1_CLK;
  1710. break;
  1711. case 2:
  1712. sdtr2 |= SDRAM_SDTR2_RCD_2_CLK;
  1713. break;
  1714. case 3:
  1715. sdtr2 |= SDRAM_SDTR2_RCD_3_CLK;
  1716. break;
  1717. case 4:
  1718. sdtr2 |= SDRAM_SDTR2_RCD_4_CLK;
  1719. break;
  1720. default:
  1721. sdtr2 |= SDRAM_SDTR2_RCD_5_CLK;
  1722. break;
  1723. }
  1724. if (sdram_ddr1 == true) { /* DDR1 */
  1725. if (sdram_freq < 200000000) {
  1726. sdtr2 |= SDRAM_SDTR2_WTR_1_CLK;
  1727. sdtr2 |= SDRAM_SDTR2_WPC_2_CLK;
  1728. sdtr2 |= SDRAM_SDTR2_RPC_2_CLK;
  1729. } else {
  1730. sdtr2 |= SDRAM_SDTR2_WTR_2_CLK;
  1731. sdtr2 |= SDRAM_SDTR2_WPC_3_CLK;
  1732. sdtr2 |= SDRAM_SDTR2_RPC_2_CLK;
  1733. }
  1734. } else { /* DDR2 */
  1735. /* loop through all the DIMM slots on the board */
  1736. for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
  1737. /* If a dimm is installed in a particular slot ... */
  1738. if (dimm_populated[dimm_num] != SDRAM_NONE) {
  1739. t_wpc_ns = max(t_wtr_ns, spd_read(iic0_dimm_addr[dimm_num], 36) >> 2);
  1740. t_wtr_ns = max(t_wtr_ns, spd_read(iic0_dimm_addr[dimm_num], 37) >> 2);
  1741. t_rpc_ns = max(t_rpc_ns, spd_read(iic0_dimm_addr[dimm_num], 38) >> 2);
  1742. }
  1743. }
  1744. /*
  1745. * convert from nanoseconds to ddr clocks
  1746. * round up if necessary
  1747. */
  1748. t_wpc_clk = MULDIV64(sdram_freq, t_wpc_ns, ONE_BILLION);
  1749. ddr_check = MULDIV64(ONE_BILLION, t_wpc_clk, t_wpc_ns);
  1750. if (sdram_freq != ddr_check)
  1751. t_wpc_clk++;
  1752. switch (t_wpc_clk) {
  1753. case 0:
  1754. case 1:
  1755. case 2:
  1756. sdtr2 |= SDRAM_SDTR2_WPC_2_CLK;
  1757. break;
  1758. case 3:
  1759. sdtr2 |= SDRAM_SDTR2_WPC_3_CLK;
  1760. break;
  1761. case 4:
  1762. sdtr2 |= SDRAM_SDTR2_WPC_4_CLK;
  1763. break;
  1764. case 5:
  1765. sdtr2 |= SDRAM_SDTR2_WPC_5_CLK;
  1766. break;
  1767. default:
  1768. sdtr2 |= SDRAM_SDTR2_WPC_6_CLK;
  1769. break;
  1770. }
  1771. /*
  1772. * convert from nanoseconds to ddr clocks
  1773. * round up if necessary
  1774. */
  1775. t_wtr_clk = MULDIV64(sdram_freq, t_wtr_ns, ONE_BILLION);
  1776. ddr_check = MULDIV64(ONE_BILLION, t_wtr_clk, t_wtr_ns);
  1777. if (sdram_freq != ddr_check)
  1778. t_wtr_clk++;
  1779. switch (t_wtr_clk) {
  1780. case 0:
  1781. case 1:
  1782. sdtr2 |= SDRAM_SDTR2_WTR_1_CLK;
  1783. break;
  1784. case 2:
  1785. sdtr2 |= SDRAM_SDTR2_WTR_2_CLK;
  1786. break;
  1787. case 3:
  1788. sdtr2 |= SDRAM_SDTR2_WTR_3_CLK;
  1789. break;
  1790. default:
  1791. sdtr2 |= SDRAM_SDTR2_WTR_4_CLK;
  1792. break;
  1793. }
  1794. /*
  1795. * convert from nanoseconds to ddr clocks
  1796. * round up if necessary
  1797. */
  1798. t_rpc_clk = MULDIV64(sdram_freq, t_rpc_ns, ONE_BILLION);
  1799. ddr_check = MULDIV64(ONE_BILLION, t_rpc_clk, t_rpc_ns);
  1800. if (sdram_freq != ddr_check)
  1801. t_rpc_clk++;
  1802. switch (t_rpc_clk) {
  1803. case 0:
  1804. case 1:
  1805. case 2:
  1806. sdtr2 |= SDRAM_SDTR2_RPC_2_CLK;
  1807. break;
  1808. case 3:
  1809. sdtr2 |= SDRAM_SDTR2_RPC_3_CLK;
  1810. break;
  1811. default:
  1812. sdtr2 |= SDRAM_SDTR2_RPC_4_CLK;
  1813. break;
  1814. }
  1815. }
  1816. /* default value */
  1817. sdtr2 |= SDRAM_SDTR2_XSNR_16_CLK;
  1818. /*
  1819. * convert t_rrd from nanoseconds to ddr clocks
  1820. * round up if necessary
  1821. */
  1822. t_rrd_clk = MULDIV64(sdram_freq, t_rrd_ns, ONE_BILLION);
  1823. ddr_check = MULDIV64(ONE_BILLION, t_rrd_clk, t_rrd_ns);
  1824. if (sdram_freq != ddr_check)
  1825. t_rrd_clk++;
  1826. if (t_rrd_clk == 3)
  1827. sdtr2 |= SDRAM_SDTR2_RRD_3_CLK;
  1828. else
  1829. sdtr2 |= SDRAM_SDTR2_RRD_2_CLK;
  1830. /*
  1831. * convert t_rp from nanoseconds to ddr clocks
  1832. * round up if necessary
  1833. */
  1834. t_rp_clk = MULDIV64(sdram_freq, t_rp_ns, ONE_BILLION);
  1835. ddr_check = MULDIV64(ONE_BILLION, t_rp_clk, t_rp_ns);
  1836. if (sdram_freq != ddr_check)
  1837. t_rp_clk++;
  1838. switch (t_rp_clk) {
  1839. case 0:
  1840. case 1:
  1841. case 2:
  1842. case 3:
  1843. sdtr2 |= SDRAM_SDTR2_RP_3_CLK;
  1844. break;
  1845. case 4:
  1846. sdtr2 |= SDRAM_SDTR2_RP_4_CLK;
  1847. break;
  1848. case 5:
  1849. sdtr2 |= SDRAM_SDTR2_RP_5_CLK;
  1850. break;
  1851. case 6:
  1852. sdtr2 |= SDRAM_SDTR2_RP_6_CLK;
  1853. break;
  1854. default:
  1855. sdtr2 |= SDRAM_SDTR2_RP_7_CLK;
  1856. break;
  1857. }
  1858. mtsdram(SDRAM_SDTR2, sdtr2);
  1859. /*------------------------------------------------------------------
  1860. * Set the SDRAM Timing Reg 3, SDRAM_TR3
  1861. *-----------------------------------------------------------------*/
  1862. mfsdram(SDRAM_SDTR3, sdtr3);
  1863. sdtr3 &= ~(SDRAM_SDTR3_RAS_MASK | SDRAM_SDTR3_RC_MASK |
  1864. SDRAM_SDTR3_XCS_MASK | SDRAM_SDTR3_RFC_MASK);
  1865. /*
  1866. * convert t_ras from nanoseconds to ddr clocks
  1867. * round up if necessary
  1868. */
  1869. t_ras_clk = MULDIV64(sdram_freq, t_ras_ns, ONE_BILLION);
  1870. ddr_check = MULDIV64(ONE_BILLION, t_ras_clk, t_ras_ns);
  1871. if (sdram_freq != ddr_check)
  1872. t_ras_clk++;
  1873. sdtr3 |= SDRAM_SDTR3_RAS_ENCODE(t_ras_clk);
  1874. /*
  1875. * convert t_rc from nanoseconds to ddr clocks
  1876. * round up if necessary
  1877. */
  1878. t_rc_clk = MULDIV64(sdram_freq, t_rc_ns, ONE_BILLION);
  1879. ddr_check = MULDIV64(ONE_BILLION, t_rc_clk, t_rc_ns);
  1880. if (sdram_freq != ddr_check)
  1881. t_rc_clk++;
  1882. sdtr3 |= SDRAM_SDTR3_RC_ENCODE(t_rc_clk);
  1883. /* default xcs value */
  1884. sdtr3 |= SDRAM_SDTR3_XCS;
  1885. /*
  1886. * convert t_rfc from nanoseconds to ddr clocks
  1887. * round up if necessary
  1888. */
  1889. t_rfc_clk = MULDIV64(sdram_freq, t_rfc_ns, ONE_BILLION);
  1890. ddr_check = MULDIV64(ONE_BILLION, t_rfc_clk, t_rfc_ns);
  1891. if (sdram_freq != ddr_check)
  1892. t_rfc_clk++;
  1893. sdtr3 |= SDRAM_SDTR3_RFC_ENCODE(t_rfc_clk);
  1894. mtsdram(SDRAM_SDTR3, sdtr3);
  1895. }
  1896. /*-----------------------------------------------------------------------------+
  1897. * program_bxcf.
  1898. *-----------------------------------------------------------------------------*/
  1899. static void program_bxcf(unsigned long *dimm_populated,
  1900. unsigned char *iic0_dimm_addr,
  1901. unsigned long num_dimm_banks)
  1902. {
  1903. unsigned long dimm_num;
  1904. unsigned long num_col_addr;
  1905. unsigned long num_ranks;
  1906. unsigned long num_banks;
  1907. unsigned long mode;
  1908. unsigned long ind_rank;
  1909. unsigned long ind;
  1910. unsigned long ind_bank;
  1911. unsigned long bank_0_populated;
  1912. /*------------------------------------------------------------------
  1913. * Set the BxCF regs. First, wipe out the bank config registers.
  1914. *-----------------------------------------------------------------*/
  1915. mtsdram(SDRAM_MB0CF, 0x00000000);
  1916. mtsdram(SDRAM_MB1CF, 0x00000000);
  1917. mtsdram(SDRAM_MB2CF, 0x00000000);
  1918. mtsdram(SDRAM_MB3CF, 0x00000000);
  1919. mode = SDRAM_BXCF_M_BE_ENABLE;
  1920. bank_0_populated = 0;
  1921. for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
  1922. if (dimm_populated[dimm_num] != SDRAM_NONE) {
  1923. num_col_addr = spd_read(iic0_dimm_addr[dimm_num], 4);
  1924. num_ranks = spd_read(iic0_dimm_addr[dimm_num], 5);
  1925. if ((spd_read(iic0_dimm_addr[dimm_num], 2)) == 0x08)
  1926. num_ranks = (num_ranks & 0x0F) +1;
  1927. else
  1928. num_ranks = num_ranks & 0x0F;
  1929. num_banks = spd_read(iic0_dimm_addr[dimm_num], 17);
  1930. for (ind_bank = 0; ind_bank < 2; ind_bank++) {
  1931. if (num_banks == 4)
  1932. ind = 0;
  1933. else
  1934. ind = 5 << 8;
  1935. switch (num_col_addr) {
  1936. case 0x08:
  1937. mode |= (SDRAM_BXCF_M_AM_0 + ind);
  1938. break;
  1939. case 0x09:
  1940. mode |= (SDRAM_BXCF_M_AM_1 + ind);
  1941. break;
  1942. case 0x0A:
  1943. mode |= (SDRAM_BXCF_M_AM_2 + ind);
  1944. break;
  1945. case 0x0B:
  1946. mode |= (SDRAM_BXCF_M_AM_3 + ind);
  1947. break;
  1948. case 0x0C:
  1949. mode |= (SDRAM_BXCF_M_AM_4 + ind);
  1950. break;
  1951. default:
  1952. printf("DDR-SDRAM: DIMM %d BxCF configuration.\n",
  1953. (unsigned int)dimm_num);
  1954. printf("ERROR: Unsupported value for number of "
  1955. "column addresses: %d.\n", (unsigned int)num_col_addr);
  1956. printf("Replace the DIMM module with a supported DIMM.\n\n");
  1957. spd_ddr_init_hang ();
  1958. }
  1959. }
  1960. if ((dimm_populated[dimm_num] != SDRAM_NONE)&& (dimm_num ==1))
  1961. bank_0_populated = 1;
  1962. for (ind_rank = 0; ind_rank < num_ranks; ind_rank++) {
  1963. mtsdram(SDRAM_MB0CF +
  1964. ((dimm_num + bank_0_populated + ind_rank) << 2),
  1965. mode);
  1966. }
  1967. }
  1968. }
  1969. }
  1970. /*------------------------------------------------------------------
  1971. * program memory queue.
  1972. *-----------------------------------------------------------------*/
  1973. static void program_memory_queue(unsigned long *dimm_populated,
  1974. unsigned char *iic0_dimm_addr,
  1975. unsigned long num_dimm_banks)
  1976. {
  1977. unsigned long dimm_num;
  1978. phys_size_t rank_base_addr;
  1979. unsigned long rank_reg;
  1980. phys_size_t rank_size_bytes;
  1981. unsigned long rank_size_id;
  1982. unsigned long num_ranks;
  1983. unsigned long baseadd_size;
  1984. unsigned long i;
  1985. unsigned long bank_0_populated = 0;
  1986. phys_size_t total_size = 0;
  1987. /*------------------------------------------------------------------
  1988. * Reset the rank_base_address.
  1989. *-----------------------------------------------------------------*/
  1990. rank_reg = SDRAM_R0BAS;
  1991. rank_base_addr = 0x00000000;
  1992. for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
  1993. if (dimm_populated[dimm_num] != SDRAM_NONE) {
  1994. num_ranks = spd_read(iic0_dimm_addr[dimm_num], 5);
  1995. if ((spd_read(iic0_dimm_addr[dimm_num], 2)) == 0x08)
  1996. num_ranks = (num_ranks & 0x0F) + 1;
  1997. else
  1998. num_ranks = num_ranks & 0x0F;
  1999. rank_size_id = spd_read(iic0_dimm_addr[dimm_num], 31);
  2000. /*------------------------------------------------------------------
  2001. * Set the sizes
  2002. *-----------------------------------------------------------------*/
  2003. baseadd_size = 0;
  2004. switch (rank_size_id) {
  2005. case 0x01:
  2006. baseadd_size |= SDRAM_RXBAS_SDSZ_1024;
  2007. total_size = 1024;
  2008. break;
  2009. case 0x02:
  2010. baseadd_size |= SDRAM_RXBAS_SDSZ_2048;
  2011. total_size = 2048;
  2012. break;
  2013. case 0x04:
  2014. baseadd_size |= SDRAM_RXBAS_SDSZ_4096;
  2015. total_size = 4096;
  2016. break;
  2017. case 0x08:
  2018. baseadd_size |= SDRAM_RXBAS_SDSZ_32;
  2019. total_size = 32;
  2020. break;
  2021. case 0x10:
  2022. baseadd_size |= SDRAM_RXBAS_SDSZ_64;
  2023. total_size = 64;
  2024. break;
  2025. case 0x20:
  2026. baseadd_size |= SDRAM_RXBAS_SDSZ_128;
  2027. total_size = 128;
  2028. break;
  2029. case 0x40:
  2030. baseadd_size |= SDRAM_RXBAS_SDSZ_256;
  2031. total_size = 256;
  2032. break;
  2033. case 0x80:
  2034. baseadd_size |= SDRAM_RXBAS_SDSZ_512;
  2035. total_size = 512;
  2036. break;
  2037. default:
  2038. printf("DDR-SDRAM: DIMM %d memory queue configuration.\n",
  2039. (unsigned int)dimm_num);
  2040. printf("ERROR: Unsupported value for the banksize: %d.\n",
  2041. (unsigned int)rank_size_id);
  2042. printf("Replace the DIMM module with a supported DIMM.\n\n");
  2043. spd_ddr_init_hang ();
  2044. }
  2045. rank_size_bytes = total_size << 20;
  2046. if ((dimm_populated[dimm_num] != SDRAM_NONE) && (dimm_num == 1))
  2047. bank_0_populated = 1;
  2048. for (i = 0; i < num_ranks; i++) {
  2049. mtdcr_any(rank_reg+i+dimm_num+bank_0_populated,
  2050. (SDRAM_RXBAS_SDBA_ENCODE(rank_base_addr) |
  2051. baseadd_size));
  2052. rank_base_addr += rank_size_bytes;
  2053. }
  2054. }
  2055. }
  2056. #if defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
  2057. defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
  2058. defined(CONFIG_460SX)
  2059. /*
  2060. * Enable high bandwidth access
  2061. * This is currently not used, but with this setup
  2062. * it is possible to use it later on in e.g. the Linux
  2063. * EMAC driver for performance gain.
  2064. */
  2065. mtdcr(SDRAM_PLBADDULL, 0x00000000); /* MQ0_BAUL */
  2066. mtdcr(SDRAM_PLBADDUHB, 0x00000008); /* MQ0_BAUH */
  2067. /*
  2068. * Set optimal value for Memory Queue HB/LL Configuration registers
  2069. */
  2070. mtdcr(SDRAM_CONF1HB, (mfdcr(SDRAM_CONF1HB) & ~SDRAM_CONF1HB_MASK) |
  2071. SDRAM_CONF1HB_AAFR | SDRAM_CONF1HB_RPEN | SDRAM_CONF1HB_RFTE |
  2072. SDRAM_CONF1HB_RPLM | SDRAM_CONF1HB_WRCL);
  2073. mtdcr(SDRAM_CONF1LL, (mfdcr(SDRAM_CONF1LL) & ~SDRAM_CONF1LL_MASK) |
  2074. SDRAM_CONF1LL_AAFR | SDRAM_CONF1LL_RPEN | SDRAM_CONF1LL_RFTE |
  2075. SDRAM_CONF1LL_RPLM);
  2076. mtdcr(SDRAM_CONFPATHB, mfdcr(SDRAM_CONFPATHB) | SDRAM_CONFPATHB_TPEN);
  2077. #endif
  2078. }
  2079. #ifdef CONFIG_DDR_ECC
  2080. /*-----------------------------------------------------------------------------+
  2081. * program_ecc.
  2082. *-----------------------------------------------------------------------------*/
  2083. static void program_ecc(unsigned long *dimm_populated,
  2084. unsigned char *iic0_dimm_addr,
  2085. unsigned long num_dimm_banks,
  2086. unsigned long tlb_word2_i_value)
  2087. {
  2088. unsigned long dimm_num;
  2089. unsigned long ecc;
  2090. ecc = 0;
  2091. /* loop through all the DIMM slots on the board */
  2092. for (dimm_num = 0; dimm_num < MAXDIMMS; dimm_num++) {
  2093. /* If a dimm is installed in a particular slot ... */
  2094. if (dimm_populated[dimm_num] != SDRAM_NONE)
  2095. ecc = max(ecc, spd_read(iic0_dimm_addr[dimm_num], 11));
  2096. }
  2097. if (ecc == 0)
  2098. return;
  2099. do_program_ecc(tlb_word2_i_value);
  2100. }
  2101. #endif
  2102. #if !defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION)
  2103. /*-----------------------------------------------------------------------------+
  2104. * program_DQS_calibration.
  2105. *-----------------------------------------------------------------------------*/
  2106. static void program_DQS_calibration(unsigned long *dimm_populated,
  2107. unsigned char *iic0_dimm_addr,
  2108. unsigned long num_dimm_banks)
  2109. {
  2110. unsigned long val;
  2111. #ifdef HARD_CODED_DQS /* calibration test with hardvalues */
  2112. mtsdram(SDRAM_RQDC, 0x80000037);
  2113. mtsdram(SDRAM_RDCC, 0x40000000);
  2114. mtsdram(SDRAM_RFDC, 0x000001DF);
  2115. test();
  2116. #else
  2117. /*------------------------------------------------------------------
  2118. * Program RDCC register
  2119. * Read sample cycle auto-update enable
  2120. *-----------------------------------------------------------------*/
  2121. mfsdram(SDRAM_RDCC, val);
  2122. mtsdram(SDRAM_RDCC,
  2123. (val & ~(SDRAM_RDCC_RDSS_MASK | SDRAM_RDCC_RSAE_MASK))
  2124. | SDRAM_RDCC_RSAE_ENABLE);
  2125. /*------------------------------------------------------------------
  2126. * Program RQDC register
  2127. * Internal DQS delay mechanism enable
  2128. *-----------------------------------------------------------------*/
  2129. mtsdram(SDRAM_RQDC, (SDRAM_RQDC_RQDE_ENABLE|SDRAM_RQDC_RQFD_ENCODE(0x38)));
  2130. /*------------------------------------------------------------------
  2131. * Program RFDC register
  2132. * Set Feedback Fractional Oversample
  2133. * Auto-detect read sample cycle enable
  2134. * Set RFOS to 1/4 of memclk cycle (0x3f)
  2135. *-----------------------------------------------------------------*/
  2136. mfsdram(SDRAM_RFDC, val);
  2137. mtsdram(SDRAM_RFDC,
  2138. (val & ~(SDRAM_RFDC_ARSE_MASK | SDRAM_RFDC_RFOS_MASK |
  2139. SDRAM_RFDC_RFFD_MASK))
  2140. | (SDRAM_RFDC_ARSE_ENABLE | SDRAM_RFDC_RFOS_ENCODE(0x3f) |
  2141. SDRAM_RFDC_RFFD_ENCODE(0)));
  2142. DQS_calibration_process();
  2143. #endif
  2144. }
  2145. static int short_mem_test(void)
  2146. {
  2147. u32 *membase;
  2148. u32 bxcr_num;
  2149. u32 bxcf;
  2150. int i;
  2151. int j;
  2152. phys_size_t base_addr;
  2153. u32 test[NUMMEMTESTS][NUMMEMWORDS] = {
  2154. {0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
  2155. 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF},
  2156. {0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
  2157. 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000},
  2158. {0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
  2159. 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555},
  2160. {0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
  2161. 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA},
  2162. {0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
  2163. 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A},
  2164. {0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
  2165. 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5},
  2166. {0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
  2167. 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA},
  2168. {0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55,
  2169. 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55} };
  2170. int l;
  2171. for (bxcr_num = 0; bxcr_num < MAXBXCF; bxcr_num++) {
  2172. mfsdram(SDRAM_MB0CF + (bxcr_num << 2), bxcf);
  2173. /* Banks enabled */
  2174. if ((bxcf & SDRAM_BXCF_M_BE_MASK) == SDRAM_BXCF_M_BE_ENABLE) {
  2175. /* Bank is enabled */
  2176. /*
  2177. * Only run test on accessable memory (below 2GB)
  2178. */
  2179. base_addr = SDRAM_RXBAS_SDBA_DECODE(mfdcr_any(SDRAM_R0BAS+bxcr_num));
  2180. if (base_addr >= CONFIG_MAX_MEM_MAPPED)
  2181. continue;
  2182. /*------------------------------------------------------------------
  2183. * Run the short memory test.
  2184. *-----------------------------------------------------------------*/
  2185. membase = (u32 *)(u32)base_addr;
  2186. for (i = 0; i < NUMMEMTESTS; i++) {
  2187. for (j = 0; j < NUMMEMWORDS; j++) {
  2188. membase[j] = test[i][j];
  2189. ppcDcbf((u32)&(membase[j]));
  2190. }
  2191. sync();
  2192. for (l=0; l<NUMLOOPS; l++) {
  2193. for (j = 0; j < NUMMEMWORDS; j++) {
  2194. if (membase[j] != test[i][j]) {
  2195. ppcDcbf((u32)&(membase[j]));
  2196. return 0;
  2197. }
  2198. ppcDcbf((u32)&(membase[j]));
  2199. }
  2200. sync();
  2201. }
  2202. }
  2203. } /* if bank enabled */
  2204. } /* for bxcf_num */
  2205. return 1;
  2206. }
  2207. #ifndef HARD_CODED_DQS
  2208. /*-----------------------------------------------------------------------------+
  2209. * DQS_calibration_process.
  2210. *-----------------------------------------------------------------------------*/
  2211. static void DQS_calibration_process(void)
  2212. {
  2213. unsigned long rfdc_reg;
  2214. unsigned long rffd;
  2215. unsigned long val;
  2216. long rffd_average;
  2217. long max_start;
  2218. unsigned long dlycal;
  2219. unsigned long dly_val;
  2220. unsigned long max_pass_length;
  2221. unsigned long current_pass_length;
  2222. unsigned long current_fail_length;
  2223. unsigned long current_start;
  2224. long max_end;
  2225. unsigned char fail_found;
  2226. unsigned char pass_found;
  2227. #if !defined(CONFIG_DDR_RQDC_FIXED)
  2228. int window_found;
  2229. u32 rqdc_reg;
  2230. u32 rqfd;
  2231. u32 rqfd_start;
  2232. u32 rqfd_average;
  2233. int loopi = 0;
  2234. char str[] = "Auto calibration -";
  2235. char slash[] = "\\|/-\\|/-";
  2236. /*------------------------------------------------------------------
  2237. * Test to determine the best read clock delay tuning bits.
  2238. *
  2239. * Before the DDR controller can be used, the read clock delay needs to be
  2240. * set. This is SDRAM_RQDC[RQFD] and SDRAM_RFDC[RFFD].
  2241. * This value cannot be hardcoded into the program because it changes
  2242. * depending on the board's setup and environment.
  2243. * To do this, all delay values are tested to see if they
  2244. * work or not. By doing this, you get groups of fails with groups of
  2245. * passing values. The idea is to find the start and end of a passing
  2246. * window and take the center of it to use as the read clock delay.
  2247. *
  2248. * A failure has to be seen first so that when we hit a pass, we know
  2249. * that it is truely the start of the window. If we get passing values
  2250. * to start off with, we don't know if we are at the start of the window.
  2251. *
  2252. * The code assumes that a failure will always be found.
  2253. * If a failure is not found, there is no easy way to get the middle
  2254. * of the passing window. I guess we can pretty much pick any value
  2255. * but some values will be better than others. Since the lowest speed
  2256. * we can clock the DDR interface at is 200 MHz (2x 100 MHz PLB speed),
  2257. * from experimentation it is safe to say you will always have a failure.
  2258. *-----------------------------------------------------------------*/
  2259. /* first fix RQDC[RQFD] to an average of 80 degre phase shift to find RFDC[RFFD] */
  2260. rqfd_start = 64; /* test-only: don't know if this is the _best_ start value */
  2261. puts(str);
  2262. calibration_loop:
  2263. mfsdram(SDRAM_RQDC, rqdc_reg);
  2264. mtsdram(SDRAM_RQDC, (rqdc_reg & ~SDRAM_RQDC_RQFD_MASK) |
  2265. SDRAM_RQDC_RQFD_ENCODE(rqfd_start));
  2266. #else /* CONFIG_DDR_RQDC_FIXED */
  2267. /*
  2268. * On Katmai the complete auto-calibration somehow doesn't seem to
  2269. * produce the best results, meaning optimal values for RQFD/RFFD.
  2270. * This was discovered by GDA using a high bandwidth scope,
  2271. * analyzing the DDR2 signals. GDA provided a fixed value for RQFD,
  2272. * so now on Katmai "only" RFFD is auto-calibrated.
  2273. */
  2274. mtsdram(SDRAM_RQDC, CONFIG_DDR_RQDC_FIXED);
  2275. #endif /* CONFIG_DDR_RQDC_FIXED */
  2276. max_start = 0;
  2277. max_pass_length = 0;
  2278. max_start = 0;
  2279. max_end = 0;
  2280. current_pass_length = 0;
  2281. current_fail_length = 0;
  2282. current_start = 0;
  2283. fail_found = false;
  2284. pass_found = false;
  2285. /*
  2286. * get the delay line calibration register value
  2287. */
  2288. mfsdram(SDRAM_DLCR, dlycal);
  2289. dly_val = SDRAM_DLYCAL_DLCV_DECODE(dlycal) << 2;
  2290. for (rffd = 0; rffd <= SDRAM_RFDC_RFFD_MAX; rffd++) {
  2291. mfsdram(SDRAM_RFDC, rfdc_reg);
  2292. rfdc_reg &= ~(SDRAM_RFDC_RFFD_MASK);
  2293. /*------------------------------------------------------------------
  2294. * Set the timing reg for the test.
  2295. *-----------------------------------------------------------------*/
  2296. mtsdram(SDRAM_RFDC, rfdc_reg | SDRAM_RFDC_RFFD_ENCODE(rffd));
  2297. /*------------------------------------------------------------------
  2298. * See if the rffd value passed.
  2299. *-----------------------------------------------------------------*/
  2300. if (short_mem_test()) {
  2301. if (fail_found == true) {
  2302. pass_found = true;
  2303. if (current_pass_length == 0)
  2304. current_start = rffd;
  2305. current_fail_length = 0;
  2306. current_pass_length++;
  2307. if (current_pass_length > max_pass_length) {
  2308. max_pass_length = current_pass_length;
  2309. max_start = current_start;
  2310. max_end = rffd;
  2311. }
  2312. }
  2313. } else {
  2314. current_pass_length = 0;
  2315. current_fail_length++;
  2316. if (current_fail_length >= (dly_val >> 2)) {
  2317. if (fail_found == false)
  2318. fail_found = true;
  2319. else if (pass_found == true)
  2320. break;
  2321. }
  2322. }
  2323. } /* for rffd */
  2324. /*------------------------------------------------------------------
  2325. * Set the average RFFD value
  2326. *-----------------------------------------------------------------*/
  2327. rffd_average = ((max_start + max_end) >> 1);
  2328. if (rffd_average < 0)
  2329. rffd_average = 0;
  2330. if (rffd_average > SDRAM_RFDC_RFFD_MAX)
  2331. rffd_average = SDRAM_RFDC_RFFD_MAX;
  2332. /* now fix RFDC[RFFD] found and find RQDC[RQFD] */
  2333. mtsdram(SDRAM_RFDC, rfdc_reg | SDRAM_RFDC_RFFD_ENCODE(rffd_average));
  2334. #if !defined(CONFIG_DDR_RQDC_FIXED)
  2335. max_pass_length = 0;
  2336. max_start = 0;
  2337. max_end = 0;
  2338. current_pass_length = 0;
  2339. current_fail_length = 0;
  2340. current_start = 0;
  2341. window_found = false;
  2342. fail_found = false;
  2343. pass_found = false;
  2344. for (rqfd = 0; rqfd <= SDRAM_RQDC_RQFD_MAX; rqfd++) {
  2345. mfsdram(SDRAM_RQDC, rqdc_reg);
  2346. rqdc_reg &= ~(SDRAM_RQDC_RQFD_MASK);
  2347. /*------------------------------------------------------------------
  2348. * Set the timing reg for the test.
  2349. *-----------------------------------------------------------------*/
  2350. mtsdram(SDRAM_RQDC, rqdc_reg | SDRAM_RQDC_RQFD_ENCODE(rqfd));
  2351. /*------------------------------------------------------------------
  2352. * See if the rffd value passed.
  2353. *-----------------------------------------------------------------*/
  2354. if (short_mem_test()) {
  2355. if (fail_found == true) {
  2356. pass_found = true;
  2357. if (current_pass_length == 0)
  2358. current_start = rqfd;
  2359. current_fail_length = 0;
  2360. current_pass_length++;
  2361. if (current_pass_length > max_pass_length) {
  2362. max_pass_length = current_pass_length;
  2363. max_start = current_start;
  2364. max_end = rqfd;
  2365. }
  2366. }
  2367. } else {
  2368. current_pass_length = 0;
  2369. current_fail_length++;
  2370. if (fail_found == false) {
  2371. fail_found = true;
  2372. } else if (pass_found == true) {
  2373. window_found = true;
  2374. break;
  2375. }
  2376. }
  2377. }
  2378. rqfd_average = ((max_start + max_end) >> 1);
  2379. /*------------------------------------------------------------------
  2380. * Make sure we found the valid read passing window. Halt if not
  2381. *-----------------------------------------------------------------*/
  2382. if (window_found == false) {
  2383. if (rqfd_start < SDRAM_RQDC_RQFD_MAX) {
  2384. putc('\b');
  2385. putc(slash[loopi++ % 8]);
  2386. /* try again from with a different RQFD start value */
  2387. rqfd_start++;
  2388. goto calibration_loop;
  2389. }
  2390. printf("\nERROR: Cannot determine a common read delay for the "
  2391. "DIMM(s) installed.\n");
  2392. debug("%s[%d] ERROR : \n", __FUNCTION__,__LINE__);
  2393. ppc4xx_ibm_ddr2_register_dump();
  2394. spd_ddr_init_hang ();
  2395. }
  2396. if (rqfd_average < 0)
  2397. rqfd_average = 0;
  2398. if (rqfd_average > SDRAM_RQDC_RQFD_MAX)
  2399. rqfd_average = SDRAM_RQDC_RQFD_MAX;
  2400. mtsdram(SDRAM_RQDC,
  2401. (rqdc_reg & ~SDRAM_RQDC_RQFD_MASK) |
  2402. SDRAM_RQDC_RQFD_ENCODE(rqfd_average));
  2403. blank_string(strlen(str));
  2404. #endif /* CONFIG_DDR_RQDC_FIXED */
  2405. mfsdram(SDRAM_DLCR, val);
  2406. debug("%s[%d] DLCR: 0x%08lX\n", __FUNCTION__, __LINE__, val);
  2407. mfsdram(SDRAM_RQDC, val);
  2408. debug("%s[%d] RQDC: 0x%08lX\n", __FUNCTION__, __LINE__, val);
  2409. mfsdram(SDRAM_RFDC, val);
  2410. debug("%s[%d] RFDC: 0x%08lX\n", __FUNCTION__, __LINE__, val);
  2411. mfsdram(SDRAM_RDCC, val);
  2412. debug("%s[%d] RDCC: 0x%08lX\n", __FUNCTION__, __LINE__, val);
  2413. }
  2414. #else /* calibration test with hardvalues */
  2415. /*-----------------------------------------------------------------------------+
  2416. * DQS_calibration_process.
  2417. *-----------------------------------------------------------------------------*/
  2418. static void test(void)
  2419. {
  2420. unsigned long dimm_num;
  2421. unsigned long ecc_temp;
  2422. unsigned long i, j;
  2423. unsigned long *membase;
  2424. unsigned long bxcf[MAXRANKS];
  2425. unsigned long val;
  2426. char window_found;
  2427. char begin_found[MAXDIMMS];
  2428. char end_found[MAXDIMMS];
  2429. char search_end[MAXDIMMS];
  2430. unsigned long test[NUMMEMTESTS][NUMMEMWORDS] = {
  2431. {0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
  2432. 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF},
  2433. {0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
  2434. 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000},
  2435. {0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
  2436. 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555},
  2437. {0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
  2438. 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA},
  2439. {0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
  2440. 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A},
  2441. {0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
  2442. 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5},
  2443. {0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
  2444. 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA},
  2445. {0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55,
  2446. 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55} };
  2447. /*------------------------------------------------------------------
  2448. * Test to determine the best read clock delay tuning bits.
  2449. *
  2450. * Before the DDR controller can be used, the read clock delay needs to be
  2451. * set. This is SDRAM_RQDC[RQFD] and SDRAM_RFDC[RFFD].
  2452. * This value cannot be hardcoded into the program because it changes
  2453. * depending on the board's setup and environment.
  2454. * To do this, all delay values are tested to see if they
  2455. * work or not. By doing this, you get groups of fails with groups of
  2456. * passing values. The idea is to find the start and end of a passing
  2457. * window and take the center of it to use as the read clock delay.
  2458. *
  2459. * A failure has to be seen first so that when we hit a pass, we know
  2460. * that it is truely the start of the window. If we get passing values
  2461. * to start off with, we don't know if we are at the start of the window.
  2462. *
  2463. * The code assumes that a failure will always be found.
  2464. * If a failure is not found, there is no easy way to get the middle
  2465. * of the passing window. I guess we can pretty much pick any value
  2466. * but some values will be better than others. Since the lowest speed
  2467. * we can clock the DDR interface at is 200 MHz (2x 100 MHz PLB speed),
  2468. * from experimentation it is safe to say you will always have a failure.
  2469. *-----------------------------------------------------------------*/
  2470. mfsdram(SDRAM_MCOPT1, ecc_temp);
  2471. ecc_temp &= SDRAM_MCOPT1_MCHK_MASK;
  2472. mfsdram(SDRAM_MCOPT1, val);
  2473. mtsdram(SDRAM_MCOPT1, (val & ~SDRAM_MCOPT1_MCHK_MASK) |
  2474. SDRAM_MCOPT1_MCHK_NON);
  2475. window_found = false;
  2476. begin_found[0] = false;
  2477. end_found[0] = false;
  2478. search_end[0] = false;
  2479. begin_found[1] = false;
  2480. end_found[1] = false;
  2481. search_end[1] = false;
  2482. for (dimm_num = 0; dimm_num < MAXDIMMS; dimm_num++) {
  2483. mfsdram(SDRAM_MB0CF + (bxcr_num << 2), bxcf[bxcr_num]);
  2484. /* Banks enabled */
  2485. if ((bxcf[dimm_num] & SDRAM_BXCF_M_BE_MASK) == SDRAM_BXCF_M_BE_ENABLE) {
  2486. /* Bank is enabled */
  2487. membase =
  2488. (unsigned long*)(SDRAM_RXBAS_SDBA_DECODE(mfdcr_any(SDRAM_R0BAS+dimm_num)));
  2489. /*------------------------------------------------------------------
  2490. * Run the short memory test.
  2491. *-----------------------------------------------------------------*/
  2492. for (i = 0; i < NUMMEMTESTS; i++) {
  2493. for (j = 0; j < NUMMEMWORDS; j++) {
  2494. membase[j] = test[i][j];
  2495. ppcDcbf((u32)&(membase[j]));
  2496. }
  2497. sync();
  2498. for (j = 0; j < NUMMEMWORDS; j++) {
  2499. if (membase[j] != test[i][j]) {
  2500. ppcDcbf((u32)&(membase[j]));
  2501. break;
  2502. }
  2503. ppcDcbf((u32)&(membase[j]));
  2504. }
  2505. sync();
  2506. if (j < NUMMEMWORDS)
  2507. break;
  2508. }
  2509. /*------------------------------------------------------------------
  2510. * See if the rffd value passed.
  2511. *-----------------------------------------------------------------*/
  2512. if (i < NUMMEMTESTS) {
  2513. if ((end_found[dimm_num] == false) &&
  2514. (search_end[dimm_num] == true)) {
  2515. end_found[dimm_num] = true;
  2516. }
  2517. if ((end_found[0] == true) &&
  2518. (end_found[1] == true))
  2519. break;
  2520. } else {
  2521. if (begin_found[dimm_num] == false) {
  2522. begin_found[dimm_num] = true;
  2523. search_end[dimm_num] = true;
  2524. }
  2525. }
  2526. } else {
  2527. begin_found[dimm_num] = true;
  2528. end_found[dimm_num] = true;
  2529. }
  2530. }
  2531. if ((begin_found[0] == true) && (begin_found[1] == true))
  2532. window_found = true;
  2533. /*------------------------------------------------------------------
  2534. * Make sure we found the valid read passing window. Halt if not
  2535. *-----------------------------------------------------------------*/
  2536. if (window_found == false) {
  2537. printf("ERROR: Cannot determine a common read delay for the "
  2538. "DIMM(s) installed.\n");
  2539. spd_ddr_init_hang ();
  2540. }
  2541. /*------------------------------------------------------------------
  2542. * Restore the ECC variable to what it originally was
  2543. *-----------------------------------------------------------------*/
  2544. mtsdram(SDRAM_MCOPT1,
  2545. (ppcMfdcr_sdram(SDRAM_MCOPT1) & ~SDRAM_MCOPT1_MCHK_MASK)
  2546. | ecc_temp);
  2547. }
  2548. #endif /* !HARD_CODED_DQS */
  2549. #endif /* !defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION) */
  2550. #else /* CONFIG_SPD_EEPROM */
  2551. /*-----------------------------------------------------------------------------
  2552. * Function: initdram
  2553. * Description: Configures the PPC4xx IBM DDR1/DDR2 SDRAM memory controller.
  2554. * The configuration is performed using static, compile-
  2555. * time parameters.
  2556. * Configures the PPC405EX(r) and PPC460EX/GT
  2557. *---------------------------------------------------------------------------*/
  2558. phys_size_t initdram(int board_type)
  2559. {
  2560. unsigned long val;
  2561. #if defined(CONFIG_440)
  2562. mtdcr(SDRAM_R0BAS, CONFIG_SYS_SDRAM_R0BAS);
  2563. mtdcr(SDRAM_R1BAS, CONFIG_SYS_SDRAM_R1BAS);
  2564. mtdcr(SDRAM_R2BAS, CONFIG_SYS_SDRAM_R2BAS);
  2565. mtdcr(SDRAM_R3BAS, CONFIG_SYS_SDRAM_R3BAS);
  2566. mtdcr(SDRAM_PLBADDULL, CONFIG_SYS_SDRAM_PLBADDULL); /* MQ0_BAUL */
  2567. mtdcr(SDRAM_PLBADDUHB, CONFIG_SYS_SDRAM_PLBADDUHB); /* MQ0_BAUH */
  2568. mtdcr(SDRAM_CONF1LL, CONFIG_SYS_SDRAM_CONF1LL);
  2569. mtdcr(SDRAM_CONF1HB, CONFIG_SYS_SDRAM_CONF1HB);
  2570. mtdcr(SDRAM_CONFPATHB, CONFIG_SYS_SDRAM_CONFPATHB);
  2571. #endif
  2572. /* Set Memory Bank Configuration Registers */
  2573. mtsdram(SDRAM_MB0CF, CONFIG_SYS_SDRAM0_MB0CF);
  2574. mtsdram(SDRAM_MB1CF, CONFIG_SYS_SDRAM0_MB1CF);
  2575. mtsdram(SDRAM_MB2CF, CONFIG_SYS_SDRAM0_MB2CF);
  2576. mtsdram(SDRAM_MB3CF, CONFIG_SYS_SDRAM0_MB3CF);
  2577. /* Set Memory Clock Timing Register */
  2578. mtsdram(SDRAM_CLKTR, CONFIG_SYS_SDRAM0_CLKTR);
  2579. /* Set Refresh Time Register */
  2580. mtsdram(SDRAM_RTR, CONFIG_SYS_SDRAM0_RTR);
  2581. /* Set SDRAM Timing Registers */
  2582. mtsdram(SDRAM_SDTR1, CONFIG_SYS_SDRAM0_SDTR1);
  2583. mtsdram(SDRAM_SDTR2, CONFIG_SYS_SDRAM0_SDTR2);
  2584. mtsdram(SDRAM_SDTR3, CONFIG_SYS_SDRAM0_SDTR3);
  2585. /* Set Mode and Extended Mode Registers */
  2586. mtsdram(SDRAM_MMODE, CONFIG_SYS_SDRAM0_MMODE);
  2587. mtsdram(SDRAM_MEMODE, CONFIG_SYS_SDRAM0_MEMODE);
  2588. /* Set Memory Controller Options 1 Register */
  2589. mtsdram(SDRAM_MCOPT1, CONFIG_SYS_SDRAM0_MCOPT1);
  2590. /* Set Manual Initialization Control Registers */
  2591. mtsdram(SDRAM_INITPLR0, CONFIG_SYS_SDRAM0_INITPLR0);
  2592. mtsdram(SDRAM_INITPLR1, CONFIG_SYS_SDRAM0_INITPLR1);
  2593. mtsdram(SDRAM_INITPLR2, CONFIG_SYS_SDRAM0_INITPLR2);
  2594. mtsdram(SDRAM_INITPLR3, CONFIG_SYS_SDRAM0_INITPLR3);
  2595. mtsdram(SDRAM_INITPLR4, CONFIG_SYS_SDRAM0_INITPLR4);
  2596. mtsdram(SDRAM_INITPLR5, CONFIG_SYS_SDRAM0_INITPLR5);
  2597. mtsdram(SDRAM_INITPLR6, CONFIG_SYS_SDRAM0_INITPLR6);
  2598. mtsdram(SDRAM_INITPLR7, CONFIG_SYS_SDRAM0_INITPLR7);
  2599. mtsdram(SDRAM_INITPLR8, CONFIG_SYS_SDRAM0_INITPLR8);
  2600. mtsdram(SDRAM_INITPLR9, CONFIG_SYS_SDRAM0_INITPLR9);
  2601. mtsdram(SDRAM_INITPLR10, CONFIG_SYS_SDRAM0_INITPLR10);
  2602. mtsdram(SDRAM_INITPLR11, CONFIG_SYS_SDRAM0_INITPLR11);
  2603. mtsdram(SDRAM_INITPLR12, CONFIG_SYS_SDRAM0_INITPLR12);
  2604. mtsdram(SDRAM_INITPLR13, CONFIG_SYS_SDRAM0_INITPLR13);
  2605. mtsdram(SDRAM_INITPLR14, CONFIG_SYS_SDRAM0_INITPLR14);
  2606. mtsdram(SDRAM_INITPLR15, CONFIG_SYS_SDRAM0_INITPLR15);
  2607. /* Set On-Die Termination Registers */
  2608. mtsdram(SDRAM_CODT, CONFIG_SYS_SDRAM0_CODT);
  2609. mtsdram(SDRAM_MODT0, CONFIG_SYS_SDRAM0_MODT0);
  2610. mtsdram(SDRAM_MODT1, CONFIG_SYS_SDRAM0_MODT1);
  2611. /* Set Write Timing Register */
  2612. mtsdram(SDRAM_WRDTR, CONFIG_SYS_SDRAM0_WRDTR);
  2613. /*
  2614. * Start Initialization by SDRAM0_MCOPT2[SREN] = 0 and
  2615. * SDRAM0_MCOPT2[IPTR] = 1
  2616. */
  2617. mtsdram(SDRAM_MCOPT2, (SDRAM_MCOPT2_SREN_EXIT |
  2618. SDRAM_MCOPT2_IPTR_EXECUTE));
  2619. /*
  2620. * Poll SDRAM0_MCSTAT[MIC] for assertion to indicate the
  2621. * completion of initialization.
  2622. */
  2623. do {
  2624. mfsdram(SDRAM_MCSTAT, val);
  2625. } while ((val & SDRAM_MCSTAT_MIC_MASK) != SDRAM_MCSTAT_MIC_COMP);
  2626. /* Set Delay Control Registers */
  2627. mtsdram(SDRAM_DLCR, CONFIG_SYS_SDRAM0_DLCR);
  2628. #if !defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION)
  2629. mtsdram(SDRAM_RDCC, CONFIG_SYS_SDRAM0_RDCC);
  2630. mtsdram(SDRAM_RQDC, CONFIG_SYS_SDRAM0_RQDC);
  2631. mtsdram(SDRAM_RFDC, CONFIG_SYS_SDRAM0_RFDC);
  2632. #endif /* !CONFIG_PPC4xx_DDR_AUTOCALIBRATION */
  2633. /*
  2634. * Enable Controller by SDRAM0_MCOPT2[DCEN] = 1:
  2635. */
  2636. mfsdram(SDRAM_MCOPT2, val);
  2637. mtsdram(SDRAM_MCOPT2, val | SDRAM_MCOPT2_DCEN_ENABLE);
  2638. #if defined(CONFIG_440)
  2639. /*
  2640. * Program TLB entries with caches enabled, for best performace
  2641. * while auto-calibrating and ECC generation
  2642. */
  2643. program_tlb(0, 0, (CONFIG_SYS_MBYTES_SDRAM << 20), 0);
  2644. #endif
  2645. #if defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION)
  2646. /*------------------------------------------------------------------
  2647. | DQS calibration.
  2648. +-----------------------------------------------------------------*/
  2649. DQS_autocalibration();
  2650. #endif /* CONFIG_PPC4xx_DDR_AUTOCALIBRATION */
  2651. /*
  2652. * Now complete RDSS configuration as mentioned on page 7 of the AMCC
  2653. * PowerPC440SP/SPe DDR2 application note:
  2654. * "DDR1/DDR2 Initialization Sequence and Dynamic Tuning"
  2655. */
  2656. update_rdcc();
  2657. #if defined(CONFIG_DDR_ECC)
  2658. do_program_ecc(0);
  2659. #endif /* defined(CONFIG_DDR_ECC) */
  2660. #if defined(CONFIG_440)
  2661. /*
  2662. * Now after initialization (auto-calibration and ECC generation)
  2663. * remove the TLB entries with caches enabled and program again with
  2664. * desired cache functionality
  2665. */
  2666. remove_tlb(0, (CONFIG_SYS_MBYTES_SDRAM << 20));
  2667. program_tlb(0, 0, (CONFIG_SYS_MBYTES_SDRAM << 20), MY_TLB_WORD2_I_ENABLE);
  2668. #endif
  2669. ppc4xx_ibm_ddr2_register_dump();
  2670. #if defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION)
  2671. /*
  2672. * Clear potential errors resulting from auto-calibration.
  2673. * If not done, then we could get an interrupt later on when
  2674. * exceptions are enabled.
  2675. */
  2676. set_mcsr(get_mcsr());
  2677. #endif /* CONFIG_PPC4xx_DDR_AUTOCALIBRATION */
  2678. return (CONFIG_SYS_MBYTES_SDRAM << 20);
  2679. }
  2680. #endif /* CONFIG_SPD_EEPROM */
  2681. #if defined(CONFIG_440)
  2682. u32 mfdcr_any(u32 dcr)
  2683. {
  2684. u32 val;
  2685. switch (dcr) {
  2686. case SDRAM_R0BAS + 0:
  2687. val = mfdcr(SDRAM_R0BAS + 0);
  2688. break;
  2689. case SDRAM_R0BAS + 1:
  2690. val = mfdcr(SDRAM_R0BAS + 1);
  2691. break;
  2692. case SDRAM_R0BAS + 2:
  2693. val = mfdcr(SDRAM_R0BAS + 2);
  2694. break;
  2695. case SDRAM_R0BAS + 3:
  2696. val = mfdcr(SDRAM_R0BAS + 3);
  2697. break;
  2698. default:
  2699. printf("DCR %d not defined in case statement!!!\n", dcr);
  2700. val = 0; /* just to satisfy the compiler */
  2701. }
  2702. return val;
  2703. }
  2704. void mtdcr_any(u32 dcr, u32 val)
  2705. {
  2706. switch (dcr) {
  2707. case SDRAM_R0BAS + 0:
  2708. mtdcr(SDRAM_R0BAS + 0, val);
  2709. break;
  2710. case SDRAM_R0BAS + 1:
  2711. mtdcr(SDRAM_R0BAS + 1, val);
  2712. break;
  2713. case SDRAM_R0BAS + 2:
  2714. mtdcr(SDRAM_R0BAS + 2, val);
  2715. break;
  2716. case SDRAM_R0BAS + 3:
  2717. mtdcr(SDRAM_R0BAS + 3, val);
  2718. break;
  2719. default:
  2720. printf("DCR %d not defined in case statement!!!\n", dcr);
  2721. }
  2722. }
  2723. #endif /* defined(CONFIG_440) */
  2724. inline void ppc4xx_ibm_ddr2_register_dump(void)
  2725. {
  2726. #if defined(DEBUG)
  2727. printf("\nPPC4xx IBM DDR2 Register Dump:\n");
  2728. #if (defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
  2729. defined(CONFIG_460EX) || defined(CONFIG_460GT))
  2730. PPC4xx_IBM_DDR2_DUMP_MQ_REGISTER(R0BAS);
  2731. PPC4xx_IBM_DDR2_DUMP_MQ_REGISTER(R1BAS);
  2732. PPC4xx_IBM_DDR2_DUMP_MQ_REGISTER(R2BAS);
  2733. PPC4xx_IBM_DDR2_DUMP_MQ_REGISTER(R3BAS);
  2734. #endif /* (defined(CONFIG_440SP) || ... */
  2735. #if defined(CONFIG_405EX)
  2736. PPC4xx_IBM_DDR2_DUMP_REGISTER(BESR);
  2737. PPC4xx_IBM_DDR2_DUMP_REGISTER(BEARL);
  2738. PPC4xx_IBM_DDR2_DUMP_REGISTER(BEARH);
  2739. PPC4xx_IBM_DDR2_DUMP_REGISTER(WMIRQ);
  2740. PPC4xx_IBM_DDR2_DUMP_REGISTER(PLBOPT);
  2741. PPC4xx_IBM_DDR2_DUMP_REGISTER(PUABA);
  2742. #endif /* defined(CONFIG_405EX) */
  2743. PPC4xx_IBM_DDR2_DUMP_REGISTER(MB0CF);
  2744. PPC4xx_IBM_DDR2_DUMP_REGISTER(MB1CF);
  2745. PPC4xx_IBM_DDR2_DUMP_REGISTER(MB2CF);
  2746. PPC4xx_IBM_DDR2_DUMP_REGISTER(MB3CF);
  2747. PPC4xx_IBM_DDR2_DUMP_REGISTER(MCSTAT);
  2748. PPC4xx_IBM_DDR2_DUMP_REGISTER(MCOPT1);
  2749. PPC4xx_IBM_DDR2_DUMP_REGISTER(MCOPT2);
  2750. PPC4xx_IBM_DDR2_DUMP_REGISTER(MODT0);
  2751. PPC4xx_IBM_DDR2_DUMP_REGISTER(MODT1);
  2752. PPC4xx_IBM_DDR2_DUMP_REGISTER(MODT2);
  2753. PPC4xx_IBM_DDR2_DUMP_REGISTER(MODT3);
  2754. PPC4xx_IBM_DDR2_DUMP_REGISTER(CODT);
  2755. #if (defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
  2756. defined(CONFIG_460EX) || defined(CONFIG_460GT))
  2757. PPC4xx_IBM_DDR2_DUMP_REGISTER(VVPR);
  2758. PPC4xx_IBM_DDR2_DUMP_REGISTER(OPARS);
  2759. /*
  2760. * OPART is only used as a trigger register.
  2761. *
  2762. * No data is contained in this register, and reading or writing
  2763. * to is can cause bad things to happen (hangs). Just skip it and
  2764. * report "N/A".
  2765. */
  2766. printf("%20s = N/A\n", "SDRAM_OPART");
  2767. #endif /* defined(CONFIG_440SP) || ... */
  2768. PPC4xx_IBM_DDR2_DUMP_REGISTER(RTR);
  2769. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR0);
  2770. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR1);
  2771. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR2);
  2772. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR3);
  2773. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR4);
  2774. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR5);
  2775. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR6);
  2776. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR7);
  2777. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR8);
  2778. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR9);
  2779. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR10);
  2780. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR11);
  2781. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR12);
  2782. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR13);
  2783. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR14);
  2784. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR15);
  2785. PPC4xx_IBM_DDR2_DUMP_REGISTER(RQDC);
  2786. PPC4xx_IBM_DDR2_DUMP_REGISTER(RFDC);
  2787. PPC4xx_IBM_DDR2_DUMP_REGISTER(RDCC);
  2788. PPC4xx_IBM_DDR2_DUMP_REGISTER(DLCR);
  2789. PPC4xx_IBM_DDR2_DUMP_REGISTER(CLKTR);
  2790. PPC4xx_IBM_DDR2_DUMP_REGISTER(WRDTR);
  2791. PPC4xx_IBM_DDR2_DUMP_REGISTER(SDTR1);
  2792. PPC4xx_IBM_DDR2_DUMP_REGISTER(SDTR2);
  2793. PPC4xx_IBM_DDR2_DUMP_REGISTER(SDTR3);
  2794. PPC4xx_IBM_DDR2_DUMP_REGISTER(MMODE);
  2795. PPC4xx_IBM_DDR2_DUMP_REGISTER(MEMODE);
  2796. PPC4xx_IBM_DDR2_DUMP_REGISTER(ECCES);
  2797. #if (defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
  2798. defined(CONFIG_460EX) || defined(CONFIG_460GT))
  2799. PPC4xx_IBM_DDR2_DUMP_REGISTER(CID);
  2800. #endif /* defined(CONFIG_440SP) || ... */
  2801. PPC4xx_IBM_DDR2_DUMP_REGISTER(RID);
  2802. PPC4xx_IBM_DDR2_DUMP_REGISTER(FCSR);
  2803. PPC4xx_IBM_DDR2_DUMP_REGISTER(RTSR);
  2804. #endif /* defined(DEBUG) */
  2805. }