aty128fb.c 65 KB

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