1/*	$NetBSD: nouveau_nvkm_subdev_ibus_gk20a.c,v 1.3 2021/12/18 23:45:40 riastradh Exp $	*/
2
3/*
4 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
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 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24#include <sys/cdefs.h>
25__KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_ibus_gk20a.c,v 1.3 2021/12/18 23:45:40 riastradh Exp $");
26
27#include <subdev/ibus.h>
28#include <subdev/timer.h>
29
30static void
31gk20a_ibus_init_ibus_ring(struct nvkm_subdev *ibus)
32{
33	struct nvkm_device *device = ibus->device;
34	nvkm_mask(device, 0x137250, 0x3f, 0);
35
36	nvkm_mask(device, 0x000200, 0x20, 0);
37	udelay(20);
38	nvkm_mask(device, 0x000200, 0x20, 0x20);
39
40	nvkm_wr32(device, 0x12004c, 0x4);
41	nvkm_wr32(device, 0x122204, 0x2);
42	nvkm_rd32(device, 0x122204);
43
44	/*
45	 * Bug: increase clock timeout to avoid operation failure at high
46	 * gpcclk rate.
47	 */
48	nvkm_wr32(device, 0x122354, 0x800);
49	nvkm_wr32(device, 0x128328, 0x800);
50	nvkm_wr32(device, 0x124320, 0x800);
51}
52
53static void
54gk20a_ibus_intr(struct nvkm_subdev *ibus)
55{
56	struct nvkm_device *device = ibus->device;
57	u32 status0 = nvkm_rd32(device, 0x120058);
58
59	if (status0 & 0x7) {
60		nvkm_debug(ibus, "resetting ibus ring\n");
61		gk20a_ibus_init_ibus_ring(ibus);
62	}
63
64	/* Acknowledge interrupt */
65	nvkm_mask(device, 0x12004c, 0x2, 0x2);
66	nvkm_msec(device, 2000,
67		if (!(nvkm_rd32(device, 0x12004c) & 0x0000003f))
68			break;
69	);
70}
71
72static int
73gk20a_ibus_init(struct nvkm_subdev *ibus)
74{
75	gk20a_ibus_init_ibus_ring(ibus);
76	return 0;
77}
78
79static const struct nvkm_subdev_func
80gk20a_ibus = {
81	.init = gk20a_ibus_init,
82	.intr = gk20a_ibus_intr,
83};
84
85int
86gk20a_ibus_new(struct nvkm_device *device, int index,
87	       struct nvkm_subdev **pibus)
88{
89	struct nvkm_subdev *ibus;
90	if (!(ibus = *pibus = kzalloc(sizeof(*ibus), GFP_KERNEL)))
91		return -ENOMEM;
92	nvkm_subdev_ctor(&gk20a_ibus, device, index, ibus);
93	return 0;
94}
95