blob: 19e8b95b7af60963478807910b4f5d6e9a362d0e [file] [log] [blame]
Ben Hutchings12d00ca2009-10-23 08:30:46 +00001/****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
Ben Hutchings0a6f40c2011-02-25 00:01:34 +00004 * Copyright 2006-2010 Solarflare Communications Inc.
Ben Hutchings12d00ca2009-10-23 08:30:46 +00005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation, incorporated herein by reference.
9 */
10
11#ifndef EFX_IO_H
12#define EFX_IO_H
13
14#include <linux/io.h>
15#include <linux/spinlock.h>
16
17/**************************************************************************
18 *
19 * NIC register I/O
20 *
21 **************************************************************************
22 *
23 * Notes on locking strategy:
24 *
Ben Hutchings778cdaf2012-09-18 01:56:50 +010025 * Many CSRs are very wide and cannot be read or written atomically.
26 * Writes from the host are buffered by the Bus Interface Unit (BIU)
27 * up to 128 bits. Whenever the host writes part of such a register,
28 * the BIU collects the written value and does not write to the
29 * underlying register until all 4 dwords have been written. A
30 * similar buffering scheme applies to host access to the NIC's 64-bit
31 * SRAM.
Ben Hutchings12d00ca2009-10-23 08:30:46 +000032 *
Ben Hutchings778cdaf2012-09-18 01:56:50 +010033 * Writes to different CSRs and 64-bit SRAM words must be serialised,
34 * since interleaved access can result in lost writes. We use
35 * efx_nic::biu_lock for this.
36 *
37 * We also serialise reads from 128-bit CSRs and SRAM with the same
38 * spinlock. This may not be necessary, but it doesn't really matter
39 * as there are no such reads on the fast path.
Ben Hutchings12d00ca2009-10-23 08:30:46 +000040 *
Ben Hutchings9f2f6cd2010-12-06 22:55:00 +000041 * The DMA descriptor pointers (RX_DESC_UPD and TX_DESC_UPD) are
42 * 128-bit but are special-cased in the BIU to avoid the need for
43 * locking in the host:
Ben Hutchings12d00ca2009-10-23 08:30:46 +000044 *
Ben Hutchings9f2f6cd2010-12-06 22:55:00 +000045 * - They are write-only.
46 * - The semantics of writing to these registers are such that
47 * replacing the low 96 bits with zero does not affect functionality.
48 * - If the host writes to the last dword address of such a register
49 * (i.e. the high 32 bits) the underlying register will always be
Ben Hutchings483f97f2011-09-01 12:09:59 +000050 * written. If the collector and the current write together do not
51 * provide values for all 128 bits of the register, the low 96 bits
52 * will be written as zero.
Ben Hutchings9f2f6cd2010-12-06 22:55:00 +000053 * - If the host writes to the address of any other part of such a
54 * register while the collector already holds values for some other
55 * register, the write is discarded and the collector maintains its
56 * current state.
Ben Hutchings12d00ca2009-10-23 08:30:46 +000057 */
58
59#if BITS_PER_LONG == 64
60#define EFX_USE_QWORD_IO 1
61#endif
62
63#ifdef EFX_USE_QWORD_IO
64static inline void _efx_writeq(struct efx_nic *efx, __le64 value,
65 unsigned int reg)
66{
67 __raw_writeq((__force u64)value, efx->membase + reg);
68}
69static inline __le64 _efx_readq(struct efx_nic *efx, unsigned int reg)
70{
71 return (__force __le64)__raw_readq(efx->membase + reg);
72}
73#endif
74
75static inline void _efx_writed(struct efx_nic *efx, __le32 value,
76 unsigned int reg)
77{
78 __raw_writel((__force u32)value, efx->membase + reg);
79}
80static inline __le32 _efx_readd(struct efx_nic *efx, unsigned int reg)
81{
82 return (__force __le32)__raw_readl(efx->membase + reg);
83}
84
Ben Hutchings9f2f6cd2010-12-06 22:55:00 +000085/* Write a normal 128-bit CSR, locking as appropriate. */
Ben Hutchings53838252012-09-13 01:11:23 +010086static inline void efx_writeo(struct efx_nic *efx, const efx_oword_t *value,
Ben Hutchings12d00ca2009-10-23 08:30:46 +000087 unsigned int reg)
88{
89 unsigned long flags __attribute__ ((unused));
90
Ben Hutchings62776d02010-06-23 11:30:07 +000091 netif_vdbg(efx, hw, efx->net_dev,
92 "writing register %x with " EFX_OWORD_FMT "\n", reg,
93 EFX_OWORD_VAL(*value));
Ben Hutchings12d00ca2009-10-23 08:30:46 +000094
95 spin_lock_irqsave(&efx->biu_lock, flags);
96#ifdef EFX_USE_QWORD_IO
97 _efx_writeq(efx, value->u64[0], reg + 0);
Ben Hutchings12d00ca2009-10-23 08:30:46 +000098 _efx_writeq(efx, value->u64[1], reg + 8);
99#else
100 _efx_writed(efx, value->u32[0], reg + 0);
101 _efx_writed(efx, value->u32[1], reg + 4);
102 _efx_writed(efx, value->u32[2], reg + 8);
Ben Hutchings12d00ca2009-10-23 08:30:46 +0000103 _efx_writed(efx, value->u32[3], reg + 12);
104#endif
105 mmiowb();
106 spin_unlock_irqrestore(&efx->biu_lock, flags);
107}
108
Ben Hutchings9f2f6cd2010-12-06 22:55:00 +0000109/* Write 64-bit SRAM through the supplied mapping, locking as appropriate. */
Ben Hutchings12d00ca2009-10-23 08:30:46 +0000110static inline void efx_sram_writeq(struct efx_nic *efx, void __iomem *membase,
Ben Hutchings53838252012-09-13 01:11:23 +0100111 const efx_qword_t *value, unsigned int index)
Ben Hutchings12d00ca2009-10-23 08:30:46 +0000112{
113 unsigned int addr = index * sizeof(*value);
114 unsigned long flags __attribute__ ((unused));
115
Ben Hutchings62776d02010-06-23 11:30:07 +0000116 netif_vdbg(efx, hw, efx->net_dev,
117 "writing SRAM address %x with " EFX_QWORD_FMT "\n",
118 addr, EFX_QWORD_VAL(*value));
Ben Hutchings12d00ca2009-10-23 08:30:46 +0000119
120 spin_lock_irqsave(&efx->biu_lock, flags);
121#ifdef EFX_USE_QWORD_IO
122 __raw_writeq((__force u64)value->u64[0], membase + addr);
123#else
124 __raw_writel((__force u32)value->u32[0], membase + addr);
Ben Hutchings12d00ca2009-10-23 08:30:46 +0000125 __raw_writel((__force u32)value->u32[1], membase + addr + 4);
126#endif
127 mmiowb();
128 spin_unlock_irqrestore(&efx->biu_lock, flags);
129}
130
Ben Hutchings9f2f6cd2010-12-06 22:55:00 +0000131/* Write a 32-bit CSR or the last dword of a special 128-bit CSR */
Ben Hutchings53838252012-09-13 01:11:23 +0100132static inline void efx_writed(struct efx_nic *efx, const efx_dword_t *value,
Ben Hutchings12d00ca2009-10-23 08:30:46 +0000133 unsigned int reg)
134{
Ben Hutchings62776d02010-06-23 11:30:07 +0000135 netif_vdbg(efx, hw, efx->net_dev,
Ben Hutchings9f2f6cd2010-12-06 22:55:00 +0000136 "writing register %x with "EFX_DWORD_FMT"\n",
Ben Hutchings62776d02010-06-23 11:30:07 +0000137 reg, EFX_DWORD_VAL(*value));
Ben Hutchings12d00ca2009-10-23 08:30:46 +0000138
139 /* No lock required */
140 _efx_writed(efx, value->u32[0], reg);
141}
142
Ben Hutchings9f2f6cd2010-12-06 22:55:00 +0000143/* Read a 128-bit CSR, locking as appropriate. */
Ben Hutchings12d00ca2009-10-23 08:30:46 +0000144static inline void efx_reado(struct efx_nic *efx, efx_oword_t *value,
145 unsigned int reg)
146{
147 unsigned long flags __attribute__ ((unused));
148
149 spin_lock_irqsave(&efx->biu_lock, flags);
150 value->u32[0] = _efx_readd(efx, reg + 0);
Ben Hutchings12d00ca2009-10-23 08:30:46 +0000151 value->u32[1] = _efx_readd(efx, reg + 4);
152 value->u32[2] = _efx_readd(efx, reg + 8);
153 value->u32[3] = _efx_readd(efx, reg + 12);
154 spin_unlock_irqrestore(&efx->biu_lock, flags);
155
Ben Hutchings62776d02010-06-23 11:30:07 +0000156 netif_vdbg(efx, hw, efx->net_dev,
157 "read from register %x, got " EFX_OWORD_FMT "\n", reg,
158 EFX_OWORD_VAL(*value));
Ben Hutchings12d00ca2009-10-23 08:30:46 +0000159}
160
Ben Hutchings9f2f6cd2010-12-06 22:55:00 +0000161/* Read 64-bit SRAM through the supplied mapping, locking as appropriate. */
Ben Hutchings12d00ca2009-10-23 08:30:46 +0000162static inline void efx_sram_readq(struct efx_nic *efx, void __iomem *membase,
163 efx_qword_t *value, unsigned int index)
164{
165 unsigned int addr = index * sizeof(*value);
166 unsigned long flags __attribute__ ((unused));
167
168 spin_lock_irqsave(&efx->biu_lock, flags);
169#ifdef EFX_USE_QWORD_IO
170 value->u64[0] = (__force __le64)__raw_readq(membase + addr);
171#else
172 value->u32[0] = (__force __le32)__raw_readl(membase + addr);
Ben Hutchings12d00ca2009-10-23 08:30:46 +0000173 value->u32[1] = (__force __le32)__raw_readl(membase + addr + 4);
174#endif
175 spin_unlock_irqrestore(&efx->biu_lock, flags);
176
Ben Hutchings62776d02010-06-23 11:30:07 +0000177 netif_vdbg(efx, hw, efx->net_dev,
178 "read from SRAM address %x, got "EFX_QWORD_FMT"\n",
179 addr, EFX_QWORD_VAL(*value));
Ben Hutchings12d00ca2009-10-23 08:30:46 +0000180}
181
Ben Hutchings9f2f6cd2010-12-06 22:55:00 +0000182/* Read a 32-bit CSR or SRAM */
Ben Hutchings12d00ca2009-10-23 08:30:46 +0000183static inline void efx_readd(struct efx_nic *efx, efx_dword_t *value,
184 unsigned int reg)
185{
186 value->u32[0] = _efx_readd(efx, reg);
Ben Hutchings62776d02010-06-23 11:30:07 +0000187 netif_vdbg(efx, hw, efx->net_dev,
188 "read from register %x, got "EFX_DWORD_FMT"\n",
189 reg, EFX_DWORD_VAL(*value));
Ben Hutchings12d00ca2009-10-23 08:30:46 +0000190}
191
Ben Hutchings9f2f6cd2010-12-06 22:55:00 +0000192/* Write a 128-bit CSR forming part of a table */
Ben Hutchings53838252012-09-13 01:11:23 +0100193static inline void
194efx_writeo_table(struct efx_nic *efx, const efx_oword_t *value,
195 unsigned int reg, unsigned int index)
Ben Hutchings12d00ca2009-10-23 08:30:46 +0000196{
197 efx_writeo(efx, value, reg + index * sizeof(efx_oword_t));
198}
199
Ben Hutchings9f2f6cd2010-12-06 22:55:00 +0000200/* Read a 128-bit CSR forming part of a table */
Ben Hutchings12d00ca2009-10-23 08:30:46 +0000201static inline void efx_reado_table(struct efx_nic *efx, efx_oword_t *value,
202 unsigned int reg, unsigned int index)
203{
204 efx_reado(efx, value, reg + index * sizeof(efx_oword_t));
205}
206
Ben Hutchings64a27752013-06-28 20:14:46 +0100207/* Page size used as step between per-VI registers */
208#define EFX_VI_PAGE_SIZE 0x2000
Ben Hutchings12d00ca2009-10-23 08:30:46 +0000209
Ben Hutchings64a27752013-06-28 20:14:46 +0100210/* Calculate offset to page-mapped register */
Ben Hutchings12d00ca2009-10-23 08:30:46 +0000211#define EFX_PAGED_REG(page, reg) \
Ben Hutchings64a27752013-06-28 20:14:46 +0100212 ((page) * EFX_VI_PAGE_SIZE + (reg))
Ben Hutchings12d00ca2009-10-23 08:30:46 +0000213
Ben Hutchings9f2f6cd2010-12-06 22:55:00 +0000214/* Write the whole of RX_DESC_UPD or TX_DESC_UPD */
Ben Hutchings1a29cc42010-12-06 22:55:33 +0000215static inline void _efx_writeo_page(struct efx_nic *efx, efx_oword_t *value,
216 unsigned int reg, unsigned int page)
Ben Hutchings12d00ca2009-10-23 08:30:46 +0000217{
Ben Hutchingse5061472010-12-06 22:58:41 +0000218 reg = EFX_PAGED_REG(page, reg);
219
220 netif_vdbg(efx, hw, efx->net_dev,
221 "writing register %x with " EFX_OWORD_FMT "\n", reg,
222 EFX_OWORD_VAL(*value));
223
224#ifdef EFX_USE_QWORD_IO
225 _efx_writeq(efx, value->u64[0], reg + 0);
Ben Hutchings483f97f2011-09-01 12:09:59 +0000226 _efx_writeq(efx, value->u64[1], reg + 8);
Ben Hutchingse5061472010-12-06 22:58:41 +0000227#else
228 _efx_writed(efx, value->u32[0], reg + 0);
229 _efx_writed(efx, value->u32[1], reg + 4);
Ben Hutchingse5061472010-12-06 22:58:41 +0000230 _efx_writed(efx, value->u32[2], reg + 8);
231 _efx_writed(efx, value->u32[3], reg + 12);
Ben Hutchings483f97f2011-09-01 12:09:59 +0000232#endif
Ben Hutchings12d00ca2009-10-23 08:30:46 +0000233}
Ben Hutchings1a29cc42010-12-06 22:55:33 +0000234#define efx_writeo_page(efx, value, reg, page) \
235 _efx_writeo_page(efx, value, \
236 reg + \
237 BUILD_BUG_ON_ZERO((reg) != 0x830 && (reg) != 0xa10), \
238 page)
Ben Hutchings12d00ca2009-10-23 08:30:46 +0000239
Ben Hutchings9f2f6cd2010-12-06 22:55:00 +0000240/* Write a page-mapped 32-bit CSR (EVQ_RPTR or the high bits of
241 * RX_DESC_UPD or TX_DESC_UPD)
242 */
Ben Hutchings53838252012-09-13 01:11:23 +0100243static inline void
244_efx_writed_page(struct efx_nic *efx, const efx_dword_t *value,
245 unsigned int reg, unsigned int page)
Ben Hutchings12d00ca2009-10-23 08:30:46 +0000246{
247 efx_writed(efx, value, EFX_PAGED_REG(page, reg));
248}
Ben Hutchings1a29cc42010-12-06 22:55:33 +0000249#define efx_writed_page(efx, value, reg, page) \
250 _efx_writed_page(efx, value, \
251 reg + \
252 BUILD_BUG_ON_ZERO((reg) != 0x400 && (reg) != 0x83c \
253 && (reg) != 0xa1c), \
254 page)
Ben Hutchings12d00ca2009-10-23 08:30:46 +0000255
Ben Hutchings9f2f6cd2010-12-06 22:55:00 +0000256/* Write TIMER_COMMAND. This is a page-mapped 32-bit CSR, but a bug
257 * in the BIU means that writes to TIMER_COMMAND[0] invalidate the
258 * collector register.
259 */
Ben Hutchings1a29cc42010-12-06 22:55:33 +0000260static inline void _efx_writed_page_locked(struct efx_nic *efx,
Ben Hutchings53838252012-09-13 01:11:23 +0100261 const efx_dword_t *value,
Ben Hutchings1a29cc42010-12-06 22:55:33 +0000262 unsigned int reg,
263 unsigned int page)
Ben Hutchings12d00ca2009-10-23 08:30:46 +0000264{
265 unsigned long flags __attribute__ ((unused));
266
267 if (page == 0) {
268 spin_lock_irqsave(&efx->biu_lock, flags);
269 efx_writed(efx, value, EFX_PAGED_REG(page, reg));
270 spin_unlock_irqrestore(&efx->biu_lock, flags);
271 } else {
272 efx_writed(efx, value, EFX_PAGED_REG(page, reg));
273 }
274}
Ben Hutchings1a29cc42010-12-06 22:55:33 +0000275#define efx_writed_page_locked(efx, value, reg, page) \
276 _efx_writed_page_locked(efx, value, \
277 reg + BUILD_BUG_ON_ZERO((reg) != 0x420), \
278 page)
Ben Hutchings12d00ca2009-10-23 08:30:46 +0000279
280#endif /* EFX_IO_H */