c_can.c 34 KB

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