ifi_canfd.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. /*
  2. * CAN bus driver for IFI CANFD controller
  3. *
  4. * Copyright (C) 2016 Marek Vasut <marex@denx.de>
  5. *
  6. * Details about this controller can be found at
  7. * http://www.ifi-pld.de/IP/CANFD/canfd.html
  8. *
  9. * This file is licensed under the terms of the GNU General Public
  10. * License version 2. This program is licensed "as is" without any
  11. * warranty of any kind, whether express or implied.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/io.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/can/dev.h>
  24. #define IFI_CANFD_STCMD 0x0
  25. #define IFI_CANFD_STCMD_HARDRESET 0xDEADCAFD
  26. #define IFI_CANFD_STCMD_ENABLE BIT(0)
  27. #define IFI_CANFD_STCMD_ERROR_ACTIVE BIT(2)
  28. #define IFI_CANFD_STCMD_ERROR_PASSIVE BIT(3)
  29. #define IFI_CANFD_STCMD_BUSOFF BIT(4)
  30. #define IFI_CANFD_STCMD_ERROR_WARNING BIT(5)
  31. #define IFI_CANFD_STCMD_BUSMONITOR BIT(16)
  32. #define IFI_CANFD_STCMD_LOOPBACK BIT(18)
  33. #define IFI_CANFD_STCMD_DISABLE_CANFD BIT(24)
  34. #define IFI_CANFD_STCMD_ENABLE_ISO BIT(25)
  35. #define IFI_CANFD_STCMD_ENABLE_7_9_8_8_TIMING BIT(26)
  36. #define IFI_CANFD_STCMD_NORMAL_MODE ((u32)BIT(31))
  37. #define IFI_CANFD_RXSTCMD 0x4
  38. #define IFI_CANFD_RXSTCMD_REMOVE_MSG BIT(0)
  39. #define IFI_CANFD_RXSTCMD_RESET BIT(7)
  40. #define IFI_CANFD_RXSTCMD_EMPTY BIT(8)
  41. #define IFI_CANFD_RXSTCMD_OVERFLOW BIT(13)
  42. #define IFI_CANFD_TXSTCMD 0x8
  43. #define IFI_CANFD_TXSTCMD_ADD_MSG BIT(0)
  44. #define IFI_CANFD_TXSTCMD_HIGH_PRIO BIT(1)
  45. #define IFI_CANFD_TXSTCMD_RESET BIT(7)
  46. #define IFI_CANFD_TXSTCMD_EMPTY BIT(8)
  47. #define IFI_CANFD_TXSTCMD_FULL BIT(12)
  48. #define IFI_CANFD_TXSTCMD_OVERFLOW BIT(13)
  49. #define IFI_CANFD_INTERRUPT 0xc
  50. #define IFI_CANFD_INTERRUPT_ERROR_BUSOFF BIT(0)
  51. #define IFI_CANFD_INTERRUPT_ERROR_WARNING BIT(1)
  52. #define IFI_CANFD_INTERRUPT_ERROR_STATE_CHG BIT(2)
  53. #define IFI_CANFD_INTERRUPT_ERROR_REC_TEC_INC BIT(3)
  54. #define IFI_CANFD_INTERRUPT_ERROR_COUNTER BIT(10)
  55. #define IFI_CANFD_INTERRUPT_TXFIFO_EMPTY BIT(16)
  56. #define IFI_CANFD_INTERRUPT_TXFIFO_REMOVE BIT(22)
  57. #define IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY BIT(24)
  58. #define IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY_PER BIT(25)
  59. #define IFI_CANFD_INTERRUPT_SET_IRQ ((u32)BIT(31))
  60. #define IFI_CANFD_IRQMASK 0x10
  61. #define IFI_CANFD_IRQMASK_ERROR_BUSOFF BIT(0)
  62. #define IFI_CANFD_IRQMASK_ERROR_WARNING BIT(1)
  63. #define IFI_CANFD_IRQMASK_ERROR_STATE_CHG BIT(2)
  64. #define IFI_CANFD_IRQMASK_ERROR_REC_TEC_INC BIT(3)
  65. #define IFI_CANFD_IRQMASK_SET_ERR BIT(7)
  66. #define IFI_CANFD_IRQMASK_SET_TS BIT(15)
  67. #define IFI_CANFD_IRQMASK_TXFIFO_EMPTY BIT(16)
  68. #define IFI_CANFD_IRQMASK_SET_TX BIT(23)
  69. #define IFI_CANFD_IRQMASK_RXFIFO_NEMPTY BIT(24)
  70. #define IFI_CANFD_IRQMASK_SET_RX ((u32)BIT(31))
  71. #define IFI_CANFD_TIME 0x14
  72. #define IFI_CANFD_FTIME 0x18
  73. #define IFI_CANFD_TIME_TIMEB_OFF 0
  74. #define IFI_CANFD_TIME_TIMEA_OFF 8
  75. #define IFI_CANFD_TIME_PRESCALE_OFF 16
  76. #define IFI_CANFD_TIME_SJW_OFF_7_9_8_8 25
  77. #define IFI_CANFD_TIME_SJW_OFF_4_12_6_6 28
  78. #define IFI_CANFD_TIME_SET_SJW_4_12_6_6 BIT(6)
  79. #define IFI_CANFD_TIME_SET_TIMEB_4_12_6_6 BIT(7)
  80. #define IFI_CANFD_TIME_SET_PRESC_4_12_6_6 BIT(14)
  81. #define IFI_CANFD_TIME_SET_TIMEA_4_12_6_6 BIT(15)
  82. #define IFI_CANFD_TDELAY 0x1c
  83. #define IFI_CANFD_TDELAY_DEFAULT 0xb
  84. #define IFI_CANFD_TDELAY_MASK 0x3fff
  85. #define IFI_CANFD_TDELAY_ABS BIT(14)
  86. #define IFI_CANFD_TDELAY_EN BIT(15)
  87. #define IFI_CANFD_ERROR 0x20
  88. #define IFI_CANFD_ERROR_TX_OFFSET 0
  89. #define IFI_CANFD_ERROR_TX_MASK 0xff
  90. #define IFI_CANFD_ERROR_RX_OFFSET 16
  91. #define IFI_CANFD_ERROR_RX_MASK 0xff
  92. #define IFI_CANFD_ERRCNT 0x24
  93. #define IFI_CANFD_SUSPEND 0x28
  94. #define IFI_CANFD_REPEAT 0x2c
  95. #define IFI_CANFD_TRAFFIC 0x30
  96. #define IFI_CANFD_TSCONTROL 0x34
  97. #define IFI_CANFD_TSC 0x38
  98. #define IFI_CANFD_TST 0x3c
  99. #define IFI_CANFD_RES1 0x40
  100. #define IFI_CANFD_ERROR_CTR 0x44
  101. #define IFI_CANFD_ERROR_CTR_UNLOCK_MAGIC 0x21302899
  102. #define IFI_CANFD_ERROR_CTR_OVERLOAD_FIRST BIT(0)
  103. #define IFI_CANFD_ERROR_CTR_ACK_ERROR_FIRST BIT(1)
  104. #define IFI_CANFD_ERROR_CTR_BIT0_ERROR_FIRST BIT(2)
  105. #define IFI_CANFD_ERROR_CTR_BIT1_ERROR_FIRST BIT(3)
  106. #define IFI_CANFD_ERROR_CTR_STUFF_ERROR_FIRST BIT(4)
  107. #define IFI_CANFD_ERROR_CTR_CRC_ERROR_FIRST BIT(5)
  108. #define IFI_CANFD_ERROR_CTR_FORM_ERROR_FIRST BIT(6)
  109. #define IFI_CANFD_ERROR_CTR_OVERLOAD_ALL BIT(8)
  110. #define IFI_CANFD_ERROR_CTR_ACK_ERROR_ALL BIT(9)
  111. #define IFI_CANFD_ERROR_CTR_BIT0_ERROR_ALL BIT(10)
  112. #define IFI_CANFD_ERROR_CTR_BIT1_ERROR_ALL BIT(11)
  113. #define IFI_CANFD_ERROR_CTR_STUFF_ERROR_ALL BIT(12)
  114. #define IFI_CANFD_ERROR_CTR_CRC_ERROR_ALL BIT(13)
  115. #define IFI_CANFD_ERROR_CTR_FORM_ERROR_ALL BIT(14)
  116. #define IFI_CANFD_ERROR_CTR_BITPOSITION_OFFSET 16
  117. #define IFI_CANFD_ERROR_CTR_BITPOSITION_MASK 0xff
  118. #define IFI_CANFD_ERROR_CTR_ER_RESET BIT(30)
  119. #define IFI_CANFD_ERROR_CTR_ER_ENABLE ((u32)BIT(31))
  120. #define IFI_CANFD_PAR 0x48
  121. #define IFI_CANFD_CANCLOCK 0x4c
  122. #define IFI_CANFD_SYSCLOCK 0x50
  123. #define IFI_CANFD_VER 0x54
  124. #define IFI_CANFD_VER_REV_MASK 0xff
  125. #define IFI_CANFD_VER_REV_MIN_SUPPORTED 0x15
  126. #define IFI_CANFD_IP_ID 0x58
  127. #define IFI_CANFD_IP_ID_VALUE 0xD073CAFD
  128. #define IFI_CANFD_TEST 0x5c
  129. #define IFI_CANFD_RXFIFO_TS_63_32 0x60
  130. #define IFI_CANFD_RXFIFO_TS_31_0 0x64
  131. #define IFI_CANFD_RXFIFO_DLC 0x68
  132. #define IFI_CANFD_RXFIFO_DLC_DLC_OFFSET 0
  133. #define IFI_CANFD_RXFIFO_DLC_DLC_MASK 0xf
  134. #define IFI_CANFD_RXFIFO_DLC_RTR BIT(4)
  135. #define IFI_CANFD_RXFIFO_DLC_EDL BIT(5)
  136. #define IFI_CANFD_RXFIFO_DLC_BRS BIT(6)
  137. #define IFI_CANFD_RXFIFO_DLC_ESI BIT(7)
  138. #define IFI_CANFD_RXFIFO_DLC_OBJ_OFFSET 8
  139. #define IFI_CANFD_RXFIFO_DLC_OBJ_MASK 0x1ff
  140. #define IFI_CANFD_RXFIFO_DLC_FNR_OFFSET 24
  141. #define IFI_CANFD_RXFIFO_DLC_FNR_MASK 0xff
  142. #define IFI_CANFD_RXFIFO_ID 0x6c
  143. #define IFI_CANFD_RXFIFO_ID_ID_OFFSET 0
  144. #define IFI_CANFD_RXFIFO_ID_ID_STD_MASK CAN_SFF_MASK
  145. #define IFI_CANFD_RXFIFO_ID_ID_STD_OFFSET 0
  146. #define IFI_CANFD_RXFIFO_ID_ID_STD_WIDTH 10
  147. #define IFI_CANFD_RXFIFO_ID_ID_XTD_MASK CAN_EFF_MASK
  148. #define IFI_CANFD_RXFIFO_ID_ID_XTD_OFFSET 11
  149. #define IFI_CANFD_RXFIFO_ID_ID_XTD_WIDTH 18
  150. #define IFI_CANFD_RXFIFO_ID_IDE BIT(29)
  151. #define IFI_CANFD_RXFIFO_DATA 0x70 /* 0x70..0xac */
  152. #define IFI_CANFD_TXFIFO_SUSPEND_US 0xb0
  153. #define IFI_CANFD_TXFIFO_REPEATCOUNT 0xb4
  154. #define IFI_CANFD_TXFIFO_DLC 0xb8
  155. #define IFI_CANFD_TXFIFO_DLC_DLC_OFFSET 0
  156. #define IFI_CANFD_TXFIFO_DLC_DLC_MASK 0xf
  157. #define IFI_CANFD_TXFIFO_DLC_RTR BIT(4)
  158. #define IFI_CANFD_TXFIFO_DLC_EDL BIT(5)
  159. #define IFI_CANFD_TXFIFO_DLC_BRS BIT(6)
  160. #define IFI_CANFD_TXFIFO_DLC_FNR_OFFSET 24
  161. #define IFI_CANFD_TXFIFO_DLC_FNR_MASK 0xff
  162. #define IFI_CANFD_TXFIFO_ID 0xbc
  163. #define IFI_CANFD_TXFIFO_ID_ID_OFFSET 0
  164. #define IFI_CANFD_TXFIFO_ID_ID_STD_MASK CAN_SFF_MASK
  165. #define IFI_CANFD_TXFIFO_ID_ID_STD_OFFSET 0
  166. #define IFI_CANFD_TXFIFO_ID_ID_STD_WIDTH 10
  167. #define IFI_CANFD_TXFIFO_ID_ID_XTD_MASK CAN_EFF_MASK
  168. #define IFI_CANFD_TXFIFO_ID_ID_XTD_OFFSET 11
  169. #define IFI_CANFD_TXFIFO_ID_ID_XTD_WIDTH 18
  170. #define IFI_CANFD_TXFIFO_ID_IDE BIT(29)
  171. #define IFI_CANFD_TXFIFO_DATA 0xc0 /* 0xb0..0xfc */
  172. #define IFI_CANFD_FILTER_MASK(n) (0x800 + ((n) * 8) + 0)
  173. #define IFI_CANFD_FILTER_MASK_EXT BIT(29)
  174. #define IFI_CANFD_FILTER_MASK_EDL BIT(30)
  175. #define IFI_CANFD_FILTER_MASK_VALID ((u32)BIT(31))
  176. #define IFI_CANFD_FILTER_IDENT(n) (0x800 + ((n) * 8) + 4)
  177. #define IFI_CANFD_FILTER_IDENT_IDE BIT(29)
  178. #define IFI_CANFD_FILTER_IDENT_CANFD BIT(30)
  179. #define IFI_CANFD_FILTER_IDENT_VALID ((u32)BIT(31))
  180. /* IFI CANFD private data structure */
  181. struct ifi_canfd_priv {
  182. struct can_priv can; /* must be the first member */
  183. struct napi_struct napi;
  184. struct net_device *ndev;
  185. void __iomem *base;
  186. };
  187. static void ifi_canfd_irq_enable(struct net_device *ndev, bool enable)
  188. {
  189. struct ifi_canfd_priv *priv = netdev_priv(ndev);
  190. u32 enirq = 0;
  191. if (enable) {
  192. enirq = IFI_CANFD_IRQMASK_TXFIFO_EMPTY |
  193. IFI_CANFD_IRQMASK_RXFIFO_NEMPTY |
  194. IFI_CANFD_IRQMASK_ERROR_STATE_CHG |
  195. IFI_CANFD_IRQMASK_ERROR_WARNING |
  196. IFI_CANFD_IRQMASK_ERROR_BUSOFF;
  197. if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
  198. enirq |= IFI_CANFD_INTERRUPT_ERROR_COUNTER;
  199. }
  200. writel(IFI_CANFD_IRQMASK_SET_ERR |
  201. IFI_CANFD_IRQMASK_SET_TS |
  202. IFI_CANFD_IRQMASK_SET_TX |
  203. IFI_CANFD_IRQMASK_SET_RX | enirq,
  204. priv->base + IFI_CANFD_IRQMASK);
  205. }
  206. static void ifi_canfd_read_fifo(struct net_device *ndev)
  207. {
  208. struct net_device_stats *stats = &ndev->stats;
  209. struct ifi_canfd_priv *priv = netdev_priv(ndev);
  210. struct canfd_frame *cf;
  211. struct sk_buff *skb;
  212. const u32 rx_irq_mask = IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY |
  213. IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY_PER;
  214. u32 rxdlc, rxid;
  215. u32 dlc, id;
  216. int i;
  217. rxdlc = readl(priv->base + IFI_CANFD_RXFIFO_DLC);
  218. if (rxdlc & IFI_CANFD_RXFIFO_DLC_EDL)
  219. skb = alloc_canfd_skb(ndev, &cf);
  220. else
  221. skb = alloc_can_skb(ndev, (struct can_frame **)&cf);
  222. if (!skb) {
  223. stats->rx_dropped++;
  224. return;
  225. }
  226. dlc = (rxdlc >> IFI_CANFD_RXFIFO_DLC_DLC_OFFSET) &
  227. IFI_CANFD_RXFIFO_DLC_DLC_MASK;
  228. if (rxdlc & IFI_CANFD_RXFIFO_DLC_EDL)
  229. cf->len = can_dlc2len(dlc);
  230. else
  231. cf->len = get_can_dlc(dlc);
  232. rxid = readl(priv->base + IFI_CANFD_RXFIFO_ID);
  233. id = (rxid >> IFI_CANFD_RXFIFO_ID_ID_OFFSET);
  234. if (id & IFI_CANFD_RXFIFO_ID_IDE) {
  235. id &= IFI_CANFD_RXFIFO_ID_ID_XTD_MASK;
  236. /*
  237. * In case the Extended ID frame is received, the standard
  238. * and extended part of the ID are swapped in the register,
  239. * so swap them back to obtain the correct ID.
  240. */
  241. id = (id >> IFI_CANFD_RXFIFO_ID_ID_XTD_OFFSET) |
  242. ((id & IFI_CANFD_RXFIFO_ID_ID_STD_MASK) <<
  243. IFI_CANFD_RXFIFO_ID_ID_XTD_WIDTH);
  244. id |= CAN_EFF_FLAG;
  245. } else {
  246. id &= IFI_CANFD_RXFIFO_ID_ID_STD_MASK;
  247. }
  248. cf->can_id = id;
  249. if (rxdlc & IFI_CANFD_RXFIFO_DLC_ESI) {
  250. cf->flags |= CANFD_ESI;
  251. netdev_dbg(ndev, "ESI Error\n");
  252. }
  253. if (!(rxdlc & IFI_CANFD_RXFIFO_DLC_EDL) &&
  254. (rxdlc & IFI_CANFD_RXFIFO_DLC_RTR)) {
  255. cf->can_id |= CAN_RTR_FLAG;
  256. } else {
  257. if (rxdlc & IFI_CANFD_RXFIFO_DLC_BRS)
  258. cf->flags |= CANFD_BRS;
  259. for (i = 0; i < cf->len; i += 4) {
  260. *(u32 *)(cf->data + i) =
  261. readl(priv->base + IFI_CANFD_RXFIFO_DATA + i);
  262. }
  263. }
  264. /* Remove the packet from FIFO */
  265. writel(IFI_CANFD_RXSTCMD_REMOVE_MSG, priv->base + IFI_CANFD_RXSTCMD);
  266. writel(rx_irq_mask, priv->base + IFI_CANFD_INTERRUPT);
  267. stats->rx_packets++;
  268. stats->rx_bytes += cf->len;
  269. netif_receive_skb(skb);
  270. }
  271. static int ifi_canfd_do_rx_poll(struct net_device *ndev, int quota)
  272. {
  273. struct ifi_canfd_priv *priv = netdev_priv(ndev);
  274. u32 pkts = 0;
  275. u32 rxst;
  276. rxst = readl(priv->base + IFI_CANFD_RXSTCMD);
  277. if (rxst & IFI_CANFD_RXSTCMD_EMPTY) {
  278. netdev_dbg(ndev, "No messages in RX FIFO\n");
  279. return 0;
  280. }
  281. for (;;) {
  282. if (rxst & IFI_CANFD_RXSTCMD_EMPTY)
  283. break;
  284. if (quota <= 0)
  285. break;
  286. ifi_canfd_read_fifo(ndev);
  287. quota--;
  288. pkts++;
  289. rxst = readl(priv->base + IFI_CANFD_RXSTCMD);
  290. }
  291. if (pkts)
  292. can_led_event(ndev, CAN_LED_EVENT_RX);
  293. return pkts;
  294. }
  295. static int ifi_canfd_handle_lost_msg(struct net_device *ndev)
  296. {
  297. struct net_device_stats *stats = &ndev->stats;
  298. struct sk_buff *skb;
  299. struct can_frame *frame;
  300. netdev_err(ndev, "RX FIFO overflow, message(s) lost.\n");
  301. stats->rx_errors++;
  302. stats->rx_over_errors++;
  303. skb = alloc_can_err_skb(ndev, &frame);
  304. if (unlikely(!skb))
  305. return 0;
  306. frame->can_id |= CAN_ERR_CRTL;
  307. frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
  308. netif_receive_skb(skb);
  309. return 1;
  310. }
  311. static int ifi_canfd_handle_lec_err(struct net_device *ndev)
  312. {
  313. struct ifi_canfd_priv *priv = netdev_priv(ndev);
  314. struct net_device_stats *stats = &ndev->stats;
  315. struct can_frame *cf;
  316. struct sk_buff *skb;
  317. u32 errctr = readl(priv->base + IFI_CANFD_ERROR_CTR);
  318. const u32 errmask = IFI_CANFD_ERROR_CTR_OVERLOAD_FIRST |
  319. IFI_CANFD_ERROR_CTR_ACK_ERROR_FIRST |
  320. IFI_CANFD_ERROR_CTR_BIT0_ERROR_FIRST |
  321. IFI_CANFD_ERROR_CTR_BIT1_ERROR_FIRST |
  322. IFI_CANFD_ERROR_CTR_STUFF_ERROR_FIRST |
  323. IFI_CANFD_ERROR_CTR_CRC_ERROR_FIRST |
  324. IFI_CANFD_ERROR_CTR_FORM_ERROR_FIRST;
  325. if (!(errctr & errmask)) /* No error happened. */
  326. return 0;
  327. priv->can.can_stats.bus_error++;
  328. stats->rx_errors++;
  329. /* Propagate the error condition to the CAN stack. */
  330. skb = alloc_can_err_skb(ndev, &cf);
  331. if (unlikely(!skb))
  332. return 0;
  333. /* Read the error counter register and check for new errors. */
  334. cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
  335. if (errctr & IFI_CANFD_ERROR_CTR_OVERLOAD_FIRST)
  336. cf->data[2] |= CAN_ERR_PROT_OVERLOAD;
  337. if (errctr & IFI_CANFD_ERROR_CTR_ACK_ERROR_FIRST)
  338. cf->data[3] = CAN_ERR_PROT_LOC_ACK;
  339. if (errctr & IFI_CANFD_ERROR_CTR_BIT0_ERROR_FIRST)
  340. cf->data[2] |= CAN_ERR_PROT_BIT0;
  341. if (errctr & IFI_CANFD_ERROR_CTR_BIT1_ERROR_FIRST)
  342. cf->data[2] |= CAN_ERR_PROT_BIT1;
  343. if (errctr & IFI_CANFD_ERROR_CTR_STUFF_ERROR_FIRST)
  344. cf->data[2] |= CAN_ERR_PROT_STUFF;
  345. if (errctr & IFI_CANFD_ERROR_CTR_CRC_ERROR_FIRST)
  346. cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ;
  347. if (errctr & IFI_CANFD_ERROR_CTR_FORM_ERROR_FIRST)
  348. cf->data[2] |= CAN_ERR_PROT_FORM;
  349. /* Reset the error counter, ack the IRQ and re-enable the counter. */
  350. writel(IFI_CANFD_ERROR_CTR_ER_RESET, priv->base + IFI_CANFD_ERROR_CTR);
  351. writel(IFI_CANFD_INTERRUPT_ERROR_COUNTER,
  352. priv->base + IFI_CANFD_INTERRUPT);
  353. writel(IFI_CANFD_ERROR_CTR_ER_ENABLE, priv->base + IFI_CANFD_ERROR_CTR);
  354. stats->rx_packets++;
  355. stats->rx_bytes += cf->can_dlc;
  356. netif_receive_skb(skb);
  357. return 1;
  358. }
  359. static int ifi_canfd_get_berr_counter(const struct net_device *ndev,
  360. struct can_berr_counter *bec)
  361. {
  362. struct ifi_canfd_priv *priv = netdev_priv(ndev);
  363. u32 err;
  364. err = readl(priv->base + IFI_CANFD_ERROR);
  365. bec->rxerr = (err >> IFI_CANFD_ERROR_RX_OFFSET) &
  366. IFI_CANFD_ERROR_RX_MASK;
  367. bec->txerr = (err >> IFI_CANFD_ERROR_TX_OFFSET) &
  368. IFI_CANFD_ERROR_TX_MASK;
  369. return 0;
  370. }
  371. static int ifi_canfd_handle_state_change(struct net_device *ndev,
  372. enum can_state new_state)
  373. {
  374. struct ifi_canfd_priv *priv = netdev_priv(ndev);
  375. struct net_device_stats *stats = &ndev->stats;
  376. struct can_frame *cf;
  377. struct sk_buff *skb;
  378. struct can_berr_counter bec;
  379. switch (new_state) {
  380. case CAN_STATE_ERROR_ACTIVE:
  381. /* error active state */
  382. priv->can.can_stats.error_warning++;
  383. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  384. break;
  385. case CAN_STATE_ERROR_WARNING:
  386. /* error warning state */
  387. priv->can.can_stats.error_warning++;
  388. priv->can.state = CAN_STATE_ERROR_WARNING;
  389. break;
  390. case CAN_STATE_ERROR_PASSIVE:
  391. /* error passive state */
  392. priv->can.can_stats.error_passive++;
  393. priv->can.state = CAN_STATE_ERROR_PASSIVE;
  394. break;
  395. case CAN_STATE_BUS_OFF:
  396. /* bus-off state */
  397. priv->can.state = CAN_STATE_BUS_OFF;
  398. ifi_canfd_irq_enable(ndev, 0);
  399. priv->can.can_stats.bus_off++;
  400. can_bus_off(ndev);
  401. break;
  402. default:
  403. break;
  404. }
  405. /* propagate the error condition to the CAN stack */
  406. skb = alloc_can_err_skb(ndev, &cf);
  407. if (unlikely(!skb))
  408. return 0;
  409. ifi_canfd_get_berr_counter(ndev, &bec);
  410. switch (new_state) {
  411. case CAN_STATE_ERROR_WARNING:
  412. /* error warning state */
  413. cf->can_id |= CAN_ERR_CRTL;
  414. cf->data[1] = (bec.txerr > bec.rxerr) ?
  415. CAN_ERR_CRTL_TX_WARNING :
  416. CAN_ERR_CRTL_RX_WARNING;
  417. cf->data[6] = bec.txerr;
  418. cf->data[7] = bec.rxerr;
  419. break;
  420. case CAN_STATE_ERROR_PASSIVE:
  421. /* error passive state */
  422. cf->can_id |= CAN_ERR_CRTL;
  423. cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
  424. if (bec.txerr > 127)
  425. cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
  426. cf->data[6] = bec.txerr;
  427. cf->data[7] = bec.rxerr;
  428. break;
  429. case CAN_STATE_BUS_OFF:
  430. /* bus-off state */
  431. cf->can_id |= CAN_ERR_BUSOFF;
  432. break;
  433. default:
  434. break;
  435. }
  436. stats->rx_packets++;
  437. stats->rx_bytes += cf->can_dlc;
  438. netif_receive_skb(skb);
  439. return 1;
  440. }
  441. static int ifi_canfd_handle_state_errors(struct net_device *ndev)
  442. {
  443. struct ifi_canfd_priv *priv = netdev_priv(ndev);
  444. u32 stcmd = readl(priv->base + IFI_CANFD_STCMD);
  445. int work_done = 0;
  446. if ((stcmd & IFI_CANFD_STCMD_ERROR_ACTIVE) &&
  447. (priv->can.state != CAN_STATE_ERROR_ACTIVE)) {
  448. netdev_dbg(ndev, "Error, entered active state\n");
  449. work_done += ifi_canfd_handle_state_change(ndev,
  450. CAN_STATE_ERROR_ACTIVE);
  451. }
  452. if ((stcmd & IFI_CANFD_STCMD_ERROR_WARNING) &&
  453. (priv->can.state != CAN_STATE_ERROR_WARNING)) {
  454. netdev_dbg(ndev, "Error, entered warning state\n");
  455. work_done += ifi_canfd_handle_state_change(ndev,
  456. CAN_STATE_ERROR_WARNING);
  457. }
  458. if ((stcmd & IFI_CANFD_STCMD_ERROR_PASSIVE) &&
  459. (priv->can.state != CAN_STATE_ERROR_PASSIVE)) {
  460. netdev_dbg(ndev, "Error, entered passive state\n");
  461. work_done += ifi_canfd_handle_state_change(ndev,
  462. CAN_STATE_ERROR_PASSIVE);
  463. }
  464. if ((stcmd & IFI_CANFD_STCMD_BUSOFF) &&
  465. (priv->can.state != CAN_STATE_BUS_OFF)) {
  466. netdev_dbg(ndev, "Error, entered bus-off state\n");
  467. work_done += ifi_canfd_handle_state_change(ndev,
  468. CAN_STATE_BUS_OFF);
  469. }
  470. return work_done;
  471. }
  472. static int ifi_canfd_poll(struct napi_struct *napi, int quota)
  473. {
  474. struct net_device *ndev = napi->dev;
  475. struct ifi_canfd_priv *priv = netdev_priv(ndev);
  476. u32 rxstcmd = readl(priv->base + IFI_CANFD_RXSTCMD);
  477. int work_done = 0;
  478. /* Handle bus state changes */
  479. work_done += ifi_canfd_handle_state_errors(ndev);
  480. /* Handle lost messages on RX */
  481. if (rxstcmd & IFI_CANFD_RXSTCMD_OVERFLOW)
  482. work_done += ifi_canfd_handle_lost_msg(ndev);
  483. /* Handle lec errors on the bus */
  484. if (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING)
  485. work_done += ifi_canfd_handle_lec_err(ndev);
  486. /* Handle normal messages on RX */
  487. if (!(rxstcmd & IFI_CANFD_RXSTCMD_EMPTY))
  488. work_done += ifi_canfd_do_rx_poll(ndev, quota - work_done);
  489. if (work_done < quota) {
  490. napi_complete_done(napi, work_done);
  491. ifi_canfd_irq_enable(ndev, 1);
  492. }
  493. return work_done;
  494. }
  495. static irqreturn_t ifi_canfd_isr(int irq, void *dev_id)
  496. {
  497. struct net_device *ndev = (struct net_device *)dev_id;
  498. struct ifi_canfd_priv *priv = netdev_priv(ndev);
  499. struct net_device_stats *stats = &ndev->stats;
  500. const u32 rx_irq_mask = IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY |
  501. IFI_CANFD_INTERRUPT_RXFIFO_NEMPTY_PER |
  502. IFI_CANFD_INTERRUPT_ERROR_COUNTER |
  503. IFI_CANFD_INTERRUPT_ERROR_STATE_CHG |
  504. IFI_CANFD_INTERRUPT_ERROR_WARNING |
  505. IFI_CANFD_INTERRUPT_ERROR_BUSOFF;
  506. const u32 tx_irq_mask = IFI_CANFD_INTERRUPT_TXFIFO_EMPTY |
  507. IFI_CANFD_INTERRUPT_TXFIFO_REMOVE;
  508. const u32 clr_irq_mask = ~((u32)IFI_CANFD_INTERRUPT_SET_IRQ);
  509. u32 isr;
  510. isr = readl(priv->base + IFI_CANFD_INTERRUPT);
  511. /* No interrupt */
  512. if (isr == 0)
  513. return IRQ_NONE;
  514. /* Clear all pending interrupts but ErrWarn */
  515. writel(clr_irq_mask, priv->base + IFI_CANFD_INTERRUPT);
  516. /* RX IRQ or bus warning, start NAPI */
  517. if (isr & rx_irq_mask) {
  518. ifi_canfd_irq_enable(ndev, 0);
  519. napi_schedule(&priv->napi);
  520. }
  521. /* TX IRQ */
  522. if (isr & IFI_CANFD_INTERRUPT_TXFIFO_REMOVE) {
  523. stats->tx_bytes += can_get_echo_skb(ndev, 0);
  524. stats->tx_packets++;
  525. can_led_event(ndev, CAN_LED_EVENT_TX);
  526. }
  527. if (isr & tx_irq_mask)
  528. netif_wake_queue(ndev);
  529. return IRQ_HANDLED;
  530. }
  531. static const struct can_bittiming_const ifi_canfd_bittiming_const = {
  532. .name = KBUILD_MODNAME,
  533. .tseg1_min = 1, /* Time segment 1 = prop_seg + phase_seg1 */
  534. .tseg1_max = 256,
  535. .tseg2_min = 2, /* Time segment 2 = phase_seg2 */
  536. .tseg2_max = 256,
  537. .sjw_max = 128,
  538. .brp_min = 2,
  539. .brp_max = 512,
  540. .brp_inc = 1,
  541. };
  542. static void ifi_canfd_set_bittiming(struct net_device *ndev)
  543. {
  544. struct ifi_canfd_priv *priv = netdev_priv(ndev);
  545. const struct can_bittiming *bt = &priv->can.bittiming;
  546. const struct can_bittiming *dbt = &priv->can.data_bittiming;
  547. u16 brp, sjw, tseg1, tseg2, tdc;
  548. /* Configure bit timing */
  549. brp = bt->brp - 2;
  550. sjw = bt->sjw - 1;
  551. tseg1 = bt->prop_seg + bt->phase_seg1 - 1;
  552. tseg2 = bt->phase_seg2 - 2;
  553. writel((tseg2 << IFI_CANFD_TIME_TIMEB_OFF) |
  554. (tseg1 << IFI_CANFD_TIME_TIMEA_OFF) |
  555. (brp << IFI_CANFD_TIME_PRESCALE_OFF) |
  556. (sjw << IFI_CANFD_TIME_SJW_OFF_7_9_8_8),
  557. priv->base + IFI_CANFD_TIME);
  558. /* Configure data bit timing */
  559. brp = dbt->brp - 2;
  560. sjw = dbt->sjw - 1;
  561. tseg1 = dbt->prop_seg + dbt->phase_seg1 - 1;
  562. tseg2 = dbt->phase_seg2 - 2;
  563. writel((tseg2 << IFI_CANFD_TIME_TIMEB_OFF) |
  564. (tseg1 << IFI_CANFD_TIME_TIMEA_OFF) |
  565. (brp << IFI_CANFD_TIME_PRESCALE_OFF) |
  566. (sjw << IFI_CANFD_TIME_SJW_OFF_7_9_8_8),
  567. priv->base + IFI_CANFD_FTIME);
  568. /* Configure transmitter delay */
  569. tdc = dbt->brp * (dbt->prop_seg + dbt->phase_seg1);
  570. tdc &= IFI_CANFD_TDELAY_MASK;
  571. writel(IFI_CANFD_TDELAY_EN | tdc, priv->base + IFI_CANFD_TDELAY);
  572. }
  573. static void ifi_canfd_set_filter(struct net_device *ndev, const u32 id,
  574. const u32 mask, const u32 ident)
  575. {
  576. struct ifi_canfd_priv *priv = netdev_priv(ndev);
  577. writel(mask, priv->base + IFI_CANFD_FILTER_MASK(id));
  578. writel(ident, priv->base + IFI_CANFD_FILTER_IDENT(id));
  579. }
  580. static void ifi_canfd_set_filters(struct net_device *ndev)
  581. {
  582. /* Receive all CAN frames (standard ID) */
  583. ifi_canfd_set_filter(ndev, 0,
  584. IFI_CANFD_FILTER_MASK_VALID |
  585. IFI_CANFD_FILTER_MASK_EXT,
  586. IFI_CANFD_FILTER_IDENT_VALID);
  587. /* Receive all CAN frames (extended ID) */
  588. ifi_canfd_set_filter(ndev, 1,
  589. IFI_CANFD_FILTER_MASK_VALID |
  590. IFI_CANFD_FILTER_MASK_EXT,
  591. IFI_CANFD_FILTER_IDENT_VALID |
  592. IFI_CANFD_FILTER_IDENT_IDE);
  593. /* Receive all CANFD frames */
  594. ifi_canfd_set_filter(ndev, 2,
  595. IFI_CANFD_FILTER_MASK_VALID |
  596. IFI_CANFD_FILTER_MASK_EDL |
  597. IFI_CANFD_FILTER_MASK_EXT,
  598. IFI_CANFD_FILTER_IDENT_VALID |
  599. IFI_CANFD_FILTER_IDENT_CANFD |
  600. IFI_CANFD_FILTER_IDENT_IDE);
  601. }
  602. static void ifi_canfd_start(struct net_device *ndev)
  603. {
  604. struct ifi_canfd_priv *priv = netdev_priv(ndev);
  605. u32 stcmd;
  606. /* Reset the IP */
  607. writel(IFI_CANFD_STCMD_HARDRESET, priv->base + IFI_CANFD_STCMD);
  608. writel(IFI_CANFD_STCMD_ENABLE_7_9_8_8_TIMING,
  609. priv->base + IFI_CANFD_STCMD);
  610. ifi_canfd_set_bittiming(ndev);
  611. ifi_canfd_set_filters(ndev);
  612. /* Reset FIFOs */
  613. writel(IFI_CANFD_RXSTCMD_RESET, priv->base + IFI_CANFD_RXSTCMD);
  614. writel(0, priv->base + IFI_CANFD_RXSTCMD);
  615. writel(IFI_CANFD_TXSTCMD_RESET, priv->base + IFI_CANFD_TXSTCMD);
  616. writel(0, priv->base + IFI_CANFD_TXSTCMD);
  617. /* Repeat transmission until successful */
  618. writel(0, priv->base + IFI_CANFD_REPEAT);
  619. writel(0, priv->base + IFI_CANFD_SUSPEND);
  620. /* Clear all pending interrupts */
  621. writel((u32)(~IFI_CANFD_INTERRUPT_SET_IRQ),
  622. priv->base + IFI_CANFD_INTERRUPT);
  623. stcmd = IFI_CANFD_STCMD_ENABLE | IFI_CANFD_STCMD_NORMAL_MODE |
  624. IFI_CANFD_STCMD_ENABLE_7_9_8_8_TIMING;
  625. if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
  626. stcmd |= IFI_CANFD_STCMD_BUSMONITOR;
  627. if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
  628. stcmd |= IFI_CANFD_STCMD_LOOPBACK;
  629. if ((priv->can.ctrlmode & CAN_CTRLMODE_FD) &&
  630. !(priv->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO))
  631. stcmd |= IFI_CANFD_STCMD_ENABLE_ISO;
  632. if (!(priv->can.ctrlmode & CAN_CTRLMODE_FD))
  633. stcmd |= IFI_CANFD_STCMD_DISABLE_CANFD;
  634. priv->can.state = CAN_STATE_ERROR_ACTIVE;
  635. ifi_canfd_irq_enable(ndev, 1);
  636. /* Unlock, reset and enable the error counter. */
  637. writel(IFI_CANFD_ERROR_CTR_UNLOCK_MAGIC,
  638. priv->base + IFI_CANFD_ERROR_CTR);
  639. writel(IFI_CANFD_ERROR_CTR_ER_RESET, priv->base + IFI_CANFD_ERROR_CTR);
  640. writel(IFI_CANFD_ERROR_CTR_ER_ENABLE, priv->base + IFI_CANFD_ERROR_CTR);
  641. /* Enable controller */
  642. writel(stcmd, priv->base + IFI_CANFD_STCMD);
  643. }
  644. static void ifi_canfd_stop(struct net_device *ndev)
  645. {
  646. struct ifi_canfd_priv *priv = netdev_priv(ndev);
  647. /* Reset and disable the error counter. */
  648. writel(IFI_CANFD_ERROR_CTR_ER_RESET, priv->base + IFI_CANFD_ERROR_CTR);
  649. writel(0, priv->base + IFI_CANFD_ERROR_CTR);
  650. /* Reset the IP */
  651. writel(IFI_CANFD_STCMD_HARDRESET, priv->base + IFI_CANFD_STCMD);
  652. /* Mask all interrupts */
  653. writel(~0, priv->base + IFI_CANFD_IRQMASK);
  654. /* Clear all pending interrupts */
  655. writel((u32)(~IFI_CANFD_INTERRUPT_SET_IRQ),
  656. priv->base + IFI_CANFD_INTERRUPT);
  657. /* Set the state as STOPPED */
  658. priv->can.state = CAN_STATE_STOPPED;
  659. }
  660. static int ifi_canfd_set_mode(struct net_device *ndev, enum can_mode mode)
  661. {
  662. switch (mode) {
  663. case CAN_MODE_START:
  664. ifi_canfd_start(ndev);
  665. netif_wake_queue(ndev);
  666. break;
  667. default:
  668. return -EOPNOTSUPP;
  669. }
  670. return 0;
  671. }
  672. static int ifi_canfd_open(struct net_device *ndev)
  673. {
  674. struct ifi_canfd_priv *priv = netdev_priv(ndev);
  675. int ret;
  676. ret = open_candev(ndev);
  677. if (ret) {
  678. netdev_err(ndev, "Failed to open CAN device\n");
  679. return ret;
  680. }
  681. /* Register interrupt handler */
  682. ret = request_irq(ndev->irq, ifi_canfd_isr, IRQF_SHARED,
  683. ndev->name, ndev);
  684. if (ret < 0) {
  685. netdev_err(ndev, "Failed to request interrupt\n");
  686. goto err_irq;
  687. }
  688. ifi_canfd_start(ndev);
  689. can_led_event(ndev, CAN_LED_EVENT_OPEN);
  690. napi_enable(&priv->napi);
  691. netif_start_queue(ndev);
  692. return 0;
  693. err_irq:
  694. close_candev(ndev);
  695. return ret;
  696. }
  697. static int ifi_canfd_close(struct net_device *ndev)
  698. {
  699. struct ifi_canfd_priv *priv = netdev_priv(ndev);
  700. netif_stop_queue(ndev);
  701. napi_disable(&priv->napi);
  702. ifi_canfd_stop(ndev);
  703. free_irq(ndev->irq, ndev);
  704. close_candev(ndev);
  705. can_led_event(ndev, CAN_LED_EVENT_STOP);
  706. return 0;
  707. }
  708. static netdev_tx_t ifi_canfd_start_xmit(struct sk_buff *skb,
  709. struct net_device *ndev)
  710. {
  711. struct ifi_canfd_priv *priv = netdev_priv(ndev);
  712. struct canfd_frame *cf = (struct canfd_frame *)skb->data;
  713. u32 txst, txid, txdlc;
  714. int i;
  715. if (can_dropped_invalid_skb(ndev, skb))
  716. return NETDEV_TX_OK;
  717. /* Check if the TX buffer is full */
  718. txst = readl(priv->base + IFI_CANFD_TXSTCMD);
  719. if (txst & IFI_CANFD_TXSTCMD_FULL) {
  720. netif_stop_queue(ndev);
  721. netdev_err(ndev, "BUG! TX FIFO full when queue awake!\n");
  722. return NETDEV_TX_BUSY;
  723. }
  724. netif_stop_queue(ndev);
  725. if (cf->can_id & CAN_EFF_FLAG) {
  726. txid = cf->can_id & CAN_EFF_MASK;
  727. /*
  728. * In case the Extended ID frame is transmitted, the
  729. * standard and extended part of the ID are swapped
  730. * in the register, so swap them back to send the
  731. * correct ID.
  732. */
  733. txid = (txid >> IFI_CANFD_TXFIFO_ID_ID_XTD_WIDTH) |
  734. ((txid & IFI_CANFD_TXFIFO_ID_ID_XTD_MASK) <<
  735. IFI_CANFD_TXFIFO_ID_ID_XTD_OFFSET);
  736. txid |= IFI_CANFD_TXFIFO_ID_IDE;
  737. } else {
  738. txid = cf->can_id & CAN_SFF_MASK;
  739. }
  740. txdlc = can_len2dlc(cf->len);
  741. if ((priv->can.ctrlmode & CAN_CTRLMODE_FD) && can_is_canfd_skb(skb)) {
  742. txdlc |= IFI_CANFD_TXFIFO_DLC_EDL;
  743. if (cf->flags & CANFD_BRS)
  744. txdlc |= IFI_CANFD_TXFIFO_DLC_BRS;
  745. }
  746. if (cf->can_id & CAN_RTR_FLAG)
  747. txdlc |= IFI_CANFD_TXFIFO_DLC_RTR;
  748. /* message ram configuration */
  749. writel(txid, priv->base + IFI_CANFD_TXFIFO_ID);
  750. writel(txdlc, priv->base + IFI_CANFD_TXFIFO_DLC);
  751. for (i = 0; i < cf->len; i += 4) {
  752. writel(*(u32 *)(cf->data + i),
  753. priv->base + IFI_CANFD_TXFIFO_DATA + i);
  754. }
  755. writel(0, priv->base + IFI_CANFD_TXFIFO_REPEATCOUNT);
  756. writel(0, priv->base + IFI_CANFD_TXFIFO_SUSPEND_US);
  757. can_put_echo_skb(skb, ndev, 0);
  758. /* Start the transmission */
  759. writel(IFI_CANFD_TXSTCMD_ADD_MSG, priv->base + IFI_CANFD_TXSTCMD);
  760. return NETDEV_TX_OK;
  761. }
  762. static const struct net_device_ops ifi_canfd_netdev_ops = {
  763. .ndo_open = ifi_canfd_open,
  764. .ndo_stop = ifi_canfd_close,
  765. .ndo_start_xmit = ifi_canfd_start_xmit,
  766. .ndo_change_mtu = can_change_mtu,
  767. };
  768. static int ifi_canfd_plat_probe(struct platform_device *pdev)
  769. {
  770. struct device *dev = &pdev->dev;
  771. struct net_device *ndev;
  772. struct ifi_canfd_priv *priv;
  773. struct resource *res;
  774. void __iomem *addr;
  775. int irq, ret;
  776. u32 id, rev;
  777. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  778. addr = devm_ioremap_resource(dev, res);
  779. irq = platform_get_irq(pdev, 0);
  780. if (IS_ERR(addr) || irq < 0)
  781. return -EINVAL;
  782. id = readl(addr + IFI_CANFD_IP_ID);
  783. if (id != IFI_CANFD_IP_ID_VALUE) {
  784. dev_err(dev, "This block is not IFI CANFD, id=%08x\n", id);
  785. return -EINVAL;
  786. }
  787. rev = readl(addr + IFI_CANFD_VER) & IFI_CANFD_VER_REV_MASK;
  788. if (rev < IFI_CANFD_VER_REV_MIN_SUPPORTED) {
  789. dev_err(dev, "This block is too old (rev %i), minimum supported is rev %i\n",
  790. rev, IFI_CANFD_VER_REV_MIN_SUPPORTED);
  791. return -EINVAL;
  792. }
  793. ndev = alloc_candev(sizeof(*priv), 1);
  794. if (!ndev)
  795. return -ENOMEM;
  796. ndev->irq = irq;
  797. ndev->flags |= IFF_ECHO; /* we support local echo */
  798. ndev->netdev_ops = &ifi_canfd_netdev_ops;
  799. priv = netdev_priv(ndev);
  800. priv->ndev = ndev;
  801. priv->base = addr;
  802. netif_napi_add(ndev, &priv->napi, ifi_canfd_poll, 64);
  803. priv->can.state = CAN_STATE_STOPPED;
  804. priv->can.clock.freq = readl(addr + IFI_CANFD_CANCLOCK);
  805. priv->can.bittiming_const = &ifi_canfd_bittiming_const;
  806. priv->can.data_bittiming_const = &ifi_canfd_bittiming_const;
  807. priv->can.do_set_mode = ifi_canfd_set_mode;
  808. priv->can.do_get_berr_counter = ifi_canfd_get_berr_counter;
  809. /* IFI CANFD can do both Bosch FD and ISO FD */
  810. priv->can.ctrlmode = CAN_CTRLMODE_FD;
  811. /* IFI CANFD can do both Bosch FD and ISO FD */
  812. priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
  813. CAN_CTRLMODE_LISTENONLY |
  814. CAN_CTRLMODE_FD |
  815. CAN_CTRLMODE_FD_NON_ISO |
  816. CAN_CTRLMODE_BERR_REPORTING;
  817. platform_set_drvdata(pdev, ndev);
  818. SET_NETDEV_DEV(ndev, dev);
  819. ret = register_candev(ndev);
  820. if (ret) {
  821. dev_err(dev, "Failed to register (ret=%d)\n", ret);
  822. goto err_reg;
  823. }
  824. devm_can_led_init(ndev);
  825. dev_info(dev, "Driver registered: regs=%p, irq=%d, clock=%d\n",
  826. priv->base, ndev->irq, priv->can.clock.freq);
  827. return 0;
  828. err_reg:
  829. free_candev(ndev);
  830. return ret;
  831. }
  832. static int ifi_canfd_plat_remove(struct platform_device *pdev)
  833. {
  834. struct net_device *ndev = platform_get_drvdata(pdev);
  835. unregister_candev(ndev);
  836. platform_set_drvdata(pdev, NULL);
  837. free_candev(ndev);
  838. return 0;
  839. }
  840. static const struct of_device_id ifi_canfd_of_table[] = {
  841. { .compatible = "ifi,canfd-1.0", .data = NULL },
  842. { /* sentinel */ },
  843. };
  844. MODULE_DEVICE_TABLE(of, ifi_canfd_of_table);
  845. static struct platform_driver ifi_canfd_plat_driver = {
  846. .driver = {
  847. .name = KBUILD_MODNAME,
  848. .of_match_table = ifi_canfd_of_table,
  849. },
  850. .probe = ifi_canfd_plat_probe,
  851. .remove = ifi_canfd_plat_remove,
  852. };
  853. module_platform_driver(ifi_canfd_plat_driver);
  854. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  855. MODULE_LICENSE("GPL v2");
  856. MODULE_DESCRIPTION("CAN bus driver for IFI CANFD controller");