savagefb_driver.c 65 KB

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