Home | History | Annotate | Line # | Download | only in radeon
      1  1.3  riastrad /*	$NetBSD: radeon_legacy_tv.c,v 1.4 2021/12/18 23:45:43 riastradh Exp $	*/
      2  1.3  riastrad 
      3  1.4  riastrad // SPDX-License-Identifier: MIT
      4  1.4  riastrad 
      5  1.3  riastrad #include <sys/cdefs.h>
      6  1.3  riastrad __KERNEL_RCSID(0, "$NetBSD: radeon_legacy_tv.c,v 1.4 2021/12/18 23:45:43 riastradh Exp $");
      7  1.3  riastrad 
      8  1.1  riastrad #include <drm/drm_crtc_helper.h>
      9  1.4  riastrad #include <drm/drm_device.h>
     10  1.4  riastrad 
     11  1.1  riastrad #include "radeon.h"
     12  1.1  riastrad 
     13  1.1  riastrad /*
     14  1.1  riastrad  * Integrated TV out support based on the GATOS code by
     15  1.1  riastrad  * Federico Ulivi <fulivi (at) lycos.com>
     16  1.1  riastrad  */
     17  1.1  riastrad 
     18  1.1  riastrad 
     19  1.1  riastrad /*
     20  1.1  riastrad  * Limits of h/v positions (hPos & vPos)
     21  1.1  riastrad  */
     22  1.1  riastrad #define MAX_H_POSITION 5 /* Range: [-5..5], negative is on the left, 0 is default, positive is on the right */
     23  1.1  riastrad #define MAX_V_POSITION 5 /* Range: [-5..5], negative is up, 0 is default, positive is down */
     24  1.1  riastrad 
     25  1.1  riastrad /*
     26  1.1  riastrad  * Unit for hPos (in TV clock periods)
     27  1.1  riastrad  */
     28  1.1  riastrad #define H_POS_UNIT 10
     29  1.1  riastrad 
     30  1.1  riastrad /*
     31  1.1  riastrad  * Indexes in h. code timing table for horizontal line position adjustment
     32  1.1  riastrad  */
     33  1.1  riastrad #define H_TABLE_POS1 6
     34  1.1  riastrad #define H_TABLE_POS2 8
     35  1.1  riastrad 
     36  1.1  riastrad /*
     37  1.1  riastrad  * Limits of hor. size (hSize)
     38  1.1  riastrad  */
     39  1.1  riastrad #define MAX_H_SIZE 5 /* Range: [-5..5], negative is smaller, positive is larger */
     40  1.1  riastrad 
     41  1.1  riastrad /* tv standard constants */
     42  1.1  riastrad #define NTSC_TV_CLOCK_T 233
     43  1.1  riastrad #define NTSC_TV_VFTOTAL 1
     44  1.1  riastrad #define NTSC_TV_LINES_PER_FRAME 525
     45  1.1  riastrad #define NTSC_TV_ZERO_H_SIZE 479166
     46  1.1  riastrad #define NTSC_TV_H_SIZE_UNIT 9478
     47  1.1  riastrad 
     48  1.1  riastrad #define PAL_TV_CLOCK_T 188
     49  1.1  riastrad #define PAL_TV_VFTOTAL 3
     50  1.1  riastrad #define PAL_TV_LINES_PER_FRAME 625
     51  1.1  riastrad #define PAL_TV_ZERO_H_SIZE 473200
     52  1.1  riastrad #define PAL_TV_H_SIZE_UNIT 9360
     53  1.1  riastrad 
     54  1.1  riastrad /* tv pll setting for 27 mhz ref clk */
     55  1.1  riastrad #define NTSC_TV_PLL_M_27 22
     56  1.1  riastrad #define NTSC_TV_PLL_N_27 175
     57  1.1  riastrad #define NTSC_TV_PLL_P_27 5
     58  1.1  riastrad 
     59  1.1  riastrad #define PAL_TV_PLL_M_27 113
     60  1.1  riastrad #define PAL_TV_PLL_N_27 668
     61  1.1  riastrad #define PAL_TV_PLL_P_27 3
     62  1.1  riastrad 
     63  1.1  riastrad /* tv pll setting for 14 mhz ref clk */
     64  1.1  riastrad #define NTSC_TV_PLL_M_14 33
     65  1.1  riastrad #define NTSC_TV_PLL_N_14 693
     66  1.1  riastrad #define NTSC_TV_PLL_P_14 7
     67  1.1  riastrad 
     68  1.1  riastrad #define PAL_TV_PLL_M_14 19
     69  1.1  riastrad #define PAL_TV_PLL_N_14 353
     70  1.1  riastrad #define PAL_TV_PLL_P_14 5
     71  1.1  riastrad 
     72  1.1  riastrad #define VERT_LEAD_IN_LINES 2
     73  1.1  riastrad #define FRAC_BITS 0xe
     74  1.1  riastrad #define FRAC_MASK 0x3fff
     75  1.1  riastrad 
     76  1.1  riastrad struct radeon_tv_mode_constants {
     77  1.1  riastrad 	uint16_t hor_resolution;
     78  1.1  riastrad 	uint16_t ver_resolution;
     79  1.1  riastrad 	enum radeon_tv_std standard;
     80  1.1  riastrad 	uint16_t hor_total;
     81  1.1  riastrad 	uint16_t ver_total;
     82  1.1  riastrad 	uint16_t hor_start;
     83  1.1  riastrad 	uint16_t hor_syncstart;
     84  1.1  riastrad 	uint16_t ver_syncstart;
     85  1.1  riastrad 	unsigned def_restart;
     86  1.1  riastrad 	uint16_t crtcPLL_N;
     87  1.1  riastrad 	uint8_t  crtcPLL_M;
     88  1.1  riastrad 	uint8_t  crtcPLL_post_div;
     89  1.1  riastrad 	unsigned pix_to_tv;
     90  1.1  riastrad };
     91  1.1  riastrad 
     92  1.1  riastrad static const uint16_t hor_timing_NTSC[MAX_H_CODE_TIMING_LEN] = {
     93  1.1  riastrad 	0x0007,
     94  1.1  riastrad 	0x003f,
     95  1.1  riastrad 	0x0263,
     96  1.1  riastrad 	0x0a24,
     97  1.1  riastrad 	0x2a6b,
     98  1.1  riastrad 	0x0a36,
     99  1.1  riastrad 	0x126d, /* H_TABLE_POS1 */
    100  1.1  riastrad 	0x1bfe,
    101  1.1  riastrad 	0x1a8f, /* H_TABLE_POS2 */
    102  1.1  riastrad 	0x1ec7,
    103  1.1  riastrad 	0x3863,
    104  1.1  riastrad 	0x1bfe,
    105  1.1  riastrad 	0x1bfe,
    106  1.1  riastrad 	0x1a2a,
    107  1.1  riastrad 	0x1e95,
    108  1.1  riastrad 	0x0e31,
    109  1.1  riastrad 	0x201b,
    110  1.1  riastrad 	0
    111  1.1  riastrad };
    112  1.1  riastrad 
    113  1.1  riastrad static const uint16_t vert_timing_NTSC[MAX_V_CODE_TIMING_LEN] = {
    114  1.1  riastrad 	0x2001,
    115  1.1  riastrad 	0x200d,
    116  1.1  riastrad 	0x1006,
    117  1.1  riastrad 	0x0c06,
    118  1.1  riastrad 	0x1006,
    119  1.1  riastrad 	0x1818,
    120  1.1  riastrad 	0x21e3,
    121  1.1  riastrad 	0x1006,
    122  1.1  riastrad 	0x0c06,
    123  1.1  riastrad 	0x1006,
    124  1.1  riastrad 	0x1817,
    125  1.1  riastrad 	0x21d4,
    126  1.1  riastrad 	0x0002,
    127  1.1  riastrad 	0
    128  1.1  riastrad };
    129  1.1  riastrad 
    130  1.1  riastrad static const uint16_t hor_timing_PAL[MAX_H_CODE_TIMING_LEN] = {
    131  1.1  riastrad 	0x0007,
    132  1.1  riastrad 	0x0058,
    133  1.1  riastrad 	0x027c,
    134  1.1  riastrad 	0x0a31,
    135  1.1  riastrad 	0x2a77,
    136  1.1  riastrad 	0x0a95,
    137  1.1  riastrad 	0x124f, /* H_TABLE_POS1 */
    138  1.1  riastrad 	0x1bfe,
    139  1.1  riastrad 	0x1b22, /* H_TABLE_POS2 */
    140  1.1  riastrad 	0x1ef9,
    141  1.1  riastrad 	0x387c,
    142  1.1  riastrad 	0x1bfe,
    143  1.1  riastrad 	0x1bfe,
    144  1.1  riastrad 	0x1b31,
    145  1.1  riastrad 	0x1eb5,
    146  1.1  riastrad 	0x0e43,
    147  1.1  riastrad 	0x201b,
    148  1.1  riastrad 	0
    149  1.1  riastrad };
    150  1.1  riastrad 
    151  1.1  riastrad static const uint16_t vert_timing_PAL[MAX_V_CODE_TIMING_LEN] = {
    152  1.1  riastrad 	0x2001,
    153  1.1  riastrad 	0x200c,
    154  1.1  riastrad 	0x1005,
    155  1.1  riastrad 	0x0c05,
    156  1.1  riastrad 	0x1005,
    157  1.1  riastrad 	0x1401,
    158  1.1  riastrad 	0x1821,
    159  1.1  riastrad 	0x2240,
    160  1.1  riastrad 	0x1005,
    161  1.1  riastrad 	0x0c05,
    162  1.1  riastrad 	0x1005,
    163  1.1  riastrad 	0x1401,
    164  1.1  riastrad 	0x1822,
    165  1.1  riastrad 	0x2230,
    166  1.1  riastrad 	0x0002,
    167  1.1  riastrad 	0
    168  1.1  riastrad };
    169  1.1  riastrad 
    170  1.1  riastrad /**********************************************************************
    171  1.1  riastrad  *
    172  1.1  riastrad  * availableModes
    173  1.1  riastrad  *
    174  1.1  riastrad  * Table of all allowed modes for tv output
    175  1.1  riastrad  *
    176  1.1  riastrad  **********************************************************************/
    177  1.1  riastrad static const struct radeon_tv_mode_constants available_tv_modes[] = {
    178  1.1  riastrad 	{   /* NTSC timing for 27 Mhz ref clk */
    179  1.1  riastrad 		800,                /* horResolution */
    180  1.1  riastrad 		600,                /* verResolution */
    181  1.1  riastrad 		TV_STD_NTSC,        /* standard */
    182  1.1  riastrad 		990,                /* horTotal */
    183  1.1  riastrad 		740,                /* verTotal */
    184  1.1  riastrad 		813,                /* horStart */
    185  1.1  riastrad 		824,                /* horSyncStart */
    186  1.1  riastrad 		632,                /* verSyncStart */
    187  1.1  riastrad 		625592,             /* defRestart */
    188  1.1  riastrad 		592,                /* crtcPLL_N */
    189  1.1  riastrad 		91,                 /* crtcPLL_M */
    190  1.1  riastrad 		4,                  /* crtcPLL_postDiv */
    191  1.1  riastrad 		1022,               /* pixToTV */
    192  1.1  riastrad 	},
    193  1.1  riastrad 	{   /* PAL timing for 27 Mhz ref clk */
    194  1.1  riastrad 		800,               /* horResolution */
    195  1.1  riastrad 		600,               /* verResolution */
    196  1.1  riastrad 		TV_STD_PAL,        /* standard */
    197  1.1  riastrad 		1144,              /* horTotal */
    198  1.1  riastrad 		706,               /* verTotal */
    199  1.1  riastrad 		812,               /* horStart */
    200  1.1  riastrad 		824,               /* horSyncStart */
    201  1.1  riastrad 		669,               /* verSyncStart */
    202  1.1  riastrad 		696700,            /* defRestart */
    203  1.1  riastrad 		1382,              /* crtcPLL_N */
    204  1.1  riastrad 		231,               /* crtcPLL_M */
    205  1.1  riastrad 		4,                 /* crtcPLL_postDiv */
    206  1.1  riastrad 		759,               /* pixToTV */
    207  1.1  riastrad 	},
    208  1.1  riastrad 	{   /* NTSC timing for 14 Mhz ref clk */
    209  1.1  riastrad 		800,                /* horResolution */
    210  1.1  riastrad 		600,                /* verResolution */
    211  1.1  riastrad 		TV_STD_NTSC,        /* standard */
    212  1.1  riastrad 		1018,               /* horTotal */
    213  1.1  riastrad 		727,                /* verTotal */
    214  1.1  riastrad 		813,                /* horStart */
    215  1.1  riastrad 		840,                /* horSyncStart */
    216  1.1  riastrad 		633,                /* verSyncStart */
    217  1.1  riastrad 		630627,             /* defRestart */
    218  1.1  riastrad 		347,                /* crtcPLL_N */
    219  1.1  riastrad 		14,                 /* crtcPLL_M */
    220  1.1  riastrad 		8,                  /* crtcPLL_postDiv */
    221  1.1  riastrad 		1022,               /* pixToTV */
    222  1.1  riastrad 	},
    223  1.1  riastrad 	{ /* PAL timing for 14 Mhz ref clk */
    224  1.1  riastrad 		800,                /* horResolution */
    225  1.1  riastrad 		600,                /* verResolution */
    226  1.1  riastrad 		TV_STD_PAL,         /* standard */
    227  1.1  riastrad 		1131,               /* horTotal */
    228  1.1  riastrad 		742,                /* verTotal */
    229  1.1  riastrad 		813,                /* horStart */
    230  1.1  riastrad 		840,                /* horSyncStart */
    231  1.1  riastrad 		633,                /* verSyncStart */
    232  1.1  riastrad 		708369,             /* defRestart */
    233  1.1  riastrad 		211,                /* crtcPLL_N */
    234  1.1  riastrad 		9,                  /* crtcPLL_M */
    235  1.1  riastrad 		8,                  /* crtcPLL_postDiv */
    236  1.1  riastrad 		759,                /* pixToTV */
    237  1.1  riastrad 	},
    238  1.1  riastrad };
    239  1.1  riastrad 
    240  1.1  riastrad #define N_AVAILABLE_MODES ARRAY_SIZE(available_tv_modes)
    241  1.1  riastrad 
    242  1.1  riastrad static const struct radeon_tv_mode_constants *radeon_legacy_tv_get_std_mode(struct radeon_encoder *radeon_encoder,
    243  1.1  riastrad 									    uint16_t *pll_ref_freq)
    244  1.1  riastrad {
    245  1.1  riastrad 	struct drm_device *dev = radeon_encoder->base.dev;
    246  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    247  1.1  riastrad 	struct radeon_crtc *radeon_crtc;
    248  1.1  riastrad 	struct radeon_encoder_tv_dac *tv_dac = radeon_encoder->enc_priv;
    249  1.1  riastrad 	const struct radeon_tv_mode_constants *const_ptr;
    250  1.1  riastrad 	struct radeon_pll *pll;
    251  1.1  riastrad 
    252  1.1  riastrad 	radeon_crtc = to_radeon_crtc(radeon_encoder->base.crtc);
    253  1.1  riastrad 	if (radeon_crtc->crtc_id == 1)
    254  1.1  riastrad 		pll = &rdev->clock.p2pll;
    255  1.1  riastrad 	else
    256  1.1  riastrad 		pll = &rdev->clock.p1pll;
    257  1.1  riastrad 
    258  1.1  riastrad 	if (pll_ref_freq)
    259  1.1  riastrad 		*pll_ref_freq = pll->reference_freq;
    260  1.1  riastrad 
    261  1.1  riastrad 	if (tv_dac->tv_std == TV_STD_NTSC ||
    262  1.1  riastrad 	    tv_dac->tv_std == TV_STD_NTSC_J ||
    263  1.1  riastrad 	    tv_dac->tv_std == TV_STD_PAL_M) {
    264  1.1  riastrad 		if (pll->reference_freq == 2700)
    265  1.1  riastrad 			const_ptr = &available_tv_modes[0];
    266  1.1  riastrad 		else
    267  1.1  riastrad 			const_ptr = &available_tv_modes[2];
    268  1.1  riastrad 	} else {
    269  1.1  riastrad 		if (pll->reference_freq == 2700)
    270  1.1  riastrad 			const_ptr = &available_tv_modes[1];
    271  1.1  riastrad 		else
    272  1.1  riastrad 			const_ptr = &available_tv_modes[3];
    273  1.1  riastrad 	}
    274  1.1  riastrad 	return const_ptr;
    275  1.1  riastrad }
    276  1.1  riastrad 
    277  1.1  riastrad static long YCOEF_value[5] = { 2, 2, 0, 4, 0 };
    278  1.1  riastrad static long YCOEF_EN_value[5] = { 1, 1, 0, 1, 0 };
    279  1.1  riastrad static long SLOPE_value[5] = { 1, 2, 2, 4, 8 };
    280  1.1  riastrad static long SLOPE_limit[5] = { 6, 5, 4, 3, 2 };
    281  1.1  riastrad 
    282  1.1  riastrad static void radeon_wait_pll_lock(struct drm_encoder *encoder, unsigned n_tests,
    283  1.1  riastrad 				 unsigned n_wait_loops, unsigned cnt_threshold)
    284  1.1  riastrad {
    285  1.1  riastrad 	struct drm_device *dev = encoder->dev;
    286  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    287  1.1  riastrad 	uint32_t save_pll_test;
    288  1.1  riastrad 	unsigned int i, j;
    289  1.1  riastrad 
    290  1.1  riastrad 	WREG32(RADEON_TEST_DEBUG_MUX, (RREG32(RADEON_TEST_DEBUG_MUX) & 0xffff60ff) | 0x100);
    291  1.1  riastrad 	save_pll_test = RREG32_PLL(RADEON_PLL_TEST_CNTL);
    292  1.1  riastrad 	WREG32_PLL(RADEON_PLL_TEST_CNTL, save_pll_test & ~RADEON_PLL_MASK_READ_B);
    293  1.1  riastrad 
    294  1.1  riastrad 	WREG8(RADEON_CLOCK_CNTL_INDEX, RADEON_PLL_TEST_CNTL);
    295  1.1  riastrad 	for (i = 0; i < n_tests; i++) {
    296  1.1  riastrad 		WREG8(RADEON_CLOCK_CNTL_DATA + 3, 0);
    297  1.1  riastrad 		for (j = 0; j < n_wait_loops; j++)
    298  1.1  riastrad 			if (RREG8(RADEON_CLOCK_CNTL_DATA + 3) >= cnt_threshold)
    299  1.1  riastrad 				break;
    300  1.1  riastrad 	}
    301  1.1  riastrad 	WREG32_PLL(RADEON_PLL_TEST_CNTL, save_pll_test);
    302  1.1  riastrad 	WREG32(RADEON_TEST_DEBUG_MUX, RREG32(RADEON_TEST_DEBUG_MUX) & 0xffffe0ff);
    303  1.1  riastrad }
    304  1.1  riastrad 
    305  1.1  riastrad 
    306  1.1  riastrad static void radeon_legacy_tv_write_fifo(struct radeon_encoder *radeon_encoder,
    307  1.1  riastrad 					uint16_t addr, uint32_t value)
    308  1.1  riastrad {
    309  1.1  riastrad 	struct drm_device *dev = radeon_encoder->base.dev;
    310  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    311  1.1  riastrad 	uint32_t tmp;
    312  1.1  riastrad 	int i = 0;
    313  1.1  riastrad 
    314  1.1  riastrad 	WREG32(RADEON_TV_HOST_WRITE_DATA, value);
    315  1.1  riastrad 
    316  1.1  riastrad 	WREG32(RADEON_TV_HOST_RD_WT_CNTL, addr);
    317  1.1  riastrad 	WREG32(RADEON_TV_HOST_RD_WT_CNTL, addr | RADEON_HOST_FIFO_WT);
    318  1.1  riastrad 
    319  1.1  riastrad 	do {
    320  1.1  riastrad 		tmp = RREG32(RADEON_TV_HOST_RD_WT_CNTL);
    321  1.1  riastrad 		if ((tmp & RADEON_HOST_FIFO_WT_ACK) == 0)
    322  1.1  riastrad 			break;
    323  1.1  riastrad 		i++;
    324  1.1  riastrad 	} while (i < 10000);
    325  1.1  riastrad 	WREG32(RADEON_TV_HOST_RD_WT_CNTL, 0);
    326  1.1  riastrad }
    327  1.1  riastrad 
    328  1.1  riastrad #if 0 /* included for completeness */
    329  1.1  riastrad static uint32_t radeon_legacy_tv_read_fifo(struct radeon_encoder *radeon_encoder, uint16_t addr)
    330  1.1  riastrad {
    331  1.1  riastrad 	struct drm_device *dev = radeon_encoder->base.dev;
    332  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    333  1.1  riastrad 	uint32_t tmp;
    334  1.1  riastrad 	int i = 0;
    335  1.1  riastrad 
    336  1.1  riastrad 	WREG32(RADEON_TV_HOST_RD_WT_CNTL, addr);
    337  1.1  riastrad 	WREG32(RADEON_TV_HOST_RD_WT_CNTL, addr | RADEON_HOST_FIFO_RD);
    338  1.1  riastrad 
    339  1.1  riastrad 	do {
    340  1.1  riastrad 		tmp = RREG32(RADEON_TV_HOST_RD_WT_CNTL);
    341  1.1  riastrad 		if ((tmp & RADEON_HOST_FIFO_RD_ACK) == 0)
    342  1.1  riastrad 			break;
    343  1.1  riastrad 		i++;
    344  1.1  riastrad 	} while (i < 10000);
    345  1.1  riastrad 	WREG32(RADEON_TV_HOST_RD_WT_CNTL, 0);
    346  1.1  riastrad 	return RREG32(RADEON_TV_HOST_READ_DATA);
    347  1.1  riastrad }
    348  1.1  riastrad #endif
    349  1.1  riastrad 
    350  1.1  riastrad static uint16_t radeon_get_htiming_tables_addr(uint32_t tv_uv_adr)
    351  1.1  riastrad {
    352  1.1  riastrad 	uint16_t h_table;
    353  1.1  riastrad 
    354  1.1  riastrad 	switch ((tv_uv_adr & RADEON_HCODE_TABLE_SEL_MASK) >> RADEON_HCODE_TABLE_SEL_SHIFT) {
    355  1.1  riastrad 	case 0:
    356  1.1  riastrad 		h_table = RADEON_TV_MAX_FIFO_ADDR_INTERNAL;
    357  1.1  riastrad 		break;
    358  1.1  riastrad 	case 1:
    359  1.1  riastrad 		h_table = ((tv_uv_adr & RADEON_TABLE1_BOT_ADR_MASK) >> RADEON_TABLE1_BOT_ADR_SHIFT) * 2;
    360  1.1  riastrad 		break;
    361  1.1  riastrad 	case 2:
    362  1.1  riastrad 		h_table = ((tv_uv_adr & RADEON_TABLE3_TOP_ADR_MASK) >> RADEON_TABLE3_TOP_ADR_SHIFT) * 2;
    363  1.1  riastrad 		break;
    364  1.1  riastrad 	default:
    365  1.1  riastrad 		h_table = 0;
    366  1.1  riastrad 		break;
    367  1.1  riastrad 	}
    368  1.1  riastrad 	return h_table;
    369  1.1  riastrad }
    370  1.1  riastrad 
    371  1.1  riastrad static uint16_t radeon_get_vtiming_tables_addr(uint32_t tv_uv_adr)
    372  1.1  riastrad {
    373  1.1  riastrad 	uint16_t v_table;
    374  1.1  riastrad 
    375  1.1  riastrad 	switch ((tv_uv_adr & RADEON_VCODE_TABLE_SEL_MASK) >> RADEON_VCODE_TABLE_SEL_SHIFT) {
    376  1.1  riastrad 	case 0:
    377  1.1  riastrad 		v_table = ((tv_uv_adr & RADEON_MAX_UV_ADR_MASK) >> RADEON_MAX_UV_ADR_SHIFT) * 2 + 1;
    378  1.1  riastrad 		break;
    379  1.1  riastrad 	case 1:
    380  1.1  riastrad 		v_table = ((tv_uv_adr & RADEON_TABLE1_BOT_ADR_MASK) >> RADEON_TABLE1_BOT_ADR_SHIFT) * 2 + 1;
    381  1.1  riastrad 		break;
    382  1.1  riastrad 	case 2:
    383  1.1  riastrad 		v_table = ((tv_uv_adr & RADEON_TABLE3_TOP_ADR_MASK) >> RADEON_TABLE3_TOP_ADR_SHIFT) * 2 + 1;
    384  1.1  riastrad 		break;
    385  1.1  riastrad 	default:
    386  1.1  riastrad 		v_table = 0;
    387  1.1  riastrad 		break;
    388  1.1  riastrad 	}
    389  1.1  riastrad 	return v_table;
    390  1.1  riastrad }
    391  1.1  riastrad 
    392  1.1  riastrad static void radeon_restore_tv_timing_tables(struct radeon_encoder *radeon_encoder)
    393  1.1  riastrad {
    394  1.1  riastrad 	struct drm_device *dev = radeon_encoder->base.dev;
    395  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    396  1.1  riastrad 	struct radeon_encoder_tv_dac *tv_dac = radeon_encoder->enc_priv;
    397  1.1  riastrad 	uint16_t h_table, v_table;
    398  1.1  riastrad 	uint32_t tmp;
    399  1.1  riastrad 	int i;
    400  1.1  riastrad 
    401  1.1  riastrad 	WREG32(RADEON_TV_UV_ADR, tv_dac->tv.tv_uv_adr);
    402  1.1  riastrad 	h_table = radeon_get_htiming_tables_addr(tv_dac->tv.tv_uv_adr);
    403  1.1  riastrad 	v_table = radeon_get_vtiming_tables_addr(tv_dac->tv.tv_uv_adr);
    404  1.1  riastrad 
    405  1.1  riastrad 	for (i = 0; i < MAX_H_CODE_TIMING_LEN; i += 2, h_table--) {
    406  1.1  riastrad 		tmp = ((uint32_t)tv_dac->tv.h_code_timing[i] << 14) | ((uint32_t)tv_dac->tv.h_code_timing[i+1]);
    407  1.1  riastrad 		radeon_legacy_tv_write_fifo(radeon_encoder, h_table, tmp);
    408  1.1  riastrad 		if (tv_dac->tv.h_code_timing[i] == 0 || tv_dac->tv.h_code_timing[i + 1] == 0)
    409  1.1  riastrad 			break;
    410  1.1  riastrad 	}
    411  1.1  riastrad 	for (i = 0; i < MAX_V_CODE_TIMING_LEN; i += 2, v_table++) {
    412  1.1  riastrad 		tmp = ((uint32_t)tv_dac->tv.v_code_timing[i+1] << 14) | ((uint32_t)tv_dac->tv.v_code_timing[i]);
    413  1.1  riastrad 		radeon_legacy_tv_write_fifo(radeon_encoder, v_table, tmp);
    414  1.1  riastrad 		if (tv_dac->tv.v_code_timing[i] == 0 || tv_dac->tv.v_code_timing[i + 1] == 0)
    415  1.1  riastrad 			break;
    416  1.1  riastrad 	}
    417  1.1  riastrad }
    418  1.1  riastrad 
    419  1.1  riastrad static void radeon_legacy_write_tv_restarts(struct radeon_encoder *radeon_encoder)
    420  1.1  riastrad {
    421  1.1  riastrad 	struct drm_device *dev = radeon_encoder->base.dev;
    422  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    423  1.1  riastrad 	struct radeon_encoder_tv_dac *tv_dac = radeon_encoder->enc_priv;
    424  1.1  riastrad 	WREG32(RADEON_TV_FRESTART, tv_dac->tv.frestart);
    425  1.1  riastrad 	WREG32(RADEON_TV_HRESTART, tv_dac->tv.hrestart);
    426  1.1  riastrad 	WREG32(RADEON_TV_VRESTART, tv_dac->tv.vrestart);
    427  1.1  riastrad }
    428  1.1  riastrad 
    429  1.1  riastrad static bool radeon_legacy_tv_init_restarts(struct drm_encoder *encoder)
    430  1.1  riastrad {
    431  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    432  1.1  riastrad 	struct radeon_encoder_tv_dac *tv_dac = radeon_encoder->enc_priv;
    433  1.1  riastrad 	int restart;
    434  1.1  riastrad 	unsigned int h_total, v_total, f_total;
    435  1.1  riastrad 	int v_offset, h_offset;
    436  1.1  riastrad 	u16 p1, p2, h_inc;
    437  1.1  riastrad 	bool h_changed;
    438  1.1  riastrad 	const struct radeon_tv_mode_constants *const_ptr;
    439  1.1  riastrad 
    440  1.1  riastrad 	const_ptr = radeon_legacy_tv_get_std_mode(radeon_encoder, NULL);
    441  1.1  riastrad 	if (!const_ptr)
    442  1.1  riastrad 		return false;
    443  1.1  riastrad 
    444  1.1  riastrad 	h_total = const_ptr->hor_total;
    445  1.1  riastrad 	v_total = const_ptr->ver_total;
    446  1.1  riastrad 
    447  1.1  riastrad 	if (tv_dac->tv_std == TV_STD_NTSC ||
    448  1.1  riastrad 	    tv_dac->tv_std == TV_STD_NTSC_J ||
    449  1.1  riastrad 	    tv_dac->tv_std == TV_STD_PAL_M ||
    450  1.1  riastrad 	    tv_dac->tv_std == TV_STD_PAL_60)
    451  1.1  riastrad 		f_total = NTSC_TV_VFTOTAL + 1;
    452  1.1  riastrad 	else
    453  1.1  riastrad 		f_total = PAL_TV_VFTOTAL + 1;
    454  1.1  riastrad 
    455  1.1  riastrad 	/* adjust positions 1&2 in hor. cod timing table */
    456  1.1  riastrad 	h_offset = tv_dac->h_pos * H_POS_UNIT;
    457  1.1  riastrad 
    458  1.1  riastrad 	if (tv_dac->tv_std == TV_STD_NTSC ||
    459  1.1  riastrad 	    tv_dac->tv_std == TV_STD_NTSC_J ||
    460  1.1  riastrad 	    tv_dac->tv_std == TV_STD_PAL_M) {
    461  1.1  riastrad 		h_offset -= 50;
    462  1.1  riastrad 		p1 = hor_timing_NTSC[H_TABLE_POS1];
    463  1.1  riastrad 		p2 = hor_timing_NTSC[H_TABLE_POS2];
    464  1.1  riastrad 	} else {
    465  1.1  riastrad 		p1 = hor_timing_PAL[H_TABLE_POS1];
    466  1.1  riastrad 		p2 = hor_timing_PAL[H_TABLE_POS2];
    467  1.1  riastrad 	}
    468  1.1  riastrad 
    469  1.1  riastrad 	p1 = (u16)((int)p1 + h_offset);
    470  1.1  riastrad 	p2 = (u16)((int)p2 - h_offset);
    471  1.1  riastrad 
    472  1.1  riastrad 	h_changed = (p1 != tv_dac->tv.h_code_timing[H_TABLE_POS1] ||
    473  1.1  riastrad 		     p2 != tv_dac->tv.h_code_timing[H_TABLE_POS2]);
    474  1.1  riastrad 
    475  1.1  riastrad 	tv_dac->tv.h_code_timing[H_TABLE_POS1] = p1;
    476  1.1  riastrad 	tv_dac->tv.h_code_timing[H_TABLE_POS2] = p2;
    477  1.1  riastrad 
    478  1.1  riastrad 	/* Convert hOffset from n. of TV clock periods to n. of CRTC clock periods (CRTC pixels) */
    479  1.1  riastrad 	h_offset = (h_offset * (int)(const_ptr->pix_to_tv)) / 1000;
    480  1.1  riastrad 
    481  1.1  riastrad 	/* adjust restart */
    482  1.1  riastrad 	restart = const_ptr->def_restart;
    483  1.1  riastrad 
    484  1.1  riastrad 	/*
    485  1.1  riastrad 	 * convert v_pos TV lines to n. of CRTC pixels
    486  1.1  riastrad 	 */
    487  1.1  riastrad 	if (tv_dac->tv_std == TV_STD_NTSC ||
    488  1.1  riastrad 	    tv_dac->tv_std == TV_STD_NTSC_J ||
    489  1.1  riastrad 	    tv_dac->tv_std == TV_STD_PAL_M ||
    490  1.1  riastrad 	    tv_dac->tv_std == TV_STD_PAL_60)
    491  1.1  riastrad 		v_offset = ((int)(v_total * h_total) * 2 * tv_dac->v_pos) / (int)(NTSC_TV_LINES_PER_FRAME);
    492  1.1  riastrad 	else
    493  1.1  riastrad 		v_offset = ((int)(v_total * h_total) * 2 * tv_dac->v_pos) / (int)(PAL_TV_LINES_PER_FRAME);
    494  1.1  riastrad 
    495  1.1  riastrad 	restart -= v_offset + h_offset;
    496  1.1  riastrad 
    497  1.1  riastrad 	DRM_DEBUG_KMS("compute_restarts: def = %u h = %d v = %d, p1 = %04x, p2 = %04x, restart = %d\n",
    498  1.1  riastrad 		  const_ptr->def_restart, tv_dac->h_pos, tv_dac->v_pos, p1, p2, restart);
    499  1.1  riastrad 
    500  1.1  riastrad 	tv_dac->tv.hrestart = restart % h_total;
    501  1.1  riastrad 	restart /= h_total;
    502  1.1  riastrad 	tv_dac->tv.vrestart = restart % v_total;
    503  1.1  riastrad 	restart /= v_total;
    504  1.1  riastrad 	tv_dac->tv.frestart = restart % f_total;
    505  1.1  riastrad 
    506  1.1  riastrad 	DRM_DEBUG_KMS("compute_restart: F/H/V=%u,%u,%u\n",
    507  1.1  riastrad 		  (unsigned)tv_dac->tv.frestart,
    508  1.1  riastrad 		  (unsigned)tv_dac->tv.vrestart,
    509  1.1  riastrad 		  (unsigned)tv_dac->tv.hrestart);
    510  1.1  riastrad 
    511  1.1  riastrad 	/* compute h_inc from hsize */
    512  1.1  riastrad 	if (tv_dac->tv_std == TV_STD_NTSC ||
    513  1.1  riastrad 	    tv_dac->tv_std == TV_STD_NTSC_J ||
    514  1.1  riastrad 	    tv_dac->tv_std == TV_STD_PAL_M)
    515  1.1  riastrad 		h_inc = (u16)((int)(const_ptr->hor_resolution * 4096 * NTSC_TV_CLOCK_T) /
    516  1.1  riastrad 			      (tv_dac->h_size * (int)(NTSC_TV_H_SIZE_UNIT) + (int)(NTSC_TV_ZERO_H_SIZE)));
    517  1.1  riastrad 	else
    518  1.1  riastrad 		h_inc = (u16)((int)(const_ptr->hor_resolution * 4096 * PAL_TV_CLOCK_T) /
    519  1.1  riastrad 			      (tv_dac->h_size * (int)(PAL_TV_H_SIZE_UNIT) + (int)(PAL_TV_ZERO_H_SIZE)));
    520  1.1  riastrad 
    521  1.1  riastrad 	tv_dac->tv.timing_cntl = (tv_dac->tv.timing_cntl & ~RADEON_H_INC_MASK) |
    522  1.1  riastrad 		((u32)h_inc << RADEON_H_INC_SHIFT);
    523  1.1  riastrad 
    524  1.1  riastrad 	DRM_DEBUG_KMS("compute_restart: h_size = %d h_inc = %d\n", tv_dac->h_size, h_inc);
    525  1.1  riastrad 
    526  1.1  riastrad 	return h_changed;
    527  1.1  riastrad }
    528  1.1  riastrad 
    529  1.1  riastrad void radeon_legacy_tv_mode_set(struct drm_encoder *encoder,
    530  1.1  riastrad 			       struct drm_display_mode *mode,
    531  1.1  riastrad 			       struct drm_display_mode *adjusted_mode)
    532  1.1  riastrad {
    533  1.1  riastrad 	struct drm_device *dev = encoder->dev;
    534  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    535  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    536  1.1  riastrad 	struct radeon_encoder_tv_dac *tv_dac = radeon_encoder->enc_priv;
    537  1.1  riastrad 	const struct radeon_tv_mode_constants *const_ptr;
    538  1.1  riastrad 	struct radeon_crtc *radeon_crtc;
    539  1.1  riastrad 	int i;
    540  1.1  riastrad 	uint16_t pll_ref_freq;
    541  1.1  riastrad 	uint32_t vert_space, flicker_removal, tmp;
    542  1.1  riastrad 	uint32_t tv_master_cntl, tv_rgb_cntl, tv_dac_cntl;
    543  1.1  riastrad 	uint32_t tv_modulator_cntl1, tv_modulator_cntl2;
    544  1.1  riastrad 	uint32_t tv_vscaler_cntl1, tv_vscaler_cntl2;
    545  1.4  riastrad 	uint32_t tv_pll_cntl, tv_ftotal;
    546  1.1  riastrad 	uint32_t tv_y_fall_cntl, tv_y_rise_cntl, tv_y_saw_tooth_cntl;
    547  1.1  riastrad 	uint32_t m, n, p;
    548  1.1  riastrad 	const uint16_t *hor_timing;
    549  1.1  riastrad 	const uint16_t *vert_timing;
    550  1.1  riastrad 
    551  1.1  riastrad 	const_ptr = radeon_legacy_tv_get_std_mode(radeon_encoder, &pll_ref_freq);
    552  1.1  riastrad 	if (!const_ptr)
    553  1.1  riastrad 		return;
    554  1.1  riastrad 
    555  1.1  riastrad 	radeon_crtc = to_radeon_crtc(encoder->crtc);
    556  1.1  riastrad 
    557  1.1  riastrad 	tv_master_cntl = (RADEON_VIN_ASYNC_RST |
    558  1.1  riastrad 			  RADEON_CRT_FIFO_CE_EN |
    559  1.1  riastrad 			  RADEON_TV_FIFO_CE_EN |
    560  1.1  riastrad 			  RADEON_TV_ON);
    561  1.1  riastrad 
    562  1.1  riastrad 	if (!ASIC_IS_R300(rdev))
    563  1.1  riastrad 		tv_master_cntl |= RADEON_TVCLK_ALWAYS_ONb;
    564  1.1  riastrad 
    565  1.1  riastrad 	if (tv_dac->tv_std == TV_STD_NTSC ||
    566  1.1  riastrad 	    tv_dac->tv_std == TV_STD_NTSC_J)
    567  1.1  riastrad 		tv_master_cntl |= RADEON_RESTART_PHASE_FIX;
    568  1.1  riastrad 
    569  1.1  riastrad 	tv_modulator_cntl1 = (RADEON_SLEW_RATE_LIMIT |
    570  1.1  riastrad 			      RADEON_SYNC_TIP_LEVEL |
    571  1.1  riastrad 			      RADEON_YFLT_EN |
    572  1.1  riastrad 			      RADEON_UVFLT_EN |
    573  1.1  riastrad 			      (6 << RADEON_CY_FILT_BLEND_SHIFT));
    574  1.1  riastrad 
    575  1.1  riastrad 	if (tv_dac->tv_std == TV_STD_NTSC ||
    576  1.1  riastrad 	    tv_dac->tv_std == TV_STD_NTSC_J) {
    577  1.1  riastrad 		tv_modulator_cntl1 |= (0x46 << RADEON_SET_UP_LEVEL_SHIFT) |
    578  1.1  riastrad 			(0x3b << RADEON_BLANK_LEVEL_SHIFT);
    579  1.1  riastrad 		tv_modulator_cntl2 = (-111 & RADEON_TV_U_BURST_LEVEL_MASK) |
    580  1.1  riastrad 			((0 & RADEON_TV_V_BURST_LEVEL_MASK) << RADEON_TV_V_BURST_LEVEL_SHIFT);
    581  1.1  riastrad 	} else if (tv_dac->tv_std == TV_STD_SCART_PAL) {
    582  1.1  riastrad 		tv_modulator_cntl1 |= RADEON_ALT_PHASE_EN;
    583  1.1  riastrad 		tv_modulator_cntl2 = (0 & RADEON_TV_U_BURST_LEVEL_MASK) |
    584  1.1  riastrad 			((0 & RADEON_TV_V_BURST_LEVEL_MASK) << RADEON_TV_V_BURST_LEVEL_SHIFT);
    585  1.1  riastrad 	} else {
    586  1.1  riastrad 		tv_modulator_cntl1 |= RADEON_ALT_PHASE_EN |
    587  1.1  riastrad 			(0x3b << RADEON_SET_UP_LEVEL_SHIFT) |
    588  1.1  riastrad 			(0x3b << RADEON_BLANK_LEVEL_SHIFT);
    589  1.1  riastrad 		tv_modulator_cntl2 = (-78 & RADEON_TV_U_BURST_LEVEL_MASK) |
    590  1.1  riastrad 			((62 & RADEON_TV_V_BURST_LEVEL_MASK) << RADEON_TV_V_BURST_LEVEL_SHIFT);
    591  1.1  riastrad 	}
    592  1.1  riastrad 
    593  1.1  riastrad 
    594  1.1  riastrad 	tv_rgb_cntl = (RADEON_RGB_DITHER_EN
    595  1.1  riastrad 		       | RADEON_TVOUT_SCALE_EN
    596  1.1  riastrad 		       | (0x0b << RADEON_UVRAM_READ_MARGIN_SHIFT)
    597  1.1  riastrad 		       | (0x07 << RADEON_FIFORAM_FFMACRO_READ_MARGIN_SHIFT)
    598  1.1  riastrad 		       | RADEON_RGB_ATTEN_SEL(0x3)
    599  1.1  riastrad 		       | RADEON_RGB_ATTEN_VAL(0xc));
    600  1.1  riastrad 
    601  1.1  riastrad 	if (radeon_crtc->crtc_id == 1)
    602  1.1  riastrad 		tv_rgb_cntl |= RADEON_RGB_SRC_SEL_CRTC2;
    603  1.1  riastrad 	else {
    604  1.1  riastrad 		if (radeon_crtc->rmx_type != RMX_OFF)
    605  1.1  riastrad 			tv_rgb_cntl |= RADEON_RGB_SRC_SEL_RMX;
    606  1.1  riastrad 		else
    607  1.1  riastrad 			tv_rgb_cntl |= RADEON_RGB_SRC_SEL_CRTC1;
    608  1.1  riastrad 	}
    609  1.1  riastrad 
    610  1.1  riastrad 	if (tv_dac->tv_std == TV_STD_NTSC ||
    611  1.1  riastrad 	    tv_dac->tv_std == TV_STD_NTSC_J ||
    612  1.1  riastrad 	    tv_dac->tv_std == TV_STD_PAL_M ||
    613  1.1  riastrad 	    tv_dac->tv_std == TV_STD_PAL_60)
    614  1.1  riastrad 		vert_space = const_ptr->ver_total * 2 * 10000 / NTSC_TV_LINES_PER_FRAME;
    615  1.1  riastrad 	else
    616  1.1  riastrad 		vert_space = const_ptr->ver_total * 2 * 10000 / PAL_TV_LINES_PER_FRAME;
    617  1.1  riastrad 
    618  1.1  riastrad 	tmp = RREG32(RADEON_TV_VSCALER_CNTL1);
    619  1.1  riastrad 	tmp &= 0xe3ff0000;
    620  1.1  riastrad 	tmp |= (vert_space * (1 << FRAC_BITS) / 10000);
    621  1.1  riastrad 	tv_vscaler_cntl1 = tmp;
    622  1.1  riastrad 
    623  1.1  riastrad 	if (pll_ref_freq == 2700)
    624  1.1  riastrad 		tv_vscaler_cntl1 |= RADEON_RESTART_FIELD;
    625  1.1  riastrad 
    626  1.1  riastrad 	if (const_ptr->hor_resolution == 1024)
    627  1.1  riastrad 		tv_vscaler_cntl1 |= (4 << RADEON_Y_DEL_W_SIG_SHIFT);
    628  1.1  riastrad 	else
    629  1.1  riastrad 		tv_vscaler_cntl1 |= (2 << RADEON_Y_DEL_W_SIG_SHIFT);
    630  1.1  riastrad 
    631  1.1  riastrad 	/* scale up for int divide */
    632  1.1  riastrad 	tmp = const_ptr->ver_total * 2 * 1000;
    633  1.1  riastrad 	if (tv_dac->tv_std == TV_STD_NTSC ||
    634  1.1  riastrad 	    tv_dac->tv_std == TV_STD_NTSC_J ||
    635  1.1  riastrad 	    tv_dac->tv_std == TV_STD_PAL_M ||
    636  1.1  riastrad 	    tv_dac->tv_std == TV_STD_PAL_60) {
    637  1.1  riastrad 		tmp /= NTSC_TV_LINES_PER_FRAME;
    638  1.1  riastrad 	} else {
    639  1.1  riastrad 		tmp /= PAL_TV_LINES_PER_FRAME;
    640  1.1  riastrad 	}
    641  1.1  riastrad 	flicker_removal = (tmp + 500) / 1000;
    642  1.1  riastrad 
    643  1.1  riastrad 	if (flicker_removal < 3)
    644  1.1  riastrad 		flicker_removal = 3;
    645  1.1  riastrad 	for (i = 0; i < ARRAY_SIZE(SLOPE_limit); ++i) {
    646  1.1  riastrad 		if (flicker_removal == SLOPE_limit[i])
    647  1.1  riastrad 			break;
    648  1.1  riastrad 	}
    649  1.1  riastrad 
    650  1.1  riastrad 	tv_y_saw_tooth_cntl = (vert_space * SLOPE_value[i] * (1 << (FRAC_BITS - 1)) +
    651  1.1  riastrad 				5001) / 10000 / 8 | ((SLOPE_value[i] *
    652  1.1  riastrad 				(1 << (FRAC_BITS - 1)) / 8) << 16);
    653  1.1  riastrad 	tv_y_fall_cntl =
    654  1.1  riastrad 		(YCOEF_EN_value[i] << 17) | ((YCOEF_value[i] * (1 << 8) / 8) << 24) |
    655  1.1  riastrad 		RADEON_Y_FALL_PING_PONG | (272 * SLOPE_value[i] / 8) * (1 << (FRAC_BITS - 1)) /
    656  1.1  riastrad 		1024;
    657  1.1  riastrad 	tv_y_rise_cntl = RADEON_Y_RISE_PING_PONG|
    658  1.1  riastrad 		(flicker_removal * 1024 - 272) * SLOPE_value[i] / 8 * (1 << (FRAC_BITS - 1)) / 1024;
    659  1.1  riastrad 
    660  1.1  riastrad 	tv_vscaler_cntl2 = RREG32(RADEON_TV_VSCALER_CNTL2) & 0x00fffff0;
    661  1.1  riastrad 	tv_vscaler_cntl2 |= (0x10 << 24) |
    662  1.1  riastrad 		RADEON_DITHER_MODE |
    663  1.1  riastrad 		RADEON_Y_OUTPUT_DITHER_EN |
    664  1.1  riastrad 		RADEON_UV_OUTPUT_DITHER_EN |
    665  1.1  riastrad 		RADEON_UV_TO_BUF_DITHER_EN;
    666  1.1  riastrad 
    667  1.1  riastrad 	tmp = (tv_vscaler_cntl1 >> RADEON_UV_INC_SHIFT) & RADEON_UV_INC_MASK;
    668  1.1  riastrad 	tmp = ((16384 * 256 * 10) / tmp + 5) / 10;
    669  1.1  riastrad 	tmp = (tmp << RADEON_UV_OUTPUT_POST_SCALE_SHIFT) | 0x000b0000;
    670  1.1  riastrad 	tv_dac->tv.timing_cntl = tmp;
    671  1.1  riastrad 
    672  1.1  riastrad 	if (tv_dac->tv_std == TV_STD_NTSC ||
    673  1.1  riastrad 	    tv_dac->tv_std == TV_STD_NTSC_J ||
    674  1.1  riastrad 	    tv_dac->tv_std == TV_STD_PAL_M ||
    675  1.1  riastrad 	    tv_dac->tv_std == TV_STD_PAL_60)
    676  1.1  riastrad 		tv_dac_cntl = tv_dac->ntsc_tvdac_adj;
    677  1.1  riastrad 	else
    678  1.1  riastrad 		tv_dac_cntl = tv_dac->pal_tvdac_adj;
    679  1.1  riastrad 
    680  1.1  riastrad 	tv_dac_cntl |= RADEON_TV_DAC_NBLANK | RADEON_TV_DAC_NHOLD;
    681  1.1  riastrad 
    682  1.1  riastrad 	if (tv_dac->tv_std == TV_STD_NTSC ||
    683  1.1  riastrad 	    tv_dac->tv_std == TV_STD_NTSC_J)
    684  1.1  riastrad 		tv_dac_cntl |= RADEON_TV_DAC_STD_NTSC;
    685  1.1  riastrad 	else
    686  1.1  riastrad 		tv_dac_cntl |= RADEON_TV_DAC_STD_PAL;
    687  1.1  riastrad 
    688  1.1  riastrad 	if (tv_dac->tv_std == TV_STD_NTSC ||
    689  1.1  riastrad 	    tv_dac->tv_std == TV_STD_NTSC_J) {
    690  1.1  riastrad 		if (pll_ref_freq == 2700) {
    691  1.1  riastrad 			m = NTSC_TV_PLL_M_27;
    692  1.1  riastrad 			n = NTSC_TV_PLL_N_27;
    693  1.1  riastrad 			p = NTSC_TV_PLL_P_27;
    694  1.1  riastrad 		} else {
    695  1.1  riastrad 			m = NTSC_TV_PLL_M_14;
    696  1.1  riastrad 			n = NTSC_TV_PLL_N_14;
    697  1.1  riastrad 			p = NTSC_TV_PLL_P_14;
    698  1.1  riastrad 		}
    699  1.1  riastrad 	} else {
    700  1.1  riastrad 		if (pll_ref_freq == 2700) {
    701  1.1  riastrad 			m = PAL_TV_PLL_M_27;
    702  1.1  riastrad 			n = PAL_TV_PLL_N_27;
    703  1.1  riastrad 			p = PAL_TV_PLL_P_27;
    704  1.1  riastrad 		} else {
    705  1.1  riastrad 			m = PAL_TV_PLL_M_14;
    706  1.1  riastrad 			n = PAL_TV_PLL_N_14;
    707  1.1  riastrad 			p = PAL_TV_PLL_P_14;
    708  1.1  riastrad 		}
    709  1.1  riastrad 	}
    710  1.1  riastrad 
    711  1.1  riastrad 	tv_pll_cntl = (m & RADEON_TV_M0LO_MASK) |
    712  1.1  riastrad 		(((m >> 8) & RADEON_TV_M0HI_MASK) << RADEON_TV_M0HI_SHIFT) |
    713  1.1  riastrad 		((n & RADEON_TV_N0LO_MASK) << RADEON_TV_N0LO_SHIFT) |
    714  1.1  riastrad 		(((n >> 9) & RADEON_TV_N0HI_MASK) << RADEON_TV_N0HI_SHIFT) |
    715  1.1  riastrad 		((p & RADEON_TV_P_MASK) << RADEON_TV_P_SHIFT);
    716  1.1  riastrad 
    717  1.1  riastrad 	tv_dac->tv.tv_uv_adr = 0xc8;
    718  1.1  riastrad 
    719  1.1  riastrad 	if (tv_dac->tv_std == TV_STD_NTSC ||
    720  1.1  riastrad 	    tv_dac->tv_std == TV_STD_NTSC_J ||
    721  1.1  riastrad 	    tv_dac->tv_std == TV_STD_PAL_M ||
    722  1.1  riastrad 	    tv_dac->tv_std == TV_STD_PAL_60) {
    723  1.1  riastrad 		tv_ftotal = NTSC_TV_VFTOTAL;
    724  1.1  riastrad 		hor_timing = hor_timing_NTSC;
    725  1.1  riastrad 		vert_timing = vert_timing_NTSC;
    726  1.1  riastrad 	} else {
    727  1.1  riastrad 		hor_timing = hor_timing_PAL;
    728  1.1  riastrad 		vert_timing = vert_timing_PAL;
    729  1.1  riastrad 		tv_ftotal = PAL_TV_VFTOTAL;
    730  1.1  riastrad 	}
    731  1.1  riastrad 
    732  1.1  riastrad 	for (i = 0; i < MAX_H_CODE_TIMING_LEN; i++) {
    733  1.1  riastrad 		if ((tv_dac->tv.h_code_timing[i] = hor_timing[i]) == 0)
    734  1.1  riastrad 			break;
    735  1.1  riastrad 	}
    736  1.1  riastrad 
    737  1.1  riastrad 	for (i = 0; i < MAX_V_CODE_TIMING_LEN; i++) {
    738  1.1  riastrad 		if ((tv_dac->tv.v_code_timing[i] = vert_timing[i]) == 0)
    739  1.1  riastrad 			break;
    740  1.1  riastrad 	}
    741  1.1  riastrad 
    742  1.1  riastrad 	radeon_legacy_tv_init_restarts(encoder);
    743  1.1  riastrad 
    744  1.1  riastrad 	/* play with DAC_CNTL */
    745  1.1  riastrad 	/* play with GPIOPAD_A */
    746  1.1  riastrad 	/* DISP_OUTPUT_CNTL */
    747  1.1  riastrad 	/* use reference freq */
    748  1.1  riastrad 
    749  1.1  riastrad 	/* program the TV registers */
    750  1.1  riastrad 	WREG32(RADEON_TV_MASTER_CNTL, (tv_master_cntl | RADEON_TV_ASYNC_RST |
    751  1.1  riastrad 				       RADEON_CRT_ASYNC_RST | RADEON_TV_FIFO_ASYNC_RST));
    752  1.1  riastrad 
    753  1.1  riastrad 	tmp = RREG32(RADEON_TV_DAC_CNTL);
    754  1.1  riastrad 	tmp &= ~RADEON_TV_DAC_NBLANK;
    755  1.1  riastrad 	tmp |= RADEON_TV_DAC_BGSLEEP |
    756  1.1  riastrad 		RADEON_TV_DAC_RDACPD |
    757  1.1  riastrad 		RADEON_TV_DAC_GDACPD |
    758  1.1  riastrad 		RADEON_TV_DAC_BDACPD;
    759  1.1  riastrad 	WREG32(RADEON_TV_DAC_CNTL, tmp);
    760  1.1  riastrad 
    761  1.1  riastrad 	/* TV PLL */
    762  1.1  riastrad 	WREG32_PLL_P(RADEON_TV_PLL_CNTL1, 0, ~RADEON_TVCLK_SRC_SEL_TVPLL);
    763  1.1  riastrad 	WREG32_PLL(RADEON_TV_PLL_CNTL, tv_pll_cntl);
    764  1.1  riastrad 	WREG32_PLL_P(RADEON_TV_PLL_CNTL1, RADEON_TVPLL_RESET, ~RADEON_TVPLL_RESET);
    765  1.1  riastrad 
    766  1.1  riastrad 	radeon_wait_pll_lock(encoder, 200, 800, 135);
    767  1.1  riastrad 
    768  1.1  riastrad 	WREG32_PLL_P(RADEON_TV_PLL_CNTL1, 0, ~RADEON_TVPLL_RESET);
    769  1.1  riastrad 
    770  1.1  riastrad 	radeon_wait_pll_lock(encoder, 300, 160, 27);
    771  1.1  riastrad 	radeon_wait_pll_lock(encoder, 200, 800, 135);
    772  1.1  riastrad 
    773  1.1  riastrad 	WREG32_PLL_P(RADEON_TV_PLL_CNTL1, 0, ~0xf);
    774  1.1  riastrad 	WREG32_PLL_P(RADEON_TV_PLL_CNTL1, RADEON_TVCLK_SRC_SEL_TVPLL, ~RADEON_TVCLK_SRC_SEL_TVPLL);
    775  1.1  riastrad 
    776  1.1  riastrad 	WREG32_PLL_P(RADEON_TV_PLL_CNTL1, (1 << RADEON_TVPDC_SHIFT), ~RADEON_TVPDC_MASK);
    777  1.1  riastrad 	WREG32_PLL_P(RADEON_TV_PLL_CNTL1, 0, ~RADEON_TVPLL_SLEEP);
    778  1.1  riastrad 
    779  1.1  riastrad 	/* TV HV */
    780  1.1  riastrad 	WREG32(RADEON_TV_RGB_CNTL, tv_rgb_cntl);
    781  1.1  riastrad 	WREG32(RADEON_TV_HTOTAL, const_ptr->hor_total - 1);
    782  1.1  riastrad 	WREG32(RADEON_TV_HDISP, const_ptr->hor_resolution - 1);
    783  1.1  riastrad 	WREG32(RADEON_TV_HSTART, const_ptr->hor_start);
    784  1.1  riastrad 
    785  1.1  riastrad 	WREG32(RADEON_TV_VTOTAL, const_ptr->ver_total - 1);
    786  1.1  riastrad 	WREG32(RADEON_TV_VDISP, const_ptr->ver_resolution - 1);
    787  1.1  riastrad 	WREG32(RADEON_TV_FTOTAL, tv_ftotal);
    788  1.1  riastrad 	WREG32(RADEON_TV_VSCALER_CNTL1, tv_vscaler_cntl1);
    789  1.1  riastrad 	WREG32(RADEON_TV_VSCALER_CNTL2, tv_vscaler_cntl2);
    790  1.1  riastrad 
    791  1.1  riastrad 	WREG32(RADEON_TV_Y_FALL_CNTL, tv_y_fall_cntl);
    792  1.1  riastrad 	WREG32(RADEON_TV_Y_RISE_CNTL, tv_y_rise_cntl);
    793  1.1  riastrad 	WREG32(RADEON_TV_Y_SAW_TOOTH_CNTL, tv_y_saw_tooth_cntl);
    794  1.1  riastrad 
    795  1.1  riastrad 	WREG32(RADEON_TV_MASTER_CNTL, (tv_master_cntl | RADEON_TV_ASYNC_RST |
    796  1.1  riastrad 				       RADEON_CRT_ASYNC_RST));
    797  1.1  riastrad 
    798  1.1  riastrad 	/* TV restarts */
    799  1.1  riastrad 	radeon_legacy_write_tv_restarts(radeon_encoder);
    800  1.1  riastrad 
    801  1.1  riastrad 	/* tv timings */
    802  1.1  riastrad 	radeon_restore_tv_timing_tables(radeon_encoder);
    803  1.1  riastrad 
    804  1.1  riastrad 	WREG32(RADEON_TV_MASTER_CNTL, (tv_master_cntl | RADEON_TV_ASYNC_RST));
    805  1.1  riastrad 
    806  1.1  riastrad 	/* tv std */
    807  1.1  riastrad 	WREG32(RADEON_TV_SYNC_CNTL, (RADEON_SYNC_PUB | RADEON_TV_SYNC_IO_DRIVE));
    808  1.1  riastrad 	WREG32(RADEON_TV_TIMING_CNTL, tv_dac->tv.timing_cntl);
    809  1.1  riastrad 	WREG32(RADEON_TV_MODULATOR_CNTL1, tv_modulator_cntl1);
    810  1.1  riastrad 	WREG32(RADEON_TV_MODULATOR_CNTL2, tv_modulator_cntl2);
    811  1.1  riastrad 	WREG32(RADEON_TV_PRE_DAC_MUX_CNTL, (RADEON_Y_RED_EN |
    812  1.1  riastrad 					    RADEON_C_GRN_EN |
    813  1.1  riastrad 					    RADEON_CMP_BLU_EN |
    814  1.1  riastrad 					    RADEON_DAC_DITHER_EN));
    815  1.1  riastrad 
    816  1.1  riastrad 	WREG32(RADEON_TV_CRC_CNTL, 0);
    817  1.1  riastrad 
    818  1.1  riastrad 	WREG32(RADEON_TV_MASTER_CNTL, tv_master_cntl);
    819  1.1  riastrad 
    820  1.1  riastrad 	WREG32(RADEON_TV_GAIN_LIMIT_SETTINGS, ((0x17f << RADEON_UV_GAIN_LIMIT_SHIFT) |
    821  1.1  riastrad 					       (0x5ff << RADEON_Y_GAIN_LIMIT_SHIFT)));
    822  1.1  riastrad 	WREG32(RADEON_TV_LINEAR_GAIN_SETTINGS, ((0x100 << RADEON_UV_GAIN_SHIFT) |
    823  1.1  riastrad 						(0x100 << RADEON_Y_GAIN_SHIFT)));
    824  1.1  riastrad 
    825  1.1  riastrad 	WREG32(RADEON_TV_DAC_CNTL, tv_dac_cntl);
    826  1.1  riastrad 
    827  1.1  riastrad }
    828  1.1  riastrad 
    829  1.1  riastrad void radeon_legacy_tv_adjust_crtc_reg(struct drm_encoder *encoder,
    830  1.1  riastrad 				      uint32_t *h_total_disp, uint32_t *h_sync_strt_wid,
    831  1.1  riastrad 				      uint32_t *v_total_disp, uint32_t *v_sync_strt_wid)
    832  1.1  riastrad {
    833  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    834  1.1  riastrad 	const struct radeon_tv_mode_constants *const_ptr;
    835  1.1  riastrad 	uint32_t tmp;
    836  1.1  riastrad 
    837  1.1  riastrad 	const_ptr = radeon_legacy_tv_get_std_mode(radeon_encoder, NULL);
    838  1.1  riastrad 	if (!const_ptr)
    839  1.1  riastrad 		return;
    840  1.1  riastrad 
    841  1.1  riastrad 	*h_total_disp = (((const_ptr->hor_resolution / 8) - 1) << RADEON_CRTC_H_DISP_SHIFT) |
    842  1.1  riastrad 		(((const_ptr->hor_total / 8) - 1) << RADEON_CRTC_H_TOTAL_SHIFT);
    843  1.1  riastrad 
    844  1.1  riastrad 	tmp = *h_sync_strt_wid;
    845  1.1  riastrad 	tmp &= ~(RADEON_CRTC_H_SYNC_STRT_PIX | RADEON_CRTC_H_SYNC_STRT_CHAR);
    846  1.1  riastrad 	tmp |= (((const_ptr->hor_syncstart / 8) - 1) << RADEON_CRTC_H_SYNC_STRT_CHAR_SHIFT) |
    847  1.1  riastrad 		(const_ptr->hor_syncstart & 7);
    848  1.1  riastrad 	*h_sync_strt_wid = tmp;
    849  1.1  riastrad 
    850  1.1  riastrad 	*v_total_disp = ((const_ptr->ver_resolution - 1) << RADEON_CRTC_V_DISP_SHIFT) |
    851  1.1  riastrad 		((const_ptr->ver_total - 1) << RADEON_CRTC_V_TOTAL_SHIFT);
    852  1.1  riastrad 
    853  1.1  riastrad 	tmp = *v_sync_strt_wid;
    854  1.1  riastrad 	tmp &= ~RADEON_CRTC_V_SYNC_STRT;
    855  1.1  riastrad 	tmp |= ((const_ptr->ver_syncstart - 1) << RADEON_CRTC_V_SYNC_STRT_SHIFT);
    856  1.1  riastrad 	*v_sync_strt_wid = tmp;
    857  1.1  riastrad }
    858  1.1  riastrad 
    859  1.1  riastrad static int get_post_div(int value)
    860  1.1  riastrad {
    861  1.1  riastrad 	int post_div;
    862  1.1  riastrad 	switch (value) {
    863  1.1  riastrad 	case 1: post_div = 0; break;
    864  1.1  riastrad 	case 2: post_div = 1; break;
    865  1.1  riastrad 	case 3: post_div = 4; break;
    866  1.1  riastrad 	case 4: post_div = 2; break;
    867  1.1  riastrad 	case 6: post_div = 6; break;
    868  1.1  riastrad 	case 8: post_div = 3; break;
    869  1.1  riastrad 	case 12: post_div = 7; break;
    870  1.1  riastrad 	case 16:
    871  1.1  riastrad 	default: post_div = 5; break;
    872  1.1  riastrad 	}
    873  1.1  riastrad 	return post_div;
    874  1.1  riastrad }
    875  1.1  riastrad 
    876  1.1  riastrad void radeon_legacy_tv_adjust_pll1(struct drm_encoder *encoder,
    877  1.1  riastrad 				  uint32_t *htotal_cntl, uint32_t *ppll_ref_div,
    878  1.1  riastrad 				  uint32_t *ppll_div_3, uint32_t *pixclks_cntl)
    879  1.1  riastrad {
    880  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    881  1.1  riastrad 	const struct radeon_tv_mode_constants *const_ptr;
    882  1.1  riastrad 
    883  1.1  riastrad 	const_ptr = radeon_legacy_tv_get_std_mode(radeon_encoder, NULL);
    884  1.1  riastrad 	if (!const_ptr)
    885  1.1  riastrad 		return;
    886  1.1  riastrad 
    887  1.1  riastrad 	*htotal_cntl = (const_ptr->hor_total & 0x7) | RADEON_HTOT_CNTL_VGA_EN;
    888  1.1  riastrad 
    889  1.1  riastrad 	*ppll_ref_div = const_ptr->crtcPLL_M;
    890  1.1  riastrad 
    891  1.1  riastrad 	*ppll_div_3 = (const_ptr->crtcPLL_N & 0x7ff) | (get_post_div(const_ptr->crtcPLL_post_div) << 16);
    892  1.1  riastrad 	*pixclks_cntl &= ~(RADEON_PIX2CLK_SRC_SEL_MASK | RADEON_PIXCLK_TV_SRC_SEL);
    893  1.1  riastrad 	*pixclks_cntl |= RADEON_PIX2CLK_SRC_SEL_P2PLLCLK;
    894  1.1  riastrad }
    895  1.1  riastrad 
    896  1.1  riastrad void radeon_legacy_tv_adjust_pll2(struct drm_encoder *encoder,
    897  1.1  riastrad 				  uint32_t *htotal2_cntl, uint32_t *p2pll_ref_div,
    898  1.1  riastrad 				  uint32_t *p2pll_div_0, uint32_t *pixclks_cntl)
    899  1.1  riastrad {
    900  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    901  1.1  riastrad 	const struct radeon_tv_mode_constants *const_ptr;
    902  1.1  riastrad 
    903  1.1  riastrad 	const_ptr = radeon_legacy_tv_get_std_mode(radeon_encoder, NULL);
    904  1.1  riastrad 	if (!const_ptr)
    905  1.1  riastrad 		return;
    906  1.1  riastrad 
    907  1.1  riastrad 	*htotal2_cntl = (const_ptr->hor_total & 0x7);
    908  1.1  riastrad 
    909  1.1  riastrad 	*p2pll_ref_div = const_ptr->crtcPLL_M;
    910  1.1  riastrad 
    911  1.1  riastrad 	*p2pll_div_0 = (const_ptr->crtcPLL_N & 0x7ff) | (get_post_div(const_ptr->crtcPLL_post_div) << 16);
    912  1.1  riastrad 	*pixclks_cntl &= ~RADEON_PIX2CLK_SRC_SEL_MASK;
    913  1.1  riastrad 	*pixclks_cntl |= RADEON_PIX2CLK_SRC_SEL_P2PLLCLK | RADEON_PIXCLK_TV_SRC_SEL;
    914  1.1  riastrad }
    915  1.1  riastrad 
    916