• RK3568 CAN驱动更新说明


    RK3568 CAN问题:同时收发数据一段时间(几秒钟)can出现错误收发功能异常,必须重新down、up恢复正常
    内核更新rockchip_canfd.c、iopoll.h,配置Networking support  --->CAN bus subsystem support  --->CAN Device Drivers  --->Rockchip CANFD controller, make ARCH=arm64 menuconfig;内核配置后将.config覆盖到arch/arm64/configs/rockchip_linux_defconfig
    修改设备树can节点.arch/arm64/boot/dts/rockchip/rk3568-evb1-ddr4-v10-linux.dts
    compatible = "rockchip,rk3568-can-2.0",assigned-clock-rates = <300000000>;//如果CAN的比特率1M建议修改CAN时钟到300M,信号更稳定
    ./build.sh kernerl
    注意:测试时两个节点帧ID不能相同,否则会发生数据错误,且负载率宜控制在30%以内
    【负载率计算】
    假设报文以定周期发送,总线上的报文只有数据帧一种类型,不存在远程帧、错误帧和过载帧这三种类型。因此每帧报文包括:帧起始(1bit)、仲裁域(29bit)、控制域(6bit)、数据域(8×8bit)、循环冗余码域(15bit)、应答域(2bit)和帧结尾(7bit),共124位。
    假设每秒发送的数据帧数为185帧
    对于高速CAN总线(500kbps)的负载率 = frac{185*124}\{500*1000}*100\% = 4.58\%
    对于低速CAN总线(250kbps)的负载率 = frac{185*124}\{250*1000}*100\% = 9.16\%
    【rockchip_canfd.c】
    1. // SPDX-License-Identifier: GPL-2.0
    2. /*
    3.  * Copyright (c) 2020 Rockchip Electronics Co. Ltd.
    4.  * Rockchip CANFD driver
    5.  */
    6. #include <linux/delay.h>
    7. #include <linux/iopoll.h>
    8. #include <linux/pinctrl/consumer.h>
    9. #include <linux/clk.h>
    10. #include <linux/errno.h>
    11. #include <linux/init.h>
    12. #include <linux/interrupt.h>
    13. #include <linux/io.h>
    14. #include <linux/kernel.h>
    15. #include <linux/module.h>
    16. #include <linux/netdevice.h>
    17. #include <linux/of.h>
    18. #include <linux/of_device.h>
    19. #include <linux/platform_device.h>
    20. #include <linux/skbuff.h>
    21. #include <linux/spinlock.h>
    22. #include <linux/string.h>
    23. #include <linux/types.h>
    24. #include <linux/can/dev.h>
    25. #include <linux/can/error.h>
    26. #include <linux/can/led.h>
    27. #include <linux/reset.h>
    28. #include <linux/pm_runtime.h>
    29. /* registers definition */
    30. enum rockchip_canfd_reg {
    31.         CAN_MODE = 0x00,
    32.         CAN_CMD = 0x04,
    33.         CAN_STATE = 0x08,
    34.         CAN_INT = 0x0c,
    35.         CAN_INT_MASK = 0x10,
    36.         CAN_LOSTARB_CODE = 0x28,
    37.         CAN_ERR_CODE = 0x2c,
    38.         CAN_RX_ERR_CNT = 0x34,
    39.         CAN_TX_ERR_CNT = 0x38,
    40.         CAN_IDCODE = 0x3c,
    41.         CAN_IDMASK = 0x40,
    42.         CAN_TX_CHECK_FIC = 0x50,
    43.         CAN_NBTP = 0x100,
    44.         CAN_DBTP = 0x104,
    45.         CAN_TDCR = 0x108,
    46.         CAN_TSCC = 0x10c,
    47.         CAN_TSCV = 0x110,
    48.         CAN_TXEFC = 0x114,
    49.         CAN_RXFC = 0x118,
    50.         CAN_AFC = 0x11c,
    51.         CAN_IDCODE0 = 0x120,
    52.         CAN_IDMASK0 = 0x124,
    53.         CAN_IDCODE1 = 0x128,
    54.         CAN_IDMASK1 = 0x12c,
    55.         CAN_IDCODE2 = 0x130,
    56.         CAN_IDMASK2 = 0x134,
    57.         CAN_IDCODE3 = 0x138,
    58.         CAN_IDMASK3 = 0x13c,
    59.         CAN_IDCODE4 = 0x140,
    60.         CAN_IDMASK4 = 0x144,
    61.         CAN_TXFIC = 0x200,
    62.         CAN_TXID = 0x204,
    63.         CAN_TXDAT0 = 0x208,
    64.         CAN_TXDAT1 = 0x20c,
    65.         CAN_TXDAT2 = 0x210,
    66.         CAN_TXDAT3 = 0x214,
    67.         CAN_TXDAT4 = 0x218,
    68.         CAN_TXDAT5 = 0x21c,
    69.         CAN_TXDAT6 = 0x220,
    70.         CAN_TXDAT7 = 0x224,
    71.         CAN_TXDAT8 = 0x228,
    72.         CAN_TXDAT9 = 0x22c,
    73.         CAN_TXDAT10 = 0x230,
    74.         CAN_TXDAT11 = 0x234,
    75.         CAN_TXDAT12 = 0x238,
    76.         CAN_TXDAT13 = 0x23c,
    77.         CAN_TXDAT14 = 0x240,
    78.         CAN_TXDAT15 = 0x244,
    79.         CAN_RXFIC = 0x300,
    80.         CAN_RXID = 0x304,
    81.         CAN_RXTS = 0x308,
    82.         CAN_RXDAT0 = 0x30c,
    83.         CAN_RXDAT1 = 0x310,
    84.         CAN_RXDAT2 = 0x314,
    85.         CAN_RXDAT3 = 0x318,
    86.         CAN_RXDAT4 = 0x31c,
    87.         CAN_RXDAT5 = 0x320,
    88.         CAN_RXDAT6 = 0x324,
    89.         CAN_RXDAT7 = 0x328,
    90.         CAN_RXDAT8 = 0x32c,
    91.         CAN_RXDAT9 = 0x330,
    92.         CAN_RXDAT10 = 0x334,
    93.         CAN_RXDAT11 = 0x338,
    94.         CAN_RXDAT12 = 0x33c,
    95.         CAN_RXDAT13 = 0x340,
    96.         CAN_RXDAT14 = 0x344,
    97.         CAN_RXDAT15 = 0x348,
    98.         CAN_RXFRD = 0x400,
    99.         CAN_TXEFRD = 0x500,
    100. };
    101. enum {
    102.         ROCKCHIP_CANFD_MODE = 0,
    103.         ROCKCHIP_CAN_MODE,
    104.         ROCKCHIP_RK3568_CAN_MODE,
    105. };
    106. #define DATE_LENGTH_12_BYTE     (0x9)
    107. #define DATE_LENGTH_16_BYTE     (0xa)
    108. #define DATE_LENGTH_20_BYTE     (0xb)
    109. #define DATE_LENGTH_24_BYTE     (0xc)
    110. #define DATE_LENGTH_32_BYTE     (0xd)
    111. #define DATE_LENGTH_48_BYTE     (0xe)
    112. #define DATE_LENGTH_64_BYTE     (0xf)
    113. #define CAN_TX0_REQ             BIT(0)
    114. #define CAN_TX1_REQ             BIT(1)
    115. #define CAN_TX_REQ_FULL         ((CAN_TX0_REQ) | (CAN_TX1_REQ))
    116. #define MODE_FDOE               BIT(15)
    117. #define MODE_BRSD               BIT(13)
    118. #define MODE_SPACE_RX           BIT(12)
    119. #define MODE_AUTO_RETX          BIT(10)
    120. #define MODE_RXSORT             BIT(7)
    121. #define MODE_TXORDER            BIT(6)
    122. #define MODE_RXSTX              BIT(5)
    123. #define MODE_LBACK              BIT(4)
    124. #define MODE_SILENT             BIT(3)
    125. #define MODE_SELF_TEST          BIT(2)
    126. #define MODE_SLEEP              BIT(1)
    127. #define RESET_MODE              0
    128. #define WORK_MODE               BIT(0)
    129. #define RX_FINISH_INT           BIT(0)
    130. #define TX_FINISH_INT           BIT(1)
    131. #define ERR_WARN_INT            BIT(2)
    132. #define RX_BUF_OV_INT           BIT(3)
    133. #define PASSIVE_ERR_INT         BIT(4)
    134. #define TX_LOSTARB_INT          BIT(5)
    135. #define BUS_ERR_INT             BIT(6)
    136. #define RX_FIFO_FULL_INT        BIT(7)
    137. #define RX_FIFO_OV_INT          BIT(8)
    138. #define BUS_OFF_INT             BIT(9)
    139. #define BUS_OFF_RECOVERY_INT    BIT(10)
    140. #define TSC_OV_INT              BIT(11)
    141. #define TXE_FIFO_OV_INT         BIT(12)
    142. #define TXE_FIFO_FULL_INT       BIT(13)
    143. #define WAKEUP_INT              BIT(14)
    144. #define ERR_TYPE_MASK           GENMASK(28, 26)
    145. #define ERR_TYPE_SHIFT          26
    146. #define BIT_ERR                 0
    147. #define STUFF_ERR               1
    148. #define FORM_ERR                2
    149. #define ACK_ERR                 3
    150. #define CRC_ERR                 4
    151. #define ERR_DIR_RX              BIT(25)
    152. #define ERR_LOC_MASK            GENMASK(15, 0)
    153. /* Nominal Bit Timing & Prescaler Register (NBTP) */
    154. #define NBTP_MODE_3_SAMPLES     BIT(31)
    155. #define NBTP_NSJW_SHIFT         24
    156. #define NBTP_NSJW_MASK          (0x7f << NBTP_NSJW_SHIFT)
    157. #define NBTP_NBRP_SHIFT         16
    158. #define NBTP_NBRP_MASK          (0xff << NBTP_NBRP_SHIFT)
    159. #define NBTP_NTSEG2_SHIFT       8
    160. #define NBTP_NTSEG2_MASK        (0x7f << NBTP_NTSEG2_SHIFT)
    161. #define NBTP_NTSEG1_SHIFT       0
    162. #define NBTP_NTSEG1_MASK        (0x7f << NBTP_NTSEG1_SHIFT)
    163. /* Data Bit Timing & Prescaler Register (DBTP) */
    164. #define DBTP_MODE_3_SAMPLES     BIT(21)
    165. #define DBTP_DSJW_SHIFT         17
    166. #define DBTP_DSJW_MASK          (0xf << DBTP_DSJW_SHIFT)
    167. #define DBTP_DBRP_SHIFT         9
    168. #define DBTP_DBRP_MASK          (0xff << DBTP_DBRP_SHIFT)
    169. #define DBTP_DTSEG2_SHIFT       5
    170. #define DBTP_DTSEG2_MASK        (0xf << DBTP_DTSEG2_SHIFT)
    171. #define DBTP_DTSEG1_SHIFT       0
    172. #define DBTP_DTSEG1_MASK        (0x1f << DBTP_DTSEG1_SHIFT)
    173. /* Transmitter Delay Compensation Register (TDCR) */
    174. #define TDCR_TDCO_SHIFT         1
    175. #define TDCR_TDCO_MASK          (0x3f << TDCR_TDCO_SHIFT)
    176. #define TDCR_TDC_ENABLE         BIT(0)
    177. #define TX_FD_ENABLE            BIT(5)
    178. #define TX_FD_BRS_ENABLE        BIT(4)
    179. #define FIFO_ENABLE             BIT(0)
    180. #define RX_FIFO_CNT0_SHIFT      4
    181. #define RX_FIFO_CNT0_MASK       (0x7 << RX_FIFO_CNT0_SHIFT)
    182. #define RX_FIFO_CNT1_SHIFT      5
    183. #define RX_FIFO_CNT1_MASK       (0x7 << RX_FIFO_CNT1_SHIFT)
    184. #define FORMAT_SHIFT            7
    185. #define FORMAT_MASK             (0x1 << FORMAT_SHIFT)
    186. #define RTR_SHIFT               6
    187. #define RTR_MASK                (0x1 << RTR_SHIFT)
    188. #define FDF_SHIFT               5
    189. #define FDF_MASK                (0x1 << FDF_SHIFT)
    190. #define BRS_SHIFT               4
    191. #define BRS_MASK                (0x1 << BRS_SHIFT)
    192. #define DLC_SHIFT               0
    193. #define DLC_MASK                (0xF << DLC_SHIFT)
    194. #define CAN_RF_SIZE             0x48
    195. #define CAN_TEF_SIZE            0x8
    196. #define CAN_TXEFRD_OFFSET(n)    (CAN_TXEFRD + CAN_TEF_SIZE * (n))
    197. #define CAN_RXFRD_OFFSET(n)     (CAN_RXFRD + CAN_RF_SIZE * (n))
    198. #define CAN_RX_FILTER_MASK      0x1fffffff
    199. #define DRV_NAME        "rockchip_canfd"
    200. /* rockchip_canfd private data structure */
    201. struct rockchip_canfd {
    202.         struct can_priv can;
    203.         struct device *dev;
    204.         struct clk_bulk_data *clks;
    205.         int num_clks;
    206.         struct reset_control *reset;
    207.         void __iomem *base;
    208.         u32 irqstatus;
    209.         unsigned long mode;
    210.         int rx_fifo_shift;
    211.         u32 rx_fifo_mask;
    212.         bool txtorx;
    213.         u32 tx_invalid[4];
    214.         struct delayed_work tx_err_work;
    215. };
    216. static inline u32 rockchip_canfd_read(const struct rockchip_canfd *priv,
    217.                                       enum rockchip_canfd_reg reg)
    218. {
    219.         return readl(priv->base + reg);
    220. }
    221. static inline void rockchip_canfd_write(const struct rockchip_canfd *priv,
    222.                                         enum rockchip_canfd_reg reg, u32 val)
    223. {
    224.         writel(val, priv->base + reg);
    225. }
    226. static const struct can_bittiming_const rockchip_canfd_bittiming_const = {
    227.         .name = DRV_NAME,
    228.         .tseg1_min = 1,
    229.         .tseg1_max = 128,
    230.         .tseg2_min = 1,
    231.         .tseg2_max = 128,
    232.         .sjw_max = 128,
    233.         .brp_min = 1,
    234.         .brp_max = 256,
    235.         .brp_inc = 2,
    236. };
    237. static const struct can_bittiming_const rockchip_canfd_data_bittiming_const = {
    238.         .name = DRV_NAME,
    239.         .tseg1_min = 1,
    240.         .tseg1_max = 32,
    241.         .tseg2_min = 1,
    242.         .tseg2_max = 16,
    243.         .sjw_max = 16,
    244.         .brp_min = 1,
    245.         .brp_max = 256,
    246.         .brp_inc = 2,
    247. };
    248. static int set_reset_mode(struct net_device *ndev)
    249. {
    250.         struct rockchip_canfd *rcan = netdev_priv(ndev);
    251.         reset_control_assert(rcan->reset);
    252.         udelay(2);
    253.         reset_control_deassert(rcan->reset);
    254.         rockchip_canfd_write(rcan, CAN_MODE, 0);
    255.         netdev_dbg(ndev, "%s MODE=0x%08x\n", __func__,
    256.                    rockchip_canfd_read(rcan, CAN_MODE));
    257.         return 0;
    258. }
    259. static int set_normal_mode(struct net_device *ndev)
    260. {
    261.         struct rockchip_canfd *rcan = netdev_priv(ndev);
    262.         u32 val;
    263.         val = rockchip_canfd_read(rcan, CAN_MODE);
    264.         val |= WORK_MODE;
    265.         rockchip_canfd_write(rcan, CAN_MODE, val);
    266.         netdev_dbg(ndev, "%s MODE=0x%08x\n", __func__,
    267.                    rockchip_canfd_read(rcan, CAN_MODE));
    268.         return 0;
    269. }
    270. /* bittiming is called in reset_mode only */
    271. static int rockchip_canfd_set_bittiming(struct net_device *ndev)
    272. {
    273.         struct rockchip_canfd *rcan = netdev_priv(ndev);
    274.         const struct can_bittiming *bt = &rcan->can.bittiming;
    275.         const struct can_bittiming *dbt = &rcan->can.data_bittiming;
    276.         u16 brp, sjw, tseg1, tseg2;
    277.         u32 reg_btp;
    278.         brp = (bt->brp >> 1) - 1;
    279.         sjw = bt->sjw - 1;
    280.         tseg1 = bt->prop_seg + bt->phase_seg1 - 1;
    281.         tseg2 = bt->phase_seg2 - 1;
    282.         reg_btp = (brp << NBTP_NBRP_SHIFT) | (sjw << NBTP_NSJW_SHIFT) |
    283.                   (tseg1 << NBTP_NTSEG1_SHIFT) |
    284.                   (tseg2 << NBTP_NTSEG2_SHIFT);
    285.         if (rcan->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
    286.                 reg_btp |= NBTP_MODE_3_SAMPLES;
    287.         rockchip_canfd_write(rcan, CAN_NBTP, reg_btp);
    288.         if (rcan->can.ctrlmode & CAN_CTRLMODE_FD) {
    289.                 reg_btp = 0;
    290.                 brp = (dbt->brp >> 1) - 1;
    291.                 sjw = dbt->sjw - 1;
    292.                 tseg1 = dbt->prop_seg + dbt->phase_seg1 - 1;
    293.                 tseg2 = dbt->phase_seg2 - 1;
    294.                 if (dbt->bitrate > 2200000) {
    295.                         u32 tdco;
    296.                         /* Equation based on Bosch's ROCKCHIP_CAN User Manual's
    297.                          * Transmitter Delay Compensation Section
    298.                          */
    299.                         tdco = (rcan->can.clock.freq / dbt->bitrate) * 2 / 3;
    300.                         /* Max valid TDCO value is 63 */
    301.                         if (tdco > 63)
    302.                                 tdco = 63;
    303.                         rockchip_canfd_write(rcan, CAN_TDCR,
    304.                                              (tdco << TDCR_TDCO_SHIFT) |
    305.                                              TDCR_TDC_ENABLE);
    306.                 }
    307.                 reg_btp |= (brp << DBTP_DBRP_SHIFT) |
    308.                            (sjw << DBTP_DSJW_SHIFT) |
    309.                            (tseg1 << DBTP_DTSEG1_SHIFT) |
    310.                            (tseg2 << DBTP_DTSEG2_SHIFT);
    311.                 if (rcan->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES)
    312.                         reg_btp |= DBTP_MODE_3_SAMPLES;
    313.                 rockchip_canfd_write(rcan, CAN_DBTP, reg_btp);
    314.         }
    315.         netdev_dbg(ndev, "%s NBTP=0x%08x, DBTP=0x%08x, TDCR=0x%08x\n", __func__,
    316.                    rockchip_canfd_read(rcan, CAN_NBTP),
    317.                    rockchip_canfd_read(rcan, CAN_DBTP),
    318.                    rockchip_canfd_read(rcan, CAN_TDCR));
    319.         return 0;
    320. }
    321. static int rockchip_canfd_get_berr_counter(const struct net_device *ndev,
    322.                                            struct can_berr_counter *bec)
    323. {
    324.         struct rockchip_canfd *rcan = netdev_priv(ndev);
    325.         int err;
    326.         err = pm_runtime_get_sync(rcan->dev);
    327.         if (err < 0) {
    328.                 netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n",
    329.                            __func__, err);
    330.                 return err;
    331.         }
    332.         bec->rxerr = rockchip_canfd_read(rcan, CAN_RX_ERR_CNT);
    333.         bec->txerr = rockchip_canfd_read(rcan, CAN_TX_ERR_CNT);
    334.         pm_runtime_put(rcan->dev);
    335.         netdev_dbg(ndev, "%s RX_ERR_CNT=0x%08x, TX_ERR_CNT=0x%08x\n", __func__,
    336.                    rockchip_canfd_read(rcan, CAN_RX_ERR_CNT),
    337.                    rockchip_canfd_read(rcan, CAN_TX_ERR_CNT));
    338.         return 0;
    339. }
    340. static int rockchip_canfd_start(struct net_device *ndev)
    341. {
    342.         struct rockchip_canfd *rcan = netdev_priv(ndev);
    343.         u32 val;
    344.         /* we need to enter the reset mode */
    345.         set_reset_mode(ndev);
    346.         rockchip_canfd_write(rcan, CAN_INT_MASK, 0);
    347.         /* RECEIVING FILTER, accept all */
    348.         rockchip_canfd_write(rcan, CAN_IDCODE, 0);
    349.         rockchip_canfd_write(rcan, CAN_IDMASK, CAN_RX_FILTER_MASK);
    350.         rockchip_canfd_write(rcan, CAN_IDCODE0, 0);
    351.         rockchip_canfd_write(rcan, CAN_IDMASK0, CAN_RX_FILTER_MASK);
    352.         rockchip_canfd_write(rcan, CAN_IDCODE1, 0);
    353.         rockchip_canfd_write(rcan, CAN_IDMASK1, CAN_RX_FILTER_MASK);
    354.         rockchip_canfd_write(rcan, CAN_IDCODE2, 0);
    355.         rockchip_canfd_write(rcan, CAN_IDMASK2, CAN_RX_FILTER_MASK);
    356.         rockchip_canfd_write(rcan, CAN_IDCODE3, 0);
    357.         rockchip_canfd_write(rcan, CAN_IDMASK3, CAN_RX_FILTER_MASK);
    358.         rockchip_canfd_write(rcan, CAN_IDCODE4, 0);
    359.         rockchip_canfd_write(rcan, CAN_IDMASK4, CAN_RX_FILTER_MASK);
    360.         /* set mode */
    361.         val = rockchip_canfd_read(rcan, CAN_MODE);
    362.         /* rx fifo enable */
    363.         rockchip_canfd_write(rcan, CAN_RXFC,
    364.                              rockchip_canfd_read(rcan, CAN_RXFC) | FIFO_ENABLE);
    365.         /* Mode */
    366.         val |= MODE_FDOE;
    367.         /* Loopback Mode */
    368.         if (rcan->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
    369.                 val |= MODE_SELF_TEST | MODE_LBACK;
    370.         val |= MODE_AUTO_RETX;
    371.         rockchip_canfd_write(rcan, CAN_MODE, val);
    372.         rockchip_canfd_set_bittiming(ndev);
    373.         set_normal_mode(ndev);
    374.         rcan->can.state = CAN_STATE_ERROR_ACTIVE;
    375.         netdev_dbg(ndev, "%s MODE=0x%08x, INT_MASK=0x%08x\n", __func__,
    376.                    rockchip_canfd_read(rcan, CAN_MODE),
    377.                    rockchip_canfd_read(rcan, CAN_INT_MASK));
    378.         return 0;
    379. }
    380. static int rockchip_canfd_stop(struct net_device *ndev)
    381. {
    382.         struct rockchip_canfd *rcan = netdev_priv(ndev);
    383.         rcan->can.state = CAN_STATE_STOPPED;
    384.         /* we need to enter reset mode */
    385.         set_reset_mode(ndev);
    386.         /* disable all interrupts */
    387.         rockchip_canfd_write(rcan, CAN_INT_MASK, 0xffff);
    388.         netdev_dbg(ndev, "%s MODE=0x%08x, INT_MASK=0x%08x\n", __func__,
    389.                    rockchip_canfd_read(rcan, CAN_MODE),
    390.                    rockchip_canfd_read(rcan, CAN_INT_MASK));
    391.         return 0;
    392. }
    393. static int rockchip_canfd_set_mode(struct net_device *ndev,
    394.                                    enum can_mode mode)
    395. {
    396.         int err;
    397.         switch (mode) {
    398.         case CAN_MODE_START:
    399.                 err = rockchip_canfd_start(ndev);
    400.                 if (err) {
    401.                         netdev_err(ndev, "starting CAN controller failed!\n");
    402.                         return err;
    403.                 }
    404.                 if (netif_queue_stopped(ndev))
    405.                         netif_wake_queue(ndev);
    406.                 break;
    407.         default:
    408.                 return -EOPNOTSUPP;
    409.         }
    410.         return 0;
    411. }
    412. static void rockchip_canfd_tx_err_delay_work(struct work_struct *work)
    413. {
    414.         struct rockchip_canfd *rcan =
    415.                 container_of(work, struct rockchip_canfd, tx_err_work.work);
    416.         u32 mode, err_code, id;
    417.         id = rockchip_canfd_read(rcan, CAN_TXID);
    418.         err_code = rockchip_canfd_read(rcan, CAN_ERR_CODE);
    419.         if (err_code & 0x1fe0000) {
    420.                 mode = rockchip_canfd_read(rcan, CAN_MODE);
    421.                 rockchip_canfd_write(rcan, CAN_MODE, 0);
    422.                 rockchip_canfd_write(rcan, CAN_MODE, mode);
    423.                 rockchip_canfd_write(rcan, CAN_CMD, CAN_TX1_REQ);
    424.                 schedule_delayed_work(&rcan->tx_err_work, 1);
    425.         } else if (rcan->txtorx && rcan->mode >= ROCKCHIP_CAN_MODE && id & CAN_EFF_FLAG) {
    426.                 schedule_delayed_work(&rcan->tx_err_work, 1);
    427.         }
    428. }
    429. /* transmit a CAN message
    430.  * message layout in the sk_buff should be like this:
    431.  * xx xx xx xx         ff         ll 00 11 22 33 44 55 66 77
    432.  * [ can_id ] [flags] [len] [can data (up to 8 bytes]
    433.  */
    434. static int rockchip_canfd_start_xmit(struct sk_buff *skb,
    435.                                      struct net_device *ndev)
    436. {
    437.         struct rockchip_canfd *rcan = netdev_priv(ndev);
    438.         struct canfd_frame *cf = (struct canfd_frame *)skb->data;
    439.         u32 id, dlc;
    440.         u32 cmd = CAN_TX0_REQ;
    441.         int i;
    442.         unsigned long flags;
    443.         if (can_dropped_invalid_skb(ndev, skb))
    444.                 return NETDEV_TX_OK;
    445.         netif_stop_queue(ndev);
    446.         if (rockchip_canfd_read(rcan, CAN_CMD) & CAN_TX0_REQ)
    447.                 cmd = CAN_TX1_REQ;
    448.         /* Watch carefully on the bit sequence */
    449.         if (cf->can_id & CAN_EFF_FLAG) {
    450.                 /* Extended CAN ID format */
    451.                 id = cf->can_id & CAN_EFF_MASK;
    452.                 dlc = can_len2dlc(cf->len) & DLC_MASK;
    453.                 dlc |= FORMAT_MASK;
    454.                 /* Extended frames remote TX request */
    455.                 if (cf->can_id & CAN_RTR_FLAG)
    456.                         dlc |= RTR_MASK;
    457.         } else {
    458.                 /* Standard CAN ID format */
    459.                 id = cf->can_id & CAN_SFF_MASK;
    460.                 dlc = can_len2dlc(cf->len) & DLC_MASK;
    461.                 /* Standard frames remote TX request */
    462.                 if (cf->can_id & CAN_RTR_FLAG)
    463.                         dlc |= RTR_MASK;
    464.         }
    465.         if ((rcan->can.ctrlmode & CAN_CTRLMODE_FD) && can_is_canfd_skb(skb)) {
    466.                 dlc |= TX_FD_ENABLE;
    467.                 if (cf->flags & CANFD_BRS)
    468.                         dlc |= TX_FD_BRS_ENABLE;
    469.         }
    470.         if (rcan->txtorx && rcan->mode >= ROCKCHIP_CAN_MODE && cf->can_id & CAN_EFF_FLAG)
    471.                 rockchip_canfd_write(rcan, CAN_MODE, rockchip_canfd_read(rcan, CAN_MODE) | MODE_RXSTX);
    472.         else
    473.                 rockchip_canfd_write(rcan, CAN_MODE, rockchip_canfd_read(rcan, CAN_MODE) & (~MODE_RXSTX));
    474.         if (!rcan->txtorx && rcan->mode >= ROCKCHIP_CAN_MODE && cf->can_id & CAN_EFF_FLAG) {
    475.                 /* Two frames are sent consecutively.
    476.                  * Before the first frame is tx finished,
    477.                  * the register of the second frame is configured.
    478.                  * Don't be interrupted in the middle.
    479.                  */
    480.                 local_irq_save(flags);
    481.                 rockchip_canfd_write(rcan, CAN_TXID, rcan->tx_invalid[1]);
    482.                 rockchip_canfd_write(rcan, CAN_TXFIC, rcan->tx_invalid[0]);
    483.                 rockchip_canfd_write(rcan, CAN_TXDAT0, rcan->tx_invalid[2]);
    484.                 rockchip_canfd_write(rcan, CAN_TXDAT1, rcan->tx_invalid[3]);
    485.                 rockchip_canfd_write(rcan, CAN_CMD, CAN_TX0_REQ);
    486.                 rockchip_canfd_write(rcan, CAN_TXID, id);
    487.                 rockchip_canfd_write(rcan, CAN_TXFIC, dlc);
    488.                 for (i = 0; i < cf->len; i += 4)
    489.                         rockchip_canfd_write(rcan, CAN_TXDAT0 + i,
    490.                                              *(u32 *)(cf->data + i));
    491.                 rockchip_canfd_write(rcan, CAN_CMD, CAN_TX1_REQ);
    492.                 local_irq_restore(flags);
    493.                 can_put_echo_skb(skb, ndev, 0);
    494.                 return NETDEV_TX_OK;
    495.         }
    496.         rockchip_canfd_write(rcan, CAN_TXID, id);
    497.         rockchip_canfd_write(rcan, CAN_TXFIC, dlc);
    498.         for (i = 0; i < cf->len; i += 4)
    499.                 rockchip_canfd_write(rcan, CAN_TXDAT0 + i,
    500.                                      *(u32 *)(cf->data + i));
    501.         rockchip_canfd_write(rcan, CAN_CMD, CAN_TX0_REQ);
    502.         if (rcan->txtorx && rcan->mode >= ROCKCHIP_CAN_MODE && cf->can_id & CAN_EFF_FLAG)
    503.                 schedule_delayed_work(&rcan->tx_err_work, 1);
    504.         can_put_echo_skb(skb, ndev, 0);
    505.         return NETDEV_TX_OK;
    506. }
    507. static int rockchip_canfd_rx(struct net_device *ndev)
    508. {
    509.         struct rockchip_canfd *rcan = netdev_priv(ndev);
    510.         struct net_device_stats *stats = &ndev->stats;
    511.         struct canfd_frame *cf;
    512.         struct sk_buff *skb;
    513.         u32 id_rockchip_canfd, dlc;
    514.         int i = 0;
    515.         u32 __maybe_unused ts, ret;
    516.         u32 data[16] = {0};
    517.         dlc = rockchip_canfd_read(rcan, CAN_RXFRD);
    518.         id_rockchip_canfd = rockchip_canfd_read(rcan, CAN_RXFRD);
    519.         ts = rockchip_canfd_read(rcan, CAN_RXFRD);
    520.         for (i = 0; i < 16; i++)
    521.                 data[i] = rockchip_canfd_read(rcan, CAN_RXFRD);
    522.         if (rcan->mode >= ROCKCHIP_CAN_MODE) {
    523.                 /* may be an empty frame */
    524.                 if (!dlc && !id_rockchip_canfd)
    525.                         return 1;
    526.                 if (rcan->txtorx) {
    527.                         if (rockchip_canfd_read(rcan, CAN_TX_CHECK_FIC) & FORMAT_MASK) {
    528.                                 ret = rockchip_canfd_read(rcan, CAN_TXID) & CAN_SFF_MASK;
    529.                                 if ((id_rockchip_canfd == ret) && !(dlc & FORMAT_MASK)) {
    530.                                         rockchip_canfd_write(rcan, CAN_TX_CHECK_FIC,
    531.                                                              ts | CAN_TX0_REQ);
    532.                                         return 1;
    533.                                 } else {
    534.                                         return 1;
    535.                                 }
    536.                         }
    537.                 }
    538.         }
    539.         /* create zero'ed CAN frame buffer */
    540.         if (dlc & FDF_MASK)
    541.                 skb = alloc_canfd_skb(ndev, &cf);
    542.         else
    543.                 skb = alloc_can_skb(ndev, (struct can_frame **)&cf);
    544.         if (!skb) {
    545.                 stats->rx_dropped++;
    546.                 return 1;
    547.         }
    548.         /* Change CAN data length format to socketCAN data format */
    549.         if (dlc & FDF_MASK)
    550.                 cf->len = can_dlc2len(dlc & DLC_MASK);
    551.         else
    552.                 cf->len = get_can_dlc(dlc & DLC_MASK);
    553.         /* Change CAN ID format to socketCAN ID format */
    554.         if (dlc & FORMAT_MASK) {
    555.                 /* The received frame is an Extended format frame */
    556.                 cf->can_id = id_rockchip_canfd;
    557.                 cf->can_id |= CAN_EFF_FLAG;
    558.                 if (dlc & RTR_MASK)
    559.                         cf->can_id |= CAN_RTR_FLAG;
    560.         } else {
    561.                 /* The received frame is a standard format frame */
    562.                 cf->can_id = id_rockchip_canfd;
    563.                 if (dlc & RTR_MASK)
    564.                         cf->can_id |= CAN_RTR_FLAG;
    565.         }
    566.         if (dlc & BRS_MASK)
    567.                 cf->flags |= CANFD_BRS;
    568.         if (!(cf->can_id & CAN_RTR_FLAG)) {
    569.                 /* Change CAN data format to socketCAN data format */
    570.                 for (i = 0; i < cf->len; i += 4)
    571.                         *(u32 *)(cf->data + i) = data[i / 4];
    572.         }
    573.         stats->rx_packets++;
    574.         stats->rx_bytes += cf->len;
    575.         netif_rx(skb);
    576.         can_led_event(ndev, CAN_LED_EVENT_RX);
    577.         return 1;
    578. }
    579. static int rockchip_canfd_err(struct net_device *ndev, u32 isr)
    580. {
    581.         struct rockchip_canfd *rcan = netdev_priv(ndev);
    582.         struct net_device_stats *stats = &ndev->stats;
    583.         struct can_frame *cf;
    584.         struct sk_buff *skb;
    585.         unsigned int rxerr, txerr;
    586.         u32 sta_reg;
    587.         skb = alloc_can_err_skb(ndev, &cf);
    588.         rxerr = rockchip_canfd_read(rcan, CAN_RX_ERR_CNT);
    589.         txerr = rockchip_canfd_read(rcan, CAN_TX_ERR_CNT);
    590.         sta_reg = rockchip_canfd_read(rcan, CAN_STATE);
    591.         if (skb) {
    592.                 cf->data[6] = txerr;
    593.                 cf->data[7] = rxerr;
    594.         }
    595.         if (isr & BUS_OFF_INT) {
    596.                 rcan->can.state = CAN_STATE_BUS_OFF;
    597.                 rcan->can.can_stats.bus_off++;
    598.                 cf->can_id |= CAN_ERR_BUSOFF;
    599.         } else if (isr & ERR_WARN_INT) {
    600.                 rcan->can.can_stats.error_warning++;
    601.                 rcan->can.state = CAN_STATE_ERROR_WARNING;
    602.                 /* error warning state */
    603.                 if (likely(skb)) {
    604.                         cf->can_id |= CAN_ERR_CRTL;
    605.                         cf->data[1] = (txerr > rxerr) ?
    606.                                 CAN_ERR_CRTL_TX_WARNING :
    607.                                 CAN_ERR_CRTL_RX_WARNING;
    608.                         cf->data[6] = txerr;
    609.                         cf->data[7] = rxerr;
    610.                 }
    611.         } else if (isr & PASSIVE_ERR_INT) {
    612.                 rcan->can.can_stats.error_passive++;
    613.                 rcan->can.state = CAN_STATE_ERROR_PASSIVE;
    614.                 /* error passive state */
    615.                 cf->can_id |= CAN_ERR_CRTL;
    616.                 cf->data[1] = (txerr > rxerr) ?
    617.                                         CAN_ERR_CRTL_TX_WARNING :
    618.                                         CAN_ERR_CRTL_RX_WARNING;
    619.                 cf->data[6] = txerr;
    620.                 cf->data[7] = rxerr;
    621.         }
    622.         if (rcan->can.state >= CAN_STATE_BUS_OFF ||
    623.             ((sta_reg & 0x20) == 0x20)) {
    624.                 can_bus_off(ndev);
    625.         }
    626.         stats->rx_packets++;
    627.         stats->rx_bytes += cf->can_dlc;
    628.         netif_rx(skb);
    629.         return 0;
    630. }
    631. static irqreturn_t rockchip_canfd_interrupt(int irq, void *dev_id)
    632. {
    633.         struct net_device *ndev = (struct net_device *)dev_id;
    634.         struct rockchip_canfd *rcan = netdev_priv(ndev);
    635.         struct net_device_stats *stats = &ndev->stats;
    636.         u32 err_int = ERR_WARN_INT | RX_BUF_OV_INT | PASSIVE_ERR_INT |
    637.                       TX_LOSTARB_INT | BUS_ERR_INT | BUS_OFF_INT;
    638.         u32 isr;
    639.         u32 dlc = 0;
    640.         u32 quota, work_done = 0;
    641.         isr = rockchip_canfd_read(rcan, CAN_INT);
    642.         if (isr & TX_FINISH_INT) {
    643.                 dlc = rockchip_canfd_read(rcan, CAN_TXFIC);
    644.                 /* transmission complete interrupt */
    645.                 if (dlc & FDF_MASK)
    646.                         stats->tx_bytes += can_dlc2len(dlc & DLC_MASK);
    647.                 else
    648.                         stats->tx_bytes += (dlc & DLC_MASK);
    649.                 stats->tx_packets++;
    650.                 if (rcan->txtorx && rcan->mode >= ROCKCHIP_CAN_MODE && dlc & FORMAT_MASK) {
    651.                         cancel_delayed_work(&rcan->tx_err_work);
    652.                         rockchip_canfd_write(rcan, CAN_TX_CHECK_FIC, FORMAT_MASK);
    653.                         do {
    654.                                 quota = (rockchip_canfd_read(rcan, CAN_RXFC) &
    655.                                          rcan->rx_fifo_mask) >>
    656.                                         rcan->rx_fifo_shift;
    657.                         } while (quota == 0); 
    658.                         if (quota) {
    659.                                 while (work_done < quota)
    660.                                         work_done += rockchip_canfd_rx(ndev);
    661.                         }
    662.                         if (rockchip_canfd_read(rcan, CAN_TX_CHECK_FIC) & CAN_TX0_REQ) {
    663.                                 rockchip_canfd_write(rcan, CAN_CMD, CAN_TX1_REQ);
    664.                                 read_poll_timeout_atomic(rockchip_canfd_read, quota,
    665.                                      (quota & 0x3),
    666.                                      0, 5000000, false, rcan, CAN_CMD);
    667.                         }
    668.                         rockchip_canfd_write(rcan, CAN_TX_CHECK_FIC, 0);
    669.                 }
    670.                 rockchip_canfd_write(rcan, CAN_CMD, 0);
    671.                 can_get_echo_skb(ndev, 0);
    672.                 netif_wake_queue(ndev);
    673.                 can_led_event(ndev, CAN_LED_EVENT_TX);
    674.         }
    675.         if (isr & RX_FINISH_INT) {
    676.                 quota = (rockchip_canfd_read(rcan, CAN_RXFC) & rcan->rx_fifo_mask) >>
    677.                         rcan->rx_fifo_shift;
    678.                 if (quota) {
    679.                         while (work_done < quota)
    680.                                 work_done += rockchip_canfd_rx(ndev);
    681.                 }
    682.         }
    683.         if (isr & err_int) {
    684.                 /* error interrupt */
    685.                 if (rockchip_canfd_err(ndev, isr))
    686.                         netdev_err(ndev, "can't allocate buffer - clearing pending interrupts\n");
    687.         }
    688.         rockchip_canfd_write(rcan, CAN_INT, isr);
    689.         return IRQ_HANDLED;
    690. }
    691. static int rockchip_canfd_open(struct net_device *ndev)
    692. {
    693.         struct rockchip_canfd *rcan = netdev_priv(ndev);
    694.         int err;
    695.         /* common open */
    696.         err = open_candev(ndev);
    697.         if (err)
    698.                 return err;
    699.         err = pm_runtime_get_sync(rcan->dev);
    700.         if (err < 0) {
    701.                 netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n",
    702.                            __func__, err);
    703.                 goto exit;
    704.         }
    705.         err = rockchip_canfd_start(ndev);
    706.         if (err) {
    707.                 netdev_err(ndev, "could not start CAN peripheral\n");
    708.                 goto exit_can_start;
    709.         }
    710.         can_led_event(ndev, CAN_LED_EVENT_OPEN);
    711.         netif_start_queue(ndev);
    712.         netdev_dbg(ndev, "%s\n", __func__);
    713.         return 0;
    714. exit_can_start:
    715.         pm_runtime_put(rcan->dev);
    716. exit:
    717.         close_candev(ndev);
    718.         return err;
    719. }
    720. static int rockchip_canfd_close(struct net_device *ndev)
    721. {
    722.         struct rockchip_canfd *rcan = netdev_priv(ndev);
    723.         netif_stop_queue(ndev);
    724.         rockchip_canfd_stop(ndev);
    725.         close_candev(ndev);
    726.         can_led_event(ndev, CAN_LED_EVENT_STOP);
    727.         pm_runtime_put(rcan->dev);
    728.         cancel_delayed_work_sync(&rcan->tx_err_work);
    729.         netdev_dbg(ndev, "%s\n", __func__);
    730.         return 0;
    731. }
    732. static const struct net_device_ops rockchip_canfd_netdev_ops = {
    733.         .ndo_open = rockchip_canfd_open,
    734.         .ndo_stop = rockchip_canfd_close,
    735.         .ndo_start_xmit = rockchip_canfd_start_xmit,
    736.         .ndo_change_mtu = can_change_mtu,
    737. };
    738. /**
    739.  * rockchip_canfd_suspend - Suspend method for the driver
    740.  * @dev:        Address of the device structure
    741.  *
    742.  * Put the driver into low power mode.
    743.  * Return: 0 on success and failure value on error
    744.  */
    745. static int __maybe_unused rockchip_canfd_suspend(struct device *dev)
    746. {
    747.         struct net_device *ndev = dev_get_drvdata(dev);
    748.         if (netif_running(ndev)) {
    749.                 netif_stop_queue(ndev);
    750.                 netif_device_detach(ndev);
    751.                 rockchip_canfd_stop(ndev);
    752.         }
    753.         return pm_runtime_force_suspend(dev);
    754. }
    755. /**
    756.  * rockchip_canfd_resume - Resume from suspend
    757.  * @dev:        Address of the device structure
    758.  *
    759.  * Resume operation after suspend.
    760.  * Return: 0 on success and failure value on error
    761.  */
    762. static int __maybe_unused rockchip_canfd_resume(struct device *dev)
    763. {
    764.         struct net_device *ndev = dev_get_drvdata(dev);
    765.         int ret;
    766.         ret = pm_runtime_force_resume(dev);
    767.         if (ret) {
    768.                 dev_err(dev, "pm_runtime_force_resume failed on resume\n");
    769.                 return ret;
    770.         }
    771.         if (netif_running(ndev)) {
    772.                 ret = rockchip_canfd_start(ndev);
    773.                 if (ret) {
    774.                         dev_err(dev, "rockchip_canfd_chip_start failed on resume\n");
    775.                         return ret;
    776.                 }
    777.                 netif_device_attach(ndev);
    778.                 netif_start_queue(ndev);
    779.         }
    780.         return 0;
    781. }
    782. /**
    783.  * rockchip_canfd_runtime_suspend - Runtime suspend method for the driver
    784.  * @dev:        Address of the device structure
    785.  *
    786.  * Put the driver into low power mode.
    787.  * Return: 0 always
    788.  */
    789. static int __maybe_unused rockchip_canfd_runtime_suspend(struct device *dev)
    790. {
    791.         struct net_device *ndev = dev_get_drvdata(dev);
    792.         struct rockchip_canfd *rcan = netdev_priv(ndev);
    793.         clk_bulk_disable_unprepare(rcan->num_clks, rcan->clks);
    794.         return 0;
    795. }
    796. /**
    797.  * rockchip_canfd_runtime_resume - Runtime resume from suspend
    798.  * @dev:        Address of the device structure
    799.  *
    800.  * Resume operation after suspend.
    801.  * Return: 0 on success and failure value on error
    802.  */
    803. static int __maybe_unused rockchip_canfd_runtime_resume(struct device *dev)
    804. {
    805.         struct net_device *ndev = dev_get_drvdata(dev);
    806.         struct rockchip_canfd *rcan = netdev_priv(ndev);
    807.         int ret;
    808.         ret = clk_bulk_prepare_enable(rcan->num_clks, rcan->clks);
    809.         if (ret) {
    810.                 dev_err(dev, "Cannot enable clock.\n");
    811.                 return ret;
    812.         }
    813.         return 0;
    814. }
    815. static const struct dev_pm_ops rockchip_canfd_dev_pm_ops = {
    816.         SET_SYSTEM_SLEEP_PM_OPS(rockchip_canfd_suspend, rockchip_canfd_resume)
    817.         SET_RUNTIME_PM_OPS(rockchip_canfd_runtime_suspend,
    818.                            rockchip_canfd_runtime_resume, NULL)
    819. };
    820. static const struct of_device_id rockchip_canfd_of_match[] = {
    821.         {
    822.                 .compatible = "rockchip,canfd-1.0",
    823.                 .data = (void *)ROCKCHIP_CANFD_MODE
    824.         },
    825.         {
    826.                 .compatible = "rockchip,can-2.0",
    827.                 .data = (void *)ROCKCHIP_CAN_MODE
    828.         },
    829.         {
    830.                 .compatible = "rockchip,rk3568-can-2.0",
    831.                 .data = (void *)ROCKCHIP_RK3568_CAN_MODE
    832.         },
    833.         {},
    834. };
    835. MODULE_DEVICE_TABLE(of, rockchip_canfd_of_match);
    836. static int rockchip_canfd_probe(struct platform_device *pdev)
    837. {
    838.         struct net_device *ndev;
    839.         struct rockchip_canfd *rcan;
    840.         struct resource *res;
    841.         void __iomem *addr;
    842.         int err, irq;
    843.         irq = platform_get_irq(pdev, 0);
    844.         if (irq < 0) {
    845.                 dev_err(&pdev->dev, "could not get a valid irq\n");
    846.                 return -ENODEV;
    847.         }
    848.         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
    849.         addr = devm_ioremap_resource(&pdev->dev, res);
    850.         if (IS_ERR(addr))
    851.                 return -EBUSY;
    852.         ndev = alloc_candev(sizeof(struct rockchip_canfd), 1);
    853.         if (!ndev) {
    854.                 dev_err(&pdev->dev, "could not allocate memory for CANFD device\n");
    855.                 return -ENOMEM;
    856.         }
    857.         rcan = netdev_priv(ndev);
    858.         /* register interrupt handler */
    859.         err = devm_request_irq(&pdev->dev, irq, rockchip_canfd_interrupt,
    860.                                0, ndev->name, ndev);
    861.         if (err) {
    862.                 dev_err(&pdev->dev, "request_irq err: %d\n", err);
    863.                 return err;
    864.         }
    865.         rcan->reset = devm_reset_control_array_get(&pdev->dev, false, false);
    866.         if (IS_ERR(rcan->reset)) {
    867.                 if (PTR_ERR(rcan->reset) != -EPROBE_DEFER)
    868.                         dev_err(&pdev->dev, "failed to get canfd reset lines\n");
    869.                 return PTR_ERR(rcan->reset);
    870.         }
    871.         rcan->num_clks = devm_clk_bulk_get_all(&pdev->dev, &rcan->clks);
    872.         if (rcan->num_clks < 1)
    873.                 return -ENODEV;
    874.         rcan->mode = (unsigned long)of_device_get_match_data(&pdev->dev);
    875.         rcan->base = addr;
    876.         rcan->can.clock.freq = clk_get_rate(rcan->clks[0].clk);
    877.         rcan->dev = &pdev->dev;
    878.         rcan->can.state = CAN_STATE_STOPPED;
    879.         switch (rcan->mode) {
    880.         case ROCKCHIP_CANFD_MODE:
    881.                 rcan->can.bittiming_const = &rockchip_canfd_bittiming_const;
    882.                 rcan->can.data_bittiming_const = &rockchip_canfd_data_bittiming_const;
    883.                 rcan->can.do_set_mode = rockchip_canfd_set_mode;
    884.                 rcan->can.do_get_berr_counter = rockchip_canfd_get_berr_counter;
    885.                 rcan->can.do_set_bittiming = rockchip_canfd_set_bittiming;
    886.                 rcan->can.do_set_data_bittiming = rockchip_canfd_set_bittiming;
    887.                 rcan->can.ctrlmode = CAN_CTRLMODE_FD;
    888.                 /* IFI CANFD can do both Bosch FD and ISO FD */
    889.                 rcan->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
    890.                                                CAN_CTRLMODE_FD;
    891.                 rcan->rx_fifo_shift = RX_FIFO_CNT0_SHIFT;
    892.                 rcan->rx_fifo_mask = RX_FIFO_CNT0_MASK;
    893.                 break;
    894.         case ROCKCHIP_CAN_MODE:
    895.         case ROCKCHIP_RK3568_CAN_MODE:
    896.                 rcan->can.bittiming_const = &rockchip_canfd_bittiming_const;
    897.                 rcan->can.do_set_mode = rockchip_canfd_set_mode;
    898.                 rcan->can.do_get_berr_counter = rockchip_canfd_get_berr_counter;
    899.                 rcan->can.ctrlmode_supported = CAN_CTRLMODE_BERR_REPORTING |
    900.                                                CAN_CTRLMODE_LISTENONLY |
    901.                                                CAN_CTRLMODE_LOOPBACK |
    902.                                                CAN_CTRLMODE_3_SAMPLES;
    903.                 rcan->rx_fifo_shift = RX_FIFO_CNT0_SHIFT;
    904.                 rcan->rx_fifo_mask = RX_FIFO_CNT0_MASK;
    905.                 break;
    906.         default:
    907.                 return -EINVAL;
    908.         }
    909.         if (rcan->mode == ROCKCHIP_CAN_MODE) {
    910.                 rcan->rx_fifo_shift = RX_FIFO_CNT1_SHIFT;
    911.                 rcan->rx_fifo_mask = RX_FIFO_CNT1_MASK;
    912.         }
    913.         if (device_property_read_u32_array(&pdev->dev,
    914.                                            "rockchip,tx-invalid-info",
    915.                                            rcan->tx_invalid, 4))
    916.                 rcan->txtorx = 1;
    917.         ndev->netdev_ops = &rockchip_canfd_netdev_ops;
    918.         ndev->irq = irq;
    919.         ndev->flags |= IFF_ECHO;
    920.         rcan->can.restart_ms = 1;
    921.         INIT_DELAYED_WORK(&rcan->tx_err_work, rockchip_canfd_tx_err_delay_work);
    922.         platform_set_drvdata(pdev, ndev);
    923.         SET_NETDEV_DEV(ndev, &pdev->dev);
    924.         pm_runtime_enable(&pdev->dev);
    925.         err = pm_runtime_get_sync(&pdev->dev);
    926.         if (err < 0) {
    927.                 dev_err(&pdev->dev, "%s: pm_runtime_get failed(%d)\n",
    928.                         __func__, err);
    929.                 goto err_pmdisable;
    930.         }
    931.         err = register_candev(ndev);
    932.         if (err) {
    933.                 dev_err(&pdev->dev, "registering %s failed (err=%d)\n",
    934.                         DRV_NAME, err);
    935.                 goto err_disableclks;
    936.         }
    937.         devm_can_led_init(ndev);
    938.         return 0;
    939. err_disableclks:
    940.         pm_runtime_put(&pdev->dev);
    941. err_pmdisable:
    942.         pm_runtime_disable(&pdev->dev);
    943.         free_candev(ndev);
    944.         return err;
    945. }
    946. static int rockchip_canfd_remove(struct platform_device *pdev)
    947. {
    948.         struct net_device *ndev = platform_get_drvdata(pdev);
    949.         unregister_netdev(ndev);
    950.         pm_runtime_disable(&pdev->dev);
    951.         free_candev(ndev);
    952.         return 0;
    953. }
    954. static struct platform_driver rockchip_canfd_driver = {
    955.         .driver = {
    956.                 .name = DRV_NAME,
    957.                 .pm = &rockchip_canfd_dev_pm_ops,
    958.                 .of_match_table = rockchip_canfd_of_match,
    959.         },
    960.         .probe = rockchip_canfd_probe,
    961.         .remove = rockchip_canfd_remove,
    962. };
    963. module_platform_driver(rockchip_canfd_driver);
    964. MODULE_AUTHOR("Elaine Zhang ");
    965. MODULE_LICENSE("GPL");
    966. MODULE_DESCRIPTION("Rockchip CANFD Drivers");

    【iopoll.h】
    1. /* SPDX-License-Identifier: GPL-2.0-only */
    2. /*
    3.  * Copyright (c) 2012-2014 The Linux Foundation. All rights reserved.
    4.  */
    5. #ifndef _LINUX_IOPOLL_H
    6. #define _LINUX_IOPOLL_H
    7. #include <linux/kernel.h>
    8. #include <linux/types.h>
    9. #include <linux/ktime.h>
    10. #include <linux/delay.h>
    11. #include <linux/errno.h>
    12. #include <linux/io.h>
    13. /**
    14.  * read_poll_timeout - Periodically poll an address until a condition is
    15.  *                      met or a timeout occurs
    16.  * @op: accessor function (takes @args as its arguments)
    17.  * @val: Variable to read the value into
    18.  * @cond: Break condition (usually involving @val)
    19.  * @sleep_us: Maximum time to sleep between reads in us (0
    20.  *            tight-loops).  Should be less than ~20ms since usleep_range
    21.  *            is used (see Documentation/timers/timers-howto.rst).
    22.  * @timeout_us: Timeout in us, 0 means never timeout
    23.  * @sleep_before_read: if it is true, sleep @sleep_us before read.
    24.  * @args: arguments for @op poll
    25.  *
    26.  * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
    27.  * case, the last read value at @args is stored in @val. Must not
    28.  * be called from atomic context if sleep_us or timeout_us are used.
    29.  *
    30.  * When available, you'll probably want to use one of the specialized
    31.  * macros defined below rather than this macro directly.
    32.  */
    33. #define read_poll_timeout(op, val, cond, sleep_us, timeout_us, \
    34.                                 sleep_before_read, args...) \
    35. ({ \
    36.         u64 __timeout_us = (timeout_us); \
    37.         unsigned long __sleep_us = (sleep_us); \
    38.         ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
    39.         might_sleep_if((__sleep_us) != 0); \
    40.         if (sleep_before_read && __sleep_us) \
    41.                 usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
    42.         for (;;) { \
    43.                 (val) = op(args); \
    44.                 if (cond) \
    45.                         break; \
    46.                 if (__timeout_us && \
    47.                     ktime_compare(ktime_get(), __timeout) > 0) { \
    48.                         (val) = op(args); \
    49.                         break; \
    50.                 } \
    51.                 if (__sleep_us) \
    52.                         usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
    53.         } \
    54.         (cond) ? 0 : -ETIMEDOUT; \
    55. })
    56. /**
    57.  * read_poll_timeout_atomic - Periodically poll an address until a condition is
    58.  *                              met or a timeout occurs
    59.  * @op: accessor function (takes @args as its arguments)
    60.  * @val: Variable to read the value into
    61.  * @cond: Break condition (usually involving @val)
    62.  * @delay_us: Time to udelay between reads in us (0 tight-loops).  Should
    63.  *            be less than ~10us since udelay is used (see
    64.  *            Documentation/timers/timers-howto.rst).
    65.  * @timeout_us: Timeout in us, 0 means never timeout
    66.  * @delay_before_read: if it is true, delay @delay_us before read.
    67.  * @args: arguments for @op poll
    68.  *
    69.  * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
    70.  * case, the last read value at @args is stored in @val.
    71.  *
    72.  * When available, you'll probably want to use one of the specialized
    73.  * macros defined below rather than this macro directly.
    74.  */
    75. #define read_poll_timeout_atomic(op, val, cond, delay_us, timeout_us, \
    76.                                         delay_before_read, args...) \
    77. ({ \
    78.         u64 __timeout_us = (timeout_us); \
    79.         unsigned long __delay_us = (delay_us); \
    80.         ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
    81.         if (delay_before_read && __delay_us) \
    82.                 udelay(__delay_us); \
    83.         for (;;) { \
    84.                 (val) = op(args); \
    85.                 if (cond) \
    86.                         break; \
    87.                 if (__timeout_us && \
    88.                     ktime_compare(ktime_get(), __timeout) > 0) { \
    89.                         (val) = op(args); \
    90.                         break; \
    91.                 } \
    92.                 if (__delay_us) \
    93.                         udelay(__delay_us); \
    94.         } \
    95.         (cond) ? 0 : -ETIMEDOUT; \
    96. })
    97. /**
    98.  * readx_poll_timeout - Periodically poll an address until a condition is met or a timeout occurs
    99.  * @op: accessor function (takes @addr as its only argument)
    100.  * @addr: Address to poll
    101.  * @val: Variable to read the value into
    102.  * @cond: Break condition (usually involving @val)
    103.  * @sleep_us: Maximum time to sleep between reads in us (0
    104.  *            tight-loops).  Should be less than ~20ms since usleep_range
    105.  *            is used (see Documentation/timers/timers-howto.rst).
    106.  * @timeout_us: Timeout in us, 0 means never timeout
    107.  *
    108.  * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
    109.  * case, the last read value at @addr is stored in @val. Must not
    110.  * be called from atomic context if sleep_us or timeout_us are used.
    111.  *
    112.  * When available, you'll probably want to use one of the specialized
    113.  * macros defined below rather than this macro directly.
    114.  */
    115. #define readx_poll_timeout(op, addr, val, cond, sleep_us, timeout_us)   \
    116.         read_poll_timeout(op, val, cond, sleep_us, timeout_us, false, addr)
    117. /**
    118.  * readx_poll_timeout_atomic - Periodically poll an address until a condition is met or a timeout occurs
    119.  * @op: accessor function (takes @addr as its only argument)
    120.  * @addr: Address to poll
    121.  * @val: Variable to read the value into
    122.  * @cond: Break condition (usually involving @val)
    123.  * @delay_us: Time to udelay between reads in us (0 tight-loops).  Should
    124.  *            be less than ~10us since udelay is used (see
    125.  *            Documentation/timers/timers-howto.rst).
    126.  * @timeout_us: Timeout in us, 0 means never timeout
    127.  *
    128.  * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
    129.  * case, the last read value at @addr is stored in @val.
    130.  *
    131.  * When available, you'll probably want to use one of the specialized
    132.  * macros defined below rather than this macro directly.
    133.  */
    134. #define readx_poll_timeout_atomic(op, addr, val, cond, delay_us, timeout_us) \
    135.         read_poll_timeout_atomic(op, val, cond, delay_us, timeout_us, false, addr)
    136. #define readb_poll_timeout(addr, val, cond, delay_us, timeout_us) \
    137.         readx_poll_timeout(readb, addr, val, cond, delay_us, timeout_us)
    138. #define readb_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
    139.         readx_poll_timeout_atomic(readb, addr, val, cond, delay_us, timeout_us)
    140. #define readw_poll_timeout(addr, val, cond, delay_us, timeout_us) \
    141.         readx_poll_timeout(readw, addr, val, cond, delay_us, timeout_us)
    142. #define readw_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
    143.         readx_poll_timeout_atomic(readw, addr, val, cond, delay_us, timeout_us)
    144. #define readl_poll_timeout(addr, val, cond, delay_us, timeout_us) \
    145.         readx_poll_timeout(readl, addr, val, cond, delay_us, timeout_us)
    146. #define readl_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
    147.         readx_poll_timeout_atomic(readl, addr, val, cond, delay_us, timeout_us)
    148. #define readq_poll_timeout(addr, val, cond, delay_us, timeout_us) \
    149.         readx_poll_timeout(readq, addr, val, cond, delay_us, timeout_us)
    150. #define readq_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
    151.         readx_poll_timeout_atomic(readq, addr, val, cond, delay_us, timeout_us)
    152. #define readb_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
    153.         readx_poll_timeout(readb_relaxed, addr, val, cond, delay_us, timeout_us)
    154. #define readb_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
    155.         readx_poll_timeout_atomic(readb_relaxed, addr, val, cond, delay_us, timeout_us)
    156. #define readw_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
    157.         readx_poll_timeout(readw_relaxed, addr, val, cond, delay_us, timeout_us)
    158. #define readw_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
    159.         readx_poll_timeout_atomic(readw_relaxed, addr, val, cond, delay_us, timeout_us)
    160. #define readl_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
    161.         readx_poll_timeout(readl_relaxed, addr, val, cond, delay_us, timeout_us)
    162. #define readl_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
    163.         readx_poll_timeout_atomic(readl_relaxed, addr, val, cond, delay_us, timeout_us)
    164. #define readq_relaxed_poll_timeout(addr, val, cond, delay_us, timeout_us) \
    165.         readx_poll_timeout(readq_relaxed, addr, val, cond, delay_us, timeout_us)
    166. #define readq_relaxed_poll_timeout_atomic(addr, val, cond, delay_us, timeout_us) \
    167.         readx_poll_timeout_atomic(readq_relaxed, addr, val, cond, delay_us, timeout_us)
    168. #endif /* _LINUX_IOPOLL_H */

  • 相关阅读:
    云原生周刊:KubeSphere 3.4.1 发布 | 2023.11.13
    前端:时间你用对了吗?
    淘宝问大家怎么投诉不良评价?
    [Database] MySQL 5.7+ JSON 字段的使用的处理
    STM32G0开发笔记-Platformio平台下使用libopencm3库
    互联网大厂面试必问的40个SpringBoot面试题【建议收藏】
    Mybatis学习笔记11 缓存相关
    vue首页多模块布局(标题布局)
    服务器推送数据之SSE (server send event)
    ssm医院人事管理系统设计与实现 毕业设计源码111151
  • 原文地址:https://blog.csdn.net/weixin_55163060/article/details/132879875