Debian Codegraph @

drivers/dahdi/wcxb.c

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