18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Socket CAN driver for Aeroflex Gaisler GRCAN and GRHCAN.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * 2012 (c) Aeroflex Gaisler AB
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * This driver supports GRCAN and GRHCAN CAN controllers available in the GRLIB
88c2ecf20Sopenharmony_ci * VHDL IP core library.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * Full documentation of the GRCAN core can be found here:
118c2ecf20Sopenharmony_ci * http://www.gaisler.com/products/grlib/grip.pdf
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * See "Documentation/devicetree/bindings/net/can/grcan.txt" for information on
148c2ecf20Sopenharmony_ci * open firmware properties.
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * See "Documentation/ABI/testing/sysfs-class-net-grcan" for information on the
178c2ecf20Sopenharmony_ci * sysfs interface.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * See "Documentation/admin-guide/kernel-parameters.rst" for information on the module
208c2ecf20Sopenharmony_ci * parameters.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * Contributors: Andreas Larsson <andreas@gaisler.com>
238c2ecf20Sopenharmony_ci */
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include <linux/kernel.h>
268c2ecf20Sopenharmony_ci#include <linux/module.h>
278c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
288c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
298c2ecf20Sopenharmony_ci#include <linux/delay.h>
308c2ecf20Sopenharmony_ci#include <linux/io.h>
318c2ecf20Sopenharmony_ci#include <linux/can/dev.h>
328c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
338c2ecf20Sopenharmony_ci#include <linux/of_platform.h>
348c2ecf20Sopenharmony_ci#include <linux/of_irq.h>
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#define DRV_NAME	"grcan"
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#define GRCAN_NAPI_WEIGHT	32
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define GRCAN_RESERVE_SIZE(slot1, slot2) (((slot2) - (slot1)) / 4 - 1)
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistruct grcan_registers {
458c2ecf20Sopenharmony_ci	u32 conf;	/* 0x00 */
468c2ecf20Sopenharmony_ci	u32 stat;	/* 0x04 */
478c2ecf20Sopenharmony_ci	u32 ctrl;	/* 0x08 */
488c2ecf20Sopenharmony_ci	u32 __reserved1[GRCAN_RESERVE_SIZE(0x08, 0x18)];
498c2ecf20Sopenharmony_ci	u32 smask;	/* 0x18 - CanMASK */
508c2ecf20Sopenharmony_ci	u32 scode;	/* 0x1c - CanCODE */
518c2ecf20Sopenharmony_ci	u32 __reserved2[GRCAN_RESERVE_SIZE(0x1c, 0x100)];
528c2ecf20Sopenharmony_ci	u32 pimsr;	/* 0x100 */
538c2ecf20Sopenharmony_ci	u32 pimr;	/* 0x104 */
548c2ecf20Sopenharmony_ci	u32 pisr;	/* 0x108 */
558c2ecf20Sopenharmony_ci	u32 pir;	/* 0x10C */
568c2ecf20Sopenharmony_ci	u32 imr;	/* 0x110 */
578c2ecf20Sopenharmony_ci	u32 picr;	/* 0x114 */
588c2ecf20Sopenharmony_ci	u32 __reserved3[GRCAN_RESERVE_SIZE(0x114, 0x200)];
598c2ecf20Sopenharmony_ci	u32 txctrl;	/* 0x200 */
608c2ecf20Sopenharmony_ci	u32 txaddr;	/* 0x204 */
618c2ecf20Sopenharmony_ci	u32 txsize;	/* 0x208 */
628c2ecf20Sopenharmony_ci	u32 txwr;	/* 0x20C */
638c2ecf20Sopenharmony_ci	u32 txrd;	/* 0x210 */
648c2ecf20Sopenharmony_ci	u32 txirq;	/* 0x214 */
658c2ecf20Sopenharmony_ci	u32 __reserved4[GRCAN_RESERVE_SIZE(0x214, 0x300)];
668c2ecf20Sopenharmony_ci	u32 rxctrl;	/* 0x300 */
678c2ecf20Sopenharmony_ci	u32 rxaddr;	/* 0x304 */
688c2ecf20Sopenharmony_ci	u32 rxsize;	/* 0x308 */
698c2ecf20Sopenharmony_ci	u32 rxwr;	/* 0x30C */
708c2ecf20Sopenharmony_ci	u32 rxrd;	/* 0x310 */
718c2ecf20Sopenharmony_ci	u32 rxirq;	/* 0x314 */
728c2ecf20Sopenharmony_ci	u32 rxmask;	/* 0x318 */
738c2ecf20Sopenharmony_ci	u32 rxcode;	/* 0x31C */
748c2ecf20Sopenharmony_ci};
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci#define GRCAN_CONF_ABORT	0x00000001
778c2ecf20Sopenharmony_ci#define GRCAN_CONF_ENABLE0	0x00000002
788c2ecf20Sopenharmony_ci#define GRCAN_CONF_ENABLE1	0x00000004
798c2ecf20Sopenharmony_ci#define GRCAN_CONF_SELECT	0x00000008
808c2ecf20Sopenharmony_ci#define GRCAN_CONF_SILENT	0x00000010
818c2ecf20Sopenharmony_ci#define GRCAN_CONF_SAM		0x00000020 /* Available in some hardware */
828c2ecf20Sopenharmony_ci#define GRCAN_CONF_BPR		0x00000300 /* Note: not BRP */
838c2ecf20Sopenharmony_ci#define GRCAN_CONF_RSJ		0x00007000
848c2ecf20Sopenharmony_ci#define GRCAN_CONF_PS1		0x00f00000
858c2ecf20Sopenharmony_ci#define GRCAN_CONF_PS2		0x000f0000
868c2ecf20Sopenharmony_ci#define GRCAN_CONF_SCALER	0xff000000
878c2ecf20Sopenharmony_ci#define GRCAN_CONF_OPERATION						\
888c2ecf20Sopenharmony_ci	(GRCAN_CONF_ABORT | GRCAN_CONF_ENABLE0 | GRCAN_CONF_ENABLE1	\
898c2ecf20Sopenharmony_ci	 | GRCAN_CONF_SELECT | GRCAN_CONF_SILENT | GRCAN_CONF_SAM)
908c2ecf20Sopenharmony_ci#define GRCAN_CONF_TIMING						\
918c2ecf20Sopenharmony_ci	(GRCAN_CONF_BPR | GRCAN_CONF_RSJ | GRCAN_CONF_PS1		\
928c2ecf20Sopenharmony_ci	 | GRCAN_CONF_PS2 | GRCAN_CONF_SCALER)
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci#define GRCAN_CONF_RSJ_MIN	1
958c2ecf20Sopenharmony_ci#define GRCAN_CONF_RSJ_MAX	4
968c2ecf20Sopenharmony_ci#define GRCAN_CONF_PS1_MIN	1
978c2ecf20Sopenharmony_ci#define GRCAN_CONF_PS1_MAX	15
988c2ecf20Sopenharmony_ci#define GRCAN_CONF_PS2_MIN	2
998c2ecf20Sopenharmony_ci#define GRCAN_CONF_PS2_MAX	8
1008c2ecf20Sopenharmony_ci#define GRCAN_CONF_SCALER_MIN	0
1018c2ecf20Sopenharmony_ci#define GRCAN_CONF_SCALER_MAX	255
1028c2ecf20Sopenharmony_ci#define GRCAN_CONF_SCALER_INC	1
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci#define GRCAN_CONF_BPR_BIT	8
1058c2ecf20Sopenharmony_ci#define GRCAN_CONF_RSJ_BIT	12
1068c2ecf20Sopenharmony_ci#define GRCAN_CONF_PS1_BIT	20
1078c2ecf20Sopenharmony_ci#define GRCAN_CONF_PS2_BIT	16
1088c2ecf20Sopenharmony_ci#define GRCAN_CONF_SCALER_BIT	24
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci#define GRCAN_STAT_PASS		0x000001
1118c2ecf20Sopenharmony_ci#define GRCAN_STAT_OFF		0x000002
1128c2ecf20Sopenharmony_ci#define GRCAN_STAT_OR		0x000004
1138c2ecf20Sopenharmony_ci#define GRCAN_STAT_AHBERR	0x000008
1148c2ecf20Sopenharmony_ci#define GRCAN_STAT_ACTIVE	0x000010
1158c2ecf20Sopenharmony_ci#define GRCAN_STAT_RXERRCNT	0x00ff00
1168c2ecf20Sopenharmony_ci#define GRCAN_STAT_TXERRCNT	0xff0000
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci#define GRCAN_STAT_ERRCTR_RELATED	(GRCAN_STAT_PASS | GRCAN_STAT_OFF)
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci#define GRCAN_STAT_RXERRCNT_BIT	8
1218c2ecf20Sopenharmony_ci#define GRCAN_STAT_TXERRCNT_BIT	16
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci#define GRCAN_STAT_ERRCNT_WARNING_LIMIT	96
1248c2ecf20Sopenharmony_ci#define GRCAN_STAT_ERRCNT_PASSIVE_LIMIT	127
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci#define GRCAN_CTRL_RESET	0x2
1278c2ecf20Sopenharmony_ci#define GRCAN_CTRL_ENABLE	0x1
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci#define GRCAN_TXCTRL_ENABLE	0x1
1308c2ecf20Sopenharmony_ci#define GRCAN_TXCTRL_ONGOING	0x2
1318c2ecf20Sopenharmony_ci#define GRCAN_TXCTRL_SINGLE	0x4
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci#define GRCAN_RXCTRL_ENABLE	0x1
1348c2ecf20Sopenharmony_ci#define GRCAN_RXCTRL_ONGOING	0x2
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci/* Relative offset of IRQ sources to AMBA Plug&Play */
1378c2ecf20Sopenharmony_ci#define GRCAN_IRQIX_IRQ		0
1388c2ecf20Sopenharmony_ci#define GRCAN_IRQIX_TXSYNC	1
1398c2ecf20Sopenharmony_ci#define GRCAN_IRQIX_RXSYNC	2
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci#define GRCAN_IRQ_PASS		0x00001
1428c2ecf20Sopenharmony_ci#define GRCAN_IRQ_OFF		0x00002
1438c2ecf20Sopenharmony_ci#define GRCAN_IRQ_OR		0x00004
1448c2ecf20Sopenharmony_ci#define GRCAN_IRQ_RXAHBERR	0x00008
1458c2ecf20Sopenharmony_ci#define GRCAN_IRQ_TXAHBERR	0x00010
1468c2ecf20Sopenharmony_ci#define GRCAN_IRQ_RXIRQ		0x00020
1478c2ecf20Sopenharmony_ci#define GRCAN_IRQ_TXIRQ		0x00040
1488c2ecf20Sopenharmony_ci#define GRCAN_IRQ_RXFULL	0x00080
1498c2ecf20Sopenharmony_ci#define GRCAN_IRQ_TXEMPTY	0x00100
1508c2ecf20Sopenharmony_ci#define GRCAN_IRQ_RX		0x00200
1518c2ecf20Sopenharmony_ci#define GRCAN_IRQ_TX		0x00400
1528c2ecf20Sopenharmony_ci#define GRCAN_IRQ_RXSYNC	0x00800
1538c2ecf20Sopenharmony_ci#define GRCAN_IRQ_TXSYNC	0x01000
1548c2ecf20Sopenharmony_ci#define GRCAN_IRQ_RXERRCTR	0x02000
1558c2ecf20Sopenharmony_ci#define GRCAN_IRQ_TXERRCTR	0x04000
1568c2ecf20Sopenharmony_ci#define GRCAN_IRQ_RXMISS	0x08000
1578c2ecf20Sopenharmony_ci#define GRCAN_IRQ_TXLOSS	0x10000
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci#define GRCAN_IRQ_NONE	0
1608c2ecf20Sopenharmony_ci#define GRCAN_IRQ_ALL							\
1618c2ecf20Sopenharmony_ci	(GRCAN_IRQ_PASS | GRCAN_IRQ_OFF | GRCAN_IRQ_OR			\
1628c2ecf20Sopenharmony_ci	 | GRCAN_IRQ_RXAHBERR | GRCAN_IRQ_TXAHBERR			\
1638c2ecf20Sopenharmony_ci	 | GRCAN_IRQ_RXIRQ | GRCAN_IRQ_TXIRQ				\
1648c2ecf20Sopenharmony_ci	 | GRCAN_IRQ_RXFULL | GRCAN_IRQ_TXEMPTY				\
1658c2ecf20Sopenharmony_ci	 | GRCAN_IRQ_RX | GRCAN_IRQ_TX | GRCAN_IRQ_RXSYNC		\
1668c2ecf20Sopenharmony_ci	 | GRCAN_IRQ_TXSYNC | GRCAN_IRQ_RXERRCTR			\
1678c2ecf20Sopenharmony_ci	 | GRCAN_IRQ_TXERRCTR | GRCAN_IRQ_RXMISS			\
1688c2ecf20Sopenharmony_ci	 | GRCAN_IRQ_TXLOSS)
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci#define GRCAN_IRQ_ERRCTR_RELATED (GRCAN_IRQ_RXERRCTR | GRCAN_IRQ_TXERRCTR \
1718c2ecf20Sopenharmony_ci				  | GRCAN_IRQ_PASS | GRCAN_IRQ_OFF)
1728c2ecf20Sopenharmony_ci#define GRCAN_IRQ_ERRORS (GRCAN_IRQ_ERRCTR_RELATED | GRCAN_IRQ_OR	\
1738c2ecf20Sopenharmony_ci			  | GRCAN_IRQ_TXAHBERR | GRCAN_IRQ_RXAHBERR	\
1748c2ecf20Sopenharmony_ci			  | GRCAN_IRQ_TXLOSS)
1758c2ecf20Sopenharmony_ci#define GRCAN_IRQ_DEFAULT (GRCAN_IRQ_RX | GRCAN_IRQ_TX | GRCAN_IRQ_ERRORS)
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci#define GRCAN_MSG_SIZE		16
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci#define GRCAN_MSG_IDE		0x80000000
1808c2ecf20Sopenharmony_ci#define GRCAN_MSG_RTR		0x40000000
1818c2ecf20Sopenharmony_ci#define GRCAN_MSG_BID		0x1ffc0000
1828c2ecf20Sopenharmony_ci#define GRCAN_MSG_EID		0x1fffffff
1838c2ecf20Sopenharmony_ci#define GRCAN_MSG_IDE_BIT	31
1848c2ecf20Sopenharmony_ci#define GRCAN_MSG_RTR_BIT	30
1858c2ecf20Sopenharmony_ci#define GRCAN_MSG_BID_BIT	18
1868c2ecf20Sopenharmony_ci#define GRCAN_MSG_EID_BIT	0
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci#define GRCAN_MSG_DLC		0xf0000000
1898c2ecf20Sopenharmony_ci#define GRCAN_MSG_TXERRC	0x00ff0000
1908c2ecf20Sopenharmony_ci#define GRCAN_MSG_RXERRC	0x0000ff00
1918c2ecf20Sopenharmony_ci#define GRCAN_MSG_DLC_BIT	28
1928c2ecf20Sopenharmony_ci#define GRCAN_MSG_TXERRC_BIT	16
1938c2ecf20Sopenharmony_ci#define GRCAN_MSG_RXERRC_BIT	8
1948c2ecf20Sopenharmony_ci#define GRCAN_MSG_AHBERR	0x00000008
1958c2ecf20Sopenharmony_ci#define GRCAN_MSG_OR		0x00000004
1968c2ecf20Sopenharmony_ci#define GRCAN_MSG_OFF		0x00000002
1978c2ecf20Sopenharmony_ci#define GRCAN_MSG_PASS		0x00000001
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci#define GRCAN_MSG_DATA_SLOT_INDEX(i) (2 + (i) / 4)
2008c2ecf20Sopenharmony_ci#define GRCAN_MSG_DATA_SHIFT(i) ((3 - (i) % 4) * 8)
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci#define GRCAN_BUFFER_ALIGNMENT		1024
2038c2ecf20Sopenharmony_ci#define GRCAN_DEFAULT_BUFFER_SIZE	1024
2048c2ecf20Sopenharmony_ci#define GRCAN_VALID_TR_SIZE_MASK	0x001fffc0
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci#define GRCAN_INVALID_BUFFER_SIZE(s)			\
2078c2ecf20Sopenharmony_ci	((s) == 0 || ((s) & ~GRCAN_VALID_TR_SIZE_MASK))
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci#if GRCAN_INVALID_BUFFER_SIZE(GRCAN_DEFAULT_BUFFER_SIZE)
2108c2ecf20Sopenharmony_ci#error "Invalid default buffer size"
2118c2ecf20Sopenharmony_ci#endif
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistruct grcan_dma_buffer {
2148c2ecf20Sopenharmony_ci	size_t size;
2158c2ecf20Sopenharmony_ci	void *buf;
2168c2ecf20Sopenharmony_ci	dma_addr_t handle;
2178c2ecf20Sopenharmony_ci};
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_cistruct grcan_dma {
2208c2ecf20Sopenharmony_ci	size_t base_size;
2218c2ecf20Sopenharmony_ci	void *base_buf;
2228c2ecf20Sopenharmony_ci	dma_addr_t base_handle;
2238c2ecf20Sopenharmony_ci	struct grcan_dma_buffer tx;
2248c2ecf20Sopenharmony_ci	struct grcan_dma_buffer rx;
2258c2ecf20Sopenharmony_ci};
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci/* GRCAN configuration parameters */
2288c2ecf20Sopenharmony_cistruct grcan_device_config {
2298c2ecf20Sopenharmony_ci	unsigned short enable0;
2308c2ecf20Sopenharmony_ci	unsigned short enable1;
2318c2ecf20Sopenharmony_ci	unsigned short select;
2328c2ecf20Sopenharmony_ci	unsigned int txsize;
2338c2ecf20Sopenharmony_ci	unsigned int rxsize;
2348c2ecf20Sopenharmony_ci};
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci#define GRCAN_DEFAULT_DEVICE_CONFIG {				\
2378c2ecf20Sopenharmony_ci		.enable0	= 0,				\
2388c2ecf20Sopenharmony_ci		.enable1	= 0,				\
2398c2ecf20Sopenharmony_ci		.select		= 0,				\
2408c2ecf20Sopenharmony_ci		.txsize		= GRCAN_DEFAULT_BUFFER_SIZE,	\
2418c2ecf20Sopenharmony_ci		.rxsize		= GRCAN_DEFAULT_BUFFER_SIZE,	\
2428c2ecf20Sopenharmony_ci		}
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci#define GRCAN_TXBUG_SAFE_GRLIB_VERSION	4100
2458c2ecf20Sopenharmony_ci#define GRLIB_VERSION_MASK		0xffff
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci/* GRCAN private data structure */
2488c2ecf20Sopenharmony_cistruct grcan_priv {
2498c2ecf20Sopenharmony_ci	struct can_priv can;	/* must be the first member */
2508c2ecf20Sopenharmony_ci	struct net_device *dev;
2518c2ecf20Sopenharmony_ci	struct device *ofdev_dev;
2528c2ecf20Sopenharmony_ci	struct napi_struct napi;
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	struct grcan_registers __iomem *regs;	/* ioremap'ed registers */
2558c2ecf20Sopenharmony_ci	struct grcan_device_config config;
2568c2ecf20Sopenharmony_ci	struct grcan_dma dma;
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	struct sk_buff **echo_skb;	/* We allocate this on our own */
2598c2ecf20Sopenharmony_ci	u8 *txdlc;			/* Length of queued frames */
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	/* The echo skb pointer, pointing into echo_skb and indicating which
2628c2ecf20Sopenharmony_ci	 * frames can be echoed back. See the "Notes on the tx cyclic buffer
2638c2ecf20Sopenharmony_ci	 * handling"-comment for grcan_start_xmit for more details.
2648c2ecf20Sopenharmony_ci	 */
2658c2ecf20Sopenharmony_ci	u32 eskbp;
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	/* Lock for controlling changes to the netif tx queue state, accesses to
2688c2ecf20Sopenharmony_ci	 * the echo_skb pointer eskbp and for making sure that a running reset
2698c2ecf20Sopenharmony_ci	 * and/or a close of the interface is done without interference from
2708c2ecf20Sopenharmony_ci	 * other parts of the code.
2718c2ecf20Sopenharmony_ci	 *
2728c2ecf20Sopenharmony_ci	 * The echo_skb pointer, eskbp, should only be accessed under this lock
2738c2ecf20Sopenharmony_ci	 * as it can be changed in several places and together with decisions on
2748c2ecf20Sopenharmony_ci	 * whether to wake up the tx queue.
2758c2ecf20Sopenharmony_ci	 *
2768c2ecf20Sopenharmony_ci	 * The tx queue must never be woken up if there is a running reset or
2778c2ecf20Sopenharmony_ci	 * close in progress.
2788c2ecf20Sopenharmony_ci	 *
2798c2ecf20Sopenharmony_ci	 * A running reset (see below on need_txbug_workaround) should never be
2808c2ecf20Sopenharmony_ci	 * done if the interface is closing down and several running resets
2818c2ecf20Sopenharmony_ci	 * should never be scheduled simultaneously.
2828c2ecf20Sopenharmony_ci	 */
2838c2ecf20Sopenharmony_ci	spinlock_t lock;
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	/* Whether a workaround is needed due to a bug in older hardware. In
2868c2ecf20Sopenharmony_ci	 * this case, the driver both tries to prevent the bug from being
2878c2ecf20Sopenharmony_ci	 * triggered and recovers, if the bug nevertheless happens, by doing a
2888c2ecf20Sopenharmony_ci	 * running reset. A running reset, resets the device and continues from
2898c2ecf20Sopenharmony_ci	 * where it were without being noticeable from outside the driver (apart
2908c2ecf20Sopenharmony_ci	 * from slight delays).
2918c2ecf20Sopenharmony_ci	 */
2928c2ecf20Sopenharmony_ci	bool need_txbug_workaround;
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	/* To trigger initization of running reset and to trigger running reset
2958c2ecf20Sopenharmony_ci	 * respectively in the case of a hanged device due to a txbug.
2968c2ecf20Sopenharmony_ci	 */
2978c2ecf20Sopenharmony_ci	struct timer_list hang_timer;
2988c2ecf20Sopenharmony_ci	struct timer_list rr_timer;
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	/* To avoid waking up the netif queue and restarting timers
3018c2ecf20Sopenharmony_ci	 * when a reset is scheduled or when closing of the device is
3028c2ecf20Sopenharmony_ci	 * undergoing
3038c2ecf20Sopenharmony_ci	 */
3048c2ecf20Sopenharmony_ci	bool resetting;
3058c2ecf20Sopenharmony_ci	bool closing;
3068c2ecf20Sopenharmony_ci};
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci/* Wait time for a short wait for ongoing to clear */
3098c2ecf20Sopenharmony_ci#define GRCAN_SHORTWAIT_USECS	10
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci/* Limit on the number of transmitted bits of an eff frame according to the CAN
3128c2ecf20Sopenharmony_ci * specification: 1 bit start of frame, 32 bits arbitration field, 6 bits
3138c2ecf20Sopenharmony_ci * control field, 8 bytes data field, 16 bits crc field, 2 bits ACK field and 7
3148c2ecf20Sopenharmony_ci * bits end of frame
3158c2ecf20Sopenharmony_ci */
3168c2ecf20Sopenharmony_ci#define GRCAN_EFF_FRAME_MAX_BITS	(1+32+6+8*8+16+2+7)
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci#if defined(__BIG_ENDIAN)
3198c2ecf20Sopenharmony_cistatic inline u32 grcan_read_reg(u32 __iomem *reg)
3208c2ecf20Sopenharmony_ci{
3218c2ecf20Sopenharmony_ci	return ioread32be(reg);
3228c2ecf20Sopenharmony_ci}
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_cistatic inline void grcan_write_reg(u32 __iomem *reg, u32 val)
3258c2ecf20Sopenharmony_ci{
3268c2ecf20Sopenharmony_ci	iowrite32be(val, reg);
3278c2ecf20Sopenharmony_ci}
3288c2ecf20Sopenharmony_ci#else
3298c2ecf20Sopenharmony_cistatic inline u32 grcan_read_reg(u32 __iomem *reg)
3308c2ecf20Sopenharmony_ci{
3318c2ecf20Sopenharmony_ci	return ioread32(reg);
3328c2ecf20Sopenharmony_ci}
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_cistatic inline void grcan_write_reg(u32 __iomem *reg, u32 val)
3358c2ecf20Sopenharmony_ci{
3368c2ecf20Sopenharmony_ci	iowrite32(val, reg);
3378c2ecf20Sopenharmony_ci}
3388c2ecf20Sopenharmony_ci#endif
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_cistatic inline void grcan_clear_bits(u32 __iomem *reg, u32 mask)
3418c2ecf20Sopenharmony_ci{
3428c2ecf20Sopenharmony_ci	grcan_write_reg(reg, grcan_read_reg(reg) & ~mask);
3438c2ecf20Sopenharmony_ci}
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_cistatic inline void grcan_set_bits(u32 __iomem *reg, u32 mask)
3468c2ecf20Sopenharmony_ci{
3478c2ecf20Sopenharmony_ci	grcan_write_reg(reg, grcan_read_reg(reg) | mask);
3488c2ecf20Sopenharmony_ci}
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_cistatic inline u32 grcan_read_bits(u32 __iomem *reg, u32 mask)
3518c2ecf20Sopenharmony_ci{
3528c2ecf20Sopenharmony_ci	return grcan_read_reg(reg) & mask;
3538c2ecf20Sopenharmony_ci}
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_cistatic inline void grcan_write_bits(u32 __iomem *reg, u32 value, u32 mask)
3568c2ecf20Sopenharmony_ci{
3578c2ecf20Sopenharmony_ci	u32 old = grcan_read_reg(reg);
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	grcan_write_reg(reg, (old & ~mask) | (value & mask));
3608c2ecf20Sopenharmony_ci}
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci/* a and b should both be in [0,size] and a == b == size should not hold */
3638c2ecf20Sopenharmony_cistatic inline u32 grcan_ring_add(u32 a, u32 b, u32 size)
3648c2ecf20Sopenharmony_ci{
3658c2ecf20Sopenharmony_ci	u32 sum = a + b;
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	if (sum < size)
3688c2ecf20Sopenharmony_ci		return sum;
3698c2ecf20Sopenharmony_ci	else
3708c2ecf20Sopenharmony_ci		return sum - size;
3718c2ecf20Sopenharmony_ci}
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci/* a and b should both be in [0,size) */
3748c2ecf20Sopenharmony_cistatic inline u32 grcan_ring_sub(u32 a, u32 b, u32 size)
3758c2ecf20Sopenharmony_ci{
3768c2ecf20Sopenharmony_ci	return grcan_ring_add(a, size - b, size);
3778c2ecf20Sopenharmony_ci}
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci/* Available slots for new transmissions */
3808c2ecf20Sopenharmony_cistatic inline u32 grcan_txspace(size_t txsize, u32 txwr, u32 eskbp)
3818c2ecf20Sopenharmony_ci{
3828c2ecf20Sopenharmony_ci	u32 slots = txsize / GRCAN_MSG_SIZE - 1;
3838c2ecf20Sopenharmony_ci	u32 used = grcan_ring_sub(txwr, eskbp, txsize) / GRCAN_MSG_SIZE;
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	return slots - used;
3868c2ecf20Sopenharmony_ci}
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci/* Configuration parameters that can be set via module parameters */
3898c2ecf20Sopenharmony_cistatic struct grcan_device_config grcan_module_config =
3908c2ecf20Sopenharmony_ci	GRCAN_DEFAULT_DEVICE_CONFIG;
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_cistatic const struct can_bittiming_const grcan_bittiming_const = {
3938c2ecf20Sopenharmony_ci	.name		= DRV_NAME,
3948c2ecf20Sopenharmony_ci	.tseg1_min	= GRCAN_CONF_PS1_MIN + 1,
3958c2ecf20Sopenharmony_ci	.tseg1_max	= GRCAN_CONF_PS1_MAX + 1,
3968c2ecf20Sopenharmony_ci	.tseg2_min	= GRCAN_CONF_PS2_MIN,
3978c2ecf20Sopenharmony_ci	.tseg2_max	= GRCAN_CONF_PS2_MAX,
3988c2ecf20Sopenharmony_ci	.sjw_max	= GRCAN_CONF_RSJ_MAX,
3998c2ecf20Sopenharmony_ci	.brp_min	= GRCAN_CONF_SCALER_MIN + 1,
4008c2ecf20Sopenharmony_ci	.brp_max	= GRCAN_CONF_SCALER_MAX + 1,
4018c2ecf20Sopenharmony_ci	.brp_inc	= GRCAN_CONF_SCALER_INC,
4028c2ecf20Sopenharmony_ci};
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_cistatic int grcan_set_bittiming(struct net_device *dev)
4058c2ecf20Sopenharmony_ci{
4068c2ecf20Sopenharmony_ci	struct grcan_priv *priv = netdev_priv(dev);
4078c2ecf20Sopenharmony_ci	struct grcan_registers __iomem *regs = priv->regs;
4088c2ecf20Sopenharmony_ci	struct can_bittiming *bt = &priv->can.bittiming;
4098c2ecf20Sopenharmony_ci	u32 timing = 0;
4108c2ecf20Sopenharmony_ci	int bpr, rsj, ps1, ps2, scaler;
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	/* Should never happen - function will not be called when
4138c2ecf20Sopenharmony_ci	 * device is up
4148c2ecf20Sopenharmony_ci	 */
4158c2ecf20Sopenharmony_ci	if (grcan_read_bits(&regs->ctrl, GRCAN_CTRL_ENABLE))
4168c2ecf20Sopenharmony_ci		return -EBUSY;
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	bpr = 0; /* Note bpr and brp are different concepts */
4198c2ecf20Sopenharmony_ci	rsj = bt->sjw;
4208c2ecf20Sopenharmony_ci	ps1 = (bt->prop_seg + bt->phase_seg1) - 1; /* tseg1 - 1 */
4218c2ecf20Sopenharmony_ci	ps2 = bt->phase_seg2;
4228c2ecf20Sopenharmony_ci	scaler = (bt->brp - 1);
4238c2ecf20Sopenharmony_ci	netdev_dbg(dev, "Request for BPR=%d, RSJ=%d, PS1=%d, PS2=%d, SCALER=%d",
4248c2ecf20Sopenharmony_ci		   bpr, rsj, ps1, ps2, scaler);
4258c2ecf20Sopenharmony_ci	if (!(ps1 > ps2)) {
4268c2ecf20Sopenharmony_ci		netdev_err(dev, "PS1 > PS2 must hold: PS1=%d, PS2=%d\n",
4278c2ecf20Sopenharmony_ci			   ps1, ps2);
4288c2ecf20Sopenharmony_ci		return -EINVAL;
4298c2ecf20Sopenharmony_ci	}
4308c2ecf20Sopenharmony_ci	if (!(ps2 >= rsj)) {
4318c2ecf20Sopenharmony_ci		netdev_err(dev, "PS2 >= RSJ must hold: PS2=%d, RSJ=%d\n",
4328c2ecf20Sopenharmony_ci			   ps2, rsj);
4338c2ecf20Sopenharmony_ci		return -EINVAL;
4348c2ecf20Sopenharmony_ci	}
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	timing |= (bpr << GRCAN_CONF_BPR_BIT) & GRCAN_CONF_BPR;
4378c2ecf20Sopenharmony_ci	timing |= (rsj << GRCAN_CONF_RSJ_BIT) & GRCAN_CONF_RSJ;
4388c2ecf20Sopenharmony_ci	timing |= (ps1 << GRCAN_CONF_PS1_BIT) & GRCAN_CONF_PS1;
4398c2ecf20Sopenharmony_ci	timing |= (ps2 << GRCAN_CONF_PS2_BIT) & GRCAN_CONF_PS2;
4408c2ecf20Sopenharmony_ci	timing |= (scaler << GRCAN_CONF_SCALER_BIT) & GRCAN_CONF_SCALER;
4418c2ecf20Sopenharmony_ci	netdev_info(dev, "setting timing=0x%x\n", timing);
4428c2ecf20Sopenharmony_ci	grcan_write_bits(&regs->conf, timing, GRCAN_CONF_TIMING);
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	return 0;
4458c2ecf20Sopenharmony_ci}
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_cistatic int grcan_get_berr_counter(const struct net_device *dev,
4488c2ecf20Sopenharmony_ci				  struct can_berr_counter *bec)
4498c2ecf20Sopenharmony_ci{
4508c2ecf20Sopenharmony_ci	struct grcan_priv *priv = netdev_priv(dev);
4518c2ecf20Sopenharmony_ci	struct grcan_registers __iomem *regs = priv->regs;
4528c2ecf20Sopenharmony_ci	u32 status = grcan_read_reg(&regs->stat);
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	bec->txerr = (status & GRCAN_STAT_TXERRCNT) >> GRCAN_STAT_TXERRCNT_BIT;
4558c2ecf20Sopenharmony_ci	bec->rxerr = (status & GRCAN_STAT_RXERRCNT) >> GRCAN_STAT_RXERRCNT_BIT;
4568c2ecf20Sopenharmony_ci	return 0;
4578c2ecf20Sopenharmony_ci}
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_cistatic int grcan_poll(struct napi_struct *napi, int budget);
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_ci/* Reset device, but keep configuration information */
4628c2ecf20Sopenharmony_cistatic void grcan_reset(struct net_device *dev)
4638c2ecf20Sopenharmony_ci{
4648c2ecf20Sopenharmony_ci	struct grcan_priv *priv = netdev_priv(dev);
4658c2ecf20Sopenharmony_ci	struct grcan_registers __iomem *regs = priv->regs;
4668c2ecf20Sopenharmony_ci	u32 config = grcan_read_reg(&regs->conf);
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci	grcan_set_bits(&regs->ctrl, GRCAN_CTRL_RESET);
4698c2ecf20Sopenharmony_ci	grcan_write_reg(&regs->conf, config);
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	priv->eskbp = grcan_read_reg(&regs->txrd);
4728c2ecf20Sopenharmony_ci	priv->can.state = CAN_STATE_STOPPED;
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	/* Turn off hardware filtering - regs->rxcode set to 0 by reset */
4758c2ecf20Sopenharmony_ci	grcan_write_reg(&regs->rxmask, 0);
4768c2ecf20Sopenharmony_ci}
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci/* stop device without changing any configurations */
4798c2ecf20Sopenharmony_cistatic void grcan_stop_hardware(struct net_device *dev)
4808c2ecf20Sopenharmony_ci{
4818c2ecf20Sopenharmony_ci	struct grcan_priv *priv = netdev_priv(dev);
4828c2ecf20Sopenharmony_ci	struct grcan_registers __iomem *regs = priv->regs;
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	grcan_write_reg(&regs->imr, GRCAN_IRQ_NONE);
4858c2ecf20Sopenharmony_ci	grcan_clear_bits(&regs->txctrl, GRCAN_TXCTRL_ENABLE);
4868c2ecf20Sopenharmony_ci	grcan_clear_bits(&regs->rxctrl, GRCAN_RXCTRL_ENABLE);
4878c2ecf20Sopenharmony_ci	grcan_clear_bits(&regs->ctrl, GRCAN_CTRL_ENABLE);
4888c2ecf20Sopenharmony_ci}
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci/* Let priv->eskbp catch up to regs->txrd and echo back the skbs if echo
4918c2ecf20Sopenharmony_ci * is true and free them otherwise.
4928c2ecf20Sopenharmony_ci *
4938c2ecf20Sopenharmony_ci * If budget is >= 0, stop after handling at most budget skbs. Otherwise,
4948c2ecf20Sopenharmony_ci * continue until priv->eskbp catches up to regs->txrd.
4958c2ecf20Sopenharmony_ci *
4968c2ecf20Sopenharmony_ci * priv->lock *must* be held when calling this function
4978c2ecf20Sopenharmony_ci */
4988c2ecf20Sopenharmony_cistatic int catch_up_echo_skb(struct net_device *dev, int budget, bool echo)
4998c2ecf20Sopenharmony_ci{
5008c2ecf20Sopenharmony_ci	struct grcan_priv *priv = netdev_priv(dev);
5018c2ecf20Sopenharmony_ci	struct grcan_registers __iomem *regs = priv->regs;
5028c2ecf20Sopenharmony_ci	struct grcan_dma *dma = &priv->dma;
5038c2ecf20Sopenharmony_ci	struct net_device_stats *stats = &dev->stats;
5048c2ecf20Sopenharmony_ci	int i, work_done;
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	/* Updates to priv->eskbp and wake-ups of the queue needs to
5078c2ecf20Sopenharmony_ci	 * be atomic towards the reads of priv->eskbp and shut-downs
5088c2ecf20Sopenharmony_ci	 * of the queue in grcan_start_xmit.
5098c2ecf20Sopenharmony_ci	 */
5108c2ecf20Sopenharmony_ci	u32 txrd = grcan_read_reg(&regs->txrd);
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci	for (work_done = 0; work_done < budget || budget < 0; work_done++) {
5138c2ecf20Sopenharmony_ci		if (priv->eskbp == txrd)
5148c2ecf20Sopenharmony_ci			break;
5158c2ecf20Sopenharmony_ci		i = priv->eskbp / GRCAN_MSG_SIZE;
5168c2ecf20Sopenharmony_ci		if (echo) {
5178c2ecf20Sopenharmony_ci			/* Normal echo of messages */
5188c2ecf20Sopenharmony_ci			stats->tx_packets++;
5198c2ecf20Sopenharmony_ci			stats->tx_bytes += priv->txdlc[i];
5208c2ecf20Sopenharmony_ci			priv->txdlc[i] = 0;
5218c2ecf20Sopenharmony_ci			can_get_echo_skb(dev, i);
5228c2ecf20Sopenharmony_ci		} else {
5238c2ecf20Sopenharmony_ci			/* For cleanup of untransmitted messages */
5248c2ecf20Sopenharmony_ci			can_free_echo_skb(dev, i);
5258c2ecf20Sopenharmony_ci		}
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci		priv->eskbp = grcan_ring_add(priv->eskbp, GRCAN_MSG_SIZE,
5288c2ecf20Sopenharmony_ci					     dma->tx.size);
5298c2ecf20Sopenharmony_ci		txrd = grcan_read_reg(&regs->txrd);
5308c2ecf20Sopenharmony_ci	}
5318c2ecf20Sopenharmony_ci	return work_done;
5328c2ecf20Sopenharmony_ci}
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_cistatic void grcan_lost_one_shot_frame(struct net_device *dev)
5358c2ecf20Sopenharmony_ci{
5368c2ecf20Sopenharmony_ci	struct grcan_priv *priv = netdev_priv(dev);
5378c2ecf20Sopenharmony_ci	struct grcan_registers __iomem *regs = priv->regs;
5388c2ecf20Sopenharmony_ci	struct grcan_dma *dma = &priv->dma;
5398c2ecf20Sopenharmony_ci	u32 txrd;
5408c2ecf20Sopenharmony_ci	unsigned long flags;
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	catch_up_echo_skb(dev, -1, true);
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci	if (unlikely(grcan_read_bits(&regs->txctrl, GRCAN_TXCTRL_ENABLE))) {
5478c2ecf20Sopenharmony_ci		/* Should never happen */
5488c2ecf20Sopenharmony_ci		netdev_err(dev, "TXCTRL enabled at TXLOSS in one shot mode\n");
5498c2ecf20Sopenharmony_ci	} else {
5508c2ecf20Sopenharmony_ci		/* By the time an GRCAN_IRQ_TXLOSS is generated in
5518c2ecf20Sopenharmony_ci		 * one-shot mode there is no problem in writing
5528c2ecf20Sopenharmony_ci		 * to TXRD even in versions of the hardware in
5538c2ecf20Sopenharmony_ci		 * which GRCAN_TXCTRL_ONGOING is not cleared properly
5548c2ecf20Sopenharmony_ci		 * in one-shot mode.
5558c2ecf20Sopenharmony_ci		 */
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci		/* Skip message and discard echo-skb */
5588c2ecf20Sopenharmony_ci		txrd = grcan_read_reg(&regs->txrd);
5598c2ecf20Sopenharmony_ci		txrd = grcan_ring_add(txrd, GRCAN_MSG_SIZE, dma->tx.size);
5608c2ecf20Sopenharmony_ci		grcan_write_reg(&regs->txrd, txrd);
5618c2ecf20Sopenharmony_ci		catch_up_echo_skb(dev, -1, false);
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci		if (!priv->resetting && !priv->closing &&
5648c2ecf20Sopenharmony_ci		    !(priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)) {
5658c2ecf20Sopenharmony_ci			netif_wake_queue(dev);
5668c2ecf20Sopenharmony_ci			grcan_set_bits(&regs->txctrl, GRCAN_TXCTRL_ENABLE);
5678c2ecf20Sopenharmony_ci		}
5688c2ecf20Sopenharmony_ci	}
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&priv->lock, flags);
5718c2ecf20Sopenharmony_ci}
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_cistatic void grcan_err(struct net_device *dev, u32 sources, u32 status)
5748c2ecf20Sopenharmony_ci{
5758c2ecf20Sopenharmony_ci	struct grcan_priv *priv = netdev_priv(dev);
5768c2ecf20Sopenharmony_ci	struct grcan_registers __iomem *regs = priv->regs;
5778c2ecf20Sopenharmony_ci	struct grcan_dma *dma = &priv->dma;
5788c2ecf20Sopenharmony_ci	struct net_device_stats *stats = &dev->stats;
5798c2ecf20Sopenharmony_ci	struct can_frame cf;
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	/* Zero potential error_frame */
5828c2ecf20Sopenharmony_ci	memset(&cf, 0, sizeof(cf));
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci	/* Message lost interrupt. This might be due to arbitration error, but
5858c2ecf20Sopenharmony_ci	 * is also triggered when there is no one else on the can bus or when
5868c2ecf20Sopenharmony_ci	 * there is a problem with the hardware interface or the bus itself. As
5878c2ecf20Sopenharmony_ci	 * arbitration errors can not be singled out, no error frames are
5888c2ecf20Sopenharmony_ci	 * generated reporting this event as an arbitration error.
5898c2ecf20Sopenharmony_ci	 */
5908c2ecf20Sopenharmony_ci	if (sources & GRCAN_IRQ_TXLOSS) {
5918c2ecf20Sopenharmony_ci		/* Take care of failed one-shot transmit */
5928c2ecf20Sopenharmony_ci		if (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
5938c2ecf20Sopenharmony_ci			grcan_lost_one_shot_frame(dev);
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci		/* Stop printing as soon as error passive or bus off is in
5968c2ecf20Sopenharmony_ci		 * effect to limit the amount of txloss debug printouts.
5978c2ecf20Sopenharmony_ci		 */
5988c2ecf20Sopenharmony_ci		if (!(status & GRCAN_STAT_ERRCTR_RELATED)) {
5998c2ecf20Sopenharmony_ci			netdev_dbg(dev, "tx message lost\n");
6008c2ecf20Sopenharmony_ci			stats->tx_errors++;
6018c2ecf20Sopenharmony_ci		}
6028c2ecf20Sopenharmony_ci	}
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci	/* Conditions dealing with the error counters. There is no interrupt for
6058c2ecf20Sopenharmony_ci	 * error warning, but there are interrupts for increases of the error
6068c2ecf20Sopenharmony_ci	 * counters.
6078c2ecf20Sopenharmony_ci	 */
6088c2ecf20Sopenharmony_ci	if ((sources & GRCAN_IRQ_ERRCTR_RELATED) ||
6098c2ecf20Sopenharmony_ci	    (status & GRCAN_STAT_ERRCTR_RELATED)) {
6108c2ecf20Sopenharmony_ci		enum can_state state = priv->can.state;
6118c2ecf20Sopenharmony_ci		enum can_state oldstate = state;
6128c2ecf20Sopenharmony_ci		u32 txerr = (status & GRCAN_STAT_TXERRCNT)
6138c2ecf20Sopenharmony_ci			>> GRCAN_STAT_TXERRCNT_BIT;
6148c2ecf20Sopenharmony_ci		u32 rxerr = (status & GRCAN_STAT_RXERRCNT)
6158c2ecf20Sopenharmony_ci			>> GRCAN_STAT_RXERRCNT_BIT;
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci		/* Figure out current state */
6188c2ecf20Sopenharmony_ci		if (status & GRCAN_STAT_OFF) {
6198c2ecf20Sopenharmony_ci			state = CAN_STATE_BUS_OFF;
6208c2ecf20Sopenharmony_ci		} else if (status & GRCAN_STAT_PASS) {
6218c2ecf20Sopenharmony_ci			state = CAN_STATE_ERROR_PASSIVE;
6228c2ecf20Sopenharmony_ci		} else if (txerr >= GRCAN_STAT_ERRCNT_WARNING_LIMIT ||
6238c2ecf20Sopenharmony_ci			   rxerr >= GRCAN_STAT_ERRCNT_WARNING_LIMIT) {
6248c2ecf20Sopenharmony_ci			state = CAN_STATE_ERROR_WARNING;
6258c2ecf20Sopenharmony_ci		} else {
6268c2ecf20Sopenharmony_ci			state = CAN_STATE_ERROR_ACTIVE;
6278c2ecf20Sopenharmony_ci		}
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci		/* Handle and report state changes */
6308c2ecf20Sopenharmony_ci		if (state != oldstate) {
6318c2ecf20Sopenharmony_ci			switch (state) {
6328c2ecf20Sopenharmony_ci			case CAN_STATE_BUS_OFF:
6338c2ecf20Sopenharmony_ci				netdev_dbg(dev, "bus-off\n");
6348c2ecf20Sopenharmony_ci				netif_carrier_off(dev);
6358c2ecf20Sopenharmony_ci				priv->can.can_stats.bus_off++;
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci				/* Prevent the hardware from recovering from bus
6388c2ecf20Sopenharmony_ci				 * off on its own if restart is disabled.
6398c2ecf20Sopenharmony_ci				 */
6408c2ecf20Sopenharmony_ci				if (!priv->can.restart_ms)
6418c2ecf20Sopenharmony_ci					grcan_stop_hardware(dev);
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci				cf.can_id |= CAN_ERR_BUSOFF;
6448c2ecf20Sopenharmony_ci				break;
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci			case CAN_STATE_ERROR_PASSIVE:
6478c2ecf20Sopenharmony_ci				netdev_dbg(dev, "Error passive condition\n");
6488c2ecf20Sopenharmony_ci				priv->can.can_stats.error_passive++;
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci				cf.can_id |= CAN_ERR_CRTL;
6518c2ecf20Sopenharmony_ci				if (txerr >= GRCAN_STAT_ERRCNT_PASSIVE_LIMIT)
6528c2ecf20Sopenharmony_ci					cf.data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
6538c2ecf20Sopenharmony_ci				if (rxerr >= GRCAN_STAT_ERRCNT_PASSIVE_LIMIT)
6548c2ecf20Sopenharmony_ci					cf.data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
6558c2ecf20Sopenharmony_ci				break;
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci			case CAN_STATE_ERROR_WARNING:
6588c2ecf20Sopenharmony_ci				netdev_dbg(dev, "Error warning condition\n");
6598c2ecf20Sopenharmony_ci				priv->can.can_stats.error_warning++;
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci				cf.can_id |= CAN_ERR_CRTL;
6628c2ecf20Sopenharmony_ci				if (txerr >= GRCAN_STAT_ERRCNT_WARNING_LIMIT)
6638c2ecf20Sopenharmony_ci					cf.data[1] |= CAN_ERR_CRTL_TX_WARNING;
6648c2ecf20Sopenharmony_ci				if (rxerr >= GRCAN_STAT_ERRCNT_WARNING_LIMIT)
6658c2ecf20Sopenharmony_ci					cf.data[1] |= CAN_ERR_CRTL_RX_WARNING;
6668c2ecf20Sopenharmony_ci				break;
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci			case CAN_STATE_ERROR_ACTIVE:
6698c2ecf20Sopenharmony_ci				netdev_dbg(dev, "Error active condition\n");
6708c2ecf20Sopenharmony_ci				cf.can_id |= CAN_ERR_CRTL;
6718c2ecf20Sopenharmony_ci				break;
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci			default:
6748c2ecf20Sopenharmony_ci				/* There are no others at this point */
6758c2ecf20Sopenharmony_ci				break;
6768c2ecf20Sopenharmony_ci			}
6778c2ecf20Sopenharmony_ci			cf.data[6] = txerr;
6788c2ecf20Sopenharmony_ci			cf.data[7] = rxerr;
6798c2ecf20Sopenharmony_ci			priv->can.state = state;
6808c2ecf20Sopenharmony_ci		}
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ci		/* Report automatic restarts */
6838c2ecf20Sopenharmony_ci		if (priv->can.restart_ms && oldstate == CAN_STATE_BUS_OFF) {
6848c2ecf20Sopenharmony_ci			unsigned long flags;
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci			cf.can_id |= CAN_ERR_RESTARTED;
6878c2ecf20Sopenharmony_ci			netdev_dbg(dev, "restarted\n");
6888c2ecf20Sopenharmony_ci			priv->can.can_stats.restarts++;
6898c2ecf20Sopenharmony_ci			netif_carrier_on(dev);
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci			spin_lock_irqsave(&priv->lock, flags);
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci			if (!priv->resetting && !priv->closing) {
6948c2ecf20Sopenharmony_ci				u32 txwr = grcan_read_reg(&regs->txwr);
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci				if (grcan_txspace(dma->tx.size, txwr,
6978c2ecf20Sopenharmony_ci						  priv->eskbp))
6988c2ecf20Sopenharmony_ci					netif_wake_queue(dev);
6998c2ecf20Sopenharmony_ci			}
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&priv->lock, flags);
7028c2ecf20Sopenharmony_ci		}
7038c2ecf20Sopenharmony_ci	}
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci	/* Data overrun interrupt */
7068c2ecf20Sopenharmony_ci	if ((sources & GRCAN_IRQ_OR) || (status & GRCAN_STAT_OR)) {
7078c2ecf20Sopenharmony_ci		netdev_dbg(dev, "got data overrun interrupt\n");
7088c2ecf20Sopenharmony_ci		stats->rx_over_errors++;
7098c2ecf20Sopenharmony_ci		stats->rx_errors++;
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci		cf.can_id |= CAN_ERR_CRTL;
7128c2ecf20Sopenharmony_ci		cf.data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
7138c2ecf20Sopenharmony_ci	}
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci	/* AHB bus error interrupts (not CAN bus errors) - shut down the
7168c2ecf20Sopenharmony_ci	 * device.
7178c2ecf20Sopenharmony_ci	 */
7188c2ecf20Sopenharmony_ci	if (sources & (GRCAN_IRQ_TXAHBERR | GRCAN_IRQ_RXAHBERR) ||
7198c2ecf20Sopenharmony_ci	    (status & GRCAN_STAT_AHBERR)) {
7208c2ecf20Sopenharmony_ci		char *txrx = "";
7218c2ecf20Sopenharmony_ci		unsigned long flags;
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci		if (sources & GRCAN_IRQ_TXAHBERR) {
7248c2ecf20Sopenharmony_ci			txrx = "on tx ";
7258c2ecf20Sopenharmony_ci			stats->tx_errors++;
7268c2ecf20Sopenharmony_ci		} else if (sources & GRCAN_IRQ_RXAHBERR) {
7278c2ecf20Sopenharmony_ci			txrx = "on rx ";
7288c2ecf20Sopenharmony_ci			stats->rx_errors++;
7298c2ecf20Sopenharmony_ci		}
7308c2ecf20Sopenharmony_ci		netdev_err(dev, "Fatal AHB bus error %s- halting device\n",
7318c2ecf20Sopenharmony_ci			   txrx);
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci		spin_lock_irqsave(&priv->lock, flags);
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci		/* Prevent anything to be enabled again and halt device */
7368c2ecf20Sopenharmony_ci		priv->closing = true;
7378c2ecf20Sopenharmony_ci		netif_stop_queue(dev);
7388c2ecf20Sopenharmony_ci		grcan_stop_hardware(dev);
7398c2ecf20Sopenharmony_ci		priv->can.state = CAN_STATE_STOPPED;
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&priv->lock, flags);
7428c2ecf20Sopenharmony_ci	}
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci	/* Pass on error frame if something to report,
7458c2ecf20Sopenharmony_ci	 * i.e. id contains some information
7468c2ecf20Sopenharmony_ci	 */
7478c2ecf20Sopenharmony_ci	if (cf.can_id) {
7488c2ecf20Sopenharmony_ci		struct can_frame *skb_cf;
7498c2ecf20Sopenharmony_ci		struct sk_buff *skb = alloc_can_err_skb(dev, &skb_cf);
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci		if (skb == NULL) {
7528c2ecf20Sopenharmony_ci			netdev_dbg(dev, "could not allocate error frame\n");
7538c2ecf20Sopenharmony_ci			return;
7548c2ecf20Sopenharmony_ci		}
7558c2ecf20Sopenharmony_ci		skb_cf->can_id |= cf.can_id;
7568c2ecf20Sopenharmony_ci		memcpy(skb_cf->data, cf.data, sizeof(cf.data));
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci		netif_rx(skb);
7598c2ecf20Sopenharmony_ci	}
7608c2ecf20Sopenharmony_ci}
7618c2ecf20Sopenharmony_ci
7628c2ecf20Sopenharmony_cistatic irqreturn_t grcan_interrupt(int irq, void *dev_id)
7638c2ecf20Sopenharmony_ci{
7648c2ecf20Sopenharmony_ci	struct net_device *dev = dev_id;
7658c2ecf20Sopenharmony_ci	struct grcan_priv *priv = netdev_priv(dev);
7668c2ecf20Sopenharmony_ci	struct grcan_registers __iomem *regs = priv->regs;
7678c2ecf20Sopenharmony_ci	u32 sources, status;
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci	/* Find out the source */
7708c2ecf20Sopenharmony_ci	sources = grcan_read_reg(&regs->pimsr);
7718c2ecf20Sopenharmony_ci	if (!sources)
7728c2ecf20Sopenharmony_ci		return IRQ_NONE;
7738c2ecf20Sopenharmony_ci	grcan_write_reg(&regs->picr, sources);
7748c2ecf20Sopenharmony_ci	status = grcan_read_reg(&regs->stat);
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci	/* If we got TX progress, the device has not hanged,
7778c2ecf20Sopenharmony_ci	 * so disable the hang timer
7788c2ecf20Sopenharmony_ci	 */
7798c2ecf20Sopenharmony_ci	if (priv->need_txbug_workaround &&
7808c2ecf20Sopenharmony_ci	    (sources & (GRCAN_IRQ_TX | GRCAN_IRQ_TXLOSS))) {
7818c2ecf20Sopenharmony_ci		del_timer(&priv->hang_timer);
7828c2ecf20Sopenharmony_ci	}
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_ci	/* Frame(s) received or transmitted */
7858c2ecf20Sopenharmony_ci	if (sources & (GRCAN_IRQ_TX | GRCAN_IRQ_RX)) {
7868c2ecf20Sopenharmony_ci		/* Disable tx/rx interrupts and schedule poll(). No need for
7878c2ecf20Sopenharmony_ci		 * locking as interference from a running reset at worst leads
7888c2ecf20Sopenharmony_ci		 * to an extra interrupt.
7898c2ecf20Sopenharmony_ci		 */
7908c2ecf20Sopenharmony_ci		grcan_clear_bits(&regs->imr, GRCAN_IRQ_TX | GRCAN_IRQ_RX);
7918c2ecf20Sopenharmony_ci		napi_schedule(&priv->napi);
7928c2ecf20Sopenharmony_ci	}
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci	/* (Potential) error conditions to take care of */
7958c2ecf20Sopenharmony_ci	if (sources & GRCAN_IRQ_ERRORS)
7968c2ecf20Sopenharmony_ci		grcan_err(dev, sources, status);
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
7998c2ecf20Sopenharmony_ci}
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci/* Reset device and restart operations from where they were.
8028c2ecf20Sopenharmony_ci *
8038c2ecf20Sopenharmony_ci * This assumes that RXCTRL & RXCTRL is properly disabled and that RX
8048c2ecf20Sopenharmony_ci * is not ONGOING (TX might be stuck in ONGOING due to a harwrware bug
8058c2ecf20Sopenharmony_ci * for single shot)
8068c2ecf20Sopenharmony_ci */
8078c2ecf20Sopenharmony_cistatic void grcan_running_reset(struct timer_list *t)
8088c2ecf20Sopenharmony_ci{
8098c2ecf20Sopenharmony_ci	struct grcan_priv *priv = from_timer(priv, t, rr_timer);
8108c2ecf20Sopenharmony_ci	struct net_device *dev = priv->dev;
8118c2ecf20Sopenharmony_ci	struct grcan_registers __iomem *regs = priv->regs;
8128c2ecf20Sopenharmony_ci	unsigned long flags;
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci	/* This temporarily messes with eskbp, so we need to lock
8158c2ecf20Sopenharmony_ci	 * priv->lock
8168c2ecf20Sopenharmony_ci	 */
8178c2ecf20Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ci	priv->resetting = false;
8208c2ecf20Sopenharmony_ci	del_timer(&priv->hang_timer);
8218c2ecf20Sopenharmony_ci	del_timer(&priv->rr_timer);
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci	if (!priv->closing) {
8248c2ecf20Sopenharmony_ci		/* Save and reset - config register preserved by grcan_reset */
8258c2ecf20Sopenharmony_ci		u32 imr = grcan_read_reg(&regs->imr);
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_ci		u32 txaddr = grcan_read_reg(&regs->txaddr);
8288c2ecf20Sopenharmony_ci		u32 txsize = grcan_read_reg(&regs->txsize);
8298c2ecf20Sopenharmony_ci		u32 txwr = grcan_read_reg(&regs->txwr);
8308c2ecf20Sopenharmony_ci		u32 txrd = grcan_read_reg(&regs->txrd);
8318c2ecf20Sopenharmony_ci		u32 eskbp = priv->eskbp;
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci		u32 rxaddr = grcan_read_reg(&regs->rxaddr);
8348c2ecf20Sopenharmony_ci		u32 rxsize = grcan_read_reg(&regs->rxsize);
8358c2ecf20Sopenharmony_ci		u32 rxwr = grcan_read_reg(&regs->rxwr);
8368c2ecf20Sopenharmony_ci		u32 rxrd = grcan_read_reg(&regs->rxrd);
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci		grcan_reset(dev);
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci		/* Restore */
8418c2ecf20Sopenharmony_ci		grcan_write_reg(&regs->txaddr, txaddr);
8428c2ecf20Sopenharmony_ci		grcan_write_reg(&regs->txsize, txsize);
8438c2ecf20Sopenharmony_ci		grcan_write_reg(&regs->txwr, txwr);
8448c2ecf20Sopenharmony_ci		grcan_write_reg(&regs->txrd, txrd);
8458c2ecf20Sopenharmony_ci		priv->eskbp = eskbp;
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_ci		grcan_write_reg(&regs->rxaddr, rxaddr);
8488c2ecf20Sopenharmony_ci		grcan_write_reg(&regs->rxsize, rxsize);
8498c2ecf20Sopenharmony_ci		grcan_write_reg(&regs->rxwr, rxwr);
8508c2ecf20Sopenharmony_ci		grcan_write_reg(&regs->rxrd, rxrd);
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_ci		/* Turn on device again */
8538c2ecf20Sopenharmony_ci		grcan_write_reg(&regs->imr, imr);
8548c2ecf20Sopenharmony_ci		priv->can.state = CAN_STATE_ERROR_ACTIVE;
8558c2ecf20Sopenharmony_ci		grcan_write_reg(&regs->txctrl, GRCAN_TXCTRL_ENABLE
8568c2ecf20Sopenharmony_ci				| (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT
8578c2ecf20Sopenharmony_ci				   ? GRCAN_TXCTRL_SINGLE : 0));
8588c2ecf20Sopenharmony_ci		grcan_write_reg(&regs->rxctrl, GRCAN_RXCTRL_ENABLE);
8598c2ecf20Sopenharmony_ci		grcan_write_reg(&regs->ctrl, GRCAN_CTRL_ENABLE);
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci		/* Start queue if there is size and listen-onle mode is not
8628c2ecf20Sopenharmony_ci		 * enabled
8638c2ecf20Sopenharmony_ci		 */
8648c2ecf20Sopenharmony_ci		if (grcan_txspace(priv->dma.tx.size, txwr, priv->eskbp) &&
8658c2ecf20Sopenharmony_ci		    !(priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
8668c2ecf20Sopenharmony_ci			netif_wake_queue(dev);
8678c2ecf20Sopenharmony_ci	}
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&priv->lock, flags);
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci	netdev_err(dev, "Device reset and restored\n");
8728c2ecf20Sopenharmony_ci}
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_ci/* Waiting time in usecs corresponding to the transmission of three maximum
8758c2ecf20Sopenharmony_ci * sized can frames in the given bitrate (in bits/sec). Waiting for this amount
8768c2ecf20Sopenharmony_ci * of time makes sure that the can controller have time to finish sending or
8778c2ecf20Sopenharmony_ci * receiving a frame with a good margin.
8788c2ecf20Sopenharmony_ci *
8798c2ecf20Sopenharmony_ci * usecs/sec * number of frames * bits/frame / bits/sec
8808c2ecf20Sopenharmony_ci */
8818c2ecf20Sopenharmony_cistatic inline u32 grcan_ongoing_wait_usecs(__u32 bitrate)
8828c2ecf20Sopenharmony_ci{
8838c2ecf20Sopenharmony_ci	return 1000000 * 3 * GRCAN_EFF_FRAME_MAX_BITS / bitrate;
8848c2ecf20Sopenharmony_ci}
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci/* Set timer so that it will not fire until after a period in which the can
8878c2ecf20Sopenharmony_ci * controller have a good margin to finish transmitting a frame unless it has
8888c2ecf20Sopenharmony_ci * hanged
8898c2ecf20Sopenharmony_ci */
8908c2ecf20Sopenharmony_cistatic inline void grcan_reset_timer(struct timer_list *timer, __u32 bitrate)
8918c2ecf20Sopenharmony_ci{
8928c2ecf20Sopenharmony_ci	u32 wait_jiffies = usecs_to_jiffies(grcan_ongoing_wait_usecs(bitrate));
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ci	mod_timer(timer, jiffies + wait_jiffies);
8958c2ecf20Sopenharmony_ci}
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci/* Disable channels and schedule a running reset */
8988c2ecf20Sopenharmony_cistatic void grcan_initiate_running_reset(struct timer_list *t)
8998c2ecf20Sopenharmony_ci{
9008c2ecf20Sopenharmony_ci	struct grcan_priv *priv = from_timer(priv, t, hang_timer);
9018c2ecf20Sopenharmony_ci	struct net_device *dev = priv->dev;
9028c2ecf20Sopenharmony_ci	struct grcan_registers __iomem *regs = priv->regs;
9038c2ecf20Sopenharmony_ci	unsigned long flags;
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci	netdev_err(dev, "Device seems hanged - reset scheduled\n");
9068c2ecf20Sopenharmony_ci
9078c2ecf20Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
9088c2ecf20Sopenharmony_ci
9098c2ecf20Sopenharmony_ci	/* The main body of this function must never be executed again
9108c2ecf20Sopenharmony_ci	 * until after an execution of grcan_running_reset
9118c2ecf20Sopenharmony_ci	 */
9128c2ecf20Sopenharmony_ci	if (!priv->resetting && !priv->closing) {
9138c2ecf20Sopenharmony_ci		priv->resetting = true;
9148c2ecf20Sopenharmony_ci		netif_stop_queue(dev);
9158c2ecf20Sopenharmony_ci		grcan_clear_bits(&regs->txctrl, GRCAN_TXCTRL_ENABLE);
9168c2ecf20Sopenharmony_ci		grcan_clear_bits(&regs->rxctrl, GRCAN_RXCTRL_ENABLE);
9178c2ecf20Sopenharmony_ci		grcan_reset_timer(&priv->rr_timer, priv->can.bittiming.bitrate);
9188c2ecf20Sopenharmony_ci	}
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&priv->lock, flags);
9218c2ecf20Sopenharmony_ci}
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_cistatic void grcan_free_dma_buffers(struct net_device *dev)
9248c2ecf20Sopenharmony_ci{
9258c2ecf20Sopenharmony_ci	struct grcan_priv *priv = netdev_priv(dev);
9268c2ecf20Sopenharmony_ci	struct grcan_dma *dma = &priv->dma;
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_ci	dma_free_coherent(priv->ofdev_dev, dma->base_size, dma->base_buf,
9298c2ecf20Sopenharmony_ci			  dma->base_handle);
9308c2ecf20Sopenharmony_ci	memset(dma, 0, sizeof(*dma));
9318c2ecf20Sopenharmony_ci}
9328c2ecf20Sopenharmony_ci
9338c2ecf20Sopenharmony_cistatic int grcan_allocate_dma_buffers(struct net_device *dev,
9348c2ecf20Sopenharmony_ci				      size_t tsize, size_t rsize)
9358c2ecf20Sopenharmony_ci{
9368c2ecf20Sopenharmony_ci	struct grcan_priv *priv = netdev_priv(dev);
9378c2ecf20Sopenharmony_ci	struct grcan_dma *dma = &priv->dma;
9388c2ecf20Sopenharmony_ci	struct grcan_dma_buffer *large = rsize > tsize ? &dma->rx : &dma->tx;
9398c2ecf20Sopenharmony_ci	struct grcan_dma_buffer *small = rsize > tsize ? &dma->tx : &dma->rx;
9408c2ecf20Sopenharmony_ci	size_t shift;
9418c2ecf20Sopenharmony_ci
9428c2ecf20Sopenharmony_ci	/* Need a whole number of GRCAN_BUFFER_ALIGNMENT for the large,
9438c2ecf20Sopenharmony_ci	 * i.e. first buffer
9448c2ecf20Sopenharmony_ci	 */
9458c2ecf20Sopenharmony_ci	size_t maxs = max(tsize, rsize);
9468c2ecf20Sopenharmony_ci	size_t lsize = ALIGN(maxs, GRCAN_BUFFER_ALIGNMENT);
9478c2ecf20Sopenharmony_ci
9488c2ecf20Sopenharmony_ci	/* Put the small buffer after that */
9498c2ecf20Sopenharmony_ci	size_t ssize = min(tsize, rsize);
9508c2ecf20Sopenharmony_ci
9518c2ecf20Sopenharmony_ci	/* Extra GRCAN_BUFFER_ALIGNMENT to allow for alignment */
9528c2ecf20Sopenharmony_ci	dma->base_size = lsize + ssize + GRCAN_BUFFER_ALIGNMENT;
9538c2ecf20Sopenharmony_ci	dma->base_buf = dma_alloc_coherent(priv->ofdev_dev,
9548c2ecf20Sopenharmony_ci					   dma->base_size,
9558c2ecf20Sopenharmony_ci					   &dma->base_handle,
9568c2ecf20Sopenharmony_ci					   GFP_KERNEL);
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_ci	if (!dma->base_buf)
9598c2ecf20Sopenharmony_ci		return -ENOMEM;
9608c2ecf20Sopenharmony_ci
9618c2ecf20Sopenharmony_ci	dma->tx.size = tsize;
9628c2ecf20Sopenharmony_ci	dma->rx.size = rsize;
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ci	large->handle = ALIGN(dma->base_handle, GRCAN_BUFFER_ALIGNMENT);
9658c2ecf20Sopenharmony_ci	small->handle = large->handle + lsize;
9668c2ecf20Sopenharmony_ci	shift = large->handle - dma->base_handle;
9678c2ecf20Sopenharmony_ci
9688c2ecf20Sopenharmony_ci	large->buf = dma->base_buf + shift;
9698c2ecf20Sopenharmony_ci	small->buf = large->buf + lsize;
9708c2ecf20Sopenharmony_ci
9718c2ecf20Sopenharmony_ci	return 0;
9728c2ecf20Sopenharmony_ci}
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_ci/* priv->lock *must* be held when calling this function */
9758c2ecf20Sopenharmony_cistatic int grcan_start(struct net_device *dev)
9768c2ecf20Sopenharmony_ci{
9778c2ecf20Sopenharmony_ci	struct grcan_priv *priv = netdev_priv(dev);
9788c2ecf20Sopenharmony_ci	struct grcan_registers __iomem *regs = priv->regs;
9798c2ecf20Sopenharmony_ci	u32 confop, txctrl;
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_ci	grcan_reset(dev);
9828c2ecf20Sopenharmony_ci
9838c2ecf20Sopenharmony_ci	grcan_write_reg(&regs->txaddr, priv->dma.tx.handle);
9848c2ecf20Sopenharmony_ci	grcan_write_reg(&regs->txsize, priv->dma.tx.size);
9858c2ecf20Sopenharmony_ci	/* regs->txwr, regs->txrd and priv->eskbp already set to 0 by reset */
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_ci	grcan_write_reg(&regs->rxaddr, priv->dma.rx.handle);
9888c2ecf20Sopenharmony_ci	grcan_write_reg(&regs->rxsize, priv->dma.rx.size);
9898c2ecf20Sopenharmony_ci	/* regs->rxwr and regs->rxrd already set to 0 by reset */
9908c2ecf20Sopenharmony_ci
9918c2ecf20Sopenharmony_ci	/* Enable interrupts */
9928c2ecf20Sopenharmony_ci	grcan_read_reg(&regs->pir);
9938c2ecf20Sopenharmony_ci	grcan_write_reg(&regs->imr, GRCAN_IRQ_DEFAULT);
9948c2ecf20Sopenharmony_ci
9958c2ecf20Sopenharmony_ci	/* Enable interfaces, channels and device */
9968c2ecf20Sopenharmony_ci	confop = GRCAN_CONF_ABORT
9978c2ecf20Sopenharmony_ci		| (priv->config.enable0 ? GRCAN_CONF_ENABLE0 : 0)
9988c2ecf20Sopenharmony_ci		| (priv->config.enable1 ? GRCAN_CONF_ENABLE1 : 0)
9998c2ecf20Sopenharmony_ci		| (priv->config.select ? GRCAN_CONF_SELECT : 0)
10008c2ecf20Sopenharmony_ci		| (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY ?
10018c2ecf20Sopenharmony_ci		   GRCAN_CONF_SILENT : 0)
10028c2ecf20Sopenharmony_ci		| (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES ?
10038c2ecf20Sopenharmony_ci		   GRCAN_CONF_SAM : 0);
10048c2ecf20Sopenharmony_ci	grcan_write_bits(&regs->conf, confop, GRCAN_CONF_OPERATION);
10058c2ecf20Sopenharmony_ci	txctrl = GRCAN_TXCTRL_ENABLE
10068c2ecf20Sopenharmony_ci		| (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT
10078c2ecf20Sopenharmony_ci		   ? GRCAN_TXCTRL_SINGLE : 0);
10088c2ecf20Sopenharmony_ci	grcan_write_reg(&regs->txctrl, txctrl);
10098c2ecf20Sopenharmony_ci	grcan_write_reg(&regs->rxctrl, GRCAN_RXCTRL_ENABLE);
10108c2ecf20Sopenharmony_ci	grcan_write_reg(&regs->ctrl, GRCAN_CTRL_ENABLE);
10118c2ecf20Sopenharmony_ci
10128c2ecf20Sopenharmony_ci	priv->can.state = CAN_STATE_ERROR_ACTIVE;
10138c2ecf20Sopenharmony_ci
10148c2ecf20Sopenharmony_ci	return 0;
10158c2ecf20Sopenharmony_ci}
10168c2ecf20Sopenharmony_ci
10178c2ecf20Sopenharmony_cistatic int grcan_set_mode(struct net_device *dev, enum can_mode mode)
10188c2ecf20Sopenharmony_ci{
10198c2ecf20Sopenharmony_ci	struct grcan_priv *priv = netdev_priv(dev);
10208c2ecf20Sopenharmony_ci	unsigned long flags;
10218c2ecf20Sopenharmony_ci	int err = 0;
10228c2ecf20Sopenharmony_ci
10238c2ecf20Sopenharmony_ci	if (mode == CAN_MODE_START) {
10248c2ecf20Sopenharmony_ci		/* This might be called to restart the device to recover from
10258c2ecf20Sopenharmony_ci		 * bus off errors
10268c2ecf20Sopenharmony_ci		 */
10278c2ecf20Sopenharmony_ci		spin_lock_irqsave(&priv->lock, flags);
10288c2ecf20Sopenharmony_ci		if (priv->closing || priv->resetting) {
10298c2ecf20Sopenharmony_ci			err = -EBUSY;
10308c2ecf20Sopenharmony_ci		} else {
10318c2ecf20Sopenharmony_ci			netdev_info(dev, "Restarting device\n");
10328c2ecf20Sopenharmony_ci			grcan_start(dev);
10338c2ecf20Sopenharmony_ci			if (!(priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
10348c2ecf20Sopenharmony_ci				netif_wake_queue(dev);
10358c2ecf20Sopenharmony_ci		}
10368c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&priv->lock, flags);
10378c2ecf20Sopenharmony_ci		return err;
10388c2ecf20Sopenharmony_ci	}
10398c2ecf20Sopenharmony_ci	return -EOPNOTSUPP;
10408c2ecf20Sopenharmony_ci}
10418c2ecf20Sopenharmony_ci
10428c2ecf20Sopenharmony_cistatic int grcan_open(struct net_device *dev)
10438c2ecf20Sopenharmony_ci{
10448c2ecf20Sopenharmony_ci	struct grcan_priv *priv = netdev_priv(dev);
10458c2ecf20Sopenharmony_ci	struct grcan_dma *dma = &priv->dma;
10468c2ecf20Sopenharmony_ci	unsigned long flags;
10478c2ecf20Sopenharmony_ci	int err;
10488c2ecf20Sopenharmony_ci
10498c2ecf20Sopenharmony_ci	/* Allocate memory */
10508c2ecf20Sopenharmony_ci	err = grcan_allocate_dma_buffers(dev, priv->config.txsize,
10518c2ecf20Sopenharmony_ci					 priv->config.rxsize);
10528c2ecf20Sopenharmony_ci	if (err) {
10538c2ecf20Sopenharmony_ci		netdev_err(dev, "could not allocate DMA buffers\n");
10548c2ecf20Sopenharmony_ci		return err;
10558c2ecf20Sopenharmony_ci	}
10568c2ecf20Sopenharmony_ci
10578c2ecf20Sopenharmony_ci	priv->echo_skb = kcalloc(dma->tx.size, sizeof(*priv->echo_skb),
10588c2ecf20Sopenharmony_ci				 GFP_KERNEL);
10598c2ecf20Sopenharmony_ci	if (!priv->echo_skb) {
10608c2ecf20Sopenharmony_ci		err = -ENOMEM;
10618c2ecf20Sopenharmony_ci		goto exit_free_dma_buffers;
10628c2ecf20Sopenharmony_ci	}
10638c2ecf20Sopenharmony_ci	priv->can.echo_skb_max = dma->tx.size;
10648c2ecf20Sopenharmony_ci	priv->can.echo_skb = priv->echo_skb;
10658c2ecf20Sopenharmony_ci
10668c2ecf20Sopenharmony_ci	priv->txdlc = kcalloc(dma->tx.size, sizeof(*priv->txdlc), GFP_KERNEL);
10678c2ecf20Sopenharmony_ci	if (!priv->txdlc) {
10688c2ecf20Sopenharmony_ci		err = -ENOMEM;
10698c2ecf20Sopenharmony_ci		goto exit_free_echo_skb;
10708c2ecf20Sopenharmony_ci	}
10718c2ecf20Sopenharmony_ci
10728c2ecf20Sopenharmony_ci	/* Get can device up */
10738c2ecf20Sopenharmony_ci	err = open_candev(dev);
10748c2ecf20Sopenharmony_ci	if (err)
10758c2ecf20Sopenharmony_ci		goto exit_free_txdlc;
10768c2ecf20Sopenharmony_ci
10778c2ecf20Sopenharmony_ci	err = request_irq(dev->irq, grcan_interrupt, IRQF_SHARED,
10788c2ecf20Sopenharmony_ci			  dev->name, dev);
10798c2ecf20Sopenharmony_ci	if (err)
10808c2ecf20Sopenharmony_ci		goto exit_close_candev;
10818c2ecf20Sopenharmony_ci
10828c2ecf20Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
10838c2ecf20Sopenharmony_ci
10848c2ecf20Sopenharmony_ci	napi_enable(&priv->napi);
10858c2ecf20Sopenharmony_ci	grcan_start(dev);
10868c2ecf20Sopenharmony_ci	if (!(priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
10878c2ecf20Sopenharmony_ci		netif_start_queue(dev);
10888c2ecf20Sopenharmony_ci	priv->resetting = false;
10898c2ecf20Sopenharmony_ci	priv->closing = false;
10908c2ecf20Sopenharmony_ci
10918c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&priv->lock, flags);
10928c2ecf20Sopenharmony_ci
10938c2ecf20Sopenharmony_ci	return 0;
10948c2ecf20Sopenharmony_ci
10958c2ecf20Sopenharmony_ciexit_close_candev:
10968c2ecf20Sopenharmony_ci	close_candev(dev);
10978c2ecf20Sopenharmony_ciexit_free_txdlc:
10988c2ecf20Sopenharmony_ci	kfree(priv->txdlc);
10998c2ecf20Sopenharmony_ciexit_free_echo_skb:
11008c2ecf20Sopenharmony_ci	kfree(priv->echo_skb);
11018c2ecf20Sopenharmony_ciexit_free_dma_buffers:
11028c2ecf20Sopenharmony_ci	grcan_free_dma_buffers(dev);
11038c2ecf20Sopenharmony_ci	return err;
11048c2ecf20Sopenharmony_ci}
11058c2ecf20Sopenharmony_ci
11068c2ecf20Sopenharmony_cistatic int grcan_close(struct net_device *dev)
11078c2ecf20Sopenharmony_ci{
11088c2ecf20Sopenharmony_ci	struct grcan_priv *priv = netdev_priv(dev);
11098c2ecf20Sopenharmony_ci	unsigned long flags;
11108c2ecf20Sopenharmony_ci
11118c2ecf20Sopenharmony_ci	napi_disable(&priv->napi);
11128c2ecf20Sopenharmony_ci
11138c2ecf20Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
11148c2ecf20Sopenharmony_ci
11158c2ecf20Sopenharmony_ci	priv->closing = true;
11168c2ecf20Sopenharmony_ci	if (priv->need_txbug_workaround) {
11178c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&priv->lock, flags);
11188c2ecf20Sopenharmony_ci		del_timer_sync(&priv->hang_timer);
11198c2ecf20Sopenharmony_ci		del_timer_sync(&priv->rr_timer);
11208c2ecf20Sopenharmony_ci		spin_lock_irqsave(&priv->lock, flags);
11218c2ecf20Sopenharmony_ci	}
11228c2ecf20Sopenharmony_ci	netif_stop_queue(dev);
11238c2ecf20Sopenharmony_ci	grcan_stop_hardware(dev);
11248c2ecf20Sopenharmony_ci	priv->can.state = CAN_STATE_STOPPED;
11258c2ecf20Sopenharmony_ci
11268c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&priv->lock, flags);
11278c2ecf20Sopenharmony_ci
11288c2ecf20Sopenharmony_ci	free_irq(dev->irq, dev);
11298c2ecf20Sopenharmony_ci	close_candev(dev);
11308c2ecf20Sopenharmony_ci
11318c2ecf20Sopenharmony_ci	grcan_free_dma_buffers(dev);
11328c2ecf20Sopenharmony_ci	priv->can.echo_skb_max = 0;
11338c2ecf20Sopenharmony_ci	priv->can.echo_skb = NULL;
11348c2ecf20Sopenharmony_ci	kfree(priv->echo_skb);
11358c2ecf20Sopenharmony_ci	kfree(priv->txdlc);
11368c2ecf20Sopenharmony_ci
11378c2ecf20Sopenharmony_ci	return 0;
11388c2ecf20Sopenharmony_ci}
11398c2ecf20Sopenharmony_ci
11408c2ecf20Sopenharmony_cistatic void grcan_transmit_catch_up(struct net_device *dev)
11418c2ecf20Sopenharmony_ci{
11428c2ecf20Sopenharmony_ci	struct grcan_priv *priv = netdev_priv(dev);
11438c2ecf20Sopenharmony_ci	unsigned long flags;
11448c2ecf20Sopenharmony_ci	int work_done;
11458c2ecf20Sopenharmony_ci
11468c2ecf20Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
11478c2ecf20Sopenharmony_ci
11488c2ecf20Sopenharmony_ci	work_done = catch_up_echo_skb(dev, -1, true);
11498c2ecf20Sopenharmony_ci	if (work_done) {
11508c2ecf20Sopenharmony_ci		if (!priv->resetting && !priv->closing &&
11518c2ecf20Sopenharmony_ci		    !(priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY))
11528c2ecf20Sopenharmony_ci			netif_wake_queue(dev);
11538c2ecf20Sopenharmony_ci
11548c2ecf20Sopenharmony_ci		/* With napi we don't get TX interrupts for a while,
11558c2ecf20Sopenharmony_ci		 * so prevent a running reset while catching up
11568c2ecf20Sopenharmony_ci		 */
11578c2ecf20Sopenharmony_ci		if (priv->need_txbug_workaround)
11588c2ecf20Sopenharmony_ci			del_timer(&priv->hang_timer);
11598c2ecf20Sopenharmony_ci	}
11608c2ecf20Sopenharmony_ci
11618c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&priv->lock, flags);
11628c2ecf20Sopenharmony_ci}
11638c2ecf20Sopenharmony_ci
11648c2ecf20Sopenharmony_cistatic int grcan_receive(struct net_device *dev, int budget)
11658c2ecf20Sopenharmony_ci{
11668c2ecf20Sopenharmony_ci	struct grcan_priv *priv = netdev_priv(dev);
11678c2ecf20Sopenharmony_ci	struct grcan_registers __iomem *regs = priv->regs;
11688c2ecf20Sopenharmony_ci	struct grcan_dma *dma = &priv->dma;
11698c2ecf20Sopenharmony_ci	struct net_device_stats *stats = &dev->stats;
11708c2ecf20Sopenharmony_ci	struct can_frame *cf;
11718c2ecf20Sopenharmony_ci	struct sk_buff *skb;
11728c2ecf20Sopenharmony_ci	u32 wr, rd, startrd;
11738c2ecf20Sopenharmony_ci	u32 *slot;
11748c2ecf20Sopenharmony_ci	u32 i, rtr, eff, j, shift;
11758c2ecf20Sopenharmony_ci	int work_done = 0;
11768c2ecf20Sopenharmony_ci
11778c2ecf20Sopenharmony_ci	rd = grcan_read_reg(&regs->rxrd);
11788c2ecf20Sopenharmony_ci	startrd = rd;
11798c2ecf20Sopenharmony_ci	for (work_done = 0; work_done < budget; work_done++) {
11808c2ecf20Sopenharmony_ci		/* Check for packet to receive */
11818c2ecf20Sopenharmony_ci		wr = grcan_read_reg(&regs->rxwr);
11828c2ecf20Sopenharmony_ci		if (rd == wr)
11838c2ecf20Sopenharmony_ci			break;
11848c2ecf20Sopenharmony_ci
11858c2ecf20Sopenharmony_ci		/* Take care of packet */
11868c2ecf20Sopenharmony_ci		skb = alloc_can_skb(dev, &cf);
11878c2ecf20Sopenharmony_ci		if (skb == NULL) {
11888c2ecf20Sopenharmony_ci			netdev_err(dev,
11898c2ecf20Sopenharmony_ci				   "dropping frame: skb allocation failed\n");
11908c2ecf20Sopenharmony_ci			stats->rx_dropped++;
11918c2ecf20Sopenharmony_ci			continue;
11928c2ecf20Sopenharmony_ci		}
11938c2ecf20Sopenharmony_ci
11948c2ecf20Sopenharmony_ci		slot = dma->rx.buf + rd;
11958c2ecf20Sopenharmony_ci		eff = slot[0] & GRCAN_MSG_IDE;
11968c2ecf20Sopenharmony_ci		rtr = slot[0] & GRCAN_MSG_RTR;
11978c2ecf20Sopenharmony_ci		if (eff) {
11988c2ecf20Sopenharmony_ci			cf->can_id = ((slot[0] & GRCAN_MSG_EID)
11998c2ecf20Sopenharmony_ci				      >> GRCAN_MSG_EID_BIT);
12008c2ecf20Sopenharmony_ci			cf->can_id |= CAN_EFF_FLAG;
12018c2ecf20Sopenharmony_ci		} else {
12028c2ecf20Sopenharmony_ci			cf->can_id = ((slot[0] & GRCAN_MSG_BID)
12038c2ecf20Sopenharmony_ci				      >> GRCAN_MSG_BID_BIT);
12048c2ecf20Sopenharmony_ci		}
12058c2ecf20Sopenharmony_ci		cf->can_dlc = get_can_dlc((slot[1] & GRCAN_MSG_DLC)
12068c2ecf20Sopenharmony_ci					  >> GRCAN_MSG_DLC_BIT);
12078c2ecf20Sopenharmony_ci		if (rtr) {
12088c2ecf20Sopenharmony_ci			cf->can_id |= CAN_RTR_FLAG;
12098c2ecf20Sopenharmony_ci		} else {
12108c2ecf20Sopenharmony_ci			for (i = 0; i < cf->can_dlc; i++) {
12118c2ecf20Sopenharmony_ci				j = GRCAN_MSG_DATA_SLOT_INDEX(i);
12128c2ecf20Sopenharmony_ci				shift = GRCAN_MSG_DATA_SHIFT(i);
12138c2ecf20Sopenharmony_ci				cf->data[i] = (u8)(slot[j] >> shift);
12148c2ecf20Sopenharmony_ci			}
12158c2ecf20Sopenharmony_ci		}
12168c2ecf20Sopenharmony_ci
12178c2ecf20Sopenharmony_ci		/* Update statistics and read pointer */
12188c2ecf20Sopenharmony_ci		stats->rx_packets++;
12198c2ecf20Sopenharmony_ci		stats->rx_bytes += cf->can_dlc;
12208c2ecf20Sopenharmony_ci		netif_receive_skb(skb);
12218c2ecf20Sopenharmony_ci
12228c2ecf20Sopenharmony_ci		rd = grcan_ring_add(rd, GRCAN_MSG_SIZE, dma->rx.size);
12238c2ecf20Sopenharmony_ci	}
12248c2ecf20Sopenharmony_ci
12258c2ecf20Sopenharmony_ci	/* Make sure everything is read before allowing hardware to
12268c2ecf20Sopenharmony_ci	 * use the memory
12278c2ecf20Sopenharmony_ci	 */
12288c2ecf20Sopenharmony_ci	mb();
12298c2ecf20Sopenharmony_ci
12308c2ecf20Sopenharmony_ci	/* Update read pointer - no need to check for ongoing */
12318c2ecf20Sopenharmony_ci	if (likely(rd != startrd))
12328c2ecf20Sopenharmony_ci		grcan_write_reg(&regs->rxrd, rd);
12338c2ecf20Sopenharmony_ci
12348c2ecf20Sopenharmony_ci	return work_done;
12358c2ecf20Sopenharmony_ci}
12368c2ecf20Sopenharmony_ci
12378c2ecf20Sopenharmony_cistatic int grcan_poll(struct napi_struct *napi, int budget)
12388c2ecf20Sopenharmony_ci{
12398c2ecf20Sopenharmony_ci	struct grcan_priv *priv = container_of(napi, struct grcan_priv, napi);
12408c2ecf20Sopenharmony_ci	struct net_device *dev = priv->dev;
12418c2ecf20Sopenharmony_ci	struct grcan_registers __iomem *regs = priv->regs;
12428c2ecf20Sopenharmony_ci	unsigned long flags;
12438c2ecf20Sopenharmony_ci	int work_done;
12448c2ecf20Sopenharmony_ci
12458c2ecf20Sopenharmony_ci	work_done = grcan_receive(dev, budget);
12468c2ecf20Sopenharmony_ci
12478c2ecf20Sopenharmony_ci	grcan_transmit_catch_up(dev);
12488c2ecf20Sopenharmony_ci
12498c2ecf20Sopenharmony_ci	if (work_done < budget) {
12508c2ecf20Sopenharmony_ci		napi_complete(napi);
12518c2ecf20Sopenharmony_ci
12528c2ecf20Sopenharmony_ci		/* Guarantee no interference with a running reset that otherwise
12538c2ecf20Sopenharmony_ci		 * could turn off interrupts.
12548c2ecf20Sopenharmony_ci		 */
12558c2ecf20Sopenharmony_ci		spin_lock_irqsave(&priv->lock, flags);
12568c2ecf20Sopenharmony_ci
12578c2ecf20Sopenharmony_ci		/* Enable tx and rx interrupts again. No need to check
12588c2ecf20Sopenharmony_ci		 * priv->closing as napi_disable in grcan_close is waiting for
12598c2ecf20Sopenharmony_ci		 * scheduled napi calls to finish.
12608c2ecf20Sopenharmony_ci		 */
12618c2ecf20Sopenharmony_ci		grcan_set_bits(&regs->imr, GRCAN_IRQ_TX | GRCAN_IRQ_RX);
12628c2ecf20Sopenharmony_ci
12638c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&priv->lock, flags);
12648c2ecf20Sopenharmony_ci	}
12658c2ecf20Sopenharmony_ci
12668c2ecf20Sopenharmony_ci	return work_done;
12678c2ecf20Sopenharmony_ci}
12688c2ecf20Sopenharmony_ci
12698c2ecf20Sopenharmony_ci/* Work tx bug by waiting while for the risky situation to clear. If that fails,
12708c2ecf20Sopenharmony_ci * drop a frame in one-shot mode or indicate a busy device otherwise.
12718c2ecf20Sopenharmony_ci *
12728c2ecf20Sopenharmony_ci * Returns 0 on successful wait. Otherwise it sets *netdev_tx_status to the
12738c2ecf20Sopenharmony_ci * value that should be returned by grcan_start_xmit when aborting the xmit.
12748c2ecf20Sopenharmony_ci */
12758c2ecf20Sopenharmony_cistatic int grcan_txbug_workaround(struct net_device *dev, struct sk_buff *skb,
12768c2ecf20Sopenharmony_ci				  u32 txwr, u32 oneshotmode,
12778c2ecf20Sopenharmony_ci				  netdev_tx_t *netdev_tx_status)
12788c2ecf20Sopenharmony_ci{
12798c2ecf20Sopenharmony_ci	struct grcan_priv *priv = netdev_priv(dev);
12808c2ecf20Sopenharmony_ci	struct grcan_registers __iomem *regs = priv->regs;
12818c2ecf20Sopenharmony_ci	struct grcan_dma *dma = &priv->dma;
12828c2ecf20Sopenharmony_ci	int i;
12838c2ecf20Sopenharmony_ci	unsigned long flags;
12848c2ecf20Sopenharmony_ci
12858c2ecf20Sopenharmony_ci	/* Wait a while for ongoing to be cleared or read pointer to catch up to
12868c2ecf20Sopenharmony_ci	 * write pointer. The latter is needed due to a bug in older versions of
12878c2ecf20Sopenharmony_ci	 * GRCAN in which ONGOING is not cleared properly one-shot mode when a
12888c2ecf20Sopenharmony_ci	 * transmission fails.
12898c2ecf20Sopenharmony_ci	 */
12908c2ecf20Sopenharmony_ci	for (i = 0; i < GRCAN_SHORTWAIT_USECS; i++) {
12918c2ecf20Sopenharmony_ci		udelay(1);
12928c2ecf20Sopenharmony_ci		if (!grcan_read_bits(&regs->txctrl, GRCAN_TXCTRL_ONGOING) ||
12938c2ecf20Sopenharmony_ci		    grcan_read_reg(&regs->txrd) == txwr) {
12948c2ecf20Sopenharmony_ci			return 0;
12958c2ecf20Sopenharmony_ci		}
12968c2ecf20Sopenharmony_ci	}
12978c2ecf20Sopenharmony_ci
12988c2ecf20Sopenharmony_ci	/* Clean up, in case the situation was not resolved */
12998c2ecf20Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
13008c2ecf20Sopenharmony_ci	if (!priv->resetting && !priv->closing) {
13018c2ecf20Sopenharmony_ci		/* Queue might have been stopped earlier in grcan_start_xmit */
13028c2ecf20Sopenharmony_ci		if (grcan_txspace(dma->tx.size, txwr, priv->eskbp))
13038c2ecf20Sopenharmony_ci			netif_wake_queue(dev);
13048c2ecf20Sopenharmony_ci		/* Set a timer to resolve a hanged tx controller */
13058c2ecf20Sopenharmony_ci		if (!timer_pending(&priv->hang_timer))
13068c2ecf20Sopenharmony_ci			grcan_reset_timer(&priv->hang_timer,
13078c2ecf20Sopenharmony_ci					  priv->can.bittiming.bitrate);
13088c2ecf20Sopenharmony_ci	}
13098c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&priv->lock, flags);
13108c2ecf20Sopenharmony_ci
13118c2ecf20Sopenharmony_ci	if (oneshotmode) {
13128c2ecf20Sopenharmony_ci		/* In one-shot mode we should never end up here because
13138c2ecf20Sopenharmony_ci		 * then the interrupt handler increases txrd on TXLOSS,
13148c2ecf20Sopenharmony_ci		 * but it is consistent with one-shot mode to drop the
13158c2ecf20Sopenharmony_ci		 * frame in this case.
13168c2ecf20Sopenharmony_ci		 */
13178c2ecf20Sopenharmony_ci		kfree_skb(skb);
13188c2ecf20Sopenharmony_ci		*netdev_tx_status = NETDEV_TX_OK;
13198c2ecf20Sopenharmony_ci	} else {
13208c2ecf20Sopenharmony_ci		/* In normal mode the socket-can transmission queue get
13218c2ecf20Sopenharmony_ci		 * to keep the frame so that it can be retransmitted
13228c2ecf20Sopenharmony_ci		 * later
13238c2ecf20Sopenharmony_ci		 */
13248c2ecf20Sopenharmony_ci		*netdev_tx_status = NETDEV_TX_BUSY;
13258c2ecf20Sopenharmony_ci	}
13268c2ecf20Sopenharmony_ci	return -EBUSY;
13278c2ecf20Sopenharmony_ci}
13288c2ecf20Sopenharmony_ci
13298c2ecf20Sopenharmony_ci/* Notes on the tx cyclic buffer handling:
13308c2ecf20Sopenharmony_ci *
13318c2ecf20Sopenharmony_ci * regs->txwr	- the next slot for the driver to put data to be sent
13328c2ecf20Sopenharmony_ci * regs->txrd	- the next slot for the device to read data
13338c2ecf20Sopenharmony_ci * priv->eskbp	- the next slot for the driver to call can_put_echo_skb for
13348c2ecf20Sopenharmony_ci *
13358c2ecf20Sopenharmony_ci * grcan_start_xmit can enter more messages as long as regs->txwr does
13368c2ecf20Sopenharmony_ci * not reach priv->eskbp (within 1 message gap)
13378c2ecf20Sopenharmony_ci *
13388c2ecf20Sopenharmony_ci * The device sends messages until regs->txrd reaches regs->txwr
13398c2ecf20Sopenharmony_ci *
13408c2ecf20Sopenharmony_ci * The interrupt calls handler calls can_put_echo_skb until
13418c2ecf20Sopenharmony_ci * priv->eskbp reaches regs->txrd
13428c2ecf20Sopenharmony_ci */
13438c2ecf20Sopenharmony_cistatic netdev_tx_t grcan_start_xmit(struct sk_buff *skb,
13448c2ecf20Sopenharmony_ci				    struct net_device *dev)
13458c2ecf20Sopenharmony_ci{
13468c2ecf20Sopenharmony_ci	struct grcan_priv *priv = netdev_priv(dev);
13478c2ecf20Sopenharmony_ci	struct grcan_registers __iomem *regs = priv->regs;
13488c2ecf20Sopenharmony_ci	struct grcan_dma *dma = &priv->dma;
13498c2ecf20Sopenharmony_ci	struct can_frame *cf = (struct can_frame *)skb->data;
13508c2ecf20Sopenharmony_ci	u32 id, txwr, txrd, space, txctrl;
13518c2ecf20Sopenharmony_ci	int slotindex;
13528c2ecf20Sopenharmony_ci	u32 *slot;
13538c2ecf20Sopenharmony_ci	u32 i, rtr, eff, dlc, tmp, err;
13548c2ecf20Sopenharmony_ci	int j, shift;
13558c2ecf20Sopenharmony_ci	unsigned long flags;
13568c2ecf20Sopenharmony_ci	u32 oneshotmode = priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT;
13578c2ecf20Sopenharmony_ci
13588c2ecf20Sopenharmony_ci	if (can_dropped_invalid_skb(dev, skb))
13598c2ecf20Sopenharmony_ci		return NETDEV_TX_OK;
13608c2ecf20Sopenharmony_ci
13618c2ecf20Sopenharmony_ci	/* Trying to transmit in silent mode will generate error interrupts, but
13628c2ecf20Sopenharmony_ci	 * this should never happen - the queue should not have been started.
13638c2ecf20Sopenharmony_ci	 */
13648c2ecf20Sopenharmony_ci	if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
13658c2ecf20Sopenharmony_ci		return NETDEV_TX_BUSY;
13668c2ecf20Sopenharmony_ci
13678c2ecf20Sopenharmony_ci	/* Reads of priv->eskbp and shut-downs of the queue needs to
13688c2ecf20Sopenharmony_ci	 * be atomic towards the updates to priv->eskbp and wake-ups
13698c2ecf20Sopenharmony_ci	 * of the queue in the interrupt handler.
13708c2ecf20Sopenharmony_ci	 */
13718c2ecf20Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
13728c2ecf20Sopenharmony_ci
13738c2ecf20Sopenharmony_ci	txwr = grcan_read_reg(&regs->txwr);
13748c2ecf20Sopenharmony_ci	space = grcan_txspace(dma->tx.size, txwr, priv->eskbp);
13758c2ecf20Sopenharmony_ci
13768c2ecf20Sopenharmony_ci	slotindex = txwr / GRCAN_MSG_SIZE;
13778c2ecf20Sopenharmony_ci	slot = dma->tx.buf + txwr;
13788c2ecf20Sopenharmony_ci
13798c2ecf20Sopenharmony_ci	if (unlikely(space == 1))
13808c2ecf20Sopenharmony_ci		netif_stop_queue(dev);
13818c2ecf20Sopenharmony_ci
13828c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&priv->lock, flags);
13838c2ecf20Sopenharmony_ci	/* End of critical section*/
13848c2ecf20Sopenharmony_ci
13858c2ecf20Sopenharmony_ci	/* This should never happen. If circular buffer is full, the
13868c2ecf20Sopenharmony_ci	 * netif_stop_queue should have been stopped already.
13878c2ecf20Sopenharmony_ci	 */
13888c2ecf20Sopenharmony_ci	if (unlikely(!space)) {
13898c2ecf20Sopenharmony_ci		netdev_err(dev, "No buffer space, but queue is non-stopped.\n");
13908c2ecf20Sopenharmony_ci		return NETDEV_TX_BUSY;
13918c2ecf20Sopenharmony_ci	}
13928c2ecf20Sopenharmony_ci
13938c2ecf20Sopenharmony_ci	/* Convert and write CAN message to DMA buffer */
13948c2ecf20Sopenharmony_ci	eff = cf->can_id & CAN_EFF_FLAG;
13958c2ecf20Sopenharmony_ci	rtr = cf->can_id & CAN_RTR_FLAG;
13968c2ecf20Sopenharmony_ci	id = cf->can_id & (eff ? CAN_EFF_MASK : CAN_SFF_MASK);
13978c2ecf20Sopenharmony_ci	dlc = cf->can_dlc;
13988c2ecf20Sopenharmony_ci	if (eff)
13998c2ecf20Sopenharmony_ci		tmp = (id << GRCAN_MSG_EID_BIT) & GRCAN_MSG_EID;
14008c2ecf20Sopenharmony_ci	else
14018c2ecf20Sopenharmony_ci		tmp = (id << GRCAN_MSG_BID_BIT) & GRCAN_MSG_BID;
14028c2ecf20Sopenharmony_ci	slot[0] = (eff ? GRCAN_MSG_IDE : 0) | (rtr ? GRCAN_MSG_RTR : 0) | tmp;
14038c2ecf20Sopenharmony_ci
14048c2ecf20Sopenharmony_ci	slot[1] = ((dlc << GRCAN_MSG_DLC_BIT) & GRCAN_MSG_DLC);
14058c2ecf20Sopenharmony_ci	slot[2] = 0;
14068c2ecf20Sopenharmony_ci	slot[3] = 0;
14078c2ecf20Sopenharmony_ci	for (i = 0; i < dlc; i++) {
14088c2ecf20Sopenharmony_ci		j = GRCAN_MSG_DATA_SLOT_INDEX(i);
14098c2ecf20Sopenharmony_ci		shift = GRCAN_MSG_DATA_SHIFT(i);
14108c2ecf20Sopenharmony_ci		slot[j] |= cf->data[i] << shift;
14118c2ecf20Sopenharmony_ci	}
14128c2ecf20Sopenharmony_ci
14138c2ecf20Sopenharmony_ci	/* Checking that channel has not been disabled. These cases
14148c2ecf20Sopenharmony_ci	 * should never happen
14158c2ecf20Sopenharmony_ci	 */
14168c2ecf20Sopenharmony_ci	txctrl = grcan_read_reg(&regs->txctrl);
14178c2ecf20Sopenharmony_ci	if (!(txctrl & GRCAN_TXCTRL_ENABLE))
14188c2ecf20Sopenharmony_ci		netdev_err(dev, "tx channel spuriously disabled\n");
14198c2ecf20Sopenharmony_ci
14208c2ecf20Sopenharmony_ci	if (oneshotmode && !(txctrl & GRCAN_TXCTRL_SINGLE))
14218c2ecf20Sopenharmony_ci		netdev_err(dev, "one-shot mode spuriously disabled\n");
14228c2ecf20Sopenharmony_ci
14238c2ecf20Sopenharmony_ci	/* Bug workaround for old version of grcan where updating txwr
14248c2ecf20Sopenharmony_ci	 * in the same clock cycle as the controller updates txrd to
14258c2ecf20Sopenharmony_ci	 * the current txwr could hang the can controller
14268c2ecf20Sopenharmony_ci	 */
14278c2ecf20Sopenharmony_ci	if (priv->need_txbug_workaround) {
14288c2ecf20Sopenharmony_ci		txrd = grcan_read_reg(&regs->txrd);
14298c2ecf20Sopenharmony_ci		if (unlikely(grcan_ring_sub(txwr, txrd, dma->tx.size) == 1)) {
14308c2ecf20Sopenharmony_ci			netdev_tx_t txstatus;
14318c2ecf20Sopenharmony_ci
14328c2ecf20Sopenharmony_ci			err = grcan_txbug_workaround(dev, skb, txwr,
14338c2ecf20Sopenharmony_ci						     oneshotmode, &txstatus);
14348c2ecf20Sopenharmony_ci			if (err)
14358c2ecf20Sopenharmony_ci				return txstatus;
14368c2ecf20Sopenharmony_ci		}
14378c2ecf20Sopenharmony_ci	}
14388c2ecf20Sopenharmony_ci
14398c2ecf20Sopenharmony_ci	/* Prepare skb for echoing. This must be after the bug workaround above
14408c2ecf20Sopenharmony_ci	 * as ownership of the skb is passed on by calling can_put_echo_skb.
14418c2ecf20Sopenharmony_ci	 * Returning NETDEV_TX_BUSY or accessing skb or cf after a call to
14428c2ecf20Sopenharmony_ci	 * can_put_echo_skb would be an error unless other measures are
14438c2ecf20Sopenharmony_ci	 * taken.
14448c2ecf20Sopenharmony_ci	 */
14458c2ecf20Sopenharmony_ci	priv->txdlc[slotindex] = cf->can_dlc; /* Store dlc for statistics */
14468c2ecf20Sopenharmony_ci	can_put_echo_skb(skb, dev, slotindex);
14478c2ecf20Sopenharmony_ci
14488c2ecf20Sopenharmony_ci	/* Make sure everything is written before allowing hardware to
14498c2ecf20Sopenharmony_ci	 * read from the memory
14508c2ecf20Sopenharmony_ci	 */
14518c2ecf20Sopenharmony_ci	wmb();
14528c2ecf20Sopenharmony_ci
14538c2ecf20Sopenharmony_ci	/* Update write pointer to start transmission */
14548c2ecf20Sopenharmony_ci	grcan_write_reg(&regs->txwr,
14558c2ecf20Sopenharmony_ci			grcan_ring_add(txwr, GRCAN_MSG_SIZE, dma->tx.size));
14568c2ecf20Sopenharmony_ci
14578c2ecf20Sopenharmony_ci	return NETDEV_TX_OK;
14588c2ecf20Sopenharmony_ci}
14598c2ecf20Sopenharmony_ci
14608c2ecf20Sopenharmony_ci/* ========== Setting up sysfs interface and module parameters ========== */
14618c2ecf20Sopenharmony_ci
14628c2ecf20Sopenharmony_ci#define GRCAN_NOT_BOOL(unsigned_val) ((unsigned_val) > 1)
14638c2ecf20Sopenharmony_ci
14648c2ecf20Sopenharmony_ci#define GRCAN_MODULE_PARAM(name, mtype, valcheckf, desc)		\
14658c2ecf20Sopenharmony_ci	static void grcan_sanitize_##name(struct platform_device *pd)	\
14668c2ecf20Sopenharmony_ci	{								\
14678c2ecf20Sopenharmony_ci		struct grcan_device_config grcan_default_config		\
14688c2ecf20Sopenharmony_ci			= GRCAN_DEFAULT_DEVICE_CONFIG;			\
14698c2ecf20Sopenharmony_ci		if (valcheckf(grcan_module_config.name)) {		\
14708c2ecf20Sopenharmony_ci			dev_err(&pd->dev,				\
14718c2ecf20Sopenharmony_ci				"Invalid module parameter value for "	\
14728c2ecf20Sopenharmony_ci				#name " - setting default\n");		\
14738c2ecf20Sopenharmony_ci			grcan_module_config.name =			\
14748c2ecf20Sopenharmony_ci				grcan_default_config.name;		\
14758c2ecf20Sopenharmony_ci		}							\
14768c2ecf20Sopenharmony_ci	}								\
14778c2ecf20Sopenharmony_ci	module_param_named(name, grcan_module_config.name,		\
14788c2ecf20Sopenharmony_ci			   mtype, 0444);				\
14798c2ecf20Sopenharmony_ci	MODULE_PARM_DESC(name, desc)
14808c2ecf20Sopenharmony_ci
14818c2ecf20Sopenharmony_ci#define GRCAN_CONFIG_ATTR(name, desc)					\
14828c2ecf20Sopenharmony_ci	static ssize_t grcan_store_##name(struct device *sdev,		\
14838c2ecf20Sopenharmony_ci					  struct device_attribute *att,	\
14848c2ecf20Sopenharmony_ci					  const char *buf,		\
14858c2ecf20Sopenharmony_ci					  size_t count)			\
14868c2ecf20Sopenharmony_ci	{								\
14878c2ecf20Sopenharmony_ci		struct net_device *dev = to_net_dev(sdev);		\
14888c2ecf20Sopenharmony_ci		struct grcan_priv *priv = netdev_priv(dev);		\
14898c2ecf20Sopenharmony_ci		u8 val;							\
14908c2ecf20Sopenharmony_ci		int ret;						\
14918c2ecf20Sopenharmony_ci		if (dev->flags & IFF_UP)				\
14928c2ecf20Sopenharmony_ci			return -EBUSY;					\
14938c2ecf20Sopenharmony_ci		ret = kstrtou8(buf, 0, &val);				\
14948c2ecf20Sopenharmony_ci		if (ret < 0 || val > 1)					\
14958c2ecf20Sopenharmony_ci			return -EINVAL;					\
14968c2ecf20Sopenharmony_ci		priv->config.name = val;				\
14978c2ecf20Sopenharmony_ci		return count;						\
14988c2ecf20Sopenharmony_ci	}								\
14998c2ecf20Sopenharmony_ci	static ssize_t grcan_show_##name(struct device *sdev,		\
15008c2ecf20Sopenharmony_ci					 struct device_attribute *att,	\
15018c2ecf20Sopenharmony_ci					 char *buf)			\
15028c2ecf20Sopenharmony_ci	{								\
15038c2ecf20Sopenharmony_ci		struct net_device *dev = to_net_dev(sdev);		\
15048c2ecf20Sopenharmony_ci		struct grcan_priv *priv = netdev_priv(dev);		\
15058c2ecf20Sopenharmony_ci		return sprintf(buf, "%d\n", priv->config.name);		\
15068c2ecf20Sopenharmony_ci	}								\
15078c2ecf20Sopenharmony_ci	static DEVICE_ATTR(name, 0644,					\
15088c2ecf20Sopenharmony_ci			   grcan_show_##name,				\
15098c2ecf20Sopenharmony_ci			   grcan_store_##name);				\
15108c2ecf20Sopenharmony_ci	GRCAN_MODULE_PARAM(name, ushort, GRCAN_NOT_BOOL, desc)
15118c2ecf20Sopenharmony_ci
15128c2ecf20Sopenharmony_ci/* The following configuration options are made available both via module
15138c2ecf20Sopenharmony_ci * parameters and writable sysfs files. See the chapter about GRCAN in the
15148c2ecf20Sopenharmony_ci * documentation for the GRLIB VHDL library for further details.
15158c2ecf20Sopenharmony_ci */
15168c2ecf20Sopenharmony_ciGRCAN_CONFIG_ATTR(enable0,
15178c2ecf20Sopenharmony_ci		  "Configuration of physical interface 0. Determines\n"	\
15188c2ecf20Sopenharmony_ci		  "the \"Enable 0\" bit of the configuration register.\n" \
15198c2ecf20Sopenharmony_ci		  "Format: 0 | 1\nDefault: 0\n");
15208c2ecf20Sopenharmony_ci
15218c2ecf20Sopenharmony_ciGRCAN_CONFIG_ATTR(enable1,
15228c2ecf20Sopenharmony_ci		  "Configuration of physical interface 1. Determines\n"	\
15238c2ecf20Sopenharmony_ci		  "the \"Enable 1\" bit of the configuration register.\n" \
15248c2ecf20Sopenharmony_ci		  "Format: 0 | 1\nDefault: 0\n");
15258c2ecf20Sopenharmony_ci
15268c2ecf20Sopenharmony_ciGRCAN_CONFIG_ATTR(select,
15278c2ecf20Sopenharmony_ci		  "Select which physical interface to use.\n"	\
15288c2ecf20Sopenharmony_ci		  "Format: 0 | 1\nDefault: 0\n");
15298c2ecf20Sopenharmony_ci
15308c2ecf20Sopenharmony_ci/* The tx and rx buffer size configuration options are only available via module
15318c2ecf20Sopenharmony_ci * parameters.
15328c2ecf20Sopenharmony_ci */
15338c2ecf20Sopenharmony_ciGRCAN_MODULE_PARAM(txsize, uint, GRCAN_INVALID_BUFFER_SIZE,
15348c2ecf20Sopenharmony_ci		   "Sets the size of the tx buffer.\n"			\
15358c2ecf20Sopenharmony_ci		   "Format: <unsigned int> where (txsize & ~0x1fffc0) == 0\n" \
15368c2ecf20Sopenharmony_ci		   "Default: 1024\n");
15378c2ecf20Sopenharmony_ciGRCAN_MODULE_PARAM(rxsize, uint, GRCAN_INVALID_BUFFER_SIZE,
15388c2ecf20Sopenharmony_ci		   "Sets the size of the rx buffer.\n"			\
15398c2ecf20Sopenharmony_ci		   "Format: <unsigned int> where (size & ~0x1fffc0) == 0\n" \
15408c2ecf20Sopenharmony_ci		   "Default: 1024\n");
15418c2ecf20Sopenharmony_ci
15428c2ecf20Sopenharmony_ci/* Function that makes sure that configuration done using
15438c2ecf20Sopenharmony_ci * module parameters are set to valid values
15448c2ecf20Sopenharmony_ci */
15458c2ecf20Sopenharmony_cistatic void grcan_sanitize_module_config(struct platform_device *ofdev)
15468c2ecf20Sopenharmony_ci{
15478c2ecf20Sopenharmony_ci	grcan_sanitize_enable0(ofdev);
15488c2ecf20Sopenharmony_ci	grcan_sanitize_enable1(ofdev);
15498c2ecf20Sopenharmony_ci	grcan_sanitize_select(ofdev);
15508c2ecf20Sopenharmony_ci	grcan_sanitize_txsize(ofdev);
15518c2ecf20Sopenharmony_ci	grcan_sanitize_rxsize(ofdev);
15528c2ecf20Sopenharmony_ci}
15538c2ecf20Sopenharmony_ci
15548c2ecf20Sopenharmony_cistatic const struct attribute *const sysfs_grcan_attrs[] = {
15558c2ecf20Sopenharmony_ci	/* Config attrs */
15568c2ecf20Sopenharmony_ci	&dev_attr_enable0.attr,
15578c2ecf20Sopenharmony_ci	&dev_attr_enable1.attr,
15588c2ecf20Sopenharmony_ci	&dev_attr_select.attr,
15598c2ecf20Sopenharmony_ci	NULL,
15608c2ecf20Sopenharmony_ci};
15618c2ecf20Sopenharmony_ci
15628c2ecf20Sopenharmony_cistatic const struct attribute_group sysfs_grcan_group = {
15638c2ecf20Sopenharmony_ci	.name	= "grcan",
15648c2ecf20Sopenharmony_ci	.attrs	= (struct attribute **)sysfs_grcan_attrs,
15658c2ecf20Sopenharmony_ci};
15668c2ecf20Sopenharmony_ci
15678c2ecf20Sopenharmony_ci/* ========== Setting up the driver ========== */
15688c2ecf20Sopenharmony_ci
15698c2ecf20Sopenharmony_cistatic const struct net_device_ops grcan_netdev_ops = {
15708c2ecf20Sopenharmony_ci	.ndo_open	= grcan_open,
15718c2ecf20Sopenharmony_ci	.ndo_stop	= grcan_close,
15728c2ecf20Sopenharmony_ci	.ndo_start_xmit	= grcan_start_xmit,
15738c2ecf20Sopenharmony_ci	.ndo_change_mtu = can_change_mtu,
15748c2ecf20Sopenharmony_ci};
15758c2ecf20Sopenharmony_ci
15768c2ecf20Sopenharmony_cistatic int grcan_setup_netdev(struct platform_device *ofdev,
15778c2ecf20Sopenharmony_ci			      void __iomem *base,
15788c2ecf20Sopenharmony_ci			      int irq, u32 ambafreq, bool txbug)
15798c2ecf20Sopenharmony_ci{
15808c2ecf20Sopenharmony_ci	struct net_device *dev;
15818c2ecf20Sopenharmony_ci	struct grcan_priv *priv;
15828c2ecf20Sopenharmony_ci	struct grcan_registers __iomem *regs;
15838c2ecf20Sopenharmony_ci	int err;
15848c2ecf20Sopenharmony_ci
15858c2ecf20Sopenharmony_ci	dev = alloc_candev(sizeof(struct grcan_priv), 0);
15868c2ecf20Sopenharmony_ci	if (!dev)
15878c2ecf20Sopenharmony_ci		return -ENOMEM;
15888c2ecf20Sopenharmony_ci
15898c2ecf20Sopenharmony_ci	dev->irq = irq;
15908c2ecf20Sopenharmony_ci	dev->flags |= IFF_ECHO;
15918c2ecf20Sopenharmony_ci	dev->netdev_ops = &grcan_netdev_ops;
15928c2ecf20Sopenharmony_ci	dev->sysfs_groups[0] = &sysfs_grcan_group;
15938c2ecf20Sopenharmony_ci
15948c2ecf20Sopenharmony_ci	priv = netdev_priv(dev);
15958c2ecf20Sopenharmony_ci	memcpy(&priv->config, &grcan_module_config,
15968c2ecf20Sopenharmony_ci	       sizeof(struct grcan_device_config));
15978c2ecf20Sopenharmony_ci	priv->dev = dev;
15988c2ecf20Sopenharmony_ci	priv->ofdev_dev = &ofdev->dev;
15998c2ecf20Sopenharmony_ci	priv->regs = base;
16008c2ecf20Sopenharmony_ci	priv->can.bittiming_const = &grcan_bittiming_const;
16018c2ecf20Sopenharmony_ci	priv->can.do_set_bittiming = grcan_set_bittiming;
16028c2ecf20Sopenharmony_ci	priv->can.do_set_mode = grcan_set_mode;
16038c2ecf20Sopenharmony_ci	priv->can.do_get_berr_counter = grcan_get_berr_counter;
16048c2ecf20Sopenharmony_ci	priv->can.clock.freq = ambafreq;
16058c2ecf20Sopenharmony_ci	priv->can.ctrlmode_supported =
16068c2ecf20Sopenharmony_ci		CAN_CTRLMODE_LISTENONLY | CAN_CTRLMODE_ONE_SHOT;
16078c2ecf20Sopenharmony_ci	priv->need_txbug_workaround = txbug;
16088c2ecf20Sopenharmony_ci
16098c2ecf20Sopenharmony_ci	/* Discover if triple sampling is supported by hardware */
16108c2ecf20Sopenharmony_ci	regs = priv->regs;
16118c2ecf20Sopenharmony_ci	grcan_set_bits(&regs->ctrl, GRCAN_CTRL_RESET);
16128c2ecf20Sopenharmony_ci	grcan_set_bits(&regs->conf, GRCAN_CONF_SAM);
16138c2ecf20Sopenharmony_ci	if (grcan_read_bits(&regs->conf, GRCAN_CONF_SAM)) {
16148c2ecf20Sopenharmony_ci		priv->can.ctrlmode_supported |= CAN_CTRLMODE_3_SAMPLES;
16158c2ecf20Sopenharmony_ci		dev_dbg(&ofdev->dev, "Hardware supports triple-sampling\n");
16168c2ecf20Sopenharmony_ci	}
16178c2ecf20Sopenharmony_ci
16188c2ecf20Sopenharmony_ci	spin_lock_init(&priv->lock);
16198c2ecf20Sopenharmony_ci
16208c2ecf20Sopenharmony_ci	if (priv->need_txbug_workaround) {
16218c2ecf20Sopenharmony_ci		timer_setup(&priv->rr_timer, grcan_running_reset, 0);
16228c2ecf20Sopenharmony_ci		timer_setup(&priv->hang_timer, grcan_initiate_running_reset, 0);
16238c2ecf20Sopenharmony_ci	}
16248c2ecf20Sopenharmony_ci
16258c2ecf20Sopenharmony_ci	netif_napi_add(dev, &priv->napi, grcan_poll, GRCAN_NAPI_WEIGHT);
16268c2ecf20Sopenharmony_ci
16278c2ecf20Sopenharmony_ci	SET_NETDEV_DEV(dev, &ofdev->dev);
16288c2ecf20Sopenharmony_ci	dev_info(&ofdev->dev, "regs=0x%p, irq=%d, clock=%d\n",
16298c2ecf20Sopenharmony_ci		 priv->regs, dev->irq, priv->can.clock.freq);
16308c2ecf20Sopenharmony_ci
16318c2ecf20Sopenharmony_ci	err = register_candev(dev);
16328c2ecf20Sopenharmony_ci	if (err)
16338c2ecf20Sopenharmony_ci		goto exit_free_candev;
16348c2ecf20Sopenharmony_ci
16358c2ecf20Sopenharmony_ci	platform_set_drvdata(ofdev, dev);
16368c2ecf20Sopenharmony_ci
16378c2ecf20Sopenharmony_ci	/* Reset device to allow bit-timing to be set. No need to call
16388c2ecf20Sopenharmony_ci	 * grcan_reset at this stage. That is done in grcan_open.
16398c2ecf20Sopenharmony_ci	 */
16408c2ecf20Sopenharmony_ci	grcan_write_reg(&regs->ctrl, GRCAN_CTRL_RESET);
16418c2ecf20Sopenharmony_ci
16428c2ecf20Sopenharmony_ci	return 0;
16438c2ecf20Sopenharmony_ciexit_free_candev:
16448c2ecf20Sopenharmony_ci	free_candev(dev);
16458c2ecf20Sopenharmony_ci	return err;
16468c2ecf20Sopenharmony_ci}
16478c2ecf20Sopenharmony_ci
16488c2ecf20Sopenharmony_cistatic int grcan_probe(struct platform_device *ofdev)
16498c2ecf20Sopenharmony_ci{
16508c2ecf20Sopenharmony_ci	struct device_node *np = ofdev->dev.of_node;
16518c2ecf20Sopenharmony_ci	struct device_node *sysid_parent;
16528c2ecf20Sopenharmony_ci	u32 sysid, ambafreq;
16538c2ecf20Sopenharmony_ci	int irq, err;
16548c2ecf20Sopenharmony_ci	void __iomem *base;
16558c2ecf20Sopenharmony_ci	bool txbug = true;
16568c2ecf20Sopenharmony_ci
16578c2ecf20Sopenharmony_ci	/* Compare GRLIB version number with the first that does not
16588c2ecf20Sopenharmony_ci	 * have the tx bug (see start_xmit)
16598c2ecf20Sopenharmony_ci	 */
16608c2ecf20Sopenharmony_ci	sysid_parent = of_find_node_by_path("/ambapp0");
16618c2ecf20Sopenharmony_ci	if (sysid_parent) {
16628c2ecf20Sopenharmony_ci		err = of_property_read_u32(sysid_parent, "systemid", &sysid);
16638c2ecf20Sopenharmony_ci		if (!err && ((sysid & GRLIB_VERSION_MASK) >=
16648c2ecf20Sopenharmony_ci			     GRCAN_TXBUG_SAFE_GRLIB_VERSION))
16658c2ecf20Sopenharmony_ci			txbug = false;
16668c2ecf20Sopenharmony_ci		of_node_put(sysid_parent);
16678c2ecf20Sopenharmony_ci	}
16688c2ecf20Sopenharmony_ci
16698c2ecf20Sopenharmony_ci	err = of_property_read_u32(np, "freq", &ambafreq);
16708c2ecf20Sopenharmony_ci	if (err) {
16718c2ecf20Sopenharmony_ci		dev_err(&ofdev->dev, "unable to fetch \"freq\" property\n");
16728c2ecf20Sopenharmony_ci		goto exit_error;
16738c2ecf20Sopenharmony_ci	}
16748c2ecf20Sopenharmony_ci
16758c2ecf20Sopenharmony_ci	base = devm_platform_ioremap_resource(ofdev, 0);
16768c2ecf20Sopenharmony_ci	if (IS_ERR(base)) {
16778c2ecf20Sopenharmony_ci		err = PTR_ERR(base);
16788c2ecf20Sopenharmony_ci		goto exit_error;
16798c2ecf20Sopenharmony_ci	}
16808c2ecf20Sopenharmony_ci
16818c2ecf20Sopenharmony_ci	irq = irq_of_parse_and_map(np, GRCAN_IRQIX_IRQ);
16828c2ecf20Sopenharmony_ci	if (!irq) {
16838c2ecf20Sopenharmony_ci		dev_err(&ofdev->dev, "no irq found\n");
16848c2ecf20Sopenharmony_ci		err = -ENODEV;
16858c2ecf20Sopenharmony_ci		goto exit_error;
16868c2ecf20Sopenharmony_ci	}
16878c2ecf20Sopenharmony_ci
16888c2ecf20Sopenharmony_ci	grcan_sanitize_module_config(ofdev);
16898c2ecf20Sopenharmony_ci
16908c2ecf20Sopenharmony_ci	err = grcan_setup_netdev(ofdev, base, irq, ambafreq, txbug);
16918c2ecf20Sopenharmony_ci	if (err)
16928c2ecf20Sopenharmony_ci		goto exit_dispose_irq;
16938c2ecf20Sopenharmony_ci
16948c2ecf20Sopenharmony_ci	return 0;
16958c2ecf20Sopenharmony_ci
16968c2ecf20Sopenharmony_ciexit_dispose_irq:
16978c2ecf20Sopenharmony_ci	irq_dispose_mapping(irq);
16988c2ecf20Sopenharmony_ciexit_error:
16998c2ecf20Sopenharmony_ci	dev_err(&ofdev->dev,
17008c2ecf20Sopenharmony_ci		"%s socket CAN driver initialization failed with error %d\n",
17018c2ecf20Sopenharmony_ci		DRV_NAME, err);
17028c2ecf20Sopenharmony_ci	return err;
17038c2ecf20Sopenharmony_ci}
17048c2ecf20Sopenharmony_ci
17058c2ecf20Sopenharmony_cistatic int grcan_remove(struct platform_device *ofdev)
17068c2ecf20Sopenharmony_ci{
17078c2ecf20Sopenharmony_ci	struct net_device *dev = platform_get_drvdata(ofdev);
17088c2ecf20Sopenharmony_ci	struct grcan_priv *priv = netdev_priv(dev);
17098c2ecf20Sopenharmony_ci
17108c2ecf20Sopenharmony_ci	unregister_candev(dev); /* Will in turn call grcan_close */
17118c2ecf20Sopenharmony_ci
17128c2ecf20Sopenharmony_ci	irq_dispose_mapping(dev->irq);
17138c2ecf20Sopenharmony_ci	netif_napi_del(&priv->napi);
17148c2ecf20Sopenharmony_ci	free_candev(dev);
17158c2ecf20Sopenharmony_ci
17168c2ecf20Sopenharmony_ci	return 0;
17178c2ecf20Sopenharmony_ci}
17188c2ecf20Sopenharmony_ci
17198c2ecf20Sopenharmony_cistatic const struct of_device_id grcan_match[] = {
17208c2ecf20Sopenharmony_ci	{.name = "GAISLER_GRCAN"},
17218c2ecf20Sopenharmony_ci	{.name = "01_03d"},
17228c2ecf20Sopenharmony_ci	{.name = "GAISLER_GRHCAN"},
17238c2ecf20Sopenharmony_ci	{.name = "01_034"},
17248c2ecf20Sopenharmony_ci	{},
17258c2ecf20Sopenharmony_ci};
17268c2ecf20Sopenharmony_ci
17278c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, grcan_match);
17288c2ecf20Sopenharmony_ci
17298c2ecf20Sopenharmony_cistatic struct platform_driver grcan_driver = {
17308c2ecf20Sopenharmony_ci	.driver = {
17318c2ecf20Sopenharmony_ci		.name = DRV_NAME,
17328c2ecf20Sopenharmony_ci		.of_match_table = grcan_match,
17338c2ecf20Sopenharmony_ci	},
17348c2ecf20Sopenharmony_ci	.probe = grcan_probe,
17358c2ecf20Sopenharmony_ci	.remove = grcan_remove,
17368c2ecf20Sopenharmony_ci};
17378c2ecf20Sopenharmony_ci
17388c2ecf20Sopenharmony_cimodule_platform_driver(grcan_driver);
17398c2ecf20Sopenharmony_ci
17408c2ecf20Sopenharmony_ciMODULE_AUTHOR("Aeroflex Gaisler AB.");
17418c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Socket CAN driver for Aeroflex Gaisler GRCAN");
17428c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
1743