savagefb_driver.c 64 KB

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