PageRenderTime 1467ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/net/can/ti_hecc.c

https://github.com/Mengqi/linux-2.6
C | 1056 lines | 764 code | 137 blank | 155 comment | 86 complexity | d5c16435677d266d008deac191dee732 MD5 | raw file
  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. * Your platform definitions should specify module ram offsets and interrupt
  21. * number to use as follows:
  22. *
  23. * static struct ti_hecc_platform_data am3517_evm_hecc_pdata = {
  24. * .scc_hecc_offset = 0,
  25. * .scc_ram_offset = 0x3000,
  26. * .hecc_ram_offset = 0x3000,
  27. * .mbx_offset = 0x2000,
  28. * .int_line = 0,
  29. * .revision = 1,
  30. * .transceiver_switch = hecc_phy_control,
  31. * };
  32. *
  33. * Please see include/linux/can/platform/ti_hecc.h for description of
  34. * above fields.
  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. #include <linux/can/dev.h>
  48. #include <linux/can/error.h>
  49. #include <linux/can/platform/ti_hecc.h>
  50. #define DRV_NAME "ti_hecc"
  51. #define HECC_MODULE_VERSION "0.7"
  52. MODULE_VERSION(HECC_MODULE_VERSION);
  53. #define DRV_DESC "TI High End CAN Controller Driver " HECC_MODULE_VERSION
  54. /* TX / RX Mailbox Configuration */
  55. #define HECC_MAX_MAILBOXES 32 /* hardware mailboxes - do not change */
  56. #define MAX_TX_PRIO 0x3F /* hardware value - do not change */
  57. /*
  58. * Important Note: TX mailbox configuration
  59. * TX mailboxes should be restricted to the number of SKB buffers to avoid
  60. * maintaining SKB buffers separately. TX mailboxes should be a power of 2
  61. * for the mailbox logic to work. Top mailbox numbers are reserved for RX
  62. * and lower mailboxes for TX.
  63. *
  64. * HECC_MAX_TX_MBOX HECC_MB_TX_SHIFT
  65. * 4 (default) 2
  66. * 8 3
  67. * 16 4
  68. */
  69. #define HECC_MB_TX_SHIFT 2 /* as per table above */
  70. #define HECC_MAX_TX_MBOX BIT(HECC_MB_TX_SHIFT)
  71. #define HECC_TX_PRIO_SHIFT (HECC_MB_TX_SHIFT)
  72. #define HECC_TX_PRIO_MASK (MAX_TX_PRIO << HECC_MB_TX_SHIFT)
  73. #define HECC_TX_MB_MASK (HECC_MAX_TX_MBOX - 1)
  74. #define HECC_TX_MASK ((HECC_MAX_TX_MBOX - 1) | HECC_TX_PRIO_MASK)
  75. #define HECC_TX_MBOX_MASK (~(BIT(HECC_MAX_TX_MBOX) - 1))
  76. #define HECC_DEF_NAPI_WEIGHT HECC_MAX_RX_MBOX
  77. /*
  78. * Important Note: RX mailbox configuration
  79. * RX mailboxes are further logically split into two - main and buffer
  80. * mailboxes. The goal is to get all packets into main mailboxes as
  81. * driven by mailbox number and receive priority (higher to lower) and
  82. * buffer mailboxes are used to receive pkts while main mailboxes are being
  83. * processed. This ensures in-order packet reception.
  84. *
  85. * Here are the recommended values for buffer mailbox. Note that RX mailboxes
  86. * start after TX mailboxes:
  87. *
  88. * HECC_MAX_RX_MBOX HECC_RX_BUFFER_MBOX No of buffer mailboxes
  89. * 28 12 8
  90. * 16 20 4
  91. */
  92. #define HECC_MAX_RX_MBOX (HECC_MAX_MAILBOXES - HECC_MAX_TX_MBOX)
  93. #define HECC_RX_BUFFER_MBOX 12 /* as per table above */
  94. #define HECC_RX_FIRST_MBOX (HECC_MAX_MAILBOXES - 1)
  95. #define HECC_RX_HIGH_MBOX_MASK (~(BIT(HECC_RX_BUFFER_MBOX) - 1))
  96. /* TI HECC module registers */
  97. #define HECC_CANME 0x0 /* Mailbox enable */
  98. #define HECC_CANMD 0x4 /* Mailbox direction */
  99. #define HECC_CANTRS 0x8 /* Transmit request set */
  100. #define HECC_CANTRR 0xC /* Transmit request */
  101. #define HECC_CANTA 0x10 /* Transmission acknowledge */
  102. #define HECC_CANAA 0x14 /* Abort acknowledge */
  103. #define HECC_CANRMP 0x18 /* Receive message pending */
  104. #define HECC_CANRML 0x1C /* Remote message lost */
  105. #define HECC_CANRFP 0x20 /* Remote frame pending */
  106. #define HECC_CANGAM 0x24 /* SECC only:Global acceptance mask */
  107. #define HECC_CANMC 0x28 /* Master control */
  108. #define HECC_CANBTC 0x2C /* Bit timing configuration */
  109. #define HECC_CANES 0x30 /* Error and status */
  110. #define HECC_CANTEC 0x34 /* Transmit error counter */
  111. #define HECC_CANREC 0x38 /* Receive error counter */
  112. #define HECC_CANGIF0 0x3C /* Global interrupt flag 0 */
  113. #define HECC_CANGIM 0x40 /* Global interrupt mask */
  114. #define HECC_CANGIF1 0x44 /* Global interrupt flag 1 */
  115. #define HECC_CANMIM 0x48 /* Mailbox interrupt mask */
  116. #define HECC_CANMIL 0x4C /* Mailbox interrupt level */
  117. #define HECC_CANOPC 0x50 /* Overwrite protection control */
  118. #define HECC_CANTIOC 0x54 /* Transmit I/O control */
  119. #define HECC_CANRIOC 0x58 /* Receive I/O control */
  120. #define HECC_CANLNT 0x5C /* HECC only: Local network time */
  121. #define HECC_CANTOC 0x60 /* HECC only: Time-out control */
  122. #define HECC_CANTOS 0x64 /* HECC only: Time-out status */
  123. #define HECC_CANTIOCE 0x68 /* SCC only:Enhanced TX I/O control */
  124. #define HECC_CANRIOCE 0x6C /* SCC only:Enhanced RX I/O control */
  125. /* Mailbox registers */
  126. #define HECC_CANMID 0x0
  127. #define HECC_CANMCF 0x4
  128. #define HECC_CANMDL 0x8
  129. #define HECC_CANMDH 0xC
  130. #define HECC_SET_REG 0xFFFFFFFF
  131. #define HECC_CANID_MASK 0x3FF /* 18 bits mask for extended id's */
  132. #define HECC_CCE_WAIT_COUNT 100 /* Wait for ~1 sec for CCE bit */
  133. #define HECC_CANMC_SCM BIT(13) /* SCC compat mode */
  134. #define HECC_CANMC_CCR BIT(12) /* Change config request */
  135. #define HECC_CANMC_PDR BIT(11) /* Local Power down - for sleep mode */
  136. #define HECC_CANMC_ABO BIT(7) /* Auto Bus On */
  137. #define HECC_CANMC_STM BIT(6) /* Self test mode - loopback */
  138. #define HECC_CANMC_SRES BIT(5) /* Software reset */
  139. #define HECC_CANTIOC_EN BIT(3) /* Enable CAN TX I/O pin */
  140. #define HECC_CANRIOC_EN BIT(3) /* Enable CAN RX I/O pin */
  141. #define HECC_CANMID_IDE BIT(31) /* Extended frame format */
  142. #define HECC_CANMID_AME BIT(30) /* Acceptance mask enable */
  143. #define HECC_CANMID_AAM BIT(29) /* Auto answer mode */
  144. #define HECC_CANES_FE BIT(24) /* form error */
  145. #define HECC_CANES_BE BIT(23) /* bit error */
  146. #define HECC_CANES_SA1 BIT(22) /* stuck at dominant error */
  147. #define HECC_CANES_CRCE BIT(21) /* CRC error */
  148. #define HECC_CANES_SE BIT(20) /* stuff bit error */
  149. #define HECC_CANES_ACKE BIT(19) /* ack error */
  150. #define HECC_CANES_BO BIT(18) /* Bus off status */
  151. #define HECC_CANES_EP BIT(17) /* Error passive status */
  152. #define HECC_CANES_EW BIT(16) /* Error warning status */
  153. #define HECC_CANES_SMA BIT(5) /* suspend mode ack */
  154. #define HECC_CANES_CCE BIT(4) /* Change config enabled */
  155. #define HECC_CANES_PDA BIT(3) /* Power down mode ack */
  156. #define HECC_CANBTC_SAM BIT(7) /* sample points */
  157. #define HECC_BUS_ERROR (HECC_CANES_FE | HECC_CANES_BE |\
  158. HECC_CANES_CRCE | HECC_CANES_SE |\
  159. HECC_CANES_ACKE)
  160. #define HECC_CANMCF_RTR BIT(4) /* Remote transmit request */
  161. #define HECC_CANGIF_MAIF BIT(17) /* Message alarm interrupt */
  162. #define HECC_CANGIF_TCOIF BIT(16) /* Timer counter overflow int */
  163. #define HECC_CANGIF_GMIF BIT(15) /* Global mailbox interrupt */
  164. #define HECC_CANGIF_AAIF BIT(14) /* Abort ack interrupt */
  165. #define HECC_CANGIF_WDIF BIT(13) /* Write denied interrupt */
  166. #define HECC_CANGIF_WUIF BIT(12) /* Wake up interrupt */
  167. #define HECC_CANGIF_RMLIF BIT(11) /* Receive message lost interrupt */
  168. #define HECC_CANGIF_BOIF BIT(10) /* Bus off interrupt */
  169. #define HECC_CANGIF_EPIF BIT(9) /* Error passive interrupt */
  170. #define HECC_CANGIF_WLIF BIT(8) /* Warning level interrupt */
  171. #define HECC_CANGIF_MBOX_MASK 0x1F /* Mailbox number mask */
  172. #define HECC_CANGIM_I1EN BIT(1) /* Int line 1 enable */
  173. #define HECC_CANGIM_I0EN BIT(0) /* Int line 0 enable */
  174. #define HECC_CANGIM_DEF_MASK 0x700 /* only busoff/warning/passive */
  175. #define HECC_CANGIM_SIL BIT(2) /* system interrupts to int line 1 */
  176. /* CAN Bittiming constants as per HECC specs */
  177. static struct can_bittiming_const ti_hecc_bittiming_const = {
  178. .name = DRV_NAME,
  179. .tseg1_min = 1,
  180. .tseg1_max = 16,
  181. .tseg2_min = 1,
  182. .tseg2_max = 8,
  183. .sjw_max = 4,
  184. .brp_min = 1,
  185. .brp_max = 256,
  186. .brp_inc = 1,
  187. };
  188. struct ti_hecc_priv {
  189. struct can_priv can; /* MUST be first member/field */
  190. struct napi_struct napi;
  191. struct net_device *ndev;
  192. struct clk *clk;
  193. void __iomem *base;
  194. u32 scc_ram_offset;
  195. u32 hecc_ram_offset;
  196. u32 mbx_offset;
  197. u32 int_line;
  198. spinlock_t mbx_lock; /* CANME register needs protection */
  199. u32 tx_head;
  200. u32 tx_tail;
  201. u32 rx_next;
  202. void (*transceiver_switch)(int);
  203. };
  204. static inline int get_tx_head_mb(struct ti_hecc_priv *priv)
  205. {
  206. return priv->tx_head & HECC_TX_MB_MASK;
  207. }
  208. static inline int get_tx_tail_mb(struct ti_hecc_priv *priv)
  209. {
  210. return priv->tx_tail & HECC_TX_MB_MASK;
  211. }
  212. static inline int get_tx_head_prio(struct ti_hecc_priv *priv)
  213. {
  214. return (priv->tx_head >> HECC_TX_PRIO_SHIFT) & MAX_TX_PRIO;
  215. }
  216. static inline void hecc_write_lam(struct ti_hecc_priv *priv, u32 mbxno, u32 val)
  217. {
  218. __raw_writel(val, priv->base + priv->hecc_ram_offset + mbxno * 4);
  219. }
  220. static inline void hecc_write_mbx(struct ti_hecc_priv *priv, u32 mbxno,
  221. u32 reg, u32 val)
  222. {
  223. __raw_writel(val, priv->base + priv->mbx_offset + mbxno * 0x10 +
  224. reg);
  225. }
  226. static inline u32 hecc_read_mbx(struct ti_hecc_priv *priv, u32 mbxno, u32 reg)
  227. {
  228. return __raw_readl(priv->base + priv->mbx_offset + mbxno * 0x10 +
  229. reg);
  230. }
  231. static inline void hecc_write(struct ti_hecc_priv *priv, u32 reg, u32 val)
  232. {
  233. __raw_writel(val, priv->base + reg);
  234. }
  235. static inline u32 hecc_read(struct ti_hecc_priv *priv, int reg)
  236. {
  237. return __raw_readl(priv->base + reg);
  238. }
  239. static inline void hecc_set_bit(struct ti_hecc_priv *priv, int reg,
  240. u32 bit_mask)
  241. {
  242. hecc_write(priv, reg, hecc_read(priv, reg) | bit_mask);
  243. }
  244. static inline void hecc_clear_bit(struct ti_hecc_priv *priv, int reg,
  245. u32 bit_mask)
  246. {
  247. hecc_write(priv, reg, hecc_read(priv, reg) & ~bit_mask);
  248. }
  249. static inline u32 hecc_get_bit(struct ti_hecc_priv *priv, int reg, u32 bit_mask)
  250. {
  251. return (hecc_read(priv, reg) & bit_mask) ? 1 : 0;
  252. }
  253. static int ti_hecc_get_state(const struct net_device *ndev,
  254. enum can_state *state)
  255. {
  256. struct ti_hecc_priv *priv = netdev_priv(ndev);
  257. *state = priv->can.state;
  258. return 0;
  259. }
  260. static int ti_hecc_set_btc(struct ti_hecc_priv *priv)
  261. {
  262. struct can_bittiming *bit_timing = &priv->can.bittiming;
  263. u32 can_btc;
  264. can_btc = (bit_timing->phase_seg2 - 1) & 0x7;
  265. can_btc |= ((bit_timing->phase_seg1 + bit_timing->prop_seg - 1)
  266. & 0xF) << 3;
  267. if (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) {
  268. if (bit_timing->brp > 4)
  269. can_btc |= HECC_CANBTC_SAM;
  270. else
  271. dev_warn(priv->ndev->dev.parent, "WARN: Triple" \
  272. "sampling not set due to h/w limitations");
  273. }
  274. can_btc |= ((bit_timing->sjw - 1) & 0x3) << 8;
  275. can_btc |= ((bit_timing->brp - 1) & 0xFF) << 16;
  276. /* ERM being set to 0 by default meaning resync at falling edge */
  277. hecc_write(priv, HECC_CANBTC, can_btc);
  278. dev_info(priv->ndev->dev.parent, "setting CANBTC=%#x\n", can_btc);
  279. return 0;
  280. }
  281. static void ti_hecc_transceiver_switch(const struct ti_hecc_priv *priv,
  282. int on)
  283. {
  284. if (priv->transceiver_switch)
  285. priv->transceiver_switch(on);
  286. }
  287. static void ti_hecc_reset(struct net_device *ndev)
  288. {
  289. u32 cnt;
  290. struct ti_hecc_priv *priv = netdev_priv(ndev);
  291. dev_dbg(ndev->dev.parent, "resetting hecc ...\n");
  292. hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_SRES);
  293. /* Set change control request and wait till enabled */
  294. hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
  295. /*
  296. * INFO: It has been observed that at times CCE bit may not be
  297. * set and hw seems to be ok even if this bit is not set so
  298. * timing out with a timing of 1ms to respect the specs
  299. */
  300. cnt = HECC_CCE_WAIT_COUNT;
  301. while (!hecc_get_bit(priv, HECC_CANES, HECC_CANES_CCE) && cnt != 0) {
  302. --cnt;
  303. udelay(10);
  304. }
  305. /*
  306. * Note: On HECC, BTC can be programmed only in initialization mode, so
  307. * it is expected that the can bittiming parameters are set via ip
  308. * utility before the device is opened
  309. */
  310. ti_hecc_set_btc(priv);
  311. /* Clear CCR (and CANMC register) and wait for CCE = 0 enable */
  312. hecc_write(priv, HECC_CANMC, 0);
  313. /*
  314. * INFO: CAN net stack handles bus off and hence disabling auto-bus-on
  315. * hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_ABO);
  316. */
  317. /*
  318. * INFO: It has been observed that at times CCE bit may not be
  319. * set and hw seems to be ok even if this bit is not set so
  320. */
  321. cnt = HECC_CCE_WAIT_COUNT;
  322. while (hecc_get_bit(priv, HECC_CANES, HECC_CANES_CCE) && cnt != 0) {
  323. --cnt;
  324. udelay(10);
  325. }
  326. /* Enable TX and RX I/O Control pins */
  327. hecc_write(priv, HECC_CANTIOC, HECC_CANTIOC_EN);
  328. hecc_write(priv, HECC_CANRIOC, HECC_CANRIOC_EN);
  329. /* Clear registers for clean operation */
  330. hecc_write(priv, HECC_CANTA, HECC_SET_REG);
  331. hecc_write(priv, HECC_CANRMP, HECC_SET_REG);
  332. hecc_write(priv, HECC_CANGIF0, HECC_SET_REG);
  333. hecc_write(priv, HECC_CANGIF1, HECC_SET_REG);
  334. hecc_write(priv, HECC_CANME, 0);
  335. hecc_write(priv, HECC_CANMD, 0);
  336. /* SCC compat mode NOT supported (and not needed too) */
  337. hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_SCM);
  338. }
  339. static void ti_hecc_start(struct net_device *ndev)
  340. {
  341. struct ti_hecc_priv *priv = netdev_priv(ndev);
  342. u32 cnt, mbxno, mbx_mask;
  343. /* put HECC in initialization mode and set btc */
  344. ti_hecc_reset(ndev);
  345. priv->tx_head = priv->tx_tail = HECC_TX_MASK;
  346. priv->rx_next = HECC_RX_FIRST_MBOX;
  347. /* Enable local and global acceptance mask registers */
  348. hecc_write(priv, HECC_CANGAM, HECC_SET_REG);
  349. /* Prepare configured mailboxes to receive messages */
  350. for (cnt = 0; cnt < HECC_MAX_RX_MBOX; cnt++) {
  351. mbxno = HECC_MAX_MAILBOXES - 1 - cnt;
  352. mbx_mask = BIT(mbxno);
  353. hecc_clear_bit(priv, HECC_CANME, mbx_mask);
  354. hecc_write_mbx(priv, mbxno, HECC_CANMID, HECC_CANMID_AME);
  355. hecc_write_lam(priv, mbxno, HECC_SET_REG);
  356. hecc_set_bit(priv, HECC_CANMD, mbx_mask);
  357. hecc_set_bit(priv, HECC_CANME, mbx_mask);
  358. hecc_set_bit(priv, HECC_CANMIM, mbx_mask);
  359. }
  360. /* Prevent message over-write & Enable interrupts */
  361. hecc_write(priv, HECC_CANOPC, HECC_SET_REG);
  362. if (priv->int_line) {
  363. hecc_write(priv, HECC_CANMIL, HECC_SET_REG);
  364. hecc_write(priv, HECC_CANGIM, HECC_CANGIM_DEF_MASK |
  365. HECC_CANGIM_I1EN | HECC_CANGIM_SIL);
  366. } else {
  367. hecc_write(priv, HECC_CANMIL, 0);
  368. hecc_write(priv, HECC_CANGIM,
  369. HECC_CANGIM_DEF_MASK | HECC_CANGIM_I0EN);
  370. }
  371. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  372. }
  373. static void ti_hecc_stop(struct net_device *ndev)
  374. {
  375. struct ti_hecc_priv *priv = netdev_priv(ndev);
  376. /* Disable interrupts and disable mailboxes */
  377. hecc_write(priv, HECC_CANGIM, 0);
  378. hecc_write(priv, HECC_CANMIM, 0);
  379. hecc_write(priv, HECC_CANME, 0);
  380. priv->can.state = CAN_STATE_STOPPED;
  381. }
  382. static int ti_hecc_do_set_mode(struct net_device *ndev, enum can_mode mode)
  383. {
  384. int ret = 0;
  385. switch (mode) {
  386. case CAN_MODE_START:
  387. ti_hecc_start(ndev);
  388. netif_wake_queue(ndev);
  389. break;
  390. default:
  391. ret = -EOPNOTSUPP;
  392. break;
  393. }
  394. return ret;
  395. }
  396. /*
  397. * ti_hecc_xmit: HECC Transmit
  398. *
  399. * The transmit mailboxes start from 0 to HECC_MAX_TX_MBOX. In HECC the
  400. * priority of the mailbox for tranmission is dependent upon priority setting
  401. * field in mailbox registers. The mailbox with highest value in priority field
  402. * is transmitted first. Only when two mailboxes have the same value in
  403. * priority field the highest numbered mailbox is transmitted first.
  404. *
  405. * To utilize the HECC priority feature as described above we start with the
  406. * highest numbered mailbox with highest priority level and move on to the next
  407. * mailbox with the same priority level and so on. Once we loop through all the
  408. * transmit mailboxes we choose the next priority level (lower) and so on
  409. * until we reach the lowest priority level on the lowest numbered mailbox
  410. * when we stop transmission until all mailboxes are transmitted and then
  411. * restart at highest numbered mailbox with highest priority.
  412. *
  413. * Two counters (head and tail) are used to track the next mailbox to transmit
  414. * and to track the echo buffer for already transmitted mailbox. The queue
  415. * is stopped when all the mailboxes are busy or when there is a priority
  416. * value roll-over happens.
  417. */
  418. static netdev_tx_t ti_hecc_xmit(struct sk_buff *skb, struct net_device *ndev)
  419. {
  420. struct ti_hecc_priv *priv = netdev_priv(ndev);
  421. struct can_frame *cf = (struct can_frame *)skb->data;
  422. u32 mbxno, mbx_mask, data;
  423. unsigned long flags;
  424. if (can_dropped_invalid_skb(ndev, skb))
  425. return NETDEV_TX_OK;
  426. mbxno = get_tx_head_mb(priv);
  427. mbx_mask = BIT(mbxno);
  428. spin_lock_irqsave(&priv->mbx_lock, flags);
  429. if (unlikely(hecc_read(priv, HECC_CANME) & mbx_mask)) {
  430. spin_unlock_irqrestore(&priv->mbx_lock, flags);
  431. netif_stop_queue(ndev);
  432. dev_err(priv->ndev->dev.parent,
  433. "BUG: TX mbx not ready tx_head=%08X, tx_tail=%08X\n",
  434. priv->tx_head, priv->tx_tail);
  435. return NETDEV_TX_BUSY;
  436. }
  437. spin_unlock_irqrestore(&priv->mbx_lock, flags);
  438. /* Prepare mailbox for transmission */
  439. data = cf->can_dlc | (get_tx_head_prio(priv) << 8);
  440. if (cf->can_id & CAN_RTR_FLAG) /* Remote transmission request */
  441. data |= HECC_CANMCF_RTR;
  442. hecc_write_mbx(priv, mbxno, HECC_CANMCF, data);
  443. if (cf->can_id & CAN_EFF_FLAG) /* Extended frame format */
  444. data = (cf->can_id & CAN_EFF_MASK) | HECC_CANMID_IDE;
  445. else /* Standard frame format */
  446. data = (cf->can_id & CAN_SFF_MASK) << 18;
  447. hecc_write_mbx(priv, mbxno, HECC_CANMID, data);
  448. hecc_write_mbx(priv, mbxno, HECC_CANMDL,
  449. be32_to_cpu(*(u32 *)(cf->data)));
  450. if (cf->can_dlc > 4)
  451. hecc_write_mbx(priv, mbxno, HECC_CANMDH,
  452. be32_to_cpu(*(u32 *)(cf->data + 4)));
  453. else
  454. *(u32 *)(cf->data + 4) = 0;
  455. can_put_echo_skb(skb, ndev, mbxno);
  456. spin_lock_irqsave(&priv->mbx_lock, flags);
  457. --priv->tx_head;
  458. if ((hecc_read(priv, HECC_CANME) & BIT(get_tx_head_mb(priv))) ||
  459. (priv->tx_head & HECC_TX_MASK) == HECC_TX_MASK) {
  460. netif_stop_queue(ndev);
  461. }
  462. hecc_set_bit(priv, HECC_CANME, mbx_mask);
  463. spin_unlock_irqrestore(&priv->mbx_lock, flags);
  464. hecc_clear_bit(priv, HECC_CANMD, mbx_mask);
  465. hecc_set_bit(priv, HECC_CANMIM, mbx_mask);
  466. hecc_write(priv, HECC_CANTRS, mbx_mask);
  467. return NETDEV_TX_OK;
  468. }
  469. static int ti_hecc_rx_pkt(struct ti_hecc_priv *priv, int mbxno)
  470. {
  471. struct net_device_stats *stats = &priv->ndev->stats;
  472. struct can_frame *cf;
  473. struct sk_buff *skb;
  474. u32 data, mbx_mask;
  475. unsigned long flags;
  476. skb = alloc_can_skb(priv->ndev, &cf);
  477. if (!skb) {
  478. if (printk_ratelimit())
  479. dev_err(priv->ndev->dev.parent,
  480. "ti_hecc_rx_pkt: alloc_can_skb() failed\n");
  481. return -ENOMEM;
  482. }
  483. mbx_mask = BIT(mbxno);
  484. data = hecc_read_mbx(priv, mbxno, HECC_CANMID);
  485. if (data & HECC_CANMID_IDE)
  486. cf->can_id = (data & CAN_EFF_MASK) | CAN_EFF_FLAG;
  487. else
  488. cf->can_id = (data >> 18) & CAN_SFF_MASK;
  489. data = hecc_read_mbx(priv, mbxno, HECC_CANMCF);
  490. if (data & HECC_CANMCF_RTR)
  491. cf->can_id |= CAN_RTR_FLAG;
  492. cf->can_dlc = get_can_dlc(data & 0xF);
  493. data = hecc_read_mbx(priv, mbxno, HECC_CANMDL);
  494. *(u32 *)(cf->data) = cpu_to_be32(data);
  495. if (cf->can_dlc > 4) {
  496. data = hecc_read_mbx(priv, mbxno, HECC_CANMDH);
  497. *(u32 *)(cf->data + 4) = cpu_to_be32(data);
  498. } else {
  499. *(u32 *)(cf->data + 4) = 0;
  500. }
  501. spin_lock_irqsave(&priv->mbx_lock, flags);
  502. hecc_clear_bit(priv, HECC_CANME, mbx_mask);
  503. hecc_write(priv, HECC_CANRMP, mbx_mask);
  504. /* enable mailbox only if it is part of rx buffer mailboxes */
  505. if (priv->rx_next < HECC_RX_BUFFER_MBOX)
  506. hecc_set_bit(priv, HECC_CANME, mbx_mask);
  507. spin_unlock_irqrestore(&priv->mbx_lock, flags);
  508. stats->rx_bytes += cf->can_dlc;
  509. netif_receive_skb(skb);
  510. stats->rx_packets++;
  511. return 0;
  512. }
  513. /*
  514. * ti_hecc_rx_poll - HECC receive pkts
  515. *
  516. * The receive mailboxes start from highest numbered mailbox till last xmit
  517. * mailbox. On CAN frame reception the hardware places the data into highest
  518. * numbered mailbox that matches the CAN ID filter. Since all receive mailboxes
  519. * have same filtering (ALL CAN frames) packets will arrive in the highest
  520. * available RX mailbox and we need to ensure in-order packet reception.
  521. *
  522. * To ensure the packets are received in the right order we logically divide
  523. * the RX mailboxes into main and buffer mailboxes. Packets are received as per
  524. * mailbox priotity (higher to lower) in the main bank and once it is full we
  525. * disable further reception into main mailboxes. While the main mailboxes are
  526. * processed in NAPI, further packets are received in buffer mailboxes.
  527. *
  528. * We maintain a RX next mailbox counter to process packets and once all main
  529. * mailboxe packets are passed to the upper stack we enable all of them but
  530. * continue to process packets received in buffer mailboxes. With each packet
  531. * received from buffer mailbox we enable it immediately so as to handle the
  532. * overflow from higher mailboxes.
  533. */
  534. static int ti_hecc_rx_poll(struct napi_struct *napi, int quota)
  535. {
  536. struct net_device *ndev = napi->dev;
  537. struct ti_hecc_priv *priv = netdev_priv(ndev);
  538. u32 num_pkts = 0;
  539. u32 mbx_mask;
  540. unsigned long pending_pkts, flags;
  541. if (!netif_running(ndev))
  542. return 0;
  543. while ((pending_pkts = hecc_read(priv, HECC_CANRMP)) &&
  544. num_pkts < quota) {
  545. mbx_mask = BIT(priv->rx_next); /* next rx mailbox to process */
  546. if (mbx_mask & pending_pkts) {
  547. if (ti_hecc_rx_pkt(priv, priv->rx_next) < 0)
  548. return num_pkts;
  549. ++num_pkts;
  550. } else if (priv->rx_next > HECC_RX_BUFFER_MBOX) {
  551. break; /* pkt not received yet */
  552. }
  553. --priv->rx_next;
  554. if (priv->rx_next == HECC_RX_BUFFER_MBOX) {
  555. /* enable high bank mailboxes */
  556. spin_lock_irqsave(&priv->mbx_lock, flags);
  557. mbx_mask = hecc_read(priv, HECC_CANME);
  558. mbx_mask |= HECC_RX_HIGH_MBOX_MASK;
  559. hecc_write(priv, HECC_CANME, mbx_mask);
  560. spin_unlock_irqrestore(&priv->mbx_lock, flags);
  561. } else if (priv->rx_next == HECC_MAX_TX_MBOX - 1) {
  562. priv->rx_next = HECC_RX_FIRST_MBOX;
  563. break;
  564. }
  565. }
  566. /* Enable packet interrupt if all pkts are handled */
  567. if (hecc_read(priv, HECC_CANRMP) == 0) {
  568. napi_complete(napi);
  569. /* Re-enable RX mailbox interrupts */
  570. mbx_mask = hecc_read(priv, HECC_CANMIM);
  571. mbx_mask |= HECC_TX_MBOX_MASK;
  572. hecc_write(priv, HECC_CANMIM, mbx_mask);
  573. }
  574. return num_pkts;
  575. }
  576. static int ti_hecc_error(struct net_device *ndev, int int_status,
  577. int err_status)
  578. {
  579. struct ti_hecc_priv *priv = netdev_priv(ndev);
  580. struct net_device_stats *stats = &ndev->stats;
  581. struct can_frame *cf;
  582. struct sk_buff *skb;
  583. /* propagate the error condition to the can stack */
  584. skb = alloc_can_err_skb(ndev, &cf);
  585. if (!skb) {
  586. if (printk_ratelimit())
  587. dev_err(priv->ndev->dev.parent,
  588. "ti_hecc_error: alloc_can_err_skb() failed\n");
  589. return -ENOMEM;
  590. }
  591. if (int_status & HECC_CANGIF_WLIF) { /* warning level int */
  592. if ((int_status & HECC_CANGIF_BOIF) == 0) {
  593. priv->can.state = CAN_STATE_ERROR_WARNING;
  594. ++priv->can.can_stats.error_warning;
  595. cf->can_id |= CAN_ERR_CRTL;
  596. if (hecc_read(priv, HECC_CANTEC) > 96)
  597. cf->data[1] |= CAN_ERR_CRTL_TX_WARNING;
  598. if (hecc_read(priv, HECC_CANREC) > 96)
  599. cf->data[1] |= CAN_ERR_CRTL_RX_WARNING;
  600. }
  601. hecc_set_bit(priv, HECC_CANES, HECC_CANES_EW);
  602. dev_dbg(priv->ndev->dev.parent, "Error Warning interrupt\n");
  603. hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
  604. }
  605. if (int_status & HECC_CANGIF_EPIF) { /* error passive int */
  606. if ((int_status & HECC_CANGIF_BOIF) == 0) {
  607. priv->can.state = CAN_STATE_ERROR_PASSIVE;
  608. ++priv->can.can_stats.error_passive;
  609. cf->can_id |= CAN_ERR_CRTL;
  610. if (hecc_read(priv, HECC_CANTEC) > 127)
  611. cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
  612. if (hecc_read(priv, HECC_CANREC) > 127)
  613. cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
  614. }
  615. hecc_set_bit(priv, HECC_CANES, HECC_CANES_EP);
  616. dev_dbg(priv->ndev->dev.parent, "Error passive interrupt\n");
  617. hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
  618. }
  619. /*
  620. * Need to check busoff condition in error status register too to
  621. * ensure warning interrupts don't hog the system
  622. */
  623. if ((int_status & HECC_CANGIF_BOIF) || (err_status & HECC_CANES_BO)) {
  624. priv->can.state = CAN_STATE_BUS_OFF;
  625. cf->can_id |= CAN_ERR_BUSOFF;
  626. hecc_set_bit(priv, HECC_CANES, HECC_CANES_BO);
  627. hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_CCR);
  628. /* Disable all interrupts in bus-off to avoid int hog */
  629. hecc_write(priv, HECC_CANGIM, 0);
  630. can_bus_off(ndev);
  631. }
  632. if (err_status & HECC_BUS_ERROR) {
  633. ++priv->can.can_stats.bus_error;
  634. cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT;
  635. cf->data[2] |= CAN_ERR_PROT_UNSPEC;
  636. if (err_status & HECC_CANES_FE) {
  637. hecc_set_bit(priv, HECC_CANES, HECC_CANES_FE);
  638. cf->data[2] |= CAN_ERR_PROT_FORM;
  639. }
  640. if (err_status & HECC_CANES_BE) {
  641. hecc_set_bit(priv, HECC_CANES, HECC_CANES_BE);
  642. cf->data[2] |= CAN_ERR_PROT_BIT;
  643. }
  644. if (err_status & HECC_CANES_SE) {
  645. hecc_set_bit(priv, HECC_CANES, HECC_CANES_SE);
  646. cf->data[2] |= CAN_ERR_PROT_STUFF;
  647. }
  648. if (err_status & HECC_CANES_CRCE) {
  649. hecc_set_bit(priv, HECC_CANES, HECC_CANES_CRCE);
  650. cf->data[2] |= CAN_ERR_PROT_LOC_CRC_SEQ |
  651. CAN_ERR_PROT_LOC_CRC_DEL;
  652. }
  653. if (err_status & HECC_CANES_ACKE) {
  654. hecc_set_bit(priv, HECC_CANES, HECC_CANES_ACKE);
  655. cf->data[2] |= CAN_ERR_PROT_LOC_ACK |
  656. CAN_ERR_PROT_LOC_ACK_DEL;
  657. }
  658. }
  659. netif_receive_skb(skb);
  660. stats->rx_packets++;
  661. stats->rx_bytes += cf->can_dlc;
  662. return 0;
  663. }
  664. static irqreturn_t ti_hecc_interrupt(int irq, void *dev_id)
  665. {
  666. struct net_device *ndev = (struct net_device *)dev_id;
  667. struct ti_hecc_priv *priv = netdev_priv(ndev);
  668. struct net_device_stats *stats = &ndev->stats;
  669. u32 mbxno, mbx_mask, int_status, err_status;
  670. unsigned long ack, flags;
  671. int_status = hecc_read(priv,
  672. (priv->int_line) ? HECC_CANGIF1 : HECC_CANGIF0);
  673. if (!int_status)
  674. return IRQ_NONE;
  675. err_status = hecc_read(priv, HECC_CANES);
  676. if (err_status & (HECC_BUS_ERROR | HECC_CANES_BO |
  677. HECC_CANES_EP | HECC_CANES_EW))
  678. ti_hecc_error(ndev, int_status, err_status);
  679. if (int_status & HECC_CANGIF_GMIF) {
  680. while (priv->tx_tail - priv->tx_head > 0) {
  681. mbxno = get_tx_tail_mb(priv);
  682. mbx_mask = BIT(mbxno);
  683. if (!(mbx_mask & hecc_read(priv, HECC_CANTA)))
  684. break;
  685. hecc_clear_bit(priv, HECC_CANMIM, mbx_mask);
  686. hecc_write(priv, HECC_CANTA, mbx_mask);
  687. spin_lock_irqsave(&priv->mbx_lock, flags);
  688. hecc_clear_bit(priv, HECC_CANME, mbx_mask);
  689. spin_unlock_irqrestore(&priv->mbx_lock, flags);
  690. stats->tx_bytes += hecc_read_mbx(priv, mbxno,
  691. HECC_CANMCF) & 0xF;
  692. stats->tx_packets++;
  693. can_get_echo_skb(ndev, mbxno);
  694. --priv->tx_tail;
  695. }
  696. /* restart queue if wrap-up or if queue stalled on last pkt */
  697. if (((priv->tx_head == priv->tx_tail) &&
  698. ((priv->tx_head & HECC_TX_MASK) != HECC_TX_MASK)) ||
  699. (((priv->tx_tail & HECC_TX_MASK) == HECC_TX_MASK) &&
  700. ((priv->tx_head & HECC_TX_MASK) == HECC_TX_MASK)))
  701. netif_wake_queue(ndev);
  702. /* Disable RX mailbox interrupts and let NAPI reenable them */
  703. if (hecc_read(priv, HECC_CANRMP)) {
  704. ack = hecc_read(priv, HECC_CANMIM);
  705. ack &= BIT(HECC_MAX_TX_MBOX) - 1;
  706. hecc_write(priv, HECC_CANMIM, ack);
  707. napi_schedule(&priv->napi);
  708. }
  709. }
  710. /* clear all interrupt conditions - read back to avoid spurious ints */
  711. if (priv->int_line) {
  712. hecc_write(priv, HECC_CANGIF1, HECC_SET_REG);
  713. int_status = hecc_read(priv, HECC_CANGIF1);
  714. } else {
  715. hecc_write(priv, HECC_CANGIF0, HECC_SET_REG);
  716. int_status = hecc_read(priv, HECC_CANGIF0);
  717. }
  718. return IRQ_HANDLED;
  719. }
  720. static int ti_hecc_open(struct net_device *ndev)
  721. {
  722. struct ti_hecc_priv *priv = netdev_priv(ndev);
  723. int err;
  724. err = request_irq(ndev->irq, ti_hecc_interrupt, IRQF_SHARED,
  725. ndev->name, ndev);
  726. if (err) {
  727. dev_err(ndev->dev.parent, "error requesting interrupt\n");
  728. return err;
  729. }
  730. ti_hecc_transceiver_switch(priv, 1);
  731. /* Open common can device */
  732. err = open_candev(ndev);
  733. if (err) {
  734. dev_err(ndev->dev.parent, "open_candev() failed %d\n", err);
  735. ti_hecc_transceiver_switch(priv, 0);
  736. free_irq(ndev->irq, ndev);
  737. return err;
  738. }
  739. ti_hecc_start(ndev);
  740. napi_enable(&priv->napi);
  741. netif_start_queue(ndev);
  742. return 0;
  743. }
  744. static int ti_hecc_close(struct net_device *ndev)
  745. {
  746. struct ti_hecc_priv *priv = netdev_priv(ndev);
  747. netif_stop_queue(ndev);
  748. napi_disable(&priv->napi);
  749. ti_hecc_stop(ndev);
  750. free_irq(ndev->irq, ndev);
  751. close_candev(ndev);
  752. ti_hecc_transceiver_switch(priv, 0);
  753. return 0;
  754. }
  755. static const struct net_device_ops ti_hecc_netdev_ops = {
  756. .ndo_open = ti_hecc_open,
  757. .ndo_stop = ti_hecc_close,
  758. .ndo_start_xmit = ti_hecc_xmit,
  759. };
  760. static int ti_hecc_probe(struct platform_device *pdev)
  761. {
  762. struct net_device *ndev = (struct net_device *)0;
  763. struct ti_hecc_priv *priv;
  764. struct ti_hecc_platform_data *pdata;
  765. struct resource *mem, *irq;
  766. void __iomem *addr;
  767. int err = -ENODEV;
  768. pdata = pdev->dev.platform_data;
  769. if (!pdata) {
  770. dev_err(&pdev->dev, "No platform data\n");
  771. goto probe_exit;
  772. }
  773. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  774. if (!mem) {
  775. dev_err(&pdev->dev, "No mem resources\n");
  776. goto probe_exit;
  777. }
  778. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  779. if (!irq) {
  780. dev_err(&pdev->dev, "No irq resource\n");
  781. goto probe_exit;
  782. }
  783. if (!request_mem_region(mem->start, resource_size(mem), pdev->name)) {
  784. dev_err(&pdev->dev, "HECC region already claimed\n");
  785. err = -EBUSY;
  786. goto probe_exit;
  787. }
  788. addr = ioremap(mem->start, resource_size(mem));
  789. if (!addr) {
  790. dev_err(&pdev->dev, "ioremap failed\n");
  791. err = -ENOMEM;
  792. goto probe_exit_free_region;
  793. }
  794. ndev = alloc_candev(sizeof(struct ti_hecc_priv), HECC_MAX_TX_MBOX);
  795. if (!ndev) {
  796. dev_err(&pdev->dev, "alloc_candev failed\n");
  797. err = -ENOMEM;
  798. goto probe_exit_iounmap;
  799. }
  800. priv = netdev_priv(ndev);
  801. priv->ndev = ndev;
  802. priv->base = addr;
  803. priv->scc_ram_offset = pdata->scc_ram_offset;
  804. priv->hecc_ram_offset = pdata->hecc_ram_offset;
  805. priv->mbx_offset = pdata->mbx_offset;
  806. priv->int_line = pdata->int_line;
  807. priv->transceiver_switch = pdata->transceiver_switch;
  808. priv->can.bittiming_const = &ti_hecc_bittiming_const;
  809. priv->can.do_set_mode = ti_hecc_do_set_mode;
  810. priv->can.do_get_state = ti_hecc_get_state;
  811. priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
  812. spin_lock_init(&priv->mbx_lock);
  813. ndev->irq = irq->start;
  814. ndev->flags |= IFF_ECHO;
  815. platform_set_drvdata(pdev, ndev);
  816. SET_NETDEV_DEV(ndev, &pdev->dev);
  817. ndev->netdev_ops = &ti_hecc_netdev_ops;
  818. priv->clk = clk_get(&pdev->dev, "hecc_ck");
  819. if (IS_ERR(priv->clk)) {
  820. dev_err(&pdev->dev, "No clock available\n");
  821. err = PTR_ERR(priv->clk);
  822. priv->clk = NULL;
  823. goto probe_exit_candev;
  824. }
  825. priv->can.clock.freq = clk_get_rate(priv->clk);
  826. netif_napi_add(ndev, &priv->napi, ti_hecc_rx_poll,
  827. HECC_DEF_NAPI_WEIGHT);
  828. clk_enable(priv->clk);
  829. err = register_candev(ndev);
  830. if (err) {
  831. dev_err(&pdev->dev, "register_candev() failed\n");
  832. goto probe_exit_clk;
  833. }
  834. dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%u)\n",
  835. priv->base, (u32) ndev->irq);
  836. return 0;
  837. probe_exit_clk:
  838. clk_put(priv->clk);
  839. probe_exit_candev:
  840. free_candev(ndev);
  841. probe_exit_iounmap:
  842. iounmap(addr);
  843. probe_exit_free_region:
  844. release_mem_region(mem->start, resource_size(mem));
  845. probe_exit:
  846. return err;
  847. }
  848. static int __devexit ti_hecc_remove(struct platform_device *pdev)
  849. {
  850. struct resource *res;
  851. struct net_device *ndev = platform_get_drvdata(pdev);
  852. struct ti_hecc_priv *priv = netdev_priv(ndev);
  853. clk_disable(priv->clk);
  854. clk_put(priv->clk);
  855. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  856. iounmap(priv->base);
  857. release_mem_region(res->start, resource_size(res));
  858. unregister_candev(ndev);
  859. free_candev(ndev);
  860. platform_set_drvdata(pdev, NULL);
  861. return 0;
  862. }
  863. #ifdef CONFIG_PM
  864. static int ti_hecc_suspend(struct platform_device *pdev, pm_message_t state)
  865. {
  866. struct net_device *dev = platform_get_drvdata(pdev);
  867. struct ti_hecc_priv *priv = netdev_priv(dev);
  868. if (netif_running(dev)) {
  869. netif_stop_queue(dev);
  870. netif_device_detach(dev);
  871. }
  872. hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_PDR);
  873. priv->can.state = CAN_STATE_SLEEPING;
  874. clk_disable(priv->clk);
  875. return 0;
  876. }
  877. static int ti_hecc_resume(struct platform_device *pdev)
  878. {
  879. struct net_device *dev = platform_get_drvdata(pdev);
  880. struct ti_hecc_priv *priv = netdev_priv(dev);
  881. clk_enable(priv->clk);
  882. hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_PDR);
  883. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  884. if (netif_running(dev)) {
  885. netif_device_attach(dev);
  886. netif_start_queue(dev);
  887. }
  888. return 0;
  889. }
  890. #else
  891. #define ti_hecc_suspend NULL
  892. #define ti_hecc_resume NULL
  893. #endif
  894. /* TI HECC netdevice driver: platform driver structure */
  895. static struct platform_driver ti_hecc_driver = {
  896. .driver = {
  897. .name = DRV_NAME,
  898. .owner = THIS_MODULE,
  899. },
  900. .probe = ti_hecc_probe,
  901. .remove = __devexit_p(ti_hecc_remove),
  902. .suspend = ti_hecc_suspend,
  903. .resume = ti_hecc_resume,
  904. };
  905. static int __init ti_hecc_init_driver(void)
  906. {
  907. printk(KERN_INFO DRV_DESC "\n");
  908. return platform_driver_register(&ti_hecc_driver);
  909. }
  910. static void __exit ti_hecc_exit_driver(void)
  911. {
  912. printk(KERN_INFO DRV_DESC " unloaded\n");
  913. platform_driver_unregister(&ti_hecc_driver);
  914. }
  915. module_exit(ti_hecc_exit_driver);
  916. module_init(ti_hecc_init_driver);
  917. MODULE_AUTHOR("Anant Gole <anantgole@ti.com>");
  918. MODULE_LICENSE("GPL v2");
  919. MODULE_DESCRIPTION(DRV_DESC);