1 /**********************************************************************
4 * Contact: support@cavium.com
5 * Please include "LiquidIO" in the subject.
7 * Copyright (c) 2003-2015 Cavium, Inc.
9 * This file is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License, Version 2, as
11 * published by the Free Software Foundation.
13 * This file is distributed in the hope that it will be useful, but
14 * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16 * NONINFRINGEMENT. See the GNU General Public License for more
19 * This file may also be available under a different license from Cavium.
20 * Contact Cavium, Inc. for more information
21 **********************************************************************/
22 #include <linux/pci.h>
23 #include <linux/netdevice.h>
24 #include "liquidio_common.h"
25 #include "octeon_droq.h"
26 #include "octeon_iq.h"
27 #include "response_manager.h"
28 #include "octeon_device.h"
30 #define MEMOPS_IDX MAX_BAR1_MAP_INDEX
32 #ifdef __BIG_ENDIAN_BITFIELD
34 octeon_toggle_bar1_swapmode(struct octeon_device
*oct
, u32 idx
)
38 mask
= oct
->fn_list
.bar1_idx_read(oct
, idx
);
39 mask
= (mask
& 0x2) ? (mask
& ~2) : (mask
| 2);
40 oct
->fn_list
.bar1_idx_write(oct
, idx
, mask
);
43 #define octeon_toggle_bar1_swapmode(oct, idx) (oct = oct)
47 octeon_pci_fastwrite(struct octeon_device
*oct
, u8 __iomem
*mapped_addr
,
50 while ((len
) && ((unsigned long)mapped_addr
) & 7) {
51 writeb(*(hostbuf
++), mapped_addr
++);
55 octeon_toggle_bar1_swapmode(oct
, MEMOPS_IDX
);
58 writeq(*((u64
*)hostbuf
), mapped_addr
);
64 octeon_toggle_bar1_swapmode(oct
, MEMOPS_IDX
);
67 writeb(*(hostbuf
++), mapped_addr
++);
71 octeon_pci_fastread(struct octeon_device
*oct
, u8 __iomem
*mapped_addr
,
74 while ((len
) && ((unsigned long)mapped_addr
) & 7) {
75 *(hostbuf
++) = readb(mapped_addr
++);
79 octeon_toggle_bar1_swapmode(oct
, MEMOPS_IDX
);
82 *((u64
*)hostbuf
) = readq(mapped_addr
);
88 octeon_toggle_bar1_swapmode(oct
, MEMOPS_IDX
);
91 *(hostbuf
++) = readb(mapped_addr
++);
94 /* Core mem read/write with temporary bar1 settings. */
95 /* op = 1 to read, op = 0 to write. */
97 __octeon_pci_rw_core_mem(struct octeon_device
*oct
, u64 addr
,
98 u8
*hostbuf
, u32 len
, u32 op
)
100 u32 copy_len
= 0, index_reg_val
= 0;
102 u8 __iomem
*mapped_addr
;
104 spin_lock_irqsave(&oct
->mem_access_lock
, flags
);
106 /* Save the original index reg value. */
107 index_reg_val
= oct
->fn_list
.bar1_idx_read(oct
, MEMOPS_IDX
);
109 oct
->fn_list
.bar1_idx_setup(oct
, addr
, MEMOPS_IDX
, 1);
110 mapped_addr
= oct
->mmio
[1].hw_addr
111 + (MEMOPS_IDX
<< 22) + (addr
& 0x3fffff);
113 /* If operation crosses a 4MB boundary, split the transfer
117 if (((addr
+ len
- 1) & ~(0x3fffff)) != (addr
& ~(0x3fffff))) {
118 copy_len
= (u32
)(((addr
& ~(0x3fffff)) +
119 (MEMOPS_IDX
<< 22)) - addr
);
124 if (op
) { /* read from core */
125 octeon_pci_fastread(oct
, mapped_addr
, hostbuf
,
128 octeon_pci_fastwrite(oct
, mapped_addr
, hostbuf
,
138 oct
->fn_list
.bar1_idx_write(oct
, MEMOPS_IDX
, index_reg_val
);
140 spin_unlock_irqrestore(&oct
->mem_access_lock
, flags
);
144 octeon_pci_read_core_mem(struct octeon_device
*oct
,
149 __octeon_pci_rw_core_mem(oct
, coreaddr
, buf
, len
, 1);
153 octeon_pci_write_core_mem(struct octeon_device
*oct
,
158 __octeon_pci_rw_core_mem(oct
, coreaddr
, buf
, len
, 0);
161 u64
octeon_read_device_mem64(struct octeon_device
*oct
, u64 coreaddr
)
165 __octeon_pci_rw_core_mem(oct
, coreaddr
, (u8
*)&ret
, 8, 1);
167 return be64_to_cpu(ret
);
170 u32
octeon_read_device_mem32(struct octeon_device
*oct
, u64 coreaddr
)
174 __octeon_pci_rw_core_mem(oct
, coreaddr
, (u8
*)&ret
, 4, 1);
176 return be32_to_cpu(ret
);
179 void octeon_write_device_mem32(struct octeon_device
*oct
, u64 coreaddr
,
182 __be32 t
= cpu_to_be32(val
);
184 __octeon_pci_rw_core_mem(oct
, coreaddr
, (u8
*)&t
, 4, 0);