aty128fb.c 63 KB

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