blob: 8215ca0555c21631b46f73c87d89838e6a84f206 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __RADEONFB_H__
2#define __RADEONFB_H__
3
Jean Delvareb0313f82008-04-28 02:15:12 -07004#ifdef CONFIG_FB_RADEON_DEBUG
5#define DEBUG 1
6#endif
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/module.h>
9#include <linux/kernel.h>
10#include <linux/sched.h>
11#include <linux/delay.h>
12#include <linux/pci.h>
13#include <linux/fb.h>
14
15
Jean Delvaree764a202005-11-07 01:00:34 -080016#ifdef CONFIG_FB_RADEON_I2C
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/i2c-algo-bit.h>
Jean Delvaree764a202005-11-07 01:00:34 -080019#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <asm/io.h>
22
David S. Miller9f47df22007-03-29 01:33:46 -070023#if defined(CONFIG_PPC_OF) || defined(CONFIG_SPARC)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/prom.h>
25#endif
26
27#include <video/radeon.h>
28
29/***************************************************************
30 * Most of the definitions here are adapted right from XFree86 *
31 ***************************************************************/
32
33
34/*
35 * Chip families. Must fit in the low 16 bits of a long word
36 */
37enum radeon_family {
38 CHIP_FAMILY_UNKNOW,
39 CHIP_FAMILY_LEGACY,
40 CHIP_FAMILY_RADEON,
41 CHIP_FAMILY_RV100,
42 CHIP_FAMILY_RS100, /* U1 (IGP320M) or A3 (IGP320)*/
43 CHIP_FAMILY_RV200,
44 CHIP_FAMILY_RS200, /* U2 (IGP330M/340M/350M) or A4 (IGP330/340/345/350),
45 RS250 (IGP 7000) */
46 CHIP_FAMILY_R200,
47 CHIP_FAMILY_RV250,
48 CHIP_FAMILY_RS300, /* Radeon 9000 IGP */
49 CHIP_FAMILY_RV280,
50 CHIP_FAMILY_R300,
51 CHIP_FAMILY_R350,
52 CHIP_FAMILY_RV350,
53 CHIP_FAMILY_RV380, /* RV370/RV380/M22/M24 */
54 CHIP_FAMILY_R420, /* R420/R423/M18 */
Sellout Bessie0b693ea2007-10-16 01:29:30 -070055 CHIP_FAMILY_RC410,
johan henrikssondd144712007-05-08 00:37:59 -070056 CHIP_FAMILY_RS480,
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 CHIP_FAMILY_LAST,
58};
59
60#define IS_RV100_VARIANT(rinfo) (((rinfo)->family == CHIP_FAMILY_RV100) || \
61 ((rinfo)->family == CHIP_FAMILY_RV200) || \
62 ((rinfo)->family == CHIP_FAMILY_RS100) || \
63 ((rinfo)->family == CHIP_FAMILY_RS200) || \
64 ((rinfo)->family == CHIP_FAMILY_RV250) || \
65 ((rinfo)->family == CHIP_FAMILY_RV280) || \
66 ((rinfo)->family == CHIP_FAMILY_RS300))
67
68
69#define IS_R300_VARIANT(rinfo) (((rinfo)->family == CHIP_FAMILY_R300) || \
70 ((rinfo)->family == CHIP_FAMILY_RV350) || \
71 ((rinfo)->family == CHIP_FAMILY_R350) || \
72 ((rinfo)->family == CHIP_FAMILY_RV380) || \
johan henrikssondd144712007-05-08 00:37:59 -070073 ((rinfo)->family == CHIP_FAMILY_R420) || \
Sellout Bessie0b693ea2007-10-16 01:29:30 -070074 ((rinfo)->family == CHIP_FAMILY_RC410) || \
75 ((rinfo)->family == CHIP_FAMILY_RS480))
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77/*
78 * Chip flags
79 */
80enum radeon_chip_flags {
81 CHIP_FAMILY_MASK = 0x0000ffffUL,
82 CHIP_FLAGS_MASK = 0xffff0000UL,
83 CHIP_IS_MOBILITY = 0x00010000UL,
84 CHIP_IS_IGP = 0x00020000UL,
85 CHIP_HAS_CRTC2 = 0x00040000UL,
86};
87
88/*
89 * Errata workarounds
90 */
91enum radeon_errata {
92 CHIP_ERRATA_R300_CG = 0x00000001,
93 CHIP_ERRATA_PLL_DUMMYREADS = 0x00000002,
94 CHIP_ERRATA_PLL_DELAY = 0x00000004,
95};
96
97
98/*
99 * Monitor types
100 */
101enum radeon_montype {
102 MT_NONE = 0,
103 MT_CRT, /* CRT */
104 MT_LCD, /* LCD */
105 MT_DFP, /* DVI */
106 MT_CTV, /* composite TV */
107 MT_STV /* S-Video out */
108};
109
110/*
111 * DDC i2c ports
112 */
113enum ddc_type {
114 ddc_none,
115 ddc_monid,
116 ddc_dvi,
117 ddc_vga,
118 ddc_crt2,
119};
120
121/*
122 * Connector types
123 */
124enum conn_type {
125 conn_none,
126 conn_proprietary,
127 conn_crt,
128 conn_DVI_I,
129 conn_DVI_D,
130};
131
132
133/*
134 * PLL infos
135 */
136struct pll_info {
137 int ppll_max;
138 int ppll_min;
139 int sclk, mclk;
140 int ref_div;
141 int ref_clk;
142};
143
144
145/*
146 * This structure contains the various registers manipulated by this
147 * driver for setting or restoring a mode. It's mostly copied from
148 * XFree's RADEONSaveRec structure. A few chip settings might still be
149 * tweaked without beeing reflected or saved in these registers though
150 */
151struct radeon_regs {
152 /* Common registers */
153 u32 ovr_clr;
154 u32 ovr_wid_left_right;
155 u32 ovr_wid_top_bottom;
156 u32 ov0_scale_cntl;
157 u32 mpp_tb_config;
158 u32 mpp_gp_config;
159 u32 subpic_cntl;
160 u32 viph_control;
161 u32 i2c_cntl_1;
162 u32 gen_int_cntl;
163 u32 cap0_trig_cntl;
164 u32 cap1_trig_cntl;
165 u32 bus_cntl;
166 u32 surface_cntl;
167 u32 bios_5_scratch;
168
169 /* Other registers to save for VT switches or driver load/unload */
170 u32 dp_datatype;
171 u32 rbbm_soft_reset;
172 u32 clock_cntl_index;
173 u32 amcgpio_en_reg;
174 u32 amcgpio_mask;
175
176 /* Surface/tiling registers */
177 u32 surf_lower_bound[8];
178 u32 surf_upper_bound[8];
179 u32 surf_info[8];
180
181 /* CRTC registers */
182 u32 crtc_gen_cntl;
183 u32 crtc_ext_cntl;
184 u32 dac_cntl;
185 u32 crtc_h_total_disp;
186 u32 crtc_h_sync_strt_wid;
187 u32 crtc_v_total_disp;
188 u32 crtc_v_sync_strt_wid;
189 u32 crtc_offset;
190 u32 crtc_offset_cntl;
191 u32 crtc_pitch;
192 u32 disp_merge_cntl;
193 u32 grph_buffer_cntl;
194 u32 crtc_more_cntl;
195
196 /* CRTC2 registers */
197 u32 crtc2_gen_cntl;
198 u32 dac2_cntl;
199 u32 disp_output_cntl;
200 u32 disp_hw_debug;
201 u32 disp2_merge_cntl;
202 u32 grph2_buffer_cntl;
203 u32 crtc2_h_total_disp;
204 u32 crtc2_h_sync_strt_wid;
205 u32 crtc2_v_total_disp;
206 u32 crtc2_v_sync_strt_wid;
207 u32 crtc2_offset;
208 u32 crtc2_offset_cntl;
209 u32 crtc2_pitch;
210
211 /* Flat panel regs */
212 u32 fp_crtc_h_total_disp;
213 u32 fp_crtc_v_total_disp;
214 u32 fp_gen_cntl;
215 u32 fp2_gen_cntl;
216 u32 fp_h_sync_strt_wid;
217 u32 fp2_h_sync_strt_wid;
218 u32 fp_horz_stretch;
219 u32 fp_panel_cntl;
220 u32 fp_v_sync_strt_wid;
221 u32 fp2_v_sync_strt_wid;
222 u32 fp_vert_stretch;
223 u32 lvds_gen_cntl;
224 u32 lvds_pll_cntl;
225 u32 tmds_crc;
226 u32 tmds_transmitter_cntl;
227
228 /* Computed values for PLL */
229 u32 dot_clock_freq;
230 int feedback_div;
231 int post_div;
232
233 /* PLL registers */
234 u32 ppll_div_3;
235 u32 ppll_ref_div;
236 u32 vclk_ecp_cntl;
237 u32 clk_cntl_index;
238
239 /* Computed values for PLL2 */
240 u32 dot_clock_freq_2;
241 int feedback_div_2;
242 int post_div_2;
243
244 /* PLL2 registers */
245 u32 p2pll_ref_div;
246 u32 p2pll_div_0;
247 u32 htotal_cntl2;
248
249 /* Palette */
250 int palette_valid;
251};
252
253struct panel_info {
254 int xres, yres;
255 int valid;
256 int clock;
257 int hOver_plus, hSync_width, hblank;
258 int vOver_plus, vSync_width, vblank;
259 int hAct_high, vAct_high, interlaced;
260 int pwr_delay;
261 int use_bios_dividers;
262 int ref_divider;
263 int post_divider;
264 int fbk_divider;
265};
266
267struct radeonfb_info;
268
269#ifdef CONFIG_FB_RADEON_I2C
270struct radeon_i2c_chan {
271 struct radeonfb_info *rinfo;
272 u32 ddc_reg;
273 struct i2c_adapter adapter;
274 struct i2c_algo_bit_data algo;
275};
276#endif
277
278enum radeon_pm_mode {
279 radeon_pm_none = 0, /* Nothing supported */
280 radeon_pm_d2 = 0x00000001, /* Can do D2 state */
281 radeon_pm_off = 0x00000002, /* Can resume from D3 cold */
282};
283
Volker Braun994aad22006-07-30 03:04:18 -0700284typedef void (*reinit_function_ptr)(struct radeonfb_info *rinfo);
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286struct radeonfb_info {
287 struct fb_info *info;
288
289 struct radeon_regs state;
290 struct radeon_regs init_state;
291
292 char name[DEVICE_NAME_SIZE];
293
294 unsigned long mmio_base_phys;
295 unsigned long fb_base_phys;
296
297 void __iomem *mmio_base;
298 void __iomem *fb_base;
299
300 unsigned long fb_local_base;
301
302 struct pci_dev *pdev;
David S. Miller9f47df22007-03-29 01:33:46 -0700303#if defined(CONFIG_PPC_OF) || defined(CONFIG_SPARC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 struct device_node *of_node;
305#endif
306
307 void __iomem *bios_seg;
308 int fp_bios_start;
309
Antonino A. Daplasf0e3ad42007-07-17 04:05:43 -0700310 u32 pseudo_palette[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 struct { u8 red, green, blue, pad; }
312 palette[256];
313
314 int chipset;
315 u8 family;
316 u8 rev;
317 unsigned int errata;
318 unsigned long video_ram;
319 unsigned long mapped_vram;
320 int vram_width;
321 int vram_ddr;
322
323 int pitch, bpp, depth;
324
325 int has_CRTC2;
326 int is_mobility;
327 int is_IGP;
328 int reversed_DAC;
329 int reversed_TMDS;
330 struct panel_info panel_info;
331 int mon1_type;
332 u8 *mon1_EDID;
333 struct fb_videomode *mon1_modedb;
334 int mon1_dbsize;
335 int mon2_type;
336 u8 *mon2_EDID;
337
338 u32 dp_gui_master_cntl;
339
340 struct pll_info pll;
341
342 int mtrr_hdl;
343
344 int pm_reg;
345 u32 save_regs[100];
346 int asleep;
347 int lock_blank;
348 int dynclk;
349 int no_schedule;
350 enum radeon_pm_mode pm_mode;
Volker Braun994aad22006-07-30 03:04:18 -0700351 reinit_function_ptr reinit_func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 /* Lock on register access */
354 spinlock_t reg_lock;
355
356 /* Timer used for delayed LVDS operations */
357 struct timer_list lvds_timer;
358 u32 pending_lvds_gen_cntl;
359
360#ifdef CONFIG_FB_RADEON_I2C
361 struct radeon_i2c_chan i2c[4];
362#endif
363
364 u32 cfg_save[64];
365};
366
367
368#define PRIMARY_MONITOR(rinfo) (rinfo->mon1_type)
369
370
371/*
372 * Debugging stuffs
373 */
Jean Delvareb0313f82008-04-28 02:15:12 -0700374#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375#define RTRACE printk
376#else
377#define RTRACE if(0) printk
378#endif
379
380
381/*
382 * IO macros
383 */
384
385/* Note about this function: we have some rare cases where we must not schedule,
386 * this typically happen with our special "wake up early" hook which allows us to
387 * wake up the graphic chip (and thus get the console back) before everything else
Adrian Bunkb3c2ffd2006-06-30 18:20:44 +0200388 * on some machines that support that mechanism. At this point, interrupts are off
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 * and scheduling is not permitted
390 */
391static inline void _radeon_msleep(struct radeonfb_info *rinfo, unsigned long ms)
392{
393 if (rinfo->no_schedule || oops_in_progress)
394 mdelay(ms);
395 else
396 msleep(ms);
397}
398
399
400#define INREG8(addr) readb((rinfo->mmio_base)+addr)
401#define OUTREG8(addr,val) writeb(val, (rinfo->mmio_base)+addr)
David S. Miller017fb982005-09-29 19:26:51 -0700402#define INREG16(addr) readw((rinfo->mmio_base)+addr)
403#define OUTREG16(addr,val) writew(val, (rinfo->mmio_base)+addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404#define INREG(addr) readl((rinfo->mmio_base)+addr)
405#define OUTREG(addr,val) writel(val, (rinfo->mmio_base)+addr)
406
407static inline void _OUTREGP(struct radeonfb_info *rinfo, u32 addr,
408 u32 val, u32 mask)
409{
410 unsigned long flags;
411 unsigned int tmp;
412
413 spin_lock_irqsave(&rinfo->reg_lock, flags);
414 tmp = INREG(addr);
415 tmp &= (mask);
416 tmp |= (val);
417 OUTREG(addr, tmp);
418 spin_unlock_irqrestore(&rinfo->reg_lock, flags);
419}
420
421#define OUTREGP(addr,val,mask) _OUTREGP(rinfo, addr, val,mask)
422
423/*
424 * Note about PLL register accesses:
425 *
426 * I have removed the spinlock on them on purpose. The driver now
427 * expects that it will only manipulate the PLL registers in normal
428 * task environment, where radeon_msleep() will be called, protected
429 * by a semaphore (currently the console semaphore) so that no conflict
430 * will happen on the PLL register index.
431 *
432 * With the latest changes to the VT layer, this is guaranteed for all
433 * calls except the actual drawing/blits which aren't supposed to use
434 * the PLL registers anyway
435 *
436 * This is very important for the workarounds to work properly. The only
437 * possible exception to this rule is the call to unblank(), which may
438 * be done at irq time if an oops is in progress.
439 */
440static inline void radeon_pll_errata_after_index(struct radeonfb_info *rinfo)
441{
442 if (!(rinfo->errata & CHIP_ERRATA_PLL_DUMMYREADS))
443 return;
444
445 (void)INREG(CLOCK_CNTL_DATA);
446 (void)INREG(CRTC_GEN_CNTL);
447}
448
449static inline void radeon_pll_errata_after_data(struct radeonfb_info *rinfo)
450{
451 if (rinfo->errata & CHIP_ERRATA_PLL_DELAY) {
452 /* we can't deal with posted writes here ... */
453 _radeon_msleep(rinfo, 5);
454 }
455 if (rinfo->errata & CHIP_ERRATA_R300_CG) {
456 u32 save, tmp;
457 save = INREG(CLOCK_CNTL_INDEX);
458 tmp = save & ~(0x3f | PLL_WR_EN);
459 OUTREG(CLOCK_CNTL_INDEX, tmp);
460 tmp = INREG(CLOCK_CNTL_DATA);
461 OUTREG(CLOCK_CNTL_INDEX, save);
462 }
463}
464
465static inline u32 __INPLL(struct radeonfb_info *rinfo, u32 addr)
466{
467 u32 data;
468
469 OUTREG8(CLOCK_CNTL_INDEX, addr & 0x0000003f);
470 radeon_pll_errata_after_index(rinfo);
471 data = INREG(CLOCK_CNTL_DATA);
472 radeon_pll_errata_after_data(rinfo);
473 return data;
474}
475
476static inline void __OUTPLL(struct radeonfb_info *rinfo, unsigned int index,
477 u32 val)
478{
479
480 OUTREG8(CLOCK_CNTL_INDEX, (index & 0x0000003f) | 0x00000080);
481 radeon_pll_errata_after_index(rinfo);
482 OUTREG(CLOCK_CNTL_DATA, val);
483 radeon_pll_errata_after_data(rinfo);
484}
485
486
487static inline void __OUTPLLP(struct radeonfb_info *rinfo, unsigned int index,
488 u32 val, u32 mask)
489{
490 unsigned int tmp;
491
492 tmp = __INPLL(rinfo, index);
493 tmp &= (mask);
494 tmp |= (val);
495 __OUTPLL(rinfo, index, tmp);
496}
497
498
499#define INPLL(addr) __INPLL(rinfo, addr)
500#define OUTPLL(index, val) __OUTPLL(rinfo, index, val)
501#define OUTPLLP(index, val, mask) __OUTPLLP(rinfo, index, val, mask)
502
503
504#define BIOS_IN8(v) (readb(rinfo->bios_seg + (v)))
505#define BIOS_IN16(v) (readb(rinfo->bios_seg + (v)) | \
506 (readb(rinfo->bios_seg + (v) + 1) << 8))
507#define BIOS_IN32(v) (readb(rinfo->bios_seg + (v)) | \
508 (readb(rinfo->bios_seg + (v) + 1) << 8) | \
509 (readb(rinfo->bios_seg + (v) + 2) << 16) | \
510 (readb(rinfo->bios_seg + (v) + 3) << 24))
511
512/*
513 * Inline utilities
514 */
515static inline int round_div(int num, int den)
516{
517 return (num + (den / 2)) / den;
518}
519
520static inline int var_to_depth(const struct fb_var_screeninfo *var)
521{
522 if (var->bits_per_pixel != 16)
523 return var->bits_per_pixel;
524 return (var->green.length == 5) ? 15 : 16;
525}
526
527static inline u32 radeon_get_dstbpp(u16 depth)
528{
529 switch (depth) {
530 case 8:
531 return DST_8BPP;
532 case 15:
533 return DST_15BPP;
534 case 16:
535 return DST_16BPP;
536 case 32:
537 return DST_32BPP;
538 default:
539 return 0;
540 }
541}
542
543/*
544 * 2D Engine helper routines
545 */
546static inline void radeon_engine_flush (struct radeonfb_info *rinfo)
547{
548 int i;
549
550 /* initiate flush */
551 OUTREGP(RB2D_DSTCACHE_CTLSTAT, RB2D_DC_FLUSH_ALL,
552 ~RB2D_DC_FLUSH_ALL);
553
554 for (i=0; i < 2000000; i++) {
555 if (!(INREG(RB2D_DSTCACHE_CTLSTAT) & RB2D_DC_BUSY))
556 return;
557 udelay(1);
558 }
559 printk(KERN_ERR "radeonfb: Flush Timeout !\n");
560}
561
562
563static inline void _radeon_fifo_wait(struct radeonfb_info *rinfo, int entries)
564{
565 int i;
566
567 for (i=0; i<2000000; i++) {
568 if ((INREG(RBBM_STATUS) & 0x7f) >= entries)
569 return;
570 udelay(1);
571 }
572 printk(KERN_ERR "radeonfb: FIFO Timeout !\n");
573}
574
575
576static inline void _radeon_engine_idle(struct radeonfb_info *rinfo)
577{
578 int i;
579
580 /* ensure FIFO is empty before waiting for idle */
581 _radeon_fifo_wait (rinfo, 64);
582
583 for (i=0; i<2000000; i++) {
584 if (((INREG(RBBM_STATUS) & GUI_ACTIVE)) == 0) {
585 radeon_engine_flush (rinfo);
586 return;
587 }
588 udelay(1);
589 }
590 printk(KERN_ERR "radeonfb: Idle Timeout !\n");
591}
592
593
594#define radeon_engine_idle() _radeon_engine_idle(rinfo)
595#define radeon_fifo_wait(entries) _radeon_fifo_wait(rinfo,entries)
596#define radeon_msleep(ms) _radeon_msleep(rinfo,ms)
597
598
599/* I2C Functions */
600extern void radeon_create_i2c_busses(struct radeonfb_info *rinfo);
601extern void radeon_delete_i2c_busses(struct radeonfb_info *rinfo);
602extern int radeon_probe_i2c_connector(struct radeonfb_info *rinfo, int conn, u8 **out_edid);
603
604/* PM Functions */
605extern int radeonfb_pci_suspend(struct pci_dev *pdev, pm_message_t state);
606extern int radeonfb_pci_resume(struct pci_dev *pdev);
Volker Braun994aad22006-07-30 03:04:18 -0700607extern void radeonfb_pm_init(struct radeonfb_info *rinfo, int dynclk, int ignore_devlist, int force_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608extern void radeonfb_pm_exit(struct radeonfb_info *rinfo);
609
610/* Monitor probe functions */
611extern void radeon_probe_screens(struct radeonfb_info *rinfo,
612 const char *monitor_layout, int ignore_edid);
613extern void radeon_check_modes(struct radeonfb_info *rinfo, const char *mode_option);
614extern int radeon_match_mode(struct radeonfb_info *rinfo,
615 struct fb_var_screeninfo *dest,
616 const struct fb_var_screeninfo *src);
617
618/* Accel functions */
619extern void radeonfb_fillrect(struct fb_info *info, const struct fb_fillrect *region);
620extern void radeonfb_copyarea(struct fb_info *info, const struct fb_copyarea *area);
621extern void radeonfb_imageblit(struct fb_info *p, const struct fb_image *image);
622extern int radeonfb_sync(struct fb_info *info);
623extern void radeonfb_engine_init (struct radeonfb_info *rinfo);
624extern void radeonfb_engine_reset(struct radeonfb_info *rinfo);
625
626/* Other functions */
627extern int radeon_screen_blank(struct radeonfb_info *rinfo, int blank, int mode_switch);
628extern void radeon_write_mode (struct radeonfb_info *rinfo, struct radeon_regs *mode,
629 int reg_only);
630
Michael Hanselmann5474c122006-06-25 05:47:08 -0700631/* Backlight functions */
632#ifdef CONFIG_FB_RADEON_BACKLIGHT
633extern void radeonfb_bl_init(struct radeonfb_info *rinfo);
634extern void radeonfb_bl_exit(struct radeonfb_info *rinfo);
635#else
636static inline void radeonfb_bl_init(struct radeonfb_info *rinfo) {}
637static inline void radeonfb_bl_exit(struct radeonfb_info *rinfo) {}
638#endif
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640#endif /* __RADEONFB_H__ */