44x_spd_ddr2.c 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139
  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, (unsigned long)
  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,
  1670. (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 29) >> 2);
  1671. t_rrd_ns = max(t_rrd_ns,
  1672. (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 28) >> 2);
  1673. t_rp_ns = max(t_rp_ns,
  1674. (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 27) >> 2);
  1675. t_ras_ns = max(t_ras_ns,
  1676. (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 30));
  1677. t_rc_ns = max(t_rc_ns,
  1678. (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 41));
  1679. t_rfc_ns = max(t_rfc_ns,
  1680. (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 42));
  1681. }
  1682. }
  1683. /*------------------------------------------------------------------
  1684. * Set the SDRAM Timing Reg 1, SDRAM_TR1
  1685. *-----------------------------------------------------------------*/
  1686. mfsdram(SDRAM_SDTR1, sdtr1);
  1687. sdtr1 &= ~(SDRAM_SDTR1_LDOF_MASK | SDRAM_SDTR1_RTW_MASK |
  1688. SDRAM_SDTR1_WTWO_MASK | SDRAM_SDTR1_RTRO_MASK);
  1689. /* default values */
  1690. sdtr1 |= SDRAM_SDTR1_LDOF_2_CLK;
  1691. sdtr1 |= SDRAM_SDTR1_RTW_2_CLK;
  1692. /* normal operations */
  1693. sdtr1 |= SDRAM_SDTR1_WTWO_0_CLK;
  1694. sdtr1 |= SDRAM_SDTR1_RTRO_1_CLK;
  1695. mtsdram(SDRAM_SDTR1, sdtr1);
  1696. /*------------------------------------------------------------------
  1697. * Set the SDRAM Timing Reg 2, SDRAM_TR2
  1698. *-----------------------------------------------------------------*/
  1699. mfsdram(SDRAM_SDTR2, sdtr2);
  1700. sdtr2 &= ~(SDRAM_SDTR2_RCD_MASK | SDRAM_SDTR2_WTR_MASK |
  1701. SDRAM_SDTR2_XSNR_MASK | SDRAM_SDTR2_WPC_MASK |
  1702. SDRAM_SDTR2_RPC_MASK | SDRAM_SDTR2_RP_MASK |
  1703. SDRAM_SDTR2_RRD_MASK);
  1704. /*
  1705. * convert t_rcd from nanoseconds to ddr clocks
  1706. * round up if necessary
  1707. */
  1708. t_rcd_clk = MULDIV64(sdram_freq, t_rcd_ns, ONE_BILLION);
  1709. ddr_check = MULDIV64(ONE_BILLION, t_rcd_clk, t_rcd_ns);
  1710. if (sdram_freq != ddr_check)
  1711. t_rcd_clk++;
  1712. switch (t_rcd_clk) {
  1713. case 0:
  1714. case 1:
  1715. sdtr2 |= SDRAM_SDTR2_RCD_1_CLK;
  1716. break;
  1717. case 2:
  1718. sdtr2 |= SDRAM_SDTR2_RCD_2_CLK;
  1719. break;
  1720. case 3:
  1721. sdtr2 |= SDRAM_SDTR2_RCD_3_CLK;
  1722. break;
  1723. case 4:
  1724. sdtr2 |= SDRAM_SDTR2_RCD_4_CLK;
  1725. break;
  1726. default:
  1727. sdtr2 |= SDRAM_SDTR2_RCD_5_CLK;
  1728. break;
  1729. }
  1730. if (sdram_ddr1 == true) { /* DDR1 */
  1731. if (sdram_freq < 200000000) {
  1732. sdtr2 |= SDRAM_SDTR2_WTR_1_CLK;
  1733. sdtr2 |= SDRAM_SDTR2_WPC_2_CLK;
  1734. sdtr2 |= SDRAM_SDTR2_RPC_2_CLK;
  1735. } else {
  1736. sdtr2 |= SDRAM_SDTR2_WTR_2_CLK;
  1737. sdtr2 |= SDRAM_SDTR2_WPC_3_CLK;
  1738. sdtr2 |= SDRAM_SDTR2_RPC_2_CLK;
  1739. }
  1740. } else { /* DDR2 */
  1741. /* loop through all the DIMM slots on the board */
  1742. for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
  1743. /* If a dimm is installed in a particular slot ... */
  1744. if (dimm_populated[dimm_num] != SDRAM_NONE) {
  1745. t_wpc_ns = max(t_wtr_ns,
  1746. (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 36) >> 2);
  1747. t_wtr_ns = max(t_wtr_ns,
  1748. (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 37) >> 2);
  1749. t_rpc_ns = max(t_rpc_ns,
  1750. (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 38) >> 2);
  1751. }
  1752. }
  1753. /*
  1754. * convert from nanoseconds to ddr clocks
  1755. * round up if necessary
  1756. */
  1757. t_wpc_clk = MULDIV64(sdram_freq, t_wpc_ns, ONE_BILLION);
  1758. ddr_check = MULDIV64(ONE_BILLION, t_wpc_clk, t_wpc_ns);
  1759. if (sdram_freq != ddr_check)
  1760. t_wpc_clk++;
  1761. switch (t_wpc_clk) {
  1762. case 0:
  1763. case 1:
  1764. case 2:
  1765. sdtr2 |= SDRAM_SDTR2_WPC_2_CLK;
  1766. break;
  1767. case 3:
  1768. sdtr2 |= SDRAM_SDTR2_WPC_3_CLK;
  1769. break;
  1770. case 4:
  1771. sdtr2 |= SDRAM_SDTR2_WPC_4_CLK;
  1772. break;
  1773. case 5:
  1774. sdtr2 |= SDRAM_SDTR2_WPC_5_CLK;
  1775. break;
  1776. default:
  1777. sdtr2 |= SDRAM_SDTR2_WPC_6_CLK;
  1778. break;
  1779. }
  1780. /*
  1781. * convert from nanoseconds to ddr clocks
  1782. * round up if necessary
  1783. */
  1784. t_wtr_clk = MULDIV64(sdram_freq, t_wtr_ns, ONE_BILLION);
  1785. ddr_check = MULDIV64(ONE_BILLION, t_wtr_clk, t_wtr_ns);
  1786. if (sdram_freq != ddr_check)
  1787. t_wtr_clk++;
  1788. switch (t_wtr_clk) {
  1789. case 0:
  1790. case 1:
  1791. sdtr2 |= SDRAM_SDTR2_WTR_1_CLK;
  1792. break;
  1793. case 2:
  1794. sdtr2 |= SDRAM_SDTR2_WTR_2_CLK;
  1795. break;
  1796. case 3:
  1797. sdtr2 |= SDRAM_SDTR2_WTR_3_CLK;
  1798. break;
  1799. default:
  1800. sdtr2 |= SDRAM_SDTR2_WTR_4_CLK;
  1801. break;
  1802. }
  1803. /*
  1804. * convert from nanoseconds to ddr clocks
  1805. * round up if necessary
  1806. */
  1807. t_rpc_clk = MULDIV64(sdram_freq, t_rpc_ns, ONE_BILLION);
  1808. ddr_check = MULDIV64(ONE_BILLION, t_rpc_clk, t_rpc_ns);
  1809. if (sdram_freq != ddr_check)
  1810. t_rpc_clk++;
  1811. switch (t_rpc_clk) {
  1812. case 0:
  1813. case 1:
  1814. case 2:
  1815. sdtr2 |= SDRAM_SDTR2_RPC_2_CLK;
  1816. break;
  1817. case 3:
  1818. sdtr2 |= SDRAM_SDTR2_RPC_3_CLK;
  1819. break;
  1820. default:
  1821. sdtr2 |= SDRAM_SDTR2_RPC_4_CLK;
  1822. break;
  1823. }
  1824. }
  1825. /* default value */
  1826. sdtr2 |= SDRAM_SDTR2_XSNR_16_CLK;
  1827. /*
  1828. * convert t_rrd from nanoseconds to ddr clocks
  1829. * round up if necessary
  1830. */
  1831. t_rrd_clk = MULDIV64(sdram_freq, t_rrd_ns, ONE_BILLION);
  1832. ddr_check = MULDIV64(ONE_BILLION, t_rrd_clk, t_rrd_ns);
  1833. if (sdram_freq != ddr_check)
  1834. t_rrd_clk++;
  1835. if (t_rrd_clk == 3)
  1836. sdtr2 |= SDRAM_SDTR2_RRD_3_CLK;
  1837. else
  1838. sdtr2 |= SDRAM_SDTR2_RRD_2_CLK;
  1839. /*
  1840. * convert t_rp from nanoseconds to ddr clocks
  1841. * round up if necessary
  1842. */
  1843. t_rp_clk = MULDIV64(sdram_freq, t_rp_ns, ONE_BILLION);
  1844. ddr_check = MULDIV64(ONE_BILLION, t_rp_clk, t_rp_ns);
  1845. if (sdram_freq != ddr_check)
  1846. t_rp_clk++;
  1847. switch (t_rp_clk) {
  1848. case 0:
  1849. case 1:
  1850. case 2:
  1851. case 3:
  1852. sdtr2 |= SDRAM_SDTR2_RP_3_CLK;
  1853. break;
  1854. case 4:
  1855. sdtr2 |= SDRAM_SDTR2_RP_4_CLK;
  1856. break;
  1857. case 5:
  1858. sdtr2 |= SDRAM_SDTR2_RP_5_CLK;
  1859. break;
  1860. case 6:
  1861. sdtr2 |= SDRAM_SDTR2_RP_6_CLK;
  1862. break;
  1863. default:
  1864. sdtr2 |= SDRAM_SDTR2_RP_7_CLK;
  1865. break;
  1866. }
  1867. mtsdram(SDRAM_SDTR2, sdtr2);
  1868. /*------------------------------------------------------------------
  1869. * Set the SDRAM Timing Reg 3, SDRAM_TR3
  1870. *-----------------------------------------------------------------*/
  1871. mfsdram(SDRAM_SDTR3, sdtr3);
  1872. sdtr3 &= ~(SDRAM_SDTR3_RAS_MASK | SDRAM_SDTR3_RC_MASK |
  1873. SDRAM_SDTR3_XCS_MASK | SDRAM_SDTR3_RFC_MASK);
  1874. /*
  1875. * convert t_ras from nanoseconds to ddr clocks
  1876. * round up if necessary
  1877. */
  1878. t_ras_clk = MULDIV64(sdram_freq, t_ras_ns, ONE_BILLION);
  1879. ddr_check = MULDIV64(ONE_BILLION, t_ras_clk, t_ras_ns);
  1880. if (sdram_freq != ddr_check)
  1881. t_ras_clk++;
  1882. sdtr3 |= SDRAM_SDTR3_RAS_ENCODE(t_ras_clk);
  1883. /*
  1884. * convert t_rc from nanoseconds to ddr clocks
  1885. * round up if necessary
  1886. */
  1887. t_rc_clk = MULDIV64(sdram_freq, t_rc_ns, ONE_BILLION);
  1888. ddr_check = MULDIV64(ONE_BILLION, t_rc_clk, t_rc_ns);
  1889. if (sdram_freq != ddr_check)
  1890. t_rc_clk++;
  1891. sdtr3 |= SDRAM_SDTR3_RC_ENCODE(t_rc_clk);
  1892. /* default xcs value */
  1893. sdtr3 |= SDRAM_SDTR3_XCS;
  1894. /*
  1895. * convert t_rfc from nanoseconds to ddr clocks
  1896. * round up if necessary
  1897. */
  1898. t_rfc_clk = MULDIV64(sdram_freq, t_rfc_ns, ONE_BILLION);
  1899. ddr_check = MULDIV64(ONE_BILLION, t_rfc_clk, t_rfc_ns);
  1900. if (sdram_freq != ddr_check)
  1901. t_rfc_clk++;
  1902. sdtr3 |= SDRAM_SDTR3_RFC_ENCODE(t_rfc_clk);
  1903. mtsdram(SDRAM_SDTR3, sdtr3);
  1904. }
  1905. /*-----------------------------------------------------------------------------+
  1906. * program_bxcf.
  1907. *-----------------------------------------------------------------------------*/
  1908. static void program_bxcf(unsigned long *dimm_populated,
  1909. unsigned char *iic0_dimm_addr,
  1910. unsigned long num_dimm_banks)
  1911. {
  1912. unsigned long dimm_num;
  1913. unsigned long num_col_addr;
  1914. unsigned long num_ranks;
  1915. unsigned long num_banks;
  1916. unsigned long mode;
  1917. unsigned long ind_rank;
  1918. unsigned long ind;
  1919. unsigned long ind_bank;
  1920. unsigned long bank_0_populated;
  1921. /*------------------------------------------------------------------
  1922. * Set the BxCF regs. First, wipe out the bank config registers.
  1923. *-----------------------------------------------------------------*/
  1924. mtsdram(SDRAM_MB0CF, 0x00000000);
  1925. mtsdram(SDRAM_MB1CF, 0x00000000);
  1926. mtsdram(SDRAM_MB2CF, 0x00000000);
  1927. mtsdram(SDRAM_MB3CF, 0x00000000);
  1928. mode = SDRAM_BXCF_M_BE_ENABLE;
  1929. bank_0_populated = 0;
  1930. for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
  1931. if (dimm_populated[dimm_num] != SDRAM_NONE) {
  1932. num_col_addr = spd_read(iic0_dimm_addr[dimm_num], 4);
  1933. num_ranks = spd_read(iic0_dimm_addr[dimm_num], 5);
  1934. if ((spd_read(iic0_dimm_addr[dimm_num], 2)) == 0x08)
  1935. num_ranks = (num_ranks & 0x0F) +1;
  1936. else
  1937. num_ranks = num_ranks & 0x0F;
  1938. num_banks = spd_read(iic0_dimm_addr[dimm_num], 17);
  1939. for (ind_bank = 0; ind_bank < 2; ind_bank++) {
  1940. if (num_banks == 4)
  1941. ind = 0;
  1942. else
  1943. ind = 5 << 8;
  1944. switch (num_col_addr) {
  1945. case 0x08:
  1946. mode |= (SDRAM_BXCF_M_AM_0 + ind);
  1947. break;
  1948. case 0x09:
  1949. mode |= (SDRAM_BXCF_M_AM_1 + ind);
  1950. break;
  1951. case 0x0A:
  1952. mode |= (SDRAM_BXCF_M_AM_2 + ind);
  1953. break;
  1954. case 0x0B:
  1955. mode |= (SDRAM_BXCF_M_AM_3 + ind);
  1956. break;
  1957. case 0x0C:
  1958. mode |= (SDRAM_BXCF_M_AM_4 + ind);
  1959. break;
  1960. default:
  1961. printf("DDR-SDRAM: DIMM %d BxCF configuration.\n",
  1962. (unsigned int)dimm_num);
  1963. printf("ERROR: Unsupported value for number of "
  1964. "column addresses: %d.\n", (unsigned int)num_col_addr);
  1965. printf("Replace the DIMM module with a supported DIMM.\n\n");
  1966. spd_ddr_init_hang ();
  1967. }
  1968. }
  1969. if ((dimm_populated[dimm_num] != SDRAM_NONE)&& (dimm_num ==1))
  1970. bank_0_populated = 1;
  1971. for (ind_rank = 0; ind_rank < num_ranks; ind_rank++) {
  1972. mtsdram(SDRAM_MB0CF +
  1973. ((dimm_num + bank_0_populated + ind_rank) << 2),
  1974. mode);
  1975. }
  1976. }
  1977. }
  1978. }
  1979. /*------------------------------------------------------------------
  1980. * program memory queue.
  1981. *-----------------------------------------------------------------*/
  1982. static void program_memory_queue(unsigned long *dimm_populated,
  1983. unsigned char *iic0_dimm_addr,
  1984. unsigned long num_dimm_banks)
  1985. {
  1986. unsigned long dimm_num;
  1987. phys_size_t rank_base_addr;
  1988. unsigned long rank_reg;
  1989. phys_size_t rank_size_bytes;
  1990. unsigned long rank_size_id;
  1991. unsigned long num_ranks;
  1992. unsigned long baseadd_size;
  1993. unsigned long i;
  1994. unsigned long bank_0_populated = 0;
  1995. phys_size_t total_size = 0;
  1996. /*------------------------------------------------------------------
  1997. * Reset the rank_base_address.
  1998. *-----------------------------------------------------------------*/
  1999. rank_reg = SDRAM_R0BAS;
  2000. rank_base_addr = 0x00000000;
  2001. for (dimm_num = 0; dimm_num < num_dimm_banks; dimm_num++) {
  2002. if (dimm_populated[dimm_num] != SDRAM_NONE) {
  2003. num_ranks = spd_read(iic0_dimm_addr[dimm_num], 5);
  2004. if ((spd_read(iic0_dimm_addr[dimm_num], 2)) == 0x08)
  2005. num_ranks = (num_ranks & 0x0F) + 1;
  2006. else
  2007. num_ranks = num_ranks & 0x0F;
  2008. rank_size_id = spd_read(iic0_dimm_addr[dimm_num], 31);
  2009. /*------------------------------------------------------------------
  2010. * Set the sizes
  2011. *-----------------------------------------------------------------*/
  2012. baseadd_size = 0;
  2013. switch (rank_size_id) {
  2014. case 0x01:
  2015. baseadd_size |= SDRAM_RXBAS_SDSZ_1024;
  2016. total_size = 1024;
  2017. break;
  2018. case 0x02:
  2019. baseadd_size |= SDRAM_RXBAS_SDSZ_2048;
  2020. total_size = 2048;
  2021. break;
  2022. case 0x04:
  2023. baseadd_size |= SDRAM_RXBAS_SDSZ_4096;
  2024. total_size = 4096;
  2025. break;
  2026. case 0x08:
  2027. baseadd_size |= SDRAM_RXBAS_SDSZ_32;
  2028. total_size = 32;
  2029. break;
  2030. case 0x10:
  2031. baseadd_size |= SDRAM_RXBAS_SDSZ_64;
  2032. total_size = 64;
  2033. break;
  2034. case 0x20:
  2035. baseadd_size |= SDRAM_RXBAS_SDSZ_128;
  2036. total_size = 128;
  2037. break;
  2038. case 0x40:
  2039. baseadd_size |= SDRAM_RXBAS_SDSZ_256;
  2040. total_size = 256;
  2041. break;
  2042. case 0x80:
  2043. baseadd_size |= SDRAM_RXBAS_SDSZ_512;
  2044. total_size = 512;
  2045. break;
  2046. default:
  2047. printf("DDR-SDRAM: DIMM %d memory queue configuration.\n",
  2048. (unsigned int)dimm_num);
  2049. printf("ERROR: Unsupported value for the banksize: %d.\n",
  2050. (unsigned int)rank_size_id);
  2051. printf("Replace the DIMM module with a supported DIMM.\n\n");
  2052. spd_ddr_init_hang ();
  2053. }
  2054. rank_size_bytes = total_size << 20;
  2055. if ((dimm_populated[dimm_num] != SDRAM_NONE) && (dimm_num == 1))
  2056. bank_0_populated = 1;
  2057. for (i = 0; i < num_ranks; i++) {
  2058. mtdcr_any(rank_reg+i+dimm_num+bank_0_populated,
  2059. (SDRAM_RXBAS_SDBA_ENCODE(rank_base_addr) |
  2060. baseadd_size));
  2061. rank_base_addr += rank_size_bytes;
  2062. }
  2063. }
  2064. }
  2065. #if defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
  2066. defined(CONFIG_460EX) || defined(CONFIG_460GT) || \
  2067. defined(CONFIG_460SX)
  2068. /*
  2069. * Enable high bandwidth access
  2070. * This is currently not used, but with this setup
  2071. * it is possible to use it later on in e.g. the Linux
  2072. * EMAC driver for performance gain.
  2073. */
  2074. mtdcr(SDRAM_PLBADDULL, 0x00000000); /* MQ0_BAUL */
  2075. mtdcr(SDRAM_PLBADDUHB, 0x00000008); /* MQ0_BAUH */
  2076. /*
  2077. * Set optimal value for Memory Queue HB/LL Configuration registers
  2078. */
  2079. mtdcr(SDRAM_CONF1HB, (mfdcr(SDRAM_CONF1HB) & ~SDRAM_CONF1HB_MASK) |
  2080. SDRAM_CONF1HB_AAFR | SDRAM_CONF1HB_RPEN | SDRAM_CONF1HB_RFTE |
  2081. SDRAM_CONF1HB_RPLM | SDRAM_CONF1HB_WRCL);
  2082. mtdcr(SDRAM_CONF1LL, (mfdcr(SDRAM_CONF1LL) & ~SDRAM_CONF1LL_MASK) |
  2083. SDRAM_CONF1LL_AAFR | SDRAM_CONF1LL_RPEN | SDRAM_CONF1LL_RFTE |
  2084. SDRAM_CONF1LL_RPLM);
  2085. mtdcr(SDRAM_CONFPATHB, mfdcr(SDRAM_CONFPATHB) | SDRAM_CONFPATHB_TPEN);
  2086. #endif
  2087. }
  2088. #ifdef CONFIG_DDR_ECC
  2089. /*-----------------------------------------------------------------------------+
  2090. * program_ecc.
  2091. *-----------------------------------------------------------------------------*/
  2092. static void program_ecc(unsigned long *dimm_populated,
  2093. unsigned char *iic0_dimm_addr,
  2094. unsigned long num_dimm_banks,
  2095. unsigned long tlb_word2_i_value)
  2096. {
  2097. unsigned long dimm_num;
  2098. unsigned long ecc;
  2099. ecc = 0;
  2100. /* loop through all the DIMM slots on the board */
  2101. for (dimm_num = 0; dimm_num < MAXDIMMS; dimm_num++) {
  2102. /* If a dimm is installed in a particular slot ... */
  2103. if (dimm_populated[dimm_num] != SDRAM_NONE)
  2104. ecc = max(ecc,
  2105. (unsigned long)spd_read(iic0_dimm_addr[dimm_num], 11));
  2106. }
  2107. if (ecc == 0)
  2108. return;
  2109. do_program_ecc(tlb_word2_i_value);
  2110. }
  2111. #endif
  2112. #if !defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION)
  2113. /*-----------------------------------------------------------------------------+
  2114. * program_DQS_calibration.
  2115. *-----------------------------------------------------------------------------*/
  2116. static void program_DQS_calibration(unsigned long *dimm_populated,
  2117. unsigned char *iic0_dimm_addr,
  2118. unsigned long num_dimm_banks)
  2119. {
  2120. unsigned long val;
  2121. #ifdef HARD_CODED_DQS /* calibration test with hardvalues */
  2122. mtsdram(SDRAM_RQDC, 0x80000037);
  2123. mtsdram(SDRAM_RDCC, 0x40000000);
  2124. mtsdram(SDRAM_RFDC, 0x000001DF);
  2125. test();
  2126. #else
  2127. /*------------------------------------------------------------------
  2128. * Program RDCC register
  2129. * Read sample cycle auto-update enable
  2130. *-----------------------------------------------------------------*/
  2131. mfsdram(SDRAM_RDCC, val);
  2132. mtsdram(SDRAM_RDCC,
  2133. (val & ~(SDRAM_RDCC_RDSS_MASK | SDRAM_RDCC_RSAE_MASK))
  2134. | SDRAM_RDCC_RSAE_ENABLE);
  2135. /*------------------------------------------------------------------
  2136. * Program RQDC register
  2137. * Internal DQS delay mechanism enable
  2138. *-----------------------------------------------------------------*/
  2139. mtsdram(SDRAM_RQDC, (SDRAM_RQDC_RQDE_ENABLE|SDRAM_RQDC_RQFD_ENCODE(0x38)));
  2140. /*------------------------------------------------------------------
  2141. * Program RFDC register
  2142. * Set Feedback Fractional Oversample
  2143. * Auto-detect read sample cycle enable
  2144. * Set RFOS to 1/4 of memclk cycle (0x3f)
  2145. *-----------------------------------------------------------------*/
  2146. mfsdram(SDRAM_RFDC, val);
  2147. mtsdram(SDRAM_RFDC,
  2148. (val & ~(SDRAM_RFDC_ARSE_MASK | SDRAM_RFDC_RFOS_MASK |
  2149. SDRAM_RFDC_RFFD_MASK))
  2150. | (SDRAM_RFDC_ARSE_ENABLE | SDRAM_RFDC_RFOS_ENCODE(0x3f) |
  2151. SDRAM_RFDC_RFFD_ENCODE(0)));
  2152. DQS_calibration_process();
  2153. #endif
  2154. }
  2155. static int short_mem_test(void)
  2156. {
  2157. u32 *membase;
  2158. u32 bxcr_num;
  2159. u32 bxcf;
  2160. int i;
  2161. int j;
  2162. phys_size_t base_addr;
  2163. u32 test[NUMMEMTESTS][NUMMEMWORDS] = {
  2164. {0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
  2165. 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF},
  2166. {0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
  2167. 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000},
  2168. {0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
  2169. 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555},
  2170. {0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
  2171. 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA},
  2172. {0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
  2173. 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A},
  2174. {0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
  2175. 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5},
  2176. {0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
  2177. 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA},
  2178. {0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55,
  2179. 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55} };
  2180. int l;
  2181. for (bxcr_num = 0; bxcr_num < MAXBXCF; bxcr_num++) {
  2182. mfsdram(SDRAM_MB0CF + (bxcr_num << 2), bxcf);
  2183. /* Banks enabled */
  2184. if ((bxcf & SDRAM_BXCF_M_BE_MASK) == SDRAM_BXCF_M_BE_ENABLE) {
  2185. /* Bank is enabled */
  2186. /*
  2187. * Only run test on accessable memory (below 2GB)
  2188. */
  2189. base_addr = SDRAM_RXBAS_SDBA_DECODE(mfdcr_any(SDRAM_R0BAS+bxcr_num));
  2190. if (base_addr >= CONFIG_MAX_MEM_MAPPED)
  2191. continue;
  2192. /*------------------------------------------------------------------
  2193. * Run the short memory test.
  2194. *-----------------------------------------------------------------*/
  2195. membase = (u32 *)(u32)base_addr;
  2196. for (i = 0; i < NUMMEMTESTS; i++) {
  2197. for (j = 0; j < NUMMEMWORDS; j++) {
  2198. membase[j] = test[i][j];
  2199. ppcDcbf((u32)&(membase[j]));
  2200. }
  2201. sync();
  2202. for (l=0; l<NUMLOOPS; l++) {
  2203. for (j = 0; j < NUMMEMWORDS; j++) {
  2204. if (membase[j] != test[i][j]) {
  2205. ppcDcbf((u32)&(membase[j]));
  2206. return 0;
  2207. }
  2208. ppcDcbf((u32)&(membase[j]));
  2209. }
  2210. sync();
  2211. }
  2212. }
  2213. } /* if bank enabled */
  2214. } /* for bxcf_num */
  2215. return 1;
  2216. }
  2217. #ifndef HARD_CODED_DQS
  2218. /*-----------------------------------------------------------------------------+
  2219. * DQS_calibration_process.
  2220. *-----------------------------------------------------------------------------*/
  2221. static void DQS_calibration_process(void)
  2222. {
  2223. unsigned long rfdc_reg;
  2224. unsigned long rffd;
  2225. unsigned long val;
  2226. long rffd_average;
  2227. long max_start;
  2228. unsigned long dlycal;
  2229. unsigned long dly_val;
  2230. unsigned long max_pass_length;
  2231. unsigned long current_pass_length;
  2232. unsigned long current_fail_length;
  2233. unsigned long current_start;
  2234. long max_end;
  2235. unsigned char fail_found;
  2236. unsigned char pass_found;
  2237. #if !defined(CONFIG_DDR_RQDC_FIXED)
  2238. int window_found;
  2239. u32 rqdc_reg;
  2240. u32 rqfd;
  2241. u32 rqfd_start;
  2242. u32 rqfd_average;
  2243. int loopi = 0;
  2244. char str[] = "Auto calibration -";
  2245. char slash[] = "\\|/-\\|/-";
  2246. /*------------------------------------------------------------------
  2247. * Test to determine the best read clock delay tuning bits.
  2248. *
  2249. * Before the DDR controller can be used, the read clock delay needs to be
  2250. * set. This is SDRAM_RQDC[RQFD] and SDRAM_RFDC[RFFD].
  2251. * This value cannot be hardcoded into the program because it changes
  2252. * depending on the board's setup and environment.
  2253. * To do this, all delay values are tested to see if they
  2254. * work or not. By doing this, you get groups of fails with groups of
  2255. * passing values. The idea is to find the start and end of a passing
  2256. * window and take the center of it to use as the read clock delay.
  2257. *
  2258. * A failure has to be seen first so that when we hit a pass, we know
  2259. * that it is truely the start of the window. If we get passing values
  2260. * to start off with, we don't know if we are at the start of the window.
  2261. *
  2262. * The code assumes that a failure will always be found.
  2263. * If a failure is not found, there is no easy way to get the middle
  2264. * of the passing window. I guess we can pretty much pick any value
  2265. * but some values will be better than others. Since the lowest speed
  2266. * we can clock the DDR interface at is 200 MHz (2x 100 MHz PLB speed),
  2267. * from experimentation it is safe to say you will always have a failure.
  2268. *-----------------------------------------------------------------*/
  2269. /* first fix RQDC[RQFD] to an average of 80 degre phase shift to find RFDC[RFFD] */
  2270. rqfd_start = 64; /* test-only: don't know if this is the _best_ start value */
  2271. puts(str);
  2272. calibration_loop:
  2273. mfsdram(SDRAM_RQDC, rqdc_reg);
  2274. mtsdram(SDRAM_RQDC, (rqdc_reg & ~SDRAM_RQDC_RQFD_MASK) |
  2275. SDRAM_RQDC_RQFD_ENCODE(rqfd_start));
  2276. #else /* CONFIG_DDR_RQDC_FIXED */
  2277. /*
  2278. * On Katmai the complete auto-calibration somehow doesn't seem to
  2279. * produce the best results, meaning optimal values for RQFD/RFFD.
  2280. * This was discovered by GDA using a high bandwidth scope,
  2281. * analyzing the DDR2 signals. GDA provided a fixed value for RQFD,
  2282. * so now on Katmai "only" RFFD is auto-calibrated.
  2283. */
  2284. mtsdram(SDRAM_RQDC, CONFIG_DDR_RQDC_FIXED);
  2285. #endif /* CONFIG_DDR_RQDC_FIXED */
  2286. max_start = 0;
  2287. max_pass_length = 0;
  2288. max_start = 0;
  2289. max_end = 0;
  2290. current_pass_length = 0;
  2291. current_fail_length = 0;
  2292. current_start = 0;
  2293. fail_found = false;
  2294. pass_found = false;
  2295. /*
  2296. * get the delay line calibration register value
  2297. */
  2298. mfsdram(SDRAM_DLCR, dlycal);
  2299. dly_val = SDRAM_DLYCAL_DLCV_DECODE(dlycal) << 2;
  2300. for (rffd = 0; rffd <= SDRAM_RFDC_RFFD_MAX; rffd++) {
  2301. mfsdram(SDRAM_RFDC, rfdc_reg);
  2302. rfdc_reg &= ~(SDRAM_RFDC_RFFD_MASK);
  2303. /*------------------------------------------------------------------
  2304. * Set the timing reg for the test.
  2305. *-----------------------------------------------------------------*/
  2306. mtsdram(SDRAM_RFDC, rfdc_reg | SDRAM_RFDC_RFFD_ENCODE(rffd));
  2307. /*------------------------------------------------------------------
  2308. * See if the rffd value passed.
  2309. *-----------------------------------------------------------------*/
  2310. if (short_mem_test()) {
  2311. if (fail_found == true) {
  2312. pass_found = true;
  2313. if (current_pass_length == 0)
  2314. current_start = rffd;
  2315. current_fail_length = 0;
  2316. current_pass_length++;
  2317. if (current_pass_length > max_pass_length) {
  2318. max_pass_length = current_pass_length;
  2319. max_start = current_start;
  2320. max_end = rffd;
  2321. }
  2322. }
  2323. } else {
  2324. current_pass_length = 0;
  2325. current_fail_length++;
  2326. if (current_fail_length >= (dly_val >> 2)) {
  2327. if (fail_found == false)
  2328. fail_found = true;
  2329. else if (pass_found == true)
  2330. break;
  2331. }
  2332. }
  2333. } /* for rffd */
  2334. /*------------------------------------------------------------------
  2335. * Set the average RFFD value
  2336. *-----------------------------------------------------------------*/
  2337. rffd_average = ((max_start + max_end) >> 1);
  2338. if (rffd_average < 0)
  2339. rffd_average = 0;
  2340. if (rffd_average > SDRAM_RFDC_RFFD_MAX)
  2341. rffd_average = SDRAM_RFDC_RFFD_MAX;
  2342. /* now fix RFDC[RFFD] found and find RQDC[RQFD] */
  2343. mtsdram(SDRAM_RFDC, rfdc_reg | SDRAM_RFDC_RFFD_ENCODE(rffd_average));
  2344. #if !defined(CONFIG_DDR_RQDC_FIXED)
  2345. max_pass_length = 0;
  2346. max_start = 0;
  2347. max_end = 0;
  2348. current_pass_length = 0;
  2349. current_fail_length = 0;
  2350. current_start = 0;
  2351. window_found = false;
  2352. fail_found = false;
  2353. pass_found = false;
  2354. for (rqfd = 0; rqfd <= SDRAM_RQDC_RQFD_MAX; rqfd++) {
  2355. mfsdram(SDRAM_RQDC, rqdc_reg);
  2356. rqdc_reg &= ~(SDRAM_RQDC_RQFD_MASK);
  2357. /*------------------------------------------------------------------
  2358. * Set the timing reg for the test.
  2359. *-----------------------------------------------------------------*/
  2360. mtsdram(SDRAM_RQDC, rqdc_reg | SDRAM_RQDC_RQFD_ENCODE(rqfd));
  2361. /*------------------------------------------------------------------
  2362. * See if the rffd value passed.
  2363. *-----------------------------------------------------------------*/
  2364. if (short_mem_test()) {
  2365. if (fail_found == true) {
  2366. pass_found = true;
  2367. if (current_pass_length == 0)
  2368. current_start = rqfd;
  2369. current_fail_length = 0;
  2370. current_pass_length++;
  2371. if (current_pass_length > max_pass_length) {
  2372. max_pass_length = current_pass_length;
  2373. max_start = current_start;
  2374. max_end = rqfd;
  2375. }
  2376. }
  2377. } else {
  2378. current_pass_length = 0;
  2379. current_fail_length++;
  2380. if (fail_found == false) {
  2381. fail_found = true;
  2382. } else if (pass_found == true) {
  2383. window_found = true;
  2384. break;
  2385. }
  2386. }
  2387. }
  2388. rqfd_average = ((max_start + max_end) >> 1);
  2389. /*------------------------------------------------------------------
  2390. * Make sure we found the valid read passing window. Halt if not
  2391. *-----------------------------------------------------------------*/
  2392. if (window_found == false) {
  2393. if (rqfd_start < SDRAM_RQDC_RQFD_MAX) {
  2394. putc('\b');
  2395. putc(slash[loopi++ % 8]);
  2396. /* try again from with a different RQFD start value */
  2397. rqfd_start++;
  2398. goto calibration_loop;
  2399. }
  2400. printf("\nERROR: Cannot determine a common read delay for the "
  2401. "DIMM(s) installed.\n");
  2402. debug("%s[%d] ERROR : \n", __FUNCTION__,__LINE__);
  2403. ppc4xx_ibm_ddr2_register_dump();
  2404. spd_ddr_init_hang ();
  2405. }
  2406. if (rqfd_average < 0)
  2407. rqfd_average = 0;
  2408. if (rqfd_average > SDRAM_RQDC_RQFD_MAX)
  2409. rqfd_average = SDRAM_RQDC_RQFD_MAX;
  2410. mtsdram(SDRAM_RQDC,
  2411. (rqdc_reg & ~SDRAM_RQDC_RQFD_MASK) |
  2412. SDRAM_RQDC_RQFD_ENCODE(rqfd_average));
  2413. blank_string(strlen(str));
  2414. #endif /* CONFIG_DDR_RQDC_FIXED */
  2415. mfsdram(SDRAM_DLCR, val);
  2416. debug("%s[%d] DLCR: 0x%08lX\n", __FUNCTION__, __LINE__, val);
  2417. mfsdram(SDRAM_RQDC, val);
  2418. debug("%s[%d] RQDC: 0x%08lX\n", __FUNCTION__, __LINE__, val);
  2419. mfsdram(SDRAM_RFDC, val);
  2420. debug("%s[%d] RFDC: 0x%08lX\n", __FUNCTION__, __LINE__, val);
  2421. mfsdram(SDRAM_RDCC, val);
  2422. debug("%s[%d] RDCC: 0x%08lX\n", __FUNCTION__, __LINE__, val);
  2423. }
  2424. #else /* calibration test with hardvalues */
  2425. /*-----------------------------------------------------------------------------+
  2426. * DQS_calibration_process.
  2427. *-----------------------------------------------------------------------------*/
  2428. static void test(void)
  2429. {
  2430. unsigned long dimm_num;
  2431. unsigned long ecc_temp;
  2432. unsigned long i, j;
  2433. unsigned long *membase;
  2434. unsigned long bxcf[MAXRANKS];
  2435. unsigned long val;
  2436. char window_found;
  2437. char begin_found[MAXDIMMS];
  2438. char end_found[MAXDIMMS];
  2439. char search_end[MAXDIMMS];
  2440. unsigned long test[NUMMEMTESTS][NUMMEMWORDS] = {
  2441. {0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF,
  2442. 0x00000000, 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF},
  2443. {0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000,
  2444. 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000},
  2445. {0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555,
  2446. 0xAAAAAAAA, 0xAAAAAAAA, 0x55555555, 0x55555555},
  2447. {0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA,
  2448. 0x55555555, 0x55555555, 0xAAAAAAAA, 0xAAAAAAAA},
  2449. {0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A,
  2450. 0xA5A5A5A5, 0xA5A5A5A5, 0x5A5A5A5A, 0x5A5A5A5A},
  2451. {0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5,
  2452. 0x5A5A5A5A, 0x5A5A5A5A, 0xA5A5A5A5, 0xA5A5A5A5},
  2453. {0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA,
  2454. 0xAA55AA55, 0xAA55AA55, 0x55AA55AA, 0x55AA55AA},
  2455. {0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55,
  2456. 0x55AA55AA, 0x55AA55AA, 0xAA55AA55, 0xAA55AA55} };
  2457. /*------------------------------------------------------------------
  2458. * Test to determine the best read clock delay tuning bits.
  2459. *
  2460. * Before the DDR controller can be used, the read clock delay needs to be
  2461. * set. This is SDRAM_RQDC[RQFD] and SDRAM_RFDC[RFFD].
  2462. * This value cannot be hardcoded into the program because it changes
  2463. * depending on the board's setup and environment.
  2464. * To do this, all delay values are tested to see if they
  2465. * work or not. By doing this, you get groups of fails with groups of
  2466. * passing values. The idea is to find the start and end of a passing
  2467. * window and take the center of it to use as the read clock delay.
  2468. *
  2469. * A failure has to be seen first so that when we hit a pass, we know
  2470. * that it is truely the start of the window. If we get passing values
  2471. * to start off with, we don't know if we are at the start of the window.
  2472. *
  2473. * The code assumes that a failure will always be found.
  2474. * If a failure is not found, there is no easy way to get the middle
  2475. * of the passing window. I guess we can pretty much pick any value
  2476. * but some values will be better than others. Since the lowest speed
  2477. * we can clock the DDR interface at is 200 MHz (2x 100 MHz PLB speed),
  2478. * from experimentation it is safe to say you will always have a failure.
  2479. *-----------------------------------------------------------------*/
  2480. mfsdram(SDRAM_MCOPT1, ecc_temp);
  2481. ecc_temp &= SDRAM_MCOPT1_MCHK_MASK;
  2482. mfsdram(SDRAM_MCOPT1, val);
  2483. mtsdram(SDRAM_MCOPT1, (val & ~SDRAM_MCOPT1_MCHK_MASK) |
  2484. SDRAM_MCOPT1_MCHK_NON);
  2485. window_found = false;
  2486. begin_found[0] = false;
  2487. end_found[0] = false;
  2488. search_end[0] = false;
  2489. begin_found[1] = false;
  2490. end_found[1] = false;
  2491. search_end[1] = false;
  2492. for (dimm_num = 0; dimm_num < MAXDIMMS; dimm_num++) {
  2493. mfsdram(SDRAM_MB0CF + (bxcr_num << 2), bxcf[bxcr_num]);
  2494. /* Banks enabled */
  2495. if ((bxcf[dimm_num] & SDRAM_BXCF_M_BE_MASK) == SDRAM_BXCF_M_BE_ENABLE) {
  2496. /* Bank is enabled */
  2497. membase =
  2498. (unsigned long*)(SDRAM_RXBAS_SDBA_DECODE(mfdcr_any(SDRAM_R0BAS+dimm_num)));
  2499. /*------------------------------------------------------------------
  2500. * Run the short memory test.
  2501. *-----------------------------------------------------------------*/
  2502. for (i = 0; i < NUMMEMTESTS; i++) {
  2503. for (j = 0; j < NUMMEMWORDS; j++) {
  2504. membase[j] = test[i][j];
  2505. ppcDcbf((u32)&(membase[j]));
  2506. }
  2507. sync();
  2508. for (j = 0; j < NUMMEMWORDS; j++) {
  2509. if (membase[j] != test[i][j]) {
  2510. ppcDcbf((u32)&(membase[j]));
  2511. break;
  2512. }
  2513. ppcDcbf((u32)&(membase[j]));
  2514. }
  2515. sync();
  2516. if (j < NUMMEMWORDS)
  2517. break;
  2518. }
  2519. /*------------------------------------------------------------------
  2520. * See if the rffd value passed.
  2521. *-----------------------------------------------------------------*/
  2522. if (i < NUMMEMTESTS) {
  2523. if ((end_found[dimm_num] == false) &&
  2524. (search_end[dimm_num] == true)) {
  2525. end_found[dimm_num] = true;
  2526. }
  2527. if ((end_found[0] == true) &&
  2528. (end_found[1] == true))
  2529. break;
  2530. } else {
  2531. if (begin_found[dimm_num] == false) {
  2532. begin_found[dimm_num] = true;
  2533. search_end[dimm_num] = true;
  2534. }
  2535. }
  2536. } else {
  2537. begin_found[dimm_num] = true;
  2538. end_found[dimm_num] = true;
  2539. }
  2540. }
  2541. if ((begin_found[0] == true) && (begin_found[1] == true))
  2542. window_found = true;
  2543. /*------------------------------------------------------------------
  2544. * Make sure we found the valid read passing window. Halt if not
  2545. *-----------------------------------------------------------------*/
  2546. if (window_found == false) {
  2547. printf("ERROR: Cannot determine a common read delay for the "
  2548. "DIMM(s) installed.\n");
  2549. spd_ddr_init_hang ();
  2550. }
  2551. /*------------------------------------------------------------------
  2552. * Restore the ECC variable to what it originally was
  2553. *-----------------------------------------------------------------*/
  2554. mtsdram(SDRAM_MCOPT1,
  2555. (ppcMfdcr_sdram(SDRAM_MCOPT1) & ~SDRAM_MCOPT1_MCHK_MASK)
  2556. | ecc_temp);
  2557. }
  2558. #endif /* !HARD_CODED_DQS */
  2559. #endif /* !defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION) */
  2560. #else /* CONFIG_SPD_EEPROM */
  2561. /*-----------------------------------------------------------------------------
  2562. * Function: initdram
  2563. * Description: Configures the PPC4xx IBM DDR1/DDR2 SDRAM memory controller.
  2564. * The configuration is performed using static, compile-
  2565. * time parameters.
  2566. * Configures the PPC405EX(r) and PPC460EX/GT
  2567. *---------------------------------------------------------------------------*/
  2568. phys_size_t initdram(int board_type)
  2569. {
  2570. unsigned long val;
  2571. #if defined(CONFIG_440)
  2572. mtdcr(SDRAM_R0BAS, CONFIG_SYS_SDRAM_R0BAS);
  2573. mtdcr(SDRAM_R1BAS, CONFIG_SYS_SDRAM_R1BAS);
  2574. mtdcr(SDRAM_R2BAS, CONFIG_SYS_SDRAM_R2BAS);
  2575. mtdcr(SDRAM_R3BAS, CONFIG_SYS_SDRAM_R3BAS);
  2576. mtdcr(SDRAM_PLBADDULL, CONFIG_SYS_SDRAM_PLBADDULL); /* MQ0_BAUL */
  2577. mtdcr(SDRAM_PLBADDUHB, CONFIG_SYS_SDRAM_PLBADDUHB); /* MQ0_BAUH */
  2578. mtdcr(SDRAM_CONF1LL, CONFIG_SYS_SDRAM_CONF1LL);
  2579. mtdcr(SDRAM_CONF1HB, CONFIG_SYS_SDRAM_CONF1HB);
  2580. mtdcr(SDRAM_CONFPATHB, CONFIG_SYS_SDRAM_CONFPATHB);
  2581. #endif
  2582. /* Set Memory Bank Configuration Registers */
  2583. mtsdram(SDRAM_MB0CF, CONFIG_SYS_SDRAM0_MB0CF);
  2584. mtsdram(SDRAM_MB1CF, CONFIG_SYS_SDRAM0_MB1CF);
  2585. mtsdram(SDRAM_MB2CF, CONFIG_SYS_SDRAM0_MB2CF);
  2586. mtsdram(SDRAM_MB3CF, CONFIG_SYS_SDRAM0_MB3CF);
  2587. /* Set Memory Clock Timing Register */
  2588. mtsdram(SDRAM_CLKTR, CONFIG_SYS_SDRAM0_CLKTR);
  2589. /* Set Refresh Time Register */
  2590. mtsdram(SDRAM_RTR, CONFIG_SYS_SDRAM0_RTR);
  2591. /* Set SDRAM Timing Registers */
  2592. mtsdram(SDRAM_SDTR1, CONFIG_SYS_SDRAM0_SDTR1);
  2593. mtsdram(SDRAM_SDTR2, CONFIG_SYS_SDRAM0_SDTR2);
  2594. mtsdram(SDRAM_SDTR3, CONFIG_SYS_SDRAM0_SDTR3);
  2595. /* Set Mode and Extended Mode Registers */
  2596. mtsdram(SDRAM_MMODE, CONFIG_SYS_SDRAM0_MMODE);
  2597. mtsdram(SDRAM_MEMODE, CONFIG_SYS_SDRAM0_MEMODE);
  2598. /* Set Memory Controller Options 1 Register */
  2599. mtsdram(SDRAM_MCOPT1, CONFIG_SYS_SDRAM0_MCOPT1);
  2600. /* Set Manual Initialization Control Registers */
  2601. mtsdram(SDRAM_INITPLR0, CONFIG_SYS_SDRAM0_INITPLR0);
  2602. mtsdram(SDRAM_INITPLR1, CONFIG_SYS_SDRAM0_INITPLR1);
  2603. mtsdram(SDRAM_INITPLR2, CONFIG_SYS_SDRAM0_INITPLR2);
  2604. mtsdram(SDRAM_INITPLR3, CONFIG_SYS_SDRAM0_INITPLR3);
  2605. mtsdram(SDRAM_INITPLR4, CONFIG_SYS_SDRAM0_INITPLR4);
  2606. mtsdram(SDRAM_INITPLR5, CONFIG_SYS_SDRAM0_INITPLR5);
  2607. mtsdram(SDRAM_INITPLR6, CONFIG_SYS_SDRAM0_INITPLR6);
  2608. mtsdram(SDRAM_INITPLR7, CONFIG_SYS_SDRAM0_INITPLR7);
  2609. mtsdram(SDRAM_INITPLR8, CONFIG_SYS_SDRAM0_INITPLR8);
  2610. mtsdram(SDRAM_INITPLR9, CONFIG_SYS_SDRAM0_INITPLR9);
  2611. mtsdram(SDRAM_INITPLR10, CONFIG_SYS_SDRAM0_INITPLR10);
  2612. mtsdram(SDRAM_INITPLR11, CONFIG_SYS_SDRAM0_INITPLR11);
  2613. mtsdram(SDRAM_INITPLR12, CONFIG_SYS_SDRAM0_INITPLR12);
  2614. mtsdram(SDRAM_INITPLR13, CONFIG_SYS_SDRAM0_INITPLR13);
  2615. mtsdram(SDRAM_INITPLR14, CONFIG_SYS_SDRAM0_INITPLR14);
  2616. mtsdram(SDRAM_INITPLR15, CONFIG_SYS_SDRAM0_INITPLR15);
  2617. /* Set On-Die Termination Registers */
  2618. mtsdram(SDRAM_CODT, CONFIG_SYS_SDRAM0_CODT);
  2619. mtsdram(SDRAM_MODT0, CONFIG_SYS_SDRAM0_MODT0);
  2620. mtsdram(SDRAM_MODT1, CONFIG_SYS_SDRAM0_MODT1);
  2621. /* Set Write Timing Register */
  2622. mtsdram(SDRAM_WRDTR, CONFIG_SYS_SDRAM0_WRDTR);
  2623. /*
  2624. * Start Initialization by SDRAM0_MCOPT2[SREN] = 0 and
  2625. * SDRAM0_MCOPT2[IPTR] = 1
  2626. */
  2627. mtsdram(SDRAM_MCOPT2, (SDRAM_MCOPT2_SREN_EXIT |
  2628. SDRAM_MCOPT2_IPTR_EXECUTE));
  2629. /*
  2630. * Poll SDRAM0_MCSTAT[MIC] for assertion to indicate the
  2631. * completion of initialization.
  2632. */
  2633. do {
  2634. mfsdram(SDRAM_MCSTAT, val);
  2635. } while ((val & SDRAM_MCSTAT_MIC_MASK) != SDRAM_MCSTAT_MIC_COMP);
  2636. /* Set Delay Control Registers */
  2637. mtsdram(SDRAM_DLCR, CONFIG_SYS_SDRAM0_DLCR);
  2638. #if !defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION)
  2639. mtsdram(SDRAM_RDCC, CONFIG_SYS_SDRAM0_RDCC);
  2640. mtsdram(SDRAM_RQDC, CONFIG_SYS_SDRAM0_RQDC);
  2641. mtsdram(SDRAM_RFDC, CONFIG_SYS_SDRAM0_RFDC);
  2642. #endif /* !CONFIG_PPC4xx_DDR_AUTOCALIBRATION */
  2643. /*
  2644. * Enable Controller by SDRAM0_MCOPT2[DCEN] = 1:
  2645. */
  2646. mfsdram(SDRAM_MCOPT2, val);
  2647. mtsdram(SDRAM_MCOPT2, val | SDRAM_MCOPT2_DCEN_ENABLE);
  2648. #if defined(CONFIG_440)
  2649. /*
  2650. * Program TLB entries with caches enabled, for best performace
  2651. * while auto-calibrating and ECC generation
  2652. */
  2653. program_tlb(0, 0, (CONFIG_SYS_MBYTES_SDRAM << 20), 0);
  2654. #endif
  2655. #if defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION)
  2656. /*------------------------------------------------------------------
  2657. | DQS calibration.
  2658. +-----------------------------------------------------------------*/
  2659. DQS_autocalibration();
  2660. #endif /* CONFIG_PPC4xx_DDR_AUTOCALIBRATION */
  2661. /*
  2662. * Now complete RDSS configuration as mentioned on page 7 of the AMCC
  2663. * PowerPC440SP/SPe DDR2 application note:
  2664. * "DDR1/DDR2 Initialization Sequence and Dynamic Tuning"
  2665. */
  2666. update_rdcc();
  2667. #if defined(CONFIG_DDR_ECC)
  2668. do_program_ecc(0);
  2669. #endif /* defined(CONFIG_DDR_ECC) */
  2670. #if defined(CONFIG_440)
  2671. /*
  2672. * Now after initialization (auto-calibration and ECC generation)
  2673. * remove the TLB entries with caches enabled and program again with
  2674. * desired cache functionality
  2675. */
  2676. remove_tlb(0, (CONFIG_SYS_MBYTES_SDRAM << 20));
  2677. program_tlb(0, 0, (CONFIG_SYS_MBYTES_SDRAM << 20), MY_TLB_WORD2_I_ENABLE);
  2678. #endif
  2679. ppc4xx_ibm_ddr2_register_dump();
  2680. #if defined(CONFIG_PPC4xx_DDR_AUTOCALIBRATION)
  2681. /*
  2682. * Clear potential errors resulting from auto-calibration.
  2683. * If not done, then we could get an interrupt later on when
  2684. * exceptions are enabled.
  2685. */
  2686. set_mcsr(get_mcsr());
  2687. #endif /* CONFIG_PPC4xx_DDR_AUTOCALIBRATION */
  2688. return (CONFIG_SYS_MBYTES_SDRAM << 20);
  2689. }
  2690. #endif /* CONFIG_SPD_EEPROM */
  2691. #if defined(CONFIG_440)
  2692. u32 mfdcr_any(u32 dcr)
  2693. {
  2694. u32 val;
  2695. switch (dcr) {
  2696. case SDRAM_R0BAS + 0:
  2697. val = mfdcr(SDRAM_R0BAS + 0);
  2698. break;
  2699. case SDRAM_R0BAS + 1:
  2700. val = mfdcr(SDRAM_R0BAS + 1);
  2701. break;
  2702. case SDRAM_R0BAS + 2:
  2703. val = mfdcr(SDRAM_R0BAS + 2);
  2704. break;
  2705. case SDRAM_R0BAS + 3:
  2706. val = mfdcr(SDRAM_R0BAS + 3);
  2707. break;
  2708. default:
  2709. printf("DCR %d not defined in case statement!!!\n", dcr);
  2710. val = 0; /* just to satisfy the compiler */
  2711. }
  2712. return val;
  2713. }
  2714. void mtdcr_any(u32 dcr, u32 val)
  2715. {
  2716. switch (dcr) {
  2717. case SDRAM_R0BAS + 0:
  2718. mtdcr(SDRAM_R0BAS + 0, val);
  2719. break;
  2720. case SDRAM_R0BAS + 1:
  2721. mtdcr(SDRAM_R0BAS + 1, val);
  2722. break;
  2723. case SDRAM_R0BAS + 2:
  2724. mtdcr(SDRAM_R0BAS + 2, val);
  2725. break;
  2726. case SDRAM_R0BAS + 3:
  2727. mtdcr(SDRAM_R0BAS + 3, val);
  2728. break;
  2729. default:
  2730. printf("DCR %d not defined in case statement!!!\n", dcr);
  2731. }
  2732. }
  2733. #endif /* defined(CONFIG_440) */
  2734. inline void ppc4xx_ibm_ddr2_register_dump(void)
  2735. {
  2736. #if defined(DEBUG)
  2737. printf("\nPPC4xx IBM DDR2 Register Dump:\n");
  2738. #if (defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
  2739. defined(CONFIG_460EX) || defined(CONFIG_460GT))
  2740. PPC4xx_IBM_DDR2_DUMP_MQ_REGISTER(R0BAS);
  2741. PPC4xx_IBM_DDR2_DUMP_MQ_REGISTER(R1BAS);
  2742. PPC4xx_IBM_DDR2_DUMP_MQ_REGISTER(R2BAS);
  2743. PPC4xx_IBM_DDR2_DUMP_MQ_REGISTER(R3BAS);
  2744. #endif /* (defined(CONFIG_440SP) || ... */
  2745. #if defined(CONFIG_405EX)
  2746. PPC4xx_IBM_DDR2_DUMP_REGISTER(BESR);
  2747. PPC4xx_IBM_DDR2_DUMP_REGISTER(BEARL);
  2748. PPC4xx_IBM_DDR2_DUMP_REGISTER(BEARH);
  2749. PPC4xx_IBM_DDR2_DUMP_REGISTER(WMIRQ);
  2750. PPC4xx_IBM_DDR2_DUMP_REGISTER(PLBOPT);
  2751. PPC4xx_IBM_DDR2_DUMP_REGISTER(PUABA);
  2752. #endif /* defined(CONFIG_405EX) */
  2753. PPC4xx_IBM_DDR2_DUMP_REGISTER(MB0CF);
  2754. PPC4xx_IBM_DDR2_DUMP_REGISTER(MB1CF);
  2755. PPC4xx_IBM_DDR2_DUMP_REGISTER(MB2CF);
  2756. PPC4xx_IBM_DDR2_DUMP_REGISTER(MB3CF);
  2757. PPC4xx_IBM_DDR2_DUMP_REGISTER(MCSTAT);
  2758. PPC4xx_IBM_DDR2_DUMP_REGISTER(MCOPT1);
  2759. PPC4xx_IBM_DDR2_DUMP_REGISTER(MCOPT2);
  2760. PPC4xx_IBM_DDR2_DUMP_REGISTER(MODT0);
  2761. PPC4xx_IBM_DDR2_DUMP_REGISTER(MODT1);
  2762. PPC4xx_IBM_DDR2_DUMP_REGISTER(MODT2);
  2763. PPC4xx_IBM_DDR2_DUMP_REGISTER(MODT3);
  2764. PPC4xx_IBM_DDR2_DUMP_REGISTER(CODT);
  2765. #if (defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
  2766. defined(CONFIG_460EX) || defined(CONFIG_460GT))
  2767. PPC4xx_IBM_DDR2_DUMP_REGISTER(VVPR);
  2768. PPC4xx_IBM_DDR2_DUMP_REGISTER(OPARS);
  2769. /*
  2770. * OPART is only used as a trigger register.
  2771. *
  2772. * No data is contained in this register, and reading or writing
  2773. * to is can cause bad things to happen (hangs). Just skip it and
  2774. * report "N/A".
  2775. */
  2776. printf("%20s = N/A\n", "SDRAM_OPART");
  2777. #endif /* defined(CONFIG_440SP) || ... */
  2778. PPC4xx_IBM_DDR2_DUMP_REGISTER(RTR);
  2779. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR0);
  2780. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR1);
  2781. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR2);
  2782. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR3);
  2783. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR4);
  2784. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR5);
  2785. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR6);
  2786. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR7);
  2787. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR8);
  2788. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR9);
  2789. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR10);
  2790. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR11);
  2791. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR12);
  2792. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR13);
  2793. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR14);
  2794. PPC4xx_IBM_DDR2_DUMP_REGISTER(INITPLR15);
  2795. PPC4xx_IBM_DDR2_DUMP_REGISTER(RQDC);
  2796. PPC4xx_IBM_DDR2_DUMP_REGISTER(RFDC);
  2797. PPC4xx_IBM_DDR2_DUMP_REGISTER(RDCC);
  2798. PPC4xx_IBM_DDR2_DUMP_REGISTER(DLCR);
  2799. PPC4xx_IBM_DDR2_DUMP_REGISTER(CLKTR);
  2800. PPC4xx_IBM_DDR2_DUMP_REGISTER(WRDTR);
  2801. PPC4xx_IBM_DDR2_DUMP_REGISTER(SDTR1);
  2802. PPC4xx_IBM_DDR2_DUMP_REGISTER(SDTR2);
  2803. PPC4xx_IBM_DDR2_DUMP_REGISTER(SDTR3);
  2804. PPC4xx_IBM_DDR2_DUMP_REGISTER(MMODE);
  2805. PPC4xx_IBM_DDR2_DUMP_REGISTER(MEMODE);
  2806. PPC4xx_IBM_DDR2_DUMP_REGISTER(ECCES);
  2807. #if (defined(CONFIG_440SP) || defined(CONFIG_440SPE) || \
  2808. defined(CONFIG_460EX) || defined(CONFIG_460GT))
  2809. PPC4xx_IBM_DDR2_DUMP_REGISTER(CID);
  2810. #endif /* defined(CONFIG_440SP) || ... */
  2811. PPC4xx_IBM_DDR2_DUMP_REGISTER(RID);
  2812. PPC4xx_IBM_DDR2_DUMP_REGISTER(FCSR);
  2813. PPC4xx_IBM_DDR2_DUMP_REGISTER(RTSR);
  2814. #endif /* defined(DEBUG) */
  2815. }