gm204.c 6.1 KB
Newer Older
B
Ben Skeggs 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * Copyright 2012 Red Hat Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: Ben Skeggs
 */
#include "nv50.h"

26 27
#define AUX_DBG(fmt, args...) nv_debug(i2c, "AUXCH(%d): " fmt, ch, ##args)
#define AUX_ERR(fmt, args...) nv_error(i2c, "AUXCH(%d): " fmt, ch, ##args)
B
Ben Skeggs 已提交
28 29

static void
30
auxch_fini(struct nvkm_i2c *i2c, int ch)
B
Ben Skeggs 已提交
31
{
32 33
	struct nvkm_device *device = i2c->subdev.device;
	nvkm_mask(device, 0x00d954 + (ch * 0x50), 0x00310000, 0x00000000);
B
Ben Skeggs 已提交
34 35 36
}

static int
37
auxch_init(struct nvkm_i2c *i2c, int ch)
B
Ben Skeggs 已提交
38
{
39
	struct nvkm_device *device = i2c->subdev.device;
B
Ben Skeggs 已提交
40 41 42 43 44 45 46 47
	const u32 unksel = 1; /* nfi which to use, or if it matters.. */
	const u32 ureq = unksel ? 0x00100000 : 0x00200000;
	const u32 urep = unksel ? 0x01000000 : 0x02000000;
	u32 ctrl, timeout;

	/* wait up to 1ms for any previous transaction to be done... */
	timeout = 1000;
	do {
48
		ctrl = nvkm_rd32(device, 0x00d954 + (ch * 0x50));
B
Ben Skeggs 已提交
49 50 51 52 53 54 55 56
		udelay(1);
		if (!timeout--) {
			AUX_ERR("begin idle timeout 0x%08x\n", ctrl);
			return -EBUSY;
		}
	} while (ctrl & 0x03010000);

	/* set some magic, and wait up to 1ms for it to appear */
57
	nvkm_mask(device, 0x00d954 + (ch * 0x50), 0x00300000, ureq);
B
Ben Skeggs 已提交
58 59
	timeout = 1000;
	do {
60
		ctrl = nvkm_rd32(device, 0x00d954 + (ch * 0x50));
B
Ben Skeggs 已提交
61 62 63
		udelay(1);
		if (!timeout--) {
			AUX_ERR("magic wait 0x%08x\n", ctrl);
64
			auxch_fini(i2c, ch);
B
Ben Skeggs 已提交
65 66 67 68 69 70 71 72
			return -EBUSY;
		}
	} while ((ctrl & 0x03000000) != urep);

	return 0;
}

int
73
gm204_aux(struct nvkm_i2c_port *base, bool retry,
B
Ben Skeggs 已提交
74 75
	 u8 type, u32 addr, u8 *data, u8 size)
{
76 77
	struct nvkm_i2c *i2c = nvkm_i2c(base);
	struct nvkm_device *device = i2c->subdev.device;
B
Ben Skeggs 已提交
78 79 80 81 82 83 84 85
	struct nv50_i2c_port *port = (void *)base;
	u32 ctrl, stat, timeout, retries;
	u32 xbuf[4] = {};
	int ch = port->addr;
	int ret, i;

	AUX_DBG("%d: 0x%08x %d\n", type, addr, size);

86
	ret = auxch_init(i2c, ch);
B
Ben Skeggs 已提交
87
	if (ret < 0)
B
Ben Skeggs 已提交
88 89
		goto out;

90
	stat = nvkm_rd32(device, 0x00d958 + (ch * 0x50));
B
Ben Skeggs 已提交
91 92 93 94 95 96 97 98 99 100
	if (!(stat & 0x10000000)) {
		AUX_DBG("sink not detected\n");
		ret = -ENXIO;
		goto out;
	}

	if (!(type & 1)) {
		memcpy(xbuf, data, size);
		for (i = 0; i < 16; i += 4) {
			AUX_DBG("wr 0x%08x\n", xbuf[i / 4]);
101
			nvkm_wr32(device, 0x00d930 + (ch * 0x50) + i, xbuf[i / 4]);
B
Ben Skeggs 已提交
102 103 104
		}
	}

105
	ctrl  = nvkm_rd32(device, 0x00d954 + (ch * 0x50));
B
Ben Skeggs 已提交
106 107 108
	ctrl &= ~0x0001f0ff;
	ctrl |= type << 12;
	ctrl |= size - 1;
109
	nvkm_wr32(device, 0x00d950 + (ch * 0x50), addr);
B
Ben Skeggs 已提交
110 111 112 113

	/* (maybe) retry transaction a number of times on failure... */
	for (retries = 0; !ret && retries < 32; retries++) {
		/* reset, and delay a while if this is a retry */
114 115
		nvkm_wr32(device, 0x00d954 + (ch * 0x50), 0x80000000 | ctrl);
		nvkm_wr32(device, 0x00d954 + (ch * 0x50), 0x00000000 | ctrl);
B
Ben Skeggs 已提交
116 117 118 119
		if (retries)
			udelay(400);

		/* transaction request, wait up to 1ms for it to complete */
120
		nvkm_wr32(device, 0x00d954 + (ch * 0x50), 0x00010000 | ctrl);
B
Ben Skeggs 已提交
121 122 123

		timeout = 1000;
		do {
124
			ctrl = nvkm_rd32(device, 0x00d954 + (ch * 0x50));
B
Ben Skeggs 已提交
125 126 127 128 129 130 131 132 133 134
			udelay(1);
			if (!timeout--) {
				AUX_ERR("tx req timeout 0x%08x\n", ctrl);
				ret = -EIO;
				goto out;
			}
		} while (ctrl & 0x00010000);
		ret = 1;

		/* read status, and check if transaction completed ok */
135
		stat = nvkm_mask(device, 0x00d958 + (ch * 0x50), 0, 0);
B
Ben Skeggs 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148
		if ((stat & 0x000f0000) == 0x00080000 ||
		    (stat & 0x000f0000) == 0x00020000)
			ret = retry ? 0 : 1;
		if ((stat & 0x00000100))
			ret = -ETIMEDOUT;
		if ((stat & 0x00000e00))
			ret = -EIO;

		AUX_DBG("%02d 0x%08x 0x%08x\n", retries, ctrl, stat);
	}

	if (type & 1) {
		for (i = 0; i < 16; i += 4) {
149
			xbuf[i / 4] = nvkm_rd32(device, 0x00d940 + (ch * 0x50) + i);
B
Ben Skeggs 已提交
150 151 152 153 154 155
			AUX_DBG("rd 0x%08x\n", xbuf[i / 4]);
		}
		memcpy(data, xbuf, size);
	}

out:
156
	auxch_fini(i2c, ch);
B
Ben Skeggs 已提交
157 158 159
	return ret < 0 ? ret : (stat & 0x000f0000) >> 16;
}

160
static const struct nvkm_i2c_func
B
Ben Skeggs 已提交
161 162 163 164 165
gm204_aux_func = {
	.aux       = gm204_aux,
};

int
166 167 168 169
gm204_aux_port_ctor(struct nvkm_object *parent,
		    struct nvkm_object *engine,
		    struct nvkm_oclass *oclass, void *data, u32 index,
		    struct nvkm_object **pobject)
B
Ben Skeggs 已提交
170 171 172 173 174
{
	struct dcb_i2c_entry *info = data;
	struct nv50_i2c_port *port;
	int ret;

175 176
	ret = nvkm_i2c_port_create(parent, engine, oclass, index,
				   &nvkm_i2c_aux_algo, &gm204_aux_func, &port);
B
Ben Skeggs 已提交
177 178 179 180 181 182 183 184 185
	*pobject = nv_object(port);
	if (ret)
		return ret;

	port->base.aux = info->auxch;
	port->addr = info->auxch;
	return 0;
}

186
struct nvkm_oclass
B
Ben Skeggs 已提交
187 188
gm204_i2c_sclass[] = {
	{ .handle = NV_I2C_TYPE_DCBI2C(DCB_I2C_NVIO_BIT),
189 190 191
	  .ofuncs = &(struct nvkm_ofuncs) {
		  .ctor = gf110_i2c_port_ctor,
		  .dtor = _nvkm_i2c_port_dtor,
B
Ben Skeggs 已提交
192
		  .init = nv50_i2c_port_init,
193
		  .fini = _nvkm_i2c_port_fini,
B
Ben Skeggs 已提交
194 195 196
	  },
	},
	{ .handle = NV_I2C_TYPE_DCBI2C(DCB_I2C_NVIO_AUX),
197
	  .ofuncs = &(struct nvkm_ofuncs) {
B
Ben Skeggs 已提交
198
		  .ctor = gm204_aux_port_ctor,
199 200 201
		  .dtor = _nvkm_i2c_port_dtor,
		  .init = _nvkm_i2c_port_init,
		  .fini = _nvkm_i2c_port_fini,
B
Ben Skeggs 已提交
202 203 204 205 206
	  },
	},
	{}
};

207 208
struct nvkm_oclass *
gm204_i2c_oclass = &(struct nvkm_i2c_impl) {
B
Ben Skeggs 已提交
209
	.base.handle = NV_SUBDEV(I2C, 0x24),
210 211 212 213 214
	.base.ofuncs = &(struct nvkm_ofuncs) {
		.ctor = _nvkm_i2c_ctor,
		.dtor = _nvkm_i2c_dtor,
		.init = _nvkm_i2c_init,
		.fini = _nvkm_i2c_fini,
B
Ben Skeggs 已提交
215 216 217 218 219
	},
	.sclass = gm204_i2c_sclass,
	.pad_x = &nv04_i2c_pad_oclass,
	.pad_s = &gm204_i2c_pad_oclass,
	.aux = 8,
220 221
	.aux_stat = gk104_aux_stat,
	.aux_mask = gk104_aux_mask,
B
Ben Skeggs 已提交
222
}.base;