ti_hecc.c 32 KB

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