carma-fpga.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  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 = 0;
  552. dst_sg = buf->vb.sglist;
  553. dst_nents = buf->vb.sglen;
  554. src_sg = priv->corl_table.sgl;
  555. src_nents = priv->corl_nents;
  556. /*
  557. * All buffers passed to this function should be ready and mapped
  558. * for DMA already. Therefore, we don't need to do anything except
  559. * submit it to the Freescale DMA Engine for processing
  560. */
  561. /* setup the scatterlist to scatterlist transfer */
  562. tx = chan->device->device_prep_dma_sg(chan,
  563. dst_sg, dst_nents,
  564. src_sg, src_nents,
  565. 0);
  566. if (!tx) {
  567. dev_err(priv->dev, "unable to prep scatterlist DMA\n");
  568. return -ENOMEM;
  569. }
  570. /* submit the transaction to the DMA controller */
  571. cookie = tx->tx_submit(tx);
  572. if (dma_submit_error(cookie)) {
  573. dev_err(priv->dev, "unable to submit scatterlist DMA\n");
  574. return -ENOMEM;
  575. }
  576. /* Prepare the re-read of the SYS-FPGA block */
  577. dst = sg_dma_address(dst_sg) + (NUM_FPGA * REG_BLOCK_SIZE);
  578. src = SYS_FPGA_BLOCK;
  579. tx = chan->device->device_prep_dma_memcpy(chan, dst, src,
  580. REG_BLOCK_SIZE,
  581. dma_flags);
  582. if (!tx) {
  583. dev_err(priv->dev, "unable to prep SYS-FPGA DMA\n");
  584. return -ENOMEM;
  585. }
  586. /* Setup the callback */
  587. tx->callback = data_dma_cb;
  588. tx->callback_param = priv;
  589. /* submit the transaction to the DMA controller */
  590. cookie = tx->tx_submit(tx);
  591. if (dma_submit_error(cookie)) {
  592. dev_err(priv->dev, "unable to submit SYS-FPGA DMA\n");
  593. return -ENOMEM;
  594. }
  595. return 0;
  596. }
  597. #define CORL_DONE 0x1
  598. #define CORL_ERR 0x2
  599. static irqreturn_t data_irq(int irq, void *dev_id)
  600. {
  601. struct fpga_device *priv = dev_id;
  602. bool submitted = false;
  603. struct data_buf *buf;
  604. u32 status;
  605. int i;
  606. /* detect spurious interrupts via FPGA status */
  607. for (i = 0; i < 4; i++) {
  608. status = fpga_read_reg(priv, i, MMAP_REG_STATUS);
  609. if (!(status & (CORL_DONE | CORL_ERR))) {
  610. dev_err(priv->dev, "spurious irq detected (FPGA)\n");
  611. return IRQ_NONE;
  612. }
  613. }
  614. /* detect spurious interrupts via raw IRQ pin readback */
  615. status = ioread32be(priv->regs + SYS_IRQ_INPUT_DATA);
  616. if (status & IRQ_CORL_DONE) {
  617. dev_err(priv->dev, "spurious irq detected (IRQ)\n");
  618. return IRQ_NONE;
  619. }
  620. spin_lock(&priv->lock);
  621. /*
  622. * This is an error case that should never happen.
  623. *
  624. * If this driver has a bug and manages to re-enable interrupts while
  625. * a DMA is in progress, then we will hit this statement and should
  626. * start paying attention immediately.
  627. */
  628. BUG_ON(priv->inflight != NULL);
  629. /* hide the interrupt by switching the IRQ driver to GPIO */
  630. data_disable_interrupts(priv);
  631. /* If there are no free buffers, drop this data */
  632. if (list_empty(&priv->free)) {
  633. priv->num_dropped++;
  634. goto out;
  635. }
  636. buf = list_first_entry(&priv->free, struct data_buf, entry);
  637. list_del_init(&buf->entry);
  638. BUG_ON(buf->size != priv->bufsize);
  639. /* Submit a DMA transfer to get the correlation data */
  640. if (data_submit_dma(priv, buf)) {
  641. dev_err(priv->dev, "Unable to setup DMA transfer\n");
  642. list_move_tail(&buf->entry, &priv->free);
  643. goto out;
  644. }
  645. /* Save the buffer for the DMA callback */
  646. priv->inflight = buf;
  647. submitted = true;
  648. /* Start the DMA Engine */
  649. dma_async_issue_pending(priv->chan);
  650. out:
  651. /* If no DMA was submitted, re-enable interrupts */
  652. if (!submitted)
  653. data_enable_interrupts(priv);
  654. spin_unlock(&priv->lock);
  655. return IRQ_HANDLED;
  656. }
  657. /*
  658. * Realtime Device Enable Helpers
  659. */
  660. /**
  661. * data_device_enable() - enable the device for buffered dumping
  662. * @priv: the driver's private data structure
  663. *
  664. * Enable the device for buffered dumping. Allocates buffers and hooks up
  665. * the interrupt handler. When this finishes, data will come pouring in.
  666. *
  667. * LOCKING: must hold dev->mutex
  668. * CONTEXT: user context only
  669. *
  670. * Returns 0 on success, -ERRNO otherwise
  671. */
  672. static int data_device_enable(struct fpga_device *priv)
  673. {
  674. bool enabled;
  675. u32 val;
  676. int ret;
  677. /* multiple enables are safe: they do nothing */
  678. spin_lock_irq(&priv->lock);
  679. enabled = priv->enabled;
  680. spin_unlock_irq(&priv->lock);
  681. if (enabled)
  682. return 0;
  683. /* check that the FPGAs are programmed */
  684. val = ioread32be(priv->regs + SYS_FPGA_CONFIG_STATUS);
  685. if (!(val & (1 << 18))) {
  686. dev_err(priv->dev, "DATA-FPGAs are not enabled\n");
  687. return -ENODATA;
  688. }
  689. /* read the FPGAs to calculate the buffer size */
  690. ret = data_calculate_bufsize(priv);
  691. if (ret) {
  692. dev_err(priv->dev, "unable to calculate buffer size\n");
  693. goto out_error;
  694. }
  695. /* allocate the correlation data buffers */
  696. ret = data_alloc_buffers(priv);
  697. if (ret) {
  698. dev_err(priv->dev, "unable to allocate buffers\n");
  699. goto out_error;
  700. }
  701. /* setup the source scatterlist for dumping correlation data */
  702. ret = data_setup_corl_table(priv);
  703. if (ret) {
  704. dev_err(priv->dev, "unable to setup correlation DMA table\n");
  705. goto out_error;
  706. }
  707. /* prevent the FPGAs from generating interrupts */
  708. data_disable_interrupts(priv);
  709. /* hookup the irq handler */
  710. ret = request_irq(priv->irq, data_irq, IRQF_SHARED, drv_name, priv);
  711. if (ret) {
  712. dev_err(priv->dev, "unable to request IRQ handler\n");
  713. goto out_error;
  714. }
  715. /* allow the DMA callback to re-enable FPGA interrupts */
  716. spin_lock_irq(&priv->lock);
  717. priv->enabled = true;
  718. spin_unlock_irq(&priv->lock);
  719. /* allow the FPGAs to generate interrupts */
  720. data_enable_interrupts(priv);
  721. return 0;
  722. out_error:
  723. sg_free_table(&priv->corl_table);
  724. priv->corl_nents = 0;
  725. data_free_buffers(priv);
  726. return ret;
  727. }
  728. /**
  729. * data_device_disable() - disable the device for buffered dumping
  730. * @priv: the driver's private data structure
  731. *
  732. * Disable the device for buffered dumping. Stops new DMA transactions from
  733. * being generated, waits for all outstanding DMA to complete, and then frees
  734. * all buffers.
  735. *
  736. * LOCKING: must hold dev->mutex
  737. * CONTEXT: user only
  738. *
  739. * Returns 0 on success, -ERRNO otherwise
  740. */
  741. static int data_device_disable(struct fpga_device *priv)
  742. {
  743. spin_lock_irq(&priv->lock);
  744. /* allow multiple disable */
  745. if (!priv->enabled) {
  746. spin_unlock_irq(&priv->lock);
  747. return 0;
  748. }
  749. /*
  750. * Mark the device disabled
  751. *
  752. * This stops DMA callbacks from re-enabling interrupts
  753. */
  754. priv->enabled = false;
  755. /* prevent the FPGAs from generating interrupts */
  756. data_disable_interrupts(priv);
  757. /* wait until all ongoing DMA has finished */
  758. while (priv->inflight != NULL) {
  759. spin_unlock_irq(&priv->lock);
  760. wait_event(priv->wait, priv->inflight == NULL);
  761. spin_lock_irq(&priv->lock);
  762. }
  763. spin_unlock_irq(&priv->lock);
  764. /* unhook the irq handler */
  765. free_irq(priv->irq, priv);
  766. /* free the correlation table */
  767. sg_free_table(&priv->corl_table);
  768. priv->corl_nents = 0;
  769. /* free all buffers: the free and used lists are not being changed */
  770. data_free_buffers(priv);
  771. return 0;
  772. }
  773. /*
  774. * DEBUGFS Interface
  775. */
  776. #ifdef CONFIG_DEBUG_FS
  777. /*
  778. * Count the number of entries in the given list
  779. */
  780. static unsigned int list_num_entries(struct list_head *list)
  781. {
  782. struct list_head *entry;
  783. unsigned int ret = 0;
  784. list_for_each(entry, list)
  785. ret++;
  786. return ret;
  787. }
  788. static int data_debug_show(struct seq_file *f, void *offset)
  789. {
  790. struct fpga_device *priv = f->private;
  791. spin_lock_irq(&priv->lock);
  792. seq_printf(f, "enabled: %d\n", priv->enabled);
  793. seq_printf(f, "bufsize: %d\n", priv->bufsize);
  794. seq_printf(f, "num_buffers: %d\n", priv->num_buffers);
  795. seq_printf(f, "num_free: %d\n", list_num_entries(&priv->free));
  796. seq_printf(f, "inflight: %d\n", priv->inflight != NULL);
  797. seq_printf(f, "num_used: %d\n", list_num_entries(&priv->used));
  798. seq_printf(f, "num_dropped: %d\n", priv->num_dropped);
  799. spin_unlock_irq(&priv->lock);
  800. return 0;
  801. }
  802. static int data_debug_open(struct inode *inode, struct file *file)
  803. {
  804. return single_open(file, data_debug_show, inode->i_private);
  805. }
  806. static const struct file_operations data_debug_fops = {
  807. .owner = THIS_MODULE,
  808. .open = data_debug_open,
  809. .read = seq_read,
  810. .llseek = seq_lseek,
  811. .release = single_release,
  812. };
  813. static int data_debugfs_init(struct fpga_device *priv)
  814. {
  815. priv->dbg_entry = debugfs_create_file(drv_name, S_IRUGO, NULL, priv,
  816. &data_debug_fops);
  817. if (IS_ERR(priv->dbg_entry))
  818. return PTR_ERR(priv->dbg_entry);
  819. return 0;
  820. }
  821. static void data_debugfs_exit(struct fpga_device *priv)
  822. {
  823. debugfs_remove(priv->dbg_entry);
  824. }
  825. #else
  826. static inline int data_debugfs_init(struct fpga_device *priv)
  827. {
  828. return 0;
  829. }
  830. static inline void data_debugfs_exit(struct fpga_device *priv)
  831. {
  832. }
  833. #endif /* CONFIG_DEBUG_FS */
  834. /*
  835. * SYSFS Attributes
  836. */
  837. static ssize_t data_en_show(struct device *dev, struct device_attribute *attr,
  838. char *buf)
  839. {
  840. struct fpga_device *priv = dev_get_drvdata(dev);
  841. int ret;
  842. spin_lock_irq(&priv->lock);
  843. ret = snprintf(buf, PAGE_SIZE, "%u\n", priv->enabled);
  844. spin_unlock_irq(&priv->lock);
  845. return ret;
  846. }
  847. static ssize_t data_en_set(struct device *dev, struct device_attribute *attr,
  848. const char *buf, size_t count)
  849. {
  850. struct fpga_device *priv = dev_get_drvdata(dev);
  851. unsigned long enable;
  852. int ret;
  853. ret = kstrtoul(buf, 0, &enable);
  854. if (ret) {
  855. dev_err(priv->dev, "unable to parse enable input\n");
  856. return ret;
  857. }
  858. /* protect against concurrent enable/disable */
  859. ret = mutex_lock_interruptible(&priv->mutex);
  860. if (ret)
  861. return ret;
  862. if (enable)
  863. ret = data_device_enable(priv);
  864. else
  865. ret = data_device_disable(priv);
  866. if (ret) {
  867. dev_err(priv->dev, "device %s failed\n",
  868. enable ? "enable" : "disable");
  869. count = ret;
  870. goto out_unlock;
  871. }
  872. out_unlock:
  873. mutex_unlock(&priv->mutex);
  874. return count;
  875. }
  876. static DEVICE_ATTR(enable, S_IWUSR | S_IRUGO, data_en_show, data_en_set);
  877. static struct attribute *data_sysfs_attrs[] = {
  878. &dev_attr_enable.attr,
  879. NULL,
  880. };
  881. static const struct attribute_group rt_sysfs_attr_group = {
  882. .attrs = data_sysfs_attrs,
  883. };
  884. /*
  885. * FPGA Realtime Data Character Device
  886. */
  887. static int data_open(struct inode *inode, struct file *filp)
  888. {
  889. /*
  890. * The miscdevice layer puts our struct miscdevice into the
  891. * filp->private_data field. We use this to find our private
  892. * data and then overwrite it with our own private structure.
  893. */
  894. struct fpga_device *priv = container_of(filp->private_data,
  895. struct fpga_device, miscdev);
  896. struct fpga_reader *reader;
  897. int ret;
  898. /* allocate private data */
  899. reader = kzalloc(sizeof(*reader), GFP_KERNEL);
  900. if (!reader)
  901. return -ENOMEM;
  902. reader->priv = priv;
  903. reader->buf = NULL;
  904. filp->private_data = reader;
  905. ret = nonseekable_open(inode, filp);
  906. if (ret) {
  907. dev_err(priv->dev, "nonseekable-open failed\n");
  908. kfree(reader);
  909. return ret;
  910. }
  911. /*
  912. * success, increase the reference count of the private data structure
  913. * so that it doesn't disappear if the device is unbound
  914. */
  915. kref_get(&priv->ref);
  916. return 0;
  917. }
  918. static int data_release(struct inode *inode, struct file *filp)
  919. {
  920. struct fpga_reader *reader = filp->private_data;
  921. struct fpga_device *priv = reader->priv;
  922. /* free the per-reader structure */
  923. data_free_buffer(reader->buf);
  924. kfree(reader);
  925. filp->private_data = NULL;
  926. /* decrement our reference count to the private data */
  927. kref_put(&priv->ref, fpga_device_release);
  928. return 0;
  929. }
  930. static ssize_t data_read(struct file *filp, char __user *ubuf, size_t count,
  931. loff_t *f_pos)
  932. {
  933. struct fpga_reader *reader = filp->private_data;
  934. struct fpga_device *priv = reader->priv;
  935. struct list_head *used = &priv->used;
  936. bool drop_buffer = false;
  937. struct data_buf *dbuf;
  938. size_t avail;
  939. void *data;
  940. int ret;
  941. /* check if we already have a partial buffer */
  942. if (reader->buf) {
  943. dbuf = reader->buf;
  944. goto have_buffer;
  945. }
  946. spin_lock_irq(&priv->lock);
  947. /* Block until there is at least one buffer on the used list */
  948. while (list_empty(used)) {
  949. spin_unlock_irq(&priv->lock);
  950. if (filp->f_flags & O_NONBLOCK)
  951. return -EAGAIN;
  952. ret = wait_event_interruptible(priv->wait, !list_empty(used));
  953. if (ret)
  954. return ret;
  955. spin_lock_irq(&priv->lock);
  956. }
  957. /* Grab the first buffer off of the used list */
  958. dbuf = list_first_entry(used, struct data_buf, entry);
  959. list_del_init(&dbuf->entry);
  960. spin_unlock_irq(&priv->lock);
  961. /* Buffers are always mapped: unmap it */
  962. videobuf_dma_unmap(priv->dev, &dbuf->vb);
  963. /* save the buffer for later */
  964. reader->buf = dbuf;
  965. reader->buf_start = 0;
  966. have_buffer:
  967. /* Get the number of bytes available */
  968. avail = dbuf->size - reader->buf_start;
  969. data = dbuf->vb.vaddr + reader->buf_start;
  970. /* Get the number of bytes we can transfer */
  971. count = min(count, avail);
  972. /* Copy the data to the userspace buffer */
  973. if (copy_to_user(ubuf, data, count))
  974. return -EFAULT;
  975. /* Update the amount of available space */
  976. avail -= count;
  977. /*
  978. * If there is still some data available, save the buffer for the
  979. * next userspace call to read() and return
  980. */
  981. if (avail > 0) {
  982. reader->buf_start += count;
  983. reader->buf = dbuf;
  984. return count;
  985. }
  986. /*
  987. * Get the buffer ready to be reused for DMA
  988. *
  989. * If it fails, we pretend that the read never happed and return
  990. * -EFAULT to userspace. The read will be retried.
  991. */
  992. ret = videobuf_dma_map(priv->dev, &dbuf->vb);
  993. if (ret) {
  994. dev_err(priv->dev, "unable to remap buffer for DMA\n");
  995. return -EFAULT;
  996. }
  997. /* Lock against concurrent enable/disable */
  998. spin_lock_irq(&priv->lock);
  999. /* the reader is finished with this buffer */
  1000. reader->buf = NULL;
  1001. /*
  1002. * One of two things has happened, the device is disabled, or the
  1003. * device has been reconfigured underneath us. In either case, we
  1004. * should just throw away the buffer.
  1005. *
  1006. * Lockdep complains if this is done under the spinlock, so we
  1007. * handle it during the unlock path.
  1008. */
  1009. if (!priv->enabled || dbuf->size != priv->bufsize) {
  1010. drop_buffer = true;
  1011. goto out_unlock;
  1012. }
  1013. /* The buffer is safe to reuse, so add it back to the free list */
  1014. list_add_tail(&dbuf->entry, &priv->free);
  1015. out_unlock:
  1016. spin_unlock_irq(&priv->lock);
  1017. if (drop_buffer) {
  1018. videobuf_dma_unmap(priv->dev, &dbuf->vb);
  1019. data_free_buffer(dbuf);
  1020. }
  1021. return count;
  1022. }
  1023. static unsigned int data_poll(struct file *filp, struct poll_table_struct *tbl)
  1024. {
  1025. struct fpga_reader *reader = filp->private_data;
  1026. struct fpga_device *priv = reader->priv;
  1027. unsigned int mask = 0;
  1028. poll_wait(filp, &priv->wait, tbl);
  1029. if (!list_empty(&priv->used))
  1030. mask |= POLLIN | POLLRDNORM;
  1031. return mask;
  1032. }
  1033. static int data_mmap(struct file *filp, struct vm_area_struct *vma)
  1034. {
  1035. struct fpga_reader *reader = filp->private_data;
  1036. struct fpga_device *priv = reader->priv;
  1037. unsigned long offset, vsize, psize, addr;
  1038. /* VMA properties */
  1039. offset = vma->vm_pgoff << PAGE_SHIFT;
  1040. vsize = vma->vm_end - vma->vm_start;
  1041. psize = priv->phys_size - offset;
  1042. addr = (priv->phys_addr + offset) >> PAGE_SHIFT;
  1043. /* Check against the FPGA region's physical memory size */
  1044. if (vsize > psize) {
  1045. dev_err(priv->dev, "requested mmap mapping too large\n");
  1046. return -EINVAL;
  1047. }
  1048. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  1049. return io_remap_pfn_range(vma, vma->vm_start, addr, vsize,
  1050. vma->vm_page_prot);
  1051. }
  1052. static const struct file_operations data_fops = {
  1053. .owner = THIS_MODULE,
  1054. .open = data_open,
  1055. .release = data_release,
  1056. .read = data_read,
  1057. .poll = data_poll,
  1058. .mmap = data_mmap,
  1059. .llseek = no_llseek,
  1060. };
  1061. /*
  1062. * OpenFirmware Device Subsystem
  1063. */
  1064. static bool dma_filter(struct dma_chan *chan, void *data)
  1065. {
  1066. /*
  1067. * DMA Channel #0 is used for the FPGA Programmer, so ignore it
  1068. *
  1069. * This probably won't survive an unload/load cycle of the Freescale
  1070. * DMAEngine driver, but that won't be a problem
  1071. */
  1072. if (chan->chan_id == 0 && chan->device->dev_id == 0)
  1073. return false;
  1074. return true;
  1075. }
  1076. static int data_of_probe(struct platform_device *op)
  1077. {
  1078. struct device_node *of_node = op->dev.of_node;
  1079. struct device *this_device;
  1080. struct fpga_device *priv;
  1081. struct resource res;
  1082. dma_cap_mask_t mask;
  1083. int ret;
  1084. /* Allocate private data */
  1085. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  1086. if (!priv) {
  1087. dev_err(&op->dev, "Unable to allocate device private data\n");
  1088. ret = -ENOMEM;
  1089. goto out_return;
  1090. }
  1091. platform_set_drvdata(op, priv);
  1092. priv->dev = &op->dev;
  1093. kref_init(&priv->ref);
  1094. mutex_init(&priv->mutex);
  1095. dev_set_drvdata(priv->dev, priv);
  1096. spin_lock_init(&priv->lock);
  1097. INIT_LIST_HEAD(&priv->free);
  1098. INIT_LIST_HEAD(&priv->used);
  1099. init_waitqueue_head(&priv->wait);
  1100. /* Setup the misc device */
  1101. priv->miscdev.minor = MISC_DYNAMIC_MINOR;
  1102. priv->miscdev.name = drv_name;
  1103. priv->miscdev.fops = &data_fops;
  1104. /* Get the physical address of the FPGA registers */
  1105. ret = of_address_to_resource(of_node, 0, &res);
  1106. if (ret) {
  1107. dev_err(&op->dev, "Unable to find FPGA physical address\n");
  1108. ret = -ENODEV;
  1109. goto out_free_priv;
  1110. }
  1111. priv->phys_addr = res.start;
  1112. priv->phys_size = resource_size(&res);
  1113. /* ioremap the registers for use */
  1114. priv->regs = of_iomap(of_node, 0);
  1115. if (!priv->regs) {
  1116. dev_err(&op->dev, "Unable to ioremap registers\n");
  1117. ret = -ENOMEM;
  1118. goto out_free_priv;
  1119. }
  1120. dma_cap_zero(mask);
  1121. dma_cap_set(DMA_MEMCPY, mask);
  1122. dma_cap_set(DMA_INTERRUPT, mask);
  1123. dma_cap_set(DMA_SLAVE, mask);
  1124. dma_cap_set(DMA_SG, mask);
  1125. /* Request a DMA channel */
  1126. priv->chan = dma_request_channel(mask, dma_filter, NULL);
  1127. if (!priv->chan) {
  1128. dev_err(&op->dev, "Unable to request DMA channel\n");
  1129. ret = -ENODEV;
  1130. goto out_unmap_regs;
  1131. }
  1132. /* Find the correct IRQ number */
  1133. priv->irq = irq_of_parse_and_map(of_node, 0);
  1134. if (priv->irq == NO_IRQ) {
  1135. dev_err(&op->dev, "Unable to find IRQ line\n");
  1136. ret = -ENODEV;
  1137. goto out_release_dma;
  1138. }
  1139. /* Drive the GPIO for FPGA IRQ high (no interrupt) */
  1140. iowrite32be(IRQ_CORL_DONE, priv->regs + SYS_IRQ_OUTPUT_DATA);
  1141. /* Register the miscdevice */
  1142. ret = misc_register(&priv->miscdev);
  1143. if (ret) {
  1144. dev_err(&op->dev, "Unable to register miscdevice\n");
  1145. goto out_irq_dispose_mapping;
  1146. }
  1147. /* Create the debugfs files */
  1148. ret = data_debugfs_init(priv);
  1149. if (ret) {
  1150. dev_err(&op->dev, "Unable to create debugfs files\n");
  1151. goto out_misc_deregister;
  1152. }
  1153. /* Create the sysfs files */
  1154. this_device = priv->miscdev.this_device;
  1155. dev_set_drvdata(this_device, priv);
  1156. ret = sysfs_create_group(&this_device->kobj, &rt_sysfs_attr_group);
  1157. if (ret) {
  1158. dev_err(&op->dev, "Unable to create sysfs files\n");
  1159. goto out_data_debugfs_exit;
  1160. }
  1161. dev_info(&op->dev, "CARMA FPGA Realtime Data Driver Loaded\n");
  1162. return 0;
  1163. out_data_debugfs_exit:
  1164. data_debugfs_exit(priv);
  1165. out_misc_deregister:
  1166. misc_deregister(&priv->miscdev);
  1167. out_irq_dispose_mapping:
  1168. irq_dispose_mapping(priv->irq);
  1169. out_release_dma:
  1170. dma_release_channel(priv->chan);
  1171. out_unmap_regs:
  1172. iounmap(priv->regs);
  1173. out_free_priv:
  1174. kref_put(&priv->ref, fpga_device_release);
  1175. out_return:
  1176. return ret;
  1177. }
  1178. static int data_of_remove(struct platform_device *op)
  1179. {
  1180. struct fpga_device *priv = platform_get_drvdata(op);
  1181. struct device *this_device = priv->miscdev.this_device;
  1182. /* remove all sysfs files, now the device cannot be re-enabled */
  1183. sysfs_remove_group(&this_device->kobj, &rt_sysfs_attr_group);
  1184. /* remove all debugfs files */
  1185. data_debugfs_exit(priv);
  1186. /* disable the device from generating data */
  1187. data_device_disable(priv);
  1188. /* remove the character device to stop new readers from appearing */
  1189. misc_deregister(&priv->miscdev);
  1190. /* cleanup everything not needed by readers */
  1191. irq_dispose_mapping(priv->irq);
  1192. dma_release_channel(priv->chan);
  1193. iounmap(priv->regs);
  1194. /* release our reference */
  1195. kref_put(&priv->ref, fpga_device_release);
  1196. return 0;
  1197. }
  1198. static struct of_device_id data_of_match[] = {
  1199. { .compatible = "carma,carma-fpga", },
  1200. {},
  1201. };
  1202. static struct platform_driver data_of_driver = {
  1203. .probe = data_of_probe,
  1204. .remove = data_of_remove,
  1205. .driver = {
  1206. .name = drv_name,
  1207. .of_match_table = data_of_match,
  1208. .owner = THIS_MODULE,
  1209. },
  1210. };
  1211. module_platform_driver(data_of_driver);
  1212. MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
  1213. MODULE_DESCRIPTION("CARMA DATA-FPGA Access Driver");
  1214. MODULE_LICENSE("GPL");