22 #include <linux/module.h>
23 #include <linux/sched.h>
24 #include <linux/pci.h>
26 #include <linux/kernel.h>
27 #include <linux/types.h>
28 #include <linux/errno.h>
29 #include <linux/netdevice.h>
35 #define PCH_CTRL_INIT BIT(0)
36 #define PCH_CTRL_IE BIT(1)
37 #define PCH_CTRL_IE_SIE_EIE (BIT(3) | BIT(2) | BIT(1))
38 #define PCH_CTRL_CCE BIT(6)
39 #define PCH_CTRL_OPT BIT(7)
40 #define PCH_OPT_SILENT BIT(3)
41 #define PCH_OPT_LBACK BIT(4)
43 #define PCH_CMASK_RX_TX_SET 0x00f3
44 #define PCH_CMASK_RX_TX_GET 0x0073
45 #define PCH_CMASK_ALL 0xff
46 #define PCH_CMASK_NEWDAT BIT(2)
47 #define PCH_CMASK_CLRINTPND BIT(3)
48 #define PCH_CMASK_CTRL BIT(4)
49 #define PCH_CMASK_ARB BIT(5)
50 #define PCH_CMASK_MASK BIT(6)
51 #define PCH_CMASK_RDWR BIT(7)
52 #define PCH_IF_MCONT_NEWDAT BIT(15)
53 #define PCH_IF_MCONT_MSGLOST BIT(14)
54 #define PCH_IF_MCONT_INTPND BIT(13)
55 #define PCH_IF_MCONT_UMASK BIT(12)
56 #define PCH_IF_MCONT_TXIE BIT(11)
57 #define PCH_IF_MCONT_RXIE BIT(10)
58 #define PCH_IF_MCONT_RMTEN BIT(9)
59 #define PCH_IF_MCONT_TXRQXT BIT(8)
60 #define PCH_IF_MCONT_EOB BIT(7)
61 #define PCH_IF_MCONT_DLC (BIT(0) | BIT(1) | BIT(2) | BIT(3))
62 #define PCH_MASK2_MDIR_MXTD (BIT(14) | BIT(15))
63 #define PCH_ID2_DIR BIT(13)
64 #define PCH_ID2_XTD BIT(14)
65 #define PCH_ID_MSGVAL BIT(15)
66 #define PCH_IF_CREQ_BUSY BIT(15)
68 #define PCH_STATUS_INT 0x8000
69 #define PCH_RP 0x00008000
70 #define PCH_REC 0x00007f00
71 #define PCH_TEC 0x000000ff
73 #define PCH_TX_OK BIT(3)
74 #define PCH_RX_OK BIT(4)
75 #define PCH_EPASSIV BIT(5)
76 #define PCH_EWARN BIT(6)
77 #define PCH_BUS_OFF BIT(7)
80 #define PCH_BIT_BRP_SHIFT 0
81 #define PCH_BIT_SJW_SHIFT 6
82 #define PCH_BIT_TSEG1_SHIFT 8
83 #define PCH_BIT_TSEG2_SHIFT 12
84 #define PCH_BIT_BRPE_BRPE_SHIFT 6
86 #define PCH_MSK_BITT_BRP 0x3f
87 #define PCH_MSK_BRPE_BRPE 0x3c0
88 #define PCH_MSK_CTRL_IE_SIE_EIE 0x07
89 #define PCH_COUNTER_LIMIT 10
91 #define PCH_CAN_CLK 50000000
98 #define PCH_RX_OBJ_NUM 26
99 #define PCH_TX_OBJ_NUM 6
100 #define PCH_RX_OBJ_START 1
101 #define PCH_RX_OBJ_END PCH_RX_OBJ_NUM
102 #define PCH_TX_OBJ_START (PCH_RX_OBJ_END + 1)
103 #define PCH_TX_OBJ_END (PCH_RX_OBJ_NUM + PCH_TX_OBJ_NUM)
105 #define PCH_FIFO_THRESH 16
108 #define PCH_TREQ2_TX_MASK (((1 << PCH_TX_OBJ_NUM) - 1) <<\
109 (PCH_RX_OBJ_END - 16))
188 .name = KBUILD_MODNAME,
210 static inline void pch_can_bit_clear(
void __iomem *addr,
u32 mask)
228 netdev_err(priv->
ndev,
"%s -> Invalid Mode.\n", __func__);
233 static void pch_can_set_optmode(
struct pch_can_priv *priv)
247 static void pch_can_rw_msg_obj(
void __iomem *creq_addr,
u32 num)
261 pr_err(
"%s:IF1 BUSY Flag is set forever.\n", __func__);
264 static void pch_can_set_int_enables(
struct pch_can_priv *priv,
267 switch (interrupt_no) {
281 netdev_err(priv->
ndev,
"Invalid interrupt number.\n");
298 pch_can_rw_msg_obj(&priv->
regs->ifregs[dir].creq, buff_num);
302 &priv->
regs->ifregs[dir].cmask);
306 pch_can_bit_set(&priv->
regs->ifregs[dir].mcont, ie);
310 pch_can_bit_clear(&priv->
regs->ifregs[dir].mcont, ie);
314 pch_can_rw_msg_obj(&priv->
regs->ifregs[dir].creq, buff_num);
317 static void pch_can_set_rx_all(
struct pch_can_priv *priv,
int set)
326 static void pch_can_set_tx_all(
struct pch_can_priv *priv,
int set)
340 static void pch_can_clear_if_buffers(
struct pch_can_priv *priv)
357 &priv->
regs->ifregs[0].cmask);
358 pch_can_rw_msg_obj(&priv->
regs->ifregs[0].creq, i);
362 static void pch_can_config_rx_tx_buffers(
struct pch_can_priv *priv)
368 pch_can_rw_msg_obj(&priv->
regs->ifregs[0].creq, i);
373 pch_can_bit_set(&priv->
regs->ifregs[0].mcont,
378 pch_can_bit_set(&priv->
regs->ifregs[0].mcont,
381 pch_can_bit_clear(&priv->
regs->ifregs[0].mcont,
385 pch_can_bit_clear(&priv->
regs->ifregs[0].mask2,
392 pch_can_rw_msg_obj(&priv->
regs->ifregs[0].creq, i);
397 pch_can_rw_msg_obj(&priv->
regs->ifregs[1].creq, i);
405 &priv->
regs->ifregs[1].mcont);
408 pch_can_bit_clear(&priv->
regs->ifregs[1].mask2, 0x1fff);
414 pch_can_rw_msg_obj(&priv->
regs->ifregs[1].creq, i);
424 pch_can_clear_if_buffers(priv);
427 pch_can_config_rx_tx_buffers(priv);
442 pch_can_set_rx_all(priv, 0);
445 pch_can_set_tx_all(priv, 0);
455 &priv->
regs->ifregs[0].cmask);
461 pch_can_bit_clear(&priv->
regs->ifregs[0].mcont,
464 pch_can_rw_msg_obj(&priv->
regs->ifregs[0].creq, mask);
470 &priv->
regs->ifregs[1].cmask);
473 pch_can_bit_set(&priv->
regs->ifregs[1].id2,
478 pch_can_bit_clear(&priv->
regs->ifregs[1].mcont,
481 pch_can_rw_msg_obj(&priv->
regs->ifregs[1].creq, mask);
506 pch_can_set_tx_all(priv, 0);
507 pch_can_set_rx_all(priv, 0);
517 priv->
can.can_stats.error_warning++;
519 if (((errc &
PCH_REC) >> 8) > 96)
524 "%s -> Error Counter is more than 96.\n", __func__);
528 priv->
can.can_stats.error_passive++;
536 "%s -> CAN controller is ERROR PASSIVE .\n", __func__);
543 priv->
can.can_stats.bus_error++;
548 priv->
can.can_stats.bus_error++;
553 priv->
can.can_stats.bus_error++;
559 priv->
can.can_stats.bus_error++;
565 priv->
can.can_stats.bus_error++;
573 cf->data[7] = (errc &
PCH_REC) >> 8;
587 if (!pch_can_int_pending(priv))
591 napi_schedule(&priv->
napi);
595 static void pch_fifo_thresh(
struct pch_can_priv *priv,
int obj_id)
605 pch_can_bit_clear(&priv->
regs->ifregs[0].mcont,
607 pch_can_rw_msg_obj(&priv->
regs->ifregs[0].creq, obj_id);
609 pch_can_int_clr(priv, obj_id);
613 pch_can_int_clr(priv, cnt + 1);
617 static void pch_can_rx_msg_lost(
struct net_device *ndev,
int obj_id)
625 pch_can_bit_clear(&priv->
regs->ifregs[0].mcont,
628 &priv->
regs->ifregs[0].cmask);
629 pch_can_rw_msg_obj(&priv->
regs->ifregs[0].creq, obj_id);
643 static int pch_can_rx_normal(
struct net_device *ndev,
u32 obj_num,
int quota)
659 pch_can_rw_msg_obj(&priv->
regs->ifregs[0].creq, obj_num);
669 pch_can_rx_msg_lost(ndev, obj_num);
681 netdev_err(ndev,
"alloc_can_skb Failed\n");
689 id |= (((
id2) & 0x1fff) << 16);
700 ifregs[0].mcont)) & 0xF);
702 for (i = 0; i < cf->
can_dlc; i += 2) {
703 data_reg =
ioread16(&priv->
regs->ifregs[0].data[i / 2]);
704 cf->data[
i] = data_reg;
705 cf->data[i + 1] = data_reg >> 8;
714 pch_fifo_thresh(priv, obj_num);
721 static void pch_can_tx_complete(
struct net_device *ndev,
u32 int_stat)
729 &priv->
regs->ifregs[1].cmask);
730 pch_can_rw_msg_obj(&priv->
regs->ifregs[1].creq, int_stat);
736 netif_wake_queue(ndev);
745 int quota_save = quota;
747 int_stat = pch_can_int_pending(priv);
754 if ((reg_stat & (PCH_BUS_OFF | PCH_LEC_ALL)) &&
755 ((reg_stat & PCH_LEC_ALL) != PCH_LEC_ALL)) {
756 pch_can_error(ndev, reg_stat);
761 pch_can_bit_clear(&priv->
regs->stat,
764 int_stat = pch_can_int_pending(priv);
771 quota -= pch_can_rx_normal(ndev, int_stat, quota);
775 pch_can_tx_complete(ndev, int_stat);
782 return quota_save - quota;
785 static int pch_set_bittiming(
struct net_device *ndev)
807 static void pch_can_start(
struct net_device *ndev)
814 pch_set_bittiming(ndev);
815 pch_can_set_optmode(priv);
817 pch_can_set_tx_all(priv, 1);
818 pch_can_set_rx_all(priv, 1);
835 netif_wake_queue(ndev);
845 static int pch_can_open(
struct net_device *ndev)
854 netdev_err(ndev,
"request_irq failed.\n");
861 netdev_err(ndev,
"open_candev() failed %d\n", retval);
862 goto err_open_candev;
867 napi_enable(&priv->
napi);
868 netif_start_queue(ndev);
875 pch_can_release(priv);
884 netif_stop_queue(ndev);
885 napi_disable(&priv->
napi);
886 pch_can_release(priv);
901 if (can_dropped_invalid_skb(ndev, skb))
907 netif_stop_queue(ndev);
935 for (i = 0; i < cf->
can_dlc; i += 2) {
936 iowrite16(cf->data[i] | (cf->data[i + 1] << 8),
937 &priv->
regs->ifregs[1].data[i / 2]);
946 pch_can_rw_msg_obj(&priv->
regs->ifregs[1].creq, tx_obj_no);
952 .ndo_open = pch_can_open,
953 .ndo_stop = pch_close,
954 .ndo_start_xmit = pch_xmit,
959 struct net_device *ndev = pci_get_drvdata(pdev);
967 pci_set_drvdata(pdev,
NULL);
974 static void pch_can_set_int_custom(
struct pch_can_priv *priv)
980 pch_can_bit_set(&priv->
regs->cont,
1002 pch_can_rw_msg_obj(&priv->
regs->ifregs[dir].creq, buff_num);
1013 static void pch_can_set_rx_buffer_link(
struct pch_can_priv *priv,
1014 u32 buffer_num,
int set)
1017 pch_can_rw_msg_obj(&priv->
regs->ifregs[0].creq, buffer_num);
1019 &priv->
regs->ifregs[0].cmask);
1021 pch_can_bit_clear(&priv->
regs->ifregs[0].mcont,
1024 pch_can_bit_set(&priv->
regs->ifregs[0].mcont, PCH_IF_MCONT_EOB);
1026 pch_can_rw_msg_obj(&priv->
regs->ifregs[0].creq, buffer_num);
1034 pch_can_rw_msg_obj(&priv->
regs->ifregs[0].creq, buffer_num);
1036 if (
ioread32(&priv->
regs->ifregs[0].mcont) & PCH_IF_MCONT_EOB)
1043 static int pch_can_get_buffer_status(
struct pch_can_priv *priv)
1067 buf_stat = pch_can_get_buffer_status(priv);
1074 dev_err(&pdev->
dev,
"%s -> Transmission time out.\n", __func__);
1077 priv->
int_enables = pch_can_get_int_enables(priv);
1082 priv->
tx_enable[i - 1] = pch_can_get_rxtx_ir(priv, i,
1086 pch_can_set_tx_all(priv, 0);
1090 priv->
rx_enable[i - 1] = pch_can_get_rxtx_ir(priv, i,
1092 priv->
rx_link[i - 1] = pch_can_get_rx_buffer_link(priv, i);
1096 pch_can_set_rx_all(priv, 0);
1099 dev_err(&pdev->
dev,
"pci_save_state failed.\n");
1113 struct net_device *dev = pci_get_drvdata(pdev);
1120 dev_err(&pdev->
dev,
"pci_enable_device failed.\n");
1135 pch_can_config_rx_tx_buffers(priv);
1138 pch_set_bittiming(dev);
1141 pch_can_set_optmode(priv);
1150 pch_can_set_rx_buffer_link(priv, i, priv->
rx_link[i - 1]);
1157 pch_can_set_int_custom(priv);
1165 #define pch_can_suspend NULL
1166 #define pch_can_resume NULL
1169 static int pch_can_get_berr_counter(
const struct net_device *dev,
1191 dev_err(&pdev->
dev,
"Failed pci_enable_device %d\n", rc);
1192 goto probe_exit_endev;
1197 dev_err(&pdev->
dev,
"Failed pci_request_regions %d\n", rc);
1198 goto probe_exit_pcireq;
1201 addr = pci_iomap(pdev, 1, 0);
1205 goto probe_exit_ipmap;
1211 dev_err(&pdev->
dev,
"Failed alloc_candev\n");
1212 goto probe_exit_alloc_candev;
1215 priv = netdev_priv(ndev);
1219 priv->
can.bittiming_const = &pch_can_bittiming_const;
1220 priv->
can.do_set_mode = pch_can_do_set_mode;
1221 priv->
can.do_get_berr_counter = pch_can_get_berr_counter;
1229 pci_set_drvdata(pdev, ndev);
1236 rc = pci_enable_msi(priv->
dev);
1238 netdev_err(ndev,
"PCH CAN opened without MSI\n");
1241 netdev_err(ndev,
"PCH CAN opened with MSI\n");
1248 dev_err(&pdev->
dev,
"Failed register_candev %d\n", rc);
1249 goto probe_exit_reg_candev;
1254 probe_exit_reg_candev:
1258 probe_exit_alloc_candev:
1268 static struct pci_driver pch_can_pci_driver = {
1270 .id_table = pch_pci_tbl,
1271 .probe = pch_can_probe,