1/* $NetBSD: nouveau_subdev_bios_gpio.c,v 1.1.1.1 2014/08/06 12:36:28 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
27#include <sys/cdefs.h>
28__KERNEL_RCSID(0, "$NetBSD: nouveau_subdev_bios_gpio.c,v 1.1.1.1 2014/08/06 12:36:28 riastradh Exp $");
29
30#include <subdev/bios.h>
31#include <subdev/bios/dcb.h>
32#include <subdev/bios/gpio.h>
33#include <subdev/bios/xpio.h>
34
35u16
36dcb_gpio_table(struct nouveau_bios *bios, u8 *ver, u8 *hdr, u8 *cnt, u8 *len)
37{
38 u16 data = 0x0000;
39 u16 dcb = dcb_table(bios, ver, hdr, cnt, len);
40 if (dcb) {
41 if (*ver >= 0x30 && *hdr >= 0x0c)
42 data = nv_ro16(bios, dcb + 0x0a);
43 else
44 if (*ver >= 0x22 && nv_ro08(bios, dcb - 1) >= 0x13)
45 data = nv_ro16(bios, dcb - 0x0f);
46
47 if (data) {
48 *ver = nv_ro08(bios, data + 0x00);
49 if (*ver < 0x30) {
50 *hdr = 3;
51 *cnt = nv_ro08(bios, data + 0x02);
52 *len = nv_ro08(bios, data + 0x01);
53 } else
54 if (*ver <= 0x41) {
55 *hdr = nv_ro08(bios, data + 0x01);
56 *cnt = nv_ro08(bios, data + 0x02);
57 *len = nv_ro08(bios, data + 0x03);
58 } else {
59 data = 0x0000;
60 }
61 }
62 }
63 return data;
64}
65
66u16
67dcb_gpio_entry(struct nouveau_bios *bios, int idx, int ent, u8 *ver, u8 *len)
68{
69 u8 hdr, cnt, xver; /* use gpio version for xpio entry parsing */
70 u16 gpio;
71
72 if (!idx--)
73 gpio = dcb_gpio_table(bios, ver, &hdr, &cnt, len);
74 else
75 gpio = dcb_xpio_table(bios, idx, &xver, &hdr, &cnt, len);
76
77 if (gpio && ent < cnt)
78 return gpio + hdr + (ent * *len);
79 return 0x0000;
80}
81
82u16
83dcb_gpio_parse(struct nouveau_bios *bios, int idx, int ent, u8 *ver, u8 *len,
84 struct dcb_gpio_func *gpio)
85{
86 u16 data = dcb_gpio_entry(bios, idx, ent, ver, len);
87 if (data) {
88 if (*ver < 0x40) {
89 u16 info = nv_ro16(bios, data);
90 *gpio = (struct dcb_gpio_func) {
91 .line = (info & 0x001f) >> 0,
92 .func = (info & 0x07e0) >> 5,
93 .log[0] = (info & 0x1800) >> 11,
94 .log[1] = (info & 0x6000) >> 13,
95 .param = !!(info & 0x8000),
96 };
97 } else
98 if (*ver < 0x41) {
99 u32 info = nv_ro32(bios, data);
100 *gpio = (struct dcb_gpio_func) {
101 .line = (info & 0x0000001f) >> 0,
102 .func = (info & 0x0000ff00) >> 8,
103 .log[0] = (info & 0x18000000) >> 27,
104 .log[1] = (info & 0x60000000) >> 29,
105 .param = !!(info & 0x80000000),
106 };
107 } else {
108 u32 info = nv_ro32(bios, data + 0);
109 u8 info1 = nv_ro32(bios, data + 4);
110 *gpio = (struct dcb_gpio_func) {
111 .line = (info & 0x0000003f) >> 0,
112 .func = (info & 0x0000ff00) >> 8,
113 .log[0] = (info1 & 0x30) >> 4,
114 .log[1] = (info1 & 0xc0) >> 6,
115 .param = !!(info & 0x80000000),
116 };
117 }
118 }
119
120 return data;
121}
122
123u16
124dcb_gpio_match(struct nouveau_bios *bios, int idx, u8 func, u8 line,
125 u8 *ver, u8 *len, struct dcb_gpio_func *gpio)
126{
127 u8 hdr, cnt, i = 0;
128 u16 data;
129
130 while ((data = dcb_gpio_parse(bios, idx, i++, ver, len, gpio))) {
131 if ((line == 0xff || line == gpio->line) &&
132 (func == 0xff || func == gpio->func))
133 return data;
134 }
135
136 /* DCB 2.2, fixed TVDAC GPIO data */
137 if ((data = dcb_table(bios, ver, &hdr, &cnt, len))) {
138 if (*ver >= 0x22 && *ver < 0x30 && func == DCB_GPIO_TVDAC0) {
139 u8 conf = nv_ro08(bios, data - 5);
140 u8 addr = nv_ro08(bios, data - 4);
141 if (conf & 0x01) {
142 *gpio = (struct dcb_gpio_func) {
143 .func = DCB_GPIO_TVDAC0,
144 .line = addr >> 4,
145 .log[0] = !!(conf & 0x02),
146 .log[1] = !(conf & 0x02),
147 };
148 *ver = 0x00;
149 return data;
150 }
151 }
152 }
153
154 return 0x0000;
155}
156