1/* $NetBSD: nouveau_subdev_bus_hwsq.c,v 1.1.1.1 2014/08/06 12:36:29 riastradh Exp $ */
2
3/*
4 * Copyright 2013 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 <bskeggs@redhat.com>
25 */
26
27#include <sys/cdefs.h>
28__KERNEL_RCSID(0, "$NetBSD: nouveau_subdev_bus_hwsq.c,v 1.1.1.1 2014/08/06 12:36:29 riastradh Exp $");
29
30#include <subdev/timer.h>
31#include <subdev/bus.h>
32
33struct nouveau_hwsq {
34 struct nouveau_bus *pbus;
35 u32 addr;
36 u32 data;
37 struct {
38 u8 data[512];
39 u8 size;
40 } c;
41};
42
43static void
44hwsq_cmd(struct nouveau_hwsq *hwsq, int size, u8 data[])
45{
46 memcpy(&hwsq->c.data[hwsq->c.size], data, size * sizeof(data[0]));
47 hwsq->c.size += size;
48}
49
50int
51nouveau_hwsq_init(struct nouveau_bus *pbus, struct nouveau_hwsq **phwsq)
52{
53 struct nouveau_hwsq *hwsq;
54
55 hwsq = *phwsq = kmalloc(sizeof(*hwsq), GFP_KERNEL);
56 if (hwsq) {
57 hwsq->pbus = pbus;
58 hwsq->addr = ~0;
59 hwsq->data = ~0;
60 memset(hwsq->c.data, 0x7f, sizeof(hwsq->c.data));
61 hwsq->c.size = 0;
62 }
63
64 return hwsq ? 0 : -ENOMEM;
65}
66
67int
68nouveau_hwsq_fini(struct nouveau_hwsq **phwsq, bool exec)
69{
70 struct nouveau_hwsq *hwsq = *phwsq;
71 int ret = 0, i;
72 if (hwsq) {
73 struct nouveau_bus *pbus = hwsq->pbus;
74 hwsq->c.size = (hwsq->c.size + 4) / 4;
75 if (hwsq->c.size <= pbus->hwsq_size) {
76 if (exec)
77 ret = pbus->hwsq_exec(pbus, (u32 *)hwsq->c.data,
78 hwsq->c.size);
79 if (ret)
80 nv_error(pbus, "hwsq exec failed: %d\n", ret);
81 } else {
82 nv_error(pbus, "hwsq ucode too large\n");
83 ret = -ENOSPC;
84 }
85
86 for (i = 0; ret && i < hwsq->c.size; i++)
87 nv_error(pbus, "\t0x%08x\n", ((u32 *)hwsq->c.data)[i]);
88
89 *phwsq = NULL;
90 kfree(hwsq);
91 }
92 return ret;
93}
94
95void
96nouveau_hwsq_wr32(struct nouveau_hwsq *hwsq, u32 addr, u32 data)
97{
98 nv_debug(hwsq->pbus, "R[%06x] = 0x%08x\n", addr, data);
99
100 if (hwsq->data != data) {
101 if ((data & 0xffff0000) != (hwsq->data & 0xffff0000)) {
102 hwsq_cmd(hwsq, 5, (u8[]){ 0xe2, data, data >> 8,
103 data >> 16, data >> 24 });
104 } else {
105 hwsq_cmd(hwsq, 3, (u8[]){ 0x42, data, data >> 8 });
106 }
107 }
108
109 if ((addr & 0xffff0000) != (hwsq->addr & 0xffff0000)) {
110 hwsq_cmd(hwsq, 5, (u8[]){ 0xe0, addr, addr >> 8,
111 addr >> 16, addr >> 24 });
112 } else {
113 hwsq_cmd(hwsq, 3, (u8[]){ 0x40, addr, addr >> 8 });
114 }
115
116 hwsq->addr = addr;
117 hwsq->data = data;
118}
119
120void
121nouveau_hwsq_setf(struct nouveau_hwsq *hwsq, u8 flag, int data)
122{
123 nv_debug(hwsq->pbus, " FLAG[%02x] = %d\n", flag, data);
124 flag += 0x80;
125 if (data >= 0)
126 flag += 0x20;
127 if (data >= 1)
128 flag += 0x20;
129 hwsq_cmd(hwsq, 1, (u8[]){ flag });
130}
131
132void
133nouveau_hwsq_wait(struct nouveau_hwsq *hwsq, u8 flag, u8 data)
134{
135 nv_debug(hwsq->pbus, " WAIT[%02x] = %d\n", flag, data);
136 hwsq_cmd(hwsq, 3, (u8[]){ 0x5f, flag, data });
137}
138
139void
140nouveau_hwsq_nsec(struct nouveau_hwsq *hwsq, u32 nsec)
141{
142 u8 shift = 0, usec = nsec / 1000;
143 while (usec & ~3) {
144 usec >>= 2;
145 shift++;
146 }
147
148 nv_debug(hwsq->pbus, " DELAY = %d ns\n", nsec);
149 hwsq_cmd(hwsq, 1, (u8[]){ 0x00 | (shift << 2) | usec });
150}
151