stb0899_algo.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  1. /*
  2. STB0899 Multistandard Frontend driver
  3. Copyright (C) Manu Abraham (abraham.manu@gmail.com)
  4. Copyright (C) ST Microelectronics
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #include "stb0899_drv.h"
  18. #include "stb0899_priv.h"
  19. #include "stb0899_reg.h"
  20. /*
  21. * BinaryFloatDiv
  22. * float division with integer
  23. */
  24. static long BinaryFloatDiv(long n1, long n2, int precision)
  25. {
  26. int i = 0;
  27. long result = 0;
  28. while (i <= precision) {
  29. if (n1 < n2) {
  30. result *= 2;
  31. n1 *= 2;
  32. } else {
  33. result = result * 2 + 1;
  34. n1 = (n1 - n2) * 2;
  35. }
  36. i++;
  37. }
  38. return result;
  39. }
  40. /*
  41. * stb0899_calc_srate
  42. * Compute symbol rate
  43. */
  44. static u32 stb0899_calc_srate(u32 master_clk, u8 *sfr)
  45. {
  46. u32 tmp, tmp2, mclk;
  47. mclk = master_clk / 4096L; /* MasterClock * 10 / 2^20 */
  48. tmp = (((u32) sfr[0] << 12) + ((u32) sfr[1] << 4)) / 16;
  49. tmp *= mclk;
  50. tmp /= 16;
  51. tmp2 = ((u32) sfr[2] * mclk) / 256;
  52. tmp += tmp2;
  53. return tmp;
  54. }
  55. /*
  56. * stb0899_get_srate
  57. * Get the current symbol rate
  58. */
  59. u32 stb0899_get_srate(struct stb0899_state *state)
  60. {
  61. struct stb0899_internal *internal = &state->internal;
  62. u8 sfr[4];
  63. stb0899_read_regs(state, STB0899_SFRH, sfr, 3);
  64. return stb0899_calc_srate(internal->master_clk, sfr);
  65. }
  66. /*
  67. * stb0899_set_srate
  68. * Set symbol frequency
  69. * MasterClock: master clock frequency (hz)
  70. * SymbolRate: symbol rate (bauds)
  71. * return symbol frequency
  72. */
  73. static u32 stb0899_set_srate(struct stb0899_state *state, u32 master_clk, u32 srate)
  74. {
  75. u32 tmp, tmp_up, srate_up;
  76. u8 sfr_up[3], sfr[3];
  77. // srate_up = srate;
  78. dprintk(state->verbose, FE_DEBUG, 1, "-->");
  79. /*
  80. * in order to have the maximum precision, the symbol rate entered into
  81. * the chip is computed as the closest value of the "true value".
  82. * In this purpose, the symbol rate value is rounded (1 is added on the bit
  83. * below the LSB )
  84. */
  85. // srate_up += (srate_up * 3) / 100;
  86. tmp = BinaryFloatDiv(srate, master_clk, 20);
  87. // tmp_up = BinaryFloatDiv(srate_up, master_clk, 20);
  88. // sfr_up[0] = (tmp_up >> 12) & 0xff;
  89. // sfr_up[1] = (tmp_up >> 4) & 0xff;
  90. // sfr_up[2] = tmp_up & 0x0f;
  91. sfr[0] = (tmp >> 12) & 0xff;
  92. sfr[1] = (tmp >> 4) & 0xff;
  93. sfr[2] = (tmp << 4) & 0xf0;
  94. // stb0899_write_regs(state, STB0899_SFRUPH, sfr_up, 3);
  95. stb0899_write_regs(state, STB0899_SFRH, sfr, 3);
  96. return srate;
  97. }
  98. /*
  99. * stb0899_calc_loop_time
  100. * Compute the amount of time needed by the timing loop to lock
  101. * SymbolRate: Symbol rate
  102. * return: timing loop time constant (ms)
  103. */
  104. static long stb0899_calc_loop_time(long srate)
  105. {
  106. if (srate > 0)
  107. return (100000 / (srate / 1000));
  108. else
  109. return 0;
  110. }
  111. /*
  112. * stb0899_calc_derot_time
  113. * Compute the amount of time needed by the derotator to lock
  114. * SymbolRate: Symbol rate
  115. * return: derotator time constant (ms)
  116. */
  117. static long stb0899_calc_derot_time(long srate)
  118. {
  119. if (srate > 0)
  120. return (100000 / (srate / 1000));
  121. else
  122. return 0;
  123. }
  124. /*
  125. * stb0899_carr_width
  126. * Compute the width of the carrier
  127. * return: width of carrier (kHz or Mhz)
  128. */
  129. long stb0899_carr_width(struct stb0899_state *state)
  130. {
  131. struct stb0899_internal *internal = &state->internal;
  132. return (internal->srate + (internal->srate * internal->rolloff) / 100);
  133. }
  134. /*
  135. * stb0899_first_subrange
  136. * Compute the first subrange of the search
  137. */
  138. static void stb0899_first_subrange(struct stb0899_state *state)
  139. {
  140. struct stb0899_internal *internal = &state->internal;
  141. struct stb0899_params *params = &state->params;
  142. struct stb0899_config *config = state->config;
  143. int range = 0;
  144. u32 bandwidth = 0;
  145. if (config->tuner_get_bandwidth) {
  146. config->tuner_get_bandwidth(&state->frontend, &bandwidth);
  147. range = bandwidth - stb0899_carr_width(state) / 2;
  148. }
  149. if (range > 0)
  150. internal->sub_range = MIN(internal->srch_range, range);
  151. else
  152. internal->sub_range = 0;
  153. internal->freq = params->freq;
  154. internal->tuner_offst = 0L;
  155. internal->sub_dir = 1;
  156. }
  157. /*
  158. * stb0899_check_tmg
  159. * check for timing lock
  160. * internal.Ttiming: time to wait for loop lock
  161. */
  162. static enum stb0899_status stb0899_check_tmg(struct stb0899_state *state)
  163. {
  164. struct stb0899_internal *internal = &state->internal;
  165. int lock;
  166. u8 reg;
  167. s8 timing;
  168. msleep(internal->t_timing);
  169. reg = stb0899_read_reg(state, STB0899_RTF);
  170. STB0899_SETFIELD_VAL(RTF_TIMING_LOOP_FREQ, reg, 0xf2);
  171. stb0899_write_reg(state, STB0899_RTF, reg);
  172. reg = stb0899_read_reg(state, STB0899_TLIR);
  173. lock = STB0899_GETFIELD(TLIR_TMG_LOCK_IND, reg);
  174. timing = stb0899_read_reg(state, STB0899_RTF);
  175. if (lock >= 42) {
  176. if ((lock > 48) && (ABS(timing) >= 110)) {
  177. internal->status = ANALOGCARRIER;
  178. dprintk(state->verbose, FE_DEBUG, 1, "-->ANALOG Carrier !");
  179. } else {
  180. internal->status = TIMINGOK;
  181. dprintk(state->verbose, FE_DEBUG, 1, "------->TIMING OK !");
  182. }
  183. } else {
  184. internal->status = NOTIMING;
  185. dprintk(state->verbose, FE_DEBUG, 1, "-->NO TIMING !");
  186. }
  187. return internal->status;
  188. }
  189. /*
  190. * stb0899_search_tmg
  191. * perform a fs/2 zig-zag to find timing
  192. */
  193. static enum stb0899_status stb0899_search_tmg(struct stb0899_state *state)
  194. {
  195. struct stb0899_internal *internal = &state->internal;
  196. struct stb0899_params *params = &state->params;
  197. short int derot_step, derot_freq = 0, derot_limit, next_loop = 3;
  198. int index = 0;
  199. u8 cfr[2];
  200. internal->status = NOTIMING;
  201. /* timing loop computation & symbol rate optimisation */
  202. derot_limit = (internal->sub_range / 2L) / internal->mclk;
  203. derot_step = (params->srate / 2L) / internal->mclk;
  204. while ((stb0899_check_tmg(state) != TIMINGOK) && next_loop) {
  205. index++;
  206. derot_freq += index * internal->direction * derot_step; /* next derot zig zag position */
  207. if (ABS(derot_freq) > derot_limit)
  208. next_loop--;
  209. if (next_loop) {
  210. STB0899_SETFIELD_VAL(CFRM, cfr[0], MSB(state->config->inversion * derot_freq));
  211. STB0899_SETFIELD_VAL(CFRL, cfr[1], LSB(state->config->inversion * derot_freq));
  212. stb0899_write_regs(state, STB0899_CFRM, cfr, 2); /* derotator frequency */
  213. }
  214. internal->direction = -internal->direction; /* Change zigzag direction */
  215. }
  216. if (internal->status == TIMINGOK) {
  217. stb0899_read_regs(state, STB0899_CFRM, cfr, 2); /* get derotator frequency */
  218. internal->derot_freq = state->config->inversion * MAKEWORD16(cfr[0], cfr[1]);
  219. dprintk(state->verbose, FE_DEBUG, 1, "------->TIMING OK ! Derot Freq = %d", internal->derot_freq);
  220. }
  221. return internal->status;
  222. }
  223. /*
  224. * stb0899_check_carrier
  225. * Check for carrier found
  226. */
  227. static enum stb0899_status stb0899_check_carrier(struct stb0899_state *state)
  228. {
  229. struct stb0899_internal *internal = &state->internal;
  230. u8 reg;
  231. msleep(internal->t_derot); /* wait for derotator ok */
  232. reg = stb0899_read_reg(state, STB0899_CFD);
  233. STB0899_SETFIELD_VAL(CFD_ON, reg, 1);
  234. stb0899_write_reg(state, STB0899_CFD, reg);
  235. reg = stb0899_read_reg(state, STB0899_DSTATUS);
  236. dprintk(state->verbose, FE_DEBUG, 1, "--------------------> STB0899_DSTATUS=[0x%02x]", reg);
  237. if (STB0899_GETFIELD(CARRIER_FOUND, reg)) {
  238. internal->status = CARRIEROK;
  239. dprintk(state->verbose, FE_DEBUG, 1, "-------------> CARRIEROK !");
  240. } else {
  241. internal->status = NOCARRIER;
  242. dprintk(state->verbose, FE_DEBUG, 1, "-------------> NOCARRIER !");
  243. }
  244. return internal->status;
  245. }
  246. /*
  247. * stb0899_search_carrier
  248. * Search for a QPSK carrier with the derotator
  249. */
  250. static enum stb0899_status stb0899_search_carrier(struct stb0899_state *state)
  251. {
  252. struct stb0899_internal *internal = &state->internal;
  253. short int derot_freq = 0, last_derot_freq = 0, derot_limit, next_loop = 3;
  254. int index = 0;
  255. u8 cfr[2];
  256. u8 reg;
  257. internal->status = NOCARRIER;
  258. derot_limit = (internal->sub_range / 2L) / internal->mclk;
  259. derot_freq = internal->derot_freq;
  260. reg = stb0899_read_reg(state, STB0899_CFD);
  261. STB0899_SETFIELD_VAL(CFD_ON, reg, 1);
  262. stb0899_write_reg(state, STB0899_CFD, reg);
  263. do {
  264. dprintk(state->verbose, FE_DEBUG, 1, "Derot Freq=%d, mclk=%d", derot_freq, internal->mclk);
  265. if (stb0899_check_carrier(state) == NOCARRIER) {
  266. index++;
  267. last_derot_freq = derot_freq;
  268. derot_freq += index * internal->direction * internal->derot_step; /* next zig zag derotator position */
  269. if(ABS(derot_freq) > derot_limit)
  270. next_loop--;
  271. if (next_loop) {
  272. reg = stb0899_read_reg(state, STB0899_CFD);
  273. STB0899_SETFIELD_VAL(CFD_ON, reg, 1);
  274. stb0899_write_reg(state, STB0899_CFD, reg);
  275. STB0899_SETFIELD_VAL(CFRM, cfr[0], MSB(state->config->inversion * derot_freq));
  276. STB0899_SETFIELD_VAL(CFRL, cfr[1], LSB(state->config->inversion * derot_freq));
  277. stb0899_write_regs(state, STB0899_CFRM, cfr, 2); /* derotator frequency */
  278. }
  279. }
  280. internal->direction = -internal->direction; /* Change zigzag direction */
  281. } while ((internal->status != CARRIEROK) && next_loop);
  282. if (internal->status == CARRIEROK) {
  283. stb0899_read_regs(state, STB0899_CFRM, cfr, 2); /* get derotator frequency */
  284. internal->derot_freq = state->config->inversion * MAKEWORD16(cfr[0], cfr[1]);
  285. dprintk(state->verbose, FE_DEBUG, 1, "----> CARRIER OK !, Derot Freq=%d", internal->derot_freq);
  286. } else {
  287. internal->derot_freq = last_derot_freq;
  288. }
  289. return internal->status;
  290. }
  291. /*
  292. * stb0899_check_data
  293. * Check for data found
  294. */
  295. static enum stb0899_status stb0899_check_data(struct stb0899_state *state)
  296. {
  297. struct stb0899_internal *internal = &state->internal;
  298. struct stb0899_params *params = &state->params;
  299. int lock = 0, index = 0, dataTime = 500, loop;
  300. u8 reg;
  301. internal->status = NODATA;
  302. /* RESET FEC */
  303. reg = stb0899_read_reg(state, STB0899_TSTRES);
  304. STB0899_SETFIELD_VAL(FRESACS, reg, 1);
  305. stb0899_write_reg(state, STB0899_TSTRES, reg);
  306. msleep(1);
  307. reg = stb0899_read_reg(state, STB0899_TSTRES);
  308. STB0899_SETFIELD_VAL(FRESACS, reg, 0);
  309. stb0899_write_reg(state, STB0899_TSTRES, reg);
  310. if (params->srate <= 2000000)
  311. dataTime = 2000;
  312. else if (params->srate <= 5000000)
  313. dataTime = 1500;
  314. else if (params->srate <= 15000000)
  315. dataTime = 1000;
  316. else
  317. dataTime = 500;
  318. stb0899_write_reg(state, STB0899_DSTATUS2, 0x00); /* force search loop */
  319. while (1) {
  320. /* WARNING! VIT LOCKED has to be tested before VIT_END_LOOOP */
  321. reg = stb0899_read_reg(state, STB0899_VSTATUS);
  322. lock = STB0899_GETFIELD(VSTATUS_LOCKEDVIT, reg);
  323. loop = STB0899_GETFIELD(VSTATUS_END_LOOPVIT, reg);
  324. if (lock || loop || (index > dataTime))
  325. break;
  326. index++;
  327. }
  328. if (lock) { /* DATA LOCK indicator */
  329. internal->status = DATAOK;
  330. dprintk(state->verbose, FE_DEBUG, 1, "-----------------> DATA OK !");
  331. }
  332. return internal->status;
  333. }
  334. /*
  335. * stb0899_search_data
  336. * Search for a QPSK carrier with the derotator
  337. */
  338. static enum stb0899_status stb0899_search_data(struct stb0899_state *state)
  339. {
  340. short int derot_freq, derot_step, derot_limit, next_loop = 3;
  341. u8 cfr[2];
  342. u8 reg;
  343. int index = 1;
  344. struct stb0899_internal *internal = &state->internal;
  345. struct stb0899_params *params = &state->params;
  346. derot_step = (params->srate / 4L) / internal->mclk;
  347. derot_limit = (internal->sub_range / 2L) / internal->mclk;
  348. derot_freq = internal->derot_freq;
  349. do {
  350. if ((internal->status != CARRIEROK) || (stb0899_check_data(state) != DATAOK)) {
  351. derot_freq += index * internal->direction * derot_step; /* next zig zag derotator position */
  352. if (ABS(derot_freq) > derot_limit)
  353. next_loop--;
  354. if (next_loop) {
  355. dprintk(state->verbose, FE_DEBUG, 1, "Derot freq=%d, mclk=%d", derot_freq, internal->mclk);
  356. reg = stb0899_read_reg(state, STB0899_CFD);
  357. STB0899_SETFIELD_VAL(CFD_ON, reg, 1);
  358. stb0899_write_reg(state, STB0899_CFD, reg);
  359. STB0899_SETFIELD_VAL(CFRM, cfr[0], MSB(state->config->inversion * derot_freq));
  360. STB0899_SETFIELD_VAL(CFRL, cfr[1], LSB(state->config->inversion * derot_freq));
  361. stb0899_write_regs(state, STB0899_CFRM, cfr, 2); /* derotator frequency */
  362. stb0899_check_carrier(state);
  363. index++;
  364. }
  365. }
  366. internal->direction = -internal->direction; /* change zig zag direction */
  367. } while ((internal->status != DATAOK) && next_loop);
  368. if (internal->status == DATAOK) {
  369. stb0899_read_regs(state, STB0899_CFRM, cfr, 2); /* get derotator frequency */
  370. internal->derot_freq = state->config->inversion * MAKEWORD16(cfr[0], cfr[1]);
  371. dprintk(state->verbose, FE_DEBUG, 1, "------> DATAOK ! Derot Freq=%d", internal->derot_freq);
  372. }
  373. return internal->status;
  374. }
  375. /*
  376. * stb0899_check_range
  377. * check if the found frequency is in the correct range
  378. */
  379. static enum stb0899_status stb0899_check_range(struct stb0899_state *state)
  380. {
  381. struct stb0899_internal *internal = &state->internal;
  382. struct stb0899_params *params = &state->params;
  383. int range_offst, tp_freq;
  384. range_offst = internal->srch_range / 2000;
  385. tp_freq = internal->freq + (internal->derot_freq * internal->mclk) / 1000;
  386. if ((tp_freq >= params->freq - range_offst) && (tp_freq <= params->freq + range_offst)) {
  387. internal->status = RANGEOK;
  388. dprintk(state->verbose, FE_DEBUG, 1, "----> RANGEOK !");
  389. } else {
  390. internal->status = OUTOFRANGE;
  391. dprintk(state->verbose, FE_DEBUG, 1, "----> OUT OF RANGE !");
  392. }
  393. return internal->status;
  394. }
  395. /*
  396. * NextSubRange
  397. * Compute the next subrange of the search
  398. */
  399. static void next_sub_range(struct stb0899_state *state)
  400. {
  401. struct stb0899_internal *internal = &state->internal;
  402. struct stb0899_params *params = &state->params;
  403. long old_sub_range;
  404. if (internal->sub_dir > 0) {
  405. old_sub_range = internal->sub_range;
  406. internal->sub_range = MIN((internal->srch_range / 2) -
  407. (internal->tuner_offst + internal->sub_range / 2),
  408. internal->sub_range);
  409. if (internal->sub_range < 0)
  410. internal->sub_range = 0;
  411. internal->tuner_offst += (old_sub_range + internal->sub_range) / 2;
  412. }
  413. internal->freq = params->freq + (internal->sub_dir * internal->tuner_offst) / 1000;
  414. internal->sub_dir = -internal->sub_dir;
  415. }
  416. /*
  417. * stb0899_dvbs_algo
  418. * Search for a signal, timing, carrier and data for a
  419. * given frequency in a given range
  420. */
  421. enum stb0899_status stb0899_dvbs_algo(struct stb0899_state *state)
  422. {
  423. struct stb0899_params *params = &state->params;
  424. struct stb0899_internal *internal = &state->internal;
  425. struct stb0899_config *config = state->config;
  426. u8 bclc, reg;
  427. u8 cfr[2];
  428. u8 eq_const[10];
  429. s32 clnI = 3;
  430. u32 bandwidth = 0;
  431. /* BETA values rated @ 99MHz */
  432. s32 betaTab[5][4] = {
  433. /* 5 10 20 30MBps */
  434. { 37, 34, 32, 31 }, /* QPSK 1/2 */
  435. { 37, 35, 33, 31 }, /* QPSK 2/3 */
  436. { 37, 35, 33, 31 }, /* QPSK 3/4 */
  437. { 37, 36, 33, 32 }, /* QPSK 5/6 */
  438. { 37, 36, 33, 32 } /* QPSK 7/8 */
  439. };
  440. internal->direction = 1;
  441. stb0899_set_srate(state, internal->master_clk, params->srate);
  442. /* Carrier loop optimization versus symbol rate for acquisition*/
  443. if (params->srate <= 5000000) {
  444. stb0899_write_reg(state, STB0899_ACLC, 0x89);
  445. bclc = stb0899_read_reg(state, STB0899_BCLC);
  446. STB0899_SETFIELD_VAL(BETA, bclc, 0x1c);
  447. stb0899_write_reg(state, STB0899_BCLC, bclc);
  448. clnI = 0;
  449. } else if (params->srate <= 15000000) {
  450. stb0899_write_reg(state, STB0899_ACLC, 0xc9);
  451. bclc = stb0899_read_reg(state, STB0899_BCLC);
  452. STB0899_SETFIELD_VAL(BETA, bclc, 0x22);
  453. stb0899_write_reg(state, STB0899_BCLC, bclc);
  454. clnI = 1;
  455. } else if(params->srate <= 25000000) {
  456. stb0899_write_reg(state, STB0899_ACLC, 0x89);
  457. bclc = stb0899_read_reg(state, STB0899_BCLC);
  458. STB0899_SETFIELD_VAL(BETA, bclc, 0x27);
  459. stb0899_write_reg(state, STB0899_BCLC, bclc);
  460. clnI = 2;
  461. } else {
  462. stb0899_write_reg(state, STB0899_ACLC, 0xc8);
  463. bclc = stb0899_read_reg(state, STB0899_BCLC);
  464. STB0899_SETFIELD_VAL(BETA, bclc, 0x29);
  465. stb0899_write_reg(state, STB0899_BCLC, bclc);
  466. clnI = 3;
  467. }
  468. dprintk(state->verbose, FE_DEBUG, 1, "Set the timing loop to acquisition");
  469. /* Set the timing loop to acquisition */
  470. stb0899_write_reg(state, STB0899_RTC, 0x46);
  471. stb0899_write_reg(state, STB0899_CFD, 0xee);
  472. /* !! WARNING !!
  473. * Do not read any status variables while acquisition,
  474. * If any needed, read before the acquisition starts
  475. * querying status while acquiring causes the
  476. * acquisition to go bad and hence no locks.
  477. */
  478. dprintk(state->verbose, FE_DEBUG, 1, "Derot Percent=%d Srate=%d mclk=%d",
  479. internal->derot_percent, params->srate, internal->mclk);
  480. /* Initial calculations */
  481. internal->derot_step = internal->derot_percent * (params->srate / 1000L) / internal->mclk; /* DerotStep/1000 * Fsymbol */
  482. internal->t_timing = stb0899_calc_loop_time(params->srate);
  483. internal->t_derot = stb0899_calc_derot_time(params->srate);
  484. internal->t_data = 500;
  485. dprintk(state->verbose, FE_DEBUG, 1, "RESET stream merger");
  486. /* RESET Stream merger */
  487. reg = stb0899_read_reg(state, STB0899_TSTRES);
  488. STB0899_SETFIELD_VAL(FRESRS, reg, 1);
  489. stb0899_write_reg(state, STB0899_TSTRES, reg);
  490. /*
  491. * Set KDIVIDER to an intermediate value between
  492. * 1/2 and 7/8 for acquisition
  493. */
  494. reg = stb0899_read_reg(state, STB0899_DEMAPVIT);
  495. STB0899_SETFIELD_VAL(DEMAPVIT_KDIVIDER, reg, 60);
  496. stb0899_write_reg(state, STB0899_DEMAPVIT, reg);
  497. stb0899_write_reg(state, STB0899_EQON, 0x01); /* Equalizer OFF while acquiring */
  498. stb0899_write_reg(state, STB0899_VITSYNC, 0x19);
  499. stb0899_first_subrange(state);
  500. do {
  501. /* Initialisations */
  502. cfr[0] = cfr[1] = 0;
  503. stb0899_write_regs(state, STB0899_CFRM, cfr, 2); /* RESET derotator frequency */
  504. reg = stb0899_read_reg(state, STB0899_RTF);
  505. STB0899_SETFIELD_VAL(RTF_TIMING_LOOP_FREQ, reg, 0);
  506. stb0899_write_reg(state, STB0899_RTF, reg);
  507. reg = stb0899_read_reg(state, STB0899_CFD);
  508. STB0899_SETFIELD_VAL(CFD_ON, reg, 1);
  509. stb0899_write_reg(state, STB0899_CFD, reg);
  510. internal->derot_freq = 0;
  511. internal->status = NOAGC1;
  512. /* Move tuner to frequency */
  513. dprintk(state->verbose, FE_DEBUG, 1, "Tuner set frequency");
  514. if (state->config->tuner_set_frequency)
  515. state->config->tuner_set_frequency(&state->frontend, internal->freq);
  516. if (state->config->tuner_get_frequency)
  517. state->config->tuner_get_frequency(&state->frontend, &internal->freq);
  518. msleep(internal->t_agc1 + internal->t_agc2 + internal->t_timing); /* AGC1, AGC2 and timing loop */
  519. dprintk(state->verbose, FE_DEBUG, 1, "current derot freq=%d", internal->derot_freq);
  520. internal->status = AGC1OK;
  521. /* There is signal in the band */
  522. if (config->tuner_get_bandwidth)
  523. config->tuner_get_bandwidth(&state->frontend, &bandwidth);
  524. if (params->srate <= bandwidth / 2)
  525. stb0899_search_tmg(state); /* For low rates (SCPC) */
  526. else
  527. stb0899_check_tmg(state); /* For high rates (MCPC) */
  528. if (internal->status == TIMINGOK) {
  529. dprintk(state->verbose, FE_DEBUG, 1,
  530. "TIMING OK ! Derot freq=%d, mclk=%d",
  531. internal->derot_freq, internal->mclk);
  532. if (stb0899_search_carrier(state) == CARRIEROK) { /* Search for carrier */
  533. dprintk(state->verbose, FE_DEBUG, 1,
  534. "CARRIER OK ! Derot freq=%d, mclk=%d",
  535. internal->derot_freq, internal->mclk);
  536. if (stb0899_search_data(state) == DATAOK) { /* Check for data */
  537. dprintk(state->verbose, FE_DEBUG, 1,
  538. "DATA OK ! Derot freq=%d, mclk=%d",
  539. internal->derot_freq, internal->mclk);
  540. if (stb0899_check_range(state) == RANGEOK) {
  541. dprintk(state->verbose, FE_DEBUG, 1,
  542. "RANGE OK ! derot freq=%d, mclk=%d",
  543. internal->derot_freq, internal->mclk);
  544. internal->freq = params->freq + ((internal->derot_freq * internal->mclk) / 1000);
  545. reg = stb0899_read_reg(state, STB0899_PLPARM);
  546. internal->fecrate = STB0899_GETFIELD(VITCURPUN, reg);
  547. dprintk(state->verbose, FE_DEBUG, 1,
  548. "freq=%d, internal resultant freq=%d",
  549. params->freq, internal->freq);
  550. dprintk(state->verbose, FE_DEBUG, 1,
  551. "internal puncture rate=%d",
  552. internal->fecrate);
  553. }
  554. }
  555. }
  556. }
  557. if (internal->status != RANGEOK)
  558. next_sub_range(state);
  559. } while (internal->sub_range && internal->status != RANGEOK);
  560. /* Set the timing loop to tracking */
  561. stb0899_write_reg(state, STB0899_RTC, 0x33);
  562. stb0899_write_reg(state, STB0899_CFD, 0xf7);
  563. /* if locked and range ok, set Kdiv */
  564. if (internal->status == RANGEOK) {
  565. dprintk(state->verbose, FE_DEBUG, 1, "Locked & Range OK !");
  566. stb0899_write_reg(state, STB0899_EQON, 0x41); /* Equalizer OFF while acquiring */
  567. stb0899_write_reg(state, STB0899_VITSYNC, 0x39); /* SN to b'11 for acquisition */
  568. /*
  569. * Carrier loop optimization versus
  570. * symbol Rate/Puncture Rate for Tracking
  571. */
  572. reg = stb0899_read_reg(state, STB0899_BCLC);
  573. switch (internal->fecrate) {
  574. case STB0899_FEC_1_2: /* 13 */
  575. stb0899_write_reg(state, STB0899_DEMAPVIT, 0x1a);
  576. STB0899_SETFIELD_VAL(BETA, reg, betaTab[0][clnI]);
  577. stb0899_write_reg(state, STB0899_BCLC, reg);
  578. break;
  579. case STB0899_FEC_2_3: /* 18 */
  580. stb0899_write_reg(state, STB0899_DEMAPVIT, 44);
  581. STB0899_SETFIELD_VAL(BETA, reg, betaTab[1][clnI]);
  582. stb0899_write_reg(state, STB0899_BCLC, reg);
  583. break;
  584. case STB0899_FEC_3_4: /* 21 */
  585. stb0899_write_reg(state, STB0899_DEMAPVIT, 60);
  586. STB0899_SETFIELD_VAL(BETA, reg, betaTab[2][clnI]);
  587. stb0899_write_reg(state, STB0899_BCLC, reg);
  588. break;
  589. case STB0899_FEC_5_6: /* 24 */
  590. stb0899_write_reg(state, STB0899_DEMAPVIT, 75);
  591. STB0899_SETFIELD_VAL(BETA, reg, betaTab[3][clnI]);
  592. stb0899_write_reg(state, STB0899_BCLC, reg);
  593. break;
  594. case STB0899_FEC_6_7: /* 25 */
  595. stb0899_write_reg(state, STB0899_DEMAPVIT, 88);
  596. stb0899_write_reg(state, STB0899_ACLC, 0x88);
  597. stb0899_write_reg(state, STB0899_BCLC, 0x9a);
  598. break;
  599. case STB0899_FEC_7_8: /* 26 */
  600. stb0899_write_reg(state, STB0899_DEMAPVIT, 94);
  601. STB0899_SETFIELD_VAL(BETA, reg, betaTab[4][clnI]);
  602. stb0899_write_reg(state, STB0899_BCLC, reg);
  603. break;
  604. default:
  605. dprintk(state->verbose, FE_DEBUG, 1, "Unsupported Puncture Rate");
  606. break;
  607. }
  608. /* release stream merger RESET */
  609. reg = stb0899_read_reg(state, STB0899_TSTRES);
  610. STB0899_SETFIELD_VAL(FRESRS, reg, 0);
  611. stb0899_write_reg(state, STB0899_TSTRES, reg);
  612. /* disable carrier detector */
  613. reg = stb0899_read_reg(state, STB0899_CFD);
  614. STB0899_SETFIELD_VAL(CFD_ON, reg, 0);
  615. stb0899_write_reg(state, STB0899_CFD, reg);
  616. stb0899_read_regs(state, STB0899_EQUAI1, eq_const, 10);
  617. }
  618. return internal->status;
  619. }
  620. /*
  621. * stb0899_dvbs2_config_uwp
  622. * Configure UWP state machine
  623. */
  624. static void stb0899_dvbs2_config_uwp(struct stb0899_state *state)
  625. {
  626. struct stb0899_internal *internal = &state->internal;
  627. struct stb0899_config *config = state->config;
  628. u32 uwp1, uwp2, uwp3, reg;
  629. uwp1 = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL1);
  630. uwp2 = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL2);
  631. uwp3 = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_CNTRL3);
  632. STB0899_SETFIELD_VAL(UWP_ESN0_AVE, uwp1, config->esno_ave);
  633. STB0899_SETFIELD_VAL(UWP_ESN0_QUANT, uwp1, config->esno_quant);
  634. STB0899_SETFIELD_VAL(UWP_TH_SOF, uwp1, config->uwp_threshold_sof);
  635. STB0899_SETFIELD_VAL(FE_COARSE_TRK, uwp2, internal->av_frame_coarse);
  636. STB0899_SETFIELD_VAL(FE_FINE_TRK, uwp2, internal->av_frame_fine);
  637. STB0899_SETFIELD_VAL(UWP_MISS_TH, uwp2, config->miss_threshold);
  638. STB0899_SETFIELD_VAL(UWP_TH_ACQ, uwp3, config->uwp_threshold_acq);
  639. STB0899_SETFIELD_VAL(UWP_TH_TRACK, uwp3, config->uwp_threshold_track);
  640. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_UWP_CNTRL1, STB0899_OFF0_UWP_CNTRL1, uwp1);
  641. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_UWP_CNTRL2, STB0899_OFF0_UWP_CNTRL2, uwp2);
  642. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_UWP_CNTRL3, STB0899_OFF0_UWP_CNTRL3, uwp3);
  643. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, SOF_SRCH_TO);
  644. STB0899_SETFIELD_VAL(SOF_SEARCH_TIMEOUT, reg, config->sof_search_timeout);
  645. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_SOF_SRCH_TO, STB0899_OFF0_SOF_SRCH_TO, reg);
  646. }
  647. /*
  648. * stb0899_dvbs2_config_csm_auto
  649. * Set CSM to AUTO mode
  650. */
  651. static void stb0899_dvbs2_config_csm_auto(struct stb0899_state *state)
  652. {
  653. u32 reg;
  654. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL1);
  655. STB0899_SETFIELD_VAL(CSM_AUTO_PARAM, reg, 1);
  656. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL1, STB0899_OFF0_CSM_CNTRL1, reg);
  657. }
  658. long Log2Int(int number)
  659. {
  660. int i;
  661. i = 0;
  662. while ((1 << i) <= ABS(number))
  663. i++;
  664. if (number == 0)
  665. i = 1;
  666. return i - 1;
  667. }
  668. /*
  669. * stb0899_dvbs2_calc_srate
  670. * compute BTR_NOM_FREQ for the symbol rate
  671. */
  672. static u32 stb0899_dvbs2_calc_srate(struct stb0899_state *state)
  673. {
  674. struct stb0899_internal *internal = &state->internal;
  675. struct stb0899_config *config = state->config;
  676. u32 dec_ratio, dec_rate, decim, remain, intval, btr_nom_freq;
  677. u32 master_clk, srate;
  678. dec_ratio = (internal->master_clk * 2) / (5 * internal->srate);
  679. dec_ratio = (dec_ratio == 0) ? 1 : dec_ratio;
  680. dec_rate = Log2Int(dec_ratio);
  681. decim = 1 << dec_rate;
  682. master_clk = internal->master_clk / 1000;
  683. srate = internal->srate / 1000;
  684. if (decim <= 4) {
  685. intval = (decim * (1 << (config->btr_nco_bits - 1))) / master_clk;
  686. remain = (decim * (1 << (config->btr_nco_bits - 1))) % master_clk;
  687. } else {
  688. intval = (1 << (config->btr_nco_bits - 1)) / (master_clk / 100) * decim / 100;
  689. remain = (decim * (1 << (config->btr_nco_bits - 1))) % master_clk;
  690. }
  691. btr_nom_freq = (intval * srate) + ((remain * srate) / master_clk);
  692. return btr_nom_freq;
  693. }
  694. /*
  695. * stb0899_dvbs2_calc_dev
  696. * compute the correction to be applied to symbol rate
  697. */
  698. static u32 stb0899_dvbs2_calc_dev(struct stb0899_state *state)
  699. {
  700. struct stb0899_internal *internal = &state->internal;
  701. u32 dec_ratio, correction, master_clk, srate;
  702. dec_ratio = (internal->master_clk * 2) / (5 * internal->srate);
  703. dec_ratio = (dec_ratio == 0) ? 1 : dec_ratio;
  704. master_clk = internal->master_clk / 1000; /* for integer Caculation*/
  705. srate = internal->srate / 1000; /* for integer Caculation*/
  706. correction = (512 * master_clk) / (2 * dec_ratio * srate);
  707. return correction;
  708. }
  709. /*
  710. * stb0899_dvbs2_set_srate
  711. * Set DVBS2 symbol rate
  712. */
  713. static void stb0899_dvbs2_set_srate(struct stb0899_state *state)
  714. {
  715. struct stb0899_internal *internal = &state->internal;
  716. u32 dec_ratio, dec_rate, win_sel, decim, f_sym, btr_nom_freq;
  717. u32 correction, freq_adj, band_lim, decim_cntrl, reg;
  718. u8 anti_alias;
  719. /*set decimation to 1*/
  720. dec_ratio = (internal->master_clk * 2) / (5 * internal->srate);
  721. dec_ratio = (dec_ratio == 0) ? 1 : dec_ratio;
  722. dec_rate = Log2Int(dec_ratio);
  723. win_sel = 0;
  724. if (dec_rate >= 5)
  725. win_sel = dec_rate - 4;
  726. decim = (1 << dec_rate);
  727. /* (FSamp/Fsymbol *100) for integer Caculation */
  728. f_sym = internal->master_clk / ((decim * internal->srate) / 1000);
  729. if (f_sym <= 2250) /* don't band limit signal going into btr block*/
  730. band_lim = 1;
  731. else
  732. band_lim = 0; /* band limit signal going into btr block*/
  733. decim_cntrl = ((win_sel << 3) & 0x18) + ((band_lim << 5) & 0x20) + (dec_rate & 0x7);
  734. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_DECIM_CNTRL, STB0899_OFF0_DECIM_CNTRL, decim_cntrl);
  735. if (f_sym <= 3450)
  736. anti_alias = 0;
  737. else if (f_sym <= 4250)
  738. anti_alias = 1;
  739. else
  740. anti_alias = 2;
  741. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_ANTI_ALIAS_SEL, STB0899_OFF0_ANTI_ALIAS_SEL, anti_alias);
  742. btr_nom_freq = stb0899_dvbs2_calc_srate(state);
  743. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_NOM_FREQ, STB0899_OFF0_BTR_NOM_FREQ, btr_nom_freq);
  744. correction = stb0899_dvbs2_calc_dev(state);
  745. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, BTR_CNTRL);
  746. STB0899_SETFIELD_VAL(BTR_FREQ_CORR, reg, correction);
  747. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_CNTRL, STB0899_OFF0_BTR_CNTRL, reg);
  748. /* scale UWP+CSM frequency to sample rate*/
  749. freq_adj = internal->srate / (internal->master_clk / 4096);
  750. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_FREQ_ADJ_SCALE, STB0899_OFF0_FREQ_ADJ_SCALE, freq_adj);
  751. }
  752. /*
  753. * stb0899_dvbs2_set_btr_loopbw
  754. * set bit timing loop bandwidth as a percentage of the symbol rate
  755. */
  756. static void stb0899_dvbs2_set_btr_loopbw(struct stb0899_state *state)
  757. {
  758. struct stb0899_internal *internal = &state->internal;
  759. struct stb0899_config *config = state->config;
  760. u32 sym_peak = 23, zeta = 707, loopbw_percent = 60;
  761. s32 dec_ratio, dec_rate, k_btr1_rshft, k_btr1, k_btr0_rshft;
  762. s32 k_btr0, k_btr2_rshft, k_direct_shift, k_indirect_shift;
  763. u32 decim, K, wn, k_direct, k_indirect;
  764. u32 reg;
  765. dec_ratio = (internal->master_clk * 2) / (5 * internal->srate);
  766. dec_ratio = (dec_ratio == 0) ? 1 : dec_ratio;
  767. dec_rate = Log2Int(dec_ratio);
  768. decim = (1 << dec_rate);
  769. sym_peak *= 576000;
  770. K = (1 << config->btr_nco_bits) / (internal->master_clk / 1000);
  771. K *= (internal->srate / 1000000) * decim; /*k=k 10^-8*/
  772. if (K != 0) {
  773. K = sym_peak / K;
  774. wn = (4 * zeta * zeta) + 1000000;
  775. wn = (2 * (loopbw_percent * 1000) * 40 * zeta) /wn; /*wn =wn 10^-8*/
  776. k_indirect = (wn * wn) / K;
  777. k_indirect = k_indirect; /*kindirect = kindirect 10^-6*/
  778. k_direct = (2 * wn * zeta) / K; /*kDirect = kDirect 10^-2*/
  779. k_direct *= 100;
  780. k_direct_shift = Log2Int(k_direct) - Log2Int(10000) - 2;
  781. k_btr1_rshft = (-1 * k_direct_shift) + config->btr_gain_shift_offset;
  782. k_btr1 = k_direct / (1 << k_direct_shift);
  783. k_btr1 /= 10000;
  784. k_indirect_shift = Log2Int(k_indirect + 15) - 20 /*- 2*/;
  785. k_btr0_rshft = (-1 * k_indirect_shift) + config->btr_gain_shift_offset;
  786. k_btr0 = k_indirect * (1 << (-k_indirect_shift));
  787. k_btr0 /= 1000000;
  788. k_btr2_rshft = 0;
  789. if (k_btr0_rshft > 15) {
  790. k_btr2_rshft = k_btr0_rshft - 15;
  791. k_btr0_rshft = 15;
  792. }
  793. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, BTR_LOOP_GAIN);
  794. STB0899_SETFIELD_VAL(KBTR0_RSHFT, reg, k_btr0_rshft);
  795. STB0899_SETFIELD_VAL(KBTR0, reg, k_btr0);
  796. STB0899_SETFIELD_VAL(KBTR1_RSHFT, reg, k_btr1_rshft);
  797. STB0899_SETFIELD_VAL(KBTR1, reg, k_btr1);
  798. STB0899_SETFIELD_VAL(KBTR2_RSHFT, reg, k_btr2_rshft);
  799. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_LOOP_GAIN, STB0899_OFF0_BTR_LOOP_GAIN, reg);
  800. } else
  801. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_LOOP_GAIN, STB0899_OFF0_BTR_LOOP_GAIN, 0xc4c4f);
  802. }
  803. /*
  804. * stb0899_dvbs2_set_carr_freq
  805. * set nominal frequency for carrier search
  806. */
  807. static void stb0899_dvbs2_set_carr_freq(struct stb0899_state *state, s32 carr_freq, u32 master_clk)
  808. {
  809. struct stb0899_config *config = state->config;
  810. s32 crl_nom_freq;
  811. u32 reg;
  812. crl_nom_freq = (1 << config->crl_nco_bits) / master_clk;
  813. crl_nom_freq *= carr_freq;
  814. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_NOM_FREQ);
  815. STB0899_SETFIELD_VAL(CRL_NOM_FREQ, reg, crl_nom_freq);
  816. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_NOM_FREQ, STB0899_OFF0_CRL_NOM_FREQ, reg);
  817. }
  818. /*
  819. * stb0899_dvbs2_init_calc
  820. * Initialize DVBS2 UWP, CSM, carrier and timing loops
  821. */
  822. static void stb0899_dvbs2_init_calc(struct stb0899_state *state)
  823. {
  824. struct stb0899_internal *internal = &state->internal;
  825. s32 steps, step_size;
  826. u32 range, reg;
  827. /* config uwp and csm */
  828. stb0899_dvbs2_config_uwp(state);
  829. stb0899_dvbs2_config_csm_auto(state);
  830. /* initialize BTR */
  831. stb0899_dvbs2_set_srate(state);
  832. stb0899_dvbs2_set_btr_loopbw(state);
  833. if (internal->srate / 1000000 >= 15)
  834. step_size = (1 << 17) / 5;
  835. else if (internal->srate / 1000000 >= 10)
  836. step_size = (1 << 17) / 7;
  837. else if (internal->srate / 1000000 >= 5)
  838. step_size = (1 << 17) / 10;
  839. else
  840. step_size = (1 << 17) / 4;
  841. range = internal->srch_range / 1000000;
  842. steps = (10 * range * (1 << 17)) / (step_size * (internal->srate / 1000000));
  843. steps = (steps + 6) / 10;
  844. steps = (steps == 0) ? 1 : steps;
  845. if (steps % 2 == 0)
  846. stb0899_dvbs2_set_carr_freq(state, internal->center_freq -
  847. (internal->step_size * (internal->srate / 20000000)),
  848. (internal->master_clk) / 1000000);
  849. else
  850. stb0899_dvbs2_set_carr_freq(state, internal->center_freq, (internal->master_clk) / 1000000);
  851. /*Set Carrier Search params (zigzag, num steps and freq step size*/
  852. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, ACQ_CNTRL2);
  853. STB0899_SETFIELD_VAL(ZIGZAG, reg, 1);
  854. STB0899_SETFIELD_VAL(NUM_STEPS, reg, steps);
  855. STB0899_SETFIELD_VAL(FREQ_STEPSIZE, reg, step_size);
  856. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_ACQ_CNTRL2, STB0899_OFF0_ACQ_CNTRL2, reg);
  857. }
  858. /*
  859. * stb0899_dvbs2_btr_init
  860. * initialize the timing loop
  861. */
  862. static void stb0899_dvbs2_btr_init(struct stb0899_state *state)
  863. {
  864. u32 reg;
  865. /* set enable BTR loopback */
  866. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, BTR_CNTRL);
  867. STB0899_SETFIELD_VAL(INTRP_PHS_SENSE, reg, 1);
  868. STB0899_SETFIELD_VAL(BTR_ERR_ENA, reg, 1);
  869. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_CNTRL, STB0899_OFF0_BTR_CNTRL, reg);
  870. /* fix btr freq accum at 0 */
  871. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_FREQ_INIT, STB0899_OFF0_BTR_FREQ_INIT, 0x10000000);
  872. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_FREQ_INIT, STB0899_OFF0_BTR_FREQ_INIT, 0x00000000);
  873. /* fix btr freq accum at 0 */
  874. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_PHS_INIT, STB0899_OFF0_BTR_PHS_INIT, 0x10000000);
  875. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_BTR_PHS_INIT, STB0899_OFF0_BTR_PHS_INIT, 0x00000000);
  876. }
  877. /*
  878. * stb0899_dvbs2_reacquire
  879. * trigger a DVB-S2 acquisition
  880. */
  881. static void stb0899_dvbs2_reacquire(struct stb0899_state *state)
  882. {
  883. u32 reg = 0;
  884. /* demod soft reset */
  885. STB0899_SETFIELD_VAL(DVBS2_RESET, reg, 1);
  886. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_RESET_CNTRL, STB0899_OFF0_RESET_CNTRL, reg);
  887. /*Reset Timing Loop */
  888. stb0899_dvbs2_btr_init(state);
  889. /* reset Carrier loop */
  890. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_FREQ_INIT, STB0899_OFF0_CRL_FREQ_INIT, (1 << 30));
  891. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_FREQ_INIT, STB0899_OFF0_CRL_FREQ_INIT, 0);
  892. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_LOOP_GAIN, STB0899_OFF0_CRL_LOOP_GAIN, 0);
  893. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_PHS_INIT, STB0899_OFF0_CRL_PHS_INIT, (1 << 30));
  894. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_PHS_INIT, STB0899_OFF0_CRL_PHS_INIT, 0);
  895. /*release demod soft reset */
  896. reg = 0;
  897. STB0899_SETFIELD_VAL(DVBS2_RESET, reg, 0);
  898. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_RESET_CNTRL, STB0899_OFF0_RESET_CNTRL, reg);
  899. /* start acquisition process */
  900. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_ACQUIRE_TRIG, STB0899_OFF0_ACQUIRE_TRIG, 1);
  901. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_LOCK_LOST, STB0899_OFF0_LOCK_LOST, 0);
  902. /* equalizer Init */
  903. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_EQUALIZER_INIT, STB0899_OFF0_EQUALIZER_INIT, 1);
  904. /*Start equilizer */
  905. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_EQUALIZER_INIT, STB0899_OFF0_EQUALIZER_INIT, 0);
  906. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, EQ_CNTRL);
  907. STB0899_SETFIELD_VAL(EQ_SHIFT, reg, 0);
  908. STB0899_SETFIELD_VAL(EQ_DISABLE_UPDATE, reg, 0);
  909. STB0899_SETFIELD_VAL(EQ_DELAY, reg, 0x05);
  910. STB0899_SETFIELD_VAL(EQ_ADAPT_MODE, reg, 0x01);
  911. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_EQ_CNTRL, STB0899_OFF0_EQ_CNTRL, reg);
  912. /* RESET Packet delineator */
  913. stb0899_write_reg(state, STB0899_PDELCTRL, 0x4a);
  914. }
  915. /*
  916. * stb0899_dvbs2_get_dmd_status
  917. * get DVB-S2 Demod LOCK status
  918. */
  919. static enum stb0899_status stb0899_dvbs2_get_dmd_status(struct stb0899_state *state, int timeout)
  920. {
  921. int time = -10, lock = 0, uwp, csm;
  922. u32 reg;
  923. do {
  924. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STATUS);
  925. dprintk(state->verbose, FE_DEBUG, 1, "DMD_STATUS=[0x%02x]", reg);
  926. if (STB0899_GETFIELD(IF_AGC_LOCK, reg))
  927. dprintk(state->verbose, FE_DEBUG, 1, "------------->IF AGC LOCKED !");
  928. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_STAT2);
  929. dprintk(state->verbose, FE_DEBUG, 1, "----------->DMD STAT2=[0x%02x]", reg);
  930. uwp = STB0899_GETFIELD(UWP_LOCK, reg);
  931. csm = STB0899_GETFIELD(CSM_LOCK, reg);
  932. if (uwp && csm)
  933. lock = 1;
  934. time += 10;
  935. msleep(10);
  936. } while ((!lock) && (time <= timeout));
  937. if (lock) {
  938. dprintk(state->verbose, FE_DEBUG, 1, "----------------> DVB-S2 LOCK !");
  939. return DVBS2_DEMOD_LOCK;
  940. } else {
  941. return DVBS2_DEMOD_NOLOCK;
  942. }
  943. }
  944. /*
  945. * stb0899_dvbs2_get_data_lock
  946. * get FEC status
  947. */
  948. static int stb0899_dvbs2_get_data_lock(struct stb0899_state *state, int timeout)
  949. {
  950. int time = 0, lock = 0;
  951. u8 reg;
  952. while ((!lock) && (time < timeout)) {
  953. reg = stb0899_read_reg(state, STB0899_CFGPDELSTATUS1);
  954. dprintk(state->verbose, FE_DEBUG, 1, "---------> CFGPDELSTATUS=[0x%02x]", reg);
  955. lock = STB0899_GETFIELD(CFGPDELSTATUS_LOCK, reg);
  956. time++;
  957. }
  958. return lock;
  959. }
  960. /*
  961. * stb0899_dvbs2_get_fec_status
  962. * get DVB-S2 FEC LOCK status
  963. */
  964. static enum stb0899_status stb0899_dvbs2_get_fec_status(struct stb0899_state *state, int timeout)
  965. {
  966. int time = 0, Locked;
  967. do {
  968. Locked = stb0899_dvbs2_get_data_lock(state, 1);
  969. time++;
  970. msleep(1);
  971. } while ((!Locked) && (time < timeout));
  972. if (Locked) {
  973. dprintk(state->verbose, FE_DEBUG, 1, "---------->DVB-S2 FEC LOCK !");
  974. return DVBS2_FEC_LOCK;
  975. } else {
  976. return DVBS2_FEC_NOLOCK;
  977. }
  978. }
  979. /*
  980. * stb0899_dvbs2_init_csm
  981. * set parameters for manual mode
  982. */
  983. static void stb0899_dvbs2_init_csm(struct stb0899_state *state, int pilots, enum stb0899_modcod modcod)
  984. {
  985. struct stb0899_internal *internal = &state->internal;
  986. s32 dvt_tbl = 1, two_pass = 0, agc_gain = 6, agc_shift = 0, loop_shift = 0, phs_diff_thr = 0x80;
  987. s32 gamma_acq, gamma_rho_acq, gamma_trk, gamma_rho_trk, lock_count_thr;
  988. u32 csm1, csm2, csm3, csm4;
  989. if (((internal->master_clk / internal->srate) <= 4) && (modcod <= 11) && (pilots == 1)) {
  990. switch (modcod) {
  991. case STB0899_QPSK_12:
  992. gamma_acq = 25;
  993. gamma_rho_acq = 2700;
  994. gamma_trk = 12;
  995. gamma_rho_trk = 180;
  996. lock_count_thr = 8;
  997. break;
  998. case STB0899_QPSK_35:
  999. gamma_acq = 38;
  1000. gamma_rho_acq = 7182;
  1001. gamma_trk = 14;
  1002. gamma_rho_trk = 308;
  1003. lock_count_thr = 8;
  1004. break;
  1005. case STB0899_QPSK_23:
  1006. gamma_acq = 42;
  1007. gamma_rho_acq = 9408;
  1008. gamma_trk = 17;
  1009. gamma_rho_trk = 476;
  1010. lock_count_thr = 8;
  1011. break;
  1012. case STB0899_QPSK_34:
  1013. gamma_acq = 53;
  1014. gamma_rho_acq = 16642;
  1015. gamma_trk = 19;
  1016. gamma_rho_trk = 646;
  1017. lock_count_thr = 8;
  1018. break;
  1019. case STB0899_QPSK_45:
  1020. gamma_acq = 53;
  1021. gamma_rho_acq = 17119;
  1022. gamma_trk = 22;
  1023. gamma_rho_trk = 880;
  1024. lock_count_thr = 8;
  1025. break;
  1026. case STB0899_QPSK_56:
  1027. gamma_acq = 55;
  1028. gamma_rho_acq = 19250;
  1029. gamma_trk = 23;
  1030. gamma_rho_trk = 989;
  1031. lock_count_thr = 8;
  1032. break;
  1033. case STB0899_QPSK_89:
  1034. gamma_acq = 60;
  1035. gamma_rho_acq = 24240;
  1036. gamma_trk = 24;
  1037. gamma_rho_trk = 1176;
  1038. lock_count_thr = 8;
  1039. break;
  1040. case STB0899_QPSK_910:
  1041. gamma_acq = 66;
  1042. gamma_rho_acq = 29634;
  1043. gamma_trk = 24;
  1044. gamma_rho_trk = 1176;
  1045. lock_count_thr = 8;
  1046. break;
  1047. default:
  1048. gamma_acq = 66;
  1049. gamma_rho_acq = 29634;
  1050. gamma_trk = 24;
  1051. gamma_rho_trk = 1176;
  1052. lock_count_thr = 8;
  1053. break;
  1054. }
  1055. csm1 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL1);
  1056. STB0899_SETFIELD_VAL(CSM_AUTO_PARAM, csm1, 0);
  1057. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL1, STB0899_OFF0_CSM_CNTRL1, csm1);
  1058. csm1 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL1);
  1059. csm2 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL2);
  1060. csm3 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL3);
  1061. csm4 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL4);
  1062. STB0899_SETFIELD_VAL(CSM_DVT_TABLE, csm1, dvt_tbl);
  1063. STB0899_SETFIELD_VAL(CSM_TWO_PASS, csm1, two_pass);
  1064. STB0899_SETFIELD_VAL(CSM_AGC_GAIN, csm1, agc_gain);
  1065. STB0899_SETFIELD_VAL(CSM_AGC_SHIFT, csm1, agc_shift);
  1066. STB0899_SETFIELD_VAL(FE_LOOP_SHIFT, csm1, loop_shift);
  1067. STB0899_SETFIELD_VAL(CSM_GAMMA_ACQ, csm2, gamma_acq);
  1068. STB0899_SETFIELD_VAL(CSM_GAMMA_RHOACQ, csm2, gamma_rho_acq);
  1069. STB0899_SETFIELD_VAL(CSM_GAMMA_TRACK, csm3, gamma_trk);
  1070. STB0899_SETFIELD_VAL(CSM_GAMMA_RHOTRACK, csm3, gamma_rho_trk);
  1071. STB0899_SETFIELD_VAL(CSM_LOCKCOUNT_THRESH, csm4, lock_count_thr);
  1072. STB0899_SETFIELD_VAL(CSM_PHASEDIFF_THRESH, csm4, phs_diff_thr);
  1073. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL1, STB0899_OFF0_CSM_CNTRL1, csm1);
  1074. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL2, STB0899_OFF0_CSM_CNTRL2, csm2);
  1075. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL3, STB0899_OFF0_CSM_CNTRL3, csm3);
  1076. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL4, STB0899_OFF0_CSM_CNTRL4, csm4);
  1077. }
  1078. }
  1079. /*
  1080. * stb0899_dvbs2_get_srate
  1081. * get DVB-S2 Symbol Rate
  1082. */
  1083. static u32 stb0899_dvbs2_get_srate(struct stb0899_state *state)
  1084. {
  1085. struct stb0899_internal *internal = &state->internal;
  1086. struct stb0899_config *config = state->config;
  1087. u32 bTrNomFreq, srate, decimRate, intval1, intval2, reg;
  1088. int div1, div2, rem1, rem2;
  1089. div1 = config->btr_nco_bits / 2;
  1090. div2 = config->btr_nco_bits - div1 - 1;
  1091. bTrNomFreq = STB0899_READ_S2REG(STB0899_S2DEMOD, BTR_NOM_FREQ);
  1092. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DECIM_CNTRL);
  1093. decimRate = STB0899_GETFIELD(DECIM_RATE, reg);
  1094. decimRate = (1 << decimRate);
  1095. intval1 = internal->master_clk / (1 << div1);
  1096. intval2 = bTrNomFreq / (1 << div2);
  1097. rem1 = internal->master_clk % (1 << div1);
  1098. rem2 = bTrNomFreq % (1 << div2);
  1099. /* only for integer calculation */
  1100. srate = (intval1 * intval2) + ((intval1 * rem2) / (1 << div2)) + ((intval2 * rem1) / (1 << div1));
  1101. srate /= decimRate; /*symbrate = (btrnomfreq_register_val*MasterClock)/2^(27+decim_rate_field) */
  1102. return srate;
  1103. }
  1104. /*
  1105. * stb0899_dvbs2_algo
  1106. * Search for signal, timing, carrier and data for a given
  1107. * frequency in a given range
  1108. */
  1109. enum stb0899_status stb0899_dvbs2_algo(struct stb0899_state *state)
  1110. {
  1111. struct stb0899_internal *internal = &state->internal;
  1112. enum stb0899_modcod modcod;
  1113. s32 offsetfreq, searchTime, FecLockTime, pilots, iqSpectrum;
  1114. int i = 0;
  1115. u32 reg, csm1;
  1116. if (internal->srate <= 2000000) {
  1117. searchTime = 5000; /* 5000 ms max time to lock UWP and CSM, SYMB <= 2Mbs */
  1118. FecLockTime = 350; /* 350 ms max time to lock FEC, SYMB <= 2Mbs */
  1119. } else if (internal->srate <= 5000000) {
  1120. searchTime = 2500; /* 2500 ms max time to lock UWP and CSM, 2Mbs < SYMB <= 5Mbs */
  1121. FecLockTime = 170; /* 170 ms max time to lock FEC, 2Mbs< SYMB <= 5Mbs */
  1122. } else if (internal->srate <= 10000000) {
  1123. searchTime = 1500; /* 1500 ms max time to lock UWP and CSM, 5Mbs <SYMB <= 10Mbs */
  1124. FecLockTime = 80; /* 80 ms max time to lock FEC, 5Mbs< SYMB <= 10Mbs */
  1125. } else if (internal->srate <= 15000000) {
  1126. searchTime = 500; /* 500 ms max time to lock UWP and CSM, 10Mbs <SYMB <= 15Mbs */
  1127. FecLockTime = 50; /* 50 ms max time to lock FEC, 10Mbs< SYMB <= 15Mbs */
  1128. } else if (internal->srate <= 20000000) {
  1129. searchTime = 300; /* 300 ms max time to lock UWP and CSM, 15Mbs < SYMB <= 20Mbs */
  1130. FecLockTime = 30; /* 50 ms max time to lock FEC, 15Mbs< SYMB <= 20Mbs */
  1131. } else if (internal->srate <= 25000000) {
  1132. searchTime = 250; /* 250 ms max time to lock UWP and CSM, 20 Mbs < SYMB <= 25Mbs */
  1133. FecLockTime = 25; /* 25 ms max time to lock FEC, 20Mbs< SYMB <= 25Mbs */
  1134. } else {
  1135. searchTime = 150; /* 150 ms max time to lock UWP and CSM, SYMB > 25Mbs */
  1136. FecLockTime = 20; /* 20 ms max time to lock FEC, 20Mbs< SYMB <= 25Mbs */
  1137. }
  1138. /* Maintain Stream Merger in reset during acquisition */
  1139. reg = stb0899_read_reg(state, STB0899_TSTRES);
  1140. STB0899_SETFIELD_VAL(FRESRS, reg, 1);
  1141. stb0899_write_reg(state, STB0899_TSTRES, reg);
  1142. /* Move tuner to frequency */
  1143. if (state->config->tuner_set_frequency)
  1144. state->config->tuner_set_frequency(&state->frontend, internal->freq);
  1145. if (state->config->tuner_get_frequency)
  1146. state->config->tuner_get_frequency(&state->frontend, &internal->freq);
  1147. /* Set IF AGC to acquisition */
  1148. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL);
  1149. STB0899_SETFIELD_VAL(IF_LOOP_GAIN, reg, 4);
  1150. STB0899_SETFIELD_VAL(IF_AGC_REF, reg, 32);
  1151. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL, STB0899_OFF0_IF_AGC_CNTRL, reg);
  1152. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL2);
  1153. STB0899_SETFIELD_VAL(IF_AGC_DUMP_PER, reg, 0);
  1154. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL2, STB0899_OFF0_IF_AGC_CNTRL2, reg);
  1155. /* Initialisation */
  1156. stb0899_dvbs2_init_calc(state);
  1157. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CNTRL2);
  1158. switch (internal->inversion) {
  1159. case IQ_SWAP_OFF:
  1160. STB0899_SETFIELD_VAL(SPECTRUM_INVERT, reg, 0);
  1161. break;
  1162. case IQ_SWAP_ON:
  1163. STB0899_SETFIELD_VAL(SPECTRUM_INVERT, reg, 1);
  1164. break;
  1165. case IQ_SWAP_AUTO: /* use last successful search first */
  1166. STB0899_SETFIELD_VAL(SPECTRUM_INVERT, reg, 1);
  1167. break;
  1168. }
  1169. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_DMD_CNTRL2, STB0899_OFF0_DMD_CNTRL2, reg);
  1170. stb0899_dvbs2_reacquire(state);
  1171. /* Wait for demod lock (UWP and CSM) */
  1172. internal->status = stb0899_dvbs2_get_dmd_status(state, searchTime);
  1173. if (internal->status == DVBS2_DEMOD_LOCK) {
  1174. dprintk(state->verbose, FE_DEBUG, 1, "------------> DVB-S2 DEMOD LOCK !");
  1175. i = 0;
  1176. /* Demod Locked, check FEC status */
  1177. internal->status = stb0899_dvbs2_get_fec_status(state, FecLockTime);
  1178. /*If false lock (UWP and CSM Locked but no FEC) try 3 time max*/
  1179. while ((internal->status != DVBS2_FEC_LOCK) && (i < 3)) {
  1180. /* Read the frequency offset*/
  1181. offsetfreq = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_FREQ);
  1182. /* Set the Nominal frequency to the found frequency offset for the next reacquire*/
  1183. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_NOM_FREQ);
  1184. STB0899_SETFIELD_VAL(CRL_NOM_FREQ, reg, offsetfreq);
  1185. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_NOM_FREQ, STB0899_OFF0_CRL_NOM_FREQ, reg);
  1186. stb0899_dvbs2_reacquire(state);
  1187. internal->status = stb0899_dvbs2_get_fec_status(state, searchTime);
  1188. i++;
  1189. }
  1190. }
  1191. if (internal->status != DVBS2_FEC_LOCK) {
  1192. if (internal->inversion == IQ_SWAP_AUTO) {
  1193. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CNTRL2);
  1194. iqSpectrum = STB0899_GETFIELD(SPECTRUM_INVERT, reg);
  1195. /* IQ Spectrum Inversion */
  1196. STB0899_SETFIELD_VAL(SPECTRUM_INVERT, reg, !iqSpectrum);
  1197. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_DMD_CNTRL2, STB0899_OFF0_DMD_CNTRL2, reg);
  1198. /* start acquistion process */
  1199. stb0899_dvbs2_reacquire(state);
  1200. /* Wait for demod lock (UWP and CSM) */
  1201. internal->status = stb0899_dvbs2_get_dmd_status(state, searchTime);
  1202. if (internal->status == DVBS2_DEMOD_LOCK) {
  1203. i = 0;
  1204. /* Demod Locked, check FEC */
  1205. internal->status = stb0899_dvbs2_get_fec_status(state, FecLockTime);
  1206. /*try thrice for false locks, (UWP and CSM Locked but no FEC) */
  1207. while ((internal->status != DVBS2_FEC_LOCK) && (i < 3)) {
  1208. /* Read the frequency offset*/
  1209. offsetfreq = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_FREQ);
  1210. /* Set the Nominal frequency to the found frequency offset for the next reacquire*/
  1211. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_NOM_FREQ);
  1212. STB0899_SETFIELD_VAL(CRL_NOM_FREQ, reg, offsetfreq);
  1213. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CRL_NOM_FREQ, STB0899_OFF0_CRL_NOM_FREQ, reg);
  1214. stb0899_dvbs2_reacquire(state);
  1215. internal->status = stb0899_dvbs2_get_fec_status(state, searchTime);
  1216. i++;
  1217. }
  1218. }
  1219. /*
  1220. if (pParams->DVBS2State == FE_DVBS2_FEC_LOCKED)
  1221. pParams->IQLocked = !iqSpectrum;
  1222. */
  1223. }
  1224. }
  1225. if (internal->status == DVBS2_FEC_LOCK) {
  1226. dprintk(state->verbose, FE_DEBUG, 1, "----------------> DVB-S2 FEC Lock !");
  1227. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_STAT2);
  1228. modcod = STB0899_GETFIELD(UWP_DECODE_MOD, reg) >> 2;
  1229. pilots = STB0899_GETFIELD(UWP_DECODE_MOD, reg) & 0x01;
  1230. if ((((10 * internal->master_clk) / (internal->srate / 10)) <= 410) &&
  1231. (INRANGE(STB0899_QPSK_23, modcod, STB0899_QPSK_910)) &&
  1232. (pilots == 1)) {
  1233. stb0899_dvbs2_init_csm(state, pilots, modcod);
  1234. /* Wait for UWP,CSM and data LOCK 20ms max */
  1235. internal->status = stb0899_dvbs2_get_fec_status(state, FecLockTime);
  1236. i = 0;
  1237. while ((internal->status != DVBS2_FEC_LOCK) && (i < 3)) {
  1238. csm1 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL1);
  1239. STB0899_SETFIELD_VAL(CSM_TWO_PASS, csm1, 1);
  1240. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL1, STB0899_OFF0_CSM_CNTRL1, csm1);
  1241. csm1 = STB0899_READ_S2REG(STB0899_S2DEMOD, CSM_CNTRL1);
  1242. STB0899_SETFIELD_VAL(CSM_TWO_PASS, csm1, 0);
  1243. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_CSM_CNTRL1, STB0899_OFF0_CSM_CNTRL1, csm1);
  1244. internal->status = stb0899_dvbs2_get_fec_status(state, FecLockTime);
  1245. i++;
  1246. }
  1247. }
  1248. if ((((10 * internal->master_clk) / (internal->srate / 10)) <= 410) &&
  1249. (INRANGE(STB0899_QPSK_12, modcod, STB0899_QPSK_35)) &&
  1250. (pilots == 1)) {
  1251. /* Equalizer Disable update */
  1252. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, EQ_CNTRL);
  1253. STB0899_SETFIELD_VAL(EQ_DISABLE_UPDATE, reg, 1);
  1254. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_EQ_CNTRL, STB0899_OFF0_EQ_CNTRL, reg);
  1255. }
  1256. /* slow down the Equalizer once locked */
  1257. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, EQ_CNTRL);
  1258. STB0899_SETFIELD_VAL(EQ_SHIFT, reg, 0x02);
  1259. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_EQ_CNTRL, STB0899_OFF0_EQ_CNTRL, reg);
  1260. /* Store signal parameters */
  1261. offsetfreq = STB0899_READ_S2REG(STB0899_S2DEMOD, CRL_FREQ);
  1262. offsetfreq = offsetfreq / ((1 << 30) / 1000);
  1263. offsetfreq *= (internal->master_clk / 1000000);
  1264. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, DMD_CNTRL2);
  1265. if (STB0899_GETFIELD(SPECTRUM_INVERT, reg))
  1266. offsetfreq *= -1;
  1267. internal->freq = internal->freq - offsetfreq;
  1268. internal->srate = stb0899_dvbs2_get_srate(state);
  1269. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, UWP_STAT2);
  1270. internal->modcod = STB0899_GETFIELD(UWP_DECODE_MOD, reg) >> 2;
  1271. internal->pilots = STB0899_GETFIELD(UWP_DECODE_MOD, reg) & 0x01;
  1272. internal->frame_length = (STB0899_GETFIELD(UWP_DECODE_MOD, reg) >> 1) & 0x01;
  1273. /* Set IF AGC to tracking */
  1274. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL);
  1275. STB0899_SETFIELD_VAL(IF_LOOP_GAIN, reg, 3);
  1276. /* if QPSK 1/2,QPSK 3/5 or QPSK 2/3 set IF AGC reference to 16 otherwise 32*/
  1277. if (INRANGE(STB0899_QPSK_12, internal->modcod, STB0899_QPSK_23))
  1278. STB0899_SETFIELD_VAL(IF_AGC_REF, reg, 16);
  1279. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL, STB0899_OFF0_IF_AGC_CNTRL, reg);
  1280. reg = STB0899_READ_S2REG(STB0899_S2DEMOD, IF_AGC_CNTRL2);
  1281. STB0899_SETFIELD_VAL(IF_AGC_DUMP_PER, reg, 7);
  1282. stb0899_write_s2reg(state, STB0899_S2DEMOD, STB0899_BASE_IF_AGC_CNTRL2, STB0899_OFF0_IF_AGC_CNTRL2, reg);
  1283. }
  1284. /* Release Stream Merger Reset */
  1285. reg = stb0899_read_reg(state, STB0899_TSTRES);
  1286. STB0899_SETFIELD_VAL(FRESRS, reg, 0);
  1287. stb0899_write_reg(state, STB0899_TSTRES, reg);
  1288. return internal->status;
  1289. }