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, see <http://www.gnu.org/licenses/>.
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/interrupt.h>
26 #define TX_ECHO_SKB_MAX (((TXMAX+1)/2)-1)
29 * test is a specific CAN netdev
30 * is online (ie. up 'n running, not sleeping, not busoff
32 static inline int canif_is_active(struct net_device *netdev)
34 struct can_priv *can = netdev_priv(netdev);
36 if (!netif_running(netdev))
38 return (can->state <= CAN_STATE_ERROR_PASSIVE);
42 static inline void softing_set_reset_dpram(struct softing *card)
44 if (card->pdat->generation >= 2) {
45 spin_lock_bh(&card->spin);
46 iowrite8(ioread8(&card->dpram[DPRAM_V2_RESET]) & ~1,
47 &card->dpram[DPRAM_V2_RESET]);
48 spin_unlock_bh(&card->spin);
52 static inline void softing_clr_reset_dpram(struct softing *card)
54 if (card->pdat->generation >= 2) {
55 spin_lock_bh(&card->spin);
56 iowrite8(ioread8(&card->dpram[DPRAM_V2_RESET]) | 1,
57 &card->dpram[DPRAM_V2_RESET]);
58 spin_unlock_bh(&card->spin);
62 /* trigger the tx queue-ing */
63 static netdev_tx_t softing_netdev_start_xmit(struct sk_buff *skb,
64 struct net_device *dev)
66 struct softing_priv *priv = netdev_priv(dev);
67 struct softing *card = priv->card;
70 uint8_t fifo_wr, fifo_rd;
71 struct can_frame *cf = (struct can_frame *)skb->data;
72 uint8_t buf[DPRAM_TX_SIZE];
74 if (can_dropped_invalid_skb(dev, skb))
77 spin_lock(&card->spin);
81 (card->tx.pending >= TXMAX) ||
82 (priv->tx.pending >= TX_ECHO_SKB_MAX))
84 fifo_wr = ioread8(&card->dpram[DPRAM_TX_WR]);
85 fifo_rd = ioread8(&card->dpram[DPRAM_TX_RD]);
86 if (fifo_wr == fifo_rd)
89 memset(buf, 0, sizeof(buf));
92 if (cf->can_id & CAN_RTR_FLAG)
94 if (cf->can_id & CAN_EFF_FLAG)
100 *ptr++ = (cf->can_id >> 0);
101 *ptr++ = (cf->can_id >> 8);
102 if (cf->can_id & CAN_EFF_FLAG) {
103 *ptr++ = (cf->can_id >> 16);
104 *ptr++ = (cf->can_id >> 24);
106 /* increment 1, not 2 as you might think */
109 if (!(cf->can_id & CAN_RTR_FLAG))
110 memcpy(ptr, &cf->data[0], cf->can_dlc);
111 memcpy_toio(&card->dpram[DPRAM_TX + DPRAM_TX_SIZE * fifo_wr],
113 if (++fifo_wr >= DPRAM_TX_CNT)
115 iowrite8(fifo_wr, &card->dpram[DPRAM_TX_WR]);
116 card->tx.last_bus = priv->index;
119 can_put_echo_skb(skb, dev, priv->tx.echo_put);
121 if (priv->tx.echo_put >= TX_ECHO_SKB_MAX)
122 priv->tx.echo_put = 0;
123 /* can_put_echo_skb() saves the skb, safe to return TX_OK */
126 spin_unlock(&card->spin);
127 if (card->tx.pending >= TXMAX) {
129 for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
131 netif_stop_queue(card->net[j]);
134 if (ret != NETDEV_TX_OK)
135 netif_stop_queue(dev);
141 * shortcut for skb delivery
143 int softing_netdev_rx(struct net_device *netdev, const struct can_frame *msg,
147 struct can_frame *cf;
149 skb = alloc_can_skb(netdev, &cf);
152 memcpy(cf, msg, sizeof(*msg));
154 return netif_rx(skb);
159 * pop 1 entry from the DPRAM queue, and process
161 static int softing_handle_1(struct softing *card)
163 struct net_device *netdev;
164 struct softing_priv *priv;
166 struct can_frame msg;
167 int cnt = 0, lost_msg;
168 uint8_t fifo_rd, fifo_wr, cmd;
171 uint8_t buf[DPRAM_RX_SIZE];
173 memset(&msg, 0, sizeof(msg));
174 /* test for lost msgs */
175 lost_msg = ioread8(&card->dpram[DPRAM_RX_LOST]);
178 /* reset condition */
179 iowrite8(0, &card->dpram[DPRAM_RX_LOST]);
181 msg.can_id = CAN_ERR_FLAG | CAN_ERR_CRTL;
182 msg.can_dlc = CAN_ERR_DLC;
183 msg.data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
185 * service to all busses, we don't know which it was applicable
186 * but only service busses that are online
188 for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
189 netdev = card->net[j];
192 if (!canif_is_active(netdev))
193 /* a dead bus has no overflows */
195 ++netdev->stats.rx_over_errors;
196 softing_netdev_rx(netdev, &msg, ktime_set(0, 0));
198 /* prepare for other use */
199 memset(&msg, 0, sizeof(msg));
203 fifo_rd = ioread8(&card->dpram[DPRAM_RX_RD]);
204 fifo_wr = ioread8(&card->dpram[DPRAM_RX_WR]);
206 if (++fifo_rd >= DPRAM_RX_CNT)
208 if (fifo_wr == fifo_rd)
211 memcpy_fromio(buf, &card->dpram[DPRAM_RX + DPRAM_RX_SIZE*fifo_rd],
214 /* trigger dual port RAM */
215 iowrite8(fifo_rd, &card->dpram[DPRAM_RX_RD]);
220 /* not quite useful, probably the card has got out */
222 netdev = card->net[0];
224 netdev = card->net[1];
225 priv = netdev_priv(netdev);
228 uint8_t can_state, state;
232 msg.can_id = CAN_ERR_FLAG;
233 msg.can_dlc = CAN_ERR_DLC;
235 if (state & SF_MASK_BUSOFF) {
236 can_state = CAN_STATE_BUS_OFF;
237 msg.can_id |= CAN_ERR_BUSOFF;
238 state = STATE_BUSOFF;
239 } else if (state & SF_MASK_EPASSIVE) {
240 can_state = CAN_STATE_ERROR_PASSIVE;
241 msg.can_id |= CAN_ERR_CRTL;
242 msg.data[1] = CAN_ERR_CRTL_TX_PASSIVE;
243 state = STATE_EPASSIVE;
245 can_state = CAN_STATE_ERROR_ACTIVE;
246 msg.can_id |= CAN_ERR_CRTL;
247 state = STATE_EACTIVE;
250 iowrite8(state, &card->dpram[priv->index ?
251 DPRAM_INFO_BUSSTATE2 : DPRAM_INFO_BUSSTATE]);
253 tmp_u32 = le32_to_cpup((void *)ptr);
255 ktime = softing_raw2ktime(card, tmp_u32);
257 ++netdev->stats.rx_errors;
258 /* update internal status */
259 if (can_state != priv->can.state) {
260 priv->can.state = can_state;
261 if (can_state == CAN_STATE_ERROR_PASSIVE)
262 ++priv->can.can_stats.error_passive;
263 else if (can_state == CAN_STATE_BUS_OFF) {
264 /* this calls can_close_cleanup() */
266 netif_stop_queue(netdev);
268 /* trigger socketcan */
269 softing_netdev_rx(netdev, &msg, ktime);
274 msg.can_id |= CAN_RTR_FLAG;
275 msg.can_dlc = get_can_dlc(*ptr++);
277 msg.can_id |= CAN_EFF_FLAG;
278 msg.can_id |= le32_to_cpup((void *)ptr);
281 msg.can_id |= le16_to_cpup((void *)ptr);
285 tmp_u32 = le32_to_cpup((void *)ptr);
287 ktime = softing_raw2ktime(card, tmp_u32);
288 if (!(msg.can_id & CAN_RTR_FLAG))
289 memcpy(&msg.data[0], ptr, 8);
293 /* acknowledge, was tx msg */
295 skb = priv->can.echo_skb[priv->tx.echo_get];
298 can_get_echo_skb(netdev, priv->tx.echo_get);
300 if (priv->tx.echo_get >= TX_ECHO_SKB_MAX)
301 priv->tx.echo_get = 0;
302 if (priv->tx.pending)
304 if (card->tx.pending)
306 ++netdev->stats.tx_packets;
307 if (!(msg.can_id & CAN_RTR_FLAG))
308 netdev->stats.tx_bytes += msg.can_dlc;
312 ret = softing_netdev_rx(netdev, &msg, ktime);
313 if (ret == NET_RX_SUCCESS) {
314 ++netdev->stats.rx_packets;
315 if (!(msg.can_id & CAN_RTR_FLAG))
316 netdev->stats.rx_bytes += msg.can_dlc;
318 ++netdev->stats.rx_dropped;
327 * real interrupt handler
329 static irqreturn_t softing_irq_thread(int irq, void *dev_id)
331 struct softing *card = (struct softing *)dev_id;
332 struct net_device *netdev;
333 struct softing_priv *priv;
334 int j, offset, work_done;
337 spin_lock_bh(&card->spin);
338 while (softing_handle_1(card) > 0) {
339 ++card->irq.svc_count;
342 spin_unlock_bh(&card->spin);
343 /* resume tx queue's */
344 offset = card->tx.last_bus;
345 for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
346 if (card->tx.pending >= TXMAX)
348 netdev = card->net[(j + offset + 1) % card->pdat->nbus];
351 priv = netdev_priv(netdev);
352 if (!canif_is_active(netdev))
353 /* it makes no sense to wake dead busses */
355 if (priv->tx.pending >= TX_ECHO_SKB_MAX)
358 netif_wake_queue(netdev);
360 return work_done ? IRQ_HANDLED : IRQ_NONE;
364 * interrupt routines:
365 * schedule the 'real interrupt handler'
367 static irqreturn_t softing_irq_v2(int irq, void *dev_id)
369 struct softing *card = (struct softing *)dev_id;
372 ir = ioread8(&card->dpram[DPRAM_V2_IRQ_TOHOST]);
373 iowrite8(0, &card->dpram[DPRAM_V2_IRQ_TOHOST]);
374 return (1 == ir) ? IRQ_WAKE_THREAD : IRQ_NONE;
377 static irqreturn_t softing_irq_v1(int irq, void *dev_id)
379 struct softing *card = (struct softing *)dev_id;
382 ir = ioread8(&card->dpram[DPRAM_IRQ_TOHOST]);
383 iowrite8(0, &card->dpram[DPRAM_IRQ_TOHOST]);
384 return ir ? IRQ_WAKE_THREAD : IRQ_NONE;
388 * netdev/candev inter-operability
390 static int softing_netdev_open(struct net_device *ndev)
394 /* check or determine and set bittime */
395 ret = open_candev(ndev);
397 ret = softing_startstop(ndev, 1);
401 static int softing_netdev_stop(struct net_device *ndev)
405 netif_stop_queue(ndev);
407 /* softing cycle does close_candev() */
408 ret = softing_startstop(ndev, 0);
412 static int softing_candev_set_mode(struct net_device *ndev, enum can_mode mode)
418 /* softing_startstop does close_candev() */
419 ret = softing_startstop(ndev, 1);
429 * Softing device management helpers
431 int softing_enable_irq(struct softing *card, int enable)
437 } else if (card->irq.requested && !enable) {
438 free_irq(card->irq.nr, card);
439 card->irq.requested = 0;
440 } else if (!card->irq.requested && enable) {
441 ret = request_threaded_irq(card->irq.nr,
442 (card->pdat->generation >= 2) ?
443 softing_irq_v2 : softing_irq_v1,
444 softing_irq_thread, IRQF_SHARED,
445 dev_name(&card->pdev->dev), card);
447 dev_alert(&card->pdev->dev,
448 "request_threaded_irq(%u) failed\n",
452 card->irq.requested = 1;
457 static void softing_card_shutdown(struct softing *card)
461 if (mutex_lock_interruptible(&card->fw.lock))
462 /* return -ERESTARTSYS */;
466 if (card->irq.requested && card->irq.nr) {
467 free_irq(card->irq.nr, card);
468 card->irq.requested = 0;
471 if (card->pdat->enable_irq)
472 card->pdat->enable_irq(card->pdev, 0);
473 softing_set_reset_dpram(card);
474 if (card->pdat->reset)
475 card->pdat->reset(card->pdev, 1);
477 mutex_unlock(&card->fw.lock);
480 static int softing_card_boot(struct softing *card)
483 static const uint8_t stream[] = {
484 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, };
485 unsigned char back[sizeof(stream)];
487 if (mutex_lock_interruptible(&card->fw.lock))
490 mutex_unlock(&card->fw.lock);
494 if (card->pdat->enable_irq)
495 card->pdat->enable_irq(card->pdev, 1);
497 softing_set_reset_dpram(card);
498 if (card->pdat->reset)
499 card->pdat->reset(card->pdev, 1);
500 for (j = 0; (j + sizeof(stream)) < card->dpram_size;
501 j += sizeof(stream)) {
503 memcpy_toio(&card->dpram[j], stream, sizeof(stream));
506 memcpy_fromio(back, &card->dpram[j], sizeof(stream));
508 if (!memcmp(back, stream, sizeof(stream)))
510 /* memory is not equal */
511 dev_alert(&card->pdev->dev, "dpram failed at 0x%04x\n", j);
516 /* load boot firmware */
517 ret = softing_load_fw(card->pdat->boot.fw, card, card->dpram,
519 card->pdat->boot.offs - card->pdat->boot.addr);
522 /* load loader firmware */
523 ret = softing_load_fw(card->pdat->load.fw, card, card->dpram,
525 card->pdat->load.offs - card->pdat->load.addr);
529 if (card->pdat->reset)
530 card->pdat->reset(card->pdev, 0);
531 softing_clr_reset_dpram(card);
532 ret = softing_bootloader_command(card, 0, "card boot");
535 ret = softing_load_app_fw(card->pdat->app.fw, card);
539 ret = softing_chip_poweron(card);
544 mutex_unlock(&card->fw.lock);
548 if (card->pdat->enable_irq)
549 card->pdat->enable_irq(card->pdev, 0);
550 softing_set_reset_dpram(card);
551 if (card->pdat->reset)
552 card->pdat->reset(card->pdev, 1);
553 mutex_unlock(&card->fw.lock);
560 static ssize_t show_channel(struct device *dev, struct device_attribute *attr,
563 struct net_device *ndev = to_net_dev(dev);
564 struct softing_priv *priv = netdev2softing(ndev);
566 return sprintf(buf, "%i\n", priv->index);
569 static ssize_t show_chip(struct device *dev, struct device_attribute *attr,
572 struct net_device *ndev = to_net_dev(dev);
573 struct softing_priv *priv = netdev2softing(ndev);
575 return sprintf(buf, "%i\n", priv->chip);
578 static ssize_t show_output(struct device *dev, struct device_attribute *attr,
581 struct net_device *ndev = to_net_dev(dev);
582 struct softing_priv *priv = netdev2softing(ndev);
584 return sprintf(buf, "0x%02x\n", priv->output);
587 static ssize_t store_output(struct device *dev, struct device_attribute *attr,
588 const char *buf, size_t count)
590 struct net_device *ndev = to_net_dev(dev);
591 struct softing_priv *priv = netdev2softing(ndev);
592 struct softing *card = priv->card;
596 ret = kstrtoul(buf, 0, &val);
601 ret = mutex_lock_interruptible(&card->fw.lock);
604 if (netif_running(ndev)) {
605 mutex_unlock(&card->fw.lock);
609 mutex_unlock(&card->fw.lock);
613 static const DEVICE_ATTR(channel, S_IRUGO, show_channel, NULL);
614 static const DEVICE_ATTR(chip, S_IRUGO, show_chip, NULL);
615 static const DEVICE_ATTR(output, S_IRUGO | S_IWUSR, show_output, store_output);
617 static const struct attribute *const netdev_sysfs_attrs[] = {
618 &dev_attr_channel.attr,
620 &dev_attr_output.attr,
623 static const struct attribute_group netdev_sysfs_group = {
625 .attrs = (struct attribute **)netdev_sysfs_attrs,
628 static const struct net_device_ops softing_netdev_ops = {
629 .ndo_open = softing_netdev_open,
630 .ndo_stop = softing_netdev_stop,
631 .ndo_start_xmit = softing_netdev_start_xmit,
634 static const struct can_bittiming_const softing_btr_const = {
640 .sjw_max = 4, /* overruled */
642 .brp_max = 32, /* overruled */
647 static struct net_device *softing_netdev_create(struct softing *card,
650 struct net_device *netdev;
651 struct softing_priv *priv;
653 netdev = alloc_candev(sizeof(*priv), TX_ECHO_SKB_MAX);
655 dev_alert(&card->pdev->dev, "alloc_candev failed\n");
658 priv = netdev_priv(netdev);
659 priv->netdev = netdev;
661 memcpy(&priv->btr_const, &softing_btr_const, sizeof(priv->btr_const));
662 priv->btr_const.brp_max = card->pdat->max_brp;
663 priv->btr_const.sjw_max = card->pdat->max_sjw;
664 priv->can.bittiming_const = &priv->btr_const;
665 priv->can.clock.freq = 8000000;
666 priv->chip = chip_id;
667 priv->output = softing_default_output(netdev);
668 SET_NETDEV_DEV(netdev, &card->pdev->dev);
670 netdev->flags |= IFF_ECHO;
671 netdev->netdev_ops = &softing_netdev_ops;
672 priv->can.do_set_mode = softing_candev_set_mode;
673 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
678 static int softing_netdev_register(struct net_device *netdev)
682 netdev->sysfs_groups[0] = &netdev_sysfs_group;
683 ret = register_candev(netdev);
685 dev_alert(&netdev->dev, "register failed\n");
691 static void softing_netdev_cleanup(struct net_device *netdev)
693 unregister_candev(netdev);
698 * sysfs for Platform device
700 #define DEV_ATTR_RO(name, member) \
701 static ssize_t show_##name(struct device *dev, \
702 struct device_attribute *attr, char *buf) \
704 struct softing *card = platform_get_drvdata(to_platform_device(dev)); \
705 return sprintf(buf, "%u\n", card->member); \
707 static DEVICE_ATTR(name, 0444, show_##name, NULL)
709 #define DEV_ATTR_RO_STR(name, member) \
710 static ssize_t show_##name(struct device *dev, \
711 struct device_attribute *attr, char *buf) \
713 struct softing *card = platform_get_drvdata(to_platform_device(dev)); \
714 return sprintf(buf, "%s\n", card->member); \
716 static DEVICE_ATTR(name, 0444, show_##name, NULL)
718 DEV_ATTR_RO(serial, id.serial);
719 DEV_ATTR_RO_STR(firmware, pdat->app.fw);
720 DEV_ATTR_RO(firmware_version, id.fw_version);
721 DEV_ATTR_RO_STR(hardware, pdat->name);
722 DEV_ATTR_RO(hardware_version, id.hw_version);
723 DEV_ATTR_RO(license, id.license);
724 DEV_ATTR_RO(frequency, id.freq);
725 DEV_ATTR_RO(txpending, tx.pending);
727 static struct attribute *softing_pdev_attrs[] = {
728 &dev_attr_serial.attr,
729 &dev_attr_firmware.attr,
730 &dev_attr_firmware_version.attr,
731 &dev_attr_hardware.attr,
732 &dev_attr_hardware_version.attr,
733 &dev_attr_license.attr,
734 &dev_attr_frequency.attr,
735 &dev_attr_txpending.attr,
739 static const struct attribute_group softing_pdev_group = {
741 .attrs = softing_pdev_attrs,
747 static int softing_pdev_remove(struct platform_device *pdev)
749 struct softing *card = platform_get_drvdata(pdev);
752 /* first, disable card*/
753 softing_card_shutdown(card);
755 for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
758 softing_netdev_cleanup(card->net[j]);
761 sysfs_remove_group(&pdev->dev.kobj, &softing_pdev_group);
763 iounmap(card->dpram);
768 static int softing_pdev_probe(struct platform_device *pdev)
770 const struct softing_platform_data *pdat = dev_get_platdata(&pdev->dev);
771 struct softing *card;
772 struct net_device *netdev;
773 struct softing_priv *priv;
774 struct resource *pres;
779 dev_warn(&pdev->dev, "no platform data\n");
782 if (pdat->nbus > ARRAY_SIZE(card->net)) {
783 dev_warn(&pdev->dev, "%u nets??\n", pdat->nbus);
787 card = kzalloc(sizeof(*card), GFP_KERNEL);
792 platform_set_drvdata(pdev, card);
793 mutex_init(&card->fw.lock);
794 spin_lock_init(&card->spin);
797 pres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
799 goto platform_resource_failed;
800 card->dpram_phys = pres->start;
801 card->dpram_size = resource_size(pres);
802 card->dpram = ioremap_nocache(card->dpram_phys, card->dpram_size);
804 dev_alert(&card->pdev->dev, "dpram ioremap failed\n");
808 pres = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
810 card->irq.nr = pres->start;
813 ret = softing_card_boot(card);
815 dev_alert(&pdev->dev, "failed to boot\n");
819 /* only now, the chip's are known */
820 card->id.freq = card->pdat->freq;
822 ret = sysfs_create_group(&pdev->dev.kobj, &softing_pdev_group);
824 dev_alert(&card->pdev->dev, "sysfs failed\n");
828 for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
829 card->net[j] = netdev =
830 softing_netdev_create(card, card->id.chip[j]);
832 dev_alert(&pdev->dev, "failed to make can[%i]", j);
836 priv = netdev_priv(card->net[j]);
838 ret = softing_netdev_register(netdev);
842 dev_alert(&card->pdev->dev,
843 "failed to register can[%i]\n", j);
847 dev_info(&card->pdev->dev, "%s ready.\n", card->pdat->name);
851 for (j = 0; j < ARRAY_SIZE(card->net); ++j) {
854 softing_netdev_cleanup(card->net[j]);
856 sysfs_remove_group(&pdev->dev.kobj, &softing_pdev_group);
858 softing_card_shutdown(card);
860 iounmap(card->dpram);
862 platform_resource_failed:
867 static struct platform_driver softing_driver = {
870 .owner = THIS_MODULE,
872 .probe = softing_pdev_probe,
873 .remove = softing_pdev_remove,
876 module_platform_driver(softing_driver);
878 MODULE_ALIAS("platform:softing");
879 MODULE_DESCRIPTION("Softing DPRAM CAN driver");
880 MODULE_AUTHOR("Kurt Van Dijck <kurt.van.dijck@eia.be>");
881 MODULE_LICENSE("GPL v2");