39 #include <linux/module.h>
41 #include <linux/kernel.h>
42 #include <linux/types.h>
44 #include <linux/errno.h>
45 #include <linux/netdevice.h>
55 #define DRV_NAME "ti_hecc"
56 #define HECC_MODULE_VERSION "0.7"
58 #define DRV_DESC "TI High End CAN Controller Driver " HECC_MODULE_VERSION
61 #define HECC_MAX_MAILBOXES 32
62 #define MAX_TX_PRIO 0x3F
76 #define HECC_MB_TX_SHIFT 2
77 #define HECC_MAX_TX_MBOX BIT(HECC_MB_TX_SHIFT)
79 #define HECC_TX_PRIO_SHIFT (HECC_MB_TX_SHIFT)
80 #define HECC_TX_PRIO_MASK (MAX_TX_PRIO << HECC_MB_TX_SHIFT)
81 #define HECC_TX_MB_MASK (HECC_MAX_TX_MBOX - 1)
82 #define HECC_TX_MASK ((HECC_MAX_TX_MBOX - 1) | HECC_TX_PRIO_MASK)
83 #define HECC_TX_MBOX_MASK (~(BIT(HECC_MAX_TX_MBOX) - 1))
84 #define HECC_DEF_NAPI_WEIGHT HECC_MAX_RX_MBOX
102 #define HECC_MAX_RX_MBOX (HECC_MAX_MAILBOXES - HECC_MAX_TX_MBOX)
103 #define HECC_RX_BUFFER_MBOX 12
104 #define HECC_RX_FIRST_MBOX (HECC_MAX_MAILBOXES - 1)
105 #define HECC_RX_HIGH_MBOX_MASK (~(BIT(HECC_RX_BUFFER_MBOX) - 1))
108 #define HECC_CANME 0x0
109 #define HECC_CANMD 0x4
110 #define HECC_CANTRS 0x8
111 #define HECC_CANTRR 0xC
112 #define HECC_CANTA 0x10
113 #define HECC_CANAA 0x14
114 #define HECC_CANRMP 0x18
115 #define HECC_CANRML 0x1C
116 #define HECC_CANRFP 0x20
117 #define HECC_CANGAM 0x24
118 #define HECC_CANMC 0x28
119 #define HECC_CANBTC 0x2C
120 #define HECC_CANES 0x30
121 #define HECC_CANTEC 0x34
122 #define HECC_CANREC 0x38
123 #define HECC_CANGIF0 0x3C
124 #define HECC_CANGIM 0x40
125 #define HECC_CANGIF1 0x44
126 #define HECC_CANMIM 0x48
127 #define HECC_CANMIL 0x4C
128 #define HECC_CANOPC 0x50
129 #define HECC_CANTIOC 0x54
130 #define HECC_CANRIOC 0x58
131 #define HECC_CANLNT 0x5C
132 #define HECC_CANTOC 0x60
133 #define HECC_CANTOS 0x64
134 #define HECC_CANTIOCE 0x68
135 #define HECC_CANRIOCE 0x6C
138 #define HECC_CANMID 0x0
139 #define HECC_CANMCF 0x4
140 #define HECC_CANMDL 0x8
141 #define HECC_CANMDH 0xC
143 #define HECC_SET_REG 0xFFFFFFFF
144 #define HECC_CANID_MASK 0x3FF
145 #define HECC_CCE_WAIT_COUNT 100
147 #define HECC_CANMC_SCM BIT(13)
148 #define HECC_CANMC_CCR BIT(12)
149 #define HECC_CANMC_PDR BIT(11)
150 #define HECC_CANMC_ABO BIT(7)
151 #define HECC_CANMC_STM BIT(6)
152 #define HECC_CANMC_SRES BIT(5)
154 #define HECC_CANTIOC_EN BIT(3)
155 #define HECC_CANRIOC_EN BIT(3)
157 #define HECC_CANMID_IDE BIT(31)
158 #define HECC_CANMID_AME BIT(30)
159 #define HECC_CANMID_AAM BIT(29)
161 #define HECC_CANES_FE BIT(24)
162 #define HECC_CANES_BE BIT(23)
163 #define HECC_CANES_SA1 BIT(22)
164 #define HECC_CANES_CRCE BIT(21)
165 #define HECC_CANES_SE BIT(20)
166 #define HECC_CANES_ACKE BIT(19)
167 #define HECC_CANES_BO BIT(18)
168 #define HECC_CANES_EP BIT(17)
169 #define HECC_CANES_EW BIT(16)
170 #define HECC_CANES_SMA BIT(5)
171 #define HECC_CANES_CCE BIT(4)
172 #define HECC_CANES_PDA BIT(3)
174 #define HECC_CANBTC_SAM BIT(7)
176 #define HECC_BUS_ERROR (HECC_CANES_FE | HECC_CANES_BE |\
177 HECC_CANES_CRCE | HECC_CANES_SE |\
180 #define HECC_CANMCF_RTR BIT(4)
182 #define HECC_CANGIF_MAIF BIT(17)
183 #define HECC_CANGIF_TCOIF BIT(16)
184 #define HECC_CANGIF_GMIF BIT(15)
185 #define HECC_CANGIF_AAIF BIT(14)
186 #define HECC_CANGIF_WDIF BIT(13)
187 #define HECC_CANGIF_WUIF BIT(12)
188 #define HECC_CANGIF_RMLIF BIT(11)
189 #define HECC_CANGIF_BOIF BIT(10)
190 #define HECC_CANGIF_EPIF BIT(9)
191 #define HECC_CANGIF_WLIF BIT(8)
192 #define HECC_CANGIF_MBOX_MASK 0x1F
193 #define HECC_CANGIM_I1EN BIT(1)
194 #define HECC_CANGIM_I0EN BIT(0)
195 #define HECC_CANGIM_DEF_MASK 0x700
196 #define HECC_CANGIM_SIL BIT(2)
248 static inline void hecc_write_mbx(
struct ti_hecc_priv *priv,
u32 mbxno,
274 hecc_write(priv, reg, hecc_read(priv, reg) | bit_mask);
277 static inline void hecc_clear_bit(
struct ti_hecc_priv *priv,
int reg,
280 hecc_write(priv, reg, hecc_read(priv, reg) & ~bit_mask);
285 return (hecc_read(priv, reg) & bit_mask) ? 1 : 0;
293 *state = priv->
can.state;
306 if (bit_timing->
brp > 4)
309 netdev_warn(priv->
ndev,
"WARN: Triple"
310 "sampling not set due to h/w limitations");
312 can_btc |= ((bit_timing->
sjw - 1) & 0x3) << 8;
313 can_btc |= ((bit_timing->
brp - 1) & 0xFF) << 16;
318 netdev_info(priv->
ndev,
"setting CANBTC=%#x\n", can_btc);
323 static void ti_hecc_transceiver_switch(
const struct ti_hecc_priv *priv,
357 ti_hecc_set_btc(priv);
393 static void ti_hecc_start(
struct net_device *ndev)
410 mbx_mask =
BIT(mbxno);
433 static void ti_hecc_stop(
struct net_device *ndev)
451 netif_wake_queue(ndev);
461 static int ti_hecc_get_berr_counter(
const struct net_device *ndev,
501 if (can_dropped_invalid_skb(ndev, skb))
504 mbxno = get_tx_head_mb(priv);
505 mbx_mask =
BIT(mbxno);
509 netif_stop_queue(ndev);
510 netdev_err(priv->
ndev,
511 "BUG: TX mbx not ready tx_head=%08X, tx_tail=%08X\n",
518 data = cf->
can_dlc | (get_tx_head_prio(priv) << 8);
534 *(
u32 *)(cf->data + 4) = 0;
539 if ((hecc_read(priv,
HECC_CANME) &
BIT(get_tx_head_mb(priv))) ||
541 netif_stop_queue(ndev);
553 static int ti_hecc_rx_pkt(
struct ti_hecc_priv *priv,
int mbxno)
563 if (printk_ratelimit())
564 netdev_err(priv->
ndev,
565 "ti_hecc_rx_pkt: alloc_can_skb() failed\n");
569 mbx_mask =
BIT(mbxno);
585 *(
u32 *)(cf->data + 4) = 0;
593 spin_unlock_irqrestore(&priv->
mbx_lock, flags);
629 unsigned long pending_pkts,
flags;
631 if (!netif_running(ndev))
634 while ((pending_pkts = hecc_read(priv,
HECC_CANRMP)) &&
637 if (mbx_mask & pending_pkts) {
638 if (ti_hecc_rx_pkt(priv, priv->
rx_next) < 0)
651 spin_unlock_irqrestore(&priv->
mbx_lock, flags);
681 if (printk_ratelimit())
682 netdev_err(priv->
ndev,
683 "ti_hecc_error: alloc_can_err_skb() failed\n");
690 ++priv->
can.can_stats.error_warning;
703 if ((int_status & HECC_CANGIF_BOIF) == 0) {
705 ++priv->
can.can_stats.error_passive;
721 if ((int_status & HECC_CANGIF_BOIF) || (err_status &
HECC_CANES_BO)) {
724 hecc_set_bit(priv,
HECC_CANES, HECC_CANES_BO);
732 ++priv->
can.can_stats.bus_error;
736 hecc_set_bit(priv,
HECC_CANES, HECC_CANES_FE);
740 hecc_set_bit(priv,
HECC_CANES, HECC_CANES_BE);
744 hecc_set_bit(priv,
HECC_CANES, HECC_CANES_SE);
748 hecc_set_bit(priv,
HECC_CANES, HECC_CANES_CRCE);
753 hecc_set_bit(priv,
HECC_CANES, HECC_CANES_ACKE);
774 int_status = hecc_read(priv,
781 if (err_status & (HECC_BUS_ERROR | HECC_CANES_BO |
783 ti_hecc_error(ndev, int_status, err_status);
787 mbxno = get_tx_tail_mb(priv);
788 mbx_mask =
BIT(mbxno);
789 if (!(mbx_mask & hecc_read(priv,
HECC_CANTA)))
795 spin_unlock_irqrestore(&priv->
mbx_lock, flags);
796 stats->
tx_bytes += hecc_read_mbx(priv, mbxno,
808 netif_wake_queue(ndev);
815 napi_schedule(&priv->
napi);
831 static int ti_hecc_open(
struct net_device *ndev)
839 netdev_err(ndev,
"error requesting interrupt\n");
843 ti_hecc_transceiver_switch(priv, 1);
848 netdev_err(ndev,
"open_candev() failed %d\n", err);
849 ti_hecc_transceiver_switch(priv, 0);
855 napi_enable(&priv->
napi);
856 netif_start_queue(ndev);
861 static int ti_hecc_close(
struct net_device *ndev)
865 netif_stop_queue(ndev);
866 napi_disable(&priv->
napi);
870 ti_hecc_transceiver_switch(priv, 0);
876 .ndo_open = ti_hecc_open,
877 .ndo_stop = ti_hecc_close,
878 .ndo_start_xmit = ti_hecc_xmit,
890 pdata = pdev->
dev.platform_data;
907 dev_err(&pdev->
dev,
"HECC region already claimed\n");
915 goto probe_exit_free_region;
922 goto probe_exit_iounmap;
925 priv = netdev_priv(ndev);
934 priv->
can.bittiming_const = &ti_hecc_bittiming_const;
935 priv->
can.do_set_mode = ti_hecc_do_set_mode;
936 priv->
can.do_get_state = ti_hecc_get_state;
937 priv->
can.do_get_berr_counter = ti_hecc_get_berr_counter;
943 platform_set_drvdata(pdev, ndev);
948 if (IS_ERR(priv->
clk)) {
950 err = PTR_ERR(priv->
clk);
952 goto probe_exit_candev;
961 dev_err(&pdev->
dev,
"register_candev() failed\n");
964 dev_info(&pdev->
dev,
"device registered (reg_base=%p, irq=%u)\n",
975 probe_exit_free_region:
984 struct net_device *ndev = platform_get_drvdata(pdev);
994 platform_set_drvdata(pdev,
NULL);
1006 if (netif_running(dev)) {
1007 netif_stop_queue(dev);
1021 struct net_device *dev = platform_get_drvdata(pdev);
1029 if (netif_running(dev)) {
1031 netif_start_queue(dev);
1037 #define ti_hecc_suspend NULL
1038 #define ti_hecc_resume NULL
1047 .probe = ti_hecc_probe,