stb0899_algo.c 50 KB

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