init.c 55 KB

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