| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
// |
| 3 |
// Copyright (c) 2023, 2024 Pengutronix, |
| 4 |
// Marc Kleine-Budde <[email protected]> |
| 5 |
// |
| 6 |
// Based on: |
| 7 |
// |
| 8 |
// Rockchip CANFD driver |
| 9 |
// |
| 10 |
// Copyright (c) 2020 Rockchip Electronics Co. Ltd. |
| 11 |
// |
| 12 |
|
| 13 |
#include <linux/delay.h> |
| 14 |
#include <linux/errno.h> |
| 15 |
#include <linux/init.h> |
| 16 |
#include <linux/interrupt.h> |
| 17 |
#include <linux/kernel.h> |
| 18 |
#include <linux/module.h> |
| 19 |
#include <linux/of.h> |
| 20 |
#include <linux/of_device.h> |
| 21 |
#include <linux/platform_device.h> |
| 22 |
#include <linux/pm_runtime.h> |
| 23 |
#include <linux/string.h> |
| 24 |
|
| 25 |
#include "rockchip_canfd.h" |
| 26 |
|
| 27 |
static const struct rkcanfd_devtype_data rkcanfd_devtype_data_rk3568v2 = { |
| 28 |
.model = RKCANFD_MODEL_RK3568V2, |
| 29 |
.quirks = RKCANFD_QUIRK_RK3568_ERRATUM_1 | RKCANFD_QUIRK_RK3568_ERRATUM_2 | |
| 30 |
RKCANFD_QUIRK_RK3568_ERRATUM_3 | RKCANFD_QUIRK_RK3568_ERRATUM_4 | |
| 31 |
RKCANFD_QUIRK_RK3568_ERRATUM_5 | RKCANFD_QUIRK_RK3568_ERRATUM_6 | |
| 32 |
RKCANFD_QUIRK_RK3568_ERRATUM_7 | RKCANFD_QUIRK_RK3568_ERRATUM_8 | |
| 33 |
RKCANFD_QUIRK_RK3568_ERRATUM_9 | RKCANFD_QUIRK_RK3568_ERRATUM_10 | |
| 34 |
RKCANFD_QUIRK_RK3568_ERRATUM_11 | RKCANFD_QUIRK_RK3568_ERRATUM_12 | |
| 35 |
RKCANFD_QUIRK_CANFD_BROKEN, |
| 36 |
}; |
| 37 |
|
| 38 |
/* The rk3568 CAN-FD errata sheet as of Tue 07 Nov 2023 11:25:31 +08:00 |
| 39 |
* states that only the rk3568v2 is affected by erratum 5, but tests |
| 40 |
* with the rk3568v2 and rk3568v3 show that the RX_FIFO_CNT is |
| 41 |
* sometimes too high. In contrast to the errata sheet mark rk3568v3 |
| 42 |
* as effected by erratum 5, too. |
| 43 |
*/ |
| 44 |
static const struct rkcanfd_devtype_data rkcanfd_devtype_data_rk3568v3 = { |
| 45 |
.model = RKCANFD_MODEL_RK3568V3, |
| 46 |
.quirks = RKCANFD_QUIRK_RK3568_ERRATUM_1 | RKCANFD_QUIRK_RK3568_ERRATUM_2 | |
| 47 |
RKCANFD_QUIRK_RK3568_ERRATUM_5 | RKCANFD_QUIRK_RK3568_ERRATUM_7 | |
| 48 |
RKCANFD_QUIRK_RK3568_ERRATUM_8 | RKCANFD_QUIRK_RK3568_ERRATUM_10 | |
| 49 |
RKCANFD_QUIRK_RK3568_ERRATUM_11 | RKCANFD_QUIRK_RK3568_ERRATUM_12 | |
| 50 |
RKCANFD_QUIRK_CANFD_BROKEN, |
| 51 |
}; |
| 52 |
|
| 53 |
static const char *__rkcanfd_get_model_str(enum rkcanfd_model model) |
| 54 |
{ |
| 55 |
switch (model) { |
| 56 |
case RKCANFD_MODEL_RK3568V2: |
| 57 |
return "rk3568v2"; |
| 58 |
case RKCANFD_MODEL_RK3568V3: |
| 59 |
return "rk3568v3"; |
| 60 |
} |
| 61 |
|
| 62 |
return "<unknown>"; |
| 63 |
} |
| 64 |
|
| 65 |
static inline const char * |
| 66 |
rkcanfd_get_model_str(const struct rkcanfd_priv *priv) |
| 67 |
{ |
| 68 |
return __rkcanfd_get_model_str(priv->devtype_data.model); |
| 69 |
} |
| 70 |
|
| 71 |
/* Note: |
| 72 |
* |
| 73 |
* The formula to calculate the CAN System Clock is: |
| 74 |
* |
| 75 |
* Tsclk = 2 x Tclk x (brp + 1) |
| 76 |
* |
| 77 |
* Double the data sheet's brp_min, brp_max and brp_inc values (both |
| 78 |
* for the arbitration and data bit timing) to take the "2 x" into |
| 79 |
* account. |
| 80 |
*/ |
| 81 |
static const struct can_bittiming_const rkcanfd_bittiming_const = { |
| 82 |
.name = DEVICE_NAME, |
| 83 |
.tseg1_min = 1, |
| 84 |
.tseg1_max = 256, |
| 85 |
.tseg2_min = 1, |
| 86 |
.tseg2_max = 128, |
| 87 |
.sjw_max = 128, |
| 88 |
.brp_min = 2, /* value from data sheet x2 */ |
| 89 |
.brp_max = 512, /* value from data sheet x2 */ |
| 90 |
.brp_inc = 2, /* value from data sheet x2 */ |
| 91 |
}; |
| 92 |
|
| 93 |
static const struct can_bittiming_const rkcanfd_data_bittiming_const = { |
| 94 |
.name = DEVICE_NAME, |
| 95 |
.tseg1_min = 1, |
| 96 |
.tseg1_max = 32, |
| 97 |
.tseg2_min = 1, |
| 98 |
.tseg2_max = 16, |
| 99 |
.sjw_max = 16, |
| 100 |
.brp_min = 2, /* value from data sheet x2 */ |
| 101 |
.brp_max = 512, /* value from data sheet x2 */ |
| 102 |
.brp_inc = 2, /* value from data sheet x2 */ |
| 103 |
}; |
| 104 |
|
| 105 |
static void rkcanfd_chip_set_reset_mode(const struct rkcanfd_priv *priv) |
| 106 |
{ |
| 107 |
reset_control_assert(priv->reset); |
| 108 |
udelay(2); |
| 109 |
reset_control_deassert(priv->reset); |
| 110 |
|
| 111 |
rkcanfd_write(priv, RKCANFD_REG_MODE, 0x0); |
| 112 |
} |
| 113 |
|
| 114 |
static void rkcanfd_chip_set_work_mode(const struct rkcanfd_priv *priv) |
| 115 |
{ |
| 116 |
rkcanfd_write(priv, RKCANFD_REG_MODE, priv->reg_mode_default); |
| 117 |
} |
| 118 |
|
| 119 |
static int rkcanfd_set_bittiming(struct rkcanfd_priv *priv) |
| 120 |
{ |
| 121 |
const struct can_bittiming *dbt = &priv->can.fd.data_bittiming; |
| 122 |
const struct can_bittiming *bt = &priv->can.bittiming; |
| 123 |
u32 reg_nbt, reg_dbt, reg_tdc; |
| 124 |
u32 tdco; |
| 125 |
|
| 126 |
reg_nbt = FIELD_PREP(RKCANFD_REG_FD_NOMINAL_BITTIMING_SJW, |
| 127 |
bt->sjw - 1) | |
| 128 |
FIELD_PREP(RKCANFD_REG_FD_NOMINAL_BITTIMING_BRP, |
| 129 |
(bt->brp / 2) - 1) | |
| 130 |
FIELD_PREP(RKCANFD_REG_FD_NOMINAL_BITTIMING_TSEG2, |
| 131 |
bt->phase_seg2 - 1) | |
| 132 |
FIELD_PREP(RKCANFD_REG_FD_NOMINAL_BITTIMING_TSEG1, |
| 133 |
bt->prop_seg + bt->phase_seg1 - 1); |
| 134 |
|
| 135 |
rkcanfd_write(priv, RKCANFD_REG_FD_NOMINAL_BITTIMING, reg_nbt); |
| 136 |
|
| 137 |
if (!(priv->can.ctrlmode & CAN_CTRLMODE_FD)) |
| 138 |
return 0; |
| 139 |
|
| 140 |
reg_dbt = FIELD_PREP(RKCANFD_REG_FD_DATA_BITTIMING_SJW, |
| 141 |
dbt->sjw - 1) | |
| 142 |
FIELD_PREP(RKCANFD_REG_FD_DATA_BITTIMING_BRP, |
| 143 |
(dbt->brp / 2) - 1) | |
| 144 |
FIELD_PREP(RKCANFD_REG_FD_DATA_BITTIMING_TSEG2, |
| 145 |
dbt->phase_seg2 - 1) | |
| 146 |
FIELD_PREP(RKCANFD_REG_FD_DATA_BITTIMING_TSEG1, |
| 147 |
dbt->prop_seg + dbt->phase_seg1 - 1); |
| 148 |
|
| 149 |
rkcanfd_write(priv, RKCANFD_REG_FD_DATA_BITTIMING, reg_dbt); |
| 150 |
|
| 151 |
tdco = (priv->can.clock.freq / dbt->bitrate) * 2 / 3; |
| 152 |
tdco = min(tdco, FIELD_MAX(RKCANFD_REG_TRANSMIT_DELAY_COMPENSATION_TDC_OFFSET)); |
| 153 |
|
| 154 |
reg_tdc = FIELD_PREP(RKCANFD_REG_TRANSMIT_DELAY_COMPENSATION_TDC_OFFSET, tdco) | |
| 155 |
RKCANFD_REG_TRANSMIT_DELAY_COMPENSATION_TDC_ENABLE; |
| 156 |
rkcanfd_write(priv, RKCANFD_REG_TRANSMIT_DELAY_COMPENSATION, |
| 157 |
reg_tdc); |
| 158 |
|
| 159 |
return 0; |
| 160 |
} |
| 161 |
|
| 162 |
static void rkcanfd_get_berr_counter_corrected(struct rkcanfd_priv *priv, |
| 163 |
struct can_berr_counter *bec) |
| 164 |
{ |
| 165 |
struct can_berr_counter bec_raw; |
| 166 |
u32 reg_state; |
| 167 |
|
| 168 |
bec->rxerr = rkcanfd_read(priv, RKCANFD_REG_RXERRORCNT); |
| 169 |
bec->txerr = rkcanfd_read(priv, RKCANFD_REG_TXERRORCNT); |
| 170 |
bec_raw = *bec; |
| 171 |
|
| 172 |
/* Tests show that sometimes both CAN bus error counters read |
| 173 |
* 0x0, even if the controller is in warning mode |
| 174 |
* (RKCANFD_REG_STATE_ERROR_WARNING_STATE in RKCANFD_REG_STATE |
| 175 |
* set). |
| 176 |
* |
| 177 |
* In case both error counters read 0x0, use the struct |
| 178 |
* priv->bec, otherwise save the read value to priv->bec. |
| 179 |
* |
| 180 |
* rkcanfd_handle_rx_int_one() handles the decrementing of |
| 181 |
* priv->bec.rxerr for successfully RX'ed CAN frames. |
| 182 |
* |
| 183 |
* Luckily the controller doesn't decrement the RX CAN bus |
| 184 |
* error counter in hardware for self received TX'ed CAN |
| 185 |
* frames (RKCANFD_REG_MODE_RXSTX_MODE), so RXSTX doesn't |
| 186 |
* interfere with proper RX CAN bus error counters. |
| 187 |
* |
| 188 |
* rkcanfd_handle_tx_done_one() handles the decrementing of |
| 189 |
* priv->bec.txerr for successfully TX'ed CAN frames. |
| 190 |
*/ |
| 191 |
if (!bec->rxerr && !bec->txerr) |
| 192 |
*bec = priv->bec; |
| 193 |
else |
| 194 |
priv->bec = *bec; |
| 195 |
|
| 196 |
reg_state = rkcanfd_read(priv, RKCANFD_REG_STATE); |
| 197 |
netdev_vdbg(priv->ndev, |
| 198 |
"%s: Raw/Cor: txerr=%3u/%3u rxerr=%3u/%3u Bus Off=%u Warning=%u\n", |
| 199 |
__func__, |
| 200 |
bec_raw.txerr, bec->txerr, bec_raw.rxerr, bec->rxerr, |
| 201 |
!!(reg_state & RKCANFD_REG_STATE_BUS_OFF_STATE), |
| 202 |
!!(reg_state & RKCANFD_REG_STATE_ERROR_WARNING_STATE)); |
| 203 |
} |
| 204 |
|
| 205 |
static int rkcanfd_get_berr_counter(const struct net_device *ndev, |
| 206 |
struct can_berr_counter *bec) |
| 207 |
{ |
| 208 |
struct rkcanfd_priv *priv = netdev_priv(ndev); |
| 209 |
int err; |
| 210 |
|
| 211 |
err = pm_runtime_resume_and_get(ndev->dev.parent); |
| 212 |
if (err) |
| 213 |
return err; |
| 214 |
|
| 215 |
rkcanfd_get_berr_counter_corrected(priv, bec); |
| 216 |
|
| 217 |
pm_runtime_put(ndev->dev.parent); |
| 218 |
|
| 219 |
return 0; |
| 220 |
} |
| 221 |
|
| 222 |
static void rkcanfd_chip_interrupts_enable(const struct rkcanfd_priv *priv) |
| 223 |
{ |
| 224 |
rkcanfd_write(priv, RKCANFD_REG_INT_MASK, priv->reg_int_mask_default); |
| 225 |
|
| 226 |
netdev_dbg(priv->ndev, "%s: reg_int_mask=0x%08x\n", __func__, |
| 227 |
rkcanfd_read(priv, RKCANFD_REG_INT_MASK)); |
| 228 |
} |
| 229 |
|
| 230 |
static void rkcanfd_chip_interrupts_disable(const struct rkcanfd_priv *priv) |
| 231 |
{ |
| 232 |
rkcanfd_write(priv, RKCANFD_REG_INT_MASK, RKCANFD_REG_INT_ALL); |
| 233 |
} |
| 234 |
|
| 235 |
static void rkcanfd_chip_fifo_setup(struct rkcanfd_priv *priv) |
| 236 |
{ |
| 237 |
u32 reg; |
| 238 |
|
| 239 |
/* RX FIFO */ |
| 240 |
reg = rkcanfd_read(priv, RKCANFD_REG_RX_FIFO_CTRL); |
| 241 |
reg |= RKCANFD_REG_RX_FIFO_CTRL_RX_FIFO_ENABLE; |
| 242 |
rkcanfd_write(priv, RKCANFD_REG_RX_FIFO_CTRL, reg); |
| 243 |
|
| 244 |
WRITE_ONCE(priv->tx_head, 0); |
| 245 |
WRITE_ONCE(priv->tx_tail, 0); |
| 246 |
netdev_reset_queue(priv->ndev); |
| 247 |
} |
| 248 |
|
| 249 |
static void rkcanfd_chip_start(struct rkcanfd_priv *priv) |
| 250 |
{ |
| 251 |
u32 reg; |
| 252 |
|
| 253 |
rkcanfd_chip_set_reset_mode(priv); |
| 254 |
|
| 255 |
/* Receiving Filter: accept all */ |
| 256 |
rkcanfd_write(priv, RKCANFD_REG_IDCODE, 0x0); |
| 257 |
rkcanfd_write(priv, RKCANFD_REG_IDMASK, RKCANFD_REG_IDCODE_EXTENDED_FRAME_ID); |
| 258 |
|
| 259 |
/* enable: |
| 260 |
* - CAN_FD: enable CAN-FD |
| 261 |
* - AUTO_RETX_MODE: auto retransmission on TX error |
| 262 |
* - COVER_MODE: RX-FIFO overwrite mode, do not send OVERLOAD frames |
| 263 |
* - RXSTX_MODE: Receive Self Transmit data mode |
| 264 |
* - WORK_MODE: transition from reset to working mode |
| 265 |
*/ |
| 266 |
reg = rkcanfd_read(priv, RKCANFD_REG_MODE); |
| 267 |
priv->reg_mode_default = reg | |
| 268 |
RKCANFD_REG_MODE_CAN_FD_MODE_ENABLE | |
| 269 |
RKCANFD_REG_MODE_AUTO_RETX_MODE | |
| 270 |
RKCANFD_REG_MODE_COVER_MODE | |
| 271 |
RKCANFD_REG_MODE_RXSTX_MODE | |
| 272 |
RKCANFD_REG_MODE_WORK_MODE; |
| 273 |
|
| 274 |
if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) |
| 275 |
priv->reg_mode_default |= RKCANFD_REG_MODE_LBACK_MODE | |
| 276 |
RKCANFD_REG_MODE_SILENT_MODE | |
| 277 |
RKCANFD_REG_MODE_SELF_TEST; |
| 278 |
|
| 279 |
/* mask, i.e. ignore: |
| 280 |
* - TIMESTAMP_COUNTER_OVERFLOW_INT - timestamp counter overflow interrupt |
| 281 |
* - TX_ARBIT_FAIL_INT - TX arbitration fail interrupt |
| 282 |
* - OVERLOAD_INT - CAN bus overload interrupt |
| 283 |
* - TX_FINISH_INT - Transmit finish interrupt |
| 284 |
*/ |
| 285 |
priv->reg_int_mask_default = |
| 286 |
RKCANFD_REG_INT_TIMESTAMP_COUNTER_OVERFLOW_INT | |
| 287 |
RKCANFD_REG_INT_TX_ARBIT_FAIL_INT | |
| 288 |
RKCANFD_REG_INT_OVERLOAD_INT | |
| 289 |
RKCANFD_REG_INT_TX_FINISH_INT; |
| 290 |
|
| 291 |
/* Do not mask the bus error interrupt if the bus error |
| 292 |
* reporting is requested. |
| 293 |
*/ |
| 294 |
if (!(priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)) |
| 295 |
priv->reg_int_mask_default |= RKCANFD_REG_INT_ERROR_INT; |
| 296 |
|
| 297 |
memset(&priv->bec, 0x0, sizeof(priv->bec)); |
| 298 |
|
| 299 |
rkcanfd_chip_fifo_setup(priv); |
| 300 |
rkcanfd_timestamp_init(priv); |
| 301 |
rkcanfd_timestamp_start(priv); |
| 302 |
|
| 303 |
rkcanfd_set_bittiming(priv); |
| 304 |
|
| 305 |
rkcanfd_chip_interrupts_disable(priv); |
| 306 |
rkcanfd_chip_set_work_mode(priv); |
| 307 |
|
| 308 |
priv->can.state = CAN_STATE_ERROR_ACTIVE; |
| 309 |
|
| 310 |
netdev_dbg(priv->ndev, "%s: reg_mode=0x%08x\n", __func__, |
| 311 |
rkcanfd_read(priv, RKCANFD_REG_MODE)); |
| 312 |
} |
| 313 |
|
| 314 |
static void __rkcanfd_chip_stop(struct rkcanfd_priv *priv, const enum can_state state) |
| 315 |
{ |
| 316 |
priv->can.state = state; |
| 317 |
|
| 318 |
rkcanfd_chip_set_reset_mode(priv); |
| 319 |
rkcanfd_chip_interrupts_disable(priv); |
| 320 |
} |
| 321 |
|
| 322 |
static void rkcanfd_chip_stop(struct rkcanfd_priv *priv, const enum can_state state) |
| 323 |
{ |
| 324 |
priv->can.state = state; |
| 325 |
|
| 326 |
rkcanfd_timestamp_stop(priv); |
| 327 |
__rkcanfd_chip_stop(priv, state); |
| 328 |
} |
| 329 |
|
| 330 |
static void rkcanfd_chip_stop_sync(struct rkcanfd_priv *priv, const enum can_state state) |
| 331 |
{ |
| 332 |
priv->can.state = state; |
| 333 |
|
| 334 |
rkcanfd_timestamp_stop_sync(priv); |
| 335 |
__rkcanfd_chip_stop(priv, state); |
| 336 |
} |
| 337 |
|
| 338 |
static int rkcanfd_set_mode(struct net_device *ndev, |
| 339 |
enum can_mode mode) |
| 340 |
{ |
| 341 |
struct rkcanfd_priv *priv = netdev_priv(ndev); |
| 342 |
|
| 343 |
switch (mode) { |
| 344 |
case CAN_MODE_START: |
| 345 |
rkcanfd_chip_start(priv); |
| 346 |
rkcanfd_chip_interrupts_enable(priv); |
| 347 |
netif_wake_queue(ndev); |
| 348 |
break; |
| 349 |
|
| 350 |
default: |
| 351 |
return -EOPNOTSUPP; |
| 352 |
} |
| 353 |
|
| 354 |
return 0; |
| 355 |
} |
| 356 |
|
| 357 |
static struct sk_buff * |
| 358 |
rkcanfd_alloc_can_err_skb(struct rkcanfd_priv *priv, |
| 359 |
struct can_frame **cf, u32 *timestamp) |
| 360 |
{ |
| 361 |
struct sk_buff *skb; |
| 362 |
|
| 363 |
*timestamp = rkcanfd_get_timestamp(priv); |
| 364 |
|
| 365 |
skb = alloc_can_err_skb(priv->ndev, cf); |
| 366 |
if (skb) |
| 367 |
rkcanfd_skb_set_timestamp(priv, skb, *timestamp); |
| 368 |
|
| 369 |
return skb; |
| 370 |
} |
| 371 |
|
| 372 |
static const char *rkcanfd_get_error_type_str(unsigned int type) |
| 373 |
{ |
| 374 |
switch (type) { |
| 375 |
case RKCANFD_REG_ERROR_CODE_TYPE_BIT: |
| 376 |
return "Bit"; |
| 377 |
case RKCANFD_REG_ERROR_CODE_TYPE_STUFF: |
| 378 |
return "Stuff"; |
| 379 |
case RKCANFD_REG_ERROR_CODE_TYPE_FORM: |
| 380 |
return "Form"; |
| 381 |
case RKCANFD_REG_ERROR_CODE_TYPE_ACK: |
| 382 |
return "ACK"; |
| 383 |
case RKCANFD_REG_ERROR_CODE_TYPE_CRC: |
| 384 |
return "CRC"; |
| 385 |
} |
| 386 |
|
| 387 |
return "<unknown>"; |
| 388 |
} |
| 389 |
|
| 390 |
#define RKCAN_ERROR_CODE(reg_ec, code) \ |
| 391 |
((reg_ec) & RKCANFD_REG_ERROR_CODE_##code ? __stringify(code) " " : "") |
| 392 |
|
| 393 |
static void |
| 394 |
rkcanfd_handle_error_int_reg_ec(struct rkcanfd_priv *priv, struct can_frame *cf, |
| 395 |
const u32 reg_ec) |
| 396 |
{ |
| 397 |
struct net_device_stats *stats = &priv->ndev->stats; |
| 398 |
unsigned int type; |
| 399 |
u32 reg_state, reg_cmd; |
| 400 |
|
| 401 |
type = FIELD_GET(RKCANFD_REG_ERROR_CODE_TYPE, reg_ec); |
| 402 |
reg_cmd = rkcanfd_read(priv, RKCANFD_REG_CMD); |
| 403 |
reg_state = rkcanfd_read(priv, RKCANFD_REG_STATE); |
| 404 |
|
| 405 |
netdev_dbg(priv->ndev, "%s Error in %s %s Phase: %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s(0x%08x) CMD=%u RX=%u TX=%u Error-Warning=%u Bus-Off=%u\n", |
| 406 |
rkcanfd_get_error_type_str(type), |
| 407 |
reg_ec & RKCANFD_REG_ERROR_CODE_DIRECTION_RX ? "RX" : "TX", |
| 408 |
reg_ec & RKCANFD_REG_ERROR_CODE_PHASE ? "Data" : "Arbitration", |
| 409 |
RKCAN_ERROR_CODE(reg_ec, TX_OVERLOAD), |
| 410 |
RKCAN_ERROR_CODE(reg_ec, TX_ERROR), |
| 411 |
RKCAN_ERROR_CODE(reg_ec, TX_ACK), |
| 412 |
RKCAN_ERROR_CODE(reg_ec, TX_ACK_EOF), |
| 413 |
RKCAN_ERROR_CODE(reg_ec, TX_CRC), |
| 414 |
RKCAN_ERROR_CODE(reg_ec, TX_STUFF_COUNT), |
| 415 |
RKCAN_ERROR_CODE(reg_ec, TX_DATA), |
| 416 |
RKCAN_ERROR_CODE(reg_ec, TX_SOF_DLC), |
| 417 |
RKCAN_ERROR_CODE(reg_ec, TX_IDLE), |
| 418 |
RKCAN_ERROR_CODE(reg_ec, RX_BUF_INT), |
| 419 |
RKCAN_ERROR_CODE(reg_ec, RX_SPACE), |
| 420 |
RKCAN_ERROR_CODE(reg_ec, RX_EOF), |
| 421 |
RKCAN_ERROR_CODE(reg_ec, RX_ACK_LIM), |
| 422 |
RKCAN_ERROR_CODE(reg_ec, RX_ACK), |
| 423 |
RKCAN_ERROR_CODE(reg_ec, RX_CRC_LIM), |
| 424 |
RKCAN_ERROR_CODE(reg_ec, RX_CRC), |
| 425 |
RKCAN_ERROR_CODE(reg_ec, RX_STUFF_COUNT), |
| 426 |
RKCAN_ERROR_CODE(reg_ec, RX_DATA), |
| 427 |
RKCAN_ERROR_CODE(reg_ec, RX_DLC), |
| 428 |
RKCAN_ERROR_CODE(reg_ec, RX_BRS_ESI), |
| 429 |
RKCAN_ERROR_CODE(reg_ec, RX_RES), |
| 430 |
RKCAN_ERROR_CODE(reg_ec, RX_FDF), |
| 431 |
RKCAN_ERROR_CODE(reg_ec, RX_ID2_RTR), |
| 432 |
RKCAN_ERROR_CODE(reg_ec, RX_SOF_IDE), |
| 433 |
RKCAN_ERROR_CODE(reg_ec, RX_IDLE), |
| 434 |
reg_ec, reg_cmd, |
| 435 |
!!(reg_state & RKCANFD_REG_STATE_RX_PERIOD), |
| 436 |
!!(reg_state & RKCANFD_REG_STATE_TX_PERIOD), |
| 437 |
!!(reg_state & RKCANFD_REG_STATE_ERROR_WARNING_STATE), |
| 438 |
!!(reg_state & RKCANFD_REG_STATE_BUS_OFF_STATE)); |
| 439 |
|
| 440 |
priv->can.can_stats.bus_error++; |
| 441 |
|
| 442 |
if (reg_ec & RKCANFD_REG_ERROR_CODE_DIRECTION_RX) |
| 443 |
stats->rx_errors++; |
| 444 |
else |
| 445 |
stats->tx_errors++; |
| 446 |
|
| 447 |
if (!cf) |
| 448 |
return; |
| 449 |
|
| 450 |
if (reg_ec & RKCANFD_REG_ERROR_CODE_DIRECTION_RX) { |
| 451 |
if (reg_ec & RKCANFD_REG_ERROR_CODE_RX_SOF_IDE) |
| 452 |
cf->data[3] = CAN_ERR_PROT_LOC_SOF; |
| 453 |
else if (reg_ec & RKCANFD_REG_ERROR_CODE_RX_ID2_RTR) |
| 454 |
cf->data[3] = CAN_ERR_PROT_LOC_RTR; |
| 455 |
/* RKCANFD_REG_ERROR_CODE_RX_FDF */ |
| 456 |
else if (reg_ec & RKCANFD_REG_ERROR_CODE_RX_RES) |
| 457 |
cf->data[3] = CAN_ERR_PROT_LOC_RES0; |
| 458 |
/* RKCANFD_REG_ERROR_CODE_RX_BRS_ESI */ |
| 459 |
else if (reg_ec & RKCANFD_REG_ERROR_CODE_RX_DLC) |
| 460 |
cf->data[3] = CAN_ERR_PROT_LOC_DLC; |
| 461 |
else if (reg_ec & RKCANFD_REG_ERROR_CODE_RX_DATA) |
| 462 |
cf->data[3] = CAN_ERR_PROT_LOC_DATA; |
| 463 |
/* RKCANFD_REG_ERROR_CODE_RX_STUFF_COUNT */ |
| 464 |
else if (reg_ec & RKCANFD_REG_ERROR_CODE_RX_CRC) |
| 465 |
cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ; |
| 466 |
else if (reg_ec & RKCANFD_REG_ERROR_CODE_RX_CRC_LIM) |
| 467 |
cf->data[3] = CAN_ERR_PROT_LOC_ACK_DEL; |
| 468 |
else if (reg_ec & RKCANFD_REG_ERROR_CODE_RX_ACK) |
| 469 |
cf->data[3] = CAN_ERR_PROT_LOC_ACK; |
| 470 |
else if (reg_ec & RKCANFD_REG_ERROR_CODE_RX_ACK_LIM) |
| 471 |
cf->data[3] = CAN_ERR_PROT_LOC_ACK_DEL; |
| 472 |
else if (reg_ec & RKCANFD_REG_ERROR_CODE_RX_EOF) |
| 473 |
cf->data[3] = CAN_ERR_PROT_LOC_EOF; |
| 474 |
else if (reg_ec & RKCANFD_REG_ERROR_CODE_RX_SPACE) |
| 475 |
cf->data[3] = CAN_ERR_PROT_LOC_EOF; |
| 476 |
else if (reg_ec & RKCANFD_REG_ERROR_CODE_RX_BUF_INT) |
| 477 |
cf->data[3] = CAN_ERR_PROT_LOC_INTERM; |
| 478 |
} else { |
| 479 |
cf->data[2] |= CAN_ERR_PROT_TX; |
| 480 |
|
| 481 |
if (reg_ec & RKCANFD_REG_ERROR_CODE_TX_SOF_DLC) |
| 482 |
cf->data[3] = CAN_ERR_PROT_LOC_SOF; |
| 483 |
else if (reg_ec & RKCANFD_REG_ERROR_CODE_TX_DATA) |
| 484 |
cf->data[3] = CAN_ERR_PROT_LOC_DATA; |
| 485 |
/* RKCANFD_REG_ERROR_CODE_TX_STUFF_COUNT */ |
| 486 |
else if (reg_ec & RKCANFD_REG_ERROR_CODE_TX_CRC) |
| 487 |
cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ; |
| 488 |
else if (reg_ec & RKCANFD_REG_ERROR_CODE_TX_ACK_EOF) |
| 489 |
cf->data[3] = CAN_ERR_PROT_LOC_ACK_DEL; |
| 490 |
else if (reg_ec & RKCANFD_REG_ERROR_CODE_TX_ACK) |
| 491 |
cf->data[3] = CAN_ERR_PROT_LOC_ACK; |
| 492 |
/* RKCANFD_REG_ERROR_CODE_TX_ERROR */ |
| 493 |
else if (reg_ec & RKCANFD_REG_ERROR_CODE_TX_OVERLOAD) |
| 494 |
cf->data[2] |= CAN_ERR_PROT_OVERLOAD; |
| 495 |
} |
| 496 |
|
| 497 |
switch (reg_ec & RKCANFD_REG_ERROR_CODE_TYPE) { |
| 498 |
case FIELD_PREP_CONST(RKCANFD_REG_ERROR_CODE_TYPE, |
| 499 |
RKCANFD_REG_ERROR_CODE_TYPE_BIT): |
| 500 |
|
| 501 |
cf->data[2] |= CAN_ERR_PROT_BIT; |
| 502 |
break; |
| 503 |
case FIELD_PREP_CONST(RKCANFD_REG_ERROR_CODE_TYPE, |
| 504 |
RKCANFD_REG_ERROR_CODE_TYPE_STUFF): |
| 505 |
cf->data[2] |= CAN_ERR_PROT_STUFF; |
| 506 |
break; |
| 507 |
case FIELD_PREP_CONST(RKCANFD_REG_ERROR_CODE_TYPE, |
| 508 |
RKCANFD_REG_ERROR_CODE_TYPE_FORM): |
| 509 |
cf->data[2] |= CAN_ERR_PROT_FORM; |
| 510 |
break; |
| 511 |
case FIELD_PREP_CONST(RKCANFD_REG_ERROR_CODE_TYPE, |
| 512 |
RKCANFD_REG_ERROR_CODE_TYPE_ACK): |
| 513 |
cf->can_id |= CAN_ERR_ACK; |
| 514 |
break; |
| 515 |
case FIELD_PREP_CONST(RKCANFD_REG_ERROR_CODE_TYPE, |
| 516 |
RKCANFD_REG_ERROR_CODE_TYPE_CRC): |
| 517 |
cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ; |
| 518 |
break; |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
static int rkcanfd_handle_error_int(struct rkcanfd_priv *priv) |
| 523 |
{ |
| 524 |
struct net_device_stats *stats = &priv->ndev->stats; |
| 525 |
struct can_frame *cf = NULL; |
| 526 |
u32 reg_ec, timestamp; |
| 527 |
struct sk_buff *skb; |
| 528 |
int err; |
| 529 |
|
| 530 |
reg_ec = rkcanfd_read(priv, RKCANFD_REG_ERROR_CODE); |
| 531 |
|
| 532 |
if (!reg_ec) |
| 533 |
return 0; |
| 534 |
|
| 535 |
if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) { |
| 536 |
skb = rkcanfd_alloc_can_err_skb(priv, &cf, ×tamp); |
| 537 |
if (cf) { |
| 538 |
struct can_berr_counter bec; |
| 539 |
|
| 540 |
rkcanfd_get_berr_counter_corrected(priv, &bec); |
| 541 |
cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR | CAN_ERR_CNT; |
| 542 |
cf->data[6] = bec.txerr; |
| 543 |
cf->data[7] = bec.rxerr; |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
rkcanfd_handle_error_int_reg_ec(priv, cf, reg_ec); |
| 548 |
|
| 549 |
if (!cf) |
| 550 |
return 0; |
| 551 |
|
| 552 |
err = can_rx_offload_queue_timestamp(&priv->offload, skb, timestamp); |
| 553 |
if (err) |
| 554 |
stats->rx_fifo_errors++; |
| 555 |
|
| 556 |
return 0; |
| 557 |
} |
| 558 |
|
| 559 |
static int rkcanfd_handle_state_error_int(struct rkcanfd_priv *priv) |
| 560 |
{ |
| 561 |
struct net_device_stats *stats = &priv->ndev->stats; |
| 562 |
enum can_state new_state, rx_state, tx_state; |
| 563 |
struct net_device *ndev = priv->ndev; |
| 564 |
struct can_berr_counter bec; |
| 565 |
struct can_frame *cf = NULL; |
| 566 |
struct sk_buff *skb; |
| 567 |
u32 timestamp; |
| 568 |
int err; |
| 569 |
|
| 570 |
rkcanfd_get_berr_counter_corrected(priv, &bec); |
| 571 |
can_state_get_by_berr_counter(ndev, &bec, &tx_state, &rx_state); |
| 572 |
|
| 573 |
new_state = max(tx_state, rx_state); |
| 574 |
if (new_state == priv->can.state) |
| 575 |
return 0; |
| 576 |
|
| 577 |
/* The skb allocation might fail, but can_change_state() |
| 578 |
* handles cf == NULL. |
| 579 |
*/ |
| 580 |
skb = rkcanfd_alloc_can_err_skb(priv, &cf, ×tamp); |
| 581 |
can_change_state(ndev, cf, tx_state, rx_state); |
| 582 |
|
| 583 |
if (new_state == CAN_STATE_BUS_OFF) { |
| 584 |
rkcanfd_chip_stop(priv, CAN_STATE_BUS_OFF); |
| 585 |
can_bus_off(ndev); |
| 586 |
} |
| 587 |
|
| 588 |
if (!skb) |
| 589 |
return 0; |
| 590 |
|
| 591 |
if (new_state != CAN_STATE_BUS_OFF) { |
| 592 |
cf->can_id |= CAN_ERR_CNT; |
| 593 |
cf->data[6] = bec.txerr; |
| 594 |
cf->data[7] = bec.rxerr; |
| 595 |
} |
| 596 |
|
| 597 |
err = can_rx_offload_queue_timestamp(&priv->offload, skb, timestamp); |
| 598 |
if (err) |
| 599 |
stats->rx_fifo_errors++; |
| 600 |
|
| 601 |
return 0; |
| 602 |
} |
| 603 |
|
| 604 |
static int |
| 605 |
rkcanfd_handle_rx_fifo_overflow_int(struct rkcanfd_priv *priv) |
| 606 |
{ |
| 607 |
struct net_device_stats *stats = &priv->ndev->stats; |
| 608 |
struct can_berr_counter bec; |
| 609 |
struct can_frame *cf = NULL; |
| 610 |
struct sk_buff *skb; |
| 611 |
u32 timestamp; |
| 612 |
int err; |
| 613 |
|
| 614 |
stats->rx_over_errors++; |
| 615 |
stats->rx_errors++; |
| 616 |
|
| 617 |
netdev_dbg(priv->ndev, "RX-FIFO overflow\n"); |
| 618 |
|
| 619 |
skb = rkcanfd_alloc_can_err_skb(priv, &cf, ×tamp); |
| 620 |
if (!skb) |
| 621 |
return 0; |
| 622 |
|
| 623 |
rkcanfd_get_berr_counter_corrected(priv, &bec); |
| 624 |
|
| 625 |
cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT; |
| 626 |
cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; |
| 627 |
cf->data[6] = bec.txerr; |
| 628 |
cf->data[7] = bec.rxerr; |
| 629 |
|
| 630 |
err = can_rx_offload_queue_timestamp(&priv->offload, skb, timestamp); |
| 631 |
if (err) |
| 632 |
stats->rx_fifo_errors++; |
| 633 |
|
| 634 |
return 0; |
| 635 |
} |
| 636 |
|
| 637 |
#define rkcanfd_handle(priv, irq, ...) \ |
| 638 |
({ \ |
| 639 |
struct rkcanfd_priv *_priv = (priv); \ |
| 640 |
int err; \ |
| 641 |
\ |
| 642 |
err = rkcanfd_handle_##irq(_priv, ## __VA_ARGS__); \ |
| 643 |
if (err) \ |
| 644 |
netdev_err(_priv->ndev, \ |
| 645 |
"IRQ handler rkcanfd_handle_%s() returned error: %pe\n", \ |
| 646 |
__stringify(irq), ERR_PTR(err)); \ |
| 647 |
err; \ |
| 648 |
}) |
| 649 |
|
| 650 |
static irqreturn_t rkcanfd_irq(int irq, void *dev_id) |
| 651 |
{ |
| 652 |
struct rkcanfd_priv *priv = dev_id; |
| 653 |
u32 reg_int_unmasked, reg_int; |
| 654 |
|
| 655 |
reg_int_unmasked = rkcanfd_read(priv, RKCANFD_REG_INT); |
| 656 |
reg_int = reg_int_unmasked & ~priv->reg_int_mask_default; |
| 657 |
|
| 658 |
if (!reg_int) |
| 659 |
return IRQ_NONE; |
| 660 |
|
| 661 |
/* First ACK then handle, to avoid lost-IRQ race condition on |
| 662 |
* fast re-occurring interrupts. |
| 663 |
*/ |
| 664 |
rkcanfd_write(priv, RKCANFD_REG_INT, reg_int); |
| 665 |
|
| 666 |
if (reg_int & RKCANFD_REG_INT_RX_FINISH_INT) |
| 667 |
rkcanfd_handle(priv, rx_int); |
| 668 |
|
| 669 |
if (reg_int & RKCANFD_REG_INT_ERROR_INT) |
| 670 |
rkcanfd_handle(priv, error_int); |
| 671 |
|
| 672 |
if (reg_int & (RKCANFD_REG_INT_BUS_OFF_INT | |
| 673 |
RKCANFD_REG_INT_PASSIVE_ERROR_INT | |
| 674 |
RKCANFD_REG_INT_ERROR_WARNING_INT) || |
| 675 |
priv->can.state > CAN_STATE_ERROR_ACTIVE) |
| 676 |
rkcanfd_handle(priv, state_error_int); |
| 677 |
|
| 678 |
if (reg_int & RKCANFD_REG_INT_RX_FIFO_OVERFLOW_INT) |
| 679 |
rkcanfd_handle(priv, rx_fifo_overflow_int); |
| 680 |
|
| 681 |
if (reg_int & ~(RKCANFD_REG_INT_ALL_ERROR | |
| 682 |
RKCANFD_REG_INT_RX_FIFO_OVERFLOW_INT | |
| 683 |
RKCANFD_REG_INT_RX_FINISH_INT)) |
| 684 |
netdev_err(priv->ndev, "%s: int=0x%08x\n", __func__, reg_int); |
| 685 |
|
| 686 |
if (reg_int & RKCANFD_REG_INT_WAKEUP_INT) |
| 687 |
netdev_info(priv->ndev, "%s: WAKEUP_INT\n", __func__); |
| 688 |
|
| 689 |
if (reg_int & RKCANFD_REG_INT_TXE_FIFO_FULL_INT) |
| 690 |
netdev_info(priv->ndev, "%s: TXE_FIFO_FULL_INT\n", __func__); |
| 691 |
|
| 692 |
if (reg_int & RKCANFD_REG_INT_TXE_FIFO_OV_INT) |
| 693 |
netdev_info(priv->ndev, "%s: TXE_FIFO_OV_INT\n", __func__); |
| 694 |
|
| 695 |
if (reg_int & RKCANFD_REG_INT_BUS_OFF_RECOVERY_INT) |
| 696 |
netdev_info(priv->ndev, "%s: BUS_OFF_RECOVERY_INT\n", __func__); |
| 697 |
|
| 698 |
if (reg_int & RKCANFD_REG_INT_RX_FIFO_FULL_INT) |
| 699 |
netdev_info(priv->ndev, "%s: RX_FIFO_FULL_INT\n", __func__); |
| 700 |
|
| 701 |
if (reg_int & RKCANFD_REG_INT_OVERLOAD_INT) |
| 702 |
netdev_info(priv->ndev, "%s: OVERLOAD_INT\n", __func__); |
| 703 |
|
| 704 |
can_rx_offload_irq_finish(&priv->offload); |
| 705 |
|
| 706 |
return IRQ_HANDLED; |
| 707 |
} |
| 708 |
|
| 709 |
static int rkcanfd_open(struct net_device *ndev) |
| 710 |
{ |
| 711 |
struct rkcanfd_priv *priv = netdev_priv(ndev); |
| 712 |
int err; |
| 713 |
|
| 714 |
err = open_candev(ndev); |
| 715 |
if (err) |
| 716 |
return err; |
| 717 |
|
| 718 |
err = pm_runtime_resume_and_get(ndev->dev.parent); |
| 719 |
if (err) |
| 720 |
goto out_close_candev; |
| 721 |
|
| 722 |
rkcanfd_chip_start(priv); |
| 723 |
can_rx_offload_enable(&priv->offload); |
| 724 |
|
| 725 |
err = request_irq(ndev->irq, rkcanfd_irq, IRQF_SHARED, ndev->name, priv); |
| 726 |
if (err) |
| 727 |
goto out_rkcanfd_chip_stop; |
| 728 |
|
| 729 |
rkcanfd_chip_interrupts_enable(priv); |
| 730 |
|
| 731 |
netif_start_queue(ndev); |
| 732 |
|
| 733 |
return 0; |
| 734 |
|
| 735 |
out_rkcanfd_chip_stop: |
| 736 |
rkcanfd_chip_stop_sync(priv, CAN_STATE_STOPPED); |
| 737 |
pm_runtime_put(ndev->dev.parent); |
| 738 |
out_close_candev: |
| 739 |
close_candev(ndev); |
| 740 |
return err; |
| 741 |
} |
| 742 |
|
| 743 |
static int rkcanfd_stop(struct net_device *ndev) |
| 744 |
{ |
| 745 |
struct rkcanfd_priv *priv = netdev_priv(ndev); |
| 746 |
|
| 747 |
netif_stop_queue(ndev); |
| 748 |
|
| 749 |
rkcanfd_chip_interrupts_disable(priv); |
| 750 |
free_irq(ndev->irq, priv); |
| 751 |
can_rx_offload_disable(&priv->offload); |
| 752 |
rkcanfd_chip_stop_sync(priv, CAN_STATE_STOPPED); |
| 753 |
close_candev(ndev); |
| 754 |
|
| 755 |
pm_runtime_put(ndev->dev.parent); |
| 756 |
|
| 757 |
return 0; |
| 758 |
} |
| 759 |
|
| 760 |
static const struct net_device_ops rkcanfd_netdev_ops = { |
| 761 |
.ndo_open = rkcanfd_open, |
| 762 |
.ndo_stop = rkcanfd_stop, |
| 763 |
.ndo_start_xmit = rkcanfd_start_xmit, |
| 764 |
}; |
| 765 |
|
| 766 |
static int __maybe_unused rkcanfd_runtime_suspend(struct device *dev) |
| 767 |
{ |
| 768 |
struct rkcanfd_priv *priv = dev_get_drvdata(dev); |
| 769 |
|
| 770 |
clk_bulk_disable_unprepare(priv->clks_num, priv->clks); |
| 771 |
|
| 772 |
return 0; |
| 773 |
} |
| 774 |
|
| 775 |
static int __maybe_unused rkcanfd_runtime_resume(struct device *dev) |
| 776 |
{ |
| 777 |
struct rkcanfd_priv *priv = dev_get_drvdata(dev); |
| 778 |
|
| 779 |
return clk_bulk_prepare_enable(priv->clks_num, priv->clks); |
| 780 |
} |
| 781 |
|
| 782 |
static void rkcanfd_register_done(const struct rkcanfd_priv *priv) |
| 783 |
{ |
| 784 |
u32 dev_id; |
| 785 |
|
| 786 |
dev_id = rkcanfd_read(priv, RKCANFD_REG_RTL_VERSION); |
| 787 |
|
| 788 |
netdev_info(priv->ndev, |
| 789 |
"Rockchip-CANFD %s rev%lu.%lu (errata 0x%04x) found\n", |
| 790 |
rkcanfd_get_model_str(priv), |
| 791 |
FIELD_GET(RKCANFD_REG_RTL_VERSION_MAJOR, dev_id), |
| 792 |
FIELD_GET(RKCANFD_REG_RTL_VERSION_MINOR, dev_id), |
| 793 |
priv->devtype_data.quirks); |
| 794 |
|
| 795 |
if (priv->devtype_data.quirks & RKCANFD_QUIRK_RK3568_ERRATUM_5 && |
| 796 |
priv->can.clock.freq < RKCANFD_ERRATUM_5_SYSCLOCK_HZ_MIN) |
| 797 |
netdev_info(priv->ndev, |
| 798 |
"Erratum 5: CAN clock frequency (%luMHz) lower than known good (%luMHz), expect degraded performance\n", |
| 799 |
priv->can.clock.freq / MEGA, |
| 800 |
RKCANFD_ERRATUM_5_SYSCLOCK_HZ_MIN / MEGA); |
| 801 |
} |
| 802 |
|
| 803 |
static int rkcanfd_register(struct rkcanfd_priv *priv) |
| 804 |
{ |
| 805 |
struct net_device *ndev = priv->ndev; |
| 806 |
int err; |
| 807 |
|
| 808 |
pm_runtime_enable(ndev->dev.parent); |
| 809 |
|
| 810 |
err = pm_runtime_resume_and_get(ndev->dev.parent); |
| 811 |
if (err) |
| 812 |
goto out_pm_runtime_disable; |
| 813 |
|
| 814 |
rkcanfd_ethtool_init(priv); |
| 815 |
|
| 816 |
err = register_candev(ndev); |
| 817 |
if (err) |
| 818 |
goto out_pm_runtime_put_sync; |
| 819 |
|
| 820 |
rkcanfd_register_done(priv); |
| 821 |
|
| 822 |
pm_runtime_put(ndev->dev.parent); |
| 823 |
|
| 824 |
return 0; |
| 825 |
|
| 826 |
out_pm_runtime_put_sync: |
| 827 |
pm_runtime_put_sync(ndev->dev.parent); |
| 828 |
out_pm_runtime_disable: |
| 829 |
pm_runtime_disable(ndev->dev.parent); |
| 830 |
|
| 831 |
return err; |
| 832 |
} |
| 833 |
|
| 834 |
static inline void rkcanfd_unregister(struct rkcanfd_priv *priv) |
| 835 |
{ |
| 836 |
struct net_device *ndev = priv->ndev; |
| 837 |
|
| 838 |
unregister_candev(ndev); |
| 839 |
pm_runtime_disable(ndev->dev.parent); |
| 840 |
} |
| 841 |
|
| 842 |
static const struct of_device_id rkcanfd_of_match[] = { |
| 843 |
{ |
| 844 |
.compatible = "rockchip,rk3568v2-canfd", |
| 845 |
.data = &rkcanfd_devtype_data_rk3568v2, |
| 846 |
}, { |
| 847 |
.compatible = "rockchip,rk3568v3-canfd", |
| 848 |
.data = &rkcanfd_devtype_data_rk3568v3, |
| 849 |
}, { |
| 850 |
/* sentinel */ |
| 851 |
}, |
| 852 |
}; |
| 853 |
MODULE_DEVICE_TABLE(of, rkcanfd_of_match); |
| 854 |
|
| 855 |
static int rkcanfd_probe(struct platform_device *pdev) |
| 856 |
{ |
| 857 |
struct rkcanfd_priv *priv; |
| 858 |
struct net_device *ndev; |
| 859 |
const void *match; |
| 860 |
int err; |
| 861 |
|
| 862 |
ndev = alloc_candev(sizeof(struct rkcanfd_priv), RKCANFD_TXFIFO_DEPTH); |
| 863 |
if (!ndev) |
| 864 |
return -ENOMEM; |
| 865 |
|
| 866 |
priv = netdev_priv(ndev); |
| 867 |
|
| 868 |
ndev->irq = platform_get_irq(pdev, 0); |
| 869 |
if (ndev->irq < 0) { |
| 870 |
err = ndev->irq; |
| 871 |
goto out_free_candev; |
| 872 |
} |
| 873 |
|
| 874 |
priv->clks_num = devm_clk_bulk_get_all(&pdev->dev, &priv->clks); |
| 875 |
if (priv->clks_num < 0) { |
| 876 |
err = priv->clks_num; |
| 877 |
goto out_free_candev; |
| 878 |
} |
| 879 |
|
| 880 |
priv->regs = devm_platform_ioremap_resource(pdev, 0); |
| 881 |
if (IS_ERR(priv->regs)) { |
| 882 |
err = PTR_ERR(priv->regs); |
| 883 |
goto out_free_candev; |
| 884 |
} |
| 885 |
|
| 886 |
priv->reset = devm_reset_control_array_get_exclusive(&pdev->dev); |
| 887 |
if (IS_ERR(priv->reset)) { |
| 888 |
err = dev_err_probe(&pdev->dev, PTR_ERR(priv->reset), |
| 889 |
"Failed to get reset line\n"); |
| 890 |
goto out_free_candev; |
| 891 |
} |
| 892 |
|
| 893 |
SET_NETDEV_DEV(ndev, &pdev->dev); |
| 894 |
|
| 895 |
ndev->netdev_ops = &rkcanfd_netdev_ops; |
| 896 |
ndev->flags |= IFF_ECHO; |
| 897 |
|
| 898 |
platform_set_drvdata(pdev, priv); |
| 899 |
priv->can.clock.freq = clk_get_rate(priv->clks[0].clk); |
| 900 |
priv->can.bittiming_const = &rkcanfd_bittiming_const; |
| 901 |
priv->can.fd.data_bittiming_const = &rkcanfd_data_bittiming_const; |
| 902 |
priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK | |
| 903 |
CAN_CTRLMODE_BERR_REPORTING; |
| 904 |
priv->can.do_set_mode = rkcanfd_set_mode; |
| 905 |
priv->can.do_get_berr_counter = rkcanfd_get_berr_counter; |
| 906 |
priv->ndev = ndev; |
| 907 |
|
| 908 |
match = device_get_match_data(&pdev->dev); |
| 909 |
if (match) { |
| 910 |
priv->devtype_data = *(struct rkcanfd_devtype_data *)match; |
| 911 |
if (!(priv->devtype_data.quirks & RKCANFD_QUIRK_CANFD_BROKEN)) |
| 912 |
priv->can.ctrlmode_supported |= CAN_CTRLMODE_FD; |
| 913 |
} |
| 914 |
|
| 915 |
err = can_rx_offload_add_manual(ndev, &priv->offload, |
| 916 |
RKCANFD_NAPI_WEIGHT); |
| 917 |
if (err) |
| 918 |
goto out_free_candev; |
| 919 |
|
| 920 |
err = rkcanfd_register(priv); |
| 921 |
if (err) |
| 922 |
goto out_can_rx_offload_del; |
| 923 |
|
| 924 |
return 0; |
| 925 |
|
| 926 |
out_can_rx_offload_del: |
| 927 |
can_rx_offload_del(&priv->offload); |
| 928 |
out_free_candev: |
| 929 |
free_candev(ndev); |
| 930 |
|
| 931 |
return err; |
| 932 |
} |
| 933 |
|
| 934 |
static void rkcanfd_remove(struct platform_device *pdev) |
| 935 |
{ |
| 936 |
struct rkcanfd_priv *priv = platform_get_drvdata(pdev); |
| 937 |
struct net_device *ndev = priv->ndev; |
| 938 |
|
| 939 |
rkcanfd_unregister(priv); |
| 940 |
can_rx_offload_del(&priv->offload); |
| 941 |
free_candev(ndev); |
| 942 |
} |
| 943 |
|
| 944 |
static const struct dev_pm_ops rkcanfd_pm_ops = { |
| 945 |
SET_RUNTIME_PM_OPS(rkcanfd_runtime_suspend, |
| 946 |
rkcanfd_runtime_resume, NULL) |
| 947 |
}; |
| 948 |
|
| 949 |
static struct platform_driver rkcanfd_driver = { |
| 950 |
.driver = { |
| 951 |
.name = DEVICE_NAME, |
| 952 |
.pm = &rkcanfd_pm_ops, |
| 953 |
.of_match_table = rkcanfd_of_match, |
| 954 |
}, |
| 955 |
.probe = rkcanfd_probe, |
| 956 |
.remove = rkcanfd_remove, |
| 957 |
}; |
| 958 |
module_platform_driver(rkcanfd_driver); |
| 959 |
|
| 960 |
MODULE_AUTHOR("Marc Kleine-Budde <[email protected]>"); |
| 961 |
MODULE_DESCRIPTION("Rockchip CAN-FD Driver"); |
| 962 |
MODULE_LICENSE("GPL"); |