ti_hecc.c 30 KB

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