kvaser_pciefd.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916
  1. // SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
  2. /* Copyright (C) 2018 KVASER AB, Sweden. All rights reserved.
  3. * Parts of this driver are based on the following:
  4. * - Kvaser linux pciefd driver (version 5.25)
  5. * - PEAK linux canfd driver
  6. * - Altera Avalon EPCS flash controller driver
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/device.h>
  11. #include <linux/pci.h>
  12. #include <linux/can/dev.h>
  13. #include <linux/timer.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/crc32.h>
  16. #include <linux/iopoll.h>
  17. MODULE_LICENSE("Dual BSD/GPL");
  18. MODULE_AUTHOR("Kvaser AB <support@kvaser.com>");
  19. MODULE_DESCRIPTION("CAN driver for Kvaser CAN/PCIe devices");
  20. #define KVASER_PCIEFD_DRV_NAME "kvaser_pciefd"
  21. #define KVASER_PCIEFD_WAIT_TIMEOUT msecs_to_jiffies(1000)
  22. #define KVASER_PCIEFD_BEC_POLL_FREQ (jiffies + msecs_to_jiffies(200))
  23. #define KVASER_PCIEFD_MAX_ERR_REP 256
  24. #define KVASER_PCIEFD_CAN_TX_MAX_COUNT 17
  25. #define KVASER_PCIEFD_MAX_CAN_CHANNELS 4
  26. #define KVASER_PCIEFD_DMA_COUNT 2
  27. #define KVASER_PCIEFD_DMA_SIZE (4 * 1024)
  28. #define KVASER_PCIEFD_64BIT_DMA_BIT BIT(0)
  29. #define KVASER_PCIEFD_VENDOR 0x1a07
  30. #define KVASER_PCIEFD_4HS_ID 0x0d
  31. #define KVASER_PCIEFD_2HS_ID 0x0e
  32. #define KVASER_PCIEFD_HS_ID 0x0f
  33. #define KVASER_PCIEFD_MINIPCIE_HS_ID 0x10
  34. #define KVASER_PCIEFD_MINIPCIE_2HS_ID 0x11
  35. /* PCIe IRQ registers */
  36. #define KVASER_PCIEFD_IRQ_REG 0x40
  37. #define KVASER_PCIEFD_IEN_REG 0x50
  38. /* DMA map */
  39. #define KVASER_PCIEFD_DMA_MAP_BASE 0x1000
  40. /* Kvaser KCAN CAN controller registers */
  41. #define KVASER_PCIEFD_KCAN0_BASE 0x10000
  42. #define KVASER_PCIEFD_KCAN_BASE_OFFSET 0x1000
  43. #define KVASER_PCIEFD_KCAN_FIFO_REG 0x100
  44. #define KVASER_PCIEFD_KCAN_FIFO_LAST_REG 0x180
  45. #define KVASER_PCIEFD_KCAN_CTRL_REG 0x2c0
  46. #define KVASER_PCIEFD_KCAN_CMD_REG 0x400
  47. #define KVASER_PCIEFD_KCAN_IEN_REG 0x408
  48. #define KVASER_PCIEFD_KCAN_IRQ_REG 0x410
  49. #define KVASER_PCIEFD_KCAN_TX_NPACKETS_REG 0x414
  50. #define KVASER_PCIEFD_KCAN_STAT_REG 0x418
  51. #define KVASER_PCIEFD_KCAN_MODE_REG 0x41c
  52. #define KVASER_PCIEFD_KCAN_BTRN_REG 0x420
  53. #define KVASER_PCIEFD_KCAN_BUS_LOAD_REG 0x424
  54. #define KVASER_PCIEFD_KCAN_BTRD_REG 0x428
  55. #define KVASER_PCIEFD_KCAN_PWM_REG 0x430
  56. /* Loopback control register */
  57. #define KVASER_PCIEFD_LOOP_REG 0x1f000
  58. /* System identification and information registers */
  59. #define KVASER_PCIEFD_SYSID_BASE 0x1f020
  60. #define KVASER_PCIEFD_SYSID_VERSION_REG (KVASER_PCIEFD_SYSID_BASE + 0x8)
  61. #define KVASER_PCIEFD_SYSID_CANFREQ_REG (KVASER_PCIEFD_SYSID_BASE + 0xc)
  62. #define KVASER_PCIEFD_SYSID_BUSFREQ_REG (KVASER_PCIEFD_SYSID_BASE + 0x10)
  63. #define KVASER_PCIEFD_SYSID_BUILD_REG (KVASER_PCIEFD_SYSID_BASE + 0x14)
  64. /* Shared receive buffer registers */
  65. #define KVASER_PCIEFD_SRB_BASE 0x1f200
  66. #define KVASER_PCIEFD_SRB_CMD_REG (KVASER_PCIEFD_SRB_BASE + 0x200)
  67. #define KVASER_PCIEFD_SRB_IEN_REG (KVASER_PCIEFD_SRB_BASE + 0x204)
  68. #define KVASER_PCIEFD_SRB_IRQ_REG (KVASER_PCIEFD_SRB_BASE + 0x20c)
  69. #define KVASER_PCIEFD_SRB_STAT_REG (KVASER_PCIEFD_SRB_BASE + 0x210)
  70. #define KVASER_PCIEFD_SRB_CTRL_REG (KVASER_PCIEFD_SRB_BASE + 0x218)
  71. /* EPCS flash controller registers */
  72. #define KVASER_PCIEFD_SPI_BASE 0x1fc00
  73. #define KVASER_PCIEFD_SPI_RX_REG KVASER_PCIEFD_SPI_BASE
  74. #define KVASER_PCIEFD_SPI_TX_REG (KVASER_PCIEFD_SPI_BASE + 0x4)
  75. #define KVASER_PCIEFD_SPI_STATUS_REG (KVASER_PCIEFD_SPI_BASE + 0x8)
  76. #define KVASER_PCIEFD_SPI_CTRL_REG (KVASER_PCIEFD_SPI_BASE + 0xc)
  77. #define KVASER_PCIEFD_SPI_SSEL_REG (KVASER_PCIEFD_SPI_BASE + 0x14)
  78. #define KVASER_PCIEFD_IRQ_ALL_MSK 0x1f
  79. #define KVASER_PCIEFD_IRQ_SRB BIT(4)
  80. #define KVASER_PCIEFD_SYSID_NRCHAN_SHIFT 24
  81. #define KVASER_PCIEFD_SYSID_MAJOR_VER_SHIFT 16
  82. #define KVASER_PCIEFD_SYSID_BUILD_VER_SHIFT 1
  83. /* Reset DMA buffer 0, 1 and FIFO offset */
  84. #define KVASER_PCIEFD_SRB_CMD_RDB0 BIT(4)
  85. #define KVASER_PCIEFD_SRB_CMD_RDB1 BIT(5)
  86. #define KVASER_PCIEFD_SRB_CMD_FOR BIT(0)
  87. /* DMA packet done, buffer 0 and 1 */
  88. #define KVASER_PCIEFD_SRB_IRQ_DPD0 BIT(8)
  89. #define KVASER_PCIEFD_SRB_IRQ_DPD1 BIT(9)
  90. /* DMA overflow, buffer 0 and 1 */
  91. #define KVASER_PCIEFD_SRB_IRQ_DOF0 BIT(10)
  92. #define KVASER_PCIEFD_SRB_IRQ_DOF1 BIT(11)
  93. /* DMA underflow, buffer 0 and 1 */
  94. #define KVASER_PCIEFD_SRB_IRQ_DUF0 BIT(12)
  95. #define KVASER_PCIEFD_SRB_IRQ_DUF1 BIT(13)
  96. /* DMA idle */
  97. #define KVASER_PCIEFD_SRB_STAT_DI BIT(15)
  98. /* DMA support */
  99. #define KVASER_PCIEFD_SRB_STAT_DMA BIT(24)
  100. /* DMA Enable */
  101. #define KVASER_PCIEFD_SRB_CTRL_DMA_ENABLE BIT(0)
  102. /* EPCS flash controller definitions */
  103. #define KVASER_PCIEFD_CFG_IMG_SZ (64 * 1024)
  104. #define KVASER_PCIEFD_CFG_IMG_OFFSET (31 * 65536L)
  105. #define KVASER_PCIEFD_CFG_MAX_PARAMS 256
  106. #define KVASER_PCIEFD_CFG_MAGIC 0xcafef00d
  107. #define KVASER_PCIEFD_CFG_PARAM_MAX_SZ 24
  108. #define KVASER_PCIEFD_CFG_SYS_VER 1
  109. #define KVASER_PCIEFD_CFG_PARAM_NR_CHAN 130
  110. #define KVASER_PCIEFD_SPI_TMT BIT(5)
  111. #define KVASER_PCIEFD_SPI_TRDY BIT(6)
  112. #define KVASER_PCIEFD_SPI_RRDY BIT(7)
  113. #define KVASER_PCIEFD_FLASH_ID_EPCS16 0x14
  114. /* Commands for controlling the onboard flash */
  115. #define KVASER_PCIEFD_FLASH_RES_CMD 0xab
  116. #define KVASER_PCIEFD_FLASH_READ_CMD 0x3
  117. #define KVASER_PCIEFD_FLASH_STATUS_CMD 0x5
  118. /* Kvaser KCAN definitions */
  119. #define KVASER_PCIEFD_KCAN_CTRL_EFLUSH (4 << 29)
  120. #define KVASER_PCIEFD_KCAN_CTRL_EFRAME (5 << 29)
  121. #define KVASER_PCIEFD_KCAN_CMD_SEQ_SHIFT 16
  122. /* Request status packet */
  123. #define KVASER_PCIEFD_KCAN_CMD_SRQ BIT(0)
  124. /* Abort, flush and reset */
  125. #define KVASER_PCIEFD_KCAN_CMD_AT BIT(1)
  126. /* Tx FIFO unaligned read */
  127. #define KVASER_PCIEFD_KCAN_IRQ_TAR BIT(0)
  128. /* Tx FIFO unaligned end */
  129. #define KVASER_PCIEFD_KCAN_IRQ_TAE BIT(1)
  130. /* Bus parameter protection error */
  131. #define KVASER_PCIEFD_KCAN_IRQ_BPP BIT(2)
  132. /* FDF bit when controller is in classic mode */
  133. #define KVASER_PCIEFD_KCAN_IRQ_FDIC BIT(3)
  134. /* Rx FIFO overflow */
  135. #define KVASER_PCIEFD_KCAN_IRQ_ROF BIT(5)
  136. /* Abort done */
  137. #define KVASER_PCIEFD_KCAN_IRQ_ABD BIT(13)
  138. /* Tx buffer flush done */
  139. #define KVASER_PCIEFD_KCAN_IRQ_TFD BIT(14)
  140. /* Tx FIFO overflow */
  141. #define KVASER_PCIEFD_KCAN_IRQ_TOF BIT(15)
  142. /* Tx FIFO empty */
  143. #define KVASER_PCIEFD_KCAN_IRQ_TE BIT(16)
  144. /* Transmitter unaligned */
  145. #define KVASER_PCIEFD_KCAN_IRQ_TAL BIT(17)
  146. #define KVASER_PCIEFD_KCAN_TX_NPACKETS_MAX_SHIFT 16
  147. #define KVASER_PCIEFD_KCAN_STAT_SEQNO_SHIFT 24
  148. /* Abort request */
  149. #define KVASER_PCIEFD_KCAN_STAT_AR BIT(7)
  150. /* Idle state. Controller in reset mode and no abort or flush pending */
  151. #define KVASER_PCIEFD_KCAN_STAT_IDLE BIT(10)
  152. /* Bus off */
  153. #define KVASER_PCIEFD_KCAN_STAT_BOFF BIT(11)
  154. /* Reset mode request */
  155. #define KVASER_PCIEFD_KCAN_STAT_RMR BIT(14)
  156. /* Controller in reset mode */
  157. #define KVASER_PCIEFD_KCAN_STAT_IRM BIT(15)
  158. /* Controller got one-shot capability */
  159. #define KVASER_PCIEFD_KCAN_STAT_CAP BIT(16)
  160. /* Controller got CAN FD capability */
  161. #define KVASER_PCIEFD_KCAN_STAT_FD BIT(19)
  162. #define KVASER_PCIEFD_KCAN_STAT_BUS_OFF_MSK (KVASER_PCIEFD_KCAN_STAT_AR | \
  163. KVASER_PCIEFD_KCAN_STAT_BOFF | KVASER_PCIEFD_KCAN_STAT_RMR | \
  164. KVASER_PCIEFD_KCAN_STAT_IRM)
  165. /* Reset mode */
  166. #define KVASER_PCIEFD_KCAN_MODE_RM BIT(8)
  167. /* Listen only mode */
  168. #define KVASER_PCIEFD_KCAN_MODE_LOM BIT(9)
  169. /* Error packet enable */
  170. #define KVASER_PCIEFD_KCAN_MODE_EPEN BIT(12)
  171. /* CAN FD non-ISO */
  172. #define KVASER_PCIEFD_KCAN_MODE_NIFDEN BIT(15)
  173. /* Acknowledgment packet type */
  174. #define KVASER_PCIEFD_KCAN_MODE_APT BIT(20)
  175. /* Active error flag enable. Clear to force error passive */
  176. #define KVASER_PCIEFD_KCAN_MODE_EEN BIT(23)
  177. /* Classic CAN mode */
  178. #define KVASER_PCIEFD_KCAN_MODE_CCM BIT(31)
  179. #define KVASER_PCIEFD_KCAN_BTRN_SJW_SHIFT 13
  180. #define KVASER_PCIEFD_KCAN_BTRN_TSEG1_SHIFT 17
  181. #define KVASER_PCIEFD_KCAN_BTRN_TSEG2_SHIFT 26
  182. #define KVASER_PCIEFD_KCAN_PWM_TOP_SHIFT 16
  183. /* Kvaser KCAN packet types */
  184. #define KVASER_PCIEFD_PACK_TYPE_DATA 0
  185. #define KVASER_PCIEFD_PACK_TYPE_ACK 1
  186. #define KVASER_PCIEFD_PACK_TYPE_TXRQ 2
  187. #define KVASER_PCIEFD_PACK_TYPE_ERROR 3
  188. #define KVASER_PCIEFD_PACK_TYPE_EFLUSH_ACK 4
  189. #define KVASER_PCIEFD_PACK_TYPE_EFRAME_ACK 5
  190. #define KVASER_PCIEFD_PACK_TYPE_ACK_DATA 6
  191. #define KVASER_PCIEFD_PACK_TYPE_STATUS 8
  192. #define KVASER_PCIEFD_PACK_TYPE_BUS_LOAD 9
  193. /* Kvaser KCAN packet common definitions */
  194. #define KVASER_PCIEFD_PACKET_SEQ_MSK 0xff
  195. #define KVASER_PCIEFD_PACKET_CHID_SHIFT 25
  196. #define KVASER_PCIEFD_PACKET_TYPE_SHIFT 28
  197. /* Kvaser KCAN TDATA and RDATA first word */
  198. #define KVASER_PCIEFD_RPACKET_IDE BIT(30)
  199. #define KVASER_PCIEFD_RPACKET_RTR BIT(29)
  200. /* Kvaser KCAN TDATA and RDATA second word */
  201. #define KVASER_PCIEFD_RPACKET_ESI BIT(13)
  202. #define KVASER_PCIEFD_RPACKET_BRS BIT(14)
  203. #define KVASER_PCIEFD_RPACKET_FDF BIT(15)
  204. #define KVASER_PCIEFD_RPACKET_DLC_SHIFT 8
  205. /* Kvaser KCAN TDATA second word */
  206. #define KVASER_PCIEFD_TPACKET_SMS BIT(16)
  207. #define KVASER_PCIEFD_TPACKET_AREQ BIT(31)
  208. /* Kvaser KCAN APACKET */
  209. #define KVASER_PCIEFD_APACKET_FLU BIT(8)
  210. #define KVASER_PCIEFD_APACKET_CT BIT(9)
  211. #define KVASER_PCIEFD_APACKET_ABL BIT(10)
  212. #define KVASER_PCIEFD_APACKET_NACK BIT(11)
  213. /* Kvaser KCAN SPACK first word */
  214. #define KVASER_PCIEFD_SPACK_RXERR_SHIFT 8
  215. #define KVASER_PCIEFD_SPACK_BOFF BIT(16)
  216. #define KVASER_PCIEFD_SPACK_IDET BIT(20)
  217. #define KVASER_PCIEFD_SPACK_IRM BIT(21)
  218. #define KVASER_PCIEFD_SPACK_RMCD BIT(22)
  219. /* Kvaser KCAN SPACK second word */
  220. #define KVASER_PCIEFD_SPACK_AUTO BIT(21)
  221. #define KVASER_PCIEFD_SPACK_EWLR BIT(23)
  222. #define KVASER_PCIEFD_SPACK_EPLR BIT(24)
  223. struct kvaser_pciefd;
  224. struct kvaser_pciefd_can {
  225. struct can_priv can;
  226. struct kvaser_pciefd *kv_pcie;
  227. void __iomem *reg_base;
  228. struct can_berr_counter bec;
  229. u8 cmd_seq;
  230. int err_rep_cnt;
  231. int echo_idx;
  232. spinlock_t lock; /* Locks sensitive registers (e.g. MODE) */
  233. spinlock_t echo_lock; /* Locks the message echo buffer */
  234. struct timer_list bec_poll_timer;
  235. struct completion start_comp, flush_comp;
  236. };
  237. struct kvaser_pciefd {
  238. struct pci_dev *pci;
  239. void __iomem *reg_base;
  240. struct kvaser_pciefd_can *can[KVASER_PCIEFD_MAX_CAN_CHANNELS];
  241. void *dma_data[KVASER_PCIEFD_DMA_COUNT];
  242. u8 nr_channels;
  243. u32 bus_freq;
  244. u32 freq;
  245. u32 freq_to_ticks_div;
  246. };
  247. struct kvaser_pciefd_rx_packet {
  248. u32 header[2];
  249. u64 timestamp;
  250. };
  251. struct kvaser_pciefd_tx_packet {
  252. u32 header[2];
  253. u8 data[64];
  254. };
  255. static const struct can_bittiming_const kvaser_pciefd_bittiming_const = {
  256. .name = KVASER_PCIEFD_DRV_NAME,
  257. .tseg1_min = 1,
  258. .tseg1_max = 512,
  259. .tseg2_min = 1,
  260. .tseg2_max = 32,
  261. .sjw_max = 16,
  262. .brp_min = 1,
  263. .brp_max = 8192,
  264. .brp_inc = 1,
  265. };
  266. struct kvaser_pciefd_cfg_param {
  267. __le32 magic;
  268. __le32 nr;
  269. __le32 len;
  270. u8 data[KVASER_PCIEFD_CFG_PARAM_MAX_SZ];
  271. };
  272. struct kvaser_pciefd_cfg_img {
  273. __le32 version;
  274. __le32 magic;
  275. __le32 crc;
  276. struct kvaser_pciefd_cfg_param params[KVASER_PCIEFD_CFG_MAX_PARAMS];
  277. };
  278. static struct pci_device_id kvaser_pciefd_id_table[] = {
  279. { PCI_DEVICE(KVASER_PCIEFD_VENDOR, KVASER_PCIEFD_4HS_ID), },
  280. { PCI_DEVICE(KVASER_PCIEFD_VENDOR, KVASER_PCIEFD_2HS_ID), },
  281. { PCI_DEVICE(KVASER_PCIEFD_VENDOR, KVASER_PCIEFD_HS_ID), },
  282. { PCI_DEVICE(KVASER_PCIEFD_VENDOR, KVASER_PCIEFD_MINIPCIE_HS_ID), },
  283. { PCI_DEVICE(KVASER_PCIEFD_VENDOR, KVASER_PCIEFD_MINIPCIE_2HS_ID), },
  284. { 0,},
  285. };
  286. MODULE_DEVICE_TABLE(pci, kvaser_pciefd_id_table);
  287. /* Onboard flash memory functions */
  288. static int kvaser_pciefd_spi_wait_loop(struct kvaser_pciefd *pcie, int msk)
  289. {
  290. u32 res;
  291. int ret;
  292. ret = readl_poll_timeout(pcie->reg_base + KVASER_PCIEFD_SPI_STATUS_REG,
  293. res, res & msk, 0, 10);
  294. return ret;
  295. }
  296. static int kvaser_pciefd_spi_cmd(struct kvaser_pciefd *pcie, const u8 *tx,
  297. u32 tx_len, u8 *rx, u32 rx_len)
  298. {
  299. int c;
  300. iowrite32(BIT(0), pcie->reg_base + KVASER_PCIEFD_SPI_SSEL_REG);
  301. iowrite32(BIT(10), pcie->reg_base + KVASER_PCIEFD_SPI_CTRL_REG);
  302. ioread32(pcie->reg_base + KVASER_PCIEFD_SPI_RX_REG);
  303. c = tx_len;
  304. while (c--) {
  305. if (kvaser_pciefd_spi_wait_loop(pcie, KVASER_PCIEFD_SPI_TRDY))
  306. return -EIO;
  307. iowrite32(*tx++, pcie->reg_base + KVASER_PCIEFD_SPI_TX_REG);
  308. if (kvaser_pciefd_spi_wait_loop(pcie, KVASER_PCIEFD_SPI_RRDY))
  309. return -EIO;
  310. ioread32(pcie->reg_base + KVASER_PCIEFD_SPI_RX_REG);
  311. }
  312. c = rx_len;
  313. while (c-- > 0) {
  314. if (kvaser_pciefd_spi_wait_loop(pcie, KVASER_PCIEFD_SPI_TRDY))
  315. return -EIO;
  316. iowrite32(0, pcie->reg_base + KVASER_PCIEFD_SPI_TX_REG);
  317. if (kvaser_pciefd_spi_wait_loop(pcie, KVASER_PCIEFD_SPI_RRDY))
  318. return -EIO;
  319. *rx++ = ioread32(pcie->reg_base + KVASER_PCIEFD_SPI_RX_REG);
  320. }
  321. if (kvaser_pciefd_spi_wait_loop(pcie, KVASER_PCIEFD_SPI_TMT))
  322. return -EIO;
  323. iowrite32(0, pcie->reg_base + KVASER_PCIEFD_SPI_CTRL_REG);
  324. if (c != -1) {
  325. dev_err(&pcie->pci->dev, "Flash SPI transfer failed\n");
  326. return -EIO;
  327. }
  328. return 0;
  329. }
  330. static int kvaser_pciefd_cfg_read_and_verify(struct kvaser_pciefd *pcie,
  331. struct kvaser_pciefd_cfg_img *img)
  332. {
  333. int offset = KVASER_PCIEFD_CFG_IMG_OFFSET;
  334. int res, crc;
  335. u8 *crc_buff;
  336. u8 cmd[] = {
  337. KVASER_PCIEFD_FLASH_READ_CMD,
  338. (u8)((offset >> 16) & 0xff),
  339. (u8)((offset >> 8) & 0xff),
  340. (u8)(offset & 0xff)
  341. };
  342. res = kvaser_pciefd_spi_cmd(pcie, cmd, ARRAY_SIZE(cmd), (u8 *)img,
  343. KVASER_PCIEFD_CFG_IMG_SZ);
  344. if (res)
  345. return res;
  346. crc_buff = (u8 *)img->params;
  347. if (le32_to_cpu(img->version) != KVASER_PCIEFD_CFG_SYS_VER) {
  348. dev_err(&pcie->pci->dev,
  349. "Config flash corrupted, version number is wrong\n");
  350. return -ENODEV;
  351. }
  352. if (le32_to_cpu(img->magic) != KVASER_PCIEFD_CFG_MAGIC) {
  353. dev_err(&pcie->pci->dev,
  354. "Config flash corrupted, magic number is wrong\n");
  355. return -ENODEV;
  356. }
  357. crc = ~crc32_be(0xffffffff, crc_buff, sizeof(img->params));
  358. if (le32_to_cpu(img->crc) != crc) {
  359. dev_err(&pcie->pci->dev,
  360. "Stored CRC does not match flash image contents\n");
  361. return -EIO;
  362. }
  363. return 0;
  364. }
  365. static void kvaser_pciefd_cfg_read_params(struct kvaser_pciefd *pcie,
  366. struct kvaser_pciefd_cfg_img *img)
  367. {
  368. struct kvaser_pciefd_cfg_param *param;
  369. param = &img->params[KVASER_PCIEFD_CFG_PARAM_NR_CHAN];
  370. memcpy(&pcie->nr_channels, param->data, le32_to_cpu(param->len));
  371. }
  372. static int kvaser_pciefd_read_cfg(struct kvaser_pciefd *pcie)
  373. {
  374. int res;
  375. struct kvaser_pciefd_cfg_img *img;
  376. /* Read electronic signature */
  377. u8 cmd[] = {KVASER_PCIEFD_FLASH_RES_CMD, 0, 0, 0};
  378. res = kvaser_pciefd_spi_cmd(pcie, cmd, ARRAY_SIZE(cmd), cmd, 1);
  379. if (res)
  380. return -EIO;
  381. img = kmalloc(KVASER_PCIEFD_CFG_IMG_SZ, GFP_KERNEL);
  382. if (!img)
  383. return -ENOMEM;
  384. if (cmd[0] != KVASER_PCIEFD_FLASH_ID_EPCS16) {
  385. dev_err(&pcie->pci->dev,
  386. "Flash id is 0x%x instead of expected EPCS16 (0x%x)\n",
  387. cmd[0], KVASER_PCIEFD_FLASH_ID_EPCS16);
  388. res = -ENODEV;
  389. goto image_free;
  390. }
  391. cmd[0] = KVASER_PCIEFD_FLASH_STATUS_CMD;
  392. res = kvaser_pciefd_spi_cmd(pcie, cmd, 1, cmd, 1);
  393. if (res) {
  394. goto image_free;
  395. } else if (cmd[0] & 1) {
  396. res = -EIO;
  397. /* No write is ever done, the WIP should never be set */
  398. dev_err(&pcie->pci->dev, "Unexpected WIP bit set in flash\n");
  399. goto image_free;
  400. }
  401. res = kvaser_pciefd_cfg_read_and_verify(pcie, img);
  402. if (res) {
  403. res = -EIO;
  404. goto image_free;
  405. }
  406. kvaser_pciefd_cfg_read_params(pcie, img);
  407. image_free:
  408. kfree(img);
  409. return res;
  410. }
  411. static void kvaser_pciefd_request_status(struct kvaser_pciefd_can *can)
  412. {
  413. u32 cmd;
  414. cmd = KVASER_PCIEFD_KCAN_CMD_SRQ;
  415. cmd |= ++can->cmd_seq << KVASER_PCIEFD_KCAN_CMD_SEQ_SHIFT;
  416. iowrite32(cmd, can->reg_base + KVASER_PCIEFD_KCAN_CMD_REG);
  417. }
  418. static void kvaser_pciefd_enable_err_gen(struct kvaser_pciefd_can *can)
  419. {
  420. u32 mode;
  421. unsigned long irq;
  422. spin_lock_irqsave(&can->lock, irq);
  423. mode = ioread32(can->reg_base + KVASER_PCIEFD_KCAN_MODE_REG);
  424. if (!(mode & KVASER_PCIEFD_KCAN_MODE_EPEN)) {
  425. mode |= KVASER_PCIEFD_KCAN_MODE_EPEN;
  426. iowrite32(mode, can->reg_base + KVASER_PCIEFD_KCAN_MODE_REG);
  427. }
  428. spin_unlock_irqrestore(&can->lock, irq);
  429. }
  430. static void kvaser_pciefd_disable_err_gen(struct kvaser_pciefd_can *can)
  431. {
  432. u32 mode;
  433. unsigned long irq;
  434. spin_lock_irqsave(&can->lock, irq);
  435. mode = ioread32(can->reg_base + KVASER_PCIEFD_KCAN_MODE_REG);
  436. mode &= ~KVASER_PCIEFD_KCAN_MODE_EPEN;
  437. iowrite32(mode, can->reg_base + KVASER_PCIEFD_KCAN_MODE_REG);
  438. spin_unlock_irqrestore(&can->lock, irq);
  439. }
  440. static int kvaser_pciefd_set_tx_irq(struct kvaser_pciefd_can *can)
  441. {
  442. u32 msk;
  443. msk = KVASER_PCIEFD_KCAN_IRQ_TE | KVASER_PCIEFD_KCAN_IRQ_ROF |
  444. KVASER_PCIEFD_KCAN_IRQ_TOF | KVASER_PCIEFD_KCAN_IRQ_ABD |
  445. KVASER_PCIEFD_KCAN_IRQ_TAE | KVASER_PCIEFD_KCAN_IRQ_TAL |
  446. KVASER_PCIEFD_KCAN_IRQ_FDIC | KVASER_PCIEFD_KCAN_IRQ_BPP |
  447. KVASER_PCIEFD_KCAN_IRQ_TAR | KVASER_PCIEFD_KCAN_IRQ_TFD;
  448. iowrite32(msk, can->reg_base + KVASER_PCIEFD_KCAN_IEN_REG);
  449. return 0;
  450. }
  451. static void kvaser_pciefd_setup_controller(struct kvaser_pciefd_can *can)
  452. {
  453. u32 mode;
  454. unsigned long irq;
  455. spin_lock_irqsave(&can->lock, irq);
  456. mode = ioread32(can->reg_base + KVASER_PCIEFD_KCAN_MODE_REG);
  457. if (can->can.ctrlmode & CAN_CTRLMODE_FD) {
  458. mode &= ~KVASER_PCIEFD_KCAN_MODE_CCM;
  459. if (can->can.ctrlmode & CAN_CTRLMODE_FD_NON_ISO)
  460. mode |= KVASER_PCIEFD_KCAN_MODE_NIFDEN;
  461. else
  462. mode &= ~KVASER_PCIEFD_KCAN_MODE_NIFDEN;
  463. } else {
  464. mode |= KVASER_PCIEFD_KCAN_MODE_CCM;
  465. mode &= ~KVASER_PCIEFD_KCAN_MODE_NIFDEN;
  466. }
  467. if (can->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)
  468. mode |= KVASER_PCIEFD_KCAN_MODE_LOM;
  469. mode |= KVASER_PCIEFD_KCAN_MODE_EEN;
  470. mode |= KVASER_PCIEFD_KCAN_MODE_EPEN;
  471. /* Use ACK packet type */
  472. mode &= ~KVASER_PCIEFD_KCAN_MODE_APT;
  473. mode &= ~KVASER_PCIEFD_KCAN_MODE_RM;
  474. iowrite32(mode, can->reg_base + KVASER_PCIEFD_KCAN_MODE_REG);
  475. spin_unlock_irqrestore(&can->lock, irq);
  476. }
  477. static void kvaser_pciefd_start_controller_flush(struct kvaser_pciefd_can *can)
  478. {
  479. u32 status;
  480. unsigned long irq;
  481. spin_lock_irqsave(&can->lock, irq);
  482. iowrite32(-1, can->reg_base + KVASER_PCIEFD_KCAN_IRQ_REG);
  483. iowrite32(KVASER_PCIEFD_KCAN_IRQ_ABD | KVASER_PCIEFD_KCAN_IRQ_TFD,
  484. can->reg_base + KVASER_PCIEFD_KCAN_IEN_REG);
  485. status = ioread32(can->reg_base + KVASER_PCIEFD_KCAN_STAT_REG);
  486. if (status & KVASER_PCIEFD_KCAN_STAT_IDLE) {
  487. u32 cmd;
  488. /* If controller is already idle, run abort, flush and reset */
  489. cmd = KVASER_PCIEFD_KCAN_CMD_AT;
  490. cmd |= ++can->cmd_seq << KVASER_PCIEFD_KCAN_CMD_SEQ_SHIFT;
  491. iowrite32(cmd, can->reg_base + KVASER_PCIEFD_KCAN_CMD_REG);
  492. } else if (!(status & KVASER_PCIEFD_KCAN_STAT_RMR)) {
  493. u32 mode;
  494. /* Put controller in reset mode */
  495. mode = ioread32(can->reg_base + KVASER_PCIEFD_KCAN_MODE_REG);
  496. mode |= KVASER_PCIEFD_KCAN_MODE_RM;
  497. iowrite32(mode, can->reg_base + KVASER_PCIEFD_KCAN_MODE_REG);
  498. }
  499. spin_unlock_irqrestore(&can->lock, irq);
  500. }
  501. static int kvaser_pciefd_bus_on(struct kvaser_pciefd_can *can)
  502. {
  503. u32 mode;
  504. unsigned long irq;
  505. del_timer(&can->bec_poll_timer);
  506. if (!completion_done(&can->flush_comp))
  507. kvaser_pciefd_start_controller_flush(can);
  508. if (!wait_for_completion_timeout(&can->flush_comp,
  509. KVASER_PCIEFD_WAIT_TIMEOUT)) {
  510. netdev_err(can->can.dev, "Timeout during bus on flush\n");
  511. return -ETIMEDOUT;
  512. }
  513. spin_lock_irqsave(&can->lock, irq);
  514. iowrite32(0, can->reg_base + KVASER_PCIEFD_KCAN_IEN_REG);
  515. iowrite32(-1, can->reg_base + KVASER_PCIEFD_KCAN_IRQ_REG);
  516. iowrite32(KVASER_PCIEFD_KCAN_IRQ_ABD | KVASER_PCIEFD_KCAN_IRQ_TFD,
  517. can->reg_base + KVASER_PCIEFD_KCAN_IEN_REG);
  518. mode = ioread32(can->reg_base + KVASER_PCIEFD_KCAN_MODE_REG);
  519. mode &= ~KVASER_PCIEFD_KCAN_MODE_RM;
  520. iowrite32(mode, can->reg_base + KVASER_PCIEFD_KCAN_MODE_REG);
  521. spin_unlock_irqrestore(&can->lock, irq);
  522. if (!wait_for_completion_timeout(&can->start_comp,
  523. KVASER_PCIEFD_WAIT_TIMEOUT)) {
  524. netdev_err(can->can.dev, "Timeout during bus on reset\n");
  525. return -ETIMEDOUT;
  526. }
  527. /* Reset interrupt handling */
  528. iowrite32(0, can->reg_base + KVASER_PCIEFD_KCAN_IEN_REG);
  529. iowrite32(-1, can->reg_base + KVASER_PCIEFD_KCAN_IRQ_REG);
  530. kvaser_pciefd_set_tx_irq(can);
  531. kvaser_pciefd_setup_controller(can);
  532. can->can.state = CAN_STATE_ERROR_ACTIVE;
  533. netif_wake_queue(can->can.dev);
  534. can->bec.txerr = 0;
  535. can->bec.rxerr = 0;
  536. can->err_rep_cnt = 0;
  537. return 0;
  538. }
  539. static void kvaser_pciefd_pwm_stop(struct kvaser_pciefd_can *can)
  540. {
  541. u8 top;
  542. u32 pwm_ctrl;
  543. unsigned long irq;
  544. spin_lock_irqsave(&can->lock, irq);
  545. pwm_ctrl = ioread32(can->reg_base + KVASER_PCIEFD_KCAN_PWM_REG);
  546. top = (pwm_ctrl >> KVASER_PCIEFD_KCAN_PWM_TOP_SHIFT) & 0xff;
  547. /* Set duty cycle to zero */
  548. pwm_ctrl |= top;
  549. iowrite32(pwm_ctrl, can->reg_base + KVASER_PCIEFD_KCAN_PWM_REG);
  550. spin_unlock_irqrestore(&can->lock, irq);
  551. }
  552. static void kvaser_pciefd_pwm_start(struct kvaser_pciefd_can *can)
  553. {
  554. int top, trigger;
  555. u32 pwm_ctrl;
  556. unsigned long irq;
  557. kvaser_pciefd_pwm_stop(can);
  558. spin_lock_irqsave(&can->lock, irq);
  559. /* Set frequency to 500 KHz*/
  560. top = can->kv_pcie->bus_freq / (2 * 500000) - 1;
  561. pwm_ctrl = top & 0xff;
  562. pwm_ctrl |= (top & 0xff) << KVASER_PCIEFD_KCAN_PWM_TOP_SHIFT;
  563. iowrite32(pwm_ctrl, can->reg_base + KVASER_PCIEFD_KCAN_PWM_REG);
  564. /* Set duty cycle to 95 */
  565. trigger = (100 * top - 95 * (top + 1) + 50) / 100;
  566. pwm_ctrl = trigger & 0xff;
  567. pwm_ctrl |= (top & 0xff) << KVASER_PCIEFD_KCAN_PWM_TOP_SHIFT;
  568. iowrite32(pwm_ctrl, can->reg_base + KVASER_PCIEFD_KCAN_PWM_REG);
  569. spin_unlock_irqrestore(&can->lock, irq);
  570. }
  571. static int kvaser_pciefd_open(struct net_device *netdev)
  572. {
  573. int err;
  574. struct kvaser_pciefd_can *can = netdev_priv(netdev);
  575. err = open_candev(netdev);
  576. if (err)
  577. return err;
  578. err = kvaser_pciefd_bus_on(can);
  579. if (err)
  580. return err;
  581. return 0;
  582. }
  583. static int kvaser_pciefd_stop(struct net_device *netdev)
  584. {
  585. struct kvaser_pciefd_can *can = netdev_priv(netdev);
  586. int ret = 0;
  587. /* Don't interrupt ongoing flush */
  588. if (!completion_done(&can->flush_comp))
  589. kvaser_pciefd_start_controller_flush(can);
  590. if (!wait_for_completion_timeout(&can->flush_comp,
  591. KVASER_PCIEFD_WAIT_TIMEOUT)) {
  592. netdev_err(can->can.dev, "Timeout during stop\n");
  593. ret = -ETIMEDOUT;
  594. } else {
  595. iowrite32(0, can->reg_base + KVASER_PCIEFD_KCAN_IEN_REG);
  596. del_timer(&can->bec_poll_timer);
  597. }
  598. close_candev(netdev);
  599. return ret;
  600. }
  601. static int kvaser_pciefd_prepare_tx_packet(struct kvaser_pciefd_tx_packet *p,
  602. struct kvaser_pciefd_can *can,
  603. struct sk_buff *skb)
  604. {
  605. struct canfd_frame *cf = (struct canfd_frame *)skb->data;
  606. int packet_size;
  607. int seq = can->echo_idx;
  608. memset(p, 0, sizeof(*p));
  609. if (can->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT)
  610. p->header[1] |= KVASER_PCIEFD_TPACKET_SMS;
  611. if (cf->can_id & CAN_RTR_FLAG)
  612. p->header[0] |= KVASER_PCIEFD_RPACKET_RTR;
  613. if (cf->can_id & CAN_EFF_FLAG)
  614. p->header[0] |= KVASER_PCIEFD_RPACKET_IDE;
  615. p->header[0] |= cf->can_id & CAN_EFF_MASK;
  616. p->header[1] |= can_len2dlc(cf->len) << KVASER_PCIEFD_RPACKET_DLC_SHIFT;
  617. p->header[1] |= KVASER_PCIEFD_TPACKET_AREQ;
  618. if (can_is_canfd_skb(skb)) {
  619. p->header[1] |= KVASER_PCIEFD_RPACKET_FDF;
  620. if (cf->flags & CANFD_BRS)
  621. p->header[1] |= KVASER_PCIEFD_RPACKET_BRS;
  622. if (cf->flags & CANFD_ESI)
  623. p->header[1] |= KVASER_PCIEFD_RPACKET_ESI;
  624. }
  625. p->header[1] |= seq & KVASER_PCIEFD_PACKET_SEQ_MSK;
  626. packet_size = cf->len;
  627. memcpy(p->data, cf->data, packet_size);
  628. return DIV_ROUND_UP(packet_size, 4);
  629. }
  630. static netdev_tx_t kvaser_pciefd_start_xmit(struct sk_buff *skb,
  631. struct net_device *netdev)
  632. {
  633. struct kvaser_pciefd_can *can = netdev_priv(netdev);
  634. unsigned long irq_flags;
  635. struct kvaser_pciefd_tx_packet packet;
  636. int nwords;
  637. u8 count;
  638. if (can_dropped_invalid_skb(netdev, skb))
  639. return NETDEV_TX_OK;
  640. nwords = kvaser_pciefd_prepare_tx_packet(&packet, can, skb);
  641. spin_lock_irqsave(&can->echo_lock, irq_flags);
  642. /* Prepare and save echo skb in internal slot */
  643. can_put_echo_skb(skb, netdev, can->echo_idx);
  644. /* Move echo index to the next slot */
  645. can->echo_idx = (can->echo_idx + 1) % can->can.echo_skb_max;
  646. /* Write header to fifo */
  647. iowrite32(packet.header[0],
  648. can->reg_base + KVASER_PCIEFD_KCAN_FIFO_REG);
  649. iowrite32(packet.header[1],
  650. can->reg_base + KVASER_PCIEFD_KCAN_FIFO_REG);
  651. if (nwords) {
  652. u32 data_last = ((u32 *)packet.data)[nwords - 1];
  653. /* Write data to fifo, except last word */
  654. iowrite32_rep(can->reg_base +
  655. KVASER_PCIEFD_KCAN_FIFO_REG, packet.data,
  656. nwords - 1);
  657. /* Write last word to end of fifo */
  658. __raw_writel(data_last, can->reg_base +
  659. KVASER_PCIEFD_KCAN_FIFO_LAST_REG);
  660. } else {
  661. /* Complete write to fifo */
  662. __raw_writel(0, can->reg_base +
  663. KVASER_PCIEFD_KCAN_FIFO_LAST_REG);
  664. }
  665. count = ioread32(can->reg_base + KVASER_PCIEFD_KCAN_TX_NPACKETS_REG);
  666. /* No room for a new message, stop the queue until at least one
  667. * successful transmit
  668. */
  669. if (count >= KVASER_PCIEFD_CAN_TX_MAX_COUNT ||
  670. can->can.echo_skb[can->echo_idx])
  671. netif_stop_queue(netdev);
  672. spin_unlock_irqrestore(&can->echo_lock, irq_flags);
  673. return NETDEV_TX_OK;
  674. }
  675. static int kvaser_pciefd_set_bittiming(struct kvaser_pciefd_can *can, bool data)
  676. {
  677. u32 mode, test, btrn;
  678. unsigned long irq_flags;
  679. int ret;
  680. struct can_bittiming *bt;
  681. if (data)
  682. bt = &can->can.data_bittiming;
  683. else
  684. bt = &can->can.bittiming;
  685. btrn = ((bt->phase_seg2 - 1) & 0x1f) <<
  686. KVASER_PCIEFD_KCAN_BTRN_TSEG2_SHIFT |
  687. (((bt->prop_seg + bt->phase_seg1) - 1) & 0x1ff) <<
  688. KVASER_PCIEFD_KCAN_BTRN_TSEG1_SHIFT |
  689. ((bt->sjw - 1) & 0xf) << KVASER_PCIEFD_KCAN_BTRN_SJW_SHIFT |
  690. ((bt->brp - 1) & 0x1fff);
  691. spin_lock_irqsave(&can->lock, irq_flags);
  692. mode = ioread32(can->reg_base + KVASER_PCIEFD_KCAN_MODE_REG);
  693. /* Put the circuit in reset mode */
  694. iowrite32(mode | KVASER_PCIEFD_KCAN_MODE_RM,
  695. can->reg_base + KVASER_PCIEFD_KCAN_MODE_REG);
  696. /* Can only set bittiming if in reset mode */
  697. ret = readl_poll_timeout(can->reg_base + KVASER_PCIEFD_KCAN_MODE_REG,
  698. test, test & KVASER_PCIEFD_KCAN_MODE_RM,
  699. 0, 10);
  700. if (ret) {
  701. spin_unlock_irqrestore(&can->lock, irq_flags);
  702. return -EBUSY;
  703. }
  704. if (data)
  705. iowrite32(btrn, can->reg_base + KVASER_PCIEFD_KCAN_BTRD_REG);
  706. else
  707. iowrite32(btrn, can->reg_base + KVASER_PCIEFD_KCAN_BTRN_REG);
  708. /* Restore previous reset mode status */
  709. iowrite32(mode, can->reg_base + KVASER_PCIEFD_KCAN_MODE_REG);
  710. spin_unlock_irqrestore(&can->lock, irq_flags);
  711. return 0;
  712. }
  713. static int kvaser_pciefd_set_nominal_bittiming(struct net_device *ndev)
  714. {
  715. return kvaser_pciefd_set_bittiming(netdev_priv(ndev), false);
  716. }
  717. static int kvaser_pciefd_set_data_bittiming(struct net_device *ndev)
  718. {
  719. return kvaser_pciefd_set_bittiming(netdev_priv(ndev), true);
  720. }
  721. static int kvaser_pciefd_set_mode(struct net_device *ndev, enum can_mode mode)
  722. {
  723. struct kvaser_pciefd_can *can = netdev_priv(ndev);
  724. int ret = 0;
  725. switch (mode) {
  726. case CAN_MODE_START:
  727. if (!can->can.restart_ms)
  728. ret = kvaser_pciefd_bus_on(can);
  729. break;
  730. default:
  731. return -EOPNOTSUPP;
  732. }
  733. return ret;
  734. }
  735. static int kvaser_pciefd_get_berr_counter(const struct net_device *ndev,
  736. struct can_berr_counter *bec)
  737. {
  738. struct kvaser_pciefd_can *can = netdev_priv(ndev);
  739. bec->rxerr = can->bec.rxerr;
  740. bec->txerr = can->bec.txerr;
  741. return 0;
  742. }
  743. static void kvaser_pciefd_bec_poll_timer(struct timer_list *data)
  744. {
  745. struct kvaser_pciefd_can *can = from_timer(can, data, bec_poll_timer);
  746. kvaser_pciefd_enable_err_gen(can);
  747. kvaser_pciefd_request_status(can);
  748. can->err_rep_cnt = 0;
  749. }
  750. static const struct net_device_ops kvaser_pciefd_netdev_ops = {
  751. .ndo_open = kvaser_pciefd_open,
  752. .ndo_stop = kvaser_pciefd_stop,
  753. .ndo_start_xmit = kvaser_pciefd_start_xmit,
  754. .ndo_change_mtu = can_change_mtu,
  755. };
  756. static int kvaser_pciefd_setup_can_ctrls(struct kvaser_pciefd *pcie)
  757. {
  758. int i;
  759. for (i = 0; i < pcie->nr_channels; i++) {
  760. struct net_device *netdev;
  761. struct kvaser_pciefd_can *can;
  762. u32 status, tx_npackets;
  763. netdev = alloc_candev(sizeof(struct kvaser_pciefd_can),
  764. KVASER_PCIEFD_CAN_TX_MAX_COUNT);
  765. if (!netdev)
  766. return -ENOMEM;
  767. can = netdev_priv(netdev);
  768. netdev->netdev_ops = &kvaser_pciefd_netdev_ops;
  769. can->reg_base = pcie->reg_base + KVASER_PCIEFD_KCAN0_BASE +
  770. i * KVASER_PCIEFD_KCAN_BASE_OFFSET;
  771. can->kv_pcie = pcie;
  772. can->cmd_seq = 0;
  773. can->err_rep_cnt = 0;
  774. can->bec.txerr = 0;
  775. can->bec.rxerr = 0;
  776. init_completion(&can->start_comp);
  777. init_completion(&can->flush_comp);
  778. timer_setup(&can->bec_poll_timer, kvaser_pciefd_bec_poll_timer,
  779. 0);
  780. /* Disable Bus load reporting */
  781. iowrite32(0, can->reg_base + KVASER_PCIEFD_KCAN_BUS_LOAD_REG);
  782. tx_npackets = ioread32(can->reg_base +
  783. KVASER_PCIEFD_KCAN_TX_NPACKETS_REG);
  784. if (((tx_npackets >> KVASER_PCIEFD_KCAN_TX_NPACKETS_MAX_SHIFT) &
  785. 0xff) < KVASER_PCIEFD_CAN_TX_MAX_COUNT) {
  786. dev_err(&pcie->pci->dev,
  787. "Max Tx count is smaller than expected\n");
  788. free_candev(netdev);
  789. return -ENODEV;
  790. }
  791. can->can.clock.freq = pcie->freq;
  792. can->can.echo_skb_max = KVASER_PCIEFD_CAN_TX_MAX_COUNT;
  793. can->echo_idx = 0;
  794. spin_lock_init(&can->echo_lock);
  795. spin_lock_init(&can->lock);
  796. can->can.bittiming_const = &kvaser_pciefd_bittiming_const;
  797. can->can.data_bittiming_const = &kvaser_pciefd_bittiming_const;
  798. can->can.do_set_bittiming = kvaser_pciefd_set_nominal_bittiming;
  799. can->can.do_set_data_bittiming =
  800. kvaser_pciefd_set_data_bittiming;
  801. can->can.do_set_mode = kvaser_pciefd_set_mode;
  802. can->can.do_get_berr_counter = kvaser_pciefd_get_berr_counter;
  803. can->can.ctrlmode_supported = CAN_CTRLMODE_LISTENONLY |
  804. CAN_CTRLMODE_FD |
  805. CAN_CTRLMODE_FD_NON_ISO;
  806. status = ioread32(can->reg_base + KVASER_PCIEFD_KCAN_STAT_REG);
  807. if (!(status & KVASER_PCIEFD_KCAN_STAT_FD)) {
  808. dev_err(&pcie->pci->dev,
  809. "CAN FD not supported as expected %d\n", i);
  810. free_candev(netdev);
  811. return -ENODEV;
  812. }
  813. if (status & KVASER_PCIEFD_KCAN_STAT_CAP)
  814. can->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT;
  815. netdev->flags |= IFF_ECHO;
  816. SET_NETDEV_DEV(netdev, &pcie->pci->dev);
  817. iowrite32(-1, can->reg_base + KVASER_PCIEFD_KCAN_IRQ_REG);
  818. iowrite32(KVASER_PCIEFD_KCAN_IRQ_ABD |
  819. KVASER_PCIEFD_KCAN_IRQ_TFD,
  820. can->reg_base + KVASER_PCIEFD_KCAN_IEN_REG);
  821. pcie->can[i] = can;
  822. kvaser_pciefd_pwm_start(can);
  823. }
  824. return 0;
  825. }
  826. static int kvaser_pciefd_reg_candev(struct kvaser_pciefd *pcie)
  827. {
  828. int i;
  829. for (i = 0; i < pcie->nr_channels; i++) {
  830. int err = register_candev(pcie->can[i]->can.dev);
  831. if (err) {
  832. int j;
  833. /* Unregister all successfully registered devices. */
  834. for (j = 0; j < i; j++)
  835. unregister_candev(pcie->can[j]->can.dev);
  836. return err;
  837. }
  838. }
  839. return 0;
  840. }
  841. static void kvaser_pciefd_write_dma_map(struct kvaser_pciefd *pcie,
  842. dma_addr_t addr, int offset)
  843. {
  844. u32 word1, word2;
  845. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  846. word1 = addr | KVASER_PCIEFD_64BIT_DMA_BIT;
  847. word2 = addr >> 32;
  848. #else
  849. word1 = addr;
  850. word2 = 0;
  851. #endif
  852. iowrite32(word1, pcie->reg_base + offset);
  853. iowrite32(word2, pcie->reg_base + offset + 4);
  854. }
  855. static int kvaser_pciefd_setup_dma(struct kvaser_pciefd *pcie)
  856. {
  857. int i;
  858. u32 srb_status;
  859. dma_addr_t dma_addr[KVASER_PCIEFD_DMA_COUNT];
  860. /* Disable the DMA */
  861. iowrite32(0, pcie->reg_base + KVASER_PCIEFD_SRB_CTRL_REG);
  862. for (i = 0; i < KVASER_PCIEFD_DMA_COUNT; i++) {
  863. unsigned int offset = KVASER_PCIEFD_DMA_MAP_BASE + 8 * i;
  864. pcie->dma_data[i] =
  865. dmam_alloc_coherent(&pcie->pci->dev,
  866. KVASER_PCIEFD_DMA_SIZE,
  867. &dma_addr[i],
  868. GFP_KERNEL);
  869. if (!pcie->dma_data[i] || !dma_addr[i]) {
  870. dev_err(&pcie->pci->dev, "Rx dma_alloc(%u) failure\n",
  871. KVASER_PCIEFD_DMA_SIZE);
  872. return -ENOMEM;
  873. }
  874. kvaser_pciefd_write_dma_map(pcie, dma_addr[i], offset);
  875. }
  876. /* Reset Rx FIFO, and both DMA buffers */
  877. iowrite32(KVASER_PCIEFD_SRB_CMD_FOR | KVASER_PCIEFD_SRB_CMD_RDB0 |
  878. KVASER_PCIEFD_SRB_CMD_RDB1,
  879. pcie->reg_base + KVASER_PCIEFD_SRB_CMD_REG);
  880. srb_status = ioread32(pcie->reg_base + KVASER_PCIEFD_SRB_STAT_REG);
  881. if (!(srb_status & KVASER_PCIEFD_SRB_STAT_DI)) {
  882. dev_err(&pcie->pci->dev, "DMA not idle before enabling\n");
  883. return -EIO;
  884. }
  885. /* Enable the DMA */
  886. iowrite32(KVASER_PCIEFD_SRB_CTRL_DMA_ENABLE,
  887. pcie->reg_base + KVASER_PCIEFD_SRB_CTRL_REG);
  888. return 0;
  889. }
  890. static int kvaser_pciefd_setup_board(struct kvaser_pciefd *pcie)
  891. {
  892. u32 sysid, srb_status, build;
  893. u8 sysid_nr_chan;
  894. int ret;
  895. ret = kvaser_pciefd_read_cfg(pcie);
  896. if (ret)
  897. return ret;
  898. sysid = ioread32(pcie->reg_base + KVASER_PCIEFD_SYSID_VERSION_REG);
  899. sysid_nr_chan = (sysid >> KVASER_PCIEFD_SYSID_NRCHAN_SHIFT) & 0xff;
  900. if (pcie->nr_channels != sysid_nr_chan) {
  901. dev_err(&pcie->pci->dev,
  902. "Number of channels does not match: %u vs %u\n",
  903. pcie->nr_channels,
  904. sysid_nr_chan);
  905. return -ENODEV;
  906. }
  907. if (pcie->nr_channels > KVASER_PCIEFD_MAX_CAN_CHANNELS)
  908. pcie->nr_channels = KVASER_PCIEFD_MAX_CAN_CHANNELS;
  909. build = ioread32(pcie->reg_base + KVASER_PCIEFD_SYSID_BUILD_REG);
  910. dev_dbg(&pcie->pci->dev, "Version %u.%u.%u\n",
  911. (sysid >> KVASER_PCIEFD_SYSID_MAJOR_VER_SHIFT) & 0xff,
  912. sysid & 0xff,
  913. (build >> KVASER_PCIEFD_SYSID_BUILD_VER_SHIFT) & 0x7fff);
  914. srb_status = ioread32(pcie->reg_base + KVASER_PCIEFD_SRB_STAT_REG);
  915. if (!(srb_status & KVASER_PCIEFD_SRB_STAT_DMA)) {
  916. dev_err(&pcie->pci->dev,
  917. "Hardware without DMA is not supported\n");
  918. return -ENODEV;
  919. }
  920. pcie->bus_freq = ioread32(pcie->reg_base +
  921. KVASER_PCIEFD_SYSID_BUSFREQ_REG);
  922. pcie->freq = ioread32(pcie->reg_base + KVASER_PCIEFD_SYSID_CANFREQ_REG);
  923. pcie->freq_to_ticks_div = pcie->freq / 1000000;
  924. if (pcie->freq_to_ticks_div == 0)
  925. pcie->freq_to_ticks_div = 1;
  926. /* Turn off all loopback functionality */
  927. iowrite32(0, pcie->reg_base + KVASER_PCIEFD_LOOP_REG);
  928. return ret;
  929. }
  930. static int kvaser_pciefd_handle_data_packet(struct kvaser_pciefd *pcie,
  931. struct kvaser_pciefd_rx_packet *p,
  932. __le32 *data)
  933. {
  934. struct sk_buff *skb;
  935. struct canfd_frame *cf;
  936. struct can_priv *priv;
  937. struct net_device_stats *stats;
  938. struct skb_shared_hwtstamps *shhwtstamps;
  939. u8 ch_id = (p->header[1] >> KVASER_PCIEFD_PACKET_CHID_SHIFT) & 0x7;
  940. if (ch_id >= pcie->nr_channels)
  941. return -EIO;
  942. priv = &pcie->can[ch_id]->can;
  943. stats = &priv->dev->stats;
  944. if (p->header[1] & KVASER_PCIEFD_RPACKET_FDF) {
  945. skb = alloc_canfd_skb(priv->dev, &cf);
  946. if (!skb) {
  947. stats->rx_dropped++;
  948. return -ENOMEM;
  949. }
  950. if (p->header[1] & KVASER_PCIEFD_RPACKET_BRS)
  951. cf->flags |= CANFD_BRS;
  952. if (p->header[1] & KVASER_PCIEFD_RPACKET_ESI)
  953. cf->flags |= CANFD_ESI;
  954. } else {
  955. skb = alloc_can_skb(priv->dev, (struct can_frame **)&cf);
  956. if (!skb) {
  957. stats->rx_dropped++;
  958. return -ENOMEM;
  959. }
  960. }
  961. cf->can_id = p->header[0] & CAN_EFF_MASK;
  962. if (p->header[0] & KVASER_PCIEFD_RPACKET_IDE)
  963. cf->can_id |= CAN_EFF_FLAG;
  964. cf->len = can_dlc2len(p->header[1] >> KVASER_PCIEFD_RPACKET_DLC_SHIFT);
  965. if (p->header[0] & KVASER_PCIEFD_RPACKET_RTR)
  966. cf->can_id |= CAN_RTR_FLAG;
  967. else
  968. memcpy(cf->data, data, cf->len);
  969. shhwtstamps = skb_hwtstamps(skb);
  970. shhwtstamps->hwtstamp =
  971. ns_to_ktime(div_u64(p->timestamp * 1000,
  972. pcie->freq_to_ticks_div));
  973. stats->rx_bytes += cf->len;
  974. stats->rx_packets++;
  975. return netif_rx(skb);
  976. }
  977. static void kvaser_pciefd_change_state(struct kvaser_pciefd_can *can,
  978. struct can_frame *cf,
  979. enum can_state new_state,
  980. enum can_state tx_state,
  981. enum can_state rx_state)
  982. {
  983. can_change_state(can->can.dev, cf, tx_state, rx_state);
  984. if (new_state == CAN_STATE_BUS_OFF) {
  985. struct net_device *ndev = can->can.dev;
  986. unsigned long irq_flags;
  987. spin_lock_irqsave(&can->lock, irq_flags);
  988. netif_stop_queue(can->can.dev);
  989. spin_unlock_irqrestore(&can->lock, irq_flags);
  990. /* Prevent CAN controller from auto recover from bus off */
  991. if (!can->can.restart_ms) {
  992. kvaser_pciefd_start_controller_flush(can);
  993. can_bus_off(ndev);
  994. }
  995. }
  996. }
  997. static void kvaser_pciefd_packet_to_state(struct kvaser_pciefd_rx_packet *p,
  998. struct can_berr_counter *bec,
  999. enum can_state *new_state,
  1000. enum can_state *tx_state,
  1001. enum can_state *rx_state)
  1002. {
  1003. if (p->header[0] & KVASER_PCIEFD_SPACK_BOFF ||
  1004. p->header[0] & KVASER_PCIEFD_SPACK_IRM)
  1005. *new_state = CAN_STATE_BUS_OFF;
  1006. else if (bec->txerr >= 255 || bec->rxerr >= 255)
  1007. *new_state = CAN_STATE_BUS_OFF;
  1008. else if (p->header[1] & KVASER_PCIEFD_SPACK_EPLR)
  1009. *new_state = CAN_STATE_ERROR_PASSIVE;
  1010. else if (bec->txerr >= 128 || bec->rxerr >= 128)
  1011. *new_state = CAN_STATE_ERROR_PASSIVE;
  1012. else if (p->header[1] & KVASER_PCIEFD_SPACK_EWLR)
  1013. *new_state = CAN_STATE_ERROR_WARNING;
  1014. else if (bec->txerr >= 96 || bec->rxerr >= 96)
  1015. *new_state = CAN_STATE_ERROR_WARNING;
  1016. else
  1017. *new_state = CAN_STATE_ERROR_ACTIVE;
  1018. *tx_state = bec->txerr >= bec->rxerr ? *new_state : 0;
  1019. *rx_state = bec->txerr <= bec->rxerr ? *new_state : 0;
  1020. }
  1021. static int kvaser_pciefd_rx_error_frame(struct kvaser_pciefd_can *can,
  1022. struct kvaser_pciefd_rx_packet *p)
  1023. {
  1024. struct can_berr_counter bec;
  1025. enum can_state old_state, new_state, tx_state, rx_state;
  1026. struct net_device *ndev = can->can.dev;
  1027. struct sk_buff *skb;
  1028. struct can_frame *cf = NULL;
  1029. struct skb_shared_hwtstamps *shhwtstamps;
  1030. struct net_device_stats *stats = &ndev->stats;
  1031. old_state = can->can.state;
  1032. bec.txerr = p->header[0] & 0xff;
  1033. bec.rxerr = (p->header[0] >> KVASER_PCIEFD_SPACK_RXERR_SHIFT) & 0xff;
  1034. kvaser_pciefd_packet_to_state(p, &bec, &new_state, &tx_state,
  1035. &rx_state);
  1036. skb = alloc_can_err_skb(ndev, &cf);
  1037. if (new_state != old_state) {
  1038. kvaser_pciefd_change_state(can, cf, new_state, tx_state,
  1039. rx_state);
  1040. if (old_state == CAN_STATE_BUS_OFF &&
  1041. new_state == CAN_STATE_ERROR_ACTIVE &&
  1042. can->can.restart_ms) {
  1043. can->can.can_stats.restarts++;
  1044. if (skb)
  1045. cf->can_id |= CAN_ERR_RESTARTED;
  1046. }
  1047. }
  1048. can->err_rep_cnt++;
  1049. can->can.can_stats.bus_error++;
  1050. stats->rx_errors++;
  1051. can->bec.txerr = bec.txerr;
  1052. can->bec.rxerr = bec.rxerr;
  1053. if (!skb) {
  1054. stats->rx_dropped++;
  1055. return -ENOMEM;
  1056. }
  1057. shhwtstamps = skb_hwtstamps(skb);
  1058. shhwtstamps->hwtstamp =
  1059. ns_to_ktime(div_u64(p->timestamp * 1000,
  1060. can->kv_pcie->freq_to_ticks_div));
  1061. cf->can_id |= CAN_ERR_BUSERROR;
  1062. cf->data[6] = bec.txerr;
  1063. cf->data[7] = bec.rxerr;
  1064. stats->rx_packets++;
  1065. stats->rx_bytes += cf->can_dlc;
  1066. netif_rx(skb);
  1067. return 0;
  1068. }
  1069. static int kvaser_pciefd_handle_error_packet(struct kvaser_pciefd *pcie,
  1070. struct kvaser_pciefd_rx_packet *p)
  1071. {
  1072. struct kvaser_pciefd_can *can;
  1073. u8 ch_id = (p->header[1] >> KVASER_PCIEFD_PACKET_CHID_SHIFT) & 0x7;
  1074. if (ch_id >= pcie->nr_channels)
  1075. return -EIO;
  1076. can = pcie->can[ch_id];
  1077. kvaser_pciefd_rx_error_frame(can, p);
  1078. if (can->err_rep_cnt >= KVASER_PCIEFD_MAX_ERR_REP)
  1079. /* Do not report more errors, until bec_poll_timer expires */
  1080. kvaser_pciefd_disable_err_gen(can);
  1081. /* Start polling the error counters */
  1082. mod_timer(&can->bec_poll_timer, KVASER_PCIEFD_BEC_POLL_FREQ);
  1083. return 0;
  1084. }
  1085. static int kvaser_pciefd_handle_status_resp(struct kvaser_pciefd_can *can,
  1086. struct kvaser_pciefd_rx_packet *p)
  1087. {
  1088. struct can_berr_counter bec;
  1089. enum can_state old_state, new_state, tx_state, rx_state;
  1090. old_state = can->can.state;
  1091. bec.txerr = p->header[0] & 0xff;
  1092. bec.rxerr = (p->header[0] >> KVASER_PCIEFD_SPACK_RXERR_SHIFT) & 0xff;
  1093. kvaser_pciefd_packet_to_state(p, &bec, &new_state, &tx_state,
  1094. &rx_state);
  1095. if (new_state != old_state) {
  1096. struct net_device *ndev = can->can.dev;
  1097. struct sk_buff *skb;
  1098. struct can_frame *cf;
  1099. struct skb_shared_hwtstamps *shhwtstamps;
  1100. skb = alloc_can_err_skb(ndev, &cf);
  1101. if (!skb) {
  1102. struct net_device_stats *stats = &ndev->stats;
  1103. stats->rx_dropped++;
  1104. return -ENOMEM;
  1105. }
  1106. kvaser_pciefd_change_state(can, cf, new_state, tx_state,
  1107. rx_state);
  1108. if (old_state == CAN_STATE_BUS_OFF &&
  1109. new_state == CAN_STATE_ERROR_ACTIVE &&
  1110. can->can.restart_ms) {
  1111. can->can.can_stats.restarts++;
  1112. cf->can_id |= CAN_ERR_RESTARTED;
  1113. }
  1114. shhwtstamps = skb_hwtstamps(skb);
  1115. shhwtstamps->hwtstamp =
  1116. ns_to_ktime(div_u64(p->timestamp * 1000,
  1117. can->kv_pcie->freq_to_ticks_div));
  1118. cf->data[6] = bec.txerr;
  1119. cf->data[7] = bec.rxerr;
  1120. netif_rx(skb);
  1121. }
  1122. can->bec.txerr = bec.txerr;
  1123. can->bec.rxerr = bec.rxerr;
  1124. /* Check if we need to poll the error counters */
  1125. if (bec.txerr || bec.rxerr)
  1126. mod_timer(&can->bec_poll_timer, KVASER_PCIEFD_BEC_POLL_FREQ);
  1127. return 0;
  1128. }
  1129. static int kvaser_pciefd_handle_status_packet(struct kvaser_pciefd *pcie,
  1130. struct kvaser_pciefd_rx_packet *p)
  1131. {
  1132. struct kvaser_pciefd_can *can;
  1133. u8 cmdseq;
  1134. u32 status;
  1135. u8 ch_id = (p->header[1] >> KVASER_PCIEFD_PACKET_CHID_SHIFT) & 0x7;
  1136. if (ch_id >= pcie->nr_channels)
  1137. return -EIO;
  1138. can = pcie->can[ch_id];
  1139. status = ioread32(can->reg_base + KVASER_PCIEFD_KCAN_STAT_REG);
  1140. cmdseq = (status >> KVASER_PCIEFD_KCAN_STAT_SEQNO_SHIFT) & 0xff;
  1141. /* Reset done, start abort and flush */
  1142. if (p->header[0] & KVASER_PCIEFD_SPACK_IRM &&
  1143. p->header[0] & KVASER_PCIEFD_SPACK_RMCD &&
  1144. p->header[1] & KVASER_PCIEFD_SPACK_AUTO &&
  1145. cmdseq == (p->header[1] & KVASER_PCIEFD_PACKET_SEQ_MSK) &&
  1146. status & KVASER_PCIEFD_KCAN_STAT_IDLE) {
  1147. u32 cmd;
  1148. iowrite32(KVASER_PCIEFD_KCAN_IRQ_ABD,
  1149. can->reg_base + KVASER_PCIEFD_KCAN_IRQ_REG);
  1150. cmd = KVASER_PCIEFD_KCAN_CMD_AT;
  1151. cmd |= ++can->cmd_seq << KVASER_PCIEFD_KCAN_CMD_SEQ_SHIFT;
  1152. iowrite32(cmd, can->reg_base + KVASER_PCIEFD_KCAN_CMD_REG);
  1153. iowrite32(KVASER_PCIEFD_KCAN_IRQ_TFD,
  1154. can->reg_base + KVASER_PCIEFD_KCAN_IEN_REG);
  1155. } else if (p->header[0] & KVASER_PCIEFD_SPACK_IDET &&
  1156. p->header[0] & KVASER_PCIEFD_SPACK_IRM &&
  1157. cmdseq == (p->header[1] & KVASER_PCIEFD_PACKET_SEQ_MSK) &&
  1158. status & KVASER_PCIEFD_KCAN_STAT_IDLE) {
  1159. /* Reset detected, send end of flush if no packet are in FIFO */
  1160. u8 count = ioread32(can->reg_base +
  1161. KVASER_PCIEFD_KCAN_TX_NPACKETS_REG) & 0xff;
  1162. if (!count)
  1163. iowrite32(KVASER_PCIEFD_KCAN_CTRL_EFLUSH,
  1164. can->reg_base + KVASER_PCIEFD_KCAN_CTRL_REG);
  1165. } else if (!(p->header[1] & KVASER_PCIEFD_SPACK_AUTO) &&
  1166. cmdseq == (p->header[1] & KVASER_PCIEFD_PACKET_SEQ_MSK)) {
  1167. /* Response to status request received */
  1168. kvaser_pciefd_handle_status_resp(can, p);
  1169. if (can->can.state != CAN_STATE_BUS_OFF &&
  1170. can->can.state != CAN_STATE_ERROR_ACTIVE) {
  1171. mod_timer(&can->bec_poll_timer,
  1172. KVASER_PCIEFD_BEC_POLL_FREQ);
  1173. }
  1174. } else if (p->header[0] & KVASER_PCIEFD_SPACK_RMCD &&
  1175. !(status & KVASER_PCIEFD_KCAN_STAT_BUS_OFF_MSK)) {
  1176. /* Reset to bus on detected */
  1177. if (!completion_done(&can->start_comp))
  1178. complete(&can->start_comp);
  1179. }
  1180. return 0;
  1181. }
  1182. static int kvaser_pciefd_handle_eack_packet(struct kvaser_pciefd *pcie,
  1183. struct kvaser_pciefd_rx_packet *p)
  1184. {
  1185. struct kvaser_pciefd_can *can;
  1186. u8 ch_id = (p->header[1] >> KVASER_PCIEFD_PACKET_CHID_SHIFT) & 0x7;
  1187. if (ch_id >= pcie->nr_channels)
  1188. return -EIO;
  1189. can = pcie->can[ch_id];
  1190. /* If this is the last flushed packet, send end of flush */
  1191. if (p->header[0] & KVASER_PCIEFD_APACKET_FLU) {
  1192. u8 count = ioread32(can->reg_base +
  1193. KVASER_PCIEFD_KCAN_TX_NPACKETS_REG) & 0xff;
  1194. if (count == 0)
  1195. iowrite32(KVASER_PCIEFD_KCAN_CTRL_EFLUSH,
  1196. can->reg_base + KVASER_PCIEFD_KCAN_CTRL_REG);
  1197. } else {
  1198. int echo_idx = p->header[0] & KVASER_PCIEFD_PACKET_SEQ_MSK;
  1199. int dlc = can_get_echo_skb(can->can.dev, echo_idx);
  1200. struct net_device_stats *stats = &can->can.dev->stats;
  1201. stats->tx_bytes += dlc;
  1202. stats->tx_packets++;
  1203. if (netif_queue_stopped(can->can.dev))
  1204. netif_wake_queue(can->can.dev);
  1205. }
  1206. return 0;
  1207. }
  1208. static void kvaser_pciefd_handle_nack_packet(struct kvaser_pciefd_can *can,
  1209. struct kvaser_pciefd_rx_packet *p)
  1210. {
  1211. struct sk_buff *skb;
  1212. struct net_device_stats *stats = &can->can.dev->stats;
  1213. struct can_frame *cf;
  1214. skb = alloc_can_err_skb(can->can.dev, &cf);
  1215. stats->tx_errors++;
  1216. if (p->header[0] & KVASER_PCIEFD_APACKET_ABL) {
  1217. if (skb)
  1218. cf->can_id |= CAN_ERR_LOSTARB;
  1219. can->can.can_stats.arbitration_lost++;
  1220. } else if (skb) {
  1221. cf->can_id |= CAN_ERR_ACK;
  1222. }
  1223. if (skb) {
  1224. cf->can_id |= CAN_ERR_BUSERROR;
  1225. stats->rx_bytes += cf->can_dlc;
  1226. stats->rx_packets++;
  1227. netif_rx(skb);
  1228. } else {
  1229. stats->rx_dropped++;
  1230. netdev_warn(can->can.dev, "No memory left for err_skb\n");
  1231. }
  1232. }
  1233. static int kvaser_pciefd_handle_ack_packet(struct kvaser_pciefd *pcie,
  1234. struct kvaser_pciefd_rx_packet *p)
  1235. {
  1236. struct kvaser_pciefd_can *can;
  1237. bool one_shot_fail = false;
  1238. u8 ch_id = (p->header[1] >> KVASER_PCIEFD_PACKET_CHID_SHIFT) & 0x7;
  1239. if (ch_id >= pcie->nr_channels)
  1240. return -EIO;
  1241. can = pcie->can[ch_id];
  1242. /* Ignore control packet ACK */
  1243. if (p->header[0] & KVASER_PCIEFD_APACKET_CT)
  1244. return 0;
  1245. if (p->header[0] & KVASER_PCIEFD_APACKET_NACK) {
  1246. kvaser_pciefd_handle_nack_packet(can, p);
  1247. one_shot_fail = true;
  1248. }
  1249. if (p->header[0] & KVASER_PCIEFD_APACKET_FLU) {
  1250. netdev_dbg(can->can.dev, "Packet was flushed\n");
  1251. } else {
  1252. int echo_idx = p->header[0] & KVASER_PCIEFD_PACKET_SEQ_MSK;
  1253. int dlc = can_get_echo_skb(can->can.dev, echo_idx);
  1254. u8 count = ioread32(can->reg_base +
  1255. KVASER_PCIEFD_KCAN_TX_NPACKETS_REG) & 0xff;
  1256. if (count < KVASER_PCIEFD_CAN_TX_MAX_COUNT &&
  1257. netif_queue_stopped(can->can.dev))
  1258. netif_wake_queue(can->can.dev);
  1259. if (!one_shot_fail) {
  1260. struct net_device_stats *stats = &can->can.dev->stats;
  1261. stats->tx_bytes += dlc;
  1262. stats->tx_packets++;
  1263. }
  1264. }
  1265. return 0;
  1266. }
  1267. static int kvaser_pciefd_handle_eflush_packet(struct kvaser_pciefd *pcie,
  1268. struct kvaser_pciefd_rx_packet *p)
  1269. {
  1270. struct kvaser_pciefd_can *can;
  1271. u8 ch_id = (p->header[1] >> KVASER_PCIEFD_PACKET_CHID_SHIFT) & 0x7;
  1272. if (ch_id >= pcie->nr_channels)
  1273. return -EIO;
  1274. can = pcie->can[ch_id];
  1275. if (!completion_done(&can->flush_comp))
  1276. complete(&can->flush_comp);
  1277. return 0;
  1278. }
  1279. static int kvaser_pciefd_read_packet(struct kvaser_pciefd *pcie, int *start_pos,
  1280. int dma_buf)
  1281. {
  1282. __le32 *buffer = pcie->dma_data[dma_buf];
  1283. __le64 timestamp;
  1284. struct kvaser_pciefd_rx_packet packet;
  1285. struct kvaser_pciefd_rx_packet *p = &packet;
  1286. u8 type;
  1287. int pos = *start_pos;
  1288. int size;
  1289. int ret = 0;
  1290. size = le32_to_cpu(buffer[pos++]);
  1291. if (!size) {
  1292. *start_pos = 0;
  1293. return 0;
  1294. }
  1295. p->header[0] = le32_to_cpu(buffer[pos++]);
  1296. p->header[1] = le32_to_cpu(buffer[pos++]);
  1297. /* Read 64-bit timestamp */
  1298. memcpy(&timestamp, &buffer[pos], sizeof(__le64));
  1299. pos += 2;
  1300. p->timestamp = le64_to_cpu(timestamp);
  1301. type = (p->header[1] >> KVASER_PCIEFD_PACKET_TYPE_SHIFT) & 0xf;
  1302. switch (type) {
  1303. case KVASER_PCIEFD_PACK_TYPE_DATA:
  1304. ret = kvaser_pciefd_handle_data_packet(pcie, p, &buffer[pos]);
  1305. if (!(p->header[0] & KVASER_PCIEFD_RPACKET_RTR)) {
  1306. u8 data_len;
  1307. data_len = can_dlc2len(p->header[1] >>
  1308. KVASER_PCIEFD_RPACKET_DLC_SHIFT);
  1309. pos += DIV_ROUND_UP(data_len, 4);
  1310. }
  1311. break;
  1312. case KVASER_PCIEFD_PACK_TYPE_ACK:
  1313. ret = kvaser_pciefd_handle_ack_packet(pcie, p);
  1314. break;
  1315. case KVASER_PCIEFD_PACK_TYPE_STATUS:
  1316. ret = kvaser_pciefd_handle_status_packet(pcie, p);
  1317. break;
  1318. case KVASER_PCIEFD_PACK_TYPE_ERROR:
  1319. ret = kvaser_pciefd_handle_error_packet(pcie, p);
  1320. break;
  1321. case KVASER_PCIEFD_PACK_TYPE_EFRAME_ACK:
  1322. ret = kvaser_pciefd_handle_eack_packet(pcie, p);
  1323. break;
  1324. case KVASER_PCIEFD_PACK_TYPE_EFLUSH_ACK:
  1325. ret = kvaser_pciefd_handle_eflush_packet(pcie, p);
  1326. break;
  1327. case KVASER_PCIEFD_PACK_TYPE_ACK_DATA:
  1328. case KVASER_PCIEFD_PACK_TYPE_BUS_LOAD:
  1329. case KVASER_PCIEFD_PACK_TYPE_TXRQ:
  1330. dev_info(&pcie->pci->dev,
  1331. "Received unexpected packet type 0x%08X\n", type);
  1332. break;
  1333. default:
  1334. dev_err(&pcie->pci->dev, "Unknown packet type 0x%08X\n", type);
  1335. ret = -EIO;
  1336. break;
  1337. }
  1338. if (ret)
  1339. return ret;
  1340. /* Position does not point to the end of the package,
  1341. * corrupted packet size?
  1342. */
  1343. if ((*start_pos + size) != pos)
  1344. return -EIO;
  1345. /* Point to the next packet header, if any */
  1346. *start_pos = pos;
  1347. return ret;
  1348. }
  1349. static int kvaser_pciefd_read_buffer(struct kvaser_pciefd *pcie, int dma_buf)
  1350. {
  1351. int pos = 0;
  1352. int res = 0;
  1353. do {
  1354. res = kvaser_pciefd_read_packet(pcie, &pos, dma_buf);
  1355. } while (!res && pos > 0 && pos < KVASER_PCIEFD_DMA_SIZE);
  1356. return res;
  1357. }
  1358. static int kvaser_pciefd_receive_irq(struct kvaser_pciefd *pcie)
  1359. {
  1360. u32 irq;
  1361. irq = ioread32(pcie->reg_base + KVASER_PCIEFD_SRB_IRQ_REG);
  1362. if (irq & KVASER_PCIEFD_SRB_IRQ_DPD0) {
  1363. kvaser_pciefd_read_buffer(pcie, 0);
  1364. /* Reset DMA buffer 0 */
  1365. iowrite32(KVASER_PCIEFD_SRB_CMD_RDB0,
  1366. pcie->reg_base + KVASER_PCIEFD_SRB_CMD_REG);
  1367. }
  1368. if (irq & KVASER_PCIEFD_SRB_IRQ_DPD1) {
  1369. kvaser_pciefd_read_buffer(pcie, 1);
  1370. /* Reset DMA buffer 1 */
  1371. iowrite32(KVASER_PCIEFD_SRB_CMD_RDB1,
  1372. pcie->reg_base + KVASER_PCIEFD_SRB_CMD_REG);
  1373. }
  1374. if (irq & KVASER_PCIEFD_SRB_IRQ_DOF0 ||
  1375. irq & KVASER_PCIEFD_SRB_IRQ_DOF1 ||
  1376. irq & KVASER_PCIEFD_SRB_IRQ_DUF0 ||
  1377. irq & KVASER_PCIEFD_SRB_IRQ_DUF1)
  1378. dev_err(&pcie->pci->dev, "DMA IRQ error 0x%08X\n", irq);
  1379. iowrite32(irq, pcie->reg_base + KVASER_PCIEFD_SRB_IRQ_REG);
  1380. return 0;
  1381. }
  1382. static int kvaser_pciefd_transmit_irq(struct kvaser_pciefd_can *can)
  1383. {
  1384. u32 irq = ioread32(can->reg_base + KVASER_PCIEFD_KCAN_IRQ_REG);
  1385. if (irq & KVASER_PCIEFD_KCAN_IRQ_TOF)
  1386. netdev_err(can->can.dev, "Tx FIFO overflow\n");
  1387. if (irq & KVASER_PCIEFD_KCAN_IRQ_TFD) {
  1388. u8 count = ioread32(can->reg_base +
  1389. KVASER_PCIEFD_KCAN_TX_NPACKETS_REG) & 0xff;
  1390. if (count == 0)
  1391. iowrite32(KVASER_PCIEFD_KCAN_CTRL_EFLUSH,
  1392. can->reg_base + KVASER_PCIEFD_KCAN_CTRL_REG);
  1393. }
  1394. if (irq & KVASER_PCIEFD_KCAN_IRQ_BPP)
  1395. netdev_err(can->can.dev,
  1396. "Fail to change bittiming, when not in reset mode\n");
  1397. if (irq & KVASER_PCIEFD_KCAN_IRQ_FDIC)
  1398. netdev_err(can->can.dev, "CAN FD frame in CAN mode\n");
  1399. if (irq & KVASER_PCIEFD_KCAN_IRQ_ROF)
  1400. netdev_err(can->can.dev, "Rx FIFO overflow\n");
  1401. iowrite32(irq, can->reg_base + KVASER_PCIEFD_KCAN_IRQ_REG);
  1402. return 0;
  1403. }
  1404. static irqreturn_t kvaser_pciefd_irq_handler(int irq, void *dev)
  1405. {
  1406. struct kvaser_pciefd *pcie = (struct kvaser_pciefd *)dev;
  1407. u32 board_irq;
  1408. int i;
  1409. board_irq = ioread32(pcie->reg_base + KVASER_PCIEFD_IRQ_REG);
  1410. if (!(board_irq & KVASER_PCIEFD_IRQ_ALL_MSK))
  1411. return IRQ_NONE;
  1412. if (board_irq & KVASER_PCIEFD_IRQ_SRB)
  1413. kvaser_pciefd_receive_irq(pcie);
  1414. for (i = 0; i < pcie->nr_channels; i++) {
  1415. if (!pcie->can[i]) {
  1416. dev_err(&pcie->pci->dev,
  1417. "IRQ mask points to unallocated controller\n");
  1418. break;
  1419. }
  1420. /* Check that mask matches channel (i) IRQ mask */
  1421. if (board_irq & (1 << i))
  1422. kvaser_pciefd_transmit_irq(pcie->can[i]);
  1423. }
  1424. iowrite32(board_irq, pcie->reg_base + KVASER_PCIEFD_IRQ_REG);
  1425. return IRQ_HANDLED;
  1426. }
  1427. static void kvaser_pciefd_teardown_can_ctrls(struct kvaser_pciefd *pcie)
  1428. {
  1429. int i;
  1430. struct kvaser_pciefd_can *can;
  1431. for (i = 0; i < pcie->nr_channels; i++) {
  1432. can = pcie->can[i];
  1433. if (can) {
  1434. iowrite32(0,
  1435. can->reg_base + KVASER_PCIEFD_KCAN_IEN_REG);
  1436. kvaser_pciefd_pwm_stop(can);
  1437. free_candev(can->can.dev);
  1438. }
  1439. }
  1440. }
  1441. static int kvaser_pciefd_probe(struct pci_dev *pdev,
  1442. const struct pci_device_id *id)
  1443. {
  1444. int err;
  1445. struct kvaser_pciefd *pcie;
  1446. pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
  1447. if (!pcie)
  1448. return -ENOMEM;
  1449. pci_set_drvdata(pdev, pcie);
  1450. pcie->pci = pdev;
  1451. err = pci_enable_device(pdev);
  1452. if (err)
  1453. return err;
  1454. err = pci_request_regions(pdev, KVASER_PCIEFD_DRV_NAME);
  1455. if (err)
  1456. goto err_disable_pci;
  1457. pcie->reg_base = pci_iomap(pdev, 0, 0);
  1458. if (!pcie->reg_base) {
  1459. err = -ENOMEM;
  1460. goto err_release_regions;
  1461. }
  1462. err = kvaser_pciefd_setup_board(pcie);
  1463. if (err)
  1464. goto err_pci_iounmap;
  1465. err = kvaser_pciefd_setup_dma(pcie);
  1466. if (err)
  1467. goto err_pci_iounmap;
  1468. pci_set_master(pdev);
  1469. err = kvaser_pciefd_setup_can_ctrls(pcie);
  1470. if (err)
  1471. goto err_teardown_can_ctrls;
  1472. iowrite32(KVASER_PCIEFD_SRB_IRQ_DPD0 | KVASER_PCIEFD_SRB_IRQ_DPD1,
  1473. pcie->reg_base + KVASER_PCIEFD_SRB_IRQ_REG);
  1474. iowrite32(KVASER_PCIEFD_SRB_IRQ_DPD0 | KVASER_PCIEFD_SRB_IRQ_DPD1 |
  1475. KVASER_PCIEFD_SRB_IRQ_DOF0 | KVASER_PCIEFD_SRB_IRQ_DOF1 |
  1476. KVASER_PCIEFD_SRB_IRQ_DUF0 | KVASER_PCIEFD_SRB_IRQ_DUF1,
  1477. pcie->reg_base + KVASER_PCIEFD_SRB_IEN_REG);
  1478. /* Reset IRQ handling, expected to be off before */
  1479. iowrite32(KVASER_PCIEFD_IRQ_ALL_MSK,
  1480. pcie->reg_base + KVASER_PCIEFD_IRQ_REG);
  1481. iowrite32(KVASER_PCIEFD_IRQ_ALL_MSK,
  1482. pcie->reg_base + KVASER_PCIEFD_IEN_REG);
  1483. /* Ready the DMA buffers */
  1484. iowrite32(KVASER_PCIEFD_SRB_CMD_RDB0,
  1485. pcie->reg_base + KVASER_PCIEFD_SRB_CMD_REG);
  1486. iowrite32(KVASER_PCIEFD_SRB_CMD_RDB1,
  1487. pcie->reg_base + KVASER_PCIEFD_SRB_CMD_REG);
  1488. err = request_irq(pcie->pci->irq, kvaser_pciefd_irq_handler,
  1489. IRQF_SHARED, KVASER_PCIEFD_DRV_NAME, pcie);
  1490. if (err)
  1491. goto err_teardown_can_ctrls;
  1492. err = kvaser_pciefd_reg_candev(pcie);
  1493. if (err)
  1494. goto err_free_irq;
  1495. return 0;
  1496. err_free_irq:
  1497. free_irq(pcie->pci->irq, pcie);
  1498. err_teardown_can_ctrls:
  1499. kvaser_pciefd_teardown_can_ctrls(pcie);
  1500. iowrite32(0, pcie->reg_base + KVASER_PCIEFD_SRB_CTRL_REG);
  1501. pci_clear_master(pdev);
  1502. err_pci_iounmap:
  1503. pci_iounmap(pdev, pcie->reg_base);
  1504. err_release_regions:
  1505. pci_release_regions(pdev);
  1506. err_disable_pci:
  1507. pci_disable_device(pdev);
  1508. return err;
  1509. }
  1510. static void kvaser_pciefd_remove_all_ctrls(struct kvaser_pciefd *pcie)
  1511. {
  1512. struct kvaser_pciefd_can *can;
  1513. int i;
  1514. for (i = 0; i < pcie->nr_channels; i++) {
  1515. can = pcie->can[i];
  1516. if (can) {
  1517. iowrite32(0,
  1518. can->reg_base + KVASER_PCIEFD_KCAN_IEN_REG);
  1519. unregister_candev(can->can.dev);
  1520. del_timer(&can->bec_poll_timer);
  1521. kvaser_pciefd_pwm_stop(can);
  1522. free_candev(can->can.dev);
  1523. }
  1524. }
  1525. }
  1526. static void kvaser_pciefd_remove(struct pci_dev *pdev)
  1527. {
  1528. struct kvaser_pciefd *pcie = pci_get_drvdata(pdev);
  1529. kvaser_pciefd_remove_all_ctrls(pcie);
  1530. /* Turn off IRQ generation */
  1531. iowrite32(0, pcie->reg_base + KVASER_PCIEFD_SRB_CTRL_REG);
  1532. iowrite32(KVASER_PCIEFD_IRQ_ALL_MSK,
  1533. pcie->reg_base + KVASER_PCIEFD_IRQ_REG);
  1534. iowrite32(0, pcie->reg_base + KVASER_PCIEFD_IEN_REG);
  1535. free_irq(pcie->pci->irq, pcie);
  1536. pci_clear_master(pdev);
  1537. pci_iounmap(pdev, pcie->reg_base);
  1538. pci_release_regions(pdev);
  1539. pci_disable_device(pdev);
  1540. }
  1541. static struct pci_driver kvaser_pciefd = {
  1542. .name = KVASER_PCIEFD_DRV_NAME,
  1543. .id_table = kvaser_pciefd_id_table,
  1544. .probe = kvaser_pciefd_probe,
  1545. .remove = kvaser_pciefd_remove,
  1546. };
  1547. module_pci_driver(kvaser_pciefd)