Thomas Gleixner | 2b72c9e | 2019-05-29 16:57:54 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 1999 - 2010 Intel Corporation. |
Tomoya MORINAGA | 74b5127 | 2012-01-27 20:05:21 +0900 | [diff] [blame] | 4 | * Copyright (C) 2010 LAPIS SEMICONDUCTOR CO., LTD. |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/interrupt.h> |
| 8 | #include <linux/delay.h> |
| 9 | #include <linux/io.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/sched.h> |
| 12 | #include <linux/pci.h> |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 13 | #include <linux/kernel.h> |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/netdevice.h> |
| 17 | #include <linux/skbuff.h> |
| 18 | #include <linux/can.h> |
| 19 | #include <linux/can/dev.h> |
| 20 | #include <linux/can/error.h> |
| 21 | |
Tomoya | 0a80410 | 2010-11-17 14:06:25 +0000 | [diff] [blame] | 22 | #define PCH_CTRL_INIT BIT(0) /* The INIT bit of CANCONT register. */ |
| 23 | #define PCH_CTRL_IE BIT(1) /* The IE bit of CAN control register */ |
| 24 | #define PCH_CTRL_IE_SIE_EIE (BIT(3) | BIT(2) | BIT(1)) |
| 25 | #define PCH_CTRL_CCE BIT(6) |
| 26 | #define PCH_CTRL_OPT BIT(7) /* The OPT bit of CANCONT register. */ |
| 27 | #define PCH_OPT_SILENT BIT(3) /* The Silent bit of CANOPT reg. */ |
| 28 | #define PCH_OPT_LBACK BIT(4) /* The LoopBack bit of CANOPT reg. */ |
| 29 | |
Tomoya | 086b565 | 2010-11-17 01:13:16 +0000 | [diff] [blame] | 30 | #define PCH_CMASK_RX_TX_SET 0x00f3 |
| 31 | #define PCH_CMASK_RX_TX_GET 0x0073 |
| 32 | #define PCH_CMASK_ALL 0xff |
Tomoya | 0a80410 | 2010-11-17 14:06:25 +0000 | [diff] [blame] | 33 | #define PCH_CMASK_NEWDAT BIT(2) |
| 34 | #define PCH_CMASK_CLRINTPND BIT(3) |
| 35 | #define PCH_CMASK_CTRL BIT(4) |
| 36 | #define PCH_CMASK_ARB BIT(5) |
| 37 | #define PCH_CMASK_MASK BIT(6) |
| 38 | #define PCH_CMASK_RDWR BIT(7) |
| 39 | #define PCH_IF_MCONT_NEWDAT BIT(15) |
| 40 | #define PCH_IF_MCONT_MSGLOST BIT(14) |
| 41 | #define PCH_IF_MCONT_INTPND BIT(13) |
| 42 | #define PCH_IF_MCONT_UMASK BIT(12) |
| 43 | #define PCH_IF_MCONT_TXIE BIT(11) |
| 44 | #define PCH_IF_MCONT_RXIE BIT(10) |
| 45 | #define PCH_IF_MCONT_RMTEN BIT(9) |
| 46 | #define PCH_IF_MCONT_TXRQXT BIT(8) |
| 47 | #define PCH_IF_MCONT_EOB BIT(7) |
| 48 | #define PCH_IF_MCONT_DLC (BIT(0) | BIT(1) | BIT(2) | BIT(3)) |
| 49 | #define PCH_MASK2_MDIR_MXTD (BIT(14) | BIT(15)) |
| 50 | #define PCH_ID2_DIR BIT(13) |
| 51 | #define PCH_ID2_XTD BIT(14) |
| 52 | #define PCH_ID_MSGVAL BIT(15) |
| 53 | #define PCH_IF_CREQ_BUSY BIT(15) |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 54 | |
Tomoya | 086b565 | 2010-11-17 01:13:16 +0000 | [diff] [blame] | 55 | #define PCH_STATUS_INT 0x8000 |
Xi Wang | 44b0052c | 2011-12-12 02:16:20 -0500 | [diff] [blame] | 56 | #define PCH_RP 0x00008000 |
Tomoya | 086b565 | 2010-11-17 01:13:16 +0000 | [diff] [blame] | 57 | #define PCH_REC 0x00007f00 |
| 58 | #define PCH_TEC 0x000000ff |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 59 | |
Tomoya | 0a80410 | 2010-11-17 14:06:25 +0000 | [diff] [blame] | 60 | #define PCH_TX_OK BIT(3) |
| 61 | #define PCH_RX_OK BIT(4) |
| 62 | #define PCH_EPASSIV BIT(5) |
| 63 | #define PCH_EWARN BIT(6) |
| 64 | #define PCH_BUS_OFF BIT(7) |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 65 | |
| 66 | /* bit position of certain controller bits. */ |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 67 | #define PCH_BIT_BRP_SHIFT 0 |
| 68 | #define PCH_BIT_SJW_SHIFT 6 |
| 69 | #define PCH_BIT_TSEG1_SHIFT 8 |
| 70 | #define PCH_BIT_TSEG2_SHIFT 12 |
| 71 | #define PCH_BIT_BRPE_BRPE_SHIFT 6 |
| 72 | |
Tomoya | 086b565 | 2010-11-17 01:13:16 +0000 | [diff] [blame] | 73 | #define PCH_MSK_BITT_BRP 0x3f |
| 74 | #define PCH_MSK_BRPE_BRPE 0x3c0 |
| 75 | #define PCH_MSK_CTRL_IE_SIE_EIE 0x07 |
| 76 | #define PCH_COUNTER_LIMIT 10 |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 77 | |
| 78 | #define PCH_CAN_CLK 50000000 /* 50MHz */ |
| 79 | |
Tomoya | 9388b16 | 2010-12-12 20:24:17 +0000 | [diff] [blame] | 80 | /* |
| 81 | * Define the number of message object. |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 82 | * PCH CAN communications are done via Message RAM. |
Tomoya | 9388b16 | 2010-12-12 20:24:17 +0000 | [diff] [blame] | 83 | * The Message RAM consists of 32 message objects. |
| 84 | */ |
Tomoya | 15ffc8f | 2010-11-29 18:15:02 +0000 | [diff] [blame] | 85 | #define PCH_RX_OBJ_NUM 26 |
| 86 | #define PCH_TX_OBJ_NUM 6 |
| 87 | #define PCH_RX_OBJ_START 1 |
| 88 | #define PCH_RX_OBJ_END PCH_RX_OBJ_NUM |
| 89 | #define PCH_TX_OBJ_START (PCH_RX_OBJ_END + 1) |
| 90 | #define PCH_TX_OBJ_END (PCH_RX_OBJ_NUM + PCH_TX_OBJ_NUM) |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 91 | |
| 92 | #define PCH_FIFO_THRESH 16 |
| 93 | |
Tomoya | 76d94b2 | 2010-12-12 20:24:07 +0000 | [diff] [blame] | 94 | /* TxRqst2 show status of MsgObjNo.17~32 */ |
| 95 | #define PCH_TREQ2_TX_MASK (((1 << PCH_TX_OBJ_NUM) - 1) <<\ |
| 96 | (PCH_RX_OBJ_END - 16)) |
| 97 | |
Tomoya | 8339a7e | 2010-11-29 18:11:52 +0000 | [diff] [blame] | 98 | enum pch_ifreg { |
| 99 | PCH_RX_IFREG, |
| 100 | PCH_TX_IFREG, |
| 101 | }; |
| 102 | |
Tomoya | d68f683 | 2010-11-29 18:16:15 +0000 | [diff] [blame] | 103 | enum pch_can_err { |
| 104 | PCH_STUF_ERR = 1, |
| 105 | PCH_FORM_ERR, |
| 106 | PCH_ACK_ERR, |
| 107 | PCH_BIT1_ERR, |
| 108 | PCH_BIT0_ERR, |
| 109 | PCH_CRC_ERR, |
| 110 | PCH_LEC_ALL, |
| 111 | }; |
| 112 | |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 113 | enum pch_can_mode { |
| 114 | PCH_CAN_ENABLE, |
| 115 | PCH_CAN_DISABLE, |
| 116 | PCH_CAN_ALL, |
| 117 | PCH_CAN_NONE, |
| 118 | PCH_CAN_STOP, |
Tomoya | 9388b16 | 2010-12-12 20:24:17 +0000 | [diff] [blame] | 119 | PCH_CAN_RUN, |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
Tomoya | 8339a7e | 2010-11-29 18:11:52 +0000 | [diff] [blame] | 122 | struct pch_can_if_regs { |
| 123 | u32 creq; |
| 124 | u32 cmask; |
| 125 | u32 mask1; |
| 126 | u32 mask2; |
| 127 | u32 id1; |
| 128 | u32 id2; |
| 129 | u32 mcont; |
Tomoya | 8ac9702 | 2010-12-12 20:24:09 +0000 | [diff] [blame] | 130 | u32 data[4]; |
Tomoya | 8339a7e | 2010-11-29 18:11:52 +0000 | [diff] [blame] | 131 | u32 rsv[13]; |
| 132 | }; |
| 133 | |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 134 | struct pch_can_regs { |
| 135 | u32 cont; |
| 136 | u32 stat; |
| 137 | u32 errc; |
| 138 | u32 bitt; |
| 139 | u32 intr; |
| 140 | u32 opt; |
| 141 | u32 brpe; |
Tomoya | 8339a7e | 2010-11-29 18:11:52 +0000 | [diff] [blame] | 142 | u32 reserve; |
| 143 | struct pch_can_if_regs ifregs[2]; /* [0]=if1 [1]=if2 */ |
| 144 | u32 reserve1[8]; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 145 | u32 treq1; |
| 146 | u32 treq2; |
Tomoya | 8339a7e | 2010-11-29 18:11:52 +0000 | [diff] [blame] | 147 | u32 reserve2[6]; |
| 148 | u32 data1; |
| 149 | u32 data2; |
| 150 | u32 reserve3[6]; |
| 151 | u32 canipend1; |
| 152 | u32 canipend2; |
| 153 | u32 reserve4[6]; |
| 154 | u32 canmval1; |
| 155 | u32 canmval2; |
| 156 | u32 reserve5[37]; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 157 | u32 srst; |
| 158 | }; |
| 159 | |
| 160 | struct pch_can_priv { |
| 161 | struct can_priv can; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 162 | struct pci_dev *dev; |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 163 | u32 tx_enable[PCH_TX_OBJ_END]; |
| 164 | u32 rx_enable[PCH_TX_OBJ_END]; |
| 165 | u32 rx_link[PCH_TX_OBJ_END]; |
| 166 | u32 int_enables; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 167 | struct net_device *ndev; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 168 | struct pch_can_regs __iomem *regs; |
| 169 | struct napi_struct napi; |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 170 | int tx_obj; /* Point next Tx Obj index */ |
| 171 | int use_msi; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 172 | }; |
| 173 | |
Marc Kleine-Budde | 194b9a4 | 2012-07-16 12:58:31 +0200 | [diff] [blame] | 174 | static const struct can_bittiming_const pch_can_bittiming_const = { |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 175 | .name = KBUILD_MODNAME, |
Tomoya MORINAGA | ebc02e9 | 2011-02-09 16:46:21 -0800 | [diff] [blame] | 176 | .tseg1_min = 2, |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 177 | .tseg1_max = 16, |
Tomoya MORINAGA | ebc02e9 | 2011-02-09 16:46:21 -0800 | [diff] [blame] | 178 | .tseg2_min = 1, |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 179 | .tseg2_max = 8, |
| 180 | .sjw_max = 4, |
| 181 | .brp_min = 1, |
| 182 | .brp_max = 1024, /* 6bit + extended 4bit */ |
| 183 | .brp_inc = 1, |
| 184 | }; |
| 185 | |
Benoit Taine | 9baa3c3 | 2014-08-08 15:56:03 +0200 | [diff] [blame] | 186 | static const struct pci_device_id pch_pci_tbl[] = { |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 187 | {PCI_VENDOR_ID_INTEL, 0x8818, PCI_ANY_ID, PCI_ANY_ID,}, |
| 188 | {0,} |
| 189 | }; |
| 190 | MODULE_DEVICE_TABLE(pci, pch_pci_tbl); |
| 191 | |
Marc Kleine-Budde | 526de53 | 2010-10-30 16:27:48 -0700 | [diff] [blame] | 192 | static inline void pch_can_bit_set(void __iomem *addr, u32 mask) |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 193 | { |
| 194 | iowrite32(ioread32(addr) | mask, addr); |
| 195 | } |
| 196 | |
Marc Kleine-Budde | 526de53 | 2010-10-30 16:27:48 -0700 | [diff] [blame] | 197 | static inline void pch_can_bit_clear(void __iomem *addr, u32 mask) |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 198 | { |
| 199 | iowrite32(ioread32(addr) & ~mask, addr); |
| 200 | } |
| 201 | |
| 202 | static void pch_can_set_run_mode(struct pch_can_priv *priv, |
| 203 | enum pch_can_mode mode) |
| 204 | { |
| 205 | switch (mode) { |
| 206 | case PCH_CAN_RUN: |
Tomoya | 086b565 | 2010-11-17 01:13:16 +0000 | [diff] [blame] | 207 | pch_can_bit_clear(&priv->regs->cont, PCH_CTRL_INIT); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 208 | break; |
| 209 | |
| 210 | case PCH_CAN_STOP: |
Tomoya | 086b565 | 2010-11-17 01:13:16 +0000 | [diff] [blame] | 211 | pch_can_bit_set(&priv->regs->cont, PCH_CTRL_INIT); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 212 | break; |
| 213 | |
| 214 | default: |
Tomoya | 435b4ef | 2010-12-12 20:24:16 +0000 | [diff] [blame] | 215 | netdev_err(priv->ndev, "%s -> Invalid Mode.\n", __func__); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 216 | break; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | static void pch_can_set_optmode(struct pch_can_priv *priv) |
| 221 | { |
| 222 | u32 reg_val = ioread32(&priv->regs->opt); |
| 223 | |
| 224 | if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) |
Tomoya | 086b565 | 2010-11-17 01:13:16 +0000 | [diff] [blame] | 225 | reg_val |= PCH_OPT_SILENT; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 226 | |
| 227 | if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) |
Tomoya | 086b565 | 2010-11-17 01:13:16 +0000 | [diff] [blame] | 228 | reg_val |= PCH_OPT_LBACK; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 229 | |
Tomoya | 086b565 | 2010-11-17 01:13:16 +0000 | [diff] [blame] | 230 | pch_can_bit_set(&priv->regs->cont, PCH_CTRL_OPT); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 231 | iowrite32(reg_val, &priv->regs->opt); |
| 232 | } |
| 233 | |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 234 | static void pch_can_rw_msg_obj(void __iomem *creq_addr, u32 num) |
| 235 | { |
| 236 | int counter = PCH_COUNTER_LIMIT; |
| 237 | u32 ifx_creq; |
| 238 | |
| 239 | iowrite32(num, creq_addr); |
| 240 | while (counter) { |
| 241 | ifx_creq = ioread32(creq_addr) & PCH_IF_CREQ_BUSY; |
| 242 | if (!ifx_creq) |
| 243 | break; |
| 244 | counter--; |
| 245 | udelay(1); |
| 246 | } |
| 247 | if (!counter) |
| 248 | pr_err("%s:IF1 BUSY Flag is set forever.\n", __func__); |
| 249 | } |
| 250 | |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 251 | static void pch_can_set_int_enables(struct pch_can_priv *priv, |
| 252 | enum pch_can_mode interrupt_no) |
| 253 | { |
| 254 | switch (interrupt_no) { |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 255 | case PCH_CAN_DISABLE: |
Tomoya | 086b565 | 2010-11-17 01:13:16 +0000 | [diff] [blame] | 256 | pch_can_bit_clear(&priv->regs->cont, PCH_CTRL_IE); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 257 | break; |
| 258 | |
| 259 | case PCH_CAN_ALL: |
Tomoya | 086b565 | 2010-11-17 01:13:16 +0000 | [diff] [blame] | 260 | pch_can_bit_set(&priv->regs->cont, PCH_CTRL_IE_SIE_EIE); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 261 | break; |
| 262 | |
| 263 | case PCH_CAN_NONE: |
Tomoya | 086b565 | 2010-11-17 01:13:16 +0000 | [diff] [blame] | 264 | pch_can_bit_clear(&priv->regs->cont, PCH_CTRL_IE_SIE_EIE); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 265 | break; |
| 266 | |
| 267 | default: |
Tomoya | 435b4ef | 2010-12-12 20:24:16 +0000 | [diff] [blame] | 268 | netdev_err(priv->ndev, "Invalid interrupt number.\n"); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 269 | break; |
| 270 | } |
| 271 | } |
| 272 | |
Tomoya | 8339a7e | 2010-11-29 18:11:52 +0000 | [diff] [blame] | 273 | static void pch_can_set_rxtx(struct pch_can_priv *priv, u32 buff_num, |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 274 | int set, enum pch_ifreg dir) |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 275 | { |
Tomoya | 8339a7e | 2010-11-29 18:11:52 +0000 | [diff] [blame] | 276 | u32 ie; |
| 277 | |
| 278 | if (dir) |
| 279 | ie = PCH_IF_MCONT_TXIE; |
| 280 | else |
| 281 | ie = PCH_IF_MCONT_RXIE; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 282 | |
Tomoya | c755145 | 2010-12-12 20:24:21 +0000 | [diff] [blame] | 283 | /* Reading the Msg buffer from Message RAM to IF1/2 registers. */ |
Tomoya | 8339a7e | 2010-11-29 18:11:52 +0000 | [diff] [blame] | 284 | iowrite32(PCH_CMASK_RX_TX_GET, &priv->regs->ifregs[dir].cmask); |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 285 | pch_can_rw_msg_obj(&priv->regs->ifregs[dir].creq, buff_num); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 286 | |
Tomoya | 9388b16 | 2010-12-12 20:24:17 +0000 | [diff] [blame] | 287 | /* Setting the IF1/2MASK1 register to access MsgVal and RxIE bits */ |
Tomoya | 086b565 | 2010-11-17 01:13:16 +0000 | [diff] [blame] | 288 | iowrite32(PCH_CMASK_RDWR | PCH_CMASK_ARB | PCH_CMASK_CTRL, |
Tomoya | 8339a7e | 2010-11-29 18:11:52 +0000 | [diff] [blame] | 289 | &priv->regs->ifregs[dir].cmask); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 290 | |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 291 | if (set) { |
Tomoya | 9388b16 | 2010-12-12 20:24:17 +0000 | [diff] [blame] | 292 | /* Setting the MsgVal and RxIE/TxIE bits */ |
Tomoya | 8339a7e | 2010-11-29 18:11:52 +0000 | [diff] [blame] | 293 | pch_can_bit_set(&priv->regs->ifregs[dir].mcont, ie); |
| 294 | pch_can_bit_set(&priv->regs->ifregs[dir].id2, PCH_ID_MSGVAL); |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 295 | } else { |
Tomoya | 9388b16 | 2010-12-12 20:24:17 +0000 | [diff] [blame] | 296 | /* Clearing the MsgVal and RxIE/TxIE bits */ |
Tomoya | 8339a7e | 2010-11-29 18:11:52 +0000 | [diff] [blame] | 297 | pch_can_bit_clear(&priv->regs->ifregs[dir].mcont, ie); |
| 298 | pch_can_bit_clear(&priv->regs->ifregs[dir].id2, PCH_ID_MSGVAL); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 301 | pch_can_rw_msg_obj(&priv->regs->ifregs[dir].creq, buff_num); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 304 | static void pch_can_set_rx_all(struct pch_can_priv *priv, int set) |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 305 | { |
| 306 | int i; |
| 307 | |
| 308 | /* Traversing to obtain the object configured as receivers. */ |
Tomoya | 15ffc8f | 2010-11-29 18:15:02 +0000 | [diff] [blame] | 309 | for (i = PCH_RX_OBJ_START; i <= PCH_RX_OBJ_END; i++) |
| 310 | pch_can_set_rxtx(priv, i, set, PCH_RX_IFREG); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 313 | static void pch_can_set_tx_all(struct pch_can_priv *priv, int set) |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 314 | { |
| 315 | int i; |
| 316 | |
| 317 | /* Traversing to obtain the object configured as transmit object. */ |
Tomoya | 15ffc8f | 2010-11-29 18:15:02 +0000 | [diff] [blame] | 318 | for (i = PCH_TX_OBJ_START; i <= PCH_TX_OBJ_END; i++) |
| 319 | pch_can_set_rxtx(priv, i, set, PCH_TX_IFREG); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 322 | static u32 pch_can_int_pending(struct pch_can_priv *priv) |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 323 | { |
| 324 | return ioread32(&priv->regs->intr) & 0xffff; |
| 325 | } |
| 326 | |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 327 | static void pch_can_clear_if_buffers(struct pch_can_priv *priv) |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 328 | { |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 329 | int i; /* Msg Obj ID (1~32) */ |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 330 | |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 331 | for (i = PCH_RX_OBJ_START; i <= PCH_TX_OBJ_END; i++) { |
Tomoya | 8339a7e | 2010-11-29 18:11:52 +0000 | [diff] [blame] | 332 | iowrite32(PCH_CMASK_RX_TX_SET, &priv->regs->ifregs[0].cmask); |
| 333 | iowrite32(0xffff, &priv->regs->ifregs[0].mask1); |
| 334 | iowrite32(0xffff, &priv->regs->ifregs[0].mask2); |
| 335 | iowrite32(0x0, &priv->regs->ifregs[0].id1); |
| 336 | iowrite32(0x0, &priv->regs->ifregs[0].id2); |
| 337 | iowrite32(0x0, &priv->regs->ifregs[0].mcont); |
Tomoya | 8ac9702 | 2010-12-12 20:24:09 +0000 | [diff] [blame] | 338 | iowrite32(0x0, &priv->regs->ifregs[0].data[0]); |
| 339 | iowrite32(0x0, &priv->regs->ifregs[0].data[1]); |
| 340 | iowrite32(0x0, &priv->regs->ifregs[0].data[2]); |
| 341 | iowrite32(0x0, &priv->regs->ifregs[0].data[3]); |
Tomoya | 086b565 | 2010-11-17 01:13:16 +0000 | [diff] [blame] | 342 | iowrite32(PCH_CMASK_RDWR | PCH_CMASK_MASK | |
| 343 | PCH_CMASK_ARB | PCH_CMASK_CTRL, |
Tomoya | 8339a7e | 2010-11-29 18:11:52 +0000 | [diff] [blame] | 344 | &priv->regs->ifregs[0].cmask); |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 345 | pch_can_rw_msg_obj(&priv->regs->ifregs[0].creq, i); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
| 349 | static void pch_can_config_rx_tx_buffers(struct pch_can_priv *priv) |
| 350 | { |
| 351 | int i; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 352 | |
Tomoya | 15ffc8f | 2010-11-29 18:15:02 +0000 | [diff] [blame] | 353 | for (i = PCH_RX_OBJ_START; i <= PCH_RX_OBJ_END; i++) { |
Tomoya | 9388b16 | 2010-12-12 20:24:17 +0000 | [diff] [blame] | 354 | iowrite32(PCH_CMASK_RX_TX_GET, &priv->regs->ifregs[0].cmask); |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 355 | pch_can_rw_msg_obj(&priv->regs->ifregs[0].creq, i); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 356 | |
Tomoya | 15ffc8f | 2010-11-29 18:15:02 +0000 | [diff] [blame] | 357 | iowrite32(0x0, &priv->regs->ifregs[0].id1); |
| 358 | iowrite32(0x0, &priv->regs->ifregs[0].id2); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 359 | |
Tomoya | 15ffc8f | 2010-11-29 18:15:02 +0000 | [diff] [blame] | 360 | pch_can_bit_set(&priv->regs->ifregs[0].mcont, |
| 361 | PCH_IF_MCONT_UMASK); |
| 362 | |
Tomoya | 15ffc8f | 2010-11-29 18:15:02 +0000 | [diff] [blame] | 363 | /* In case FIFO mode, Last EoB of Rx Obj must be 1 */ |
| 364 | if (i == PCH_RX_OBJ_END) |
Tomoya | 8339a7e | 2010-11-29 18:11:52 +0000 | [diff] [blame] | 365 | pch_can_bit_set(&priv->regs->ifregs[0].mcont, |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 366 | PCH_IF_MCONT_EOB); |
| 367 | else |
| 368 | pch_can_bit_clear(&priv->regs->ifregs[0].mcont, |
Tomoya | 086b565 | 2010-11-17 01:13:16 +0000 | [diff] [blame] | 369 | PCH_IF_MCONT_EOB); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 370 | |
Tomoya | 15ffc8f | 2010-11-29 18:15:02 +0000 | [diff] [blame] | 371 | iowrite32(0, &priv->regs->ifregs[0].mask1); |
| 372 | pch_can_bit_clear(&priv->regs->ifregs[0].mask2, |
| 373 | 0x1fff | PCH_MASK2_MDIR_MXTD); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 374 | |
Tomoya | 15ffc8f | 2010-11-29 18:15:02 +0000 | [diff] [blame] | 375 | /* Setting CMASK for writing */ |
Tomoya | 9388b16 | 2010-12-12 20:24:17 +0000 | [diff] [blame] | 376 | iowrite32(PCH_CMASK_RDWR | PCH_CMASK_MASK | PCH_CMASK_ARB | |
| 377 | PCH_CMASK_CTRL, &priv->regs->ifregs[0].cmask); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 378 | |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 379 | pch_can_rw_msg_obj(&priv->regs->ifregs[0].creq, i); |
Tomoya | 15ffc8f | 2010-11-29 18:15:02 +0000 | [diff] [blame] | 380 | } |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 381 | |
Tomoya | 15ffc8f | 2010-11-29 18:15:02 +0000 | [diff] [blame] | 382 | for (i = PCH_TX_OBJ_START; i <= PCH_TX_OBJ_END; i++) { |
Tomoya | 9388b16 | 2010-12-12 20:24:17 +0000 | [diff] [blame] | 383 | iowrite32(PCH_CMASK_RX_TX_GET, &priv->regs->ifregs[1].cmask); |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 384 | pch_can_rw_msg_obj(&priv->regs->ifregs[1].creq, i); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 385 | |
Tomoya | 15ffc8f | 2010-11-29 18:15:02 +0000 | [diff] [blame] | 386 | /* Resetting DIR bit for reception */ |
| 387 | iowrite32(0x0, &priv->regs->ifregs[1].id1); |
Tomoya | 44c9aa8 | 2010-12-12 20:24:14 +0000 | [diff] [blame] | 388 | iowrite32(PCH_ID2_DIR, &priv->regs->ifregs[1].id2); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 389 | |
Tomoya | 15ffc8f | 2010-11-29 18:15:02 +0000 | [diff] [blame] | 390 | /* Setting EOB bit for transmitter */ |
Tomoya | 44c9aa8 | 2010-12-12 20:24:14 +0000 | [diff] [blame] | 391 | iowrite32(PCH_IF_MCONT_EOB | PCH_IF_MCONT_UMASK, |
| 392 | &priv->regs->ifregs[1].mcont); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 393 | |
Tomoya | 15ffc8f | 2010-11-29 18:15:02 +0000 | [diff] [blame] | 394 | iowrite32(0, &priv->regs->ifregs[1].mask1); |
| 395 | pch_can_bit_clear(&priv->regs->ifregs[1].mask2, 0x1fff); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 396 | |
Tomoya | 15ffc8f | 2010-11-29 18:15:02 +0000 | [diff] [blame] | 397 | /* Setting CMASK for writing */ |
Tomoya | 9388b16 | 2010-12-12 20:24:17 +0000 | [diff] [blame] | 398 | iowrite32(PCH_CMASK_RDWR | PCH_CMASK_MASK | PCH_CMASK_ARB | |
| 399 | PCH_CMASK_CTRL, &priv->regs->ifregs[1].cmask); |
Tomoya | 15ffc8f | 2010-11-29 18:15:02 +0000 | [diff] [blame] | 400 | |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 401 | pch_can_rw_msg_obj(&priv->regs->ifregs[1].creq, i); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 402 | } |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | static void pch_can_init(struct pch_can_priv *priv) |
| 406 | { |
| 407 | /* Stopping the Can device. */ |
| 408 | pch_can_set_run_mode(priv, PCH_CAN_STOP); |
| 409 | |
| 410 | /* Clearing all the message object buffers. */ |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 411 | pch_can_clear_if_buffers(priv); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 412 | |
| 413 | /* Configuring the respective message object as either rx/tx object. */ |
| 414 | pch_can_config_rx_tx_buffers(priv); |
| 415 | |
| 416 | /* Enabling the interrupts. */ |
| 417 | pch_can_set_int_enables(priv, PCH_CAN_ALL); |
| 418 | } |
| 419 | |
| 420 | static void pch_can_release(struct pch_can_priv *priv) |
| 421 | { |
| 422 | /* Stooping the CAN device. */ |
| 423 | pch_can_set_run_mode(priv, PCH_CAN_STOP); |
| 424 | |
| 425 | /* Disabling the interrupts. */ |
| 426 | pch_can_set_int_enables(priv, PCH_CAN_NONE); |
| 427 | |
| 428 | /* Disabling all the receive object. */ |
Tomoya | 8339a7e | 2010-11-29 18:11:52 +0000 | [diff] [blame] | 429 | pch_can_set_rx_all(priv, 0); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 430 | |
| 431 | /* Disabling all the transmit object. */ |
Tomoya | 8339a7e | 2010-11-29 18:11:52 +0000 | [diff] [blame] | 432 | pch_can_set_tx_all(priv, 0); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | /* This function clears interrupt(s) from the CAN device. */ |
| 436 | static void pch_can_int_clr(struct pch_can_priv *priv, u32 mask) |
| 437 | { |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 438 | /* Clear interrupt for transmit object */ |
Tomoya | 15ffc8f | 2010-11-29 18:15:02 +0000 | [diff] [blame] | 439 | if ((mask >= PCH_RX_OBJ_START) && (mask <= PCH_RX_OBJ_END)) { |
| 440 | /* Setting CMASK for clearing the reception interrupts. */ |
| 441 | iowrite32(PCH_CMASK_RDWR | PCH_CMASK_CTRL | PCH_CMASK_ARB, |
| 442 | &priv->regs->ifregs[0].cmask); |
| 443 | |
| 444 | /* Clearing the Dir bit. */ |
| 445 | pch_can_bit_clear(&priv->regs->ifregs[0].id2, PCH_ID2_DIR); |
| 446 | |
| 447 | /* Clearing NewDat & IntPnd */ |
| 448 | pch_can_bit_clear(&priv->regs->ifregs[0].mcont, |
| 449 | PCH_IF_MCONT_NEWDAT | PCH_IF_MCONT_INTPND); |
| 450 | |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 451 | pch_can_rw_msg_obj(&priv->regs->ifregs[0].creq, mask); |
Tomoya | 15ffc8f | 2010-11-29 18:15:02 +0000 | [diff] [blame] | 452 | } else if ((mask >= PCH_TX_OBJ_START) && (mask <= PCH_TX_OBJ_END)) { |
Tomoya | 9388b16 | 2010-12-12 20:24:17 +0000 | [diff] [blame] | 453 | /* |
| 454 | * Setting CMASK for clearing interrupts for frame transmission. |
| 455 | */ |
Tomoya | 086b565 | 2010-11-17 01:13:16 +0000 | [diff] [blame] | 456 | iowrite32(PCH_CMASK_RDWR | PCH_CMASK_CTRL | PCH_CMASK_ARB, |
Tomoya | 8339a7e | 2010-11-29 18:11:52 +0000 | [diff] [blame] | 457 | &priv->regs->ifregs[1].cmask); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 458 | |
| 459 | /* Resetting the ID registers. */ |
Tomoya | 8339a7e | 2010-11-29 18:11:52 +0000 | [diff] [blame] | 460 | pch_can_bit_set(&priv->regs->ifregs[1].id2, |
Tomoya | 086b565 | 2010-11-17 01:13:16 +0000 | [diff] [blame] | 461 | PCH_ID2_DIR | (0x7ff << 2)); |
Tomoya | 8339a7e | 2010-11-29 18:11:52 +0000 | [diff] [blame] | 462 | iowrite32(0x0, &priv->regs->ifregs[1].id1); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 463 | |
Marc Kleine-Budde | 88bfb9a | 2020-09-16 00:34:55 +0200 | [diff] [blame] | 464 | /* Clearing NewDat, TxRqst & IntPnd */ |
Tomoya | 8339a7e | 2010-11-29 18:11:52 +0000 | [diff] [blame] | 465 | pch_can_bit_clear(&priv->regs->ifregs[1].mcont, |
Tomoya | 086b565 | 2010-11-17 01:13:16 +0000 | [diff] [blame] | 466 | PCH_IF_MCONT_NEWDAT | PCH_IF_MCONT_INTPND | |
| 467 | PCH_IF_MCONT_TXRQXT); |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 468 | pch_can_rw_msg_obj(&priv->regs->ifregs[1].creq, mask); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 472 | static void pch_can_reset(struct pch_can_priv *priv) |
| 473 | { |
| 474 | /* write to sw reset register */ |
| 475 | iowrite32(1, &priv->regs->srst); |
| 476 | iowrite32(0, &priv->regs->srst); |
| 477 | } |
| 478 | |
| 479 | static void pch_can_error(struct net_device *ndev, u32 status) |
| 480 | { |
| 481 | struct sk_buff *skb; |
| 482 | struct pch_can_priv *priv = netdev_priv(ndev); |
| 483 | struct can_frame *cf; |
Tomoya | d68f683 | 2010-11-29 18:16:15 +0000 | [diff] [blame] | 484 | u32 errc, lec; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 485 | struct net_device_stats *stats = &(priv->ndev->stats); |
| 486 | enum can_state state = priv->can.state; |
| 487 | |
| 488 | skb = alloc_can_err_skb(ndev, &cf); |
| 489 | if (!skb) |
| 490 | return; |
| 491 | |
| 492 | if (status & PCH_BUS_OFF) { |
Tomoya | 8339a7e | 2010-11-29 18:11:52 +0000 | [diff] [blame] | 493 | pch_can_set_tx_all(priv, 0); |
| 494 | pch_can_set_rx_all(priv, 0); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 495 | state = CAN_STATE_BUS_OFF; |
| 496 | cf->can_id |= CAN_ERR_BUSOFF; |
Andri Yngvason | be38a6f | 2015-01-16 14:30:28 +0000 | [diff] [blame] | 497 | priv->can.can_stats.bus_off++; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 498 | can_bus_off(ndev); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Tomoya | 44c9aa8 | 2010-12-12 20:24:14 +0000 | [diff] [blame] | 501 | errc = ioread32(&priv->regs->errc); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 502 | /* Warning interrupt. */ |
| 503 | if (status & PCH_EWARN) { |
| 504 | state = CAN_STATE_ERROR_WARNING; |
| 505 | priv->can.can_stats.error_warning++; |
| 506 | cf->can_id |= CAN_ERR_CRTL; |
Tomoya | 086b565 | 2010-11-17 01:13:16 +0000 | [diff] [blame] | 507 | if (((errc & PCH_REC) >> 8) > 96) |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 508 | cf->data[1] |= CAN_ERR_CRTL_RX_WARNING; |
Tomoya | 086b565 | 2010-11-17 01:13:16 +0000 | [diff] [blame] | 509 | if ((errc & PCH_TEC) > 96) |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 510 | cf->data[1] |= CAN_ERR_CRTL_TX_WARNING; |
Tomoya | 435b4ef | 2010-12-12 20:24:16 +0000 | [diff] [blame] | 511 | netdev_dbg(ndev, |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 512 | "%s -> Error Counter is more than 96.\n", __func__); |
| 513 | } |
| 514 | /* Error passive interrupt. */ |
| 515 | if (status & PCH_EPASSIV) { |
| 516 | priv->can.can_stats.error_passive++; |
| 517 | state = CAN_STATE_ERROR_PASSIVE; |
| 518 | cf->can_id |= CAN_ERR_CRTL; |
Xi Wang | 44b0052c | 2011-12-12 02:16:20 -0500 | [diff] [blame] | 519 | if (errc & PCH_RP) |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 520 | cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE; |
Tomoya | 086b565 | 2010-11-17 01:13:16 +0000 | [diff] [blame] | 521 | if ((errc & PCH_TEC) > 127) |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 522 | cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE; |
Tomoya | 435b4ef | 2010-12-12 20:24:16 +0000 | [diff] [blame] | 523 | netdev_dbg(ndev, |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 524 | "%s -> CAN controller is ERROR PASSIVE .\n", __func__); |
| 525 | } |
| 526 | |
Tomoya | d68f683 | 2010-11-29 18:16:15 +0000 | [diff] [blame] | 527 | lec = status & PCH_LEC_ALL; |
| 528 | switch (lec) { |
| 529 | case PCH_STUF_ERR: |
| 530 | cf->data[2] |= CAN_ERR_PROT_STUFF; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 531 | priv->can.can_stats.bus_error++; |
| 532 | stats->rx_errors++; |
Tomoya | d68f683 | 2010-11-29 18:16:15 +0000 | [diff] [blame] | 533 | break; |
| 534 | case PCH_FORM_ERR: |
| 535 | cf->data[2] |= CAN_ERR_PROT_FORM; |
| 536 | priv->can.can_stats.bus_error++; |
| 537 | stats->rx_errors++; |
| 538 | break; |
| 539 | case PCH_ACK_ERR: |
| 540 | cf->can_id |= CAN_ERR_ACK; |
| 541 | priv->can.can_stats.bus_error++; |
| 542 | stats->rx_errors++; |
| 543 | break; |
| 544 | case PCH_BIT1_ERR: |
| 545 | case PCH_BIT0_ERR: |
| 546 | cf->data[2] |= CAN_ERR_PROT_BIT; |
| 547 | priv->can.can_stats.bus_error++; |
| 548 | stats->rx_errors++; |
| 549 | break; |
| 550 | case PCH_CRC_ERR: |
Oliver Hartkopp | ffd461f | 2015-11-21 18:41:20 +0100 | [diff] [blame] | 551 | cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ; |
Tomoya | d68f683 | 2010-11-29 18:16:15 +0000 | [diff] [blame] | 552 | priv->can.can_stats.bus_error++; |
| 553 | stats->rx_errors++; |
| 554 | break; |
| 555 | case PCH_LEC_ALL: /* Written by CPU. No error status */ |
| 556 | break; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Tomoya | 0c78ab7 | 2010-12-12 20:24:25 +0000 | [diff] [blame] | 559 | cf->data[6] = errc & PCH_TEC; |
| 560 | cf->data[7] = (errc & PCH_REC) >> 8; |
| 561 | |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 562 | priv->can.state = state; |
Tomoya | cfb7e5f | 2010-12-12 20:24:26 +0000 | [diff] [blame] | 563 | netif_receive_skb(skb); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | static irqreturn_t pch_can_interrupt(int irq, void *dev_id) |
| 567 | { |
| 568 | struct net_device *ndev = (struct net_device *)dev_id; |
| 569 | struct pch_can_priv *priv = netdev_priv(ndev); |
| 570 | |
Tomoya | 3332bc5 | 2010-12-12 20:24:23 +0000 | [diff] [blame] | 571 | if (!pch_can_int_pending(priv)) |
| 572 | return IRQ_NONE; |
| 573 | |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 574 | pch_can_set_int_enables(priv, PCH_CAN_NONE); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 575 | napi_schedule(&priv->napi); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 576 | return IRQ_HANDLED; |
| 577 | } |
| 578 | |
Tomoya | 1d5b4b2 | 2010-12-12 20:24:10 +0000 | [diff] [blame] | 579 | static void pch_fifo_thresh(struct pch_can_priv *priv, int obj_id) |
| 580 | { |
| 581 | if (obj_id < PCH_FIFO_THRESH) { |
| 582 | iowrite32(PCH_CMASK_RDWR | PCH_CMASK_CTRL | |
| 583 | PCH_CMASK_ARB, &priv->regs->ifregs[0].cmask); |
| 584 | |
| 585 | /* Clearing the Dir bit. */ |
| 586 | pch_can_bit_clear(&priv->regs->ifregs[0].id2, PCH_ID2_DIR); |
| 587 | |
| 588 | /* Clearing NewDat & IntPnd */ |
| 589 | pch_can_bit_clear(&priv->regs->ifregs[0].mcont, |
| 590 | PCH_IF_MCONT_INTPND); |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 591 | pch_can_rw_msg_obj(&priv->regs->ifregs[0].creq, obj_id); |
Tomoya | 1d5b4b2 | 2010-12-12 20:24:10 +0000 | [diff] [blame] | 592 | } else if (obj_id > PCH_FIFO_THRESH) { |
| 593 | pch_can_int_clr(priv, obj_id); |
| 594 | } else if (obj_id == PCH_FIFO_THRESH) { |
| 595 | int cnt; |
| 596 | for (cnt = 0; cnt < PCH_FIFO_THRESH; cnt++) |
| 597 | pch_can_int_clr(priv, cnt + 1); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | static void pch_can_rx_msg_lost(struct net_device *ndev, int obj_id) |
| 602 | { |
| 603 | struct pch_can_priv *priv = netdev_priv(ndev); |
| 604 | struct net_device_stats *stats = &(priv->ndev->stats); |
| 605 | struct sk_buff *skb; |
| 606 | struct can_frame *cf; |
| 607 | |
| 608 | netdev_dbg(priv->ndev, "Msg Obj is overwritten.\n"); |
| 609 | pch_can_bit_clear(&priv->regs->ifregs[0].mcont, |
| 610 | PCH_IF_MCONT_MSGLOST); |
| 611 | iowrite32(PCH_CMASK_RDWR | PCH_CMASK_CTRL, |
| 612 | &priv->regs->ifregs[0].cmask); |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 613 | pch_can_rw_msg_obj(&priv->regs->ifregs[0].creq, obj_id); |
Tomoya | 1d5b4b2 | 2010-12-12 20:24:10 +0000 | [diff] [blame] | 614 | |
| 615 | skb = alloc_can_err_skb(ndev, &cf); |
| 616 | if (!skb) |
| 617 | return; |
| 618 | |
| 619 | cf->can_id |= CAN_ERR_CRTL; |
| 620 | cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; |
| 621 | stats->rx_over_errors++; |
| 622 | stats->rx_errors++; |
| 623 | |
| 624 | netif_receive_skb(skb); |
| 625 | } |
| 626 | |
| 627 | static int pch_can_rx_normal(struct net_device *ndev, u32 obj_num, int quota) |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 628 | { |
| 629 | u32 reg; |
| 630 | canid_t id; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 631 | int rcv_pkts = 0; |
| 632 | struct sk_buff *skb; |
| 633 | struct can_frame *cf; |
| 634 | struct pch_can_priv *priv = netdev_priv(ndev); |
| 635 | struct net_device_stats *stats = &(priv->ndev->stats); |
Tomoya | 1d5b4b2 | 2010-12-12 20:24:10 +0000 | [diff] [blame] | 636 | int i; |
| 637 | u32 id2; |
Tomoya | 8ac9702 | 2010-12-12 20:24:09 +0000 | [diff] [blame] | 638 | u16 data_reg; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 639 | |
Tomoya | 1d5b4b2 | 2010-12-12 20:24:10 +0000 | [diff] [blame] | 640 | do { |
Justin P. Mattock | 70f23fd | 2011-05-10 10:16:21 +0200 | [diff] [blame] | 641 | /* Reading the message object from the Message RAM */ |
Tomoya | 1d5b4b2 | 2010-12-12 20:24:10 +0000 | [diff] [blame] | 642 | iowrite32(PCH_CMASK_RX_TX_GET, &priv->regs->ifregs[0].cmask); |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 643 | pch_can_rw_msg_obj(&priv->regs->ifregs[0].creq, obj_num); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 644 | |
Tomoya | 1d5b4b2 | 2010-12-12 20:24:10 +0000 | [diff] [blame] | 645 | /* Reading the MCONT register. */ |
| 646 | reg = ioread32(&priv->regs->ifregs[0].mcont); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 647 | |
Tomoya | 1d5b4b2 | 2010-12-12 20:24:10 +0000 | [diff] [blame] | 648 | if (reg & PCH_IF_MCONT_EOB) |
| 649 | break; |
| 650 | |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 651 | /* If MsgLost bit set. */ |
Tomoya | 086b565 | 2010-11-17 01:13:16 +0000 | [diff] [blame] | 652 | if (reg & PCH_IF_MCONT_MSGLOST) { |
Tomoya | 1d5b4b2 | 2010-12-12 20:24:10 +0000 | [diff] [blame] | 653 | pch_can_rx_msg_lost(ndev, obj_num); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 654 | rcv_pkts++; |
Tomoya | 1d5b4b2 | 2010-12-12 20:24:10 +0000 | [diff] [blame] | 655 | quota--; |
| 656 | obj_num++; |
| 657 | continue; |
| 658 | } else if (!(reg & PCH_IF_MCONT_NEWDAT)) { |
| 659 | obj_num++; |
| 660 | continue; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 661 | } |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 662 | |
| 663 | skb = alloc_can_skb(priv->ndev, &cf); |
Tomoya | 3332bc5 | 2010-12-12 20:24:23 +0000 | [diff] [blame] | 664 | if (!skb) { |
| 665 | netdev_err(ndev, "alloc_can_skb Failed\n"); |
| 666 | return rcv_pkts; |
| 667 | } |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 668 | |
| 669 | /* Get Received data */ |
Tomoya | 1d5b4b2 | 2010-12-12 20:24:10 +0000 | [diff] [blame] | 670 | id2 = ioread32(&priv->regs->ifregs[0].id2); |
| 671 | if (id2 & PCH_ID2_XTD) { |
Tomoya | 8339a7e | 2010-11-29 18:11:52 +0000 | [diff] [blame] | 672 | id = (ioread32(&priv->regs->ifregs[0].id1) & 0xffff); |
Tomoya | 1d5b4b2 | 2010-12-12 20:24:10 +0000 | [diff] [blame] | 673 | id |= (((id2) & 0x1fff) << 16); |
| 674 | cf->can_id = id | CAN_EFF_FLAG; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 675 | } else { |
Tomoya | 1d5b4b2 | 2010-12-12 20:24:10 +0000 | [diff] [blame] | 676 | id = (id2 >> 2) & CAN_SFF_MASK; |
| 677 | cf->can_id = id; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Oliver Hartkopp | c7b7496 | 2020-11-20 11:04:44 +0100 | [diff] [blame] | 680 | cf->len = can_cc_dlc2len((ioread32(&priv->regs-> |
Tomoya | 1d5b4b2 | 2010-12-12 20:24:10 +0000 | [diff] [blame] | 681 | ifregs[0].mcont)) & 0xF); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 682 | |
Vincent Mailhol | f68eafe | 2021-12-07 21:15:29 +0900 | [diff] [blame] | 683 | if (id2 & PCH_ID2_DIR) { |
| 684 | cf->can_id |= CAN_RTR_FLAG; |
| 685 | } else { |
| 686 | for (i = 0; i < cf->len; i += 2) { |
| 687 | data_reg = ioread16(&priv->regs->ifregs[0].data[i / 2]); |
| 688 | cf->data[i] = data_reg; |
| 689 | cf->data[i + 1] = data_reg >> 8; |
| 690 | } |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 691 | |
Vincent Mailhol | 8e674ca | 2021-12-07 21:15:30 +0900 | [diff] [blame] | 692 | stats->rx_bytes += cf->len; |
| 693 | } |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 694 | stats->rx_packets++; |
Vincent Mailhol | 8e674ca | 2021-12-07 21:15:30 +0900 | [diff] [blame] | 695 | rcv_pkts++; |
Tomoya | 1d5b4b2 | 2010-12-12 20:24:10 +0000 | [diff] [blame] | 696 | quota--; |
Vincent Mailhol | 94cddf1 | 2021-11-23 20:16:54 +0900 | [diff] [blame] | 697 | netif_receive_skb(skb); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 698 | |
Tomoya | 1d5b4b2 | 2010-12-12 20:24:10 +0000 | [diff] [blame] | 699 | pch_fifo_thresh(priv, obj_num); |
| 700 | obj_num++; |
| 701 | } while (quota > 0); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 702 | |
| 703 | return rcv_pkts; |
| 704 | } |
Tomoya | e489cce | 2010-12-12 20:24:08 +0000 | [diff] [blame] | 705 | |
| 706 | static void pch_can_tx_complete(struct net_device *ndev, u32 int_stat) |
| 707 | { |
| 708 | struct pch_can_priv *priv = netdev_priv(ndev); |
| 709 | struct net_device_stats *stats = &(priv->ndev->stats); |
Tomoya | e489cce | 2010-12-12 20:24:08 +0000 | [diff] [blame] | 710 | |
Vincent Mailhol | cc4b08c | 2021-12-07 21:15:31 +0900 | [diff] [blame] | 711 | stats->tx_bytes += can_get_echo_skb(ndev, int_stat - PCH_RX_OBJ_END - 1, |
| 712 | NULL); |
| 713 | stats->tx_packets++; |
Tomoya | e489cce | 2010-12-12 20:24:08 +0000 | [diff] [blame] | 714 | iowrite32(PCH_CMASK_RX_TX_GET | PCH_CMASK_CLRINTPND, |
| 715 | &priv->regs->ifregs[1].cmask); |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 716 | pch_can_rw_msg_obj(&priv->regs->ifregs[1].creq, int_stat); |
Tomoya | e489cce | 2010-12-12 20:24:08 +0000 | [diff] [blame] | 717 | if (int_stat == PCH_TX_OBJ_END) |
| 718 | netif_wake_queue(ndev); |
| 719 | } |
| 720 | |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 721 | static int pch_can_poll(struct napi_struct *napi, int quota) |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 722 | { |
| 723 | struct net_device *ndev = napi->dev; |
| 724 | struct pch_can_priv *priv = netdev_priv(ndev); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 725 | u32 int_stat; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 726 | u32 reg_stat; |
Tomoya | 3332bc5 | 2010-12-12 20:24:23 +0000 | [diff] [blame] | 727 | int quota_save = quota; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 728 | |
| 729 | int_stat = pch_can_int_pending(priv); |
| 730 | if (!int_stat) |
Tomoya | e489cce | 2010-12-12 20:24:08 +0000 | [diff] [blame] | 731 | goto end; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 732 | |
Tomoya | 8714fca | 2010-12-12 20:24:18 +0000 | [diff] [blame] | 733 | if (int_stat == PCH_STATUS_INT) { |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 734 | reg_stat = ioread32(&priv->regs->stat); |
Tomoya | fea9294 | 2010-12-12 20:24:24 +0000 | [diff] [blame] | 735 | |
| 736 | if ((reg_stat & (PCH_BUS_OFF | PCH_LEC_ALL)) && |
| 737 | ((reg_stat & PCH_LEC_ALL) != PCH_LEC_ALL)) { |
| 738 | pch_can_error(ndev, reg_stat); |
| 739 | quota--; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Tomoya | fea9294 | 2010-12-12 20:24:24 +0000 | [diff] [blame] | 742 | if (reg_stat & (PCH_TX_OK | PCH_RX_OK)) |
| 743 | pch_can_bit_clear(&priv->regs->stat, |
| 744 | reg_stat & (PCH_TX_OK | PCH_RX_OK)); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 745 | |
| 746 | int_stat = pch_can_int_pending(priv); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Tomoya | e489cce | 2010-12-12 20:24:08 +0000 | [diff] [blame] | 749 | if (quota == 0) |
| 750 | goto end; |
| 751 | |
Tomoya | 15ffc8f | 2010-11-29 18:15:02 +0000 | [diff] [blame] | 752 | if ((int_stat >= PCH_RX_OBJ_START) && (int_stat <= PCH_RX_OBJ_END)) { |
Tomoya | 3332bc5 | 2010-12-12 20:24:23 +0000 | [diff] [blame] | 753 | quota -= pch_can_rx_normal(ndev, int_stat, quota); |
Tomoya | 15ffc8f | 2010-11-29 18:15:02 +0000 | [diff] [blame] | 754 | } else if ((int_stat >= PCH_TX_OBJ_START) && |
| 755 | (int_stat <= PCH_TX_OBJ_END)) { |
| 756 | /* Handle transmission interrupt */ |
Tomoya | e489cce | 2010-12-12 20:24:08 +0000 | [diff] [blame] | 757 | pch_can_tx_complete(ndev, int_stat); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Tomoya | e489cce | 2010-12-12 20:24:08 +0000 | [diff] [blame] | 760 | end: |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 761 | napi_complete(napi); |
| 762 | pch_can_set_int_enables(priv, PCH_CAN_ALL); |
| 763 | |
Tomoya | 3332bc5 | 2010-12-12 20:24:23 +0000 | [diff] [blame] | 764 | return quota_save - quota; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | static int pch_set_bittiming(struct net_device *ndev) |
| 768 | { |
| 769 | struct pch_can_priv *priv = netdev_priv(ndev); |
| 770 | const struct can_bittiming *bt = &priv->can.bittiming; |
| 771 | u32 canbit; |
| 772 | u32 bepe; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 773 | |
| 774 | /* Setting the CCE bit for accessing the Can Timing register. */ |
Tomoya | 086b565 | 2010-11-17 01:13:16 +0000 | [diff] [blame] | 775 | pch_can_bit_set(&priv->regs->cont, PCH_CTRL_CCE); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 776 | |
Tomoya | 0e0805c | 2010-12-12 20:24:19 +0000 | [diff] [blame] | 777 | canbit = (bt->brp - 1) & PCH_MSK_BITT_BRP; |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 778 | canbit |= (bt->sjw - 1) << PCH_BIT_SJW_SHIFT; |
| 779 | canbit |= (bt->phase_seg1 + bt->prop_seg - 1) << PCH_BIT_TSEG1_SHIFT; |
| 780 | canbit |= (bt->phase_seg2 - 1) << PCH_BIT_TSEG2_SHIFT; |
Tomoya | 0e0805c | 2010-12-12 20:24:19 +0000 | [diff] [blame] | 781 | bepe = ((bt->brp - 1) & PCH_MSK_BRPE_BRPE) >> PCH_BIT_BRPE_BRPE_SHIFT; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 782 | iowrite32(canbit, &priv->regs->bitt); |
| 783 | iowrite32(bepe, &priv->regs->brpe); |
Tomoya | 086b565 | 2010-11-17 01:13:16 +0000 | [diff] [blame] | 784 | pch_can_bit_clear(&priv->regs->cont, PCH_CTRL_CCE); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 785 | |
| 786 | return 0; |
| 787 | } |
| 788 | |
| 789 | static void pch_can_start(struct net_device *ndev) |
| 790 | { |
| 791 | struct pch_can_priv *priv = netdev_priv(ndev); |
| 792 | |
| 793 | if (priv->can.state != CAN_STATE_STOPPED) |
| 794 | pch_can_reset(priv); |
| 795 | |
| 796 | pch_set_bittiming(ndev); |
| 797 | pch_can_set_optmode(priv); |
| 798 | |
Tomoya | 8339a7e | 2010-11-29 18:11:52 +0000 | [diff] [blame] | 799 | pch_can_set_tx_all(priv, 1); |
| 800 | pch_can_set_rx_all(priv, 1); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 801 | |
| 802 | /* Setting the CAN to run mode. */ |
| 803 | pch_can_set_run_mode(priv, PCH_CAN_RUN); |
| 804 | |
| 805 | priv->can.state = CAN_STATE_ERROR_ACTIVE; |
| 806 | |
| 807 | return; |
| 808 | } |
| 809 | |
| 810 | static int pch_can_do_set_mode(struct net_device *ndev, enum can_mode mode) |
| 811 | { |
| 812 | int ret = 0; |
| 813 | |
| 814 | switch (mode) { |
| 815 | case CAN_MODE_START: |
| 816 | pch_can_start(ndev); |
| 817 | netif_wake_queue(ndev); |
| 818 | break; |
| 819 | default: |
| 820 | ret = -EOPNOTSUPP; |
| 821 | break; |
| 822 | } |
| 823 | |
| 824 | return ret; |
| 825 | } |
| 826 | |
| 827 | static int pch_can_open(struct net_device *ndev) |
| 828 | { |
| 829 | struct pch_can_priv *priv = netdev_priv(ndev); |
| 830 | int retval; |
| 831 | |
Marc Kleine-Budde | 88bfb9a | 2020-09-16 00:34:55 +0200 | [diff] [blame] | 832 | /* Registering the interrupt. */ |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 833 | retval = request_irq(priv->dev->irq, pch_can_interrupt, IRQF_SHARED, |
| 834 | ndev->name, ndev); |
| 835 | if (retval) { |
Tomoya | 435b4ef | 2010-12-12 20:24:16 +0000 | [diff] [blame] | 836 | netdev_err(ndev, "request_irq failed.\n"); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 837 | goto req_irq_err; |
| 838 | } |
| 839 | |
| 840 | /* Open common can device */ |
| 841 | retval = open_candev(ndev); |
| 842 | if (retval) { |
Tomoya | 435b4ef | 2010-12-12 20:24:16 +0000 | [diff] [blame] | 843 | netdev_err(ndev, "open_candev() failed %d\n", retval); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 844 | goto err_open_candev; |
| 845 | } |
| 846 | |
| 847 | pch_can_init(priv); |
| 848 | pch_can_start(ndev); |
| 849 | napi_enable(&priv->napi); |
| 850 | netif_start_queue(ndev); |
| 851 | |
| 852 | return 0; |
| 853 | |
| 854 | err_open_candev: |
| 855 | free_irq(priv->dev->irq, ndev); |
| 856 | req_irq_err: |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 857 | pch_can_release(priv); |
| 858 | |
| 859 | return retval; |
| 860 | } |
| 861 | |
| 862 | static int pch_close(struct net_device *ndev) |
| 863 | { |
| 864 | struct pch_can_priv *priv = netdev_priv(ndev); |
| 865 | |
| 866 | netif_stop_queue(ndev); |
| 867 | napi_disable(&priv->napi); |
| 868 | pch_can_release(priv); |
| 869 | free_irq(priv->dev->irq, ndev); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 870 | close_candev(ndev); |
| 871 | priv->can.state = CAN_STATE_STOPPED; |
| 872 | return 0; |
| 873 | } |
| 874 | |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 875 | static netdev_tx_t pch_xmit(struct sk_buff *skb, struct net_device *ndev) |
| 876 | { |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 877 | struct pch_can_priv *priv = netdev_priv(ndev); |
| 878 | struct can_frame *cf = (struct can_frame *)skb->data; |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 879 | int tx_obj_no; |
Tomoya | 8ac9702 | 2010-12-12 20:24:09 +0000 | [diff] [blame] | 880 | int i; |
Tomoya | 44c9aa8 | 2010-12-12 20:24:14 +0000 | [diff] [blame] | 881 | u32 id2; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 882 | |
| 883 | if (can_dropped_invalid_skb(ndev, skb)) |
| 884 | return NETDEV_TX_OK; |
| 885 | |
Tomoya | fea9294 | 2010-12-12 20:24:24 +0000 | [diff] [blame] | 886 | tx_obj_no = priv->tx_obj; |
Tomoya | 76d94b2 | 2010-12-12 20:24:07 +0000 | [diff] [blame] | 887 | if (priv->tx_obj == PCH_TX_OBJ_END) { |
| 888 | if (ioread32(&priv->regs->treq2) & PCH_TREQ2_TX_MASK) |
| 889 | netif_stop_queue(ndev); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 890 | |
Tomoya | 76d94b2 | 2010-12-12 20:24:07 +0000 | [diff] [blame] | 891 | priv->tx_obj = PCH_TX_OBJ_START; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 892 | } else { |
Tomoya | 76d94b2 | 2010-12-12 20:24:07 +0000 | [diff] [blame] | 893 | priv->tx_obj++; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 894 | } |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 895 | |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 896 | /* Setting the CMASK register. */ |
Tomoya | 8339a7e | 2010-11-29 18:11:52 +0000 | [diff] [blame] | 897 | pch_can_bit_set(&priv->regs->ifregs[1].cmask, PCH_CMASK_ALL); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 898 | |
| 899 | /* If ID extended is set. */ |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 900 | if (cf->can_id & CAN_EFF_FLAG) { |
Tomoya | 44c9aa8 | 2010-12-12 20:24:14 +0000 | [diff] [blame] | 901 | iowrite32(cf->can_id & 0xffff, &priv->regs->ifregs[1].id1); |
| 902 | id2 = ((cf->can_id >> 16) & 0x1fff) | PCH_ID2_XTD; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 903 | } else { |
Tomoya | 44c9aa8 | 2010-12-12 20:24:14 +0000 | [diff] [blame] | 904 | iowrite32(0, &priv->regs->ifregs[1].id1); |
| 905 | id2 = (cf->can_id & CAN_SFF_MASK) << 2; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 906 | } |
| 907 | |
Tomoya | 44c9aa8 | 2010-12-12 20:24:14 +0000 | [diff] [blame] | 908 | id2 |= PCH_ID_MSGVAL; |
| 909 | |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 910 | /* If remote frame has to be transmitted.. */ |
Tomoya | fea9294 | 2010-12-12 20:24:24 +0000 | [diff] [blame] | 911 | if (!(cf->can_id & CAN_RTR_FLAG)) |
Tomoya | 44c9aa8 | 2010-12-12 20:24:14 +0000 | [diff] [blame] | 912 | id2 |= PCH_ID2_DIR; |
| 913 | |
| 914 | iowrite32(id2, &priv->regs->ifregs[1].id2); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 915 | |
Tomoya | 8ac9702 | 2010-12-12 20:24:09 +0000 | [diff] [blame] | 916 | /* Copy data to register */ |
Oliver Hartkopp | c7b7496 | 2020-11-20 11:04:44 +0100 | [diff] [blame] | 917 | for (i = 0; i < cf->len; i += 2) { |
Tomoya | 8ac9702 | 2010-12-12 20:24:09 +0000 | [diff] [blame] | 918 | iowrite16(cf->data[i] | (cf->data[i + 1] << 8), |
| 919 | &priv->regs->ifregs[1].data[i / 2]); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Vincent Mailhol | 1dcb6e5 | 2021-01-11 15:19:27 +0100 | [diff] [blame] | 922 | can_put_echo_skb(skb, ndev, tx_obj_no - PCH_RX_OBJ_END - 1, 0); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 923 | |
Tomoya | c755145 | 2010-12-12 20:24:21 +0000 | [diff] [blame] | 924 | /* Set the size of the data. Update if2_mcont */ |
Oliver Hartkopp | c7b7496 | 2020-11-20 11:04:44 +0100 | [diff] [blame] | 925 | iowrite32(cf->len | PCH_IF_MCONT_NEWDAT | PCH_IF_MCONT_TXRQXT | |
Tomoya | 44c9aa8 | 2010-12-12 20:24:14 +0000 | [diff] [blame] | 926 | PCH_IF_MCONT_TXIE, &priv->regs->ifregs[1].mcont); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 927 | |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 928 | pch_can_rw_msg_obj(&priv->regs->ifregs[1].creq, tx_obj_no); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 929 | |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 930 | return NETDEV_TX_OK; |
| 931 | } |
| 932 | |
| 933 | static const struct net_device_ops pch_can_netdev_ops = { |
| 934 | .ndo_open = pch_can_open, |
| 935 | .ndo_stop = pch_close, |
| 936 | .ndo_start_xmit = pch_xmit, |
Oliver Hartkopp | c971fa2 | 2014-03-07 09:23:41 +0100 | [diff] [blame] | 937 | .ndo_change_mtu = can_change_mtu, |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 938 | }; |
| 939 | |
Bill Pemberton | 3c8ac0f | 2012-12-03 09:22:44 -0500 | [diff] [blame] | 940 | static void pch_can_remove(struct pci_dev *pdev) |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 941 | { |
| 942 | struct net_device *ndev = pci_get_drvdata(pdev); |
| 943 | struct pch_can_priv *priv = netdev_priv(ndev); |
| 944 | |
| 945 | unregister_candev(priv->ndev); |
Tomoya | a6f6d6b | 2010-12-12 20:24:22 +0000 | [diff] [blame] | 946 | if (priv->use_msi) |
| 947 | pci_disable_msi(priv->dev); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 948 | pci_release_regions(pdev); |
| 949 | pci_disable_device(pdev); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 950 | pch_can_reset(priv); |
Tomoya | ce9736d | 2011-02-07 23:29:02 +0000 | [diff] [blame] | 951 | pci_iounmap(pdev, priv->regs); |
Tomoya | a6f6d6b | 2010-12-12 20:24:22 +0000 | [diff] [blame] | 952 | free_candev(priv->ndev); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 953 | } |
| 954 | |
Vaibhav Gupta | 306df0b0 | 2020-07-28 14:27:57 +0530 | [diff] [blame] | 955 | static void __maybe_unused pch_can_set_int_custom(struct pch_can_priv *priv) |
Tomoya | 7f2bc50 | 2010-12-12 20:24:11 +0000 | [diff] [blame] | 956 | { |
| 957 | /* Clearing the IE, SIE and EIE bits of Can control register. */ |
| 958 | pch_can_bit_clear(&priv->regs->cont, PCH_CTRL_IE_SIE_EIE); |
| 959 | |
| 960 | /* Appropriately setting them. */ |
| 961 | pch_can_bit_set(&priv->regs->cont, |
| 962 | ((priv->int_enables & PCH_MSK_CTRL_IE_SIE_EIE) << 1)); |
| 963 | } |
| 964 | |
| 965 | /* This function retrieves interrupt enabled for the CAN device. */ |
Vaibhav Gupta | 306df0b0 | 2020-07-28 14:27:57 +0530 | [diff] [blame] | 966 | static u32 __maybe_unused pch_can_get_int_enables(struct pch_can_priv *priv) |
Tomoya | 7f2bc50 | 2010-12-12 20:24:11 +0000 | [diff] [blame] | 967 | { |
| 968 | /* Obtaining the status of IE, SIE and EIE interrupt bits. */ |
Tomoya | ca2b004 | 2010-12-12 20:24:13 +0000 | [diff] [blame] | 969 | return (ioread32(&priv->regs->cont) & PCH_CTRL_IE_SIE_EIE) >> 1; |
Tomoya | 7f2bc50 | 2010-12-12 20:24:11 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Vaibhav Gupta | 306df0b0 | 2020-07-28 14:27:57 +0530 | [diff] [blame] | 972 | static u32 __maybe_unused pch_can_get_rxtx_ir(struct pch_can_priv *priv, |
| 973 | u32 buff_num, enum pch_ifreg dir) |
Tomoya | 7f2bc50 | 2010-12-12 20:24:11 +0000 | [diff] [blame] | 974 | { |
| 975 | u32 ie, enable; |
| 976 | |
| 977 | if (dir) |
| 978 | ie = PCH_IF_MCONT_RXIE; |
| 979 | else |
| 980 | ie = PCH_IF_MCONT_TXIE; |
| 981 | |
| 982 | iowrite32(PCH_CMASK_RX_TX_GET, &priv->regs->ifregs[dir].cmask); |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 983 | pch_can_rw_msg_obj(&priv->regs->ifregs[dir].creq, buff_num); |
Tomoya | 7f2bc50 | 2010-12-12 20:24:11 +0000 | [diff] [blame] | 984 | |
| 985 | if (((ioread32(&priv->regs->ifregs[dir].id2)) & PCH_ID_MSGVAL) && |
Tomoya | 9388b16 | 2010-12-12 20:24:17 +0000 | [diff] [blame] | 986 | ((ioread32(&priv->regs->ifregs[dir].mcont)) & ie)) |
Tomoya | 7f2bc50 | 2010-12-12 20:24:11 +0000 | [diff] [blame] | 987 | enable = 1; |
Tomoya | 9388b16 | 2010-12-12 20:24:17 +0000 | [diff] [blame] | 988 | else |
Tomoya | 7f2bc50 | 2010-12-12 20:24:11 +0000 | [diff] [blame] | 989 | enable = 0; |
Tomoya | 9388b16 | 2010-12-12 20:24:17 +0000 | [diff] [blame] | 990 | |
Tomoya | 7f2bc50 | 2010-12-12 20:24:11 +0000 | [diff] [blame] | 991 | return enable; |
| 992 | } |
| 993 | |
Vaibhav Gupta | 306df0b0 | 2020-07-28 14:27:57 +0530 | [diff] [blame] | 994 | static void __maybe_unused pch_can_set_rx_buffer_link(struct pch_can_priv *priv, |
| 995 | u32 buffer_num, int set) |
Tomoya | 7f2bc50 | 2010-12-12 20:24:11 +0000 | [diff] [blame] | 996 | { |
| 997 | iowrite32(PCH_CMASK_RX_TX_GET, &priv->regs->ifregs[0].cmask); |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 998 | pch_can_rw_msg_obj(&priv->regs->ifregs[0].creq, buffer_num); |
Tomoya | 7f2bc50 | 2010-12-12 20:24:11 +0000 | [diff] [blame] | 999 | iowrite32(PCH_CMASK_RDWR | PCH_CMASK_CTRL, |
| 1000 | &priv->regs->ifregs[0].cmask); |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 1001 | if (set) |
Tomoya | 7f2bc50 | 2010-12-12 20:24:11 +0000 | [diff] [blame] | 1002 | pch_can_bit_clear(&priv->regs->ifregs[0].mcont, |
| 1003 | PCH_IF_MCONT_EOB); |
| 1004 | else |
| 1005 | pch_can_bit_set(&priv->regs->ifregs[0].mcont, PCH_IF_MCONT_EOB); |
| 1006 | |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 1007 | pch_can_rw_msg_obj(&priv->regs->ifregs[0].creq, buffer_num); |
Tomoya | 7f2bc50 | 2010-12-12 20:24:11 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
Vaibhav Gupta | 306df0b0 | 2020-07-28 14:27:57 +0530 | [diff] [blame] | 1010 | static u32 __maybe_unused pch_can_get_rx_buffer_link(struct pch_can_priv *priv, |
| 1011 | u32 buffer_num) |
Tomoya | 7f2bc50 | 2010-12-12 20:24:11 +0000 | [diff] [blame] | 1012 | { |
Tomoya | ca2b004 | 2010-12-12 20:24:13 +0000 | [diff] [blame] | 1013 | u32 link; |
| 1014 | |
Tomoya | 7f2bc50 | 2010-12-12 20:24:11 +0000 | [diff] [blame] | 1015 | iowrite32(PCH_CMASK_RX_TX_GET, &priv->regs->ifregs[0].cmask); |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 1016 | pch_can_rw_msg_obj(&priv->regs->ifregs[0].creq, buffer_num); |
Tomoya | 7f2bc50 | 2010-12-12 20:24:11 +0000 | [diff] [blame] | 1017 | |
| 1018 | if (ioread32(&priv->regs->ifregs[0].mcont) & PCH_IF_MCONT_EOB) |
Tomoya | ca2b004 | 2010-12-12 20:24:13 +0000 | [diff] [blame] | 1019 | link = 0; |
Tomoya | 7f2bc50 | 2010-12-12 20:24:11 +0000 | [diff] [blame] | 1020 | else |
Tomoya | ca2b004 | 2010-12-12 20:24:13 +0000 | [diff] [blame] | 1021 | link = 1; |
| 1022 | return link; |
Tomoya | 7f2bc50 | 2010-12-12 20:24:11 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
Vaibhav Gupta | 306df0b0 | 2020-07-28 14:27:57 +0530 | [diff] [blame] | 1025 | static int __maybe_unused pch_can_get_buffer_status(struct pch_can_priv *priv) |
Tomoya | 7f2bc50 | 2010-12-12 20:24:11 +0000 | [diff] [blame] | 1026 | { |
| 1027 | return (ioread32(&priv->regs->treq1) & 0xffff) | |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 1028 | (ioread32(&priv->regs->treq2) << 16); |
Tomoya | 7f2bc50 | 2010-12-12 20:24:11 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
Vaibhav Gupta | 306df0b0 | 2020-07-28 14:27:57 +0530 | [diff] [blame] | 1031 | static int __maybe_unused pch_can_suspend(struct device *dev_d) |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1032 | { |
Tomoya | c755145 | 2010-12-12 20:24:21 +0000 | [diff] [blame] | 1033 | int i; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1034 | u32 buf_stat; /* Variable for reading the transmit buffer status. */ |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 1035 | int counter = PCH_COUNTER_LIMIT; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1036 | |
Vaibhav Gupta | 306df0b0 | 2020-07-28 14:27:57 +0530 | [diff] [blame] | 1037 | struct net_device *dev = dev_get_drvdata(dev_d); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1038 | struct pch_can_priv *priv = netdev_priv(dev); |
| 1039 | |
| 1040 | /* Stop the CAN controller */ |
| 1041 | pch_can_set_run_mode(priv, PCH_CAN_STOP); |
| 1042 | |
| 1043 | /* Indicate that we are aboutto/in suspend */ |
Tomoya | d06848b | 2010-12-12 20:24:20 +0000 | [diff] [blame] | 1044 | priv->can.state = CAN_STATE_STOPPED; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1045 | |
| 1046 | /* Waiting for all transmission to complete. */ |
| 1047 | while (counter) { |
| 1048 | buf_stat = pch_can_get_buffer_status(priv); |
| 1049 | if (!buf_stat) |
| 1050 | break; |
| 1051 | counter--; |
| 1052 | udelay(1); |
| 1053 | } |
| 1054 | if (!counter) |
Vaibhav Gupta | 306df0b0 | 2020-07-28 14:27:57 +0530 | [diff] [blame] | 1055 | dev_err(dev_d, "%s -> Transmission time out.\n", __func__); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1056 | |
| 1057 | /* Save interrupt configuration and then disable them */ |
Tomoya | ca2b004 | 2010-12-12 20:24:13 +0000 | [diff] [blame] | 1058 | priv->int_enables = pch_can_get_int_enables(priv); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1059 | pch_can_set_int_enables(priv, PCH_CAN_DISABLE); |
| 1060 | |
| 1061 | /* Save Tx buffer enable state */ |
Tomoya | 15ffc8f | 2010-11-29 18:15:02 +0000 | [diff] [blame] | 1062 | for (i = PCH_TX_OBJ_START; i <= PCH_TX_OBJ_END; i++) |
Tomoya | f622691c | 2010-12-22 03:00:39 +0000 | [diff] [blame] | 1063 | priv->tx_enable[i - 1] = pch_can_get_rxtx_ir(priv, i, |
| 1064 | PCH_TX_IFREG); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1065 | |
| 1066 | /* Disable all Transmit buffers */ |
Tomoya | 8339a7e | 2010-11-29 18:11:52 +0000 | [diff] [blame] | 1067 | pch_can_set_tx_all(priv, 0); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1068 | |
| 1069 | /* Save Rx buffer enable state */ |
Tomoya | 15ffc8f | 2010-11-29 18:15:02 +0000 | [diff] [blame] | 1070 | for (i = PCH_RX_OBJ_START; i <= PCH_RX_OBJ_END; i++) { |
Tomoya | f622691c | 2010-12-22 03:00:39 +0000 | [diff] [blame] | 1071 | priv->rx_enable[i - 1] = pch_can_get_rxtx_ir(priv, i, |
| 1072 | PCH_RX_IFREG); |
| 1073 | priv->rx_link[i - 1] = pch_can_get_rx_buffer_link(priv, i); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | /* Disable all Receive buffers */ |
Tomoya | 8339a7e | 2010-11-29 18:11:52 +0000 | [diff] [blame] | 1077 | pch_can_set_rx_all(priv, 0); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1078 | |
Vaibhav Gupta | 306df0b0 | 2020-07-28 14:27:57 +0530 | [diff] [blame] | 1079 | return 0; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Vaibhav Gupta | 306df0b0 | 2020-07-28 14:27:57 +0530 | [diff] [blame] | 1082 | static int __maybe_unused pch_can_resume(struct device *dev_d) |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1083 | { |
Tomoya | c755145 | 2010-12-12 20:24:21 +0000 | [diff] [blame] | 1084 | int i; |
Vaibhav Gupta | 306df0b0 | 2020-07-28 14:27:57 +0530 | [diff] [blame] | 1085 | struct net_device *dev = dev_get_drvdata(dev_d); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1086 | struct pch_can_priv *priv = netdev_priv(dev); |
| 1087 | |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1088 | priv->can.state = CAN_STATE_ERROR_ACTIVE; |
| 1089 | |
| 1090 | /* Disabling all interrupts. */ |
| 1091 | pch_can_set_int_enables(priv, PCH_CAN_DISABLE); |
| 1092 | |
| 1093 | /* Setting the CAN device in Stop Mode. */ |
| 1094 | pch_can_set_run_mode(priv, PCH_CAN_STOP); |
| 1095 | |
| 1096 | /* Configuring the transmit and receive buffers. */ |
| 1097 | pch_can_config_rx_tx_buffers(priv); |
| 1098 | |
| 1099 | /* Restore the CAN state */ |
| 1100 | pch_set_bittiming(dev); |
| 1101 | |
| 1102 | /* Listen/Active */ |
| 1103 | pch_can_set_optmode(priv); |
| 1104 | |
| 1105 | /* Enabling the transmit buffer. */ |
Tomoya | 15ffc8f | 2010-11-29 18:15:02 +0000 | [diff] [blame] | 1106 | for (i = PCH_TX_OBJ_START; i <= PCH_TX_OBJ_END; i++) |
Tomoya | f622691c | 2010-12-22 03:00:39 +0000 | [diff] [blame] | 1107 | pch_can_set_rxtx(priv, i, priv->tx_enable[i - 1], PCH_TX_IFREG); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1108 | |
| 1109 | /* Configuring the receive buffer and enabling them. */ |
Tomoya | 15ffc8f | 2010-11-29 18:15:02 +0000 | [diff] [blame] | 1110 | for (i = PCH_RX_OBJ_START; i <= PCH_RX_OBJ_END; i++) { |
| 1111 | /* Restore buffer link */ |
Tomoya | f622691c | 2010-12-22 03:00:39 +0000 | [diff] [blame] | 1112 | pch_can_set_rx_buffer_link(priv, i, priv->rx_link[i - 1]); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1113 | |
Tomoya | 15ffc8f | 2010-11-29 18:15:02 +0000 | [diff] [blame] | 1114 | /* Restore buffer enables */ |
Tomoya | f622691c | 2010-12-22 03:00:39 +0000 | [diff] [blame] | 1115 | pch_can_set_rxtx(priv, i, priv->rx_enable[i - 1], PCH_RX_IFREG); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | /* Enable CAN Interrupts */ |
| 1119 | pch_can_set_int_custom(priv); |
| 1120 | |
| 1121 | /* Restore Run Mode */ |
| 1122 | pch_can_set_run_mode(priv, PCH_CAN_RUN); |
| 1123 | |
Vaibhav Gupta | 306df0b0 | 2020-07-28 14:27:57 +0530 | [diff] [blame] | 1124 | return 0; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1125 | } |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1126 | |
| 1127 | static int pch_can_get_berr_counter(const struct net_device *dev, |
| 1128 | struct can_berr_counter *bec) |
| 1129 | { |
| 1130 | struct pch_can_priv *priv = netdev_priv(dev); |
Tomoya | 44c9aa8 | 2010-12-12 20:24:14 +0000 | [diff] [blame] | 1131 | u32 errc = ioread32(&priv->regs->errc); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1132 | |
Tomoya | 44c9aa8 | 2010-12-12 20:24:14 +0000 | [diff] [blame] | 1133 | bec->txerr = errc & PCH_TEC; |
| 1134 | bec->rxerr = (errc & PCH_REC) >> 8; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1135 | |
| 1136 | return 0; |
| 1137 | } |
| 1138 | |
Bill Pemberton | 3c8ac0f | 2012-12-03 09:22:44 -0500 | [diff] [blame] | 1139 | static int pch_can_probe(struct pci_dev *pdev, |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1140 | const struct pci_device_id *id) |
| 1141 | { |
| 1142 | struct net_device *ndev; |
| 1143 | struct pch_can_priv *priv; |
| 1144 | int rc; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1145 | void __iomem *addr; |
| 1146 | |
| 1147 | rc = pci_enable_device(pdev); |
| 1148 | if (rc) { |
| 1149 | dev_err(&pdev->dev, "Failed pci_enable_device %d\n", rc); |
| 1150 | goto probe_exit_endev; |
| 1151 | } |
| 1152 | |
| 1153 | rc = pci_request_regions(pdev, KBUILD_MODNAME); |
| 1154 | if (rc) { |
| 1155 | dev_err(&pdev->dev, "Failed pci_request_regions %d\n", rc); |
| 1156 | goto probe_exit_pcireq; |
| 1157 | } |
| 1158 | |
| 1159 | addr = pci_iomap(pdev, 1, 0); |
| 1160 | if (!addr) { |
| 1161 | rc = -EIO; |
| 1162 | dev_err(&pdev->dev, "Failed pci_iomap\n"); |
| 1163 | goto probe_exit_ipmap; |
| 1164 | } |
| 1165 | |
Tomoya | 15ffc8f | 2010-11-29 18:15:02 +0000 | [diff] [blame] | 1166 | ndev = alloc_candev(sizeof(struct pch_can_priv), PCH_TX_OBJ_END); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1167 | if (!ndev) { |
| 1168 | rc = -ENOMEM; |
| 1169 | dev_err(&pdev->dev, "Failed alloc_candev\n"); |
| 1170 | goto probe_exit_alloc_candev; |
| 1171 | } |
| 1172 | |
| 1173 | priv = netdev_priv(ndev); |
| 1174 | priv->ndev = ndev; |
| 1175 | priv->regs = addr; |
| 1176 | priv->dev = pdev; |
| 1177 | priv->can.bittiming_const = &pch_can_bittiming_const; |
| 1178 | priv->can.do_set_mode = pch_can_do_set_mode; |
| 1179 | priv->can.do_get_berr_counter = pch_can_get_berr_counter; |
| 1180 | priv->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY | |
| 1181 | CAN_CTRLMODE_LOOPBACK; |
Tomoya | 15ffc8f | 2010-11-29 18:15:02 +0000 | [diff] [blame] | 1182 | priv->tx_obj = PCH_TX_OBJ_START; /* Point head of Tx Obj */ |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1183 | |
| 1184 | ndev->irq = pdev->irq; |
| 1185 | ndev->flags |= IFF_ECHO; |
| 1186 | |
| 1187 | pci_set_drvdata(pdev, ndev); |
| 1188 | SET_NETDEV_DEV(ndev, &pdev->dev); |
| 1189 | ndev->netdev_ops = &pch_can_netdev_ops; |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1190 | priv->can.clock.freq = PCH_CAN_CLK; /* Hz */ |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1191 | |
Tomoya | bd58cbc | 2010-12-12 20:24:12 +0000 | [diff] [blame] | 1192 | netif_napi_add(ndev, &priv->napi, pch_can_poll, PCH_RX_OBJ_END); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1193 | |
Tomoya | a6f6d6b | 2010-12-12 20:24:22 +0000 | [diff] [blame] | 1194 | rc = pci_enable_msi(priv->dev); |
| 1195 | if (rc) { |
| 1196 | netdev_err(ndev, "PCH CAN opened without MSI\n"); |
| 1197 | priv->use_msi = 0; |
| 1198 | } else { |
| 1199 | netdev_err(ndev, "PCH CAN opened with MSI\n"); |
Tomoya | c69b909 | 2011-02-07 23:29:03 +0000 | [diff] [blame] | 1200 | pci_set_master(pdev); |
Tomoya | a6f6d6b | 2010-12-12 20:24:22 +0000 | [diff] [blame] | 1201 | priv->use_msi = 1; |
| 1202 | } |
| 1203 | |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1204 | rc = register_candev(ndev); |
| 1205 | if (rc) { |
| 1206 | dev_err(&pdev->dev, "Failed register_candev %d\n", rc); |
| 1207 | goto probe_exit_reg_candev; |
| 1208 | } |
| 1209 | |
| 1210 | return 0; |
| 1211 | |
| 1212 | probe_exit_reg_candev: |
Tomoya | a6f6d6b | 2010-12-12 20:24:22 +0000 | [diff] [blame] | 1213 | if (priv->use_msi) |
| 1214 | pci_disable_msi(priv->dev); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1215 | free_candev(ndev); |
| 1216 | probe_exit_alloc_candev: |
| 1217 | pci_iounmap(pdev, addr); |
| 1218 | probe_exit_ipmap: |
| 1219 | pci_release_regions(pdev); |
| 1220 | probe_exit_pcireq: |
| 1221 | pci_disable_device(pdev); |
| 1222 | probe_exit_endev: |
| 1223 | return rc; |
| 1224 | } |
| 1225 | |
Vaibhav Gupta | 306df0b0 | 2020-07-28 14:27:57 +0530 | [diff] [blame] | 1226 | static SIMPLE_DEV_PM_OPS(pch_can_pm_ops, |
| 1227 | pch_can_suspend, |
| 1228 | pch_can_resume); |
| 1229 | |
Marc Kleine-Budde | bdfa3d8 | 2010-10-30 16:28:16 -0700 | [diff] [blame] | 1230 | static struct pci_driver pch_can_pci_driver = { |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1231 | .name = "pch_can", |
| 1232 | .id_table = pch_pci_tbl, |
| 1233 | .probe = pch_can_probe, |
Bill Pemberton | 3c8ac0f | 2012-12-03 09:22:44 -0500 | [diff] [blame] | 1234 | .remove = pch_can_remove, |
Vaibhav Gupta | 306df0b0 | 2020-07-28 14:27:57 +0530 | [diff] [blame] | 1235 | .driver.pm = &pch_can_pm_ops, |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1236 | }; |
| 1237 | |
Axel Lin | fb7944b | 2012-04-14 12:38:43 +0800 | [diff] [blame] | 1238 | module_pci_driver(pch_can_pci_driver); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1239 | |
Tomoya | e91530e | 2010-12-12 20:24:15 +0000 | [diff] [blame] | 1240 | MODULE_DESCRIPTION("Intel EG20T PCH CAN(Controller Area Network) Driver"); |
Masayuki Ohtake | b21d18b | 2010-10-15 03:00:28 +0000 | [diff] [blame] | 1241 | MODULE_LICENSE("GPL v2"); |
| 1242 | MODULE_VERSION("0.94"); |