savagefb_driver.c 63 KB

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