ti_hecc.c 31 KB

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