1 // SPDX-License-Identifier: GPL-2.0 2 // CAN bus driver for Bosch M_CAN controller 3 // Copyright (C) 2014 Freescale Semiconductor, Inc. 4 // Dong Aisheng <b29396@freescale.com> 5 // Copyright (C) 2018-19 Texas Instruments Incorporated - http://www.ti.com/ 6 7 /* Bosch M_CAN user manual can be obtained from: 8 * https://github.com/linux-can/can-doc/tree/master/m_can 9 */ 10 11 #include <linux/bitfield.h> 12 #include <linux/can/dev.h> 13 #include <linux/ethtool.h> 14 #include <linux/hrtimer.h> 15 #include <linux/interrupt.h> 16 #include <linux/io.h> 17 #include <linux/iopoll.h> 18 #include <linux/kernel.h> 19 #include <linux/module.h> 20 #include <linux/netdevice.h> 21 #include <linux/of.h> 22 #include <linux/of_device.h> 23 #include <linux/phy/phy.h> 24 #include <linux/pinctrl/consumer.h> 25 #include <linux/platform_device.h> 26 #include <linux/pm_runtime.h> 27 28 #include "m_can.h" 29 30 /* registers definition */ 31 enum m_can_reg { 32 M_CAN_CREL = 0x0, 33 M_CAN_ENDN = 0x4, 34 M_CAN_CUST = 0x8, 35 M_CAN_DBTP = 0xc, 36 M_CAN_TEST = 0x10, 37 M_CAN_RWD = 0x14, 38 M_CAN_CCCR = 0x18, 39 M_CAN_NBTP = 0x1c, 40 M_CAN_TSCC = 0x20, 41 M_CAN_TSCV = 0x24, 42 M_CAN_TOCC = 0x28, 43 M_CAN_TOCV = 0x2c, 44 M_CAN_ECR = 0x40, 45 M_CAN_PSR = 0x44, 46 /* TDCR Register only available for version >=3.1.x */ 47 M_CAN_TDCR = 0x48, 48 M_CAN_IR = 0x50, 49 M_CAN_IE = 0x54, 50 M_CAN_ILS = 0x58, 51 M_CAN_ILE = 0x5c, 52 M_CAN_GFC = 0x80, 53 M_CAN_SIDFC = 0x84, 54 M_CAN_XIDFC = 0x88, 55 M_CAN_XIDAM = 0x90, 56 M_CAN_HPMS = 0x94, 57 M_CAN_NDAT1 = 0x98, 58 M_CAN_NDAT2 = 0x9c, 59 M_CAN_RXF0C = 0xa0, 60 M_CAN_RXF0S = 0xa4, 61 M_CAN_RXF0A = 0xa8, 62 M_CAN_RXBC = 0xac, 63 M_CAN_RXF1C = 0xb0, 64 M_CAN_RXF1S = 0xb4, 65 M_CAN_RXF1A = 0xb8, 66 M_CAN_RXESC = 0xbc, 67 M_CAN_TXBC = 0xc0, 68 M_CAN_TXFQS = 0xc4, 69 M_CAN_TXESC = 0xc8, 70 M_CAN_TXBRP = 0xcc, 71 M_CAN_TXBAR = 0xd0, 72 M_CAN_TXBCR = 0xd4, 73 M_CAN_TXBTO = 0xd8, 74 M_CAN_TXBCF = 0xdc, 75 M_CAN_TXBTIE = 0xe0, 76 M_CAN_TXBCIE = 0xe4, 77 M_CAN_TXEFC = 0xf0, 78 M_CAN_TXEFS = 0xf4, 79 M_CAN_TXEFA = 0xf8, 80 }; 81 82 /* message ram configuration data length */ 83 #define MRAM_CFG_LEN 8 84 85 /* Core Release Register (CREL) */ 86 #define CREL_REL_MASK GENMASK(31, 28) 87 #define CREL_STEP_MASK GENMASK(27, 24) 88 #define CREL_SUBSTEP_MASK GENMASK(23, 20) 89 90 /* Data Bit Timing & Prescaler Register (DBTP) */ 91 #define DBTP_TDC BIT(23) 92 #define DBTP_DBRP_MASK GENMASK(20, 16) 93 #define DBTP_DTSEG1_MASK GENMASK(12, 8) 94 #define DBTP_DTSEG2_MASK GENMASK(7, 4) 95 #define DBTP_DSJW_MASK GENMASK(3, 0) 96 97 /* Transmitter Delay Compensation Register (TDCR) */ 98 #define TDCR_TDCO_MASK GENMASK(14, 8) 99 #define TDCR_TDCF_MASK GENMASK(6, 0) 100 101 /* Test Register (TEST) */ 102 #define TEST_LBCK BIT(4) 103 104 /* CC Control Register (CCCR) */ 105 #define CCCR_TXP BIT(14) 106 #define CCCR_TEST BIT(7) 107 #define CCCR_DAR BIT(6) 108 #define CCCR_MON BIT(5) 109 #define CCCR_CSR BIT(4) 110 #define CCCR_CSA BIT(3) 111 #define CCCR_ASM BIT(2) 112 #define CCCR_CCE BIT(1) 113 #define CCCR_INIT BIT(0) 114 /* for version 3.0.x */ 115 #define CCCR_CMR_MASK GENMASK(11, 10) 116 #define CCCR_CMR_CANFD 0x1 117 #define CCCR_CMR_CANFD_BRS 0x2 118 #define CCCR_CMR_CAN 0x3 119 #define CCCR_CME_MASK GENMASK(9, 8) 120 #define CCCR_CME_CAN 0 121 #define CCCR_CME_CANFD 0x1 122 #define CCCR_CME_CANFD_BRS 0x2 123 /* for version >=3.1.x */ 124 #define CCCR_EFBI BIT(13) 125 #define CCCR_PXHD BIT(12) 126 #define CCCR_BRSE BIT(9) 127 #define CCCR_FDOE BIT(8) 128 /* for version >=3.2.x */ 129 #define CCCR_NISO BIT(15) 130 /* for version >=3.3.x */ 131 #define CCCR_WMM BIT(11) 132 #define CCCR_UTSU BIT(10) 133 134 /* Nominal Bit Timing & Prescaler Register (NBTP) */ 135 #define NBTP_NSJW_MASK GENMASK(31, 25) 136 #define NBTP_NBRP_MASK GENMASK(24, 16) 137 #define NBTP_NTSEG1_MASK GENMASK(15, 8) 138 #define NBTP_NTSEG2_MASK GENMASK(6, 0) 139 140 /* Timestamp Counter Configuration Register (TSCC) */ 141 #define TSCC_TCP_MASK GENMASK(19, 16) 142 #define TSCC_TSS_MASK GENMASK(1, 0) 143 #define TSCC_TSS_DISABLE 0x0 144 #define TSCC_TSS_INTERNAL 0x1 145 #define TSCC_TSS_EXTERNAL 0x2 146 147 /* Timestamp Counter Value Register (TSCV) */ 148 #define TSCV_TSC_MASK GENMASK(15, 0) 149 150 /* Error Counter Register (ECR) */ 151 #define ECR_RP BIT(15) 152 #define ECR_REC_MASK GENMASK(14, 8) 153 #define ECR_TEC_MASK GENMASK(7, 0) 154 155 /* Protocol Status Register (PSR) */ 156 #define PSR_BO BIT(7) 157 #define PSR_EW BIT(6) 158 #define PSR_EP BIT(5) 159 #define PSR_LEC_MASK GENMASK(2, 0) 160 #define PSR_DLEC_MASK GENMASK(10, 8) 161 162 /* Interrupt Register (IR) */ 163 #define IR_ALL_INT 0xffffffff 164 165 /* Renamed bits for versions > 3.1.x */ 166 #define IR_ARA BIT(29) 167 #define IR_PED BIT(28) 168 #define IR_PEA BIT(27) 169 170 /* Bits for version 3.0.x */ 171 #define IR_STE BIT(31) 172 #define IR_FOE BIT(30) 173 #define IR_ACKE BIT(29) 174 #define IR_BE BIT(28) 175 #define IR_CRCE BIT(27) 176 #define IR_WDI BIT(26) 177 #define IR_BO BIT(25) 178 #define IR_EW BIT(24) 179 #define IR_EP BIT(23) 180 #define IR_ELO BIT(22) 181 #define IR_BEU BIT(21) 182 #define IR_BEC BIT(20) 183 #define IR_DRX BIT(19) 184 #define IR_TOO BIT(18) 185 #define IR_MRAF BIT(17) 186 #define IR_TSW BIT(16) 187 #define IR_TEFL BIT(15) 188 #define IR_TEFF BIT(14) 189 #define IR_TEFW BIT(13) 190 #define IR_TEFN BIT(12) 191 #define IR_TFE BIT(11) 192 #define IR_TCF BIT(10) 193 #define IR_TC BIT(9) 194 #define IR_HPM BIT(8) 195 #define IR_RF1L BIT(7) 196 #define IR_RF1F BIT(6) 197 #define IR_RF1W BIT(5) 198 #define IR_RF1N BIT(4) 199 #define IR_RF0L BIT(3) 200 #define IR_RF0F BIT(2) 201 #define IR_RF0W BIT(1) 202 #define IR_RF0N BIT(0) 203 #define IR_ERR_STATE (IR_BO | IR_EW | IR_EP) 204 205 /* Interrupts for version 3.0.x */ 206 #define IR_ERR_LEC_30X (IR_STE | IR_FOE | IR_ACKE | IR_BE | IR_CRCE) 207 #define IR_ERR_BUS_30X (IR_ERR_LEC_30X | IR_WDI | IR_BEU | IR_BEC | \ 208 IR_TOO | IR_MRAF | IR_TSW | IR_TEFL | IR_RF1L | \ 209 IR_RF0L) 210 #define IR_ERR_ALL_30X (IR_ERR_STATE | IR_ERR_BUS_30X) 211 212 /* Interrupts for version >= 3.1.x */ 213 #define IR_ERR_LEC_31X (IR_PED | IR_PEA) 214 #define IR_ERR_BUS_31X (IR_ERR_LEC_31X | IR_WDI | IR_BEU | IR_BEC | \ 215 IR_TOO | IR_MRAF | IR_TSW | IR_TEFL | IR_RF1L | \ 216 IR_RF0L) 217 #define IR_ERR_ALL_31X (IR_ERR_STATE | IR_ERR_BUS_31X) 218 219 /* Interrupt Line Select (ILS) */ 220 #define ILS_ALL_INT0 0x0 221 #define ILS_ALL_INT1 0xFFFFFFFF 222 223 /* Interrupt Line Enable (ILE) */ 224 #define ILE_EINT1 BIT(1) 225 #define ILE_EINT0 BIT(0) 226 227 /* Rx FIFO 0/1 Configuration (RXF0C/RXF1C) */ 228 #define RXFC_FWM_MASK GENMASK(30, 24) 229 #define RXFC_FS_MASK GENMASK(22, 16) 230 231 /* Rx FIFO 0/1 Status (RXF0S/RXF1S) */ 232 #define RXFS_RFL BIT(25) 233 #define RXFS_FF BIT(24) 234 #define RXFS_FPI_MASK GENMASK(21, 16) 235 #define RXFS_FGI_MASK GENMASK(13, 8) 236 #define RXFS_FFL_MASK GENMASK(6, 0) 237 238 /* Rx Buffer / FIFO Element Size Configuration (RXESC) */ 239 #define RXESC_RBDS_MASK GENMASK(10, 8) 240 #define RXESC_F1DS_MASK GENMASK(6, 4) 241 #define RXESC_F0DS_MASK GENMASK(2, 0) 242 #define RXESC_64B 0x7 243 244 /* Tx Buffer Configuration (TXBC) */ 245 #define TXBC_TFQS_MASK GENMASK(29, 24) 246 #define TXBC_NDTB_MASK GENMASK(21, 16) 247 248 /* Tx FIFO/Queue Status (TXFQS) */ 249 #define TXFQS_TFQF BIT(21) 250 #define TXFQS_TFQPI_MASK GENMASK(20, 16) 251 #define TXFQS_TFGI_MASK GENMASK(12, 8) 252 #define TXFQS_TFFL_MASK GENMASK(5, 0) 253 254 /* Tx Buffer Element Size Configuration (TXESC) */ 255 #define TXESC_TBDS_MASK GENMASK(2, 0) 256 #define TXESC_TBDS_64B 0x7 257 258 /* Tx Event FIFO Configuration (TXEFC) */ 259 #define TXEFC_EFS_MASK GENMASK(21, 16) 260 261 /* Tx Event FIFO Status (TXEFS) */ 262 #define TXEFS_TEFL BIT(25) 263 #define TXEFS_EFF BIT(24) 264 #define TXEFS_EFGI_MASK GENMASK(12, 8) 265 #define TXEFS_EFFL_MASK GENMASK(5, 0) 266 267 /* Tx Event FIFO Acknowledge (TXEFA) */ 268 #define TXEFA_EFAI_MASK GENMASK(4, 0) 269 270 /* Message RAM Configuration (in bytes) */ 271 #define SIDF_ELEMENT_SIZE 4 272 #define XIDF_ELEMENT_SIZE 8 273 #define RXF0_ELEMENT_SIZE 72 274 #define RXF1_ELEMENT_SIZE 72 275 #define RXB_ELEMENT_SIZE 72 276 #define TXE_ELEMENT_SIZE 8 277 #define TXB_ELEMENT_SIZE 72 278 279 /* Message RAM Elements */ 280 #define M_CAN_FIFO_ID 0x0 281 #define M_CAN_FIFO_DLC 0x4 282 #define M_CAN_FIFO_DATA 0x8 283 284 /* Rx Buffer Element */ 285 /* R0 */ 286 #define RX_BUF_ESI BIT(31) 287 #define RX_BUF_XTD BIT(30) 288 #define RX_BUF_RTR BIT(29) 289 /* R1 */ 290 #define RX_BUF_ANMF BIT(31) 291 #define RX_BUF_FDF BIT(21) 292 #define RX_BUF_BRS BIT(20) 293 #define RX_BUF_RXTS_MASK GENMASK(15, 0) 294 295 /* Tx Buffer Element */ 296 /* T0 */ 297 #define TX_BUF_ESI BIT(31) 298 #define TX_BUF_XTD BIT(30) 299 #define TX_BUF_RTR BIT(29) 300 /* T1 */ 301 #define TX_BUF_EFC BIT(23) 302 #define TX_BUF_FDF BIT(21) 303 #define TX_BUF_BRS BIT(20) 304 #define TX_BUF_MM_MASK GENMASK(31, 24) 305 #define TX_BUF_DLC_MASK GENMASK(19, 16) 306 307 /* Tx event FIFO Element */ 308 /* E1 */ 309 #define TX_EVENT_MM_MASK GENMASK(31, 24) 310 #define TX_EVENT_TXTS_MASK GENMASK(15, 0) 311 312 /* Hrtimer polling interval */ 313 #define HRTIMER_POLL_INTERVAL_MS 1 314 315 /* The ID and DLC registers are adjacent in M_CAN FIFO memory, 316 * and we can save a (potentially slow) bus round trip by combining 317 * reads and writes to them. 318 */ 319 struct id_and_dlc { 320 u32 id; 321 u32 dlc; 322 }; 323 324 static inline u32 m_can_read(struct m_can_classdev *cdev, enum m_can_reg reg) 325 { 326 return cdev->ops->read_reg(cdev, reg); 327 } 328 329 static inline void m_can_write(struct m_can_classdev *cdev, enum m_can_reg reg, 330 u32 val) 331 { 332 cdev->ops->write_reg(cdev, reg, val); 333 } 334 335 static int 336 m_can_fifo_read(struct m_can_classdev *cdev, 337 u32 fgi, unsigned int offset, void *val, size_t val_count) 338 { 339 u32 addr_offset = cdev->mcfg[MRAM_RXF0].off + fgi * RXF0_ELEMENT_SIZE + 340 offset; 341 342 if (val_count == 0) 343 return 0; 344 345 return cdev->ops->read_fifo(cdev, addr_offset, val, val_count); 346 } 347 348 static int 349 m_can_fifo_write(struct m_can_classdev *cdev, 350 u32 fpi, unsigned int offset, const void *val, size_t val_count) 351 { 352 u32 addr_offset = cdev->mcfg[MRAM_TXB].off + fpi * TXB_ELEMENT_SIZE + 353 offset; 354 355 if (val_count == 0) 356 return 0; 357 358 return cdev->ops->write_fifo(cdev, addr_offset, val, val_count); 359 } 360 361 static inline int m_can_fifo_write_no_off(struct m_can_classdev *cdev, 362 u32 fpi, u32 val) 363 { 364 return cdev->ops->write_fifo(cdev, fpi, &val, 1); 365 } 366 367 static int 368 m_can_txe_fifo_read(struct m_can_classdev *cdev, u32 fgi, u32 offset, u32 *val) 369 { 370 u32 addr_offset = cdev->mcfg[MRAM_TXE].off + fgi * TXE_ELEMENT_SIZE + 371 offset; 372 373 return cdev->ops->read_fifo(cdev, addr_offset, val, 1); 374 } 375 376 static inline bool _m_can_tx_fifo_full(u32 txfqs) 377 { 378 return !!(txfqs & TXFQS_TFQF); 379 } 380 381 static inline bool m_can_tx_fifo_full(struct m_can_classdev *cdev) 382 { 383 return _m_can_tx_fifo_full(m_can_read(cdev, M_CAN_TXFQS)); 384 } 385 386 static void m_can_config_endisable(struct m_can_classdev *cdev, bool enable) 387 { 388 u32 cccr = m_can_read(cdev, M_CAN_CCCR); 389 u32 timeout = 10; 390 u32 val = 0; 391 392 /* Clear the Clock stop request if it was set */ 393 if (cccr & CCCR_CSR) 394 cccr &= ~CCCR_CSR; 395 396 if (enable) { 397 /* enable m_can configuration */ 398 m_can_write(cdev, M_CAN_CCCR, cccr | CCCR_INIT); 399 udelay(5); 400 /* CCCR.CCE can only be set/reset while CCCR.INIT = '1' */ 401 m_can_write(cdev, M_CAN_CCCR, cccr | CCCR_INIT | CCCR_CCE); 402 } else { 403 m_can_write(cdev, M_CAN_CCCR, cccr & ~(CCCR_INIT | CCCR_CCE)); 404 } 405 406 /* there's a delay for module initialization */ 407 if (enable) 408 val = CCCR_INIT | CCCR_CCE; 409 410 while ((m_can_read(cdev, M_CAN_CCCR) & (CCCR_INIT | CCCR_CCE)) != val) { 411 if (timeout == 0) { 412 netdev_warn(cdev->net, "Failed to init module\n"); 413 return; 414 } 415 timeout--; 416 udelay(1); 417 } 418 } 419 420 static inline void m_can_enable_all_interrupts(struct m_can_classdev *cdev) 421 { 422 /* Only interrupt line 0 is used in this driver */ 423 m_can_write(cdev, M_CAN_ILE, ILE_EINT0); 424 } 425 426 static inline void m_can_disable_all_interrupts(struct m_can_classdev *cdev) 427 { 428 m_can_write(cdev, M_CAN_ILE, 0x0); 429 } 430 431 /* Retrieve internal timestamp counter from TSCV.TSC, and shift it to 32-bit 432 * width. 433 */ 434 static u32 m_can_get_timestamp(struct m_can_classdev *cdev) 435 { 436 u32 tscv; 437 u32 tsc; 438 439 tscv = m_can_read(cdev, M_CAN_TSCV); 440 tsc = FIELD_GET(TSCV_TSC_MASK, tscv); 441 442 return (tsc << 16); 443 } 444 445 static void m_can_clean(struct net_device *net) 446 { 447 struct m_can_classdev *cdev = netdev_priv(net); 448 449 if (cdev->tx_skb) { 450 int putidx = 0; 451 452 net->stats.tx_errors++; 453 if (cdev->version > 30) 454 putidx = FIELD_GET(TXFQS_TFQPI_MASK, 455 m_can_read(cdev, M_CAN_TXFQS)); 456 457 can_free_echo_skb(cdev->net, putidx, NULL); 458 cdev->tx_skb = NULL; 459 } 460 } 461 462 /* For peripherals, pass skb to rx-offload, which will push skb from 463 * napi. For non-peripherals, RX is done in napi already, so push 464 * directly. timestamp is used to ensure good skb ordering in 465 * rx-offload and is ignored for non-peripherals. 466 */ 467 static void m_can_receive_skb(struct m_can_classdev *cdev, 468 struct sk_buff *skb, 469 u32 timestamp) 470 { 471 if (cdev->is_peripheral) { 472 struct net_device_stats *stats = &cdev->net->stats; 473 int err; 474 475 err = can_rx_offload_queue_timestamp(&cdev->offload, skb, 476 timestamp); 477 if (err) 478 stats->rx_fifo_errors++; 479 } else { 480 netif_receive_skb(skb); 481 } 482 } 483 484 static int m_can_read_fifo(struct net_device *dev, u32 fgi) 485 { 486 struct net_device_stats *stats = &dev->stats; 487 struct m_can_classdev *cdev = netdev_priv(dev); 488 struct canfd_frame *cf; 489 struct sk_buff *skb; 490 struct id_and_dlc fifo_header; 491 u32 timestamp = 0; 492 int err; 493 494 err = m_can_fifo_read(cdev, fgi, M_CAN_FIFO_ID, &fifo_header, 2); 495 if (err) 496 goto out_fail; 497 498 if (fifo_header.dlc & RX_BUF_FDF) 499 skb = alloc_canfd_skb(dev, &cf); 500 else 501 skb = alloc_can_skb(dev, (struct can_frame **)&cf); 502 if (!skb) { 503 stats->rx_dropped++; 504 return 0; 505 } 506 507 if (fifo_header.dlc & RX_BUF_FDF) 508 cf->len = can_fd_dlc2len((fifo_header.dlc >> 16) & 0x0F); 509 else 510 cf->len = can_cc_dlc2len((fifo_header.dlc >> 16) & 0x0F); 511 512 if (fifo_header.id & RX_BUF_XTD) 513 cf->can_id = (fifo_header.id & CAN_EFF_MASK) | CAN_EFF_FLAG; 514 else 515 cf->can_id = (fifo_header.id >> 18) & CAN_SFF_MASK; 516 517 if (fifo_header.id & RX_BUF_ESI) { 518 cf->flags |= CANFD_ESI; 519 netdev_dbg(dev, "ESI Error\n"); 520 } 521 522 if (!(fifo_header.dlc & RX_BUF_FDF) && (fifo_header.id & RX_BUF_RTR)) { 523 cf->can_id |= CAN_RTR_FLAG; 524 } else { 525 if (fifo_header.dlc & RX_BUF_BRS) 526 cf->flags |= CANFD_BRS; 527 528 err = m_can_fifo_read(cdev, fgi, M_CAN_FIFO_DATA, 529 cf->data, DIV_ROUND_UP(cf->len, 4)); 530 if (err) 531 goto out_free_skb; 532 533 stats->rx_bytes += cf->len; 534 } 535 stats->rx_packets++; 536 537 timestamp = FIELD_GET(RX_BUF_RXTS_MASK, fifo_header.dlc) << 16; 538 539 m_can_receive_skb(cdev, skb, timestamp); 540 541 return 0; 542 543 out_free_skb: 544 kfree_skb(skb); 545 out_fail: 546 netdev_err(dev, "FIFO read returned %d\n", err); 547 return err; 548 } 549 550 static int m_can_do_rx_poll(struct net_device *dev, int quota) 551 { 552 struct m_can_classdev *cdev = netdev_priv(dev); 553 u32 pkts = 0; 554 u32 rxfs; 555 u32 rx_count; 556 u32 fgi; 557 int ack_fgi = -1; 558 int i; 559 int err = 0; 560 561 rxfs = m_can_read(cdev, M_CAN_RXF0S); 562 if (!(rxfs & RXFS_FFL_MASK)) { 563 netdev_dbg(dev, "no messages in fifo0\n"); 564 return 0; 565 } 566 567 rx_count = FIELD_GET(RXFS_FFL_MASK, rxfs); 568 fgi = FIELD_GET(RXFS_FGI_MASK, rxfs); 569 570 for (i = 0; i < rx_count && quota > 0; ++i) { 571 err = m_can_read_fifo(dev, fgi); 572 if (err) 573 break; 574 575 quota--; 576 pkts++; 577 ack_fgi = fgi; 578 fgi = (++fgi >= cdev->mcfg[MRAM_RXF0].num ? 0 : fgi); 579 } 580 581 if (ack_fgi != -1) 582 m_can_write(cdev, M_CAN_RXF0A, ack_fgi); 583 584 if (err) 585 return err; 586 587 return pkts; 588 } 589 590 static int m_can_handle_lost_msg(struct net_device *dev) 591 { 592 struct m_can_classdev *cdev = netdev_priv(dev); 593 struct net_device_stats *stats = &dev->stats; 594 struct sk_buff *skb; 595 struct can_frame *frame; 596 u32 timestamp = 0; 597 598 netdev_err(dev, "msg lost in rxf0\n"); 599 600 stats->rx_errors++; 601 stats->rx_over_errors++; 602 603 skb = alloc_can_err_skb(dev, &frame); 604 if (unlikely(!skb)) 605 return 0; 606 607 frame->can_id |= CAN_ERR_CRTL; 608 frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; 609 610 if (cdev->is_peripheral) 611 timestamp = m_can_get_timestamp(cdev); 612 613 m_can_receive_skb(cdev, skb, timestamp); 614 615 return 1; 616 } 617 618 static int m_can_handle_lec_err(struct net_device *dev, 619 enum m_can_lec_type lec_type) 620 { 621 struct m_can_classdev *cdev = netdev_priv(dev); 622 struct net_device_stats *stats = &dev->stats; 623 struct can_frame *cf; 624 struct sk_buff *skb; 625 u32 timestamp = 0; 626 627 cdev->can.can_stats.bus_error++; 628 stats->rx_errors++; 629 630 /* propagate the error condition to the CAN stack */ 631 skb = alloc_can_err_skb(dev, &cf); 632 if (unlikely(!skb)) 633 return 0; 634 635 /* check for 'last error code' which tells us the 636 * type of the last error to occur on the CAN bus 637 */ 638 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; 639 640 switch (lec_type) { 641 case LEC_STUFF_ERROR: 642 netdev_dbg(dev, "stuff error\n"); 643 cf->data[2] |= CAN_ERR_PROT_STUFF; 644 break; 645 case LEC_FORM_ERROR: 646 netdev_dbg(dev, "form error\n"); 647 cf->data[2] |= CAN_ERR_PROT_FORM; 648 break; 649 case LEC_ACK_ERROR: 650 netdev_dbg(dev, "ack error\n"); 651 cf->data[3] = CAN_ERR_PROT_LOC_ACK; 652 break; 653 case LEC_BIT1_ERROR: 654 netdev_dbg(dev, "bit1 error\n"); 655 cf->data[2] |= CAN_ERR_PROT_BIT1; 656 break; 657 case LEC_BIT0_ERROR: 658 netdev_dbg(dev, "bit0 error\n"); 659 cf->data[2] |= CAN_ERR_PROT_BIT0; 660 break; 661 case LEC_CRC_ERROR: 662 netdev_dbg(dev, "CRC error\n"); 663 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ; 664 break; 665 default: 666 break; 667 } 668 669 if (cdev->is_peripheral) 670 timestamp = m_can_get_timestamp(cdev); 671 672 m_can_receive_skb(cdev, skb, timestamp); 673 674 return 1; 675 } 676 677 static int __m_can_get_berr_counter(const struct net_device *dev, 678 struct can_berr_counter *bec) 679 { 680 struct m_can_classdev *cdev = netdev_priv(dev); 681 unsigned int ecr; 682 683 ecr = m_can_read(cdev, M_CAN_ECR); 684 bec->rxerr = FIELD_GET(ECR_REC_MASK, ecr); 685 bec->txerr = FIELD_GET(ECR_TEC_MASK, ecr); 686 687 return 0; 688 } 689 690 static int m_can_clk_start(struct m_can_classdev *cdev) 691 { 692 if (cdev->pm_clock_support == 0) 693 return 0; 694 695 return pm_runtime_resume_and_get(cdev->dev); 696 } 697 698 static void m_can_clk_stop(struct m_can_classdev *cdev) 699 { 700 if (cdev->pm_clock_support) 701 pm_runtime_put_sync(cdev->dev); 702 } 703 704 static int m_can_get_berr_counter(const struct net_device *dev, 705 struct can_berr_counter *bec) 706 { 707 struct m_can_classdev *cdev = netdev_priv(dev); 708 int err; 709 710 err = m_can_clk_start(cdev); 711 if (err) 712 return err; 713 714 __m_can_get_berr_counter(dev, bec); 715 716 m_can_clk_stop(cdev); 717 718 return 0; 719 } 720 721 static int m_can_handle_state_change(struct net_device *dev, 722 enum can_state new_state) 723 { 724 struct m_can_classdev *cdev = netdev_priv(dev); 725 struct can_frame *cf; 726 struct sk_buff *skb; 727 struct can_berr_counter bec; 728 unsigned int ecr; 729 u32 timestamp = 0; 730 731 switch (new_state) { 732 case CAN_STATE_ERROR_WARNING: 733 /* error warning state */ 734 cdev->can.can_stats.error_warning++; 735 cdev->can.state = CAN_STATE_ERROR_WARNING; 736 break; 737 case CAN_STATE_ERROR_PASSIVE: 738 /* error passive state */ 739 cdev->can.can_stats.error_passive++; 740 cdev->can.state = CAN_STATE_ERROR_PASSIVE; 741 break; 742 case CAN_STATE_BUS_OFF: 743 /* bus-off state */ 744 cdev->can.state = CAN_STATE_BUS_OFF; 745 m_can_disable_all_interrupts(cdev); 746 cdev->can.can_stats.bus_off++; 747 can_bus_off(dev); 748 break; 749 default: 750 break; 751 } 752 753 /* propagate the error condition to the CAN stack */ 754 skb = alloc_can_err_skb(dev, &cf); 755 if (unlikely(!skb)) 756 return 0; 757 758 __m_can_get_berr_counter(dev, &bec); 759 760 switch (new_state) { 761 case CAN_STATE_ERROR_WARNING: 762 /* error warning state */ 763 cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT; 764 cf->data[1] = (bec.txerr > bec.rxerr) ? 765 CAN_ERR_CRTL_TX_WARNING : 766 CAN_ERR_CRTL_RX_WARNING; 767 cf->data[6] = bec.txerr; 768 cf->data[7] = bec.rxerr; 769 break; 770 case CAN_STATE_ERROR_PASSIVE: 771 /* error passive state */ 772 cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT; 773 ecr = m_can_read(cdev, M_CAN_ECR); 774 if (ecr & ECR_RP) 775 cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE; 776 if (bec.txerr > 127) 777 cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE; 778 cf->data[6] = bec.txerr; 779 cf->data[7] = bec.rxerr; 780 break; 781 case CAN_STATE_BUS_OFF: 782 /* bus-off state */ 783 cf->can_id |= CAN_ERR_BUSOFF; 784 break; 785 default: 786 break; 787 } 788 789 if (cdev->is_peripheral) 790 timestamp = m_can_get_timestamp(cdev); 791 792 m_can_receive_skb(cdev, skb, timestamp); 793 794 return 1; 795 } 796 797 static int m_can_handle_state_errors(struct net_device *dev, u32 psr) 798 { 799 struct m_can_classdev *cdev = netdev_priv(dev); 800 int work_done = 0; 801 802 if (psr & PSR_EW && cdev->can.state != CAN_STATE_ERROR_WARNING) { 803 netdev_dbg(dev, "entered error warning state\n"); 804 work_done += m_can_handle_state_change(dev, 805 CAN_STATE_ERROR_WARNING); 806 } 807 808 if (psr & PSR_EP && cdev->can.state != CAN_STATE_ERROR_PASSIVE) { 809 netdev_dbg(dev, "entered error passive state\n"); 810 work_done += m_can_handle_state_change(dev, 811 CAN_STATE_ERROR_PASSIVE); 812 } 813 814 if (psr & PSR_BO && cdev->can.state != CAN_STATE_BUS_OFF) { 815 netdev_dbg(dev, "entered error bus off state\n"); 816 work_done += m_can_handle_state_change(dev, 817 CAN_STATE_BUS_OFF); 818 } 819 820 return work_done; 821 } 822 823 static void m_can_handle_other_err(struct net_device *dev, u32 irqstatus) 824 { 825 if (irqstatus & IR_WDI) 826 netdev_err(dev, "Message RAM Watchdog event due to missing READY\n"); 827 if (irqstatus & IR_BEU) 828 netdev_err(dev, "Bit Error Uncorrected\n"); 829 if (irqstatus & IR_BEC) 830 netdev_err(dev, "Bit Error Corrected\n"); 831 if (irqstatus & IR_TOO) 832 netdev_err(dev, "Timeout reached\n"); 833 if (irqstatus & IR_MRAF) 834 netdev_err(dev, "Message RAM access failure occurred\n"); 835 } 836 837 static inline bool is_lec_err(u8 lec) 838 { 839 return lec != LEC_NO_ERROR && lec != LEC_NO_CHANGE; 840 } 841 842 static inline bool m_can_is_protocol_err(u32 irqstatus) 843 { 844 return irqstatus & IR_ERR_LEC_31X; 845 } 846 847 static int m_can_handle_protocol_error(struct net_device *dev, u32 irqstatus) 848 { 849 struct net_device_stats *stats = &dev->stats; 850 struct m_can_classdev *cdev = netdev_priv(dev); 851 struct can_frame *cf; 852 struct sk_buff *skb; 853 u32 timestamp = 0; 854 855 /* propagate the error condition to the CAN stack */ 856 skb = alloc_can_err_skb(dev, &cf); 857 858 /* update tx error stats since there is protocol error */ 859 stats->tx_errors++; 860 861 /* update arbitration lost status */ 862 if (cdev->version >= 31 && (irqstatus & IR_PEA)) { 863 netdev_dbg(dev, "Protocol error in Arbitration fail\n"); 864 cdev->can.can_stats.arbitration_lost++; 865 if (skb) { 866 cf->can_id |= CAN_ERR_LOSTARB; 867 cf->data[0] |= CAN_ERR_LOSTARB_UNSPEC; 868 } 869 } 870 871 if (unlikely(!skb)) { 872 netdev_dbg(dev, "allocation of skb failed\n"); 873 return 0; 874 } 875 876 if (cdev->is_peripheral) 877 timestamp = m_can_get_timestamp(cdev); 878 879 m_can_receive_skb(cdev, skb, timestamp); 880 881 return 1; 882 } 883 884 static int m_can_handle_bus_errors(struct net_device *dev, u32 irqstatus, 885 u32 psr) 886 { 887 struct m_can_classdev *cdev = netdev_priv(dev); 888 int work_done = 0; 889 890 if (irqstatus & IR_RF0L) 891 work_done += m_can_handle_lost_msg(dev); 892 893 /* handle lec errors on the bus */ 894 if (cdev->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) { 895 u8 lec = FIELD_GET(PSR_LEC_MASK, psr); 896 u8 dlec = FIELD_GET(PSR_DLEC_MASK, psr); 897 898 if (is_lec_err(lec)) { 899 netdev_dbg(dev, "Arbitration phase error detected\n"); 900 work_done += m_can_handle_lec_err(dev, lec); 901 } 902 903 if (is_lec_err(dlec)) { 904 netdev_dbg(dev, "Data phase error detected\n"); 905 work_done += m_can_handle_lec_err(dev, dlec); 906 } 907 } 908 909 /* handle protocol errors in arbitration phase */ 910 if ((cdev->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) && 911 m_can_is_protocol_err(irqstatus)) 912 work_done += m_can_handle_protocol_error(dev, irqstatus); 913 914 /* other unproccessed error interrupts */ 915 m_can_handle_other_err(dev, irqstatus); 916 917 return work_done; 918 } 919 920 static int m_can_rx_handler(struct net_device *dev, int quota, u32 irqstatus) 921 { 922 struct m_can_classdev *cdev = netdev_priv(dev); 923 int rx_work_or_err; 924 int work_done = 0; 925 926 if (!irqstatus) 927 goto end; 928 929 /* Errata workaround for issue "Needless activation of MRAF irq" 930 * During frame reception while the MCAN is in Error Passive state 931 * and the Receive Error Counter has the value MCAN_ECR.REC = 127, 932 * it may happen that MCAN_IR.MRAF is set although there was no 933 * Message RAM access failure. 934 * If MCAN_IR.MRAF is enabled, an interrupt to the Host CPU is generated 935 * The Message RAM Access Failure interrupt routine needs to check 936 * whether MCAN_ECR.RP = ’1’ and MCAN_ECR.REC = 127. 937 * In this case, reset MCAN_IR.MRAF. No further action is required. 938 */ 939 if (cdev->version <= 31 && irqstatus & IR_MRAF && 940 m_can_read(cdev, M_CAN_ECR) & ECR_RP) { 941 struct can_berr_counter bec; 942 943 __m_can_get_berr_counter(dev, &bec); 944 if (bec.rxerr == 127) { 945 m_can_write(cdev, M_CAN_IR, IR_MRAF); 946 irqstatus &= ~IR_MRAF; 947 } 948 } 949 950 if (irqstatus & IR_ERR_STATE) 951 work_done += m_can_handle_state_errors(dev, 952 m_can_read(cdev, M_CAN_PSR)); 953 954 if (irqstatus & IR_ERR_BUS_30X) 955 work_done += m_can_handle_bus_errors(dev, irqstatus, 956 m_can_read(cdev, M_CAN_PSR)); 957 958 if (irqstatus & IR_RF0N) { 959 rx_work_or_err = m_can_do_rx_poll(dev, (quota - work_done)); 960 if (rx_work_or_err < 0) 961 return rx_work_or_err; 962 963 work_done += rx_work_or_err; 964 } 965 end: 966 return work_done; 967 } 968 969 static int m_can_rx_peripheral(struct net_device *dev, u32 irqstatus) 970 { 971 struct m_can_classdev *cdev = netdev_priv(dev); 972 int work_done; 973 974 work_done = m_can_rx_handler(dev, NAPI_POLL_WEIGHT, irqstatus); 975 976 /* Don't re-enable interrupts if the driver had a fatal error 977 * (e.g., FIFO read failure). 978 */ 979 if (work_done < 0) 980 m_can_disable_all_interrupts(cdev); 981 982 return work_done; 983 } 984 985 static int m_can_poll(struct napi_struct *napi, int quota) 986 { 987 struct net_device *dev = napi->dev; 988 struct m_can_classdev *cdev = netdev_priv(dev); 989 int work_done; 990 u32 irqstatus; 991 992 irqstatus = cdev->irqstatus | m_can_read(cdev, M_CAN_IR); 993 994 work_done = m_can_rx_handler(dev, quota, irqstatus); 995 996 /* Don't re-enable interrupts if the driver had a fatal error 997 * (e.g., FIFO read failure). 998 */ 999 if (work_done >= 0 && work_done < quota) { 1000 napi_complete_done(napi, work_done); 1001 m_can_enable_all_interrupts(cdev); 1002 } 1003 1004 return work_done; 1005 } 1006 1007 /* Echo tx skb and update net stats. Peripherals use rx-offload for 1008 * echo. timestamp is used for peripherals to ensure correct ordering 1009 * by rx-offload, and is ignored for non-peripherals. 1010 */ 1011 static void m_can_tx_update_stats(struct m_can_classdev *cdev, 1012 unsigned int msg_mark, 1013 u32 timestamp) 1014 { 1015 struct net_device *dev = cdev->net; 1016 struct net_device_stats *stats = &dev->stats; 1017 1018 if (cdev->is_peripheral) 1019 stats->tx_bytes += 1020 can_rx_offload_get_echo_skb(&cdev->offload, 1021 msg_mark, 1022 timestamp, 1023 NULL); 1024 else 1025 stats->tx_bytes += can_get_echo_skb(dev, msg_mark, NULL); 1026 1027 stats->tx_packets++; 1028 } 1029 1030 static int m_can_echo_tx_event(struct net_device *dev) 1031 { 1032 u32 txe_count = 0; 1033 u32 m_can_txefs; 1034 u32 fgi = 0; 1035 int ack_fgi = -1; 1036 int i = 0; 1037 int err = 0; 1038 unsigned int msg_mark; 1039 1040 struct m_can_classdev *cdev = netdev_priv(dev); 1041 1042 /* read tx event fifo status */ 1043 m_can_txefs = m_can_read(cdev, M_CAN_TXEFS); 1044 1045 /* Get Tx Event fifo element count */ 1046 txe_count = FIELD_GET(TXEFS_EFFL_MASK, m_can_txefs); 1047 fgi = FIELD_GET(TXEFS_EFGI_MASK, m_can_txefs); 1048 1049 /* Get and process all sent elements */ 1050 for (i = 0; i < txe_count; i++) { 1051 u32 txe, timestamp = 0; 1052 1053 /* get message marker, timestamp */ 1054 err = m_can_txe_fifo_read(cdev, fgi, 4, &txe); 1055 if (err) { 1056 netdev_err(dev, "TXE FIFO read returned %d\n", err); 1057 break; 1058 } 1059 1060 msg_mark = FIELD_GET(TX_EVENT_MM_MASK, txe); 1061 timestamp = FIELD_GET(TX_EVENT_TXTS_MASK, txe) << 16; 1062 1063 ack_fgi = fgi; 1064 fgi = (++fgi >= cdev->mcfg[MRAM_TXE].num ? 0 : fgi); 1065 1066 /* update stats */ 1067 m_can_tx_update_stats(cdev, msg_mark, timestamp); 1068 } 1069 1070 if (ack_fgi != -1) 1071 m_can_write(cdev, M_CAN_TXEFA, FIELD_PREP(TXEFA_EFAI_MASK, 1072 ack_fgi)); 1073 1074 return err; 1075 } 1076 1077 static irqreturn_t m_can_isr(int irq, void *dev_id) 1078 { 1079 struct net_device *dev = (struct net_device *)dev_id; 1080 struct m_can_classdev *cdev = netdev_priv(dev); 1081 u32 ir; 1082 1083 if (pm_runtime_suspended(cdev->dev)) 1084 return IRQ_NONE; 1085 ir = m_can_read(cdev, M_CAN_IR); 1086 if (!ir) 1087 return IRQ_NONE; 1088 1089 /* ACK all irqs */ 1090 m_can_write(cdev, M_CAN_IR, ir); 1091 1092 if (cdev->ops->clear_interrupts) 1093 cdev->ops->clear_interrupts(cdev); 1094 1095 /* schedule NAPI in case of 1096 * - rx IRQ 1097 * - state change IRQ 1098 * - bus error IRQ and bus error reporting 1099 */ 1100 if ((ir & IR_RF0N) || (ir & IR_ERR_ALL_30X)) { 1101 cdev->irqstatus = ir; 1102 if (!cdev->is_peripheral) { 1103 m_can_disable_all_interrupts(cdev); 1104 napi_schedule(&cdev->napi); 1105 } else if (m_can_rx_peripheral(dev, ir) < 0) { 1106 goto out_fail; 1107 } 1108 } 1109 1110 if (cdev->version == 30) { 1111 if (ir & IR_TC) { 1112 /* Transmission Complete Interrupt*/ 1113 u32 timestamp = 0; 1114 1115 if (cdev->is_peripheral) 1116 timestamp = m_can_get_timestamp(cdev); 1117 m_can_tx_update_stats(cdev, 0, timestamp); 1118 netif_wake_queue(dev); 1119 } 1120 } else { 1121 if (ir & IR_TEFN) { 1122 /* New TX FIFO Element arrived */ 1123 if (m_can_echo_tx_event(dev) != 0) 1124 goto out_fail; 1125 1126 if (netif_queue_stopped(dev) && 1127 !m_can_tx_fifo_full(cdev)) 1128 netif_wake_queue(dev); 1129 } 1130 } 1131 1132 if (cdev->is_peripheral) 1133 can_rx_offload_threaded_irq_finish(&cdev->offload); 1134 1135 return IRQ_HANDLED; 1136 1137 out_fail: 1138 m_can_disable_all_interrupts(cdev); 1139 return IRQ_HANDLED; 1140 } 1141 1142 static const struct can_bittiming_const m_can_bittiming_const_30X = { 1143 .name = KBUILD_MODNAME, 1144 .tseg1_min = 2, /* Time segment 1 = prop_seg + phase_seg1 */ 1145 .tseg1_max = 64, 1146 .tseg2_min = 1, /* Time segment 2 = phase_seg2 */ 1147 .tseg2_max = 16, 1148 .sjw_max = 16, 1149 .brp_min = 1, 1150 .brp_max = 1024, 1151 .brp_inc = 1, 1152 }; 1153 1154 static const struct can_bittiming_const m_can_data_bittiming_const_30X = { 1155 .name = KBUILD_MODNAME, 1156 .tseg1_min = 2, /* Time segment 1 = prop_seg + phase_seg1 */ 1157 .tseg1_max = 16, 1158 .tseg2_min = 1, /* Time segment 2 = phase_seg2 */ 1159 .tseg2_max = 8, 1160 .sjw_max = 4, 1161 .brp_min = 1, 1162 .brp_max = 32, 1163 .brp_inc = 1, 1164 }; 1165 1166 static const struct can_bittiming_const m_can_bittiming_const_31X = { 1167 .name = KBUILD_MODNAME, 1168 .tseg1_min = 2, /* Time segment 1 = prop_seg + phase_seg1 */ 1169 .tseg1_max = 256, 1170 .tseg2_min = 2, /* Time segment 2 = phase_seg2 */ 1171 .tseg2_max = 128, 1172 .sjw_max = 128, 1173 .brp_min = 1, 1174 .brp_max = 512, 1175 .brp_inc = 1, 1176 }; 1177 1178 static const struct can_bittiming_const m_can_data_bittiming_const_31X = { 1179 .name = KBUILD_MODNAME, 1180 .tseg1_min = 1, /* Time segment 1 = prop_seg + phase_seg1 */ 1181 .tseg1_max = 32, 1182 .tseg2_min = 1, /* Time segment 2 = phase_seg2 */ 1183 .tseg2_max = 16, 1184 .sjw_max = 16, 1185 .brp_min = 1, 1186 .brp_max = 32, 1187 .brp_inc = 1, 1188 }; 1189 1190 static int m_can_set_bittiming(struct net_device *dev) 1191 { 1192 struct m_can_classdev *cdev = netdev_priv(dev); 1193 const struct can_bittiming *bt = &cdev->can.bittiming; 1194 const struct can_bittiming *dbt = &cdev->can.data_bittiming; 1195 u16 brp, sjw, tseg1, tseg2; 1196 u32 reg_btp; 1197 1198 brp = bt->brp - 1; 1199 sjw = bt->sjw - 1; 1200 tseg1 = bt->prop_seg + bt->phase_seg1 - 1; 1201 tseg2 = bt->phase_seg2 - 1; 1202 reg_btp = FIELD_PREP(NBTP_NBRP_MASK, brp) | 1203 FIELD_PREP(NBTP_NSJW_MASK, sjw) | 1204 FIELD_PREP(NBTP_NTSEG1_MASK, tseg1) | 1205 FIELD_PREP(NBTP_NTSEG2_MASK, tseg2); 1206 m_can_write(cdev, M_CAN_NBTP, reg_btp); 1207 1208 if (cdev->can.ctrlmode & CAN_CTRLMODE_FD) { 1209 reg_btp = 0; 1210 brp = dbt->brp - 1; 1211 sjw = dbt->sjw - 1; 1212 tseg1 = dbt->prop_seg + dbt->phase_seg1 - 1; 1213 tseg2 = dbt->phase_seg2 - 1; 1214 1215 /* TDC is only needed for bitrates beyond 2.5 MBit/s. 1216 * This is mentioned in the "Bit Time Requirements for CAN FD" 1217 * paper presented at the International CAN Conference 2013 1218 */ 1219 if (dbt->bitrate > 2500000) { 1220 u32 tdco, ssp; 1221 1222 /* Use the same value of secondary sampling point 1223 * as the data sampling point 1224 */ 1225 ssp = dbt->sample_point; 1226 1227 /* Equation based on Bosch's M_CAN User Manual's 1228 * Transmitter Delay Compensation Section 1229 */ 1230 tdco = (cdev->can.clock.freq / 1000) * 1231 ssp / dbt->bitrate; 1232 1233 /* Max valid TDCO value is 127 */ 1234 if (tdco > 127) { 1235 netdev_warn(dev, "TDCO value of %u is beyond maximum. Using maximum possible value\n", 1236 tdco); 1237 tdco = 127; 1238 } 1239 1240 reg_btp |= DBTP_TDC; 1241 m_can_write(cdev, M_CAN_TDCR, 1242 FIELD_PREP(TDCR_TDCO_MASK, tdco)); 1243 } 1244 1245 reg_btp |= FIELD_PREP(DBTP_DBRP_MASK, brp) | 1246 FIELD_PREP(DBTP_DSJW_MASK, sjw) | 1247 FIELD_PREP(DBTP_DTSEG1_MASK, tseg1) | 1248 FIELD_PREP(DBTP_DTSEG2_MASK, tseg2); 1249 1250 m_can_write(cdev, M_CAN_DBTP, reg_btp); 1251 } 1252 1253 return 0; 1254 } 1255 1256 /* Configure M_CAN chip: 1257 * - set rx buffer/fifo element size 1258 * - configure rx fifo 1259 * - accept non-matching frame into fifo 0 1260 * - configure tx buffer 1261 * - >= v3.1.x: TX FIFO is used 1262 * - configure mode 1263 * - setup bittiming 1264 * - configure timestamp generation 1265 */ 1266 static int m_can_chip_config(struct net_device *dev) 1267 { 1268 struct m_can_classdev *cdev = netdev_priv(dev); 1269 u32 interrupts = IR_ALL_INT; 1270 u32 cccr, test; 1271 int err; 1272 1273 err = m_can_init_ram(cdev); 1274 if (err) { 1275 dev_err(cdev->dev, "Message RAM configuration failed\n"); 1276 return err; 1277 } 1278 1279 /* Disable unused interrupts */ 1280 interrupts &= ~(IR_ARA | IR_ELO | IR_DRX | IR_TEFF | IR_TEFW | IR_TFE | 1281 IR_TCF | IR_HPM | IR_RF1F | IR_RF1W | IR_RF1N | 1282 IR_RF0F | IR_RF0W); 1283 1284 m_can_config_endisable(cdev, true); 1285 1286 /* RX Buffer/FIFO Element Size 64 bytes data field */ 1287 m_can_write(cdev, M_CAN_RXESC, 1288 FIELD_PREP(RXESC_RBDS_MASK, RXESC_64B) | 1289 FIELD_PREP(RXESC_F1DS_MASK, RXESC_64B) | 1290 FIELD_PREP(RXESC_F0DS_MASK, RXESC_64B)); 1291 1292 /* Accept Non-matching Frames Into FIFO 0 */ 1293 m_can_write(cdev, M_CAN_GFC, 0x0); 1294 1295 if (cdev->version == 30) { 1296 /* only support one Tx Buffer currently */ 1297 m_can_write(cdev, M_CAN_TXBC, FIELD_PREP(TXBC_NDTB_MASK, 1) | 1298 cdev->mcfg[MRAM_TXB].off); 1299 } else { 1300 /* TX FIFO is used for newer IP Core versions */ 1301 m_can_write(cdev, M_CAN_TXBC, 1302 FIELD_PREP(TXBC_TFQS_MASK, 1303 cdev->mcfg[MRAM_TXB].num) | 1304 cdev->mcfg[MRAM_TXB].off); 1305 } 1306 1307 /* support 64 bytes payload */ 1308 m_can_write(cdev, M_CAN_TXESC, 1309 FIELD_PREP(TXESC_TBDS_MASK, TXESC_TBDS_64B)); 1310 1311 /* TX Event FIFO */ 1312 if (cdev->version == 30) { 1313 m_can_write(cdev, M_CAN_TXEFC, 1314 FIELD_PREP(TXEFC_EFS_MASK, 1) | 1315 cdev->mcfg[MRAM_TXE].off); 1316 } else { 1317 /* Full TX Event FIFO is used */ 1318 m_can_write(cdev, M_CAN_TXEFC, 1319 FIELD_PREP(TXEFC_EFS_MASK, 1320 cdev->mcfg[MRAM_TXE].num) | 1321 cdev->mcfg[MRAM_TXE].off); 1322 } 1323 1324 /* rx fifo configuration, blocking mode, fifo size 1 */ 1325 m_can_write(cdev, M_CAN_RXF0C, 1326 FIELD_PREP(RXFC_FS_MASK, cdev->mcfg[MRAM_RXF0].num) | 1327 cdev->mcfg[MRAM_RXF0].off); 1328 1329 m_can_write(cdev, M_CAN_RXF1C, 1330 FIELD_PREP(RXFC_FS_MASK, cdev->mcfg[MRAM_RXF1].num) | 1331 cdev->mcfg[MRAM_RXF1].off); 1332 1333 cccr = m_can_read(cdev, M_CAN_CCCR); 1334 test = m_can_read(cdev, M_CAN_TEST); 1335 test &= ~TEST_LBCK; 1336 if (cdev->version == 30) { 1337 /* Version 3.0.x */ 1338 1339 cccr &= ~(CCCR_TEST | CCCR_MON | CCCR_DAR | 1340 FIELD_PREP(CCCR_CMR_MASK, FIELD_MAX(CCCR_CMR_MASK)) | 1341 FIELD_PREP(CCCR_CME_MASK, FIELD_MAX(CCCR_CME_MASK))); 1342 1343 if (cdev->can.ctrlmode & CAN_CTRLMODE_FD) 1344 cccr |= FIELD_PREP(CCCR_CME_MASK, CCCR_CME_CANFD_BRS); 1345 1346 } else { 1347 /* Version 3.1.x or 3.2.x */ 1348 cccr &= ~(CCCR_TEST | CCCR_MON | CCCR_BRSE | CCCR_FDOE | 1349 CCCR_NISO | CCCR_DAR); 1350 1351 /* Only 3.2.x has NISO Bit implemented */ 1352 if (cdev->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO) 1353 cccr |= CCCR_NISO; 1354 1355 if (cdev->can.ctrlmode & CAN_CTRLMODE_FD) 1356 cccr |= (CCCR_BRSE | CCCR_FDOE); 1357 } 1358 1359 /* Loopback Mode */ 1360 if (cdev->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) { 1361 cccr |= CCCR_TEST | CCCR_MON; 1362 test |= TEST_LBCK; 1363 } 1364 1365 /* Enable Monitoring (all versions) */ 1366 if (cdev->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) 1367 cccr |= CCCR_MON; 1368 1369 /* Disable Auto Retransmission (all versions) */ 1370 if (cdev->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT) 1371 cccr |= CCCR_DAR; 1372 1373 /* Write config */ 1374 m_can_write(cdev, M_CAN_CCCR, cccr); 1375 m_can_write(cdev, M_CAN_TEST, test); 1376 1377 /* Enable interrupts */ 1378 if (!(cdev->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)) { 1379 if (cdev->version == 30) 1380 interrupts &= ~(IR_ERR_LEC_30X); 1381 else 1382 interrupts &= ~(IR_ERR_LEC_31X); 1383 } 1384 m_can_write(cdev, M_CAN_IE, interrupts); 1385 1386 /* route all interrupts to INT0 */ 1387 m_can_write(cdev, M_CAN_ILS, ILS_ALL_INT0); 1388 1389 /* set bittiming params */ 1390 m_can_set_bittiming(dev); 1391 1392 /* enable internal timestamp generation, with a prescaler of 16. The 1393 * prescaler is applied to the nominal bit timing 1394 */ 1395 m_can_write(cdev, M_CAN_TSCC, 1396 FIELD_PREP(TSCC_TCP_MASK, 0xf) | 1397 FIELD_PREP(TSCC_TSS_MASK, TSCC_TSS_INTERNAL)); 1398 1399 m_can_config_endisable(cdev, false); 1400 1401 if (cdev->ops->init) 1402 cdev->ops->init(cdev); 1403 1404 return 0; 1405 } 1406 1407 static int m_can_start(struct net_device *dev) 1408 { 1409 struct m_can_classdev *cdev = netdev_priv(dev); 1410 int ret; 1411 1412 /* basic m_can configuration */ 1413 ret = m_can_chip_config(dev); 1414 if (ret) 1415 return ret; 1416 1417 cdev->can.state = CAN_STATE_ERROR_ACTIVE; 1418 1419 m_can_enable_all_interrupts(cdev); 1420 1421 if (!dev->irq) { 1422 dev_dbg(cdev->dev, "Start hrtimer\n"); 1423 hrtimer_start(&cdev->hrtimer, ms_to_ktime(HRTIMER_POLL_INTERVAL_MS), 1424 HRTIMER_MODE_REL_PINNED); 1425 } 1426 1427 return 0; 1428 } 1429 1430 static int m_can_set_mode(struct net_device *dev, enum can_mode mode) 1431 { 1432 switch (mode) { 1433 case CAN_MODE_START: 1434 m_can_clean(dev); 1435 m_can_start(dev); 1436 netif_wake_queue(dev); 1437 break; 1438 default: 1439 return -EOPNOTSUPP; 1440 } 1441 1442 return 0; 1443 } 1444 1445 /* Checks core release number of M_CAN 1446 * returns 0 if an unsupported device is detected 1447 * else it returns the release and step coded as: 1448 * return value = 10 * <release> + 1 * <step> 1449 */ 1450 static int m_can_check_core_release(struct m_can_classdev *cdev) 1451 { 1452 u32 crel_reg; 1453 u8 rel; 1454 u8 step; 1455 int res; 1456 1457 /* Read Core Release Version and split into version number 1458 * Example: Version 3.2.1 => rel = 3; step = 2; substep = 1; 1459 */ 1460 crel_reg = m_can_read(cdev, M_CAN_CREL); 1461 rel = (u8)FIELD_GET(CREL_REL_MASK, crel_reg); 1462 step = (u8)FIELD_GET(CREL_STEP_MASK, crel_reg); 1463 1464 if (rel == 3) { 1465 /* M_CAN v3.x.y: create return value */ 1466 res = 30 + step; 1467 } else { 1468 /* Unsupported M_CAN version */ 1469 res = 0; 1470 } 1471 1472 return res; 1473 } 1474 1475 /* Selectable Non ISO support only in version 3.2.x 1476 * This function checks if the bit is writable. 1477 */ 1478 static bool m_can_niso_supported(struct m_can_classdev *cdev) 1479 { 1480 u32 cccr_reg, cccr_poll = 0; 1481 int niso_timeout = -ETIMEDOUT; 1482 int i; 1483 1484 m_can_config_endisable(cdev, true); 1485 cccr_reg = m_can_read(cdev, M_CAN_CCCR); 1486 cccr_reg |= CCCR_NISO; 1487 m_can_write(cdev, M_CAN_CCCR, cccr_reg); 1488 1489 for (i = 0; i <= 10; i++) { 1490 cccr_poll = m_can_read(cdev, M_CAN_CCCR); 1491 if (cccr_poll == cccr_reg) { 1492 niso_timeout = 0; 1493 break; 1494 } 1495 1496 usleep_range(1, 5); 1497 } 1498 1499 /* Clear NISO */ 1500 cccr_reg &= ~(CCCR_NISO); 1501 m_can_write(cdev, M_CAN_CCCR, cccr_reg); 1502 1503 m_can_config_endisable(cdev, false); 1504 1505 /* return false if time out (-ETIMEDOUT), else return true */ 1506 return !niso_timeout; 1507 } 1508 1509 static int m_can_dev_setup(struct m_can_classdev *cdev) 1510 { 1511 struct net_device *dev = cdev->net; 1512 int m_can_version, err; 1513 1514 m_can_version = m_can_check_core_release(cdev); 1515 /* return if unsupported version */ 1516 if (!m_can_version) { 1517 dev_err(cdev->dev, "Unsupported version number: %2d", 1518 m_can_version); 1519 return -EINVAL; 1520 } 1521 1522 if (!cdev->is_peripheral) 1523 netif_napi_add(dev, &cdev->napi, m_can_poll); 1524 1525 /* Shared properties of all M_CAN versions */ 1526 cdev->version = m_can_version; 1527 cdev->can.do_set_mode = m_can_set_mode; 1528 cdev->can.do_get_berr_counter = m_can_get_berr_counter; 1529 1530 /* Set M_CAN supported operations */ 1531 cdev->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK | 1532 CAN_CTRLMODE_LISTENONLY | 1533 CAN_CTRLMODE_BERR_REPORTING | 1534 CAN_CTRLMODE_FD | 1535 CAN_CTRLMODE_ONE_SHOT; 1536 1537 /* Set properties depending on M_CAN version */ 1538 switch (cdev->version) { 1539 case 30: 1540 /* CAN_CTRLMODE_FD_NON_ISO is fixed with M_CAN IP v3.0.x */ 1541 err = can_set_static_ctrlmode(dev, CAN_CTRLMODE_FD_NON_ISO); 1542 if (err) 1543 return err; 1544 cdev->can.bittiming_const = &m_can_bittiming_const_30X; 1545 cdev->can.data_bittiming_const = &m_can_data_bittiming_const_30X; 1546 break; 1547 case 31: 1548 /* CAN_CTRLMODE_FD_NON_ISO is fixed with M_CAN IP v3.1.x */ 1549 err = can_set_static_ctrlmode(dev, CAN_CTRLMODE_FD_NON_ISO); 1550 if (err) 1551 return err; 1552 cdev->can.bittiming_const = &m_can_bittiming_const_31X; 1553 cdev->can.data_bittiming_const = &m_can_data_bittiming_const_31X; 1554 break; 1555 case 32: 1556 case 33: 1557 /* Support both MCAN version v3.2.x and v3.3.0 */ 1558 cdev->can.bittiming_const = &m_can_bittiming_const_31X; 1559 cdev->can.data_bittiming_const = &m_can_data_bittiming_const_31X; 1560 1561 cdev->can.ctrlmode_supported |= 1562 (m_can_niso_supported(cdev) ? 1563 CAN_CTRLMODE_FD_NON_ISO : 0); 1564 break; 1565 default: 1566 dev_err(cdev->dev, "Unsupported version number: %2d", 1567 cdev->version); 1568 return -EINVAL; 1569 } 1570 1571 if (cdev->ops->init) 1572 cdev->ops->init(cdev); 1573 1574 return 0; 1575 } 1576 1577 static void m_can_stop(struct net_device *dev) 1578 { 1579 struct m_can_classdev *cdev = netdev_priv(dev); 1580 1581 if (!dev->irq) { 1582 dev_dbg(cdev->dev, "Stop hrtimer\n"); 1583 hrtimer_cancel(&cdev->hrtimer); 1584 } 1585 1586 /* disable all interrupts */ 1587 m_can_disable_all_interrupts(cdev); 1588 1589 /* Set init mode to disengage from the network */ 1590 m_can_config_endisable(cdev, true); 1591 1592 /* set the state as STOPPED */ 1593 cdev->can.state = CAN_STATE_STOPPED; 1594 } 1595 1596 static int m_can_close(struct net_device *dev) 1597 { 1598 struct m_can_classdev *cdev = netdev_priv(dev); 1599 1600 netif_stop_queue(dev); 1601 1602 if (!cdev->is_peripheral) 1603 napi_disable(&cdev->napi); 1604 1605 m_can_stop(dev); 1606 m_can_clk_stop(cdev); 1607 free_irq(dev->irq, dev); 1608 1609 if (cdev->is_peripheral) { 1610 cdev->tx_skb = NULL; 1611 destroy_workqueue(cdev->tx_wq); 1612 cdev->tx_wq = NULL; 1613 can_rx_offload_disable(&cdev->offload); 1614 } 1615 1616 close_candev(dev); 1617 1618 phy_power_off(cdev->transceiver); 1619 1620 return 0; 1621 } 1622 1623 static int m_can_next_echo_skb_occupied(struct net_device *dev, int putidx) 1624 { 1625 struct m_can_classdev *cdev = netdev_priv(dev); 1626 /*get wrap around for loopback skb index */ 1627 unsigned int wrap = cdev->can.echo_skb_max; 1628 int next_idx; 1629 1630 /* calculate next index */ 1631 next_idx = (++putidx >= wrap ? 0 : putidx); 1632 1633 /* check if occupied */ 1634 return !!cdev->can.echo_skb[next_idx]; 1635 } 1636 1637 static netdev_tx_t m_can_tx_handler(struct m_can_classdev *cdev) 1638 { 1639 struct canfd_frame *cf = (struct canfd_frame *)cdev->tx_skb->data; 1640 struct net_device *dev = cdev->net; 1641 struct sk_buff *skb = cdev->tx_skb; 1642 struct id_and_dlc fifo_header; 1643 u32 cccr, fdflags; 1644 u32 txfqs; 1645 int err; 1646 int putidx; 1647 1648 cdev->tx_skb = NULL; 1649 1650 /* Generate ID field for TX buffer Element */ 1651 /* Common to all supported M_CAN versions */ 1652 if (cf->can_id & CAN_EFF_FLAG) { 1653 fifo_header.id = cf->can_id & CAN_EFF_MASK; 1654 fifo_header.id |= TX_BUF_XTD; 1655 } else { 1656 fifo_header.id = ((cf->can_id & CAN_SFF_MASK) << 18); 1657 } 1658 1659 if (cf->can_id & CAN_RTR_FLAG) 1660 fifo_header.id |= TX_BUF_RTR; 1661 1662 if (cdev->version == 30) { 1663 netif_stop_queue(dev); 1664 1665 fifo_header.dlc = can_fd_len2dlc(cf->len) << 16; 1666 1667 /* Write the frame ID, DLC, and payload to the FIFO element. */ 1668 err = m_can_fifo_write(cdev, 0, M_CAN_FIFO_ID, &fifo_header, 2); 1669 if (err) 1670 goto out_fail; 1671 1672 err = m_can_fifo_write(cdev, 0, M_CAN_FIFO_DATA, 1673 cf->data, DIV_ROUND_UP(cf->len, 4)); 1674 if (err) 1675 goto out_fail; 1676 1677 if (cdev->can.ctrlmode & CAN_CTRLMODE_FD) { 1678 cccr = m_can_read(cdev, M_CAN_CCCR); 1679 cccr &= ~CCCR_CMR_MASK; 1680 if (can_is_canfd_skb(skb)) { 1681 if (cf->flags & CANFD_BRS) 1682 cccr |= FIELD_PREP(CCCR_CMR_MASK, 1683 CCCR_CMR_CANFD_BRS); 1684 else 1685 cccr |= FIELD_PREP(CCCR_CMR_MASK, 1686 CCCR_CMR_CANFD); 1687 } else { 1688 cccr |= FIELD_PREP(CCCR_CMR_MASK, CCCR_CMR_CAN); 1689 } 1690 m_can_write(cdev, M_CAN_CCCR, cccr); 1691 } 1692 m_can_write(cdev, M_CAN_TXBTIE, 0x1); 1693 1694 can_put_echo_skb(skb, dev, 0, 0); 1695 1696 m_can_write(cdev, M_CAN_TXBAR, 0x1); 1697 /* End of xmit function for version 3.0.x */ 1698 } else { 1699 /* Transmit routine for version >= v3.1.x */ 1700 1701 txfqs = m_can_read(cdev, M_CAN_TXFQS); 1702 1703 /* Check if FIFO full */ 1704 if (_m_can_tx_fifo_full(txfqs)) { 1705 /* This shouldn't happen */ 1706 netif_stop_queue(dev); 1707 netdev_warn(dev, 1708 "TX queue active although FIFO is full."); 1709 1710 if (cdev->is_peripheral) { 1711 kfree_skb(skb); 1712 dev->stats.tx_dropped++; 1713 return NETDEV_TX_OK; 1714 } else { 1715 return NETDEV_TX_BUSY; 1716 } 1717 } 1718 1719 /* get put index for frame */ 1720 putidx = FIELD_GET(TXFQS_TFQPI_MASK, txfqs); 1721 1722 /* Construct DLC Field, with CAN-FD configuration. 1723 * Use the put index of the fifo as the message marker, 1724 * used in the TX interrupt for sending the correct echo frame. 1725 */ 1726 1727 /* get CAN FD configuration of frame */ 1728 fdflags = 0; 1729 if (can_is_canfd_skb(skb)) { 1730 fdflags |= TX_BUF_FDF; 1731 if (cf->flags & CANFD_BRS) 1732 fdflags |= TX_BUF_BRS; 1733 } 1734 1735 fifo_header.dlc = FIELD_PREP(TX_BUF_MM_MASK, putidx) | 1736 FIELD_PREP(TX_BUF_DLC_MASK, can_fd_len2dlc(cf->len)) | 1737 fdflags | TX_BUF_EFC; 1738 err = m_can_fifo_write(cdev, putidx, M_CAN_FIFO_ID, &fifo_header, 2); 1739 if (err) 1740 goto out_fail; 1741 1742 err = m_can_fifo_write(cdev, putidx, M_CAN_FIFO_DATA, 1743 cf->data, DIV_ROUND_UP(cf->len, 4)); 1744 if (err) 1745 goto out_fail; 1746 1747 /* Push loopback echo. 1748 * Will be looped back on TX interrupt based on message marker 1749 */ 1750 can_put_echo_skb(skb, dev, putidx, 0); 1751 1752 /* Enable TX FIFO element to start transfer */ 1753 m_can_write(cdev, M_CAN_TXBAR, (1 << putidx)); 1754 1755 /* stop network queue if fifo full */ 1756 if (m_can_tx_fifo_full(cdev) || 1757 m_can_next_echo_skb_occupied(dev, putidx)) 1758 netif_stop_queue(dev); 1759 } 1760 1761 return NETDEV_TX_OK; 1762 1763 out_fail: 1764 netdev_err(dev, "FIFO write returned %d\n", err); 1765 m_can_disable_all_interrupts(cdev); 1766 return NETDEV_TX_BUSY; 1767 } 1768 1769 static void m_can_tx_work_queue(struct work_struct *ws) 1770 { 1771 struct m_can_classdev *cdev = container_of(ws, struct m_can_classdev, 1772 tx_work); 1773 1774 m_can_tx_handler(cdev); 1775 } 1776 1777 static netdev_tx_t m_can_start_xmit(struct sk_buff *skb, 1778 struct net_device *dev) 1779 { 1780 struct m_can_classdev *cdev = netdev_priv(dev); 1781 1782 if (can_dev_dropped_skb(dev, skb)) 1783 return NETDEV_TX_OK; 1784 1785 if (cdev->is_peripheral) { 1786 if (cdev->tx_skb) { 1787 netdev_err(dev, "hard_xmit called while tx busy\n"); 1788 return NETDEV_TX_BUSY; 1789 } 1790 1791 if (cdev->can.state == CAN_STATE_BUS_OFF) { 1792 m_can_clean(dev); 1793 } else { 1794 /* Need to stop the queue to avoid numerous requests 1795 * from being sent. Suggested improvement is to create 1796 * a queueing mechanism that will queue the skbs and 1797 * process them in order. 1798 */ 1799 cdev->tx_skb = skb; 1800 netif_stop_queue(cdev->net); 1801 queue_work(cdev->tx_wq, &cdev->tx_work); 1802 } 1803 } else { 1804 cdev->tx_skb = skb; 1805 return m_can_tx_handler(cdev); 1806 } 1807 1808 return NETDEV_TX_OK; 1809 } 1810 1811 static enum hrtimer_restart hrtimer_callback(struct hrtimer *timer) 1812 { 1813 struct m_can_classdev *cdev = container_of(timer, struct 1814 m_can_classdev, hrtimer); 1815 1816 m_can_isr(0, cdev->net); 1817 1818 hrtimer_forward_now(timer, ms_to_ktime(HRTIMER_POLL_INTERVAL_MS)); 1819 1820 return HRTIMER_RESTART; 1821 } 1822 1823 static int m_can_open(struct net_device *dev) 1824 { 1825 struct m_can_classdev *cdev = netdev_priv(dev); 1826 int err; 1827 1828 err = phy_power_on(cdev->transceiver); 1829 if (err) 1830 return err; 1831 1832 err = m_can_clk_start(cdev); 1833 if (err) 1834 goto out_phy_power_off; 1835 1836 /* open the can device */ 1837 err = open_candev(dev); 1838 if (err) { 1839 netdev_err(dev, "failed to open can device\n"); 1840 goto exit_disable_clks; 1841 } 1842 1843 if (cdev->is_peripheral) 1844 can_rx_offload_enable(&cdev->offload); 1845 1846 /* register interrupt handler */ 1847 if (cdev->is_peripheral) { 1848 cdev->tx_skb = NULL; 1849 cdev->tx_wq = alloc_workqueue("mcan_wq", 1850 WQ_FREEZABLE | WQ_MEM_RECLAIM, 0); 1851 if (!cdev->tx_wq) { 1852 err = -ENOMEM; 1853 goto out_wq_fail; 1854 } 1855 1856 INIT_WORK(&cdev->tx_work, m_can_tx_work_queue); 1857 1858 err = request_threaded_irq(dev->irq, NULL, m_can_isr, 1859 IRQF_ONESHOT, 1860 dev->name, dev); 1861 } else if (dev->irq) { 1862 err = request_irq(dev->irq, m_can_isr, IRQF_SHARED, dev->name, 1863 dev); 1864 } 1865 1866 if (err < 0) { 1867 netdev_err(dev, "failed to request interrupt\n"); 1868 goto exit_irq_fail; 1869 } 1870 1871 /* start the m_can controller */ 1872 err = m_can_start(dev); 1873 if (err) 1874 goto exit_irq_fail; 1875 1876 if (!cdev->is_peripheral) 1877 napi_enable(&cdev->napi); 1878 1879 netif_start_queue(dev); 1880 1881 return 0; 1882 1883 exit_irq_fail: 1884 if (cdev->is_peripheral) 1885 destroy_workqueue(cdev->tx_wq); 1886 out_wq_fail: 1887 if (cdev->is_peripheral) 1888 can_rx_offload_disable(&cdev->offload); 1889 close_candev(dev); 1890 exit_disable_clks: 1891 m_can_clk_stop(cdev); 1892 out_phy_power_off: 1893 phy_power_off(cdev->transceiver); 1894 return err; 1895 } 1896 1897 static const struct net_device_ops m_can_netdev_ops = { 1898 .ndo_open = m_can_open, 1899 .ndo_stop = m_can_close, 1900 .ndo_start_xmit = m_can_start_xmit, 1901 .ndo_change_mtu = can_change_mtu, 1902 }; 1903 1904 static const struct ethtool_ops m_can_ethtool_ops = { 1905 .get_ts_info = ethtool_op_get_ts_info, 1906 }; 1907 1908 static int register_m_can_dev(struct net_device *dev) 1909 { 1910 dev->flags |= IFF_ECHO; /* we support local echo */ 1911 dev->netdev_ops = &m_can_netdev_ops; 1912 dev->ethtool_ops = &m_can_ethtool_ops; 1913 1914 return register_candev(dev); 1915 } 1916 1917 static void m_can_of_parse_mram(struct m_can_classdev *cdev, 1918 const u32 *mram_config_vals) 1919 { 1920 cdev->mcfg[MRAM_SIDF].off = mram_config_vals[0]; 1921 cdev->mcfg[MRAM_SIDF].num = mram_config_vals[1]; 1922 cdev->mcfg[MRAM_XIDF].off = cdev->mcfg[MRAM_SIDF].off + 1923 cdev->mcfg[MRAM_SIDF].num * SIDF_ELEMENT_SIZE; 1924 cdev->mcfg[MRAM_XIDF].num = mram_config_vals[2]; 1925 cdev->mcfg[MRAM_RXF0].off = cdev->mcfg[MRAM_XIDF].off + 1926 cdev->mcfg[MRAM_XIDF].num * XIDF_ELEMENT_SIZE; 1927 cdev->mcfg[MRAM_RXF0].num = mram_config_vals[3] & 1928 FIELD_MAX(RXFC_FS_MASK); 1929 cdev->mcfg[MRAM_RXF1].off = cdev->mcfg[MRAM_RXF0].off + 1930 cdev->mcfg[MRAM_RXF0].num * RXF0_ELEMENT_SIZE; 1931 cdev->mcfg[MRAM_RXF1].num = mram_config_vals[4] & 1932 FIELD_MAX(RXFC_FS_MASK); 1933 cdev->mcfg[MRAM_RXB].off = cdev->mcfg[MRAM_RXF1].off + 1934 cdev->mcfg[MRAM_RXF1].num * RXF1_ELEMENT_SIZE; 1935 cdev->mcfg[MRAM_RXB].num = mram_config_vals[5]; 1936 cdev->mcfg[MRAM_TXE].off = cdev->mcfg[MRAM_RXB].off + 1937 cdev->mcfg[MRAM_RXB].num * RXB_ELEMENT_SIZE; 1938 cdev->mcfg[MRAM_TXE].num = mram_config_vals[6]; 1939 cdev->mcfg[MRAM_TXB].off = cdev->mcfg[MRAM_TXE].off + 1940 cdev->mcfg[MRAM_TXE].num * TXE_ELEMENT_SIZE; 1941 cdev->mcfg[MRAM_TXB].num = mram_config_vals[7] & 1942 FIELD_MAX(TXBC_NDTB_MASK); 1943 1944 dev_dbg(cdev->dev, 1945 "sidf 0x%x %d xidf 0x%x %d rxf0 0x%x %d rxf1 0x%x %d rxb 0x%x %d txe 0x%x %d txb 0x%x %d\n", 1946 cdev->mcfg[MRAM_SIDF].off, cdev->mcfg[MRAM_SIDF].num, 1947 cdev->mcfg[MRAM_XIDF].off, cdev->mcfg[MRAM_XIDF].num, 1948 cdev->mcfg[MRAM_RXF0].off, cdev->mcfg[MRAM_RXF0].num, 1949 cdev->mcfg[MRAM_RXF1].off, cdev->mcfg[MRAM_RXF1].num, 1950 cdev->mcfg[MRAM_RXB].off, cdev->mcfg[MRAM_RXB].num, 1951 cdev->mcfg[MRAM_TXE].off, cdev->mcfg[MRAM_TXE].num, 1952 cdev->mcfg[MRAM_TXB].off, cdev->mcfg[MRAM_TXB].num); 1953 } 1954 1955 int m_can_init_ram(struct m_can_classdev *cdev) 1956 { 1957 int end, i, start; 1958 int err = 0; 1959 1960 /* initialize the entire Message RAM in use to avoid possible 1961 * ECC/parity checksum errors when reading an uninitialized buffer 1962 */ 1963 start = cdev->mcfg[MRAM_SIDF].off; 1964 end = cdev->mcfg[MRAM_TXB].off + 1965 cdev->mcfg[MRAM_TXB].num * TXB_ELEMENT_SIZE; 1966 1967 for (i = start; i < end; i += 4) { 1968 err = m_can_fifo_write_no_off(cdev, i, 0x0); 1969 if (err) 1970 break; 1971 } 1972 1973 return err; 1974 } 1975 EXPORT_SYMBOL_GPL(m_can_init_ram); 1976 1977 int m_can_class_get_clocks(struct m_can_classdev *cdev) 1978 { 1979 int ret = 0; 1980 1981 cdev->hclk = devm_clk_get(cdev->dev, "hclk"); 1982 cdev->cclk = devm_clk_get(cdev->dev, "cclk"); 1983 1984 if (IS_ERR(cdev->hclk) || IS_ERR(cdev->cclk)) { 1985 dev_err(cdev->dev, "no clock found\n"); 1986 ret = -ENODEV; 1987 } 1988 1989 return ret; 1990 } 1991 EXPORT_SYMBOL_GPL(m_can_class_get_clocks); 1992 1993 struct m_can_classdev *m_can_class_allocate_dev(struct device *dev, 1994 int sizeof_priv) 1995 { 1996 struct m_can_classdev *class_dev = NULL; 1997 u32 mram_config_vals[MRAM_CFG_LEN]; 1998 struct net_device *net_dev; 1999 u32 tx_fifo_size; 2000 int ret; 2001 2002 ret = fwnode_property_read_u32_array(dev_fwnode(dev), 2003 "bosch,mram-cfg", 2004 mram_config_vals, 2005 sizeof(mram_config_vals) / 4); 2006 if (ret) { 2007 dev_err(dev, "Could not get Message RAM configuration."); 2008 goto out; 2009 } 2010 2011 /* Get TX FIFO size 2012 * Defines the total amount of echo buffers for loopback 2013 */ 2014 tx_fifo_size = mram_config_vals[7]; 2015 2016 /* allocate the m_can device */ 2017 net_dev = alloc_candev(sizeof_priv, tx_fifo_size); 2018 if (!net_dev) { 2019 dev_err(dev, "Failed to allocate CAN device"); 2020 goto out; 2021 } 2022 2023 class_dev = netdev_priv(net_dev); 2024 class_dev->net = net_dev; 2025 class_dev->dev = dev; 2026 SET_NETDEV_DEV(net_dev, dev); 2027 2028 m_can_of_parse_mram(class_dev, mram_config_vals); 2029 out: 2030 return class_dev; 2031 } 2032 EXPORT_SYMBOL_GPL(m_can_class_allocate_dev); 2033 2034 void m_can_class_free_dev(struct net_device *net) 2035 { 2036 free_candev(net); 2037 } 2038 EXPORT_SYMBOL_GPL(m_can_class_free_dev); 2039 2040 int m_can_class_register(struct m_can_classdev *cdev) 2041 { 2042 int ret; 2043 2044 if (cdev->pm_clock_support) { 2045 ret = m_can_clk_start(cdev); 2046 if (ret) 2047 return ret; 2048 } 2049 2050 if (cdev->is_peripheral) { 2051 ret = can_rx_offload_add_manual(cdev->net, &cdev->offload, 2052 NAPI_POLL_WEIGHT); 2053 if (ret) 2054 goto clk_disable; 2055 } 2056 2057 if (!cdev->net->irq) 2058 cdev->hrtimer.function = &hrtimer_callback; 2059 2060 ret = m_can_dev_setup(cdev); 2061 if (ret) 2062 goto rx_offload_del; 2063 2064 ret = register_m_can_dev(cdev->net); 2065 if (ret) { 2066 dev_err(cdev->dev, "registering %s failed (err=%d)\n", 2067 cdev->net->name, ret); 2068 goto rx_offload_del; 2069 } 2070 2071 of_can_transceiver(cdev->net); 2072 2073 dev_info(cdev->dev, "%s device registered (irq=%d, version=%d)\n", 2074 KBUILD_MODNAME, cdev->net->irq, cdev->version); 2075 2076 /* Probe finished 2077 * Stop clocks. They will be reactivated once the M_CAN device is opened 2078 */ 2079 m_can_clk_stop(cdev); 2080 2081 return 0; 2082 2083 rx_offload_del: 2084 if (cdev->is_peripheral) 2085 can_rx_offload_del(&cdev->offload); 2086 clk_disable: 2087 m_can_clk_stop(cdev); 2088 2089 return ret; 2090 } 2091 EXPORT_SYMBOL_GPL(m_can_class_register); 2092 2093 void m_can_class_unregister(struct m_can_classdev *cdev) 2094 { 2095 if (cdev->is_peripheral) 2096 can_rx_offload_del(&cdev->offload); 2097 unregister_candev(cdev->net); 2098 } 2099 EXPORT_SYMBOL_GPL(m_can_class_unregister); 2100 2101 int m_can_class_suspend(struct device *dev) 2102 { 2103 struct m_can_classdev *cdev = dev_get_drvdata(dev); 2104 struct net_device *ndev = cdev->net; 2105 2106 if (netif_running(ndev)) { 2107 netif_stop_queue(ndev); 2108 netif_device_detach(ndev); 2109 m_can_stop(ndev); 2110 m_can_clk_stop(cdev); 2111 } 2112 2113 pinctrl_pm_select_sleep_state(dev); 2114 2115 cdev->can.state = CAN_STATE_SLEEPING; 2116 2117 return 0; 2118 } 2119 EXPORT_SYMBOL_GPL(m_can_class_suspend); 2120 2121 int m_can_class_resume(struct device *dev) 2122 { 2123 struct m_can_classdev *cdev = dev_get_drvdata(dev); 2124 struct net_device *ndev = cdev->net; 2125 2126 pinctrl_pm_select_default_state(dev); 2127 2128 cdev->can.state = CAN_STATE_ERROR_ACTIVE; 2129 2130 if (netif_running(ndev)) { 2131 int ret; 2132 2133 ret = m_can_clk_start(cdev); 2134 if (ret) 2135 return ret; 2136 ret = m_can_start(ndev); 2137 if (ret) { 2138 m_can_clk_stop(cdev); 2139 2140 return ret; 2141 } 2142 2143 netif_device_attach(ndev); 2144 netif_start_queue(ndev); 2145 } 2146 2147 return 0; 2148 } 2149 EXPORT_SYMBOL_GPL(m_can_class_resume); 2150 2151 MODULE_AUTHOR("Dong Aisheng <b29396@freescale.com>"); 2152 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>"); 2153 MODULE_LICENSE("GPL v2"); 2154 MODULE_DESCRIPTION("CAN bus driver for Bosch M_CAN controller"); 2155