ti_hecc.c 30 KB

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