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 "priv.h"
25 
26 #include <core/option.h>
27 #include <subdev/vga.h>
28 
29 u32
30 nvkm_devinit_mmio(struct nvkm_devinit *init, u32 addr)
31 {
32 	if (init->func->mmio)
33 		addr = init->func->mmio(init, addr);
34 	return addr;
35 }
36 
37 int
38 nvkm_devinit_pll_set(struct nvkm_devinit *init, u32 type, u32 khz)
39 {
40 	return init->func->pll_set(init, type, khz);
41 }
42 
43 void
44 nvkm_devinit_meminit(struct nvkm_devinit *init)
45 {
46 	if (init->func->meminit)
47 		init->func->meminit(init);
48 }
49 
50 u64
51 nvkm_devinit_disable(struct nvkm_devinit *init)
52 {
53 	if (init && init->func->disable)
54 		init->func->disable(init);
55 
56 	return 0;
57 }
58 
59 int
60 nvkm_devinit_post(struct nvkm_devinit *init)
61 {
62 	int ret = 0;
63 	if (init && init->func->post)
64 		ret = init->func->post(init, init->post);
65 	nvkm_devinit_disable(init);
66 	return ret;
67 }
68 
69 static int
70 nvkm_devinit_fini(struct nvkm_subdev *subdev, bool suspend)
71 {
72 	struct nvkm_devinit *init = nvkm_devinit(subdev);
73 	/* force full reinit on resume */
74 	if (suspend)
75 		init->post = true;
76 	return 0;
77 }
78 
79 static int
80 nvkm_devinit_preinit(struct nvkm_subdev *subdev)
81 {
82 	struct nvkm_devinit *init = nvkm_devinit(subdev);
83 
84 	if (init->func->preinit)
85 		init->func->preinit(init);
86 
87 	/* Override the post flag during the first call if NvForcePost is set */
88 	if (init->force_post) {
89 		init->post = init->force_post;
90 		init->force_post = false;
91 	}
92 
93 	/* unlock the extended vga crtc regs */
94 	nvkm_lockvgac(subdev->device, false);
95 	return 0;
96 }
97 
98 static int
99 nvkm_devinit_init(struct nvkm_subdev *subdev)
100 {
101 	struct nvkm_devinit *init = nvkm_devinit(subdev);
102 	if (init->func->init)
103 		init->func->init(init);
104 	return 0;
105 }
106 
107 static void *
108 nvkm_devinit_dtor(struct nvkm_subdev *subdev)
109 {
110 	struct nvkm_devinit *init = nvkm_devinit(subdev);
111 	void *data = init;
112 
113 	if (init->func->dtor)
114 		data = init->func->dtor(init);
115 
116 	/* lock crtc regs */
117 	nvkm_lockvgac(subdev->device, true);
118 	return data;
119 }
120 
121 static const struct nvkm_subdev_func
122 nvkm_devinit = {
123 	.dtor = nvkm_devinit_dtor,
124 	.preinit = nvkm_devinit_preinit,
125 	.init = nvkm_devinit_init,
126 	.fini = nvkm_devinit_fini,
127 };
128 
129 void
130 nvkm_devinit_ctor(const struct nvkm_devinit_func *func, struct nvkm_device *device,
131 		  enum nvkm_subdev_type type, int inst, struct nvkm_devinit *init)
132 {
133 	nvkm_subdev_ctor(&nvkm_devinit, device, type, inst, &init->subdev);
134 	init->func = func;
135 	init->force_post = nvkm_boolopt(device->cfgopt, "NvForcePost", false);
136 }
137