aty128fb.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592
  1. /* $Id: aty128fb.c,v 1.1.1.1.36.1 1999/12/11 09:03:05 Exp $
  2. * linux/drivers/video/aty128fb.c -- Frame buffer device for ATI Rage128
  3. *
  4. * Copyright (C) 1999-2003, Brad Douglas <brad@neruo.com>
  5. * Copyright (C) 1999, Anthony Tong <atong@uiuc.edu>
  6. *
  7. * Ani Joshi / Jeff Garzik
  8. * - Code cleanup
  9. *
  10. * Michel Danzer <michdaen@iiic.ethz.ch>
  11. * - 15/16 bit cleanup
  12. * - fix panning
  13. *
  14. * Benjamin Herrenschmidt
  15. * - pmac-specific PM stuff
  16. * - various fixes & cleanups
  17. *
  18. * Andreas Hundt <andi@convergence.de>
  19. * - FB_ACTIVATE fixes
  20. *
  21. * Paul Mackerras <paulus@samba.org>
  22. * - Convert to new framebuffer API,
  23. * fix colormap setting at 16 bits/pixel (565)
  24. *
  25. * Paul Mundt
  26. * - PCI hotplug
  27. *
  28. * Jon Smirl <jonsmirl@yahoo.com>
  29. * - PCI ID update
  30. * - replace ROM BIOS search
  31. *
  32. * Based off of Geert's atyfb.c and vfb.c.
  33. *
  34. * TODO:
  35. * - monitor sensing (DDC)
  36. * - virtual display
  37. * - other platform support (only ppc/x86 supported)
  38. * - hardware cursor support
  39. *
  40. * Please cc: your patches to brad@neruo.com.
  41. */
  42. /*
  43. * A special note of gratitude to ATI's devrel for providing documentation,
  44. * example code and hardware. Thanks Nitya. -atong and brad
  45. */
  46. #include <linux/module.h>
  47. #include <linux/moduleparam.h>
  48. #include <linux/kernel.h>
  49. #include <linux/errno.h>
  50. #include <linux/string.h>
  51. #include <linux/mm.h>
  52. #include <linux/slab.h>
  53. #include <linux/vmalloc.h>
  54. #include <linux/delay.h>
  55. #include <linux/interrupt.h>
  56. #include <asm/uaccess.h>
  57. #include <linux/fb.h>
  58. #include <linux/init.h>
  59. #include <linux/pci.h>
  60. #include <linux/ioport.h>
  61. #include <linux/console.h>
  62. #include <linux/backlight.h>
  63. #include <asm/io.h>
  64. #ifdef CONFIG_PPC_PMAC
  65. #include <asm/machdep.h>
  66. #include <asm/pmac_feature.h>
  67. #include <asm/prom.h>
  68. #include <asm/pci-bridge.h>
  69. #include "../macmodes.h"
  70. #endif
  71. #ifdef CONFIG_PMAC_BACKLIGHT
  72. #include <asm/backlight.h>
  73. #endif
  74. #ifdef CONFIG_BOOTX_TEXT
  75. #include <asm/btext.h>
  76. #endif /* CONFIG_BOOTX_TEXT */
  77. #ifdef CONFIG_MTRR
  78. #include <asm/mtrr.h>
  79. #endif
  80. #include <video/aty128.h>
  81. /* Debug flag */
  82. #undef DEBUG
  83. #ifdef DEBUG
  84. #define DBG(fmt, args...) printk(KERN_DEBUG "aty128fb: %s " fmt, __FUNCTION__, ##args);
  85. #else
  86. #define DBG(fmt, args...)
  87. #endif
  88. #ifndef CONFIG_PPC_PMAC
  89. /* default mode */
  90. static struct fb_var_screeninfo default_var __devinitdata = {
  91. /* 640x480, 60 Hz, Non-Interlaced (25.175 MHz dotclock) */
  92. 640, 480, 640, 480, 0, 0, 8, 0,
  93. {0, 8, 0}, {0, 8, 0}, {0, 8, 0}, {0, 0, 0},
  94. 0, 0, -1, -1, 0, 39722, 48, 16, 33, 10, 96, 2,
  95. 0, FB_VMODE_NONINTERLACED
  96. };
  97. #else /* CONFIG_PPC_PMAC */
  98. /* default to 1024x768 at 75Hz on PPC - this will work
  99. * on the iMac, the usual 640x480 @ 60Hz doesn't. */
  100. static struct fb_var_screeninfo default_var = {
  101. /* 1024x768, 75 Hz, Non-Interlaced (78.75 MHz dotclock) */
  102. 1024, 768, 1024, 768, 0, 0, 8, 0,
  103. {0, 8, 0}, {0, 8, 0}, {0, 8, 0}, {0, 0, 0},
  104. 0, 0, -1, -1, 0, 12699, 160, 32, 28, 1, 96, 3,
  105. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  106. FB_VMODE_NONINTERLACED
  107. };
  108. #endif /* CONFIG_PPC_PMAC */
  109. /* default modedb mode */
  110. /* 640x480, 60 Hz, Non-Interlaced (25.172 MHz dotclock) */
  111. static struct fb_videomode defaultmode __devinitdata = {
  112. .refresh = 60,
  113. .xres = 640,
  114. .yres = 480,
  115. .pixclock = 39722,
  116. .left_margin = 48,
  117. .right_margin = 16,
  118. .upper_margin = 33,
  119. .lower_margin = 10,
  120. .hsync_len = 96,
  121. .vsync_len = 2,
  122. .sync = 0,
  123. .vmode = FB_VMODE_NONINTERLACED
  124. };
  125. /* Chip generations */
  126. enum {
  127. rage_128,
  128. rage_128_pci,
  129. rage_128_pro,
  130. rage_128_pro_pci,
  131. rage_M3,
  132. rage_M3_pci,
  133. rage_M4,
  134. rage_128_ultra,
  135. };
  136. /* Must match above enum */
  137. static const char *r128_family[] __devinitdata = {
  138. "AGP",
  139. "PCI",
  140. "PRO AGP",
  141. "PRO PCI",
  142. "M3 AGP",
  143. "M3 PCI",
  144. "M4 AGP",
  145. "Ultra AGP",
  146. };
  147. /*
  148. * PCI driver prototypes
  149. */
  150. static int aty128_probe(struct pci_dev *pdev,
  151. const struct pci_device_id *ent);
  152. static void aty128_remove(struct pci_dev *pdev);
  153. static int aty128_pci_suspend(struct pci_dev *pdev, pm_message_t state);
  154. static int aty128_pci_resume(struct pci_dev *pdev);
  155. static int aty128_do_resume(struct pci_dev *pdev);
  156. /* supported Rage128 chipsets */
  157. static struct pci_device_id aty128_pci_tbl[] = {
  158. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_LE,
  159. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_M3_pci },
  160. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_LF,
  161. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_M3 },
  162. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_MF,
  163. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_M4 },
  164. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_ML,
  165. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_M4 },
  166. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PA,
  167. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  168. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PB,
  169. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  170. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PC,
  171. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  172. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PD,
  173. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro_pci },
  174. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PE,
  175. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  176. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PF,
  177. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  178. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PG,
  179. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  180. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PH,
  181. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  182. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PI,
  183. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  184. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PJ,
  185. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  186. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PK,
  187. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  188. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PL,
  189. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  190. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PM,
  191. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  192. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PN,
  193. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  194. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PO,
  195. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  196. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PP,
  197. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro_pci },
  198. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PQ,
  199. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  200. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PR,
  201. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro_pci },
  202. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PS,
  203. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  204. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PT,
  205. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  206. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PU,
  207. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  208. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PV,
  209. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  210. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PW,
  211. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  212. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PX,
  213. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  214. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_RE,
  215. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pci },
  216. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_RF,
  217. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  218. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_RG,
  219. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  220. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_RK,
  221. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pci },
  222. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_RL,
  223. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  224. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SE,
  225. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  226. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SF,
  227. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pci },
  228. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SG,
  229. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  230. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SH,
  231. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  232. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SK,
  233. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  234. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SL,
  235. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  236. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SM,
  237. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  238. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SN,
  239. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  240. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_TF,
  241. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_ultra },
  242. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_TL,
  243. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_ultra },
  244. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_TR,
  245. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_ultra },
  246. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_TS,
  247. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_ultra },
  248. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_TT,
  249. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_ultra },
  250. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_TU,
  251. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_ultra },
  252. { 0, }
  253. };
  254. MODULE_DEVICE_TABLE(pci, aty128_pci_tbl);
  255. static struct pci_driver aty128fb_driver = {
  256. .name = "aty128fb",
  257. .id_table = aty128_pci_tbl,
  258. .probe = aty128_probe,
  259. .remove = __devexit_p(aty128_remove),
  260. .suspend = aty128_pci_suspend,
  261. .resume = aty128_pci_resume,
  262. };
  263. /* packed BIOS settings */
  264. #ifndef CONFIG_PPC
  265. typedef struct {
  266. u8 clock_chip_type;
  267. u8 struct_size;
  268. u8 accelerator_entry;
  269. u8 VGA_entry;
  270. u16 VGA_table_offset;
  271. u16 POST_table_offset;
  272. u16 XCLK;
  273. u16 MCLK;
  274. u8 num_PLL_blocks;
  275. u8 size_PLL_blocks;
  276. u16 PCLK_ref_freq;
  277. u16 PCLK_ref_divider;
  278. u32 PCLK_min_freq;
  279. u32 PCLK_max_freq;
  280. u16 MCLK_ref_freq;
  281. u16 MCLK_ref_divider;
  282. u32 MCLK_min_freq;
  283. u32 MCLK_max_freq;
  284. u16 XCLK_ref_freq;
  285. u16 XCLK_ref_divider;
  286. u32 XCLK_min_freq;
  287. u32 XCLK_max_freq;
  288. } __attribute__ ((packed)) PLL_BLOCK;
  289. #endif /* !CONFIG_PPC */
  290. /* onboard memory information */
  291. struct aty128_meminfo {
  292. u8 ML;
  293. u8 MB;
  294. u8 Trcd;
  295. u8 Trp;
  296. u8 Twr;
  297. u8 CL;
  298. u8 Tr2w;
  299. u8 LoopLatency;
  300. u8 DspOn;
  301. u8 Rloop;
  302. const char *name;
  303. };
  304. /* various memory configurations */
  305. static const struct aty128_meminfo sdr_128 =
  306. { 4, 4, 3, 3, 1, 3, 1, 16, 30, 16, "128-bit SDR SGRAM (1:1)" };
  307. static const struct aty128_meminfo sdr_64 =
  308. { 4, 8, 3, 3, 1, 3, 1, 17, 46, 17, "64-bit SDR SGRAM (1:1)" };
  309. static const struct aty128_meminfo sdr_sgram =
  310. { 4, 4, 1, 2, 1, 2, 1, 16, 24, 16, "64-bit SDR SGRAM (2:1)" };
  311. static const struct aty128_meminfo ddr_sgram =
  312. { 4, 4, 3, 3, 2, 3, 1, 16, 31, 16, "64-bit DDR SGRAM" };
  313. static struct fb_fix_screeninfo aty128fb_fix __devinitdata = {
  314. .id = "ATY Rage128",
  315. .type = FB_TYPE_PACKED_PIXELS,
  316. .visual = FB_VISUAL_PSEUDOCOLOR,
  317. .xpanstep = 8,
  318. .ypanstep = 1,
  319. .mmio_len = 0x2000,
  320. .accel = FB_ACCEL_ATI_RAGE128,
  321. };
  322. static char *mode_option __devinitdata = NULL;
  323. #ifdef CONFIG_PPC_PMAC
  324. static int default_vmode __devinitdata = VMODE_1024_768_60;
  325. static int default_cmode __devinitdata = CMODE_8;
  326. #endif
  327. static int default_crt_on __devinitdata = 0;
  328. static int default_lcd_on __devinitdata = 1;
  329. #ifdef CONFIG_MTRR
  330. static int mtrr = 1;
  331. #endif
  332. /* PLL constants */
  333. struct aty128_constants {
  334. u32 ref_clk;
  335. u32 ppll_min;
  336. u32 ppll_max;
  337. u32 ref_divider;
  338. u32 xclk;
  339. u32 fifo_width;
  340. u32 fifo_depth;
  341. };
  342. struct aty128_crtc {
  343. u32 gen_cntl;
  344. u32 h_total, h_sync_strt_wid;
  345. u32 v_total, v_sync_strt_wid;
  346. u32 pitch;
  347. u32 offset, offset_cntl;
  348. u32 xoffset, yoffset;
  349. u32 vxres, vyres;
  350. u32 depth, bpp;
  351. };
  352. struct aty128_pll {
  353. u32 post_divider;
  354. u32 feedback_divider;
  355. u32 vclk;
  356. };
  357. struct aty128_ddafifo {
  358. u32 dda_config;
  359. u32 dda_on_off;
  360. };
  361. /* register values for a specific mode */
  362. struct aty128fb_par {
  363. struct aty128_crtc crtc;
  364. struct aty128_pll pll;
  365. struct aty128_ddafifo fifo_reg;
  366. u32 accel_flags;
  367. struct aty128_constants constants; /* PLL and others */
  368. void __iomem *regbase; /* remapped mmio */
  369. u32 vram_size; /* onboard video ram */
  370. int chip_gen;
  371. const struct aty128_meminfo *mem; /* onboard mem info */
  372. #ifdef CONFIG_MTRR
  373. struct { int vram; int vram_valid; } mtrr;
  374. #endif
  375. int blitter_may_be_busy;
  376. int fifo_slots; /* free slots in FIFO (64 max) */
  377. int pm_reg;
  378. int crt_on, lcd_on;
  379. struct pci_dev *pdev;
  380. struct fb_info *next;
  381. int asleep;
  382. int lock_blank;
  383. u8 red[32]; /* see aty128fb_setcolreg */
  384. u8 green[64];
  385. u8 blue[32];
  386. u32 pseudo_palette[16]; /* used for TRUECOLOR */
  387. };
  388. #define round_div(n, d) ((n+(d/2))/d)
  389. static int aty128fb_check_var(struct fb_var_screeninfo *var,
  390. struct fb_info *info);
  391. static int aty128fb_set_par(struct fb_info *info);
  392. static int aty128fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  393. u_int transp, struct fb_info *info);
  394. static int aty128fb_pan_display(struct fb_var_screeninfo *var,
  395. struct fb_info *fb);
  396. static int aty128fb_blank(int blank, struct fb_info *fb);
  397. static int aty128fb_ioctl(struct fb_info *info, u_int cmd, unsigned long arg);
  398. static int aty128fb_sync(struct fb_info *info);
  399. /*
  400. * Internal routines
  401. */
  402. static int aty128_encode_var(struct fb_var_screeninfo *var,
  403. const struct aty128fb_par *par);
  404. static int aty128_decode_var(struct fb_var_screeninfo *var,
  405. struct aty128fb_par *par);
  406. #if 0
  407. static void __devinit aty128_get_pllinfo(struct aty128fb_par *par,
  408. void __iomem *bios);
  409. static void __devinit __iomem *aty128_map_ROM(struct pci_dev *pdev, const struct aty128fb_par *par);
  410. #endif
  411. static void aty128_timings(struct aty128fb_par *par);
  412. static void aty128_init_engine(struct aty128fb_par *par);
  413. static void aty128_reset_engine(const struct aty128fb_par *par);
  414. static void aty128_flush_pixel_cache(const struct aty128fb_par *par);
  415. static void do_wait_for_fifo(u16 entries, struct aty128fb_par *par);
  416. static void wait_for_fifo(u16 entries, struct aty128fb_par *par);
  417. static void wait_for_idle(struct aty128fb_par *par);
  418. static u32 depth_to_dst(u32 depth);
  419. static void aty128_bl_set_power(struct fb_info *info, int power);
  420. #define BIOS_IN8(v) (readb(bios + (v)))
  421. #define BIOS_IN16(v) (readb(bios + (v)) | \
  422. (readb(bios + (v) + 1) << 8))
  423. #define BIOS_IN32(v) (readb(bios + (v)) | \
  424. (readb(bios + (v) + 1) << 8) | \
  425. (readb(bios + (v) + 2) << 16) | \
  426. (readb(bios + (v) + 3) << 24))
  427. static struct fb_ops aty128fb_ops = {
  428. .owner = THIS_MODULE,
  429. .fb_check_var = aty128fb_check_var,
  430. .fb_set_par = aty128fb_set_par,
  431. .fb_setcolreg = aty128fb_setcolreg,
  432. .fb_pan_display = aty128fb_pan_display,
  433. .fb_blank = aty128fb_blank,
  434. .fb_ioctl = aty128fb_ioctl,
  435. .fb_sync = aty128fb_sync,
  436. .fb_fillrect = cfb_fillrect,
  437. .fb_copyarea = cfb_copyarea,
  438. .fb_imageblit = cfb_imageblit,
  439. };
  440. /*
  441. * Functions to read from/write to the mmio registers
  442. * - endian conversions may possibly be avoided by
  443. * using the other register aperture. TODO.
  444. */
  445. static inline u32 _aty_ld_le32(volatile unsigned int regindex,
  446. const struct aty128fb_par *par)
  447. {
  448. return readl (par->regbase + regindex);
  449. }
  450. static inline void _aty_st_le32(volatile unsigned int regindex, u32 val,
  451. const struct aty128fb_par *par)
  452. {
  453. writel (val, par->regbase + regindex);
  454. }
  455. static inline u8 _aty_ld_8(unsigned int regindex,
  456. const struct aty128fb_par *par)
  457. {
  458. return readb (par->regbase + regindex);
  459. }
  460. static inline void _aty_st_8(unsigned int regindex, u8 val,
  461. const struct aty128fb_par *par)
  462. {
  463. writeb (val, par->regbase + regindex);
  464. }
  465. #define aty_ld_le32(regindex) _aty_ld_le32(regindex, par)
  466. #define aty_st_le32(regindex, val) _aty_st_le32(regindex, val, par)
  467. #define aty_ld_8(regindex) _aty_ld_8(regindex, par)
  468. #define aty_st_8(regindex, val) _aty_st_8(regindex, val, par)
  469. /*
  470. * Functions to read from/write to the pll registers
  471. */
  472. #define aty_ld_pll(pll_index) _aty_ld_pll(pll_index, par)
  473. #define aty_st_pll(pll_index, val) _aty_st_pll(pll_index, val, par)
  474. static u32 _aty_ld_pll(unsigned int pll_index,
  475. const struct aty128fb_par *par)
  476. {
  477. aty_st_8(CLOCK_CNTL_INDEX, pll_index & 0x3F);
  478. return aty_ld_le32(CLOCK_CNTL_DATA);
  479. }
  480. static void _aty_st_pll(unsigned int pll_index, u32 val,
  481. const struct aty128fb_par *par)
  482. {
  483. aty_st_8(CLOCK_CNTL_INDEX, (pll_index & 0x3F) | PLL_WR_EN);
  484. aty_st_le32(CLOCK_CNTL_DATA, val);
  485. }
  486. /* return true when the PLL has completed an atomic update */
  487. static int aty_pll_readupdate(const struct aty128fb_par *par)
  488. {
  489. return !(aty_ld_pll(PPLL_REF_DIV) & PPLL_ATOMIC_UPDATE_R);
  490. }
  491. static void aty_pll_wait_readupdate(const struct aty128fb_par *par)
  492. {
  493. unsigned long timeout = jiffies + HZ/100; // should be more than enough
  494. int reset = 1;
  495. while (time_before(jiffies, timeout))
  496. if (aty_pll_readupdate(par)) {
  497. reset = 0;
  498. break;
  499. }
  500. if (reset) /* reset engine?? */
  501. printk(KERN_DEBUG "aty128fb: PLL write timeout!\n");
  502. }
  503. /* tell PLL to update */
  504. static void aty_pll_writeupdate(const struct aty128fb_par *par)
  505. {
  506. aty_pll_wait_readupdate(par);
  507. aty_st_pll(PPLL_REF_DIV,
  508. aty_ld_pll(PPLL_REF_DIV) | PPLL_ATOMIC_UPDATE_W);
  509. }
  510. /* write to the scratch register to test r/w functionality */
  511. static int __devinit register_test(const struct aty128fb_par *par)
  512. {
  513. u32 val;
  514. int flag = 0;
  515. val = aty_ld_le32(BIOS_0_SCRATCH);
  516. aty_st_le32(BIOS_0_SCRATCH, 0x55555555);
  517. if (aty_ld_le32(BIOS_0_SCRATCH) == 0x55555555) {
  518. aty_st_le32(BIOS_0_SCRATCH, 0xAAAAAAAA);
  519. if (aty_ld_le32(BIOS_0_SCRATCH) == 0xAAAAAAAA)
  520. flag = 1;
  521. }
  522. aty_st_le32(BIOS_0_SCRATCH, val); // restore value
  523. return flag;
  524. }
  525. /*
  526. * Accelerator engine functions
  527. */
  528. static void do_wait_for_fifo(u16 entries, struct aty128fb_par *par)
  529. {
  530. int i;
  531. for (;;) {
  532. for (i = 0; i < 2000000; i++) {
  533. par->fifo_slots = aty_ld_le32(GUI_STAT) & 0x0fff;
  534. if (par->fifo_slots >= entries)
  535. return;
  536. }
  537. aty128_reset_engine(par);
  538. }
  539. }
  540. static void wait_for_idle(struct aty128fb_par *par)
  541. {
  542. int i;
  543. do_wait_for_fifo(64, par);
  544. for (;;) {
  545. for (i = 0; i < 2000000; i++) {
  546. if (!(aty_ld_le32(GUI_STAT) & (1 << 31))) {
  547. aty128_flush_pixel_cache(par);
  548. par->blitter_may_be_busy = 0;
  549. return;
  550. }
  551. }
  552. aty128_reset_engine(par);
  553. }
  554. }
  555. static void wait_for_fifo(u16 entries, struct aty128fb_par *par)
  556. {
  557. if (par->fifo_slots < entries)
  558. do_wait_for_fifo(64, par);
  559. par->fifo_slots -= entries;
  560. }
  561. static void aty128_flush_pixel_cache(const struct aty128fb_par *par)
  562. {
  563. int i;
  564. u32 tmp;
  565. tmp = aty_ld_le32(PC_NGUI_CTLSTAT);
  566. tmp &= ~(0x00ff);
  567. tmp |= 0x00ff;
  568. aty_st_le32(PC_NGUI_CTLSTAT, tmp);
  569. for (i = 0; i < 2000000; i++)
  570. if (!(aty_ld_le32(PC_NGUI_CTLSTAT) & PC_BUSY))
  571. break;
  572. }
  573. static void aty128_reset_engine(const struct aty128fb_par *par)
  574. {
  575. u32 gen_reset_cntl, clock_cntl_index, mclk_cntl;
  576. aty128_flush_pixel_cache(par);
  577. clock_cntl_index = aty_ld_le32(CLOCK_CNTL_INDEX);
  578. mclk_cntl = aty_ld_pll(MCLK_CNTL);
  579. aty_st_pll(MCLK_CNTL, mclk_cntl | 0x00030000);
  580. gen_reset_cntl = aty_ld_le32(GEN_RESET_CNTL);
  581. aty_st_le32(GEN_RESET_CNTL, gen_reset_cntl | SOFT_RESET_GUI);
  582. aty_ld_le32(GEN_RESET_CNTL);
  583. aty_st_le32(GEN_RESET_CNTL, gen_reset_cntl & ~(SOFT_RESET_GUI));
  584. aty_ld_le32(GEN_RESET_CNTL);
  585. aty_st_pll(MCLK_CNTL, mclk_cntl);
  586. aty_st_le32(CLOCK_CNTL_INDEX, clock_cntl_index);
  587. aty_st_le32(GEN_RESET_CNTL, gen_reset_cntl);
  588. /* use old pio mode */
  589. aty_st_le32(PM4_BUFFER_CNTL, PM4_BUFFER_CNTL_NONPM4);
  590. DBG("engine reset");
  591. }
  592. static void aty128_init_engine(struct aty128fb_par *par)
  593. {
  594. u32 pitch_value;
  595. wait_for_idle(par);
  596. /* 3D scaler not spoken here */
  597. wait_for_fifo(1, par);
  598. aty_st_le32(SCALE_3D_CNTL, 0x00000000);
  599. aty128_reset_engine(par);
  600. pitch_value = par->crtc.pitch;
  601. if (par->crtc.bpp == 24) {
  602. pitch_value = pitch_value * 3;
  603. }
  604. wait_for_fifo(4, par);
  605. /* setup engine offset registers */
  606. aty_st_le32(DEFAULT_OFFSET, 0x00000000);
  607. /* setup engine pitch registers */
  608. aty_st_le32(DEFAULT_PITCH, pitch_value);
  609. /* set the default scissor register to max dimensions */
  610. aty_st_le32(DEFAULT_SC_BOTTOM_RIGHT, (0x1FFF << 16) | 0x1FFF);
  611. /* set the drawing controls registers */
  612. aty_st_le32(DP_GUI_MASTER_CNTL,
  613. GMC_SRC_PITCH_OFFSET_DEFAULT |
  614. GMC_DST_PITCH_OFFSET_DEFAULT |
  615. GMC_SRC_CLIP_DEFAULT |
  616. GMC_DST_CLIP_DEFAULT |
  617. GMC_BRUSH_SOLIDCOLOR |
  618. (depth_to_dst(par->crtc.depth) << 8) |
  619. GMC_SRC_DSTCOLOR |
  620. GMC_BYTE_ORDER_MSB_TO_LSB |
  621. GMC_DP_CONVERSION_TEMP_6500 |
  622. ROP3_PATCOPY |
  623. GMC_DP_SRC_RECT |
  624. GMC_3D_FCN_EN_CLR |
  625. GMC_DST_CLR_CMP_FCN_CLEAR |
  626. GMC_AUX_CLIP_CLEAR |
  627. GMC_WRITE_MASK_SET);
  628. wait_for_fifo(8, par);
  629. /* clear the line drawing registers */
  630. aty_st_le32(DST_BRES_ERR, 0);
  631. aty_st_le32(DST_BRES_INC, 0);
  632. aty_st_le32(DST_BRES_DEC, 0);
  633. /* set brush color registers */
  634. aty_st_le32(DP_BRUSH_FRGD_CLR, 0xFFFFFFFF); /* white */
  635. aty_st_le32(DP_BRUSH_BKGD_CLR, 0x00000000); /* black */
  636. /* set source color registers */
  637. aty_st_le32(DP_SRC_FRGD_CLR, 0xFFFFFFFF); /* white */
  638. aty_st_le32(DP_SRC_BKGD_CLR, 0x00000000); /* black */
  639. /* default write mask */
  640. aty_st_le32(DP_WRITE_MASK, 0xFFFFFFFF);
  641. /* Wait for all the writes to be completed before returning */
  642. wait_for_idle(par);
  643. }
  644. /* convert depth values to their register representation */
  645. static u32 depth_to_dst(u32 depth)
  646. {
  647. if (depth <= 8)
  648. return DST_8BPP;
  649. else if (depth <= 15)
  650. return DST_15BPP;
  651. else if (depth == 16)
  652. return DST_16BPP;
  653. else if (depth <= 24)
  654. return DST_24BPP;
  655. else if (depth <= 32)
  656. return DST_32BPP;
  657. return -EINVAL;
  658. }
  659. /*
  660. * PLL informations retreival
  661. */
  662. #ifndef __sparc__
  663. static void __iomem * __devinit aty128_map_ROM(const struct aty128fb_par *par, struct pci_dev *dev)
  664. {
  665. u16 dptr;
  666. u8 rom_type;
  667. void __iomem *bios;
  668. size_t rom_size;
  669. /* Fix from ATI for problem with Rage128 hardware not leaving ROM enabled */
  670. unsigned int temp;
  671. temp = aty_ld_le32(RAGE128_MPP_TB_CONFIG);
  672. temp &= 0x00ffffffu;
  673. temp |= 0x04 << 24;
  674. aty_st_le32(RAGE128_MPP_TB_CONFIG, temp);
  675. temp = aty_ld_le32(RAGE128_MPP_TB_CONFIG);
  676. bios = pci_map_rom(dev, &rom_size);
  677. if (!bios) {
  678. printk(KERN_ERR "aty128fb: ROM failed to map\n");
  679. return NULL;
  680. }
  681. /* Very simple test to make sure it appeared */
  682. if (BIOS_IN16(0) != 0xaa55) {
  683. printk(KERN_DEBUG "aty128fb: Invalid ROM signature %x should "
  684. " be 0xaa55\n", BIOS_IN16(0));
  685. goto failed;
  686. }
  687. /* Look for the PCI data to check the ROM type */
  688. dptr = BIOS_IN16(0x18);
  689. /* Check the PCI data signature. If it's wrong, we still assume a normal x86 ROM
  690. * for now, until I've verified this works everywhere. The goal here is more
  691. * to phase out Open Firmware images.
  692. *
  693. * Currently, we only look at the first PCI data, we could iteratre and deal with
  694. * them all, and we should use fb_bios_start relative to start of image and not
  695. * relative start of ROM, but so far, I never found a dual-image ATI card
  696. *
  697. * typedef struct {
  698. * u32 signature; + 0x00
  699. * u16 vendor; + 0x04
  700. * u16 device; + 0x06
  701. * u16 reserved_1; + 0x08
  702. * u16 dlen; + 0x0a
  703. * u8 drevision; + 0x0c
  704. * u8 class_hi; + 0x0d
  705. * u16 class_lo; + 0x0e
  706. * u16 ilen; + 0x10
  707. * u16 irevision; + 0x12
  708. * u8 type; + 0x14
  709. * u8 indicator; + 0x15
  710. * u16 reserved_2; + 0x16
  711. * } pci_data_t;
  712. */
  713. if (BIOS_IN32(dptr) != (('R' << 24) | ('I' << 16) | ('C' << 8) | 'P')) {
  714. printk(KERN_WARNING "aty128fb: PCI DATA signature in ROM incorrect: %08x\n",
  715. BIOS_IN32(dptr));
  716. goto anyway;
  717. }
  718. rom_type = BIOS_IN8(dptr + 0x14);
  719. switch(rom_type) {
  720. case 0:
  721. printk(KERN_INFO "aty128fb: Found Intel x86 BIOS ROM Image\n");
  722. break;
  723. case 1:
  724. printk(KERN_INFO "aty128fb: Found Open Firmware ROM Image\n");
  725. goto failed;
  726. case 2:
  727. printk(KERN_INFO "aty128fb: Found HP PA-RISC ROM Image\n");
  728. goto failed;
  729. default:
  730. printk(KERN_INFO "aty128fb: Found unknown type %d ROM Image\n", rom_type);
  731. goto failed;
  732. }
  733. anyway:
  734. return bios;
  735. failed:
  736. pci_unmap_rom(dev, bios);
  737. return NULL;
  738. }
  739. static void __devinit aty128_get_pllinfo(struct aty128fb_par *par, unsigned char __iomem *bios)
  740. {
  741. unsigned int bios_hdr;
  742. unsigned int bios_pll;
  743. bios_hdr = BIOS_IN16(0x48);
  744. bios_pll = BIOS_IN16(bios_hdr + 0x30);
  745. par->constants.ppll_max = BIOS_IN32(bios_pll + 0x16);
  746. par->constants.ppll_min = BIOS_IN32(bios_pll + 0x12);
  747. par->constants.xclk = BIOS_IN16(bios_pll + 0x08);
  748. par->constants.ref_divider = BIOS_IN16(bios_pll + 0x10);
  749. par->constants.ref_clk = BIOS_IN16(bios_pll + 0x0e);
  750. DBG("ppll_max %d ppll_min %d xclk %d ref_divider %d ref clock %d\n",
  751. par->constants.ppll_max, par->constants.ppll_min,
  752. par->constants.xclk, par->constants.ref_divider,
  753. par->constants.ref_clk);
  754. }
  755. #ifdef CONFIG_X86
  756. static void __iomem * __devinit aty128_find_mem_vbios(struct aty128fb_par *par)
  757. {
  758. /* I simplified this code as we used to miss the signatures in
  759. * a lot of case. It's now closer to XFree, we just don't check
  760. * for signatures at all... Something better will have to be done
  761. * if we end up having conflicts
  762. */
  763. u32 segstart;
  764. unsigned char __iomem *rom_base = NULL;
  765. for (segstart=0x000c0000; segstart<0x000f0000; segstart+=0x00001000) {
  766. rom_base = ioremap(segstart, 0x10000);
  767. if (rom_base == NULL)
  768. return NULL;
  769. if (readb(rom_base) == 0x55 && readb(rom_base + 1) == 0xaa)
  770. break;
  771. iounmap(rom_base);
  772. rom_base = NULL;
  773. }
  774. return rom_base;
  775. }
  776. #endif
  777. #endif /* ndef(__sparc__) */
  778. /* fill in known card constants if pll_block is not available */
  779. static void __devinit aty128_timings(struct aty128fb_par *par)
  780. {
  781. #ifdef CONFIG_PPC_OF
  782. /* instead of a table lookup, assume OF has properly
  783. * setup the PLL registers and use their values
  784. * to set the XCLK values and reference divider values */
  785. u32 x_mpll_ref_fb_div;
  786. u32 xclk_cntl;
  787. u32 Nx, M;
  788. unsigned PostDivSet[] = { 0, 1, 2, 4, 8, 3, 6, 12 };
  789. #endif
  790. if (!par->constants.ref_clk)
  791. par->constants.ref_clk = 2950;
  792. #ifdef CONFIG_PPC_OF
  793. x_mpll_ref_fb_div = aty_ld_pll(X_MPLL_REF_FB_DIV);
  794. xclk_cntl = aty_ld_pll(XCLK_CNTL) & 0x7;
  795. Nx = (x_mpll_ref_fb_div & 0x00ff00) >> 8;
  796. M = x_mpll_ref_fb_div & 0x0000ff;
  797. par->constants.xclk = round_div((2 * Nx * par->constants.ref_clk),
  798. (M * PostDivSet[xclk_cntl]));
  799. par->constants.ref_divider =
  800. aty_ld_pll(PPLL_REF_DIV) & PPLL_REF_DIV_MASK;
  801. #endif
  802. if (!par->constants.ref_divider) {
  803. par->constants.ref_divider = 0x3b;
  804. aty_st_pll(X_MPLL_REF_FB_DIV, 0x004c4c1e);
  805. aty_pll_writeupdate(par);
  806. }
  807. aty_st_pll(PPLL_REF_DIV, par->constants.ref_divider);
  808. aty_pll_writeupdate(par);
  809. /* from documentation */
  810. if (!par->constants.ppll_min)
  811. par->constants.ppll_min = 12500;
  812. if (!par->constants.ppll_max)
  813. par->constants.ppll_max = 25000; /* 23000 on some cards? */
  814. if (!par->constants.xclk)
  815. par->constants.xclk = 0x1d4d; /* same as mclk */
  816. par->constants.fifo_width = 128;
  817. par->constants.fifo_depth = 32;
  818. switch (aty_ld_le32(MEM_CNTL) & 0x3) {
  819. case 0:
  820. par->mem = &sdr_128;
  821. break;
  822. case 1:
  823. par->mem = &sdr_sgram;
  824. break;
  825. case 2:
  826. par->mem = &ddr_sgram;
  827. break;
  828. default:
  829. par->mem = &sdr_sgram;
  830. }
  831. }
  832. /*
  833. * CRTC programming
  834. */
  835. /* Program the CRTC registers */
  836. static void aty128_set_crtc(const struct aty128_crtc *crtc,
  837. const struct aty128fb_par *par)
  838. {
  839. aty_st_le32(CRTC_GEN_CNTL, crtc->gen_cntl);
  840. aty_st_le32(CRTC_H_TOTAL_DISP, crtc->h_total);
  841. aty_st_le32(CRTC_H_SYNC_STRT_WID, crtc->h_sync_strt_wid);
  842. aty_st_le32(CRTC_V_TOTAL_DISP, crtc->v_total);
  843. aty_st_le32(CRTC_V_SYNC_STRT_WID, crtc->v_sync_strt_wid);
  844. aty_st_le32(CRTC_PITCH, crtc->pitch);
  845. aty_st_le32(CRTC_OFFSET, crtc->offset);
  846. aty_st_le32(CRTC_OFFSET_CNTL, crtc->offset_cntl);
  847. /* Disable ATOMIC updating. Is this the right place? */
  848. aty_st_pll(PPLL_CNTL, aty_ld_pll(PPLL_CNTL) & ~(0x00030000));
  849. }
  850. static int aty128_var_to_crtc(const struct fb_var_screeninfo *var,
  851. struct aty128_crtc *crtc,
  852. const struct aty128fb_par *par)
  853. {
  854. u32 xres, yres, vxres, vyres, xoffset, yoffset, bpp, dst;
  855. u32 left, right, upper, lower, hslen, vslen, sync, vmode;
  856. u32 h_total, h_disp, h_sync_strt, h_sync_wid, h_sync_pol;
  857. u32 v_total, v_disp, v_sync_strt, v_sync_wid, v_sync_pol, c_sync;
  858. u32 depth, bytpp;
  859. u8 mode_bytpp[7] = { 0, 0, 1, 2, 2, 3, 4 };
  860. /* input */
  861. xres = var->xres;
  862. yres = var->yres;
  863. vxres = var->xres_virtual;
  864. vyres = var->yres_virtual;
  865. xoffset = var->xoffset;
  866. yoffset = var->yoffset;
  867. bpp = var->bits_per_pixel;
  868. left = var->left_margin;
  869. right = var->right_margin;
  870. upper = var->upper_margin;
  871. lower = var->lower_margin;
  872. hslen = var->hsync_len;
  873. vslen = var->vsync_len;
  874. sync = var->sync;
  875. vmode = var->vmode;
  876. if (bpp != 16)
  877. depth = bpp;
  878. else
  879. depth = (var->green.length == 6) ? 16 : 15;
  880. /* check for mode eligibility
  881. * accept only non interlaced modes */
  882. if ((vmode & FB_VMODE_MASK) != FB_VMODE_NONINTERLACED)
  883. return -EINVAL;
  884. /* convert (and round up) and validate */
  885. xres = (xres + 7) & ~7;
  886. xoffset = (xoffset + 7) & ~7;
  887. if (vxres < xres + xoffset)
  888. vxres = xres + xoffset;
  889. if (vyres < yres + yoffset)
  890. vyres = yres + yoffset;
  891. /* convert depth into ATI register depth */
  892. dst = depth_to_dst(depth);
  893. if (dst == -EINVAL) {
  894. printk(KERN_ERR "aty128fb: Invalid depth or RGBA\n");
  895. return -EINVAL;
  896. }
  897. /* convert register depth to bytes per pixel */
  898. bytpp = mode_bytpp[dst];
  899. /* make sure there is enough video ram for the mode */
  900. if ((u32)(vxres * vyres * bytpp) > par->vram_size) {
  901. printk(KERN_ERR "aty128fb: Not enough memory for mode\n");
  902. return -EINVAL;
  903. }
  904. h_disp = (xres >> 3) - 1;
  905. h_total = (((xres + right + hslen + left) >> 3) - 1) & 0xFFFFL;
  906. v_disp = yres - 1;
  907. v_total = (yres + upper + vslen + lower - 1) & 0xFFFFL;
  908. /* check to make sure h_total and v_total are in range */
  909. if (((h_total >> 3) - 1) > 0x1ff || (v_total - 1) > 0x7FF) {
  910. printk(KERN_ERR "aty128fb: invalid width ranges\n");
  911. return -EINVAL;
  912. }
  913. h_sync_wid = (hslen + 7) >> 3;
  914. if (h_sync_wid == 0)
  915. h_sync_wid = 1;
  916. else if (h_sync_wid > 0x3f) /* 0x3f = max hwidth */
  917. h_sync_wid = 0x3f;
  918. h_sync_strt = (h_disp << 3) + right;
  919. v_sync_wid = vslen;
  920. if (v_sync_wid == 0)
  921. v_sync_wid = 1;
  922. else if (v_sync_wid > 0x1f) /* 0x1f = max vwidth */
  923. v_sync_wid = 0x1f;
  924. v_sync_strt = v_disp + lower;
  925. h_sync_pol = sync & FB_SYNC_HOR_HIGH_ACT ? 0 : 1;
  926. v_sync_pol = sync & FB_SYNC_VERT_HIGH_ACT ? 0 : 1;
  927. c_sync = sync & FB_SYNC_COMP_HIGH_ACT ? (1 << 4) : 0;
  928. crtc->gen_cntl = 0x3000000L | c_sync | (dst << 8);
  929. crtc->h_total = h_total | (h_disp << 16);
  930. crtc->v_total = v_total | (v_disp << 16);
  931. crtc->h_sync_strt_wid = h_sync_strt | (h_sync_wid << 16) |
  932. (h_sync_pol << 23);
  933. crtc->v_sync_strt_wid = v_sync_strt | (v_sync_wid << 16) |
  934. (v_sync_pol << 23);
  935. crtc->pitch = vxres >> 3;
  936. crtc->offset = 0;
  937. if ((var->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW)
  938. crtc->offset_cntl = 0x00010000;
  939. else
  940. crtc->offset_cntl = 0;
  941. crtc->vxres = vxres;
  942. crtc->vyres = vyres;
  943. crtc->xoffset = xoffset;
  944. crtc->yoffset = yoffset;
  945. crtc->depth = depth;
  946. crtc->bpp = bpp;
  947. return 0;
  948. }
  949. static int aty128_pix_width_to_var(int pix_width, struct fb_var_screeninfo *var)
  950. {
  951. /* fill in pixel info */
  952. var->red.msb_right = 0;
  953. var->green.msb_right = 0;
  954. var->blue.offset = 0;
  955. var->blue.msb_right = 0;
  956. var->transp.offset = 0;
  957. var->transp.length = 0;
  958. var->transp.msb_right = 0;
  959. switch (pix_width) {
  960. case CRTC_PIX_WIDTH_8BPP:
  961. var->bits_per_pixel = 8;
  962. var->red.offset = 0;
  963. var->red.length = 8;
  964. var->green.offset = 0;
  965. var->green.length = 8;
  966. var->blue.length = 8;
  967. break;
  968. case CRTC_PIX_WIDTH_15BPP:
  969. var->bits_per_pixel = 16;
  970. var->red.offset = 10;
  971. var->red.length = 5;
  972. var->green.offset = 5;
  973. var->green.length = 5;
  974. var->blue.length = 5;
  975. break;
  976. case CRTC_PIX_WIDTH_16BPP:
  977. var->bits_per_pixel = 16;
  978. var->red.offset = 11;
  979. var->red.length = 5;
  980. var->green.offset = 5;
  981. var->green.length = 6;
  982. var->blue.length = 5;
  983. break;
  984. case CRTC_PIX_WIDTH_24BPP:
  985. var->bits_per_pixel = 24;
  986. var->red.offset = 16;
  987. var->red.length = 8;
  988. var->green.offset = 8;
  989. var->green.length = 8;
  990. var->blue.length = 8;
  991. break;
  992. case CRTC_PIX_WIDTH_32BPP:
  993. var->bits_per_pixel = 32;
  994. var->red.offset = 16;
  995. var->red.length = 8;
  996. var->green.offset = 8;
  997. var->green.length = 8;
  998. var->blue.length = 8;
  999. var->transp.offset = 24;
  1000. var->transp.length = 8;
  1001. break;
  1002. default:
  1003. printk(KERN_ERR "aty128fb: Invalid pixel width\n");
  1004. return -EINVAL;
  1005. }
  1006. return 0;
  1007. }
  1008. static int aty128_crtc_to_var(const struct aty128_crtc *crtc,
  1009. struct fb_var_screeninfo *var)
  1010. {
  1011. u32 xres, yres, left, right, upper, lower, hslen, vslen, sync;
  1012. u32 h_total, h_disp, h_sync_strt, h_sync_dly, h_sync_wid, h_sync_pol;
  1013. u32 v_total, v_disp, v_sync_strt, v_sync_wid, v_sync_pol, c_sync;
  1014. u32 pix_width;
  1015. /* fun with masking */
  1016. h_total = crtc->h_total & 0x1ff;
  1017. h_disp = (crtc->h_total >> 16) & 0xff;
  1018. h_sync_strt = (crtc->h_sync_strt_wid >> 3) & 0x1ff;
  1019. h_sync_dly = crtc->h_sync_strt_wid & 0x7;
  1020. h_sync_wid = (crtc->h_sync_strt_wid >> 16) & 0x3f;
  1021. h_sync_pol = (crtc->h_sync_strt_wid >> 23) & 0x1;
  1022. v_total = crtc->v_total & 0x7ff;
  1023. v_disp = (crtc->v_total >> 16) & 0x7ff;
  1024. v_sync_strt = crtc->v_sync_strt_wid & 0x7ff;
  1025. v_sync_wid = (crtc->v_sync_strt_wid >> 16) & 0x1f;
  1026. v_sync_pol = (crtc->v_sync_strt_wid >> 23) & 0x1;
  1027. c_sync = crtc->gen_cntl & CRTC_CSYNC_EN ? 1 : 0;
  1028. pix_width = crtc->gen_cntl & CRTC_PIX_WIDTH_MASK;
  1029. /* do conversions */
  1030. xres = (h_disp + 1) << 3;
  1031. yres = v_disp + 1;
  1032. left = ((h_total - h_sync_strt - h_sync_wid) << 3) - h_sync_dly;
  1033. right = ((h_sync_strt - h_disp) << 3) + h_sync_dly;
  1034. hslen = h_sync_wid << 3;
  1035. upper = v_total - v_sync_strt - v_sync_wid;
  1036. lower = v_sync_strt - v_disp;
  1037. vslen = v_sync_wid;
  1038. sync = (h_sync_pol ? 0 : FB_SYNC_HOR_HIGH_ACT) |
  1039. (v_sync_pol ? 0 : FB_SYNC_VERT_HIGH_ACT) |
  1040. (c_sync ? FB_SYNC_COMP_HIGH_ACT : 0);
  1041. aty128_pix_width_to_var(pix_width, var);
  1042. var->xres = xres;
  1043. var->yres = yres;
  1044. var->xres_virtual = crtc->vxres;
  1045. var->yres_virtual = crtc->vyres;
  1046. var->xoffset = crtc->xoffset;
  1047. var->yoffset = crtc->yoffset;
  1048. var->left_margin = left;
  1049. var->right_margin = right;
  1050. var->upper_margin = upper;
  1051. var->lower_margin = lower;
  1052. var->hsync_len = hslen;
  1053. var->vsync_len = vslen;
  1054. var->sync = sync;
  1055. var->vmode = FB_VMODE_NONINTERLACED;
  1056. return 0;
  1057. }
  1058. static void aty128_set_crt_enable(struct aty128fb_par *par, int on)
  1059. {
  1060. if (on) {
  1061. aty_st_le32(CRTC_EXT_CNTL, aty_ld_le32(CRTC_EXT_CNTL) | CRT_CRTC_ON);
  1062. aty_st_le32(DAC_CNTL, (aty_ld_le32(DAC_CNTL) | DAC_PALETTE2_SNOOP_EN));
  1063. } else
  1064. aty_st_le32(CRTC_EXT_CNTL, aty_ld_le32(CRTC_EXT_CNTL) & ~CRT_CRTC_ON);
  1065. }
  1066. static void aty128_set_lcd_enable(struct aty128fb_par *par, int on)
  1067. {
  1068. u32 reg;
  1069. #ifdef CONFIG_FB_ATY128_BACKLIGHT
  1070. struct fb_info *info = pci_get_drvdata(par->pdev);
  1071. #endif
  1072. if (on) {
  1073. reg = aty_ld_le32(LVDS_GEN_CNTL);
  1074. reg |= LVDS_ON | LVDS_EN | LVDS_BLON | LVDS_DIGION;
  1075. reg &= ~LVDS_DISPLAY_DIS;
  1076. aty_st_le32(LVDS_GEN_CNTL, reg);
  1077. #ifdef CONFIG_FB_ATY128_BACKLIGHT
  1078. aty128_bl_set_power(info, FB_BLANK_UNBLANK);
  1079. #endif
  1080. } else {
  1081. #ifdef CONFIG_FB_ATY128_BACKLIGHT
  1082. aty128_bl_set_power(info, FB_BLANK_POWERDOWN);
  1083. #endif
  1084. reg = aty_ld_le32(LVDS_GEN_CNTL);
  1085. reg |= LVDS_DISPLAY_DIS;
  1086. aty_st_le32(LVDS_GEN_CNTL, reg);
  1087. mdelay(100);
  1088. reg &= ~(LVDS_ON /*| LVDS_EN*/);
  1089. aty_st_le32(LVDS_GEN_CNTL, reg);
  1090. }
  1091. }
  1092. static void aty128_set_pll(struct aty128_pll *pll, const struct aty128fb_par *par)
  1093. {
  1094. u32 div3;
  1095. unsigned char post_conv[] = /* register values for post dividers */
  1096. { 2, 0, 1, 4, 2, 2, 6, 2, 3, 2, 2, 2, 7 };
  1097. /* select PPLL_DIV_3 */
  1098. aty_st_le32(CLOCK_CNTL_INDEX, aty_ld_le32(CLOCK_CNTL_INDEX) | (3 << 8));
  1099. /* reset PLL */
  1100. aty_st_pll(PPLL_CNTL,
  1101. aty_ld_pll(PPLL_CNTL) | PPLL_RESET | PPLL_ATOMIC_UPDATE_EN);
  1102. /* write the reference divider */
  1103. aty_pll_wait_readupdate(par);
  1104. aty_st_pll(PPLL_REF_DIV, par->constants.ref_divider & 0x3ff);
  1105. aty_pll_writeupdate(par);
  1106. div3 = aty_ld_pll(PPLL_DIV_3);
  1107. div3 &= ~PPLL_FB3_DIV_MASK;
  1108. div3 |= pll->feedback_divider;
  1109. div3 &= ~PPLL_POST3_DIV_MASK;
  1110. div3 |= post_conv[pll->post_divider] << 16;
  1111. /* write feedback and post dividers */
  1112. aty_pll_wait_readupdate(par);
  1113. aty_st_pll(PPLL_DIV_3, div3);
  1114. aty_pll_writeupdate(par);
  1115. aty_pll_wait_readupdate(par);
  1116. aty_st_pll(HTOTAL_CNTL, 0); /* no horiz crtc adjustment */
  1117. aty_pll_writeupdate(par);
  1118. /* clear the reset, just in case */
  1119. aty_st_pll(PPLL_CNTL, aty_ld_pll(PPLL_CNTL) & ~PPLL_RESET);
  1120. }
  1121. static int aty128_var_to_pll(u32 period_in_ps, struct aty128_pll *pll,
  1122. const struct aty128fb_par *par)
  1123. {
  1124. const struct aty128_constants c = par->constants;
  1125. unsigned char post_dividers[] = {1,2,4,8,3,6,12};
  1126. u32 output_freq;
  1127. u32 vclk; /* in .01 MHz */
  1128. int i = 0;
  1129. u32 n, d;
  1130. vclk = 100000000 / period_in_ps; /* convert units to 10 kHz */
  1131. /* adjust pixel clock if necessary */
  1132. if (vclk > c.ppll_max)
  1133. vclk = c.ppll_max;
  1134. if (vclk * 12 < c.ppll_min)
  1135. vclk = c.ppll_min/12;
  1136. /* now, find an acceptable divider */
  1137. for (i = 0; i < sizeof(post_dividers); i++) {
  1138. output_freq = post_dividers[i] * vclk;
  1139. if (output_freq >= c.ppll_min && output_freq <= c.ppll_max) {
  1140. pll->post_divider = post_dividers[i];
  1141. break;
  1142. }
  1143. }
  1144. /* calculate feedback divider */
  1145. n = c.ref_divider * output_freq;
  1146. d = c.ref_clk;
  1147. pll->feedback_divider = round_div(n, d);
  1148. pll->vclk = vclk;
  1149. DBG("post %d feedback %d vlck %d output %d ref_divider %d "
  1150. "vclk_per: %d\n", pll->post_divider,
  1151. pll->feedback_divider, vclk, output_freq,
  1152. c.ref_divider, period_in_ps);
  1153. return 0;
  1154. }
  1155. static int aty128_pll_to_var(const struct aty128_pll *pll, struct fb_var_screeninfo *var)
  1156. {
  1157. var->pixclock = 100000000 / pll->vclk;
  1158. return 0;
  1159. }
  1160. static void aty128_set_fifo(const struct aty128_ddafifo *dsp,
  1161. const struct aty128fb_par *par)
  1162. {
  1163. aty_st_le32(DDA_CONFIG, dsp->dda_config);
  1164. aty_st_le32(DDA_ON_OFF, dsp->dda_on_off);
  1165. }
  1166. static int aty128_ddafifo(struct aty128_ddafifo *dsp,
  1167. const struct aty128_pll *pll,
  1168. u32 depth,
  1169. const struct aty128fb_par *par)
  1170. {
  1171. const struct aty128_meminfo *m = par->mem;
  1172. u32 xclk = par->constants.xclk;
  1173. u32 fifo_width = par->constants.fifo_width;
  1174. u32 fifo_depth = par->constants.fifo_depth;
  1175. s32 x, b, p, ron, roff;
  1176. u32 n, d, bpp;
  1177. /* round up to multiple of 8 */
  1178. bpp = (depth+7) & ~7;
  1179. n = xclk * fifo_width;
  1180. d = pll->vclk * bpp;
  1181. x = round_div(n, d);
  1182. ron = 4 * m->MB +
  1183. 3 * ((m->Trcd - 2 > 0) ? m->Trcd - 2 : 0) +
  1184. 2 * m->Trp +
  1185. m->Twr +
  1186. m->CL +
  1187. m->Tr2w +
  1188. x;
  1189. DBG("x %x\n", x);
  1190. b = 0;
  1191. while (x) {
  1192. x >>= 1;
  1193. b++;
  1194. }
  1195. p = b + 1;
  1196. ron <<= (11 - p);
  1197. n <<= (11 - p);
  1198. x = round_div(n, d);
  1199. roff = x * (fifo_depth - 4);
  1200. if ((ron + m->Rloop) >= roff) {
  1201. printk(KERN_ERR "aty128fb: Mode out of range!\n");
  1202. return -EINVAL;
  1203. }
  1204. DBG("p: %x rloop: %x x: %x ron: %x roff: %x\n",
  1205. p, m->Rloop, x, ron, roff);
  1206. dsp->dda_config = p << 16 | m->Rloop << 20 | x;
  1207. dsp->dda_on_off = ron << 16 | roff;
  1208. return 0;
  1209. }
  1210. /*
  1211. * This actually sets the video mode.
  1212. */
  1213. static int aty128fb_set_par(struct fb_info *info)
  1214. {
  1215. struct aty128fb_par *par = info->par;
  1216. u32 config;
  1217. int err;
  1218. if ((err = aty128_decode_var(&info->var, par)) != 0)
  1219. return err;
  1220. if (par->blitter_may_be_busy)
  1221. wait_for_idle(par);
  1222. /* clear all registers that may interfere with mode setting */
  1223. aty_st_le32(OVR_CLR, 0);
  1224. aty_st_le32(OVR_WID_LEFT_RIGHT, 0);
  1225. aty_st_le32(OVR_WID_TOP_BOTTOM, 0);
  1226. aty_st_le32(OV0_SCALE_CNTL, 0);
  1227. aty_st_le32(MPP_TB_CONFIG, 0);
  1228. aty_st_le32(MPP_GP_CONFIG, 0);
  1229. aty_st_le32(SUBPIC_CNTL, 0);
  1230. aty_st_le32(VIPH_CONTROL, 0);
  1231. aty_st_le32(I2C_CNTL_1, 0); /* turn off i2c */
  1232. aty_st_le32(GEN_INT_CNTL, 0); /* turn off interrupts */
  1233. aty_st_le32(CAP0_TRIG_CNTL, 0);
  1234. aty_st_le32(CAP1_TRIG_CNTL, 0);
  1235. aty_st_8(CRTC_EXT_CNTL + 1, 4); /* turn video off */
  1236. aty128_set_crtc(&par->crtc, par);
  1237. aty128_set_pll(&par->pll, par);
  1238. aty128_set_fifo(&par->fifo_reg, par);
  1239. config = aty_ld_le32(CONFIG_CNTL) & ~3;
  1240. #if defined(__BIG_ENDIAN)
  1241. if (par->crtc.bpp == 32)
  1242. config |= 2; /* make aperture do 32 bit swapping */
  1243. else if (par->crtc.bpp == 16)
  1244. config |= 1; /* make aperture do 16 bit swapping */
  1245. #endif
  1246. aty_st_le32(CONFIG_CNTL, config);
  1247. aty_st_8(CRTC_EXT_CNTL + 1, 0); /* turn the video back on */
  1248. info->fix.line_length = (par->crtc.vxres * par->crtc.bpp) >> 3;
  1249. info->fix.visual = par->crtc.bpp == 8 ? FB_VISUAL_PSEUDOCOLOR
  1250. : FB_VISUAL_DIRECTCOLOR;
  1251. if (par->chip_gen == rage_M3) {
  1252. aty128_set_crt_enable(par, par->crt_on);
  1253. aty128_set_lcd_enable(par, par->lcd_on);
  1254. }
  1255. if (par->accel_flags & FB_ACCELF_TEXT)
  1256. aty128_init_engine(par);
  1257. #ifdef CONFIG_BOOTX_TEXT
  1258. btext_update_display(info->fix.smem_start,
  1259. (((par->crtc.h_total>>16) & 0xff)+1)*8,
  1260. ((par->crtc.v_total>>16) & 0x7ff)+1,
  1261. par->crtc.bpp,
  1262. par->crtc.vxres*par->crtc.bpp/8);
  1263. #endif /* CONFIG_BOOTX_TEXT */
  1264. return 0;
  1265. }
  1266. /*
  1267. * encode/decode the User Defined Part of the Display
  1268. */
  1269. static int aty128_decode_var(struct fb_var_screeninfo *var, struct aty128fb_par *par)
  1270. {
  1271. int err;
  1272. struct aty128_crtc crtc;
  1273. struct aty128_pll pll;
  1274. struct aty128_ddafifo fifo_reg;
  1275. if ((err = aty128_var_to_crtc(var, &crtc, par)))
  1276. return err;
  1277. if ((err = aty128_var_to_pll(var->pixclock, &pll, par)))
  1278. return err;
  1279. if ((err = aty128_ddafifo(&fifo_reg, &pll, crtc.depth, par)))
  1280. return err;
  1281. par->crtc = crtc;
  1282. par->pll = pll;
  1283. par->fifo_reg = fifo_reg;
  1284. par->accel_flags = var->accel_flags;
  1285. return 0;
  1286. }
  1287. static int aty128_encode_var(struct fb_var_screeninfo *var,
  1288. const struct aty128fb_par *par)
  1289. {
  1290. int err;
  1291. if ((err = aty128_crtc_to_var(&par->crtc, var)))
  1292. return err;
  1293. if ((err = aty128_pll_to_var(&par->pll, var)))
  1294. return err;
  1295. var->nonstd = 0;
  1296. var->activate = 0;
  1297. var->height = -1;
  1298. var->width = -1;
  1299. var->accel_flags = par->accel_flags;
  1300. return 0;
  1301. }
  1302. static int aty128fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  1303. {
  1304. struct aty128fb_par par;
  1305. int err;
  1306. par = *(struct aty128fb_par *)info->par;
  1307. if ((err = aty128_decode_var(var, &par)) != 0)
  1308. return err;
  1309. aty128_encode_var(var, &par);
  1310. return 0;
  1311. }
  1312. /*
  1313. * Pan or Wrap the Display
  1314. */
  1315. static int aty128fb_pan_display(struct fb_var_screeninfo *var, struct fb_info *fb)
  1316. {
  1317. struct aty128fb_par *par = fb->par;
  1318. u32 xoffset, yoffset;
  1319. u32 offset;
  1320. u32 xres, yres;
  1321. xres = (((par->crtc.h_total >> 16) & 0xff) + 1) << 3;
  1322. yres = ((par->crtc.v_total >> 16) & 0x7ff) + 1;
  1323. xoffset = (var->xoffset +7) & ~7;
  1324. yoffset = var->yoffset;
  1325. if (xoffset+xres > par->crtc.vxres || yoffset+yres > par->crtc.vyres)
  1326. return -EINVAL;
  1327. par->crtc.xoffset = xoffset;
  1328. par->crtc.yoffset = yoffset;
  1329. offset = ((yoffset * par->crtc.vxres + xoffset)*(par->crtc.bpp >> 3)) & ~7;
  1330. if (par->crtc.bpp == 24)
  1331. offset += 8 * (offset % 3); /* Must be multiple of 8 and 3 */
  1332. aty_st_le32(CRTC_OFFSET, offset);
  1333. return 0;
  1334. }
  1335. /*
  1336. * Helper function to store a single palette register
  1337. */
  1338. static void aty128_st_pal(u_int regno, u_int red, u_int green, u_int blue,
  1339. struct aty128fb_par *par)
  1340. {
  1341. if (par->chip_gen == rage_M3) {
  1342. #if 0
  1343. /* Note: For now, on M3, we set palette on both heads, which may
  1344. * be useless. Can someone with a M3 check this ?
  1345. *
  1346. * This code would still be useful if using the second CRTC to
  1347. * do mirroring
  1348. */
  1349. aty_st_le32(DAC_CNTL, aty_ld_le32(DAC_CNTL) | DAC_PALETTE_ACCESS_CNTL);
  1350. aty_st_8(PALETTE_INDEX, regno);
  1351. aty_st_le32(PALETTE_DATA, (red<<16)|(green<<8)|blue);
  1352. #endif
  1353. aty_st_le32(DAC_CNTL, aty_ld_le32(DAC_CNTL) & ~DAC_PALETTE_ACCESS_CNTL);
  1354. }
  1355. aty_st_8(PALETTE_INDEX, regno);
  1356. aty_st_le32(PALETTE_DATA, (red<<16)|(green<<8)|blue);
  1357. }
  1358. static int aty128fb_sync(struct fb_info *info)
  1359. {
  1360. struct aty128fb_par *par = info->par;
  1361. if (par->blitter_may_be_busy)
  1362. wait_for_idle(par);
  1363. return 0;
  1364. }
  1365. #ifndef MODULE
  1366. static int __devinit aty128fb_setup(char *options)
  1367. {
  1368. char *this_opt;
  1369. if (!options || !*options)
  1370. return 0;
  1371. while ((this_opt = strsep(&options, ",")) != NULL) {
  1372. if (!strncmp(this_opt, "lcd:", 4)) {
  1373. default_lcd_on = simple_strtoul(this_opt+4, NULL, 0);
  1374. continue;
  1375. } else if (!strncmp(this_opt, "crt:", 4)) {
  1376. default_crt_on = simple_strtoul(this_opt+4, NULL, 0);
  1377. continue;
  1378. }
  1379. #ifdef CONFIG_MTRR
  1380. if(!strncmp(this_opt, "nomtrr", 6)) {
  1381. mtrr = 0;
  1382. continue;
  1383. }
  1384. #endif
  1385. #ifdef CONFIG_PPC_PMAC
  1386. /* vmode and cmode deprecated */
  1387. if (!strncmp(this_opt, "vmode:", 6)) {
  1388. unsigned int vmode = simple_strtoul(this_opt+6, NULL, 0);
  1389. if (vmode > 0 && vmode <= VMODE_MAX)
  1390. default_vmode = vmode;
  1391. continue;
  1392. } else if (!strncmp(this_opt, "cmode:", 6)) {
  1393. unsigned int cmode = simple_strtoul(this_opt+6, NULL, 0);
  1394. switch (cmode) {
  1395. case 0:
  1396. case 8:
  1397. default_cmode = CMODE_8;
  1398. break;
  1399. case 15:
  1400. case 16:
  1401. default_cmode = CMODE_16;
  1402. break;
  1403. case 24:
  1404. case 32:
  1405. default_cmode = CMODE_32;
  1406. break;
  1407. }
  1408. continue;
  1409. }
  1410. #endif /* CONFIG_PPC_PMAC */
  1411. mode_option = this_opt;
  1412. }
  1413. return 0;
  1414. }
  1415. #endif /* MODULE */
  1416. /* Backlight */
  1417. #ifdef CONFIG_FB_ATY128_BACKLIGHT
  1418. #define MAX_LEVEL 0xFF
  1419. static struct backlight_properties aty128_bl_data;
  1420. /* Call with fb_info->bl_mutex held */
  1421. static int aty128_bl_get_level_brightness(struct aty128fb_par *par,
  1422. int level)
  1423. {
  1424. struct fb_info *info = pci_get_drvdata(par->pdev);
  1425. int atylevel;
  1426. /* Get and convert the value */
  1427. atylevel = MAX_LEVEL -
  1428. (info->bl_curve[level] * FB_BACKLIGHT_MAX / MAX_LEVEL);
  1429. if (atylevel < 0)
  1430. atylevel = 0;
  1431. else if (atylevel > MAX_LEVEL)
  1432. atylevel = MAX_LEVEL;
  1433. return atylevel;
  1434. }
  1435. /* We turn off the LCD completely instead of just dimming the backlight.
  1436. * This provides greater power saving and the display is useless without
  1437. * backlight anyway
  1438. */
  1439. #define BACKLIGHT_LVDS_OFF
  1440. /* That one prevents proper CRT output with LCD off */
  1441. #undef BACKLIGHT_DAC_OFF
  1442. /* Call with fb_info->bl_mutex held */
  1443. static int __aty128_bl_update_status(struct backlight_device *bd)
  1444. {
  1445. struct aty128fb_par *par = class_get_devdata(&bd->class_dev);
  1446. unsigned int reg = aty_ld_le32(LVDS_GEN_CNTL);
  1447. int level;
  1448. if (bd->props->power != FB_BLANK_UNBLANK ||
  1449. bd->props->fb_blank != FB_BLANK_UNBLANK ||
  1450. !par->lcd_on)
  1451. level = 0;
  1452. else
  1453. level = bd->props->brightness;
  1454. reg |= LVDS_BL_MOD_EN | LVDS_BLON;
  1455. if (level > 0) {
  1456. reg |= LVDS_DIGION;
  1457. if (!(reg & LVDS_ON)) {
  1458. reg &= ~LVDS_BLON;
  1459. aty_st_le32(LVDS_GEN_CNTL, reg);
  1460. aty_ld_le32(LVDS_GEN_CNTL);
  1461. mdelay(10);
  1462. reg |= LVDS_BLON;
  1463. aty_st_le32(LVDS_GEN_CNTL, reg);
  1464. }
  1465. reg &= ~LVDS_BL_MOD_LEVEL_MASK;
  1466. reg |= (aty128_bl_get_level_brightness(par, level) << LVDS_BL_MOD_LEVEL_SHIFT);
  1467. #ifdef BACKLIGHT_LVDS_OFF
  1468. reg |= LVDS_ON | LVDS_EN;
  1469. reg &= ~LVDS_DISPLAY_DIS;
  1470. #endif
  1471. aty_st_le32(LVDS_GEN_CNTL, reg);
  1472. #ifdef BACKLIGHT_DAC_OFF
  1473. aty_st_le32(DAC_CNTL, aty_ld_le32(DAC_CNTL) & (~DAC_PDWN));
  1474. #endif
  1475. } else {
  1476. reg &= ~LVDS_BL_MOD_LEVEL_MASK;
  1477. reg |= (aty128_bl_get_level_brightness(par, 0) << LVDS_BL_MOD_LEVEL_SHIFT);
  1478. #ifdef BACKLIGHT_LVDS_OFF
  1479. reg |= LVDS_DISPLAY_DIS;
  1480. aty_st_le32(LVDS_GEN_CNTL, reg);
  1481. aty_ld_le32(LVDS_GEN_CNTL);
  1482. udelay(10);
  1483. reg &= ~(LVDS_ON | LVDS_EN | LVDS_BLON | LVDS_DIGION);
  1484. #endif
  1485. aty_st_le32(LVDS_GEN_CNTL, reg);
  1486. #ifdef BACKLIGHT_DAC_OFF
  1487. aty_st_le32(DAC_CNTL, aty_ld_le32(DAC_CNTL) | DAC_PDWN);
  1488. #endif
  1489. }
  1490. return 0;
  1491. }
  1492. static int aty128_bl_update_status(struct backlight_device *bd)
  1493. {
  1494. struct aty128fb_par *par = class_get_devdata(&bd->class_dev);
  1495. struct fb_info *info = pci_get_drvdata(par->pdev);
  1496. int ret;
  1497. mutex_lock(&info->bl_mutex);
  1498. ret = __aty128_bl_update_status(bd);
  1499. mutex_unlock(&info->bl_mutex);
  1500. return ret;
  1501. }
  1502. static int aty128_bl_get_brightness(struct backlight_device *bd)
  1503. {
  1504. return bd->props->brightness;
  1505. }
  1506. static struct backlight_properties aty128_bl_data = {
  1507. .owner = THIS_MODULE,
  1508. .get_brightness = aty128_bl_get_brightness,
  1509. .update_status = aty128_bl_update_status,
  1510. .max_brightness = (FB_BACKLIGHT_LEVELS - 1),
  1511. };
  1512. static void aty128_bl_set_power(struct fb_info *info, int power)
  1513. {
  1514. mutex_lock(&info->bl_mutex);
  1515. up(&info->bl_dev->sem);
  1516. info->bl_dev->props->power = power;
  1517. __aty128_bl_update_status(info->bl_dev);
  1518. down(&info->bl_dev->sem);
  1519. mutex_unlock(&info->bl_mutex);
  1520. }
  1521. static void aty128_bl_init(struct aty128fb_par *par)
  1522. {
  1523. struct fb_info *info = pci_get_drvdata(par->pdev);
  1524. struct backlight_device *bd;
  1525. char name[12];
  1526. /* Could be extended to Rage128Pro LVDS output too */
  1527. if (par->chip_gen != rage_M3)
  1528. return;
  1529. #ifdef CONFIG_PMAC_BACKLIGHT
  1530. if (!pmac_has_backlight_type("ati"))
  1531. return;
  1532. #endif
  1533. snprintf(name, sizeof(name), "aty128bl%d", info->node);
  1534. bd = backlight_device_register(name, par, &aty128_bl_data);
  1535. if (IS_ERR(bd)) {
  1536. info->bl_dev = NULL;
  1537. printk("aty128: Backlight registration failed\n");
  1538. goto error;
  1539. }
  1540. mutex_lock(&info->bl_mutex);
  1541. info->bl_dev = bd;
  1542. fb_bl_default_curve(info, 0,
  1543. 63 * FB_BACKLIGHT_MAX / MAX_LEVEL,
  1544. 219 * FB_BACKLIGHT_MAX / MAX_LEVEL);
  1545. mutex_unlock(&info->bl_mutex);
  1546. up(&bd->sem);
  1547. bd->props->brightness = aty128_bl_data.max_brightness;
  1548. bd->props->power = FB_BLANK_UNBLANK;
  1549. bd->props->update_status(bd);
  1550. down(&bd->sem);
  1551. #ifdef CONFIG_PMAC_BACKLIGHT
  1552. mutex_lock(&pmac_backlight_mutex);
  1553. if (!pmac_backlight)
  1554. pmac_backlight = bd;
  1555. mutex_unlock(&pmac_backlight_mutex);
  1556. #endif
  1557. printk("aty128: Backlight initialized (%s)\n", name);
  1558. return;
  1559. error:
  1560. return;
  1561. }
  1562. static void aty128_bl_exit(struct aty128fb_par *par)
  1563. {
  1564. struct fb_info *info = pci_get_drvdata(par->pdev);
  1565. #ifdef CONFIG_PMAC_BACKLIGHT
  1566. mutex_lock(&pmac_backlight_mutex);
  1567. #endif
  1568. mutex_lock(&info->bl_mutex);
  1569. if (info->bl_dev) {
  1570. #ifdef CONFIG_PMAC_BACKLIGHT
  1571. if (pmac_backlight == info->bl_dev)
  1572. pmac_backlight = NULL;
  1573. #endif
  1574. backlight_device_unregister(info->bl_dev);
  1575. info->bl_dev = NULL;
  1576. printk("aty128: Backlight unloaded\n");
  1577. }
  1578. mutex_unlock(&info->bl_mutex);
  1579. #ifdef CONFIG_PMAC_BACKLIGHT
  1580. mutex_unlock(&pmac_backlight_mutex);
  1581. #endif
  1582. }
  1583. #endif /* CONFIG_FB_ATY128_BACKLIGHT */
  1584. /*
  1585. * Initialisation
  1586. */
  1587. #ifdef CONFIG_PPC_PMAC
  1588. static void aty128_early_resume(void *data)
  1589. {
  1590. struct aty128fb_par *par = data;
  1591. if (try_acquire_console_sem())
  1592. return;
  1593. aty128_do_resume(par->pdev);
  1594. release_console_sem();
  1595. }
  1596. #endif /* CONFIG_PPC_PMAC */
  1597. static int __devinit aty128_init(struct pci_dev *pdev, const struct pci_device_id *ent)
  1598. {
  1599. struct fb_info *info = pci_get_drvdata(pdev);
  1600. struct aty128fb_par *par = info->par;
  1601. struct fb_var_screeninfo var;
  1602. char video_card[DEVICE_NAME_SIZE];
  1603. u8 chip_rev;
  1604. u32 dac;
  1605. if (!par->vram_size) /* may have already been probed */
  1606. par->vram_size = aty_ld_le32(CONFIG_MEMSIZE) & 0x03FFFFFF;
  1607. /* Get the chip revision */
  1608. chip_rev = (aty_ld_le32(CONFIG_CNTL) >> 16) & 0x1F;
  1609. strcpy(video_card, "Rage128 XX ");
  1610. video_card[8] = ent->device >> 8;
  1611. video_card[9] = ent->device & 0xFF;
  1612. /* range check to make sure */
  1613. if (ent->driver_data < ARRAY_SIZE(r128_family))
  1614. strncat(video_card, r128_family[ent->driver_data], sizeof(video_card));
  1615. printk(KERN_INFO "aty128fb: %s [chip rev 0x%x] ", video_card, chip_rev);
  1616. if (par->vram_size % (1024 * 1024) == 0)
  1617. printk("%dM %s\n", par->vram_size / (1024*1024), par->mem->name);
  1618. else
  1619. printk("%dk %s\n", par->vram_size / 1024, par->mem->name);
  1620. par->chip_gen = ent->driver_data;
  1621. /* fill in info */
  1622. info->fbops = &aty128fb_ops;
  1623. info->flags = FBINFO_FLAG_DEFAULT;
  1624. par->lcd_on = default_lcd_on;
  1625. par->crt_on = default_crt_on;
  1626. var = default_var;
  1627. #ifdef CONFIG_PPC_PMAC
  1628. if (machine_is(powermac)) {
  1629. /* Indicate sleep capability */
  1630. if (par->chip_gen == rage_M3) {
  1631. pmac_call_feature(PMAC_FTR_DEVICE_CAN_WAKE, NULL, 0, 1);
  1632. pmac_set_early_video_resume(aty128_early_resume, par);
  1633. }
  1634. /* Find default mode */
  1635. if (mode_option) {
  1636. if (!mac_find_mode(&var, info, mode_option, 8))
  1637. var = default_var;
  1638. } else {
  1639. if (default_vmode <= 0 || default_vmode > VMODE_MAX)
  1640. default_vmode = VMODE_1024_768_60;
  1641. /* iMacs need that resolution
  1642. * PowerMac2,1 first r128 iMacs
  1643. * PowerMac2,2 summer 2000 iMacs
  1644. * PowerMac4,1 january 2001 iMacs "flower power"
  1645. */
  1646. if (machine_is_compatible("PowerMac2,1") ||
  1647. machine_is_compatible("PowerMac2,2") ||
  1648. machine_is_compatible("PowerMac4,1"))
  1649. default_vmode = VMODE_1024_768_75;
  1650. /* iBook SE */
  1651. if (machine_is_compatible("PowerBook2,2"))
  1652. default_vmode = VMODE_800_600_60;
  1653. /* PowerBook Firewire (Pismo), iBook Dual USB */
  1654. if (machine_is_compatible("PowerBook3,1") ||
  1655. machine_is_compatible("PowerBook4,1"))
  1656. default_vmode = VMODE_1024_768_60;
  1657. /* PowerBook Titanium */
  1658. if (machine_is_compatible("PowerBook3,2"))
  1659. default_vmode = VMODE_1152_768_60;
  1660. if (default_cmode > 16)
  1661. default_cmode = CMODE_32;
  1662. else if (default_cmode > 8)
  1663. default_cmode = CMODE_16;
  1664. else
  1665. default_cmode = CMODE_8;
  1666. if (mac_vmode_to_var(default_vmode, default_cmode, &var))
  1667. var = default_var;
  1668. }
  1669. } else
  1670. #endif /* CONFIG_PPC_PMAC */
  1671. {
  1672. if (mode_option)
  1673. if (fb_find_mode(&var, info, mode_option, NULL,
  1674. 0, &defaultmode, 8) == 0)
  1675. var = default_var;
  1676. }
  1677. var.accel_flags &= ~FB_ACCELF_TEXT;
  1678. // var.accel_flags |= FB_ACCELF_TEXT;/* FIXME Will add accel later */
  1679. if (aty128fb_check_var(&var, info)) {
  1680. printk(KERN_ERR "aty128fb: Cannot set default mode.\n");
  1681. return 0;
  1682. }
  1683. /* setup the DAC the way we like it */
  1684. dac = aty_ld_le32(DAC_CNTL);
  1685. dac |= (DAC_8BIT_EN | DAC_RANGE_CNTL);
  1686. dac |= DAC_MASK;
  1687. if (par->chip_gen == rage_M3)
  1688. dac |= DAC_PALETTE2_SNOOP_EN;
  1689. aty_st_le32(DAC_CNTL, dac);
  1690. /* turn off bus mastering, just in case */
  1691. aty_st_le32(BUS_CNTL, aty_ld_le32(BUS_CNTL) | BUS_MASTER_DIS);
  1692. info->var = var;
  1693. fb_alloc_cmap(&info->cmap, 256, 0);
  1694. var.activate = FB_ACTIVATE_NOW;
  1695. aty128_init_engine(par);
  1696. if (register_framebuffer(info) < 0)
  1697. return 0;
  1698. par->pm_reg = pci_find_capability(pdev, PCI_CAP_ID_PM);
  1699. par->pdev = pdev;
  1700. par->asleep = 0;
  1701. par->lock_blank = 0;
  1702. #ifdef CONFIG_FB_ATY128_BACKLIGHT
  1703. aty128_bl_init(par);
  1704. #endif
  1705. printk(KERN_INFO "fb%d: %s frame buffer device on %s\n",
  1706. info->node, info->fix.id, video_card);
  1707. return 1; /* success! */
  1708. }
  1709. #ifdef CONFIG_PCI
  1710. /* register a card ++ajoshi */
  1711. static int __devinit aty128_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  1712. {
  1713. unsigned long fb_addr, reg_addr;
  1714. struct aty128fb_par *par;
  1715. struct fb_info *info;
  1716. int err;
  1717. #ifndef __sparc__
  1718. void __iomem *bios = NULL;
  1719. #endif
  1720. /* Enable device in PCI config */
  1721. if ((err = pci_enable_device(pdev))) {
  1722. printk(KERN_ERR "aty128fb: Cannot enable PCI device: %d\n",
  1723. err);
  1724. return -ENODEV;
  1725. }
  1726. fb_addr = pci_resource_start(pdev, 0);
  1727. if (!request_mem_region(fb_addr, pci_resource_len(pdev, 0),
  1728. "aty128fb FB")) {
  1729. printk(KERN_ERR "aty128fb: cannot reserve frame "
  1730. "buffer memory\n");
  1731. return -ENODEV;
  1732. }
  1733. reg_addr = pci_resource_start(pdev, 2);
  1734. if (!request_mem_region(reg_addr, pci_resource_len(pdev, 2),
  1735. "aty128fb MMIO")) {
  1736. printk(KERN_ERR "aty128fb: cannot reserve MMIO region\n");
  1737. goto err_free_fb;
  1738. }
  1739. /* We have the resources. Now virtualize them */
  1740. info = framebuffer_alloc(sizeof(struct aty128fb_par), &pdev->dev);
  1741. if (info == NULL) {
  1742. printk(KERN_ERR "aty128fb: can't alloc fb_info_aty128\n");
  1743. goto err_free_mmio;
  1744. }
  1745. par = info->par;
  1746. info->pseudo_palette = par->pseudo_palette;
  1747. info->fix = aty128fb_fix;
  1748. /* Virtualize mmio region */
  1749. info->fix.mmio_start = reg_addr;
  1750. par->regbase = ioremap(reg_addr, pci_resource_len(pdev, 2));
  1751. if (!par->regbase)
  1752. goto err_free_info;
  1753. /* Grab memory size from the card */
  1754. // How does this relate to the resource length from the PCI hardware?
  1755. par->vram_size = aty_ld_le32(CONFIG_MEMSIZE) & 0x03FFFFFF;
  1756. /* Virtualize the framebuffer */
  1757. info->screen_base = ioremap(fb_addr, par->vram_size);
  1758. if (!info->screen_base)
  1759. goto err_unmap_out;
  1760. /* Set up info->fix */
  1761. info->fix = aty128fb_fix;
  1762. info->fix.smem_start = fb_addr;
  1763. info->fix.smem_len = par->vram_size;
  1764. info->fix.mmio_start = reg_addr;
  1765. /* If we can't test scratch registers, something is seriously wrong */
  1766. if (!register_test(par)) {
  1767. printk(KERN_ERR "aty128fb: Can't write to video register!\n");
  1768. goto err_out;
  1769. }
  1770. #ifndef __sparc__
  1771. bios = aty128_map_ROM(par, pdev);
  1772. #ifdef CONFIG_X86
  1773. if (bios == NULL)
  1774. bios = aty128_find_mem_vbios(par);
  1775. #endif
  1776. if (bios == NULL)
  1777. printk(KERN_INFO "aty128fb: BIOS not located, guessing timings.\n");
  1778. else {
  1779. printk(KERN_INFO "aty128fb: Rage128 BIOS located\n");
  1780. aty128_get_pllinfo(par, bios);
  1781. pci_unmap_rom(pdev, bios);
  1782. }
  1783. #endif /* __sparc__ */
  1784. aty128_timings(par);
  1785. pci_set_drvdata(pdev, info);
  1786. if (!aty128_init(pdev, ent))
  1787. goto err_out;
  1788. #ifdef CONFIG_MTRR
  1789. if (mtrr) {
  1790. par->mtrr.vram = mtrr_add(info->fix.smem_start,
  1791. par->vram_size, MTRR_TYPE_WRCOMB, 1);
  1792. par->mtrr.vram_valid = 1;
  1793. /* let there be speed */
  1794. printk(KERN_INFO "aty128fb: Rage128 MTRR set to ON\n");
  1795. }
  1796. #endif /* CONFIG_MTRR */
  1797. return 0;
  1798. err_out:
  1799. iounmap(info->screen_base);
  1800. err_unmap_out:
  1801. iounmap(par->regbase);
  1802. err_free_info:
  1803. framebuffer_release(info);
  1804. err_free_mmio:
  1805. release_mem_region(pci_resource_start(pdev, 2),
  1806. pci_resource_len(pdev, 2));
  1807. err_free_fb:
  1808. release_mem_region(pci_resource_start(pdev, 0),
  1809. pci_resource_len(pdev, 0));
  1810. return -ENODEV;
  1811. }
  1812. static void __devexit aty128_remove(struct pci_dev *pdev)
  1813. {
  1814. struct fb_info *info = pci_get_drvdata(pdev);
  1815. struct aty128fb_par *par;
  1816. if (!info)
  1817. return;
  1818. par = info->par;
  1819. #ifdef CONFIG_FB_ATY128_BACKLIGHT
  1820. aty128_bl_exit(par);
  1821. #endif
  1822. unregister_framebuffer(info);
  1823. #ifdef CONFIG_MTRR
  1824. if (par->mtrr.vram_valid)
  1825. mtrr_del(par->mtrr.vram, info->fix.smem_start,
  1826. par->vram_size);
  1827. #endif /* CONFIG_MTRR */
  1828. iounmap(par->regbase);
  1829. iounmap(info->screen_base);
  1830. release_mem_region(pci_resource_start(pdev, 0),
  1831. pci_resource_len(pdev, 0));
  1832. release_mem_region(pci_resource_start(pdev, 2),
  1833. pci_resource_len(pdev, 2));
  1834. framebuffer_release(info);
  1835. }
  1836. #endif /* CONFIG_PCI */
  1837. /*
  1838. * Blank the display.
  1839. */
  1840. static int aty128fb_blank(int blank, struct fb_info *fb)
  1841. {
  1842. struct aty128fb_par *par = fb->par;
  1843. u8 state = 0;
  1844. if (par->lock_blank || par->asleep)
  1845. return 0;
  1846. #ifdef CONFIG_FB_ATY128_BACKLIGHT
  1847. if (machine_is(powermac) && blank)
  1848. aty128_bl_set_power(fb, FB_BLANK_POWERDOWN);
  1849. #endif
  1850. if (blank & FB_BLANK_VSYNC_SUSPEND)
  1851. state |= 2;
  1852. if (blank & FB_BLANK_HSYNC_SUSPEND)
  1853. state |= 1;
  1854. if (blank & FB_BLANK_POWERDOWN)
  1855. state |= 4;
  1856. aty_st_8(CRTC_EXT_CNTL+1, state);
  1857. if (par->chip_gen == rage_M3) {
  1858. aty128_set_crt_enable(par, par->crt_on && !blank);
  1859. aty128_set_lcd_enable(par, par->lcd_on && !blank);
  1860. }
  1861. #ifdef CONFIG_FB_ATY128_BACKLIGHT
  1862. if (machine_is(powermac) && !blank)
  1863. aty128_bl_set_power(fb, FB_BLANK_UNBLANK);
  1864. #endif
  1865. return 0;
  1866. }
  1867. /*
  1868. * Set a single color register. The values supplied are already
  1869. * rounded down to the hardware's capabilities (according to the
  1870. * entries in the var structure). Return != 0 for invalid regno.
  1871. */
  1872. static int aty128fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  1873. u_int transp, struct fb_info *info)
  1874. {
  1875. struct aty128fb_par *par = info->par;
  1876. if (regno > 255
  1877. || (par->crtc.depth == 16 && regno > 63)
  1878. || (par->crtc.depth == 15 && regno > 31))
  1879. return 1;
  1880. red >>= 8;
  1881. green >>= 8;
  1882. blue >>= 8;
  1883. if (regno < 16) {
  1884. int i;
  1885. u32 *pal = info->pseudo_palette;
  1886. switch (par->crtc.depth) {
  1887. case 15:
  1888. pal[regno] = (regno << 10) | (regno << 5) | regno;
  1889. break;
  1890. case 16:
  1891. pal[regno] = (regno << 11) | (regno << 6) | regno;
  1892. break;
  1893. case 24:
  1894. pal[regno] = (regno << 16) | (regno << 8) | regno;
  1895. break;
  1896. case 32:
  1897. i = (regno << 8) | regno;
  1898. pal[regno] = (i << 16) | i;
  1899. break;
  1900. }
  1901. }
  1902. if (par->crtc.depth == 16 && regno > 0) {
  1903. /*
  1904. * With the 5-6-5 split of bits for RGB at 16 bits/pixel, we
  1905. * have 32 slots for R and B values but 64 slots for G values.
  1906. * Thus the R and B values go in one slot but the G value
  1907. * goes in a different slot, and we have to avoid disturbing
  1908. * the other fields in the slots we touch.
  1909. */
  1910. par->green[regno] = green;
  1911. if (regno < 32) {
  1912. par->red[regno] = red;
  1913. par->blue[regno] = blue;
  1914. aty128_st_pal(regno * 8, red, par->green[regno*2],
  1915. blue, par);
  1916. }
  1917. red = par->red[regno/2];
  1918. blue = par->blue[regno/2];
  1919. regno <<= 2;
  1920. } else if (par->crtc.bpp == 16)
  1921. regno <<= 3;
  1922. aty128_st_pal(regno, red, green, blue, par);
  1923. return 0;
  1924. }
  1925. #define ATY_MIRROR_LCD_ON 0x00000001
  1926. #define ATY_MIRROR_CRT_ON 0x00000002
  1927. /* out param: u32* backlight value: 0 to 15 */
  1928. #define FBIO_ATY128_GET_MIRROR _IOR('@', 1, __u32)
  1929. /* in param: u32* backlight value: 0 to 15 */
  1930. #define FBIO_ATY128_SET_MIRROR _IOW('@', 2, __u32)
  1931. static int aty128fb_ioctl(struct fb_info *info, u_int cmd, u_long arg)
  1932. {
  1933. struct aty128fb_par *par = info->par;
  1934. u32 value;
  1935. int rc;
  1936. switch (cmd) {
  1937. case FBIO_ATY128_SET_MIRROR:
  1938. if (par->chip_gen != rage_M3)
  1939. return -EINVAL;
  1940. rc = get_user(value, (__u32 __user *)arg);
  1941. if (rc)
  1942. return rc;
  1943. par->lcd_on = (value & 0x01) != 0;
  1944. par->crt_on = (value & 0x02) != 0;
  1945. if (!par->crt_on && !par->lcd_on)
  1946. par->lcd_on = 1;
  1947. aty128_set_crt_enable(par, par->crt_on);
  1948. aty128_set_lcd_enable(par, par->lcd_on);
  1949. return 0;
  1950. case FBIO_ATY128_GET_MIRROR:
  1951. if (par->chip_gen != rage_M3)
  1952. return -EINVAL;
  1953. value = (par->crt_on << 1) | par->lcd_on;
  1954. return put_user(value, (__u32 __user *)arg);
  1955. }
  1956. return -EINVAL;
  1957. }
  1958. #if 0
  1959. /*
  1960. * Accelerated functions
  1961. */
  1962. static inline void aty128_rectcopy(int srcx, int srcy, int dstx, int dsty,
  1963. u_int width, u_int height,
  1964. struct fb_info_aty128 *par)
  1965. {
  1966. u32 save_dp_datatype, save_dp_cntl, dstval;
  1967. if (!width || !height)
  1968. return;
  1969. dstval = depth_to_dst(par->current_par.crtc.depth);
  1970. if (dstval == DST_24BPP) {
  1971. srcx *= 3;
  1972. dstx *= 3;
  1973. width *= 3;
  1974. } else if (dstval == -EINVAL) {
  1975. printk("aty128fb: invalid depth or RGBA\n");
  1976. return;
  1977. }
  1978. wait_for_fifo(2, par);
  1979. save_dp_datatype = aty_ld_le32(DP_DATATYPE);
  1980. save_dp_cntl = aty_ld_le32(DP_CNTL);
  1981. wait_for_fifo(6, par);
  1982. aty_st_le32(SRC_Y_X, (srcy << 16) | srcx);
  1983. aty_st_le32(DP_MIX, ROP3_SRCCOPY | DP_SRC_RECT);
  1984. aty_st_le32(DP_CNTL, DST_X_LEFT_TO_RIGHT | DST_Y_TOP_TO_BOTTOM);
  1985. aty_st_le32(DP_DATATYPE, save_dp_datatype | dstval | SRC_DSTCOLOR);
  1986. aty_st_le32(DST_Y_X, (dsty << 16) | dstx);
  1987. aty_st_le32(DST_HEIGHT_WIDTH, (height << 16) | width);
  1988. par->blitter_may_be_busy = 1;
  1989. wait_for_fifo(2, par);
  1990. aty_st_le32(DP_DATATYPE, save_dp_datatype);
  1991. aty_st_le32(DP_CNTL, save_dp_cntl);
  1992. }
  1993. /*
  1994. * Text mode accelerated functions
  1995. */
  1996. static void fbcon_aty128_bmove(struct display *p, int sy, int sx, int dy, int dx,
  1997. int height, int width)
  1998. {
  1999. sx *= fontwidth(p);
  2000. sy *= fontheight(p);
  2001. dx *= fontwidth(p);
  2002. dy *= fontheight(p);
  2003. width *= fontwidth(p);
  2004. height *= fontheight(p);
  2005. aty128_rectcopy(sx, sy, dx, dy, width, height,
  2006. (struct fb_info_aty128 *)p->fb_info);
  2007. }
  2008. #endif /* 0 */
  2009. static void aty128_set_suspend(struct aty128fb_par *par, int suspend)
  2010. {
  2011. u32 pmgt;
  2012. u16 pwr_command;
  2013. struct pci_dev *pdev = par->pdev;
  2014. if (!par->pm_reg)
  2015. return;
  2016. /* Set the chip into the appropriate suspend mode (we use D2,
  2017. * D3 would require a complete re-initialisation of the chip,
  2018. * including PCI config registers, clocks, AGP configuration, ...)
  2019. */
  2020. if (suspend) {
  2021. /* Make sure CRTC2 is reset. Remove that the day we decide to
  2022. * actually use CRTC2 and replace it with real code for disabling
  2023. * the CRTC2 output during sleep
  2024. */
  2025. aty_st_le32(CRTC2_GEN_CNTL, aty_ld_le32(CRTC2_GEN_CNTL) &
  2026. ~(CRTC2_EN));
  2027. /* Set the power management mode to be PCI based */
  2028. /* Use this magic value for now */
  2029. pmgt = 0x0c005407;
  2030. aty_st_pll(POWER_MANAGEMENT, pmgt);
  2031. (void)aty_ld_pll(POWER_MANAGEMENT);
  2032. aty_st_le32(BUS_CNTL1, 0x00000010);
  2033. aty_st_le32(MEM_POWER_MISC, 0x0c830000);
  2034. mdelay(100);
  2035. pci_read_config_word(pdev, par->pm_reg+PCI_PM_CTRL, &pwr_command);
  2036. /* Switch PCI power management to D2 */
  2037. pci_write_config_word(pdev, par->pm_reg+PCI_PM_CTRL,
  2038. (pwr_command & ~PCI_PM_CTRL_STATE_MASK) | 2);
  2039. pci_read_config_word(pdev, par->pm_reg+PCI_PM_CTRL, &pwr_command);
  2040. } else {
  2041. /* Switch back PCI power management to D0 */
  2042. mdelay(100);
  2043. pci_write_config_word(pdev, par->pm_reg+PCI_PM_CTRL, 0);
  2044. pci_read_config_word(pdev, par->pm_reg+PCI_PM_CTRL, &pwr_command);
  2045. mdelay(100);
  2046. }
  2047. }
  2048. static int aty128_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  2049. {
  2050. struct fb_info *info = pci_get_drvdata(pdev);
  2051. struct aty128fb_par *par = info->par;
  2052. /* We don't do anything but D2, for now we return 0, but
  2053. * we may want to change that. How do we know if the BIOS
  2054. * can properly take care of D3 ? Also, with swsusp, we
  2055. * know we'll be rebooted, ...
  2056. */
  2057. #ifndef CONFIG_PPC_PMAC
  2058. /* HACK ALERT ! Once I find a proper way to say to each driver
  2059. * individually what will happen with it's PCI slot, I'll change
  2060. * that. On laptops, the AGP slot is just unclocked, so D2 is
  2061. * expected, while on desktops, the card is powered off
  2062. */
  2063. return 0;
  2064. #endif /* CONFIG_PPC_PMAC */
  2065. if (state.event == pdev->dev.power.power_state.event)
  2066. return 0;
  2067. printk(KERN_DEBUG "aty128fb: suspending...\n");
  2068. acquire_console_sem();
  2069. fb_set_suspend(info, 1);
  2070. /* Make sure engine is reset */
  2071. wait_for_idle(par);
  2072. aty128_reset_engine(par);
  2073. wait_for_idle(par);
  2074. /* Blank display and LCD */
  2075. aty128fb_blank(VESA_POWERDOWN, info);
  2076. /* Sleep */
  2077. par->asleep = 1;
  2078. par->lock_blank = 1;
  2079. #ifdef CONFIG_PPC_PMAC
  2080. /* On powermac, we have hooks to properly suspend/resume AGP now,
  2081. * use them here. We'll ultimately need some generic support here,
  2082. * but the generic code isn't quite ready for that yet
  2083. */
  2084. pmac_suspend_agp_for_card(pdev);
  2085. #endif /* CONFIG_PPC_PMAC */
  2086. /* We need a way to make sure the fbdev layer will _not_ touch the
  2087. * framebuffer before we put the chip to suspend state. On 2.4, I
  2088. * used dummy fb ops, 2.5 need proper support for this at the
  2089. * fbdev level
  2090. */
  2091. if (state.event != PM_EVENT_ON)
  2092. aty128_set_suspend(par, 1);
  2093. release_console_sem();
  2094. pdev->dev.power.power_state = state;
  2095. return 0;
  2096. }
  2097. static int aty128_do_resume(struct pci_dev *pdev)
  2098. {
  2099. struct fb_info *info = pci_get_drvdata(pdev);
  2100. struct aty128fb_par *par = info->par;
  2101. if (pdev->dev.power.power_state.event == PM_EVENT_ON)
  2102. return 0;
  2103. /* Wakeup chip */
  2104. aty128_set_suspend(par, 0);
  2105. par->asleep = 0;
  2106. /* Restore display & engine */
  2107. aty128_reset_engine(par);
  2108. wait_for_idle(par);
  2109. aty128fb_set_par(info);
  2110. fb_pan_display(info, &info->var);
  2111. fb_set_cmap(&info->cmap, info);
  2112. /* Refresh */
  2113. fb_set_suspend(info, 0);
  2114. /* Unblank */
  2115. par->lock_blank = 0;
  2116. aty128fb_blank(0, info);
  2117. #ifdef CONFIG_PPC_PMAC
  2118. /* On powermac, we have hooks to properly suspend/resume AGP now,
  2119. * use them here. We'll ultimately need some generic support here,
  2120. * but the generic code isn't quite ready for that yet
  2121. */
  2122. pmac_resume_agp_for_card(pdev);
  2123. #endif /* CONFIG_PPC_PMAC */
  2124. pdev->dev.power.power_state = PMSG_ON;
  2125. printk(KERN_DEBUG "aty128fb: resumed !\n");
  2126. return 0;
  2127. }
  2128. static int aty128_pci_resume(struct pci_dev *pdev)
  2129. {
  2130. int rc;
  2131. acquire_console_sem();
  2132. rc = aty128_do_resume(pdev);
  2133. release_console_sem();
  2134. return rc;
  2135. }
  2136. static int __devinit aty128fb_init(void)
  2137. {
  2138. #ifndef MODULE
  2139. char *option = NULL;
  2140. if (fb_get_options("aty128fb", &option))
  2141. return -ENODEV;
  2142. aty128fb_setup(option);
  2143. #endif
  2144. return pci_register_driver(&aty128fb_driver);
  2145. }
  2146. static void __exit aty128fb_exit(void)
  2147. {
  2148. pci_unregister_driver(&aty128fb_driver);
  2149. }
  2150. module_init(aty128fb_init);
  2151. module_exit(aty128fb_exit);
  2152. MODULE_AUTHOR("(c)1999-2003 Brad Douglas <brad@neruo.com>");
  2153. MODULE_DESCRIPTION("FBDev driver for ATI Rage128 / Pro cards");
  2154. MODULE_LICENSE("GPL");
  2155. module_param(mode_option, charp, 0);
  2156. MODULE_PARM_DESC(mode_option, "Specify resolution as \"<xres>x<yres>[-<bpp>][@<refresh>]\" ");
  2157. #ifdef CONFIG_MTRR
  2158. module_param_named(nomtrr, mtrr, invbool, 0);
  2159. MODULE_PARM_DESC(nomtrr, "bool: Disable MTRR support (0 or 1=disabled) (default=0)");
  2160. #endif