ti_hecc.c 32 KB

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