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