23 #include <linux/errno.h>
24 #include <linux/if_arp.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/netdevice.h>
31 #include <linux/rtnetlink.h>
34 #include <linux/string.h>
35 #include <linux/types.h>
40 #include <mach/board.h>
42 #define AT91_MB_MASK(i) ((1 << (i)) - 1)
60 #define AT91_MMR(i) (enum at91_reg)(0x200 + ((i) * 0x20))
61 #define AT91_MAM(i) (enum at91_reg)(0x204 + ((i) * 0x20))
62 #define AT91_MID(i) (enum at91_reg)(0x208 + ((i) * 0x20))
63 #define AT91_MFID(i) (enum at91_reg)(0x20C + ((i) * 0x20))
64 #define AT91_MSR(i) (enum at91_reg)(0x210 + ((i) * 0x20))
65 #define AT91_MDL(i) (enum at91_reg)(0x214 + ((i) * 0x20))
66 #define AT91_MDH(i) (enum at91_reg)(0x218 + ((i) * 0x20))
67 #define AT91_MCR(i) (enum at91_reg)(0x21C + ((i) * 0x20))
70 #define AT91_MR_CANEN BIT(0)
71 #define AT91_MR_LPM BIT(1)
72 #define AT91_MR_ABM BIT(2)
73 #define AT91_MR_OVL BIT(3)
74 #define AT91_MR_TEOF BIT(4)
75 #define AT91_MR_TTM BIT(5)
76 #define AT91_MR_TIMFRZ BIT(6)
77 #define AT91_MR_DRPT BIT(7)
79 #define AT91_SR_RBSY BIT(29)
81 #define AT91_MMR_PRIO_SHIFT (16)
83 #define AT91_MID_MIDE BIT(29)
85 #define AT91_MSR_MRTR BIT(20)
86 #define AT91_MSR_MABT BIT(22)
87 #define AT91_MSR_MRDY BIT(23)
88 #define AT91_MSR_MMI BIT(24)
90 #define AT91_MCR_MRTR BIT(20)
91 #define AT91_MCR_MTCR BIT(23)
104 #define AT91_IRQ_ERRA (1 << 16)
105 #define AT91_IRQ_WARN (1 << 17)
106 #define AT91_IRQ_ERRP (1 << 18)
107 #define AT91_IRQ_BOFF (1 << 19)
108 #define AT91_IRQ_SLEEP (1 << 20)
109 #define AT91_IRQ_WAKEUP (1 << 21)
110 #define AT91_IRQ_TOVF (1 << 22)
111 #define AT91_IRQ_TSTP (1 << 23)
112 #define AT91_IRQ_CERR (1 << 24)
113 #define AT91_IRQ_SERR (1 << 25)
114 #define AT91_IRQ_AERR (1 << 26)
115 #define AT91_IRQ_FERR (1 << 27)
116 #define AT91_IRQ_BERR (1 << 28)
118 #define AT91_IRQ_ERR_ALL (0x1fff0000)
119 #define AT91_IRQ_ERR_FRAME (AT91_IRQ_CERR | AT91_IRQ_SERR | \
120 AT91_IRQ_AERR | AT91_IRQ_FERR | AT91_IRQ_BERR)
121 #define AT91_IRQ_ERR_LINE (AT91_IRQ_ERRA | AT91_IRQ_WARN | \
122 AT91_IRQ_ERRP | AT91_IRQ_BOFF)
124 #define AT91_IRQ_ALL (0x1fffffff)
174 .name = KBUILD_MODNAME,
185 #define AT91_IS(_model) \
186 static inline int at91_is_sam##_model(const struct at91_priv *priv) \
188 return priv->devtype_data.type == AT91_DEVTYPE_SAM##_model; \
194 static inline unsigned int get_mb_rx_first(
const struct at91_priv *
priv)
199 static inline unsigned int get_mb_rx_last(
const struct at91_priv *
priv)
204 static inline unsigned int get_mb_rx_split(
const struct at91_priv *
priv)
209 static inline unsigned int get_mb_rx_num(
const struct at91_priv *
priv)
211 return get_mb_rx_last(priv) - get_mb_rx_first(priv) + 1;
214 static inline unsigned int get_mb_rx_low_last(
const struct at91_priv *
priv)
216 return get_mb_rx_split(priv) - 1;
219 static inline unsigned int get_mb_rx_low_mask(
const struct at91_priv *
priv)
225 static inline unsigned int get_mb_tx_shift(
const struct at91_priv *priv)
230 static inline unsigned int get_mb_tx_num(
const struct at91_priv *priv)
232 return 1 << get_mb_tx_shift(priv);
235 static inline unsigned int get_mb_tx_first(
const struct at91_priv *priv)
237 return get_mb_rx_last(priv) + 1;
240 static inline unsigned int get_mb_tx_last(
const struct at91_priv *priv)
242 return get_mb_tx_first(priv) + get_mb_tx_num(priv) - 1;
245 static inline unsigned int get_next_prio_shift(
const struct at91_priv *priv)
247 return get_mb_tx_shift(priv);
250 static inline unsigned int get_next_prio_mask(
const struct at91_priv *priv)
252 return 0xf << get_mb_tx_shift(priv);
255 static inline unsigned int get_next_mb_mask(
const struct at91_priv *priv)
260 static inline unsigned int get_next_mask(
const struct at91_priv *priv)
262 return get_next_mb_mask(priv) | get_next_prio_mask(priv);
265 static inline unsigned int get_irq_mb_rx(
const struct at91_priv *priv)
271 static inline unsigned int get_irq_mb_tx(
const struct at91_priv *priv)
277 static inline unsigned int get_tx_next_mb(
const struct at91_priv *priv)
279 return (priv->
tx_next & get_next_mb_mask(priv)) + get_mb_tx_first(priv);
282 static inline unsigned int get_tx_next_prio(
const struct at91_priv *priv)
284 return (priv->
tx_next >> get_next_prio_shift(priv)) & 0xf;
287 static inline unsigned int get_tx_echo_mb(
const struct at91_priv *priv)
289 return (priv->
tx_echo & get_next_mb_mask(priv)) + get_mb_tx_first(priv);
303 static inline void set_mb_mode_prio(
const struct at91_priv *priv,
306 at91_write(priv,
AT91_MMR(mb), (mode << 24) | (prio << 16));
309 static inline void set_mb_mode(
const struct at91_priv *priv,
unsigned int mb,
312 set_mb_mode_prio(priv, mb, mode, 0);
315 static inline u32 at91_can_id_to_reg_mid(
canid_t can_id)
330 static void at91_transceiver_switch(
const struct at91_priv *priv,
int on)
332 if (priv->
pdata && priv->
pdata->transceiver_switch)
333 priv->
pdata->transceiver_switch(on);
338 struct at91_priv *priv = netdev_priv(dev);
349 reg_mid = at91_can_id_to_reg_mid(priv->
mb0_id);
350 for (i = 0; i < get_mb_rx_first(priv); i++) {
352 at91_write(priv,
AT91_MID(i), reg_mid);
356 for (i = get_mb_rx_first(priv); i < get_mb_rx_last(priv); i++)
361 for (i = get_mb_rx_first(priv); i <= get_mb_rx_last(priv); i++) {
367 for (i = get_mb_tx_first(priv); i <= get_mb_tx_last(priv); i++)
372 priv->
rx_next = get_mb_rx_first(priv);
375 static int at91_set_bittiming(
struct net_device *dev)
377 const struct at91_priv *priv = netdev_priv(dev);
382 ((bt->
brp - 1) << 16) | ((bt->
sjw - 1) << 12) |
386 netdev_info(dev,
"writing AT91_BR: 0x%08x\n", reg_br);
388 at91_write(priv,
AT91_BR, reg_br);
393 static int at91_get_berr_counter(
const struct net_device *dev,
396 const struct at91_priv *priv = netdev_priv(dev);
399 bec->
rxerr = reg_ecr & 0xff;
400 bec->
txerr = reg_ecr >> 16;
405 static void at91_chip_start(
struct net_device *dev)
407 struct at91_priv *priv = netdev_priv(dev);
414 reg_mr = at91_read(priv,
AT91_MR);
417 at91_set_bittiming(dev);
418 at91_setup_mailboxes(dev);
419 at91_transceiver_switch(priv, 1);
429 at91_write(priv,
AT91_IER, reg_ier);
434 struct at91_priv *priv = netdev_priv(dev);
440 reg_mr = at91_read(priv,
AT91_MR);
443 at91_transceiver_switch(priv, 0);
472 struct at91_priv *priv = netdev_priv(dev);
475 unsigned int mb, prio;
476 u32 reg_mid, reg_mcr;
478 if (can_dropped_invalid_skb(dev, skb))
481 mb = get_tx_next_mb(priv);
482 prio = get_tx_next_prio(priv);
485 netif_stop_queue(dev);
487 netdev_err(dev,
"BUG! TX buffer full when queue awake!\n");
490 reg_mid = at91_can_id_to_reg_mid(cf->
can_id);
496 at91_write(priv,
AT91_MID(mb), reg_mid);
499 at91_write(priv,
AT91_MDL(mb), *(
u32 *)(cf->data + 0));
500 at91_write(priv,
AT91_MDH(mb), *(
u32 *)(cf->data + 4));
503 at91_write(priv,
AT91_MCR(mb), reg_mcr);
519 if (!(at91_read(priv,
AT91_MSR(get_tx_next_mb(priv))) &
521 (priv->
tx_next & get_next_mask(priv)) == 0)
522 netif_stop_queue(dev);
525 at91_write(priv,
AT91_IER, 1 << mb);
536 static inline void at91_activate_rx_low(
const struct at91_priv *priv)
538 u32 mask = get_mb_rx_low_mask(priv);
549 static inline void at91_activate_rx_mb(
const struct at91_priv *priv,
560 static void at91_rx_overflow_err(
struct net_device *dev)
591 static void at91_read_mb(
struct net_device *dev,
unsigned int mb,
594 const struct at91_priv *priv = netdev_priv(dev);
595 u32 reg_msr, reg_mid;
597 reg_mid = at91_read(priv,
AT91_MID(mb));
603 reg_msr = at91_read(priv,
AT91_MSR(mb));
609 *(
u32 *)(cf->data + 0) = at91_read(priv,
AT91_MDL(mb));
610 *(
u32 *)(cf->data + 4) = at91_read(priv,
AT91_MDH(mb));
614 at91_write(priv,
AT91_MID(mb), AT91_MID_MIDE);
617 at91_rx_overflow_err(dev);
628 static void at91_read_msg(
struct net_device *dev,
unsigned int mb)
640 at91_read_mb(dev, mb, cf);
696 static int at91_poll_rx(
struct net_device *dev,
int quota)
698 struct at91_priv *priv = netdev_priv(dev);
700 const unsigned long *
addr = (
unsigned long *)®_sr;
704 if (priv->
rx_next > get_mb_rx_low_last(priv) &&
705 reg_sr & get_mb_rx_low_mask(priv))
707 "order of incoming frames cannot be guaranteed\n");
711 mb < get_mb_tx_first(priv) && quota > 0;
712 reg_sr = at91_read(priv,
AT91_SR),
714 at91_read_msg(dev, mb);
717 if (mb == get_mb_rx_low_last(priv))
719 at91_activate_rx_low(priv);
720 else if (mb > get_mb_rx_low_last(priv))
722 at91_activate_rx_mb(priv, mb);
729 if (priv->
rx_next > get_mb_rx_low_last(priv) &&
730 quota > 0 && mb > get_mb_rx_last(priv)) {
731 priv->
rx_next = get_mb_rx_first(priv);
738 static void at91_poll_err_frame(
struct net_device *dev,
741 struct at91_priv *priv = netdev_priv(dev);
746 dev->
stats.rx_errors++;
747 priv->
can.can_stats.bus_error++;
754 dev->
stats.rx_errors++;
755 priv->
can.can_stats.bus_error++;
763 dev->
stats.tx_errors++;
770 dev->
stats.rx_errors++;
771 priv->
can.can_stats.bus_error++;
779 dev->
stats.tx_errors++;
780 priv->
can.can_stats.bus_error++;
786 static int at91_poll_err(
struct net_device *dev,
int quota,
u32 reg_sr)
798 at91_poll_err_frame(dev, cf, reg_sr);
801 dev->
stats.rx_packets++;
810 const struct at91_priv *priv = netdev_priv(dev);
814 if (reg_sr & get_irq_mb_rx(priv))
815 work_done += at91_poll_rx(dev, quota - work_done);
823 work_done += at91_poll_err(dev, quota - work_done, reg_sr);
825 if (work_done < quota) {
831 at91_write(priv,
AT91_IER, reg_ier);
851 struct at91_priv *priv = netdev_priv(dev);
858 mb = get_tx_echo_mb(priv);
861 if (!(reg_sr & (1 << mb)))
865 at91_write(priv,
AT91_IDR, 1 << mb);
873 reg_msr = at91_read(priv,
AT91_MSR(mb));
874 if (
likely(reg_msr & AT91_MSR_MRDY &&
878 dev->
stats.tx_packets++;
887 if ((priv->
tx_next & get_next_mask(priv)) != 0 ||
888 (priv->
tx_echo & get_next_mask(priv)) == 0)
889 netif_wake_queue(dev);
892 static void at91_irq_err_state(
struct net_device *dev,
895 struct at91_priv *priv = netdev_priv(dev);
899 at91_get_berr_counter(dev, &bec);
901 switch (priv->
can.state) {
911 priv->
can.can_stats.error_warning++;
914 cf->data[1] = (bec.txerr > bec.rxerr) ?
927 priv->
can.can_stats.error_passive++;
930 cf->data[1] = (bec.txerr > bec.rxerr) ?
944 priv->
can.can_stats.restarts++;
947 netif_wake_queue(dev);
984 priv->
can.can_stats.bus_off++;
987 if (!priv->
can.restart_ms) {
996 at91_write(priv,
AT91_IDR, reg_idr);
1000 static int at91_get_state_by_bec(
const struct net_device *dev,
1006 err = at91_get_berr_counter(dev, &bec);
1010 if (bec.txerr < 96 && bec.rxerr < 96)
1012 else if (bec.txerr < 128 && bec.rxerr < 128)
1014 else if (bec.txerr < 256 && bec.rxerr < 256)
1023 static void at91_irq_err(
struct net_device *dev)
1025 struct at91_priv *priv = netdev_priv(dev);
1032 if (at91_is_sam9263(priv)) {
1033 reg_sr = at91_read(priv,
AT91_SR);
1045 netdev_err(dev,
"BUG! hardware in undefined state\n");
1049 err = at91_get_state_by_bec(dev, &new_state);
1055 if (
likely(new_state == priv->
can.state))
1062 at91_irq_err_state(dev, cf, new_state);
1065 dev->
stats.rx_packets++;
1077 struct at91_priv *priv = netdev_priv(dev);
1081 reg_sr = at91_read(priv,
AT91_SR);
1082 reg_imr = at91_read(priv,
AT91_IMR);
1092 if (reg_sr & (get_irq_mb_rx(priv) | AT91_IRQ_ERR_FRAME)) {
1099 get_irq_mb_rx(priv) | AT91_IRQ_ERR_FRAME);
1100 napi_schedule(&priv->
napi);
1104 if (reg_sr & get_irq_mb_tx(priv))
1105 at91_irq_tx(dev, reg_sr);
1115 struct at91_priv *priv = netdev_priv(dev);
1133 at91_chip_start(dev);
1134 napi_enable(&priv->
napi);
1135 netif_start_queue(dev);
1150 static int at91_close(
struct net_device *dev)
1152 struct at91_priv *priv = netdev_priv(dev);
1154 netif_stop_queue(dev);
1155 napi_disable(&priv->
napi);
1170 at91_chip_start(dev);
1171 netif_wake_queue(dev);
1182 .ndo_open = at91_open,
1183 .ndo_stop = at91_close,
1184 .ndo_start_xmit = at91_start_xmit,
1202 struct at91_priv *priv = netdev_priv(ndev);
1203 unsigned long can_id;
1234 at91_sysfs_show_mb0_id, at91_sysfs_set_mb0_id);
1236 static struct attribute *at91_sysfs_attrs[] = {
1237 &dev_attr_mb0_id.attr,
1242 .attrs = at91_sysfs_attrs,
1256 devtype = pdev->
id_entry->driver_data;
1268 if (!res || irq <= 0) {
1297 priv = netdev_priv(dev);
1299 priv->
can.bittiming_const = &at91_bittiming_const;
1300 priv->
can.do_set_mode = at91_set_mode;
1301 priv->
can.do_get_berr_counter = at91_get_berr_counter;
1308 priv->
pdata = pdev->
dev.platform_data;
1313 if (at91_is_sam9263(priv))
1321 dev_err(&pdev->
dev,
"registering netdev failed\n");
1325 dev_info(&pdev->
dev,
"device registered (reg_base=%p, irq=%d)\n",
1344 struct net_device *dev = platform_get_drvdata(pdev);
1345 struct at91_priv *priv = netdev_priv(dev);
1350 platform_set_drvdata(pdev,
NULL);
1369 .name =
"at91sam9x5_can",
1377 .probe = at91_can_probe,
1380 .name = KBUILD_MODNAME,
1383 .id_table = at91_can_id_table,