1/* $NetBSD: nouveau_subdev_bios_extdev.c,v 1.1.1.1 2014/08/06 12:36:28 riastradh Exp $ */
2
3/*
4 * Copyright 2012 Nouveau Community
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: Martin Peres
25 */
26
27#include <sys/cdefs.h>
28__KERNEL_RCSID(0, "$NetBSD: nouveau_subdev_bios_extdev.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/extdev.h>
33
34static u16
35extdev_table(struct nouveau_bios *bios, u8 *ver, u8 *hdr, u8 *len, u8 *cnt)
36{
37 u8 dcb_ver, dcb_hdr, dcb_cnt, dcb_len;
38 u16 dcb, extdev = 0;
39
40 dcb = dcb_table(bios, &dcb_ver, &dcb_hdr, &dcb_cnt, &dcb_len);
41 if (!dcb || (dcb_ver != 0x30 && dcb_ver != 0x40))
42 return 0x0000;
43
44 extdev = nv_ro16(bios, dcb + 18);
45 if (!extdev)
46 return 0x0000;
47
48 *ver = nv_ro08(bios, extdev + 0);
49 *hdr = nv_ro08(bios, extdev + 1);
50 *cnt = nv_ro08(bios, extdev + 2);
51 *len = nv_ro08(bios, extdev + 3);
52
53 return extdev + *hdr;
54}
55
56static u16
57nvbios_extdev_entry(struct nouveau_bios *bios, int idx, u8 *ver, u8 *len)
58{
59 u8 hdr, cnt;
60 u16 extdev = extdev_table(bios, ver, &hdr, len, &cnt);
61 if (extdev && idx < cnt)
62 return extdev + idx * *len;
63 return 0x0000;
64}
65
66static void
67extdev_parse_entry(struct nouveau_bios *bios, u16 offset,
68 struct nvbios_extdev_func *entry)
69{
70 entry->type = nv_ro08(bios, offset + 0);
71 entry->addr = nv_ro08(bios, offset + 1);
72 entry->bus = (nv_ro08(bios, offset + 2) >> 4) & 1;
73}
74
75int
76nvbios_extdev_parse(struct nouveau_bios *bios, int idx,
77 struct nvbios_extdev_func *func)
78{
79 u8 ver, len;
80 u16 entry;
81
82 if (!(entry = nvbios_extdev_entry(bios, idx, &ver, &len)))
83 return -EINVAL;
84
85 extdev_parse_entry(bios, entry, func);
86
87 return 0;
88}
89
90int
91nvbios_extdev_find(struct nouveau_bios *bios, enum nvbios_extdev_type type,
92 struct nvbios_extdev_func *func)
93{
94 u8 ver, len, i;
95 u16 entry;
96
97 i = 0;
98 while (!(entry = nvbios_extdev_entry(bios, i++, &ver, &len))) {
99 extdev_parse_entry(bios, entry, func);
100 if (func->type == type)
101 return 0;
102 }
103
104 return -EINVAL;
105}
106