1 /*
2  * Copyright 2014 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  * Authors: Ben Skeggs
23  */
24 #include <subdev/bios.h>
25 #include <subdev/bios/bit.h>
26 #include <subdev/bios/M0203.h>
27 
28 u32
nvbios_M0203Te(struct nvkm_bios * bios,u8 * ver,u8 * hdr,u8 * cnt,u8 * len)29 nvbios_M0203Te(struct nvkm_bios *bios, u8 *ver, u8 *hdr, u8 *cnt, u8 *len)
30 {
31 	struct bit_entry bit_M;
32 	u32 data = 0x00000000;
33 
34 	if (!bit_entry(bios, 'M', &bit_M)) {
35 		if (bit_M.version == 2 && bit_M.length > 0x04)
36 			data = nvbios_rd16(bios, bit_M.offset + 0x03);
37 		if (data) {
38 			*ver = nvbios_rd08(bios, data + 0x00);
39 			switch (*ver) {
40 			case 0x10:
41 				*hdr = nvbios_rd08(bios, data + 0x01);
42 				*len = nvbios_rd08(bios, data + 0x02);
43 				*cnt = nvbios_rd08(bios, data + 0x03);
44 				return data;
45 			default:
46 				break;
47 			}
48 		}
49 	}
50 
51 	return 0x00000000;
52 }
53 
54 u32
nvbios_M0203Tp(struct nvkm_bios * bios,u8 * ver,u8 * hdr,u8 * cnt,u8 * len,struct nvbios_M0203T * info)55 nvbios_M0203Tp(struct nvkm_bios *bios, u8 *ver, u8 *hdr, u8 *cnt, u8 *len,
56 	       struct nvbios_M0203T *info)
57 {
58 	u32 data = nvbios_M0203Te(bios, ver, hdr, cnt, len);
59 	memset(info, 0x00, sizeof(*info));
60 	switch (!!data * *ver) {
61 	case 0x10:
62 		info->type    = nvbios_rd08(bios, data + 0x04);
63 		info->pointer = nvbios_rd16(bios, data + 0x05);
64 		break;
65 	default:
66 		break;
67 	}
68 	return data;
69 }
70 
71 u32
nvbios_M0203Ee(struct nvkm_bios * bios,int idx,u8 * ver,u8 * hdr)72 nvbios_M0203Ee(struct nvkm_bios *bios, int idx, u8 *ver, u8 *hdr)
73 {
74 	u8  cnt, len;
75 	u32 data = nvbios_M0203Te(bios, ver, hdr, &cnt, &len);
76 	if (data && idx < cnt) {
77 		data = data + *hdr + idx * len;
78 		*hdr = len;
79 		return data;
80 	}
81 	return 0x00000000;
82 }
83 
84 u32
nvbios_M0203Ep(struct nvkm_bios * bios,int idx,u8 * ver,u8 * hdr,struct nvbios_M0203E * info)85 nvbios_M0203Ep(struct nvkm_bios *bios, int idx, u8 *ver, u8 *hdr,
86 	       struct nvbios_M0203E *info)
87 {
88 	u32 data = nvbios_M0203Ee(bios, idx, ver, hdr);
89 	memset(info, 0x00, sizeof(*info));
90 	switch (!!data * *ver) {
91 	case 0x10:
92 		info->type  = (nvbios_rd08(bios, data + 0x00) & 0x0f) >> 0;
93 		info->strap = (nvbios_rd08(bios, data + 0x00) & 0xf0) >> 4;
94 		info->group = (nvbios_rd08(bios, data + 0x01) & 0x0f) >> 0;
95 		return data;
96 	default:
97 		break;
98 	}
99 	return 0x00000000;
100 }
101 
102 u32
nvbios_M0203Em(struct nvkm_bios * bios,u8 ramcfg,u8 * ver,u8 * hdr,struct nvbios_M0203E * info)103 nvbios_M0203Em(struct nvkm_bios *bios, u8 ramcfg, u8 *ver, u8 *hdr,
104 	       struct nvbios_M0203E *info)
105 {
106 	struct nvkm_subdev *subdev = &bios->subdev;
107 	struct nvbios_M0203T M0203T;
108 	u8  cnt, len, idx = 0xff;
109 	u32 data;
110 
111 	if (!nvbios_M0203Tp(bios, ver, hdr, &cnt, &len, &M0203T)) {
112 		nvkm_warn(subdev, "M0203T not found\n");
113 		return 0x00000000;
114 	}
115 
116 	while ((data = nvbios_M0203Ep(bios, ++idx, ver, hdr, info))) {
117 		switch (M0203T.type) {
118 		case M0203T_TYPE_RAMCFG:
119 			if (info->strap != ramcfg)
120 				continue;
121 			return data;
122 		default:
123 			nvkm_warn(subdev, "M0203T type %02x\n", M0203T.type);
124 			return 0x00000000;
125 		}
126 	}
127 
128 	return data;
129 }
130