-
Notifications
You must be signed in to change notification settings - Fork 2
/
lsi6_lib.c
255 lines (209 loc) · 6.62 KB
/
lsi6_lib.c
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/errno.h>
#include <linux/pci.h>
#include <linux/init.h>
#include <linux/delay.h> //usleep_range
#ifndef LINUX_VERSION_CODE
#include <linux/version.h>
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,4,0)
#include <asm/system.h>
#endif
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/uaccess.h>
#include "k0607.h"
#include "lsi6.h"
#include "lsi6camac.h"
//#define DEBUG
#ifdef DEBUG
#define DP(x) x
#else
#define DP(x)
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,36)
// This function appears in 2.6.36
void usleep_range(unsigned long min, unsigned long max) {
cond_resched();
}
#endif
int lsi6_wait_channel(lsi6_dev_t *lsi, int chnum)
{
int chmask = 1 << chnum;
lsi6_regs_t *regs = (lsi6_regs_t *)lsi->base;
channel_regs_t *chan =
(channel_regs_t *)(lsi->base + (0x2000 << chnum));
int to = 0;
int status;
while((readl(®s->busy) & chmask) == 0) {
usleep_range(1, 5); // System becomes unresponsive if we read PCI permanently.
to++;
if (to == 100000) {
printk("lsi6: unexpected timeout !\n");
return -1;
}
}
DP(printk("Channel is ready after %d wait cycles\n", to));
status = readl(&chan->status);
if (status & LSI6_STATUS_LT) {
printk("lsi6: LT timeout !\n");
return -1;
}
return ((status & (LSI6_STATUS_LX | LSI6_STATUS_LQ)) >> 6);
}
int k0607_write_csr(lsi6_dev_t *lsi, int chnum, int CSR)
{
channel_regs_t *chan =
(channel_regs_t *)(lsi->base + (0x2000 << chnum));
writel(K0607_CSR_A << 1, &chan->addr);
writel(CSR, &chan->data_with_init);
return lsi6_wait_channel(lsi, chnum);
}
int k0607_read_csr(lsi6_dev_t *lsi, int chnum, int *CSR)
{
channel_regs_t *chan =
(channel_regs_t *)(lsi->base + (0x2000 << chnum));
int status;
writel(K0607_CSR_A << 1, &chan->addr);
readl(&chan->data_with_init);
status = lsi6_wait_channel(lsi, chnum);
if (status == -1) return -1;
*CSR = readl(&chan->data_wout_init);
return status;
}
int k0607_enable_lgroup(lsi6_dev_t *lsi, int chnum, int lgroup)
{
channel_regs_t *chan =
(channel_regs_t *)(lsi->base + (0x2000 << chnum));
int status, mask;
writel(K0607_LMR_A << 1, &chan->addr);
readl(&chan->data_with_init);
status = lsi6_wait_channel(lsi, chnum);
if (status == -1) return -1;
mask = readl(&chan->data_wout_init);
mask |= 1 << lgroup;
writel(mask, &chan->data_with_init);
return lsi6_wait_channel(lsi, chnum);
}
int k0607_read_lmr(lsi6_dev_t *lsi, int chnum, int *lmr)
{
channel_regs_t *chan =
(channel_regs_t *)(lsi->base + (0x2000 << chnum));
int status;
writel(K0607_LMR_A << 1, &chan->addr);
readl(&chan->data_with_init);
status = lsi6_wait_channel(lsi, chnum);
if (status == -1) return -1;
*lmr = readl(&chan->data_wout_init);
return status;
}
int k0607_write_lmr(lsi6_dev_t *lsi, int chnum, int lmr)
{
channel_regs_t *chan =
(channel_regs_t *)(lsi->base + (0x2000 << chnum));
writel(K0607_LMR_A << 1, &chan->addr);
writel(lmr, &chan->data_with_init);
return lsi6_wait_channel(lsi, chnum);
}
int k0607_write_hb(lsi6_dev_t *lsi, int chnum, int hb)
{
channel_regs_t *chan =
(channel_regs_t *)(lsi->base + (0x2000 << chnum));
writel(K0607_HB_A << 1, &chan->addr);
writel(hb & 0xff, &chan->data_with_init);
return lsi6_wait_channel(lsi, chnum);
}
int k0607_read_hb(lsi6_dev_t *lsi, int chnum, int *hb)
{
channel_regs_t *chan =
(channel_regs_t *)(lsi->base + (0x2000 << chnum));
int status;
writel(K0607_HB_A << 1, &chan->addr);
readl(&chan->data_with_init);
status = lsi6_wait_channel(lsi, chnum);
if (status == -1) return -1;
*hb = readl(&chan->data_wout_init) & 0xff;
return status;
}
int lsi6_do_naf(lsi6_dev_t *lsi, int chnum, int n, int a, int f, unsigned long *data)
{
channel_regs_t *chan =
(channel_regs_t *)(lsi->base + (0x2000 << chnum));
int status;
if (k0607_write_csr(lsi, chnum, lsi->CSR[chnum] | f) == -1) return -1;
writel(((n << 5) | (a << 1)), &chan->addr);
if (f < 8) readl(&chan->data_with_init);
else writel(*data, &chan->data_with_init);
status = lsi6_wait_channel(lsi, chnum);
if (status == -1) return -1;
if (f < 8) *data = readl(&chan->data_wout_init);
return status;
}
int lsi6_do_block(lsi6_dev_t *lsi, int chnum, int n, int a, int f, unsigned long *data, int *count)
{
channel_regs_t *chan =
(channel_regs_t *)(lsi->base + (0x2000 << chnum));
int status = 0, i;
if (k0607_write_csr(lsi, chnum, lsi->CSR[chnum] | f) == -1) return -1;
writel(((n << 5) | (a << 1)), &chan->addr);
for (i = 0; i < *count; i++) {
if (f < 8) readl(&chan->data_with_init);
else writel(data[i], &chan->data_with_init);
status = lsi6_wait_channel(lsi, chnum);
if ((status < 0) || (status & 0x02)) break;
if (f < 8) data[i] = readl(&chan->data_wout_init);
}
*count -= i;
return status;
}
int lsi6_do_naf24(lsi6_dev_t *lsi, int chnum, int n, int a, int f, unsigned long *data)
{
channel_regs_t *chan =
(channel_regs_t *)(lsi->base + (0x2000 << chnum));
int status, hb;
if (f >= 8) {
hb = (*data >> 16) & 0xff;
if (k0607_write_hb(lsi, chnum, hb) == -1) return -1;
}
if (k0607_write_csr(lsi, chnum, lsi->CSR[chnum] | f) == -1) return -1;
writel(((n << 5) | (a << 1)), &chan->addr);
if (f < 8) readl(&chan->data_with_init);
else writel(*data & 0xffff, &chan->data_with_init);
status = lsi6_wait_channel(lsi, chnum);
if (status == -1) return -1;
if (f < 8) {
unsigned long x;
x = readl(&chan->data_wout_init) & 0xffff;
if (k0607_read_hb(lsi, chnum, &hb) == -1) return -1;
*data = x | (hb << 16);
}
return status;
}
int lsi6_do_block24(lsi6_dev_t *lsi, int chnum, int n, int a, int f, unsigned long *data, int *count)
{
channel_regs_t *chan =
(channel_regs_t *)(lsi->base + (0x2000 << chnum));
int status = 0, hb, i;
if (k0607_write_csr(lsi, chnum, lsi->CSR[chnum] | f) == -1) return -1;
for (i = 0; i < *count; i++) {
if (f >= 8) {
hb = (data[i] >> 16) & 0xff;
if (k0607_write_hb(lsi, chnum, hb) == -1) break;
}
writel(((n << 5) | (a << 1)), &chan->addr);
if (f < 8) readl(&chan->data_with_init);
else writel(data[i] & 0xffff, &chan->data_with_init);
status = lsi6_wait_channel(lsi, chnum);
if ((status < 0) || (status & 0x02)) break;
if (f < 8) {
unsigned long x;
x = readl(&chan->data_wout_init) & 0xffff;
if (k0607_read_hb(lsi, chnum, &hb) == -1) return -1;
data[i] = x | (hb << 16);
}
}
*count -= i;
return status;
}