xilinx_can.c 34 KB

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