ddr3_training.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662
  1. /*
  2. * Copyright (C) Marvell International Ltd. and its affiliates
  3. *
  4. * SPDX-License-Identifier: GPL-2.0
  5. */
  6. #include <common.h>
  7. #include <spl.h>
  8. #include <asm/io.h>
  9. #include <asm/arch/cpu.h>
  10. #include <asm/arch/soc.h>
  11. #include "ddr3_init.h"
  12. #define GET_MAX_VALUE(x, y) \
  13. ((x) > (y)) ? (x) : (y)
  14. #define CEIL_DIVIDE(x, y) \
  15. ((x - (x / y) * y) == 0) ? ((x / y) - 1) : (x / y)
  16. #define TIME_2_CLOCK_CYCLES CEIL_DIVIDE
  17. #define GET_CS_FROM_MASK(mask) (cs_mask2_num[mask])
  18. #define CS_CBE_VALUE(cs_num) (cs_cbe_reg[cs_num])
  19. #define TIMES_9_TREFI_CYCLES 0x8
  20. u32 window_mem_addr = 0;
  21. u32 phy_reg0_val = 0;
  22. u32 phy_reg1_val = 8;
  23. u32 phy_reg2_val = 0;
  24. u32 phy_reg3_val = 0xa;
  25. enum hws_ddr_freq init_freq = DDR_FREQ_667;
  26. enum hws_ddr_freq low_freq = DDR_FREQ_LOW_FREQ;
  27. enum hws_ddr_freq medium_freq;
  28. u32 debug_dunit = 0;
  29. u32 odt_additional = 1;
  30. u32 *dq_map_table = NULL;
  31. u32 odt_config = 1;
  32. #if defined(CONFIG_ARMADA_38X) || defined(CONFIG_ALLEYCAT3) || \
  33. defined(CONFIG_ARMADA_39X)
  34. u32 is_pll_before_init = 0, is_adll_calib_before_init = 0, is_dfs_in_init = 0;
  35. u32 dfs_low_freq = 130;
  36. #else
  37. u32 is_pll_before_init = 0, is_adll_calib_before_init = 1, is_dfs_in_init = 0;
  38. u32 dfs_low_freq = 100;
  39. #endif
  40. u32 g_rtt_nom_c_s0, g_rtt_nom_c_s1;
  41. u8 calibration_update_control; /* 2 external only, 1 is internal only */
  42. enum hws_result training_result[MAX_STAGE_LIMIT][MAX_INTERFACE_NUM];
  43. enum auto_tune_stage training_stage = INIT_CONTROLLER;
  44. u32 finger_test = 0, p_finger_start = 11, p_finger_end = 64,
  45. n_finger_start = 11, n_finger_end = 64,
  46. p_finger_step = 3, n_finger_step = 3;
  47. u32 clamp_tbl[] = { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 };
  48. /* Initiate to 0xff, this variable is define by user in debug mode */
  49. u32 mode2_t = 0xff;
  50. u32 xsb_validate_type = 0;
  51. u32 xsb_validation_base_address = 0xf000;
  52. u32 first_active_if = 0;
  53. u32 dfs_low_phy1 = 0x1f;
  54. u32 multicast_id = 0;
  55. int use_broadcast = 0;
  56. struct hws_tip_freq_config_info *freq_info_table = NULL;
  57. u8 is_cbe_required = 0;
  58. u32 debug_mode = 0;
  59. u32 delay_enable = 0;
  60. int rl_mid_freq_wa = 0;
  61. u32 effective_cs = 0;
  62. u32 mask_tune_func = (SET_MEDIUM_FREQ_MASK_BIT |
  63. WRITE_LEVELING_MASK_BIT |
  64. LOAD_PATTERN_2_MASK_BIT |
  65. READ_LEVELING_MASK_BIT |
  66. SET_TARGET_FREQ_MASK_BIT | WRITE_LEVELING_TF_MASK_BIT |
  67. READ_LEVELING_TF_MASK_BIT |
  68. CENTRALIZATION_RX_MASK_BIT | CENTRALIZATION_TX_MASK_BIT);
  69. void ddr3_print_version(void)
  70. {
  71. printf(DDR3_TIP_VERSION_STRING);
  72. }
  73. static int ddr3_tip_ddr3_training_main_flow(u32 dev_num);
  74. static int ddr3_tip_write_odt(u32 dev_num, enum hws_access_type access_type,
  75. u32 if_id, u32 cl_value, u32 cwl_value);
  76. static int ddr3_tip_ddr3_auto_tune(u32 dev_num);
  77. static int is_bus_access_done(u32 dev_num, u32 if_id,
  78. u32 dunit_reg_adrr, u32 bit);
  79. #ifdef ODT_TEST_SUPPORT
  80. static int odt_test(u32 dev_num, enum hws_algo_type algo_type);
  81. #endif
  82. int adll_calibration(u32 dev_num, enum hws_access_type access_type,
  83. u32 if_id, enum hws_ddr_freq frequency);
  84. static int ddr3_tip_set_timing(u32 dev_num, enum hws_access_type access_type,
  85. u32 if_id, enum hws_ddr_freq frequency);
  86. static struct page_element page_param[] = {
  87. /*
  88. * 8bits 16 bits
  89. * page-size(K) page-size(K) mask
  90. */
  91. { 1, 2, 2},
  92. /* 512M */
  93. { 1, 2, 3},
  94. /* 1G */
  95. { 1, 2, 0},
  96. /* 2G */
  97. { 1, 2, 4},
  98. /* 4G */
  99. { 2, 2, 5}
  100. /* 8G */
  101. };
  102. static u8 mem_size_config[MEM_SIZE_LAST] = {
  103. 0x2, /* 512Mbit */
  104. 0x3, /* 1Gbit */
  105. 0x0, /* 2Gbit */
  106. 0x4, /* 4Gbit */
  107. 0x5 /* 8Gbit */
  108. };
  109. static u8 cs_mask2_num[] = { 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 };
  110. static struct reg_data odpg_default_value[] = {
  111. {0x1034, 0x38000, MASK_ALL_BITS},
  112. {0x1038, 0x0, MASK_ALL_BITS},
  113. {0x10b0, 0x0, MASK_ALL_BITS},
  114. {0x10b8, 0x0, MASK_ALL_BITS},
  115. {0x10c0, 0x0, MASK_ALL_BITS},
  116. {0x10f0, 0x0, MASK_ALL_BITS},
  117. {0x10f4, 0x0, MASK_ALL_BITS},
  118. {0x10f8, 0xff, MASK_ALL_BITS},
  119. {0x10fc, 0xffff, MASK_ALL_BITS},
  120. {0x1130, 0x0, MASK_ALL_BITS},
  121. {0x1830, 0x2000000, MASK_ALL_BITS},
  122. {0x14d0, 0x0, MASK_ALL_BITS},
  123. {0x14d4, 0x0, MASK_ALL_BITS},
  124. {0x14d8, 0x0, MASK_ALL_BITS},
  125. {0x14dc, 0x0, MASK_ALL_BITS},
  126. {0x1454, 0x0, MASK_ALL_BITS},
  127. {0x1594, 0x0, MASK_ALL_BITS},
  128. {0x1598, 0x0, MASK_ALL_BITS},
  129. {0x159c, 0x0, MASK_ALL_BITS},
  130. {0x15a0, 0x0, MASK_ALL_BITS},
  131. {0x15a4, 0x0, MASK_ALL_BITS},
  132. {0x15a8, 0x0, MASK_ALL_BITS},
  133. {0x15ac, 0x0, MASK_ALL_BITS},
  134. {0x1604, 0x0, MASK_ALL_BITS},
  135. {0x1608, 0x0, MASK_ALL_BITS},
  136. {0x160c, 0x0, MASK_ALL_BITS},
  137. {0x1610, 0x0, MASK_ALL_BITS},
  138. {0x1614, 0x0, MASK_ALL_BITS},
  139. {0x1618, 0x0, MASK_ALL_BITS},
  140. {0x1624, 0x0, MASK_ALL_BITS},
  141. {0x1690, 0x0, MASK_ALL_BITS},
  142. {0x1694, 0x0, MASK_ALL_BITS},
  143. {0x1698, 0x0, MASK_ALL_BITS},
  144. {0x169c, 0x0, MASK_ALL_BITS},
  145. {0x14b8, 0x6f67, MASK_ALL_BITS},
  146. {0x1630, 0x0, MASK_ALL_BITS},
  147. {0x1634, 0x0, MASK_ALL_BITS},
  148. {0x1638, 0x0, MASK_ALL_BITS},
  149. {0x163c, 0x0, MASK_ALL_BITS},
  150. {0x16b0, 0x0, MASK_ALL_BITS},
  151. {0x16b4, 0x0, MASK_ALL_BITS},
  152. {0x16b8, 0x0, MASK_ALL_BITS},
  153. {0x16bc, 0x0, MASK_ALL_BITS},
  154. {0x16c0, 0x0, MASK_ALL_BITS},
  155. {0x16c4, 0x0, MASK_ALL_BITS},
  156. {0x16c8, 0x0, MASK_ALL_BITS},
  157. {0x16cc, 0x1, MASK_ALL_BITS},
  158. {0x16f0, 0x1, MASK_ALL_BITS},
  159. {0x16f4, 0x0, MASK_ALL_BITS},
  160. {0x16f8, 0x0, MASK_ALL_BITS},
  161. {0x16fc, 0x0, MASK_ALL_BITS}
  162. };
  163. static int ddr3_tip_bus_access(u32 dev_num, enum hws_access_type interface_access,
  164. u32 if_id, enum hws_access_type phy_access,
  165. u32 phy_id, enum hws_ddr_phy phy_type, u32 reg_addr,
  166. u32 data_value, enum hws_operation oper_type);
  167. static int ddr3_tip_pad_inv(u32 dev_num, u32 if_id);
  168. static int ddr3_tip_rank_control(u32 dev_num, u32 if_id);
  169. /*
  170. * Update global training parameters by data from user
  171. */
  172. int ddr3_tip_tune_training_params(u32 dev_num,
  173. struct tune_train_params *params)
  174. {
  175. if (params->ck_delay != -1)
  176. ck_delay = params->ck_delay;
  177. if (params->ck_delay_16 != -1)
  178. ck_delay_16 = params->ck_delay_16;
  179. if (params->phy_reg3_val != -1)
  180. phy_reg3_val = params->phy_reg3_val;
  181. return MV_OK;
  182. }
  183. /*
  184. * Configure CS
  185. */
  186. int ddr3_tip_configure_cs(u32 dev_num, u32 if_id, u32 cs_num, u32 enable)
  187. {
  188. u32 data, addr_hi, data_high;
  189. u32 mem_index;
  190. struct hws_topology_map *tm = ddr3_get_topology_map();
  191. if (enable == 1) {
  192. data = (tm->interface_params[if_id].bus_width ==
  193. BUS_WIDTH_8) ? 0 : 1;
  194. CHECK_STATUS(ddr3_tip_if_write
  195. (dev_num, ACCESS_TYPE_UNICAST, if_id,
  196. SDRAM_ACCESS_CONTROL_REG, (data << (cs_num * 4)),
  197. 0x3 << (cs_num * 4)));
  198. mem_index = tm->interface_params[if_id].memory_size;
  199. addr_hi = mem_size_config[mem_index] & 0x3;
  200. CHECK_STATUS(ddr3_tip_if_write
  201. (dev_num, ACCESS_TYPE_UNICAST, if_id,
  202. SDRAM_ACCESS_CONTROL_REG,
  203. (addr_hi << (2 + cs_num * 4)),
  204. 0x3 << (2 + cs_num * 4)));
  205. data_high = (mem_size_config[mem_index] & 0x4) >> 2;
  206. CHECK_STATUS(ddr3_tip_if_write
  207. (dev_num, ACCESS_TYPE_UNICAST, if_id,
  208. SDRAM_ACCESS_CONTROL_REG,
  209. data_high << (20 + cs_num), 1 << (20 + cs_num)));
  210. /* Enable Address Select Mode */
  211. CHECK_STATUS(ddr3_tip_if_write
  212. (dev_num, ACCESS_TYPE_UNICAST, if_id,
  213. SDRAM_ACCESS_CONTROL_REG, 1 << (16 + cs_num),
  214. 1 << (16 + cs_num)));
  215. }
  216. switch (cs_num) {
  217. case 0:
  218. case 1:
  219. case 2:
  220. CHECK_STATUS(ddr3_tip_if_write
  221. (dev_num, ACCESS_TYPE_UNICAST, if_id,
  222. DDR_CONTROL_LOW_REG, (enable << (cs_num + 11)),
  223. 1 << (cs_num + 11)));
  224. break;
  225. case 3:
  226. CHECK_STATUS(ddr3_tip_if_write
  227. (dev_num, ACCESS_TYPE_UNICAST, if_id,
  228. DDR_CONTROL_LOW_REG, (enable << 15), 1 << 15));
  229. break;
  230. }
  231. return MV_OK;
  232. }
  233. /*
  234. * Calculate number of CS
  235. */
  236. static int calc_cs_num(u32 dev_num, u32 if_id, u32 *cs_num)
  237. {
  238. u32 cs;
  239. u32 bus_cnt;
  240. u32 cs_count;
  241. u32 cs_bitmask;
  242. u32 curr_cs_num = 0;
  243. struct hws_topology_map *tm = ddr3_get_topology_map();
  244. for (bus_cnt = 0; bus_cnt < GET_TOPOLOGY_NUM_OF_BUSES(); bus_cnt++) {
  245. VALIDATE_ACTIVE(tm->bus_act_mask, bus_cnt);
  246. cs_count = 0;
  247. cs_bitmask = tm->interface_params[if_id].
  248. as_bus_params[bus_cnt].cs_bitmask;
  249. for (cs = 0; cs < MAX_CS_NUM; cs++) {
  250. if ((cs_bitmask >> cs) & 1)
  251. cs_count++;
  252. }
  253. if (curr_cs_num == 0) {
  254. curr_cs_num = cs_count;
  255. } else if (cs_count != curr_cs_num) {
  256. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  257. ("CS number is different per bus (IF %d BUS %d cs_num %d curr_cs_num %d)\n",
  258. if_id, bus_cnt, cs_count,
  259. curr_cs_num));
  260. return MV_NOT_SUPPORTED;
  261. }
  262. }
  263. *cs_num = curr_cs_num;
  264. return MV_OK;
  265. }
  266. /*
  267. * Init Controller Flow
  268. */
  269. int hws_ddr3_tip_init_controller(u32 dev_num, struct init_cntr_param *init_cntr_prm)
  270. {
  271. u32 if_id;
  272. u32 cs_num;
  273. u32 t_refi = 0, t_hclk = 0, t_ckclk = 0, t_faw = 0, t_pd = 0,
  274. t_wr = 0, t2t = 0, txpdll = 0;
  275. u32 data_value = 0, bus_width = 0, page_size = 0, cs_cnt = 0,
  276. mem_mask = 0, bus_index = 0;
  277. enum hws_speed_bin speed_bin_index = SPEED_BIN_DDR_2133N;
  278. enum hws_mem_size memory_size = MEM_2G;
  279. enum hws_ddr_freq freq = init_freq;
  280. enum hws_timing timing;
  281. u32 cs_mask = 0;
  282. u32 cl_value = 0, cwl_val = 0;
  283. u32 refresh_interval_cnt = 0, bus_cnt = 0, adll_tap = 0;
  284. enum hws_access_type access_type = ACCESS_TYPE_UNICAST;
  285. u32 data_read[MAX_INTERFACE_NUM];
  286. struct hws_topology_map *tm = ddr3_get_topology_map();
  287. u32 odt_config = g_odt_config_2cs;
  288. DEBUG_TRAINING_IP(DEBUG_LEVEL_TRACE,
  289. ("Init_controller, do_mrs_phy=%d, is_ctrl64_bit=%d\n",
  290. init_cntr_prm->do_mrs_phy,
  291. init_cntr_prm->is_ctrl64_bit));
  292. if (init_cntr_prm->init_phy == 1) {
  293. CHECK_STATUS(ddr3_tip_configure_phy(dev_num));
  294. }
  295. if (generic_init_controller == 1) {
  296. for (if_id = 0; if_id <= MAX_INTERFACE_NUM - 1; if_id++) {
  297. VALIDATE_ACTIVE(tm->if_act_mask, if_id);
  298. DEBUG_TRAINING_IP(DEBUG_LEVEL_TRACE,
  299. ("active IF %d\n", if_id));
  300. mem_mask = 0;
  301. for (bus_index = 0;
  302. bus_index < GET_TOPOLOGY_NUM_OF_BUSES();
  303. bus_index++) {
  304. VALIDATE_ACTIVE(tm->bus_act_mask, bus_index);
  305. mem_mask |=
  306. tm->interface_params[if_id].
  307. as_bus_params[bus_index].mirror_enable_bitmask;
  308. }
  309. if (mem_mask != 0) {
  310. CHECK_STATUS(ddr3_tip_if_write
  311. (dev_num, ACCESS_TYPE_MULTICAST,
  312. if_id, CS_ENABLE_REG, 0,
  313. 0x8));
  314. }
  315. memory_size =
  316. tm->interface_params[if_id].
  317. memory_size;
  318. speed_bin_index =
  319. tm->interface_params[if_id].
  320. speed_bin_index;
  321. freq = init_freq;
  322. t_refi =
  323. (tm->interface_params[if_id].
  324. interface_temp ==
  325. HWS_TEMP_HIGH) ? TREFI_HIGH : TREFI_LOW;
  326. t_refi *= 1000; /* psec */
  327. DEBUG_TRAINING_IP(DEBUG_LEVEL_TRACE,
  328. ("memy_size %d speed_bin_ind %d freq %d t_refi %d\n",
  329. memory_size, speed_bin_index, freq,
  330. t_refi));
  331. /* HCLK & CK CLK in 2:1[ps] */
  332. /* t_ckclk is external clock */
  333. t_ckclk = (MEGA / freq_val[freq]);
  334. /* t_hclk is internal clock */
  335. t_hclk = 2 * t_ckclk;
  336. refresh_interval_cnt = t_refi / t_hclk; /* no units */
  337. bus_width =
  338. (DDR3_IS_16BIT_DRAM_MODE(tm->bus_act_mask)
  339. == 1) ? (16) : (32);
  340. if (init_cntr_prm->is_ctrl64_bit)
  341. bus_width = 64;
  342. data_value =
  343. (refresh_interval_cnt | 0x4000 |
  344. ((bus_width ==
  345. 32) ? 0x8000 : 0) | 0x1000000) & ~(1 << 26);
  346. /* Interface Bus Width */
  347. /* SRMode */
  348. CHECK_STATUS(ddr3_tip_if_write
  349. (dev_num, access_type, if_id,
  350. SDRAM_CONFIGURATION_REG, data_value,
  351. 0x100ffff));
  352. /* Interleave first command pre-charge enable (TBD) */
  353. CHECK_STATUS(ddr3_tip_if_write
  354. (dev_num, access_type, if_id,
  355. SDRAM_OPEN_PAGE_CONTROL_REG, (1 << 10),
  356. (1 << 10)));
  357. /* PHY configuration */
  358. /*
  359. * Postamble Length = 1.5cc, Addresscntl to clk skew
  360. * \BD, Preamble length normal, parralal ADLL enable
  361. */
  362. CHECK_STATUS(ddr3_tip_if_write
  363. (dev_num, access_type, if_id,
  364. DRAM_PHY_CONFIGURATION, 0x28, 0x3e));
  365. if (init_cntr_prm->is_ctrl64_bit) {
  366. /* positive edge */
  367. CHECK_STATUS(ddr3_tip_if_write
  368. (dev_num, access_type, if_id,
  369. DRAM_PHY_CONFIGURATION, 0x0,
  370. 0xff80));
  371. }
  372. /* calibration block disable */
  373. /* Xbar Read buffer select (for Internal access) */
  374. CHECK_STATUS(ddr3_tip_if_write
  375. (dev_num, access_type, if_id,
  376. CALIB_MACHINE_CTRL_REG, 0x1200c,
  377. 0x7dffe01c));
  378. CHECK_STATUS(ddr3_tip_if_write
  379. (dev_num, access_type, if_id,
  380. CALIB_MACHINE_CTRL_REG,
  381. calibration_update_control << 3, 0x3 << 3));
  382. /* Pad calibration control - enable */
  383. CHECK_STATUS(ddr3_tip_if_write
  384. (dev_num, access_type, if_id,
  385. CALIB_MACHINE_CTRL_REG, 0x1, 0x1));
  386. cs_mask = 0;
  387. data_value = 0x7;
  388. /*
  389. * Address ctrl \96 Part of the Generic code
  390. * The next configuration is done:
  391. * 1) Memory Size
  392. * 2) Bus_width
  393. * 3) CS#
  394. * 4) Page Number
  395. * 5) t_faw
  396. * Per Dunit get from the Map_topology the parameters:
  397. * Bus_width
  398. * t_faw is per Dunit not per CS
  399. */
  400. page_size =
  401. (tm->interface_params[if_id].
  402. bus_width ==
  403. BUS_WIDTH_8) ? page_param[memory_size].
  404. page_size_8bit : page_param[memory_size].
  405. page_size_16bit;
  406. t_faw =
  407. (page_size == 1) ? speed_bin_table(speed_bin_index,
  408. SPEED_BIN_TFAW1K)
  409. : speed_bin_table(speed_bin_index,
  410. SPEED_BIN_TFAW2K);
  411. data_value = TIME_2_CLOCK_CYCLES(t_faw, t_ckclk);
  412. data_value = data_value << 24;
  413. CHECK_STATUS(ddr3_tip_if_write
  414. (dev_num, access_type, if_id,
  415. SDRAM_ACCESS_CONTROL_REG, data_value,
  416. 0x7f000000));
  417. data_value =
  418. (tm->interface_params[if_id].
  419. bus_width == BUS_WIDTH_8) ? 0 : 1;
  420. /* create merge cs mask for all cs available in dunit */
  421. for (bus_cnt = 0;
  422. bus_cnt < GET_TOPOLOGY_NUM_OF_BUSES();
  423. bus_cnt++) {
  424. VALIDATE_ACTIVE(tm->bus_act_mask, bus_cnt);
  425. cs_mask |=
  426. tm->interface_params[if_id].
  427. as_bus_params[bus_cnt].cs_bitmask;
  428. }
  429. DEBUG_TRAINING_IP(DEBUG_LEVEL_TRACE,
  430. ("Init_controller IF %d cs_mask %d\n",
  431. if_id, cs_mask));
  432. /*
  433. * Configure the next upon the Map Topology \96 If the
  434. * Dunit is CS0 Configure CS0 if it is multi CS
  435. * configure them both: The Bust_width it\92s the
  436. * Memory Bus width \96 x8 or x16
  437. */
  438. for (cs_cnt = 0; cs_cnt < NUM_OF_CS; cs_cnt++) {
  439. ddr3_tip_configure_cs(dev_num, if_id, cs_cnt,
  440. ((cs_mask & (1 << cs_cnt)) ? 1
  441. : 0));
  442. }
  443. if (init_cntr_prm->do_mrs_phy) {
  444. /*
  445. * MR0 \96 Part of the Generic code
  446. * The next configuration is done:
  447. * 1) Burst Length
  448. * 2) CAS Latency
  449. * get for each dunit what is it Speed_bin &
  450. * Target Frequency. From those both parameters
  451. * get the appropriate Cas_l from the CL table
  452. */
  453. cl_value =
  454. tm->interface_params[if_id].
  455. cas_l;
  456. cwl_val =
  457. tm->interface_params[if_id].
  458. cas_wl;
  459. DEBUG_TRAINING_IP(DEBUG_LEVEL_TRACE,
  460. ("cl_value 0x%x cwl_val 0x%x\n",
  461. cl_value, cwl_val));
  462. t_wr = TIME_2_CLOCK_CYCLES(speed_bin_table(speed_bin_index,
  463. SPEED_BIN_TWR),
  464. t_ckclk);
  465. data_value =
  466. ((cl_mask_table[cl_value] & 0x1) << 2) |
  467. ((cl_mask_table[cl_value] & 0xe) << 3);
  468. CHECK_STATUS(ddr3_tip_if_write
  469. (dev_num, access_type, if_id,
  470. MR0_REG, data_value,
  471. (0x7 << 4) | (1 << 2)));
  472. CHECK_STATUS(ddr3_tip_if_write
  473. (dev_num, access_type, if_id,
  474. MR0_REG, twr_mask_table[t_wr + 1] << 9,
  475. (0x7 << 9)));
  476. /*
  477. * MR1: Set RTT and DIC Design GL values
  478. * configured by user
  479. */
  480. CHECK_STATUS(ddr3_tip_if_write
  481. (dev_num, ACCESS_TYPE_MULTICAST,
  482. PARAM_NOT_CARE, MR1_REG,
  483. g_dic | g_rtt_nom, 0x266));
  484. /* MR2 - Part of the Generic code */
  485. /*
  486. * The next configuration is done:
  487. * 1) SRT
  488. * 2) CAS Write Latency
  489. */
  490. data_value = (cwl_mask_table[cwl_val] << 3);
  491. data_value |=
  492. ((tm->interface_params[if_id].
  493. interface_temp ==
  494. HWS_TEMP_HIGH) ? (1 << 7) : 0);
  495. CHECK_STATUS(ddr3_tip_if_write
  496. (dev_num, access_type, if_id,
  497. MR2_REG, data_value,
  498. (0x7 << 3) | (0x1 << 7) | (0x3 <<
  499. 9)));
  500. }
  501. ddr3_tip_write_odt(dev_num, access_type, if_id,
  502. cl_value, cwl_val);
  503. ddr3_tip_set_timing(dev_num, access_type, if_id, freq);
  504. CHECK_STATUS(ddr3_tip_if_write
  505. (dev_num, access_type, if_id,
  506. DUNIT_CONTROL_HIGH_REG, 0x177,
  507. 0x1000177));
  508. if (init_cntr_prm->is_ctrl64_bit) {
  509. /* disable 0.25 cc delay */
  510. CHECK_STATUS(ddr3_tip_if_write
  511. (dev_num, access_type, if_id,
  512. DUNIT_CONTROL_HIGH_REG, 0x0,
  513. 0x800));
  514. }
  515. /* reset bit 7 */
  516. CHECK_STATUS(ddr3_tip_if_write
  517. (dev_num, access_type, if_id,
  518. DUNIT_CONTROL_HIGH_REG,
  519. (init_cntr_prm->msys_init << 7), (1 << 7)));
  520. /* calculate number of CS (per interface) */
  521. CHECK_STATUS(calc_cs_num
  522. (dev_num, if_id, &cs_num));
  523. timing = tm->interface_params[if_id].timing;
  524. if (mode2_t != 0xff) {
  525. t2t = mode2_t;
  526. } else if (timing != HWS_TIM_DEFAULT) {
  527. /* Board topology map is forcing timing */
  528. t2t = (timing == HWS_TIM_2T) ? 1 : 0;
  529. } else {
  530. t2t = (cs_num == 1) ? 0 : 1;
  531. }
  532. CHECK_STATUS(ddr3_tip_if_write
  533. (dev_num, access_type, if_id,
  534. DDR_CONTROL_LOW_REG, t2t << 3,
  535. 0x3 << 3));
  536. /* move the block to ddr3_tip_set_timing - start */
  537. t_pd = TIMES_9_TREFI_CYCLES;
  538. txpdll = GET_MAX_VALUE(t_ckclk * 10,
  539. speed_bin_table(speed_bin_index,
  540. SPEED_BIN_TXPDLL));
  541. txpdll = CEIL_DIVIDE((txpdll - 1), t_ckclk);
  542. CHECK_STATUS(ddr3_tip_if_write
  543. (dev_num, access_type, if_id,
  544. DDR_TIMING_REG, txpdll << 4 | t_pd,
  545. 0x1f << 4 | 0xf));
  546. CHECK_STATUS(ddr3_tip_if_write
  547. (dev_num, access_type, if_id,
  548. DDR_TIMING_REG, 0x28 << 9, 0x3f << 9));
  549. CHECK_STATUS(ddr3_tip_if_write
  550. (dev_num, access_type, if_id,
  551. DDR_TIMING_REG, 0xa << 21, 0xff << 21));
  552. /* move the block to ddr3_tip_set_timing - end */
  553. /* AUTO_ZQC_TIMING */
  554. CHECK_STATUS(ddr3_tip_if_write
  555. (dev_num, access_type, if_id,
  556. TIMING_REG, (AUTO_ZQC_TIMING | (2 << 20)),
  557. 0x3fffff));
  558. CHECK_STATUS(ddr3_tip_if_read
  559. (dev_num, access_type, if_id,
  560. DRAM_PHY_CONFIGURATION, data_read, 0x30));
  561. data_value =
  562. (data_read[if_id] == 0) ? (1 << 11) : 0;
  563. CHECK_STATUS(ddr3_tip_if_write
  564. (dev_num, access_type, if_id,
  565. DUNIT_CONTROL_HIGH_REG, data_value,
  566. (1 << 11)));
  567. /* Set Active control for ODT write transactions */
  568. if (cs_num == 1)
  569. odt_config = g_odt_config_1cs;
  570. CHECK_STATUS(ddr3_tip_if_write
  571. (dev_num, ACCESS_TYPE_MULTICAST,
  572. PARAM_NOT_CARE, 0x1494, odt_config,
  573. MASK_ALL_BITS));
  574. }
  575. } else {
  576. #ifdef STATIC_ALGO_SUPPORT
  577. CHECK_STATUS(ddr3_tip_static_init_controller(dev_num));
  578. #if defined(CONFIG_ARMADA_38X) || defined(CONFIG_ARMADA_39X)
  579. CHECK_STATUS(ddr3_tip_static_phy_init_controller(dev_num));
  580. #endif
  581. #endif /* STATIC_ALGO_SUPPORT */
  582. }
  583. for (if_id = 0; if_id <= MAX_INTERFACE_NUM - 1; if_id++) {
  584. VALIDATE_ACTIVE(tm->if_act_mask, if_id);
  585. CHECK_STATUS(ddr3_tip_rank_control(dev_num, if_id));
  586. if (init_cntr_prm->do_mrs_phy) {
  587. CHECK_STATUS(ddr3_tip_pad_inv(dev_num, if_id));
  588. }
  589. /* Pad calibration control - disable */
  590. CHECK_STATUS(ddr3_tip_if_write
  591. (dev_num, access_type, if_id,
  592. CALIB_MACHINE_CTRL_REG, 0x0, 0x1));
  593. CHECK_STATUS(ddr3_tip_if_write
  594. (dev_num, access_type, if_id,
  595. CALIB_MACHINE_CTRL_REG,
  596. calibration_update_control << 3, 0x3 << 3));
  597. }
  598. CHECK_STATUS(ddr3_tip_enable_init_sequence(dev_num));
  599. if (delay_enable != 0) {
  600. adll_tap = MEGA / (freq_val[freq] * 64);
  601. ddr3_tip_cmd_addr_init_delay(dev_num, adll_tap);
  602. }
  603. return MV_OK;
  604. }
  605. /*
  606. * Load Topology map
  607. */
  608. int hws_ddr3_tip_load_topology_map(u32 dev_num, struct hws_topology_map *tm)
  609. {
  610. enum hws_speed_bin speed_bin_index;
  611. enum hws_ddr_freq freq = DDR_FREQ_LIMIT;
  612. u32 if_id;
  613. freq_val[DDR_FREQ_LOW_FREQ] = dfs_low_freq;
  614. tm = ddr3_get_topology_map();
  615. CHECK_STATUS(ddr3_tip_get_first_active_if
  616. ((u8)dev_num, tm->if_act_mask,
  617. &first_active_if));
  618. DEBUG_TRAINING_IP(DEBUG_LEVEL_TRACE,
  619. ("board IF_Mask=0x%x num_of_bus_per_interface=0x%x\n",
  620. tm->if_act_mask,
  621. tm->num_of_bus_per_interface));
  622. /*
  623. * if CL, CWL values are missing in topology map, then fill them
  624. * according to speedbin tables
  625. */
  626. for (if_id = 0; if_id <= MAX_INTERFACE_NUM - 1; if_id++) {
  627. VALIDATE_ACTIVE(tm->if_act_mask, if_id);
  628. speed_bin_index =
  629. tm->interface_params[if_id].speed_bin_index;
  630. /* TBD memory frequency of interface 0 only is used ! */
  631. freq = tm->interface_params[first_active_if].memory_freq;
  632. DEBUG_TRAINING_IP(DEBUG_LEVEL_TRACE,
  633. ("speed_bin_index =%d freq=%d cl=%d cwl=%d\n",
  634. speed_bin_index, freq_val[freq],
  635. tm->interface_params[if_id].
  636. cas_l,
  637. tm->interface_params[if_id].
  638. cas_wl));
  639. if (tm->interface_params[if_id].cas_l == 0) {
  640. tm->interface_params[if_id].cas_l =
  641. cas_latency_table[speed_bin_index].cl_val[freq];
  642. }
  643. if (tm->interface_params[if_id].cas_wl == 0) {
  644. tm->interface_params[if_id].cas_wl =
  645. cas_write_latency_table[speed_bin_index].cl_val[freq];
  646. }
  647. }
  648. return MV_OK;
  649. }
  650. /*
  651. * RANK Control Flow
  652. */
  653. static int ddr3_tip_rank_control(u32 dev_num, u32 if_id)
  654. {
  655. u32 data_value = 0, bus_cnt;
  656. struct hws_topology_map *tm = ddr3_get_topology_map();
  657. for (bus_cnt = 1; bus_cnt < GET_TOPOLOGY_NUM_OF_BUSES(); bus_cnt++) {
  658. VALIDATE_ACTIVE(tm->bus_act_mask, bus_cnt);
  659. if ((tm->interface_params[if_id].
  660. as_bus_params[0].cs_bitmask !=
  661. tm->interface_params[if_id].
  662. as_bus_params[bus_cnt].cs_bitmask) ||
  663. (tm->interface_params[if_id].
  664. as_bus_params[0].mirror_enable_bitmask !=
  665. tm->interface_params[if_id].
  666. as_bus_params[bus_cnt].mirror_enable_bitmask))
  667. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  668. ("WARNING:Wrong configuration for pup #%d CS mask and CS mirroring for all pups should be the same\n",
  669. bus_cnt));
  670. }
  671. data_value |= tm->interface_params[if_id].
  672. as_bus_params[0].cs_bitmask;
  673. data_value |= tm->interface_params[if_id].
  674. as_bus_params[0].mirror_enable_bitmask << 4;
  675. CHECK_STATUS(ddr3_tip_if_write
  676. (dev_num, ACCESS_TYPE_UNICAST, if_id, RANK_CTRL_REG,
  677. data_value, 0xff));
  678. return MV_OK;
  679. }
  680. /*
  681. * PAD Inverse Flow
  682. */
  683. static int ddr3_tip_pad_inv(u32 dev_num, u32 if_id)
  684. {
  685. u32 bus_cnt, data_value, ck_swap_pup_ctrl;
  686. struct hws_topology_map *tm = ddr3_get_topology_map();
  687. for (bus_cnt = 0; bus_cnt < GET_TOPOLOGY_NUM_OF_BUSES(); bus_cnt++) {
  688. VALIDATE_ACTIVE(tm->bus_act_mask, bus_cnt);
  689. if (tm->interface_params[if_id].
  690. as_bus_params[bus_cnt].is_dqs_swap == 1) {
  691. /* dqs swap */
  692. ddr3_tip_bus_read_modify_write(dev_num, ACCESS_TYPE_UNICAST,
  693. if_id, bus_cnt,
  694. DDR_PHY_DATA,
  695. PHY_CONTROL_PHY_REG, 0xc0,
  696. 0xc0);
  697. }
  698. if (tm->interface_params[if_id].
  699. as_bus_params[bus_cnt].is_ck_swap == 1) {
  700. if (bus_cnt <= 1)
  701. data_value = 0x5 << 2;
  702. else
  703. data_value = 0xa << 2;
  704. /* mask equals data */
  705. /* ck swap pup is only control pup #0 ! */
  706. ck_swap_pup_ctrl = 0;
  707. ddr3_tip_bus_read_modify_write(dev_num, ACCESS_TYPE_UNICAST,
  708. if_id, ck_swap_pup_ctrl,
  709. DDR_PHY_CONTROL,
  710. PHY_CONTROL_PHY_REG,
  711. data_value, data_value);
  712. }
  713. }
  714. return MV_OK;
  715. }
  716. /*
  717. * Run Training Flow
  718. */
  719. int hws_ddr3_tip_run_alg(u32 dev_num, enum hws_algo_type algo_type)
  720. {
  721. int ret = MV_OK, ret_tune = MV_OK;
  722. #ifdef ODT_TEST_SUPPORT
  723. if (finger_test == 1)
  724. return odt_test(dev_num, algo_type);
  725. #endif
  726. if (algo_type == ALGO_TYPE_DYNAMIC) {
  727. ret = ddr3_tip_ddr3_auto_tune(dev_num);
  728. } else {
  729. #ifdef STATIC_ALGO_SUPPORT
  730. {
  731. enum hws_ddr_freq freq;
  732. freq = init_freq;
  733. /* add to mask */
  734. if (is_adll_calib_before_init != 0) {
  735. printf("with adll calib before init\n");
  736. adll_calibration(dev_num, ACCESS_TYPE_MULTICAST,
  737. 0, freq);
  738. }
  739. /*
  740. * Frequency per interface is not relevant,
  741. * only interface 0
  742. */
  743. ret = ddr3_tip_run_static_alg(dev_num,
  744. freq);
  745. }
  746. #endif
  747. }
  748. if (ret != MV_OK) {
  749. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  750. ("Run_alg: tuning failed %d\n", ret_tune));
  751. }
  752. return ret;
  753. }
  754. #ifdef ODT_TEST_SUPPORT
  755. /*
  756. * ODT Test
  757. */
  758. static int odt_test(u32 dev_num, enum hws_algo_type algo_type)
  759. {
  760. int ret = MV_OK, ret_tune = MV_OK;
  761. int pfinger_val = 0, nfinger_val;
  762. for (pfinger_val = p_finger_start; pfinger_val <= p_finger_end;
  763. pfinger_val += p_finger_step) {
  764. for (nfinger_val = n_finger_start; nfinger_val <= n_finger_end;
  765. nfinger_val += n_finger_step) {
  766. if (finger_test != 0) {
  767. DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
  768. ("pfinger_val %d nfinger_val %d\n",
  769. pfinger_val, nfinger_val));
  770. p_finger = pfinger_val;
  771. n_finger = nfinger_val;
  772. }
  773. if (algo_type == ALGO_TYPE_DYNAMIC) {
  774. ret = ddr3_tip_ddr3_auto_tune(dev_num);
  775. } else {
  776. /*
  777. * Frequency per interface is not relevant,
  778. * only interface 0
  779. */
  780. ret = ddr3_tip_run_static_alg(dev_num,
  781. init_freq);
  782. }
  783. }
  784. }
  785. if (ret_tune != MV_OK) {
  786. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  787. ("Run_alg: tuning failed %d\n", ret_tune));
  788. ret = (ret == MV_OK) ? ret_tune : ret;
  789. }
  790. return ret;
  791. }
  792. #endif
  793. /*
  794. * Select Controller
  795. */
  796. int hws_ddr3_tip_select_ddr_controller(u32 dev_num, int enable)
  797. {
  798. if (config_func_info[dev_num].tip_dunit_mux_select_func != NULL) {
  799. return config_func_info[dev_num].
  800. tip_dunit_mux_select_func((u8)dev_num, enable);
  801. }
  802. return MV_FAIL;
  803. }
  804. /*
  805. * Dunit Register Write
  806. */
  807. int ddr3_tip_if_write(u32 dev_num, enum hws_access_type interface_access,
  808. u32 if_id, u32 reg_addr, u32 data_value, u32 mask)
  809. {
  810. if (config_func_info[dev_num].tip_dunit_write_func != NULL) {
  811. return config_func_info[dev_num].
  812. tip_dunit_write_func((u8)dev_num, interface_access,
  813. if_id, reg_addr,
  814. data_value, mask);
  815. }
  816. return MV_FAIL;
  817. }
  818. /*
  819. * Dunit Register Read
  820. */
  821. int ddr3_tip_if_read(u32 dev_num, enum hws_access_type interface_access,
  822. u32 if_id, u32 reg_addr, u32 *data, u32 mask)
  823. {
  824. if (config_func_info[dev_num].tip_dunit_read_func != NULL) {
  825. return config_func_info[dev_num].
  826. tip_dunit_read_func((u8)dev_num, interface_access,
  827. if_id, reg_addr,
  828. data, mask);
  829. }
  830. return MV_FAIL;
  831. }
  832. /*
  833. * Dunit Register Polling
  834. */
  835. int ddr3_tip_if_polling(u32 dev_num, enum hws_access_type access_type,
  836. u32 if_id, u32 exp_value, u32 mask, u32 offset,
  837. u32 poll_tries)
  838. {
  839. u32 poll_cnt = 0, interface_num = 0, start_if, end_if;
  840. u32 read_data[MAX_INTERFACE_NUM];
  841. int ret;
  842. int is_fail = 0, is_if_fail;
  843. struct hws_topology_map *tm = ddr3_get_topology_map();
  844. if (access_type == ACCESS_TYPE_MULTICAST) {
  845. start_if = 0;
  846. end_if = MAX_INTERFACE_NUM - 1;
  847. } else {
  848. start_if = if_id;
  849. end_if = if_id;
  850. }
  851. for (interface_num = start_if; interface_num <= end_if; interface_num++) {
  852. /* polling bit 3 for n times */
  853. VALIDATE_ACTIVE(tm->if_act_mask, interface_num);
  854. is_if_fail = 0;
  855. for (poll_cnt = 0; poll_cnt < poll_tries; poll_cnt++) {
  856. ret =
  857. ddr3_tip_if_read(dev_num, ACCESS_TYPE_UNICAST,
  858. interface_num, offset, read_data,
  859. mask);
  860. if (ret != MV_OK)
  861. return ret;
  862. if (read_data[interface_num] == exp_value)
  863. break;
  864. }
  865. if (poll_cnt >= poll_tries) {
  866. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  867. ("max poll IF #%d\n", interface_num));
  868. is_fail = 1;
  869. is_if_fail = 1;
  870. }
  871. training_result[training_stage][interface_num] =
  872. (is_if_fail == 1) ? TEST_FAILED : TEST_SUCCESS;
  873. }
  874. return (is_fail == 0) ? MV_OK : MV_FAIL;
  875. }
  876. /*
  877. * Bus read access
  878. */
  879. int ddr3_tip_bus_read(u32 dev_num, u32 if_id,
  880. enum hws_access_type phy_access, u32 phy_id,
  881. enum hws_ddr_phy phy_type, u32 reg_addr, u32 *data)
  882. {
  883. u32 bus_index = 0;
  884. u32 data_read[MAX_INTERFACE_NUM];
  885. struct hws_topology_map *tm = ddr3_get_topology_map();
  886. if (phy_access == ACCESS_TYPE_MULTICAST) {
  887. for (bus_index = 0; bus_index < GET_TOPOLOGY_NUM_OF_BUSES();
  888. bus_index++) {
  889. VALIDATE_ACTIVE(tm->bus_act_mask, bus_index);
  890. CHECK_STATUS(ddr3_tip_bus_access
  891. (dev_num, ACCESS_TYPE_UNICAST,
  892. if_id, ACCESS_TYPE_UNICAST,
  893. bus_index, phy_type, reg_addr, 0,
  894. OPERATION_READ));
  895. CHECK_STATUS(ddr3_tip_if_read
  896. (dev_num, ACCESS_TYPE_UNICAST, if_id,
  897. PHY_REG_FILE_ACCESS, data_read,
  898. MASK_ALL_BITS));
  899. data[bus_index] = (data_read[if_id] & 0xffff);
  900. }
  901. } else {
  902. CHECK_STATUS(ddr3_tip_bus_access
  903. (dev_num, ACCESS_TYPE_UNICAST, if_id,
  904. phy_access, phy_id, phy_type, reg_addr, 0,
  905. OPERATION_READ));
  906. CHECK_STATUS(ddr3_tip_if_read
  907. (dev_num, ACCESS_TYPE_UNICAST, if_id,
  908. PHY_REG_FILE_ACCESS, data_read, MASK_ALL_BITS));
  909. /*
  910. * only 16 lsb bit are valid in Phy (each register is different,
  911. * some can actually be less than 16 bits)
  912. */
  913. *data = (data_read[if_id] & 0xffff);
  914. }
  915. return MV_OK;
  916. }
  917. /*
  918. * Bus write access
  919. */
  920. int ddr3_tip_bus_write(u32 dev_num, enum hws_access_type interface_access,
  921. u32 if_id, enum hws_access_type phy_access,
  922. u32 phy_id, enum hws_ddr_phy phy_type, u32 reg_addr,
  923. u32 data_value)
  924. {
  925. CHECK_STATUS(ddr3_tip_bus_access
  926. (dev_num, interface_access, if_id, phy_access,
  927. phy_id, phy_type, reg_addr, data_value, OPERATION_WRITE));
  928. return MV_OK;
  929. }
  930. /*
  931. * Bus access routine (relevant for both read & write)
  932. */
  933. static int ddr3_tip_bus_access(u32 dev_num, enum hws_access_type interface_access,
  934. u32 if_id, enum hws_access_type phy_access,
  935. u32 phy_id, enum hws_ddr_phy phy_type, u32 reg_addr,
  936. u32 data_value, enum hws_operation oper_type)
  937. {
  938. u32 addr_low = 0x3f & reg_addr;
  939. u32 addr_hi = ((0xc0 & reg_addr) >> 6);
  940. u32 data_p1 =
  941. (oper_type << 30) + (addr_hi << 28) + (phy_access << 27) +
  942. (phy_type << 26) + (phy_id << 22) + (addr_low << 16) +
  943. (data_value & 0xffff);
  944. u32 data_p2 = data_p1 + (1 << 31);
  945. u32 start_if, end_if;
  946. struct hws_topology_map *tm = ddr3_get_topology_map();
  947. CHECK_STATUS(ddr3_tip_if_write
  948. (dev_num, interface_access, if_id, PHY_REG_FILE_ACCESS,
  949. data_p1, MASK_ALL_BITS));
  950. CHECK_STATUS(ddr3_tip_if_write
  951. (dev_num, interface_access, if_id, PHY_REG_FILE_ACCESS,
  952. data_p2, MASK_ALL_BITS));
  953. if (interface_access == ACCESS_TYPE_UNICAST) {
  954. start_if = if_id;
  955. end_if = if_id;
  956. } else {
  957. start_if = 0;
  958. end_if = MAX_INTERFACE_NUM - 1;
  959. }
  960. /* polling for read/write execution done */
  961. for (if_id = start_if; if_id <= end_if; if_id++) {
  962. VALIDATE_ACTIVE(tm->if_act_mask, if_id);
  963. CHECK_STATUS(is_bus_access_done
  964. (dev_num, if_id, PHY_REG_FILE_ACCESS, 31));
  965. }
  966. return MV_OK;
  967. }
  968. /*
  969. * Check bus access done
  970. */
  971. static int is_bus_access_done(u32 dev_num, u32 if_id, u32 dunit_reg_adrr,
  972. u32 bit)
  973. {
  974. u32 rd_data = 1;
  975. u32 cnt = 0;
  976. u32 data_read[MAX_INTERFACE_NUM];
  977. CHECK_STATUS(ddr3_tip_if_read
  978. (dev_num, ACCESS_TYPE_UNICAST, if_id, dunit_reg_adrr,
  979. data_read, MASK_ALL_BITS));
  980. rd_data = data_read[if_id];
  981. rd_data &= (1 << bit);
  982. while (rd_data != 0) {
  983. if (cnt++ >= MAX_POLLING_ITERATIONS)
  984. break;
  985. CHECK_STATUS(ddr3_tip_if_read
  986. (dev_num, ACCESS_TYPE_UNICAST, if_id,
  987. dunit_reg_adrr, data_read, MASK_ALL_BITS));
  988. rd_data = data_read[if_id];
  989. rd_data &= (1 << bit);
  990. }
  991. if (cnt < MAX_POLLING_ITERATIONS)
  992. return MV_OK;
  993. else
  994. return MV_FAIL;
  995. }
  996. /*
  997. * Phy read-modify-write
  998. */
  999. int ddr3_tip_bus_read_modify_write(u32 dev_num, enum hws_access_type access_type,
  1000. u32 interface_id, u32 phy_id,
  1001. enum hws_ddr_phy phy_type, u32 reg_addr,
  1002. u32 data_value, u32 reg_mask)
  1003. {
  1004. u32 data_val = 0, if_id, start_if, end_if;
  1005. struct hws_topology_map *tm = ddr3_get_topology_map();
  1006. if (access_type == ACCESS_TYPE_MULTICAST) {
  1007. start_if = 0;
  1008. end_if = MAX_INTERFACE_NUM - 1;
  1009. } else {
  1010. start_if = interface_id;
  1011. end_if = interface_id;
  1012. }
  1013. for (if_id = start_if; if_id <= end_if; if_id++) {
  1014. VALIDATE_ACTIVE(tm->if_act_mask, if_id);
  1015. CHECK_STATUS(ddr3_tip_bus_read
  1016. (dev_num, if_id, ACCESS_TYPE_UNICAST, phy_id,
  1017. phy_type, reg_addr, &data_val));
  1018. data_value = (data_val & (~reg_mask)) | (data_value & reg_mask);
  1019. CHECK_STATUS(ddr3_tip_bus_write
  1020. (dev_num, ACCESS_TYPE_UNICAST, if_id,
  1021. ACCESS_TYPE_UNICAST, phy_id, phy_type, reg_addr,
  1022. data_value));
  1023. }
  1024. return MV_OK;
  1025. }
  1026. /*
  1027. * ADLL Calibration
  1028. */
  1029. int adll_calibration(u32 dev_num, enum hws_access_type access_type,
  1030. u32 if_id, enum hws_ddr_freq frequency)
  1031. {
  1032. struct hws_tip_freq_config_info freq_config_info;
  1033. u32 bus_cnt = 0;
  1034. struct hws_topology_map *tm = ddr3_get_topology_map();
  1035. /* Reset Diver_b assert -> de-assert */
  1036. CHECK_STATUS(ddr3_tip_if_write
  1037. (dev_num, access_type, if_id, SDRAM_CONFIGURATION_REG,
  1038. 0, 0x10000000));
  1039. mdelay(10);
  1040. CHECK_STATUS(ddr3_tip_if_write
  1041. (dev_num, access_type, if_id, SDRAM_CONFIGURATION_REG,
  1042. 0x10000000, 0x10000000));
  1043. if (config_func_info[dev_num].tip_get_freq_config_info_func != NULL) {
  1044. CHECK_STATUS(config_func_info[dev_num].
  1045. tip_get_freq_config_info_func((u8)dev_num, frequency,
  1046. &freq_config_info));
  1047. } else {
  1048. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  1049. ("tip_get_freq_config_info_func is NULL"));
  1050. return MV_NOT_INITIALIZED;
  1051. }
  1052. for (bus_cnt = 0; bus_cnt < GET_TOPOLOGY_NUM_OF_BUSES(); bus_cnt++) {
  1053. VALIDATE_ACTIVE(tm->bus_act_mask, bus_cnt);
  1054. CHECK_STATUS(ddr3_tip_bus_read_modify_write
  1055. (dev_num, access_type, if_id, bus_cnt,
  1056. DDR_PHY_DATA, BW_PHY_REG,
  1057. freq_config_info.bw_per_freq << 8, 0x700));
  1058. CHECK_STATUS(ddr3_tip_bus_read_modify_write
  1059. (dev_num, access_type, if_id, bus_cnt,
  1060. DDR_PHY_DATA, RATE_PHY_REG,
  1061. freq_config_info.rate_per_freq, 0x7));
  1062. }
  1063. /* DUnit to Phy drive post edge, ADLL reset assert de-assert */
  1064. CHECK_STATUS(ddr3_tip_if_write
  1065. (dev_num, access_type, if_id, DRAM_PHY_CONFIGURATION,
  1066. 0, (0x80000000 | 0x40000000)));
  1067. mdelay(100 / (freq_val[frequency] / freq_val[DDR_FREQ_LOW_FREQ]));
  1068. CHECK_STATUS(ddr3_tip_if_write
  1069. (dev_num, access_type, if_id, DRAM_PHY_CONFIGURATION,
  1070. (0x80000000 | 0x40000000), (0x80000000 | 0x40000000)));
  1071. /* polling for ADLL Done */
  1072. if (ddr3_tip_if_polling(dev_num, access_type, if_id,
  1073. 0x3ff03ff, 0x3ff03ff, PHY_LOCK_STATUS_REG,
  1074. MAX_POLLING_ITERATIONS) != MV_OK) {
  1075. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  1076. ("Freq_set: DDR3 poll failed(1)"));
  1077. }
  1078. /* pup data_pup reset assert-> deassert */
  1079. CHECK_STATUS(ddr3_tip_if_write
  1080. (dev_num, access_type, if_id, SDRAM_CONFIGURATION_REG,
  1081. 0, 0x60000000));
  1082. mdelay(10);
  1083. CHECK_STATUS(ddr3_tip_if_write
  1084. (dev_num, access_type, if_id, SDRAM_CONFIGURATION_REG,
  1085. 0x60000000, 0x60000000));
  1086. return MV_OK;
  1087. }
  1088. int ddr3_tip_freq_set(u32 dev_num, enum hws_access_type access_type,
  1089. u32 if_id, enum hws_ddr_freq frequency)
  1090. {
  1091. u32 cl_value = 0, cwl_value = 0, mem_mask = 0, val = 0,
  1092. bus_cnt = 0, t_hclk = 0, t_wr = 0,
  1093. refresh_interval_cnt = 0, cnt_id;
  1094. u32 t_ckclk;
  1095. u32 t_refi = 0, end_if, start_if;
  1096. u32 bus_index = 0;
  1097. int is_dll_off = 0;
  1098. enum hws_speed_bin speed_bin_index = 0;
  1099. struct hws_tip_freq_config_info freq_config_info;
  1100. enum hws_result *flow_result = training_result[training_stage];
  1101. u32 adll_tap = 0;
  1102. u32 cs_mask[MAX_INTERFACE_NUM];
  1103. struct hws_topology_map *tm = ddr3_get_topology_map();
  1104. DEBUG_TRAINING_IP(DEBUG_LEVEL_TRACE,
  1105. ("dev %d access %d IF %d freq %d\n", dev_num,
  1106. access_type, if_id, frequency));
  1107. if (frequency == DDR_FREQ_LOW_FREQ)
  1108. is_dll_off = 1;
  1109. if (access_type == ACCESS_TYPE_MULTICAST) {
  1110. start_if = 0;
  1111. end_if = MAX_INTERFACE_NUM - 1;
  1112. } else {
  1113. start_if = if_id;
  1114. end_if = if_id;
  1115. }
  1116. /* calculate interface cs mask - Oferb 4/11 */
  1117. /* speed bin can be different for each interface */
  1118. for (if_id = 0; if_id <= MAX_INTERFACE_NUM - 1; if_id++) {
  1119. /* cs enable is active low */
  1120. VALIDATE_ACTIVE(tm->if_act_mask, if_id);
  1121. cs_mask[if_id] = CS_BIT_MASK;
  1122. training_result[training_stage][if_id] = TEST_SUCCESS;
  1123. ddr3_tip_calc_cs_mask(dev_num, if_id, effective_cs,
  1124. &cs_mask[if_id]);
  1125. }
  1126. /* speed bin can be different for each interface */
  1127. /*
  1128. * moti b - need to remove the loop for multicas access functions
  1129. * and loop the unicast access functions
  1130. */
  1131. for (if_id = start_if; if_id <= end_if; if_id++) {
  1132. if (IS_ACTIVE(tm->if_act_mask, if_id) == 0)
  1133. continue;
  1134. flow_result[if_id] = TEST_SUCCESS;
  1135. speed_bin_index =
  1136. tm->interface_params[if_id].speed_bin_index;
  1137. if (tm->interface_params[if_id].memory_freq ==
  1138. frequency) {
  1139. cl_value =
  1140. tm->interface_params[if_id].cas_l;
  1141. cwl_value =
  1142. tm->interface_params[if_id].cas_wl;
  1143. } else {
  1144. cl_value =
  1145. cas_latency_table[speed_bin_index].cl_val[frequency];
  1146. cwl_value =
  1147. cas_write_latency_table[speed_bin_index].
  1148. cl_val[frequency];
  1149. }
  1150. DEBUG_TRAINING_IP(DEBUG_LEVEL_TRACE,
  1151. ("Freq_set dev 0x%x access 0x%x if 0x%x freq 0x%x speed %d:\n\t",
  1152. dev_num, access_type, if_id,
  1153. frequency, speed_bin_index));
  1154. for (cnt_id = 0; cnt_id < DDR_FREQ_LIMIT; cnt_id++) {
  1155. DEBUG_TRAINING_IP(DEBUG_LEVEL_TRACE,
  1156. ("%d ",
  1157. cas_latency_table[speed_bin_index].
  1158. cl_val[cnt_id]));
  1159. }
  1160. DEBUG_TRAINING_IP(DEBUG_LEVEL_TRACE, ("\n"));
  1161. mem_mask = 0;
  1162. for (bus_index = 0; bus_index < GET_TOPOLOGY_NUM_OF_BUSES();
  1163. bus_index++) {
  1164. VALIDATE_ACTIVE(tm->bus_act_mask, bus_index);
  1165. mem_mask |=
  1166. tm->interface_params[if_id].
  1167. as_bus_params[bus_index].mirror_enable_bitmask;
  1168. }
  1169. if (mem_mask != 0) {
  1170. /* motib redundant in KW28 */
  1171. CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type,
  1172. if_id,
  1173. CS_ENABLE_REG, 0, 0x8));
  1174. }
  1175. /* dll state after exiting SR */
  1176. if (is_dll_off == 1) {
  1177. CHECK_STATUS(ddr3_tip_if_write
  1178. (dev_num, access_type, if_id,
  1179. DFS_REG, 0x1, 0x1));
  1180. } else {
  1181. CHECK_STATUS(ddr3_tip_if_write
  1182. (dev_num, access_type, if_id,
  1183. DFS_REG, 0, 0x1));
  1184. }
  1185. CHECK_STATUS(ddr3_tip_if_write
  1186. (dev_num, access_type, if_id,
  1187. DUNIT_MMASK_REG, 0, 0x1));
  1188. /* DFS - block transactions */
  1189. CHECK_STATUS(ddr3_tip_if_write
  1190. (dev_num, access_type, if_id,
  1191. DFS_REG, 0x2, 0x2));
  1192. /* disable ODT in case of dll off */
  1193. if (is_dll_off == 1) {
  1194. CHECK_STATUS(ddr3_tip_if_write
  1195. (dev_num, access_type, if_id,
  1196. 0x1874, 0, 0x244));
  1197. CHECK_STATUS(ddr3_tip_if_write
  1198. (dev_num, access_type, if_id,
  1199. 0x1884, 0, 0x244));
  1200. CHECK_STATUS(ddr3_tip_if_write
  1201. (dev_num, access_type, if_id,
  1202. 0x1894, 0, 0x244));
  1203. CHECK_STATUS(ddr3_tip_if_write
  1204. (dev_num, access_type, if_id,
  1205. 0x18a4, 0, 0x244));
  1206. }
  1207. /* DFS - Enter Self-Refresh */
  1208. CHECK_STATUS(ddr3_tip_if_write
  1209. (dev_num, access_type, if_id, DFS_REG, 0x4,
  1210. 0x4));
  1211. /* polling on self refresh entry */
  1212. if (ddr3_tip_if_polling(dev_num, ACCESS_TYPE_UNICAST,
  1213. if_id, 0x8, 0x8, DFS_REG,
  1214. MAX_POLLING_ITERATIONS) != MV_OK) {
  1215. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  1216. ("Freq_set: DDR3 poll failed on SR entry\n"));
  1217. }
  1218. /* PLL configuration */
  1219. if (config_func_info[dev_num].tip_set_freq_divider_func != NULL) {
  1220. config_func_info[dev_num].
  1221. tip_set_freq_divider_func(dev_num, if_id,
  1222. frequency);
  1223. }
  1224. /* PLL configuration End */
  1225. /* adjust t_refi to new frequency */
  1226. t_refi = (tm->interface_params[if_id].interface_temp ==
  1227. HWS_TEMP_HIGH) ? TREFI_HIGH : TREFI_LOW;
  1228. t_refi *= 1000; /*psec */
  1229. /* HCLK in[ps] */
  1230. t_hclk = MEGA / (freq_val[frequency] / 2);
  1231. refresh_interval_cnt = t_refi / t_hclk; /* no units */
  1232. val = 0x4000 | refresh_interval_cnt;
  1233. CHECK_STATUS(ddr3_tip_if_write
  1234. (dev_num, access_type, if_id,
  1235. SDRAM_CONFIGURATION_REG, val, 0x7fff));
  1236. /* DFS - CL/CWL/WR parameters after exiting SR */
  1237. CHECK_STATUS(ddr3_tip_if_write
  1238. (dev_num, access_type, if_id, DFS_REG,
  1239. (cl_mask_table[cl_value] << 8), 0xf00));
  1240. CHECK_STATUS(ddr3_tip_if_write
  1241. (dev_num, access_type, if_id, DFS_REG,
  1242. (cwl_mask_table[cwl_value] << 12), 0x7000));
  1243. t_ckclk = MEGA / freq_val[frequency];
  1244. t_wr = TIME_2_CLOCK_CYCLES(speed_bin_table(speed_bin_index,
  1245. SPEED_BIN_TWR),
  1246. t_ckclk);
  1247. CHECK_STATUS(ddr3_tip_if_write
  1248. (dev_num, access_type, if_id, DFS_REG,
  1249. (twr_mask_table[t_wr + 1] << 16), 0x70000));
  1250. /* Restore original RTT values if returning from DLL OFF mode */
  1251. if (is_dll_off == 1) {
  1252. CHECK_STATUS(ddr3_tip_if_write
  1253. (dev_num, access_type, if_id, 0x1874,
  1254. g_dic | g_rtt_nom, 0x266));
  1255. CHECK_STATUS(ddr3_tip_if_write
  1256. (dev_num, access_type, if_id, 0x1884,
  1257. g_dic | g_rtt_nom, 0x266));
  1258. CHECK_STATUS(ddr3_tip_if_write
  1259. (dev_num, access_type, if_id, 0x1894,
  1260. g_dic | g_rtt_nom, 0x266));
  1261. CHECK_STATUS(ddr3_tip_if_write
  1262. (dev_num, access_type, if_id, 0x18a4,
  1263. g_dic | g_rtt_nom, 0x266));
  1264. }
  1265. /* Reset Diver_b assert -> de-assert */
  1266. CHECK_STATUS(ddr3_tip_if_write
  1267. (dev_num, access_type, if_id,
  1268. SDRAM_CONFIGURATION_REG, 0, 0x10000000));
  1269. mdelay(10);
  1270. CHECK_STATUS(ddr3_tip_if_write
  1271. (dev_num, access_type, if_id,
  1272. SDRAM_CONFIGURATION_REG, 0x10000000, 0x10000000));
  1273. /* Adll configuration function of process and Frequency */
  1274. if (config_func_info[dev_num].tip_get_freq_config_info_func != NULL) {
  1275. CHECK_STATUS(config_func_info[dev_num].
  1276. tip_get_freq_config_info_func(dev_num, frequency,
  1277. &freq_config_info));
  1278. }
  1279. /* TBD check milo5 using device ID ? */
  1280. for (bus_cnt = 0; bus_cnt < GET_TOPOLOGY_NUM_OF_BUSES();
  1281. bus_cnt++) {
  1282. VALIDATE_ACTIVE(tm->bus_act_mask, bus_cnt);
  1283. CHECK_STATUS(ddr3_tip_bus_read_modify_write
  1284. (dev_num, ACCESS_TYPE_UNICAST,
  1285. if_id, bus_cnt, DDR_PHY_DATA,
  1286. 0x92,
  1287. freq_config_info.
  1288. bw_per_freq << 8
  1289. /*freq_mask[dev_num][frequency] << 8 */
  1290. , 0x700));
  1291. CHECK_STATUS(ddr3_tip_bus_read_modify_write
  1292. (dev_num, ACCESS_TYPE_UNICAST, if_id,
  1293. bus_cnt, DDR_PHY_DATA, 0x94,
  1294. freq_config_info.rate_per_freq, 0x7));
  1295. }
  1296. /* DUnit to Phy drive post edge, ADLL reset assert de-assert */
  1297. CHECK_STATUS(ddr3_tip_if_write
  1298. (dev_num, access_type, if_id,
  1299. DRAM_PHY_CONFIGURATION, 0,
  1300. (0x80000000 | 0x40000000)));
  1301. mdelay(100 / (freq_val[frequency] / freq_val[DDR_FREQ_LOW_FREQ]));
  1302. CHECK_STATUS(ddr3_tip_if_write
  1303. (dev_num, access_type, if_id,
  1304. DRAM_PHY_CONFIGURATION, (0x80000000 | 0x40000000),
  1305. (0x80000000 | 0x40000000)));
  1306. /* polling for ADLL Done */
  1307. if (ddr3_tip_if_polling
  1308. (dev_num, ACCESS_TYPE_UNICAST, if_id, 0x3ff03ff,
  1309. 0x3ff03ff, PHY_LOCK_STATUS_REG,
  1310. MAX_POLLING_ITERATIONS) != MV_OK) {
  1311. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  1312. ("Freq_set: DDR3 poll failed(1)\n"));
  1313. }
  1314. /* pup data_pup reset assert-> deassert */
  1315. CHECK_STATUS(ddr3_tip_if_write
  1316. (dev_num, access_type, if_id,
  1317. SDRAM_CONFIGURATION_REG, 0, 0x60000000));
  1318. mdelay(10);
  1319. CHECK_STATUS(ddr3_tip_if_write
  1320. (dev_num, access_type, if_id,
  1321. SDRAM_CONFIGURATION_REG, 0x60000000, 0x60000000));
  1322. /* Set proper timing params before existing Self-Refresh */
  1323. ddr3_tip_set_timing(dev_num, access_type, if_id, frequency);
  1324. if (delay_enable != 0) {
  1325. adll_tap = MEGA / (freq_val[frequency] * 64);
  1326. ddr3_tip_cmd_addr_init_delay(dev_num, adll_tap);
  1327. }
  1328. /* Exit SR */
  1329. CHECK_STATUS(ddr3_tip_if_write
  1330. (dev_num, access_type, if_id, DFS_REG, 0,
  1331. 0x4));
  1332. if (ddr3_tip_if_polling
  1333. (dev_num, ACCESS_TYPE_UNICAST, if_id, 0, 0x8, DFS_REG,
  1334. MAX_POLLING_ITERATIONS) != MV_OK) {
  1335. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  1336. ("Freq_set: DDR3 poll failed(2)"));
  1337. }
  1338. /* Refresh Command */
  1339. CHECK_STATUS(ddr3_tip_if_write
  1340. (dev_num, access_type, if_id,
  1341. SDRAM_OPERATION_REG, 0x2, 0xf1f));
  1342. if (ddr3_tip_if_polling
  1343. (dev_num, ACCESS_TYPE_UNICAST, if_id, 0, 0x1f,
  1344. SDRAM_OPERATION_REG, MAX_POLLING_ITERATIONS) != MV_OK) {
  1345. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  1346. ("Freq_set: DDR3 poll failed(3)"));
  1347. }
  1348. /* Release DFS Block */
  1349. CHECK_STATUS(ddr3_tip_if_write
  1350. (dev_num, access_type, if_id, DFS_REG, 0,
  1351. 0x2));
  1352. /* Controller to MBUS Retry - normal */
  1353. CHECK_STATUS(ddr3_tip_if_write
  1354. (dev_num, access_type, if_id, DUNIT_MMASK_REG,
  1355. 0x1, 0x1));
  1356. /* MRO: Burst Length 8, CL , Auto_precharge 0x16cc */
  1357. val =
  1358. ((cl_mask_table[cl_value] & 0x1) << 2) |
  1359. ((cl_mask_table[cl_value] & 0xe) << 3);
  1360. CHECK_STATUS(ddr3_tip_if_write
  1361. (dev_num, access_type, if_id, MR0_REG,
  1362. val, (0x7 << 4) | (1 << 2)));
  1363. /* MR2: CWL = 10 , Auto Self-Refresh - disable */
  1364. val = (cwl_mask_table[cwl_value] << 3);
  1365. /*
  1366. * nklein 24.10.13 - should not be here - leave value as set in
  1367. * the init configuration val |= (1 << 9);
  1368. * val |= ((tm->interface_params[if_id].
  1369. * interface_temp == HWS_TEMP_HIGH) ? (1 << 7) : 0);
  1370. */
  1371. /* nklein 24.10.13 - see above comment */
  1372. CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type,
  1373. if_id, MR2_REG,
  1374. val, (0x7 << 3)));
  1375. /* ODT TIMING */
  1376. val = ((cl_value - cwl_value + 1) << 4) |
  1377. ((cl_value - cwl_value + 6) << 8) |
  1378. ((cl_value - 1) << 12) | ((cl_value + 6) << 16);
  1379. CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type,
  1380. if_id, ODT_TIMING_LOW,
  1381. val, 0xffff0));
  1382. val = 0x91 | ((cwl_value - 1) << 8) | ((cwl_value + 5) << 12);
  1383. CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type,
  1384. if_id, ODT_TIMING_HI_REG,
  1385. val, 0xffff));
  1386. /* ODT Active */
  1387. CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type,
  1388. if_id,
  1389. DUNIT_ODT_CONTROL_REG,
  1390. 0xf, 0xf));
  1391. /* re-write CL */
  1392. val = ((cl_mask_table[cl_value] & 0x1) << 2) |
  1393. ((cl_mask_table[cl_value] & 0xe) << 3);
  1394. CHECK_STATUS(ddr3_tip_if_write(dev_num, ACCESS_TYPE_MULTICAST,
  1395. 0, MR0_REG, val,
  1396. (0x7 << 4) | (1 << 2)));
  1397. /* re-write CWL */
  1398. val = (cwl_mask_table[cwl_value] << 3);
  1399. CHECK_STATUS(ddr3_tip_write_mrs_cmd(dev_num, cs_mask, MRS2_CMD,
  1400. val, (0x7 << 3)));
  1401. CHECK_STATUS(ddr3_tip_if_write(dev_num, ACCESS_TYPE_MULTICAST,
  1402. 0, MR2_REG, val, (0x7 << 3)));
  1403. if (mem_mask != 0) {
  1404. CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type,
  1405. if_id,
  1406. CS_ENABLE_REG,
  1407. 1 << 3, 0x8));
  1408. }
  1409. }
  1410. return MV_OK;
  1411. }
  1412. /*
  1413. * Set ODT values
  1414. */
  1415. static int ddr3_tip_write_odt(u32 dev_num, enum hws_access_type access_type,
  1416. u32 if_id, u32 cl_value, u32 cwl_value)
  1417. {
  1418. /* ODT TIMING */
  1419. u32 val = (cl_value - cwl_value + 6);
  1420. val = ((cl_value - cwl_value + 1) << 4) | ((val & 0xf) << 8) |
  1421. (((cl_value - 1) & 0xf) << 12) |
  1422. (((cl_value + 6) & 0xf) << 16) | (((val & 0x10) >> 4) << 21);
  1423. val |= (((cl_value - 1) >> 4) << 22) | (((cl_value + 6) >> 4) << 23);
  1424. CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id,
  1425. ODT_TIMING_LOW, val, 0xffff0));
  1426. val = 0x91 | ((cwl_value - 1) << 8) | ((cwl_value + 5) << 12);
  1427. CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id,
  1428. ODT_TIMING_HI_REG, val, 0xffff));
  1429. if (odt_additional == 1) {
  1430. CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type,
  1431. if_id,
  1432. SDRAM_ODT_CONTROL_HIGH_REG,
  1433. 0xf, 0xf));
  1434. }
  1435. /* ODT Active */
  1436. CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id,
  1437. DUNIT_ODT_CONTROL_REG, 0xf, 0xf));
  1438. return MV_OK;
  1439. }
  1440. /*
  1441. * Set Timing values for training
  1442. */
  1443. static int ddr3_tip_set_timing(u32 dev_num, enum hws_access_type access_type,
  1444. u32 if_id, enum hws_ddr_freq frequency)
  1445. {
  1446. u32 t_ckclk = 0, t_ras = 0;
  1447. u32 t_rcd = 0, t_rp = 0, t_wr = 0, t_wtr = 0, t_rrd = 0, t_rtp = 0,
  1448. t_rfc = 0, t_mod = 0;
  1449. u32 val = 0, page_size = 0;
  1450. enum hws_speed_bin speed_bin_index;
  1451. enum hws_mem_size memory_size = MEM_2G;
  1452. struct hws_topology_map *tm = ddr3_get_topology_map();
  1453. speed_bin_index = tm->interface_params[if_id].speed_bin_index;
  1454. memory_size = tm->interface_params[if_id].memory_size;
  1455. page_size =
  1456. (tm->interface_params[if_id].bus_width ==
  1457. BUS_WIDTH_8) ? page_param[memory_size].
  1458. page_size_8bit : page_param[memory_size].page_size_16bit;
  1459. t_ckclk = (MEGA / freq_val[frequency]);
  1460. t_rrd = (page_size == 1) ? speed_bin_table(speed_bin_index,
  1461. SPEED_BIN_TRRD1K) :
  1462. speed_bin_table(speed_bin_index, SPEED_BIN_TRRD2K);
  1463. t_rrd = GET_MAX_VALUE(t_ckclk * 4, t_rrd);
  1464. t_rtp = GET_MAX_VALUE(t_ckclk * 4, speed_bin_table(speed_bin_index,
  1465. SPEED_BIN_TRTP));
  1466. t_wtr = GET_MAX_VALUE(t_ckclk * 4, speed_bin_table(speed_bin_index,
  1467. SPEED_BIN_TWTR));
  1468. t_ras = TIME_2_CLOCK_CYCLES(speed_bin_table(speed_bin_index,
  1469. SPEED_BIN_TRAS),
  1470. t_ckclk);
  1471. t_rcd = TIME_2_CLOCK_CYCLES(speed_bin_table(speed_bin_index,
  1472. SPEED_BIN_TRCD),
  1473. t_ckclk);
  1474. t_rp = TIME_2_CLOCK_CYCLES(speed_bin_table(speed_bin_index,
  1475. SPEED_BIN_TRP),
  1476. t_ckclk);
  1477. t_wr = TIME_2_CLOCK_CYCLES(speed_bin_table(speed_bin_index,
  1478. SPEED_BIN_TWR),
  1479. t_ckclk);
  1480. t_wtr = TIME_2_CLOCK_CYCLES(t_wtr, t_ckclk);
  1481. t_rrd = TIME_2_CLOCK_CYCLES(t_rrd, t_ckclk);
  1482. t_rtp = TIME_2_CLOCK_CYCLES(t_rtp, t_ckclk);
  1483. t_rfc = TIME_2_CLOCK_CYCLES(rfc_table[memory_size] * 1000, t_ckclk);
  1484. t_mod = GET_MAX_VALUE(t_ckclk * 24, 15000);
  1485. t_mod = TIME_2_CLOCK_CYCLES(t_mod, t_ckclk);
  1486. /* SDRAM Timing Low */
  1487. val = (t_ras & 0xf) | (t_rcd << 4) | (t_rp << 8) | (t_wr << 12) |
  1488. (t_wtr << 16) | (((t_ras & 0x30) >> 4) << 20) | (t_rrd << 24) |
  1489. (t_rtp << 28);
  1490. CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id,
  1491. SDRAM_TIMING_LOW_REG, val, 0xff3fffff));
  1492. /* SDRAM Timing High */
  1493. CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id,
  1494. SDRAM_TIMING_HIGH_REG,
  1495. t_rfc & 0x7f, 0x7f));
  1496. CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id,
  1497. SDRAM_TIMING_HIGH_REG,
  1498. 0x180, 0x180));
  1499. CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id,
  1500. SDRAM_TIMING_HIGH_REG,
  1501. 0x600, 0x600));
  1502. CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id,
  1503. SDRAM_TIMING_HIGH_REG,
  1504. 0x1800, 0xf800));
  1505. CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id,
  1506. SDRAM_TIMING_HIGH_REG,
  1507. ((t_rfc & 0x380) >> 7) << 16, 0x70000));
  1508. CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id,
  1509. SDRAM_TIMING_HIGH_REG, 0,
  1510. 0x380000));
  1511. CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id,
  1512. SDRAM_TIMING_HIGH_REG,
  1513. (t_mod & 0xf) << 25, 0x1e00000));
  1514. CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id,
  1515. SDRAM_TIMING_HIGH_REG,
  1516. (t_mod >> 4) << 30, 0xc0000000));
  1517. CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id,
  1518. SDRAM_TIMING_HIGH_REG,
  1519. 0x16000000, 0x1e000000));
  1520. CHECK_STATUS(ddr3_tip_if_write(dev_num, access_type, if_id,
  1521. SDRAM_TIMING_HIGH_REG,
  1522. 0x40000000, 0xc0000000));
  1523. return MV_OK;
  1524. }
  1525. /*
  1526. * Mode Read
  1527. */
  1528. int hws_ddr3_tip_mode_read(u32 dev_num, struct mode_info *mode_info)
  1529. {
  1530. u32 ret;
  1531. ret = ddr3_tip_if_read(dev_num, ACCESS_TYPE_MULTICAST, PARAM_NOT_CARE,
  1532. MR0_REG, mode_info->reg_mr0, MASK_ALL_BITS);
  1533. if (ret != MV_OK)
  1534. return ret;
  1535. ret = ddr3_tip_if_read(dev_num, ACCESS_TYPE_MULTICAST, PARAM_NOT_CARE,
  1536. MR1_REG, mode_info->reg_mr1, MASK_ALL_BITS);
  1537. if (ret != MV_OK)
  1538. return ret;
  1539. ret = ddr3_tip_if_read(dev_num, ACCESS_TYPE_MULTICAST, PARAM_NOT_CARE,
  1540. MR2_REG, mode_info->reg_mr2, MASK_ALL_BITS);
  1541. if (ret != MV_OK)
  1542. return ret;
  1543. ret = ddr3_tip_if_read(dev_num, ACCESS_TYPE_MULTICAST, PARAM_NOT_CARE,
  1544. MR3_REG, mode_info->reg_mr2, MASK_ALL_BITS);
  1545. if (ret != MV_OK)
  1546. return ret;
  1547. ret = ddr3_tip_if_read(dev_num, ACCESS_TYPE_MULTICAST, PARAM_NOT_CARE,
  1548. READ_DATA_SAMPLE_DELAY, mode_info->read_data_sample,
  1549. MASK_ALL_BITS);
  1550. if (ret != MV_OK)
  1551. return ret;
  1552. ret = ddr3_tip_if_read(dev_num, ACCESS_TYPE_MULTICAST, PARAM_NOT_CARE,
  1553. READ_DATA_READY_DELAY, mode_info->read_data_ready,
  1554. MASK_ALL_BITS);
  1555. if (ret != MV_OK)
  1556. return ret;
  1557. return MV_OK;
  1558. }
  1559. /*
  1560. * Get first active IF
  1561. */
  1562. int ddr3_tip_get_first_active_if(u8 dev_num, u32 interface_mask,
  1563. u32 *interface_id)
  1564. {
  1565. u32 if_id;
  1566. struct hws_topology_map *tm = ddr3_get_topology_map();
  1567. for (if_id = 0; if_id <= MAX_INTERFACE_NUM - 1; if_id++) {
  1568. VALIDATE_ACTIVE(tm->if_act_mask, if_id);
  1569. if (interface_mask & (1 << if_id)) {
  1570. *interface_id = if_id;
  1571. break;
  1572. }
  1573. }
  1574. return MV_OK;
  1575. }
  1576. /*
  1577. * Write CS Result
  1578. */
  1579. int ddr3_tip_write_cs_result(u32 dev_num, u32 offset)
  1580. {
  1581. u32 if_id, bus_num, cs_bitmask, data_val, cs_num;
  1582. struct hws_topology_map *tm = ddr3_get_topology_map();
  1583. for (if_id = 0; if_id <= MAX_INTERFACE_NUM - 1; if_id++) {
  1584. VALIDATE_ACTIVE(tm->if_act_mask, if_id);
  1585. for (bus_num = 0; bus_num < tm->num_of_bus_per_interface;
  1586. bus_num++) {
  1587. VALIDATE_ACTIVE(tm->bus_act_mask, bus_num);
  1588. cs_bitmask =
  1589. tm->interface_params[if_id].
  1590. as_bus_params[bus_num].cs_bitmask;
  1591. if (cs_bitmask != effective_cs) {
  1592. cs_num = GET_CS_FROM_MASK(cs_bitmask);
  1593. ddr3_tip_bus_read(dev_num, if_id,
  1594. ACCESS_TYPE_UNICAST, bus_num,
  1595. DDR_PHY_DATA,
  1596. offset +
  1597. CS_REG_VALUE(effective_cs),
  1598. &data_val);
  1599. ddr3_tip_bus_write(dev_num,
  1600. ACCESS_TYPE_UNICAST,
  1601. if_id,
  1602. ACCESS_TYPE_UNICAST,
  1603. bus_num, DDR_PHY_DATA,
  1604. offset +
  1605. CS_REG_VALUE(cs_num),
  1606. data_val);
  1607. }
  1608. }
  1609. }
  1610. return MV_OK;
  1611. }
  1612. /*
  1613. * Write MRS
  1614. */
  1615. int ddr3_tip_write_mrs_cmd(u32 dev_num, u32 *cs_mask_arr, u32 cmd,
  1616. u32 data, u32 mask)
  1617. {
  1618. u32 if_id, reg;
  1619. struct hws_topology_map *tm = ddr3_get_topology_map();
  1620. reg = (cmd == MRS1_CMD) ? MR1_REG : MR2_REG;
  1621. CHECK_STATUS(ddr3_tip_if_write(dev_num, ACCESS_TYPE_MULTICAST,
  1622. PARAM_NOT_CARE, reg, data, mask));
  1623. for (if_id = 0; if_id <= MAX_INTERFACE_NUM - 1; if_id++) {
  1624. VALIDATE_ACTIVE(tm->if_act_mask, if_id);
  1625. CHECK_STATUS(ddr3_tip_if_write
  1626. (dev_num, ACCESS_TYPE_UNICAST, if_id,
  1627. SDRAM_OPERATION_REG,
  1628. (cs_mask_arr[if_id] << 8) | cmd, 0xf1f));
  1629. }
  1630. for (if_id = 0; if_id <= MAX_INTERFACE_NUM - 1; if_id++) {
  1631. VALIDATE_ACTIVE(tm->if_act_mask, if_id);
  1632. if (ddr3_tip_if_polling(dev_num, ACCESS_TYPE_UNICAST, if_id, 0,
  1633. 0x1f, SDRAM_OPERATION_REG,
  1634. MAX_POLLING_ITERATIONS) != MV_OK) {
  1635. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  1636. ("write_mrs_cmd: Poll cmd fail"));
  1637. }
  1638. }
  1639. return MV_OK;
  1640. }
  1641. /*
  1642. * Reset XSB Read FIFO
  1643. */
  1644. int ddr3_tip_reset_fifo_ptr(u32 dev_num)
  1645. {
  1646. u32 if_id = 0;
  1647. /* Configure PHY reset value to 0 in order to "clean" the FIFO */
  1648. CHECK_STATUS(ddr3_tip_if_write(dev_num, ACCESS_TYPE_MULTICAST,
  1649. if_id, 0x15c8, 0, 0xff000000));
  1650. /*
  1651. * Move PHY to RL mode (only in RL mode the PHY overrides FIFO values
  1652. * during FIFO reset)
  1653. */
  1654. CHECK_STATUS(ddr3_tip_if_write(dev_num, ACCESS_TYPE_MULTICAST,
  1655. if_id, TRAINING_SW_2_REG,
  1656. 0x1, 0x9));
  1657. /* In order that above configuration will influence the PHY */
  1658. CHECK_STATUS(ddr3_tip_if_write(dev_num, ACCESS_TYPE_MULTICAST,
  1659. if_id, 0x15b0,
  1660. 0x80000000, 0x80000000));
  1661. /* Reset read fifo assertion */
  1662. CHECK_STATUS(ddr3_tip_if_write(dev_num, ACCESS_TYPE_MULTICAST,
  1663. if_id, 0x1400, 0, 0x40000000));
  1664. /* Reset read fifo deassertion */
  1665. CHECK_STATUS(ddr3_tip_if_write(dev_num, ACCESS_TYPE_MULTICAST,
  1666. if_id, 0x1400,
  1667. 0x40000000, 0x40000000));
  1668. /* Move PHY back to functional mode */
  1669. CHECK_STATUS(ddr3_tip_if_write(dev_num, ACCESS_TYPE_MULTICAST,
  1670. if_id, TRAINING_SW_2_REG,
  1671. 0x8, 0x9));
  1672. /* Stop training machine */
  1673. CHECK_STATUS(ddr3_tip_if_write(dev_num, ACCESS_TYPE_MULTICAST,
  1674. if_id, 0x15b4, 0x10000, 0x10000));
  1675. return MV_OK;
  1676. }
  1677. /*
  1678. * Reset Phy registers
  1679. */
  1680. int ddr3_tip_ddr3_reset_phy_regs(u32 dev_num)
  1681. {
  1682. u32 if_id, phy_id, cs;
  1683. struct hws_topology_map *tm = ddr3_get_topology_map();
  1684. for (if_id = 0; if_id <= MAX_INTERFACE_NUM - 1; if_id++) {
  1685. VALIDATE_ACTIVE(tm->if_act_mask, if_id);
  1686. for (phy_id = 0; phy_id < tm->num_of_bus_per_interface;
  1687. phy_id++) {
  1688. VALIDATE_ACTIVE(tm->bus_act_mask, phy_id);
  1689. CHECK_STATUS(ddr3_tip_bus_write
  1690. (dev_num, ACCESS_TYPE_UNICAST,
  1691. if_id, ACCESS_TYPE_UNICAST,
  1692. phy_id, DDR_PHY_DATA,
  1693. WL_PHY_REG +
  1694. CS_REG_VALUE(effective_cs),
  1695. phy_reg0_val));
  1696. CHECK_STATUS(ddr3_tip_bus_write
  1697. (dev_num, ACCESS_TYPE_UNICAST, if_id,
  1698. ACCESS_TYPE_UNICAST, phy_id, DDR_PHY_DATA,
  1699. RL_PHY_REG + CS_REG_VALUE(effective_cs),
  1700. phy_reg2_val));
  1701. CHECK_STATUS(ddr3_tip_bus_write
  1702. (dev_num, ACCESS_TYPE_UNICAST, if_id,
  1703. ACCESS_TYPE_UNICAST, phy_id, DDR_PHY_DATA,
  1704. READ_CENTRALIZATION_PHY_REG +
  1705. CS_REG_VALUE(effective_cs), phy_reg3_val));
  1706. CHECK_STATUS(ddr3_tip_bus_write
  1707. (dev_num, ACCESS_TYPE_UNICAST, if_id,
  1708. ACCESS_TYPE_UNICAST, phy_id, DDR_PHY_DATA,
  1709. WRITE_CENTRALIZATION_PHY_REG +
  1710. CS_REG_VALUE(effective_cs), phy_reg3_val));
  1711. }
  1712. }
  1713. /* Set Receiver Calibration value */
  1714. for (cs = 0; cs < MAX_CS_NUM; cs++) {
  1715. /* PHY register 0xdb bits[5:0] - configure to 63 */
  1716. CHECK_STATUS(ddr3_tip_bus_write
  1717. (dev_num, ACCESS_TYPE_MULTICAST, PARAM_NOT_CARE,
  1718. ACCESS_TYPE_MULTICAST, PARAM_NOT_CARE,
  1719. DDR_PHY_DATA, CSN_IOB_VREF_REG(cs), 63));
  1720. }
  1721. return MV_OK;
  1722. }
  1723. /*
  1724. * Restore Dunit registers
  1725. */
  1726. int ddr3_tip_restore_dunit_regs(u32 dev_num)
  1727. {
  1728. u32 index_cnt;
  1729. CHECK_STATUS(ddr3_tip_if_write(dev_num, ACCESS_TYPE_MULTICAST,
  1730. PARAM_NOT_CARE, CALIB_MACHINE_CTRL_REG,
  1731. 0x1, 0x1));
  1732. CHECK_STATUS(ddr3_tip_if_write(dev_num, ACCESS_TYPE_MULTICAST,
  1733. PARAM_NOT_CARE, CALIB_MACHINE_CTRL_REG,
  1734. calibration_update_control << 3,
  1735. 0x3 << 3));
  1736. CHECK_STATUS(ddr3_tip_if_write(dev_num, ACCESS_TYPE_MULTICAST,
  1737. PARAM_NOT_CARE,
  1738. ODPG_WRITE_READ_MODE_ENABLE_REG,
  1739. 0xffff, MASK_ALL_BITS));
  1740. for (index_cnt = 0; index_cnt < ARRAY_SIZE(odpg_default_value);
  1741. index_cnt++) {
  1742. CHECK_STATUS(ddr3_tip_if_write
  1743. (dev_num, ACCESS_TYPE_MULTICAST, PARAM_NOT_CARE,
  1744. odpg_default_value[index_cnt].reg_addr,
  1745. odpg_default_value[index_cnt].reg_data,
  1746. odpg_default_value[index_cnt].reg_mask));
  1747. }
  1748. return MV_OK;
  1749. }
  1750. /*
  1751. * Auto tune main flow
  1752. */
  1753. static int ddr3_tip_ddr3_training_main_flow(u32 dev_num)
  1754. {
  1755. enum hws_ddr_freq freq = init_freq;
  1756. struct init_cntr_param init_cntr_prm;
  1757. int ret = MV_OK;
  1758. u32 if_id;
  1759. u32 max_cs = hws_ddr3_tip_max_cs_get();
  1760. struct hws_topology_map *tm = ddr3_get_topology_map();
  1761. #ifndef EXCLUDE_SWITCH_DEBUG
  1762. if (debug_training == DEBUG_LEVEL_TRACE) {
  1763. CHECK_STATUS(print_device_info((u8)dev_num));
  1764. }
  1765. #endif
  1766. for (effective_cs = 0; effective_cs < max_cs; effective_cs++) {
  1767. CHECK_STATUS(ddr3_tip_ddr3_reset_phy_regs(dev_num));
  1768. }
  1769. /* Set to 0 after each loop to avoid illegal value may be used */
  1770. effective_cs = 0;
  1771. freq = init_freq;
  1772. if (is_pll_before_init != 0) {
  1773. for (if_id = 0; if_id < MAX_INTERFACE_NUM; if_id++) {
  1774. VALIDATE_ACTIVE(tm->if_act_mask, if_id);
  1775. config_func_info[dev_num].tip_set_freq_divider_func(
  1776. (u8)dev_num, if_id, freq);
  1777. }
  1778. }
  1779. if (is_adll_calib_before_init != 0) {
  1780. DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
  1781. ("with adll calib before init\n"));
  1782. adll_calibration(dev_num, ACCESS_TYPE_MULTICAST, 0, freq);
  1783. }
  1784. if (is_reg_dump != 0) {
  1785. DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
  1786. ("Dump before init controller\n"));
  1787. ddr3_tip_reg_dump(dev_num);
  1788. }
  1789. if (mask_tune_func & INIT_CONTROLLER_MASK_BIT) {
  1790. training_stage = INIT_CONTROLLER;
  1791. DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
  1792. ("INIT_CONTROLLER_MASK_BIT\n"));
  1793. init_cntr_prm.do_mrs_phy = 1;
  1794. init_cntr_prm.is_ctrl64_bit = 0;
  1795. init_cntr_prm.init_phy = 1;
  1796. init_cntr_prm.msys_init = 0;
  1797. ret = hws_ddr3_tip_init_controller(dev_num, &init_cntr_prm);
  1798. if (is_reg_dump != 0)
  1799. ddr3_tip_reg_dump(dev_num);
  1800. if (ret != MV_OK) {
  1801. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  1802. ("hws_ddr3_tip_init_controller failure\n"));
  1803. if (debug_mode == 0)
  1804. return MV_FAIL;
  1805. }
  1806. }
  1807. #ifdef STATIC_ALGO_SUPPORT
  1808. if (mask_tune_func & STATIC_LEVELING_MASK_BIT) {
  1809. training_stage = STATIC_LEVELING;
  1810. DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
  1811. ("STATIC_LEVELING_MASK_BIT\n"));
  1812. ret = ddr3_tip_run_static_alg(dev_num, freq);
  1813. if (is_reg_dump != 0)
  1814. ddr3_tip_reg_dump(dev_num);
  1815. if (ret != MV_OK) {
  1816. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  1817. ("ddr3_tip_run_static_alg failure\n"));
  1818. if (debug_mode == 0)
  1819. return MV_FAIL;
  1820. }
  1821. }
  1822. #endif
  1823. if (mask_tune_func & SET_LOW_FREQ_MASK_BIT) {
  1824. training_stage = SET_LOW_FREQ;
  1825. DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
  1826. ("SET_LOW_FREQ_MASK_BIT %d\n",
  1827. freq_val[low_freq]));
  1828. ret = ddr3_tip_freq_set(dev_num, ACCESS_TYPE_MULTICAST,
  1829. PARAM_NOT_CARE, low_freq);
  1830. if (is_reg_dump != 0)
  1831. ddr3_tip_reg_dump(dev_num);
  1832. if (ret != MV_OK) {
  1833. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  1834. ("ddr3_tip_freq_set failure\n"));
  1835. if (debug_mode == 0)
  1836. return MV_FAIL;
  1837. }
  1838. }
  1839. for (effective_cs = 0; effective_cs < max_cs; effective_cs++) {
  1840. if (mask_tune_func & LOAD_PATTERN_MASK_BIT) {
  1841. training_stage = LOAD_PATTERN;
  1842. DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
  1843. ("LOAD_PATTERN_MASK_BIT #%d\n",
  1844. effective_cs));
  1845. ret = ddr3_tip_load_all_pattern_to_mem(dev_num);
  1846. if (is_reg_dump != 0)
  1847. ddr3_tip_reg_dump(dev_num);
  1848. if (ret != MV_OK) {
  1849. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  1850. ("ddr3_tip_load_all_pattern_to_mem failure CS #%d\n",
  1851. effective_cs));
  1852. if (debug_mode == 0)
  1853. return MV_FAIL;
  1854. }
  1855. }
  1856. }
  1857. /* Set to 0 after each loop to avoid illegal value may be used */
  1858. effective_cs = 0;
  1859. if (mask_tune_func & SET_MEDIUM_FREQ_MASK_BIT) {
  1860. training_stage = SET_MEDIUM_FREQ;
  1861. DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
  1862. ("SET_MEDIUM_FREQ_MASK_BIT %d\n",
  1863. freq_val[medium_freq]));
  1864. ret =
  1865. ddr3_tip_freq_set(dev_num, ACCESS_TYPE_MULTICAST,
  1866. PARAM_NOT_CARE, medium_freq);
  1867. if (is_reg_dump != 0)
  1868. ddr3_tip_reg_dump(dev_num);
  1869. if (ret != MV_OK) {
  1870. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  1871. ("ddr3_tip_freq_set failure\n"));
  1872. if (debug_mode == 0)
  1873. return MV_FAIL;
  1874. }
  1875. }
  1876. if (mask_tune_func & WRITE_LEVELING_MASK_BIT) {
  1877. training_stage = WRITE_LEVELING;
  1878. DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
  1879. ("WRITE_LEVELING_MASK_BIT\n"));
  1880. if ((rl_mid_freq_wa == 0) || (freq_val[medium_freq] == 533)) {
  1881. ret = ddr3_tip_dynamic_write_leveling(dev_num);
  1882. } else {
  1883. /* Use old WL */
  1884. ret = ddr3_tip_legacy_dynamic_write_leveling(dev_num);
  1885. }
  1886. if (is_reg_dump != 0)
  1887. ddr3_tip_reg_dump(dev_num);
  1888. if (ret != MV_OK) {
  1889. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  1890. ("ddr3_tip_dynamic_write_leveling failure\n"));
  1891. if (debug_mode == 0)
  1892. return MV_FAIL;
  1893. }
  1894. }
  1895. for (effective_cs = 0; effective_cs < max_cs; effective_cs++) {
  1896. if (mask_tune_func & LOAD_PATTERN_2_MASK_BIT) {
  1897. training_stage = LOAD_PATTERN_2;
  1898. DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
  1899. ("LOAD_PATTERN_2_MASK_BIT CS #%d\n",
  1900. effective_cs));
  1901. ret = ddr3_tip_load_all_pattern_to_mem(dev_num);
  1902. if (is_reg_dump != 0)
  1903. ddr3_tip_reg_dump(dev_num);
  1904. if (ret != MV_OK) {
  1905. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  1906. ("ddr3_tip_load_all_pattern_to_mem failure CS #%d\n",
  1907. effective_cs));
  1908. if (debug_mode == 0)
  1909. return MV_FAIL;
  1910. }
  1911. }
  1912. }
  1913. /* Set to 0 after each loop to avoid illegal value may be used */
  1914. effective_cs = 0;
  1915. if (mask_tune_func & READ_LEVELING_MASK_BIT) {
  1916. training_stage = READ_LEVELING;
  1917. DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
  1918. ("READ_LEVELING_MASK_BIT\n"));
  1919. if ((rl_mid_freq_wa == 0) || (freq_val[medium_freq] == 533)) {
  1920. ret = ddr3_tip_dynamic_read_leveling(dev_num, medium_freq);
  1921. } else {
  1922. /* Use old RL */
  1923. ret = ddr3_tip_legacy_dynamic_read_leveling(dev_num);
  1924. }
  1925. if (is_reg_dump != 0)
  1926. ddr3_tip_reg_dump(dev_num);
  1927. if (ret != MV_OK) {
  1928. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  1929. ("ddr3_tip_dynamic_read_leveling failure\n"));
  1930. if (debug_mode == 0)
  1931. return MV_FAIL;
  1932. }
  1933. }
  1934. if (mask_tune_func & WRITE_LEVELING_SUPP_MASK_BIT) {
  1935. training_stage = WRITE_LEVELING_SUPP;
  1936. DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
  1937. ("WRITE_LEVELING_SUPP_MASK_BIT\n"));
  1938. ret = ddr3_tip_dynamic_write_leveling_supp(dev_num);
  1939. if (is_reg_dump != 0)
  1940. ddr3_tip_reg_dump(dev_num);
  1941. if (ret != MV_OK) {
  1942. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  1943. ("ddr3_tip_dynamic_write_leveling_supp failure\n"));
  1944. if (debug_mode == 0)
  1945. return MV_FAIL;
  1946. }
  1947. }
  1948. for (effective_cs = 0; effective_cs < max_cs; effective_cs++) {
  1949. if (mask_tune_func & PBS_RX_MASK_BIT) {
  1950. training_stage = PBS_RX;
  1951. DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
  1952. ("PBS_RX_MASK_BIT CS #%d\n",
  1953. effective_cs));
  1954. ret = ddr3_tip_pbs_rx(dev_num);
  1955. if (is_reg_dump != 0)
  1956. ddr3_tip_reg_dump(dev_num);
  1957. if (ret != MV_OK) {
  1958. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  1959. ("ddr3_tip_pbs_rx failure CS #%d\n",
  1960. effective_cs));
  1961. if (debug_mode == 0)
  1962. return MV_FAIL;
  1963. }
  1964. }
  1965. }
  1966. for (effective_cs = 0; effective_cs < max_cs; effective_cs++) {
  1967. if (mask_tune_func & PBS_TX_MASK_BIT) {
  1968. training_stage = PBS_TX;
  1969. DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
  1970. ("PBS_TX_MASK_BIT CS #%d\n",
  1971. effective_cs));
  1972. ret = ddr3_tip_pbs_tx(dev_num);
  1973. if (is_reg_dump != 0)
  1974. ddr3_tip_reg_dump(dev_num);
  1975. if (ret != MV_OK) {
  1976. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  1977. ("ddr3_tip_pbs_tx failure CS #%d\n",
  1978. effective_cs));
  1979. if (debug_mode == 0)
  1980. return MV_FAIL;
  1981. }
  1982. }
  1983. }
  1984. /* Set to 0 after each loop to avoid illegal value may be used */
  1985. effective_cs = 0;
  1986. if (mask_tune_func & SET_TARGET_FREQ_MASK_BIT) {
  1987. training_stage = SET_TARGET_FREQ;
  1988. DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
  1989. ("SET_TARGET_FREQ_MASK_BIT %d\n",
  1990. freq_val[tm->
  1991. interface_params[first_active_if].
  1992. memory_freq]));
  1993. ret = ddr3_tip_freq_set(dev_num, ACCESS_TYPE_MULTICAST,
  1994. PARAM_NOT_CARE,
  1995. tm->interface_params[first_active_if].
  1996. memory_freq);
  1997. if (is_reg_dump != 0)
  1998. ddr3_tip_reg_dump(dev_num);
  1999. if (ret != MV_OK) {
  2000. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  2001. ("ddr3_tip_freq_set failure\n"));
  2002. if (debug_mode == 0)
  2003. return MV_FAIL;
  2004. }
  2005. }
  2006. if (mask_tune_func & WRITE_LEVELING_TF_MASK_BIT) {
  2007. training_stage = WRITE_LEVELING_TF;
  2008. DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
  2009. ("WRITE_LEVELING_TF_MASK_BIT\n"));
  2010. ret = ddr3_tip_dynamic_write_leveling(dev_num);
  2011. if (is_reg_dump != 0)
  2012. ddr3_tip_reg_dump(dev_num);
  2013. if (ret != MV_OK) {
  2014. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  2015. ("ddr3_tip_dynamic_write_leveling TF failure\n"));
  2016. if (debug_mode == 0)
  2017. return MV_FAIL;
  2018. }
  2019. }
  2020. if (mask_tune_func & LOAD_PATTERN_HIGH_MASK_BIT) {
  2021. training_stage = LOAD_PATTERN_HIGH;
  2022. DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO, ("LOAD_PATTERN_HIGH\n"));
  2023. ret = ddr3_tip_load_all_pattern_to_mem(dev_num);
  2024. if (is_reg_dump != 0)
  2025. ddr3_tip_reg_dump(dev_num);
  2026. if (ret != MV_OK) {
  2027. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  2028. ("ddr3_tip_load_all_pattern_to_mem failure\n"));
  2029. if (debug_mode == 0)
  2030. return MV_FAIL;
  2031. }
  2032. }
  2033. if (mask_tune_func & READ_LEVELING_TF_MASK_BIT) {
  2034. training_stage = READ_LEVELING_TF;
  2035. DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
  2036. ("READ_LEVELING_TF_MASK_BIT\n"));
  2037. ret = ddr3_tip_dynamic_read_leveling(dev_num, tm->
  2038. interface_params[first_active_if].
  2039. memory_freq);
  2040. if (is_reg_dump != 0)
  2041. ddr3_tip_reg_dump(dev_num);
  2042. if (ret != MV_OK) {
  2043. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  2044. ("ddr3_tip_dynamic_read_leveling TF failure\n"));
  2045. if (debug_mode == 0)
  2046. return MV_FAIL;
  2047. }
  2048. }
  2049. if (mask_tune_func & DM_PBS_TX_MASK_BIT) {
  2050. DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO, ("DM_PBS_TX_MASK_BIT\n"));
  2051. }
  2052. for (effective_cs = 0; effective_cs < max_cs; effective_cs++) {
  2053. if (mask_tune_func & VREF_CALIBRATION_MASK_BIT) {
  2054. training_stage = VREF_CALIBRATION;
  2055. DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO, ("VREF\n"));
  2056. ret = ddr3_tip_vref(dev_num);
  2057. if (is_reg_dump != 0) {
  2058. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  2059. ("VREF Dump\n"));
  2060. ddr3_tip_reg_dump(dev_num);
  2061. }
  2062. if (ret != MV_OK) {
  2063. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  2064. ("ddr3_tip_vref failure\n"));
  2065. if (debug_mode == 0)
  2066. return MV_FAIL;
  2067. }
  2068. }
  2069. }
  2070. /* Set to 0 after each loop to avoid illegal value may be used */
  2071. effective_cs = 0;
  2072. for (effective_cs = 0; effective_cs < max_cs; effective_cs++) {
  2073. if (mask_tune_func & CENTRALIZATION_RX_MASK_BIT) {
  2074. training_stage = CENTRALIZATION_RX;
  2075. DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
  2076. ("CENTRALIZATION_RX_MASK_BIT CS #%d\n",
  2077. effective_cs));
  2078. ret = ddr3_tip_centralization_rx(dev_num);
  2079. if (is_reg_dump != 0)
  2080. ddr3_tip_reg_dump(dev_num);
  2081. if (ret != MV_OK) {
  2082. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  2083. ("ddr3_tip_centralization_rx failure CS #%d\n",
  2084. effective_cs));
  2085. if (debug_mode == 0)
  2086. return MV_FAIL;
  2087. }
  2088. }
  2089. }
  2090. /* Set to 0 after each loop to avoid illegal value may be used */
  2091. effective_cs = 0;
  2092. for (effective_cs = 0; effective_cs < max_cs; effective_cs++) {
  2093. if (mask_tune_func & WRITE_LEVELING_SUPP_TF_MASK_BIT) {
  2094. training_stage = WRITE_LEVELING_SUPP_TF;
  2095. DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
  2096. ("WRITE_LEVELING_SUPP_TF_MASK_BIT CS #%d\n",
  2097. effective_cs));
  2098. ret = ddr3_tip_dynamic_write_leveling_supp(dev_num);
  2099. if (is_reg_dump != 0)
  2100. ddr3_tip_reg_dump(dev_num);
  2101. if (ret != MV_OK) {
  2102. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  2103. ("ddr3_tip_dynamic_write_leveling_supp TF failure CS #%d\n",
  2104. effective_cs));
  2105. if (debug_mode == 0)
  2106. return MV_FAIL;
  2107. }
  2108. }
  2109. }
  2110. /* Set to 0 after each loop to avoid illegal value may be used */
  2111. effective_cs = 0;
  2112. for (effective_cs = 0; effective_cs < max_cs; effective_cs++) {
  2113. if (mask_tune_func & CENTRALIZATION_TX_MASK_BIT) {
  2114. training_stage = CENTRALIZATION_TX;
  2115. DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
  2116. ("CENTRALIZATION_TX_MASK_BIT CS #%d\n",
  2117. effective_cs));
  2118. ret = ddr3_tip_centralization_tx(dev_num);
  2119. if (is_reg_dump != 0)
  2120. ddr3_tip_reg_dump(dev_num);
  2121. if (ret != MV_OK) {
  2122. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  2123. ("ddr3_tip_centralization_tx failure CS #%d\n",
  2124. effective_cs));
  2125. if (debug_mode == 0)
  2126. return MV_FAIL;
  2127. }
  2128. }
  2129. }
  2130. /* Set to 0 after each loop to avoid illegal value may be used */
  2131. effective_cs = 0;
  2132. DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO, ("restore registers to default\n"));
  2133. /* restore register values */
  2134. CHECK_STATUS(ddr3_tip_restore_dunit_regs(dev_num));
  2135. if (is_reg_dump != 0)
  2136. ddr3_tip_reg_dump(dev_num);
  2137. return MV_OK;
  2138. }
  2139. /*
  2140. * DDR3 Dynamic training flow
  2141. */
  2142. static int ddr3_tip_ddr3_auto_tune(u32 dev_num)
  2143. {
  2144. u32 if_id, stage, ret;
  2145. int is_if_fail = 0, is_auto_tune_fail = 0;
  2146. training_stage = INIT_CONTROLLER;
  2147. for (if_id = 0; if_id <= MAX_INTERFACE_NUM - 1; if_id++) {
  2148. for (stage = 0; stage < MAX_STAGE_LIMIT; stage++)
  2149. training_result[stage][if_id] = NO_TEST_DONE;
  2150. }
  2151. ret = ddr3_tip_ddr3_training_main_flow(dev_num);
  2152. /* activate XSB test */
  2153. if (xsb_validate_type != 0) {
  2154. run_xsb_test(dev_num, xsb_validation_base_address, 1, 1,
  2155. 0x1024);
  2156. }
  2157. if (is_reg_dump != 0)
  2158. ddr3_tip_reg_dump(dev_num);
  2159. /* print log */
  2160. CHECK_STATUS(ddr3_tip_print_log(dev_num, window_mem_addr));
  2161. if (ret != MV_OK) {
  2162. CHECK_STATUS(ddr3_tip_print_stability_log(dev_num));
  2163. }
  2164. for (if_id = 0; if_id <= MAX_INTERFACE_NUM - 1; if_id++) {
  2165. is_if_fail = 0;
  2166. for (stage = 0; stage < MAX_STAGE_LIMIT; stage++) {
  2167. if (training_result[stage][if_id] == TEST_FAILED)
  2168. is_if_fail = 1;
  2169. }
  2170. if (is_if_fail == 1) {
  2171. is_auto_tune_fail = 1;
  2172. DEBUG_TRAINING_IP(DEBUG_LEVEL_INFO,
  2173. ("Auto Tune failed for IF %d\n",
  2174. if_id));
  2175. }
  2176. }
  2177. if ((ret == MV_FAIL) || (is_auto_tune_fail == 1))
  2178. return MV_FAIL;
  2179. else
  2180. return MV_OK;
  2181. }
  2182. /*
  2183. * Enable init sequence
  2184. */
  2185. int ddr3_tip_enable_init_sequence(u32 dev_num)
  2186. {
  2187. int is_fail = 0;
  2188. u32 if_id = 0, mem_mask = 0, bus_index = 0;
  2189. struct hws_topology_map *tm = ddr3_get_topology_map();
  2190. /* Enable init sequence */
  2191. CHECK_STATUS(ddr3_tip_if_write(dev_num, ACCESS_TYPE_MULTICAST, 0,
  2192. SDRAM_INIT_CONTROL_REG, 0x1, 0x1));
  2193. for (if_id = 0; if_id <= MAX_INTERFACE_NUM - 1; if_id++) {
  2194. VALIDATE_ACTIVE(tm->if_act_mask, if_id);
  2195. if (ddr3_tip_if_polling
  2196. (dev_num, ACCESS_TYPE_UNICAST, if_id, 0, 0x1,
  2197. SDRAM_INIT_CONTROL_REG,
  2198. MAX_POLLING_ITERATIONS) != MV_OK) {
  2199. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  2200. ("polling failed IF %d\n",
  2201. if_id));
  2202. is_fail = 1;
  2203. continue;
  2204. }
  2205. mem_mask = 0;
  2206. for (bus_index = 0; bus_index < GET_TOPOLOGY_NUM_OF_BUSES();
  2207. bus_index++) {
  2208. VALIDATE_ACTIVE(tm->bus_act_mask, bus_index);
  2209. mem_mask |=
  2210. tm->interface_params[if_id].
  2211. as_bus_params[bus_index].mirror_enable_bitmask;
  2212. }
  2213. if (mem_mask != 0) {
  2214. /* Disable Multi CS */
  2215. CHECK_STATUS(ddr3_tip_if_write
  2216. (dev_num, ACCESS_TYPE_MULTICAST,
  2217. if_id, CS_ENABLE_REG, 1 << 3,
  2218. 1 << 3));
  2219. }
  2220. }
  2221. return (is_fail == 0) ? MV_OK : MV_FAIL;
  2222. }
  2223. int ddr3_tip_register_dq_table(u32 dev_num, u32 *table)
  2224. {
  2225. dq_map_table = table;
  2226. return MV_OK;
  2227. }
  2228. /*
  2229. * Check if pup search is locked
  2230. */
  2231. int ddr3_tip_is_pup_lock(u32 *pup_buf, enum hws_training_result read_mode)
  2232. {
  2233. u32 bit_start = 0, bit_end = 0, bit_id;
  2234. if (read_mode == RESULT_PER_BIT) {
  2235. bit_start = 0;
  2236. bit_end = BUS_WIDTH_IN_BITS - 1;
  2237. } else {
  2238. bit_start = 0;
  2239. bit_end = 0;
  2240. }
  2241. for (bit_id = bit_start; bit_id <= bit_end; bit_id++) {
  2242. if (GET_LOCK_RESULT(pup_buf[bit_id]) == 0)
  2243. return 0;
  2244. }
  2245. return 1;
  2246. }
  2247. /*
  2248. * Get minimum buffer value
  2249. */
  2250. u8 ddr3_tip_get_buf_min(u8 *buf_ptr)
  2251. {
  2252. u8 min_val = 0xff;
  2253. u8 cnt = 0;
  2254. for (cnt = 0; cnt < BUS_WIDTH_IN_BITS; cnt++) {
  2255. if (buf_ptr[cnt] < min_val)
  2256. min_val = buf_ptr[cnt];
  2257. }
  2258. return min_val;
  2259. }
  2260. /*
  2261. * Get maximum buffer value
  2262. */
  2263. u8 ddr3_tip_get_buf_max(u8 *buf_ptr)
  2264. {
  2265. u8 max_val = 0;
  2266. u8 cnt = 0;
  2267. for (cnt = 0; cnt < BUS_WIDTH_IN_BITS; cnt++) {
  2268. if (buf_ptr[cnt] > max_val)
  2269. max_val = buf_ptr[cnt];
  2270. }
  2271. return max_val;
  2272. }
  2273. /*
  2274. * The following functions return memory parameters:
  2275. * bus and device width, device size
  2276. */
  2277. u32 hws_ddr3_get_bus_width(void)
  2278. {
  2279. struct hws_topology_map *tm = ddr3_get_topology_map();
  2280. return (DDR3_IS_16BIT_DRAM_MODE(tm->bus_act_mask) ==
  2281. 1) ? 16 : 32;
  2282. }
  2283. u32 hws_ddr3_get_device_width(u32 if_id)
  2284. {
  2285. struct hws_topology_map *tm = ddr3_get_topology_map();
  2286. return (tm->interface_params[if_id].bus_width ==
  2287. BUS_WIDTH_8) ? 8 : 16;
  2288. }
  2289. u32 hws_ddr3_get_device_size(u32 if_id)
  2290. {
  2291. struct hws_topology_map *tm = ddr3_get_topology_map();
  2292. if (tm->interface_params[if_id].memory_size >=
  2293. MEM_SIZE_LAST) {
  2294. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  2295. ("Error: Wrong device size of Cs: %d",
  2296. tm->interface_params[if_id].memory_size));
  2297. return 0;
  2298. } else {
  2299. return 1 << tm->interface_params[if_id].memory_size;
  2300. }
  2301. }
  2302. int hws_ddr3_calc_mem_cs_size(u32 if_id, u32 cs, u32 *cs_size)
  2303. {
  2304. u32 cs_mem_size, dev_size;
  2305. dev_size = hws_ddr3_get_device_size(if_id);
  2306. if (dev_size != 0) {
  2307. cs_mem_size = ((hws_ddr3_get_bus_width() /
  2308. hws_ddr3_get_device_width(if_id)) * dev_size);
  2309. /* the calculated result in Gbytex16 to avoid float using */
  2310. if (cs_mem_size == 2) {
  2311. *cs_size = _128M;
  2312. } else if (cs_mem_size == 4) {
  2313. *cs_size = _256M;
  2314. } else if (cs_mem_size == 8) {
  2315. *cs_size = _512M;
  2316. } else if (cs_mem_size == 16) {
  2317. *cs_size = _1G;
  2318. } else if (cs_mem_size == 32) {
  2319. *cs_size = _2G;
  2320. } else {
  2321. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  2322. ("Error: Wrong Memory size of Cs: %d", cs));
  2323. return MV_FAIL;
  2324. }
  2325. return MV_OK;
  2326. } else {
  2327. return MV_FAIL;
  2328. }
  2329. }
  2330. int hws_ddr3_cs_base_adr_calc(u32 if_id, u32 cs, u32 *cs_base_addr)
  2331. {
  2332. u32 cs_mem_size = 0;
  2333. #ifdef DEVICE_MAX_DRAM_ADDRESS_SIZE
  2334. u32 physical_mem_size;
  2335. u32 max_mem_size = DEVICE_MAX_DRAM_ADDRESS_SIZE;
  2336. #endif
  2337. if (hws_ddr3_calc_mem_cs_size(if_id, cs, &cs_mem_size) != MV_OK)
  2338. return MV_FAIL;
  2339. #ifdef DEVICE_MAX_DRAM_ADDRESS_SIZE
  2340. struct hws_topology_map *tm = ddr3_get_topology_map();
  2341. /*
  2342. * if number of address pins doesn't allow to use max mem size that
  2343. * is defined in topology mem size is defined by
  2344. * DEVICE_MAX_DRAM_ADDRESS_SIZE
  2345. */
  2346. physical_mem_size =
  2347. mv_hwsmem_size[tm->interface_params[0].memory_size];
  2348. if (hws_ddr3_get_device_width(cs) == 16) {
  2349. /*
  2350. * 16bit mem device can be twice more - no need in less
  2351. * significant pin
  2352. */
  2353. max_mem_size = DEVICE_MAX_DRAM_ADDRESS_SIZE * 2;
  2354. }
  2355. if (physical_mem_size > max_mem_size) {
  2356. cs_mem_size = max_mem_size *
  2357. (hws_ddr3_get_bus_width() /
  2358. hws_ddr3_get_device_width(if_id));
  2359. DEBUG_TRAINING_IP(DEBUG_LEVEL_ERROR,
  2360. ("Updated Physical Mem size is from 0x%x to %x\n",
  2361. physical_mem_size,
  2362. DEVICE_MAX_DRAM_ADDRESS_SIZE));
  2363. }
  2364. #endif
  2365. /* calculate CS base addr */
  2366. *cs_base_addr = ((cs_mem_size) * cs) & 0xffff0000;
  2367. return MV_OK;
  2368. }