Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Blackfin On-Chip CAN Driver |
| 3 | * |
| 4 | * Copyright 2004-2009 Analog Devices Inc. |
| 5 | * |
| 6 | * Enter bugs at http://blackfin.uclinux.org/ |
| 7 | * |
| 8 | * Licensed under the GPL-2 or later. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 12 | #include <linux/kernel.h> |
| 13 | #include <linux/bitops.h> |
| 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/netdevice.h> |
| 17 | #include <linux/skbuff.h> |
| 18 | #include <linux/platform_device.h> |
| 19 | |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 20 | #include <linux/can/dev.h> |
| 21 | #include <linux/can/error.h> |
| 22 | |
Mike Frysinger | 21afc27 | 2010-03-21 21:06:01 +0000 | [diff] [blame] | 23 | #include <asm/bfin_can.h> |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 24 | #include <asm/portmux.h> |
| 25 | |
| 26 | #define DRV_NAME "bfin_can" |
| 27 | #define BFIN_CAN_TIMEOUT 100 |
Barry Song | e9dcd16 | 2010-03-08 12:13:57 -0800 | [diff] [blame] | 28 | #define TX_ECHO_SKB_MAX 1 |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 29 | |
| 30 | /* |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 31 | * bfin can private data |
| 32 | */ |
| 33 | struct bfin_can_priv { |
| 34 | struct can_priv can; /* must be the first member */ |
| 35 | struct net_device *dev; |
| 36 | void __iomem *membase; |
| 37 | int rx_irq; |
| 38 | int tx_irq; |
| 39 | int err_irq; |
| 40 | unsigned short *pin_list; |
| 41 | }; |
| 42 | |
| 43 | /* |
| 44 | * bfin can timing parameters |
| 45 | */ |
Marc Kleine-Budde | 194b9a4 | 2012-07-16 12:58:31 +0200 | [diff] [blame] | 46 | static const struct can_bittiming_const bfin_can_bittiming_const = { |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 47 | .name = DRV_NAME, |
| 48 | .tseg1_min = 1, |
| 49 | .tseg1_max = 16, |
| 50 | .tseg2_min = 1, |
| 51 | .tseg2_max = 8, |
| 52 | .sjw_max = 4, |
| 53 | /* |
| 54 | * Although the BRP field can be set to any value, it is recommended |
| 55 | * that the value be greater than or equal to 4, as restrictions |
| 56 | * apply to the bit timing configuration when BRP is less than 4. |
| 57 | */ |
| 58 | .brp_min = 4, |
| 59 | .brp_max = 1024, |
| 60 | .brp_inc = 1, |
| 61 | }; |
| 62 | |
| 63 | static int bfin_can_set_bittiming(struct net_device *dev) |
| 64 | { |
| 65 | struct bfin_can_priv *priv = netdev_priv(dev); |
| 66 | struct bfin_can_regs __iomem *reg = priv->membase; |
| 67 | struct can_bittiming *bt = &priv->can.bittiming; |
| 68 | u16 clk, timing; |
| 69 | |
| 70 | clk = bt->brp - 1; |
| 71 | timing = ((bt->sjw - 1) << 8) | (bt->prop_seg + bt->phase_seg1 - 1) | |
| 72 | ((bt->phase_seg2 - 1) << 4); |
| 73 | |
| 74 | /* |
| 75 | * If the SAM bit is set, the input signal is oversampled three times |
| 76 | * at the SCLK rate. |
| 77 | */ |
| 78 | if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) |
| 79 | timing |= SAM; |
| 80 | |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 81 | writew(clk, ®->clock); |
| 82 | writew(timing, ®->timing); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 83 | |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 84 | netdev_info(dev, "setting CLOCK=0x%04x TIMING=0x%04x\n", clk, timing); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static void bfin_can_set_reset_mode(struct net_device *dev) |
| 90 | { |
| 91 | struct bfin_can_priv *priv = netdev_priv(dev); |
| 92 | struct bfin_can_regs __iomem *reg = priv->membase; |
| 93 | int timeout = BFIN_CAN_TIMEOUT; |
| 94 | int i; |
| 95 | |
| 96 | /* disable interrupts */ |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 97 | writew(0, ®->mbim1); |
| 98 | writew(0, ®->mbim2); |
| 99 | writew(0, ®->gim); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 100 | |
| 101 | /* reset can and enter configuration mode */ |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 102 | writew(SRS | CCR, ®->control); |
| 103 | writew(CCR, ®->control); |
| 104 | while (!(readw(®->control) & CCA)) { |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 105 | udelay(10); |
| 106 | if (--timeout == 0) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 107 | netdev_err(dev, "fail to enter configuration mode\n"); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 108 | BUG(); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * All mailbox configurations are marked as inactive |
| 114 | * by writing to CAN Mailbox Configuration Registers 1 and 2 |
| 115 | * For all bits: 0 - Mailbox disabled, 1 - Mailbox enabled |
| 116 | */ |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 117 | writew(0, ®->mc1); |
| 118 | writew(0, ®->mc2); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 119 | |
| 120 | /* Set Mailbox Direction */ |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 121 | writew(0xFFFF, ®->md1); /* mailbox 1-16 are RX */ |
| 122 | writew(0, ®->md2); /* mailbox 17-32 are TX */ |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 123 | |
| 124 | /* RECEIVE_STD_CHL */ |
| 125 | for (i = 0; i < 2; i++) { |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 126 | writew(0, ®->chl[RECEIVE_STD_CHL + i].id0); |
| 127 | writew(AME, ®->chl[RECEIVE_STD_CHL + i].id1); |
| 128 | writew(0, ®->chl[RECEIVE_STD_CHL + i].dlc); |
| 129 | writew(0x1FFF, ®->msk[RECEIVE_STD_CHL + i].amh); |
| 130 | writew(0xFFFF, ®->msk[RECEIVE_STD_CHL + i].aml); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /* RECEIVE_EXT_CHL */ |
| 134 | for (i = 0; i < 2; i++) { |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 135 | writew(0, ®->chl[RECEIVE_EXT_CHL + i].id0); |
| 136 | writew(AME | IDE, ®->chl[RECEIVE_EXT_CHL + i].id1); |
| 137 | writew(0, ®->chl[RECEIVE_EXT_CHL + i].dlc); |
| 138 | writew(0x1FFF, ®->msk[RECEIVE_EXT_CHL + i].amh); |
| 139 | writew(0xFFFF, ®->msk[RECEIVE_EXT_CHL + i].aml); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 142 | writew(BIT(TRANSMIT_CHL - 16), ®->mc2); |
| 143 | writew(BIT(RECEIVE_STD_CHL) + BIT(RECEIVE_EXT_CHL), ®->mc1); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 144 | |
| 145 | priv->can.state = CAN_STATE_STOPPED; |
| 146 | } |
| 147 | |
| 148 | static void bfin_can_set_normal_mode(struct net_device *dev) |
| 149 | { |
| 150 | struct bfin_can_priv *priv = netdev_priv(dev); |
| 151 | struct bfin_can_regs __iomem *reg = priv->membase; |
| 152 | int timeout = BFIN_CAN_TIMEOUT; |
| 153 | |
| 154 | /* |
| 155 | * leave configuration mode |
| 156 | */ |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 157 | writew(readw(®->control) & ~CCR, ®->control); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 158 | |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 159 | while (readw(®->status) & CCA) { |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 160 | udelay(10); |
| 161 | if (--timeout == 0) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 162 | netdev_err(dev, "fail to leave configuration mode\n"); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 163 | BUG(); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /* |
| 168 | * clear _All_ tx and rx interrupts |
| 169 | */ |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 170 | writew(0xFFFF, ®->mbtif1); |
| 171 | writew(0xFFFF, ®->mbtif2); |
| 172 | writew(0xFFFF, ®->mbrif1); |
| 173 | writew(0xFFFF, ®->mbrif2); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 174 | |
| 175 | /* |
| 176 | * clear global interrupt status register |
| 177 | */ |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 178 | writew(0x7FF, ®->gis); /* overwrites with '1' */ |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 179 | |
| 180 | /* |
| 181 | * Initialize Interrupts |
| 182 | * - set bits in the mailbox interrupt mask register |
| 183 | * - global interrupt mask |
| 184 | */ |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 185 | writew(BIT(RECEIVE_STD_CHL) + BIT(RECEIVE_EXT_CHL), ®->mbim1); |
| 186 | writew(BIT(TRANSMIT_CHL - 16), ®->mbim2); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 187 | |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 188 | writew(EPIM | BOIM | RMLIM, ®->gim); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static void bfin_can_start(struct net_device *dev) |
| 192 | { |
| 193 | struct bfin_can_priv *priv = netdev_priv(dev); |
| 194 | |
| 195 | /* enter reset mode */ |
| 196 | if (priv->can.state != CAN_STATE_STOPPED) |
| 197 | bfin_can_set_reset_mode(dev); |
| 198 | |
| 199 | /* leave reset mode */ |
| 200 | bfin_can_set_normal_mode(dev); |
| 201 | } |
| 202 | |
| 203 | static int bfin_can_set_mode(struct net_device *dev, enum can_mode mode) |
| 204 | { |
| 205 | switch (mode) { |
| 206 | case CAN_MODE_START: |
| 207 | bfin_can_start(dev); |
| 208 | if (netif_queue_stopped(dev)) |
| 209 | netif_wake_queue(dev); |
| 210 | break; |
| 211 | |
| 212 | default: |
| 213 | return -EOPNOTSUPP; |
| 214 | } |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
Wolfgang Grandegger | 79d0d8a | 2012-02-01 11:14:13 +0100 | [diff] [blame] | 219 | static int bfin_can_get_berr_counter(const struct net_device *dev, |
| 220 | struct can_berr_counter *bec) |
| 221 | { |
| 222 | struct bfin_can_priv *priv = netdev_priv(dev); |
| 223 | struct bfin_can_regs __iomem *reg = priv->membase; |
| 224 | |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 225 | u16 cec = readw(®->cec); |
Wolfgang Grandegger | 79d0d8a | 2012-02-01 11:14:13 +0100 | [diff] [blame] | 226 | |
| 227 | bec->txerr = cec >> 8; |
| 228 | bec->rxerr = cec; |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 233 | static int bfin_can_start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 234 | { |
| 235 | struct bfin_can_priv *priv = netdev_priv(dev); |
| 236 | struct bfin_can_regs __iomem *reg = priv->membase; |
| 237 | struct can_frame *cf = (struct can_frame *)skb->data; |
| 238 | u8 dlc = cf->can_dlc; |
| 239 | canid_t id = cf->can_id; |
| 240 | u8 *data = cf->data; |
| 241 | u16 val; |
| 242 | int i; |
| 243 | |
Oliver Hartkopp | 3ccd4c6 | 2010-01-12 02:00:46 -0800 | [diff] [blame] | 244 | if (can_dropped_invalid_skb(dev, skb)) |
| 245 | return NETDEV_TX_OK; |
| 246 | |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 247 | netif_stop_queue(dev); |
| 248 | |
| 249 | /* fill id */ |
| 250 | if (id & CAN_EFF_FLAG) { |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 251 | writew(id, ®->chl[TRANSMIT_CHL].id0); |
Mike Frysinger | 9118f08 | 2011-06-24 04:33:01 +0000 | [diff] [blame] | 252 | val = ((id & 0x1FFF0000) >> 16) | IDE; |
| 253 | } else |
| 254 | val = (id << 2); |
| 255 | if (id & CAN_RTR_FLAG) |
| 256 | val |= RTR; |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 257 | writew(val | AME, ®->chl[TRANSMIT_CHL].id1); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 258 | |
| 259 | /* fill payload */ |
| 260 | for (i = 0; i < 8; i += 2) { |
| 261 | val = ((7 - i) < dlc ? (data[7 - i]) : 0) + |
| 262 | ((6 - i) < dlc ? (data[6 - i] << 8) : 0); |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 263 | writew(val, ®->chl[TRANSMIT_CHL].data[i]); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /* fill data length code */ |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 267 | writew(dlc, ®->chl[TRANSMIT_CHL].dlc); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 268 | |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 269 | can_put_echo_skb(skb, dev, 0); |
| 270 | |
| 271 | /* set transmit request */ |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 272 | writew(BIT(TRANSMIT_CHL - 16), ®->trs2); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | static void bfin_can_rx(struct net_device *dev, u16 isrc) |
| 278 | { |
| 279 | struct bfin_can_priv *priv = netdev_priv(dev); |
| 280 | struct net_device_stats *stats = &dev->stats; |
| 281 | struct bfin_can_regs __iomem *reg = priv->membase; |
| 282 | struct can_frame *cf; |
| 283 | struct sk_buff *skb; |
| 284 | int obj; |
| 285 | int i; |
| 286 | u16 val; |
| 287 | |
| 288 | skb = alloc_can_skb(dev, &cf); |
| 289 | if (skb == NULL) |
| 290 | return; |
| 291 | |
| 292 | /* get id */ |
| 293 | if (isrc & BIT(RECEIVE_EXT_CHL)) { |
| 294 | /* extended frame format (EFF) */ |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 295 | cf->can_id = ((readw(®->chl[RECEIVE_EXT_CHL].id1) |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 296 | & 0x1FFF) << 16) |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 297 | + readw(®->chl[RECEIVE_EXT_CHL].id0); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 298 | cf->can_id |= CAN_EFF_FLAG; |
| 299 | obj = RECEIVE_EXT_CHL; |
| 300 | } else { |
| 301 | /* standard frame format (SFF) */ |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 302 | cf->can_id = (readw(®->chl[RECEIVE_STD_CHL].id1) |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 303 | & 0x1ffc) >> 2; |
| 304 | obj = RECEIVE_STD_CHL; |
| 305 | } |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 306 | if (readw(®->chl[obj].id1) & RTR) |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 307 | cf->can_id |= CAN_RTR_FLAG; |
| 308 | |
| 309 | /* get data length code */ |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 310 | cf->can_dlc = get_can_dlc(readw(®->chl[obj].dlc) & 0xF); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 311 | |
| 312 | /* get payload */ |
| 313 | for (i = 0; i < 8; i += 2) { |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 314 | val = readw(®->chl[obj].data[i]); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 315 | cf->data[7 - i] = (7 - i) < cf->can_dlc ? val : 0; |
| 316 | cf->data[6 - i] = (6 - i) < cf->can_dlc ? (val >> 8) : 0; |
| 317 | } |
| 318 | |
| 319 | netif_rx(skb); |
| 320 | |
| 321 | stats->rx_packets++; |
| 322 | stats->rx_bytes += cf->can_dlc; |
| 323 | } |
| 324 | |
| 325 | static int bfin_can_err(struct net_device *dev, u16 isrc, u16 status) |
| 326 | { |
| 327 | struct bfin_can_priv *priv = netdev_priv(dev); |
| 328 | struct bfin_can_regs __iomem *reg = priv->membase; |
| 329 | struct net_device_stats *stats = &dev->stats; |
| 330 | struct can_frame *cf; |
| 331 | struct sk_buff *skb; |
| 332 | enum can_state state = priv->can.state; |
| 333 | |
| 334 | skb = alloc_can_err_skb(dev, &cf); |
| 335 | if (skb == NULL) |
| 336 | return -ENOMEM; |
| 337 | |
| 338 | if (isrc & RMLIS) { |
| 339 | /* data overrun interrupt */ |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 340 | netdev_dbg(dev, "data overrun interrupt\n"); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 341 | cf->can_id |= CAN_ERR_CRTL; |
| 342 | cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW; |
| 343 | stats->rx_over_errors++; |
| 344 | stats->rx_errors++; |
| 345 | } |
| 346 | |
| 347 | if (isrc & BOIS) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 348 | netdev_dbg(dev, "bus-off mode interrupt\n"); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 349 | state = CAN_STATE_BUS_OFF; |
| 350 | cf->can_id |= CAN_ERR_BUSOFF; |
Andri Yngvason | be38a6f | 2015-01-16 14:30:28 +0000 | [diff] [blame] | 351 | priv->can.can_stats.bus_off++; |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 352 | can_bus_off(dev); |
| 353 | } |
| 354 | |
| 355 | if (isrc & EPIS) { |
| 356 | /* error passive interrupt */ |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 357 | netdev_dbg(dev, "error passive interrupt\n"); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 358 | state = CAN_STATE_ERROR_PASSIVE; |
| 359 | } |
| 360 | |
| 361 | if ((isrc & EWTIS) || (isrc & EWRIS)) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 362 | netdev_dbg(dev, "Error Warning Transmit/Receive Interrupt\n"); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 363 | state = CAN_STATE_ERROR_WARNING; |
| 364 | } |
| 365 | |
| 366 | if (state != priv->can.state && (state == CAN_STATE_ERROR_WARNING || |
| 367 | state == CAN_STATE_ERROR_PASSIVE)) { |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 368 | u16 cec = readw(®->cec); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 369 | u8 rxerr = cec; |
| 370 | u8 txerr = cec >> 8; |
| 371 | |
| 372 | cf->can_id |= CAN_ERR_CRTL; |
| 373 | if (state == CAN_STATE_ERROR_WARNING) { |
| 374 | priv->can.can_stats.error_warning++; |
| 375 | cf->data[1] = (txerr > rxerr) ? |
| 376 | CAN_ERR_CRTL_TX_WARNING : |
| 377 | CAN_ERR_CRTL_RX_WARNING; |
| 378 | } else { |
| 379 | priv->can.can_stats.error_passive++; |
| 380 | cf->data[1] = (txerr > rxerr) ? |
| 381 | CAN_ERR_CRTL_TX_PASSIVE : |
| 382 | CAN_ERR_CRTL_RX_PASSIVE; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | if (status) { |
| 387 | priv->can.can_stats.bus_error++; |
| 388 | |
| 389 | cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR; |
| 390 | |
| 391 | if (status & BEF) |
| 392 | cf->data[2] |= CAN_ERR_PROT_BIT; |
| 393 | else if (status & FER) |
| 394 | cf->data[2] |= CAN_ERR_PROT_FORM; |
| 395 | else if (status & SER) |
| 396 | cf->data[2] |= CAN_ERR_PROT_STUFF; |
| 397 | else |
| 398 | cf->data[2] |= CAN_ERR_PROT_UNSPEC; |
| 399 | } |
| 400 | |
| 401 | priv->can.state = state; |
| 402 | |
| 403 | netif_rx(skb); |
| 404 | |
| 405 | stats->rx_packets++; |
| 406 | stats->rx_bytes += cf->can_dlc; |
| 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | |
Marc Kleine-Budde | 21c11bc | 2012-10-11 23:20:27 +0200 | [diff] [blame] | 411 | static irqreturn_t bfin_can_interrupt(int irq, void *dev_id) |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 412 | { |
| 413 | struct net_device *dev = dev_id; |
| 414 | struct bfin_can_priv *priv = netdev_priv(dev); |
| 415 | struct bfin_can_regs __iomem *reg = priv->membase; |
| 416 | struct net_device_stats *stats = &dev->stats; |
| 417 | u16 status, isrc; |
| 418 | |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 419 | if ((irq == priv->tx_irq) && readw(®->mbtif2)) { |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 420 | /* transmission complete interrupt */ |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 421 | writew(0xFFFF, ®->mbtif2); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 422 | stats->tx_packets++; |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 423 | stats->tx_bytes += readw(®->chl[TRANSMIT_CHL].dlc); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 424 | can_get_echo_skb(dev, 0); |
| 425 | netif_wake_queue(dev); |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 426 | } else if ((irq == priv->rx_irq) && readw(®->mbrif1)) { |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 427 | /* receive interrupt */ |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 428 | isrc = readw(®->mbrif1); |
| 429 | writew(0xFFFF, ®->mbrif1); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 430 | bfin_can_rx(dev, isrc); |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 431 | } else if ((irq == priv->err_irq) && readw(®->gis)) { |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 432 | /* error interrupt */ |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 433 | isrc = readw(®->gis); |
| 434 | status = readw(®->esr); |
| 435 | writew(0x7FF, ®->gis); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 436 | bfin_can_err(dev, isrc, status); |
| 437 | } else { |
| 438 | return IRQ_NONE; |
| 439 | } |
| 440 | |
| 441 | return IRQ_HANDLED; |
| 442 | } |
| 443 | |
| 444 | static int bfin_can_open(struct net_device *dev) |
| 445 | { |
| 446 | struct bfin_can_priv *priv = netdev_priv(dev); |
| 447 | int err; |
| 448 | |
| 449 | /* set chip into reset mode */ |
| 450 | bfin_can_set_reset_mode(dev); |
| 451 | |
| 452 | /* common open */ |
| 453 | err = open_candev(dev); |
| 454 | if (err) |
| 455 | goto exit_open; |
| 456 | |
| 457 | /* register interrupt handler */ |
| 458 | err = request_irq(priv->rx_irq, &bfin_can_interrupt, 0, |
| 459 | "bfin-can-rx", dev); |
| 460 | if (err) |
| 461 | goto exit_rx_irq; |
| 462 | err = request_irq(priv->tx_irq, &bfin_can_interrupt, 0, |
| 463 | "bfin-can-tx", dev); |
| 464 | if (err) |
| 465 | goto exit_tx_irq; |
| 466 | err = request_irq(priv->err_irq, &bfin_can_interrupt, 0, |
| 467 | "bfin-can-err", dev); |
| 468 | if (err) |
| 469 | goto exit_err_irq; |
| 470 | |
| 471 | bfin_can_start(dev); |
| 472 | |
| 473 | netif_start_queue(dev); |
| 474 | |
| 475 | return 0; |
| 476 | |
| 477 | exit_err_irq: |
| 478 | free_irq(priv->tx_irq, dev); |
| 479 | exit_tx_irq: |
| 480 | free_irq(priv->rx_irq, dev); |
| 481 | exit_rx_irq: |
| 482 | close_candev(dev); |
| 483 | exit_open: |
| 484 | return err; |
| 485 | } |
| 486 | |
| 487 | static int bfin_can_close(struct net_device *dev) |
| 488 | { |
| 489 | struct bfin_can_priv *priv = netdev_priv(dev); |
| 490 | |
| 491 | netif_stop_queue(dev); |
| 492 | bfin_can_set_reset_mode(dev); |
| 493 | |
| 494 | close_candev(dev); |
| 495 | |
| 496 | free_irq(priv->rx_irq, dev); |
| 497 | free_irq(priv->tx_irq, dev); |
| 498 | free_irq(priv->err_irq, dev); |
| 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | |
Marc Kleine-Budde | 21c11bc | 2012-10-11 23:20:27 +0200 | [diff] [blame] | 503 | static struct net_device *alloc_bfin_candev(void) |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 504 | { |
| 505 | struct net_device *dev; |
| 506 | struct bfin_can_priv *priv; |
| 507 | |
Barry Song | e9dcd16 | 2010-03-08 12:13:57 -0800 | [diff] [blame] | 508 | dev = alloc_candev(sizeof(*priv), TX_ECHO_SKB_MAX); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 509 | if (!dev) |
| 510 | return NULL; |
| 511 | |
| 512 | priv = netdev_priv(dev); |
| 513 | |
| 514 | priv->dev = dev; |
| 515 | priv->can.bittiming_const = &bfin_can_bittiming_const; |
| 516 | priv->can.do_set_bittiming = bfin_can_set_bittiming; |
| 517 | priv->can.do_set_mode = bfin_can_set_mode; |
Wolfgang Grandegger | 79d0d8a | 2012-02-01 11:14:13 +0100 | [diff] [blame] | 518 | priv->can.do_get_berr_counter = bfin_can_get_berr_counter; |
Christian Pellegrin | ad72c34 | 2010-01-14 07:08:34 +0000 | [diff] [blame] | 519 | priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES; |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 520 | |
| 521 | return dev; |
| 522 | } |
| 523 | |
| 524 | static const struct net_device_ops bfin_can_netdev_ops = { |
| 525 | .ndo_open = bfin_can_open, |
| 526 | .ndo_stop = bfin_can_close, |
| 527 | .ndo_start_xmit = bfin_can_start_xmit, |
Oliver Hartkopp | c971fa2 | 2014-03-07 09:23:41 +0100 | [diff] [blame] | 528 | .ndo_change_mtu = can_change_mtu, |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 529 | }; |
| 530 | |
Bill Pemberton | 3c8ac0f | 2012-12-03 09:22:44 -0500 | [diff] [blame] | 531 | static int bfin_can_probe(struct platform_device *pdev) |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 532 | { |
| 533 | int err; |
| 534 | struct net_device *dev; |
| 535 | struct bfin_can_priv *priv; |
| 536 | struct resource *res_mem, *rx_irq, *tx_irq, *err_irq; |
| 537 | unsigned short *pdata; |
| 538 | |
Jingoo Han | 321165c | 2013-09-10 17:40:42 +0900 | [diff] [blame] | 539 | pdata = dev_get_platdata(&pdev->dev); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 540 | if (!pdata) { |
| 541 | dev_err(&pdev->dev, "No platform data provided!\n"); |
| 542 | err = -EINVAL; |
| 543 | goto exit; |
| 544 | } |
| 545 | |
| 546 | res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 547 | rx_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 548 | tx_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 1); |
| 549 | err_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 2); |
| 550 | if (!res_mem || !rx_irq || !tx_irq || !err_irq) { |
| 551 | err = -EINVAL; |
| 552 | goto exit; |
| 553 | } |
| 554 | |
| 555 | if (!request_mem_region(res_mem->start, resource_size(res_mem), |
| 556 | dev_name(&pdev->dev))) { |
| 557 | err = -EBUSY; |
| 558 | goto exit; |
| 559 | } |
| 560 | |
| 561 | /* request peripheral pins */ |
| 562 | err = peripheral_request_list(pdata, dev_name(&pdev->dev)); |
| 563 | if (err) |
| 564 | goto exit_mem_release; |
| 565 | |
| 566 | dev = alloc_bfin_candev(); |
| 567 | if (!dev) { |
| 568 | err = -ENOMEM; |
| 569 | goto exit_peri_pin_free; |
| 570 | } |
| 571 | |
| 572 | priv = netdev_priv(dev); |
| 573 | priv->membase = (void __iomem *)res_mem->start; |
| 574 | priv->rx_irq = rx_irq->start; |
| 575 | priv->tx_irq = tx_irq->start; |
| 576 | priv->err_irq = err_irq->start; |
| 577 | priv->pin_list = pdata; |
| 578 | priv->can.clock.freq = get_sclk(); |
| 579 | |
Jingoo Han | 00e4bbc | 2013-05-23 19:47:58 +0900 | [diff] [blame] | 580 | platform_set_drvdata(pdev, dev); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 581 | SET_NETDEV_DEV(dev, &pdev->dev); |
| 582 | |
| 583 | dev->flags |= IFF_ECHO; /* we support local echo */ |
| 584 | dev->netdev_ops = &bfin_can_netdev_ops; |
| 585 | |
| 586 | bfin_can_set_reset_mode(dev); |
| 587 | |
| 588 | err = register_candev(dev); |
| 589 | if (err) { |
| 590 | dev_err(&pdev->dev, "registering failed (err=%d)\n", err); |
| 591 | goto exit_candev_free; |
| 592 | } |
| 593 | |
| 594 | dev_info(&pdev->dev, |
| 595 | "%s device registered" |
| 596 | "(®_base=%p, rx_irq=%d, tx_irq=%d, err_irq=%d, sclk=%d)\n", |
Joe Perches | c2fd03a | 2012-06-04 12:44:18 +0000 | [diff] [blame] | 597 | DRV_NAME, priv->membase, priv->rx_irq, |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 598 | priv->tx_irq, priv->err_irq, priv->can.clock.freq); |
| 599 | return 0; |
| 600 | |
| 601 | exit_candev_free: |
| 602 | free_candev(dev); |
| 603 | exit_peri_pin_free: |
| 604 | peripheral_free_list(pdata); |
| 605 | exit_mem_release: |
| 606 | release_mem_region(res_mem->start, resource_size(res_mem)); |
| 607 | exit: |
| 608 | return err; |
| 609 | } |
| 610 | |
Bill Pemberton | 3c8ac0f | 2012-12-03 09:22:44 -0500 | [diff] [blame] | 611 | static int bfin_can_remove(struct platform_device *pdev) |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 612 | { |
Jingoo Han | 00e4bbc | 2013-05-23 19:47:58 +0900 | [diff] [blame] | 613 | struct net_device *dev = platform_get_drvdata(pdev); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 614 | struct bfin_can_priv *priv = netdev_priv(dev); |
| 615 | struct resource *res; |
| 616 | |
| 617 | bfin_can_set_reset_mode(dev); |
| 618 | |
| 619 | unregister_candev(dev); |
| 620 | |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 621 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 622 | release_mem_region(res->start, resource_size(res)); |
| 623 | |
| 624 | peripheral_free_list(priv->pin_list); |
| 625 | |
| 626 | free_candev(dev); |
| 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | #ifdef CONFIG_PM |
| 631 | static int bfin_can_suspend(struct platform_device *pdev, pm_message_t mesg) |
| 632 | { |
Jingoo Han | 00e4bbc | 2013-05-23 19:47:58 +0900 | [diff] [blame] | 633 | struct net_device *dev = platform_get_drvdata(pdev); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 634 | struct bfin_can_priv *priv = netdev_priv(dev); |
| 635 | struct bfin_can_regs __iomem *reg = priv->membase; |
| 636 | int timeout = BFIN_CAN_TIMEOUT; |
| 637 | |
| 638 | if (netif_running(dev)) { |
| 639 | /* enter sleep mode */ |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 640 | writew(readw(®->control) | SMR, ®->control); |
| 641 | while (!(readw(®->intr) & SMACK)) { |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 642 | udelay(10); |
| 643 | if (--timeout == 0) { |
Wolfgang Grandegger | aabdfd6 | 2012-02-01 11:02:05 +0100 | [diff] [blame] | 644 | netdev_err(dev, "fail to enter sleep mode\n"); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 645 | BUG(); |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | static int bfin_can_resume(struct platform_device *pdev) |
| 654 | { |
Jingoo Han | 00e4bbc | 2013-05-23 19:47:58 +0900 | [diff] [blame] | 655 | struct net_device *dev = platform_get_drvdata(pdev); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 656 | struct bfin_can_priv *priv = netdev_priv(dev); |
| 657 | struct bfin_can_regs __iomem *reg = priv->membase; |
| 658 | |
| 659 | if (netif_running(dev)) { |
| 660 | /* leave sleep mode */ |
Aaron Wu | e4936e0 | 2015-02-10 17:40:34 +0800 | [diff] [blame^] | 661 | writew(0, ®->intr); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | return 0; |
| 665 | } |
| 666 | #else |
| 667 | #define bfin_can_suspend NULL |
| 668 | #define bfin_can_resume NULL |
| 669 | #endif /* CONFIG_PM */ |
| 670 | |
| 671 | static struct platform_driver bfin_can_driver = { |
| 672 | .probe = bfin_can_probe, |
Bill Pemberton | 3c8ac0f | 2012-12-03 09:22:44 -0500 | [diff] [blame] | 673 | .remove = bfin_can_remove, |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 674 | .suspend = bfin_can_suspend, |
| 675 | .resume = bfin_can_resume, |
| 676 | .driver = { |
| 677 | .name = DRV_NAME, |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 678 | }, |
| 679 | }; |
| 680 | |
Axel Lin | 871d337 | 2011-11-27 15:42:31 +0000 | [diff] [blame] | 681 | module_platform_driver(bfin_can_driver); |
Barry Song | bbb8461 | 2009-12-10 23:46:28 +0000 | [diff] [blame] | 682 | |
| 683 | MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); |
| 684 | MODULE_LICENSE("GPL"); |
| 685 | MODULE_DESCRIPTION("Blackfin on-chip CAN netdevice driver"); |
Marc Kleine-Budde | 73ae949 | 2012-10-12 09:45:14 +0200 | [diff] [blame] | 686 | MODULE_ALIAS("platform:" DRV_NAME); |