c_can.c 32 KB

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