sisusb_init.c 25 KB

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