1 /*
2  * Copyright 2013 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 #define gf110_pmu_code gk104_pmu_code
25 #define gf110_pmu_data gk104_pmu_data
26 #include "priv.h"
27 #include "fuc/gf110.fuc4.h"
28 
29 #include <core/device.h>
30 #include <core/option.h>
31 #include <subdev/timer.h>
32 
33 static void
34 magic_(struct nvkm_device *device, u32 ctrl, int size)
35 {
36 	nvkm_wr32(device, 0x00c800, 0x00000000);
37 	nvkm_wr32(device, 0x00c808, 0x00000000);
38 	nvkm_wr32(device, 0x00c800, ctrl);
39 	if (nv_wait(device, 0x00c800, 0x40000000, 0x40000000)) {
40 		while (size--)
41 			nvkm_wr32(device, 0x00c804, 0x00000000);
42 	}
43 	nvkm_wr32(device, 0x00c800, 0x00000000);
44 }
45 
46 static void
47 magic(struct nvkm_device *device, u32 ctrl)
48 {
49 	magic_(device, 0x8000a41f | ctrl, 6);
50 	magic_(device, 0x80000421 | ctrl, 1);
51 }
52 
53 static void
54 gk104_pmu_pgob(struct nvkm_pmu *pmu, bool enable)
55 {
56 	struct nvkm_device *device = pmu->subdev.device;
57 
58 	nvkm_mask(device, 0x000200, 0x00001000, 0x00000000);
59 	nvkm_rd32(device, 0x000200);
60 	nvkm_mask(device, 0x000200, 0x08000000, 0x08000000);
61 	msleep(50);
62 
63 	nvkm_mask(device, 0x10a78c, 0x00000002, 0x00000002);
64 	nvkm_mask(device, 0x10a78c, 0x00000001, 0x00000001);
65 	nvkm_mask(device, 0x10a78c, 0x00000001, 0x00000000);
66 
67 	nvkm_mask(device, 0x020004, 0xc0000000, enable ? 0xc0000000 : 0x40000000);
68 	msleep(50);
69 
70 	nvkm_mask(device, 0x10a78c, 0x00000002, 0x00000000);
71 	nvkm_mask(device, 0x10a78c, 0x00000001, 0x00000001);
72 	nvkm_mask(device, 0x10a78c, 0x00000001, 0x00000000);
73 
74 	nvkm_mask(device, 0x000200, 0x08000000, 0x00000000);
75 	nvkm_mask(device, 0x000200, 0x00001000, 0x00001000);
76 	nvkm_rd32(device, 0x000200);
77 
78 	if (nv_device_match(device, 0x11fc, 0x17aa, 0x2211) /* Lenovo W541 */
79 	 || nv_device_match(device, 0x11fc, 0x17aa, 0x221e) /* Lenovo W541 */
80 	 || nvkm_boolopt(device->cfgopt, "War00C800_0", false)) {
81 		nv_info(pmu, "hw bug workaround enabled\n");
82 		switch (device->chipset) {
83 		case 0xe4:
84 			magic(device, 0x04000000);
85 			magic(device, 0x06000000);
86 			magic(device, 0x0c000000);
87 			magic(device, 0x0e000000);
88 			break;
89 		case 0xe6:
90 			magic(device, 0x02000000);
91 			magic(device, 0x04000000);
92 			magic(device, 0x0a000000);
93 			break;
94 		case 0xe7:
95 			magic(device, 0x02000000);
96 			break;
97 		default:
98 			break;
99 		}
100 	}
101 }
102 
103 struct nvkm_oclass *
104 gk104_pmu_oclass = &(struct nvkm_pmu_impl) {
105 	.base.handle = NV_SUBDEV(PMU, 0xe4),
106 	.base.ofuncs = &(struct nvkm_ofuncs) {
107 		.ctor = _nvkm_pmu_ctor,
108 		.dtor = _nvkm_pmu_dtor,
109 		.init = _nvkm_pmu_init,
110 		.fini = _nvkm_pmu_fini,
111 	},
112 	.code.data = gk104_pmu_code,
113 	.code.size = sizeof(gk104_pmu_code),
114 	.data.data = gk104_pmu_data,
115 	.data.size = sizeof(gk104_pmu_data),
116 	.pgob = gk104_pmu_pgob,
117 }.base;
118