c_can.c 34 KB

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