1*b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 203fd3cf5SKurt Van Dijck /* 303fd3cf5SKurt Van Dijck * softing common interfaces 403fd3cf5SKurt Van Dijck * 503fd3cf5SKurt Van Dijck * by Kurt Van Dijck, 2008-2010 603fd3cf5SKurt Van Dijck */ 703fd3cf5SKurt Van Dijck 803fd3cf5SKurt Van Dijck #include <linux/atomic.h> 903fd3cf5SKurt Van Dijck #include <linux/netdevice.h> 1003fd3cf5SKurt Van Dijck #include <linux/ktime.h> 1103fd3cf5SKurt Van Dijck #include <linux/mutex.h> 1203fd3cf5SKurt Van Dijck #include <linux/spinlock.h> 1303fd3cf5SKurt Van Dijck #include <linux/can.h> 1403fd3cf5SKurt Van Dijck #include <linux/can/dev.h> 1503fd3cf5SKurt Van Dijck 1603fd3cf5SKurt Van Dijck #include "softing_platform.h" 1703fd3cf5SKurt Van Dijck 1803fd3cf5SKurt Van Dijck struct softing; 1903fd3cf5SKurt Van Dijck 2003fd3cf5SKurt Van Dijck struct softing_priv { 2103fd3cf5SKurt Van Dijck struct can_priv can; /* must be the first member! */ 2203fd3cf5SKurt Van Dijck struct net_device *netdev; 2303fd3cf5SKurt Van Dijck struct softing *card; 2403fd3cf5SKurt Van Dijck struct { 2503fd3cf5SKurt Van Dijck int pending; 2625985edcSLucas De Marchi /* variables which hold the circular buffer */ 2703fd3cf5SKurt Van Dijck int echo_put; 2803fd3cf5SKurt Van Dijck int echo_get; 2903fd3cf5SKurt Van Dijck } tx; 3003fd3cf5SKurt Van Dijck struct can_bittiming_const btr_const; 3103fd3cf5SKurt Van Dijck int index; 3203fd3cf5SKurt Van Dijck uint8_t output; 3303fd3cf5SKurt Van Dijck uint16_t chip; 3403fd3cf5SKurt Van Dijck }; 3503fd3cf5SKurt Van Dijck #define netdev2softing(netdev) ((struct softing_priv *)netdev_priv(netdev)) 3603fd3cf5SKurt Van Dijck 3703fd3cf5SKurt Van Dijck struct softing { 3803fd3cf5SKurt Van Dijck const struct softing_platform_data *pdat; 3903fd3cf5SKurt Van Dijck struct platform_device *pdev; 4003fd3cf5SKurt Van Dijck struct net_device *net[2]; 4103fd3cf5SKurt Van Dijck spinlock_t spin; /* protect this structure & DPRAM access */ 4203fd3cf5SKurt Van Dijck ktime_t ts_ref; 4303fd3cf5SKurt Van Dijck ktime_t ts_overflow; /* timestamp overflow value, in ktime */ 4403fd3cf5SKurt Van Dijck 4503fd3cf5SKurt Van Dijck struct { 4603fd3cf5SKurt Van Dijck /* indication of firmware status */ 4703fd3cf5SKurt Van Dijck int up; 4803fd3cf5SKurt Van Dijck /* protection of the 'up' variable */ 4903fd3cf5SKurt Van Dijck struct mutex lock; 5003fd3cf5SKurt Van Dijck } fw; 5103fd3cf5SKurt Van Dijck struct { 5203fd3cf5SKurt Van Dijck int nr; 5303fd3cf5SKurt Van Dijck int requested; 5403fd3cf5SKurt Van Dijck int svc_count; 5503fd3cf5SKurt Van Dijck unsigned int dpram_position; 5603fd3cf5SKurt Van Dijck } irq; 5703fd3cf5SKurt Van Dijck struct { 5803fd3cf5SKurt Van Dijck int pending; 5903fd3cf5SKurt Van Dijck int last_bus; 6003fd3cf5SKurt Van Dijck /* 6103fd3cf5SKurt Van Dijck * keep the bus that last tx'd a message, 6203fd3cf5SKurt Van Dijck * in order to let every netdev queue resume 6303fd3cf5SKurt Van Dijck */ 6403fd3cf5SKurt Van Dijck } tx; 6503fd3cf5SKurt Van Dijck __iomem uint8_t *dpram; 6603fd3cf5SKurt Van Dijck unsigned long dpram_phys; 6703fd3cf5SKurt Van Dijck unsigned long dpram_size; 6803fd3cf5SKurt Van Dijck struct { 6903fd3cf5SKurt Van Dijck uint16_t fw_version, hw_version, license, serial; 7003fd3cf5SKurt Van Dijck uint16_t chip[2]; 7103fd3cf5SKurt Van Dijck unsigned int freq; /* remote cpu's operating frequency */ 7203fd3cf5SKurt Van Dijck } id; 7303fd3cf5SKurt Van Dijck }; 7403fd3cf5SKurt Van Dijck 75405eb0e5SJoe Perches int softing_default_output(struct net_device *netdev); 7603fd3cf5SKurt Van Dijck 77405eb0e5SJoe Perches ktime_t softing_raw2ktime(struct softing *card, u32 raw); 7803fd3cf5SKurt Van Dijck 79405eb0e5SJoe Perches int softing_chip_poweron(struct softing *card); 8003fd3cf5SKurt Van Dijck 81405eb0e5SJoe Perches int softing_bootloader_command(struct softing *card, int16_t cmd, 8203fd3cf5SKurt Van Dijck const char *msg); 8303fd3cf5SKurt Van Dijck 8403fd3cf5SKurt Van Dijck /* Load firmware after reset */ 85405eb0e5SJoe Perches int softing_load_fw(const char *file, struct softing *card, 8603fd3cf5SKurt Van Dijck __iomem uint8_t *virt, unsigned int size, int offset); 8703fd3cf5SKurt Van Dijck 8803fd3cf5SKurt Van Dijck /* Load final application firmware after bootloader */ 89405eb0e5SJoe Perches int softing_load_app_fw(const char *file, struct softing *card); 9003fd3cf5SKurt Van Dijck 9103fd3cf5SKurt Van Dijck /* 9203fd3cf5SKurt Van Dijck * enable or disable irq 9303fd3cf5SKurt Van Dijck * only called with fw.lock locked 9403fd3cf5SKurt Van Dijck */ 95405eb0e5SJoe Perches int softing_enable_irq(struct softing *card, int enable); 9603fd3cf5SKurt Van Dijck 9703fd3cf5SKurt Van Dijck /* start/stop 1 bus on card */ 98405eb0e5SJoe Perches int softing_startstop(struct net_device *netdev, int up); 9903fd3cf5SKurt Van Dijck 10003fd3cf5SKurt Van Dijck /* netif_rx() */ 101405eb0e5SJoe Perches int softing_netdev_rx(struct net_device *netdev, const struct can_frame *msg, 102405eb0e5SJoe Perches ktime_t ktime); 10303fd3cf5SKurt Van Dijck 10403fd3cf5SKurt Van Dijck /* SOFTING DPRAM mappings */ 10503fd3cf5SKurt Van Dijck #define DPRAM_RX 0x0000 10603fd3cf5SKurt Van Dijck #define DPRAM_RX_SIZE 32 10703fd3cf5SKurt Van Dijck #define DPRAM_RX_CNT 16 10803fd3cf5SKurt Van Dijck #define DPRAM_RX_RD 0x0201 /* uint8_t */ 10903fd3cf5SKurt Van Dijck #define DPRAM_RX_WR 0x0205 /* uint8_t */ 11003fd3cf5SKurt Van Dijck #define DPRAM_RX_LOST 0x0207 /* uint8_t */ 11103fd3cf5SKurt Van Dijck 11203fd3cf5SKurt Van Dijck #define DPRAM_FCT_PARAM 0x0300 /* int16_t [20] */ 11303fd3cf5SKurt Van Dijck #define DPRAM_FCT_RESULT 0x0328 /* int16_t */ 11403fd3cf5SKurt Van Dijck #define DPRAM_FCT_HOST 0x032b /* uint16_t */ 11503fd3cf5SKurt Van Dijck 11603fd3cf5SKurt Van Dijck #define DPRAM_INFO_BUSSTATE 0x0331 /* uint16_t */ 11703fd3cf5SKurt Van Dijck #define DPRAM_INFO_BUSSTATE2 0x0335 /* uint16_t */ 11803fd3cf5SKurt Van Dijck #define DPRAM_INFO_ERRSTATE 0x0339 /* uint16_t */ 11903fd3cf5SKurt Van Dijck #define DPRAM_INFO_ERRSTATE2 0x033d /* uint16_t */ 12003fd3cf5SKurt Van Dijck #define DPRAM_RESET 0x0341 /* uint16_t */ 12103fd3cf5SKurt Van Dijck #define DPRAM_CLR_RECV_FIFO 0x0345 /* uint16_t */ 12203fd3cf5SKurt Van Dijck #define DPRAM_RESET_TIME 0x034d /* uint16_t */ 12303fd3cf5SKurt Van Dijck #define DPRAM_TIME 0x0350 /* uint64_t */ 12403fd3cf5SKurt Van Dijck #define DPRAM_WR_START 0x0358 /* uint8_t */ 12503fd3cf5SKurt Van Dijck #define DPRAM_WR_END 0x0359 /* uint8_t */ 12603fd3cf5SKurt Van Dijck #define DPRAM_RESET_RX_FIFO 0x0361 /* uint16_t */ 12703fd3cf5SKurt Van Dijck #define DPRAM_RESET_TX_FIFO 0x0364 /* uint8_t */ 12803fd3cf5SKurt Van Dijck #define DPRAM_READ_FIFO_LEVEL 0x0365 /* uint8_t */ 12903fd3cf5SKurt Van Dijck #define DPRAM_RX_FIFO_LEVEL 0x0366 /* uint16_t */ 13003fd3cf5SKurt Van Dijck #define DPRAM_TX_FIFO_LEVEL 0x0366 /* uint16_t */ 13103fd3cf5SKurt Van Dijck 13203fd3cf5SKurt Van Dijck #define DPRAM_TX 0x0400 /* uint16_t */ 13303fd3cf5SKurt Van Dijck #define DPRAM_TX_SIZE 16 13403fd3cf5SKurt Van Dijck #define DPRAM_TX_CNT 32 13503fd3cf5SKurt Van Dijck #define DPRAM_TX_RD 0x0601 /* uint8_t */ 13603fd3cf5SKurt Van Dijck #define DPRAM_TX_WR 0x0605 /* uint8_t */ 13703fd3cf5SKurt Van Dijck 13803fd3cf5SKurt Van Dijck #define DPRAM_COMMAND 0x07e0 /* uint16_t */ 13903fd3cf5SKurt Van Dijck #define DPRAM_RECEIPT 0x07f0 /* uint16_t */ 14003fd3cf5SKurt Van Dijck #define DPRAM_IRQ_TOHOST 0x07fe /* uint8_t */ 14103fd3cf5SKurt Van Dijck #define DPRAM_IRQ_TOCARD 0x07ff /* uint8_t */ 14203fd3cf5SKurt Van Dijck 14303fd3cf5SKurt Van Dijck #define DPRAM_V2_RESET 0x0e00 /* uint8_t */ 14403fd3cf5SKurt Van Dijck #define DPRAM_V2_IRQ_TOHOST 0x0e02 /* uint8_t */ 14503fd3cf5SKurt Van Dijck 14603fd3cf5SKurt Van Dijck #define TXMAX (DPRAM_TX_CNT - 1) 14703fd3cf5SKurt Van Dijck 14803fd3cf5SKurt Van Dijck /* DPRAM return codes */ 14903fd3cf5SKurt Van Dijck #define RES_NONE 0 15003fd3cf5SKurt Van Dijck #define RES_OK 1 15103fd3cf5SKurt Van Dijck #define RES_NOK 2 15203fd3cf5SKurt Van Dijck #define RES_UNKNOWN 3 15303fd3cf5SKurt Van Dijck /* DPRAM flags */ 15403fd3cf5SKurt Van Dijck #define CMD_TX 0x01 15503fd3cf5SKurt Van Dijck #define CMD_ACK 0x02 15603fd3cf5SKurt Van Dijck #define CMD_XTD 0x04 15703fd3cf5SKurt Van Dijck #define CMD_RTR 0x08 15803fd3cf5SKurt Van Dijck #define CMD_ERR 0x10 15903fd3cf5SKurt Van Dijck #define CMD_BUS2 0x80 16003fd3cf5SKurt Van Dijck 16103fd3cf5SKurt Van Dijck /* returned fifo entry bus state masks */ 16203fd3cf5SKurt Van Dijck #define SF_MASK_BUSOFF 0x80 16303fd3cf5SKurt Van Dijck #define SF_MASK_EPASSIVE 0x60 16403fd3cf5SKurt Van Dijck 16503fd3cf5SKurt Van Dijck /* bus states */ 16603fd3cf5SKurt Van Dijck #define STATE_BUSOFF 2 16703fd3cf5SKurt Van Dijck #define STATE_EPASSIVE 1 16803fd3cf5SKurt Van Dijck #define STATE_EACTIVE 0 169