carma-fpga.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  1. /*
  2. * CARMA DATA-FPGA Access Driver
  3. *
  4. * Copyright (c) 2009-2011 Ira W. Snyder <iws@ovro.caltech.edu>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. */
  11. /*
  12. * FPGA Memory Dump Format
  13. *
  14. * FPGA #0 control registers (32 x 32-bit words)
  15. * FPGA #1 control registers (32 x 32-bit words)
  16. * FPGA #2 control registers (32 x 32-bit words)
  17. * FPGA #3 control registers (32 x 32-bit words)
  18. * SYSFPGA control registers (32 x 32-bit words)
  19. * FPGA #0 correlation array (NUM_CORL0 correlation blocks)
  20. * FPGA #1 correlation array (NUM_CORL1 correlation blocks)
  21. * FPGA #2 correlation array (NUM_CORL2 correlation blocks)
  22. * FPGA #3 correlation array (NUM_CORL3 correlation blocks)
  23. *
  24. * Each correlation array consists of:
  25. *
  26. * Correlation Data (2 x NUM_LAGSn x 32-bit words)
  27. * Pipeline Metadata (2 x NUM_METAn x 32-bit words)
  28. * Quantization Counters (2 x NUM_QCNTn x 32-bit words)
  29. *
  30. * The NUM_CORLn, NUM_LAGSn, NUM_METAn, and NUM_QCNTn values come from
  31. * the FPGA configuration registers. They do not change once the FPGA's
  32. * have been programmed, they only change on re-programming.
  33. */
  34. /*
  35. * Basic Description:
  36. *
  37. * This driver is used to capture correlation spectra off of the four data
  38. * processing FPGAs. The FPGAs are often reprogrammed at runtime, therefore
  39. * this driver supports dynamic enable/disable of capture while the device
  40. * remains open.
  41. *
  42. * The nominal capture rate is 64Hz (every 15.625ms). To facilitate this fast
  43. * capture rate, all buffers are pre-allocated to avoid any potentially long
  44. * running memory allocations while capturing.
  45. *
  46. * There are two lists and one pointer which are used to keep track of the
  47. * different states of data buffers.
  48. *
  49. * 1) free list
  50. * This list holds all empty data buffers which are ready to receive data.
  51. *
  52. * 2) inflight pointer
  53. * This pointer holds the currently inflight data buffer. This buffer is having
  54. * data copied into it by the DMA engine.
  55. *
  56. * 3) used list
  57. * This list holds data buffers which have been filled, and are waiting to be
  58. * read by userspace.
  59. *
  60. * All buffers start life on the free list, then move successively to the
  61. * inflight pointer, and then to the used list. After they have been read by
  62. * userspace, they are moved back to the free list. The cycle repeats as long
  63. * as necessary.
  64. *
  65. * It should be noted that all buffers are mapped and ready for DMA when they
  66. * are on any of the three lists. They are only unmapped when they are in the
  67. * process of being read by userspace.
  68. */
  69. /*
  70. * Notes on the IRQ masking scheme:
  71. *
  72. * The IRQ masking scheme here is different than most other hardware. The only
  73. * way for the DATA-FPGAs to detect if the kernel has taken too long to copy
  74. * the data is if the status registers are not cleared before the next
  75. * correlation data dump is ready.
  76. *
  77. * The interrupt line is connected to the status registers, such that when they
  78. * are cleared, the interrupt is de-asserted. Therein lies our problem. We need
  79. * to schedule a long-running DMA operation and return from the interrupt
  80. * handler quickly, but we cannot clear the status registers.
  81. *
  82. * To handle this, the system controller FPGA has the capability to connect the
  83. * interrupt line to a user-controlled GPIO pin. This pin is driven high
  84. * (unasserted) and left that way. To mask the interrupt, we change the
  85. * interrupt source to the GPIO pin. Tada, we hid the interrupt. :)
  86. */
  87. #include <linux/of_address.h>
  88. #include <linux/of_irq.h>
  89. #include <linux/of_platform.h>
  90. #include <linux/dma-mapping.h>
  91. #include <linux/miscdevice.h>
  92. #include <linux/interrupt.h>
  93. #include <linux/dmaengine.h>
  94. #include <linux/seq_file.h>
  95. #include <linux/highmem.h>
  96. #include <linux/debugfs.h>
  97. #include <linux/kernel.h>
  98. #include <linux/module.h>
  99. #include <linux/poll.h>
  100. #include <linux/init.h>
  101. #include <linux/slab.h>
  102. #include <linux/kref.h>
  103. #include <linux/io.h>
  104. #include <media/videobuf-dma-sg.h>
  105. /* system controller registers */
  106. #define SYS_IRQ_SOURCE_CTL 0x24
  107. #define SYS_IRQ_OUTPUT_EN 0x28
  108. #define SYS_IRQ_OUTPUT_DATA 0x2C
  109. #define SYS_IRQ_INPUT_DATA 0x30
  110. #define SYS_FPGA_CONFIG_STATUS 0x44
  111. /* GPIO IRQ line assignment */
  112. #define IRQ_CORL_DONE 0x10
  113. /* FPGA registers */
  114. #define MMAP_REG_VERSION 0x00
  115. #define MMAP_REG_CORL_CONF1 0x08
  116. #define MMAP_REG_CORL_CONF2 0x0C
  117. #define MMAP_REG_STATUS 0x48
  118. #define SYS_FPGA_BLOCK 0xF0000000
  119. #define DATA_FPGA_START 0x400000
  120. #define DATA_FPGA_SIZE 0x80000
  121. static const char drv_name[] = "carma-fpga";
  122. #define NUM_FPGA 4
  123. #define MIN_DATA_BUFS 8
  124. #define MAX_DATA_BUFS 64
  125. struct fpga_info {
  126. unsigned int num_lag_ram;
  127. unsigned int blk_size;
  128. };
  129. struct data_buf {
  130. struct list_head entry;
  131. struct videobuf_dmabuf vb;
  132. size_t size;
  133. };
  134. struct fpga_device {
  135. /* character device */
  136. struct miscdevice miscdev;
  137. struct device *dev;
  138. struct mutex mutex;
  139. /* reference count */
  140. struct kref ref;
  141. /* FPGA registers and information */
  142. struct fpga_info info[NUM_FPGA];
  143. void __iomem *regs;
  144. int irq;
  145. /* FPGA Physical Address/Size Information */
  146. resource_size_t phys_addr;
  147. size_t phys_size;
  148. /* DMA structures */
  149. struct sg_table corl_table;
  150. unsigned int corl_nents;
  151. struct dma_chan *chan;
  152. /* Protection for all members below */
  153. spinlock_t lock;
  154. /* Device enable/disable flag */
  155. bool enabled;
  156. /* Correlation data buffers */
  157. wait_queue_head_t wait;
  158. struct list_head free;
  159. struct list_head used;
  160. struct data_buf *inflight;
  161. /* Information about data buffers */
  162. unsigned int num_dropped;
  163. unsigned int num_buffers;
  164. size_t bufsize;
  165. struct dentry *dbg_entry;
  166. };
  167. struct fpga_reader {
  168. struct fpga_device *priv;
  169. struct data_buf *buf;
  170. off_t buf_start;
  171. };
  172. static void fpga_device_release(struct kref *ref)
  173. {
  174. struct fpga_device *priv = container_of(ref, struct fpga_device, ref);
  175. /* the last reader has exited, cleanup the last bits */
  176. mutex_destroy(&priv->mutex);
  177. kfree(priv);
  178. }
  179. /*
  180. * Data Buffer Allocation Helpers
  181. */
  182. /**
  183. * data_free_buffer() - free a single data buffer and all allocated memory
  184. * @buf: the buffer to free
  185. *
  186. * This will free all of the pages allocated to the given data buffer, and
  187. * then free the structure itself
  188. */
  189. static void data_free_buffer(struct data_buf *buf)
  190. {
  191. /* It is ok to free a NULL buffer */
  192. if (!buf)
  193. return;
  194. /* free all memory */
  195. videobuf_dma_free(&buf->vb);
  196. kfree(buf);
  197. }
  198. /**
  199. * data_alloc_buffer() - allocate and fill a data buffer with pages
  200. * @bytes: the number of bytes required
  201. *
  202. * This allocates all space needed for a data buffer. It must be mapped before
  203. * use in a DMA transaction using videobuf_dma_map().
  204. *
  205. * Returns NULL on failure
  206. */
  207. static struct data_buf *data_alloc_buffer(const size_t bytes)
  208. {
  209. unsigned int nr_pages;
  210. struct data_buf *buf;
  211. int ret;
  212. /* calculate the number of pages necessary */
  213. nr_pages = DIV_ROUND_UP(bytes, PAGE_SIZE);
  214. /* allocate the buffer structure */
  215. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  216. if (!buf)
  217. goto out_return;
  218. /* initialize internal fields */
  219. INIT_LIST_HEAD(&buf->entry);
  220. buf->size = bytes;
  221. /* allocate the videobuf */
  222. videobuf_dma_init(&buf->vb);
  223. ret = videobuf_dma_init_kernel(&buf->vb, DMA_FROM_DEVICE, nr_pages);
  224. if (ret)
  225. goto out_free_buf;
  226. return buf;
  227. out_free_buf:
  228. kfree(buf);
  229. out_return:
  230. return NULL;
  231. }
  232. /**
  233. * data_free_buffers() - free all allocated buffers
  234. * @priv: the driver's private data structure
  235. *
  236. * Free all buffers allocated by the driver (except those currently in the
  237. * process of being read by userspace).
  238. *
  239. * LOCKING: must hold dev->mutex
  240. * CONTEXT: user
  241. */
  242. static void data_free_buffers(struct fpga_device *priv)
  243. {
  244. struct data_buf *buf, *tmp;
  245. /* the device should be stopped, no DMA in progress */
  246. BUG_ON(priv->inflight != NULL);
  247. list_for_each_entry_safe(buf, tmp, &priv->free, entry) {
  248. list_del_init(&buf->entry);
  249. videobuf_dma_unmap(priv->dev, &buf->vb);
  250. data_free_buffer(buf);
  251. }
  252. list_for_each_entry_safe(buf, tmp, &priv->used, entry) {
  253. list_del_init(&buf->entry);
  254. videobuf_dma_unmap(priv->dev, &buf->vb);
  255. data_free_buffer(buf);
  256. }
  257. priv->num_buffers = 0;
  258. priv->bufsize = 0;
  259. }
  260. /**
  261. * data_alloc_buffers() - allocate 1 seconds worth of data buffers
  262. * @priv: the driver's private data structure
  263. *
  264. * Allocate enough buffers for a whole second worth of data
  265. *
  266. * This routine will attempt to degrade nicely by succeeding even if a full
  267. * second worth of data buffers could not be allocated, as long as a minimum
  268. * number were allocated. In this case, it will print a message to the kernel
  269. * log.
  270. *
  271. * The device must not be modifying any lists when this is called.
  272. *
  273. * CONTEXT: user
  274. * LOCKING: must hold dev->mutex
  275. *
  276. * Returns 0 on success, -ERRNO otherwise
  277. */
  278. static int data_alloc_buffers(struct fpga_device *priv)
  279. {
  280. struct data_buf *buf;
  281. int i, ret;
  282. for (i = 0; i < MAX_DATA_BUFS; i++) {
  283. /* allocate a buffer */
  284. buf = data_alloc_buffer(priv->bufsize);
  285. if (!buf)
  286. break;
  287. /* map it for DMA */
  288. ret = videobuf_dma_map(priv->dev, &buf->vb);
  289. if (ret) {
  290. data_free_buffer(buf);
  291. break;
  292. }
  293. /* add it to the list of free buffers */
  294. list_add_tail(&buf->entry, &priv->free);
  295. priv->num_buffers++;
  296. }
  297. /* Make sure we allocated the minimum required number of buffers */
  298. if (priv->num_buffers < MIN_DATA_BUFS) {
  299. dev_err(priv->dev, "Unable to allocate enough data buffers\n");
  300. data_free_buffers(priv);
  301. return -ENOMEM;
  302. }
  303. /* Warn if we are running in a degraded state, but do not fail */
  304. if (priv->num_buffers < MAX_DATA_BUFS) {
  305. dev_warn(priv->dev,
  306. "Unable to allocate %d buffers, using %d buffers instead\n",
  307. MAX_DATA_BUFS, i);
  308. }
  309. return 0;
  310. }
  311. /*
  312. * DMA Operations Helpers
  313. */
  314. /**
  315. * fpga_start_addr() - get the physical address a DATA-FPGA
  316. * @priv: the driver's private data structure
  317. * @fpga: the DATA-FPGA number (zero based)
  318. */
  319. static dma_addr_t fpga_start_addr(struct fpga_device *priv, unsigned int fpga)
  320. {
  321. return priv->phys_addr + 0x400000 + (0x80000 * fpga);
  322. }
  323. /**
  324. * fpga_block_addr() - get the physical address of a correlation data block
  325. * @priv: the driver's private data structure
  326. * @fpga: the DATA-FPGA number (zero based)
  327. * @blknum: the correlation block number (zero based)
  328. */
  329. static dma_addr_t fpga_block_addr(struct fpga_device *priv, unsigned int fpga,
  330. unsigned int blknum)
  331. {
  332. return fpga_start_addr(priv, fpga) + (0x10000 * (1 + blknum));
  333. }
  334. #define REG_BLOCK_SIZE (32 * 4)
  335. /**
  336. * data_setup_corl_table() - create the scatterlist for correlation dumps
  337. * @priv: the driver's private data structure
  338. *
  339. * Create the scatterlist for transferring a correlation dump from the
  340. * DATA FPGAs. This structure will be reused for each buffer than needs
  341. * to be filled with correlation data.
  342. *
  343. * Returns 0 on success, -ERRNO otherwise
  344. */
  345. static int data_setup_corl_table(struct fpga_device *priv)
  346. {
  347. struct sg_table *table = &priv->corl_table;
  348. struct scatterlist *sg;
  349. struct fpga_info *info;
  350. int i, j, ret;
  351. /* Calculate the number of entries needed */
  352. priv->corl_nents = (1 + NUM_FPGA) * REG_BLOCK_SIZE;
  353. for (i = 0; i < NUM_FPGA; i++)
  354. priv->corl_nents += priv->info[i].num_lag_ram;
  355. /* Allocate the scatterlist table */
  356. ret = sg_alloc_table(table, priv->corl_nents, GFP_KERNEL);
  357. if (ret) {
  358. dev_err(priv->dev, "unable to allocate DMA table\n");
  359. return ret;
  360. }
  361. /* Add the DATA FPGA registers to the scatterlist */
  362. sg = table->sgl;
  363. for (i = 0; i < NUM_FPGA; i++) {
  364. sg_dma_address(sg) = fpga_start_addr(priv, i);
  365. sg_dma_len(sg) = REG_BLOCK_SIZE;
  366. sg = sg_next(sg);
  367. }
  368. /* Add the SYS-FPGA registers to the scatterlist */
  369. sg_dma_address(sg) = SYS_FPGA_BLOCK;
  370. sg_dma_len(sg) = REG_BLOCK_SIZE;
  371. sg = sg_next(sg);
  372. /* Add the FPGA correlation data blocks to the scatterlist */
  373. for (i = 0; i < NUM_FPGA; i++) {
  374. info = &priv->info[i];
  375. for (j = 0; j < info->num_lag_ram; j++) {
  376. sg_dma_address(sg) = fpga_block_addr(priv, i, j);
  377. sg_dma_len(sg) = info->blk_size;
  378. sg = sg_next(sg);
  379. }
  380. }
  381. /*
  382. * All physical addresses and lengths are present in the structure
  383. * now. It can be reused for every FPGA DATA interrupt
  384. */
  385. return 0;
  386. }
  387. /*
  388. * FPGA Register Access Helpers
  389. */
  390. static void fpga_write_reg(struct fpga_device *priv, unsigned int fpga,
  391. unsigned int reg, u32 val)
  392. {
  393. const int fpga_start = DATA_FPGA_START + (fpga * DATA_FPGA_SIZE);
  394. iowrite32be(val, priv->regs + fpga_start + reg);
  395. }
  396. static u32 fpga_read_reg(struct fpga_device *priv, unsigned int fpga,
  397. unsigned int reg)
  398. {
  399. const int fpga_start = DATA_FPGA_START + (fpga * DATA_FPGA_SIZE);
  400. return ioread32be(priv->regs + fpga_start + reg);
  401. }
  402. /**
  403. * data_calculate_bufsize() - calculate the data buffer size required
  404. * @priv: the driver's private data structure
  405. *
  406. * Calculate the total buffer size needed to hold a single block
  407. * of correlation data
  408. *
  409. * CONTEXT: user
  410. *
  411. * Returns 0 on success, -ERRNO otherwise
  412. */
  413. static int data_calculate_bufsize(struct fpga_device *priv)
  414. {
  415. u32 num_corl, num_lags, num_meta, num_qcnt, num_pack;
  416. u32 conf1, conf2, version;
  417. u32 num_lag_ram, blk_size;
  418. int i;
  419. /* Each buffer starts with the 5 FPGA register areas */
  420. priv->bufsize = (1 + NUM_FPGA) * REG_BLOCK_SIZE;
  421. /* Read and store the configuration data for each FPGA */
  422. for (i = 0; i < NUM_FPGA; i++) {
  423. version = fpga_read_reg(priv, i, MMAP_REG_VERSION);
  424. conf1 = fpga_read_reg(priv, i, MMAP_REG_CORL_CONF1);
  425. conf2 = fpga_read_reg(priv, i, MMAP_REG_CORL_CONF2);
  426. /* minor version 2 and later */
  427. if ((version & 0x000000FF) >= 2) {
  428. num_corl = (conf1 & 0x000000F0) >> 4;
  429. num_pack = (conf1 & 0x00000F00) >> 8;
  430. num_lags = (conf1 & 0x00FFF000) >> 12;
  431. num_meta = (conf1 & 0x7F000000) >> 24;
  432. num_qcnt = (conf2 & 0x00000FFF) >> 0;
  433. } else {
  434. num_corl = (conf1 & 0x000000F0) >> 4;
  435. num_pack = 1; /* implied */
  436. num_lags = (conf1 & 0x000FFF00) >> 8;
  437. num_meta = (conf1 & 0x7FF00000) >> 20;
  438. num_qcnt = (conf2 & 0x00000FFF) >> 0;
  439. }
  440. num_lag_ram = (num_corl + num_pack - 1) / num_pack;
  441. blk_size = ((num_pack * num_lags) + num_meta + num_qcnt) * 8;
  442. priv->info[i].num_lag_ram = num_lag_ram;
  443. priv->info[i].blk_size = blk_size;
  444. priv->bufsize += num_lag_ram * blk_size;
  445. dev_dbg(priv->dev, "FPGA %d NUM_CORL: %d\n", i, num_corl);
  446. dev_dbg(priv->dev, "FPGA %d NUM_PACK: %d\n", i, num_pack);
  447. dev_dbg(priv->dev, "FPGA %d NUM_LAGS: %d\n", i, num_lags);
  448. dev_dbg(priv->dev, "FPGA %d NUM_META: %d\n", i, num_meta);
  449. dev_dbg(priv->dev, "FPGA %d NUM_QCNT: %d\n", i, num_qcnt);
  450. dev_dbg(priv->dev, "FPGA %d BLK_SIZE: %d\n", i, blk_size);
  451. }
  452. dev_dbg(priv->dev, "TOTAL BUFFER SIZE: %zu bytes\n", priv->bufsize);
  453. return 0;
  454. }
  455. /*
  456. * Interrupt Handling
  457. */
  458. /**
  459. * data_disable_interrupts() - stop the device from generating interrupts
  460. * @priv: the driver's private data structure
  461. *
  462. * Hide interrupts by switching to GPIO interrupt source
  463. *
  464. * LOCKING: must hold dev->lock
  465. */
  466. static void data_disable_interrupts(struct fpga_device *priv)
  467. {
  468. /* hide the interrupt by switching the IRQ driver to GPIO */
  469. iowrite32be(0x2F, priv->regs + SYS_IRQ_SOURCE_CTL);
  470. }
  471. /**
  472. * data_enable_interrupts() - allow the device to generate interrupts
  473. * @priv: the driver's private data structure
  474. *
  475. * Unhide interrupts by switching to the FPGA interrupt source. At the
  476. * same time, clear the DATA-FPGA status registers.
  477. *
  478. * LOCKING: must hold dev->lock
  479. */
  480. static void data_enable_interrupts(struct fpga_device *priv)
  481. {
  482. /* clear the actual FPGA corl_done interrupt */
  483. fpga_write_reg(priv, 0, MMAP_REG_STATUS, 0x0);
  484. fpga_write_reg(priv, 1, MMAP_REG_STATUS, 0x0);
  485. fpga_write_reg(priv, 2, MMAP_REG_STATUS, 0x0);
  486. fpga_write_reg(priv, 3, MMAP_REG_STATUS, 0x0);
  487. /* flush the writes */
  488. fpga_read_reg(priv, 0, MMAP_REG_STATUS);
  489. fpga_read_reg(priv, 1, MMAP_REG_STATUS);
  490. fpga_read_reg(priv, 2, MMAP_REG_STATUS);
  491. fpga_read_reg(priv, 3, MMAP_REG_STATUS);
  492. /* switch back to the external interrupt source */
  493. iowrite32be(0x3F, priv->regs + SYS_IRQ_SOURCE_CTL);
  494. }
  495. /**
  496. * data_dma_cb() - DMAEngine callback for DMA completion
  497. * @data: the driver's private data structure
  498. *
  499. * Complete a DMA transfer from the DATA-FPGA's
  500. *
  501. * This is called via the DMA callback mechanism, and will handle moving the
  502. * completed DMA transaction to the used list, and then wake any processes
  503. * waiting for new data
  504. *
  505. * CONTEXT: any, softirq expected
  506. */
  507. static void data_dma_cb(void *data)
  508. {
  509. struct fpga_device *priv = data;
  510. unsigned long flags;
  511. spin_lock_irqsave(&priv->lock, flags);
  512. /* If there is no inflight buffer, we've got a bug */
  513. BUG_ON(priv->inflight == NULL);
  514. /* Move the inflight buffer onto the used list */
  515. list_move_tail(&priv->inflight->entry, &priv->used);
  516. priv->inflight = NULL;
  517. /*
  518. * If data dumping is still enabled, then clear the FPGA
  519. * status registers and re-enable FPGA interrupts
  520. */
  521. if (priv->enabled)
  522. data_enable_interrupts(priv);
  523. spin_unlock_irqrestore(&priv->lock, flags);
  524. /*
  525. * We've changed both the inflight and used lists, so we need
  526. * to wake up any processes that are blocking for those events
  527. */
  528. wake_up(&priv->wait);
  529. }
  530. /**
  531. * data_submit_dma() - prepare and submit the required DMA to fill a buffer
  532. * @priv: the driver's private data structure
  533. * @buf: the data buffer
  534. *
  535. * Prepare and submit the necessary DMA transactions to fill a correlation
  536. * data buffer.
  537. *
  538. * LOCKING: must hold dev->lock
  539. * CONTEXT: hardirq only
  540. *
  541. * Returns 0 on success, -ERRNO otherwise
  542. */
  543. static int data_submit_dma(struct fpga_device *priv, struct data_buf *buf)
  544. {
  545. struct scatterlist *dst_sg, *src_sg;
  546. unsigned int dst_nents, src_nents;
  547. struct dma_chan *chan = priv->chan;
  548. struct dma_async_tx_descriptor *tx;
  549. dma_cookie_t cookie;
  550. dma_addr_t dst, src;
  551. unsigned long dma_flags = DMA_COMPL_SKIP_DEST_UNMAP |
  552. DMA_COMPL_SKIP_SRC_UNMAP;
  553. dst_sg = buf->vb.sglist;
  554. dst_nents = buf->vb.sglen;
  555. src_sg = priv->corl_table.sgl;
  556. src_nents = priv->corl_nents;
  557. /*
  558. * All buffers passed to this function should be ready and mapped
  559. * for DMA already. Therefore, we don't need to do anything except
  560. * submit it to the Freescale DMA Engine for processing
  561. */
  562. /* setup the scatterlist to scatterlist transfer */
  563. tx = chan->device->device_prep_dma_sg(chan,
  564. dst_sg, dst_nents,
  565. src_sg, src_nents,
  566. 0);
  567. if (!tx) {
  568. dev_err(priv->dev, "unable to prep scatterlist DMA\n");
  569. return -ENOMEM;
  570. }
  571. /* submit the transaction to the DMA controller */
  572. cookie = tx->tx_submit(tx);
  573. if (dma_submit_error(cookie)) {
  574. dev_err(priv->dev, "unable to submit scatterlist DMA\n");
  575. return -ENOMEM;
  576. }
  577. /* Prepare the re-read of the SYS-FPGA block */
  578. dst = sg_dma_address(dst_sg) + (NUM_FPGA * REG_BLOCK_SIZE);
  579. src = SYS_FPGA_BLOCK;
  580. tx = chan->device->device_prep_dma_memcpy(chan, dst, src,
  581. REG_BLOCK_SIZE,
  582. dma_flags);
  583. if (!tx) {
  584. dev_err(priv->dev, "unable to prep SYS-FPGA DMA\n");
  585. return -ENOMEM;
  586. }
  587. /* Setup the callback */
  588. tx->callback = data_dma_cb;
  589. tx->callback_param = priv;
  590. /* submit the transaction to the DMA controller */
  591. cookie = tx->tx_submit(tx);
  592. if (dma_submit_error(cookie)) {
  593. dev_err(priv->dev, "unable to submit SYS-FPGA DMA\n");
  594. return -ENOMEM;
  595. }
  596. return 0;
  597. }
  598. #define CORL_DONE 0x1
  599. #define CORL_ERR 0x2
  600. static irqreturn_t data_irq(int irq, void *dev_id)
  601. {
  602. struct fpga_device *priv = dev_id;
  603. bool submitted = false;
  604. struct data_buf *buf;
  605. u32 status;
  606. int i;
  607. /* detect spurious interrupts via FPGA status */
  608. for (i = 0; i < 4; i++) {
  609. status = fpga_read_reg(priv, i, MMAP_REG_STATUS);
  610. if (!(status & (CORL_DONE | CORL_ERR))) {
  611. dev_err(priv->dev, "spurious irq detected (FPGA)\n");
  612. return IRQ_NONE;
  613. }
  614. }
  615. /* detect spurious interrupts via raw IRQ pin readback */
  616. status = ioread32be(priv->regs + SYS_IRQ_INPUT_DATA);
  617. if (status & IRQ_CORL_DONE) {
  618. dev_err(priv->dev, "spurious irq detected (IRQ)\n");
  619. return IRQ_NONE;
  620. }
  621. spin_lock(&priv->lock);
  622. /*
  623. * This is an error case that should never happen.
  624. *
  625. * If this driver has a bug and manages to re-enable interrupts while
  626. * a DMA is in progress, then we will hit this statement and should
  627. * start paying attention immediately.
  628. */
  629. BUG_ON(priv->inflight != NULL);
  630. /* hide the interrupt by switching the IRQ driver to GPIO */
  631. data_disable_interrupts(priv);
  632. /* If there are no free buffers, drop this data */
  633. if (list_empty(&priv->free)) {
  634. priv->num_dropped++;
  635. goto out;
  636. }
  637. buf = list_first_entry(&priv->free, struct data_buf, entry);
  638. list_del_init(&buf->entry);
  639. BUG_ON(buf->size != priv->bufsize);
  640. /* Submit a DMA transfer to get the correlation data */
  641. if (data_submit_dma(priv, buf)) {
  642. dev_err(priv->dev, "Unable to setup DMA transfer\n");
  643. list_move_tail(&buf->entry, &priv->free);
  644. goto out;
  645. }
  646. /* Save the buffer for the DMA callback */
  647. priv->inflight = buf;
  648. submitted = true;
  649. /* Start the DMA Engine */
  650. dma_async_issue_pending(priv->chan);
  651. out:
  652. /* If no DMA was submitted, re-enable interrupts */
  653. if (!submitted)
  654. data_enable_interrupts(priv);
  655. spin_unlock(&priv->lock);
  656. return IRQ_HANDLED;
  657. }
  658. /*
  659. * Realtime Device Enable Helpers
  660. */
  661. /**
  662. * data_device_enable() - enable the device for buffered dumping
  663. * @priv: the driver's private data structure
  664. *
  665. * Enable the device for buffered dumping. Allocates buffers and hooks up
  666. * the interrupt handler. When this finishes, data will come pouring in.
  667. *
  668. * LOCKING: must hold dev->mutex
  669. * CONTEXT: user context only
  670. *
  671. * Returns 0 on success, -ERRNO otherwise
  672. */
  673. static int data_device_enable(struct fpga_device *priv)
  674. {
  675. bool enabled;
  676. u32 val;
  677. int ret;
  678. /* multiple enables are safe: they do nothing */
  679. spin_lock_irq(&priv->lock);
  680. enabled = priv->enabled;
  681. spin_unlock_irq(&priv->lock);
  682. if (enabled)
  683. return 0;
  684. /* check that the FPGAs are programmed */
  685. val = ioread32be(priv->regs + SYS_FPGA_CONFIG_STATUS);
  686. if (!(val & (1 << 18))) {
  687. dev_err(priv->dev, "DATA-FPGAs are not enabled\n");
  688. return -ENODATA;
  689. }
  690. /* read the FPGAs to calculate the buffer size */
  691. ret = data_calculate_bufsize(priv);
  692. if (ret) {
  693. dev_err(priv->dev, "unable to calculate buffer size\n");
  694. goto out_error;
  695. }
  696. /* allocate the correlation data buffers */
  697. ret = data_alloc_buffers(priv);
  698. if (ret) {
  699. dev_err(priv->dev, "unable to allocate buffers\n");
  700. goto out_error;
  701. }
  702. /* setup the source scatterlist for dumping correlation data */
  703. ret = data_setup_corl_table(priv);
  704. if (ret) {
  705. dev_err(priv->dev, "unable to setup correlation DMA table\n");
  706. goto out_error;
  707. }
  708. /* prevent the FPGAs from generating interrupts */
  709. data_disable_interrupts(priv);
  710. /* hookup the irq handler */
  711. ret = request_irq(priv->irq, data_irq, IRQF_SHARED, drv_name, priv);
  712. if (ret) {
  713. dev_err(priv->dev, "unable to request IRQ handler\n");
  714. goto out_error;
  715. }
  716. /* allow the DMA callback to re-enable FPGA interrupts */
  717. spin_lock_irq(&priv->lock);
  718. priv->enabled = true;
  719. spin_unlock_irq(&priv->lock);
  720. /* allow the FPGAs to generate interrupts */
  721. data_enable_interrupts(priv);
  722. return 0;
  723. out_error:
  724. sg_free_table(&priv->corl_table);
  725. priv->corl_nents = 0;
  726. data_free_buffers(priv);
  727. return ret;
  728. }
  729. /**
  730. * data_device_disable() - disable the device for buffered dumping
  731. * @priv: the driver's private data structure
  732. *
  733. * Disable the device for buffered dumping. Stops new DMA transactions from
  734. * being generated, waits for all outstanding DMA to complete, and then frees
  735. * all buffers.
  736. *
  737. * LOCKING: must hold dev->mutex
  738. * CONTEXT: user only
  739. *
  740. * Returns 0 on success, -ERRNO otherwise
  741. */
  742. static int data_device_disable(struct fpga_device *priv)
  743. {
  744. spin_lock_irq(&priv->lock);
  745. /* allow multiple disable */
  746. if (!priv->enabled) {
  747. spin_unlock_irq(&priv->lock);
  748. return 0;
  749. }
  750. /*
  751. * Mark the device disabled
  752. *
  753. * This stops DMA callbacks from re-enabling interrupts
  754. */
  755. priv->enabled = false;
  756. /* prevent the FPGAs from generating interrupts */
  757. data_disable_interrupts(priv);
  758. /* wait until all ongoing DMA has finished */
  759. while (priv->inflight != NULL) {
  760. spin_unlock_irq(&priv->lock);
  761. wait_event(priv->wait, priv->inflight == NULL);
  762. spin_lock_irq(&priv->lock);
  763. }
  764. spin_unlock_irq(&priv->lock);
  765. /* unhook the irq handler */
  766. free_irq(priv->irq, priv);
  767. /* free the correlation table */
  768. sg_free_table(&priv->corl_table);
  769. priv->corl_nents = 0;
  770. /* free all buffers: the free and used lists are not being changed */
  771. data_free_buffers(priv);
  772. return 0;
  773. }
  774. /*
  775. * DEBUGFS Interface
  776. */
  777. #ifdef CONFIG_DEBUG_FS
  778. /*
  779. * Count the number of entries in the given list
  780. */
  781. static unsigned int list_num_entries(struct list_head *list)
  782. {
  783. struct list_head *entry;
  784. unsigned int ret = 0;
  785. list_for_each(entry, list)
  786. ret++;
  787. return ret;
  788. }
  789. static int data_debug_show(struct seq_file *f, void *offset)
  790. {
  791. struct fpga_device *priv = f->private;
  792. spin_lock_irq(&priv->lock);
  793. seq_printf(f, "enabled: %d\n", priv->enabled);
  794. seq_printf(f, "bufsize: %d\n", priv->bufsize);
  795. seq_printf(f, "num_buffers: %d\n", priv->num_buffers);
  796. seq_printf(f, "num_free: %d\n", list_num_entries(&priv->free));
  797. seq_printf(f, "inflight: %d\n", priv->inflight != NULL);
  798. seq_printf(f, "num_used: %d\n", list_num_entries(&priv->used));
  799. seq_printf(f, "num_dropped: %d\n", priv->num_dropped);
  800. spin_unlock_irq(&priv->lock);
  801. return 0;
  802. }
  803. static int data_debug_open(struct inode *inode, struct file *file)
  804. {
  805. return single_open(file, data_debug_show, inode->i_private);
  806. }
  807. static const struct file_operations data_debug_fops = {
  808. .owner = THIS_MODULE,
  809. .open = data_debug_open,
  810. .read = seq_read,
  811. .llseek = seq_lseek,
  812. .release = single_release,
  813. };
  814. static int data_debugfs_init(struct fpga_device *priv)
  815. {
  816. priv->dbg_entry = debugfs_create_file(drv_name, S_IRUGO, NULL, priv,
  817. &data_debug_fops);
  818. if (IS_ERR(priv->dbg_entry))
  819. return PTR_ERR(priv->dbg_entry);
  820. return 0;
  821. }
  822. static void data_debugfs_exit(struct fpga_device *priv)
  823. {
  824. debugfs_remove(priv->dbg_entry);
  825. }
  826. #else
  827. static inline int data_debugfs_init(struct fpga_device *priv)
  828. {
  829. return 0;
  830. }
  831. static inline void data_debugfs_exit(struct fpga_device *priv)
  832. {
  833. }
  834. #endif /* CONFIG_DEBUG_FS */
  835. /*
  836. * SYSFS Attributes
  837. */
  838. static ssize_t data_en_show(struct device *dev, struct device_attribute *attr,
  839. char *buf)
  840. {
  841. struct fpga_device *priv = dev_get_drvdata(dev);
  842. int ret;
  843. spin_lock_irq(&priv->lock);
  844. ret = snprintf(buf, PAGE_SIZE, "%u\n", priv->enabled);
  845. spin_unlock_irq(&priv->lock);
  846. return ret;
  847. }
  848. static ssize_t data_en_set(struct device *dev, struct device_attribute *attr,
  849. const char *buf, size_t count)
  850. {
  851. struct fpga_device *priv = dev_get_drvdata(dev);
  852. unsigned long enable;
  853. int ret;
  854. ret = kstrtoul(buf, 0, &enable);
  855. if (ret) {
  856. dev_err(priv->dev, "unable to parse enable input\n");
  857. return ret;
  858. }
  859. /* protect against concurrent enable/disable */
  860. ret = mutex_lock_interruptible(&priv->mutex);
  861. if (ret)
  862. return ret;
  863. if (enable)
  864. ret = data_device_enable(priv);
  865. else
  866. ret = data_device_disable(priv);
  867. if (ret) {
  868. dev_err(priv->dev, "device %s failed\n",
  869. enable ? "enable" : "disable");
  870. count = ret;
  871. goto out_unlock;
  872. }
  873. out_unlock:
  874. mutex_unlock(&priv->mutex);
  875. return count;
  876. }
  877. static DEVICE_ATTR(enable, S_IWUSR | S_IRUGO, data_en_show, data_en_set);
  878. static struct attribute *data_sysfs_attrs[] = {
  879. &dev_attr_enable.attr,
  880. NULL,
  881. };
  882. static const struct attribute_group rt_sysfs_attr_group = {
  883. .attrs = data_sysfs_attrs,
  884. };
  885. /*
  886. * FPGA Realtime Data Character Device
  887. */
  888. static int data_open(struct inode *inode, struct file *filp)
  889. {
  890. /*
  891. * The miscdevice layer puts our struct miscdevice into the
  892. * filp->private_data field. We use this to find our private
  893. * data and then overwrite it with our own private structure.
  894. */
  895. struct fpga_device *priv = container_of(filp->private_data,
  896. struct fpga_device, miscdev);
  897. struct fpga_reader *reader;
  898. int ret;
  899. /* allocate private data */
  900. reader = kzalloc(sizeof(*reader), GFP_KERNEL);
  901. if (!reader)
  902. return -ENOMEM;
  903. reader->priv = priv;
  904. reader->buf = NULL;
  905. filp->private_data = reader;
  906. ret = nonseekable_open(inode, filp);
  907. if (ret) {
  908. dev_err(priv->dev, "nonseekable-open failed\n");
  909. kfree(reader);
  910. return ret;
  911. }
  912. /*
  913. * success, increase the reference count of the private data structure
  914. * so that it doesn't disappear if the device is unbound
  915. */
  916. kref_get(&priv->ref);
  917. return 0;
  918. }
  919. static int data_release(struct inode *inode, struct file *filp)
  920. {
  921. struct fpga_reader *reader = filp->private_data;
  922. struct fpga_device *priv = reader->priv;
  923. /* free the per-reader structure */
  924. data_free_buffer(reader->buf);
  925. kfree(reader);
  926. filp->private_data = NULL;
  927. /* decrement our reference count to the private data */
  928. kref_put(&priv->ref, fpga_device_release);
  929. return 0;
  930. }
  931. static ssize_t data_read(struct file *filp, char __user *ubuf, size_t count,
  932. loff_t *f_pos)
  933. {
  934. struct fpga_reader *reader = filp->private_data;
  935. struct fpga_device *priv = reader->priv;
  936. struct list_head *used = &priv->used;
  937. bool drop_buffer = false;
  938. struct data_buf *dbuf;
  939. size_t avail;
  940. void *data;
  941. int ret;
  942. /* check if we already have a partial buffer */
  943. if (reader->buf) {
  944. dbuf = reader->buf;
  945. goto have_buffer;
  946. }
  947. spin_lock_irq(&priv->lock);
  948. /* Block until there is at least one buffer on the used list */
  949. while (list_empty(used)) {
  950. spin_unlock_irq(&priv->lock);
  951. if (filp->f_flags & O_NONBLOCK)
  952. return -EAGAIN;
  953. ret = wait_event_interruptible(priv->wait, !list_empty(used));
  954. if (ret)
  955. return ret;
  956. spin_lock_irq(&priv->lock);
  957. }
  958. /* Grab the first buffer off of the used list */
  959. dbuf = list_first_entry(used, struct data_buf, entry);
  960. list_del_init(&dbuf->entry);
  961. spin_unlock_irq(&priv->lock);
  962. /* Buffers are always mapped: unmap it */
  963. videobuf_dma_unmap(priv->dev, &dbuf->vb);
  964. /* save the buffer for later */
  965. reader->buf = dbuf;
  966. reader->buf_start = 0;
  967. have_buffer:
  968. /* Get the number of bytes available */
  969. avail = dbuf->size - reader->buf_start;
  970. data = dbuf->vb.vaddr + reader->buf_start;
  971. /* Get the number of bytes we can transfer */
  972. count = min(count, avail);
  973. /* Copy the data to the userspace buffer */
  974. if (copy_to_user(ubuf, data, count))
  975. return -EFAULT;
  976. /* Update the amount of available space */
  977. avail -= count;
  978. /*
  979. * If there is still some data available, save the buffer for the
  980. * next userspace call to read() and return
  981. */
  982. if (avail > 0) {
  983. reader->buf_start += count;
  984. reader->buf = dbuf;
  985. return count;
  986. }
  987. /*
  988. * Get the buffer ready to be reused for DMA
  989. *
  990. * If it fails, we pretend that the read never happed and return
  991. * -EFAULT to userspace. The read will be retried.
  992. */
  993. ret = videobuf_dma_map(priv->dev, &dbuf->vb);
  994. if (ret) {
  995. dev_err(priv->dev, "unable to remap buffer for DMA\n");
  996. return -EFAULT;
  997. }
  998. /* Lock against concurrent enable/disable */
  999. spin_lock_irq(&priv->lock);
  1000. /* the reader is finished with this buffer */
  1001. reader->buf = NULL;
  1002. /*
  1003. * One of two things has happened, the device is disabled, or the
  1004. * device has been reconfigured underneath us. In either case, we
  1005. * should just throw away the buffer.
  1006. *
  1007. * Lockdep complains if this is done under the spinlock, so we
  1008. * handle it during the unlock path.
  1009. */
  1010. if (!priv->enabled || dbuf->size != priv->bufsize) {
  1011. drop_buffer = true;
  1012. goto out_unlock;
  1013. }
  1014. /* The buffer is safe to reuse, so add it back to the free list */
  1015. list_add_tail(&dbuf->entry, &priv->free);
  1016. out_unlock:
  1017. spin_unlock_irq(&priv->lock);
  1018. if (drop_buffer) {
  1019. videobuf_dma_unmap(priv->dev, &dbuf->vb);
  1020. data_free_buffer(dbuf);
  1021. }
  1022. return count;
  1023. }
  1024. static unsigned int data_poll(struct file *filp, struct poll_table_struct *tbl)
  1025. {
  1026. struct fpga_reader *reader = filp->private_data;
  1027. struct fpga_device *priv = reader->priv;
  1028. unsigned int mask = 0;
  1029. poll_wait(filp, &priv->wait, tbl);
  1030. if (!list_empty(&priv->used))
  1031. mask |= POLLIN | POLLRDNORM;
  1032. return mask;
  1033. }
  1034. static int data_mmap(struct file *filp, struct vm_area_struct *vma)
  1035. {
  1036. struct fpga_reader *reader = filp->private_data;
  1037. struct fpga_device *priv = reader->priv;
  1038. unsigned long offset, vsize, psize, addr;
  1039. /* VMA properties */
  1040. offset = vma->vm_pgoff << PAGE_SHIFT;
  1041. vsize = vma->vm_end - vma->vm_start;
  1042. psize = priv->phys_size - offset;
  1043. addr = (priv->phys_addr + offset) >> PAGE_SHIFT;
  1044. /* Check against the FPGA region's physical memory size */
  1045. if (vsize > psize) {
  1046. dev_err(priv->dev, "requested mmap mapping too large\n");
  1047. return -EINVAL;
  1048. }
  1049. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  1050. return io_remap_pfn_range(vma, vma->vm_start, addr, vsize,
  1051. vma->vm_page_prot);
  1052. }
  1053. static const struct file_operations data_fops = {
  1054. .owner = THIS_MODULE,
  1055. .open = data_open,
  1056. .release = data_release,
  1057. .read = data_read,
  1058. .poll = data_poll,
  1059. .mmap = data_mmap,
  1060. .llseek = no_llseek,
  1061. };
  1062. /*
  1063. * OpenFirmware Device Subsystem
  1064. */
  1065. static bool dma_filter(struct dma_chan *chan, void *data)
  1066. {
  1067. /*
  1068. * DMA Channel #0 is used for the FPGA Programmer, so ignore it
  1069. *
  1070. * This probably won't survive an unload/load cycle of the Freescale
  1071. * DMAEngine driver, but that won't be a problem
  1072. */
  1073. if (chan->chan_id == 0 && chan->device->dev_id == 0)
  1074. return false;
  1075. return true;
  1076. }
  1077. static int data_of_probe(struct platform_device *op)
  1078. {
  1079. struct device_node *of_node = op->dev.of_node;
  1080. struct device *this_device;
  1081. struct fpga_device *priv;
  1082. struct resource res;
  1083. dma_cap_mask_t mask;
  1084. int ret;
  1085. /* Allocate private data */
  1086. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  1087. if (!priv) {
  1088. dev_err(&op->dev, "Unable to allocate device private data\n");
  1089. ret = -ENOMEM;
  1090. goto out_return;
  1091. }
  1092. platform_set_drvdata(op, priv);
  1093. priv->dev = &op->dev;
  1094. kref_init(&priv->ref);
  1095. mutex_init(&priv->mutex);
  1096. dev_set_drvdata(priv->dev, priv);
  1097. spin_lock_init(&priv->lock);
  1098. INIT_LIST_HEAD(&priv->free);
  1099. INIT_LIST_HEAD(&priv->used);
  1100. init_waitqueue_head(&priv->wait);
  1101. /* Setup the misc device */
  1102. priv->miscdev.minor = MISC_DYNAMIC_MINOR;
  1103. priv->miscdev.name = drv_name;
  1104. priv->miscdev.fops = &data_fops;
  1105. /* Get the physical address of the FPGA registers */
  1106. ret = of_address_to_resource(of_node, 0, &res);
  1107. if (ret) {
  1108. dev_err(&op->dev, "Unable to find FPGA physical address\n");
  1109. ret = -ENODEV;
  1110. goto out_free_priv;
  1111. }
  1112. priv->phys_addr = res.start;
  1113. priv->phys_size = resource_size(&res);
  1114. /* ioremap the registers for use */
  1115. priv->regs = of_iomap(of_node, 0);
  1116. if (!priv->regs) {
  1117. dev_err(&op->dev, "Unable to ioremap registers\n");
  1118. ret = -ENOMEM;
  1119. goto out_free_priv;
  1120. }
  1121. dma_cap_zero(mask);
  1122. dma_cap_set(DMA_MEMCPY, mask);
  1123. dma_cap_set(DMA_INTERRUPT, mask);
  1124. dma_cap_set(DMA_SLAVE, mask);
  1125. dma_cap_set(DMA_SG, mask);
  1126. /* Request a DMA channel */
  1127. priv->chan = dma_request_channel(mask, dma_filter, NULL);
  1128. if (!priv->chan) {
  1129. dev_err(&op->dev, "Unable to request DMA channel\n");
  1130. ret = -ENODEV;
  1131. goto out_unmap_regs;
  1132. }
  1133. /* Find the correct IRQ number */
  1134. priv->irq = irq_of_parse_and_map(of_node, 0);
  1135. if (priv->irq == NO_IRQ) {
  1136. dev_err(&op->dev, "Unable to find IRQ line\n");
  1137. ret = -ENODEV;
  1138. goto out_release_dma;
  1139. }
  1140. /* Drive the GPIO for FPGA IRQ high (no interrupt) */
  1141. iowrite32be(IRQ_CORL_DONE, priv->regs + SYS_IRQ_OUTPUT_DATA);
  1142. /* Register the miscdevice */
  1143. ret = misc_register(&priv->miscdev);
  1144. if (ret) {
  1145. dev_err(&op->dev, "Unable to register miscdevice\n");
  1146. goto out_irq_dispose_mapping;
  1147. }
  1148. /* Create the debugfs files */
  1149. ret = data_debugfs_init(priv);
  1150. if (ret) {
  1151. dev_err(&op->dev, "Unable to create debugfs files\n");
  1152. goto out_misc_deregister;
  1153. }
  1154. /* Create the sysfs files */
  1155. this_device = priv->miscdev.this_device;
  1156. dev_set_drvdata(this_device, priv);
  1157. ret = sysfs_create_group(&this_device->kobj, &rt_sysfs_attr_group);
  1158. if (ret) {
  1159. dev_err(&op->dev, "Unable to create sysfs files\n");
  1160. goto out_data_debugfs_exit;
  1161. }
  1162. dev_info(&op->dev, "CARMA FPGA Realtime Data Driver Loaded\n");
  1163. return 0;
  1164. out_data_debugfs_exit:
  1165. data_debugfs_exit(priv);
  1166. out_misc_deregister:
  1167. misc_deregister(&priv->miscdev);
  1168. out_irq_dispose_mapping:
  1169. irq_dispose_mapping(priv->irq);
  1170. out_release_dma:
  1171. dma_release_channel(priv->chan);
  1172. out_unmap_regs:
  1173. iounmap(priv->regs);
  1174. out_free_priv:
  1175. kref_put(&priv->ref, fpga_device_release);
  1176. out_return:
  1177. return ret;
  1178. }
  1179. static int data_of_remove(struct platform_device *op)
  1180. {
  1181. struct fpga_device *priv = platform_get_drvdata(op);
  1182. struct device *this_device = priv->miscdev.this_device;
  1183. /* remove all sysfs files, now the device cannot be re-enabled */
  1184. sysfs_remove_group(&this_device->kobj, &rt_sysfs_attr_group);
  1185. /* remove all debugfs files */
  1186. data_debugfs_exit(priv);
  1187. /* disable the device from generating data */
  1188. data_device_disable(priv);
  1189. /* remove the character device to stop new readers from appearing */
  1190. misc_deregister(&priv->miscdev);
  1191. /* cleanup everything not needed by readers */
  1192. irq_dispose_mapping(priv->irq);
  1193. dma_release_channel(priv->chan);
  1194. iounmap(priv->regs);
  1195. /* release our reference */
  1196. kref_put(&priv->ref, fpga_device_release);
  1197. return 0;
  1198. }
  1199. static struct of_device_id data_of_match[] = {
  1200. { .compatible = "carma,carma-fpga", },
  1201. {},
  1202. };
  1203. static struct platform_driver data_of_driver = {
  1204. .probe = data_of_probe,
  1205. .remove = data_of_remove,
  1206. .driver = {
  1207. .name = drv_name,
  1208. .of_match_table = data_of_match,
  1209. .owner = THIS_MODULE,
  1210. },
  1211. };
  1212. module_platform_driver(data_of_driver);
  1213. MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
  1214. MODULE_DESCRIPTION("CARMA DATA-FPGA Access Driver");
  1215. MODULE_LICENSE("GPL");