dib9000.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403
  1. /*
  2. * Linux-DVB Driver for DiBcom's DiB9000 and demodulator-family.
  3. *
  4. * Copyright (C) 2005-10 DiBcom (http://www.dibcom.fr/)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, version 2.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/i2c.h>
  12. #include <linux/mutex.h>
  13. #include "dvb_math.h"
  14. #include "dvb_frontend.h"
  15. #include "dib9000.h"
  16. #include "dibx000_common.h"
  17. static int debug;
  18. module_param(debug, int, 0644);
  19. MODULE_PARM_DESC(debug, "turn on debugging (default: 0)");
  20. #define dprintk(args...) do { if (debug) { printk(KERN_DEBUG "DiB9000: "); printk(args); printk("\n"); } } while (0)
  21. #define MAX_NUMBER_OF_FRONTENDS 6
  22. struct i2c_device {
  23. struct i2c_adapter *i2c_adap;
  24. u8 i2c_addr;
  25. u8 *i2c_read_buffer;
  26. u8 *i2c_write_buffer;
  27. };
  28. /* lock */
  29. #define DIB_LOCK struct mutex
  30. #define DibAcquireLock(lock) do { if (mutex_lock_interruptible(lock) < 0) dprintk("could not get the lock"); } while (0)
  31. #define DibReleaseLock(lock) mutex_unlock(lock)
  32. #define DibInitLock(lock) mutex_init(lock)
  33. #define DibFreeLock(lock)
  34. struct dib9000_state {
  35. struct i2c_device i2c;
  36. struct dibx000_i2c_master i2c_master;
  37. struct i2c_adapter tuner_adap;
  38. struct i2c_adapter component_bus;
  39. u16 revision;
  40. u8 reg_offs;
  41. enum frontend_tune_state tune_state;
  42. u32 status;
  43. struct dvb_frontend_parametersContext channel_status;
  44. u8 fe_id;
  45. #define DIB9000_GPIO_DEFAULT_DIRECTIONS 0xffff
  46. u16 gpio_dir;
  47. #define DIB9000_GPIO_DEFAULT_VALUES 0x0000
  48. u16 gpio_val;
  49. #define DIB9000_GPIO_DEFAULT_PWM_POS 0xffff
  50. u16 gpio_pwm_pos;
  51. union { /* common for all chips */
  52. struct {
  53. u8 mobile_mode:1;
  54. } host;
  55. struct {
  56. struct dib9000_fe_memory_map {
  57. u16 addr;
  58. u16 size;
  59. } fe_mm[18];
  60. u8 memcmd;
  61. DIB_LOCK mbx_if_lock; /* to protect read/write operations */
  62. DIB_LOCK mbx_lock; /* to protect the whole mailbox handling */
  63. DIB_LOCK mem_lock; /* to protect the memory accesses */
  64. DIB_LOCK mem_mbx_lock; /* to protect the memory-based mailbox */
  65. #define MBX_MAX_WORDS (256 - 200 - 2)
  66. #define DIB9000_MSG_CACHE_SIZE 2
  67. u16 message_cache[DIB9000_MSG_CACHE_SIZE][MBX_MAX_WORDS];
  68. u8 fw_is_running;
  69. } risc;
  70. } platform;
  71. union { /* common for all platforms */
  72. struct {
  73. struct dib9000_config cfg;
  74. } d9;
  75. } chip;
  76. struct dvb_frontend *fe[MAX_NUMBER_OF_FRONTENDS];
  77. u16 component_bus_speed;
  78. /* for the I2C transfer */
  79. struct i2c_msg msg[2];
  80. u8 i2c_write_buffer[255];
  81. u8 i2c_read_buffer[255];
  82. };
  83. static const u32 fe_info[44] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  84. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  85. 0, 0, 0, 0, 0, 0, 0, 0
  86. };
  87. enum dib9000_power_mode {
  88. DIB9000_POWER_ALL = 0,
  89. DIB9000_POWER_NO,
  90. DIB9000_POWER_INTERF_ANALOG_AGC,
  91. DIB9000_POWER_COR4_DINTLV_ICIRM_EQUAL_CFROD,
  92. DIB9000_POWER_COR4_CRY_ESRAM_MOUT_NUD,
  93. DIB9000_POWER_INTERFACE_ONLY,
  94. };
  95. enum dib9000_out_messages {
  96. OUT_MSG_HBM_ACK,
  97. OUT_MSG_HOST_BUF_FAIL,
  98. OUT_MSG_REQ_VERSION,
  99. OUT_MSG_BRIDGE_I2C_W,
  100. OUT_MSG_BRIDGE_I2C_R,
  101. OUT_MSG_BRIDGE_APB_W,
  102. OUT_MSG_BRIDGE_APB_R,
  103. OUT_MSG_SCAN_CHANNEL,
  104. OUT_MSG_MONIT_DEMOD,
  105. OUT_MSG_CONF_GPIO,
  106. OUT_MSG_DEBUG_HELP,
  107. OUT_MSG_SUBBAND_SEL,
  108. OUT_MSG_ENABLE_TIME_SLICE,
  109. OUT_MSG_FE_FW_DL,
  110. OUT_MSG_FE_CHANNEL_SEARCH,
  111. OUT_MSG_FE_CHANNEL_TUNE,
  112. OUT_MSG_FE_SLEEP,
  113. OUT_MSG_FE_SYNC,
  114. OUT_MSG_CTL_MONIT,
  115. OUT_MSG_CONF_SVC,
  116. OUT_MSG_SET_HBM,
  117. OUT_MSG_INIT_DEMOD,
  118. OUT_MSG_ENABLE_DIVERSITY,
  119. OUT_MSG_SET_OUTPUT_MODE,
  120. OUT_MSG_SET_PRIORITARY_CHANNEL,
  121. OUT_MSG_ACK_FRG,
  122. OUT_MSG_INIT_PMU,
  123. };
  124. enum dib9000_in_messages {
  125. IN_MSG_DATA,
  126. IN_MSG_FRAME_INFO,
  127. IN_MSG_CTL_MONIT,
  128. IN_MSG_ACK_FREE_ITEM,
  129. IN_MSG_DEBUG_BUF,
  130. IN_MSG_MPE_MONITOR,
  131. IN_MSG_RAWTS_MONITOR,
  132. IN_MSG_END_BRIDGE_I2C_RW,
  133. IN_MSG_END_BRIDGE_APB_RW,
  134. IN_MSG_VERSION,
  135. IN_MSG_END_OF_SCAN,
  136. IN_MSG_MONIT_DEMOD,
  137. IN_MSG_ERROR,
  138. IN_MSG_FE_FW_DL_DONE,
  139. IN_MSG_EVENT,
  140. IN_MSG_ACK_CHANGE_SVC,
  141. IN_MSG_HBM_PROF,
  142. };
  143. /* memory_access requests */
  144. #define FE_MM_W_CHANNEL 0
  145. #define FE_MM_W_FE_INFO 1
  146. #define FE_MM_RW_SYNC 2
  147. #define FE_SYNC_CHANNEL 1
  148. #define FE_SYNC_W_GENERIC_MONIT 2
  149. #define FE_SYNC_COMPONENT_ACCESS 3
  150. #define FE_MM_R_CHANNEL_SEARCH_STATE 3
  151. #define FE_MM_R_CHANNEL_UNION_CONTEXT 4
  152. #define FE_MM_R_FE_INFO 5
  153. #define FE_MM_R_FE_MONITOR 6
  154. #define FE_MM_W_CHANNEL_HEAD 7
  155. #define FE_MM_W_CHANNEL_UNION 8
  156. #define FE_MM_W_CHANNEL_CONTEXT 9
  157. #define FE_MM_R_CHANNEL_UNION 10
  158. #define FE_MM_R_CHANNEL_CONTEXT 11
  159. #define FE_MM_R_CHANNEL_TUNE_STATE 12
  160. #define FE_MM_R_GENERIC_MONITORING_SIZE 13
  161. #define FE_MM_W_GENERIC_MONITORING 14
  162. #define FE_MM_R_GENERIC_MONITORING 15
  163. #define FE_MM_W_COMPONENT_ACCESS 16
  164. #define FE_MM_RW_COMPONENT_ACCESS_BUFFER 17
  165. static int dib9000_risc_apb_access_read(struct dib9000_state *state, u32 address, u16 attribute, const u8 * tx, u32 txlen, u8 * b, u32 len);
  166. static int dib9000_risc_apb_access_write(struct dib9000_state *state, u32 address, u16 attribute, const u8 * b, u32 len);
  167. static u16 to_fw_output_mode(u16 mode)
  168. {
  169. switch (mode) {
  170. case OUTMODE_HIGH_Z:
  171. return 0;
  172. case OUTMODE_MPEG2_PAR_GATED_CLK:
  173. return 4;
  174. case OUTMODE_MPEG2_PAR_CONT_CLK:
  175. return 8;
  176. case OUTMODE_MPEG2_SERIAL:
  177. return 16;
  178. case OUTMODE_DIVERSITY:
  179. return 128;
  180. case OUTMODE_MPEG2_FIFO:
  181. return 2;
  182. case OUTMODE_ANALOG_ADC:
  183. return 1;
  184. default:
  185. return 0;
  186. }
  187. }
  188. static u16 dib9000_read16_attr(struct dib9000_state *state, u16 reg, u8 * b, u32 len, u16 attribute)
  189. {
  190. u32 chunk_size = 126;
  191. u32 l;
  192. int ret;
  193. if (state->platform.risc.fw_is_running && (reg < 1024))
  194. return dib9000_risc_apb_access_read(state, reg, attribute, NULL, 0, b, len);
  195. memset(state->msg, 0, 2 * sizeof(struct i2c_msg));
  196. state->msg[0].addr = state->i2c.i2c_addr >> 1;
  197. state->msg[0].flags = 0;
  198. state->msg[0].buf = state->i2c_write_buffer;
  199. state->msg[0].len = 2;
  200. state->msg[1].addr = state->i2c.i2c_addr >> 1;
  201. state->msg[1].flags = I2C_M_RD;
  202. state->msg[1].buf = b;
  203. state->msg[1].len = len;
  204. state->i2c_write_buffer[0] = reg >> 8;
  205. state->i2c_write_buffer[1] = reg & 0xff;
  206. if (attribute & DATA_BUS_ACCESS_MODE_8BIT)
  207. state->i2c_write_buffer[0] |= (1 << 5);
  208. if (attribute & DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT)
  209. state->i2c_write_buffer[0] |= (1 << 4);
  210. do {
  211. l = len < chunk_size ? len : chunk_size;
  212. state->msg[1].len = l;
  213. state->msg[1].buf = b;
  214. ret = i2c_transfer(state->i2c.i2c_adap, state->msg, 2) != 2 ? -EREMOTEIO : 0;
  215. if (ret != 0) {
  216. dprintk("i2c read error on %d", reg);
  217. return -EREMOTEIO;
  218. }
  219. b += l;
  220. len -= l;
  221. if (!(attribute & DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT))
  222. reg += l / 2;
  223. } while ((ret == 0) && len);
  224. return 0;
  225. }
  226. static u16 dib9000_i2c_read16(struct i2c_device *i2c, u16 reg)
  227. {
  228. struct i2c_msg msg[2] = {
  229. {.addr = i2c->i2c_addr >> 1, .flags = 0,
  230. .buf = i2c->i2c_write_buffer, .len = 2},
  231. {.addr = i2c->i2c_addr >> 1, .flags = I2C_M_RD,
  232. .buf = i2c->i2c_read_buffer, .len = 2},
  233. };
  234. i2c->i2c_write_buffer[0] = reg >> 8;
  235. i2c->i2c_write_buffer[1] = reg & 0xff;
  236. if (i2c_transfer(i2c->i2c_adap, msg, 2) != 2) {
  237. dprintk("read register %x error", reg);
  238. return 0;
  239. }
  240. return (i2c->i2c_read_buffer[0] << 8) | i2c->i2c_read_buffer[1];
  241. }
  242. static inline u16 dib9000_read_word(struct dib9000_state *state, u16 reg)
  243. {
  244. if (dib9000_read16_attr(state, reg, state->i2c_read_buffer, 2, 0) != 0)
  245. return 0;
  246. return (state->i2c_read_buffer[0] << 8) | state->i2c_read_buffer[1];
  247. }
  248. static inline u16 dib9000_read_word_attr(struct dib9000_state *state, u16 reg, u16 attribute)
  249. {
  250. if (dib9000_read16_attr(state, reg, state->i2c_read_buffer, 2,
  251. attribute) != 0)
  252. return 0;
  253. return (state->i2c_read_buffer[0] << 8) | state->i2c_read_buffer[1];
  254. }
  255. #define dib9000_read16_noinc_attr(state, reg, b, len, attribute) dib9000_read16_attr(state, reg, b, len, (attribute) | DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT)
  256. static u16 dib9000_write16_attr(struct dib9000_state *state, u16 reg, const u8 * buf, u32 len, u16 attribute)
  257. {
  258. u32 chunk_size = 126;
  259. u32 l;
  260. int ret;
  261. if (state->platform.risc.fw_is_running && (reg < 1024)) {
  262. if (dib9000_risc_apb_access_write
  263. (state, reg, DATA_BUS_ACCESS_MODE_16BIT | DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT | attribute, buf, len) != 0)
  264. return -EINVAL;
  265. return 0;
  266. }
  267. memset(&state->msg[0], 0, sizeof(struct i2c_msg));
  268. state->msg[0].addr = state->i2c.i2c_addr >> 1;
  269. state->msg[0].flags = 0;
  270. state->msg[0].buf = state->i2c_write_buffer;
  271. state->msg[0].len = len + 2;
  272. state->i2c_write_buffer[0] = (reg >> 8) & 0xff;
  273. state->i2c_write_buffer[1] = (reg) & 0xff;
  274. if (attribute & DATA_BUS_ACCESS_MODE_8BIT)
  275. state->i2c_write_buffer[0] |= (1 << 5);
  276. if (attribute & DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT)
  277. state->i2c_write_buffer[0] |= (1 << 4);
  278. do {
  279. l = len < chunk_size ? len : chunk_size;
  280. state->msg[0].len = l + 2;
  281. memcpy(&state->i2c_write_buffer[2], buf, l);
  282. ret = i2c_transfer(state->i2c.i2c_adap, state->msg, 1) != 1 ? -EREMOTEIO : 0;
  283. buf += l;
  284. len -= l;
  285. if (!(attribute & DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT))
  286. reg += l / 2;
  287. } while ((ret == 0) && len);
  288. return ret;
  289. }
  290. static int dib9000_i2c_write16(struct i2c_device *i2c, u16 reg, u16 val)
  291. {
  292. struct i2c_msg msg = {
  293. .addr = i2c->i2c_addr >> 1, .flags = 0,
  294. .buf = i2c->i2c_write_buffer, .len = 4
  295. };
  296. i2c->i2c_write_buffer[0] = (reg >> 8) & 0xff;
  297. i2c->i2c_write_buffer[1] = reg & 0xff;
  298. i2c->i2c_write_buffer[2] = (val >> 8) & 0xff;
  299. i2c->i2c_write_buffer[3] = val & 0xff;
  300. return i2c_transfer(i2c->i2c_adap, &msg, 1) != 1 ? -EREMOTEIO : 0;
  301. }
  302. static inline int dib9000_write_word(struct dib9000_state *state, u16 reg, u16 val)
  303. {
  304. u8 b[2] = { val >> 8, val & 0xff };
  305. return dib9000_write16_attr(state, reg, b, 2, 0);
  306. }
  307. static inline int dib9000_write_word_attr(struct dib9000_state *state, u16 reg, u16 val, u16 attribute)
  308. {
  309. u8 b[2] = { val >> 8, val & 0xff };
  310. return dib9000_write16_attr(state, reg, b, 2, attribute);
  311. }
  312. #define dib9000_write(state, reg, buf, len) dib9000_write16_attr(state, reg, buf, len, 0)
  313. #define dib9000_write16_noinc(state, reg, buf, len) dib9000_write16_attr(state, reg, buf, len, DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT)
  314. #define dib9000_write16_noinc_attr(state, reg, buf, len, attribute) dib9000_write16_attr(state, reg, buf, len, DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT | (attribute))
  315. #define dib9000_mbx_send(state, id, data, len) dib9000_mbx_send_attr(state, id, data, len, 0)
  316. #define dib9000_mbx_get_message(state, id, msg, len) dib9000_mbx_get_message_attr(state, id, msg, len, 0)
  317. #define MAC_IRQ (1 << 1)
  318. #define IRQ_POL_MSK (1 << 4)
  319. #define dib9000_risc_mem_read_chunks(state, b, len) dib9000_read16_attr(state, 1063, b, len, DATA_BUS_ACCESS_MODE_8BIT | DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT)
  320. #define dib9000_risc_mem_write_chunks(state, buf, len) dib9000_write16_attr(state, 1063, buf, len, DATA_BUS_ACCESS_MODE_8BIT | DATA_BUS_ACCESS_MODE_NO_ADDRESS_INCREMENT)
  321. static void dib9000_risc_mem_setup_cmd(struct dib9000_state *state, u32 addr, u32 len, u8 reading)
  322. {
  323. u8 b[14] = { 0 };
  324. /* dprintk("%d memcmd: %d %d %d\n", state->fe_id, addr, addr+len, len); */
  325. /* b[0] = 0 << 7; */
  326. b[1] = 1;
  327. /* b[2] = 0; */
  328. /* b[3] = 0; */
  329. b[4] = (u8) (addr >> 8);
  330. b[5] = (u8) (addr & 0xff);
  331. /* b[10] = 0; */
  332. /* b[11] = 0; */
  333. b[12] = (u8) (addr >> 8);
  334. b[13] = (u8) (addr & 0xff);
  335. addr += len;
  336. /* b[6] = 0; */
  337. /* b[7] = 0; */
  338. b[8] = (u8) (addr >> 8);
  339. b[9] = (u8) (addr & 0xff);
  340. dib9000_write(state, 1056, b, 14);
  341. if (reading)
  342. dib9000_write_word(state, 1056, (1 << 15) | 1);
  343. state->platform.risc.memcmd = -1; /* if it was called directly reset it - to force a future setup-call to set it */
  344. }
  345. static void dib9000_risc_mem_setup(struct dib9000_state *state, u8 cmd)
  346. {
  347. struct dib9000_fe_memory_map *m = &state->platform.risc.fe_mm[cmd & 0x7f];
  348. /* decide whether we need to "refresh" the memory controller */
  349. if (state->platform.risc.memcmd == cmd && /* same command */
  350. !(cmd & 0x80 && m->size < 67)) /* and we do not want to read something with less than 67 bytes looping - working around a bug in the memory controller */
  351. return;
  352. dib9000_risc_mem_setup_cmd(state, m->addr, m->size, cmd & 0x80);
  353. state->platform.risc.memcmd = cmd;
  354. }
  355. static int dib9000_risc_mem_read(struct dib9000_state *state, u8 cmd, u8 * b, u16 len)
  356. {
  357. if (!state->platform.risc.fw_is_running)
  358. return -EIO;
  359. DibAcquireLock(&state->platform.risc.mem_lock);
  360. dib9000_risc_mem_setup(state, cmd | 0x80);
  361. dib9000_risc_mem_read_chunks(state, b, len);
  362. DibReleaseLock(&state->platform.risc.mem_lock);
  363. return 0;
  364. }
  365. static int dib9000_risc_mem_write(struct dib9000_state *state, u8 cmd, const u8 * b)
  366. {
  367. struct dib9000_fe_memory_map *m = &state->platform.risc.fe_mm[cmd];
  368. if (!state->platform.risc.fw_is_running)
  369. return -EIO;
  370. DibAcquireLock(&state->platform.risc.mem_lock);
  371. dib9000_risc_mem_setup(state, cmd);
  372. dib9000_risc_mem_write_chunks(state, b, m->size);
  373. DibReleaseLock(&state->platform.risc.mem_lock);
  374. return 0;
  375. }
  376. static int dib9000_firmware_download(struct dib9000_state *state, u8 risc_id, u16 key, const u8 * code, u32 len)
  377. {
  378. u16 offs;
  379. if (risc_id == 1)
  380. offs = 16;
  381. else
  382. offs = 0;
  383. /* config crtl reg */
  384. dib9000_write_word(state, 1024 + offs, 0x000f);
  385. dib9000_write_word(state, 1025 + offs, 0);
  386. dib9000_write_word(state, 1031 + offs, key);
  387. dprintk("going to download %dB of microcode", len);
  388. if (dib9000_write16_noinc(state, 1026 + offs, (u8 *) code, (u16) len) != 0) {
  389. dprintk("error while downloading microcode for RISC %c", 'A' + risc_id);
  390. return -EIO;
  391. }
  392. dprintk("Microcode for RISC %c loaded", 'A' + risc_id);
  393. return 0;
  394. }
  395. static int dib9000_mbx_host_init(struct dib9000_state *state, u8 risc_id)
  396. {
  397. u16 mbox_offs;
  398. u16 reset_reg;
  399. u16 tries = 1000;
  400. if (risc_id == 1)
  401. mbox_offs = 16;
  402. else
  403. mbox_offs = 0;
  404. /* Reset mailbox */
  405. dib9000_write_word(state, 1027 + mbox_offs, 0x8000);
  406. /* Read reset status */
  407. do {
  408. reset_reg = dib9000_read_word(state, 1027 + mbox_offs);
  409. msleep(100);
  410. } while ((reset_reg & 0x8000) && --tries);
  411. if (reset_reg & 0x8000) {
  412. dprintk("MBX: init ERROR, no response from RISC %c", 'A' + risc_id);
  413. return -EIO;
  414. }
  415. dprintk("MBX: initialized");
  416. return 0;
  417. }
  418. #define MAX_MAILBOX_TRY 100
  419. static int dib9000_mbx_send_attr(struct dib9000_state *state, u8 id, u16 * data, u8 len, u16 attr)
  420. {
  421. u8 *d, b[2];
  422. u16 tmp;
  423. u16 size;
  424. u32 i;
  425. int ret = 0;
  426. if (!state->platform.risc.fw_is_running)
  427. return -EINVAL;
  428. DibAcquireLock(&state->platform.risc.mbx_if_lock);
  429. tmp = MAX_MAILBOX_TRY;
  430. do {
  431. size = dib9000_read_word_attr(state, 1043, attr) & 0xff;
  432. if ((size + len + 1) > MBX_MAX_WORDS && --tmp) {
  433. dprintk("MBX: RISC mbx full, retrying");
  434. msleep(100);
  435. } else
  436. break;
  437. } while (1);
  438. /*dprintk( "MBX: size: %d", size); */
  439. if (tmp == 0) {
  440. ret = -EINVAL;
  441. goto out;
  442. }
  443. #ifdef DUMP_MSG
  444. dprintk("--> %02x %d ", id, len + 1);
  445. for (i = 0; i < len; i++)
  446. dprintk("%04x ", data[i]);
  447. dprintk("\n");
  448. #endif
  449. /* byte-order conversion - works on big (where it is not necessary) or little endian */
  450. d = (u8 *) data;
  451. for (i = 0; i < len; i++) {
  452. tmp = data[i];
  453. *d++ = tmp >> 8;
  454. *d++ = tmp & 0xff;
  455. }
  456. /* write msg */
  457. b[0] = id;
  458. b[1] = len + 1;
  459. if (dib9000_write16_noinc_attr(state, 1045, b, 2, attr) != 0 || dib9000_write16_noinc_attr(state, 1045, (u8 *) data, len * 2, attr) != 0) {
  460. ret = -EIO;
  461. goto out;
  462. }
  463. /* update register nb_mes_in_RX */
  464. ret = (u8) dib9000_write_word_attr(state, 1043, 1 << 14, attr);
  465. out:
  466. DibReleaseLock(&state->platform.risc.mbx_if_lock);
  467. return ret;
  468. }
  469. static u8 dib9000_mbx_read(struct dib9000_state *state, u16 * data, u8 risc_id, u16 attr)
  470. {
  471. #ifdef DUMP_MSG
  472. u16 *d = data;
  473. #endif
  474. u16 tmp, i;
  475. u8 size;
  476. u8 mc_base;
  477. if (!state->platform.risc.fw_is_running)
  478. return 0;
  479. DibAcquireLock(&state->platform.risc.mbx_if_lock);
  480. if (risc_id == 1)
  481. mc_base = 16;
  482. else
  483. mc_base = 0;
  484. /* Length and type in the first word */
  485. *data = dib9000_read_word_attr(state, 1029 + mc_base, attr);
  486. size = *data & 0xff;
  487. if (size <= MBX_MAX_WORDS) {
  488. data++;
  489. size--; /* Initial word already read */
  490. dib9000_read16_noinc_attr(state, 1029 + mc_base, (u8 *) data, size * 2, attr);
  491. /* to word conversion */
  492. for (i = 0; i < size; i++) {
  493. tmp = *data;
  494. *data = (tmp >> 8) | (tmp << 8);
  495. data++;
  496. }
  497. #ifdef DUMP_MSG
  498. dprintk("<-- ");
  499. for (i = 0; i < size + 1; i++)
  500. dprintk("%04x ", d[i]);
  501. dprintk("\n");
  502. #endif
  503. } else {
  504. dprintk("MBX: message is too big for message cache (%d), flushing message", size);
  505. size--; /* Initial word already read */
  506. while (size--)
  507. dib9000_read16_noinc_attr(state, 1029 + mc_base, (u8 *) data, 2, attr);
  508. }
  509. /* Update register nb_mes_in_TX */
  510. dib9000_write_word_attr(state, 1028 + mc_base, 1 << 14, attr);
  511. DibReleaseLock(&state->platform.risc.mbx_if_lock);
  512. return size + 1;
  513. }
  514. static int dib9000_risc_debug_buf(struct dib9000_state *state, u16 * data, u8 size)
  515. {
  516. u32 ts = data[1] << 16 | data[0];
  517. char *b = (char *)&data[2];
  518. b[2 * (size - 2) - 1] = '\0'; /* Bullet proof the buffer */
  519. if (*b == '~') {
  520. b++;
  521. dprintk(b);
  522. } else
  523. dprintk("RISC%d: %d.%04d %s", state->fe_id, ts / 10000, ts % 10000, *b ? b : "<emtpy>");
  524. return 1;
  525. }
  526. static int dib9000_mbx_fetch_to_cache(struct dib9000_state *state, u16 attr)
  527. {
  528. int i;
  529. u8 size;
  530. u16 *block;
  531. /* find a free slot */
  532. for (i = 0; i < DIB9000_MSG_CACHE_SIZE; i++) {
  533. block = state->platform.risc.message_cache[i];
  534. if (*block == 0) {
  535. size = dib9000_mbx_read(state, block, 1, attr);
  536. /* dprintk( "MBX: fetched %04x message to cache", *block); */
  537. switch (*block >> 8) {
  538. case IN_MSG_DEBUG_BUF:
  539. dib9000_risc_debug_buf(state, block + 1, size); /* debug-messages are going to be printed right away */
  540. *block = 0; /* free the block */
  541. break;
  542. #if 0
  543. case IN_MSG_DATA: /* FE-TRACE */
  544. dib9000_risc_data_process(state, block + 1, size);
  545. *block = 0;
  546. break;
  547. #endif
  548. default:
  549. break;
  550. }
  551. return 1;
  552. }
  553. }
  554. dprintk("MBX: no free cache-slot found for new message...");
  555. return -1;
  556. }
  557. static u8 dib9000_mbx_count(struct dib9000_state *state, u8 risc_id, u16 attr)
  558. {
  559. if (risc_id == 0)
  560. return (u8) (dib9000_read_word_attr(state, 1028, attr) >> 10) & 0x1f; /* 5 bit field */
  561. else
  562. return (u8) (dib9000_read_word_attr(state, 1044, attr) >> 8) & 0x7f; /* 7 bit field */
  563. }
  564. static int dib9000_mbx_process(struct dib9000_state *state, u16 attr)
  565. {
  566. int ret = 0;
  567. u16 tmp;
  568. if (!state->platform.risc.fw_is_running)
  569. return -1;
  570. DibAcquireLock(&state->platform.risc.mbx_lock);
  571. if (dib9000_mbx_count(state, 1, attr)) /* 1=RiscB */
  572. ret = dib9000_mbx_fetch_to_cache(state, attr);
  573. tmp = dib9000_read_word_attr(state, 1229, attr); /* Clear the IRQ */
  574. /* if (tmp) */
  575. /* dprintk( "cleared IRQ: %x", tmp); */
  576. DibReleaseLock(&state->platform.risc.mbx_lock);
  577. return ret;
  578. }
  579. static int dib9000_mbx_get_message_attr(struct dib9000_state *state, u16 id, u16 * msg, u8 * size, u16 attr)
  580. {
  581. u8 i;
  582. u16 *block;
  583. u16 timeout = 30;
  584. *msg = 0;
  585. do {
  586. /* dib9000_mbx_get_from_cache(); */
  587. for (i = 0; i < DIB9000_MSG_CACHE_SIZE; i++) {
  588. block = state->platform.risc.message_cache[i];
  589. if ((*block >> 8) == id) {
  590. *size = (*block & 0xff) - 1;
  591. memcpy(msg, block + 1, (*size) * 2);
  592. *block = 0; /* free the block */
  593. i = 0; /* signal that we found a message */
  594. break;
  595. }
  596. }
  597. if (i == 0)
  598. break;
  599. if (dib9000_mbx_process(state, attr) == -1) /* try to fetch one message - if any */
  600. return -1;
  601. } while (--timeout);
  602. if (timeout == 0) {
  603. dprintk("waiting for message %d timed out", id);
  604. return -1;
  605. }
  606. return i == 0;
  607. }
  608. static int dib9000_risc_check_version(struct dib9000_state *state)
  609. {
  610. u8 r[4];
  611. u8 size;
  612. u16 fw_version = 0;
  613. if (dib9000_mbx_send(state, OUT_MSG_REQ_VERSION, &fw_version, 1) != 0)
  614. return -EIO;
  615. if (dib9000_mbx_get_message(state, IN_MSG_VERSION, (u16 *) r, &size) < 0)
  616. return -EIO;
  617. fw_version = (r[0] << 8) | r[1];
  618. dprintk("RISC: ver: %d.%02d (IC: %d)", fw_version >> 10, fw_version & 0x3ff, (r[2] << 8) | r[3]);
  619. if ((fw_version >> 10) != 7)
  620. return -EINVAL;
  621. switch (fw_version & 0x3ff) {
  622. case 11:
  623. case 12:
  624. case 14:
  625. case 15:
  626. case 16:
  627. case 17:
  628. break;
  629. default:
  630. dprintk("RISC: invalid firmware version");
  631. return -EINVAL;
  632. }
  633. dprintk("RISC: valid firmware version");
  634. return 0;
  635. }
  636. static int dib9000_fw_boot(struct dib9000_state *state, const u8 * codeA, u32 lenA, const u8 * codeB, u32 lenB)
  637. {
  638. /* Reconfig pool mac ram */
  639. dib9000_write_word(state, 1225, 0x02); /* A: 8k C, 4 k D - B: 32k C 6 k D - IRAM 96k */
  640. dib9000_write_word(state, 1226, 0x05);
  641. /* Toggles IP crypto to Host APB interface. */
  642. dib9000_write_word(state, 1542, 1);
  643. /* Set jump and no jump in the dma box */
  644. dib9000_write_word(state, 1074, 0);
  645. dib9000_write_word(state, 1075, 0);
  646. /* Set MAC as APB Master. */
  647. dib9000_write_word(state, 1237, 0);
  648. /* Reset the RISCs */
  649. if (codeA != NULL)
  650. dib9000_write_word(state, 1024, 2);
  651. else
  652. dib9000_write_word(state, 1024, 15);
  653. if (codeB != NULL)
  654. dib9000_write_word(state, 1040, 2);
  655. if (codeA != NULL)
  656. dib9000_firmware_download(state, 0, 0x1234, codeA, lenA);
  657. if (codeB != NULL)
  658. dib9000_firmware_download(state, 1, 0x1234, codeB, lenB);
  659. /* Run the RISCs */
  660. if (codeA != NULL)
  661. dib9000_write_word(state, 1024, 0);
  662. if (codeB != NULL)
  663. dib9000_write_word(state, 1040, 0);
  664. if (codeA != NULL)
  665. if (dib9000_mbx_host_init(state, 0) != 0)
  666. return -EIO;
  667. if (codeB != NULL)
  668. if (dib9000_mbx_host_init(state, 1) != 0)
  669. return -EIO;
  670. msleep(100);
  671. state->platform.risc.fw_is_running = 1;
  672. if (dib9000_risc_check_version(state) != 0)
  673. return -EINVAL;
  674. state->platform.risc.memcmd = 0xff;
  675. return 0;
  676. }
  677. static u16 dib9000_identify(struct i2c_device *client)
  678. {
  679. u16 value;
  680. value = dib9000_i2c_read16(client, 896);
  681. if (value != 0x01b3) {
  682. dprintk("wrong Vendor ID (0x%x)", value);
  683. return 0;
  684. }
  685. value = dib9000_i2c_read16(client, 897);
  686. if (value != 0x4000 && value != 0x4001 && value != 0x4002 && value != 0x4003 && value != 0x4004 && value != 0x4005) {
  687. dprintk("wrong Device ID (0x%x)", value);
  688. return 0;
  689. }
  690. /* protect this driver to be used with 7000PC */
  691. if (value == 0x4000 && dib9000_i2c_read16(client, 769) == 0x4000) {
  692. dprintk("this driver does not work with DiB7000PC");
  693. return 0;
  694. }
  695. switch (value) {
  696. case 0x4000:
  697. dprintk("found DiB7000MA/PA/MB/PB");
  698. break;
  699. case 0x4001:
  700. dprintk("found DiB7000HC");
  701. break;
  702. case 0x4002:
  703. dprintk("found DiB7000MC");
  704. break;
  705. case 0x4003:
  706. dprintk("found DiB9000A");
  707. break;
  708. case 0x4004:
  709. dprintk("found DiB9000H");
  710. break;
  711. case 0x4005:
  712. dprintk("found DiB9000M");
  713. break;
  714. }
  715. return value;
  716. }
  717. static void dib9000_set_power_mode(struct dib9000_state *state, enum dib9000_power_mode mode)
  718. {
  719. /* by default everything is going to be powered off */
  720. u16 reg_903 = 0x3fff, reg_904 = 0xffff, reg_905 = 0xffff, reg_906;
  721. u8 offset;
  722. if (state->revision == 0x4003 || state->revision == 0x4004 || state->revision == 0x4005)
  723. offset = 1;
  724. else
  725. offset = 0;
  726. reg_906 = dib9000_read_word(state, 906 + offset) | 0x3; /* keep settings for RISC */
  727. /* now, depending on the requested mode, we power on */
  728. switch (mode) {
  729. /* power up everything in the demod */
  730. case DIB9000_POWER_ALL:
  731. reg_903 = 0x0000;
  732. reg_904 = 0x0000;
  733. reg_905 = 0x0000;
  734. reg_906 = 0x0000;
  735. break;
  736. /* just leave power on the control-interfaces: GPIO and (I2C or SDIO or SRAM) */
  737. case DIB9000_POWER_INTERFACE_ONLY: /* TODO power up either SDIO or I2C or SRAM */
  738. reg_905 &= ~((1 << 7) | (1 << 6) | (1 << 5) | (1 << 2));
  739. break;
  740. case DIB9000_POWER_INTERF_ANALOG_AGC:
  741. reg_903 &= ~((1 << 15) | (1 << 14) | (1 << 11) | (1 << 10));
  742. reg_905 &= ~((1 << 7) | (1 << 6) | (1 << 5) | (1 << 4) | (1 << 2));
  743. reg_906 &= ~((1 << 0));
  744. break;
  745. case DIB9000_POWER_COR4_DINTLV_ICIRM_EQUAL_CFROD:
  746. reg_903 = 0x0000;
  747. reg_904 = 0x801f;
  748. reg_905 = 0x0000;
  749. reg_906 &= ~((1 << 0));
  750. break;
  751. case DIB9000_POWER_COR4_CRY_ESRAM_MOUT_NUD:
  752. reg_903 = 0x0000;
  753. reg_904 = 0x8000;
  754. reg_905 = 0x010b;
  755. reg_906 &= ~((1 << 0));
  756. break;
  757. default:
  758. case DIB9000_POWER_NO:
  759. break;
  760. }
  761. /* always power down unused parts */
  762. if (!state->platform.host.mobile_mode)
  763. reg_904 |= (1 << 7) | (1 << 6) | (1 << 4) | (1 << 2) | (1 << 1);
  764. /* P_sdio_select_clk = 0 on MC and after */
  765. if (state->revision != 0x4000)
  766. reg_906 <<= 1;
  767. dib9000_write_word(state, 903 + offset, reg_903);
  768. dib9000_write_word(state, 904 + offset, reg_904);
  769. dib9000_write_word(state, 905 + offset, reg_905);
  770. dib9000_write_word(state, 906 + offset, reg_906);
  771. }
  772. static int dib9000_fw_reset(struct dvb_frontend *fe)
  773. {
  774. struct dib9000_state *state = fe->demodulator_priv;
  775. dib9000_write_word(state, 1817, 0x0003);
  776. dib9000_write_word(state, 1227, 1);
  777. dib9000_write_word(state, 1227, 0);
  778. switch ((state->revision = dib9000_identify(&state->i2c))) {
  779. case 0x4003:
  780. case 0x4004:
  781. case 0x4005:
  782. state->reg_offs = 1;
  783. break;
  784. default:
  785. return -EINVAL;
  786. }
  787. /* reset the i2c-master to use the host interface */
  788. dibx000_reset_i2c_master(&state->i2c_master);
  789. dib9000_set_power_mode(state, DIB9000_POWER_ALL);
  790. /* unforce divstr regardless whether i2c enumeration was done or not */
  791. dib9000_write_word(state, 1794, dib9000_read_word(state, 1794) & ~(1 << 1));
  792. dib9000_write_word(state, 1796, 0);
  793. dib9000_write_word(state, 1805, 0x805);
  794. /* restart all parts */
  795. dib9000_write_word(state, 898, 0xffff);
  796. dib9000_write_word(state, 899, 0xffff);
  797. dib9000_write_word(state, 900, 0x0001);
  798. dib9000_write_word(state, 901, 0xff19);
  799. dib9000_write_word(state, 902, 0x003c);
  800. dib9000_write_word(state, 898, 0);
  801. dib9000_write_word(state, 899, 0);
  802. dib9000_write_word(state, 900, 0);
  803. dib9000_write_word(state, 901, 0);
  804. dib9000_write_word(state, 902, 0);
  805. dib9000_write_word(state, 911, state->chip.d9.cfg.if_drives);
  806. dib9000_set_power_mode(state, DIB9000_POWER_INTERFACE_ONLY);
  807. return 0;
  808. }
  809. static int dib9000_risc_apb_access_read(struct dib9000_state *state, u32 address, u16 attribute, const u8 * tx, u32 txlen, u8 * b, u32 len)
  810. {
  811. u16 mb[10];
  812. u8 i, s;
  813. if (address >= 1024 || !state->platform.risc.fw_is_running)
  814. return -EINVAL;
  815. /* dprintk( "APB access thru rd fw %d %x", address, attribute); */
  816. mb[0] = (u16) address;
  817. mb[1] = len / 2;
  818. dib9000_mbx_send_attr(state, OUT_MSG_BRIDGE_APB_R, mb, 2, attribute);
  819. switch (dib9000_mbx_get_message_attr(state, IN_MSG_END_BRIDGE_APB_RW, mb, &s, attribute)) {
  820. case 1:
  821. s--;
  822. for (i = 0; i < s; i++) {
  823. b[i * 2] = (mb[i + 1] >> 8) & 0xff;
  824. b[i * 2 + 1] = (mb[i + 1]) & 0xff;
  825. }
  826. return 0;
  827. default:
  828. return -EIO;
  829. }
  830. return -EIO;
  831. }
  832. static int dib9000_risc_apb_access_write(struct dib9000_state *state, u32 address, u16 attribute, const u8 * b, u32 len)
  833. {
  834. u16 mb[10];
  835. u8 s, i;
  836. if (address >= 1024 || !state->platform.risc.fw_is_running)
  837. return -EINVAL;
  838. /* dprintk( "APB access thru wr fw %d %x", address, attribute); */
  839. mb[0] = (unsigned short)address;
  840. for (i = 0; i < len && i < 20; i += 2)
  841. mb[1 + (i / 2)] = (b[i] << 8 | b[i + 1]);
  842. dib9000_mbx_send_attr(state, OUT_MSG_BRIDGE_APB_W, mb, 1 + len / 2, attribute);
  843. return dib9000_mbx_get_message_attr(state, IN_MSG_END_BRIDGE_APB_RW, mb, &s, attribute) == 1 ? 0 : -EINVAL;
  844. }
  845. static int dib9000_fw_memmbx_sync(struct dib9000_state *state, u8 i)
  846. {
  847. u8 index_loop = 10;
  848. if (!state->platform.risc.fw_is_running)
  849. return 0;
  850. dib9000_risc_mem_write(state, FE_MM_RW_SYNC, &i);
  851. do {
  852. dib9000_risc_mem_read(state, FE_MM_RW_SYNC, state->i2c_read_buffer, 1);
  853. } while (state->i2c_read_buffer[0] && index_loop--);
  854. if (index_loop > 0)
  855. return 0;
  856. return -EIO;
  857. }
  858. static int dib9000_fw_init(struct dib9000_state *state)
  859. {
  860. struct dibGPIOFunction *f;
  861. u16 b[40] = { 0 };
  862. u8 i;
  863. u8 size;
  864. if (dib9000_fw_boot(state, NULL, 0, state->chip.d9.cfg.microcode_B_fe_buffer, state->chip.d9.cfg.microcode_B_fe_size) != 0)
  865. return -EIO;
  866. /* initialize the firmware */
  867. for (i = 0; i < ARRAY_SIZE(state->chip.d9.cfg.gpio_function); i++) {
  868. f = &state->chip.d9.cfg.gpio_function[i];
  869. if (f->mask) {
  870. switch (f->function) {
  871. case BOARD_GPIO_FUNCTION_COMPONENT_ON:
  872. b[0] = (u16) f->mask;
  873. b[1] = (u16) f->direction;
  874. b[2] = (u16) f->value;
  875. break;
  876. case BOARD_GPIO_FUNCTION_COMPONENT_OFF:
  877. b[3] = (u16) f->mask;
  878. b[4] = (u16) f->direction;
  879. b[5] = (u16) f->value;
  880. break;
  881. }
  882. }
  883. }
  884. if (dib9000_mbx_send(state, OUT_MSG_CONF_GPIO, b, 15) != 0)
  885. return -EIO;
  886. /* subband */
  887. b[0] = state->chip.d9.cfg.subband.size; /* type == 0 -> GPIO - PWM not yet supported */
  888. for (i = 0; i < state->chip.d9.cfg.subband.size; i++) {
  889. b[1 + i * 4] = state->chip.d9.cfg.subband.subband[i].f_mhz;
  890. b[2 + i * 4] = (u16) state->chip.d9.cfg.subband.subband[i].gpio.mask;
  891. b[3 + i * 4] = (u16) state->chip.d9.cfg.subband.subband[i].gpio.direction;
  892. b[4 + i * 4] = (u16) state->chip.d9.cfg.subband.subband[i].gpio.value;
  893. }
  894. b[1 + i * 4] = 0; /* fe_id */
  895. if (dib9000_mbx_send(state, OUT_MSG_SUBBAND_SEL, b, 2 + 4 * i) != 0)
  896. return -EIO;
  897. /* 0 - id, 1 - no_of_frontends */
  898. b[0] = (0 << 8) | 1;
  899. /* 0 = i2c-address demod, 0 = tuner */
  900. b[1] = (0 << 8) | (0);
  901. b[2] = (u16) (((state->chip.d9.cfg.xtal_clock_khz * 1000) >> 16) & 0xffff);
  902. b[3] = (u16) (((state->chip.d9.cfg.xtal_clock_khz * 1000)) & 0xffff);
  903. b[4] = (u16) ((state->chip.d9.cfg.vcxo_timer >> 16) & 0xffff);
  904. b[5] = (u16) ((state->chip.d9.cfg.vcxo_timer) & 0xffff);
  905. b[6] = (u16) ((state->chip.d9.cfg.timing_frequency >> 16) & 0xffff);
  906. b[7] = (u16) ((state->chip.d9.cfg.timing_frequency) & 0xffff);
  907. b[29] = state->chip.d9.cfg.if_drives;
  908. if (dib9000_mbx_send(state, OUT_MSG_INIT_DEMOD, b, ARRAY_SIZE(b)) != 0)
  909. return -EIO;
  910. if (dib9000_mbx_send(state, OUT_MSG_FE_FW_DL, NULL, 0) != 0)
  911. return -EIO;
  912. if (dib9000_mbx_get_message(state, IN_MSG_FE_FW_DL_DONE, b, &size) < 0)
  913. return -EIO;
  914. if (size > ARRAY_SIZE(b)) {
  915. dprintk("error : firmware returned %dbytes needed but the used buffer has only %dbytes\n Firmware init ABORTED", size,
  916. (int)ARRAY_SIZE(b));
  917. return -EINVAL;
  918. }
  919. for (i = 0; i < size; i += 2) {
  920. state->platform.risc.fe_mm[i / 2].addr = b[i + 0];
  921. state->platform.risc.fe_mm[i / 2].size = b[i + 1];
  922. }
  923. return 0;
  924. }
  925. static void dib9000_fw_set_channel_head(struct dib9000_state *state, struct dvb_frontend_parameters *ch)
  926. {
  927. u8 b[9];
  928. u32 freq = state->fe[0]->dtv_property_cache.frequency / 1000;
  929. if (state->fe_id % 2)
  930. freq += 101;
  931. b[0] = (u8) ((freq >> 0) & 0xff);
  932. b[1] = (u8) ((freq >> 8) & 0xff);
  933. b[2] = (u8) ((freq >> 16) & 0xff);
  934. b[3] = (u8) ((freq >> 24) & 0xff);
  935. b[4] = (u8) ((state->fe[0]->dtv_property_cache.bandwidth_hz / 1000 >> 0) & 0xff);
  936. b[5] = (u8) ((state->fe[0]->dtv_property_cache.bandwidth_hz / 1000 >> 8) & 0xff);
  937. b[6] = (u8) ((state->fe[0]->dtv_property_cache.bandwidth_hz / 1000 >> 16) & 0xff);
  938. b[7] = (u8) ((state->fe[0]->dtv_property_cache.bandwidth_hz / 1000 >> 24) & 0xff);
  939. b[8] = 0x80; /* do not wait for CELL ID when doing autosearch */
  940. if (state->fe[0]->dtv_property_cache.delivery_system == SYS_DVBT)
  941. b[8] |= 1;
  942. dib9000_risc_mem_write(state, FE_MM_W_CHANNEL_HEAD, b);
  943. }
  944. static int dib9000_fw_get_channel(struct dvb_frontend *fe, struct dvb_frontend_parameters *channel)
  945. {
  946. struct dib9000_state *state = fe->demodulator_priv;
  947. struct dibDVBTChannel {
  948. s8 spectrum_inversion;
  949. s8 nfft;
  950. s8 guard;
  951. s8 constellation;
  952. s8 hrch;
  953. s8 alpha;
  954. s8 code_rate_hp;
  955. s8 code_rate_lp;
  956. s8 select_hp;
  957. s8 intlv_native;
  958. };
  959. struct dibDVBTChannel *ch;
  960. int ret = 0;
  961. DibAcquireLock(&state->platform.risc.mem_mbx_lock);
  962. if (dib9000_fw_memmbx_sync(state, FE_SYNC_CHANNEL) < 0) {
  963. goto error;
  964. ret = -EIO;
  965. }
  966. dib9000_risc_mem_read(state, FE_MM_R_CHANNEL_UNION,
  967. state->i2c_read_buffer, sizeof(struct dibDVBTChannel));
  968. ch = (struct dibDVBTChannel *)state->i2c_read_buffer;
  969. switch (ch->spectrum_inversion & 0x7) {
  970. case 1:
  971. state->fe[0]->dtv_property_cache.inversion = INVERSION_ON;
  972. break;
  973. case 0:
  974. state->fe[0]->dtv_property_cache.inversion = INVERSION_OFF;
  975. break;
  976. default:
  977. case -1:
  978. state->fe[0]->dtv_property_cache.inversion = INVERSION_AUTO;
  979. break;
  980. }
  981. switch (ch->nfft) {
  982. case 0:
  983. state->fe[0]->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_2K;
  984. break;
  985. case 2:
  986. state->fe[0]->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_4K;
  987. break;
  988. case 1:
  989. state->fe[0]->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_8K;
  990. break;
  991. default:
  992. case -1:
  993. state->fe[0]->dtv_property_cache.transmission_mode = TRANSMISSION_MODE_AUTO;
  994. break;
  995. }
  996. switch (ch->guard) {
  997. case 0:
  998. state->fe[0]->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_32;
  999. break;
  1000. case 1:
  1001. state->fe[0]->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_16;
  1002. break;
  1003. case 2:
  1004. state->fe[0]->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_8;
  1005. break;
  1006. case 3:
  1007. state->fe[0]->dtv_property_cache.guard_interval = GUARD_INTERVAL_1_4;
  1008. break;
  1009. default:
  1010. case -1:
  1011. state->fe[0]->dtv_property_cache.guard_interval = GUARD_INTERVAL_AUTO;
  1012. break;
  1013. }
  1014. switch (ch->constellation) {
  1015. case 2:
  1016. state->fe[0]->dtv_property_cache.modulation = QAM_64;
  1017. break;
  1018. case 1:
  1019. state->fe[0]->dtv_property_cache.modulation = QAM_16;
  1020. break;
  1021. case 0:
  1022. state->fe[0]->dtv_property_cache.modulation = QPSK;
  1023. break;
  1024. default:
  1025. case -1:
  1026. state->fe[0]->dtv_property_cache.modulation = QAM_AUTO;
  1027. break;
  1028. }
  1029. switch (ch->hrch) {
  1030. case 0:
  1031. state->fe[0]->dtv_property_cache.hierarchy = HIERARCHY_NONE;
  1032. break;
  1033. case 1:
  1034. state->fe[0]->dtv_property_cache.hierarchy = HIERARCHY_1;
  1035. break;
  1036. default:
  1037. case -1:
  1038. state->fe[0]->dtv_property_cache.hierarchy = HIERARCHY_AUTO;
  1039. break;
  1040. }
  1041. switch (ch->code_rate_hp) {
  1042. case 1:
  1043. state->fe[0]->dtv_property_cache.code_rate_HP = FEC_1_2;
  1044. break;
  1045. case 2:
  1046. state->fe[0]->dtv_property_cache.code_rate_HP = FEC_2_3;
  1047. break;
  1048. case 3:
  1049. state->fe[0]->dtv_property_cache.code_rate_HP = FEC_3_4;
  1050. break;
  1051. case 5:
  1052. state->fe[0]->dtv_property_cache.code_rate_HP = FEC_5_6;
  1053. break;
  1054. case 7:
  1055. state->fe[0]->dtv_property_cache.code_rate_HP = FEC_7_8;
  1056. break;
  1057. default:
  1058. case -1:
  1059. state->fe[0]->dtv_property_cache.code_rate_HP = FEC_AUTO;
  1060. break;
  1061. }
  1062. switch (ch->code_rate_lp) {
  1063. case 1:
  1064. state->fe[0]->dtv_property_cache.code_rate_LP = FEC_1_2;
  1065. break;
  1066. case 2:
  1067. state->fe[0]->dtv_property_cache.code_rate_LP = FEC_2_3;
  1068. break;
  1069. case 3:
  1070. state->fe[0]->dtv_property_cache.code_rate_LP = FEC_3_4;
  1071. break;
  1072. case 5:
  1073. state->fe[0]->dtv_property_cache.code_rate_LP = FEC_5_6;
  1074. break;
  1075. case 7:
  1076. state->fe[0]->dtv_property_cache.code_rate_LP = FEC_7_8;
  1077. break;
  1078. default:
  1079. case -1:
  1080. state->fe[0]->dtv_property_cache.code_rate_LP = FEC_AUTO;
  1081. break;
  1082. }
  1083. error:
  1084. DibReleaseLock(&state->platform.risc.mem_mbx_lock);
  1085. return ret;
  1086. }
  1087. static int dib9000_fw_set_channel_union(struct dvb_frontend *fe, struct dvb_frontend_parameters *channel)
  1088. {
  1089. struct dib9000_state *state = fe->demodulator_priv;
  1090. struct dibDVBTChannel {
  1091. s8 spectrum_inversion;
  1092. s8 nfft;
  1093. s8 guard;
  1094. s8 constellation;
  1095. s8 hrch;
  1096. s8 alpha;
  1097. s8 code_rate_hp;
  1098. s8 code_rate_lp;
  1099. s8 select_hp;
  1100. s8 intlv_native;
  1101. };
  1102. struct dibDVBTChannel ch;
  1103. switch (state->fe[0]->dtv_property_cache.inversion) {
  1104. case INVERSION_ON:
  1105. ch.spectrum_inversion = 1;
  1106. break;
  1107. case INVERSION_OFF:
  1108. ch.spectrum_inversion = 0;
  1109. break;
  1110. default:
  1111. case INVERSION_AUTO:
  1112. ch.spectrum_inversion = -1;
  1113. break;
  1114. }
  1115. switch (state->fe[0]->dtv_property_cache.transmission_mode) {
  1116. case TRANSMISSION_MODE_2K:
  1117. ch.nfft = 0;
  1118. break;
  1119. case TRANSMISSION_MODE_4K:
  1120. ch.nfft = 2;
  1121. break;
  1122. case TRANSMISSION_MODE_8K:
  1123. ch.nfft = 1;
  1124. break;
  1125. default:
  1126. case TRANSMISSION_MODE_AUTO:
  1127. ch.nfft = 1;
  1128. break;
  1129. }
  1130. switch (state->fe[0]->dtv_property_cache.guard_interval) {
  1131. case GUARD_INTERVAL_1_32:
  1132. ch.guard = 0;
  1133. break;
  1134. case GUARD_INTERVAL_1_16:
  1135. ch.guard = 1;
  1136. break;
  1137. case GUARD_INTERVAL_1_8:
  1138. ch.guard = 2;
  1139. break;
  1140. case GUARD_INTERVAL_1_4:
  1141. ch.guard = 3;
  1142. break;
  1143. default:
  1144. case GUARD_INTERVAL_AUTO:
  1145. ch.guard = -1;
  1146. break;
  1147. }
  1148. switch (state->fe[0]->dtv_property_cache.modulation) {
  1149. case QAM_64:
  1150. ch.constellation = 2;
  1151. break;
  1152. case QAM_16:
  1153. ch.constellation = 1;
  1154. break;
  1155. case QPSK:
  1156. ch.constellation = 0;
  1157. break;
  1158. default:
  1159. case QAM_AUTO:
  1160. ch.constellation = -1;
  1161. break;
  1162. }
  1163. switch (state->fe[0]->dtv_property_cache.hierarchy) {
  1164. case HIERARCHY_NONE:
  1165. ch.hrch = 0;
  1166. break;
  1167. case HIERARCHY_1:
  1168. case HIERARCHY_2:
  1169. case HIERARCHY_4:
  1170. ch.hrch = 1;
  1171. break;
  1172. default:
  1173. case HIERARCHY_AUTO:
  1174. ch.hrch = -1;
  1175. break;
  1176. }
  1177. ch.alpha = 1;
  1178. switch (state->fe[0]->dtv_property_cache.code_rate_HP) {
  1179. case FEC_1_2:
  1180. ch.code_rate_hp = 1;
  1181. break;
  1182. case FEC_2_3:
  1183. ch.code_rate_hp = 2;
  1184. break;
  1185. case FEC_3_4:
  1186. ch.code_rate_hp = 3;
  1187. break;
  1188. case FEC_5_6:
  1189. ch.code_rate_hp = 5;
  1190. break;
  1191. case FEC_7_8:
  1192. ch.code_rate_hp = 7;
  1193. break;
  1194. default:
  1195. case FEC_AUTO:
  1196. ch.code_rate_hp = -1;
  1197. break;
  1198. }
  1199. switch (state->fe[0]->dtv_property_cache.code_rate_LP) {
  1200. case FEC_1_2:
  1201. ch.code_rate_lp = 1;
  1202. break;
  1203. case FEC_2_3:
  1204. ch.code_rate_lp = 2;
  1205. break;
  1206. case FEC_3_4:
  1207. ch.code_rate_lp = 3;
  1208. break;
  1209. case FEC_5_6:
  1210. ch.code_rate_lp = 5;
  1211. break;
  1212. case FEC_7_8:
  1213. ch.code_rate_lp = 7;
  1214. break;
  1215. default:
  1216. case FEC_AUTO:
  1217. ch.code_rate_lp = -1;
  1218. break;
  1219. }
  1220. ch.select_hp = 1;
  1221. ch.intlv_native = 1;
  1222. dib9000_risc_mem_write(state, FE_MM_W_CHANNEL_UNION, (u8 *) &ch);
  1223. return 0;
  1224. }
  1225. static int dib9000_fw_tune(struct dvb_frontend *fe, struct dvb_frontend_parameters *ch)
  1226. {
  1227. struct dib9000_state *state = fe->demodulator_priv;
  1228. int ret = 10, search = state->channel_status.status == CHANNEL_STATUS_PARAMETERS_UNKNOWN;
  1229. s8 i;
  1230. switch (state->tune_state) {
  1231. case CT_DEMOD_START:
  1232. dib9000_fw_set_channel_head(state, ch);
  1233. /* write the channel context - a channel is initialized to 0, so it is OK */
  1234. dib9000_risc_mem_write(state, FE_MM_W_CHANNEL_CONTEXT, (u8 *) fe_info);
  1235. dib9000_risc_mem_write(state, FE_MM_W_FE_INFO, (u8 *) fe_info);
  1236. if (search)
  1237. dib9000_mbx_send(state, OUT_MSG_FE_CHANNEL_SEARCH, NULL, 0);
  1238. else {
  1239. dib9000_fw_set_channel_union(fe, ch);
  1240. dib9000_mbx_send(state, OUT_MSG_FE_CHANNEL_TUNE, NULL, 0);
  1241. }
  1242. state->tune_state = CT_DEMOD_STEP_1;
  1243. break;
  1244. case CT_DEMOD_STEP_1:
  1245. if (search)
  1246. dib9000_risc_mem_read(state, FE_MM_R_CHANNEL_SEARCH_STATE, state->i2c_read_buffer, 1);
  1247. else
  1248. dib9000_risc_mem_read(state, FE_MM_R_CHANNEL_TUNE_STATE, state->i2c_read_buffer, 1);
  1249. i = (s8)state->i2c_read_buffer[0];
  1250. switch (i) { /* something happened */
  1251. case 0:
  1252. break;
  1253. case -2: /* tps locks are "slower" than MPEG locks -> even in autosearch data is OK here */
  1254. if (search)
  1255. state->status = FE_STATUS_DEMOD_SUCCESS;
  1256. else {
  1257. state->tune_state = CT_DEMOD_STOP;
  1258. state->status = FE_STATUS_LOCKED;
  1259. }
  1260. break;
  1261. default:
  1262. state->status = FE_STATUS_TUNE_FAILED;
  1263. state->tune_state = CT_DEMOD_STOP;
  1264. break;
  1265. }
  1266. break;
  1267. default:
  1268. ret = FE_CALLBACK_TIME_NEVER;
  1269. break;
  1270. }
  1271. return ret;
  1272. }
  1273. static int dib9000_fw_set_diversity_in(struct dvb_frontend *fe, int onoff)
  1274. {
  1275. struct dib9000_state *state = fe->demodulator_priv;
  1276. u16 mode = (u16) onoff;
  1277. return dib9000_mbx_send(state, OUT_MSG_ENABLE_DIVERSITY, &mode, 1);
  1278. }
  1279. static int dib9000_fw_set_output_mode(struct dvb_frontend *fe, int mode)
  1280. {
  1281. struct dib9000_state *state = fe->demodulator_priv;
  1282. u16 outreg, smo_mode;
  1283. dprintk("setting output mode for demod %p to %d", fe, mode);
  1284. switch (mode) {
  1285. case OUTMODE_MPEG2_PAR_GATED_CLK:
  1286. outreg = (1 << 10); /* 0x0400 */
  1287. break;
  1288. case OUTMODE_MPEG2_PAR_CONT_CLK:
  1289. outreg = (1 << 10) | (1 << 6); /* 0x0440 */
  1290. break;
  1291. case OUTMODE_MPEG2_SERIAL:
  1292. outreg = (1 << 10) | (2 << 6) | (0 << 1); /* 0x0482 */
  1293. break;
  1294. case OUTMODE_DIVERSITY:
  1295. outreg = (1 << 10) | (4 << 6); /* 0x0500 */
  1296. break;
  1297. case OUTMODE_MPEG2_FIFO:
  1298. outreg = (1 << 10) | (5 << 6);
  1299. break;
  1300. case OUTMODE_HIGH_Z:
  1301. outreg = 0;
  1302. break;
  1303. default:
  1304. dprintk("Unhandled output_mode passed to be set for demod %p", &state->fe[0]);
  1305. return -EINVAL;
  1306. }
  1307. dib9000_write_word(state, 1795, outreg);
  1308. switch (mode) {
  1309. case OUTMODE_MPEG2_PAR_GATED_CLK:
  1310. case OUTMODE_MPEG2_PAR_CONT_CLK:
  1311. case OUTMODE_MPEG2_SERIAL:
  1312. case OUTMODE_MPEG2_FIFO:
  1313. smo_mode = (dib9000_read_word(state, 295) & 0x0010) | (1 << 1);
  1314. if (state->chip.d9.cfg.output_mpeg2_in_188_bytes)
  1315. smo_mode |= (1 << 5);
  1316. dib9000_write_word(state, 295, smo_mode);
  1317. break;
  1318. }
  1319. outreg = to_fw_output_mode(mode);
  1320. return dib9000_mbx_send(state, OUT_MSG_SET_OUTPUT_MODE, &outreg, 1);
  1321. }
  1322. static int dib9000_tuner_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
  1323. {
  1324. struct dib9000_state *state = i2c_get_adapdata(i2c_adap);
  1325. u16 i, len, t, index_msg;
  1326. for (index_msg = 0; index_msg < num; index_msg++) {
  1327. if (msg[index_msg].flags & I2C_M_RD) { /* read */
  1328. len = msg[index_msg].len;
  1329. if (len > 16)
  1330. len = 16;
  1331. if (dib9000_read_word(state, 790) != 0)
  1332. dprintk("TunerITF: read busy");
  1333. dib9000_write_word(state, 784, (u16) (msg[index_msg].addr));
  1334. dib9000_write_word(state, 787, (len / 2) - 1);
  1335. dib9000_write_word(state, 786, 1); /* start read */
  1336. i = 1000;
  1337. while (dib9000_read_word(state, 790) != (len / 2) && i)
  1338. i--;
  1339. if (i == 0)
  1340. dprintk("TunerITF: read failed");
  1341. for (i = 0; i < len; i += 2) {
  1342. t = dib9000_read_word(state, 785);
  1343. msg[index_msg].buf[i] = (t >> 8) & 0xff;
  1344. msg[index_msg].buf[i + 1] = (t) & 0xff;
  1345. }
  1346. if (dib9000_read_word(state, 790) != 0)
  1347. dprintk("TunerITF: read more data than expected");
  1348. } else {
  1349. i = 1000;
  1350. while (dib9000_read_word(state, 789) && i)
  1351. i--;
  1352. if (i == 0)
  1353. dprintk("TunerITF: write busy");
  1354. len = msg[index_msg].len;
  1355. if (len > 16)
  1356. len = 16;
  1357. for (i = 0; i < len; i += 2)
  1358. dib9000_write_word(state, 785, (msg[index_msg].buf[i] << 8) | msg[index_msg].buf[i + 1]);
  1359. dib9000_write_word(state, 784, (u16) msg[index_msg].addr);
  1360. dib9000_write_word(state, 787, (len / 2) - 1);
  1361. dib9000_write_word(state, 786, 0); /* start write */
  1362. i = 1000;
  1363. while (dib9000_read_word(state, 791) > 0 && i)
  1364. i--;
  1365. if (i == 0)
  1366. dprintk("TunerITF: write failed");
  1367. }
  1368. }
  1369. return num;
  1370. }
  1371. int dib9000_fw_set_component_bus_speed(struct dvb_frontend *fe, u16 speed)
  1372. {
  1373. struct dib9000_state *state = fe->demodulator_priv;
  1374. state->component_bus_speed = speed;
  1375. return 0;
  1376. }
  1377. EXPORT_SYMBOL(dib9000_fw_set_component_bus_speed);
  1378. static int dib9000_fw_component_bus_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
  1379. {
  1380. struct dib9000_state *state = i2c_get_adapdata(i2c_adap);
  1381. u8 type = 0; /* I2C */
  1382. u8 port = DIBX000_I2C_INTERFACE_GPIO_3_4;
  1383. u16 scl = state->component_bus_speed; /* SCL frequency */
  1384. struct dib9000_fe_memory_map *m = &state->platform.risc.fe_mm[FE_MM_RW_COMPONENT_ACCESS_BUFFER];
  1385. u8 p[13] = { 0 };
  1386. p[0] = type;
  1387. p[1] = port;
  1388. p[2] = msg[0].addr << 1;
  1389. p[3] = (u8) scl & 0xff; /* scl */
  1390. p[4] = (u8) (scl >> 8);
  1391. p[7] = 0;
  1392. p[8] = 0;
  1393. p[9] = (u8) (msg[0].len);
  1394. p[10] = (u8) (msg[0].len >> 8);
  1395. if ((num > 1) && (msg[1].flags & I2C_M_RD)) {
  1396. p[11] = (u8) (msg[1].len);
  1397. p[12] = (u8) (msg[1].len >> 8);
  1398. } else {
  1399. p[11] = 0;
  1400. p[12] = 0;
  1401. }
  1402. DibAcquireLock(&state->platform.risc.mem_mbx_lock);
  1403. dib9000_risc_mem_write(state, FE_MM_W_COMPONENT_ACCESS, p);
  1404. { /* write-part */
  1405. dib9000_risc_mem_setup_cmd(state, m->addr, msg[0].len, 0);
  1406. dib9000_risc_mem_write_chunks(state, msg[0].buf, msg[0].len);
  1407. }
  1408. /* do the transaction */
  1409. if (dib9000_fw_memmbx_sync(state, FE_SYNC_COMPONENT_ACCESS) < 0) {
  1410. DibReleaseLock(&state->platform.risc.mem_mbx_lock);
  1411. return 0;
  1412. }
  1413. /* read back any possible result */
  1414. if ((num > 1) && (msg[1].flags & I2C_M_RD))
  1415. dib9000_risc_mem_read(state, FE_MM_RW_COMPONENT_ACCESS_BUFFER, msg[1].buf, msg[1].len);
  1416. DibReleaseLock(&state->platform.risc.mem_mbx_lock);
  1417. return num;
  1418. }
  1419. static u32 dib9000_i2c_func(struct i2c_adapter *adapter)
  1420. {
  1421. return I2C_FUNC_I2C;
  1422. }
  1423. static struct i2c_algorithm dib9000_tuner_algo = {
  1424. .master_xfer = dib9000_tuner_xfer,
  1425. .functionality = dib9000_i2c_func,
  1426. };
  1427. static struct i2c_algorithm dib9000_component_bus_algo = {
  1428. .master_xfer = dib9000_fw_component_bus_xfer,
  1429. .functionality = dib9000_i2c_func,
  1430. };
  1431. struct i2c_adapter *dib9000_get_tuner_interface(struct dvb_frontend *fe)
  1432. {
  1433. struct dib9000_state *st = fe->demodulator_priv;
  1434. return &st->tuner_adap;
  1435. }
  1436. EXPORT_SYMBOL(dib9000_get_tuner_interface);
  1437. struct i2c_adapter *dib9000_get_component_bus_interface(struct dvb_frontend *fe)
  1438. {
  1439. struct dib9000_state *st = fe->demodulator_priv;
  1440. return &st->component_bus;
  1441. }
  1442. EXPORT_SYMBOL(dib9000_get_component_bus_interface);
  1443. struct i2c_adapter *dib9000_get_i2c_master(struct dvb_frontend *fe, enum dibx000_i2c_interface intf, int gating)
  1444. {
  1445. struct dib9000_state *st = fe->demodulator_priv;
  1446. return dibx000_get_i2c_adapter(&st->i2c_master, intf, gating);
  1447. }
  1448. EXPORT_SYMBOL(dib9000_get_i2c_master);
  1449. int dib9000_set_i2c_adapter(struct dvb_frontend *fe, struct i2c_adapter *i2c)
  1450. {
  1451. struct dib9000_state *st = fe->demodulator_priv;
  1452. st->i2c.i2c_adap = i2c;
  1453. return 0;
  1454. }
  1455. EXPORT_SYMBOL(dib9000_set_i2c_adapter);
  1456. static int dib9000_cfg_gpio(struct dib9000_state *st, u8 num, u8 dir, u8 val)
  1457. {
  1458. st->gpio_dir = dib9000_read_word(st, 773);
  1459. st->gpio_dir &= ~(1 << num); /* reset the direction bit */
  1460. st->gpio_dir |= (dir & 0x1) << num; /* set the new direction */
  1461. dib9000_write_word(st, 773, st->gpio_dir);
  1462. st->gpio_val = dib9000_read_word(st, 774);
  1463. st->gpio_val &= ~(1 << num); /* reset the direction bit */
  1464. st->gpio_val |= (val & 0x01) << num; /* set the new value */
  1465. dib9000_write_word(st, 774, st->gpio_val);
  1466. dprintk("gpio dir: %04x: gpio val: %04x", st->gpio_dir, st->gpio_val);
  1467. return 0;
  1468. }
  1469. int dib9000_set_gpio(struct dvb_frontend *fe, u8 num, u8 dir, u8 val)
  1470. {
  1471. struct dib9000_state *state = fe->demodulator_priv;
  1472. return dib9000_cfg_gpio(state, num, dir, val);
  1473. }
  1474. EXPORT_SYMBOL(dib9000_set_gpio);
  1475. int dib9000_fw_pid_filter_ctrl(struct dvb_frontend *fe, u8 onoff)
  1476. {
  1477. struct dib9000_state *state = fe->demodulator_priv;
  1478. u16 val = dib9000_read_word(state, 294 + 1) & 0xffef;
  1479. val |= (onoff & 0x1) << 4;
  1480. dprintk("PID filter enabled %d", onoff);
  1481. return dib9000_write_word(state, 294 + 1, val);
  1482. }
  1483. EXPORT_SYMBOL(dib9000_fw_pid_filter_ctrl);
  1484. int dib9000_fw_pid_filter(struct dvb_frontend *fe, u8 id, u16 pid, u8 onoff)
  1485. {
  1486. struct dib9000_state *state = fe->demodulator_priv;
  1487. dprintk("Index %x, PID %d, OnOff %d", id, pid, onoff);
  1488. return dib9000_write_word(state, 300 + 1 + id, onoff ? (1 << 13) | pid : 0);
  1489. }
  1490. EXPORT_SYMBOL(dib9000_fw_pid_filter);
  1491. int dib9000_firmware_post_pll_init(struct dvb_frontend *fe)
  1492. {
  1493. struct dib9000_state *state = fe->demodulator_priv;
  1494. return dib9000_fw_init(state);
  1495. }
  1496. EXPORT_SYMBOL(dib9000_firmware_post_pll_init);
  1497. static void dib9000_release(struct dvb_frontend *demod)
  1498. {
  1499. struct dib9000_state *st = demod->demodulator_priv;
  1500. u8 index_frontend;
  1501. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (st->fe[index_frontend] != NULL); index_frontend++)
  1502. dvb_frontend_detach(st->fe[index_frontend]);
  1503. DibFreeLock(&state->platform.risc.mbx_if_lock);
  1504. DibFreeLock(&state->platform.risc.mbx_lock);
  1505. DibFreeLock(&state->platform.risc.mem_lock);
  1506. DibFreeLock(&state->platform.risc.mem_mbx_lock);
  1507. dibx000_exit_i2c_master(&st->i2c_master);
  1508. i2c_del_adapter(&st->tuner_adap);
  1509. i2c_del_adapter(&st->component_bus);
  1510. kfree(st->fe[0]);
  1511. kfree(st);
  1512. }
  1513. static int dib9000_wakeup(struct dvb_frontend *fe)
  1514. {
  1515. return 0;
  1516. }
  1517. static int dib9000_sleep(struct dvb_frontend *fe)
  1518. {
  1519. struct dib9000_state *state = fe->demodulator_priv;
  1520. u8 index_frontend;
  1521. int ret;
  1522. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1523. ret = state->fe[index_frontend]->ops.sleep(state->fe[index_frontend]);
  1524. if (ret < 0)
  1525. return ret;
  1526. }
  1527. return dib9000_mbx_send(state, OUT_MSG_FE_SLEEP, NULL, 0);
  1528. }
  1529. static int dib9000_fe_get_tune_settings(struct dvb_frontend *fe, struct dvb_frontend_tune_settings *tune)
  1530. {
  1531. tune->min_delay_ms = 1000;
  1532. return 0;
  1533. }
  1534. static int dib9000_get_frontend(struct dvb_frontend *fe, struct dvb_frontend_parameters *fep)
  1535. {
  1536. struct dib9000_state *state = fe->demodulator_priv;
  1537. u8 index_frontend, sub_index_frontend;
  1538. fe_status_t stat;
  1539. int ret;
  1540. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1541. state->fe[index_frontend]->ops.read_status(state->fe[index_frontend], &stat);
  1542. if (stat & FE_HAS_SYNC) {
  1543. dprintk("TPS lock on the slave%i", index_frontend);
  1544. /* synchronize the cache with the other frontends */
  1545. state->fe[index_frontend]->ops.get_frontend(state->fe[index_frontend], fep);
  1546. for (sub_index_frontend = 0; (sub_index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[sub_index_frontend] != NULL);
  1547. sub_index_frontend++) {
  1548. if (sub_index_frontend != index_frontend) {
  1549. state->fe[sub_index_frontend]->dtv_property_cache.modulation =
  1550. state->fe[index_frontend]->dtv_property_cache.modulation;
  1551. state->fe[sub_index_frontend]->dtv_property_cache.inversion =
  1552. state->fe[index_frontend]->dtv_property_cache.inversion;
  1553. state->fe[sub_index_frontend]->dtv_property_cache.transmission_mode =
  1554. state->fe[index_frontend]->dtv_property_cache.transmission_mode;
  1555. state->fe[sub_index_frontend]->dtv_property_cache.guard_interval =
  1556. state->fe[index_frontend]->dtv_property_cache.guard_interval;
  1557. state->fe[sub_index_frontend]->dtv_property_cache.hierarchy =
  1558. state->fe[index_frontend]->dtv_property_cache.hierarchy;
  1559. state->fe[sub_index_frontend]->dtv_property_cache.code_rate_HP =
  1560. state->fe[index_frontend]->dtv_property_cache.code_rate_HP;
  1561. state->fe[sub_index_frontend]->dtv_property_cache.code_rate_LP =
  1562. state->fe[index_frontend]->dtv_property_cache.code_rate_LP;
  1563. state->fe[sub_index_frontend]->dtv_property_cache.rolloff =
  1564. state->fe[index_frontend]->dtv_property_cache.rolloff;
  1565. }
  1566. }
  1567. return 0;
  1568. }
  1569. }
  1570. /* get the channel from master chip */
  1571. ret = dib9000_fw_get_channel(fe, fep);
  1572. if (ret != 0)
  1573. return ret;
  1574. /* synchronize the cache with the other frontends */
  1575. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1576. state->fe[index_frontend]->dtv_property_cache.inversion = fe->dtv_property_cache.inversion;
  1577. state->fe[index_frontend]->dtv_property_cache.transmission_mode = fe->dtv_property_cache.transmission_mode;
  1578. state->fe[index_frontend]->dtv_property_cache.guard_interval = fe->dtv_property_cache.guard_interval;
  1579. state->fe[index_frontend]->dtv_property_cache.modulation = fe->dtv_property_cache.modulation;
  1580. state->fe[index_frontend]->dtv_property_cache.hierarchy = fe->dtv_property_cache.hierarchy;
  1581. state->fe[index_frontend]->dtv_property_cache.code_rate_HP = fe->dtv_property_cache.code_rate_HP;
  1582. state->fe[index_frontend]->dtv_property_cache.code_rate_LP = fe->dtv_property_cache.code_rate_LP;
  1583. state->fe[index_frontend]->dtv_property_cache.rolloff = fe->dtv_property_cache.rolloff;
  1584. }
  1585. return 0;
  1586. }
  1587. static int dib9000_set_tune_state(struct dvb_frontend *fe, enum frontend_tune_state tune_state)
  1588. {
  1589. struct dib9000_state *state = fe->demodulator_priv;
  1590. state->tune_state = tune_state;
  1591. if (tune_state == CT_DEMOD_START)
  1592. state->status = FE_STATUS_TUNE_PENDING;
  1593. return 0;
  1594. }
  1595. static u32 dib9000_get_status(struct dvb_frontend *fe)
  1596. {
  1597. struct dib9000_state *state = fe->demodulator_priv;
  1598. return state->status;
  1599. }
  1600. static int dib9000_set_channel_status(struct dvb_frontend *fe, struct dvb_frontend_parametersContext *channel_status)
  1601. {
  1602. struct dib9000_state *state = fe->demodulator_priv;
  1603. memcpy(&state->channel_status, channel_status, sizeof(struct dvb_frontend_parametersContext));
  1604. return 0;
  1605. }
  1606. static int dib9000_set_frontend(struct dvb_frontend *fe, struct dvb_frontend_parameters *fep)
  1607. {
  1608. struct dib9000_state *state = fe->demodulator_priv;
  1609. int sleep_time, sleep_time_slave;
  1610. u32 frontend_status;
  1611. u8 nbr_pending, exit_condition, index_frontend, index_frontend_success;
  1612. struct dvb_frontend_parametersContext channel_status;
  1613. /* check that the correct parameters are set */
  1614. if (state->fe[0]->dtv_property_cache.frequency == 0) {
  1615. dprintk("dib9000: must specify frequency ");
  1616. return 0;
  1617. }
  1618. if (state->fe[0]->dtv_property_cache.bandwidth_hz == 0) {
  1619. dprintk("dib9000: must specify bandwidth ");
  1620. return 0;
  1621. }
  1622. fe->dtv_property_cache.delivery_system = SYS_DVBT;
  1623. /* set the master status */
  1624. if (fep->u.ofdm.transmission_mode == TRANSMISSION_MODE_AUTO ||
  1625. fep->u.ofdm.guard_interval == GUARD_INTERVAL_AUTO || fep->u.ofdm.constellation == QAM_AUTO || fep->u.ofdm.code_rate_HP == FEC_AUTO) {
  1626. /* no channel specified, autosearch the channel */
  1627. state->channel_status.status = CHANNEL_STATUS_PARAMETERS_UNKNOWN;
  1628. } else
  1629. state->channel_status.status = CHANNEL_STATUS_PARAMETERS_SET;
  1630. /* set mode and status for the different frontends */
  1631. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1632. dib9000_fw_set_diversity_in(state->fe[index_frontend], 1);
  1633. /* synchronization of the cache */
  1634. memcpy(&state->fe[index_frontend]->dtv_property_cache, &fe->dtv_property_cache, sizeof(struct dtv_frontend_properties));
  1635. state->fe[index_frontend]->dtv_property_cache.delivery_system = SYS_DVBT;
  1636. dib9000_fw_set_output_mode(state->fe[index_frontend], OUTMODE_HIGH_Z);
  1637. dib9000_set_channel_status(state->fe[index_frontend], &state->channel_status);
  1638. dib9000_set_tune_state(state->fe[index_frontend], CT_DEMOD_START);
  1639. }
  1640. /* actual tune */
  1641. exit_condition = 0; /* 0: tune pending; 1: tune failed; 2:tune success */
  1642. index_frontend_success = 0;
  1643. do {
  1644. sleep_time = dib9000_fw_tune(state->fe[0], NULL);
  1645. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1646. sleep_time_slave = dib9000_fw_tune(state->fe[index_frontend], NULL);
  1647. if (sleep_time == FE_CALLBACK_TIME_NEVER)
  1648. sleep_time = sleep_time_slave;
  1649. else if ((sleep_time_slave != FE_CALLBACK_TIME_NEVER) && (sleep_time_slave > sleep_time))
  1650. sleep_time = sleep_time_slave;
  1651. }
  1652. if (sleep_time != FE_CALLBACK_TIME_NEVER)
  1653. msleep(sleep_time / 10);
  1654. else
  1655. break;
  1656. nbr_pending = 0;
  1657. exit_condition = 0;
  1658. index_frontend_success = 0;
  1659. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1660. frontend_status = -dib9000_get_status(state->fe[index_frontend]);
  1661. if (frontend_status > -FE_STATUS_TUNE_PENDING) {
  1662. exit_condition = 2; /* tune success */
  1663. index_frontend_success = index_frontend;
  1664. break;
  1665. }
  1666. if (frontend_status == -FE_STATUS_TUNE_PENDING)
  1667. nbr_pending++; /* some frontends are still tuning */
  1668. }
  1669. if ((exit_condition != 2) && (nbr_pending == 0))
  1670. exit_condition = 1; /* if all tune are done and no success, exit: tune failed */
  1671. } while (exit_condition == 0);
  1672. /* check the tune result */
  1673. if (exit_condition == 1) { /* tune failed */
  1674. dprintk("tune failed");
  1675. return 0;
  1676. }
  1677. dprintk("tune success on frontend%i", index_frontend_success);
  1678. /* synchronize all the channel cache */
  1679. dib9000_get_frontend(state->fe[0], fep);
  1680. /* retune the other frontends with the found channel */
  1681. channel_status.status = CHANNEL_STATUS_PARAMETERS_SET;
  1682. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1683. /* only retune the frontends which was not tuned success */
  1684. if (index_frontend != index_frontend_success) {
  1685. dib9000_set_channel_status(state->fe[index_frontend], &channel_status);
  1686. dib9000_set_tune_state(state->fe[index_frontend], CT_DEMOD_START);
  1687. }
  1688. }
  1689. do {
  1690. sleep_time = FE_CALLBACK_TIME_NEVER;
  1691. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1692. if (index_frontend != index_frontend_success) {
  1693. sleep_time_slave = dib9000_fw_tune(state->fe[index_frontend], NULL);
  1694. if (sleep_time == FE_CALLBACK_TIME_NEVER)
  1695. sleep_time = sleep_time_slave;
  1696. else if ((sleep_time_slave != FE_CALLBACK_TIME_NEVER) && (sleep_time_slave > sleep_time))
  1697. sleep_time = sleep_time_slave;
  1698. }
  1699. }
  1700. if (sleep_time != FE_CALLBACK_TIME_NEVER)
  1701. msleep(sleep_time / 10);
  1702. else
  1703. break;
  1704. nbr_pending = 0;
  1705. for (index_frontend = 0; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1706. if (index_frontend != index_frontend_success) {
  1707. frontend_status = -dib9000_get_status(state->fe[index_frontend]);
  1708. if ((index_frontend != index_frontend_success) && (frontend_status == -FE_STATUS_TUNE_PENDING))
  1709. nbr_pending++; /* some frontends are still tuning */
  1710. }
  1711. }
  1712. } while (nbr_pending != 0);
  1713. /* set the output mode */
  1714. dib9000_fw_set_output_mode(state->fe[0], state->chip.d9.cfg.output_mode);
  1715. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
  1716. dib9000_fw_set_output_mode(state->fe[index_frontend], OUTMODE_DIVERSITY);
  1717. /* turn off the diversity for the last frontend */
  1718. dib9000_fw_set_diversity_in(state->fe[index_frontend - 1], 0);
  1719. return 0;
  1720. }
  1721. static u16 dib9000_read_lock(struct dvb_frontend *fe)
  1722. {
  1723. struct dib9000_state *state = fe->demodulator_priv;
  1724. return dib9000_read_word(state, 535);
  1725. }
  1726. static int dib9000_read_status(struct dvb_frontend *fe, fe_status_t * stat)
  1727. {
  1728. struct dib9000_state *state = fe->demodulator_priv;
  1729. u8 index_frontend;
  1730. u16 lock = 0, lock_slave = 0;
  1731. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
  1732. lock_slave |= dib9000_read_lock(state->fe[index_frontend]);
  1733. lock = dib9000_read_word(state, 535);
  1734. *stat = 0;
  1735. if ((lock & 0x8000) || (lock_slave & 0x8000))
  1736. *stat |= FE_HAS_SIGNAL;
  1737. if ((lock & 0x3000) || (lock_slave & 0x3000))
  1738. *stat |= FE_HAS_CARRIER;
  1739. if ((lock & 0x0100) || (lock_slave & 0x0100))
  1740. *stat |= FE_HAS_VITERBI;
  1741. if (((lock & 0x0038) == 0x38) || ((lock_slave & 0x0038) == 0x38))
  1742. *stat |= FE_HAS_SYNC;
  1743. if ((lock & 0x0008) || (lock_slave & 0x0008))
  1744. *stat |= FE_HAS_LOCK;
  1745. return 0;
  1746. }
  1747. static int dib9000_read_ber(struct dvb_frontend *fe, u32 * ber)
  1748. {
  1749. struct dib9000_state *state = fe->demodulator_priv;
  1750. u16 *c;
  1751. DibAcquireLock(&state->platform.risc.mem_mbx_lock);
  1752. if (dib9000_fw_memmbx_sync(state, FE_SYNC_CHANNEL) < 0)
  1753. return -EIO;
  1754. dib9000_risc_mem_read(state, FE_MM_R_FE_MONITOR,
  1755. state->i2c_read_buffer, 16 * 2);
  1756. DibReleaseLock(&state->platform.risc.mem_mbx_lock);
  1757. c = (u16 *)state->i2c_read_buffer;
  1758. *ber = c[10] << 16 | c[11];
  1759. return 0;
  1760. }
  1761. static int dib9000_read_signal_strength(struct dvb_frontend *fe, u16 * strength)
  1762. {
  1763. struct dib9000_state *state = fe->demodulator_priv;
  1764. u8 index_frontend;
  1765. u16 *c = (u16 *)state->i2c_read_buffer;
  1766. u16 val;
  1767. *strength = 0;
  1768. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++) {
  1769. state->fe[index_frontend]->ops.read_signal_strength(state->fe[index_frontend], &val);
  1770. if (val > 65535 - *strength)
  1771. *strength = 65535;
  1772. else
  1773. *strength += val;
  1774. }
  1775. DibAcquireLock(&state->platform.risc.mem_mbx_lock);
  1776. if (dib9000_fw_memmbx_sync(state, FE_SYNC_CHANNEL) < 0)
  1777. return -EIO;
  1778. dib9000_risc_mem_read(state, FE_MM_R_FE_MONITOR, (u8 *) c, 16 * 2);
  1779. DibReleaseLock(&state->platform.risc.mem_mbx_lock);
  1780. val = 65535 - c[4];
  1781. if (val > 65535 - *strength)
  1782. *strength = 65535;
  1783. else
  1784. *strength += val;
  1785. return 0;
  1786. }
  1787. static u32 dib9000_get_snr(struct dvb_frontend *fe)
  1788. {
  1789. struct dib9000_state *state = fe->demodulator_priv;
  1790. u16 *c = (u16 *)state->i2c_read_buffer;
  1791. u32 n, s, exp;
  1792. u16 val;
  1793. DibAcquireLock(&state->platform.risc.mem_mbx_lock);
  1794. if (dib9000_fw_memmbx_sync(state, FE_SYNC_CHANNEL) < 0)
  1795. return -EIO;
  1796. dib9000_risc_mem_read(state, FE_MM_R_FE_MONITOR, (u8 *) c, 16 * 2);
  1797. DibReleaseLock(&state->platform.risc.mem_mbx_lock);
  1798. val = c[7];
  1799. n = (val >> 4) & 0xff;
  1800. exp = ((val & 0xf) << 2);
  1801. val = c[8];
  1802. exp += ((val >> 14) & 0x3);
  1803. if ((exp & 0x20) != 0)
  1804. exp -= 0x40;
  1805. n <<= exp + 16;
  1806. s = (val >> 6) & 0xFF;
  1807. exp = (val & 0x3F);
  1808. if ((exp & 0x20) != 0)
  1809. exp -= 0x40;
  1810. s <<= exp + 16;
  1811. if (n > 0) {
  1812. u32 t = (s / n) << 16;
  1813. return t + ((s << 16) - n * t) / n;
  1814. }
  1815. return 0xffffffff;
  1816. }
  1817. static int dib9000_read_snr(struct dvb_frontend *fe, u16 * snr)
  1818. {
  1819. struct dib9000_state *state = fe->demodulator_priv;
  1820. u8 index_frontend;
  1821. u32 snr_master;
  1822. snr_master = dib9000_get_snr(fe);
  1823. for (index_frontend = 1; (index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL); index_frontend++)
  1824. snr_master += dib9000_get_snr(state->fe[index_frontend]);
  1825. if ((snr_master >> 16) != 0) {
  1826. snr_master = 10 * intlog10(snr_master >> 16);
  1827. *snr = snr_master / ((1 << 24) / 10);
  1828. } else
  1829. *snr = 0;
  1830. return 0;
  1831. }
  1832. static int dib9000_read_unc_blocks(struct dvb_frontend *fe, u32 * unc)
  1833. {
  1834. struct dib9000_state *state = fe->demodulator_priv;
  1835. u16 *c = (u16 *)state->i2c_read_buffer;
  1836. DibAcquireLock(&state->platform.risc.mem_mbx_lock);
  1837. if (dib9000_fw_memmbx_sync(state, FE_SYNC_CHANNEL) < 0)
  1838. return -EIO;
  1839. dib9000_risc_mem_read(state, FE_MM_R_FE_MONITOR, (u8 *) c, 16 * 2);
  1840. DibReleaseLock(&state->platform.risc.mem_mbx_lock);
  1841. *unc = c[12];
  1842. return 0;
  1843. }
  1844. int dib9000_i2c_enumeration(struct i2c_adapter *i2c, int no_of_demods, u8 default_addr, u8 first_addr)
  1845. {
  1846. int k = 0, ret = 0;
  1847. u8 new_addr = 0;
  1848. struct i2c_device client = {.i2c_adap = i2c };
  1849. client.i2c_write_buffer = kzalloc(4 * sizeof(u8), GFP_KERNEL);
  1850. if (!client.i2c_write_buffer) {
  1851. dprintk("%s: not enough memory", __func__);
  1852. return -ENOMEM;
  1853. }
  1854. client.i2c_read_buffer = kzalloc(4 * sizeof(u8), GFP_KERNEL);
  1855. if (!client.i2c_read_buffer) {
  1856. dprintk("%s: not enough memory", __func__);
  1857. ret = -ENOMEM;
  1858. goto error_memory;
  1859. }
  1860. client.i2c_addr = default_addr + 16;
  1861. dib9000_i2c_write16(&client, 1796, 0x0);
  1862. for (k = no_of_demods - 1; k >= 0; k--) {
  1863. /* designated i2c address */
  1864. new_addr = first_addr + (k << 1);
  1865. client.i2c_addr = default_addr;
  1866. dib9000_i2c_write16(&client, 1817, 3);
  1867. dib9000_i2c_write16(&client, 1796, 0);
  1868. dib9000_i2c_write16(&client, 1227, 1);
  1869. dib9000_i2c_write16(&client, 1227, 0);
  1870. client.i2c_addr = new_addr;
  1871. dib9000_i2c_write16(&client, 1817, 3);
  1872. dib9000_i2c_write16(&client, 1796, 0);
  1873. dib9000_i2c_write16(&client, 1227, 1);
  1874. dib9000_i2c_write16(&client, 1227, 0);
  1875. if (dib9000_identify(&client) == 0) {
  1876. client.i2c_addr = default_addr;
  1877. if (dib9000_identify(&client) == 0) {
  1878. dprintk("DiB9000 #%d: not identified", k);
  1879. ret = -EIO;
  1880. goto error;
  1881. }
  1882. }
  1883. dib9000_i2c_write16(&client, 1795, (1 << 10) | (4 << 6));
  1884. dib9000_i2c_write16(&client, 1794, (new_addr << 2) | 2);
  1885. dprintk("IC %d initialized (to i2c_address 0x%x)", k, new_addr);
  1886. }
  1887. for (k = 0; k < no_of_demods; k++) {
  1888. new_addr = first_addr | (k << 1);
  1889. client.i2c_addr = new_addr;
  1890. dib9000_i2c_write16(&client, 1794, (new_addr << 2));
  1891. dib9000_i2c_write16(&client, 1795, 0);
  1892. }
  1893. error:
  1894. kfree(client.i2c_read_buffer);
  1895. error_memory:
  1896. kfree(client.i2c_write_buffer);
  1897. return ret;
  1898. }
  1899. EXPORT_SYMBOL(dib9000_i2c_enumeration);
  1900. int dib9000_set_slave_frontend(struct dvb_frontend *fe, struct dvb_frontend *fe_slave)
  1901. {
  1902. struct dib9000_state *state = fe->demodulator_priv;
  1903. u8 index_frontend = 1;
  1904. while ((index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL))
  1905. index_frontend++;
  1906. if (index_frontend < MAX_NUMBER_OF_FRONTENDS) {
  1907. dprintk("set slave fe %p to index %i", fe_slave, index_frontend);
  1908. state->fe[index_frontend] = fe_slave;
  1909. return 0;
  1910. }
  1911. dprintk("too many slave frontend");
  1912. return -ENOMEM;
  1913. }
  1914. EXPORT_SYMBOL(dib9000_set_slave_frontend);
  1915. int dib9000_remove_slave_frontend(struct dvb_frontend *fe)
  1916. {
  1917. struct dib9000_state *state = fe->demodulator_priv;
  1918. u8 index_frontend = 1;
  1919. while ((index_frontend < MAX_NUMBER_OF_FRONTENDS) && (state->fe[index_frontend] != NULL))
  1920. index_frontend++;
  1921. if (index_frontend != 1) {
  1922. dprintk("remove slave fe %p (index %i)", state->fe[index_frontend - 1], index_frontend - 1);
  1923. state->fe[index_frontend] = NULL;
  1924. return 0;
  1925. }
  1926. dprintk("no frontend to be removed");
  1927. return -ENODEV;
  1928. }
  1929. EXPORT_SYMBOL(dib9000_remove_slave_frontend);
  1930. struct dvb_frontend *dib9000_get_slave_frontend(struct dvb_frontend *fe, int slave_index)
  1931. {
  1932. struct dib9000_state *state = fe->demodulator_priv;
  1933. if (slave_index >= MAX_NUMBER_OF_FRONTENDS)
  1934. return NULL;
  1935. return state->fe[slave_index];
  1936. }
  1937. EXPORT_SYMBOL(dib9000_get_slave_frontend);
  1938. static struct dvb_frontend_ops dib9000_ops;
  1939. struct dvb_frontend *dib9000_attach(struct i2c_adapter *i2c_adap, u8 i2c_addr, const struct dib9000_config *cfg)
  1940. {
  1941. struct dvb_frontend *fe;
  1942. struct dib9000_state *st;
  1943. st = kzalloc(sizeof(struct dib9000_state), GFP_KERNEL);
  1944. if (st == NULL)
  1945. return NULL;
  1946. fe = kzalloc(sizeof(struct dvb_frontend), GFP_KERNEL);
  1947. if (fe == NULL) {
  1948. kfree(st);
  1949. return NULL;
  1950. }
  1951. memcpy(&st->chip.d9.cfg, cfg, sizeof(struct dib9000_config));
  1952. st->i2c.i2c_adap = i2c_adap;
  1953. st->i2c.i2c_addr = i2c_addr;
  1954. st->i2c.i2c_write_buffer = st->i2c_write_buffer;
  1955. st->i2c.i2c_read_buffer = st->i2c_read_buffer;
  1956. st->gpio_dir = DIB9000_GPIO_DEFAULT_DIRECTIONS;
  1957. st->gpio_val = DIB9000_GPIO_DEFAULT_VALUES;
  1958. st->gpio_pwm_pos = DIB9000_GPIO_DEFAULT_PWM_POS;
  1959. DibInitLock(&st->platform.risc.mbx_if_lock);
  1960. DibInitLock(&st->platform.risc.mbx_lock);
  1961. DibInitLock(&st->platform.risc.mem_lock);
  1962. DibInitLock(&st->platform.risc.mem_mbx_lock);
  1963. st->fe[0] = fe;
  1964. fe->demodulator_priv = st;
  1965. memcpy(&st->fe[0]->ops, &dib9000_ops, sizeof(struct dvb_frontend_ops));
  1966. /* Ensure the output mode remains at the previous default if it's
  1967. * not specifically set by the caller.
  1968. */
  1969. if ((st->chip.d9.cfg.output_mode != OUTMODE_MPEG2_SERIAL) && (st->chip.d9.cfg.output_mode != OUTMODE_MPEG2_PAR_GATED_CLK))
  1970. st->chip.d9.cfg.output_mode = OUTMODE_MPEG2_FIFO;
  1971. if (dib9000_identify(&st->i2c) == 0)
  1972. goto error;
  1973. dibx000_init_i2c_master(&st->i2c_master, DIB7000MC, st->i2c.i2c_adap, st->i2c.i2c_addr);
  1974. st->tuner_adap.dev.parent = i2c_adap->dev.parent;
  1975. strncpy(st->tuner_adap.name, "DIB9000_FW TUNER ACCESS", sizeof(st->tuner_adap.name));
  1976. st->tuner_adap.algo = &dib9000_tuner_algo;
  1977. st->tuner_adap.algo_data = NULL;
  1978. i2c_set_adapdata(&st->tuner_adap, st);
  1979. if (i2c_add_adapter(&st->tuner_adap) < 0)
  1980. goto error;
  1981. st->component_bus.dev.parent = i2c_adap->dev.parent;
  1982. strncpy(st->component_bus.name, "DIB9000_FW COMPONENT BUS ACCESS", sizeof(st->component_bus.name));
  1983. st->component_bus.algo = &dib9000_component_bus_algo;
  1984. st->component_bus.algo_data = NULL;
  1985. st->component_bus_speed = 340;
  1986. i2c_set_adapdata(&st->component_bus, st);
  1987. if (i2c_add_adapter(&st->component_bus) < 0)
  1988. goto component_bus_add_error;
  1989. dib9000_fw_reset(fe);
  1990. return fe;
  1991. component_bus_add_error:
  1992. i2c_del_adapter(&st->tuner_adap);
  1993. error:
  1994. kfree(st);
  1995. return NULL;
  1996. }
  1997. EXPORT_SYMBOL(dib9000_attach);
  1998. static struct dvb_frontend_ops dib9000_ops = {
  1999. .info = {
  2000. .name = "DiBcom 9000",
  2001. .type = FE_OFDM,
  2002. .frequency_min = 44250000,
  2003. .frequency_max = 867250000,
  2004. .frequency_stepsize = 62500,
  2005. .caps = FE_CAN_INVERSION_AUTO |
  2006. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  2007. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  2008. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  2009. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_RECOVER | FE_CAN_HIERARCHY_AUTO,
  2010. },
  2011. .release = dib9000_release,
  2012. .init = dib9000_wakeup,
  2013. .sleep = dib9000_sleep,
  2014. .set_frontend = dib9000_set_frontend,
  2015. .get_tune_settings = dib9000_fe_get_tune_settings,
  2016. .get_frontend = dib9000_get_frontend,
  2017. .read_status = dib9000_read_status,
  2018. .read_ber = dib9000_read_ber,
  2019. .read_signal_strength = dib9000_read_signal_strength,
  2020. .read_snr = dib9000_read_snr,
  2021. .read_ucblocks = dib9000_read_unc_blocks,
  2022. };
  2023. MODULE_AUTHOR("Patrick Boettcher <pboettcher@dibcom.fr>");
  2024. MODULE_AUTHOR("Olivier Grenie <ogrenie@dibcom.fr>");
  2025. MODULE_DESCRIPTION("Driver for the DiBcom 9000 COFDM demodulator");
  2026. MODULE_LICENSE("GPL");