sisusb_init.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
  2. /*
  3. * sisusb - usb kernel driver for SiS315(E) based USB2VGA dongles
  4. *
  5. * Display mode initializing code
  6. *
  7. * Copyright (C) 2001-2005 by Thomas Winischhofer, Vienna, Austria
  8. *
  9. * If distributed as part of the Linux kernel, this code is licensed under the
  10. * terms of the GPL v2.
  11. *
  12. * Otherwise, the following license terms apply:
  13. *
  14. * * Redistribution and use in source and binary forms, with or without
  15. * * modification, are permitted provided that the following conditions
  16. * * are met:
  17. * * 1) Redistributions of source code must retain the above copyright
  18. * * notice, this list of conditions and the following disclaimer.
  19. * * 2) Redistributions in binary form must reproduce the above copyright
  20. * * notice, this list of conditions and the following disclaimer in the
  21. * * documentation and/or other materials provided with the distribution.
  22. * * 3) The name of the author may not be used to endorse or promote products
  23. * * derived from this software without specific prior written permission.
  24. * *
  25. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  26. * * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  27. * * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  28. * * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  30. * * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  34. * * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. * Author: Thomas Winischhofer <thomas@winischhofer.net>
  37. *
  38. */
  39. #include <linux/module.h>
  40. #include <linux/kernel.h>
  41. #include <linux/errno.h>
  42. #include <linux/poll.h>
  43. #include <linux/spinlock.h>
  44. #include "sisusb.h"
  45. #ifdef INCL_SISUSB_CON
  46. #include "sisusb_init.h"
  47. /*********************************************/
  48. /* POINTER INITIALIZATION */
  49. /*********************************************/
  50. static void SiSUSB_InitPtr(struct SiS_Private *SiS_Pr)
  51. {
  52. SiS_Pr->SiS_ModeResInfo = SiSUSB_ModeResInfo;
  53. SiS_Pr->SiS_StandTable = SiSUSB_StandTable;
  54. SiS_Pr->SiS_SModeIDTable = SiSUSB_SModeIDTable;
  55. SiS_Pr->SiS_EModeIDTable = SiSUSB_EModeIDTable;
  56. SiS_Pr->SiS_RefIndex = SiSUSB_RefIndex;
  57. SiS_Pr->SiS_CRT1Table = SiSUSB_CRT1Table;
  58. SiS_Pr->SiS_VCLKData = SiSUSB_VCLKData;
  59. }
  60. /*********************************************/
  61. /* HELPER: SetReg, GetReg */
  62. /*********************************************/
  63. static void
  64. SiS_SetReg(struct SiS_Private *SiS_Pr, unsigned long port,
  65. unsigned short index, unsigned short data)
  66. {
  67. sisusb_setidxreg(SiS_Pr->sisusb, port, index, data);
  68. }
  69. static void
  70. SiS_SetRegByte(struct SiS_Private *SiS_Pr, unsigned long port,
  71. unsigned short data)
  72. {
  73. sisusb_setreg(SiS_Pr->sisusb, port, data);
  74. }
  75. static unsigned char
  76. SiS_GetReg(struct SiS_Private *SiS_Pr, unsigned long port, unsigned short index)
  77. {
  78. u8 data;
  79. sisusb_getidxreg(SiS_Pr->sisusb, port, index, &data);
  80. return data;
  81. }
  82. static unsigned char
  83. SiS_GetRegByte(struct SiS_Private *SiS_Pr, unsigned long port)
  84. {
  85. u8 data;
  86. sisusb_getreg(SiS_Pr->sisusb, port, &data);
  87. return data;
  88. }
  89. static void
  90. SiS_SetRegANDOR(struct SiS_Private *SiS_Pr, unsigned long port,
  91. unsigned short index, unsigned short DataAND,
  92. unsigned short DataOR)
  93. {
  94. sisusb_setidxregandor(SiS_Pr->sisusb, port, index, DataAND, DataOR);
  95. }
  96. static void
  97. SiS_SetRegAND(struct SiS_Private *SiS_Pr, unsigned long port,
  98. unsigned short index, unsigned short DataAND)
  99. {
  100. sisusb_setidxregand(SiS_Pr->sisusb, port, index, DataAND);
  101. }
  102. static void
  103. SiS_SetRegOR(struct SiS_Private *SiS_Pr, unsigned long port,
  104. unsigned short index, unsigned short DataOR)
  105. {
  106. sisusb_setidxregor(SiS_Pr->sisusb, port, index, DataOR);
  107. }
  108. /*********************************************/
  109. /* HELPER: DisplayOn, DisplayOff */
  110. /*********************************************/
  111. static void SiS_DisplayOn(struct SiS_Private *SiS_Pr)
  112. {
  113. SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3c4, 0x01, 0xDF);
  114. }
  115. /*********************************************/
  116. /* HELPER: Init Port Addresses */
  117. /*********************************************/
  118. static void SiSUSBRegInit(struct SiS_Private *SiS_Pr, unsigned long BaseAddr)
  119. {
  120. SiS_Pr->SiS_P3c4 = BaseAddr + 0x14;
  121. SiS_Pr->SiS_P3d4 = BaseAddr + 0x24;
  122. SiS_Pr->SiS_P3c0 = BaseAddr + 0x10;
  123. SiS_Pr->SiS_P3ce = BaseAddr + 0x1e;
  124. SiS_Pr->SiS_P3c2 = BaseAddr + 0x12;
  125. SiS_Pr->SiS_P3ca = BaseAddr + 0x1a;
  126. SiS_Pr->SiS_P3c6 = BaseAddr + 0x16;
  127. SiS_Pr->SiS_P3c7 = BaseAddr + 0x17;
  128. SiS_Pr->SiS_P3c8 = BaseAddr + 0x18;
  129. SiS_Pr->SiS_P3c9 = BaseAddr + 0x19;
  130. SiS_Pr->SiS_P3cb = BaseAddr + 0x1b;
  131. SiS_Pr->SiS_P3cc = BaseAddr + 0x1c;
  132. SiS_Pr->SiS_P3cd = BaseAddr + 0x1d;
  133. SiS_Pr->SiS_P3da = BaseAddr + 0x2a;
  134. SiS_Pr->SiS_Part1Port = BaseAddr + SIS_CRT2_PORT_04;
  135. }
  136. /*********************************************/
  137. /* HELPER: GetSysFlags */
  138. /*********************************************/
  139. static void SiS_GetSysFlags(struct SiS_Private *SiS_Pr)
  140. {
  141. SiS_Pr->SiS_MyCR63 = 0x63;
  142. }
  143. /*********************************************/
  144. /* HELPER: Init PCI & Engines */
  145. /*********************************************/
  146. static void SiSInitPCIetc(struct SiS_Private *SiS_Pr)
  147. {
  148. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x20, 0xa1);
  149. /* - Enable 2D (0x40)
  150. * - Enable 3D (0x02)
  151. * - Enable 3D vertex command fetch (0x10)
  152. * - Enable 3D command parser (0x08)
  153. * - Enable 3D G/L transformation engine (0x80)
  154. */
  155. SiS_SetRegOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x1E, 0xDA);
  156. }
  157. /*********************************************/
  158. /* HELPER: SET SEGMENT REGISTERS */
  159. /*********************************************/
  160. static void SiS_SetSegRegLower(struct SiS_Private *SiS_Pr, unsigned short value)
  161. {
  162. unsigned short temp;
  163. value &= 0x00ff;
  164. temp = SiS_GetRegByte(SiS_Pr, SiS_Pr->SiS_P3cb) & 0xf0;
  165. temp |= (value >> 4);
  166. SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3cb, temp);
  167. temp = SiS_GetRegByte(SiS_Pr, SiS_Pr->SiS_P3cd) & 0xf0;
  168. temp |= (value & 0x0f);
  169. SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3cd, temp);
  170. }
  171. static void SiS_SetSegRegUpper(struct SiS_Private *SiS_Pr, unsigned short value)
  172. {
  173. unsigned short temp;
  174. value &= 0x00ff;
  175. temp = SiS_GetRegByte(SiS_Pr, SiS_Pr->SiS_P3cb) & 0x0f;
  176. temp |= (value & 0xf0);
  177. SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3cb, temp);
  178. temp = SiS_GetRegByte(SiS_Pr, SiS_Pr->SiS_P3cd) & 0x0f;
  179. temp |= (value << 4);
  180. SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3cd, temp);
  181. }
  182. static void SiS_SetSegmentReg(struct SiS_Private *SiS_Pr, unsigned short value)
  183. {
  184. SiS_SetSegRegLower(SiS_Pr, value);
  185. SiS_SetSegRegUpper(SiS_Pr, value);
  186. }
  187. static void SiS_ResetSegmentReg(struct SiS_Private *SiS_Pr)
  188. {
  189. SiS_SetSegmentReg(SiS_Pr, 0);
  190. }
  191. static void
  192. SiS_SetSegmentRegOver(struct SiS_Private *SiS_Pr, unsigned short value)
  193. {
  194. unsigned short temp = value >> 8;
  195. temp &= 0x07;
  196. temp |= (temp << 4);
  197. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x1d, temp);
  198. SiS_SetSegmentReg(SiS_Pr, value);
  199. }
  200. static void SiS_ResetSegmentRegOver(struct SiS_Private *SiS_Pr)
  201. {
  202. SiS_SetSegmentRegOver(SiS_Pr, 0);
  203. }
  204. static void SiS_ResetSegmentRegisters(struct SiS_Private *SiS_Pr)
  205. {
  206. SiS_ResetSegmentReg(SiS_Pr);
  207. SiS_ResetSegmentRegOver(SiS_Pr);
  208. }
  209. /*********************************************/
  210. /* HELPER: SearchModeID */
  211. /*********************************************/
  212. static int
  213. SiS_SearchModeID(struct SiS_Private *SiS_Pr, unsigned short *ModeNo,
  214. unsigned short *ModeIdIndex)
  215. {
  216. if ((*ModeNo) <= 0x13) {
  217. if ((*ModeNo) != 0x03)
  218. return 0;
  219. (*ModeIdIndex) = 0;
  220. } else {
  221. for (*ModeIdIndex = 0;; (*ModeIdIndex)++) {
  222. if (SiS_Pr->SiS_EModeIDTable[*ModeIdIndex].Ext_ModeID ==
  223. (*ModeNo))
  224. break;
  225. if (SiS_Pr->SiS_EModeIDTable[*ModeIdIndex].Ext_ModeID ==
  226. 0xFF)
  227. return 0;
  228. }
  229. }
  230. return 1;
  231. }
  232. /*********************************************/
  233. /* HELPER: ENABLE CRT1 */
  234. /*********************************************/
  235. static void SiS_HandleCRT1(struct SiS_Private *SiS_Pr)
  236. {
  237. /* Enable CRT1 gating */
  238. SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3d4, SiS_Pr->SiS_MyCR63, 0xbf);
  239. }
  240. /*********************************************/
  241. /* HELPER: GetColorDepth */
  242. /*********************************************/
  243. static unsigned short
  244. SiS_GetColorDepth(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
  245. unsigned short ModeIdIndex)
  246. {
  247. static const unsigned short ColorDepth[6] = { 1, 2, 4, 4, 6, 8 };
  248. unsigned short modeflag;
  249. short index;
  250. if (ModeNo <= 0x13) {
  251. modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
  252. } else {
  253. modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
  254. }
  255. index = (modeflag & ModeTypeMask) - ModeEGA;
  256. if (index < 0)
  257. index = 0;
  258. return ColorDepth[index];
  259. }
  260. /*********************************************/
  261. /* HELPER: GetOffset */
  262. /*********************************************/
  263. static unsigned short
  264. SiS_GetOffset(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
  265. unsigned short ModeIdIndex, unsigned short rrti)
  266. {
  267. unsigned short xres, temp, colordepth, infoflag;
  268. infoflag = SiS_Pr->SiS_RefIndex[rrti].Ext_InfoFlag;
  269. xres = SiS_Pr->SiS_RefIndex[rrti].XRes;
  270. colordepth = SiS_GetColorDepth(SiS_Pr, ModeNo, ModeIdIndex);
  271. temp = xres / 16;
  272. if (infoflag & InterlaceMode)
  273. temp <<= 1;
  274. temp *= colordepth;
  275. if (xres % 16)
  276. temp += (colordepth >> 1);
  277. return temp;
  278. }
  279. /*********************************************/
  280. /* SEQ */
  281. /*********************************************/
  282. static void
  283. SiS_SetSeqRegs(struct SiS_Private *SiS_Pr, unsigned short StandTableIndex)
  284. {
  285. unsigned char SRdata;
  286. int i;
  287. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x00, 0x03);
  288. SRdata = SiS_Pr->SiS_StandTable[StandTableIndex].SR[0] | 0x20;
  289. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x01, SRdata);
  290. for (i = 2; i <= 4; i++) {
  291. SRdata = SiS_Pr->SiS_StandTable[StandTableIndex].SR[i - 1];
  292. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, i, SRdata);
  293. }
  294. }
  295. /*********************************************/
  296. /* MISC */
  297. /*********************************************/
  298. static void
  299. SiS_SetMiscRegs(struct SiS_Private *SiS_Pr, unsigned short StandTableIndex)
  300. {
  301. unsigned char Miscdata = SiS_Pr->SiS_StandTable[StandTableIndex].MISC;
  302. SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3c2, Miscdata);
  303. }
  304. /*********************************************/
  305. /* CRTC */
  306. /*********************************************/
  307. static void
  308. SiS_SetCRTCRegs(struct SiS_Private *SiS_Pr, unsigned short StandTableIndex)
  309. {
  310. unsigned char CRTCdata;
  311. unsigned short i;
  312. SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3d4, 0x11, 0x7f);
  313. for (i = 0; i <= 0x18; i++) {
  314. CRTCdata = SiS_Pr->SiS_StandTable[StandTableIndex].CRTC[i];
  315. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, i, CRTCdata);
  316. }
  317. }
  318. /*********************************************/
  319. /* ATT */
  320. /*********************************************/
  321. static void
  322. SiS_SetATTRegs(struct SiS_Private *SiS_Pr, unsigned short StandTableIndex)
  323. {
  324. unsigned char ARdata;
  325. unsigned short i;
  326. for (i = 0; i <= 0x13; i++) {
  327. ARdata = SiS_Pr->SiS_StandTable[StandTableIndex].ATTR[i];
  328. SiS_GetRegByte(SiS_Pr, SiS_Pr->SiS_P3da);
  329. SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3c0, i);
  330. SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3c0, ARdata);
  331. }
  332. SiS_GetRegByte(SiS_Pr, SiS_Pr->SiS_P3da);
  333. SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3c0, 0x14);
  334. SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3c0, 0x00);
  335. SiS_GetRegByte(SiS_Pr, SiS_Pr->SiS_P3da);
  336. SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3c0, 0x20);
  337. SiS_GetRegByte(SiS_Pr, SiS_Pr->SiS_P3da);
  338. }
  339. /*********************************************/
  340. /* GRC */
  341. /*********************************************/
  342. static void
  343. SiS_SetGRCRegs(struct SiS_Private *SiS_Pr, unsigned short StandTableIndex)
  344. {
  345. unsigned char GRdata;
  346. unsigned short i;
  347. for (i = 0; i <= 0x08; i++) {
  348. GRdata = SiS_Pr->SiS_StandTable[StandTableIndex].GRC[i];
  349. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3ce, i, GRdata);
  350. }
  351. if (SiS_Pr->SiS_ModeType > ModeVGA) {
  352. /* 256 color disable */
  353. SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3ce, 0x05, 0xBF);
  354. }
  355. }
  356. /*********************************************/
  357. /* CLEAR EXTENDED REGISTERS */
  358. /*********************************************/
  359. static void SiS_ClearExt1Regs(struct SiS_Private *SiS_Pr, unsigned short ModeNo)
  360. {
  361. int i;
  362. for (i = 0x0A; i <= 0x0E; i++) {
  363. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, i, 0x00);
  364. }
  365. SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3c4, 0x37, 0xFE);
  366. }
  367. /*********************************************/
  368. /* Get rate index */
  369. /*********************************************/
  370. static unsigned short
  371. SiS_GetRatePtr(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
  372. unsigned short ModeIdIndex)
  373. {
  374. unsigned short rrti, i, index, temp;
  375. if (ModeNo <= 0x13)
  376. return 0xFFFF;
  377. index = SiS_GetReg(SiS_Pr, SiS_Pr->SiS_P3d4, 0x33) & 0x0F;
  378. if (index > 0)
  379. index--;
  380. rrti = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].REFindex;
  381. ModeNo = SiS_Pr->SiS_RefIndex[rrti].ModeID;
  382. i = 0;
  383. do {
  384. if (SiS_Pr->SiS_RefIndex[rrti + i].ModeID != ModeNo)
  385. break;
  386. temp =
  387. SiS_Pr->SiS_RefIndex[rrti + i].Ext_InfoFlag & ModeTypeMask;
  388. if (temp < SiS_Pr->SiS_ModeType)
  389. break;
  390. i++;
  391. index--;
  392. } while (index != 0xFFFF);
  393. i--;
  394. return (rrti + i);
  395. }
  396. /*********************************************/
  397. /* SYNC */
  398. /*********************************************/
  399. static void SiS_SetCRT1Sync(struct SiS_Private *SiS_Pr, unsigned short rrti)
  400. {
  401. unsigned short sync = SiS_Pr->SiS_RefIndex[rrti].Ext_InfoFlag >> 8;
  402. sync &= 0xC0;
  403. sync |= 0x2f;
  404. SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3c2, sync);
  405. }
  406. /*********************************************/
  407. /* CRTC/2 */
  408. /*********************************************/
  409. static void
  410. SiS_SetCRT1CRTC(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
  411. unsigned short ModeIdIndex, unsigned short rrti)
  412. {
  413. unsigned char index;
  414. unsigned short temp, i, j, modeflag;
  415. SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3d4, 0x11, 0x7f);
  416. modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
  417. index = SiS_Pr->SiS_RefIndex[rrti].Ext_CRT1CRTC;
  418. for (i = 0, j = 0; i <= 7; i++, j++) {
  419. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, j,
  420. SiS_Pr->SiS_CRT1Table[index].CR[i]);
  421. }
  422. for (j = 0x10; i <= 10; i++, j++) {
  423. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, j,
  424. SiS_Pr->SiS_CRT1Table[index].CR[i]);
  425. }
  426. for (j = 0x15; i <= 12; i++, j++) {
  427. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, j,
  428. SiS_Pr->SiS_CRT1Table[index].CR[i]);
  429. }
  430. for (j = 0x0A; i <= 15; i++, j++) {
  431. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, j,
  432. SiS_Pr->SiS_CRT1Table[index].CR[i]);
  433. }
  434. temp = SiS_Pr->SiS_CRT1Table[index].CR[16] & 0xE0;
  435. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x0E, temp);
  436. temp = ((SiS_Pr->SiS_CRT1Table[index].CR[16]) & 0x01) << 5;
  437. if (modeflag & DoubleScanMode)
  438. temp |= 0x80;
  439. SiS_SetRegANDOR(SiS_Pr, SiS_Pr->SiS_P3d4, 0x09, 0x5F, temp);
  440. if (SiS_Pr->SiS_ModeType > ModeVGA)
  441. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, 0x14, 0x4F);
  442. }
  443. /*********************************************/
  444. /* OFFSET & PITCH */
  445. /*********************************************/
  446. /* (partly overruled by SetPitch() in XF86) */
  447. /*********************************************/
  448. static void
  449. SiS_SetCRT1Offset(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
  450. unsigned short ModeIdIndex, unsigned short rrti)
  451. {
  452. unsigned short du = SiS_GetOffset(SiS_Pr, ModeNo, ModeIdIndex, rrti);
  453. unsigned short infoflag = SiS_Pr->SiS_RefIndex[rrti].Ext_InfoFlag;
  454. unsigned short temp;
  455. temp = (du >> 8) & 0x0f;
  456. SiS_SetRegANDOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x0E, 0xF0, temp);
  457. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, 0x13, (du & 0xFF));
  458. if (infoflag & InterlaceMode)
  459. du >>= 1;
  460. du <<= 5;
  461. temp = (du >> 8) & 0xff;
  462. if (du & 0xff)
  463. temp++;
  464. temp++;
  465. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x10, temp);
  466. }
  467. /*********************************************/
  468. /* VCLK */
  469. /*********************************************/
  470. static void
  471. SiS_SetCRT1VCLK(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
  472. unsigned short rrti)
  473. {
  474. unsigned short index = SiS_Pr->SiS_RefIndex[rrti].Ext_CRTVCLK;
  475. unsigned short clka = SiS_Pr->SiS_VCLKData[index].SR2B;
  476. unsigned short clkb = SiS_Pr->SiS_VCLKData[index].SR2C;
  477. SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3c4, 0x31, 0xCF);
  478. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x2B, clka);
  479. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x2C, clkb);
  480. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x2D, 0x01);
  481. }
  482. /*********************************************/
  483. /* FIFO */
  484. /*********************************************/
  485. static void
  486. SiS_SetCRT1FIFO_310(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
  487. unsigned short mi)
  488. {
  489. unsigned short modeflag = SiS_Pr->SiS_EModeIDTable[mi].Ext_ModeFlag;
  490. /* disable auto-threshold */
  491. SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3c4, 0x3D, 0xFE);
  492. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x08, 0xAE);
  493. SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3c4, 0x09, 0xF0);
  494. if (ModeNo <= 0x13)
  495. return;
  496. if ((!(modeflag & DoubleScanMode)) || (!(modeflag & HalfDCLK))) {
  497. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x08, 0x34);
  498. SiS_SetRegOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x3D, 0x01);
  499. }
  500. }
  501. /*********************************************/
  502. /* MODE REGISTERS */
  503. /*********************************************/
  504. static void
  505. SiS_SetVCLKState(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
  506. unsigned short rrti)
  507. {
  508. unsigned short data = 0, VCLK = 0, index = 0;
  509. if (ModeNo > 0x13) {
  510. index = SiS_Pr->SiS_RefIndex[rrti].Ext_CRTVCLK;
  511. VCLK = SiS_Pr->SiS_VCLKData[index].CLOCK;
  512. }
  513. if (VCLK >= 166)
  514. data |= 0x0c;
  515. SiS_SetRegANDOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x32, 0xf3, data);
  516. if (VCLK >= 166)
  517. SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3c4, 0x1f, 0xe7);
  518. /* DAC speed */
  519. data = 0x03;
  520. if (VCLK >= 260)
  521. data = 0x00;
  522. else if (VCLK >= 160)
  523. data = 0x01;
  524. else if (VCLK >= 135)
  525. data = 0x02;
  526. SiS_SetRegANDOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x07, 0xF8, data);
  527. }
  528. static void
  529. SiS_SetCRT1ModeRegs(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
  530. unsigned short ModeIdIndex, unsigned short rrti)
  531. {
  532. unsigned short data, infoflag = 0, modeflag;
  533. if (ModeNo <= 0x13)
  534. modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
  535. else {
  536. modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
  537. infoflag = SiS_Pr->SiS_RefIndex[rrti].Ext_InfoFlag;
  538. }
  539. /* Disable DPMS */
  540. SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3c4, 0x1F, 0x3F);
  541. data = 0;
  542. if (ModeNo > 0x13) {
  543. if (SiS_Pr->SiS_ModeType > ModeEGA) {
  544. data |= 0x02;
  545. data |= ((SiS_Pr->SiS_ModeType - ModeVGA) << 2);
  546. }
  547. if (infoflag & InterlaceMode)
  548. data |= 0x20;
  549. }
  550. SiS_SetRegANDOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x06, 0xC0, data);
  551. data = 0;
  552. if (infoflag & InterlaceMode) {
  553. /* data = (Hsync / 8) - ((Htotal / 8) / 2) + 3 */
  554. unsigned short hrs =
  555. (SiS_GetReg(SiS_Pr, SiS_Pr->SiS_P3d4, 0x04) |
  556. ((SiS_GetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x0b) & 0xc0) << 2))
  557. - 3;
  558. unsigned short hto =
  559. (SiS_GetReg(SiS_Pr, SiS_Pr->SiS_P3d4, 0x00) |
  560. ((SiS_GetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x0b) & 0x03) << 8))
  561. + 5;
  562. data = hrs - (hto >> 1) + 3;
  563. }
  564. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, 0x19, (data & 0xFF));
  565. SiS_SetRegANDOR(SiS_Pr, SiS_Pr->SiS_P3d4, 0x1a, 0xFC, (data >> 8));
  566. if (modeflag & HalfDCLK)
  567. SiS_SetRegOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x01, 0x08);
  568. data = 0;
  569. if (modeflag & LineCompareOff)
  570. data = 0x08;
  571. SiS_SetRegANDOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x0F, 0xB7, data);
  572. if ((SiS_Pr->SiS_ModeType == ModeEGA) && (ModeNo > 0x13))
  573. SiS_SetRegOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x0F, 0x40);
  574. SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3c4, 0x31, 0xfb);
  575. data = 0x60;
  576. if (SiS_Pr->SiS_ModeType != ModeText) {
  577. data ^= 0x60;
  578. if (SiS_Pr->SiS_ModeType != ModeEGA)
  579. data ^= 0xA0;
  580. }
  581. SiS_SetRegANDOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x21, 0x1F, data);
  582. SiS_SetVCLKState(SiS_Pr, ModeNo, rrti);
  583. if (SiS_GetReg(SiS_Pr, SiS_Pr->SiS_P3d4, 0x31) & 0x40)
  584. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, 0x52, 0x2c);
  585. else
  586. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, 0x52, 0x6c);
  587. }
  588. /*********************************************/
  589. /* LOAD DAC */
  590. /*********************************************/
  591. static void
  592. SiS_WriteDAC(struct SiS_Private *SiS_Pr, unsigned long DACData,
  593. unsigned short shiftflag, unsigned short dl, unsigned short ah,
  594. unsigned short al, unsigned short dh)
  595. {
  596. unsigned short d1, d2, d3;
  597. switch (dl) {
  598. case 0:
  599. d1 = dh;
  600. d2 = ah;
  601. d3 = al;
  602. break;
  603. case 1:
  604. d1 = ah;
  605. d2 = al;
  606. d3 = dh;
  607. break;
  608. default:
  609. d1 = al;
  610. d2 = dh;
  611. d3 = ah;
  612. }
  613. SiS_SetRegByte(SiS_Pr, DACData, (d1 << shiftflag));
  614. SiS_SetRegByte(SiS_Pr, DACData, (d2 << shiftflag));
  615. SiS_SetRegByte(SiS_Pr, DACData, (d3 << shiftflag));
  616. }
  617. static void
  618. SiS_LoadDAC(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
  619. unsigned short mi)
  620. {
  621. unsigned short data, data2, time, i, j, k, m, n, o;
  622. unsigned short si, di, bx, sf;
  623. unsigned long DACAddr, DACData;
  624. const unsigned char *table = NULL;
  625. if (ModeNo < 0x13)
  626. data = SiS_Pr->SiS_SModeIDTable[mi].St_ModeFlag;
  627. else
  628. data = SiS_Pr->SiS_EModeIDTable[mi].Ext_ModeFlag;
  629. data &= DACInfoFlag;
  630. j = time = 64;
  631. if (data == 0x00)
  632. table = SiS_MDA_DAC;
  633. else if (data == 0x08)
  634. table = SiS_CGA_DAC;
  635. else if (data == 0x10)
  636. table = SiS_EGA_DAC;
  637. else {
  638. j = 16;
  639. time = 256;
  640. table = SiS_VGA_DAC;
  641. }
  642. DACAddr = SiS_Pr->SiS_P3c8;
  643. DACData = SiS_Pr->SiS_P3c9;
  644. sf = 0;
  645. SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3c6, 0xFF);
  646. SiS_SetRegByte(SiS_Pr, DACAddr, 0x00);
  647. for (i = 0; i < j; i++) {
  648. data = table[i];
  649. for (k = 0; k < 3; k++) {
  650. data2 = 0;
  651. if (data & 0x01)
  652. data2 += 0x2A;
  653. if (data & 0x02)
  654. data2 += 0x15;
  655. SiS_SetRegByte(SiS_Pr, DACData, (data2 << sf));
  656. data >>= 2;
  657. }
  658. }
  659. if (time == 256) {
  660. for (i = 16; i < 32; i++) {
  661. data = table[i] << sf;
  662. for (k = 0; k < 3; k++)
  663. SiS_SetRegByte(SiS_Pr, DACData, data);
  664. }
  665. si = 32;
  666. for (m = 0; m < 9; m++) {
  667. di = si;
  668. bx = si + 4;
  669. for (n = 0; n < 3; n++) {
  670. for (o = 0; o < 5; o++) {
  671. SiS_WriteDAC(SiS_Pr, DACData, sf, n,
  672. table[di], table[bx],
  673. table[si]);
  674. si++;
  675. }
  676. si -= 2;
  677. for (o = 0; o < 3; o++) {
  678. SiS_WriteDAC(SiS_Pr, DACData, sf, n,
  679. table[di], table[si],
  680. table[bx]);
  681. si--;
  682. }
  683. }
  684. si += 5;
  685. }
  686. }
  687. }
  688. /*********************************************/
  689. /* SET CRT1 REGISTER GROUP */
  690. /*********************************************/
  691. static void
  692. SiS_SetCRT1Group(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
  693. unsigned short ModeIdIndex)
  694. {
  695. unsigned short StandTableIndex, rrti;
  696. SiS_Pr->SiS_CRT1Mode = ModeNo;
  697. if (ModeNo <= 0x13)
  698. StandTableIndex = 0;
  699. else
  700. StandTableIndex = 1;
  701. SiS_ResetSegmentRegisters(SiS_Pr);
  702. SiS_SetSeqRegs(SiS_Pr, StandTableIndex);
  703. SiS_SetMiscRegs(SiS_Pr, StandTableIndex);
  704. SiS_SetCRTCRegs(SiS_Pr, StandTableIndex);
  705. SiS_SetATTRegs(SiS_Pr, StandTableIndex);
  706. SiS_SetGRCRegs(SiS_Pr, StandTableIndex);
  707. SiS_ClearExt1Regs(SiS_Pr, ModeNo);
  708. rrti = SiS_GetRatePtr(SiS_Pr, ModeNo, ModeIdIndex);
  709. if (rrti != 0xFFFF) {
  710. SiS_SetCRT1Sync(SiS_Pr, rrti);
  711. SiS_SetCRT1CRTC(SiS_Pr, ModeNo, ModeIdIndex, rrti);
  712. SiS_SetCRT1Offset(SiS_Pr, ModeNo, ModeIdIndex, rrti);
  713. SiS_SetCRT1VCLK(SiS_Pr, ModeNo, rrti);
  714. }
  715. SiS_SetCRT1FIFO_310(SiS_Pr, ModeNo, ModeIdIndex);
  716. SiS_SetCRT1ModeRegs(SiS_Pr, ModeNo, ModeIdIndex, rrti);
  717. SiS_LoadDAC(SiS_Pr, ModeNo, ModeIdIndex);
  718. SiS_DisplayOn(SiS_Pr);
  719. }
  720. /*********************************************/
  721. /* SiSSetMode() */
  722. /*********************************************/
  723. int SiSUSBSetMode(struct SiS_Private *SiS_Pr, unsigned short ModeNo)
  724. {
  725. unsigned short ModeIdIndex;
  726. unsigned long BaseAddr = SiS_Pr->IOAddress;
  727. SiSUSB_InitPtr(SiS_Pr);
  728. SiSUSBRegInit(SiS_Pr, BaseAddr);
  729. SiS_GetSysFlags(SiS_Pr);
  730. if (!(SiS_SearchModeID(SiS_Pr, &ModeNo, &ModeIdIndex)))
  731. return 0;
  732. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x05, 0x86);
  733. SiSInitPCIetc(SiS_Pr);
  734. ModeNo &= 0x7f;
  735. SiS_Pr->SiS_ModeType =
  736. SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag & ModeTypeMask;
  737. SiS_Pr->SiS_SetFlag = LowModeTests;
  738. /* Set mode on CRT1 */
  739. SiS_SetCRT1Group(SiS_Pr, ModeNo, ModeIdIndex);
  740. SiS_HandleCRT1(SiS_Pr);
  741. SiS_DisplayOn(SiS_Pr);
  742. SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3c6, 0xFF);
  743. /* Store mode number */
  744. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, 0x34, ModeNo);
  745. return 1;
  746. }
  747. int SiSUSBSetVESAMode(struct SiS_Private *SiS_Pr, unsigned short VModeNo)
  748. {
  749. unsigned short ModeNo = 0;
  750. int i;
  751. SiSUSB_InitPtr(SiS_Pr);
  752. if (VModeNo == 0x03) {
  753. ModeNo = 0x03;
  754. } else {
  755. i = 0;
  756. do {
  757. if (SiS_Pr->SiS_EModeIDTable[i].Ext_VESAID == VModeNo) {
  758. ModeNo = SiS_Pr->SiS_EModeIDTable[i].Ext_ModeID;
  759. break;
  760. }
  761. } while (SiS_Pr->SiS_EModeIDTable[i++].Ext_ModeID != 0xff);
  762. }
  763. if (!ModeNo)
  764. return 0;
  765. return SiSUSBSetMode(SiS_Pr, ModeNo);
  766. }
  767. #endif /* INCL_SISUSB_CON */