c_can.c 33 KB

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