savagefb_driver.c 64 KB

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