IP: Cleanups
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / net / can / ti_hecc.c
CommitLineData
3758bf25
AG
1/*
2 * TI HECC (CAN) device driver
3 *
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>
6 *
7 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
8 *
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.
12 *
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.
17 *
18 */
19
20/*
21 * Your platform definitions should specify module ram offsets and interrupt
22 * number to use as follows:
23 *
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 * };
32 *
33 * Please see include/can/platform/ti_hecc.h for description of above fields
34 *
35 */
36
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>
47
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>
52
53#define DRV_NAME "ti_hecc"
54#define HECC_MODULE_VERSION "0.7"
55MODULE_VERSION(HECC_MODULE_VERSION);
56#define DRV_DESC "TI High End CAN Controller Driver " HECC_MODULE_VERSION
57
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 */
61
62/*
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.
68 *
69 * HECC_MAX_TX_MBOX HECC_MB_TX_SHIFT
70 * 4 (default) 2
71 * 8 3
72 * 16 4
73 */
74#define HECC_MB_TX_SHIFT 2 /* as per table above */
75#define HECC_MAX_TX_MBOX BIT(HECC_MB_TX_SHIFT)
76
3758bf25
AG
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
83
84/*
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.
91 *
92 * Here are the recommended values for buffer mailbox. Note that RX mailboxes
93 * start after TX mailboxes:
94 *
95 * HECC_MAX_RX_MBOX HECC_RX_BUFFER_MBOX No of buffer mailboxes
96 * 28 12 8
97 * 16 20 4
98 */
99
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))
104
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 */
134
135/* Mailbox registers */
136#define HECC_CANMID 0x0
137#define HECC_CANMCF 0x4
138#define HECC_CANMDL 0x8
139#define HECC_CANMDH 0xC
140
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 */
144
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 */
151
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 */
154
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 */
158
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 */
171
172#define HECC_CANBTC_SAM BIT(7) /* sample points */
173
174#define HECC_BUS_ERROR (HECC_CANES_FE | HECC_CANES_BE |\
175 HECC_CANES_CRCE | HECC_CANES_SE |\
176 HECC_CANES_ACKE)
177
178#define HECC_CANMCF_RTR BIT(4) /* Remote transmit request */
179
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 */
195
196/* CAN Bittiming constants as per HECC specs */
197static 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,
207};
208
209struct 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;
223};
224
225static inline int get_tx_head_mb(struct ti_hecc_priv *priv)
226{
227 return priv->tx_head & HECC_TX_MB_MASK;
228}
229
230static inline int get_tx_tail_mb(struct ti_hecc_priv *priv)
231{
232 return priv->tx_tail & HECC_TX_MB_MASK;
233}
234
235static inline int get_tx_head_prio(struct ti_hecc_priv *priv)
236{
237 return (priv->tx_head >> HECC_TX_PRIO_SHIFT) & MAX_TX_PRIO;
238}
239
240static inline void hecc_write_lam(struct ti_hecc_priv *priv, u32 mbxno, u32 val)
241{
242 __raw_writel(val, priv->base + priv->hecc_ram_offset + mbxno * 4);
243}
244
245static inline void hecc_write_mbx(struct ti_hecc_priv *priv, u32 mbxno,
246 u32 reg, u32 val)
247{
248 __raw_writel(val, priv->base + priv->mbx_offset + mbxno * 0x10 +
249 reg);
250}
251
252static inline u32 hecc_read_mbx(struct ti_hecc_priv *priv, u32 mbxno, u32 reg)
253{
254 return __raw_readl(priv->base + priv->mbx_offset + mbxno * 0x10 +
255 reg);
256}
257
258static inline void hecc_write(struct ti_hecc_priv *priv, u32 reg, u32 val)
259{
260 __raw_writel(val, priv->base + reg);
261}
262
263static inline u32 hecc_read(struct ti_hecc_priv *priv, int reg)
264{
265 return __raw_readl(priv->base + reg);
266}
267
268static inline void hecc_set_bit(struct ti_hecc_priv *priv, int reg,
269 u32 bit_mask)
270{
271 hecc_write(priv, reg, hecc_read(priv, reg) | bit_mask);
272}
273
274static inline void hecc_clear_bit(struct ti_hecc_priv *priv, int reg,
275 u32 bit_mask)
276{
277 hecc_write(priv, reg, hecc_read(priv, reg) & ~bit_mask);
278}
279
280static inline u32 hecc_get_bit(struct ti_hecc_priv *priv, int reg, u32 bit_mask)
281{
282 return (hecc_read(priv, reg) & bit_mask) ? 1 : 0;
283}
284
285static int ti_hecc_get_state(const struct net_device *ndev,
286 enum can_state *state)
287{
288 struct ti_hecc_priv *priv = netdev_priv(ndev);
289
290 *state = priv->can.state;
291 return 0;
292}
293
294static int ti_hecc_set_btc(struct ti_hecc_priv *priv)
295{
296 struct can_bittiming *bit_timing = &priv->can.bittiming;
297 u32 can_btc;
298
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");
308 }
309 can_btc |= ((bit_timing->sjw - 1) & 0x3) << 8;
310 can_btc |= ((bit_timing->brp - 1) & 0xFF) << 16;
311
312 /* ERM being set to 0 by default meaning resync at falling edge */
313
314 hecc_write(priv, HECC_CANBTC, can_btc);
315 dev_info(priv->ndev->dev.parent, "setting CANBTC=%#x\n", can_btc);
316
317 return 0;
318}
319
320static void ti_hecc_reset(struct net_device *ndev)
321{
322 u32 cnt;
323 struct ti_hecc_priv *priv = netdev_priv(ndev);
324
325 dev_dbg(ndev->dev.parent, "resetting hecc ...\n");
326 hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_SRES);
327
328 /* Set change control request and wait till enabled */
329 hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
330
331 /*
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
335 */
336 cnt = HECC_CCE_WAIT_COUNT;
337 while (!hecc_get_bit(priv, HECC_CANES, HECC_CANES_CCE) && cnt != 0) {
338 --cnt;
339 udelay(10);
340 }
341
342 /*
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
346 */
347 ti_hecc_set_btc(priv);
348
349 /* Clear CCR (and CANMC register) and wait for CCE = 0 enable */
350 hecc_write(priv, HECC_CANMC, 0);
351
352 /*
353 * INFO: CAN net stack handles bus off and hence disabling auto-bus-on
354 * hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_ABO);
355 */
356
357 /*
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
360 */
361 cnt = HECC_CCE_WAIT_COUNT;
362 while (hecc_get_bit(priv, HECC_CANES, HECC_CANES_CCE) && cnt != 0) {
363 --cnt;
364 udelay(10);
365 }
366
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);
370
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);
378
379 /* SCC compat mode NOT supported (and not needed too) */
380 hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_SCM);
381}
382
383static void ti_hecc_start(struct net_device *ndev)
384{
385 struct ti_hecc_priv *priv = netdev_priv(ndev);
386 u32 cnt, mbxno, mbx_mask;
387
388 /* put HECC in initialization mode and set btc */
389 ti_hecc_reset(ndev);
390
391 priv->tx_head = priv->tx_tail = HECC_TX_MASK;
392 priv->rx_next = HECC_RX_FIRST_MBOX;
393
394 /* Enable local and global acceptance mask registers */
395 hecc_write(priv, HECC_CANGAM, HECC_SET_REG);
396
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);
407 }
408
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);
419 }
420 priv->can.state = CAN_STATE_ERROR_ACTIVE;
421}
422
423static void ti_hecc_stop(struct net_device *ndev)
424{
425 struct ti_hecc_priv *priv = netdev_priv(ndev);
426
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;
432}
433
434static int ti_hecc_do_set_mode(struct net_device *ndev, enum can_mode mode)
435{
436 int ret = 0;
437
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;
446 }
447
448 return ret;
449}
450
451/*
452 * ti_hecc_xmit: HECC Transmit
453 *
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.
459 *
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.
467 *
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.
472 */
473static netdev_tx_t ti_hecc_xmit(struct sk_buff *skb, struct net_device *ndev)
474{
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;
479
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;
490 }
491 spin_unlock_irqrestore(&priv->mbx_lock, flags);
492
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);
499
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);
513
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);
519 }
520 hecc_set_bit(priv, HECC_CANME, mbx_mask);
521 spin_unlock_irqrestore(&priv->mbx_lock, flags);
522
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);
526
527 return NETDEV_TX_OK;
528}
529
530static int ti_hecc_rx_pkt(struct ti_hecc_priv *priv, int mbxno)
531{
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;
537
538 skb = netdev_alloc_skb(priv->ndev, sizeof(struct can_frame));
539 if (!skb) {
540 if (printk_ratelimit())
541 dev_err(priv->ndev->dev.parent,
542 "ti_hecc_rx_pkt: netdev_alloc_skb() failed\n");
543 return -ENOMEM;
544 }
545 skb->protocol = __constant_htons(ETH_P_CAN);
546 skb->ip_summed = CHECKSUM_UNNECESSARY;
547
548 mbx_mask = BIT(mbxno);
549 cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
550 data = hecc_read_mbx(priv, mbxno, HECC_CANMID);
551 if (data & HECC_CANMID_IDE)
552 cf->can_id = (data & CAN_EFF_MASK) | CAN_EFF_FLAG;
553 else
554 cf->can_id = (data >> 18) & CAN_SFF_MASK;
555 data = hecc_read_mbx(priv, mbxno, HECC_CANMCF);
556 if (data & HECC_CANMCF_RTR)
557 cf->can_id |= CAN_RTR_FLAG;
558 cf->can_dlc = data & 0xF;
559 data = hecc_read_mbx(priv, mbxno, HECC_CANMDL);
560 *(u32 *)(cf->data) = cpu_to_be32(data);
561 if (cf->can_dlc > 4) {
562 data = hecc_read_mbx(priv, mbxno, HECC_CANMDH);
563 *(u32 *)(cf->data + 4) = cpu_to_be32(data);
564 } else {
565 *(u32 *)(cf->data + 4) = 0;
566 }
567 spin_lock_irqsave(&priv->mbx_lock, flags);
568 hecc_clear_bit(priv, HECC_CANME, mbx_mask);
569 hecc_write(priv, HECC_CANRMP, mbx_mask);
570 /* enable mailbox only if it is part of rx buffer mailboxes */
571 if (priv->rx_next < HECC_RX_BUFFER_MBOX)
572 hecc_set_bit(priv, HECC_CANME, mbx_mask);
573 spin_unlock_irqrestore(&priv->mbx_lock, flags);
574
575 stats->rx_bytes += cf->can_dlc;
576 netif_receive_skb(skb);
577 stats->rx_packets++;
578
579 return 0;
580}
581
582/*
583 * ti_hecc_rx_poll - HECC receive pkts
584 *
585 * The receive mailboxes start from highest numbered mailbox till last xmit
586 * mailbox. On CAN frame reception the hardware places the data into highest
587 * numbered mailbox that matches the CAN ID filter. Since all receive mailboxes
588 * have same filtering (ALL CAN frames) packets will arrive in the highest
589 * available RX mailbox and we need to ensure in-order packet reception.
590 *
591 * To ensure the packets are received in the right order we logically divide
592 * the RX mailboxes into main and buffer mailboxes. Packets are received as per
593 * mailbox priotity (higher to lower) in the main bank and once it is full we
594 * disable further reception into main mailboxes. While the main mailboxes are
595 * processed in NAPI, further packets are received in buffer mailboxes.
596 *
597 * We maintain a RX next mailbox counter to process packets and once all main
598 * mailboxe packets are passed to the upper stack we enable all of them but
599 * continue to process packets received in buffer mailboxes. With each packet
600 * received from buffer mailbox we enable it immediately so as to handle the
601 * overflow from higher mailboxes.
602 */
603static int ti_hecc_rx_poll(struct napi_struct *napi, int quota)
604{
605 struct net_device *ndev = napi->dev;
606 struct ti_hecc_priv *priv = netdev_priv(ndev);
607 u32 num_pkts = 0;
608 u32 mbx_mask;
609 unsigned long pending_pkts, flags;
610
611 if (!netif_running(ndev))
612 return 0;
613
614 while ((pending_pkts = hecc_read(priv, HECC_CANRMP)) &&
615 num_pkts < quota) {
616 mbx_mask = BIT(priv->rx_next); /* next rx mailbox to process */
617 if (mbx_mask & pending_pkts) {
618 if (ti_hecc_rx_pkt(priv, priv->rx_next) < 0)
619 return num_pkts;
620 ++num_pkts;
621 } else if (priv->rx_next > HECC_RX_BUFFER_MBOX) {
622 break; /* pkt not received yet */
623 }
624 --priv->rx_next;
625 if (priv->rx_next == HECC_RX_BUFFER_MBOX) {
626 /* enable high bank mailboxes */
627 spin_lock_irqsave(&priv->mbx_lock, flags);
628 mbx_mask = hecc_read(priv, HECC_CANME);
629 mbx_mask |= HECC_RX_HIGH_MBOX_MASK;
630 hecc_write(priv, HECC_CANME, mbx_mask);
631 spin_unlock_irqrestore(&priv->mbx_lock, flags);
632 } else if (priv->rx_next == HECC_MAX_TX_MBOX - 1) {
633 priv->rx_next = HECC_RX_FIRST_MBOX;
634 break;
635 }
636 }
637
638 /* Enable packet interrupt if all pkts are handled */
639 if (hecc_read(priv, HECC_CANRMP) == 0) {
640 napi_complete(napi);
641 /* Re-enable RX mailbox interrupts */
642 mbx_mask = hecc_read(priv, HECC_CANMIM);
643 mbx_mask |= HECC_TX_MBOX_MASK;
644 hecc_write(priv, HECC_CANMIM, mbx_mask);
645 }
646
647 return num_pkts;
648}
649
650static int ti_hecc_error(struct net_device *ndev, int int_status,
651 int err_status)
652{
653 struct ti_hecc_priv *priv = netdev_priv(ndev);
654 struct net_device_stats *stats = &ndev->stats;
655 struct can_frame *cf;
656 struct sk_buff *skb;
657
658 /* propogate the error condition to the can stack */
659 skb = netdev_alloc_skb(ndev, sizeof(struct can_frame));
660 if (!skb) {
661 if (printk_ratelimit())
662 dev_err(priv->ndev->dev.parent,
663 "ti_hecc_error: netdev_alloc_skb() failed\n");
664 return -ENOMEM;
665 }
666 skb->protocol = __constant_htons(ETH_P_CAN);
667 skb->ip_summed = CHECKSUM_UNNECESSARY;
668 cf = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
669 memset(cf, 0, sizeof(struct can_frame));
670 cf->can_id = CAN_ERR_FLAG;
671 cf->can_dlc = CAN_ERR_DLC;
672
673 if (int_status & HECC_CANGIF_WLIF) { /* warning level int */
674 if ((int_status & HECC_CANGIF_BOIF) == 0) {
675 priv->can.state = CAN_STATE_ERROR_WARNING;
676 ++priv->can.can_stats.error_warning;
677 cf->can_id |= CAN_ERR_CRTL;
678 if (hecc_read(priv, HECC_CANTEC) > 96)
679 cf->data[1] |= CAN_ERR_CRTL_TX_WARNING;
680 if (hecc_read(priv, HECC_CANREC) > 96)
681 cf->data[1] |= CAN_ERR_CRTL_RX_WARNING;
682 }
683 hecc_set_bit(priv, HECC_CANES, HECC_CANES_EW);
684 dev_dbg(priv->ndev->dev.parent, "Error Warning interrupt\n");
685 hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
686 }
687
688 if (int_status & HECC_CANGIF_EPIF) { /* error passive int */
689 if ((int_status & HECC_CANGIF_BOIF) == 0) {
690 priv->can.state = CAN_STATE_ERROR_PASSIVE;
691 ++priv->can.can_stats.error_passive;
692 cf->can_id |= CAN_ERR_CRTL;
693 if (hecc_read(priv, HECC_CANTEC) > 127)
694 cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
695 if (hecc_read(priv, HECC_CANREC) > 127)
696 cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
697 }
698 hecc_set_bit(priv, HECC_CANES, HECC_CANES_EP);
699 dev_dbg(priv->ndev->dev.parent, "Error passive interrupt\n");
700 hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
701 }
702
703 /*
704 * Need to check busoff condition in error status register too to
705 * ensure warning interrupts don't hog the system
706 */
707 if ((int_status & HECC_CANGIF_BOIF) || (err_status & HECC_CANES_BO)) {
708 priv->can.state = CAN_STATE_BUS_OFF;
709 cf->can_id |= CAN_ERR_BUSOFF;
710 hecc_set_bit(priv, HECC_CANES, HECC_CANES_BO);
711 hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
712 /* Disable all interrupts in bus-off to avoid int hog */
713 hecc_write(priv, HECC_CANGIM, 0);
714 can_bus_off(ndev);
715 }
716
717 if (err_status & HECC_BUS_ERROR) {
718 ++priv->can.can_stats.bus_error;
719 cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
720 cf->data[2] |= CAN_ERR_PROT_UNSPEC;
721 if (err_status & HECC_CANES_FE) {
722 hecc_set_bit(priv, HECC_CANES, HECC_CANES_FE);
723 cf->data[2] |= CAN_ERR_PROT_FORM;
724 }
725 if (err_status & HECC_CANES_BE) {
726 hecc_set_bit(priv, HECC_CANES, HECC_CANES_BE);
727 cf->data[2] |= CAN_ERR_PROT_BIT;
728 }
729 if (err_status & HECC_CANES_SE) {
730 hecc_set_bit(priv, HECC_CANES, HECC_CANES_SE);
731 cf->data[2] |= CAN_ERR_PROT_STUFF;
732 }
733 if (err_status & HECC_CANES_CRCE) {
734 hecc_set_bit(priv, HECC_CANES, HECC_CANES_CRCE);
735 cf->data[2] |= CAN_ERR_PROT_LOC_CRC_SEQ |
736 CAN_ERR_PROT_LOC_CRC_DEL;
737 }
738 if (err_status & HECC_CANES_ACKE) {
739 hecc_set_bit(priv, HECC_CANES, HECC_CANES_ACKE);
740 cf->data[2] |= CAN_ERR_PROT_LOC_ACK |
741 CAN_ERR_PROT_LOC_ACK_DEL;
742 }
743 }
744
745 netif_receive_skb(skb);
746 stats->rx_packets++;
747 stats->rx_bytes += cf->can_dlc;
748 return 0;
749}
750
751static irqreturn_t ti_hecc_interrupt(int irq, void *dev_id)
752{
753 struct net_device *ndev = (struct net_device *)dev_id;
754 struct ti_hecc_priv *priv = netdev_priv(ndev);
755 struct net_device_stats *stats = &ndev->stats;
756 u32 mbxno, mbx_mask, int_status, err_status;
757 unsigned long ack, flags;
758
759 int_status = hecc_read(priv,
760 (priv->int_line) ? HECC_CANGIF1 : HECC_CANGIF0);
761
762 if (!int_status)
763 return IRQ_NONE;
764
765 err_status = hecc_read(priv, HECC_CANES);
766 if (err_status & (HECC_BUS_ERROR | HECC_CANES_BO |
767 HECC_CANES_EP | HECC_CANES_EW))
768 ti_hecc_error(ndev, int_status, err_status);
769
770 if (int_status & HECC_CANGIF_GMIF) {
771 while (priv->tx_tail - priv->tx_head > 0) {
772 mbxno = get_tx_tail_mb(priv);
773 mbx_mask = BIT(mbxno);
774 if (!(mbx_mask & hecc_read(priv, HECC_CANTA)))
775 break;
776 hecc_clear_bit(priv, HECC_CANMIM, mbx_mask);
777 hecc_write(priv, HECC_CANTA, mbx_mask);
778 spin_lock_irqsave(&priv->mbx_lock, flags);
779 hecc_clear_bit(priv, HECC_CANME, mbx_mask);
780 spin_unlock_irqrestore(&priv->mbx_lock, flags);
781 stats->tx_bytes += hecc_read_mbx(priv, mbxno,
782 HECC_CANMCF) & 0xF;
783 stats->tx_packets++;
784 can_get_echo_skb(ndev, mbxno);
785 --priv->tx_tail;
786 }
787
788 /* restart queue if wrap-up or if queue stalled on last pkt */
789 if (((priv->tx_head == priv->tx_tail) &&
790 ((priv->tx_head & HECC_TX_MASK) != HECC_TX_MASK)) ||
791 (((priv->tx_tail & HECC_TX_MASK) == HECC_TX_MASK) &&
792 ((priv->tx_head & HECC_TX_MASK) == HECC_TX_MASK)))
793 netif_wake_queue(ndev);
794
795 /* Disable RX mailbox interrupts and let NAPI reenable them */
796 if (hecc_read(priv, HECC_CANRMP)) {
797 ack = hecc_read(priv, HECC_CANMIM);
798 ack &= BIT(HECC_MAX_TX_MBOX) - 1;
799 hecc_write(priv, HECC_CANMIM, ack);
800 napi_schedule(&priv->napi);
801 }
802 }
803
804 /* clear all interrupt conditions - read back to avoid spurious ints */
805 if (priv->int_line) {
806 hecc_write(priv, HECC_CANGIF1, HECC_SET_REG);
807 int_status = hecc_read(priv, HECC_CANGIF1);
808 } else {
809 hecc_write(priv, HECC_CANGIF0, HECC_SET_REG);
810 int_status = hecc_read(priv, HECC_CANGIF0);
811 }
812
813 return IRQ_HANDLED;
814}
815
816static int ti_hecc_open(struct net_device *ndev)
817{
818 struct ti_hecc_priv *priv = netdev_priv(ndev);
819 int err;
820
821 err = request_irq(ndev->irq, ti_hecc_interrupt, IRQF_SHARED,
822 ndev->name, ndev);
823 if (err) {
824 dev_err(ndev->dev.parent, "error requesting interrupt\n");
825 return err;
826 }
827
828 /* Open common can device */
829 err = open_candev(ndev);
830 if (err) {
831 dev_err(ndev->dev.parent, "open_candev() failed %d\n", err);
832 free_irq(ndev->irq, ndev);
833 return err;
834 }
835
836 clk_enable(priv->clk);
837 ti_hecc_start(ndev);
838 napi_enable(&priv->napi);
839 netif_start_queue(ndev);
840
841 return 0;
842}
843
844static int ti_hecc_close(struct net_device *ndev)
845{
846 struct ti_hecc_priv *priv = netdev_priv(ndev);
847
848 netif_stop_queue(ndev);
849 napi_disable(&priv->napi);
850 ti_hecc_stop(ndev);
851 free_irq(ndev->irq, ndev);
852 clk_disable(priv->clk);
853 close_candev(ndev);
854
855 return 0;
856}
857
858static const struct net_device_ops ti_hecc_netdev_ops = {
859 .ndo_open = ti_hecc_open,
860 .ndo_stop = ti_hecc_close,
861 .ndo_start_xmit = ti_hecc_xmit,
862};
863
864static int ti_hecc_probe(struct platform_device *pdev)
865{
866 struct net_device *ndev = (struct net_device *)0;
867 struct ti_hecc_priv *priv;
868 struct ti_hecc_platform_data *pdata;
869 struct resource *mem, *irq;
870 void __iomem *addr;
871 int err = -ENODEV;
872
873 pdata = pdev->dev.platform_data;
874 if (!pdata) {
875 dev_err(&pdev->dev, "No platform data\n");
876 goto probe_exit;
877 }
878
879 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
880 if (!mem) {
881 dev_err(&pdev->dev, "No mem resources\n");
882 goto probe_exit;
883 }
884 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
885 if (!irq) {
886 dev_err(&pdev->dev, "No irq resource\n");
887 goto probe_exit;
888 }
889 if (!request_mem_region(mem->start, resource_size(mem), pdev->name)) {
890 dev_err(&pdev->dev, "HECC region already claimed\n");
891 err = -EBUSY;
892 goto probe_exit;
893 }
894 addr = ioremap(mem->start, resource_size(mem));
895 if (!addr) {
896 dev_err(&pdev->dev, "ioremap failed\n");
897 err = -ENOMEM;
898 goto probe_exit_free_region;
899 }
900
a6e4bc53 901 ndev = alloc_candev(sizeof(struct ti_hecc_priv), HECC_MAX_TX_MBOX);
3758bf25
AG
902 if (!ndev) {
903 dev_err(&pdev->dev, "alloc_candev failed\n");
904 err = -ENOMEM;
905 goto probe_exit_iounmap;
906 }
907
908 priv = netdev_priv(ndev);
909 priv->ndev = ndev;
910 priv->base = addr;
911 priv->scc_ram_offset = pdata->scc_ram_offset;
912 priv->hecc_ram_offset = pdata->hecc_ram_offset;
913 priv->mbx_offset = pdata->mbx_offset;
914 priv->int_line = pdata->int_line;
915
916 priv->can.bittiming_const = &ti_hecc_bittiming_const;
917 priv->can.do_set_mode = ti_hecc_do_set_mode;
918 priv->can.do_get_state = ti_hecc_get_state;
919
920 ndev->irq = irq->start;
921 ndev->flags |= IFF_ECHO;
922 platform_set_drvdata(pdev, ndev);
923 SET_NETDEV_DEV(ndev, &pdev->dev);
924 ndev->netdev_ops = &ti_hecc_netdev_ops;
925
926 priv->clk = clk_get(&pdev->dev, "hecc_ck");
927 if (IS_ERR(priv->clk)) {
928 dev_err(&pdev->dev, "No clock available\n");
929 err = PTR_ERR(priv->clk);
930 priv->clk = NULL;
931 goto probe_exit_candev;
932 }
933 priv->can.clock.freq = clk_get_rate(priv->clk);
934 netif_napi_add(ndev, &priv->napi, ti_hecc_rx_poll,
935 HECC_DEF_NAPI_WEIGHT);
936
937 err = register_candev(ndev);
938 if (err) {
939 dev_err(&pdev->dev, "register_candev() failed\n");
940 goto probe_exit_clk;
941 }
942 dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%u)\n",
943 priv->base, (u32) ndev->irq);
944
945 return 0;
946
947probe_exit_clk:
948 clk_put(priv->clk);
949probe_exit_candev:
950 free_candev(ndev);
951probe_exit_iounmap:
952 iounmap(addr);
953probe_exit_free_region:
954 release_mem_region(mem->start, resource_size(mem));
955probe_exit:
956 return err;
957}
958
959static int __devexit ti_hecc_remove(struct platform_device *pdev)
960{
961 struct resource *res;
962 struct net_device *ndev = platform_get_drvdata(pdev);
963 struct ti_hecc_priv *priv = netdev_priv(ndev);
964
965 clk_put(priv->clk);
966 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
967 iounmap(priv->base);
968 release_mem_region(res->start, resource_size(res));
969 unregister_candev(ndev);
970 free_candev(ndev);
971 platform_set_drvdata(pdev, NULL);
972
973 return 0;
974}
975
976/* TI HECC netdevice driver: platform driver structure */
977static struct platform_driver ti_hecc_driver = {
978 .driver = {
979 .name = DRV_NAME,
980 .owner = THIS_MODULE,
981 },
982 .probe = ti_hecc_probe,
983 .remove = __devexit_p(ti_hecc_remove),
984};
985
986static int __init ti_hecc_init_driver(void)
987{
988 printk(KERN_INFO DRV_DESC "\n");
989 return platform_driver_register(&ti_hecc_driver);
990}
991module_init(ti_hecc_init_driver);
992
993static void __exit ti_hecc_exit_driver(void)
994{
995 printk(KERN_INFO DRV_DESC " unloaded\n");
996 platform_driver_unregister(&ti_hecc_driver);
997}
998module_exit(ti_hecc_exit_driver);
999
1000MODULE_AUTHOR("Anant Gole <anantgole@ti.com>");
1001MODULE_LICENSE("GPL v2");
1002MODULE_DESCRIPTION(DRV_DESC);