root/drivers/net/can/ifi_canfd/ifi_canfd.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ifi_canfd_irq_enable
  2. ifi_canfd_read_fifo
  3. ifi_canfd_do_rx_poll
  4. ifi_canfd_handle_lost_msg
  5. ifi_canfd_handle_lec_err
  6. ifi_canfd_get_berr_counter
  7. ifi_canfd_handle_state_change
  8. ifi_canfd_handle_state_errors
  9. ifi_canfd_poll
  10. ifi_canfd_isr
  11. ifi_canfd_set_bittiming
  12. ifi_canfd_set_filter
  13. ifi_canfd_set_filters
  14. ifi_canfd_start
  15. ifi_canfd_stop
  16. ifi_canfd_set_mode
  17. ifi_canfd_open
  18. ifi_canfd_close
  19. ifi_canfd_start_xmit
  20. ifi_canfd_plat_probe
  21. ifi_canfd_plat_remove

   1 /*
   2  * CAN bus driver for IFI CANFD controller
   3  *
   4  * Copyright (C) 2016 Marek Vasut <marex@denx.de>
   5  *
   6  * Details about this controller can be found at
   7  * http://www.ifi-pld.de/IP/CANFD/canfd.html
   8  *
   9  * This file is licensed under the terms of the GNU General Public
  10  * License version 2. This program is licensed "as is" without any
  11  * warranty of any kind, whether express or implied.
  12  */
  13 
  14 #include <linux/clk.h>
  15 #include <linux/delay.h>
  16 #include <linux/interrupt.h>
  17 #include <linux/io.h>
  18 #include <linux/kernel.h>
  19 #include <linux/module.h>
  20 #include <linux/netdevice.h>
  21 #include <linux/of.h>
  22 #include <linux/of_device.h>
  23 #include <linux/platform_device.h>
  24 
  25 #include <linux/can/dev.h>
  26 
  27 #define IFI_CANFD_STCMD                         0x0
  28 #define IFI_CANFD_STCMD_HARDRESET               0xDEADCAFD
  29 #define IFI_CANFD_STCMD_ENABLE                  BIT(0)
  30 #define IFI_CANFD_STCMD_ERROR_ACTIVE            BIT(2)
  31 #define IFI_CANFD_STCMD_ERROR_PASSIVE           BIT(3)
  32 #define IFI_CANFD_STCMD_BUSOFF                  BIT(4)
  33 #define IFI_CANFD_STCMD_ERROR_WARNING           BIT(5)
  34 #define IFI_CANFD_STCMD_BUSMONITOR              BIT(16)
  35 #define IFI_CANFD_STCMD_LOOPBACK                BIT(18)
  36 #define IFI_CANFD_STCMD_DISABLE_CANFD           BIT(24)
  37 #define IFI_CANFD_STCMD_ENABLE_ISO              BIT(25)
  38 #define IFI_CANFD_STCMD_ENABLE_7_9_8_8_TIMING   BIT(26)
  39 #define IFI_CANFD_STCMD_NORMAL_MODE             ((u32)BIT(31))
  40 
  41 #define IFI_CANFD_RXSTCMD                       0x4
  42 #define IFI_CANFD_RXSTCMD_REMOVE_MSG            BIT(0)
  43 #define IFI_CANFD_RXSTCMD_RESET                 BIT(7)
  44 #define IFI_CANFD_RXSTCMD_EMPTY                 BIT(8)
  45 #define IFI_CANFD_RXSTCMD_OVERFLOW              BIT(13)
  46 
  47 #define IFI_CANFD_TXSTCMD                       0x8
  48 #define IFI_CANFD_TXSTCMD_ADD_MSG               BIT(0)
  49 #define IFI_CANFD_TXSTCMD_HIGH_PRIO             BIT(1)
  50 #define IFI_CANFD_TXSTCMD_RESET                 BIT(7)
  51 #define IFI_CANFD_TXSTCMD_EMPTY                 BIT(8)
  52 #define IFI_CANFD_TXSTCMD_FULL                  BIT(12)
  53 #define IFI_CANFD_TXSTCMD_OVERFLOW              BIT(13)
  54 
  55 #define IFI_CANFD_INTERRUPT                     0xc
  56 #define IFI_CANFD_INTERRUPT_ERROR_BUSOFF        BIT(0)
  57 #define IFI_CANFD_INTERRUPT_ERROR_WARNING       BIT(1)
  58 #define IFI_CANFD_INTERRUPT_ERROR_STATE_CHG     BIT(2)
  59 #define IFI_CANFD_INTERRUPT_ERROR_REC_TEC_INC   BIT(3)
  60 #define IFI_CANFD_INTERRUPT_ERROR_COUNTER       BIT(10)
  61 #define IFI_CANFD_INTERRUPT_TXFIFO_EMPTY        BIT(16)
  62 #define IFI_CANFD_INTERRUPT_TXFIFO_REMOVE       BIT(22)
  63 #define IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY       BIT(24)
  64 #define IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY_PER   BIT(25)
  65 #define IFI_CANFD_INTERRUPT_SET_IRQ             ((u32)BIT(31))
  66 
  67 #define IFI_CANFD_IRQMASK                       0x10
  68 #define IFI_CANFD_IRQMASK_ERROR_BUSOFF          BIT(0)
  69 #define IFI_CANFD_IRQMASK_ERROR_WARNING         BIT(1)
  70 #define IFI_CANFD_IRQMASK_ERROR_STATE_CHG       BIT(2)
  71 #define IFI_CANFD_IRQMASK_ERROR_REC_TEC_INC     BIT(3)
  72 #define IFI_CANFD_IRQMASK_SET_ERR               BIT(7)
  73 #define IFI_CANFD_IRQMASK_SET_TS                BIT(15)
  74 #define IFI_CANFD_IRQMASK_TXFIFO_EMPTY          BIT(16)
  75 #define IFI_CANFD_IRQMASK_SET_TX                BIT(23)
  76 #define IFI_CANFD_IRQMASK_RXFIFO_NEMPTY         BIT(24)
  77 #define IFI_CANFD_IRQMASK_SET_RX                ((u32)BIT(31))
  78 
  79 #define IFI_CANFD_TIME                          0x14
  80 #define IFI_CANFD_FTIME                         0x18
  81 #define IFI_CANFD_TIME_TIMEB_OFF                0
  82 #define IFI_CANFD_TIME_TIMEA_OFF                8
  83 #define IFI_CANFD_TIME_PRESCALE_OFF             16
  84 #define IFI_CANFD_TIME_SJW_OFF_7_9_8_8          25
  85 #define IFI_CANFD_TIME_SJW_OFF_4_12_6_6         28
  86 #define IFI_CANFD_TIME_SET_SJW_4_12_6_6         BIT(6)
  87 #define IFI_CANFD_TIME_SET_TIMEB_4_12_6_6       BIT(7)
  88 #define IFI_CANFD_TIME_SET_PRESC_4_12_6_6       BIT(14)
  89 #define IFI_CANFD_TIME_SET_TIMEA_4_12_6_6       BIT(15)
  90 
  91 #define IFI_CANFD_TDELAY                        0x1c
  92 #define IFI_CANFD_TDELAY_DEFAULT                0xb
  93 #define IFI_CANFD_TDELAY_MASK                   0x3fff
  94 #define IFI_CANFD_TDELAY_ABS                    BIT(14)
  95 #define IFI_CANFD_TDELAY_EN                     BIT(15)
  96 
  97 #define IFI_CANFD_ERROR                         0x20
  98 #define IFI_CANFD_ERROR_TX_OFFSET               0
  99 #define IFI_CANFD_ERROR_TX_MASK                 0xff
 100 #define IFI_CANFD_ERROR_RX_OFFSET               16
 101 #define IFI_CANFD_ERROR_RX_MASK                 0xff
 102 
 103 #define IFI_CANFD_ERRCNT                        0x24
 104 
 105 #define IFI_CANFD_SUSPEND                       0x28
 106 
 107 #define IFI_CANFD_REPEAT                        0x2c
 108 
 109 #define IFI_CANFD_TRAFFIC                       0x30
 110 
 111 #define IFI_CANFD_TSCONTROL                     0x34
 112 
 113 #define IFI_CANFD_TSC                           0x38
 114 
 115 #define IFI_CANFD_TST                           0x3c
 116 
 117 #define IFI_CANFD_RES1                          0x40
 118 
 119 #define IFI_CANFD_ERROR_CTR                     0x44
 120 #define IFI_CANFD_ERROR_CTR_UNLOCK_MAGIC        0x21302899
 121 #define IFI_CANFD_ERROR_CTR_OVERLOAD_FIRST      BIT(0)
 122 #define IFI_CANFD_ERROR_CTR_ACK_ERROR_FIRST     BIT(1)
 123 #define IFI_CANFD_ERROR_CTR_BIT0_ERROR_FIRST    BIT(2)
 124 #define IFI_CANFD_ERROR_CTR_BIT1_ERROR_FIRST    BIT(3)
 125 #define IFI_CANFD_ERROR_CTR_STUFF_ERROR_FIRST   BIT(4)
 126 #define IFI_CANFD_ERROR_CTR_CRC_ERROR_FIRST     BIT(5)
 127 #define IFI_CANFD_ERROR_CTR_FORM_ERROR_FIRST    BIT(6)
 128 #define IFI_CANFD_ERROR_CTR_OVERLOAD_ALL        BIT(8)
 129 #define IFI_CANFD_ERROR_CTR_ACK_ERROR_ALL       BIT(9)
 130 #define IFI_CANFD_ERROR_CTR_BIT0_ERROR_ALL      BIT(10)
 131 #define IFI_CANFD_ERROR_CTR_BIT1_ERROR_ALL      BIT(11)
 132 #define IFI_CANFD_ERROR_CTR_STUFF_ERROR_ALL     BIT(12)
 133 #define IFI_CANFD_ERROR_CTR_CRC_ERROR_ALL       BIT(13)
 134 #define IFI_CANFD_ERROR_CTR_FORM_ERROR_ALL      BIT(14)
 135 #define IFI_CANFD_ERROR_CTR_BITPOSITION_OFFSET  16
 136 #define IFI_CANFD_ERROR_CTR_BITPOSITION_MASK    0xff
 137 #define IFI_CANFD_ERROR_CTR_ER_RESET            BIT(30)
 138 #define IFI_CANFD_ERROR_CTR_ER_ENABLE           ((u32)BIT(31))
 139 
 140 #define IFI_CANFD_PAR                           0x48
 141 
 142 #define IFI_CANFD_CANCLOCK                      0x4c
 143 
 144 #define IFI_CANFD_SYSCLOCK                      0x50
 145 
 146 #define IFI_CANFD_VER                           0x54
 147 #define IFI_CANFD_VER_REV_MASK                  0xff
 148 #define IFI_CANFD_VER_REV_MIN_SUPPORTED         0x15
 149 
 150 #define IFI_CANFD_IP_ID                         0x58
 151 #define IFI_CANFD_IP_ID_VALUE                   0xD073CAFD
 152 
 153 #define IFI_CANFD_TEST                          0x5c
 154 
 155 #define IFI_CANFD_RXFIFO_TS_63_32               0x60
 156 
 157 #define IFI_CANFD_RXFIFO_TS_31_0                0x64
 158 
 159 #define IFI_CANFD_RXFIFO_DLC                    0x68
 160 #define IFI_CANFD_RXFIFO_DLC_DLC_OFFSET         0
 161 #define IFI_CANFD_RXFIFO_DLC_DLC_MASK           0xf
 162 #define IFI_CANFD_RXFIFO_DLC_RTR                BIT(4)
 163 #define IFI_CANFD_RXFIFO_DLC_EDL                BIT(5)
 164 #define IFI_CANFD_RXFIFO_DLC_BRS                BIT(6)
 165 #define IFI_CANFD_RXFIFO_DLC_ESI                BIT(7)
 166 #define IFI_CANFD_RXFIFO_DLC_OBJ_OFFSET         8
 167 #define IFI_CANFD_RXFIFO_DLC_OBJ_MASK           0x1ff
 168 #define IFI_CANFD_RXFIFO_DLC_FNR_OFFSET         24
 169 #define IFI_CANFD_RXFIFO_DLC_FNR_MASK           0xff
 170 
 171 #define IFI_CANFD_RXFIFO_ID                     0x6c
 172 #define IFI_CANFD_RXFIFO_ID_ID_OFFSET           0
 173 #define IFI_CANFD_RXFIFO_ID_ID_STD_MASK         CAN_SFF_MASK
 174 #define IFI_CANFD_RXFIFO_ID_ID_STD_OFFSET       0
 175 #define IFI_CANFD_RXFIFO_ID_ID_STD_WIDTH        10
 176 #define IFI_CANFD_RXFIFO_ID_ID_XTD_MASK         CAN_EFF_MASK
 177 #define IFI_CANFD_RXFIFO_ID_ID_XTD_OFFSET       11
 178 #define IFI_CANFD_RXFIFO_ID_ID_XTD_WIDTH        18
 179 #define IFI_CANFD_RXFIFO_ID_IDE                 BIT(29)
 180 
 181 #define IFI_CANFD_RXFIFO_DATA                   0x70    /* 0x70..0xac */
 182 
 183 #define IFI_CANFD_TXFIFO_SUSPEND_US             0xb0
 184 
 185 #define IFI_CANFD_TXFIFO_REPEATCOUNT            0xb4
 186 
 187 #define IFI_CANFD_TXFIFO_DLC                    0xb8
 188 #define IFI_CANFD_TXFIFO_DLC_DLC_OFFSET         0
 189 #define IFI_CANFD_TXFIFO_DLC_DLC_MASK           0xf
 190 #define IFI_CANFD_TXFIFO_DLC_RTR                BIT(4)
 191 #define IFI_CANFD_TXFIFO_DLC_EDL                BIT(5)
 192 #define IFI_CANFD_TXFIFO_DLC_BRS                BIT(6)
 193 #define IFI_CANFD_TXFIFO_DLC_FNR_OFFSET         24
 194 #define IFI_CANFD_TXFIFO_DLC_FNR_MASK           0xff
 195 
 196 #define IFI_CANFD_TXFIFO_ID                     0xbc
 197 #define IFI_CANFD_TXFIFO_ID_ID_OFFSET           0
 198 #define IFI_CANFD_TXFIFO_ID_ID_STD_MASK         CAN_SFF_MASK
 199 #define IFI_CANFD_TXFIFO_ID_ID_STD_OFFSET       0
 200 #define IFI_CANFD_TXFIFO_ID_ID_STD_WIDTH        10
 201 #define IFI_CANFD_TXFIFO_ID_ID_XTD_MASK         CAN_EFF_MASK
 202 #define IFI_CANFD_TXFIFO_ID_ID_XTD_OFFSET       11
 203 #define IFI_CANFD_TXFIFO_ID_ID_XTD_WIDTH        18
 204 #define IFI_CANFD_TXFIFO_ID_IDE                 BIT(29)
 205 
 206 #define IFI_CANFD_TXFIFO_DATA                   0xc0    /* 0xb0..0xfc */
 207 
 208 #define IFI_CANFD_FILTER_MASK(n)                (0x800 + ((n) * 8) + 0)
 209 #define IFI_CANFD_FILTER_MASK_EXT               BIT(29)
 210 #define IFI_CANFD_FILTER_MASK_EDL               BIT(30)
 211 #define IFI_CANFD_FILTER_MASK_VALID             ((u32)BIT(31))
 212 
 213 #define IFI_CANFD_FILTER_IDENT(n)               (0x800 + ((n) * 8) + 4)
 214 #define IFI_CANFD_FILTER_IDENT_IDE              BIT(29)
 215 #define IFI_CANFD_FILTER_IDENT_CANFD            BIT(30)
 216 #define IFI_CANFD_FILTER_IDENT_VALID            ((u32)BIT(31))
 217 
 218 /* IFI CANFD private data structure */
 219 struct ifi_canfd_priv {
 220         struct can_priv         can;    /* must be the first member */
 221         struct napi_struct      napi;
 222         struct net_device       *ndev;
 223         void __iomem            *base;
 224 };
 225 
 226 static void ifi_canfd_irq_enable(struct net_device *ndev, bool enable)
 227 {
 228         struct ifi_canfd_priv *priv = netdev_priv(ndev);
 229         u32 enirq = 0;
 230 
 231         if (enable) {
 232                 enirq = IFI_CANFD_IRQMASK_TXFIFO_EMPTY |
 233                         IFI_CANFD_IRQMASK_RXFIFO_NEMPTY |
 234                         IFI_CANFD_IRQMASK_ERROR_STATE_CHG |
 235                         IFI_CANFD_IRQMASK_ERROR_WARNING |
 236                         IFI_CANFD_IRQMASK_ERROR_BUSOFF;
 237                 if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
 238                         enirq |= IFI_CANFD_INTERRUPT_ERROR_COUNTER;
 239         }
 240 
 241         writel(IFI_CANFD_IRQMASK_SET_ERR |
 242                IFI_CANFD_IRQMASK_SET_TS |
 243                IFI_CANFD_IRQMASK_SET_TX |
 244                IFI_CANFD_IRQMASK_SET_RX | enirq,
 245                priv->base + IFI_CANFD_IRQMASK);
 246 }
 247 
 248 static void ifi_canfd_read_fifo(struct net_device *ndev)
 249 {
 250         struct net_device_stats *stats = &ndev->stats;
 251         struct ifi_canfd_priv *priv = netdev_priv(ndev);
 252         struct canfd_frame *cf;
 253         struct sk_buff *skb;
 254         const u32 rx_irq_mask = IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY |
 255                                 IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY_PER;
 256         u32 rxdlc, rxid;
 257         u32 dlc, id;
 258         int i;
 259 
 260         rxdlc = readl(priv->base + IFI_CANFD_RXFIFO_DLC);
 261         if (rxdlc & IFI_CANFD_RXFIFO_DLC_EDL)
 262                 skb = alloc_canfd_skb(ndev, &cf);
 263         else
 264                 skb = alloc_can_skb(ndev, (struct can_frame **)&cf);
 265 
 266         if (!skb) {
 267                 stats->rx_dropped++;
 268                 return;
 269         }
 270 
 271         dlc = (rxdlc >> IFI_CANFD_RXFIFO_DLC_DLC_OFFSET) &
 272               IFI_CANFD_RXFIFO_DLC_DLC_MASK;
 273         if (rxdlc & IFI_CANFD_RXFIFO_DLC_EDL)
 274                 cf->len = can_dlc2len(dlc);
 275         else
 276                 cf->len = get_can_dlc(dlc);
 277 
 278         rxid = readl(priv->base + IFI_CANFD_RXFIFO_ID);
 279         id = (rxid >> IFI_CANFD_RXFIFO_ID_ID_OFFSET);
 280         if (id & IFI_CANFD_RXFIFO_ID_IDE) {
 281                 id &= IFI_CANFD_RXFIFO_ID_ID_XTD_MASK;
 282                 /*
 283                  * In case the Extended ID frame is received, the standard
 284                  * and extended part of the ID are swapped in the register,
 285                  * so swap them back to obtain the correct ID.
 286                  */
 287                 id = (id >> IFI_CANFD_RXFIFO_ID_ID_XTD_OFFSET) |
 288                      ((id & IFI_CANFD_RXFIFO_ID_ID_STD_MASK) <<
 289                        IFI_CANFD_RXFIFO_ID_ID_XTD_WIDTH);
 290                 id |= CAN_EFF_FLAG;
 291         } else {
 292                 id &= IFI_CANFD_RXFIFO_ID_ID_STD_MASK;
 293         }
 294         cf->can_id = id;
 295 
 296         if (rxdlc & IFI_CANFD_RXFIFO_DLC_ESI) {
 297                 cf->flags |= CANFD_ESI;
 298                 netdev_dbg(ndev, "ESI Error\n");
 299         }
 300 
 301         if (!(rxdlc & IFI_CANFD_RXFIFO_DLC_EDL) &&
 302             (rxdlc & IFI_CANFD_RXFIFO_DLC_RTR)) {
 303                 cf->can_id |= CAN_RTR_FLAG;
 304         } else {
 305                 if (rxdlc & IFI_CANFD_RXFIFO_DLC_BRS)
 306                         cf->flags |= CANFD_BRS;
 307 
 308                 for (i = 0; i < cf->len; i += 4) {
 309                         *(u32 *)(cf->data + i) =
 310                                 readl(priv->base + IFI_CANFD_RXFIFO_DATA + i);
 311                 }
 312         }
 313 
 314         /* Remove the packet from FIFO */
 315         writel(IFI_CANFD_RXSTCMD_REMOVE_MSG, priv->base + IFI_CANFD_RXSTCMD);
 316         writel(rx_irq_mask, priv->base + IFI_CANFD_INTERRUPT);
 317 
 318         stats->rx_packets++;
 319         stats->rx_bytes += cf->len;
 320 
 321         netif_receive_skb(skb);
 322 }
 323 
 324 static int ifi_canfd_do_rx_poll(struct net_device *ndev, int quota)
 325 {
 326         struct ifi_canfd_priv *priv = netdev_priv(ndev);
 327         u32 pkts = 0;
 328         u32 rxst;
 329 
 330         rxst = readl(priv->base + IFI_CANFD_RXSTCMD);
 331         if (rxst & IFI_CANFD_RXSTCMD_EMPTY) {
 332                 netdev_dbg(ndev, "No messages in RX FIFO\n");
 333                 return 0;
 334         }
 335 
 336         for (;;) {
 337                 if (rxst & IFI_CANFD_RXSTCMD_EMPTY)
 338                         break;
 339                 if (quota <= 0)
 340                         break;
 341 
 342                 ifi_canfd_read_fifo(ndev);
 343                 quota--;
 344                 pkts++;
 345                 rxst = readl(priv->base + IFI_CANFD_RXSTCMD);
 346         }
 347 
 348         if (pkts)
 349                 can_led_event(ndev, CAN_LED_EVENT_RX);
 350 
 351         return pkts;
 352 }
 353 
 354 static int ifi_canfd_handle_lost_msg(struct net_device *ndev)
 355 {
 356         struct net_device_stats *stats = &ndev->stats;
 357         struct sk_buff *skb;
 358         struct can_frame *frame;
 359 
 360         netdev_err(ndev, "RX FIFO overflow, message(s) lost.\n");
 361 
 362         stats->rx_errors++;
 363         stats->rx_over_errors++;
 364 
 365         skb = alloc_can_err_skb(ndev, &frame);
 366         if (unlikely(!skb))
 367                 return 0;
 368 
 369         frame->can_id |= CAN_ERR_CRTL;
 370         frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
 371 
 372         netif_receive_skb(skb);
 373 
 374         return 1;
 375 }
 376 
 377 static int ifi_canfd_handle_lec_err(struct net_device *ndev)
 378 {
 379         struct ifi_canfd_priv *priv = netdev_priv(ndev);
 380         struct net_device_stats *stats = &ndev->stats;
 381         struct can_frame *cf;
 382         struct sk_buff *skb;
 383         u32 errctr = readl(priv->base + IFI_CANFD_ERROR_CTR);
 384         const u32 errmask = IFI_CANFD_ERROR_CTR_OVERLOAD_FIRST |
 385                             IFI_CANFD_ERROR_CTR_ACK_ERROR_FIRST |
 386                             IFI_CANFD_ERROR_CTR_BIT0_ERROR_FIRST |
 387                             IFI_CANFD_ERROR_CTR_BIT1_ERROR_FIRST |
 388                             IFI_CANFD_ERROR_CTR_STUFF_ERROR_FIRST |
 389                             IFI_CANFD_ERROR_CTR_CRC_ERROR_FIRST |
 390                             IFI_CANFD_ERROR_CTR_FORM_ERROR_FIRST;
 391 
 392         if (!(errctr & errmask))        /* No error happened. */
 393                 return 0;
 394 
 395         priv->can.can_stats.bus_error++;
 396         stats->rx_errors++;
 397 
 398         /* Propagate the error condition to the CAN stack. */
 399         skb = alloc_can_err_skb(ndev, &cf);
 400         if (unlikely(!skb))
 401                 return 0;
 402 
 403         /* Read the error counter register and check for new errors. */
 404         cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
 405 
 406         if (errctr & IFI_CANFD_ERROR_CTR_OVERLOAD_FIRST)
 407                 cf->data[2] |= CAN_ERR_PROT_OVERLOAD;
 408 
 409         if (errctr & IFI_CANFD_ERROR_CTR_ACK_ERROR_FIRST)
 410                 cf->data[3] = CAN_ERR_PROT_LOC_ACK;
 411 
 412         if (errctr & IFI_CANFD_ERROR_CTR_BIT0_ERROR_FIRST)
 413                 cf->data[2] |= CAN_ERR_PROT_BIT0;
 414 
 415         if (errctr & IFI_CANFD_ERROR_CTR_BIT1_ERROR_FIRST)
 416                 cf->data[2] |= CAN_ERR_PROT_BIT1;
 417 
 418         if (errctr & IFI_CANFD_ERROR_CTR_STUFF_ERROR_FIRST)
 419                 cf->data[2] |= CAN_ERR_PROT_STUFF;
 420 
 421         if (errctr & IFI_CANFD_ERROR_CTR_CRC_ERROR_FIRST)
 422                 cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
 423 
 424         if (errctr & IFI_CANFD_ERROR_CTR_FORM_ERROR_FIRST)
 425                 cf->data[2] |= CAN_ERR_PROT_FORM;
 426 
 427         /* Reset the error counter, ack the IRQ and re-enable the counter. */
 428         writel(IFI_CANFD_ERROR_CTR_ER_RESET, priv->base + IFI_CANFD_ERROR_CTR);
 429         writel(IFI_CANFD_INTERRUPT_ERROR_COUNTER,
 430                priv->base + IFI_CANFD_INTERRUPT);
 431         writel(IFI_CANFD_ERROR_CTR_ER_ENABLE, priv->base + IFI_CANFD_ERROR_CTR);
 432 
 433         stats->rx_packets++;
 434         stats->rx_bytes += cf->can_dlc;
 435         netif_receive_skb(skb);
 436 
 437         return 1;
 438 }
 439 
 440 static int ifi_canfd_get_berr_counter(const struct net_device *ndev,
 441                                       struct can_berr_counter *bec)
 442 {
 443         struct ifi_canfd_priv *priv = netdev_priv(ndev);
 444         u32 err;
 445 
 446         err = readl(priv->base + IFI_CANFD_ERROR);
 447         bec->rxerr = (err >> IFI_CANFD_ERROR_RX_OFFSET) &
 448                      IFI_CANFD_ERROR_RX_MASK;
 449         bec->txerr = (err >> IFI_CANFD_ERROR_TX_OFFSET) &
 450                      IFI_CANFD_ERROR_TX_MASK;
 451 
 452         return 0;
 453 }
 454 
 455 static int ifi_canfd_handle_state_change(struct net_device *ndev,
 456                                          enum can_state new_state)
 457 {
 458         struct ifi_canfd_priv *priv = netdev_priv(ndev);
 459         struct net_device_stats *stats = &ndev->stats;
 460         struct can_frame *cf;
 461         struct sk_buff *skb;
 462         struct can_berr_counter bec;
 463 
 464         switch (new_state) {
 465         case CAN_STATE_ERROR_ACTIVE:
 466                 /* error active state */
 467                 priv->can.can_stats.error_warning++;
 468                 priv->can.state = CAN_STATE_ERROR_ACTIVE;
 469                 break;
 470         case CAN_STATE_ERROR_WARNING:
 471                 /* error warning state */
 472                 priv->can.can_stats.error_warning++;
 473                 priv->can.state = CAN_STATE_ERROR_WARNING;
 474                 break;
 475         case CAN_STATE_ERROR_PASSIVE:
 476                 /* error passive state */
 477                 priv->can.can_stats.error_passive++;
 478                 priv->can.state = CAN_STATE_ERROR_PASSIVE;
 479                 break;
 480         case CAN_STATE_BUS_OFF:
 481                 /* bus-off state */
 482                 priv->can.state = CAN_STATE_BUS_OFF;
 483                 ifi_canfd_irq_enable(ndev, 0);
 484                 priv->can.can_stats.bus_off++;
 485                 can_bus_off(ndev);
 486                 break;
 487         default:
 488                 break;
 489         }
 490 
 491         /* propagate the error condition to the CAN stack */
 492         skb = alloc_can_err_skb(ndev, &cf);
 493         if (unlikely(!skb))
 494                 return 0;
 495 
 496         ifi_canfd_get_berr_counter(ndev, &bec);
 497 
 498         switch (new_state) {
 499         case CAN_STATE_ERROR_WARNING:
 500                 /* error warning state */
 501                 cf->can_id |= CAN_ERR_CRTL;
 502                 cf->data[1] = (bec.txerr > bec.rxerr) ?
 503                         CAN_ERR_CRTL_TX_WARNING :
 504                         CAN_ERR_CRTL_RX_WARNING;
 505                 cf->data[6] = bec.txerr;
 506                 cf->data[7] = bec.rxerr;
 507                 break;
 508         case CAN_STATE_ERROR_PASSIVE:
 509                 /* error passive state */
 510                 cf->can_id |= CAN_ERR_CRTL;
 511                 cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
 512                 if (bec.txerr > 127)
 513                         cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
 514                 cf->data[6] = bec.txerr;
 515                 cf->data[7] = bec.rxerr;
 516                 break;
 517         case CAN_STATE_BUS_OFF:
 518                 /* bus-off state */
 519                 cf->can_id |= CAN_ERR_BUSOFF;
 520                 break;
 521         default:
 522                 break;
 523         }
 524 
 525         stats->rx_packets++;
 526         stats->rx_bytes += cf->can_dlc;
 527         netif_receive_skb(skb);
 528 
 529         return 1;
 530 }
 531 
 532 static int ifi_canfd_handle_state_errors(struct net_device *ndev)
 533 {
 534         struct ifi_canfd_priv *priv = netdev_priv(ndev);
 535         u32 stcmd = readl(priv->base + IFI_CANFD_STCMD);
 536         int work_done = 0;
 537 
 538         if ((stcmd & IFI_CANFD_STCMD_ERROR_ACTIVE) &&
 539             (priv->can.state != CAN_STATE_ERROR_ACTIVE)) {
 540                 netdev_dbg(ndev, "Error, entered active state\n");
 541                 work_done += ifi_canfd_handle_state_change(ndev,
 542                                                 CAN_STATE_ERROR_ACTIVE);
 543         }
 544 
 545         if ((stcmd & IFI_CANFD_STCMD_ERROR_WARNING) &&
 546             (priv->can.state != CAN_STATE_ERROR_WARNING)) {
 547                 netdev_dbg(ndev, "Error, entered warning state\n");
 548                 work_done += ifi_canfd_handle_state_change(ndev,
 549                                                 CAN_STATE_ERROR_WARNING);
 550         }
 551 
 552         if ((stcmd & IFI_CANFD_STCMD_ERROR_PASSIVE) &&
 553             (priv->can.state != CAN_STATE_ERROR_PASSIVE)) {
 554                 netdev_dbg(ndev, "Error, entered passive state\n");
 555                 work_done += ifi_canfd_handle_state_change(ndev,
 556                                                 CAN_STATE_ERROR_PASSIVE);
 557         }
 558 
 559         if ((stcmd & IFI_CANFD_STCMD_BUSOFF) &&
 560             (priv->can.state != CAN_STATE_BUS_OFF)) {
 561                 netdev_dbg(ndev, "Error, entered bus-off state\n");
 562                 work_done += ifi_canfd_handle_state_change(ndev,
 563                                                 CAN_STATE_BUS_OFF);
 564         }
 565 
 566         return work_done;
 567 }
 568 
 569 static int ifi_canfd_poll(struct napi_struct *napi, int quota)
 570 {
 571         struct net_device *ndev = napi->dev;
 572         struct ifi_canfd_priv *priv = netdev_priv(ndev);
 573         u32 rxstcmd = readl(priv->base + IFI_CANFD_RXSTCMD);
 574         int work_done = 0;
 575 
 576         /* Handle bus state changes */
 577         work_done += ifi_canfd_handle_state_errors(ndev);
 578 
 579         /* Handle lost messages on RX */
 580         if (rxstcmd & IFI_CANFD_RXSTCMD_OVERFLOW)
 581                 work_done += ifi_canfd_handle_lost_msg(ndev);
 582 
 583         /* Handle lec errors on the bus */
 584         if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
 585                 work_done += ifi_canfd_handle_lec_err(ndev);
 586 
 587         /* Handle normal messages on RX */
 588         if (!(rxstcmd & IFI_CANFD_RXSTCMD_EMPTY))
 589                 work_done += ifi_canfd_do_rx_poll(ndev, quota - work_done);
 590 
 591         if (work_done < quota) {
 592                 napi_complete_done(napi, work_done);
 593                 ifi_canfd_irq_enable(ndev, 1);
 594         }
 595 
 596         return work_done;
 597 }
 598 
 599 static irqreturn_t ifi_canfd_isr(int irq, void *dev_id)
 600 {
 601         struct net_device *ndev = (struct net_device *)dev_id;
 602         struct ifi_canfd_priv *priv = netdev_priv(ndev);
 603         struct net_device_stats *stats = &ndev->stats;
 604         const u32 rx_irq_mask = IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY |
 605                                 IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY_PER |
 606                                 IFI_CANFD_INTERRUPT_ERROR_COUNTER |
 607                                 IFI_CANFD_INTERRUPT_ERROR_STATE_CHG |
 608                                 IFI_CANFD_INTERRUPT_ERROR_WARNING |
 609                                 IFI_CANFD_INTERRUPT_ERROR_BUSOFF;
 610         const u32 tx_irq_mask = IFI_CANFD_INTERRUPT_TXFIFO_EMPTY |
 611                                 IFI_CANFD_INTERRUPT_TXFIFO_REMOVE;
 612         const u32 clr_irq_mask = ~((u32)IFI_CANFD_INTERRUPT_SET_IRQ);
 613         u32 isr;
 614 
 615         isr = readl(priv->base + IFI_CANFD_INTERRUPT);
 616 
 617         /* No interrupt */
 618         if (isr == 0)
 619                 return IRQ_NONE;
 620 
 621         /* Clear all pending interrupts but ErrWarn */
 622         writel(clr_irq_mask, priv->base + IFI_CANFD_INTERRUPT);
 623 
 624         /* RX IRQ or bus warning, start NAPI */
 625         if (isr & rx_irq_mask) {
 626                 ifi_canfd_irq_enable(ndev, 0);
 627                 napi_schedule(&priv->napi);
 628         }
 629 
 630         /* TX IRQ */
 631         if (isr & IFI_CANFD_INTERRUPT_TXFIFO_REMOVE) {
 632                 stats->tx_bytes += can_get_echo_skb(ndev, 0);
 633                 stats->tx_packets++;
 634                 can_led_event(ndev, CAN_LED_EVENT_TX);
 635         }
 636 
 637         if (isr & tx_irq_mask)
 638                 netif_wake_queue(ndev);
 639 
 640         return IRQ_HANDLED;
 641 }
 642 
 643 static const struct can_bittiming_const ifi_canfd_bittiming_const = {
 644         .name           = KBUILD_MODNAME,
 645         .tseg1_min      = 1,    /* Time segment 1 = prop_seg + phase_seg1 */
 646         .tseg1_max      = 256,
 647         .tseg2_min      = 2,    /* Time segment 2 = phase_seg2 */
 648         .tseg2_max      = 256,
 649         .sjw_max        = 128,
 650         .brp_min        = 2,
 651         .brp_max        = 512,
 652         .brp_inc        = 1,
 653 };
 654 
 655 static void ifi_canfd_set_bittiming(struct net_device *ndev)
 656 {
 657         struct ifi_canfd_priv *priv = netdev_priv(ndev);
 658         const struct can_bittiming *bt = &priv->can.bittiming;
 659         const struct can_bittiming *dbt = &priv->can.data_bittiming;
 660         u16 brp, sjw, tseg1, tseg2, tdc;
 661 
 662         /* Configure bit timing */
 663         brp = bt->brp - 2;
 664         sjw = bt->sjw - 1;
 665         tseg1 = bt->prop_seg + bt->phase_seg1 - 1;
 666         tseg2 = bt->phase_seg2 - 2;
 667         writel((tseg2 << IFI_CANFD_TIME_TIMEB_OFF) |
 668                (tseg1 << IFI_CANFD_TIME_TIMEA_OFF) |
 669                (brp << IFI_CANFD_TIME_PRESCALE_OFF) |
 670                (sjw << IFI_CANFD_TIME_SJW_OFF_7_9_8_8),
 671                priv->base + IFI_CANFD_TIME);
 672 
 673         /* Configure data bit timing */
 674         brp = dbt->brp - 2;
 675         sjw = dbt->sjw - 1;
 676         tseg1 = dbt->prop_seg + dbt->phase_seg1 - 1;
 677         tseg2 = dbt->phase_seg2 - 2;
 678         writel((tseg2 << IFI_CANFD_TIME_TIMEB_OFF) |
 679                (tseg1 << IFI_CANFD_TIME_TIMEA_OFF) |
 680                (brp << IFI_CANFD_TIME_PRESCALE_OFF) |
 681                (sjw << IFI_CANFD_TIME_SJW_OFF_7_9_8_8),
 682                priv->base + IFI_CANFD_FTIME);
 683 
 684         /* Configure transmitter delay */
 685         tdc = dbt->brp * (dbt->prop_seg + dbt->phase_seg1);
 686         tdc &= IFI_CANFD_TDELAY_MASK;
 687         writel(IFI_CANFD_TDELAY_EN | tdc, priv->base + IFI_CANFD_TDELAY);
 688 }
 689 
 690 static void ifi_canfd_set_filter(struct net_device *ndev, const u32 id,
 691                                  const u32 mask, const u32 ident)
 692 {
 693         struct ifi_canfd_priv *priv = netdev_priv(ndev);
 694 
 695         writel(mask, priv->base + IFI_CANFD_FILTER_MASK(id));
 696         writel(ident, priv->base + IFI_CANFD_FILTER_IDENT(id));
 697 }
 698 
 699 static void ifi_canfd_set_filters(struct net_device *ndev)
 700 {
 701         /* Receive all CAN frames (standard ID) */
 702         ifi_canfd_set_filter(ndev, 0,
 703                              IFI_CANFD_FILTER_MASK_VALID |
 704                              IFI_CANFD_FILTER_MASK_EXT,
 705                              IFI_CANFD_FILTER_IDENT_VALID);
 706 
 707         /* Receive all CAN frames (extended ID) */
 708         ifi_canfd_set_filter(ndev, 1,
 709                              IFI_CANFD_FILTER_MASK_VALID |
 710                              IFI_CANFD_FILTER_MASK_EXT,
 711                              IFI_CANFD_FILTER_IDENT_VALID |
 712                              IFI_CANFD_FILTER_IDENT_IDE);
 713 
 714         /* Receive all CANFD frames */
 715         ifi_canfd_set_filter(ndev, 2,
 716                              IFI_CANFD_FILTER_MASK_VALID |
 717                              IFI_CANFD_FILTER_MASK_EDL |
 718                              IFI_CANFD_FILTER_MASK_EXT,
 719                              IFI_CANFD_FILTER_IDENT_VALID |
 720                              IFI_CANFD_FILTER_IDENT_CANFD |
 721                              IFI_CANFD_FILTER_IDENT_IDE);
 722 }
 723 
 724 static void ifi_canfd_start(struct net_device *ndev)
 725 {
 726         struct ifi_canfd_priv *priv = netdev_priv(ndev);
 727         u32 stcmd;
 728 
 729         /* Reset the IP */
 730         writel(IFI_CANFD_STCMD_HARDRESET, priv->base + IFI_CANFD_STCMD);
 731         writel(IFI_CANFD_STCMD_ENABLE_7_9_8_8_TIMING,
 732                priv->base + IFI_CANFD_STCMD);
 733 
 734         ifi_canfd_set_bittiming(ndev);
 735         ifi_canfd_set_filters(ndev);
 736 
 737         /* Reset FIFOs */
 738         writel(IFI_CANFD_RXSTCMD_RESET, priv->base + IFI_CANFD_RXSTCMD);
 739         writel(0, priv->base + IFI_CANFD_RXSTCMD);
 740         writel(IFI_CANFD_TXSTCMD_RESET, priv->base + IFI_CANFD_TXSTCMD);
 741         writel(0, priv->base + IFI_CANFD_TXSTCMD);
 742 
 743         /* Repeat transmission until successful */
 744         writel(0, priv->base + IFI_CANFD_REPEAT);
 745         writel(0, priv->base + IFI_CANFD_SUSPEND);
 746 
 747         /* Clear all pending interrupts */
 748         writel((u32)(~IFI_CANFD_INTERRUPT_SET_IRQ),
 749                priv->base + IFI_CANFD_INTERRUPT);
 750 
 751         stcmd = IFI_CANFD_STCMD_ENABLE | IFI_CANFD_STCMD_NORMAL_MODE |
 752                 IFI_CANFD_STCMD_ENABLE_7_9_8_8_TIMING;
 753 
 754         if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
 755                 stcmd |= IFI_CANFD_STCMD_BUSMONITOR;
 756 
 757         if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
 758                 stcmd |= IFI_CANFD_STCMD_LOOPBACK;
 759 
 760         if ((priv->can.ctrlmode & CAN_CTRLMODE_FD) &&
 761             !(priv->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO))
 762                 stcmd |= IFI_CANFD_STCMD_ENABLE_ISO;
 763 
 764         if (!(priv->can.ctrlmode & CAN_CTRLMODE_FD))
 765                 stcmd |= IFI_CANFD_STCMD_DISABLE_CANFD;
 766 
 767         priv->can.state = CAN_STATE_ERROR_ACTIVE;
 768 
 769         ifi_canfd_irq_enable(ndev, 1);
 770 
 771         /* Unlock, reset and enable the error counter. */
 772         writel(IFI_CANFD_ERROR_CTR_UNLOCK_MAGIC,
 773                priv->base + IFI_CANFD_ERROR_CTR);
 774         writel(IFI_CANFD_ERROR_CTR_ER_RESET, priv->base + IFI_CANFD_ERROR_CTR);
 775         writel(IFI_CANFD_ERROR_CTR_ER_ENABLE, priv->base + IFI_CANFD_ERROR_CTR);
 776 
 777         /* Enable controller */
 778         writel(stcmd, priv->base + IFI_CANFD_STCMD);
 779 }
 780 
 781 static void ifi_canfd_stop(struct net_device *ndev)
 782 {
 783         struct ifi_canfd_priv *priv = netdev_priv(ndev);
 784 
 785         /* Reset and disable the error counter. */
 786         writel(IFI_CANFD_ERROR_CTR_ER_RESET, priv->base + IFI_CANFD_ERROR_CTR);
 787         writel(0, priv->base + IFI_CANFD_ERROR_CTR);
 788 
 789         /* Reset the IP */
 790         writel(IFI_CANFD_STCMD_HARDRESET, priv->base + IFI_CANFD_STCMD);
 791 
 792         /* Mask all interrupts */
 793         writel(~0, priv->base + IFI_CANFD_IRQMASK);
 794 
 795         /* Clear all pending interrupts */
 796         writel((u32)(~IFI_CANFD_INTERRUPT_SET_IRQ),
 797                priv->base + IFI_CANFD_INTERRUPT);
 798 
 799         /* Set the state as STOPPED */
 800         priv->can.state = CAN_STATE_STOPPED;
 801 }
 802 
 803 static int ifi_canfd_set_mode(struct net_device *ndev, enum can_mode mode)
 804 {
 805         switch (mode) {
 806         case CAN_MODE_START:
 807                 ifi_canfd_start(ndev);
 808                 netif_wake_queue(ndev);
 809                 break;
 810         default:
 811                 return -EOPNOTSUPP;
 812         }
 813 
 814         return 0;
 815 }
 816 
 817 static int ifi_canfd_open(struct net_device *ndev)
 818 {
 819         struct ifi_canfd_priv *priv = netdev_priv(ndev);
 820         int ret;
 821 
 822         ret = open_candev(ndev);
 823         if (ret) {
 824                 netdev_err(ndev, "Failed to open CAN device\n");
 825                 return ret;
 826         }
 827 
 828         /* Register interrupt handler */
 829         ret = request_irq(ndev->irq, ifi_canfd_isr, IRQF_SHARED,
 830                           ndev->name, ndev);
 831         if (ret < 0) {
 832                 netdev_err(ndev, "Failed to request interrupt\n");
 833                 goto err_irq;
 834         }
 835 
 836         ifi_canfd_start(ndev);
 837 
 838         can_led_event(ndev, CAN_LED_EVENT_OPEN);
 839         napi_enable(&priv->napi);
 840         netif_start_queue(ndev);
 841 
 842         return 0;
 843 err_irq:
 844         close_candev(ndev);
 845         return ret;
 846 }
 847 
 848 static int ifi_canfd_close(struct net_device *ndev)
 849 {
 850         struct ifi_canfd_priv *priv = netdev_priv(ndev);
 851 
 852         netif_stop_queue(ndev);
 853         napi_disable(&priv->napi);
 854 
 855         ifi_canfd_stop(ndev);
 856 
 857         free_irq(ndev->irq, ndev);
 858 
 859         close_candev(ndev);
 860 
 861         can_led_event(ndev, CAN_LED_EVENT_STOP);
 862 
 863         return 0;
 864 }
 865 
 866 static netdev_tx_t ifi_canfd_start_xmit(struct sk_buff *skb,
 867                                         struct net_device *ndev)
 868 {
 869         struct ifi_canfd_priv *priv = netdev_priv(ndev);
 870         struct canfd_frame *cf = (struct canfd_frame *)skb->data;
 871         u32 txst, txid, txdlc;
 872         int i;
 873 
 874         if (can_dropped_invalid_skb(ndev, skb))
 875                 return NETDEV_TX_OK;
 876 
 877         /* Check if the TX buffer is full */
 878         txst = readl(priv->base + IFI_CANFD_TXSTCMD);
 879         if (txst & IFI_CANFD_TXSTCMD_FULL) {
 880                 netif_stop_queue(ndev);
 881                 netdev_err(ndev, "BUG! TX FIFO full when queue awake!\n");
 882                 return NETDEV_TX_BUSY;
 883         }
 884 
 885         netif_stop_queue(ndev);
 886 
 887         if (cf->can_id & CAN_EFF_FLAG) {
 888                 txid = cf->can_id & CAN_EFF_MASK;
 889                 /*
 890                  * In case the Extended ID frame is transmitted, the
 891                  * standard and extended part of the ID are swapped
 892                  * in the register, so swap them back to send the
 893                  * correct ID.
 894                  */
 895                 txid = (txid >> IFI_CANFD_TXFIFO_ID_ID_XTD_WIDTH) |
 896                        ((txid & IFI_CANFD_TXFIFO_ID_ID_XTD_MASK) <<
 897                          IFI_CANFD_TXFIFO_ID_ID_XTD_OFFSET);
 898                 txid |= IFI_CANFD_TXFIFO_ID_IDE;
 899         } else {
 900                 txid = cf->can_id & CAN_SFF_MASK;
 901         }
 902 
 903         txdlc = can_len2dlc(cf->len);
 904         if ((priv->can.ctrlmode & CAN_CTRLMODE_FD) && can_is_canfd_skb(skb)) {
 905                 txdlc |= IFI_CANFD_TXFIFO_DLC_EDL;
 906                 if (cf->flags & CANFD_BRS)
 907                         txdlc |= IFI_CANFD_TXFIFO_DLC_BRS;
 908         }
 909 
 910         if (cf->can_id & CAN_RTR_FLAG)
 911                 txdlc |= IFI_CANFD_TXFIFO_DLC_RTR;
 912 
 913         /* message ram configuration */
 914         writel(txid, priv->base + IFI_CANFD_TXFIFO_ID);
 915         writel(txdlc, priv->base + IFI_CANFD_TXFIFO_DLC);
 916 
 917         for (i = 0; i < cf->len; i += 4) {
 918                 writel(*(u32 *)(cf->data + i),
 919                        priv->base + IFI_CANFD_TXFIFO_DATA + i);
 920         }
 921 
 922         writel(0, priv->base + IFI_CANFD_TXFIFO_REPEATCOUNT);
 923         writel(0, priv->base + IFI_CANFD_TXFIFO_SUSPEND_US);
 924 
 925         can_put_echo_skb(skb, ndev, 0);
 926 
 927         /* Start the transmission */
 928         writel(IFI_CANFD_TXSTCMD_ADD_MSG, priv->base + IFI_CANFD_TXSTCMD);
 929 
 930         return NETDEV_TX_OK;
 931 }
 932 
 933 static const struct net_device_ops ifi_canfd_netdev_ops = {
 934         .ndo_open       = ifi_canfd_open,
 935         .ndo_stop       = ifi_canfd_close,
 936         .ndo_start_xmit = ifi_canfd_start_xmit,
 937         .ndo_change_mtu = can_change_mtu,
 938 };
 939 
 940 static int ifi_canfd_plat_probe(struct platform_device *pdev)
 941 {
 942         struct device *dev = &pdev->dev;
 943         struct net_device *ndev;
 944         struct ifi_canfd_priv *priv;
 945         struct resource *res;
 946         void __iomem *addr;
 947         int irq, ret;
 948         u32 id, rev;
 949 
 950         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 951         addr = devm_ioremap_resource(dev, res);
 952         irq = platform_get_irq(pdev, 0);
 953         if (IS_ERR(addr) || irq < 0)
 954                 return -EINVAL;
 955 
 956         id = readl(addr + IFI_CANFD_IP_ID);
 957         if (id != IFI_CANFD_IP_ID_VALUE) {
 958                 dev_err(dev, "This block is not IFI CANFD, id=%08x\n", id);
 959                 return -EINVAL;
 960         }
 961 
 962         rev = readl(addr + IFI_CANFD_VER) & IFI_CANFD_VER_REV_MASK;
 963         if (rev < IFI_CANFD_VER_REV_MIN_SUPPORTED) {
 964                 dev_err(dev, "This block is too old (rev %i), minimum supported is rev %i\n",
 965                         rev, IFI_CANFD_VER_REV_MIN_SUPPORTED);
 966                 return -EINVAL;
 967         }
 968 
 969         ndev = alloc_candev(sizeof(*priv), 1);
 970         if (!ndev)
 971                 return -ENOMEM;
 972 
 973         ndev->irq = irq;
 974         ndev->flags |= IFF_ECHO;        /* we support local echo */
 975         ndev->netdev_ops = &ifi_canfd_netdev_ops;
 976 
 977         priv = netdev_priv(ndev);
 978         priv->ndev = ndev;
 979         priv->base = addr;
 980 
 981         netif_napi_add(ndev, &priv->napi, ifi_canfd_poll, 64);
 982 
 983         priv->can.state = CAN_STATE_STOPPED;
 984 
 985         priv->can.clock.freq = readl(addr + IFI_CANFD_CANCLOCK);
 986 
 987         priv->can.bittiming_const       = &ifi_canfd_bittiming_const;
 988         priv->can.data_bittiming_const  = &ifi_canfd_bittiming_const;
 989         priv->can.do_set_mode           = ifi_canfd_set_mode;
 990         priv->can.do_get_berr_counter   = ifi_canfd_get_berr_counter;
 991 
 992         /* IFI CANFD can do both Bosch FD and ISO FD */
 993         priv->can.ctrlmode = CAN_CTRLMODE_FD;
 994 
 995         /* IFI CANFD can do both Bosch FD and ISO FD */
 996         priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
 997                                        CAN_CTRLMODE_LISTENONLY |
 998                                        CAN_CTRLMODE_FD |
 999                                        CAN_CTRLMODE_FD_NON_ISO |
1000                                        CAN_CTRLMODE_BERR_REPORTING;
1001 
1002         platform_set_drvdata(pdev, ndev);
1003         SET_NETDEV_DEV(ndev, dev);
1004 
1005         ret = register_candev(ndev);
1006         if (ret) {
1007                 dev_err(dev, "Failed to register (ret=%d)\n", ret);
1008                 goto err_reg;
1009         }
1010 
1011         devm_can_led_init(ndev);
1012 
1013         dev_info(dev, "Driver registered: regs=%p, irq=%d, clock=%d\n",
1014                  priv->base, ndev->irq, priv->can.clock.freq);
1015 
1016         return 0;
1017 
1018 err_reg:
1019         free_candev(ndev);
1020         return ret;
1021 }
1022 
1023 static int ifi_canfd_plat_remove(struct platform_device *pdev)
1024 {
1025         struct net_device *ndev = platform_get_drvdata(pdev);
1026 
1027         unregister_candev(ndev);
1028         platform_set_drvdata(pdev, NULL);
1029         free_candev(ndev);
1030 
1031         return 0;
1032 }
1033 
1034 static const struct of_device_id ifi_canfd_of_table[] = {
1035         { .compatible = "ifi,canfd-1.0", .data = NULL },
1036         { /* sentinel */ },
1037 };
1038 MODULE_DEVICE_TABLE(of, ifi_canfd_of_table);
1039 
1040 static struct platform_driver ifi_canfd_plat_driver = {
1041         .driver = {
1042                 .name           = KBUILD_MODNAME,
1043                 .of_match_table = ifi_canfd_of_table,
1044         },
1045         .probe  = ifi_canfd_plat_probe,
1046         .remove = ifi_canfd_plat_remove,
1047 };
1048 
1049 module_platform_driver(ifi_canfd_plat_driver);
1050 
1051 MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
1052 MODULE_LICENSE("GPL v2");
1053 MODULE_DESCRIPTION("CAN bus driver for IFI CANFD controller");

/* [<][>][^][v][top][bottom][index][help] */