aty128fb.c 66 KB

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