2 * Copyright (C) 2008-2010
4 * - Kurt Van Dijck, EIA Electronics
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
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/interrupt.h>
27 #define TX_ECHO_SKB_MAX (((TXMAX+1)/2)-1)
30 * test is a specific CAN netdev
31 * is online (ie. up 'n running, not sleeping, not busoff
33 static inline int canif_is_active(struct net_device *netdev)
35 struct can_priv *can = netdev_priv(netdev);
37 if (!netif_running(netdev))
39 return (can->state <= CAN_STATE_ERROR_PASSIVE);
43 static inline void softing_set_reset_dpram(struct softing *card)
45 if (card->pdat->generation >= 2) {
46 spin_lock_bh(&card->spin);
47 iowrite8(ioread8(&card->dpram[DPRAM_V2_RESET]) & ~1,
48 &card->dpram[DPRAM_V2_RESET]);
49 spin_unlock_bh(&card->spin);
53 static inline void softing_clr_reset_dpram(struct softing *card)
55 if (card->pdat->generation >= 2) {
56 spin_lock_bh(&card->spin);
57 iowrite8(ioread8(&card->dpram[DPRAM_V2_RESET]) | 1,
58 &card->dpram[DPRAM_V2_RESET]);
59 spin_unlock_bh(&card->spin);
63 /* trigger the tx queue-ing */
64 static netdev_tx_t softing_netdev_start_xmit(struct sk_buff *skb,
65 struct net_device *dev)
67 struct softing_priv *priv = netdev_priv(dev);
68 struct softing *card = priv->card;
71 uint8_t fifo_wr, fifo_rd;
72 struct can_frame *cf = (struct can_frame *)skb->data;
73 uint8_t buf[DPRAM_TX_SIZE];
75 if (can_dropped_invalid_skb(dev, skb))
78 spin_lock(&card->spin);
82 (card->tx.pending >= TXMAX) ||
83 (priv->tx.pending >= TX_ECHO_SKB_MAX))
85 fifo_wr = ioread8(&card->dpram[DPRAM_TX_WR]);
86 fifo_rd = ioread8(&card->dpram[DPRAM_TX_RD]);
87 if (fifo_wr == fifo_rd)
90 memset(buf, 0, sizeof(buf));
93 if (cf->can_id & CAN_RTR_FLAG)
95 if (cf->can_id & CAN_EFF_FLAG)
100 *ptr++ = cf->can_dlc;
101 *ptr++ = (cf->can_id >> 0);
102 *ptr++ = (cf->can_id >> 8);
103 if (cf->can_id & CAN_EFF_FLAG) {
104 *ptr++ = (cf->can_id >> 16);
105 *ptr++ = (cf->can_id >> 24);
107 /* increment 1, not 2 as you might think */
110 if (!(cf->can_id & CAN_RTR_FLAG))
111 memcpy(ptr, &cf->data[0], cf->can_dlc);
112 memcpy_toio(&card->dpram[DPRAM_TX + DPRAM_TX_SIZE * fifo_wr],
114 if (++fifo_wr >= DPRAM_TX_CNT)
116 iowrite8(fifo_wr, &card->dpram[DPRAM_TX_WR]);
117 card->tx.last_bus = priv->index;
120 can_put_echo_skb(skb, dev, priv->tx.echo_put);
122 if (priv->tx.echo_put >= TX_ECHO_SKB_MAX)
123 priv->tx.echo_put = 0;
124 /* can_put_echo_skb() saves the skb, safe to return TX_OK */
127 spin_unlock(&card->spin);
128 if (card->tx.pending >= TXMAX) {
130 for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
132 netif_stop_queue(card->net[j]);
135 if (ret != NETDEV_TX_OK)
136 netif_stop_queue(dev);
142 * shortcut for skb delivery
144 int softing_netdev_rx(struct net_device *netdev, const struct can_frame *msg,
148 struct can_frame *cf;
150 skb = alloc_can_skb(netdev, &cf);
153 memcpy(cf, msg, sizeof(*msg));
155 return netif_rx(skb);
160 * pop 1 entry from the DPRAM queue, and process
162 static int softing_handle_1(struct softing *card)
164 struct net_device *netdev;
165 struct softing_priv *priv;
167 struct can_frame msg;
168 int cnt = 0, lost_msg;
169 uint8_t fifo_rd, fifo_wr, cmd;
172 uint8_t buf[DPRAM_RX_SIZE];
174 memset(&msg, 0, sizeof(msg));
175 /* test for lost msgs */
176 lost_msg = ioread8(&card->dpram[DPRAM_RX_LOST]);
179 /* reset condition */
180 iowrite8(0, &card->dpram[DPRAM_RX_LOST]);
182 msg.can_id = CAN_ERR_FLAG | CAN_ERR_CRTL;
183 msg.can_dlc = CAN_ERR_DLC;
184 msg.data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
186 * service to all busses, we don't know which it was applicable
187 * but only service busses that are online
189 for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
190 netdev = card->net[j];
193 if (!canif_is_active(netdev))
194 /* a dead bus has no overflows */
196 ++netdev->stats.rx_over_errors;
197 softing_netdev_rx(netdev, &msg, ktime_set(0, 0));
199 /* prepare for other use */
200 memset(&msg, 0, sizeof(msg));
204 fifo_rd = ioread8(&card->dpram[DPRAM_RX_RD]);
205 fifo_wr = ioread8(&card->dpram[DPRAM_RX_WR]);
207 if (++fifo_rd >= DPRAM_RX_CNT)
209 if (fifo_wr == fifo_rd)
212 memcpy_fromio(buf, &card->dpram[DPRAM_RX + DPRAM_RX_SIZE*fifo_rd],
215 /* trigger dual port RAM */
216 iowrite8(fifo_rd, &card->dpram[DPRAM_RX_RD]);
221 /* not quite useful, probably the card has got out */
223 netdev = card->net[0];
225 netdev = card->net[1];
226 priv = netdev_priv(netdev);
229 uint8_t can_state, state;
233 msg.can_id = CAN_ERR_FLAG;
234 msg.can_dlc = CAN_ERR_DLC;
236 if (state & SF_MASK_BUSOFF) {
237 can_state = CAN_STATE_BUS_OFF;
238 msg.can_id |= CAN_ERR_BUSOFF;
239 state = STATE_BUSOFF;
240 } else if (state & SF_MASK_EPASSIVE) {
241 can_state = CAN_STATE_ERROR_PASSIVE;
242 msg.can_id |= CAN_ERR_CRTL;
243 msg.data[1] = CAN_ERR_CRTL_TX_PASSIVE;
244 state = STATE_EPASSIVE;
246 can_state = CAN_STATE_ERROR_ACTIVE;
247 msg.can_id |= CAN_ERR_CRTL;
248 state = STATE_EACTIVE;
251 iowrite8(state, &card->dpram[priv->index ?
252 DPRAM_INFO_BUSSTATE2 : DPRAM_INFO_BUSSTATE]);
254 tmp_u32 = le32_to_cpup((void *)ptr);
256 ktime = softing_raw2ktime(card, tmp_u32);
258 ++netdev->stats.rx_errors;
259 /* update internal status */
260 if (can_state != priv->can.state) {
261 priv->can.state = can_state;
262 if (can_state == CAN_STATE_ERROR_PASSIVE)
263 ++priv->can.can_stats.error_passive;
264 else if (can_state == CAN_STATE_BUS_OFF) {
265 /* this calls can_close_cleanup() */
267 netif_stop_queue(netdev);
269 /* trigger socketcan */
270 softing_netdev_rx(netdev, &msg, ktime);
275 msg.can_id |= CAN_RTR_FLAG;
276 msg.can_dlc = get_can_dlc(*ptr++);
278 msg.can_id |= CAN_EFF_FLAG;
279 msg.can_id |= le32_to_cpup((void *)ptr);
282 msg.can_id |= le16_to_cpup((void *)ptr);
286 tmp_u32 = le32_to_cpup((void *)ptr);
288 ktime = softing_raw2ktime(card, tmp_u32);
289 if (!(msg.can_id & CAN_RTR_FLAG))
290 memcpy(&msg.data[0], ptr, 8);
294 /* acknowledge, was tx msg */
296 skb = priv->can.echo_skb[priv->tx.echo_get];
299 can_get_echo_skb(netdev, priv->tx.echo_get);
301 if (priv->tx.echo_get >= TX_ECHO_SKB_MAX)
302 priv->tx.echo_get = 0;
303 if (priv->tx.pending)
305 if (card->tx.pending)
307 ++netdev->stats.tx_packets;
308 if (!(msg.can_id & CAN_RTR_FLAG))
309 netdev->stats.tx_bytes += msg.can_dlc;
313 ret = softing_netdev_rx(netdev, &msg, ktime);
314 if (ret == NET_RX_SUCCESS) {
315 ++netdev->stats.rx_packets;
316 if (!(msg.can_id & CAN_RTR_FLAG))
317 netdev->stats.rx_bytes += msg.can_dlc;
319 ++netdev->stats.rx_dropped;
328 * real interrupt handler
330 static irqreturn_t softing_irq_thread(int irq, void *dev_id)
332 struct softing *card = (struct softing *)dev_id;
333 struct net_device *netdev;
334 struct softing_priv *priv;
335 int j, offset, work_done;
338 spin_lock_bh(&card->spin);
339 while (softing_handle_1(card) > 0) {
340 ++card->irq.svc_count;
343 spin_unlock_bh(&card->spin);
344 /* resume tx queue's */
345 offset = card->tx.last_bus;
346 for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
347 if (card->tx.pending >= TXMAX)
349 netdev = card->net[(j + offset + 1) % card->pdat->nbus];
352 priv = netdev_priv(netdev);
353 if (!canif_is_active(netdev))
354 /* it makes no sense to wake dead busses */
356 if (priv->tx.pending >= TX_ECHO_SKB_MAX)
359 netif_wake_queue(netdev);
361 return work_done ? IRQ_HANDLED : IRQ_NONE;
365 * interrupt routines:
366 * schedule the 'real interrupt handler'
368 static irqreturn_t softing_irq_v2(int irq, void *dev_id)
370 struct softing *card = (struct softing *)dev_id;
373 ir = ioread8(&card->dpram[DPRAM_V2_IRQ_TOHOST]);
374 iowrite8(0, &card->dpram[DPRAM_V2_IRQ_TOHOST]);
375 return (1 == ir) ? IRQ_WAKE_THREAD : IRQ_NONE;
378 static irqreturn_t softing_irq_v1(int irq, void *dev_id)
380 struct softing *card = (struct softing *)dev_id;
383 ir = ioread8(&card->dpram[DPRAM_IRQ_TOHOST]);
384 iowrite8(0, &card->dpram[DPRAM_IRQ_TOHOST]);
385 return ir ? IRQ_WAKE_THREAD : IRQ_NONE;
389 * netdev/candev inter-operability
391 static int softing_netdev_open(struct net_device *ndev)
395 /* check or determine and set bittime */
396 ret = open_candev(ndev);
398 ret = softing_startstop(ndev, 1);
402 static int softing_netdev_stop(struct net_device *ndev)
406 netif_stop_queue(ndev);
408 /* softing cycle does close_candev() */
409 ret = softing_startstop(ndev, 0);
413 static int softing_candev_set_mode(struct net_device *ndev, enum can_mode mode)
419 /* softing_startstop does close_candev() */
420 ret = softing_startstop(ndev, 1);
430 * Softing device management helpers
432 int softing_enable_irq(struct softing *card, int enable)
438 } else if (card->irq.requested && !enable) {
439 free_irq(card->irq.nr, card);
440 card->irq.requested = 0;
441 } else if (!card->irq.requested && enable) {
442 ret = request_threaded_irq(card->irq.nr,
443 (card->pdat->generation >= 2) ?
444 softing_irq_v2 : softing_irq_v1,
445 softing_irq_thread, IRQF_SHARED,
446 dev_name(&card->pdev->dev), card);
448 dev_alert(&card->pdev->dev,
449 "request_threaded_irq(%u) failed\n",
453 card->irq.requested = 1;
458 static void softing_card_shutdown(struct softing *card)
462 if (mutex_lock_interruptible(&card->fw.lock))
463 /* return -ERESTARTSYS */;
467 if (card->irq.requested && card->irq.nr) {
468 free_irq(card->irq.nr, card);
469 card->irq.requested = 0;
472 if (card->pdat->enable_irq)
473 card->pdat->enable_irq(card->pdev, 0);
474 softing_set_reset_dpram(card);
475 if (card->pdat->reset)
476 card->pdat->reset(card->pdev, 1);
478 mutex_unlock(&card->fw.lock);
481 static int softing_card_boot(struct softing *card)
484 static const uint8_t stream[] = {
485 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, };
486 unsigned char back[sizeof(stream)];
488 if (mutex_lock_interruptible(&card->fw.lock))
491 mutex_unlock(&card->fw.lock);
495 if (card->pdat->enable_irq)
496 card->pdat->enable_irq(card->pdev, 1);
498 softing_set_reset_dpram(card);
499 if (card->pdat->reset)
500 card->pdat->reset(card->pdev, 1);
501 for (j = 0; (j + sizeof(stream)) < card->dpram_size;
502 j += sizeof(stream)) {
504 memcpy_toio(&card->dpram[j], stream, sizeof(stream));
507 memcpy_fromio(back, &card->dpram[j], sizeof(stream));
509 if (!memcmp(back, stream, sizeof(stream)))
511 /* memory is not equal */
512 dev_alert(&card->pdev->dev, "dpram failed at 0x%04x\n", j);
517 /* load boot firmware */
518 ret = softing_load_fw(card->pdat->boot.fw, card, card->dpram,
520 card->pdat->boot.offs - card->pdat->boot.addr);
523 /* load loader firmware */
524 ret = softing_load_fw(card->pdat->load.fw, card, card->dpram,
526 card->pdat->load.offs - card->pdat->load.addr);
530 if (card->pdat->reset)
531 card->pdat->reset(card->pdev, 0);
532 softing_clr_reset_dpram(card);
533 ret = softing_bootloader_command(card, 0, "card boot");
536 ret = softing_load_app_fw(card->pdat->app.fw, card);
540 ret = softing_chip_poweron(card);
545 mutex_unlock(&card->fw.lock);
549 if (card->pdat->enable_irq)
550 card->pdat->enable_irq(card->pdev, 0);
551 softing_set_reset_dpram(card);
552 if (card->pdat->reset)
553 card->pdat->reset(card->pdev, 1);
554 mutex_unlock(&card->fw.lock);
561 static ssize_t show_channel(struct device *dev, struct device_attribute *attr,
564 struct net_device *ndev = to_net_dev(dev);
565 struct softing_priv *priv = netdev2softing(ndev);
567 return sprintf(buf, "%i\n", priv->index);
570 static ssize_t show_chip(struct device *dev, struct device_attribute *attr,
573 struct net_device *ndev = to_net_dev(dev);
574 struct softing_priv *priv = netdev2softing(ndev);
576 return sprintf(buf, "%i\n", priv->chip);
579 static ssize_t show_output(struct device *dev, struct device_attribute *attr,
582 struct net_device *ndev = to_net_dev(dev);
583 struct softing_priv *priv = netdev2softing(ndev);
585 return sprintf(buf, "0x%02x\n", priv->output);
588 static ssize_t store_output(struct device *dev, struct device_attribute *attr,
589 const char *buf, size_t count)
591 struct net_device *ndev = to_net_dev(dev);
592 struct softing_priv *priv = netdev2softing(ndev);
593 struct softing *card = priv->card;
597 ret = kstrtoul(buf, 0, &val);
602 ret = mutex_lock_interruptible(&card->fw.lock);
605 if (netif_running(ndev)) {
606 mutex_unlock(&card->fw.lock);
610 mutex_unlock(&card->fw.lock);
614 static const DEVICE_ATTR(channel, S_IRUGO, show_channel, NULL);
615 static const DEVICE_ATTR(chip, S_IRUGO, show_chip, NULL);
616 static const DEVICE_ATTR(output, S_IRUGO | S_IWUSR, show_output, store_output);
618 static const struct attribute *const netdev_sysfs_attrs[] = {
619 &dev_attr_channel.attr,
621 &dev_attr_output.attr,
624 static const struct attribute_group netdev_sysfs_group = {
626 .attrs = (struct attribute **)netdev_sysfs_attrs,
629 static const struct net_device_ops softing_netdev_ops = {
630 .ndo_open = softing_netdev_open,
631 .ndo_stop = softing_netdev_stop,
632 .ndo_start_xmit = softing_netdev_start_xmit,
635 static const struct can_bittiming_const softing_btr_const = {
641 .sjw_max = 4, /* overruled */
643 .brp_max = 32, /* overruled */
648 static struct net_device *softing_netdev_create(struct softing *card,
651 struct net_device *netdev;
652 struct softing_priv *priv;
654 netdev = alloc_candev(sizeof(*priv), TX_ECHO_SKB_MAX);
656 dev_alert(&card->pdev->dev, "alloc_candev failed\n");
659 priv = netdev_priv(netdev);
660 priv->netdev = netdev;
662 memcpy(&priv->btr_const, &softing_btr_const, sizeof(priv->btr_const));
663 priv->btr_const.brp_max = card->pdat->max_brp;
664 priv->btr_const.sjw_max = card->pdat->max_sjw;
665 priv->can.bittiming_const = &priv->btr_const;
666 priv->can.clock.freq = 8000000;
667 priv->chip = chip_id;
668 priv->output = softing_default_output(netdev);
669 SET_NETDEV_DEV(netdev, &card->pdev->dev);
671 netdev->flags |= IFF_ECHO;
672 netdev->netdev_ops = &softing_netdev_ops;
673 priv->can.do_set_mode = softing_candev_set_mode;
674 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
679 static int softing_netdev_register(struct net_device *netdev)
683 netdev->sysfs_groups[0] = &netdev_sysfs_group;
684 ret = register_candev(netdev);
686 dev_alert(&netdev->dev, "register failed\n");
692 static void softing_netdev_cleanup(struct net_device *netdev)
694 unregister_candev(netdev);
699 * sysfs for Platform device
701 #define DEV_ATTR_RO(name, member) \
702 static ssize_t show_##name(struct device *dev, \
703 struct device_attribute *attr, char *buf) \
705 struct softing *card = platform_get_drvdata(to_platform_device(dev)); \
706 return sprintf(buf, "%u\n", card->member); \
708 static DEVICE_ATTR(name, 0444, show_##name, NULL)
710 #define DEV_ATTR_RO_STR(name, member) \
711 static ssize_t show_##name(struct device *dev, \
712 struct device_attribute *attr, char *buf) \
714 struct softing *card = platform_get_drvdata(to_platform_device(dev)); \
715 return sprintf(buf, "%s\n", card->member); \
717 static DEVICE_ATTR(name, 0444, show_##name, NULL)
719 DEV_ATTR_RO(serial, id.serial);
720 DEV_ATTR_RO_STR(firmware, pdat->app.fw);
721 DEV_ATTR_RO(firmware_version, id.fw_version);
722 DEV_ATTR_RO_STR(hardware, pdat->name);
723 DEV_ATTR_RO(hardware_version, id.hw_version);
724 DEV_ATTR_RO(license, id.license);
725 DEV_ATTR_RO(frequency, id.freq);
726 DEV_ATTR_RO(txpending, tx.pending);
728 static struct attribute *softing_pdev_attrs[] = {
729 &dev_attr_serial.attr,
730 &dev_attr_firmware.attr,
731 &dev_attr_firmware_version.attr,
732 &dev_attr_hardware.attr,
733 &dev_attr_hardware_version.attr,
734 &dev_attr_license.attr,
735 &dev_attr_frequency.attr,
736 &dev_attr_txpending.attr,
740 static const struct attribute_group softing_pdev_group = {
742 .attrs = softing_pdev_attrs,
748 static int softing_pdev_remove(struct platform_device *pdev)
750 struct softing *card = platform_get_drvdata(pdev);
753 /* first, disable card*/
754 softing_card_shutdown(card);
756 for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
759 softing_netdev_cleanup(card->net[j]);
762 sysfs_remove_group(&pdev->dev.kobj, &softing_pdev_group);
764 iounmap(card->dpram);
769 static int softing_pdev_probe(struct platform_device *pdev)
771 const struct softing_platform_data *pdat = pdev->dev.platform_data;
772 struct softing *card;
773 struct net_device *netdev;
774 struct softing_priv *priv;
775 struct resource *pres;
780 dev_warn(&pdev->dev, "no platform data\n");
783 if (pdat->nbus > ARRAY_SIZE(card->net)) {
784 dev_warn(&pdev->dev, "%u nets??\n", pdat->nbus);
788 card = kzalloc(sizeof(*card), GFP_KERNEL);
793 platform_set_drvdata(pdev, card);
794 mutex_init(&card->fw.lock);
795 spin_lock_init(&card->spin);
798 pres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
800 goto platform_resource_failed;
801 card->dpram_phys = pres->start;
802 card->dpram_size = resource_size(pres);
803 card->dpram = ioremap_nocache(card->dpram_phys, card->dpram_size);
805 dev_alert(&card->pdev->dev, "dpram ioremap failed\n");
809 pres = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
811 card->irq.nr = pres->start;
814 ret = softing_card_boot(card);
816 dev_alert(&pdev->dev, "failed to boot\n");
820 /* only now, the chip's are known */
821 card->id.freq = card->pdat->freq;
823 ret = sysfs_create_group(&pdev->dev.kobj, &softing_pdev_group);
825 dev_alert(&card->pdev->dev, "sysfs failed\n");
829 for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
830 card->net[j] = netdev =
831 softing_netdev_create(card, card->id.chip[j]);
833 dev_alert(&pdev->dev, "failed to make can[%i]", j);
837 priv = netdev_priv(card->net[j]);
839 ret = softing_netdev_register(netdev);
843 dev_alert(&card->pdev->dev,
844 "failed to register can[%i]\n", j);
848 dev_info(&card->pdev->dev, "%s ready.\n", card->pdat->name);
852 for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
855 softing_netdev_cleanup(card->net[j]);
857 sysfs_remove_group(&pdev->dev.kobj, &softing_pdev_group);
859 softing_card_shutdown(card);
861 iounmap(card->dpram);
863 platform_resource_failed:
868 static struct platform_driver softing_driver = {
871 .owner = THIS_MODULE,
873 .probe = softing_pdev_probe,
874 .remove = softing_pdev_remove,
877 module_platform_driver(softing_driver);
879 MODULE_ALIAS("platform:softing");
880 MODULE_DESCRIPTION("Softing DPRAM CAN driver");
882 MODULE_LICENSE("GPL v2");