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