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