Allwinner A10/A20 CAN Controller support
v3 cover Allwinner A10/A20 CAN Controller support
Allwinner A10/A20 CAN Controller support
[PATCH v3 1/2] Device Tree Binding Documentation
[PATCH v3 2/2] Kernel Module
History:
v3: changed error state change handling (thx Andri for the hint)
use bittiming function correct (no need to call it) strip down priv (suggested by Marc) scripts/checkpatch.pl-> no matches anymore sparse -> no errors or warnings anymore
v2: cleaning
v1: initial
--- .../devicetree/bindings/net/can/sunxi_can.txt | 42 ++ drivers/net/can/Kconfig | 10 + drivers/net/can/Makefile | 1 + drivers/net/can/sunxi_can.c | 834 +++++++++++++++++++++ 4 files changed, 887 insertions(+)
PATCH v3 1/2 Allwinner A10/A20 CAN Controller support
This patch adds the Device Tree Binding Documentation for SUNXI CAN
--- .../devicetree/bindings/net/can/sunxi_can.txt | 42 ++ 1 files changed, 42 insertions(+)
Documentation/devicetree/bindings/net/can/sunxi_can.txt new file +42 -0
@@ -0,0 +1,42 @@ +Allwinner A10/A20 CAN controller Device Tree Bindings +----------------------------------------------------- + +Required properties: +- compatible: "allwinner,sunxican" +- reg: physical base address and size of the Allwinner A10/A20 CAN register map. +- interrupts: interrupt specifier for the sole interrupt. +- clocks: phandle and clock specifier. +- pinctrl-0: pin control group to be used for this controller. +- pinctrl-names: must be "default". + + +Example +------- + +SoC common .dtsi file: + + can0_pins_a: can0 <at> 0 { + allwinner,pins = "PH20","PH21"; + allwinner,function = "can"; + allwinner,drive = <0>; + allwinner,pull = <0>; + }; + + can0: can <at> 01c2bc00 { + compatible = "allwinner,sunxican"; + reg = <0x01c2bc00 0x400>; + interrupts = <0 26 4>; + clocks = <&apb1_gates 4>; + status = "disabled"; + #address-cells = <1>; + #size-cells = <0>; + }; + +Board specific .dts file: + + can0: can <at> 01c2bc00 { + pinctrl-names = "default"; + pinctrl-0 = <&can0_pins_a>; + status = "okay"; + }; +
PATCH v3 2/2 Allwinner A10/A20 CAN Controller support
In reply to Gerhard BertelsmannThis patch adds the Allwinner A10/A20 Kernel module
--- drivers/net/can/Kconfig | 10 + drivers/net/can/Makefile | 1 + drivers/net/can/sunxi_can.c | 834 +++++++++++++++++++++ 3 files changed, 843 insertions(+)
drivers/net/can/Kconfig +10 -0
@@ -129,6 +129,16 @@ config CAN_RCAR To compile this driver as a module, choose M here: the module will be called rcar_can. +config CAN_SUNXI + tristate "Allwinner SUNXI CAN controller" + depends on MACH_SUN4I || MACH_SUN7I + ---help--- + Say Y here if you want to use CAN controller found on Allwinner + A10/A20 SoCs. + + To compile this driver as a module, choose M here: the module will + be called sunxi_can. + config CAN_XILINXCAN tristate "Xilinx CAN" depends on ARCH_ZYNQ || ARM64 || MICROBLAZE || COMPILE_TEST
drivers/net/can/Makefile +1 -0
@@ -27,6 +27,7 @@ obj-$(CONFIG_CAN_FLEXCAN) += flexcan.o obj-$(CONFIG_PCH_CAN) += pch_can.o obj-$(CONFIG_CAN_GRCAN) += grcan.o obj-$(CONFIG_CAN_RCAR) += rcar_can.o +obj-$(CONFIG_CAN_SUNXI) += sunxi_can.o obj-$(CONFIG_CAN_XILINXCAN) += xilinx_can.o subdir-ccflags-y += -D__CHECK_ENDIAN__
drivers/net/can/sunxi_can.c new file +834 -0
834 lines not on this page · raw message
2015-09-04 16:29 GMT+02:00 Gerhard Bertelsmann <[email protected]>:
Quoted 126 lines
This patch adds the Allwinner A10/A20 Kernel module
drivers/net/can/sunxi_can.c new file +123
-0+ +/* Registers address */ +#define CAN_BASE0 0x01C2BC00 +#define CAN_MSEL_ADDR 0x0000/* CAN Mode Select */ +#define CAN_CMD_ADDR 0x0004/* CAN Command */ +#define CAN_STA_ADDR 0x0008/* CAN Status */ +#define CAN_INT_ADDR 0x000c/* CAN Interrupt Flag */ +#define CAN_INTEN_ADDR 0x0010/* CAN Interrupt Enable */ +#define CAN_BTIME_ADDR 0x0014/* CAN Bus Timing 0 */ +#define CAN_TEWL_ADDR 0x0018/* CAN Tx Error Warning Limit */ +#define CAN_ERRC_ADDR 0x001c/* CAN Error Counter */ +#define CAN_RMCNT_ADDR 0x0020/* CAN Receive Message Counter */ +#define CAN_RBUFSA_ADDR 0x0024/* CAN Receive Buffer Start Address */ +#define CAN_BUF0_ADDR 0x0040/* CAN Tx/Rx Buffer 0 */ +#define CAN_BUF1_ADDR 0x0044/* CAN Tx/Rx Buffer 1 */ +#define CAN_BUF2_ADDR 0x0048/* CAN Tx/Rx Buffer 2 */ +#define CAN_BUF3_ADDR 0x004c/* CAN Tx/Rx Buffer 3 */ +#define CAN_BUF4_ADDR 0x0050/* CAN Tx/Rx Buffer 4 */ +#define CAN_BUF5_ADDR 0x0054/* CAN Tx/Rx Buffer 5 */ +#define CAN_BUF6_ADDR 0x0058/* CAN Tx/Rx Buffer 6 */ +#define CAN_BUF7_ADDR 0x005c/* CAN Tx/Rx Buffer 7 */ +#define CAN_BUF8_ADDR 0x0060/* CAN Tx/Rx Buffer 8 */ +#define CAN_BUF9_ADDR 0x0064/* CAN Tx/Rx Buffer 9 */ +#define CAN_BUF10_ADDR 0x0068/* CAN Tx/Rx Buffer 10 */ +#define CAN_BUF11_ADDR 0x006c/* CAN Tx/Rx Buffer 11 */ +#define CAN_BUF12_ADDR 0x0070/* CAN Tx/Rx Buffer 12 */ +#define CAN_ACPC_ADDR 0x0040/* CAN Acceptance Code 0 */ +#define CAN_ACPM_ADDR 0x0044/* CAN Acceptance Mask 0 */ +#define CAN_RBUF_RBACK_START_ADDR 0x0180/* CAN transmit buffer start */ +#define CAN_RBUF_RBACK_END_ADDR 0x01b0/* CAN transmit buffer end */ + +/* Controller Register Description */ + +/* mode select register (r/w) +* offset:0x0000 default:0x0000_0001 +*/ +#define SLEEP_MODE (0x01 << 4)/* write in reset mode */ +#define WAKE_UP (0x00 << 4) +#define SINGLE_FILTER (0x01 << 3)/* write in reset mode */ +#define DUAL_FILTERS (0x00 << 3) +#define LOOPBACK_MODE BIT(2) +#define LISTEN_ONLY_MODE BIT(1) +#define RESET_MODE BIT(0) + +/* command register (w) +* offset:0x0004 default:0x0000_0000 +*/ +#define BUS_OFF_REQ BIT(5) +#define SELF_RCV_REQ BIT(4) +#define CLEAR_OR_FLAG BIT(3) +#define RELEASE_RBUF BIT(2) +#define ABORT_REQ BIT(1) +#define TRANS_REQ BIT(0) + +/* status register (r) +* offset:0x0008 default:0x0000_003c +*/ +#define BIT_ERR (0x00 << 22) +#define FORM_ERR (0x01 << 22) +#define STUFF_ERR (0x02 << 22) +#define OTHER_ERR (0x03 << 22) +#define MASK_ERR (0x03 << 22) +#define ERR_DIR BIT(21) +#define ERR_SEG_CODE (0x1f << 16) +#define START (0x03 << 16) +#define ID28_21 (0x02 << 16) +#define ID20_18 (0x06 << 16) +#define SRTR (0x04 << 16) +#define IDE (0x05 << 16) +#define ID17_13 (0x07 << 16) +#define ID12_5 (0x0f << 16) +#define ID4_0 (0x0e << 16) +#define RTR (0x0c << 16) +#define RB1 (0x0d << 16) +#define RB0 (0x09 << 16) +#define DLEN (0x0b << 16) +#define DATA_FIELD (0x0a << 16) +#define CRC_SEQUENCE (0x08 << 16) +#define CRC_DELIMITER (0x18 << 16) +#define ACK (0x19 << 16) +#define ACK_DELIMITER (0x1b << 16) +#define END (0x1a << 16) +#define INTERMISSION (0x12 << 16) +#define ACTIVE_ERROR (0x11 << 16) +#define PASSIVE_ERROR (0x16 << 16) +#define TOLERATE_DOMINANT_BITS (0x13 << 16) +#define ERROR_DELIMITER (0x17 << 16) +#define OVERLOAD (0x1c << 16) +#define BUS_OFF BIT(7) +#define ERR_STA BIT(6) +#define TRANS_BUSY BIT(5) +#define RCV_BUSY BIT(4) +#define TRANS_OVER BIT(3) +#define TBUF_RDY BIT(2) +#define DATA_ORUN BIT(1) +#define RBUF_RDY BIT(0) + +/* interrupt register (r) +* offset:0x000c default:0x0000_0000 +*/ +#define BUS_ERR BIT(7) +#define ARB_LOST BIT(6) +#define ERR_PASSIVE BIT(5) +#define WAKEUP BIT(4) +#define DATA_OR BIT(3) +#define ERR_WRN BIT(2) +#define TBUF_VLD BIT(1) +#define RBUF_VLD BIT(0) + +/* interrupt enable register (r/w) +* offset:0x0010 default:0x0000_0000 +*/ +#define BERR_IRQ_EN BIT(7) +#define ARB_LOST_IRQ_EN BIT(6) +#define ERR_PASSIVE_IRQ_EN BIT(5) +#define WAKEUP_IRQ_EN BIT(4) +#define OR_IRQ_EN BIT(3) +#define ERR_WRN_IRQ_EN BIT(2) +#define TX_IRQ_EN BIT(1) +#define RX_IRQ_EN BIT(0) + +/* error code */ +#define ERR_INRCV (0x1 << 5)
drivers/net/can/sunxi_can.c line 177
Quoted 10 lines
drivers/net/can/sunxi_can.c new file +10
-0+#define ERR_INTRANS (0x0 << 5) + +/* filter mode */ +#define FILTER_CLOSE 0 +#define SINGLE_FLTER_MODE 1 +#define DUAL_FILTER_MODE 2 + +/* max. number of interrupts handled in ISR */ +#define SUNXI_CAN_MAX_IRQ 20 +
Might be a good idea to use a prefix for all defines to avoid possible conflicts. Something like SUNXI_.
There have previously been conflicts in the SJA1000 driver, See [1].
Signature · 14 lines
Med Vänliga Hälsningar / Best Regards ******************************************************************* Mirza Krak Host Mobility AB [email protected] Anders Personsgatan 12, 416 64 Göteborg Sweden http://www.hostmobility.com Direct: +46 31 31 32 704 Phone: +46 31 31 32 700 Fax: +46 31 80 67 51 Mobile: +46 730 28 06 22 *******************************************************************
Hi Mirza,
Am 2015-09-05 20:37, schrieb Mirza Krak:
Quoted 10 lines
Might be a good idea to use a prefix for all defines to avoid possible conflicts. Something like
SUNXI_.There have previously been conflicts in the SJA1000 driver, See [1].
thx for your hint. Do you think that's really needed ? The scope of the defines are limited to this file. There is no other hardware dependant code involved.
IMHO the SJA1000 is a different story. The SJA1000 could be connected via different busses where other registers are involved.
Nevertheles if the gurus here say I have to change this I'm going to add a prefix to the defines.
Regards
Gerd
2015-09-07 18:25 GMT+02:00 Gerhard Bertelsmann <[email protected]>:
Quoted 6 lines
Hi Mirza,
thx for your hint. Do you think that's really needed ? The scope of the defines are limited to this file. There is no other hardware dependant code involved.
Well if we look at the files that you are including, chances are that they might define something and produce "redefinition" errors. Which was the case for the SJA1000 driver as well.
I am mostly reacting to defines like END, ACK, WAKEUP.
Quoted 3 lines
IMHO the SJA1000 is a different story. The SJA1000 could be connected via different busses where other registers are involved.
The fact that the SJA1000 driver has a different structure had nothing to do with the conflict. Since the conflict was in arch/sh/include/asm/ptrace_32.h
Quoted 3 lines
Nevertheles if the gurus here say I have to change this I'm going to add a prefix to the defines.
Agreed :). This might be a no- issue but I am putting it out there.
Signature · 14 lines
Med Vänliga Hälsningar / Best Regards ******************************************************************* Mirza Krak Host Mobility AB [email protected] Anders Personsgatan 12, 416 64 Göteborg Sweden http://www.hostmobility.com Direct: +46 31 31 32 704 Phone: +46 31 31 32 700 Fax: +46 31 80 67 51 Mobile: +46 730 28 06 22 *******************************************************************
Marc Kleine-Budde
maintainerQuoted 3 lines · Gerhard Bertelsmann
This patch adds the Allwinner A10/A20 Kernel module
Signed-off-by Gerhard Bertelsmann < [email protected] >
^ ^
Please remove the space.
Quoted 1 line
Submitting a patch includes testing it :) You can remove that tag.
Quoted 48 lines
drivers/net/can/sunxi_can.c new file +48
-0+static int sunxican_probe (struct platform_device *pdev) +{ +struct resource *res; +struct clk *clk; +void __iomem *addr; +int err, irq; +u32 temp_irqen; +struct net_device *dev; +struct sunxican_priv *priv; + + clk =clk_get (&pdev->dev,"apb1_can" ); +if (IS_ERR (clk)) { +dev_err (&pdev->dev,"no clock defined\n" ); + err = -ENODEV ; + goto exit; + } +/* turn on clocking for CAN peripheral block */ + err =clk_prepare_enable (clk); +if (err) { +dev_err (&pdev->dev,"could not enable clocking (apb1_can)\n" ); + goto exit; + } + res =platform_get_resource (pdev,IORESOURCE_MEM ,0 ); + irq =platform_get_irq (pdev,0 ); + +if (!res || irq <=0 ) { +dev_err (&pdev->dev,"could not get a valid irq\n" ); + err = -ENODEV ; + goto exit_put; + } +if (!request_mem_region (res->start,resource_size (res), pdev->name)) { +dev_err (&pdev->dev,"could not get io memory resource\n" ); + err = -EBUSY ; + goto exit_put; + } + addr =ioremap_nocache (res->start,resource_size (res)); +if (!addr) { +dev_err (&pdev->dev,"could not map io memory\n" ); + err = -ENOMEM ; + goto exit_release; + } + dev =alloc_candev (sizeof (struct sunxican_priv ),1 ); + +if (!dev) { +dev_err (&pdev->dev, +"could not allocate memory for CAN device\n" ); + err = -ENOMEM ; + goto exit_iounmap;
drivers/net/can/sunxi_can.c line 741
Quoted 10 lines
drivers/net/can/sunxi_can.c new file +10
-0+ } + + dev->netdev_ops = &sunxican_netdev_ops; + dev->irq = irq; + dev->flags |=IFF_ECHO ; + + priv =netdev_priv (dev); + priv->can.clock.freq =clk_get_rate (clk); + priv->can.bittiming_const = &sunxican_bittiming_const; + priv->can.do_set_bittiming = sunxican_set_bittiming;
Please don't do this, call set_bittiming during open();
Quoted 83 lines
drivers/net/can/sunxi_can.c new file +83
-0+ priv->can.do_set_mode = sunxican_set_mode; + priv->can.do_get_berr_counter = sunxican_get_berr_counter; + priv->can.ctrlmode_supported =CAN_CTRLMODE_BERR_REPORTING | +CAN_CTRLMODE_LISTENONLY | +CAN_CTRLMODE_LOOPBACK | +CAN_CTRLMODE_3_SAMPLES ; + priv->base = addr; + priv->clk = clk; +spin_lock_init (&priv->cmdreg_lock); + +/* enable CAN specific interrupts */ +set_reset_mode (dev); + temp_irqen =BERR_IRQ_EN |ERR_PASSIVE_IRQ_EN |OR_IRQ_EN |RX_IRQ_EN ; +writel (readl (priv->base +CAN_INTEN_ADDR ) | temp_irqen, + priv->base +CAN_INTEN_ADDR ); + +platform_set_drvdata (pdev, dev); +SET_NETDEV_DEV (dev, &pdev->dev); + + err =register_candev (dev); +if (err) { +dev_err (&pdev->dev,"registering %s failed (err=%d)\n" , +DRV_NAME , err); + goto exit_free; + } +devm_can_led_init (dev); + +dev_info (&pdev->dev,"device registered (base=%p, irq=%d)\n" , + priv->base, dev->irq); + +return 0 ; + +exit_free: +free_candev (dev); +exit_iounmap: +iounmap (addr); +exit_release: +release_mem_region (res->start,resource_size (res)); +exit_put: +clk_put (clk); +exit: +return err; +} + +static int __maybe_unused sunxi_can_suspend (struct device *device) +{ +struct net_device *dev =dev_get_drvdata (device); +struct sunxican_priv *priv =netdev_priv (dev); + +if (netif_running (dev)) { +netif_stop_queue (dev); +netif_device_detach (dev); + } + priv->can.state =CAN_STATE_SLEEPING ; + +return 0 ; +} + +static int __maybe_unused sunxi_can_resume (struct device *device) +{ +struct net_device *dev =dev_get_drvdata (device); +struct sunxican_priv *priv =netdev_priv (dev); + + priv->can.state =CAN_STATE_ERROR_ACTIVE ; +if (netif_running (dev)) { +netif_device_attach (dev); +netif_start_queue (dev); + } +return 0 ; +} + +static SIMPLE_DEV_PM_OPS(sunxi_can_pm_ops, sunxi_can_suspend, sunxi_can_resume); + +static struct platform_driver sunxi_can_driver = { + .driver = { + .name =DRV_NAME , + .owner =THIS_MODULE , + .pm = &sunxi_can_pm_ops, + .of_match_table = sunxican_of_match, + }, + .probe = sunxican_probe, + .remove = sunxican_remove, + .id_table = sunxican_id_table,
drivers/net/can/sunxi_can.c line 834
Quoted 10 lines
drivers/net/can/sunxi_can.c new file +10
-0+}; + +module_platform_driver (sunxi_can_driver); + +MODULE_AUTHOR ("Peter Chen <[email protected]>" ); +MODULE_AUTHOR ("Gerhard Bertelsmann <[email protected]>" ); +MODULE_LICENSE ("Dual BSD/GPL" ); +MODULE_DESCRIPTION (DRV_NAME "CAN driver for Allwinner SoCs (A10/A20)" ); +MODULE_VERSION (DRV_MODULE_VERSION ); +
Marc
Signature · 4 lines
Pengutronix e.K. | Marc Kleine-Budde | Industrial Linux Solutions | Phone: +49-231-2826-924 | Vertretung West/Dortmund | Fax: +49-5121-206917-5555 | Amtsgericht Hildesheim, HRA 2686 | http://www.pengutronix.de |