2 * Copyright (C) 2006-2007 PA Semi, Inc
4 * Common functions for DMA access on PA Semi PWRficient
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/kernel.h>
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/pci.h>
26 #include <asm/pasemi_dma.h>
33 static struct pasdma_status
*dma_status
;
35 static void __iomem
*iob_regs
;
36 static void __iomem
*mac_regs
[6];
37 static void __iomem
*dma_regs
;
39 static int base_hw_irq
;
41 static int num_txch
, num_rxch
;
43 static struct pci_dev
*dma_pdev
;
45 /* Bitmaps to handle allocation of channels */
47 static DECLARE_BITMAP(txch_free
, MAX_TXCH
);
48 static DECLARE_BITMAP(rxch_free
, MAX_RXCH
);
49 static DECLARE_BITMAP(flags_free
, MAX_FLAGS
);
50 static DECLARE_BITMAP(fun_free
, MAX_FUN
);
52 /* pasemi_read_iob_reg - read IOB register
53 * @reg: Register to read (offset into PCI CFG space)
55 unsigned int pasemi_read_iob_reg(unsigned int reg
)
57 return in_le32(iob_regs
+reg
);
59 EXPORT_SYMBOL(pasemi_read_iob_reg
);
61 /* pasemi_write_iob_reg - write IOB register
62 * @reg: Register to write to (offset into PCI CFG space)
63 * @val: Value to write
65 void pasemi_write_iob_reg(unsigned int reg
, unsigned int val
)
67 out_le32(iob_regs
+reg
, val
);
69 EXPORT_SYMBOL(pasemi_write_iob_reg
);
71 /* pasemi_read_mac_reg - read MAC register
72 * @intf: MAC interface
73 * @reg: Register to read (offset into PCI CFG space)
75 unsigned int pasemi_read_mac_reg(int intf
, unsigned int reg
)
77 return in_le32(mac_regs
[intf
]+reg
);
79 EXPORT_SYMBOL(pasemi_read_mac_reg
);
81 /* pasemi_write_mac_reg - write MAC register
82 * @intf: MAC interface
83 * @reg: Register to write to (offset into PCI CFG space)
84 * @val: Value to write
86 void pasemi_write_mac_reg(int intf
, unsigned int reg
, unsigned int val
)
88 out_le32(mac_regs
[intf
]+reg
, val
);
90 EXPORT_SYMBOL(pasemi_write_mac_reg
);
92 /* pasemi_read_dma_reg - read DMA register
93 * @reg: Register to read (offset into PCI CFG space)
95 unsigned int pasemi_read_dma_reg(unsigned int reg
)
97 return in_le32(dma_regs
+reg
);
99 EXPORT_SYMBOL(pasemi_read_dma_reg
);
101 /* pasemi_write_dma_reg - write DMA register
102 * @reg: Register to write to (offset into PCI CFG space)
103 * @val: Value to write
105 void pasemi_write_dma_reg(unsigned int reg
, unsigned int val
)
107 out_le32(dma_regs
+reg
, val
);
109 EXPORT_SYMBOL(pasemi_write_dma_reg
);
111 static int pasemi_alloc_tx_chan(enum pasemi_dmachan_type type
)
116 switch (type
& (TXCHAN_EVT0
|TXCHAN_EVT1
)) {
131 bit
= find_next_bit(txch_free
, MAX_TXCH
, start
);
134 if (!test_and_clear_bit(bit
, txch_free
))
140 static void pasemi_free_tx_chan(int chan
)
142 BUG_ON(test_bit(chan
, txch_free
));
143 set_bit(chan
, txch_free
);
146 static int pasemi_alloc_rx_chan(void)
150 bit
= find_first_bit(rxch_free
, MAX_RXCH
);
153 if (!test_and_clear_bit(bit
, rxch_free
))
159 static void pasemi_free_rx_chan(int chan
)
161 BUG_ON(test_bit(chan
, rxch_free
));
162 set_bit(chan
, rxch_free
);
165 /* pasemi_dma_alloc_chan - Allocate a DMA channel
166 * @type: Type of channel to allocate
167 * @total_size: Total size of structure to allocate (to allow for more
168 * room behind the structure to be used by the client)
169 * @offset: Offset in bytes from start of the total structure to the beginning
170 * of struct pasemi_dmachan. Needed when struct pasemi_dmachan is
171 * not the first member of the client structure.
173 * pasemi_dma_alloc_chan allocates a DMA channel for use by a client. The
174 * type argument specifies whether it's a RX or TX channel, and in the case
175 * of TX channels which group it needs to belong to (if any).
177 * Returns a pointer to the total structure allocated on success, NULL
180 void *pasemi_dma_alloc_chan(enum pasemi_dmachan_type type
,
181 int total_size
, int offset
)
184 struct pasemi_dmachan
*chan
;
187 BUG_ON(total_size
< sizeof(struct pasemi_dmachan
));
189 buf
= kzalloc(total_size
, GFP_KERNEL
);
197 switch (type
& (TXCHAN
|RXCHAN
)) {
199 chno
= pasemi_alloc_rx_chan();
201 chan
->irq
= irq_create_mapping(NULL
,
202 base_hw_irq
+ num_txch
+ chno
);
203 chan
->status
= &dma_status
->rx_sta
[chno
];
206 chno
= pasemi_alloc_tx_chan(type
);
208 chan
->irq
= irq_create_mapping(NULL
, base_hw_irq
+ chno
);
209 chan
->status
= &dma_status
->tx_sta
[chno
];
213 chan
->chan_type
= type
;
217 EXPORT_SYMBOL(pasemi_dma_alloc_chan
);
219 /* pasemi_dma_free_chan - Free a previously allocated channel
220 * @chan: Channel to free
222 * Frees a previously allocated channel. It will also deallocate any
223 * descriptor ring associated with the channel, if allocated.
225 void pasemi_dma_free_chan(struct pasemi_dmachan
*chan
)
228 pasemi_dma_free_ring(chan
);
230 switch (chan
->chan_type
& (RXCHAN
|TXCHAN
)) {
232 pasemi_free_rx_chan(chan
->chno
);
235 pasemi_free_tx_chan(chan
->chno
);
241 EXPORT_SYMBOL(pasemi_dma_free_chan
);
243 /* pasemi_dma_alloc_ring - Allocate descriptor ring for a channel
244 * @chan: Channel for which to allocate
245 * @ring_size: Ring size in 64-bit (8-byte) words
247 * Allocate a descriptor ring for a channel. Returns 0 on success, errno
248 * on failure. The passed in struct pasemi_dmachan is updated with the
249 * virtual and DMA addresses of the ring.
251 int pasemi_dma_alloc_ring(struct pasemi_dmachan
*chan
, int ring_size
)
253 BUG_ON(chan
->ring_virt
);
255 chan
->ring_size
= ring_size
;
257 chan
->ring_virt
= dma_alloc_coherent(&dma_pdev
->dev
,
258 ring_size
* sizeof(u64
),
259 &chan
->ring_dma
, GFP_KERNEL
);
261 if (!chan
->ring_virt
)
264 memset(chan
->ring_virt
, 0, ring_size
* sizeof(u64
));
268 EXPORT_SYMBOL(pasemi_dma_alloc_ring
);
270 /* pasemi_dma_free_ring - Free an allocated descriptor ring for a channel
271 * @chan: Channel for which to free the descriptor ring
273 * Frees a previously allocated descriptor ring for a channel.
275 void pasemi_dma_free_ring(struct pasemi_dmachan
*chan
)
277 BUG_ON(!chan
->ring_virt
);
279 dma_free_coherent(&dma_pdev
->dev
, chan
->ring_size
* sizeof(u64
),
280 chan
->ring_virt
, chan
->ring_dma
);
281 chan
->ring_virt
= NULL
;
285 EXPORT_SYMBOL(pasemi_dma_free_ring
);
287 /* pasemi_dma_start_chan - Start a DMA channel
288 * @chan: Channel to start
289 * @cmdsta: Additional CCMDSTA/TCMDSTA bits to write
291 * Enables (starts) a DMA channel with optional additional arguments.
293 void pasemi_dma_start_chan(const struct pasemi_dmachan
*chan
, const u32 cmdsta
)
295 if (chan
->chan_type
== RXCHAN
)
296 pasemi_write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(chan
->chno
),
297 cmdsta
| PAS_DMA_RXCHAN_CCMDSTA_EN
);
299 pasemi_write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(chan
->chno
),
300 cmdsta
| PAS_DMA_TXCHAN_TCMDSTA_EN
);
302 EXPORT_SYMBOL(pasemi_dma_start_chan
);
304 /* pasemi_dma_stop_chan - Stop a DMA channel
305 * @chan: Channel to stop
307 * Stops (disables) a DMA channel. This is done by setting the ST bit in the
308 * CMDSTA register and waiting on the ACT (active) bit to clear, then
309 * finally disabling the whole channel.
311 * This function will only try for a short while for the channel to stop, if
312 * it doesn't it will return failure.
314 * Returns 1 on success, 0 on failure.
316 #define MAX_RETRIES 5000
317 int pasemi_dma_stop_chan(const struct pasemi_dmachan
*chan
)
322 if (chan
->chan_type
== RXCHAN
) {
323 reg
= PAS_DMA_RXCHAN_CCMDSTA(chan
->chno
);
324 pasemi_write_dma_reg(reg
, PAS_DMA_RXCHAN_CCMDSTA_ST
);
325 for (retries
= 0; retries
< MAX_RETRIES
; retries
++) {
326 sta
= pasemi_read_dma_reg(reg
);
327 if (!(sta
& PAS_DMA_RXCHAN_CCMDSTA_ACT
)) {
328 pasemi_write_dma_reg(reg
, 0);
334 reg
= PAS_DMA_TXCHAN_TCMDSTA(chan
->chno
);
335 pasemi_write_dma_reg(reg
, PAS_DMA_TXCHAN_TCMDSTA_ST
);
336 for (retries
= 0; retries
< MAX_RETRIES
; retries
++) {
337 sta
= pasemi_read_dma_reg(reg
);
338 if (!(sta
& PAS_DMA_TXCHAN_TCMDSTA_ACT
)) {
339 pasemi_write_dma_reg(reg
, 0);
348 EXPORT_SYMBOL(pasemi_dma_stop_chan
);
350 /* pasemi_dma_alloc_buf - Allocate a buffer to use for DMA
351 * @chan: Channel to allocate for
352 * @size: Size of buffer in bytes
353 * @handle: DMA handle
355 * Allocate a buffer to be used by the DMA engine for read/write,
356 * similar to dma_alloc_coherent().
358 * Returns the virtual address of the buffer, or NULL in case of failure.
360 void *pasemi_dma_alloc_buf(struct pasemi_dmachan
*chan
, int size
,
363 return dma_alloc_coherent(&dma_pdev
->dev
, size
, handle
, GFP_KERNEL
);
365 EXPORT_SYMBOL(pasemi_dma_alloc_buf
);
367 /* pasemi_dma_free_buf - Free a buffer used for DMA
368 * @chan: Channel the buffer was allocated for
369 * @size: Size of buffer in bytes
370 * @handle: DMA handle
372 * Frees a previously allocated buffer.
374 void pasemi_dma_free_buf(struct pasemi_dmachan
*chan
, int size
,
377 dma_free_coherent(&dma_pdev
->dev
, size
, handle
, GFP_KERNEL
);
379 EXPORT_SYMBOL(pasemi_dma_free_buf
);
381 /* pasemi_dma_alloc_flag - Allocate a flag (event) for channel syncronization
383 * Allocates a flag for use with channel syncronization (event descriptors).
384 * Returns allocated flag (0-63), < 0 on error.
386 int pasemi_dma_alloc_flag(void)
391 bit
= find_next_bit(flags_free
, MAX_FLAGS
, 0);
392 if (bit
>= MAX_FLAGS
)
394 if (!test_and_clear_bit(bit
, flags_free
))
399 EXPORT_SYMBOL(pasemi_dma_alloc_flag
);
402 /* pasemi_dma_free_flag - Deallocates a flag (event)
403 * @flag: Flag number to deallocate
405 * Frees up a flag so it can be reused for other purposes.
407 void pasemi_dma_free_flag(int flag
)
409 BUG_ON(test_bit(flag
, flags_free
));
410 BUG_ON(flag
>= MAX_FLAGS
);
411 set_bit(flag
, flags_free
);
413 EXPORT_SYMBOL(pasemi_dma_free_flag
);
416 /* pasemi_dma_set_flag - Sets a flag (event) to 1
417 * @flag: Flag number to set active
419 * Sets the flag provided to 1.
421 void pasemi_dma_set_flag(int flag
)
423 BUG_ON(flag
>= MAX_FLAGS
);
425 pasemi_write_dma_reg(PAS_DMA_TXF_SFLG0
, 1 << flag
);
427 pasemi_write_dma_reg(PAS_DMA_TXF_SFLG1
, 1 << flag
);
429 EXPORT_SYMBOL(pasemi_dma_set_flag
);
431 /* pasemi_dma_clear_flag - Sets a flag (event) to 0
432 * @flag: Flag number to set inactive
434 * Sets the flag provided to 0.
436 void pasemi_dma_clear_flag(int flag
)
438 BUG_ON(flag
>= MAX_FLAGS
);
440 pasemi_write_dma_reg(PAS_DMA_TXF_CFLG0
, 1 << flag
);
442 pasemi_write_dma_reg(PAS_DMA_TXF_CFLG1
, 1 << flag
);
444 EXPORT_SYMBOL(pasemi_dma_clear_flag
);
446 /* pasemi_dma_alloc_fun - Allocate a function engine
448 * Allocates a function engine to use for crypto/checksum offload
449 * Returns allocated engine (0-8), < 0 on error.
451 int pasemi_dma_alloc_fun(void)
456 bit
= find_next_bit(fun_free
, MAX_FLAGS
, 0);
457 if (bit
>= MAX_FLAGS
)
459 if (!test_and_clear_bit(bit
, fun_free
))
464 EXPORT_SYMBOL(pasemi_dma_alloc_fun
);
467 /* pasemi_dma_free_fun - Deallocates a function engine
468 * @flag: Engine number to deallocate
470 * Frees up a function engine so it can be used for other purposes.
472 void pasemi_dma_free_fun(int fun
)
474 BUG_ON(test_bit(fun
, fun_free
));
475 BUG_ON(fun
>= MAX_FLAGS
);
476 set_bit(fun
, fun_free
);
478 EXPORT_SYMBOL(pasemi_dma_free_fun
);
481 static void *map_onedev(struct pci_dev
*p
, int index
)
483 struct device_node
*dn
;
486 dn
= pci_device_to_OF_node(p
);
490 ret
= of_iomap(dn
, index
);
496 /* This is hardcoded and ugly, but we have some firmware versions
497 * that don't provide the register space in the device tree. Luckily
498 * they are at well-known locations so we can just do the math here.
500 return ioremap(0xe0000000 + (p
->devfn
<< 12), 0x2000);
503 /* pasemi_dma_init - Initialize the PA Semi DMA library
505 * This function initializes the DMA library. It must be called before
506 * any other function in the library.
508 * Returns 0 on success, errno on failure.
510 int pasemi_dma_init(void)
512 static DEFINE_SPINLOCK(init_lock
);
513 struct pci_dev
*iob_pdev
;
514 struct pci_dev
*pdev
;
516 struct device_node
*dn
;
517 int i
, intf
, err
= 0;
518 unsigned long timeout
;
521 if (!machine_is(pasemi
))
524 spin_lock(&init_lock
);
526 /* Make sure we haven't already initialized */
530 iob_pdev
= pci_get_device(PCI_VENDOR_ID_PASEMI
, 0xa001, NULL
);
533 printk(KERN_WARNING
"Can't find I/O Bridge\n");
537 iob_regs
= map_onedev(iob_pdev
, 0);
539 dma_pdev
= pci_get_device(PCI_VENDOR_ID_PASEMI
, 0xa007, NULL
);
542 printk(KERN_WARNING
"Can't find DMA controller\n");
546 dma_regs
= map_onedev(dma_pdev
, 0);
547 base_hw_irq
= virq_to_hw(dma_pdev
->irq
);
549 pci_read_config_dword(dma_pdev
, PAS_DMA_CAP_TXCH
, &tmp
);
550 num_txch
= (tmp
& PAS_DMA_CAP_TXCH_TCHN_M
) >> PAS_DMA_CAP_TXCH_TCHN_S
;
552 pci_read_config_dword(dma_pdev
, PAS_DMA_CAP_RXCH
, &tmp
);
553 num_rxch
= (tmp
& PAS_DMA_CAP_RXCH_RCHN_M
) >> PAS_DMA_CAP_RXCH_RCHN_S
;
556 for (pdev
= pci_get_device(PCI_VENDOR_ID_PASEMI
, 0xa006, NULL
);
558 pdev
= pci_get_device(PCI_VENDOR_ID_PASEMI
, 0xa006, pdev
))
559 mac_regs
[intf
++] = map_onedev(pdev
, 0);
563 for (pdev
= pci_get_device(PCI_VENDOR_ID_PASEMI
, 0xa005, NULL
);
565 pdev
= pci_get_device(PCI_VENDOR_ID_PASEMI
, 0xa005, pdev
))
566 mac_regs
[intf
++] = map_onedev(pdev
, 0);
570 dn
= pci_device_to_OF_node(iob_pdev
);
572 err
= of_address_to_resource(dn
, 1, &res
);
574 /* Fallback for old firmware */
575 res
.start
= 0xfd800000;
576 res
.end
= res
.start
+ 0x1000;
578 dma_status
= __ioremap(res
.start
, res
.end
-res
.start
, 0);
579 pci_dev_put(iob_pdev
);
581 for (i
= 0; i
< MAX_TXCH
; i
++)
582 __set_bit(i
, txch_free
);
584 for (i
= 0; i
< MAX_RXCH
; i
++)
585 __set_bit(i
, rxch_free
);
587 timeout
= jiffies
+ HZ
;
588 pasemi_write_dma_reg(PAS_DMA_COM_RXCMD
, 0);
589 while (pasemi_read_dma_reg(PAS_DMA_COM_RXSTA
) & 1) {
590 if (time_after(jiffies
, timeout
)) {
591 pr_warning("Warning: Could not disable RX section\n");
596 timeout
= jiffies
+ HZ
;
597 pasemi_write_dma_reg(PAS_DMA_COM_TXCMD
, 0);
598 while (pasemi_read_dma_reg(PAS_DMA_COM_TXSTA
) & 1) {
599 if (time_after(jiffies
, timeout
)) {
600 pr_warning("Warning: Could not disable TX section\n");
605 /* setup resource allocations for the different DMA sections */
606 tmp
= pasemi_read_dma_reg(PAS_DMA_COM_CFG
);
607 pasemi_write_dma_reg(PAS_DMA_COM_CFG
, tmp
| 0x18000000);
609 /* enable tx section */
610 pasemi_write_dma_reg(PAS_DMA_COM_TXCMD
, PAS_DMA_COM_TXCMD_EN
);
612 /* enable rx section */
613 pasemi_write_dma_reg(PAS_DMA_COM_RXCMD
, PAS_DMA_COM_RXCMD_EN
);
615 for (i
= 0; i
< MAX_FLAGS
; i
++)
616 __set_bit(i
, flags_free
);
618 for (i
= 0; i
< MAX_FUN
; i
++)
619 __set_bit(i
, fun_free
);
621 /* clear all status flags */
622 pasemi_write_dma_reg(PAS_DMA_TXF_CFLG0
, 0xffffffff);
623 pasemi_write_dma_reg(PAS_DMA_TXF_CFLG1
, 0xffffffff);
625 printk(KERN_INFO
"PA Semi PWRficient DMA library initialized "
626 "(%d tx, %d rx channels)\n", num_txch
, num_rxch
);
629 spin_unlock(&init_lock
);
632 EXPORT_SYMBOL(pasemi_dma_init
);