/src/qemu/hw/net/can/can_sja1000.c
Line | Count | Source |
1 | | /* |
2 | | * CAN device - SJA1000 chip emulation for QEMU |
3 | | * |
4 | | * Copyright (c) 2013-2014 Jin Yang |
5 | | * Copyright (c) 2014-2018 Pavel Pisa |
6 | | * |
7 | | * Initial development supported by Google GSoC 2013 from RTEMS project slot |
8 | | * |
9 | | * Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | | * of this software and associated documentation files (the "Software"), to deal |
11 | | * in the Software without restriction, including without limitation the rights |
12 | | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 | | * copies of the Software, and to permit persons to whom the Software is |
14 | | * furnished to do so, subject to the following conditions: |
15 | | * |
16 | | * The above copyright notice and this permission notice shall be included in |
17 | | * all copies or substantial portions of the Software. |
18 | | * |
19 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
22 | | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 | | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25 | | * THE SOFTWARE. |
26 | | */ |
27 | | |
28 | | #include "qemu/osdep.h" |
29 | | #include "qemu/log.h" |
30 | | #include "qemu/bitops.h" |
31 | | #include "hw/core/irq.h" |
32 | | #include "migration/vmstate.h" |
33 | | #include "net/can_emu.h" |
34 | | |
35 | | #include "can_sja1000.h" |
36 | | |
37 | | #ifndef DEBUG_FILTER |
38 | 0 | #define DEBUG_FILTER 0 |
39 | | #endif /*DEBUG_FILTER*/ |
40 | | |
41 | | #ifndef DEBUG_CAN |
42 | 0 | #define DEBUG_CAN 0 |
43 | | #endif /*DEBUG_CAN*/ |
44 | | |
45 | | #define DPRINTF(fmt, ...) \ |
46 | 0 | do { \ |
47 | 0 | if (DEBUG_CAN) { \ |
48 | 0 | qemu_log("[cansja]: " fmt , ## __VA_ARGS__); \ |
49 | 0 | } \ |
50 | 0 | } while (0) |
51 | | |
52 | | static void can_sja_software_reset(CanSJA1000State *s) |
53 | 0 | { |
54 | 0 | s->mode &= ~0x31; |
55 | 0 | s->mode |= 0x01; |
56 | 0 | s->status_pel &= ~0x37; |
57 | 0 | s->status_pel |= 0x34; |
58 | |
|
59 | 0 | s->rxbuf_start = 0x00; |
60 | 0 | s->rxmsg_cnt = 0x00; |
61 | 0 | s->rx_cnt = 0x00; |
62 | 0 | } |
63 | | |
64 | | void can_sja_hardware_reset(CanSJA1000State *s) |
65 | 0 | { |
66 | | /* Reset by hardware, p10 */ |
67 | 0 | s->mode = 0x01; |
68 | 0 | s->status_pel = 0x3c; |
69 | 0 | s->interrupt_pel = 0x00; |
70 | 0 | s->clock = 0x00; |
71 | 0 | s->rxbuf_start = 0x00; |
72 | 0 | s->rxmsg_cnt = 0x00; |
73 | 0 | s->rx_cnt = 0x00; |
74 | |
|
75 | 0 | s->control = 0x01; |
76 | 0 | s->status_bas = 0x0c; |
77 | 0 | s->interrupt_bas = 0x00; |
78 | |
|
79 | 0 | qemu_irq_lower(s->irq); |
80 | 0 | } |
81 | | |
82 | | static |
83 | | void can_sja_single_filter(struct qemu_can_filter *filter, |
84 | | const uint8_t *acr, const uint8_t *amr, int extended) |
85 | 0 | { |
86 | 0 | if (extended) { |
87 | 0 | filter->can_id = (uint32_t)acr[0] << 21; |
88 | 0 | filter->can_id |= (uint32_t)acr[1] << 13; |
89 | 0 | filter->can_id |= (uint32_t)acr[2] << 5; |
90 | 0 | filter->can_id |= (uint32_t)acr[3] >> 3; |
91 | 0 | if (acr[3] & 4) { |
92 | 0 | filter->can_id |= QEMU_CAN_RTR_FLAG; |
93 | 0 | } |
94 | |
|
95 | 0 | filter->can_mask = (uint32_t)amr[0] << 21; |
96 | 0 | filter->can_mask |= (uint32_t)amr[1] << 13; |
97 | 0 | filter->can_mask |= (uint32_t)amr[2] << 5; |
98 | 0 | filter->can_mask |= (uint32_t)amr[3] >> 3; |
99 | 0 | filter->can_mask = ~filter->can_mask & QEMU_CAN_EFF_MASK; |
100 | 0 | if (!(amr[3] & 4)) { |
101 | 0 | filter->can_mask |= QEMU_CAN_RTR_FLAG; |
102 | 0 | } |
103 | 0 | } else { |
104 | 0 | filter->can_id = (uint32_t)acr[0] << 3; |
105 | 0 | filter->can_id |= (uint32_t)acr[1] >> 5; |
106 | 0 | if (acr[1] & 0x10) { |
107 | 0 | filter->can_id |= QEMU_CAN_RTR_FLAG; |
108 | 0 | } |
109 | |
|
110 | 0 | filter->can_mask = (uint32_t)amr[0] << 3; |
111 | 0 | filter->can_mask |= (uint32_t)amr[1] >> 5; |
112 | 0 | filter->can_mask = ~filter->can_mask & QEMU_CAN_SFF_MASK; |
113 | 0 | if (!(amr[1] & 0x10)) { |
114 | 0 | filter->can_mask |= QEMU_CAN_RTR_FLAG; |
115 | 0 | } |
116 | 0 | } |
117 | 0 | } |
118 | | |
119 | | static |
120 | | void can_sja_dual_filter(struct qemu_can_filter *filter, |
121 | | const uint8_t *acr, const uint8_t *amr, int extended) |
122 | 0 | { |
123 | 0 | if (extended) { |
124 | 0 | filter->can_id = (uint32_t)acr[0] << 21; |
125 | 0 | filter->can_id |= (uint32_t)acr[1] << 13; |
126 | |
|
127 | 0 | filter->can_mask = (uint32_t)amr[0] << 21; |
128 | 0 | filter->can_mask |= (uint32_t)amr[1] << 13; |
129 | 0 | filter->can_mask = ~filter->can_mask & QEMU_CAN_EFF_MASK & ~0x1fff; |
130 | 0 | } else { |
131 | 0 | filter->can_id = (uint32_t)acr[0] << 3; |
132 | 0 | filter->can_id |= (uint32_t)acr[1] >> 5; |
133 | 0 | if (acr[1] & 0x10) { |
134 | 0 | filter->can_id |= QEMU_CAN_RTR_FLAG; |
135 | 0 | } |
136 | |
|
137 | 0 | filter->can_mask = (uint32_t)amr[0] << 3; |
138 | 0 | filter->can_mask |= (uint32_t)amr[1] >> 5; |
139 | 0 | filter->can_mask = ~filter->can_mask & QEMU_CAN_SFF_MASK; |
140 | 0 | if (!(amr[1] & 0x10)) { |
141 | 0 | filter->can_mask |= QEMU_CAN_RTR_FLAG; |
142 | 0 | } |
143 | 0 | } |
144 | 0 | } |
145 | | |
146 | | /* Details in DS-p22, what we need to do here is to test the data. */ |
147 | | static |
148 | | int can_sja_accept_filter(CanSJA1000State *s, |
149 | | const qemu_can_frame *frame) |
150 | 0 | { |
151 | |
|
152 | 0 | struct qemu_can_filter filter; |
153 | |
|
154 | 0 | if (s->clock & 0x80) { /* PeliCAN Mode */ |
155 | 0 | if (s->mode & (1 << 3)) { /* Single mode. */ |
156 | 0 | if (frame->can_id & QEMU_CAN_EFF_FLAG) { /* EFF */ |
157 | 0 | can_sja_single_filter(&filter, |
158 | 0 | s->code_mask + 0, s->code_mask + 4, 1); |
159 | |
|
160 | 0 | if (!can_bus_filter_match(&filter, frame->can_id)) { |
161 | 0 | return 0; |
162 | 0 | } |
163 | 0 | } else { /* SFF */ |
164 | 0 | can_sja_single_filter(&filter, |
165 | 0 | s->code_mask + 0, s->code_mask + 4, 0); |
166 | |
|
167 | 0 | if (!can_bus_filter_match(&filter, frame->can_id)) { |
168 | 0 | return 0; |
169 | 0 | } |
170 | | |
171 | 0 | if (frame->can_id & QEMU_CAN_RTR_FLAG) { /* RTR */ |
172 | 0 | return 1; |
173 | 0 | } |
174 | | |
175 | 0 | if (frame->can_dlc == 0) { |
176 | 0 | return 1; |
177 | 0 | } |
178 | | |
179 | 0 | if ((frame->data[0] & ~(s->code_mask[6])) != |
180 | 0 | (s->code_mask[2] & ~(s->code_mask[6]))) { |
181 | 0 | return 0; |
182 | 0 | } |
183 | | |
184 | 0 | if (frame->can_dlc < 2) { |
185 | 0 | return 1; |
186 | 0 | } |
187 | | |
188 | 0 | if ((frame->data[1] & ~(s->code_mask[7])) == |
189 | 0 | (s->code_mask[3] & ~(s->code_mask[7]))) { |
190 | 0 | return 1; |
191 | 0 | } |
192 | | |
193 | 0 | return 0; |
194 | 0 | } |
195 | 0 | } else { /* Dual mode */ |
196 | 0 | if (frame->can_id & QEMU_CAN_EFF_FLAG) { /* EFF */ |
197 | 0 | can_sja_dual_filter(&filter, |
198 | 0 | s->code_mask + 0, s->code_mask + 4, 1); |
199 | |
|
200 | 0 | if (can_bus_filter_match(&filter, frame->can_id)) { |
201 | 0 | return 1; |
202 | 0 | } |
203 | | |
204 | 0 | can_sja_dual_filter(&filter, |
205 | 0 | s->code_mask + 2, s->code_mask + 6, 1); |
206 | |
|
207 | 0 | if (can_bus_filter_match(&filter, frame->can_id)) { |
208 | 0 | return 1; |
209 | 0 | } |
210 | | |
211 | 0 | return 0; |
212 | 0 | } else { |
213 | 0 | can_sja_dual_filter(&filter, |
214 | 0 | s->code_mask + 0, s->code_mask + 4, 0); |
215 | |
|
216 | 0 | if (can_bus_filter_match(&filter, frame->can_id)) { |
217 | 0 | uint8_t expect; |
218 | 0 | uint8_t mask; |
219 | 0 | expect = s->code_mask[1] << 4; |
220 | 0 | expect |= s->code_mask[3] & 0x0f; |
221 | |
|
222 | 0 | mask = s->code_mask[5] << 4; |
223 | 0 | mask |= s->code_mask[7] & 0x0f; |
224 | 0 | mask = ~mask & 0xff; |
225 | |
|
226 | 0 | if ((frame->data[0] & mask) == |
227 | 0 | (expect & mask)) { |
228 | 0 | return 1; |
229 | 0 | } |
230 | 0 | } |
231 | | |
232 | 0 | can_sja_dual_filter(&filter, |
233 | 0 | s->code_mask + 2, s->code_mask + 6, 0); |
234 | |
|
235 | 0 | if (can_bus_filter_match(&filter, frame->can_id)) { |
236 | 0 | return 1; |
237 | 0 | } |
238 | | |
239 | 0 | return 0; |
240 | 0 | } |
241 | 0 | } |
242 | 0 | } |
243 | | |
244 | 0 | return 1; |
245 | 0 | } |
246 | | |
247 | | static void can_display_msg(const char *prefix, const qemu_can_frame *msg) |
248 | 0 | { |
249 | 0 | int i; |
250 | 0 | FILE *logfile = qemu_log_trylock(); |
251 | 0 |
|
252 | 0 | if (logfile) { |
253 | 0 | fprintf(logfile, "%s%03X [%01d] %s %s", |
254 | 0 | prefix, |
255 | 0 | msg->can_id & QEMU_CAN_EFF_MASK, |
256 | 0 | msg->can_dlc, |
257 | 0 | msg->can_id & QEMU_CAN_EFF_FLAG ? "EFF" : "SFF", |
258 | 0 | msg->can_id & QEMU_CAN_RTR_FLAG ? "RTR" : "DAT"); |
259 | 0 |
|
260 | 0 | for (i = 0; i < msg->can_dlc; i++) { |
261 | 0 | fprintf(logfile, " %02X", msg->data[i]); |
262 | 0 | } |
263 | 0 | fprintf(logfile, "\n"); |
264 | 0 | qemu_log_unlock(logfile); |
265 | 0 | } |
266 | 0 | } |
267 | | |
268 | | static void buff2frame_pel(const uint8_t *buff, qemu_can_frame *frame) |
269 | 0 | { |
270 | 0 | uint8_t i; |
271 | |
|
272 | 0 | frame->flags = 0; |
273 | 0 | frame->can_id = 0; |
274 | 0 | if (buff[0] & 0x40) { /* RTR */ |
275 | 0 | frame->can_id = QEMU_CAN_RTR_FLAG; |
276 | 0 | } |
277 | 0 | frame->can_dlc = buff[0] & 0x0f; |
278 | |
|
279 | 0 | if (frame->can_dlc > 8) { |
280 | 0 | frame->can_dlc = 8; |
281 | 0 | } |
282 | |
|
283 | 0 | if (buff[0] & 0x80) { /* Extended */ |
284 | 0 | frame->can_id |= QEMU_CAN_EFF_FLAG; |
285 | 0 | frame->can_id |= buff[1] << 21; /* ID.28~ID.21 */ |
286 | 0 | frame->can_id |= buff[2] << 13; /* ID.20~ID.13 */ |
287 | 0 | frame->can_id |= buff[3] << 5; |
288 | 0 | frame->can_id |= buff[4] >> 3; |
289 | 0 | for (i = 0; i < frame->can_dlc; i++) { |
290 | 0 | frame->data[i] = buff[5 + i]; |
291 | 0 | } |
292 | 0 | for (; i < 8; i++) { |
293 | 0 | frame->data[i] = 0; |
294 | 0 | } |
295 | 0 | } else { |
296 | 0 | frame->can_id |= buff[1] << 3; |
297 | 0 | frame->can_id |= buff[2] >> 5; |
298 | 0 | for (i = 0; i < frame->can_dlc; i++) { |
299 | 0 | frame->data[i] = buff[3 + i]; |
300 | 0 | } |
301 | 0 | for (; i < 8; i++) { |
302 | 0 | frame->data[i] = 0; |
303 | 0 | } |
304 | 0 | } |
305 | 0 | } |
306 | | |
307 | | |
308 | | static void buff2frame_bas(const uint8_t *buff, qemu_can_frame *frame) |
309 | 0 | { |
310 | 0 | uint8_t i; |
311 | |
|
312 | 0 | frame->flags = 0; |
313 | 0 | frame->can_id = ((buff[0] << 3) & (0xff << 3)) + ((buff[1] >> 5) & 0x07); |
314 | 0 | if (buff[1] & 0x10) { /* RTR */ |
315 | 0 | frame->can_id = QEMU_CAN_RTR_FLAG; |
316 | 0 | } |
317 | 0 | frame->can_dlc = buff[1] & 0x0f; |
318 | |
|
319 | 0 | if (frame->can_dlc > 8) { |
320 | 0 | frame->can_dlc = 8; |
321 | 0 | } |
322 | |
|
323 | 0 | for (i = 0; i < frame->can_dlc; i++) { |
324 | 0 | frame->data[i] = buff[2 + i]; |
325 | 0 | } |
326 | 0 | for (; i < 8; i++) { |
327 | 0 | frame->data[i] = 0; |
328 | 0 | } |
329 | 0 | } |
330 | | |
331 | | |
332 | | static int frame2buff_pel(const qemu_can_frame *frame, uint8_t *buff) |
333 | 0 | { |
334 | 0 | int i; |
335 | 0 | int dlen = frame->can_dlc; |
336 | |
|
337 | 0 | if (frame->can_id & QEMU_CAN_ERR_FLAG) { /* error frame, NOT support now. */ |
338 | 0 | return -1; |
339 | 0 | } |
340 | | |
341 | 0 | if (dlen > 8) { |
342 | 0 | return -1; |
343 | 0 | } |
344 | | |
345 | 0 | buff[0] = 0x0f & frame->can_dlc; /* DLC */ |
346 | 0 | if (frame->can_id & QEMU_CAN_RTR_FLAG) { /* RTR */ |
347 | 0 | buff[0] |= (1 << 6); |
348 | 0 | } |
349 | 0 | if (frame->can_id & QEMU_CAN_EFF_FLAG) { /* EFF */ |
350 | 0 | buff[0] |= (1 << 7); |
351 | 0 | buff[1] = extract32(frame->can_id, 21, 8); /* ID.28~ID.21 */ |
352 | 0 | buff[2] = extract32(frame->can_id, 13, 8); /* ID.20~ID.13 */ |
353 | 0 | buff[3] = extract32(frame->can_id, 5, 8); /* ID.12~ID.05 */ |
354 | 0 | buff[4] = extract32(frame->can_id, 0, 5) << 3; /* ID.04~ID.00,xxx */ |
355 | 0 | for (i = 0; i < dlen; i++) { |
356 | 0 | buff[5 + i] = frame->data[i]; |
357 | 0 | } |
358 | 0 | return dlen + 5; |
359 | 0 | } else { /* SFF */ |
360 | 0 | buff[1] = extract32(frame->can_id, 3, 8); /* ID.10~ID.03 */ |
361 | 0 | buff[2] = extract32(frame->can_id, 0, 3) << 5; /* ID.02~ID.00,xxxxx */ |
362 | 0 | for (i = 0; i < dlen; i++) { |
363 | 0 | buff[3 + i] = frame->data[i]; |
364 | 0 | } |
365 | |
|
366 | 0 | return dlen + 3; |
367 | 0 | } |
368 | | |
369 | 0 | return -1; |
370 | 0 | } |
371 | | |
372 | | static int frame2buff_bas(const qemu_can_frame *frame, uint8_t *buff) |
373 | 0 | { |
374 | 0 | int i; |
375 | 0 | int dlen = frame->can_dlc; |
376 | | |
377 | | /* |
378 | | * EFF, no support for BasicMode |
379 | | * No use for Error frames now, |
380 | | * they could be used in future to update SJA1000 error state |
381 | | */ |
382 | 0 | if ((frame->can_id & QEMU_CAN_EFF_FLAG) || |
383 | 0 | (frame->can_id & QEMU_CAN_ERR_FLAG)) { |
384 | 0 | return -1; |
385 | 0 | } |
386 | | |
387 | 0 | if (dlen > 8) { |
388 | 0 | return -1; |
389 | 0 | } |
390 | | |
391 | 0 | buff[0] = extract32(frame->can_id, 3, 8); /* ID.10~ID.03 */ |
392 | 0 | buff[1] = extract32(frame->can_id, 0, 3) << 5; /* ID.02~ID.00,xxxxx */ |
393 | 0 | if (frame->can_id & QEMU_CAN_RTR_FLAG) { /* RTR */ |
394 | 0 | buff[1] |= (1 << 4); |
395 | 0 | } |
396 | 0 | buff[1] |= frame->can_dlc & 0x0f; |
397 | 0 | for (i = 0; i < dlen; i++) { |
398 | 0 | buff[2 + i] = frame->data[i]; |
399 | 0 | } |
400 | |
|
401 | 0 | return dlen + 2; |
402 | 0 | } |
403 | | |
404 | | static void can_sja_update_pel_irq(CanSJA1000State *s) |
405 | 0 | { |
406 | 0 | if (s->interrupt_en & s->interrupt_pel) { |
407 | 0 | qemu_irq_raise(s->irq); |
408 | 0 | } else { |
409 | 0 | qemu_irq_lower(s->irq); |
410 | 0 | } |
411 | 0 | } |
412 | | |
413 | | static void can_sja_update_bas_irq(CanSJA1000State *s) |
414 | 0 | { |
415 | 0 | if ((s->control >> 1) & s->interrupt_bas) { |
416 | 0 | qemu_irq_raise(s->irq); |
417 | 0 | } else { |
418 | 0 | qemu_irq_lower(s->irq); |
419 | 0 | } |
420 | 0 | } |
421 | | |
422 | | void can_sja_mem_write(CanSJA1000State *s, hwaddr addr, uint64_t val, |
423 | | unsigned size) |
424 | 0 | { |
425 | 0 | qemu_can_frame frame; |
426 | 0 | uint32_t tmp; |
427 | 0 | uint8_t tmp8, count; |
428 | | |
429 | |
|
430 | 0 | DPRINTF("write 0x%02llx addr 0x%02x\n", |
431 | 0 | (unsigned long long)val, (unsigned int)addr); |
432 | |
|
433 | 0 | if (addr > CAN_SJA_MEM_SIZE) { |
434 | 0 | return; |
435 | 0 | } |
436 | | |
437 | 0 | if (s->clock & 0x80) { /* PeliCAN Mode */ |
438 | 0 | switch (addr) { |
439 | 0 | case SJA_MOD: /* Mode register */ |
440 | 0 | s->mode = 0x1f & val; |
441 | 0 | if ((s->mode & 0x01) && ((val & 0x01) == 0)) { |
442 | | /* Go to operation mode from reset mode. */ |
443 | 0 | if (s->mode & (1 << 3)) { /* Single mode. */ |
444 | | /* For EFF */ |
445 | 0 | can_sja_single_filter(&s->filter[0], |
446 | 0 | s->code_mask + 0, s->code_mask + 4, 1); |
447 | | |
448 | | /* For SFF */ |
449 | 0 | can_sja_single_filter(&s->filter[1], |
450 | 0 | s->code_mask + 0, s->code_mask + 4, 0); |
451 | |
|
452 | 0 | can_bus_client_set_filters(&s->bus_client, s->filter, 2); |
453 | 0 | } else { /* Dual mode */ |
454 | | /* For EFF */ |
455 | 0 | can_sja_dual_filter(&s->filter[0], |
456 | 0 | s->code_mask + 0, s->code_mask + 4, 1); |
457 | |
|
458 | 0 | can_sja_dual_filter(&s->filter[1], |
459 | 0 | s->code_mask + 2, s->code_mask + 6, 1); |
460 | | |
461 | | /* For SFF */ |
462 | 0 | can_sja_dual_filter(&s->filter[2], |
463 | 0 | s->code_mask + 0, s->code_mask + 4, 0); |
464 | |
|
465 | 0 | can_sja_dual_filter(&s->filter[3], |
466 | 0 | s->code_mask + 2, s->code_mask + 6, 0); |
467 | |
|
468 | 0 | can_bus_client_set_filters(&s->bus_client, s->filter, 4); |
469 | 0 | } |
470 | |
|
471 | 0 | s->rxmsg_cnt = 0; |
472 | 0 | s->rx_cnt = 0; |
473 | 0 | } |
474 | 0 | break; |
475 | | |
476 | 0 | case SJA_CMR: /* Command register. */ |
477 | 0 | if (0x01 & val) { /* Send transmission request. */ |
478 | 0 | buff2frame_pel(s->tx_buff, &frame); |
479 | 0 | if (DEBUG_FILTER) { |
480 | 0 | can_display_msg("[cansja]: Tx request " , &frame); |
481 | 0 | } |
482 | | |
483 | | /* |
484 | | * Clear transmission complete status, |
485 | | * and Transmit Buffer Status. |
486 | | * write to the backends. |
487 | | */ |
488 | 0 | s->status_pel &= ~(3 << 2); |
489 | |
|
490 | 0 | can_bus_client_send(&s->bus_client, &frame, 1); |
491 | | |
492 | | /* |
493 | | * Set transmission complete status |
494 | | * and Transmit Buffer Status. |
495 | | */ |
496 | 0 | s->status_pel |= (3 << 2); |
497 | | |
498 | | /* Clear transmit status. */ |
499 | 0 | s->status_pel &= ~(1 << 5); |
500 | 0 | s->interrupt_pel |= 0x02; |
501 | 0 | can_sja_update_pel_irq(s); |
502 | 0 | } |
503 | 0 | if (0x04 & val) { /* Release Receive Buffer */ |
504 | 0 | if (s->rxmsg_cnt <= 0) { |
505 | 0 | break; |
506 | 0 | } |
507 | | |
508 | 0 | tmp8 = s->rx_buff[s->rxbuf_start]; count = 0; |
509 | 0 | if (tmp8 & (1 << 7)) { /* EFF */ |
510 | 0 | count += 2; |
511 | 0 | } |
512 | 0 | count += 3; |
513 | 0 | if (!(tmp8 & (1 << 6))) { /* DATA */ |
514 | 0 | count += (tmp8 & 0x0f); |
515 | 0 | } |
516 | |
|
517 | 0 | if (DEBUG_FILTER) { |
518 | 0 | qemu_log("[cansja]: message released from " |
519 | 0 | "Rx FIFO cnt=%d, count=%d\n", s->rx_cnt, count); |
520 | 0 | } |
521 | |
|
522 | 0 | s->rxbuf_start += count; |
523 | 0 | s->rxbuf_start %= SJA_RCV_BUF_LEN; |
524 | |
|
525 | 0 | s->rx_cnt -= count; |
526 | 0 | s->rxmsg_cnt--; |
527 | 0 | if (s->rxmsg_cnt == 0) { |
528 | 0 | s->status_pel &= ~(1 << 0); |
529 | 0 | s->interrupt_pel &= ~(1 << 0); |
530 | 0 | can_sja_update_pel_irq(s); |
531 | 0 | } |
532 | 0 | } |
533 | 0 | if (0x08 & val) { /* Clear data overrun */ |
534 | 0 | s->status_pel &= ~(1 << 1); |
535 | 0 | s->interrupt_pel &= ~(1 << 3); |
536 | 0 | can_sja_update_pel_irq(s); |
537 | 0 | } |
538 | 0 | break; |
539 | 0 | case SJA_SR: /* Status register */ |
540 | 0 | case SJA_IR: /* Interrupt register */ |
541 | 0 | break; /* Do nothing */ |
542 | 0 | case SJA_IER: /* Interrupt enable register */ |
543 | 0 | s->interrupt_en = val; |
544 | 0 | break; |
545 | 0 | case 16: /* RX frame information addr16-28. */ |
546 | 0 | s->status_pel |= (1 << 5); /* Set transmit status. */ |
547 | | /* fallthrough */ |
548 | 0 | case 17 ... 28: |
549 | 0 | if (s->mode & 0x01) { /* Reset mode */ |
550 | 0 | if (addr < 24) { |
551 | 0 | s->code_mask[addr - 16] = val; |
552 | 0 | } |
553 | 0 | } else { /* Operation mode */ |
554 | 0 | s->tx_buff[addr - 16] = val; /* Store to TX buffer directly. */ |
555 | 0 | } |
556 | 0 | break; |
557 | 0 | case SJA_CDR: |
558 | 0 | s->clock = val; |
559 | 0 | break; |
560 | 0 | } |
561 | 0 | } else { /* Basic Mode */ |
562 | 0 | switch (addr) { |
563 | 0 | case SJA_BCAN_CTR: /* Control register, addr 0 */ |
564 | 0 | if ((s->control & 0x01) && ((val & 0x01) == 0)) { |
565 | | /* Go to operation mode from reset mode. */ |
566 | 0 | s->filter[0].can_id = (s->code << 3) & (0xff << 3); |
567 | 0 | tmp = (~(s->mask << 3)) & (0xff << 3); |
568 | 0 | tmp |= QEMU_CAN_EFF_FLAG; /* Only Basic CAN Frame. */ |
569 | 0 | s->filter[0].can_mask = tmp; |
570 | 0 | can_bus_client_set_filters(&s->bus_client, s->filter, 1); |
571 | |
|
572 | 0 | s->rxmsg_cnt = 0; |
573 | 0 | s->rx_cnt = 0; |
574 | 0 | } else if (!(s->control & 0x01) && !(val & 0x01)) { |
575 | 0 | can_sja_software_reset(s); |
576 | 0 | } |
577 | |
|
578 | 0 | s->control = 0x1f & val; |
579 | 0 | break; |
580 | 0 | case SJA_BCAN_CMR: /* Command register, addr 1 */ |
581 | 0 | if (0x01 & val) { /* Send transmission request. */ |
582 | 0 | buff2frame_bas(s->tx_buff, &frame); |
583 | 0 | if (DEBUG_FILTER) { |
584 | 0 | can_display_msg("[cansja]: Tx request " , &frame); |
585 | 0 | } |
586 | | |
587 | | /* |
588 | | * Clear transmission complete status, |
589 | | * and Transmit Buffer Status. |
590 | | */ |
591 | 0 | s->status_bas &= ~(3 << 2); |
592 | | |
593 | | /* write to the backends. */ |
594 | 0 | can_bus_client_send(&s->bus_client, &frame, 1); |
595 | | |
596 | | /* |
597 | | * Set transmission complete status, |
598 | | * and Transmit Buffer Status. |
599 | | */ |
600 | 0 | s->status_bas |= (3 << 2); |
601 | | |
602 | | /* Clear transmit status. */ |
603 | 0 | s->status_bas &= ~(1 << 5); |
604 | 0 | s->interrupt_bas |= 0x02; |
605 | 0 | can_sja_update_bas_irq(s); |
606 | 0 | } |
607 | 0 | if (0x04 & val) { /* Release Receive Buffer */ |
608 | 0 | if (s->rxmsg_cnt <= 0) { |
609 | 0 | break; |
610 | 0 | } |
611 | | |
612 | 0 | tmp8 = s->rx_buff[(s->rxbuf_start + 1) % SJA_RCV_BUF_LEN]; |
613 | 0 | count = 2 + (tmp8 & 0x0f); |
614 | |
|
615 | 0 | if (DEBUG_FILTER) { |
616 | 0 | qemu_log("[cansja]: message released from " |
617 | 0 | "Rx FIFO cnt=%d, count=%d\n", s->rx_cnt, count); |
618 | 0 | } |
619 | |
|
620 | 0 | s->rxbuf_start += count; |
621 | 0 | s->rxbuf_start %= SJA_RCV_BUF_LEN; |
622 | 0 | s->rx_cnt -= count; |
623 | 0 | s->rxmsg_cnt--; |
624 | |
|
625 | 0 | if (s->rxmsg_cnt == 0) { |
626 | 0 | s->status_bas &= ~(1 << 0); |
627 | 0 | s->interrupt_bas &= ~(1 << 0); |
628 | 0 | can_sja_update_bas_irq(s); |
629 | 0 | } |
630 | 0 | } |
631 | 0 | if (0x08 & val) { /* Clear data overrun */ |
632 | 0 | s->status_bas &= ~(1 << 1); |
633 | 0 | s->interrupt_bas &= ~(1 << 3); |
634 | 0 | can_sja_update_bas_irq(s); |
635 | 0 | } |
636 | 0 | break; |
637 | 0 | case 4: |
638 | 0 | s->code = val; |
639 | 0 | break; |
640 | 0 | case 5: |
641 | 0 | s->mask = val; |
642 | 0 | break; |
643 | 0 | case 10: |
644 | 0 | s->status_bas |= (1 << 5); /* Set transmit status. */ |
645 | | /* fallthrough */ |
646 | 0 | case 11 ... 19: |
647 | 0 | if ((s->control & 0x01) == 0) { /* Operation mode */ |
648 | 0 | s->tx_buff[addr - 10] = val; /* Store to TX buffer directly. */ |
649 | 0 | } |
650 | 0 | break; |
651 | 0 | case SJA_CDR: |
652 | 0 | s->clock = val; |
653 | 0 | break; |
654 | 0 | } |
655 | 0 | } |
656 | 0 | } |
657 | | |
658 | | uint64_t can_sja_mem_read(CanSJA1000State *s, hwaddr addr, unsigned size) |
659 | 0 | { |
660 | 0 | uint64_t temp = 0; |
661 | |
|
662 | 0 | DPRINTF("read addr 0x%02x ...\n", (unsigned int)addr); |
663 | |
|
664 | 0 | if (addr > CAN_SJA_MEM_SIZE) { |
665 | 0 | return 0; |
666 | 0 | } |
667 | | |
668 | 0 | if (s->clock & 0x80) { /* PeliCAN Mode */ |
669 | 0 | switch (addr) { |
670 | 0 | case SJA_MOD: /* Mode register, addr 0 */ |
671 | 0 | temp = s->mode; |
672 | 0 | break; |
673 | 0 | case SJA_CMR: /* Command register, addr 1 */ |
674 | 0 | temp = 0x00; /* Command register, cannot be read. */ |
675 | 0 | break; |
676 | 0 | case SJA_SR: /* Status register, addr 2 */ |
677 | 0 | temp = s->status_pel; |
678 | 0 | break; |
679 | 0 | case SJA_IR: /* Interrupt register, addr 3 */ |
680 | 0 | temp = s->interrupt_pel; |
681 | 0 | s->interrupt_pel = 0; |
682 | 0 | if (s->rxmsg_cnt) { |
683 | 0 | s->interrupt_pel |= (1 << 0); /* Receive interrupt. */ |
684 | 0 | } |
685 | 0 | can_sja_update_pel_irq(s); |
686 | 0 | break; |
687 | 0 | case SJA_IER: /* Interrupt enable register, addr 4 */ |
688 | 0 | temp = s->interrupt_en; |
689 | 0 | break; |
690 | 0 | case 5: /* Reserved */ |
691 | 0 | case 6: /* Bus timing 0, hardware related, not support now. */ |
692 | 0 | case 7: /* Bus timing 1, hardware related, not support now. */ |
693 | 0 | case 8: /* |
694 | | * Output control register, hardware related, |
695 | | * not supported for now. |
696 | | */ |
697 | 0 | case 9: /* Test. */ |
698 | 0 | case 10 ... 15: /* Reserved */ |
699 | 0 | temp = 0x00; |
700 | 0 | break; |
701 | | |
702 | 0 | case 16 ... 28: |
703 | 0 | if (s->mode & 0x01) { /* Reset mode */ |
704 | 0 | if (addr < 24) { |
705 | 0 | temp = s->code_mask[addr - 16]; |
706 | 0 | } else { |
707 | 0 | temp = 0x00; |
708 | 0 | } |
709 | 0 | } else { /* Operation mode */ |
710 | 0 | temp = s->rx_buff[(s->rxbuf_start + addr - 16) % |
711 | 0 | SJA_RCV_BUF_LEN]; |
712 | 0 | } |
713 | 0 | break; |
714 | 0 | case SJA_CDR: |
715 | 0 | temp = s->clock; |
716 | 0 | break; |
717 | 0 | default: |
718 | 0 | temp = 0xff; |
719 | 0 | } |
720 | 0 | } else { /* Basic Mode */ |
721 | 0 | switch (addr) { |
722 | 0 | case SJA_BCAN_CTR: /* Control register, addr 0 */ |
723 | 0 | temp = s->control; |
724 | 0 | break; |
725 | 0 | case SJA_BCAN_SR: /* Status register, addr 2 */ |
726 | 0 | temp = s->status_bas; |
727 | 0 | break; |
728 | 0 | case SJA_BCAN_IR: /* Interrupt register, addr 3 */ |
729 | 0 | temp = s->interrupt_bas; |
730 | 0 | s->interrupt_bas = 0; |
731 | 0 | if (s->rxmsg_cnt) { |
732 | 0 | s->interrupt_bas |= (1 << 0); /* Receive interrupt. */ |
733 | 0 | } |
734 | 0 | can_sja_update_bas_irq(s); |
735 | 0 | break; |
736 | 0 | case 4: |
737 | 0 | temp = s->code; |
738 | 0 | break; |
739 | 0 | case 5: |
740 | 0 | temp = s->mask; |
741 | 0 | break; |
742 | 0 | case 20 ... 29: |
743 | 0 | temp = s->rx_buff[(s->rxbuf_start + addr - 20) % SJA_RCV_BUF_LEN]; |
744 | 0 | break; |
745 | 0 | case 31: |
746 | 0 | temp = s->clock; |
747 | 0 | break; |
748 | 0 | default: |
749 | 0 | temp = 0xff; |
750 | 0 | break; |
751 | 0 | } |
752 | 0 | } |
753 | 0 | DPRINTF("read addr 0x%02x, %d bytes, content 0x%02lx\n", |
754 | 0 | (int)addr, size, (long unsigned int)temp); |
755 | |
|
756 | 0 | return temp; |
757 | 0 | } |
758 | | |
759 | | bool can_sja_can_receive(CanBusClientState *client) |
760 | 0 | { |
761 | 0 | CanSJA1000State *s = container_of(client, CanSJA1000State, bus_client); |
762 | |
|
763 | 0 | if (s->clock & 0x80) { /* PeliCAN Mode */ |
764 | 0 | if (s->mode & 0x01) { /* reset mode. */ |
765 | 0 | return false; |
766 | 0 | } |
767 | 0 | } else { /* BasicCAN mode */ |
768 | 0 | if (s->control & 0x01) { |
769 | 0 | return false; |
770 | 0 | } |
771 | 0 | } |
772 | | |
773 | 0 | return true; /* always return true, when operation mode */ |
774 | 0 | } |
775 | | |
776 | | ssize_t can_sja_receive(CanBusClientState *client, const qemu_can_frame *frames, |
777 | | size_t frames_cnt) |
778 | 0 | { |
779 | 0 | CanSJA1000State *s = container_of(client, CanSJA1000State, bus_client); |
780 | 0 | static uint8_t rcv[SJA_MSG_MAX_LEN]; |
781 | 0 | int i; |
782 | 0 | int ret = -1; |
783 | 0 | const qemu_can_frame *frame = frames; |
784 | |
|
785 | 0 | if (frames_cnt <= 0) { |
786 | 0 | return 0; |
787 | 0 | } |
788 | 0 | if (frame->flags & QEMU_CAN_FRMF_TYPE_FD) { |
789 | 0 | if (DEBUG_FILTER) { |
790 | 0 | can_display_msg("[cansja]: ignor fd frame ", frame); |
791 | 0 | } |
792 | 0 | return 1; |
793 | 0 | } |
794 | | |
795 | 0 | if (DEBUG_FILTER) { |
796 | 0 | can_display_msg("[cansja]: receive ", frame); |
797 | 0 | } |
798 | |
|
799 | 0 | if (s->clock & 0x80) { /* PeliCAN Mode */ |
800 | | |
801 | | /* the CAN controller is receiving a message */ |
802 | 0 | s->status_pel |= (1 << 4); |
803 | |
|
804 | 0 | if (can_sja_accept_filter(s, frame) == 0) { |
805 | 0 | s->status_pel &= ~(1 << 4); |
806 | 0 | if (DEBUG_FILTER) { |
807 | 0 | qemu_log("[cansja]: filter rejects message\n"); |
808 | 0 | } |
809 | 0 | return ret; |
810 | 0 | } |
811 | | |
812 | 0 | ret = frame2buff_pel(frame, rcv); |
813 | 0 | if (ret < 0) { |
814 | 0 | s->status_pel &= ~(1 << 4); |
815 | 0 | if (DEBUG_FILTER) { |
816 | 0 | qemu_log("[cansja]: message store failed\n"); |
817 | 0 | } |
818 | 0 | return ret; /* maybe not support now. */ |
819 | 0 | } |
820 | | |
821 | 0 | if (s->rx_cnt + ret > SJA_RCV_BUF_LEN) { /* Data overrun. */ |
822 | 0 | s->status_pel |= (1 << 1); /* Overrun status */ |
823 | 0 | s->interrupt_pel |= (1 << 3); |
824 | 0 | s->status_pel &= ~(1 << 4); |
825 | 0 | if (DEBUG_FILTER) { |
826 | 0 | qemu_log("[cansja]: receive FIFO overrun\n"); |
827 | 0 | } |
828 | 0 | can_sja_update_pel_irq(s); |
829 | 0 | return ret; |
830 | 0 | } |
831 | 0 | s->rx_cnt += ret; |
832 | 0 | s->rxmsg_cnt++; |
833 | 0 | if (DEBUG_FILTER) { |
834 | 0 | qemu_log("[cansja]: message stored in receive FIFO\n"); |
835 | 0 | } |
836 | |
|
837 | 0 | for (i = 0; i < ret; i++) { |
838 | 0 | s->rx_buff[(s->rx_ptr++) % SJA_RCV_BUF_LEN] = rcv[i]; |
839 | 0 | } |
840 | 0 | s->rx_ptr %= SJA_RCV_BUF_LEN; /* update the pointer. */ |
841 | |
|
842 | 0 | s->status_pel |= 0x01; /* Set the Receive Buffer Status. DS-p23 */ |
843 | 0 | s->interrupt_pel |= 0x01; |
844 | 0 | s->status_pel &= ~(1 << 4); |
845 | 0 | can_sja_update_pel_irq(s); |
846 | 0 | } else { /* BasicCAN mode */ |
847 | | |
848 | | /* the CAN controller is receiving a message */ |
849 | 0 | s->status_bas |= (1 << 4); |
850 | |
|
851 | 0 | ret = frame2buff_bas(frame, rcv); |
852 | 0 | if (ret < 0) { |
853 | 0 | s->status_bas &= ~(1 << 4); |
854 | 0 | if (DEBUG_FILTER) { |
855 | 0 | qemu_log("[cansja]: message store failed\n"); |
856 | 0 | } |
857 | 0 | return ret; /* maybe not support now. */ |
858 | 0 | } |
859 | | |
860 | 0 | if (s->rx_cnt + ret > SJA_RCV_BUF_LEN) { /* Data overrun. */ |
861 | 0 | s->status_bas |= (1 << 1); /* Overrun status */ |
862 | 0 | s->status_bas &= ~(1 << 4); |
863 | 0 | s->interrupt_bas |= (1 << 3); |
864 | 0 | can_sja_update_bas_irq(s); |
865 | 0 | if (DEBUG_FILTER) { |
866 | 0 | qemu_log("[cansja]: receive FIFO overrun\n"); |
867 | 0 | } |
868 | 0 | return ret; |
869 | 0 | } |
870 | 0 | s->rx_cnt += ret; |
871 | 0 | s->rxmsg_cnt++; |
872 | |
|
873 | 0 | if (DEBUG_FILTER) { |
874 | 0 | qemu_log("[cansja]: message stored\n"); |
875 | 0 | } |
876 | |
|
877 | 0 | for (i = 0; i < ret; i++) { |
878 | 0 | s->rx_buff[(s->rx_ptr++) % SJA_RCV_BUF_LEN] = rcv[i]; |
879 | 0 | } |
880 | 0 | s->rx_ptr %= SJA_RCV_BUF_LEN; /* update the pointer. */ |
881 | |
|
882 | 0 | s->status_bas |= 0x01; /* Set the Receive Buffer Status. DS-p15 */ |
883 | 0 | s->status_bas &= ~(1 << 4); |
884 | 0 | s->interrupt_bas |= (1 << 0); |
885 | 0 | can_sja_update_bas_irq(s); |
886 | 0 | } |
887 | 0 | return 1; |
888 | 0 | } |
889 | | |
890 | | static CanBusClientInfo can_sja_bus_client_info = { |
891 | | .can_receive = can_sja_can_receive, |
892 | | .receive = can_sja_receive, |
893 | | }; |
894 | | |
895 | | |
896 | | int can_sja_connect_to_bus(CanSJA1000State *s, CanBusState *bus) |
897 | 0 | { |
898 | 0 | s->bus_client.info = &can_sja_bus_client_info; |
899 | |
|
900 | 0 | if (!bus) { |
901 | 0 | return -EINVAL; |
902 | 0 | } |
903 | | |
904 | 0 | if (can_bus_insert_client(bus, &s->bus_client) < 0) { |
905 | 0 | return -1; |
906 | 0 | } |
907 | | |
908 | 0 | return 0; |
909 | 0 | } |
910 | | |
911 | | void can_sja_disconnect(CanSJA1000State *s) |
912 | 0 | { |
913 | 0 | can_bus_remove_client(&s->bus_client); |
914 | 0 | } |
915 | | |
916 | | int can_sja_init(CanSJA1000State *s, qemu_irq irq) |
917 | 0 | { |
918 | 0 | s->irq = irq; |
919 | |
|
920 | 0 | qemu_irq_lower(s->irq); |
921 | |
|
922 | 0 | can_sja_hardware_reset(s); |
923 | |
|
924 | 0 | return 0; |
925 | 0 | } |
926 | | |
927 | | const VMStateDescription vmstate_qemu_can_filter = { |
928 | | .name = "qemu_can_filter", |
929 | | .version_id = 1, |
930 | | .minimum_version_id = 1, |
931 | | .fields = (const VMStateField[]) { |
932 | | VMSTATE_UINT32(can_id, qemu_can_filter), |
933 | | VMSTATE_UINT32(can_mask, qemu_can_filter), |
934 | | VMSTATE_END_OF_LIST() |
935 | | } |
936 | | }; |
937 | | |
938 | | static int can_sja_post_load(void *opaque, int version_id) |
939 | 0 | { |
940 | 0 | CanSJA1000State *s = opaque; |
941 | 0 | if (s->clock & 0x80) { /* PeliCAN Mode */ |
942 | 0 | can_sja_update_pel_irq(s); |
943 | 0 | } else { |
944 | 0 | can_sja_update_bas_irq(s); |
945 | 0 | } |
946 | 0 | return 0; |
947 | 0 | } |
948 | | |
949 | | /* VMState is needed for live migration of QEMU images */ |
950 | | const VMStateDescription vmstate_can_sja = { |
951 | | .name = "can_sja", |
952 | | .version_id = 1, |
953 | | .minimum_version_id = 1, |
954 | | .post_load = can_sja_post_load, |
955 | | .fields = (const VMStateField[]) { |
956 | | VMSTATE_UINT8(mode, CanSJA1000State), |
957 | | |
958 | | VMSTATE_UINT8(status_pel, CanSJA1000State), |
959 | | VMSTATE_UINT8(interrupt_pel, CanSJA1000State), |
960 | | VMSTATE_UINT8(interrupt_en, CanSJA1000State), |
961 | | VMSTATE_UINT8(rxmsg_cnt, CanSJA1000State), |
962 | | VMSTATE_UINT8(rxbuf_start, CanSJA1000State), |
963 | | VMSTATE_UINT8(clock, CanSJA1000State), |
964 | | |
965 | | VMSTATE_BUFFER(code_mask, CanSJA1000State), |
966 | | VMSTATE_BUFFER(tx_buff, CanSJA1000State), |
967 | | |
968 | | VMSTATE_BUFFER(rx_buff, CanSJA1000State), |
969 | | |
970 | | VMSTATE_UINT32(rx_ptr, CanSJA1000State), |
971 | | VMSTATE_UINT32(rx_cnt, CanSJA1000State), |
972 | | |
973 | | VMSTATE_UINT8(control, CanSJA1000State), |
974 | | |
975 | | VMSTATE_UINT8(status_bas, CanSJA1000State), |
976 | | VMSTATE_UINT8(interrupt_bas, CanSJA1000State), |
977 | | VMSTATE_UINT8(code, CanSJA1000State), |
978 | | VMSTATE_UINT8(mask, CanSJA1000State), |
979 | | |
980 | | VMSTATE_STRUCT_ARRAY(filter, CanSJA1000State, 4, 0, |
981 | | vmstate_qemu_can_filter, qemu_can_filter), |
982 | | |
983 | | |
984 | | VMSTATE_END_OF_LIST() |
985 | | } |
986 | | }; |