1 /*
2  * Copyright 2021 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 #include "priv.h"
23 
24 static void
25 ga102_gpio_reset(struct nvkm_gpio *gpio, u8 match)
26 {
27 	struct nvkm_device *device = gpio->subdev.device;
28 	struct nvkm_bios *bios = device->bios;
29 	u8 ver, len;
30 	u16 entry;
31 	int ent = -1;
32 
33 	while ((entry = dcb_gpio_entry(bios, 0, ++ent, &ver, &len))) {
34 		u32 data = nvbios_rd32(bios, entry);
35 		u8  line =   (data & 0x0000003f);
36 		u8  defs = !!(data & 0x00000080);
37 		u8  func =   (data & 0x0000ff00) >> 8;
38 		u8  unk0 =   (data & 0x00ff0000) >> 16;
39 		u8  unk1 =   (data & 0x1f000000) >> 24;
40 
41 		if ( func  == DCB_GPIO_UNUSED ||
42 		    (match != DCB_GPIO_UNUSED && match != func))
43 			continue;
44 
45 		nvkm_gpio_set(gpio, 0, func, line, defs);
46 
47 		nvkm_mask(device, 0x021200 + (line * 4), 0xff, unk0);
48 		if (unk1--)
49 			nvkm_mask(device, 0x00d740 + (unk1 * 4), 0xff, line);
50 	}
51 }
52 
53 static int
54 ga102_gpio_drive(struct nvkm_gpio *gpio, int line, int dir, int out)
55 {
56 	struct nvkm_device *device = gpio->subdev.device;
57 	u32 data = ((dir ^ 1) << 13) | (out << 12);
58 	nvkm_mask(device, 0x021200 + (line * 4), 0x00003000, data);
59 	nvkm_mask(device, 0x00d604, 0x00000001, 0x00000001); /* update? */
60 	return 0;
61 }
62 
63 static int
64 ga102_gpio_sense(struct nvkm_gpio *gpio, int line)
65 {
66 	struct nvkm_device *device = gpio->subdev.device;
67 	return !!(nvkm_rd32(device, 0x021200 + (line * 4)) & 0x00004000);
68 }
69 
70 static void
71 ga102_gpio_intr_stat(struct nvkm_gpio *gpio, u32 *hi, u32 *lo)
72 {
73 	struct nvkm_device *device = gpio->subdev.device;
74 	u32 intr0 = nvkm_rd32(device, 0x021640);
75 	u32 intr1 = nvkm_rd32(device, 0x02164c);
76 	u32 stat0 = nvkm_rd32(device, 0x021648) & intr0;
77 	u32 stat1 = nvkm_rd32(device, 0x021654) & intr1;
78 	*lo = (stat1 & 0xffff0000) | (stat0 >> 16);
79 	*hi = (stat1 << 16) | (stat0 & 0x0000ffff);
80 	nvkm_wr32(device, 0x021640, intr0);
81 	nvkm_wr32(device, 0x02164c, intr1);
82 }
83 
84 static void
85 ga102_gpio_intr_mask(struct nvkm_gpio *gpio, u32 type, u32 mask, u32 data)
86 {
87 	struct nvkm_device *device = gpio->subdev.device;
88 	u32 inte0 = nvkm_rd32(device, 0x021648);
89 	u32 inte1 = nvkm_rd32(device, 0x021654);
90 	if (type & NVKM_GPIO_LO)
91 		inte0 = (inte0 & ~(mask << 16)) | (data << 16);
92 	if (type & NVKM_GPIO_HI)
93 		inte0 = (inte0 & ~(mask & 0xffff)) | (data & 0xffff);
94 	mask >>= 16;
95 	data >>= 16;
96 	if (type & NVKM_GPIO_LO)
97 		inte1 = (inte1 & ~(mask << 16)) | (data << 16);
98 	if (type & NVKM_GPIO_HI)
99 		inte1 = (inte1 & ~mask) | data;
100 	nvkm_wr32(device, 0x021648, inte0);
101 	nvkm_wr32(device, 0x021654, inte1);
102 }
103 
104 static const struct nvkm_gpio_func
105 ga102_gpio = {
106 	.lines = 32,
107 	.intr_stat = ga102_gpio_intr_stat,
108 	.intr_mask = ga102_gpio_intr_mask,
109 	.drive = ga102_gpio_drive,
110 	.sense = ga102_gpio_sense,
111 	.reset = ga102_gpio_reset,
112 };
113 
114 int
115 ga102_gpio_new(struct nvkm_device *device, enum nvkm_subdev_type type, int inst,
116 	       struct nvkm_gpio **pgpio)
117 {
118 	return nvkm_gpio_new_(&ga102_gpio, device, type, inst, pgpio);
119 }
120