peak_pciefd_main.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /*
  2. * Copyright (C) 2007, 2011 Wolfgang Grandegger <wg@grandegger.com>
  3. * Copyright (C) 2012 Stephane Grosjean <s.grosjean@peak-system.com>
  4. *
  5. * Derived from the PCAN project file driver/src/pcan_pci.c:
  6. *
  7. * Copyright (C) 2001-2006 PEAK System-Technik GmbH
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the version 2 of the GNU General Public License
  11. * as published by the Free Software Foundation
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/delay.h>
  23. #include <linux/pci.h>
  24. #include <linux/io.h>
  25. #include <linux/can.h>
  26. #include <linux/can/dev.h>
  27. #include "peak_canfd_user.h"
  28. MODULE_AUTHOR("Stephane Grosjean <s.grosjean@peak-system.com>");
  29. MODULE_DESCRIPTION("Socket-CAN driver for PEAK PCAN PCIe FD family cards");
  30. MODULE_SUPPORTED_DEVICE("PEAK PCAN PCIe FD CAN cards");
  31. MODULE_LICENSE("GPL v2");
  32. #define PCIEFD_DRV_NAME "peak_pciefd"
  33. #define PEAK_PCI_VENDOR_ID 0x001c /* The PCI device and vendor IDs */
  34. #define PEAK_PCIEFD_ID 0x0013 /* for PCIe slot cards */
  35. /* PEAK PCIe board access description */
  36. #define PCIEFD_BAR0_SIZE (64 * 1024)
  37. #define PCIEFD_RX_DMA_SIZE (4 * 1024)
  38. #define PCIEFD_TX_DMA_SIZE (4 * 1024)
  39. #define PCIEFD_TX_PAGE_SIZE (2 * 1024)
  40. /* System Control Registers */
  41. #define PCIEFD_REG_SYS_CTL_SET 0x0000 /* set bits */
  42. #define PCIEFD_REG_SYS_CTL_CLR 0x0004 /* clear bits */
  43. /* Version info registers */
  44. #define PCIEFD_REG_SYS_VER1 0x0040 /* version reg #1 */
  45. #define PCIEFD_REG_SYS_VER2 0x0044 /* version reg #2 */
  46. /* System Control Registers Bits */
  47. #define PCIEFD_SYS_CTL_TS_RST 0x00000001 /* timestamp clock */
  48. #define PCIEFD_SYS_CTL_CLK_EN 0x00000002 /* system clock */
  49. /* CAN-FD channel addresses */
  50. #define PCIEFD_CANX_OFF(c) (((c) + 1) * 0x1000)
  51. #define PCIEFD_ECHO_SKB_MAX PCANFD_ECHO_SKB_DEF
  52. /* CAN-FD channel registers */
  53. #define PCIEFD_REG_CAN_MISC 0x0000 /* Misc. control */
  54. #define PCIEFD_REG_CAN_CLK_SEL 0x0008 /* Clock selector */
  55. #define PCIEFD_REG_CAN_CMD_PORT_L 0x0010 /* 64-bits command port */
  56. #define PCIEFD_REG_CAN_CMD_PORT_H 0x0014
  57. #define PCIEFD_REG_CAN_TX_REQ_ACC 0x0020 /* Tx request accumulator */
  58. #define PCIEFD_REG_CAN_TX_CTL_SET 0x0030 /* Tx control set register */
  59. #define PCIEFD_REG_CAN_TX_CTL_CLR 0x0038 /* Tx control clear register */
  60. #define PCIEFD_REG_CAN_TX_DMA_ADDR_L 0x0040 /* 64-bits addr for Tx DMA */
  61. #define PCIEFD_REG_CAN_TX_DMA_ADDR_H 0x0044
  62. #define PCIEFD_REG_CAN_RX_CTL_SET 0x0050 /* Rx control set register */
  63. #define PCIEFD_REG_CAN_RX_CTL_CLR 0x0058 /* Rx control clear register */
  64. #define PCIEFD_REG_CAN_RX_CTL_WRT 0x0060 /* Rx control write register */
  65. #define PCIEFD_REG_CAN_RX_CTL_ACK 0x0068 /* Rx control ACK register */
  66. #define PCIEFD_REG_CAN_RX_DMA_ADDR_L 0x0070 /* 64-bits addr for Rx DMA */
  67. #define PCIEFD_REG_CAN_RX_DMA_ADDR_H 0x0074
  68. /* CAN-FD channel misc register bits */
  69. #define CANFD_MISC_TS_RST 0x00000001 /* timestamp cnt rst */
  70. /* CAN-FD channel Clock SELector Source & DIVider */
  71. #define CANFD_CLK_SEL_DIV_MASK 0x00000007
  72. #define CANFD_CLK_SEL_DIV_60MHZ 0x00000000 /* SRC=240MHz only */
  73. #define CANFD_CLK_SEL_DIV_40MHZ 0x00000001 /* SRC=240MHz only */
  74. #define CANFD_CLK_SEL_DIV_30MHZ 0x00000002 /* SRC=240MHz only */
  75. #define CANFD_CLK_SEL_DIV_24MHZ 0x00000003 /* SRC=240MHz only */
  76. #define CANFD_CLK_SEL_DIV_20MHZ 0x00000004 /* SRC=240MHz only */
  77. #define CANFD_CLK_SEL_SRC_MASK 0x00000008 /* 0=80MHz, 1=240MHz */
  78. #define CANFD_CLK_SEL_SRC_240MHZ 0x00000008
  79. #define CANFD_CLK_SEL_SRC_80MHZ (~CANFD_CLK_SEL_SRC_240MHZ & \
  80. CANFD_CLK_SEL_SRC_MASK)
  81. #define CANFD_CLK_SEL_20MHZ (CANFD_CLK_SEL_SRC_240MHZ |\
  82. CANFD_CLK_SEL_DIV_20MHZ)
  83. #define CANFD_CLK_SEL_24MHZ (CANFD_CLK_SEL_SRC_240MHZ |\
  84. CANFD_CLK_SEL_DIV_24MHZ)
  85. #define CANFD_CLK_SEL_30MHZ (CANFD_CLK_SEL_SRC_240MHZ |\
  86. CANFD_CLK_SEL_DIV_30MHZ)
  87. #define CANFD_CLK_SEL_40MHZ (CANFD_CLK_SEL_SRC_240MHZ |\
  88. CANFD_CLK_SEL_DIV_40MHZ)
  89. #define CANFD_CLK_SEL_60MHZ (CANFD_CLK_SEL_SRC_240MHZ |\
  90. CANFD_CLK_SEL_DIV_60MHZ)
  91. #define CANFD_CLK_SEL_80MHZ (CANFD_CLK_SEL_SRC_80MHZ)
  92. /* CAN-FD channel Rx/Tx control register bits */
  93. #define CANFD_CTL_UNC_BIT 0x00010000 /* Uncached DMA mem */
  94. #define CANFD_CTL_RST_BIT 0x00020000 /* reset DMA action */
  95. #define CANFD_CTL_IEN_BIT 0x00040000 /* IRQ enable */
  96. /* Rx IRQ Count and Time Limits */
  97. #define CANFD_CTL_IRQ_CL_DEF 16 /* Rx msg max nb per IRQ in Rx DMA */
  98. #define CANFD_CTL_IRQ_TL_DEF 10 /* Time before IRQ if < CL (x100 µs) */
  99. #define CANFD_OPTIONS_SET (CANFD_OPTION_ERROR | CANFD_OPTION_BUSLOAD)
  100. /* Tx anticipation window (link logical address should be aligned on 2K
  101. * boundary)
  102. */
  103. #define PCIEFD_TX_PAGE_COUNT (PCIEFD_TX_DMA_SIZE / PCIEFD_TX_PAGE_SIZE)
  104. #define CANFD_MSG_LNK_TX 0x1001 /* Tx msgs link */
  105. /* 32-bits IRQ status fields, heading Rx DMA area */
  106. static inline int pciefd_irq_tag(u32 irq_status)
  107. {
  108. return irq_status & 0x0000000f;
  109. }
  110. static inline int pciefd_irq_rx_cnt(u32 irq_status)
  111. {
  112. return (irq_status & 0x000007f0) >> 4;
  113. }
  114. static inline int pciefd_irq_is_lnk(u32 irq_status)
  115. {
  116. return irq_status & 0x00010000;
  117. }
  118. /* Rx record */
  119. struct pciefd_rx_dma {
  120. __le32 irq_status;
  121. __le32 sys_time_low;
  122. __le32 sys_time_high;
  123. struct pucan_rx_msg msg[0];
  124. } __packed __aligned(4);
  125. /* Tx Link record */
  126. struct pciefd_tx_link {
  127. __le16 size;
  128. __le16 type;
  129. __le32 laddr_lo;
  130. __le32 laddr_hi;
  131. } __packed __aligned(4);
  132. /* Tx page descriptor */
  133. struct pciefd_page {
  134. void *vbase; /* page virtual address */
  135. dma_addr_t lbase; /* page logical address */
  136. u32 offset;
  137. u32 size;
  138. };
  139. #define CANFD_IRQ_SET 0x00000001
  140. #define CANFD_TX_PATH_SET 0x00000002
  141. /* CAN-FD channel object */
  142. struct pciefd_board;
  143. struct pciefd_can {
  144. struct peak_canfd_priv ucan; /* must be the first member */
  145. void __iomem *reg_base; /* channel config base addr */
  146. struct pciefd_board *board; /* reverse link */
  147. struct pucan_command pucan_cmd; /* command buffer */
  148. dma_addr_t rx_dma_laddr; /* DMA virtual and logical addr */
  149. void *rx_dma_vaddr; /* for Rx and Tx areas */
  150. dma_addr_t tx_dma_laddr;
  151. void *tx_dma_vaddr;
  152. struct pciefd_page tx_pages[PCIEFD_TX_PAGE_COUNT];
  153. u16 tx_pages_free; /* free Tx pages counter */
  154. u16 tx_page_index; /* current page used for Tx */
  155. spinlock_t tx_lock;
  156. u32 irq_status;
  157. u32 irq_tag; /* next irq tag */
  158. };
  159. /* PEAK-PCIe FD board object */
  160. struct pciefd_board {
  161. void __iomem *reg_base;
  162. struct pci_dev *pci_dev;
  163. int can_count;
  164. spinlock_t cmd_lock; /* 64-bits cmds must be atomic */
  165. struct pciefd_can *can[0]; /* array of network devices */
  166. };
  167. /* supported device ids. */
  168. static const struct pci_device_id peak_pciefd_tbl[] = {
  169. {PEAK_PCI_VENDOR_ID, PEAK_PCIEFD_ID, PCI_ANY_ID, PCI_ANY_ID,},
  170. {0,}
  171. };
  172. MODULE_DEVICE_TABLE(pci, peak_pciefd_tbl);
  173. /* read a 32 bits value from a SYS block register */
  174. static inline u32 pciefd_sys_readreg(const struct pciefd_board *priv, u16 reg)
  175. {
  176. return readl(priv->reg_base + reg);
  177. }
  178. /* write a 32 bits value into a SYS block register */
  179. static inline void pciefd_sys_writereg(const struct pciefd_board *priv,
  180. u32 val, u16 reg)
  181. {
  182. writel(val, priv->reg_base + reg);
  183. }
  184. /* read a 32 bits value from CAN-FD block register */
  185. static inline u32 pciefd_can_readreg(const struct pciefd_can *priv, u16 reg)
  186. {
  187. return readl(priv->reg_base + reg);
  188. }
  189. /* write a 32 bits value into a CAN-FD block register */
  190. static inline void pciefd_can_writereg(const struct pciefd_can *priv,
  191. u32 val, u16 reg)
  192. {
  193. writel(val, priv->reg_base + reg);
  194. }
  195. /* give a channel logical Rx DMA address to the board */
  196. static void pciefd_can_setup_rx_dma(struct pciefd_can *priv)
  197. {
  198. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  199. const u32 dma_addr_h = (u32)(priv->rx_dma_laddr >> 32);
  200. #else
  201. const u32 dma_addr_h = 0;
  202. #endif
  203. /* (DMA must be reset for Rx) */
  204. pciefd_can_writereg(priv, CANFD_CTL_RST_BIT, PCIEFD_REG_CAN_RX_CTL_SET);
  205. /* write the logical address of the Rx DMA area for this channel */
  206. pciefd_can_writereg(priv, (u32)priv->rx_dma_laddr,
  207. PCIEFD_REG_CAN_RX_DMA_ADDR_L);
  208. pciefd_can_writereg(priv, dma_addr_h, PCIEFD_REG_CAN_RX_DMA_ADDR_H);
  209. /* also indicates that Rx DMA is cacheable */
  210. pciefd_can_writereg(priv, CANFD_CTL_UNC_BIT, PCIEFD_REG_CAN_RX_CTL_CLR);
  211. }
  212. /* clear channel logical Rx DMA address from the board */
  213. static void pciefd_can_clear_rx_dma(struct pciefd_can *priv)
  214. {
  215. /* DMA must be reset for Rx */
  216. pciefd_can_writereg(priv, CANFD_CTL_RST_BIT, PCIEFD_REG_CAN_RX_CTL_SET);
  217. /* clear the logical address of the Rx DMA area for this channel */
  218. pciefd_can_writereg(priv, 0, PCIEFD_REG_CAN_RX_DMA_ADDR_L);
  219. pciefd_can_writereg(priv, 0, PCIEFD_REG_CAN_RX_DMA_ADDR_H);
  220. }
  221. /* give a channel logical Tx DMA address to the board */
  222. static void pciefd_can_setup_tx_dma(struct pciefd_can *priv)
  223. {
  224. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  225. const u32 dma_addr_h = (u32)(priv->tx_dma_laddr >> 32);
  226. #else
  227. const u32 dma_addr_h = 0;
  228. #endif
  229. /* (DMA must be reset for Tx) */
  230. pciefd_can_writereg(priv, CANFD_CTL_RST_BIT, PCIEFD_REG_CAN_TX_CTL_SET);
  231. /* write the logical address of the Tx DMA area for this channel */
  232. pciefd_can_writereg(priv, (u32)priv->tx_dma_laddr,
  233. PCIEFD_REG_CAN_TX_DMA_ADDR_L);
  234. pciefd_can_writereg(priv, dma_addr_h, PCIEFD_REG_CAN_TX_DMA_ADDR_H);
  235. /* also indicates that Tx DMA is cacheable */
  236. pciefd_can_writereg(priv, CANFD_CTL_UNC_BIT, PCIEFD_REG_CAN_TX_CTL_CLR);
  237. }
  238. /* clear channel logical Tx DMA address from the board */
  239. static void pciefd_can_clear_tx_dma(struct pciefd_can *priv)
  240. {
  241. /* DMA must be reset for Tx */
  242. pciefd_can_writereg(priv, CANFD_CTL_RST_BIT, PCIEFD_REG_CAN_TX_CTL_SET);
  243. /* clear the logical address of the Tx DMA area for this channel */
  244. pciefd_can_writereg(priv, 0, PCIEFD_REG_CAN_TX_DMA_ADDR_L);
  245. pciefd_can_writereg(priv, 0, PCIEFD_REG_CAN_TX_DMA_ADDR_H);
  246. }
  247. static void pciefd_can_ack_rx_dma(struct pciefd_can *priv)
  248. {
  249. /* read value of current IRQ tag and inc it for next one */
  250. priv->irq_tag = le32_to_cpu(*(__le32 *)priv->rx_dma_vaddr);
  251. priv->irq_tag++;
  252. priv->irq_tag &= 0xf;
  253. /* write the next IRQ tag for this CAN */
  254. pciefd_can_writereg(priv, priv->irq_tag, PCIEFD_REG_CAN_RX_CTL_ACK);
  255. }
  256. /* IRQ handler */
  257. static irqreturn_t pciefd_irq_handler(int irq, void *arg)
  258. {
  259. struct pciefd_can *priv = arg;
  260. struct pciefd_rx_dma *rx_dma = priv->rx_dma_vaddr;
  261. /* INTA mode only to sync with PCIe transaction */
  262. if (!pci_dev_msi_enabled(priv->board->pci_dev))
  263. (void)pciefd_sys_readreg(priv->board, PCIEFD_REG_SYS_VER1);
  264. /* read IRQ status from the first 32-bits of the Rx DMA area */
  265. priv->irq_status = le32_to_cpu(rx_dma->irq_status);
  266. /* check if this (shared) IRQ is for this CAN */
  267. if (pciefd_irq_tag(priv->irq_status) != priv->irq_tag)
  268. return IRQ_NONE;
  269. /* handle rx messages (if any) */
  270. peak_canfd_handle_msgs_list(&priv->ucan,
  271. rx_dma->msg,
  272. pciefd_irq_rx_cnt(priv->irq_status));
  273. /* handle tx link interrupt (if any) */
  274. if (pciefd_irq_is_lnk(priv->irq_status)) {
  275. unsigned long flags;
  276. spin_lock_irqsave(&priv->tx_lock, flags);
  277. priv->tx_pages_free++;
  278. spin_unlock_irqrestore(&priv->tx_lock, flags);
  279. /* wake producer up */
  280. netif_wake_queue(priv->ucan.ndev);
  281. }
  282. /* re-enable Rx DMA transfer for this CAN */
  283. pciefd_can_ack_rx_dma(priv);
  284. return IRQ_HANDLED;
  285. }
  286. static int pciefd_enable_tx_path(struct peak_canfd_priv *ucan)
  287. {
  288. struct pciefd_can *priv = (struct pciefd_can *)ucan;
  289. int i;
  290. /* initialize the Tx pages descriptors */
  291. priv->tx_pages_free = PCIEFD_TX_PAGE_COUNT - 1;
  292. priv->tx_page_index = 0;
  293. priv->tx_pages[0].vbase = priv->tx_dma_vaddr;
  294. priv->tx_pages[0].lbase = priv->tx_dma_laddr;
  295. for (i = 0; i < PCIEFD_TX_PAGE_COUNT; i++) {
  296. priv->tx_pages[i].offset = 0;
  297. priv->tx_pages[i].size = PCIEFD_TX_PAGE_SIZE -
  298. sizeof(struct pciefd_tx_link);
  299. if (i) {
  300. priv->tx_pages[i].vbase =
  301. priv->tx_pages[i - 1].vbase +
  302. PCIEFD_TX_PAGE_SIZE;
  303. priv->tx_pages[i].lbase =
  304. priv->tx_pages[i - 1].lbase +
  305. PCIEFD_TX_PAGE_SIZE;
  306. }
  307. }
  308. /* setup Tx DMA addresses into IP core */
  309. pciefd_can_setup_tx_dma(priv);
  310. /* start (TX_RST=0) Tx Path */
  311. pciefd_can_writereg(priv, CANFD_CTL_RST_BIT, PCIEFD_REG_CAN_TX_CTL_CLR);
  312. return 0;
  313. }
  314. /* board specific CANFD command pre-processing */
  315. static int pciefd_pre_cmd(struct peak_canfd_priv *ucan)
  316. {
  317. struct pciefd_can *priv = (struct pciefd_can *)ucan;
  318. u16 cmd = pucan_cmd_get_opcode(&priv->pucan_cmd);
  319. int err;
  320. /* pre-process command */
  321. switch (cmd) {
  322. case PUCAN_CMD_NORMAL_MODE:
  323. case PUCAN_CMD_LISTEN_ONLY_MODE:
  324. if (ucan->can.state == CAN_STATE_BUS_OFF)
  325. break;
  326. /* going into operational mode: setup IRQ handler */
  327. err = request_irq(priv->board->pci_dev->irq,
  328. pciefd_irq_handler,
  329. IRQF_SHARED,
  330. PCIEFD_DRV_NAME,
  331. priv);
  332. if (err)
  333. return err;
  334. /* setup Rx DMA address */
  335. pciefd_can_setup_rx_dma(priv);
  336. /* setup max count of msgs per IRQ */
  337. pciefd_can_writereg(priv, (CANFD_CTL_IRQ_TL_DEF) << 8 |
  338. CANFD_CTL_IRQ_CL_DEF,
  339. PCIEFD_REG_CAN_RX_CTL_WRT);
  340. /* clear DMA RST for Rx (Rx start) */
  341. pciefd_can_writereg(priv, CANFD_CTL_RST_BIT,
  342. PCIEFD_REG_CAN_RX_CTL_CLR);
  343. /* reset timestamps */
  344. pciefd_can_writereg(priv, !CANFD_MISC_TS_RST,
  345. PCIEFD_REG_CAN_MISC);
  346. /* do an initial ACK */
  347. pciefd_can_ack_rx_dma(priv);
  348. /* enable IRQ for this CAN after having set next irq_tag */
  349. pciefd_can_writereg(priv, CANFD_CTL_IEN_BIT,
  350. PCIEFD_REG_CAN_RX_CTL_SET);
  351. /* Tx path will be setup as soon as RX_BARRIER is received */
  352. break;
  353. default:
  354. break;
  355. }
  356. return 0;
  357. }
  358. /* write a command */
  359. static int pciefd_write_cmd(struct peak_canfd_priv *ucan)
  360. {
  361. struct pciefd_can *priv = (struct pciefd_can *)ucan;
  362. unsigned long flags;
  363. /* 64-bits command is atomic */
  364. spin_lock_irqsave(&priv->board->cmd_lock, flags);
  365. pciefd_can_writereg(priv, *(u32 *)ucan->cmd_buffer,
  366. PCIEFD_REG_CAN_CMD_PORT_L);
  367. pciefd_can_writereg(priv, *(u32 *)(ucan->cmd_buffer + 4),
  368. PCIEFD_REG_CAN_CMD_PORT_H);
  369. spin_unlock_irqrestore(&priv->board->cmd_lock, flags);
  370. return 0;
  371. }
  372. /* board specific CANFD command post-processing */
  373. static int pciefd_post_cmd(struct peak_canfd_priv *ucan)
  374. {
  375. struct pciefd_can *priv = (struct pciefd_can *)ucan;
  376. u16 cmd = pucan_cmd_get_opcode(&priv->pucan_cmd);
  377. switch (cmd) {
  378. case PUCAN_CMD_RESET_MODE:
  379. if (ucan->can.state == CAN_STATE_STOPPED)
  380. break;
  381. /* controller now in reset mode: */
  382. /* stop and reset DMA addresses in Tx/Rx engines */
  383. pciefd_can_clear_tx_dma(priv);
  384. pciefd_can_clear_rx_dma(priv);
  385. /* disable IRQ for this CAN */
  386. pciefd_can_writereg(priv, CANFD_CTL_IEN_BIT,
  387. PCIEFD_REG_CAN_RX_CTL_CLR);
  388. free_irq(priv->board->pci_dev->irq, priv);
  389. ucan->can.state = CAN_STATE_STOPPED;
  390. break;
  391. }
  392. return 0;
  393. }
  394. static void *pciefd_alloc_tx_msg(struct peak_canfd_priv *ucan, u16 msg_size,
  395. int *room_left)
  396. {
  397. struct pciefd_can *priv = (struct pciefd_can *)ucan;
  398. struct pciefd_page *page = priv->tx_pages + priv->tx_page_index;
  399. unsigned long flags;
  400. void *msg;
  401. spin_lock_irqsave(&priv->tx_lock, flags);
  402. if (page->offset + msg_size > page->size) {
  403. struct pciefd_tx_link *lk;
  404. /* not enough space in this page: try another one */
  405. if (!priv->tx_pages_free) {
  406. spin_unlock_irqrestore(&priv->tx_lock, flags);
  407. /* Tx overflow */
  408. return NULL;
  409. }
  410. priv->tx_pages_free--;
  411. /* keep address of the very last free slot of current page */
  412. lk = page->vbase + page->offset;
  413. /* next, move on a new free page */
  414. priv->tx_page_index = (priv->tx_page_index + 1) %
  415. PCIEFD_TX_PAGE_COUNT;
  416. page = priv->tx_pages + priv->tx_page_index;
  417. /* put link record to this new page at the end of prev one */
  418. lk->size = cpu_to_le16(sizeof(*lk));
  419. lk->type = cpu_to_le16(CANFD_MSG_LNK_TX);
  420. lk->laddr_lo = cpu_to_le32(page->lbase);
  421. #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
  422. lk->laddr_hi = cpu_to_le32(page->lbase >> 32);
  423. #else
  424. lk->laddr_hi = 0;
  425. #endif
  426. /* next msgs will be put from the begininng of this new page */
  427. page->offset = 0;
  428. }
  429. *room_left = priv->tx_pages_free * page->size;
  430. spin_unlock_irqrestore(&priv->tx_lock, flags);
  431. msg = page->vbase + page->offset;
  432. /* give back room left in the tx ring */
  433. *room_left += page->size - (page->offset + msg_size);
  434. return msg;
  435. }
  436. static int pciefd_write_tx_msg(struct peak_canfd_priv *ucan,
  437. struct pucan_tx_msg *msg)
  438. {
  439. struct pciefd_can *priv = (struct pciefd_can *)ucan;
  440. struct pciefd_page *page = priv->tx_pages + priv->tx_page_index;
  441. /* this slot is now reserved for writing the frame */
  442. page->offset += le16_to_cpu(msg->size);
  443. /* tell the board a frame has been written in Tx DMA area */
  444. pciefd_can_writereg(priv, 1, PCIEFD_REG_CAN_TX_REQ_ACC);
  445. return 0;
  446. }
  447. /* probe for CAN-FD channel #pciefd_board->can_count */
  448. static int pciefd_can_probe(struct pciefd_board *pciefd)
  449. {
  450. struct net_device *ndev;
  451. struct pciefd_can *priv;
  452. u32 clk;
  453. int err;
  454. /* allocate the candev object with default isize of echo skbs ring */
  455. ndev = alloc_peak_canfd_dev(sizeof(*priv), pciefd->can_count,
  456. PCIEFD_ECHO_SKB_MAX);
  457. if (!ndev) {
  458. dev_err(&pciefd->pci_dev->dev,
  459. "failed to alloc candev object\n");
  460. goto failure;
  461. }
  462. priv = netdev_priv(ndev);
  463. /* fill-in candev private object: */
  464. /* setup PCIe-FD own callbacks */
  465. priv->ucan.pre_cmd = pciefd_pre_cmd;
  466. priv->ucan.write_cmd = pciefd_write_cmd;
  467. priv->ucan.post_cmd = pciefd_post_cmd;
  468. priv->ucan.enable_tx_path = pciefd_enable_tx_path;
  469. priv->ucan.alloc_tx_msg = pciefd_alloc_tx_msg;
  470. priv->ucan.write_tx_msg = pciefd_write_tx_msg;
  471. /* setup PCIe-FD own command buffer */
  472. priv->ucan.cmd_buffer = &priv->pucan_cmd;
  473. priv->ucan.cmd_maxlen = sizeof(priv->pucan_cmd);
  474. priv->board = pciefd;
  475. /* CAN config regs block address */
  476. priv->reg_base = pciefd->reg_base + PCIEFD_CANX_OFF(priv->ucan.index);
  477. /* allocate non-cacheable DMA'able 4KB memory area for Rx */
  478. priv->rx_dma_vaddr = dmam_alloc_coherent(&pciefd->pci_dev->dev,
  479. PCIEFD_RX_DMA_SIZE,
  480. &priv->rx_dma_laddr,
  481. GFP_KERNEL);
  482. if (!priv->rx_dma_vaddr) {
  483. dev_err(&pciefd->pci_dev->dev,
  484. "Rx dmam_alloc_coherent(%u) failure\n",
  485. PCIEFD_RX_DMA_SIZE);
  486. goto err_free_candev;
  487. }
  488. /* allocate non-cacheable DMA'able 4KB memory area for Tx */
  489. priv->tx_dma_vaddr = dmam_alloc_coherent(&pciefd->pci_dev->dev,
  490. PCIEFD_TX_DMA_SIZE,
  491. &priv->tx_dma_laddr,
  492. GFP_KERNEL);
  493. if (!priv->tx_dma_vaddr) {
  494. dev_err(&pciefd->pci_dev->dev,
  495. "Tx dmaim_alloc_coherent(%u) failure\n",
  496. PCIEFD_TX_DMA_SIZE);
  497. goto err_free_candev;
  498. }
  499. /* CAN clock in RST mode */
  500. pciefd_can_writereg(priv, CANFD_MISC_TS_RST, PCIEFD_REG_CAN_MISC);
  501. /* read current clock value */
  502. clk = pciefd_can_readreg(priv, PCIEFD_REG_CAN_CLK_SEL);
  503. switch (clk) {
  504. case CANFD_CLK_SEL_20MHZ:
  505. priv->ucan.can.clock.freq = 20 * 1000 * 1000;
  506. break;
  507. case CANFD_CLK_SEL_24MHZ:
  508. priv->ucan.can.clock.freq = 24 * 1000 * 1000;
  509. break;
  510. case CANFD_CLK_SEL_30MHZ:
  511. priv->ucan.can.clock.freq = 30 * 1000 * 1000;
  512. break;
  513. case CANFD_CLK_SEL_40MHZ:
  514. priv->ucan.can.clock.freq = 40 * 1000 * 1000;
  515. break;
  516. case CANFD_CLK_SEL_60MHZ:
  517. priv->ucan.can.clock.freq = 60 * 1000 * 1000;
  518. break;
  519. default:
  520. pciefd_can_writereg(priv, CANFD_CLK_SEL_80MHZ,
  521. PCIEFD_REG_CAN_CLK_SEL);
  522. /* fallthough */
  523. case CANFD_CLK_SEL_80MHZ:
  524. priv->ucan.can.clock.freq = 80 * 1000 * 1000;
  525. break;
  526. }
  527. ndev->irq = pciefd->pci_dev->irq;
  528. SET_NETDEV_DEV(ndev, &pciefd->pci_dev->dev);
  529. err = register_candev(ndev);
  530. if (err) {
  531. dev_err(&pciefd->pci_dev->dev,
  532. "couldn't register CAN device: %d\n", err);
  533. goto err_free_candev;
  534. }
  535. spin_lock_init(&priv->tx_lock);
  536. /* save the object address in the board structure */
  537. pciefd->can[pciefd->can_count] = priv;
  538. dev_info(&pciefd->pci_dev->dev, "%s at reg_base=0x%p irq=%d\n",
  539. ndev->name, priv->reg_base, pciefd->pci_dev->irq);
  540. return 0;
  541. err_free_candev:
  542. free_candev(ndev);
  543. failure:
  544. return -ENOMEM;
  545. }
  546. /* remove a CAN-FD channel by releasing all of its resources */
  547. static void pciefd_can_remove(struct pciefd_can *priv)
  548. {
  549. /* unregister (close) the can device to go back to RST mode first */
  550. unregister_candev(priv->ucan.ndev);
  551. /* finally, free the candev object */
  552. free_candev(priv->ucan.ndev);
  553. }
  554. /* remove all CAN-FD channels by releasing their own resources */
  555. static void pciefd_can_remove_all(struct pciefd_board *pciefd)
  556. {
  557. while (pciefd->can_count > 0)
  558. pciefd_can_remove(pciefd->can[--pciefd->can_count]);
  559. }
  560. /* probe for the entire device */
  561. static int peak_pciefd_probe(struct pci_dev *pdev,
  562. const struct pci_device_id *ent)
  563. {
  564. struct pciefd_board *pciefd;
  565. int err, can_count;
  566. u16 sub_sys_id;
  567. u8 hw_ver_major;
  568. u8 hw_ver_minor;
  569. u8 hw_ver_sub;
  570. u32 v2;
  571. err = pci_enable_device(pdev);
  572. if (err)
  573. return err;
  574. err = pci_request_regions(pdev, PCIEFD_DRV_NAME);
  575. if (err)
  576. goto err_disable_pci;
  577. /* the number of channels depends on sub-system id */
  578. err = pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &sub_sys_id);
  579. if (err)
  580. goto err_release_regions;
  581. dev_dbg(&pdev->dev, "probing device %04x:%04x:%04x\n",
  582. pdev->vendor, pdev->device, sub_sys_id);
  583. if (sub_sys_id >= 0x0012)
  584. can_count = 4;
  585. else if (sub_sys_id >= 0x0010)
  586. can_count = 3;
  587. else if (sub_sys_id >= 0x0004)
  588. can_count = 2;
  589. else
  590. can_count = 1;
  591. /* allocate board structure object */
  592. pciefd = devm_kzalloc(&pdev->dev, sizeof(*pciefd) +
  593. can_count * sizeof(*pciefd->can),
  594. GFP_KERNEL);
  595. if (!pciefd) {
  596. err = -ENOMEM;
  597. goto err_release_regions;
  598. }
  599. /* initialize the board structure */
  600. pciefd->pci_dev = pdev;
  601. spin_lock_init(&pciefd->cmd_lock);
  602. /* save the PCI BAR0 virtual address for further system regs access */
  603. pciefd->reg_base = pci_iomap(pdev, 0, PCIEFD_BAR0_SIZE);
  604. if (!pciefd->reg_base) {
  605. dev_err(&pdev->dev, "failed to map PCI resource #0\n");
  606. err = -ENOMEM;
  607. goto err_release_regions;
  608. }
  609. /* read the firmware version number */
  610. v2 = pciefd_sys_readreg(pciefd, PCIEFD_REG_SYS_VER2);
  611. hw_ver_major = (v2 & 0x0000f000) >> 12;
  612. hw_ver_minor = (v2 & 0x00000f00) >> 8;
  613. hw_ver_sub = (v2 & 0x000000f0) >> 4;
  614. dev_info(&pdev->dev,
  615. "%ux CAN-FD PCAN-PCIe FPGA v%u.%u.%u:\n", can_count,
  616. hw_ver_major, hw_ver_minor, hw_ver_sub);
  617. /* stop system clock */
  618. pciefd_sys_writereg(pciefd, PCIEFD_SYS_CTL_CLK_EN,
  619. PCIEFD_REG_SYS_CTL_CLR);
  620. pci_set_master(pdev);
  621. /* create now the corresponding channels objects */
  622. while (pciefd->can_count < can_count) {
  623. err = pciefd_can_probe(pciefd);
  624. if (err)
  625. goto err_free_canfd;
  626. pciefd->can_count++;
  627. }
  628. /* set system timestamps counter in RST mode */
  629. pciefd_sys_writereg(pciefd, PCIEFD_SYS_CTL_TS_RST,
  630. PCIEFD_REG_SYS_CTL_SET);
  631. /* wait a bit (read cycle) */
  632. (void)pciefd_sys_readreg(pciefd, PCIEFD_REG_SYS_VER1);
  633. /* free all clocks */
  634. pciefd_sys_writereg(pciefd, PCIEFD_SYS_CTL_TS_RST,
  635. PCIEFD_REG_SYS_CTL_CLR);
  636. /* start system clock */
  637. pciefd_sys_writereg(pciefd, PCIEFD_SYS_CTL_CLK_EN,
  638. PCIEFD_REG_SYS_CTL_SET);
  639. /* remember the board structure address in the device user data */
  640. pci_set_drvdata(pdev, pciefd);
  641. return 0;
  642. err_free_canfd:
  643. pciefd_can_remove_all(pciefd);
  644. pci_iounmap(pdev, pciefd->reg_base);
  645. err_release_regions:
  646. pci_release_regions(pdev);
  647. err_disable_pci:
  648. pci_disable_device(pdev);
  649. return err;
  650. }
  651. /* free the board structure object, as well as its resources: */
  652. static void peak_pciefd_remove(struct pci_dev *pdev)
  653. {
  654. struct pciefd_board *pciefd = pci_get_drvdata(pdev);
  655. /* release CAN-FD channels resources */
  656. pciefd_can_remove_all(pciefd);
  657. pci_iounmap(pdev, pciefd->reg_base);
  658. pci_release_regions(pdev);
  659. pci_disable_device(pdev);
  660. }
  661. static struct pci_driver peak_pciefd_driver = {
  662. .name = PCIEFD_DRV_NAME,
  663. .id_table = peak_pciefd_tbl,
  664. .probe = peak_pciefd_probe,
  665. .remove = peak_pciefd_remove,
  666. };
  667. module_pci_driver(peak_pciefd_driver);