aty128fb.c 64 KB

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