1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @ingroup CANBus |
---|
7 | * |
---|
8 | * @brief Controller Area Network (DCAN) Controller Implementation |
---|
9 | * |
---|
10 | */ |
---|
11 | |
---|
12 | /* |
---|
13 | * Copyright (C) 2022 Prashanth S (fishesprashanth@gmail.com) |
---|
14 | * |
---|
15 | * Redistribution and use in source and binary forms, with or without |
---|
16 | * modification, are permitted provided that the following conditions |
---|
17 | * are met: |
---|
18 | * 1. Redistributions of source code must retain the above copyright |
---|
19 | * notice, this list of conditions and the following disclaimer. |
---|
20 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
21 | * notice, this list of conditions and the following disclaimer in the |
---|
22 | * documentation and/or other materials provided with the distribution. |
---|
23 | * |
---|
24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
30 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
32 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
34 | * POSSIBILITY OF SUCH DAMAGE. |
---|
35 | */ |
---|
36 | |
---|
37 | #ifndef _BSP_AM335X_CAN_H |
---|
38 | #define _BSP_AM335X_CAN_H |
---|
39 | |
---|
40 | #include <stdio.h> |
---|
41 | |
---|
42 | #include <dev/can/can.h> |
---|
43 | #include <bsp.h> |
---|
44 | |
---|
45 | #define CAN_NODES 2 |
---|
46 | |
---|
47 | #define CAN_NUM_OF_MSG_OBJS (0x40) |
---|
48 | #define CAN_RX_MSG_OBJ_NUM (0x20) |
---|
49 | #define CAN_TX_MSG_OBJ_NUM (0x20) |
---|
50 | |
---|
51 | #define CAN_EXT_MSG (0x00000001) |
---|
52 | |
---|
53 | #define CAN_TX_MSG_OBJ_START_NUM ((CAN_NUM_OF_MSG_OBJS / 2) + 1) |
---|
54 | #define CAN_TX_MSG_OBJ_END_NUM (CAN_NUM_OF_MSG_OBJS) |
---|
55 | |
---|
56 | #define CAN_RX_MSG_OBJ_START_NUM (1) |
---|
57 | #define CAN_RX_MSG_OBJ_END_NUM (CAN_NUM_OF_MSG_OBJS / 2) |
---|
58 | |
---|
59 | #define DCAN_IFCMD_MSG_NUM_SHIFT (0) /* Bits 0-7: Number of message object in message RAM which is used for data transfer */ |
---|
60 | #define DCAN_IFCMD_MSG_NUM_MASK (0xff << DCAN_IFCMD_MSG_NUM_SHIFT) |
---|
61 | #define DCAN_IFCMD_MSG_NUM(n) (((unsigned int)(n) & DCAN_IFCMD_MSG_NUM_MASK) << DCAN_IFCMD_MSG_NUM_SHIFT) |
---|
62 | |
---|
63 | #define CONTROL_STATUS_SYSBOOT1_MASK (3 << CONTROL_STATUS_SYSBOOT1_SHIFT) |
---|
64 | #define CONTROL_STATUS_SYSBOOT1_19p2MHZ (0 << CONTROL_STATUS_SYSBOOT1_SHIFT) |
---|
65 | #define CONTROL_STATUS_SYSBOOT1_24MHZ (1 << CONTROL_STATUS_SYSBOOT1_SHIFT) |
---|
66 | #define CONTROL_STATUS_SYSBOOT1_25MHZ (2 << CONTROL_STATUS_SYSBOOT1_SHIFT) |
---|
67 | #define CONTROL_STATUS_SYSBOOT1_26MHZ (3 << CONTROL_STATUS_SYSBOOT1_SHIFT) |
---|
68 | |
---|
69 | #define CONFIG_AM335X_CAN_TSEG1 6 |
---|
70 | #define CONFIG_AM335X_CAN_TSEG2 1 |
---|
71 | #define CAN_BIT_QUANTA (CONFIG_AM335X_CAN_TSEG1 + CONFIG_AM335X_CAN_TSEG2 + 1) |
---|
72 | #define CAN_CLOCK_FREQUENCY (am335x_get_sysclk()) |
---|
73 | |
---|
74 | struct am335x_dcan_irq { |
---|
75 | uint32_t dcan_intr0; |
---|
76 | uint32_t dcan_intr1; |
---|
77 | uint32_t dcan_parity; |
---|
78 | }; |
---|
79 | |
---|
80 | struct am335x_dcan_priv { |
---|
81 | uint32_t node; |
---|
82 | uint32_t base_reg; |
---|
83 | uint32_t baudrate; |
---|
84 | struct can_bus *bus; |
---|
85 | struct am335x_dcan_irq irq; |
---|
86 | }; |
---|
87 | |
---|
88 | int dcan_init(struct am335x_dcan_priv *dcan_priv); |
---|
89 | void beagle_can_init(void *node); |
---|
90 | |
---|
91 | void dcan_init_ops(struct am335x_dcan_priv *priv); |
---|
92 | |
---|
93 | #define can_putreg(priv, off, val) \ |
---|
94 | do { \ |
---|
95 | REG(priv->base_reg + off) = val; \ |
---|
96 | CAN_DEBUG_REG("%s:%d %08x<-%08x\n", __FILE__, __LINE__, priv->base_reg + off, val); \ |
---|
97 | } while (0) |
---|
98 | |
---|
99 | static inline uint32_t can_getreg(struct am335x_dcan_priv *priv, uint32_t off) |
---|
100 | { |
---|
101 | CAN_DEBUG_REG("can_getreg 0x%08x = 0x%08x\n", priv->base_reg + off, |
---|
102 | REG(priv->base_reg + off)); |
---|
103 | return REG(priv->base_reg + off); |
---|
104 | } |
---|
105 | |
---|
106 | #endif /* _BSP_AM335X_CAN_H */ |
---|