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