peak_pciefd_main.c 25 KB

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