fbdev.c 57 KB

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