Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'linux-can-next-for-4.8-20160617' of git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can-next

Marc Kleine-Budde says:

====================
pull-request: can-next 2016-06-17

this is a pull request of 14 patches for net-next/master.

Geert Uytterhoeven contributes a patch that adds a file patterns for
CAN device tree bindings to MAINTAINERS. A patch by Alexander Aring
fixes warnings when building without proc support. A patch by me
improves the sample point calculation. Marek Vasut's patch converts
the slcan driver to use CAN_MTU. A patch by William Breathitt Gray
converts the tscan1 driver to use module_isa_driver.

Two patches by Maximilian Schneider for the gs_usb driver fix coding
style and add support for set_phys_id callback. 5 patches by Oliver
Hartkopp add support for CANFD to the bcm. And finally two patches
by Ramesh Shanmugasundaram, which add support for the rcar_canfd
driver.
====================

Signed-off-by: David S. Miller <davem@davemloft.net>

+2219 -255
+89
Documentation/devicetree/bindings/net/can/rcar_canfd.txt
··· 1 + Renesas R-Car CAN FD controller Device Tree Bindings 2 + ---------------------------------------------------- 3 + 4 + Required properties: 5 + - compatible: Must contain one or more of the following: 6 + - "renesas,rcar-gen3-canfd" for R-Car Gen3 compatible controller. 7 + - "renesas,r8a7795-canfd" for R8A7795 (R-Car H3) compatible controller. 8 + 9 + When compatible with the generic version, nodes must list the 10 + SoC-specific version corresponding to the platform first, followed by the 11 + family-specific and/or generic versions. 12 + 13 + - reg: physical base address and size of the R-Car CAN FD register map. 14 + - interrupts: interrupt specifier for the Global & Channel interrupts 15 + - clocks: phandles and clock specifiers for 3 clock inputs. 16 + - clock-names: 3 clock input name strings: "fck", "canfd", "can_clk". 17 + - pinctrl-0: pin control group to be used for this controller. 18 + - pinctrl-names: must be "default". 19 + 20 + Required child nodes: 21 + The controller supports two channels and each is represented as a child node. 22 + The name of the child nodes are "channel0" and "channel1" respectively. Each 23 + child node supports the "status" property only, which is used to 24 + enable/disable the respective channel. 25 + 26 + Required properties for "renesas,r8a7795-canfd" compatible: 27 + In R8A7795 SoC, canfd clock is a div6 clock and can be used by both CAN 28 + and CAN FD controller at the same time. It needs to be scaled to maximum 29 + frequency if any of these controllers use it. This is done using the 30 + below properties. 31 + 32 + - assigned-clocks: phandle of canfd clock. 33 + - assigned-clock-rates: maximum frequency of this clock. 34 + 35 + Example 36 + ------- 37 + 38 + SoC common .dtsi file: 39 + 40 + canfd: can@e66c0000 { 41 + compatible = "renesas,r8a7795-canfd", 42 + "renesas,rcar-gen3-canfd"; 43 + reg = <0 0xe66c0000 0 0x8000>; 44 + interrupts = <GIC_SPI 29 IRQ_TYPE_LEVEL_HIGH>, 45 + <GIC_SPI 30 IRQ_TYPE_LEVEL_HIGH>; 46 + clocks = <&cpg CPG_MOD 914>, 47 + <&cpg CPG_CORE R8A7795_CLK_CANFD>, 48 + <&can_clk>; 49 + clock-names = "fck", "canfd", "can_clk"; 50 + assigned-clocks = <&cpg CPG_CORE R8A7795_CLK_CANFD>; 51 + assigned-clock-rates = <40000000>; 52 + power-domains = <&cpg>; 53 + status = "disabled"; 54 + 55 + channel0 { 56 + status = "disabled"; 57 + }; 58 + 59 + channel1 { 60 + status = "disabled"; 61 + }; 62 + }; 63 + 64 + Board specific .dts file: 65 + 66 + E.g. below enables Channel 1 alone in the board. 67 + 68 + &canfd { 69 + pinctrl-0 = <&canfd1_pins>; 70 + pinctrl-names = "default"; 71 + status = "okay"; 72 + 73 + channel1 { 74 + status = "okay"; 75 + }; 76 + }; 77 + 78 + E.g. below enables Channel 0 alone in the board using External clock 79 + as fCAN clock. 80 + 81 + &canfd { 82 + pinctrl-0 = <&canfd0_pins &can_clk_pins>; 83 + pinctrl-names = "default"; 84 + status = "okay"; 85 + 86 + channel0 { 87 + status = "okay"; 88 + }; 89 + };
+24 -1
Documentation/networking/can.txt
··· 31 31 4.2.4 Broadcast Manager message sequence transmission 32 32 4.2.5 Broadcast Manager receive filter timers 33 33 4.2.6 Broadcast Manager multiplex message receive filter 34 + 4.2.7 Broadcast Manager CAN FD support 34 35 4.3 connected transport protocols (SOCK_SEQPACKET) 35 36 4.4 unconnected transport protocols (SOCK_DGRAM) 36 37 ··· 800 799 } mytxmsg; 801 800 802 801 (..) 803 - mytxmsg.nframes = 4; 802 + mytxmsg.msg_head.nframes = 4; 804 803 (..) 805 804 806 805 write(s, &mytxmsg, sizeof(mytxmsg)); ··· 852 851 U64_DATA(&msg.frame[4]) = 0x4F07FC0FF0000000ULL; /* data mask (MUX 0x4F) */ 853 852 854 853 write(s, &msg, sizeof(msg)); 854 + 855 + 4.2.7 Broadcast Manager CAN FD support 856 + 857 + The programming API of the CAN_BCM depends on struct can_frame which is 858 + given as array directly behind the bcm_msg_head structure. To follow this 859 + schema for the CAN FD frames a new flag 'CAN_FD_FRAME' in the bcm_msg_head 860 + flags indicates that the concatenated CAN frame structures behind the 861 + bcm_msg_head are defined as struct canfd_frame. 862 + 863 + struct { 864 + struct bcm_msg_head msg_head; 865 + struct canfd_frame frame[5]; 866 + } msg; 867 + 868 + msg.msg_head.opcode = RX_SETUP; 869 + msg.msg_head.can_id = 0x42; 870 + msg.msg_head.flags = CAN_FD_FRAME; 871 + msg.msg_head.nframes = 5; 872 + (..) 873 + 874 + When using CAN FD frames for multiplex filtering the MUX mask is still 875 + expected in the first 64 bit of the struct canfd_frame data section. 855 876 856 877 4.3 connected transport protocols (SOCK_SEQPACKET) 857 878 4.4 unconnected transport protocols (SOCK_DGRAM)
+1
MAINTAINERS
··· 2814 2814 T: git git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can.git 2815 2815 T: git git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can-next.git 2816 2816 S: Maintained 2817 + F: Documentation/devicetree/bindings/net/can/ 2817 2818 F: drivers/net/can/ 2818 2819 F: include/linux/can/dev.h 2819 2820 F: include/linux/can/platform/
+1 -10
drivers/net/can/Kconfig
··· 104 104 This driver can also be built as a module. If so, the module will be 105 105 called janz-ican3.ko. 106 106 107 - config CAN_RCAR 108 - tristate "Renesas R-Car CAN controller" 109 - depends on ARCH_RENESAS || ARM 110 - ---help--- 111 - Say Y here if you want to use CAN controller found on Renesas R-Car 112 - SoCs. 113 - 114 - To compile this driver as a module, choose M here: the module will 115 - be called rcar_can. 116 - 117 107 config CAN_SUN4I 118 108 tristate "Allwinner A10 CAN controller" 119 109 depends on MACH_SUN4I || MACH_SUN7I || COMPILE_TEST ··· 142 152 source "drivers/net/can/ifi_canfd/Kconfig" 143 153 source "drivers/net/can/m_can/Kconfig" 144 154 source "drivers/net/can/mscan/Kconfig" 155 + source "drivers/net/can/rcar/Kconfig" 145 156 source "drivers/net/can/sja1000/Kconfig" 146 157 source "drivers/net/can/softing/Kconfig" 147 158 source "drivers/net/can/spi/Kconfig"
+1 -1
drivers/net/can/Makefile
··· 10 10 11 11 can-dev-$(CONFIG_CAN_LEDS) += led.o 12 12 13 + obj-y += rcar/ 13 14 obj-y += spi/ 14 15 obj-y += usb/ 15 16 obj-y += softing/ ··· 25 24 obj-$(CONFIG_CAN_JANZ_ICAN3) += janz-ican3.o 26 25 obj-$(CONFIG_CAN_MSCAN) += mscan/ 27 26 obj-$(CONFIG_CAN_M_CAN) += m_can/ 28 - obj-$(CONFIG_CAN_RCAR) += rcar_can.o 29 27 obj-$(CONFIG_CAN_SJA1000) += sja1000/ 30 28 obj-$(CONFIG_CAN_SUN4I) += sun4i_can.o 31 29 obj-$(CONFIG_CAN_TI_HECC) += ti_hecc.o
+85 -55
drivers/net/can/dev.c
··· 69 69 70 70 #ifdef CONFIG_CAN_CALC_BITTIMING 71 71 #define CAN_CALC_MAX_ERROR 50 /* in one-tenth of a percent */ 72 + #define CAN_CALC_SYNC_SEG 1 72 73 73 74 /* 74 75 * Bit-timing calculation derived from: ··· 84 83 * registers of the CAN controller. You can find more information 85 84 * in the header file linux/can/netlink.h. 86 85 */ 87 - static int can_update_spt(const struct can_bittiming_const *btc, 88 - int sampl_pt, int tseg, int *tseg1, int *tseg2) 86 + static int can_update_sample_point(const struct can_bittiming_const *btc, 87 + unsigned int sample_point_nominal, unsigned int tseg, 88 + unsigned int *tseg1_ptr, unsigned int *tseg2_ptr, 89 + unsigned int *sample_point_error_ptr) 89 90 { 90 - *tseg2 = tseg + 1 - (sampl_pt * (tseg + 1)) / 1000; 91 - if (*tseg2 < btc->tseg2_min) 92 - *tseg2 = btc->tseg2_min; 93 - if (*tseg2 > btc->tseg2_max) 94 - *tseg2 = btc->tseg2_max; 95 - *tseg1 = tseg - *tseg2; 96 - if (*tseg1 > btc->tseg1_max) { 97 - *tseg1 = btc->tseg1_max; 98 - *tseg2 = tseg - *tseg1; 91 + unsigned int sample_point_error, best_sample_point_error = UINT_MAX; 92 + unsigned int sample_point, best_sample_point = 0; 93 + unsigned int tseg1, tseg2; 94 + int i; 95 + 96 + for (i = 0; i <= 1; i++) { 97 + tseg2 = tseg + CAN_CALC_SYNC_SEG - (sample_point_nominal * (tseg + CAN_CALC_SYNC_SEG)) / 1000 - i; 98 + tseg2 = clamp(tseg2, btc->tseg2_min, btc->tseg2_max); 99 + tseg1 = tseg - tseg2; 100 + if (tseg1 > btc->tseg1_max) { 101 + tseg1 = btc->tseg1_max; 102 + tseg2 = tseg - tseg1; 103 + } 104 + 105 + sample_point = 1000 * (tseg + CAN_CALC_SYNC_SEG - tseg2) / (tseg + CAN_CALC_SYNC_SEG); 106 + sample_point_error = abs(sample_point_nominal - sample_point); 107 + 108 + if ((sample_point <= sample_point_nominal) && (sample_point_error < best_sample_point_error)) { 109 + best_sample_point = sample_point; 110 + best_sample_point_error = sample_point_error; 111 + *tseg1_ptr = tseg1; 112 + *tseg2_ptr = tseg2; 113 + } 99 114 } 100 - return 1000 * (tseg + 1 - *tseg2) / (tseg + 1); 115 + 116 + if (sample_point_error_ptr) 117 + *sample_point_error_ptr = best_sample_point_error; 118 + 119 + return best_sample_point; 101 120 } 102 121 103 122 static int can_calc_bittiming(struct net_device *dev, struct can_bittiming *bt, 104 123 const struct can_bittiming_const *btc) 105 124 { 106 125 struct can_priv *priv = netdev_priv(dev); 107 - long best_error = 1000000000, error = 0; 108 - int best_tseg = 0, best_brp = 0, brp = 0; 109 - int tsegall, tseg = 0, tseg1 = 0, tseg2 = 0; 110 - int spt_error = 1000, spt = 0, sampl_pt; 111 - long rate; 126 + unsigned int bitrate; /* current bitrate */ 127 + unsigned int bitrate_error; /* difference between current and nominal value */ 128 + unsigned int best_bitrate_error = UINT_MAX; 129 + unsigned int sample_point_error; /* difference between current and nominal value */ 130 + unsigned int best_sample_point_error = UINT_MAX; 131 + unsigned int sample_point_nominal; /* nominal sample point */ 132 + unsigned int best_tseg = 0; /* current best value for tseg */ 133 + unsigned int best_brp = 0; /* current best value for brp */ 134 + unsigned int brp, tsegall, tseg, tseg1 = 0, tseg2 = 0; 112 135 u64 v64; 113 136 114 137 /* Use CiA recommended sample points */ 115 138 if (bt->sample_point) { 116 - sampl_pt = bt->sample_point; 139 + sample_point_nominal = bt->sample_point; 117 140 } else { 118 141 if (bt->bitrate > 800000) 119 - sampl_pt = 750; 142 + sample_point_nominal = 750; 120 143 else if (bt->bitrate > 500000) 121 - sampl_pt = 800; 144 + sample_point_nominal = 800; 122 145 else 123 - sampl_pt = 875; 146 + sample_point_nominal = 875; 124 147 } 125 148 126 149 /* tseg even = round down, odd = round up */ 127 150 for (tseg = (btc->tseg1_max + btc->tseg2_max) * 2 + 1; 128 151 tseg >= (btc->tseg1_min + btc->tseg2_min) * 2; tseg--) { 129 - tsegall = 1 + tseg / 2; 152 + tsegall = CAN_CALC_SYNC_SEG + tseg / 2; 153 + 130 154 /* Compute all possible tseg choices (tseg=tseg1+tseg2) */ 131 155 brp = priv->clock.freq / (tsegall * bt->bitrate) + tseg % 2; 132 - /* chose brp step which is possible in system */ 156 + 157 + /* choose brp step which is possible in system */ 133 158 brp = (brp / btc->brp_inc) * btc->brp_inc; 134 159 if ((brp < btc->brp_min) || (brp > btc->brp_max)) 135 160 continue; 136 - rate = priv->clock.freq / (brp * tsegall); 137 - error = bt->bitrate - rate; 161 + 162 + bitrate = priv->clock.freq / (brp * tsegall); 163 + bitrate_error = abs(bt->bitrate - bitrate); 164 + 138 165 /* tseg brp biterror */ 139 - if (error < 0) 140 - error = -error; 141 - if (error > best_error) 166 + if (bitrate_error > best_bitrate_error) 142 167 continue; 143 - best_error = error; 144 - if (error == 0) { 145 - spt = can_update_spt(btc, sampl_pt, tseg / 2, 146 - &tseg1, &tseg2); 147 - error = sampl_pt - spt; 148 - if (error < 0) 149 - error = -error; 150 - if (error > spt_error) 151 - continue; 152 - spt_error = error; 153 - } 168 + 169 + /* reset sample point error if we have a better bitrate */ 170 + if (bitrate_error < best_bitrate_error) 171 + best_sample_point_error = UINT_MAX; 172 + 173 + can_update_sample_point(btc, sample_point_nominal, tseg / 2, &tseg1, &tseg2, &sample_point_error); 174 + if (sample_point_error > best_sample_point_error) 175 + continue; 176 + 177 + best_sample_point_error = sample_point_error; 178 + best_bitrate_error = bitrate_error; 154 179 best_tseg = tseg / 2; 155 180 best_brp = brp; 156 - if (error == 0) 181 + 182 + if (bitrate_error == 0 && sample_point_error == 0) 157 183 break; 158 184 } 159 185 160 - if (best_error) { 186 + if (best_bitrate_error) { 161 187 /* Error in one-tenth of a percent */ 162 - error = (best_error * 1000) / bt->bitrate; 163 - if (error > CAN_CALC_MAX_ERROR) { 188 + v64 = (u64)best_bitrate_error * 1000; 189 + do_div(v64, bt->bitrate); 190 + bitrate_error = (u32)v64; 191 + if (bitrate_error > CAN_CALC_MAX_ERROR) { 164 192 netdev_err(dev, 165 - "bitrate error %ld.%ld%% too high\n", 166 - error / 10, error % 10); 193 + "bitrate error %d.%d%% too high\n", 194 + bitrate_error / 10, bitrate_error % 10); 167 195 return -EDOM; 168 - } else { 169 - netdev_warn(dev, "bitrate error %ld.%ld%%\n", 170 - error / 10, error % 10); 171 196 } 197 + netdev_warn(dev, "bitrate error %d.%d%%\n", 198 + bitrate_error / 10, bitrate_error % 10); 172 199 } 173 200 174 201 /* real sample point */ 175 - bt->sample_point = can_update_spt(btc, sampl_pt, best_tseg, 176 - &tseg1, &tseg2); 202 + bt->sample_point = can_update_sample_point(btc, sample_point_nominal, best_tseg, 203 + &tseg1, &tseg2, NULL); 177 204 178 - v64 = (u64)best_brp * 1000000000UL; 205 + v64 = (u64)best_brp * 1000 * 1000 * 1000; 179 206 do_div(v64, priv->clock.freq); 180 207 bt->tq = (u32)v64; 181 208 bt->prop_seg = tseg1 / 2; ··· 211 182 bt->phase_seg2 = tseg2; 212 183 213 184 /* check for sjw user settings */ 214 - if (!bt->sjw || !btc->sjw_max) 185 + if (!bt->sjw || !btc->sjw_max) { 215 186 bt->sjw = 1; 216 - else { 187 + } else { 217 188 /* bt->sjw is at least 1 -> sanitize upper bound to sjw_max */ 218 189 if (bt->sjw > btc->sjw_max) 219 190 bt->sjw = btc->sjw_max; ··· 223 194 } 224 195 225 196 bt->brp = best_brp; 226 - /* real bit-rate */ 227 - bt->bitrate = priv->clock.freq / (bt->brp * (tseg1 + tseg2 + 1)); 197 + 198 + /* real bitrate */ 199 + bt->bitrate = priv->clock.freq / (bt->brp * (CAN_CALC_SYNC_SEG + tseg1 + tseg2)); 228 200 229 201 return 0; 230 202 }
+21
drivers/net/can/rcar/Kconfig
··· 1 + config CAN_RCAR 2 + tristate "Renesas R-Car CAN controller" 3 + depends on ARCH_RENESAS || ARM 4 + ---help--- 5 + Say Y here if you want to use CAN controller found on Renesas R-Car 6 + SoCs. 7 + 8 + To compile this driver as a module, choose M here: the module will 9 + be called rcar_can. 10 + 11 + config CAN_RCAR_CANFD 12 + tristate "Renesas R-Car CAN FD controller" 13 + depends on ARCH_RENESAS || ARM 14 + ---help--- 15 + Say Y here if you want to use CAN FD controller found on 16 + Renesas R-Car SoCs. The driver puts the controller in CAN FD only 17 + mode, which can interoperate with CAN2.0 nodes but does not support 18 + dedicated CAN 2.0 mode. 19 + 20 + To compile this driver as a module, choose M here: the module will 21 + be called rcar_canfd.
+6
drivers/net/can/rcar/Makefile
··· 1 + # 2 + # Makefile for the Renesas R-Car CAN & CAN FD controller drivers 3 + # 4 + 5 + obj-$(CONFIG_CAN_RCAR) += rcar_can.o 6 + obj-$(CONFIG_CAN_RCAR_CANFD) += rcar_canfd.o
+1695
drivers/net/can/rcar/rcar_canfd.c
··· 1 + /* Renesas R-Car CAN FD device driver 2 + * 3 + * Copyright (C) 2015 Renesas Electronics Corp. 4 + * 5 + * This program is free software; you can redistribute it and/or modify it 6 + * under the terms of the GNU General Public License as published by the 7 + * Free Software Foundation; either version 2 of the License, or (at your 8 + * option) any later version. 9 + */ 10 + 11 + /* The R-Car CAN FD controller can operate in either one of the below two modes 12 + * - CAN FD only mode 13 + * - Classical CAN (CAN 2.0) only mode 14 + * 15 + * This driver puts the controller in CAN FD only mode by default. In this 16 + * mode, the controller acts as a CAN FD node that can also interoperate with 17 + * CAN 2.0 nodes. 18 + * 19 + * As of now, this driver does not support the Classical CAN (CAN 2.0) mode, 20 + * which is handled by a different register map compared to CAN FD only mode. 21 + * 22 + * Note: The h/w manual register naming convention is clumsy and not acceptable 23 + * to use as it is in the driver. However, those names are added as comments 24 + * wherever it is modified to a readable name. 25 + */ 26 + 27 + #include <linux/module.h> 28 + #include <linux/moduleparam.h> 29 + #include <linux/kernel.h> 30 + #include <linux/types.h> 31 + #include <linux/interrupt.h> 32 + #include <linux/errno.h> 33 + #include <linux/netdevice.h> 34 + #include <linux/platform_device.h> 35 + #include <linux/can/led.h> 36 + #include <linux/can/dev.h> 37 + #include <linux/clk.h> 38 + #include <linux/of.h> 39 + #include <linux/of_device.h> 40 + #include <linux/bitmap.h> 41 + #include <linux/bitops.h> 42 + #include <linux/iopoll.h> 43 + 44 + #define RCANFD_DRV_NAME "rcar_canfd" 45 + 46 + /* Global register bits */ 47 + 48 + /* RSCFDnCFDGRMCFG */ 49 + #define RCANFD_GRMCFG_RCMC BIT(0) 50 + 51 + /* RSCFDnCFDGCFG */ 52 + #define RCANFD_GCFG_CMPOC BIT(5) 53 + #define RCANFD_GCFG_DCS BIT(4) 54 + #define RCANFD_GCFG_DCE BIT(1) 55 + #define RCANFD_GCFG_TPRI BIT(0) 56 + 57 + /* RSCFDnCFDGCTR */ 58 + #define RCANFD_GCTR_TSRST BIT(16) 59 + #define RCANFD_GCTR_CFMPOFIE BIT(11) 60 + #define RCANFD_GCTR_THLEIE BIT(10) 61 + #define RCANFD_GCTR_MEIE BIT(9) 62 + #define RCANFD_GCTR_DEIE BIT(8) 63 + #define RCANFD_GCTR_GSLPR BIT(2) 64 + #define RCANFD_GCTR_GMDC_MASK (0x3) 65 + #define RCANFD_GCTR_GMDC_GOPM (0x0) 66 + #define RCANFD_GCTR_GMDC_GRESET (0x1) 67 + #define RCANFD_GCTR_GMDC_GTEST (0x2) 68 + 69 + /* RSCFDnCFDGSTS */ 70 + #define RCANFD_GSTS_GRAMINIT BIT(3) 71 + #define RCANFD_GSTS_GSLPSTS BIT(2) 72 + #define RCANFD_GSTS_GHLTSTS BIT(1) 73 + #define RCANFD_GSTS_GRSTSTS BIT(0) 74 + /* Non-operational status */ 75 + #define RCANFD_GSTS_GNOPM (BIT(0) | BIT(1) | BIT(2) | BIT(3)) 76 + 77 + /* RSCFDnCFDGERFL */ 78 + #define RCANFD_GERFL_EEF1 BIT(17) 79 + #define RCANFD_GERFL_EEF0 BIT(16) 80 + #define RCANFD_GERFL_CMPOF BIT(3) 81 + #define RCANFD_GERFL_THLES BIT(2) 82 + #define RCANFD_GERFL_MES BIT(1) 83 + #define RCANFD_GERFL_DEF BIT(0) 84 + 85 + #define RCANFD_GERFL_ERR(x) ((x) & (RCANFD_GERFL_EEF1 |\ 86 + RCANFD_GERFL_EEF0 |\ 87 + RCANFD_GERFL_MES |\ 88 + RCANFD_GERFL_CMPOF)) 89 + 90 + /* AFL Rx rules registers */ 91 + 92 + /* RSCFDnCFDGAFLCFG0 */ 93 + #define RCANFD_GAFLCFG_SETRNC(n, x) (((x) & 0xff) << (24 - n * 8)) 94 + #define RCANFD_GAFLCFG_GETRNC(n, x) (((x) >> (24 - n * 8)) & 0xff) 95 + 96 + /* RSCFDnCFDGAFLECTR */ 97 + #define RCANFD_GAFLECTR_AFLDAE BIT(8) 98 + #define RCANFD_GAFLECTR_AFLPN(x) ((x) & 0x1f) 99 + 100 + /* RSCFDnCFDGAFLIDj */ 101 + #define RCANFD_GAFLID_GAFLLB BIT(29) 102 + 103 + /* RSCFDnCFDGAFLP1_j */ 104 + #define RCANFD_GAFLP1_GAFLFDP(x) (1 << (x)) 105 + 106 + /* Channel register bits */ 107 + 108 + /* RSCFDnCFDCmNCFG */ 109 + #define RCANFD_NCFG_NTSEG2(x) (((x) & 0x1f) << 24) 110 + #define RCANFD_NCFG_NTSEG1(x) (((x) & 0x7f) << 16) 111 + #define RCANFD_NCFG_NSJW(x) (((x) & 0x1f) << 11) 112 + #define RCANFD_NCFG_NBRP(x) (((x) & 0x3ff) << 0) 113 + 114 + /* RSCFDnCFDCmCTR */ 115 + #define RCANFD_CCTR_CTME BIT(24) 116 + #define RCANFD_CCTR_ERRD BIT(23) 117 + #define RCANFD_CCTR_BOM_MASK (0x3 << 21) 118 + #define RCANFD_CCTR_BOM_ISO (0x0 << 21) 119 + #define RCANFD_CCTR_BOM_BENTRY (0x1 << 21) 120 + #define RCANFD_CCTR_BOM_BEND (0x2 << 21) 121 + #define RCANFD_CCTR_TDCVFIE BIT(19) 122 + #define RCANFD_CCTR_SOCOIE BIT(18) 123 + #define RCANFD_CCTR_EOCOIE BIT(17) 124 + #define RCANFD_CCTR_TAIE BIT(16) 125 + #define RCANFD_CCTR_ALIE BIT(15) 126 + #define RCANFD_CCTR_BLIE BIT(14) 127 + #define RCANFD_CCTR_OLIE BIT(13) 128 + #define RCANFD_CCTR_BORIE BIT(12) 129 + #define RCANFD_CCTR_BOEIE BIT(11) 130 + #define RCANFD_CCTR_EPIE BIT(10) 131 + #define RCANFD_CCTR_EWIE BIT(9) 132 + #define RCANFD_CCTR_BEIE BIT(8) 133 + #define RCANFD_CCTR_CSLPR BIT(2) 134 + #define RCANFD_CCTR_CHMDC_MASK (0x3) 135 + #define RCANFD_CCTR_CHDMC_COPM (0x0) 136 + #define RCANFD_CCTR_CHDMC_CRESET (0x1) 137 + #define RCANFD_CCTR_CHDMC_CHLT (0x2) 138 + 139 + /* RSCFDnCFDCmSTS */ 140 + #define RCANFD_CSTS_COMSTS BIT(7) 141 + #define RCANFD_CSTS_RECSTS BIT(6) 142 + #define RCANFD_CSTS_TRMSTS BIT(5) 143 + #define RCANFD_CSTS_BOSTS BIT(4) 144 + #define RCANFD_CSTS_EPSTS BIT(3) 145 + #define RCANFD_CSTS_SLPSTS BIT(2) 146 + #define RCANFD_CSTS_HLTSTS BIT(1) 147 + #define RCANFD_CSTS_CRSTSTS BIT(0) 148 + 149 + #define RCANFD_CSTS_TECCNT(x) (((x) >> 24) & 0xff) 150 + #define RCANFD_CSTS_RECCNT(x) (((x) >> 16) & 0xff) 151 + 152 + /* RSCFDnCFDCmERFL */ 153 + #define RCANFD_CERFL_ADERR BIT(14) 154 + #define RCANFD_CERFL_B0ERR BIT(13) 155 + #define RCANFD_CERFL_B1ERR BIT(12) 156 + #define RCANFD_CERFL_CERR BIT(11) 157 + #define RCANFD_CERFL_AERR BIT(10) 158 + #define RCANFD_CERFL_FERR BIT(9) 159 + #define RCANFD_CERFL_SERR BIT(8) 160 + #define RCANFD_CERFL_ALF BIT(7) 161 + #define RCANFD_CERFL_BLF BIT(6) 162 + #define RCANFD_CERFL_OVLF BIT(5) 163 + #define RCANFD_CERFL_BORF BIT(4) 164 + #define RCANFD_CERFL_BOEF BIT(3) 165 + #define RCANFD_CERFL_EPF BIT(2) 166 + #define RCANFD_CERFL_EWF BIT(1) 167 + #define RCANFD_CERFL_BEF BIT(0) 168 + 169 + #define RCANFD_CERFL_ERR(x) ((x) & (0x7fff)) /* above bits 14:0 */ 170 + 171 + /* RSCFDnCFDCmDCFG */ 172 + #define RCANFD_DCFG_DSJW(x) (((x) & 0x7) << 24) 173 + #define RCANFD_DCFG_DTSEG2(x) (((x) & 0x7) << 20) 174 + #define RCANFD_DCFG_DTSEG1(x) (((x) & 0xf) << 16) 175 + #define RCANFD_DCFG_DBRP(x) (((x) & 0xff) << 0) 176 + 177 + /* RSCFDnCFDCmFDCFG */ 178 + #define RCANFD_FDCFG_TDCE BIT(9) 179 + #define RCANFD_FDCFG_TDCOC BIT(8) 180 + #define RCANFD_FDCFG_TDCO(x) (((x) & 0x7f) >> 16) 181 + 182 + /* RSCFDnCFDRFCCx */ 183 + #define RCANFD_RFCC_RFIM BIT(12) 184 + #define RCANFD_RFCC_RFDC(x) (((x) & 0x7) << 8) 185 + #define RCANFD_RFCC_RFPLS(x) (((x) & 0x7) << 4) 186 + #define RCANFD_RFCC_RFIE BIT(1) 187 + #define RCANFD_RFCC_RFE BIT(0) 188 + 189 + /* RSCFDnCFDRFSTSx */ 190 + #define RCANFD_RFSTS_RFIF BIT(3) 191 + #define RCANFD_RFSTS_RFMLT BIT(2) 192 + #define RCANFD_RFSTS_RFFLL BIT(1) 193 + #define RCANFD_RFSTS_RFEMP BIT(0) 194 + 195 + /* RSCFDnCFDRFIDx */ 196 + #define RCANFD_RFID_RFIDE BIT(31) 197 + #define RCANFD_RFID_RFRTR BIT(30) 198 + 199 + /* RSCFDnCFDRFPTRx */ 200 + #define RCANFD_RFPTR_RFDLC(x) (((x) >> 28) & 0xf) 201 + #define RCANFD_RFPTR_RFPTR(x) (((x) >> 16) & 0xfff) 202 + #define RCANFD_RFPTR_RFTS(x) (((x) >> 0) & 0xffff) 203 + 204 + /* RSCFDnCFDRFFDSTSx */ 205 + #define RCANFD_RFFDSTS_RFFDF BIT(2) 206 + #define RCANFD_RFFDSTS_RFBRS BIT(1) 207 + #define RCANFD_RFFDSTS_RFESI BIT(0) 208 + 209 + /* Common FIFO bits */ 210 + 211 + /* RSCFDnCFDCFCCk */ 212 + #define RCANFD_CFCC_CFTML(x) (((x) & 0xf) << 20) 213 + #define RCANFD_CFCC_CFM(x) (((x) & 0x3) << 16) 214 + #define RCANFD_CFCC_CFIM BIT(12) 215 + #define RCANFD_CFCC_CFDC(x) (((x) & 0x7) << 8) 216 + #define RCANFD_CFCC_CFPLS(x) (((x) & 0x7) << 4) 217 + #define RCANFD_CFCC_CFTXIE BIT(2) 218 + #define RCANFD_CFCC_CFE BIT(0) 219 + 220 + /* RSCFDnCFDCFSTSk */ 221 + #define RCANFD_CFSTS_CFMC(x) (((x) >> 8) & 0xff) 222 + #define RCANFD_CFSTS_CFTXIF BIT(4) 223 + #define RCANFD_CFSTS_CFMLT BIT(2) 224 + #define RCANFD_CFSTS_CFFLL BIT(1) 225 + #define RCANFD_CFSTS_CFEMP BIT(0) 226 + 227 + /* RSCFDnCFDCFIDk */ 228 + #define RCANFD_CFID_CFIDE BIT(31) 229 + #define RCANFD_CFID_CFRTR BIT(30) 230 + #define RCANFD_CFID_CFID_MASK(x) ((x) & 0x1fffffff) 231 + 232 + /* RSCFDnCFDCFPTRk */ 233 + #define RCANFD_CFPTR_CFDLC(x) (((x) & 0xf) << 28) 234 + #define RCANFD_CFPTR_CFPTR(x) (((x) & 0xfff) << 16) 235 + #define RCANFD_CFPTR_CFTS(x) (((x) & 0xff) << 0) 236 + 237 + /* RSCFDnCFDCFFDCSTSk */ 238 + #define RCANFD_CFFDCSTS_CFFDF BIT(2) 239 + #define RCANFD_CFFDCSTS_CFBRS BIT(1) 240 + #define RCANFD_CFFDCSTS_CFESI BIT(0) 241 + 242 + /* This controller supports classical CAN only mode or CAN FD only mode. These 243 + * modes are supported in two separate set of register maps & names. However, 244 + * some of the register offsets are common for both modes. Those offsets are 245 + * listed below as Common registers. 246 + * 247 + * The CAN FD only specific registers are listed separately and their names 248 + * starts with RCANFD_F_xxx names. When classical CAN only specific registers 249 + * are added, those specific registers can be prefixed as RCANFD_C_xxx. 250 + */ 251 + 252 + /* Common registers */ 253 + 254 + /* RSCFDnCFDCmNCFG / RSCFDnCmCFG */ 255 + #define RCANFD_CCFG(m) (0x0000 + (0x10 * (m))) 256 + /* RSCFDnCFDCmCTR / RSCFDnCmCTR */ 257 + #define RCANFD_CCTR(m) (0x0004 + (0x10 * (m))) 258 + /* RSCFDnCFDCmSTS / RSCFDnCmSTS */ 259 + #define RCANFD_CSTS(m) (0x0008 + (0x10 * (m))) 260 + /* RSCFDnCFDCmERFL / RSCFDnCmERFL */ 261 + #define RCANFD_CERFL(m) (0x000C + (0x10 * (m))) 262 + 263 + /* RSCFDnCFDGCFG / RSCFDnGCFG */ 264 + #define RCANFD_GCFG (0x0084) 265 + /* RSCFDnCFDGCTR / RSCFDnGCTR */ 266 + #define RCANFD_GCTR (0x0088) 267 + /* RSCFDnCFDGCTS / RSCFDnGCTS */ 268 + #define RCANFD_GSTS (0x008c) 269 + /* RSCFDnCFDGERFL / RSCFDnGERFL */ 270 + #define RCANFD_GERFL (0x0090) 271 + /* RSCFDnCFDGTSC / RSCFDnGTSC */ 272 + #define RCANFD_GTSC (0x0094) 273 + /* RSCFDnCFDGAFLECTR / RSCFDnGAFLECTR */ 274 + #define RCANFD_GAFLECTR (0x0098) 275 + /* RSCFDnCFDGAFLCFG0 / RSCFDnGAFLCFG0 */ 276 + #define RCANFD_GAFLCFG0 (0x009c) 277 + /* RSCFDnCFDGAFLCFG1 / RSCFDnGAFLCFG1 */ 278 + #define RCANFD_GAFLCFG1 (0x00a0) 279 + /* RSCFDnCFDRMNB / RSCFDnRMNB */ 280 + #define RCANFD_RMNB (0x00a4) 281 + /* RSCFDnCFDRMND / RSCFDnRMND */ 282 + #define RCANFD_RMND(y) (0x00a8 + (0x04 * (y))) 283 + 284 + /* RSCFDnCFDRFCCx / RSCFDnRFCCx */ 285 + #define RCANFD_RFCC(x) (0x00b8 + (0x04 * (x))) 286 + /* RSCFDnCFDRFSTSx / RSCFDnRFSTSx */ 287 + #define RCANFD_RFSTS(x) (0x00d8 + (0x04 * (x))) 288 + /* RSCFDnCFDRFPCTRx / RSCFDnRFPCTRx */ 289 + #define RCANFD_RFPCTR(x) (0x00f8 + (0x04 * (x))) 290 + 291 + /* Common FIFO Control registers */ 292 + 293 + /* RSCFDnCFDCFCCx / RSCFDnCFCCx */ 294 + #define RCANFD_CFCC(ch, idx) (0x0118 + (0x0c * (ch)) + \ 295 + (0x04 * (idx))) 296 + /* RSCFDnCFDCFSTSx / RSCFDnCFSTSx */ 297 + #define RCANFD_CFSTS(ch, idx) (0x0178 + (0x0c * (ch)) + \ 298 + (0x04 * (idx))) 299 + /* RSCFDnCFDCFPCTRx / RSCFDnCFPCTRx */ 300 + #define RCANFD_CFPCTR(ch, idx) (0x01d8 + (0x0c * (ch)) + \ 301 + (0x04 * (idx))) 302 + 303 + /* RSCFDnCFDFESTS / RSCFDnFESTS */ 304 + #define RCANFD_FESTS (0x0238) 305 + /* RSCFDnCFDFFSTS / RSCFDnFFSTS */ 306 + #define RCANFD_FFSTS (0x023c) 307 + /* RSCFDnCFDFMSTS / RSCFDnFMSTS */ 308 + #define RCANFD_FMSTS (0x0240) 309 + /* RSCFDnCFDRFISTS / RSCFDnRFISTS */ 310 + #define RCANFD_RFISTS (0x0244) 311 + /* RSCFDnCFDCFRISTS / RSCFDnCFRISTS */ 312 + #define RCANFD_CFRISTS (0x0248) 313 + /* RSCFDnCFDCFTISTS / RSCFDnCFTISTS */ 314 + #define RCANFD_CFTISTS (0x024c) 315 + 316 + /* RSCFDnCFDTMCp / RSCFDnTMCp */ 317 + #define RCANFD_TMC(p) (0x0250 + (0x01 * (p))) 318 + /* RSCFDnCFDTMSTSp / RSCFDnTMSTSp */ 319 + #define RCANFD_TMSTS(p) (0x02d0 + (0x01 * (p))) 320 + 321 + /* RSCFDnCFDTMTRSTSp / RSCFDnTMTRSTSp */ 322 + #define RCANFD_TMTRSTS(y) (0x0350 + (0x04 * (y))) 323 + /* RSCFDnCFDTMTARSTSp / RSCFDnTMTARSTSp */ 324 + #define RCANFD_TMTARSTS(y) (0x0360 + (0x04 * (y))) 325 + /* RSCFDnCFDTMTCSTSp / RSCFDnTMTCSTSp */ 326 + #define RCANFD_TMTCSTS(y) (0x0370 + (0x04 * (y))) 327 + /* RSCFDnCFDTMTASTSp / RSCFDnTMTASTSp */ 328 + #define RCANFD_TMTASTS(y) (0x0380 + (0x04 * (y))) 329 + /* RSCFDnCFDTMIECy / RSCFDnTMIECy */ 330 + #define RCANFD_TMIEC(y) (0x0390 + (0x04 * (y))) 331 + 332 + /* RSCFDnCFDTXQCCm / RSCFDnTXQCCm */ 333 + #define RCANFD_TXQCC(m) (0x03a0 + (0x04 * (m))) 334 + /* RSCFDnCFDTXQSTSm / RSCFDnTXQSTSm */ 335 + #define RCANFD_TXQSTS(m) (0x03c0 + (0x04 * (m))) 336 + /* RSCFDnCFDTXQPCTRm / RSCFDnTXQPCTRm */ 337 + #define RCANFD_TXQPCTR(m) (0x03e0 + (0x04 * (m))) 338 + 339 + /* RSCFDnCFDTHLCCm / RSCFDnTHLCCm */ 340 + #define RCANFD_THLCC(m) (0x0400 + (0x04 * (m))) 341 + /* RSCFDnCFDTHLSTSm / RSCFDnTHLSTSm */ 342 + #define RCANFD_THLSTS(m) (0x0420 + (0x04 * (m))) 343 + /* RSCFDnCFDTHLPCTRm / RSCFDnTHLPCTRm */ 344 + #define RCANFD_THLPCTR(m) (0x0440 + (0x04 * (m))) 345 + 346 + /* RSCFDnCFDGTINTSTS0 / RSCFDnGTINTSTS0 */ 347 + #define RCANFD_GTINTSTS0 (0x0460) 348 + /* RSCFDnCFDGTINTSTS1 / RSCFDnGTINTSTS1 */ 349 + #define RCANFD_GTINTSTS1 (0x0464) 350 + /* RSCFDnCFDGTSTCFG / RSCFDnGTSTCFG */ 351 + #define RCANFD_GTSTCFG (0x0468) 352 + /* RSCFDnCFDGTSTCTR / RSCFDnGTSTCTR */ 353 + #define RCANFD_GTSTCTR (0x046c) 354 + /* RSCFDnCFDGLOCKK / RSCFDnGLOCKK */ 355 + #define RCANFD_GLOCKK (0x047c) 356 + /* RSCFDnCFDGRMCFG / RSCFDnGRMCFG */ 357 + #define RCANFD_GRMCFG (0x04fc) 358 + 359 + /* RSCFDnCFDGAFLIDj / RSCFDnGAFLIDj */ 360 + #define RCANFD_GAFLID(offset, j) ((offset) + (0x10 * (j))) 361 + /* RSCFDnCFDGAFLMj / RSCFDnGAFLMj */ 362 + #define RCANFD_GAFLM(offset, j) ((offset) + 0x04 + (0x10 * (j))) 363 + /* RSCFDnCFDGAFLP0j / RSCFDnGAFLP0j */ 364 + #define RCANFD_GAFLP0(offset, j) ((offset) + 0x08 + (0x10 * (j))) 365 + /* RSCFDnCFDGAFLP1j / RSCFDnGAFLP1j */ 366 + #define RCANFD_GAFLP1(offset, j) ((offset) + 0x0c + (0x10 * (j))) 367 + 368 + /* CAN FD mode specific regsiter map */ 369 + 370 + /* RSCFDnCFDCmXXX -> RCANFD_F_XXX(m) */ 371 + #define RCANFD_F_DCFG(m) (0x0500 + (0x20 * (m))) 372 + #define RCANFD_F_CFDCFG(m) (0x0504 + (0x20 * (m))) 373 + #define RCANFD_F_CFDCTR(m) (0x0508 + (0x20 * (m))) 374 + #define RCANFD_F_CFDSTS(m) (0x050c + (0x20 * (m))) 375 + #define RCANFD_F_CFDCRC(m) (0x0510 + (0x20 * (m))) 376 + 377 + /* RSCFDnCFDGAFLXXXj offset */ 378 + #define RCANFD_F_GAFL_OFFSET (0x1000) 379 + 380 + /* RSCFDnCFDRMXXXq -> RCANFD_F_RMXXX(q) */ 381 + #define RCANFD_F_RMID(q) (0x2000 + (0x20 * (q))) 382 + #define RCANFD_F_RMPTR(q) (0x2004 + (0x20 * (q))) 383 + #define RCANFD_F_RMFDSTS(q) (0x2008 + (0x20 * (q))) 384 + #define RCANFD_F_RMDF(q, b) (0x200c + (0x04 * (b)) + (0x20 * (q))) 385 + 386 + /* RSCFDnCFDRFXXx -> RCANFD_F_RFXX(x) */ 387 + #define RCANFD_F_RFOFFSET (0x3000) 388 + #define RCANFD_F_RFID(x) (RCANFD_F_RFOFFSET + (0x80 * (x))) 389 + #define RCANFD_F_RFPTR(x) (RCANFD_F_RFOFFSET + 0x04 + \ 390 + (0x80 * (x))) 391 + #define RCANFD_F_RFFDSTS(x) (RCANFD_F_RFOFFSET + 0x08 + \ 392 + (0x80 * (x))) 393 + #define RCANFD_F_RFDF(x, df) (RCANFD_F_RFOFFSET + 0x0c + \ 394 + (0x80 * (x)) + (0x04 * (df))) 395 + 396 + /* RSCFDnCFDCFXXk -> RCANFD_F_CFXX(ch, k) */ 397 + #define RCANFD_F_CFOFFSET (0x3400) 398 + #define RCANFD_F_CFID(ch, idx) (RCANFD_F_CFOFFSET + (0x180 * (ch)) + \ 399 + (0x80 * (idx))) 400 + #define RCANFD_F_CFPTR(ch, idx) (RCANFD_F_CFOFFSET + 0x04 + \ 401 + (0x180 * (ch)) + (0x80 * (idx))) 402 + #define RCANFD_F_CFFDCSTS(ch, idx) (RCANFD_F_CFOFFSET + 0x08 + \ 403 + (0x180 * (ch)) + (0x80 * (idx))) 404 + #define RCANFD_F_CFDF(ch, idx, df) (RCANFD_F_CFOFFSET + 0x0c + \ 405 + (0x180 * (ch)) + (0x80 * (idx)) + \ 406 + (0x04 * (df))) 407 + 408 + /* RSCFDnCFDTMXXp -> RCANFD_F_TMXX(p) */ 409 + #define RCANFD_F_TMID(p) (0x4000 + (0x20 * (p))) 410 + #define RCANFD_F_TMPTR(p) (0x4004 + (0x20 * (p))) 411 + #define RCANFD_F_TMFDCTR(p) (0x4008 + (0x20 * (p))) 412 + #define RCANFD_F_TMDF(p, b) (0x400c + (0x20 * (p)) + (0x04 * (b))) 413 + 414 + /* RSCFDnCFDTHLACCm */ 415 + #define RCANFD_F_THLACC(m) (0x6000 + (0x04 * (m))) 416 + /* RSCFDnCFDRPGACCr */ 417 + #define RCANFD_F_RPGACC(r) (0x6400 + (0x04 * (r))) 418 + 419 + /* Constants */ 420 + #define RCANFD_FIFO_DEPTH 8 /* Tx FIFO depth */ 421 + #define RCANFD_NAPI_WEIGHT 8 /* Rx poll quota */ 422 + 423 + #define RCANFD_NUM_CHANNELS 2 /* Two channels max */ 424 + #define RCANFD_CHANNELS_MASK BIT((RCANFD_NUM_CHANNELS) - 1) 425 + 426 + #define RCANFD_GAFL_PAGENUM(entry) ((entry) / 16) 427 + #define RCANFD_CHANNEL_NUMRULES 1 /* only one rule per channel */ 428 + 429 + /* Rx FIFO is a global resource of the controller. There are 8 such FIFOs 430 + * available. Each channel gets a dedicated Rx FIFO (i.e.) the channel 431 + * number is added to RFFIFO index. 432 + */ 433 + #define RCANFD_RFFIFO_IDX 0 434 + 435 + /* Tx/Rx or Common FIFO is a per channel resource. Each channel has 3 Common 436 + * FIFOs dedicated to them. Use the first (index 0) FIFO out of the 3 for Tx. 437 + */ 438 + #define RCANFD_CFFIFO_IDX 0 439 + 440 + /* fCAN clock select register settings */ 441 + enum rcar_canfd_fcanclk { 442 + RCANFD_CANFDCLK = 0, /* CANFD clock */ 443 + RCANFD_EXTCLK, /* Externally input clock */ 444 + }; 445 + 446 + struct rcar_canfd_global; 447 + 448 + /* Channel priv data */ 449 + struct rcar_canfd_channel { 450 + struct can_priv can; /* Must be the first member */ 451 + struct net_device *ndev; 452 + struct rcar_canfd_global *gpriv; /* Controller reference */ 453 + void __iomem *base; /* Register base address */ 454 + struct napi_struct napi; 455 + u8 tx_len[RCANFD_FIFO_DEPTH]; /* For net stats */ 456 + u32 tx_head; /* Incremented on xmit */ 457 + u32 tx_tail; /* Incremented on xmit done */ 458 + u32 channel; /* Channel number */ 459 + spinlock_t tx_lock; /* To protect tx path */ 460 + }; 461 + 462 + /* Global priv data */ 463 + struct rcar_canfd_global { 464 + struct rcar_canfd_channel *ch[RCANFD_NUM_CHANNELS]; 465 + void __iomem *base; /* Register base address */ 466 + struct platform_device *pdev; /* Respective platform device */ 467 + struct clk *clkp; /* Peripheral clock */ 468 + struct clk *can_clk; /* fCAN clock */ 469 + enum rcar_canfd_fcanclk fcan; /* CANFD or Ext clock */ 470 + unsigned long channels_mask; /* Enabled channels mask */ 471 + }; 472 + 473 + /* CAN FD mode nominal rate constants */ 474 + static const struct can_bittiming_const rcar_canfd_nom_bittiming_const = { 475 + .name = RCANFD_DRV_NAME, 476 + .tseg1_min = 2, 477 + .tseg1_max = 128, 478 + .tseg2_min = 2, 479 + .tseg2_max = 32, 480 + .sjw_max = 32, 481 + .brp_min = 1, 482 + .brp_max = 1024, 483 + .brp_inc = 1, 484 + }; 485 + 486 + /* CAN FD mode data rate constants */ 487 + static const struct can_bittiming_const rcar_canfd_data_bittiming_const = { 488 + .name = RCANFD_DRV_NAME, 489 + .tseg1_min = 2, 490 + .tseg1_max = 16, 491 + .tseg2_min = 2, 492 + .tseg2_max = 8, 493 + .sjw_max = 8, 494 + .brp_min = 1, 495 + .brp_max = 256, 496 + .brp_inc = 1, 497 + }; 498 + 499 + /* Helper functions */ 500 + static inline void rcar_canfd_update(u32 mask, u32 val, u32 __iomem *reg) 501 + { 502 + u32 data = readl(reg); 503 + 504 + data &= ~mask; 505 + data |= (val & mask); 506 + writel(data, reg); 507 + } 508 + 509 + static inline u32 rcar_canfd_read(void __iomem *base, u32 offset) 510 + { 511 + return readl(base + (offset)); 512 + } 513 + 514 + static inline void rcar_canfd_write(void __iomem *base, u32 offset, u32 val) 515 + { 516 + writel(val, base + (offset)); 517 + } 518 + 519 + static void rcar_canfd_set_bit(void __iomem *base, u32 reg, u32 val) 520 + { 521 + rcar_canfd_update(val, val, base + (reg)); 522 + } 523 + 524 + static void rcar_canfd_clear_bit(void __iomem *base, u32 reg, u32 val) 525 + { 526 + rcar_canfd_update(val, 0, base + (reg)); 527 + } 528 + 529 + static void rcar_canfd_update_bit(void __iomem *base, u32 reg, 530 + u32 mask, u32 val) 531 + { 532 + rcar_canfd_update(mask, val, base + (reg)); 533 + } 534 + 535 + static void rcar_canfd_get_data(struct rcar_canfd_channel *priv, 536 + struct canfd_frame *cf, u32 off) 537 + { 538 + u32 i, lwords; 539 + 540 + lwords = DIV_ROUND_UP(cf->len, sizeof(u32)); 541 + for (i = 0; i < lwords; i++) 542 + *((u32 *)cf->data + i) = 543 + rcar_canfd_read(priv->base, off + (i * sizeof(u32))); 544 + } 545 + 546 + static void rcar_canfd_put_data(struct rcar_canfd_channel *priv, 547 + struct canfd_frame *cf, u32 off) 548 + { 549 + u32 i, lwords; 550 + 551 + lwords = DIV_ROUND_UP(cf->len, sizeof(u32)); 552 + for (i = 0; i < lwords; i++) 553 + rcar_canfd_write(priv->base, off + (i * sizeof(u32)), 554 + *((u32 *)cf->data + i)); 555 + } 556 + 557 + static void rcar_canfd_tx_failure_cleanup(struct net_device *ndev) 558 + { 559 + u32 i; 560 + 561 + for (i = 0; i < RCANFD_FIFO_DEPTH; i++) 562 + can_free_echo_skb(ndev, i); 563 + } 564 + 565 + static int rcar_canfd_reset_controller(struct rcar_canfd_global *gpriv) 566 + { 567 + u32 sts, ch; 568 + int err; 569 + 570 + /* Check RAMINIT flag as CAN RAM initialization takes place 571 + * after the MCU reset 572 + */ 573 + err = readl_poll_timeout((gpriv->base + RCANFD_GSTS), sts, 574 + !(sts & RCANFD_GSTS_GRAMINIT), 2, 500000); 575 + if (err) { 576 + dev_dbg(&gpriv->pdev->dev, "global raminit failed\n"); 577 + return err; 578 + } 579 + 580 + /* Transition to Global Reset mode */ 581 + rcar_canfd_clear_bit(gpriv->base, RCANFD_GCTR, RCANFD_GCTR_GSLPR); 582 + rcar_canfd_update_bit(gpriv->base, RCANFD_GCTR, 583 + RCANFD_GCTR_GMDC_MASK, RCANFD_GCTR_GMDC_GRESET); 584 + 585 + /* Ensure Global reset mode */ 586 + err = readl_poll_timeout((gpriv->base + RCANFD_GSTS), sts, 587 + (sts & RCANFD_GSTS_GRSTSTS), 2, 500000); 588 + if (err) { 589 + dev_dbg(&gpriv->pdev->dev, "global reset failed\n"); 590 + return err; 591 + } 592 + 593 + /* Reset Global error flags */ 594 + rcar_canfd_write(gpriv->base, RCANFD_GERFL, 0x0); 595 + 596 + /* Set the controller into FD mode */ 597 + rcar_canfd_set_bit(gpriv->base, RCANFD_GRMCFG, RCANFD_GRMCFG_RCMC); 598 + 599 + /* Transition all Channels to reset mode */ 600 + for_each_set_bit(ch, &gpriv->channels_mask, RCANFD_NUM_CHANNELS) { 601 + rcar_canfd_clear_bit(gpriv->base, 602 + RCANFD_CCTR(ch), RCANFD_CCTR_CSLPR); 603 + 604 + rcar_canfd_update_bit(gpriv->base, RCANFD_CCTR(ch), 605 + RCANFD_CCTR_CHMDC_MASK, 606 + RCANFD_CCTR_CHDMC_CRESET); 607 + 608 + /* Ensure Channel reset mode */ 609 + err = readl_poll_timeout((gpriv->base + RCANFD_CSTS(ch)), sts, 610 + (sts & RCANFD_CSTS_CRSTSTS), 611 + 2, 500000); 612 + if (err) { 613 + dev_dbg(&gpriv->pdev->dev, 614 + "channel %u reset failed\n", ch); 615 + return err; 616 + } 617 + } 618 + return 0; 619 + } 620 + 621 + static void rcar_canfd_configure_controller(struct rcar_canfd_global *gpriv) 622 + { 623 + u32 cfg, ch; 624 + 625 + /* Global configuration settings */ 626 + 627 + /* Truncate payload to configured message size RFPLS */ 628 + cfg = RCANFD_GCFG_CMPOC; 629 + 630 + /* Set External Clock if selected */ 631 + if (gpriv->fcan != RCANFD_CANFDCLK) 632 + cfg |= RCANFD_GCFG_DCS; 633 + 634 + rcar_canfd_set_bit(gpriv->base, RCANFD_GCFG, cfg); 635 + 636 + /* Channel configuration settings */ 637 + for_each_set_bit(ch, &gpriv->channels_mask, RCANFD_NUM_CHANNELS) { 638 + rcar_canfd_set_bit(gpriv->base, RCANFD_CCTR(ch), 639 + RCANFD_CCTR_ERRD); 640 + rcar_canfd_update_bit(gpriv->base, RCANFD_CCTR(ch), 641 + RCANFD_CCTR_BOM_MASK, 642 + RCANFD_CCTR_BOM_BENTRY); 643 + } 644 + } 645 + 646 + static void rcar_canfd_configure_afl_rules(struct rcar_canfd_global *gpriv, 647 + u32 ch) 648 + { 649 + u32 cfg; 650 + int start, page, num_rules = RCANFD_CHANNEL_NUMRULES; 651 + u32 ridx = ch + RCANFD_RFFIFO_IDX; 652 + 653 + if (ch == 0) { 654 + start = 0; /* Channel 0 always starts from 0th rule */ 655 + } else { 656 + /* Get number of Channel 0 rules and adjust */ 657 + cfg = rcar_canfd_read(gpriv->base, RCANFD_GAFLCFG0); 658 + start = RCANFD_GAFLCFG_GETRNC(0, cfg); 659 + } 660 + 661 + /* Enable write access to entry */ 662 + page = RCANFD_GAFL_PAGENUM(start); 663 + rcar_canfd_set_bit(gpriv->base, RCANFD_GAFLECTR, 664 + (RCANFD_GAFLECTR_AFLPN(page) | 665 + RCANFD_GAFLECTR_AFLDAE)); 666 + 667 + /* Write number of rules for channel */ 668 + rcar_canfd_set_bit(gpriv->base, RCANFD_GAFLCFG0, 669 + RCANFD_GAFLCFG_SETRNC(ch, num_rules)); 670 + 671 + /* Accept all IDs */ 672 + rcar_canfd_write(gpriv->base, 673 + RCANFD_GAFLID(RCANFD_F_GAFL_OFFSET, start), 0); 674 + /* IDE or RTR is not considered for matching */ 675 + rcar_canfd_write(gpriv->base, 676 + RCANFD_GAFLM(RCANFD_F_GAFL_OFFSET, start), 0); 677 + /* Any data length accepted */ 678 + rcar_canfd_write(gpriv->base, 679 + RCANFD_GAFLP0(RCANFD_F_GAFL_OFFSET, start), 0); 680 + /* Place the msg in corresponding Rx FIFO entry */ 681 + rcar_canfd_write(gpriv->base, 682 + RCANFD_GAFLP1(RCANFD_F_GAFL_OFFSET, start), 683 + RCANFD_GAFLP1_GAFLFDP(ridx)); 684 + 685 + /* Disable write access to page */ 686 + rcar_canfd_clear_bit(gpriv->base, 687 + RCANFD_GAFLECTR, RCANFD_GAFLECTR_AFLDAE); 688 + } 689 + 690 + static void rcar_canfd_configure_rx(struct rcar_canfd_global *gpriv, u32 ch) 691 + { 692 + /* Rx FIFO is used for reception */ 693 + u32 cfg; 694 + u16 rfdc, rfpls; 695 + 696 + /* Select Rx FIFO based on channel */ 697 + u32 ridx = ch + RCANFD_RFFIFO_IDX; 698 + 699 + rfdc = 2; /* b010 - 8 messages Rx FIFO depth */ 700 + rfpls = 7; /* b111 - Max 64 bytes payload */ 701 + 702 + cfg = (RCANFD_RFCC_RFIM | RCANFD_RFCC_RFDC(rfdc) | 703 + RCANFD_RFCC_RFPLS(rfpls) | RCANFD_RFCC_RFIE); 704 + rcar_canfd_write(gpriv->base, RCANFD_RFCC(ridx), cfg); 705 + } 706 + 707 + static void rcar_canfd_configure_tx(struct rcar_canfd_global *gpriv, u32 ch) 708 + { 709 + /* Tx/Rx(Common) FIFO configured in Tx mode is 710 + * used for transmission 711 + * 712 + * Each channel has 3 Common FIFO dedicated to them. 713 + * Use the 1st (index 0) out of 3 714 + */ 715 + u32 cfg; 716 + u16 cftml, cfm, cfdc, cfpls; 717 + 718 + cftml = 0; /* 0th buffer */ 719 + cfm = 1; /* b01 - Transmit mode */ 720 + cfdc = 2; /* b010 - 8 messages Tx FIFO depth */ 721 + cfpls = 7; /* b111 - Max 64 bytes payload */ 722 + 723 + cfg = (RCANFD_CFCC_CFTML(cftml) | RCANFD_CFCC_CFM(cfm) | 724 + RCANFD_CFCC_CFIM | RCANFD_CFCC_CFDC(cfdc) | 725 + RCANFD_CFCC_CFPLS(cfpls) | RCANFD_CFCC_CFTXIE); 726 + rcar_canfd_write(gpriv->base, RCANFD_CFCC(ch, RCANFD_CFFIFO_IDX), cfg); 727 + 728 + /* Clear FD mode specific control/status register */ 729 + rcar_canfd_write(gpriv->base, 730 + RCANFD_F_CFFDCSTS(ch, RCANFD_CFFIFO_IDX), 0); 731 + } 732 + 733 + static void rcar_canfd_enable_global_interrupts(struct rcar_canfd_global *gpriv) 734 + { 735 + u32 ctr; 736 + 737 + /* Clear any stray error interrupt flags */ 738 + rcar_canfd_write(gpriv->base, RCANFD_GERFL, 0); 739 + 740 + /* Global interrupts setup */ 741 + ctr = RCANFD_GCTR_MEIE; 742 + ctr |= RCANFD_GCTR_CFMPOFIE; 743 + 744 + rcar_canfd_set_bit(gpriv->base, RCANFD_GCTR, ctr); 745 + } 746 + 747 + static void rcar_canfd_disable_global_interrupts(struct rcar_canfd_global 748 + *gpriv) 749 + { 750 + /* Disable all interrupts */ 751 + rcar_canfd_write(gpriv->base, RCANFD_GCTR, 0); 752 + 753 + /* Clear any stray error interrupt flags */ 754 + rcar_canfd_write(gpriv->base, RCANFD_GERFL, 0); 755 + } 756 + 757 + static void rcar_canfd_enable_channel_interrupts(struct rcar_canfd_channel 758 + *priv) 759 + { 760 + u32 ctr, ch = priv->channel; 761 + 762 + /* Clear any stray error flags */ 763 + rcar_canfd_write(priv->base, RCANFD_CERFL(ch), 0); 764 + 765 + /* Channel interrupts setup */ 766 + ctr = (RCANFD_CCTR_TAIE | 767 + RCANFD_CCTR_ALIE | RCANFD_CCTR_BLIE | 768 + RCANFD_CCTR_OLIE | RCANFD_CCTR_BORIE | 769 + RCANFD_CCTR_BOEIE | RCANFD_CCTR_EPIE | 770 + RCANFD_CCTR_EWIE | RCANFD_CCTR_BEIE); 771 + rcar_canfd_set_bit(priv->base, RCANFD_CCTR(ch), ctr); 772 + } 773 + 774 + static void rcar_canfd_disable_channel_interrupts(struct rcar_canfd_channel 775 + *priv) 776 + { 777 + u32 ctr, ch = priv->channel; 778 + 779 + ctr = (RCANFD_CCTR_TAIE | 780 + RCANFD_CCTR_ALIE | RCANFD_CCTR_BLIE | 781 + RCANFD_CCTR_OLIE | RCANFD_CCTR_BORIE | 782 + RCANFD_CCTR_BOEIE | RCANFD_CCTR_EPIE | 783 + RCANFD_CCTR_EWIE | RCANFD_CCTR_BEIE); 784 + rcar_canfd_clear_bit(priv->base, RCANFD_CCTR(ch), ctr); 785 + 786 + /* Clear any stray error flags */ 787 + rcar_canfd_write(priv->base, RCANFD_CERFL(ch), 0); 788 + } 789 + 790 + static void rcar_canfd_global_error(struct net_device *ndev) 791 + { 792 + struct rcar_canfd_channel *priv = netdev_priv(ndev); 793 + struct net_device_stats *stats = &ndev->stats; 794 + u32 ch = priv->channel; 795 + u32 gerfl, sts; 796 + u32 ridx = ch + RCANFD_RFFIFO_IDX; 797 + 798 + gerfl = rcar_canfd_read(priv->base, RCANFD_GERFL); 799 + if ((gerfl & RCANFD_GERFL_EEF0) && (ch == 0)) { 800 + netdev_dbg(ndev, "Ch0: ECC Error flag\n"); 801 + stats->tx_dropped++; 802 + } 803 + if ((gerfl & RCANFD_GERFL_EEF1) && (ch == 1)) { 804 + netdev_dbg(ndev, "Ch1: ECC Error flag\n"); 805 + stats->tx_dropped++; 806 + } 807 + if (gerfl & RCANFD_GERFL_MES) { 808 + sts = rcar_canfd_read(priv->base, 809 + RCANFD_CFSTS(ch, RCANFD_CFFIFO_IDX)); 810 + if (sts & RCANFD_CFSTS_CFMLT) { 811 + netdev_dbg(ndev, "Tx Message Lost flag\n"); 812 + stats->tx_dropped++; 813 + rcar_canfd_write(priv->base, 814 + RCANFD_CFSTS(ch, RCANFD_CFFIFO_IDX), 815 + sts & ~RCANFD_CFSTS_CFMLT); 816 + } 817 + 818 + sts = rcar_canfd_read(priv->base, RCANFD_RFSTS(ridx)); 819 + if (sts & RCANFD_RFSTS_RFMLT) { 820 + netdev_dbg(ndev, "Rx Message Lost flag\n"); 821 + stats->rx_dropped++; 822 + rcar_canfd_write(priv->base, RCANFD_RFSTS(ridx), 823 + sts & ~RCANFD_RFSTS_RFMLT); 824 + } 825 + } 826 + if (gerfl & RCANFD_GERFL_CMPOF) { 827 + /* Message Lost flag will be set for respective channel 828 + * when this condition happens with counters and flags 829 + * already updated. 830 + */ 831 + netdev_dbg(ndev, "global payload overflow interrupt\n"); 832 + } 833 + 834 + /* Clear all global error interrupts. Only affected channels bits 835 + * get cleared 836 + */ 837 + rcar_canfd_write(priv->base, RCANFD_GERFL, 0); 838 + } 839 + 840 + static void rcar_canfd_error(struct net_device *ndev) 841 + { 842 + struct rcar_canfd_channel *priv = netdev_priv(ndev); 843 + struct net_device_stats *stats = &ndev->stats; 844 + struct can_frame *cf; 845 + struct sk_buff *skb; 846 + u32 cerfl, csts; 847 + u32 txerr = 0, rxerr = 0; 848 + u32 ch = priv->channel; 849 + 850 + /* Propagate the error condition to the CAN stack */ 851 + skb = alloc_can_err_skb(ndev, &cf); 852 + if (!skb) { 853 + stats->rx_dropped++; 854 + return; 855 + } 856 + 857 + /* Channel error interrupt */ 858 + cerfl = rcar_canfd_read(priv->base, RCANFD_CERFL(ch)); 859 + csts = rcar_canfd_read(priv->base, RCANFD_CSTS(ch)); 860 + txerr = RCANFD_CSTS_TECCNT(csts); 861 + rxerr = RCANFD_CSTS_RECCNT(csts); 862 + 863 + netdev_dbg(ndev, "ch erfl %x sts %x txerr %u rxerr %u\n", 864 + cerfl, csts, txerr, rxerr); 865 + 866 + if (cerfl & RCANFD_CERFL_BEF) { 867 + netdev_dbg(ndev, "Bus error\n"); 868 + cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT; 869 + cf->data[2] = CAN_ERR_PROT_UNSPEC; 870 + priv->can.can_stats.bus_error++; 871 + } 872 + if (cerfl & RCANFD_CERFL_ADERR) { 873 + netdev_dbg(ndev, "ACK Delimiter Error\n"); 874 + stats->tx_errors++; 875 + cf->data[3] |= CAN_ERR_PROT_LOC_ACK_DEL; 876 + } 877 + if (cerfl & RCANFD_CERFL_B0ERR) { 878 + netdev_dbg(ndev, "Bit Error (dominant)\n"); 879 + stats->tx_errors++; 880 + cf->data[2] |= CAN_ERR_PROT_BIT0; 881 + } 882 + if (cerfl & RCANFD_CERFL_B1ERR) { 883 + netdev_dbg(ndev, "Bit Error (recessive)\n"); 884 + stats->tx_errors++; 885 + cf->data[2] |= CAN_ERR_PROT_BIT1; 886 + } 887 + if (cerfl & RCANFD_CERFL_CERR) { 888 + netdev_dbg(ndev, "CRC Error\n"); 889 + stats->rx_errors++; 890 + cf->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ; 891 + } 892 + if (cerfl & RCANFD_CERFL_AERR) { 893 + netdev_dbg(ndev, "ACK Error\n"); 894 + stats->tx_errors++; 895 + cf->can_id |= CAN_ERR_ACK; 896 + cf->data[3] |= CAN_ERR_PROT_LOC_ACK; 897 + } 898 + if (cerfl & RCANFD_CERFL_FERR) { 899 + netdev_dbg(ndev, "Form Error\n"); 900 + stats->rx_errors++; 901 + cf->data[2] |= CAN_ERR_PROT_FORM; 902 + } 903 + if (cerfl & RCANFD_CERFL_SERR) { 904 + netdev_dbg(ndev, "Stuff Error\n"); 905 + stats->rx_errors++; 906 + cf->data[2] |= CAN_ERR_PROT_STUFF; 907 + } 908 + if (cerfl & RCANFD_CERFL_ALF) { 909 + netdev_dbg(ndev, "Arbitration lost Error\n"); 910 + priv->can.can_stats.arbitration_lost++; 911 + cf->can_id |= CAN_ERR_LOSTARB; 912 + cf->data[0] |= CAN_ERR_LOSTARB_UNSPEC; 913 + } 914 + if (cerfl & RCANFD_CERFL_BLF) { 915 + netdev_dbg(ndev, "Bus Lock Error\n"); 916 + stats->rx_errors++; 917 + cf->can_id |= CAN_ERR_BUSERROR; 918 + } 919 + if (cerfl & RCANFD_CERFL_EWF) { 920 + netdev_dbg(ndev, "Error warning interrupt\n"); 921 + priv->can.state = CAN_STATE_ERROR_WARNING; 922 + priv->can.can_stats.error_warning++; 923 + cf->can_id |= CAN_ERR_CRTL; 924 + cf->data[1] = txerr > rxerr ? CAN_ERR_CRTL_TX_WARNING : 925 + CAN_ERR_CRTL_RX_WARNING; 926 + cf->data[6] = txerr; 927 + cf->data[7] = rxerr; 928 + } 929 + if (cerfl & RCANFD_CERFL_EPF) { 930 + netdev_dbg(ndev, "Error passive interrupt\n"); 931 + priv->can.state = CAN_STATE_ERROR_PASSIVE; 932 + priv->can.can_stats.error_passive++; 933 + cf->can_id |= CAN_ERR_CRTL; 934 + cf->data[1] = txerr > rxerr ? CAN_ERR_CRTL_TX_PASSIVE : 935 + CAN_ERR_CRTL_RX_PASSIVE; 936 + cf->data[6] = txerr; 937 + cf->data[7] = rxerr; 938 + } 939 + if (cerfl & RCANFD_CERFL_BOEF) { 940 + netdev_dbg(ndev, "Bus-off entry interrupt\n"); 941 + rcar_canfd_tx_failure_cleanup(ndev); 942 + priv->can.state = CAN_STATE_BUS_OFF; 943 + priv->can.can_stats.bus_off++; 944 + can_bus_off(ndev); 945 + cf->can_id |= CAN_ERR_BUSOFF; 946 + } 947 + if (cerfl & RCANFD_CERFL_OVLF) { 948 + netdev_dbg(ndev, 949 + "Overload Frame Transmission error interrupt\n"); 950 + stats->tx_errors++; 951 + cf->can_id |= CAN_ERR_PROT; 952 + cf->data[2] |= CAN_ERR_PROT_OVERLOAD; 953 + } 954 + 955 + /* Clear all channel error interrupts */ 956 + rcar_canfd_write(priv->base, RCANFD_CERFL(ch), 0); 957 + stats->rx_packets++; 958 + stats->rx_bytes += cf->can_dlc; 959 + netif_rx(skb); 960 + } 961 + 962 + static void rcar_canfd_tx_done(struct net_device *ndev) 963 + { 964 + struct rcar_canfd_channel *priv = netdev_priv(ndev); 965 + struct net_device_stats *stats = &ndev->stats; 966 + u32 sts; 967 + unsigned long flags; 968 + u32 ch = priv->channel; 969 + 970 + do { 971 + u8 unsent, sent; 972 + 973 + sent = priv->tx_tail % RCANFD_FIFO_DEPTH; 974 + stats->tx_packets++; 975 + stats->tx_bytes += priv->tx_len[sent]; 976 + priv->tx_len[sent] = 0; 977 + can_get_echo_skb(ndev, sent); 978 + 979 + spin_lock_irqsave(&priv->tx_lock, flags); 980 + priv->tx_tail++; 981 + sts = rcar_canfd_read(priv->base, 982 + RCANFD_CFSTS(ch, RCANFD_CFFIFO_IDX)); 983 + unsent = RCANFD_CFSTS_CFMC(sts); 984 + 985 + /* Wake producer only when there is room */ 986 + if (unsent != RCANFD_FIFO_DEPTH) 987 + netif_wake_queue(ndev); 988 + 989 + if (priv->tx_head - priv->tx_tail <= unsent) { 990 + spin_unlock_irqrestore(&priv->tx_lock, flags); 991 + break; 992 + } 993 + spin_unlock_irqrestore(&priv->tx_lock, flags); 994 + 995 + } while (1); 996 + 997 + /* Clear interrupt */ 998 + rcar_canfd_write(priv->base, RCANFD_CFSTS(ch, RCANFD_CFFIFO_IDX), 999 + sts & ~RCANFD_CFSTS_CFTXIF); 1000 + can_led_event(ndev, CAN_LED_EVENT_TX); 1001 + } 1002 + 1003 + static irqreturn_t rcar_canfd_global_interrupt(int irq, void *dev_id) 1004 + { 1005 + struct rcar_canfd_global *gpriv = dev_id; 1006 + struct net_device *ndev; 1007 + struct rcar_canfd_channel *priv; 1008 + u32 sts, gerfl; 1009 + u32 ch, ridx; 1010 + 1011 + /* Global error interrupts still indicate a condition specific 1012 + * to a channel. RxFIFO interrupt is a global interrupt. 1013 + */ 1014 + for_each_set_bit(ch, &gpriv->channels_mask, RCANFD_NUM_CHANNELS) { 1015 + priv = gpriv->ch[ch]; 1016 + ndev = priv->ndev; 1017 + ridx = ch + RCANFD_RFFIFO_IDX; 1018 + 1019 + /* Global error interrupts */ 1020 + gerfl = rcar_canfd_read(priv->base, RCANFD_GERFL); 1021 + if (RCANFD_GERFL_ERR(gerfl)) 1022 + rcar_canfd_global_error(ndev); 1023 + 1024 + /* Handle Rx interrupts */ 1025 + sts = rcar_canfd_read(priv->base, RCANFD_RFSTS(ridx)); 1026 + if (sts & RCANFD_RFSTS_RFIF) { 1027 + if (napi_schedule_prep(&priv->napi)) { 1028 + /* Disable Rx FIFO interrupts */ 1029 + rcar_canfd_clear_bit(priv->base, 1030 + RCANFD_RFCC(ridx), 1031 + RCANFD_RFCC_RFIE); 1032 + __napi_schedule(&priv->napi); 1033 + } 1034 + } 1035 + } 1036 + return IRQ_HANDLED; 1037 + } 1038 + 1039 + static irqreturn_t rcar_canfd_channel_interrupt(int irq, void *dev_id) 1040 + { 1041 + struct rcar_canfd_global *gpriv = dev_id; 1042 + struct net_device *ndev; 1043 + struct rcar_canfd_channel *priv; 1044 + u32 sts, cerfl, ch; 1045 + 1046 + /* Common FIFO is a per channel resource */ 1047 + for_each_set_bit(ch, &gpriv->channels_mask, RCANFD_NUM_CHANNELS) { 1048 + priv = gpriv->ch[ch]; 1049 + ndev = priv->ndev; 1050 + 1051 + /* Channel error interrupts */ 1052 + cerfl = rcar_canfd_read(priv->base, RCANFD_CERFL(ch)); 1053 + if (RCANFD_CERFL_ERR(cerfl)) 1054 + rcar_canfd_error(ndev); 1055 + 1056 + /* Handle Tx interrupts */ 1057 + sts = rcar_canfd_read(priv->base, 1058 + RCANFD_CFSTS(ch, RCANFD_CFFIFO_IDX)); 1059 + if (sts & RCANFD_CFSTS_CFTXIF) 1060 + rcar_canfd_tx_done(ndev); 1061 + } 1062 + return IRQ_HANDLED; 1063 + } 1064 + 1065 + static void rcar_canfd_set_bittiming(struct net_device *dev) 1066 + { 1067 + struct rcar_canfd_channel *priv = netdev_priv(dev); 1068 + const struct can_bittiming *bt = &priv->can.bittiming; 1069 + const struct can_bittiming *dbt = &priv->can.data_bittiming; 1070 + u16 brp, sjw, tseg1, tseg2; 1071 + u32 cfg; 1072 + u32 ch = priv->channel; 1073 + 1074 + /* Nominal bit timing settings */ 1075 + brp = bt->brp - 1; 1076 + sjw = bt->sjw - 1; 1077 + tseg1 = bt->prop_seg + bt->phase_seg1 - 1; 1078 + tseg2 = bt->phase_seg2 - 1; 1079 + 1080 + cfg = (RCANFD_NCFG_NTSEG1(tseg1) | RCANFD_NCFG_NBRP(brp) | 1081 + RCANFD_NCFG_NSJW(sjw) | RCANFD_NCFG_NTSEG2(tseg2)); 1082 + 1083 + rcar_canfd_write(priv->base, RCANFD_CCFG(ch), cfg); 1084 + netdev_dbg(priv->ndev, "nrate: brp %u, sjw %u, tseg1 %u, tseg2 %u\n", 1085 + brp, sjw, tseg1, tseg2); 1086 + 1087 + /* Data bit timing settings */ 1088 + brp = dbt->brp - 1; 1089 + sjw = dbt->sjw - 1; 1090 + tseg1 = dbt->prop_seg + dbt->phase_seg1 - 1; 1091 + tseg2 = dbt->phase_seg2 - 1; 1092 + 1093 + cfg = (RCANFD_DCFG_DTSEG1(tseg1) | RCANFD_DCFG_DBRP(brp) | 1094 + RCANFD_DCFG_DSJW(sjw) | RCANFD_DCFG_DTSEG2(tseg2)); 1095 + 1096 + rcar_canfd_write(priv->base, RCANFD_F_DCFG(ch), cfg); 1097 + netdev_dbg(priv->ndev, "drate: brp %u, sjw %u, tseg1 %u, tseg2 %u\n", 1098 + brp, sjw, tseg1, tseg2); 1099 + } 1100 + 1101 + static int rcar_canfd_start(struct net_device *ndev) 1102 + { 1103 + struct rcar_canfd_channel *priv = netdev_priv(ndev); 1104 + int err = -EOPNOTSUPP; 1105 + u32 sts, ch = priv->channel; 1106 + u32 ridx = ch + RCANFD_RFFIFO_IDX; 1107 + 1108 + rcar_canfd_set_bittiming(ndev); 1109 + 1110 + rcar_canfd_enable_channel_interrupts(priv); 1111 + 1112 + /* Set channel to Operational mode */ 1113 + rcar_canfd_update_bit(priv->base, RCANFD_CCTR(ch), 1114 + RCANFD_CCTR_CHMDC_MASK, RCANFD_CCTR_CHDMC_COPM); 1115 + 1116 + /* Verify channel mode change */ 1117 + err = readl_poll_timeout((priv->base + RCANFD_CSTS(ch)), sts, 1118 + (sts & RCANFD_CSTS_COMSTS), 2, 500000); 1119 + if (err) { 1120 + netdev_err(ndev, "channel %u communication state failed\n", ch); 1121 + goto fail_mode_change; 1122 + } 1123 + 1124 + /* Enable Common & Rx FIFO */ 1125 + rcar_canfd_set_bit(priv->base, RCANFD_CFCC(ch, RCANFD_CFFIFO_IDX), 1126 + RCANFD_CFCC_CFE); 1127 + rcar_canfd_set_bit(priv->base, RCANFD_RFCC(ridx), RCANFD_RFCC_RFE); 1128 + 1129 + priv->can.state = CAN_STATE_ERROR_ACTIVE; 1130 + return 0; 1131 + 1132 + fail_mode_change: 1133 + rcar_canfd_disable_channel_interrupts(priv); 1134 + return err; 1135 + } 1136 + 1137 + static int rcar_canfd_open(struct net_device *ndev) 1138 + { 1139 + struct rcar_canfd_channel *priv = netdev_priv(ndev); 1140 + struct rcar_canfd_global *gpriv = priv->gpriv; 1141 + int err; 1142 + 1143 + /* Peripheral clock is already enabled in probe */ 1144 + err = clk_prepare_enable(gpriv->can_clk); 1145 + if (err) { 1146 + netdev_err(ndev, "failed to enable CAN clock, error %d\n", err); 1147 + goto out_clock; 1148 + } 1149 + 1150 + err = open_candev(ndev); 1151 + if (err) { 1152 + netdev_err(ndev, "open_candev() failed, error %d\n", err); 1153 + goto out_can_clock; 1154 + } 1155 + 1156 + napi_enable(&priv->napi); 1157 + err = rcar_canfd_start(ndev); 1158 + if (err) 1159 + goto out_close; 1160 + netif_start_queue(ndev); 1161 + can_led_event(ndev, CAN_LED_EVENT_OPEN); 1162 + return 0; 1163 + out_close: 1164 + napi_disable(&priv->napi); 1165 + close_candev(ndev); 1166 + out_can_clock: 1167 + clk_disable_unprepare(gpriv->can_clk); 1168 + out_clock: 1169 + return err; 1170 + } 1171 + 1172 + static void rcar_canfd_stop(struct net_device *ndev) 1173 + { 1174 + struct rcar_canfd_channel *priv = netdev_priv(ndev); 1175 + int err; 1176 + u32 sts, ch = priv->channel; 1177 + u32 ridx = ch + RCANFD_RFFIFO_IDX; 1178 + 1179 + /* Transition to channel reset mode */ 1180 + rcar_canfd_update_bit(priv->base, RCANFD_CCTR(ch), 1181 + RCANFD_CCTR_CHMDC_MASK, RCANFD_CCTR_CHDMC_CRESET); 1182 + 1183 + /* Check Channel reset mode */ 1184 + err = readl_poll_timeout((priv->base + RCANFD_CSTS(ch)), sts, 1185 + (sts & RCANFD_CSTS_CRSTSTS), 2, 500000); 1186 + if (err) 1187 + netdev_err(ndev, "channel %u reset failed\n", ch); 1188 + 1189 + rcar_canfd_disable_channel_interrupts(priv); 1190 + 1191 + /* Disable Common & Rx FIFO */ 1192 + rcar_canfd_clear_bit(priv->base, RCANFD_CFCC(ch, RCANFD_CFFIFO_IDX), 1193 + RCANFD_CFCC_CFE); 1194 + rcar_canfd_clear_bit(priv->base, RCANFD_RFCC(ridx), RCANFD_RFCC_RFE); 1195 + 1196 + /* Set the state as STOPPED */ 1197 + priv->can.state = CAN_STATE_STOPPED; 1198 + } 1199 + 1200 + static int rcar_canfd_close(struct net_device *ndev) 1201 + { 1202 + struct rcar_canfd_channel *priv = netdev_priv(ndev); 1203 + struct rcar_canfd_global *gpriv = priv->gpriv; 1204 + 1205 + netif_stop_queue(ndev); 1206 + rcar_canfd_stop(ndev); 1207 + napi_disable(&priv->napi); 1208 + clk_disable_unprepare(gpriv->can_clk); 1209 + close_candev(ndev); 1210 + can_led_event(ndev, CAN_LED_EVENT_STOP); 1211 + return 0; 1212 + } 1213 + 1214 + static netdev_tx_t rcar_canfd_start_xmit(struct sk_buff *skb, 1215 + struct net_device *ndev) 1216 + { 1217 + struct rcar_canfd_channel *priv = netdev_priv(ndev); 1218 + struct canfd_frame *cf = (struct canfd_frame *)skb->data; 1219 + u32 sts = 0, id, dlc; 1220 + unsigned long flags; 1221 + u32 ch = priv->channel; 1222 + 1223 + if (can_dropped_invalid_skb(ndev, skb)) 1224 + return NETDEV_TX_OK; 1225 + 1226 + if (cf->can_id & CAN_EFF_FLAG) { 1227 + id = cf->can_id & CAN_EFF_MASK; 1228 + id |= RCANFD_CFID_CFIDE; 1229 + } else { 1230 + id = cf->can_id & CAN_SFF_MASK; 1231 + } 1232 + 1233 + if (cf->can_id & CAN_RTR_FLAG) 1234 + id |= RCANFD_CFID_CFRTR; 1235 + 1236 + rcar_canfd_write(priv->base, 1237 + RCANFD_F_CFID(ch, RCANFD_CFFIFO_IDX), id); 1238 + dlc = RCANFD_CFPTR_CFDLC(can_len2dlc(cf->len)); 1239 + rcar_canfd_write(priv->base, 1240 + RCANFD_F_CFPTR(ch, RCANFD_CFFIFO_IDX), dlc); 1241 + 1242 + if (can_is_canfd_skb(skb)) { 1243 + /* CAN FD frame format */ 1244 + sts |= RCANFD_CFFDCSTS_CFFDF; 1245 + if (cf->flags & CANFD_BRS) 1246 + sts |= RCANFD_CFFDCSTS_CFBRS; 1247 + 1248 + if (priv->can.state == CAN_STATE_ERROR_PASSIVE) 1249 + sts |= RCANFD_CFFDCSTS_CFESI; 1250 + } 1251 + 1252 + rcar_canfd_write(priv->base, RCANFD_F_CFFDCSTS(ch, RCANFD_CFFIFO_IDX), 1253 + sts); 1254 + 1255 + rcar_canfd_put_data(priv, cf, 1256 + RCANFD_F_CFDF(ch, RCANFD_CFFIFO_IDX, 0)); 1257 + 1258 + priv->tx_len[priv->tx_head % RCANFD_FIFO_DEPTH] = cf->len; 1259 + can_put_echo_skb(skb, ndev, priv->tx_head % RCANFD_FIFO_DEPTH); 1260 + 1261 + spin_lock_irqsave(&priv->tx_lock, flags); 1262 + priv->tx_head++; 1263 + 1264 + /* Stop the queue if we've filled all FIFO entries */ 1265 + if (priv->tx_head - priv->tx_tail >= RCANFD_FIFO_DEPTH) 1266 + netif_stop_queue(ndev); 1267 + 1268 + /* Start Tx: Write 0xff to CFPC to increment the CPU-side 1269 + * pointer for the Common FIFO 1270 + */ 1271 + rcar_canfd_write(priv->base, 1272 + RCANFD_CFPCTR(ch, RCANFD_CFFIFO_IDX), 0xff); 1273 + 1274 + spin_unlock_irqrestore(&priv->tx_lock, flags); 1275 + return NETDEV_TX_OK; 1276 + } 1277 + 1278 + static void rcar_canfd_rx_pkt(struct rcar_canfd_channel *priv) 1279 + { 1280 + struct net_device_stats *stats = &priv->ndev->stats; 1281 + struct canfd_frame *cf; 1282 + struct sk_buff *skb; 1283 + u32 sts = 0, id, ptr; 1284 + u32 ch = priv->channel; 1285 + u32 ridx = ch + RCANFD_RFFIFO_IDX; 1286 + 1287 + id = rcar_canfd_read(priv->base, RCANFD_F_RFID(ridx)); 1288 + ptr = rcar_canfd_read(priv->base, RCANFD_F_RFPTR(ridx)); 1289 + 1290 + sts = rcar_canfd_read(priv->base, RCANFD_F_RFFDSTS(ridx)); 1291 + if (sts & RCANFD_RFFDSTS_RFFDF) 1292 + skb = alloc_canfd_skb(priv->ndev, &cf); 1293 + else 1294 + skb = alloc_can_skb(priv->ndev, 1295 + (struct can_frame **)&cf); 1296 + 1297 + if (!skb) { 1298 + stats->rx_dropped++; 1299 + return; 1300 + } 1301 + 1302 + if (sts & RCANFD_RFFDSTS_RFFDF) 1303 + cf->len = can_dlc2len(RCANFD_RFPTR_RFDLC(ptr)); 1304 + else 1305 + cf->len = get_can_dlc(RCANFD_RFPTR_RFDLC(ptr)); 1306 + 1307 + if (sts & RCANFD_RFFDSTS_RFESI) { 1308 + cf->flags |= CANFD_ESI; 1309 + netdev_dbg(priv->ndev, "ESI Error\n"); 1310 + } 1311 + 1312 + if (id & RCANFD_RFID_RFIDE) 1313 + cf->can_id = (id & CAN_EFF_MASK) | CAN_EFF_FLAG; 1314 + else 1315 + cf->can_id = id & CAN_SFF_MASK; 1316 + 1317 + if (!(sts & RCANFD_RFFDSTS_RFFDF) && (id & RCANFD_RFID_RFRTR)) { 1318 + cf->can_id |= CAN_RTR_FLAG; 1319 + } else { 1320 + if (sts & RCANFD_RFFDSTS_RFBRS) 1321 + cf->flags |= CANFD_BRS; 1322 + 1323 + rcar_canfd_get_data(priv, cf, RCANFD_F_RFDF(ridx, 0)); 1324 + } 1325 + 1326 + /* Write 0xff to RFPC to increment the CPU-side 1327 + * pointer of the Rx FIFO 1328 + */ 1329 + rcar_canfd_write(priv->base, RCANFD_RFPCTR(ridx), 0xff); 1330 + 1331 + can_led_event(priv->ndev, CAN_LED_EVENT_RX); 1332 + 1333 + stats->rx_bytes += cf->len; 1334 + stats->rx_packets++; 1335 + netif_receive_skb(skb); 1336 + } 1337 + 1338 + static int rcar_canfd_rx_poll(struct napi_struct *napi, int quota) 1339 + { 1340 + struct rcar_canfd_channel *priv = 1341 + container_of(napi, struct rcar_canfd_channel, napi); 1342 + int num_pkts; 1343 + u32 sts; 1344 + u32 ch = priv->channel; 1345 + u32 ridx = ch + RCANFD_RFFIFO_IDX; 1346 + 1347 + for (num_pkts = 0; num_pkts < quota; num_pkts++) { 1348 + sts = rcar_canfd_read(priv->base, RCANFD_RFSTS(ridx)); 1349 + /* Check FIFO empty condition */ 1350 + if (sts & RCANFD_RFSTS_RFEMP) 1351 + break; 1352 + 1353 + rcar_canfd_rx_pkt(priv); 1354 + 1355 + /* Clear interrupt bit */ 1356 + if (sts & RCANFD_RFSTS_RFIF) 1357 + rcar_canfd_write(priv->base, RCANFD_RFSTS(ridx), 1358 + sts & ~RCANFD_RFSTS_RFIF); 1359 + } 1360 + 1361 + /* All packets processed */ 1362 + if (num_pkts < quota) { 1363 + napi_complete(napi); 1364 + /* Enable Rx FIFO interrupts */ 1365 + rcar_canfd_set_bit(priv->base, RCANFD_RFCC(ridx), 1366 + RCANFD_RFCC_RFIE); 1367 + } 1368 + return num_pkts; 1369 + } 1370 + 1371 + static int rcar_canfd_do_set_mode(struct net_device *ndev, enum can_mode mode) 1372 + { 1373 + int err; 1374 + 1375 + switch (mode) { 1376 + case CAN_MODE_START: 1377 + err = rcar_canfd_start(ndev); 1378 + if (err) 1379 + return err; 1380 + netif_wake_queue(ndev); 1381 + return 0; 1382 + default: 1383 + return -EOPNOTSUPP; 1384 + } 1385 + } 1386 + 1387 + static int rcar_canfd_get_berr_counter(const struct net_device *dev, 1388 + struct can_berr_counter *bec) 1389 + { 1390 + struct rcar_canfd_channel *priv = netdev_priv(dev); 1391 + u32 val, ch = priv->channel; 1392 + 1393 + /* Peripheral clock is already enabled in probe */ 1394 + val = rcar_canfd_read(priv->base, RCANFD_CSTS(ch)); 1395 + bec->txerr = RCANFD_CSTS_TECCNT(val); 1396 + bec->rxerr = RCANFD_CSTS_RECCNT(val); 1397 + return 0; 1398 + } 1399 + 1400 + static const struct net_device_ops rcar_canfd_netdev_ops = { 1401 + .ndo_open = rcar_canfd_open, 1402 + .ndo_stop = rcar_canfd_close, 1403 + .ndo_start_xmit = rcar_canfd_start_xmit, 1404 + .ndo_change_mtu = can_change_mtu, 1405 + }; 1406 + 1407 + static int rcar_canfd_channel_probe(struct rcar_canfd_global *gpriv, u32 ch, 1408 + u32 fcan_freq) 1409 + { 1410 + struct platform_device *pdev = gpriv->pdev; 1411 + struct rcar_canfd_channel *priv; 1412 + struct net_device *ndev; 1413 + int err = -ENODEV; 1414 + 1415 + ndev = alloc_candev(sizeof(*priv), RCANFD_FIFO_DEPTH); 1416 + if (!ndev) { 1417 + dev_err(&pdev->dev, "alloc_candev() failed\n"); 1418 + err = -ENOMEM; 1419 + goto fail; 1420 + } 1421 + priv = netdev_priv(ndev); 1422 + 1423 + ndev->netdev_ops = &rcar_canfd_netdev_ops; 1424 + ndev->flags |= IFF_ECHO; 1425 + priv->ndev = ndev; 1426 + priv->base = gpriv->base; 1427 + priv->channel = ch; 1428 + priv->can.clock.freq = fcan_freq; 1429 + dev_info(&pdev->dev, "can_clk rate is %u\n", priv->can.clock.freq); 1430 + 1431 + priv->can.bittiming_const = &rcar_canfd_nom_bittiming_const; 1432 + priv->can.data_bittiming_const = 1433 + &rcar_canfd_data_bittiming_const; 1434 + 1435 + /* Controller starts in CAN FD only mode */ 1436 + can_set_static_ctrlmode(ndev, CAN_CTRLMODE_FD); 1437 + priv->can.ctrlmode_supported = CAN_CTRLMODE_BERR_REPORTING; 1438 + 1439 + priv->can.do_set_mode = rcar_canfd_do_set_mode; 1440 + priv->can.do_get_berr_counter = rcar_canfd_get_berr_counter; 1441 + priv->gpriv = gpriv; 1442 + SET_NETDEV_DEV(ndev, &pdev->dev); 1443 + 1444 + netif_napi_add(ndev, &priv->napi, rcar_canfd_rx_poll, 1445 + RCANFD_NAPI_WEIGHT); 1446 + err = register_candev(ndev); 1447 + if (err) { 1448 + dev_err(&pdev->dev, 1449 + "register_candev() failed, error %d\n", err); 1450 + goto fail_candev; 1451 + } 1452 + spin_lock_init(&priv->tx_lock); 1453 + devm_can_led_init(ndev); 1454 + gpriv->ch[priv->channel] = priv; 1455 + dev_info(&pdev->dev, "device registered (channel %u)\n", priv->channel); 1456 + return 0; 1457 + 1458 + fail_candev: 1459 + netif_napi_del(&priv->napi); 1460 + free_candev(ndev); 1461 + fail: 1462 + return err; 1463 + } 1464 + 1465 + static void rcar_canfd_channel_remove(struct rcar_canfd_global *gpriv, u32 ch) 1466 + { 1467 + struct rcar_canfd_channel *priv = gpriv->ch[ch]; 1468 + 1469 + if (priv) { 1470 + unregister_candev(priv->ndev); 1471 + netif_napi_del(&priv->napi); 1472 + free_candev(priv->ndev); 1473 + } 1474 + } 1475 + 1476 + static int rcar_canfd_probe(struct platform_device *pdev) 1477 + { 1478 + struct resource *mem; 1479 + void __iomem *addr; 1480 + u32 sts, ch, fcan_freq; 1481 + struct rcar_canfd_global *gpriv; 1482 + struct device_node *of_child; 1483 + unsigned long channels_mask = 0; 1484 + int err, ch_irq, g_irq; 1485 + 1486 + of_child = of_get_child_by_name(pdev->dev.of_node, "channel0"); 1487 + if (of_child && of_device_is_available(of_child)) 1488 + channels_mask |= BIT(0); /* Channel 0 */ 1489 + 1490 + of_child = of_get_child_by_name(pdev->dev.of_node, "channel1"); 1491 + if (of_child && of_device_is_available(of_child)) 1492 + channels_mask |= BIT(1); /* Channel 1 */ 1493 + 1494 + ch_irq = platform_get_irq(pdev, 0); 1495 + if (ch_irq < 0) { 1496 + dev_err(&pdev->dev, "no Channel IRQ resource\n"); 1497 + err = ch_irq; 1498 + goto fail_dev; 1499 + } 1500 + 1501 + g_irq = platform_get_irq(pdev, 1); 1502 + if (g_irq < 0) { 1503 + dev_err(&pdev->dev, "no Global IRQ resource\n"); 1504 + err = g_irq; 1505 + goto fail_dev; 1506 + } 1507 + 1508 + /* Global controller context */ 1509 + gpriv = devm_kzalloc(&pdev->dev, sizeof(*gpriv), GFP_KERNEL); 1510 + if (!gpriv) { 1511 + err = -ENOMEM; 1512 + goto fail_dev; 1513 + } 1514 + gpriv->pdev = pdev; 1515 + gpriv->channels_mask = channels_mask; 1516 + 1517 + /* Peripheral clock */ 1518 + gpriv->clkp = devm_clk_get(&pdev->dev, "fck"); 1519 + if (IS_ERR(gpriv->clkp)) { 1520 + err = PTR_ERR(gpriv->clkp); 1521 + dev_err(&pdev->dev, "cannot get peripheral clock, error %d\n", 1522 + err); 1523 + goto fail_dev; 1524 + } 1525 + 1526 + /* fCAN clock: Pick External clock. If not available fallback to 1527 + * CANFD clock 1528 + */ 1529 + gpriv->can_clk = devm_clk_get(&pdev->dev, "can_clk"); 1530 + if (IS_ERR(gpriv->can_clk) || (clk_get_rate(gpriv->can_clk) == 0)) { 1531 + gpriv->can_clk = devm_clk_get(&pdev->dev, "canfd"); 1532 + if (IS_ERR(gpriv->can_clk)) { 1533 + err = PTR_ERR(gpriv->can_clk); 1534 + dev_err(&pdev->dev, 1535 + "cannot get canfd clock, error %d\n", err); 1536 + goto fail_dev; 1537 + } 1538 + gpriv->fcan = RCANFD_CANFDCLK; 1539 + 1540 + } else { 1541 + gpriv->fcan = RCANFD_EXTCLK; 1542 + } 1543 + fcan_freq = clk_get_rate(gpriv->can_clk); 1544 + 1545 + if (gpriv->fcan == RCANFD_CANFDCLK) 1546 + /* CANFD clock is further divided by (1/2) within the IP */ 1547 + fcan_freq /= 2; 1548 + 1549 + mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1550 + addr = devm_ioremap_resource(&pdev->dev, mem); 1551 + if (IS_ERR(addr)) { 1552 + err = PTR_ERR(addr); 1553 + goto fail_dev; 1554 + } 1555 + gpriv->base = addr; 1556 + 1557 + /* Request IRQ that's common for both channels */ 1558 + err = devm_request_irq(&pdev->dev, ch_irq, 1559 + rcar_canfd_channel_interrupt, 0, 1560 + "canfd.chn", gpriv); 1561 + if (err) { 1562 + dev_err(&pdev->dev, "devm_request_irq(%d) failed, error %d\n", 1563 + ch_irq, err); 1564 + goto fail_dev; 1565 + } 1566 + err = devm_request_irq(&pdev->dev, g_irq, 1567 + rcar_canfd_global_interrupt, 0, 1568 + "canfd.gbl", gpriv); 1569 + if (err) { 1570 + dev_err(&pdev->dev, "devm_request_irq(%d) failed, error %d\n", 1571 + g_irq, err); 1572 + goto fail_dev; 1573 + } 1574 + 1575 + /* Enable peripheral clock for register access */ 1576 + err = clk_prepare_enable(gpriv->clkp); 1577 + if (err) { 1578 + dev_err(&pdev->dev, 1579 + "failed to enable peripheral clock, error %d\n", err); 1580 + goto fail_dev; 1581 + } 1582 + 1583 + err = rcar_canfd_reset_controller(gpriv); 1584 + if (err) { 1585 + dev_err(&pdev->dev, "reset controller failed\n"); 1586 + goto fail_clk; 1587 + } 1588 + 1589 + /* Controller in Global reset & Channel reset mode */ 1590 + rcar_canfd_configure_controller(gpriv); 1591 + 1592 + /* Configure per channel attributes */ 1593 + for_each_set_bit(ch, &gpriv->channels_mask, RCANFD_NUM_CHANNELS) { 1594 + /* Configure Channel's Rx fifo */ 1595 + rcar_canfd_configure_rx(gpriv, ch); 1596 + 1597 + /* Configure Channel's Tx (Common) fifo */ 1598 + rcar_canfd_configure_tx(gpriv, ch); 1599 + 1600 + /* Configure receive rules */ 1601 + rcar_canfd_configure_afl_rules(gpriv, ch); 1602 + } 1603 + 1604 + /* Configure common interrupts */ 1605 + rcar_canfd_enable_global_interrupts(gpriv); 1606 + 1607 + /* Start Global operation mode */ 1608 + rcar_canfd_update_bit(gpriv->base, RCANFD_GCTR, RCANFD_GCTR_GMDC_MASK, 1609 + RCANFD_GCTR_GMDC_GOPM); 1610 + 1611 + /* Verify mode change */ 1612 + err = readl_poll_timeout((gpriv->base + RCANFD_GSTS), sts, 1613 + !(sts & RCANFD_GSTS_GNOPM), 2, 500000); 1614 + if (err) { 1615 + dev_err(&pdev->dev, "global operational mode failed\n"); 1616 + goto fail_mode; 1617 + } 1618 + 1619 + for_each_set_bit(ch, &gpriv->channels_mask, RCANFD_NUM_CHANNELS) { 1620 + err = rcar_canfd_channel_probe(gpriv, ch, fcan_freq); 1621 + if (err) 1622 + goto fail_channel; 1623 + } 1624 + 1625 + platform_set_drvdata(pdev, gpriv); 1626 + dev_info(&pdev->dev, "global operational state (clk %d)\n", 1627 + gpriv->fcan); 1628 + return 0; 1629 + 1630 + fail_channel: 1631 + for_each_set_bit(ch, &gpriv->channels_mask, RCANFD_NUM_CHANNELS) 1632 + rcar_canfd_channel_remove(gpriv, ch); 1633 + fail_mode: 1634 + rcar_canfd_disable_global_interrupts(gpriv); 1635 + fail_clk: 1636 + clk_disable_unprepare(gpriv->clkp); 1637 + fail_dev: 1638 + return err; 1639 + } 1640 + 1641 + static int rcar_canfd_remove(struct platform_device *pdev) 1642 + { 1643 + struct rcar_canfd_global *gpriv = platform_get_drvdata(pdev); 1644 + u32 ch; 1645 + 1646 + rcar_canfd_reset_controller(gpriv); 1647 + rcar_canfd_disable_global_interrupts(gpriv); 1648 + 1649 + for_each_set_bit(ch, &gpriv->channels_mask, RCANFD_NUM_CHANNELS) { 1650 + rcar_canfd_disable_channel_interrupts(gpriv->ch[ch]); 1651 + rcar_canfd_channel_remove(gpriv, ch); 1652 + } 1653 + 1654 + /* Enter global sleep mode */ 1655 + rcar_canfd_set_bit(gpriv->base, RCANFD_GCTR, RCANFD_GCTR_GSLPR); 1656 + clk_disable_unprepare(gpriv->clkp); 1657 + return 0; 1658 + } 1659 + 1660 + static int __maybe_unused rcar_canfd_suspend(struct device *dev) 1661 + { 1662 + return 0; 1663 + } 1664 + 1665 + static int __maybe_unused rcar_canfd_resume(struct device *dev) 1666 + { 1667 + return 0; 1668 + } 1669 + 1670 + static SIMPLE_DEV_PM_OPS(rcar_canfd_pm_ops, rcar_canfd_suspend, 1671 + rcar_canfd_resume); 1672 + 1673 + static const struct of_device_id rcar_canfd_of_table[] = { 1674 + { .compatible = "renesas,rcar-gen3-canfd" }, 1675 + { } 1676 + }; 1677 + 1678 + MODULE_DEVICE_TABLE(of, rcar_canfd_of_table); 1679 + 1680 + static struct platform_driver rcar_canfd_driver = { 1681 + .driver = { 1682 + .name = RCANFD_DRV_NAME, 1683 + .of_match_table = of_match_ptr(rcar_canfd_of_table), 1684 + .pm = &rcar_canfd_pm_ops, 1685 + }, 1686 + .probe = rcar_canfd_probe, 1687 + .remove = rcar_canfd_remove, 1688 + }; 1689 + 1690 + module_platform_driver(rcar_canfd_driver); 1691 + 1692 + MODULE_AUTHOR("Ramesh Shanmugasundaram <ramesh.shanmugasundaram@bp.renesas.com>"); 1693 + MODULE_LICENSE("GPL"); 1694 + MODULE_DESCRIPTION("CAN FD driver for Renesas R-Car SoC"); 1695 + MODULE_ALIAS("platform:" RCANFD_DRV_NAME);
drivers/net/can/rcar_can.c drivers/net/can/rcar/rcar_can.c
+1 -11
drivers/net/can/sja1000/tscan1.c
··· 203 203 }, 204 204 }; 205 205 206 - static int __init tscan1_init(void) 207 - { 208 - return isa_register_driver(&tscan1_isa_driver, TSCAN1_MAXDEV); 209 - } 210 - module_init(tscan1_init); 211 - 212 - static void __exit tscan1_exit(void) 213 - { 214 - isa_unregister_driver(&tscan1_isa_driver); 215 - } 216 - module_exit(tscan1_exit); 206 + module_isa_driver(tscan1_isa_driver, TSCAN1_MAXDEV);
+2 -2
drivers/net/can/slcan.c
··· 354 354 { 355 355 struct slcan *sl = netdev_priv(dev); 356 356 357 - if (skb->len != sizeof(struct can_frame)) 357 + if (skb->len != CAN_MTU) 358 358 goto out; 359 359 360 360 spin_lock(&sl->lock); ··· 442 442 dev->addr_len = 0; 443 443 dev->tx_queue_len = 10; 444 444 445 - dev->mtu = sizeof(struct can_frame); 445 + dev->mtu = CAN_MTU; 446 446 dev->type = ARPHRD_CAN; 447 447 448 448 /* New-style flags. */
+99 -42
drivers/net/can/usb/gs_usb.c
··· 39 39 GS_USB_BREQ_MODE, 40 40 GS_USB_BREQ_BERR, 41 41 GS_USB_BREQ_BT_CONST, 42 - GS_USB_BREQ_DEVICE_CONFIG 42 + GS_USB_BREQ_DEVICE_CONFIG, 43 + GS_USB_BREQ_TIMESTAMP, 44 + GS_USB_BREQ_IDENTIFY, 43 45 }; 44 46 45 47 enum gs_can_mode { ··· 58 56 GS_CAN_STATE_BUS_OFF, 59 57 GS_CAN_STATE_STOPPED, 60 58 GS_CAN_STATE_SLEEPING 59 + }; 60 + 61 + enum gs_can_identify_mode { 62 + GS_CAN_IDENTIFY_OFF = 0, 63 + GS_CAN_IDENTIFY_ON 61 64 }; 62 65 63 66 /* data types passed between host and device */ ··· 84 77 } __packed; 85 78 86 79 #define GS_CAN_MODE_NORMAL 0 87 - #define GS_CAN_MODE_LISTEN_ONLY (1<<0) 88 - #define GS_CAN_MODE_LOOP_BACK (1<<1) 89 - #define GS_CAN_MODE_TRIPLE_SAMPLE (1<<2) 90 - #define GS_CAN_MODE_ONE_SHOT (1<<3) 80 + #define GS_CAN_MODE_LISTEN_ONLY BIT(0) 81 + #define GS_CAN_MODE_LOOP_BACK BIT(1) 82 + #define GS_CAN_MODE_TRIPLE_SAMPLE BIT(2) 83 + #define GS_CAN_MODE_ONE_SHOT BIT(3) 91 84 92 85 struct gs_device_mode { 93 86 u32 mode; ··· 108 101 u32 brp; 109 102 } __packed; 110 103 111 - #define GS_CAN_FEATURE_LISTEN_ONLY (1<<0) 112 - #define GS_CAN_FEATURE_LOOP_BACK (1<<1) 113 - #define GS_CAN_FEATURE_TRIPLE_SAMPLE (1<<2) 114 - #define GS_CAN_FEATURE_ONE_SHOT (1<<3) 104 + struct gs_identify_mode { 105 + u32 mode; 106 + } __packed; 107 + 108 + #define GS_CAN_FEATURE_LISTEN_ONLY BIT(0) 109 + #define GS_CAN_FEATURE_LOOP_BACK BIT(1) 110 + #define GS_CAN_FEATURE_TRIPLE_SAMPLE BIT(2) 111 + #define GS_CAN_FEATURE_ONE_SHOT BIT(3) 112 + #define GS_CAN_FEATURE_HW_TIMESTAMP BIT(4) 113 + #define GS_CAN_FEATURE_IDENTIFY BIT(5) 115 114 116 115 struct gs_device_bt_const { 117 116 u32 feature; ··· 222 209 223 210 /* Get a tx context by id. 224 211 */ 225 - static struct gs_tx_context *gs_get_tx_context(struct gs_can *dev, unsigned int id) 212 + static struct gs_tx_context *gs_get_tx_context(struct gs_can *dev, 213 + unsigned int id) 226 214 { 227 215 unsigned long flags; 228 216 ··· 466 452 netif_wake_queue(netdev); 467 453 } 468 454 469 - static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb, struct net_device *netdev) 455 + static netdev_tx_t gs_can_start_xmit(struct sk_buff *skb, 456 + struct net_device *netdev) 470 457 { 471 458 struct gs_can *dev = netdev_priv(netdev); 472 459 struct net_device_stats *stats = &dev->netdev->stats; ··· 673 658 rc = usb_control_msg(interface_to_usbdev(dev->iface), 674 659 usb_sndctrlpipe(interface_to_usbdev(dev->iface), 0), 675 660 GS_USB_BREQ_MODE, 676 - USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, 661 + USB_DIR_OUT | USB_TYPE_VENDOR | 662 + USB_RECIP_INTERFACE, 677 663 dev->channel, 678 664 0, 679 665 dm, ··· 737 721 .ndo_change_mtu = can_change_mtu, 738 722 }; 739 723 740 - static struct gs_can *gs_make_candev(unsigned int channel, struct usb_interface *intf) 724 + static int gs_usb_set_identify(struct net_device *netdev, bool do_identify) 725 + { 726 + struct gs_can *dev = netdev_priv(netdev); 727 + struct gs_identify_mode imode; 728 + int rc; 729 + 730 + if (do_identify) 731 + imode.mode = GS_CAN_IDENTIFY_ON; 732 + else 733 + imode.mode = GS_CAN_IDENTIFY_OFF; 734 + 735 + rc = usb_control_msg(interface_to_usbdev(dev->iface), 736 + usb_sndctrlpipe(interface_to_usbdev(dev->iface), 737 + 0), 738 + GS_USB_BREQ_IDENTIFY, 739 + USB_DIR_OUT | USB_TYPE_VENDOR | 740 + USB_RECIP_INTERFACE, 741 + dev->channel, 742 + 0, 743 + &imode, 744 + sizeof(imode), 745 + 100); 746 + 747 + return (rc > 0) ? 0 : rc; 748 + } 749 + 750 + /* blink LED's for finding the this interface */ 751 + static int gs_usb_set_phys_id(struct net_device *dev, 752 + enum ethtool_phys_id_state state) 753 + { 754 + int rc = 0; 755 + 756 + switch (state) { 757 + case ETHTOOL_ID_ACTIVE: 758 + rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_ON); 759 + break; 760 + case ETHTOOL_ID_INACTIVE: 761 + rc = gs_usb_set_identify(dev, GS_CAN_IDENTIFY_OFF); 762 + break; 763 + default: 764 + break; 765 + } 766 + 767 + return rc; 768 + } 769 + 770 + static const struct ethtool_ops gs_usb_ethtool_ops = { 771 + .set_phys_id = gs_usb_set_phys_id, 772 + }; 773 + 774 + static struct gs_can *gs_make_candev(unsigned int channel, 775 + struct usb_interface *intf, 776 + struct gs_device_config *dconf) 741 777 { 742 778 struct gs_can *dev; 743 779 struct net_device *netdev; ··· 877 809 if (bt_const->feature & GS_CAN_FEATURE_ONE_SHOT) 878 810 dev->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT; 879 811 880 - kfree(bt_const); 881 - 882 812 SET_NETDEV_DEV(netdev, &intf->dev); 813 + 814 + if (dconf->sw_version > 1) 815 + if (bt_const->feature & GS_CAN_FEATURE_IDENTIFY) 816 + netdev->ethtool_ops = &gs_usb_ethtool_ops; 817 + 818 + kfree(bt_const); 883 819 884 820 rc = register_candev(dev->netdev); 885 821 if (rc) { ··· 902 830 free_candev(dev->netdev); 903 831 } 904 832 905 - static int gs_usb_probe(struct usb_interface *intf, const struct usb_device_id *id) 833 + static int gs_usb_probe(struct usb_interface *intf, 834 + const struct usb_device_id *id) 906 835 { 907 836 struct gs_usb *dev; 908 837 int rc = -ENOMEM; 909 838 unsigned int icount, i; 910 - struct gs_host_config *hconf; 911 - struct gs_device_config *dconf; 912 - 913 - hconf = kmalloc(sizeof(*hconf), GFP_KERNEL); 914 - if (!hconf) 915 - return -ENOMEM; 916 - 917 - hconf->byte_order = 0x0000beef; 839 + struct gs_host_config hconf = { 840 + .byte_order = 0x0000beef, 841 + }; 842 + struct gs_device_config dconf; 918 843 919 844 /* send host config */ 920 845 rc = usb_control_msg(interface_to_usbdev(intf), ··· 920 851 USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, 921 852 1, 922 853 intf->altsetting[0].desc.bInterfaceNumber, 923 - hconf, 924 - sizeof(*hconf), 854 + &hconf, 855 + sizeof(hconf), 925 856 1000); 926 - 927 - kfree(hconf); 928 857 929 858 if (rc < 0) { 930 859 dev_err(&intf->dev, "Couldn't send data format (err=%d)\n", 931 860 rc); 932 861 return rc; 933 862 } 934 - 935 - dconf = kmalloc(sizeof(*dconf), GFP_KERNEL); 936 - if (!dconf) 937 - return -ENOMEM; 938 863 939 864 /* read device config */ 940 865 rc = usb_control_msg(interface_to_usbdev(intf), ··· 937 874 USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, 938 875 1, 939 876 intf->altsetting[0].desc.bInterfaceNumber, 940 - dconf, 941 - sizeof(*dconf), 877 + &dconf, 878 + sizeof(dconf), 942 879 1000); 943 880 if (rc < 0) { 944 881 dev_err(&intf->dev, "Couldn't get device config: (err=%d)\n", 945 882 rc); 946 - 947 - kfree(dconf); 948 - 949 883 return rc; 950 884 } 951 885 952 - icount = dconf->icount+1; 953 - 954 - kfree(dconf); 955 - 886 + icount = dconf.icount + 1; 956 887 dev_info(&intf->dev, "Configuring for %d interfaces\n", icount); 957 888 958 889 if (icount > GS_MAX_INTF) { ··· 967 910 dev->udev = interface_to_usbdev(intf); 968 911 969 912 for (i = 0; i < icount; i++) { 970 - dev->canch[i] = gs_make_candev(i, intf); 913 + dev->canch[i] = gs_make_candev(i, intf, &dconf); 971 914 if (IS_ERR_OR_NULL(dev->canch[i])) { 972 915 /* save error code to return later */ 973 916 rc = PTR_ERR(dev->canch[i]);
+1
include/uapi/linux/can/bcm.h
··· 99 99 #define RX_ANNOUNCE_RESUME 0x0100 100 100 #define TX_RESET_MULTI_IDX 0x0200 101 101 #define RX_RTR_FRAME 0x0400 102 + #define CAN_FD_FRAME 0x0800 102 103 103 104 #endif /* !_UAPI_CAN_BCM_H */
+2 -1
net/can/Makefile
··· 3 3 # 4 4 5 5 obj-$(CONFIG_CAN) += can.o 6 - can-y := af_can.o proc.o 6 + can-y := af_can.o 7 + can-$(CONFIG_PROC_FS) += proc.o 7 8 8 9 obj-$(CONFIG_CAN_RAW) += can-raw.o 9 10 can-raw-y := raw.o
+11
net/can/af_can.h
··· 113 113 extern struct dev_rcv_lists can_rx_alldev_list; 114 114 115 115 /* function prototypes for the CAN networklayer procfs (proc.c) */ 116 + #ifdef CONFIG_PROC_FS 116 117 void can_init_proc(void); 117 118 void can_remove_proc(void); 119 + #else 120 + static inline void can_init_proc(void) 121 + { 122 + pr_info("can: Can't create /proc/net/can. CONFIG_PROC_FS missing!\n"); 123 + } 124 + 125 + static inline void can_remove_proc(void) 126 + { 127 + } 128 + #endif 118 129 void can_stat_update(unsigned long data); 119 130 120 131 /* structures and variables from af_can.c needed in proc.c for reading */
+179 -130
net/can/bcm.c
··· 1 1 /* 2 2 * bcm.c - Broadcast Manager to filter/send (cyclic) CAN content 3 3 * 4 - * Copyright (c) 2002-2007 Volkswagen Group Electronic Research 4 + * Copyright (c) 2002-2016 Volkswagen Group Electronic Research 5 5 * All rights reserved. 6 6 * 7 7 * Redistribution and use in source and binary forms, with or without ··· 67 67 */ 68 68 #define MAX_NFRAMES 256 69 69 70 - /* use of last_frames[index].can_dlc */ 70 + /* use of last_frames[index].flags */ 71 71 #define RX_RECV 0x40 /* received data for this element */ 72 72 #define RX_THR 0x80 /* element not been sent due to throttle feature */ 73 - #define BCM_CAN_DLC_MASK 0x0F /* clean private flags in can_dlc by masking */ 73 + #define BCM_CAN_FLAGS_MASK 0x3F /* to clean private flags after usage */ 74 74 75 75 /* get best masking value for can_rx_register() for a given single can_id */ 76 76 #define REGMASK(id) ((id & CAN_EFF_FLAG) ? \ 77 77 (CAN_EFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG) : \ 78 78 (CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG)) 79 79 80 - #define CAN_BCM_VERSION CAN_VERSION 80 + #define CAN_BCM_VERSION "20160617" 81 81 82 82 MODULE_DESCRIPTION("PF_CAN broadcast manager protocol"); 83 83 MODULE_LICENSE("Dual BSD/GPL"); 84 84 MODULE_AUTHOR("Oliver Hartkopp <oliver.hartkopp@volkswagen.de>"); 85 85 MODULE_ALIAS("can-proto-2"); 86 86 87 - /* easy access to can_frame payload */ 88 - static inline u64 GET_U64(const struct can_frame *cp) 87 + /* 88 + * easy access to the first 64 bit of can(fd)_frame payload. cp->data is 89 + * 64 bit aligned so the offset has to be multiples of 8 which is ensured 90 + * by the only callers in bcm_rx_cmp_to_index() bcm_rx_handler(). 91 + */ 92 + static inline u64 get_u64(const struct canfd_frame *cp, int offset) 89 93 { 90 - return *(u64 *)cp->data; 94 + return *(u64 *)(cp->data + offset); 91 95 } 92 96 93 97 struct bcm_op { ··· 105 101 struct tasklet_struct tsklet, thrtsklet; 106 102 ktime_t rx_stamp, kt_ival1, kt_ival2, kt_lastmsg; 107 103 int rx_ifindex; 104 + int cfsiz; 108 105 u32 count; 109 106 u32 nframes; 110 107 u32 currframe; 111 - struct can_frame *frames; 112 - struct can_frame *last_frames; 113 - struct can_frame sframe; 114 - struct can_frame last_sframe; 108 + struct canfd_frame *frames; 109 + struct canfd_frame *last_frames; 110 + struct canfd_frame sframe; 111 + struct canfd_frame last_sframe; 115 112 struct sock *sk; 116 113 struct net_device *rx_reg_dev; 117 114 }; ··· 141 136 return ktime_set(tv.tv_sec, tv.tv_usec * NSEC_PER_USEC); 142 137 } 143 138 144 - #define CFSIZ sizeof(struct can_frame) 139 + #define CFSIZ(flags) ((flags & CAN_FD_FRAME) ? CANFD_MTU : CAN_MTU) 145 140 #define OPSIZ sizeof(struct bcm_op) 146 141 #define MHSIZ sizeof(struct bcm_msg_head) 147 142 ··· 188 183 if (!op->frames_abs) 189 184 continue; 190 185 191 - seq_printf(m, "rx_op: %03X %-5s ", 192 - op->can_id, bcm_proc_getifname(ifname, op->ifindex)); 193 - seq_printf(m, "[%u]%c ", op->nframes, 194 - (op->flags & RX_CHECK_DLC)?'d':' '); 186 + seq_printf(m, "rx_op: %03X %-5s ", op->can_id, 187 + bcm_proc_getifname(ifname, op->ifindex)); 188 + 189 + if (op->flags & CAN_FD_FRAME) 190 + seq_printf(m, "(%u)", op->nframes); 191 + else 192 + seq_printf(m, "[%u]", op->nframes); 193 + 194 + seq_printf(m, "%c ", (op->flags & RX_CHECK_DLC) ? 'd' : ' '); 195 + 195 196 if (op->kt_ival1.tv64) 196 197 seq_printf(m, "timeo=%lld ", 197 - (long long) 198 - ktime_to_us(op->kt_ival1)); 198 + (long long)ktime_to_us(op->kt_ival1)); 199 199 200 200 if (op->kt_ival2.tv64) 201 201 seq_printf(m, "thr=%lld ", 202 - (long long) 203 - ktime_to_us(op->kt_ival2)); 202 + (long long)ktime_to_us(op->kt_ival2)); 204 203 205 204 seq_printf(m, "# recv %ld (%ld) => reduction: ", 206 - op->frames_filtered, op->frames_abs); 205 + op->frames_filtered, op->frames_abs); 207 206 208 207 reduction = 100 - (op->frames_filtered * 100) / op->frames_abs; 209 208 210 209 seq_printf(m, "%s%ld%%\n", 211 - (reduction == 100)?"near ":"", reduction); 210 + (reduction == 100) ? "near " : "", reduction); 212 211 } 213 212 214 213 list_for_each_entry(op, &bo->tx_ops, list) { 215 214 216 - seq_printf(m, "tx_op: %03X %s [%u] ", 217 - op->can_id, 218 - bcm_proc_getifname(ifname, op->ifindex), 219 - op->nframes); 215 + seq_printf(m, "tx_op: %03X %s ", op->can_id, 216 + bcm_proc_getifname(ifname, op->ifindex)); 217 + 218 + if (op->flags & CAN_FD_FRAME) 219 + seq_printf(m, "(%u) ", op->nframes); 220 + else 221 + seq_printf(m, "[%u] ", op->nframes); 220 222 221 223 if (op->kt_ival1.tv64) 222 224 seq_printf(m, "t1=%lld ", 223 - (long long) ktime_to_us(op->kt_ival1)); 225 + (long long)ktime_to_us(op->kt_ival1)); 224 226 225 227 if (op->kt_ival2.tv64) 226 228 seq_printf(m, "t2=%lld ", 227 - (long long) ktime_to_us(op->kt_ival2)); 229 + (long long)ktime_to_us(op->kt_ival2)); 228 230 229 231 seq_printf(m, "# sent %ld\n", op->frames_abs); 230 232 } ··· 260 248 { 261 249 struct sk_buff *skb; 262 250 struct net_device *dev; 263 - struct can_frame *cf = &op->frames[op->currframe]; 251 + struct canfd_frame *cf = op->frames + op->cfsiz * op->currframe; 264 252 265 253 /* no target device? => exit */ 266 254 if (!op->ifindex) ··· 272 260 return; 273 261 } 274 262 275 - skb = alloc_skb(CFSIZ + sizeof(struct can_skb_priv), gfp_any()); 263 + skb = alloc_skb(op->cfsiz + sizeof(struct can_skb_priv), gfp_any()); 276 264 if (!skb) 277 265 goto out; 278 266 ··· 280 268 can_skb_prv(skb)->ifindex = dev->ifindex; 281 269 can_skb_prv(skb)->skbcnt = 0; 282 270 283 - memcpy(skb_put(skb, CFSIZ), cf, CFSIZ); 271 + memcpy(skb_put(skb, op->cfsiz), cf, op->cfsiz); 284 272 285 273 /* send with loopback */ 286 274 skb->dev = dev; ··· 294 282 /* reached last frame? */ 295 283 if (op->currframe >= op->nframes) 296 284 op->currframe = 0; 297 - out: 285 + out: 298 286 dev_put(dev); 299 287 } 300 288 ··· 303 291 * (consisting of bcm_msg_head + x CAN frames) 304 292 */ 305 293 static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head, 306 - struct can_frame *frames, int has_timestamp) 294 + struct canfd_frame *frames, int has_timestamp) 307 295 { 308 296 struct sk_buff *skb; 309 - struct can_frame *firstframe; 297 + struct canfd_frame *firstframe; 310 298 struct sockaddr_can *addr; 311 299 struct sock *sk = op->sk; 312 - unsigned int datalen = head->nframes * CFSIZ; 300 + unsigned int datalen = head->nframes * op->cfsiz; 313 301 int err; 314 302 315 303 skb = alloc_skb(sizeof(*head) + datalen, gfp_any()); ··· 319 307 memcpy(skb_put(skb, sizeof(*head)), head, sizeof(*head)); 320 308 321 309 if (head->nframes) { 322 - /* can_frames starting here */ 323 - firstframe = (struct can_frame *)skb_tail_pointer(skb); 310 + /* CAN frames starting here */ 311 + firstframe = (struct canfd_frame *)skb_tail_pointer(skb); 324 312 325 313 memcpy(skb_put(skb, datalen), frames, datalen); 326 314 327 315 /* 328 - * the BCM uses the can_dlc-element of the can_frame 316 + * the BCM uses the flags-element of the canfd_frame 329 317 * structure for internal purposes. This is only 330 318 * relevant for updates that are generated by the 331 319 * BCM, where nframes is 1 332 320 */ 333 321 if (head->nframes == 1) 334 - firstframe->can_dlc &= BCM_CAN_DLC_MASK; 322 + firstframe->flags &= BCM_CAN_FLAGS_MASK; 335 323 } 336 324 337 325 if (has_timestamp) { ··· 418 406 /* 419 407 * bcm_rx_changed - create a RX_CHANGED notification due to changed content 420 408 */ 421 - static void bcm_rx_changed(struct bcm_op *op, struct can_frame *data) 409 + static void bcm_rx_changed(struct bcm_op *op, struct canfd_frame *data) 422 410 { 423 411 struct bcm_msg_head head; 424 412 ··· 430 418 op->frames_filtered = op->frames_abs = 0; 431 419 432 420 /* this element is not throttled anymore */ 433 - data->can_dlc &= (BCM_CAN_DLC_MASK|RX_RECV); 421 + data->flags &= (BCM_CAN_FLAGS_MASK|RX_RECV); 434 422 435 423 head.opcode = RX_CHANGED; 436 424 head.flags = op->flags; ··· 449 437 * 2. send a notification to the user (if possible) 450 438 */ 451 439 static void bcm_rx_update_and_send(struct bcm_op *op, 452 - struct can_frame *lastdata, 453 - const struct can_frame *rxdata) 440 + struct canfd_frame *lastdata, 441 + const struct canfd_frame *rxdata) 454 442 { 455 - memcpy(lastdata, rxdata, CFSIZ); 443 + memcpy(lastdata, rxdata, op->cfsiz); 456 444 457 445 /* mark as used and throttled by default */ 458 - lastdata->can_dlc |= (RX_RECV|RX_THR); 446 + lastdata->flags |= (RX_RECV|RX_THR); 459 447 460 448 /* throttling mode inactive ? */ 461 449 if (!op->kt_ival2.tv64) { ··· 493 481 * received data stored in op->last_frames[] 494 482 */ 495 483 static void bcm_rx_cmp_to_index(struct bcm_op *op, unsigned int index, 496 - const struct can_frame *rxdata) 484 + const struct canfd_frame *rxdata) 497 485 { 486 + struct canfd_frame *cf = op->frames + op->cfsiz * index; 487 + struct canfd_frame *lcf = op->last_frames + op->cfsiz * index; 488 + int i; 489 + 498 490 /* 499 - * no one uses the MSBs of can_dlc for comparison, 491 + * no one uses the MSBs of flags for comparison, 500 492 * so we use it here to detect the first time of reception 501 493 */ 502 494 503 - if (!(op->last_frames[index].can_dlc & RX_RECV)) { 495 + if (!(lcf->flags & RX_RECV)) { 504 496 /* received data for the first time => send update to user */ 505 - bcm_rx_update_and_send(op, &op->last_frames[index], rxdata); 497 + bcm_rx_update_and_send(op, lcf, rxdata); 506 498 return; 507 499 } 508 500 509 - /* do a real check in can_frame data section */ 510 - 511 - if ((GET_U64(&op->frames[index]) & GET_U64(rxdata)) != 512 - (GET_U64(&op->frames[index]) & GET_U64(&op->last_frames[index]))) { 513 - bcm_rx_update_and_send(op, &op->last_frames[index], rxdata); 514 - return; 501 + /* do a real check in CAN frame data section */ 502 + for (i = 0; i < rxdata->len; i += 8) { 503 + if ((get_u64(cf, i) & get_u64(rxdata, i)) != 504 + (get_u64(cf, i) & get_u64(lcf, i))) { 505 + bcm_rx_update_and_send(op, lcf, rxdata); 506 + return; 507 + } 515 508 } 516 509 517 510 if (op->flags & RX_CHECK_DLC) { 518 - /* do a real check in can_frame dlc */ 519 - if (rxdata->can_dlc != (op->last_frames[index].can_dlc & 520 - BCM_CAN_DLC_MASK)) { 521 - bcm_rx_update_and_send(op, &op->last_frames[index], 522 - rxdata); 511 + /* do a real check in CAN frame length */ 512 + if (rxdata->len != lcf->len) { 513 + bcm_rx_update_and_send(op, lcf, rxdata); 523 514 return; 524 515 } 525 516 } ··· 571 556 572 557 /* if user wants to be informed, when cyclic CAN-Messages come back */ 573 558 if ((op->flags & RX_ANNOUNCE_RESUME) && op->last_frames) { 574 - /* clear received can_frames to indicate 'nothing received' */ 575 - memset(op->last_frames, 0, op->nframes * CFSIZ); 559 + /* clear received CAN frames to indicate 'nothing received' */ 560 + memset(op->last_frames, 0, op->nframes * op->cfsiz); 576 561 } 577 562 578 563 return HRTIMER_NORESTART; ··· 584 569 static inline int bcm_rx_do_flush(struct bcm_op *op, int update, 585 570 unsigned int index) 586 571 { 587 - if ((op->last_frames) && (op->last_frames[index].can_dlc & RX_THR)) { 572 + struct canfd_frame *lcf = op->last_frames + op->cfsiz * index; 573 + 574 + if ((op->last_frames) && (lcf->flags & RX_THR)) { 588 575 if (update) 589 - bcm_rx_changed(op, &op->last_frames[index]); 576 + bcm_rx_changed(op, lcf); 590 577 return 1; 591 578 } 592 579 return 0; ··· 653 636 static void bcm_rx_handler(struct sk_buff *skb, void *data) 654 637 { 655 638 struct bcm_op *op = (struct bcm_op *)data; 656 - const struct can_frame *rxframe = (struct can_frame *)skb->data; 639 + const struct canfd_frame *rxframe = (struct canfd_frame *)skb->data; 657 640 unsigned int i; 658 - 659 - /* disable timeout */ 660 - hrtimer_cancel(&op->timer); 661 641 662 642 if (op->can_id != rxframe->can_id) 663 643 return; 644 + 645 + /* make sure to handle the correct frame type (CAN / CAN FD) */ 646 + if (skb->len != op->cfsiz) 647 + return; 648 + 649 + /* disable timeout */ 650 + hrtimer_cancel(&op->timer); 664 651 665 652 /* save rx timestamp */ 666 653 op->rx_stamp = skb->tstamp; ··· 696 675 * multiplex compare 697 676 * 698 677 * find the first multiplex mask that fits. 699 - * Remark: The MUX-mask is stored in index 0 678 + * Remark: The MUX-mask is stored in index 0 - but only the 679 + * first 64 bits of the frame data[] are relevant (CAN FD) 700 680 */ 701 681 702 682 for (i = 1; i < op->nframes; i++) { 703 - if ((GET_U64(&op->frames[0]) & GET_U64(rxframe)) == 704 - (GET_U64(&op->frames[0]) & 705 - GET_U64(&op->frames[i]))) { 683 + if ((get_u64(op->frames, 0) & get_u64(rxframe, 0)) == 684 + (get_u64(op->frames, 0) & 685 + get_u64(op->frames + op->cfsiz * i, 0))) { 706 686 bcm_rx_cmp_to_index(op, i, rxframe); 707 687 break; 708 688 } ··· 717 695 /* 718 696 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements 719 697 */ 720 - static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id, 721 - int ifindex) 698 + static struct bcm_op *bcm_find_op(struct list_head *ops, 699 + struct bcm_msg_head *mh, int ifindex) 722 700 { 723 701 struct bcm_op *op; 724 702 725 703 list_for_each_entry(op, ops, list) { 726 - if ((op->can_id == can_id) && (op->ifindex == ifindex)) 704 + if ((op->can_id == mh->can_id) && (op->ifindex == ifindex) && 705 + (op->flags & CAN_FD_FRAME) == (mh->flags & CAN_FD_FRAME)) 727 706 return op; 728 707 } 729 708 ··· 767 744 /* 768 745 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops) 769 746 */ 770 - static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex) 747 + static int bcm_delete_rx_op(struct list_head *ops, struct bcm_msg_head *mh, 748 + int ifindex) 771 749 { 772 750 struct bcm_op *op, *n; 773 751 774 752 list_for_each_entry_safe(op, n, ops, list) { 775 - if ((op->can_id == can_id) && (op->ifindex == ifindex)) { 753 + if ((op->can_id == mh->can_id) && (op->ifindex == ifindex) && 754 + (op->flags & CAN_FD_FRAME) == (mh->flags & CAN_FD_FRAME)) { 776 755 777 756 /* 778 757 * Don't care if we're bound or not (due to netdev ··· 814 789 /* 815 790 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops) 816 791 */ 817 - static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex) 792 + static int bcm_delete_tx_op(struct list_head *ops, struct bcm_msg_head *mh, 793 + int ifindex) 818 794 { 819 795 struct bcm_op *op, *n; 820 796 821 797 list_for_each_entry_safe(op, n, ops, list) { 822 - if ((op->can_id == can_id) && (op->ifindex == ifindex)) { 798 + if ((op->can_id == mh->can_id) && (op->ifindex == ifindex) && 799 + (op->flags & CAN_FD_FRAME) == (mh->flags & CAN_FD_FRAME)) { 823 800 list_del(&op->list); 824 801 bcm_remove_op(op); 825 802 return 1; /* done */ ··· 837 810 static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head, 838 811 int ifindex) 839 812 { 840 - struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex); 813 + struct bcm_op *op = bcm_find_op(ops, msg_head, ifindex); 841 814 842 815 if (!op) 843 816 return -EINVAL; ··· 862 835 { 863 836 struct bcm_sock *bo = bcm_sk(sk); 864 837 struct bcm_op *op; 838 + struct canfd_frame *cf; 865 839 unsigned int i; 866 840 int err; 867 841 ··· 870 842 if (!ifindex) 871 843 return -ENODEV; 872 844 873 - /* check nframes boundaries - we need at least one can_frame */ 845 + /* check nframes boundaries - we need at least one CAN frame */ 874 846 if (msg_head->nframes < 1 || msg_head->nframes > MAX_NFRAMES) 875 847 return -EINVAL; 876 848 877 849 /* check the given can_id */ 878 - op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex); 879 - 850 + op = bcm_find_op(&bo->tx_ops, msg_head, ifindex); 880 851 if (op) { 881 852 /* update existing BCM operation */ 882 853 883 854 /* 884 - * Do we need more space for the can_frames than currently 855 + * Do we need more space for the CAN frames than currently 885 856 * allocated? -> This is a _really_ unusual use-case and 886 857 * therefore (complexity / locking) it is not supported. 887 858 */ 888 859 if (msg_head->nframes > op->nframes) 889 860 return -E2BIG; 890 861 891 - /* update can_frames content */ 862 + /* update CAN frames content */ 892 863 for (i = 0; i < msg_head->nframes; i++) { 893 - err = memcpy_from_msg((u8 *)&op->frames[i], msg, CFSIZ); 894 864 895 - if (op->frames[i].can_dlc > 8) 896 - err = -EINVAL; 865 + cf = op->frames + op->cfsiz * i; 866 + err = memcpy_from_msg((u8 *)cf, msg, op->cfsiz); 867 + 868 + if (op->flags & CAN_FD_FRAME) { 869 + if (cf->len > 64) 870 + err = -EINVAL; 871 + } else { 872 + if (cf->len > 8) 873 + err = -EINVAL; 874 + } 897 875 898 876 if (err < 0) 899 877 return err; 900 878 901 879 if (msg_head->flags & TX_CP_CAN_ID) { 902 880 /* copy can_id into frame */ 903 - op->frames[i].can_id = msg_head->can_id; 881 + cf->can_id = msg_head->can_id; 904 882 } 905 883 } 884 + op->flags = msg_head->flags; 906 885 907 886 } else { 908 887 /* insert new BCM operation for the given can_id */ ··· 918 883 if (!op) 919 884 return -ENOMEM; 920 885 921 - op->can_id = msg_head->can_id; 886 + op->can_id = msg_head->can_id; 887 + op->cfsiz = CFSIZ(msg_head->flags); 888 + op->flags = msg_head->flags; 922 889 923 - /* create array for can_frames and copy the data */ 890 + /* create array for CAN frames and copy the data */ 924 891 if (msg_head->nframes > 1) { 925 - op->frames = kmalloc(msg_head->nframes * CFSIZ, 892 + op->frames = kmalloc(msg_head->nframes * op->cfsiz, 926 893 GFP_KERNEL); 927 894 if (!op->frames) { 928 895 kfree(op); ··· 934 897 op->frames = &op->sframe; 935 898 936 899 for (i = 0; i < msg_head->nframes; i++) { 937 - err = memcpy_from_msg((u8 *)&op->frames[i], msg, CFSIZ); 938 900 939 - if (op->frames[i].can_dlc > 8) 940 - err = -EINVAL; 901 + cf = op->frames + op->cfsiz * i; 902 + err = memcpy_from_msg((u8 *)cf, msg, op->cfsiz); 903 + 904 + if (op->flags & CAN_FD_FRAME) { 905 + if (cf->len > 64) 906 + err = -EINVAL; 907 + } else { 908 + if (cf->len > 8) 909 + err = -EINVAL; 910 + } 941 911 942 912 if (err < 0) { 943 913 if (op->frames != &op->sframe) ··· 955 911 956 912 if (msg_head->flags & TX_CP_CAN_ID) { 957 913 /* copy can_id into frame */ 958 - op->frames[i].can_id = msg_head->can_id; 914 + cf->can_id = msg_head->can_id; 959 915 } 960 916 } 961 917 ··· 990 946 991 947 /* check flags */ 992 948 993 - op->flags = msg_head->flags; 994 - 995 949 if (op->flags & TX_RESET_MULTI_IDX) { 996 950 /* start multiple frame transmission with index 0 */ 997 951 op->currframe = 0; ··· 1010 968 1011 969 if (op->flags & STARTTIMER) { 1012 970 hrtimer_cancel(&op->timer); 1013 - /* spec: send can_frame when starting timer */ 971 + /* spec: send CAN frame when starting timer */ 1014 972 op->flags |= TX_ANNOUNCE; 1015 973 } 1016 974 ··· 1023 981 if (op->flags & STARTTIMER) 1024 982 bcm_tx_start_timer(op); 1025 983 1026 - return msg_head->nframes * CFSIZ + MHSIZ; 984 + return msg_head->nframes * op->cfsiz + MHSIZ; 1027 985 } 1028 986 1029 987 /* ··· 1054 1012 return -EINVAL; 1055 1013 1056 1014 /* check the given can_id */ 1057 - op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex); 1015 + op = bcm_find_op(&bo->rx_ops, msg_head, ifindex); 1058 1016 if (op) { 1059 1017 /* update existing BCM operation */ 1060 1018 1061 1019 /* 1062 - * Do we need more space for the can_frames than currently 1020 + * Do we need more space for the CAN frames than currently 1063 1021 * allocated? -> This is a _really_ unusual use-case and 1064 1022 * therefore (complexity / locking) it is not supported. 1065 1023 */ ··· 1067 1025 return -E2BIG; 1068 1026 1069 1027 if (msg_head->nframes) { 1070 - /* update can_frames content */ 1028 + /* update CAN frames content */ 1071 1029 err = memcpy_from_msg((u8 *)op->frames, msg, 1072 - msg_head->nframes * CFSIZ); 1030 + msg_head->nframes * op->cfsiz); 1073 1031 if (err < 0) 1074 1032 return err; 1075 1033 1076 1034 /* clear last_frames to indicate 'nothing received' */ 1077 - memset(op->last_frames, 0, msg_head->nframes * CFSIZ); 1035 + memset(op->last_frames, 0, msg_head->nframes * op->cfsiz); 1078 1036 } 1079 1037 1080 1038 op->nframes = msg_head->nframes; 1039 + op->flags = msg_head->flags; 1081 1040 1082 1041 /* Only an update -> do not call can_rx_register() */ 1083 1042 do_rx_register = 0; ··· 1089 1046 if (!op) 1090 1047 return -ENOMEM; 1091 1048 1092 - op->can_id = msg_head->can_id; 1093 - op->nframes = msg_head->nframes; 1049 + op->can_id = msg_head->can_id; 1050 + op->nframes = msg_head->nframes; 1051 + op->cfsiz = CFSIZ(msg_head->flags); 1052 + op->flags = msg_head->flags; 1094 1053 1095 1054 if (msg_head->nframes > 1) { 1096 - /* create array for can_frames and copy the data */ 1097 - op->frames = kmalloc(msg_head->nframes * CFSIZ, 1055 + /* create array for CAN frames and copy the data */ 1056 + op->frames = kmalloc(msg_head->nframes * op->cfsiz, 1098 1057 GFP_KERNEL); 1099 1058 if (!op->frames) { 1100 1059 kfree(op); 1101 1060 return -ENOMEM; 1102 1061 } 1103 1062 1104 - /* create and init array for received can_frames */ 1105 - op->last_frames = kzalloc(msg_head->nframes * CFSIZ, 1063 + /* create and init array for received CAN frames */ 1064 + op->last_frames = kzalloc(msg_head->nframes * op->cfsiz, 1106 1065 GFP_KERNEL); 1107 1066 if (!op->last_frames) { 1108 1067 kfree(op->frames); ··· 1119 1074 1120 1075 if (msg_head->nframes) { 1121 1076 err = memcpy_from_msg((u8 *)op->frames, msg, 1122 - msg_head->nframes * CFSIZ); 1077 + msg_head->nframes * op->cfsiz); 1123 1078 if (err < 0) { 1124 1079 if (op->frames != &op->sframe) 1125 1080 kfree(op->frames); ··· 1161 1116 } /* if ((op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex))) */ 1162 1117 1163 1118 /* check flags */ 1164 - op->flags = msg_head->flags; 1165 1119 1166 1120 if (op->flags & RX_RTR_FRAME) { 1167 1121 ··· 1232 1188 } 1233 1189 } 1234 1190 1235 - return msg_head->nframes * CFSIZ + MHSIZ; 1191 + return msg_head->nframes * op->cfsiz + MHSIZ; 1236 1192 } 1237 1193 1238 1194 /* 1239 1195 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg) 1240 1196 */ 1241 - static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk) 1197 + static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk, 1198 + int cfsiz) 1242 1199 { 1243 1200 struct sk_buff *skb; 1244 1201 struct net_device *dev; ··· 1249 1204 if (!ifindex) 1250 1205 return -ENODEV; 1251 1206 1252 - skb = alloc_skb(CFSIZ + sizeof(struct can_skb_priv), GFP_KERNEL); 1207 + skb = alloc_skb(cfsiz + sizeof(struct can_skb_priv), GFP_KERNEL); 1253 1208 if (!skb) 1254 1209 return -ENOMEM; 1255 1210 1256 1211 can_skb_reserve(skb); 1257 1212 1258 - err = memcpy_from_msg(skb_put(skb, CFSIZ), msg, CFSIZ); 1213 + err = memcpy_from_msg(skb_put(skb, cfsiz), msg, cfsiz); 1259 1214 if (err < 0) { 1260 1215 kfree_skb(skb); 1261 1216 return err; ··· 1277 1232 if (err) 1278 1233 return err; 1279 1234 1280 - return CFSIZ + MHSIZ; 1235 + return cfsiz + MHSIZ; 1281 1236 } 1282 1237 1283 1238 /* ··· 1289 1244 struct bcm_sock *bo = bcm_sk(sk); 1290 1245 int ifindex = bo->ifindex; /* default ifindex for this bcm_op */ 1291 1246 struct bcm_msg_head msg_head; 1247 + int cfsiz; 1292 1248 int ret; /* read bytes or error codes as return value */ 1293 1249 1294 1250 if (!bo->bound) 1295 1251 return -ENOTCONN; 1296 1252 1297 1253 /* check for valid message length from userspace */ 1298 - if (size < MHSIZ || (size - MHSIZ) % CFSIZ) 1254 + if (size < MHSIZ) 1255 + return -EINVAL; 1256 + 1257 + /* read message head information */ 1258 + ret = memcpy_from_msg((u8 *)&msg_head, msg, MHSIZ); 1259 + if (ret < 0) 1260 + return ret; 1261 + 1262 + cfsiz = CFSIZ(msg_head.flags); 1263 + if ((size - MHSIZ) % cfsiz) 1299 1264 return -EINVAL; 1300 1265 1301 1266 /* check for alternative ifindex for this bcm_op */ ··· 1339 1284 } 1340 1285 } 1341 1286 1342 - /* read message head information */ 1343 - 1344 - ret = memcpy_from_msg((u8 *)&msg_head, msg, MHSIZ); 1345 - if (ret < 0) 1346 - return ret; 1347 - 1348 1287 lock_sock(sk); 1349 1288 1350 1289 switch (msg_head.opcode) { ··· 1352 1303 break; 1353 1304 1354 1305 case TX_DELETE: 1355 - if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex)) 1306 + if (bcm_delete_tx_op(&bo->tx_ops, &msg_head, ifindex)) 1356 1307 ret = MHSIZ; 1357 1308 else 1358 1309 ret = -EINVAL; 1359 1310 break; 1360 1311 1361 1312 case RX_DELETE: 1362 - if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex)) 1313 + if (bcm_delete_rx_op(&bo->rx_ops, &msg_head, ifindex)) 1363 1314 ret = MHSIZ; 1364 1315 else 1365 1316 ret = -EINVAL; ··· 1378 1329 break; 1379 1330 1380 1331 case TX_SEND: 1381 - /* we need exactly one can_frame behind the msg head */ 1382 - if ((msg_head.nframes != 1) || (size != CFSIZ + MHSIZ)) 1332 + /* we need exactly one CAN frame behind the msg head */ 1333 + if ((msg_head.nframes != 1) || (size != cfsiz + MHSIZ)) 1383 1334 ret = -EINVAL; 1384 1335 else 1385 - ret = bcm_tx_send(msg, ifindex, sk); 1336 + ret = bcm_tx_send(msg, ifindex, sk, cfsiz); 1386 1337 break; 1387 1338 1388 1339 default:
+1 -2
net/can/proc.c
··· 517 517 can_dir = proc_mkdir("can", init_net.proc_net); 518 518 519 519 if (!can_dir) { 520 - printk(KERN_INFO "can: failed to create /proc/net/can . " 521 - "CONFIG_PROC_FS missing?\n"); 520 + pr_info("can: failed to create /proc/net/can.\n"); 522 521 return; 523 522 } 524 523