xilinx_can.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  1. /* Xilinx CAN device driver
  2. *
  3. * Copyright (C) 2012 - 2014 Xilinx, Inc.
  4. * Copyright (C) 2009 PetaLogix. All rights reserved.
  5. * Copyright (C) 2017 Sandvik Mining and Construction Oy
  6. *
  7. * Description:
  8. * This driver is developed for Axi CAN IP and for Zynq CANPS Controller.
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/clk.h>
  20. #include <linux/errno.h>
  21. #include <linux/init.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/io.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/of.h>
  28. #include <linux/of_device.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/skbuff.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/string.h>
  33. #include <linux/types.h>
  34. #include <linux/can/dev.h>
  35. #include <linux/can/error.h>
  36. #include <linux/can/led.h>
  37. #include <linux/pm_runtime.h>
  38. #define DRIVER_NAME "xilinx_can"
  39. /* CAN registers set */
  40. enum xcan_reg {
  41. XCAN_SRR_OFFSET = 0x00, /* Software reset */
  42. XCAN_MSR_OFFSET = 0x04, /* Mode select */
  43. XCAN_BRPR_OFFSET = 0x08, /* Baud rate prescaler */
  44. XCAN_BTR_OFFSET = 0x0C, /* Bit timing */
  45. XCAN_ECR_OFFSET = 0x10, /* Error counter */
  46. XCAN_ESR_OFFSET = 0x14, /* Error status */
  47. XCAN_SR_OFFSET = 0x18, /* Status */
  48. XCAN_ISR_OFFSET = 0x1C, /* Interrupt status */
  49. XCAN_IER_OFFSET = 0x20, /* Interrupt enable */
  50. XCAN_ICR_OFFSET = 0x24, /* Interrupt clear */
  51. XCAN_TXFIFO_ID_OFFSET = 0x30,/* TX FIFO ID */
  52. XCAN_TXFIFO_DLC_OFFSET = 0x34, /* TX FIFO DLC */
  53. XCAN_TXFIFO_DW1_OFFSET = 0x38, /* TX FIFO Data Word 1 */
  54. XCAN_TXFIFO_DW2_OFFSET = 0x3C, /* TX FIFO Data Word 2 */
  55. XCAN_RXFIFO_ID_OFFSET = 0x50, /* RX FIFO ID */
  56. XCAN_RXFIFO_DLC_OFFSET = 0x54, /* RX FIFO DLC */
  57. XCAN_RXFIFO_DW1_OFFSET = 0x58, /* RX FIFO Data Word 1 */
  58. XCAN_RXFIFO_DW2_OFFSET = 0x5C, /* RX FIFO Data Word 2 */
  59. };
  60. /* CAN register bit masks - XCAN_<REG>_<BIT>_MASK */
  61. #define XCAN_SRR_CEN_MASK 0x00000002 /* CAN enable */
  62. #define XCAN_SRR_RESET_MASK 0x00000001 /* Soft Reset the CAN core */
  63. #define XCAN_MSR_LBACK_MASK 0x00000002 /* Loop back mode select */
  64. #define XCAN_MSR_SLEEP_MASK 0x00000001 /* Sleep mode select */
  65. #define XCAN_BRPR_BRP_MASK 0x000000FF /* Baud rate prescaler */
  66. #define XCAN_BTR_SJW_MASK 0x00000180 /* Synchronous jump width */
  67. #define XCAN_BTR_TS2_MASK 0x00000070 /* Time segment 2 */
  68. #define XCAN_BTR_TS1_MASK 0x0000000F /* Time segment 1 */
  69. #define XCAN_ECR_REC_MASK 0x0000FF00 /* Receive error counter */
  70. #define XCAN_ECR_TEC_MASK 0x000000FF /* Transmit error counter */
  71. #define XCAN_ESR_ACKER_MASK 0x00000010 /* ACK error */
  72. #define XCAN_ESR_BERR_MASK 0x00000008 /* Bit error */
  73. #define XCAN_ESR_STER_MASK 0x00000004 /* Stuff error */
  74. #define XCAN_ESR_FMER_MASK 0x00000002 /* Form error */
  75. #define XCAN_ESR_CRCER_MASK 0x00000001 /* CRC error */
  76. #define XCAN_SR_TXFLL_MASK 0x00000400 /* TX FIFO is full */
  77. #define XCAN_SR_ESTAT_MASK 0x00000180 /* Error status */
  78. #define XCAN_SR_ERRWRN_MASK 0x00000040 /* Error warning */
  79. #define XCAN_SR_NORMAL_MASK 0x00000008 /* Normal mode */
  80. #define XCAN_SR_LBACK_MASK 0x00000002 /* Loop back mode */
  81. #define XCAN_SR_CONFIG_MASK 0x00000001 /* Configuration mode */
  82. #define XCAN_IXR_TXFEMP_MASK 0x00004000 /* TX FIFO Empty */
  83. #define XCAN_IXR_WKUP_MASK 0x00000800 /* Wake up interrupt */
  84. #define XCAN_IXR_SLP_MASK 0x00000400 /* Sleep interrupt */
  85. #define XCAN_IXR_BSOFF_MASK 0x00000200 /* Bus off interrupt */
  86. #define XCAN_IXR_ERROR_MASK 0x00000100 /* Error interrupt */
  87. #define XCAN_IXR_RXNEMP_MASK 0x00000080 /* RX FIFO NotEmpty intr */
  88. #define XCAN_IXR_RXOFLW_MASK 0x00000040 /* RX FIFO Overflow intr */
  89. #define XCAN_IXR_RXOK_MASK 0x00000010 /* Message received intr */
  90. #define XCAN_IXR_TXFLL_MASK 0x00000004 /* Tx FIFO Full intr */
  91. #define XCAN_IXR_TXOK_MASK 0x00000002 /* TX successful intr */
  92. #define XCAN_IXR_ARBLST_MASK 0x00000001 /* Arbitration lost intr */
  93. #define XCAN_IDR_ID1_MASK 0xFFE00000 /* Standard msg identifier */
  94. #define XCAN_IDR_SRR_MASK 0x00100000 /* Substitute remote TXreq */
  95. #define XCAN_IDR_IDE_MASK 0x00080000 /* Identifier extension */
  96. #define XCAN_IDR_ID2_MASK 0x0007FFFE /* Extended message ident */
  97. #define XCAN_IDR_RTR_MASK 0x00000001 /* Remote TX request */
  98. #define XCAN_DLCR_DLC_MASK 0xF0000000 /* Data length code */
  99. #define XCAN_INTR_ALL (XCAN_IXR_TXOK_MASK | XCAN_IXR_BSOFF_MASK |\
  100. XCAN_IXR_WKUP_MASK | XCAN_IXR_SLP_MASK | \
  101. XCAN_IXR_RXNEMP_MASK | XCAN_IXR_ERROR_MASK | \
  102. XCAN_IXR_RXOFLW_MASK | XCAN_IXR_ARBLST_MASK)
  103. /* CAN register bit shift - XCAN_<REG>_<BIT>_SHIFT */
  104. #define XCAN_BTR_SJW_SHIFT 7 /* Synchronous jump width */
  105. #define XCAN_BTR_TS2_SHIFT 4 /* Time segment 2 */
  106. #define XCAN_IDR_ID1_SHIFT 21 /* Standard Messg Identifier */
  107. #define XCAN_IDR_ID2_SHIFT 1 /* Extended Message Identifier */
  108. #define XCAN_DLCR_DLC_SHIFT 28 /* Data length code */
  109. #define XCAN_ESR_REC_SHIFT 8 /* Rx Error Count */
  110. /* CAN frame length constants */
  111. #define XCAN_FRAME_MAX_DATA_LEN 8
  112. #define XCAN_TIMEOUT (1 * HZ)
  113. /**
  114. * struct xcan_priv - This definition define CAN driver instance
  115. * @can: CAN private data structure.
  116. * @tx_lock: Lock for synchronizing TX interrupt handling
  117. * @tx_head: Tx CAN packets ready to send on the queue
  118. * @tx_tail: Tx CAN packets successfully sended on the queue
  119. * @tx_max: Maximum number packets the driver can send
  120. * @napi: NAPI structure
  121. * @read_reg: For reading data from CAN registers
  122. * @write_reg: For writing data to CAN registers
  123. * @dev: Network device data structure
  124. * @reg_base: Ioremapped address to registers
  125. * @irq_flags: For request_irq()
  126. * @bus_clk: Pointer to struct clk
  127. * @can_clk: Pointer to struct clk
  128. */
  129. struct xcan_priv {
  130. struct can_priv can;
  131. spinlock_t tx_lock;
  132. unsigned int tx_head;
  133. unsigned int tx_tail;
  134. unsigned int tx_max;
  135. struct napi_struct napi;
  136. u32 (*read_reg)(const struct xcan_priv *priv, enum xcan_reg reg);
  137. void (*write_reg)(const struct xcan_priv *priv, enum xcan_reg reg,
  138. u32 val);
  139. struct device *dev;
  140. void __iomem *reg_base;
  141. unsigned long irq_flags;
  142. struct clk *bus_clk;
  143. struct clk *can_clk;
  144. };
  145. /* CAN Bittiming constants as per Xilinx CAN specs */
  146. static const struct can_bittiming_const xcan_bittiming_const = {
  147. .name = DRIVER_NAME,
  148. .tseg1_min = 1,
  149. .tseg1_max = 16,
  150. .tseg2_min = 1,
  151. .tseg2_max = 8,
  152. .sjw_max = 4,
  153. .brp_min = 1,
  154. .brp_max = 256,
  155. .brp_inc = 1,
  156. };
  157. #define XCAN_CAP_WATERMARK 0x0001
  158. struct xcan_devtype_data {
  159. unsigned int caps;
  160. };
  161. /**
  162. * xcan_write_reg_le - Write a value to the device register little endian
  163. * @priv: Driver private data structure
  164. * @reg: Register offset
  165. * @val: Value to write at the Register offset
  166. *
  167. * Write data to the paricular CAN register
  168. */
  169. static void xcan_write_reg_le(const struct xcan_priv *priv, enum xcan_reg reg,
  170. u32 val)
  171. {
  172. iowrite32(val, priv->reg_base + reg);
  173. }
  174. /**
  175. * xcan_read_reg_le - Read a value from the device register little endian
  176. * @priv: Driver private data structure
  177. * @reg: Register offset
  178. *
  179. * Read data from the particular CAN register
  180. * Return: value read from the CAN register
  181. */
  182. static u32 xcan_read_reg_le(const struct xcan_priv *priv, enum xcan_reg reg)
  183. {
  184. return ioread32(priv->reg_base + reg);
  185. }
  186. /**
  187. * xcan_write_reg_be - Write a value to the device register big endian
  188. * @priv: Driver private data structure
  189. * @reg: Register offset
  190. * @val: Value to write at the Register offset
  191. *
  192. * Write data to the paricular CAN register
  193. */
  194. static void xcan_write_reg_be(const struct xcan_priv *priv, enum xcan_reg reg,
  195. u32 val)
  196. {
  197. iowrite32be(val, priv->reg_base + reg);
  198. }
  199. /**
  200. * xcan_read_reg_be - Read a value from the device register big endian
  201. * @priv: Driver private data structure
  202. * @reg: Register offset
  203. *
  204. * Read data from the particular CAN register
  205. * Return: value read from the CAN register
  206. */
  207. static u32 xcan_read_reg_be(const struct xcan_priv *priv, enum xcan_reg reg)
  208. {
  209. return ioread32be(priv->reg_base + reg);
  210. }
  211. /**
  212. * set_reset_mode - Resets the CAN device mode
  213. * @ndev: Pointer to net_device structure
  214. *
  215. * This is the driver reset mode routine.The driver
  216. * enters into configuration mode.
  217. *
  218. * Return: 0 on success and failure value on error
  219. */
  220. static int set_reset_mode(struct net_device *ndev)
  221. {
  222. struct xcan_priv *priv = netdev_priv(ndev);
  223. unsigned long timeout;
  224. priv->write_reg(priv, XCAN_SRR_OFFSET, XCAN_SRR_RESET_MASK);
  225. timeout = jiffies + XCAN_TIMEOUT;
  226. while (!(priv->read_reg(priv, XCAN_SR_OFFSET) & XCAN_SR_CONFIG_MASK)) {
  227. if (time_after(jiffies, timeout)) {
  228. netdev_warn(ndev, "timed out for config mode\n");
  229. return -ETIMEDOUT;
  230. }
  231. usleep_range(500, 10000);
  232. }
  233. /* reset clears FIFOs */
  234. priv->tx_head = 0;
  235. priv->tx_tail = 0;
  236. return 0;
  237. }
  238. /**
  239. * xcan_set_bittiming - CAN set bit timing routine
  240. * @ndev: Pointer to net_device structure
  241. *
  242. * This is the driver set bittiming routine.
  243. * Return: 0 on success and failure value on error
  244. */
  245. static int xcan_set_bittiming(struct net_device *ndev)
  246. {
  247. struct xcan_priv *priv = netdev_priv(ndev);
  248. struct can_bittiming *bt = &priv->can.bittiming;
  249. u32 btr0, btr1;
  250. u32 is_config_mode;
  251. /* Check whether Xilinx CAN is in configuration mode.
  252. * It cannot set bit timing if Xilinx CAN is not in configuration mode.
  253. */
  254. is_config_mode = priv->read_reg(priv, XCAN_SR_OFFSET) &
  255. XCAN_SR_CONFIG_MASK;
  256. if (!is_config_mode) {
  257. netdev_alert(ndev,
  258. "BUG! Cannot set bittiming - CAN is not in config mode\n");
  259. return -EPERM;
  260. }
  261. /* Setting Baud Rate prescalar value in BRPR Register */
  262. btr0 = (bt->brp - 1);
  263. /* Setting Time Segment 1 in BTR Register */
  264. btr1 = (bt->prop_seg + bt->phase_seg1 - 1);
  265. /* Setting Time Segment 2 in BTR Register */
  266. btr1 |= (bt->phase_seg2 - 1) << XCAN_BTR_TS2_SHIFT;
  267. /* Setting Synchronous jump width in BTR Register */
  268. btr1 |= (bt->sjw - 1) << XCAN_BTR_SJW_SHIFT;
  269. priv->write_reg(priv, XCAN_BRPR_OFFSET, btr0);
  270. priv->write_reg(priv, XCAN_BTR_OFFSET, btr1);
  271. netdev_dbg(ndev, "BRPR=0x%08x, BTR=0x%08x\n",
  272. priv->read_reg(priv, XCAN_BRPR_OFFSET),
  273. priv->read_reg(priv, XCAN_BTR_OFFSET));
  274. return 0;
  275. }
  276. /**
  277. * xcan_chip_start - This the drivers start routine
  278. * @ndev: Pointer to net_device structure
  279. *
  280. * This is the drivers start routine.
  281. * Based on the State of the CAN device it puts
  282. * the CAN device into a proper mode.
  283. *
  284. * Return: 0 on success and failure value on error
  285. */
  286. static int xcan_chip_start(struct net_device *ndev)
  287. {
  288. struct xcan_priv *priv = netdev_priv(ndev);
  289. u32 reg_msr, reg_sr_mask;
  290. int err;
  291. unsigned long timeout;
  292. /* Check if it is in reset mode */
  293. err = set_reset_mode(ndev);
  294. if (err < 0)
  295. return err;
  296. err = xcan_set_bittiming(ndev);
  297. if (err < 0)
  298. return err;
  299. /* Enable interrupts */
  300. priv->write_reg(priv, XCAN_IER_OFFSET, XCAN_INTR_ALL);
  301. /* Check whether it is loopback mode or normal mode */
  302. if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) {
  303. reg_msr = XCAN_MSR_LBACK_MASK;
  304. reg_sr_mask = XCAN_SR_LBACK_MASK;
  305. } else {
  306. reg_msr = 0x0;
  307. reg_sr_mask = XCAN_SR_NORMAL_MASK;
  308. }
  309. priv->write_reg(priv, XCAN_MSR_OFFSET, reg_msr);
  310. priv->write_reg(priv, XCAN_SRR_OFFSET, XCAN_SRR_CEN_MASK);
  311. timeout = jiffies + XCAN_TIMEOUT;
  312. while (!(priv->read_reg(priv, XCAN_SR_OFFSET) & reg_sr_mask)) {
  313. if (time_after(jiffies, timeout)) {
  314. netdev_warn(ndev,
  315. "timed out for correct mode\n");
  316. return -ETIMEDOUT;
  317. }
  318. }
  319. netdev_dbg(ndev, "status:#x%08x\n",
  320. priv->read_reg(priv, XCAN_SR_OFFSET));
  321. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  322. return 0;
  323. }
  324. /**
  325. * xcan_do_set_mode - This sets the mode of the driver
  326. * @ndev: Pointer to net_device structure
  327. * @mode: Tells the mode of the driver
  328. *
  329. * This check the drivers state and calls the
  330. * the corresponding modes to set.
  331. *
  332. * Return: 0 on success and failure value on error
  333. */
  334. static int xcan_do_set_mode(struct net_device *ndev, enum can_mode mode)
  335. {
  336. int ret;
  337. switch (mode) {
  338. case CAN_MODE_START:
  339. ret = xcan_chip_start(ndev);
  340. if (ret < 0) {
  341. netdev_err(ndev, "xcan_chip_start failed!\n");
  342. return ret;
  343. }
  344. netif_wake_queue(ndev);
  345. break;
  346. default:
  347. ret = -EOPNOTSUPP;
  348. break;
  349. }
  350. return ret;
  351. }
  352. /**
  353. * xcan_start_xmit - Starts the transmission
  354. * @skb: sk_buff pointer that contains data to be Txed
  355. * @ndev: Pointer to net_device structure
  356. *
  357. * This function is invoked from upper layers to initiate transmission. This
  358. * function uses the next available free txbuff and populates their fields to
  359. * start the transmission.
  360. *
  361. * Return: 0 on success and failure value on error
  362. */
  363. static int xcan_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  364. {
  365. struct xcan_priv *priv = netdev_priv(ndev);
  366. struct net_device_stats *stats = &ndev->stats;
  367. struct can_frame *cf = (struct can_frame *)skb->data;
  368. u32 id, dlc, data[2] = {0, 0};
  369. unsigned long flags;
  370. if (can_dropped_invalid_skb(ndev, skb))
  371. return NETDEV_TX_OK;
  372. /* Check if the TX buffer is full */
  373. if (unlikely(priv->read_reg(priv, XCAN_SR_OFFSET) &
  374. XCAN_SR_TXFLL_MASK)) {
  375. netif_stop_queue(ndev);
  376. netdev_err(ndev, "BUG!, TX FIFO full when queue awake!\n");
  377. return NETDEV_TX_BUSY;
  378. }
  379. /* Watch carefully on the bit sequence */
  380. if (cf->can_id & CAN_EFF_FLAG) {
  381. /* Extended CAN ID format */
  382. id = ((cf->can_id & CAN_EFF_MASK) << XCAN_IDR_ID2_SHIFT) &
  383. XCAN_IDR_ID2_MASK;
  384. id |= (((cf->can_id & CAN_EFF_MASK) >>
  385. (CAN_EFF_ID_BITS-CAN_SFF_ID_BITS)) <<
  386. XCAN_IDR_ID1_SHIFT) & XCAN_IDR_ID1_MASK;
  387. /* The substibute remote TX request bit should be "1"
  388. * for extended frames as in the Xilinx CAN datasheet
  389. */
  390. id |= XCAN_IDR_IDE_MASK | XCAN_IDR_SRR_MASK;
  391. if (cf->can_id & CAN_RTR_FLAG)
  392. /* Extended frames remote TX request */
  393. id |= XCAN_IDR_RTR_MASK;
  394. } else {
  395. /* Standard CAN ID format */
  396. id = ((cf->can_id & CAN_SFF_MASK) << XCAN_IDR_ID1_SHIFT) &
  397. XCAN_IDR_ID1_MASK;
  398. if (cf->can_id & CAN_RTR_FLAG)
  399. /* Standard frames remote TX request */
  400. id |= XCAN_IDR_SRR_MASK;
  401. }
  402. dlc = cf->can_dlc << XCAN_DLCR_DLC_SHIFT;
  403. if (cf->can_dlc > 0)
  404. data[0] = be32_to_cpup((__be32 *)(cf->data + 0));
  405. if (cf->can_dlc > 4)
  406. data[1] = be32_to_cpup((__be32 *)(cf->data + 4));
  407. can_put_echo_skb(skb, ndev, priv->tx_head % priv->tx_max);
  408. spin_lock_irqsave(&priv->tx_lock, flags);
  409. priv->tx_head++;
  410. /* Write the Frame to Xilinx CAN TX FIFO */
  411. priv->write_reg(priv, XCAN_TXFIFO_ID_OFFSET, id);
  412. /* If the CAN frame is RTR frame this write triggers tranmission */
  413. priv->write_reg(priv, XCAN_TXFIFO_DLC_OFFSET, dlc);
  414. if (!(cf->can_id & CAN_RTR_FLAG)) {
  415. priv->write_reg(priv, XCAN_TXFIFO_DW1_OFFSET, data[0]);
  416. /* If the CAN frame is Standard/Extended frame this
  417. * write triggers tranmission
  418. */
  419. priv->write_reg(priv, XCAN_TXFIFO_DW2_OFFSET, data[1]);
  420. stats->tx_bytes += cf->can_dlc;
  421. }
  422. /* Clear TX-FIFO-empty interrupt for xcan_tx_interrupt() */
  423. if (priv->tx_max > 1)
  424. priv->write_reg(priv, XCAN_ICR_OFFSET, XCAN_IXR_TXFEMP_MASK);
  425. /* Check if the TX buffer is full */
  426. if ((priv->tx_head - priv->tx_tail) == priv->tx_max)
  427. netif_stop_queue(ndev);
  428. spin_unlock_irqrestore(&priv->tx_lock, flags);
  429. return NETDEV_TX_OK;
  430. }
  431. /**
  432. * xcan_rx - Is called from CAN isr to complete the received
  433. * frame processing
  434. * @ndev: Pointer to net_device structure
  435. *
  436. * This function is invoked from the CAN isr(poll) to process the Rx frames. It
  437. * does minimal processing and invokes "netif_receive_skb" to complete further
  438. * processing.
  439. * Return: 1 on success and 0 on failure.
  440. */
  441. static int xcan_rx(struct net_device *ndev)
  442. {
  443. struct xcan_priv *priv = netdev_priv(ndev);
  444. struct net_device_stats *stats = &ndev->stats;
  445. struct can_frame *cf;
  446. struct sk_buff *skb;
  447. u32 id_xcan, dlc, data[2] = {0, 0};
  448. skb = alloc_can_skb(ndev, &cf);
  449. if (unlikely(!skb)) {
  450. stats->rx_dropped++;
  451. return 0;
  452. }
  453. /* Read a frame from Xilinx zynq CANPS */
  454. id_xcan = priv->read_reg(priv, XCAN_RXFIFO_ID_OFFSET);
  455. dlc = priv->read_reg(priv, XCAN_RXFIFO_DLC_OFFSET) >>
  456. XCAN_DLCR_DLC_SHIFT;
  457. /* Change Xilinx CAN data length format to socketCAN data format */
  458. cf->can_dlc = get_can_dlc(dlc);
  459. /* Change Xilinx CAN ID format to socketCAN ID format */
  460. if (id_xcan & XCAN_IDR_IDE_MASK) {
  461. /* The received frame is an Extended format frame */
  462. cf->can_id = (id_xcan & XCAN_IDR_ID1_MASK) >> 3;
  463. cf->can_id |= (id_xcan & XCAN_IDR_ID2_MASK) >>
  464. XCAN_IDR_ID2_SHIFT;
  465. cf->can_id |= CAN_EFF_FLAG;
  466. if (id_xcan & XCAN_IDR_RTR_MASK)
  467. cf->can_id |= CAN_RTR_FLAG;
  468. } else {
  469. /* The received frame is a standard format frame */
  470. cf->can_id = (id_xcan & XCAN_IDR_ID1_MASK) >>
  471. XCAN_IDR_ID1_SHIFT;
  472. if (id_xcan & XCAN_IDR_SRR_MASK)
  473. cf->can_id |= CAN_RTR_FLAG;
  474. }
  475. /* DW1/DW2 must always be read to remove message from RXFIFO */
  476. data[0] = priv->read_reg(priv, XCAN_RXFIFO_DW1_OFFSET);
  477. data[1] = priv->read_reg(priv, XCAN_RXFIFO_DW2_OFFSET);
  478. if (!(cf->can_id & CAN_RTR_FLAG)) {
  479. /* Change Xilinx CAN data format to socketCAN data format */
  480. if (cf->can_dlc > 0)
  481. *(__be32 *)(cf->data) = cpu_to_be32(data[0]);
  482. if (cf->can_dlc > 4)
  483. *(__be32 *)(cf->data + 4) = cpu_to_be32(data[1]);
  484. }
  485. stats->rx_bytes += cf->can_dlc;
  486. stats->rx_packets++;
  487. netif_receive_skb(skb);
  488. return 1;
  489. }
  490. /**
  491. * xcan_current_error_state - Get current error state from HW
  492. * @ndev: Pointer to net_device structure
  493. *
  494. * Checks the current CAN error state from the HW. Note that this
  495. * only checks for ERROR_PASSIVE and ERROR_WARNING.
  496. *
  497. * Return:
  498. * ERROR_PASSIVE or ERROR_WARNING if either is active, ERROR_ACTIVE
  499. * otherwise.
  500. */
  501. static enum can_state xcan_current_error_state(struct net_device *ndev)
  502. {
  503. struct xcan_priv *priv = netdev_priv(ndev);
  504. u32 status = priv->read_reg(priv, XCAN_SR_OFFSET);
  505. if ((status & XCAN_SR_ESTAT_MASK) == XCAN_SR_ESTAT_MASK)
  506. return CAN_STATE_ERROR_PASSIVE;
  507. else if (status & XCAN_SR_ERRWRN_MASK)
  508. return CAN_STATE_ERROR_WARNING;
  509. else
  510. return CAN_STATE_ERROR_ACTIVE;
  511. }
  512. /**
  513. * xcan_set_error_state - Set new CAN error state
  514. * @ndev: Pointer to net_device structure
  515. * @new_state: The new CAN state to be set
  516. * @cf: Error frame to be populated or NULL
  517. *
  518. * Set new CAN error state for the device, updating statistics and
  519. * populating the error frame if given.
  520. */
  521. static void xcan_set_error_state(struct net_device *ndev,
  522. enum can_state new_state,
  523. struct can_frame *cf)
  524. {
  525. struct xcan_priv *priv = netdev_priv(ndev);
  526. u32 ecr = priv->read_reg(priv, XCAN_ECR_OFFSET);
  527. u32 txerr = ecr & XCAN_ECR_TEC_MASK;
  528. u32 rxerr = (ecr & XCAN_ECR_REC_MASK) >> XCAN_ESR_REC_SHIFT;
  529. priv->can.state = new_state;
  530. if (cf) {
  531. cf->can_id |= CAN_ERR_CRTL;
  532. cf->data[6] = txerr;
  533. cf->data[7] = rxerr;
  534. }
  535. switch (new_state) {
  536. case CAN_STATE_ERROR_PASSIVE:
  537. priv->can.can_stats.error_passive++;
  538. if (cf)
  539. cf->data[1] = (rxerr > 127) ?
  540. CAN_ERR_CRTL_RX_PASSIVE :
  541. CAN_ERR_CRTL_TX_PASSIVE;
  542. break;
  543. case CAN_STATE_ERROR_WARNING:
  544. priv->can.can_stats.error_warning++;
  545. if (cf)
  546. cf->data[1] |= (txerr > rxerr) ?
  547. CAN_ERR_CRTL_TX_WARNING :
  548. CAN_ERR_CRTL_RX_WARNING;
  549. break;
  550. case CAN_STATE_ERROR_ACTIVE:
  551. if (cf)
  552. cf->data[1] |= CAN_ERR_CRTL_ACTIVE;
  553. break;
  554. default:
  555. /* non-ERROR states are handled elsewhere */
  556. WARN_ON(1);
  557. break;
  558. }
  559. }
  560. /**
  561. * xcan_update_error_state_after_rxtx - Update CAN error state after RX/TX
  562. * @ndev: Pointer to net_device structure
  563. *
  564. * If the device is in a ERROR-WARNING or ERROR-PASSIVE state, check if
  565. * the performed RX/TX has caused it to drop to a lesser state and set
  566. * the interface state accordingly.
  567. */
  568. static void xcan_update_error_state_after_rxtx(struct net_device *ndev)
  569. {
  570. struct xcan_priv *priv = netdev_priv(ndev);
  571. enum can_state old_state = priv->can.state;
  572. enum can_state new_state;
  573. /* changing error state due to successful frame RX/TX can only
  574. * occur from these states
  575. */
  576. if (old_state != CAN_STATE_ERROR_WARNING &&
  577. old_state != CAN_STATE_ERROR_PASSIVE)
  578. return;
  579. new_state = xcan_current_error_state(ndev);
  580. if (new_state != old_state) {
  581. struct sk_buff *skb;
  582. struct can_frame *cf;
  583. skb = alloc_can_err_skb(ndev, &cf);
  584. xcan_set_error_state(ndev, new_state, skb ? cf : NULL);
  585. if (skb) {
  586. struct net_device_stats *stats = &ndev->stats;
  587. stats->rx_packets++;
  588. stats->rx_bytes += cf->can_dlc;
  589. netif_rx(skb);
  590. }
  591. }
  592. }
  593. /**
  594. * xcan_err_interrupt - error frame Isr
  595. * @ndev: net_device pointer
  596. * @isr: interrupt status register value
  597. *
  598. * This is the CAN error interrupt and it will
  599. * check the the type of error and forward the error
  600. * frame to upper layers.
  601. */
  602. static void xcan_err_interrupt(struct net_device *ndev, u32 isr)
  603. {
  604. struct xcan_priv *priv = netdev_priv(ndev);
  605. struct net_device_stats *stats = &ndev->stats;
  606. struct can_frame *cf;
  607. struct sk_buff *skb;
  608. u32 err_status;
  609. skb = alloc_can_err_skb(ndev, &cf);
  610. err_status = priv->read_reg(priv, XCAN_ESR_OFFSET);
  611. priv->write_reg(priv, XCAN_ESR_OFFSET, err_status);
  612. if (isr & XCAN_IXR_BSOFF_MASK) {
  613. priv->can.state = CAN_STATE_BUS_OFF;
  614. priv->can.can_stats.bus_off++;
  615. /* Leave device in Config Mode in bus-off state */
  616. priv->write_reg(priv, XCAN_SRR_OFFSET, XCAN_SRR_RESET_MASK);
  617. can_bus_off(ndev);
  618. if (skb)
  619. cf->can_id |= CAN_ERR_BUSOFF;
  620. } else {
  621. enum can_state new_state = xcan_current_error_state(ndev);
  622. xcan_set_error_state(ndev, new_state, skb ? cf : NULL);
  623. }
  624. /* Check for Arbitration lost interrupt */
  625. if (isr & XCAN_IXR_ARBLST_MASK) {
  626. priv->can.can_stats.arbitration_lost++;
  627. if (skb) {
  628. cf->can_id |= CAN_ERR_LOSTARB;
  629. cf->data[0] = CAN_ERR_LOSTARB_UNSPEC;
  630. }
  631. }
  632. /* Check for RX FIFO Overflow interrupt */
  633. if (isr & XCAN_IXR_RXOFLW_MASK) {
  634. stats->rx_over_errors++;
  635. stats->rx_errors++;
  636. if (skb) {
  637. cf->can_id |= CAN_ERR_CRTL;
  638. cf->data[1] |= CAN_ERR_CRTL_RX_OVERFLOW;
  639. }
  640. }
  641. /* Check for error interrupt */
  642. if (isr & XCAN_IXR_ERROR_MASK) {
  643. if (skb)
  644. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  645. /* Check for Ack error interrupt */
  646. if (err_status & XCAN_ESR_ACKER_MASK) {
  647. stats->tx_errors++;
  648. if (skb) {
  649. cf->can_id |= CAN_ERR_ACK;
  650. cf->data[3] = CAN_ERR_PROT_LOC_ACK;
  651. }
  652. }
  653. /* Check for Bit error interrupt */
  654. if (err_status & XCAN_ESR_BERR_MASK) {
  655. stats->tx_errors++;
  656. if (skb) {
  657. cf->can_id |= CAN_ERR_PROT;
  658. cf->data[2] = CAN_ERR_PROT_BIT;
  659. }
  660. }
  661. /* Check for Stuff error interrupt */
  662. if (err_status & XCAN_ESR_STER_MASK) {
  663. stats->rx_errors++;
  664. if (skb) {
  665. cf->can_id |= CAN_ERR_PROT;
  666. cf->data[2] = CAN_ERR_PROT_STUFF;
  667. }
  668. }
  669. /* Check for Form error interrupt */
  670. if (err_status & XCAN_ESR_FMER_MASK) {
  671. stats->rx_errors++;
  672. if (skb) {
  673. cf->can_id |= CAN_ERR_PROT;
  674. cf->data[2] = CAN_ERR_PROT_FORM;
  675. }
  676. }
  677. /* Check for CRC error interrupt */
  678. if (err_status & XCAN_ESR_CRCER_MASK) {
  679. stats->rx_errors++;
  680. if (skb) {
  681. cf->can_id |= CAN_ERR_PROT;
  682. cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
  683. }
  684. }
  685. priv->can.can_stats.bus_error++;
  686. }
  687. if (skb) {
  688. stats->rx_packets++;
  689. stats->rx_bytes += cf->can_dlc;
  690. netif_rx(skb);
  691. }
  692. netdev_dbg(ndev, "%s: error status register:0x%x\n",
  693. __func__, priv->read_reg(priv, XCAN_ESR_OFFSET));
  694. }
  695. /**
  696. * xcan_state_interrupt - It will check the state of the CAN device
  697. * @ndev: net_device pointer
  698. * @isr: interrupt status register value
  699. *
  700. * This will checks the state of the CAN device
  701. * and puts the device into appropriate state.
  702. */
  703. static void xcan_state_interrupt(struct net_device *ndev, u32 isr)
  704. {
  705. struct xcan_priv *priv = netdev_priv(ndev);
  706. /* Check for Sleep interrupt if set put CAN device in sleep state */
  707. if (isr & XCAN_IXR_SLP_MASK)
  708. priv->can.state = CAN_STATE_SLEEPING;
  709. /* Check for Wake up interrupt if set put CAN device in Active state */
  710. if (isr & XCAN_IXR_WKUP_MASK)
  711. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  712. }
  713. /**
  714. * xcan_rx_poll - Poll routine for rx packets (NAPI)
  715. * @napi: napi structure pointer
  716. * @quota: Max number of rx packets to be processed.
  717. *
  718. * This is the poll routine for rx part.
  719. * It will process the packets maximux quota value.
  720. *
  721. * Return: number of packets received
  722. */
  723. static int xcan_rx_poll(struct napi_struct *napi, int quota)
  724. {
  725. struct net_device *ndev = napi->dev;
  726. struct xcan_priv *priv = netdev_priv(ndev);
  727. u32 isr, ier;
  728. int work_done = 0;
  729. isr = priv->read_reg(priv, XCAN_ISR_OFFSET);
  730. while ((isr & XCAN_IXR_RXNEMP_MASK) && (work_done < quota)) {
  731. work_done += xcan_rx(ndev);
  732. priv->write_reg(priv, XCAN_ICR_OFFSET, XCAN_IXR_RXNEMP_MASK);
  733. isr = priv->read_reg(priv, XCAN_ISR_OFFSET);
  734. }
  735. if (work_done) {
  736. can_led_event(ndev, CAN_LED_EVENT_RX);
  737. xcan_update_error_state_after_rxtx(ndev);
  738. }
  739. if (work_done < quota) {
  740. napi_complete(napi);
  741. ier = priv->read_reg(priv, XCAN_IER_OFFSET);
  742. ier |= XCAN_IXR_RXNEMP_MASK;
  743. priv->write_reg(priv, XCAN_IER_OFFSET, ier);
  744. }
  745. return work_done;
  746. }
  747. /**
  748. * xcan_tx_interrupt - Tx Done Isr
  749. * @ndev: net_device pointer
  750. * @isr: Interrupt status register value
  751. */
  752. static void xcan_tx_interrupt(struct net_device *ndev, u32 isr)
  753. {
  754. struct xcan_priv *priv = netdev_priv(ndev);
  755. struct net_device_stats *stats = &ndev->stats;
  756. unsigned int frames_in_fifo;
  757. int frames_sent = 1; /* TXOK => at least 1 frame was sent */
  758. unsigned long flags;
  759. int retries = 0;
  760. /* Synchronize with xmit as we need to know the exact number
  761. * of frames in the FIFO to stay in sync due to the TXFEMP
  762. * handling.
  763. * This also prevents a race between netif_wake_queue() and
  764. * netif_stop_queue().
  765. */
  766. spin_lock_irqsave(&priv->tx_lock, flags);
  767. frames_in_fifo = priv->tx_head - priv->tx_tail;
  768. if (WARN_ON_ONCE(frames_in_fifo == 0)) {
  769. /* clear TXOK anyway to avoid getting back here */
  770. priv->write_reg(priv, XCAN_ICR_OFFSET, XCAN_IXR_TXOK_MASK);
  771. spin_unlock_irqrestore(&priv->tx_lock, flags);
  772. return;
  773. }
  774. /* Check if 2 frames were sent (TXOK only means that at least 1
  775. * frame was sent).
  776. */
  777. if (frames_in_fifo > 1) {
  778. WARN_ON(frames_in_fifo > priv->tx_max);
  779. /* Synchronize TXOK and isr so that after the loop:
  780. * (1) isr variable is up-to-date at least up to TXOK clear
  781. * time. This avoids us clearing a TXOK of a second frame
  782. * but not noticing that the FIFO is now empty and thus
  783. * marking only a single frame as sent.
  784. * (2) No TXOK is left. Having one could mean leaving a
  785. * stray TXOK as we might process the associated frame
  786. * via TXFEMP handling as we read TXFEMP *after* TXOK
  787. * clear to satisfy (1).
  788. */
  789. while ((isr & XCAN_IXR_TXOK_MASK) && !WARN_ON(++retries == 100)) {
  790. priv->write_reg(priv, XCAN_ICR_OFFSET, XCAN_IXR_TXOK_MASK);
  791. isr = priv->read_reg(priv, XCAN_ISR_OFFSET);
  792. }
  793. if (isr & XCAN_IXR_TXFEMP_MASK) {
  794. /* nothing in FIFO anymore */
  795. frames_sent = frames_in_fifo;
  796. }
  797. } else {
  798. /* single frame in fifo, just clear TXOK */
  799. priv->write_reg(priv, XCAN_ICR_OFFSET, XCAN_IXR_TXOK_MASK);
  800. }
  801. while (frames_sent--) {
  802. can_get_echo_skb(ndev, priv->tx_tail %
  803. priv->tx_max);
  804. priv->tx_tail++;
  805. stats->tx_packets++;
  806. }
  807. netif_wake_queue(ndev);
  808. spin_unlock_irqrestore(&priv->tx_lock, flags);
  809. can_led_event(ndev, CAN_LED_EVENT_TX);
  810. xcan_update_error_state_after_rxtx(ndev);
  811. }
  812. /**
  813. * xcan_interrupt - CAN Isr
  814. * @irq: irq number
  815. * @dev_id: device id poniter
  816. *
  817. * This is the xilinx CAN Isr. It checks for the type of interrupt
  818. * and invokes the corresponding ISR.
  819. *
  820. * Return:
  821. * IRQ_NONE - If CAN device is in sleep mode, IRQ_HANDLED otherwise
  822. */
  823. static irqreturn_t xcan_interrupt(int irq, void *dev_id)
  824. {
  825. struct net_device *ndev = (struct net_device *)dev_id;
  826. struct xcan_priv *priv = netdev_priv(ndev);
  827. u32 isr, ier;
  828. u32 isr_errors;
  829. /* Get the interrupt status from Xilinx CAN */
  830. isr = priv->read_reg(priv, XCAN_ISR_OFFSET);
  831. if (!isr)
  832. return IRQ_NONE;
  833. /* Check for the type of interrupt and Processing it */
  834. if (isr & (XCAN_IXR_SLP_MASK | XCAN_IXR_WKUP_MASK)) {
  835. priv->write_reg(priv, XCAN_ICR_OFFSET, (XCAN_IXR_SLP_MASK |
  836. XCAN_IXR_WKUP_MASK));
  837. xcan_state_interrupt(ndev, isr);
  838. }
  839. /* Check for Tx interrupt and Processing it */
  840. if (isr & XCAN_IXR_TXOK_MASK)
  841. xcan_tx_interrupt(ndev, isr);
  842. /* Check for the type of error interrupt and Processing it */
  843. isr_errors = isr & (XCAN_IXR_ERROR_MASK | XCAN_IXR_RXOFLW_MASK |
  844. XCAN_IXR_BSOFF_MASK | XCAN_IXR_ARBLST_MASK);
  845. if (isr_errors) {
  846. priv->write_reg(priv, XCAN_ICR_OFFSET, isr_errors);
  847. xcan_err_interrupt(ndev, isr);
  848. }
  849. /* Check for the type of receive interrupt and Processing it */
  850. if (isr & XCAN_IXR_RXNEMP_MASK) {
  851. ier = priv->read_reg(priv, XCAN_IER_OFFSET);
  852. ier &= ~XCAN_IXR_RXNEMP_MASK;
  853. priv->write_reg(priv, XCAN_IER_OFFSET, ier);
  854. napi_schedule(&priv->napi);
  855. }
  856. return IRQ_HANDLED;
  857. }
  858. /**
  859. * xcan_chip_stop - Driver stop routine
  860. * @ndev: Pointer to net_device structure
  861. *
  862. * This is the drivers stop routine. It will disable the
  863. * interrupts and put the device into configuration mode.
  864. */
  865. static void xcan_chip_stop(struct net_device *ndev)
  866. {
  867. struct xcan_priv *priv = netdev_priv(ndev);
  868. /* Disable interrupts and leave the can in configuration mode */
  869. set_reset_mode(ndev);
  870. priv->can.state = CAN_STATE_STOPPED;
  871. }
  872. /**
  873. * xcan_open - Driver open routine
  874. * @ndev: Pointer to net_device structure
  875. *
  876. * This is the driver open routine.
  877. * Return: 0 on success and failure value on error
  878. */
  879. static int xcan_open(struct net_device *ndev)
  880. {
  881. struct xcan_priv *priv = netdev_priv(ndev);
  882. int ret;
  883. ret = pm_runtime_get_sync(priv->dev);
  884. if (ret < 0) {
  885. netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n",
  886. __func__, ret);
  887. return ret;
  888. }
  889. ret = request_irq(ndev->irq, xcan_interrupt, priv->irq_flags,
  890. ndev->name, ndev);
  891. if (ret < 0) {
  892. netdev_err(ndev, "irq allocation for CAN failed\n");
  893. goto err;
  894. }
  895. /* Set chip into reset mode */
  896. ret = set_reset_mode(ndev);
  897. if (ret < 0) {
  898. netdev_err(ndev, "mode resetting failed!\n");
  899. goto err_irq;
  900. }
  901. /* Common open */
  902. ret = open_candev(ndev);
  903. if (ret)
  904. goto err_irq;
  905. ret = xcan_chip_start(ndev);
  906. if (ret < 0) {
  907. netdev_err(ndev, "xcan_chip_start failed!\n");
  908. goto err_candev;
  909. }
  910. can_led_event(ndev, CAN_LED_EVENT_OPEN);
  911. napi_enable(&priv->napi);
  912. netif_start_queue(ndev);
  913. return 0;
  914. err_candev:
  915. close_candev(ndev);
  916. err_irq:
  917. free_irq(ndev->irq, ndev);
  918. err:
  919. pm_runtime_put(priv->dev);
  920. return ret;
  921. }
  922. /**
  923. * xcan_close - Driver close routine
  924. * @ndev: Pointer to net_device structure
  925. *
  926. * Return: 0 always
  927. */
  928. static int xcan_close(struct net_device *ndev)
  929. {
  930. struct xcan_priv *priv = netdev_priv(ndev);
  931. netif_stop_queue(ndev);
  932. napi_disable(&priv->napi);
  933. xcan_chip_stop(ndev);
  934. free_irq(ndev->irq, ndev);
  935. close_candev(ndev);
  936. can_led_event(ndev, CAN_LED_EVENT_STOP);
  937. pm_runtime_put(priv->dev);
  938. return 0;
  939. }
  940. /**
  941. * xcan_get_berr_counter - error counter routine
  942. * @ndev: Pointer to net_device structure
  943. * @bec: Pointer to can_berr_counter structure
  944. *
  945. * This is the driver error counter routine.
  946. * Return: 0 on success and failure value on error
  947. */
  948. static int xcan_get_berr_counter(const struct net_device *ndev,
  949. struct can_berr_counter *bec)
  950. {
  951. struct xcan_priv *priv = netdev_priv(ndev);
  952. int ret;
  953. ret = pm_runtime_get_sync(priv->dev);
  954. if (ret < 0) {
  955. netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n",
  956. __func__, ret);
  957. return ret;
  958. }
  959. bec->txerr = priv->read_reg(priv, XCAN_ECR_OFFSET) & XCAN_ECR_TEC_MASK;
  960. bec->rxerr = ((priv->read_reg(priv, XCAN_ECR_OFFSET) &
  961. XCAN_ECR_REC_MASK) >> XCAN_ESR_REC_SHIFT);
  962. pm_runtime_put(priv->dev);
  963. return 0;
  964. }
  965. static const struct net_device_ops xcan_netdev_ops = {
  966. .ndo_open = xcan_open,
  967. .ndo_stop = xcan_close,
  968. .ndo_start_xmit = xcan_start_xmit,
  969. .ndo_change_mtu = can_change_mtu,
  970. };
  971. /**
  972. * xcan_suspend - Suspend method for the driver
  973. * @dev: Address of the device structure
  974. *
  975. * Put the driver into low power mode.
  976. * Return: 0 on success and failure value on error
  977. */
  978. static int __maybe_unused xcan_suspend(struct device *dev)
  979. {
  980. struct net_device *ndev = dev_get_drvdata(dev);
  981. if (netif_running(ndev)) {
  982. netif_stop_queue(ndev);
  983. netif_device_detach(ndev);
  984. xcan_chip_stop(ndev);
  985. }
  986. return pm_runtime_force_suspend(dev);
  987. }
  988. /**
  989. * xcan_resume - Resume from suspend
  990. * @dev: Address of the device structure
  991. *
  992. * Resume operation after suspend.
  993. * Return: 0 on success and failure value on error
  994. */
  995. static int __maybe_unused xcan_resume(struct device *dev)
  996. {
  997. struct net_device *ndev = dev_get_drvdata(dev);
  998. int ret;
  999. ret = pm_runtime_force_resume(dev);
  1000. if (ret) {
  1001. dev_err(dev, "pm_runtime_force_resume failed on resume\n");
  1002. return ret;
  1003. }
  1004. if (netif_running(ndev)) {
  1005. ret = xcan_chip_start(ndev);
  1006. if (ret) {
  1007. dev_err(dev, "xcan_chip_start failed on resume\n");
  1008. return ret;
  1009. }
  1010. netif_device_attach(ndev);
  1011. netif_start_queue(ndev);
  1012. }
  1013. return 0;
  1014. }
  1015. /**
  1016. * xcan_runtime_suspend - Runtime suspend method for the driver
  1017. * @dev: Address of the device structure
  1018. *
  1019. * Put the driver into low power mode.
  1020. * Return: 0 always
  1021. */
  1022. static int __maybe_unused xcan_runtime_suspend(struct device *dev)
  1023. {
  1024. struct net_device *ndev = dev_get_drvdata(dev);
  1025. struct xcan_priv *priv = netdev_priv(ndev);
  1026. clk_disable_unprepare(priv->bus_clk);
  1027. clk_disable_unprepare(priv->can_clk);
  1028. return 0;
  1029. }
  1030. /**
  1031. * xcan_runtime_resume - Runtime resume from suspend
  1032. * @dev: Address of the device structure
  1033. *
  1034. * Resume operation after suspend.
  1035. * Return: 0 on success and failure value on error
  1036. */
  1037. static int __maybe_unused xcan_runtime_resume(struct device *dev)
  1038. {
  1039. struct net_device *ndev = dev_get_drvdata(dev);
  1040. struct xcan_priv *priv = netdev_priv(ndev);
  1041. int ret;
  1042. ret = clk_prepare_enable(priv->bus_clk);
  1043. if (ret) {
  1044. dev_err(dev, "Cannot enable clock.\n");
  1045. return ret;
  1046. }
  1047. ret = clk_prepare_enable(priv->can_clk);
  1048. if (ret) {
  1049. dev_err(dev, "Cannot enable clock.\n");
  1050. clk_disable_unprepare(priv->bus_clk);
  1051. return ret;
  1052. }
  1053. return 0;
  1054. }
  1055. static const struct dev_pm_ops xcan_dev_pm_ops = {
  1056. SET_SYSTEM_SLEEP_PM_OPS(xcan_suspend, xcan_resume)
  1057. SET_RUNTIME_PM_OPS(xcan_runtime_suspend, xcan_runtime_resume, NULL)
  1058. };
  1059. static const struct xcan_devtype_data xcan_zynq_data = {
  1060. .caps = XCAN_CAP_WATERMARK,
  1061. };
  1062. /* Match table for OF platform binding */
  1063. static const struct of_device_id xcan_of_match[] = {
  1064. { .compatible = "xlnx,zynq-can-1.0", .data = &xcan_zynq_data },
  1065. { .compatible = "xlnx,axi-can-1.00.a", },
  1066. { /* end of list */ },
  1067. };
  1068. MODULE_DEVICE_TABLE(of, xcan_of_match);
  1069. /**
  1070. * xcan_probe - Platform registration call
  1071. * @pdev: Handle to the platform device structure
  1072. *
  1073. * This function does all the memory allocation and registration for the CAN
  1074. * device.
  1075. *
  1076. * Return: 0 on success and failure value on error
  1077. */
  1078. static int xcan_probe(struct platform_device *pdev)
  1079. {
  1080. struct resource *res; /* IO mem resources */
  1081. struct net_device *ndev;
  1082. struct xcan_priv *priv;
  1083. const struct of_device_id *of_id;
  1084. int caps = 0;
  1085. void __iomem *addr;
  1086. int ret, rx_max, tx_max, tx_fifo_depth;
  1087. /* Get the virtual base address for the device */
  1088. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1089. addr = devm_ioremap_resource(&pdev->dev, res);
  1090. if (IS_ERR(addr)) {
  1091. ret = PTR_ERR(addr);
  1092. goto err;
  1093. }
  1094. ret = of_property_read_u32(pdev->dev.of_node, "tx-fifo-depth",
  1095. &tx_fifo_depth);
  1096. if (ret < 0)
  1097. goto err;
  1098. ret = of_property_read_u32(pdev->dev.of_node, "rx-fifo-depth", &rx_max);
  1099. if (ret < 0)
  1100. goto err;
  1101. of_id = of_match_device(xcan_of_match, &pdev->dev);
  1102. if (of_id) {
  1103. const struct xcan_devtype_data *devtype_data = of_id->data;
  1104. if (devtype_data)
  1105. caps = devtype_data->caps;
  1106. }
  1107. /* There is no way to directly figure out how many frames have been
  1108. * sent when the TXOK interrupt is processed. If watermark programming
  1109. * is supported, we can have 2 frames in the FIFO and use TXFEMP
  1110. * to determine if 1 or 2 frames have been sent.
  1111. * Theoretically we should be able to use TXFWMEMP to determine up
  1112. * to 3 frames, but it seems that after putting a second frame in the
  1113. * FIFO, with watermark at 2 frames, it can happen that TXFWMEMP (less
  1114. * than 2 frames in FIFO) is set anyway with no TXOK (a frame was
  1115. * sent), which is not a sensible state - possibly TXFWMEMP is not
  1116. * completely synchronized with the rest of the bits?
  1117. */
  1118. if (caps & XCAN_CAP_WATERMARK)
  1119. tx_max = min(tx_fifo_depth, 2);
  1120. else
  1121. tx_max = 1;
  1122. /* Create a CAN device instance */
  1123. ndev = alloc_candev(sizeof(struct xcan_priv), tx_max);
  1124. if (!ndev)
  1125. return -ENOMEM;
  1126. priv = netdev_priv(ndev);
  1127. priv->dev = &pdev->dev;
  1128. priv->can.bittiming_const = &xcan_bittiming_const;
  1129. priv->can.do_set_mode = xcan_do_set_mode;
  1130. priv->can.do_get_berr_counter = xcan_get_berr_counter;
  1131. priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
  1132. CAN_CTRLMODE_BERR_REPORTING;
  1133. priv->reg_base = addr;
  1134. priv->tx_max = tx_max;
  1135. spin_lock_init(&priv->tx_lock);
  1136. /* Get IRQ for the device */
  1137. ndev->irq = platform_get_irq(pdev, 0);
  1138. ndev->flags |= IFF_ECHO; /* We support local echo */
  1139. platform_set_drvdata(pdev, ndev);
  1140. SET_NETDEV_DEV(ndev, &pdev->dev);
  1141. ndev->netdev_ops = &xcan_netdev_ops;
  1142. /* Getting the CAN can_clk info */
  1143. priv->can_clk = devm_clk_get(&pdev->dev, "can_clk");
  1144. if (IS_ERR(priv->can_clk)) {
  1145. dev_err(&pdev->dev, "Device clock not found.\n");
  1146. ret = PTR_ERR(priv->can_clk);
  1147. goto err_free;
  1148. }
  1149. /* Check for type of CAN device */
  1150. if (of_device_is_compatible(pdev->dev.of_node,
  1151. "xlnx,zynq-can-1.0")) {
  1152. priv->bus_clk = devm_clk_get(&pdev->dev, "pclk");
  1153. if (IS_ERR(priv->bus_clk)) {
  1154. dev_err(&pdev->dev, "bus clock not found\n");
  1155. ret = PTR_ERR(priv->bus_clk);
  1156. goto err_free;
  1157. }
  1158. } else {
  1159. priv->bus_clk = devm_clk_get(&pdev->dev, "s_axi_aclk");
  1160. if (IS_ERR(priv->bus_clk)) {
  1161. dev_err(&pdev->dev, "bus clock not found\n");
  1162. ret = PTR_ERR(priv->bus_clk);
  1163. goto err_free;
  1164. }
  1165. }
  1166. priv->write_reg = xcan_write_reg_le;
  1167. priv->read_reg = xcan_read_reg_le;
  1168. pm_runtime_enable(&pdev->dev);
  1169. ret = pm_runtime_get_sync(&pdev->dev);
  1170. if (ret < 0) {
  1171. netdev_err(ndev, "%s: pm_runtime_get failed(%d)\n",
  1172. __func__, ret);
  1173. goto err_pmdisable;
  1174. }
  1175. if (priv->read_reg(priv, XCAN_SR_OFFSET) != XCAN_SR_CONFIG_MASK) {
  1176. priv->write_reg = xcan_write_reg_be;
  1177. priv->read_reg = xcan_read_reg_be;
  1178. }
  1179. priv->can.clock.freq = clk_get_rate(priv->can_clk);
  1180. netif_napi_add(ndev, &priv->napi, xcan_rx_poll, rx_max);
  1181. ret = register_candev(ndev);
  1182. if (ret) {
  1183. dev_err(&pdev->dev, "fail to register failed (err=%d)\n", ret);
  1184. goto err_disableclks;
  1185. }
  1186. devm_can_led_init(ndev);
  1187. pm_runtime_put(&pdev->dev);
  1188. netdev_dbg(ndev, "reg_base=0x%p irq=%d clock=%d, tx fifo depth: actual %d, using %d\n",
  1189. priv->reg_base, ndev->irq, priv->can.clock.freq,
  1190. tx_fifo_depth, priv->tx_max);
  1191. return 0;
  1192. err_disableclks:
  1193. pm_runtime_put(priv->dev);
  1194. err_pmdisable:
  1195. pm_runtime_disable(&pdev->dev);
  1196. err_free:
  1197. free_candev(ndev);
  1198. err:
  1199. return ret;
  1200. }
  1201. /**
  1202. * xcan_remove - Unregister the device after releasing the resources
  1203. * @pdev: Handle to the platform device structure
  1204. *
  1205. * This function frees all the resources allocated to the device.
  1206. * Return: 0 always
  1207. */
  1208. static int xcan_remove(struct platform_device *pdev)
  1209. {
  1210. struct net_device *ndev = platform_get_drvdata(pdev);
  1211. struct xcan_priv *priv = netdev_priv(ndev);
  1212. unregister_candev(ndev);
  1213. pm_runtime_disable(&pdev->dev);
  1214. netif_napi_del(&priv->napi);
  1215. free_candev(ndev);
  1216. return 0;
  1217. }
  1218. static struct platform_driver xcan_driver = {
  1219. .probe = xcan_probe,
  1220. .remove = xcan_remove,
  1221. .driver = {
  1222. .name = DRIVER_NAME,
  1223. .pm = &xcan_dev_pm_ops,
  1224. .of_match_table = xcan_of_match,
  1225. },
  1226. };
  1227. module_platform_driver(xcan_driver);
  1228. MODULE_LICENSE("GPL");
  1229. MODULE_AUTHOR("Xilinx Inc");
  1230. MODULE_DESCRIPTION("Xilinx CAN interface");