4 * Copyright (C) 2016 - 2017 Xilinx, Inc.
7 * This driver is developed for SDFEC16 (Soft Decision FEC 16nm)
8 * IP. It exposes a char device interface in sysfs and supports file
9 * operations like open(), close() and ioctl().
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include <linux/cdev.h>
26 #include <linux/device.h>
29 #include <linux/interrupt.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
33 #include <linux/of_platform.h>
34 #include <linux/platform_device.h>
35 #include <linux/poll.h>
36 #include <linux/slab.h>
37 #include <linux/uaccess.h>
39 #include <uapi/misc/xilinx_sdfec.h>
41 #define DRIVER_NAME "xilinx_sdfec"
42 #define DRIVER_VERSION "0.3"
43 #define DRIVER_MAX_DEV (6)
45 static struct class *xsdfec_class;
46 static atomic_t xsdfec_ndevs = ATOMIC_INIT(0);
47 static dev_t xsdfec_devt;
49 /* Xilinx SDFEC Register Map */
50 #define XSDFEC_AXI_WR_PROTECT_ADDR (0x00000)
51 #define XSDFEC_CODE_WR_PROTECT_ADDR (0x00004)
52 #define XSDFEC_ACTIVE_ADDR (0x00008)
53 #define XSDFEC_AXIS_WIDTH_ADDR (0x0000c)
54 #define XSDFEC_AXIS_ENABLE_ADDR (0x00010)
55 #define XSDFEC_AXIS_ENABLE_MASK (0x0001F)
56 #define XSDFEC_FEC_CODE_ADDR (0x00014)
57 #define XSDFEC_ORDER_ADDR (0x00018)
59 /* Interrupt Status Register Bit Mask*/
60 #define XSDFEC_ISR_MASK (0x0003F)
61 /* Interrupt Status Register */
62 #define XSDFEC_ISR_ADDR (0x0001c)
63 /* Write Only - Interrupt Enable Register */
64 #define XSDFEC_IER_ADDR (0x00020)
65 /* Write Only - Interrupt Disable Register */
66 #define XSDFEC_IDR_ADDR (0x00024)
67 /* Read Only - Interrupt Mask Register */
68 #define XSDFEC_IMR_ADDR (0x00028)
70 /* Single Bit Errors */
71 #define XSDFEC_ECC_ISR_SBE (0x7FF)
72 /* Multi Bit Errors */
73 #define XSDFEC_ECC_ISR_MBE (0x3FF800)
74 /* ECC Interrupt Status Bit Mask */
75 #define XSDFEC_ECC_ISR_MASK (XSDFEC_ECC_ISR_SBE | XSDFEC_ECC_ISR_MBE)
77 /* Multi Bit Error Postion */
78 #define XSDFEC_ECC_MULTI_BIT_POS (11)
79 #define XSDFEC_ERROR_MAX_THRESHOLD (100)
81 /* ECC Interrupt Status Register */
82 #define XSDFEC_ECC_ISR_ADDR (0x0002c)
83 /* Write Only - ECC Interrupt Enable Register */
84 #define XSDFEC_ECC_IER_ADDR (0x00030)
85 /* Write Only - ECC Interrupt Disable Register */
86 #define XSDFEC_ECC_IDR_ADDR (0x00034)
87 /* Read Only - ECC Interrupt Mask Register */
88 #define XSDFEC_ECC_IMR_ADDR (0x00038)
90 #define XSDFEC_BYPASS_ADDR (0x0003c)
91 #define XSDFEC_TEST_EMA_ADDR_BASE (0x00080)
92 #define XSDFEC_TEST_EMA_ADDR_HIGH (0x00089)
93 #define XSDFEC_TURBO_ADDR (0x00100)
94 #define XSDFEC_LDPC_CODE_REG0_ADDR_BASE (0x02000)
95 #define XSDFEC_LDPC_CODE_REG0_ADDR_HIGH (0x021fc)
96 #define XSDFEC_LDPC_CODE_REG1_ADDR_BASE (0x02004)
97 #define XSDFEC_LDPC_CODE_REG1_ADDR_HIGH (0x02200)
98 #define XSDFEC_LDPC_CODE_REG2_ADDR_BASE (0x02008)
99 #define XSDFEC_LDPC_CODE_REG2_ADDR_HIGH (0x02204)
100 #define XSDFEC_LDPC_CODE_REG3_ADDR_BASE (0x0200c)
101 #define XSDFEC_LDPC_CODE_REG3_ADDR_HIGH (0x02208)
104 * struct xsdfec_dev - Driver data for SDFEC
105 * @regs: device physical base address
106 * @dev: pointer to device struct
107 * @fec_id: Instance number
108 * @intr_enabled: indicates IRQ enabled
109 * @wr_protect: indicates Write Protect enabled
110 * @code: LDPC or Turbo Codes being used
111 * @order: In-Order or Out-of-Order
112 * @state: State of the SDFEC device
113 * @op_mode: Operating in Encode or Decode
114 * @isr_err_count: Count of ISR errors
115 * @cecc_count: Count of Correctable ECC errors (SBE)
116 * @uecc_count: Count of Uncorrectable ECC errors (MBE)
117 * @reset_count: Count of Resets requested
118 * @open_count: Count of char device being opened
120 * @xsdfec_cdev: Character device handle
121 * @sc_off: Shared Scale Table Offset
122 * @qc_off: Shared Circulant Table Offset
123 * @la_off: Shared Layer Table Offset
124 * @waitq: Driver wait queue
126 * This structure contains necessary state for SDFEC driver to operate
134 enum xsdfec_code code;
135 enum xsdfec_order order;
136 enum xsdfec_state state;
137 enum xsdfec_op_mode op_mode;
138 atomic_t isr_err_count;
141 atomic_t reset_count;
144 struct cdev xsdfec_cdev;
148 wait_queue_head_t waitq;
152 xsdfec_regwrite(struct xsdfec_dev *xsdfec, u32 addr, u32 value)
154 if (xsdfec->wr_protect) {
155 dev_err(xsdfec->dev, "SDFEC in write protect");
160 "Writing 0x%x to offset 0x%x", value, addr);
161 iowrite32(value, xsdfec->regs + addr);
165 xsdfec_regread(struct xsdfec_dev *xsdfec, u32 addr)
169 rval = ioread32(xsdfec->regs + addr);
170 dev_info(xsdfec->dev,
171 "Read value = 0x%x from offset 0x%x",
176 #define XSDFEC_WRITE_PROTECT_ENABLE (1)
177 #define XSDFEC_WRITE_PROTECT_DISABLE (0)
179 xsdfec_wr_protect(struct xsdfec_dev *xsdfec, bool wr_pr)
182 xsdfec_regwrite(xsdfec,
183 XSDFEC_CODE_WR_PROTECT_ADDR,
184 XSDFEC_WRITE_PROTECT_ENABLE);
185 xsdfec_regwrite(xsdfec,
186 XSDFEC_AXI_WR_PROTECT_ADDR,
187 XSDFEC_WRITE_PROTECT_ENABLE);
189 xsdfec_regwrite(xsdfec,
190 XSDFEC_AXI_WR_PROTECT_ADDR,
191 XSDFEC_WRITE_PROTECT_DISABLE);
192 xsdfec_regwrite(xsdfec,
193 XSDFEC_CODE_WR_PROTECT_ADDR,
194 XSDFEC_WRITE_PROTECT_DISABLE);
196 xsdfec->wr_protect = wr_pr;
200 xsdfec_dev_open(struct inode *iptr, struct file *fptr)
202 struct xsdfec_dev *xsdfec;
204 xsdfec = container_of(iptr->i_cdev, struct xsdfec_dev, xsdfec_cdev);
208 /* Only one open per device at a time */
209 if (!atomic_dec_and_test(&xsdfec->open_count))
212 fptr->private_data = xsdfec;
217 xsdfec_dev_release(struct inode *iptr, struct file *fptr)
219 struct xsdfec_dev *xsdfec;
221 xsdfec = container_of(iptr->i_cdev, struct xsdfec_dev, xsdfec_cdev);
225 atomic_inc(&xsdfec->open_count);
229 #define XSDFEC_IS_ACTIVITY_SET (0x1)
231 xsdfec_get_status(struct xsdfec_dev *xsdfec, void __user *arg)
233 struct xsdfec_status status;
236 status.fec_id = xsdfec->fec_id;
237 status.state = xsdfec->state;
238 status.code = xsdfec->code;
239 status.order = xsdfec->order;
240 status.mode = xsdfec->op_mode;
242 (xsdfec_regread(xsdfec,
243 XSDFEC_ACTIVE_ADDR) &
244 XSDFEC_IS_ACTIVITY_SET);
245 status.cecc_count = atomic_read(&xsdfec->cecc_count);
247 err = copy_to_user(arg, &status, sizeof(status));
249 dev_err(xsdfec->dev, "%s failed for SDFEC%d",
250 __func__, xsdfec->fec_id);
257 xsdfec_get_config(struct xsdfec_dev *xsdfec, void __user *arg)
259 struct xsdfec_config config;
262 config.fec_id = xsdfec->fec_id;
263 config.state = xsdfec->state;
264 config.code = xsdfec->code;
265 config.mode = xsdfec->op_mode;
266 config.order = xsdfec->order;
268 err = copy_to_user(arg, &config, sizeof(config));
270 dev_err(xsdfec->dev, "%s failed for SDFEC%d",
271 __func__, xsdfec->fec_id);
278 xsdfec_isr_enable(struct xsdfec_dev *xsdfec, bool enable)
284 xsdfec_regwrite(xsdfec, XSDFEC_IER_ADDR,
286 mask_read = xsdfec_regread(xsdfec, XSDFEC_IMR_ADDR);
287 if (mask_read & XSDFEC_ISR_MASK) {
289 "SDFEC enabling irq with IER failed");
294 xsdfec_regwrite(xsdfec, XSDFEC_IDR_ADDR,
296 mask_read = xsdfec_regread(xsdfec, XSDFEC_IMR_ADDR);
297 if ((mask_read & XSDFEC_ISR_MASK) != XSDFEC_ISR_MASK) {
299 "SDFEC disabling irq with IDR failed");
307 xsdfec_ecc_isr_enable(struct xsdfec_dev *xsdfec, bool enable)
313 xsdfec_regwrite(xsdfec, XSDFEC_ECC_IER_ADDR,
314 XSDFEC_ECC_ISR_MASK);
315 mask_read = xsdfec_regread(xsdfec, XSDFEC_ECC_IMR_ADDR);
316 if (mask_read & XSDFEC_ECC_ISR_MASK) {
318 "SDFEC enabling ECC irq with ECC IER failed");
323 xsdfec_regwrite(xsdfec, XSDFEC_ECC_IDR_ADDR,
324 XSDFEC_ECC_ISR_MASK);
325 mask_read = xsdfec_regread(xsdfec, XSDFEC_ECC_IMR_ADDR);
326 if ((mask_read & XSDFEC_ECC_ISR_MASK) != XSDFEC_ECC_ISR_MASK) {
328 "SDFEC disable ECC irq with ECC IDR failed");
336 xsdfec_set_irq(struct xsdfec_dev *xsdfec, void __user *arg)
338 struct xsdfec_irq irq;
341 err = copy_from_user(&irq, arg, sizeof(irq));
343 dev_err(xsdfec->dev, "%s failed for SDFEC%d",
344 __func__, xsdfec->fec_id);
348 /* Setup tlast related IRQ */
349 if (irq.enable_isr) {
350 err = xsdfec_isr_enable(xsdfec, true);
355 /* Setup ECC related IRQ */
356 if (irq.enable_ecc_isr) {
357 err = xsdfec_ecc_isr_enable(xsdfec, true);
365 #define XSDFEC_TURBO_SCALE_MASK (0xF)
366 #define XSDFEC_TURBO_SCALE_BIT_POS (8)
368 xsdfec_set_turbo(struct xsdfec_dev *xsdfec, void __user *arg)
370 struct xsdfec_turbo turbo;
374 err = copy_from_user(&turbo, arg, sizeof(turbo));
376 dev_err(xsdfec->dev, "%s failed for SDFEC%d",
377 __func__, xsdfec->fec_id);
381 /* Check to see what device tree says about the FEC codes */
382 if (xsdfec->code == XSDFEC_LDPC_CODE) {
384 "%s: Unable to write Turbo to SDFEC%d check DT",
385 __func__, xsdfec->fec_id);
387 } else if (xsdfec->code == XSDFEC_CODE_INVALID) {
388 xsdfec->code = XSDFEC_TURBO_CODE;
391 if (xsdfec->wr_protect)
392 xsdfec_wr_protect(xsdfec, false);
394 xsdfec_regwrite(xsdfec, XSDFEC_FEC_CODE_ADDR, (xsdfec->code - 1));
395 turbo_write = ((turbo.scale & XSDFEC_TURBO_SCALE_MASK) <<
396 XSDFEC_TURBO_SCALE_BIT_POS) | turbo.alg;
397 xsdfec_regwrite(xsdfec, XSDFEC_TURBO_ADDR, turbo_write);
401 #define XSDFEC_LDPC_REG_JUMP (0x10)
402 #define XSDFEC_REG0_N_MASK (0x0000FFFF)
403 #define XSDFEC_REG0_N_LSB (0)
404 #define XSDFEC_REG0_K_MASK (0x7fff0000)
405 #define XSDFEC_REG0_K_LSB (16)
407 xsdfec_reg0_write(struct xsdfec_dev *xsdfec,
408 u32 n, u32 k, u32 offset)
412 /* Use only lower 16 bits */
413 if (n & ~XSDFEC_REG0_N_MASK)
414 dev_err(xsdfec->dev, "N value is beyond 16 bits");
415 n &= XSDFEC_REG0_N_MASK;
416 n <<= XSDFEC_REG0_N_LSB;
418 if (k & XSDFEC_REG0_K_MASK)
419 dev_err(xsdfec->dev, "K value is beyond 16 bits");
421 k = ((k << XSDFEC_REG0_K_LSB) & XSDFEC_REG0_K_MASK);
424 if (XSDFEC_LDPC_CODE_REG0_ADDR_BASE + (offset * XSDFEC_LDPC_REG_JUMP)
425 > XSDFEC_LDPC_CODE_REG0_ADDR_HIGH) {
427 "Writing outside of LDPC reg0 space 0x%x",
428 XSDFEC_LDPC_CODE_REG0_ADDR_BASE +
429 (offset * XSDFEC_LDPC_REG_JUMP));
432 xsdfec_regwrite(xsdfec,
433 XSDFEC_LDPC_CODE_REG0_ADDR_BASE +
434 (offset * XSDFEC_LDPC_REG_JUMP), wdata);
438 #define XSDFEC_REG1_PSIZE_MASK (0x000001ff)
439 #define XSDFEC_REG1_NO_PACKING_MASK (0x00000400)
440 #define XSDFEC_REG1_NO_PACKING_LSB (10)
441 #define XSDFEC_REG1_NM_MASK (0x000ff800)
442 #define XSDFEC_REG1_NM_LSB (11)
443 #define XSDFEC_REG1_BYPASS_MASK (0x00100000)
445 xsdfec_reg1_write(struct xsdfec_dev *xsdfec, u32 psize,
446 u32 no_packing, u32 nm, u32 offset)
450 if (psize & ~XSDFEC_REG1_PSIZE_MASK)
451 dev_err(xsdfec->dev, "Psize is beyond 10 bits");
452 psize &= XSDFEC_REG1_PSIZE_MASK;
454 if (no_packing != 0 && no_packing != 1)
455 dev_err(xsdfec->dev, "No-packing bit register invalid");
456 no_packing = ((no_packing << XSDFEC_REG1_NO_PACKING_LSB) &
457 XSDFEC_REG1_NO_PACKING_MASK);
459 if (nm & ~(XSDFEC_REG1_NM_MASK >> XSDFEC_REG1_NM_LSB))
460 dev_err(xsdfec->dev, "NM is beyond 10 bits");
461 nm = (nm << XSDFEC_REG1_NM_LSB) & XSDFEC_REG1_NM_MASK;
463 wdata = nm | no_packing | psize;
464 if (XSDFEC_LDPC_CODE_REG1_ADDR_BASE + (offset * XSDFEC_LDPC_REG_JUMP)
465 > XSDFEC_LDPC_CODE_REG1_ADDR_HIGH) {
467 "Writing outside of LDPC reg1 space 0x%x",
468 XSDFEC_LDPC_CODE_REG1_ADDR_BASE +
469 (offset * XSDFEC_LDPC_REG_JUMP));
472 xsdfec_regwrite(xsdfec, XSDFEC_LDPC_CODE_REG1_ADDR_BASE +
473 (offset * XSDFEC_LDPC_REG_JUMP), wdata);
477 #define XSDFEC_REG2_NLAYERS_MASK (0x000001FF)
478 #define XSDFEC_REG2_NLAYERS_LSB (0)
479 #define XSDFEC_REG2_NNMQC_MASK (0x000FFE00)
480 #define XSDFEC_REG2_NMQC_LSB (9)
481 #define XSDFEC_REG2_NORM_TYPE_MASK (0x00100000)
482 #define XSDFEC_REG2_NORM_TYPE_LSB (20)
483 #define XSDFEC_REG2_SPECIAL_QC_MASK (0x00200000)
484 #define XSDFEC_REG2_SPEICAL_QC_LSB (21)
485 #define XSDFEC_REG2_NO_FINAL_PARITY_MASK (0x00400000)
486 #define XSDFEC_REG2_NO_FINAL_PARITY_LSB (22)
487 #define XSDFEC_REG2_MAX_SCHEDULE_MASK (0x01800000)
488 #define XSDFEC_REG2_MAX_SCHEDULE_LSB (23)
491 xsdfec_reg2_write(struct xsdfec_dev *xsdfec, u32 nlayers, u32 nmqc,
492 u32 norm_type, u32 special_qc, u32 no_final_parity,
493 u32 max_schedule, u32 offset)
497 if (nlayers & ~(XSDFEC_REG2_NLAYERS_MASK >>
498 XSDFEC_REG2_NLAYERS_LSB))
499 dev_err(xsdfec->dev, "Nlayers exceeds 9 bits");
500 nlayers &= XSDFEC_REG2_NLAYERS_MASK;
502 if (nmqc & ~(XSDFEC_REG2_NNMQC_MASK >> XSDFEC_REG2_NMQC_LSB))
503 dev_err(xsdfec->dev, "NMQC exceeds 11 bits");
504 nmqc = (nmqc << XSDFEC_REG2_NMQC_LSB) & XSDFEC_REG2_NNMQC_MASK;
507 dev_err(xsdfec->dev, "Norm type is invalid");
508 norm_type = ((norm_type << XSDFEC_REG2_NORM_TYPE_LSB) &
509 XSDFEC_REG2_NORM_TYPE_MASK);
511 dev_err(xsdfec->dev, "Special QC in invalid");
512 special_qc = ((special_qc << XSDFEC_REG2_SPEICAL_QC_LSB) &
513 XSDFEC_REG2_SPECIAL_QC_MASK);
515 if (no_final_parity > 1)
516 dev_err(xsdfec->dev, "No final parity check invalid");
518 ((no_final_parity << XSDFEC_REG2_NO_FINAL_PARITY_LSB) &
519 XSDFEC_REG2_NO_FINAL_PARITY_MASK);
520 if (max_schedule & ~(XSDFEC_REG2_MAX_SCHEDULE_MASK >>
521 XSDFEC_REG2_MAX_SCHEDULE_LSB))
522 dev_err(xsdfec->dev, "Max Schdule exceeds 2 bits");
523 max_schedule = ((max_schedule << XSDFEC_REG2_MAX_SCHEDULE_LSB) &
524 XSDFEC_REG2_MAX_SCHEDULE_MASK);
526 wdata = (max_schedule | no_final_parity | special_qc | norm_type |
529 if (XSDFEC_LDPC_CODE_REG2_ADDR_BASE + (offset * XSDFEC_LDPC_REG_JUMP)
530 > XSDFEC_LDPC_CODE_REG2_ADDR_HIGH) {
532 "Writing outside of LDPC reg2 space 0x%x",
533 XSDFEC_LDPC_CODE_REG2_ADDR_BASE +
534 (offset * XSDFEC_LDPC_REG_JUMP));
537 xsdfec_regwrite(xsdfec, XSDFEC_LDPC_CODE_REG2_ADDR_BASE +
538 (offset * XSDFEC_LDPC_REG_JUMP), wdata);
542 #define XSDFEC_REG3_LA_OFF_LSB (8)
543 #define XSDFEC_REG3_QC_OFF_LSB (16)
545 xsdfec_reg3_write(struct xsdfec_dev *xsdfec, u8 sc_off,
546 u8 la_off, u16 qc_off, u32 offset)
550 wdata = ((qc_off << XSDFEC_REG3_QC_OFF_LSB) |
551 (la_off << XSDFEC_REG3_LA_OFF_LSB) | sc_off);
552 if (XSDFEC_LDPC_CODE_REG3_ADDR_BASE +
553 (offset * XSDFEC_LDPC_REG_JUMP) >
554 XSDFEC_LDPC_CODE_REG3_ADDR_HIGH) {
556 "Writing outside of LDPC reg3 space 0x%x",
557 XSDFEC_LDPC_CODE_REG3_ADDR_BASE +
558 (offset * XSDFEC_LDPC_REG_JUMP));
561 xsdfec_regwrite(xsdfec, XSDFEC_LDPC_CODE_REG3_ADDR_BASE +
562 (offset * XSDFEC_LDPC_REG_JUMP), wdata);
566 #define XSDFEC_SC_TABLE_DEPTH (0x3fc)
567 #define XSDFEC_REG_WIDTH_JUMP (4)
569 xsdfec_sc_table_write(struct xsdfec_dev *xsdfec, u32 offset,
570 u32 *sc_ptr, u32 len)
575 * Writes that go beyond the length of
576 * Shared Scale(SC) table should fail
578 if ((XSDFEC_REG_WIDTH_JUMP * (offset + len)) > XSDFEC_SC_TABLE_DEPTH) {
579 dev_err(xsdfec->dev, "Write exceeds SC table length");
584 * sc_off tracks the points to the last written location
585 * in the Shared Scale(SC) table. Those shared codes might
586 * be in use. Updating them without quiescing the device
587 * can put the SDFEC device in an indeterminate state
589 if ((XSDFEC_REG_WIDTH_JUMP * offset) < xsdfec->sc_off) {
590 dev_err(xsdfec->dev, "Might write to in use shared SC code");
594 for (reg = 0; reg < len; reg++) {
595 xsdfec_regwrite(xsdfec, XSDFEC_LDPC_SC_TABLE_ADDR_BASE +
596 (offset + reg) * XSDFEC_REG_WIDTH_JUMP, sc_ptr[reg]);
598 xsdfec->sc_off = reg + (XSDFEC_REG_WIDTH_JUMP * offset);
602 #define XSDFEC_LA_TABLE_DEPTH (0xFFC)
604 xsdfec_la_table_write(struct xsdfec_dev *xsdfec, u32 offset,
605 u32 *la_ptr, u32 len)
609 if (XSDFEC_REG_WIDTH_JUMP * (offset + len) > XSDFEC_LA_TABLE_DEPTH) {
610 dev_err(xsdfec->dev, "Write exceeds LA table length");
614 if (XSDFEC_REG_WIDTH_JUMP * offset < xsdfec->la_off) {
615 dev_err(xsdfec->dev, "Might write to in use shared LA code");
619 for (reg = 0; reg < len; reg++) {
620 xsdfec_regwrite(xsdfec, XSDFEC_LDPC_LA_TABLE_ADDR_BASE +
621 (offset + reg) * XSDFEC_REG_WIDTH_JUMP,
624 xsdfec->la_off = reg + (offset * XSDFEC_REG_WIDTH_JUMP);
628 #define XSDFEC_QC_TABLE_DEPTH (0x7FFC)
630 xsdfec_qc_table_write(struct xsdfec_dev *xsdfec,
631 u32 offset, u32 *qc_ptr, u32 len)
635 if (XSDFEC_REG_WIDTH_JUMP * (offset + len) > XSDFEC_QC_TABLE_DEPTH) {
636 dev_err(xsdfec->dev, "Write exceeds QC table length");
640 if (XSDFEC_REG_WIDTH_JUMP * offset < xsdfec->qc_off) {
641 dev_err(xsdfec->dev, "Might write to in use shared LA code");
645 for (reg = 0; reg < len; reg++) {
646 xsdfec_regwrite(xsdfec, XSDFEC_LDPC_QC_TABLE_ADDR_BASE +
647 (offset + reg) * XSDFEC_REG_WIDTH_JUMP, qc_ptr[reg]);
650 xsdfec->qc_off = reg + (offset * XSDFEC_REG_WIDTH_JUMP);
655 xsdfec_add_ldpc(struct xsdfec_dev *xsdfec, void __user *arg)
657 struct xsdfec_ldpc_params *ldpc;
660 ldpc = kzalloc(sizeof(*ldpc), GFP_KERNEL);
664 err = copy_from_user(ldpc, arg, sizeof(*ldpc));
667 "%s failed to copy from user for SDFEC%d",
668 __func__, xsdfec->fec_id);
671 if (xsdfec->code == XSDFEC_TURBO_CODE) {
673 "%s: Unable to write LDPC to SDFEC%d check DT",
674 __func__, xsdfec->fec_id);
677 xsdfec->code = XSDFEC_LDPC_CODE;
678 /* Disable Write Protection before proceeding */
679 if (xsdfec->wr_protect)
680 xsdfec_wr_protect(xsdfec, false);
682 /* Write LDPC to CODE Register */
683 xsdfec_regwrite(xsdfec, XSDFEC_FEC_CODE_ADDR, (xsdfec->code - 1));
685 err = xsdfec_reg0_write(xsdfec, ldpc->n, ldpc->k, ldpc->code_id);
690 err = xsdfec_reg1_write(xsdfec, ldpc->psize, ldpc->no_packing,
691 ldpc->nm, ldpc->code_id);
696 err = xsdfec_reg2_write(xsdfec, ldpc->nlayers, ldpc->nmqc,
697 ldpc->norm_type, ldpc->special_qc,
698 ldpc->no_final_parity, ldpc->max_schedule,
704 err = xsdfec_reg3_write(xsdfec, ldpc->sc_off,
705 ldpc->la_off, ldpc->qc_off, ldpc->code_id);
709 /* Write Shared Codes */
710 err = xsdfec_sc_table_write(xsdfec, ldpc->sc_off,
711 ldpc->sc_table, ldpc->nlayers);
715 err = xsdfec_la_table_write(xsdfec, 4 * ldpc->la_off,
716 ldpc->la_table, ldpc->nlayers);
720 err = xsdfec_qc_table_write(xsdfec, 4 * ldpc->qc_off,
721 ldpc->qc_table, ldpc->nqc);
733 static int xsdfec_start(struct xsdfec_dev *xsdfec)
737 /* Verify Code is loaded */
738 if (xsdfec->code == XSDFEC_CODE_INVALID) {
740 "%s : set code before start for SDFEC%d",
741 __func__, xsdfec->fec_id);
744 regread = xsdfec_regread(xsdfec, XSDFEC_FEC_CODE_ADDR);
746 if (regread + 1 != xsdfec->code) {
748 "%s SDFEC HW code does not match driver code",
752 /* Set Order to maintain order */
753 xsdfec->order = XSDFEC_MAINTAIN_ORDER;
754 xsdfec_regwrite(xsdfec, XSDFEC_ORDER_ADDR, (xsdfec->order - 1));
756 xsdfec_regwrite(xsdfec, XSDFEC_AXIS_WIDTH_ADDR, 0);
757 /* Set AXIS enable */
758 xsdfec_regwrite(xsdfec,
759 XSDFEC_AXIS_ENABLE_ADDR,
760 XSDFEC_AXIS_ENABLE_MASK);
761 /* Write Protect Code and Registers */
762 xsdfec_wr_protect(xsdfec, true);
764 xsdfec->state = XSDFEC_STARTED;
769 xsdfec_stop(struct xsdfec_dev *xsdfec)
773 if (xsdfec->state != XSDFEC_STARTED)
774 dev_err(xsdfec->dev, "Device not started correctly");
775 /* Disable Write Protect */
776 xsdfec_wr_protect(xsdfec, false);
777 /* Disable AXIS_ENABLE register */
778 regread = xsdfec_regread(xsdfec, XSDFEC_AXIS_ENABLE_ADDR);
779 regread &= (~XSDFEC_AXIS_ENABLE_MASK);
780 xsdfec_regwrite(xsdfec, XSDFEC_AXIS_ENABLE_ADDR, regread);
782 xsdfec->state = XSDFEC_STOPPED;
787 * Reset will happen asynchronously
788 * since there is no in-band reset register
789 * Prepare driver for reset
793 xsdfec_reset_req(struct xsdfec_dev *xsdfec)
795 xsdfec->state = XSDFEC_INIT;
796 xsdfec->order = XSDFEC_INVALID_ORDER;
800 xsdfec->wr_protect = false;
801 atomic_set(&xsdfec->isr_err_count, 0);
802 atomic_set(&xsdfec->uecc_count, 0);
803 atomic_set(&xsdfec->cecc_count, 0);
804 atomic_inc(&xsdfec->reset_count);
809 xsdfec_dev_ioctl(struct file *fptr, unsigned int cmd, unsigned long data)
811 struct xsdfec_dev *xsdfec = fptr->private_data;
812 void __user *arg = (void __user *)data;
820 /* In failed state allow only reset and get status IOCTLs */
821 if (xsdfec->state == XSDFEC_NEEDS_RESET &&
822 (cmd != XSDFEC_RESET_REQ && cmd != XSDFEC_GET_STATUS)) {
824 "SDFEC%d in failed state. Reset Required",
830 case XSDFEC_START_DEV:
831 rval = xsdfec_start(xsdfec);
833 case XSDFEC_STOP_DEV:
834 rval = xsdfec_stop(xsdfec);
836 case XSDFEC_RESET_REQ:
837 rval = xsdfec_reset_req(xsdfec);
839 case XSDFEC_GET_STATUS:
840 rval = xsdfec_get_status(xsdfec, arg);
842 case XSDFEC_GET_CONFIG:
843 rval = xsdfec_get_config(xsdfec, arg);
846 rval = xsdfec_set_irq(xsdfec, arg);
848 case XSDFEC_SET_TURBO:
849 rval = xsdfec_set_turbo(xsdfec, arg);
851 case XSDFEC_ADD_LDPC:
852 rval = xsdfec_add_ldpc(xsdfec, arg);
855 /* Should not get here */
856 dev_err(xsdfec->dev, "Undefined SDFEC IOCTL");
863 xsdfec_poll(struct file *file, poll_table *wait)
866 struct xsdfec_dev *xsdfec = file->private_data;
869 return POLLNVAL | POLLHUP;
871 poll_wait(file, &xsdfec->waitq, wait);
873 /* XSDFEC ISR detected an error */
874 if (xsdfec->state == XSDFEC_NEEDS_RESET)
875 mask = POLLIN | POLLRDNORM;
877 mask = POLLPRI | POLLERR;
882 static const struct file_operations xsdfec_fops = {
883 .owner = THIS_MODULE,
884 .open = xsdfec_dev_open,
885 .release = xsdfec_dev_release,
886 .unlocked_ioctl = xsdfec_dev_ioctl,
891 xsdfec_parse_of(struct xsdfec_dev *xsdfec)
893 struct device *dev = xsdfec->dev;
894 struct device_node *node = dev->of_node;
896 const char *fec_code;
897 const char *fec_op_mode;
899 rval = of_property_read_string(node,
900 "xlnx,sdfec-op-mode",
903 dev_err(dev, "xlnx,sdfec-op-mode not in DT");
907 if (!strcasecmp(fec_op_mode, "encode")) {
908 xsdfec->op_mode = XSDFEC_ENCODE;
909 } else if (!strcasecmp(fec_op_mode, "decode")) {
910 xsdfec->op_mode = XSDFEC_DECODE;
912 dev_err(dev, "Encode or Decode not specified in DT");
916 rval = of_property_read_string(node, "xlnx,sdfec-code", &fec_code);
918 dev_err(dev, "xlnx,sdfec-code not in DT");
922 if (!strcasecmp(fec_code, "ldpc")) {
923 xsdfec->code = XSDFEC_LDPC_CODE;
924 } else if (!strcasecmp(fec_code, "turbo")) {
925 xsdfec->code = XSDFEC_TURBO_CODE;
927 dev_err(xsdfec->dev, "Invalid Op Mode in DT");
935 xsdfec_log_ecc_errors(struct xsdfec_dev *xsdfec, u32 ecc_err)
940 cecc = ecc_err & XSDFEC_ECC_ISR_SBE;
941 uecc = ecc_err & XSDFEC_ECC_ISR_MBE;
943 uecc_cnt = atomic_add_return(hweight32(uecc), &xsdfec->uecc_count);
944 atomic_add(hweight32(cecc), &xsdfec->cecc_count);
946 if (uecc_cnt > 0 && uecc_cnt < XSDFEC_ERROR_MAX_THRESHOLD) {
948 "Multi-bit error on xsdfec%d. Needs reset",
952 /* Clear ECC errors */
953 xsdfec_regwrite(xsdfec, XSDFEC_ECC_ISR_ADDR, 0);
957 xsdfec_log_isr_errors(struct xsdfec_dev *xsdfec, u32 isr_err)
961 /* Update ISR error counts */
962 isr_err_cnt = atomic_add_return(hweight32(isr_err),
963 &xsdfec->isr_err_count);
964 if (isr_err_cnt > 0 && isr_err_cnt < XSDFEC_ERROR_MAX_THRESHOLD) {
966 "Tlast,or DIN_WORDS or DOUT_WORDS not correct");
969 /* Clear ISR error status */
970 xsdfec_regwrite(xsdfec, XSDFEC_ECC_ISR_ADDR, 0);
974 xsdfec_reset_required(struct xsdfec_dev *xsdfec)
976 xsdfec->state = XSDFEC_NEEDS_RESET;
980 xsdfec_irq_thread(int irq, void *dev_id)
982 struct xsdfec_dev *xsdfec = dev_id;
983 irqreturn_t ret = IRQ_HANDLED;
986 bool fatal_err = false;
988 WARN_ON(xsdfec->irq != irq);
990 /* Mask Interrupts */
991 xsdfec_isr_enable(xsdfec, false);
992 xsdfec_ecc_isr_enable(xsdfec, false);
994 /* Read Interrupt Status Registers */
995 ecc_err = xsdfec_regread(xsdfec, XSDFEC_ECC_ISR_ADDR);
996 isr_err = xsdfec_regread(xsdfec, XSDFEC_ISR_ADDR);
998 if (ecc_err & XSDFEC_ECC_ISR_MBE) {
999 /* Multi-Bit Errors need Reset */
1000 xsdfec_log_ecc_errors(xsdfec, ecc_err);
1001 xsdfec_reset_required(xsdfec);
1003 } else if (isr_err & XSDFEC_ISR_MASK) {
1005 * Tlast, DIN_WORDS and DOUT_WORDS related
1008 xsdfec_log_isr_errors(xsdfec, isr_err);
1009 xsdfec_reset_required(xsdfec);
1011 } else if (ecc_err & XSDFEC_ECC_ISR_SBE) {
1012 /* Correctable ECC Errors */
1013 xsdfec_log_ecc_errors(xsdfec, ecc_err);
1019 wake_up_interruptible(&xsdfec->waitq);
1021 /* Unmaks Interrupts */
1022 xsdfec_isr_enable(xsdfec, true);
1023 xsdfec_ecc_isr_enable(xsdfec, true);
1029 xsdfec_probe(struct platform_device *pdev)
1031 struct xsdfec_dev *xsdfec;
1033 struct device *dev_create;
1034 struct resource *res;
1036 bool irq_enabled = true;
1038 xsdfec = devm_kzalloc(&pdev->dev, sizeof(*xsdfec), GFP_KERNEL);
1042 xsdfec->dev = &pdev->dev;
1043 if (atomic_read(&xsdfec_ndevs) > DRIVER_MAX_DEV) {
1045 "Cannot instantiate more than %d SDFEC instances",
1046 (DRIVER_MAX_DEV + 1));
1050 xsdfec->fec_id = atomic_read(&xsdfec_ndevs);
1053 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1054 xsdfec->regs = devm_ioremap_resource(dev, res);
1055 if (IS_ERR(xsdfec->regs)) {
1056 dev_err(dev, "Unable to map resource");
1057 err = PTR_ERR(xsdfec->regs);
1058 goto err_xsdfec_dev;
1061 xsdfec->irq = platform_get_irq(pdev, 0);
1062 if (xsdfec->irq < 0) {
1063 dev_dbg(dev, "platform_get_irq failed");
1064 irq_enabled = false;
1067 err = xsdfec_parse_of(xsdfec);
1069 goto err_xsdfec_dev;
1071 /* Save driver private data */
1072 platform_set_drvdata(pdev, xsdfec);
1075 init_waitqueue_head(&xsdfec->waitq);
1076 /* Register IRQ thread */
1077 err = devm_request_threaded_irq(dev, xsdfec->irq, NULL,
1083 dev_err(dev, "unable to request IRQ%d", xsdfec->irq);
1084 goto err_xsdfec_dev;
1088 cdev_init(&xsdfec->xsdfec_cdev, &xsdfec_fops);
1089 xsdfec->xsdfec_cdev.owner = THIS_MODULE;
1090 err = cdev_add(&xsdfec->xsdfec_cdev,
1091 MKDEV(MAJOR(xsdfec_devt), xsdfec->fec_id), 1);
1093 dev_err(dev, "cdev_add failed");
1095 goto err_xsdfec_dev;
1098 if (!xsdfec_class) {
1100 dev_err(dev, "xsdfec class not created correctly");
1101 goto err_xsdfec_cdev;
1104 dev_create = device_create(xsdfec_class, dev,
1105 MKDEV(MAJOR(xsdfec_devt), xsdfec->fec_id),
1106 xsdfec, "xsdfec%d", xsdfec->fec_id);
1107 if (IS_ERR(dev_create)) {
1108 dev_err(dev, "unable to create device");
1109 err = PTR_ERR(dev_create);
1110 goto err_xsdfec_cdev;
1113 atomic_set(&xsdfec->open_count, 1);
1114 dev_info(dev, "XSDFEC%d Probe Successful", xsdfec->fec_id);
1115 atomic_inc(&xsdfec_ndevs);
1118 /* Failure cleanup */
1120 cdev_del(&xsdfec->xsdfec_cdev);
1126 xsdfec_remove(struct platform_device *pdev)
1128 struct xsdfec_dev *xsdfec;
1129 struct device *dev = &pdev->dev;
1131 xsdfec = platform_get_drvdata(pdev);
1135 if (!xsdfec_class) {
1136 dev_err(dev, "xsdfec_class is NULL");
1140 device_destroy(xsdfec_class,
1141 MKDEV(MAJOR(xsdfec_devt), xsdfec->fec_id));
1142 cdev_del(&xsdfec->xsdfec_cdev);
1143 atomic_dec(&xsdfec_ndevs);
1147 static const struct of_device_id xsdfec_of_match[] = {
1148 { .compatible = "xlnx,fec-engine", },
1149 { /* end of table */ }
1151 MODULE_DEVICE_TABLE(of, xsdfec_of_match);
1153 static struct platform_driver xsdfec_driver = {
1155 .name = "xilinx-sdfec",
1156 .of_match_table = xsdfec_of_match,
1158 .probe = xsdfec_probe,
1159 .remove = xsdfec_remove,
1162 static int __init xsdfec_init_mod(void)
1166 xsdfec_class = class_create(THIS_MODULE, DRIVER_NAME);
1167 if (IS_ERR(xsdfec_class)) {
1168 err = PTR_ERR(xsdfec_class);
1169 pr_err("%s : Unable to register xsdfec class", __func__);
1173 err = alloc_chrdev_region(&xsdfec_devt,
1174 0, DRIVER_MAX_DEV, DRIVER_NAME);
1176 pr_err("%s : Unable to get major number", __func__);
1177 goto err_xsdfec_class;
1180 err = platform_driver_register(&xsdfec_driver);
1182 pr_err("%s Unabled to register %s driver",
1183 __func__, DRIVER_NAME);
1184 goto err_xsdfec_drv;
1190 unregister_chrdev_region(xsdfec_devt, DRIVER_MAX_DEV);
1192 class_destroy(xsdfec_class);
1196 static void __exit xsdfec_cleanup_mod(void)
1198 platform_driver_unregister(&xsdfec_driver);
1199 unregister_chrdev_region(xsdfec_devt, DRIVER_MAX_DEV);
1200 class_destroy(xsdfec_class);
1201 xsdfec_class = NULL;
1204 module_init(xsdfec_init_mod);
1205 module_exit(xsdfec_cleanup_mod);
1207 MODULE_AUTHOR("Xilinx, Inc");
1208 MODULE_DESCRIPTION("Xilinx SD-FEC16 Driver");
1209 MODULE_LICENSE("GPL");
1210 MODULE_VERSION(DRIVER_VERSION);