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