xref: /openbmc/linux/drivers/gpu/drm/nouveau/nvkm/subdev/devinit/tu102.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1b51f9dfaSBen Skeggs /*
2b51f9dfaSBen Skeggs  * Copyright 2018 Red Hat Inc.
3b51f9dfaSBen Skeggs  *
4b51f9dfaSBen Skeggs  * Permission is hereby granted, free of charge, to any person obtaining a
5b51f9dfaSBen Skeggs  * copy of this software and associated documentation files (the "Software"),
6b51f9dfaSBen Skeggs  * to deal in the Software without restriction, including without limitation
7b51f9dfaSBen Skeggs  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b51f9dfaSBen Skeggs  * and/or sell copies of the Software, and to permit persons to whom the
9b51f9dfaSBen Skeggs  * Software is furnished to do so, subject to the following conditions:
10b51f9dfaSBen Skeggs  *
11b51f9dfaSBen Skeggs  * The above copyright notice and this permission notice shall be included in
12b51f9dfaSBen Skeggs  * all copies or substantial portions of the Software.
13b51f9dfaSBen Skeggs  *
14b51f9dfaSBen Skeggs  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15b51f9dfaSBen Skeggs  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16b51f9dfaSBen Skeggs  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17b51f9dfaSBen Skeggs  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18b51f9dfaSBen Skeggs  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19b51f9dfaSBen Skeggs  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20b51f9dfaSBen Skeggs  * OTHER DEALINGS IN THE SOFTWARE.
21b51f9dfaSBen Skeggs  */
22b51f9dfaSBen Skeggs #include "nv50.h"
23b51f9dfaSBen Skeggs 
24b51f9dfaSBen Skeggs #include <subdev/bios.h>
25b51f9dfaSBen Skeggs #include <subdev/bios/pll.h>
26b51f9dfaSBen Skeggs #include <subdev/clk/pll.h>
27b51f9dfaSBen Skeggs 
28b51f9dfaSBen Skeggs static int
tu102_devinit_pll_set(struct nvkm_devinit * init,u32 type,u32 freq)29b51f9dfaSBen Skeggs tu102_devinit_pll_set(struct nvkm_devinit *init, u32 type, u32 freq)
30b51f9dfaSBen Skeggs {
31b51f9dfaSBen Skeggs 	struct nvkm_subdev *subdev = &init->subdev;
32b51f9dfaSBen Skeggs 	struct nvkm_device *device = subdev->device;
33b51f9dfaSBen Skeggs 	struct nvbios_pll info;
34b51f9dfaSBen Skeggs 	int head = type - PLL_VPLL0;
35b51f9dfaSBen Skeggs 	int N, fN, M, P;
36b51f9dfaSBen Skeggs 	int ret;
37b51f9dfaSBen Skeggs 
38b51f9dfaSBen Skeggs 	ret = nvbios_pll_parse(device->bios, type, &info);
39b51f9dfaSBen Skeggs 	if (ret)
40b51f9dfaSBen Skeggs 		return ret;
41b51f9dfaSBen Skeggs 
42b51f9dfaSBen Skeggs 	ret = gt215_pll_calc(subdev, &info, freq, &N, &fN, &M, &P);
43b51f9dfaSBen Skeggs 	if (ret < 0)
44b51f9dfaSBen Skeggs 		return ret;
45b51f9dfaSBen Skeggs 
46b51f9dfaSBen Skeggs 	switch (info.type) {
47b51f9dfaSBen Skeggs 	case PLL_VPLL0:
48b51f9dfaSBen Skeggs 	case PLL_VPLL1:
49b51f9dfaSBen Skeggs 	case PLL_VPLL2:
50b51f9dfaSBen Skeggs 	case PLL_VPLL3:
51b51f9dfaSBen Skeggs 		nvkm_wr32(device, 0x00ef10 + (head * 0x40), fN << 16);
52b51f9dfaSBen Skeggs 		nvkm_wr32(device, 0x00ef04 + (head * 0x40), (P << 16) |
53b51f9dfaSBen Skeggs 							    (N <<  8) |
54b51f9dfaSBen Skeggs 							    (M <<  0));
55b51f9dfaSBen Skeggs 		/*XXX*/
56b51f9dfaSBen Skeggs 		nvkm_wr32(device, 0x00ef0c + (head * 0x40), 0x00000900);
57b51f9dfaSBen Skeggs 		nvkm_wr32(device, 0x00ef00 + (head * 0x40), 0x02000014);
58b51f9dfaSBen Skeggs 		break;
59b51f9dfaSBen Skeggs 	default:
60b51f9dfaSBen Skeggs 		nvkm_warn(subdev, "%08x/%dKhz unimplemented\n", type, freq);
61b51f9dfaSBen Skeggs 		ret = -EINVAL;
62b51f9dfaSBen Skeggs 		break;
63b51f9dfaSBen Skeggs 	}
64b51f9dfaSBen Skeggs 
65b51f9dfaSBen Skeggs 	return ret;
66b51f9dfaSBen Skeggs }
67b51f9dfaSBen Skeggs 
68*d22915d2SBen Skeggs static int
tu102_devinit_wait(struct nvkm_device * device)69*d22915d2SBen Skeggs tu102_devinit_wait(struct nvkm_device *device)
70*d22915d2SBen Skeggs {
71*d22915d2SBen Skeggs 	unsigned timeout = 50 + 2000;
72*d22915d2SBen Skeggs 
73*d22915d2SBen Skeggs 	do {
74*d22915d2SBen Skeggs 		if (nvkm_rd32(device, 0x118128) & 0x00000001) {
75*d22915d2SBen Skeggs 			if ((nvkm_rd32(device, 0x118234) & 0x000000ff) == 0xff)
76*d22915d2SBen Skeggs 				return 0;
77*d22915d2SBen Skeggs 		}
78*d22915d2SBen Skeggs 
79*d22915d2SBen Skeggs 		usleep_range(1000, 2000);
80*d22915d2SBen Skeggs 	} while (timeout--);
81*d22915d2SBen Skeggs 
82*d22915d2SBen Skeggs 	return -ETIMEDOUT;
83*d22915d2SBen Skeggs }
84*d22915d2SBen Skeggs 
857ddf5e95SBen Skeggs int
tu102_devinit_post(struct nvkm_devinit * base,bool post)86b51f9dfaSBen Skeggs tu102_devinit_post(struct nvkm_devinit *base, bool post)
87b51f9dfaSBen Skeggs {
88b51f9dfaSBen Skeggs 	struct nv50_devinit *init = nv50_devinit(base);
89*d22915d2SBen Skeggs 	int ret;
90*d22915d2SBen Skeggs 
91*d22915d2SBen Skeggs 	ret = tu102_devinit_wait(init->base.subdev.device);
92*d22915d2SBen Skeggs 	if (ret)
93*d22915d2SBen Skeggs 		return ret;
94*d22915d2SBen Skeggs 
95b51f9dfaSBen Skeggs 	gm200_devinit_preos(init, post);
96b51f9dfaSBen Skeggs 	return 0;
97b51f9dfaSBen Skeggs }
98b51f9dfaSBen Skeggs 
99b51f9dfaSBen Skeggs static const struct nvkm_devinit_func
100b51f9dfaSBen Skeggs tu102_devinit = {
101b51f9dfaSBen Skeggs 	.init = nv50_devinit_init,
102b51f9dfaSBen Skeggs 	.post = tu102_devinit_post,
103b51f9dfaSBen Skeggs 	.pll_set = tu102_devinit_pll_set,
104b51f9dfaSBen Skeggs 	.disable = gm107_devinit_disable,
105b51f9dfaSBen Skeggs };
106b51f9dfaSBen Skeggs 
107b51f9dfaSBen Skeggs int
tu102_devinit_new(struct nvkm_device * device,enum nvkm_subdev_type type,int inst,struct nvkm_devinit ** pinit)1084a34fd0eSBen Skeggs tu102_devinit_new(struct nvkm_device *device, enum nvkm_subdev_type type, int inst,
109b51f9dfaSBen Skeggs 		  struct nvkm_devinit **pinit)
110b51f9dfaSBen Skeggs {
1114a34fd0eSBen Skeggs 	return nv50_devinit_new_(&tu102_devinit, device, type, inst, pinit);
112b51f9dfaSBen Skeggs }
113