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_lock();
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
]);
264 qemu_log_unlock(logfile
);
267 static void buff2frame_pel(const uint8_t *buff
, qemu_can_frame
*frame
)
273 if (buff
[0] & 0x40) { /* RTR */
274 frame
->can_id
= QEMU_CAN_RTR_FLAG
;
276 frame
->can_dlc
= buff
[0] & 0x0f;
278 if (buff
[0] & 0x80) { /* Extended */
279 frame
->can_id
|= QEMU_CAN_EFF_FLAG
;
280 frame
->can_id
|= buff
[1] << 21; /* ID.28~ID.21 */
281 frame
->can_id
|= buff
[2] << 13; /* ID.20~ID.13 */
282 frame
->can_id
|= buff
[3] << 5;
283 frame
->can_id
|= buff
[4] >> 3;
284 for (i
= 0; i
< frame
->can_dlc
; i
++) {
285 frame
->data
[i
] = buff
[5 + i
];
291 frame
->can_id
|= buff
[1] << 3;
292 frame
->can_id
|= buff
[2] >> 5;
293 for (i
= 0; i
< frame
->can_dlc
; i
++) {
294 frame
->data
[i
] = buff
[3 + i
];
303 static void buff2frame_bas(const uint8_t *buff
, qemu_can_frame
*frame
)
308 frame
->can_id
= ((buff
[0] << 3) & (0xff << 3)) + ((buff
[1] >> 5) & 0x07);
309 if (buff
[1] & 0x10) { /* RTR */
310 frame
->can_id
= QEMU_CAN_RTR_FLAG
;
312 frame
->can_dlc
= buff
[1] & 0x0f;
314 for (i
= 0; i
< frame
->can_dlc
; i
++) {
315 frame
->data
[i
] = buff
[2 + i
];
323 static int frame2buff_pel(const qemu_can_frame
*frame
, uint8_t *buff
)
326 int dlen
= frame
->can_dlc
;
328 if (frame
->can_id
& QEMU_CAN_ERR_FLAG
) { /* error frame, NOT support now. */
336 buff
[0] = 0x0f & frame
->can_dlc
; /* DLC */
337 if (frame
->can_id
& QEMU_CAN_RTR_FLAG
) { /* RTR */
340 if (frame
->can_id
& QEMU_CAN_EFF_FLAG
) { /* EFF */
342 buff
[1] = extract32(frame
->can_id
, 21, 8); /* ID.28~ID.21 */
343 buff
[2] = extract32(frame
->can_id
, 13, 8); /* ID.20~ID.13 */
344 buff
[3] = extract32(frame
->can_id
, 5, 8); /* ID.12~ID.05 */
345 buff
[4] = extract32(frame
->can_id
, 0, 5) << 3; /* ID.04~ID.00,xxx */
346 for (i
= 0; i
< dlen
; i
++) {
347 buff
[5 + i
] = frame
->data
[i
];
351 buff
[1] = extract32(frame
->can_id
, 3, 8); /* ID.10~ID.03 */
352 buff
[2] = extract32(frame
->can_id
, 0, 3) << 5; /* ID.02~ID.00,xxxxx */
353 for (i
= 0; i
< dlen
; i
++) {
354 buff
[3 + i
] = frame
->data
[i
];
363 static int frame2buff_bas(const qemu_can_frame
*frame
, uint8_t *buff
)
366 int dlen
= frame
->can_dlc
;
369 * EFF, no support for BasicMode
370 * No use for Error frames now,
371 * they could be used in future to update SJA1000 error state
373 if ((frame
->can_id
& QEMU_CAN_EFF_FLAG
) ||
374 (frame
->can_id
& QEMU_CAN_ERR_FLAG
)) {
382 buff
[0] = extract32(frame
->can_id
, 3, 8); /* ID.10~ID.03 */
383 buff
[1] = extract32(frame
->can_id
, 0, 3) << 5; /* ID.02~ID.00,xxxxx */
384 if (frame
->can_id
& QEMU_CAN_RTR_FLAG
) { /* RTR */
387 buff
[1] |= frame
->can_dlc
& 0x0f;
388 for (i
= 0; i
< dlen
; i
++) {
389 buff
[2 + i
] = frame
->data
[i
];
395 static void can_sja_update_pel_irq(CanSJA1000State
*s
)
397 if (s
->interrupt_en
& s
->interrupt_pel
) {
398 qemu_irq_raise(s
->irq
);
400 qemu_irq_lower(s
->irq
);
404 static void can_sja_update_bas_irq(CanSJA1000State
*s
)
406 if ((s
->control
>> 1) & s
->interrupt_bas
) {
407 qemu_irq_raise(s
->irq
);
409 qemu_irq_lower(s
->irq
);
413 void can_sja_mem_write(CanSJA1000State
*s
, hwaddr addr
, uint64_t val
,
416 qemu_can_frame frame
;
421 DPRINTF("write 0x%02llx addr 0x%02x\n",
422 (unsigned long long)val
, (unsigned int)addr
);
424 if (addr
> CAN_SJA_MEM_SIZE
) {
428 if (s
->clock
& 0x80) { /* PeliCAN Mode */
430 case SJA_MOD
: /* Mode register */
431 s
->mode
= 0x1f & val
;
432 if ((s
->mode
& 0x01) && ((val
& 0x01) == 0)) {
433 /* Go to operation mode from reset mode. */
434 if (s
->mode
& (1 << 3)) { /* Single mode. */
436 can_sja_single_filter(&s
->filter
[0],
437 s
->code_mask
+ 0, s
->code_mask
+ 4, 1);
440 can_sja_single_filter(&s
->filter
[1],
441 s
->code_mask
+ 0, s
->code_mask
+ 4, 0);
443 can_bus_client_set_filters(&s
->bus_client
, s
->filter
, 2);
444 } else { /* Dual mode */
446 can_sja_dual_filter(&s
->filter
[0],
447 s
->code_mask
+ 0, s
->code_mask
+ 4, 1);
449 can_sja_dual_filter(&s
->filter
[1],
450 s
->code_mask
+ 2, s
->code_mask
+ 6, 1);
453 can_sja_dual_filter(&s
->filter
[2],
454 s
->code_mask
+ 0, s
->code_mask
+ 4, 0);
456 can_sja_dual_filter(&s
->filter
[3],
457 s
->code_mask
+ 2, s
->code_mask
+ 6, 0);
459 can_bus_client_set_filters(&s
->bus_client
, s
->filter
, 4);
467 case SJA_CMR
: /* Command register. */
468 if (0x01 & val
) { /* Send transmission request. */
469 buff2frame_pel(s
->tx_buff
, &frame
);
471 can_display_msg("[cansja]: Tx request " , &frame
);
475 * Clear transmission complete status,
476 * and Transmit Buffer Status.
477 * write to the backends.
479 s
->status_pel
&= ~(3 << 2);
481 can_bus_client_send(&s
->bus_client
, &frame
, 1);
484 * Set transmission complete status
485 * and Transmit Buffer Status.
487 s
->status_pel
|= (3 << 2);
489 /* Clear transmit status. */
490 s
->status_pel
&= ~(1 << 5);
491 s
->interrupt_pel
|= 0x02;
492 can_sja_update_pel_irq(s
);
494 if (0x04 & val
) { /* Release Receive Buffer */
495 if (s
->rxmsg_cnt
<= 0) {
499 tmp8
= s
->rx_buff
[s
->rxbuf_start
]; count
= 0;
500 if (tmp8
& (1 << 7)) { /* EFF */
504 if (!(tmp8
& (1 << 6))) { /* DATA */
505 count
+= (tmp8
& 0x0f);
509 qemu_log("[cansja]: message released from "
510 "Rx FIFO cnt=%d, count=%d\n", s
->rx_cnt
, count
);
513 s
->rxbuf_start
+= count
;
514 s
->rxbuf_start
%= SJA_RCV_BUF_LEN
;
518 if (s
->rxmsg_cnt
== 0) {
519 s
->status_pel
&= ~(1 << 0);
520 s
->interrupt_pel
&= ~(1 << 0);
521 can_sja_update_pel_irq(s
);
524 if (0x08 & val
) { /* Clear data overrun */
525 s
->status_pel
&= ~(1 << 1);
526 s
->interrupt_pel
&= ~(1 << 3);
527 can_sja_update_pel_irq(s
);
530 case SJA_SR
: /* Status register */
531 case SJA_IR
: /* Interrupt register */
532 break; /* Do nothing */
533 case SJA_IER
: /* Interrupt enable register */
534 s
->interrupt_en
= val
;
536 case 16: /* RX frame information addr16-28. */
537 s
->status_pel
|= (1 << 5); /* Set transmit status. */
540 if (s
->mode
& 0x01) { /* Reset mode */
542 s
->code_mask
[addr
- 16] = val
;
544 } else { /* Operation mode */
545 s
->tx_buff
[addr
- 16] = val
; /* Store to TX buffer directly. */
552 } else { /* Basic Mode */
554 case SJA_BCAN_CTR
: /* Control register, addr 0 */
555 if ((s
->control
& 0x01) && ((val
& 0x01) == 0)) {
556 /* Go to operation mode from reset mode. */
557 s
->filter
[0].can_id
= (s
->code
<< 3) & (0xff << 3);
558 tmp
= (~(s
->mask
<< 3)) & (0xff << 3);
559 tmp
|= QEMU_CAN_EFF_FLAG
; /* Only Basic CAN Frame. */
560 s
->filter
[0].can_mask
= tmp
;
561 can_bus_client_set_filters(&s
->bus_client
, s
->filter
, 1);
565 } else if (!(s
->control
& 0x01) && !(val
& 0x01)) {
566 can_sja_software_reset(s
);
569 s
->control
= 0x1f & val
;
571 case SJA_BCAN_CMR
: /* Command register, addr 1 */
572 if (0x01 & val
) { /* Send transmission request. */
573 buff2frame_bas(s
->tx_buff
, &frame
);
575 can_display_msg("[cansja]: Tx request " , &frame
);
579 * Clear transmission complete status,
580 * and Transmit Buffer Status.
582 s
->status_bas
&= ~(3 << 2);
584 /* write to the backends. */
585 can_bus_client_send(&s
->bus_client
, &frame
, 1);
588 * Set transmission complete status,
589 * and Transmit Buffer Status.
591 s
->status_bas
|= (3 << 2);
593 /* Clear transmit status. */
594 s
->status_bas
&= ~(1 << 5);
595 s
->interrupt_bas
|= 0x02;
596 can_sja_update_bas_irq(s
);
598 if (0x04 & val
) { /* Release Receive Buffer */
599 if (s
->rxmsg_cnt
<= 0) {
603 tmp8
= s
->rx_buff
[(s
->rxbuf_start
+ 1) % SJA_RCV_BUF_LEN
];
604 count
= 2 + (tmp8
& 0x0f);
607 qemu_log("[cansja]: message released from "
608 "Rx FIFO cnt=%d, count=%d\n", s
->rx_cnt
, count
);
611 s
->rxbuf_start
+= count
;
612 s
->rxbuf_start
%= SJA_RCV_BUF_LEN
;
616 if (s
->rxmsg_cnt
== 0) {
617 s
->status_bas
&= ~(1 << 0);
618 s
->interrupt_bas
&= ~(1 << 0);
619 can_sja_update_bas_irq(s
);
622 if (0x08 & val
) { /* Clear data overrun */
623 s
->status_bas
&= ~(1 << 1);
624 s
->interrupt_bas
&= ~(1 << 3);
625 can_sja_update_bas_irq(s
);
635 s
->status_bas
|= (1 << 5); /* Set transmit status. */
638 if ((s
->control
& 0x01) == 0) { /* Operation mode */
639 s
->tx_buff
[addr
- 10] = val
; /* Store to TX buffer directly. */
649 uint64_t can_sja_mem_read(CanSJA1000State
*s
, hwaddr addr
, unsigned size
)
653 DPRINTF("read addr 0x%02x ...\n", (unsigned int)addr
);
655 if (addr
> CAN_SJA_MEM_SIZE
) {
659 if (s
->clock
& 0x80) { /* PeliCAN Mode */
661 case SJA_MOD
: /* Mode register, addr 0 */
664 case SJA_CMR
: /* Command register, addr 1 */
665 temp
= 0x00; /* Command register, cannot be read. */
667 case SJA_SR
: /* Status register, addr 2 */
668 temp
= s
->status_pel
;
670 case SJA_IR
: /* Interrupt register, addr 3 */
671 temp
= s
->interrupt_pel
;
672 s
->interrupt_pel
= 0;
674 s
->interrupt_pel
|= (1 << 0); /* Receive interrupt. */
676 can_sja_update_pel_irq(s
);
678 case SJA_IER
: /* Interrupt enable register, addr 4 */
679 temp
= s
->interrupt_en
;
681 case 5: /* Reserved */
682 case 6: /* Bus timing 0, hardware related, not support now. */
683 case 7: /* Bus timing 1, hardware related, not support now. */
685 * Output control register, hardware related,
686 * not supported for now.
689 case 10 ... 15: /* Reserved */
694 if (s
->mode
& 0x01) { /* Reset mode */
696 temp
= s
->code_mask
[addr
- 16];
700 } else { /* Operation mode */
701 temp
= s
->rx_buff
[(s
->rxbuf_start
+ addr
- 16) %
711 } else { /* Basic Mode */
713 case SJA_BCAN_CTR
: /* Control register, addr 0 */
716 case SJA_BCAN_SR
: /* Status register, addr 2 */
717 temp
= s
->status_bas
;
719 case SJA_BCAN_IR
: /* Interrupt register, addr 3 */
720 temp
= s
->interrupt_bas
;
721 s
->interrupt_bas
= 0;
723 s
->interrupt_bas
|= (1 << 0); /* Receive interrupt. */
725 can_sja_update_bas_irq(s
);
734 temp
= s
->rx_buff
[(s
->rxbuf_start
+ addr
- 20) % SJA_RCV_BUF_LEN
];
744 DPRINTF("read addr 0x%02x, %d bytes, content 0x%02lx\n",
745 (int)addr
, size
, (long unsigned int)temp
);
750 bool can_sja_can_receive(CanBusClientState
*client
)
752 CanSJA1000State
*s
= container_of(client
, CanSJA1000State
, bus_client
);
754 if (s
->clock
& 0x80) { /* PeliCAN Mode */
755 if (s
->mode
& 0x01) { /* reset mode. */
758 } else { /* BasicCAN mode */
759 if (s
->control
& 0x01) {
764 return true; /* always return true, when operation mode */
767 ssize_t
can_sja_receive(CanBusClientState
*client
, const qemu_can_frame
*frames
,
770 CanSJA1000State
*s
= container_of(client
, CanSJA1000State
, bus_client
);
771 static uint8_t rcv
[SJA_MSG_MAX_LEN
];
774 const qemu_can_frame
*frame
= frames
;
776 if (frames_cnt
<= 0) {
779 if (frame
->flags
& QEMU_CAN_FRMF_TYPE_FD
) {
781 can_display_msg("[cansja]: ignor fd frame ", frame
);
787 can_display_msg("[cansja]: receive ", frame
);
790 if (s
->clock
& 0x80) { /* PeliCAN Mode */
792 /* the CAN controller is receiving a message */
793 s
->status_pel
|= (1 << 4);
795 if (can_sja_accept_filter(s
, frame
) == 0) {
796 s
->status_pel
&= ~(1 << 4);
798 qemu_log("[cansja]: filter rejects message\n");
803 ret
= frame2buff_pel(frame
, rcv
);
805 s
->status_pel
&= ~(1 << 4);
807 qemu_log("[cansja]: message store failed\n");
809 return ret
; /* maybe not support now. */
812 if (s
->rx_cnt
+ ret
> SJA_RCV_BUF_LEN
) { /* Data overrun. */
813 s
->status_pel
|= (1 << 1); /* Overrun status */
814 s
->interrupt_pel
|= (1 << 3);
815 s
->status_pel
&= ~(1 << 4);
817 qemu_log("[cansja]: receive FIFO overrun\n");
819 can_sja_update_pel_irq(s
);
825 qemu_log("[cansja]: message stored in receive FIFO\n");
828 for (i
= 0; i
< ret
; i
++) {
829 s
->rx_buff
[(s
->rx_ptr
++) % SJA_RCV_BUF_LEN
] = rcv
[i
];
831 s
->rx_ptr
%= SJA_RCV_BUF_LEN
; /* update the pointer. */
833 s
->status_pel
|= 0x01; /* Set the Receive Buffer Status. DS-p23 */
834 s
->interrupt_pel
|= 0x01;
835 s
->status_pel
&= ~(1 << 4);
836 s
->status_pel
|= (1 << 0);
837 can_sja_update_pel_irq(s
);
838 } else { /* BasicCAN mode */
840 /* the CAN controller is receiving a message */
841 s
->status_bas
|= (1 << 4);
843 ret
= frame2buff_bas(frame
, rcv
);
845 s
->status_bas
&= ~(1 << 4);
847 qemu_log("[cansja]: message store failed\n");
849 return ret
; /* maybe not support now. */
852 if (s
->rx_cnt
+ ret
> SJA_RCV_BUF_LEN
) { /* Data overrun. */
853 s
->status_bas
|= (1 << 1); /* Overrun status */
854 s
->status_bas
&= ~(1 << 4);
855 s
->interrupt_bas
|= (1 << 3);
856 can_sja_update_bas_irq(s
);
858 qemu_log("[cansja]: receive FIFO overrun\n");
866 qemu_log("[cansja]: message stored\n");
869 for (i
= 0; i
< ret
; i
++) {
870 s
->rx_buff
[(s
->rx_ptr
++) % SJA_RCV_BUF_LEN
] = rcv
[i
];
872 s
->rx_ptr
%= SJA_RCV_BUF_LEN
; /* update the pointer. */
874 s
->status_bas
|= 0x01; /* Set the Receive Buffer Status. DS-p15 */
875 s
->status_bas
&= ~(1 << 4);
876 s
->interrupt_bas
|= (1 << 0);
877 can_sja_update_bas_irq(s
);
882 static CanBusClientInfo can_sja_bus_client_info
= {
883 .can_receive
= can_sja_can_receive
,
884 .receive
= can_sja_receive
,
888 int can_sja_connect_to_bus(CanSJA1000State
*s
, CanBusState
*bus
)
890 s
->bus_client
.info
= &can_sja_bus_client_info
;
896 if (can_bus_insert_client(bus
, &s
->bus_client
) < 0) {
903 void can_sja_disconnect(CanSJA1000State
*s
)
905 can_bus_remove_client(&s
->bus_client
);
908 int can_sja_init(CanSJA1000State
*s
, qemu_irq irq
)
912 qemu_irq_lower(s
->irq
);
914 can_sja_hardware_reset(s
);
919 const VMStateDescription vmstate_qemu_can_filter
= {
920 .name
= "qemu_can_filter",
922 .minimum_version_id
= 1,
923 .minimum_version_id_old
= 1,
924 .fields
= (VMStateField
[]) {
925 VMSTATE_UINT32(can_id
, qemu_can_filter
),
926 VMSTATE_UINT32(can_mask
, qemu_can_filter
),
927 VMSTATE_END_OF_LIST()
931 static int can_sja_post_load(void *opaque
, int version_id
)
933 CanSJA1000State
*s
= opaque
;
934 if (s
->clock
& 0x80) { /* PeliCAN Mode */
935 can_sja_update_pel_irq(s
);
937 can_sja_update_bas_irq(s
);
942 /* VMState is needed for live migration of QEMU images */
943 const VMStateDescription vmstate_can_sja
= {
946 .minimum_version_id
= 1,
947 .minimum_version_id_old
= 1,
948 .post_load
= can_sja_post_load
,
949 .fields
= (VMStateField
[]) {
950 VMSTATE_UINT8(mode
, CanSJA1000State
),
952 VMSTATE_UINT8(status_pel
, CanSJA1000State
),
953 VMSTATE_UINT8(interrupt_pel
, CanSJA1000State
),
954 VMSTATE_UINT8(interrupt_en
, CanSJA1000State
),
955 VMSTATE_UINT8(rxmsg_cnt
, CanSJA1000State
),
956 VMSTATE_UINT8(rxbuf_start
, CanSJA1000State
),
957 VMSTATE_UINT8(clock
, CanSJA1000State
),
959 VMSTATE_BUFFER(code_mask
, CanSJA1000State
),
960 VMSTATE_BUFFER(tx_buff
, CanSJA1000State
),
962 VMSTATE_BUFFER(rx_buff
, CanSJA1000State
),
964 VMSTATE_UINT32(rx_ptr
, CanSJA1000State
),
965 VMSTATE_UINT32(rx_cnt
, CanSJA1000State
),
967 VMSTATE_UINT8(control
, CanSJA1000State
),
969 VMSTATE_UINT8(status_bas
, CanSJA1000State
),
970 VMSTATE_UINT8(interrupt_bas
, CanSJA1000State
),
971 VMSTATE_UINT8(code
, CanSJA1000State
),
972 VMSTATE_UINT8(mask
, CanSJA1000State
),
974 VMSTATE_STRUCT_ARRAY(filter
, CanSJA1000State
, 4, 0,
975 vmstate_qemu_can_filter
, qemu_can_filter
),
978 VMSTATE_END_OF_LIST()