raw/ifpga/base: add SPI and MAX10 device driver
[dpdk.git] / drivers / raw / ifpga_rawdev / base / ifpga_feature_dev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4
5 #include <sys/ioctl.h>
6
7 #include "ifpga_feature_dev.h"
8
9 /*
10  * Enable Port by clear the port soft reset bit, which is set by default.
11  * The AFU is unable to respond to any MMIO access while in reset.
12  * __fpga_port_enable function should only be used after __fpga_port_disable
13  * function.
14  */
15 void __fpga_port_enable(struct ifpga_port_hw *port)
16 {
17         struct feature_port_header *port_hdr;
18         struct feature_port_control control;
19
20         WARN_ON(!port->disable_count);
21
22         if (--port->disable_count != 0)
23                 return;
24
25         port_hdr = get_port_feature_ioaddr_by_index(port,
26                                                     PORT_FEATURE_ID_HEADER);
27         WARN_ON(!port_hdr);
28
29         control.csr = readq(&port_hdr->control);
30         control.port_sftrst = 0x0;
31         writeq(control.csr, &port_hdr->control);
32 }
33
34 int __fpga_port_disable(struct ifpga_port_hw *port)
35 {
36         struct feature_port_header *port_hdr;
37         struct feature_port_control control;
38
39         if (port->disable_count++ != 0)
40                 return 0;
41
42         port_hdr = get_port_feature_ioaddr_by_index(port,
43                                                     PORT_FEATURE_ID_HEADER);
44         WARN_ON(!port_hdr);
45
46         /* Set port soft reset */
47         control.csr = readq(&port_hdr->control);
48         control.port_sftrst = 0x1;
49         writeq(control.csr, &port_hdr->control);
50
51         /*
52          * HW sets ack bit to 1 when all outstanding requests have been drained
53          * on this port and minimum soft reset pulse width has elapsed.
54          * Driver polls port_soft_reset_ack to determine if reset done by HW.
55          */
56         control.port_sftrst_ack = 1;
57
58         if (fpga_wait_register_field(port_sftrst_ack, control,
59                                      &port_hdr->control, RST_POLL_TIMEOUT,
60                                      RST_POLL_INVL)) {
61                 dev_err(port, "timeout, fail to reset device\n");
62                 return -ETIMEDOUT;
63         }
64
65         return 0;
66 }
67
68 int fpga_get_afu_uuid(struct ifpga_port_hw *port, struct uuid *uuid)
69 {
70         struct feature_port_header *port_hdr;
71         u64 guidl, guidh;
72
73         if (!uuid)
74                 return -EINVAL;
75
76         port_hdr = get_port_feature_ioaddr_by_index(port, PORT_FEATURE_ID_UAFU);
77
78         spinlock_lock(&port->lock);
79         guidl = readq(&port_hdr->afu_header.guid.b[0]);
80         guidh = readq(&port_hdr->afu_header.guid.b[8]);
81         spinlock_unlock(&port->lock);
82
83         opae_memcpy(uuid->b, &guidl, sizeof(u64));
84         opae_memcpy(uuid->b + 8, &guidh, sizeof(u64));
85
86         return 0;
87 }
88
89 /* Mask / Unmask Port Errors by the Error Mask register. */
90 void port_err_mask(struct ifpga_port_hw *port, bool mask)
91 {
92         struct feature_port_error *port_err;
93         struct feature_port_err_key err_mask;
94
95         port_err = get_port_feature_ioaddr_by_index(port,
96                                                     PORT_FEATURE_ID_ERROR);
97
98         if (mask)
99                 err_mask.csr = PORT_ERR_MASK;
100         else
101                 err_mask.csr = 0;
102
103         writeq(err_mask.csr, &port_err->error_mask);
104 }
105
106 /* Clear All Port Errors. */
107 int port_err_clear(struct ifpga_port_hw *port, u64 err)
108 {
109         struct feature_port_header *port_hdr;
110         struct feature_port_error *port_err;
111         struct feature_port_err_key mask;
112         struct feature_port_first_err_key first;
113         struct feature_port_status status;
114         int ret = 0;
115
116         port_err = get_port_feature_ioaddr_by_index(port,
117                                                     PORT_FEATURE_ID_ERROR);
118         port_hdr = get_port_feature_ioaddr_by_index(port,
119                                                     PORT_FEATURE_ID_HEADER);
120
121         /*
122          * Clear All Port Errors
123          *
124          * - Check for AP6 State
125          * - Halt Port by keeping Port in reset
126          * - Set PORT Error mask to all 1 to mask errors
127          * - Clear all errors
128          * - Set Port mask to all 0 to enable errors
129          * - All errors start capturing new errors
130          * - Enable Port by pulling the port out of reset
131          */
132
133         /* If device is still in AP6 state, can not clear any error.*/
134         status.csr = readq(&port_hdr->status);
135         if (status.power_state == PORT_POWER_STATE_AP6) {
136                 dev_err(dev, "Could not clear errors, device in AP6 state.\n");
137                 return -EBUSY;
138         }
139
140         /* Halt Port by keeping Port in reset */
141         ret = __fpga_port_disable(port);
142         if (ret)
143                 return ret;
144
145         /* Mask all errors */
146         port_err_mask(port, true);
147
148         /* Clear errors if err input matches with current port errors.*/
149         mask.csr = readq(&port_err->port_error);
150
151         if (mask.csr == err) {
152                 writeq(mask.csr, &port_err->port_error);
153
154                 first.csr = readq(&port_err->port_first_error);
155                 writeq(first.csr, &port_err->port_first_error);
156         } else {
157                 ret = -EBUSY;
158         }
159
160         /* Clear mask */
161         port_err_mask(port, false);
162
163         /* Enable the Port by clear the reset */
164         __fpga_port_enable(port);
165
166         return ret;
167 }
168
169 int port_clear_error(struct ifpga_port_hw *port)
170 {
171         struct feature_port_error *port_err;
172         struct feature_port_err_key error;
173
174         port_err = get_port_feature_ioaddr_by_index(port,
175                                                     PORT_FEATURE_ID_ERROR);
176         error.csr = readq(&port_err->port_error);
177
178         dev_info(port, "read port error: 0x%lx\n", (unsigned long)error.csr);
179
180         return port_err_clear(port, error.csr);
181 }
182
183 static struct feature_driver fme_feature_drvs[] = {
184         {FEATURE_DRV(FME_FEATURE_ID_HEADER, FME_FEATURE_HEADER,
185                         &fme_hdr_ops),},
186         {FEATURE_DRV(FME_FEATURE_ID_THERMAL_MGMT, FME_FEATURE_THERMAL_MGMT,
187                         &fme_thermal_mgmt_ops),},
188         {FEATURE_DRV(FME_FEATURE_ID_POWER_MGMT, FME_FEATURE_POWER_MGMT,
189                         &fme_power_mgmt_ops),},
190         {FEATURE_DRV(FME_FEATURE_ID_GLOBAL_ERR, FME_FEATURE_GLOBAL_ERR,
191                         &fme_global_err_ops),},
192         {FEATURE_DRV(FME_FEATURE_ID_PR_MGMT, FME_FEATURE_PR_MGMT,
193                         &fme_pr_mgmt_ops),},
194         {FEATURE_DRV(FME_FEATURE_ID_GLOBAL_DPERF, FME_FEATURE_GLOBAL_DPERF,
195                         &fme_global_dperf_ops),},
196         {FEATURE_DRV(FME_FEATURE_ID_HSSI_ETH, FME_FEATURE_HSSI_ETH,
197         &fme_hssi_eth_ops),},
198         {FEATURE_DRV(FME_FEATURE_ID_EMIF_MGMT, FME_FEATURE_EMIF_MGMT,
199         &fme_emif_ops),},
200         {FEATURE_DRV(FME_FEATURE_ID_MAX10_SPI, FME_FEATURE_MAX10_SPI,
201         &fme_spi_master_ops),},
202         {FEATURE_DRV(FME_FEATURE_ID_NIOS_SPI, FME_FEATURE_NIOS_SPI,
203         &fme_nios_spi_master_ops),},
204         {0, NULL, NULL}, /* end of arrary */
205 };
206
207 static struct feature_driver port_feature_drvs[] = {
208         {FEATURE_DRV(PORT_FEATURE_ID_HEADER, PORT_FEATURE_HEADER,
209                         &ifpga_rawdev_port_hdr_ops)},
210         {FEATURE_DRV(PORT_FEATURE_ID_ERROR, PORT_FEATURE_ERR,
211                         &ifpga_rawdev_port_error_ops)},
212         {FEATURE_DRV(PORT_FEATURE_ID_UINT, PORT_FEATURE_UINT,
213                         &ifpga_rawdev_port_uint_ops)},
214         {FEATURE_DRV(PORT_FEATURE_ID_STP, PORT_FEATURE_STP,
215                         &ifpga_rawdev_port_stp_ops)},
216         {FEATURE_DRV(PORT_FEATURE_ID_UAFU, PORT_FEATURE_UAFU,
217                         &ifpga_rawdev_port_afu_ops)},
218         {0, NULL, NULL}, /* end of array */
219 };
220
221 const char *get_fme_feature_name(unsigned int id)
222 {
223         struct feature_driver *drv = fme_feature_drvs;
224
225         while (drv->name) {
226                 if (drv->id == id)
227                         return drv->name;
228
229                 drv++;
230         }
231
232         return NULL;
233 }
234
235 const char *get_port_feature_name(unsigned int id)
236 {
237         struct feature_driver *drv = port_feature_drvs;
238
239         while (drv->name) {
240                 if (drv->id == id)
241                         return drv->name;
242
243                 drv++;
244         }
245
246         return NULL;
247 }
248
249 static void feature_uinit(struct ifpga_feature_list *list)
250 {
251         struct feature *feature;
252
253         TAILQ_FOREACH(feature, list, next) {
254                 if (feature->state != IFPGA_FEATURE_ATTACHED)
255                         continue;
256                 if (feature->ops && feature->ops->uinit)
257                         feature->ops->uinit(feature);
258         }
259 }
260
261 static int feature_init(struct feature_driver *drv,
262                 struct ifpga_feature_list *list)
263 {
264         struct feature *feature;
265         int ret;
266
267         while (drv->ops) {
268                 TAILQ_FOREACH(feature, list, next) {
269                         if (feature->state != IFPGA_FEATURE_ATTACHED)
270                                 continue;
271                         if (feature->id == drv->id) {
272                                 feature->ops = drv->ops;
273                                 feature->name = drv->name;
274                                 if (feature->ops->init) {
275                                         ret = feature->ops->init(feature);
276                                         if (ret)
277                                                 goto error;
278                                 }
279                         }
280                 }
281                 drv++;
282         }
283
284         return 0;
285 error:
286         feature_uinit(list);
287         return ret;
288 }
289
290 int fme_hw_init(struct ifpga_fme_hw *fme)
291 {
292         int ret;
293
294         if (fme->state != IFPGA_FME_IMPLEMENTED)
295                 return -ENODEV;
296
297         ret = feature_init(fme_feature_drvs, &fme->feature_list);
298         if (ret)
299                 return ret;
300
301         return 0;
302 }
303
304 void fme_hw_uinit(struct ifpga_fme_hw *fme)
305 {
306         feature_uinit(&fme->feature_list);
307 }
308
309 void port_hw_uinit(struct ifpga_port_hw *port)
310 {
311         feature_uinit(&port->feature_list);
312 }
313
314 int port_hw_init(struct ifpga_port_hw *port)
315 {
316         int ret;
317
318         if (port->state == IFPGA_PORT_UNUSED)
319                 return 0;
320
321         ret = feature_init(port_feature_drvs, &port->feature_list);
322         if (ret)
323                 goto error;
324
325         return 0;
326 error:
327         port_hw_uinit(port);
328         return ret;
329 }