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
)
252 qemu_log("%s%03X [%01d] %s %s",
254 msg
->can_id
& QEMU_CAN_EFF_MASK
,
256 msg
->can_id
& QEMU_CAN_EFF_FLAG
? "EFF" : "SFF",
257 msg
->can_id
& QEMU_CAN_RTR_FLAG
? "RTR" : "DAT");
259 for (i
= 0; i
< msg
->can_dlc
; i
++) {
260 qemu_log(" %02X", msg
->data
[i
]);
267 static void buff2frame_pel(const uint8_t *buff
, qemu_can_frame
*frame
)
272 if (buff
[0] & 0x40) { /* RTR */
273 frame
->can_id
= QEMU_CAN_RTR_FLAG
;
275 frame
->can_dlc
= buff
[0] & 0x0f;
277 if (buff
[0] & 0x80) { /* Extended */
278 frame
->can_id
|= QEMU_CAN_EFF_FLAG
;
279 frame
->can_id
|= buff
[1] << 21; /* ID.28~ID.21 */
280 frame
->can_id
|= buff
[2] << 13; /* ID.20~ID.13 */
281 frame
->can_id
|= buff
[3] << 5;
282 frame
->can_id
|= buff
[4] >> 3;
283 for (i
= 0; i
< frame
->can_dlc
; i
++) {
284 frame
->data
[i
] = buff
[5 + i
];
290 frame
->can_id
|= buff
[1] << 3;
291 frame
->can_id
|= buff
[2] >> 5;
292 for (i
= 0; i
< frame
->can_dlc
; i
++) {
293 frame
->data
[i
] = buff
[3 + i
];
302 static void buff2frame_bas(const uint8_t *buff
, qemu_can_frame
*frame
)
306 frame
->can_id
= ((buff
[0] << 3) & (0xff << 3)) + ((buff
[1] >> 5) & 0x07);
307 if (buff
[1] & 0x10) { /* RTR */
308 frame
->can_id
= QEMU_CAN_RTR_FLAG
;
310 frame
->can_dlc
= buff
[1] & 0x0f;
312 for (i
= 0; i
< frame
->can_dlc
; i
++) {
313 frame
->data
[i
] = buff
[2 + i
];
321 static int frame2buff_pel(const qemu_can_frame
*frame
, uint8_t *buff
)
325 if (frame
->can_id
& QEMU_CAN_ERR_FLAG
) { /* error frame, NOT support now. */
329 buff
[0] = 0x0f & frame
->can_dlc
; /* DLC */
330 if (frame
->can_id
& QEMU_CAN_RTR_FLAG
) { /* RTR */
333 if (frame
->can_id
& QEMU_CAN_EFF_FLAG
) { /* EFF */
335 buff
[1] = extract32(frame
->can_id
, 21, 8); /* ID.28~ID.21 */
336 buff
[2] = extract32(frame
->can_id
, 13, 8); /* ID.20~ID.13 */
337 buff
[3] = extract32(frame
->can_id
, 5, 8); /* ID.12~ID.05 */
338 buff
[4] = extract32(frame
->can_id
, 0, 5) << 3; /* ID.04~ID.00,xxx */
339 for (i
= 0; i
< frame
->can_dlc
; i
++) {
340 buff
[5 + i
] = frame
->data
[i
];
342 return frame
->can_dlc
+ 5;
344 buff
[1] = extract32(frame
->can_id
, 3, 8); /* ID.10~ID.03 */
345 buff
[2] = extract32(frame
->can_id
, 0, 3) << 5; /* ID.02~ID.00,xxxxx */
346 for (i
= 0; i
< frame
->can_dlc
; i
++) {
347 buff
[3 + i
] = frame
->data
[i
];
350 return frame
->can_dlc
+ 3;
356 static int frame2buff_bas(const qemu_can_frame
*frame
, uint8_t *buff
)
361 * EFF, no support for BasicMode
362 * No use for Error frames now,
363 * they could be used in future to update SJA1000 error state
365 if ((frame
->can_id
& QEMU_CAN_EFF_FLAG
) ||
366 (frame
->can_id
& QEMU_CAN_ERR_FLAG
)) {
370 buff
[0] = extract32(frame
->can_id
, 3, 8); /* ID.10~ID.03 */
371 buff
[1] = extract32(frame
->can_id
, 0, 3) << 5; /* ID.02~ID.00,xxxxx */
372 if (frame
->can_id
& QEMU_CAN_RTR_FLAG
) { /* RTR */
375 buff
[1] |= frame
->can_dlc
& 0x0f;
376 for (i
= 0; i
< frame
->can_dlc
; i
++) {
377 buff
[2 + i
] = frame
->data
[i
];
380 return frame
->can_dlc
+ 2;
383 static void can_sja_update_pel_irq(CanSJA1000State
*s
)
385 if (s
->interrupt_en
& s
->interrupt_pel
) {
386 qemu_irq_raise(s
->irq
);
388 qemu_irq_lower(s
->irq
);
392 static void can_sja_update_bas_irq(CanSJA1000State
*s
)
394 if ((s
->control
>> 1) & s
->interrupt_bas
) {
395 qemu_irq_raise(s
->irq
);
397 qemu_irq_lower(s
->irq
);
401 void can_sja_mem_write(CanSJA1000State
*s
, hwaddr addr
, uint64_t val
,
404 qemu_can_frame frame
;
409 DPRINTF("write 0x%02llx addr 0x%02x\n",
410 (unsigned long long)val
, (unsigned int)addr
);
412 if (addr
> CAN_SJA_MEM_SIZE
) {
416 if (s
->clock
& 0x80) { /* PeliCAN Mode */
418 case SJA_MOD
: /* Mode register */
419 s
->mode
= 0x1f & val
;
420 if ((s
->mode
& 0x01) && ((val
& 0x01) == 0)) {
421 /* Go to operation mode from reset mode. */
422 if (s
->mode
& (1 << 3)) { /* Single mode. */
424 can_sja_single_filter(&s
->filter
[0],
425 s
->code_mask
+ 0, s
->code_mask
+ 4, 1);
428 can_sja_single_filter(&s
->filter
[1],
429 s
->code_mask
+ 0, s
->code_mask
+ 4, 0);
431 can_bus_client_set_filters(&s
->bus_client
, s
->filter
, 2);
432 } else { /* Dual mode */
434 can_sja_dual_filter(&s
->filter
[0],
435 s
->code_mask
+ 0, s
->code_mask
+ 4, 1);
437 can_sja_dual_filter(&s
->filter
[1],
438 s
->code_mask
+ 2, s
->code_mask
+ 6, 1);
441 can_sja_dual_filter(&s
->filter
[2],
442 s
->code_mask
+ 0, s
->code_mask
+ 4, 0);
444 can_sja_dual_filter(&s
->filter
[3],
445 s
->code_mask
+ 2, s
->code_mask
+ 6, 0);
447 can_bus_client_set_filters(&s
->bus_client
, s
->filter
, 4);
455 case SJA_CMR
: /* Command register. */
456 if (0x01 & val
) { /* Send transmission request. */
457 buff2frame_pel(s
->tx_buff
, &frame
);
459 can_display_msg("[cansja]: Tx request " , &frame
);
463 * Clear transmission complete status,
464 * and Transmit Buffer Status.
465 * write to the backends.
467 s
->status_pel
&= ~(3 << 2);
469 can_bus_client_send(&s
->bus_client
, &frame
, 1);
472 * Set transmission complete status
473 * and Transmit Buffer Status.
475 s
->status_pel
|= (3 << 2);
477 /* Clear transmit status. */
478 s
->status_pel
&= ~(1 << 5);
479 s
->interrupt_pel
|= 0x02;
480 can_sja_update_pel_irq(s
);
482 if (0x04 & val
) { /* Release Receive Buffer */
483 if (s
->rxmsg_cnt
<= 0) {
487 tmp8
= s
->rx_buff
[s
->rxbuf_start
]; count
= 0;
488 if (tmp8
& (1 << 7)) { /* EFF */
492 if (!(tmp8
& (1 << 6))) { /* DATA */
493 count
+= (tmp8
& 0x0f);
497 qemu_log("[cansja]: message released from "
498 "Rx FIFO cnt=%d, count=%d\n", s
->rx_cnt
, count
);
501 s
->rxbuf_start
+= count
;
502 s
->rxbuf_start
%= SJA_RCV_BUF_LEN
;
506 if (s
->rxmsg_cnt
== 0) {
507 s
->status_pel
&= ~(1 << 0);
508 s
->interrupt_pel
&= ~(1 << 0);
509 can_sja_update_pel_irq(s
);
512 if (0x08 & val
) { /* Clear data overrun */
513 s
->status_pel
&= ~(1 << 1);
514 s
->interrupt_pel
&= ~(1 << 3);
515 can_sja_update_pel_irq(s
);
518 case SJA_SR
: /* Status register */
519 case SJA_IR
: /* Interrupt register */
520 break; /* Do nothing */
521 case SJA_IER
: /* Interrupt enable register */
522 s
->interrupt_en
= val
;
524 case 16: /* RX frame information addr16-28. */
525 s
->status_pel
|= (1 << 5); /* Set transmit status. */
527 if (s
->mode
& 0x01) { /* Reset mode */
529 s
->code_mask
[addr
- 16] = val
;
531 } else { /* Operation mode */
532 s
->tx_buff
[addr
- 16] = val
; /* Store to TX buffer directly. */
539 } else { /* Basic Mode */
541 case SJA_BCAN_CTR
: /* Control register, addr 0 */
542 if ((s
->control
& 0x01) && ((val
& 0x01) == 0)) {
543 /* Go to operation mode from reset mode. */
544 s
->filter
[0].can_id
= (s
->code
<< 3) & (0xff << 3);
545 tmp
= (~(s
->mask
<< 3)) & (0xff << 3);
546 tmp
|= QEMU_CAN_EFF_FLAG
; /* Only Basic CAN Frame. */
547 s
->filter
[0].can_mask
= tmp
;
548 can_bus_client_set_filters(&s
->bus_client
, s
->filter
, 1);
552 } else if (!(s
->control
& 0x01) && !(val
& 0x01)) {
553 can_sja_software_reset(s
);
556 s
->control
= 0x1f & val
;
558 case SJA_BCAN_CMR
: /* Command register, addr 1 */
559 if (0x01 & val
) { /* Send transmission request. */
560 buff2frame_bas(s
->tx_buff
, &frame
);
562 can_display_msg("[cansja]: Tx request " , &frame
);
566 * Clear transmission complete status,
567 * and Transmit Buffer Status.
569 s
->status_bas
&= ~(3 << 2);
571 /* write to the backends. */
572 can_bus_client_send(&s
->bus_client
, &frame
, 1);
575 * Set transmission complete status,
576 * and Transmit Buffer Status.
578 s
->status_bas
|= (3 << 2);
580 /* Clear transmit status. */
581 s
->status_bas
&= ~(1 << 5);
582 s
->interrupt_bas
|= 0x02;
583 can_sja_update_bas_irq(s
);
585 if (0x04 & val
) { /* Release Receive Buffer */
586 if (s
->rxmsg_cnt
<= 0) {
590 tmp8
= s
->rx_buff
[(s
->rxbuf_start
+ 1) % SJA_RCV_BUF_LEN
];
591 count
= 2 + (tmp8
& 0x0f);
594 qemu_log("[cansja]: message released from "
595 "Rx FIFO cnt=%d, count=%d\n", s
->rx_cnt
, count
);
598 s
->rxbuf_start
+= count
;
599 s
->rxbuf_start
%= SJA_RCV_BUF_LEN
;
603 if (s
->rxmsg_cnt
== 0) {
604 s
->status_bas
&= ~(1 << 0);
605 s
->interrupt_bas
&= ~(1 << 0);
606 can_sja_update_bas_irq(s
);
609 if (0x08 & val
) { /* Clear data overrun */
610 s
->status_bas
&= ~(1 << 1);
611 s
->interrupt_bas
&= ~(1 << 3);
612 can_sja_update_bas_irq(s
);
622 s
->status_bas
|= (1 << 5); /* Set transmit status. */
624 if ((s
->control
& 0x01) == 0) { /* Operation mode */
625 s
->tx_buff
[addr
- 10] = val
; /* Store to TX buffer directly. */
635 uint64_t can_sja_mem_read(CanSJA1000State
*s
, hwaddr addr
, unsigned size
)
639 DPRINTF("read addr 0x%02x ...\n", (unsigned int)addr
);
641 if (addr
> CAN_SJA_MEM_SIZE
) {
645 if (s
->clock
& 0x80) { /* PeliCAN Mode */
647 case SJA_MOD
: /* Mode register, addr 0 */
650 case SJA_CMR
: /* Command register, addr 1 */
651 temp
= 0x00; /* Command register, cannot be read. */
653 case SJA_SR
: /* Status register, addr 2 */
654 temp
= s
->status_pel
;
656 case SJA_IR
: /* Interrupt register, addr 3 */
657 temp
= s
->interrupt_pel
;
658 s
->interrupt_pel
= 0;
660 s
->interrupt_pel
|= (1 << 0); /* Receive interrupt. */
662 can_sja_update_pel_irq(s
);
664 case SJA_IER
: /* Interrupt enable register, addr 4 */
665 temp
= s
->interrupt_en
;
667 case 5: /* Reserved */
668 case 6: /* Bus timing 0, hardware related, not support now. */
669 case 7: /* Bus timing 1, hardware related, not support now. */
671 * Output control register, hardware related,
672 * not supported for now.
675 case 10 ... 15: /* Reserved */
680 if (s
->mode
& 0x01) { /* Reset mode */
682 temp
= s
->code_mask
[addr
- 16];
686 } else { /* Operation mode */
687 temp
= s
->rx_buff
[(s
->rxbuf_start
+ addr
- 16) %
697 } else { /* Basic Mode */
699 case SJA_BCAN_CTR
: /* Control register, addr 0 */
702 case SJA_BCAN_SR
: /* Status register, addr 2 */
703 temp
= s
->status_bas
;
705 case SJA_BCAN_IR
: /* Interrupt register, addr 3 */
706 temp
= s
->interrupt_bas
;
707 s
->interrupt_bas
= 0;
709 s
->interrupt_bas
|= (1 << 0); /* Receive interrupt. */
711 can_sja_update_bas_irq(s
);
720 temp
= s
->rx_buff
[(s
->rxbuf_start
+ addr
- 20) % SJA_RCV_BUF_LEN
];
730 DPRINTF("read addr 0x%02x, %d bytes, content 0x%02lx\n",
731 (int)addr
, size
, (long unsigned int)temp
);
736 int can_sja_can_receive(CanBusClientState
*client
)
738 CanSJA1000State
*s
= container_of(client
, CanSJA1000State
, bus_client
);
740 if (s
->clock
& 0x80) { /* PeliCAN Mode */
741 if (s
->mode
& 0x01) { /* reset mode. */
744 } else { /* BasicCAN mode */
745 if (s
->control
& 0x01) {
750 return 1; /* always return 1, when operation mode */
753 ssize_t
can_sja_receive(CanBusClientState
*client
, const qemu_can_frame
*frames
,
756 CanSJA1000State
*s
= container_of(client
, CanSJA1000State
, bus_client
);
757 static uint8_t rcv
[SJA_MSG_MAX_LEN
];
760 const qemu_can_frame
*frame
= frames
;
762 if (frames_cnt
<= 0) {
766 can_display_msg("[cansja]: receive ", frame
);
769 if (s
->clock
& 0x80) { /* PeliCAN Mode */
771 /* the CAN controller is receiving a message */
772 s
->status_pel
|= (1 << 4);
774 if (can_sja_accept_filter(s
, frame
) == 0) {
775 s
->status_pel
&= ~(1 << 4);
777 qemu_log("[cansja]: filter rejects message\n");
782 ret
= frame2buff_pel(frame
, rcv
);
784 s
->status_pel
&= ~(1 << 4);
786 qemu_log("[cansja]: message store failed\n");
788 return ret
; /* maybe not support now. */
791 if (s
->rx_cnt
+ ret
> SJA_RCV_BUF_LEN
) { /* Data overrun. */
792 s
->status_pel
|= (1 << 1); /* Overrun status */
793 s
->interrupt_pel
|= (1 << 3);
794 s
->status_pel
&= ~(1 << 4);
796 qemu_log("[cansja]: receive FIFO overrun\n");
798 can_sja_update_pel_irq(s
);
804 qemu_log("[cansja]: message stored in receive FIFO\n");
807 for (i
= 0; i
< ret
; i
++) {
808 s
->rx_buff
[(s
->rx_ptr
++) % SJA_RCV_BUF_LEN
] = rcv
[i
];
810 s
->rx_ptr
%= SJA_RCV_BUF_LEN
; /* update the pointer. */
812 s
->status_pel
|= 0x01; /* Set the Receive Buffer Status. DS-p23 */
813 s
->interrupt_pel
|= 0x01;
814 s
->status_pel
&= ~(1 << 4);
815 s
->status_pel
|= (1 << 0);
816 can_sja_update_pel_irq(s
);
817 } else { /* BasicCAN mode */
819 /* the CAN controller is receiving a message */
820 s
->status_bas
|= (1 << 4);
822 ret
= frame2buff_bas(frame
, rcv
);
824 s
->status_bas
&= ~(1 << 4);
826 qemu_log("[cansja]: message store failed\n");
828 return ret
; /* maybe not support now. */
831 if (s
->rx_cnt
+ ret
> SJA_RCV_BUF_LEN
) { /* Data overrun. */
832 s
->status_bas
|= (1 << 1); /* Overrun status */
833 s
->status_bas
&= ~(1 << 4);
834 s
->interrupt_bas
|= (1 << 3);
835 can_sja_update_bas_irq(s
);
837 qemu_log("[cansja]: receive FIFO overrun\n");
845 qemu_log("[cansja]: message stored\n");
848 for (i
= 0; i
< ret
; i
++) {
849 s
->rx_buff
[(s
->rx_ptr
++) % SJA_RCV_BUF_LEN
] = rcv
[i
];
851 s
->rx_ptr
%= SJA_RCV_BUF_LEN
; /* update the pointer. */
853 s
->status_bas
|= 0x01; /* Set the Receive Buffer Status. DS-p15 */
854 s
->status_bas
&= ~(1 << 4);
855 s
->interrupt_bas
|= (1 << 0);
856 can_sja_update_bas_irq(s
);
861 static CanBusClientInfo can_sja_bus_client_info
= {
862 .can_receive
= can_sja_can_receive
,
863 .receive
= can_sja_receive
,
867 int can_sja_connect_to_bus(CanSJA1000State
*s
, CanBusState
*bus
)
869 s
->bus_client
.info
= &can_sja_bus_client_info
;
875 if (can_bus_insert_client(bus
, &s
->bus_client
) < 0) {
882 void can_sja_disconnect(CanSJA1000State
*s
)
884 can_bus_remove_client(&s
->bus_client
);
887 int can_sja_init(CanSJA1000State
*s
, qemu_irq irq
)
891 qemu_irq_lower(s
->irq
);
893 can_sja_hardware_reset(s
);
898 const VMStateDescription vmstate_qemu_can_filter
= {
899 .name
= "qemu_can_filter",
901 .minimum_version_id
= 1,
902 .minimum_version_id_old
= 1,
903 .fields
= (VMStateField
[]) {
904 VMSTATE_UINT32(can_id
, qemu_can_filter
),
905 VMSTATE_UINT32(can_mask
, qemu_can_filter
),
906 VMSTATE_END_OF_LIST()
910 static int can_sja_post_load(void *opaque
, int version_id
)
912 CanSJA1000State
*s
= opaque
;
913 if (s
->clock
& 0x80) { /* PeliCAN Mode */
914 can_sja_update_pel_irq(s
);
916 can_sja_update_bas_irq(s
);
921 /* VMState is needed for live migration of QEMU images */
922 const VMStateDescription vmstate_can_sja
= {
925 .minimum_version_id
= 1,
926 .minimum_version_id_old
= 1,
927 .post_load
= can_sja_post_load
,
928 .fields
= (VMStateField
[]) {
929 VMSTATE_UINT8(mode
, CanSJA1000State
),
931 VMSTATE_UINT8(status_pel
, CanSJA1000State
),
932 VMSTATE_UINT8(interrupt_pel
, CanSJA1000State
),
933 VMSTATE_UINT8(interrupt_en
, CanSJA1000State
),
934 VMSTATE_UINT8(rxmsg_cnt
, CanSJA1000State
),
935 VMSTATE_UINT8(rxbuf_start
, CanSJA1000State
),
936 VMSTATE_UINT8(clock
, CanSJA1000State
),
938 VMSTATE_BUFFER(code_mask
, CanSJA1000State
),
939 VMSTATE_BUFFER(tx_buff
, CanSJA1000State
),
941 VMSTATE_BUFFER(rx_buff
, CanSJA1000State
),
943 VMSTATE_UINT32(rx_ptr
, CanSJA1000State
),
944 VMSTATE_UINT32(rx_cnt
, CanSJA1000State
),
946 VMSTATE_UINT8(control
, CanSJA1000State
),
948 VMSTATE_UINT8(status_bas
, CanSJA1000State
),
949 VMSTATE_UINT8(interrupt_bas
, CanSJA1000State
),
950 VMSTATE_UINT8(code
, CanSJA1000State
),
951 VMSTATE_UINT8(mask
, CanSJA1000State
),
953 VMSTATE_STRUCT_ARRAY(filter
, CanSJA1000State
, 4, 0,
954 vmstate_qemu_can_filter
, qemu_can_filter
),
957 VMSTATE_END_OF_LIST()