c_can.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  1. /*
  2. * CAN bus driver for Bosch C_CAN controller
  3. *
  4. * Copyright (C) 2010 ST Microelectronics
  5. * Bhupesh Sharma <bhupesh.sharma@st.com>
  6. *
  7. * Borrowed heavily from the C_CAN driver originally written by:
  8. * Copyright (C) 2007
  9. * - Sascha Hauer, Marc Kleine-Budde, Pengutronix <s.hauer@pengutronix.de>
  10. * - Simon Kallweit, intefo AG <simon.kallweit@intefo.ch>
  11. *
  12. * TX and RX NAPI implementation has been borrowed from at91 CAN driver
  13. * written by:
  14. * Copyright
  15. * (C) 2007 by Hans J. Koch <hjk@hansjkoch.de>
  16. * (C) 2008, 2009 by Marc Kleine-Budde <kernel@pengutronix.de>
  17. *
  18. * Bosch C_CAN controller is compliant to CAN protocol version 2.0 part A and B.
  19. * Bosch C_CAN user manual can be obtained from:
  20. * http://www.semiconductors.bosch.de/media/en/pdf/ipmodules_1/c_can/
  21. * users_manual_c_can.pdf
  22. *
  23. * This file is licensed under the terms of the GNU General Public
  24. * License version 2. This program is licensed "as is" without any
  25. * warranty of any kind, whether express or implied.
  26. */
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/delay.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/if_arp.h>
  33. #include <linux/if_ether.h>
  34. #include <linux/list.h>
  35. #include <linux/io.h>
  36. #include <linux/pm_runtime.h>
  37. #include <linux/can.h>
  38. #include <linux/can/dev.h>
  39. #include <linux/can/error.h>
  40. #include "c_can.h"
  41. /* Number of interface registers */
  42. #define IF_ENUM_REG_LEN 11
  43. #define C_CAN_IFACE(reg, iface) (C_CAN_IF1_##reg + (iface) * IF_ENUM_REG_LEN)
  44. /* control extension register D_CAN specific */
  45. #define CONTROL_EX_PDR BIT(8)
  46. /* control register */
  47. #define CONTROL_TEST BIT(7)
  48. #define CONTROL_CCE BIT(6)
  49. #define CONTROL_DISABLE_AR BIT(5)
  50. #define CONTROL_ENABLE_AR (0 << 5)
  51. #define CONTROL_EIE BIT(3)
  52. #define CONTROL_SIE BIT(2)
  53. #define CONTROL_IE BIT(1)
  54. #define CONTROL_INIT BIT(0)
  55. /* test register */
  56. #define TEST_RX BIT(7)
  57. #define TEST_TX1 BIT(6)
  58. #define TEST_TX2 BIT(5)
  59. #define TEST_LBACK BIT(4)
  60. #define TEST_SILENT BIT(3)
  61. #define TEST_BASIC BIT(2)
  62. /* status register */
  63. #define STATUS_PDA BIT(10)
  64. #define STATUS_BOFF BIT(7)
  65. #define STATUS_EWARN BIT(6)
  66. #define STATUS_EPASS BIT(5)
  67. #define STATUS_RXOK BIT(4)
  68. #define STATUS_TXOK BIT(3)
  69. /* error counter register */
  70. #define ERR_CNT_TEC_MASK 0xff
  71. #define ERR_CNT_TEC_SHIFT 0
  72. #define ERR_CNT_REC_SHIFT 8
  73. #define ERR_CNT_REC_MASK (0x7f << ERR_CNT_REC_SHIFT)
  74. #define ERR_CNT_RP_SHIFT 15
  75. #define ERR_CNT_RP_MASK (0x1 << ERR_CNT_RP_SHIFT)
  76. /* bit-timing register */
  77. #define BTR_BRP_MASK 0x3f
  78. #define BTR_BRP_SHIFT 0
  79. #define BTR_SJW_SHIFT 6
  80. #define BTR_SJW_MASK (0x3 << BTR_SJW_SHIFT)
  81. #define BTR_TSEG1_SHIFT 8
  82. #define BTR_TSEG1_MASK (0xf << BTR_TSEG1_SHIFT)
  83. #define BTR_TSEG2_SHIFT 12
  84. #define BTR_TSEG2_MASK (0x7 << BTR_TSEG2_SHIFT)
  85. /* brp extension register */
  86. #define BRP_EXT_BRPE_MASK 0x0f
  87. #define BRP_EXT_BRPE_SHIFT 0
  88. /* IFx command request */
  89. #define IF_COMR_BUSY BIT(15)
  90. /* IFx command mask */
  91. #define IF_COMM_WR BIT(7)
  92. #define IF_COMM_MASK BIT(6)
  93. #define IF_COMM_ARB BIT(5)
  94. #define IF_COMM_CONTROL BIT(4)
  95. #define IF_COMM_CLR_INT_PND BIT(3)
  96. #define IF_COMM_TXRQST BIT(2)
  97. #define IF_COMM_DATAA BIT(1)
  98. #define IF_COMM_DATAB BIT(0)
  99. #define IF_COMM_ALL (IF_COMM_MASK | IF_COMM_ARB | \
  100. IF_COMM_CONTROL | IF_COMM_TXRQST | \
  101. IF_COMM_DATAA | IF_COMM_DATAB)
  102. /* IFx arbitration */
  103. #define IF_ARB_MSGVAL BIT(15)
  104. #define IF_ARB_MSGXTD BIT(14)
  105. #define IF_ARB_TRANSMIT BIT(13)
  106. /* IFx message control */
  107. #define IF_MCONT_NEWDAT BIT(15)
  108. #define IF_MCONT_MSGLST BIT(14)
  109. #define IF_MCONT_CLR_MSGLST (0 << 14)
  110. #define IF_MCONT_INTPND BIT(13)
  111. #define IF_MCONT_UMASK BIT(12)
  112. #define IF_MCONT_TXIE BIT(11)
  113. #define IF_MCONT_RXIE BIT(10)
  114. #define IF_MCONT_RMTEN BIT(9)
  115. #define IF_MCONT_TXRQST BIT(8)
  116. #define IF_MCONT_EOB BIT(7)
  117. #define IF_MCONT_DLC_MASK 0xf
  118. /*
  119. * IFx register masks:
  120. * allow easy operation on 16-bit registers when the
  121. * argument is 32-bit instead
  122. */
  123. #define IFX_WRITE_LOW_16BIT(x) ((x) & 0xFFFF)
  124. #define IFX_WRITE_HIGH_16BIT(x) (((x) & 0xFFFF0000) >> 16)
  125. /* message object split */
  126. #define C_CAN_NO_OF_OBJECTS 32
  127. #define C_CAN_MSG_OBJ_RX_NUM 16
  128. #define C_CAN_MSG_OBJ_TX_NUM 16
  129. #define C_CAN_MSG_OBJ_RX_FIRST 1
  130. #define C_CAN_MSG_OBJ_RX_LAST (C_CAN_MSG_OBJ_RX_FIRST + \
  131. C_CAN_MSG_OBJ_RX_NUM - 1)
  132. #define C_CAN_MSG_OBJ_TX_FIRST (C_CAN_MSG_OBJ_RX_LAST + 1)
  133. #define C_CAN_MSG_OBJ_TX_LAST (C_CAN_MSG_OBJ_TX_FIRST + \
  134. C_CAN_MSG_OBJ_TX_NUM - 1)
  135. #define C_CAN_MSG_OBJ_RX_SPLIT 9
  136. #define C_CAN_MSG_RX_LOW_LAST (C_CAN_MSG_OBJ_RX_SPLIT - 1)
  137. #define C_CAN_NEXT_MSG_OBJ_MASK (C_CAN_MSG_OBJ_TX_NUM - 1)
  138. #define RECEIVE_OBJECT_BITS 0x0000ffff
  139. /* status interrupt */
  140. #define STATUS_INTERRUPT 0x8000
  141. /* global interrupt masks */
  142. #define ENABLE_ALL_INTERRUPTS 1
  143. #define DISABLE_ALL_INTERRUPTS 0
  144. /* minimum timeout for checking BUSY status */
  145. #define MIN_TIMEOUT_VALUE 6
  146. /* Wait for ~1 sec for INIT bit */
  147. #define INIT_WAIT_MS 1000
  148. /* napi related */
  149. #define C_CAN_NAPI_WEIGHT C_CAN_MSG_OBJ_RX_NUM
  150. /* c_can lec values */
  151. enum c_can_lec_type {
  152. LEC_NO_ERROR = 0,
  153. LEC_STUFF_ERROR,
  154. LEC_FORM_ERROR,
  155. LEC_ACK_ERROR,
  156. LEC_BIT1_ERROR,
  157. LEC_BIT0_ERROR,
  158. LEC_CRC_ERROR,
  159. LEC_UNUSED,
  160. };
  161. /*
  162. * c_can error types:
  163. * Bus errors (BUS_OFF, ERROR_WARNING, ERROR_PASSIVE) are supported
  164. */
  165. enum c_can_bus_error_types {
  166. C_CAN_NO_ERROR = 0,
  167. C_CAN_BUS_OFF,
  168. C_CAN_ERROR_WARNING,
  169. C_CAN_ERROR_PASSIVE,
  170. };
  171. static const struct can_bittiming_const c_can_bittiming_const = {
  172. .name = KBUILD_MODNAME,
  173. .tseg1_min = 2, /* Time segment 1 = prop_seg + phase_seg1 */
  174. .tseg1_max = 16,
  175. .tseg2_min = 1, /* Time segment 2 = phase_seg2 */
  176. .tseg2_max = 8,
  177. .sjw_max = 4,
  178. .brp_min = 1,
  179. .brp_max = 1024, /* 6-bit BRP field + 4-bit BRPE field*/
  180. .brp_inc = 1,
  181. };
  182. static inline void c_can_pm_runtime_enable(const struct c_can_priv *priv)
  183. {
  184. if (priv->device)
  185. pm_runtime_enable(priv->device);
  186. }
  187. static inline void c_can_pm_runtime_disable(const struct c_can_priv *priv)
  188. {
  189. if (priv->device)
  190. pm_runtime_disable(priv->device);
  191. }
  192. static inline void c_can_pm_runtime_get_sync(const struct c_can_priv *priv)
  193. {
  194. if (priv->device)
  195. pm_runtime_get_sync(priv->device);
  196. }
  197. static inline void c_can_pm_runtime_put_sync(const struct c_can_priv *priv)
  198. {
  199. if (priv->device)
  200. pm_runtime_put_sync(priv->device);
  201. }
  202. static inline void c_can_reset_ram(const struct c_can_priv *priv, bool enable)
  203. {
  204. if (priv->raminit)
  205. priv->raminit(priv, enable);
  206. }
  207. static inline int get_tx_next_msg_obj(const struct c_can_priv *priv)
  208. {
  209. return (priv->tx_next & C_CAN_NEXT_MSG_OBJ_MASK) +
  210. C_CAN_MSG_OBJ_TX_FIRST;
  211. }
  212. static inline int get_tx_echo_msg_obj(const struct c_can_priv *priv)
  213. {
  214. return (priv->tx_echo & C_CAN_NEXT_MSG_OBJ_MASK) +
  215. C_CAN_MSG_OBJ_TX_FIRST;
  216. }
  217. static u32 c_can_read_reg32(struct c_can_priv *priv, enum reg index)
  218. {
  219. u32 val = priv->read_reg(priv, index);
  220. val |= ((u32) priv->read_reg(priv, index + 1)) << 16;
  221. return val;
  222. }
  223. static void c_can_enable_all_interrupts(struct c_can_priv *priv,
  224. int enable)
  225. {
  226. unsigned int cntrl_save = priv->read_reg(priv,
  227. C_CAN_CTRL_REG);
  228. if (enable)
  229. cntrl_save |= (CONTROL_SIE | CONTROL_EIE | CONTROL_IE);
  230. else
  231. cntrl_save &= ~(CONTROL_EIE | CONTROL_IE | CONTROL_SIE);
  232. priv->write_reg(priv, C_CAN_CTRL_REG, cntrl_save);
  233. }
  234. static inline int c_can_msg_obj_is_busy(struct c_can_priv *priv, int iface)
  235. {
  236. int count = MIN_TIMEOUT_VALUE;
  237. while (count && priv->read_reg(priv,
  238. C_CAN_IFACE(COMREQ_REG, iface)) &
  239. IF_COMR_BUSY) {
  240. count--;
  241. udelay(1);
  242. }
  243. if (!count)
  244. return 1;
  245. return 0;
  246. }
  247. static inline void c_can_object_get(struct net_device *dev,
  248. int iface, int objno, int mask)
  249. {
  250. struct c_can_priv *priv = netdev_priv(dev);
  251. /*
  252. * As per specs, after writting the message object number in the
  253. * IF command request register the transfer b/w interface
  254. * register and message RAM must be complete in 6 CAN-CLK
  255. * period.
  256. */
  257. priv->write_reg(priv, C_CAN_IFACE(COMMSK_REG, iface),
  258. IFX_WRITE_LOW_16BIT(mask));
  259. priv->write_reg(priv, C_CAN_IFACE(COMREQ_REG, iface),
  260. IFX_WRITE_LOW_16BIT(objno));
  261. if (c_can_msg_obj_is_busy(priv, iface))
  262. netdev_err(dev, "timed out in object get\n");
  263. }
  264. static inline void c_can_object_put(struct net_device *dev,
  265. int iface, int objno, int mask)
  266. {
  267. struct c_can_priv *priv = netdev_priv(dev);
  268. /*
  269. * As per specs, after writting the message object number in the
  270. * IF command request register the transfer b/w interface
  271. * register and message RAM must be complete in 6 CAN-CLK
  272. * period.
  273. */
  274. priv->write_reg(priv, C_CAN_IFACE(COMMSK_REG, iface),
  275. (IF_COMM_WR | IFX_WRITE_LOW_16BIT(mask)));
  276. priv->write_reg(priv, C_CAN_IFACE(COMREQ_REG, iface),
  277. IFX_WRITE_LOW_16BIT(objno));
  278. if (c_can_msg_obj_is_busy(priv, iface))
  279. netdev_err(dev, "timed out in object put\n");
  280. }
  281. static void c_can_write_msg_object(struct net_device *dev,
  282. int iface, struct can_frame *frame, int objno)
  283. {
  284. int i;
  285. u16 flags = 0;
  286. unsigned int id;
  287. struct c_can_priv *priv = netdev_priv(dev);
  288. if (!(frame->can_id & CAN_RTR_FLAG))
  289. flags |= IF_ARB_TRANSMIT;
  290. if (frame->can_id & CAN_EFF_FLAG) {
  291. id = frame->can_id & CAN_EFF_MASK;
  292. flags |= IF_ARB_MSGXTD;
  293. } else
  294. id = ((frame->can_id & CAN_SFF_MASK) << 18);
  295. flags |= IF_ARB_MSGVAL;
  296. priv->write_reg(priv, C_CAN_IFACE(ARB1_REG, iface),
  297. IFX_WRITE_LOW_16BIT(id));
  298. priv->write_reg(priv, C_CAN_IFACE(ARB2_REG, iface), flags |
  299. IFX_WRITE_HIGH_16BIT(id));
  300. for (i = 0; i < frame->can_dlc; i += 2) {
  301. priv->write_reg(priv, C_CAN_IFACE(DATA1_REG, iface) + i / 2,
  302. frame->data[i] | (frame->data[i + 1] << 8));
  303. }
  304. /* enable interrupt for this message object */
  305. priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface),
  306. IF_MCONT_TXIE | IF_MCONT_TXRQST | IF_MCONT_EOB |
  307. frame->can_dlc);
  308. c_can_object_put(dev, iface, objno, IF_COMM_ALL);
  309. }
  310. static inline void c_can_mark_rx_msg_obj(struct net_device *dev,
  311. int iface, int ctrl_mask,
  312. int obj)
  313. {
  314. struct c_can_priv *priv = netdev_priv(dev);
  315. priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface),
  316. ctrl_mask & ~(IF_MCONT_MSGLST | IF_MCONT_INTPND));
  317. c_can_object_put(dev, iface, obj, IF_COMM_CONTROL);
  318. }
  319. static inline void c_can_activate_all_lower_rx_msg_obj(struct net_device *dev,
  320. int iface,
  321. int ctrl_mask)
  322. {
  323. int i;
  324. struct c_can_priv *priv = netdev_priv(dev);
  325. for (i = C_CAN_MSG_OBJ_RX_FIRST; i <= C_CAN_MSG_RX_LOW_LAST; i++) {
  326. priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface),
  327. ctrl_mask & ~(IF_MCONT_MSGLST |
  328. IF_MCONT_INTPND | IF_MCONT_NEWDAT));
  329. c_can_object_put(dev, iface, i, IF_COMM_CONTROL);
  330. }
  331. }
  332. static inline void c_can_activate_rx_msg_obj(struct net_device *dev,
  333. int iface, int ctrl_mask,
  334. int obj)
  335. {
  336. struct c_can_priv *priv = netdev_priv(dev);
  337. priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface),
  338. ctrl_mask & ~(IF_MCONT_MSGLST |
  339. IF_MCONT_INTPND | IF_MCONT_NEWDAT));
  340. c_can_object_put(dev, iface, obj, IF_COMM_CONTROL);
  341. }
  342. static void c_can_handle_lost_msg_obj(struct net_device *dev,
  343. int iface, int objno)
  344. {
  345. struct c_can_priv *priv = netdev_priv(dev);
  346. struct net_device_stats *stats = &dev->stats;
  347. struct sk_buff *skb;
  348. struct can_frame *frame;
  349. netdev_err(dev, "msg lost in buffer %d\n", objno);
  350. c_can_object_get(dev, iface, objno, IF_COMM_ALL & ~IF_COMM_TXRQST);
  351. priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface),
  352. IF_MCONT_CLR_MSGLST);
  353. c_can_object_put(dev, 0, objno, IF_COMM_CONTROL);
  354. /* create an error msg */
  355. skb = alloc_can_err_skb(dev, &frame);
  356. if (unlikely(!skb))
  357. return;
  358. frame->can_id |= CAN_ERR_CRTL;
  359. frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  360. stats->rx_errors++;
  361. stats->rx_over_errors++;
  362. netif_receive_skb(skb);
  363. }
  364. static int c_can_read_msg_object(struct net_device *dev, int iface, int ctrl)
  365. {
  366. u16 flags, data;
  367. int i;
  368. unsigned int val;
  369. struct c_can_priv *priv = netdev_priv(dev);
  370. struct net_device_stats *stats = &dev->stats;
  371. struct sk_buff *skb;
  372. struct can_frame *frame;
  373. skb = alloc_can_skb(dev, &frame);
  374. if (!skb) {
  375. stats->rx_dropped++;
  376. return -ENOMEM;
  377. }
  378. frame->can_dlc = get_can_dlc(ctrl & 0x0F);
  379. flags = priv->read_reg(priv, C_CAN_IFACE(ARB2_REG, iface));
  380. val = priv->read_reg(priv, C_CAN_IFACE(ARB1_REG, iface)) |
  381. (flags << 16);
  382. if (flags & IF_ARB_MSGXTD)
  383. frame->can_id = (val & CAN_EFF_MASK) | CAN_EFF_FLAG;
  384. else
  385. frame->can_id = (val >> 18) & CAN_SFF_MASK;
  386. if (flags & IF_ARB_TRANSMIT)
  387. frame->can_id |= CAN_RTR_FLAG;
  388. else {
  389. for (i = 0; i < frame->can_dlc; i += 2) {
  390. data = priv->read_reg(priv,
  391. C_CAN_IFACE(DATA1_REG, iface) + i / 2);
  392. frame->data[i] = data;
  393. frame->data[i + 1] = data >> 8;
  394. }
  395. }
  396. netif_receive_skb(skb);
  397. stats->rx_packets++;
  398. stats->rx_bytes += frame->can_dlc;
  399. return 0;
  400. }
  401. static void c_can_setup_receive_object(struct net_device *dev, int iface,
  402. int objno, unsigned int mask,
  403. unsigned int id, unsigned int mcont)
  404. {
  405. struct c_can_priv *priv = netdev_priv(dev);
  406. priv->write_reg(priv, C_CAN_IFACE(MASK1_REG, iface),
  407. IFX_WRITE_LOW_16BIT(mask));
  408. priv->write_reg(priv, C_CAN_IFACE(MASK2_REG, iface),
  409. IFX_WRITE_HIGH_16BIT(mask));
  410. priv->write_reg(priv, C_CAN_IFACE(ARB1_REG, iface),
  411. IFX_WRITE_LOW_16BIT(id));
  412. priv->write_reg(priv, C_CAN_IFACE(ARB2_REG, iface),
  413. (IF_ARB_MSGVAL | IFX_WRITE_HIGH_16BIT(id)));
  414. priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), mcont);
  415. c_can_object_put(dev, iface, objno, IF_COMM_ALL & ~IF_COMM_TXRQST);
  416. netdev_dbg(dev, "obj no:%d, msgval:0x%08x\n", objno,
  417. c_can_read_reg32(priv, C_CAN_MSGVAL1_REG));
  418. }
  419. static void c_can_inval_msg_object(struct net_device *dev, int iface, int objno)
  420. {
  421. struct c_can_priv *priv = netdev_priv(dev);
  422. priv->write_reg(priv, C_CAN_IFACE(ARB1_REG, iface), 0);
  423. priv->write_reg(priv, C_CAN_IFACE(ARB2_REG, iface), 0);
  424. priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), 0);
  425. c_can_object_put(dev, iface, objno, IF_COMM_ARB | IF_COMM_CONTROL);
  426. netdev_dbg(dev, "obj no:%d, msgval:0x%08x\n", objno,
  427. c_can_read_reg32(priv, C_CAN_MSGVAL1_REG));
  428. }
  429. static inline int c_can_is_next_tx_obj_busy(struct c_can_priv *priv, int objno)
  430. {
  431. int val = c_can_read_reg32(priv, C_CAN_TXRQST1_REG);
  432. /*
  433. * as transmission request register's bit n-1 corresponds to
  434. * message object n, we need to handle the same properly.
  435. */
  436. if (val & (1 << (objno - 1)))
  437. return 1;
  438. return 0;
  439. }
  440. static netdev_tx_t c_can_start_xmit(struct sk_buff *skb,
  441. struct net_device *dev)
  442. {
  443. u32 msg_obj_no;
  444. struct c_can_priv *priv = netdev_priv(dev);
  445. struct can_frame *frame = (struct can_frame *)skb->data;
  446. if (can_dropped_invalid_skb(dev, skb))
  447. return NETDEV_TX_OK;
  448. msg_obj_no = get_tx_next_msg_obj(priv);
  449. /* prepare message object for transmission */
  450. c_can_write_msg_object(dev, 0, frame, msg_obj_no);
  451. can_put_echo_skb(skb, dev, msg_obj_no - C_CAN_MSG_OBJ_TX_FIRST);
  452. /*
  453. * we have to stop the queue in case of a wrap around or
  454. * if the next TX message object is still in use
  455. */
  456. priv->tx_next++;
  457. if (c_can_is_next_tx_obj_busy(priv, get_tx_next_msg_obj(priv)) ||
  458. (priv->tx_next & C_CAN_NEXT_MSG_OBJ_MASK) == 0)
  459. netif_stop_queue(dev);
  460. return NETDEV_TX_OK;
  461. }
  462. static int c_can_set_bittiming(struct net_device *dev)
  463. {
  464. unsigned int reg_btr, reg_brpe, ctrl_save;
  465. u8 brp, brpe, sjw, tseg1, tseg2;
  466. u32 ten_bit_brp;
  467. struct c_can_priv *priv = netdev_priv(dev);
  468. const struct can_bittiming *bt = &priv->can.bittiming;
  469. /* c_can provides a 6-bit brp and 4-bit brpe fields */
  470. ten_bit_brp = bt->brp - 1;
  471. brp = ten_bit_brp & BTR_BRP_MASK;
  472. brpe = ten_bit_brp >> 6;
  473. sjw = bt->sjw - 1;
  474. tseg1 = bt->prop_seg + bt->phase_seg1 - 1;
  475. tseg2 = bt->phase_seg2 - 1;
  476. reg_btr = brp | (sjw << BTR_SJW_SHIFT) | (tseg1 << BTR_TSEG1_SHIFT) |
  477. (tseg2 << BTR_TSEG2_SHIFT);
  478. reg_brpe = brpe & BRP_EXT_BRPE_MASK;
  479. netdev_info(dev,
  480. "setting BTR=%04x BRPE=%04x\n", reg_btr, reg_brpe);
  481. ctrl_save = priv->read_reg(priv, C_CAN_CTRL_REG);
  482. priv->write_reg(priv, C_CAN_CTRL_REG,
  483. ctrl_save | CONTROL_CCE | CONTROL_INIT);
  484. priv->write_reg(priv, C_CAN_BTR_REG, reg_btr);
  485. priv->write_reg(priv, C_CAN_BRPEXT_REG, reg_brpe);
  486. priv->write_reg(priv, C_CAN_CTRL_REG, ctrl_save);
  487. return 0;
  488. }
  489. /*
  490. * Configure C_CAN message objects for Tx and Rx purposes:
  491. * C_CAN provides a total of 32 message objects that can be configured
  492. * either for Tx or Rx purposes. Here the first 16 message objects are used as
  493. * a reception FIFO. The end of reception FIFO is signified by the EoB bit
  494. * being SET. The remaining 16 message objects are kept aside for Tx purposes.
  495. * See user guide document for further details on configuring message
  496. * objects.
  497. */
  498. static void c_can_configure_msg_objects(struct net_device *dev)
  499. {
  500. int i;
  501. /* first invalidate all message objects */
  502. for (i = C_CAN_MSG_OBJ_RX_FIRST; i <= C_CAN_NO_OF_OBJECTS; i++)
  503. c_can_inval_msg_object(dev, 0, i);
  504. /* setup receive message objects */
  505. for (i = C_CAN_MSG_OBJ_RX_FIRST; i < C_CAN_MSG_OBJ_RX_LAST; i++)
  506. c_can_setup_receive_object(dev, 0, i, 0, 0,
  507. (IF_MCONT_RXIE | IF_MCONT_UMASK) & ~IF_MCONT_EOB);
  508. c_can_setup_receive_object(dev, 0, C_CAN_MSG_OBJ_RX_LAST, 0, 0,
  509. IF_MCONT_EOB | IF_MCONT_RXIE | IF_MCONT_UMASK);
  510. }
  511. /*
  512. * Configure C_CAN chip:
  513. * - enable/disable auto-retransmission
  514. * - set operating mode
  515. * - configure message objects
  516. */
  517. static void c_can_chip_config(struct net_device *dev)
  518. {
  519. struct c_can_priv *priv = netdev_priv(dev);
  520. /* enable automatic retransmission */
  521. priv->write_reg(priv, C_CAN_CTRL_REG,
  522. CONTROL_ENABLE_AR);
  523. if ((priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) &&
  524. (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)) {
  525. /* loopback + silent mode : useful for hot self-test */
  526. priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_EIE |
  527. CONTROL_SIE | CONTROL_IE | CONTROL_TEST);
  528. priv->write_reg(priv, C_CAN_TEST_REG,
  529. TEST_LBACK | TEST_SILENT);
  530. } else if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) {
  531. /* loopback mode : useful for self-test function */
  532. priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_EIE |
  533. CONTROL_SIE | CONTROL_IE | CONTROL_TEST);
  534. priv->write_reg(priv, C_CAN_TEST_REG, TEST_LBACK);
  535. } else if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) {
  536. /* silent mode : bus-monitoring mode */
  537. priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_EIE |
  538. CONTROL_SIE | CONTROL_IE | CONTROL_TEST);
  539. priv->write_reg(priv, C_CAN_TEST_REG, TEST_SILENT);
  540. } else
  541. /* normal mode*/
  542. priv->write_reg(priv, C_CAN_CTRL_REG,
  543. CONTROL_EIE | CONTROL_SIE | CONTROL_IE);
  544. /* configure message objects */
  545. c_can_configure_msg_objects(dev);
  546. /* set a `lec` value so that we can check for updates later */
  547. priv->write_reg(priv, C_CAN_STS_REG, LEC_UNUSED);
  548. /* set bittiming params */
  549. c_can_set_bittiming(dev);
  550. }
  551. static void c_can_start(struct net_device *dev)
  552. {
  553. struct c_can_priv *priv = netdev_priv(dev);
  554. /* basic c_can configuration */
  555. c_can_chip_config(dev);
  556. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  557. /* reset tx helper pointers */
  558. priv->tx_next = priv->tx_echo = 0;
  559. /* enable status change, error and module interrupts */
  560. c_can_enable_all_interrupts(priv, ENABLE_ALL_INTERRUPTS);
  561. }
  562. static void c_can_stop(struct net_device *dev)
  563. {
  564. struct c_can_priv *priv = netdev_priv(dev);
  565. /* disable all interrupts */
  566. c_can_enable_all_interrupts(priv, DISABLE_ALL_INTERRUPTS);
  567. /* set the state as STOPPED */
  568. priv->can.state = CAN_STATE_STOPPED;
  569. }
  570. static int c_can_set_mode(struct net_device *dev, enum can_mode mode)
  571. {
  572. switch (mode) {
  573. case CAN_MODE_START:
  574. c_can_start(dev);
  575. netif_wake_queue(dev);
  576. break;
  577. default:
  578. return -EOPNOTSUPP;
  579. }
  580. return 0;
  581. }
  582. static int c_can_get_berr_counter(const struct net_device *dev,
  583. struct can_berr_counter *bec)
  584. {
  585. unsigned int reg_err_counter;
  586. struct c_can_priv *priv = netdev_priv(dev);
  587. c_can_pm_runtime_get_sync(priv);
  588. reg_err_counter = priv->read_reg(priv, C_CAN_ERR_CNT_REG);
  589. bec->rxerr = (reg_err_counter & ERR_CNT_REC_MASK) >>
  590. ERR_CNT_REC_SHIFT;
  591. bec->txerr = reg_err_counter & ERR_CNT_TEC_MASK;
  592. c_can_pm_runtime_put_sync(priv);
  593. return 0;
  594. }
  595. /*
  596. * theory of operation:
  597. *
  598. * priv->tx_echo holds the number of the oldest can_frame put for
  599. * transmission into the hardware, but not yet ACKed by the CAN tx
  600. * complete IRQ.
  601. *
  602. * We iterate from priv->tx_echo to priv->tx_next and check if the
  603. * packet has been transmitted, echo it back to the CAN framework.
  604. * If we discover a not yet transmitted packet, stop looking for more.
  605. */
  606. static void c_can_do_tx(struct net_device *dev)
  607. {
  608. u32 val;
  609. u32 msg_obj_no;
  610. struct c_can_priv *priv = netdev_priv(dev);
  611. struct net_device_stats *stats = &dev->stats;
  612. for (/* nix */; (priv->tx_next - priv->tx_echo) > 0; priv->tx_echo++) {
  613. msg_obj_no = get_tx_echo_msg_obj(priv);
  614. val = c_can_read_reg32(priv, C_CAN_TXRQST1_REG);
  615. if (!(val & (1 << (msg_obj_no - 1)))) {
  616. can_get_echo_skb(dev,
  617. msg_obj_no - C_CAN_MSG_OBJ_TX_FIRST);
  618. stats->tx_bytes += priv->read_reg(priv,
  619. C_CAN_IFACE(MSGCTRL_REG, 0))
  620. & IF_MCONT_DLC_MASK;
  621. stats->tx_packets++;
  622. c_can_inval_msg_object(dev, 0, msg_obj_no);
  623. } else {
  624. break;
  625. }
  626. }
  627. /* restart queue if wrap-up or if queue stalled on last pkt */
  628. if (((priv->tx_next & C_CAN_NEXT_MSG_OBJ_MASK) != 0) ||
  629. ((priv->tx_echo & C_CAN_NEXT_MSG_OBJ_MASK) == 0))
  630. netif_wake_queue(dev);
  631. }
  632. /*
  633. * theory of operation:
  634. *
  635. * c_can core saves a received CAN message into the first free message
  636. * object it finds free (starting with the lowest). Bits NEWDAT and
  637. * INTPND are set for this message object indicating that a new message
  638. * has arrived. To work-around this issue, we keep two groups of message
  639. * objects whose partitioning is defined by C_CAN_MSG_OBJ_RX_SPLIT.
  640. *
  641. * To ensure in-order frame reception we use the following
  642. * approach while re-activating a message object to receive further
  643. * frames:
  644. * - if the current message object number is lower than
  645. * C_CAN_MSG_RX_LOW_LAST, do not clear the NEWDAT bit while clearing
  646. * the INTPND bit.
  647. * - if the current message object number is equal to
  648. * C_CAN_MSG_RX_LOW_LAST then clear the NEWDAT bit of all lower
  649. * receive message objects.
  650. * - if the current message object number is greater than
  651. * C_CAN_MSG_RX_LOW_LAST then clear the NEWDAT bit of
  652. * only this message object.
  653. */
  654. static int c_can_do_rx_poll(struct net_device *dev, int quota)
  655. {
  656. u32 num_rx_pkts = 0;
  657. unsigned int msg_obj, msg_ctrl_save;
  658. struct c_can_priv *priv = netdev_priv(dev);
  659. u32 val = c_can_read_reg32(priv, C_CAN_INTPND1_REG);
  660. for (msg_obj = C_CAN_MSG_OBJ_RX_FIRST;
  661. msg_obj <= C_CAN_MSG_OBJ_RX_LAST && quota > 0;
  662. val = c_can_read_reg32(priv, C_CAN_INTPND1_REG),
  663. msg_obj++) {
  664. /*
  665. * as interrupt pending register's bit n-1 corresponds to
  666. * message object n, we need to handle the same properly.
  667. */
  668. if (val & (1 << (msg_obj - 1))) {
  669. c_can_object_get(dev, 0, msg_obj, IF_COMM_ALL &
  670. ~IF_COMM_TXRQST);
  671. msg_ctrl_save = priv->read_reg(priv,
  672. C_CAN_IFACE(MSGCTRL_REG, 0));
  673. if (msg_ctrl_save & IF_MCONT_EOB)
  674. return num_rx_pkts;
  675. if (msg_ctrl_save & IF_MCONT_MSGLST) {
  676. c_can_handle_lost_msg_obj(dev, 0, msg_obj);
  677. num_rx_pkts++;
  678. quota--;
  679. continue;
  680. }
  681. if (!(msg_ctrl_save & IF_MCONT_NEWDAT))
  682. continue;
  683. /* read the data from the message object */
  684. c_can_read_msg_object(dev, 0, msg_ctrl_save);
  685. if (msg_obj < C_CAN_MSG_RX_LOW_LAST)
  686. c_can_mark_rx_msg_obj(dev, 0,
  687. msg_ctrl_save, msg_obj);
  688. else if (msg_obj > C_CAN_MSG_RX_LOW_LAST)
  689. /* activate this msg obj */
  690. c_can_activate_rx_msg_obj(dev, 0,
  691. msg_ctrl_save, msg_obj);
  692. else if (msg_obj == C_CAN_MSG_RX_LOW_LAST)
  693. /* activate all lower message objects */
  694. c_can_activate_all_lower_rx_msg_obj(dev,
  695. 0, msg_ctrl_save);
  696. num_rx_pkts++;
  697. quota--;
  698. }
  699. }
  700. return num_rx_pkts;
  701. }
  702. static inline int c_can_has_and_handle_berr(struct c_can_priv *priv)
  703. {
  704. return (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) &&
  705. (priv->current_status & LEC_UNUSED);
  706. }
  707. static int c_can_handle_state_change(struct net_device *dev,
  708. enum c_can_bus_error_types error_type)
  709. {
  710. unsigned int reg_err_counter;
  711. unsigned int rx_err_passive;
  712. struct c_can_priv *priv = netdev_priv(dev);
  713. struct net_device_stats *stats = &dev->stats;
  714. struct can_frame *cf;
  715. struct sk_buff *skb;
  716. struct can_berr_counter bec;
  717. /* propagate the error condition to the CAN stack */
  718. skb = alloc_can_err_skb(dev, &cf);
  719. if (unlikely(!skb))
  720. return 0;
  721. c_can_get_berr_counter(dev, &bec);
  722. reg_err_counter = priv->read_reg(priv, C_CAN_ERR_CNT_REG);
  723. rx_err_passive = (reg_err_counter & ERR_CNT_RP_MASK) >>
  724. ERR_CNT_RP_SHIFT;
  725. switch (error_type) {
  726. case C_CAN_ERROR_WARNING:
  727. /* error warning state */
  728. priv->can.can_stats.error_warning++;
  729. priv->can.state = CAN_STATE_ERROR_WARNING;
  730. cf->can_id |= CAN_ERR_CRTL;
  731. cf->data[1] = (bec.txerr > bec.rxerr) ?
  732. CAN_ERR_CRTL_TX_WARNING :
  733. CAN_ERR_CRTL_RX_WARNING;
  734. cf->data[6] = bec.txerr;
  735. cf->data[7] = bec.rxerr;
  736. break;
  737. case C_CAN_ERROR_PASSIVE:
  738. /* error passive state */
  739. priv->can.can_stats.error_passive++;
  740. priv->can.state = CAN_STATE_ERROR_PASSIVE;
  741. cf->can_id |= CAN_ERR_CRTL;
  742. if (rx_err_passive)
  743. cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
  744. if (bec.txerr > 127)
  745. cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
  746. cf->data[6] = bec.txerr;
  747. cf->data[7] = bec.rxerr;
  748. break;
  749. case C_CAN_BUS_OFF:
  750. /* bus-off state */
  751. priv->can.state = CAN_STATE_BUS_OFF;
  752. cf->can_id |= CAN_ERR_BUSOFF;
  753. /*
  754. * disable all interrupts in bus-off mode to ensure that
  755. * the CPU is not hogged down
  756. */
  757. c_can_enable_all_interrupts(priv, DISABLE_ALL_INTERRUPTS);
  758. can_bus_off(dev);
  759. break;
  760. default:
  761. break;
  762. }
  763. netif_receive_skb(skb);
  764. stats->rx_packets++;
  765. stats->rx_bytes += cf->can_dlc;
  766. return 1;
  767. }
  768. static int c_can_handle_bus_err(struct net_device *dev,
  769. enum c_can_lec_type lec_type)
  770. {
  771. struct c_can_priv *priv = netdev_priv(dev);
  772. struct net_device_stats *stats = &dev->stats;
  773. struct can_frame *cf;
  774. struct sk_buff *skb;
  775. /*
  776. * early exit if no lec update or no error.
  777. * no lec update means that no CAN bus event has been detected
  778. * since CPU wrote 0x7 value to status reg.
  779. */
  780. if (lec_type == LEC_UNUSED || lec_type == LEC_NO_ERROR)
  781. return 0;
  782. /* propagate the error condition to the CAN stack */
  783. skb = alloc_can_err_skb(dev, &cf);
  784. if (unlikely(!skb))
  785. return 0;
  786. /*
  787. * check for 'last error code' which tells us the
  788. * type of the last error to occur on the CAN bus
  789. */
  790. /* common for all type of bus errors */
  791. priv->can.can_stats.bus_error++;
  792. stats->rx_errors++;
  793. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  794. cf->data[2] |= CAN_ERR_PROT_UNSPEC;
  795. switch (lec_type) {
  796. case LEC_STUFF_ERROR:
  797. netdev_dbg(dev, "stuff error\n");
  798. cf->data[2] |= CAN_ERR_PROT_STUFF;
  799. break;
  800. case LEC_FORM_ERROR:
  801. netdev_dbg(dev, "form error\n");
  802. cf->data[2] |= CAN_ERR_PROT_FORM;
  803. break;
  804. case LEC_ACK_ERROR:
  805. netdev_dbg(dev, "ack error\n");
  806. cf->data[2] |= (CAN_ERR_PROT_LOC_ACK |
  807. CAN_ERR_PROT_LOC_ACK_DEL);
  808. break;
  809. case LEC_BIT1_ERROR:
  810. netdev_dbg(dev, "bit1 error\n");
  811. cf->data[2] |= CAN_ERR_PROT_BIT1;
  812. break;
  813. case LEC_BIT0_ERROR:
  814. netdev_dbg(dev, "bit0 error\n");
  815. cf->data[2] |= CAN_ERR_PROT_BIT0;
  816. break;
  817. case LEC_CRC_ERROR:
  818. netdev_dbg(dev, "CRC error\n");
  819. cf->data[2] |= (CAN_ERR_PROT_LOC_CRC_SEQ |
  820. CAN_ERR_PROT_LOC_CRC_DEL);
  821. break;
  822. default:
  823. break;
  824. }
  825. /* set a `lec` value so that we can check for updates later */
  826. priv->write_reg(priv, C_CAN_STS_REG, LEC_UNUSED);
  827. netif_receive_skb(skb);
  828. stats->rx_packets++;
  829. stats->rx_bytes += cf->can_dlc;
  830. return 1;
  831. }
  832. static int c_can_poll(struct napi_struct *napi, int quota)
  833. {
  834. u16 irqstatus;
  835. int lec_type = 0;
  836. int work_done = 0;
  837. struct net_device *dev = napi->dev;
  838. struct c_can_priv *priv = netdev_priv(dev);
  839. irqstatus = priv->irqstatus;
  840. if (!irqstatus)
  841. goto end;
  842. /* status events have the highest priority */
  843. if (irqstatus == STATUS_INTERRUPT) {
  844. priv->current_status = priv->read_reg(priv,
  845. C_CAN_STS_REG);
  846. /* handle Tx/Rx events */
  847. if (priv->current_status & STATUS_TXOK)
  848. priv->write_reg(priv, C_CAN_STS_REG,
  849. priv->current_status & ~STATUS_TXOK);
  850. if (priv->current_status & STATUS_RXOK)
  851. priv->write_reg(priv, C_CAN_STS_REG,
  852. priv->current_status & ~STATUS_RXOK);
  853. /* handle state changes */
  854. if ((priv->current_status & STATUS_EWARN) &&
  855. (!(priv->last_status & STATUS_EWARN))) {
  856. netdev_dbg(dev, "entered error warning state\n");
  857. work_done += c_can_handle_state_change(dev,
  858. C_CAN_ERROR_WARNING);
  859. }
  860. if ((priv->current_status & STATUS_EPASS) &&
  861. (!(priv->last_status & STATUS_EPASS))) {
  862. netdev_dbg(dev, "entered error passive state\n");
  863. work_done += c_can_handle_state_change(dev,
  864. C_CAN_ERROR_PASSIVE);
  865. }
  866. if ((priv->current_status & STATUS_BOFF) &&
  867. (!(priv->last_status & STATUS_BOFF))) {
  868. netdev_dbg(dev, "entered bus off state\n");
  869. work_done += c_can_handle_state_change(dev,
  870. C_CAN_BUS_OFF);
  871. }
  872. /* handle bus recovery events */
  873. if ((!(priv->current_status & STATUS_BOFF)) &&
  874. (priv->last_status & STATUS_BOFF)) {
  875. netdev_dbg(dev, "left bus off state\n");
  876. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  877. }
  878. if ((!(priv->current_status & STATUS_EPASS)) &&
  879. (priv->last_status & STATUS_EPASS)) {
  880. netdev_dbg(dev, "left error passive state\n");
  881. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  882. }
  883. priv->last_status = priv->current_status;
  884. /* handle lec errors on the bus */
  885. lec_type = c_can_has_and_handle_berr(priv);
  886. if (lec_type)
  887. work_done += c_can_handle_bus_err(dev, lec_type);
  888. } else if ((irqstatus >= C_CAN_MSG_OBJ_RX_FIRST) &&
  889. (irqstatus <= C_CAN_MSG_OBJ_RX_LAST)) {
  890. /* handle events corresponding to receive message objects */
  891. work_done += c_can_do_rx_poll(dev, (quota - work_done));
  892. } else if ((irqstatus >= C_CAN_MSG_OBJ_TX_FIRST) &&
  893. (irqstatus <= C_CAN_MSG_OBJ_TX_LAST)) {
  894. /* handle events corresponding to transmit message objects */
  895. c_can_do_tx(dev);
  896. }
  897. end:
  898. if (work_done < quota) {
  899. napi_complete(napi);
  900. /* enable all IRQs */
  901. c_can_enable_all_interrupts(priv, ENABLE_ALL_INTERRUPTS);
  902. }
  903. return work_done;
  904. }
  905. static irqreturn_t c_can_isr(int irq, void *dev_id)
  906. {
  907. struct net_device *dev = (struct net_device *)dev_id;
  908. struct c_can_priv *priv = netdev_priv(dev);
  909. priv->irqstatus = priv->read_reg(priv, C_CAN_INT_REG);
  910. if (!priv->irqstatus)
  911. return IRQ_NONE;
  912. /* disable all interrupts and schedule the NAPI */
  913. c_can_enable_all_interrupts(priv, DISABLE_ALL_INTERRUPTS);
  914. napi_schedule(&priv->napi);
  915. return IRQ_HANDLED;
  916. }
  917. static int c_can_open(struct net_device *dev)
  918. {
  919. int err;
  920. struct c_can_priv *priv = netdev_priv(dev);
  921. c_can_pm_runtime_get_sync(priv);
  922. c_can_reset_ram(priv, true);
  923. /* open the can device */
  924. err = open_candev(dev);
  925. if (err) {
  926. netdev_err(dev, "failed to open can device\n");
  927. goto exit_open_fail;
  928. }
  929. /* register interrupt handler */
  930. err = request_irq(dev->irq, &c_can_isr, IRQF_SHARED, dev->name,
  931. dev);
  932. if (err < 0) {
  933. netdev_err(dev, "failed to request interrupt\n");
  934. goto exit_irq_fail;
  935. }
  936. napi_enable(&priv->napi);
  937. /* start the c_can controller */
  938. c_can_start(dev);
  939. netif_start_queue(dev);
  940. return 0;
  941. exit_irq_fail:
  942. close_candev(dev);
  943. exit_open_fail:
  944. c_can_reset_ram(priv, false);
  945. c_can_pm_runtime_put_sync(priv);
  946. return err;
  947. }
  948. static int c_can_close(struct net_device *dev)
  949. {
  950. struct c_can_priv *priv = netdev_priv(dev);
  951. netif_stop_queue(dev);
  952. napi_disable(&priv->napi);
  953. c_can_stop(dev);
  954. free_irq(dev->irq, dev);
  955. close_candev(dev);
  956. c_can_reset_ram(priv, false);
  957. c_can_pm_runtime_put_sync(priv);
  958. return 0;
  959. }
  960. struct net_device *alloc_c_can_dev(void)
  961. {
  962. struct net_device *dev;
  963. struct c_can_priv *priv;
  964. dev = alloc_candev(sizeof(struct c_can_priv), C_CAN_MSG_OBJ_TX_NUM);
  965. if (!dev)
  966. return NULL;
  967. priv = netdev_priv(dev);
  968. netif_napi_add(dev, &priv->napi, c_can_poll, C_CAN_NAPI_WEIGHT);
  969. priv->dev = dev;
  970. priv->can.bittiming_const = &c_can_bittiming_const;
  971. priv->can.do_set_mode = c_can_set_mode;
  972. priv->can.do_get_berr_counter = c_can_get_berr_counter;
  973. priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
  974. CAN_CTRLMODE_LISTENONLY |
  975. CAN_CTRLMODE_BERR_REPORTING;
  976. return dev;
  977. }
  978. EXPORT_SYMBOL_GPL(alloc_c_can_dev);
  979. #ifdef CONFIG_PM
  980. int c_can_power_down(struct net_device *dev)
  981. {
  982. u32 val;
  983. unsigned long time_out;
  984. struct c_can_priv *priv = netdev_priv(dev);
  985. if (!(dev->flags & IFF_UP))
  986. return 0;
  987. WARN_ON(priv->type != BOSCH_D_CAN);
  988. /* set PDR value so the device goes to power down mode */
  989. val = priv->read_reg(priv, C_CAN_CTRL_EX_REG);
  990. val |= CONTROL_EX_PDR;
  991. priv->write_reg(priv, C_CAN_CTRL_EX_REG, val);
  992. /* Wait for the PDA bit to get set */
  993. time_out = jiffies + msecs_to_jiffies(INIT_WAIT_MS);
  994. while (!(priv->read_reg(priv, C_CAN_STS_REG) & STATUS_PDA) &&
  995. time_after(time_out, jiffies))
  996. cpu_relax();
  997. if (time_after(jiffies, time_out))
  998. return -ETIMEDOUT;
  999. c_can_stop(dev);
  1000. c_can_reset_ram(priv, false);
  1001. c_can_pm_runtime_put_sync(priv);
  1002. return 0;
  1003. }
  1004. EXPORT_SYMBOL_GPL(c_can_power_down);
  1005. int c_can_power_up(struct net_device *dev)
  1006. {
  1007. u32 val;
  1008. unsigned long time_out;
  1009. struct c_can_priv *priv = netdev_priv(dev);
  1010. if (!(dev->flags & IFF_UP))
  1011. return 0;
  1012. WARN_ON(priv->type != BOSCH_D_CAN);
  1013. c_can_pm_runtime_get_sync(priv);
  1014. c_can_reset_ram(priv, true);
  1015. /* Clear PDR and INIT bits */
  1016. val = priv->read_reg(priv, C_CAN_CTRL_EX_REG);
  1017. val &= ~CONTROL_EX_PDR;
  1018. priv->write_reg(priv, C_CAN_CTRL_EX_REG, val);
  1019. val = priv->read_reg(priv, C_CAN_CTRL_REG);
  1020. val &= ~CONTROL_INIT;
  1021. priv->write_reg(priv, C_CAN_CTRL_REG, val);
  1022. /* Wait for the PDA bit to get clear */
  1023. time_out = jiffies + msecs_to_jiffies(INIT_WAIT_MS);
  1024. while ((priv->read_reg(priv, C_CAN_STS_REG) & STATUS_PDA) &&
  1025. time_after(time_out, jiffies))
  1026. cpu_relax();
  1027. if (time_after(jiffies, time_out))
  1028. return -ETIMEDOUT;
  1029. c_can_start(dev);
  1030. return 0;
  1031. }
  1032. EXPORT_SYMBOL_GPL(c_can_power_up);
  1033. #endif
  1034. void free_c_can_dev(struct net_device *dev)
  1035. {
  1036. free_candev(dev);
  1037. }
  1038. EXPORT_SYMBOL_GPL(free_c_can_dev);
  1039. static const struct net_device_ops c_can_netdev_ops = {
  1040. .ndo_open = c_can_open,
  1041. .ndo_stop = c_can_close,
  1042. .ndo_start_xmit = c_can_start_xmit,
  1043. };
  1044. int register_c_can_dev(struct net_device *dev)
  1045. {
  1046. struct c_can_priv *priv = netdev_priv(dev);
  1047. int err;
  1048. c_can_pm_runtime_enable(priv);
  1049. dev->flags |= IFF_ECHO; /* we support local echo */
  1050. dev->netdev_ops = &c_can_netdev_ops;
  1051. err = register_candev(dev);
  1052. if (err)
  1053. c_can_pm_runtime_disable(priv);
  1054. return err;
  1055. }
  1056. EXPORT_SYMBOL_GPL(register_c_can_dev);
  1057. void unregister_c_can_dev(struct net_device *dev)
  1058. {
  1059. struct c_can_priv *priv = netdev_priv(dev);
  1060. unregister_candev(dev);
  1061. c_can_pm_runtime_disable(priv);
  1062. }
  1063. EXPORT_SYMBOL_GPL(unregister_c_can_dev);
  1064. MODULE_AUTHOR("Bhupesh Sharma <bhupesh.sharma@st.com>");
  1065. MODULE_LICENSE("GPL v2");
  1066. MODULE_DESCRIPTION("CAN bus driver for Bosch C_CAN controller");