ddr3_training.c 75 KB

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