2 * Copyright (C) 2015-2017 Netronome Systems, Inc.
4 * This software is dual licensed under the GNU General License Version 2,
5 * June 1991 as shown in the file COPYING in the top-level directory of this
6 * source tree or the BSD 2-Clause License provided below. You have the
7 * option to license this software under the complete terms of either license.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * 2. Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 * Library of functions to access the NFP's CPP bus
37 * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
38 * Jason McMullan <jason.mcmullan@netronome.com>
39 * Rolf Neugebauer <rolf.neugebauer@netronome.com>
42 #include <asm/unaligned.h>
43 #include <linux/bitfield.h>
44 #include <linux/delay.h>
45 #include <linux/kernel.h>
46 #include <linux/module.h>
47 #include <linux/slab.h>
48 #include <linux/sched.h>
51 #include "nfp6000/nfp6000.h"
52 #include "nfp6000/nfp_xpb.h"
55 #define NFP_PL_DEVICE_ID 0x00000004
56 #define NFP_PL_DEVICE_ID_MASK GENMASK(7, 0)
58 #define NFP6000_ARM_GCSR_SOFTMODEL0 0x00400144
61 * nfp_cpp_readl() - Read a u32 word from a CPP location
62 * @cpp: CPP device handle
63 * @cpp_id: CPP ID for operation
64 * @address: Address for operation
65 * @value: Pointer to read buffer
67 * Return: length of the io, or -ERRNO
69 int nfp_cpp_readl(struct nfp_cpp *cpp, u32 cpp_id,
70 unsigned long long address, u32 *value)
75 err = nfp_cpp_read(cpp, cpp_id, address, tmp, sizeof(tmp));
76 *value = get_unaligned_le32(tmp);
82 * nfp_cpp_writel() - Write a u32 word to a CPP location
83 * @cpp: CPP device handle
84 * @cpp_id: CPP ID for operation
85 * @address: Address for operation
86 * @value: Value to write
88 * Return: length of the io, or -ERRNO
90 int nfp_cpp_writel(struct nfp_cpp *cpp, u32 cpp_id,
91 unsigned long long address, u32 value)
95 put_unaligned_le32(value, tmp);
96 return nfp_cpp_write(cpp, cpp_id, address, tmp, sizeof(tmp));
100 * nfp_cpp_readq() - Read a u64 word from a CPP location
101 * @cpp: CPP device handle
102 * @cpp_id: CPP ID for operation
103 * @address: Address for operation
104 * @value: Pointer to read buffer
106 * Return: length of the io, or -ERRNO
108 int nfp_cpp_readq(struct nfp_cpp *cpp, u32 cpp_id,
109 unsigned long long address, u64 *value)
114 err = nfp_cpp_read(cpp, cpp_id, address, tmp, sizeof(tmp));
115 *value = get_unaligned_le64(tmp);
121 * nfp_cpp_writeq() - Write a u64 word to a CPP location
122 * @cpp: CPP device handle
123 * @cpp_id: CPP ID for operation
124 * @address: Address for operation
125 * @value: Value to write
127 * Return: length of the io, or -ERRNO
129 int nfp_cpp_writeq(struct nfp_cpp *cpp, u32 cpp_id,
130 unsigned long long address, u64 value)
134 put_unaligned_le64(value, tmp);
135 return nfp_cpp_write(cpp, cpp_id, address, tmp, sizeof(tmp));
138 /* NOTE: This code should not use nfp_xpb_* functions,
139 * as those are model-specific
141 int nfp_cpp_model_autodetect(struct nfp_cpp *cpp, u32 *model)
143 const u32 arm_id = NFP_CPP_ID(NFP_CPP_TARGET_ARM, 0, 0);
147 err = nfp_cpp_readl(cpp, arm_id, NFP6000_ARM_GCSR_SOFTMODEL0, model);
151 /* The PL's PluDeviceID revision code is authoratative */
153 err = nfp_xpb_readl(cpp, NFP_XPB_DEVICE(1, 1, 16) + NFP_PL_DEVICE_ID,
158 *model |= (NFP_PL_DEVICE_ID_MASK & reg) - 0x10;
163 static u8 nfp_bytemask(int width, u64 addr)
168 return 0x0f << (addr & 4);
170 return 0x03 << (addr & 6);
172 return 0x01 << (addr & 7);
177 int nfp_cpp_explicit_read(struct nfp_cpp *cpp, u32 cpp_id,
178 u64 addr, void *buff, size_t len, int width_read)
180 struct nfp_cpp_explicit *expl;
185 if (len & (width_read - 1))
188 expl = nfp_cpp_explicit_acquire(cpp);
192 incr = min_t(int, 16 * width_read, 128);
193 incr = min_t(int, incr, len);
195 /* Translate a NFP_CPP_ACTION_RW to action 0 */
196 if (NFP_CPP_ID_ACTION_of(cpp_id) == NFP_CPP_ACTION_RW)
197 cpp_id = NFP_CPP_ID(NFP_CPP_ID_TARGET_of(cpp_id), 0,
198 NFP_CPP_ID_TOKEN_of(cpp_id));
200 byte_mask = nfp_bytemask(width_read, addr);
202 nfp_cpp_explicit_set_target(expl, cpp_id,
203 incr / width_read - 1, byte_mask);
204 nfp_cpp_explicit_set_posted(expl, 1, 0, NFP_SIGNAL_PUSH,
207 for (i = 0; i < len; i += incr, addr += incr, tmp += incr) {
208 if (i + incr > len) {
210 nfp_cpp_explicit_set_target(expl, cpp_id,
211 incr / width_read - 1,
215 err = nfp_cpp_explicit_do(expl, addr);
219 err = nfp_cpp_explicit_get(expl, tmp, incr);
225 nfp_cpp_explicit_release(expl);
230 int nfp_cpp_explicit_write(struct nfp_cpp *cpp, u32 cpp_id, u64 addr,
231 const void *buff, size_t len, int width_write)
233 struct nfp_cpp_explicit *expl;
234 const char *tmp = buff;
238 if (len & (width_write - 1))
241 expl = nfp_cpp_explicit_acquire(cpp);
245 incr = min_t(int, 16 * width_write, 128);
246 incr = min_t(int, incr, len);
248 /* Translate a NFP_CPP_ACTION_RW to action 1 */
249 if (NFP_CPP_ID_ACTION_of(cpp_id) == NFP_CPP_ACTION_RW)
250 cpp_id = NFP_CPP_ID(NFP_CPP_ID_TARGET_of(cpp_id), 1,
251 NFP_CPP_ID_TOKEN_of(cpp_id));
253 byte_mask = nfp_bytemask(width_write, addr);
255 nfp_cpp_explicit_set_target(expl, cpp_id,
256 incr / width_write - 1, byte_mask);
257 nfp_cpp_explicit_set_posted(expl, 1, 0, NFP_SIGNAL_PULL,
260 for (i = 0; i < len; i += incr, addr += incr, tmp += incr) {
261 if (i + incr > len) {
263 nfp_cpp_explicit_set_target(expl, cpp_id,
264 incr / width_write - 1,
268 err = nfp_cpp_explicit_put(expl, tmp, incr);
272 err = nfp_cpp_explicit_do(expl, addr);
278 nfp_cpp_explicit_release(expl);
284 * nfp_cpp_map_area() - Helper function to map an area
285 * @cpp: NFP CPP handler
286 * @name: Name for the area
287 * @domain: CPP domain
288 * @target: CPP target
290 * @size: Size of the area
291 * @area: Area handle (output)
293 * Map an area of IOMEM access. To undo the effect of this function call
294 * @nfp_cpp_area_release_free(*area).
296 * Return: Pointer to memory mapped area or ERR_PTR
299 nfp_cpp_map_area(struct nfp_cpp *cpp, const char *name, int domain, int target,
300 u64 addr, unsigned long size, struct nfp_cpp_area **area)
305 dest = NFP_CPP_ISLAND_ID(target, NFP_CPP_ACTION_RW, 0, domain);
307 *area = nfp_cpp_area_alloc_acquire(cpp, name, dest, addr, size);
311 res = nfp_cpp_area_iomem(*area);
313 goto err_release_free;
318 nfp_cpp_area_release_free(*area);
320 return (u8 __iomem *)ERR_PTR(-EIO);