Merge tag 'pull-loongarch-20241016' of https://gitlab.com/gaosong/qemu into staging
[qemu/armbru.git] / hw / net / can / can_sja1000.c
blob6694d7bfd84abf2a4209ce7e24c9d53bb931e5f5
1 /*
2 * CAN device - SJA1000 chip emulation for QEMU
4 * Copyright (c) 2013-2014 Jin Yang
5 * Copyright (c) 2014-2018 Pavel Pisa
7 * Initial development supported by Google GSoC 2013 from RTEMS project slot
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:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
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.
28 #include "qemu/osdep.h"
29 #include "qemu/log.h"
30 #include "chardev/char.h"
31 #include "hw/irq.h"
32 #include "migration/vmstate.h"
33 #include "net/can_emu.h"
35 #include "can_sja1000.h"
37 #ifndef DEBUG_FILTER
38 #define DEBUG_FILTER 0
39 #endif /*DEBUG_FILTER*/
41 #ifndef DEBUG_CAN
42 #define DEBUG_CAN 0
43 #endif /*DEBUG_CAN*/
45 #define DPRINTF(fmt, ...) \
46 do { \
47 if (DEBUG_CAN) { \
48 qemu_log("[cansja]: " fmt , ## __VA_ARGS__); \
49 } \
50 } while (0)
52 static void can_sja_software_reset(CanSJA1000State *s)
54 s->mode &= ~0x31;
55 s->mode |= 0x01;
56 s->status_pel &= ~0x37;
57 s->status_pel |= 0x34;
59 s->rxbuf_start = 0x00;
60 s->rxmsg_cnt = 0x00;
61 s->rx_cnt = 0x00;
64 void can_sja_hardware_reset(CanSJA1000State *s)
66 /* Reset by hardware, p10 */
67 s->mode = 0x01;
68 s->status_pel = 0x3c;
69 s->interrupt_pel = 0x00;
70 s->clock = 0x00;
71 s->rxbuf_start = 0x00;
72 s->rxmsg_cnt = 0x00;
73 s->rx_cnt = 0x00;
75 s->control = 0x01;
76 s->status_bas = 0x0c;
77 s->interrupt_bas = 0x00;
79 qemu_irq_lower(s->irq);
82 static
83 void can_sja_single_filter(struct qemu_can_filter *filter,
84 const uint8_t *acr, const uint8_t *amr, int extended)
86 if (extended) {
87 filter->can_id = (uint32_t)acr[0] << 21;
88 filter->can_id |= (uint32_t)acr[1] << 13;
89 filter->can_id |= (uint32_t)acr[2] << 5;
90 filter->can_id |= (uint32_t)acr[3] >> 3;
91 if (acr[3] & 4) {
92 filter->can_id |= QEMU_CAN_RTR_FLAG;
95 filter->can_mask = (uint32_t)amr[0] << 21;
96 filter->can_mask |= (uint32_t)amr[1] << 13;
97 filter->can_mask |= (uint32_t)amr[2] << 5;
98 filter->can_mask |= (uint32_t)amr[3] >> 3;
99 filter->can_mask = ~filter->can_mask & QEMU_CAN_EFF_MASK;
100 if (!(amr[3] & 4)) {
101 filter->can_mask |= QEMU_CAN_RTR_FLAG;
103 } else {
104 filter->can_id = (uint32_t)acr[0] << 3;
105 filter->can_id |= (uint32_t)acr[1] >> 5;
106 if (acr[1] & 0x10) {
107 filter->can_id |= QEMU_CAN_RTR_FLAG;
110 filter->can_mask = (uint32_t)amr[0] << 3;
111 filter->can_mask |= (uint32_t)amr[1] >> 5;
112 filter->can_mask = ~filter->can_mask & QEMU_CAN_SFF_MASK;
113 if (!(amr[1] & 0x10)) {
114 filter->can_mask |= QEMU_CAN_RTR_FLAG;
119 static
120 void can_sja_dual_filter(struct qemu_can_filter *filter,
121 const uint8_t *acr, const uint8_t *amr, int extended)
123 if (extended) {
124 filter->can_id = (uint32_t)acr[0] << 21;
125 filter->can_id |= (uint32_t)acr[1] << 13;
127 filter->can_mask = (uint32_t)amr[0] << 21;
128 filter->can_mask |= (uint32_t)amr[1] << 13;
129 filter->can_mask = ~filter->can_mask & QEMU_CAN_EFF_MASK & ~0x1fff;
130 } else {
131 filter->can_id = (uint32_t)acr[0] << 3;
132 filter->can_id |= (uint32_t)acr[1] >> 5;
133 if (acr[1] & 0x10) {
134 filter->can_id |= QEMU_CAN_RTR_FLAG;
137 filter->can_mask = (uint32_t)amr[0] << 3;
138 filter->can_mask |= (uint32_t)amr[1] >> 5;
139 filter->can_mask = ~filter->can_mask & QEMU_CAN_SFF_MASK;
140 if (!(amr[1] & 0x10)) {
141 filter->can_mask |= QEMU_CAN_RTR_FLAG;
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)
152 struct qemu_can_filter filter;
154 if (s->clock & 0x80) { /* PeliCAN Mode */
155 if (s->mode & (1 << 3)) { /* Single mode. */
156 if (frame->can_id & QEMU_CAN_EFF_FLAG) { /* EFF */
157 can_sja_single_filter(&filter,
158 s->code_mask + 0, s->code_mask + 4, 1);
160 if (!can_bus_filter_match(&filter, frame->can_id)) {
161 return 0;
163 } else { /* SFF */
164 can_sja_single_filter(&filter,
165 s->code_mask + 0, s->code_mask + 4, 0);
167 if (!can_bus_filter_match(&filter, frame->can_id)) {
168 return 0;
171 if (frame->can_id & QEMU_CAN_RTR_FLAG) { /* RTR */
172 return 1;
175 if (frame->can_dlc == 0) {
176 return 1;
179 if ((frame->data[0] & ~(s->code_mask[6])) !=
180 (s->code_mask[2] & ~(s->code_mask[6]))) {
181 return 0;
184 if (frame->can_dlc < 2) {
185 return 1;
188 if ((frame->data[1] & ~(s->code_mask[7])) ==
189 (s->code_mask[3] & ~(s->code_mask[7]))) {
190 return 1;
193 return 0;
195 } else { /* Dual mode */
196 if (frame->can_id & QEMU_CAN_EFF_FLAG) { /* EFF */
197 can_sja_dual_filter(&filter,
198 s->code_mask + 0, s->code_mask + 4, 1);
200 if (can_bus_filter_match(&filter, frame->can_id)) {
201 return 1;
204 can_sja_dual_filter(&filter,
205 s->code_mask + 2, s->code_mask + 6, 1);
207 if (can_bus_filter_match(&filter, frame->can_id)) {
208 return 1;
211 return 0;
212 } else {
213 can_sja_dual_filter(&filter,
214 s->code_mask + 0, s->code_mask + 4, 0);
216 if (can_bus_filter_match(&filter, frame->can_id)) {
217 uint8_t expect;
218 uint8_t mask;
219 expect = s->code_mask[1] << 4;
220 expect |= s->code_mask[3] & 0x0f;
222 mask = s->code_mask[5] << 4;
223 mask |= s->code_mask[7] & 0x0f;
224 mask = ~mask & 0xff;
226 if ((frame->data[0] & mask) ==
227 (expect & mask)) {
228 return 1;
232 can_sja_dual_filter(&filter,
233 s->code_mask + 2, s->code_mask + 6, 0);
235 if (can_bus_filter_match(&filter, frame->can_id)) {
236 return 1;
239 return 0;
244 return 1;
247 static void can_display_msg(const char *prefix, const qemu_can_frame *msg)
249 int i;
250 FILE *logfile = qemu_log_trylock();
252 if (logfile) {
253 fprintf(logfile, "%s%03X [%01d] %s %s",
254 prefix,
255 msg->can_id & QEMU_CAN_EFF_MASK,
256 msg->can_dlc,
257 msg->can_id & QEMU_CAN_EFF_FLAG ? "EFF" : "SFF",
258 msg->can_id & QEMU_CAN_RTR_FLAG ? "RTR" : "DAT");
260 for (i = 0; i < msg->can_dlc; i++) {
261 fprintf(logfile, " %02X", msg->data[i]);
263 fprintf(logfile, "\n");
264 qemu_log_unlock(logfile);
268 static void buff2frame_pel(const uint8_t *buff, qemu_can_frame *frame)
270 uint8_t i;
272 frame->flags = 0;
273 frame->can_id = 0;
274 if (buff[0] & 0x40) { /* RTR */
275 frame->can_id = QEMU_CAN_RTR_FLAG;
277 frame->can_dlc = buff[0] & 0x0f;
279 if (frame->can_dlc > 8) {
280 frame->can_dlc = 8;
283 if (buff[0] & 0x80) { /* Extended */
284 frame->can_id |= QEMU_CAN_EFF_FLAG;
285 frame->can_id |= buff[1] << 21; /* ID.28~ID.21 */
286 frame->can_id |= buff[2] << 13; /* ID.20~ID.13 */
287 frame->can_id |= buff[3] << 5;
288 frame->can_id |= buff[4] >> 3;
289 for (i = 0; i < frame->can_dlc; i++) {
290 frame->data[i] = buff[5 + i];
292 for (; i < 8; i++) {
293 frame->data[i] = 0;
295 } else {
296 frame->can_id |= buff[1] << 3;
297 frame->can_id |= buff[2] >> 5;
298 for (i = 0; i < frame->can_dlc; i++) {
299 frame->data[i] = buff[3 + i];
301 for (; i < 8; i++) {
302 frame->data[i] = 0;
308 static void buff2frame_bas(const uint8_t *buff, qemu_can_frame *frame)
310 uint8_t i;
312 frame->flags = 0;
313 frame->can_id = ((buff[0] << 3) & (0xff << 3)) + ((buff[1] >> 5) & 0x07);
314 if (buff[1] & 0x10) { /* RTR */
315 frame->can_id = QEMU_CAN_RTR_FLAG;
317 frame->can_dlc = buff[1] & 0x0f;
319 if (frame->can_dlc > 8) {
320 frame->can_dlc = 8;
323 for (i = 0; i < frame->can_dlc; i++) {
324 frame->data[i] = buff[2 + i];
326 for (; i < 8; i++) {
327 frame->data[i] = 0;
332 static int frame2buff_pel(const qemu_can_frame *frame, uint8_t *buff)
334 int i;
335 int dlen = frame->can_dlc;
337 if (frame->can_id & QEMU_CAN_ERR_FLAG) { /* error frame, NOT support now. */
338 return -1;
341 if (dlen > 8) {
342 return -1;
345 buff[0] = 0x0f & frame->can_dlc; /* DLC */
346 if (frame->can_id & QEMU_CAN_RTR_FLAG) { /* RTR */
347 buff[0] |= (1 << 6);
349 if (frame->can_id & QEMU_CAN_EFF_FLAG) { /* EFF */
350 buff[0] |= (1 << 7);
351 buff[1] = extract32(frame->can_id, 21, 8); /* ID.28~ID.21 */
352 buff[2] = extract32(frame->can_id, 13, 8); /* ID.20~ID.13 */
353 buff[3] = extract32(frame->can_id, 5, 8); /* ID.12~ID.05 */
354 buff[4] = extract32(frame->can_id, 0, 5) << 3; /* ID.04~ID.00,xxx */
355 for (i = 0; i < dlen; i++) {
356 buff[5 + i] = frame->data[i];
358 return dlen + 5;
359 } else { /* SFF */
360 buff[1] = extract32(frame->can_id, 3, 8); /* ID.10~ID.03 */
361 buff[2] = extract32(frame->can_id, 0, 3) << 5; /* ID.02~ID.00,xxxxx */
362 for (i = 0; i < dlen; i++) {
363 buff[3 + i] = frame->data[i];
366 return dlen + 3;
369 return -1;
372 static int frame2buff_bas(const qemu_can_frame *frame, uint8_t *buff)
374 int i;
375 int dlen = frame->can_dlc;
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
382 if ((frame->can_id & QEMU_CAN_EFF_FLAG) ||
383 (frame->can_id & QEMU_CAN_ERR_FLAG)) {
384 return -1;
387 if (dlen > 8) {
388 return -1;
391 buff[0] = extract32(frame->can_id, 3, 8); /* ID.10~ID.03 */
392 buff[1] = extract32(frame->can_id, 0, 3) << 5; /* ID.02~ID.00,xxxxx */
393 if (frame->can_id & QEMU_CAN_RTR_FLAG) { /* RTR */
394 buff[1] |= (1 << 4);
396 buff[1] |= frame->can_dlc & 0x0f;
397 for (i = 0; i < dlen; i++) {
398 buff[2 + i] = frame->data[i];
401 return dlen + 2;
404 static void can_sja_update_pel_irq(CanSJA1000State *s)
406 if (s->interrupt_en & s->interrupt_pel) {
407 qemu_irq_raise(s->irq);
408 } else {
409 qemu_irq_lower(s->irq);
413 static void can_sja_update_bas_irq(CanSJA1000State *s)
415 if ((s->control >> 1) & s->interrupt_bas) {
416 qemu_irq_raise(s->irq);
417 } else {
418 qemu_irq_lower(s->irq);
422 void can_sja_mem_write(CanSJA1000State *s, hwaddr addr, uint64_t val,
423 unsigned size)
425 qemu_can_frame frame;
426 uint32_t tmp;
427 uint8_t tmp8, count;
430 DPRINTF("write 0x%02llx addr 0x%02x\n",
431 (unsigned long long)val, (unsigned int)addr);
433 if (addr > CAN_SJA_MEM_SIZE) {
434 return;
437 if (s->clock & 0x80) { /* PeliCAN Mode */
438 switch (addr) {
439 case SJA_MOD: /* Mode register */
440 s->mode = 0x1f & val;
441 if ((s->mode & 0x01) && ((val & 0x01) == 0)) {
442 /* Go to operation mode from reset mode. */
443 if (s->mode & (1 << 3)) { /* Single mode. */
444 /* For EFF */
445 can_sja_single_filter(&s->filter[0],
446 s->code_mask + 0, s->code_mask + 4, 1);
448 /* For SFF */
449 can_sja_single_filter(&s->filter[1],
450 s->code_mask + 0, s->code_mask + 4, 0);
452 can_bus_client_set_filters(&s->bus_client, s->filter, 2);
453 } else { /* Dual mode */
454 /* For EFF */
455 can_sja_dual_filter(&s->filter[0],
456 s->code_mask + 0, s->code_mask + 4, 1);
458 can_sja_dual_filter(&s->filter[1],
459 s->code_mask + 2, s->code_mask + 6, 1);
461 /* For SFF */
462 can_sja_dual_filter(&s->filter[2],
463 s->code_mask + 0, s->code_mask + 4, 0);
465 can_sja_dual_filter(&s->filter[3],
466 s->code_mask + 2, s->code_mask + 6, 0);
468 can_bus_client_set_filters(&s->bus_client, s->filter, 4);
471 s->rxmsg_cnt = 0;
472 s->rx_cnt = 0;
474 break;
476 case SJA_CMR: /* Command register. */
477 if (0x01 & val) { /* Send transmission request. */
478 buff2frame_pel(s->tx_buff, &frame);
479 if (DEBUG_FILTER) {
480 can_display_msg("[cansja]: Tx request " , &frame);
484 * Clear transmission complete status,
485 * and Transmit Buffer Status.
486 * write to the backends.
488 s->status_pel &= ~(3 << 2);
490 can_bus_client_send(&s->bus_client, &frame, 1);
493 * Set transmission complete status
494 * and Transmit Buffer Status.
496 s->status_pel |= (3 << 2);
498 /* Clear transmit status. */
499 s->status_pel &= ~(1 << 5);
500 s->interrupt_pel |= 0x02;
501 can_sja_update_pel_irq(s);
503 if (0x04 & val) { /* Release Receive Buffer */
504 if (s->rxmsg_cnt <= 0) {
505 break;
508 tmp8 = s->rx_buff[s->rxbuf_start]; count = 0;
509 if (tmp8 & (1 << 7)) { /* EFF */
510 count += 2;
512 count += 3;
513 if (!(tmp8 & (1 << 6))) { /* DATA */
514 count += (tmp8 & 0x0f);
517 if (DEBUG_FILTER) {
518 qemu_log("[cansja]: message released from "
519 "Rx FIFO cnt=%d, count=%d\n", s->rx_cnt, count);
522 s->rxbuf_start += count;
523 s->rxbuf_start %= SJA_RCV_BUF_LEN;
525 s->rx_cnt -= count;
526 s->rxmsg_cnt--;
527 if (s->rxmsg_cnt == 0) {
528 s->status_pel &= ~(1 << 0);
529 s->interrupt_pel &= ~(1 << 0);
530 can_sja_update_pel_irq(s);
533 if (0x08 & val) { /* Clear data overrun */
534 s->status_pel &= ~(1 << 1);
535 s->interrupt_pel &= ~(1 << 3);
536 can_sja_update_pel_irq(s);
538 break;
539 case SJA_SR: /* Status register */
540 case SJA_IR: /* Interrupt register */
541 break; /* Do nothing */
542 case SJA_IER: /* Interrupt enable register */
543 s->interrupt_en = val;
544 break;
545 case 16: /* RX frame information addr16-28. */
546 s->status_pel |= (1 << 5); /* Set transmit status. */
547 /* fallthrough */
548 case 17 ... 28:
549 if (s->mode & 0x01) { /* Reset mode */
550 if (addr < 24) {
551 s->code_mask[addr - 16] = val;
553 } else { /* Operation mode */
554 s->tx_buff[addr - 16] = val; /* Store to TX buffer directly. */
556 break;
557 case SJA_CDR:
558 s->clock = val;
559 break;
561 } else { /* Basic Mode */
562 switch (addr) {
563 case SJA_BCAN_CTR: /* Control register, addr 0 */
564 if ((s->control & 0x01) && ((val & 0x01) == 0)) {
565 /* Go to operation mode from reset mode. */
566 s->filter[0].can_id = (s->code << 3) & (0xff << 3);
567 tmp = (~(s->mask << 3)) & (0xff << 3);
568 tmp |= QEMU_CAN_EFF_FLAG; /* Only Basic CAN Frame. */
569 s->filter[0].can_mask = tmp;
570 can_bus_client_set_filters(&s->bus_client, s->filter, 1);
572 s->rxmsg_cnt = 0;
573 s->rx_cnt = 0;
574 } else if (!(s->control & 0x01) && !(val & 0x01)) {
575 can_sja_software_reset(s);
578 s->control = 0x1f & val;
579 break;
580 case SJA_BCAN_CMR: /* Command register, addr 1 */
581 if (0x01 & val) { /* Send transmission request. */
582 buff2frame_bas(s->tx_buff, &frame);
583 if (DEBUG_FILTER) {
584 can_display_msg("[cansja]: Tx request " , &frame);
588 * Clear transmission complete status,
589 * and Transmit Buffer Status.
591 s->status_bas &= ~(3 << 2);
593 /* write to the backends. */
594 can_bus_client_send(&s->bus_client, &frame, 1);
597 * Set transmission complete status,
598 * and Transmit Buffer Status.
600 s->status_bas |= (3 << 2);
602 /* Clear transmit status. */
603 s->status_bas &= ~(1 << 5);
604 s->interrupt_bas |= 0x02;
605 can_sja_update_bas_irq(s);
607 if (0x04 & val) { /* Release Receive Buffer */
608 if (s->rxmsg_cnt <= 0) {
609 break;
612 tmp8 = s->rx_buff[(s->rxbuf_start + 1) % SJA_RCV_BUF_LEN];
613 count = 2 + (tmp8 & 0x0f);
615 if (DEBUG_FILTER) {
616 qemu_log("[cansja]: message released from "
617 "Rx FIFO cnt=%d, count=%d\n", s->rx_cnt, count);
620 s->rxbuf_start += count;
621 s->rxbuf_start %= SJA_RCV_BUF_LEN;
622 s->rx_cnt -= count;
623 s->rxmsg_cnt--;
625 if (s->rxmsg_cnt == 0) {
626 s->status_bas &= ~(1 << 0);
627 s->interrupt_bas &= ~(1 << 0);
628 can_sja_update_bas_irq(s);
631 if (0x08 & val) { /* Clear data overrun */
632 s->status_bas &= ~(1 << 1);
633 s->interrupt_bas &= ~(1 << 3);
634 can_sja_update_bas_irq(s);
636 break;
637 case 4:
638 s->code = val;
639 break;
640 case 5:
641 s->mask = val;
642 break;
643 case 10:
644 s->status_bas |= (1 << 5); /* Set transmit status. */
645 /* fallthrough */
646 case 11 ... 19:
647 if ((s->control & 0x01) == 0) { /* Operation mode */
648 s->tx_buff[addr - 10] = val; /* Store to TX buffer directly. */
650 break;
651 case SJA_CDR:
652 s->clock = val;
653 break;
658 uint64_t can_sja_mem_read(CanSJA1000State *s, hwaddr addr, unsigned size)
660 uint64_t temp = 0;
662 DPRINTF("read addr 0x%02x ...\n", (unsigned int)addr);
664 if (addr > CAN_SJA_MEM_SIZE) {
665 return 0;
668 if (s->clock & 0x80) { /* PeliCAN Mode */
669 switch (addr) {
670 case SJA_MOD: /* Mode register, addr 0 */
671 temp = s->mode;
672 break;
673 case SJA_CMR: /* Command register, addr 1 */
674 temp = 0x00; /* Command register, cannot be read. */
675 break;
676 case SJA_SR: /* Status register, addr 2 */
677 temp = s->status_pel;
678 break;
679 case SJA_IR: /* Interrupt register, addr 3 */
680 temp = s->interrupt_pel;
681 s->interrupt_pel = 0;
682 if (s->rxmsg_cnt) {
683 s->interrupt_pel |= (1 << 0); /* Receive interrupt. */
685 can_sja_update_pel_irq(s);
686 break;
687 case SJA_IER: /* Interrupt enable register, addr 4 */
688 temp = s->interrupt_en;
689 break;
690 case 5: /* Reserved */
691 case 6: /* Bus timing 0, hardware related, not support now. */
692 case 7: /* Bus timing 1, hardware related, not support now. */
693 case 8: /*
694 * Output control register, hardware related,
695 * not supported for now.
697 case 9: /* Test. */
698 case 10 ... 15: /* Reserved */
699 temp = 0x00;
700 break;
702 case 16 ... 28:
703 if (s->mode & 0x01) { /* Reset mode */
704 if (addr < 24) {
705 temp = s->code_mask[addr - 16];
706 } else {
707 temp = 0x00;
709 } else { /* Operation mode */
710 temp = s->rx_buff[(s->rxbuf_start + addr - 16) %
711 SJA_RCV_BUF_LEN];
713 break;
714 case SJA_CDR:
715 temp = s->clock;
716 break;
717 default:
718 temp = 0xff;
720 } else { /* Basic Mode */
721 switch (addr) {
722 case SJA_BCAN_CTR: /* Control register, addr 0 */
723 temp = s->control;
724 break;
725 case SJA_BCAN_SR: /* Status register, addr 2 */
726 temp = s->status_bas;
727 break;
728 case SJA_BCAN_IR: /* Interrupt register, addr 3 */
729 temp = s->interrupt_bas;
730 s->interrupt_bas = 0;
731 if (s->rxmsg_cnt) {
732 s->interrupt_bas |= (1 << 0); /* Receive interrupt. */
734 can_sja_update_bas_irq(s);
735 break;
736 case 4:
737 temp = s->code;
738 break;
739 case 5:
740 temp = s->mask;
741 break;
742 case 20 ... 29:
743 temp = s->rx_buff[(s->rxbuf_start + addr - 20) % SJA_RCV_BUF_LEN];
744 break;
745 case 31:
746 temp = s->clock;
747 break;
748 default:
749 temp = 0xff;
750 break;
753 DPRINTF("read addr 0x%02x, %d bytes, content 0x%02lx\n",
754 (int)addr, size, (long unsigned int)temp);
756 return temp;
759 bool can_sja_can_receive(CanBusClientState *client)
761 CanSJA1000State *s = container_of(client, CanSJA1000State, bus_client);
763 if (s->clock & 0x80) { /* PeliCAN Mode */
764 if (s->mode & 0x01) { /* reset mode. */
765 return false;
767 } else { /* BasicCAN mode */
768 if (s->control & 0x01) {
769 return false;
773 return true; /* always return true, when operation mode */
776 ssize_t can_sja_receive(CanBusClientState *client, const qemu_can_frame *frames,
777 size_t frames_cnt)
779 CanSJA1000State *s = container_of(client, CanSJA1000State, bus_client);
780 static uint8_t rcv[SJA_MSG_MAX_LEN];
781 int i;
782 int ret = -1;
783 const qemu_can_frame *frame = frames;
785 if (frames_cnt <= 0) {
786 return 0;
788 if (frame->flags & QEMU_CAN_FRMF_TYPE_FD) {
789 if (DEBUG_FILTER) {
790 can_display_msg("[cansja]: ignor fd frame ", frame);
792 return 1;
795 if (DEBUG_FILTER) {
796 can_display_msg("[cansja]: receive ", frame);
799 if (s->clock & 0x80) { /* PeliCAN Mode */
801 /* the CAN controller is receiving a message */
802 s->status_pel |= (1 << 4);
804 if (can_sja_accept_filter(s, frame) == 0) {
805 s->status_pel &= ~(1 << 4);
806 if (DEBUG_FILTER) {
807 qemu_log("[cansja]: filter rejects message\n");
809 return ret;
812 ret = frame2buff_pel(frame, rcv);
813 if (ret < 0) {
814 s->status_pel &= ~(1 << 4);
815 if (DEBUG_FILTER) {
816 qemu_log("[cansja]: message store failed\n");
818 return ret; /* maybe not support now. */
821 if (s->rx_cnt + ret > SJA_RCV_BUF_LEN) { /* Data overrun. */
822 s->status_pel |= (1 << 1); /* Overrun status */
823 s->interrupt_pel |= (1 << 3);
824 s->status_pel &= ~(1 << 4);
825 if (DEBUG_FILTER) {
826 qemu_log("[cansja]: receive FIFO overrun\n");
828 can_sja_update_pel_irq(s);
829 return ret;
831 s->rx_cnt += ret;
832 s->rxmsg_cnt++;
833 if (DEBUG_FILTER) {
834 qemu_log("[cansja]: message stored in receive FIFO\n");
837 for (i = 0; i < ret; i++) {
838 s->rx_buff[(s->rx_ptr++) % SJA_RCV_BUF_LEN] = rcv[i];
840 s->rx_ptr %= SJA_RCV_BUF_LEN; /* update the pointer. */
842 s->status_pel |= 0x01; /* Set the Receive Buffer Status. DS-p23 */
843 s->interrupt_pel |= 0x01;
844 s->status_pel &= ~(1 << 4);
845 s->status_pel |= (1 << 0);
846 can_sja_update_pel_irq(s);
847 } else { /* BasicCAN mode */
849 /* the CAN controller is receiving a message */
850 s->status_bas |= (1 << 4);
852 ret = frame2buff_bas(frame, rcv);
853 if (ret < 0) {
854 s->status_bas &= ~(1 << 4);
855 if (DEBUG_FILTER) {
856 qemu_log("[cansja]: message store failed\n");
858 return ret; /* maybe not support now. */
861 if (s->rx_cnt + ret > SJA_RCV_BUF_LEN) { /* Data overrun. */
862 s->status_bas |= (1 << 1); /* Overrun status */
863 s->status_bas &= ~(1 << 4);
864 s->interrupt_bas |= (1 << 3);
865 can_sja_update_bas_irq(s);
866 if (DEBUG_FILTER) {
867 qemu_log("[cansja]: receive FIFO overrun\n");
869 return ret;
871 s->rx_cnt += ret;
872 s->rxmsg_cnt++;
874 if (DEBUG_FILTER) {
875 qemu_log("[cansja]: message stored\n");
878 for (i = 0; i < ret; i++) {
879 s->rx_buff[(s->rx_ptr++) % SJA_RCV_BUF_LEN] = rcv[i];
881 s->rx_ptr %= SJA_RCV_BUF_LEN; /* update the pointer. */
883 s->status_bas |= 0x01; /* Set the Receive Buffer Status. DS-p15 */
884 s->status_bas &= ~(1 << 4);
885 s->interrupt_bas |= (1 << 0);
886 can_sja_update_bas_irq(s);
888 return 1;
891 static CanBusClientInfo can_sja_bus_client_info = {
892 .can_receive = can_sja_can_receive,
893 .receive = can_sja_receive,
897 int can_sja_connect_to_bus(CanSJA1000State *s, CanBusState *bus)
899 s->bus_client.info = &can_sja_bus_client_info;
901 if (!bus) {
902 return -EINVAL;
905 if (can_bus_insert_client(bus, &s->bus_client) < 0) {
906 return -1;
909 return 0;
912 void can_sja_disconnect(CanSJA1000State *s)
914 can_bus_remove_client(&s->bus_client);
917 int can_sja_init(CanSJA1000State *s, qemu_irq irq)
919 s->irq = irq;
921 qemu_irq_lower(s->irq);
923 can_sja_hardware_reset(s);
925 return 0;
928 const VMStateDescription vmstate_qemu_can_filter = {
929 .name = "qemu_can_filter",
930 .version_id = 1,
931 .minimum_version_id = 1,
932 .fields = (const VMStateField[]) {
933 VMSTATE_UINT32(can_id, qemu_can_filter),
934 VMSTATE_UINT32(can_mask, qemu_can_filter),
935 VMSTATE_END_OF_LIST()
939 static int can_sja_post_load(void *opaque, int version_id)
941 CanSJA1000State *s = opaque;
942 if (s->clock & 0x80) { /* PeliCAN Mode */
943 can_sja_update_pel_irq(s);
944 } else {
945 can_sja_update_bas_irq(s);
947 return 0;
950 /* VMState is needed for live migration of QEMU images */
951 const VMStateDescription vmstate_can_sja = {
952 .name = "can_sja",
953 .version_id = 1,
954 .minimum_version_id = 1,
955 .post_load = can_sja_post_load,
956 .fields = (const VMStateField[]) {
957 VMSTATE_UINT8(mode, CanSJA1000State),
959 VMSTATE_UINT8(status_pel, CanSJA1000State),
960 VMSTATE_UINT8(interrupt_pel, CanSJA1000State),
961 VMSTATE_UINT8(interrupt_en, CanSJA1000State),
962 VMSTATE_UINT8(rxmsg_cnt, CanSJA1000State),
963 VMSTATE_UINT8(rxbuf_start, CanSJA1000State),
964 VMSTATE_UINT8(clock, CanSJA1000State),
966 VMSTATE_BUFFER(code_mask, CanSJA1000State),
967 VMSTATE_BUFFER(tx_buff, CanSJA1000State),
969 VMSTATE_BUFFER(rx_buff, CanSJA1000State),
971 VMSTATE_UINT32(rx_ptr, CanSJA1000State),
972 VMSTATE_UINT32(rx_cnt, CanSJA1000State),
974 VMSTATE_UINT8(control, CanSJA1000State),
976 VMSTATE_UINT8(status_bas, CanSJA1000State),
977 VMSTATE_UINT8(interrupt_bas, CanSJA1000State),
978 VMSTATE_UINT8(code, CanSJA1000State),
979 VMSTATE_UINT8(mask, CanSJA1000State),
981 VMSTATE_STRUCT_ARRAY(filter, CanSJA1000State, 4, 0,
982 vmstate_qemu_can_filter, qemu_can_filter),
985 VMSTATE_END_OF_LIST()