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