ti_hecc.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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_U32(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. };
  707. static const struct ethtool_ops ti_hecc_ethtool_ops = {
  708. .get_ts_info = ethtool_op_get_ts_info,
  709. };
  710. static const struct of_device_id ti_hecc_dt_ids[] = {
  711. {
  712. .compatible = "ti,am3517-hecc",
  713. },
  714. { }
  715. };
  716. MODULE_DEVICE_TABLE(of, ti_hecc_dt_ids);
  717. static int ti_hecc_probe(struct platform_device *pdev)
  718. {
  719. struct net_device *ndev = (struct net_device *)0;
  720. struct ti_hecc_priv *priv;
  721. struct device_node *np = pdev->dev.of_node;
  722. struct regulator *reg_xceiver;
  723. int err = -ENODEV;
  724. if (!IS_ENABLED(CONFIG_OF) || !np)
  725. return -EINVAL;
  726. reg_xceiver = devm_regulator_get(&pdev->dev, "xceiver");
  727. if (PTR_ERR(reg_xceiver) == -EPROBE_DEFER)
  728. return -EPROBE_DEFER;
  729. else if (IS_ERR(reg_xceiver))
  730. reg_xceiver = NULL;
  731. ndev = alloc_candev(sizeof(struct ti_hecc_priv), HECC_MAX_TX_MBOX);
  732. if (!ndev) {
  733. dev_err(&pdev->dev, "alloc_candev failed\n");
  734. return -ENOMEM;
  735. }
  736. priv = netdev_priv(ndev);
  737. /* handle hecc memory */
  738. priv->base = devm_platform_ioremap_resource_byname(pdev, "hecc");
  739. if (IS_ERR(priv->base)) {
  740. dev_err(&pdev->dev, "hecc ioremap failed\n");
  741. err = PTR_ERR(priv->base);
  742. goto probe_exit_candev;
  743. }
  744. /* handle hecc-ram memory */
  745. priv->hecc_ram = devm_platform_ioremap_resource_byname(pdev,
  746. "hecc-ram");
  747. if (IS_ERR(priv->hecc_ram)) {
  748. dev_err(&pdev->dev, "hecc-ram ioremap failed\n");
  749. err = PTR_ERR(priv->hecc_ram);
  750. goto probe_exit_candev;
  751. }
  752. /* handle mbx memory */
  753. priv->mbx = devm_platform_ioremap_resource_byname(pdev, "mbx");
  754. if (IS_ERR(priv->mbx)) {
  755. dev_err(&pdev->dev, "mbx ioremap failed\n");
  756. err = PTR_ERR(priv->mbx);
  757. goto probe_exit_candev;
  758. }
  759. ndev->irq = platform_get_irq(pdev, 0);
  760. if (ndev->irq < 0) {
  761. err = ndev->irq;
  762. goto probe_exit_candev;
  763. }
  764. priv->ndev = ndev;
  765. priv->reg_xceiver = reg_xceiver;
  766. priv->use_hecc1int = of_property_read_bool(np, "ti,use-hecc1int");
  767. priv->can.bittiming_const = &ti_hecc_bittiming_const;
  768. priv->can.do_set_mode = ti_hecc_do_set_mode;
  769. priv->can.do_get_berr_counter = ti_hecc_get_berr_counter;
  770. priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
  771. spin_lock_init(&priv->mbx_lock);
  772. ndev->flags |= IFF_ECHO;
  773. platform_set_drvdata(pdev, ndev);
  774. SET_NETDEV_DEV(ndev, &pdev->dev);
  775. ndev->netdev_ops = &ti_hecc_netdev_ops;
  776. ndev->ethtool_ops = &ti_hecc_ethtool_ops;
  777. priv->clk = clk_get(&pdev->dev, "hecc_ck");
  778. if (IS_ERR(priv->clk)) {
  779. dev_err(&pdev->dev, "No clock available\n");
  780. err = PTR_ERR(priv->clk);
  781. priv->clk = NULL;
  782. goto probe_exit_candev;
  783. }
  784. priv->can.clock.freq = clk_get_rate(priv->clk);
  785. err = clk_prepare_enable(priv->clk);
  786. if (err) {
  787. dev_err(&pdev->dev, "clk_prepare_enable() failed\n");
  788. goto probe_exit_release_clk;
  789. }
  790. priv->offload.mailbox_read = ti_hecc_mailbox_read;
  791. priv->offload.mb_first = HECC_RX_FIRST_MBOX;
  792. priv->offload.mb_last = HECC_RX_LAST_MBOX;
  793. err = can_rx_offload_add_timestamp(ndev, &priv->offload);
  794. if (err) {
  795. dev_err(&pdev->dev, "can_rx_offload_add_timestamp() failed\n");
  796. goto probe_exit_disable_clk;
  797. }
  798. err = register_candev(ndev);
  799. if (err) {
  800. dev_err(&pdev->dev, "register_candev() failed\n");
  801. goto probe_exit_offload;
  802. }
  803. dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%u)\n",
  804. priv->base, (u32)ndev->irq);
  805. return 0;
  806. probe_exit_offload:
  807. can_rx_offload_del(&priv->offload);
  808. probe_exit_disable_clk:
  809. clk_disable_unprepare(priv->clk);
  810. probe_exit_release_clk:
  811. clk_put(priv->clk);
  812. probe_exit_candev:
  813. free_candev(ndev);
  814. return err;
  815. }
  816. static void ti_hecc_remove(struct platform_device *pdev)
  817. {
  818. struct net_device *ndev = platform_get_drvdata(pdev);
  819. struct ti_hecc_priv *priv = netdev_priv(ndev);
  820. unregister_candev(ndev);
  821. clk_disable_unprepare(priv->clk);
  822. clk_put(priv->clk);
  823. can_rx_offload_del(&priv->offload);
  824. free_candev(ndev);
  825. }
  826. #ifdef CONFIG_PM
  827. static int ti_hecc_suspend(struct platform_device *pdev, pm_message_t state)
  828. {
  829. struct net_device *dev = platform_get_drvdata(pdev);
  830. struct ti_hecc_priv *priv = netdev_priv(dev);
  831. if (netif_running(dev)) {
  832. netif_stop_queue(dev);
  833. netif_device_detach(dev);
  834. }
  835. hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_PDR);
  836. priv->can.state = CAN_STATE_SLEEPING;
  837. clk_disable_unprepare(priv->clk);
  838. return 0;
  839. }
  840. static int ti_hecc_resume(struct platform_device *pdev)
  841. {
  842. struct net_device *dev = platform_get_drvdata(pdev);
  843. struct ti_hecc_priv *priv = netdev_priv(dev);
  844. int err;
  845. err = clk_prepare_enable(priv->clk);
  846. if (err)
  847. return err;
  848. hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_PDR);
  849. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  850. if (netif_running(dev)) {
  851. netif_device_attach(dev);
  852. netif_start_queue(dev);
  853. }
  854. return 0;
  855. }
  856. #else
  857. #define ti_hecc_suspend NULL
  858. #define ti_hecc_resume NULL
  859. #endif
  860. /* TI HECC netdevice driver: platform driver structure */
  861. static struct platform_driver ti_hecc_driver = {
  862. .driver = {
  863. .name = DRV_NAME,
  864. .of_match_table = ti_hecc_dt_ids,
  865. },
  866. .probe = ti_hecc_probe,
  867. .remove = ti_hecc_remove,
  868. .suspend = ti_hecc_suspend,
  869. .resume = ti_hecc_resume,
  870. };
  871. module_platform_driver(ti_hecc_driver);
  872. MODULE_AUTHOR("Anant Gole <anantgole@ti.com>");
  873. MODULE_LICENSE("GPL v2");
  874. MODULE_DESCRIPTION(DRV_DESC);
  875. MODULE_ALIAS("platform:" DRV_NAME);