-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpt3_i2c.c
271 lines (228 loc) · 5.22 KB
/
pt3_i2c.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*******************************************************************************
earthsoft PT3 Linux driver
This program is free software; you can redistribute it and/or modify it
under the terms and conditions of the GNU General Public License,
version 3, as published by the Free Software Foundation.
This program is distributed in the hope it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
The full GNU General Public License is included in this distribution in
the file called "COPYING".
*******************************************************************************/
#if defined(__FreeBSD__)
#include "pt3_misc.h"
#else
#include "version.h"
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/pci.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/version.h>
#include <linux/mutex.h>
#include <linux/sched.h>
#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>
#endif
#include "pt3_com.h"
#include "pt3_pci.h"
#include "pt3_i2c.h"
#define DATA_OFFSET 2048
static void
wait(PT3_I2C *i2c, __u32 *data)
{
__u32 val;
while (1) {
#if defined(__FreeBSD__)
struct ptx_softc *scp;
scp = i2c->scp;
val = bus_space_read_4(scp->bt, scp->bh, REGS_I2C_R);
#else
val = readl(i2c->bar[0] + REGS_I2C_R);
#endif
if (!BIT_SHIFT_MASK(val, 0, 1))
break;
schedule_timeout_interruptible(msecs_to_jiffies(1));
}
if (data != NULL)
*data = val;
}
static STATUS
run_code(PT3_I2C *i2c, __u32 start_addr, __u32 *ack)
{
__u32 data, a;
wait(i2c, &data);
if (unlikely(start_addr >= (1 << 13)))
PT3_PRINTK(0, KERN_DEBUG, "start address is over.\n");
#if defined(__FreeBSD__)
struct ptx_softc *scp;
scp = i2c->scp;
bus_space_write_4(scp->bt,scp->bh, REGS_I2C_W, 1 << 16 | start_addr);
#else
writel(1 << 16 | start_addr, i2c->bar[0] + REGS_I2C_W);
#endif
#if 0
PT3_PRINTK(7, KERN_DEBUG, "run i2c start_addr=0x%x\n", start_addr);
#endif
wait(i2c, &data);
a = BIT_SHIFT_MASK(data, 1, 2);
if (ack != NULL)
*ack = a;
if (a)
PT3_PRINTK(0, KERN_DEBUG, "fail i2c run_code status 0x%x\n", data);
return BIT_SHIFT_MASK(data, 1, 2) ? STATUS_I2C_ERROR : STATUS_OK;
}
void
pt3_i2c_copy(PT3_I2C *i2c, PT3_BUS *bus)
{
#if defined(__FreeBSD__)
uint8_t dst;
#else
void __iomem *dst;
#endif
__u8 *src;
__u32 i;
src = &bus->insts[0];
#if defined(__FreeBSD__)
struct ptx_softc *scp;
scp = i2c->scp;
dst = DATA_OFFSET + (bus->inst_addr / 2);
#else
dst = i2c->bar[1] + DATA_OFFSET + (bus->inst_addr / 2);
#endif
#if 0
PT3_PRINTK(7, KERN_DEBUG, "PT3 : i2c_copy. base=%p dst=%p src=%p size=%d\n",
i2c->bar[1], dst, src, bus->inst_pos);
#endif
#if 1
for (i = 0; i < bus->inst_pos; i++) {
#if defined(__FreeBSD__)
bus_space_write_1(scp->pt3_bt,scp->pt3_bh, dst + i , src[i]);
#else
writeb(src[i], dst + i);
#endif
}
#else
memcpy(dst, src, bus->inst_pos);
#endif
}
STATUS
pt3_i2c_run(PT3_I2C *i2c, PT3_BUS *bus, __u32 *ack, int copy)
{
STATUS status;
__u32 rsize, i;
#if defined(__FreeBSD__)
struct ptx_softc *scp;
scp = i2c->scp;
mtx_lock(&scp->lock);
while(scp->i2c_progress) {
msleep(&scp->i2c_progress, &scp->lock, 0|PCATCH, "ptxi2w", 0);
}
scp->i2c_progress = 1;
mtx_unlock(&scp->lock);
#else
mutex_lock(&i2c->lock);
#endif
if (copy) {
pt3_i2c_copy(i2c, bus);
}
status = run_code(i2c, bus->inst_addr, ack);
rsize = bus->read_addr;
for (i = 0; i < rsize; i++) {
#if defined(__FreeBSD__)
pt3_bus_push_read_data(bus, bus_space_read_1(scp->pt3_bt,scp->pt3_bh, DATA_OFFSET +
i));
#else
pt3_bus_push_read_data(bus, readb(i2c->bar[1] + DATA_OFFSET + i));
#endif
}
#if 0
if (rsize > 0) {
for (i = 1; i < 10; i++) {
PT3_PRINTK(7, KERN_DEBUG, "bus_read_data + %d = 0x%x inst = 0x%x\n",
i, readb(i2c->bar[1] + DATA_OFFSET + i),
bus->insts[i]);
}
}
#endif
#if defined(__FreeBSD__)
mtx_lock(&scp->lock);
scp->i2c_progress = 0;
wakeup(&scp->i2c_progress);
mtx_unlock(&scp->lock);
#else
mutex_unlock(&i2c->lock);
#endif
return status;
}
int
pt3_i2c_is_clean(PT3_I2C *i2c)
{
__u32 val;
#if defined(__FreeBSD__)
struct ptx_softc *scp;
scp = i2c->scp;
val = bus_space_read_4(scp->bt,scp->bh,REGS_I2C_R);
#else
val = readl(i2c->bar[0] + REGS_I2C_R);
#endif
return BIT_SHIFT_MASK(val, 3, 1);
}
void
pt3_i2c_reset(PT3_I2C *i2c)
{
#if defined(__FreeBSD__)
struct ptx_softc *scp;
scp = i2c->scp;
bus_space_write_4(scp->bt,scp->bh, REGS_I2C_W , 1<<17);
#else
writel(1 << 17, i2c->bar[0] + REGS_I2C_W);
#endif
}
#if defined(__FreeBSD__)
PT3_I2C *
create_pt3_i2c(void *p)
{
PT3_I2C *i2c;
struct ptx_softc *scp;
scp = (struct ptx_softc *)p;
i2c = pt3_vzalloc(sizeof(PT3_I2C));
if (i2c == NULL)
return NULL;
i2c->scp = scp;
return i2c;
}
void
free_pt3_i2c(PT3_I2C *i2c)
{
vfree(i2c);
}
#else
PT3_I2C *
create_pt3_i2c(__u8 __iomem *bar[])
{
PT3_I2C *i2c;
i2c = pt3_vzalloc(sizeof(PT3_I2C));
if (i2c == NULL)
goto fail;
mutex_init(&i2c->lock);
i2c->bar[0] = bar[0];
i2c->bar[1] = bar[1];
return i2c;
fail:
if (i2c != NULL)
free_pt3_i2c(i2c);
return NULL;
}
void
free_pt3_i2c(PT3_I2C *i2c)
{
vfree(i2c);
}
#endif