1 /*
2 * Copyright (c) 2022 Vestas Wind Systems A/S
3 * Copyright (c) 2021 Alexander Wachter
4 * Copyright (c) 2022 Kamil Serwus
5 * Copyright (c) 2023 Sebastian Schlupp
6 *
7 * SPDX-License-Identifier: Apache-2.0
8 */
9
10 #include <zephyr/drivers/can.h>
11 #include <zephyr/drivers/can/can_mcan.h>
12 #include <zephyr/drivers/pinctrl.h>
13 #include <zephyr/irq.h>
14 #include <zephyr/kernel.h>
15 #include <zephyr/logging/log.h>
16 #include <soc.h>
17
18 LOG_MODULE_REGISTER(can_sam0, CONFIG_CAN_LOG_LEVEL);
19
20 #define DT_DRV_COMPAT atmel_sam0_can
21
22 struct can_sam0_config {
23 mm_reg_t base;
24 mem_addr_t mram;
25 void (*config_irq)(void);
26 const struct pinctrl_dev_config *pcfg;
27 volatile uint32_t *mclk;
28 uint32_t mclk_mask;
29 uint16_t gclk_core_id;
30 int divider;
31 };
32
can_sam0_read_reg(const struct device * dev,uint16_t reg,uint32_t * val)33 static int can_sam0_read_reg(const struct device *dev, uint16_t reg, uint32_t *val)
34 {
35 const struct can_mcan_config *mcan_config = dev->config;
36 const struct can_sam0_config *sam_config = mcan_config->custom;
37
38 return can_mcan_sys_read_reg(sam_config->base, reg, val);
39 }
40
can_sam0_write_reg(const struct device * dev,uint16_t reg,uint32_t val)41 static int can_sam0_write_reg(const struct device *dev, uint16_t reg, uint32_t val)
42 {
43 const struct can_mcan_config *mcan_config = dev->config;
44 const struct can_sam0_config *sam_config = mcan_config->custom;
45
46 switch (reg) {
47 case CAN_MCAN_ILS:
48 /* All interrupts are assigned to MCAN_INT0 */
49 val = 0;
50 break;
51 case CAN_MCAN_ILE:
52 /* SAM0 has only one line to handle interrupts */
53 val = CAN_MCAN_ILE_EINT0;
54 break;
55 default:
56 /* No field remap needed */
57 break;
58 };
59
60 return can_mcan_sys_write_reg(sam_config->base, reg, val);
61 }
62
can_sam0_read_mram(const struct device * dev,uint16_t offset,void * dst,size_t len)63 static int can_sam0_read_mram(const struct device *dev, uint16_t offset, void *dst, size_t len)
64 {
65 const struct can_mcan_config *mcan_config = dev->config;
66 const struct can_sam0_config *sam_config = mcan_config->custom;
67
68 return can_mcan_sys_read_mram(sam_config->mram, offset, dst, len);
69 }
70
can_sam0_write_mram(const struct device * dev,uint16_t offset,const void * src,size_t len)71 static int can_sam0_write_mram(const struct device *dev, uint16_t offset, const void *src,
72 size_t len)
73 {
74 const struct can_mcan_config *mcan_config = dev->config;
75 const struct can_sam0_config *sam_config = mcan_config->custom;
76
77 return can_mcan_sys_write_mram(sam_config->mram, offset, src, len);
78 }
79
can_sam0_clear_mram(const struct device * dev,uint16_t offset,size_t len)80 static int can_sam0_clear_mram(const struct device *dev, uint16_t offset, size_t len)
81 {
82 const struct can_mcan_config *mcan_config = dev->config;
83 const struct can_sam0_config *sam_config = mcan_config->custom;
84
85 return can_mcan_sys_clear_mram(sam_config->mram, offset, len);
86 }
87
can_sam0_line_x_isr(const struct device * dev)88 void can_sam0_line_x_isr(const struct device *dev)
89 {
90 can_mcan_line_0_isr(dev);
91 can_mcan_line_1_isr(dev);
92 }
93
can_sam0_get_core_clock(const struct device * dev,uint32_t * rate)94 static int can_sam0_get_core_clock(const struct device *dev, uint32_t *rate)
95 {
96 const struct can_mcan_config *mcan_cfg = dev->config;
97 const struct can_sam0_config *sam_cfg = mcan_cfg->custom;
98
99 #if defined(CONFIG_SOC_SERIES_SAME51) || defined(CONFIG_SOC_SERIES_SAME54)
100 /*DFFL has to be used as clock source for the ATSAME51/54 family of SoCs*/
101 *rate = SOC_ATMEL_SAM0_DFLL48_FREQ_HZ / (sam_cfg->divider);
102 #elif defined(CONFIG_SOC_SERIES_SAMC21)
103 /*OSC48M has to be used as clock source for the ATSAMC21 family of SoCs*/
104 *rate = SOC_ATMEL_SAM0_OSC48M_FREQ_HZ / (sam_cfg->divider);
105 #endif
106
107 return 0;
108 }
109
can_sam0_clock_enable(const struct can_sam0_config * cfg)110 static void can_sam0_clock_enable(const struct can_sam0_config *cfg)
111 {
112 /* Enable the GLCK7 with DIV*/
113 #if defined(CONFIG_SOC_SERIES_SAME51) || defined(CONFIG_SOC_SERIES_SAME54)
114 /*DFFL has to be used as clock source for the ATSAME51/54 family of SoCs*/
115 GCLK->GENCTRL[7].reg = GCLK_GENCTRL_SRC(GCLK_GENCTRL_SRC_DFLL)
116 | GCLK_GENCTRL_DIV(cfg->divider)
117 | GCLK_GENCTRL_GENEN;
118 #elif defined(CONFIG_SOC_SERIES_SAMC21)
119 /*OSC48M has to be used as clock source for the ATSAMC21 family of SoCs*/
120 GCLK->GENCTRL[7].reg = GCLK_GENCTRL_SRC(GCLK_GENCTRL_SRC_OSC48M)
121 | GCLK_GENCTRL_DIV(cfg->divider)
122 | GCLK_GENCTRL_GENEN;
123 #endif
124
125 /* Route channel */
126 GCLK->PCHCTRL[cfg->gclk_core_id].reg = GCLK_PCHCTRL_GEN_GCLK7
127 | GCLK_PCHCTRL_CHEN;
128
129 /* Enable CAN clock in MCLK */
130 *cfg->mclk |= cfg->mclk_mask;
131 }
132
can_sam0_init(const struct device * dev)133 static int can_sam0_init(const struct device *dev)
134 {
135 const struct can_mcan_config *mcan_cfg = dev->config;
136 const struct can_sam0_config *sam_cfg = mcan_cfg->custom;
137 int ret;
138
139 can_sam0_clock_enable(sam_cfg);
140
141 ret = pinctrl_apply_state(sam_cfg->pcfg, PINCTRL_STATE_DEFAULT);
142 if (ret < 0) {
143 LOG_ERR("failed to apply pinctrl");
144 return ret;
145 }
146
147 ret = can_mcan_configure_mram(dev, 0U, sam_cfg->mram);
148 if (ret != 0) {
149 LOG_ERR("failed to configure message ram");
150 return ret;
151 }
152
153 ret = can_mcan_init(dev);
154 if (ret != 0) {
155 LOG_ERR("failed to mcan init");
156 return ret;
157 }
158
159 sam_cfg->config_irq();
160
161 return ret;
162 }
163
164 static DEVICE_API(can, can_sam0_driver_api) = {
165 .get_capabilities = can_mcan_get_capabilities,
166 .start = can_mcan_start,
167 .stop = can_mcan_stop,
168 .set_mode = can_mcan_set_mode,
169 .set_timing = can_mcan_set_timing,
170 .send = can_mcan_send,
171 .add_rx_filter = can_mcan_add_rx_filter,
172 .remove_rx_filter = can_mcan_remove_rx_filter,
173 .get_state = can_mcan_get_state,
174 #ifdef CONFIG_CAN_MANUAL_RECOVERY_MODE
175 .recover = can_mcan_recover,
176 #endif /* CONFIG_CAN_MANUAL_RECOVERY_MODE */
177 .get_core_clock = can_sam0_get_core_clock,
178 .get_max_filters = can_mcan_get_max_filters,
179 .set_state_change_callback = can_mcan_set_state_change_callback,
180 .timing_min = CAN_MCAN_TIMING_MIN_INITIALIZER,
181 .timing_max = CAN_MCAN_TIMING_MAX_INITIALIZER,
182 #ifdef CONFIG_CAN_FD_MODE
183 .set_timing_data = can_mcan_set_timing_data,
184 .timing_data_min = CAN_MCAN_TIMING_DATA_MIN_INITIALIZER,
185 .timing_data_max = CAN_MCAN_TIMING_DATA_MAX_INITIALIZER,
186 #endif /* CONFIG_CAN_FD_MODE */
187 };
188
189 static const struct can_mcan_ops can_sam0_ops = {
190 .read_reg = can_sam0_read_reg,
191 .write_reg = can_sam0_write_reg,
192 .read_mram = can_sam0_read_mram,
193 .write_mram = can_sam0_write_mram,
194 .clear_mram = can_sam0_clear_mram,
195 };
196
197 #define CAN_SAM0_IRQ_CFG_FUNCTION(inst) \
198 static void config_can_##inst##_irq(void) \
199 { \
200 LOG_DBG("Enable CAN##inst## IRQ"); \
201 IRQ_CONNECT(DT_INST_IRQ_BY_NAME(inst, int0, irq), \
202 DT_INST_IRQ_BY_NAME(inst, int0, priority), can_sam0_line_x_isr, \
203 DEVICE_DT_INST_GET(inst), 0); \
204 irq_enable(DT_INST_IRQ_BY_NAME(inst, int0, irq)); \
205 }
206
207 #define CAN_SAM0_CFG_INST(inst) \
208 CAN_MCAN_DT_INST_CALLBACKS_DEFINE(inst, can_sam0_cbs_##inst); \
209 CAN_MCAN_DT_INST_MRAM_DEFINE(inst, can_sam0_mram_##inst); \
210 \
211 static const struct can_sam0_config can_sam0_cfg_##inst = { \
212 .base = CAN_MCAN_DT_INST_MCAN_ADDR(inst), \
213 .mram = (mem_addr_t)POINTER_TO_UINT(&can_sam0_mram_##inst), \
214 .mclk = (volatile uint32_t *)MCLK_MASK_DT_INT_REG_ADDR(inst), \
215 .mclk_mask = BIT(DT_INST_CLOCKS_CELL_BY_NAME(inst, mclk, bit)), \
216 .gclk_core_id = DT_INST_CLOCKS_CELL_BY_NAME(inst, gclk, periph_ch), \
217 .divider = DT_INST_PROP(inst, divider), \
218 .pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(inst), \
219 .config_irq = config_can_##inst##_irq, \
220 }; \
221 \
222 static const struct can_mcan_config can_mcan_cfg_##inst = \
223 CAN_MCAN_DT_CONFIG_INST_GET(inst, &can_sam0_cfg_##inst, &can_sam0_ops, \
224 &can_sam0_cbs_##inst);
225
226 #define CAN_SAM0_DATA_INST(inst) \
227 static struct can_mcan_data can_mcan_data_##inst = \
228 CAN_MCAN_DATA_INITIALIZER(NULL);
229
230 #define CAN_SAM0_DEVICE_INST(inst) \
231 CAN_DEVICE_DT_INST_DEFINE(inst, can_sam0_init, NULL, \
232 &can_mcan_data_##inst, \
233 &can_mcan_cfg_##inst, \
234 POST_KERNEL, CONFIG_CAN_INIT_PRIORITY, \
235 &can_sam0_driver_api);
236
237 #define CAN_SAM0_INST(inst) \
238 CAN_MCAN_DT_INST_BUILD_ASSERT_MRAM_CFG(inst); \
239 PINCTRL_DT_INST_DEFINE(inst); \
240 CAN_SAM0_IRQ_CFG_FUNCTION(inst) \
241 CAN_SAM0_CFG_INST(inst) \
242 CAN_SAM0_DATA_INST(inst) \
243 CAN_SAM0_DEVICE_INST(inst)
244
245 DT_INST_FOREACH_STATUS_OKAY(CAN_SAM0_INST)
246