fbdev.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199
  1. /*
  2. * linux/drivers/video/riva/fbdev.c - nVidia RIVA 128/TNT/TNT2 fb driver
  3. *
  4. * Maintained by Ani Joshi <ajoshi@shell.unixbox.com>
  5. *
  6. * Copyright 1999-2000 Jeff Garzik
  7. *
  8. * Contributors:
  9. *
  10. * Ani Joshi: Lots of debugging and cleanup work, really helped
  11. * get the driver going
  12. *
  13. * Ferenc Bakonyi: Bug fixes, cleanup, modularization
  14. *
  15. * Jindrich Makovicka: Accel code help, hw cursor, mtrr
  16. *
  17. * Paul Richards: Bug fixes, updates
  18. *
  19. * Initial template from skeletonfb.c, created 28 Dec 1997 by Geert Uytterhoeven
  20. * Includes riva_hw.c from nVidia, see copyright below.
  21. * KGI code provided the basis for state storage, init, and mode switching.
  22. *
  23. * This file is subject to the terms and conditions of the GNU General Public
  24. * License. See the file COPYING in the main directory of this archive
  25. * for more details.
  26. *
  27. * Known bugs and issues:
  28. * restoring text mode fails
  29. * doublescan modes are broken
  30. */
  31. #include <linux/aperture.h>
  32. #include <linux/module.h>
  33. #include <linux/kernel.h>
  34. #include <linux/errno.h>
  35. #include <linux/string.h>
  36. #include <linux/mm.h>
  37. #include <linux/slab.h>
  38. #include <linux/delay.h>
  39. #include <linux/fb.h>
  40. #include <linux/init.h>
  41. #include <linux/pci.h>
  42. #include <linux/backlight.h>
  43. #include <linux/bitrev.h>
  44. #ifdef CONFIG_PMAC_BACKLIGHT
  45. #include <asm/machdep.h>
  46. #include <asm/backlight.h>
  47. #endif
  48. #include "rivafb.h"
  49. #include "nvreg.h"
  50. /* version number of this driver */
  51. #define RIVAFB_VERSION "0.9.5b"
  52. /* ------------------------------------------------------------------------- *
  53. *
  54. * various helpful macros and constants
  55. *
  56. * ------------------------------------------------------------------------- */
  57. #ifdef CONFIG_FB_RIVA_DEBUG
  58. #define NVTRACE printk
  59. #else
  60. #define NVTRACE if(0) printk
  61. #endif
  62. #define NVTRACE_ENTER(...) NVTRACE("%s START\n", __func__)
  63. #define NVTRACE_LEAVE(...) NVTRACE("%s END\n", __func__)
  64. #ifdef CONFIG_FB_RIVA_DEBUG
  65. #define assert(expr) \
  66. if(!(expr)) { \
  67. printk( "Assertion failed! %s,%s,%s,line=%d\n",\
  68. #expr,__FILE__,__func__,__LINE__); \
  69. BUG(); \
  70. }
  71. #else
  72. #define assert(expr)
  73. #endif
  74. #define PFX "rivafb: "
  75. /* macro that allows you to set overflow bits */
  76. #define SetBitField(value,from,to) SetBF(to,GetBF(value,from))
  77. #define SetBit(n) (1<<(n))
  78. #define Set8Bits(value) ((value)&0xff)
  79. /* HW cursor parameters */
  80. #define MAX_CURS 32
  81. /* ------------------------------------------------------------------------- *
  82. *
  83. * prototypes
  84. *
  85. * ------------------------------------------------------------------------- */
  86. static int rivafb_blank(int blank, struct fb_info *info);
  87. /* ------------------------------------------------------------------------- *
  88. *
  89. * card identification
  90. *
  91. * ------------------------------------------------------------------------- */
  92. static const struct pci_device_id rivafb_pci_tbl[] = {
  93. { PCI_VENDOR_ID_NVIDIA_SGS, PCI_DEVICE_ID_NVIDIA_SGS_RIVA128,
  94. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  95. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_TNT,
  96. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  97. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_TNT2,
  98. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  99. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_UTNT2,
  100. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  101. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_VTNT2,
  102. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  103. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_UVTNT2,
  104. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  105. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_ITNT2,
  106. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  107. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE_SDR,
  108. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  109. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE_DDR,
  110. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  111. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO,
  112. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  113. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE2_MX,
  114. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  115. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE2_MX2,
  116. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  117. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE2_GO,
  118. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  119. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO2_MXR,
  120. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  121. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE2_GTS,
  122. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  123. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE2_GTS2,
  124. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  125. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE2_ULTRA,
  126. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  127. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO2_PRO,
  128. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  129. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_MX_460,
  130. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  131. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_MX_440,
  132. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  133. // NF2/IGP version, GeForce 4 MX, NV18
  134. { PCI_VENDOR_ID_NVIDIA, 0x01f0,
  135. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  136. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_MX_420,
  137. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  138. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_440_GO,
  139. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  140. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_420_GO,
  141. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  142. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_420_GO_M32,
  143. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  144. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO4_500XGL,
  145. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  146. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_440_GO_M64,
  147. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  148. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO4_200,
  149. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  150. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO4_550XGL,
  151. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  152. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO4_500_GOGL,
  153. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  154. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_IGEFORCE2,
  155. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  156. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE3,
  157. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  158. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE3_1,
  159. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  160. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE3_2,
  161. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  162. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO_DDC,
  163. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  164. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_TI_4600,
  165. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  166. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_TI_4400,
  167. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  168. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE4_TI_4200,
  169. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  170. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO4_900XGL,
  171. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  172. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO4_750XGL,
  173. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  174. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_QUADRO4_700XGL,
  175. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  176. { PCI_VENDOR_ID_NVIDIA, PCI_DEVICE_ID_NVIDIA_GEFORCE_FX_GO_5200,
  177. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  178. { 0, } /* terminate list */
  179. };
  180. MODULE_DEVICE_TABLE(pci, rivafb_pci_tbl);
  181. /* ------------------------------------------------------------------------- *
  182. *
  183. * global variables
  184. *
  185. * ------------------------------------------------------------------------- */
  186. /* command line data, set in rivafb_setup() */
  187. static int flatpanel = -1; /* Autodetect later */
  188. static int forceCRTC = -1;
  189. static bool noaccel = 0;
  190. static bool nomtrr = 0;
  191. static int backlight = IS_BUILTIN(CONFIG_PMAC_BACKLIGHT);
  192. static char *mode_option = NULL;
  193. static bool strictmode = 0;
  194. static struct fb_fix_screeninfo rivafb_fix = {
  195. .type = FB_TYPE_PACKED_PIXELS,
  196. .xpanstep = 1,
  197. .ypanstep = 1,
  198. };
  199. static struct fb_var_screeninfo rivafb_default_var = {
  200. .xres = 640,
  201. .yres = 480,
  202. .xres_virtual = 640,
  203. .yres_virtual = 480,
  204. .bits_per_pixel = 8,
  205. .red = {0, 8, 0},
  206. .green = {0, 8, 0},
  207. .blue = {0, 8, 0},
  208. .transp = {0, 0, 0},
  209. .activate = FB_ACTIVATE_NOW,
  210. .height = -1,
  211. .width = -1,
  212. .pixclock = 39721,
  213. .left_margin = 40,
  214. .right_margin = 24,
  215. .upper_margin = 32,
  216. .lower_margin = 11,
  217. .hsync_len = 96,
  218. .vsync_len = 2,
  219. .vmode = FB_VMODE_NONINTERLACED
  220. };
  221. /* from GGI */
  222. static const struct riva_regs reg_template = {
  223. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, /* ATTR */
  224. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  225. 0x41, 0x01, 0x0F, 0x00, 0x00},
  226. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* CRT */
  227. 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
  228. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE3, /* 0x10 */
  229. 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  230. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x20 */
  231. 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  232. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 0x30 */
  233. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  234. 0x00, /* 0x40 */
  235. },
  236. {0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x05, 0x0F, /* GRA */
  237. 0xFF},
  238. {0x03, 0x01, 0x0F, 0x00, 0x0E}, /* SEQ */
  239. 0xEB /* MISC */
  240. };
  241. /*
  242. * Backlight control
  243. */
  244. #ifdef CONFIG_FB_RIVA_BACKLIGHT
  245. /* We do not have any information about which values are allowed, thus
  246. * we used safe values.
  247. */
  248. #define MIN_LEVEL 0x158
  249. #define MAX_LEVEL 0x534
  250. #define LEVEL_STEP ((MAX_LEVEL - MIN_LEVEL) / FB_BACKLIGHT_MAX)
  251. static int riva_bl_get_level_brightness(struct riva_par *par,
  252. int level)
  253. {
  254. struct fb_info *info = pci_get_drvdata(par->pdev);
  255. int nlevel;
  256. /* Get and convert the value */
  257. /* No locking on bl_curve since accessing a single value */
  258. nlevel = MIN_LEVEL + info->bl_curve[level] * LEVEL_STEP;
  259. if (nlevel < 0)
  260. nlevel = 0;
  261. else if (nlevel < MIN_LEVEL)
  262. nlevel = MIN_LEVEL;
  263. else if (nlevel > MAX_LEVEL)
  264. nlevel = MAX_LEVEL;
  265. return nlevel;
  266. }
  267. static int riva_bl_update_status(struct backlight_device *bd)
  268. {
  269. struct riva_par *par = bl_get_data(bd);
  270. U032 tmp_pcrt, tmp_pmc;
  271. int level = backlight_get_brightness(bd);
  272. tmp_pmc = NV_RD32(par->riva.PMC, 0x10F0) & 0x0000FFFF;
  273. tmp_pcrt = NV_RD32(par->riva.PCRTC0, 0x081C) & 0xFFFFFFFC;
  274. if(level > 0) {
  275. tmp_pcrt |= 0x1;
  276. tmp_pmc |= (1 << 31); /* backlight bit */
  277. tmp_pmc |= riva_bl_get_level_brightness(par, level) << 16; /* level */
  278. }
  279. NV_WR32(par->riva.PCRTC0, 0x081C, tmp_pcrt);
  280. NV_WR32(par->riva.PMC, 0x10F0, tmp_pmc);
  281. return 0;
  282. }
  283. static const struct backlight_ops riva_bl_ops = {
  284. .update_status = riva_bl_update_status,
  285. };
  286. static void riva_bl_init(struct riva_par *par)
  287. {
  288. struct backlight_properties props;
  289. struct fb_info *info = pci_get_drvdata(par->pdev);
  290. struct backlight_device *bd;
  291. char name[12];
  292. if (!par->FlatPanel)
  293. return;
  294. #ifdef CONFIG_PMAC_BACKLIGHT
  295. if (!machine_is(powermac) ||
  296. !pmac_has_backlight_type("mnca"))
  297. return;
  298. #endif
  299. snprintf(name, sizeof(name), "rivabl%d", info->node);
  300. memset(&props, 0, sizeof(struct backlight_properties));
  301. props.type = BACKLIGHT_RAW;
  302. props.max_brightness = FB_BACKLIGHT_LEVELS - 1;
  303. bd = backlight_device_register(name, info->device, par, &riva_bl_ops,
  304. &props);
  305. if (IS_ERR(bd)) {
  306. info->bl_dev = NULL;
  307. printk(KERN_WARNING "riva: Backlight registration failed\n");
  308. goto error;
  309. }
  310. info->bl_dev = bd;
  311. fb_bl_default_curve(info, 0,
  312. MIN_LEVEL * FB_BACKLIGHT_MAX / MAX_LEVEL,
  313. FB_BACKLIGHT_MAX);
  314. bd->props.brightness = bd->props.max_brightness;
  315. bd->props.power = BACKLIGHT_POWER_ON;
  316. backlight_update_status(bd);
  317. printk("riva: Backlight initialized (%s)\n", name);
  318. return;
  319. error:
  320. return;
  321. }
  322. static void riva_bl_exit(struct fb_info *info)
  323. {
  324. struct backlight_device *bd = info->bl_dev;
  325. backlight_device_unregister(bd);
  326. printk("riva: Backlight unloaded\n");
  327. }
  328. #else
  329. static inline void riva_bl_init(struct riva_par *par) {}
  330. static inline void riva_bl_exit(struct fb_info *info) {}
  331. #endif /* CONFIG_FB_RIVA_BACKLIGHT */
  332. /* ------------------------------------------------------------------------- *
  333. *
  334. * MMIO access macros
  335. *
  336. * ------------------------------------------------------------------------- */
  337. static inline void CRTCout(struct riva_par *par, unsigned char index,
  338. unsigned char val)
  339. {
  340. VGA_WR08(par->riva.PCIO, 0x3d4, index);
  341. VGA_WR08(par->riva.PCIO, 0x3d5, val);
  342. }
  343. static inline unsigned char CRTCin(struct riva_par *par,
  344. unsigned char index)
  345. {
  346. VGA_WR08(par->riva.PCIO, 0x3d4, index);
  347. return (VGA_RD08(par->riva.PCIO, 0x3d5));
  348. }
  349. static inline void GRAout(struct riva_par *par, unsigned char index,
  350. unsigned char val)
  351. {
  352. VGA_WR08(par->riva.PVIO, 0x3ce, index);
  353. VGA_WR08(par->riva.PVIO, 0x3cf, val);
  354. }
  355. static inline unsigned char GRAin(struct riva_par *par,
  356. unsigned char index)
  357. {
  358. VGA_WR08(par->riva.PVIO, 0x3ce, index);
  359. return (VGA_RD08(par->riva.PVIO, 0x3cf));
  360. }
  361. static inline void SEQout(struct riva_par *par, unsigned char index,
  362. unsigned char val)
  363. {
  364. VGA_WR08(par->riva.PVIO, 0x3c4, index);
  365. VGA_WR08(par->riva.PVIO, 0x3c5, val);
  366. }
  367. static inline unsigned char SEQin(struct riva_par *par,
  368. unsigned char index)
  369. {
  370. VGA_WR08(par->riva.PVIO, 0x3c4, index);
  371. return (VGA_RD08(par->riva.PVIO, 0x3c5));
  372. }
  373. static inline void ATTRout(struct riva_par *par, unsigned char index,
  374. unsigned char val)
  375. {
  376. VGA_WR08(par->riva.PCIO, 0x3c0, index);
  377. VGA_WR08(par->riva.PCIO, 0x3c0, val);
  378. }
  379. static inline unsigned char ATTRin(struct riva_par *par,
  380. unsigned char index)
  381. {
  382. VGA_WR08(par->riva.PCIO, 0x3c0, index);
  383. return (VGA_RD08(par->riva.PCIO, 0x3c1));
  384. }
  385. static inline void MISCout(struct riva_par *par, unsigned char val)
  386. {
  387. VGA_WR08(par->riva.PVIO, 0x3c2, val);
  388. }
  389. static inline unsigned char MISCin(struct riva_par *par)
  390. {
  391. return (VGA_RD08(par->riva.PVIO, 0x3cc));
  392. }
  393. static inline void reverse_order(u32 *l)
  394. {
  395. u8 *a = (u8 *)l;
  396. a[0] = bitrev8(a[0]);
  397. a[1] = bitrev8(a[1]);
  398. a[2] = bitrev8(a[2]);
  399. a[3] = bitrev8(a[3]);
  400. }
  401. /* ------------------------------------------------------------------------- *
  402. *
  403. * cursor stuff
  404. *
  405. * ------------------------------------------------------------------------- */
  406. /**
  407. * rivafb_load_cursor_image - load cursor image to hardware
  408. * @data8: address to monochrome bitmap (1 = foreground color, 0 = background)
  409. * @par: pointer to private data
  410. * @w: width of cursor image in pixels
  411. * @h: height of cursor image in scanlines
  412. * @bg: background color (ARGB1555) - alpha bit determines opacity
  413. * @fg: foreground color (ARGB1555)
  414. *
  415. * DESCRIPTiON:
  416. * Loads cursor image based on a monochrome source and mask bitmap. The
  417. * image bits determines the color of the pixel, 0 for background, 1 for
  418. * foreground. Only the affected region (as determined by @w and @h
  419. * parameters) will be updated.
  420. *
  421. * CALLED FROM:
  422. * rivafb_cursor()
  423. */
  424. static void rivafb_load_cursor_image(struct riva_par *par, u8 *data8,
  425. u16 bg, u16 fg, u32 w, u32 h)
  426. {
  427. int i, j, k = 0;
  428. u32 b, tmp;
  429. u32 *data = (u32 *)data8;
  430. bg = le16_to_cpu(bg);
  431. fg = le16_to_cpu(fg);
  432. w = (w + 1) & ~1;
  433. for (i = 0; i < h; i++) {
  434. b = *data++;
  435. reverse_order(&b);
  436. for (j = 0; j < w/2; j++) {
  437. tmp = 0;
  438. #if defined (__BIG_ENDIAN)
  439. tmp = (b & (1 << 31)) ? fg << 16 : bg << 16;
  440. b <<= 1;
  441. tmp |= (b & (1 << 31)) ? fg : bg;
  442. b <<= 1;
  443. #else
  444. tmp = (b & 1) ? fg : bg;
  445. b >>= 1;
  446. tmp |= (b & 1) ? fg << 16 : bg << 16;
  447. b >>= 1;
  448. #endif
  449. writel(tmp, &par->riva.CURSOR[k++]);
  450. }
  451. k += (MAX_CURS - w)/2;
  452. }
  453. }
  454. /* ------------------------------------------------------------------------- *
  455. *
  456. * general utility functions
  457. *
  458. * ------------------------------------------------------------------------- */
  459. /**
  460. * riva_wclut - set CLUT entry
  461. * @chip: pointer to RIVA_HW_INST object
  462. * @regnum: register number
  463. * @red: red component
  464. * @green: green component
  465. * @blue: blue component
  466. *
  467. * DESCRIPTION:
  468. * Sets color register @regnum.
  469. *
  470. * CALLED FROM:
  471. * rivafb_setcolreg()
  472. */
  473. static void riva_wclut(RIVA_HW_INST *chip,
  474. unsigned char regnum, unsigned char red,
  475. unsigned char green, unsigned char blue)
  476. {
  477. VGA_WR08(chip->PDIO, 0x3c8, regnum);
  478. VGA_WR08(chip->PDIO, 0x3c9, red);
  479. VGA_WR08(chip->PDIO, 0x3c9, green);
  480. VGA_WR08(chip->PDIO, 0x3c9, blue);
  481. }
  482. /**
  483. * riva_rclut - read fromCLUT register
  484. * @chip: pointer to RIVA_HW_INST object
  485. * @regnum: register number
  486. * @red: red component
  487. * @green: green component
  488. * @blue: blue component
  489. *
  490. * DESCRIPTION:
  491. * Reads red, green, and blue from color register @regnum.
  492. *
  493. * CALLED FROM:
  494. * rivafb_setcolreg()
  495. */
  496. static void riva_rclut(RIVA_HW_INST *chip,
  497. unsigned char regnum, unsigned char *red,
  498. unsigned char *green, unsigned char *blue)
  499. {
  500. VGA_WR08(chip->PDIO, 0x3c7, regnum);
  501. *red = VGA_RD08(chip->PDIO, 0x3c9);
  502. *green = VGA_RD08(chip->PDIO, 0x3c9);
  503. *blue = VGA_RD08(chip->PDIO, 0x3c9);
  504. }
  505. /**
  506. * riva_save_state - saves current chip state
  507. * @par: pointer to riva_par object containing info for current riva board
  508. * @regs: pointer to riva_regs object
  509. *
  510. * DESCRIPTION:
  511. * Saves current chip state to @regs.
  512. *
  513. * CALLED FROM:
  514. * rivafb_probe()
  515. */
  516. /* from GGI */
  517. static void riva_save_state(struct riva_par *par, struct riva_regs *regs)
  518. {
  519. int i;
  520. NVTRACE_ENTER();
  521. par->riva.LockUnlock(&par->riva, 0);
  522. par->riva.UnloadStateExt(&par->riva, &regs->ext);
  523. regs->misc_output = MISCin(par);
  524. for (i = 0; i < NUM_CRT_REGS; i++)
  525. regs->crtc[i] = CRTCin(par, i);
  526. for (i = 0; i < NUM_ATC_REGS; i++)
  527. regs->attr[i] = ATTRin(par, i);
  528. for (i = 0; i < NUM_GRC_REGS; i++)
  529. regs->gra[i] = GRAin(par, i);
  530. for (i = 0; i < NUM_SEQ_REGS; i++)
  531. regs->seq[i] = SEQin(par, i);
  532. NVTRACE_LEAVE();
  533. }
  534. /**
  535. * riva_load_state - loads current chip state
  536. * @par: pointer to riva_par object containing info for current riva board
  537. * @regs: pointer to riva_regs object
  538. *
  539. * DESCRIPTION:
  540. * Loads chip state from @regs.
  541. *
  542. * CALLED FROM:
  543. * riva_load_video_mode()
  544. * rivafb_probe()
  545. * rivafb_remove()
  546. */
  547. /* from GGI */
  548. static void riva_load_state(struct riva_par *par, struct riva_regs *regs)
  549. {
  550. RIVA_HW_STATE *state = &regs->ext;
  551. int i;
  552. NVTRACE_ENTER();
  553. CRTCout(par, 0x11, 0x00);
  554. par->riva.LockUnlock(&par->riva, 0);
  555. par->riva.LoadStateExt(&par->riva, state);
  556. MISCout(par, regs->misc_output);
  557. for (i = 0; i < NUM_CRT_REGS; i++) {
  558. switch (i) {
  559. case 0x19:
  560. case 0x20 ... 0x40:
  561. break;
  562. default:
  563. CRTCout(par, i, regs->crtc[i]);
  564. }
  565. }
  566. for (i = 0; i < NUM_ATC_REGS; i++)
  567. ATTRout(par, i, regs->attr[i]);
  568. for (i = 0; i < NUM_GRC_REGS; i++)
  569. GRAout(par, i, regs->gra[i]);
  570. for (i = 0; i < NUM_SEQ_REGS; i++)
  571. SEQout(par, i, regs->seq[i]);
  572. NVTRACE_LEAVE();
  573. }
  574. /**
  575. * riva_load_video_mode - calculate timings
  576. * @info: pointer to fb_info object containing info for current riva board
  577. *
  578. * DESCRIPTION:
  579. * Calculate some timings and then send em off to riva_load_state().
  580. *
  581. * CALLED FROM:
  582. * rivafb_set_par()
  583. */
  584. static int riva_load_video_mode(struct fb_info *info)
  585. {
  586. int bpp, width, hDisplaySize, hDisplay, hStart,
  587. hEnd, hTotal, height, vDisplay, vStart, vEnd, vTotal, dotClock;
  588. int hBlankStart, hBlankEnd, vBlankStart, vBlankEnd;
  589. int rc;
  590. struct riva_par *par = info->par;
  591. struct riva_regs newmode;
  592. NVTRACE_ENTER();
  593. /* time to calculate */
  594. rivafb_blank(FB_BLANK_NORMAL, info);
  595. bpp = info->var.bits_per_pixel;
  596. if (bpp == 16 && info->var.green.length == 5)
  597. bpp = 15;
  598. width = info->var.xres_virtual;
  599. hDisplaySize = info->var.xres;
  600. hDisplay = (hDisplaySize / 8) - 1;
  601. hStart = (hDisplaySize + info->var.right_margin) / 8 - 1;
  602. hEnd = (hDisplaySize + info->var.right_margin +
  603. info->var.hsync_len) / 8 - 1;
  604. hTotal = (hDisplaySize + info->var.right_margin +
  605. info->var.hsync_len + info->var.left_margin) / 8 - 5;
  606. hBlankStart = hDisplay;
  607. hBlankEnd = hTotal + 4;
  608. height = info->var.yres_virtual;
  609. vDisplay = info->var.yres - 1;
  610. vStart = info->var.yres + info->var.lower_margin - 1;
  611. vEnd = info->var.yres + info->var.lower_margin +
  612. info->var.vsync_len - 1;
  613. vTotal = info->var.yres + info->var.lower_margin +
  614. info->var.vsync_len + info->var.upper_margin + 2;
  615. vBlankStart = vDisplay;
  616. vBlankEnd = vTotal + 1;
  617. dotClock = 1000000000 / info->var.pixclock;
  618. memcpy(&newmode, &reg_template, sizeof(struct riva_regs));
  619. if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED)
  620. vTotal |= 1;
  621. if (par->FlatPanel) {
  622. vStart = vTotal - 3;
  623. vEnd = vTotal - 2;
  624. vBlankStart = vStart;
  625. hStart = hTotal - 3;
  626. hEnd = hTotal - 2;
  627. hBlankEnd = hTotal + 4;
  628. }
  629. newmode.crtc[0x0] = Set8Bits (hTotal);
  630. newmode.crtc[0x1] = Set8Bits (hDisplay);
  631. newmode.crtc[0x2] = Set8Bits (hBlankStart);
  632. newmode.crtc[0x3] = SetBitField (hBlankEnd, 4: 0, 4:0) | SetBit (7);
  633. newmode.crtc[0x4] = Set8Bits (hStart);
  634. newmode.crtc[0x5] = SetBitField (hBlankEnd, 5: 5, 7:7)
  635. | SetBitField (hEnd, 4: 0, 4:0);
  636. newmode.crtc[0x6] = SetBitField (vTotal, 7: 0, 7:0);
  637. newmode.crtc[0x7] = SetBitField (vTotal, 8: 8, 0:0)
  638. | SetBitField (vDisplay, 8: 8, 1:1)
  639. | SetBitField (vStart, 8: 8, 2:2)
  640. | SetBitField (vBlankStart, 8: 8, 3:3)
  641. | SetBit (4)
  642. | SetBitField (vTotal, 9: 9, 5:5)
  643. | SetBitField (vDisplay, 9: 9, 6:6)
  644. | SetBitField (vStart, 9: 9, 7:7);
  645. newmode.crtc[0x9] = SetBitField (vBlankStart, 9: 9, 5:5)
  646. | SetBit (6);
  647. newmode.crtc[0x10] = Set8Bits (vStart);
  648. newmode.crtc[0x11] = SetBitField (vEnd, 3: 0, 3:0)
  649. | SetBit (5);
  650. newmode.crtc[0x12] = Set8Bits (vDisplay);
  651. newmode.crtc[0x13] = (width / 8) * ((bpp + 1) / 8);
  652. newmode.crtc[0x15] = Set8Bits (vBlankStart);
  653. newmode.crtc[0x16] = Set8Bits (vBlankEnd);
  654. newmode.ext.screen = SetBitField(hBlankEnd,6:6,4:4)
  655. | SetBitField(vBlankStart,10:10,3:3)
  656. | SetBitField(vStart,10:10,2:2)
  657. | SetBitField(vDisplay,10:10,1:1)
  658. | SetBitField(vTotal,10:10,0:0);
  659. newmode.ext.horiz = SetBitField(hTotal,8:8,0:0)
  660. | SetBitField(hDisplay,8:8,1:1)
  661. | SetBitField(hBlankStart,8:8,2:2)
  662. | SetBitField(hStart,8:8,3:3);
  663. newmode.ext.extra = SetBitField(vTotal,11:11,0:0)
  664. | SetBitField(vDisplay,11:11,2:2)
  665. | SetBitField(vStart,11:11,4:4)
  666. | SetBitField(vBlankStart,11:11,6:6);
  667. if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED) {
  668. int tmp = (hTotal >> 1) & ~1;
  669. newmode.ext.interlace = Set8Bits(tmp);
  670. newmode.ext.horiz |= SetBitField(tmp, 8:8,4:4);
  671. } else
  672. newmode.ext.interlace = 0xff; /* interlace off */
  673. if (par->riva.Architecture >= NV_ARCH_10)
  674. par->riva.CURSOR = (U032 __iomem *)(info->screen_base + par->riva.CursorStart);
  675. if (info->var.sync & FB_SYNC_HOR_HIGH_ACT)
  676. newmode.misc_output &= ~0x40;
  677. else
  678. newmode.misc_output |= 0x40;
  679. if (info->var.sync & FB_SYNC_VERT_HIGH_ACT)
  680. newmode.misc_output &= ~0x80;
  681. else
  682. newmode.misc_output |= 0x80;
  683. rc = CalcStateExt(&par->riva, &newmode.ext, par->pdev, bpp, width,
  684. hDisplaySize, height, dotClock);
  685. if (rc)
  686. goto out;
  687. newmode.ext.scale = NV_RD32(par->riva.PRAMDAC, 0x00000848) &
  688. 0xfff000ff;
  689. if (par->FlatPanel == 1) {
  690. newmode.ext.pixel |= (1 << 7);
  691. newmode.ext.scale |= (1 << 8);
  692. }
  693. if (par->SecondCRTC) {
  694. newmode.ext.head = NV_RD32(par->riva.PCRTC0, 0x00000860) &
  695. ~0x00001000;
  696. newmode.ext.head2 = NV_RD32(par->riva.PCRTC0, 0x00002860) |
  697. 0x00001000;
  698. newmode.ext.crtcOwner = 3;
  699. newmode.ext.pllsel |= 0x20000800;
  700. newmode.ext.vpll2 = newmode.ext.vpll;
  701. } else if (par->riva.twoHeads) {
  702. newmode.ext.head = NV_RD32(par->riva.PCRTC0, 0x00000860) |
  703. 0x00001000;
  704. newmode.ext.head2 = NV_RD32(par->riva.PCRTC0, 0x00002860) &
  705. ~0x00001000;
  706. newmode.ext.crtcOwner = 0;
  707. newmode.ext.vpll2 = NV_RD32(par->riva.PRAMDAC0, 0x00000520);
  708. }
  709. if (par->FlatPanel == 1) {
  710. newmode.ext.pixel |= (1 << 7);
  711. newmode.ext.scale |= (1 << 8);
  712. }
  713. newmode.ext.cursorConfig = 0x02000100;
  714. par->current_state = newmode;
  715. riva_load_state(par, &par->current_state);
  716. par->riva.LockUnlock(&par->riva, 0); /* important for HW cursor */
  717. out:
  718. rivafb_blank(FB_BLANK_UNBLANK, info);
  719. NVTRACE_LEAVE();
  720. return rc;
  721. }
  722. static void riva_update_var(struct fb_var_screeninfo *var,
  723. const struct fb_videomode *modedb)
  724. {
  725. NVTRACE_ENTER();
  726. var->xres = var->xres_virtual = modedb->xres;
  727. var->yres = modedb->yres;
  728. if (var->yres_virtual < var->yres)
  729. var->yres_virtual = var->yres;
  730. var->xoffset = var->yoffset = 0;
  731. var->pixclock = modedb->pixclock;
  732. var->left_margin = modedb->left_margin;
  733. var->right_margin = modedb->right_margin;
  734. var->upper_margin = modedb->upper_margin;
  735. var->lower_margin = modedb->lower_margin;
  736. var->hsync_len = modedb->hsync_len;
  737. var->vsync_len = modedb->vsync_len;
  738. var->sync = modedb->sync;
  739. var->vmode = modedb->vmode;
  740. NVTRACE_LEAVE();
  741. }
  742. /**
  743. * rivafb_do_maximize -
  744. * @info: pointer to fb_info object containing info for current riva board
  745. * @var: standard kernel fb changeable data
  746. * @nom: nom
  747. * @den: den
  748. *
  749. * DESCRIPTION:
  750. * .
  751. *
  752. * RETURNS:
  753. * -EINVAL on failure, 0 on success
  754. *
  755. *
  756. * CALLED FROM:
  757. * rivafb_check_var()
  758. */
  759. static int rivafb_do_maximize(struct fb_info *info,
  760. struct fb_var_screeninfo *var,
  761. int nom, int den)
  762. {
  763. static struct {
  764. int xres, yres;
  765. } modes[] = {
  766. {1600, 1280},
  767. {1280, 1024},
  768. {1024, 768},
  769. {800, 600},
  770. {640, 480},
  771. {-1, -1}
  772. };
  773. int i;
  774. NVTRACE_ENTER();
  775. /* use highest possible virtual resolution */
  776. if (var->xres_virtual == -1 && var->yres_virtual == -1) {
  777. printk(KERN_WARNING PFX
  778. "using maximum available virtual resolution\n");
  779. for (i = 0; modes[i].xres != -1; i++) {
  780. if (modes[i].xres * nom / den * modes[i].yres <
  781. info->fix.smem_len)
  782. break;
  783. }
  784. if (modes[i].xres == -1) {
  785. printk(KERN_ERR PFX
  786. "could not find a virtual resolution that fits into video memory!!\n");
  787. NVTRACE("EXIT - EINVAL error\n");
  788. return -EINVAL;
  789. }
  790. var->xres_virtual = modes[i].xres;
  791. var->yres_virtual = modes[i].yres;
  792. printk(KERN_INFO PFX
  793. "virtual resolution set to maximum of %dx%d\n",
  794. var->xres_virtual, var->yres_virtual);
  795. } else if (var->xres_virtual == -1) {
  796. var->xres_virtual = (info->fix.smem_len * den /
  797. (nom * var->yres_virtual)) & ~15;
  798. printk(KERN_WARNING PFX
  799. "setting virtual X resolution to %d\n", var->xres_virtual);
  800. } else if (var->yres_virtual == -1) {
  801. var->xres_virtual = (var->xres_virtual + 15) & ~15;
  802. var->yres_virtual = info->fix.smem_len * den /
  803. (nom * var->xres_virtual);
  804. printk(KERN_WARNING PFX
  805. "setting virtual Y resolution to %d\n", var->yres_virtual);
  806. } else {
  807. var->xres_virtual = (var->xres_virtual + 15) & ~15;
  808. if (var->xres_virtual * nom / den * var->yres_virtual > info->fix.smem_len) {
  809. printk(KERN_ERR PFX
  810. "mode %dx%dx%d rejected...resolution too high to fit into video memory!\n",
  811. var->xres, var->yres, var->bits_per_pixel);
  812. NVTRACE("EXIT - EINVAL error\n");
  813. return -EINVAL;
  814. }
  815. }
  816. if (var->xres_virtual * nom / den >= 8192) {
  817. printk(KERN_WARNING PFX
  818. "virtual X resolution (%d) is too high, lowering to %d\n",
  819. var->xres_virtual, 8192 * den / nom - 16);
  820. var->xres_virtual = 8192 * den / nom - 16;
  821. }
  822. if (var->xres_virtual < var->xres) {
  823. printk(KERN_ERR PFX
  824. "virtual X resolution (%d) is smaller than real\n", var->xres_virtual);
  825. return -EINVAL;
  826. }
  827. if (var->yres_virtual < var->yres) {
  828. printk(KERN_ERR PFX
  829. "virtual Y resolution (%d) is smaller than real\n", var->yres_virtual);
  830. return -EINVAL;
  831. }
  832. if (var->yres_virtual > 0x7fff/nom)
  833. var->yres_virtual = 0x7fff/nom;
  834. if (var->xres_virtual > 0x7fff/nom)
  835. var->xres_virtual = 0x7fff/nom;
  836. NVTRACE_LEAVE();
  837. return 0;
  838. }
  839. static void
  840. riva_set_pattern(struct riva_par *par, int clr0, int clr1, int pat0, int pat1)
  841. {
  842. RIVA_FIFO_FREE(par->riva, Patt, 4);
  843. NV_WR32(&par->riva.Patt->Color0, 0, clr0);
  844. NV_WR32(&par->riva.Patt->Color1, 0, clr1);
  845. NV_WR32(par->riva.Patt->Monochrome, 0, pat0);
  846. NV_WR32(par->riva.Patt->Monochrome, 4, pat1);
  847. }
  848. /* acceleration routines */
  849. static inline void wait_for_idle(struct riva_par *par)
  850. {
  851. while (par->riva.Busy(&par->riva));
  852. }
  853. /*
  854. * Set ROP. Translate X rop into ROP3. Internal routine.
  855. */
  856. static void
  857. riva_set_rop_solid(struct riva_par *par, int rop)
  858. {
  859. riva_set_pattern(par, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
  860. RIVA_FIFO_FREE(par->riva, Rop, 1);
  861. NV_WR32(&par->riva.Rop->Rop3, 0, rop);
  862. }
  863. static void riva_setup_accel(struct fb_info *info)
  864. {
  865. struct riva_par *par = info->par;
  866. RIVA_FIFO_FREE(par->riva, Clip, 2);
  867. NV_WR32(&par->riva.Clip->TopLeft, 0, 0x0);
  868. NV_WR32(&par->riva.Clip->WidthHeight, 0,
  869. (info->var.xres_virtual & 0xffff) |
  870. (info->var.yres_virtual << 16));
  871. riva_set_rop_solid(par, 0xcc);
  872. wait_for_idle(par);
  873. }
  874. /**
  875. * riva_get_cmap_len - query current color map length
  876. * @var: standard kernel fb changeable data
  877. *
  878. * DESCRIPTION:
  879. * Get current color map length.
  880. *
  881. * RETURNS:
  882. * Length of color map
  883. *
  884. * CALLED FROM:
  885. * rivafb_setcolreg()
  886. */
  887. static int riva_get_cmap_len(const struct fb_var_screeninfo *var)
  888. {
  889. int rc = 256; /* reasonable default */
  890. switch (var->green.length) {
  891. case 8:
  892. rc = 256; /* 256 entries (2^8), 8 bpp and RGB8888 */
  893. break;
  894. case 5:
  895. rc = 32; /* 32 entries (2^5), 16 bpp, RGB555 */
  896. break;
  897. case 6:
  898. rc = 64; /* 64 entries (2^6), 16 bpp, RGB565 */
  899. break;
  900. default:
  901. /* should not occur */
  902. break;
  903. }
  904. return rc;
  905. }
  906. /* ------------------------------------------------------------------------- *
  907. *
  908. * framebuffer operations
  909. *
  910. * ------------------------------------------------------------------------- */
  911. static int rivafb_open(struct fb_info *info, int user)
  912. {
  913. struct riva_par *par = info->par;
  914. NVTRACE_ENTER();
  915. mutex_lock(&par->open_lock);
  916. if (!par->ref_count) {
  917. #ifdef CONFIG_X86
  918. memset(&par->state, 0, sizeof(struct vgastate));
  919. par->state.flags = VGA_SAVE_MODE | VGA_SAVE_FONTS;
  920. /* save the DAC for Riva128 */
  921. if (par->riva.Architecture == NV_ARCH_03)
  922. par->state.flags |= VGA_SAVE_CMAP;
  923. save_vga(&par->state);
  924. #endif
  925. /* vgaHWunlock() + riva unlock (0x7F) */
  926. CRTCout(par, 0x11, 0xFF);
  927. par->riva.LockUnlock(&par->riva, 0);
  928. riva_save_state(par, &par->initial_state);
  929. }
  930. par->ref_count++;
  931. mutex_unlock(&par->open_lock);
  932. NVTRACE_LEAVE();
  933. return 0;
  934. }
  935. static int rivafb_release(struct fb_info *info, int user)
  936. {
  937. struct riva_par *par = info->par;
  938. NVTRACE_ENTER();
  939. mutex_lock(&par->open_lock);
  940. if (!par->ref_count) {
  941. mutex_unlock(&par->open_lock);
  942. return -EINVAL;
  943. }
  944. if (par->ref_count == 1) {
  945. par->riva.LockUnlock(&par->riva, 0);
  946. par->riva.LoadStateExt(&par->riva, &par->initial_state.ext);
  947. riva_load_state(par, &par->initial_state);
  948. #ifdef CONFIG_X86
  949. restore_vga(&par->state);
  950. #endif
  951. par->riva.LockUnlock(&par->riva, 1);
  952. }
  953. par->ref_count--;
  954. mutex_unlock(&par->open_lock);
  955. NVTRACE_LEAVE();
  956. return 0;
  957. }
  958. static int rivafb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  959. {
  960. const struct fb_videomode *mode;
  961. struct riva_par *par = info->par;
  962. int nom, den; /* translating from pixels->bytes */
  963. int mode_valid = 0;
  964. NVTRACE_ENTER();
  965. if (!var->pixclock)
  966. return -EINVAL;
  967. switch (var->bits_per_pixel) {
  968. case 1 ... 8:
  969. var->red.offset = var->green.offset = var->blue.offset = 0;
  970. var->red.length = var->green.length = var->blue.length = 8;
  971. var->bits_per_pixel = 8;
  972. nom = den = 1;
  973. break;
  974. case 9 ... 15:
  975. var->green.length = 5;
  976. fallthrough;
  977. case 16:
  978. var->bits_per_pixel = 16;
  979. /* The Riva128 supports RGB555 only */
  980. if (par->riva.Architecture == NV_ARCH_03)
  981. var->green.length = 5;
  982. if (var->green.length == 5) {
  983. /* 0rrrrrgg gggbbbbb */
  984. var->red.offset = 10;
  985. var->green.offset = 5;
  986. var->blue.offset = 0;
  987. var->red.length = 5;
  988. var->green.length = 5;
  989. var->blue.length = 5;
  990. } else {
  991. /* rrrrrggg gggbbbbb */
  992. var->red.offset = 11;
  993. var->green.offset = 5;
  994. var->blue.offset = 0;
  995. var->red.length = 5;
  996. var->green.length = 6;
  997. var->blue.length = 5;
  998. }
  999. nom = 2;
  1000. den = 1;
  1001. break;
  1002. case 17 ... 32:
  1003. var->red.length = var->green.length = var->blue.length = 8;
  1004. var->bits_per_pixel = 32;
  1005. var->red.offset = 16;
  1006. var->green.offset = 8;
  1007. var->blue.offset = 0;
  1008. nom = 4;
  1009. den = 1;
  1010. break;
  1011. default:
  1012. printk(KERN_ERR PFX
  1013. "mode %dx%dx%d rejected...color depth not supported.\n",
  1014. var->xres, var->yres, var->bits_per_pixel);
  1015. NVTRACE("EXIT, returning -EINVAL\n");
  1016. return -EINVAL;
  1017. }
  1018. if (!strictmode) {
  1019. if (!info->monspecs.vfmax || !info->monspecs.hfmax ||
  1020. !info->monspecs.dclkmax || !fb_validate_mode(var, info))
  1021. mode_valid = 1;
  1022. }
  1023. /* calculate modeline if supported by monitor */
  1024. if (!mode_valid && info->monspecs.gtf) {
  1025. if (!fb_get_mode(FB_MAXTIMINGS, 0, var, info))
  1026. mode_valid = 1;
  1027. }
  1028. if (!mode_valid) {
  1029. mode = fb_find_best_mode(var, &info->modelist);
  1030. if (mode) {
  1031. riva_update_var(var, mode);
  1032. mode_valid = 1;
  1033. }
  1034. }
  1035. if (!mode_valid && info->monspecs.modedb_len)
  1036. return -EINVAL;
  1037. if (var->xres_virtual < var->xres)
  1038. var->xres_virtual = var->xres;
  1039. if (var->yres_virtual <= var->yres)
  1040. var->yres_virtual = -1;
  1041. if (rivafb_do_maximize(info, var, nom, den) < 0)
  1042. return -EINVAL;
  1043. /* truncate xoffset and yoffset to maximum if too high */
  1044. if (var->xoffset > var->xres_virtual - var->xres)
  1045. var->xoffset = var->xres_virtual - var->xres - 1;
  1046. if (var->yoffset > var->yres_virtual - var->yres)
  1047. var->yoffset = var->yres_virtual - var->yres - 1;
  1048. var->red.msb_right =
  1049. var->green.msb_right =
  1050. var->blue.msb_right =
  1051. var->transp.offset = var->transp.length = var->transp.msb_right = 0;
  1052. NVTRACE_LEAVE();
  1053. return 0;
  1054. }
  1055. static int rivafb_set_par(struct fb_info *info)
  1056. {
  1057. struct riva_par *par = info->par;
  1058. int rc = 0;
  1059. NVTRACE_ENTER();
  1060. /* vgaHWunlock() + riva unlock (0x7F) */
  1061. CRTCout(par, 0x11, 0xFF);
  1062. par->riva.LockUnlock(&par->riva, 0);
  1063. rc = riva_load_video_mode(info);
  1064. if (rc)
  1065. goto out;
  1066. if(!(info->flags & FBINFO_HWACCEL_DISABLED))
  1067. riva_setup_accel(info);
  1068. par->cursor_reset = 1;
  1069. info->fix.line_length = (info->var.xres_virtual * (info->var.bits_per_pixel >> 3));
  1070. info->fix.visual = (info->var.bits_per_pixel == 8) ?
  1071. FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_DIRECTCOLOR;
  1072. if (info->flags & FBINFO_HWACCEL_DISABLED)
  1073. info->pixmap.scan_align = 1;
  1074. else
  1075. info->pixmap.scan_align = 4;
  1076. out:
  1077. NVTRACE_LEAVE();
  1078. return rc;
  1079. }
  1080. /**
  1081. * rivafb_pan_display
  1082. * @var: standard kernel fb changeable data
  1083. * @info: pointer to fb_info object containing info for current riva board
  1084. *
  1085. * DESCRIPTION:
  1086. * Pan (or wrap, depending on the `vmode' field) the display using the
  1087. * `xoffset' and `yoffset' fields of the `var' structure.
  1088. * If the values don't fit, return -EINVAL.
  1089. *
  1090. * This call looks only at xoffset, yoffset and the FB_VMODE_YWRAP flag
  1091. */
  1092. static int rivafb_pan_display(struct fb_var_screeninfo *var,
  1093. struct fb_info *info)
  1094. {
  1095. struct riva_par *par = info->par;
  1096. unsigned int base;
  1097. NVTRACE_ENTER();
  1098. base = var->yoffset * info->fix.line_length + var->xoffset;
  1099. par->riva.SetStartAddress(&par->riva, base);
  1100. NVTRACE_LEAVE();
  1101. return 0;
  1102. }
  1103. static int rivafb_blank(int blank, struct fb_info *info)
  1104. {
  1105. struct riva_par *par= info->par;
  1106. unsigned char tmp, vesa;
  1107. tmp = SEQin(par, 0x01) & ~0x20; /* screen on/off */
  1108. vesa = CRTCin(par, 0x1a) & ~0xc0; /* sync on/off */
  1109. NVTRACE_ENTER();
  1110. if (blank)
  1111. tmp |= 0x20;
  1112. switch (blank) {
  1113. case FB_BLANK_UNBLANK:
  1114. case FB_BLANK_NORMAL:
  1115. break;
  1116. case FB_BLANK_VSYNC_SUSPEND:
  1117. vesa |= 0x80;
  1118. break;
  1119. case FB_BLANK_HSYNC_SUSPEND:
  1120. vesa |= 0x40;
  1121. break;
  1122. case FB_BLANK_POWERDOWN:
  1123. vesa |= 0xc0;
  1124. break;
  1125. }
  1126. SEQout(par, 0x01, tmp);
  1127. CRTCout(par, 0x1a, vesa);
  1128. NVTRACE_LEAVE();
  1129. return 0;
  1130. }
  1131. /**
  1132. * rivafb_setcolreg
  1133. * @regno: register index
  1134. * @red: red component
  1135. * @green: green component
  1136. * @blue: blue component
  1137. * @transp: transparency
  1138. * @info: pointer to fb_info object containing info for current riva board
  1139. *
  1140. * DESCRIPTION:
  1141. * Set a single color register. The values supplied have a 16 bit
  1142. * magnitude.
  1143. *
  1144. * RETURNS:
  1145. * Return != 0 for invalid regno.
  1146. *
  1147. * CALLED FROM:
  1148. * fbcmap.c:fb_set_cmap()
  1149. */
  1150. static int rivafb_setcolreg(unsigned regno, unsigned red, unsigned green,
  1151. unsigned blue, unsigned transp,
  1152. struct fb_info *info)
  1153. {
  1154. struct riva_par *par = info->par;
  1155. RIVA_HW_INST *chip = &par->riva;
  1156. int i;
  1157. if (regno >= riva_get_cmap_len(&info->var))
  1158. return -EINVAL;
  1159. if (info->var.grayscale) {
  1160. /* gray = 0.30*R + 0.59*G + 0.11*B */
  1161. red = green = blue =
  1162. (red * 77 + green * 151 + blue * 28) >> 8;
  1163. }
  1164. if (regno < 16 && info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
  1165. ((u32 *) info->pseudo_palette)[regno] =
  1166. (regno << info->var.red.offset) |
  1167. (regno << info->var.green.offset) |
  1168. (regno << info->var.blue.offset);
  1169. /*
  1170. * The Riva128 2D engine requires color information in
  1171. * TrueColor format even if framebuffer is in DirectColor
  1172. */
  1173. if (par->riva.Architecture == NV_ARCH_03) {
  1174. switch (info->var.bits_per_pixel) {
  1175. case 16:
  1176. par->palette[regno] = ((red & 0xf800) >> 1) |
  1177. ((green & 0xf800) >> 6) |
  1178. ((blue & 0xf800) >> 11);
  1179. break;
  1180. case 32:
  1181. par->palette[regno] = ((red & 0xff00) << 8) |
  1182. ((green & 0xff00)) |
  1183. ((blue & 0xff00) >> 8);
  1184. break;
  1185. }
  1186. }
  1187. }
  1188. switch (info->var.bits_per_pixel) {
  1189. case 8:
  1190. /* "transparent" stuff is completely ignored. */
  1191. riva_wclut(chip, regno, red >> 8, green >> 8, blue >> 8);
  1192. break;
  1193. case 16:
  1194. if (info->var.green.length == 5) {
  1195. for (i = 0; i < 8; i++) {
  1196. riva_wclut(chip, regno*8+i, red >> 8,
  1197. green >> 8, blue >> 8);
  1198. }
  1199. } else {
  1200. u8 r, g, b;
  1201. if (regno < 32) {
  1202. for (i = 0; i < 8; i++) {
  1203. riva_wclut(chip, regno*8+i,
  1204. red >> 8, green >> 8,
  1205. blue >> 8);
  1206. }
  1207. }
  1208. riva_rclut(chip, regno*4, &r, &g, &b);
  1209. for (i = 0; i < 4; i++)
  1210. riva_wclut(chip, regno*4+i, r,
  1211. green >> 8, b);
  1212. }
  1213. break;
  1214. case 32:
  1215. riva_wclut(chip, regno, red >> 8, green >> 8, blue >> 8);
  1216. break;
  1217. default:
  1218. /* do nothing */
  1219. break;
  1220. }
  1221. return 0;
  1222. }
  1223. /**
  1224. * rivafb_fillrect - hardware accelerated color fill function
  1225. * @info: pointer to fb_info structure
  1226. * @rect: pointer to fb_fillrect structure
  1227. *
  1228. * DESCRIPTION:
  1229. * This function fills up a region of framebuffer memory with a solid
  1230. * color with a choice of two different ROP's, copy or invert.
  1231. *
  1232. * CALLED FROM:
  1233. * framebuffer hook
  1234. */
  1235. static void rivafb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
  1236. {
  1237. struct riva_par *par = info->par;
  1238. u_int color, rop = 0;
  1239. if ((info->flags & FBINFO_HWACCEL_DISABLED)) {
  1240. cfb_fillrect(info, rect);
  1241. return;
  1242. }
  1243. if (info->var.bits_per_pixel == 8)
  1244. color = rect->color;
  1245. else {
  1246. if (par->riva.Architecture != NV_ARCH_03)
  1247. color = ((u32 *)info->pseudo_palette)[rect->color];
  1248. else
  1249. color = par->palette[rect->color];
  1250. }
  1251. switch (rect->rop) {
  1252. case ROP_XOR:
  1253. rop = 0x66;
  1254. break;
  1255. case ROP_COPY:
  1256. default:
  1257. rop = 0xCC;
  1258. break;
  1259. }
  1260. riva_set_rop_solid(par, rop);
  1261. RIVA_FIFO_FREE(par->riva, Bitmap, 1);
  1262. NV_WR32(&par->riva.Bitmap->Color1A, 0, color);
  1263. RIVA_FIFO_FREE(par->riva, Bitmap, 2);
  1264. NV_WR32(&par->riva.Bitmap->UnclippedRectangle[0].TopLeft, 0,
  1265. (rect->dx << 16) | rect->dy);
  1266. mb();
  1267. NV_WR32(&par->riva.Bitmap->UnclippedRectangle[0].WidthHeight, 0,
  1268. (rect->width << 16) | rect->height);
  1269. mb();
  1270. riva_set_rop_solid(par, 0xcc);
  1271. }
  1272. /**
  1273. * rivafb_copyarea - hardware accelerated blit function
  1274. * @info: pointer to fb_info structure
  1275. * @region: pointer to fb_copyarea structure
  1276. *
  1277. * DESCRIPTION:
  1278. * This copies an area of pixels from one location to another
  1279. *
  1280. * CALLED FROM:
  1281. * framebuffer hook
  1282. */
  1283. static void rivafb_copyarea(struct fb_info *info, const struct fb_copyarea *region)
  1284. {
  1285. struct riva_par *par = info->par;
  1286. if ((info->flags & FBINFO_HWACCEL_DISABLED)) {
  1287. cfb_copyarea(info, region);
  1288. return;
  1289. }
  1290. RIVA_FIFO_FREE(par->riva, Blt, 3);
  1291. NV_WR32(&par->riva.Blt->TopLeftSrc, 0,
  1292. (region->sy << 16) | region->sx);
  1293. NV_WR32(&par->riva.Blt->TopLeftDst, 0,
  1294. (region->dy << 16) | region->dx);
  1295. mb();
  1296. NV_WR32(&par->riva.Blt->WidthHeight, 0,
  1297. (region->height << 16) | region->width);
  1298. mb();
  1299. }
  1300. static inline void convert_bgcolor_16(u32 *col)
  1301. {
  1302. *col = ((*col & 0x0000F800) << 8)
  1303. | ((*col & 0x00007E0) << 5)
  1304. | ((*col & 0x0000001F) << 3)
  1305. | 0xFF000000;
  1306. mb();
  1307. }
  1308. /**
  1309. * rivafb_imageblit: hardware accelerated color expand function
  1310. * @info: pointer to fb_info structure
  1311. * @image: pointer to fb_image structure
  1312. *
  1313. * DESCRIPTION:
  1314. * If the source is a monochrome bitmap, the function fills up a a region
  1315. * of framebuffer memory with pixels whose color is determined by the bit
  1316. * setting of the bitmap, 1 - foreground, 0 - background.
  1317. *
  1318. * If the source is not a monochrome bitmap, color expansion is not done.
  1319. * In this case, it is channeled to a software function.
  1320. *
  1321. * CALLED FROM:
  1322. * framebuffer hook
  1323. */
  1324. static void rivafb_imageblit(struct fb_info *info,
  1325. const struct fb_image *image)
  1326. {
  1327. struct riva_par *par = info->par;
  1328. u32 fgx = 0, bgx = 0, width, tmp;
  1329. u8 *cdat = (u8 *) image->data;
  1330. volatile u32 __iomem *d;
  1331. int i, size;
  1332. if ((info->flags & FBINFO_HWACCEL_DISABLED) || image->depth != 1) {
  1333. cfb_imageblit(info, image);
  1334. return;
  1335. }
  1336. switch (info->var.bits_per_pixel) {
  1337. case 8:
  1338. fgx = image->fg_color;
  1339. bgx = image->bg_color;
  1340. break;
  1341. case 16:
  1342. case 32:
  1343. if (par->riva.Architecture != NV_ARCH_03) {
  1344. fgx = ((u32 *)info->pseudo_palette)[image->fg_color];
  1345. bgx = ((u32 *)info->pseudo_palette)[image->bg_color];
  1346. } else {
  1347. fgx = par->palette[image->fg_color];
  1348. bgx = par->palette[image->bg_color];
  1349. }
  1350. if (info->var.green.length == 6)
  1351. convert_bgcolor_16(&bgx);
  1352. break;
  1353. }
  1354. RIVA_FIFO_FREE(par->riva, Bitmap, 7);
  1355. NV_WR32(&par->riva.Bitmap->ClipE.TopLeft, 0,
  1356. (image->dy << 16) | (image->dx & 0xFFFF));
  1357. NV_WR32(&par->riva.Bitmap->ClipE.BottomRight, 0,
  1358. (((image->dy + image->height) << 16) |
  1359. ((image->dx + image->width) & 0xffff)));
  1360. NV_WR32(&par->riva.Bitmap->Color0E, 0, bgx);
  1361. NV_WR32(&par->riva.Bitmap->Color1E, 0, fgx);
  1362. NV_WR32(&par->riva.Bitmap->WidthHeightInE, 0,
  1363. (image->height << 16) | ((image->width + 31) & ~31));
  1364. NV_WR32(&par->riva.Bitmap->WidthHeightOutE, 0,
  1365. (image->height << 16) | ((image->width + 31) & ~31));
  1366. NV_WR32(&par->riva.Bitmap->PointE, 0,
  1367. (image->dy << 16) | (image->dx & 0xFFFF));
  1368. d = &par->riva.Bitmap->MonochromeData01E;
  1369. width = (image->width + 31)/32;
  1370. size = width * image->height;
  1371. while (size >= 16) {
  1372. RIVA_FIFO_FREE(par->riva, Bitmap, 16);
  1373. for (i = 0; i < 16; i++) {
  1374. tmp = *((u32 *)cdat);
  1375. cdat = (u8 *)((u32 *)cdat + 1);
  1376. reverse_order(&tmp);
  1377. NV_WR32(d, i*4, tmp);
  1378. }
  1379. size -= 16;
  1380. }
  1381. if (size) {
  1382. RIVA_FIFO_FREE(par->riva, Bitmap, size);
  1383. for (i = 0; i < size; i++) {
  1384. tmp = *((u32 *) cdat);
  1385. cdat = (u8 *)((u32 *)cdat + 1);
  1386. reverse_order(&tmp);
  1387. NV_WR32(d, i*4, tmp);
  1388. }
  1389. }
  1390. }
  1391. /**
  1392. * rivafb_cursor - hardware cursor function
  1393. * @info: pointer to info structure
  1394. * @cursor: pointer to fbcursor structure
  1395. *
  1396. * DESCRIPTION:
  1397. * A cursor function that supports displaying a cursor image via hardware.
  1398. * Within the kernel, copy and invert rops are supported. If exported
  1399. * to user space, only the copy rop will be supported.
  1400. *
  1401. * CALLED FROM
  1402. * framebuffer hook
  1403. */
  1404. static int rivafb_cursor(struct fb_info *info, struct fb_cursor *cursor)
  1405. {
  1406. struct riva_par *par = info->par;
  1407. u8 data[MAX_CURS * MAX_CURS/8];
  1408. int i, set = cursor->set;
  1409. u16 fg, bg;
  1410. if (cursor->image.width > MAX_CURS || cursor->image.height > MAX_CURS)
  1411. return -ENXIO;
  1412. par->riva.ShowHideCursor(&par->riva, 0);
  1413. if (par->cursor_reset) {
  1414. set = FB_CUR_SETALL;
  1415. par->cursor_reset = 0;
  1416. }
  1417. if (set & FB_CUR_SETSIZE)
  1418. memset_io(par->riva.CURSOR, 0, MAX_CURS * MAX_CURS * 2);
  1419. if (set & FB_CUR_SETPOS) {
  1420. u32 xx, yy, temp;
  1421. yy = cursor->image.dy - info->var.yoffset;
  1422. xx = cursor->image.dx - info->var.xoffset;
  1423. temp = xx & 0xFFFF;
  1424. temp |= yy << 16;
  1425. NV_WR32(par->riva.PRAMDAC, 0x0000300, temp);
  1426. }
  1427. if (set & (FB_CUR_SETSHAPE | FB_CUR_SETCMAP | FB_CUR_SETIMAGE)) {
  1428. u32 bg_idx = cursor->image.bg_color;
  1429. u32 fg_idx = cursor->image.fg_color;
  1430. u32 s_pitch = (cursor->image.width+7) >> 3;
  1431. u32 d_pitch = MAX_CURS/8;
  1432. u8 *dat = (u8 *) cursor->image.data;
  1433. u8 *msk = (u8 *) cursor->mask;
  1434. u8 *src;
  1435. src = kmalloc_array(s_pitch, cursor->image.height, GFP_ATOMIC);
  1436. if (src) {
  1437. switch (cursor->rop) {
  1438. case ROP_XOR:
  1439. for (i = 0; i < s_pitch * cursor->image.height; i++)
  1440. src[i] = dat[i] ^ msk[i];
  1441. break;
  1442. case ROP_COPY:
  1443. default:
  1444. for (i = 0; i < s_pitch * cursor->image.height; i++)
  1445. src[i] = dat[i] & msk[i];
  1446. break;
  1447. }
  1448. fb_pad_aligned_buffer(data, d_pitch, src, s_pitch,
  1449. cursor->image.height);
  1450. bg = ((info->cmap.red[bg_idx] & 0xf8) << 7) |
  1451. ((info->cmap.green[bg_idx] & 0xf8) << 2) |
  1452. ((info->cmap.blue[bg_idx] & 0xf8) >> 3) |
  1453. 1 << 15;
  1454. fg = ((info->cmap.red[fg_idx] & 0xf8) << 7) |
  1455. ((info->cmap.green[fg_idx] & 0xf8) << 2) |
  1456. ((info->cmap.blue[fg_idx] & 0xf8) >> 3) |
  1457. 1 << 15;
  1458. par->riva.LockUnlock(&par->riva, 0);
  1459. rivafb_load_cursor_image(par, data, bg, fg,
  1460. cursor->image.width,
  1461. cursor->image.height);
  1462. kfree(src);
  1463. }
  1464. }
  1465. if (cursor->enable)
  1466. par->riva.ShowHideCursor(&par->riva, 1);
  1467. return 0;
  1468. }
  1469. static int rivafb_sync(struct fb_info *info)
  1470. {
  1471. struct riva_par *par = info->par;
  1472. wait_for_idle(par);
  1473. return 0;
  1474. }
  1475. /* ------------------------------------------------------------------------- *
  1476. *
  1477. * initialization helper functions
  1478. *
  1479. * ------------------------------------------------------------------------- */
  1480. /* kernel interface */
  1481. static const struct fb_ops riva_fb_ops = {
  1482. .owner = THIS_MODULE,
  1483. .fb_open = rivafb_open,
  1484. .fb_release = rivafb_release,
  1485. __FB_DEFAULT_IOMEM_OPS_RDWR,
  1486. .fb_check_var = rivafb_check_var,
  1487. .fb_set_par = rivafb_set_par,
  1488. .fb_setcolreg = rivafb_setcolreg,
  1489. .fb_pan_display = rivafb_pan_display,
  1490. .fb_blank = rivafb_blank,
  1491. .fb_fillrect = rivafb_fillrect,
  1492. .fb_copyarea = rivafb_copyarea,
  1493. .fb_imageblit = rivafb_imageblit,
  1494. .fb_cursor = rivafb_cursor,
  1495. .fb_sync = rivafb_sync,
  1496. __FB_DEFAULT_IOMEM_OPS_MMAP,
  1497. };
  1498. static int riva_set_fbinfo(struct fb_info *info)
  1499. {
  1500. unsigned int cmap_len;
  1501. struct riva_par *par = info->par;
  1502. NVTRACE_ENTER();
  1503. info->flags = FBINFO_HWACCEL_XPAN
  1504. | FBINFO_HWACCEL_YPAN
  1505. | FBINFO_HWACCEL_COPYAREA
  1506. | FBINFO_HWACCEL_FILLRECT
  1507. | FBINFO_HWACCEL_IMAGEBLIT;
  1508. /* Accel seems to not work properly on NV30 yet...*/
  1509. if ((par->riva.Architecture == NV_ARCH_30) || noaccel) {
  1510. printk(KERN_DEBUG PFX "disabling acceleration\n");
  1511. info->flags |= FBINFO_HWACCEL_DISABLED;
  1512. }
  1513. info->var = rivafb_default_var;
  1514. info->fix.visual = (info->var.bits_per_pixel == 8) ?
  1515. FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_DIRECTCOLOR;
  1516. info->pseudo_palette = par->pseudo_palette;
  1517. cmap_len = riva_get_cmap_len(&info->var);
  1518. fb_alloc_cmap(&info->cmap, cmap_len, 0);
  1519. info->pixmap.size = 8 * 1024;
  1520. info->pixmap.buf_align = 4;
  1521. info->pixmap.access_align = 32;
  1522. info->pixmap.flags = FB_PIXMAP_SYSTEM;
  1523. info->var.yres_virtual = -1;
  1524. NVTRACE_LEAVE();
  1525. return (rivafb_check_var(&info->var, info));
  1526. }
  1527. static int riva_get_EDID_OF(struct fb_info *info, struct pci_dev *pd)
  1528. {
  1529. struct riva_par *par = info->par;
  1530. struct device_node *dp;
  1531. const unsigned char *pedid = NULL;
  1532. const unsigned char *disptype = NULL;
  1533. static char *propnames[] = {
  1534. "DFP,EDID", "LCD,EDID", "EDID", "EDID1", "EDID,B", "EDID,A", NULL };
  1535. int i;
  1536. NVTRACE_ENTER();
  1537. dp = pci_device_to_OF_node(pd);
  1538. for (; dp != NULL; dp = dp->child) {
  1539. disptype = of_get_property(dp, "display-type", NULL);
  1540. if (disptype == NULL)
  1541. continue;
  1542. if (strncmp(disptype, "LCD", 3) != 0)
  1543. continue;
  1544. for (i = 0; propnames[i] != NULL; ++i) {
  1545. pedid = of_get_property(dp, propnames[i], NULL);
  1546. if (pedid != NULL) {
  1547. par->EDID = (unsigned char *)pedid;
  1548. NVTRACE("LCD found.\n");
  1549. return 1;
  1550. }
  1551. }
  1552. }
  1553. NVTRACE_LEAVE();
  1554. return 0;
  1555. }
  1556. #if defined(CONFIG_FB_RIVA_I2C)
  1557. static int riva_get_EDID_i2c(struct fb_info *info)
  1558. {
  1559. struct riva_par *par = info->par;
  1560. struct fb_var_screeninfo var;
  1561. int i;
  1562. NVTRACE_ENTER();
  1563. par->riva.LockUnlock(&par->riva, 0);
  1564. riva_create_i2c_busses(par);
  1565. for (i = 0; i < 3; i++) {
  1566. if (!par->chan[i].par)
  1567. continue;
  1568. riva_probe_i2c_connector(par, i, &par->EDID);
  1569. if (par->EDID && !fb_parse_edid(par->EDID, &var)) {
  1570. printk(PFX "Found EDID Block from BUS %i\n", i);
  1571. break;
  1572. }
  1573. }
  1574. NVTRACE_LEAVE();
  1575. return (par->EDID) ? 1 : 0;
  1576. }
  1577. #endif /* CONFIG_FB_RIVA_I2C */
  1578. static void riva_update_default_var(struct fb_var_screeninfo *var,
  1579. struct fb_info *info)
  1580. {
  1581. struct fb_monspecs *specs = &info->monspecs;
  1582. struct fb_videomode modedb;
  1583. NVTRACE_ENTER();
  1584. /* respect mode options */
  1585. if (mode_option) {
  1586. fb_find_mode(var, info, mode_option,
  1587. specs->modedb, specs->modedb_len,
  1588. NULL, 8);
  1589. } else if (specs->modedb != NULL) {
  1590. /* get first mode in database as fallback */
  1591. modedb = specs->modedb[0];
  1592. /* get preferred timing */
  1593. if (info->monspecs.misc & FB_MISC_1ST_DETAIL) {
  1594. int i;
  1595. for (i = 0; i < specs->modedb_len; i++) {
  1596. if (specs->modedb[i].flag & FB_MODE_IS_FIRST) {
  1597. modedb = specs->modedb[i];
  1598. break;
  1599. }
  1600. }
  1601. }
  1602. var->bits_per_pixel = 8;
  1603. riva_update_var(var, &modedb);
  1604. }
  1605. NVTRACE_LEAVE();
  1606. }
  1607. static void riva_get_EDID(struct fb_info *info, struct pci_dev *pdev)
  1608. {
  1609. NVTRACE_ENTER();
  1610. if (riva_get_EDID_OF(info, pdev)) {
  1611. NVTRACE_LEAVE();
  1612. return;
  1613. }
  1614. if (IS_ENABLED(CONFIG_OF))
  1615. printk(PFX "could not retrieve EDID from OF\n");
  1616. #if defined(CONFIG_FB_RIVA_I2C)
  1617. if (!riva_get_EDID_i2c(info))
  1618. printk(PFX "could not retrieve EDID from DDC/I2C\n");
  1619. #endif
  1620. NVTRACE_LEAVE();
  1621. }
  1622. static void riva_get_edidinfo(struct fb_info *info)
  1623. {
  1624. struct fb_var_screeninfo *var = &rivafb_default_var;
  1625. struct riva_par *par = info->par;
  1626. fb_edid_to_monspecs(par->EDID, &info->monspecs);
  1627. fb_videomode_to_modelist(info->monspecs.modedb, info->monspecs.modedb_len,
  1628. &info->modelist);
  1629. riva_update_default_var(var, info);
  1630. /* if user specified flatpanel, we respect that */
  1631. if (info->monspecs.input & FB_DISP_DDI)
  1632. par->FlatPanel = 1;
  1633. }
  1634. /* ------------------------------------------------------------------------- *
  1635. *
  1636. * PCI bus
  1637. *
  1638. * ------------------------------------------------------------------------- */
  1639. static u32 riva_get_arch(struct pci_dev *pd)
  1640. {
  1641. u32 arch = 0;
  1642. switch (pd->device & 0x0ff0) {
  1643. case 0x0100: /* GeForce 256 */
  1644. case 0x0110: /* GeForce2 MX */
  1645. case 0x0150: /* GeForce2 */
  1646. case 0x0170: /* GeForce4 MX */
  1647. case 0x0180: /* GeForce4 MX (8x AGP) */
  1648. case 0x01A0: /* nForce */
  1649. case 0x01F0: /* nForce2 */
  1650. arch = NV_ARCH_10;
  1651. break;
  1652. case 0x0200: /* GeForce3 */
  1653. case 0x0250: /* GeForce4 Ti */
  1654. case 0x0280: /* GeForce4 Ti (8x AGP) */
  1655. arch = NV_ARCH_20;
  1656. break;
  1657. case 0x0300: /* GeForceFX 5800 */
  1658. case 0x0310: /* GeForceFX 5600 */
  1659. case 0x0320: /* GeForceFX 5200 */
  1660. case 0x0330: /* GeForceFX 5900 */
  1661. case 0x0340: /* GeForceFX 5700 */
  1662. arch = NV_ARCH_30;
  1663. break;
  1664. case 0x0020: /* TNT, TNT2 */
  1665. arch = NV_ARCH_04;
  1666. break;
  1667. case 0x0010: /* Riva128 */
  1668. arch = NV_ARCH_03;
  1669. break;
  1670. default: /* unknown architecture */
  1671. break;
  1672. }
  1673. return arch;
  1674. }
  1675. static int rivafb_probe(struct pci_dev *pd, const struct pci_device_id *ent)
  1676. {
  1677. struct riva_par *default_par;
  1678. struct fb_info *info;
  1679. int ret;
  1680. NVTRACE_ENTER();
  1681. assert(pd != NULL);
  1682. ret = aperture_remove_conflicting_pci_devices(pd, "rivafb");
  1683. if (ret)
  1684. return ret;
  1685. info = framebuffer_alloc(sizeof(struct riva_par), &pd->dev);
  1686. if (!info) {
  1687. ret = -ENOMEM;
  1688. goto err_ret;
  1689. }
  1690. default_par = info->par;
  1691. default_par->pdev = pd;
  1692. info->pixmap.addr = kzalloc(8 * 1024, GFP_KERNEL);
  1693. if (info->pixmap.addr == NULL) {
  1694. ret = -ENOMEM;
  1695. goto err_framebuffer_release;
  1696. }
  1697. ret = pci_enable_device(pd);
  1698. if (ret < 0) {
  1699. printk(KERN_ERR PFX "cannot enable PCI device\n");
  1700. goto err_free_pixmap;
  1701. }
  1702. ret = pci_request_regions(pd, "rivafb");
  1703. if (ret < 0) {
  1704. printk(KERN_ERR PFX "cannot request PCI regions\n");
  1705. goto err_disable_device;
  1706. }
  1707. mutex_init(&default_par->open_lock);
  1708. default_par->riva.Architecture = riva_get_arch(pd);
  1709. default_par->Chipset = (pd->vendor << 16) | pd->device;
  1710. printk(KERN_INFO PFX "nVidia device/chipset %X\n",default_par->Chipset);
  1711. if(default_par->riva.Architecture == 0) {
  1712. printk(KERN_ERR PFX "unknown NV_ARCH\n");
  1713. ret=-ENODEV;
  1714. goto err_release_region;
  1715. }
  1716. if(default_par->riva.Architecture == NV_ARCH_10 ||
  1717. default_par->riva.Architecture == NV_ARCH_20 ||
  1718. default_par->riva.Architecture == NV_ARCH_30) {
  1719. sprintf(rivafb_fix.id, "NV%x", (pd->device & 0x0ff0) >> 4);
  1720. } else {
  1721. sprintf(rivafb_fix.id, "NV%x", default_par->riva.Architecture);
  1722. }
  1723. default_par->FlatPanel = flatpanel;
  1724. if (flatpanel == 1)
  1725. printk(KERN_INFO PFX "flatpanel support enabled\n");
  1726. default_par->forceCRTC = forceCRTC;
  1727. rivafb_fix.mmio_len = pci_resource_len(pd, 0);
  1728. rivafb_fix.smem_len = pci_resource_len(pd, 1);
  1729. {
  1730. /* enable IO and mem if not already done */
  1731. unsigned short cmd;
  1732. pci_read_config_word(pd, PCI_COMMAND, &cmd);
  1733. cmd |= (PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
  1734. pci_write_config_word(pd, PCI_COMMAND, cmd);
  1735. }
  1736. rivafb_fix.mmio_start = pci_resource_start(pd, 0);
  1737. rivafb_fix.smem_start = pci_resource_start(pd, 1);
  1738. default_par->ctrl_base = ioremap(rivafb_fix.mmio_start,
  1739. rivafb_fix.mmio_len);
  1740. if (!default_par->ctrl_base) {
  1741. printk(KERN_ERR PFX "cannot ioremap MMIO base\n");
  1742. ret = -EIO;
  1743. goto err_release_region;
  1744. }
  1745. switch (default_par->riva.Architecture) {
  1746. case NV_ARCH_03:
  1747. /* Riva128's PRAMIN is in the "framebuffer" space
  1748. * Since these cards were never made with more than 8 megabytes
  1749. * we can safely allocate this separately.
  1750. */
  1751. default_par->riva.PRAMIN = ioremap(rivafb_fix.smem_start + 0x00C00000, 0x00008000);
  1752. if (!default_par->riva.PRAMIN) {
  1753. printk(KERN_ERR PFX "cannot ioremap PRAMIN region\n");
  1754. ret = -EIO;
  1755. goto err_iounmap_ctrl_base;
  1756. }
  1757. break;
  1758. case NV_ARCH_04:
  1759. case NV_ARCH_10:
  1760. case NV_ARCH_20:
  1761. case NV_ARCH_30:
  1762. default_par->riva.PCRTC0 =
  1763. (u32 __iomem *)(default_par->ctrl_base + 0x00600000);
  1764. default_par->riva.PRAMIN =
  1765. (u32 __iomem *)(default_par->ctrl_base + 0x00710000);
  1766. break;
  1767. }
  1768. riva_common_setup(default_par);
  1769. if (default_par->riva.Architecture == NV_ARCH_03) {
  1770. default_par->riva.PCRTC = default_par->riva.PCRTC0
  1771. = default_par->riva.PGRAPH;
  1772. }
  1773. rivafb_fix.smem_len = riva_get_memlen(default_par) * 1024;
  1774. default_par->dclk_max = riva_get_maxdclk(default_par) * 1000;
  1775. info->screen_base = ioremap_wc(rivafb_fix.smem_start,
  1776. rivafb_fix.smem_len);
  1777. if (!info->screen_base) {
  1778. printk(KERN_ERR PFX "cannot ioremap FB base\n");
  1779. ret = -EIO;
  1780. goto err_iounmap_pramin;
  1781. }
  1782. if (!nomtrr)
  1783. default_par->wc_cookie =
  1784. arch_phys_wc_add(rivafb_fix.smem_start,
  1785. rivafb_fix.smem_len);
  1786. info->fbops = &riva_fb_ops;
  1787. info->fix = rivafb_fix;
  1788. riva_get_EDID(info, pd);
  1789. riva_get_edidinfo(info);
  1790. ret=riva_set_fbinfo(info);
  1791. if (ret < 0) {
  1792. printk(KERN_ERR PFX "error setting initial video mode\n");
  1793. goto err_iounmap_screen_base;
  1794. }
  1795. fb_destroy_modedb(info->monspecs.modedb);
  1796. info->monspecs.modedb = NULL;
  1797. pci_set_drvdata(pd, info);
  1798. ret = register_framebuffer(info);
  1799. if (ret < 0) {
  1800. printk(KERN_ERR PFX
  1801. "error registering riva framebuffer\n");
  1802. goto err_iounmap_screen_base;
  1803. }
  1804. if (backlight)
  1805. riva_bl_init(info->par);
  1806. printk(KERN_INFO PFX
  1807. "PCI nVidia %s framebuffer ver %s (%dMB @ 0x%lX)\n",
  1808. info->fix.id,
  1809. RIVAFB_VERSION,
  1810. info->fix.smem_len / (1024 * 1024),
  1811. info->fix.smem_start);
  1812. NVTRACE_LEAVE();
  1813. return 0;
  1814. err_iounmap_screen_base:
  1815. #ifdef CONFIG_FB_RIVA_I2C
  1816. riva_delete_i2c_busses(info->par);
  1817. #endif
  1818. iounmap(info->screen_base);
  1819. err_iounmap_pramin:
  1820. if (default_par->riva.Architecture == NV_ARCH_03)
  1821. iounmap(default_par->riva.PRAMIN);
  1822. err_iounmap_ctrl_base:
  1823. iounmap(default_par->ctrl_base);
  1824. err_release_region:
  1825. pci_release_regions(pd);
  1826. err_disable_device:
  1827. err_free_pixmap:
  1828. kfree(info->pixmap.addr);
  1829. err_framebuffer_release:
  1830. framebuffer_release(info);
  1831. err_ret:
  1832. return ret;
  1833. }
  1834. static void rivafb_remove(struct pci_dev *pd)
  1835. {
  1836. struct fb_info *info = pci_get_drvdata(pd);
  1837. struct riva_par *par = info->par;
  1838. NVTRACE_ENTER();
  1839. #ifdef CONFIG_FB_RIVA_I2C
  1840. riva_delete_i2c_busses(par);
  1841. kfree(par->EDID);
  1842. #endif
  1843. riva_bl_exit(info);
  1844. unregister_framebuffer(info);
  1845. arch_phys_wc_del(par->wc_cookie);
  1846. iounmap(par->ctrl_base);
  1847. iounmap(info->screen_base);
  1848. if (par->riva.Architecture == NV_ARCH_03)
  1849. iounmap(par->riva.PRAMIN);
  1850. pci_release_regions(pd);
  1851. kfree(info->pixmap.addr);
  1852. framebuffer_release(info);
  1853. NVTRACE_LEAVE();
  1854. }
  1855. /* ------------------------------------------------------------------------- *
  1856. *
  1857. * initialization
  1858. *
  1859. * ------------------------------------------------------------------------- */
  1860. #ifndef MODULE
  1861. static int rivafb_setup(char *options)
  1862. {
  1863. char *this_opt;
  1864. NVTRACE_ENTER();
  1865. if (!options || !*options)
  1866. return 0;
  1867. while ((this_opt = strsep(&options, ",")) != NULL) {
  1868. if (!strncmp(this_opt, "forceCRTC", 9)) {
  1869. char *p;
  1870. p = this_opt + 9;
  1871. if (!*p || !*(++p)) continue;
  1872. forceCRTC = *p - '0';
  1873. if (forceCRTC < 0 || forceCRTC > 1)
  1874. forceCRTC = -1;
  1875. } else if (!strncmp(this_opt, "flatpanel", 9)) {
  1876. flatpanel = 1;
  1877. } else if (!strncmp(this_opt, "backlight:", 10)) {
  1878. backlight = simple_strtoul(this_opt+10, NULL, 0);
  1879. } else if (!strncmp(this_opt, "nomtrr", 6)) {
  1880. nomtrr = 1;
  1881. } else if (!strncmp(this_opt, "strictmode", 10)) {
  1882. strictmode = 1;
  1883. } else if (!strncmp(this_opt, "noaccel", 7)) {
  1884. noaccel = 1;
  1885. } else
  1886. mode_option = this_opt;
  1887. }
  1888. NVTRACE_LEAVE();
  1889. return 0;
  1890. }
  1891. #endif /* !MODULE */
  1892. static struct pci_driver rivafb_driver = {
  1893. .name = "rivafb",
  1894. .id_table = rivafb_pci_tbl,
  1895. .probe = rivafb_probe,
  1896. .remove = rivafb_remove,
  1897. };
  1898. /* ------------------------------------------------------------------------- *
  1899. *
  1900. * modularization
  1901. *
  1902. * ------------------------------------------------------------------------- */
  1903. static int rivafb_init(void)
  1904. {
  1905. #ifndef MODULE
  1906. char *option = NULL;
  1907. #endif
  1908. if (fb_modesetting_disabled("rivafb"))
  1909. return -ENODEV;
  1910. #ifndef MODULE
  1911. if (fb_get_options("rivafb", &option))
  1912. return -ENODEV;
  1913. rivafb_setup(option);
  1914. #endif
  1915. return pci_register_driver(&rivafb_driver);
  1916. }
  1917. module_init(rivafb_init);
  1918. static void __exit rivafb_exit(void)
  1919. {
  1920. pci_unregister_driver(&rivafb_driver);
  1921. }
  1922. module_exit(rivafb_exit);
  1923. module_param(noaccel, bool, 0);
  1924. MODULE_PARM_DESC(noaccel, "bool: disable acceleration");
  1925. module_param(flatpanel, int, 0);
  1926. MODULE_PARM_DESC(flatpanel, "Enables experimental flat panel support for some chipsets. (0 or 1=enabled) (default=0)");
  1927. module_param(forceCRTC, int, 0);
  1928. MODULE_PARM_DESC(forceCRTC, "Forces usage of a particular CRTC in case autodetection fails. (0 or 1) (default=autodetect)");
  1929. module_param(nomtrr, bool, 0);
  1930. MODULE_PARM_DESC(nomtrr, "Disables MTRR support (0 or 1=disabled) (default=0)");
  1931. module_param(strictmode, bool, 0);
  1932. MODULE_PARM_DESC(strictmode, "Only use video modes from EDID");
  1933. MODULE_AUTHOR("Ani Joshi, maintainer");
  1934. MODULE_DESCRIPTION("Framebuffer driver for nVidia Riva 128, TNT, TNT2, and the GeForce series");
  1935. MODULE_LICENSE("GPL");