c_can.c 34 KB

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