Linux Audio

Check our new training course

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