aty128fb.c 66 KB

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