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
28 #include "qemu/osdep.h"
30 #include "chardev/char.h"
32 #include "migration/vmstate.h"
33 #include "net/can_emu.h"
35 #include "can_sja1000.h"
38 #define DEBUG_FILTER 0
39 #endif /*DEBUG_FILTER*/
45 #define DPRINTF(fmt, ...) \
48 qemu_log("[cansja]: " fmt , ## __VA_ARGS__); \
52 static void can_sja_software_reset(CanSJA1000State
*s
)
56 s
->status_pel
&= ~0x37;
57 s
->status_pel
|= 0x34;
59 s
->rxbuf_start
= 0x00;
64 void can_sja_hardware_reset(CanSJA1000State
*s
)
66 /* Reset by hardware, p10 */
69 s
->interrupt_pel
= 0x00;
71 s
->rxbuf_start
= 0x00;
77 s
->interrupt_bas
= 0x00;
79 qemu_irq_lower(s
->irq
);
83 void can_sja_single_filter(struct qemu_can_filter
*filter
,
84 const uint8_t *acr
, const uint8_t *amr
, int 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;
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
;
101 filter
->can_mask
|= QEMU_CAN_RTR_FLAG
;
104 filter
->can_id
= (uint32_t)acr
[0] << 3;
105 filter
->can_id
|= (uint32_t)acr
[1] >> 5;
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
;
120 void can_sja_dual_filter(struct qemu_can_filter
*filter
,
121 const uint8_t *acr
, const uint8_t *amr
, int 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;
131 filter
->can_id
= (uint32_t)acr
[0] << 3;
132 filter
->can_id
|= (uint32_t)acr
[1] >> 5;
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. */
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
)) {
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
)) {
171 if (frame
->can_id
& QEMU_CAN_RTR_FLAG
) { /* RTR */
175 if (frame
->can_dlc
== 0) {
179 if ((frame
->data
[0] & ~(s
->code_mask
[6])) !=
180 (s
->code_mask
[2] & ~(s
->code_mask
[6]))) {
184 if (frame
->can_dlc
< 2) {
188 if ((frame
->data
[1] & ~(s
->code_mask
[7])) ==
189 (s
->code_mask
[3] & ~(s
->code_mask
[7]))) {
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
)) {
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
)) {
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
)) {
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;
226 if ((frame
->data
[0] & mask
) ==
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
)) {
247 static void can_display_msg(const char *prefix
, const qemu_can_frame
*msg
)
250 FILE *logfile
= qemu_log_trylock();
253 fprintf(logfile
, "%s%03X [%01d] %s %s",
255 msg
->can_id
& QEMU_CAN_EFF_MASK
,
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
)
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) {
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
];
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
];
308 static void buff2frame_bas(const uint8_t *buff
, qemu_can_frame
*frame
)
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) {
323 for (i
= 0; i
< frame
->can_dlc
; i
++) {
324 frame
->data
[i
] = buff
[2 + i
];
332 static int frame2buff_pel(const qemu_can_frame
*frame
, uint8_t *buff
)
335 int dlen
= frame
->can_dlc
;
337 if (frame
->can_id
& QEMU_CAN_ERR_FLAG
) { /* error frame, NOT support now. */
345 buff
[0] = 0x0f & frame
->can_dlc
; /* DLC */
346 if (frame
->can_id
& QEMU_CAN_RTR_FLAG
) { /* RTR */
349 if (frame
->can_id
& QEMU_CAN_EFF_FLAG
) { /* EFF */
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
];
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
];
372 static int frame2buff_bas(const qemu_can_frame
*frame
, uint8_t *buff
)
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
)) {
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 */
396 buff
[1] |= frame
->can_dlc
& 0x0f;
397 for (i
= 0; i
< dlen
; i
++) {
398 buff
[2 + i
] = frame
->data
[i
];
404 static void can_sja_update_pel_irq(CanSJA1000State
*s
)
406 if (s
->interrupt_en
& s
->interrupt_pel
) {
407 qemu_irq_raise(s
->irq
);
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
);
418 qemu_irq_lower(s
->irq
);
422 void can_sja_mem_write(CanSJA1000State
*s
, hwaddr addr
, uint64_t val
,
425 qemu_can_frame frame
;
430 DPRINTF("write 0x%02llx addr 0x%02x\n",
431 (unsigned long long)val
, (unsigned int)addr
);
433 if (addr
> CAN_SJA_MEM_SIZE
) {
437 if (s
->clock
& 0x80) { /* PeliCAN Mode */
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. */
445 can_sja_single_filter(&s
->filter
[0],
446 s
->code_mask
+ 0, s
->code_mask
+ 4, 1);
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 */
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);
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);
476 case SJA_CMR
: /* Command register. */
477 if (0x01 & val
) { /* Send transmission request. */
478 buff2frame_pel(s
->tx_buff
, &frame
);
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) {
508 tmp8
= s
->rx_buff
[s
->rxbuf_start
]; count
= 0;
509 if (tmp8
& (1 << 7)) { /* EFF */
513 if (!(tmp8
& (1 << 6))) { /* DATA */
514 count
+= (tmp8
& 0x0f);
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
;
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
);
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
;
545 case 16: /* RX frame information addr16-28. */
546 s
->status_pel
|= (1 << 5); /* Set transmit status. */
549 if (s
->mode
& 0x01) { /* Reset mode */
551 s
->code_mask
[addr
- 16] = val
;
553 } else { /* Operation mode */
554 s
->tx_buff
[addr
- 16] = val
; /* Store to TX buffer directly. */
561 } else { /* Basic Mode */
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);
574 } else if (!(s
->control
& 0x01) && !(val
& 0x01)) {
575 can_sja_software_reset(s
);
578 s
->control
= 0x1f & val
;
580 case SJA_BCAN_CMR
: /* Command register, addr 1 */
581 if (0x01 & val
) { /* Send transmission request. */
582 buff2frame_bas(s
->tx_buff
, &frame
);
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) {
612 tmp8
= s
->rx_buff
[(s
->rxbuf_start
+ 1) % SJA_RCV_BUF_LEN
];
613 count
= 2 + (tmp8
& 0x0f);
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
;
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
);
644 s
->status_bas
|= (1 << 5); /* Set transmit status. */
647 if ((s
->control
& 0x01) == 0) { /* Operation mode */
648 s
->tx_buff
[addr
- 10] = val
; /* Store to TX buffer directly. */
658 uint64_t can_sja_mem_read(CanSJA1000State
*s
, hwaddr addr
, unsigned size
)
662 DPRINTF("read addr 0x%02x ...\n", (unsigned int)addr
);
664 if (addr
> CAN_SJA_MEM_SIZE
) {
668 if (s
->clock
& 0x80) { /* PeliCAN Mode */
670 case SJA_MOD
: /* Mode register, addr 0 */
673 case SJA_CMR
: /* Command register, addr 1 */
674 temp
= 0x00; /* Command register, cannot be read. */
676 case SJA_SR
: /* Status register, addr 2 */
677 temp
= s
->status_pel
;
679 case SJA_IR
: /* Interrupt register, addr 3 */
680 temp
= s
->interrupt_pel
;
681 s
->interrupt_pel
= 0;
683 s
->interrupt_pel
|= (1 << 0); /* Receive interrupt. */
685 can_sja_update_pel_irq(s
);
687 case SJA_IER
: /* Interrupt enable register, addr 4 */
688 temp
= s
->interrupt_en
;
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. */
694 * Output control register, hardware related,
695 * not supported for now.
698 case 10 ... 15: /* Reserved */
703 if (s
->mode
& 0x01) { /* Reset mode */
705 temp
= s
->code_mask
[addr
- 16];
709 } else { /* Operation mode */
710 temp
= s
->rx_buff
[(s
->rxbuf_start
+ addr
- 16) %
720 } else { /* Basic Mode */
722 case SJA_BCAN_CTR
: /* Control register, addr 0 */
725 case SJA_BCAN_SR
: /* Status register, addr 2 */
726 temp
= s
->status_bas
;
728 case SJA_BCAN_IR
: /* Interrupt register, addr 3 */
729 temp
= s
->interrupt_bas
;
730 s
->interrupt_bas
= 0;
732 s
->interrupt_bas
|= (1 << 0); /* Receive interrupt. */
734 can_sja_update_bas_irq(s
);
743 temp
= s
->rx_buff
[(s
->rxbuf_start
+ addr
- 20) % SJA_RCV_BUF_LEN
];
753 DPRINTF("read addr 0x%02x, %d bytes, content 0x%02lx\n",
754 (int)addr
, size
, (long unsigned int)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. */
767 } else { /* BasicCAN mode */
768 if (s
->control
& 0x01) {
773 return true; /* always return true, when operation mode */
776 ssize_t
can_sja_receive(CanBusClientState
*client
, const qemu_can_frame
*frames
,
779 CanSJA1000State
*s
= container_of(client
, CanSJA1000State
, bus_client
);
780 static uint8_t rcv
[SJA_MSG_MAX_LEN
];
783 const qemu_can_frame
*frame
= frames
;
785 if (frames_cnt
<= 0) {
788 if (frame
->flags
& QEMU_CAN_FRMF_TYPE_FD
) {
790 can_display_msg("[cansja]: ignor fd frame ", frame
);
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);
807 qemu_log("[cansja]: filter rejects message\n");
812 ret
= frame2buff_pel(frame
, rcv
);
814 s
->status_pel
&= ~(1 << 4);
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);
826 qemu_log("[cansja]: receive FIFO overrun\n");
828 can_sja_update_pel_irq(s
);
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
);
854 s
->status_bas
&= ~(1 << 4);
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
);
867 qemu_log("[cansja]: receive FIFO overrun\n");
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
);
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
;
905 if (can_bus_insert_client(bus
, &s
->bus_client
) < 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
)
921 qemu_irq_lower(s
->irq
);
923 can_sja_hardware_reset(s
);
928 const VMStateDescription vmstate_qemu_can_filter
= {
929 .name
= "qemu_can_filter",
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
);
945 can_sja_update_bas_irq(s
);
950 /* VMState is needed for live migration of QEMU images */
951 const VMStateDescription vmstate_can_sja
= {
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()