28 #include <linux/kernel.h>
29 #include <linux/module.h>
32 #include <linux/netdevice.h>
33 #include <linux/if_arp.h>
34 #include <linux/if_ether.h>
35 #include <linux/list.h>
46 #define IF_ENUM_REG_LEN 11
47 #define C_CAN_IFACE(reg, iface) (C_CAN_IF1_##reg + (iface) * IF_ENUM_REG_LEN)
50 #define CONTROL_EX_PDR BIT(8)
53 #define CONTROL_TEST BIT(7)
54 #define CONTROL_CCE BIT(6)
55 #define CONTROL_DISABLE_AR BIT(5)
56 #define CONTROL_ENABLE_AR (0 << 5)
57 #define CONTROL_EIE BIT(3)
58 #define CONTROL_SIE BIT(2)
59 #define CONTROL_IE BIT(1)
60 #define CONTROL_INIT BIT(0)
63 #define TEST_RX BIT(7)
64 #define TEST_TX1 BIT(6)
65 #define TEST_TX2 BIT(5)
66 #define TEST_LBACK BIT(4)
67 #define TEST_SILENT BIT(3)
68 #define TEST_BASIC BIT(2)
71 #define STATUS_PDA BIT(10)
72 #define STATUS_BOFF BIT(7)
73 #define STATUS_EWARN BIT(6)
74 #define STATUS_EPASS BIT(5)
75 #define STATUS_RXOK BIT(4)
76 #define STATUS_TXOK BIT(3)
79 #define ERR_CNT_TEC_MASK 0xff
80 #define ERR_CNT_TEC_SHIFT 0
81 #define ERR_CNT_REC_SHIFT 8
82 #define ERR_CNT_REC_MASK (0x7f << ERR_CNT_REC_SHIFT)
83 #define ERR_CNT_RP_SHIFT 15
84 #define ERR_CNT_RP_MASK (0x1 << ERR_CNT_RP_SHIFT)
87 #define BTR_BRP_MASK 0x3f
88 #define BTR_BRP_SHIFT 0
89 #define BTR_SJW_SHIFT 6
90 #define BTR_SJW_MASK (0x3 << BTR_SJW_SHIFT)
91 #define BTR_TSEG1_SHIFT 8
92 #define BTR_TSEG1_MASK (0xf << BTR_TSEG1_SHIFT)
93 #define BTR_TSEG2_SHIFT 12
94 #define BTR_TSEG2_MASK (0x7 << BTR_TSEG2_SHIFT)
97 #define BRP_EXT_BRPE_MASK 0x0f
98 #define BRP_EXT_BRPE_SHIFT 0
101 #define IF_COMR_BUSY BIT(15)
104 #define IF_COMM_WR BIT(7)
105 #define IF_COMM_MASK BIT(6)
106 #define IF_COMM_ARB BIT(5)
107 #define IF_COMM_CONTROL BIT(4)
108 #define IF_COMM_CLR_INT_PND BIT(3)
109 #define IF_COMM_TXRQST BIT(2)
110 #define IF_COMM_DATAA BIT(1)
111 #define IF_COMM_DATAB BIT(0)
112 #define IF_COMM_ALL (IF_COMM_MASK | IF_COMM_ARB | \
113 IF_COMM_CONTROL | IF_COMM_TXRQST | \
114 IF_COMM_DATAA | IF_COMM_DATAB)
117 #define IF_ARB_MSGVAL BIT(15)
118 #define IF_ARB_MSGXTD BIT(14)
119 #define IF_ARB_TRANSMIT BIT(13)
122 #define IF_MCONT_NEWDAT BIT(15)
123 #define IF_MCONT_MSGLST BIT(14)
124 #define IF_MCONT_CLR_MSGLST (0 << 14)
125 #define IF_MCONT_INTPND BIT(13)
126 #define IF_MCONT_UMASK BIT(12)
127 #define IF_MCONT_TXIE BIT(11)
128 #define IF_MCONT_RXIE BIT(10)
129 #define IF_MCONT_RMTEN BIT(9)
130 #define IF_MCONT_TXRQST BIT(8)
131 #define IF_MCONT_EOB BIT(7)
132 #define IF_MCONT_DLC_MASK 0xf
139 #define IFX_WRITE_LOW_16BIT(x) ((x) & 0xFFFF)
140 #define IFX_WRITE_HIGH_16BIT(x) (((x) & 0xFFFF0000) >> 16)
143 #define C_CAN_NO_OF_OBJECTS 32
144 #define C_CAN_MSG_OBJ_RX_NUM 16
145 #define C_CAN_MSG_OBJ_TX_NUM 16
147 #define C_CAN_MSG_OBJ_RX_FIRST 1
148 #define C_CAN_MSG_OBJ_RX_LAST (C_CAN_MSG_OBJ_RX_FIRST + \
149 C_CAN_MSG_OBJ_RX_NUM - 1)
151 #define C_CAN_MSG_OBJ_TX_FIRST (C_CAN_MSG_OBJ_RX_LAST + 1)
152 #define C_CAN_MSG_OBJ_TX_LAST (C_CAN_MSG_OBJ_TX_FIRST + \
153 C_CAN_MSG_OBJ_TX_NUM - 1)
155 #define C_CAN_MSG_OBJ_RX_SPLIT 9
156 #define C_CAN_MSG_RX_LOW_LAST (C_CAN_MSG_OBJ_RX_SPLIT - 1)
158 #define C_CAN_NEXT_MSG_OBJ_MASK (C_CAN_MSG_OBJ_TX_NUM - 1)
159 #define RECEIVE_OBJECT_BITS 0x0000ffff
162 #define STATUS_INTERRUPT 0x8000
165 #define ENABLE_ALL_INTERRUPTS 1
166 #define DISABLE_ALL_INTERRUPTS 0
169 #define MIN_TIMEOUT_VALUE 6
172 #define INIT_WAIT_MS 1000
175 #define C_CAN_NAPI_WEIGHT C_CAN_MSG_OBJ_RX_NUM
201 .name = KBUILD_MODNAME,
212 static inline void c_can_pm_runtime_enable(
const struct c_can_priv *
priv)
218 static inline void c_can_pm_runtime_disable(
const struct c_can_priv *priv)
221 pm_runtime_disable(priv->
device);
224 static inline void c_can_pm_runtime_get_sync(
const struct c_can_priv *priv)
227 pm_runtime_get_sync(priv->
device);
230 static inline void c_can_pm_runtime_put_sync(
const struct c_can_priv *priv)
233 pm_runtime_put_sync(priv->
device);
236 static inline int get_tx_next_msg_obj(
const struct c_can_priv *priv)
242 static inline int get_tx_echo_msg_obj(
const struct c_can_priv *priv)
251 val |= ((
u32) priv->
read_reg(priv, index + 1)) << 16;
255 static void c_can_enable_all_interrupts(
struct c_can_priv *priv,
258 unsigned int cntrl_save = priv->
read_reg(priv,
269 static inline int c_can_msg_obj_is_busy(
struct c_can_priv *priv,
int iface)
273 while (count && priv->
read_reg(priv,
287 int iface,
int objno,
int mask)
302 if (c_can_msg_obj_is_busy(priv, iface))
303 netdev_err(dev,
"timed out in object get\n");
306 static inline void c_can_object_put(
struct net_device *dev,
307 int iface,
int objno,
int mask)
322 if (c_can_msg_obj_is_busy(priv, iface))
323 netdev_err(dev,
"timed out in object put\n");
326 static void c_can_write_msg_object(
struct net_device *dev,
350 for (i = 0; i < frame->
can_dlc; i += 2) {
352 frame->data[i] | (frame->data[i + 1] << 8));
362 static inline void c_can_mark_rx_msg_obj(
struct net_device *dev,
363 int iface,
int ctrl_mask,
374 static inline void c_can_activate_all_lower_rx_msg_obj(
struct net_device *dev,
389 static inline void c_can_activate_rx_msg_obj(
struct net_device *dev,
390 int iface,
int ctrl_mask,
401 static void c_can_handle_lost_msg_obj(
struct net_device *dev,
402 int iface,
int objno)
409 netdev_err(dev,
"msg lost in buffer %d\n", objno);
431 static int c_can_read_msg_object(
struct net_device *dev,
int iface,
int ctrl)
461 for (i = 0; i < frame->
can_dlc; i += 2) {
464 frame->data[
i] =
data;
465 frame->data[i + 1] = data >> 8;
477 static void c_can_setup_receive_object(
struct net_device *dev,
int iface,
478 int objno,
unsigned int mask,
479 unsigned int id,
unsigned int mcont)
496 netdev_dbg(dev,
"obj no:%d, msgval:0x%08x\n", objno,
500 static void c_can_inval_msg_object(
struct net_device *dev,
int iface,
int objno)
510 netdev_dbg(dev,
"obj no:%d, msgval:0x%08x\n", objno,
514 static inline int c_can_is_next_tx_obj_busy(
struct c_can_priv *priv,
int objno)
522 if (val & (1 << (objno - 1)))
535 if (can_dropped_invalid_skb(dev, skb))
538 msg_obj_no = get_tx_next_msg_obj(priv);
541 c_can_write_msg_object(dev, 0, frame, msg_obj_no);
549 if (c_can_is_next_tx_obj_busy(priv, get_tx_next_msg_obj(priv)) ||
551 netif_stop_queue(dev);
556 static int c_can_set_bittiming(
struct net_device *dev)
558 unsigned int reg_btr, reg_brpe, ctrl_save;
559 u8 brp, brpe, sjw, tseg1, tseg2;
565 ten_bit_brp = bt->
brp - 1;
567 brpe = ten_bit_brp >> 6;
577 "setting BTR=%04x BRPE=%04x\n", reg_btr, reg_brpe);
598 static void c_can_configure_msg_objects(
struct net_device *dev)
604 c_can_inval_msg_object(dev, 0, i);
608 c_can_setup_receive_object(dev, 0, i, 0, 0,
611 c_can_setup_receive_object(dev, 0, C_CAN_MSG_OBJ_RX_LAST, 0, 0,
621 static void c_can_chip_config(
struct net_device *dev)
652 c_can_configure_msg_objects(dev);
658 c_can_set_bittiming(dev);
661 static void c_can_start(
struct net_device *dev)
666 c_can_chip_config(dev);
677 static void c_can_stop(
struct net_device *dev)
693 netif_wake_queue(dev);
702 static int c_can_get_berr_counter(
const struct net_device *dev,
705 unsigned int reg_err_counter;
708 c_can_pm_runtime_get_sync(priv);
715 c_can_pm_runtime_put_sync(priv);
731 static void c_can_do_tx(
struct net_device *dev)
739 msg_obj_no = get_tx_echo_msg_obj(priv);
741 if (!(val & (1 << (msg_obj_no - 1)))) {
748 c_can_inval_msg_object(dev, 0, msg_obj_no);
757 netif_wake_queue(dev);
782 static int c_can_do_rx_poll(
struct net_device *dev,
int quota)
785 unsigned int msg_obj, msg_ctrl_save;
790 msg_obj <= C_CAN_MSG_OBJ_RX_LAST && quota > 0;
797 if (val & (1 << (msg_obj - 1))) {
800 msg_ctrl_save = priv->
read_reg(priv,
807 c_can_handle_lost_msg_obj(dev, 0, msg_obj);
817 c_can_read_msg_object(dev, 0, msg_ctrl_save);
820 c_can_mark_rx_msg_obj(dev, 0,
821 msg_ctrl_save, msg_obj);
824 c_can_activate_rx_msg_obj(dev, 0,
825 msg_ctrl_save, msg_obj);
828 c_can_activate_all_lower_rx_msg_obj(dev,
839 static inline int c_can_has_and_handle_berr(
struct c_can_priv *priv)
845 static int c_can_handle_state_change(
struct net_device *dev,
848 unsigned int reg_err_counter;
849 unsigned int rx_err_passive;
861 c_can_get_berr_counter(dev, &bec);
866 switch (error_type) {
869 priv->
can.can_stats.error_warning++;
875 cf->data[6] = bec.
txerr;
876 cf->data[7] = bec.
rxerr;
881 priv->
can.can_stats.error_passive++;
889 cf->data[6] = bec.
txerr;
890 cf->data[7] = bec.
rxerr;
914 static int c_can_handle_bus_err(
struct net_device *dev,
941 priv->
can.can_stats.bus_error++;
1016 netdev_dbg(dev,
"entered error warning state\n");
1017 work_done += c_can_handle_state_change(dev,
1022 netdev_dbg(dev,
"entered error passive state\n");
1023 work_done += c_can_handle_state_change(dev,
1029 work_done += c_can_handle_state_change(dev,
1041 netdev_dbg(dev,
"left error passive state\n");
1048 lec_type = c_can_has_and_handle_berr(priv);
1050 work_done += c_can_handle_bus_err(dev, lec_type);
1052 (irqstatus <= C_CAN_MSG_OBJ_RX_LAST)) {
1054 work_done += c_can_do_rx_poll(dev, (quota - work_done));
1062 if (work_done < quota) {
1082 napi_schedule(&priv->
napi);
1087 static int c_can_open(
struct net_device *dev)
1092 c_can_pm_runtime_get_sync(priv);
1097 netdev_err(dev,
"failed to open can device\n");
1098 goto exit_open_fail;
1105 netdev_err(dev,
"failed to request interrupt\n");
1109 napi_enable(&priv->
napi);
1114 netif_start_queue(dev);
1121 c_can_pm_runtime_put_sync(priv);
1125 static int c_can_close(
struct net_device *dev)
1129 netif_stop_queue(dev);
1130 napi_disable(&priv->
napi);
1134 c_can_pm_runtime_put_sync(priv);
1148 priv = netdev_priv(dev);
1152 priv->
can.bittiming_const = &c_can_bittiming_const;
1153 priv->
can.do_set_mode = c_can_set_mode;
1154 priv->
can.do_get_berr_counter = c_can_get_berr_counter;
1167 unsigned long time_out;
1191 c_can_pm_runtime_put_sync(priv);
1200 unsigned long time_out;
1208 c_can_pm_runtime_get_sync(priv);
1241 .ndo_open = c_can_open,
1242 .ndo_stop = c_can_close,
1243 .ndo_start_xmit = c_can_start_xmit,
1251 c_can_pm_runtime_enable(priv);
1258 c_can_pm_runtime_disable(priv);
1270 c_can_pm_runtime_disable(priv);