12 #include <linux/kernel.h>
13 #include <linux/module.h>
19 #include <linux/netdevice.h>
28 #define DPM_NUM_PAGES 256
29 #define DPM_PAGE_SIZE 256
30 #define DPM_PAGE_ADDR(p) ((p) * DPM_PAGE_SIZE)
33 #define QUEUE_OLD_CONTROL 0
34 #define QUEUE_OLD_RB0 1
35 #define QUEUE_OLD_RB1 2
36 #define QUEUE_OLD_WB0 3
37 #define QUEUE_OLD_WB1 4
40 #define MSYNC_PEER 0x00
41 #define MSYNC_LOCL 0x01
42 #define TARGET_RUNNING 0x02
44 #define MSYNC_RB0 0x01
45 #define MSYNC_RB1 0x02
46 #define MSYNC_RBLW 0x04
47 #define MSYNC_RB_MASK (MSYNC_RB0 | MSYNC_RB1)
49 #define MSYNC_WB0 0x10
50 #define MSYNC_WB1 0x20
51 #define MSYNC_WBLW 0x40
52 #define MSYNC_WB_MASK (MSYNC_WB0 | MSYNC_WB1)
55 #define QUEUE_TOHOST 5
56 #define QUEUE_FROMHOST_MID 6
57 #define QUEUE_FROMHOST_HIGH 7
58 #define QUEUE_FROMHOST_LOW 8
61 #define DPM_FREE_START 9
64 #define DESC_VALID 0x80
65 #define DESC_WRAP 0x40
66 #define DESC_INTERRUPT 0x20
67 #define DESC_IVALID 0x10
68 #define DESC_LEN(len) (len)
71 #define MSG_CONNECTI 0x02
72 #define MSG_DISCONNECT 0x03
73 #define MSG_IDVERS 0x04
74 #define MSG_MSGLOST 0x05
75 #define MSG_NEWHOSTIF 0x08
76 #define MSG_INQUIRY 0x0a
77 #define MSG_SETAFILMASK 0x10
78 #define MSG_INITFDPMQUEUE 0x11
79 #define MSG_HWCONF 0x12
80 #define MSG_FMSGLOST 0x15
81 #define MSG_CEVTIND 0x37
82 #define MSG_CBTRREQ 0x41
83 #define MSG_COFFREQ 0x42
84 #define MSG_CONREQ 0x43
85 #define MSG_CCONFREQ 0x47
95 #define INQUIRY_STATUS 0x00
96 #define INQUIRY_TERMINATION 0x01
97 #define INQUIRY_EXTENDED 0x04
100 #define SETAFILMASK_REJECT 0x00
101 #define SETAFILMASK_FASTIF 0x02
104 #define HWCONF_TERMINATE_ON 0x01
105 #define HWCONF_TERMINATE_OFF 0x00
108 #define CEVTIND_EI 0x01
109 #define CEVTIND_DOI 0x02
110 #define CEVTIND_LOST 0x04
111 #define CEVTIND_FULL 0x08
112 #define CEVTIND_BEI 0x10
114 #define CEVTIND_CHIP_SJA1000 0x02
116 #define ICAN3_BUSERR_QUOTA_MAX 255
119 #define ICAN3_SNGL 0x02
120 #define ICAN3_ECHO 0x10
121 #define ICAN3_EFF_RTR 0x40
122 #define ICAN3_SFF_RTR 0x10
123 #define ICAN3_EFF 0x80
125 #define ICAN3_CAN_TYPE_MASK 0x0f
126 #define ICAN3_CAN_TYPE_SFF 0x00
127 #define ICAN3_CAN_TYPE_EFF 0x01
129 #define ICAN3_CAN_DLC_MASK 0x0f
147 #define SR_CRIT (SR_BS|SR_ES)
154 #define ECC_FORM 0x40
155 #define ECC_STUFF 0x80
156 #define ECC_MASK 0xc0
159 #define ICAN3_NEW_BUFFERS 16
162 #define ICAN3_TX_BUFFERS 512
163 #define ICAN3_RX_BUFFERS 1024
166 #define ICAN3_CAN_CLOCK 8000000
169 #define DRV_NAME "janz-ican3"
288 unsigned int mbox, mbox_page;
303 if ((xord & MSYNC_RB_MASK) == MSYNC_RB_MASK)
310 ican3_set_page(mod, mbox_page);
333 unsigned int mbox, mbox_page;
352 ican3_set_page(mod, mbox_page);
391 if (i == ICAN3_NEW_BUFFERS - 1)
412 if (i == ICAN3_NEW_BUFFERS - 1)
440 spin_unlock_irqrestore(&mod->
lock, flags);
471 if (i == ICAN3_RX_BUFFERS - 1)
480 addr +=
sizeof(
desc);
507 if (i == ICAN3_TX_BUFFERS - 1)
516 addr +=
sizeof(
desc);
525 spin_unlock_irqrestore(&mod->
lock, flags);
545 dev_dbg(mod->
dev,
"%s: no free buffers\n", __func__);
550 ican3_set_page(mod,
desc.pointer);
576 dev_dbg(mod->
dev,
"%s: no buffers to recv\n", __func__);
581 ican3_set_page(mod,
desc.pointer);
606 ret = ican3_old_send_msg(mod, msg);
608 ret = ican3_new_send_msg(mod, msg);
610 spin_unlock_irqrestore(&mod->
lock, flags);
622 ret = ican3_old_recv_msg(mod, msg);
624 ret = ican3_new_recv_msg(mod, msg);
626 spin_unlock_irqrestore(&mod->
lock, flags);
638 memset(&msg, 0,
sizeof(msg));
642 return ican3_send_msg(mod, &msg);
649 memset(&msg, 0,
sizeof(msg));
653 return ican3_send_msg(mod, &msg);
661 memset(&msg, 0,
sizeof(msg));
668 ret = ican3_send_msg(mod, &msg);
682 memset(&msg, 0,
sizeof(msg));
688 msg.
data[0] = addr & 0xff;
689 msg.
data[1] = (addr >> 8) & 0xff;
690 msg.
data[2] = (addr >> 16) & 0xff;
691 msg.
data[3] = (addr >> 24) & 0xff;
695 msg.
data[4] = addr & 0xff;
696 msg.
data[5] = (addr >> 8) & 0xff;
697 msg.
data[6] = (addr >> 16) & 0xff;
698 msg.
data[7] = (addr >> 24) & 0xff;
703 return ican3_send_msg(mod, &msg);
716 memset(&msg, 0,
sizeof(msg));
727 ret = ican3_send_msg(mod, &msg);
732 memset(&msg, 0,
sizeof(msg));
748 return ican3_send_msg(mod, &msg);
754 static int ican3_set_bus_state(
struct ican3_dev *mod,
bool on)
758 memset(&msg, 0,
sizeof(msg));
762 return ican3_send_msg(mod, &msg);
765 static int ican3_set_termination(
struct ican3_dev *mod,
bool on)
769 memset(&msg, 0,
sizeof(msg));
775 return ican3_send_msg(mod, &msg);
778 static int ican3_send_inquiry(
struct ican3_dev *mod,
u8 subspec)
782 memset(&msg, 0,
sizeof(msg));
785 msg.
data[0] = subspec;
788 return ican3_send_msg(mod, &msg);
791 static int ican3_set_buserror(
struct ican3_dev *mod,
u8 quota)
795 memset(&msg, 0,
sizeof(msg));
801 return ican3_send_msg(mod, &msg);
808 static void ican3_to_can_frame(
struct ican3_dev *mod,
840 static void can_frame_to_ican3(
struct ican3_dev *mod,
862 desc->
data[2] = (cf->
can_id & 0x1fe00000) >> 21;
863 desc->
data[3] = (cf->
can_id & 0x001fe000) >> 13;
864 desc->
data[4] = (cf->
can_id & 0x00001fe0) >> 5;
865 desc->
data[5] = (cf->
can_id & 0x0000001f) << 3;
942 dev_err(mod->
dev,
"unable to handle errors on non-SJA1000\n");
948 dev_err(mod->
dev,
"error message too short\n");
954 status = msg->
data[3];
955 rxerr = msg->
data[4];
956 txerr = msg->
data[5];
984 ret = ican3_set_buserror(mod, 1);
986 dev_err(mod->
dev,
"unable to re-enable bus-error\n");
1001 dev_dbg(mod->
dev,
"data overrun interrupt\n");
1010 dev_dbg(mod->
dev,
"error warning + passive interrupt\n");
1011 if (status &
SR_BS) {
1015 }
else if (status &
SR_ES) {
1016 if (rxerr >= 128 || txerr >= 128)
1027 mod->
can.can_stats.bus_error++;
1046 if (!(ecc & ECC_DIR))
1049 cf->data[6] = txerr;
1050 cf->data[7] = rxerr;
1057 mod->
can.can_stats.error_warning++;
1058 cf->data[1] = (txerr > rxerr) ?
1062 mod->
can.can_stats.error_passive++;
1063 cf->data[1] = (txerr > rxerr) ?
1068 cf->data[6] = txerr;
1069 cf->data[7] = rxerr;
1079 switch (msg->
data[0]) {
1082 mod->
bec.rxerr = msg->
data[5];
1083 mod->
bec.txerr = msg->
data[6];
1091 dev_err(mod->
dev,
"received an unknown inquiry response\n");
1096 static void ican3_handle_unknown_message(
struct ican3_dev *mod,
1099 dev_warn(mod->
dev,
"received unknown message: spec 0x%.2x length %d\n",
1108 dev_dbg(mod->
dev,
"%s: modno %d spec 0x%.2x len %d bytes\n", __func__,
1111 switch (msg->
spec) {
1113 ican3_handle_idvers(mod, msg);
1117 ican3_handle_msglost(mod, msg);
1120 ican3_handle_cevtind(mod, msg);
1123 ican3_handle_inquiry(mod, msg);
1126 ican3_handle_unknown_message(mod, msg);
1137 struct sock *srcsk = skb->
sk;
1156 static unsigned int ican3_get_echo_skb(
struct ican3_dev *mod)
1164 netdev_err(mod->
ndev,
"BUG: echo skb not occupied\n");
1194 static bool ican3_echo_skb_matches(
struct ican3_dev *mod,
struct sk_buff *skb)
1203 echo_cf = (
struct can_frame *)echo_skb->data;
1218 static bool ican3_txok(
struct ican3_dev *mod)
1224 if (skb_queue_len(&mod->
echoq) >= ICAN3_TX_BUFFERS)
1244 static int ican3_recv_skb(
struct ican3_dev *mod)
1252 unsigned long flags;
1258 desc_addr = mod->
dpm + ((mod->
fastrx_num % 16) *
sizeof(desc));
1261 spin_unlock_irqrestore(&mod->
lock, flags);
1275 ican3_to_can_frame(mod, &desc, cf);
1287 if (ican3_echo_skb_matches(mod, skb)) {
1289 stats->
tx_bytes += ican3_get_echo_skb(mod);
1313 spin_unlock_irqrestore(&mod->
lock, flags);
1320 unsigned long flags;
1327 ret = ican3_recv_msg(mod, &msg);
1331 ican3_handle_message(mod, &msg);
1335 while (received < budget) {
1336 ret = ican3_recv_skb(mod);
1345 if (received < budget)
1351 if (netif_queue_stopped(mod->
ndev) && ican3_txok(mod))
1352 netif_wake_queue(mod->
ndev);
1354 spin_unlock_irqrestore(&mod->
lock, flags);
1371 if (stat == (1 << mod->
num))
1379 napi_schedule(&mod->
napi);
1392 static int ican3_reset_module(
struct ican3_dev *mod)
1394 unsigned long start;
1414 if (runnew == (runold ^ 0xff))
1420 dev_err(mod->
dev,
"failed to reset CAN module\n");
1426 ican3_msg_disconnect(mod);
1427 ican3_reset_module(mod);
1437 ret = ican3_reset_module(mod);
1439 dev_err(mod->
dev,
"unable to reset module\n");
1446 ret = ican3_msg_connect(mod);
1448 dev_err(mod->
dev,
"unable to connect to module\n");
1452 ican3_init_new_host_interface(mod);
1453 ret = ican3_msg_newhostif(mod);
1455 dev_err(mod->
dev,
"unable to switch to new-style interface\n");
1460 ret = ican3_set_termination(mod,
true);
1462 dev_err(mod->
dev,
"unable to enable termination\n");
1467 ret = ican3_set_buserror(mod, 1);
1469 dev_err(mod->
dev,
"unable to set bus-error\n");
1473 ican3_init_fast_host_interface(mod);
1474 ret = ican3_msg_fasthostif(mod);
1476 dev_err(mod->
dev,
"unable to switch to fast host interface\n");
1480 ret = ican3_set_id_filter(mod,
true);
1482 dev_err(mod->
dev,
"unable to set acceptance filter\n");
1493 static int ican3_open(
struct net_device *ndev)
1495 struct ican3_dev *mod = netdev_priv(ndev);
1501 dev_err(mod->
dev,
"unable to start CAN layer\n");
1506 ret = ican3_set_bus_state(mod,
true);
1515 netif_start_queue(ndev);
1520 static int ican3_stop(
struct net_device *ndev)
1522 struct ican3_dev *mod = netdev_priv(ndev);
1526 netif_stop_queue(ndev);
1530 ret = ican3_set_bus_state(mod,
false);
1532 dev_err(mod->
dev,
"unable to set bus-off\n");
1546 struct ican3_dev *mod = netdev_priv(ndev);
1550 unsigned long flags;
1552 if (can_dropped_invalid_skb(ndev, skb))
1558 if (!ican3_txok(mod)) {
1559 dev_err(mod->
dev,
"BUG: no free descriptors\n");
1560 spin_unlock_irqrestore(&mod->
lock, flags);
1566 desc_addr = mod->
dpm + ((mod->
fasttx_num % 16) *
sizeof(desc));
1567 memset(&desc, 0,
sizeof(desc));
1571 can_frame_to_ican3(mod, cf, &desc);
1579 ican3_put_echo_skb(mod, skb);
1600 if (!ican3_txok(mod))
1601 netif_stop_queue(ndev);
1603 spin_unlock_irqrestore(&mod->
lock, flags);
1608 .ndo_open = ican3_open,
1609 .ndo_stop = ican3_stop,
1610 .ndo_start_xmit = ican3_xmit,
1636 static int ican3_set_bittiming(
struct net_device *ndev)
1638 struct ican3_dev *mod = netdev_priv(ndev);
1643 btr0 = ((bt->
brp - 1) & 0x3f) | (((bt->
sjw - 1) & 0x3) << 6);
1649 memset(&msg, 0,
sizeof(msg));
1657 return ican3_send_msg(mod, &msg);
1662 struct ican3_dev *mod = netdev_priv(ndev);
1669 ret = ican3_set_bus_state(mod,
true);
1678 if (netif_queue_stopped(ndev))
1679 netif_wake_queue(ndev);
1684 static int ican3_get_berr_counter(
const struct net_device *ndev,
1687 struct ican3_dev *mod = netdev_priv(ndev);
1731 const char *buf,
size_t count)
1740 ret = ican3_set_termination(mod, enable);
1748 ican3_sysfs_set_term);
1750 static struct attribute *ican3_sysfs_attrs[] = {
1751 &dev_attr_termination.attr,
1756 .attrs = ican3_sysfs_attrs,
1772 pdata = pdev->
dev.platform_data;
1784 dev_err(dev,
"unable to allocate CANdev\n");
1789 platform_set_drvdata(pdev, ndev);
1790 mod = netdev_priv(ndev);
1795 skb_queue_head_init(&mod->
echoq);
1811 mod->
can.bittiming_const = &ican3_bittiming_const;
1812 mod->
can.do_set_bittiming = ican3_set_bittiming;
1813 mod->
can.do_set_mode = ican3_set_mode;
1814 mod->
can.do_get_berr_counter = ican3_get_berr_counter;
1822 dev_err(dev,
"IRQ line not found\n");
1832 dev_err(dev,
"MODULbus registers not found\n");
1839 dev_err(dev,
"MODULbus registers not ioremap\n");
1849 dev_err(dev,
"CONTROL registers not found\n");
1851 goto out_iounmap_dpm;
1856 dev_err(dev,
"CONTROL registers not ioremap\n");
1858 goto out_iounmap_dpm;
1865 dev_err(dev,
"unable to request IRQ\n");
1866 goto out_iounmap_ctrl;
1870 napi_enable(&mod->
napi);
1871 ret = ican3_startup_module(mod);
1873 dev_err(dev,
"%s: unable to start CANdev\n", __func__);
1880 dev_err(dev,
"%s: unable to register CANdev\n", __func__);
1884 dev_info(dev,
"module %d: registered CAN device\n", pdata->
modno);
1888 napi_disable(&mod->
napi);
1903 struct net_device *ndev = platform_get_drvdata(pdev);
1904 struct ican3_dev *mod = netdev_priv(ndev);
1908 napi_disable(&mod->
napi);
1913 ican3_shutdown_module(mod);
1929 .probe = ican3_probe,