init.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332
  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_GENERIC_CONDITION - opcode 0x3a
  691. *
  692. */
  693. static void
  694. init_generic_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 size = nvbios_rd08(bios, init->offset + 2);
  700. u8 ver, hdr, cnt, len;
  701. u16 data;
  702. trace("GENERIC_CONDITION\t0x%02x 0x%02x\n", cond, size);
  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("INIT_GENERIC_CONDITON: unknown 0x%02x\n", cond);
  730. init->offset += size;
  731. break;
  732. }
  733. }
  734. /**
  735. * INIT_IO_MASK_OR - opcode 0x3b
  736. *
  737. */
  738. static void
  739. init_io_mask_or(struct nvbios_init *init)
  740. {
  741. struct nvkm_bios *bios = init->bios;
  742. u8 index = nvbios_rd08(bios, init->offset + 1);
  743. u8 or = init_or(init);
  744. u8 data;
  745. trace("IO_MASK_OR\t0x03d4[0x%02x] &= ~(1 << 0x%02x)\n", index, or);
  746. init->offset += 2;
  747. data = init_rdvgai(init, 0x03d4, index);
  748. init_wrvgai(init, 0x03d4, index, data &= ~(1 << or));
  749. }
  750. /**
  751. * INIT_IO_OR - opcode 0x3c
  752. *
  753. */
  754. static void
  755. init_io_or(struct nvbios_init *init)
  756. {
  757. struct nvkm_bios *bios = init->bios;
  758. u8 index = nvbios_rd08(bios, init->offset + 1);
  759. u8 or = init_or(init);
  760. u8 data;
  761. trace("IO_OR\t0x03d4[0x%02x] |= (1 << 0x%02x)\n", index, or);
  762. init->offset += 2;
  763. data = init_rdvgai(init, 0x03d4, index);
  764. init_wrvgai(init, 0x03d4, index, data | (1 << or));
  765. }
  766. /**
  767. * INIT_ANDN_REG - opcode 0x47
  768. *
  769. */
  770. static void
  771. init_andn_reg(struct nvbios_init *init)
  772. {
  773. struct nvkm_bios *bios = init->bios;
  774. u32 reg = nvbios_rd32(bios, init->offset + 1);
  775. u32 mask = nvbios_rd32(bios, init->offset + 5);
  776. trace("ANDN_REG\tR[0x%06x] &= ~0x%08x\n", reg, mask);
  777. init->offset += 9;
  778. init_mask(init, reg, mask, 0);
  779. }
  780. /**
  781. * INIT_OR_REG - opcode 0x48
  782. *
  783. */
  784. static void
  785. init_or_reg(struct nvbios_init *init)
  786. {
  787. struct nvkm_bios *bios = init->bios;
  788. u32 reg = nvbios_rd32(bios, init->offset + 1);
  789. u32 mask = nvbios_rd32(bios, init->offset + 5);
  790. trace("OR_REG\tR[0x%06x] |= 0x%08x\n", reg, mask);
  791. init->offset += 9;
  792. init_mask(init, reg, 0, mask);
  793. }
  794. /**
  795. * INIT_INDEX_ADDRESS_LATCHED - opcode 0x49
  796. *
  797. */
  798. static void
  799. init_idx_addr_latched(struct nvbios_init *init)
  800. {
  801. struct nvkm_bios *bios = init->bios;
  802. u32 creg = nvbios_rd32(bios, init->offset + 1);
  803. u32 dreg = nvbios_rd32(bios, init->offset + 5);
  804. u32 mask = nvbios_rd32(bios, init->offset + 9);
  805. u32 data = nvbios_rd32(bios, init->offset + 13);
  806. u8 count = nvbios_rd08(bios, init->offset + 17);
  807. trace("INDEX_ADDRESS_LATCHED\tR[0x%06x] : R[0x%06x]\n", creg, dreg);
  808. trace("\tCTRL &= 0x%08x |= 0x%08x\n", mask, data);
  809. init->offset += 18;
  810. while (count--) {
  811. u8 iaddr = nvbios_rd08(bios, init->offset + 0);
  812. u8 idata = nvbios_rd08(bios, init->offset + 1);
  813. trace("\t[0x%02x] = 0x%02x\n", iaddr, idata);
  814. init->offset += 2;
  815. init_wr32(init, dreg, idata);
  816. init_mask(init, creg, ~mask, data | iaddr);
  817. }
  818. }
  819. /**
  820. * INIT_IO_RESTRICT_PLL2 - opcode 0x4a
  821. *
  822. */
  823. static void
  824. init_io_restrict_pll2(struct nvbios_init *init)
  825. {
  826. struct nvkm_bios *bios = init->bios;
  827. u16 port = nvbios_rd16(bios, init->offset + 1);
  828. u8 index = nvbios_rd08(bios, init->offset + 3);
  829. u8 mask = nvbios_rd08(bios, init->offset + 4);
  830. u8 shift = nvbios_rd08(bios, init->offset + 5);
  831. u8 count = nvbios_rd08(bios, init->offset + 6);
  832. u32 reg = nvbios_rd32(bios, init->offset + 7);
  833. u8 conf, i;
  834. trace("IO_RESTRICT_PLL2\t"
  835. "R[0x%06x] =PLL= ((0x%04x[0x%02x] & 0x%02x) >> 0x%02x) [{\n",
  836. reg, port, index, mask, shift);
  837. init->offset += 11;
  838. conf = (init_rdvgai(init, port, index) & mask) >> shift;
  839. for (i = 0; i < count; i++) {
  840. u32 freq = nvbios_rd32(bios, init->offset);
  841. if (i == conf) {
  842. trace("\t%dkHz *\n", freq);
  843. init_prog_pll(init, reg, freq);
  844. } else {
  845. trace("\t%dkHz\n", freq);
  846. }
  847. init->offset += 4;
  848. }
  849. trace("}]\n");
  850. }
  851. /**
  852. * INIT_PLL2 - opcode 0x4b
  853. *
  854. */
  855. static void
  856. init_pll2(struct nvbios_init *init)
  857. {
  858. struct nvkm_bios *bios = init->bios;
  859. u32 reg = nvbios_rd32(bios, init->offset + 1);
  860. u32 freq = nvbios_rd32(bios, init->offset + 5);
  861. trace("PLL2\tR[0x%06x] =PLL= %dkHz\n", reg, freq);
  862. init->offset += 9;
  863. init_prog_pll(init, reg, freq);
  864. }
  865. /**
  866. * INIT_I2C_BYTE - opcode 0x4c
  867. *
  868. */
  869. static void
  870. init_i2c_byte(struct nvbios_init *init)
  871. {
  872. struct nvkm_bios *bios = init->bios;
  873. u8 index = nvbios_rd08(bios, init->offset + 1);
  874. u8 addr = nvbios_rd08(bios, init->offset + 2) >> 1;
  875. u8 count = nvbios_rd08(bios, init->offset + 3);
  876. trace("I2C_BYTE\tI2C[0x%02x][0x%02x]\n", index, addr);
  877. init->offset += 4;
  878. while (count--) {
  879. u8 reg = nvbios_rd08(bios, init->offset + 0);
  880. u8 mask = nvbios_rd08(bios, init->offset + 1);
  881. u8 data = nvbios_rd08(bios, init->offset + 2);
  882. int val;
  883. trace("\t[0x%02x] &= 0x%02x |= 0x%02x\n", reg, mask, data);
  884. init->offset += 3;
  885. val = init_rdi2cr(init, index, addr, reg);
  886. if (val < 0)
  887. continue;
  888. init_wri2cr(init, index, addr, reg, (val & mask) | data);
  889. }
  890. }
  891. /**
  892. * INIT_ZM_I2C_BYTE - opcode 0x4d
  893. *
  894. */
  895. static void
  896. init_zm_i2c_byte(struct nvbios_init *init)
  897. {
  898. struct nvkm_bios *bios = init->bios;
  899. u8 index = nvbios_rd08(bios, init->offset + 1);
  900. u8 addr = nvbios_rd08(bios, init->offset + 2) >> 1;
  901. u8 count = nvbios_rd08(bios, init->offset + 3);
  902. trace("ZM_I2C_BYTE\tI2C[0x%02x][0x%02x]\n", index, addr);
  903. init->offset += 4;
  904. while (count--) {
  905. u8 reg = nvbios_rd08(bios, init->offset + 0);
  906. u8 data = nvbios_rd08(bios, init->offset + 1);
  907. trace("\t[0x%02x] = 0x%02x\n", reg, data);
  908. init->offset += 2;
  909. init_wri2cr(init, index, addr, reg, data);
  910. }
  911. }
  912. /**
  913. * INIT_ZM_I2C - opcode 0x4e
  914. *
  915. */
  916. static void
  917. init_zm_i2c(struct nvbios_init *init)
  918. {
  919. struct nvkm_bios *bios = init->bios;
  920. u8 index = nvbios_rd08(bios, init->offset + 1);
  921. u8 addr = nvbios_rd08(bios, init->offset + 2) >> 1;
  922. u8 count = nvbios_rd08(bios, init->offset + 3);
  923. u8 data[256], i;
  924. trace("ZM_I2C\tI2C[0x%02x][0x%02x]\n", index, addr);
  925. init->offset += 4;
  926. for (i = 0; i < count; i++) {
  927. data[i] = nvbios_rd08(bios, init->offset);
  928. trace("\t0x%02x\n", data[i]);
  929. init->offset++;
  930. }
  931. if (init_exec(init)) {
  932. struct i2c_adapter *adap = init_i2c(init, index);
  933. struct i2c_msg msg = {
  934. .addr = addr, .flags = 0, .len = count, .buf = data,
  935. };
  936. int ret;
  937. if (adap && (ret = i2c_transfer(adap, &msg, 1)) != 1)
  938. warn("i2c wr failed, %d\n", ret);
  939. }
  940. }
  941. /**
  942. * INIT_TMDS - opcode 0x4f
  943. *
  944. */
  945. static void
  946. init_tmds(struct nvbios_init *init)
  947. {
  948. struct nvkm_bios *bios = init->bios;
  949. u8 tmds = nvbios_rd08(bios, init->offset + 1);
  950. u8 addr = nvbios_rd08(bios, init->offset + 2);
  951. u8 mask = nvbios_rd08(bios, init->offset + 3);
  952. u8 data = nvbios_rd08(bios, init->offset + 4);
  953. u32 reg = init_tmds_reg(init, tmds);
  954. trace("TMDS\tT[0x%02x][0x%02x] &= 0x%02x |= 0x%02x\n",
  955. tmds, addr, mask, data);
  956. init->offset += 5;
  957. if (reg == 0)
  958. return;
  959. init_wr32(init, reg + 0, addr | 0x00010000);
  960. init_wr32(init, reg + 4, data | (init_rd32(init, reg + 4) & mask));
  961. init_wr32(init, reg + 0, addr);
  962. }
  963. /**
  964. * INIT_ZM_TMDS_GROUP - opcode 0x50
  965. *
  966. */
  967. static void
  968. init_zm_tmds_group(struct nvbios_init *init)
  969. {
  970. struct nvkm_bios *bios = init->bios;
  971. u8 tmds = nvbios_rd08(bios, init->offset + 1);
  972. u8 count = nvbios_rd08(bios, init->offset + 2);
  973. u32 reg = init_tmds_reg(init, tmds);
  974. trace("TMDS_ZM_GROUP\tT[0x%02x]\n", tmds);
  975. init->offset += 3;
  976. while (count--) {
  977. u8 addr = nvbios_rd08(bios, init->offset + 0);
  978. u8 data = nvbios_rd08(bios, init->offset + 1);
  979. trace("\t[0x%02x] = 0x%02x\n", addr, data);
  980. init->offset += 2;
  981. init_wr32(init, reg + 4, data);
  982. init_wr32(init, reg + 0, addr);
  983. }
  984. }
  985. /**
  986. * INIT_CR_INDEX_ADDRESS_LATCHED - opcode 0x51
  987. *
  988. */
  989. static void
  990. init_cr_idx_adr_latch(struct nvbios_init *init)
  991. {
  992. struct nvkm_bios *bios = init->bios;
  993. u8 addr0 = nvbios_rd08(bios, init->offset + 1);
  994. u8 addr1 = nvbios_rd08(bios, init->offset + 2);
  995. u8 base = nvbios_rd08(bios, init->offset + 3);
  996. u8 count = nvbios_rd08(bios, init->offset + 4);
  997. u8 save0;
  998. trace("CR_INDEX_ADDR C[%02x] C[%02x]\n", addr0, addr1);
  999. init->offset += 5;
  1000. save0 = init_rdvgai(init, 0x03d4, addr0);
  1001. while (count--) {
  1002. u8 data = nvbios_rd08(bios, init->offset);
  1003. trace("\t\t[0x%02x] = 0x%02x\n", base, data);
  1004. init->offset += 1;
  1005. init_wrvgai(init, 0x03d4, addr0, base++);
  1006. init_wrvgai(init, 0x03d4, addr1, data);
  1007. }
  1008. init_wrvgai(init, 0x03d4, addr0, save0);
  1009. }
  1010. /**
  1011. * INIT_CR - opcode 0x52
  1012. *
  1013. */
  1014. static void
  1015. init_cr(struct nvbios_init *init)
  1016. {
  1017. struct nvkm_bios *bios = init->bios;
  1018. u8 addr = nvbios_rd08(bios, init->offset + 1);
  1019. u8 mask = nvbios_rd08(bios, init->offset + 2);
  1020. u8 data = nvbios_rd08(bios, init->offset + 3);
  1021. u8 val;
  1022. trace("CR\t\tC[0x%02x] &= 0x%02x |= 0x%02x\n", addr, mask, data);
  1023. init->offset += 4;
  1024. val = init_rdvgai(init, 0x03d4, addr) & mask;
  1025. init_wrvgai(init, 0x03d4, addr, val | data);
  1026. }
  1027. /**
  1028. * INIT_ZM_CR - opcode 0x53
  1029. *
  1030. */
  1031. static void
  1032. init_zm_cr(struct nvbios_init *init)
  1033. {
  1034. struct nvkm_bios *bios = init->bios;
  1035. u8 addr = nvbios_rd08(bios, init->offset + 1);
  1036. u8 data = nvbios_rd08(bios, init->offset + 2);
  1037. trace("ZM_CR\tC[0x%02x] = 0x%02x\n", addr, data);
  1038. init->offset += 3;
  1039. init_wrvgai(init, 0x03d4, addr, data);
  1040. }
  1041. /**
  1042. * INIT_ZM_CR_GROUP - opcode 0x54
  1043. *
  1044. */
  1045. static void
  1046. init_zm_cr_group(struct nvbios_init *init)
  1047. {
  1048. struct nvkm_bios *bios = init->bios;
  1049. u8 count = nvbios_rd08(bios, init->offset + 1);
  1050. trace("ZM_CR_GROUP\n");
  1051. init->offset += 2;
  1052. while (count--) {
  1053. u8 addr = nvbios_rd08(bios, init->offset + 0);
  1054. u8 data = nvbios_rd08(bios, init->offset + 1);
  1055. trace("\t\tC[0x%02x] = 0x%02x\n", addr, data);
  1056. init->offset += 2;
  1057. init_wrvgai(init, 0x03d4, addr, data);
  1058. }
  1059. }
  1060. /**
  1061. * INIT_CONDITION_TIME - opcode 0x56
  1062. *
  1063. */
  1064. static void
  1065. init_condition_time(struct nvbios_init *init)
  1066. {
  1067. struct nvkm_bios *bios = init->bios;
  1068. u8 cond = nvbios_rd08(bios, init->offset + 1);
  1069. u8 retry = nvbios_rd08(bios, init->offset + 2);
  1070. u8 wait = min((u16)retry * 50, 100);
  1071. trace("CONDITION_TIME\t0x%02x 0x%02x\n", cond, retry);
  1072. init->offset += 3;
  1073. if (!init_exec(init))
  1074. return;
  1075. while (wait--) {
  1076. if (init_condition_met(init, cond))
  1077. return;
  1078. mdelay(20);
  1079. }
  1080. init_exec_set(init, false);
  1081. }
  1082. /**
  1083. * INIT_LTIME - opcode 0x57
  1084. *
  1085. */
  1086. static void
  1087. init_ltime(struct nvbios_init *init)
  1088. {
  1089. struct nvkm_bios *bios = init->bios;
  1090. u16 msec = nvbios_rd16(bios, init->offset + 1);
  1091. trace("LTIME\t0x%04x\n", msec);
  1092. init->offset += 3;
  1093. if (init_exec(init))
  1094. mdelay(msec);
  1095. }
  1096. /**
  1097. * INIT_ZM_REG_SEQUENCE - opcode 0x58
  1098. *
  1099. */
  1100. static void
  1101. init_zm_reg_sequence(struct nvbios_init *init)
  1102. {
  1103. struct nvkm_bios *bios = init->bios;
  1104. u32 base = nvbios_rd32(bios, init->offset + 1);
  1105. u8 count = nvbios_rd08(bios, init->offset + 5);
  1106. trace("ZM_REG_SEQUENCE\t0x%02x\n", count);
  1107. init->offset += 6;
  1108. while (count--) {
  1109. u32 data = nvbios_rd32(bios, init->offset);
  1110. trace("\t\tR[0x%06x] = 0x%08x\n", base, data);
  1111. init->offset += 4;
  1112. init_wr32(init, base, data);
  1113. base += 4;
  1114. }
  1115. }
  1116. /**
  1117. * INIT_PLL_INDIRECT - opcode 0x59
  1118. *
  1119. */
  1120. static void
  1121. init_pll_indirect(struct nvbios_init *init)
  1122. {
  1123. struct nvkm_bios *bios = init->bios;
  1124. u32 reg = nvbios_rd32(bios, init->offset + 1);
  1125. u16 addr = nvbios_rd16(bios, init->offset + 5);
  1126. u32 freq = (u32)nvbios_rd16(bios, addr) * 1000;
  1127. trace("PLL_INDIRECT\tR[0x%06x] =PLL= VBIOS[%04x] = %dkHz\n",
  1128. reg, addr, freq);
  1129. init->offset += 7;
  1130. init_prog_pll(init, reg, freq);
  1131. }
  1132. /**
  1133. * INIT_ZM_REG_INDIRECT - opcode 0x5a
  1134. *
  1135. */
  1136. static void
  1137. init_zm_reg_indirect(struct nvbios_init *init)
  1138. {
  1139. struct nvkm_bios *bios = init->bios;
  1140. u32 reg = nvbios_rd32(bios, init->offset + 1);
  1141. u16 addr = nvbios_rd16(bios, init->offset + 5);
  1142. u32 data = nvbios_rd32(bios, addr);
  1143. trace("ZM_REG_INDIRECT\tR[0x%06x] = VBIOS[0x%04x] = 0x%08x\n",
  1144. reg, addr, data);
  1145. init->offset += 7;
  1146. init_wr32(init, addr, data);
  1147. }
  1148. /**
  1149. * INIT_SUB_DIRECT - opcode 0x5b
  1150. *
  1151. */
  1152. static void
  1153. init_sub_direct(struct nvbios_init *init)
  1154. {
  1155. struct nvkm_bios *bios = init->bios;
  1156. u16 addr = nvbios_rd16(bios, init->offset + 1);
  1157. u16 save;
  1158. trace("SUB_DIRECT\t0x%04x\n", addr);
  1159. if (init_exec(init)) {
  1160. save = init->offset;
  1161. init->offset = addr;
  1162. if (nvbios_exec(init)) {
  1163. error("error parsing sub-table\n");
  1164. return;
  1165. }
  1166. init->offset = save;
  1167. }
  1168. init->offset += 3;
  1169. }
  1170. /**
  1171. * INIT_JUMP - opcode 0x5c
  1172. *
  1173. */
  1174. static void
  1175. init_jump(struct nvbios_init *init)
  1176. {
  1177. struct nvkm_bios *bios = init->bios;
  1178. u16 offset = nvbios_rd16(bios, init->offset + 1);
  1179. trace("JUMP\t0x%04x\n", offset);
  1180. if (init_exec(init))
  1181. init->offset = offset;
  1182. else
  1183. init->offset += 3;
  1184. }
  1185. /**
  1186. * INIT_I2C_IF - opcode 0x5e
  1187. *
  1188. */
  1189. static void
  1190. init_i2c_if(struct nvbios_init *init)
  1191. {
  1192. struct nvkm_bios *bios = init->bios;
  1193. u8 index = nvbios_rd08(bios, init->offset + 1);
  1194. u8 addr = nvbios_rd08(bios, init->offset + 2);
  1195. u8 reg = nvbios_rd08(bios, init->offset + 3);
  1196. u8 mask = nvbios_rd08(bios, init->offset + 4);
  1197. u8 data = nvbios_rd08(bios, init->offset + 5);
  1198. u8 value;
  1199. trace("I2C_IF\tI2C[0x%02x][0x%02x][0x%02x] & 0x%02x == 0x%02x\n",
  1200. index, addr, reg, mask, data);
  1201. init->offset += 6;
  1202. init_exec_force(init, true);
  1203. value = init_rdi2cr(init, index, addr, reg);
  1204. if ((value & mask) != data)
  1205. init_exec_set(init, false);
  1206. init_exec_force(init, false);
  1207. }
  1208. /**
  1209. * INIT_COPY_NV_REG - opcode 0x5f
  1210. *
  1211. */
  1212. static void
  1213. init_copy_nv_reg(struct nvbios_init *init)
  1214. {
  1215. struct nvkm_bios *bios = init->bios;
  1216. u32 sreg = nvbios_rd32(bios, init->offset + 1);
  1217. u8 shift = nvbios_rd08(bios, init->offset + 5);
  1218. u32 smask = nvbios_rd32(bios, init->offset + 6);
  1219. u32 sxor = nvbios_rd32(bios, init->offset + 10);
  1220. u32 dreg = nvbios_rd32(bios, init->offset + 14);
  1221. u32 dmask = nvbios_rd32(bios, init->offset + 18);
  1222. u32 data;
  1223. trace("COPY_NV_REG\tR[0x%06x] &= 0x%08x |= "
  1224. "((R[0x%06x] %s 0x%02x) & 0x%08x ^ 0x%08x)\n",
  1225. dreg, dmask, sreg, (shift & 0x80) ? "<<" : ">>",
  1226. (shift & 0x80) ? (0x100 - shift) : shift, smask, sxor);
  1227. init->offset += 22;
  1228. data = init_shift(init_rd32(init, sreg), shift);
  1229. init_mask(init, dreg, ~dmask, (data & smask) ^ sxor);
  1230. }
  1231. /**
  1232. * INIT_ZM_INDEX_IO - opcode 0x62
  1233. *
  1234. */
  1235. static void
  1236. init_zm_index_io(struct nvbios_init *init)
  1237. {
  1238. struct nvkm_bios *bios = init->bios;
  1239. u16 port = nvbios_rd16(bios, init->offset + 1);
  1240. u8 index = nvbios_rd08(bios, init->offset + 3);
  1241. u8 data = nvbios_rd08(bios, init->offset + 4);
  1242. trace("ZM_INDEX_IO\tI[0x%04x][0x%02x] = 0x%02x\n", port, index, data);
  1243. init->offset += 5;
  1244. init_wrvgai(init, port, index, data);
  1245. }
  1246. /**
  1247. * INIT_COMPUTE_MEM - opcode 0x63
  1248. *
  1249. */
  1250. static void
  1251. init_compute_mem(struct nvbios_init *init)
  1252. {
  1253. struct nvkm_devinit *devinit = init->bios->subdev.device->devinit;
  1254. trace("COMPUTE_MEM\n");
  1255. init->offset += 1;
  1256. init_exec_force(init, true);
  1257. if (init_exec(init))
  1258. nvkm_devinit_meminit(devinit);
  1259. init_exec_force(init, false);
  1260. }
  1261. /**
  1262. * INIT_RESET - opcode 0x65
  1263. *
  1264. */
  1265. static void
  1266. init_reset(struct nvbios_init *init)
  1267. {
  1268. struct nvkm_bios *bios = init->bios;
  1269. u32 reg = nvbios_rd32(bios, init->offset + 1);
  1270. u32 data1 = nvbios_rd32(bios, init->offset + 5);
  1271. u32 data2 = nvbios_rd32(bios, init->offset + 9);
  1272. u32 savepci19;
  1273. trace("RESET\tR[0x%08x] = 0x%08x, 0x%08x", reg, data1, data2);
  1274. init->offset += 13;
  1275. init_exec_force(init, true);
  1276. savepci19 = init_mask(init, 0x00184c, 0x00000f00, 0x00000000);
  1277. init_wr32(init, reg, data1);
  1278. udelay(10);
  1279. init_wr32(init, reg, data2);
  1280. init_wr32(init, 0x00184c, savepci19);
  1281. init_mask(init, 0x001850, 0x00000001, 0x00000000);
  1282. init_exec_force(init, false);
  1283. }
  1284. /**
  1285. * INIT_CONFIGURE_MEM - opcode 0x66
  1286. *
  1287. */
  1288. static u16
  1289. init_configure_mem_clk(struct nvbios_init *init)
  1290. {
  1291. u16 mdata = bmp_mem_init_table(init->bios);
  1292. if (mdata)
  1293. mdata += (init_rdvgai(init, 0x03d4, 0x3c) >> 4) * 66;
  1294. return mdata;
  1295. }
  1296. static void
  1297. init_configure_mem(struct nvbios_init *init)
  1298. {
  1299. struct nvkm_bios *bios = init->bios;
  1300. u16 mdata, sdata;
  1301. u32 addr, data;
  1302. trace("CONFIGURE_MEM\n");
  1303. init->offset += 1;
  1304. if (bios->version.major > 2) {
  1305. init_done(init);
  1306. return;
  1307. }
  1308. init_exec_force(init, true);
  1309. mdata = init_configure_mem_clk(init);
  1310. sdata = bmp_sdr_seq_table(bios);
  1311. if (nvbios_rd08(bios, mdata) & 0x01)
  1312. sdata = bmp_ddr_seq_table(bios);
  1313. mdata += 6; /* skip to data */
  1314. data = init_rdvgai(init, 0x03c4, 0x01);
  1315. init_wrvgai(init, 0x03c4, 0x01, data | 0x20);
  1316. for (; (addr = nvbios_rd32(bios, sdata)) != 0xffffffff; sdata += 4) {
  1317. switch (addr) {
  1318. case 0x10021c: /* CKE_NORMAL */
  1319. case 0x1002d0: /* CMD_REFRESH */
  1320. case 0x1002d4: /* CMD_PRECHARGE */
  1321. data = 0x00000001;
  1322. break;
  1323. default:
  1324. data = nvbios_rd32(bios, mdata);
  1325. mdata += 4;
  1326. if (data == 0xffffffff)
  1327. continue;
  1328. break;
  1329. }
  1330. init_wr32(init, addr, data);
  1331. }
  1332. init_exec_force(init, false);
  1333. }
  1334. /**
  1335. * INIT_CONFIGURE_CLK - opcode 0x67
  1336. *
  1337. */
  1338. static void
  1339. init_configure_clk(struct nvbios_init *init)
  1340. {
  1341. struct nvkm_bios *bios = init->bios;
  1342. u16 mdata, clock;
  1343. trace("CONFIGURE_CLK\n");
  1344. init->offset += 1;
  1345. if (bios->version.major > 2) {
  1346. init_done(init);
  1347. return;
  1348. }
  1349. init_exec_force(init, true);
  1350. mdata = init_configure_mem_clk(init);
  1351. /* NVPLL */
  1352. clock = nvbios_rd16(bios, mdata + 4) * 10;
  1353. init_prog_pll(init, 0x680500, clock);
  1354. /* MPLL */
  1355. clock = nvbios_rd16(bios, mdata + 2) * 10;
  1356. if (nvbios_rd08(bios, mdata) & 0x01)
  1357. clock *= 2;
  1358. init_prog_pll(init, 0x680504, clock);
  1359. init_exec_force(init, false);
  1360. }
  1361. /**
  1362. * INIT_CONFIGURE_PREINIT - opcode 0x68
  1363. *
  1364. */
  1365. static void
  1366. init_configure_preinit(struct nvbios_init *init)
  1367. {
  1368. struct nvkm_bios *bios = init->bios;
  1369. u32 strap;
  1370. trace("CONFIGURE_PREINIT\n");
  1371. init->offset += 1;
  1372. if (bios->version.major > 2) {
  1373. init_done(init);
  1374. return;
  1375. }
  1376. init_exec_force(init, true);
  1377. strap = init_rd32(init, 0x101000);
  1378. strap = ((strap << 2) & 0xf0) | ((strap & 0x40) >> 6);
  1379. init_wrvgai(init, 0x03d4, 0x3c, strap);
  1380. init_exec_force(init, false);
  1381. }
  1382. /**
  1383. * INIT_IO - opcode 0x69
  1384. *
  1385. */
  1386. static void
  1387. init_io(struct nvbios_init *init)
  1388. {
  1389. struct nvkm_bios *bios = init->bios;
  1390. u16 port = nvbios_rd16(bios, init->offset + 1);
  1391. u8 mask = nvbios_rd16(bios, init->offset + 3);
  1392. u8 data = nvbios_rd16(bios, init->offset + 4);
  1393. u8 value;
  1394. trace("IO\t\tI[0x%04x] &= 0x%02x |= 0x%02x\n", port, mask, data);
  1395. init->offset += 5;
  1396. /* ummm.. yes.. should really figure out wtf this is and why it's
  1397. * needed some day.. it's almost certainly wrong, but, it also
  1398. * somehow makes things work...
  1399. */
  1400. if (bios->subdev.device->card_type >= NV_50 &&
  1401. port == 0x03c3 && data == 0x01) {
  1402. init_mask(init, 0x614100, 0xf0800000, 0x00800000);
  1403. init_mask(init, 0x00e18c, 0x00020000, 0x00020000);
  1404. init_mask(init, 0x614900, 0xf0800000, 0x00800000);
  1405. init_mask(init, 0x000200, 0x40000000, 0x00000000);
  1406. mdelay(10);
  1407. init_mask(init, 0x00e18c, 0x00020000, 0x00000000);
  1408. init_mask(init, 0x000200, 0x40000000, 0x40000000);
  1409. init_wr32(init, 0x614100, 0x00800018);
  1410. init_wr32(init, 0x614900, 0x00800018);
  1411. mdelay(10);
  1412. init_wr32(init, 0x614100, 0x10000018);
  1413. init_wr32(init, 0x614900, 0x10000018);
  1414. }
  1415. value = init_rdport(init, port) & mask;
  1416. init_wrport(init, port, data | value);
  1417. }
  1418. /**
  1419. * INIT_SUB - opcode 0x6b
  1420. *
  1421. */
  1422. static void
  1423. init_sub(struct nvbios_init *init)
  1424. {
  1425. struct nvkm_bios *bios = init->bios;
  1426. u8 index = nvbios_rd08(bios, init->offset + 1);
  1427. u16 addr, save;
  1428. trace("SUB\t0x%02x\n", index);
  1429. addr = init_script(bios, index);
  1430. if (addr && init_exec(init)) {
  1431. save = init->offset;
  1432. init->offset = addr;
  1433. if (nvbios_exec(init)) {
  1434. error("error parsing sub-table\n");
  1435. return;
  1436. }
  1437. init->offset = save;
  1438. }
  1439. init->offset += 2;
  1440. }
  1441. /**
  1442. * INIT_RAM_CONDITION - opcode 0x6d
  1443. *
  1444. */
  1445. static void
  1446. init_ram_condition(struct nvbios_init *init)
  1447. {
  1448. struct nvkm_bios *bios = init->bios;
  1449. u8 mask = nvbios_rd08(bios, init->offset + 1);
  1450. u8 value = nvbios_rd08(bios, init->offset + 2);
  1451. trace("RAM_CONDITION\t"
  1452. "(R[0x100000] & 0x%02x) == 0x%02x\n", mask, value);
  1453. init->offset += 3;
  1454. if ((init_rd32(init, 0x100000) & mask) != value)
  1455. init_exec_set(init, false);
  1456. }
  1457. /**
  1458. * INIT_NV_REG - opcode 0x6e
  1459. *
  1460. */
  1461. static void
  1462. init_nv_reg(struct nvbios_init *init)
  1463. {
  1464. struct nvkm_bios *bios = init->bios;
  1465. u32 reg = nvbios_rd32(bios, init->offset + 1);
  1466. u32 mask = nvbios_rd32(bios, init->offset + 5);
  1467. u32 data = nvbios_rd32(bios, init->offset + 9);
  1468. trace("NV_REG\tR[0x%06x] &= 0x%08x |= 0x%08x\n", reg, mask, data);
  1469. init->offset += 13;
  1470. init_mask(init, reg, ~mask, data);
  1471. }
  1472. /**
  1473. * INIT_MACRO - opcode 0x6f
  1474. *
  1475. */
  1476. static void
  1477. init_macro(struct nvbios_init *init)
  1478. {
  1479. struct nvkm_bios *bios = init->bios;
  1480. u8 macro = nvbios_rd08(bios, init->offset + 1);
  1481. u16 table;
  1482. trace("MACRO\t0x%02x\n", macro);
  1483. table = init_macro_table(init);
  1484. if (table) {
  1485. u32 addr = nvbios_rd32(bios, table + (macro * 8) + 0);
  1486. u32 data = nvbios_rd32(bios, table + (macro * 8) + 4);
  1487. trace("\t\tR[0x%06x] = 0x%08x\n", addr, data);
  1488. init_wr32(init, addr, data);
  1489. }
  1490. init->offset += 2;
  1491. }
  1492. /**
  1493. * INIT_RESUME - opcode 0x72
  1494. *
  1495. */
  1496. static void
  1497. init_resume(struct nvbios_init *init)
  1498. {
  1499. trace("RESUME\n");
  1500. init->offset += 1;
  1501. init_exec_set(init, true);
  1502. }
  1503. /**
  1504. * INIT_STRAP_CONDITION - opcode 0x73
  1505. *
  1506. */
  1507. static void
  1508. init_strap_condition(struct nvbios_init *init)
  1509. {
  1510. struct nvkm_bios *bios = init->bios;
  1511. u32 mask = nvbios_rd32(bios, init->offset + 1);
  1512. u32 value = nvbios_rd32(bios, init->offset + 5);
  1513. trace("STRAP_CONDITION\t(R[0x101000] & 0x%08x) == 0x%08x\n", mask, value);
  1514. init->offset += 9;
  1515. if ((init_rd32(init, 0x101000) & mask) != value)
  1516. init_exec_set(init, false);
  1517. }
  1518. /**
  1519. * INIT_TIME - opcode 0x74
  1520. *
  1521. */
  1522. static void
  1523. init_time(struct nvbios_init *init)
  1524. {
  1525. struct nvkm_bios *bios = init->bios;
  1526. u16 usec = nvbios_rd16(bios, init->offset + 1);
  1527. trace("TIME\t0x%04x\n", usec);
  1528. init->offset += 3;
  1529. if (init_exec(init)) {
  1530. if (usec < 1000)
  1531. udelay(usec);
  1532. else
  1533. mdelay((usec + 900) / 1000);
  1534. }
  1535. }
  1536. /**
  1537. * INIT_CONDITION - opcode 0x75
  1538. *
  1539. */
  1540. static void
  1541. init_condition(struct nvbios_init *init)
  1542. {
  1543. struct nvkm_bios *bios = init->bios;
  1544. u8 cond = nvbios_rd08(bios, init->offset + 1);
  1545. trace("CONDITION\t0x%02x\n", cond);
  1546. init->offset += 2;
  1547. if (!init_condition_met(init, cond))
  1548. init_exec_set(init, false);
  1549. }
  1550. /**
  1551. * INIT_IO_CONDITION - opcode 0x76
  1552. *
  1553. */
  1554. static void
  1555. init_io_condition(struct nvbios_init *init)
  1556. {
  1557. struct nvkm_bios *bios = init->bios;
  1558. u8 cond = nvbios_rd08(bios, init->offset + 1);
  1559. trace("IO_CONDITION\t0x%02x\n", cond);
  1560. init->offset += 2;
  1561. if (!init_io_condition_met(init, cond))
  1562. init_exec_set(init, false);
  1563. }
  1564. /**
  1565. * INIT_ZM_REG16 - opcode 0x77
  1566. *
  1567. */
  1568. static void
  1569. init_zm_reg16(struct nvbios_init *init)
  1570. {
  1571. struct nvkm_bios *bios = init->bios;
  1572. u32 addr = nvbios_rd32(bios, init->offset + 1);
  1573. u16 data = nvbios_rd16(bios, init->offset + 5);
  1574. trace("ZM_REG\tR[0x%06x] = 0x%04x\n", addr, data);
  1575. init->offset += 7;
  1576. init_wr32(init, addr, data);
  1577. }
  1578. /**
  1579. * INIT_INDEX_IO - opcode 0x78
  1580. *
  1581. */
  1582. static void
  1583. init_index_io(struct nvbios_init *init)
  1584. {
  1585. struct nvkm_bios *bios = init->bios;
  1586. u16 port = nvbios_rd16(bios, init->offset + 1);
  1587. u8 index = nvbios_rd16(bios, init->offset + 3);
  1588. u8 mask = nvbios_rd08(bios, init->offset + 4);
  1589. u8 data = nvbios_rd08(bios, init->offset + 5);
  1590. u8 value;
  1591. trace("INDEX_IO\tI[0x%04x][0x%02x] &= 0x%02x |= 0x%02x\n",
  1592. port, index, mask, data);
  1593. init->offset += 6;
  1594. value = init_rdvgai(init, port, index) & mask;
  1595. init_wrvgai(init, port, index, data | value);
  1596. }
  1597. /**
  1598. * INIT_PLL - opcode 0x79
  1599. *
  1600. */
  1601. static void
  1602. init_pll(struct nvbios_init *init)
  1603. {
  1604. struct nvkm_bios *bios = init->bios;
  1605. u32 reg = nvbios_rd32(bios, init->offset + 1);
  1606. u32 freq = nvbios_rd16(bios, init->offset + 5) * 10;
  1607. trace("PLL\tR[0x%06x] =PLL= %dkHz\n", reg, freq);
  1608. init->offset += 7;
  1609. init_prog_pll(init, reg, freq);
  1610. }
  1611. /**
  1612. * INIT_ZM_REG - opcode 0x7a
  1613. *
  1614. */
  1615. static void
  1616. init_zm_reg(struct nvbios_init *init)
  1617. {
  1618. struct nvkm_bios *bios = init->bios;
  1619. u32 addr = nvbios_rd32(bios, init->offset + 1);
  1620. u32 data = nvbios_rd32(bios, init->offset + 5);
  1621. trace("ZM_REG\tR[0x%06x] = 0x%08x\n", addr, data);
  1622. init->offset += 9;
  1623. if (addr == 0x000200)
  1624. data |= 0x00000001;
  1625. init_wr32(init, addr, data);
  1626. }
  1627. /**
  1628. * INIT_RAM_RESTRICT_PLL - opcde 0x87
  1629. *
  1630. */
  1631. static void
  1632. init_ram_restrict_pll(struct nvbios_init *init)
  1633. {
  1634. struct nvkm_bios *bios = init->bios;
  1635. u8 type = nvbios_rd08(bios, init->offset + 1);
  1636. u8 count = init_ram_restrict_group_count(init);
  1637. u8 strap = init_ram_restrict(init);
  1638. u8 cconf;
  1639. trace("RAM_RESTRICT_PLL\t0x%02x\n", type);
  1640. init->offset += 2;
  1641. for (cconf = 0; cconf < count; cconf++) {
  1642. u32 freq = nvbios_rd32(bios, init->offset);
  1643. if (cconf == strap) {
  1644. trace("%dkHz *\n", freq);
  1645. init_prog_pll(init, type, freq);
  1646. } else {
  1647. trace("%dkHz\n", freq);
  1648. }
  1649. init->offset += 4;
  1650. }
  1651. }
  1652. /**
  1653. * INIT_GPIO - opcode 0x8e
  1654. *
  1655. */
  1656. static void
  1657. init_gpio(struct nvbios_init *init)
  1658. {
  1659. struct nvkm_gpio *gpio = init->bios->subdev.device->gpio;
  1660. trace("GPIO\n");
  1661. init->offset += 1;
  1662. if (init_exec(init))
  1663. nvkm_gpio_reset(gpio, DCB_GPIO_UNUSED);
  1664. }
  1665. /**
  1666. * INIT_RAM_RESTRICT_ZM_GROUP - opcode 0x8f
  1667. *
  1668. */
  1669. static void
  1670. init_ram_restrict_zm_reg_group(struct nvbios_init *init)
  1671. {
  1672. struct nvkm_bios *bios = init->bios;
  1673. u32 addr = nvbios_rd32(bios, init->offset + 1);
  1674. u8 incr = nvbios_rd08(bios, init->offset + 5);
  1675. u8 num = nvbios_rd08(bios, init->offset + 6);
  1676. u8 count = init_ram_restrict_group_count(init);
  1677. u8 index = init_ram_restrict(init);
  1678. u8 i, j;
  1679. trace("RAM_RESTRICT_ZM_REG_GROUP\t"
  1680. "R[0x%08x] 0x%02x 0x%02x\n", addr, incr, num);
  1681. init->offset += 7;
  1682. for (i = 0; i < num; i++) {
  1683. trace("\tR[0x%06x] = {\n", addr);
  1684. for (j = 0; j < count; j++) {
  1685. u32 data = nvbios_rd32(bios, init->offset);
  1686. if (j == index) {
  1687. trace("\t\t0x%08x *\n", data);
  1688. init_wr32(init, addr, data);
  1689. } else {
  1690. trace("\t\t0x%08x\n", data);
  1691. }
  1692. init->offset += 4;
  1693. }
  1694. trace("\t}\n");
  1695. addr += incr;
  1696. }
  1697. }
  1698. /**
  1699. * INIT_COPY_ZM_REG - opcode 0x90
  1700. *
  1701. */
  1702. static void
  1703. init_copy_zm_reg(struct nvbios_init *init)
  1704. {
  1705. struct nvkm_bios *bios = init->bios;
  1706. u32 sreg = nvbios_rd32(bios, init->offset + 1);
  1707. u32 dreg = nvbios_rd32(bios, init->offset + 5);
  1708. trace("COPY_ZM_REG\tR[0x%06x] = R[0x%06x]\n", dreg, sreg);
  1709. init->offset += 9;
  1710. init_wr32(init, dreg, init_rd32(init, sreg));
  1711. }
  1712. /**
  1713. * INIT_ZM_REG_GROUP - opcode 0x91
  1714. *
  1715. */
  1716. static void
  1717. init_zm_reg_group(struct nvbios_init *init)
  1718. {
  1719. struct nvkm_bios *bios = init->bios;
  1720. u32 addr = nvbios_rd32(bios, init->offset + 1);
  1721. u8 count = nvbios_rd08(bios, init->offset + 5);
  1722. trace("ZM_REG_GROUP\tR[0x%06x] =\n", addr);
  1723. init->offset += 6;
  1724. while (count--) {
  1725. u32 data = nvbios_rd32(bios, init->offset);
  1726. trace("\t0x%08x\n", data);
  1727. init_wr32(init, addr, data);
  1728. init->offset += 4;
  1729. }
  1730. }
  1731. /**
  1732. * INIT_XLAT - opcode 0x96
  1733. *
  1734. */
  1735. static void
  1736. init_xlat(struct nvbios_init *init)
  1737. {
  1738. struct nvkm_bios *bios = init->bios;
  1739. u32 saddr = nvbios_rd32(bios, init->offset + 1);
  1740. u8 sshift = nvbios_rd08(bios, init->offset + 5);
  1741. u8 smask = nvbios_rd08(bios, init->offset + 6);
  1742. u8 index = nvbios_rd08(bios, init->offset + 7);
  1743. u32 daddr = nvbios_rd32(bios, init->offset + 8);
  1744. u32 dmask = nvbios_rd32(bios, init->offset + 12);
  1745. u8 shift = nvbios_rd08(bios, init->offset + 16);
  1746. u32 data;
  1747. trace("INIT_XLAT\tR[0x%06x] &= 0x%08x |= "
  1748. "(X%02x((R[0x%06x] %s 0x%02x) & 0x%02x) << 0x%02x)\n",
  1749. daddr, dmask, index, saddr, (sshift & 0x80) ? "<<" : ">>",
  1750. (sshift & 0x80) ? (0x100 - sshift) : sshift, smask, shift);
  1751. init->offset += 17;
  1752. data = init_shift(init_rd32(init, saddr), sshift) & smask;
  1753. data = init_xlat_(init, index, data) << shift;
  1754. init_mask(init, daddr, ~dmask, data);
  1755. }
  1756. /**
  1757. * INIT_ZM_MASK_ADD - opcode 0x97
  1758. *
  1759. */
  1760. static void
  1761. init_zm_mask_add(struct nvbios_init *init)
  1762. {
  1763. struct nvkm_bios *bios = init->bios;
  1764. u32 addr = nvbios_rd32(bios, init->offset + 1);
  1765. u32 mask = nvbios_rd32(bios, init->offset + 5);
  1766. u32 add = nvbios_rd32(bios, init->offset + 9);
  1767. u32 data;
  1768. trace("ZM_MASK_ADD\tR[0x%06x] &= 0x%08x += 0x%08x\n", addr, mask, add);
  1769. init->offset += 13;
  1770. data = init_rd32(init, addr);
  1771. data = (data & mask) | ((data + add) & ~mask);
  1772. init_wr32(init, addr, data);
  1773. }
  1774. /**
  1775. * INIT_AUXCH - opcode 0x98
  1776. *
  1777. */
  1778. static void
  1779. init_auxch(struct nvbios_init *init)
  1780. {
  1781. struct nvkm_bios *bios = init->bios;
  1782. u32 addr = nvbios_rd32(bios, init->offset + 1);
  1783. u8 count = nvbios_rd08(bios, init->offset + 5);
  1784. trace("AUXCH\tAUX[0x%08x] 0x%02x\n", addr, count);
  1785. init->offset += 6;
  1786. while (count--) {
  1787. u8 mask = nvbios_rd08(bios, init->offset + 0);
  1788. u8 data = nvbios_rd08(bios, init->offset + 1);
  1789. trace("\tAUX[0x%08x] &= 0x%02x |= 0x%02x\n", addr, mask, data);
  1790. mask = init_rdauxr(init, addr) & mask;
  1791. init_wrauxr(init, addr, mask | data);
  1792. init->offset += 2;
  1793. }
  1794. }
  1795. /**
  1796. * INIT_AUXCH - opcode 0x99
  1797. *
  1798. */
  1799. static void
  1800. init_zm_auxch(struct nvbios_init *init)
  1801. {
  1802. struct nvkm_bios *bios = init->bios;
  1803. u32 addr = nvbios_rd32(bios, init->offset + 1);
  1804. u8 count = nvbios_rd08(bios, init->offset + 5);
  1805. trace("ZM_AUXCH\tAUX[0x%08x] 0x%02x\n", addr, count);
  1806. init->offset += 6;
  1807. while (count--) {
  1808. u8 data = nvbios_rd08(bios, init->offset + 0);
  1809. trace("\tAUX[0x%08x] = 0x%02x\n", addr, data);
  1810. init_wrauxr(init, addr, data);
  1811. init->offset += 1;
  1812. }
  1813. }
  1814. /**
  1815. * INIT_I2C_LONG_IF - opcode 0x9a
  1816. *
  1817. */
  1818. static void
  1819. init_i2c_long_if(struct nvbios_init *init)
  1820. {
  1821. struct nvkm_bios *bios = init->bios;
  1822. u8 index = nvbios_rd08(bios, init->offset + 1);
  1823. u8 addr = nvbios_rd08(bios, init->offset + 2) >> 1;
  1824. u8 reglo = nvbios_rd08(bios, init->offset + 3);
  1825. u8 reghi = nvbios_rd08(bios, init->offset + 4);
  1826. u8 mask = nvbios_rd08(bios, init->offset + 5);
  1827. u8 data = nvbios_rd08(bios, init->offset + 6);
  1828. struct i2c_adapter *adap;
  1829. trace("I2C_LONG_IF\t"
  1830. "I2C[0x%02x][0x%02x][0x%02x%02x] & 0x%02x == 0x%02x\n",
  1831. index, addr, reglo, reghi, mask, data);
  1832. init->offset += 7;
  1833. adap = init_i2c(init, index);
  1834. if (adap) {
  1835. u8 i[2] = { reghi, reglo };
  1836. u8 o[1] = {};
  1837. struct i2c_msg msg[] = {
  1838. { .addr = addr, .flags = 0, .len = 2, .buf = i },
  1839. { .addr = addr, .flags = I2C_M_RD, .len = 1, .buf = o }
  1840. };
  1841. int ret;
  1842. ret = i2c_transfer(adap, msg, 2);
  1843. if (ret == 2 && ((o[0] & mask) == data))
  1844. return;
  1845. }
  1846. init_exec_set(init, false);
  1847. }
  1848. /**
  1849. * INIT_GPIO_NE - opcode 0xa9
  1850. *
  1851. */
  1852. static void
  1853. init_gpio_ne(struct nvbios_init *init)
  1854. {
  1855. struct nvkm_bios *bios = init->bios;
  1856. struct nvkm_gpio *gpio = bios->subdev.device->gpio;
  1857. struct dcb_gpio_func func;
  1858. u8 count = nvbios_rd08(bios, init->offset + 1);
  1859. u8 idx = 0, ver, len;
  1860. u16 data, i;
  1861. trace("GPIO_NE\t");
  1862. init->offset += 2;
  1863. for (i = init->offset; i < init->offset + count; i++)
  1864. cont("0x%02x ", nvbios_rd08(bios, i));
  1865. cont("\n");
  1866. while ((data = dcb_gpio_parse(bios, 0, idx++, &ver, &len, &func))) {
  1867. if (func.func != DCB_GPIO_UNUSED) {
  1868. for (i = init->offset; i < init->offset + count; i++) {
  1869. if (func.func == nvbios_rd08(bios, i))
  1870. break;
  1871. }
  1872. trace("\tFUNC[0x%02x]", func.func);
  1873. if (i == (init->offset + count)) {
  1874. cont(" *");
  1875. if (init_exec(init))
  1876. nvkm_gpio_reset(gpio, func.func);
  1877. }
  1878. cont("\n");
  1879. }
  1880. }
  1881. init->offset += count;
  1882. }
  1883. static struct nvbios_init_opcode {
  1884. void (*exec)(struct nvbios_init *);
  1885. } init_opcode[] = {
  1886. [0x32] = { init_io_restrict_prog },
  1887. [0x33] = { init_repeat },
  1888. [0x34] = { init_io_restrict_pll },
  1889. [0x36] = { init_end_repeat },
  1890. [0x37] = { init_copy },
  1891. [0x38] = { init_not },
  1892. [0x39] = { init_io_flag_condition },
  1893. [0x3a] = { init_generic_condition },
  1894. [0x3b] = { init_io_mask_or },
  1895. [0x3c] = { init_io_or },
  1896. [0x47] = { init_andn_reg },
  1897. [0x48] = { init_or_reg },
  1898. [0x49] = { init_idx_addr_latched },
  1899. [0x4a] = { init_io_restrict_pll2 },
  1900. [0x4b] = { init_pll2 },
  1901. [0x4c] = { init_i2c_byte },
  1902. [0x4d] = { init_zm_i2c_byte },
  1903. [0x4e] = { init_zm_i2c },
  1904. [0x4f] = { init_tmds },
  1905. [0x50] = { init_zm_tmds_group },
  1906. [0x51] = { init_cr_idx_adr_latch },
  1907. [0x52] = { init_cr },
  1908. [0x53] = { init_zm_cr },
  1909. [0x54] = { init_zm_cr_group },
  1910. [0x56] = { init_condition_time },
  1911. [0x57] = { init_ltime },
  1912. [0x58] = { init_zm_reg_sequence },
  1913. [0x59] = { init_pll_indirect },
  1914. [0x5a] = { init_zm_reg_indirect },
  1915. [0x5b] = { init_sub_direct },
  1916. [0x5c] = { init_jump },
  1917. [0x5e] = { init_i2c_if },
  1918. [0x5f] = { init_copy_nv_reg },
  1919. [0x62] = { init_zm_index_io },
  1920. [0x63] = { init_compute_mem },
  1921. [0x65] = { init_reset },
  1922. [0x66] = { init_configure_mem },
  1923. [0x67] = { init_configure_clk },
  1924. [0x68] = { init_configure_preinit },
  1925. [0x69] = { init_io },
  1926. [0x6b] = { init_sub },
  1927. [0x6d] = { init_ram_condition },
  1928. [0x6e] = { init_nv_reg },
  1929. [0x6f] = { init_macro },
  1930. [0x71] = { init_done },
  1931. [0x72] = { init_resume },
  1932. [0x73] = { init_strap_condition },
  1933. [0x74] = { init_time },
  1934. [0x75] = { init_condition },
  1935. [0x76] = { init_io_condition },
  1936. [0x77] = { init_zm_reg16 },
  1937. [0x78] = { init_index_io },
  1938. [0x79] = { init_pll },
  1939. [0x7a] = { init_zm_reg },
  1940. [0x87] = { init_ram_restrict_pll },
  1941. [0x8c] = { init_reserved },
  1942. [0x8d] = { init_reserved },
  1943. [0x8e] = { init_gpio },
  1944. [0x8f] = { init_ram_restrict_zm_reg_group },
  1945. [0x90] = { init_copy_zm_reg },
  1946. [0x91] = { init_zm_reg_group },
  1947. [0x92] = { init_reserved },
  1948. [0x96] = { init_xlat },
  1949. [0x97] = { init_zm_mask_add },
  1950. [0x98] = { init_auxch },
  1951. [0x99] = { init_zm_auxch },
  1952. [0x9a] = { init_i2c_long_if },
  1953. [0xa9] = { init_gpio_ne },
  1954. [0xaa] = { init_reserved },
  1955. };
  1956. #define init_opcode_nr (sizeof(init_opcode) / sizeof(init_opcode[0]))
  1957. int
  1958. nvbios_exec(struct nvbios_init *init)
  1959. {
  1960. init->nested++;
  1961. while (init->offset) {
  1962. u8 opcode = nvbios_rd08(init->bios, init->offset);
  1963. if (opcode >= init_opcode_nr || !init_opcode[opcode].exec) {
  1964. error("unknown opcode 0x%02x\n", opcode);
  1965. return -EINVAL;
  1966. }
  1967. init_opcode[opcode].exec(init);
  1968. }
  1969. init->nested--;
  1970. return 0;
  1971. }
  1972. int
  1973. nvbios_init(struct nvkm_subdev *subdev, bool execute)
  1974. {
  1975. struct nvkm_bios *bios = subdev->device->bios;
  1976. int ret = 0;
  1977. int i = -1;
  1978. u16 data;
  1979. if (execute)
  1980. nvkm_debug(subdev, "running init tables\n");
  1981. while (!ret && (data = (init_script(bios, ++i)))) {
  1982. struct nvbios_init init = {
  1983. .subdev = subdev,
  1984. .bios = bios,
  1985. .offset = data,
  1986. .outp = NULL,
  1987. .crtc = -1,
  1988. .execute = execute ? 1 : 0,
  1989. };
  1990. ret = nvbios_exec(&init);
  1991. }
  1992. /* the vbios parser will run this right after the normal init
  1993. * tables, whereas the binary driver appears to run it later.
  1994. */
  1995. if (!ret && (data = init_unknown_script(bios))) {
  1996. struct nvbios_init init = {
  1997. .subdev = subdev,
  1998. .bios = bios,
  1999. .offset = data,
  2000. .outp = NULL,
  2001. .crtc = -1,
  2002. .execute = execute ? 1 : 0,
  2003. };
  2004. ret = nvbios_exec(&init);
  2005. }
  2006. return ret;
  2007. }