44x_spd_ddr2.c 99 KB

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