]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/radeon/radeon_atombios.c
drm/radeon/kms: Add quirk for HIS X1300 board
[karo-tx-linux.git] / drivers / gpu / drm / radeon / radeon_atombios.c
1 /*
2  * Copyright 2007-8 Advanced Micro Devices, Inc.
3  * Copyright 2008 Red Hat Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Authors: Dave Airlie
24  *          Alex Deucher
25  */
26 #include "drmP.h"
27 #include "radeon_drm.h"
28 #include "radeon.h"
29
30 #include "atom.h"
31 #include "atom-bits.h"
32
33 /* from radeon_encoder.c */
34 extern uint32_t
35 radeon_get_encoder_id(struct drm_device *dev, uint32_t supported_device,
36                       uint8_t dac);
37 extern void radeon_link_encoder_connector(struct drm_device *dev);
38 extern void
39 radeon_add_atom_encoder(struct drm_device *dev, uint32_t encoder_id,
40                         uint32_t supported_device);
41
42 /* from radeon_connector.c */
43 extern void
44 radeon_add_atom_connector(struct drm_device *dev,
45                           uint32_t connector_id,
46                           uint32_t supported_device,
47                           int connector_type,
48                           struct radeon_i2c_bus_rec *i2c_bus,
49                           bool linkb, uint32_t igp_lane_info);
50
51 /* from radeon_legacy_encoder.c */
52 extern void
53 radeon_add_legacy_encoder(struct drm_device *dev, uint32_t encoder_id,
54                           uint32_t supported_device);
55
56 union atom_supported_devices {
57         struct _ATOM_SUPPORTED_DEVICES_INFO info;
58         struct _ATOM_SUPPORTED_DEVICES_INFO_2 info_2;
59         struct _ATOM_SUPPORTED_DEVICES_INFO_2d1 info_2d1;
60 };
61
62 static inline struct radeon_i2c_bus_rec radeon_lookup_gpio(struct drm_device
63                                                            *dev, uint8_t id)
64 {
65         struct radeon_device *rdev = dev->dev_private;
66         struct atom_context *ctx = rdev->mode_info.atom_context;
67         ATOM_GPIO_I2C_ASSIGMENT gpio;
68         struct radeon_i2c_bus_rec i2c;
69         int index = GetIndexIntoMasterTable(DATA, GPIO_I2C_Info);
70         struct _ATOM_GPIO_I2C_INFO *i2c_info;
71         uint16_t data_offset;
72
73         memset(&i2c, 0, sizeof(struct radeon_i2c_bus_rec));
74         i2c.valid = false;
75
76         atom_parse_data_header(ctx, index, NULL, NULL, NULL, &data_offset);
77
78         i2c_info = (struct _ATOM_GPIO_I2C_INFO *)(ctx->bios + data_offset);
79
80         gpio = i2c_info->asGPIO_Info[id];
81
82         i2c.mask_clk_reg = le16_to_cpu(gpio.usClkMaskRegisterIndex) * 4;
83         i2c.mask_data_reg = le16_to_cpu(gpio.usDataMaskRegisterIndex) * 4;
84         i2c.put_clk_reg = le16_to_cpu(gpio.usClkEnRegisterIndex) * 4;
85         i2c.put_data_reg = le16_to_cpu(gpio.usDataEnRegisterIndex) * 4;
86         i2c.get_clk_reg = le16_to_cpu(gpio.usClkY_RegisterIndex) * 4;
87         i2c.get_data_reg = le16_to_cpu(gpio.usDataY_RegisterIndex) * 4;
88         i2c.a_clk_reg = le16_to_cpu(gpio.usClkA_RegisterIndex) * 4;
89         i2c.a_data_reg = le16_to_cpu(gpio.usDataA_RegisterIndex) * 4;
90         i2c.mask_clk_mask = (1 << gpio.ucClkMaskShift);
91         i2c.mask_data_mask = (1 << gpio.ucDataMaskShift);
92         i2c.put_clk_mask = (1 << gpio.ucClkEnShift);
93         i2c.put_data_mask = (1 << gpio.ucDataEnShift);
94         i2c.get_clk_mask = (1 << gpio.ucClkY_Shift);
95         i2c.get_data_mask = (1 << gpio.ucDataY_Shift);
96         i2c.a_clk_mask = (1 << gpio.ucClkA_Shift);
97         i2c.a_data_mask = (1 << gpio.ucDataA_Shift);
98         i2c.valid = true;
99
100         return i2c;
101 }
102
103 static bool radeon_atom_apply_quirks(struct drm_device *dev,
104                                      uint32_t supported_device,
105                                      int *connector_type,
106                                      struct radeon_i2c_bus_rec *i2c_bus,
107                                      uint8_t *line_mux)
108 {
109
110         /* Asus M2A-VM HDMI board lists the DVI port as HDMI */
111         if ((dev->pdev->device == 0x791e) &&
112             (dev->pdev->subsystem_vendor == 0x1043) &&
113             (dev->pdev->subsystem_device == 0x826d)) {
114                 if ((*connector_type == DRM_MODE_CONNECTOR_HDMIA) &&
115                     (supported_device == ATOM_DEVICE_DFP3_SUPPORT))
116                         *connector_type = DRM_MODE_CONNECTOR_DVID;
117         }
118
119         /* a-bit f-i90hd - ciaranm on #radeonhd - this board has no DVI */
120         if ((dev->pdev->device == 0x7941) &&
121             (dev->pdev->subsystem_vendor == 0x147b) &&
122             (dev->pdev->subsystem_device == 0x2412)) {
123                 if (*connector_type == DRM_MODE_CONNECTOR_DVII)
124                         return false;
125         }
126
127         /* Falcon NW laptop lists vga ddc line for LVDS */
128         if ((dev->pdev->device == 0x5653) &&
129             (dev->pdev->subsystem_vendor == 0x1462) &&
130             (dev->pdev->subsystem_device == 0x0291)) {
131                 if (*connector_type == DRM_MODE_CONNECTOR_LVDS) {
132                         i2c_bus->valid = false;
133                         *line_mux = 53;
134                 }
135         }
136
137         /* HIS X1300 is DVI+VGA, not DVI+DVI */
138         if ((dev->pdev->device == 0x7146) &&
139             (dev->pdev->subsystem_vendor == 0x17af) &&
140             (dev->pdev->subsystem_device == 0x2058)) {
141                 if (supported_device == ATOM_DEVICE_DFP1_SUPPORT)
142                         return false;
143         }
144
145         /* Funky macbooks */
146         if ((dev->pdev->device == 0x71C5) &&
147             (dev->pdev->subsystem_vendor == 0x106b) &&
148             (dev->pdev->subsystem_device == 0x0080)) {
149                 if ((supported_device == ATOM_DEVICE_CRT1_SUPPORT) ||
150                     (supported_device == ATOM_DEVICE_DFP2_SUPPORT))
151                         return false;
152         }
153
154         /* some BIOSes seem to report DAC on HDMI - they hurt me with their lies */
155         if ((*connector_type == DRM_MODE_CONNECTOR_HDMIA) ||
156             (*connector_type == DRM_MODE_CONNECTOR_HDMIB)) {
157                 if (supported_device & (ATOM_DEVICE_CRT_SUPPORT)) {
158                         return false;
159                 }
160         }
161
162         /* ASUS HD 3600 XT board lists the DVI port as HDMI */
163         if ((dev->pdev->device == 0x9598) &&
164             (dev->pdev->subsystem_vendor == 0x1043) &&
165             (dev->pdev->subsystem_device == 0x01da)) {
166                 if (*connector_type == DRM_MODE_CONNECTOR_HDMIB) {
167                         *connector_type = DRM_MODE_CONNECTOR_DVID;
168                 }
169         }
170
171         return true;
172 }
173
174 const int supported_devices_connector_convert[] = {
175         DRM_MODE_CONNECTOR_Unknown,
176         DRM_MODE_CONNECTOR_VGA,
177         DRM_MODE_CONNECTOR_DVII,
178         DRM_MODE_CONNECTOR_DVID,
179         DRM_MODE_CONNECTOR_DVIA,
180         DRM_MODE_CONNECTOR_SVIDEO,
181         DRM_MODE_CONNECTOR_Composite,
182         DRM_MODE_CONNECTOR_LVDS,
183         DRM_MODE_CONNECTOR_Unknown,
184         DRM_MODE_CONNECTOR_Unknown,
185         DRM_MODE_CONNECTOR_HDMIA,
186         DRM_MODE_CONNECTOR_HDMIB,
187         DRM_MODE_CONNECTOR_Unknown,
188         DRM_MODE_CONNECTOR_Unknown,
189         DRM_MODE_CONNECTOR_9PinDIN,
190         DRM_MODE_CONNECTOR_DisplayPort
191 };
192
193 const int object_connector_convert[] = {
194         DRM_MODE_CONNECTOR_Unknown,
195         DRM_MODE_CONNECTOR_DVII,
196         DRM_MODE_CONNECTOR_DVII,
197         DRM_MODE_CONNECTOR_DVID,
198         DRM_MODE_CONNECTOR_DVID,
199         DRM_MODE_CONNECTOR_VGA,
200         DRM_MODE_CONNECTOR_Composite,
201         DRM_MODE_CONNECTOR_SVIDEO,
202         DRM_MODE_CONNECTOR_Unknown,
203         DRM_MODE_CONNECTOR_9PinDIN,
204         DRM_MODE_CONNECTOR_Unknown,
205         DRM_MODE_CONNECTOR_HDMIA,
206         DRM_MODE_CONNECTOR_HDMIB,
207         DRM_MODE_CONNECTOR_HDMIB,
208         DRM_MODE_CONNECTOR_LVDS,
209         DRM_MODE_CONNECTOR_9PinDIN,
210         DRM_MODE_CONNECTOR_Unknown,
211         DRM_MODE_CONNECTOR_Unknown,
212         DRM_MODE_CONNECTOR_Unknown,
213         DRM_MODE_CONNECTOR_DisplayPort
214 };
215
216 bool radeon_get_atom_connector_info_from_object_table(struct drm_device *dev)
217 {
218         struct radeon_device *rdev = dev->dev_private;
219         struct radeon_mode_info *mode_info = &rdev->mode_info;
220         struct atom_context *ctx = mode_info->atom_context;
221         int index = GetIndexIntoMasterTable(DATA, Object_Header);
222         uint16_t size, data_offset;
223         uint8_t frev, crev, line_mux = 0;
224         ATOM_CONNECTOR_OBJECT_TABLE *con_obj;
225         ATOM_DISPLAY_OBJECT_PATH_TABLE *path_obj;
226         ATOM_OBJECT_HEADER *obj_header;
227         int i, j, path_size, device_support;
228         int connector_type;
229         uint16_t igp_lane_info;
230         bool linkb;
231         struct radeon_i2c_bus_rec ddc_bus;
232
233         atom_parse_data_header(ctx, index, &size, &frev, &crev, &data_offset);
234
235         if (data_offset == 0)
236                 return false;
237
238         if (crev < 2)
239                 return false;
240
241         obj_header = (ATOM_OBJECT_HEADER *) (ctx->bios + data_offset);
242         path_obj = (ATOM_DISPLAY_OBJECT_PATH_TABLE *)
243             (ctx->bios + data_offset +
244              le16_to_cpu(obj_header->usDisplayPathTableOffset));
245         con_obj = (ATOM_CONNECTOR_OBJECT_TABLE *)
246             (ctx->bios + data_offset +
247              le16_to_cpu(obj_header->usConnectorObjectTableOffset));
248         device_support = le16_to_cpu(obj_header->usDeviceSupport);
249
250         path_size = 0;
251         for (i = 0; i < path_obj->ucNumOfDispPath; i++) {
252                 uint8_t *addr = (uint8_t *) path_obj->asDispPath;
253                 ATOM_DISPLAY_OBJECT_PATH *path;
254                 addr += path_size;
255                 path = (ATOM_DISPLAY_OBJECT_PATH *) addr;
256                 path_size += le16_to_cpu(path->usSize);
257                 linkb = false;
258
259                 if (device_support & le16_to_cpu(path->usDeviceTag)) {
260                         uint8_t con_obj_id, con_obj_num, con_obj_type;
261
262                         con_obj_id =
263                             (le16_to_cpu(path->usConnObjectId) & OBJECT_ID_MASK)
264                             >> OBJECT_ID_SHIFT;
265                         con_obj_num =
266                             (le16_to_cpu(path->usConnObjectId) & ENUM_ID_MASK)
267                             >> ENUM_ID_SHIFT;
268                         con_obj_type =
269                             (le16_to_cpu(path->usConnObjectId) &
270                              OBJECT_TYPE_MASK) >> OBJECT_TYPE_SHIFT;
271
272                         if ((le16_to_cpu(path->usDeviceTag) ==
273                              ATOM_DEVICE_TV1_SUPPORT)
274                             || (le16_to_cpu(path->usDeviceTag) ==
275                                 ATOM_DEVICE_TV2_SUPPORT)
276                             || (le16_to_cpu(path->usDeviceTag) ==
277                                 ATOM_DEVICE_CV_SUPPORT))
278                                 continue;
279
280                         if ((rdev->family == CHIP_RS780) &&
281                             (con_obj_id ==
282                              CONNECTOR_OBJECT_ID_PCIE_CONNECTOR)) {
283                                 uint16_t igp_offset = 0;
284                                 ATOM_INTEGRATED_SYSTEM_INFO_V2 *igp_obj;
285
286                                 index =
287                                     GetIndexIntoMasterTable(DATA,
288                                                             IntegratedSystemInfo);
289
290                                 atom_parse_data_header(ctx, index, &size, &frev,
291                                                        &crev, &igp_offset);
292
293                                 if (crev >= 2) {
294                                         igp_obj =
295                                             (ATOM_INTEGRATED_SYSTEM_INFO_V2
296                                              *) (ctx->bios + igp_offset);
297
298                                         if (igp_obj) {
299                                                 uint32_t slot_config, ct;
300
301                                                 if (con_obj_num == 1)
302                                                         slot_config =
303                                                             igp_obj->
304                                                             ulDDISlot1Config;
305                                                 else
306                                                         slot_config =
307                                                             igp_obj->
308                                                             ulDDISlot2Config;
309
310                                                 ct = (slot_config >> 16) & 0xff;
311                                                 connector_type =
312                                                     object_connector_convert
313                                                     [ct];
314                                                 igp_lane_info =
315                                                     slot_config & 0xffff;
316                                         } else
317                                                 continue;
318                                 } else
319                                         continue;
320                         } else {
321                                 igp_lane_info = 0;
322                                 connector_type =
323                                     object_connector_convert[con_obj_id];
324                         }
325
326                         if (connector_type == DRM_MODE_CONNECTOR_Unknown)
327                                 continue;
328
329                         for (j = 0; j < ((le16_to_cpu(path->usSize) - 8) / 2);
330                              j++) {
331                                 uint8_t enc_obj_id, enc_obj_num, enc_obj_type;
332
333                                 enc_obj_id =
334                                     (le16_to_cpu(path->usGraphicObjIds[j]) &
335                                      OBJECT_ID_MASK) >> OBJECT_ID_SHIFT;
336                                 enc_obj_num =
337                                     (le16_to_cpu(path->usGraphicObjIds[j]) &
338                                      ENUM_ID_MASK) >> ENUM_ID_SHIFT;
339                                 enc_obj_type =
340                                     (le16_to_cpu(path->usGraphicObjIds[j]) &
341                                      OBJECT_TYPE_MASK) >> OBJECT_TYPE_SHIFT;
342
343                                 /* FIXME: add support for router objects */
344                                 if (enc_obj_type == GRAPH_OBJECT_TYPE_ENCODER) {
345                                         if (enc_obj_num == 2)
346                                                 linkb = true;
347                                         else
348                                                 linkb = false;
349
350                                         radeon_add_atom_encoder(dev,
351                                                                 enc_obj_id,
352                                                                 le16_to_cpu
353                                                                 (path->
354                                                                  usDeviceTag));
355
356                                 }
357                         }
358
359                         /* look up gpio for ddc */
360                         if ((le16_to_cpu(path->usDeviceTag) &
361                              (ATOM_DEVICE_TV_SUPPORT | ATOM_DEVICE_CV_SUPPORT))
362                             == 0) {
363                                 for (j = 0; j < con_obj->ucNumberOfObjects; j++) {
364                                         if (le16_to_cpu(path->usConnObjectId) ==
365                                             le16_to_cpu(con_obj->asObjects[j].
366                                                         usObjectID)) {
367                                                 ATOM_COMMON_RECORD_HEADER
368                                                     *record =
369                                                     (ATOM_COMMON_RECORD_HEADER
370                                                      *)
371                                                     (ctx->bios + data_offset +
372                                                      le16_to_cpu(con_obj->
373                                                                  asObjects[j].
374                                                                  usRecordOffset));
375                                                 ATOM_I2C_RECORD *i2c_record;
376
377                                                 while (record->ucRecordType > 0
378                                                        && record->
379                                                        ucRecordType <=
380                                                        ATOM_MAX_OBJECT_RECORD_NUMBER) {
381                                                         DRM_ERROR
382                                                             ("record type %d\n",
383                                                              record->
384                                                              ucRecordType);
385                                                         switch (record->
386                                                                 ucRecordType) {
387                                                         case ATOM_I2C_RECORD_TYPE:
388                                                                 i2c_record =
389                                                                     (ATOM_I2C_RECORD
390                                                                      *) record;
391                                                                 line_mux =
392                                                                     i2c_record->
393                                                                     sucI2cId.
394                                                                     bfI2C_LineMux;
395                                                                 break;
396                                                         }
397                                                         record =
398                                                             (ATOM_COMMON_RECORD_HEADER
399                                                              *) ((char *)record
400                                                                  +
401                                                                  record->
402                                                                  ucRecordSize);
403                                                 }
404                                                 break;
405                                         }
406                                 }
407                         } else
408                                 line_mux = 0;
409
410                         if ((le16_to_cpu(path->usDeviceTag) ==
411                              ATOM_DEVICE_TV1_SUPPORT)
412                             || (le16_to_cpu(path->usDeviceTag) ==
413                                 ATOM_DEVICE_TV2_SUPPORT)
414                             || (le16_to_cpu(path->usDeviceTag) ==
415                                 ATOM_DEVICE_CV_SUPPORT))
416                                 ddc_bus.valid = false;
417                         else
418                                 ddc_bus = radeon_lookup_gpio(dev, line_mux);
419
420                         radeon_add_atom_connector(dev,
421                                                   le16_to_cpu(path->
422                                                               usConnObjectId),
423                                                   le16_to_cpu(path->
424                                                               usDeviceTag),
425                                                   connector_type, &ddc_bus,
426                                                   linkb, igp_lane_info);
427
428                 }
429         }
430
431         radeon_link_encoder_connector(dev);
432
433         return true;
434 }
435
436 struct bios_connector {
437         bool valid;
438         uint8_t line_mux;
439         uint16_t devices;
440         int connector_type;
441         struct radeon_i2c_bus_rec ddc_bus;
442 };
443
444 bool radeon_get_atom_connector_info_from_supported_devices_table(struct
445                                                                  drm_device
446                                                                  *dev)
447 {
448         struct radeon_device *rdev = dev->dev_private;
449         struct radeon_mode_info *mode_info = &rdev->mode_info;
450         struct atom_context *ctx = mode_info->atom_context;
451         int index = GetIndexIntoMasterTable(DATA, SupportedDevicesInfo);
452         uint16_t size, data_offset;
453         uint8_t frev, crev;
454         uint16_t device_support;
455         uint8_t dac;
456         union atom_supported_devices *supported_devices;
457         int i, j;
458         struct bios_connector bios_connectors[ATOM_MAX_SUPPORTED_DEVICE];
459
460         atom_parse_data_header(ctx, index, &size, &frev, &crev, &data_offset);
461
462         supported_devices =
463             (union atom_supported_devices *)(ctx->bios + data_offset);
464
465         device_support = le16_to_cpu(supported_devices->info.usDeviceSupport);
466
467         for (i = 0; i < ATOM_MAX_SUPPORTED_DEVICE; i++) {
468                 ATOM_CONNECTOR_INFO_I2C ci =
469                     supported_devices->info.asConnInfo[i];
470
471                 bios_connectors[i].valid = false;
472
473                 if (!(device_support & (1 << i))) {
474                         continue;
475                 }
476
477                 if (i == ATOM_DEVICE_CV_INDEX) {
478                         DRM_DEBUG("Skipping Component Video\n");
479                         continue;
480                 }
481
482                 if (i == ATOM_DEVICE_TV1_INDEX) {
483                         DRM_DEBUG("Skipping TV Out\n");
484                         continue;
485                 }
486
487                 bios_connectors[i].connector_type =
488                     supported_devices_connector_convert[ci.sucConnectorInfo.
489                                                         sbfAccess.
490                                                         bfConnectorType];
491
492                 if (bios_connectors[i].connector_type ==
493                     DRM_MODE_CONNECTOR_Unknown)
494                         continue;
495
496                 dac = ci.sucConnectorInfo.sbfAccess.bfAssociatedDAC;
497
498                 if ((rdev->family == CHIP_RS690) ||
499                     (rdev->family == CHIP_RS740)) {
500                         if ((i == ATOM_DEVICE_DFP2_INDEX)
501                             && (ci.sucI2cId.sbfAccess.bfI2C_LineMux == 2))
502                                 bios_connectors[i].line_mux =
503                                     ci.sucI2cId.sbfAccess.bfI2C_LineMux + 1;
504                         else if ((i == ATOM_DEVICE_DFP3_INDEX)
505                                  && (ci.sucI2cId.sbfAccess.bfI2C_LineMux == 1))
506                                 bios_connectors[i].line_mux =
507                                     ci.sucI2cId.sbfAccess.bfI2C_LineMux + 1;
508                         else
509                                 bios_connectors[i].line_mux =
510                                     ci.sucI2cId.sbfAccess.bfI2C_LineMux;
511                 } else
512                         bios_connectors[i].line_mux =
513                             ci.sucI2cId.sbfAccess.bfI2C_LineMux;
514
515                 /* give tv unique connector ids */
516                 if (i == ATOM_DEVICE_TV1_INDEX) {
517                         bios_connectors[i].ddc_bus.valid = false;
518                         bios_connectors[i].line_mux = 50;
519                 } else if (i == ATOM_DEVICE_TV2_INDEX) {
520                         bios_connectors[i].ddc_bus.valid = false;
521                         bios_connectors[i].line_mux = 51;
522                 } else if (i == ATOM_DEVICE_CV_INDEX) {
523                         bios_connectors[i].ddc_bus.valid = false;
524                         bios_connectors[i].line_mux = 52;
525                 } else
526                         bios_connectors[i].ddc_bus =
527                             radeon_lookup_gpio(dev,
528                                                bios_connectors[i].line_mux);
529
530                 /* Always set the connector type to VGA for CRT1/CRT2. if they are
531                  * shared with a DVI port, we'll pick up the DVI connector when we
532                  * merge the outputs.  Some bioses incorrectly list VGA ports as DVI.
533                  */
534                 if (i == ATOM_DEVICE_CRT1_INDEX || i == ATOM_DEVICE_CRT2_INDEX)
535                         bios_connectors[i].connector_type =
536                             DRM_MODE_CONNECTOR_VGA;
537
538                 if (!radeon_atom_apply_quirks
539                     (dev, (1 << i), &bios_connectors[i].connector_type,
540                      &bios_connectors[i].ddc_bus, &bios_connectors[i].line_mux))
541                         continue;
542
543                 bios_connectors[i].valid = true;
544                 bios_connectors[i].devices = (1 << i);
545
546                 if (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom)
547                         radeon_add_atom_encoder(dev,
548                                                 radeon_get_encoder_id(dev,
549                                                                       (1 << i),
550                                                                       dac),
551                                                 (1 << i));
552                 else
553                         radeon_add_legacy_encoder(dev,
554                                                   radeon_get_encoder_id(dev,
555                                                                         (1 <<
556                                                                          i),
557                                                                         dac),
558                                                   (1 << i));
559         }
560
561         /* combine shared connectors */
562         for (i = 0; i < ATOM_MAX_SUPPORTED_DEVICE; i++) {
563                 if (bios_connectors[i].valid) {
564                         for (j = 0; j < ATOM_MAX_SUPPORTED_DEVICE; j++) {
565                                 if (bios_connectors[j].valid && (i != j)) {
566                                         if (bios_connectors[i].line_mux ==
567                                             bios_connectors[j].line_mux) {
568                                                 if (((bios_connectors[i].
569                                                       devices &
570                                                       (ATOM_DEVICE_DFP_SUPPORT))
571                                                      && (bios_connectors[j].
572                                                          devices &
573                                                          (ATOM_DEVICE_CRT_SUPPORT)))
574                                                     ||
575                                                     ((bios_connectors[j].
576                                                       devices &
577                                                       (ATOM_DEVICE_DFP_SUPPORT))
578                                                      && (bios_connectors[i].
579                                                          devices &
580                                                          (ATOM_DEVICE_CRT_SUPPORT)))) {
581                                                         bios_connectors[i].
582                                                             devices |=
583                                                             bios_connectors[j].
584                                                             devices;
585                                                         bios_connectors[i].
586                                                             connector_type =
587                                                             DRM_MODE_CONNECTOR_DVII;
588                                                         bios_connectors[j].
589                                                             valid = false;
590                                                 }
591                                         }
592                                 }
593                         }
594                 }
595         }
596
597         /* add the connectors */
598         for (i = 0; i < ATOM_MAX_SUPPORTED_DEVICE; i++) {
599                 if (bios_connectors[i].valid)
600                         radeon_add_atom_connector(dev,
601                                                   bios_connectors[i].line_mux,
602                                                   bios_connectors[i].devices,
603                                                   bios_connectors[i].
604                                                   connector_type,
605                                                   &bios_connectors[i].ddc_bus,
606                                                   false, 0);
607         }
608
609         radeon_link_encoder_connector(dev);
610
611         return true;
612 }
613
614 union firmware_info {
615         ATOM_FIRMWARE_INFO info;
616         ATOM_FIRMWARE_INFO_V1_2 info_12;
617         ATOM_FIRMWARE_INFO_V1_3 info_13;
618         ATOM_FIRMWARE_INFO_V1_4 info_14;
619 };
620
621 bool radeon_atom_get_clock_info(struct drm_device *dev)
622 {
623         struct radeon_device *rdev = dev->dev_private;
624         struct radeon_mode_info *mode_info = &rdev->mode_info;
625         int index = GetIndexIntoMasterTable(DATA, FirmwareInfo);
626         union firmware_info *firmware_info;
627         uint8_t frev, crev;
628         struct radeon_pll *p1pll = &rdev->clock.p1pll;
629         struct radeon_pll *p2pll = &rdev->clock.p2pll;
630         struct radeon_pll *spll = &rdev->clock.spll;
631         struct radeon_pll *mpll = &rdev->clock.mpll;
632         uint16_t data_offset;
633
634         atom_parse_data_header(mode_info->atom_context, index, NULL, &frev,
635                                &crev, &data_offset);
636
637         firmware_info =
638             (union firmware_info *)(mode_info->atom_context->bios +
639                                     data_offset);
640
641         if (firmware_info) {
642                 /* pixel clocks */
643                 p1pll->reference_freq =
644                     le16_to_cpu(firmware_info->info.usReferenceClock);
645                 p1pll->reference_div = 0;
646
647                 p1pll->pll_out_min =
648                     le16_to_cpu(firmware_info->info.usMinPixelClockPLL_Output);
649                 p1pll->pll_out_max =
650                     le32_to_cpu(firmware_info->info.ulMaxPixelClockPLL_Output);
651
652                 if (p1pll->pll_out_min == 0) {
653                         if (ASIC_IS_AVIVO(rdev))
654                                 p1pll->pll_out_min = 64800;
655                         else
656                                 p1pll->pll_out_min = 20000;
657                 }
658
659                 p1pll->pll_in_min =
660                     le16_to_cpu(firmware_info->info.usMinPixelClockPLL_Input);
661                 p1pll->pll_in_max =
662                     le16_to_cpu(firmware_info->info.usMaxPixelClockPLL_Input);
663
664                 *p2pll = *p1pll;
665
666                 /* system clock */
667                 spll->reference_freq =
668                     le16_to_cpu(firmware_info->info.usReferenceClock);
669                 spll->reference_div = 0;
670
671                 spll->pll_out_min =
672                     le16_to_cpu(firmware_info->info.usMinEngineClockPLL_Output);
673                 spll->pll_out_max =
674                     le32_to_cpu(firmware_info->info.ulMaxEngineClockPLL_Output);
675
676                 /* ??? */
677                 if (spll->pll_out_min == 0) {
678                         if (ASIC_IS_AVIVO(rdev))
679                                 spll->pll_out_min = 64800;
680                         else
681                                 spll->pll_out_min = 20000;
682                 }
683
684                 spll->pll_in_min =
685                     le16_to_cpu(firmware_info->info.usMinEngineClockPLL_Input);
686                 spll->pll_in_max =
687                     le16_to_cpu(firmware_info->info.usMaxEngineClockPLL_Input);
688
689                 /* memory clock */
690                 mpll->reference_freq =
691                     le16_to_cpu(firmware_info->info.usReferenceClock);
692                 mpll->reference_div = 0;
693
694                 mpll->pll_out_min =
695                     le16_to_cpu(firmware_info->info.usMinMemoryClockPLL_Output);
696                 mpll->pll_out_max =
697                     le32_to_cpu(firmware_info->info.ulMaxMemoryClockPLL_Output);
698
699                 /* ??? */
700                 if (mpll->pll_out_min == 0) {
701                         if (ASIC_IS_AVIVO(rdev))
702                                 mpll->pll_out_min = 64800;
703                         else
704                                 mpll->pll_out_min = 20000;
705                 }
706
707                 mpll->pll_in_min =
708                     le16_to_cpu(firmware_info->info.usMinMemoryClockPLL_Input);
709                 mpll->pll_in_max =
710                     le16_to_cpu(firmware_info->info.usMaxMemoryClockPLL_Input);
711
712                 rdev->clock.default_sclk =
713                     le32_to_cpu(firmware_info->info.ulDefaultEngineClock);
714                 rdev->clock.default_mclk =
715                     le32_to_cpu(firmware_info->info.ulDefaultMemoryClock);
716
717                 return true;
718         }
719         return false;
720 }
721
722 struct radeon_encoder_int_tmds *radeon_atombios_get_tmds_info(struct
723                                                               radeon_encoder
724                                                               *encoder)
725 {
726         struct drm_device *dev = encoder->base.dev;
727         struct radeon_device *rdev = dev->dev_private;
728         struct radeon_mode_info *mode_info = &rdev->mode_info;
729         int index = GetIndexIntoMasterTable(DATA, TMDS_Info);
730         uint16_t data_offset;
731         struct _ATOM_TMDS_INFO *tmds_info;
732         uint8_t frev, crev;
733         uint16_t maxfreq;
734         int i;
735         struct radeon_encoder_int_tmds *tmds = NULL;
736
737         atom_parse_data_header(mode_info->atom_context, index, NULL, &frev,
738                                &crev, &data_offset);
739
740         tmds_info =
741             (struct _ATOM_TMDS_INFO *)(mode_info->atom_context->bios +
742                                        data_offset);
743
744         if (tmds_info) {
745                 tmds =
746                     kzalloc(sizeof(struct radeon_encoder_int_tmds), GFP_KERNEL);
747
748                 if (!tmds)
749                         return NULL;
750
751                 maxfreq = le16_to_cpu(tmds_info->usMaxFrequency);
752                 for (i = 0; i < 4; i++) {
753                         tmds->tmds_pll[i].freq =
754                             le16_to_cpu(tmds_info->asMiscInfo[i].usFrequency);
755                         tmds->tmds_pll[i].value =
756                             tmds_info->asMiscInfo[i].ucPLL_ChargePump & 0x3f;
757                         tmds->tmds_pll[i].value |=
758                             (tmds_info->asMiscInfo[i].
759                              ucPLL_VCO_Gain & 0x3f) << 6;
760                         tmds->tmds_pll[i].value |=
761                             (tmds_info->asMiscInfo[i].
762                              ucPLL_DutyCycle & 0xf) << 12;
763                         tmds->tmds_pll[i].value |=
764                             (tmds_info->asMiscInfo[i].
765                              ucPLL_VoltageSwing & 0xf) << 16;
766
767                         DRM_DEBUG("TMDS PLL From ATOMBIOS %u %x\n",
768                                   tmds->tmds_pll[i].freq,
769                                   tmds->tmds_pll[i].value);
770
771                         if (maxfreq == tmds->tmds_pll[i].freq) {
772                                 tmds->tmds_pll[i].freq = 0xffffffff;
773                                 break;
774                         }
775                 }
776         }
777         return tmds;
778 }
779
780 union lvds_info {
781         struct _ATOM_LVDS_INFO info;
782         struct _ATOM_LVDS_INFO_V12 info_12;
783 };
784
785 struct radeon_encoder_atom_dig *radeon_atombios_get_lvds_info(struct
786                                                               radeon_encoder
787                                                               *encoder)
788 {
789         struct drm_device *dev = encoder->base.dev;
790         struct radeon_device *rdev = dev->dev_private;
791         struct radeon_mode_info *mode_info = &rdev->mode_info;
792         int index = GetIndexIntoMasterTable(DATA, LVDS_Info);
793         uint16_t data_offset;
794         union lvds_info *lvds_info;
795         uint8_t frev, crev;
796         struct radeon_encoder_atom_dig *lvds = NULL;
797
798         atom_parse_data_header(mode_info->atom_context, index, NULL, &frev,
799                                &crev, &data_offset);
800
801         lvds_info =
802             (union lvds_info *)(mode_info->atom_context->bios + data_offset);
803
804         if (lvds_info) {
805                 lvds =
806                     kzalloc(sizeof(struct radeon_encoder_atom_dig), GFP_KERNEL);
807
808                 if (!lvds)
809                         return NULL;
810
811                 lvds->native_mode.dotclock =
812                     le16_to_cpu(lvds_info->info.sLCDTiming.usPixClk) * 10;
813                 lvds->native_mode.panel_xres =
814                     le16_to_cpu(lvds_info->info.sLCDTiming.usHActive);
815                 lvds->native_mode.panel_yres =
816                     le16_to_cpu(lvds_info->info.sLCDTiming.usVActive);
817                 lvds->native_mode.hblank =
818                     le16_to_cpu(lvds_info->info.sLCDTiming.usHBlanking_Time);
819                 lvds->native_mode.hoverplus =
820                     le16_to_cpu(lvds_info->info.sLCDTiming.usHSyncOffset);
821                 lvds->native_mode.hsync_width =
822                     le16_to_cpu(lvds_info->info.sLCDTiming.usHSyncWidth);
823                 lvds->native_mode.vblank =
824                     le16_to_cpu(lvds_info->info.sLCDTiming.usVBlanking_Time);
825                 lvds->native_mode.voverplus =
826                     le16_to_cpu(lvds_info->info.sLCDTiming.usVSyncOffset);
827                 lvds->native_mode.vsync_width =
828                     le16_to_cpu(lvds_info->info.sLCDTiming.usVSyncWidth);
829                 lvds->panel_pwr_delay =
830                     le16_to_cpu(lvds_info->info.usOffDelayInMs);
831                 lvds->lvds_misc = lvds_info->info.ucLVDS_Misc;
832
833                 encoder->native_mode = lvds->native_mode;
834         }
835         return lvds;
836 }
837
838 struct radeon_encoder_primary_dac *
839 radeon_atombios_get_primary_dac_info(struct radeon_encoder *encoder)
840 {
841         struct drm_device *dev = encoder->base.dev;
842         struct radeon_device *rdev = dev->dev_private;
843         struct radeon_mode_info *mode_info = &rdev->mode_info;
844         int index = GetIndexIntoMasterTable(DATA, CompassionateData);
845         uint16_t data_offset;
846         struct _COMPASSIONATE_DATA *dac_info;
847         uint8_t frev, crev;
848         uint8_t bg, dac;
849         struct radeon_encoder_primary_dac *p_dac = NULL;
850
851         atom_parse_data_header(mode_info->atom_context, index, NULL, &frev, &crev, &data_offset);
852
853         dac_info = (struct _COMPASSIONATE_DATA *)(mode_info->atom_context->bios + data_offset);
854
855         if (dac_info) {
856                 p_dac = kzalloc(sizeof(struct radeon_encoder_primary_dac), GFP_KERNEL);
857
858                 if (!p_dac)
859                         return NULL;
860
861                 bg = dac_info->ucDAC1_BG_Adjustment;
862                 dac = dac_info->ucDAC1_DAC_Adjustment;
863                 p_dac->ps2_pdac_adj = (bg << 8) | (dac);
864
865         }
866         return p_dac;
867 }
868
869 struct radeon_encoder_tv_dac *
870 radeon_atombios_get_tv_dac_info(struct radeon_encoder *encoder)
871 {
872         struct drm_device *dev = encoder->base.dev;
873         struct radeon_device *rdev = dev->dev_private;
874         struct radeon_mode_info *mode_info = &rdev->mode_info;
875         int index = GetIndexIntoMasterTable(DATA, CompassionateData);
876         uint16_t data_offset;
877         struct _COMPASSIONATE_DATA *dac_info;
878         uint8_t frev, crev;
879         uint8_t bg, dac;
880         struct radeon_encoder_tv_dac *tv_dac = NULL;
881
882         atom_parse_data_header(mode_info->atom_context, index, NULL, &frev, &crev, &data_offset);
883
884         dac_info = (struct _COMPASSIONATE_DATA *)(mode_info->atom_context->bios + data_offset);
885
886         if (dac_info) {
887                 tv_dac = kzalloc(sizeof(struct radeon_encoder_tv_dac), GFP_KERNEL);
888
889                 if (!tv_dac)
890                         return NULL;
891
892                 bg = dac_info->ucDAC2_CRT2_BG_Adjustment;
893                 dac = dac_info->ucDAC2_CRT2_DAC_Adjustment;
894                 tv_dac->ps2_tvdac_adj = (bg << 16) | (dac << 20);
895
896                 bg = dac_info->ucDAC2_PAL_BG_Adjustment;
897                 dac = dac_info->ucDAC2_PAL_DAC_Adjustment;
898                 tv_dac->pal_tvdac_adj = (bg << 16) | (dac << 20);
899
900                 bg = dac_info->ucDAC2_NTSC_BG_Adjustment;
901                 dac = dac_info->ucDAC2_NTSC_DAC_Adjustment;
902                 tv_dac->ntsc_tvdac_adj = (bg << 16) | (dac << 20);
903
904         }
905         return tv_dac;
906 }
907
908 void radeon_atom_set_clock_gating(struct radeon_device *rdev, int enable)
909 {
910         DYNAMIC_CLOCK_GATING_PS_ALLOCATION args;
911         int index = GetIndexIntoMasterTable(COMMAND, DynamicClockGating);
912
913         args.ucEnable = enable;
914
915         atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
916 }
917
918 void radeon_atom_static_pwrmgt_setup(struct radeon_device *rdev, int enable)
919 {
920         ENABLE_ASIC_STATIC_PWR_MGT_PS_ALLOCATION args;
921         int index = GetIndexIntoMasterTable(COMMAND, EnableASIC_StaticPwrMgt);
922
923         args.ucEnable = enable;
924
925         atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
926 }
927
928 void radeon_atom_set_engine_clock(struct radeon_device *rdev,
929                                   uint32_t eng_clock)
930 {
931         SET_ENGINE_CLOCK_PS_ALLOCATION args;
932         int index = GetIndexIntoMasterTable(COMMAND, SetEngineClock);
933
934         args.ulTargetEngineClock = eng_clock;   /* 10 khz */
935
936         atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
937 }
938
939 void radeon_atom_set_memory_clock(struct radeon_device *rdev,
940                                   uint32_t mem_clock)
941 {
942         SET_MEMORY_CLOCK_PS_ALLOCATION args;
943         int index = GetIndexIntoMasterTable(COMMAND, SetMemoryClock);
944
945         if (rdev->flags & RADEON_IS_IGP)
946                 return;
947
948         args.ulTargetMemoryClock = mem_clock;   /* 10 khz */
949
950         atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
951 }
952
953 void radeon_atom_initialize_bios_scratch_regs(struct drm_device *dev)
954 {
955         struct radeon_device *rdev = dev->dev_private;
956         uint32_t bios_2_scratch, bios_6_scratch;
957
958         if (rdev->family >= CHIP_R600) {
959                 bios_2_scratch = RREG32(R600_BIOS_0_SCRATCH);
960                 bios_6_scratch = RREG32(R600_BIOS_6_SCRATCH);
961         } else {
962                 bios_2_scratch = RREG32(RADEON_BIOS_0_SCRATCH);
963                 bios_6_scratch = RREG32(RADEON_BIOS_6_SCRATCH);
964         }
965
966         /* let the bios control the backlight */
967         bios_2_scratch &= ~ATOM_S2_VRI_BRIGHT_ENABLE;
968
969         /* tell the bios not to handle mode switching */
970         bios_6_scratch |= (ATOM_S6_ACC_BLOCK_DISPLAY_SWITCH | ATOM_S6_ACC_MODE);
971
972         if (rdev->family >= CHIP_R600) {
973                 WREG32(R600_BIOS_2_SCRATCH, bios_2_scratch);
974                 WREG32(R600_BIOS_6_SCRATCH, bios_6_scratch);
975         } else {
976                 WREG32(RADEON_BIOS_2_SCRATCH, bios_2_scratch);
977                 WREG32(RADEON_BIOS_6_SCRATCH, bios_6_scratch);
978         }
979
980 }
981
982 void radeon_atom_output_lock(struct drm_encoder *encoder, bool lock)
983 {
984         struct drm_device *dev = encoder->dev;
985         struct radeon_device *rdev = dev->dev_private;
986         uint32_t bios_6_scratch;
987
988         if (rdev->family >= CHIP_R600)
989                 bios_6_scratch = RREG32(R600_BIOS_6_SCRATCH);
990         else
991                 bios_6_scratch = RREG32(RADEON_BIOS_6_SCRATCH);
992
993         if (lock)
994                 bios_6_scratch |= ATOM_S6_CRITICAL_STATE;
995         else
996                 bios_6_scratch &= ~ATOM_S6_CRITICAL_STATE;
997
998         if (rdev->family >= CHIP_R600)
999                 WREG32(R600_BIOS_6_SCRATCH, bios_6_scratch);
1000         else
1001                 WREG32(RADEON_BIOS_6_SCRATCH, bios_6_scratch);
1002 }
1003
1004 /* at some point we may want to break this out into individual functions */
1005 void
1006 radeon_atombios_connected_scratch_regs(struct drm_connector *connector,
1007                                        struct drm_encoder *encoder,
1008                                        bool connected)
1009 {
1010         struct drm_device *dev = connector->dev;
1011         struct radeon_device *rdev = dev->dev_private;
1012         struct radeon_connector *radeon_connector =
1013             to_radeon_connector(connector);
1014         struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
1015         uint32_t bios_0_scratch, bios_3_scratch, bios_6_scratch;
1016
1017         if (rdev->family >= CHIP_R600) {
1018                 bios_0_scratch = RREG32(R600_BIOS_0_SCRATCH);
1019                 bios_3_scratch = RREG32(R600_BIOS_3_SCRATCH);
1020                 bios_6_scratch = RREG32(R600_BIOS_6_SCRATCH);
1021         } else {
1022                 bios_0_scratch = RREG32(RADEON_BIOS_0_SCRATCH);
1023                 bios_3_scratch = RREG32(RADEON_BIOS_3_SCRATCH);
1024                 bios_6_scratch = RREG32(RADEON_BIOS_6_SCRATCH);
1025         }
1026
1027         if ((radeon_encoder->devices & ATOM_DEVICE_TV1_SUPPORT) &&
1028             (radeon_connector->devices & ATOM_DEVICE_TV1_SUPPORT)) {
1029                 if (connected) {
1030                         DRM_DEBUG("TV1 connected\n");
1031                         bios_3_scratch |= ATOM_S3_TV1_ACTIVE;
1032                         bios_6_scratch |= ATOM_S6_ACC_REQ_TV1;
1033                 } else {
1034                         DRM_DEBUG("TV1 disconnected\n");
1035                         bios_0_scratch &= ~ATOM_S0_TV1_MASK;
1036                         bios_3_scratch &= ~ATOM_S3_TV1_ACTIVE;
1037                         bios_6_scratch &= ~ATOM_S6_ACC_REQ_TV1;
1038                 }
1039         }
1040         if ((radeon_encoder->devices & ATOM_DEVICE_CV_SUPPORT) &&
1041             (radeon_connector->devices & ATOM_DEVICE_CV_SUPPORT)) {
1042                 if (connected) {
1043                         DRM_DEBUG("CV connected\n");
1044                         bios_3_scratch |= ATOM_S3_CV_ACTIVE;
1045                         bios_6_scratch |= ATOM_S6_ACC_REQ_CV;
1046                 } else {
1047                         DRM_DEBUG("CV disconnected\n");
1048                         bios_0_scratch &= ~ATOM_S0_CV_MASK;
1049                         bios_3_scratch &= ~ATOM_S3_CV_ACTIVE;
1050                         bios_6_scratch &= ~ATOM_S6_ACC_REQ_CV;
1051                 }
1052         }
1053         if ((radeon_encoder->devices & ATOM_DEVICE_LCD1_SUPPORT) &&
1054             (radeon_connector->devices & ATOM_DEVICE_LCD1_SUPPORT)) {
1055                 if (connected) {
1056                         DRM_DEBUG("LCD1 connected\n");
1057                         bios_0_scratch |= ATOM_S0_LCD1;
1058                         bios_3_scratch |= ATOM_S3_LCD1_ACTIVE;
1059                         bios_6_scratch |= ATOM_S6_ACC_REQ_LCD1;
1060                 } else {
1061                         DRM_DEBUG("LCD1 disconnected\n");
1062                         bios_0_scratch &= ~ATOM_S0_LCD1;
1063                         bios_3_scratch &= ~ATOM_S3_LCD1_ACTIVE;
1064                         bios_6_scratch &= ~ATOM_S6_ACC_REQ_LCD1;
1065                 }
1066         }
1067         if ((radeon_encoder->devices & ATOM_DEVICE_CRT1_SUPPORT) &&
1068             (radeon_connector->devices & ATOM_DEVICE_CRT1_SUPPORT)) {
1069                 if (connected) {
1070                         DRM_DEBUG("CRT1 connected\n");
1071                         bios_0_scratch |= ATOM_S0_CRT1_COLOR;
1072                         bios_3_scratch |= ATOM_S3_CRT1_ACTIVE;
1073                         bios_6_scratch |= ATOM_S6_ACC_REQ_CRT1;
1074                 } else {
1075                         DRM_DEBUG("CRT1 disconnected\n");
1076                         bios_0_scratch &= ~ATOM_S0_CRT1_MASK;
1077                         bios_3_scratch &= ~ATOM_S3_CRT1_ACTIVE;
1078                         bios_6_scratch &= ~ATOM_S6_ACC_REQ_CRT1;
1079                 }
1080         }
1081         if ((radeon_encoder->devices & ATOM_DEVICE_CRT2_SUPPORT) &&
1082             (radeon_connector->devices & ATOM_DEVICE_CRT2_SUPPORT)) {
1083                 if (connected) {
1084                         DRM_DEBUG("CRT2 connected\n");
1085                         bios_0_scratch |= ATOM_S0_CRT2_COLOR;
1086                         bios_3_scratch |= ATOM_S3_CRT2_ACTIVE;
1087                         bios_6_scratch |= ATOM_S6_ACC_REQ_CRT2;
1088                 } else {
1089                         DRM_DEBUG("CRT2 disconnected\n");
1090                         bios_0_scratch &= ~ATOM_S0_CRT2_MASK;
1091                         bios_3_scratch &= ~ATOM_S3_CRT2_ACTIVE;
1092                         bios_6_scratch &= ~ATOM_S6_ACC_REQ_CRT2;
1093                 }
1094         }
1095         if ((radeon_encoder->devices & ATOM_DEVICE_DFP1_SUPPORT) &&
1096             (radeon_connector->devices & ATOM_DEVICE_DFP1_SUPPORT)) {
1097                 if (connected) {
1098                         DRM_DEBUG("DFP1 connected\n");
1099                         bios_0_scratch |= ATOM_S0_DFP1;
1100                         bios_3_scratch |= ATOM_S3_DFP1_ACTIVE;
1101                         bios_6_scratch |= ATOM_S6_ACC_REQ_DFP1;
1102                 } else {
1103                         DRM_DEBUG("DFP1 disconnected\n");
1104                         bios_0_scratch &= ~ATOM_S0_DFP1;
1105                         bios_3_scratch &= ~ATOM_S3_DFP1_ACTIVE;
1106                         bios_6_scratch &= ~ATOM_S6_ACC_REQ_DFP1;
1107                 }
1108         }
1109         if ((radeon_encoder->devices & ATOM_DEVICE_DFP2_SUPPORT) &&
1110             (radeon_connector->devices & ATOM_DEVICE_DFP2_SUPPORT)) {
1111                 if (connected) {
1112                         DRM_DEBUG("DFP2 connected\n");
1113                         bios_0_scratch |= ATOM_S0_DFP2;
1114                         bios_3_scratch |= ATOM_S3_DFP2_ACTIVE;
1115                         bios_6_scratch |= ATOM_S6_ACC_REQ_DFP2;
1116                 } else {
1117                         DRM_DEBUG("DFP2 disconnected\n");
1118                         bios_0_scratch &= ~ATOM_S0_DFP2;
1119                         bios_3_scratch &= ~ATOM_S3_DFP2_ACTIVE;
1120                         bios_6_scratch &= ~ATOM_S6_ACC_REQ_DFP2;
1121                 }
1122         }
1123         if ((radeon_encoder->devices & ATOM_DEVICE_DFP3_SUPPORT) &&
1124             (radeon_connector->devices & ATOM_DEVICE_DFP3_SUPPORT)) {
1125                 if (connected) {
1126                         DRM_DEBUG("DFP3 connected\n");
1127                         bios_0_scratch |= ATOM_S0_DFP3;
1128                         bios_3_scratch |= ATOM_S3_DFP3_ACTIVE;
1129                         bios_6_scratch |= ATOM_S6_ACC_REQ_DFP3;
1130                 } else {
1131                         DRM_DEBUG("DFP3 disconnected\n");
1132                         bios_0_scratch &= ~ATOM_S0_DFP3;
1133                         bios_3_scratch &= ~ATOM_S3_DFP3_ACTIVE;
1134                         bios_6_scratch &= ~ATOM_S6_ACC_REQ_DFP3;
1135                 }
1136         }
1137         if ((radeon_encoder->devices & ATOM_DEVICE_DFP4_SUPPORT) &&
1138             (radeon_connector->devices & ATOM_DEVICE_DFP4_SUPPORT)) {
1139                 if (connected) {
1140                         DRM_DEBUG("DFP4 connected\n");
1141                         bios_0_scratch |= ATOM_S0_DFP4;
1142                         bios_3_scratch |= ATOM_S3_DFP4_ACTIVE;
1143                         bios_6_scratch |= ATOM_S6_ACC_REQ_DFP4;
1144                 } else {
1145                         DRM_DEBUG("DFP4 disconnected\n");
1146                         bios_0_scratch &= ~ATOM_S0_DFP4;
1147                         bios_3_scratch &= ~ATOM_S3_DFP4_ACTIVE;
1148                         bios_6_scratch &= ~ATOM_S6_ACC_REQ_DFP4;
1149                 }
1150         }
1151         if ((radeon_encoder->devices & ATOM_DEVICE_DFP5_SUPPORT) &&
1152             (radeon_connector->devices & ATOM_DEVICE_DFP5_SUPPORT)) {
1153                 if (connected) {
1154                         DRM_DEBUG("DFP5 connected\n");
1155                         bios_0_scratch |= ATOM_S0_DFP5;
1156                         bios_3_scratch |= ATOM_S3_DFP5_ACTIVE;
1157                         bios_6_scratch |= ATOM_S6_ACC_REQ_DFP5;
1158                 } else {
1159                         DRM_DEBUG("DFP5 disconnected\n");
1160                         bios_0_scratch &= ~ATOM_S0_DFP5;
1161                         bios_3_scratch &= ~ATOM_S3_DFP5_ACTIVE;
1162                         bios_6_scratch &= ~ATOM_S6_ACC_REQ_DFP5;
1163                 }
1164         }
1165
1166         if (rdev->family >= CHIP_R600) {
1167                 WREG32(R600_BIOS_0_SCRATCH, bios_0_scratch);
1168                 WREG32(R600_BIOS_3_SCRATCH, bios_3_scratch);
1169                 WREG32(R600_BIOS_6_SCRATCH, bios_6_scratch);
1170         } else {
1171                 WREG32(RADEON_BIOS_0_SCRATCH, bios_0_scratch);
1172                 WREG32(RADEON_BIOS_3_SCRATCH, bios_3_scratch);
1173                 WREG32(RADEON_BIOS_6_SCRATCH, bios_6_scratch);
1174         }
1175 }
1176
1177 void
1178 radeon_atombios_encoder_crtc_scratch_regs(struct drm_encoder *encoder, int crtc)
1179 {
1180         struct drm_device *dev = encoder->dev;
1181         struct radeon_device *rdev = dev->dev_private;
1182         struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
1183         uint32_t bios_3_scratch;
1184
1185         if (rdev->family >= CHIP_R600)
1186                 bios_3_scratch = RREG32(R600_BIOS_3_SCRATCH);
1187         else
1188                 bios_3_scratch = RREG32(RADEON_BIOS_3_SCRATCH);
1189
1190         if (radeon_encoder->devices & ATOM_DEVICE_TV1_SUPPORT) {
1191                 bios_3_scratch &= ~ATOM_S3_TV1_CRTC_ACTIVE;
1192                 bios_3_scratch |= (crtc << 18);
1193         }
1194         if (radeon_encoder->devices & ATOM_DEVICE_CV_SUPPORT) {
1195                 bios_3_scratch &= ~ATOM_S3_CV_CRTC_ACTIVE;
1196                 bios_3_scratch |= (crtc << 24);
1197         }
1198         if (radeon_encoder->devices & ATOM_DEVICE_CRT1_SUPPORT) {
1199                 bios_3_scratch &= ~ATOM_S3_CRT1_CRTC_ACTIVE;
1200                 bios_3_scratch |= (crtc << 16);
1201         }
1202         if (radeon_encoder->devices & ATOM_DEVICE_CRT2_SUPPORT) {
1203                 bios_3_scratch &= ~ATOM_S3_CRT2_CRTC_ACTIVE;
1204                 bios_3_scratch |= (crtc << 20);
1205         }
1206         if (radeon_encoder->devices & ATOM_DEVICE_LCD1_SUPPORT) {
1207                 bios_3_scratch &= ~ATOM_S3_LCD1_CRTC_ACTIVE;
1208                 bios_3_scratch |= (crtc << 17);
1209         }
1210         if (radeon_encoder->devices & ATOM_DEVICE_DFP1_SUPPORT) {
1211                 bios_3_scratch &= ~ATOM_S3_DFP1_CRTC_ACTIVE;
1212                 bios_3_scratch |= (crtc << 19);
1213         }
1214         if (radeon_encoder->devices & ATOM_DEVICE_DFP2_SUPPORT) {
1215                 bios_3_scratch &= ~ATOM_S3_DFP2_CRTC_ACTIVE;
1216                 bios_3_scratch |= (crtc << 23);
1217         }
1218         if (radeon_encoder->devices & ATOM_DEVICE_DFP3_SUPPORT) {
1219                 bios_3_scratch &= ~ATOM_S3_DFP3_CRTC_ACTIVE;
1220                 bios_3_scratch |= (crtc << 25);
1221         }
1222
1223         if (rdev->family >= CHIP_R600)
1224                 WREG32(R600_BIOS_3_SCRATCH, bios_3_scratch);
1225         else
1226                 WREG32(RADEON_BIOS_3_SCRATCH, bios_3_scratch);
1227 }
1228
1229 void
1230 radeon_atombios_encoder_dpms_scratch_regs(struct drm_encoder *encoder, bool on)
1231 {
1232         struct drm_device *dev = encoder->dev;
1233         struct radeon_device *rdev = dev->dev_private;
1234         struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
1235         uint32_t bios_2_scratch;
1236
1237         if (rdev->family >= CHIP_R600)
1238                 bios_2_scratch = RREG32(R600_BIOS_2_SCRATCH);
1239         else
1240                 bios_2_scratch = RREG32(RADEON_BIOS_2_SCRATCH);
1241
1242         if (radeon_encoder->devices & ATOM_DEVICE_TV1_SUPPORT) {
1243                 if (on)
1244                         bios_2_scratch &= ~ATOM_S2_TV1_DPMS_STATE;
1245                 else
1246                         bios_2_scratch |= ATOM_S2_TV1_DPMS_STATE;
1247         }
1248         if (radeon_encoder->devices & ATOM_DEVICE_CV_SUPPORT) {
1249                 if (on)
1250                         bios_2_scratch &= ~ATOM_S2_CV_DPMS_STATE;
1251                 else
1252                         bios_2_scratch |= ATOM_S2_CV_DPMS_STATE;
1253         }
1254         if (radeon_encoder->devices & ATOM_DEVICE_CRT1_SUPPORT) {
1255                 if (on)
1256                         bios_2_scratch &= ~ATOM_S2_CRT1_DPMS_STATE;
1257                 else
1258                         bios_2_scratch |= ATOM_S2_CRT1_DPMS_STATE;
1259         }
1260         if (radeon_encoder->devices & ATOM_DEVICE_CRT2_SUPPORT) {
1261                 if (on)
1262                         bios_2_scratch &= ~ATOM_S2_CRT2_DPMS_STATE;
1263                 else
1264                         bios_2_scratch |= ATOM_S2_CRT2_DPMS_STATE;
1265         }
1266         if (radeon_encoder->devices & ATOM_DEVICE_LCD1_SUPPORT) {
1267                 if (on)
1268                         bios_2_scratch &= ~ATOM_S2_LCD1_DPMS_STATE;
1269                 else
1270                         bios_2_scratch |= ATOM_S2_LCD1_DPMS_STATE;
1271         }
1272         if (radeon_encoder->devices & ATOM_DEVICE_DFP1_SUPPORT) {
1273                 if (on)
1274                         bios_2_scratch &= ~ATOM_S2_DFP1_DPMS_STATE;
1275                 else
1276                         bios_2_scratch |= ATOM_S2_DFP1_DPMS_STATE;
1277         }
1278         if (radeon_encoder->devices & ATOM_DEVICE_DFP2_SUPPORT) {
1279                 if (on)
1280                         bios_2_scratch &= ~ATOM_S2_DFP2_DPMS_STATE;
1281                 else
1282                         bios_2_scratch |= ATOM_S2_DFP2_DPMS_STATE;
1283         }
1284         if (radeon_encoder->devices & ATOM_DEVICE_DFP3_SUPPORT) {
1285                 if (on)
1286                         bios_2_scratch &= ~ATOM_S2_DFP3_DPMS_STATE;
1287                 else
1288                         bios_2_scratch |= ATOM_S2_DFP3_DPMS_STATE;
1289         }
1290         if (radeon_encoder->devices & ATOM_DEVICE_DFP4_SUPPORT) {
1291                 if (on)
1292                         bios_2_scratch &= ~ATOM_S2_DFP4_DPMS_STATE;
1293                 else
1294                         bios_2_scratch |= ATOM_S2_DFP4_DPMS_STATE;
1295         }
1296         if (radeon_encoder->devices & ATOM_DEVICE_DFP5_SUPPORT) {
1297                 if (on)
1298                         bios_2_scratch &= ~ATOM_S2_DFP5_DPMS_STATE;
1299                 else
1300                         bios_2_scratch |= ATOM_S2_DFP5_DPMS_STATE;
1301         }
1302
1303         if (rdev->family >= CHIP_R600)
1304                 WREG32(R600_BIOS_2_SCRATCH, bios_2_scratch);
1305         else
1306                 WREG32(RADEON_BIOS_2_SCRATCH, bios_2_scratch);
1307 }