projects/linux·blob master · refs

1446 lines · 37.3 KiB

1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Xilinx SDFEC
4 *
5 * Copyright (C) 2019 Xilinx, Inc.
6 *
7 * Description:
8 * This driver is developed for SDFEC16 (Soft Decision FEC 16nm)
9 * IP. It exposes a char device which supports file operations
10 * like open(), close() and ioctl().
11 */
12
13#include <linux/miscdevice.h>
14#include <linux/io.h>
15#include <linux/interrupt.h>
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/of.h>
19#include <linux/platform_device.h>
20#include <linux/poll.h>
21#include <linux/slab.h>
22#include <linux/clk.h>
23#include <linux/compat.h>
24#include <linux/highmem.h>
25
26#include <uapi/misc/xilinx_sdfec.h>
27
28#define DEV_NAME_LEN 12
29
30static DEFINE_IDA(dev_nrs);
31
32/* Xilinx SDFEC Register Map */
33/* CODE_WRI_PROTECT Register */
34#define XSDFEC_CODE_WR_PROTECT_ADDR (0x4)
35
36/* ACTIVE Register */
37#define XSDFEC_ACTIVE_ADDR (0x8)
38#define XSDFEC_IS_ACTIVITY_SET (0x1)
39
40/* AXIS_WIDTH Register */
41#define XSDFEC_AXIS_WIDTH_ADDR (0xC)
42#define XSDFEC_AXIS_DOUT_WORDS_LSB (5)
43#define XSDFEC_AXIS_DOUT_WIDTH_LSB (3)
44#define XSDFEC_AXIS_DIN_WORDS_LSB (2)
45#define XSDFEC_AXIS_DIN_WIDTH_LSB (0)
46
47/* AXIS_ENABLE Register */
48#define XSDFEC_AXIS_ENABLE_ADDR (0x10)
49#define XSDFEC_AXIS_OUT_ENABLE_MASK (0x38)
50#define XSDFEC_AXIS_IN_ENABLE_MASK (0x7)
51#define XSDFEC_AXIS_ENABLE_MASK \
52 (XSDFEC_AXIS_OUT_ENABLE_MASK | XSDFEC_AXIS_IN_ENABLE_MASK)
53
54/* FEC_CODE Register */
55#define XSDFEC_FEC_CODE_ADDR (0x14)
56
57/* ORDER Register Map */
58#define XSDFEC_ORDER_ADDR (0x18)
59
60/* Interrupt Status Register */
61#define XSDFEC_ISR_ADDR (0x1C)
62/* Interrupt Status Register Bit Mask */
63#define XSDFEC_ISR_MASK (0x3F)
64
65/* Write Only - Interrupt Enable Register */
66#define XSDFEC_IER_ADDR (0x20)
67/* Write Only - Interrupt Disable Register */
68#define XSDFEC_IDR_ADDR (0x24)
69/* Read Only - Interrupt Mask Register */
70#define XSDFEC_IMR_ADDR (0x28)
71
72/* ECC Interrupt Status Register */
73#define XSDFEC_ECC_ISR_ADDR (0x2C)
74/* Single Bit Errors */
75#define XSDFEC_ECC_ISR_SBE_MASK (0x7FF)
76/* PL Initialize Single Bit Errors */
77#define XSDFEC_PL_INIT_ECC_ISR_SBE_MASK (0x3C00000)
78/* Multi Bit Errors */
79#define XSDFEC_ECC_ISR_MBE_MASK (0x3FF800)
80/* PL Initialize Multi Bit Errors */
81#define XSDFEC_PL_INIT_ECC_ISR_MBE_MASK (0x3C000000)
82/* Multi Bit Error to Event Shift */
83#define XSDFEC_ECC_ISR_MBE_TO_EVENT_SHIFT (11)
84/* PL Initialize Multi Bit Error to Event Shift */
85#define XSDFEC_PL_INIT_ECC_ISR_MBE_TO_EVENT_SHIFT (4)
86/* ECC Interrupt Status Bit Mask */
87#define XSDFEC_ECC_ISR_MASK (XSDFEC_ECC_ISR_SBE_MASK | XSDFEC_ECC_ISR_MBE_MASK)
88/* ECC Interrupt Status PL Initialize Bit Mask */
89#define XSDFEC_PL_INIT_ECC_ISR_MASK \
90 (XSDFEC_PL_INIT_ECC_ISR_SBE_MASK | XSDFEC_PL_INIT_ECC_ISR_MBE_MASK)
91/* ECC Interrupt Status All Bit Mask */
92#define XSDFEC_ALL_ECC_ISR_MASK \
93 (XSDFEC_ECC_ISR_MASK | XSDFEC_PL_INIT_ECC_ISR_MASK)
94/* ECC Interrupt Status Single Bit Errors Mask */
95#define XSDFEC_ALL_ECC_ISR_SBE_MASK \
96 (XSDFEC_ECC_ISR_SBE_MASK | XSDFEC_PL_INIT_ECC_ISR_SBE_MASK)
97/* ECC Interrupt Status Multi Bit Errors Mask */
98#define XSDFEC_ALL_ECC_ISR_MBE_MASK \
99 (XSDFEC_ECC_ISR_MBE_MASK | XSDFEC_PL_INIT_ECC_ISR_MBE_MASK)
100
101/* Write Only - ECC Interrupt Enable Register */
102#define XSDFEC_ECC_IER_ADDR (0x30)
103/* Write Only - ECC Interrupt Disable Register */
104#define XSDFEC_ECC_IDR_ADDR (0x34)
105/* Read Only - ECC Interrupt Mask Register */
106#define XSDFEC_ECC_IMR_ADDR (0x38)
107
108/* BYPASS Register */
109#define XSDFEC_BYPASS_ADDR (0x3C)
110
111/* Turbo Code Register */
112#define XSDFEC_TURBO_ADDR (0x100)
113#define XSDFEC_TURBO_SCALE_MASK (0xFFF)
114#define XSDFEC_TURBO_SCALE_BIT_POS (8)
115#define XSDFEC_TURBO_SCALE_MAX (15)
116
117/* REG0 Register */
118#define XSDFEC_LDPC_CODE_REG0_ADDR_BASE (0x2000)
119#define XSDFEC_LDPC_CODE_REG0_ADDR_HIGH (0x27F0)
120#define XSDFEC_REG0_N_MIN (4)
121#define XSDFEC_REG0_N_MAX (32768)
122#define XSDFEC_REG0_N_MUL_P (256)
123#define XSDFEC_REG0_N_LSB (0)
124#define XSDFEC_REG0_K_MIN (2)
125#define XSDFEC_REG0_K_MAX (32766)
126#define XSDFEC_REG0_K_MUL_P (256)
127#define XSDFEC_REG0_K_LSB (16)
128
129/* REG1 Register */
130#define XSDFEC_LDPC_CODE_REG1_ADDR_BASE (0x2004)
131#define XSDFEC_LDPC_CODE_REG1_ADDR_HIGH (0x27f4)
132#define XSDFEC_REG1_PSIZE_MIN (2)
133#define XSDFEC_REG1_PSIZE_MAX (512)
134#define XSDFEC_REG1_NO_PACKING_MASK (0x400)
135#define XSDFEC_REG1_NO_PACKING_LSB (10)
136#define XSDFEC_REG1_NM_MASK (0xFF800)
137#define XSDFEC_REG1_NM_LSB (11)
138#define XSDFEC_REG1_BYPASS_MASK (0x100000)
139
140/* REG2 Register */
141#define XSDFEC_LDPC_CODE_REG2_ADDR_BASE (0x2008)
142#define XSDFEC_LDPC_CODE_REG2_ADDR_HIGH (0x27f8)
143#define XSDFEC_REG2_NLAYERS_MIN (1)
144#define XSDFEC_REG2_NLAYERS_MAX (256)
145#define XSDFEC_REG2_NNMQC_MASK (0xFFE00)
146#define XSDFEC_REG2_NMQC_LSB (9)
147#define XSDFEC_REG2_NORM_TYPE_MASK (0x100000)
148#define XSDFEC_REG2_NORM_TYPE_LSB (20)
149#define XSDFEC_REG2_SPECIAL_QC_MASK (0x200000)
150#define XSDFEC_REG2_SPEICAL_QC_LSB (21)
151#define XSDFEC_REG2_NO_FINAL_PARITY_MASK (0x400000)
152#define XSDFEC_REG2_NO_FINAL_PARITY_LSB (22)
153#define XSDFEC_REG2_MAX_SCHEDULE_MASK (0x1800000)
154#define XSDFEC_REG2_MAX_SCHEDULE_LSB (23)
155
156/* REG3 Register */
157#define XSDFEC_LDPC_CODE_REG3_ADDR_BASE (0x200C)
158#define XSDFEC_LDPC_CODE_REG3_ADDR_HIGH (0x27FC)
159#define XSDFEC_REG3_LA_OFF_LSB (8)
160#define XSDFEC_REG3_QC_OFF_LSB (16)
161
162#define XSDFEC_LDPC_REG_JUMP (0x10)
163#define XSDFEC_REG_WIDTH_JUMP (4)
164
165/* The maximum number of pinned pages */
166#define MAX_NUM_PAGES ((XSDFEC_QC_TABLE_DEPTH / PAGE_SIZE) + 1)
167
168/**
169 * struct xsdfec_clks - For managing SD-FEC clocks
170 * @core_clk: Main processing clock for core
171 * @axi_clk: AXI4-Lite memory-mapped clock
172 * @din_words_clk: DIN Words AXI4-Stream Slave clock
173 * @din_clk: DIN AXI4-Stream Slave clock
174 * @dout_clk: DOUT Words AXI4-Stream Slave clock
175 * @dout_words_clk: DOUT AXI4-Stream Slave clock
176 * @ctrl_clk: Control AXI4-Stream Slave clock
177 * @status_clk: Status AXI4-Stream Slave clock
178 */
179struct xsdfec_clks {
180 struct clk *core_clk;
181 struct clk *axi_clk;
182 struct clk *din_words_clk;
183 struct clk *din_clk;
184 struct clk *dout_clk;
185 struct clk *dout_words_clk;
186 struct clk *ctrl_clk;
187 struct clk *status_clk;
188};
189
190/**
191 * struct xsdfec_dev - Driver data for SDFEC
192 * @miscdev: Misc device handle
193 * @clks: Clocks managed by the SDFEC driver
194 * @waitq: Driver wait queue
195 * @config: Configuration of the SDFEC device
196 * @dev_name: Device name
197 * @flags: spinlock flags
198 * @regs: device physical base address
199 * @dev: pointer to device struct
200 * @state: State of the SDFEC device
201 * @error_data_lock: Error counter and states spinlock
202 * @dev_id: Device ID
203 * @isr_err_count: Count of ISR errors
204 * @cecc_count: Count of Correctable ECC errors (SBE)
205 * @uecc_count: Count of Uncorrectable ECC errors (MBE)
206 * @irq: IRQ number
207 * @state_updated: indicates State updated by interrupt handler
208 * @stats_updated: indicates Stats updated by interrupt handler
209 * @intr_enabled: indicates IRQ enabled
210 *
211 * This structure contains necessary state for SDFEC driver to operate
212 */
213struct xsdfec_dev {
214 struct miscdevice miscdev;
215 struct xsdfec_clks clks;
216 wait_queue_head_t waitq;
217 struct xsdfec_config config;
218 char dev_name[DEV_NAME_LEN];
219 unsigned long flags;
220 void __iomem *regs;
221 struct device *dev;
222 enum xsdfec_state state;
223 /* Spinlock to protect state_updated and stats_updated */
224 spinlock_t error_data_lock;
225 int dev_id;
226 u32 isr_err_count;
227 u32 cecc_count;
228 u32 uecc_count;
229 int irq;
230 bool state_updated;
231 bool stats_updated;
232 bool intr_enabled;
233};
234
235static inline void xsdfec_regwrite(struct xsdfec_dev *xsdfec, u32 addr,
236 u32 value)
237{
238 dev_dbg(xsdfec->dev, "Writing 0x%x to offset 0x%x", value, addr);
239 iowrite32(value, xsdfec->regs + addr);
240}
241
242static inline u32 xsdfec_regread(struct xsdfec_dev *xsdfec, u32 addr)
243{
244 u32 rval;
245
246 rval = ioread32(xsdfec->regs + addr);
247 dev_dbg(xsdfec->dev, "Read value = 0x%x from offset 0x%x", rval, addr);
248 return rval;
249}
250
251static void update_bool_config_from_reg(struct xsdfec_dev *xsdfec,
252 u32 reg_offset, u32 bit_num,
253 char *config_value)
254{
255 u32 reg_val;
256 u32 bit_mask = 1 << bit_num;
257
258 reg_val = xsdfec_regread(xsdfec, reg_offset);
259 *config_value = (reg_val & bit_mask) > 0;
260}
261
262static void update_config_from_hw(struct xsdfec_dev *xsdfec)
263{
264 u32 reg_value;
265 bool sdfec_started;
266
267 /* Update the Order */
268 reg_value = xsdfec_regread(xsdfec, XSDFEC_ORDER_ADDR);
269 xsdfec->config.order = reg_value;
270
271 update_bool_config_from_reg(xsdfec, XSDFEC_BYPASS_ADDR,
272 0, /* Bit Number, maybe change to mask */
273 &xsdfec->config.bypass);
274
275 update_bool_config_from_reg(xsdfec, XSDFEC_CODE_WR_PROTECT_ADDR,
276 0, /* Bit Number */
277 &xsdfec->config.code_wr_protect);
278
279 reg_value = xsdfec_regread(xsdfec, XSDFEC_IMR_ADDR);
280 xsdfec->config.irq.enable_isr = (reg_value & XSDFEC_ISR_MASK) > 0;
281
282 reg_value = xsdfec_regread(xsdfec, XSDFEC_ECC_IMR_ADDR);
283 xsdfec->config.irq.enable_ecc_isr =
284 (reg_value & XSDFEC_ECC_ISR_MASK) > 0;
285
286 reg_value = xsdfec_regread(xsdfec, XSDFEC_AXIS_ENABLE_ADDR);
287 sdfec_started = (reg_value & XSDFEC_AXIS_IN_ENABLE_MASK) > 0;
288 if (sdfec_started)
289 xsdfec->state = XSDFEC_STARTED;
290 else
291 xsdfec->state = XSDFEC_STOPPED;
292}
293
294static int xsdfec_get_status(struct xsdfec_dev *xsdfec, void __user *arg)
295{
296 struct xsdfec_status status;
297 int err;
298
299 memset(&status, 0, sizeof(status));
300 spin_lock_irqsave(&xsdfec->error_data_lock, xsdfec->flags);
301 status.state = xsdfec->state;
302 xsdfec->state_updated = false;
303 spin_unlock_irqrestore(&xsdfec->error_data_lock, xsdfec->flags);
304 status.activity = (xsdfec_regread(xsdfec, XSDFEC_ACTIVE_ADDR) &
305 XSDFEC_IS_ACTIVITY_SET);
306
307 err = copy_to_user(arg, &status, sizeof(status));
308 if (err)
309 err = -EFAULT;
310
311 return err;
312}
313
314static int xsdfec_get_config(struct xsdfec_dev *xsdfec, void __user *arg)
315{
316 int err;
317
318 err = copy_to_user(arg, &xsdfec->config, sizeof(xsdfec->config));
319 if (err)
320 err = -EFAULT;
321
322 return err;
323}
324
325static int xsdfec_isr_enable(struct xsdfec_dev *xsdfec, bool enable)
326{
327 u32 mask_read;
328
329 if (enable) {
330 /* Enable */
331 xsdfec_regwrite(xsdfec, XSDFEC_IER_ADDR, XSDFEC_ISR_MASK);
332 mask_read = xsdfec_regread(xsdfec, XSDFEC_IMR_ADDR);
333 if (mask_read & XSDFEC_ISR_MASK) {
334 dev_dbg(xsdfec->dev,
335 "SDFEC enabling irq with IER failed");
336 return -EIO;
337 }
338 } else {
339 /* Disable */
340 xsdfec_regwrite(xsdfec, XSDFEC_IDR_ADDR, XSDFEC_ISR_MASK);
341 mask_read = xsdfec_regread(xsdfec, XSDFEC_IMR_ADDR);
342 if ((mask_read & XSDFEC_ISR_MASK) != XSDFEC_ISR_MASK) {
343 dev_dbg(xsdfec->dev,
344 "SDFEC disabling irq with IDR failed");
345 return -EIO;
346 }
347 }
348 return 0;
349}
350
351static int xsdfec_ecc_isr_enable(struct xsdfec_dev *xsdfec, bool enable)
352{
353 u32 mask_read;
354
355 if (enable) {
356 /* Enable */
357 xsdfec_regwrite(xsdfec, XSDFEC_ECC_IER_ADDR,
358 XSDFEC_ALL_ECC_ISR_MASK);
359 mask_read = xsdfec_regread(xsdfec, XSDFEC_ECC_IMR_ADDR);
360 if (mask_read & XSDFEC_ALL_ECC_ISR_MASK) {
361 dev_dbg(xsdfec->dev,
362 "SDFEC enabling ECC irq with ECC IER failed");
363 return -EIO;
364 }
365 } else {
366 /* Disable */
367 xsdfec_regwrite(xsdfec, XSDFEC_ECC_IDR_ADDR,
368 XSDFEC_ALL_ECC_ISR_MASK);
369 mask_read = xsdfec_regread(xsdfec, XSDFEC_ECC_IMR_ADDR);
370 if (!(((mask_read & XSDFEC_ALL_ECC_ISR_MASK) ==
371 XSDFEC_ECC_ISR_MASK) ||
372 ((mask_read & XSDFEC_ALL_ECC_ISR_MASK) ==
373 XSDFEC_PL_INIT_ECC_ISR_MASK))) {
374 dev_dbg(xsdfec->dev,
375 "SDFEC disable ECC irq with ECC IDR failed");
376 return -EIO;
377 }
378 }
379 return 0;
380}
381
382static int xsdfec_set_irq(struct xsdfec_dev *xsdfec, void __user *arg)
383{
384 struct xsdfec_irq irq;
385 int err;
386 int isr_err;
387 int ecc_err;
388
389 err = copy_from_user(&irq, arg, sizeof(irq));
390 if (err)
391 return -EFAULT;
392
393 /* Setup tlast related IRQ */
394 isr_err = xsdfec_isr_enable(xsdfec, irq.enable_isr);
395 if (!isr_err)
396 xsdfec->config.irq.enable_isr = irq.enable_isr;
397
398 /* Setup ECC related IRQ */
399 ecc_err = xsdfec_ecc_isr_enable(xsdfec, irq.enable_ecc_isr);
400 if (!ecc_err)
401 xsdfec->config.irq.enable_ecc_isr = irq.enable_ecc_isr;
402
403 if (isr_err < 0 || ecc_err < 0)
404 err = -EIO;
405
406 return err;
407}
408
409static int xsdfec_set_turbo(struct xsdfec_dev *xsdfec, void __user *arg)
410{
411 struct xsdfec_turbo turbo;
412 int err;
413 u32 turbo_write;
414
415 err = copy_from_user(&turbo, arg, sizeof(turbo));
416 if (err)
417 return -EFAULT;
418
419 if (turbo.alg >= XSDFEC_TURBO_ALG_MAX)
420 return -EINVAL;
421
422 if (turbo.scale > XSDFEC_TURBO_SCALE_MAX)
423 return -EINVAL;
424
425 /* Check to see what device tree says about the FEC codes */
426 if (xsdfec->config.code == XSDFEC_LDPC_CODE)
427 return -EIO;
428
429 turbo_write = ((turbo.scale & XSDFEC_TURBO_SCALE_MASK)
430 << XSDFEC_TURBO_SCALE_BIT_POS) |
431 turbo.alg;
432 xsdfec_regwrite(xsdfec, XSDFEC_TURBO_ADDR, turbo_write);
433 return err;
434}
435
436static int xsdfec_get_turbo(struct xsdfec_dev *xsdfec, void __user *arg)
437{
438 u32 reg_value;
439 struct xsdfec_turbo turbo_params;
440 int err;
441
442 if (xsdfec->config.code == XSDFEC_LDPC_CODE)
443 return -EIO;
444
445 memset(&turbo_params, 0, sizeof(turbo_params));
446 reg_value = xsdfec_regread(xsdfec, XSDFEC_TURBO_ADDR);
447
448 turbo_params.scale = (reg_value & XSDFEC_TURBO_SCALE_MASK) >>
449 XSDFEC_TURBO_SCALE_BIT_POS;
450 turbo_params.alg = reg_value & 0x1;
451
452 err = copy_to_user(arg, &turbo_params, sizeof(turbo_params));
453 if (err)
454 err = -EFAULT;
455
456 return err;
457}
458
459static int xsdfec_ldpc_reg_addr(struct xsdfec_dev *xsdfec, u32 base, u32 high,
460 u32 offset, u32 *addr)
461{
462 if (high < base || offset > (high - base) / XSDFEC_LDPC_REG_JUMP) {
463 dev_dbg(xsdfec->dev,
464 "LDPC register offset %u outside space 0x%x-0x%x",
465 offset, base, high);
466 return -EINVAL;
467 }
468
469 *addr = base + offset * XSDFEC_LDPC_REG_JUMP;
470 return 0;
471}
472
473static int xsdfec_reg0_write(struct xsdfec_dev *xsdfec, u32 n, u32 k, u32 psize,
474 u32 offset)
475{
476 u32 wdata;
477 u32 addr;
478
479 if (n < XSDFEC_REG0_N_MIN || n > XSDFEC_REG0_N_MAX || psize == 0 ||
480 (n > XSDFEC_REG0_N_MUL_P * psize) || n <= k || ((n % psize) != 0)) {
481 dev_dbg(xsdfec->dev, "N value is not in range");
482 return -EINVAL;
483 }
484 n <<= XSDFEC_REG0_N_LSB;
485
486 if (k < XSDFEC_REG0_K_MIN || k > XSDFEC_REG0_K_MAX ||
487 (k > XSDFEC_REG0_K_MUL_P * psize) || ((k % psize) != 0)) {
488 dev_dbg(xsdfec->dev, "K value is not in range");
489 return -EINVAL;
490 }
491 k = k << XSDFEC_REG0_K_LSB;
492 wdata = k | n;
493
494 if (xsdfec_ldpc_reg_addr(xsdfec, XSDFEC_LDPC_CODE_REG0_ADDR_BASE,
495 XSDFEC_LDPC_CODE_REG0_ADDR_HIGH, offset,
496 &addr))
497 return -EINVAL;
498 xsdfec_regwrite(xsdfec, addr, wdata);
499 return 0;
500}
501
502static int xsdfec_reg1_write(struct xsdfec_dev *xsdfec, u32 psize,
503 u32 no_packing, u32 nm, u32 offset)
504{
505 u32 wdata;
506 u32 addr;
507
508 if (psize < XSDFEC_REG1_PSIZE_MIN || psize > XSDFEC_REG1_PSIZE_MAX) {
509 dev_dbg(xsdfec->dev, "Psize is not in range");
510 return -EINVAL;
511 }
512
513 if (no_packing != 0 && no_packing != 1)
514 dev_dbg(xsdfec->dev, "No-packing bit register invalid");
515 no_packing = ((no_packing << XSDFEC_REG1_NO_PACKING_LSB) &
516 XSDFEC_REG1_NO_PACKING_MASK);
517
518 if (nm & ~(XSDFEC_REG1_NM_MASK >> XSDFEC_REG1_NM_LSB))
519 dev_dbg(xsdfec->dev, "NM is beyond 10 bits");
520 nm = (nm << XSDFEC_REG1_NM_LSB) & XSDFEC_REG1_NM_MASK;
521
522 wdata = nm | no_packing | psize;
523 if (xsdfec_ldpc_reg_addr(xsdfec, XSDFEC_LDPC_CODE_REG1_ADDR_BASE,
524 XSDFEC_LDPC_CODE_REG1_ADDR_HIGH, offset,
525 &addr))
526 return -EINVAL;
527 xsdfec_regwrite(xsdfec, addr, wdata);
528 return 0;
529}
530
531static int xsdfec_reg2_write(struct xsdfec_dev *xsdfec, u32 nlayers, u32 nmqc,
532 u32 norm_type, u32 special_qc, u32 no_final_parity,
533 u32 max_schedule, u32 offset)
534{
535 u32 wdata;
536 u32 addr;
537
538 if (nlayers < XSDFEC_REG2_NLAYERS_MIN ||
539 nlayers > XSDFEC_REG2_NLAYERS_MAX) {
540 dev_dbg(xsdfec->dev, "Nlayers is not in range");
541 return -EINVAL;
542 }
543
544 if (nmqc & ~(XSDFEC_REG2_NNMQC_MASK >> XSDFEC_REG2_NMQC_LSB))
545 dev_dbg(xsdfec->dev, "NMQC exceeds 11 bits");
546 nmqc = (nmqc << XSDFEC_REG2_NMQC_LSB) & XSDFEC_REG2_NNMQC_MASK;
547
548 if (norm_type > 1)
549 dev_dbg(xsdfec->dev, "Norm type is invalid");
550 norm_type = ((norm_type << XSDFEC_REG2_NORM_TYPE_LSB) &
551 XSDFEC_REG2_NORM_TYPE_MASK);
552 if (special_qc > 1)
553 dev_dbg(xsdfec->dev, "Special QC in invalid");
554 special_qc = ((special_qc << XSDFEC_REG2_SPEICAL_QC_LSB) &
555 XSDFEC_REG2_SPECIAL_QC_MASK);
556
557 if (no_final_parity > 1)
558 dev_dbg(xsdfec->dev, "No final parity check invalid");
559 no_final_parity =
560 ((no_final_parity << XSDFEC_REG2_NO_FINAL_PARITY_LSB) &
561 XSDFEC_REG2_NO_FINAL_PARITY_MASK);
562 if (max_schedule &
563 ~(XSDFEC_REG2_MAX_SCHEDULE_MASK >> XSDFEC_REG2_MAX_SCHEDULE_LSB))
564 dev_dbg(xsdfec->dev, "Max Schedule exceeds 2 bits");
565 max_schedule = ((max_schedule << XSDFEC_REG2_MAX_SCHEDULE_LSB) &
566 XSDFEC_REG2_MAX_SCHEDULE_MASK);
567
568 wdata = (max_schedule | no_final_parity | special_qc | norm_type |
569 nmqc | nlayers);
570
571 if (xsdfec_ldpc_reg_addr(xsdfec, XSDFEC_LDPC_CODE_REG2_ADDR_BASE,
572 XSDFEC_LDPC_CODE_REG2_ADDR_HIGH, offset,
573 &addr))
574 return -EINVAL;
575 xsdfec_regwrite(xsdfec, addr, wdata);
576 return 0;
577}
578
579static int xsdfec_reg3_write(struct xsdfec_dev *xsdfec, u8 sc_off, u8 la_off,
580 u16 qc_off, u32 offset)
581{
582 u32 wdata;
583 u32 addr;
584
585 wdata = ((qc_off << XSDFEC_REG3_QC_OFF_LSB) |
586 (la_off << XSDFEC_REG3_LA_OFF_LSB) | sc_off);
587 if (xsdfec_ldpc_reg_addr(xsdfec, XSDFEC_LDPC_CODE_REG3_ADDR_BASE,
588 XSDFEC_LDPC_CODE_REG3_ADDR_HIGH, offset,
589 &addr))
590 return -EINVAL;
591 xsdfec_regwrite(xsdfec, addr, wdata);
592 return 0;
593}
594
595static int xsdfec_table_write(struct xsdfec_dev *xsdfec, u32 offset,
596 u32 *src_ptr, u32 len, const u32 base_addr,
597 const u32 depth)
598{
599 u32 reg = 0;
600 int res, i, nr_pages;
601 u32 n;
602 u32 *addr = NULL;
603 struct page *pages[MAX_NUM_PAGES];
604
605 /*
606 * Writes that go beyond the length of
607 * Shared Scale(SC) table should fail
608 */
609 if (offset > depth / XSDFEC_REG_WIDTH_JUMP ||
610 len > depth / XSDFEC_REG_WIDTH_JUMP ||
611 offset + len > depth / XSDFEC_REG_WIDTH_JUMP) {
612 dev_dbg(xsdfec->dev, "Write exceeds SC table length");
613 return -EINVAL;
614 }
615
616 n = (len * XSDFEC_REG_WIDTH_JUMP) / PAGE_SIZE;
617 if ((len * XSDFEC_REG_WIDTH_JUMP) % PAGE_SIZE)
618 n += 1;
619
620 if (WARN_ON_ONCE(n > INT_MAX))
621 return -EINVAL;
622
623 nr_pages = n;
624
625 res = pin_user_pages_fast((unsigned long)src_ptr, nr_pages, 0, pages);
626 if (res < nr_pages) {
627 if (res > 0)
628 unpin_user_pages(pages, res);
629
630 return -EINVAL;
631 }
632
633 for (i = 0; i < nr_pages; i++) {
634 addr = kmap_local_page(pages[i]);
635 do {
636 xsdfec_regwrite(xsdfec,
637 base_addr + ((offset + reg) *
638 XSDFEC_REG_WIDTH_JUMP),
639 addr[reg]);
640 reg++;
641 } while ((reg < len) &&
642 ((reg * XSDFEC_REG_WIDTH_JUMP) % PAGE_SIZE));
643 kunmap_local(addr);
644 unpin_user_page(pages[i]);
645 }
646 return 0;
647}
648
649static int xsdfec_add_ldpc(struct xsdfec_dev *xsdfec, void __user *arg)
650{
651 struct xsdfec_ldpc_params *ldpc;
652 int ret, n;
653
654 ldpc = memdup_user(arg, sizeof(*ldpc));
655 if (IS_ERR(ldpc))
656 return PTR_ERR(ldpc);
657
658 if (xsdfec->config.code == XSDFEC_TURBO_CODE) {
659 ret = -EIO;
660 goto err_out;
661 }
662
663 /* Verify Device has not started */
664 if (xsdfec->state == XSDFEC_STARTED) {
665 ret = -EIO;
666 goto err_out;
667 }
668
669 if (xsdfec->config.code_wr_protect) {
670 ret = -EIO;
671 goto err_out;
672 }
673
674 /* Write Reg 0 */
675 ret = xsdfec_reg0_write(xsdfec, ldpc->n, ldpc->k, ldpc->psize,
676 ldpc->code_id);
677 if (ret)
678 goto err_out;
679
680 /* Write Reg 1 */
681 ret = xsdfec_reg1_write(xsdfec, ldpc->psize, ldpc->no_packing, ldpc->nm,
682 ldpc->code_id);
683 if (ret)
684 goto err_out;
685
686 /* Write Reg 2 */
687 ret = xsdfec_reg2_write(xsdfec, ldpc->nlayers, ldpc->nmqc,
688 ldpc->norm_type, ldpc->special_qc,
689 ldpc->no_final_parity, ldpc->max_schedule,
690 ldpc->code_id);
691 if (ret)
692 goto err_out;
693
694 /* Write Reg 3 */
695 ret = xsdfec_reg3_write(xsdfec, ldpc->sc_off, ldpc->la_off,
696 ldpc->qc_off, ldpc->code_id);
697 if (ret)
698 goto err_out;
699
700 /* Write Shared Codes */
701 n = ldpc->nlayers / 4;
702 if (ldpc->nlayers % 4)
703 n++;
704
705 ret = xsdfec_table_write(xsdfec, ldpc->sc_off, ldpc->sc_table, n,
706 XSDFEC_LDPC_SC_TABLE_ADDR_BASE,
707 XSDFEC_SC_TABLE_DEPTH);
708 if (ret < 0)
709 goto err_out;
710
711 ret = xsdfec_table_write(xsdfec, 4 * ldpc->la_off, ldpc->la_table,
712 ldpc->nlayers, XSDFEC_LDPC_LA_TABLE_ADDR_BASE,
713 XSDFEC_LA_TABLE_DEPTH);
714 if (ret < 0)
715 goto err_out;
716
717 ret = xsdfec_table_write(xsdfec, 4 * ldpc->qc_off, ldpc->qc_table,
718 ldpc->nqc, XSDFEC_LDPC_QC_TABLE_ADDR_BASE,
719 XSDFEC_QC_TABLE_DEPTH);
720err_out:
721 kfree(ldpc);
722 return ret;
723}
724
725static int xsdfec_set_order(struct xsdfec_dev *xsdfec, void __user *arg)
726{
727 bool order_invalid;
728 enum xsdfec_order order;
729 int err;
730
731 err = get_user(order, (enum xsdfec_order __user *)arg);
732 if (err)
733 return -EFAULT;
734
735 order_invalid = (order != XSDFEC_MAINTAIN_ORDER) &&
736 (order != XSDFEC_OUT_OF_ORDER);
737 if (order_invalid)
738 return -EINVAL;
739
740 /* Verify Device has not started */
741 if (xsdfec->state == XSDFEC_STARTED)
742 return -EIO;
743
744 xsdfec_regwrite(xsdfec, XSDFEC_ORDER_ADDR, order);
745
746 xsdfec->config.order = order;
747
748 return 0;
749}
750
751static int xsdfec_set_bypass(struct xsdfec_dev *xsdfec, bool __user *arg)
752{
753 bool bypass;
754 int err;
755
756 err = get_user(bypass, arg);
757 if (err)
758 return -EFAULT;
759
760 /* Verify Device has not started */
761 if (xsdfec->state == XSDFEC_STARTED)
762 return -EIO;
763
764 if (bypass)
765 xsdfec_regwrite(xsdfec, XSDFEC_BYPASS_ADDR, 1);
766 else
767 xsdfec_regwrite(xsdfec, XSDFEC_BYPASS_ADDR, 0);
768
769 xsdfec->config.bypass = bypass;
770
771 return 0;
772}
773
774static int xsdfec_is_active(struct xsdfec_dev *xsdfec, bool __user *arg)
775{
776 u32 reg_value;
777 bool is_active;
778 int err;
779
780 reg_value = xsdfec_regread(xsdfec, XSDFEC_ACTIVE_ADDR);
781 /* using a double ! operator instead of casting */
782 is_active = !!(reg_value & XSDFEC_IS_ACTIVITY_SET);
783 err = put_user(is_active, arg);
784 if (err)
785 return -EFAULT;
786
787 return err;
788}
789
790static u32
791xsdfec_translate_axis_width_cfg_val(enum xsdfec_axis_width axis_width_cfg)
792{
793 u32 axis_width_field = 0;
794
795 switch (axis_width_cfg) {
796 case XSDFEC_1x128b:
797 axis_width_field = 0;
798 break;
799 case XSDFEC_2x128b:
800 axis_width_field = 1;
801 break;
802 case XSDFEC_4x128b:
803 axis_width_field = 2;
804 break;
805 }
806
807 return axis_width_field;
808}
809
810static u32 xsdfec_translate_axis_words_cfg_val(enum xsdfec_axis_word_include
811 axis_word_inc_cfg)
812{
813 u32 axis_words_field = 0;
814
815 if (axis_word_inc_cfg == XSDFEC_FIXED_VALUE ||
816 axis_word_inc_cfg == XSDFEC_IN_BLOCK)
817 axis_words_field = 0;
818 else if (axis_word_inc_cfg == XSDFEC_PER_AXI_TRANSACTION)
819 axis_words_field = 1;
820
821 return axis_words_field;
822}
823
824static int xsdfec_cfg_axi_streams(struct xsdfec_dev *xsdfec)
825{
826 u32 reg_value;
827 u32 dout_words_field;
828 u32 dout_width_field;
829 u32 din_words_field;
830 u32 din_width_field;
831 struct xsdfec_config *config = &xsdfec->config;
832
833 /* translate config info to register values */
834 dout_words_field =
835 xsdfec_translate_axis_words_cfg_val(config->dout_word_include);
836 dout_width_field =
837 xsdfec_translate_axis_width_cfg_val(config->dout_width);
838 din_words_field =
839 xsdfec_translate_axis_words_cfg_val(config->din_word_include);
840 din_width_field =
841 xsdfec_translate_axis_width_cfg_val(config->din_width);
842
843 reg_value = dout_words_field << XSDFEC_AXIS_DOUT_WORDS_LSB;
844 reg_value |= dout_width_field << XSDFEC_AXIS_DOUT_WIDTH_LSB;
845 reg_value |= din_words_field << XSDFEC_AXIS_DIN_WORDS_LSB;
846 reg_value |= din_width_field << XSDFEC_AXIS_DIN_WIDTH_LSB;
847
848 xsdfec_regwrite(xsdfec, XSDFEC_AXIS_WIDTH_ADDR, reg_value);
849
850 return 0;
851}
852
853static int xsdfec_start(struct xsdfec_dev *xsdfec)
854{
855 u32 regread;
856
857 regread = xsdfec_regread(xsdfec, XSDFEC_FEC_CODE_ADDR);
858 regread &= 0x1;
859 if (regread != xsdfec->config.code) {
860 dev_dbg(xsdfec->dev,
861 "%s SDFEC HW code does not match driver code, reg %d, code %d",
862 __func__, regread, xsdfec->config.code);
863 return -EINVAL;
864 }
865
866 /* Set AXIS enable */
867 xsdfec_regwrite(xsdfec, XSDFEC_AXIS_ENABLE_ADDR,
868 XSDFEC_AXIS_ENABLE_MASK);
869 /* Done */
870 xsdfec->state = XSDFEC_STARTED;
871 return 0;
872}
873
874static int xsdfec_stop(struct xsdfec_dev *xsdfec)
875{
876 u32 regread;
877
878 if (xsdfec->state != XSDFEC_STARTED)
879 dev_dbg(xsdfec->dev, "Device not started correctly");
880 /* Disable AXIS_ENABLE Input interfaces only */
881 regread = xsdfec_regread(xsdfec, XSDFEC_AXIS_ENABLE_ADDR);
882 regread &= (~XSDFEC_AXIS_IN_ENABLE_MASK);
883 xsdfec_regwrite(xsdfec, XSDFEC_AXIS_ENABLE_ADDR, regread);
884 /* Stop */
885 xsdfec->state = XSDFEC_STOPPED;
886 return 0;
887}
888
889static int xsdfec_clear_stats(struct xsdfec_dev *xsdfec)
890{
891 spin_lock_irqsave(&xsdfec->error_data_lock, xsdfec->flags);
892 xsdfec->isr_err_count = 0;
893 xsdfec->uecc_count = 0;
894 xsdfec->cecc_count = 0;
895 spin_unlock_irqrestore(&xsdfec->error_data_lock, xsdfec->flags);
896
897 return 0;
898}
899
900static int xsdfec_get_stats(struct xsdfec_dev *xsdfec, void __user *arg)
901{
902 int err;
903 struct xsdfec_stats user_stats;
904
905 spin_lock_irqsave(&xsdfec->error_data_lock, xsdfec->flags);
906 user_stats.isr_err_count = xsdfec->isr_err_count;
907 user_stats.cecc_count = xsdfec->cecc_count;
908 user_stats.uecc_count = xsdfec->uecc_count;
909 xsdfec->stats_updated = false;
910 spin_unlock_irqrestore(&xsdfec->error_data_lock, xsdfec->flags);
911
912 err = copy_to_user(arg, &user_stats, sizeof(user_stats));
913 if (err)
914 err = -EFAULT;
915
916 return err;
917}
918
919static int xsdfec_set_default_config(struct xsdfec_dev *xsdfec)
920{
921 /* Ensure registers are aligned with core configuration */
922 xsdfec_regwrite(xsdfec, XSDFEC_FEC_CODE_ADDR, xsdfec->config.code);
923 xsdfec_cfg_axi_streams(xsdfec);
924 update_config_from_hw(xsdfec);
925
926 return 0;
927}
928
929static long xsdfec_dev_ioctl(struct file *fptr, unsigned int cmd,
930 unsigned long data)
931{
932 struct xsdfec_dev *xsdfec;
933 void __user *arg = (void __user *)data;
934 int rval;
935
936 xsdfec = container_of(fptr->private_data, struct xsdfec_dev, miscdev);
937
938 /* In failed state allow only reset and get status IOCTLs */
939 if (xsdfec->state == XSDFEC_NEEDS_RESET &&
940 (cmd != XSDFEC_SET_DEFAULT_CONFIG && cmd != XSDFEC_GET_STATUS &&
941 cmd != XSDFEC_GET_STATS && cmd != XSDFEC_CLEAR_STATS)) {
942 return -EPERM;
943 }
944
945 switch (cmd) {
946 case XSDFEC_START_DEV:
947 rval = xsdfec_start(xsdfec);
948 break;
949 case XSDFEC_STOP_DEV:
950 rval = xsdfec_stop(xsdfec);
951 break;
952 case XSDFEC_CLEAR_STATS:
953 rval = xsdfec_clear_stats(xsdfec);
954 break;
955 case XSDFEC_GET_STATS:
956 rval = xsdfec_get_stats(xsdfec, arg);
957 break;
958 case XSDFEC_GET_STATUS:
959 rval = xsdfec_get_status(xsdfec, arg);
960 break;
961 case XSDFEC_GET_CONFIG:
962 rval = xsdfec_get_config(xsdfec, arg);
963 break;
964 case XSDFEC_SET_DEFAULT_CONFIG:
965 rval = xsdfec_set_default_config(xsdfec);
966 break;
967 case XSDFEC_SET_IRQ:
968 rval = xsdfec_set_irq(xsdfec, arg);
969 break;
970 case XSDFEC_SET_TURBO:
971 rval = xsdfec_set_turbo(xsdfec, arg);
972 break;
973 case XSDFEC_GET_TURBO:
974 rval = xsdfec_get_turbo(xsdfec, arg);
975 break;
976 case XSDFEC_ADD_LDPC_CODE_PARAMS:
977 rval = xsdfec_add_ldpc(xsdfec, arg);
978 break;
979 case XSDFEC_SET_ORDER:
980 rval = xsdfec_set_order(xsdfec, arg);
981 break;
982 case XSDFEC_SET_BYPASS:
983 rval = xsdfec_set_bypass(xsdfec, arg);
984 break;
985 case XSDFEC_IS_ACTIVE:
986 rval = xsdfec_is_active(xsdfec, (bool __user *)arg);
987 break;
988 default:
989 rval = -ENOTTY;
990 break;
991 }
992 return rval;
993}
994
995static __poll_t xsdfec_poll(struct file *file, poll_table *wait)
996{
997 __poll_t mask = 0;
998 struct xsdfec_dev *xsdfec;
999
1000 xsdfec = container_of(file->private_data, struct xsdfec_dev, miscdev);
1001
1002 poll_wait(file, &xsdfec->waitq, wait);
1003
1004 /* XSDFEC ISR detected an error */
1005 spin_lock_irqsave(&xsdfec->error_data_lock, xsdfec->flags);
1006 if (xsdfec->state_updated)
1007 mask |= EPOLLIN | EPOLLPRI;
1008
1009 if (xsdfec->stats_updated)
1010 mask |= EPOLLIN | EPOLLRDNORM;
1011 spin_unlock_irqrestore(&xsdfec->error_data_lock, xsdfec->flags);
1012
1013 return mask;
1014}
1015
1016static const struct file_operations xsdfec_fops = {
1017 .owner = THIS_MODULE,
1018 .unlocked_ioctl = xsdfec_dev_ioctl,
1019 .poll = xsdfec_poll,
1020 .compat_ioctl = compat_ptr_ioctl,
1021};
1022
1023static int xsdfec_parse_of(struct xsdfec_dev *xsdfec)
1024{
1025 struct device *dev = xsdfec->dev;
1026 struct device_node *node = dev->of_node;
1027 int rval;
1028 const char *fec_code;
1029 u32 din_width;
1030 u32 din_word_include;
1031 u32 dout_width;
1032 u32 dout_word_include;
1033
1034 rval = of_property_read_string(node, "xlnx,sdfec-code", &fec_code);
1035 if (rval < 0)
1036 return rval;
1037
1038 if (!strcasecmp(fec_code, "ldpc"))
1039 xsdfec->config.code = XSDFEC_LDPC_CODE;
1040 else if (!strcasecmp(fec_code, "turbo"))
1041 xsdfec->config.code = XSDFEC_TURBO_CODE;
1042 else
1043 return -EINVAL;
1044
1045 rval = of_property_read_u32(node, "xlnx,sdfec-din-words",
1046 &din_word_include);
1047 if (rval < 0)
1048 return rval;
1049
1050 if (din_word_include < XSDFEC_AXIS_WORDS_INCLUDE_MAX)
1051 xsdfec->config.din_word_include = din_word_include;
1052 else
1053 return -EINVAL;
1054
1055 rval = of_property_read_u32(node, "xlnx,sdfec-din-width", &din_width);
1056 if (rval < 0)
1057 return rval;
1058
1059 switch (din_width) {
1060 /* Fall through and set for valid values */
1061 case XSDFEC_1x128b:
1062 case XSDFEC_2x128b:
1063 case XSDFEC_4x128b:
1064 xsdfec->config.din_width = din_width;
1065 break;
1066 default:
1067 return -EINVAL;
1068 }
1069
1070 rval = of_property_read_u32(node, "xlnx,sdfec-dout-words",
1071 &dout_word_include);
1072 if (rval < 0)
1073 return rval;
1074
1075 if (dout_word_include < XSDFEC_AXIS_WORDS_INCLUDE_MAX)
1076 xsdfec->config.dout_word_include = dout_word_include;
1077 else
1078 return -EINVAL;
1079
1080 rval = of_property_read_u32(node, "xlnx,sdfec-dout-width", &dout_width);
1081 if (rval < 0)
1082 return rval;
1083
1084 switch (dout_width) {
1085 /* Fall through and set for valid values */
1086 case XSDFEC_1x128b:
1087 case XSDFEC_2x128b:
1088 case XSDFEC_4x128b:
1089 xsdfec->config.dout_width = dout_width;
1090 break;
1091 default:
1092 return -EINVAL;
1093 }
1094
1095 /* Write LDPC to CODE Register */
1096 xsdfec_regwrite(xsdfec, XSDFEC_FEC_CODE_ADDR, xsdfec->config.code);
1097
1098 xsdfec_cfg_axi_streams(xsdfec);
1099
1100 return 0;
1101}
1102
1103static irqreturn_t xsdfec_irq_thread(int irq, void *dev_id)
1104{
1105 struct xsdfec_dev *xsdfec = dev_id;
1106 irqreturn_t ret = IRQ_HANDLED;
1107 u32 ecc_err;
1108 u32 isr_err;
1109 u32 uecc_count;
1110 u32 cecc_count;
1111 u32 isr_err_count;
1112 u32 aecc_count;
1113 u32 tmp;
1114
1115 WARN_ON(xsdfec->irq != irq);
1116
1117 /* Mask Interrupts */
1118 xsdfec_isr_enable(xsdfec, false);
1119 xsdfec_ecc_isr_enable(xsdfec, false);
1120 /* Read ISR */
1121 ecc_err = xsdfec_regread(xsdfec, XSDFEC_ECC_ISR_ADDR);
1122 isr_err = xsdfec_regread(xsdfec, XSDFEC_ISR_ADDR);
1123 /* Clear the interrupts */
1124 xsdfec_regwrite(xsdfec, XSDFEC_ECC_ISR_ADDR, ecc_err);
1125 xsdfec_regwrite(xsdfec, XSDFEC_ISR_ADDR, isr_err);
1126
1127 tmp = ecc_err & XSDFEC_ALL_ECC_ISR_MBE_MASK;
1128 /* Count uncorrectable 2-bit errors */
1129 uecc_count = hweight32(tmp);
1130 /* Count all ECC errors */
1131 aecc_count = hweight32(ecc_err);
1132 /* Number of correctable 1-bit ECC error */
1133 cecc_count = aecc_count - 2 * uecc_count;
1134 /* Count ISR errors */
1135 isr_err_count = hweight32(isr_err);
1136 dev_dbg(xsdfec->dev, "tmp=%x, uecc=%x, aecc=%x, cecc=%x, isr=%x", tmp,
1137 uecc_count, aecc_count, cecc_count, isr_err_count);
1138 dev_dbg(xsdfec->dev, "uecc=%x, cecc=%x, isr=%x", xsdfec->uecc_count,
1139 xsdfec->cecc_count, xsdfec->isr_err_count);
1140
1141 spin_lock_irqsave(&xsdfec->error_data_lock, xsdfec->flags);
1142 /* Add new errors to a 2-bits counter */
1143 if (uecc_count)
1144 xsdfec->uecc_count += uecc_count;
1145 /* Add new errors to a 1-bits counter */
1146 if (cecc_count)
1147 xsdfec->cecc_count += cecc_count;
1148 /* Add new errors to a ISR counter */
1149 if (isr_err_count)
1150 xsdfec->isr_err_count += isr_err_count;
1151
1152 /* Update state/stats flag */
1153 if (uecc_count) {
1154 if (ecc_err & XSDFEC_ECC_ISR_MBE_MASK)
1155 xsdfec->state = XSDFEC_NEEDS_RESET;
1156 else if (ecc_err & XSDFEC_PL_INIT_ECC_ISR_MBE_MASK)
1157 xsdfec->state = XSDFEC_PL_RECONFIGURE;
1158 xsdfec->stats_updated = true;
1159 xsdfec->state_updated = true;
1160 }
1161
1162 if (cecc_count)
1163 xsdfec->stats_updated = true;
1164
1165 if (isr_err_count) {
1166 xsdfec->state = XSDFEC_NEEDS_RESET;
1167 xsdfec->stats_updated = true;
1168 xsdfec->state_updated = true;
1169 }
1170
1171 spin_unlock_irqrestore(&xsdfec->error_data_lock, xsdfec->flags);
1172 dev_dbg(xsdfec->dev, "state=%x, stats=%x", xsdfec->state_updated,
1173 xsdfec->stats_updated);
1174
1175 /* Enable another polling */
1176 if (xsdfec->state_updated || xsdfec->stats_updated)
1177 wake_up_interruptible(&xsdfec->waitq);
1178 else
1179 ret = IRQ_NONE;
1180
1181 /* Unmask Interrupts */
1182 xsdfec_isr_enable(xsdfec, true);
1183 xsdfec_ecc_isr_enable(xsdfec, true);
1184
1185 return ret;
1186}
1187
1188static int xsdfec_clk_init(struct platform_device *pdev,
1189 struct xsdfec_clks *clks)
1190{
1191 int err;
1192
1193 clks->core_clk = devm_clk_get(&pdev->dev, "core_clk");
1194 if (IS_ERR(clks->core_clk)) {
1195 dev_err(&pdev->dev, "failed to get core_clk");
1196 return PTR_ERR(clks->core_clk);
1197 }
1198
1199 clks->axi_clk = devm_clk_get(&pdev->dev, "s_axi_aclk");
1200 if (IS_ERR(clks->axi_clk)) {
1201 dev_err(&pdev->dev, "failed to get axi_clk");
1202 return PTR_ERR(clks->axi_clk);
1203 }
1204
1205 clks->din_words_clk = devm_clk_get(&pdev->dev, "s_axis_din_words_aclk");
1206 if (IS_ERR(clks->din_words_clk)) {
1207 if (PTR_ERR(clks->din_words_clk) != -ENOENT) {
1208 err = PTR_ERR(clks->din_words_clk);
1209 return err;
1210 }
1211 clks->din_words_clk = NULL;
1212 }
1213
1214 clks->din_clk = devm_clk_get(&pdev->dev, "s_axis_din_aclk");
1215 if (IS_ERR(clks->din_clk)) {
1216 if (PTR_ERR(clks->din_clk) != -ENOENT) {
1217 err = PTR_ERR(clks->din_clk);
1218 return err;
1219 }
1220 clks->din_clk = NULL;
1221 }
1222
1223 clks->dout_clk = devm_clk_get(&pdev->dev, "m_axis_dout_aclk");
1224 if (IS_ERR(clks->dout_clk)) {
1225 if (PTR_ERR(clks->dout_clk) != -ENOENT) {
1226 err = PTR_ERR(clks->dout_clk);
1227 return err;
1228 }
1229 clks->dout_clk = NULL;
1230 }
1231
1232 clks->dout_words_clk =
1233 devm_clk_get(&pdev->dev, "s_axis_dout_words_aclk");
1234 if (IS_ERR(clks->dout_words_clk)) {
1235 if (PTR_ERR(clks->dout_words_clk) != -ENOENT) {
1236 err = PTR_ERR(clks->dout_words_clk);
1237 return err;
1238 }
1239 clks->dout_words_clk = NULL;
1240 }
1241
1242 clks->ctrl_clk = devm_clk_get(&pdev->dev, "s_axis_ctrl_aclk");
1243 if (IS_ERR(clks->ctrl_clk)) {
1244 if (PTR_ERR(clks->ctrl_clk) != -ENOENT) {
1245 err = PTR_ERR(clks->ctrl_clk);
1246 return err;
1247 }
1248 clks->ctrl_clk = NULL;
1249 }
1250
1251 clks->status_clk = devm_clk_get(&pdev->dev, "m_axis_status_aclk");
1252 if (IS_ERR(clks->status_clk)) {
1253 if (PTR_ERR(clks->status_clk) != -ENOENT) {
1254 err = PTR_ERR(clks->status_clk);
1255 return err;
1256 }
1257 clks->status_clk = NULL;
1258 }
1259
1260 err = clk_prepare_enable(clks->core_clk);
1261 if (err) {
1262 dev_err(&pdev->dev, "failed to enable core_clk (%d)", err);
1263 return err;
1264 }
1265
1266 err = clk_prepare_enable(clks->axi_clk);
1267 if (err) {
1268 dev_err(&pdev->dev, "failed to enable axi_clk (%d)", err);
1269 goto err_disable_core_clk;
1270 }
1271
1272 err = clk_prepare_enable(clks->din_clk);
1273 if (err) {
1274 dev_err(&pdev->dev, "failed to enable din_clk (%d)", err);
1275 goto err_disable_axi_clk;
1276 }
1277
1278 err = clk_prepare_enable(clks->din_words_clk);
1279 if (err) {
1280 dev_err(&pdev->dev, "failed to enable din_words_clk (%d)", err);
1281 goto err_disable_din_clk;
1282 }
1283
1284 err = clk_prepare_enable(clks->dout_clk);
1285 if (err) {
1286 dev_err(&pdev->dev, "failed to enable dout_clk (%d)", err);
1287 goto err_disable_din_words_clk;
1288 }
1289
1290 err = clk_prepare_enable(clks->dout_words_clk);
1291 if (err) {
1292 dev_err(&pdev->dev, "failed to enable dout_words_clk (%d)",
1293 err);
1294 goto err_disable_dout_clk;
1295 }
1296
1297 err = clk_prepare_enable(clks->ctrl_clk);
1298 if (err) {
1299 dev_err(&pdev->dev, "failed to enable ctrl_clk (%d)", err);
1300 goto err_disable_dout_words_clk;
1301 }
1302
1303 err = clk_prepare_enable(clks->status_clk);
1304 if (err) {
1305 dev_err(&pdev->dev, "failed to enable status_clk (%d)\n", err);
1306 goto err_disable_ctrl_clk;
1307 }
1308
1309 return err;
1310
1311err_disable_ctrl_clk:
1312 clk_disable_unprepare(clks->ctrl_clk);
1313err_disable_dout_words_clk:
1314 clk_disable_unprepare(clks->dout_words_clk);
1315err_disable_dout_clk:
1316 clk_disable_unprepare(clks->dout_clk);
1317err_disable_din_words_clk:
1318 clk_disable_unprepare(clks->din_words_clk);
1319err_disable_din_clk:
1320 clk_disable_unprepare(clks->din_clk);
1321err_disable_axi_clk:
1322 clk_disable_unprepare(clks->axi_clk);
1323err_disable_core_clk:
1324 clk_disable_unprepare(clks->core_clk);
1325
1326 return err;
1327}
1328
1329static void xsdfec_disable_all_clks(struct xsdfec_clks *clks)
1330{
1331 clk_disable_unprepare(clks->status_clk);
1332 clk_disable_unprepare(clks->ctrl_clk);
1333 clk_disable_unprepare(clks->dout_words_clk);
1334 clk_disable_unprepare(clks->dout_clk);
1335 clk_disable_unprepare(clks->din_words_clk);
1336 clk_disable_unprepare(clks->din_clk);
1337 clk_disable_unprepare(clks->core_clk);
1338 clk_disable_unprepare(clks->axi_clk);
1339}
1340
1341static int xsdfec_probe(struct platform_device *pdev)
1342{
1343 struct xsdfec_dev *xsdfec;
1344 struct device *dev;
1345 int err;
1346 bool irq_enabled = true;
1347
1348 xsdfec = devm_kzalloc(&pdev->dev, sizeof(*xsdfec), GFP_KERNEL);
1349 if (!xsdfec)
1350 return -ENOMEM;
1351
1352 xsdfec->dev = &pdev->dev;
1353 spin_lock_init(&xsdfec->error_data_lock);
1354
1355 err = xsdfec_clk_init(pdev, &xsdfec->clks);
1356 if (err)
1357 return err;
1358
1359 dev = xsdfec->dev;
1360 xsdfec->regs = devm_platform_ioremap_resource(pdev, 0);
1361 if (IS_ERR(xsdfec->regs)) {
1362 err = PTR_ERR(xsdfec->regs);
1363 goto err_xsdfec_dev;
1364 }
1365
1366 xsdfec->irq = platform_get_irq(pdev, 0);
1367 if (xsdfec->irq < 0) {
1368 dev_dbg(dev, "platform_get_irq failed");
1369 irq_enabled = false;
1370 }
1371
1372 err = xsdfec_parse_of(xsdfec);
1373 if (err < 0)
1374 goto err_xsdfec_dev;
1375
1376 update_config_from_hw(xsdfec);
1377
1378 /* Save driver private data */
1379 platform_set_drvdata(pdev, xsdfec);
1380
1381 if (irq_enabled) {
1382 init_waitqueue_head(&xsdfec->waitq);
1383 /* Register IRQ thread */
1384 err = devm_request_threaded_irq(dev, xsdfec->irq, NULL,
1385 xsdfec_irq_thread, IRQF_ONESHOT,
1386 "xilinx-sdfec16", xsdfec);
1387 if (err < 0)
1388 goto err_xsdfec_dev;
1389 }
1390
1391 err = ida_alloc(&dev_nrs, GFP_KERNEL);
1392 if (err < 0)
1393 goto err_xsdfec_dev;
1394 xsdfec->dev_id = err;
1395
1396 snprintf(xsdfec->dev_name, DEV_NAME_LEN, "xsdfec%d", xsdfec->dev_id);
1397 xsdfec->miscdev.minor = MISC_DYNAMIC_MINOR;
1398 xsdfec->miscdev.name = xsdfec->dev_name;
1399 xsdfec->miscdev.fops = &xsdfec_fops;
1400 xsdfec->miscdev.parent = dev;
1401 err = misc_register(&xsdfec->miscdev);
1402 if (err) {
1403 dev_err(dev, "error:%d. Unable to register device", err);
1404 goto err_xsdfec_ida;
1405 }
1406 return 0;
1407
1408err_xsdfec_ida:
1409 ida_free(&dev_nrs, xsdfec->dev_id);
1410err_xsdfec_dev:
1411 xsdfec_disable_all_clks(&xsdfec->clks);
1412 return err;
1413}
1414
1415static void xsdfec_remove(struct platform_device *pdev)
1416{
1417 struct xsdfec_dev *xsdfec;
1418
1419 xsdfec = platform_get_drvdata(pdev);
1420 misc_deregister(&xsdfec->miscdev);
1421 ida_free(&dev_nrs, xsdfec->dev_id);
1422 xsdfec_disable_all_clks(&xsdfec->clks);
1423}
1424
1425static const struct of_device_id xsdfec_of_match[] = {
1426 {
1427 .compatible = "xlnx,sd-fec-1.1",
1428 },
1429 { /* end of table */ }
1430};
1431MODULE_DEVICE_TABLE(of, xsdfec_of_match);
1432
1433static struct platform_driver xsdfec_driver = {
1434 .driver = {
1435 .name = "xilinx-sdfec",
1436 .of_match_table = xsdfec_of_match,
1437 },
1438 .probe = xsdfec_probe,
1439 .remove = xsdfec_remove,
1440};
1441
1442module_platform_driver(xsdfec_driver);
1443
1444MODULE_AUTHOR("Xilinx, Inc");
1445MODULE_DESCRIPTION("Xilinx SD-FEC16 Driver");
1446MODULE_LICENSE("GPL");