Debian Codegraph @

drivers/dahdi/wcxb.c

raw symbols SCIP
1 /*
2 * wcxb SPI library
3 *
4 * Copyright (C) 2013 Digium, Inc.
5 *
6 * All rights reserved.
7 *
8 */
9
10 /*
11 * See http://www.asterisk.org for more information about
12 * the Asterisk project. Please do not directly contact
13 * any of the maintainers of this project for assistance;
14 * the project provides a web site, mailing lists and IRC
15 * channels for your use.
16 *
17 * This program is free software, distributed under the terms of
18 * the GNU General Public License Version 2 as published by the
19 * Free Software Foundation. See the LICENSE file included with
20 * this program for more details.
21 */
22
23 #include <linux/kernel.h>
24 #include <linux/spinlock.h>
25 #include <linux/errno.h>
26 #include <linux/pci.h>
27 #include <linux/interrupt.h>
28 #include <linux/firmware.h>
29 #include <linux/crc32.h>
30 #include <linux/delay.h>
31 #include <linux/version.h>
32 #include <linux/slab.h>
33
34 #define HAVE_RATELIMIT
35 #include <linux/ratelimit.h>
36
37 #include <dahdi/kernel.h>
38
39
40 #include "wcxb.h"
41 #include "wcxb_spi.h"
42 #include "wcxb_flash.h"
43
44 /* The definition for Surprise Down was added in Linux 3.6 in (a0dee2e PCI: misc
45 * pci_reg additions). It may be backported though so we won't check for the
46 * version. Same with PCI_EXP_SLTCTL_PDCE. */
47 #ifndef PCI_ERR_UNC_SURPDN
48 #define PCI_ERR_UNC_SURPDN 0x20
49 #endif
50 #ifndef PCI_EXP_SLTCTL_PDCE
51 #define PCI_EXP_SLTCTL_PDCE 0x8
52 #endif
53
54 /* FPGA Status definitions */
55 #define OCT_CPU_RESET (1 << 0)
56 #define OCT_CPU_DRAM_CKE (1 << 1)
57 #define STATUS_LED_GREEN (1 << 9)
58 #define STATUS_LED_RED (1 << 10)
59 #define FALC_CPU_RESET (1 << 11)
60
61 /* Descriptor ring definitions */
62 #define DRING_SIZE (1 << 7) /* Must be in multiples of 2 */
63 #define DRING_SIZE_MASK (DRING_SIZE-1)
64 #define DESC_EOR (1 << 0)
65 #define DESC_INT (1 << 1)
66 #define DESC_OWN (1 << 31)
67 #define DESC_DEFAULT_STATUS 0xdeadbe00
68 #define DMA_CHAN_SIZE 128
69
70 /* Echocan definitions */
71 #define OCT_OFFSET (xb->membase + 0x10000)
72 #define OCT_CONTROL_REG (OCT_OFFSET + 0)
73 #define OCT_DATA_REG (OCT_OFFSET + 0x4)
74 #define OCT_ADDRESS_HIGH_REG (OCT_OFFSET + 0x8)
75 #define OCT_ADDRESS_LOW_REG (OCT_OFFSET + 0xa)
76 #define OCT_DIRECT_WRITE_MASK 0x3001
77 #define OCT_INDIRECT_READ_MASK 0x0101
78 #define OCT_INDIRECT_WRITE_MASK 0x3101
79
80
81 /* DMA definitions */
82 #define TDM_DRING_ADDR 0x2000
83 #define TDM_CONTROL (TDM_DRING_ADDR + 0x4)
84 #define ENABLE_ECHOCAN_TDM (1 << 0)
85 #define TDM_RECOVER_CLOCK (1 << 1)
86 #define ENABLE_DMA (1 << 2)
87 #define DMA_RUNNING (1 << 3)
88 #define DMA_LOOPBACK (1 << 4)
89 #define AUTHENTICATED (1 << 5)
90 #define TDM_VERSION (TDM_DRING_ADDR + 0x24)
91
92 /* Interrupt definitions */
93 #define INTERRUPT_CONTROL 0x300
94 #define ISR (INTERRUPT_CONTROL + 0x0)
95 #define IPR (INTERRUPT_CONTROL + 0x4)
96 #define IER (INTERRUPT_CONTROL + 0x8)
97 #define IAR (INTERRUPT_CONTROL + 0xc)
98 #define SIE (INTERRUPT_CONTROL + 0x10)
99 #define CIE (INTERRUPT_CONTROL + 0x14)
100 #define IVR (INTERRUPT_CONTROL + 0x18)
101 #define MER (INTERRUPT_CONTROL + 0x1c)
102 #define MER_ME (1<<0)
103 #define MER_HIE (1<<1)
104 #define DESC_UNDERRUN (1<<0)
105 #define DESC_COMPLETE (1<<1)
106 #define OCT_INT (1<<2)
107 #define FALC_INT (1<<3)
108 #define SPI_INT (1<<4)
109
110 #define FLASH_SPI_BASE 0x200
111
112 struct wcxb_hw_desc {
113 volatile __be32 status;
114 __be32 tx_buf;
115 __be32 rx_buf;
116 volatile __be32 control;
117 } __packed;
118
119 struct wcxb_meta_desc {
120 void *tx_buf_virt;
121 void *rx_buf_virt;
122 };
123
124 static inline bool wcxb_is_pcie(const struct wcxb *xb)
125 {
126 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 33)
127 return pci_is_pcie(xb->pdev);
128 #else
129 return (xb->pdev->is_pcie > 0);
130 #endif
131 }
132
133 static const unsigned int CLK_SRC_MASK = ((1 << 13) | (1 << 12) | (1 << 1));
134
135 enum wcxb_clock_sources wcxb_get_clksrc(struct wcxb *xb)
136 {
137 static const u32 SELF = 0x0;
138 static const u32 RECOVER = (1 << 1);
139 static const u32 SLAVE = (1 << 12) | (1 << 1);
140 unsigned long flags;
141 u32 reg;
142
143 spin_lock_irqsave(&xb->lock, flags);
144 reg = ioread32be(xb->membase + TDM_CONTROL) & CLK_SRC_MASK;
145 spin_unlock_irqrestore(&xb->lock, flags);
146
147 if (SELF == reg)
148 return WCXB_CLOCK_SELF;
149 else if (RECOVER == reg)
150 return WCXB_CLOCK_RECOVER;
151 else if (SLAVE == reg)
152 return WCXB_CLOCK_SLAVE;
153 else
154 WARN_ON(1);
155 return WCXB_CLOCK_SELF;
156 }
157
158 void wcxb_set_clksrc(struct wcxb *xb, enum wcxb_clock_sources clksrc)
159 {
160 unsigned long flags;
161 u32 clkbits = 0;
162
163 switch (clksrc) {
164 case WCXB_CLOCK_RECOVER:
165 if (xb->flags.drive_timing_cable)
166 clkbits = (1<<13) | (1 << 1);
167 else
168 clkbits = (1 << 1);
169 break;
170 case WCXB_CLOCK_SELF:
171 if (xb->flags.drive_timing_cable)
172 clkbits = (1<<13);
173 else
174 clkbits = 0;
175 break;
176 case WCXB_CLOCK_SLAVE:
177 /* When we're slave, do not ever drive the timing cable. */
178 clkbits = (1<<12) | (1 << 1);
179 break;
180 };
181
182 /* set new clock select */
183 spin_lock_irqsave(&xb->lock, flags);
184 if (!wcxb_is_stopped(xb)) {
185 dev_err(&xb->pdev->dev, "ERROR: Cannot set clock source while DMA engine is running.\n");
186 } else {
187 u32 reg;
188 reg = ioread32be(xb->membase + TDM_CONTROL);
189 reg &= ~CLK_SRC_MASK;
190 reg |= (clkbits & CLK_SRC_MASK);
191 iowrite32be(reg, xb->membase + TDM_CONTROL);
192 }
193 spin_unlock_irqrestore(&xb->lock, flags);
194 }
195
196 void wcxb_enable_echocan(struct wcxb *xb)
197 {
198 u32 reg;
199 unsigned long flags;
200 spin_lock_irqsave(&xb->lock, flags);
201 reg = ioread32be(xb->membase + TDM_CONTROL);
202 reg |= ENABLE_ECHOCAN_TDM;
203 iowrite32be(reg, xb->membase + TDM_CONTROL);
204 spin_unlock_irqrestore(&xb->lock, flags);
205 }
206
207 void wcxb_disable_echocan(struct wcxb *xb)
208 {
209 u32 reg;
210 unsigned long flags;
211 spin_lock_irqsave(&xb->lock, flags);
212 reg = ioread32be(xb->membase + TDM_CONTROL);
213 reg &= ~ENABLE_ECHOCAN_TDM;
214 iowrite32be(reg, xb->membase + TDM_CONTROL);
215 spin_unlock_irqrestore(&xb->lock, flags);
216 }
217
218 void wcxb_reset_echocan(struct wcxb *xb)
219 {
220 unsigned long flags;
221 int reg;
222 spin_lock_irqsave(&xb->lock, flags);
223 reg = ioread32be(xb->membase);
224 iowrite32be((reg & ~OCT_CPU_RESET), xb->membase);
225 spin_unlock_irqrestore(&xb->lock, flags);
226
227 msleep_interruptible(1);
228
229 spin_lock_irqsave(&xb->lock, flags);
230 reg = ioread32be(xb->membase);
231 iowrite32be((reg | OCT_CPU_RESET), xb->membase);
232 spin_unlock_irqrestore(&xb->lock, flags);
233
234 dev_dbg(&xb->pdev->dev, "Reset octasic\n");
235 }
236
237 bool wcxb_is_echocan_present(struct wcxb *xb)
238 {
239 return 0x1 == ioread16be(OCT_CONTROL_REG);
240 }
241
242 void wcxb_enable_echocan_dram(struct wcxb *xb)
243 {
244 unsigned long flags;
245 int reg;
246 spin_lock_irqsave(&xb->lock, flags);
247 reg = ioread32be(xb->membase);
248 iowrite32be((reg | OCT_CPU_DRAM_CKE), xb->membase);
249 spin_unlock_irqrestore(&xb->lock, flags);
250 }
251
252 u16 wcxb_get_echocan_reg(struct wcxb *xb, u32 address)
253 {
254 uint16_t highaddress = ((address >> 20) & 0xfff);
255 uint16_t lowaddress = ((address >> 4) & 0xfffff);
256 unsigned long stop = jiffies + HZ/10;
257 unsigned long flags;
258 u16 ret;
259
260 spin_lock_irqsave(&xb->lock, flags);
261 iowrite16be(highaddress, OCT_ADDRESS_HIGH_REG);
262 iowrite16be(lowaddress, OCT_ADDRESS_LOW_REG);
263
264 iowrite16be(OCT_INDIRECT_READ_MASK | ((address & 0xe) << 8),
265 OCT_CONTROL_REG);
266 do {
267 ret = ioread16be(OCT_CONTROL_REG);
268 } while ((ret & (1<<8)) && time_before(jiffies, stop));
269
270 WARN_ON_ONCE(time_after_eq(jiffies, stop));
271
272 ret = ioread16be(OCT_DATA_REG);
273 spin_unlock_irqrestore(&xb->lock, flags);
274
275 return ret;
276 }
277
278 void wcxb_set_echocan_reg(struct wcxb *xb, u32 address, u16 val)
279 {
280 unsigned long flags;
281 uint16_t ret;
282 uint16_t highaddress = ((address >> 20) & 0xfff);
283 uint16_t lowaddress = ((address >> 4) & 0xffff);
284 unsigned long stop = jiffies + HZ/10;
285
286 spin_lock_irqsave(&xb->lock, flags);
287 iowrite16be(highaddress, OCT_ADDRESS_HIGH_REG);
288 iowrite16be(lowaddress, OCT_ADDRESS_LOW_REG);
289
290 iowrite16be(val, OCT_DATA_REG);
291 iowrite16be(OCT_INDIRECT_WRITE_MASK | ((address & 0xe) << 8),
292 OCT_CONTROL_REG);
293
294 /* No write should take longer than 100ms */
295 do {
296 ret = ioread16be(OCT_CONTROL_REG);
297 } while ((ret & (1<<8)) && time_before(jiffies, stop));
298 spin_unlock_irqrestore(&xb->lock, flags);
299
300 WARN_ON_ONCE(time_after_eq(jiffies, stop));
301 }
302
303 #ifdef HAVE_RATELIMIT
304 static DEFINE_RATELIMIT_STATE(_underrun_rl, DEFAULT_RATELIMIT_INTERVAL,
305 DEFAULT_RATELIMIT_BURST);
306 #endif
307
308 /* wcxb_reset_dring needs to be called with xb->lock held. */
309 static void _wcxb_reset_dring(struct wcxb *xb)
310 {
311 int x;
312 struct wcxb_meta_desc *mdesc;
313 struct wcxb_hw_desc *hdesc = NULL;
314
315 xb->dma_head = xb->dma_tail = 0;
316
317 if (unlikely(xb->latency > DRING_SIZE)) {
318 #ifdef HAVE_RATELIMIT
319 if (__ratelimit(&_underrun_rl)) {
320 #else
321 if (printk_ratelimit()) {
322 #endif
323 dev_info(&xb->pdev->dev,
324 "Oops! Tried to increase latency past buffer size.\n");
325 }
326 xb->latency = DRING_SIZE;
327 }
328
329 for (x = 0; x < xb->latency; x++) {
330 dma_addr_t dma_tmp;
331
332 mdesc = &xb->meta_dring[x];
333 hdesc = &xb->hw_dring[x];
334
335 hdesc->status = cpu_to_be32(DESC_DEFAULT_STATUS);
336 if (!mdesc->tx_buf_virt) {
337 mdesc->tx_buf_virt =
338 dma_pool_alloc(xb->pool, GFP_ATOMIC, &dma_tmp);
339 hdesc->tx_buf = cpu_to_be32(dma_tmp);
340 mdesc->rx_buf_virt =
341 dma_pool_alloc(xb->pool, GFP_ATOMIC, &dma_tmp);
342 hdesc->rx_buf = cpu_to_be32(dma_tmp);
343 }
344 hdesc->control = cpu_to_be32(DESC_INT|DESC_OWN);
345 BUG_ON(!mdesc->tx_buf_virt || !mdesc->rx_buf_virt);
346 }
347
348 BUG_ON(!hdesc);
349 /* Set end of ring bit in last descriptor to force hw to loop around */
350 hdesc->control |= cpu_to_be32(DESC_EOR);
351 #ifdef DEBUG
352 xb->last_retry_count = 0;
353 xb->max_retry_count = 0;
354 xb->last_dma_time = 0;
355 xb->max_dma_time = 0;
356 #endif
357 iowrite32be(xb->hw_dring_phys, xb->membase + TDM_DRING_ADDR);
358 }
359
360 static void wcxb_handle_dma(struct wcxb *xb)
361 {
362 struct wcxb_meta_desc *mdesc;
363 struct wcxb_hw_desc *tail = &(xb->hw_dring[xb->dma_tail]);
364
365 while (!(tail->control & cpu_to_be32(DESC_OWN))) {
366 u_char *frame;
367
368 #ifdef DEBUG
369 xb->last_retry_count =
370 ((be32_to_cpu(tail->control) & 0x0000ff00) >> 8);
371 xb->last_dma_time = (be32_to_cpu(tail->status));
372 #endif
373
374 mdesc = &xb->meta_dring[xb->dma_tail];
375 frame = mdesc->rx_buf_virt;
376
377 xb->ops->handle_receive(xb, frame);
378
379 xb->dma_tail =
380 (xb->dma_tail == xb->latency-1) ? 0 : xb->dma_tail + 1;
381 tail = &(xb->hw_dring[xb->dma_tail]);
382
383 mdesc = &xb->meta_dring[xb->dma_head];
384 frame = mdesc->tx_buf_virt;
385
386 xb->ops->handle_transmit(xb, frame);
387
388 wmb();
389 xb->hw_dring[xb->dma_head].control |= cpu_to_be32(DESC_OWN);
390 xb->dma_head =
391 (xb->dma_head == xb->latency-1) ? 0 : xb->dma_head + 1;
392 }
393
394 #ifdef DEBUG
395 if (xb->last_retry_count > xb->max_retry_count) {
396 xb->max_retry_count = xb->last_retry_count;
397 dev_info(&xb->pdev->dev,
398 "New DMA max retries detected: %d\n",
399 xb->max_retry_count);
400 }
401 if (xb->last_dma_time > xb->max_dma_time) {
402 xb->max_dma_time = xb->last_dma_time;
403 dev_info(&xb->pdev->dev,
404 "New DMA max transfer time detected: %d\n",
405 xb->max_dma_time);
406 }
407 #endif
408 }
409
410 static irqreturn_t _wcxb_isr(int irq, void *dev_id)
411 {
412 struct wcxb *xb = dev_id;
413 unsigned int limit = 8;
414 u32 pending;
415
416 pending = ioread32be(xb->membase + ISR);
417 if (!pending)
418 return IRQ_NONE;
419
420 do {
421 iowrite32be(pending, xb->membase + IAR);
422
423 if (pending & DESC_UNDERRUN) {
424 u32 reg;
425
426 /* Report the error in case drivers have any custom
427 * methods for indicating potential data corruption. An
428 * underrun means data loss in the TDM channel. */
429 if (xb->ops->handle_error)
430 xb->ops->handle_error(xb);
431
432 spin_lock(&xb->lock);
433
434 if (!xb->flags.latency_locked) {
435 /* bump latency */
436
437 xb->latency = min(xb->latency + 1,
438 xb->max_latency);
439 #ifdef HAVE_RATELIMIT
440 if (__ratelimit(&_underrun_rl)) {
441 #else
442 if (printk_ratelimit()) {
443 #endif
444 if (xb->latency != xb->max_latency) {
445 dev_info(&xb->pdev->dev,
446 "Underrun detected by hardware. Latency bumped to: %dms\n",
447 xb->latency);
448 } else {
449 dev_info(&xb->pdev->dev,
450 "Underrun detected by hardware. Latency at max of %dms.\n",
451 xb->latency);
452 }
453 }
454 }
455
456 /* re-setup dma ring */
457 _wcxb_reset_dring(xb);
458
459 /* set dma enable bit */
460 reg = ioread32be(xb->membase + TDM_CONTROL);
461 reg |= ENABLE_DMA;
462 iowrite32be(reg, xb->membase + TDM_CONTROL);
463
464 spin_unlock(&xb->lock);
465 }
466
467 if (pending & DESC_COMPLETE) {
468 xb->framecount++;
469 wcxb_handle_dma(xb);
470 }
471
472 if (NULL != xb->ops->handle_interrupt)
473 xb->ops->handle_interrupt(xb, pending);
474
475 pending = ioread32be(xb->membase + ISR);
476 } while (pending && --limit);
477 return IRQ_HANDLED;
478 }
479
480 static irqreturn_t wcxb_isr(int irq, void *dev_id)
481 {
482 irqreturn_t ret;
483 unsigned long flags;
484 local_irq_save(flags);
485 ret = _wcxb_isr(irq, dev_id);
486 local_irq_restore(flags);
487 return ret;
488 }
489
490 static int wcxb_alloc_dring(struct wcxb *xb, const char *board_name)
491 {
492 xb->meta_dring =
493 kzalloc(sizeof(struct wcxb_meta_desc) * DRING_SIZE,
494 GFP_KERNEL);
495 if (!xb->meta_dring)
496 return -ENOMEM;
497
498 xb->hw_dring = dma_alloc_coherent(&xb->pdev->dev,
499 sizeof(struct wcxb_hw_desc) * DRING_SIZE,
500 &xb->hw_dring_phys,
501 GFP_KERNEL);
502 if (!xb->hw_dring) {
503 kfree(xb->meta_dring);
504 return -ENOMEM;
505 }
506
507 xb->pool = dma_pool_create(board_name, &xb->pdev->dev,
508 PAGE_SIZE, PAGE_SIZE, 0);
509 if (!xb->pool) {
510 kfree(xb->meta_dring);
511 dma_free_coherent(&xb->pdev->dev,
512 sizeof(struct wcxb_hw_desc) * DRING_SIZE,
513 xb->hw_dring,
514 xb->hw_dring_phys);
515 return -ENOMEM;
516 }
517 return 0;
518 }
519
520 /**
521 * wcxb_soft_reset - Set interface registers back to known good values.
522 *
523 * This represents the normal default state after a reset of the FPGA. This
524 * function is preferred over the hard reset function.
525 *
526 */
527 static void wcxb_soft_reset(struct wcxb *xb)
528 {
529 /* digium_gpo */
530 iowrite32be(0x0, xb->membase);
531
532 /* xps_intc */
533 iowrite32be(0x0, xb->membase + 0x300);
534 iowrite32be(0x0, xb->membase + 0x308);
535 iowrite32be(0x0, xb->membase + 0x310);
536 iowrite32be(0x0, xb->membase + 0x31C);
537
538 /* xps_spi_config_flash */
539 iowrite32be(0xA, xb->membase + 0x200);
540
541 /* tdm engine */
542 iowrite32be(0x0, xb->membase + 0x2000);
543 iowrite32be(0x0, xb->membase + 0x2004);
544 }
545
546 static void _wcxb_hard_reset(struct wcxb *xb)
547 {
548 struct pci_dev *const pdev = xb->pdev;
549 u32 microblaze_version;
550 unsigned long stop_time = jiffies + msecs_to_jiffies(2000);
551
552 pci_save_state(pdev);
553 iowrite32be(0xe00, xb->membase + TDM_CONTROL);
554
555 /* This sleep is to give FPGA time to bring up the PCI/PCIe interface */
556 msleep(200);
557
558 pci_restore_state(pdev);
559
560 /* Wait for the Microblaze CPU to complete it's startup */
561 do {
562 msleep(20);
563 /* Can return either 0xffff or 0 before it's fully booted */
564 microblaze_version = ioread32be(xb->membase + 0x2018) ?: 0xffff;
565 } while (time_before(jiffies, stop_time)
566 && 0xffff == microblaze_version);
567 }
568
569 /*
570 * Since the FPGA hard reset drops the PCIe link we need to disable
571 * error reporting on the upsteam link. Otherwise Surprise Down errors
572 * may be reported in reponse to the link going away.
573 *
574 * NOTE: We cannot use pci_disable_pcie_error_reporting() because it will not
575 * disable error reporting if the system firmware is attached to the advanced
576 * error reporting mechanism.
577 */
578 static void _wcxb_pcie_hard_reset(struct wcxb *xb)
579 {
580 struct pci_dev *const parent = xb->pdev->bus->self;
581 u32 aer_mask;
582 u16 sltctl;
583 int pos_err;
584 int pos_exp;
585
586 if (!wcxb_is_pcie(xb))
587 return;
588
589 pos_err = pci_find_ext_capability(parent, PCI_EXT_CAP_ID_ERR);
590 if (pos_err) {
591 pci_read_config_dword(parent, pos_err + PCI_ERR_UNCOR_MASK,
592 &aer_mask);
593 pci_write_config_dword(parent, pos_err + PCI_ERR_UNCOR_MASK,
594 aer_mask | PCI_ERR_UNC_SURPDN);
595 }
596
597 /* Also disable any presence change reporting. */
598 pos_exp = pci_find_capability(parent, PCI_CAP_ID_EXP);
599 if (pos_exp) {
600 pci_read_config_word(parent, pos_exp + PCI_EXP_SLTCTL,
601 &sltctl);
602 pci_write_config_word(parent, pos_exp + PCI_EXP_SLTCTL,
603 sltctl & ~PCI_EXP_SLTCTL_PDCE);
604 }
605
606 _wcxb_hard_reset(xb);
607
608 if (pos_exp)
609 pci_write_config_word(parent, pos_exp + PCI_EXP_SLTCTL, sltctl);
610
611 if (pos_err) {
612 pci_write_config_dword(parent, pos_err + PCI_ERR_UNCOR_MASK,
613 aer_mask);
614
615 /* Clear the error as well from the status register. */
616 pci_write_config_dword(parent, pos_err + PCI_ERR_UNCOR_STATUS,
617 PCI_ERR_UNC_SURPDN);
618 }
619
620 return;
621 }
622
623 /**
624 * wcxb_hard_reset - Reset FPGA and reload firmware.
625 *
626 * This may be called in the context of device probe and therefore the PCI
627 * device may be locked.
628 *
629 */
630 static void wcxb_hard_reset(struct wcxb *xb)
631 {
632 if (wcxb_is_pcie(xb))
633 _wcxb_pcie_hard_reset(xb);
634 else
635 _wcxb_hard_reset(xb);
636 }
637
638 int wcxb_init(struct wcxb *xb, const char *board_name, u32 int_mode)
639 {
640 int res = 0;
641 struct pci_dev *pdev = xb->pdev;
642 u32 tdm_control;
643
644 if (pci_enable_device(pdev))
645 return -EIO;
646
647 pci_set_master(pdev);
648
649 WARN_ON(!pdev);
650 if (!pdev)
651 return -EINVAL;
652
653 xb->latency = WCXB_DEFAULT_LATENCY;
654 xb->max_latency = WCXB_DEFAULT_MAXLATENCY;
655
656 spin_lock_init(&xb->lock);
657
658 xb->membase = pci_iomap(pdev, 0, 0);
659 if (pci_request_regions(pdev, board_name))
660 dev_info(&xb->pdev->dev, "Unable to request regions\n");
661
662 wcxb_soft_reset(xb);
663
664 res = wcxb_alloc_dring(xb, board_name);
665 if (res) {
666 dev_err(&xb->pdev->dev,
667 "Failed to allocate descriptor rings.\n");
668 goto fail_exit;
669 }
670
671 /* Enable writes to fpga status register */
672 iowrite32be(0, xb->membase + 0x04);
673
674 xb->flags.have_msi = (int_mode) ? 0 : (0 == pci_enable_msi(pdev));
675
676 if (request_irq(pdev->irq, wcxb_isr,
677 (xb->flags.have_msi) ? 0 : IRQF_SHARED,
678 board_name, xb)) {
679 dev_notice(&xb->pdev->dev, "Unable to request IRQ %d\n",
680 pdev->irq);
681 res = -EIO;
682 goto fail_exit;
683 }
684
685 iowrite32be(0, xb->membase + TDM_CONTROL);
686 tdm_control = ioread32be(xb->membase + TDM_CONTROL);
687 if (!(tdm_control & 0x20)) {
688 dev_err(&xb->pdev->dev,
689 "This board is not authenticated and may not function properly.\n");
690 msleep(1000);
691 } else {
692 dev_dbg(&xb->pdev->dev, "Authenticated. %08x\n", tdm_control);
693 }
694
695 return res;
696 fail_exit:
697 pci_release_regions(xb->pdev);
698 return res;
699 }
700
701 void wcxb_stop_dma(struct wcxb *xb)
702 {
703 unsigned long flags;
704 u32 reg;
705
706 /* Quiesce DMA engine interrupts */
707 spin_lock_irqsave(&xb->lock, flags);
708 reg = ioread32be(xb->membase + TDM_CONTROL);
709 reg &= ~ENABLE_DMA;
710 iowrite32be(reg, xb->membase + TDM_CONTROL);
711 spin_unlock_irqrestore(&xb->lock, flags);
712 }
713
714 int wcxb_wait_for_stop(struct wcxb *xb, unsigned long timeout_ms)
715 {
716 unsigned long stop;
717 stop = jiffies + msecs_to_jiffies(timeout_ms);
718 do {
719 if (time_after(jiffies, stop))
720 return -EIO;
721 else
722 cpu_relax();
723 } while (!wcxb_is_stopped(xb));
724
725 return 0;
726 }
727
728 void wcxb_disable_interrupts(struct wcxb *xb)
729 {
730 iowrite32be(0, xb->membase + IER);
731 }
732
733 void wcxb_stop(struct wcxb *xb)
734 {
735 unsigned long flags;
736 spin_lock_irqsave(&xb->lock, flags);
737 /* Stop everything */
738 iowrite32be(0, xb->membase + TDM_CONTROL);
739 iowrite32be(0, xb->membase + IER);
740 iowrite32be(0, xb->membase + MER);
741 iowrite32be(-1, xb->membase + IAR);
742 /* Flush quiesce commands before exit */
743 ioread32be(xb->membase);
744 spin_unlock_irqrestore(&xb->lock, flags);
745 synchronize_irq(xb->pdev->irq);
746 }
747
748 bool wcxb_is_stopped(struct wcxb *xb)
749 {
750 return !(ioread32be(xb->membase + TDM_CONTROL) & DMA_RUNNING);
751 }
752
753 static void wcxb_free_dring(struct wcxb *xb)
754 {
755 struct wcxb_meta_desc *mdesc;
756 struct wcxb_hw_desc *hdesc;
757 int i;
758
759 /* Free tx/rx buffs */
760 for (i = 0; i < DRING_SIZE; i++) {
761 mdesc = &xb->meta_dring[i];
762 hdesc = &xb->hw_dring[i];
763 if (mdesc->tx_buf_virt) {
764 dma_pool_free(xb->pool,
765 mdesc->tx_buf_virt,
766 be32_to_cpu(hdesc->tx_buf));
767 dma_pool_free(xb->pool,
768 mdesc->rx_buf_virt,
769 be32_to_cpu(hdesc->rx_buf));
770 }
771 }
772
773 dma_pool_destroy(xb->pool);
774 dma_free_coherent(&xb->pdev->dev,
775 sizeof(struct wcxb_hw_desc) * DRING_SIZE,
776 xb->hw_dring,
777 xb->hw_dring_phys);
778 kfree(xb->meta_dring);
779 }
780
781 void wcxb_release(struct wcxb *xb)
782 {
783 wcxb_stop(xb);
784 synchronize_irq(xb->pdev->irq);
785 free_irq(xb->pdev->irq, xb);
786 if (xb->flags.have_msi)
787 pci_disable_msi(xb->pdev);
788 if (xb->membase)
789 pci_iounmap(xb->pdev, xb->membase);
790 wcxb_free_dring(xb);
791 pci_release_regions(xb->pdev);
792 pci_disable_device(xb->pdev);
793 return;
794 }
795
796 int wcxb_start(struct wcxb *xb)
797 {
798 u32 reg;
799 unsigned long flags;
800
801 spin_lock_irqsave(&xb->lock, flags);
802 _wcxb_reset_dring(xb);
803 /* Enable hardware interrupts */
804 iowrite32be(-1, xb->membase + IAR);
805 iowrite32be(DESC_UNDERRUN|DESC_COMPLETE, xb->membase + IER);
806 /* iowrite32be(0x3f7, xb->membase + IER); */
807 iowrite32be(MER_ME|MER_HIE, xb->membase + MER);
808
809 /* Start the DMA engine processing. */
810 reg = ioread32be(xb->membase + TDM_CONTROL);
811 reg |= ENABLE_DMA;
812 iowrite32be(reg, xb->membase + TDM_CONTROL);
813
814 spin_unlock_irqrestore(&xb->lock, flags);
815
816 return 0;
817 }
818
819 struct wcxb_meta_block {
820 __le32 chksum;
821 __le32 version;
822 __le32 size;
823 } __packed;
824
825 struct wcxb_firm_header {
826 u8 header[6];
827 __le32 chksum;
828 u8 pad[18];
829 __le32 version;
830 } __packed;
831
832 u32 wcxb_get_firmware_version(struct wcxb *xb)
833 {
834 u32 version = 0;
835
836 /* Two version registers are read and catenated into one */
837 /* Firmware version goes in bits upper byte */
838 version = ((ioread32be(xb->membase + 0x400) & 0xffff)<<16);
839
840 /* Microblaze version goes in lower word */
841 version += ioread32be(xb->membase + 0x2018);
842
843 return version;
844 }
845
846 static int wcxb_update_firmware(struct wcxb *xb, const struct firmware *fw,
847 const char *filename,
848 enum wcxb_reset_option reset)
849 {
850 u32 tdm_control;
851 static const int APPLICATION_ADDRESS = 0x200000;
852 static const int META_BLOCK_OFFSET = 0x170000;
853 static const int ERASE_BLOCK_SIZE = 0x010000;
854 static const int END_OFFSET = APPLICATION_ADDRESS + META_BLOCK_OFFSET +
855 ERASE_BLOCK_SIZE;
856 struct wcxb_spi_master *flash_spi_master;
857 struct wcxb_spi_device *flash_spi_device;
858 struct wcxb_meta_block meta;
859 int offset;
860 struct wcxb_firm_header *head = (struct wcxb_firm_header *)(fw->data);
861
862 if (fw->size > (META_BLOCK_OFFSET + sizeof(*head))) {
863 dev_err(&xb->pdev->dev,
864 "Firmware is too large to fit in available space.\n");
865
866 return -EINVAL;
867 }
868
869 meta.size = cpu_to_le32(fw->size);
870 meta.version = head->version;
871 meta.chksum = head->chksum;
872
873 flash_spi_master = wcxb_spi_master_create(&xb->pdev->dev,
874 xb->membase + FLASH_SPI_BASE,
875 false);
876
877 flash_spi_device = wcxb_spi_device_create(flash_spi_master, 0);
878
879 dev_info(&xb->pdev->dev,
880 "Uploading %s. This can take up to 30 seconds.\n", filename);
881
882
883 /* First erase all the blocks in the application area. */
884 offset = APPLICATION_ADDRESS;
885 while (offset < END_OFFSET) {
886 wcxb_flash_sector_erase(flash_spi_device, offset);
887 offset += ERASE_BLOCK_SIZE;
888 }
889
890 /* Then write the new firmware file. */
891 wcxb_flash_write(flash_spi_device, APPLICATION_ADDRESS,
892 &fw->data[sizeof(struct wcxb_firm_header)],
893 fw->size - sizeof(struct wcxb_firm_header));
894
895 /* Finally, update the meta block. */
896 wcxb_flash_write(flash_spi_device,
897 APPLICATION_ADDRESS + META_BLOCK_OFFSET,
898 &meta, sizeof(meta));
899
900 if (WCXB_RESET_NOW == reset) {
901 /* Reset fpga after loading firmware */
902 dev_info(&xb->pdev->dev,
903 "Firmware load complete. Reseting device.\n");
904 tdm_control = ioread32be(xb->membase + TDM_CONTROL);
905
906 wcxb_hard_reset(xb);
907
908 iowrite32be(0, xb->membase + 0x04);
909 iowrite32be(tdm_control, xb->membase + TDM_CONTROL);
910 } else {
911 dev_info(&xb->pdev->dev,
912 "Delaying reset. Firmware load requires a power cycle\n");
913 }
914
915 wcxb_spi_device_destroy(flash_spi_device);
916 wcxb_spi_master_destroy(flash_spi_master);
917 return 0;
918 }
919
920 int wcxb_check_firmware(struct wcxb *xb, const u32 expected_version,
921 const char *firmware_filename, bool force_firmware,
922 enum wcxb_reset_option reset)
923 {
924 const struct firmware *fw;
925 const struct wcxb_firm_header *header;
926 static const int APPLICATION_ADDRESS = 0x200000;
927 static const int META_BLOCK_OFFSET = 0x170000;
928 struct wcxb_spi_master *flash_spi_master;
929 struct wcxb_spi_device *flash_spi_device;
930 struct wcxb_meta_block meta;
931 int res = 0;
932 u32 crc;
933 u32 version = 0;
934
935 version = wcxb_get_firmware_version(xb);
936
937 if (0xff000000 == (version & 0xff000000)) {
938 dev_info(&xb->pdev->dev,
939 "Invalid firmware %x. Please check your hardware.\n",
940 version);
941 return -EIO;
942 }
943
944 if ((expected_version == version) && !force_firmware) {
945 dev_info(&xb->pdev->dev, "Firmware version: %x\n", version);
946 return 0;
947 }
948
949 /* Check meta firmware version for a not-booted application image */
950 flash_spi_master = wcxb_spi_master_create(&xb->pdev->dev,
951 xb->membase + FLASH_SPI_BASE,
952 false);
953 flash_spi_device = wcxb_spi_device_create(flash_spi_master, 0);
954 res = wcxb_flash_read(flash_spi_device,
955 APPLICATION_ADDRESS + META_BLOCK_OFFSET,
956 &meta, sizeof(meta));
957 if (res) {
958 dev_info(&xb->pdev->dev, "Unable to read flash\n");
959 return -EIO;
960 }
961
962 if ((meta.version == cpu_to_le32(expected_version))
963 && !force_firmware) {
964 dev_info(&xb->pdev->dev,
965 "Detected previous firmware updated to current version %x, but %x is currently running on card. You likely need to power cycle your system.\n",
966 expected_version, version);
967 return 0;
968 }
969
970 if (force_firmware) {
971 dev_info(&xb->pdev->dev,
972 "force_firmware module parameter is set. Forcing firmware load, regardless of version\n");
973 } else {
974 dev_info(&xb->pdev->dev,
975 "Firmware version %x is running, but we require version %x.\n",
976 version, expected_version);
977 }
978
979 res = request_firmware(&fw, firmware_filename, &xb->pdev->dev);
980 if (res) {
981 dev_info(&xb->pdev->dev,
982 "Firmware '%s' not available from userspace.\n",
983 firmware_filename);
984 goto cleanup;
985 }
986
987 header = (const struct wcxb_firm_header *)fw->data;
988
989 /* Check the crc */
990 crc = crc32(~0, &fw->data[10], fw->size - 10) ^ ~0;
991 if (memcmp("DIGIUM", header->header, sizeof(header->header)) ||
992 (le32_to_cpu(header->chksum) != crc)) {
993 dev_info(&xb->pdev->dev,
994 "%s is invalid. Please reinstall.\n",
995 firmware_filename);
996 goto cleanup;
997 }
998
999 /* Check the file vs required firmware versions */
1000 if (le32_to_cpu(header->version) != expected_version) {
1001 dev_err(&xb->pdev->dev,
1002 "Existing firmware file %s is version %x, but we require %x. Please install the correct firmware file.\n",
1003 firmware_filename, le32_to_cpu(header->version),
1004 expected_version);
1005 res = -EIO;
1006 goto cleanup;
1007 }
1008
1009 dev_info(&xb->pdev->dev, "Found %s (version: %x) Preparing for flash\n",
1010 firmware_filename, header->version);
1011
1012 res = wcxb_update_firmware(xb, fw, firmware_filename, reset);
1013
1014 version = wcxb_get_firmware_version(xb);
1015 if (WCXB_RESET_NOW == reset) {
1016 dev_info(&xb->pdev->dev,
1017 "Reset into firmware version: %x\n", version);
1018 } else {
1019 dev_info(&xb->pdev->dev,
1020 "Running firmware version: %x\n", version);
1021 dev_info(&xb->pdev->dev,
1022 "Loaded firmware version: %x (Will load after next power cycle)\n",
1023 header->version);
1024 }
1025
1026 if ((WCXB_RESET_NOW == reset) && (expected_version != version)
1027 && !force_firmware) {
1028 /* On the off chance that the interface is in a state where it
1029 * cannot boot into the updated firmware image, power cycling
1030 * the card can recover. A simple "reset" of the computer is not
1031 * sufficient, power has to be removed completely. */
1032 dev_err(&xb->pdev->dev,
1033 "The wrong firmware is running after update. Please power cycle and try again.\n");
1034 res = -EIO;
1035 goto cleanup;
1036 }
1037
1038 if (res) {
1039 dev_info(&xb->pdev->dev,
1040 "Failed to load firmware %s\n", firmware_filename);
1041 }
1042
1043 cleanup:
1044 release_firmware(fw);
1045 return res;
1046 }