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
27 #include "qemu/osdep.h"
29 #include "chardev/char.h"
31 #include "net/can_emu.h"
33 #include "can_sja1000.h"
36 #define DEBUG_FILTER 0
37 #endif /*DEBUG_FILTER*/
43 #define DPRINTF(fmt, ...) \
46 qemu_log("[cansja]: " fmt , ## __VA_ARGS__); \
50 static void can_sja_software_reset(CanSJA1000State
*s
)
54 s
->status_pel
&= ~0x37;
55 s
->status_pel
|= 0x34;
57 s
->rxbuf_start
= 0x00;
62 void can_sja_hardware_reset(CanSJA1000State
*s
)
64 /* Reset by hardware, p10 */
67 s
->interrupt_pel
= 0x00;
69 s
->rxbuf_start
= 0x00;
75 s
->interrupt_bas
= 0x00;
77 qemu_irq_lower(s
->irq
);
81 void can_sja_single_filter(struct qemu_can_filter
*filter
,
82 const uint8_t *acr
, const uint8_t *amr
, int extended
)
85 filter
->can_id
= (uint32_t)acr
[0] << 21;
86 filter
->can_id
|= (uint32_t)acr
[1] << 13;
87 filter
->can_id
|= (uint32_t)acr
[2] << 5;
88 filter
->can_id
|= (uint32_t)acr
[3] >> 3;
90 filter
->can_id
|= QEMU_CAN_RTR_FLAG
;
93 filter
->can_mask
= (uint32_t)amr
[0] << 21;
94 filter
->can_mask
|= (uint32_t)amr
[1] << 13;
95 filter
->can_mask
|= (uint32_t)amr
[2] << 5;
96 filter
->can_mask
|= (uint32_t)amr
[3] >> 3;
97 filter
->can_mask
= ~filter
->can_mask
& QEMU_CAN_EFF_MASK
;
99 filter
->can_mask
|= QEMU_CAN_RTR_FLAG
;
102 filter
->can_id
= (uint32_t)acr
[0] << 3;
103 filter
->can_id
|= (uint32_t)acr
[1] >> 5;
105 filter
->can_id
|= QEMU_CAN_RTR_FLAG
;
108 filter
->can_mask
= (uint32_t)amr
[0] << 3;
109 filter
->can_mask
|= (uint32_t)amr
[1] << 5;
110 filter
->can_mask
= ~filter
->can_mask
& QEMU_CAN_SFF_MASK
;
111 if (!(amr
[1] & 0x10)) {
112 filter
->can_mask
|= QEMU_CAN_RTR_FLAG
;
118 void can_sja_dual_filter(struct qemu_can_filter
*filter
,
119 const uint8_t *acr
, const uint8_t *amr
, int extended
)
122 filter
->can_id
= (uint32_t)acr
[0] << 21;
123 filter
->can_id
|= (uint32_t)acr
[1] << 13;
125 filter
->can_mask
= (uint32_t)amr
[0] << 21;
126 filter
->can_mask
|= (uint32_t)amr
[1] << 13;
127 filter
->can_mask
= ~filter
->can_mask
& QEMU_CAN_EFF_MASK
& ~0x1fff;
129 filter
->can_id
= (uint32_t)acr
[0] << 3;
130 filter
->can_id
|= (uint32_t)acr
[1] >> 5;
132 filter
->can_id
|= QEMU_CAN_RTR_FLAG
;
135 filter
->can_mask
= (uint32_t)amr
[0] << 3;
136 filter
->can_mask
|= (uint32_t)amr
[1] >> 5;
137 filter
->can_mask
= ~filter
->can_mask
& QEMU_CAN_SFF_MASK
;
138 if (!(amr
[1] & 0x10)) {
139 filter
->can_mask
|= QEMU_CAN_RTR_FLAG
;
144 /* Details in DS-p22, what we need to do here is to test the data. */
146 int can_sja_accept_filter(CanSJA1000State
*s
,
147 const qemu_can_frame
*frame
)
150 struct qemu_can_filter filter
;
152 if (s
->clock
& 0x80) { /* PeliCAN Mode */
153 if (s
->mode
& (1 << 3)) { /* Single mode. */
154 if (frame
->can_id
& QEMU_CAN_EFF_FLAG
) { /* EFF */
155 can_sja_single_filter(&filter
,
156 s
->code_mask
+ 0, s
->code_mask
+ 4, 1);
158 if (!can_bus_filter_match(&filter
, frame
->can_id
)) {
162 can_sja_single_filter(&filter
,
163 s
->code_mask
+ 0, s
->code_mask
+ 4, 0);
165 if (!can_bus_filter_match(&filter
, frame
->can_id
)) {
169 if (frame
->can_id
& QEMU_CAN_RTR_FLAG
) { /* RTR */
173 if (frame
->can_dlc
== 0) {
177 if ((frame
->data
[0] & ~(s
->code_mask
[6])) !=
178 (s
->code_mask
[2] & ~(s
->code_mask
[6]))) {
182 if (frame
->can_dlc
< 2) {
186 if ((frame
->data
[1] & ~(s
->code_mask
[7])) ==
187 (s
->code_mask
[3] & ~(s
->code_mask
[7]))) {
193 } else { /* Dual mode */
194 if (frame
->can_id
& QEMU_CAN_EFF_FLAG
) { /* EFF */
195 can_sja_dual_filter(&filter
,
196 s
->code_mask
+ 0, s
->code_mask
+ 4, 1);
198 if (can_bus_filter_match(&filter
, frame
->can_id
)) {
202 can_sja_dual_filter(&filter
,
203 s
->code_mask
+ 2, s
->code_mask
+ 6, 1);
205 if (can_bus_filter_match(&filter
, frame
->can_id
)) {
211 can_sja_dual_filter(&filter
,
212 s
->code_mask
+ 0, s
->code_mask
+ 4, 0);
214 if (can_bus_filter_match(&filter
, frame
->can_id
)) {
217 expect
= s
->code_mask
[1] << 4;
218 expect
|= s
->code_mask
[3] & 0x0f;
220 mask
= s
->code_mask
[5] << 4;
221 mask
|= s
->code_mask
[7] & 0x0f;
224 if ((frame
->data
[0] & mask
) ==
230 can_sja_dual_filter(&filter
,
231 s
->code_mask
+ 2, s
->code_mask
+ 6, 0);
233 if (can_bus_filter_match(&filter
, frame
->can_id
)) {
245 static void can_display_msg(const char *prefix
, const qemu_can_frame
*msg
)
250 qemu_log("%s%03X [%01d] %s %s",
252 msg
->can_id
& QEMU_CAN_EFF_MASK
,
254 msg
->can_id
& QEMU_CAN_EFF_FLAG
? "EFF" : "SFF",
255 msg
->can_id
& QEMU_CAN_RTR_FLAG
? "RTR" : "DAT");
257 for (i
= 0; i
< msg
->can_dlc
; i
++) {
258 qemu_log(" %02X", msg
->data
[i
]);
265 static void buff2frame_pel(const uint8_t *buff
, qemu_can_frame
*frame
)
270 if (buff
[0] & 0x40) { /* RTR */
271 frame
->can_id
= QEMU_CAN_RTR_FLAG
;
273 frame
->can_dlc
= buff
[0] & 0x0f;
275 if (buff
[0] & 0x80) { /* Extended */
276 frame
->can_id
|= QEMU_CAN_EFF_FLAG
;
277 frame
->can_id
|= buff
[1] << 21; /* ID.28~ID.21 */
278 frame
->can_id
|= buff
[2] << 13; /* ID.20~ID.13 */
279 frame
->can_id
|= buff
[3] << 5;
280 frame
->can_id
|= buff
[4] >> 3;
281 for (i
= 0; i
< frame
->can_dlc
; i
++) {
282 frame
->data
[i
] = buff
[5 + i
];
288 frame
->can_id
|= buff
[1] << 3;
289 frame
->can_id
|= buff
[2] >> 5;
290 for (i
= 0; i
< frame
->can_dlc
; i
++) {
291 frame
->data
[i
] = buff
[3 + i
];
300 static void buff2frame_bas(const uint8_t *buff
, qemu_can_frame
*frame
)
304 frame
->can_id
= ((buff
[0] << 3) & (0xff << 3)) + ((buff
[1] >> 5) & 0x07);
305 if (buff
[1] & 0x10) { /* RTR */
306 frame
->can_id
= QEMU_CAN_RTR_FLAG
;
308 frame
->can_dlc
= buff
[1] & 0x0f;
310 for (i
= 0; i
< frame
->can_dlc
; i
++) {
311 frame
->data
[i
] = buff
[2 + i
];
319 static int frame2buff_pel(const qemu_can_frame
*frame
, uint8_t *buff
)
323 if (frame
->can_id
& QEMU_CAN_ERR_FLAG
) { /* error frame, NOT support now. */
327 buff
[0] = 0x0f & frame
->can_dlc
; /* DLC */
328 if (frame
->can_id
& QEMU_CAN_RTR_FLAG
) { /* RTR */
331 if (frame
->can_id
& QEMU_CAN_EFF_FLAG
) { /* EFF */
333 buff
[1] = extract32(frame
->can_id
, 21, 8); /* ID.28~ID.21 */
334 buff
[2] = extract32(frame
->can_id
, 13, 8); /* ID.20~ID.13 */
335 buff
[3] = extract32(frame
->can_id
, 5, 8); /* ID.12~ID.05 */
336 buff
[4] = extract32(frame
->can_id
, 0, 5) << 3; /* ID.04~ID.00,xxx */
337 for (i
= 0; i
< frame
->can_dlc
; i
++) {
338 buff
[5 + i
] = frame
->data
[i
];
340 return frame
->can_dlc
+ 5;
342 buff
[1] = extract32(frame
->can_id
, 3, 8); /* ID.10~ID.03 */
343 buff
[2] = extract32(frame
->can_id
, 0, 3) << 5; /* ID.02~ID.00,xxxxx */
344 for (i
= 0; i
< frame
->can_dlc
; i
++) {
345 buff
[3 + i
] = frame
->data
[i
];
348 return frame
->can_dlc
+ 3;
354 static int frame2buff_bas(const qemu_can_frame
*frame
, uint8_t *buff
)
359 * EFF, no support for BasicMode
360 * No use for Error frames now,
361 * they could be used in future to update SJA1000 error state
363 if ((frame
->can_id
& QEMU_CAN_EFF_FLAG
) ||
364 (frame
->can_id
& QEMU_CAN_ERR_FLAG
)) {
368 buff
[0] = extract32(frame
->can_id
, 3, 8); /* ID.10~ID.03 */
369 buff
[1] = extract32(frame
->can_id
, 0, 3) << 5; /* ID.02~ID.00,xxxxx */
370 if (frame
->can_id
& QEMU_CAN_RTR_FLAG
) { /* RTR */
373 buff
[1] |= frame
->can_dlc
& 0x0f;
374 for (i
= 0; i
< frame
->can_dlc
; i
++) {
375 buff
[2 + i
] = frame
->data
[i
];
378 return frame
->can_dlc
+ 2;
381 static void can_sja_update_pel_irq(CanSJA1000State
*s
)
383 if (s
->interrupt_en
& s
->interrupt_pel
) {
384 qemu_irq_raise(s
->irq
);
386 qemu_irq_lower(s
->irq
);
390 static void can_sja_update_bas_irq(CanSJA1000State
*s
)
392 if ((s
->control
>> 1) & s
->interrupt_bas
) {
393 qemu_irq_raise(s
->irq
);
395 qemu_irq_lower(s
->irq
);
399 void can_sja_mem_write(CanSJA1000State
*s
, hwaddr addr
, uint64_t val
,
402 qemu_can_frame frame
;
407 DPRINTF("write 0x%02llx addr 0x%02x\n",
408 (unsigned long long)val
, (unsigned int)addr
);
410 if (addr
> CAN_SJA_MEM_SIZE
) {
414 if (s
->clock
& 0x80) { /* PeliCAN Mode */
416 case SJA_MOD
: /* Mode register */
417 s
->mode
= 0x1f & val
;
418 if ((s
->mode
& 0x01) && ((val
& 0x01) == 0)) {
419 /* Go to operation mode from reset mode. */
420 if (s
->mode
& (1 << 3)) { /* Single mode. */
422 can_sja_single_filter(&s
->filter
[0],
423 s
->code_mask
+ 0, s
->code_mask
+ 4, 1);
426 can_sja_single_filter(&s
->filter
[1],
427 s
->code_mask
+ 0, s
->code_mask
+ 4, 0);
429 can_bus_client_set_filters(&s
->bus_client
, s
->filter
, 2);
430 } else { /* Dual mode */
432 can_sja_dual_filter(&s
->filter
[0],
433 s
->code_mask
+ 0, s
->code_mask
+ 4, 1);
435 can_sja_dual_filter(&s
->filter
[1],
436 s
->code_mask
+ 2, s
->code_mask
+ 6, 1);
439 can_sja_dual_filter(&s
->filter
[2],
440 s
->code_mask
+ 0, s
->code_mask
+ 4, 0);
442 can_sja_dual_filter(&s
->filter
[3],
443 s
->code_mask
+ 2, s
->code_mask
+ 6, 0);
445 can_bus_client_set_filters(&s
->bus_client
, s
->filter
, 4);
453 case SJA_CMR
: /* Command register. */
454 if (0x01 & val
) { /* Send transmission request. */
455 buff2frame_pel(s
->tx_buff
, &frame
);
457 can_display_msg("[cansja]: Tx request " , &frame
);
461 * Clear transmission complete status,
462 * and Transmit Buffer Status.
463 * write to the backends.
465 s
->status_pel
&= ~(3 << 2);
467 can_bus_client_send(&s
->bus_client
, &frame
, 1);
470 * Set transmission complete status
471 * and Transmit Buffer Status.
473 s
->status_pel
|= (3 << 2);
475 /* Clear transmit status. */
476 s
->status_pel
&= ~(1 << 5);
477 s
->interrupt_pel
|= 0x02;
478 can_sja_update_pel_irq(s
);
480 if (0x04 & val
) { /* Release Receive Buffer */
481 if (s
->rxmsg_cnt
<= 0) {
485 tmp8
= s
->rx_buff
[s
->rxbuf_start
]; count
= 0;
486 if (tmp8
& (1 << 7)) { /* EFF */
490 if (!(tmp8
& (1 << 6))) { /* DATA */
491 count
+= (tmp8
& 0x0f);
495 qemu_log("[cansja]: message released from "
496 "Rx FIFO cnt=%d, count=%d\n", s
->rx_cnt
, count
);
499 s
->rxbuf_start
+= count
;
500 s
->rxbuf_start
%= SJA_RCV_BUF_LEN
;
504 if (s
->rxmsg_cnt
== 0) {
505 s
->status_pel
&= ~(1 << 0);
506 s
->interrupt_pel
&= ~(1 << 0);
507 can_sja_update_pel_irq(s
);
510 if (0x08 & val
) { /* Clear data overrun */
511 s
->status_pel
&= ~(1 << 1);
512 s
->interrupt_pel
&= ~(1 << 3);
513 can_sja_update_pel_irq(s
);
516 case SJA_SR
: /* Status register */
517 case SJA_IR
: /* Interrupt register */
518 break; /* Do nothing */
519 case SJA_IER
: /* Interrupt enable register */
520 s
->interrupt_en
= val
;
522 case 16: /* RX frame information addr16-28. */
523 s
->status_pel
|= (1 << 5); /* Set transmit status. */
525 if (s
->mode
& 0x01) { /* Reset mode */
527 s
->code_mask
[addr
- 16] = val
;
529 } else { /* Operation mode */
530 s
->tx_buff
[addr
- 16] = val
; /* Store to TX buffer directly. */
537 } else { /* Basic Mode */
539 case SJA_BCAN_CTR
: /* Control register, addr 0 */
540 if ((s
->control
& 0x01) && ((val
& 0x01) == 0)) {
541 /* Go to operation mode from reset mode. */
542 s
->filter
[0].can_id
= (s
->code
<< 3) & (0xff << 3);
543 tmp
= (~(s
->mask
<< 3)) & (0xff << 3);
544 tmp
|= QEMU_CAN_EFF_FLAG
; /* Only Basic CAN Frame. */
545 s
->filter
[0].can_mask
= tmp
;
546 can_bus_client_set_filters(&s
->bus_client
, s
->filter
, 1);
550 } else if (!(s
->control
& 0x01) && !(val
& 0x01)) {
551 can_sja_software_reset(s
);
554 s
->control
= 0x1f & val
;
556 case SJA_BCAN_CMR
: /* Command register, addr 1 */
557 if (0x01 & val
) { /* Send transmission request. */
558 buff2frame_bas(s
->tx_buff
, &frame
);
560 can_display_msg("[cansja]: Tx request " , &frame
);
564 * Clear transmission complete status,
565 * and Transmit Buffer Status.
567 s
->status_bas
&= ~(3 << 2);
569 /* write to the backends. */
570 can_bus_client_send(&s
->bus_client
, &frame
, 1);
573 * Set transmission complete status,
574 * and Transmit Buffer Status.
576 s
->status_bas
|= (3 << 2);
578 /* Clear transmit status. */
579 s
->status_bas
&= ~(1 << 5);
580 s
->interrupt_bas
|= 0x02;
581 can_sja_update_bas_irq(s
);
583 if (0x04 & val
) { /* Release Receive Buffer */
584 if (s
->rxmsg_cnt
<= 0) {
588 tmp8
= s
->rx_buff
[(s
->rxbuf_start
+ 1) % SJA_RCV_BUF_LEN
];
589 count
= 2 + (tmp8
& 0x0f);
592 qemu_log("[cansja]: message released from "
593 "Rx FIFO cnt=%d, count=%d\n", s
->rx_cnt
, count
);
596 s
->rxbuf_start
+= count
;
597 s
->rxbuf_start
%= SJA_RCV_BUF_LEN
;
601 if (s
->rxmsg_cnt
== 0) {
602 s
->status_bas
&= ~(1 << 0);
603 s
->interrupt_bas
&= ~(1 << 0);
604 can_sja_update_bas_irq(s
);
607 if (0x08 & val
) { /* Clear data overrun */
608 s
->status_bas
&= ~(1 << 1);
609 s
->interrupt_bas
&= ~(1 << 3);
610 can_sja_update_bas_irq(s
);
620 s
->status_bas
|= (1 << 5); /* Set transmit status. */
622 if ((s
->control
& 0x01) == 0) { /* Operation mode */
623 s
->tx_buff
[addr
- 10] = val
; /* Store to TX buffer directly. */
633 uint64_t can_sja_mem_read(CanSJA1000State
*s
, hwaddr addr
, unsigned size
)
637 DPRINTF("read addr 0x%02x ...\n", (unsigned int)addr
);
639 if (addr
> CAN_SJA_MEM_SIZE
) {
643 if (s
->clock
& 0x80) { /* PeliCAN Mode */
645 case SJA_MOD
: /* Mode register, addr 0 */
648 case SJA_CMR
: /* Command register, addr 1 */
649 temp
= 0x00; /* Command register, cannot be read. */
651 case SJA_SR
: /* Status register, addr 2 */
652 temp
= s
->status_pel
;
654 case SJA_IR
: /* Interrupt register, addr 3 */
655 temp
= s
->interrupt_pel
;
656 s
->interrupt_pel
= 0;
658 s
->interrupt_pel
|= (1 << 0); /* Receive interrupt. */
660 can_sja_update_pel_irq(s
);
662 case SJA_IER
: /* Interrupt enable register, addr 4 */
663 temp
= s
->interrupt_en
;
665 case 5: /* Reserved */
666 case 6: /* Bus timing 0, hardware related, not support now. */
667 case 7: /* Bus timing 1, hardware related, not support now. */
669 * Output control register, hardware related,
670 * not supported for now.
673 case 10 ... 15: /* Reserved */
678 if (s
->mode
& 0x01) { /* Reset mode */
680 temp
= s
->code_mask
[addr
- 16];
684 } else { /* Operation mode */
685 temp
= s
->rx_buff
[(s
->rxbuf_start
+ addr
- 16) %
695 } else { /* Basic Mode */
697 case SJA_BCAN_CTR
: /* Control register, addr 0 */
700 case SJA_BCAN_SR
: /* Status register, addr 2 */
701 temp
= s
->status_bas
;
703 case SJA_BCAN_IR
: /* Interrupt register, addr 3 */
704 temp
= s
->interrupt_bas
;
705 s
->interrupt_bas
= 0;
707 s
->interrupt_bas
|= (1 << 0); /* Receive interrupt. */
709 can_sja_update_bas_irq(s
);
718 temp
= s
->rx_buff
[(s
->rxbuf_start
+ addr
- 20) % SJA_RCV_BUF_LEN
];
728 DPRINTF("read addr 0x%02x, %d bytes, content 0x%02lx\n",
729 (int)addr
, size
, (long unsigned int)temp
);
734 int can_sja_can_receive(CanBusClientState
*client
)
736 CanSJA1000State
*s
= container_of(client
, CanSJA1000State
, bus_client
);
738 if (s
->clock
& 0x80) { /* PeliCAN Mode */
739 if (s
->mode
& 0x01) { /* reset mode. */
742 } else { /* BasicCAN mode */
743 if (s
->control
& 0x01) {
748 return 1; /* always return 1, when operation mode */
751 ssize_t
can_sja_receive(CanBusClientState
*client
, const qemu_can_frame
*frames
,
754 CanSJA1000State
*s
= container_of(client
, CanSJA1000State
, bus_client
);
755 static uint8_t rcv
[SJA_MSG_MAX_LEN
];
758 const qemu_can_frame
*frame
= frames
;
760 if (frames_cnt
<= 0) {
764 can_display_msg("[cansja]: receive ", frame
);
767 if (s
->clock
& 0x80) { /* PeliCAN Mode */
769 /* the CAN controller is receiving a message */
770 s
->status_pel
|= (1 << 4);
772 if (can_sja_accept_filter(s
, frame
) == 0) {
773 s
->status_pel
&= ~(1 << 4);
775 qemu_log("[cansja]: filter rejects message\n");
780 ret
= frame2buff_pel(frame
, rcv
);
782 s
->status_pel
&= ~(1 << 4);
784 qemu_log("[cansja]: message store failed\n");
786 return ret
; /* maybe not support now. */
789 if (s
->rx_cnt
+ ret
> SJA_RCV_BUF_LEN
) { /* Data overrun. */
790 s
->status_pel
|= (1 << 1); /* Overrun status */
791 s
->interrupt_pel
|= (1 << 3);
792 s
->status_pel
&= ~(1 << 4);
794 qemu_log("[cansja]: receive FIFO overrun\n");
796 can_sja_update_pel_irq(s
);
802 qemu_log("[cansja]: message stored in receive FIFO\n");
805 for (i
= 0; i
< ret
; i
++) {
806 s
->rx_buff
[(s
->rx_ptr
++) % SJA_RCV_BUF_LEN
] = rcv
[i
];
808 s
->rx_ptr
%= SJA_RCV_BUF_LEN
; /* update the pointer. */
810 s
->status_pel
|= 0x01; /* Set the Receive Buffer Status. DS-p23 */
811 s
->interrupt_pel
|= 0x01;
812 s
->status_pel
&= ~(1 << 4);
813 s
->status_pel
|= (1 << 0);
814 can_sja_update_pel_irq(s
);
815 } else { /* BasicCAN mode */
817 /* the CAN controller is receiving a message */
818 s
->status_bas
|= (1 << 4);
820 ret
= frame2buff_bas(frame
, rcv
);
822 s
->status_bas
&= ~(1 << 4);
824 qemu_log("[cansja]: message store failed\n");
826 return ret
; /* maybe not support now. */
829 if (s
->rx_cnt
+ ret
> SJA_RCV_BUF_LEN
) { /* Data overrun. */
830 s
->status_bas
|= (1 << 1); /* Overrun status */
831 s
->status_bas
&= ~(1 << 4);
832 s
->interrupt_bas
|= (1 << 3);
833 can_sja_update_bas_irq(s
);
835 qemu_log("[cansja]: receive FIFO overrun\n");
843 qemu_log("[cansja]: message stored\n");
846 for (i
= 0; i
< ret
; i
++) {
847 s
->rx_buff
[(s
->rx_ptr
++) % SJA_RCV_BUF_LEN
] = rcv
[i
];
849 s
->rx_ptr
%= SJA_RCV_BUF_LEN
; /* update the pointer. */
851 s
->status_bas
|= 0x01; /* Set the Receive Buffer Status. DS-p15 */
852 s
->status_bas
&= ~(1 << 4);
853 s
->interrupt_bas
|= (1 << 0);
854 can_sja_update_bas_irq(s
);
859 static CanBusClientInfo can_sja_bus_client_info
= {
860 .can_receive
= can_sja_can_receive
,
861 .receive
= can_sja_receive
,
865 int can_sja_connect_to_bus(CanSJA1000State
*s
, CanBusState
*bus
)
867 s
->bus_client
.info
= &can_sja_bus_client_info
;
873 if (can_bus_insert_client(bus
, &s
->bus_client
) < 0) {
880 void can_sja_disconnect(CanSJA1000State
*s
)
882 can_bus_remove_client(&s
->bus_client
);
885 int can_sja_init(CanSJA1000State
*s
, qemu_irq irq
)
889 qemu_irq_lower(s
->irq
);
891 can_sja_hardware_reset(s
);
896 const VMStateDescription vmstate_qemu_can_filter
= {
897 .name
= "qemu_can_filter",
899 .minimum_version_id
= 1,
900 .minimum_version_id_old
= 1,
901 .fields
= (VMStateField
[]) {
902 VMSTATE_UINT32(can_id
, qemu_can_filter
),
903 VMSTATE_UINT32(can_mask
, qemu_can_filter
),
904 VMSTATE_END_OF_LIST()
908 static int can_sja_post_load(void *opaque
, int version_id
)
910 CanSJA1000State
*s
= opaque
;
911 if (s
->clock
& 0x80) { /* PeliCAN Mode */
912 can_sja_update_pel_irq(s
);
914 can_sja_update_bas_irq(s
);
919 /* VMState is needed for live migration of QEMU images */
920 const VMStateDescription vmstate_can_sja
= {
923 .minimum_version_id
= 1,
924 .minimum_version_id_old
= 1,
925 .post_load
= can_sja_post_load
,
926 .fields
= (VMStateField
[]) {
927 VMSTATE_UINT8(mode
, CanSJA1000State
),
929 VMSTATE_UINT8(status_pel
, CanSJA1000State
),
930 VMSTATE_UINT8(interrupt_pel
, CanSJA1000State
),
931 VMSTATE_UINT8(interrupt_en
, CanSJA1000State
),
932 VMSTATE_UINT8(rxmsg_cnt
, CanSJA1000State
),
933 VMSTATE_UINT8(rxbuf_start
, CanSJA1000State
),
934 VMSTATE_UINT8(clock
, CanSJA1000State
),
936 VMSTATE_BUFFER(code_mask
, CanSJA1000State
),
937 VMSTATE_BUFFER(tx_buff
, CanSJA1000State
),
939 VMSTATE_BUFFER(rx_buff
, CanSJA1000State
),
941 VMSTATE_UINT32(rx_ptr
, CanSJA1000State
),
942 VMSTATE_UINT32(rx_cnt
, CanSJA1000State
),
944 VMSTATE_UINT8(control
, CanSJA1000State
),
946 VMSTATE_UINT8(status_bas
, CanSJA1000State
),
947 VMSTATE_UINT8(interrupt_bas
, CanSJA1000State
),
948 VMSTATE_UINT8(code
, CanSJA1000State
),
949 VMSTATE_UINT8(mask
, CanSJA1000State
),
951 VMSTATE_STRUCT_ARRAY(filter
, CanSJA1000State
, 4, 0,
952 vmstate_qemu_can_filter
, qemu_can_filter
),
955 VMSTATE_END_OF_LIST()