c_can.c 31 KB

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