vga.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright 2012 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. */
  24. #include <subdev/vga.h>
  25. u8
  26. nvkm_rdport(struct nvkm_device *device, int head, u16 port)
  27. {
  28. if (device->card_type >= NV_50)
  29. return nvkm_rd08(device, 0x601000 + port);
  30. if (port == 0x03c0 || port == 0x03c1 || /* AR */
  31. port == 0x03c2 || port == 0x03da || /* INP0 */
  32. port == 0x03d4 || port == 0x03d5) /* CR */
  33. return nvkm_rd08(device, 0x601000 + (head * 0x2000) + port);
  34. if (port == 0x03c2 || port == 0x03cc || /* MISC */
  35. port == 0x03c4 || port == 0x03c5 || /* SR */
  36. port == 0x03ce || port == 0x03cf) { /* GR */
  37. if (device->card_type < NV_40)
  38. head = 0; /* CR44 selects head */
  39. return nvkm_rd08(device, 0x0c0000 + (head * 0x2000) + port);
  40. }
  41. return 0x00;
  42. }
  43. void
  44. nvkm_wrport(struct nvkm_device *device, int head, u16 port, u8 data)
  45. {
  46. if (device->card_type >= NV_50)
  47. nvkm_wr08(device, 0x601000 + port, data);
  48. else
  49. if (port == 0x03c0 || port == 0x03c1 || /* AR */
  50. port == 0x03c2 || port == 0x03da || /* INP0 */
  51. port == 0x03d4 || port == 0x03d5) /* CR */
  52. nvkm_wr08(device, 0x601000 + (head * 0x2000) + port, data);
  53. else
  54. if (port == 0x03c2 || port == 0x03cc || /* MISC */
  55. port == 0x03c4 || port == 0x03c5 || /* SR */
  56. port == 0x03ce || port == 0x03cf) { /* GR */
  57. if (device->card_type < NV_40)
  58. head = 0; /* CR44 selects head */
  59. nvkm_wr08(device, 0x0c0000 + (head * 0x2000) + port, data);
  60. }
  61. }
  62. u8
  63. nvkm_rdvgas(struct nvkm_device *device, int head, u8 index)
  64. {
  65. nvkm_wrport(device, head, 0x03c4, index);
  66. return nvkm_rdport(device, head, 0x03c5);
  67. }
  68. void
  69. nvkm_wrvgas(struct nvkm_device *device, int head, u8 index, u8 value)
  70. {
  71. nvkm_wrport(device, head, 0x03c4, index);
  72. nvkm_wrport(device, head, 0x03c5, value);
  73. }
  74. u8
  75. nvkm_rdvgag(struct nvkm_device *device, int head, u8 index)
  76. {
  77. nvkm_wrport(device, head, 0x03ce, index);
  78. return nvkm_rdport(device, head, 0x03cf);
  79. }
  80. void
  81. nvkm_wrvgag(struct nvkm_device *device, int head, u8 index, u8 value)
  82. {
  83. nvkm_wrport(device, head, 0x03ce, index);
  84. nvkm_wrport(device, head, 0x03cf, value);
  85. }
  86. u8
  87. nvkm_rdvgac(struct nvkm_device *device, int head, u8 index)
  88. {
  89. nvkm_wrport(device, head, 0x03d4, index);
  90. return nvkm_rdport(device, head, 0x03d5);
  91. }
  92. void
  93. nvkm_wrvgac(struct nvkm_device *device, int head, u8 index, u8 value)
  94. {
  95. nvkm_wrport(device, head, 0x03d4, index);
  96. nvkm_wrport(device, head, 0x03d5, value);
  97. }
  98. u8
  99. nvkm_rdvgai(struct nvkm_device *device, int head, u16 port, u8 index)
  100. {
  101. if (port == 0x03c4) return nvkm_rdvgas(device, head, index);
  102. if (port == 0x03ce) return nvkm_rdvgag(device, head, index);
  103. if (port == 0x03d4) return nvkm_rdvgac(device, head, index);
  104. return 0x00;
  105. }
  106. void
  107. nvkm_wrvgai(struct nvkm_device *device, int head, u16 port, u8 index, u8 value)
  108. {
  109. if (port == 0x03c4) nvkm_wrvgas(device, head, index, value);
  110. else if (port == 0x03ce) nvkm_wrvgag(device, head, index, value);
  111. else if (port == 0x03d4) nvkm_wrvgac(device, head, index, value);
  112. }
  113. bool
  114. nvkm_lockvgac(struct nvkm_device *device, bool lock)
  115. {
  116. bool locked = !nvkm_rdvgac(device, 0, 0x1f);
  117. u8 data = lock ? 0x99 : 0x57;
  118. if (device->card_type < NV_50)
  119. nvkm_wrvgac(device, 0, 0x1f, data);
  120. else
  121. nvkm_wrvgac(device, 0, 0x3f, data);
  122. if (device->chipset == 0x11) {
  123. if (!(nvkm_rd32(device, 0x001084) & 0x10000000))
  124. nvkm_wrvgac(device, 1, 0x1f, data);
  125. }
  126. return locked;
  127. }
  128. /* CR44 takes values 0 (head A), 3 (head B) and 4 (heads tied)
  129. * it affects only the 8 bit vga io regs, which we access using mmio at
  130. * 0xc{0,2}3c*, 0x60{1,3}3*, and 0x68{1,3}3d*
  131. * in general, the set value of cr44 does not matter: reg access works as
  132. * expected and values can be set for the appropriate head by using a 0x2000
  133. * offset as required
  134. * however:
  135. * a) pre nv40, the head B range of PRMVIO regs at 0xc23c* was not exposed and
  136. * cr44 must be set to 0 or 3 for accessing values on the correct head
  137. * through the common 0xc03c* addresses
  138. * b) in tied mode (4) head B is programmed to the values set on head A, and
  139. * access using the head B addresses can have strange results, ergo we leave
  140. * tied mode in init once we know to what cr44 should be restored on exit
  141. *
  142. * the owner parameter is slightly abused:
  143. * 0 and 1 are treated as head values and so the set value is (owner * 3)
  144. * other values are treated as literal values to set
  145. */
  146. u8
  147. nvkm_rdvgaowner(struct nvkm_device *device)
  148. {
  149. if (device->card_type < NV_50) {
  150. if (device->chipset == 0x11) {
  151. u32 tied = nvkm_rd32(device, 0x001084) & 0x10000000;
  152. if (tied == 0) {
  153. u8 slA = nvkm_rdvgac(device, 0, 0x28) & 0x80;
  154. u8 tvA = nvkm_rdvgac(device, 0, 0x33) & 0x01;
  155. u8 slB = nvkm_rdvgac(device, 1, 0x28) & 0x80;
  156. u8 tvB = nvkm_rdvgac(device, 1, 0x33) & 0x01;
  157. if (slA && !tvA) return 0x00;
  158. if (slB && !tvB) return 0x03;
  159. if (slA) return 0x00;
  160. if (slB) return 0x03;
  161. return 0x00;
  162. }
  163. return 0x04;
  164. }
  165. return nvkm_rdvgac(device, 0, 0x44);
  166. }
  167. return 0x00;
  168. }
  169. void
  170. nvkm_wrvgaowner(struct nvkm_device *device, u8 select)
  171. {
  172. if (device->card_type < NV_50) {
  173. u8 owner = (select == 1) ? 3 : select;
  174. if (device->chipset == 0x11) {
  175. /* workaround hw lockup bug */
  176. nvkm_rdvgac(device, 0, 0x1f);
  177. nvkm_rdvgac(device, 1, 0x1f);
  178. }
  179. nvkm_wrvgac(device, 0, 0x44, owner);
  180. if (device->chipset == 0x11) {
  181. nvkm_wrvgac(device, 0, 0x2e, owner);
  182. nvkm_wrvgac(device, 0, 0x2e, owner);
  183. }
  184. }
  185. }