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