c_can.c 34 KB

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