1 // SPDX-License-Identifier: GPL-2.0+ 2 /* Renesas R-Car CAN FD device driver 3 * 4 * Copyright (C) 2015 Renesas Electronics Corp. 5 */ 6 7 /* The R-Car CAN FD controller can operate in either one of the below two modes 8 * - CAN FD only mode 9 * - Classical CAN (CAN 2.0) only mode 10 * 11 * This driver puts the controller in CAN FD only mode by default. In this 12 * mode, the controller acts as a CAN FD node that can also interoperate with 13 * CAN 2.0 nodes. 14 * 15 * To switch the controller to Classical CAN (CAN 2.0) only mode, add 16 * "renesas,no-can-fd" optional property to the device tree node. A h/w reset is 17 * also required to switch modes. 18 * 19 * Note: The h/w manual register naming convention is clumsy and not acceptable 20 * to use as it is in the driver. However, those names are added as comments 21 * wherever it is modified to a readable name. 22 */ 23 24 #include <linux/module.h> 25 #include <linux/moduleparam.h> 26 #include <linux/kernel.h> 27 #include <linux/types.h> 28 #include <linux/interrupt.h> 29 #include <linux/errno.h> 30 #include <linux/ethtool.h> 31 #include <linux/netdevice.h> 32 #include <linux/platform_device.h> 33 #include <linux/can/dev.h> 34 #include <linux/clk.h> 35 #include <linux/of.h> 36 #include <linux/of_device.h> 37 #include <linux/bitmap.h> 38 #include <linux/bitops.h> 39 #include <linux/iopoll.h> 40 #include <linux/reset.h> 41 42 #define RCANFD_DRV_NAME "rcar_canfd" 43 44 enum rcanfd_chip_id { 45 RENESAS_RCAR_GEN3 = 0, 46 RENESAS_RZG2L, 47 RENESAS_R8A779A0, 48 }; 49 50 /* Global register bits */ 51 52 /* RSCFDnCFDGRMCFG */ 53 #define RCANFD_GRMCFG_RCMC BIT(0) 54 55 /* RSCFDnCFDGCFG / RSCFDnGCFG */ 56 #define RCANFD_GCFG_EEFE BIT(6) 57 #define RCANFD_GCFG_CMPOC BIT(5) /* CAN FD only */ 58 #define RCANFD_GCFG_DCS BIT(4) 59 #define RCANFD_GCFG_DCE BIT(1) 60 #define RCANFD_GCFG_TPRI BIT(0) 61 62 /* RSCFDnCFDGCTR / RSCFDnGCTR */ 63 #define RCANFD_GCTR_TSRST BIT(16) 64 #define RCANFD_GCTR_CFMPOFIE BIT(11) /* CAN FD only */ 65 #define RCANFD_GCTR_THLEIE BIT(10) 66 #define RCANFD_GCTR_MEIE BIT(9) 67 #define RCANFD_GCTR_DEIE BIT(8) 68 #define RCANFD_GCTR_GSLPR BIT(2) 69 #define RCANFD_GCTR_GMDC_MASK (0x3) 70 #define RCANFD_GCTR_GMDC_GOPM (0x0) 71 #define RCANFD_GCTR_GMDC_GRESET (0x1) 72 #define RCANFD_GCTR_GMDC_GTEST (0x2) 73 74 /* RSCFDnCFDGSTS / RSCFDnGSTS */ 75 #define RCANFD_GSTS_GRAMINIT BIT(3) 76 #define RCANFD_GSTS_GSLPSTS BIT(2) 77 #define RCANFD_GSTS_GHLTSTS BIT(1) 78 #define RCANFD_GSTS_GRSTSTS BIT(0) 79 /* Non-operational status */ 80 #define RCANFD_GSTS_GNOPM (BIT(0) | BIT(1) | BIT(2) | BIT(3)) 81 82 /* RSCFDnCFDGERFL / RSCFDnGERFL */ 83 #define RCANFD_GERFL_EEF0_7 GENMASK(23, 16) 84 #define RCANFD_GERFL_EEF1 BIT(17) 85 #define RCANFD_GERFL_EEF0 BIT(16) 86 #define RCANFD_GERFL_CMPOF BIT(3) /* CAN FD only */ 87 #define RCANFD_GERFL_THLES BIT(2) 88 #define RCANFD_GERFL_MES BIT(1) 89 #define RCANFD_GERFL_DEF BIT(0) 90 91 #define RCANFD_GERFL_ERR(gpriv, x) \ 92 ((x) & (reg_v3u(gpriv, RCANFD_GERFL_EEF0_7, \ 93 RCANFD_GERFL_EEF0 | RCANFD_GERFL_EEF1) | \ 94 RCANFD_GERFL_MES | \ 95 ((gpriv)->fdmode ? RCANFD_GERFL_CMPOF : 0))) 96 97 /* AFL Rx rules registers */ 98 99 /* RSCFDnCFDGAFLCFG0 / RSCFDnGAFLCFG0 */ 100 #define RCANFD_GAFLCFG_SETRNC(gpriv, n, x) \ 101 (((x) & reg_v3u(gpriv, 0x1ff, 0xff)) << \ 102 (reg_v3u(gpriv, 16, 24) - (n) * reg_v3u(gpriv, 16, 8))) 103 104 #define RCANFD_GAFLCFG_GETRNC(gpriv, n, x) \ 105 (((x) >> (reg_v3u(gpriv, 16, 24) - (n) * reg_v3u(gpriv, 16, 8))) & \ 106 reg_v3u(gpriv, 0x1ff, 0xff)) 107 108 /* RSCFDnCFDGAFLECTR / RSCFDnGAFLECTR */ 109 #define RCANFD_GAFLECTR_AFLDAE BIT(8) 110 #define RCANFD_GAFLECTR_AFLPN(gpriv, x) ((x) & reg_v3u(gpriv, 0x7f, 0x1f)) 111 112 /* RSCFDnCFDGAFLIDj / RSCFDnGAFLIDj */ 113 #define RCANFD_GAFLID_GAFLLB BIT(29) 114 115 /* RSCFDnCFDGAFLP1_j / RSCFDnGAFLP1_j */ 116 #define RCANFD_GAFLP1_GAFLFDP(x) (1 << (x)) 117 118 /* Channel register bits */ 119 120 /* RSCFDnCmCFG - Classical CAN only */ 121 #define RCANFD_CFG_SJW(x) (((x) & 0x3) << 24) 122 #define RCANFD_CFG_TSEG2(x) (((x) & 0x7) << 20) 123 #define RCANFD_CFG_TSEG1(x) (((x) & 0xf) << 16) 124 #define RCANFD_CFG_BRP(x) (((x) & 0x3ff) << 0) 125 126 /* RSCFDnCFDCmNCFG - CAN FD only */ 127 #define RCANFD_NCFG_NTSEG2(gpriv, x) \ 128 (((x) & reg_v3u(gpriv, 0x7f, 0x1f)) << reg_v3u(gpriv, 25, 24)) 129 130 #define RCANFD_NCFG_NTSEG1(gpriv, x) \ 131 (((x) & reg_v3u(gpriv, 0xff, 0x7f)) << reg_v3u(gpriv, 17, 16)) 132 133 #define RCANFD_NCFG_NSJW(gpriv, x) \ 134 (((x) & reg_v3u(gpriv, 0x7f, 0x1f)) << reg_v3u(gpriv, 10, 11)) 135 136 #define RCANFD_NCFG_NBRP(x) (((x) & 0x3ff) << 0) 137 138 /* RSCFDnCFDCmCTR / RSCFDnCmCTR */ 139 #define RCANFD_CCTR_CTME BIT(24) 140 #define RCANFD_CCTR_ERRD BIT(23) 141 #define RCANFD_CCTR_BOM_MASK (0x3 << 21) 142 #define RCANFD_CCTR_BOM_ISO (0x0 << 21) 143 #define RCANFD_CCTR_BOM_BENTRY (0x1 << 21) 144 #define RCANFD_CCTR_BOM_BEND (0x2 << 21) 145 #define RCANFD_CCTR_TDCVFIE BIT(19) 146 #define RCANFD_CCTR_SOCOIE BIT(18) 147 #define RCANFD_CCTR_EOCOIE BIT(17) 148 #define RCANFD_CCTR_TAIE BIT(16) 149 #define RCANFD_CCTR_ALIE BIT(15) 150 #define RCANFD_CCTR_BLIE BIT(14) 151 #define RCANFD_CCTR_OLIE BIT(13) 152 #define RCANFD_CCTR_BORIE BIT(12) 153 #define RCANFD_CCTR_BOEIE BIT(11) 154 #define RCANFD_CCTR_EPIE BIT(10) 155 #define RCANFD_CCTR_EWIE BIT(9) 156 #define RCANFD_CCTR_BEIE BIT(8) 157 #define RCANFD_CCTR_CSLPR BIT(2) 158 #define RCANFD_CCTR_CHMDC_MASK (0x3) 159 #define RCANFD_CCTR_CHDMC_COPM (0x0) 160 #define RCANFD_CCTR_CHDMC_CRESET (0x1) 161 #define RCANFD_CCTR_CHDMC_CHLT (0x2) 162 163 /* RSCFDnCFDCmSTS / RSCFDnCmSTS */ 164 #define RCANFD_CSTS_COMSTS BIT(7) 165 #define RCANFD_CSTS_RECSTS BIT(6) 166 #define RCANFD_CSTS_TRMSTS BIT(5) 167 #define RCANFD_CSTS_BOSTS BIT(4) 168 #define RCANFD_CSTS_EPSTS BIT(3) 169 #define RCANFD_CSTS_SLPSTS BIT(2) 170 #define RCANFD_CSTS_HLTSTS BIT(1) 171 #define RCANFD_CSTS_CRSTSTS BIT(0) 172 173 #define RCANFD_CSTS_TECCNT(x) (((x) >> 24) & 0xff) 174 #define RCANFD_CSTS_RECCNT(x) (((x) >> 16) & 0xff) 175 176 /* RSCFDnCFDCmERFL / RSCFDnCmERFL */ 177 #define RCANFD_CERFL_ADERR BIT(14) 178 #define RCANFD_CERFL_B0ERR BIT(13) 179 #define RCANFD_CERFL_B1ERR BIT(12) 180 #define RCANFD_CERFL_CERR BIT(11) 181 #define RCANFD_CERFL_AERR BIT(10) 182 #define RCANFD_CERFL_FERR BIT(9) 183 #define RCANFD_CERFL_SERR BIT(8) 184 #define RCANFD_CERFL_ALF BIT(7) 185 #define RCANFD_CERFL_BLF BIT(6) 186 #define RCANFD_CERFL_OVLF BIT(5) 187 #define RCANFD_CERFL_BORF BIT(4) 188 #define RCANFD_CERFL_BOEF BIT(3) 189 #define RCANFD_CERFL_EPF BIT(2) 190 #define RCANFD_CERFL_EWF BIT(1) 191 #define RCANFD_CERFL_BEF BIT(0) 192 193 #define RCANFD_CERFL_ERR(x) ((x) & (0x7fff)) /* above bits 14:0 */ 194 195 /* RSCFDnCFDCmDCFG */ 196 #define RCANFD_DCFG_DSJW(x) (((x) & 0x7) << 24) 197 198 #define RCANFD_DCFG_DTSEG2(gpriv, x) \ 199 (((x) & reg_v3u(gpriv, 0x0f, 0x7)) << reg_v3u(gpriv, 16, 20)) 200 201 #define RCANFD_DCFG_DTSEG1(gpriv, x) \ 202 (((x) & reg_v3u(gpriv, 0x1f, 0xf)) << reg_v3u(gpriv, 8, 16)) 203 204 #define RCANFD_DCFG_DBRP(x) (((x) & 0xff) << 0) 205 206 /* RSCFDnCFDCmFDCFG */ 207 #define RCANFD_FDCFG_CLOE BIT(30) 208 #define RCANFD_FDCFG_FDOE BIT(28) 209 #define RCANFD_FDCFG_TDCE BIT(9) 210 #define RCANFD_FDCFG_TDCOC BIT(8) 211 #define RCANFD_FDCFG_TDCO(x) (((x) & 0x7f) >> 16) 212 213 /* RSCFDnCFDRFCCx */ 214 #define RCANFD_RFCC_RFIM BIT(12) 215 #define RCANFD_RFCC_RFDC(x) (((x) & 0x7) << 8) 216 #define RCANFD_RFCC_RFPLS(x) (((x) & 0x7) << 4) 217 #define RCANFD_RFCC_RFIE BIT(1) 218 #define RCANFD_RFCC_RFE BIT(0) 219 220 /* RSCFDnCFDRFSTSx */ 221 #define RCANFD_RFSTS_RFIF BIT(3) 222 #define RCANFD_RFSTS_RFMLT BIT(2) 223 #define RCANFD_RFSTS_RFFLL BIT(1) 224 #define RCANFD_RFSTS_RFEMP BIT(0) 225 226 /* RSCFDnCFDRFIDx */ 227 #define RCANFD_RFID_RFIDE BIT(31) 228 #define RCANFD_RFID_RFRTR BIT(30) 229 230 /* RSCFDnCFDRFPTRx */ 231 #define RCANFD_RFPTR_RFDLC(x) (((x) >> 28) & 0xf) 232 #define RCANFD_RFPTR_RFPTR(x) (((x) >> 16) & 0xfff) 233 #define RCANFD_RFPTR_RFTS(x) (((x) >> 0) & 0xffff) 234 235 /* RSCFDnCFDRFFDSTSx */ 236 #define RCANFD_RFFDSTS_RFFDF BIT(2) 237 #define RCANFD_RFFDSTS_RFBRS BIT(1) 238 #define RCANFD_RFFDSTS_RFESI BIT(0) 239 240 /* Common FIFO bits */ 241 242 /* RSCFDnCFDCFCCk */ 243 #define RCANFD_CFCC_CFTML(gpriv, x) (((x) & 0xf) << reg_v3u(gpriv, 16, 20)) 244 #define RCANFD_CFCC_CFM(gpriv, x) (((x) & 0x3) << reg_v3u(gpriv, 8, 16)) 245 #define RCANFD_CFCC_CFIM BIT(12) 246 #define RCANFD_CFCC_CFDC(gpriv, x) (((x) & 0x7) << reg_v3u(gpriv, 21, 8)) 247 #define RCANFD_CFCC_CFPLS(x) (((x) & 0x7) << 4) 248 #define RCANFD_CFCC_CFTXIE BIT(2) 249 #define RCANFD_CFCC_CFE BIT(0) 250 251 /* RSCFDnCFDCFSTSk */ 252 #define RCANFD_CFSTS_CFMC(x) (((x) >> 8) & 0xff) 253 #define RCANFD_CFSTS_CFTXIF BIT(4) 254 #define RCANFD_CFSTS_CFMLT BIT(2) 255 #define RCANFD_CFSTS_CFFLL BIT(1) 256 #define RCANFD_CFSTS_CFEMP BIT(0) 257 258 /* RSCFDnCFDCFIDk */ 259 #define RCANFD_CFID_CFIDE BIT(31) 260 #define RCANFD_CFID_CFRTR BIT(30) 261 #define RCANFD_CFID_CFID_MASK(x) ((x) & 0x1fffffff) 262 263 /* RSCFDnCFDCFPTRk */ 264 #define RCANFD_CFPTR_CFDLC(x) (((x) & 0xf) << 28) 265 #define RCANFD_CFPTR_CFPTR(x) (((x) & 0xfff) << 16) 266 #define RCANFD_CFPTR_CFTS(x) (((x) & 0xff) << 0) 267 268 /* RSCFDnCFDCFFDCSTSk */ 269 #define RCANFD_CFFDCSTS_CFFDF BIT(2) 270 #define RCANFD_CFFDCSTS_CFBRS BIT(1) 271 #define RCANFD_CFFDCSTS_CFESI BIT(0) 272 273 /* This controller supports either Classical CAN only mode or CAN FD only mode. 274 * These modes are supported in two separate set of register maps & names. 275 * However, some of the register offsets are common for both modes. Those 276 * offsets are listed below as Common registers. 277 * 278 * The CAN FD only mode specific registers & Classical CAN only mode specific 279 * registers are listed separately. Their register names starts with 280 * RCANFD_F_xxx & RCANFD_C_xxx respectively. 281 */ 282 283 /* Common registers */ 284 285 /* RSCFDnCFDCmNCFG / RSCFDnCmCFG */ 286 #define RCANFD_CCFG(m) (0x0000 + (0x10 * (m))) 287 /* RSCFDnCFDCmCTR / RSCFDnCmCTR */ 288 #define RCANFD_CCTR(m) (0x0004 + (0x10 * (m))) 289 /* RSCFDnCFDCmSTS / RSCFDnCmSTS */ 290 #define RCANFD_CSTS(m) (0x0008 + (0x10 * (m))) 291 /* RSCFDnCFDCmERFL / RSCFDnCmERFL */ 292 #define RCANFD_CERFL(m) (0x000C + (0x10 * (m))) 293 294 /* RSCFDnCFDGCFG / RSCFDnGCFG */ 295 #define RCANFD_GCFG (0x0084) 296 /* RSCFDnCFDGCTR / RSCFDnGCTR */ 297 #define RCANFD_GCTR (0x0088) 298 /* RSCFDnCFDGCTS / RSCFDnGCTS */ 299 #define RCANFD_GSTS (0x008c) 300 /* RSCFDnCFDGERFL / RSCFDnGERFL */ 301 #define RCANFD_GERFL (0x0090) 302 /* RSCFDnCFDGTSC / RSCFDnGTSC */ 303 #define RCANFD_GTSC (0x0094) 304 /* RSCFDnCFDGAFLECTR / RSCFDnGAFLECTR */ 305 #define RCANFD_GAFLECTR (0x0098) 306 /* RSCFDnCFDGAFLCFG / RSCFDnGAFLCFG */ 307 #define RCANFD_GAFLCFG(ch) (0x009c + (0x04 * ((ch) / 2))) 308 /* RSCFDnCFDRMNB / RSCFDnRMNB */ 309 #define RCANFD_RMNB (0x00a4) 310 /* RSCFDnCFDRMND / RSCFDnRMND */ 311 #define RCANFD_RMND(y) (0x00a8 + (0x04 * (y))) 312 313 /* RSCFDnCFDRFCCx / RSCFDnRFCCx */ 314 #define RCANFD_RFCC(gpriv, x) (reg_v3u(gpriv, 0x00c0, 0x00b8) + (0x04 * (x))) 315 /* RSCFDnCFDRFSTSx / RSCFDnRFSTSx */ 316 #define RCANFD_RFSTS(gpriv, x) (RCANFD_RFCC(gpriv, x) + 0x20) 317 /* RSCFDnCFDRFPCTRx / RSCFDnRFPCTRx */ 318 #define RCANFD_RFPCTR(gpriv, x) (RCANFD_RFCC(gpriv, x) + 0x40) 319 320 /* Common FIFO Control registers */ 321 322 /* RSCFDnCFDCFCCx / RSCFDnCFCCx */ 323 #define RCANFD_CFCC(gpriv, ch, idx) \ 324 (reg_v3u(gpriv, 0x0120, 0x0118) + (0x0c * (ch)) + (0x04 * (idx))) 325 /* RSCFDnCFDCFSTSx / RSCFDnCFSTSx */ 326 #define RCANFD_CFSTS(gpriv, ch, idx) \ 327 (reg_v3u(gpriv, 0x01e0, 0x0178) + (0x0c * (ch)) + (0x04 * (idx))) 328 /* RSCFDnCFDCFPCTRx / RSCFDnCFPCTRx */ 329 #define RCANFD_CFPCTR(gpriv, ch, idx) \ 330 (reg_v3u(gpriv, 0x0240, 0x01d8) + (0x0c * (ch)) + (0x04 * (idx))) 331 332 /* RSCFDnCFDFESTS / RSCFDnFESTS */ 333 #define RCANFD_FESTS (0x0238) 334 /* RSCFDnCFDFFSTS / RSCFDnFFSTS */ 335 #define RCANFD_FFSTS (0x023c) 336 /* RSCFDnCFDFMSTS / RSCFDnFMSTS */ 337 #define RCANFD_FMSTS (0x0240) 338 /* RSCFDnCFDRFISTS / RSCFDnRFISTS */ 339 #define RCANFD_RFISTS (0x0244) 340 /* RSCFDnCFDCFRISTS / RSCFDnCFRISTS */ 341 #define RCANFD_CFRISTS (0x0248) 342 /* RSCFDnCFDCFTISTS / RSCFDnCFTISTS */ 343 #define RCANFD_CFTISTS (0x024c) 344 345 /* RSCFDnCFDTMCp / RSCFDnTMCp */ 346 #define RCANFD_TMC(p) (0x0250 + (0x01 * (p))) 347 /* RSCFDnCFDTMSTSp / RSCFDnTMSTSp */ 348 #define RCANFD_TMSTS(p) (0x02d0 + (0x01 * (p))) 349 350 /* RSCFDnCFDTMTRSTSp / RSCFDnTMTRSTSp */ 351 #define RCANFD_TMTRSTS(y) (0x0350 + (0x04 * (y))) 352 /* RSCFDnCFDTMTARSTSp / RSCFDnTMTARSTSp */ 353 #define RCANFD_TMTARSTS(y) (0x0360 + (0x04 * (y))) 354 /* RSCFDnCFDTMTCSTSp / RSCFDnTMTCSTSp */ 355 #define RCANFD_TMTCSTS(y) (0x0370 + (0x04 * (y))) 356 /* RSCFDnCFDTMTASTSp / RSCFDnTMTASTSp */ 357 #define RCANFD_TMTASTS(y) (0x0380 + (0x04 * (y))) 358 /* RSCFDnCFDTMIECy / RSCFDnTMIECy */ 359 #define RCANFD_TMIEC(y) (0x0390 + (0x04 * (y))) 360 361 /* RSCFDnCFDTXQCCm / RSCFDnTXQCCm */ 362 #define RCANFD_TXQCC(m) (0x03a0 + (0x04 * (m))) 363 /* RSCFDnCFDTXQSTSm / RSCFDnTXQSTSm */ 364 #define RCANFD_TXQSTS(m) (0x03c0 + (0x04 * (m))) 365 /* RSCFDnCFDTXQPCTRm / RSCFDnTXQPCTRm */ 366 #define RCANFD_TXQPCTR(m) (0x03e0 + (0x04 * (m))) 367 368 /* RSCFDnCFDTHLCCm / RSCFDnTHLCCm */ 369 #define RCANFD_THLCC(m) (0x0400 + (0x04 * (m))) 370 /* RSCFDnCFDTHLSTSm / RSCFDnTHLSTSm */ 371 #define RCANFD_THLSTS(m) (0x0420 + (0x04 * (m))) 372 /* RSCFDnCFDTHLPCTRm / RSCFDnTHLPCTRm */ 373 #define RCANFD_THLPCTR(m) (0x0440 + (0x04 * (m))) 374 375 /* RSCFDnCFDGTINTSTS0 / RSCFDnGTINTSTS0 */ 376 #define RCANFD_GTINTSTS0 (0x0460) 377 /* RSCFDnCFDGTINTSTS1 / RSCFDnGTINTSTS1 */ 378 #define RCANFD_GTINTSTS1 (0x0464) 379 /* RSCFDnCFDGTSTCFG / RSCFDnGTSTCFG */ 380 #define RCANFD_GTSTCFG (0x0468) 381 /* RSCFDnCFDGTSTCTR / RSCFDnGTSTCTR */ 382 #define RCANFD_GTSTCTR (0x046c) 383 /* RSCFDnCFDGLOCKK / RSCFDnGLOCKK */ 384 #define RCANFD_GLOCKK (0x047c) 385 /* RSCFDnCFDGRMCFG */ 386 #define RCANFD_GRMCFG (0x04fc) 387 388 /* RSCFDnCFDGAFLIDj / RSCFDnGAFLIDj */ 389 #define RCANFD_GAFLID(offset, j) ((offset) + (0x10 * (j))) 390 /* RSCFDnCFDGAFLMj / RSCFDnGAFLMj */ 391 #define RCANFD_GAFLM(offset, j) ((offset) + 0x04 + (0x10 * (j))) 392 /* RSCFDnCFDGAFLP0j / RSCFDnGAFLP0j */ 393 #define RCANFD_GAFLP0(offset, j) ((offset) + 0x08 + (0x10 * (j))) 394 /* RSCFDnCFDGAFLP1j / RSCFDnGAFLP1j */ 395 #define RCANFD_GAFLP1(offset, j) ((offset) + 0x0c + (0x10 * (j))) 396 397 /* Classical CAN only mode register map */ 398 399 /* RSCFDnGAFLXXXj offset */ 400 #define RCANFD_C_GAFL_OFFSET (0x0500) 401 402 /* RSCFDnRMXXXq -> RCANFD_C_RMXXX(q) */ 403 #define RCANFD_C_RMID(q) (0x0600 + (0x10 * (q))) 404 #define RCANFD_C_RMPTR(q) (0x0604 + (0x10 * (q))) 405 #define RCANFD_C_RMDF0(q) (0x0608 + (0x10 * (q))) 406 #define RCANFD_C_RMDF1(q) (0x060c + (0x10 * (q))) 407 408 /* RSCFDnRFXXx -> RCANFD_C_RFXX(x) */ 409 #define RCANFD_C_RFOFFSET (0x0e00) 410 #define RCANFD_C_RFID(x) (RCANFD_C_RFOFFSET + (0x10 * (x))) 411 #define RCANFD_C_RFPTR(x) (RCANFD_C_RFOFFSET + 0x04 + (0x10 * (x))) 412 #define RCANFD_C_RFDF(x, df) \ 413 (RCANFD_C_RFOFFSET + 0x08 + (0x10 * (x)) + (0x04 * (df))) 414 415 /* RSCFDnCFXXk -> RCANFD_C_CFXX(ch, k) */ 416 #define RCANFD_C_CFOFFSET (0x0e80) 417 418 #define RCANFD_C_CFID(ch, idx) \ 419 (RCANFD_C_CFOFFSET + (0x30 * (ch)) + (0x10 * (idx))) 420 421 #define RCANFD_C_CFPTR(ch, idx) \ 422 (RCANFD_C_CFOFFSET + 0x04 + (0x30 * (ch)) + (0x10 * (idx))) 423 424 #define RCANFD_C_CFDF(ch, idx, df) \ 425 (RCANFD_C_CFOFFSET + 0x08 + (0x30 * (ch)) + (0x10 * (idx)) + (0x04 * (df))) 426 427 /* RSCFDnTMXXp -> RCANFD_C_TMXX(p) */ 428 #define RCANFD_C_TMID(p) (0x1000 + (0x10 * (p))) 429 #define RCANFD_C_TMPTR(p) (0x1004 + (0x10 * (p))) 430 #define RCANFD_C_TMDF0(p) (0x1008 + (0x10 * (p))) 431 #define RCANFD_C_TMDF1(p) (0x100c + (0x10 * (p))) 432 433 /* RSCFDnTHLACCm */ 434 #define RCANFD_C_THLACC(m) (0x1800 + (0x04 * (m))) 435 /* RSCFDnRPGACCr */ 436 #define RCANFD_C_RPGACC(r) (0x1900 + (0x04 * (r))) 437 438 /* R-Car V3U Classical and CAN FD mode specific register map */ 439 #define RCANFD_V3U_CFDCFG (0x1314) 440 #define RCANFD_V3U_DCFG(m) (0x1400 + (0x20 * (m))) 441 442 #define RCANFD_V3U_GAFL_OFFSET (0x1800) 443 444 /* CAN FD mode specific register map */ 445 446 /* RSCFDnCFDCmXXX -> RCANFD_F_XXX(m) */ 447 #define RCANFD_F_DCFG(m) (0x0500 + (0x20 * (m))) 448 #define RCANFD_F_CFDCFG(m) (0x0504 + (0x20 * (m))) 449 #define RCANFD_F_CFDCTR(m) (0x0508 + (0x20 * (m))) 450 #define RCANFD_F_CFDSTS(m) (0x050c + (0x20 * (m))) 451 #define RCANFD_F_CFDCRC(m) (0x0510 + (0x20 * (m))) 452 453 /* RSCFDnCFDGAFLXXXj offset */ 454 #define RCANFD_F_GAFL_OFFSET (0x1000) 455 456 /* RSCFDnCFDRMXXXq -> RCANFD_F_RMXXX(q) */ 457 #define RCANFD_F_RMID(q) (0x2000 + (0x20 * (q))) 458 #define RCANFD_F_RMPTR(q) (0x2004 + (0x20 * (q))) 459 #define RCANFD_F_RMFDSTS(q) (0x2008 + (0x20 * (q))) 460 #define RCANFD_F_RMDF(q, b) (0x200c + (0x04 * (b)) + (0x20 * (q))) 461 462 /* RSCFDnCFDRFXXx -> RCANFD_F_RFXX(x) */ 463 #define RCANFD_F_RFOFFSET(gpriv) reg_v3u(gpriv, 0x6000, 0x3000) 464 #define RCANFD_F_RFID(gpriv, x) (RCANFD_F_RFOFFSET(gpriv) + (0x80 * (x))) 465 #define RCANFD_F_RFPTR(gpriv, x) (RCANFD_F_RFOFFSET(gpriv) + 0x04 + (0x80 * (x))) 466 #define RCANFD_F_RFFDSTS(gpriv, x) (RCANFD_F_RFOFFSET(gpriv) + 0x08 + (0x80 * (x))) 467 #define RCANFD_F_RFDF(gpriv, x, df) \ 468 (RCANFD_F_RFOFFSET(gpriv) + 0x0c + (0x80 * (x)) + (0x04 * (df))) 469 470 /* RSCFDnCFDCFXXk -> RCANFD_F_CFXX(ch, k) */ 471 #define RCANFD_F_CFOFFSET(gpriv) reg_v3u(gpriv, 0x6400, 0x3400) 472 473 #define RCANFD_F_CFID(gpriv, ch, idx) \ 474 (RCANFD_F_CFOFFSET(gpriv) + (0x180 * (ch)) + (0x80 * (idx))) 475 476 #define RCANFD_F_CFPTR(gpriv, ch, idx) \ 477 (RCANFD_F_CFOFFSET(gpriv) + 0x04 + (0x180 * (ch)) + (0x80 * (idx))) 478 479 #define RCANFD_F_CFFDCSTS(gpriv, ch, idx) \ 480 (RCANFD_F_CFOFFSET(gpriv) + 0x08 + (0x180 * (ch)) + (0x80 * (idx))) 481 482 #define RCANFD_F_CFDF(gpriv, ch, idx, df) \ 483 (RCANFD_F_CFOFFSET(gpriv) + 0x0c + (0x180 * (ch)) + (0x80 * (idx)) + \ 484 (0x04 * (df))) 485 486 /* RSCFDnCFDTMXXp -> RCANFD_F_TMXX(p) */ 487 #define RCANFD_F_TMID(p) (0x4000 + (0x20 * (p))) 488 #define RCANFD_F_TMPTR(p) (0x4004 + (0x20 * (p))) 489 #define RCANFD_F_TMFDCTR(p) (0x4008 + (0x20 * (p))) 490 #define RCANFD_F_TMDF(p, b) (0x400c + (0x20 * (p)) + (0x04 * (b))) 491 492 /* RSCFDnCFDTHLACCm */ 493 #define RCANFD_F_THLACC(m) (0x6000 + (0x04 * (m))) 494 /* RSCFDnCFDRPGACCr */ 495 #define RCANFD_F_RPGACC(r) (0x6400 + (0x04 * (r))) 496 497 /* Constants */ 498 #define RCANFD_FIFO_DEPTH 8 /* Tx FIFO depth */ 499 #define RCANFD_NAPI_WEIGHT 8 /* Rx poll quota */ 500 501 #define RCANFD_NUM_CHANNELS 8 /* Eight channels max */ 502 #define RCANFD_CHANNELS_MASK BIT((RCANFD_NUM_CHANNELS) - 1) 503 504 #define RCANFD_GAFL_PAGENUM(entry) ((entry) / 16) 505 #define RCANFD_CHANNEL_NUMRULES 1 /* only one rule per channel */ 506 507 /* Rx FIFO is a global resource of the controller. There are 8 such FIFOs 508 * available. Each channel gets a dedicated Rx FIFO (i.e.) the channel 509 * number is added to RFFIFO index. 510 */ 511 #define RCANFD_RFFIFO_IDX 0 512 513 /* Tx/Rx or Common FIFO is a per channel resource. Each channel has 3 Common 514 * FIFOs dedicated to them. Use the first (index 0) FIFO out of the 3 for Tx. 515 */ 516 #define RCANFD_CFFIFO_IDX 0 517 518 /* fCAN clock select register settings */ 519 enum rcar_canfd_fcanclk { 520 RCANFD_CANFDCLK = 0, /* CANFD clock */ 521 RCANFD_EXTCLK, /* Externally input clock */ 522 }; 523 524 struct rcar_canfd_global; 525 526 /* Channel priv data */ 527 struct rcar_canfd_channel { 528 struct can_priv can; /* Must be the first member */ 529 struct net_device *ndev; 530 struct rcar_canfd_global *gpriv; /* Controller reference */ 531 void __iomem *base; /* Register base address */ 532 struct napi_struct napi; 533 u32 tx_head; /* Incremented on xmit */ 534 u32 tx_tail; /* Incremented on xmit done */ 535 u32 channel; /* Channel number */ 536 spinlock_t tx_lock; /* To protect tx path */ 537 }; 538 539 /* Global priv data */ 540 struct rcar_canfd_global { 541 struct rcar_canfd_channel *ch[RCANFD_NUM_CHANNELS]; 542 void __iomem *base; /* Register base address */ 543 struct platform_device *pdev; /* Respective platform device */ 544 struct clk *clkp; /* Peripheral clock */ 545 struct clk *can_clk; /* fCAN clock */ 546 enum rcar_canfd_fcanclk fcan; /* CANFD or Ext clock */ 547 unsigned long channels_mask; /* Enabled channels mask */ 548 bool fdmode; /* CAN FD or Classical CAN only mode */ 549 struct reset_control *rstc1; 550 struct reset_control *rstc2; 551 enum rcanfd_chip_id chip_id; 552 u32 max_channels; 553 }; 554 555 /* CAN FD mode nominal rate constants */ 556 static const struct can_bittiming_const rcar_canfd_nom_bittiming_const = { 557 .name = RCANFD_DRV_NAME, 558 .tseg1_min = 2, 559 .tseg1_max = 128, 560 .tseg2_min = 2, 561 .tseg2_max = 32, 562 .sjw_max = 32, 563 .brp_min = 1, 564 .brp_max = 1024, 565 .brp_inc = 1, 566 }; 567 568 /* CAN FD mode data rate constants */ 569 static const struct can_bittiming_const rcar_canfd_data_bittiming_const = { 570 .name = RCANFD_DRV_NAME, 571 .tseg1_min = 2, 572 .tseg1_max = 16, 573 .tseg2_min = 2, 574 .tseg2_max = 8, 575 .sjw_max = 8, 576 .brp_min = 1, 577 .brp_max = 256, 578 .brp_inc = 1, 579 }; 580 581 /* Classical CAN mode bitrate constants */ 582 static const struct can_bittiming_const rcar_canfd_bittiming_const = { 583 .name = RCANFD_DRV_NAME, 584 .tseg1_min = 4, 585 .tseg1_max = 16, 586 .tseg2_min = 2, 587 .tseg2_max = 8, 588 .sjw_max = 4, 589 .brp_min = 1, 590 .brp_max = 1024, 591 .brp_inc = 1, 592 }; 593 594 /* Helper functions */ 595 static inline bool is_v3u(struct rcar_canfd_global *gpriv) 596 { 597 return gpriv->chip_id == RENESAS_R8A779A0; 598 } 599 600 static inline u32 reg_v3u(struct rcar_canfd_global *gpriv, 601 u32 v3u, u32 not_v3u) 602 { 603 return is_v3u(gpriv) ? v3u : not_v3u; 604 } 605 606 static inline void rcar_canfd_update(u32 mask, u32 val, u32 __iomem *reg) 607 { 608 u32 data = readl(reg); 609 610 data &= ~mask; 611 data |= (val & mask); 612 writel(data, reg); 613 } 614 615 static inline u32 rcar_canfd_read(void __iomem *base, u32 offset) 616 { 617 return readl(base + (offset)); 618 } 619 620 static inline void rcar_canfd_write(void __iomem *base, u32 offset, u32 val) 621 { 622 writel(val, base + (offset)); 623 } 624 625 static void rcar_canfd_set_bit(void __iomem *base, u32 reg, u32 val) 626 { 627 rcar_canfd_update(val, val, base + (reg)); 628 } 629 630 static void rcar_canfd_clear_bit(void __iomem *base, u32 reg, u32 val) 631 { 632 rcar_canfd_update(val, 0, base + (reg)); 633 } 634 635 static void rcar_canfd_update_bit(void __iomem *base, u32 reg, 636 u32 mask, u32 val) 637 { 638 rcar_canfd_update(mask, val, base + (reg)); 639 } 640 641 static void rcar_canfd_get_data(struct rcar_canfd_channel *priv, 642 struct canfd_frame *cf, u32 off) 643 { 644 u32 i, lwords; 645 646 lwords = DIV_ROUND_UP(cf->len, sizeof(u32)); 647 for (i = 0; i < lwords; i++) 648 *((u32 *)cf->data + i) = 649 rcar_canfd_read(priv->base, off + (i * sizeof(u32))); 650 } 651 652 static void rcar_canfd_put_data(struct rcar_canfd_channel *priv, 653 struct canfd_frame *cf, u32 off) 654 { 655 u32 i, lwords; 656 657 lwords = DIV_ROUND_UP(cf->len, sizeof(u32)); 658 for (i = 0; i < lwords; i++) 659 rcar_canfd_write(priv->base, off + (i * sizeof(u32)), 660 *((u32 *)cf->data + i)); 661 } 662 663 static void rcar_canfd_tx_failure_cleanup(struct net_device *ndev) 664 { 665 u32 i; 666 667 for (i = 0; i < RCANFD_FIFO_DEPTH; i++) 668 can_free_echo_skb(ndev, i, NULL); 669 } 670 671 static void rcar_canfd_set_mode(struct rcar_canfd_global *gpriv) 672 { 673 if (is_v3u(gpriv)) { 674 if (gpriv->fdmode) 675 rcar_canfd_set_bit(gpriv->base, RCANFD_V3U_CFDCFG, 676 RCANFD_FDCFG_FDOE); 677 else 678 rcar_canfd_set_bit(gpriv->base, RCANFD_V3U_CFDCFG, 679 RCANFD_FDCFG_CLOE); 680 } else { 681 if (gpriv->fdmode) 682 rcar_canfd_set_bit(gpriv->base, RCANFD_GRMCFG, 683 RCANFD_GRMCFG_RCMC); 684 else 685 rcar_canfd_clear_bit(gpriv->base, RCANFD_GRMCFG, 686 RCANFD_GRMCFG_RCMC); 687 } 688 } 689 690 static int rcar_canfd_reset_controller(struct rcar_canfd_global *gpriv) 691 { 692 u32 sts, ch; 693 int err; 694 695 /* Check RAMINIT flag as CAN RAM initialization takes place 696 * after the MCU reset 697 */ 698 err = readl_poll_timeout((gpriv->base + RCANFD_GSTS), sts, 699 !(sts & RCANFD_GSTS_GRAMINIT), 2, 500000); 700 if (err) { 701 dev_dbg(&gpriv->pdev->dev, "global raminit failed\n"); 702 return err; 703 } 704 705 /* Transition to Global Reset mode */ 706 rcar_canfd_clear_bit(gpriv->base, RCANFD_GCTR, RCANFD_GCTR_GSLPR); 707 rcar_canfd_update_bit(gpriv->base, RCANFD_GCTR, 708 RCANFD_GCTR_GMDC_MASK, RCANFD_GCTR_GMDC_GRESET); 709 710 /* Ensure Global reset mode */ 711 err = readl_poll_timeout((gpriv->base + RCANFD_GSTS), sts, 712 (sts & RCANFD_GSTS_GRSTSTS), 2, 500000); 713 if (err) { 714 dev_dbg(&gpriv->pdev->dev, "global reset failed\n"); 715 return err; 716 } 717 718 /* Reset Global error flags */ 719 rcar_canfd_write(gpriv->base, RCANFD_GERFL, 0x0); 720 721 /* Set the controller into appropriate mode */ 722 rcar_canfd_set_mode(gpriv); 723 724 /* Transition all Channels to reset mode */ 725 for_each_set_bit(ch, &gpriv->channels_mask, gpriv->max_channels) { 726 rcar_canfd_clear_bit(gpriv->base, 727 RCANFD_CCTR(ch), RCANFD_CCTR_CSLPR); 728 729 rcar_canfd_update_bit(gpriv->base, RCANFD_CCTR(ch), 730 RCANFD_CCTR_CHMDC_MASK, 731 RCANFD_CCTR_CHDMC_CRESET); 732 733 /* Ensure Channel reset mode */ 734 err = readl_poll_timeout((gpriv->base + RCANFD_CSTS(ch)), sts, 735 (sts & RCANFD_CSTS_CRSTSTS), 736 2, 500000); 737 if (err) { 738 dev_dbg(&gpriv->pdev->dev, 739 "channel %u reset failed\n", ch); 740 return err; 741 } 742 } 743 return 0; 744 } 745 746 static void rcar_canfd_configure_controller(struct rcar_canfd_global *gpriv) 747 { 748 u32 cfg, ch; 749 750 /* Global configuration settings */ 751 752 /* ECC Error flag Enable */ 753 cfg = RCANFD_GCFG_EEFE; 754 755 if (gpriv->fdmode) 756 /* Truncate payload to configured message size RFPLS */ 757 cfg |= RCANFD_GCFG_CMPOC; 758 759 /* Set External Clock if selected */ 760 if (gpriv->fcan != RCANFD_CANFDCLK) 761 cfg |= RCANFD_GCFG_DCS; 762 763 rcar_canfd_set_bit(gpriv->base, RCANFD_GCFG, cfg); 764 765 /* Channel configuration settings */ 766 for_each_set_bit(ch, &gpriv->channels_mask, gpriv->max_channels) { 767 rcar_canfd_set_bit(gpriv->base, RCANFD_CCTR(ch), 768 RCANFD_CCTR_ERRD); 769 rcar_canfd_update_bit(gpriv->base, RCANFD_CCTR(ch), 770 RCANFD_CCTR_BOM_MASK, 771 RCANFD_CCTR_BOM_BENTRY); 772 } 773 } 774 775 static void rcar_canfd_configure_afl_rules(struct rcar_canfd_global *gpriv, 776 u32 ch) 777 { 778 u32 cfg; 779 int offset, start, page, num_rules = RCANFD_CHANNEL_NUMRULES; 780 u32 ridx = ch + RCANFD_RFFIFO_IDX; 781 782 if (ch == 0) { 783 start = 0; /* Channel 0 always starts from 0th rule */ 784 } else { 785 /* Get number of Channel 0 rules and adjust */ 786 cfg = rcar_canfd_read(gpriv->base, RCANFD_GAFLCFG(ch)); 787 start = RCANFD_GAFLCFG_GETRNC(gpriv, 0, cfg); 788 } 789 790 /* Enable write access to entry */ 791 page = RCANFD_GAFL_PAGENUM(start); 792 rcar_canfd_set_bit(gpriv->base, RCANFD_GAFLECTR, 793 (RCANFD_GAFLECTR_AFLPN(gpriv, page) | 794 RCANFD_GAFLECTR_AFLDAE)); 795 796 /* Write number of rules for channel */ 797 rcar_canfd_set_bit(gpriv->base, RCANFD_GAFLCFG(ch), 798 RCANFD_GAFLCFG_SETRNC(gpriv, ch, num_rules)); 799 if (is_v3u(gpriv)) 800 offset = RCANFD_V3U_GAFL_OFFSET; 801 else if (gpriv->fdmode) 802 offset = RCANFD_F_GAFL_OFFSET; 803 else 804 offset = RCANFD_C_GAFL_OFFSET; 805 806 /* Accept all IDs */ 807 rcar_canfd_write(gpriv->base, RCANFD_GAFLID(offset, start), 0); 808 /* IDE or RTR is not considered for matching */ 809 rcar_canfd_write(gpriv->base, RCANFD_GAFLM(offset, start), 0); 810 /* Any data length accepted */ 811 rcar_canfd_write(gpriv->base, RCANFD_GAFLP0(offset, start), 0); 812 /* Place the msg in corresponding Rx FIFO entry */ 813 rcar_canfd_set_bit(gpriv->base, RCANFD_GAFLP1(offset, start), 814 RCANFD_GAFLP1_GAFLFDP(ridx)); 815 816 /* Disable write access to page */ 817 rcar_canfd_clear_bit(gpriv->base, 818 RCANFD_GAFLECTR, RCANFD_GAFLECTR_AFLDAE); 819 } 820 821 static void rcar_canfd_configure_rx(struct rcar_canfd_global *gpriv, u32 ch) 822 { 823 /* Rx FIFO is used for reception */ 824 u32 cfg; 825 u16 rfdc, rfpls; 826 827 /* Select Rx FIFO based on channel */ 828 u32 ridx = ch + RCANFD_RFFIFO_IDX; 829 830 rfdc = 2; /* b010 - 8 messages Rx FIFO depth */ 831 if (gpriv->fdmode) 832 rfpls = 7; /* b111 - Max 64 bytes payload */ 833 else 834 rfpls = 0; /* b000 - Max 8 bytes payload */ 835 836 cfg = (RCANFD_RFCC_RFIM | RCANFD_RFCC_RFDC(rfdc) | 837 RCANFD_RFCC_RFPLS(rfpls) | RCANFD_RFCC_RFIE); 838 rcar_canfd_write(gpriv->base, RCANFD_RFCC(gpriv, ridx), cfg); 839 } 840 841 static void rcar_canfd_configure_tx(struct rcar_canfd_global *gpriv, u32 ch) 842 { 843 /* Tx/Rx(Common) FIFO configured in Tx mode is 844 * used for transmission 845 * 846 * Each channel has 3 Common FIFO dedicated to them. 847 * Use the 1st (index 0) out of 3 848 */ 849 u32 cfg; 850 u16 cftml, cfm, cfdc, cfpls; 851 852 cftml = 0; /* 0th buffer */ 853 cfm = 1; /* b01 - Transmit mode */ 854 cfdc = 2; /* b010 - 8 messages Tx FIFO depth */ 855 if (gpriv->fdmode) 856 cfpls = 7; /* b111 - Max 64 bytes payload */ 857 else 858 cfpls = 0; /* b000 - Max 8 bytes payload */ 859 860 cfg = (RCANFD_CFCC_CFTML(gpriv, cftml) | RCANFD_CFCC_CFM(gpriv, cfm) | 861 RCANFD_CFCC_CFIM | RCANFD_CFCC_CFDC(gpriv, cfdc) | 862 RCANFD_CFCC_CFPLS(cfpls) | RCANFD_CFCC_CFTXIE); 863 rcar_canfd_write(gpriv->base, RCANFD_CFCC(gpriv, ch, RCANFD_CFFIFO_IDX), cfg); 864 865 if (gpriv->fdmode) 866 /* Clear FD mode specific control/status register */ 867 rcar_canfd_write(gpriv->base, 868 RCANFD_F_CFFDCSTS(gpriv, ch, RCANFD_CFFIFO_IDX), 0); 869 } 870 871 static void rcar_canfd_enable_global_interrupts(struct rcar_canfd_global *gpriv) 872 { 873 u32 ctr; 874 875 /* Clear any stray error interrupt flags */ 876 rcar_canfd_write(gpriv->base, RCANFD_GERFL, 0); 877 878 /* Global interrupts setup */ 879 ctr = RCANFD_GCTR_MEIE; 880 if (gpriv->fdmode) 881 ctr |= RCANFD_GCTR_CFMPOFIE; 882 883 rcar_canfd_set_bit(gpriv->base, RCANFD_GCTR, ctr); 884 } 885 886 static void rcar_canfd_disable_global_interrupts(struct rcar_canfd_global 887 *gpriv) 888 { 889 /* Disable all interrupts */ 890 rcar_canfd_write(gpriv->base, RCANFD_GCTR, 0); 891 892 /* Clear any stray error interrupt flags */ 893 rcar_canfd_write(gpriv->base, RCANFD_GERFL, 0); 894 } 895 896 static void rcar_canfd_enable_channel_interrupts(struct rcar_canfd_channel 897 *priv) 898 { 899 u32 ctr, ch = priv->channel; 900 901 /* Clear any stray error flags */ 902 rcar_canfd_write(priv->base, RCANFD_CERFL(ch), 0); 903 904 /* Channel interrupts setup */ 905 ctr = (RCANFD_CCTR_TAIE | 906 RCANFD_CCTR_ALIE | RCANFD_CCTR_BLIE | 907 RCANFD_CCTR_OLIE | RCANFD_CCTR_BORIE | 908 RCANFD_CCTR_BOEIE | RCANFD_CCTR_EPIE | 909 RCANFD_CCTR_EWIE | RCANFD_CCTR_BEIE); 910 rcar_canfd_set_bit(priv->base, RCANFD_CCTR(ch), ctr); 911 } 912 913 static void rcar_canfd_disable_channel_interrupts(struct rcar_canfd_channel 914 *priv) 915 { 916 u32 ctr, ch = priv->channel; 917 918 ctr = (RCANFD_CCTR_TAIE | 919 RCANFD_CCTR_ALIE | RCANFD_CCTR_BLIE | 920 RCANFD_CCTR_OLIE | RCANFD_CCTR_BORIE | 921 RCANFD_CCTR_BOEIE | RCANFD_CCTR_EPIE | 922 RCANFD_CCTR_EWIE | RCANFD_CCTR_BEIE); 923 rcar_canfd_clear_bit(priv->base, RCANFD_CCTR(ch), ctr); 924 925 /* Clear any stray error flags */ 926 rcar_canfd_write(priv->base, RCANFD_CERFL(ch), 0); 927 } 928 929 static void rcar_canfd_global_error(struct net_device *ndev) 930 { 931 struct rcar_canfd_channel *priv = netdev_priv(ndev); 932 struct rcar_canfd_global *gpriv = priv->gpriv; 933 struct net_device_stats *stats = &ndev->stats; 934 u32 ch = priv->channel; 935 u32 gerfl, sts; 936 u32 ridx = ch + RCANFD_RFFIFO_IDX; 937 938 gerfl = rcar_canfd_read(priv->base, RCANFD_GERFL); 939 if ((gerfl & RCANFD_GERFL_EEF0) && (ch == 0)) { 940 netdev_dbg(ndev, "Ch0: ECC Error flag\n"); 941 stats->tx_dropped++; 942 } 943 if ((gerfl & RCANFD_GERFL_EEF1) && (ch == 1)) { 944 netdev_dbg(ndev, "Ch1: ECC Error flag\n"); 945 stats->tx_dropped++; 946 } 947 if (gerfl & RCANFD_GERFL_MES) { 948 sts = rcar_canfd_read(priv->base, 949 RCANFD_CFSTS(gpriv, ch, RCANFD_CFFIFO_IDX)); 950 if (sts & RCANFD_CFSTS_CFMLT) { 951 netdev_dbg(ndev, "Tx Message Lost flag\n"); 952 stats->tx_dropped++; 953 rcar_canfd_write(priv->base, 954 RCANFD_CFSTS(gpriv, ch, RCANFD_CFFIFO_IDX), 955 sts & ~RCANFD_CFSTS_CFMLT); 956 } 957 958 sts = rcar_canfd_read(priv->base, RCANFD_RFSTS(gpriv, ridx)); 959 if (sts & RCANFD_RFSTS_RFMLT) { 960 netdev_dbg(ndev, "Rx Message Lost flag\n"); 961 stats->rx_dropped++; 962 rcar_canfd_write(priv->base, RCANFD_RFSTS(gpriv, ridx), 963 sts & ~RCANFD_RFSTS_RFMLT); 964 } 965 } 966 if (gpriv->fdmode && gerfl & RCANFD_GERFL_CMPOF) { 967 /* Message Lost flag will be set for respective channel 968 * when this condition happens with counters and flags 969 * already updated. 970 */ 971 netdev_dbg(ndev, "global payload overflow interrupt\n"); 972 } 973 974 /* Clear all global error interrupts. Only affected channels bits 975 * get cleared 976 */ 977 rcar_canfd_write(priv->base, RCANFD_GERFL, 0); 978 } 979 980 static void rcar_canfd_error(struct net_device *ndev, u32 cerfl, 981 u16 txerr, u16 rxerr) 982 { 983 struct rcar_canfd_channel *priv = netdev_priv(ndev); 984 struct net_device_stats *stats = &ndev->stats; 985 struct can_frame *cf; 986 struct sk_buff *skb; 987 u32 ch = priv->channel; 988 989 netdev_dbg(ndev, "ch erfl %x txerr %u rxerr %u\n", cerfl, txerr, rxerr); 990 991 /* Propagate the error condition to the CAN stack */ 992 skb = alloc_can_err_skb(ndev, &cf); 993 if (!skb) { 994 stats->rx_dropped++; 995 return; 996 } 997 998 /* Channel error interrupts */ 999 if (cerfl & RCANFD_CERFL_BEF) { 1000 netdev_dbg(ndev, "Bus error\n"); 1001 cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT; 1002 cf->data[2] = CAN_ERR_PROT_UNSPEC; 1003 priv->can.can_stats.bus_error++; 1004 } 1005 if (cerfl & RCANFD_CERFL_ADERR) { 1006 netdev_dbg(ndev, "ACK Delimiter Error\n"); 1007 stats->tx_errors++; 1008 cf->data[3] |= CAN_ERR_PROT_LOC_ACK_DEL; 1009 } 1010 if (cerfl & RCANFD_CERFL_B0ERR) { 1011 netdev_dbg(ndev, "Bit Error (dominant)\n"); 1012 stats->tx_errors++; 1013 cf->data[2] |= CAN_ERR_PROT_BIT0; 1014 } 1015 if (cerfl & RCANFD_CERFL_B1ERR) { 1016 netdev_dbg(ndev, "Bit Error (recessive)\n"); 1017 stats->tx_errors++; 1018 cf->data[2] |= CAN_ERR_PROT_BIT1; 1019 } 1020 if (cerfl & RCANFD_CERFL_CERR) { 1021 netdev_dbg(ndev, "CRC Error\n"); 1022 stats->rx_errors++; 1023 cf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ; 1024 } 1025 if (cerfl & RCANFD_CERFL_AERR) { 1026 netdev_dbg(ndev, "ACK Error\n"); 1027 stats->tx_errors++; 1028 cf->can_id |= CAN_ERR_ACK; 1029 cf->data[3] |= CAN_ERR_PROT_LOC_ACK; 1030 } 1031 if (cerfl & RCANFD_CERFL_FERR) { 1032 netdev_dbg(ndev, "Form Error\n"); 1033 stats->rx_errors++; 1034 cf->data[2] |= CAN_ERR_PROT_FORM; 1035 } 1036 if (cerfl & RCANFD_CERFL_SERR) { 1037 netdev_dbg(ndev, "Stuff Error\n"); 1038 stats->rx_errors++; 1039 cf->data[2] |= CAN_ERR_PROT_STUFF; 1040 } 1041 if (cerfl & RCANFD_CERFL_ALF) { 1042 netdev_dbg(ndev, "Arbitration lost Error\n"); 1043 priv->can.can_stats.arbitration_lost++; 1044 cf->can_id |= CAN_ERR_LOSTARB; 1045 cf->data[0] |= CAN_ERR_LOSTARB_UNSPEC; 1046 } 1047 if (cerfl & RCANFD_CERFL_BLF) { 1048 netdev_dbg(ndev, "Bus Lock Error\n"); 1049 stats->rx_errors++; 1050 cf->can_id |= CAN_ERR_BUSERROR; 1051 } 1052 if (cerfl & RCANFD_CERFL_EWF) { 1053 netdev_dbg(ndev, "Error warning interrupt\n"); 1054 priv->can.state = CAN_STATE_ERROR_WARNING; 1055 priv->can.can_stats.error_warning++; 1056 cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT; 1057 cf->data[1] = txerr > rxerr ? CAN_ERR_CRTL_TX_WARNING : 1058 CAN_ERR_CRTL_RX_WARNING; 1059 cf->data[6] = txerr; 1060 cf->data[7] = rxerr; 1061 } 1062 if (cerfl & RCANFD_CERFL_EPF) { 1063 netdev_dbg(ndev, "Error passive interrupt\n"); 1064 priv->can.state = CAN_STATE_ERROR_PASSIVE; 1065 priv->can.can_stats.error_passive++; 1066 cf->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT; 1067 cf->data[1] = txerr > rxerr ? CAN_ERR_CRTL_TX_PASSIVE : 1068 CAN_ERR_CRTL_RX_PASSIVE; 1069 cf->data[6] = txerr; 1070 cf->data[7] = rxerr; 1071 } 1072 if (cerfl & RCANFD_CERFL_BOEF) { 1073 netdev_dbg(ndev, "Bus-off entry interrupt\n"); 1074 rcar_canfd_tx_failure_cleanup(ndev); 1075 priv->can.state = CAN_STATE_BUS_OFF; 1076 priv->can.can_stats.bus_off++; 1077 can_bus_off(ndev); 1078 cf->can_id |= CAN_ERR_BUSOFF; 1079 } 1080 if (cerfl & RCANFD_CERFL_OVLF) { 1081 netdev_dbg(ndev, 1082 "Overload Frame Transmission error interrupt\n"); 1083 stats->tx_errors++; 1084 cf->can_id |= CAN_ERR_PROT; 1085 cf->data[2] |= CAN_ERR_PROT_OVERLOAD; 1086 } 1087 1088 /* Clear channel error interrupts that are handled */ 1089 rcar_canfd_write(priv->base, RCANFD_CERFL(ch), 1090 RCANFD_CERFL_ERR(~cerfl)); 1091 netif_rx(skb); 1092 } 1093 1094 static void rcar_canfd_tx_done(struct net_device *ndev) 1095 { 1096 struct rcar_canfd_channel *priv = netdev_priv(ndev); 1097 struct rcar_canfd_global *gpriv = priv->gpriv; 1098 struct net_device_stats *stats = &ndev->stats; 1099 u32 sts; 1100 unsigned long flags; 1101 u32 ch = priv->channel; 1102 1103 do { 1104 u8 unsent, sent; 1105 1106 sent = priv->tx_tail % RCANFD_FIFO_DEPTH; 1107 stats->tx_packets++; 1108 stats->tx_bytes += can_get_echo_skb(ndev, sent, NULL); 1109 1110 spin_lock_irqsave(&priv->tx_lock, flags); 1111 priv->tx_tail++; 1112 sts = rcar_canfd_read(priv->base, 1113 RCANFD_CFSTS(gpriv, ch, RCANFD_CFFIFO_IDX)); 1114 unsent = RCANFD_CFSTS_CFMC(sts); 1115 1116 /* Wake producer only when there is room */ 1117 if (unsent != RCANFD_FIFO_DEPTH) 1118 netif_wake_queue(ndev); 1119 1120 if (priv->tx_head - priv->tx_tail <= unsent) { 1121 spin_unlock_irqrestore(&priv->tx_lock, flags); 1122 break; 1123 } 1124 spin_unlock_irqrestore(&priv->tx_lock, flags); 1125 1126 } while (1); 1127 1128 /* Clear interrupt */ 1129 rcar_canfd_write(priv->base, RCANFD_CFSTS(gpriv, ch, RCANFD_CFFIFO_IDX), 1130 sts & ~RCANFD_CFSTS_CFTXIF); 1131 } 1132 1133 static void rcar_canfd_handle_global_err(struct rcar_canfd_global *gpriv, u32 ch) 1134 { 1135 struct rcar_canfd_channel *priv = gpriv->ch[ch]; 1136 struct net_device *ndev = priv->ndev; 1137 u32 gerfl; 1138 1139 /* Handle global error interrupts */ 1140 gerfl = rcar_canfd_read(priv->base, RCANFD_GERFL); 1141 if (unlikely(RCANFD_GERFL_ERR(gpriv, gerfl))) 1142 rcar_canfd_global_error(ndev); 1143 } 1144 1145 static irqreturn_t rcar_canfd_global_err_interrupt(int irq, void *dev_id) 1146 { 1147 struct rcar_canfd_global *gpriv = dev_id; 1148 u32 ch; 1149 1150 for_each_set_bit(ch, &gpriv->channels_mask, gpriv->max_channels) 1151 rcar_canfd_handle_global_err(gpriv, ch); 1152 1153 return IRQ_HANDLED; 1154 } 1155 1156 static void rcar_canfd_handle_global_receive(struct rcar_canfd_global *gpriv, u32 ch) 1157 { 1158 struct rcar_canfd_channel *priv = gpriv->ch[ch]; 1159 u32 ridx = ch + RCANFD_RFFIFO_IDX; 1160 u32 sts, cc; 1161 1162 /* Handle Rx interrupts */ 1163 sts = rcar_canfd_read(priv->base, RCANFD_RFSTS(gpriv, ridx)); 1164 cc = rcar_canfd_read(priv->base, RCANFD_RFCC(gpriv, ridx)); 1165 if (likely(sts & RCANFD_RFSTS_RFIF && 1166 cc & RCANFD_RFCC_RFIE)) { 1167 if (napi_schedule_prep(&priv->napi)) { 1168 /* Disable Rx FIFO interrupts */ 1169 rcar_canfd_clear_bit(priv->base, 1170 RCANFD_RFCC(gpriv, ridx), 1171 RCANFD_RFCC_RFIE); 1172 __napi_schedule(&priv->napi); 1173 } 1174 } 1175 } 1176 1177 static irqreturn_t rcar_canfd_global_receive_fifo_interrupt(int irq, void *dev_id) 1178 { 1179 struct rcar_canfd_global *gpriv = dev_id; 1180 u32 ch; 1181 1182 for_each_set_bit(ch, &gpriv->channels_mask, gpriv->max_channels) 1183 rcar_canfd_handle_global_receive(gpriv, ch); 1184 1185 return IRQ_HANDLED; 1186 } 1187 1188 static irqreturn_t rcar_canfd_global_interrupt(int irq, void *dev_id) 1189 { 1190 struct rcar_canfd_global *gpriv = dev_id; 1191 u32 ch; 1192 1193 /* Global error interrupts still indicate a condition specific 1194 * to a channel. RxFIFO interrupt is a global interrupt. 1195 */ 1196 for_each_set_bit(ch, &gpriv->channels_mask, gpriv->max_channels) { 1197 rcar_canfd_handle_global_err(gpriv, ch); 1198 rcar_canfd_handle_global_receive(gpriv, ch); 1199 } 1200 return IRQ_HANDLED; 1201 } 1202 1203 static void rcar_canfd_state_change(struct net_device *ndev, 1204 u16 txerr, u16 rxerr) 1205 { 1206 struct rcar_canfd_channel *priv = netdev_priv(ndev); 1207 struct net_device_stats *stats = &ndev->stats; 1208 enum can_state rx_state, tx_state, state = priv->can.state; 1209 struct can_frame *cf; 1210 struct sk_buff *skb; 1211 1212 /* Handle transition from error to normal states */ 1213 if (txerr < 96 && rxerr < 96) 1214 state = CAN_STATE_ERROR_ACTIVE; 1215 else if (txerr < 128 && rxerr < 128) 1216 state = CAN_STATE_ERROR_WARNING; 1217 1218 if (state != priv->can.state) { 1219 netdev_dbg(ndev, "state: new %d, old %d: txerr %u, rxerr %u\n", 1220 state, priv->can.state, txerr, rxerr); 1221 skb = alloc_can_err_skb(ndev, &cf); 1222 if (!skb) { 1223 stats->rx_dropped++; 1224 return; 1225 } 1226 tx_state = txerr >= rxerr ? state : 0; 1227 rx_state = txerr <= rxerr ? state : 0; 1228 1229 can_change_state(ndev, cf, tx_state, rx_state); 1230 netif_rx(skb); 1231 } 1232 } 1233 1234 static void rcar_canfd_handle_channel_tx(struct rcar_canfd_global *gpriv, u32 ch) 1235 { 1236 struct rcar_canfd_channel *priv = gpriv->ch[ch]; 1237 struct net_device *ndev = priv->ndev; 1238 u32 sts; 1239 1240 /* Handle Tx interrupts */ 1241 sts = rcar_canfd_read(priv->base, 1242 RCANFD_CFSTS(gpriv, ch, RCANFD_CFFIFO_IDX)); 1243 if (likely(sts & RCANFD_CFSTS_CFTXIF)) 1244 rcar_canfd_tx_done(ndev); 1245 } 1246 1247 static irqreturn_t rcar_canfd_channel_tx_interrupt(int irq, void *dev_id) 1248 { 1249 struct rcar_canfd_channel *priv = dev_id; 1250 1251 rcar_canfd_handle_channel_tx(priv->gpriv, priv->channel); 1252 1253 return IRQ_HANDLED; 1254 } 1255 1256 static void rcar_canfd_handle_channel_err(struct rcar_canfd_global *gpriv, u32 ch) 1257 { 1258 struct rcar_canfd_channel *priv = gpriv->ch[ch]; 1259 struct net_device *ndev = priv->ndev; 1260 u16 txerr, rxerr; 1261 u32 sts, cerfl; 1262 1263 /* Handle channel error interrupts */ 1264 cerfl = rcar_canfd_read(priv->base, RCANFD_CERFL(ch)); 1265 sts = rcar_canfd_read(priv->base, RCANFD_CSTS(ch)); 1266 txerr = RCANFD_CSTS_TECCNT(sts); 1267 rxerr = RCANFD_CSTS_RECCNT(sts); 1268 if (unlikely(RCANFD_CERFL_ERR(cerfl))) 1269 rcar_canfd_error(ndev, cerfl, txerr, rxerr); 1270 1271 /* Handle state change to lower states */ 1272 if (unlikely(priv->can.state != CAN_STATE_ERROR_ACTIVE && 1273 priv->can.state != CAN_STATE_BUS_OFF)) 1274 rcar_canfd_state_change(ndev, txerr, rxerr); 1275 } 1276 1277 static irqreturn_t rcar_canfd_channel_err_interrupt(int irq, void *dev_id) 1278 { 1279 struct rcar_canfd_channel *priv = dev_id; 1280 1281 rcar_canfd_handle_channel_err(priv->gpriv, priv->channel); 1282 1283 return IRQ_HANDLED; 1284 } 1285 1286 static irqreturn_t rcar_canfd_channel_interrupt(int irq, void *dev_id) 1287 { 1288 struct rcar_canfd_global *gpriv = dev_id; 1289 u32 ch; 1290 1291 /* Common FIFO is a per channel resource */ 1292 for_each_set_bit(ch, &gpriv->channels_mask, gpriv->max_channels) { 1293 rcar_canfd_handle_channel_err(gpriv, ch); 1294 rcar_canfd_handle_channel_tx(gpriv, ch); 1295 } 1296 1297 return IRQ_HANDLED; 1298 } 1299 1300 static void rcar_canfd_set_bittiming(struct net_device *dev) 1301 { 1302 struct rcar_canfd_channel *priv = netdev_priv(dev); 1303 struct rcar_canfd_global *gpriv = priv->gpriv; 1304 const struct can_bittiming *bt = &priv->can.bittiming; 1305 const struct can_bittiming *dbt = &priv->can.data_bittiming; 1306 u16 brp, sjw, tseg1, tseg2; 1307 u32 cfg; 1308 u32 ch = priv->channel; 1309 1310 /* Nominal bit timing settings */ 1311 brp = bt->brp - 1; 1312 sjw = bt->sjw - 1; 1313 tseg1 = bt->prop_seg + bt->phase_seg1 - 1; 1314 tseg2 = bt->phase_seg2 - 1; 1315 1316 if (priv->can.ctrlmode & CAN_CTRLMODE_FD) { 1317 /* CAN FD only mode */ 1318 cfg = (RCANFD_NCFG_NTSEG1(gpriv, tseg1) | RCANFD_NCFG_NBRP(brp) | 1319 RCANFD_NCFG_NSJW(gpriv, sjw) | RCANFD_NCFG_NTSEG2(gpriv, tseg2)); 1320 1321 rcar_canfd_write(priv->base, RCANFD_CCFG(ch), cfg); 1322 netdev_dbg(priv->ndev, "nrate: brp %u, sjw %u, tseg1 %u, tseg2 %u\n", 1323 brp, sjw, tseg1, tseg2); 1324 1325 /* Data bit timing settings */ 1326 brp = dbt->brp - 1; 1327 sjw = dbt->sjw - 1; 1328 tseg1 = dbt->prop_seg + dbt->phase_seg1 - 1; 1329 tseg2 = dbt->phase_seg2 - 1; 1330 1331 cfg = (RCANFD_DCFG_DTSEG1(gpriv, tseg1) | RCANFD_DCFG_DBRP(brp) | 1332 RCANFD_DCFG_DSJW(sjw) | RCANFD_DCFG_DTSEG2(gpriv, tseg2)); 1333 1334 if (is_v3u(gpriv)) 1335 rcar_canfd_write(priv->base, RCANFD_V3U_DCFG(ch), cfg); 1336 else 1337 rcar_canfd_write(priv->base, RCANFD_F_DCFG(ch), cfg); 1338 netdev_dbg(priv->ndev, "drate: brp %u, sjw %u, tseg1 %u, tseg2 %u\n", 1339 brp, sjw, tseg1, tseg2); 1340 } else { 1341 /* Classical CAN only mode */ 1342 if (is_v3u(gpriv)) { 1343 cfg = (RCANFD_NCFG_NTSEG1(gpriv, tseg1) | 1344 RCANFD_NCFG_NBRP(brp) | 1345 RCANFD_NCFG_NSJW(gpriv, sjw) | 1346 RCANFD_NCFG_NTSEG2(gpriv, tseg2)); 1347 } else { 1348 cfg = (RCANFD_CFG_TSEG1(tseg1) | 1349 RCANFD_CFG_BRP(brp) | 1350 RCANFD_CFG_SJW(sjw) | 1351 RCANFD_CFG_TSEG2(tseg2)); 1352 } 1353 1354 rcar_canfd_write(priv->base, RCANFD_CCFG(ch), cfg); 1355 netdev_dbg(priv->ndev, 1356 "rate: brp %u, sjw %u, tseg1 %u, tseg2 %u\n", 1357 brp, sjw, tseg1, tseg2); 1358 } 1359 } 1360 1361 static int rcar_canfd_start(struct net_device *ndev) 1362 { 1363 struct rcar_canfd_channel *priv = netdev_priv(ndev); 1364 struct rcar_canfd_global *gpriv = priv->gpriv; 1365 int err = -EOPNOTSUPP; 1366 u32 sts, ch = priv->channel; 1367 u32 ridx = ch + RCANFD_RFFIFO_IDX; 1368 1369 rcar_canfd_set_bittiming(ndev); 1370 1371 rcar_canfd_enable_channel_interrupts(priv); 1372 1373 /* Set channel to Operational mode */ 1374 rcar_canfd_update_bit(priv->base, RCANFD_CCTR(ch), 1375 RCANFD_CCTR_CHMDC_MASK, RCANFD_CCTR_CHDMC_COPM); 1376 1377 /* Verify channel mode change */ 1378 err = readl_poll_timeout((priv->base + RCANFD_CSTS(ch)), sts, 1379 (sts & RCANFD_CSTS_COMSTS), 2, 500000); 1380 if (err) { 1381 netdev_err(ndev, "channel %u communication state failed\n", ch); 1382 goto fail_mode_change; 1383 } 1384 1385 /* Enable Common & Rx FIFO */ 1386 rcar_canfd_set_bit(priv->base, RCANFD_CFCC(gpriv, ch, RCANFD_CFFIFO_IDX), 1387 RCANFD_CFCC_CFE); 1388 rcar_canfd_set_bit(priv->base, RCANFD_RFCC(gpriv, ridx), RCANFD_RFCC_RFE); 1389 1390 priv->can.state = CAN_STATE_ERROR_ACTIVE; 1391 return 0; 1392 1393 fail_mode_change: 1394 rcar_canfd_disable_channel_interrupts(priv); 1395 return err; 1396 } 1397 1398 static int rcar_canfd_open(struct net_device *ndev) 1399 { 1400 struct rcar_canfd_channel *priv = netdev_priv(ndev); 1401 struct rcar_canfd_global *gpriv = priv->gpriv; 1402 int err; 1403 1404 /* Peripheral clock is already enabled in probe */ 1405 err = clk_prepare_enable(gpriv->can_clk); 1406 if (err) { 1407 netdev_err(ndev, "failed to enable CAN clock, error %d\n", err); 1408 goto out_clock; 1409 } 1410 1411 err = open_candev(ndev); 1412 if (err) { 1413 netdev_err(ndev, "open_candev() failed, error %d\n", err); 1414 goto out_can_clock; 1415 } 1416 1417 napi_enable(&priv->napi); 1418 err = rcar_canfd_start(ndev); 1419 if (err) 1420 goto out_close; 1421 netif_start_queue(ndev); 1422 return 0; 1423 out_close: 1424 napi_disable(&priv->napi); 1425 close_candev(ndev); 1426 out_can_clock: 1427 clk_disable_unprepare(gpriv->can_clk); 1428 out_clock: 1429 return err; 1430 } 1431 1432 static void rcar_canfd_stop(struct net_device *ndev) 1433 { 1434 struct rcar_canfd_channel *priv = netdev_priv(ndev); 1435 struct rcar_canfd_global *gpriv = priv->gpriv; 1436 int err; 1437 u32 sts, ch = priv->channel; 1438 u32 ridx = ch + RCANFD_RFFIFO_IDX; 1439 1440 /* Transition to channel reset mode */ 1441 rcar_canfd_update_bit(priv->base, RCANFD_CCTR(ch), 1442 RCANFD_CCTR_CHMDC_MASK, RCANFD_CCTR_CHDMC_CRESET); 1443 1444 /* Check Channel reset mode */ 1445 err = readl_poll_timeout((priv->base + RCANFD_CSTS(ch)), sts, 1446 (sts & RCANFD_CSTS_CRSTSTS), 2, 500000); 1447 if (err) 1448 netdev_err(ndev, "channel %u reset failed\n", ch); 1449 1450 rcar_canfd_disable_channel_interrupts(priv); 1451 1452 /* Disable Common & Rx FIFO */ 1453 rcar_canfd_clear_bit(priv->base, RCANFD_CFCC(gpriv, ch, RCANFD_CFFIFO_IDX), 1454 RCANFD_CFCC_CFE); 1455 rcar_canfd_clear_bit(priv->base, RCANFD_RFCC(gpriv, ridx), RCANFD_RFCC_RFE); 1456 1457 /* Set the state as STOPPED */ 1458 priv->can.state = CAN_STATE_STOPPED; 1459 } 1460 1461 static int rcar_canfd_close(struct net_device *ndev) 1462 { 1463 struct rcar_canfd_channel *priv = netdev_priv(ndev); 1464 struct rcar_canfd_global *gpriv = priv->gpriv; 1465 1466 netif_stop_queue(ndev); 1467 rcar_canfd_stop(ndev); 1468 napi_disable(&priv->napi); 1469 clk_disable_unprepare(gpriv->can_clk); 1470 close_candev(ndev); 1471 return 0; 1472 } 1473 1474 static netdev_tx_t rcar_canfd_start_xmit(struct sk_buff *skb, 1475 struct net_device *ndev) 1476 { 1477 struct rcar_canfd_channel *priv = netdev_priv(ndev); 1478 struct rcar_canfd_global *gpriv = priv->gpriv; 1479 struct canfd_frame *cf = (struct canfd_frame *)skb->data; 1480 u32 sts = 0, id, dlc; 1481 unsigned long flags; 1482 u32 ch = priv->channel; 1483 1484 if (can_dropped_invalid_skb(ndev, skb)) 1485 return NETDEV_TX_OK; 1486 1487 if (cf->can_id & CAN_EFF_FLAG) { 1488 id = cf->can_id & CAN_EFF_MASK; 1489 id |= RCANFD_CFID_CFIDE; 1490 } else { 1491 id = cf->can_id & CAN_SFF_MASK; 1492 } 1493 1494 if (cf->can_id & CAN_RTR_FLAG) 1495 id |= RCANFD_CFID_CFRTR; 1496 1497 dlc = RCANFD_CFPTR_CFDLC(can_fd_len2dlc(cf->len)); 1498 1499 if ((priv->can.ctrlmode & CAN_CTRLMODE_FD) || is_v3u(gpriv)) { 1500 rcar_canfd_write(priv->base, 1501 RCANFD_F_CFID(gpriv, ch, RCANFD_CFFIFO_IDX), id); 1502 rcar_canfd_write(priv->base, 1503 RCANFD_F_CFPTR(gpriv, ch, RCANFD_CFFIFO_IDX), dlc); 1504 1505 if (can_is_canfd_skb(skb)) { 1506 /* CAN FD frame format */ 1507 sts |= RCANFD_CFFDCSTS_CFFDF; 1508 if (cf->flags & CANFD_BRS) 1509 sts |= RCANFD_CFFDCSTS_CFBRS; 1510 1511 if (priv->can.state == CAN_STATE_ERROR_PASSIVE) 1512 sts |= RCANFD_CFFDCSTS_CFESI; 1513 } 1514 1515 rcar_canfd_write(priv->base, 1516 RCANFD_F_CFFDCSTS(gpriv, ch, RCANFD_CFFIFO_IDX), sts); 1517 1518 rcar_canfd_put_data(priv, cf, 1519 RCANFD_F_CFDF(gpriv, ch, RCANFD_CFFIFO_IDX, 0)); 1520 } else { 1521 rcar_canfd_write(priv->base, 1522 RCANFD_C_CFID(ch, RCANFD_CFFIFO_IDX), id); 1523 rcar_canfd_write(priv->base, 1524 RCANFD_C_CFPTR(ch, RCANFD_CFFIFO_IDX), dlc); 1525 rcar_canfd_put_data(priv, cf, 1526 RCANFD_C_CFDF(ch, RCANFD_CFFIFO_IDX, 0)); 1527 } 1528 1529 can_put_echo_skb(skb, ndev, priv->tx_head % RCANFD_FIFO_DEPTH, 0); 1530 1531 spin_lock_irqsave(&priv->tx_lock, flags); 1532 priv->tx_head++; 1533 1534 /* Stop the queue if we've filled all FIFO entries */ 1535 if (priv->tx_head - priv->tx_tail >= RCANFD_FIFO_DEPTH) 1536 netif_stop_queue(ndev); 1537 1538 /* Start Tx: Write 0xff to CFPC to increment the CPU-side 1539 * pointer for the Common FIFO 1540 */ 1541 rcar_canfd_write(priv->base, 1542 RCANFD_CFPCTR(gpriv, ch, RCANFD_CFFIFO_IDX), 0xff); 1543 1544 spin_unlock_irqrestore(&priv->tx_lock, flags); 1545 return NETDEV_TX_OK; 1546 } 1547 1548 static void rcar_canfd_rx_pkt(struct rcar_canfd_channel *priv) 1549 { 1550 struct net_device_stats *stats = &priv->ndev->stats; 1551 struct rcar_canfd_global *gpriv = priv->gpriv; 1552 struct canfd_frame *cf; 1553 struct sk_buff *skb; 1554 u32 sts = 0, id, dlc; 1555 u32 ch = priv->channel; 1556 u32 ridx = ch + RCANFD_RFFIFO_IDX; 1557 1558 if ((priv->can.ctrlmode & CAN_CTRLMODE_FD) || is_v3u(gpriv)) { 1559 id = rcar_canfd_read(priv->base, RCANFD_F_RFID(gpriv, ridx)); 1560 dlc = rcar_canfd_read(priv->base, RCANFD_F_RFPTR(gpriv, ridx)); 1561 1562 sts = rcar_canfd_read(priv->base, RCANFD_F_RFFDSTS(gpriv, ridx)); 1563 1564 if ((priv->can.ctrlmode & CAN_CTRLMODE_FD) && 1565 sts & RCANFD_RFFDSTS_RFFDF) 1566 skb = alloc_canfd_skb(priv->ndev, &cf); 1567 else 1568 skb = alloc_can_skb(priv->ndev, 1569 (struct can_frame **)&cf); 1570 } else { 1571 id = rcar_canfd_read(priv->base, RCANFD_C_RFID(ridx)); 1572 dlc = rcar_canfd_read(priv->base, RCANFD_C_RFPTR(ridx)); 1573 skb = alloc_can_skb(priv->ndev, (struct can_frame **)&cf); 1574 } 1575 1576 if (!skb) { 1577 stats->rx_dropped++; 1578 return; 1579 } 1580 1581 if (id & RCANFD_RFID_RFIDE) 1582 cf->can_id = (id & CAN_EFF_MASK) | CAN_EFF_FLAG; 1583 else 1584 cf->can_id = id & CAN_SFF_MASK; 1585 1586 if (priv->can.ctrlmode & CAN_CTRLMODE_FD) { 1587 if (sts & RCANFD_RFFDSTS_RFFDF) 1588 cf->len = can_fd_dlc2len(RCANFD_RFPTR_RFDLC(dlc)); 1589 else 1590 cf->len = can_cc_dlc2len(RCANFD_RFPTR_RFDLC(dlc)); 1591 1592 if (sts & RCANFD_RFFDSTS_RFESI) { 1593 cf->flags |= CANFD_ESI; 1594 netdev_dbg(priv->ndev, "ESI Error\n"); 1595 } 1596 1597 if (!(sts & RCANFD_RFFDSTS_RFFDF) && (id & RCANFD_RFID_RFRTR)) { 1598 cf->can_id |= CAN_RTR_FLAG; 1599 } else { 1600 if (sts & RCANFD_RFFDSTS_RFBRS) 1601 cf->flags |= CANFD_BRS; 1602 1603 rcar_canfd_get_data(priv, cf, RCANFD_F_RFDF(gpriv, ridx, 0)); 1604 } 1605 } else { 1606 cf->len = can_cc_dlc2len(RCANFD_RFPTR_RFDLC(dlc)); 1607 if (id & RCANFD_RFID_RFRTR) 1608 cf->can_id |= CAN_RTR_FLAG; 1609 else if (is_v3u(gpriv)) 1610 rcar_canfd_get_data(priv, cf, RCANFD_F_RFDF(gpriv, ridx, 0)); 1611 else 1612 rcar_canfd_get_data(priv, cf, RCANFD_C_RFDF(ridx, 0)); 1613 } 1614 1615 /* Write 0xff to RFPC to increment the CPU-side 1616 * pointer of the Rx FIFO 1617 */ 1618 rcar_canfd_write(priv->base, RCANFD_RFPCTR(gpriv, ridx), 0xff); 1619 1620 if (!(cf->can_id & CAN_RTR_FLAG)) 1621 stats->rx_bytes += cf->len; 1622 stats->rx_packets++; 1623 netif_receive_skb(skb); 1624 } 1625 1626 static int rcar_canfd_rx_poll(struct napi_struct *napi, int quota) 1627 { 1628 struct rcar_canfd_channel *priv = 1629 container_of(napi, struct rcar_canfd_channel, napi); 1630 struct rcar_canfd_global *gpriv = priv->gpriv; 1631 int num_pkts; 1632 u32 sts; 1633 u32 ch = priv->channel; 1634 u32 ridx = ch + RCANFD_RFFIFO_IDX; 1635 1636 for (num_pkts = 0; num_pkts < quota; num_pkts++) { 1637 sts = rcar_canfd_read(priv->base, RCANFD_RFSTS(gpriv, ridx)); 1638 /* Check FIFO empty condition */ 1639 if (sts & RCANFD_RFSTS_RFEMP) 1640 break; 1641 1642 rcar_canfd_rx_pkt(priv); 1643 1644 /* Clear interrupt bit */ 1645 if (sts & RCANFD_RFSTS_RFIF) 1646 rcar_canfd_write(priv->base, RCANFD_RFSTS(gpriv, ridx), 1647 sts & ~RCANFD_RFSTS_RFIF); 1648 } 1649 1650 /* All packets processed */ 1651 if (num_pkts < quota) { 1652 if (napi_complete_done(napi, num_pkts)) { 1653 /* Enable Rx FIFO interrupts */ 1654 rcar_canfd_set_bit(priv->base, RCANFD_RFCC(gpriv, ridx), 1655 RCANFD_RFCC_RFIE); 1656 } 1657 } 1658 return num_pkts; 1659 } 1660 1661 static int rcar_canfd_do_set_mode(struct net_device *ndev, enum can_mode mode) 1662 { 1663 int err; 1664 1665 switch (mode) { 1666 case CAN_MODE_START: 1667 err = rcar_canfd_start(ndev); 1668 if (err) 1669 return err; 1670 netif_wake_queue(ndev); 1671 return 0; 1672 default: 1673 return -EOPNOTSUPP; 1674 } 1675 } 1676 1677 static int rcar_canfd_get_berr_counter(const struct net_device *dev, 1678 struct can_berr_counter *bec) 1679 { 1680 struct rcar_canfd_channel *priv = netdev_priv(dev); 1681 u32 val, ch = priv->channel; 1682 1683 /* Peripheral clock is already enabled in probe */ 1684 val = rcar_canfd_read(priv->base, RCANFD_CSTS(ch)); 1685 bec->txerr = RCANFD_CSTS_TECCNT(val); 1686 bec->rxerr = RCANFD_CSTS_RECCNT(val); 1687 return 0; 1688 } 1689 1690 static const struct net_device_ops rcar_canfd_netdev_ops = { 1691 .ndo_open = rcar_canfd_open, 1692 .ndo_stop = rcar_canfd_close, 1693 .ndo_start_xmit = rcar_canfd_start_xmit, 1694 .ndo_change_mtu = can_change_mtu, 1695 }; 1696 1697 static const struct ethtool_ops rcar_canfd_ethtool_ops = { 1698 .get_ts_info = ethtool_op_get_ts_info, 1699 }; 1700 1701 static int rcar_canfd_channel_probe(struct rcar_canfd_global *gpriv, u32 ch, 1702 u32 fcan_freq) 1703 { 1704 struct platform_device *pdev = gpriv->pdev; 1705 struct rcar_canfd_channel *priv; 1706 struct net_device *ndev; 1707 int err = -ENODEV; 1708 1709 ndev = alloc_candev(sizeof(*priv), RCANFD_FIFO_DEPTH); 1710 if (!ndev) { 1711 dev_err(&pdev->dev, "alloc_candev() failed\n"); 1712 return -ENOMEM; 1713 } 1714 priv = netdev_priv(ndev); 1715 1716 ndev->netdev_ops = &rcar_canfd_netdev_ops; 1717 ndev->ethtool_ops = &rcar_canfd_ethtool_ops; 1718 ndev->flags |= IFF_ECHO; 1719 priv->ndev = ndev; 1720 priv->base = gpriv->base; 1721 priv->channel = ch; 1722 priv->gpriv = gpriv; 1723 priv->can.clock.freq = fcan_freq; 1724 dev_info(&pdev->dev, "can_clk rate is %u\n", priv->can.clock.freq); 1725 1726 if (gpriv->chip_id == RENESAS_RZG2L) { 1727 char *irq_name; 1728 int err_irq; 1729 int tx_irq; 1730 1731 err_irq = platform_get_irq_byname(pdev, ch == 0 ? "ch0_err" : "ch1_err"); 1732 if (err_irq < 0) { 1733 err = err_irq; 1734 goto fail; 1735 } 1736 1737 tx_irq = platform_get_irq_byname(pdev, ch == 0 ? "ch0_trx" : "ch1_trx"); 1738 if (tx_irq < 0) { 1739 err = tx_irq; 1740 goto fail; 1741 } 1742 1743 irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, 1744 "canfd.ch%d_err", ch); 1745 if (!irq_name) { 1746 err = -ENOMEM; 1747 goto fail; 1748 } 1749 err = devm_request_irq(&pdev->dev, err_irq, 1750 rcar_canfd_channel_err_interrupt, 0, 1751 irq_name, priv); 1752 if (err) { 1753 dev_err(&pdev->dev, "devm_request_irq CH Err(%d) failed, error %d\n", 1754 err_irq, err); 1755 goto fail; 1756 } 1757 irq_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, 1758 "canfd.ch%d_trx", ch); 1759 if (!irq_name) { 1760 err = -ENOMEM; 1761 goto fail; 1762 } 1763 err = devm_request_irq(&pdev->dev, tx_irq, 1764 rcar_canfd_channel_tx_interrupt, 0, 1765 irq_name, priv); 1766 if (err) { 1767 dev_err(&pdev->dev, "devm_request_irq Tx (%d) failed, error %d\n", 1768 tx_irq, err); 1769 goto fail; 1770 } 1771 } 1772 1773 if (gpriv->fdmode) { 1774 priv->can.bittiming_const = &rcar_canfd_nom_bittiming_const; 1775 priv->can.data_bittiming_const = 1776 &rcar_canfd_data_bittiming_const; 1777 1778 /* Controller starts in CAN FD only mode */ 1779 err = can_set_static_ctrlmode(ndev, CAN_CTRLMODE_FD); 1780 if (err) 1781 goto fail; 1782 priv->can.ctrlmode_supported = CAN_CTRLMODE_BERR_REPORTING; 1783 } else { 1784 /* Controller starts in Classical CAN only mode */ 1785 priv->can.bittiming_const = &rcar_canfd_bittiming_const; 1786 priv->can.ctrlmode_supported = CAN_CTRLMODE_BERR_REPORTING; 1787 } 1788 1789 priv->can.do_set_mode = rcar_canfd_do_set_mode; 1790 priv->can.do_get_berr_counter = rcar_canfd_get_berr_counter; 1791 SET_NETDEV_DEV(ndev, &pdev->dev); 1792 1793 netif_napi_add_weight(ndev, &priv->napi, rcar_canfd_rx_poll, 1794 RCANFD_NAPI_WEIGHT); 1795 spin_lock_init(&priv->tx_lock); 1796 gpriv->ch[priv->channel] = priv; 1797 err = register_candev(ndev); 1798 if (err) { 1799 dev_err(&pdev->dev, 1800 "register_candev() failed, error %d\n", err); 1801 goto fail_candev; 1802 } 1803 dev_info(&pdev->dev, "device registered (channel %u)\n", priv->channel); 1804 return 0; 1805 1806 fail_candev: 1807 netif_napi_del(&priv->napi); 1808 fail: 1809 free_candev(ndev); 1810 return err; 1811 } 1812 1813 static void rcar_canfd_channel_remove(struct rcar_canfd_global *gpriv, u32 ch) 1814 { 1815 struct rcar_canfd_channel *priv = gpriv->ch[ch]; 1816 1817 if (priv) { 1818 unregister_candev(priv->ndev); 1819 netif_napi_del(&priv->napi); 1820 free_candev(priv->ndev); 1821 } 1822 } 1823 1824 static int rcar_canfd_probe(struct platform_device *pdev) 1825 { 1826 void __iomem *addr; 1827 u32 sts, ch, fcan_freq; 1828 struct rcar_canfd_global *gpriv; 1829 struct device_node *of_child; 1830 unsigned long channels_mask = 0; 1831 int err, ch_irq, g_irq; 1832 int g_err_irq, g_recc_irq; 1833 bool fdmode = true; /* CAN FD only mode - default */ 1834 enum rcanfd_chip_id chip_id; 1835 int max_channels; 1836 char name[9] = "channelX"; 1837 int i; 1838 1839 chip_id = (uintptr_t)of_device_get_match_data(&pdev->dev); 1840 max_channels = chip_id == RENESAS_R8A779A0 ? 8 : 2; 1841 1842 if (of_property_read_bool(pdev->dev.of_node, "renesas,no-can-fd")) 1843 fdmode = false; /* Classical CAN only mode */ 1844 1845 for (i = 0; i < max_channels; ++i) { 1846 name[7] = '0' + i; 1847 of_child = of_get_child_by_name(pdev->dev.of_node, name); 1848 if (of_child && of_device_is_available(of_child)) 1849 channels_mask |= BIT(i); 1850 of_node_put(of_child); 1851 } 1852 1853 if (chip_id != RENESAS_RZG2L) { 1854 ch_irq = platform_get_irq_byname_optional(pdev, "ch_int"); 1855 if (ch_irq < 0) { 1856 /* For backward compatibility get irq by index */ 1857 ch_irq = platform_get_irq(pdev, 0); 1858 if (ch_irq < 0) 1859 return ch_irq; 1860 } 1861 1862 g_irq = platform_get_irq_byname_optional(pdev, "g_int"); 1863 if (g_irq < 0) { 1864 /* For backward compatibility get irq by index */ 1865 g_irq = platform_get_irq(pdev, 1); 1866 if (g_irq < 0) 1867 return g_irq; 1868 } 1869 } else { 1870 g_err_irq = platform_get_irq_byname(pdev, "g_err"); 1871 if (g_err_irq < 0) 1872 return g_err_irq; 1873 1874 g_recc_irq = platform_get_irq_byname(pdev, "g_recc"); 1875 if (g_recc_irq < 0) 1876 return g_recc_irq; 1877 } 1878 1879 /* Global controller context */ 1880 gpriv = devm_kzalloc(&pdev->dev, sizeof(*gpriv), GFP_KERNEL); 1881 if (!gpriv) 1882 return -ENOMEM; 1883 1884 gpriv->pdev = pdev; 1885 gpriv->channels_mask = channels_mask; 1886 gpriv->fdmode = fdmode; 1887 gpriv->chip_id = chip_id; 1888 gpriv->max_channels = max_channels; 1889 1890 if (gpriv->chip_id == RENESAS_RZG2L) { 1891 gpriv->rstc1 = devm_reset_control_get_exclusive(&pdev->dev, "rstp_n"); 1892 if (IS_ERR(gpriv->rstc1)) 1893 return dev_err_probe(&pdev->dev, PTR_ERR(gpriv->rstc1), 1894 "failed to get rstp_n\n"); 1895 1896 gpriv->rstc2 = devm_reset_control_get_exclusive(&pdev->dev, "rstc_n"); 1897 if (IS_ERR(gpriv->rstc2)) 1898 return dev_err_probe(&pdev->dev, PTR_ERR(gpriv->rstc2), 1899 "failed to get rstc_n\n"); 1900 } 1901 1902 /* Peripheral clock */ 1903 gpriv->clkp = devm_clk_get(&pdev->dev, "fck"); 1904 if (IS_ERR(gpriv->clkp)) 1905 return dev_err_probe(&pdev->dev, PTR_ERR(gpriv->clkp), 1906 "cannot get peripheral clock\n"); 1907 1908 /* fCAN clock: Pick External clock. If not available fallback to 1909 * CANFD clock 1910 */ 1911 gpriv->can_clk = devm_clk_get(&pdev->dev, "can_clk"); 1912 if (IS_ERR(gpriv->can_clk) || (clk_get_rate(gpriv->can_clk) == 0)) { 1913 gpriv->can_clk = devm_clk_get(&pdev->dev, "canfd"); 1914 if (IS_ERR(gpriv->can_clk)) 1915 return dev_err_probe(&pdev->dev, PTR_ERR(gpriv->can_clk), 1916 "cannot get canfd clock\n"); 1917 1918 gpriv->fcan = RCANFD_CANFDCLK; 1919 1920 } else { 1921 gpriv->fcan = RCANFD_EXTCLK; 1922 } 1923 fcan_freq = clk_get_rate(gpriv->can_clk); 1924 1925 if (gpriv->fcan == RCANFD_CANFDCLK && gpriv->chip_id != RENESAS_RZG2L) 1926 /* CANFD clock is further divided by (1/2) within the IP */ 1927 fcan_freq /= 2; 1928 1929 addr = devm_platform_ioremap_resource(pdev, 0); 1930 if (IS_ERR(addr)) { 1931 err = PTR_ERR(addr); 1932 goto fail_dev; 1933 } 1934 gpriv->base = addr; 1935 1936 /* Request IRQ that's common for both channels */ 1937 if (gpriv->chip_id != RENESAS_RZG2L) { 1938 err = devm_request_irq(&pdev->dev, ch_irq, 1939 rcar_canfd_channel_interrupt, 0, 1940 "canfd.ch_int", gpriv); 1941 if (err) { 1942 dev_err(&pdev->dev, "devm_request_irq(%d) failed, error %d\n", 1943 ch_irq, err); 1944 goto fail_dev; 1945 } 1946 1947 err = devm_request_irq(&pdev->dev, g_irq, 1948 rcar_canfd_global_interrupt, 0, 1949 "canfd.g_int", gpriv); 1950 if (err) { 1951 dev_err(&pdev->dev, "devm_request_irq(%d) failed, error %d\n", 1952 g_irq, err); 1953 goto fail_dev; 1954 } 1955 } else { 1956 err = devm_request_irq(&pdev->dev, g_recc_irq, 1957 rcar_canfd_global_receive_fifo_interrupt, 0, 1958 "canfd.g_recc", gpriv); 1959 1960 if (err) { 1961 dev_err(&pdev->dev, "devm_request_irq(%d) failed, error %d\n", 1962 g_recc_irq, err); 1963 goto fail_dev; 1964 } 1965 1966 err = devm_request_irq(&pdev->dev, g_err_irq, 1967 rcar_canfd_global_err_interrupt, 0, 1968 "canfd.g_err", gpriv); 1969 if (err) { 1970 dev_err(&pdev->dev, "devm_request_irq(%d) failed, error %d\n", 1971 g_err_irq, err); 1972 goto fail_dev; 1973 } 1974 } 1975 1976 err = reset_control_reset(gpriv->rstc1); 1977 if (err) 1978 goto fail_dev; 1979 err = reset_control_reset(gpriv->rstc2); 1980 if (err) { 1981 reset_control_assert(gpriv->rstc1); 1982 goto fail_dev; 1983 } 1984 1985 /* Enable peripheral clock for register access */ 1986 err = clk_prepare_enable(gpriv->clkp); 1987 if (err) { 1988 dev_err(&pdev->dev, 1989 "failed to enable peripheral clock, error %d\n", err); 1990 goto fail_reset; 1991 } 1992 1993 err = rcar_canfd_reset_controller(gpriv); 1994 if (err) { 1995 dev_err(&pdev->dev, "reset controller failed\n"); 1996 goto fail_clk; 1997 } 1998 1999 /* Controller in Global reset & Channel reset mode */ 2000 rcar_canfd_configure_controller(gpriv); 2001 2002 /* Configure per channel attributes */ 2003 for_each_set_bit(ch, &gpriv->channels_mask, max_channels) { 2004 /* Configure Channel's Rx fifo */ 2005 rcar_canfd_configure_rx(gpriv, ch); 2006 2007 /* Configure Channel's Tx (Common) fifo */ 2008 rcar_canfd_configure_tx(gpriv, ch); 2009 2010 /* Configure receive rules */ 2011 rcar_canfd_configure_afl_rules(gpriv, ch); 2012 } 2013 2014 /* Configure common interrupts */ 2015 rcar_canfd_enable_global_interrupts(gpriv); 2016 2017 /* Start Global operation mode */ 2018 rcar_canfd_update_bit(gpriv->base, RCANFD_GCTR, RCANFD_GCTR_GMDC_MASK, 2019 RCANFD_GCTR_GMDC_GOPM); 2020 2021 /* Verify mode change */ 2022 err = readl_poll_timeout((gpriv->base + RCANFD_GSTS), sts, 2023 !(sts & RCANFD_GSTS_GNOPM), 2, 500000); 2024 if (err) { 2025 dev_err(&pdev->dev, "global operational mode failed\n"); 2026 goto fail_mode; 2027 } 2028 2029 for_each_set_bit(ch, &gpriv->channels_mask, max_channels) { 2030 err = rcar_canfd_channel_probe(gpriv, ch, fcan_freq); 2031 if (err) 2032 goto fail_channel; 2033 } 2034 2035 platform_set_drvdata(pdev, gpriv); 2036 dev_info(&pdev->dev, "global operational state (clk %d, fdmode %d)\n", 2037 gpriv->fcan, gpriv->fdmode); 2038 return 0; 2039 2040 fail_channel: 2041 for_each_set_bit(ch, &gpriv->channels_mask, max_channels) 2042 rcar_canfd_channel_remove(gpriv, ch); 2043 fail_mode: 2044 rcar_canfd_disable_global_interrupts(gpriv); 2045 fail_clk: 2046 clk_disable_unprepare(gpriv->clkp); 2047 fail_reset: 2048 reset_control_assert(gpriv->rstc1); 2049 reset_control_assert(gpriv->rstc2); 2050 fail_dev: 2051 return err; 2052 } 2053 2054 static int rcar_canfd_remove(struct platform_device *pdev) 2055 { 2056 struct rcar_canfd_global *gpriv = platform_get_drvdata(pdev); 2057 u32 ch; 2058 2059 rcar_canfd_reset_controller(gpriv); 2060 rcar_canfd_disable_global_interrupts(gpriv); 2061 2062 for_each_set_bit(ch, &gpriv->channels_mask, gpriv->max_channels) { 2063 rcar_canfd_disable_channel_interrupts(gpriv->ch[ch]); 2064 rcar_canfd_channel_remove(gpriv, ch); 2065 } 2066 2067 /* Enter global sleep mode */ 2068 rcar_canfd_set_bit(gpriv->base, RCANFD_GCTR, RCANFD_GCTR_GSLPR); 2069 clk_disable_unprepare(gpriv->clkp); 2070 reset_control_assert(gpriv->rstc1); 2071 reset_control_assert(gpriv->rstc2); 2072 2073 return 0; 2074 } 2075 2076 static int __maybe_unused rcar_canfd_suspend(struct device *dev) 2077 { 2078 return 0; 2079 } 2080 2081 static int __maybe_unused rcar_canfd_resume(struct device *dev) 2082 { 2083 return 0; 2084 } 2085 2086 static SIMPLE_DEV_PM_OPS(rcar_canfd_pm_ops, rcar_canfd_suspend, 2087 rcar_canfd_resume); 2088 2089 static const __maybe_unused struct of_device_id rcar_canfd_of_table[] = { 2090 { .compatible = "renesas,rcar-gen3-canfd", .data = (void *)RENESAS_RCAR_GEN3 }, 2091 { .compatible = "renesas,rzg2l-canfd", .data = (void *)RENESAS_RZG2L }, 2092 { .compatible = "renesas,r8a779a0-canfd", .data = (void *)RENESAS_R8A779A0 }, 2093 { } 2094 }; 2095 2096 MODULE_DEVICE_TABLE(of, rcar_canfd_of_table); 2097 2098 static struct platform_driver rcar_canfd_driver = { 2099 .driver = { 2100 .name = RCANFD_DRV_NAME, 2101 .of_match_table = of_match_ptr(rcar_canfd_of_table), 2102 .pm = &rcar_canfd_pm_ops, 2103 }, 2104 .probe = rcar_canfd_probe, 2105 .remove = rcar_canfd_remove, 2106 }; 2107 2108 module_platform_driver(rcar_canfd_driver); 2109 2110 MODULE_AUTHOR("Ramesh Shanmugasundaram <ramesh.shanmugasundaram@bp.renesas.com>"); 2111 MODULE_LICENSE("GPL"); 2112 MODULE_DESCRIPTION("CAN FD driver for Renesas R-Car SoC"); 2113 MODULE_ALIAS("platform:" RCANFD_DRV_NAME); 2114