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