aty128fb.c 65 KB

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