Full support for Ginger Console
[linux-ginger.git] / drivers / net / can / ti_hecc.c
blob39329e67364e99f40d80606558d8dcf7ad6a0dd0
1 /*
2 * TI HECC (CAN) device driver
4 * This driver supports TI's HECC (High End CAN Controller module) and the
5 * specs for the same is available at <http://www.ti.com>
7 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation version 2.
13 * This program is distributed as is WITHOUT ANY WARRANTY of any
14 * kind, whether express or implied; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
21 * Your platform definitions should specify module ram offsets and interrupt
22 * number to use as follows:
24 * static struct ti_hecc_platform_data am3517_evm_hecc_pdata = {
25 * .scc_hecc_offset = 0,
26 * .scc_ram_offset = 0x3000,
27 * .hecc_ram_offset = 0x3000,
28 * .mbx_offset = 0x2000,
29 * .int_line = 0,
30 * .revision = 1,
31 * };
33 * Please see include/can/platform/ti_hecc.h for description of above fields
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/kernel.h>
40 #include <linux/types.h>
41 #include <linux/interrupt.h>
42 #include <linux/errno.h>
43 #include <linux/netdevice.h>
44 #include <linux/skbuff.h>
45 #include <linux/platform_device.h>
46 #include <linux/clk.h>
48 #include <linux/can.h>
49 #include <linux/can/dev.h>
50 #include <linux/can/error.h>
51 #include <linux/can/platform/ti_hecc.h>
53 #define DRV_NAME "ti_hecc"
54 #define HECC_MODULE_VERSION "0.7"
55 MODULE_VERSION(HECC_MODULE_VERSION);
56 #define DRV_DESC "TI High End CAN Controller Driver " HECC_MODULE_VERSION
58 /* TX / RX Mailbox Configuration */
59 #define HECC_MAX_MAILBOXES 32 /* hardware mailboxes - do not change */
60 #define MAX_TX_PRIO 0x3F /* hardware value - do not change */
63 * Important Note: TX mailbox configuration
64 * TX mailboxes should be restricted to the number of SKB buffers to avoid
65 * maintaining SKB buffers separately. TX mailboxes should be a power of 2
66 * for the mailbox logic to work. Top mailbox numbers are reserved for RX
67 * and lower mailboxes for TX.
69 * HECC_MAX_TX_MBOX HECC_MB_TX_SHIFT
70 * 4 (default) 2
71 * 8 3
72 * 16 4
74 #define HECC_MB_TX_SHIFT 2 /* as per table above */
75 #define HECC_MAX_TX_MBOX BIT(HECC_MB_TX_SHIFT)
77 #define HECC_TX_PRIO_SHIFT (HECC_MB_TX_SHIFT)
78 #define HECC_TX_PRIO_MASK (MAX_TX_PRIO << HECC_MB_TX_SHIFT)
79 #define HECC_TX_MB_MASK (HECC_MAX_TX_MBOX - 1)
80 #define HECC_TX_MASK ((HECC_MAX_TX_MBOX - 1) | HECC_TX_PRIO_MASK)
81 #define HECC_TX_MBOX_MASK (~(BIT(HECC_MAX_TX_MBOX) - 1))
82 #define HECC_DEF_NAPI_WEIGHT HECC_MAX_RX_MBOX
85 * Important Note: RX mailbox configuration
86 * RX mailboxes are further logically split into two - main and buffer
87 * mailboxes. The goal is to get all packets into main mailboxes as
88 * driven by mailbox number and receive priority (higher to lower) and
89 * buffer mailboxes are used to receive pkts while main mailboxes are being
90 * processed. This ensures in-order packet reception.
92 * Here are the recommended values for buffer mailbox. Note that RX mailboxes
93 * start after TX mailboxes:
95 * HECC_MAX_RX_MBOX HECC_RX_BUFFER_MBOX No of buffer mailboxes
96 * 28 12 8
97 * 16 20 4
100 #define HECC_MAX_RX_MBOX (HECC_MAX_MAILBOXES - HECC_MAX_TX_MBOX)
101 #define HECC_RX_BUFFER_MBOX 12 /* as per table above */
102 #define HECC_RX_FIRST_MBOX (HECC_MAX_MAILBOXES - 1)
103 #define HECC_RX_HIGH_MBOX_MASK (~(BIT(HECC_RX_BUFFER_MBOX) - 1))
105 /* TI HECC module registers */
106 #define HECC_CANME 0x0 /* Mailbox enable */
107 #define HECC_CANMD 0x4 /* Mailbox direction */
108 #define HECC_CANTRS 0x8 /* Transmit request set */
109 #define HECC_CANTRR 0xC /* Transmit request */
110 #define HECC_CANTA 0x10 /* Transmission acknowledge */
111 #define HECC_CANAA 0x14 /* Abort acknowledge */
112 #define HECC_CANRMP 0x18 /* Receive message pending */
113 #define HECC_CANRML 0x1C /* Remote message lost */
114 #define HECC_CANRFP 0x20 /* Remote frame pending */
115 #define HECC_CANGAM 0x24 /* SECC only:Global acceptance mask */
116 #define HECC_CANMC 0x28 /* Master control */
117 #define HECC_CANBTC 0x2C /* Bit timing configuration */
118 #define HECC_CANES 0x30 /* Error and status */
119 #define HECC_CANTEC 0x34 /* Transmit error counter */
120 #define HECC_CANREC 0x38 /* Receive error counter */
121 #define HECC_CANGIF0 0x3C /* Global interrupt flag 0 */
122 #define HECC_CANGIM 0x40 /* Global interrupt mask */
123 #define HECC_CANGIF1 0x44 /* Global interrupt flag 1 */
124 #define HECC_CANMIM 0x48 /* Mailbox interrupt mask */
125 #define HECC_CANMIL 0x4C /* Mailbox interrupt level */
126 #define HECC_CANOPC 0x50 /* Overwrite protection control */
127 #define HECC_CANTIOC 0x54 /* Transmit I/O control */
128 #define HECC_CANRIOC 0x58 /* Receive I/O control */
129 #define HECC_CANLNT 0x5C /* HECC only: Local network time */
130 #define HECC_CANTOC 0x60 /* HECC only: Time-out control */
131 #define HECC_CANTOS 0x64 /* HECC only: Time-out status */
132 #define HECC_CANTIOCE 0x68 /* SCC only:Enhanced TX I/O control */
133 #define HECC_CANRIOCE 0x6C /* SCC only:Enhanced RX I/O control */
135 /* Mailbox registers */
136 #define HECC_CANMID 0x0
137 #define HECC_CANMCF 0x4
138 #define HECC_CANMDL 0x8
139 #define HECC_CANMDH 0xC
141 #define HECC_SET_REG 0xFFFFFFFF
142 #define HECC_CANID_MASK 0x3FF /* 18 bits mask for extended id's */
143 #define HECC_CCE_WAIT_COUNT 100 /* Wait for ~1 sec for CCE bit */
145 #define HECC_CANMC_SCM BIT(13) /* SCC compat mode */
146 #define HECC_CANMC_CCR BIT(12) /* Change config request */
147 #define HECC_CANMC_PDR BIT(11) /* Local Power down - for sleep mode */
148 #define HECC_CANMC_ABO BIT(7) /* Auto Bus On */
149 #define HECC_CANMC_STM BIT(6) /* Self test mode - loopback */
150 #define HECC_CANMC_SRES BIT(5) /* Software reset */
152 #define HECC_CANTIOC_EN BIT(3) /* Enable CAN TX I/O pin */
153 #define HECC_CANRIOC_EN BIT(3) /* Enable CAN RX I/O pin */
155 #define HECC_CANMID_IDE BIT(31) /* Extended frame format */
156 #define HECC_CANMID_AME BIT(30) /* Acceptance mask enable */
157 #define HECC_CANMID_AAM BIT(29) /* Auto answer mode */
159 #define HECC_CANES_FE BIT(24) /* form error */
160 #define HECC_CANES_BE BIT(23) /* bit error */
161 #define HECC_CANES_SA1 BIT(22) /* stuck at dominant error */
162 #define HECC_CANES_CRCE BIT(21) /* CRC error */
163 #define HECC_CANES_SE BIT(20) /* stuff bit error */
164 #define HECC_CANES_ACKE BIT(19) /* ack error */
165 #define HECC_CANES_BO BIT(18) /* Bus off status */
166 #define HECC_CANES_EP BIT(17) /* Error passive status */
167 #define HECC_CANES_EW BIT(16) /* Error warning status */
168 #define HECC_CANES_SMA BIT(5) /* suspend mode ack */
169 #define HECC_CANES_CCE BIT(4) /* Change config enabled */
170 #define HECC_CANES_PDA BIT(3) /* Power down mode ack */
172 #define HECC_CANBTC_SAM BIT(7) /* sample points */
174 #define HECC_BUS_ERROR (HECC_CANES_FE | HECC_CANES_BE |\
175 HECC_CANES_CRCE | HECC_CANES_SE |\
176 HECC_CANES_ACKE)
178 #define HECC_CANMCF_RTR BIT(4) /* Remote transmit request */
180 #define HECC_CANGIF_MAIF BIT(17) /* Message alarm interrupt */
181 #define HECC_CANGIF_TCOIF BIT(16) /* Timer counter overflow int */
182 #define HECC_CANGIF_GMIF BIT(15) /* Global mailbox interrupt */
183 #define HECC_CANGIF_AAIF BIT(14) /* Abort ack interrupt */
184 #define HECC_CANGIF_WDIF BIT(13) /* Write denied interrupt */
185 #define HECC_CANGIF_WUIF BIT(12) /* Wake up interrupt */
186 #define HECC_CANGIF_RMLIF BIT(11) /* Receive message lost interrupt */
187 #define HECC_CANGIF_BOIF BIT(10) /* Bus off interrupt */
188 #define HECC_CANGIF_EPIF BIT(9) /* Error passive interrupt */
189 #define HECC_CANGIF_WLIF BIT(8) /* Warning level interrupt */
190 #define HECC_CANGIF_MBOX_MASK 0x1F /* Mailbox number mask */
191 #define HECC_CANGIM_I1EN BIT(1) /* Int line 1 enable */
192 #define HECC_CANGIM_I0EN BIT(0) /* Int line 0 enable */
193 #define HECC_CANGIM_DEF_MASK 0x700 /* only busoff/warning/passive */
194 #define HECC_CANGIM_SIL BIT(2) /* system interrupts to int line 1 */
196 /* CAN Bittiming constants as per HECC specs */
197 static struct can_bittiming_const ti_hecc_bittiming_const = {
198 .name = DRV_NAME,
199 .tseg1_min = 1,
200 .tseg1_max = 16,
201 .tseg2_min = 1,
202 .tseg2_max = 8,
203 .sjw_max = 4,
204 .brp_min = 1,
205 .brp_max = 256,
206 .brp_inc = 1,
209 struct ti_hecc_priv {
210 struct can_priv can; /* MUST be first member/field */
211 struct napi_struct napi;
212 struct net_device *ndev;
213 struct clk *clk;
214 void __iomem *base;
215 u32 scc_ram_offset;
216 u32 hecc_ram_offset;
217 u32 mbx_offset;
218 u32 int_line;
219 spinlock_t mbx_lock; /* CANME register needs protection */
220 u32 tx_head;
221 u32 tx_tail;
222 u32 rx_next;
225 static inline int get_tx_head_mb(struct ti_hecc_priv *priv)
227 return priv->tx_head & HECC_TX_MB_MASK;
230 static inline int get_tx_tail_mb(struct ti_hecc_priv *priv)
232 return priv->tx_tail & HECC_TX_MB_MASK;
235 static inline int get_tx_head_prio(struct ti_hecc_priv *priv)
237 return (priv->tx_head >> HECC_TX_PRIO_SHIFT) & MAX_TX_PRIO;
240 static inline void hecc_write_lam(struct ti_hecc_priv *priv, u32 mbxno, u32 val)
242 __raw_writel(val, priv->base + priv->hecc_ram_offset + mbxno * 4);
245 static inline void hecc_write_mbx(struct ti_hecc_priv *priv, u32 mbxno,
246 u32 reg, u32 val)
248 __raw_writel(val, priv->base + priv->mbx_offset + mbxno * 0x10 +
249 reg);
252 static inline u32 hecc_read_mbx(struct ti_hecc_priv *priv, u32 mbxno, u32 reg)
254 return __raw_readl(priv->base + priv->mbx_offset + mbxno * 0x10 +
255 reg);
258 static inline void hecc_write(struct ti_hecc_priv *priv, u32 reg, u32 val)
260 __raw_writel(val, priv->base + reg);
263 static inline u32 hecc_read(struct ti_hecc_priv *priv, int reg)
265 return __raw_readl(priv->base + reg);
268 static inline void hecc_set_bit(struct ti_hecc_priv *priv, int reg,
269 u32 bit_mask)
271 hecc_write(priv, reg, hecc_read(priv, reg) | bit_mask);
274 static inline void hecc_clear_bit(struct ti_hecc_priv *priv, int reg,
275 u32 bit_mask)
277 hecc_write(priv, reg, hecc_read(priv, reg) & ~bit_mask);
280 static inline u32 hecc_get_bit(struct ti_hecc_priv *priv, int reg, u32 bit_mask)
282 return (hecc_read(priv, reg) & bit_mask) ? 1 : 0;
285 static int ti_hecc_get_state(const struct net_device *ndev,
286 enum can_state *state)
288 struct ti_hecc_priv *priv = netdev_priv(ndev);
290 *state = priv->can.state;
291 return 0;
294 static int ti_hecc_set_btc(struct ti_hecc_priv *priv)
296 struct can_bittiming *bit_timing = &priv->can.bittiming;
297 u32 can_btc;
299 can_btc = (bit_timing->phase_seg2 - 1) & 0x7;
300 can_btc |= ((bit_timing->phase_seg1 + bit_timing->prop_seg - 1)
301 & 0xF) << 3;
302 if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) {
303 if (bit_timing->brp > 4)
304 can_btc |= HECC_CANBTC_SAM;
305 else
306 dev_warn(priv->ndev->dev.parent, "WARN: Triple" \
307 "sampling not set due to h/w limitations");
309 can_btc |= ((bit_timing->sjw - 1) & 0x3) << 8;
310 can_btc |= ((bit_timing->brp - 1) & 0xFF) << 16;
312 /* ERM being set to 0 by default meaning resync at falling edge */
314 hecc_write(priv, HECC_CANBTC, can_btc);
315 dev_info(priv->ndev->dev.parent, "setting CANBTC=%#x\n", can_btc);
317 return 0;
320 static void ti_hecc_reset(struct net_device *ndev)
322 u32 cnt;
323 struct ti_hecc_priv *priv = netdev_priv(ndev);
325 dev_dbg(ndev->dev.parent, "resetting hecc ...\n");
326 hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_SRES);
328 /* Set change control request and wait till enabled */
329 hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
332 * INFO: It has been observed that at times CCE bit may not be
333 * set and hw seems to be ok even if this bit is not set so
334 * timing out with a timing of 1ms to respect the specs
336 cnt = HECC_CCE_WAIT_COUNT;
337 while (!hecc_get_bit(priv, HECC_CANES, HECC_CANES_CCE) && cnt != 0) {
338 --cnt;
339 udelay(10);
343 * Note: On HECC, BTC can be programmed only in initialization mode, so
344 * it is expected that the can bittiming parameters are set via ip
345 * utility before the device is opened
347 ti_hecc_set_btc(priv);
349 /* Clear CCR (and CANMC register) and wait for CCE = 0 enable */
350 hecc_write(priv, HECC_CANMC, 0);
353 * INFO: CAN net stack handles bus off and hence disabling auto-bus-on
354 * hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_ABO);
358 * INFO: It has been observed that at times CCE bit may not be
359 * set and hw seems to be ok even if this bit is not set so
361 cnt = HECC_CCE_WAIT_COUNT;
362 while (hecc_get_bit(priv, HECC_CANES, HECC_CANES_CCE) && cnt != 0) {
363 --cnt;
364 udelay(10);
367 /* Enable TX and RX I/O Control pins */
368 hecc_write(priv, HECC_CANTIOC, HECC_CANTIOC_EN);
369 hecc_write(priv, HECC_CANRIOC, HECC_CANRIOC_EN);
371 /* Clear registers for clean operation */
372 hecc_write(priv, HECC_CANTA, HECC_SET_REG);
373 hecc_write(priv, HECC_CANRMP, HECC_SET_REG);
374 hecc_write(priv, HECC_CANGIF0, HECC_SET_REG);
375 hecc_write(priv, HECC_CANGIF1, HECC_SET_REG);
376 hecc_write(priv, HECC_CANME, 0);
377 hecc_write(priv, HECC_CANMD, 0);
379 /* SCC compat mode NOT supported (and not needed too) */
380 hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_SCM);
383 static void ti_hecc_start(struct net_device *ndev)
385 struct ti_hecc_priv *priv = netdev_priv(ndev);
386 u32 cnt, mbxno, mbx_mask;
388 /* put HECC in initialization mode and set btc */
389 ti_hecc_reset(ndev);
391 priv->tx_head = priv->tx_tail = HECC_TX_MASK;
392 priv->rx_next = HECC_RX_FIRST_MBOX;
394 /* Enable local and global acceptance mask registers */
395 hecc_write(priv, HECC_CANGAM, HECC_SET_REG);
397 /* Prepare configured mailboxes to receive messages */
398 for (cnt = 0; cnt < HECC_MAX_RX_MBOX; cnt++) {
399 mbxno = HECC_MAX_MAILBOXES - 1 - cnt;
400 mbx_mask = BIT(mbxno);
401 hecc_clear_bit(priv, HECC_CANME, mbx_mask);
402 hecc_write_mbx(priv, mbxno, HECC_CANMID, HECC_CANMID_AME);
403 hecc_write_lam(priv, mbxno, HECC_SET_REG);
404 hecc_set_bit(priv, HECC_CANMD, mbx_mask);
405 hecc_set_bit(priv, HECC_CANME, mbx_mask);
406 hecc_set_bit(priv, HECC_CANMIM, mbx_mask);
409 /* Prevent message over-write & Enable interrupts */
410 hecc_write(priv, HECC_CANOPC, HECC_SET_REG);
411 if (priv->int_line) {
412 hecc_write(priv, HECC_CANMIL, HECC_SET_REG);
413 hecc_write(priv, HECC_CANGIM, HECC_CANGIM_DEF_MASK |
414 HECC_CANGIM_I1EN | HECC_CANGIM_SIL);
415 } else {
416 hecc_write(priv, HECC_CANMIL, 0);
417 hecc_write(priv, HECC_CANGIM,
418 HECC_CANGIM_DEF_MASK | HECC_CANGIM_I0EN);
420 priv->can.state = CAN_STATE_ERROR_ACTIVE;
423 static void ti_hecc_stop(struct net_device *ndev)
425 struct ti_hecc_priv *priv = netdev_priv(ndev);
427 /* Disable interrupts and disable mailboxes */
428 hecc_write(priv, HECC_CANGIM, 0);
429 hecc_write(priv, HECC_CANMIM, 0);
430 hecc_write(priv, HECC_CANME, 0);
431 priv->can.state = CAN_STATE_STOPPED;
434 static int ti_hecc_do_set_mode(struct net_device *ndev, enum can_mode mode)
436 int ret = 0;
438 switch (mode) {
439 case CAN_MODE_START:
440 ti_hecc_start(ndev);
441 netif_wake_queue(ndev);
442 break;
443 default:
444 ret = -EOPNOTSUPP;
445 break;
448 return ret;
452 * ti_hecc_xmit: HECC Transmit
454 * The transmit mailboxes start from 0 to HECC_MAX_TX_MBOX. In HECC the
455 * priority of the mailbox for tranmission is dependent upon priority setting
456 * field in mailbox registers. The mailbox with highest value in priority field
457 * is transmitted first. Only when two mailboxes have the same value in
458 * priority field the highest numbered mailbox is transmitted first.
460 * To utilize the HECC priority feature as described above we start with the
461 * highest numbered mailbox with highest priority level and move on to the next
462 * mailbox with the same priority level and so on. Once we loop through all the
463 * transmit mailboxes we choose the next priority level (lower) and so on
464 * until we reach the lowest priority level on the lowest numbered mailbox
465 * when we stop transmission until all mailboxes are transmitted and then
466 * restart at highest numbered mailbox with highest priority.
468 * Two counters (head and tail) are used to track the next mailbox to transmit
469 * and to track the echo buffer for already transmitted mailbox. The queue
470 * is stopped when all the mailboxes are busy or when there is a priority
471 * value roll-over happens.
473 static netdev_tx_t ti_hecc_xmit(struct sk_buff *skb, struct net_device *ndev)
475 struct ti_hecc_priv *priv = netdev_priv(ndev);
476 struct can_frame *cf = (struct can_frame *)skb->data;
477 u32 mbxno, mbx_mask, data;
478 unsigned long flags;
480 mbxno = get_tx_head_mb(priv);
481 mbx_mask = BIT(mbxno);
482 spin_lock_irqsave(&priv->mbx_lock, flags);
483 if (unlikely(hecc_read(priv, HECC_CANME) & mbx_mask)) {
484 spin_unlock_irqrestore(&priv->mbx_lock, flags);
485 netif_stop_queue(ndev);
486 dev_err(priv->ndev->dev.parent,
487 "BUG: TX mbx not ready tx_head=%08X, tx_tail=%08X\n",
488 priv->tx_head, priv->tx_tail);
489 return NETDEV_TX_BUSY;
491 spin_unlock_irqrestore(&priv->mbx_lock, flags);
493 /* Prepare mailbox for transmission */
494 data = min_t(u8, cf->can_dlc, 8);
495 if (cf->can_id & CAN_RTR_FLAG) /* Remote transmission request */
496 data |= HECC_CANMCF_RTR;
497 data |= get_tx_head_prio(priv) << 8;
498 hecc_write_mbx(priv, mbxno, HECC_CANMCF, data);
500 if (cf->can_id & CAN_EFF_FLAG) /* Extended frame format */
501 data = (cf->can_id & CAN_EFF_MASK) | HECC_CANMID_IDE;
502 else /* Standard frame format */
503 data = (cf->can_id & CAN_SFF_MASK) << 18;
504 hecc_write_mbx(priv, mbxno, HECC_CANMID, data);
505 hecc_write_mbx(priv, mbxno, HECC_CANMDL,
506 be32_to_cpu(*(u32 *)(cf->data)));
507 if (cf->can_dlc > 4)
508 hecc_write_mbx(priv, mbxno, HECC_CANMDH,
509 be32_to_cpu(*(u32 *)(cf->data + 4)));
510 else
511 *(u32 *)(cf->data + 4) = 0;
512 can_put_echo_skb(skb, ndev, mbxno);
514 spin_lock_irqsave(&priv->mbx_lock, flags);
515 --priv->tx_head;
516 if ((hecc_read(priv, HECC_CANME) & BIT(get_tx_head_mb(priv))) ||
517 (priv->tx_head & HECC_TX_MASK) == HECC_TX_MASK) {
518 netif_stop_queue(ndev);
520 hecc_set_bit(priv, HECC_CANME, mbx_mask);
521 spin_unlock_irqrestore(&priv->mbx_lock, flags);
523 hecc_clear_bit(priv, HECC_CANMD, mbx_mask);
524 hecc_set_bit(priv, HECC_CANMIM, mbx_mask);
525 hecc_write(priv, HECC_CANTRS, mbx_mask);
527 return NETDEV_TX_OK;
530 static int ti_hecc_rx_pkt(struct ti_hecc_priv *priv, int mbxno)
532 struct net_device_stats *stats = &priv->ndev->stats;
533 struct can_frame *cf;
534 struct sk_buff *skb;
535 u32 data, mbx_mask;
536 unsigned long flags;
538 skb = alloc_can_skb(priv->ndev, &cf);
539 if (!skb) {
540 if (printk_ratelimit())
541 dev_err(priv->ndev->dev.parent,
542 "ti_hecc_rx_pkt: alloc_can_skb() failed\n");
543 return -ENOMEM;
546 mbx_mask = BIT(mbxno);
547 data = hecc_read_mbx(priv, mbxno, HECC_CANMID);
548 if (data & HECC_CANMID_IDE)
549 cf->can_id = (data & CAN_EFF_MASK) | CAN_EFF_FLAG;
550 else
551 cf->can_id = (data >> 18) & CAN_SFF_MASK;
552 data = hecc_read_mbx(priv, mbxno, HECC_CANMCF);
553 if (data & HECC_CANMCF_RTR)
554 cf->can_id |= CAN_RTR_FLAG;
555 cf->can_dlc = data & 0xF;
556 data = hecc_read_mbx(priv, mbxno, HECC_CANMDL);
557 *(u32 *)(cf->data) = cpu_to_be32(data);
558 if (cf->can_dlc > 4) {
559 data = hecc_read_mbx(priv, mbxno, HECC_CANMDH);
560 *(u32 *)(cf->data + 4) = cpu_to_be32(data);
561 } else {
562 *(u32 *)(cf->data + 4) = 0;
564 spin_lock_irqsave(&priv->mbx_lock, flags);
565 hecc_clear_bit(priv, HECC_CANME, mbx_mask);
566 hecc_write(priv, HECC_CANRMP, mbx_mask);
567 /* enable mailbox only if it is part of rx buffer mailboxes */
568 if (priv->rx_next < HECC_RX_BUFFER_MBOX)
569 hecc_set_bit(priv, HECC_CANME, mbx_mask);
570 spin_unlock_irqrestore(&priv->mbx_lock, flags);
572 stats->rx_bytes += cf->can_dlc;
573 netif_receive_skb(skb);
574 stats->rx_packets++;
576 return 0;
580 * ti_hecc_rx_poll - HECC receive pkts
582 * The receive mailboxes start from highest numbered mailbox till last xmit
583 * mailbox. On CAN frame reception the hardware places the data into highest
584 * numbered mailbox that matches the CAN ID filter. Since all receive mailboxes
585 * have same filtering (ALL CAN frames) packets will arrive in the highest
586 * available RX mailbox and we need to ensure in-order packet reception.
588 * To ensure the packets are received in the right order we logically divide
589 * the RX mailboxes into main and buffer mailboxes. Packets are received as per
590 * mailbox priotity (higher to lower) in the main bank and once it is full we
591 * disable further reception into main mailboxes. While the main mailboxes are
592 * processed in NAPI, further packets are received in buffer mailboxes.
594 * We maintain a RX next mailbox counter to process packets and once all main
595 * mailboxe packets are passed to the upper stack we enable all of them but
596 * continue to process packets received in buffer mailboxes. With each packet
597 * received from buffer mailbox we enable it immediately so as to handle the
598 * overflow from higher mailboxes.
600 static int ti_hecc_rx_poll(struct napi_struct *napi, int quota)
602 struct net_device *ndev = napi->dev;
603 struct ti_hecc_priv *priv = netdev_priv(ndev);
604 u32 num_pkts = 0;
605 u32 mbx_mask;
606 unsigned long pending_pkts, flags;
608 if (!netif_running(ndev))
609 return 0;
611 while ((pending_pkts = hecc_read(priv, HECC_CANRMP)) &&
612 num_pkts < quota) {
613 mbx_mask = BIT(priv->rx_next); /* next rx mailbox to process */
614 if (mbx_mask & pending_pkts) {
615 if (ti_hecc_rx_pkt(priv, priv->rx_next) < 0)
616 return num_pkts;
617 ++num_pkts;
618 } else if (priv->rx_next > HECC_RX_BUFFER_MBOX) {
619 break; /* pkt not received yet */
621 --priv->rx_next;
622 if (priv->rx_next == HECC_RX_BUFFER_MBOX) {
623 /* enable high bank mailboxes */
624 spin_lock_irqsave(&priv->mbx_lock, flags);
625 mbx_mask = hecc_read(priv, HECC_CANME);
626 mbx_mask |= HECC_RX_HIGH_MBOX_MASK;
627 hecc_write(priv, HECC_CANME, mbx_mask);
628 spin_unlock_irqrestore(&priv->mbx_lock, flags);
629 } else if (priv->rx_next == HECC_MAX_TX_MBOX - 1) {
630 priv->rx_next = HECC_RX_FIRST_MBOX;
631 break;
635 /* Enable packet interrupt if all pkts are handled */
636 if (hecc_read(priv, HECC_CANRMP) == 0) {
637 napi_complete(napi);
638 /* Re-enable RX mailbox interrupts */
639 mbx_mask = hecc_read(priv, HECC_CANMIM);
640 mbx_mask |= HECC_TX_MBOX_MASK;
641 hecc_write(priv, HECC_CANMIM, mbx_mask);
644 return num_pkts;
647 static int ti_hecc_error(struct net_device *ndev, int int_status,
648 int err_status)
650 struct ti_hecc_priv *priv = netdev_priv(ndev);
651 struct net_device_stats *stats = &ndev->stats;
652 struct can_frame *cf;
653 struct sk_buff *skb;
655 /* propogate the error condition to the can stack */
656 skb = alloc_can_err_skb(ndev, &cf);
657 if (!skb) {
658 if (printk_ratelimit())
659 dev_err(priv->ndev->dev.parent,
660 "ti_hecc_error: alloc_can_err_skb() failed\n");
661 return -ENOMEM;
664 if (int_status & HECC_CANGIF_WLIF) { /* warning level int */
665 if ((int_status & HECC_CANGIF_BOIF) == 0) {
666 priv->can.state = CAN_STATE_ERROR_WARNING;
667 ++priv->can.can_stats.error_warning;
668 cf->can_id |= CAN_ERR_CRTL;
669 if (hecc_read(priv, HECC_CANTEC) > 96)
670 cf->data[1] |= CAN_ERR_CRTL_TX_WARNING;
671 if (hecc_read(priv, HECC_CANREC) > 96)
672 cf->data[1] |= CAN_ERR_CRTL_RX_WARNING;
674 hecc_set_bit(priv, HECC_CANES, HECC_CANES_EW);
675 dev_dbg(priv->ndev->dev.parent, "Error Warning interrupt\n");
676 hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
679 if (int_status & HECC_CANGIF_EPIF) { /* error passive int */
680 if ((int_status & HECC_CANGIF_BOIF) == 0) {
681 priv->can.state = CAN_STATE_ERROR_PASSIVE;
682 ++priv->can.can_stats.error_passive;
683 cf->can_id |= CAN_ERR_CRTL;
684 if (hecc_read(priv, HECC_CANTEC) > 127)
685 cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
686 if (hecc_read(priv, HECC_CANREC) > 127)
687 cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
689 hecc_set_bit(priv, HECC_CANES, HECC_CANES_EP);
690 dev_dbg(priv->ndev->dev.parent, "Error passive interrupt\n");
691 hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
695 * Need to check busoff condition in error status register too to
696 * ensure warning interrupts don't hog the system
698 if ((int_status & HECC_CANGIF_BOIF) || (err_status & HECC_CANES_BO)) {
699 priv->can.state = CAN_STATE_BUS_OFF;
700 cf->can_id |= CAN_ERR_BUSOFF;
701 hecc_set_bit(priv, HECC_CANES, HECC_CANES_BO);
702 hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
703 /* Disable all interrupts in bus-off to avoid int hog */
704 hecc_write(priv, HECC_CANGIM, 0);
705 can_bus_off(ndev);
708 if (err_status & HECC_BUS_ERROR) {
709 ++priv->can.can_stats.bus_error;
710 cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
711 cf->data[2] |= CAN_ERR_PROT_UNSPEC;
712 if (err_status & HECC_CANES_FE) {
713 hecc_set_bit(priv, HECC_CANES, HECC_CANES_FE);
714 cf->data[2] |= CAN_ERR_PROT_FORM;
716 if (err_status & HECC_CANES_BE) {
717 hecc_set_bit(priv, HECC_CANES, HECC_CANES_BE);
718 cf->data[2] |= CAN_ERR_PROT_BIT;
720 if (err_status & HECC_CANES_SE) {
721 hecc_set_bit(priv, HECC_CANES, HECC_CANES_SE);
722 cf->data[2] |= CAN_ERR_PROT_STUFF;
724 if (err_status & HECC_CANES_CRCE) {
725 hecc_set_bit(priv, HECC_CANES, HECC_CANES_CRCE);
726 cf->data[2] |= CAN_ERR_PROT_LOC_CRC_SEQ |
727 CAN_ERR_PROT_LOC_CRC_DEL;
729 if (err_status & HECC_CANES_ACKE) {
730 hecc_set_bit(priv, HECC_CANES, HECC_CANES_ACKE);
731 cf->data[2] |= CAN_ERR_PROT_LOC_ACK |
732 CAN_ERR_PROT_LOC_ACK_DEL;
736 netif_receive_skb(skb);
737 stats->rx_packets++;
738 stats->rx_bytes += cf->can_dlc;
739 return 0;
742 static irqreturn_t ti_hecc_interrupt(int irq, void *dev_id)
744 struct net_device *ndev = (struct net_device *)dev_id;
745 struct ti_hecc_priv *priv = netdev_priv(ndev);
746 struct net_device_stats *stats = &ndev->stats;
747 u32 mbxno, mbx_mask, int_status, err_status;
748 unsigned long ack, flags;
750 int_status = hecc_read(priv,
751 (priv->int_line) ? HECC_CANGIF1 : HECC_CANGIF0);
753 if (!int_status)
754 return IRQ_NONE;
756 err_status = hecc_read(priv, HECC_CANES);
757 if (err_status & (HECC_BUS_ERROR | HECC_CANES_BO |
758 HECC_CANES_EP | HECC_CANES_EW))
759 ti_hecc_error(ndev, int_status, err_status);
761 if (int_status & HECC_CANGIF_GMIF) {
762 while (priv->tx_tail - priv->tx_head > 0) {
763 mbxno = get_tx_tail_mb(priv);
764 mbx_mask = BIT(mbxno);
765 if (!(mbx_mask & hecc_read(priv, HECC_CANTA)))
766 break;
767 hecc_clear_bit(priv, HECC_CANMIM, mbx_mask);
768 hecc_write(priv, HECC_CANTA, mbx_mask);
769 spin_lock_irqsave(&priv->mbx_lock, flags);
770 hecc_clear_bit(priv, HECC_CANME, mbx_mask);
771 spin_unlock_irqrestore(&priv->mbx_lock, flags);
772 stats->tx_bytes += hecc_read_mbx(priv, mbxno,
773 HECC_CANMCF) & 0xF;
774 stats->tx_packets++;
775 can_get_echo_skb(ndev, mbxno);
776 --priv->tx_tail;
779 /* restart queue if wrap-up or if queue stalled on last pkt */
780 if (((priv->tx_head == priv->tx_tail) &&
781 ((priv->tx_head & HECC_TX_MASK) != HECC_TX_MASK)) ||
782 (((priv->tx_tail & HECC_TX_MASK) == HECC_TX_MASK) &&
783 ((priv->tx_head & HECC_TX_MASK) == HECC_TX_MASK)))
784 netif_wake_queue(ndev);
786 /* Disable RX mailbox interrupts and let NAPI reenable them */
787 if (hecc_read(priv, HECC_CANRMP)) {
788 ack = hecc_read(priv, HECC_CANMIM);
789 ack &= BIT(HECC_MAX_TX_MBOX) - 1;
790 hecc_write(priv, HECC_CANMIM, ack);
791 napi_schedule(&priv->napi);
795 /* clear all interrupt conditions - read back to avoid spurious ints */
796 if (priv->int_line) {
797 hecc_write(priv, HECC_CANGIF1, HECC_SET_REG);
798 int_status = hecc_read(priv, HECC_CANGIF1);
799 } else {
800 hecc_write(priv, HECC_CANGIF0, HECC_SET_REG);
801 int_status = hecc_read(priv, HECC_CANGIF0);
804 return IRQ_HANDLED;
807 static int ti_hecc_open(struct net_device *ndev)
809 struct ti_hecc_priv *priv = netdev_priv(ndev);
810 int err;
812 err = request_irq(ndev->irq, ti_hecc_interrupt, IRQF_SHARED,
813 ndev->name, ndev);
814 if (err) {
815 dev_err(ndev->dev.parent, "error requesting interrupt\n");
816 return err;
819 /* Open common can device */
820 err = open_candev(ndev);
821 if (err) {
822 dev_err(ndev->dev.parent, "open_candev() failed %d\n", err);
823 free_irq(ndev->irq, ndev);
824 return err;
827 ti_hecc_start(ndev);
828 napi_enable(&priv->napi);
829 netif_start_queue(ndev);
831 return 0;
834 static int ti_hecc_close(struct net_device *ndev)
836 struct ti_hecc_priv *priv = netdev_priv(ndev);
838 netif_stop_queue(ndev);
839 napi_disable(&priv->napi);
840 ti_hecc_stop(ndev);
841 free_irq(ndev->irq, ndev);
842 close_candev(ndev);
844 return 0;
847 static const struct net_device_ops ti_hecc_netdev_ops = {
848 .ndo_open = ti_hecc_open,
849 .ndo_stop = ti_hecc_close,
850 .ndo_start_xmit = ti_hecc_xmit,
853 static int ti_hecc_probe(struct platform_device *pdev)
855 struct net_device *ndev = (struct net_device *)0;
856 struct ti_hecc_priv *priv;
857 struct ti_hecc_platform_data *pdata;
858 struct resource *mem, *irq;
859 void __iomem *addr;
860 int err = -ENODEV;
862 pdata = pdev->dev.platform_data;
863 if (!pdata) {
864 dev_err(&pdev->dev, "No platform data\n");
865 goto probe_exit;
868 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
869 if (!mem) {
870 dev_err(&pdev->dev, "No mem resources\n");
871 goto probe_exit;
873 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
874 if (!irq) {
875 dev_err(&pdev->dev, "No irq resource\n");
876 goto probe_exit;
878 if (!request_mem_region(mem->start, resource_size(mem), pdev->name)) {
879 dev_err(&pdev->dev, "HECC region already claimed\n");
880 err = -EBUSY;
881 goto probe_exit;
883 addr = ioremap(mem->start, resource_size(mem));
884 if (!addr) {
885 dev_err(&pdev->dev, "ioremap failed\n");
886 err = -ENOMEM;
887 goto probe_exit_free_region;
890 ndev = alloc_candev(sizeof(struct ti_hecc_priv), HECC_MAX_TX_MBOX);
891 if (!ndev) {
892 dev_err(&pdev->dev, "alloc_candev failed\n");
893 err = -ENOMEM;
894 goto probe_exit_iounmap;
897 priv = netdev_priv(ndev);
898 priv->ndev = ndev;
899 priv->base = addr;
900 priv->scc_ram_offset = pdata->scc_ram_offset;
901 priv->hecc_ram_offset = pdata->hecc_ram_offset;
902 priv->mbx_offset = pdata->mbx_offset;
903 priv->int_line = pdata->int_line;
905 priv->can.bittiming_const = &ti_hecc_bittiming_const;
906 priv->can.do_set_mode = ti_hecc_do_set_mode;
907 priv->can.do_get_state = ti_hecc_get_state;
909 ndev->irq = irq->start;
910 ndev->flags |= IFF_ECHO;
911 platform_set_drvdata(pdev, ndev);
912 SET_NETDEV_DEV(ndev, &pdev->dev);
913 ndev->netdev_ops = &ti_hecc_netdev_ops;
915 priv->clk = clk_get(&pdev->dev, "hecc_ck");
916 if (IS_ERR(priv->clk)) {
917 dev_err(&pdev->dev, "No clock available\n");
918 err = PTR_ERR(priv->clk);
919 priv->clk = NULL;
920 goto probe_exit_candev;
922 priv->can.clock.freq = clk_get_rate(priv->clk);
923 netif_napi_add(ndev, &priv->napi, ti_hecc_rx_poll,
924 HECC_DEF_NAPI_WEIGHT);
926 clk_enable(priv->clk);
927 err = register_candev(ndev);
928 if (err) {
929 dev_err(&pdev->dev, "register_candev() failed\n");
930 goto probe_exit_clk;
932 dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%u)\n",
933 priv->base, (u32) ndev->irq);
935 return 0;
937 probe_exit_clk:
938 clk_put(priv->clk);
939 probe_exit_candev:
940 free_candev(ndev);
941 probe_exit_iounmap:
942 iounmap(addr);
943 probe_exit_free_region:
944 release_mem_region(mem->start, resource_size(mem));
945 probe_exit:
946 return err;
949 static int __devexit ti_hecc_remove(struct platform_device *pdev)
951 struct resource *res;
952 struct net_device *ndev = platform_get_drvdata(pdev);
953 struct ti_hecc_priv *priv = netdev_priv(ndev);
955 clk_disable(priv->clk);
956 clk_put(priv->clk);
957 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
958 iounmap(priv->base);
959 release_mem_region(res->start, resource_size(res));
960 unregister_candev(ndev);
961 free_candev(ndev);
962 platform_set_drvdata(pdev, NULL);
964 return 0;
968 #ifdef CONFIG_PM
969 static int ti_hecc_suspend(struct platform_device *pdev, pm_message_t state)
971 struct net_device *dev = platform_get_drvdata(pdev);
972 struct ti_hecc_priv *priv = netdev_priv(dev);
974 if (netif_running(dev)) {
975 netif_stop_queue(dev);
976 netif_device_detach(dev);
979 hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_PDR);
980 priv->can.state = CAN_STATE_SLEEPING;
982 clk_disable(priv->clk);
984 return 0;
987 static int ti_hecc_resume(struct platform_device *pdev)
989 struct net_device *dev = platform_get_drvdata(pdev);
990 struct ti_hecc_priv *priv = netdev_priv(dev);
992 clk_enable(priv->clk);
994 hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_PDR);
995 priv->can.state = CAN_STATE_ERROR_ACTIVE;
997 if (netif_running(dev)) {
998 netif_device_attach(dev);
999 netif_start_queue(dev);
1002 return 0;
1004 #else
1005 #define ti_hecc_suspend NULL
1006 #define ti_hecc_resume NULL
1007 #endif
1009 /* TI HECC netdevice driver: platform driver structure */
1010 static struct platform_driver ti_hecc_driver = {
1011 .driver = {
1012 .name = DRV_NAME,
1013 .owner = THIS_MODULE,
1015 .probe = ti_hecc_probe,
1016 .remove = __devexit_p(ti_hecc_remove),
1017 .suspend = ti_hecc_suspend,
1018 .resume = ti_hecc_resume,
1021 static int __init ti_hecc_init_driver(void)
1023 printk(KERN_INFO DRV_DESC "\n");
1024 return platform_driver_register(&ti_hecc_driver);
1027 static void __exit ti_hecc_exit_driver(void)
1029 printk(KERN_INFO DRV_DESC " unloaded\n");
1030 platform_driver_unregister(&ti_hecc_driver);
1033 module_exit(ti_hecc_exit_driver);
1034 module_init(ti_hecc_init_driver);
1036 MODULE_AUTHOR("Anant Gole <anantgole@ti.com>");
1037 MODULE_LICENSE("GPL v2");
1038 MODULE_DESCRIPTION(DRV_DESC);