savagefb_driver.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571
  1. /*
  2. * linux/drivers/video/savagefb.c -- S3 Savage Framebuffer Driver
  3. *
  4. * Copyright (c) 2001-2002 Denis Oliver Kropp <[email protected]>
  5. * Sven Neumann <[email protected]>
  6. *
  7. *
  8. * Card specific code is based on XFree86's savage driver.
  9. * Framebuffer framework code is based on code of cyber2000fb and tdfxfb.
  10. *
  11. * This file is subject to the terms and conditions of the GNU General
  12. * Public License. See the file COPYING in the main directory of this
  13. * archive for more details.
  14. *
  15. * 0.4.0 (neo)
  16. * - hardware accelerated clear and move
  17. *
  18. * 0.3.2 (dok)
  19. * - wait for vertical retrace before writing to cr67
  20. * at the beginning of savagefb_set_par
  21. * - use synchronization registers cr23 and cr26
  22. *
  23. * 0.3.1 (dok)
  24. * - reset 3D engine
  25. * - don't return alpha bits for 32bit format
  26. *
  27. * 0.3.0 (dok)
  28. * - added WaitIdle functions for all Savage types
  29. * - do WaitIdle before mode switching
  30. * - code cleanup
  31. *
  32. * 0.2.0 (dok)
  33. * - first working version
  34. *
  35. *
  36. * TODO
  37. * - clock validations in decode_var
  38. *
  39. * BUGS
  40. * - white margin on bootup
  41. *
  42. */
  43. #include <linux/aperture.h>
  44. #include <linux/module.h>
  45. #include <linux/kernel.h>
  46. #include <linux/errno.h>
  47. #include <linux/string.h>
  48. #include <linux/mm.h>
  49. #include <linux/slab.h>
  50. #include <linux/delay.h>
  51. #include <linux/fb.h>
  52. #include <linux/pci.h>
  53. #include <linux/init.h>
  54. #include <linux/console.h>
  55. #include <asm/io.h>
  56. #include <asm/irq.h>
  57. #include "savagefb.h"
  58. #define SAVAGEFB_VERSION "0.4.0_2.6"
  59. /* --------------------------------------------------------------------- */
  60. static char *mode_option = NULL;
  61. #ifdef MODULE
  62. MODULE_AUTHOR("(c) 2001-2002 Denis Oliver Kropp <[email protected]>");
  63. MODULE_LICENSE("GPL");
  64. MODULE_DESCRIPTION("FBDev driver for S3 Savage PCI/AGP Chips");
  65. #endif
  66. /* --------------------------------------------------------------------- */
  67. static void vgaHWSeqReset(struct savagefb_par *par, int start)
  68. {
  69. if (start)
  70. VGAwSEQ(0x00, 0x01, par); /* Synchronous Reset */
  71. else
  72. VGAwSEQ(0x00, 0x03, par); /* End Reset */
  73. }
  74. static void vgaHWProtect(struct savagefb_par *par, int on)
  75. {
  76. unsigned char tmp;
  77. if (on) {
  78. /*
  79. * Turn off screen and disable sequencer.
  80. */
  81. tmp = VGArSEQ(0x01, par);
  82. vgaHWSeqReset(par, 1); /* start synchronous reset */
  83. VGAwSEQ(0x01, tmp | 0x20, par);/* disable the display */
  84. VGAenablePalette(par);
  85. } else {
  86. /*
  87. * Reenable sequencer, then turn on screen.
  88. */
  89. tmp = VGArSEQ(0x01, par);
  90. VGAwSEQ(0x01, tmp & ~0x20, par);/* reenable display */
  91. vgaHWSeqReset(par, 0); /* clear synchronous reset */
  92. VGAdisablePalette(par);
  93. }
  94. }
  95. static void vgaHWRestore(struct savagefb_par *par, struct savage_reg *reg)
  96. {
  97. int i;
  98. VGAwMISC(reg->MiscOutReg, par);
  99. for (i = 1; i < 5; i++)
  100. VGAwSEQ(i, reg->Sequencer[i], par);
  101. /* Ensure CRTC registers 0-7 are unlocked by clearing bit 7 or
  102. CRTC[17] */
  103. VGAwCR(17, reg->CRTC[17] & ~0x80, par);
  104. for (i = 0; i < 25; i++)
  105. VGAwCR(i, reg->CRTC[i], par);
  106. for (i = 0; i < 9; i++)
  107. VGAwGR(i, reg->Graphics[i], par);
  108. VGAenablePalette(par);
  109. for (i = 0; i < 21; i++)
  110. VGAwATTR(i, reg->Attribute[i], par);
  111. VGAdisablePalette(par);
  112. }
  113. static void vgaHWInit(struct fb_var_screeninfo *var,
  114. struct savagefb_par *par,
  115. struct xtimings *timings,
  116. struct savage_reg *reg)
  117. {
  118. reg->MiscOutReg = 0x23;
  119. if (!(timings->sync & FB_SYNC_HOR_HIGH_ACT))
  120. reg->MiscOutReg |= 0x40;
  121. if (!(timings->sync & FB_SYNC_VERT_HIGH_ACT))
  122. reg->MiscOutReg |= 0x80;
  123. /*
  124. * Time Sequencer
  125. */
  126. reg->Sequencer[0x00] = 0x00;
  127. reg->Sequencer[0x01] = 0x01;
  128. reg->Sequencer[0x02] = 0x0F;
  129. reg->Sequencer[0x03] = 0x00; /* Font select */
  130. reg->Sequencer[0x04] = 0x0E; /* Misc */
  131. /*
  132. * CRTC Controller
  133. */
  134. reg->CRTC[0x00] = (timings->HTotal >> 3) - 5;
  135. reg->CRTC[0x01] = (timings->HDisplay >> 3) - 1;
  136. reg->CRTC[0x02] = (timings->HSyncStart >> 3) - 1;
  137. reg->CRTC[0x03] = (((timings->HSyncEnd >> 3) - 1) & 0x1f) | 0x80;
  138. reg->CRTC[0x04] = (timings->HSyncStart >> 3);
  139. reg->CRTC[0x05] = ((((timings->HSyncEnd >> 3) - 1) & 0x20) << 2) |
  140. (((timings->HSyncEnd >> 3)) & 0x1f);
  141. reg->CRTC[0x06] = (timings->VTotal - 2) & 0xFF;
  142. reg->CRTC[0x07] = (((timings->VTotal - 2) & 0x100) >> 8) |
  143. (((timings->VDisplay - 1) & 0x100) >> 7) |
  144. ((timings->VSyncStart & 0x100) >> 6) |
  145. (((timings->VSyncStart - 1) & 0x100) >> 5) |
  146. 0x10 |
  147. (((timings->VTotal - 2) & 0x200) >> 4) |
  148. (((timings->VDisplay - 1) & 0x200) >> 3) |
  149. ((timings->VSyncStart & 0x200) >> 2);
  150. reg->CRTC[0x08] = 0x00;
  151. reg->CRTC[0x09] = (((timings->VSyncStart - 1) & 0x200) >> 4) | 0x40;
  152. if (timings->dblscan)
  153. reg->CRTC[0x09] |= 0x80;
  154. reg->CRTC[0x0a] = 0x00;
  155. reg->CRTC[0x0b] = 0x00;
  156. reg->CRTC[0x0c] = 0x00;
  157. reg->CRTC[0x0d] = 0x00;
  158. reg->CRTC[0x0e] = 0x00;
  159. reg->CRTC[0x0f] = 0x00;
  160. reg->CRTC[0x10] = timings->VSyncStart & 0xff;
  161. reg->CRTC[0x11] = (timings->VSyncEnd & 0x0f) | 0x20;
  162. reg->CRTC[0x12] = (timings->VDisplay - 1) & 0xff;
  163. reg->CRTC[0x13] = var->xres_virtual >> 4;
  164. reg->CRTC[0x14] = 0x00;
  165. reg->CRTC[0x15] = (timings->VSyncStart - 1) & 0xff;
  166. reg->CRTC[0x16] = (timings->VSyncEnd - 1) & 0xff;
  167. reg->CRTC[0x17] = 0xc3;
  168. reg->CRTC[0x18] = 0xff;
  169. /*
  170. * are these unnecessary?
  171. * vgaHWHBlankKGA(mode, regp, 0, KGA_FIX_OVERSCAN|KGA_ENABLE_ON_ZERO);
  172. * vgaHWVBlankKGA(mode, regp, 0, KGA_FIX_OVERSCAN|KGA_ENABLE_ON_ZERO);
  173. */
  174. /*
  175. * Graphics Display Controller
  176. */
  177. reg->Graphics[0x00] = 0x00;
  178. reg->Graphics[0x01] = 0x00;
  179. reg->Graphics[0x02] = 0x00;
  180. reg->Graphics[0x03] = 0x00;
  181. reg->Graphics[0x04] = 0x00;
  182. reg->Graphics[0x05] = 0x40;
  183. reg->Graphics[0x06] = 0x05; /* only map 64k VGA memory !!!! */
  184. reg->Graphics[0x07] = 0x0F;
  185. reg->Graphics[0x08] = 0xFF;
  186. reg->Attribute[0x00] = 0x00; /* standard colormap translation */
  187. reg->Attribute[0x01] = 0x01;
  188. reg->Attribute[0x02] = 0x02;
  189. reg->Attribute[0x03] = 0x03;
  190. reg->Attribute[0x04] = 0x04;
  191. reg->Attribute[0x05] = 0x05;
  192. reg->Attribute[0x06] = 0x06;
  193. reg->Attribute[0x07] = 0x07;
  194. reg->Attribute[0x08] = 0x08;
  195. reg->Attribute[0x09] = 0x09;
  196. reg->Attribute[0x0a] = 0x0A;
  197. reg->Attribute[0x0b] = 0x0B;
  198. reg->Attribute[0x0c] = 0x0C;
  199. reg->Attribute[0x0d] = 0x0D;
  200. reg->Attribute[0x0e] = 0x0E;
  201. reg->Attribute[0x0f] = 0x0F;
  202. reg->Attribute[0x10] = 0x41;
  203. reg->Attribute[0x11] = 0xFF;
  204. reg->Attribute[0x12] = 0x0F;
  205. reg->Attribute[0x13] = 0x00;
  206. reg->Attribute[0x14] = 0x00;
  207. }
  208. /* -------------------- Hardware specific routines ------------------------- */
  209. /*
  210. * Hardware Acceleration for SavageFB
  211. */
  212. /* Wait for fifo space */
  213. static void
  214. savage3D_waitfifo(struct savagefb_par *par, int space)
  215. {
  216. int slots = MAXFIFO - space;
  217. while ((savage_in32(0x48C00, par) & 0x0000ffff) > slots);
  218. }
  219. static void
  220. savage4_waitfifo(struct savagefb_par *par, int space)
  221. {
  222. int slots = MAXFIFO - space;
  223. while ((savage_in32(0x48C60, par) & 0x001fffff) > slots);
  224. }
  225. static void
  226. savage2000_waitfifo(struct savagefb_par *par, int space)
  227. {
  228. int slots = MAXFIFO - space;
  229. while ((savage_in32(0x48C60, par) & 0x0000ffff) > slots);
  230. }
  231. /* Wait for idle accelerator */
  232. static void
  233. savage3D_waitidle(struct savagefb_par *par)
  234. {
  235. while ((savage_in32(0x48C00, par) & 0x0008ffff) != 0x80000);
  236. }
  237. static void
  238. savage4_waitidle(struct savagefb_par *par)
  239. {
  240. while ((savage_in32(0x48C60, par) & 0x00a00000) != 0x00a00000);
  241. }
  242. static void
  243. savage2000_waitidle(struct savagefb_par *par)
  244. {
  245. while ((savage_in32(0x48C60, par) & 0x009fffff));
  246. }
  247. #ifdef CONFIG_FB_SAVAGE_ACCEL
  248. static void
  249. SavageSetup2DEngine(struct savagefb_par *par)
  250. {
  251. unsigned long GlobalBitmapDescriptor;
  252. GlobalBitmapDescriptor = 1 | 8 | BCI_BD_BW_DISABLE;
  253. BCI_BD_SET_BPP(GlobalBitmapDescriptor, par->depth);
  254. BCI_BD_SET_STRIDE(GlobalBitmapDescriptor, par->vwidth);
  255. switch(par->chip) {
  256. case S3_SAVAGE3D:
  257. case S3_SAVAGE_MX:
  258. /* Disable BCI */
  259. savage_out32(0x48C18, savage_in32(0x48C18, par) & 0x3FF0, par);
  260. /* Setup BCI command overflow buffer */
  261. savage_out32(0x48C14,
  262. (par->cob_offset >> 11) | (par->cob_index << 29),
  263. par);
  264. /* Program shadow status update. */
  265. savage_out32(0x48C10, 0x78207220, par);
  266. savage_out32(0x48C0C, 0, par);
  267. /* Enable BCI and command overflow buffer */
  268. savage_out32(0x48C18, savage_in32(0x48C18, par) | 0x0C, par);
  269. break;
  270. case S3_SAVAGE4:
  271. case S3_TWISTER:
  272. case S3_PROSAVAGE:
  273. case S3_PROSAVAGEDDR:
  274. case S3_SUPERSAVAGE:
  275. /* Disable BCI */
  276. savage_out32(0x48C18, savage_in32(0x48C18, par) & 0x3FF0, par);
  277. /* Program shadow status update */
  278. savage_out32(0x48C10, 0x00700040, par);
  279. savage_out32(0x48C0C, 0, par);
  280. /* Enable BCI without the COB */
  281. savage_out32(0x48C18, savage_in32(0x48C18, par) | 0x08, par);
  282. break;
  283. case S3_SAVAGE2000:
  284. /* Disable BCI */
  285. savage_out32(0x48C18, 0, par);
  286. /* Setup BCI command overflow buffer */
  287. savage_out32(0x48C18,
  288. (par->cob_offset >> 7) | (par->cob_index),
  289. par);
  290. /* Disable shadow status update */
  291. savage_out32(0x48A30, 0, par);
  292. /* Enable BCI and command overflow buffer */
  293. savage_out32(0x48C18, savage_in32(0x48C18, par) | 0x00280000,
  294. par);
  295. break;
  296. default:
  297. break;
  298. }
  299. /* Turn on 16-bit register access. */
  300. vga_out8(0x3d4, 0x31, par);
  301. vga_out8(0x3d5, 0x0c, par);
  302. /* Set stride to use GBD. */
  303. vga_out8(0x3d4, 0x50, par);
  304. vga_out8(0x3d5, vga_in8(0x3d5, par) | 0xC1, par);
  305. /* Enable 2D engine. */
  306. vga_out8(0x3d4, 0x40, par);
  307. vga_out8(0x3d5, 0x01, par);
  308. savage_out32(MONO_PAT_0, ~0, par);
  309. savage_out32(MONO_PAT_1, ~0, par);
  310. /* Setup plane masks */
  311. savage_out32(0x8128, ~0, par); /* enable all write planes */
  312. savage_out32(0x812C, ~0, par); /* enable all read planes */
  313. savage_out16(0x8134, 0x27, par);
  314. savage_out16(0x8136, 0x07, par);
  315. /* Now set the GBD */
  316. par->bci_ptr = 0;
  317. par->SavageWaitFifo(par, 4);
  318. BCI_SEND(BCI_CMD_SETREG | (1 << 16) | BCI_GBD1);
  319. BCI_SEND(0);
  320. BCI_SEND(BCI_CMD_SETREG | (1 << 16) | BCI_GBD2);
  321. BCI_SEND(GlobalBitmapDescriptor);
  322. /*
  323. * I don't know why, sending this twice fixes the initial black screen,
  324. * prevents X from crashing at least in Toshiba laptops with SavageIX.
  325. * --Tony
  326. */
  327. par->bci_ptr = 0;
  328. par->SavageWaitFifo(par, 4);
  329. BCI_SEND(BCI_CMD_SETREG | (1 << 16) | BCI_GBD1);
  330. BCI_SEND(0);
  331. BCI_SEND(BCI_CMD_SETREG | (1 << 16) | BCI_GBD2);
  332. BCI_SEND(GlobalBitmapDescriptor);
  333. }
  334. static void savagefb_set_clip(struct fb_info *info)
  335. {
  336. struct savagefb_par *par = info->par;
  337. int cmd;
  338. cmd = BCI_CMD_NOP | BCI_CMD_CLIP_NEW;
  339. par->bci_ptr = 0;
  340. par->SavageWaitFifo(par,3);
  341. BCI_SEND(cmd);
  342. BCI_SEND(BCI_CLIP_TL(0, 0));
  343. BCI_SEND(BCI_CLIP_BR(0xfff, 0xfff));
  344. }
  345. #else
  346. static void SavageSetup2DEngine(struct savagefb_par *par) {}
  347. #endif
  348. static void SavageCalcClock(long freq, int min_m, int min_n1, int max_n1,
  349. int min_n2, int max_n2, long freq_min,
  350. long freq_max, unsigned int *mdiv,
  351. unsigned int *ndiv, unsigned int *r)
  352. {
  353. long diff, best_diff;
  354. unsigned int m;
  355. unsigned char n1, n2, best_n1=16+2, best_n2=2, best_m=125+2;
  356. if (freq < freq_min / (1 << max_n2)) {
  357. printk(KERN_ERR "invalid frequency %ld Khz\n", freq);
  358. freq = freq_min / (1 << max_n2);
  359. }
  360. if (freq > freq_max / (1 << min_n2)) {
  361. printk(KERN_ERR "invalid frequency %ld Khz\n", freq);
  362. freq = freq_max / (1 << min_n2);
  363. }
  364. /* work out suitable timings */
  365. best_diff = freq;
  366. for (n2=min_n2; n2<=max_n2; n2++) {
  367. for (n1=min_n1+2; n1<=max_n1+2; n1++) {
  368. m = (freq * n1 * (1 << n2) + HALF_BASE_FREQ) /
  369. BASE_FREQ;
  370. if (m < min_m+2 || m > 127+2)
  371. continue;
  372. if ((m * BASE_FREQ >= freq_min * n1) &&
  373. (m * BASE_FREQ <= freq_max * n1)) {
  374. diff = freq * (1 << n2) * n1 - BASE_FREQ * m;
  375. if (diff < 0)
  376. diff = -diff;
  377. if (diff < best_diff) {
  378. best_diff = diff;
  379. best_m = m;
  380. best_n1 = n1;
  381. best_n2 = n2;
  382. }
  383. }
  384. }
  385. }
  386. *ndiv = best_n1 - 2;
  387. *r = best_n2;
  388. *mdiv = best_m - 2;
  389. }
  390. static int common_calc_clock(long freq, int min_m, int min_n1, int max_n1,
  391. int min_n2, int max_n2, long freq_min,
  392. long freq_max, unsigned char *mdiv,
  393. unsigned char *ndiv)
  394. {
  395. long diff, best_diff;
  396. unsigned int m;
  397. unsigned char n1, n2;
  398. unsigned char best_n1 = 16+2, best_n2 = 2, best_m = 125+2;
  399. best_diff = freq;
  400. for (n2 = min_n2; n2 <= max_n2; n2++) {
  401. for (n1 = min_n1+2; n1 <= max_n1+2; n1++) {
  402. m = (freq * n1 * (1 << n2) + HALF_BASE_FREQ) /
  403. BASE_FREQ;
  404. if (m < min_m + 2 || m > 127+2)
  405. continue;
  406. if ((m * BASE_FREQ >= freq_min * n1) &&
  407. (m * BASE_FREQ <= freq_max * n1)) {
  408. diff = freq * (1 << n2) * n1 - BASE_FREQ * m;
  409. if (diff < 0)
  410. diff = -diff;
  411. if (diff < best_diff) {
  412. best_diff = diff;
  413. best_m = m;
  414. best_n1 = n1;
  415. best_n2 = n2;
  416. }
  417. }
  418. }
  419. }
  420. if (max_n1 == 63)
  421. *ndiv = (best_n1 - 2) | (best_n2 << 6);
  422. else
  423. *ndiv = (best_n1 - 2) | (best_n2 << 5);
  424. *mdiv = best_m - 2;
  425. return 0;
  426. }
  427. #ifdef SAVAGEFB_DEBUG
  428. /* This function is used to debug, it prints out the contents of s3 regs */
  429. static void SavagePrintRegs(struct savagefb_par *par)
  430. {
  431. unsigned char i;
  432. int vgaCRIndex = 0x3d4;
  433. int vgaCRReg = 0x3d5;
  434. printk(KERN_DEBUG "SR x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE "
  435. "xF");
  436. for (i = 0; i < 0x70; i++) {
  437. if (!(i % 16))
  438. printk(KERN_DEBUG "\nSR%xx ", i >> 4);
  439. vga_out8(0x3c4, i, par);
  440. printk(KERN_DEBUG " %02x", vga_in8(0x3c5, par));
  441. }
  442. printk(KERN_DEBUG "\n\nCR x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC "
  443. "xD xE xF");
  444. for (i = 0; i < 0xB7; i++) {
  445. if (!(i % 16))
  446. printk(KERN_DEBUG "\nCR%xx ", i >> 4);
  447. vga_out8(vgaCRIndex, i, par);
  448. printk(KERN_DEBUG " %02x", vga_in8(vgaCRReg, par));
  449. }
  450. printk(KERN_DEBUG "\n\n");
  451. }
  452. #endif
  453. /* --------------------------------------------------------------------- */
  454. static void savage_get_default_par(struct savagefb_par *par, struct savage_reg *reg)
  455. {
  456. unsigned char cr3a, cr53, cr66;
  457. vga_out16(0x3d4, 0x4838, par);
  458. vga_out16(0x3d4, 0xa039, par);
  459. vga_out16(0x3c4, 0x0608, par);
  460. vga_out8(0x3d4, 0x66, par);
  461. cr66 = vga_in8(0x3d5, par);
  462. vga_out8(0x3d5, cr66 | 0x80, par);
  463. vga_out8(0x3d4, 0x3a, par);
  464. cr3a = vga_in8(0x3d5, par);
  465. vga_out8(0x3d5, cr3a | 0x80, par);
  466. vga_out8(0x3d4, 0x53, par);
  467. cr53 = vga_in8(0x3d5, par);
  468. vga_out8(0x3d5, cr53 & 0x7f, par);
  469. vga_out8(0x3d4, 0x66, par);
  470. vga_out8(0x3d5, cr66, par);
  471. vga_out8(0x3d4, 0x3a, par);
  472. vga_out8(0x3d5, cr3a, par);
  473. vga_out8(0x3d4, 0x66, par);
  474. vga_out8(0x3d5, cr66, par);
  475. vga_out8(0x3d4, 0x3a, par);
  476. vga_out8(0x3d5, cr3a, par);
  477. /* unlock extended seq regs */
  478. vga_out8(0x3c4, 0x08, par);
  479. reg->SR08 = vga_in8(0x3c5, par);
  480. vga_out8(0x3c5, 0x06, par);
  481. /* now save all the extended regs we need */
  482. vga_out8(0x3d4, 0x31, par);
  483. reg->CR31 = vga_in8(0x3d5, par);
  484. vga_out8(0x3d4, 0x32, par);
  485. reg->CR32 = vga_in8(0x3d5, par);
  486. vga_out8(0x3d4, 0x34, par);
  487. reg->CR34 = vga_in8(0x3d5, par);
  488. vga_out8(0x3d4, 0x36, par);
  489. reg->CR36 = vga_in8(0x3d5, par);
  490. vga_out8(0x3d4, 0x3a, par);
  491. reg->CR3A = vga_in8(0x3d5, par);
  492. vga_out8(0x3d4, 0x40, par);
  493. reg->CR40 = vga_in8(0x3d5, par);
  494. vga_out8(0x3d4, 0x42, par);
  495. reg->CR42 = vga_in8(0x3d5, par);
  496. vga_out8(0x3d4, 0x45, par);
  497. reg->CR45 = vga_in8(0x3d5, par);
  498. vga_out8(0x3d4, 0x50, par);
  499. reg->CR50 = vga_in8(0x3d5, par);
  500. vga_out8(0x3d4, 0x51, par);
  501. reg->CR51 = vga_in8(0x3d5, par);
  502. vga_out8(0x3d4, 0x53, par);
  503. reg->CR53 = vga_in8(0x3d5, par);
  504. vga_out8(0x3d4, 0x58, par);
  505. reg->CR58 = vga_in8(0x3d5, par);
  506. vga_out8(0x3d4, 0x60, par);
  507. reg->CR60 = vga_in8(0x3d5, par);
  508. vga_out8(0x3d4, 0x66, par);
  509. reg->CR66 = vga_in8(0x3d5, par);
  510. vga_out8(0x3d4, 0x67, par);
  511. reg->CR67 = vga_in8(0x3d5, par);
  512. vga_out8(0x3d4, 0x68, par);
  513. reg->CR68 = vga_in8(0x3d5, par);
  514. vga_out8(0x3d4, 0x69, par);
  515. reg->CR69 = vga_in8(0x3d5, par);
  516. vga_out8(0x3d4, 0x6f, par);
  517. reg->CR6F = vga_in8(0x3d5, par);
  518. vga_out8(0x3d4, 0x33, par);
  519. reg->CR33 = vga_in8(0x3d5, par);
  520. vga_out8(0x3d4, 0x86, par);
  521. reg->CR86 = vga_in8(0x3d5, par);
  522. vga_out8(0x3d4, 0x88, par);
  523. reg->CR88 = vga_in8(0x3d5, par);
  524. vga_out8(0x3d4, 0x90, par);
  525. reg->CR90 = vga_in8(0x3d5, par);
  526. vga_out8(0x3d4, 0x91, par);
  527. reg->CR91 = vga_in8(0x3d5, par);
  528. vga_out8(0x3d4, 0xb0, par);
  529. reg->CRB0 = vga_in8(0x3d5, par) | 0x80;
  530. /* extended mode timing regs */
  531. vga_out8(0x3d4, 0x3b, par);
  532. reg->CR3B = vga_in8(0x3d5, par);
  533. vga_out8(0x3d4, 0x3c, par);
  534. reg->CR3C = vga_in8(0x3d5, par);
  535. vga_out8(0x3d4, 0x43, par);
  536. reg->CR43 = vga_in8(0x3d5, par);
  537. vga_out8(0x3d4, 0x5d, par);
  538. reg->CR5D = vga_in8(0x3d5, par);
  539. vga_out8(0x3d4, 0x5e, par);
  540. reg->CR5E = vga_in8(0x3d5, par);
  541. vga_out8(0x3d4, 0x65, par);
  542. reg->CR65 = vga_in8(0x3d5, par);
  543. /* save seq extended regs for DCLK PLL programming */
  544. vga_out8(0x3c4, 0x0e, par);
  545. reg->SR0E = vga_in8(0x3c5, par);
  546. vga_out8(0x3c4, 0x0f, par);
  547. reg->SR0F = vga_in8(0x3c5, par);
  548. vga_out8(0x3c4, 0x10, par);
  549. reg->SR10 = vga_in8(0x3c5, par);
  550. vga_out8(0x3c4, 0x11, par);
  551. reg->SR11 = vga_in8(0x3c5, par);
  552. vga_out8(0x3c4, 0x12, par);
  553. reg->SR12 = vga_in8(0x3c5, par);
  554. vga_out8(0x3c4, 0x13, par);
  555. reg->SR13 = vga_in8(0x3c5, par);
  556. vga_out8(0x3c4, 0x29, par);
  557. reg->SR29 = vga_in8(0x3c5, par);
  558. vga_out8(0x3c4, 0x15, par);
  559. reg->SR15 = vga_in8(0x3c5, par);
  560. vga_out8(0x3c4, 0x30, par);
  561. reg->SR30 = vga_in8(0x3c5, par);
  562. vga_out8(0x3c4, 0x18, par);
  563. reg->SR18 = vga_in8(0x3c5, par);
  564. /* Save flat panel expansion registers. */
  565. if (par->chip == S3_SAVAGE_MX) {
  566. int i;
  567. for (i = 0; i < 8; i++) {
  568. vga_out8(0x3c4, 0x54+i, par);
  569. reg->SR54[i] = vga_in8(0x3c5, par);
  570. }
  571. }
  572. vga_out8(0x3d4, 0x66, par);
  573. cr66 = vga_in8(0x3d5, par);
  574. vga_out8(0x3d5, cr66 | 0x80, par);
  575. vga_out8(0x3d4, 0x3a, par);
  576. cr3a = vga_in8(0x3d5, par);
  577. vga_out8(0x3d5, cr3a | 0x80, par);
  578. /* now save MIU regs */
  579. if (par->chip != S3_SAVAGE_MX) {
  580. reg->MMPR0 = savage_in32(FIFO_CONTROL_REG, par);
  581. reg->MMPR1 = savage_in32(MIU_CONTROL_REG, par);
  582. reg->MMPR2 = savage_in32(STREAMS_TIMEOUT_REG, par);
  583. reg->MMPR3 = savage_in32(MISC_TIMEOUT_REG, par);
  584. }
  585. vga_out8(0x3d4, 0x3a, par);
  586. vga_out8(0x3d5, cr3a, par);
  587. vga_out8(0x3d4, 0x66, par);
  588. vga_out8(0x3d5, cr66, par);
  589. }
  590. static void savage_set_default_par(struct savagefb_par *par,
  591. struct savage_reg *reg)
  592. {
  593. unsigned char cr3a, cr53, cr66;
  594. vga_out16(0x3d4, 0x4838, par);
  595. vga_out16(0x3d4, 0xa039, par);
  596. vga_out16(0x3c4, 0x0608, par);
  597. vga_out8(0x3d4, 0x66, par);
  598. cr66 = vga_in8(0x3d5, par);
  599. vga_out8(0x3d5, cr66 | 0x80, par);
  600. vga_out8(0x3d4, 0x3a, par);
  601. cr3a = vga_in8(0x3d5, par);
  602. vga_out8(0x3d5, cr3a | 0x80, par);
  603. vga_out8(0x3d4, 0x53, par);
  604. cr53 = vga_in8(0x3d5, par);
  605. vga_out8(0x3d5, cr53 & 0x7f, par);
  606. vga_out8(0x3d4, 0x66, par);
  607. vga_out8(0x3d5, cr66, par);
  608. vga_out8(0x3d4, 0x3a, par);
  609. vga_out8(0x3d5, cr3a, par);
  610. vga_out8(0x3d4, 0x66, par);
  611. vga_out8(0x3d5, cr66, par);
  612. vga_out8(0x3d4, 0x3a, par);
  613. vga_out8(0x3d5, cr3a, par);
  614. /* unlock extended seq regs */
  615. vga_out8(0x3c4, 0x08, par);
  616. vga_out8(0x3c5, reg->SR08, par);
  617. vga_out8(0x3c5, 0x06, par);
  618. /* now restore all the extended regs we need */
  619. vga_out8(0x3d4, 0x31, par);
  620. vga_out8(0x3d5, reg->CR31, par);
  621. vga_out8(0x3d4, 0x32, par);
  622. vga_out8(0x3d5, reg->CR32, par);
  623. vga_out8(0x3d4, 0x34, par);
  624. vga_out8(0x3d5, reg->CR34, par);
  625. vga_out8(0x3d4, 0x36, par);
  626. vga_out8(0x3d5,reg->CR36, par);
  627. vga_out8(0x3d4, 0x3a, par);
  628. vga_out8(0x3d5, reg->CR3A, par);
  629. vga_out8(0x3d4, 0x40, par);
  630. vga_out8(0x3d5, reg->CR40, par);
  631. vga_out8(0x3d4, 0x42, par);
  632. vga_out8(0x3d5, reg->CR42, par);
  633. vga_out8(0x3d4, 0x45, par);
  634. vga_out8(0x3d5, reg->CR45, par);
  635. vga_out8(0x3d4, 0x50, par);
  636. vga_out8(0x3d5, reg->CR50, par);
  637. vga_out8(0x3d4, 0x51, par);
  638. vga_out8(0x3d5, reg->CR51, par);
  639. vga_out8(0x3d4, 0x53, par);
  640. vga_out8(0x3d5, reg->CR53, par);
  641. vga_out8(0x3d4, 0x58, par);
  642. vga_out8(0x3d5, reg->CR58, par);
  643. vga_out8(0x3d4, 0x60, par);
  644. vga_out8(0x3d5, reg->CR60, par);
  645. vga_out8(0x3d4, 0x66, par);
  646. vga_out8(0x3d5, reg->CR66, par);
  647. vga_out8(0x3d4, 0x67, par);
  648. vga_out8(0x3d5, reg->CR67, par);
  649. vga_out8(0x3d4, 0x68, par);
  650. vga_out8(0x3d5, reg->CR68, par);
  651. vga_out8(0x3d4, 0x69, par);
  652. vga_out8(0x3d5, reg->CR69, par);
  653. vga_out8(0x3d4, 0x6f, par);
  654. vga_out8(0x3d5, reg->CR6F, par);
  655. vga_out8(0x3d4, 0x33, par);
  656. vga_out8(0x3d5, reg->CR33, par);
  657. vga_out8(0x3d4, 0x86, par);
  658. vga_out8(0x3d5, reg->CR86, par);
  659. vga_out8(0x3d4, 0x88, par);
  660. vga_out8(0x3d5, reg->CR88, par);
  661. vga_out8(0x3d4, 0x90, par);
  662. vga_out8(0x3d5, reg->CR90, par);
  663. vga_out8(0x3d4, 0x91, par);
  664. vga_out8(0x3d5, reg->CR91, par);
  665. vga_out8(0x3d4, 0xb0, par);
  666. vga_out8(0x3d5, reg->CRB0, par);
  667. /* extended mode timing regs */
  668. vga_out8(0x3d4, 0x3b, par);
  669. vga_out8(0x3d5, reg->CR3B, par);
  670. vga_out8(0x3d4, 0x3c, par);
  671. vga_out8(0x3d5, reg->CR3C, par);
  672. vga_out8(0x3d4, 0x43, par);
  673. vga_out8(0x3d5, reg->CR43, par);
  674. vga_out8(0x3d4, 0x5d, par);
  675. vga_out8(0x3d5, reg->CR5D, par);
  676. vga_out8(0x3d4, 0x5e, par);
  677. vga_out8(0x3d5, reg->CR5E, par);
  678. vga_out8(0x3d4, 0x65, par);
  679. vga_out8(0x3d5, reg->CR65, par);
  680. /* save seq extended regs for DCLK PLL programming */
  681. vga_out8(0x3c4, 0x0e, par);
  682. vga_out8(0x3c5, reg->SR0E, par);
  683. vga_out8(0x3c4, 0x0f, par);
  684. vga_out8(0x3c5, reg->SR0F, par);
  685. vga_out8(0x3c4, 0x10, par);
  686. vga_out8(0x3c5, reg->SR10, par);
  687. vga_out8(0x3c4, 0x11, par);
  688. vga_out8(0x3c5, reg->SR11, par);
  689. vga_out8(0x3c4, 0x12, par);
  690. vga_out8(0x3c5, reg->SR12, par);
  691. vga_out8(0x3c4, 0x13, par);
  692. vga_out8(0x3c5, reg->SR13, par);
  693. vga_out8(0x3c4, 0x29, par);
  694. vga_out8(0x3c5, reg->SR29, par);
  695. vga_out8(0x3c4, 0x15, par);
  696. vga_out8(0x3c5, reg->SR15, par);
  697. vga_out8(0x3c4, 0x30, par);
  698. vga_out8(0x3c5, reg->SR30, par);
  699. vga_out8(0x3c4, 0x18, par);
  700. vga_out8(0x3c5, reg->SR18, par);
  701. /* Save flat panel expansion registers. */
  702. if (par->chip == S3_SAVAGE_MX) {
  703. int i;
  704. for (i = 0; i < 8; i++) {
  705. vga_out8(0x3c4, 0x54+i, par);
  706. vga_out8(0x3c5, reg->SR54[i], par);
  707. }
  708. }
  709. vga_out8(0x3d4, 0x66, par);
  710. cr66 = vga_in8(0x3d5, par);
  711. vga_out8(0x3d5, cr66 | 0x80, par);
  712. vga_out8(0x3d4, 0x3a, par);
  713. cr3a = vga_in8(0x3d5, par);
  714. vga_out8(0x3d5, cr3a | 0x80, par);
  715. /* now save MIU regs */
  716. if (par->chip != S3_SAVAGE_MX) {
  717. savage_out32(FIFO_CONTROL_REG, reg->MMPR0, par);
  718. savage_out32(MIU_CONTROL_REG, reg->MMPR1, par);
  719. savage_out32(STREAMS_TIMEOUT_REG, reg->MMPR2, par);
  720. savage_out32(MISC_TIMEOUT_REG, reg->MMPR3, par);
  721. }
  722. vga_out8(0x3d4, 0x3a, par);
  723. vga_out8(0x3d5, cr3a, par);
  724. vga_out8(0x3d4, 0x66, par);
  725. vga_out8(0x3d5, cr66, par);
  726. }
  727. static void savage_update_var(struct fb_var_screeninfo *var,
  728. const struct fb_videomode *modedb)
  729. {
  730. var->xres = var->xres_virtual = modedb->xres;
  731. var->yres = modedb->yres;
  732. if (var->yres_virtual < var->yres)
  733. var->yres_virtual = var->yres;
  734. var->xoffset = var->yoffset = 0;
  735. var->pixclock = modedb->pixclock;
  736. var->left_margin = modedb->left_margin;
  737. var->right_margin = modedb->right_margin;
  738. var->upper_margin = modedb->upper_margin;
  739. var->lower_margin = modedb->lower_margin;
  740. var->hsync_len = modedb->hsync_len;
  741. var->vsync_len = modedb->vsync_len;
  742. var->sync = modedb->sync;
  743. var->vmode = modedb->vmode;
  744. }
  745. static int savagefb_check_var(struct fb_var_screeninfo *var,
  746. struct fb_info *info)
  747. {
  748. struct savagefb_par *par = info->par;
  749. int memlen, vramlen, mode_valid = 0;
  750. DBG("savagefb_check_var");
  751. var->transp.offset = 0;
  752. var->transp.length = 0;
  753. switch (var->bits_per_pixel) {
  754. case 8:
  755. var->red.offset = var->green.offset =
  756. var->blue.offset = 0;
  757. var->red.length = var->green.length =
  758. var->blue.length = var->bits_per_pixel;
  759. break;
  760. case 16:
  761. var->red.offset = 11;
  762. var->red.length = 5;
  763. var->green.offset = 5;
  764. var->green.length = 6;
  765. var->blue.offset = 0;
  766. var->blue.length = 5;
  767. break;
  768. case 32:
  769. var->transp.offset = 24;
  770. var->transp.length = 8;
  771. var->red.offset = 16;
  772. var->red.length = 8;
  773. var->green.offset = 8;
  774. var->green.length = 8;
  775. var->blue.offset = 0;
  776. var->blue.length = 8;
  777. break;
  778. default:
  779. return -EINVAL;
  780. }
  781. if (!info->monspecs.hfmax || !info->monspecs.vfmax ||
  782. !info->monspecs.dclkmax || !fb_validate_mode(var, info))
  783. mode_valid = 1;
  784. /* calculate modeline if supported by monitor */
  785. if (!mode_valid && info->monspecs.gtf) {
  786. if (!fb_get_mode(FB_MAXTIMINGS, 0, var, info))
  787. mode_valid = 1;
  788. }
  789. if (!mode_valid) {
  790. const struct fb_videomode *mode;
  791. mode = fb_find_best_mode(var, &info->modelist);
  792. if (mode) {
  793. savage_update_var(var, mode);
  794. mode_valid = 1;
  795. }
  796. }
  797. if (!mode_valid && info->monspecs.modedb_len)
  798. return -EINVAL;
  799. /* Is the mode larger than the LCD panel? */
  800. if (par->SavagePanelWidth &&
  801. (var->xres > par->SavagePanelWidth ||
  802. var->yres > par->SavagePanelHeight)) {
  803. printk(KERN_INFO "Mode (%dx%d) larger than the LCD panel "
  804. "(%dx%d)\n", var->xres, var->yres,
  805. par->SavagePanelWidth,
  806. par->SavagePanelHeight);
  807. return -1;
  808. }
  809. if (var->yres_virtual < var->yres)
  810. var->yres_virtual = var->yres;
  811. if (var->xres_virtual < var->xres)
  812. var->xres_virtual = var->xres;
  813. vramlen = info->fix.smem_len;
  814. memlen = var->xres_virtual * var->bits_per_pixel *
  815. var->yres_virtual / 8;
  816. if (memlen > vramlen) {
  817. var->yres_virtual = vramlen * 8 /
  818. (var->xres_virtual * var->bits_per_pixel);
  819. memlen = var->xres_virtual * var->bits_per_pixel *
  820. var->yres_virtual / 8;
  821. }
  822. /* we must round yres/xres down, we already rounded y/xres_virtual up
  823. if it was possible. We should return -EINVAL, but I disagree */
  824. if (var->yres_virtual < var->yres)
  825. var->yres = var->yres_virtual;
  826. if (var->xres_virtual < var->xres)
  827. var->xres = var->xres_virtual;
  828. if (var->xoffset + var->xres > var->xres_virtual)
  829. var->xoffset = var->xres_virtual - var->xres;
  830. if (var->yoffset + var->yres > var->yres_virtual)
  831. var->yoffset = var->yres_virtual - var->yres;
  832. return 0;
  833. }
  834. static int savagefb_decode_var(struct fb_var_screeninfo *var,
  835. struct savagefb_par *par,
  836. struct savage_reg *reg)
  837. {
  838. struct xtimings timings;
  839. int width, dclk, i, j; /*, refresh; */
  840. unsigned int m, n, r;
  841. unsigned char tmp = 0;
  842. unsigned int pixclock = var->pixclock;
  843. DBG("savagefb_decode_var");
  844. memset(&timings, 0, sizeof(timings));
  845. if (!pixclock) pixclock = 10000; /* 10ns = 100MHz */
  846. timings.Clock = 1000000000 / pixclock;
  847. if (timings.Clock < 1) timings.Clock = 1;
  848. timings.dblscan = var->vmode & FB_VMODE_DOUBLE;
  849. timings.interlaced = var->vmode & FB_VMODE_INTERLACED;
  850. timings.HDisplay = var->xres;
  851. timings.HSyncStart = timings.HDisplay + var->right_margin;
  852. timings.HSyncEnd = timings.HSyncStart + var->hsync_len;
  853. timings.HTotal = timings.HSyncEnd + var->left_margin;
  854. timings.VDisplay = var->yres;
  855. timings.VSyncStart = timings.VDisplay + var->lower_margin;
  856. timings.VSyncEnd = timings.VSyncStart + var->vsync_len;
  857. timings.VTotal = timings.VSyncEnd + var->upper_margin;
  858. timings.sync = var->sync;
  859. par->depth = var->bits_per_pixel;
  860. par->vwidth = var->xres_virtual;
  861. if (var->bits_per_pixel == 16 && par->chip == S3_SAVAGE3D) {
  862. timings.HDisplay *= 2;
  863. timings.HSyncStart *= 2;
  864. timings.HSyncEnd *= 2;
  865. timings.HTotal *= 2;
  866. }
  867. /*
  868. * This will allocate the datastructure and initialize all of the
  869. * generic VGA registers.
  870. */
  871. vgaHWInit(var, par, &timings, reg);
  872. /* We need to set CR67 whether or not we use the BIOS. */
  873. dclk = timings.Clock;
  874. reg->CR67 = 0x00;
  875. switch(var->bits_per_pixel) {
  876. case 8:
  877. if ((par->chip == S3_SAVAGE2000) && (dclk >= 230000))
  878. reg->CR67 = 0x10; /* 8bpp, 2 pixels/clock */
  879. else
  880. reg->CR67 = 0x00; /* 8bpp, 1 pixel/clock */
  881. break;
  882. case 15:
  883. if (S3_SAVAGE_MOBILE_SERIES(par->chip) ||
  884. ((par->chip == S3_SAVAGE2000) && (dclk >= 230000)))
  885. reg->CR67 = 0x30; /* 15bpp, 2 pixel/clock */
  886. else
  887. reg->CR67 = 0x20; /* 15bpp, 1 pixels/clock */
  888. break;
  889. case 16:
  890. if (S3_SAVAGE_MOBILE_SERIES(par->chip) ||
  891. ((par->chip == S3_SAVAGE2000) && (dclk >= 230000)))
  892. reg->CR67 = 0x50; /* 16bpp, 2 pixel/clock */
  893. else
  894. reg->CR67 = 0x40; /* 16bpp, 1 pixels/clock */
  895. break;
  896. case 24:
  897. reg->CR67 = 0x70;
  898. break;
  899. case 32:
  900. reg->CR67 = 0xd0;
  901. break;
  902. }
  903. /*
  904. * Either BIOS use is disabled, or we failed to find a suitable
  905. * match. Fall back to traditional register-crunching.
  906. */
  907. vga_out8(0x3d4, 0x3a, par);
  908. tmp = vga_in8(0x3d5, par);
  909. if (1 /*FIXME:psav->pci_burst*/)
  910. reg->CR3A = (tmp & 0x7f) | 0x15;
  911. else
  912. reg->CR3A = tmp | 0x95;
  913. reg->CR53 = 0x00;
  914. reg->CR31 = 0x8c;
  915. reg->CR66 = 0x89;
  916. vga_out8(0x3d4, 0x58, par);
  917. reg->CR58 = vga_in8(0x3d5, par) & 0x80;
  918. reg->CR58 |= 0x13;
  919. reg->SR15 = 0x03 | 0x80;
  920. reg->SR18 = 0x00;
  921. reg->CR43 = reg->CR45 = reg->CR65 = 0x00;
  922. vga_out8(0x3d4, 0x40, par);
  923. reg->CR40 = vga_in8(0x3d5, par) & ~0x01;
  924. reg->MMPR0 = 0x010400;
  925. reg->MMPR1 = 0x00;
  926. reg->MMPR2 = 0x0808;
  927. reg->MMPR3 = 0x08080810;
  928. SavageCalcClock(dclk, 1, 1, 127, 0, 4, 180000, 360000, &m, &n, &r);
  929. /* m = 107; n = 4; r = 2; */
  930. if (par->MCLK <= 0) {
  931. reg->SR10 = 255;
  932. reg->SR11 = 255;
  933. } else {
  934. common_calc_clock(par->MCLK, 1, 1, 31, 0, 3, 135000, 270000,
  935. &reg->SR11, &reg->SR10);
  936. /* reg->SR10 = 80; // MCLK == 286000 */
  937. /* reg->SR11 = 125; */
  938. }
  939. reg->SR12 = (r << 6) | (n & 0x3f);
  940. reg->SR13 = m & 0xff;
  941. reg->SR29 = (r & 4) | (m & 0x100) >> 5 | (n & 0x40) >> 2;
  942. if (var->bits_per_pixel < 24)
  943. reg->MMPR0 -= 0x8000;
  944. else
  945. reg->MMPR0 -= 0x4000;
  946. if (timings.interlaced)
  947. reg->CR42 = 0x20;
  948. else
  949. reg->CR42 = 0x00;
  950. reg->CR34 = 0x10; /* display fifo */
  951. i = ((((timings.HTotal >> 3) - 5) & 0x100) >> 8) |
  952. ((((timings.HDisplay >> 3) - 1) & 0x100) >> 7) |
  953. ((((timings.HSyncStart >> 3) - 1) & 0x100) >> 6) |
  954. ((timings.HSyncStart & 0x800) >> 7);
  955. if ((timings.HSyncEnd >> 3) - (timings.HSyncStart >> 3) > 64)
  956. i |= 0x08;
  957. if ((timings.HSyncEnd >> 3) - (timings.HSyncStart >> 3) > 32)
  958. i |= 0x20;
  959. j = (reg->CRTC[0] + ((i & 0x01) << 8) +
  960. reg->CRTC[4] + ((i & 0x10) << 4) + 1) / 2;
  961. if (j - (reg->CRTC[4] + ((i & 0x10) << 4)) < 4) {
  962. if (reg->CRTC[4] + ((i & 0x10) << 4) + 4 <=
  963. reg->CRTC[0] + ((i & 0x01) << 8))
  964. j = reg->CRTC[4] + ((i & 0x10) << 4) + 4;
  965. else
  966. j = reg->CRTC[0] + ((i & 0x01) << 8) + 1;
  967. }
  968. reg->CR3B = j & 0xff;
  969. i |= (j & 0x100) >> 2;
  970. reg->CR3C = (reg->CRTC[0] + ((i & 0x01) << 8)) / 2;
  971. reg->CR5D = i;
  972. reg->CR5E = (((timings.VTotal - 2) & 0x400) >> 10) |
  973. (((timings.VDisplay - 1) & 0x400) >> 9) |
  974. (((timings.VSyncStart) & 0x400) >> 8) |
  975. (((timings.VSyncStart) & 0x400) >> 6) | 0x40;
  976. width = (var->xres_virtual * ((var->bits_per_pixel+7) / 8)) >> 3;
  977. reg->CR91 = reg->CRTC[19] = 0xff & width;
  978. reg->CR51 = (0x300 & width) >> 4;
  979. reg->CR90 = 0x80 | (width >> 8);
  980. reg->MiscOutReg |= 0x0c;
  981. /* Set frame buffer description. */
  982. if (var->bits_per_pixel <= 8)
  983. reg->CR50 = 0;
  984. else if (var->bits_per_pixel <= 16)
  985. reg->CR50 = 0x10;
  986. else
  987. reg->CR50 = 0x30;
  988. if (var->xres_virtual <= 640)
  989. reg->CR50 |= 0x40;
  990. else if (var->xres_virtual == 800)
  991. reg->CR50 |= 0x80;
  992. else if (var->xres_virtual == 1024)
  993. reg->CR50 |= 0x00;
  994. else if (var->xres_virtual == 1152)
  995. reg->CR50 |= 0x01;
  996. else if (var->xres_virtual == 1280)
  997. reg->CR50 |= 0xc0;
  998. else if (var->xres_virtual == 1600)
  999. reg->CR50 |= 0x81;
  1000. else
  1001. reg->CR50 |= 0xc1; /* Use GBD */
  1002. if (par->chip == S3_SAVAGE2000)
  1003. reg->CR33 = 0x08;
  1004. else
  1005. reg->CR33 = 0x20;
  1006. reg->CRTC[0x17] = 0xeb;
  1007. reg->CR67 |= 1;
  1008. vga_out8(0x3d4, 0x36, par);
  1009. reg->CR36 = vga_in8(0x3d5, par);
  1010. vga_out8(0x3d4, 0x68, par);
  1011. reg->CR68 = vga_in8(0x3d5, par);
  1012. reg->CR69 = 0;
  1013. vga_out8(0x3d4, 0x6f, par);
  1014. reg->CR6F = vga_in8(0x3d5, par);
  1015. vga_out8(0x3d4, 0x86, par);
  1016. reg->CR86 = vga_in8(0x3d5, par);
  1017. vga_out8(0x3d4, 0x88, par);
  1018. reg->CR88 = vga_in8(0x3d5, par) | 0x08;
  1019. vga_out8(0x3d4, 0xb0, par);
  1020. reg->CRB0 = vga_in8(0x3d5, par) | 0x80;
  1021. return 0;
  1022. }
  1023. /* --------------------------------------------------------------------- */
  1024. /*
  1025. * Set a single color register. Return != 0 for invalid regno.
  1026. */
  1027. static int savagefb_setcolreg(unsigned regno,
  1028. unsigned red,
  1029. unsigned green,
  1030. unsigned blue,
  1031. unsigned transp,
  1032. struct fb_info *info)
  1033. {
  1034. struct savagefb_par *par = info->par;
  1035. if (regno >= NR_PALETTE)
  1036. return -EINVAL;
  1037. par->palette[regno].red = red;
  1038. par->palette[regno].green = green;
  1039. par->palette[regno].blue = blue;
  1040. par->palette[regno].transp = transp;
  1041. switch (info->var.bits_per_pixel) {
  1042. case 8:
  1043. vga_out8(0x3c8, regno, par);
  1044. vga_out8(0x3c9, red >> 10, par);
  1045. vga_out8(0x3c9, green >> 10, par);
  1046. vga_out8(0x3c9, blue >> 10, par);
  1047. break;
  1048. case 16:
  1049. if (regno < 16)
  1050. ((u32 *)info->pseudo_palette)[regno] =
  1051. ((red & 0xf800) ) |
  1052. ((green & 0xfc00) >> 5) |
  1053. ((blue & 0xf800) >> 11);
  1054. break;
  1055. case 24:
  1056. if (regno < 16)
  1057. ((u32 *)info->pseudo_palette)[regno] =
  1058. ((red & 0xff00) << 8) |
  1059. ((green & 0xff00) ) |
  1060. ((blue & 0xff00) >> 8);
  1061. break;
  1062. case 32:
  1063. if (regno < 16)
  1064. ((u32 *)info->pseudo_palette)[regno] =
  1065. ((transp & 0xff00) << 16) |
  1066. ((red & 0xff00) << 8) |
  1067. ((green & 0xff00) ) |
  1068. ((blue & 0xff00) >> 8);
  1069. break;
  1070. default:
  1071. return 1;
  1072. }
  1073. return 0;
  1074. }
  1075. static void savagefb_set_par_int(struct savagefb_par *par, struct savage_reg *reg)
  1076. {
  1077. unsigned char tmp, cr3a, cr66, cr67;
  1078. DBG("savagefb_set_par_int");
  1079. par->SavageWaitIdle(par);
  1080. vga_out8(0x3c2, 0x23, par);
  1081. vga_out16(0x3d4, 0x4838, par);
  1082. vga_out16(0x3d4, 0xa539, par);
  1083. vga_out16(0x3c4, 0x0608, par);
  1084. vgaHWProtect(par, 1);
  1085. /*
  1086. * Some Savage/MX and /IX systems go nuts when trying to exit the
  1087. * server after WindowMaker has displayed a gradient background. I
  1088. * haven't been able to find what causes it, but a non-destructive
  1089. * switch to mode 3 here seems to eliminate the issue.
  1090. */
  1091. VerticalRetraceWait(par);
  1092. vga_out8(0x3d4, 0x67, par);
  1093. cr67 = vga_in8(0x3d5, par);
  1094. vga_out8(0x3d5, cr67/*par->CR67*/ & ~0x0c, par); /* no STREAMS yet */
  1095. vga_out8(0x3d4, 0x23, par);
  1096. vga_out8(0x3d5, 0x00, par);
  1097. vga_out8(0x3d4, 0x26, par);
  1098. vga_out8(0x3d5, 0x00, par);
  1099. /* restore extended regs */
  1100. vga_out8(0x3d4, 0x66, par);
  1101. vga_out8(0x3d5, reg->CR66, par);
  1102. vga_out8(0x3d4, 0x3a, par);
  1103. vga_out8(0x3d5, reg->CR3A, par);
  1104. vga_out8(0x3d4, 0x31, par);
  1105. vga_out8(0x3d5, reg->CR31, par);
  1106. vga_out8(0x3d4, 0x32, par);
  1107. vga_out8(0x3d5, reg->CR32, par);
  1108. vga_out8(0x3d4, 0x58, par);
  1109. vga_out8(0x3d5, reg->CR58, par);
  1110. vga_out8(0x3d4, 0x53, par);
  1111. vga_out8(0x3d5, reg->CR53 & 0x7f, par);
  1112. vga_out16(0x3c4, 0x0608, par);
  1113. /* Restore DCLK registers. */
  1114. vga_out8(0x3c4, 0x0e, par);
  1115. vga_out8(0x3c5, reg->SR0E, par);
  1116. vga_out8(0x3c4, 0x0f, par);
  1117. vga_out8(0x3c5, reg->SR0F, par);
  1118. vga_out8(0x3c4, 0x29, par);
  1119. vga_out8(0x3c5, reg->SR29, par);
  1120. vga_out8(0x3c4, 0x15, par);
  1121. vga_out8(0x3c5, reg->SR15, par);
  1122. /* Restore flat panel expansion registers. */
  1123. if (par->chip == S3_SAVAGE_MX) {
  1124. int i;
  1125. for (i = 0; i < 8; i++) {
  1126. vga_out8(0x3c4, 0x54+i, par);
  1127. vga_out8(0x3c5, reg->SR54[i], par);
  1128. }
  1129. }
  1130. vgaHWRestore (par, reg);
  1131. /* extended mode timing registers */
  1132. vga_out8(0x3d4, 0x53, par);
  1133. vga_out8(0x3d5, reg->CR53, par);
  1134. vga_out8(0x3d4, 0x5d, par);
  1135. vga_out8(0x3d5, reg->CR5D, par);
  1136. vga_out8(0x3d4, 0x5e, par);
  1137. vga_out8(0x3d5, reg->CR5E, par);
  1138. vga_out8(0x3d4, 0x3b, par);
  1139. vga_out8(0x3d5, reg->CR3B, par);
  1140. vga_out8(0x3d4, 0x3c, par);
  1141. vga_out8(0x3d5, reg->CR3C, par);
  1142. vga_out8(0x3d4, 0x43, par);
  1143. vga_out8(0x3d5, reg->CR43, par);
  1144. vga_out8(0x3d4, 0x65, par);
  1145. vga_out8(0x3d5, reg->CR65, par);
  1146. /* restore the desired video mode with cr67 */
  1147. vga_out8(0x3d4, 0x67, par);
  1148. /* following part not present in X11 driver */
  1149. cr67 = vga_in8(0x3d5, par) & 0xf;
  1150. vga_out8(0x3d5, 0x50 | cr67, par);
  1151. mdelay(10);
  1152. vga_out8(0x3d4, 0x67, par);
  1153. /* end of part */
  1154. vga_out8(0x3d5, reg->CR67 & ~0x0c, par);
  1155. /* other mode timing and extended regs */
  1156. vga_out8(0x3d4, 0x34, par);
  1157. vga_out8(0x3d5, reg->CR34, par);
  1158. vga_out8(0x3d4, 0x40, par);
  1159. vga_out8(0x3d5, reg->CR40, par);
  1160. vga_out8(0x3d4, 0x42, par);
  1161. vga_out8(0x3d5, reg->CR42, par);
  1162. vga_out8(0x3d4, 0x45, par);
  1163. vga_out8(0x3d5, reg->CR45, par);
  1164. vga_out8(0x3d4, 0x50, par);
  1165. vga_out8(0x3d5, reg->CR50, par);
  1166. vga_out8(0x3d4, 0x51, par);
  1167. vga_out8(0x3d5, reg->CR51, par);
  1168. /* memory timings */
  1169. vga_out8(0x3d4, 0x36, par);
  1170. vga_out8(0x3d5, reg->CR36, par);
  1171. vga_out8(0x3d4, 0x60, par);
  1172. vga_out8(0x3d5, reg->CR60, par);
  1173. vga_out8(0x3d4, 0x68, par);
  1174. vga_out8(0x3d5, reg->CR68, par);
  1175. vga_out8(0x3d4, 0x69, par);
  1176. vga_out8(0x3d5, reg->CR69, par);
  1177. vga_out8(0x3d4, 0x6f, par);
  1178. vga_out8(0x3d5, reg->CR6F, par);
  1179. vga_out8(0x3d4, 0x33, par);
  1180. vga_out8(0x3d5, reg->CR33, par);
  1181. vga_out8(0x3d4, 0x86, par);
  1182. vga_out8(0x3d5, reg->CR86, par);
  1183. vga_out8(0x3d4, 0x88, par);
  1184. vga_out8(0x3d5, reg->CR88, par);
  1185. vga_out8(0x3d4, 0x90, par);
  1186. vga_out8(0x3d5, reg->CR90, par);
  1187. vga_out8(0x3d4, 0x91, par);
  1188. vga_out8(0x3d5, reg->CR91, par);
  1189. if (par->chip == S3_SAVAGE4) {
  1190. vga_out8(0x3d4, 0xb0, par);
  1191. vga_out8(0x3d5, reg->CRB0, par);
  1192. }
  1193. vga_out8(0x3d4, 0x32, par);
  1194. vga_out8(0x3d5, reg->CR32, par);
  1195. /* unlock extended seq regs */
  1196. vga_out8(0x3c4, 0x08, par);
  1197. vga_out8(0x3c5, 0x06, par);
  1198. /* Restore extended sequencer regs for MCLK. SR10 == 255 indicates
  1199. * that we should leave the default SR10 and SR11 values there.
  1200. */
  1201. if (reg->SR10 != 255) {
  1202. vga_out8(0x3c4, 0x10, par);
  1203. vga_out8(0x3c5, reg->SR10, par);
  1204. vga_out8(0x3c4, 0x11, par);
  1205. vga_out8(0x3c5, reg->SR11, par);
  1206. }
  1207. /* restore extended seq regs for dclk */
  1208. vga_out8(0x3c4, 0x0e, par);
  1209. vga_out8(0x3c5, reg->SR0E, par);
  1210. vga_out8(0x3c4, 0x0f, par);
  1211. vga_out8(0x3c5, reg->SR0F, par);
  1212. vga_out8(0x3c4, 0x12, par);
  1213. vga_out8(0x3c5, reg->SR12, par);
  1214. vga_out8(0x3c4, 0x13, par);
  1215. vga_out8(0x3c5, reg->SR13, par);
  1216. vga_out8(0x3c4, 0x29, par);
  1217. vga_out8(0x3c5, reg->SR29, par);
  1218. vga_out8(0x3c4, 0x18, par);
  1219. vga_out8(0x3c5, reg->SR18, par);
  1220. /* load new m, n pll values for dclk & mclk */
  1221. vga_out8(0x3c4, 0x15, par);
  1222. tmp = vga_in8(0x3c5, par) & ~0x21;
  1223. vga_out8(0x3c5, tmp | 0x03, par);
  1224. vga_out8(0x3c5, tmp | 0x23, par);
  1225. vga_out8(0x3c5, tmp | 0x03, par);
  1226. vga_out8(0x3c5, reg->SR15, par);
  1227. udelay(100);
  1228. vga_out8(0x3c4, 0x30, par);
  1229. vga_out8(0x3c5, reg->SR30, par);
  1230. vga_out8(0x3c4, 0x08, par);
  1231. vga_out8(0x3c5, reg->SR08, par);
  1232. /* now write out cr67 in full, possibly starting STREAMS */
  1233. VerticalRetraceWait(par);
  1234. vga_out8(0x3d4, 0x67, par);
  1235. vga_out8(0x3d5, reg->CR67, par);
  1236. vga_out8(0x3d4, 0x66, par);
  1237. cr66 = vga_in8(0x3d5, par);
  1238. vga_out8(0x3d5, cr66 | 0x80, par);
  1239. vga_out8(0x3d4, 0x3a, par);
  1240. cr3a = vga_in8(0x3d5, par);
  1241. vga_out8(0x3d5, cr3a | 0x80, par);
  1242. if (par->chip != S3_SAVAGE_MX) {
  1243. VerticalRetraceWait(par);
  1244. savage_out32(FIFO_CONTROL_REG, reg->MMPR0, par);
  1245. par->SavageWaitIdle(par);
  1246. savage_out32(MIU_CONTROL_REG, reg->MMPR1, par);
  1247. par->SavageWaitIdle(par);
  1248. savage_out32(STREAMS_TIMEOUT_REG, reg->MMPR2, par);
  1249. par->SavageWaitIdle(par);
  1250. savage_out32(MISC_TIMEOUT_REG, reg->MMPR3, par);
  1251. }
  1252. vga_out8(0x3d4, 0x66, par);
  1253. vga_out8(0x3d5, cr66, par);
  1254. vga_out8(0x3d4, 0x3a, par);
  1255. vga_out8(0x3d5, cr3a, par);
  1256. SavageSetup2DEngine(par);
  1257. vgaHWProtect(par, 0);
  1258. }
  1259. static void savagefb_update_start(struct savagefb_par *par, int base)
  1260. {
  1261. /* program the start address registers */
  1262. vga_out16(0x3d4, (base & 0x00ff00) | 0x0c, par);
  1263. vga_out16(0x3d4, ((base & 0x00ff) << 8) | 0x0d, par);
  1264. vga_out8(0x3d4, 0x69, par);
  1265. vga_out8(0x3d5, (base & 0x7f0000) >> 16, par);
  1266. }
  1267. static void savagefb_set_fix(struct fb_info *info)
  1268. {
  1269. info->fix.line_length = info->var.xres_virtual *
  1270. info->var.bits_per_pixel / 8;
  1271. if (info->var.bits_per_pixel == 8) {
  1272. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  1273. info->fix.xpanstep = 4;
  1274. } else {
  1275. info->fix.visual = FB_VISUAL_TRUECOLOR;
  1276. info->fix.xpanstep = 2;
  1277. }
  1278. }
  1279. static int savagefb_set_par(struct fb_info *info)
  1280. {
  1281. struct savagefb_par *par = info->par;
  1282. struct fb_var_screeninfo *var = &info->var;
  1283. int err;
  1284. DBG("savagefb_set_par");
  1285. err = savagefb_decode_var(var, par, &par->state);
  1286. if (err)
  1287. return err;
  1288. if (par->dacSpeedBpp <= 0) {
  1289. if (var->bits_per_pixel > 24)
  1290. par->dacSpeedBpp = par->clock[3];
  1291. else if (var->bits_per_pixel >= 24)
  1292. par->dacSpeedBpp = par->clock[2];
  1293. else if ((var->bits_per_pixel > 8) && (var->bits_per_pixel < 24))
  1294. par->dacSpeedBpp = par->clock[1];
  1295. else if (var->bits_per_pixel <= 8)
  1296. par->dacSpeedBpp = par->clock[0];
  1297. }
  1298. /* Set ramdac limits */
  1299. par->maxClock = par->dacSpeedBpp;
  1300. par->minClock = 10000;
  1301. savagefb_set_par_int(par, &par->state);
  1302. fb_set_cmap(&info->cmap, info);
  1303. savagefb_set_fix(info);
  1304. savagefb_set_clip(info);
  1305. SavagePrintRegs(par);
  1306. return 0;
  1307. }
  1308. /*
  1309. * Pan or Wrap the Display
  1310. */
  1311. static int savagefb_pan_display(struct fb_var_screeninfo *var,
  1312. struct fb_info *info)
  1313. {
  1314. struct savagefb_par *par = info->par;
  1315. int base;
  1316. base = (var->yoffset * info->fix.line_length
  1317. + (var->xoffset & ~1) * ((info->var.bits_per_pixel+7) / 8)) >> 2;
  1318. savagefb_update_start(par, base);
  1319. return 0;
  1320. }
  1321. static int savagefb_blank(int blank, struct fb_info *info)
  1322. {
  1323. struct savagefb_par *par = info->par;
  1324. u8 sr8 = 0, srd = 0;
  1325. if (par->display_type == DISP_CRT) {
  1326. vga_out8(0x3c4, 0x08, par);
  1327. sr8 = vga_in8(0x3c5, par);
  1328. sr8 |= 0x06;
  1329. vga_out8(0x3c5, sr8, par);
  1330. vga_out8(0x3c4, 0x0d, par);
  1331. srd = vga_in8(0x3c5, par);
  1332. srd &= 0x50;
  1333. switch (blank) {
  1334. case FB_BLANK_UNBLANK:
  1335. case FB_BLANK_NORMAL:
  1336. break;
  1337. case FB_BLANK_VSYNC_SUSPEND:
  1338. srd |= 0x10;
  1339. break;
  1340. case FB_BLANK_HSYNC_SUSPEND:
  1341. srd |= 0x40;
  1342. break;
  1343. case FB_BLANK_POWERDOWN:
  1344. srd |= 0x50;
  1345. break;
  1346. }
  1347. vga_out8(0x3c4, 0x0d, par);
  1348. vga_out8(0x3c5, srd, par);
  1349. }
  1350. if (par->display_type == DISP_LCD ||
  1351. par->display_type == DISP_DFP) {
  1352. switch(blank) {
  1353. case FB_BLANK_UNBLANK:
  1354. case FB_BLANK_NORMAL:
  1355. vga_out8(0x3c4, 0x31, par); /* SR31 bit 4 - FP enable */
  1356. vga_out8(0x3c5, vga_in8(0x3c5, par) | 0x10, par);
  1357. break;
  1358. case FB_BLANK_VSYNC_SUSPEND:
  1359. case FB_BLANK_HSYNC_SUSPEND:
  1360. case FB_BLANK_POWERDOWN:
  1361. vga_out8(0x3c4, 0x31, par); /* SR31 bit 4 - FP enable */
  1362. vga_out8(0x3c5, vga_in8(0x3c5, par) & ~0x10, par);
  1363. break;
  1364. }
  1365. }
  1366. return (blank == FB_BLANK_NORMAL) ? 1 : 0;
  1367. }
  1368. static int savagefb_open(struct fb_info *info, int user)
  1369. {
  1370. struct savagefb_par *par = info->par;
  1371. mutex_lock(&par->open_lock);
  1372. if (!par->open_count) {
  1373. memset(&par->vgastate, 0, sizeof(par->vgastate));
  1374. par->vgastate.flags = VGA_SAVE_CMAP | VGA_SAVE_FONTS |
  1375. VGA_SAVE_MODE;
  1376. par->vgastate.vgabase = par->mmio.vbase + 0x8000;
  1377. save_vga(&par->vgastate);
  1378. savage_get_default_par(par, &par->initial);
  1379. }
  1380. par->open_count++;
  1381. mutex_unlock(&par->open_lock);
  1382. return 0;
  1383. }
  1384. static int savagefb_release(struct fb_info *info, int user)
  1385. {
  1386. struct savagefb_par *par = info->par;
  1387. mutex_lock(&par->open_lock);
  1388. if (par->open_count == 1) {
  1389. savage_set_default_par(par, &par->initial);
  1390. restore_vga(&par->vgastate);
  1391. }
  1392. par->open_count--;
  1393. mutex_unlock(&par->open_lock);
  1394. return 0;
  1395. }
  1396. static const struct fb_ops savagefb_ops = {
  1397. .owner = THIS_MODULE,
  1398. .fb_open = savagefb_open,
  1399. .fb_release = savagefb_release,
  1400. .fb_check_var = savagefb_check_var,
  1401. .fb_set_par = savagefb_set_par,
  1402. .fb_setcolreg = savagefb_setcolreg,
  1403. .fb_pan_display = savagefb_pan_display,
  1404. .fb_blank = savagefb_blank,
  1405. #if defined(CONFIG_FB_SAVAGE_ACCEL)
  1406. .fb_fillrect = savagefb_fillrect,
  1407. .fb_copyarea = savagefb_copyarea,
  1408. .fb_imageblit = savagefb_imageblit,
  1409. .fb_sync = savagefb_sync,
  1410. #else
  1411. .fb_fillrect = cfb_fillrect,
  1412. .fb_copyarea = cfb_copyarea,
  1413. .fb_imageblit = cfb_imageblit,
  1414. #endif
  1415. };
  1416. /* --------------------------------------------------------------------- */
  1417. static const struct fb_var_screeninfo savagefb_var800x600x8 = {
  1418. .accel_flags = FB_ACCELF_TEXT,
  1419. .xres = 800,
  1420. .yres = 600,
  1421. .xres_virtual = 800,
  1422. .yres_virtual = 600,
  1423. .bits_per_pixel = 8,
  1424. .pixclock = 25000,
  1425. .left_margin = 88,
  1426. .right_margin = 40,
  1427. .upper_margin = 23,
  1428. .lower_margin = 1,
  1429. .hsync_len = 128,
  1430. .vsync_len = 4,
  1431. .sync = FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  1432. .vmode = FB_VMODE_NONINTERLACED
  1433. };
  1434. static void savage_enable_mmio(struct savagefb_par *par)
  1435. {
  1436. unsigned char val;
  1437. DBG("savage_enable_mmio\n");
  1438. val = vga_in8(0x3c3, par);
  1439. vga_out8(0x3c3, val | 0x01, par);
  1440. val = vga_in8(0x3cc, par);
  1441. vga_out8(0x3c2, val | 0x01, par);
  1442. if (par->chip >= S3_SAVAGE4) {
  1443. vga_out8(0x3d4, 0x40, par);
  1444. val = vga_in8(0x3d5, par);
  1445. vga_out8(0x3d5, val | 1, par);
  1446. }
  1447. }
  1448. static void savage_disable_mmio(struct savagefb_par *par)
  1449. {
  1450. unsigned char val;
  1451. DBG("savage_disable_mmio\n");
  1452. if (par->chip >= S3_SAVAGE4) {
  1453. vga_out8(0x3d4, 0x40, par);
  1454. val = vga_in8(0x3d5, par);
  1455. vga_out8(0x3d5, val | 1, par);
  1456. }
  1457. }
  1458. static int savage_map_mmio(struct fb_info *info)
  1459. {
  1460. struct savagefb_par *par = info->par;
  1461. DBG("savage_map_mmio");
  1462. if (S3_SAVAGE3D_SERIES(par->chip))
  1463. par->mmio.pbase = pci_resource_start(par->pcidev, 0) +
  1464. SAVAGE_NEWMMIO_REGBASE_S3;
  1465. else
  1466. par->mmio.pbase = pci_resource_start(par->pcidev, 0) +
  1467. SAVAGE_NEWMMIO_REGBASE_S4;
  1468. par->mmio.len = SAVAGE_NEWMMIO_REGSIZE;
  1469. par->mmio.vbase = ioremap(par->mmio.pbase, par->mmio.len);
  1470. if (!par->mmio.vbase) {
  1471. printk("savagefb: unable to map memory mapped IO\n");
  1472. return -ENOMEM;
  1473. } else
  1474. printk(KERN_INFO "savagefb: mapped io at %p\n",
  1475. par->mmio.vbase);
  1476. info->fix.mmio_start = par->mmio.pbase;
  1477. info->fix.mmio_len = par->mmio.len;
  1478. par->bci_base = (u32 __iomem *)(par->mmio.vbase + BCI_BUFFER_OFFSET);
  1479. par->bci_ptr = 0;
  1480. savage_enable_mmio(par);
  1481. return 0;
  1482. }
  1483. static void savage_unmap_mmio(struct fb_info *info)
  1484. {
  1485. struct savagefb_par *par = info->par;
  1486. DBG("savage_unmap_mmio");
  1487. savage_disable_mmio(par);
  1488. if (par->mmio.vbase) {
  1489. iounmap(par->mmio.vbase);
  1490. par->mmio.vbase = NULL;
  1491. }
  1492. }
  1493. static int savage_map_video(struct fb_info *info, int video_len)
  1494. {
  1495. struct savagefb_par *par = info->par;
  1496. int resource;
  1497. DBG("savage_map_video");
  1498. if (S3_SAVAGE3D_SERIES(par->chip))
  1499. resource = 0;
  1500. else
  1501. resource = 1;
  1502. par->video.pbase = pci_resource_start(par->pcidev, resource);
  1503. par->video.len = video_len;
  1504. par->video.vbase = ioremap_wc(par->video.pbase, par->video.len);
  1505. if (!par->video.vbase) {
  1506. printk("savagefb: unable to map screen memory\n");
  1507. return -ENOMEM;
  1508. } else
  1509. printk(KERN_INFO "savagefb: mapped framebuffer at %p, "
  1510. "pbase == %x\n", par->video.vbase, par->video.pbase);
  1511. info->fix.smem_start = par->video.pbase;
  1512. info->fix.smem_len = par->video.len - par->cob_size;
  1513. info->screen_base = par->video.vbase;
  1514. par->video.wc_cookie = arch_phys_wc_add(par->video.pbase, video_len);
  1515. /* Clear framebuffer, it's all white in memory after boot */
  1516. memset_io(par->video.vbase, 0, par->video.len);
  1517. return 0;
  1518. }
  1519. static void savage_unmap_video(struct fb_info *info)
  1520. {
  1521. struct savagefb_par *par = info->par;
  1522. DBG("savage_unmap_video");
  1523. if (par->video.vbase) {
  1524. arch_phys_wc_del(par->video.wc_cookie);
  1525. iounmap(par->video.vbase);
  1526. par->video.vbase = NULL;
  1527. info->screen_base = NULL;
  1528. }
  1529. }
  1530. static int savage_init_hw(struct savagefb_par *par)
  1531. {
  1532. unsigned char config1, m, n, n1, n2, sr8, cr3f, cr66 = 0, tmp;
  1533. static unsigned char RamSavage3D[] = { 8, 4, 4, 2 };
  1534. static unsigned char RamSavage4[] = { 2, 4, 8, 12, 16, 32, 64, 32 };
  1535. static unsigned char RamSavageMX[] = { 2, 8, 4, 16, 8, 16, 4, 16 };
  1536. static unsigned char RamSavageNB[] = { 0, 2, 4, 8, 16, 32, 2, 2 };
  1537. int videoRam, videoRambytes, dvi;
  1538. DBG("savage_init_hw");
  1539. /* unprotect CRTC[0-7] */
  1540. vga_out8(0x3d4, 0x11, par);
  1541. tmp = vga_in8(0x3d5, par);
  1542. vga_out8(0x3d5, tmp & 0x7f, par);
  1543. /* unlock extended regs */
  1544. vga_out16(0x3d4, 0x4838, par);
  1545. vga_out16(0x3d4, 0xa039, par);
  1546. vga_out16(0x3c4, 0x0608, par);
  1547. vga_out8(0x3d4, 0x40, par);
  1548. tmp = vga_in8(0x3d5, par);
  1549. vga_out8(0x3d5, tmp & ~0x01, par);
  1550. /* unlock sys regs */
  1551. vga_out8(0x3d4, 0x38, par);
  1552. vga_out8(0x3d5, 0x48, par);
  1553. /* Unlock system registers. */
  1554. vga_out16(0x3d4, 0x4838, par);
  1555. /* Next go on to detect amount of installed ram */
  1556. vga_out8(0x3d4, 0x36, par); /* for register CR36 (CONFG_REG1), */
  1557. config1 = vga_in8(0x3d5, par); /* get amount of vram installed */
  1558. /* Compute the amount of video memory and offscreen memory. */
  1559. switch (par->chip) {
  1560. case S3_SAVAGE3D:
  1561. videoRam = RamSavage3D[(config1 & 0xC0) >> 6 ] * 1024;
  1562. break;
  1563. case S3_SAVAGE4:
  1564. /*
  1565. * The Savage4 has one ugly special case to consider. On
  1566. * systems with 4 banks of 2Mx32 SDRAM, the BIOS says 4MB
  1567. * when it really means 8MB. Why do it the same when you
  1568. * can do it different...
  1569. */
  1570. vga_out8(0x3d4, 0x68, par); /* memory control 1 */
  1571. if ((vga_in8(0x3d5, par) & 0xC0) == (0x01 << 6))
  1572. RamSavage4[1] = 8;
  1573. fallthrough;
  1574. case S3_SAVAGE2000:
  1575. videoRam = RamSavage4[(config1 & 0xE0) >> 5] * 1024;
  1576. break;
  1577. case S3_SAVAGE_MX:
  1578. case S3_SUPERSAVAGE:
  1579. videoRam = RamSavageMX[(config1 & 0x0E) >> 1] * 1024;
  1580. break;
  1581. case S3_PROSAVAGE:
  1582. case S3_PROSAVAGEDDR:
  1583. case S3_TWISTER:
  1584. videoRam = RamSavageNB[(config1 & 0xE0) >> 5] * 1024;
  1585. break;
  1586. default:
  1587. /* How did we get here? */
  1588. videoRam = 0;
  1589. break;
  1590. }
  1591. videoRambytes = videoRam * 1024;
  1592. printk(KERN_INFO "savagefb: probed videoram: %dk\n", videoRam);
  1593. /* reset graphics engine to avoid memory corruption */
  1594. vga_out8(0x3d4, 0x66, par);
  1595. cr66 = vga_in8(0x3d5, par);
  1596. vga_out8(0x3d5, cr66 | 0x02, par);
  1597. usleep_range(10000, 11000);
  1598. vga_out8(0x3d4, 0x66, par);
  1599. vga_out8(0x3d5, cr66 & ~0x02, par); /* clear reset flag */
  1600. usleep_range(10000, 11000);
  1601. /*
  1602. * reset memory interface, 3D engine, AGP master, PCI master,
  1603. * master engine unit, motion compensation/LPB
  1604. */
  1605. vga_out8(0x3d4, 0x3f, par);
  1606. cr3f = vga_in8(0x3d5, par);
  1607. vga_out8(0x3d5, cr3f | 0x08, par);
  1608. usleep_range(10000, 11000);
  1609. vga_out8(0x3d4, 0x3f, par);
  1610. vga_out8(0x3d5, cr3f & ~0x08, par); /* clear reset flags */
  1611. usleep_range(10000, 11000);
  1612. /* Savage ramdac speeds */
  1613. par->numClocks = 4;
  1614. par->clock[0] = 250000;
  1615. par->clock[1] = 250000;
  1616. par->clock[2] = 220000;
  1617. par->clock[3] = 220000;
  1618. /* detect current mclk */
  1619. vga_out8(0x3c4, 0x08, par);
  1620. sr8 = vga_in8(0x3c5, par);
  1621. vga_out8(0x3c5, 0x06, par);
  1622. vga_out8(0x3c4, 0x10, par);
  1623. n = vga_in8(0x3c5, par);
  1624. vga_out8(0x3c4, 0x11, par);
  1625. m = vga_in8(0x3c5, par);
  1626. vga_out8(0x3c4, 0x08, par);
  1627. vga_out8(0x3c5, sr8, par);
  1628. m &= 0x7f;
  1629. n1 = n & 0x1f;
  1630. n2 = (n >> 5) & 0x03;
  1631. par->MCLK = ((1431818 * (m+2)) / (n1+2) / (1 << n2) + 50) / 100;
  1632. printk(KERN_INFO "savagefb: Detected current MCLK value of %d kHz\n",
  1633. par->MCLK);
  1634. /* check for DVI/flat panel */
  1635. dvi = 0;
  1636. if (par->chip == S3_SAVAGE4) {
  1637. unsigned char sr30 = 0x00;
  1638. vga_out8(0x3c4, 0x30, par);
  1639. /* clear bit 1 */
  1640. vga_out8(0x3c5, vga_in8(0x3c5, par) & ~0x02, par);
  1641. sr30 = vga_in8(0x3c5, par);
  1642. if (sr30 & 0x02 /*0x04 */) {
  1643. dvi = 1;
  1644. printk("savagefb: Digital Flat Panel Detected\n");
  1645. }
  1646. }
  1647. if ((S3_SAVAGE_MOBILE_SERIES(par->chip) ||
  1648. S3_MOBILE_TWISTER_SERIES(par->chip)) && !par->crtonly)
  1649. par->display_type = DISP_LCD;
  1650. else if (dvi || (par->chip == S3_SAVAGE4 && par->dvi))
  1651. par->display_type = DISP_DFP;
  1652. else
  1653. par->display_type = DISP_CRT;
  1654. /* Check LCD panel parrmation */
  1655. if (par->display_type == DISP_LCD) {
  1656. unsigned char cr6b = VGArCR(0x6b, par);
  1657. int panelX = (VGArSEQ(0x61, par) +
  1658. ((VGArSEQ(0x66, par) & 0x02) << 7) + 1) * 8;
  1659. int panelY = (VGArSEQ(0x69, par) +
  1660. ((VGArSEQ(0x6e, par) & 0x70) << 4) + 1);
  1661. char * sTechnology = "Unknown";
  1662. /* OK, I admit it. I don't know how to limit the max dot clock
  1663. * for LCD panels of various sizes. I thought I copied the
  1664. * formula from the BIOS, but many users have parrmed me of
  1665. * my folly.
  1666. *
  1667. * Instead, I'll abandon any attempt to automatically limit the
  1668. * clock, and add an LCDClock option to XF86Config. Some day,
  1669. * I should come back to this.
  1670. */
  1671. enum ACTIVE_DISPLAYS { /* These are the bits in CR6B */
  1672. ActiveCRT = 0x01,
  1673. ActiveLCD = 0x02,
  1674. ActiveTV = 0x04,
  1675. ActiveCRT2 = 0x20,
  1676. ActiveDUO = 0x80
  1677. };
  1678. if ((VGArSEQ(0x39, par) & 0x03) == 0) {
  1679. sTechnology = "TFT";
  1680. } else if ((VGArSEQ(0x30, par) & 0x01) == 0) {
  1681. sTechnology = "DSTN";
  1682. } else {
  1683. sTechnology = "STN";
  1684. }
  1685. printk(KERN_INFO "savagefb: %dx%d %s LCD panel detected %s\n",
  1686. panelX, panelY, sTechnology,
  1687. cr6b & ActiveLCD ? "and active" : "but not active");
  1688. if (cr6b & ActiveLCD) {
  1689. /*
  1690. * If the LCD is active and panel expansion is enabled,
  1691. * we probably want to kill the HW cursor.
  1692. */
  1693. printk(KERN_INFO "savagefb: Limiting video mode to "
  1694. "%dx%d\n", panelX, panelY);
  1695. par->SavagePanelWidth = panelX;
  1696. par->SavagePanelHeight = panelY;
  1697. } else
  1698. par->display_type = DISP_CRT;
  1699. }
  1700. savage_get_default_par(par, &par->state);
  1701. par->save = par->state;
  1702. if (S3_SAVAGE4_SERIES(par->chip)) {
  1703. /*
  1704. * The Savage4 and ProSavage have COB coherency bugs which
  1705. * render the buffer useless. We disable it.
  1706. */
  1707. par->cob_index = 2;
  1708. par->cob_size = 0x8000 << par->cob_index;
  1709. par->cob_offset = videoRambytes;
  1710. } else {
  1711. /* We use 128kB for the COB on all chips. */
  1712. par->cob_index = 7;
  1713. par->cob_size = 0x400 << par->cob_index;
  1714. par->cob_offset = videoRambytes - par->cob_size;
  1715. }
  1716. return videoRambytes;
  1717. }
  1718. static int savage_init_fb_info(struct fb_info *info, struct pci_dev *dev,
  1719. const struct pci_device_id *id)
  1720. {
  1721. struct savagefb_par *par = info->par;
  1722. int err = 0;
  1723. par->pcidev = dev;
  1724. info->fix.type = FB_TYPE_PACKED_PIXELS;
  1725. info->fix.type_aux = 0;
  1726. info->fix.ypanstep = 1;
  1727. info->fix.ywrapstep = 0;
  1728. info->fix.accel = id->driver_data;
  1729. switch (info->fix.accel) {
  1730. case FB_ACCEL_SUPERSAVAGE:
  1731. par->chip = S3_SUPERSAVAGE;
  1732. snprintf(info->fix.id, 16, "SuperSavage");
  1733. break;
  1734. case FB_ACCEL_SAVAGE4:
  1735. par->chip = S3_SAVAGE4;
  1736. snprintf(info->fix.id, 16, "Savage4");
  1737. break;
  1738. case FB_ACCEL_SAVAGE3D:
  1739. par->chip = S3_SAVAGE3D;
  1740. snprintf(info->fix.id, 16, "Savage3D");
  1741. break;
  1742. case FB_ACCEL_SAVAGE3D_MV:
  1743. par->chip = S3_SAVAGE3D;
  1744. snprintf(info->fix.id, 16, "Savage3D-MV");
  1745. break;
  1746. case FB_ACCEL_SAVAGE2000:
  1747. par->chip = S3_SAVAGE2000;
  1748. snprintf(info->fix.id, 16, "Savage2000");
  1749. break;
  1750. case FB_ACCEL_SAVAGE_MX_MV:
  1751. par->chip = S3_SAVAGE_MX;
  1752. snprintf(info->fix.id, 16, "Savage/MX-MV");
  1753. break;
  1754. case FB_ACCEL_SAVAGE_MX:
  1755. par->chip = S3_SAVAGE_MX;
  1756. snprintf(info->fix.id, 16, "Savage/MX");
  1757. break;
  1758. case FB_ACCEL_SAVAGE_IX_MV:
  1759. par->chip = S3_SAVAGE_MX;
  1760. snprintf(info->fix.id, 16, "Savage/IX-MV");
  1761. break;
  1762. case FB_ACCEL_SAVAGE_IX:
  1763. par->chip = S3_SAVAGE_MX;
  1764. snprintf(info->fix.id, 16, "Savage/IX");
  1765. break;
  1766. case FB_ACCEL_PROSAVAGE_PM:
  1767. par->chip = S3_PROSAVAGE;
  1768. snprintf(info->fix.id, 16, "ProSavagePM");
  1769. break;
  1770. case FB_ACCEL_PROSAVAGE_KM:
  1771. par->chip = S3_PROSAVAGE;
  1772. snprintf(info->fix.id, 16, "ProSavageKM");
  1773. break;
  1774. case FB_ACCEL_S3TWISTER_P:
  1775. par->chip = S3_TWISTER;
  1776. snprintf(info->fix.id, 16, "TwisterP");
  1777. break;
  1778. case FB_ACCEL_S3TWISTER_K:
  1779. par->chip = S3_TWISTER;
  1780. snprintf(info->fix.id, 16, "TwisterK");
  1781. break;
  1782. case FB_ACCEL_PROSAVAGE_DDR:
  1783. par->chip = S3_PROSAVAGEDDR;
  1784. snprintf(info->fix.id, 16, "ProSavageDDR");
  1785. break;
  1786. case FB_ACCEL_PROSAVAGE_DDRK:
  1787. par->chip = S3_PROSAVAGEDDR;
  1788. snprintf(info->fix.id, 16, "ProSavage8");
  1789. break;
  1790. }
  1791. if (S3_SAVAGE3D_SERIES(par->chip)) {
  1792. par->SavageWaitIdle = savage3D_waitidle;
  1793. par->SavageWaitFifo = savage3D_waitfifo;
  1794. } else if (S3_SAVAGE4_SERIES(par->chip) ||
  1795. S3_SUPERSAVAGE == par->chip) {
  1796. par->SavageWaitIdle = savage4_waitidle;
  1797. par->SavageWaitFifo = savage4_waitfifo;
  1798. } else {
  1799. par->SavageWaitIdle = savage2000_waitidle;
  1800. par->SavageWaitFifo = savage2000_waitfifo;
  1801. }
  1802. info->var.nonstd = 0;
  1803. info->var.activate = FB_ACTIVATE_NOW;
  1804. info->var.width = -1;
  1805. info->var.height = -1;
  1806. info->var.accel_flags = 0;
  1807. info->fbops = &savagefb_ops;
  1808. info->flags = FBINFO_DEFAULT |
  1809. FBINFO_HWACCEL_YPAN |
  1810. FBINFO_HWACCEL_XPAN;
  1811. info->pseudo_palette = par->pseudo_palette;
  1812. #if defined(CONFIG_FB_SAVAGE_ACCEL)
  1813. /* FIFO size + padding for commands */
  1814. info->pixmap.addr = kcalloc(8, 1024, GFP_KERNEL);
  1815. err = -ENOMEM;
  1816. if (info->pixmap.addr) {
  1817. info->pixmap.size = 8*1024;
  1818. info->pixmap.scan_align = 4;
  1819. info->pixmap.buf_align = 4;
  1820. info->pixmap.access_align = 32;
  1821. err = fb_alloc_cmap(&info->cmap, NR_PALETTE, 0);
  1822. if (!err)
  1823. info->flags |= FBINFO_HWACCEL_COPYAREA |
  1824. FBINFO_HWACCEL_FILLRECT |
  1825. FBINFO_HWACCEL_IMAGEBLIT;
  1826. else
  1827. kfree(info->pixmap.addr);
  1828. }
  1829. #endif
  1830. return err;
  1831. }
  1832. /* --------------------------------------------------------------------- */
  1833. static int savagefb_probe(struct pci_dev *dev, const struct pci_device_id *id)
  1834. {
  1835. struct fb_info *info;
  1836. struct savagefb_par *par;
  1837. u_int h_sync, v_sync;
  1838. unsigned char __maybe_unused *edid;
  1839. int err, lpitch;
  1840. int video_len;
  1841. DBG("savagefb_probe");
  1842. err = aperture_remove_conflicting_pci_devices(dev, "savagefb");
  1843. if (err)
  1844. return err;
  1845. info = framebuffer_alloc(sizeof(struct savagefb_par), &dev->dev);
  1846. if (!info)
  1847. return -ENOMEM;
  1848. par = info->par;
  1849. mutex_init(&par->open_lock);
  1850. err = pci_enable_device(dev);
  1851. if (err)
  1852. goto failed_enable;
  1853. if ((err = pci_request_regions(dev, "savagefb"))) {
  1854. printk(KERN_ERR "cannot request PCI regions\n");
  1855. goto failed_enable;
  1856. }
  1857. err = -ENOMEM;
  1858. if ((err = savage_init_fb_info(info, dev, id)))
  1859. goto failed_init;
  1860. err = savage_map_mmio(info);
  1861. if (err)
  1862. goto failed_mmio;
  1863. video_len = savage_init_hw(par);
  1864. /* FIXME: can't be negative */
  1865. if (video_len < 0) {
  1866. err = video_len;
  1867. goto failed_mmio;
  1868. }
  1869. err = savage_map_video(info, video_len);
  1870. if (err)
  1871. goto failed_video;
  1872. INIT_LIST_HEAD(&info->modelist);
  1873. #if defined(CONFIG_FB_SAVAGE_I2C)
  1874. savagefb_create_i2c_busses(info);
  1875. savagefb_probe_i2c_connector(info, &edid);
  1876. fb_edid_to_monspecs(edid, &info->monspecs);
  1877. kfree(edid);
  1878. fb_videomode_to_modelist(info->monspecs.modedb,
  1879. info->monspecs.modedb_len,
  1880. &info->modelist);
  1881. #endif
  1882. info->var = savagefb_var800x600x8;
  1883. /* if a panel was detected, default to a CVT mode instead */
  1884. if (par->SavagePanelWidth) {
  1885. struct fb_videomode cvt_mode;
  1886. memset(&cvt_mode, 0, sizeof(cvt_mode));
  1887. cvt_mode.xres = par->SavagePanelWidth;
  1888. cvt_mode.yres = par->SavagePanelHeight;
  1889. cvt_mode.refresh = 60;
  1890. /* FIXME: if we know there is only the panel
  1891. * we can enable reduced blanking as well */
  1892. if (fb_find_mode_cvt(&cvt_mode, 0, 0))
  1893. printk(KERN_WARNING "No CVT mode found for panel\n");
  1894. else if (fb_find_mode(&info->var, info, NULL, NULL, 0,
  1895. &cvt_mode, 0) != 3)
  1896. info->var = savagefb_var800x600x8;
  1897. }
  1898. if (mode_option) {
  1899. fb_find_mode(&info->var, info, mode_option,
  1900. info->monspecs.modedb, info->monspecs.modedb_len,
  1901. NULL, 8);
  1902. } else if (info->monspecs.modedb != NULL) {
  1903. const struct fb_videomode *mode;
  1904. mode = fb_find_best_display(&info->monspecs, &info->modelist);
  1905. savage_update_var(&info->var, mode);
  1906. }
  1907. /* maximize virtual vertical length */
  1908. lpitch = info->var.xres_virtual*((info->var.bits_per_pixel + 7) >> 3);
  1909. info->var.yres_virtual = info->fix.smem_len/lpitch;
  1910. if (info->var.yres_virtual < info->var.yres) {
  1911. err = -ENOMEM;
  1912. goto failed;
  1913. }
  1914. #if defined(CONFIG_FB_SAVAGE_ACCEL)
  1915. /*
  1916. * The clipping coordinates are masked with 0xFFF, so limit our
  1917. * virtual resolutions to these sizes.
  1918. */
  1919. if (info->var.yres_virtual > 0x1000)
  1920. info->var.yres_virtual = 0x1000;
  1921. if (info->var.xres_virtual > 0x1000)
  1922. info->var.xres_virtual = 0x1000;
  1923. #endif
  1924. savagefb_check_var(&info->var, info);
  1925. savagefb_set_fix(info);
  1926. /*
  1927. * Calculate the hsync and vsync frequencies. Note that
  1928. * we split the 1e12 constant up so that we can preserve
  1929. * the precision and fit the results into 32-bit registers.
  1930. * (1953125000 * 512 = 1e12)
  1931. */
  1932. h_sync = 1953125000 / info->var.pixclock;
  1933. h_sync = h_sync * 512 / (info->var.xres + info->var.left_margin +
  1934. info->var.right_margin +
  1935. info->var.hsync_len);
  1936. v_sync = h_sync / (info->var.yres + info->var.upper_margin +
  1937. info->var.lower_margin + info->var.vsync_len);
  1938. printk(KERN_INFO "savagefb v" SAVAGEFB_VERSION ": "
  1939. "%dkB VRAM, using %dx%d, %d.%03dkHz, %dHz\n",
  1940. info->fix.smem_len >> 10,
  1941. info->var.xres, info->var.yres,
  1942. h_sync / 1000, h_sync % 1000, v_sync);
  1943. fb_destroy_modedb(info->monspecs.modedb);
  1944. info->monspecs.modedb = NULL;
  1945. err = register_framebuffer(info);
  1946. if (err < 0)
  1947. goto failed;
  1948. printk(KERN_INFO "fb: S3 %s frame buffer device\n",
  1949. info->fix.id);
  1950. /*
  1951. * Our driver data
  1952. */
  1953. pci_set_drvdata(dev, info);
  1954. return 0;
  1955. failed:
  1956. #ifdef CONFIG_FB_SAVAGE_I2C
  1957. savagefb_delete_i2c_busses(info);
  1958. #endif
  1959. fb_alloc_cmap(&info->cmap, 0, 0);
  1960. savage_unmap_video(info);
  1961. failed_video:
  1962. savage_unmap_mmio(info);
  1963. failed_mmio:
  1964. kfree(info->pixmap.addr);
  1965. failed_init:
  1966. pci_release_regions(dev);
  1967. failed_enable:
  1968. framebuffer_release(info);
  1969. return err;
  1970. }
  1971. static void savagefb_remove(struct pci_dev *dev)
  1972. {
  1973. struct fb_info *info = pci_get_drvdata(dev);
  1974. DBG("savagefb_remove");
  1975. if (info) {
  1976. unregister_framebuffer(info);
  1977. #ifdef CONFIG_FB_SAVAGE_I2C
  1978. savagefb_delete_i2c_busses(info);
  1979. #endif
  1980. fb_alloc_cmap(&info->cmap, 0, 0);
  1981. savage_unmap_video(info);
  1982. savage_unmap_mmio(info);
  1983. kfree(info->pixmap.addr);
  1984. pci_release_regions(dev);
  1985. framebuffer_release(info);
  1986. }
  1987. }
  1988. static int savagefb_suspend_late(struct device *dev, pm_message_t mesg)
  1989. {
  1990. struct fb_info *info = dev_get_drvdata(dev);
  1991. struct savagefb_par *par = info->par;
  1992. DBG("savagefb_suspend");
  1993. if (mesg.event == PM_EVENT_PRETHAW)
  1994. mesg.event = PM_EVENT_FREEZE;
  1995. par->pm_state = mesg.event;
  1996. dev->power.power_state = mesg;
  1997. /*
  1998. * For PM_EVENT_FREEZE, do not power down so the console
  1999. * can remain active.
  2000. */
  2001. if (mesg.event == PM_EVENT_FREEZE)
  2002. return 0;
  2003. console_lock();
  2004. fb_set_suspend(info, 1);
  2005. if (info->fbops->fb_sync)
  2006. info->fbops->fb_sync(info);
  2007. savagefb_blank(FB_BLANK_POWERDOWN, info);
  2008. savage_set_default_par(par, &par->save);
  2009. savage_disable_mmio(par);
  2010. console_unlock();
  2011. return 0;
  2012. }
  2013. static int __maybe_unused savagefb_suspend(struct device *dev)
  2014. {
  2015. return savagefb_suspend_late(dev, PMSG_SUSPEND);
  2016. }
  2017. static int __maybe_unused savagefb_hibernate(struct device *dev)
  2018. {
  2019. return savagefb_suspend_late(dev, PMSG_HIBERNATE);
  2020. }
  2021. static int __maybe_unused savagefb_freeze(struct device *dev)
  2022. {
  2023. return savagefb_suspend_late(dev, PMSG_FREEZE);
  2024. }
  2025. static int __maybe_unused savagefb_resume(struct device *dev)
  2026. {
  2027. struct fb_info *info = dev_get_drvdata(dev);
  2028. struct savagefb_par *par = info->par;
  2029. int cur_state = par->pm_state;
  2030. DBG("savage_resume");
  2031. par->pm_state = PM_EVENT_ON;
  2032. /*
  2033. * The adapter was not powered down coming back from a
  2034. * PM_EVENT_FREEZE.
  2035. */
  2036. if (cur_state == PM_EVENT_FREEZE)
  2037. return 0;
  2038. console_lock();
  2039. savage_enable_mmio(par);
  2040. savage_init_hw(par);
  2041. savagefb_set_par(info);
  2042. fb_set_suspend(info, 0);
  2043. savagefb_blank(FB_BLANK_UNBLANK, info);
  2044. console_unlock();
  2045. return 0;
  2046. }
  2047. static const struct dev_pm_ops savagefb_pm_ops = {
  2048. #ifdef CONFIG_PM_SLEEP
  2049. .suspend = savagefb_suspend,
  2050. .resume = savagefb_resume,
  2051. .freeze = savagefb_freeze,
  2052. .thaw = savagefb_resume,
  2053. .poweroff = savagefb_hibernate,
  2054. .restore = savagefb_resume,
  2055. #endif
  2056. };
  2057. static const struct pci_device_id savagefb_devices[] = {
  2058. {PCI_VENDOR_ID_S3, PCI_CHIP_SUPSAV_MX128,
  2059. PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SUPERSAVAGE},
  2060. {PCI_VENDOR_ID_S3, PCI_CHIP_SUPSAV_MX64,
  2061. PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SUPERSAVAGE},
  2062. {PCI_VENDOR_ID_S3, PCI_CHIP_SUPSAV_MX64C,
  2063. PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SUPERSAVAGE},
  2064. {PCI_VENDOR_ID_S3, PCI_CHIP_SUPSAV_IX128SDR,
  2065. PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SUPERSAVAGE},
  2066. {PCI_VENDOR_ID_S3, PCI_CHIP_SUPSAV_IX128DDR,
  2067. PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SUPERSAVAGE},
  2068. {PCI_VENDOR_ID_S3, PCI_CHIP_SUPSAV_IX64SDR,
  2069. PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SUPERSAVAGE},
  2070. {PCI_VENDOR_ID_S3, PCI_CHIP_SUPSAV_IX64DDR,
  2071. PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SUPERSAVAGE},
  2072. {PCI_VENDOR_ID_S3, PCI_CHIP_SUPSAV_IXCSDR,
  2073. PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SUPERSAVAGE},
  2074. {PCI_VENDOR_ID_S3, PCI_CHIP_SUPSAV_IXCDDR,
  2075. PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SUPERSAVAGE},
  2076. {PCI_VENDOR_ID_S3, PCI_CHIP_SAVAGE4,
  2077. PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SAVAGE4},
  2078. {PCI_VENDOR_ID_S3, PCI_CHIP_SAVAGE3D,
  2079. PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SAVAGE3D},
  2080. {PCI_VENDOR_ID_S3, PCI_CHIP_SAVAGE3D_MV,
  2081. PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SAVAGE3D_MV},
  2082. {PCI_VENDOR_ID_S3, PCI_CHIP_SAVAGE2000,
  2083. PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SAVAGE2000},
  2084. {PCI_VENDOR_ID_S3, PCI_CHIP_SAVAGE_MX_MV,
  2085. PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SAVAGE_MX_MV},
  2086. {PCI_VENDOR_ID_S3, PCI_CHIP_SAVAGE_MX,
  2087. PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SAVAGE_MX},
  2088. {PCI_VENDOR_ID_S3, PCI_CHIP_SAVAGE_IX_MV,
  2089. PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SAVAGE_IX_MV},
  2090. {PCI_VENDOR_ID_S3, PCI_CHIP_SAVAGE_IX,
  2091. PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_SAVAGE_IX},
  2092. {PCI_VENDOR_ID_S3, PCI_CHIP_PROSAVAGE_PM,
  2093. PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_PROSAVAGE_PM},
  2094. {PCI_VENDOR_ID_S3, PCI_CHIP_PROSAVAGE_KM,
  2095. PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_PROSAVAGE_KM},
  2096. {PCI_VENDOR_ID_S3, PCI_CHIP_S3TWISTER_P,
  2097. PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_S3TWISTER_P},
  2098. {PCI_VENDOR_ID_S3, PCI_CHIP_S3TWISTER_K,
  2099. PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_S3TWISTER_K},
  2100. {PCI_VENDOR_ID_S3, PCI_CHIP_PROSAVAGE_DDR,
  2101. PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_PROSAVAGE_DDR},
  2102. {PCI_VENDOR_ID_S3, PCI_CHIP_PROSAVAGE_DDRK,
  2103. PCI_ANY_ID, PCI_ANY_ID, 0, 0, FB_ACCEL_PROSAVAGE_DDRK},
  2104. {0, 0, 0, 0, 0, 0, 0}
  2105. };
  2106. MODULE_DEVICE_TABLE(pci, savagefb_devices);
  2107. static struct pci_driver savagefb_driver = {
  2108. .name = "savagefb",
  2109. .id_table = savagefb_devices,
  2110. .probe = savagefb_probe,
  2111. .driver.pm = &savagefb_pm_ops,
  2112. .remove = savagefb_remove,
  2113. };
  2114. /* **************************** exit-time only **************************** */
  2115. static void __exit savage_done(void)
  2116. {
  2117. DBG("savage_done");
  2118. pci_unregister_driver(&savagefb_driver);
  2119. }
  2120. /* ************************* init in-kernel code ************************** */
  2121. static int __init savagefb_setup(char *options)
  2122. {
  2123. #ifndef MODULE
  2124. char *this_opt;
  2125. if (!options || !*options)
  2126. return 0;
  2127. while ((this_opt = strsep(&options, ",")) != NULL) {
  2128. mode_option = this_opt;
  2129. }
  2130. #endif /* !MODULE */
  2131. return 0;
  2132. }
  2133. static int __init savagefb_init(void)
  2134. {
  2135. char *option;
  2136. DBG("savagefb_init");
  2137. if (fb_get_options("savagefb", &option))
  2138. return -ENODEV;
  2139. savagefb_setup(option);
  2140. return pci_register_driver(&savagefb_driver);
  2141. }
  2142. module_init(savagefb_init);
  2143. module_exit(savage_done);
  2144. module_param(mode_option, charp, 0);
  2145. MODULE_PARM_DESC(mode_option, "Specify initial video mode");