aty128fb.c 65 KB

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