aty128fb.c 66 KB

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