ddr3_training.c 75 KB

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