|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 Henrik Brix Andersen <[email protected]> |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <device.h> |
| 8 | +#include <drivers/can.h> |
| 9 | +#include <drivers/clock_control.h> |
| 10 | +#include <logging/log.h> |
| 11 | + |
| 12 | +#include "can_mcan.h" |
| 13 | + |
| 14 | +LOG_MODULE_REGISTER(mcux_mcan, CONFIG_CAN_LOG_LEVEL); |
| 15 | + |
| 16 | +#define DT_DRV_COMPAT nxp_lpc_mcan |
| 17 | + |
| 18 | +struct mcux_mcan_config { |
| 19 | + struct can_mcan_config mcan; |
| 20 | + const struct device *clock_dev; |
| 21 | + clock_control_subsys_t clock_subsys; |
| 22 | + void (*irq_config_func)(const struct device *dev); |
| 23 | +}; |
| 24 | + |
| 25 | +struct mcux_mcan_data { |
| 26 | + struct can_mcan_data mcan; |
| 27 | + struct can_mcan_msg_sram msg_ram __nocache; |
| 28 | +}; |
| 29 | + |
| 30 | +static int mcux_mcan_set_mode(const struct device *dev, enum can_mode mode) |
| 31 | +{ |
| 32 | + const struct mcux_mcan_config *config = dev->config; |
| 33 | + |
| 34 | + return can_mcan_set_mode(&config->mcan, mode); |
| 35 | +} |
| 36 | + |
| 37 | +static int mcux_mcan_set_timing(const struct device *dev, |
| 38 | + const struct can_timing *timing, |
| 39 | + const struct can_timing *timing_data) |
| 40 | +{ |
| 41 | + const struct mcux_mcan_config *config = dev->config; |
| 42 | + |
| 43 | + return can_mcan_set_timing(&config->mcan, timing, timing_data); |
| 44 | +} |
| 45 | + |
| 46 | +static int mcux_mcan_send(const struct device *dev, const struct zcan_frame *msg, |
| 47 | + k_timeout_t timeout, can_tx_callback_t callback, |
| 48 | + void *user_data) |
| 49 | +{ |
| 50 | + const struct mcux_mcan_config *config = dev->config; |
| 51 | + struct mcux_mcan_data *data = dev->data; |
| 52 | + |
| 53 | + return can_mcan_send(&config->mcan, &data->mcan, &data->msg_ram, |
| 54 | + msg, timeout, callback, user_data); |
| 55 | +} |
| 56 | + |
| 57 | +static int mcux_mcan_add_rx_filter(const struct device *dev, |
| 58 | + can_rx_callback_t cb, |
| 59 | + void *user_data, |
| 60 | + const struct zcan_filter *filter) |
| 61 | +{ |
| 62 | + struct mcux_mcan_data *data = dev->data; |
| 63 | + |
| 64 | + return can_mcan_add_rx_filter(&data->mcan, &data->msg_ram, |
| 65 | + cb, user_data, filter); |
| 66 | +} |
| 67 | + |
| 68 | +static void mcux_mcan_remove_rx_filter(const struct device *dev, int filter_id) |
| 69 | +{ |
| 70 | + struct mcux_mcan_data *data = dev->data; |
| 71 | + |
| 72 | + can_mcan_remove_rx_filter(&data->mcan, &data->msg_ram, filter_id); |
| 73 | +} |
| 74 | + |
| 75 | +static enum can_state mcux_mcan_get_state(const struct device *dev, |
| 76 | + struct can_bus_err_cnt *err_cnt) |
| 77 | +{ |
| 78 | + const struct mcux_mcan_config *config = dev->config; |
| 79 | + |
| 80 | + return can_mcan_get_state(&config->mcan, err_cnt); |
| 81 | +} |
| 82 | + |
| 83 | +static void mcux_mcan_set_state_change_callback(const struct device *dev, |
| 84 | + can_state_change_callback_t cb) |
| 85 | +{ |
| 86 | + struct mcux_mcan_data *data = dev->data; |
| 87 | + |
| 88 | + data->mcan.state_change_cb = cb; |
| 89 | +} |
| 90 | + |
| 91 | +static int mcux_mcan_get_core_clock(const struct device *dev, uint32_t *rate) |
| 92 | +{ |
| 93 | + const struct mcux_mcan_config *config = dev->config; |
| 94 | + |
| 95 | + return clock_control_get_rate(config->clock_dev, config->clock_subsys, |
| 96 | + rate); |
| 97 | +} |
| 98 | + |
| 99 | +static void mcux_mcan_line_0_isr(const struct device *dev) |
| 100 | +{ |
| 101 | + const struct mcux_mcan_config *config = dev->config; |
| 102 | + struct mcux_mcan_data *data = dev->data; |
| 103 | + |
| 104 | + can_mcan_line_0_isr(&config->mcan, &data->msg_ram, &data->mcan); |
| 105 | +} |
| 106 | + |
| 107 | +static void mcux_mcan_line_1_isr(const struct device *dev) |
| 108 | +{ |
| 109 | + const struct mcux_mcan_config *config = dev->config; |
| 110 | + struct mcux_mcan_data *data = dev->data; |
| 111 | + |
| 112 | + can_mcan_line_1_isr(&config->mcan, &data->msg_ram, &data->mcan); |
| 113 | +} |
| 114 | + |
| 115 | +static int mcux_mcan_init(const struct device *dev) |
| 116 | +{ |
| 117 | + const struct mcux_mcan_config *config = dev->config; |
| 118 | + struct mcux_mcan_data *data = dev->data; |
| 119 | + int err; |
| 120 | + |
| 121 | + err = clock_control_on(config->clock_dev, config->clock_subsys); |
| 122 | + if (err) { |
| 123 | + LOG_ERR("failed to enable clock (err %d)", err); |
| 124 | + return -EINVAL; |
| 125 | + } |
| 126 | + |
| 127 | + err = can_mcan_init(dev, &config->mcan, &data->msg_ram, &data->mcan); |
| 128 | + if (err) { |
| 129 | + LOG_ERR("failed to initialize mcan (err %d)", err); |
| 130 | + return err; |
| 131 | + } |
| 132 | + |
| 133 | + config->irq_config_func(dev); |
| 134 | + |
| 135 | + return 0; |
| 136 | +} |
| 137 | + |
| 138 | +static const struct can_driver_api mcux_mcan_driver_api = { |
| 139 | + .set_mode = mcux_mcan_set_mode, |
| 140 | + .set_timing = mcux_mcan_set_timing, |
| 141 | + .send = mcux_mcan_send, |
| 142 | + .add_rx_filter = mcux_mcan_add_rx_filter, |
| 143 | + .remove_rx_filter = mcux_mcan_remove_rx_filter, |
| 144 | +#ifndef CONFIG_CAN_AUTO_BUS_OFF_RECOVERY |
| 145 | + .recover = can_mcan_recover, |
| 146 | +#endif /* CONFIG_CAN_AUTO_BUS_OFF_RECOVERY */ |
| 147 | + .get_state = mcux_mcan_get_state, |
| 148 | + .set_state_change_callback = mcux_mcan_set_state_change_callback, |
| 149 | + .get_core_clock = mcux_mcan_get_core_clock, |
| 150 | + /* |
| 151 | + * MCUX MCAN timing limits are specified in the "Nominal bit timing and |
| 152 | + * prescaler register (NBTP)" table in the SoC reference manual. |
| 153 | + * |
| 154 | + * Note that the values here are the "physical" timing limits, whereas |
| 155 | + * the register field limits are physical values minus 1 (which is |
| 156 | + * handled by the register assignments in the common MCAN driver code). |
| 157 | + */ |
| 158 | + .timing_min = { |
| 159 | + .sjw = 1, |
| 160 | + .prop_seg = 0, |
| 161 | + .phase_seg1 = 1, |
| 162 | + .phase_seg2 = 1, |
| 163 | + .prescaler = 1 |
| 164 | + }, |
| 165 | + .timing_max = { |
| 166 | + .sjw = 128, |
| 167 | + .prop_seg = 0, |
| 168 | + .phase_seg1 = 256, |
| 169 | + .phase_seg2 = 128, |
| 170 | + .prescaler = 512, |
| 171 | + }, |
| 172 | +#ifdef CONFIG_CAN_FD_MODE |
| 173 | + /* |
| 174 | + * MCUX MCAN data timing limits are specified in the "Data bit timing |
| 175 | + * and prescaler register (DBTP)" table in the SoC reference manual. |
| 176 | + * |
| 177 | + * Note that the values here are the "physical" timing limits, whereas |
| 178 | + * the register field limits are physical values minus 1 (which is |
| 179 | + * handled by the register assignments in the common MCAN driver code). |
| 180 | + */ |
| 181 | + .timing_min_data = { |
| 182 | + .sjw = 1, |
| 183 | + .prop_seg = 0, |
| 184 | + .phase_seg1 = 1, |
| 185 | + .phase_seg2 = 1, |
| 186 | + .prescaler = 1, |
| 187 | + }, |
| 188 | + .timing_max_data = { |
| 189 | + .sjw = 16, |
| 190 | + .prop_seg = 0, |
| 191 | + .phase_seg1 = 16, |
| 192 | + .phase_seg2 = 16, |
| 193 | + .prescaler = 32, |
| 194 | + } |
| 195 | +#endif /* CONFIG_CAN_FD_MODE */ |
| 196 | +}; |
| 197 | + |
| 198 | +#ifdef CONFIG_CAN_FD_MODE |
| 199 | +#define MCUX_MCAN_MCAN_INIT(n) \ |
| 200 | + { \ |
| 201 | + .can = (struct can_mcan_reg *)DT_INST_REG_ADDR(n), \ |
| 202 | + .bus_speed = DT_INST_PROP(n, bus_speed), \ |
| 203 | + .sjw = DT_INST_PROP(n, sjw), \ |
| 204 | + .sample_point = DT_INST_PROP_OR(n, sample_point, 0), \ |
| 205 | + .prop_ts1 = DT_INST_PROP_OR(n, prop_seg, 0) + \ |
| 206 | + DT_INST_PROP_OR(n, phase_seg1, 0), \ |
| 207 | + .ts2 = DT_INST_PROP_OR(n, phase_seg2, 0), \ |
| 208 | + .bus_speed_data = DT_INST_PROP(n, bus_speed_data), \ |
| 209 | + .sjw_data = DT_INST_PROP(n, sjw_data), \ |
| 210 | + .sample_point_data = \ |
| 211 | + DT_INST_PROP_OR(n, sample_point_data, 0), \ |
| 212 | + .prop_ts1_data = DT_INST_PROP_OR(n, prop_seg_data, 0) + \ |
| 213 | + DT_INST_PROP_OR(n, phase_seg1_data, 0), \ |
| 214 | + .ts2_data = DT_INST_PROP_OR(n, phase_seg2_data, 0), \ |
| 215 | + .tx_delay_comp_offset = \ |
| 216 | + DT_INST_PROP(n, tx_delay_comp_offset) \ |
| 217 | + } |
| 218 | +#else /* CONFIG_CAN_FD_MODE */ |
| 219 | +#define MCUX_MCAN_MCAN_INIT(n) \ |
| 220 | + { \ |
| 221 | + .can = (struct can_mcan_reg *)DT_INST_REG_ADDR(n), \ |
| 222 | + .bus_speed = DT_INST_PROP(n, bus_speed), \ |
| 223 | + .sjw = DT_INST_PROP(n, sjw), \ |
| 224 | + .sample_point = DT_INST_PROP_OR(n, sample_point, 0), \ |
| 225 | + .prop_ts1 = DT_INST_PROP_OR(n, prop_seg, 0) + \ |
| 226 | + DT_INST_PROP_OR(n, phase_seg1, 0), \ |
| 227 | + .ts2 = DT_INST_PROP_OR(n, phase_seg2, 0), \ |
| 228 | + } |
| 229 | +#endif /* !CONFIG_CAN_FD_MODE */ |
| 230 | + |
| 231 | +#define MCUX_MCAN_INIT(n) \ |
| 232 | + static void mcux_mcan_irq_config_##n(const struct device *dev); \ |
| 233 | + \ |
| 234 | + static const struct mcux_mcan_config mcux_mcan_config_##n = { \ |
| 235 | + .mcan = MCUX_MCAN_MCAN_INIT(n), \ |
| 236 | + .clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(n)), \ |
| 237 | + .clock_subsys = (clock_control_subsys_t) \ |
| 238 | + DT_INST_CLOCKS_CELL(n, name), \ |
| 239 | + .irq_config_func = mcux_mcan_irq_config_##n, \ |
| 240 | + }; \ |
| 241 | + \ |
| 242 | + static struct mcux_mcan_data mcux_mcan_data_##n; \ |
| 243 | + \ |
| 244 | + DEVICE_DT_INST_DEFINE(n, &mcux_mcan_init, NULL, \ |
| 245 | + &mcux_mcan_data_##n, \ |
| 246 | + &mcux_mcan_config_##n, \ |
| 247 | + POST_KERNEL, \ |
| 248 | + CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \ |
| 249 | + &mcux_mcan_driver_api); \ |
| 250 | + \ |
| 251 | + static void mcux_mcan_irq_config_##n(const struct device *dev) \ |
| 252 | + { \ |
| 253 | + IRQ_CONNECT(DT_INST_IRQ_BY_IDX(n, 0, irq), \ |
| 254 | + DT_INST_IRQ_BY_IDX(n, 0, priority), \ |
| 255 | + mcux_mcan_line_0_isr, \ |
| 256 | + DEVICE_DT_INST_GET(n), 0); \ |
| 257 | + irq_enable(DT_INST_IRQ_BY_IDX(n, 0, irq)); \ |
| 258 | + \ |
| 259 | + IRQ_CONNECT(DT_INST_IRQ_BY_IDX(n, 1, irq), \ |
| 260 | + DT_INST_IRQ_BY_IDX(n, 1, priority), \ |
| 261 | + mcux_mcan_line_1_isr, \ |
| 262 | + DEVICE_DT_INST_GET(n), 0); \ |
| 263 | + irq_enable(DT_INST_IRQ_BY_IDX(n, 1, irq)); \ |
| 264 | + } |
| 265 | + |
| 266 | +DT_INST_FOREACH_STATUS_OKAY(MCUX_MCAN_INIT) |
0 commit comments