init.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331
  1. /*
  2. * Copyright 2012 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. */
  24. #include <subdev/bios.h>
  25. #include <subdev/bios/bit.h>
  26. #include <subdev/bios/bmp.h>
  27. #include <subdev/bios/conn.h>
  28. #include <subdev/bios/dcb.h>
  29. #include <subdev/bios/dp.h>
  30. #include <subdev/bios/gpio.h>
  31. #include <subdev/bios/init.h>
  32. #include <subdev/bios/ramcfg.h>
  33. #include <subdev/devinit.h>
  34. #include <subdev/gpio.h>
  35. #include <subdev/i2c.h>
  36. #include <subdev/vga.h>
  37. #define bioslog(lvl, fmt, args...) do { \
  38. nvkm_printk(init->subdev, lvl, info, "0x%04x[%c]: "fmt, \
  39. init->offset, init_exec(init) ? \
  40. '0' + (init->nested - 1) : ' ', ##args); \
  41. } while(0)
  42. #define cont(fmt, args...) do { \
  43. if (init->subdev->debug >= NV_DBG_TRACE) \
  44. printk(fmt, ##args); \
  45. } while(0)
  46. #define trace(fmt, args...) bioslog(TRACE, fmt, ##args)
  47. #define warn(fmt, args...) bioslog(WARN, fmt, ##args)
  48. #define error(fmt, args...) bioslog(ERROR, fmt, ##args)
  49. /******************************************************************************
  50. * init parser control flow helpers
  51. *****************************************************************************/
  52. static inline bool
  53. init_exec(struct nvbios_init *init)
  54. {
  55. return (init->execute == 1) || ((init->execute & 5) == 5);
  56. }
  57. static inline void
  58. init_exec_set(struct nvbios_init *init, bool exec)
  59. {
  60. if (exec) init->execute &= 0xfd;
  61. else init->execute |= 0x02;
  62. }
  63. static inline void
  64. init_exec_inv(struct nvbios_init *init)
  65. {
  66. init->execute ^= 0x02;
  67. }
  68. static inline void
  69. init_exec_force(struct nvbios_init *init, bool exec)
  70. {
  71. if (exec) init->execute |= 0x04;
  72. else init->execute &= 0xfb;
  73. }
  74. /******************************************************************************
  75. * init parser wrappers for normal register/i2c/whatever accessors
  76. *****************************************************************************/
  77. static inline int
  78. init_or(struct nvbios_init *init)
  79. {
  80. if (init_exec(init)) {
  81. if (init->outp)
  82. return ffs(init->outp->or) - 1;
  83. error("script needs OR!!\n");
  84. }
  85. return 0;
  86. }
  87. static inline int
  88. init_link(struct nvbios_init *init)
  89. {
  90. if (init_exec(init)) {
  91. if (init->outp)
  92. return !(init->outp->sorconf.link & 1);
  93. error("script needs OR link\n");
  94. }
  95. return 0;
  96. }
  97. static inline int
  98. init_crtc(struct nvbios_init *init)
  99. {
  100. if (init_exec(init)) {
  101. if (init->crtc >= 0)
  102. return init->crtc;
  103. error("script needs crtc\n");
  104. }
  105. return 0;
  106. }
  107. static u8
  108. init_conn(struct nvbios_init *init)
  109. {
  110. struct nvkm_bios *bios = init->bios;
  111. struct nvbios_connE connE;
  112. u8 ver, hdr;
  113. u32 conn;
  114. if (init_exec(init)) {
  115. if (init->outp) {
  116. conn = init->outp->connector;
  117. conn = nvbios_connEp(bios, conn, &ver, &hdr, &connE);
  118. if (conn)
  119. return connE.type;
  120. }
  121. error("script needs connector type\n");
  122. }
  123. return 0xff;
  124. }
  125. static inline u32
  126. init_nvreg(struct nvbios_init *init, u32 reg)
  127. {
  128. struct nvkm_devinit *devinit = init->bios->subdev.device->devinit;
  129. /* C51 (at least) sometimes has the lower bits set which the VBIOS
  130. * interprets to mean that access needs to go through certain IO
  131. * ports instead. The NVIDIA binary driver has been seen to access
  132. * these through the NV register address, so lets assume we can
  133. * do the same
  134. */
  135. reg &= ~0x00000003;
  136. /* GF8+ display scripts need register addresses mangled a bit to
  137. * select a specific CRTC/OR
  138. */
  139. if (init->bios->subdev.device->card_type >= NV_50) {
  140. if (reg & 0x80000000) {
  141. reg += init_crtc(init) * 0x800;
  142. reg &= ~0x80000000;
  143. }
  144. if (reg & 0x40000000) {
  145. reg += init_or(init) * 0x800;
  146. reg &= ~0x40000000;
  147. if (reg & 0x20000000) {
  148. reg += init_link(init) * 0x80;
  149. reg &= ~0x20000000;
  150. }
  151. }
  152. }
  153. if (reg & ~0x00fffffc)
  154. warn("unknown bits in register 0x%08x\n", reg);
  155. return nvkm_devinit_mmio(devinit, reg);
  156. }
  157. static u32
  158. init_rd32(struct nvbios_init *init, u32 reg)
  159. {
  160. struct nvkm_device *device = init->bios->subdev.device;
  161. reg = init_nvreg(init, reg);
  162. if (reg != ~0 && init_exec(init))
  163. return nvkm_rd32(device, reg);
  164. return 0x00000000;
  165. }
  166. static void
  167. init_wr32(struct nvbios_init *init, u32 reg, u32 val)
  168. {
  169. struct nvkm_device *device = init->bios->subdev.device;
  170. reg = init_nvreg(init, reg);
  171. if (reg != ~0 && init_exec(init))
  172. nvkm_wr32(device, reg, val);
  173. }
  174. static u32
  175. init_mask(struct nvbios_init *init, u32 reg, u32 mask, u32 val)
  176. {
  177. struct nvkm_device *device = init->bios->subdev.device;
  178. reg = init_nvreg(init, reg);
  179. if (reg != ~0 && init_exec(init)) {
  180. u32 tmp = nvkm_rd32(device, reg);
  181. nvkm_wr32(device, reg, (tmp & ~mask) | val);
  182. return tmp;
  183. }
  184. return 0x00000000;
  185. }
  186. static u8
  187. init_rdport(struct nvbios_init *init, u16 port)
  188. {
  189. if (init_exec(init))
  190. return nvkm_rdport(init->subdev->device, init->crtc, port);
  191. return 0x00;
  192. }
  193. static void
  194. init_wrport(struct nvbios_init *init, u16 port, u8 value)
  195. {
  196. if (init_exec(init))
  197. nvkm_wrport(init->subdev->device, init->crtc, port, value);
  198. }
  199. static u8
  200. init_rdvgai(struct nvbios_init *init, u16 port, u8 index)
  201. {
  202. struct nvkm_subdev *subdev = init->subdev;
  203. if (init_exec(init)) {
  204. int head = init->crtc < 0 ? 0 : init->crtc;
  205. return nvkm_rdvgai(subdev->device, head, port, index);
  206. }
  207. return 0x00;
  208. }
  209. static void
  210. init_wrvgai(struct nvbios_init *init, u16 port, u8 index, u8 value)
  211. {
  212. struct nvkm_device *device = init->subdev->device;
  213. /* force head 0 for updates to cr44, it only exists on first head */
  214. if (device->card_type < NV_50) {
  215. if (port == 0x03d4 && index == 0x44)
  216. init->crtc = 0;
  217. }
  218. if (init_exec(init)) {
  219. int head = init->crtc < 0 ? 0 : init->crtc;
  220. nvkm_wrvgai(device, head, port, index, value);
  221. }
  222. /* select head 1 if cr44 write selected it */
  223. if (device->card_type < NV_50) {
  224. if (port == 0x03d4 && index == 0x44 && value == 3)
  225. init->crtc = 1;
  226. }
  227. }
  228. static struct i2c_adapter *
  229. init_i2c(struct nvbios_init *init, int index)
  230. {
  231. struct nvkm_i2c *i2c = init->bios->subdev.device->i2c;
  232. struct nvkm_i2c_bus *bus;
  233. if (index == 0xff) {
  234. index = NVKM_I2C_BUS_PRI;
  235. if (init->outp && init->outp->i2c_upper_default)
  236. index = NVKM_I2C_BUS_SEC;
  237. } else
  238. if (index == 0x80) {
  239. index = NVKM_I2C_BUS_PRI;
  240. } else
  241. if (index == 0x81) {
  242. index = NVKM_I2C_BUS_SEC;
  243. }
  244. bus = nvkm_i2c_bus_find(i2c, index);
  245. return bus ? &bus->i2c : NULL;
  246. }
  247. static int
  248. init_rdi2cr(struct nvbios_init *init, u8 index, u8 addr, u8 reg)
  249. {
  250. struct i2c_adapter *adap = init_i2c(init, index);
  251. if (adap && init_exec(init))
  252. return nvkm_rdi2cr(adap, addr, reg);
  253. return -ENODEV;
  254. }
  255. static int
  256. init_wri2cr(struct nvbios_init *init, u8 index, u8 addr, u8 reg, u8 val)
  257. {
  258. struct i2c_adapter *adap = init_i2c(init, index);
  259. if (adap && init_exec(init))
  260. return nvkm_wri2cr(adap, addr, reg, val);
  261. return -ENODEV;
  262. }
  263. static struct nvkm_i2c_aux *
  264. init_aux(struct nvbios_init *init)
  265. {
  266. struct nvkm_i2c *i2c = init->bios->subdev.device->i2c;
  267. if (!init->outp) {
  268. if (init_exec(init))
  269. error("script needs output for aux\n");
  270. return NULL;
  271. }
  272. return nvkm_i2c_aux_find(i2c, init->outp->i2c_index);
  273. }
  274. static u8
  275. init_rdauxr(struct nvbios_init *init, u32 addr)
  276. {
  277. struct nvkm_i2c_aux *aux = init_aux(init);
  278. u8 data;
  279. if (aux && init_exec(init)) {
  280. int ret = nvkm_rdaux(aux, addr, &data, 1);
  281. if (ret == 0)
  282. return data;
  283. trace("auxch read failed with %d\n", ret);
  284. }
  285. return 0x00;
  286. }
  287. static int
  288. init_wrauxr(struct nvbios_init *init, u32 addr, u8 data)
  289. {
  290. struct nvkm_i2c_aux *aux = init_aux(init);
  291. if (aux && init_exec(init)) {
  292. int ret = nvkm_wraux(aux, addr, &data, 1);
  293. if (ret)
  294. trace("auxch write failed with %d\n", ret);
  295. return ret;
  296. }
  297. return -ENODEV;
  298. }
  299. static void
  300. init_prog_pll(struct nvbios_init *init, u32 id, u32 freq)
  301. {
  302. struct nvkm_devinit *devinit = init->bios->subdev.device->devinit;
  303. if (init_exec(init)) {
  304. int ret = nvkm_devinit_pll_set(devinit, id, freq);
  305. if (ret)
  306. warn("failed to prog pll 0x%08x to %dkHz\n", id, freq);
  307. }
  308. }
  309. /******************************************************************************
  310. * parsing of bios structures that are required to execute init tables
  311. *****************************************************************************/
  312. static u16
  313. init_table(struct nvkm_bios *bios, u16 *len)
  314. {
  315. struct bit_entry bit_I;
  316. if (!bit_entry(bios, 'I', &bit_I)) {
  317. *len = bit_I.length;
  318. return bit_I.offset;
  319. }
  320. if (bmp_version(bios) >= 0x0510) {
  321. *len = 14;
  322. return bios->bmp_offset + 75;
  323. }
  324. return 0x0000;
  325. }
  326. static u16
  327. init_table_(struct nvbios_init *init, u16 offset, const char *name)
  328. {
  329. struct nvkm_bios *bios = init->bios;
  330. u16 len, data = init_table(bios, &len);
  331. if (data) {
  332. if (len >= offset + 2) {
  333. data = nvbios_rd16(bios, data + offset);
  334. if (data)
  335. return data;
  336. warn("%s pointer invalid\n", name);
  337. return 0x0000;
  338. }
  339. warn("init data too short for %s pointer", name);
  340. return 0x0000;
  341. }
  342. warn("init data not found\n");
  343. return 0x0000;
  344. }
  345. #define init_script_table(b) init_table_((b), 0x00, "script table")
  346. #define init_macro_index_table(b) init_table_((b), 0x02, "macro index table")
  347. #define init_macro_table(b) init_table_((b), 0x04, "macro table")
  348. #define init_condition_table(b) init_table_((b), 0x06, "condition table")
  349. #define init_io_condition_table(b) init_table_((b), 0x08, "io condition table")
  350. #define init_io_flag_condition_table(b) init_table_((b), 0x0a, "io flag conditon table")
  351. #define init_function_table(b) init_table_((b), 0x0c, "function table")
  352. #define init_xlat_table(b) init_table_((b), 0x10, "xlat table");
  353. static u16
  354. init_script(struct nvkm_bios *bios, int index)
  355. {
  356. struct nvbios_init init = { .bios = bios };
  357. u16 bmp_ver = bmp_version(bios), data;
  358. if (bmp_ver && bmp_ver < 0x0510) {
  359. if (index > 1 || bmp_ver < 0x0100)
  360. return 0x0000;
  361. data = bios->bmp_offset + (bmp_ver < 0x0200 ? 14 : 18);
  362. return nvbios_rd16(bios, data + (index * 2));
  363. }
  364. data = init_script_table(&init);
  365. if (data)
  366. return nvbios_rd16(bios, data + (index * 2));
  367. return 0x0000;
  368. }
  369. static u16
  370. init_unknown_script(struct nvkm_bios *bios)
  371. {
  372. u16 len, data = init_table(bios, &len);
  373. if (data && len >= 16)
  374. return nvbios_rd16(bios, data + 14);
  375. return 0x0000;
  376. }
  377. static u8
  378. init_ram_restrict_group_count(struct nvbios_init *init)
  379. {
  380. return nvbios_ramcfg_count(init->bios);
  381. }
  382. static u8
  383. init_ram_restrict(struct nvbios_init *init)
  384. {
  385. /* This appears to be the behaviour of the VBIOS parser, and *is*
  386. * important to cache the NV_PEXTDEV_BOOT0 on later chipsets to
  387. * avoid fucking up the memory controller (somehow) by reading it
  388. * on every INIT_RAM_RESTRICT_ZM_GROUP opcode.
  389. *
  390. * Preserving the non-caching behaviour on earlier chipsets just
  391. * in case *not* re-reading the strap causes similar breakage.
  392. */
  393. if (!init->ramcfg || init->bios->version.major < 0x70)
  394. init->ramcfg = 0x80000000 | nvbios_ramcfg_index(init->subdev);
  395. return (init->ramcfg & 0x7fffffff);
  396. }
  397. static u8
  398. init_xlat_(struct nvbios_init *init, u8 index, u8 offset)
  399. {
  400. struct nvkm_bios *bios = init->bios;
  401. u16 table = init_xlat_table(init);
  402. if (table) {
  403. u16 data = nvbios_rd16(bios, table + (index * 2));
  404. if (data)
  405. return nvbios_rd08(bios, data + offset);
  406. warn("xlat table pointer %d invalid\n", index);
  407. }
  408. return 0x00;
  409. }
  410. /******************************************************************************
  411. * utility functions used by various init opcode handlers
  412. *****************************************************************************/
  413. static bool
  414. init_condition_met(struct nvbios_init *init, u8 cond)
  415. {
  416. struct nvkm_bios *bios = init->bios;
  417. u16 table = init_condition_table(init);
  418. if (table) {
  419. u32 reg = nvbios_rd32(bios, table + (cond * 12) + 0);
  420. u32 msk = nvbios_rd32(bios, table + (cond * 12) + 4);
  421. u32 val = nvbios_rd32(bios, table + (cond * 12) + 8);
  422. trace("\t[0x%02x] (R[0x%06x] & 0x%08x) == 0x%08x\n",
  423. cond, reg, msk, val);
  424. return (init_rd32(init, reg) & msk) == val;
  425. }
  426. return false;
  427. }
  428. static bool
  429. init_io_condition_met(struct nvbios_init *init, u8 cond)
  430. {
  431. struct nvkm_bios *bios = init->bios;
  432. u16 table = init_io_condition_table(init);
  433. if (table) {
  434. u16 port = nvbios_rd16(bios, table + (cond * 5) + 0);
  435. u8 index = nvbios_rd08(bios, table + (cond * 5) + 2);
  436. u8 mask = nvbios_rd08(bios, table + (cond * 5) + 3);
  437. u8 value = nvbios_rd08(bios, table + (cond * 5) + 4);
  438. trace("\t[0x%02x] (0x%04x[0x%02x] & 0x%02x) == 0x%02x\n",
  439. cond, port, index, mask, value);
  440. return (init_rdvgai(init, port, index) & mask) == value;
  441. }
  442. return false;
  443. }
  444. static bool
  445. init_io_flag_condition_met(struct nvbios_init *init, u8 cond)
  446. {
  447. struct nvkm_bios *bios = init->bios;
  448. u16 table = init_io_flag_condition_table(init);
  449. if (table) {
  450. u16 port = nvbios_rd16(bios, table + (cond * 9) + 0);
  451. u8 index = nvbios_rd08(bios, table + (cond * 9) + 2);
  452. u8 mask = nvbios_rd08(bios, table + (cond * 9) + 3);
  453. u8 shift = nvbios_rd08(bios, table + (cond * 9) + 4);
  454. u16 data = nvbios_rd16(bios, table + (cond * 9) + 5);
  455. u8 dmask = nvbios_rd08(bios, table + (cond * 9) + 7);
  456. u8 value = nvbios_rd08(bios, table + (cond * 9) + 8);
  457. u8 ioval = (init_rdvgai(init, port, index) & mask) >> shift;
  458. return (nvbios_rd08(bios, data + ioval) & dmask) == value;
  459. }
  460. return false;
  461. }
  462. static inline u32
  463. init_shift(u32 data, u8 shift)
  464. {
  465. if (shift < 0x80)
  466. return data >> shift;
  467. return data << (0x100 - shift);
  468. }
  469. static u32
  470. init_tmds_reg(struct nvbios_init *init, u8 tmds)
  471. {
  472. /* For mlv < 0x80, it is an index into a table of TMDS base addresses.
  473. * For mlv == 0x80 use the "or" value of the dcb_entry indexed by
  474. * CR58 for CR57 = 0 to index a table of offsets to the basic
  475. * 0x6808b0 address.
  476. * For mlv == 0x81 use the "or" value of the dcb_entry indexed by
  477. * CR58 for CR57 = 0 to index a table of offsets to the basic
  478. * 0x6808b0 address, and then flip the offset by 8.
  479. */
  480. const int pramdac_offset[13] = {
  481. 0, 0, 0x8, 0, 0x2000, 0, 0, 0, 0x2008, 0, 0, 0, 0x2000 };
  482. const u32 pramdac_table[4] = {
  483. 0x6808b0, 0x6808b8, 0x6828b0, 0x6828b8 };
  484. if (tmds >= 0x80) {
  485. if (init->outp) {
  486. u32 dacoffset = pramdac_offset[init->outp->or];
  487. if (tmds == 0x81)
  488. dacoffset ^= 8;
  489. return 0x6808b0 + dacoffset;
  490. }
  491. if (init_exec(init))
  492. error("tmds opcodes need dcb\n");
  493. } else {
  494. if (tmds < ARRAY_SIZE(pramdac_table))
  495. return pramdac_table[tmds];
  496. error("tmds selector 0x%02x unknown\n", tmds);
  497. }
  498. return 0;
  499. }
  500. /******************************************************************************
  501. * init opcode handlers
  502. *****************************************************************************/
  503. /**
  504. * init_reserved - stub for various unknown/unused single-byte opcodes
  505. *
  506. */
  507. static void
  508. init_reserved(struct nvbios_init *init)
  509. {
  510. u8 opcode = nvbios_rd08(init->bios, init->offset);
  511. u8 length, i;
  512. switch (opcode) {
  513. case 0xaa:
  514. length = 4;
  515. break;
  516. default:
  517. length = 1;
  518. break;
  519. }
  520. trace("RESERVED 0x%02x\t", opcode);
  521. for (i = 1; i < length; i++)
  522. cont(" 0x%02x", nvbios_rd08(init->bios, init->offset + i));
  523. cont("\n");
  524. init->offset += length;
  525. }
  526. /**
  527. * INIT_DONE - opcode 0x71
  528. *
  529. */
  530. static void
  531. init_done(struct nvbios_init *init)
  532. {
  533. trace("DONE\n");
  534. init->offset = 0x0000;
  535. }
  536. /**
  537. * INIT_IO_RESTRICT_PROG - opcode 0x32
  538. *
  539. */
  540. static void
  541. init_io_restrict_prog(struct nvbios_init *init)
  542. {
  543. struct nvkm_bios *bios = init->bios;
  544. u16 port = nvbios_rd16(bios, init->offset + 1);
  545. u8 index = nvbios_rd08(bios, init->offset + 3);
  546. u8 mask = nvbios_rd08(bios, init->offset + 4);
  547. u8 shift = nvbios_rd08(bios, init->offset + 5);
  548. u8 count = nvbios_rd08(bios, init->offset + 6);
  549. u32 reg = nvbios_rd32(bios, init->offset + 7);
  550. u8 conf, i;
  551. trace("IO_RESTRICT_PROG\tR[0x%06x] = "
  552. "((0x%04x[0x%02x] & 0x%02x) >> %d) [{\n",
  553. reg, port, index, mask, shift);
  554. init->offset += 11;
  555. conf = (init_rdvgai(init, port, index) & mask) >> shift;
  556. for (i = 0; i < count; i++) {
  557. u32 data = nvbios_rd32(bios, init->offset);
  558. if (i == conf) {
  559. trace("\t0x%08x *\n", data);
  560. init_wr32(init, reg, data);
  561. } else {
  562. trace("\t0x%08x\n", data);
  563. }
  564. init->offset += 4;
  565. }
  566. trace("}]\n");
  567. }
  568. /**
  569. * INIT_REPEAT - opcode 0x33
  570. *
  571. */
  572. static void
  573. init_repeat(struct nvbios_init *init)
  574. {
  575. struct nvkm_bios *bios = init->bios;
  576. u8 count = nvbios_rd08(bios, init->offset + 1);
  577. u16 repeat = init->repeat;
  578. trace("REPEAT\t0x%02x\n", count);
  579. init->offset += 2;
  580. init->repeat = init->offset;
  581. init->repend = init->offset;
  582. while (count--) {
  583. init->offset = init->repeat;
  584. nvbios_exec(init);
  585. if (count)
  586. trace("REPEAT\t0x%02x\n", count);
  587. }
  588. init->offset = init->repend;
  589. init->repeat = repeat;
  590. }
  591. /**
  592. * INIT_IO_RESTRICT_PLL - opcode 0x34
  593. *
  594. */
  595. static void
  596. init_io_restrict_pll(struct nvbios_init *init)
  597. {
  598. struct nvkm_bios *bios = init->bios;
  599. u16 port = nvbios_rd16(bios, init->offset + 1);
  600. u8 index = nvbios_rd08(bios, init->offset + 3);
  601. u8 mask = nvbios_rd08(bios, init->offset + 4);
  602. u8 shift = nvbios_rd08(bios, init->offset + 5);
  603. s8 iofc = nvbios_rd08(bios, init->offset + 6);
  604. u8 count = nvbios_rd08(bios, init->offset + 7);
  605. u32 reg = nvbios_rd32(bios, init->offset + 8);
  606. u8 conf, i;
  607. trace("IO_RESTRICT_PLL\tR[0x%06x] =PLL= "
  608. "((0x%04x[0x%02x] & 0x%02x) >> 0x%02x) IOFCOND 0x%02x [{\n",
  609. reg, port, index, mask, shift, iofc);
  610. init->offset += 12;
  611. conf = (init_rdvgai(init, port, index) & mask) >> shift;
  612. for (i = 0; i < count; i++) {
  613. u32 freq = nvbios_rd16(bios, init->offset) * 10;
  614. if (i == conf) {
  615. trace("\t%dkHz *\n", freq);
  616. if (iofc > 0 && init_io_flag_condition_met(init, iofc))
  617. freq *= 2;
  618. init_prog_pll(init, reg, freq);
  619. } else {
  620. trace("\t%dkHz\n", freq);
  621. }
  622. init->offset += 2;
  623. }
  624. trace("}]\n");
  625. }
  626. /**
  627. * INIT_END_REPEAT - opcode 0x36
  628. *
  629. */
  630. static void
  631. init_end_repeat(struct nvbios_init *init)
  632. {
  633. trace("END_REPEAT\n");
  634. init->offset += 1;
  635. if (init->repeat) {
  636. init->repend = init->offset;
  637. init->offset = 0;
  638. }
  639. }
  640. /**
  641. * INIT_COPY - opcode 0x37
  642. *
  643. */
  644. static void
  645. init_copy(struct nvbios_init *init)
  646. {
  647. struct nvkm_bios *bios = init->bios;
  648. u32 reg = nvbios_rd32(bios, init->offset + 1);
  649. u8 shift = nvbios_rd08(bios, init->offset + 5);
  650. u8 smask = nvbios_rd08(bios, init->offset + 6);
  651. u16 port = nvbios_rd16(bios, init->offset + 7);
  652. u8 index = nvbios_rd08(bios, init->offset + 9);
  653. u8 mask = nvbios_rd08(bios, init->offset + 10);
  654. u8 data;
  655. trace("COPY\t0x%04x[0x%02x] &= 0x%02x |= "
  656. "((R[0x%06x] %s 0x%02x) & 0x%02x)\n",
  657. port, index, mask, reg, (shift & 0x80) ? "<<" : ">>",
  658. (shift & 0x80) ? (0x100 - shift) : shift, smask);
  659. init->offset += 11;
  660. data = init_rdvgai(init, port, index) & mask;
  661. data |= init_shift(init_rd32(init, reg), shift) & smask;
  662. init_wrvgai(init, port, index, data);
  663. }
  664. /**
  665. * INIT_NOT - opcode 0x38
  666. *
  667. */
  668. static void
  669. init_not(struct nvbios_init *init)
  670. {
  671. trace("NOT\n");
  672. init->offset += 1;
  673. init_exec_inv(init);
  674. }
  675. /**
  676. * INIT_IO_FLAG_CONDITION - opcode 0x39
  677. *
  678. */
  679. static void
  680. init_io_flag_condition(struct nvbios_init *init)
  681. {
  682. struct nvkm_bios *bios = init->bios;
  683. u8 cond = nvbios_rd08(bios, init->offset + 1);
  684. trace("IO_FLAG_CONDITION\t0x%02x\n", cond);
  685. init->offset += 2;
  686. if (!init_io_flag_condition_met(init, cond))
  687. init_exec_set(init, false);
  688. }
  689. /**
  690. * INIT_DP_CONDITION - opcode 0x3a
  691. *
  692. */
  693. static void
  694. init_dp_condition(struct nvbios_init *init)
  695. {
  696. struct nvkm_bios *bios = init->bios;
  697. struct nvbios_dpout info;
  698. u8 cond = nvbios_rd08(bios, init->offset + 1);
  699. u8 unkn = nvbios_rd08(bios, init->offset + 2);
  700. u8 ver, hdr, cnt, len;
  701. u16 data;
  702. trace("DP_CONDITION\t0x%02x 0x%02x\n", cond, unkn);
  703. init->offset += 3;
  704. switch (cond) {
  705. case 0:
  706. if (init_conn(init) != DCB_CONNECTOR_eDP)
  707. init_exec_set(init, false);
  708. break;
  709. case 1:
  710. case 2:
  711. if ( init->outp &&
  712. (data = nvbios_dpout_match(bios, DCB_OUTPUT_DP,
  713. (init->outp->or << 0) |
  714. (init->outp->sorconf.link << 6),
  715. &ver, &hdr, &cnt, &len, &info)))
  716. {
  717. if (!(info.flags & cond))
  718. init_exec_set(init, false);
  719. break;
  720. }
  721. if (init_exec(init))
  722. warn("script needs dp output table data\n");
  723. break;
  724. case 5:
  725. if (!(init_rdauxr(init, 0x0d) & 1))
  726. init_exec_set(init, false);
  727. break;
  728. default:
  729. warn("unknown dp condition 0x%02x\n", cond);
  730. break;
  731. }
  732. }
  733. /**
  734. * INIT_IO_MASK_OR - opcode 0x3b
  735. *
  736. */
  737. static void
  738. init_io_mask_or(struct nvbios_init *init)
  739. {
  740. struct nvkm_bios *bios = init->bios;
  741. u8 index = nvbios_rd08(bios, init->offset + 1);
  742. u8 or = init_or(init);
  743. u8 data;
  744. trace("IO_MASK_OR\t0x03d4[0x%02x] &= ~(1 << 0x%02x)\n", index, or);
  745. init->offset += 2;
  746. data = init_rdvgai(init, 0x03d4, index);
  747. init_wrvgai(init, 0x03d4, index, data &= ~(1 << or));
  748. }
  749. /**
  750. * INIT_IO_OR - opcode 0x3c
  751. *
  752. */
  753. static void
  754. init_io_or(struct nvbios_init *init)
  755. {
  756. struct nvkm_bios *bios = init->bios;
  757. u8 index = nvbios_rd08(bios, init->offset + 1);
  758. u8 or = init_or(init);
  759. u8 data;
  760. trace("IO_OR\t0x03d4[0x%02x] |= (1 << 0x%02x)\n", index, or);
  761. init->offset += 2;
  762. data = init_rdvgai(init, 0x03d4, index);
  763. init_wrvgai(init, 0x03d4, index, data | (1 << or));
  764. }
  765. /**
  766. * INIT_ANDN_REG - opcode 0x47
  767. *
  768. */
  769. static void
  770. init_andn_reg(struct nvbios_init *init)
  771. {
  772. struct nvkm_bios *bios = init->bios;
  773. u32 reg = nvbios_rd32(bios, init->offset + 1);
  774. u32 mask = nvbios_rd32(bios, init->offset + 5);
  775. trace("ANDN_REG\tR[0x%06x] &= ~0x%08x\n", reg, mask);
  776. init->offset += 9;
  777. init_mask(init, reg, mask, 0);
  778. }
  779. /**
  780. * INIT_OR_REG - opcode 0x48
  781. *
  782. */
  783. static void
  784. init_or_reg(struct nvbios_init *init)
  785. {
  786. struct nvkm_bios *bios = init->bios;
  787. u32 reg = nvbios_rd32(bios, init->offset + 1);
  788. u32 mask = nvbios_rd32(bios, init->offset + 5);
  789. trace("OR_REG\tR[0x%06x] |= 0x%08x\n", reg, mask);
  790. init->offset += 9;
  791. init_mask(init, reg, 0, mask);
  792. }
  793. /**
  794. * INIT_INDEX_ADDRESS_LATCHED - opcode 0x49
  795. *
  796. */
  797. static void
  798. init_idx_addr_latched(struct nvbios_init *init)
  799. {
  800. struct nvkm_bios *bios = init->bios;
  801. u32 creg = nvbios_rd32(bios, init->offset + 1);
  802. u32 dreg = nvbios_rd32(bios, init->offset + 5);
  803. u32 mask = nvbios_rd32(bios, init->offset + 9);
  804. u32 data = nvbios_rd32(bios, init->offset + 13);
  805. u8 count = nvbios_rd08(bios, init->offset + 17);
  806. trace("INDEX_ADDRESS_LATCHED\tR[0x%06x] : R[0x%06x]\n", creg, dreg);
  807. trace("\tCTRL &= 0x%08x |= 0x%08x\n", mask, data);
  808. init->offset += 18;
  809. while (count--) {
  810. u8 iaddr = nvbios_rd08(bios, init->offset + 0);
  811. u8 idata = nvbios_rd08(bios, init->offset + 1);
  812. trace("\t[0x%02x] = 0x%02x\n", iaddr, idata);
  813. init->offset += 2;
  814. init_wr32(init, dreg, idata);
  815. init_mask(init, creg, ~mask, data | iaddr);
  816. }
  817. }
  818. /**
  819. * INIT_IO_RESTRICT_PLL2 - opcode 0x4a
  820. *
  821. */
  822. static void
  823. init_io_restrict_pll2(struct nvbios_init *init)
  824. {
  825. struct nvkm_bios *bios = init->bios;
  826. u16 port = nvbios_rd16(bios, init->offset + 1);
  827. u8 index = nvbios_rd08(bios, init->offset + 3);
  828. u8 mask = nvbios_rd08(bios, init->offset + 4);
  829. u8 shift = nvbios_rd08(bios, init->offset + 5);
  830. u8 count = nvbios_rd08(bios, init->offset + 6);
  831. u32 reg = nvbios_rd32(bios, init->offset + 7);
  832. u8 conf, i;
  833. trace("IO_RESTRICT_PLL2\t"
  834. "R[0x%06x] =PLL= ((0x%04x[0x%02x] & 0x%02x) >> 0x%02x) [{\n",
  835. reg, port, index, mask, shift);
  836. init->offset += 11;
  837. conf = (init_rdvgai(init, port, index) & mask) >> shift;
  838. for (i = 0; i < count; i++) {
  839. u32 freq = nvbios_rd32(bios, init->offset);
  840. if (i == conf) {
  841. trace("\t%dkHz *\n", freq);
  842. init_prog_pll(init, reg, freq);
  843. } else {
  844. trace("\t%dkHz\n", freq);
  845. }
  846. init->offset += 4;
  847. }
  848. trace("}]\n");
  849. }
  850. /**
  851. * INIT_PLL2 - opcode 0x4b
  852. *
  853. */
  854. static void
  855. init_pll2(struct nvbios_init *init)
  856. {
  857. struct nvkm_bios *bios = init->bios;
  858. u32 reg = nvbios_rd32(bios, init->offset + 1);
  859. u32 freq = nvbios_rd32(bios, init->offset + 5);
  860. trace("PLL2\tR[0x%06x] =PLL= %dkHz\n", reg, freq);
  861. init->offset += 9;
  862. init_prog_pll(init, reg, freq);
  863. }
  864. /**
  865. * INIT_I2C_BYTE - opcode 0x4c
  866. *
  867. */
  868. static void
  869. init_i2c_byte(struct nvbios_init *init)
  870. {
  871. struct nvkm_bios *bios = init->bios;
  872. u8 index = nvbios_rd08(bios, init->offset + 1);
  873. u8 addr = nvbios_rd08(bios, init->offset + 2) >> 1;
  874. u8 count = nvbios_rd08(bios, init->offset + 3);
  875. trace("I2C_BYTE\tI2C[0x%02x][0x%02x]\n", index, addr);
  876. init->offset += 4;
  877. while (count--) {
  878. u8 reg = nvbios_rd08(bios, init->offset + 0);
  879. u8 mask = nvbios_rd08(bios, init->offset + 1);
  880. u8 data = nvbios_rd08(bios, init->offset + 2);
  881. int val;
  882. trace("\t[0x%02x] &= 0x%02x |= 0x%02x\n", reg, mask, data);
  883. init->offset += 3;
  884. val = init_rdi2cr(init, index, addr, reg);
  885. if (val < 0)
  886. continue;
  887. init_wri2cr(init, index, addr, reg, (val & mask) | data);
  888. }
  889. }
  890. /**
  891. * INIT_ZM_I2C_BYTE - opcode 0x4d
  892. *
  893. */
  894. static void
  895. init_zm_i2c_byte(struct nvbios_init *init)
  896. {
  897. struct nvkm_bios *bios = init->bios;
  898. u8 index = nvbios_rd08(bios, init->offset + 1);
  899. u8 addr = nvbios_rd08(bios, init->offset + 2) >> 1;
  900. u8 count = nvbios_rd08(bios, init->offset + 3);
  901. trace("ZM_I2C_BYTE\tI2C[0x%02x][0x%02x]\n", index, addr);
  902. init->offset += 4;
  903. while (count--) {
  904. u8 reg = nvbios_rd08(bios, init->offset + 0);
  905. u8 data = nvbios_rd08(bios, init->offset + 1);
  906. trace("\t[0x%02x] = 0x%02x\n", reg, data);
  907. init->offset += 2;
  908. init_wri2cr(init, index, addr, reg, data);
  909. }
  910. }
  911. /**
  912. * INIT_ZM_I2C - opcode 0x4e
  913. *
  914. */
  915. static void
  916. init_zm_i2c(struct nvbios_init *init)
  917. {
  918. struct nvkm_bios *bios = init->bios;
  919. u8 index = nvbios_rd08(bios, init->offset + 1);
  920. u8 addr = nvbios_rd08(bios, init->offset + 2) >> 1;
  921. u8 count = nvbios_rd08(bios, init->offset + 3);
  922. u8 data[256], i;
  923. trace("ZM_I2C\tI2C[0x%02x][0x%02x]\n", index, addr);
  924. init->offset += 4;
  925. for (i = 0; i < count; i++) {
  926. data[i] = nvbios_rd08(bios, init->offset);
  927. trace("\t0x%02x\n", data[i]);
  928. init->offset++;
  929. }
  930. if (init_exec(init)) {
  931. struct i2c_adapter *adap = init_i2c(init, index);
  932. struct i2c_msg msg = {
  933. .addr = addr, .flags = 0, .len = count, .buf = data,
  934. };
  935. int ret;
  936. if (adap && (ret = i2c_transfer(adap, &msg, 1)) != 1)
  937. warn("i2c wr failed, %d\n", ret);
  938. }
  939. }
  940. /**
  941. * INIT_TMDS - opcode 0x4f
  942. *
  943. */
  944. static void
  945. init_tmds(struct nvbios_init *init)
  946. {
  947. struct nvkm_bios *bios = init->bios;
  948. u8 tmds = nvbios_rd08(bios, init->offset + 1);
  949. u8 addr = nvbios_rd08(bios, init->offset + 2);
  950. u8 mask = nvbios_rd08(bios, init->offset + 3);
  951. u8 data = nvbios_rd08(bios, init->offset + 4);
  952. u32 reg = init_tmds_reg(init, tmds);
  953. trace("TMDS\tT[0x%02x][0x%02x] &= 0x%02x |= 0x%02x\n",
  954. tmds, addr, mask, data);
  955. init->offset += 5;
  956. if (reg == 0)
  957. return;
  958. init_wr32(init, reg + 0, addr | 0x00010000);
  959. init_wr32(init, reg + 4, data | (init_rd32(init, reg + 4) & mask));
  960. init_wr32(init, reg + 0, addr);
  961. }
  962. /**
  963. * INIT_ZM_TMDS_GROUP - opcode 0x50
  964. *
  965. */
  966. static void
  967. init_zm_tmds_group(struct nvbios_init *init)
  968. {
  969. struct nvkm_bios *bios = init->bios;
  970. u8 tmds = nvbios_rd08(bios, init->offset + 1);
  971. u8 count = nvbios_rd08(bios, init->offset + 2);
  972. u32 reg = init_tmds_reg(init, tmds);
  973. trace("TMDS_ZM_GROUP\tT[0x%02x]\n", tmds);
  974. init->offset += 3;
  975. while (count--) {
  976. u8 addr = nvbios_rd08(bios, init->offset + 0);
  977. u8 data = nvbios_rd08(bios, init->offset + 1);
  978. trace("\t[0x%02x] = 0x%02x\n", addr, data);
  979. init->offset += 2;
  980. init_wr32(init, reg + 4, data);
  981. init_wr32(init, reg + 0, addr);
  982. }
  983. }
  984. /**
  985. * INIT_CR_INDEX_ADDRESS_LATCHED - opcode 0x51
  986. *
  987. */
  988. static void
  989. init_cr_idx_adr_latch(struct nvbios_init *init)
  990. {
  991. struct nvkm_bios *bios = init->bios;
  992. u8 addr0 = nvbios_rd08(bios, init->offset + 1);
  993. u8 addr1 = nvbios_rd08(bios, init->offset + 2);
  994. u8 base = nvbios_rd08(bios, init->offset + 3);
  995. u8 count = nvbios_rd08(bios, init->offset + 4);
  996. u8 save0;
  997. trace("CR_INDEX_ADDR C[%02x] C[%02x]\n", addr0, addr1);
  998. init->offset += 5;
  999. save0 = init_rdvgai(init, 0x03d4, addr0);
  1000. while (count--) {
  1001. u8 data = nvbios_rd08(bios, init->offset);
  1002. trace("\t\t[0x%02x] = 0x%02x\n", base, data);
  1003. init->offset += 1;
  1004. init_wrvgai(init, 0x03d4, addr0, base++);
  1005. init_wrvgai(init, 0x03d4, addr1, data);
  1006. }
  1007. init_wrvgai(init, 0x03d4, addr0, save0);
  1008. }
  1009. /**
  1010. * INIT_CR - opcode 0x52
  1011. *
  1012. */
  1013. static void
  1014. init_cr(struct nvbios_init *init)
  1015. {
  1016. struct nvkm_bios *bios = init->bios;
  1017. u8 addr = nvbios_rd08(bios, init->offset + 1);
  1018. u8 mask = nvbios_rd08(bios, init->offset + 2);
  1019. u8 data = nvbios_rd08(bios, init->offset + 3);
  1020. u8 val;
  1021. trace("CR\t\tC[0x%02x] &= 0x%02x |= 0x%02x\n", addr, mask, data);
  1022. init->offset += 4;
  1023. val = init_rdvgai(init, 0x03d4, addr) & mask;
  1024. init_wrvgai(init, 0x03d4, addr, val | data);
  1025. }
  1026. /**
  1027. * INIT_ZM_CR - opcode 0x53
  1028. *
  1029. */
  1030. static void
  1031. init_zm_cr(struct nvbios_init *init)
  1032. {
  1033. struct nvkm_bios *bios = init->bios;
  1034. u8 addr = nvbios_rd08(bios, init->offset + 1);
  1035. u8 data = nvbios_rd08(bios, init->offset + 2);
  1036. trace("ZM_CR\tC[0x%02x] = 0x%02x\n", addr, data);
  1037. init->offset += 3;
  1038. init_wrvgai(init, 0x03d4, addr, data);
  1039. }
  1040. /**
  1041. * INIT_ZM_CR_GROUP - opcode 0x54
  1042. *
  1043. */
  1044. static void
  1045. init_zm_cr_group(struct nvbios_init *init)
  1046. {
  1047. struct nvkm_bios *bios = init->bios;
  1048. u8 count = nvbios_rd08(bios, init->offset + 1);
  1049. trace("ZM_CR_GROUP\n");
  1050. init->offset += 2;
  1051. while (count--) {
  1052. u8 addr = nvbios_rd08(bios, init->offset + 0);
  1053. u8 data = nvbios_rd08(bios, init->offset + 1);
  1054. trace("\t\tC[0x%02x] = 0x%02x\n", addr, data);
  1055. init->offset += 2;
  1056. init_wrvgai(init, 0x03d4, addr, data);
  1057. }
  1058. }
  1059. /**
  1060. * INIT_CONDITION_TIME - opcode 0x56
  1061. *
  1062. */
  1063. static void
  1064. init_condition_time(struct nvbios_init *init)
  1065. {
  1066. struct nvkm_bios *bios = init->bios;
  1067. u8 cond = nvbios_rd08(bios, init->offset + 1);
  1068. u8 retry = nvbios_rd08(bios, init->offset + 2);
  1069. u8 wait = min((u16)retry * 50, 100);
  1070. trace("CONDITION_TIME\t0x%02x 0x%02x\n", cond, retry);
  1071. init->offset += 3;
  1072. if (!init_exec(init))
  1073. return;
  1074. while (wait--) {
  1075. if (init_condition_met(init, cond))
  1076. return;
  1077. mdelay(20);
  1078. }
  1079. init_exec_set(init, false);
  1080. }
  1081. /**
  1082. * INIT_LTIME - opcode 0x57
  1083. *
  1084. */
  1085. static void
  1086. init_ltime(struct nvbios_init *init)
  1087. {
  1088. struct nvkm_bios *bios = init->bios;
  1089. u16 msec = nvbios_rd16(bios, init->offset + 1);
  1090. trace("LTIME\t0x%04x\n", msec);
  1091. init->offset += 3;
  1092. if (init_exec(init))
  1093. mdelay(msec);
  1094. }
  1095. /**
  1096. * INIT_ZM_REG_SEQUENCE - opcode 0x58
  1097. *
  1098. */
  1099. static void
  1100. init_zm_reg_sequence(struct nvbios_init *init)
  1101. {
  1102. struct nvkm_bios *bios = init->bios;
  1103. u32 base = nvbios_rd32(bios, init->offset + 1);
  1104. u8 count = nvbios_rd08(bios, init->offset + 5);
  1105. trace("ZM_REG_SEQUENCE\t0x%02x\n", count);
  1106. init->offset += 6;
  1107. while (count--) {
  1108. u32 data = nvbios_rd32(bios, init->offset);
  1109. trace("\t\tR[0x%06x] = 0x%08x\n", base, data);
  1110. init->offset += 4;
  1111. init_wr32(init, base, data);
  1112. base += 4;
  1113. }
  1114. }
  1115. /**
  1116. * INIT_PLL_INDIRECT - opcode 0x59
  1117. *
  1118. */
  1119. static void
  1120. init_pll_indirect(struct nvbios_init *init)
  1121. {
  1122. struct nvkm_bios *bios = init->bios;
  1123. u32 reg = nvbios_rd32(bios, init->offset + 1);
  1124. u16 addr = nvbios_rd16(bios, init->offset + 5);
  1125. u32 freq = (u32)nvbios_rd16(bios, addr) * 1000;
  1126. trace("PLL_INDIRECT\tR[0x%06x] =PLL= VBIOS[%04x] = %dkHz\n",
  1127. reg, addr, freq);
  1128. init->offset += 7;
  1129. init_prog_pll(init, reg, freq);
  1130. }
  1131. /**
  1132. * INIT_ZM_REG_INDIRECT - opcode 0x5a
  1133. *
  1134. */
  1135. static void
  1136. init_zm_reg_indirect(struct nvbios_init *init)
  1137. {
  1138. struct nvkm_bios *bios = init->bios;
  1139. u32 reg = nvbios_rd32(bios, init->offset + 1);
  1140. u16 addr = nvbios_rd16(bios, init->offset + 5);
  1141. u32 data = nvbios_rd32(bios, addr);
  1142. trace("ZM_REG_INDIRECT\tR[0x%06x] = VBIOS[0x%04x] = 0x%08x\n",
  1143. reg, addr, data);
  1144. init->offset += 7;
  1145. init_wr32(init, addr, data);
  1146. }
  1147. /**
  1148. * INIT_SUB_DIRECT - opcode 0x5b
  1149. *
  1150. */
  1151. static void
  1152. init_sub_direct(struct nvbios_init *init)
  1153. {
  1154. struct nvkm_bios *bios = init->bios;
  1155. u16 addr = nvbios_rd16(bios, init->offset + 1);
  1156. u16 save;
  1157. trace("SUB_DIRECT\t0x%04x\n", addr);
  1158. if (init_exec(init)) {
  1159. save = init->offset;
  1160. init->offset = addr;
  1161. if (nvbios_exec(init)) {
  1162. error("error parsing sub-table\n");
  1163. return;
  1164. }
  1165. init->offset = save;
  1166. }
  1167. init->offset += 3;
  1168. }
  1169. /**
  1170. * INIT_JUMP - opcode 0x5c
  1171. *
  1172. */
  1173. static void
  1174. init_jump(struct nvbios_init *init)
  1175. {
  1176. struct nvkm_bios *bios = init->bios;
  1177. u16 offset = nvbios_rd16(bios, init->offset + 1);
  1178. trace("JUMP\t0x%04x\n", offset);
  1179. if (init_exec(init))
  1180. init->offset = offset;
  1181. else
  1182. init->offset += 3;
  1183. }
  1184. /**
  1185. * INIT_I2C_IF - opcode 0x5e
  1186. *
  1187. */
  1188. static void
  1189. init_i2c_if(struct nvbios_init *init)
  1190. {
  1191. struct nvkm_bios *bios = init->bios;
  1192. u8 index = nvbios_rd08(bios, init->offset + 1);
  1193. u8 addr = nvbios_rd08(bios, init->offset + 2);
  1194. u8 reg = nvbios_rd08(bios, init->offset + 3);
  1195. u8 mask = nvbios_rd08(bios, init->offset + 4);
  1196. u8 data = nvbios_rd08(bios, init->offset + 5);
  1197. u8 value;
  1198. trace("I2C_IF\tI2C[0x%02x][0x%02x][0x%02x] & 0x%02x == 0x%02x\n",
  1199. index, addr, reg, mask, data);
  1200. init->offset += 6;
  1201. init_exec_force(init, true);
  1202. value = init_rdi2cr(init, index, addr, reg);
  1203. if ((value & mask) != data)
  1204. init_exec_set(init, false);
  1205. init_exec_force(init, false);
  1206. }
  1207. /**
  1208. * INIT_COPY_NV_REG - opcode 0x5f
  1209. *
  1210. */
  1211. static void
  1212. init_copy_nv_reg(struct nvbios_init *init)
  1213. {
  1214. struct nvkm_bios *bios = init->bios;
  1215. u32 sreg = nvbios_rd32(bios, init->offset + 1);
  1216. u8 shift = nvbios_rd08(bios, init->offset + 5);
  1217. u32 smask = nvbios_rd32(bios, init->offset + 6);
  1218. u32 sxor = nvbios_rd32(bios, init->offset + 10);
  1219. u32 dreg = nvbios_rd32(bios, init->offset + 14);
  1220. u32 dmask = nvbios_rd32(bios, init->offset + 18);
  1221. u32 data;
  1222. trace("COPY_NV_REG\tR[0x%06x] &= 0x%08x |= "
  1223. "((R[0x%06x] %s 0x%02x) & 0x%08x ^ 0x%08x)\n",
  1224. dreg, dmask, sreg, (shift & 0x80) ? "<<" : ">>",
  1225. (shift & 0x80) ? (0x100 - shift) : shift, smask, sxor);
  1226. init->offset += 22;
  1227. data = init_shift(init_rd32(init, sreg), shift);
  1228. init_mask(init, dreg, ~dmask, (data & smask) ^ sxor);
  1229. }
  1230. /**
  1231. * INIT_ZM_INDEX_IO - opcode 0x62
  1232. *
  1233. */
  1234. static void
  1235. init_zm_index_io(struct nvbios_init *init)
  1236. {
  1237. struct nvkm_bios *bios = init->bios;
  1238. u16 port = nvbios_rd16(bios, init->offset + 1);
  1239. u8 index = nvbios_rd08(bios, init->offset + 3);
  1240. u8 data = nvbios_rd08(bios, init->offset + 4);
  1241. trace("ZM_INDEX_IO\tI[0x%04x][0x%02x] = 0x%02x\n", port, index, data);
  1242. init->offset += 5;
  1243. init_wrvgai(init, port, index, data);
  1244. }
  1245. /**
  1246. * INIT_COMPUTE_MEM - opcode 0x63
  1247. *
  1248. */
  1249. static void
  1250. init_compute_mem(struct nvbios_init *init)
  1251. {
  1252. struct nvkm_devinit *devinit = init->bios->subdev.device->devinit;
  1253. trace("COMPUTE_MEM\n");
  1254. init->offset += 1;
  1255. init_exec_force(init, true);
  1256. if (init_exec(init))
  1257. nvkm_devinit_meminit(devinit);
  1258. init_exec_force(init, false);
  1259. }
  1260. /**
  1261. * INIT_RESET - opcode 0x65
  1262. *
  1263. */
  1264. static void
  1265. init_reset(struct nvbios_init *init)
  1266. {
  1267. struct nvkm_bios *bios = init->bios;
  1268. u32 reg = nvbios_rd32(bios, init->offset + 1);
  1269. u32 data1 = nvbios_rd32(bios, init->offset + 5);
  1270. u32 data2 = nvbios_rd32(bios, init->offset + 9);
  1271. u32 savepci19;
  1272. trace("RESET\tR[0x%08x] = 0x%08x, 0x%08x", reg, data1, data2);
  1273. init->offset += 13;
  1274. init_exec_force(init, true);
  1275. savepci19 = init_mask(init, 0x00184c, 0x00000f00, 0x00000000);
  1276. init_wr32(init, reg, data1);
  1277. udelay(10);
  1278. init_wr32(init, reg, data2);
  1279. init_wr32(init, 0x00184c, savepci19);
  1280. init_mask(init, 0x001850, 0x00000001, 0x00000000);
  1281. init_exec_force(init, false);
  1282. }
  1283. /**
  1284. * INIT_CONFIGURE_MEM - opcode 0x66
  1285. *
  1286. */
  1287. static u16
  1288. init_configure_mem_clk(struct nvbios_init *init)
  1289. {
  1290. u16 mdata = bmp_mem_init_table(init->bios);
  1291. if (mdata)
  1292. mdata += (init_rdvgai(init, 0x03d4, 0x3c) >> 4) * 66;
  1293. return mdata;
  1294. }
  1295. static void
  1296. init_configure_mem(struct nvbios_init *init)
  1297. {
  1298. struct nvkm_bios *bios = init->bios;
  1299. u16 mdata, sdata;
  1300. u32 addr, data;
  1301. trace("CONFIGURE_MEM\n");
  1302. init->offset += 1;
  1303. if (bios->version.major > 2) {
  1304. init_done(init);
  1305. return;
  1306. }
  1307. init_exec_force(init, true);
  1308. mdata = init_configure_mem_clk(init);
  1309. sdata = bmp_sdr_seq_table(bios);
  1310. if (nvbios_rd08(bios, mdata) & 0x01)
  1311. sdata = bmp_ddr_seq_table(bios);
  1312. mdata += 6; /* skip to data */
  1313. data = init_rdvgai(init, 0x03c4, 0x01);
  1314. init_wrvgai(init, 0x03c4, 0x01, data | 0x20);
  1315. for (; (addr = nvbios_rd32(bios, sdata)) != 0xffffffff; sdata += 4) {
  1316. switch (addr) {
  1317. case 0x10021c: /* CKE_NORMAL */
  1318. case 0x1002d0: /* CMD_REFRESH */
  1319. case 0x1002d4: /* CMD_PRECHARGE */
  1320. data = 0x00000001;
  1321. break;
  1322. default:
  1323. data = nvbios_rd32(bios, mdata);
  1324. mdata += 4;
  1325. if (data == 0xffffffff)
  1326. continue;
  1327. break;
  1328. }
  1329. init_wr32(init, addr, data);
  1330. }
  1331. init_exec_force(init, false);
  1332. }
  1333. /**
  1334. * INIT_CONFIGURE_CLK - opcode 0x67
  1335. *
  1336. */
  1337. static void
  1338. init_configure_clk(struct nvbios_init *init)
  1339. {
  1340. struct nvkm_bios *bios = init->bios;
  1341. u16 mdata, clock;
  1342. trace("CONFIGURE_CLK\n");
  1343. init->offset += 1;
  1344. if (bios->version.major > 2) {
  1345. init_done(init);
  1346. return;
  1347. }
  1348. init_exec_force(init, true);
  1349. mdata = init_configure_mem_clk(init);
  1350. /* NVPLL */
  1351. clock = nvbios_rd16(bios, mdata + 4) * 10;
  1352. init_prog_pll(init, 0x680500, clock);
  1353. /* MPLL */
  1354. clock = nvbios_rd16(bios, mdata + 2) * 10;
  1355. if (nvbios_rd08(bios, mdata) & 0x01)
  1356. clock *= 2;
  1357. init_prog_pll(init, 0x680504, clock);
  1358. init_exec_force(init, false);
  1359. }
  1360. /**
  1361. * INIT_CONFIGURE_PREINIT - opcode 0x68
  1362. *
  1363. */
  1364. static void
  1365. init_configure_preinit(struct nvbios_init *init)
  1366. {
  1367. struct nvkm_bios *bios = init->bios;
  1368. u32 strap;
  1369. trace("CONFIGURE_PREINIT\n");
  1370. init->offset += 1;
  1371. if (bios->version.major > 2) {
  1372. init_done(init);
  1373. return;
  1374. }
  1375. init_exec_force(init, true);
  1376. strap = init_rd32(init, 0x101000);
  1377. strap = ((strap << 2) & 0xf0) | ((strap & 0x40) >> 6);
  1378. init_wrvgai(init, 0x03d4, 0x3c, strap);
  1379. init_exec_force(init, false);
  1380. }
  1381. /**
  1382. * INIT_IO - opcode 0x69
  1383. *
  1384. */
  1385. static void
  1386. init_io(struct nvbios_init *init)
  1387. {
  1388. struct nvkm_bios *bios = init->bios;
  1389. u16 port = nvbios_rd16(bios, init->offset + 1);
  1390. u8 mask = nvbios_rd16(bios, init->offset + 3);
  1391. u8 data = nvbios_rd16(bios, init->offset + 4);
  1392. u8 value;
  1393. trace("IO\t\tI[0x%04x] &= 0x%02x |= 0x%02x\n", port, mask, data);
  1394. init->offset += 5;
  1395. /* ummm.. yes.. should really figure out wtf this is and why it's
  1396. * needed some day.. it's almost certainly wrong, but, it also
  1397. * somehow makes things work...
  1398. */
  1399. if (bios->subdev.device->card_type >= NV_50 &&
  1400. port == 0x03c3 && data == 0x01) {
  1401. init_mask(init, 0x614100, 0xf0800000, 0x00800000);
  1402. init_mask(init, 0x00e18c, 0x00020000, 0x00020000);
  1403. init_mask(init, 0x614900, 0xf0800000, 0x00800000);
  1404. init_mask(init, 0x000200, 0x40000000, 0x00000000);
  1405. mdelay(10);
  1406. init_mask(init, 0x00e18c, 0x00020000, 0x00000000);
  1407. init_mask(init, 0x000200, 0x40000000, 0x40000000);
  1408. init_wr32(init, 0x614100, 0x00800018);
  1409. init_wr32(init, 0x614900, 0x00800018);
  1410. mdelay(10);
  1411. init_wr32(init, 0x614100, 0x10000018);
  1412. init_wr32(init, 0x614900, 0x10000018);
  1413. }
  1414. value = init_rdport(init, port) & mask;
  1415. init_wrport(init, port, data | value);
  1416. }
  1417. /**
  1418. * INIT_SUB - opcode 0x6b
  1419. *
  1420. */
  1421. static void
  1422. init_sub(struct nvbios_init *init)
  1423. {
  1424. struct nvkm_bios *bios = init->bios;
  1425. u8 index = nvbios_rd08(bios, init->offset + 1);
  1426. u16 addr, save;
  1427. trace("SUB\t0x%02x\n", index);
  1428. addr = init_script(bios, index);
  1429. if (addr && init_exec(init)) {
  1430. save = init->offset;
  1431. init->offset = addr;
  1432. if (nvbios_exec(init)) {
  1433. error("error parsing sub-table\n");
  1434. return;
  1435. }
  1436. init->offset = save;
  1437. }
  1438. init->offset += 2;
  1439. }
  1440. /**
  1441. * INIT_RAM_CONDITION - opcode 0x6d
  1442. *
  1443. */
  1444. static void
  1445. init_ram_condition(struct nvbios_init *init)
  1446. {
  1447. struct nvkm_bios *bios = init->bios;
  1448. u8 mask = nvbios_rd08(bios, init->offset + 1);
  1449. u8 value = nvbios_rd08(bios, init->offset + 2);
  1450. trace("RAM_CONDITION\t"
  1451. "(R[0x100000] & 0x%02x) == 0x%02x\n", mask, value);
  1452. init->offset += 3;
  1453. if ((init_rd32(init, 0x100000) & mask) != value)
  1454. init_exec_set(init, false);
  1455. }
  1456. /**
  1457. * INIT_NV_REG - opcode 0x6e
  1458. *
  1459. */
  1460. static void
  1461. init_nv_reg(struct nvbios_init *init)
  1462. {
  1463. struct nvkm_bios *bios = init->bios;
  1464. u32 reg = nvbios_rd32(bios, init->offset + 1);
  1465. u32 mask = nvbios_rd32(bios, init->offset + 5);
  1466. u32 data = nvbios_rd32(bios, init->offset + 9);
  1467. trace("NV_REG\tR[0x%06x] &= 0x%08x |= 0x%08x\n", reg, mask, data);
  1468. init->offset += 13;
  1469. init_mask(init, reg, ~mask, data);
  1470. }
  1471. /**
  1472. * INIT_MACRO - opcode 0x6f
  1473. *
  1474. */
  1475. static void
  1476. init_macro(struct nvbios_init *init)
  1477. {
  1478. struct nvkm_bios *bios = init->bios;
  1479. u8 macro = nvbios_rd08(bios, init->offset + 1);
  1480. u16 table;
  1481. trace("MACRO\t0x%02x\n", macro);
  1482. table = init_macro_table(init);
  1483. if (table) {
  1484. u32 addr = nvbios_rd32(bios, table + (macro * 8) + 0);
  1485. u32 data = nvbios_rd32(bios, table + (macro * 8) + 4);
  1486. trace("\t\tR[0x%06x] = 0x%08x\n", addr, data);
  1487. init_wr32(init, addr, data);
  1488. }
  1489. init->offset += 2;
  1490. }
  1491. /**
  1492. * INIT_RESUME - opcode 0x72
  1493. *
  1494. */
  1495. static void
  1496. init_resume(struct nvbios_init *init)
  1497. {
  1498. trace("RESUME\n");
  1499. init->offset += 1;
  1500. init_exec_set(init, true);
  1501. }
  1502. /**
  1503. * INIT_STRAP_CONDITION - opcode 0x73
  1504. *
  1505. */
  1506. static void
  1507. init_strap_condition(struct nvbios_init *init)
  1508. {
  1509. struct nvkm_bios *bios = init->bios;
  1510. u32 mask = nvbios_rd32(bios, init->offset + 1);
  1511. u32 value = nvbios_rd32(bios, init->offset + 5);
  1512. trace("STRAP_CONDITION\t(R[0x101000] & 0x%08x) == 0x%08x\n", mask, value);
  1513. init->offset += 9;
  1514. if ((init_rd32(init, 0x101000) & mask) != value)
  1515. init_exec_set(init, false);
  1516. }
  1517. /**
  1518. * INIT_TIME - opcode 0x74
  1519. *
  1520. */
  1521. static void
  1522. init_time(struct nvbios_init *init)
  1523. {
  1524. struct nvkm_bios *bios = init->bios;
  1525. u16 usec = nvbios_rd16(bios, init->offset + 1);
  1526. trace("TIME\t0x%04x\n", usec);
  1527. init->offset += 3;
  1528. if (init_exec(init)) {
  1529. if (usec < 1000)
  1530. udelay(usec);
  1531. else
  1532. mdelay((usec + 900) / 1000);
  1533. }
  1534. }
  1535. /**
  1536. * INIT_CONDITION - opcode 0x75
  1537. *
  1538. */
  1539. static void
  1540. init_condition(struct nvbios_init *init)
  1541. {
  1542. struct nvkm_bios *bios = init->bios;
  1543. u8 cond = nvbios_rd08(bios, init->offset + 1);
  1544. trace("CONDITION\t0x%02x\n", cond);
  1545. init->offset += 2;
  1546. if (!init_condition_met(init, cond))
  1547. init_exec_set(init, false);
  1548. }
  1549. /**
  1550. * INIT_IO_CONDITION - opcode 0x76
  1551. *
  1552. */
  1553. static void
  1554. init_io_condition(struct nvbios_init *init)
  1555. {
  1556. struct nvkm_bios *bios = init->bios;
  1557. u8 cond = nvbios_rd08(bios, init->offset + 1);
  1558. trace("IO_CONDITION\t0x%02x\n", cond);
  1559. init->offset += 2;
  1560. if (!init_io_condition_met(init, cond))
  1561. init_exec_set(init, false);
  1562. }
  1563. /**
  1564. * INIT_ZM_REG16 - opcode 0x77
  1565. *
  1566. */
  1567. static void
  1568. init_zm_reg16(struct nvbios_init *init)
  1569. {
  1570. struct nvkm_bios *bios = init->bios;
  1571. u32 addr = nvbios_rd32(bios, init->offset + 1);
  1572. u16 data = nvbios_rd16(bios, init->offset + 5);
  1573. trace("ZM_REG\tR[0x%06x] = 0x%04x\n", addr, data);
  1574. init->offset += 7;
  1575. init_wr32(init, addr, data);
  1576. }
  1577. /**
  1578. * INIT_INDEX_IO - opcode 0x78
  1579. *
  1580. */
  1581. static void
  1582. init_index_io(struct nvbios_init *init)
  1583. {
  1584. struct nvkm_bios *bios = init->bios;
  1585. u16 port = nvbios_rd16(bios, init->offset + 1);
  1586. u8 index = nvbios_rd16(bios, init->offset + 3);
  1587. u8 mask = nvbios_rd08(bios, init->offset + 4);
  1588. u8 data = nvbios_rd08(bios, init->offset + 5);
  1589. u8 value;
  1590. trace("INDEX_IO\tI[0x%04x][0x%02x] &= 0x%02x |= 0x%02x\n",
  1591. port, index, mask, data);
  1592. init->offset += 6;
  1593. value = init_rdvgai(init, port, index) & mask;
  1594. init_wrvgai(init, port, index, data | value);
  1595. }
  1596. /**
  1597. * INIT_PLL - opcode 0x79
  1598. *
  1599. */
  1600. static void
  1601. init_pll(struct nvbios_init *init)
  1602. {
  1603. struct nvkm_bios *bios = init->bios;
  1604. u32 reg = nvbios_rd32(bios, init->offset + 1);
  1605. u32 freq = nvbios_rd16(bios, init->offset + 5) * 10;
  1606. trace("PLL\tR[0x%06x] =PLL= %dkHz\n", reg, freq);
  1607. init->offset += 7;
  1608. init_prog_pll(init, reg, freq);
  1609. }
  1610. /**
  1611. * INIT_ZM_REG - opcode 0x7a
  1612. *
  1613. */
  1614. static void
  1615. init_zm_reg(struct nvbios_init *init)
  1616. {
  1617. struct nvkm_bios *bios = init->bios;
  1618. u32 addr = nvbios_rd32(bios, init->offset + 1);
  1619. u32 data = nvbios_rd32(bios, init->offset + 5);
  1620. trace("ZM_REG\tR[0x%06x] = 0x%08x\n", addr, data);
  1621. init->offset += 9;
  1622. if (addr == 0x000200)
  1623. data |= 0x00000001;
  1624. init_wr32(init, addr, data);
  1625. }
  1626. /**
  1627. * INIT_RAM_RESTRICT_PLL - opcde 0x87
  1628. *
  1629. */
  1630. static void
  1631. init_ram_restrict_pll(struct nvbios_init *init)
  1632. {
  1633. struct nvkm_bios *bios = init->bios;
  1634. u8 type = nvbios_rd08(bios, init->offset + 1);
  1635. u8 count = init_ram_restrict_group_count(init);
  1636. u8 strap = init_ram_restrict(init);
  1637. u8 cconf;
  1638. trace("RAM_RESTRICT_PLL\t0x%02x\n", type);
  1639. init->offset += 2;
  1640. for (cconf = 0; cconf < count; cconf++) {
  1641. u32 freq = nvbios_rd32(bios, init->offset);
  1642. if (cconf == strap) {
  1643. trace("%dkHz *\n", freq);
  1644. init_prog_pll(init, type, freq);
  1645. } else {
  1646. trace("%dkHz\n", freq);
  1647. }
  1648. init->offset += 4;
  1649. }
  1650. }
  1651. /**
  1652. * INIT_GPIO - opcode 0x8e
  1653. *
  1654. */
  1655. static void
  1656. init_gpio(struct nvbios_init *init)
  1657. {
  1658. struct nvkm_gpio *gpio = init->bios->subdev.device->gpio;
  1659. trace("GPIO\n");
  1660. init->offset += 1;
  1661. if (init_exec(init))
  1662. nvkm_gpio_reset(gpio, DCB_GPIO_UNUSED);
  1663. }
  1664. /**
  1665. * INIT_RAM_RESTRICT_ZM_GROUP - opcode 0x8f
  1666. *
  1667. */
  1668. static void
  1669. init_ram_restrict_zm_reg_group(struct nvbios_init *init)
  1670. {
  1671. struct nvkm_bios *bios = init->bios;
  1672. u32 addr = nvbios_rd32(bios, init->offset + 1);
  1673. u8 incr = nvbios_rd08(bios, init->offset + 5);
  1674. u8 num = nvbios_rd08(bios, init->offset + 6);
  1675. u8 count = init_ram_restrict_group_count(init);
  1676. u8 index = init_ram_restrict(init);
  1677. u8 i, j;
  1678. trace("RAM_RESTRICT_ZM_REG_GROUP\t"
  1679. "R[0x%08x] 0x%02x 0x%02x\n", addr, incr, num);
  1680. init->offset += 7;
  1681. for (i = 0; i < num; i++) {
  1682. trace("\tR[0x%06x] = {\n", addr);
  1683. for (j = 0; j < count; j++) {
  1684. u32 data = nvbios_rd32(bios, init->offset);
  1685. if (j == index) {
  1686. trace("\t\t0x%08x *\n", data);
  1687. init_wr32(init, addr, data);
  1688. } else {
  1689. trace("\t\t0x%08x\n", data);
  1690. }
  1691. init->offset += 4;
  1692. }
  1693. trace("\t}\n");
  1694. addr += incr;
  1695. }
  1696. }
  1697. /**
  1698. * INIT_COPY_ZM_REG - opcode 0x90
  1699. *
  1700. */
  1701. static void
  1702. init_copy_zm_reg(struct nvbios_init *init)
  1703. {
  1704. struct nvkm_bios *bios = init->bios;
  1705. u32 sreg = nvbios_rd32(bios, init->offset + 1);
  1706. u32 dreg = nvbios_rd32(bios, init->offset + 5);
  1707. trace("COPY_ZM_REG\tR[0x%06x] = R[0x%06x]\n", dreg, sreg);
  1708. init->offset += 9;
  1709. init_wr32(init, dreg, init_rd32(init, sreg));
  1710. }
  1711. /**
  1712. * INIT_ZM_REG_GROUP - opcode 0x91
  1713. *
  1714. */
  1715. static void
  1716. init_zm_reg_group(struct nvbios_init *init)
  1717. {
  1718. struct nvkm_bios *bios = init->bios;
  1719. u32 addr = nvbios_rd32(bios, init->offset + 1);
  1720. u8 count = nvbios_rd08(bios, init->offset + 5);
  1721. trace("ZM_REG_GROUP\tR[0x%06x] =\n", addr);
  1722. init->offset += 6;
  1723. while (count--) {
  1724. u32 data = nvbios_rd32(bios, init->offset);
  1725. trace("\t0x%08x\n", data);
  1726. init_wr32(init, addr, data);
  1727. init->offset += 4;
  1728. }
  1729. }
  1730. /**
  1731. * INIT_XLAT - opcode 0x96
  1732. *
  1733. */
  1734. static void
  1735. init_xlat(struct nvbios_init *init)
  1736. {
  1737. struct nvkm_bios *bios = init->bios;
  1738. u32 saddr = nvbios_rd32(bios, init->offset + 1);
  1739. u8 sshift = nvbios_rd08(bios, init->offset + 5);
  1740. u8 smask = nvbios_rd08(bios, init->offset + 6);
  1741. u8 index = nvbios_rd08(bios, init->offset + 7);
  1742. u32 daddr = nvbios_rd32(bios, init->offset + 8);
  1743. u32 dmask = nvbios_rd32(bios, init->offset + 12);
  1744. u8 shift = nvbios_rd08(bios, init->offset + 16);
  1745. u32 data;
  1746. trace("INIT_XLAT\tR[0x%06x] &= 0x%08x |= "
  1747. "(X%02x((R[0x%06x] %s 0x%02x) & 0x%02x) << 0x%02x)\n",
  1748. daddr, dmask, index, saddr, (sshift & 0x80) ? "<<" : ">>",
  1749. (sshift & 0x80) ? (0x100 - sshift) : sshift, smask, shift);
  1750. init->offset += 17;
  1751. data = init_shift(init_rd32(init, saddr), sshift) & smask;
  1752. data = init_xlat_(init, index, data) << shift;
  1753. init_mask(init, daddr, ~dmask, data);
  1754. }
  1755. /**
  1756. * INIT_ZM_MASK_ADD - opcode 0x97
  1757. *
  1758. */
  1759. static void
  1760. init_zm_mask_add(struct nvbios_init *init)
  1761. {
  1762. struct nvkm_bios *bios = init->bios;
  1763. u32 addr = nvbios_rd32(bios, init->offset + 1);
  1764. u32 mask = nvbios_rd32(bios, init->offset + 5);
  1765. u32 add = nvbios_rd32(bios, init->offset + 9);
  1766. u32 data;
  1767. trace("ZM_MASK_ADD\tR[0x%06x] &= 0x%08x += 0x%08x\n", addr, mask, add);
  1768. init->offset += 13;
  1769. data = init_rd32(init, addr);
  1770. data = (data & mask) | ((data + add) & ~mask);
  1771. init_wr32(init, addr, data);
  1772. }
  1773. /**
  1774. * INIT_AUXCH - opcode 0x98
  1775. *
  1776. */
  1777. static void
  1778. init_auxch(struct nvbios_init *init)
  1779. {
  1780. struct nvkm_bios *bios = init->bios;
  1781. u32 addr = nvbios_rd32(bios, init->offset + 1);
  1782. u8 count = nvbios_rd08(bios, init->offset + 5);
  1783. trace("AUXCH\tAUX[0x%08x] 0x%02x\n", addr, count);
  1784. init->offset += 6;
  1785. while (count--) {
  1786. u8 mask = nvbios_rd08(bios, init->offset + 0);
  1787. u8 data = nvbios_rd08(bios, init->offset + 1);
  1788. trace("\tAUX[0x%08x] &= 0x%02x |= 0x%02x\n", addr, mask, data);
  1789. mask = init_rdauxr(init, addr) & mask;
  1790. init_wrauxr(init, addr, mask | data);
  1791. init->offset += 2;
  1792. }
  1793. }
  1794. /**
  1795. * INIT_AUXCH - opcode 0x99
  1796. *
  1797. */
  1798. static void
  1799. init_zm_auxch(struct nvbios_init *init)
  1800. {
  1801. struct nvkm_bios *bios = init->bios;
  1802. u32 addr = nvbios_rd32(bios, init->offset + 1);
  1803. u8 count = nvbios_rd08(bios, init->offset + 5);
  1804. trace("ZM_AUXCH\tAUX[0x%08x] 0x%02x\n", addr, count);
  1805. init->offset += 6;
  1806. while (count--) {
  1807. u8 data = nvbios_rd08(bios, init->offset + 0);
  1808. trace("\tAUX[0x%08x] = 0x%02x\n", addr, data);
  1809. init_wrauxr(init, addr, data);
  1810. init->offset += 1;
  1811. }
  1812. }
  1813. /**
  1814. * INIT_I2C_LONG_IF - opcode 0x9a
  1815. *
  1816. */
  1817. static void
  1818. init_i2c_long_if(struct nvbios_init *init)
  1819. {
  1820. struct nvkm_bios *bios = init->bios;
  1821. u8 index = nvbios_rd08(bios, init->offset + 1);
  1822. u8 addr = nvbios_rd08(bios, init->offset + 2) >> 1;
  1823. u8 reglo = nvbios_rd08(bios, init->offset + 3);
  1824. u8 reghi = nvbios_rd08(bios, init->offset + 4);
  1825. u8 mask = nvbios_rd08(bios, init->offset + 5);
  1826. u8 data = nvbios_rd08(bios, init->offset + 6);
  1827. struct i2c_adapter *adap;
  1828. trace("I2C_LONG_IF\t"
  1829. "I2C[0x%02x][0x%02x][0x%02x%02x] & 0x%02x == 0x%02x\n",
  1830. index, addr, reglo, reghi, mask, data);
  1831. init->offset += 7;
  1832. adap = init_i2c(init, index);
  1833. if (adap) {
  1834. u8 i[2] = { reghi, reglo };
  1835. u8 o[1] = {};
  1836. struct i2c_msg msg[] = {
  1837. { .addr = addr, .flags = 0, .len = 2, .buf = i },
  1838. { .addr = addr, .flags = I2C_M_RD, .len = 1, .buf = o }
  1839. };
  1840. int ret;
  1841. ret = i2c_transfer(adap, msg, 2);
  1842. if (ret == 2 && ((o[0] & mask) == data))
  1843. return;
  1844. }
  1845. init_exec_set(init, false);
  1846. }
  1847. /**
  1848. * INIT_GPIO_NE - opcode 0xa9
  1849. *
  1850. */
  1851. static void
  1852. init_gpio_ne(struct nvbios_init *init)
  1853. {
  1854. struct nvkm_bios *bios = init->bios;
  1855. struct nvkm_gpio *gpio = bios->subdev.device->gpio;
  1856. struct dcb_gpio_func func;
  1857. u8 count = nvbios_rd08(bios, init->offset + 1);
  1858. u8 idx = 0, ver, len;
  1859. u16 data, i;
  1860. trace("GPIO_NE\t");
  1861. init->offset += 2;
  1862. for (i = init->offset; i < init->offset + count; i++)
  1863. cont("0x%02x ", nvbios_rd08(bios, i));
  1864. cont("\n");
  1865. while ((data = dcb_gpio_parse(bios, 0, idx++, &ver, &len, &func))) {
  1866. if (func.func != DCB_GPIO_UNUSED) {
  1867. for (i = init->offset; i < init->offset + count; i++) {
  1868. if (func.func == nvbios_rd08(bios, i))
  1869. break;
  1870. }
  1871. trace("\tFUNC[0x%02x]", func.func);
  1872. if (i == (init->offset + count)) {
  1873. cont(" *");
  1874. if (init_exec(init))
  1875. nvkm_gpio_reset(gpio, func.func);
  1876. }
  1877. cont("\n");
  1878. }
  1879. }
  1880. init->offset += count;
  1881. }
  1882. static struct nvbios_init_opcode {
  1883. void (*exec)(struct nvbios_init *);
  1884. } init_opcode[] = {
  1885. [0x32] = { init_io_restrict_prog },
  1886. [0x33] = { init_repeat },
  1887. [0x34] = { init_io_restrict_pll },
  1888. [0x36] = { init_end_repeat },
  1889. [0x37] = { init_copy },
  1890. [0x38] = { init_not },
  1891. [0x39] = { init_io_flag_condition },
  1892. [0x3a] = { init_dp_condition },
  1893. [0x3b] = { init_io_mask_or },
  1894. [0x3c] = { init_io_or },
  1895. [0x47] = { init_andn_reg },
  1896. [0x48] = { init_or_reg },
  1897. [0x49] = { init_idx_addr_latched },
  1898. [0x4a] = { init_io_restrict_pll2 },
  1899. [0x4b] = { init_pll2 },
  1900. [0x4c] = { init_i2c_byte },
  1901. [0x4d] = { init_zm_i2c_byte },
  1902. [0x4e] = { init_zm_i2c },
  1903. [0x4f] = { init_tmds },
  1904. [0x50] = { init_zm_tmds_group },
  1905. [0x51] = { init_cr_idx_adr_latch },
  1906. [0x52] = { init_cr },
  1907. [0x53] = { init_zm_cr },
  1908. [0x54] = { init_zm_cr_group },
  1909. [0x56] = { init_condition_time },
  1910. [0x57] = { init_ltime },
  1911. [0x58] = { init_zm_reg_sequence },
  1912. [0x59] = { init_pll_indirect },
  1913. [0x5a] = { init_zm_reg_indirect },
  1914. [0x5b] = { init_sub_direct },
  1915. [0x5c] = { init_jump },
  1916. [0x5e] = { init_i2c_if },
  1917. [0x5f] = { init_copy_nv_reg },
  1918. [0x62] = { init_zm_index_io },
  1919. [0x63] = { init_compute_mem },
  1920. [0x65] = { init_reset },
  1921. [0x66] = { init_configure_mem },
  1922. [0x67] = { init_configure_clk },
  1923. [0x68] = { init_configure_preinit },
  1924. [0x69] = { init_io },
  1925. [0x6b] = { init_sub },
  1926. [0x6d] = { init_ram_condition },
  1927. [0x6e] = { init_nv_reg },
  1928. [0x6f] = { init_macro },
  1929. [0x71] = { init_done },
  1930. [0x72] = { init_resume },
  1931. [0x73] = { init_strap_condition },
  1932. [0x74] = { init_time },
  1933. [0x75] = { init_condition },
  1934. [0x76] = { init_io_condition },
  1935. [0x77] = { init_zm_reg16 },
  1936. [0x78] = { init_index_io },
  1937. [0x79] = { init_pll },
  1938. [0x7a] = { init_zm_reg },
  1939. [0x87] = { init_ram_restrict_pll },
  1940. [0x8c] = { init_reserved },
  1941. [0x8d] = { init_reserved },
  1942. [0x8e] = { init_gpio },
  1943. [0x8f] = { init_ram_restrict_zm_reg_group },
  1944. [0x90] = { init_copy_zm_reg },
  1945. [0x91] = { init_zm_reg_group },
  1946. [0x92] = { init_reserved },
  1947. [0x96] = { init_xlat },
  1948. [0x97] = { init_zm_mask_add },
  1949. [0x98] = { init_auxch },
  1950. [0x99] = { init_zm_auxch },
  1951. [0x9a] = { init_i2c_long_if },
  1952. [0xa9] = { init_gpio_ne },
  1953. [0xaa] = { init_reserved },
  1954. };
  1955. #define init_opcode_nr (sizeof(init_opcode) / sizeof(init_opcode[0]))
  1956. int
  1957. nvbios_exec(struct nvbios_init *init)
  1958. {
  1959. init->nested++;
  1960. while (init->offset) {
  1961. u8 opcode = nvbios_rd08(init->bios, init->offset);
  1962. if (opcode >= init_opcode_nr || !init_opcode[opcode].exec) {
  1963. error("unknown opcode 0x%02x\n", opcode);
  1964. return -EINVAL;
  1965. }
  1966. init_opcode[opcode].exec(init);
  1967. }
  1968. init->nested--;
  1969. return 0;
  1970. }
  1971. int
  1972. nvbios_init(struct nvkm_subdev *subdev, bool execute)
  1973. {
  1974. struct nvkm_bios *bios = subdev->device->bios;
  1975. int ret = 0;
  1976. int i = -1;
  1977. u16 data;
  1978. if (execute)
  1979. nvkm_debug(subdev, "running init tables\n");
  1980. while (!ret && (data = (init_script(bios, ++i)))) {
  1981. struct nvbios_init init = {
  1982. .subdev = subdev,
  1983. .bios = bios,
  1984. .offset = data,
  1985. .outp = NULL,
  1986. .crtc = -1,
  1987. .execute = execute ? 1 : 0,
  1988. };
  1989. ret = nvbios_exec(&init);
  1990. }
  1991. /* the vbios parser will run this right after the normal init
  1992. * tables, whereas the binary driver appears to run it later.
  1993. */
  1994. if (!ret && (data = init_unknown_script(bios))) {
  1995. struct nvbios_init init = {
  1996. .subdev = subdev,
  1997. .bios = bios,
  1998. .offset = data,
  1999. .outp = NULL,
  2000. .crtc = -1,
  2001. .execute = execute ? 1 : 0,
  2002. };
  2003. ret = nvbios_exec(&init);
  2004. }
  2005. return ret;
  2006. }