sisusb_init.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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/config.h>
  39. #include <linux/version.h>
  40. #include <linux/module.h>
  41. #include <linux/kernel.h>
  42. #include <linux/errno.h>
  43. #include <linux/poll.h>
  44. #include <linux/init.h>
  45. #include <linux/slab.h>
  46. #include <linux/spinlock.h>
  47. #include <linux/kref.h>
  48. #include "sisusb.h"
  49. #ifdef INCL_SISUSB_CON
  50. #include "sisusb_init.h"
  51. /*********************************************/
  52. /* POINTER INITIALIZATION */
  53. /*********************************************/
  54. static void
  55. SiSUSB_InitPtr(struct SiS_Private *SiS_Pr)
  56. {
  57. SiS_Pr->SiS_ModeResInfo = SiSUSB_ModeResInfo;
  58. SiS_Pr->SiS_StandTable = SiSUSB_StandTable;
  59. SiS_Pr->SiS_SModeIDTable = SiSUSB_SModeIDTable;
  60. SiS_Pr->SiS_EModeIDTable = SiSUSB_EModeIDTable;
  61. SiS_Pr->SiS_RefIndex = SiSUSB_RefIndex;
  62. SiS_Pr->SiS_CRT1Table = SiSUSB_CRT1Table;
  63. SiS_Pr->SiS_VCLKData = SiSUSB_VCLKData;
  64. }
  65. /*********************************************/
  66. /* HELPER: Get ModeID */
  67. /*********************************************/
  68. unsigned short
  69. SiSUSB_GetModeID(int HDisplay, int VDisplay, int Depth)
  70. {
  71. unsigned short ModeIndex = 0;
  72. switch (HDisplay)
  73. {
  74. case 320:
  75. if (VDisplay == 200)
  76. ModeIndex = ModeIndex_320x200[Depth];
  77. else if (VDisplay == 240)
  78. ModeIndex = ModeIndex_320x240[Depth];
  79. break;
  80. case 400:
  81. if (VDisplay == 300)
  82. ModeIndex = ModeIndex_400x300[Depth];
  83. break;
  84. case 512:
  85. if (VDisplay == 384)
  86. ModeIndex = ModeIndex_512x384[Depth];
  87. break;
  88. case 640:
  89. if (VDisplay == 480)
  90. ModeIndex = ModeIndex_640x480[Depth];
  91. else if (VDisplay == 400)
  92. ModeIndex = ModeIndex_640x400[Depth];
  93. break;
  94. case 720:
  95. if (VDisplay == 480)
  96. ModeIndex = ModeIndex_720x480[Depth];
  97. else if (VDisplay == 576)
  98. ModeIndex = ModeIndex_720x576[Depth];
  99. break;
  100. case 768:
  101. if (VDisplay == 576)
  102. ModeIndex = ModeIndex_768x576[Depth];
  103. break;
  104. case 800:
  105. if (VDisplay == 600)
  106. ModeIndex = ModeIndex_800x600[Depth];
  107. else if (VDisplay == 480)
  108. ModeIndex = ModeIndex_800x480[Depth];
  109. break;
  110. case 848:
  111. if (VDisplay == 480)
  112. ModeIndex = ModeIndex_848x480[Depth];
  113. break;
  114. case 856:
  115. if (VDisplay == 480)
  116. ModeIndex = ModeIndex_856x480[Depth];
  117. break;
  118. case 960:
  119. if (VDisplay == 540)
  120. ModeIndex = ModeIndex_960x540[Depth];
  121. else if (VDisplay == 600)
  122. ModeIndex = ModeIndex_960x600[Depth];
  123. break;
  124. case 1024:
  125. if (VDisplay == 576)
  126. ModeIndex = ModeIndex_1024x576[Depth];
  127. else if (VDisplay == 768)
  128. ModeIndex = ModeIndex_1024x768[Depth];
  129. break;
  130. case 1152:
  131. if (VDisplay == 864)
  132. ModeIndex = ModeIndex_1152x864[Depth];
  133. break;
  134. case 1280:
  135. switch (VDisplay) {
  136. case 720:
  137. ModeIndex = ModeIndex_1280x720[Depth];
  138. break;
  139. case 768:
  140. ModeIndex = ModeIndex_1280x768[Depth];
  141. break;
  142. case 1024:
  143. ModeIndex = ModeIndex_1280x1024[Depth];
  144. break;
  145. }
  146. }
  147. return ModeIndex;
  148. }
  149. /*********************************************/
  150. /* HELPER: SetReg, GetReg */
  151. /*********************************************/
  152. static void
  153. SiS_SetReg(struct SiS_Private *SiS_Pr, unsigned long port,
  154. unsigned short index, unsigned short data)
  155. {
  156. sisusb_setidxreg(SiS_Pr->sisusb, port, index, data);
  157. }
  158. static void
  159. SiS_SetRegByte(struct SiS_Private *SiS_Pr, unsigned long port,
  160. unsigned short data)
  161. {
  162. sisusb_setreg(SiS_Pr->sisusb, port, data);
  163. }
  164. static unsigned char
  165. SiS_GetReg(struct SiS_Private *SiS_Pr, unsigned long port,
  166. unsigned short index)
  167. {
  168. u8 data;
  169. sisusb_getidxreg(SiS_Pr->sisusb, port, index, &data);
  170. return data;
  171. }
  172. static unsigned char
  173. SiS_GetRegByte(struct SiS_Private *SiS_Pr, unsigned long port)
  174. {
  175. u8 data;
  176. sisusb_getreg(SiS_Pr->sisusb, port, &data);
  177. return data;
  178. }
  179. static void
  180. SiS_SetRegANDOR(struct SiS_Private *SiS_Pr, unsigned long port,
  181. unsigned short index, unsigned short DataAND,
  182. unsigned short DataOR)
  183. {
  184. sisusb_setidxregandor(SiS_Pr->sisusb, port, index, DataAND, DataOR);
  185. }
  186. static void
  187. SiS_SetRegAND(struct SiS_Private *SiS_Pr, unsigned long port,
  188. unsigned short index, unsigned short DataAND)
  189. {
  190. sisusb_setidxregand(SiS_Pr->sisusb, port, index, DataAND);
  191. }
  192. static void
  193. SiS_SetRegOR(struct SiS_Private *SiS_Pr,unsigned long port,
  194. unsigned short index, unsigned short DataOR)
  195. {
  196. sisusb_setidxregor(SiS_Pr->sisusb, port, index, DataOR);
  197. }
  198. /*********************************************/
  199. /* HELPER: DisplayOn, DisplayOff */
  200. /*********************************************/
  201. static void
  202. SiS_DisplayOn(struct SiS_Private *SiS_Pr)
  203. {
  204. SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3c4, 0x01, 0xDF);
  205. }
  206. /*********************************************/
  207. /* HELPER: Init Port Addresses */
  208. /*********************************************/
  209. void
  210. SiSUSBRegInit(struct SiS_Private *SiS_Pr, unsigned long BaseAddr)
  211. {
  212. SiS_Pr->SiS_P3c4 = BaseAddr + 0x14;
  213. SiS_Pr->SiS_P3d4 = BaseAddr + 0x24;
  214. SiS_Pr->SiS_P3c0 = BaseAddr + 0x10;
  215. SiS_Pr->SiS_P3ce = BaseAddr + 0x1e;
  216. SiS_Pr->SiS_P3c2 = BaseAddr + 0x12;
  217. SiS_Pr->SiS_P3ca = BaseAddr + 0x1a;
  218. SiS_Pr->SiS_P3c6 = BaseAddr + 0x16;
  219. SiS_Pr->SiS_P3c7 = BaseAddr + 0x17;
  220. SiS_Pr->SiS_P3c8 = BaseAddr + 0x18;
  221. SiS_Pr->SiS_P3c9 = BaseAddr + 0x19;
  222. SiS_Pr->SiS_P3cb = BaseAddr + 0x1b;
  223. SiS_Pr->SiS_P3cc = BaseAddr + 0x1c;
  224. SiS_Pr->SiS_P3cd = BaseAddr + 0x1d;
  225. SiS_Pr->SiS_P3da = BaseAddr + 0x2a;
  226. SiS_Pr->SiS_Part1Port = BaseAddr + SIS_CRT2_PORT_04;
  227. }
  228. /*********************************************/
  229. /* HELPER: GetSysFlags */
  230. /*********************************************/
  231. static void
  232. SiS_GetSysFlags(struct SiS_Private *SiS_Pr)
  233. {
  234. SiS_Pr->SiS_MyCR63 = 0x63;
  235. }
  236. /*********************************************/
  237. /* HELPER: Init PCI & Engines */
  238. /*********************************************/
  239. static void
  240. SiSInitPCIetc(struct SiS_Private *SiS_Pr)
  241. {
  242. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x20, 0xa1);
  243. /* - Enable 2D (0x40)
  244. * - Enable 3D (0x02)
  245. * - Enable 3D vertex command fetch (0x10)
  246. * - Enable 3D command parser (0x08)
  247. * - Enable 3D G/L transformation engine (0x80)
  248. */
  249. SiS_SetRegOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x1E, 0xDA);
  250. }
  251. /*********************************************/
  252. /* HELPER: SET SEGMENT REGISTERS */
  253. /*********************************************/
  254. static void
  255. SiS_SetSegRegLower(struct SiS_Private *SiS_Pr, unsigned short value)
  256. {
  257. unsigned short temp;
  258. value &= 0x00ff;
  259. temp = SiS_GetRegByte(SiS_Pr, SiS_Pr->SiS_P3cb) & 0xf0;
  260. temp |= (value >> 4);
  261. SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3cb, temp);
  262. temp = SiS_GetRegByte(SiS_Pr, SiS_Pr->SiS_P3cd) & 0xf0;
  263. temp |= (value & 0x0f);
  264. SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3cd, temp);
  265. }
  266. static void
  267. SiS_SetSegRegUpper(struct SiS_Private *SiS_Pr, unsigned short value)
  268. {
  269. unsigned short temp;
  270. value &= 0x00ff;
  271. temp = SiS_GetRegByte(SiS_Pr, SiS_Pr->SiS_P3cb) & 0x0f;
  272. temp |= (value & 0xf0);
  273. SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3cb, temp);
  274. temp = SiS_GetRegByte(SiS_Pr, SiS_Pr->SiS_P3cd) & 0x0f;
  275. temp |= (value << 4);
  276. SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3cd, temp);
  277. }
  278. static void
  279. SiS_SetSegmentReg(struct SiS_Private *SiS_Pr, unsigned short value)
  280. {
  281. SiS_SetSegRegLower(SiS_Pr, value);
  282. SiS_SetSegRegUpper(SiS_Pr, value);
  283. }
  284. static void
  285. SiS_ResetSegmentReg(struct SiS_Private *SiS_Pr)
  286. {
  287. SiS_SetSegmentReg(SiS_Pr, 0);
  288. }
  289. static void
  290. SiS_SetSegmentRegOver(struct SiS_Private *SiS_Pr, unsigned short value)
  291. {
  292. unsigned short temp = value >> 8;
  293. temp &= 0x07;
  294. temp |= (temp << 4);
  295. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x1d, temp);
  296. SiS_SetSegmentReg(SiS_Pr, value);
  297. }
  298. static void
  299. SiS_ResetSegmentRegOver(struct SiS_Private *SiS_Pr)
  300. {
  301. SiS_SetSegmentRegOver(SiS_Pr, 0);
  302. }
  303. static void
  304. SiS_ResetSegmentRegisters(struct SiS_Private *SiS_Pr)
  305. {
  306. SiS_ResetSegmentReg(SiS_Pr);
  307. SiS_ResetSegmentRegOver(SiS_Pr);
  308. }
  309. /*********************************************/
  310. /* HELPER: SearchModeID */
  311. /*********************************************/
  312. static int
  313. SiS_SearchModeID(struct SiS_Private *SiS_Pr, unsigned short *ModeNo,
  314. unsigned short *ModeIdIndex)
  315. {
  316. if ((*ModeNo) <= 0x13) {
  317. if ((*ModeNo) != 0x03)
  318. return 0;
  319. (*ModeIdIndex) = 0;
  320. } else {
  321. for(*ModeIdIndex = 0; ;(*ModeIdIndex)++) {
  322. if (SiS_Pr->SiS_EModeIDTable[*ModeIdIndex].Ext_ModeID == (*ModeNo))
  323. break;
  324. if (SiS_Pr->SiS_EModeIDTable[*ModeIdIndex].Ext_ModeID == 0xFF)
  325. return 0;
  326. }
  327. }
  328. return 1;
  329. }
  330. /*********************************************/
  331. /* HELPER: ENABLE CRT1 */
  332. /*********************************************/
  333. static void
  334. SiS_HandleCRT1(struct SiS_Private *SiS_Pr)
  335. {
  336. /* Enable CRT1 gating */
  337. SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3d4, SiS_Pr->SiS_MyCR63, 0xbf);
  338. }
  339. /*********************************************/
  340. /* HELPER: GetColorDepth */
  341. /*********************************************/
  342. static unsigned short
  343. SiS_GetColorDepth(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
  344. unsigned short ModeIdIndex)
  345. {
  346. static const unsigned short ColorDepth[6] = { 1, 2, 4, 4, 6, 8};
  347. unsigned short modeflag;
  348. short index;
  349. if (ModeNo <= 0x13) {
  350. modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
  351. } else {
  352. modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
  353. }
  354. index = (modeflag & ModeTypeMask) - ModeEGA;
  355. if (index < 0) index = 0;
  356. return ColorDepth[index];
  357. }
  358. /*********************************************/
  359. /* HELPER: GetOffset */
  360. /*********************************************/
  361. static unsigned short
  362. SiS_GetOffset(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
  363. unsigned short ModeIdIndex, unsigned short rrti)
  364. {
  365. unsigned short xres, temp, colordepth, infoflag;
  366. infoflag = SiS_Pr->SiS_RefIndex[rrti].Ext_InfoFlag;
  367. xres = SiS_Pr->SiS_RefIndex[rrti].XRes;
  368. colordepth = SiS_GetColorDepth(SiS_Pr, ModeNo, ModeIdIndex);
  369. temp = xres / 16;
  370. if (infoflag & InterlaceMode)
  371. temp <<= 1;
  372. temp *= colordepth;
  373. if (xres % 16)
  374. temp += (colordepth >> 1);
  375. return temp;
  376. }
  377. /*********************************************/
  378. /* SEQ */
  379. /*********************************************/
  380. static void
  381. SiS_SetSeqRegs(struct SiS_Private *SiS_Pr, unsigned short StandTableIndex)
  382. {
  383. unsigned char SRdata;
  384. int i;
  385. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x00, 0x03);
  386. SRdata = SiS_Pr->SiS_StandTable[StandTableIndex].SR[0] | 0x20;
  387. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x01, SRdata);
  388. for(i = 2; i <= 4; i++) {
  389. SRdata = SiS_Pr->SiS_StandTable[StandTableIndex].SR[i-1];
  390. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, i, SRdata);
  391. }
  392. }
  393. /*********************************************/
  394. /* MISC */
  395. /*********************************************/
  396. static void
  397. SiS_SetMiscRegs(struct SiS_Private *SiS_Pr, unsigned short StandTableIndex)
  398. {
  399. unsigned char Miscdata = SiS_Pr->SiS_StandTable[StandTableIndex].MISC;
  400. SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3c2, Miscdata);
  401. }
  402. /*********************************************/
  403. /* CRTC */
  404. /*********************************************/
  405. static void
  406. SiS_SetCRTCRegs(struct SiS_Private *SiS_Pr, unsigned short StandTableIndex)
  407. {
  408. unsigned char CRTCdata;
  409. unsigned short i;
  410. SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3d4, 0x11, 0x7f);
  411. for(i = 0; i <= 0x18; i++) {
  412. CRTCdata = SiS_Pr->SiS_StandTable[StandTableIndex].CRTC[i];
  413. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, i, CRTCdata);
  414. }
  415. }
  416. /*********************************************/
  417. /* ATT */
  418. /*********************************************/
  419. static void
  420. SiS_SetATTRegs(struct SiS_Private *SiS_Pr, unsigned short StandTableIndex)
  421. {
  422. unsigned char ARdata;
  423. unsigned short i;
  424. for(i = 0; i <= 0x13; i++) {
  425. ARdata = SiS_Pr->SiS_StandTable[StandTableIndex].ATTR[i];
  426. SiS_GetRegByte(SiS_Pr, SiS_Pr->SiS_P3da);
  427. SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3c0, i);
  428. SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3c0, ARdata);
  429. }
  430. SiS_GetRegByte(SiS_Pr, SiS_Pr->SiS_P3da);
  431. SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3c0, 0x14);
  432. SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3c0, 0x00);
  433. SiS_GetRegByte(SiS_Pr, SiS_Pr->SiS_P3da);
  434. SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3c0, 0x20);
  435. SiS_GetRegByte(SiS_Pr, SiS_Pr->SiS_P3da);
  436. }
  437. /*********************************************/
  438. /* GRC */
  439. /*********************************************/
  440. static void
  441. SiS_SetGRCRegs(struct SiS_Private *SiS_Pr, unsigned short StandTableIndex)
  442. {
  443. unsigned char GRdata;
  444. unsigned short i;
  445. for(i = 0; i <= 0x08; i++) {
  446. GRdata = SiS_Pr->SiS_StandTable[StandTableIndex].GRC[i];
  447. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3ce, i, GRdata);
  448. }
  449. if (SiS_Pr->SiS_ModeType > ModeVGA) {
  450. /* 256 color disable */
  451. SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3ce, 0x05, 0xBF);
  452. }
  453. }
  454. /*********************************************/
  455. /* CLEAR EXTENDED REGISTERS */
  456. /*********************************************/
  457. static void
  458. SiS_ClearExt1Regs(struct SiS_Private *SiS_Pr, unsigned short ModeNo)
  459. {
  460. int i;
  461. for(i = 0x0A; i <= 0x0E; i++) {
  462. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, i, 0x00);
  463. }
  464. SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3c4, 0x37, 0xFE);
  465. }
  466. /*********************************************/
  467. /* Get rate index */
  468. /*********************************************/
  469. static unsigned short
  470. SiS_GetRatePtr(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
  471. unsigned short ModeIdIndex)
  472. {
  473. unsigned short rrti, i, index, temp;
  474. if (ModeNo <= 0x13)
  475. return 0xFFFF;
  476. index = SiS_GetReg(SiS_Pr,SiS_Pr->SiS_P3d4, 0x33) & 0x0F;
  477. if (index > 0) index--;
  478. rrti = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].REFindex;
  479. ModeNo = SiS_Pr->SiS_RefIndex[rrti].ModeID;
  480. i = 0;
  481. do {
  482. if (SiS_Pr->SiS_RefIndex[rrti + i].ModeID != ModeNo)
  483. break;
  484. temp = SiS_Pr->SiS_RefIndex[rrti + i].Ext_InfoFlag & ModeTypeMask;
  485. if (temp < SiS_Pr->SiS_ModeType)
  486. break;
  487. i++;
  488. index--;
  489. } while(index != 0xFFFF);
  490. i--;
  491. return (rrti + i);
  492. }
  493. /*********************************************/
  494. /* SYNC */
  495. /*********************************************/
  496. static void
  497. SiS_SetCRT1Sync(struct SiS_Private *SiS_Pr, unsigned short rrti)
  498. {
  499. unsigned short sync = SiS_Pr->SiS_RefIndex[rrti].Ext_InfoFlag >> 8;
  500. sync &= 0xC0;
  501. sync |= 0x2f;
  502. SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3c2, sync);
  503. }
  504. /*********************************************/
  505. /* CRTC/2 */
  506. /*********************************************/
  507. static void
  508. SiS_SetCRT1CRTC(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
  509. unsigned short ModeIdIndex, unsigned short rrti)
  510. {
  511. unsigned char index;
  512. unsigned short temp, i, j, modeflag;
  513. SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3d4,0x11,0x7f);
  514. modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
  515. index = SiS_Pr->SiS_RefIndex[rrti].Ext_CRT1CRTC;
  516. for(i = 0,j = 0; i <= 7; i++, j++) {
  517. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, j,
  518. SiS_Pr->SiS_CRT1Table[index].CR[i]);
  519. }
  520. for(j = 0x10; i <= 10; i++, j++) {
  521. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, j,
  522. SiS_Pr->SiS_CRT1Table[index].CR[i]);
  523. }
  524. for(j = 0x15; i <= 12; i++, j++) {
  525. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, j,
  526. SiS_Pr->SiS_CRT1Table[index].CR[i]);
  527. }
  528. for(j = 0x0A; i <= 15; i++, j++) {
  529. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, j,
  530. SiS_Pr->SiS_CRT1Table[index].CR[i]);
  531. }
  532. temp = SiS_Pr->SiS_CRT1Table[index].CR[16] & 0xE0;
  533. SiS_SetReg(SiS_Pr,SiS_Pr->SiS_P3c4, 0x0E, temp);
  534. temp = ((SiS_Pr->SiS_CRT1Table[index].CR[16]) & 0x01) << 5;
  535. if (modeflag & DoubleScanMode) temp |= 0x80;
  536. SiS_SetRegANDOR(SiS_Pr, SiS_Pr->SiS_P3d4, 0x09, 0x5F, temp);
  537. if (SiS_Pr->SiS_ModeType > ModeVGA)
  538. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, 0x14, 0x4F);
  539. }
  540. /*********************************************/
  541. /* OFFSET & PITCH */
  542. /*********************************************/
  543. /* (partly overruled by SetPitch() in XF86) */
  544. /*********************************************/
  545. static void
  546. SiS_SetCRT1Offset(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
  547. unsigned short ModeIdIndex, unsigned short rrti)
  548. {
  549. unsigned short du = SiS_GetOffset(SiS_Pr, ModeNo, ModeIdIndex, rrti);
  550. unsigned short infoflag = SiS_Pr->SiS_RefIndex[rrti].Ext_InfoFlag;
  551. unsigned short temp;
  552. temp = (du >> 8) & 0x0f;
  553. SiS_SetRegANDOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x0E, 0xF0, temp);
  554. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, 0x13, (du & 0xFF));
  555. if (infoflag & InterlaceMode) du >>= 1;
  556. du <<= 5;
  557. temp = (du >> 8) & 0xff;
  558. if (du & 0xff) temp++;
  559. temp++;
  560. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x10, temp);
  561. }
  562. /*********************************************/
  563. /* VCLK */
  564. /*********************************************/
  565. static void
  566. SiS_SetCRT1VCLK(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
  567. unsigned short rrti)
  568. {
  569. unsigned short index = SiS_Pr->SiS_RefIndex[rrti].Ext_CRTVCLK;
  570. unsigned short clka = SiS_Pr->SiS_VCLKData[index].SR2B;
  571. unsigned short clkb = SiS_Pr->SiS_VCLKData[index].SR2C;
  572. SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3c4,0x31,0xCF);
  573. SiS_SetReg(SiS_Pr,SiS_Pr->SiS_P3c4,0x2B,clka);
  574. SiS_SetReg(SiS_Pr,SiS_Pr->SiS_P3c4,0x2C,clkb);
  575. SiS_SetReg(SiS_Pr,SiS_Pr->SiS_P3c4,0x2D,0x01);
  576. }
  577. /*********************************************/
  578. /* FIFO */
  579. /*********************************************/
  580. static void
  581. SiS_SetCRT1FIFO_310(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
  582. unsigned short mi)
  583. {
  584. unsigned short modeflag = SiS_Pr->SiS_EModeIDTable[mi].Ext_ModeFlag;
  585. /* disable auto-threshold */
  586. SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3c4, 0x3D, 0xFE);
  587. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x08, 0xAE);
  588. SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3c4, 0x09, 0xF0);
  589. if (ModeNo <= 0x13)
  590. return;
  591. if ((!(modeflag & DoubleScanMode)) || (!(modeflag & HalfDCLK))) {
  592. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x08, 0x34);
  593. SiS_SetRegOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x3D, 0x01);
  594. }
  595. }
  596. /*********************************************/
  597. /* MODE REGISTERS */
  598. /*********************************************/
  599. static void
  600. SiS_SetVCLKState(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
  601. unsigned short rrti)
  602. {
  603. unsigned short data = 0, VCLK = 0, index = 0;
  604. if (ModeNo > 0x13) {
  605. index = SiS_Pr->SiS_RefIndex[rrti].Ext_CRTVCLK;
  606. VCLK = SiS_Pr->SiS_VCLKData[index].CLOCK;
  607. }
  608. if (VCLK >= 166) data |= 0x0c;
  609. SiS_SetRegANDOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x32, 0xf3, data);
  610. if (VCLK >= 166)
  611. SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3c4, 0x1f, 0xe7);
  612. /* DAC speed */
  613. data = 0x03;
  614. if (VCLK >= 260)
  615. data = 0x00;
  616. else if (VCLK >= 160)
  617. data = 0x01;
  618. else if (VCLK >= 135)
  619. data = 0x02;
  620. SiS_SetRegANDOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x07, 0xF8, data);
  621. }
  622. static void
  623. SiS_SetCRT1ModeRegs(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
  624. unsigned short ModeIdIndex, unsigned short rrti)
  625. {
  626. unsigned short data, infoflag = 0, modeflag;
  627. if (ModeNo <= 0x13)
  628. modeflag = SiS_Pr->SiS_SModeIDTable[ModeIdIndex].St_ModeFlag;
  629. else {
  630. modeflag = SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag;
  631. infoflag = SiS_Pr->SiS_RefIndex[rrti].Ext_InfoFlag;
  632. }
  633. /* Disable DPMS */
  634. SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3c4, 0x1F, 0x3F);
  635. data = 0;
  636. if (ModeNo > 0x13) {
  637. if (SiS_Pr->SiS_ModeType > ModeEGA) {
  638. data |= 0x02;
  639. data |= ((SiS_Pr->SiS_ModeType - ModeVGA) << 2);
  640. }
  641. if (infoflag & InterlaceMode) data |= 0x20;
  642. }
  643. SiS_SetRegANDOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x06, 0xC0, data);
  644. data = 0;
  645. if (infoflag & InterlaceMode) {
  646. /* data = (Hsync / 8) - ((Htotal / 8) / 2) + 3 */
  647. unsigned short hrs = (SiS_GetReg(SiS_Pr, SiS_Pr->SiS_P3d4, 0x04) |
  648. ((SiS_GetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x0b) & 0xc0) << 2)) - 3;
  649. unsigned short hto = (SiS_GetReg(SiS_Pr, SiS_Pr->SiS_P3d4, 0x00) |
  650. ((SiS_GetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x0b) & 0x03) << 8)) + 5;
  651. data = hrs - (hto >> 1) + 3;
  652. }
  653. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, 0x19, (data & 0xFF));
  654. SiS_SetRegANDOR(SiS_Pr, SiS_Pr->SiS_P3d4, 0x1a, 0xFC, (data >> 8));
  655. if (modeflag & HalfDCLK)
  656. SiS_SetRegOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x01, 0x08);
  657. data = 0;
  658. if (modeflag & LineCompareOff)
  659. data = 0x08;
  660. SiS_SetRegANDOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x0F, 0xB7, data);
  661. if ((SiS_Pr->SiS_ModeType == ModeEGA) && (ModeNo > 0x13))
  662. SiS_SetRegOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x0F, 0x40);
  663. SiS_SetRegAND(SiS_Pr, SiS_Pr->SiS_P3c4, 0x31, 0xfb);
  664. data = 0x60;
  665. if (SiS_Pr->SiS_ModeType != ModeText) {
  666. data ^= 0x60;
  667. if (SiS_Pr->SiS_ModeType != ModeEGA)
  668. data ^= 0xA0;
  669. }
  670. SiS_SetRegANDOR(SiS_Pr, SiS_Pr->SiS_P3c4, 0x21, 0x1F, data);
  671. SiS_SetVCLKState(SiS_Pr, ModeNo, rrti);
  672. if (SiS_GetReg(SiS_Pr, SiS_Pr->SiS_P3d4, 0x31) & 0x40)
  673. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, 0x52, 0x2c);
  674. else
  675. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, 0x52, 0x6c);
  676. }
  677. /*********************************************/
  678. /* LOAD DAC */
  679. /*********************************************/
  680. static void
  681. SiS_WriteDAC(struct SiS_Private *SiS_Pr, unsigned long DACData,
  682. unsigned short shiftflag, unsigned short dl, unsigned short ah,
  683. unsigned short al, unsigned short dh)
  684. {
  685. unsigned short d1, d2, d3;
  686. switch (dl) {
  687. case 0:
  688. d1 = dh; d2 = ah; d3 = al;
  689. break;
  690. case 1:
  691. d1 = ah; d2 = al; d3 = dh;
  692. break;
  693. default:
  694. d1 = al; d2 = dh; d3 = ah;
  695. }
  696. SiS_SetRegByte(SiS_Pr, DACData, (d1 << shiftflag));
  697. SiS_SetRegByte(SiS_Pr, DACData, (d2 << shiftflag));
  698. SiS_SetRegByte(SiS_Pr, DACData, (d3 << shiftflag));
  699. }
  700. static void
  701. SiS_LoadDAC(struct SiS_Private *SiS_Pr, unsigned short ModeNo, unsigned short mi)
  702. {
  703. unsigned short data, data2, time, i, j, k, m, n, o;
  704. unsigned short si, di, bx, sf;
  705. unsigned long DACAddr, DACData;
  706. const unsigned char *table = NULL;
  707. if (ModeNo < 0x13)
  708. data = SiS_Pr->SiS_SModeIDTable[mi].St_ModeFlag;
  709. else
  710. data = SiS_Pr->SiS_EModeIDTable[mi].Ext_ModeFlag;
  711. data &= DACInfoFlag;
  712. j = time = 64;
  713. if (data == 0x00)
  714. table = SiS_MDA_DAC;
  715. else if (data == 0x08)
  716. table = SiS_CGA_DAC;
  717. else if (data == 0x10)
  718. table = SiS_EGA_DAC;
  719. else {
  720. j = 16;
  721. time = 256;
  722. table = SiS_VGA_DAC;
  723. }
  724. DACAddr = SiS_Pr->SiS_P3c8;
  725. DACData = SiS_Pr->SiS_P3c9;
  726. sf = 0;
  727. SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3c6, 0xFF);
  728. SiS_SetRegByte(SiS_Pr, DACAddr, 0x00);
  729. for(i = 0; i < j; i++) {
  730. data = table[i];
  731. for(k = 0; k < 3; k++) {
  732. data2 = 0;
  733. if (data & 0x01) data2 += 0x2A;
  734. if (data & 0x02) data2 += 0x15;
  735. SiS_SetRegByte(SiS_Pr, DACData, (data2 << sf));
  736. data >>= 2;
  737. }
  738. }
  739. if (time == 256) {
  740. for(i = 16; i < 32; i++) {
  741. data = table[i] << sf;
  742. for(k = 0; k < 3; k++)
  743. SiS_SetRegByte(SiS_Pr, DACData, data);
  744. }
  745. si = 32;
  746. for(m = 0; m < 9; m++) {
  747. di = si;
  748. bx = si + 4;
  749. for(n = 0; n < 3; n++) {
  750. for(o = 0; o < 5; o++) {
  751. SiS_WriteDAC(SiS_Pr, DACData, sf, n,
  752. table[di], table[bx], table[si]);
  753. si++;
  754. }
  755. si -= 2;
  756. for(o = 0; o < 3; o++) {
  757. SiS_WriteDAC(SiS_Pr, DACData, sf, n,
  758. table[di], table[si], table[bx]);
  759. si--;
  760. }
  761. }
  762. si += 5;
  763. }
  764. }
  765. }
  766. /*********************************************/
  767. /* SET CRT1 REGISTER GROUP */
  768. /*********************************************/
  769. static void
  770. SiS_SetCRT1Group(struct SiS_Private *SiS_Pr, unsigned short ModeNo,
  771. unsigned short ModeIdIndex)
  772. {
  773. unsigned short StandTableIndex, rrti;
  774. SiS_Pr->SiS_CRT1Mode = ModeNo;
  775. if (ModeNo <= 0x13)
  776. StandTableIndex = 0;
  777. else
  778. StandTableIndex = 1;
  779. SiS_ResetSegmentRegisters(SiS_Pr);
  780. SiS_SetSeqRegs(SiS_Pr, StandTableIndex);
  781. SiS_SetMiscRegs(SiS_Pr, StandTableIndex);
  782. SiS_SetCRTCRegs(SiS_Pr, StandTableIndex);
  783. SiS_SetATTRegs(SiS_Pr, StandTableIndex);
  784. SiS_SetGRCRegs(SiS_Pr, StandTableIndex);
  785. SiS_ClearExt1Regs(SiS_Pr, ModeNo);
  786. rrti = SiS_GetRatePtr(SiS_Pr, ModeNo, ModeIdIndex);
  787. if (rrti != 0xFFFF) {
  788. SiS_SetCRT1Sync(SiS_Pr, rrti);
  789. SiS_SetCRT1CRTC(SiS_Pr, ModeNo, ModeIdIndex, rrti);
  790. SiS_SetCRT1Offset(SiS_Pr, ModeNo, ModeIdIndex, rrti);
  791. SiS_SetCRT1VCLK(SiS_Pr, ModeNo, rrti);
  792. }
  793. SiS_SetCRT1FIFO_310(SiS_Pr, ModeNo, ModeIdIndex);
  794. SiS_SetCRT1ModeRegs(SiS_Pr, ModeNo, ModeIdIndex, rrti);
  795. SiS_LoadDAC(SiS_Pr, ModeNo, ModeIdIndex);
  796. SiS_DisplayOn(SiS_Pr);
  797. }
  798. /*********************************************/
  799. /* SiSSetMode() */
  800. /*********************************************/
  801. int
  802. SiSUSBSetMode(struct SiS_Private *SiS_Pr, unsigned short ModeNo)
  803. {
  804. unsigned short ModeIdIndex;
  805. unsigned long BaseAddr = SiS_Pr->IOAddress;
  806. SiSUSB_InitPtr(SiS_Pr);
  807. SiSUSBRegInit(SiS_Pr, BaseAddr);
  808. SiS_GetSysFlags(SiS_Pr);
  809. if (!(SiS_SearchModeID(SiS_Pr, &ModeNo, &ModeIdIndex)))
  810. return 0;
  811. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3c4, 0x05, 0x86);
  812. SiSInitPCIetc(SiS_Pr);
  813. ModeNo &= 0x7f;
  814. SiS_Pr->SiS_ModeType =
  815. SiS_Pr->SiS_EModeIDTable[ModeIdIndex].Ext_ModeFlag & ModeTypeMask;
  816. SiS_Pr->SiS_SetFlag = LowModeTests;
  817. /* Set mode on CRT1 */
  818. SiS_SetCRT1Group(SiS_Pr, ModeNo, ModeIdIndex);
  819. SiS_HandleCRT1(SiS_Pr);
  820. SiS_DisplayOn(SiS_Pr);
  821. SiS_SetRegByte(SiS_Pr, SiS_Pr->SiS_P3c6, 0xFF);
  822. /* Store mode number */
  823. SiS_SetReg(SiS_Pr, SiS_Pr->SiS_P3d4, 0x34, ModeNo);
  824. return 1;
  825. }
  826. int
  827. SiSUSBSetVESAMode(struct SiS_Private *SiS_Pr, unsigned short VModeNo)
  828. {
  829. unsigned short ModeNo = 0;
  830. int i;
  831. SiSUSB_InitPtr(SiS_Pr);
  832. if (VModeNo == 0x03) {
  833. ModeNo = 0x03;
  834. } else {
  835. i = 0;
  836. do {
  837. if (SiS_Pr->SiS_EModeIDTable[i].Ext_VESAID == VModeNo) {
  838. ModeNo = SiS_Pr->SiS_EModeIDTable[i].Ext_ModeID;
  839. break;
  840. }
  841. } while (SiS_Pr->SiS_EModeIDTable[i++].Ext_ModeID != 0xff);
  842. }
  843. if (!ModeNo)
  844. return 0;
  845. return SiSUSBSetMode(SiS_Pr, ModeNo);
  846. }
  847. #endif /* INCL_SISUSB_CON */