mgag200_drv.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright 2010 Matt Turner.
  4. * Copyright 2012 Red Hat
  5. *
  6. * Authors: Matthew Garrett
  7. * Matt Turner
  8. * Dave Airlie
  9. */
  10. #ifndef __MGAG200_DRV_H__
  11. #define __MGAG200_DRV_H__
  12. #include <video/vga.h>
  13. #include <drm/drm_connector.h>
  14. #include <drm/drm_crtc.h>
  15. #include <drm/drm_encoder.h>
  16. #include <drm/drm_gem.h>
  17. #include <drm/drm_gem_shmem_helper.h>
  18. #include <drm/drm_plane.h>
  19. #include "mgag200_reg.h"
  20. #define DRIVER_AUTHOR "Matthew Garrett"
  21. #define DRIVER_NAME "mgag200"
  22. #define DRIVER_DESC "MGA G200 SE"
  23. #define DRIVER_MAJOR 1
  24. #define DRIVER_MINOR 0
  25. #define DRIVER_PATCHLEVEL 0
  26. #define RREG8(reg) ioread8(((void __iomem *)mdev->rmmio) + (reg))
  27. #define WREG8(reg, v) iowrite8(v, ((void __iomem *)mdev->rmmio) + (reg))
  28. #define RREG32(reg) ioread32(((void __iomem *)mdev->rmmio) + (reg))
  29. #define WREG32(reg, v) iowrite32(v, ((void __iomem *)mdev->rmmio) + (reg))
  30. #define MGA_BIOS_OFFSET 0x7ffc
  31. #define ATTR_INDEX 0x1fc0
  32. #define ATTR_DATA 0x1fc1
  33. #define WREG_MISC(v) \
  34. WREG8(MGA_MISC_OUT, v)
  35. #define RREG_MISC(v) \
  36. ((v) = RREG8(MGA_MISC_IN))
  37. #define WREG_MISC_MASKED(v, mask) \
  38. do { \
  39. u8 misc_; \
  40. u8 mask_ = (mask); \
  41. RREG_MISC(misc_); \
  42. misc_ &= ~mask_; \
  43. misc_ |= ((v) & mask_); \
  44. WREG_MISC(misc_); \
  45. } while (0)
  46. #define WREG_ATTR(reg, v) \
  47. do { \
  48. RREG8(0x1fda); \
  49. WREG8(ATTR_INDEX, reg); \
  50. WREG8(ATTR_DATA, v); \
  51. } while (0) \
  52. #define RREG_SEQ(reg, v) \
  53. do { \
  54. WREG8(MGAREG_SEQ_INDEX, reg); \
  55. v = RREG8(MGAREG_SEQ_DATA); \
  56. } while (0) \
  57. #define WREG_SEQ(reg, v) \
  58. do { \
  59. WREG8(MGAREG_SEQ_INDEX, reg); \
  60. WREG8(MGAREG_SEQ_DATA, v); \
  61. } while (0) \
  62. #define RREG_CRT(reg, v) \
  63. do { \
  64. WREG8(MGAREG_CRTC_INDEX, reg); \
  65. v = RREG8(MGAREG_CRTC_DATA); \
  66. } while (0) \
  67. #define WREG_CRT(reg, v) \
  68. do { \
  69. WREG8(MGAREG_CRTC_INDEX, reg); \
  70. WREG8(MGAREG_CRTC_DATA, v); \
  71. } while (0) \
  72. #define RREG_ECRT(reg, v) \
  73. do { \
  74. WREG8(MGAREG_CRTCEXT_INDEX, reg); \
  75. v = RREG8(MGAREG_CRTCEXT_DATA); \
  76. } while (0) \
  77. #define WREG_ECRT(reg, v) \
  78. do { \
  79. WREG8(MGAREG_CRTCEXT_INDEX, reg); \
  80. WREG8(MGAREG_CRTCEXT_DATA, v); \
  81. } while (0) \
  82. #define GFX_INDEX 0x1fce
  83. #define GFX_DATA 0x1fcf
  84. #define WREG_GFX(reg, v) \
  85. do { \
  86. WREG8(GFX_INDEX, reg); \
  87. WREG8(GFX_DATA, v); \
  88. } while (0) \
  89. #define DAC_INDEX 0x3c00
  90. #define DAC_DATA 0x3c0a
  91. #define RREG_DAC(reg) \
  92. ({ \
  93. WREG8(DAC_INDEX, reg); \
  94. RREG8(DAC_DATA); \
  95. }) \
  96. #define WREG_DAC(reg, v) \
  97. do { \
  98. WREG8(DAC_INDEX, reg); \
  99. WREG8(DAC_DATA, v); \
  100. } while (0) \
  101. #define MGA_MISC_OUT 0x1fc2
  102. #define MGA_MISC_IN 0x1fcc
  103. /*
  104. * TODO: This is a pretty large set of default values for all kinds of
  105. * settings. It should be split and set in the various DRM helpers,
  106. * such as the CRTC reset or atomic_enable helpers. The PLL values
  107. * probably belong to each model's PLL code.
  108. */
  109. #define MGAG200_DAC_DEFAULT(xvrefctrl, xpixclkctrl, xmiscctrl, xsyspllm, xsysplln, xsyspllp) \
  110. /* 0x00: */ 0, 0, 0, 0, 0, 0, 0x00, 0, \
  111. /* 0x08: */ 0, 0, 0, 0, 0, 0, 0, 0, \
  112. /* 0x10: */ 0, 0, 0, 0, 0, 0, 0, 0, \
  113. /* 0x18: */ (xvrefctrl), \
  114. /* 0x19: */ 0, \
  115. /* 0x1a: */ (xpixclkctrl), \
  116. /* 0x1b: */ 0xff, 0xbf, 0x20, \
  117. /* 0x1e: */ (xmiscctrl), \
  118. /* 0x1f: */ 0x20, \
  119. /* 0x20: */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
  120. /* 0x28: */ 0x00, 0x00, 0x00, 0x00, \
  121. /* 0x2c: */ (xsyspllm), \
  122. /* 0x2d: */ (xsysplln), \
  123. /* 0x2e: */ (xsyspllp), \
  124. /* 0x2f: */ 0x40, \
  125. /* 0x30: */ 0x00, 0xb0, 0x00, 0xc2, 0x34, 0x14, 0x02, 0x83, \
  126. /* 0x38: */ 0x00, 0x93, 0x00, 0x77, 0x00, 0x00, 0x00, 0x3a, \
  127. /* 0x40: */ 0, 0, 0, 0, 0, 0, 0, 0, \
  128. /* 0x48: */ 0, 0, 0, 0, 0, 0, 0, 0 \
  129. #define MGAG200_LUT_SIZE 256
  130. #define MGAG200_MAX_FB_HEIGHT 4096
  131. #define MGAG200_MAX_FB_WIDTH 4096
  132. struct mga_device;
  133. /*
  134. * Stores parameters for programming the PLLs
  135. *
  136. * Fref: reference frequency (A: 25.175 Mhz, B: 28.361, C: XX Mhz)
  137. * Fo: output frequency
  138. * Fvco = Fref * (N / M)
  139. * Fo = Fvco / P
  140. *
  141. * S = [0..3]
  142. */
  143. struct mgag200_pll_values {
  144. unsigned int m;
  145. unsigned int n;
  146. unsigned int p;
  147. unsigned int s;
  148. };
  149. struct mgag200_crtc_state {
  150. struct drm_crtc_state base;
  151. /* Primary-plane format; required for modesetting and color mgmt. */
  152. const struct drm_format_info *format;
  153. struct mgag200_pll_values pixpllc;
  154. bool set_vidrst;
  155. };
  156. static inline struct mgag200_crtc_state *to_mgag200_crtc_state(struct drm_crtc_state *base)
  157. {
  158. return container_of(base, struct mgag200_crtc_state, base);
  159. }
  160. enum mga_type {
  161. G200_PCI,
  162. G200_AGP,
  163. G200_SE_A,
  164. G200_SE_B,
  165. G200_WB,
  166. G200_EV,
  167. G200_EH,
  168. G200_EH3,
  169. G200_EH5,
  170. G200_ER,
  171. G200_EW3,
  172. };
  173. struct mgag200_device_info {
  174. u16 max_hdisplay;
  175. u16 max_vdisplay;
  176. /*
  177. * Maximum memory bandwidth (MiB/sec). Setting this to zero disables
  178. * the rsp test during mode validation.
  179. */
  180. unsigned long max_mem_bandwidth;
  181. /* Synchronize scanout with BMC */
  182. bool sync_bmc:1;
  183. struct {
  184. unsigned data_bit:3;
  185. unsigned clock_bit:3;
  186. } i2c;
  187. /*
  188. * HW does not handle 'startadd' register correctly. Always set
  189. * it's value to 0.
  190. */
  191. bool bug_no_startadd:1;
  192. };
  193. #define MGAG200_DEVICE_INFO_INIT(_max_hdisplay, _max_vdisplay, _max_mem_bandwidth, \
  194. _sync_bmc, _i2c_data_bit, _i2c_clock_bit, \
  195. _bug_no_startadd) \
  196. { \
  197. .max_hdisplay = (_max_hdisplay), \
  198. .max_vdisplay = (_max_vdisplay), \
  199. .max_mem_bandwidth = (_max_mem_bandwidth), \
  200. .sync_bmc = (_sync_bmc), \
  201. .i2c = { \
  202. .data_bit = (_i2c_data_bit), \
  203. .clock_bit = (_i2c_clock_bit), \
  204. }, \
  205. .bug_no_startadd = (_bug_no_startadd), \
  206. }
  207. struct mgag200_device_funcs {
  208. /*
  209. * Validate that the given state can be programmed into PIXPLLC. On
  210. * success, the calculated parameters should be stored in the CRTC's
  211. * state in struct @mgag200_crtc_state.pixpllc.
  212. */
  213. int (*pixpllc_atomic_check)(struct drm_crtc *crtc, struct drm_atomic_state *new_state);
  214. /*
  215. * Program PIXPLLC from the CRTC state. The parameters should have been
  216. * stored in struct @mgag200_crtc_state.pixpllc by the corresponding
  217. * implementation of @pixpllc_atomic_check.
  218. */
  219. void (*pixpllc_atomic_update)(struct drm_crtc *crtc, struct drm_atomic_state *old_state);
  220. };
  221. struct mga_device {
  222. struct drm_device base;
  223. const struct mgag200_device_info *info;
  224. const struct mgag200_device_funcs *funcs;
  225. struct resource *rmmio_res;
  226. void __iomem *rmmio;
  227. struct mutex rmmio_lock; /* Protects access to rmmio */
  228. struct resource *vram_res;
  229. void __iomem *vram;
  230. resource_size_t vram_available;
  231. struct drm_plane primary_plane;
  232. struct drm_crtc crtc;
  233. struct {
  234. struct {
  235. struct drm_encoder encoder;
  236. struct drm_connector connector;
  237. } vga;
  238. } output;
  239. };
  240. static inline struct mga_device *to_mga_device(struct drm_device *dev)
  241. {
  242. return container_of(dev, struct mga_device, base);
  243. }
  244. struct mgag200_g200_device {
  245. struct mga_device base;
  246. /* PLL constants */
  247. long ref_clk;
  248. long pclk_min;
  249. long pclk_max;
  250. };
  251. static inline struct mgag200_g200_device *to_mgag200_g200_device(struct drm_device *dev)
  252. {
  253. return container_of(to_mga_device(dev), struct mgag200_g200_device, base);
  254. }
  255. struct mgag200_g200se_device {
  256. struct mga_device base;
  257. /* SE model number stored in reg 0x1e24 */
  258. u32 unique_rev_id;
  259. };
  260. static inline struct mgag200_g200se_device *to_mgag200_g200se_device(struct drm_device *dev)
  261. {
  262. return container_of(to_mga_device(dev), struct mgag200_g200se_device, base);
  263. }
  264. /* mgag200_drv.c */
  265. int mgag200_init_pci_options(struct pci_dev *pdev, u32 option, u32 option2);
  266. resource_size_t mgag200_probe_vram(void __iomem *mem, resource_size_t size);
  267. resource_size_t mgag200_device_probe_vram(struct mga_device *mdev);
  268. int mgag200_device_preinit(struct mga_device *mdev);
  269. int mgag200_device_init(struct mga_device *mdev,
  270. const struct mgag200_device_info *info,
  271. const struct mgag200_device_funcs *funcs);
  272. /* mgag200_<device type>.c */
  273. struct mga_device *mgag200_g200_device_create(struct pci_dev *pdev, const struct drm_driver *drv);
  274. struct mga_device *mgag200_g200se_device_create(struct pci_dev *pdev, const struct drm_driver *drv,
  275. enum mga_type type);
  276. void mgag200_g200wb_init_registers(struct mga_device *mdev);
  277. void mgag200_g200wb_pixpllc_atomic_update(struct drm_crtc *crtc, struct drm_atomic_state *old_state);
  278. struct mga_device *mgag200_g200wb_device_create(struct pci_dev *pdev, const struct drm_driver *drv);
  279. struct mga_device *mgag200_g200ev_device_create(struct pci_dev *pdev, const struct drm_driver *drv);
  280. void mgag200_g200eh_init_registers(struct mga_device *mdev);
  281. void mgag200_g200eh_pixpllc_atomic_update(struct drm_crtc *crtc, struct drm_atomic_state *old_state);
  282. struct mga_device *mgag200_g200eh_device_create(struct pci_dev *pdev,
  283. const struct drm_driver *drv);
  284. struct mga_device *mgag200_g200eh3_device_create(struct pci_dev *pdev,
  285. const struct drm_driver *drv);
  286. struct mga_device *mgag200_g200eh5_device_create(struct pci_dev *pdev,
  287. const struct drm_driver *drv);
  288. struct mga_device *mgag200_g200er_device_create(struct pci_dev *pdev,
  289. const struct drm_driver *drv);
  290. struct mga_device *mgag200_g200ew3_device_create(struct pci_dev *pdev,
  291. const struct drm_driver *drv);
  292. /*
  293. * mgag200_mode.c
  294. */
  295. struct drm_crtc;
  296. struct drm_crtc_state;
  297. struct drm_display_mode;
  298. struct drm_plane;
  299. struct drm_atomic_state;
  300. struct drm_scanout_buffer;
  301. extern const uint32_t mgag200_primary_plane_formats[];
  302. extern const size_t mgag200_primary_plane_formats_size;
  303. extern const uint64_t mgag200_primary_plane_fmtmods[];
  304. int mgag200_primary_plane_helper_atomic_check(struct drm_plane *plane,
  305. struct drm_atomic_state *new_state);
  306. void mgag200_primary_plane_helper_atomic_update(struct drm_plane *plane,
  307. struct drm_atomic_state *old_state);
  308. void mgag200_primary_plane_helper_atomic_enable(struct drm_plane *plane,
  309. struct drm_atomic_state *state);
  310. void mgag200_primary_plane_helper_atomic_disable(struct drm_plane *plane,
  311. struct drm_atomic_state *old_state);
  312. int mgag200_primary_plane_helper_get_scanout_buffer(struct drm_plane *plane,
  313. struct drm_scanout_buffer *sb);
  314. #define MGAG200_PRIMARY_PLANE_HELPER_FUNCS \
  315. DRM_GEM_SHADOW_PLANE_HELPER_FUNCS, \
  316. .atomic_check = mgag200_primary_plane_helper_atomic_check, \
  317. .atomic_update = mgag200_primary_plane_helper_atomic_update, \
  318. .atomic_enable = mgag200_primary_plane_helper_atomic_enable, \
  319. .atomic_disable = mgag200_primary_plane_helper_atomic_disable, \
  320. .get_scanout_buffer = mgag200_primary_plane_helper_get_scanout_buffer
  321. #define MGAG200_PRIMARY_PLANE_FUNCS \
  322. .update_plane = drm_atomic_helper_update_plane, \
  323. .disable_plane = drm_atomic_helper_disable_plane, \
  324. .destroy = drm_plane_cleanup, \
  325. DRM_GEM_SHADOW_PLANE_FUNCS
  326. void mgag200_crtc_fill_gamma(struct mga_device *mdev, const struct drm_format_info *format);
  327. void mgag200_crtc_load_gamma(struct mga_device *mdev,
  328. const struct drm_format_info *format,
  329. struct drm_color_lut *lut);
  330. enum drm_mode_status mgag200_crtc_helper_mode_valid(struct drm_crtc *crtc,
  331. const struct drm_display_mode *mode);
  332. int mgag200_crtc_helper_atomic_check(struct drm_crtc *crtc, struct drm_atomic_state *new_state);
  333. void mgag200_crtc_helper_atomic_flush(struct drm_crtc *crtc, struct drm_atomic_state *old_state);
  334. void mgag200_crtc_helper_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_state *old_state);
  335. void mgag200_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atomic_state *old_state);
  336. #define MGAG200_CRTC_HELPER_FUNCS \
  337. .mode_valid = mgag200_crtc_helper_mode_valid, \
  338. .atomic_check = mgag200_crtc_helper_atomic_check, \
  339. .atomic_flush = mgag200_crtc_helper_atomic_flush, \
  340. .atomic_enable = mgag200_crtc_helper_atomic_enable, \
  341. .atomic_disable = mgag200_crtc_helper_atomic_disable
  342. void mgag200_crtc_reset(struct drm_crtc *crtc);
  343. struct drm_crtc_state *mgag200_crtc_atomic_duplicate_state(struct drm_crtc *crtc);
  344. void mgag200_crtc_atomic_destroy_state(struct drm_crtc *crtc, struct drm_crtc_state *crtc_state);
  345. #define MGAG200_CRTC_FUNCS \
  346. .reset = mgag200_crtc_reset, \
  347. .destroy = drm_crtc_cleanup, \
  348. .set_config = drm_atomic_helper_set_config, \
  349. .page_flip = drm_atomic_helper_page_flip, \
  350. .atomic_duplicate_state = mgag200_crtc_atomic_duplicate_state, \
  351. .atomic_destroy_state = mgag200_crtc_atomic_destroy_state
  352. void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mode *mode,
  353. bool set_vidrst);
  354. void mgag200_set_format_regs(struct mga_device *mdev, const struct drm_format_info *format);
  355. void mgag200_enable_display(struct mga_device *mdev);
  356. void mgag200_init_registers(struct mga_device *mdev);
  357. int mgag200_mode_config_init(struct mga_device *mdev, resource_size_t vram_available);
  358. /* mgag200_vga_bmc.c */
  359. int mgag200_vga_bmc_output_init(struct mga_device *mdev);
  360. /* mgag200_vga.c */
  361. int mgag200_vga_output_init(struct mga_device *mdev);
  362. /* mgag200_bmc.c */
  363. void mgag200_bmc_stop_scanout(struct mga_device *mdev);
  364. void mgag200_bmc_start_scanout(struct mga_device *mdev);
  365. #endif /* __MGAG200_DRV_H__ */