aty128fb.c 64 KB

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