Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008-2010 |
| 3 | * |
| 4 | * - Kurt Van Dijck, EIA Electronics |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the version 2 of the GNU General Public License |
| 8 | * as published by the Free Software Foundation |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
Jeff Kirsher | 05780d9 | 2013-12-06 06:28:45 -0800 | [diff] [blame] | 16 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 19 | #include <linux/module.h> |
Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 20 | #include <linux/interrupt.h> |
Alexey Dobriyan | b7f080c | 2011-06-16 11:01:34 +0000 | [diff] [blame] | 21 | #include <asm/io.h> |
Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 22 | |
| 23 | #include "softing.h" |
| 24 | |
| 25 | #define TX_ECHO_SKB_MAX (((TXMAX+1)/2)-1) |
| 26 | |
| 27 | /* |
| 28 | * test is a specific CAN netdev |
| 29 | * is online (ie. up 'n running, not sleeping, not busoff |
| 30 | */ |
| 31 | static inline int canif_is_active(struct net_device *netdev) |
| 32 | { |
| 33 | struct can_priv *can = netdev_priv(netdev); |
| 34 | |
| 35 | if (!netif_running(netdev)) |
| 36 | return 0; |
| 37 | return (can->state <= CAN_STATE_ERROR_PASSIVE); |
| 38 | } |
| 39 | |
| 40 | /* reset DPRAM */ |
| 41 | static inline void softing_set_reset_dpram(struct softing *card) |
| 42 | { |
| 43 | if (card->pdat->generation >= 2) { |
| 44 | spin_lock_bh(&card->spin); |
| 45 | iowrite8(ioread8(&card->dpram[DPRAM_V2_RESET]) & ~1, |
| 46 | &card->dpram[DPRAM_V2_RESET]); |
| 47 | spin_unlock_bh(&card->spin); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | static inline void softing_clr_reset_dpram(struct softing *card) |
| 52 | { |
| 53 | if (card->pdat->generation >= 2) { |
| 54 | spin_lock_bh(&card->spin); |
| 55 | iowrite8(ioread8(&card->dpram[DPRAM_V2_RESET]) | 1, |
| 56 | &card->dpram[DPRAM_V2_RESET]); |
| 57 | spin_unlock_bh(&card->spin); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /* trigger the tx queue-ing */ |
| 62 | static netdev_tx_t softing_netdev_start_xmit(struct sk_buff *skb, |
| 63 | struct net_device *dev) |
| 64 | { |
| 65 | struct softing_priv *priv = netdev_priv(dev); |
| 66 | struct softing *card = priv->card; |
| 67 | int ret; |
| 68 | uint8_t *ptr; |
| 69 | uint8_t fifo_wr, fifo_rd; |
| 70 | struct can_frame *cf = (struct can_frame *)skb->data; |
| 71 | uint8_t buf[DPRAM_TX_SIZE]; |
| 72 | |
| 73 | if (can_dropped_invalid_skb(dev, skb)) |
| 74 | return NETDEV_TX_OK; |
| 75 | |
| 76 | spin_lock(&card->spin); |
| 77 | |
| 78 | ret = NETDEV_TX_BUSY; |
| 79 | if (!card->fw.up || |
| 80 | (card->tx.pending >= TXMAX) || |
| 81 | (priv->tx.pending >= TX_ECHO_SKB_MAX)) |
| 82 | goto xmit_done; |
| 83 | fifo_wr = ioread8(&card->dpram[DPRAM_TX_WR]); |
| 84 | fifo_rd = ioread8(&card->dpram[DPRAM_TX_RD]); |
| 85 | if (fifo_wr == fifo_rd) |
| 86 | /* fifo full */ |
| 87 | goto xmit_done; |
| 88 | memset(buf, 0, sizeof(buf)); |
| 89 | ptr = buf; |
| 90 | *ptr = CMD_TX; |
| 91 | if (cf->can_id & CAN_RTR_FLAG) |
| 92 | *ptr |= CMD_RTR; |
| 93 | if (cf->can_id & CAN_EFF_FLAG) |
| 94 | *ptr |= CMD_XTD; |
| 95 | if (priv->index) |
| 96 | *ptr |= CMD_BUS2; |
| 97 | ++ptr; |
| 98 | *ptr++ = cf->can_dlc; |
| 99 | *ptr++ = (cf->can_id >> 0); |
| 100 | *ptr++ = (cf->can_id >> 8); |
| 101 | if (cf->can_id & CAN_EFF_FLAG) { |
| 102 | *ptr++ = (cf->can_id >> 16); |
| 103 | *ptr++ = (cf->can_id >> 24); |
| 104 | } else { |
| 105 | /* increment 1, not 2 as you might think */ |
| 106 | ptr += 1; |
| 107 | } |
| 108 | if (!(cf->can_id & CAN_RTR_FLAG)) |
| 109 | memcpy(ptr, &cf->data[0], cf->can_dlc); |
| 110 | memcpy_toio(&card->dpram[DPRAM_TX + DPRAM_TX_SIZE * fifo_wr], |
| 111 | buf, DPRAM_TX_SIZE); |
| 112 | if (++fifo_wr >= DPRAM_TX_CNT) |
| 113 | fifo_wr = 0; |
| 114 | iowrite8(fifo_wr, &card->dpram[DPRAM_TX_WR]); |
| 115 | card->tx.last_bus = priv->index; |
| 116 | ++card->tx.pending; |
| 117 | ++priv->tx.pending; |
| 118 | can_put_echo_skb(skb, dev, priv->tx.echo_put); |
| 119 | ++priv->tx.echo_put; |
| 120 | if (priv->tx.echo_put >= TX_ECHO_SKB_MAX) |
| 121 | priv->tx.echo_put = 0; |
| 122 | /* can_put_echo_skb() saves the skb, safe to return TX_OK */ |
| 123 | ret = NETDEV_TX_OK; |
| 124 | xmit_done: |
| 125 | spin_unlock(&card->spin); |
| 126 | if (card->tx.pending >= TXMAX) { |
| 127 | int j; |
| 128 | for (j = 0; j < ARRAY_SIZE(card->net); ++j) { |
| 129 | if (card->net[j]) |
| 130 | netif_stop_queue(card->net[j]); |
| 131 | } |
| 132 | } |
| 133 | if (ret != NETDEV_TX_OK) |
| 134 | netif_stop_queue(dev); |
| 135 | |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * shortcut for skb delivery |
| 141 | */ |
| 142 | int softing_netdev_rx(struct net_device *netdev, const struct can_frame *msg, |
| 143 | ktime_t ktime) |
| 144 | { |
| 145 | struct sk_buff *skb; |
| 146 | struct can_frame *cf; |
| 147 | |
| 148 | skb = alloc_can_skb(netdev, &cf); |
| 149 | if (!skb) |
| 150 | return -ENOMEM; |
| 151 | memcpy(cf, msg, sizeof(*msg)); |
| 152 | skb->tstamp = ktime; |
| 153 | return netif_rx(skb); |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * softing_handle_1 |
| 158 | * pop 1 entry from the DPRAM queue, and process |
| 159 | */ |
| 160 | static int softing_handle_1(struct softing *card) |
| 161 | { |
| 162 | struct net_device *netdev; |
| 163 | struct softing_priv *priv; |
| 164 | ktime_t ktime; |
| 165 | struct can_frame msg; |
| 166 | int cnt = 0, lost_msg; |
| 167 | uint8_t fifo_rd, fifo_wr, cmd; |
| 168 | uint8_t *ptr; |
| 169 | uint32_t tmp_u32; |
| 170 | uint8_t buf[DPRAM_RX_SIZE]; |
| 171 | |
| 172 | memset(&msg, 0, sizeof(msg)); |
| 173 | /* test for lost msgs */ |
| 174 | lost_msg = ioread8(&card->dpram[DPRAM_RX_LOST]); |
| 175 | if (lost_msg) { |
| 176 | int j; |
| 177 | /* reset condition */ |
| 178 | iowrite8(0, &card->dpram[DPRAM_RX_LOST]); |
| 179 | /* prepare msg */ |
| 180 | msg.can_id = CAN_ERR_FLAG | CAN_ERR_CRTL; |
| 181 | msg.can_dlc = CAN_ERR_DLC; |
| 182 | msg.data[1] = CAN_ERR_CRTL_RX_OVERFLOW; |
| 183 | /* |
| 184 | * service to all busses, we don't know which it was applicable |
| 185 | * but only service busses that are online |
| 186 | */ |
| 187 | for (j = 0; j < ARRAY_SIZE(card->net); ++j) { |
| 188 | netdev = card->net[j]; |
| 189 | if (!netdev) |
| 190 | continue; |
| 191 | if (!canif_is_active(netdev)) |
| 192 | /* a dead bus has no overflows */ |
| 193 | continue; |
| 194 | ++netdev->stats.rx_over_errors; |
| 195 | softing_netdev_rx(netdev, &msg, ktime_set(0, 0)); |
| 196 | } |
| 197 | /* prepare for other use */ |
| 198 | memset(&msg, 0, sizeof(msg)); |
| 199 | ++cnt; |
| 200 | } |
| 201 | |
| 202 | fifo_rd = ioread8(&card->dpram[DPRAM_RX_RD]); |
| 203 | fifo_wr = ioread8(&card->dpram[DPRAM_RX_WR]); |
| 204 | |
| 205 | if (++fifo_rd >= DPRAM_RX_CNT) |
| 206 | fifo_rd = 0; |
| 207 | if (fifo_wr == fifo_rd) |
| 208 | return cnt; |
| 209 | |
| 210 | memcpy_fromio(buf, &card->dpram[DPRAM_RX + DPRAM_RX_SIZE*fifo_rd], |
| 211 | DPRAM_RX_SIZE); |
| 212 | mb(); |
| 213 | /* trigger dual port RAM */ |
| 214 | iowrite8(fifo_rd, &card->dpram[DPRAM_RX_RD]); |
| 215 | |
| 216 | ptr = buf; |
| 217 | cmd = *ptr++; |
| 218 | if (cmd == 0xff) |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 219 | /* not quite useful, probably the card has got out */ |
Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 220 | return 0; |
| 221 | netdev = card->net[0]; |
| 222 | if (cmd & CMD_BUS2) |
| 223 | netdev = card->net[1]; |
| 224 | priv = netdev_priv(netdev); |
| 225 | |
| 226 | if (cmd & CMD_ERR) { |
| 227 | uint8_t can_state, state; |
| 228 | |
| 229 | state = *ptr++; |
| 230 | |
| 231 | msg.can_id = CAN_ERR_FLAG; |
| 232 | msg.can_dlc = CAN_ERR_DLC; |
| 233 | |
| 234 | if (state & SF_MASK_BUSOFF) { |
| 235 | can_state = CAN_STATE_BUS_OFF; |
| 236 | msg.can_id |= CAN_ERR_BUSOFF; |
| 237 | state = STATE_BUSOFF; |
| 238 | } else if (state & SF_MASK_EPASSIVE) { |
| 239 | can_state = CAN_STATE_ERROR_PASSIVE; |
| 240 | msg.can_id |= CAN_ERR_CRTL; |
| 241 | msg.data[1] = CAN_ERR_CRTL_TX_PASSIVE; |
| 242 | state = STATE_EPASSIVE; |
| 243 | } else { |
| 244 | can_state = CAN_STATE_ERROR_ACTIVE; |
| 245 | msg.can_id |= CAN_ERR_CRTL; |
| 246 | state = STATE_EACTIVE; |
| 247 | } |
| 248 | /* update DPRAM */ |
| 249 | iowrite8(state, &card->dpram[priv->index ? |
| 250 | DPRAM_INFO_BUSSTATE2 : DPRAM_INFO_BUSSTATE]); |
| 251 | /* timestamp */ |
| 252 | tmp_u32 = le32_to_cpup((void *)ptr); |
| 253 | ptr += 4; |
| 254 | ktime = softing_raw2ktime(card, tmp_u32); |
| 255 | |
| 256 | ++netdev->stats.rx_errors; |
| 257 | /* update internal status */ |
| 258 | if (can_state != priv->can.state) { |
| 259 | priv->can.state = can_state; |
| 260 | if (can_state == CAN_STATE_ERROR_PASSIVE) |
| 261 | ++priv->can.can_stats.error_passive; |
| 262 | else if (can_state == CAN_STATE_BUS_OFF) { |
| 263 | /* this calls can_close_cleanup() */ |
| 264 | can_bus_off(netdev); |
| 265 | netif_stop_queue(netdev); |
| 266 | } |
| 267 | /* trigger socketcan */ |
| 268 | softing_netdev_rx(netdev, &msg, ktime); |
| 269 | } |
| 270 | |
| 271 | } else { |
| 272 | if (cmd & CMD_RTR) |
| 273 | msg.can_id |= CAN_RTR_FLAG; |
| 274 | msg.can_dlc = get_can_dlc(*ptr++); |
| 275 | if (cmd & CMD_XTD) { |
| 276 | msg.can_id |= CAN_EFF_FLAG; |
| 277 | msg.can_id |= le32_to_cpup((void *)ptr); |
| 278 | ptr += 4; |
| 279 | } else { |
| 280 | msg.can_id |= le16_to_cpup((void *)ptr); |
| 281 | ptr += 2; |
| 282 | } |
| 283 | /* timestamp */ |
| 284 | tmp_u32 = le32_to_cpup((void *)ptr); |
| 285 | ptr += 4; |
| 286 | ktime = softing_raw2ktime(card, tmp_u32); |
| 287 | if (!(msg.can_id & CAN_RTR_FLAG)) |
| 288 | memcpy(&msg.data[0], ptr, 8); |
| 289 | ptr += 8; |
| 290 | /* update socket */ |
| 291 | if (cmd & CMD_ACK) { |
| 292 | /* acknowledge, was tx msg */ |
| 293 | struct sk_buff *skb; |
| 294 | skb = priv->can.echo_skb[priv->tx.echo_get]; |
| 295 | if (skb) |
| 296 | skb->tstamp = ktime; |
| 297 | can_get_echo_skb(netdev, priv->tx.echo_get); |
| 298 | ++priv->tx.echo_get; |
| 299 | if (priv->tx.echo_get >= TX_ECHO_SKB_MAX) |
| 300 | priv->tx.echo_get = 0; |
| 301 | if (priv->tx.pending) |
| 302 | --priv->tx.pending; |
| 303 | if (card->tx.pending) |
| 304 | --card->tx.pending; |
| 305 | ++netdev->stats.tx_packets; |
| 306 | if (!(msg.can_id & CAN_RTR_FLAG)) |
| 307 | netdev->stats.tx_bytes += msg.can_dlc; |
| 308 | } else { |
| 309 | int ret; |
| 310 | |
| 311 | ret = softing_netdev_rx(netdev, &msg, ktime); |
| 312 | if (ret == NET_RX_SUCCESS) { |
| 313 | ++netdev->stats.rx_packets; |
| 314 | if (!(msg.can_id & CAN_RTR_FLAG)) |
| 315 | netdev->stats.rx_bytes += msg.can_dlc; |
| 316 | } else { |
| 317 | ++netdev->stats.rx_dropped; |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | ++cnt; |
| 322 | return cnt; |
| 323 | } |
| 324 | |
| 325 | /* |
| 326 | * real interrupt handler |
| 327 | */ |
| 328 | static irqreturn_t softing_irq_thread(int irq, void *dev_id) |
| 329 | { |
| 330 | struct softing *card = (struct softing *)dev_id; |
| 331 | struct net_device *netdev; |
| 332 | struct softing_priv *priv; |
| 333 | int j, offset, work_done; |
| 334 | |
| 335 | work_done = 0; |
| 336 | spin_lock_bh(&card->spin); |
| 337 | while (softing_handle_1(card) > 0) { |
| 338 | ++card->irq.svc_count; |
| 339 | ++work_done; |
| 340 | } |
| 341 | spin_unlock_bh(&card->spin); |
| 342 | /* resume tx queue's */ |
| 343 | offset = card->tx.last_bus; |
| 344 | for (j = 0; j < ARRAY_SIZE(card->net); ++j) { |
| 345 | if (card->tx.pending >= TXMAX) |
| 346 | break; |
| 347 | netdev = card->net[(j + offset + 1) % card->pdat->nbus]; |
| 348 | if (!netdev) |
| 349 | continue; |
| 350 | priv = netdev_priv(netdev); |
| 351 | if (!canif_is_active(netdev)) |
| 352 | /* it makes no sense to wake dead busses */ |
| 353 | continue; |
| 354 | if (priv->tx.pending >= TX_ECHO_SKB_MAX) |
| 355 | continue; |
| 356 | ++work_done; |
| 357 | netif_wake_queue(netdev); |
| 358 | } |
| 359 | return work_done ? IRQ_HANDLED : IRQ_NONE; |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | * interrupt routines: |
| 364 | * schedule the 'real interrupt handler' |
| 365 | */ |
| 366 | static irqreturn_t softing_irq_v2(int irq, void *dev_id) |
| 367 | { |
| 368 | struct softing *card = (struct softing *)dev_id; |
| 369 | uint8_t ir; |
| 370 | |
| 371 | ir = ioread8(&card->dpram[DPRAM_V2_IRQ_TOHOST]); |
| 372 | iowrite8(0, &card->dpram[DPRAM_V2_IRQ_TOHOST]); |
| 373 | return (1 == ir) ? IRQ_WAKE_THREAD : IRQ_NONE; |
| 374 | } |
| 375 | |
| 376 | static irqreturn_t softing_irq_v1(int irq, void *dev_id) |
| 377 | { |
| 378 | struct softing *card = (struct softing *)dev_id; |
| 379 | uint8_t ir; |
| 380 | |
| 381 | ir = ioread8(&card->dpram[DPRAM_IRQ_TOHOST]); |
| 382 | iowrite8(0, &card->dpram[DPRAM_IRQ_TOHOST]); |
| 383 | return ir ? IRQ_WAKE_THREAD : IRQ_NONE; |
| 384 | } |
| 385 | |
| 386 | /* |
| 387 | * netdev/candev inter-operability |
| 388 | */ |
| 389 | static int softing_netdev_open(struct net_device *ndev) |
| 390 | { |
| 391 | int ret; |
| 392 | |
| 393 | /* check or determine and set bittime */ |
| 394 | ret = open_candev(ndev); |
| 395 | if (!ret) |
| 396 | ret = softing_startstop(ndev, 1); |
| 397 | return ret; |
| 398 | } |
| 399 | |
| 400 | static int softing_netdev_stop(struct net_device *ndev) |
| 401 | { |
| 402 | int ret; |
| 403 | |
| 404 | netif_stop_queue(ndev); |
| 405 | |
| 406 | /* softing cycle does close_candev() */ |
| 407 | ret = softing_startstop(ndev, 0); |
| 408 | return ret; |
| 409 | } |
| 410 | |
| 411 | static int softing_candev_set_mode(struct net_device *ndev, enum can_mode mode) |
| 412 | { |
| 413 | int ret; |
| 414 | |
| 415 | switch (mode) { |
| 416 | case CAN_MODE_START: |
| 417 | /* softing_startstop does close_candev() */ |
| 418 | ret = softing_startstop(ndev, 1); |
| 419 | return ret; |
| 420 | case CAN_MODE_STOP: |
| 421 | case CAN_MODE_SLEEP: |
| 422 | return -EOPNOTSUPP; |
| 423 | } |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * Softing device management helpers |
| 429 | */ |
| 430 | int softing_enable_irq(struct softing *card, int enable) |
| 431 | { |
| 432 | int ret; |
| 433 | |
| 434 | if (!card->irq.nr) { |
| 435 | return 0; |
| 436 | } else if (card->irq.requested && !enable) { |
| 437 | free_irq(card->irq.nr, card); |
| 438 | card->irq.requested = 0; |
| 439 | } else if (!card->irq.requested && enable) { |
| 440 | ret = request_threaded_irq(card->irq.nr, |
| 441 | (card->pdat->generation >= 2) ? |
| 442 | softing_irq_v2 : softing_irq_v1, |
| 443 | softing_irq_thread, IRQF_SHARED, |
| 444 | dev_name(&card->pdev->dev), card); |
| 445 | if (ret) { |
| 446 | dev_alert(&card->pdev->dev, |
| 447 | "request_threaded_irq(%u) failed\n", |
| 448 | card->irq.nr); |
| 449 | return ret; |
| 450 | } |
| 451 | card->irq.requested = 1; |
| 452 | } |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | static void softing_card_shutdown(struct softing *card) |
| 457 | { |
| 458 | int fw_up = 0; |
| 459 | |
| 460 | if (mutex_lock_interruptible(&card->fw.lock)) |
| 461 | /* return -ERESTARTSYS */; |
| 462 | fw_up = card->fw.up; |
| 463 | card->fw.up = 0; |
| 464 | |
| 465 | if (card->irq.requested && card->irq.nr) { |
| 466 | free_irq(card->irq.nr, card); |
| 467 | card->irq.requested = 0; |
| 468 | } |
| 469 | if (fw_up) { |
| 470 | if (card->pdat->enable_irq) |
| 471 | card->pdat->enable_irq(card->pdev, 0); |
| 472 | softing_set_reset_dpram(card); |
| 473 | if (card->pdat->reset) |
| 474 | card->pdat->reset(card->pdev, 1); |
| 475 | } |
| 476 | mutex_unlock(&card->fw.lock); |
| 477 | } |
| 478 | |
Bill Pemberton | 3c8ac0f | 2012-12-03 09:22:44 -0500 | [diff] [blame] | 479 | static int softing_card_boot(struct softing *card) |
Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 480 | { |
| 481 | int ret, j; |
| 482 | static const uint8_t stream[] = { |
| 483 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, }; |
| 484 | unsigned char back[sizeof(stream)]; |
| 485 | |
| 486 | if (mutex_lock_interruptible(&card->fw.lock)) |
| 487 | return -ERESTARTSYS; |
| 488 | if (card->fw.up) { |
| 489 | mutex_unlock(&card->fw.lock); |
| 490 | return 0; |
| 491 | } |
| 492 | /* reset board */ |
| 493 | if (card->pdat->enable_irq) |
| 494 | card->pdat->enable_irq(card->pdev, 1); |
| 495 | /* boot card */ |
| 496 | softing_set_reset_dpram(card); |
| 497 | if (card->pdat->reset) |
| 498 | card->pdat->reset(card->pdev, 1); |
| 499 | for (j = 0; (j + sizeof(stream)) < card->dpram_size; |
| 500 | j += sizeof(stream)) { |
| 501 | |
| 502 | memcpy_toio(&card->dpram[j], stream, sizeof(stream)); |
| 503 | /* flush IO cache */ |
| 504 | mb(); |
| 505 | memcpy_fromio(back, &card->dpram[j], sizeof(stream)); |
| 506 | |
| 507 | if (!memcmp(back, stream, sizeof(stream))) |
| 508 | continue; |
| 509 | /* memory is not equal */ |
| 510 | dev_alert(&card->pdev->dev, "dpram failed at 0x%04x\n", j); |
| 511 | ret = -EIO; |
| 512 | goto failed; |
| 513 | } |
| 514 | wmb(); |
| 515 | /* load boot firmware */ |
| 516 | ret = softing_load_fw(card->pdat->boot.fw, card, card->dpram, |
| 517 | card->dpram_size, |
| 518 | card->pdat->boot.offs - card->pdat->boot.addr); |
| 519 | if (ret < 0) |
| 520 | goto failed; |
| 521 | /* load loader firmware */ |
| 522 | ret = softing_load_fw(card->pdat->load.fw, card, card->dpram, |
| 523 | card->dpram_size, |
| 524 | card->pdat->load.offs - card->pdat->load.addr); |
| 525 | if (ret < 0) |
| 526 | goto failed; |
| 527 | |
| 528 | if (card->pdat->reset) |
| 529 | card->pdat->reset(card->pdev, 0); |
| 530 | softing_clr_reset_dpram(card); |
| 531 | ret = softing_bootloader_command(card, 0, "card boot"); |
| 532 | if (ret < 0) |
| 533 | goto failed; |
| 534 | ret = softing_load_app_fw(card->pdat->app.fw, card); |
| 535 | if (ret < 0) |
| 536 | goto failed; |
| 537 | |
| 538 | ret = softing_chip_poweron(card); |
| 539 | if (ret < 0) |
| 540 | goto failed; |
| 541 | |
| 542 | card->fw.up = 1; |
| 543 | mutex_unlock(&card->fw.lock); |
| 544 | return 0; |
| 545 | failed: |
| 546 | card->fw.up = 0; |
| 547 | if (card->pdat->enable_irq) |
| 548 | card->pdat->enable_irq(card->pdev, 0); |
| 549 | softing_set_reset_dpram(card); |
| 550 | if (card->pdat->reset) |
| 551 | card->pdat->reset(card->pdev, 1); |
| 552 | mutex_unlock(&card->fw.lock); |
| 553 | return ret; |
| 554 | } |
| 555 | |
| 556 | /* |
| 557 | * netdev sysfs |
| 558 | */ |
| 559 | static ssize_t show_channel(struct device *dev, struct device_attribute *attr, |
| 560 | char *buf) |
| 561 | { |
| 562 | struct net_device *ndev = to_net_dev(dev); |
| 563 | struct softing_priv *priv = netdev2softing(ndev); |
| 564 | |
| 565 | return sprintf(buf, "%i\n", priv->index); |
| 566 | } |
| 567 | |
| 568 | static ssize_t show_chip(struct device *dev, struct device_attribute *attr, |
| 569 | char *buf) |
| 570 | { |
| 571 | struct net_device *ndev = to_net_dev(dev); |
| 572 | struct softing_priv *priv = netdev2softing(ndev); |
| 573 | |
| 574 | return sprintf(buf, "%i\n", priv->chip); |
| 575 | } |
| 576 | |
| 577 | static ssize_t show_output(struct device *dev, struct device_attribute *attr, |
| 578 | char *buf) |
| 579 | { |
| 580 | struct net_device *ndev = to_net_dev(dev); |
| 581 | struct softing_priv *priv = netdev2softing(ndev); |
| 582 | |
| 583 | return sprintf(buf, "0x%02x\n", priv->output); |
| 584 | } |
| 585 | |
| 586 | static ssize_t store_output(struct device *dev, struct device_attribute *attr, |
| 587 | const char *buf, size_t count) |
| 588 | { |
| 589 | struct net_device *ndev = to_net_dev(dev); |
| 590 | struct softing_priv *priv = netdev2softing(ndev); |
| 591 | struct softing *card = priv->card; |
| 592 | unsigned long val; |
| 593 | int ret; |
| 594 | |
Jingoo Han | 0672f0a | 2013-05-31 21:18:55 +0000 | [diff] [blame] | 595 | ret = kstrtoul(buf, 0, &val); |
Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 596 | if (ret < 0) |
| 597 | return ret; |
| 598 | val &= 0xFF; |
| 599 | |
| 600 | ret = mutex_lock_interruptible(&card->fw.lock); |
| 601 | if (ret) |
| 602 | return -ERESTARTSYS; |
| 603 | if (netif_running(ndev)) { |
| 604 | mutex_unlock(&card->fw.lock); |
| 605 | return -EBUSY; |
| 606 | } |
| 607 | priv->output = val; |
| 608 | mutex_unlock(&card->fw.lock); |
| 609 | return count; |
| 610 | } |
| 611 | |
| 612 | static const DEVICE_ATTR(channel, S_IRUGO, show_channel, NULL); |
| 613 | static const DEVICE_ATTR(chip, S_IRUGO, show_chip, NULL); |
| 614 | static const DEVICE_ATTR(output, S_IRUGO | S_IWUSR, show_output, store_output); |
| 615 | |
| 616 | static const struct attribute *const netdev_sysfs_attrs[] = { |
| 617 | &dev_attr_channel.attr, |
| 618 | &dev_attr_chip.attr, |
| 619 | &dev_attr_output.attr, |
| 620 | NULL, |
| 621 | }; |
| 622 | static const struct attribute_group netdev_sysfs_group = { |
| 623 | .name = NULL, |
| 624 | .attrs = (struct attribute **)netdev_sysfs_attrs, |
| 625 | }; |
| 626 | |
| 627 | static const struct net_device_ops softing_netdev_ops = { |
| 628 | .ndo_open = softing_netdev_open, |
| 629 | .ndo_stop = softing_netdev_stop, |
| 630 | .ndo_start_xmit = softing_netdev_start_xmit, |
| 631 | }; |
| 632 | |
| 633 | static const struct can_bittiming_const softing_btr_const = { |
Kurt Van Dijck | dad3d44 | 2011-02-20 23:04:22 +0000 | [diff] [blame] | 634 | .name = "softing", |
Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 635 | .tseg1_min = 1, |
| 636 | .tseg1_max = 16, |
| 637 | .tseg2_min = 1, |
| 638 | .tseg2_max = 8, |
| 639 | .sjw_max = 4, /* overruled */ |
| 640 | .brp_min = 1, |
| 641 | .brp_max = 32, /* overruled */ |
| 642 | .brp_inc = 1, |
| 643 | }; |
| 644 | |
| 645 | |
Bill Pemberton | 3c8ac0f | 2012-12-03 09:22:44 -0500 | [diff] [blame] | 646 | static struct net_device *softing_netdev_create(struct softing *card, |
Greg Kroah-Hartman | 1dd06ae | 2012-12-06 14:30:56 +0000 | [diff] [blame] | 647 | uint16_t chip_id) |
Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 648 | { |
| 649 | struct net_device *netdev; |
| 650 | struct softing_priv *priv; |
| 651 | |
| 652 | netdev = alloc_candev(sizeof(*priv), TX_ECHO_SKB_MAX); |
| 653 | if (!netdev) { |
| 654 | dev_alert(&card->pdev->dev, "alloc_candev failed\n"); |
| 655 | return NULL; |
| 656 | } |
| 657 | priv = netdev_priv(netdev); |
| 658 | priv->netdev = netdev; |
| 659 | priv->card = card; |
| 660 | memcpy(&priv->btr_const, &softing_btr_const, sizeof(priv->btr_const)); |
| 661 | priv->btr_const.brp_max = card->pdat->max_brp; |
| 662 | priv->btr_const.sjw_max = card->pdat->max_sjw; |
| 663 | priv->can.bittiming_const = &priv->btr_const; |
| 664 | priv->can.clock.freq = 8000000; |
| 665 | priv->chip = chip_id; |
| 666 | priv->output = softing_default_output(netdev); |
| 667 | SET_NETDEV_DEV(netdev, &card->pdev->dev); |
| 668 | |
| 669 | netdev->flags |= IFF_ECHO; |
| 670 | netdev->netdev_ops = &softing_netdev_ops; |
| 671 | priv->can.do_set_mode = softing_candev_set_mode; |
| 672 | priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES; |
| 673 | |
| 674 | return netdev; |
| 675 | } |
| 676 | |
Bill Pemberton | 3c8ac0f | 2012-12-03 09:22:44 -0500 | [diff] [blame] | 677 | static int softing_netdev_register(struct net_device *netdev) |
Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 678 | { |
| 679 | int ret; |
| 680 | |
| 681 | netdev->sysfs_groups[0] = &netdev_sysfs_group; |
| 682 | ret = register_candev(netdev); |
| 683 | if (ret) { |
| 684 | dev_alert(&netdev->dev, "register failed\n"); |
| 685 | return ret; |
| 686 | } |
| 687 | return 0; |
| 688 | } |
| 689 | |
| 690 | static void softing_netdev_cleanup(struct net_device *netdev) |
| 691 | { |
| 692 | unregister_candev(netdev); |
| 693 | free_candev(netdev); |
| 694 | } |
| 695 | |
| 696 | /* |
| 697 | * sysfs for Platform device |
| 698 | */ |
| 699 | #define DEV_ATTR_RO(name, member) \ |
| 700 | static ssize_t show_##name(struct device *dev, \ |
| 701 | struct device_attribute *attr, char *buf) \ |
| 702 | { \ |
| 703 | struct softing *card = platform_get_drvdata(to_platform_device(dev)); \ |
| 704 | return sprintf(buf, "%u\n", card->member); \ |
| 705 | } \ |
| 706 | static DEVICE_ATTR(name, 0444, show_##name, NULL) |
| 707 | |
| 708 | #define DEV_ATTR_RO_STR(name, member) \ |
| 709 | static ssize_t show_##name(struct device *dev, \ |
| 710 | struct device_attribute *attr, char *buf) \ |
| 711 | { \ |
| 712 | struct softing *card = platform_get_drvdata(to_platform_device(dev)); \ |
| 713 | return sprintf(buf, "%s\n", card->member); \ |
| 714 | } \ |
| 715 | static DEVICE_ATTR(name, 0444, show_##name, NULL) |
| 716 | |
| 717 | DEV_ATTR_RO(serial, id.serial); |
| 718 | DEV_ATTR_RO_STR(firmware, pdat->app.fw); |
| 719 | DEV_ATTR_RO(firmware_version, id.fw_version); |
| 720 | DEV_ATTR_RO_STR(hardware, pdat->name); |
| 721 | DEV_ATTR_RO(hardware_version, id.hw_version); |
| 722 | DEV_ATTR_RO(license, id.license); |
| 723 | DEV_ATTR_RO(frequency, id.freq); |
| 724 | DEV_ATTR_RO(txpending, tx.pending); |
| 725 | |
| 726 | static struct attribute *softing_pdev_attrs[] = { |
| 727 | &dev_attr_serial.attr, |
| 728 | &dev_attr_firmware.attr, |
| 729 | &dev_attr_firmware_version.attr, |
| 730 | &dev_attr_hardware.attr, |
| 731 | &dev_attr_hardware_version.attr, |
| 732 | &dev_attr_license.attr, |
| 733 | &dev_attr_frequency.attr, |
| 734 | &dev_attr_txpending.attr, |
| 735 | NULL, |
| 736 | }; |
| 737 | |
| 738 | static const struct attribute_group softing_pdev_group = { |
| 739 | .name = NULL, |
| 740 | .attrs = softing_pdev_attrs, |
| 741 | }; |
| 742 | |
| 743 | /* |
| 744 | * platform driver |
| 745 | */ |
Bill Pemberton | 3c8ac0f | 2012-12-03 09:22:44 -0500 | [diff] [blame] | 746 | static int softing_pdev_remove(struct platform_device *pdev) |
Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 747 | { |
| 748 | struct softing *card = platform_get_drvdata(pdev); |
| 749 | int j; |
| 750 | |
| 751 | /* first, disable card*/ |
| 752 | softing_card_shutdown(card); |
| 753 | |
| 754 | for (j = 0; j < ARRAY_SIZE(card->net); ++j) { |
| 755 | if (!card->net[j]) |
| 756 | continue; |
| 757 | softing_netdev_cleanup(card->net[j]); |
| 758 | card->net[j] = NULL; |
| 759 | } |
| 760 | sysfs_remove_group(&pdev->dev.kobj, &softing_pdev_group); |
| 761 | |
| 762 | iounmap(card->dpram); |
| 763 | kfree(card); |
| 764 | return 0; |
| 765 | } |
| 766 | |
Bill Pemberton | 3c8ac0f | 2012-12-03 09:22:44 -0500 | [diff] [blame] | 767 | static int softing_pdev_probe(struct platform_device *pdev) |
Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 768 | { |
Jingoo Han | c58bd85 | 2013-09-10 17:42:53 +0900 | [diff] [blame] | 769 | const struct softing_platform_data *pdat = dev_get_platdata(&pdev->dev); |
Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 770 | struct softing *card; |
| 771 | struct net_device *netdev; |
| 772 | struct softing_priv *priv; |
| 773 | struct resource *pres; |
| 774 | int ret; |
| 775 | int j; |
| 776 | |
| 777 | if (!pdat) { |
| 778 | dev_warn(&pdev->dev, "no platform data\n"); |
| 779 | return -EINVAL; |
| 780 | } |
| 781 | if (pdat->nbus > ARRAY_SIZE(card->net)) { |
| 782 | dev_warn(&pdev->dev, "%u nets??\n", pdat->nbus); |
| 783 | return -EINVAL; |
| 784 | } |
| 785 | |
| 786 | card = kzalloc(sizeof(*card), GFP_KERNEL); |
| 787 | if (!card) |
| 788 | return -ENOMEM; |
| 789 | card->pdat = pdat; |
| 790 | card->pdev = pdev; |
| 791 | platform_set_drvdata(pdev, card); |
| 792 | mutex_init(&card->fw.lock); |
| 793 | spin_lock_init(&card->spin); |
| 794 | |
| 795 | ret = -EINVAL; |
| 796 | pres = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 797 | if (!pres) |
Justin P. Mattock | 6eab04a | 2011-04-08 19:49:08 -0700 | [diff] [blame] | 798 | goto platform_resource_failed; |
Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 799 | card->dpram_phys = pres->start; |
Joe Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 800 | card->dpram_size = resource_size(pres); |
Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 801 | card->dpram = ioremap_nocache(card->dpram_phys, card->dpram_size); |
| 802 | if (!card->dpram) { |
| 803 | dev_alert(&card->pdev->dev, "dpram ioremap failed\n"); |
| 804 | goto ioremap_failed; |
| 805 | } |
| 806 | |
| 807 | pres = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 808 | if (pres) |
| 809 | card->irq.nr = pres->start; |
| 810 | |
| 811 | /* reset card */ |
| 812 | ret = softing_card_boot(card); |
| 813 | if (ret < 0) { |
| 814 | dev_alert(&pdev->dev, "failed to boot\n"); |
| 815 | goto boot_failed; |
| 816 | } |
| 817 | |
| 818 | /* only now, the chip's are known */ |
| 819 | card->id.freq = card->pdat->freq; |
| 820 | |
| 821 | ret = sysfs_create_group(&pdev->dev.kobj, &softing_pdev_group); |
| 822 | if (ret < 0) { |
| 823 | dev_alert(&card->pdev->dev, "sysfs failed\n"); |
| 824 | goto sysfs_failed; |
| 825 | } |
| 826 | |
Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 827 | for (j = 0; j < ARRAY_SIZE(card->net); ++j) { |
| 828 | card->net[j] = netdev = |
| 829 | softing_netdev_create(card, card->id.chip[j]); |
| 830 | if (!netdev) { |
| 831 | dev_alert(&pdev->dev, "failed to make can[%i]", j); |
Julia Lawall | da78b79 | 2012-07-14 18:43:04 +0200 | [diff] [blame] | 832 | ret = -ENOMEM; |
Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 833 | goto netdev_failed; |
| 834 | } |
| 835 | priv = netdev_priv(card->net[j]); |
| 836 | priv->index = j; |
| 837 | ret = softing_netdev_register(netdev); |
| 838 | if (ret) { |
| 839 | free_candev(netdev); |
| 840 | card->net[j] = NULL; |
| 841 | dev_alert(&card->pdev->dev, |
| 842 | "failed to register can[%i]\n", j); |
| 843 | goto netdev_failed; |
| 844 | } |
| 845 | } |
| 846 | dev_info(&card->pdev->dev, "%s ready.\n", card->pdat->name); |
| 847 | return 0; |
| 848 | |
| 849 | netdev_failed: |
| 850 | for (j = 0; j < ARRAY_SIZE(card->net); ++j) { |
| 851 | if (!card->net[j]) |
| 852 | continue; |
| 853 | softing_netdev_cleanup(card->net[j]); |
| 854 | } |
| 855 | sysfs_remove_group(&pdev->dev.kobj, &softing_pdev_group); |
| 856 | sysfs_failed: |
| 857 | softing_card_shutdown(card); |
| 858 | boot_failed: |
| 859 | iounmap(card->dpram); |
| 860 | ioremap_failed: |
| 861 | platform_resource_failed: |
| 862 | kfree(card); |
| 863 | return ret; |
| 864 | } |
| 865 | |
| 866 | static struct platform_driver softing_driver = { |
| 867 | .driver = { |
| 868 | .name = "softing", |
| 869 | .owner = THIS_MODULE, |
| 870 | }, |
| 871 | .probe = softing_pdev_probe, |
Bill Pemberton | 3c8ac0f | 2012-12-03 09:22:44 -0500 | [diff] [blame] | 872 | .remove = softing_pdev_remove, |
Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 873 | }; |
| 874 | |
Axel Lin | 871d337 | 2011-11-27 15:42:31 +0000 | [diff] [blame] | 875 | module_platform_driver(softing_driver); |
| 876 | |
Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 877 | MODULE_ALIAS("platform:softing"); |
Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 878 | MODULE_DESCRIPTION("Softing DPRAM CAN driver"); |
| 879 | MODULE_AUTHOR("Kurt Van Dijck <kurt.van.dijck@eia.be>"); |
| 880 | MODULE_LICENSE("GPL v2"); |