c_can.c 31 KB

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