aty128fb.c 66 KB

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