c_can.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  1. /*
  2. * CAN bus driver for Bosch C_CAN controller
  3. *
  4. * Copyright (C) 2010 ST Microelectronics
  5. * Bhupesh Sharma <bhupesh.sharma@st.com>
  6. *
  7. * Borrowed heavily from the C_CAN driver originally written by:
  8. * Copyright (C) 2007
  9. * - Sascha Hauer, Marc Kleine-Budde, Pengutronix <s.hauer@pengutronix.de>
  10. * - Simon Kallweit, intefo AG <simon.kallweit@intefo.ch>
  11. *
  12. * TX and RX NAPI implementation has been borrowed from at91 CAN driver
  13. * written by:
  14. * Copyright
  15. * (C) 2007 by Hans J. Koch <hjk@hansjkoch.de>
  16. * (C) 2008, 2009 by Marc Kleine-Budde <kernel@pengutronix.de>
  17. *
  18. * Bosch C_CAN controller is compliant to CAN protocol version 2.0 part A and B.
  19. * Bosch C_CAN user manual can be obtained from:
  20. * http://www.semiconductors.bosch.de/media/en/pdf/ipmodules_1/c_can/
  21. * users_manual_c_can.pdf
  22. *
  23. * This file is licensed under the terms of the GNU General Public
  24. * License version 2. This program is licensed "as is" without any
  25. * warranty of any kind, whether express or implied.
  26. */
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/delay.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/if_arp.h>
  33. #include <linux/if_ether.h>
  34. #include <linux/list.h>
  35. #include <linux/io.h>
  36. #include <linux/pm_runtime.h>
  37. #include <linux/can.h>
  38. #include <linux/can/dev.h>
  39. #include <linux/can/error.h>
  40. #include <linux/can/led.h>
  41. #include "c_can.h"
  42. /* Number of interface registers */
  43. #define IF_ENUM_REG_LEN 11
  44. #define C_CAN_IFACE(reg, iface) (C_CAN_IF1_##reg + (iface) * IF_ENUM_REG_LEN)
  45. /* control extension register D_CAN specific */
  46. #define CONTROL_EX_PDR BIT(8)
  47. /* control register */
  48. #define CONTROL_TEST BIT(7)
  49. #define CONTROL_CCE BIT(6)
  50. #define CONTROL_DISABLE_AR BIT(5)
  51. #define CONTROL_ENABLE_AR (0 << 5)
  52. #define CONTROL_EIE BIT(3)
  53. #define CONTROL_SIE BIT(2)
  54. #define CONTROL_IE BIT(1)
  55. #define CONTROL_INIT BIT(0)
  56. /* test register */
  57. #define TEST_RX BIT(7)
  58. #define TEST_TX1 BIT(6)
  59. #define TEST_TX2 BIT(5)
  60. #define TEST_LBACK BIT(4)
  61. #define TEST_SILENT BIT(3)
  62. #define TEST_BASIC BIT(2)
  63. /* status register */
  64. #define STATUS_PDA BIT(10)
  65. #define STATUS_BOFF BIT(7)
  66. #define STATUS_EWARN BIT(6)
  67. #define STATUS_EPASS BIT(5)
  68. #define STATUS_RXOK BIT(4)
  69. #define STATUS_TXOK BIT(3)
  70. /* error counter register */
  71. #define ERR_CNT_TEC_MASK 0xff
  72. #define ERR_CNT_TEC_SHIFT 0
  73. #define ERR_CNT_REC_SHIFT 8
  74. #define ERR_CNT_REC_MASK (0x7f << ERR_CNT_REC_SHIFT)
  75. #define ERR_CNT_RP_SHIFT 15
  76. #define ERR_CNT_RP_MASK (0x1 << ERR_CNT_RP_SHIFT)
  77. /* bit-timing register */
  78. #define BTR_BRP_MASK 0x3f
  79. #define BTR_BRP_SHIFT 0
  80. #define BTR_SJW_SHIFT 6
  81. #define BTR_SJW_MASK (0x3 << BTR_SJW_SHIFT)
  82. #define BTR_TSEG1_SHIFT 8
  83. #define BTR_TSEG1_MASK (0xf << BTR_TSEG1_SHIFT)
  84. #define BTR_TSEG2_SHIFT 12
  85. #define BTR_TSEG2_MASK (0x7 << BTR_TSEG2_SHIFT)
  86. /* brp extension register */
  87. #define BRP_EXT_BRPE_MASK 0x0f
  88. #define BRP_EXT_BRPE_SHIFT 0
  89. /* IFx command request */
  90. #define IF_COMR_BUSY BIT(15)
  91. /* IFx command mask */
  92. #define IF_COMM_WR BIT(7)
  93. #define IF_COMM_MASK BIT(6)
  94. #define IF_COMM_ARB BIT(5)
  95. #define IF_COMM_CONTROL BIT(4)
  96. #define IF_COMM_CLR_INT_PND BIT(3)
  97. #define IF_COMM_TXRQST BIT(2)
  98. #define IF_COMM_DATAA BIT(1)
  99. #define IF_COMM_DATAB BIT(0)
  100. #define IF_COMM_ALL (IF_COMM_MASK | IF_COMM_ARB | \
  101. IF_COMM_CONTROL | IF_COMM_TXRQST | \
  102. IF_COMM_DATAA | IF_COMM_DATAB)
  103. /* IFx arbitration */
  104. #define IF_ARB_MSGVAL BIT(15)
  105. #define IF_ARB_MSGXTD BIT(14)
  106. #define IF_ARB_TRANSMIT BIT(13)
  107. /* IFx message control */
  108. #define IF_MCONT_NEWDAT BIT(15)
  109. #define IF_MCONT_MSGLST BIT(14)
  110. #define IF_MCONT_CLR_MSGLST (0 << 14)
  111. #define IF_MCONT_INTPND BIT(13)
  112. #define IF_MCONT_UMASK BIT(12)
  113. #define IF_MCONT_TXIE BIT(11)
  114. #define IF_MCONT_RXIE BIT(10)
  115. #define IF_MCONT_RMTEN BIT(9)
  116. #define IF_MCONT_TXRQST BIT(8)
  117. #define IF_MCONT_EOB BIT(7)
  118. #define IF_MCONT_DLC_MASK 0xf
  119. /*
  120. * IFx register masks:
  121. * allow easy operation on 16-bit registers when the
  122. * argument is 32-bit instead
  123. */
  124. #define IFX_WRITE_LOW_16BIT(x) ((x) & 0xFFFF)
  125. #define IFX_WRITE_HIGH_16BIT(x) (((x) & 0xFFFF0000) >> 16)
  126. /* message object split */
  127. #define C_CAN_NO_OF_OBJECTS 32
  128. #define C_CAN_MSG_OBJ_RX_NUM 16
  129. #define C_CAN_MSG_OBJ_TX_NUM 16
  130. #define C_CAN_MSG_OBJ_RX_FIRST 1
  131. #define C_CAN_MSG_OBJ_RX_LAST (C_CAN_MSG_OBJ_RX_FIRST + \
  132. C_CAN_MSG_OBJ_RX_NUM - 1)
  133. #define C_CAN_MSG_OBJ_TX_FIRST (C_CAN_MSG_OBJ_RX_LAST + 1)
  134. #define C_CAN_MSG_OBJ_TX_LAST (C_CAN_MSG_OBJ_TX_FIRST + \
  135. C_CAN_MSG_OBJ_TX_NUM - 1)
  136. #define C_CAN_MSG_OBJ_RX_SPLIT 9
  137. #define C_CAN_MSG_RX_LOW_LAST (C_CAN_MSG_OBJ_RX_SPLIT - 1)
  138. #define C_CAN_NEXT_MSG_OBJ_MASK (C_CAN_MSG_OBJ_TX_NUM - 1)
  139. #define RECEIVE_OBJECT_BITS 0x0000ffff
  140. /* status interrupt */
  141. #define STATUS_INTERRUPT 0x8000
  142. /* global interrupt masks */
  143. #define ENABLE_ALL_INTERRUPTS 1
  144. #define DISABLE_ALL_INTERRUPTS 0
  145. /* minimum timeout for checking BUSY status */
  146. #define MIN_TIMEOUT_VALUE 6
  147. /* Wait for ~1 sec for INIT bit */
  148. #define INIT_WAIT_MS 1000
  149. /* napi related */
  150. #define C_CAN_NAPI_WEIGHT C_CAN_MSG_OBJ_RX_NUM
  151. /* c_can lec values */
  152. enum c_can_lec_type {
  153. LEC_NO_ERROR = 0,
  154. LEC_STUFF_ERROR,
  155. LEC_FORM_ERROR,
  156. LEC_ACK_ERROR,
  157. LEC_BIT1_ERROR,
  158. LEC_BIT0_ERROR,
  159. LEC_CRC_ERROR,
  160. LEC_UNUSED,
  161. };
  162. /*
  163. * c_can error types:
  164. * Bus errors (BUS_OFF, ERROR_WARNING, ERROR_PASSIVE) are supported
  165. */
  166. enum c_can_bus_error_types {
  167. C_CAN_NO_ERROR = 0,
  168. C_CAN_BUS_OFF,
  169. C_CAN_ERROR_WARNING,
  170. C_CAN_ERROR_PASSIVE,
  171. };
  172. static const struct can_bittiming_const c_can_bittiming_const = {
  173. .name = KBUILD_MODNAME,
  174. .tseg1_min = 2, /* Time segment 1 = prop_seg + phase_seg1 */
  175. .tseg1_max = 16,
  176. .tseg2_min = 1, /* Time segment 2 = phase_seg2 */
  177. .tseg2_max = 8,
  178. .sjw_max = 4,
  179. .brp_min = 1,
  180. .brp_max = 1024, /* 6-bit BRP field + 4-bit BRPE field*/
  181. .brp_inc = 1,
  182. };
  183. static inline void c_can_pm_runtime_enable(const struct c_can_priv *priv)
  184. {
  185. if (priv->device)
  186. pm_runtime_enable(priv->device);
  187. }
  188. static inline void c_can_pm_runtime_disable(const struct c_can_priv *priv)
  189. {
  190. if (priv->device)
  191. pm_runtime_disable(priv->device);
  192. }
  193. static inline void c_can_pm_runtime_get_sync(const struct c_can_priv *priv)
  194. {
  195. if (priv->device)
  196. pm_runtime_get_sync(priv->device);
  197. }
  198. static inline void c_can_pm_runtime_put_sync(const struct c_can_priv *priv)
  199. {
  200. if (priv->device)
  201. pm_runtime_put_sync(priv->device);
  202. }
  203. static inline void c_can_reset_ram(const struct c_can_priv *priv, bool enable)
  204. {
  205. if (priv->raminit)
  206. priv->raminit(priv, enable);
  207. }
  208. static inline int get_tx_next_msg_obj(const struct c_can_priv *priv)
  209. {
  210. return (priv->tx_next & C_CAN_NEXT_MSG_OBJ_MASK) +
  211. C_CAN_MSG_OBJ_TX_FIRST;
  212. }
  213. static inline int get_tx_echo_msg_obj(const struct c_can_priv *priv)
  214. {
  215. return (priv->tx_echo & C_CAN_NEXT_MSG_OBJ_MASK) +
  216. C_CAN_MSG_OBJ_TX_FIRST;
  217. }
  218. static u32 c_can_read_reg32(struct c_can_priv *priv, enum reg index)
  219. {
  220. u32 val = priv->read_reg(priv, index);
  221. val |= ((u32) priv->read_reg(priv, index + 1)) << 16;
  222. return val;
  223. }
  224. static void c_can_enable_all_interrupts(struct c_can_priv *priv,
  225. int enable)
  226. {
  227. unsigned int cntrl_save = priv->read_reg(priv,
  228. C_CAN_CTRL_REG);
  229. if (enable)
  230. cntrl_save |= (CONTROL_SIE | CONTROL_EIE | CONTROL_IE);
  231. else
  232. cntrl_save &= ~(CONTROL_EIE | CONTROL_IE | CONTROL_SIE);
  233. priv->write_reg(priv, C_CAN_CTRL_REG, cntrl_save);
  234. }
  235. static inline int c_can_msg_obj_is_busy(struct c_can_priv *priv, int iface)
  236. {
  237. int count = MIN_TIMEOUT_VALUE;
  238. while (count && priv->read_reg(priv,
  239. C_CAN_IFACE(COMREQ_REG, iface)) &
  240. IF_COMR_BUSY) {
  241. count--;
  242. udelay(1);
  243. }
  244. if (!count)
  245. return 1;
  246. return 0;
  247. }
  248. static inline void c_can_object_get(struct net_device *dev,
  249. int iface, int objno, int mask)
  250. {
  251. struct c_can_priv *priv = netdev_priv(dev);
  252. /*
  253. * As per specs, after writting the message object number in the
  254. * IF command request register the transfer b/w interface
  255. * register and message RAM must be complete in 6 CAN-CLK
  256. * period.
  257. */
  258. priv->write_reg(priv, C_CAN_IFACE(COMMSK_REG, iface),
  259. IFX_WRITE_LOW_16BIT(mask));
  260. priv->write_reg(priv, C_CAN_IFACE(COMREQ_REG, iface),
  261. IFX_WRITE_LOW_16BIT(objno));
  262. if (c_can_msg_obj_is_busy(priv, iface))
  263. netdev_err(dev, "timed out in object get\n");
  264. }
  265. static inline void c_can_object_put(struct net_device *dev,
  266. int iface, int objno, int mask)
  267. {
  268. struct c_can_priv *priv = netdev_priv(dev);
  269. /*
  270. * As per specs, after writting the message object number in the
  271. * IF command request register the transfer b/w interface
  272. * register and message RAM must be complete in 6 CAN-CLK
  273. * period.
  274. */
  275. priv->write_reg(priv, C_CAN_IFACE(COMMSK_REG, iface),
  276. (IF_COMM_WR | IFX_WRITE_LOW_16BIT(mask)));
  277. priv->write_reg(priv, C_CAN_IFACE(COMREQ_REG, iface),
  278. IFX_WRITE_LOW_16BIT(objno));
  279. if (c_can_msg_obj_is_busy(priv, iface))
  280. netdev_err(dev, "timed out in object put\n");
  281. }
  282. static void c_can_write_msg_object(struct net_device *dev,
  283. int iface, struct can_frame *frame, int objno)
  284. {
  285. int i;
  286. u16 flags = 0;
  287. unsigned int id;
  288. struct c_can_priv *priv = netdev_priv(dev);
  289. if (!(frame->can_id & CAN_RTR_FLAG))
  290. flags |= IF_ARB_TRANSMIT;
  291. if (frame->can_id & CAN_EFF_FLAG) {
  292. id = frame->can_id & CAN_EFF_MASK;
  293. flags |= IF_ARB_MSGXTD;
  294. } else
  295. id = ((frame->can_id & CAN_SFF_MASK) << 18);
  296. flags |= IF_ARB_MSGVAL;
  297. priv->write_reg(priv, C_CAN_IFACE(ARB1_REG, iface),
  298. IFX_WRITE_LOW_16BIT(id));
  299. priv->write_reg(priv, C_CAN_IFACE(ARB2_REG, iface), flags |
  300. IFX_WRITE_HIGH_16BIT(id));
  301. for (i = 0; i < frame->can_dlc; i += 2) {
  302. priv->write_reg(priv, C_CAN_IFACE(DATA1_REG, iface) + i / 2,
  303. frame->data[i] | (frame->data[i + 1] << 8));
  304. }
  305. /* enable interrupt for this message object */
  306. priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface),
  307. IF_MCONT_TXIE | IF_MCONT_TXRQST | IF_MCONT_EOB |
  308. frame->can_dlc);
  309. c_can_object_put(dev, iface, objno, IF_COMM_ALL);
  310. }
  311. static inline void c_can_mark_rx_msg_obj(struct net_device *dev,
  312. int iface, int ctrl_mask,
  313. int obj)
  314. {
  315. struct c_can_priv *priv = netdev_priv(dev);
  316. priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface),
  317. ctrl_mask & ~(IF_MCONT_MSGLST | IF_MCONT_INTPND));
  318. c_can_object_put(dev, iface, obj, IF_COMM_CONTROL);
  319. }
  320. static inline void c_can_activate_all_lower_rx_msg_obj(struct net_device *dev,
  321. int iface,
  322. int ctrl_mask)
  323. {
  324. int i;
  325. struct c_can_priv *priv = netdev_priv(dev);
  326. for (i = C_CAN_MSG_OBJ_RX_FIRST; i <= C_CAN_MSG_RX_LOW_LAST; i++) {
  327. priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface),
  328. ctrl_mask & ~(IF_MCONT_MSGLST |
  329. IF_MCONT_INTPND | IF_MCONT_NEWDAT));
  330. c_can_object_put(dev, iface, i, IF_COMM_CONTROL);
  331. }
  332. }
  333. static inline void c_can_activate_rx_msg_obj(struct net_device *dev,
  334. int iface, int ctrl_mask,
  335. int obj)
  336. {
  337. struct c_can_priv *priv = netdev_priv(dev);
  338. priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface),
  339. ctrl_mask & ~(IF_MCONT_MSGLST |
  340. IF_MCONT_INTPND | IF_MCONT_NEWDAT));
  341. c_can_object_put(dev, iface, obj, IF_COMM_CONTROL);
  342. }
  343. static void c_can_handle_lost_msg_obj(struct net_device *dev,
  344. int iface, int objno)
  345. {
  346. struct c_can_priv *priv = netdev_priv(dev);
  347. struct net_device_stats *stats = &dev->stats;
  348. struct sk_buff *skb;
  349. struct can_frame *frame;
  350. netdev_err(dev, "msg lost in buffer %d\n", objno);
  351. c_can_object_get(dev, iface, objno, IF_COMM_ALL & ~IF_COMM_TXRQST);
  352. priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface),
  353. IF_MCONT_CLR_MSGLST);
  354. c_can_object_put(dev, 0, objno, IF_COMM_CONTROL);
  355. /* create an error msg */
  356. skb = alloc_can_err_skb(dev, &frame);
  357. if (unlikely(!skb))
  358. return;
  359. frame->can_id |= CAN_ERR_CRTL;
  360. frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  361. stats->rx_errors++;
  362. stats->rx_over_errors++;
  363. netif_receive_skb(skb);
  364. }
  365. static int c_can_read_msg_object(struct net_device *dev, int iface, int ctrl)
  366. {
  367. u16 flags, data;
  368. int i;
  369. unsigned int val;
  370. struct c_can_priv *priv = netdev_priv(dev);
  371. struct net_device_stats *stats = &dev->stats;
  372. struct sk_buff *skb;
  373. struct can_frame *frame;
  374. skb = alloc_can_skb(dev, &frame);
  375. if (!skb) {
  376. stats->rx_dropped++;
  377. return -ENOMEM;
  378. }
  379. frame->can_dlc = get_can_dlc(ctrl & 0x0F);
  380. flags = priv->read_reg(priv, C_CAN_IFACE(ARB2_REG, iface));
  381. val = priv->read_reg(priv, C_CAN_IFACE(ARB1_REG, iface)) |
  382. (flags << 16);
  383. if (flags & IF_ARB_MSGXTD)
  384. frame->can_id = (val & CAN_EFF_MASK) | CAN_EFF_FLAG;
  385. else
  386. frame->can_id = (val >> 18) & CAN_SFF_MASK;
  387. if (flags & IF_ARB_TRANSMIT)
  388. frame->can_id |= CAN_RTR_FLAG;
  389. else {
  390. for (i = 0; i < frame->can_dlc; i += 2) {
  391. data = priv->read_reg(priv,
  392. C_CAN_IFACE(DATA1_REG, iface) + i / 2);
  393. frame->data[i] = data;
  394. frame->data[i + 1] = data >> 8;
  395. }
  396. }
  397. netif_receive_skb(skb);
  398. stats->rx_packets++;
  399. stats->rx_bytes += frame->can_dlc;
  400. can_led_event(dev, CAN_LED_EVENT_RX);
  401. return 0;
  402. }
  403. static void c_can_setup_receive_object(struct net_device *dev, int iface,
  404. int objno, unsigned int mask,
  405. unsigned int id, unsigned int mcont)
  406. {
  407. struct c_can_priv *priv = netdev_priv(dev);
  408. priv->write_reg(priv, C_CAN_IFACE(MASK1_REG, iface),
  409. IFX_WRITE_LOW_16BIT(mask));
  410. priv->write_reg(priv, C_CAN_IFACE(MASK2_REG, iface),
  411. IFX_WRITE_HIGH_16BIT(mask));
  412. priv->write_reg(priv, C_CAN_IFACE(ARB1_REG, iface),
  413. IFX_WRITE_LOW_16BIT(id));
  414. priv->write_reg(priv, C_CAN_IFACE(ARB2_REG, iface),
  415. (IF_ARB_MSGVAL | IFX_WRITE_HIGH_16BIT(id)));
  416. priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), mcont);
  417. c_can_object_put(dev, iface, objno, IF_COMM_ALL & ~IF_COMM_TXRQST);
  418. netdev_dbg(dev, "obj no:%d, msgval:0x%08x\n", objno,
  419. c_can_read_reg32(priv, C_CAN_MSGVAL1_REG));
  420. }
  421. static void c_can_inval_msg_object(struct net_device *dev, int iface, int objno)
  422. {
  423. struct c_can_priv *priv = netdev_priv(dev);
  424. priv->write_reg(priv, C_CAN_IFACE(ARB1_REG, iface), 0);
  425. priv->write_reg(priv, C_CAN_IFACE(ARB2_REG, iface), 0);
  426. priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), 0);
  427. c_can_object_put(dev, iface, objno, IF_COMM_ARB | IF_COMM_CONTROL);
  428. netdev_dbg(dev, "obj no:%d, msgval:0x%08x\n", objno,
  429. c_can_read_reg32(priv, C_CAN_MSGVAL1_REG));
  430. }
  431. static inline int c_can_is_next_tx_obj_busy(struct c_can_priv *priv, int objno)
  432. {
  433. int val = c_can_read_reg32(priv, C_CAN_TXRQST1_REG);
  434. /*
  435. * as transmission request register's bit n-1 corresponds to
  436. * message object n, we need to handle the same properly.
  437. */
  438. if (val & (1 << (objno - 1)))
  439. return 1;
  440. return 0;
  441. }
  442. static netdev_tx_t c_can_start_xmit(struct sk_buff *skb,
  443. struct net_device *dev)
  444. {
  445. u32 msg_obj_no;
  446. struct c_can_priv *priv = netdev_priv(dev);
  447. struct can_frame *frame = (struct can_frame *)skb->data;
  448. if (can_dropped_invalid_skb(dev, skb))
  449. return NETDEV_TX_OK;
  450. msg_obj_no = get_tx_next_msg_obj(priv);
  451. /* prepare message object for transmission */
  452. c_can_write_msg_object(dev, 0, frame, msg_obj_no);
  453. can_put_echo_skb(skb, dev, msg_obj_no - C_CAN_MSG_OBJ_TX_FIRST);
  454. /*
  455. * we have to stop the queue in case of a wrap around or
  456. * if the next TX message object is still in use
  457. */
  458. priv->tx_next++;
  459. if (c_can_is_next_tx_obj_busy(priv, get_tx_next_msg_obj(priv)) ||
  460. (priv->tx_next & C_CAN_NEXT_MSG_OBJ_MASK) == 0)
  461. netif_stop_queue(dev);
  462. return NETDEV_TX_OK;
  463. }
  464. static int c_can_set_bittiming(struct net_device *dev)
  465. {
  466. unsigned int reg_btr, reg_brpe, ctrl_save;
  467. u8 brp, brpe, sjw, tseg1, tseg2;
  468. u32 ten_bit_brp;
  469. struct c_can_priv *priv = netdev_priv(dev);
  470. const struct can_bittiming *bt = &priv->can.bittiming;
  471. /* c_can provides a 6-bit brp and 4-bit brpe fields */
  472. ten_bit_brp = bt->brp - 1;
  473. brp = ten_bit_brp & BTR_BRP_MASK;
  474. brpe = ten_bit_brp >> 6;
  475. sjw = bt->sjw - 1;
  476. tseg1 = bt->prop_seg + bt->phase_seg1 - 1;
  477. tseg2 = bt->phase_seg2 - 1;
  478. reg_btr = brp | (sjw << BTR_SJW_SHIFT) | (tseg1 << BTR_TSEG1_SHIFT) |
  479. (tseg2 << BTR_TSEG2_SHIFT);
  480. reg_brpe = brpe & BRP_EXT_BRPE_MASK;
  481. netdev_info(dev,
  482. "setting BTR=%04x BRPE=%04x\n", reg_btr, reg_brpe);
  483. ctrl_save = priv->read_reg(priv, C_CAN_CTRL_REG);
  484. priv->write_reg(priv, C_CAN_CTRL_REG,
  485. ctrl_save | CONTROL_CCE | CONTROL_INIT);
  486. priv->write_reg(priv, C_CAN_BTR_REG, reg_btr);
  487. priv->write_reg(priv, C_CAN_BRPEXT_REG, reg_brpe);
  488. priv->write_reg(priv, C_CAN_CTRL_REG, ctrl_save);
  489. return 0;
  490. }
  491. /*
  492. * Configure C_CAN message objects for Tx and Rx purposes:
  493. * C_CAN provides a total of 32 message objects that can be configured
  494. * either for Tx or Rx purposes. Here the first 16 message objects are used as
  495. * a reception FIFO. The end of reception FIFO is signified by the EoB bit
  496. * being SET. The remaining 16 message objects are kept aside for Tx purposes.
  497. * See user guide document for further details on configuring message
  498. * objects.
  499. */
  500. static void c_can_configure_msg_objects(struct net_device *dev)
  501. {
  502. int i;
  503. /* first invalidate all message objects */
  504. for (i = C_CAN_MSG_OBJ_RX_FIRST; i <= C_CAN_NO_OF_OBJECTS; i++)
  505. c_can_inval_msg_object(dev, 0, i);
  506. /* setup receive message objects */
  507. for (i = C_CAN_MSG_OBJ_RX_FIRST; i < C_CAN_MSG_OBJ_RX_LAST; i++)
  508. c_can_setup_receive_object(dev, 0, i, 0, 0,
  509. (IF_MCONT_RXIE | IF_MCONT_UMASK) & ~IF_MCONT_EOB);
  510. c_can_setup_receive_object(dev, 0, C_CAN_MSG_OBJ_RX_LAST, 0, 0,
  511. IF_MCONT_EOB | IF_MCONT_RXIE | IF_MCONT_UMASK);
  512. }
  513. /*
  514. * Configure C_CAN chip:
  515. * - enable/disable auto-retransmission
  516. * - set operating mode
  517. * - configure message objects
  518. */
  519. static void c_can_chip_config(struct net_device *dev)
  520. {
  521. struct c_can_priv *priv = netdev_priv(dev);
  522. /* enable automatic retransmission */
  523. priv->write_reg(priv, C_CAN_CTRL_REG,
  524. CONTROL_ENABLE_AR);
  525. if ((priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) &&
  526. (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)) {
  527. /* loopback + silent mode : useful for hot self-test */
  528. priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_EIE |
  529. CONTROL_SIE | CONTROL_IE | CONTROL_TEST);
  530. priv->write_reg(priv, C_CAN_TEST_REG,
  531. TEST_LBACK | TEST_SILENT);
  532. } else if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) {
  533. /* loopback mode : useful for self-test function */
  534. priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_EIE |
  535. CONTROL_SIE | CONTROL_IE | CONTROL_TEST);
  536. priv->write_reg(priv, C_CAN_TEST_REG, TEST_LBACK);
  537. } else if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) {
  538. /* silent mode : bus-monitoring mode */
  539. priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_EIE |
  540. CONTROL_SIE | CONTROL_IE | CONTROL_TEST);
  541. priv->write_reg(priv, C_CAN_TEST_REG, TEST_SILENT);
  542. } else
  543. /* normal mode*/
  544. priv->write_reg(priv, C_CAN_CTRL_REG,
  545. CONTROL_EIE | CONTROL_SIE | CONTROL_IE);
  546. /* configure message objects */
  547. c_can_configure_msg_objects(dev);
  548. /* set a `lec` value so that we can check for updates later */
  549. priv->write_reg(priv, C_CAN_STS_REG, LEC_UNUSED);
  550. /* set bittiming params */
  551. c_can_set_bittiming(dev);
  552. }
  553. static void c_can_start(struct net_device *dev)
  554. {
  555. struct c_can_priv *priv = netdev_priv(dev);
  556. /* basic c_can configuration */
  557. c_can_chip_config(dev);
  558. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  559. /* reset tx helper pointers */
  560. priv->tx_next = priv->tx_echo = 0;
  561. /* enable status change, error and module interrupts */
  562. c_can_enable_all_interrupts(priv, ENABLE_ALL_INTERRUPTS);
  563. }
  564. static void c_can_stop(struct net_device *dev)
  565. {
  566. struct c_can_priv *priv = netdev_priv(dev);
  567. /* disable all interrupts */
  568. c_can_enable_all_interrupts(priv, DISABLE_ALL_INTERRUPTS);
  569. /* set the state as STOPPED */
  570. priv->can.state = CAN_STATE_STOPPED;
  571. }
  572. static int c_can_set_mode(struct net_device *dev, enum can_mode mode)
  573. {
  574. switch (mode) {
  575. case CAN_MODE_START:
  576. c_can_start(dev);
  577. netif_wake_queue(dev);
  578. break;
  579. default:
  580. return -EOPNOTSUPP;
  581. }
  582. return 0;
  583. }
  584. static int c_can_get_berr_counter(const struct net_device *dev,
  585. struct can_berr_counter *bec)
  586. {
  587. unsigned int reg_err_counter;
  588. struct c_can_priv *priv = netdev_priv(dev);
  589. c_can_pm_runtime_get_sync(priv);
  590. reg_err_counter = priv->read_reg(priv, C_CAN_ERR_CNT_REG);
  591. bec->rxerr = (reg_err_counter & ERR_CNT_REC_MASK) >>
  592. ERR_CNT_REC_SHIFT;
  593. bec->txerr = reg_err_counter & ERR_CNT_TEC_MASK;
  594. c_can_pm_runtime_put_sync(priv);
  595. return 0;
  596. }
  597. /*
  598. * theory of operation:
  599. *
  600. * priv->tx_echo holds the number of the oldest can_frame put for
  601. * transmission into the hardware, but not yet ACKed by the CAN tx
  602. * complete IRQ.
  603. *
  604. * We iterate from priv->tx_echo to priv->tx_next and check if the
  605. * packet has been transmitted, echo it back to the CAN framework.
  606. * If we discover a not yet transmitted packet, stop looking for more.
  607. */
  608. static void c_can_do_tx(struct net_device *dev)
  609. {
  610. u32 val;
  611. u32 msg_obj_no;
  612. struct c_can_priv *priv = netdev_priv(dev);
  613. struct net_device_stats *stats = &dev->stats;
  614. for (/* nix */; (priv->tx_next - priv->tx_echo) > 0; priv->tx_echo++) {
  615. msg_obj_no = get_tx_echo_msg_obj(priv);
  616. val = c_can_read_reg32(priv, C_CAN_TXRQST1_REG);
  617. if (!(val & (1 << (msg_obj_no - 1)))) {
  618. can_get_echo_skb(dev,
  619. msg_obj_no - C_CAN_MSG_OBJ_TX_FIRST);
  620. stats->tx_bytes += priv->read_reg(priv,
  621. C_CAN_IFACE(MSGCTRL_REG, 0))
  622. & IF_MCONT_DLC_MASK;
  623. stats->tx_packets++;
  624. can_led_event(dev, CAN_LED_EVENT_TX);
  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. can_led_event(dev, CAN_LED_EVENT_OPEN);
  941. /* start the c_can controller */
  942. c_can_start(dev);
  943. netif_start_queue(dev);
  944. return 0;
  945. exit_irq_fail:
  946. close_candev(dev);
  947. exit_open_fail:
  948. c_can_reset_ram(priv, false);
  949. c_can_pm_runtime_put_sync(priv);
  950. return err;
  951. }
  952. static int c_can_close(struct net_device *dev)
  953. {
  954. struct c_can_priv *priv = netdev_priv(dev);
  955. netif_stop_queue(dev);
  956. napi_disable(&priv->napi);
  957. c_can_stop(dev);
  958. free_irq(dev->irq, dev);
  959. close_candev(dev);
  960. c_can_reset_ram(priv, false);
  961. c_can_pm_runtime_put_sync(priv);
  962. can_led_event(dev, CAN_LED_EVENT_STOP);
  963. return 0;
  964. }
  965. struct net_device *alloc_c_can_dev(void)
  966. {
  967. struct net_device *dev;
  968. struct c_can_priv *priv;
  969. dev = alloc_candev(sizeof(struct c_can_priv), C_CAN_MSG_OBJ_TX_NUM);
  970. if (!dev)
  971. return NULL;
  972. priv = netdev_priv(dev);
  973. netif_napi_add(dev, &priv->napi, c_can_poll, C_CAN_NAPI_WEIGHT);
  974. priv->dev = dev;
  975. priv->can.bittiming_const = &c_can_bittiming_const;
  976. priv->can.do_set_mode = c_can_set_mode;
  977. priv->can.do_get_berr_counter = c_can_get_berr_counter;
  978. priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
  979. CAN_CTRLMODE_LISTENONLY |
  980. CAN_CTRLMODE_BERR_REPORTING;
  981. return dev;
  982. }
  983. EXPORT_SYMBOL_GPL(alloc_c_can_dev);
  984. #ifdef CONFIG_PM
  985. int c_can_power_down(struct net_device *dev)
  986. {
  987. u32 val;
  988. unsigned long time_out;
  989. struct c_can_priv *priv = netdev_priv(dev);
  990. if (!(dev->flags & IFF_UP))
  991. return 0;
  992. WARN_ON(priv->type != BOSCH_D_CAN);
  993. /* set PDR value so the device goes to power down mode */
  994. val = priv->read_reg(priv, C_CAN_CTRL_EX_REG);
  995. val |= CONTROL_EX_PDR;
  996. priv->write_reg(priv, C_CAN_CTRL_EX_REG, val);
  997. /* Wait for the PDA bit to get set */
  998. time_out = jiffies + msecs_to_jiffies(INIT_WAIT_MS);
  999. while (!(priv->read_reg(priv, C_CAN_STS_REG) & STATUS_PDA) &&
  1000. time_after(time_out, jiffies))
  1001. cpu_relax();
  1002. if (time_after(jiffies, time_out))
  1003. return -ETIMEDOUT;
  1004. c_can_stop(dev);
  1005. c_can_reset_ram(priv, false);
  1006. c_can_pm_runtime_put_sync(priv);
  1007. return 0;
  1008. }
  1009. EXPORT_SYMBOL_GPL(c_can_power_down);
  1010. int c_can_power_up(struct net_device *dev)
  1011. {
  1012. u32 val;
  1013. unsigned long time_out;
  1014. struct c_can_priv *priv = netdev_priv(dev);
  1015. if (!(dev->flags & IFF_UP))
  1016. return 0;
  1017. WARN_ON(priv->type != BOSCH_D_CAN);
  1018. c_can_pm_runtime_get_sync(priv);
  1019. c_can_reset_ram(priv, true);
  1020. /* Clear PDR and INIT bits */
  1021. val = priv->read_reg(priv, C_CAN_CTRL_EX_REG);
  1022. val &= ~CONTROL_EX_PDR;
  1023. priv->write_reg(priv, C_CAN_CTRL_EX_REG, val);
  1024. val = priv->read_reg(priv, C_CAN_CTRL_REG);
  1025. val &= ~CONTROL_INIT;
  1026. priv->write_reg(priv, C_CAN_CTRL_REG, val);
  1027. /* Wait for the PDA bit to get clear */
  1028. time_out = jiffies + msecs_to_jiffies(INIT_WAIT_MS);
  1029. while ((priv->read_reg(priv, C_CAN_STS_REG) & STATUS_PDA) &&
  1030. time_after(time_out, jiffies))
  1031. cpu_relax();
  1032. if (time_after(jiffies, time_out))
  1033. return -ETIMEDOUT;
  1034. c_can_start(dev);
  1035. return 0;
  1036. }
  1037. EXPORT_SYMBOL_GPL(c_can_power_up);
  1038. #endif
  1039. void free_c_can_dev(struct net_device *dev)
  1040. {
  1041. free_candev(dev);
  1042. }
  1043. EXPORT_SYMBOL_GPL(free_c_can_dev);
  1044. static const struct net_device_ops c_can_netdev_ops = {
  1045. .ndo_open = c_can_open,
  1046. .ndo_stop = c_can_close,
  1047. .ndo_start_xmit = c_can_start_xmit,
  1048. };
  1049. int register_c_can_dev(struct net_device *dev)
  1050. {
  1051. struct c_can_priv *priv = netdev_priv(dev);
  1052. int err;
  1053. c_can_pm_runtime_enable(priv);
  1054. dev->flags |= IFF_ECHO; /* we support local echo */
  1055. dev->netdev_ops = &c_can_netdev_ops;
  1056. err = register_candev(dev);
  1057. if (err)
  1058. c_can_pm_runtime_disable(priv);
  1059. else
  1060. devm_can_led_init(dev);
  1061. return err;
  1062. }
  1063. EXPORT_SYMBOL_GPL(register_c_can_dev);
  1064. void unregister_c_can_dev(struct net_device *dev)
  1065. {
  1066. struct c_can_priv *priv = netdev_priv(dev);
  1067. unregister_candev(dev);
  1068. c_can_pm_runtime_disable(priv);
  1069. }
  1070. EXPORT_SYMBOL_GPL(unregister_c_can_dev);
  1071. MODULE_AUTHOR("Bhupesh Sharma <bhupesh.sharma@st.com>");
  1072. MODULE_LICENSE("GPL v2");
  1073. MODULE_DESCRIPTION("CAN bus driver for Bosch C_CAN controller");