aty128fb.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555
  1. /* $Id: aty128fb.c,v 1.1.1.1.36.1 1999/12/11 09:03:05 Exp $
  2. * linux/drivers/video/aty128fb.c -- Frame buffer device for ATI Rage128
  3. *
  4. * Copyright (C) 1999-2003, Brad Douglas <brad@neruo.com>
  5. * Copyright (C) 1999, Anthony Tong <atong@uiuc.edu>
  6. *
  7. * Ani Joshi / Jeff Garzik
  8. * - Code cleanup
  9. *
  10. * Michel Danzer <michdaen@iiic.ethz.ch>
  11. * - 15/16 bit cleanup
  12. * - fix panning
  13. *
  14. * Benjamin Herrenschmidt
  15. * - pmac-specific PM stuff
  16. * - various fixes & cleanups
  17. *
  18. * Andreas Hundt <andi@convergence.de>
  19. * - FB_ACTIVATE fixes
  20. *
  21. * Paul Mackerras <paulus@samba.org>
  22. * - Convert to new framebuffer API,
  23. * fix colormap setting at 16 bits/pixel (565)
  24. *
  25. * Paul Mundt
  26. * - PCI hotplug
  27. *
  28. * Jon Smirl <jonsmirl@yahoo.com>
  29. * - PCI ID update
  30. * - replace ROM BIOS search
  31. *
  32. * Based off of Geert's atyfb.c and vfb.c.
  33. *
  34. * TODO:
  35. * - monitor sensing (DDC)
  36. * - virtual display
  37. * - other platform support (only ppc/x86 supported)
  38. * - hardware cursor support
  39. *
  40. * Please cc: your patches to brad@neruo.com.
  41. */
  42. /*
  43. * A special note of gratitude to ATI's devrel for providing documentation,
  44. * example code and hardware. Thanks Nitya. -atong and brad
  45. */
  46. #include <linux/module.h>
  47. #include <linux/moduleparam.h>
  48. #include <linux/kernel.h>
  49. #include <linux/errno.h>
  50. #include <linux/string.h>
  51. #include <linux/mm.h>
  52. #include <linux/slab.h>
  53. #include <linux/vmalloc.h>
  54. #include <linux/delay.h>
  55. #include <linux/interrupt.h>
  56. #include <linux/uaccess.h>
  57. #include <linux/fb.h>
  58. #include <linux/init.h>
  59. #include <linux/pci.h>
  60. #include <linux/ioport.h>
  61. #include <linux/console.h>
  62. #include <linux/backlight.h>
  63. #include <asm/io.h>
  64. #ifdef CONFIG_PPC_PMAC
  65. #include <asm/machdep.h>
  66. #include <asm/pmac_feature.h>
  67. #include <asm/prom.h>
  68. #include <asm/pci-bridge.h>
  69. #include "../macmodes.h"
  70. #endif
  71. #ifdef CONFIG_PMAC_BACKLIGHT
  72. #include <asm/backlight.h>
  73. #endif
  74. #ifdef CONFIG_BOOTX_TEXT
  75. #include <asm/btext.h>
  76. #endif /* CONFIG_BOOTX_TEXT */
  77. #ifdef CONFIG_MTRR
  78. #include <asm/mtrr.h>
  79. #endif
  80. #include <video/aty128.h>
  81. /* Debug flag */
  82. #undef DEBUG
  83. #ifdef DEBUG
  84. #define DBG(fmt, args...) printk(KERN_DEBUG "aty128fb: %s " fmt, __func__, ##args);
  85. #else
  86. #define DBG(fmt, args...)
  87. #endif
  88. #ifndef CONFIG_PPC_PMAC
  89. /* default mode */
  90. static struct fb_var_screeninfo default_var __devinitdata = {
  91. /* 640x480, 60 Hz, Non-Interlaced (25.175 MHz dotclock) */
  92. 640, 480, 640, 480, 0, 0, 8, 0,
  93. {0, 8, 0}, {0, 8, 0}, {0, 8, 0}, {0, 0, 0},
  94. 0, 0, -1, -1, 0, 39722, 48, 16, 33, 10, 96, 2,
  95. 0, FB_VMODE_NONINTERLACED
  96. };
  97. #else /* CONFIG_PPC_PMAC */
  98. /* default to 1024x768 at 75Hz on PPC - this will work
  99. * on the iMac, the usual 640x480 @ 60Hz doesn't. */
  100. static struct fb_var_screeninfo default_var = {
  101. /* 1024x768, 75 Hz, Non-Interlaced (78.75 MHz dotclock) */
  102. 1024, 768, 1024, 768, 0, 0, 8, 0,
  103. {0, 8, 0}, {0, 8, 0}, {0, 8, 0}, {0, 0, 0},
  104. 0, 0, -1, -1, 0, 12699, 160, 32, 28, 1, 96, 3,
  105. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  106. FB_VMODE_NONINTERLACED
  107. };
  108. #endif /* CONFIG_PPC_PMAC */
  109. /* default modedb mode */
  110. /* 640x480, 60 Hz, Non-Interlaced (25.172 MHz dotclock) */
  111. static struct fb_videomode defaultmode __devinitdata = {
  112. .refresh = 60,
  113. .xres = 640,
  114. .yres = 480,
  115. .pixclock = 39722,
  116. .left_margin = 48,
  117. .right_margin = 16,
  118. .upper_margin = 33,
  119. .lower_margin = 10,
  120. .hsync_len = 96,
  121. .vsync_len = 2,
  122. .sync = 0,
  123. .vmode = FB_VMODE_NONINTERLACED
  124. };
  125. /* Chip generations */
  126. enum {
  127. rage_128,
  128. rage_128_pci,
  129. rage_128_pro,
  130. rage_128_pro_pci,
  131. rage_M3,
  132. rage_M3_pci,
  133. rage_M4,
  134. rage_128_ultra,
  135. };
  136. /* Must match above enum */
  137. static const char *r128_family[] __devinitdata = {
  138. "AGP",
  139. "PCI",
  140. "PRO AGP",
  141. "PRO PCI",
  142. "M3 AGP",
  143. "M3 PCI",
  144. "M4 AGP",
  145. "Ultra AGP",
  146. };
  147. /*
  148. * PCI driver prototypes
  149. */
  150. static int aty128_probe(struct pci_dev *pdev,
  151. const struct pci_device_id *ent);
  152. static void aty128_remove(struct pci_dev *pdev);
  153. static int aty128_pci_suspend(struct pci_dev *pdev, pm_message_t state);
  154. static int aty128_pci_resume(struct pci_dev *pdev);
  155. static int aty128_do_resume(struct pci_dev *pdev);
  156. /* supported Rage128 chipsets */
  157. static struct pci_device_id aty128_pci_tbl[] = {
  158. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_LE,
  159. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_M3_pci },
  160. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_LF,
  161. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_M3 },
  162. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_MF,
  163. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_M4 },
  164. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_ML,
  165. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_M4 },
  166. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PA,
  167. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  168. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PB,
  169. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  170. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PC,
  171. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  172. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PD,
  173. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro_pci },
  174. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PE,
  175. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  176. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PF,
  177. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  178. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PG,
  179. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  180. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PH,
  181. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  182. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PI,
  183. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  184. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PJ,
  185. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  186. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PK,
  187. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  188. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PL,
  189. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  190. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PM,
  191. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  192. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PN,
  193. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  194. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PO,
  195. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  196. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PP,
  197. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro_pci },
  198. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PQ,
  199. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  200. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PR,
  201. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro_pci },
  202. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PS,
  203. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  204. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PT,
  205. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  206. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PU,
  207. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  208. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PV,
  209. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  210. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PW,
  211. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  212. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_PX,
  213. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pro },
  214. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_RE,
  215. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pci },
  216. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_RF,
  217. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  218. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_RG,
  219. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  220. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_RK,
  221. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pci },
  222. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_RL,
  223. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  224. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SE,
  225. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  226. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SF,
  227. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_pci },
  228. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SG,
  229. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  230. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SH,
  231. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  232. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SK,
  233. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  234. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SL,
  235. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  236. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SM,
  237. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  238. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_SN,
  239. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128 },
  240. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_TF,
  241. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_ultra },
  242. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_TL,
  243. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_ultra },
  244. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_TR,
  245. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_ultra },
  246. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_TS,
  247. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_ultra },
  248. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_TT,
  249. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_ultra },
  250. { PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RAGE128_TU,
  251. PCI_ANY_ID, PCI_ANY_ID, 0, 0, rage_128_ultra },
  252. { 0, }
  253. };
  254. MODULE_DEVICE_TABLE(pci, aty128_pci_tbl);
  255. static struct pci_driver aty128fb_driver = {
  256. .name = "aty128fb",
  257. .id_table = aty128_pci_tbl,
  258. .probe = aty128_probe,
  259. .remove = __devexit_p(aty128_remove),
  260. .suspend = aty128_pci_suspend,
  261. .resume = aty128_pci_resume,
  262. };
  263. /* packed BIOS settings */
  264. #ifndef CONFIG_PPC
  265. typedef struct {
  266. u8 clock_chip_type;
  267. u8 struct_size;
  268. u8 accelerator_entry;
  269. u8 VGA_entry;
  270. u16 VGA_table_offset;
  271. u16 POST_table_offset;
  272. u16 XCLK;
  273. u16 MCLK;
  274. u8 num_PLL_blocks;
  275. u8 size_PLL_blocks;
  276. u16 PCLK_ref_freq;
  277. u16 PCLK_ref_divider;
  278. u32 PCLK_min_freq;
  279. u32 PCLK_max_freq;
  280. u16 MCLK_ref_freq;
  281. u16 MCLK_ref_divider;
  282. u32 MCLK_min_freq;
  283. u32 MCLK_max_freq;
  284. u16 XCLK_ref_freq;
  285. u16 XCLK_ref_divider;
  286. u32 XCLK_min_freq;
  287. u32 XCLK_max_freq;
  288. } __attribute__ ((packed)) PLL_BLOCK;
  289. #endif /* !CONFIG_PPC */
  290. /* onboard memory information */
  291. struct aty128_meminfo {
  292. u8 ML;
  293. u8 MB;
  294. u8 Trcd;
  295. u8 Trp;
  296. u8 Twr;
  297. u8 CL;
  298. u8 Tr2w;
  299. u8 LoopLatency;
  300. u8 DspOn;
  301. u8 Rloop;
  302. const char *name;
  303. };
  304. /* various memory configurations */
  305. static const struct aty128_meminfo sdr_128 =
  306. { 4, 4, 3, 3, 1, 3, 1, 16, 30, 16, "128-bit SDR SGRAM (1:1)" };
  307. static const struct aty128_meminfo sdr_64 =
  308. { 4, 8, 3, 3, 1, 3, 1, 17, 46, 17, "64-bit SDR SGRAM (1:1)" };
  309. static const struct aty128_meminfo sdr_sgram =
  310. { 4, 4, 1, 2, 1, 2, 1, 16, 24, 16, "64-bit SDR SGRAM (2:1)" };
  311. static const struct aty128_meminfo ddr_sgram =
  312. { 4, 4, 3, 3, 2, 3, 1, 16, 31, 16, "64-bit DDR SGRAM" };
  313. static struct fb_fix_screeninfo aty128fb_fix __devinitdata = {
  314. .id = "ATY Rage128",
  315. .type = FB_TYPE_PACKED_PIXELS,
  316. .visual = FB_VISUAL_PSEUDOCOLOR,
  317. .xpanstep = 8,
  318. .ypanstep = 1,
  319. .mmio_len = 0x2000,
  320. .accel = FB_ACCEL_ATI_RAGE128,
  321. };
  322. static char *mode_option __devinitdata = NULL;
  323. #ifdef CONFIG_PPC_PMAC
  324. static int default_vmode __devinitdata = VMODE_1024_768_60;
  325. static int default_cmode __devinitdata = CMODE_8;
  326. #endif
  327. static int default_crt_on __devinitdata = 0;
  328. static int default_lcd_on __devinitdata = 1;
  329. #ifdef CONFIG_MTRR
  330. static int mtrr = 1;
  331. #endif
  332. #ifdef CONFIG_PMAC_BACKLIGHT
  333. static int backlight __devinitdata = 1;
  334. #else
  335. static int backlight __devinitdata = 0;
  336. #endif
  337. /* PLL constants */
  338. struct aty128_constants {
  339. u32 ref_clk;
  340. u32 ppll_min;
  341. u32 ppll_max;
  342. u32 ref_divider;
  343. u32 xclk;
  344. u32 fifo_width;
  345. u32 fifo_depth;
  346. };
  347. struct aty128_crtc {
  348. u32 gen_cntl;
  349. u32 h_total, h_sync_strt_wid;
  350. u32 v_total, v_sync_strt_wid;
  351. u32 pitch;
  352. u32 offset, offset_cntl;
  353. u32 xoffset, yoffset;
  354. u32 vxres, vyres;
  355. u32 depth, bpp;
  356. };
  357. struct aty128_pll {
  358. u32 post_divider;
  359. u32 feedback_divider;
  360. u32 vclk;
  361. };
  362. struct aty128_ddafifo {
  363. u32 dda_config;
  364. u32 dda_on_off;
  365. };
  366. /* register values for a specific mode */
  367. struct aty128fb_par {
  368. struct aty128_crtc crtc;
  369. struct aty128_pll pll;
  370. struct aty128_ddafifo fifo_reg;
  371. u32 accel_flags;
  372. struct aty128_constants constants; /* PLL and others */
  373. void __iomem *regbase; /* remapped mmio */
  374. u32 vram_size; /* onboard video ram */
  375. int chip_gen;
  376. const struct aty128_meminfo *mem; /* onboard mem info */
  377. #ifdef CONFIG_MTRR
  378. struct { int vram; int vram_valid; } mtrr;
  379. #endif
  380. int blitter_may_be_busy;
  381. int fifo_slots; /* free slots in FIFO (64 max) */
  382. int pm_reg;
  383. int crt_on, lcd_on;
  384. struct pci_dev *pdev;
  385. struct fb_info *next;
  386. int asleep;
  387. int lock_blank;
  388. u8 red[32]; /* see aty128fb_setcolreg */
  389. u8 green[64];
  390. u8 blue[32];
  391. u32 pseudo_palette[16]; /* used for TRUECOLOR */
  392. };
  393. #define round_div(n, d) ((n+(d/2))/d)
  394. static int aty128fb_check_var(struct fb_var_screeninfo *var,
  395. struct fb_info *info);
  396. static int aty128fb_set_par(struct fb_info *info);
  397. static int aty128fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  398. u_int transp, struct fb_info *info);
  399. static int aty128fb_pan_display(struct fb_var_screeninfo *var,
  400. struct fb_info *fb);
  401. static int aty128fb_blank(int blank, struct fb_info *fb);
  402. static int aty128fb_ioctl(struct fb_info *info, u_int cmd, unsigned long arg);
  403. static int aty128fb_sync(struct fb_info *info);
  404. /*
  405. * Internal routines
  406. */
  407. static int aty128_encode_var(struct fb_var_screeninfo *var,
  408. const struct aty128fb_par *par);
  409. static int aty128_decode_var(struct fb_var_screeninfo *var,
  410. struct aty128fb_par *par);
  411. #if 0
  412. static void __devinit aty128_get_pllinfo(struct aty128fb_par *par,
  413. void __iomem *bios);
  414. static void __devinit __iomem *aty128_map_ROM(struct pci_dev *pdev, const struct aty128fb_par *par);
  415. #endif
  416. static void aty128_timings(struct aty128fb_par *par);
  417. static void aty128_init_engine(struct aty128fb_par *par);
  418. static void aty128_reset_engine(const struct aty128fb_par *par);
  419. static void aty128_flush_pixel_cache(const struct aty128fb_par *par);
  420. static void do_wait_for_fifo(u16 entries, struct aty128fb_par *par);
  421. static void wait_for_fifo(u16 entries, struct aty128fb_par *par);
  422. static void wait_for_idle(struct aty128fb_par *par);
  423. static u32 depth_to_dst(u32 depth);
  424. #ifdef CONFIG_FB_ATY128_BACKLIGHT
  425. static void aty128_bl_set_power(struct fb_info *info, int power);
  426. #endif
  427. #define BIOS_IN8(v) (readb(bios + (v)))
  428. #define BIOS_IN16(v) (readb(bios + (v)) | \
  429. (readb(bios + (v) + 1) << 8))
  430. #define BIOS_IN32(v) (readb(bios + (v)) | \
  431. (readb(bios + (v) + 1) << 8) | \
  432. (readb(bios + (v) + 2) << 16) | \
  433. (readb(bios + (v) + 3) << 24))
  434. static struct fb_ops aty128fb_ops = {
  435. .owner = THIS_MODULE,
  436. .fb_check_var = aty128fb_check_var,
  437. .fb_set_par = aty128fb_set_par,
  438. .fb_setcolreg = aty128fb_setcolreg,
  439. .fb_pan_display = aty128fb_pan_display,
  440. .fb_blank = aty128fb_blank,
  441. .fb_ioctl = aty128fb_ioctl,
  442. .fb_sync = aty128fb_sync,
  443. .fb_fillrect = cfb_fillrect,
  444. .fb_copyarea = cfb_copyarea,
  445. .fb_imageblit = cfb_imageblit,
  446. };
  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 __devinit 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 * __devinit 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 __devinit 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 __devinit 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. #ifdef CONFIG_FB_ATY128_BACKLIGHT
  1077. struct fb_info *info = pci_get_drvdata(par->pdev);
  1078. #endif
  1079. if (on) {
  1080. reg = aty_ld_le32(LVDS_GEN_CNTL);
  1081. reg |= LVDS_ON | LVDS_EN | LVDS_BLON | LVDS_DIGION;
  1082. reg &= ~LVDS_DISPLAY_DIS;
  1083. aty_st_le32(LVDS_GEN_CNTL, reg);
  1084. #ifdef CONFIG_FB_ATY128_BACKLIGHT
  1085. aty128_bl_set_power(info, FB_BLANK_UNBLANK);
  1086. #endif
  1087. } else {
  1088. #ifdef CONFIG_FB_ATY128_BACKLIGHT
  1089. aty128_bl_set_power(info, FB_BLANK_POWERDOWN);
  1090. #endif
  1091. reg = aty_ld_le32(LVDS_GEN_CNTL);
  1092. reg |= LVDS_DISPLAY_DIS;
  1093. aty_st_le32(LVDS_GEN_CNTL, reg);
  1094. mdelay(100);
  1095. reg &= ~(LVDS_ON /*| LVDS_EN*/);
  1096. aty_st_le32(LVDS_GEN_CNTL, reg);
  1097. }
  1098. }
  1099. static void aty128_set_pll(struct aty128_pll *pll, const struct aty128fb_par *par)
  1100. {
  1101. u32 div3;
  1102. unsigned char post_conv[] = /* register values for post dividers */
  1103. { 2, 0, 1, 4, 2, 2, 6, 2, 3, 2, 2, 2, 7 };
  1104. /* select PPLL_DIV_3 */
  1105. aty_st_le32(CLOCK_CNTL_INDEX, aty_ld_le32(CLOCK_CNTL_INDEX) | (3 << 8));
  1106. /* reset PLL */
  1107. aty_st_pll(PPLL_CNTL,
  1108. aty_ld_pll(PPLL_CNTL) | PPLL_RESET | PPLL_ATOMIC_UPDATE_EN);
  1109. /* write the reference divider */
  1110. aty_pll_wait_readupdate(par);
  1111. aty_st_pll(PPLL_REF_DIV, par->constants.ref_divider & 0x3ff);
  1112. aty_pll_writeupdate(par);
  1113. div3 = aty_ld_pll(PPLL_DIV_3);
  1114. div3 &= ~PPLL_FB3_DIV_MASK;
  1115. div3 |= pll->feedback_divider;
  1116. div3 &= ~PPLL_POST3_DIV_MASK;
  1117. div3 |= post_conv[pll->post_divider] << 16;
  1118. /* write feedback and post dividers */
  1119. aty_pll_wait_readupdate(par);
  1120. aty_st_pll(PPLL_DIV_3, div3);
  1121. aty_pll_writeupdate(par);
  1122. aty_pll_wait_readupdate(par);
  1123. aty_st_pll(HTOTAL_CNTL, 0); /* no horiz crtc adjustment */
  1124. aty_pll_writeupdate(par);
  1125. /* clear the reset, just in case */
  1126. aty_st_pll(PPLL_CNTL, aty_ld_pll(PPLL_CNTL) & ~PPLL_RESET);
  1127. }
  1128. static int aty128_var_to_pll(u32 period_in_ps, struct aty128_pll *pll,
  1129. const struct aty128fb_par *par)
  1130. {
  1131. const struct aty128_constants c = par->constants;
  1132. unsigned char post_dividers[] = {1,2,4,8,3,6,12};
  1133. u32 output_freq;
  1134. u32 vclk; /* in .01 MHz */
  1135. int i = 0;
  1136. u32 n, d;
  1137. vclk = 100000000 / period_in_ps; /* convert units to 10 kHz */
  1138. /* adjust pixel clock if necessary */
  1139. if (vclk > c.ppll_max)
  1140. vclk = c.ppll_max;
  1141. if (vclk * 12 < c.ppll_min)
  1142. vclk = c.ppll_min/12;
  1143. /* now, find an acceptable divider */
  1144. for (i = 0; i < ARRAY_SIZE(post_dividers); i++) {
  1145. output_freq = post_dividers[i] * vclk;
  1146. if (output_freq >= c.ppll_min && output_freq <= c.ppll_max) {
  1147. pll->post_divider = post_dividers[i];
  1148. break;
  1149. }
  1150. }
  1151. if (i == ARRAY_SIZE(post_dividers))
  1152. return -EINVAL;
  1153. /* calculate feedback divider */
  1154. n = c.ref_divider * output_freq;
  1155. d = c.ref_clk;
  1156. pll->feedback_divider = round_div(n, d);
  1157. pll->vclk = vclk;
  1158. DBG("post %d feedback %d vlck %d output %d ref_divider %d "
  1159. "vclk_per: %d\n", pll->post_divider,
  1160. pll->feedback_divider, vclk, output_freq,
  1161. c.ref_divider, period_in_ps);
  1162. return 0;
  1163. }
  1164. static int aty128_pll_to_var(const struct aty128_pll *pll, struct fb_var_screeninfo *var)
  1165. {
  1166. var->pixclock = 100000000 / pll->vclk;
  1167. return 0;
  1168. }
  1169. static void aty128_set_fifo(const struct aty128_ddafifo *dsp,
  1170. const struct aty128fb_par *par)
  1171. {
  1172. aty_st_le32(DDA_CONFIG, dsp->dda_config);
  1173. aty_st_le32(DDA_ON_OFF, dsp->dda_on_off);
  1174. }
  1175. static int aty128_ddafifo(struct aty128_ddafifo *dsp,
  1176. const struct aty128_pll *pll,
  1177. u32 depth,
  1178. const struct aty128fb_par *par)
  1179. {
  1180. const struct aty128_meminfo *m = par->mem;
  1181. u32 xclk = par->constants.xclk;
  1182. u32 fifo_width = par->constants.fifo_width;
  1183. u32 fifo_depth = par->constants.fifo_depth;
  1184. s32 x, b, p, ron, roff;
  1185. u32 n, d, bpp;
  1186. /* round up to multiple of 8 */
  1187. bpp = (depth+7) & ~7;
  1188. n = xclk * fifo_width;
  1189. d = pll->vclk * bpp;
  1190. x = round_div(n, d);
  1191. ron = 4 * m->MB +
  1192. 3 * ((m->Trcd - 2 > 0) ? m->Trcd - 2 : 0) +
  1193. 2 * m->Trp +
  1194. m->Twr +
  1195. m->CL +
  1196. m->Tr2w +
  1197. x;
  1198. DBG("x %x\n", x);
  1199. b = 0;
  1200. while (x) {
  1201. x >>= 1;
  1202. b++;
  1203. }
  1204. p = b + 1;
  1205. ron <<= (11 - p);
  1206. n <<= (11 - p);
  1207. x = round_div(n, d);
  1208. roff = x * (fifo_depth - 4);
  1209. if ((ron + m->Rloop) >= roff) {
  1210. printk(KERN_ERR "aty128fb: Mode out of range!\n");
  1211. return -EINVAL;
  1212. }
  1213. DBG("p: %x rloop: %x x: %x ron: %x roff: %x\n",
  1214. p, m->Rloop, x, ron, roff);
  1215. dsp->dda_config = p << 16 | m->Rloop << 20 | x;
  1216. dsp->dda_on_off = ron << 16 | roff;
  1217. return 0;
  1218. }
  1219. /*
  1220. * This actually sets the video mode.
  1221. */
  1222. static int aty128fb_set_par(struct fb_info *info)
  1223. {
  1224. struct aty128fb_par *par = info->par;
  1225. u32 config;
  1226. int err;
  1227. if ((err = aty128_decode_var(&info->var, par)) != 0)
  1228. return err;
  1229. if (par->blitter_may_be_busy)
  1230. wait_for_idle(par);
  1231. /* clear all registers that may interfere with mode setting */
  1232. aty_st_le32(OVR_CLR, 0);
  1233. aty_st_le32(OVR_WID_LEFT_RIGHT, 0);
  1234. aty_st_le32(OVR_WID_TOP_BOTTOM, 0);
  1235. aty_st_le32(OV0_SCALE_CNTL, 0);
  1236. aty_st_le32(MPP_TB_CONFIG, 0);
  1237. aty_st_le32(MPP_GP_CONFIG, 0);
  1238. aty_st_le32(SUBPIC_CNTL, 0);
  1239. aty_st_le32(VIPH_CONTROL, 0);
  1240. aty_st_le32(I2C_CNTL_1, 0); /* turn off i2c */
  1241. aty_st_le32(GEN_INT_CNTL, 0); /* turn off interrupts */
  1242. aty_st_le32(CAP0_TRIG_CNTL, 0);
  1243. aty_st_le32(CAP1_TRIG_CNTL, 0);
  1244. aty_st_8(CRTC_EXT_CNTL + 1, 4); /* turn video off */
  1245. aty128_set_crtc(&par->crtc, par);
  1246. aty128_set_pll(&par->pll, par);
  1247. aty128_set_fifo(&par->fifo_reg, par);
  1248. config = aty_ld_le32(CONFIG_CNTL) & ~3;
  1249. #if defined(__BIG_ENDIAN)
  1250. if (par->crtc.bpp == 32)
  1251. config |= 2; /* make aperture do 32 bit swapping */
  1252. else if (par->crtc.bpp == 16)
  1253. config |= 1; /* make aperture do 16 bit swapping */
  1254. #endif
  1255. aty_st_le32(CONFIG_CNTL, config);
  1256. aty_st_8(CRTC_EXT_CNTL + 1, 0); /* turn the video back on */
  1257. info->fix.line_length = (par->crtc.vxres * par->crtc.bpp) >> 3;
  1258. info->fix.visual = par->crtc.bpp == 8 ? FB_VISUAL_PSEUDOCOLOR
  1259. : FB_VISUAL_DIRECTCOLOR;
  1260. if (par->chip_gen == rage_M3) {
  1261. aty128_set_crt_enable(par, par->crt_on);
  1262. aty128_set_lcd_enable(par, par->lcd_on);
  1263. }
  1264. if (par->accel_flags & FB_ACCELF_TEXT)
  1265. aty128_init_engine(par);
  1266. #ifdef CONFIG_BOOTX_TEXT
  1267. btext_update_display(info->fix.smem_start,
  1268. (((par->crtc.h_total>>16) & 0xff)+1)*8,
  1269. ((par->crtc.v_total>>16) & 0x7ff)+1,
  1270. par->crtc.bpp,
  1271. par->crtc.vxres*par->crtc.bpp/8);
  1272. #endif /* CONFIG_BOOTX_TEXT */
  1273. return 0;
  1274. }
  1275. /*
  1276. * encode/decode the User Defined Part of the Display
  1277. */
  1278. static int aty128_decode_var(struct fb_var_screeninfo *var, struct aty128fb_par *par)
  1279. {
  1280. int err;
  1281. struct aty128_crtc crtc;
  1282. struct aty128_pll pll;
  1283. struct aty128_ddafifo fifo_reg;
  1284. if ((err = aty128_var_to_crtc(var, &crtc, par)))
  1285. return err;
  1286. if ((err = aty128_var_to_pll(var->pixclock, &pll, par)))
  1287. return err;
  1288. if ((err = aty128_ddafifo(&fifo_reg, &pll, crtc.depth, par)))
  1289. return err;
  1290. par->crtc = crtc;
  1291. par->pll = pll;
  1292. par->fifo_reg = fifo_reg;
  1293. par->accel_flags = var->accel_flags;
  1294. return 0;
  1295. }
  1296. static int aty128_encode_var(struct fb_var_screeninfo *var,
  1297. const struct aty128fb_par *par)
  1298. {
  1299. int err;
  1300. if ((err = aty128_crtc_to_var(&par->crtc, var)))
  1301. return err;
  1302. if ((err = aty128_pll_to_var(&par->pll, var)))
  1303. return err;
  1304. var->nonstd = 0;
  1305. var->activate = 0;
  1306. var->height = -1;
  1307. var->width = -1;
  1308. var->accel_flags = par->accel_flags;
  1309. return 0;
  1310. }
  1311. static int aty128fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  1312. {
  1313. struct aty128fb_par par;
  1314. int err;
  1315. par = *(struct aty128fb_par *)info->par;
  1316. if ((err = aty128_decode_var(var, &par)) != 0)
  1317. return err;
  1318. aty128_encode_var(var, &par);
  1319. return 0;
  1320. }
  1321. /*
  1322. * Pan or Wrap the Display
  1323. */
  1324. static int aty128fb_pan_display(struct fb_var_screeninfo *var, struct fb_info *fb)
  1325. {
  1326. struct aty128fb_par *par = fb->par;
  1327. u32 xoffset, yoffset;
  1328. u32 offset;
  1329. u32 xres, yres;
  1330. xres = (((par->crtc.h_total >> 16) & 0xff) + 1) << 3;
  1331. yres = ((par->crtc.v_total >> 16) & 0x7ff) + 1;
  1332. xoffset = (var->xoffset +7) & ~7;
  1333. yoffset = var->yoffset;
  1334. if (xoffset+xres > par->crtc.vxres || yoffset+yres > par->crtc.vyres)
  1335. return -EINVAL;
  1336. par->crtc.xoffset = xoffset;
  1337. par->crtc.yoffset = yoffset;
  1338. offset = ((yoffset * par->crtc.vxres + xoffset)*(par->crtc.bpp >> 3)) & ~7;
  1339. if (par->crtc.bpp == 24)
  1340. offset += 8 * (offset % 3); /* Must be multiple of 8 and 3 */
  1341. aty_st_le32(CRTC_OFFSET, offset);
  1342. return 0;
  1343. }
  1344. /*
  1345. * Helper function to store a single palette register
  1346. */
  1347. static void aty128_st_pal(u_int regno, u_int red, u_int green, u_int blue,
  1348. struct aty128fb_par *par)
  1349. {
  1350. if (par->chip_gen == rage_M3) {
  1351. #if 0
  1352. /* Note: For now, on M3, we set palette on both heads, which may
  1353. * be useless. Can someone with a M3 check this ?
  1354. *
  1355. * This code would still be useful if using the second CRTC to
  1356. * do mirroring
  1357. */
  1358. aty_st_le32(DAC_CNTL, aty_ld_le32(DAC_CNTL) | DAC_PALETTE_ACCESS_CNTL);
  1359. aty_st_8(PALETTE_INDEX, regno);
  1360. aty_st_le32(PALETTE_DATA, (red<<16)|(green<<8)|blue);
  1361. #endif
  1362. aty_st_le32(DAC_CNTL, aty_ld_le32(DAC_CNTL) & ~DAC_PALETTE_ACCESS_CNTL);
  1363. }
  1364. aty_st_8(PALETTE_INDEX, regno);
  1365. aty_st_le32(PALETTE_DATA, (red<<16)|(green<<8)|blue);
  1366. }
  1367. static int aty128fb_sync(struct fb_info *info)
  1368. {
  1369. struct aty128fb_par *par = info->par;
  1370. if (par->blitter_may_be_busy)
  1371. wait_for_idle(par);
  1372. return 0;
  1373. }
  1374. #ifndef MODULE
  1375. static int __devinit aty128fb_setup(char *options)
  1376. {
  1377. char *this_opt;
  1378. if (!options || !*options)
  1379. return 0;
  1380. while ((this_opt = strsep(&options, ",")) != NULL) {
  1381. if (!strncmp(this_opt, "lcd:", 4)) {
  1382. default_lcd_on = simple_strtoul(this_opt+4, NULL, 0);
  1383. continue;
  1384. } else if (!strncmp(this_opt, "crt:", 4)) {
  1385. default_crt_on = simple_strtoul(this_opt+4, NULL, 0);
  1386. continue;
  1387. } else if (!strncmp(this_opt, "backlight:", 10)) {
  1388. backlight = simple_strtoul(this_opt+10, NULL, 0);
  1389. continue;
  1390. }
  1391. #ifdef CONFIG_MTRR
  1392. if(!strncmp(this_opt, "nomtrr", 6)) {
  1393. mtrr = 0;
  1394. continue;
  1395. }
  1396. #endif
  1397. #ifdef CONFIG_PPC_PMAC
  1398. /* vmode and cmode deprecated */
  1399. if (!strncmp(this_opt, "vmode:", 6)) {
  1400. unsigned int vmode = simple_strtoul(this_opt+6, NULL, 0);
  1401. if (vmode > 0 && vmode <= VMODE_MAX)
  1402. default_vmode = vmode;
  1403. continue;
  1404. } else if (!strncmp(this_opt, "cmode:", 6)) {
  1405. unsigned int cmode = simple_strtoul(this_opt+6, NULL, 0);
  1406. switch (cmode) {
  1407. case 0:
  1408. case 8:
  1409. default_cmode = CMODE_8;
  1410. break;
  1411. case 15:
  1412. case 16:
  1413. default_cmode = CMODE_16;
  1414. break;
  1415. case 24:
  1416. case 32:
  1417. default_cmode = CMODE_32;
  1418. break;
  1419. }
  1420. continue;
  1421. }
  1422. #endif /* CONFIG_PPC_PMAC */
  1423. mode_option = this_opt;
  1424. }
  1425. return 0;
  1426. }
  1427. #endif /* MODULE */
  1428. /* Backlight */
  1429. #ifdef CONFIG_FB_ATY128_BACKLIGHT
  1430. #define MAX_LEVEL 0xFF
  1431. static int aty128_bl_get_level_brightness(struct aty128fb_par *par,
  1432. int level)
  1433. {
  1434. struct fb_info *info = pci_get_drvdata(par->pdev);
  1435. int atylevel;
  1436. /* Get and convert the value */
  1437. /* No locking of bl_curve since we read a single value */
  1438. atylevel = MAX_LEVEL -
  1439. (info->bl_curve[level] * FB_BACKLIGHT_MAX / MAX_LEVEL);
  1440. if (atylevel < 0)
  1441. atylevel = 0;
  1442. else if (atylevel > MAX_LEVEL)
  1443. atylevel = MAX_LEVEL;
  1444. return atylevel;
  1445. }
  1446. /* We turn off the LCD completely instead of just dimming the backlight.
  1447. * This provides greater power saving and the display is useless without
  1448. * backlight anyway
  1449. */
  1450. #define BACKLIGHT_LVDS_OFF
  1451. /* That one prevents proper CRT output with LCD off */
  1452. #undef BACKLIGHT_DAC_OFF
  1453. static int aty128_bl_update_status(struct backlight_device *bd)
  1454. {
  1455. struct aty128fb_par *par = bl_get_data(bd);
  1456. unsigned int reg = aty_ld_le32(LVDS_GEN_CNTL);
  1457. int level;
  1458. if (bd->props.power != FB_BLANK_UNBLANK ||
  1459. bd->props.fb_blank != FB_BLANK_UNBLANK ||
  1460. !par->lcd_on)
  1461. level = 0;
  1462. else
  1463. level = bd->props.brightness;
  1464. reg |= LVDS_BL_MOD_EN | LVDS_BLON;
  1465. if (level > 0) {
  1466. reg |= LVDS_DIGION;
  1467. if (!(reg & LVDS_ON)) {
  1468. reg &= ~LVDS_BLON;
  1469. aty_st_le32(LVDS_GEN_CNTL, reg);
  1470. aty_ld_le32(LVDS_GEN_CNTL);
  1471. mdelay(10);
  1472. reg |= LVDS_BLON;
  1473. aty_st_le32(LVDS_GEN_CNTL, reg);
  1474. }
  1475. reg &= ~LVDS_BL_MOD_LEVEL_MASK;
  1476. reg |= (aty128_bl_get_level_brightness(par, level) << LVDS_BL_MOD_LEVEL_SHIFT);
  1477. #ifdef BACKLIGHT_LVDS_OFF
  1478. reg |= LVDS_ON | LVDS_EN;
  1479. reg &= ~LVDS_DISPLAY_DIS;
  1480. #endif
  1481. aty_st_le32(LVDS_GEN_CNTL, reg);
  1482. #ifdef BACKLIGHT_DAC_OFF
  1483. aty_st_le32(DAC_CNTL, aty_ld_le32(DAC_CNTL) & (~DAC_PDWN));
  1484. #endif
  1485. } else {
  1486. reg &= ~LVDS_BL_MOD_LEVEL_MASK;
  1487. reg |= (aty128_bl_get_level_brightness(par, 0) << LVDS_BL_MOD_LEVEL_SHIFT);
  1488. #ifdef BACKLIGHT_LVDS_OFF
  1489. reg |= LVDS_DISPLAY_DIS;
  1490. aty_st_le32(LVDS_GEN_CNTL, reg);
  1491. aty_ld_le32(LVDS_GEN_CNTL);
  1492. udelay(10);
  1493. reg &= ~(LVDS_ON | LVDS_EN | LVDS_BLON | LVDS_DIGION);
  1494. #endif
  1495. aty_st_le32(LVDS_GEN_CNTL, reg);
  1496. #ifdef BACKLIGHT_DAC_OFF
  1497. aty_st_le32(DAC_CNTL, aty_ld_le32(DAC_CNTL) | DAC_PDWN);
  1498. #endif
  1499. }
  1500. return 0;
  1501. }
  1502. static int aty128_bl_get_brightness(struct backlight_device *bd)
  1503. {
  1504. return bd->props.brightness;
  1505. }
  1506. static struct backlight_ops aty128_bl_data = {
  1507. .get_brightness = aty128_bl_get_brightness,
  1508. .update_status = aty128_bl_update_status,
  1509. };
  1510. static void aty128_bl_set_power(struct fb_info *info, int power)
  1511. {
  1512. if (info->bl_dev) {
  1513. info->bl_dev->props.power = power;
  1514. backlight_update_status(info->bl_dev);
  1515. }
  1516. }
  1517. static void aty128_bl_init(struct aty128fb_par *par)
  1518. {
  1519. struct fb_info *info = pci_get_drvdata(par->pdev);
  1520. struct backlight_device *bd;
  1521. char name[12];
  1522. /* Could be extended to Rage128Pro LVDS output too */
  1523. if (par->chip_gen != rage_M3)
  1524. return;
  1525. #ifdef CONFIG_PMAC_BACKLIGHT
  1526. if (!pmac_has_backlight_type("ati"))
  1527. return;
  1528. #endif
  1529. snprintf(name, sizeof(name), "aty128bl%d", info->node);
  1530. bd = backlight_device_register(name, info->dev, par, &aty128_bl_data);
  1531. if (IS_ERR(bd)) {
  1532. info->bl_dev = NULL;
  1533. printk(KERN_WARNING "aty128: Backlight registration failed\n");
  1534. goto error;
  1535. }
  1536. info->bl_dev = bd;
  1537. fb_bl_default_curve(info, 0,
  1538. 63 * FB_BACKLIGHT_MAX / MAX_LEVEL,
  1539. 219 * FB_BACKLIGHT_MAX / MAX_LEVEL);
  1540. bd->props.max_brightness = FB_BACKLIGHT_LEVELS - 1;
  1541. bd->props.brightness = bd->props.max_brightness;
  1542. bd->props.power = FB_BLANK_UNBLANK;
  1543. backlight_update_status(bd);
  1544. printk("aty128: Backlight initialized (%s)\n", name);
  1545. return;
  1546. error:
  1547. return;
  1548. }
  1549. static void aty128_bl_exit(struct backlight_device *bd)
  1550. {
  1551. backlight_device_unregister(bd);
  1552. printk("aty128: Backlight unloaded\n");
  1553. }
  1554. #endif /* CONFIG_FB_ATY128_BACKLIGHT */
  1555. /*
  1556. * Initialisation
  1557. */
  1558. #ifdef CONFIG_PPC_PMAC
  1559. static void aty128_early_resume(void *data)
  1560. {
  1561. struct aty128fb_par *par = data;
  1562. if (try_acquire_console_sem())
  1563. return;
  1564. aty128_do_resume(par->pdev);
  1565. release_console_sem();
  1566. }
  1567. #endif /* CONFIG_PPC_PMAC */
  1568. static int __devinit aty128_init(struct pci_dev *pdev, const struct pci_device_id *ent)
  1569. {
  1570. struct fb_info *info = pci_get_drvdata(pdev);
  1571. struct aty128fb_par *par = info->par;
  1572. struct fb_var_screeninfo var;
  1573. char video_card[50];
  1574. u8 chip_rev;
  1575. u32 dac;
  1576. /* Get the chip revision */
  1577. chip_rev = (aty_ld_le32(CONFIG_CNTL) >> 16) & 0x1F;
  1578. strcpy(video_card, "Rage128 XX ");
  1579. video_card[8] = ent->device >> 8;
  1580. video_card[9] = ent->device & 0xFF;
  1581. /* range check to make sure */
  1582. if (ent->driver_data < ARRAY_SIZE(r128_family))
  1583. strlcat(video_card, r128_family[ent->driver_data], sizeof(video_card));
  1584. printk(KERN_INFO "aty128fb: %s [chip rev 0x%x] ", video_card, chip_rev);
  1585. if (par->vram_size % (1024 * 1024) == 0)
  1586. printk("%dM %s\n", par->vram_size / (1024*1024), par->mem->name);
  1587. else
  1588. printk("%dk %s\n", par->vram_size / 1024, par->mem->name);
  1589. par->chip_gen = ent->driver_data;
  1590. /* fill in info */
  1591. info->fbops = &aty128fb_ops;
  1592. info->flags = FBINFO_FLAG_DEFAULT;
  1593. par->lcd_on = default_lcd_on;
  1594. par->crt_on = default_crt_on;
  1595. var = default_var;
  1596. #ifdef CONFIG_PPC_PMAC
  1597. if (machine_is(powermac)) {
  1598. /* Indicate sleep capability */
  1599. if (par->chip_gen == rage_M3) {
  1600. pmac_call_feature(PMAC_FTR_DEVICE_CAN_WAKE, NULL, 0, 1);
  1601. pmac_set_early_video_resume(aty128_early_resume, par);
  1602. }
  1603. /* Find default mode */
  1604. if (mode_option) {
  1605. if (!mac_find_mode(&var, info, mode_option, 8))
  1606. var = default_var;
  1607. } else {
  1608. if (default_vmode <= 0 || default_vmode > VMODE_MAX)
  1609. default_vmode = VMODE_1024_768_60;
  1610. /* iMacs need that resolution
  1611. * PowerMac2,1 first r128 iMacs
  1612. * PowerMac2,2 summer 2000 iMacs
  1613. * PowerMac4,1 january 2001 iMacs "flower power"
  1614. */
  1615. if (machine_is_compatible("PowerMac2,1") ||
  1616. machine_is_compatible("PowerMac2,2") ||
  1617. machine_is_compatible("PowerMac4,1"))
  1618. default_vmode = VMODE_1024_768_75;
  1619. /* iBook SE */
  1620. if (machine_is_compatible("PowerBook2,2"))
  1621. default_vmode = VMODE_800_600_60;
  1622. /* PowerBook Firewire (Pismo), iBook Dual USB */
  1623. if (machine_is_compatible("PowerBook3,1") ||
  1624. machine_is_compatible("PowerBook4,1"))
  1625. default_vmode = VMODE_1024_768_60;
  1626. /* PowerBook Titanium */
  1627. if (machine_is_compatible("PowerBook3,2"))
  1628. default_vmode = VMODE_1152_768_60;
  1629. if (default_cmode > 16)
  1630. default_cmode = CMODE_32;
  1631. else if (default_cmode > 8)
  1632. default_cmode = CMODE_16;
  1633. else
  1634. default_cmode = CMODE_8;
  1635. if (mac_vmode_to_var(default_vmode, default_cmode, &var))
  1636. var = default_var;
  1637. }
  1638. } else
  1639. #endif /* CONFIG_PPC_PMAC */
  1640. {
  1641. if (mode_option)
  1642. if (fb_find_mode(&var, info, mode_option, NULL,
  1643. 0, &defaultmode, 8) == 0)
  1644. var = default_var;
  1645. }
  1646. var.accel_flags &= ~FB_ACCELF_TEXT;
  1647. // var.accel_flags |= FB_ACCELF_TEXT;/* FIXME Will add accel later */
  1648. if (aty128fb_check_var(&var, info)) {
  1649. printk(KERN_ERR "aty128fb: Cannot set default mode.\n");
  1650. return 0;
  1651. }
  1652. /* setup the DAC the way we like it */
  1653. dac = aty_ld_le32(DAC_CNTL);
  1654. dac |= (DAC_8BIT_EN | DAC_RANGE_CNTL);
  1655. dac |= DAC_MASK;
  1656. if (par->chip_gen == rage_M3)
  1657. dac |= DAC_PALETTE2_SNOOP_EN;
  1658. aty_st_le32(DAC_CNTL, dac);
  1659. /* turn off bus mastering, just in case */
  1660. aty_st_le32(BUS_CNTL, aty_ld_le32(BUS_CNTL) | BUS_MASTER_DIS);
  1661. info->var = var;
  1662. fb_alloc_cmap(&info->cmap, 256, 0);
  1663. var.activate = FB_ACTIVATE_NOW;
  1664. aty128_init_engine(par);
  1665. par->pm_reg = pci_find_capability(pdev, PCI_CAP_ID_PM);
  1666. par->pdev = pdev;
  1667. par->asleep = 0;
  1668. par->lock_blank = 0;
  1669. #ifdef CONFIG_FB_ATY128_BACKLIGHT
  1670. if (backlight)
  1671. aty128_bl_init(par);
  1672. #endif
  1673. if (register_framebuffer(info) < 0)
  1674. return 0;
  1675. printk(KERN_INFO "fb%d: %s frame buffer device on %s\n",
  1676. info->node, info->fix.id, video_card);
  1677. return 1; /* success! */
  1678. }
  1679. #ifdef CONFIG_PCI
  1680. /* register a card ++ajoshi */
  1681. static int __devinit aty128_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  1682. {
  1683. unsigned long fb_addr, reg_addr;
  1684. struct aty128fb_par *par;
  1685. struct fb_info *info;
  1686. int err;
  1687. #ifndef __sparc__
  1688. void __iomem *bios = NULL;
  1689. #endif
  1690. /* Enable device in PCI config */
  1691. if ((err = pci_enable_device(pdev))) {
  1692. printk(KERN_ERR "aty128fb: Cannot enable PCI device: %d\n",
  1693. err);
  1694. return -ENODEV;
  1695. }
  1696. fb_addr = pci_resource_start(pdev, 0);
  1697. if (!request_mem_region(fb_addr, pci_resource_len(pdev, 0),
  1698. "aty128fb FB")) {
  1699. printk(KERN_ERR "aty128fb: cannot reserve frame "
  1700. "buffer memory\n");
  1701. return -ENODEV;
  1702. }
  1703. reg_addr = pci_resource_start(pdev, 2);
  1704. if (!request_mem_region(reg_addr, pci_resource_len(pdev, 2),
  1705. "aty128fb MMIO")) {
  1706. printk(KERN_ERR "aty128fb: cannot reserve MMIO region\n");
  1707. goto err_free_fb;
  1708. }
  1709. /* We have the resources. Now virtualize them */
  1710. info = framebuffer_alloc(sizeof(struct aty128fb_par), &pdev->dev);
  1711. if (info == NULL) {
  1712. printk(KERN_ERR "aty128fb: can't alloc fb_info_aty128\n");
  1713. goto err_free_mmio;
  1714. }
  1715. par = info->par;
  1716. info->pseudo_palette = par->pseudo_palette;
  1717. /* Virtualize mmio region */
  1718. info->fix.mmio_start = reg_addr;
  1719. par->regbase = ioremap(reg_addr, pci_resource_len(pdev, 2));
  1720. if (!par->regbase)
  1721. goto err_free_info;
  1722. /* Grab memory size from the card */
  1723. // How does this relate to the resource length from the PCI hardware?
  1724. par->vram_size = aty_ld_le32(CONFIG_MEMSIZE) & 0x03FFFFFF;
  1725. /* Virtualize the framebuffer */
  1726. info->screen_base = ioremap(fb_addr, par->vram_size);
  1727. if (!info->screen_base)
  1728. goto err_unmap_out;
  1729. /* Set up info->fix */
  1730. info->fix = aty128fb_fix;
  1731. info->fix.smem_start = fb_addr;
  1732. info->fix.smem_len = par->vram_size;
  1733. info->fix.mmio_start = reg_addr;
  1734. /* If we can't test scratch registers, something is seriously wrong */
  1735. if (!register_test(par)) {
  1736. printk(KERN_ERR "aty128fb: Can't write to video register!\n");
  1737. goto err_out;
  1738. }
  1739. #ifndef __sparc__
  1740. bios = aty128_map_ROM(par, pdev);
  1741. #ifdef CONFIG_X86
  1742. if (bios == NULL)
  1743. bios = aty128_find_mem_vbios(par);
  1744. #endif
  1745. if (bios == NULL)
  1746. printk(KERN_INFO "aty128fb: BIOS not located, guessing timings.\n");
  1747. else {
  1748. printk(KERN_INFO "aty128fb: Rage128 BIOS located\n");
  1749. aty128_get_pllinfo(par, bios);
  1750. pci_unmap_rom(pdev, bios);
  1751. }
  1752. #endif /* __sparc__ */
  1753. aty128_timings(par);
  1754. pci_set_drvdata(pdev, info);
  1755. if (!aty128_init(pdev, ent))
  1756. goto err_out;
  1757. #ifdef CONFIG_MTRR
  1758. if (mtrr) {
  1759. par->mtrr.vram = mtrr_add(info->fix.smem_start,
  1760. par->vram_size, MTRR_TYPE_WRCOMB, 1);
  1761. par->mtrr.vram_valid = 1;
  1762. /* let there be speed */
  1763. printk(KERN_INFO "aty128fb: Rage128 MTRR set to ON\n");
  1764. }
  1765. #endif /* CONFIG_MTRR */
  1766. return 0;
  1767. err_out:
  1768. iounmap(info->screen_base);
  1769. err_unmap_out:
  1770. iounmap(par->regbase);
  1771. err_free_info:
  1772. framebuffer_release(info);
  1773. err_free_mmio:
  1774. release_mem_region(pci_resource_start(pdev, 2),
  1775. pci_resource_len(pdev, 2));
  1776. err_free_fb:
  1777. release_mem_region(pci_resource_start(pdev, 0),
  1778. pci_resource_len(pdev, 0));
  1779. return -ENODEV;
  1780. }
  1781. static void __devexit aty128_remove(struct pci_dev *pdev)
  1782. {
  1783. struct fb_info *info = pci_get_drvdata(pdev);
  1784. struct aty128fb_par *par;
  1785. if (!info)
  1786. return;
  1787. par = info->par;
  1788. unregister_framebuffer(info);
  1789. #ifdef CONFIG_FB_ATY128_BACKLIGHT
  1790. aty128_bl_exit(info->bl_dev);
  1791. #endif
  1792. #ifdef CONFIG_MTRR
  1793. if (par->mtrr.vram_valid)
  1794. mtrr_del(par->mtrr.vram, info->fix.smem_start,
  1795. par->vram_size);
  1796. #endif /* CONFIG_MTRR */
  1797. iounmap(par->regbase);
  1798. iounmap(info->screen_base);
  1799. release_mem_region(pci_resource_start(pdev, 0),
  1800. pci_resource_len(pdev, 0));
  1801. release_mem_region(pci_resource_start(pdev, 2),
  1802. pci_resource_len(pdev, 2));
  1803. framebuffer_release(info);
  1804. }
  1805. #endif /* CONFIG_PCI */
  1806. /*
  1807. * Blank the display.
  1808. */
  1809. static int aty128fb_blank(int blank, struct fb_info *fb)
  1810. {
  1811. struct aty128fb_par *par = fb->par;
  1812. u8 state;
  1813. if (par->lock_blank || par->asleep)
  1814. return 0;
  1815. switch (blank) {
  1816. case FB_BLANK_NORMAL:
  1817. state = 4;
  1818. break;
  1819. case FB_BLANK_VSYNC_SUSPEND:
  1820. state = 6;
  1821. break;
  1822. case FB_BLANK_HSYNC_SUSPEND:
  1823. state = 5;
  1824. break;
  1825. case FB_BLANK_POWERDOWN:
  1826. state = 7;
  1827. break;
  1828. case FB_BLANK_UNBLANK:
  1829. default:
  1830. state = 0;
  1831. break;
  1832. }
  1833. aty_st_8(CRTC_EXT_CNTL+1, state);
  1834. if (par->chip_gen == rage_M3) {
  1835. aty128_set_crt_enable(par, par->crt_on && !blank);
  1836. aty128_set_lcd_enable(par, par->lcd_on && !blank);
  1837. }
  1838. return 0;
  1839. }
  1840. /*
  1841. * Set a single color register. The values supplied are already
  1842. * rounded down to the hardware's capabilities (according to the
  1843. * entries in the var structure). Return != 0 for invalid regno.
  1844. */
  1845. static int aty128fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  1846. u_int transp, struct fb_info *info)
  1847. {
  1848. struct aty128fb_par *par = info->par;
  1849. if (regno > 255
  1850. || (par->crtc.depth == 16 && regno > 63)
  1851. || (par->crtc.depth == 15 && regno > 31))
  1852. return 1;
  1853. red >>= 8;
  1854. green >>= 8;
  1855. blue >>= 8;
  1856. if (regno < 16) {
  1857. int i;
  1858. u32 *pal = info->pseudo_palette;
  1859. switch (par->crtc.depth) {
  1860. case 15:
  1861. pal[regno] = (regno << 10) | (regno << 5) | regno;
  1862. break;
  1863. case 16:
  1864. pal[regno] = (regno << 11) | (regno << 6) | regno;
  1865. break;
  1866. case 24:
  1867. pal[regno] = (regno << 16) | (regno << 8) | regno;
  1868. break;
  1869. case 32:
  1870. i = (regno << 8) | regno;
  1871. pal[regno] = (i << 16) | i;
  1872. break;
  1873. }
  1874. }
  1875. if (par->crtc.depth == 16 && regno > 0) {
  1876. /*
  1877. * With the 5-6-5 split of bits for RGB at 16 bits/pixel, we
  1878. * have 32 slots for R and B values but 64 slots for G values.
  1879. * Thus the R and B values go in one slot but the G value
  1880. * goes in a different slot, and we have to avoid disturbing
  1881. * the other fields in the slots we touch.
  1882. */
  1883. par->green[regno] = green;
  1884. if (regno < 32) {
  1885. par->red[regno] = red;
  1886. par->blue[regno] = blue;
  1887. aty128_st_pal(regno * 8, red, par->green[regno*2],
  1888. blue, par);
  1889. }
  1890. red = par->red[regno/2];
  1891. blue = par->blue[regno/2];
  1892. regno <<= 2;
  1893. } else if (par->crtc.bpp == 16)
  1894. regno <<= 3;
  1895. aty128_st_pal(regno, red, green, blue, par);
  1896. return 0;
  1897. }
  1898. #define ATY_MIRROR_LCD_ON 0x00000001
  1899. #define ATY_MIRROR_CRT_ON 0x00000002
  1900. /* out param: u32* backlight value: 0 to 15 */
  1901. #define FBIO_ATY128_GET_MIRROR _IOR('@', 1, __u32)
  1902. /* in param: u32* backlight value: 0 to 15 */
  1903. #define FBIO_ATY128_SET_MIRROR _IOW('@', 2, __u32)
  1904. static int aty128fb_ioctl(struct fb_info *info, u_int cmd, u_long arg)
  1905. {
  1906. struct aty128fb_par *par = info->par;
  1907. u32 value;
  1908. int rc;
  1909. switch (cmd) {
  1910. case FBIO_ATY128_SET_MIRROR:
  1911. if (par->chip_gen != rage_M3)
  1912. return -EINVAL;
  1913. rc = get_user(value, (__u32 __user *)arg);
  1914. if (rc)
  1915. return rc;
  1916. par->lcd_on = (value & 0x01) != 0;
  1917. par->crt_on = (value & 0x02) != 0;
  1918. if (!par->crt_on && !par->lcd_on)
  1919. par->lcd_on = 1;
  1920. aty128_set_crt_enable(par, par->crt_on);
  1921. aty128_set_lcd_enable(par, par->lcd_on);
  1922. return 0;
  1923. case FBIO_ATY128_GET_MIRROR:
  1924. if (par->chip_gen != rage_M3)
  1925. return -EINVAL;
  1926. value = (par->crt_on << 1) | par->lcd_on;
  1927. return put_user(value, (__u32 __user *)arg);
  1928. }
  1929. return -EINVAL;
  1930. }
  1931. #if 0
  1932. /*
  1933. * Accelerated functions
  1934. */
  1935. static inline void aty128_rectcopy(int srcx, int srcy, int dstx, int dsty,
  1936. u_int width, u_int height,
  1937. struct fb_info_aty128 *par)
  1938. {
  1939. u32 save_dp_datatype, save_dp_cntl, dstval;
  1940. if (!width || !height)
  1941. return;
  1942. dstval = depth_to_dst(par->current_par.crtc.depth);
  1943. if (dstval == DST_24BPP) {
  1944. srcx *= 3;
  1945. dstx *= 3;
  1946. width *= 3;
  1947. } else if (dstval == -EINVAL) {
  1948. printk("aty128fb: invalid depth or RGBA\n");
  1949. return;
  1950. }
  1951. wait_for_fifo(2, par);
  1952. save_dp_datatype = aty_ld_le32(DP_DATATYPE);
  1953. save_dp_cntl = aty_ld_le32(DP_CNTL);
  1954. wait_for_fifo(6, par);
  1955. aty_st_le32(SRC_Y_X, (srcy << 16) | srcx);
  1956. aty_st_le32(DP_MIX, ROP3_SRCCOPY | DP_SRC_RECT);
  1957. aty_st_le32(DP_CNTL, DST_X_LEFT_TO_RIGHT | DST_Y_TOP_TO_BOTTOM);
  1958. aty_st_le32(DP_DATATYPE, save_dp_datatype | dstval | SRC_DSTCOLOR);
  1959. aty_st_le32(DST_Y_X, (dsty << 16) | dstx);
  1960. aty_st_le32(DST_HEIGHT_WIDTH, (height << 16) | width);
  1961. par->blitter_may_be_busy = 1;
  1962. wait_for_fifo(2, par);
  1963. aty_st_le32(DP_DATATYPE, save_dp_datatype);
  1964. aty_st_le32(DP_CNTL, save_dp_cntl);
  1965. }
  1966. /*
  1967. * Text mode accelerated functions
  1968. */
  1969. static void fbcon_aty128_bmove(struct display *p, int sy, int sx, int dy, int dx,
  1970. int height, int width)
  1971. {
  1972. sx *= fontwidth(p);
  1973. sy *= fontheight(p);
  1974. dx *= fontwidth(p);
  1975. dy *= fontheight(p);
  1976. width *= fontwidth(p);
  1977. height *= fontheight(p);
  1978. aty128_rectcopy(sx, sy, dx, dy, width, height,
  1979. (struct fb_info_aty128 *)p->fb_info);
  1980. }
  1981. #endif /* 0 */
  1982. static void aty128_set_suspend(struct aty128fb_par *par, int suspend)
  1983. {
  1984. u32 pmgt;
  1985. u16 pwr_command;
  1986. struct pci_dev *pdev = par->pdev;
  1987. if (!par->pm_reg)
  1988. return;
  1989. /* Set the chip into the appropriate suspend mode (we use D2,
  1990. * D3 would require a complete re-initialisation of the chip,
  1991. * including PCI config registers, clocks, AGP configuration, ...)
  1992. */
  1993. if (suspend) {
  1994. /* Make sure CRTC2 is reset. Remove that the day we decide to
  1995. * actually use CRTC2 and replace it with real code for disabling
  1996. * the CRTC2 output during sleep
  1997. */
  1998. aty_st_le32(CRTC2_GEN_CNTL, aty_ld_le32(CRTC2_GEN_CNTL) &
  1999. ~(CRTC2_EN));
  2000. /* Set the power management mode to be PCI based */
  2001. /* Use this magic value for now */
  2002. pmgt = 0x0c005407;
  2003. aty_st_pll(POWER_MANAGEMENT, pmgt);
  2004. (void)aty_ld_pll(POWER_MANAGEMENT);
  2005. aty_st_le32(BUS_CNTL1, 0x00000010);
  2006. aty_st_le32(MEM_POWER_MISC, 0x0c830000);
  2007. mdelay(100);
  2008. pci_read_config_word(pdev, par->pm_reg+PCI_PM_CTRL, &pwr_command);
  2009. /* Switch PCI power management to D2 */
  2010. pci_write_config_word(pdev, par->pm_reg+PCI_PM_CTRL,
  2011. (pwr_command & ~PCI_PM_CTRL_STATE_MASK) | 2);
  2012. pci_read_config_word(pdev, par->pm_reg+PCI_PM_CTRL, &pwr_command);
  2013. } else {
  2014. /* Switch back PCI power management to D0 */
  2015. mdelay(100);
  2016. pci_write_config_word(pdev, par->pm_reg+PCI_PM_CTRL, 0);
  2017. pci_read_config_word(pdev, par->pm_reg+PCI_PM_CTRL, &pwr_command);
  2018. mdelay(100);
  2019. }
  2020. }
  2021. static int aty128_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  2022. {
  2023. struct fb_info *info = pci_get_drvdata(pdev);
  2024. struct aty128fb_par *par = info->par;
  2025. /* We don't do anything but D2, for now we return 0, but
  2026. * we may want to change that. How do we know if the BIOS
  2027. * can properly take care of D3 ? Also, with swsusp, we
  2028. * know we'll be rebooted, ...
  2029. */
  2030. #ifndef CONFIG_PPC_PMAC
  2031. /* HACK ALERT ! Once I find a proper way to say to each driver
  2032. * individually what will happen with it's PCI slot, I'll change
  2033. * that. On laptops, the AGP slot is just unclocked, so D2 is
  2034. * expected, while on desktops, the card is powered off
  2035. */
  2036. return 0;
  2037. #endif /* CONFIG_PPC_PMAC */
  2038. if (state.event == pdev->dev.power.power_state.event)
  2039. return 0;
  2040. printk(KERN_DEBUG "aty128fb: suspending...\n");
  2041. acquire_console_sem();
  2042. fb_set_suspend(info, 1);
  2043. /* Make sure engine is reset */
  2044. wait_for_idle(par);
  2045. aty128_reset_engine(par);
  2046. wait_for_idle(par);
  2047. /* Blank display and LCD */
  2048. aty128fb_blank(FB_BLANK_POWERDOWN, info);
  2049. /* Sleep */
  2050. par->asleep = 1;
  2051. par->lock_blank = 1;
  2052. #ifdef CONFIG_PPC_PMAC
  2053. /* On powermac, we have hooks to properly suspend/resume AGP now,
  2054. * use them here. We'll ultimately need some generic support here,
  2055. * but the generic code isn't quite ready for that yet
  2056. */
  2057. pmac_suspend_agp_for_card(pdev);
  2058. #endif /* CONFIG_PPC_PMAC */
  2059. /* We need a way to make sure the fbdev layer will _not_ touch the
  2060. * framebuffer before we put the chip to suspend state. On 2.4, I
  2061. * used dummy fb ops, 2.5 need proper support for this at the
  2062. * fbdev level
  2063. */
  2064. if (state.event != PM_EVENT_ON)
  2065. aty128_set_suspend(par, 1);
  2066. release_console_sem();
  2067. pdev->dev.power.power_state = state;
  2068. return 0;
  2069. }
  2070. static int aty128_do_resume(struct pci_dev *pdev)
  2071. {
  2072. struct fb_info *info = pci_get_drvdata(pdev);
  2073. struct aty128fb_par *par = info->par;
  2074. if (pdev->dev.power.power_state.event == PM_EVENT_ON)
  2075. return 0;
  2076. /* Wakeup chip */
  2077. aty128_set_suspend(par, 0);
  2078. par->asleep = 0;
  2079. /* Restore display & engine */
  2080. aty128_reset_engine(par);
  2081. wait_for_idle(par);
  2082. aty128fb_set_par(info);
  2083. fb_pan_display(info, &info->var);
  2084. fb_set_cmap(&info->cmap, info);
  2085. /* Refresh */
  2086. fb_set_suspend(info, 0);
  2087. /* Unblank */
  2088. par->lock_blank = 0;
  2089. aty128fb_blank(0, info);
  2090. #ifdef CONFIG_PPC_PMAC
  2091. /* On powermac, we have hooks to properly suspend/resume AGP now,
  2092. * use them here. We'll ultimately need some generic support here,
  2093. * but the generic code isn't quite ready for that yet
  2094. */
  2095. pmac_resume_agp_for_card(pdev);
  2096. #endif /* CONFIG_PPC_PMAC */
  2097. pdev->dev.power.power_state = PMSG_ON;
  2098. printk(KERN_DEBUG "aty128fb: resumed !\n");
  2099. return 0;
  2100. }
  2101. static int aty128_pci_resume(struct pci_dev *pdev)
  2102. {
  2103. int rc;
  2104. acquire_console_sem();
  2105. rc = aty128_do_resume(pdev);
  2106. release_console_sem();
  2107. return rc;
  2108. }
  2109. static int __devinit aty128fb_init(void)
  2110. {
  2111. #ifndef MODULE
  2112. char *option = NULL;
  2113. if (fb_get_options("aty128fb", &option))
  2114. return -ENODEV;
  2115. aty128fb_setup(option);
  2116. #endif
  2117. return pci_register_driver(&aty128fb_driver);
  2118. }
  2119. static void __exit aty128fb_exit(void)
  2120. {
  2121. pci_unregister_driver(&aty128fb_driver);
  2122. }
  2123. module_init(aty128fb_init);
  2124. module_exit(aty128fb_exit);
  2125. MODULE_AUTHOR("(c)1999-2003 Brad Douglas <brad@neruo.com>");
  2126. MODULE_DESCRIPTION("FBDev driver for ATI Rage128 / Pro cards");
  2127. MODULE_LICENSE("GPL");
  2128. module_param(mode_option, charp, 0);
  2129. MODULE_PARM_DESC(mode_option, "Specify resolution as \"<xres>x<yres>[-<bpp>][@<refresh>]\" ");
  2130. #ifdef CONFIG_MTRR
  2131. module_param_named(nomtrr, mtrr, invbool, 0);
  2132. MODULE_PARM_DESC(nomtrr, "bool: Disable MTRR support (0 or 1=disabled) (default=0)");
  2133. #endif