1 /* $NetBSD: nouveau_nvkm_subdev_gpio_nv50.c,v 1.4 2021/12/18 23:45:39 riastradh Exp $ */
2
3 /*
4 * Copyright 2012 Red Hat Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Ben Skeggs
25 */
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_gpio_nv50.c,v 1.4 2021/12/18 23:45:39 riastradh Exp $");
28
29 #include "priv.h"
30
31 void
nv50_gpio_reset(struct nvkm_gpio * gpio,u8 match)32 nv50_gpio_reset(struct nvkm_gpio *gpio, u8 match)
33 {
34 struct nvkm_device *device = gpio->subdev.device;
35 struct nvkm_bios *bios = device->bios;
36 u8 ver, len;
37 u16 entry;
38 int ent = -1;
39
40 while ((entry = dcb_gpio_entry(bios, 0, ++ent, &ver, &len))) {
41 static const u32 regs[] = { 0xe100, 0xe28c };
42 u32 data = nvbios_rd32(bios, entry);
43 u8 line = (data & 0x0000001f);
44 u8 func = (data & 0x0000ff00) >> 8;
45 u8 defs = !!(data & 0x01000000);
46 u8 unk0 = !!(data & 0x02000000);
47 u8 unk1 = !!(data & 0x04000000);
48 u32 val = (unk1 << 16) | unk0;
49 u32 reg = regs[line >> 4];
50 u32 lsh = line & 0x0f;
51
52 if ( func == DCB_GPIO_UNUSED ||
53 (match != DCB_GPIO_UNUSED && match != func))
54 continue;
55
56 nvkm_gpio_set(gpio, 0, func, line, defs);
57
58 nvkm_mask(device, reg, 0x00010001U << lsh, val << lsh);
59 }
60 }
61
62 static int
nv50_gpio_location(int line,u32 * reg,u32 * shift)63 nv50_gpio_location(int line, u32 *reg, u32 *shift)
64 {
65 const u32 nv50_gpio_reg[4] = { 0xe104, 0xe108, 0xe280, 0xe284 };
66
67 if (line >= 32)
68 return -EINVAL;
69
70 *reg = nv50_gpio_reg[line >> 3];
71 *shift = (line & 7) << 2;
72 return 0;
73 }
74
75 int
nv50_gpio_drive(struct nvkm_gpio * gpio,int line,int dir,int out)76 nv50_gpio_drive(struct nvkm_gpio *gpio, int line, int dir, int out)
77 {
78 struct nvkm_device *device = gpio->subdev.device;
79 u32 reg, shift;
80
81 if (nv50_gpio_location(line, ®, &shift))
82 return -EINVAL;
83
84 nvkm_mask(device, reg, 3 << shift, (((dir ^ 1) << 1) | out) << shift);
85 return 0;
86 }
87
88 int
nv50_gpio_sense(struct nvkm_gpio * gpio,int line)89 nv50_gpio_sense(struct nvkm_gpio *gpio, int line)
90 {
91 struct nvkm_device *device = gpio->subdev.device;
92 u32 reg, shift;
93
94 if (nv50_gpio_location(line, ®, &shift))
95 return -EINVAL;
96
97 return !!(nvkm_rd32(device, reg) & (4 << shift));
98 }
99
100 static void
nv50_gpio_intr_stat(struct nvkm_gpio * gpio,u32 * hi,u32 * lo)101 nv50_gpio_intr_stat(struct nvkm_gpio *gpio, u32 *hi, u32 *lo)
102 {
103 struct nvkm_device *device = gpio->subdev.device;
104 u32 intr = nvkm_rd32(device, 0x00e054);
105 u32 stat = nvkm_rd32(device, 0x00e050) & intr;
106 *lo = (stat & 0xffff0000) >> 16;
107 *hi = (stat & 0x0000ffff);
108 nvkm_wr32(device, 0x00e054, intr);
109 }
110
111 static void
nv50_gpio_intr_mask(struct nvkm_gpio * gpio,u32 type,u32 mask,u32 data)112 nv50_gpio_intr_mask(struct nvkm_gpio *gpio, u32 type, u32 mask, u32 data)
113 {
114 struct nvkm_device *device = gpio->subdev.device;
115 u32 inte = nvkm_rd32(device, 0x00e050);
116 if (type & NVKM_GPIO_LO)
117 inte = (inte & ~(mask << 16)) | (data << 16);
118 if (type & NVKM_GPIO_HI)
119 inte = (inte & ~mask) | data;
120 nvkm_wr32(device, 0x00e050, inte);
121 }
122
123 static const struct nvkm_gpio_func
124 nv50_gpio = {
125 .lines = 16,
126 .intr_stat = nv50_gpio_intr_stat,
127 .intr_mask = nv50_gpio_intr_mask,
128 .drive = nv50_gpio_drive,
129 .sense = nv50_gpio_sense,
130 .reset = nv50_gpio_reset,
131 };
132
133 int
nv50_gpio_new(struct nvkm_device * device,int index,struct nvkm_gpio ** pgpio)134 nv50_gpio_new(struct nvkm_device *device, int index, struct nvkm_gpio **pgpio)
135 {
136 return nvkm_gpio_new_(&nv50_gpio, device, index, pgpio);
137 }
138