blob: 8a5bbc13082eb92154b6b6c84fd4c415a6be485d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -07002 * Frame buffer driver for Trident TGUI, Blade and Image series
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Krzysztof Helt245a2c22007-10-16 01:28:42 -07004 * Copyright 2001, 2002 - Jani Monoses <jani@iv.ro>
Krzysztof Heltddb53d42009-03-31 15:25:40 -07005 * Copyright 2009 Krzysztof Helt <krzysztof.h1@wp.pl>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * CREDITS:(in order of appearance)
Krzysztof Helt245a2c22007-10-16 01:28:42 -07008 * skeletonfb.c by Geert Uytterhoeven and other fb code in drivers/video
9 * Special thanks ;) to Mattia Crivellini <tia@mclink.it>
10 * much inspired by the XFree86 4.x Trident driver sources
11 * by Alan Hourihane the FreeVGA project
12 * Francesco Salvestrini <salvestrini@users.sf.net> XP support,
13 * code, suggestions
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * TODO:
Krzysztof Helt245a2c22007-10-16 01:28:42 -070015 * timing value tweaking so it looks good on every monitor in every mode
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
19#include <linux/fb.h>
20#include <linux/init.h>
21#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include <linux/delay.h>
Krzysztof Helt10172ed2008-07-23 21:30:54 -070025#include <video/vga.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <video/trident.h>
27
Ondrej Zary6a5e3bd2015-09-25 00:14:14 +020028#include <linux/i2c.h>
29#include <linux/i2c-algo-bit.h>
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031struct tridentfb_par {
Krzysztof Helt245a2c22007-10-16 01:28:42 -070032 void __iomem *io_virt; /* iospace virtual memory address */
Krzysztof Heltea8ee552008-07-23 21:30:51 -070033 u32 pseudo_pal[16];
Krzysztof Helt122e8ad2008-07-23 21:30:52 -070034 int chip_id;
Krzysztof Helt6eed8e12008-07-23 21:30:53 -070035 int flatpanel;
Krzysztof Heltd9cad042008-07-23 21:30:54 -070036 void (*init_accel) (struct tridentfb_par *, int, int);
37 void (*wait_engine) (struct tridentfb_par *);
38 void (*fill_rect)
39 (struct tridentfb_par *par, u32, u32, u32, u32, u32, u32);
40 void (*copy_rect)
41 (struct tridentfb_par *par, u32, u32, u32, u32, u32, u32);
Krzysztof Helt0292be42008-07-23 21:31:08 -070042 void (*image_blit)
43 (struct tridentfb_par *par, const char*,
44 u32, u32, u32, u32, u32, u32);
Krzysztof Helt5cf13842008-07-23 21:31:05 -070045 unsigned char eng_oper; /* engine operation... */
Ondrej Zary6a5e3bd2015-09-25 00:14:14 +020046 bool ddc_registered;
47 struct i2c_adapter ddc_adapter;
48 struct i2c_algo_bit_data ddc_algo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static struct fb_fix_screeninfo tridentfb_fix = {
Krzysztof Helt245a2c22007-10-16 01:28:42 -070052 .id = "Trident",
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 .type = FB_TYPE_PACKED_PIXELS,
54 .ypanstep = 1,
55 .visual = FB_VISUAL_PSEUDOCOLOR,
56 .accel = FB_ACCEL_NONE,
57};
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/* defaults which are normally overriden by user values */
60
61/* video mode */
Ondrej Zary6a5e3bd2015-09-25 00:14:14 +020062static char *mode_option;
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -080063static int bpp = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -080065static int noaccel;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67static int center;
68static int stretch;
69
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -080070static int fp;
71static int crt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -080073static int memsize;
74static int memdiff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static int nativex;
76
Krzysztof Helt07f41e42008-04-28 02:15:06 -070077module_param(mode_option, charp, 0);
78MODULE_PARM_DESC(mode_option, "Initial video mode e.g. '648x480-8@60'");
Krzysztof Helt9e3f0ca2008-04-28 02:15:10 -070079module_param_named(mode, mode_option, charp, 0);
80MODULE_PARM_DESC(mode, "Initial video mode e.g. '648x480-8@60' (deprecated)");
Linus Torvalds1da177e2005-04-16 15:20:36 -070081module_param(bpp, int, 0);
82module_param(center, int, 0);
83module_param(stretch, int, 0);
84module_param(noaccel, int, 0);
85module_param(memsize, int, 0);
86module_param(memdiff, int, 0);
87module_param(nativex, int, 0);
88module_param(fp, int, 0);
Krzysztof Helt6eed8e12008-07-23 21:30:53 -070089MODULE_PARM_DESC(fp, "Define if flatpanel is connected");
Linus Torvalds1da177e2005-04-16 15:20:36 -070090module_param(crt, int, 0);
Krzysztof Helt6eed8e12008-07-23 21:30:53 -070091MODULE_PARM_DESC(crt, "Define if CRT is connected");
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Krzysztof Helt5cf13842008-07-23 21:31:05 -070093static inline int is_oldclock(int id)
Krzysztof Helt6bdf1032008-07-23 21:30:56 -070094{
Krzysztof Helta0d92252008-07-23 21:30:58 -070095 return (id == TGUI9440) ||
96 (id == TGUI9660) ||
Krzysztof Helt0e73a472008-07-23 21:30:58 -070097 (id == CYBER9320);
98}
99
Krzysztof Helt5cf13842008-07-23 21:31:05 -0700100static inline int is_oldprotect(int id)
Krzysztof Helt0e73a472008-07-23 21:30:58 -0700101{
Krzysztof Helt5cf13842008-07-23 21:31:05 -0700102 return is_oldclock(id) ||
Krzysztof Helt0e73a472008-07-23 21:30:58 -0700103 (id == PROVIDIA9685) ||
Krzysztof Helt0e73a472008-07-23 21:30:58 -0700104 (id == CYBER9382) ||
105 (id == CYBER9385);
Krzysztof Helt6bdf1032008-07-23 21:30:56 -0700106}
107
Krzysztof Helt5cf13842008-07-23 21:31:05 -0700108static inline int is_blade(int id)
Krzysztof Helte0759a52008-07-23 21:30:53 -0700109{
110 return (id == BLADE3D) ||
111 (id == CYBERBLADEE4) ||
112 (id == CYBERBLADEi7) ||
113 (id == CYBERBLADEi7D) ||
114 (id == CYBERBLADEi1) ||
115 (id == CYBERBLADEi1D) ||
116 (id == CYBERBLADEAi1) ||
117 (id == CYBERBLADEAi1D);
118}
119
Krzysztof Helt5cf13842008-07-23 21:31:05 -0700120static inline int is_xp(int id)
Krzysztof Helte0759a52008-07-23 21:30:53 -0700121{
122 return (id == CYBERBLADEXPAi1) ||
123 (id == CYBERBLADEXPm8) ||
124 (id == CYBERBLADEXPm16);
125}
126
Krzysztof Helt5cf13842008-07-23 21:31:05 -0700127static inline int is3Dchip(int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Krzysztof Helt5cf13842008-07-23 21:31:05 -0700129 return is_blade(id) || is_xp(id) ||
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700130 (id == CYBER9397) || (id == CYBER9397DVD) ||
131 (id == CYBER9520) || (id == CYBER9525DVD) ||
Krzysztof Helt5cf13842008-07-23 21:31:05 -0700132 (id == IMAGE975) || (id == IMAGE985);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Krzysztof Helt5cf13842008-07-23 21:31:05 -0700135static inline int iscyber(int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 switch (id) {
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700138 case CYBER9388:
139 case CYBER9382:
140 case CYBER9385:
141 case CYBER9397:
142 case CYBER9397DVD:
143 case CYBER9520:
144 case CYBER9525DVD:
145 case CYBERBLADEE4:
146 case CYBERBLADEi7D:
147 case CYBERBLADEi1:
148 case CYBERBLADEi1D:
149 case CYBERBLADEAi1:
150 case CYBERBLADEAi1D:
151 case CYBERBLADEXPAi1:
152 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700154 case CYBER9320:
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700155 case CYBERBLADEi7: /* VIA MPV4 integrated version */
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700156 default:
157 /* case CYBERBLDAEXPm8: Strange */
158 /* case CYBERBLDAEXPm16: Strange */
159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161}
162
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700163static inline void t_outb(struct tridentfb_par *p, u8 val, u16 reg)
164{
165 fb_writeb(val, p->io_virt + reg);
166}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700168static inline u8 t_inb(struct tridentfb_par *p, u16 reg)
169{
170 return fb_readb(p->io_virt + reg);
171}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700173static inline void writemmr(struct tridentfb_par *par, u16 r, u32 v)
174{
175 fb_writel(v, par->io_virt + r);
176}
177
178static inline u32 readmmr(struct tridentfb_par *par, u16 r)
179{
180 return fb_readl(par->io_virt + r);
181}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Ondrej Zary6a5e3bd2015-09-25 00:14:14 +0200183#define DDC_SDA_TGUI BIT(0)
184#define DDC_SCL_TGUI BIT(1)
185#define DDC_SCL_DRIVE_TGUI BIT(2)
186#define DDC_SDA_DRIVE_TGUI BIT(3)
187#define DDC_MASK_TGUI (DDC_SCL_DRIVE_TGUI | DDC_SDA_DRIVE_TGUI)
188
189static void tridentfb_ddc_setscl_tgui(void *data, int val)
190{
191 struct tridentfb_par *par = data;
192 u8 reg = vga_mm_rcrt(par->io_virt, I2C) & DDC_MASK_TGUI;
193
194 if (val)
195 reg &= ~DDC_SCL_DRIVE_TGUI; /* disable drive - don't drive hi */
196 else
197 reg |= DDC_SCL_DRIVE_TGUI; /* drive low */
198
199 vga_mm_wcrt(par->io_virt, I2C, reg);
200}
201
202static void tridentfb_ddc_setsda_tgui(void *data, int val)
203{
204 struct tridentfb_par *par = data;
205 u8 reg = vga_mm_rcrt(par->io_virt, I2C) & DDC_MASK_TGUI;
206
207 if (val)
208 reg &= ~DDC_SDA_DRIVE_TGUI; /* disable drive - don't drive hi */
209 else
210 reg |= DDC_SDA_DRIVE_TGUI; /* drive low */
211
212 vga_mm_wcrt(par->io_virt, I2C, reg);
213}
214
215static int tridentfb_ddc_getsda_tgui(void *data)
216{
217 struct tridentfb_par *par = data;
218
219 return !!(vga_mm_rcrt(par->io_virt, I2C) & DDC_SDA_TGUI);
220}
221
222#define DDC_SDA_IN BIT(0)
223#define DDC_SCL_OUT BIT(1)
224#define DDC_SDA_OUT BIT(3)
225#define DDC_SCL_IN BIT(6)
226#define DDC_MASK (DDC_SCL_OUT | DDC_SDA_OUT)
227
228static void tridentfb_ddc_setscl(void *data, int val)
229{
230 struct tridentfb_par *par = data;
231 unsigned char reg;
232
233 reg = vga_mm_rcrt(par->io_virt, I2C) & DDC_MASK;
234 if (val)
235 reg |= DDC_SCL_OUT;
236 else
237 reg &= ~DDC_SCL_OUT;
238 vga_mm_wcrt(par->io_virt, I2C, reg);
239}
240
241static void tridentfb_ddc_setsda(void *data, int val)
242{
243 struct tridentfb_par *par = data;
244 unsigned char reg;
245
246 reg = vga_mm_rcrt(par->io_virt, I2C) & DDC_MASK;
247 if (!val)
248 reg |= DDC_SDA_OUT;
249 else
250 reg &= ~DDC_SDA_OUT;
251 vga_mm_wcrt(par->io_virt, I2C, reg);
252}
253
254static int tridentfb_ddc_getscl(void *data)
255{
256 struct tridentfb_par *par = data;
257
258 return !!(vga_mm_rcrt(par->io_virt, I2C) & DDC_SCL_IN);
259}
260
261static int tridentfb_ddc_getsda(void *data)
262{
263 struct tridentfb_par *par = data;
264
265 return !!(vga_mm_rcrt(par->io_virt, I2C) & DDC_SDA_IN);
266}
267
268static int tridentfb_setup_ddc_bus(struct fb_info *info)
269{
270 struct tridentfb_par *par = info->par;
271
272 strlcpy(par->ddc_adapter.name, info->fix.id,
273 sizeof(par->ddc_adapter.name));
274 par->ddc_adapter.owner = THIS_MODULE;
275 par->ddc_adapter.class = I2C_CLASS_DDC;
276 par->ddc_adapter.algo_data = &par->ddc_algo;
277 par->ddc_adapter.dev.parent = info->device;
278 if (is_oldclock(par->chip_id)) { /* not sure if this check is OK */
279 par->ddc_algo.setsda = tridentfb_ddc_setsda_tgui;
280 par->ddc_algo.setscl = tridentfb_ddc_setscl_tgui;
281 par->ddc_algo.getsda = tridentfb_ddc_getsda_tgui;
282 /* no getscl */
283 } else {
284 par->ddc_algo.setsda = tridentfb_ddc_setsda;
285 par->ddc_algo.setscl = tridentfb_ddc_setscl;
286 par->ddc_algo.getsda = tridentfb_ddc_getsda;
287 par->ddc_algo.getscl = tridentfb_ddc_getscl;
288 }
289 par->ddc_algo.udelay = 10;
290 par->ddc_algo.timeout = 20;
291 par->ddc_algo.data = par;
292
293 i2c_set_adapdata(&par->ddc_adapter, par);
294
295 return i2c_bit_add_bus(&par->ddc_adapter);
296}
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298/*
299 * Blade specific acceleration.
300 */
301
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700302#define point(x, y) ((y) << 16 | (x))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700304static void blade_init_accel(struct tridentfb_par *par, int pitch, int bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700306 int v1 = (pitch >> 3) << 20;
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700307 int tmp = bpp == 24 ? 2 : (bpp >> 4);
308 int v2 = v1 | (tmp << 29);
309
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700310 writemmr(par, 0x21C0, v2);
311 writemmr(par, 0x21C4, v2);
312 writemmr(par, 0x21B8, v2);
313 writemmr(par, 0x21BC, v2);
314 writemmr(par, 0x21D0, v1);
315 writemmr(par, 0x21D4, v1);
316 writemmr(par, 0x21C8, v1);
317 writemmr(par, 0x21CC, v1);
318 writemmr(par, 0x216C, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700321static void blade_wait_engine(struct tridentfb_par *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700323 while (readmmr(par, STATUS) & 0xFA800000)
324 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700327static void blade_fill_rect(struct tridentfb_par *par,
328 u32 x, u32 y, u32 w, u32 h, u32 c, u32 rop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700330 writemmr(par, COLOR, c);
331 writemmr(par, ROP, rop ? ROP_X : ROP_S);
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700332 writemmr(par, CMD, 0x20000000 | 1 << 19 | 1 << 4 | 2 << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700334 writemmr(par, DST1, point(x, y));
335 writemmr(par, DST2, point(x + w - 1, y + h - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
Krzysztof Helt0292be42008-07-23 21:31:08 -0700338static void blade_image_blit(struct tridentfb_par *par, const char *data,
339 u32 x, u32 y, u32 w, u32 h, u32 c, u32 b)
340{
341 unsigned size = ((w + 31) >> 5) * h;
342
343 writemmr(par, COLOR, c);
344 writemmr(par, BGCOLOR, b);
345 writemmr(par, CMD, 0xa0000000 | 3 << 19);
346
347 writemmr(par, DST1, point(x, y));
348 writemmr(par, DST2, point(x + w - 1, y + h - 1));
349
Ondrej Zary7f2ea952015-08-24 21:37:25 +0200350 iowrite32_rep(par->io_virt + 0x10000, data, size);
Krzysztof Helt0292be42008-07-23 21:31:08 -0700351}
352
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700353static void blade_copy_rect(struct tridentfb_par *par,
354 u32 x1, u32 y1, u32 x2, u32 y2, u32 w, u32 h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 int direction = 2;
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700357 u32 s1 = point(x1, y1);
358 u32 s2 = point(x1 + w - 1, y1 + h - 1);
359 u32 d1 = point(x2, y2);
360 u32 d2 = point(x2 + w - 1, y2 + h - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 if ((y1 > y2) || ((y1 == y2) && (x1 > x2)))
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700363 direction = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700365 writemmr(par, ROP, ROP_S);
366 writemmr(par, CMD, 0xE0000000 | 1 << 19 | 1 << 4 | 1 << 2 | direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700368 writemmr(par, SRC1, direction ? s2 : s1);
369 writemmr(par, SRC2, direction ? s1 : s2);
370 writemmr(par, DST1, direction ? d2 : d1);
371 writemmr(par, DST2, direction ? d1 : d2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374/*
375 * BladeXP specific acceleration functions
376 */
377
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700378static void xp_init_accel(struct tridentfb_par *par, int pitch, int bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700380 unsigned char x = bpp == 24 ? 3 : (bpp >> 4);
381 int v1 = pitch << (bpp == 24 ? 20 : (18 + x));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 switch (pitch << (bpp >> 3)) {
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700384 case 8192:
385 case 512:
386 x |= 0x00;
387 break;
388 case 1024:
389 x |= 0x04;
390 break;
391 case 2048:
392 x |= 0x08;
393 break;
394 case 4096:
395 x |= 0x0C;
396 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700399 t_outb(par, x, 0x2125);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Krzysztof Helt5cf13842008-07-23 21:31:05 -0700401 par->eng_oper = x | 0x40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700403 writemmr(par, 0x2154, v1);
404 writemmr(par, 0x2150, v1);
405 t_outb(par, 3, 0x2126);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700408static void xp_wait_engine(struct tridentfb_par *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Krzysztof Helt5cf13842008-07-23 21:31:05 -0700410 int count = 0;
411 int timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700413 while (t_inb(par, STATUS) & 0x80) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 count++;
415 if (count == 10000000) {
416 /* Timeout */
417 count = 9990000;
418 timeout++;
419 if (timeout == 8) {
420 /* Reset engine */
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700421 t_outb(par, 0x00, STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return;
423 }
424 }
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700425 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
427}
428
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700429static void xp_fill_rect(struct tridentfb_par *par,
430 u32 x, u32 y, u32 w, u32 h, u32 c, u32 rop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700432 writemmr(par, 0x2127, ROP_P);
433 writemmr(par, 0x2158, c);
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700434 writemmr(par, DRAWFL, 0x4000);
435 writemmr(par, OLDDIM, point(h, w));
436 writemmr(par, OLDDST, point(y, x));
437 t_outb(par, 0x01, OLDCMD);
Krzysztof Helt5cf13842008-07-23 21:31:05 -0700438 t_outb(par, par->eng_oper, 0x2125);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700441static void xp_copy_rect(struct tridentfb_par *par,
442 u32 x1, u32 y1, u32 x2, u32 y2, u32 w, u32 h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700444 u32 x1_tmp, x2_tmp, y1_tmp, y2_tmp;
Krzysztof Helt5cf13842008-07-23 21:31:05 -0700445 int direction = 0x0004;
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if ((x1 < x2) && (y1 == y2)) {
448 direction |= 0x0200;
449 x1_tmp = x1 + w - 1;
450 x2_tmp = x2 + w - 1;
451 } else {
452 x1_tmp = x1;
453 x2_tmp = x2;
454 }
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (y1 < y2) {
457 direction |= 0x0100;
458 y1_tmp = y1 + h - 1;
459 y2_tmp = y2 + h - 1;
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700460 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 y1_tmp = y1;
462 y2_tmp = y2;
463 }
464
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700465 writemmr(par, DRAWFL, direction);
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700466 t_outb(par, ROP_S, 0x2127);
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700467 writemmr(par, OLDSRC, point(y1_tmp, x1_tmp));
468 writemmr(par, OLDDST, point(y2_tmp, x2_tmp));
469 writemmr(par, OLDDIM, point(h, w));
470 t_outb(par, 0x01, OLDCMD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473/*
474 * Image specific acceleration functions
475 */
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700476static void image_init_accel(struct tridentfb_par *par, int pitch, int bpp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700478 int tmp = bpp == 24 ? 2: (bpp >> 4);
479
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700480 writemmr(par, 0x2120, 0xF0000000);
481 writemmr(par, 0x2120, 0x40000000 | tmp);
482 writemmr(par, 0x2120, 0x80000000);
483 writemmr(par, 0x2144, 0x00000000);
484 writemmr(par, 0x2148, 0x00000000);
485 writemmr(par, 0x2150, 0x00000000);
486 writemmr(par, 0x2154, 0x00000000);
487 writemmr(par, 0x2120, 0x60000000 | (pitch << 16) | pitch);
488 writemmr(par, 0x216C, 0x00000000);
489 writemmr(par, 0x2170, 0x00000000);
490 writemmr(par, 0x217C, 0x00000000);
491 writemmr(par, 0x2120, 0x10000000);
492 writemmr(par, 0x2130, (2047 << 16) | 2047);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700495static void image_wait_engine(struct tridentfb_par *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700497 while (readmmr(par, 0x2164) & 0xF0000000)
498 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700501static void image_fill_rect(struct tridentfb_par *par,
502 u32 x, u32 y, u32 w, u32 h, u32 c, u32 rop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700504 writemmr(par, 0x2120, 0x80000000);
505 writemmr(par, 0x2120, 0x90000000 | ROP_S);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700507 writemmr(par, 0x2144, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700509 writemmr(par, DST1, point(x, y));
510 writemmr(par, DST2, point(x + w - 1, y + h - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700512 writemmr(par, 0x2124, 0x80000000 | 3 << 22 | 1 << 10 | 1 << 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700515static void image_copy_rect(struct tridentfb_par *par,
516 u32 x1, u32 y1, u32 x2, u32 y2, u32 w, u32 h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Krzysztof Helt2c86a0c2008-07-23 21:31:03 -0700518 int direction = 0x4;
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700519 u32 s1 = point(x1, y1);
520 u32 s2 = point(x1 + w - 1, y1 + h - 1);
521 u32 d1 = point(x2, y2);
522 u32 d2 = point(x2 + w - 1, y2 + h - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700524 if ((y1 > y2) || ((y1 == y2) && (x1 > x2)))
525 direction = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700527 writemmr(par, 0x2120, 0x80000000);
528 writemmr(par, 0x2120, 0x90000000 | ROP_S);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700530 writemmr(par, SRC1, direction ? s2 : s1);
531 writemmr(par, SRC2, direction ? s1 : s2);
532 writemmr(par, DST1, direction ? d2 : d1);
533 writemmr(par, DST2, direction ? d1 : d2);
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700534 writemmr(par, 0x2124,
535 0x80000000 | 1 << 22 | 1 << 10 | 1 << 7 | direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538/*
Krzysztof Heltbcac2d52008-07-23 21:31:01 -0700539 * TGUI 9440/96XX acceleration
540 */
541
542static void tgui_init_accel(struct tridentfb_par *par, int pitch, int bpp)
543{
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700544 unsigned char x = bpp == 24 ? 3 : (bpp >> 4);
Krzysztof Heltbcac2d52008-07-23 21:31:01 -0700545
546 /* disable clipping */
547 writemmr(par, 0x2148, 0);
548 writemmr(par, 0x214C, point(4095, 2047));
549
Krzysztof Heltbcac2d52008-07-23 21:31:01 -0700550 switch ((pitch * bpp) / 8) {
551 case 8192:
552 case 512:
553 x |= 0x00;
554 break;
555 case 1024:
556 x |= 0x04;
557 break;
558 case 2048:
559 x |= 0x08;
560 break;
561 case 4096:
562 x |= 0x0C;
563 break;
564 }
565
566 fb_writew(x, par->io_virt + 0x2122);
567}
568
569static void tgui_fill_rect(struct tridentfb_par *par,
570 u32 x, u32 y, u32 w, u32 h, u32 c, u32 rop)
571{
572 t_outb(par, ROP_P, 0x2127);
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700573 writemmr(par, OLDCLR, c);
574 writemmr(par, DRAWFL, 0x4020);
575 writemmr(par, OLDDIM, point(w - 1, h - 1));
576 writemmr(par, OLDDST, point(x, y));
577 t_outb(par, 1, OLDCMD);
Krzysztof Heltbcac2d52008-07-23 21:31:01 -0700578}
579
580static void tgui_copy_rect(struct tridentfb_par *par,
581 u32 x1, u32 y1, u32 x2, u32 y2, u32 w, u32 h)
582{
583 int flags = 0;
584 u16 x1_tmp, x2_tmp, y1_tmp, y2_tmp;
585
586 if ((x1 < x2) && (y1 == y2)) {
587 flags |= 0x0200;
588 x1_tmp = x1 + w - 1;
589 x2_tmp = x2 + w - 1;
590 } else {
591 x1_tmp = x1;
592 x2_tmp = x2;
593 }
594
595 if (y1 < y2) {
596 flags |= 0x0100;
597 y1_tmp = y1 + h - 1;
598 y2_tmp = y2 + h - 1;
599 } else {
600 y1_tmp = y1;
601 y2_tmp = y2;
602 }
603
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700604 writemmr(par, DRAWFL, 0x4 | flags);
Krzysztof Heltbcac2d52008-07-23 21:31:01 -0700605 t_outb(par, ROP_S, 0x2127);
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700606 writemmr(par, OLDSRC, point(x1_tmp, y1_tmp));
607 writemmr(par, OLDDST, point(x2_tmp, y2_tmp));
608 writemmr(par, OLDDIM, point(w - 1, h - 1));
609 t_outb(par, 1, OLDCMD);
Krzysztof Heltbcac2d52008-07-23 21:31:01 -0700610}
611
612/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 * Accel functions called by the upper layers
614 */
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700615static void tridentfb_fillrect(struct fb_info *info,
616 const struct fb_fillrect *fr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700618 struct tridentfb_par *par = info->par;
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700619 int col;
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700620
Krzysztof Helt01a2d9e2008-07-23 21:31:04 -0700621 if (info->flags & FBINFO_HWACCEL_DISABLED) {
622 cfb_fillrect(info, fr);
623 return;
624 }
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700625 if (info->var.bits_per_pixel == 8) {
626 col = fr->color;
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700627 col |= col << 8;
628 col |= col << 16;
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700629 } else
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700630 col = ((u32 *)(info->pseudo_palette))[fr->color];
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700631
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700632 par->wait_engine(par);
Krzysztof Heltd9cad042008-07-23 21:30:54 -0700633 par->fill_rect(par, fr->dx, fr->dy, fr->width,
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700634 fr->height, col, fr->rop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700636
Krzysztof Helt0292be42008-07-23 21:31:08 -0700637static void tridentfb_imageblit(struct fb_info *info,
638 const struct fb_image *img)
639{
640 struct tridentfb_par *par = info->par;
641 int col, bgcol;
642
643 if ((info->flags & FBINFO_HWACCEL_DISABLED) || img->depth != 1) {
644 cfb_imageblit(info, img);
645 return;
646 }
647 if (info->var.bits_per_pixel == 8) {
648 col = img->fg_color;
649 col |= col << 8;
650 col |= col << 16;
651 bgcol = img->bg_color;
652 bgcol |= bgcol << 8;
653 bgcol |= bgcol << 16;
654 } else {
655 col = ((u32 *)(info->pseudo_palette))[img->fg_color];
656 bgcol = ((u32 *)(info->pseudo_palette))[img->bg_color];
657 }
658
659 par->wait_engine(par);
660 if (par->image_blit)
661 par->image_blit(par, img->data, img->dx, img->dy,
662 img->width, img->height, col, bgcol);
663 else
664 cfb_imageblit(info, img);
665}
666
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700667static void tridentfb_copyarea(struct fb_info *info,
668 const struct fb_copyarea *ca)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700670 struct tridentfb_par *par = info->par;
671
Krzysztof Helt01a2d9e2008-07-23 21:31:04 -0700672 if (info->flags & FBINFO_HWACCEL_DISABLED) {
673 cfb_copyarea(info, ca);
674 return;
675 }
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700676 par->wait_engine(par);
Krzysztof Heltd9cad042008-07-23 21:30:54 -0700677 par->copy_rect(par, ca->sx, ca->sy, ca->dx, ca->dy,
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700678 ca->width, ca->height);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700680
681static int tridentfb_sync(struct fb_info *info)
682{
683 struct tridentfb_par *par = info->par;
684
Krzysztof Helt01a2d9e2008-07-23 21:31:04 -0700685 if (!(info->flags & FBINFO_HWACCEL_DISABLED))
686 par->wait_engine(par);
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -0700687 return 0;
688}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690/*
691 * Hardware access functions
692 */
693
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700694static inline unsigned char read3X4(struct tridentfb_par *par, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700696 return vga_mm_rcrt(par->io_virt, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700699static inline void write3X4(struct tridentfb_par *par, int reg,
700 unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700702 vga_mm_wcrt(par->io_virt, reg, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700705static inline unsigned char read3CE(struct tridentfb_par *par,
706 unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700708 return vga_mm_rgfx(par->io_virt, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700711static inline void writeAttr(struct tridentfb_par *par, int reg,
712 unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700714 fb_readb(par->io_virt + VGA_IS1_RC); /* flip-flop to index */
715 vga_mm_wattr(par->io_virt, reg, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700718static inline void write3CE(struct tridentfb_par *par, int reg,
719 unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700721 vga_mm_wgfx(par->io_virt, reg, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
Krzysztof Helt13b0de42008-07-23 21:31:06 -0700724static void enable_mmio(struct tridentfb_par *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 /* Goto New Mode */
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700727 vga_io_rseq(0x0B);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 /* Unprotect registers */
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700730 vga_io_wseq(NewMode1, 0x80);
Krzysztof Helt13b0de42008-07-23 21:31:06 -0700731 if (!is_oldprotect(par->chip_id))
732 vga_io_wseq(Protection, 0x92);
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 /* Enable MMIO */
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700735 outb(PCIReg, 0x3D4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 outb(inb(0x3D5) | 0x01, 0x3D5);
Krzysztof Helte8ed8572008-03-04 14:28:39 -0800737}
738
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700739static void disable_mmio(struct tridentfb_par *par)
Krzysztof Helte8ed8572008-03-04 14:28:39 -0800740{
Krzysztof Helte8ed8572008-03-04 14:28:39 -0800741 /* Goto New Mode */
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700742 vga_mm_rseq(par->io_virt, 0x0B);
Krzysztof Helte8ed8572008-03-04 14:28:39 -0800743
744 /* Unprotect registers */
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700745 vga_mm_wseq(par->io_virt, NewMode1, 0x80);
Krzysztof Helt13b0de42008-07-23 21:31:06 -0700746 if (!is_oldprotect(par->chip_id))
747 vga_mm_wseq(par->io_virt, Protection, 0x92);
Krzysztof Helte8ed8572008-03-04 14:28:39 -0800748
749 /* Disable MMIO */
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700750 t_outb(par, PCIReg, 0x3D4);
751 t_outb(par, t_inb(par, 0x3D5) & ~0x01, 0x3D5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
Krzysztof Helt5cf13842008-07-23 21:31:05 -0700754static inline void crtc_unlock(struct tridentfb_par *par)
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700755{
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700756 write3X4(par, VGA_CRTC_V_SYNC_END,
757 read3X4(par, VGA_CRTC_V_SYNC_END) & 0x7F);
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700758}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760/* Return flat panel's maximum x resolution */
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800761static int get_nativex(struct tridentfb_par *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700763 int x, y, tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 if (nativex)
766 return nativex;
767
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700768 tmp = (read3CE(par, VertStretch) >> 4) & 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 switch (tmp) {
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700771 case 0:
772 x = 1280; y = 1024;
773 break;
774 case 2:
775 x = 1024; y = 768;
776 break;
777 case 3:
778 x = 800; y = 600;
779 break;
780 case 4:
781 x = 1400; y = 1050;
782 break;
783 case 1:
784 default:
785 x = 640; y = 480;
786 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
788
789 output("%dx%d flat panel found\n", x, y);
790 return x;
791}
792
793/* Set pitch */
Krzysztof Helt5cf13842008-07-23 21:31:05 -0700794static inline void set_lwidth(struct tridentfb_par *par, int width)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700796 write3X4(par, VGA_CRTC_OFFSET, width & 0xFF);
Ondrej Zary23aa4db2015-08-24 21:37:26 +0200797 /* chips older than TGUI9660 have only 1 width bit in AddColReg */
798 /* touching the other one breaks I2C/DDC */
799 if (par->chip_id == TGUI9440 || par->chip_id == CYBER9320)
800 write3X4(par, AddColReg,
801 (read3X4(par, AddColReg) & 0xEF) | ((width & 0x100) >> 4));
802 else
803 write3X4(par, AddColReg,
804 (read3X4(par, AddColReg) & 0xCF) | ((width & 0x300) >> 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
807/* For resolutions smaller than FP resolution stretch */
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700808static void screen_stretch(struct tridentfb_par *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Krzysztof Helt122e8ad2008-07-23 21:30:52 -0700810 if (par->chip_id != CYBERBLADEXPAi1)
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700811 write3CE(par, BiosReg, 0);
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700812 else
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700813 write3CE(par, BiosReg, 8);
814 write3CE(par, VertStretch, (read3CE(par, VertStretch) & 0x7C) | 1);
815 write3CE(par, HorStretch, (read3CE(par, HorStretch) & 0x7C) | 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
818/* For resolutions smaller than FP resolution center */
Krzysztof Helt5cf13842008-07-23 21:31:05 -0700819static inline void screen_center(struct tridentfb_par *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700821 write3CE(par, VertStretch, (read3CE(par, VertStretch) & 0x7C) | 0x80);
822 write3CE(par, HorStretch, (read3CE(par, HorStretch) & 0x7C) | 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
825/* Address of first shown pixel in display memory */
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700826static void set_screen_start(struct tridentfb_par *par, int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700828 u8 tmp;
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700829 write3X4(par, VGA_CRTC_START_LO, base & 0xFF);
830 write3X4(par, VGA_CRTC_START_HI, (base & 0xFF00) >> 8);
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700831 tmp = read3X4(par, CRTCModuleTest) & 0xDF;
832 write3X4(par, CRTCModuleTest, tmp | ((base & 0x10000) >> 11));
833 tmp = read3X4(par, CRTHiOrd) & 0xF8;
834 write3X4(par, CRTHiOrd, tmp | ((base & 0xE0000) >> 17));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837/* Set dotclock frequency */
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700838static void set_vclk(struct tridentfb_par *par, unsigned long freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700840 int m, n, k;
Krzysztof Helt6bdf1032008-07-23 21:30:56 -0700841 unsigned long fi, d, di;
842 unsigned char best_m = 0, best_n = 0, best_k = 0;
843 unsigned char hi, lo;
Krzysztof Helt6280fd42008-07-23 21:31:08 -0700844 unsigned char shift = !is_oldclock(par->chip_id) ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Krzysztof Helt3f275ea2008-05-12 14:02:11 -0700846 d = 20000;
Krzysztof Helt6280fd42008-07-23 21:31:08 -0700847 for (k = shift; k >= 0; k--)
848 for (m = 1; m < 32; m++) {
849 n = ((m + 2) << shift) - 8;
Krzysztof Helt34dec242008-07-23 21:31:04 -0700850 for (n = (n < 0 ? 0 : n); n < 122; n++) {
Krzysztof Helt3f275ea2008-05-12 14:02:11 -0700851 fi = ((14318l * (n + 8)) / (m + 2)) >> k;
Krzysztof Helt34dec242008-07-23 21:31:04 -0700852 di = abs(fi - freq);
Krzysztof Helt6280fd42008-07-23 21:31:08 -0700853 if (di < d || (di == d && k == best_k)) {
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700854 d = di;
Krzysztof Helt6bdf1032008-07-23 21:30:56 -0700855 best_n = n;
856 best_m = m;
857 best_k = k;
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700858 }
Krzysztof Helt3f275ea2008-05-12 14:02:11 -0700859 if (fi > freq)
860 break;
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700861 }
Krzysztof Helt34dec242008-07-23 21:31:04 -0700862 }
Krzysztof Helt6bdf1032008-07-23 21:30:56 -0700863
864 if (is_oldclock(par->chip_id)) {
865 lo = best_n | (best_m << 7);
866 hi = (best_m >> 1) | (best_k << 4);
867 } else {
868 lo = best_n;
869 hi = best_m | (best_k << 6);
870 }
871
Krzysztof Helt122e8ad2008-07-23 21:30:52 -0700872 if (is3Dchip(par->chip_id)) {
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700873 vga_mm_wseq(par->io_virt, ClockHigh, hi);
874 vga_mm_wseq(par->io_virt, ClockLow, lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 } else {
Krzysztof Heltc1724fe2008-07-23 21:30:56 -0700876 t_outb(par, lo, 0x43C8);
877 t_outb(par, hi, 0x43C9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700879 debug("VCLK = %X %X\n", hi, lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
882/* Set number of lines for flat panels*/
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700883static void set_number_of_lines(struct tridentfb_par *par, int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700885 int tmp = read3CE(par, CyberEnhance) & 0x8F;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (lines > 1024)
887 tmp |= 0x50;
888 else if (lines > 768)
889 tmp |= 0x30;
890 else if (lines > 600)
891 tmp |= 0x20;
892 else if (lines > 480)
893 tmp |= 0x10;
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700894 write3CE(par, CyberEnhance, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895}
896
897/*
898 * If we see that FP is active we assume we have one.
Krzysztof Helt6eed8e12008-07-23 21:30:53 -0700899 * Otherwise we have a CRT display. User can override.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 */
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800901static int is_flatpanel(struct tridentfb_par *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
903 if (fp)
Krzysztof Helt6eed8e12008-07-23 21:30:53 -0700904 return 1;
Krzysztof Helt122e8ad2008-07-23 21:30:52 -0700905 if (crt || !iscyber(par->chip_id))
Krzysztof Helt6eed8e12008-07-23 21:30:53 -0700906 return 0;
907 return (read3CE(par, FPConfig) & 0x10) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
910/* Try detecting the video memory size */
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -0800911static unsigned int get_memsize(struct tridentfb_par *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
913 unsigned char tmp, tmp2;
914 unsigned int k;
915
916 /* If memory size provided by user */
917 if (memsize)
918 k = memsize * Kb;
919 else
Krzysztof Helt122e8ad2008-07-23 21:30:52 -0700920 switch (par->chip_id) {
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700921 case CYBER9525DVD:
922 k = 2560 * Kb;
923 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 default:
Krzysztof Helt306fa6f62008-07-23 21:30:50 -0700925 tmp = read3X4(par, SPR) & 0x0F;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 switch (tmp) {
927
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700928 case 0x01:
Krzysztof Heltb614ce82008-03-10 11:43:37 -0700929 k = 512 * Kb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 break;
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700931 case 0x02:
932 k = 6 * Mb; /* XP */
933 break;
934 case 0x03:
935 k = 1 * Mb;
936 break;
937 case 0x04:
938 k = 8 * Mb;
939 break;
940 case 0x06:
941 k = 10 * Mb; /* XP */
942 break;
943 case 0x07:
944 k = 2 * Mb;
945 break;
946 case 0x08:
947 k = 12 * Mb; /* XP */
948 break;
949 case 0x0A:
950 k = 14 * Mb; /* XP */
951 break;
952 case 0x0C:
953 k = 16 * Mb; /* XP */
954 break;
955 case 0x0E: /* XP */
956
Krzysztof Helt10172ed2008-07-23 21:30:54 -0700957 tmp2 = vga_mm_rseq(par->io_virt, 0xC1);
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700958 switch (tmp2) {
959 case 0x00:
960 k = 20 * Mb;
961 break;
962 case 0x01:
963 k = 24 * Mb;
964 break;
965 case 0x10:
966 k = 28 * Mb;
967 break;
968 case 0x11:
969 k = 32 * Mb;
970 break;
971 default:
972 k = 1 * Mb;
973 break;
974 }
975 break;
976
977 case 0x0F:
978 k = 4 * Mb;
979 break;
980 default:
981 k = 1 * Mb;
982 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 k -= memdiff * Kb;
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700987 output("framebuffer size = %d Kb\n", k / Kb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return k;
989}
990
991/* See if we can handle the video mode described in var */
Krzysztof Helt245a2c22007-10-16 01:28:42 -0700992static int tridentfb_check_var(struct fb_var_screeninfo *var,
993 struct fb_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Krzysztof Helt6eed8e12008-07-23 21:30:53 -0700995 struct tridentfb_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 int bpp = var->bits_per_pixel;
Krzysztof Heltbcac2d52008-07-23 21:31:01 -0700997 int line_length;
Krzysztof Helt74a933f2008-07-23 21:31:00 -0700998 int ramdac = 230000; /* 230MHz for most 3D chips */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 debug("enter\n");
1000
1001 /* check color depth */
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001002 if (bpp == 24)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 bpp = var->bits_per_pixel = 32;
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -07001004 if (bpp != 8 && bpp != 16 && bpp != 32)
1005 return -EINVAL;
Krzysztof Helt54f019e2008-07-23 21:31:01 -07001006 if (par->chip_id == TGUI9440 && bpp == 32)
1007 return -EINVAL;
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001008 /* check whether resolution fits on panel and in memory */
Krzysztof Helt6eed8e12008-07-23 21:30:53 -07001009 if (par->flatpanel && nativex && var->xres > nativex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return -EINVAL;
Krzysztof Helt74a933f2008-07-23 21:31:00 -07001011 /* various resolution checks */
1012 var->xres = (var->xres + 7) & ~0x7;
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -07001013 if (var->xres > var->xres_virtual)
Krzysztof Helt74a933f2008-07-23 21:31:00 -07001014 var->xres_virtual = var->xres;
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -07001015 if (var->yres > var->yres_virtual)
1016 var->yres_virtual = var->yres;
1017 if (var->xres_virtual > 4095 || var->yres > 2048)
1018 return -EINVAL;
1019 /* prevent from position overflow for acceleration */
1020 if (var->yres_virtual > 0xffff)
1021 return -EINVAL;
Krzysztof Heltbcac2d52008-07-23 21:31:01 -07001022 line_length = var->xres_virtual * bpp / 8;
Krzysztof Helt01a2d9e2008-07-23 21:31:04 -07001023
1024 if (!is3Dchip(par->chip_id) &&
1025 !(info->flags & FBINFO_HWACCEL_DISABLED)) {
Krzysztof Heltbcac2d52008-07-23 21:31:01 -07001026 /* acceleration requires line length to be power of 2 */
1027 if (line_length <= 512)
1028 var->xres_virtual = 512 * 8 / bpp;
1029 else if (line_length <= 1024)
1030 var->xres_virtual = 1024 * 8 / bpp;
1031 else if (line_length <= 2048)
1032 var->xres_virtual = 2048 * 8 / bpp;
1033 else if (line_length <= 4096)
1034 var->xres_virtual = 4096 * 8 / bpp;
1035 else if (line_length <= 8192)
1036 var->xres_virtual = 8192 * 8 / bpp;
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -07001037 else
1038 return -EINVAL;
Krzysztof Heltbcac2d52008-07-23 21:31:01 -07001039
1040 line_length = var->xres_virtual * bpp / 8;
1041 }
Krzysztof Helt01a2d9e2008-07-23 21:31:04 -07001042
Krzysztof Heltf330c4b2008-07-23 21:31:07 -07001043 /* datasheet specifies how to set panning only up to 4 MB */
1044 if (line_length * (var->yres_virtual - var->yres) > (4 << 20))
1045 var->yres_virtual = ((4 << 20) / line_length) + var->yres;
1046
Krzysztof Heltbcac2d52008-07-23 21:31:01 -07001047 if (line_length * var->yres_virtual > info->fix.smem_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 return -EINVAL;
1049
1050 switch (bpp) {
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001051 case 8:
1052 var->red.offset = 0;
Krzysztof Helta4af1792008-07-23 21:31:06 -07001053 var->red.length = 8;
1054 var->green = var->red;
1055 var->blue = var->red;
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001056 break;
1057 case 16:
1058 var->red.offset = 11;
1059 var->green.offset = 5;
1060 var->blue.offset = 0;
1061 var->red.length = 5;
1062 var->green.length = 6;
1063 var->blue.length = 5;
1064 break;
1065 case 32:
1066 var->red.offset = 16;
1067 var->green.offset = 8;
1068 var->blue.offset = 0;
1069 var->red.length = 8;
1070 var->green.length = 8;
1071 var->blue.length = 8;
1072 break;
1073 default:
1074 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
Krzysztof Helt74a933f2008-07-23 21:31:00 -07001076
1077 if (is_xp(par->chip_id))
1078 ramdac = 350000;
1079
1080 switch (par->chip_id) {
1081 case TGUI9440:
Krzysztof Helt54f019e2008-07-23 21:31:01 -07001082 ramdac = (bpp >= 16) ? 45000 : 90000;
Krzysztof Helt74a933f2008-07-23 21:31:00 -07001083 break;
1084 case CYBER9320:
1085 case TGUI9660:
1086 ramdac = 135000;
1087 break;
1088 case PROVIDIA9685:
1089 case CYBER9388:
1090 case CYBER9382:
1091 case CYBER9385:
1092 ramdac = 170000;
1093 break;
1094 }
1095
1096 /* The clock is doubled for 32 bpp */
1097 if (bpp == 32)
1098 ramdac /= 2;
1099
1100 if (PICOS2KHZ(var->pixclock) > ramdac)
1101 return -EINVAL;
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 debug("exit\n");
1104
1105 return 0;
1106
1107}
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109/* Pan the display */
1110static int tridentfb_pan_display(struct fb_var_screeninfo *var,
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001111 struct fb_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
Krzysztof Helt306fa6f62008-07-23 21:30:50 -07001113 struct tridentfb_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 unsigned int offset;
1115
1116 debug("enter\n");
Laurent Pinchart4541dbe2011-05-25 11:34:52 +02001117 offset = (var->xoffset + (var->yoffset * info->var.xres_virtual))
1118 * info->var.bits_per_pixel / 32;
Krzysztof Helt306fa6f62008-07-23 21:30:50 -07001119 set_screen_start(par, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 debug("exit\n");
1121 return 0;
1122}
1123
Krzysztof Helt5cf13842008-07-23 21:31:05 -07001124static inline void shadowmode_on(struct tridentfb_par *par)
Krzysztof Helt306fa6f62008-07-23 21:30:50 -07001125{
1126 write3CE(par, CyberControl, read3CE(par, CyberControl) | 0x81);
1127}
1128
Krzysztof Helt5cf13842008-07-23 21:31:05 -07001129static inline void shadowmode_off(struct tridentfb_par *par)
Krzysztof Helt306fa6f62008-07-23 21:30:50 -07001130{
1131 write3CE(par, CyberControl, read3CE(par, CyberControl) & 0x7E);
1132}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134/* Set the hardware to the requested video mode */
1135static int tridentfb_set_par(struct fb_info *info)
1136{
Krzysztof Helt5cf13842008-07-23 21:31:05 -07001137 struct tridentfb_par *par = info->par;
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001138 u32 htotal, hdispend, hsyncstart, hsyncend, hblankstart, hblankend;
1139 u32 vtotal, vdispend, vsyncstart, vsyncend, vblankstart, vblankend;
1140 struct fb_var_screeninfo *var = &info->var;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 int bpp = var->bits_per_pixel;
1142 unsigned char tmp;
Krzysztof Helt3f275ea2008-05-12 14:02:11 -07001143 unsigned long vclk;
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 debug("enter\n");
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001146 hdispend = var->xres / 8 - 1;
Krzysztof Helt34dec242008-07-23 21:31:04 -07001147 hsyncstart = (var->xres + var->right_margin) / 8;
1148 hsyncend = (var->xres + var->right_margin + var->hsync_len) / 8;
Krzysztof Helt7f762d22008-07-23 21:30:55 -07001149 htotal = (var->xres + var->left_margin + var->right_margin +
1150 var->hsync_len) / 8 - 5;
Krzysztof Helt0e73a472008-07-23 21:30:58 -07001151 hblankstart = hdispend + 1;
Krzysztof Helt7f762d22008-07-23 21:30:55 -07001152 hblankend = htotal + 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 vdispend = var->yres - 1;
1155 vsyncstart = var->yres + var->lower_margin;
Krzysztof Helt7f762d22008-07-23 21:30:55 -07001156 vsyncend = vsyncstart + var->vsync_len;
1157 vtotal = var->upper_margin + vsyncend - 2;
Krzysztof Helt0e73a472008-07-23 21:30:58 -07001158 vblankstart = vdispend + 1;
Krzysztof Helt7f762d22008-07-23 21:30:55 -07001159 vblankend = vtotal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Krzysztof Helt34dec242008-07-23 21:31:04 -07001161 if (info->var.vmode & FB_VMODE_INTERLACED) {
1162 vtotal /= 2;
1163 vdispend /= 2;
1164 vsyncstart /= 2;
1165 vsyncend /= 2;
1166 vblankstart /= 2;
1167 vblankend /= 2;
1168 }
1169
Krzysztof Helt13b0de42008-07-23 21:31:06 -07001170 enable_mmio(par);
Krzysztof Helt306fa6f62008-07-23 21:30:50 -07001171 crtc_unlock(par);
1172 write3CE(par, CyberControl, 8);
Krzysztof Helt34dec242008-07-23 21:31:04 -07001173 tmp = 0xEB;
1174 if (var->sync & FB_SYNC_HOR_HIGH_ACT)
1175 tmp &= ~0x40;
1176 if (var->sync & FB_SYNC_VERT_HIGH_ACT)
1177 tmp &= ~0x80;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Krzysztof Helt6eed8e12008-07-23 21:30:53 -07001179 if (par->flatpanel && var->xres < nativex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 /*
1181 * on flat panels with native size larger
1182 * than requested resolution decide whether
1183 * we stretch or center
1184 */
Krzysztof Helt34dec242008-07-23 21:31:04 -07001185 t_outb(par, tmp | 0xC0, VGA_MIS_W);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Krzysztof Helt306fa6f62008-07-23 21:30:50 -07001187 shadowmode_on(par);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001189 if (center)
Krzysztof Helt306fa6f62008-07-23 21:30:50 -07001190 screen_center(par);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 else if (stretch)
Krzysztof Helt306fa6f62008-07-23 21:30:50 -07001192 screen_stretch(par);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 } else {
Krzysztof Helt34dec242008-07-23 21:31:04 -07001195 t_outb(par, tmp, VGA_MIS_W);
Krzysztof Helt306fa6f62008-07-23 21:30:50 -07001196 write3CE(par, CyberControl, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
1198
1199 /* vertical timing values */
Krzysztof Helt10172ed2008-07-23 21:30:54 -07001200 write3X4(par, VGA_CRTC_V_TOTAL, vtotal & 0xFF);
1201 write3X4(par, VGA_CRTC_V_DISP_END, vdispend & 0xFF);
1202 write3X4(par, VGA_CRTC_V_SYNC_START, vsyncstart & 0xFF);
1203 write3X4(par, VGA_CRTC_V_SYNC_END, (vsyncend & 0x0F));
1204 write3X4(par, VGA_CRTC_V_BLANK_START, vblankstart & 0xFF);
Krzysztof Helt7f762d22008-07-23 21:30:55 -07001205 write3X4(par, VGA_CRTC_V_BLANK_END, vblankend & 0xFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 /* horizontal timing values */
Krzysztof Helt10172ed2008-07-23 21:30:54 -07001208 write3X4(par, VGA_CRTC_H_TOTAL, htotal & 0xFF);
1209 write3X4(par, VGA_CRTC_H_DISP, hdispend & 0xFF);
1210 write3X4(par, VGA_CRTC_H_SYNC_START, hsyncstart & 0xFF);
1211 write3X4(par, VGA_CRTC_H_SYNC_END,
Krzysztof Helt306fa6f62008-07-23 21:30:50 -07001212 (hsyncend & 0x1F) | ((hblankend & 0x20) << 2));
Krzysztof Helt10172ed2008-07-23 21:30:54 -07001213 write3X4(par, VGA_CRTC_H_BLANK_START, hblankstart & 0xFF);
Krzysztof Helt7f762d22008-07-23 21:30:55 -07001214 write3X4(par, VGA_CRTC_H_BLANK_END, hblankend & 0x1F);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 /* higher bits of vertical timing values */
1217 tmp = 0x10;
1218 if (vtotal & 0x100) tmp |= 0x01;
1219 if (vdispend & 0x100) tmp |= 0x02;
1220 if (vsyncstart & 0x100) tmp |= 0x04;
1221 if (vblankstart & 0x100) tmp |= 0x08;
1222
1223 if (vtotal & 0x200) tmp |= 0x20;
1224 if (vdispend & 0x200) tmp |= 0x40;
1225 if (vsyncstart & 0x200) tmp |= 0x80;
Krzysztof Helt10172ed2008-07-23 21:30:54 -07001226 write3X4(par, VGA_CRTC_OVERFLOW, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Krzysztof Helt7f762d22008-07-23 21:30:55 -07001228 tmp = read3X4(par, CRTHiOrd) & 0x07;
1229 tmp |= 0x08; /* line compare bit 10 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if (vtotal & 0x400) tmp |= 0x80;
1231 if (vblankstart & 0x400) tmp |= 0x40;
1232 if (vsyncstart & 0x400) tmp |= 0x20;
1233 if (vdispend & 0x400) tmp |= 0x10;
Krzysztof Helt306fa6f62008-07-23 21:30:50 -07001234 write3X4(par, CRTHiOrd, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Krzysztof Helt7f762d22008-07-23 21:30:55 -07001236 tmp = (htotal >> 8) & 0x01;
1237 tmp |= (hdispend >> 7) & 0x02;
1238 tmp |= (hsyncstart >> 5) & 0x08;
1239 tmp |= (hblankstart >> 4) & 0x10;
Krzysztof Helt306fa6f62008-07-23 21:30:50 -07001240 write3X4(par, HorizOverflow, tmp);
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 tmp = 0x40;
1243 if (vblankstart & 0x200) tmp |= 0x20;
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001244//FIXME if (info->var.vmode & FB_VMODE_DOUBLE) tmp |= 0x80; /* double scan for 200 line modes */
Krzysztof Helt10172ed2008-07-23 21:30:54 -07001245 write3X4(par, VGA_CRTC_MAX_SCAN, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Krzysztof Helt10172ed2008-07-23 21:30:54 -07001247 write3X4(par, VGA_CRTC_LINE_COMPARE, 0xFF);
1248 write3X4(par, VGA_CRTC_PRESET_ROW, 0);
1249 write3X4(par, VGA_CRTC_MODE, 0xC3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Krzysztof Helt306fa6f62008-07-23 21:30:50 -07001251 write3X4(par, LinearAddReg, 0x20); /* enable linear addressing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001253 tmp = (info->var.vmode & FB_VMODE_INTERLACED) ? 0x84 : 0x80;
Krzysztof Helt306fa6f62008-07-23 21:30:50 -07001254 /* enable access extended memory */
1255 write3X4(par, CRTCModuleTest, tmp);
Krzysztof Helt34dec242008-07-23 21:31:04 -07001256 tmp = read3CE(par, MiscIntContReg) & ~0x4;
1257 if (info->var.vmode & FB_VMODE_INTERLACED)
1258 tmp |= 0x4;
1259 write3CE(par, MiscIntContReg, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Krzysztof Helt306fa6f62008-07-23 21:30:50 -07001261 /* enable GE for text acceleration */
1262 write3X4(par, GraphEngReg, 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 switch (bpp) {
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001265 case 8:
1266 tmp = 0x00;
1267 break;
1268 case 16:
1269 tmp = 0x05;
1270 break;
1271 case 24:
1272 tmp = 0x29;
1273 break;
1274 case 32:
1275 tmp = 0x09;
1276 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278
Krzysztof Helt306fa6f62008-07-23 21:30:50 -07001279 write3X4(par, PixelBusReg, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Krzysztof Helt0e73a472008-07-23 21:30:58 -07001281 tmp = read3X4(par, DRAMControl);
1282 if (!is_oldprotect(par->chip_id))
1283 tmp |= 0x10;
Krzysztof Helt122e8ad2008-07-23 21:30:52 -07001284 if (iscyber(par->chip_id))
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001285 tmp |= 0x20;
Krzysztof Helt306fa6f62008-07-23 21:30:50 -07001286 write3X4(par, DRAMControl, tmp); /* both IO, linear enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Krzysztof Helt306fa6f62008-07-23 21:30:50 -07001288 write3X4(par, InterfaceSel, read3X4(par, InterfaceSel) | 0x40);
Krzysztof Helt0e73a472008-07-23 21:30:58 -07001289 if (!is_xp(par->chip_id))
1290 write3X4(par, Performance, read3X4(par, Performance) | 0x10);
Krzysztof Helt306fa6f62008-07-23 21:30:50 -07001291 /* MMIO & PCI read and write burst enable */
Krzysztof Helt13b0de42008-07-23 21:31:06 -07001292 if (par->chip_id != TGUI9440 && par->chip_id != IMAGE975)
Krzysztof Helta0d92252008-07-23 21:30:58 -07001293 write3X4(par, PCIReg, read3X4(par, PCIReg) | 0x06);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Krzysztof Helt10172ed2008-07-23 21:30:54 -07001295 vga_mm_wseq(par->io_virt, 0, 3);
1296 vga_mm_wseq(par->io_virt, 1, 1); /* set char clock 8 dots wide */
Krzysztof Helt306fa6f62008-07-23 21:30:50 -07001297 /* enable 4 maps because needed in chain4 mode */
Krzysztof Helt10172ed2008-07-23 21:30:54 -07001298 vga_mm_wseq(par->io_virt, 2, 0x0F);
1299 vga_mm_wseq(par->io_virt, 3, 0);
1300 vga_mm_wseq(par->io_virt, 4, 0x0E); /* memory mode enable bitmaps ?? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Krzysztof Helt54f019e2008-07-23 21:31:01 -07001302 /* convert from picoseconds to kHz */
1303 vclk = PICOS2KHZ(info->var.pixclock);
1304
Krzysztof Helt306fa6f62008-07-23 21:30:50 -07001305 /* divide clock by 2 if 32bpp chain4 mode display and CPU path */
Krzysztof Helt65e93e02008-07-23 21:31:00 -07001306 tmp = read3CE(par, MiscExtFunc) & 0xF0;
Krzysztof Helt54f019e2008-07-23 21:31:01 -07001307 if (bpp == 32 || (par->chip_id == TGUI9440 && bpp == 16)) {
Krzysztof Helt65e93e02008-07-23 21:31:00 -07001308 tmp |= 8;
Krzysztof Helt54f019e2008-07-23 21:31:01 -07001309 vclk *= 2;
1310 }
1311 set_vclk(par, vclk);
Krzysztof Helt65e93e02008-07-23 21:31:00 -07001312 write3CE(par, MiscExtFunc, tmp | 0x12);
Krzysztof Helt306fa6f62008-07-23 21:30:50 -07001313 write3CE(par, 0x5, 0x40); /* no CGA compat, allow 256 col */
1314 write3CE(par, 0x6, 0x05); /* graphics mode */
1315 write3CE(par, 0x7, 0x0F); /* planes? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Krzysztof Helt306fa6f62008-07-23 21:30:50 -07001317 /* graphics mode and support 256 color modes */
1318 writeAttr(par, 0x10, 0x41);
1319 writeAttr(par, 0x12, 0x0F); /* planes */
1320 writeAttr(par, 0x13, 0); /* horizontal pel panning */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001322 /* colors */
1323 for (tmp = 0; tmp < 0x10; tmp++)
Krzysztof Helt306fa6f62008-07-23 21:30:50 -07001324 writeAttr(par, tmp, tmp);
Krzysztof Helt10172ed2008-07-23 21:30:54 -07001325 fb_readb(par->io_virt + VGA_IS1_RC); /* flip-flop to index */
1326 t_outb(par, 0x20, VGA_ATT_W); /* enable attr */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 switch (bpp) {
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001329 case 8:
1330 tmp = 0;
1331 break;
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001332 case 16:
1333 tmp = 0x30;
1334 break;
1335 case 24:
1336 case 32:
1337 tmp = 0xD0;
1338 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 }
1340
Krzysztof Helt10172ed2008-07-23 21:30:54 -07001341 t_inb(par, VGA_PEL_IW);
1342 t_inb(par, VGA_PEL_MSK);
1343 t_inb(par, VGA_PEL_MSK);
1344 t_inb(par, VGA_PEL_MSK);
1345 t_inb(par, VGA_PEL_MSK);
1346 t_outb(par, tmp, VGA_PEL_MSK);
1347 t_inb(par, VGA_PEL_IW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Krzysztof Helt6eed8e12008-07-23 21:30:53 -07001349 if (par->flatpanel)
Krzysztof Helt306fa6f62008-07-23 21:30:50 -07001350 set_number_of_lines(par, info->var.yres);
Krzysztof Heltbcac2d52008-07-23 21:31:01 -07001351 info->fix.line_length = info->var.xres_virtual * bpp / 8;
1352 set_lwidth(par, info->fix.line_length / 8);
Krzysztof Helt01a2d9e2008-07-23 21:31:04 -07001353
1354 if (!(info->flags & FBINFO_HWACCEL_DISABLED))
1355 par->init_accel(par, info->var.xres_virtual, bpp);
Krzysztof Helt2c86a0c2008-07-23 21:31:03 -07001356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 info->fix.visual = (bpp == 8) ? FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001358 info->cmap.len = (bpp == 8) ? 256 : 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 debug("exit\n");
1360 return 0;
1361}
1362
1363/* Set one color register */
1364static int tridentfb_setcolreg(unsigned regno, unsigned red, unsigned green,
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001365 unsigned blue, unsigned transp,
1366 struct fb_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
1368 int bpp = info->var.bits_per_pixel;
Krzysztof Helt306fa6f62008-07-23 21:30:50 -07001369 struct tridentfb_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 if (regno >= info->cmap.len)
1372 return 1;
1373
Antonino A. Daplas973d9ab2007-07-17 04:05:41 -07001374 if (bpp == 8) {
Krzysztof Helt10172ed2008-07-23 21:30:54 -07001375 t_outb(par, 0xFF, VGA_PEL_MSK);
1376 t_outb(par, regno, VGA_PEL_IW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Krzysztof Helt10172ed2008-07-23 21:30:54 -07001378 t_outb(par, red >> 10, VGA_PEL_D);
1379 t_outb(par, green >> 10, VGA_PEL_D);
1380 t_outb(par, blue >> 10, VGA_PEL_D);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Antonino A. Daplas973d9ab2007-07-17 04:05:41 -07001382 } else if (regno < 16) {
1383 if (bpp == 16) { /* RGB 565 */
1384 u32 col;
Antonino A. Daplas8dad46c2005-08-01 23:46:44 +08001385
Antonino A. Daplas973d9ab2007-07-17 04:05:41 -07001386 col = (red & 0xF800) | ((green & 0xFC00) >> 5) |
1387 ((blue & 0xF800) >> 11);
1388 col |= col << 16;
1389 ((u32 *)(info->pseudo_palette))[regno] = col;
1390 } else if (bpp == 32) /* ARGB 8888 */
Krzysztof Helt5cf13842008-07-23 21:31:05 -07001391 ((u32 *)info->pseudo_palette)[regno] =
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001392 ((transp & 0xFF00) << 16) |
1393 ((red & 0xFF00) << 8) |
Antonino A. Daplas973d9ab2007-07-17 04:05:41 -07001394 ((green & 0xFF00)) |
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001395 ((blue & 0xFF00) >> 8);
Antonino A. Daplas973d9ab2007-07-17 04:05:41 -07001396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 return 0;
1399}
1400
Krzysztof Helt5cf13842008-07-23 21:31:05 -07001401/* Try blanking the screen. For flat panels it does nothing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402static int tridentfb_blank(int blank_mode, struct fb_info *info)
1403{
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001404 unsigned char PMCont, DPMSCont;
Krzysztof Helt306fa6f62008-07-23 21:30:50 -07001405 struct tridentfb_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 debug("enter\n");
Krzysztof Helt6eed8e12008-07-23 21:30:53 -07001408 if (par->flatpanel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return 0;
Krzysztof Helt306fa6f62008-07-23 21:30:50 -07001410 t_outb(par, 0x04, 0x83C8); /* Read DPMS Control */
1411 PMCont = t_inb(par, 0x83C6) & 0xFC;
1412 DPMSCont = read3CE(par, PowerStatus) & 0xFC;
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001413 switch (blank_mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 case FB_BLANK_UNBLANK:
1415 /* Screen: On, HSync: On, VSync: On */
1416 case FB_BLANK_NORMAL:
1417 /* Screen: Off, HSync: On, VSync: On */
1418 PMCont |= 0x03;
1419 DPMSCont |= 0x00;
1420 break;
1421 case FB_BLANK_HSYNC_SUSPEND:
1422 /* Screen: Off, HSync: Off, VSync: On */
1423 PMCont |= 0x02;
1424 DPMSCont |= 0x01;
1425 break;
1426 case FB_BLANK_VSYNC_SUSPEND:
1427 /* Screen: Off, HSync: On, VSync: Off */
1428 PMCont |= 0x02;
1429 DPMSCont |= 0x02;
1430 break;
1431 case FB_BLANK_POWERDOWN:
1432 /* Screen: Off, HSync: Off, VSync: Off */
1433 PMCont |= 0x00;
1434 DPMSCont |= 0x03;
1435 break;
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Krzysztof Helt306fa6f62008-07-23 21:30:50 -07001438 write3CE(par, PowerStatus, DPMSCont);
1439 t_outb(par, 4, 0x83C8);
1440 t_outb(par, PMCont, 0x83C6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 debug("exit\n");
1443
1444 /* let fbcon do a softblank for us */
1445 return (blank_mode == FB_BLANK_NORMAL) ? 1 : 0;
1446}
1447
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001448static struct fb_ops tridentfb_ops = {
1449 .owner = THIS_MODULE,
1450 .fb_setcolreg = tridentfb_setcolreg,
1451 .fb_pan_display = tridentfb_pan_display,
1452 .fb_blank = tridentfb_blank,
1453 .fb_check_var = tridentfb_check_var,
1454 .fb_set_par = tridentfb_set_par,
1455 .fb_fillrect = tridentfb_fillrect,
1456 .fb_copyarea = tridentfb_copyarea,
Krzysztof Helt0292be42008-07-23 21:31:08 -07001457 .fb_imageblit = tridentfb_imageblit,
Krzysztof Helt49b1f4b42008-07-23 21:31:02 -07001458 .fb_sync = tridentfb_sync,
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001459};
1460
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08001461static int trident_pci_probe(struct pci_dev *dev,
1462 const struct pci_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
1464 int err;
1465 unsigned char revision;
Krzysztof Helte09ed092008-07-23 21:30:51 -07001466 struct fb_info *info;
1467 struct tridentfb_par *default_par;
Krzysztof Helt122e8ad2008-07-23 21:30:52 -07001468 int chip3D;
1469 int chip_id;
Ondrej Zary6a5e3bd2015-09-25 00:14:14 +02001470 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 err = pci_enable_device(dev);
1473 if (err)
1474 return err;
1475
Krzysztof Helte09ed092008-07-23 21:30:51 -07001476 info = framebuffer_alloc(sizeof(struct tridentfb_par), &dev->dev);
1477 if (!info)
1478 return -ENOMEM;
1479 default_par = info->par;
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 chip_id = id->device;
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 /* If PCI id is 0x9660 then further detect chip type */
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 if (chip_id == TGUI9660) {
Krzysztof Helt10172ed2008-07-23 21:30:54 -07001486 revision = vga_io_rseq(RevisionID);
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 switch (revision) {
Krzysztof Helt0e73a472008-07-23 21:30:58 -07001489 case 0x21:
1490 chip_id = PROVIDIA9685;
1491 break;
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001492 case 0x22:
1493 case 0x23:
1494 chip_id = CYBER9397;
1495 break;
1496 case 0x2A:
1497 chip_id = CYBER9397DVD;
1498 break;
1499 case 0x30:
1500 case 0x33:
1501 case 0x34:
1502 case 0x35:
1503 case 0x38:
1504 case 0x3A:
1505 case 0xB3:
1506 chip_id = CYBER9385;
1507 break;
1508 case 0x40 ... 0x43:
1509 chip_id = CYBER9382;
1510 break;
1511 case 0x4A:
1512 chip_id = CYBER9388;
1513 break;
1514 default:
1515 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 }
1517 }
1518
1519 chip3D = is3Dchip(chip_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 if (is_xp(chip_id)) {
Krzysztof Heltd9cad042008-07-23 21:30:54 -07001522 default_par->init_accel = xp_init_accel;
1523 default_par->wait_engine = xp_wait_engine;
1524 default_par->fill_rect = xp_fill_rect;
1525 default_par->copy_rect = xp_copy_rect;
Krzysztof Helt01a2d9e2008-07-23 21:31:04 -07001526 tridentfb_fix.accel = FB_ACCEL_TRIDENT_BLADEXP;
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001527 } else if (is_blade(chip_id)) {
Krzysztof Heltd9cad042008-07-23 21:30:54 -07001528 default_par->init_accel = blade_init_accel;
1529 default_par->wait_engine = blade_wait_engine;
1530 default_par->fill_rect = blade_fill_rect;
1531 default_par->copy_rect = blade_copy_rect;
Krzysztof Helt0292be42008-07-23 21:31:08 -07001532 default_par->image_blit = blade_image_blit;
Krzysztof Helt01a2d9e2008-07-23 21:31:04 -07001533 tridentfb_fix.accel = FB_ACCEL_TRIDENT_BLADE3D;
Krzysztof Heltbcac2d52008-07-23 21:31:01 -07001534 } else if (chip3D) { /* 3DImage family left */
Krzysztof Heltd9cad042008-07-23 21:30:54 -07001535 default_par->init_accel = image_init_accel;
1536 default_par->wait_engine = image_wait_engine;
1537 default_par->fill_rect = image_fill_rect;
1538 default_par->copy_rect = image_copy_rect;
Krzysztof Helt01a2d9e2008-07-23 21:31:04 -07001539 tridentfb_fix.accel = FB_ACCEL_TRIDENT_3DIMAGE;
Krzysztof Heltbcac2d52008-07-23 21:31:01 -07001540 } else { /* TGUI 9440/96XX family */
1541 default_par->init_accel = tgui_init_accel;
1542 default_par->wait_engine = xp_wait_engine;
1543 default_par->fill_rect = tgui_fill_rect;
1544 default_par->copy_rect = tgui_copy_rect;
Krzysztof Helt01a2d9e2008-07-23 21:31:04 -07001545 tridentfb_fix.accel = FB_ACCEL_TRIDENT_TGUI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
1547
Krzysztof Helt122e8ad2008-07-23 21:30:52 -07001548 default_par->chip_id = chip_id;
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 /* setup MMIO region */
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001551 tridentfb_fix.mmio_start = pci_resource_start(dev, 1);
Krzysztof Helt5cf13842008-07-23 21:31:05 -07001552 tridentfb_fix.mmio_len = pci_resource_len(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Krzysztof Helt5cf13842008-07-23 21:31:05 -07001554 if (!request_mem_region(tridentfb_fix.mmio_start,
1555 tridentfb_fix.mmio_len, "tridentfb")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 debug("request_region failed!\n");
Krzysztof Helt3876ae82008-07-23 21:30:57 -07001557 framebuffer_release(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 return -1;
1559 }
1560
Krzysztof Helte09ed092008-07-23 21:30:51 -07001561 default_par->io_virt = ioremap_nocache(tridentfb_fix.mmio_start,
1562 tridentfb_fix.mmio_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Krzysztof Helte09ed092008-07-23 21:30:51 -07001564 if (!default_par->io_virt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 debug("ioremap failed\n");
Krzysztof Helte8ed8572008-03-04 14:28:39 -08001566 err = -1;
1567 goto out_unmap1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
1569
Krzysztof Helt13b0de42008-07-23 21:31:06 -07001570 enable_mmio(default_par);
Krzysztof Heltbcac2d52008-07-23 21:31:01 -07001571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 /* setup framebuffer memory */
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001573 tridentfb_fix.smem_start = pci_resource_start(dev, 0);
Krzysztof Helte09ed092008-07-23 21:30:51 -07001574 tridentfb_fix.smem_len = get_memsize(default_par);
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001575
Krzysztof Helt5cf13842008-07-23 21:31:05 -07001576 if (!request_mem_region(tridentfb_fix.smem_start,
1577 tridentfb_fix.smem_len, "tridentfb")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 debug("request_mem_region failed!\n");
Krzysztof Helte09ed092008-07-23 21:30:51 -07001579 disable_mmio(info->par);
Amol Lada02f6402006-12-08 02:40:03 -08001580 err = -1;
Krzysztof Helte8ed8572008-03-04 14:28:39 -08001581 goto out_unmap1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 }
1583
Krzysztof Helte09ed092008-07-23 21:30:51 -07001584 info->screen_base = ioremap_nocache(tridentfb_fix.smem_start,
1585 tridentfb_fix.smem_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Krzysztof Helte09ed092008-07-23 21:30:51 -07001587 if (!info->screen_base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 debug("ioremap failed\n");
Amol Lada02f6402006-12-08 02:40:03 -08001589 err = -1;
Krzysztof Helte8ed8572008-03-04 14:28:39 -08001590 goto out_unmap2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 }
1592
Krzysztof Helt6eed8e12008-07-23 21:30:53 -07001593 default_par->flatpanel = is_flatpanel(default_par);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Krzysztof Helt6eed8e12008-07-23 21:30:53 -07001595 if (default_par->flatpanel)
Krzysztof Helte09ed092008-07-23 21:30:51 -07001596 nativex = get_nativex(default_par);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Krzysztof Helte09ed092008-07-23 21:30:51 -07001598 info->fix = tridentfb_fix;
1599 info->fbops = &tridentfb_ops;
Krzysztof Heltaa0aa8a2008-07-23 21:30:59 -07001600 info->pseudo_palette = default_par->pseudo_pal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Krzysztof Helte09ed092008-07-23 21:30:51 -07001602 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
Krzysztof Helt01a2d9e2008-07-23 21:31:04 -07001603 if (!noaccel && default_par->init_accel) {
1604 info->flags &= ~FBINFO_HWACCEL_DISABLED;
1605 info->flags |= FBINFO_HWACCEL_COPYAREA;
1606 info->flags |= FBINFO_HWACCEL_FILLRECT;
1607 } else
1608 info->flags |= FBINFO_HWACCEL_DISABLED;
1609
Krzysztof Heltddb53d42009-03-31 15:25:40 -07001610 if (is_blade(chip_id) && chip_id != BLADE3D)
1611 info->flags |= FBINFO_READS_FAST;
1612
Krzysztof Helt0292be42008-07-23 21:31:08 -07001613 info->pixmap.addr = kmalloc(4096, GFP_KERNEL);
1614 if (!info->pixmap.addr) {
1615 err = -ENOMEM;
1616 goto out_unmap2;
1617 }
1618
1619 info->pixmap.size = 4096;
1620 info->pixmap.buf_align = 4;
1621 info->pixmap.scan_align = 1;
1622 info->pixmap.access_align = 32;
1623 info->pixmap.flags = FB_PIXMAP_SYSTEM;
Ondrej Zary6a5e3bd2015-09-25 00:14:14 +02001624 info->var.bits_per_pixel = 8;
Krzysztof Helt0292be42008-07-23 21:31:08 -07001625
1626 if (default_par->image_blit) {
1627 info->flags |= FBINFO_HWACCEL_IMAGEBLIT;
1628 info->pixmap.scan_align = 4;
1629 }
1630
1631 if (noaccel) {
1632 printk(KERN_DEBUG "disabling acceleration\n");
1633 info->flags |= FBINFO_HWACCEL_DISABLED;
1634 info->pixmap.scan_align = 1;
1635 }
1636
Ondrej Zary6a5e3bd2015-09-25 00:14:14 +02001637 if (tridentfb_setup_ddc_bus(info) == 0) {
1638 u8 *edid = fb_ddc_read(&default_par->ddc_adapter);
1639
1640 default_par->ddc_registered = true;
1641 if (edid) {
1642 fb_edid_to_monspecs(edid, &info->monspecs);
1643 kfree(edid);
1644 if (!info->monspecs.modedb)
1645 dev_err(info->device, "error getting mode database\n");
1646 else {
1647 const struct fb_videomode *m;
1648
1649 fb_videomode_to_modelist(info->monspecs.modedb,
1650 info->monspecs.modedb_len,
1651 &info->modelist);
1652 m = fb_find_best_display(&info->monspecs,
1653 &info->modelist);
1654 if (m) {
1655 fb_videomode_to_var(&info->var, m);
1656 /* fill all other info->var's fields */
1657 if (tridentfb_check_var(&info->var,
1658 info) == 0)
1659 found = true;
1660 }
1661 }
1662 }
Amol Lada02f6402006-12-08 02:40:03 -08001663 }
Ondrej Zary6a5e3bd2015-09-25 00:14:14 +02001664
1665 if (!mode_option && !found)
1666 mode_option = "640x480-8@60";
1667
1668 /* Prepare startup mode */
1669 if (mode_option) {
1670 err = fb_find_mode(&info->var, info, mode_option,
1671 info->monspecs.modedb,
1672 info->monspecs.modedb_len,
1673 NULL, info->var.bits_per_pixel);
1674 if (!err || err == 4) {
1675 err = -EINVAL;
1676 dev_err(info->device, "mode %s not found\n",
1677 mode_option);
1678 fb_destroy_modedb(info->monspecs.modedb);
1679 info->monspecs.modedb = NULL;
1680 goto out_unmap2;
1681 }
1682 }
1683
1684 fb_destroy_modedb(info->monspecs.modedb);
1685 info->monspecs.modedb = NULL;
1686
Krzysztof Helte09ed092008-07-23 21:30:51 -07001687 err = fb_alloc_cmap(&info->cmap, 256, 0);
Krzysztof Helte8ed8572008-03-04 14:28:39 -08001688 if (err < 0)
1689 goto out_unmap2;
1690
Krzysztof Heltea8ee552008-07-23 21:30:51 -07001691 info->var.activate |= FB_ACTIVATE_NOW;
Krzysztof Helte09ed092008-07-23 21:30:51 -07001692 info->device = &dev->dev;
1693 if (register_framebuffer(info) < 0) {
Krzysztof Helt5cf13842008-07-23 21:31:05 -07001694 printk(KERN_ERR "tridentfb: could not register framebuffer\n");
Krzysztof Helte09ed092008-07-23 21:30:51 -07001695 fb_dealloc_cmap(&info->cmap);
Amol Lada02f6402006-12-08 02:40:03 -08001696 err = -EINVAL;
Krzysztof Helte8ed8572008-03-04 14:28:39 -08001697 goto out_unmap2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
1699 output("fb%d: %s frame buffer device %dx%d-%dbpp\n",
Krzysztof Heltea8ee552008-07-23 21:30:51 -07001700 info->node, info->fix.id, info->var.xres,
1701 info->var.yres, info->var.bits_per_pixel);
Krzysztof Helte09ed092008-07-23 21:30:51 -07001702
1703 pci_set_drvdata(dev, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return 0;
Amol Lada02f6402006-12-08 02:40:03 -08001705
Krzysztof Helte8ed8572008-03-04 14:28:39 -08001706out_unmap2:
Ondrej Zary6a5e3bd2015-09-25 00:14:14 +02001707 if (default_par->ddc_registered)
1708 i2c_del_adapter(&default_par->ddc_adapter);
Krzysztof Helt0292be42008-07-23 21:31:08 -07001709 kfree(info->pixmap.addr);
Krzysztof Helte09ed092008-07-23 21:30:51 -07001710 if (info->screen_base)
1711 iounmap(info->screen_base);
Krzysztof Helte8ed8572008-03-04 14:28:39 -08001712 release_mem_region(tridentfb_fix.smem_start, tridentfb_fix.smem_len);
Krzysztof Helte09ed092008-07-23 21:30:51 -07001713 disable_mmio(info->par);
Krzysztof Helte8ed8572008-03-04 14:28:39 -08001714out_unmap1:
Krzysztof Helte09ed092008-07-23 21:30:51 -07001715 if (default_par->io_virt)
1716 iounmap(default_par->io_virt);
Krzysztof Helte8ed8572008-03-04 14:28:39 -08001717 release_mem_region(tridentfb_fix.mmio_start, tridentfb_fix.mmio_len);
Krzysztof Helte09ed092008-07-23 21:30:51 -07001718 framebuffer_release(info);
Amol Lada02f6402006-12-08 02:40:03 -08001719 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720}
1721
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08001722static void trident_pci_remove(struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723{
Krzysztof Helte09ed092008-07-23 21:30:51 -07001724 struct fb_info *info = pci_get_drvdata(dev);
1725 struct tridentfb_par *par = info->par;
1726
1727 unregister_framebuffer(info);
Ondrej Zary6a5e3bd2015-09-25 00:14:14 +02001728 if (par->ddc_registered)
1729 i2c_del_adapter(&par->ddc_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 iounmap(par->io_virt);
Krzysztof Helte09ed092008-07-23 21:30:51 -07001731 iounmap(info->screen_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 release_mem_region(tridentfb_fix.smem_start, tridentfb_fix.smem_len);
Krzysztof Helte8ed8572008-03-04 14:28:39 -08001733 release_mem_region(tridentfb_fix.mmio_start, tridentfb_fix.mmio_len);
Krzysztof Helt0292be42008-07-23 21:31:08 -07001734 kfree(info->pixmap.addr);
Andres Salomon07b39b42009-03-31 15:25:22 -07001735 fb_dealloc_cmap(&info->cmap);
Krzysztof Helte09ed092008-07-23 21:30:51 -07001736 framebuffer_release(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737}
1738
1739/* List of boards that we are trying to support */
1740static struct pci_device_id trident_devices[] = {
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001741 {PCI_VENDOR_ID_TRIDENT, BLADE3D, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1742 {PCI_VENDOR_ID_TRIDENT, CYBERBLADEi7, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1743 {PCI_VENDOR_ID_TRIDENT, CYBERBLADEi7D, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1744 {PCI_VENDOR_ID_TRIDENT, CYBERBLADEi1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1745 {PCI_VENDOR_ID_TRIDENT, CYBERBLADEi1D, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1746 {PCI_VENDOR_ID_TRIDENT, CYBERBLADEAi1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1747 {PCI_VENDOR_ID_TRIDENT, CYBERBLADEAi1D, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1748 {PCI_VENDOR_ID_TRIDENT, CYBERBLADEE4, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
Krzysztof Helta0d92252008-07-23 21:30:58 -07001749 {PCI_VENDOR_ID_TRIDENT, TGUI9440, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001750 {PCI_VENDOR_ID_TRIDENT, TGUI9660, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1751 {PCI_VENDOR_ID_TRIDENT, IMAGE975, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1752 {PCI_VENDOR_ID_TRIDENT, IMAGE985, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1753 {PCI_VENDOR_ID_TRIDENT, CYBER9320, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1754 {PCI_VENDOR_ID_TRIDENT, CYBER9388, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1755 {PCI_VENDOR_ID_TRIDENT, CYBER9520, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1756 {PCI_VENDOR_ID_TRIDENT, CYBER9525DVD, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1757 {PCI_VENDOR_ID_TRIDENT, CYBER9397, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1758 {PCI_VENDOR_ID_TRIDENT, CYBER9397DVD, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1759 {PCI_VENDOR_ID_TRIDENT, CYBERBLADEXPAi1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1760 {PCI_VENDOR_ID_TRIDENT, CYBERBLADEXPm8, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1761 {PCI_VENDOR_ID_TRIDENT, CYBERBLADEXPm16, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 {0,}
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001763};
1764
1765MODULE_DEVICE_TABLE(pci, trident_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767static struct pci_driver tridentfb_pci_driver = {
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001768 .name = "tridentfb",
1769 .id_table = trident_devices,
1770 .probe = trident_pci_probe,
Greg Kroah-Hartman48c68c42012-12-21 13:07:39 -08001771 .remove = trident_pci_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772};
1773
1774/*
1775 * Parse user specified options (`video=trident:')
1776 * example:
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001777 * video=trident:800x600,bpp=16,noaccel
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 */
1779#ifndef MODULE
Krzysztof Helt07f41e42008-04-28 02:15:06 -07001780static int __init tridentfb_setup(char *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001782 char *opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 if (!options || !*options)
1784 return 0;
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001785 while ((opt = strsep(&options, ",")) != NULL) {
1786 if (!*opt)
1787 continue;
1788 if (!strncmp(opt, "noaccel", 7))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 noaccel = 1;
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001790 else if (!strncmp(opt, "fp", 2))
Krzysztof Helt6eed8e12008-07-23 21:30:53 -07001791 fp = 1;
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001792 else if (!strncmp(opt, "crt", 3))
Krzysztof Helt6eed8e12008-07-23 21:30:53 -07001793 fp = 0;
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001794 else if (!strncmp(opt, "bpp=", 4))
1795 bpp = simple_strtoul(opt + 4, NULL, 0);
1796 else if (!strncmp(opt, "center", 6))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 center = 1;
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001798 else if (!strncmp(opt, "stretch", 7))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 stretch = 1;
Krzysztof Helt245a2c22007-10-16 01:28:42 -07001800 else if (!strncmp(opt, "memsize=", 8))
1801 memsize = simple_strtoul(opt + 8, NULL, 0);
1802 else if (!strncmp(opt, "memdiff=", 8))
1803 memdiff = simple_strtoul(opt + 8, NULL, 0);
1804 else if (!strncmp(opt, "nativex=", 8))
1805 nativex = simple_strtoul(opt + 8, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 else
Krzysztof Helt07f41e42008-04-28 02:15:06 -07001807 mode_option = opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 }
1809 return 0;
1810}
1811#endif
1812
1813static int __init tridentfb_init(void)
1814{
1815#ifndef MODULE
1816 char *option = NULL;
1817
1818 if (fb_get_options("tridentfb", &option))
1819 return -ENODEV;
1820 tridentfb_setup(option);
1821#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 return pci_register_driver(&tridentfb_pci_driver);
1823}
1824
1825static void __exit tridentfb_exit(void)
1826{
1827 pci_unregister_driver(&tridentfb_pci_driver);
1828}
1829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830module_init(tridentfb_init);
1831module_exit(tridentfb_exit);
1832
1833MODULE_AUTHOR("Jani Monoses <jani@iv.ro>");
1834MODULE_DESCRIPTION("Framebuffer driver for Trident cards");
1835MODULE_LICENSE("GPL");
Krzysztof Heltddb53d42009-03-31 15:25:40 -07001836MODULE_ALIAS("cyblafb");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837