1 // SPDX-License-Identifier: GPL-2.0
2
3 /***************************************************************************
4 * GPIB Driver for Fluke cda devices. Basically, its a driver for a (bugfixed)
5 * cb7210 connected to channel 0 of a pl330 dma controller.
6 * Author: Frank Mori Hess <fmh6jj@gmail.com>
7 * copyright: (C) 2006, 2010, 2015 Fluke Corporation
8 ***************************************************************************/
9
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #define dev_fmt pr_fmt
12 #define DRV_NAME KBUILD_MODNAME
13
14 #include "fluke_gpib.h"
15
16 #include "gpibP.h"
17 #include <linux/dma-mapping.h>
18 #include <linux/ioport.h>
19 #include <linux/module.h>
20 #include <linux/mod_devicetable.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23
24 MODULE_LICENSE("GPL");
25 MODULE_DESCRIPTION("GPIB Driver for Fluke cda devices");
26
27 static int fluke_attach_holdoff_all(struct gpib_board *board,
28 const struct gpib_board_config *config);
29 static int fluke_attach_holdoff_end(struct gpib_board *board,
30 const struct gpib_board_config *config);
31 static void fluke_detach(struct gpib_board *board);
32 static int fluke_config_dma(struct gpib_board *board, int output);
33 static irqreturn_t fluke_gpib_internal_interrupt(struct gpib_board *board);
34
35 static struct platform_device *fluke_gpib_pdev;
36
fluke_locking_read_byte(struct nec7210_priv * nec_priv,unsigned int register_number)37 static u8 fluke_locking_read_byte(struct nec7210_priv *nec_priv, unsigned int register_number)
38 {
39 u8 retval;
40 unsigned long flags;
41
42 spin_lock_irqsave(&nec_priv->register_page_lock, flags);
43 retval = fluke_read_byte_nolock(nec_priv, register_number);
44 spin_unlock_irqrestore(&nec_priv->register_page_lock, flags);
45 return retval;
46 }
47
fluke_locking_write_byte(struct nec7210_priv * nec_priv,u8 byte,unsigned int register_number)48 static void fluke_locking_write_byte(struct nec7210_priv *nec_priv, u8 byte,
49 unsigned int register_number)
50 {
51 unsigned long flags;
52
53 spin_lock_irqsave(&nec_priv->register_page_lock, flags);
54 fluke_write_byte_nolock(nec_priv, byte, register_number);
55 spin_unlock_irqrestore(&nec_priv->register_page_lock, flags);
56 }
57
58 // wrappers for interface functions
fluke_read(struct gpib_board * board,u8 * buffer,size_t length,int * end,size_t * bytes_read)59 static int fluke_read(struct gpib_board *board, u8 *buffer, size_t length, int *end,
60 size_t *bytes_read)
61 {
62 struct fluke_priv *priv = board->private_data;
63
64 return nec7210_read(board, &priv->nec7210_priv, buffer, length, end, bytes_read);
65 }
66
fluke_write(struct gpib_board * board,u8 * buffer,size_t length,int send_eoi,size_t * bytes_written)67 static int fluke_write(struct gpib_board *board, u8 *buffer, size_t length,
68 int send_eoi, size_t *bytes_written)
69 {
70 struct fluke_priv *priv = board->private_data;
71
72 return nec7210_write(board, &priv->nec7210_priv, buffer, length, send_eoi, bytes_written);
73 }
74
fluke_command(struct gpib_board * board,u8 * buffer,size_t length,size_t * bytes_written)75 static int fluke_command(struct gpib_board *board, u8 *buffer,
76 size_t length, size_t *bytes_written)
77 {
78 struct fluke_priv *priv = board->private_data;
79
80 return nec7210_command(board, &priv->nec7210_priv, buffer, length, bytes_written);
81 }
82
fluke_take_control(struct gpib_board * board,int synchronous)83 static int fluke_take_control(struct gpib_board *board, int synchronous)
84 {
85 struct fluke_priv *priv = board->private_data;
86
87 return nec7210_take_control(board, &priv->nec7210_priv, synchronous);
88 }
89
fluke_go_to_standby(struct gpib_board * board)90 static int fluke_go_to_standby(struct gpib_board *board)
91 {
92 struct fluke_priv *priv = board->private_data;
93
94 return nec7210_go_to_standby(board, &priv->nec7210_priv);
95 }
96
fluke_request_system_control(struct gpib_board * board,int request_control)97 static int fluke_request_system_control(struct gpib_board *board, int request_control)
98 {
99 struct fluke_priv *priv = board->private_data;
100 struct nec7210_priv *nec_priv = &priv->nec7210_priv;
101
102 return nec7210_request_system_control(board, nec_priv, request_control);
103 }
104
fluke_interface_clear(struct gpib_board * board,int assert)105 static void fluke_interface_clear(struct gpib_board *board, int assert)
106 {
107 struct fluke_priv *priv = board->private_data;
108
109 nec7210_interface_clear(board, &priv->nec7210_priv, assert);
110 }
111
fluke_remote_enable(struct gpib_board * board,int enable)112 static void fluke_remote_enable(struct gpib_board *board, int enable)
113 {
114 struct fluke_priv *priv = board->private_data;
115
116 nec7210_remote_enable(board, &priv->nec7210_priv, enable);
117 }
118
fluke_enable_eos(struct gpib_board * board,u8 eos_byte,int compare_8_bits)119 static int fluke_enable_eos(struct gpib_board *board, u8 eos_byte, int compare_8_bits)
120 {
121 struct fluke_priv *priv = board->private_data;
122
123 return nec7210_enable_eos(board, &priv->nec7210_priv, eos_byte, compare_8_bits);
124 }
125
fluke_disable_eos(struct gpib_board * board)126 static void fluke_disable_eos(struct gpib_board *board)
127 {
128 struct fluke_priv *priv = board->private_data;
129
130 nec7210_disable_eos(board, &priv->nec7210_priv);
131 }
132
fluke_update_status(struct gpib_board * board,unsigned int clear_mask)133 static unsigned int fluke_update_status(struct gpib_board *board, unsigned int clear_mask)
134 {
135 struct fluke_priv *priv = board->private_data;
136
137 return nec7210_update_status(board, &priv->nec7210_priv, clear_mask);
138 }
139
fluke_primary_address(struct gpib_board * board,unsigned int address)140 static int fluke_primary_address(struct gpib_board *board, unsigned int address)
141 {
142 struct fluke_priv *priv = board->private_data;
143
144 return nec7210_primary_address(board, &priv->nec7210_priv, address);
145 }
146
fluke_secondary_address(struct gpib_board * board,unsigned int address,int enable)147 static int fluke_secondary_address(struct gpib_board *board, unsigned int address, int enable)
148 {
149 struct fluke_priv *priv = board->private_data;
150
151 return nec7210_secondary_address(board, &priv->nec7210_priv, address, enable);
152 }
153
fluke_parallel_poll(struct gpib_board * board,u8 * result)154 static int fluke_parallel_poll(struct gpib_board *board, u8 *result)
155 {
156 struct fluke_priv *priv = board->private_data;
157
158 return nec7210_parallel_poll(board, &priv->nec7210_priv, result);
159 }
160
fluke_parallel_poll_configure(struct gpib_board * board,u8 configuration)161 static void fluke_parallel_poll_configure(struct gpib_board *board, u8 configuration)
162 {
163 struct fluke_priv *priv = board->private_data;
164
165 nec7210_parallel_poll_configure(board, &priv->nec7210_priv, configuration);
166 }
167
fluke_parallel_poll_response(struct gpib_board * board,int ist)168 static void fluke_parallel_poll_response(struct gpib_board *board, int ist)
169 {
170 struct fluke_priv *priv = board->private_data;
171
172 nec7210_parallel_poll_response(board, &priv->nec7210_priv, ist);
173 }
174
fluke_serial_poll_response(struct gpib_board * board,u8 status)175 static void fluke_serial_poll_response(struct gpib_board *board, u8 status)
176 {
177 struct fluke_priv *priv = board->private_data;
178
179 nec7210_serial_poll_response(board, &priv->nec7210_priv, status);
180 }
181
fluke_serial_poll_status(struct gpib_board * board)182 static u8 fluke_serial_poll_status(struct gpib_board *board)
183 {
184 struct fluke_priv *priv = board->private_data;
185
186 return nec7210_serial_poll_status(board, &priv->nec7210_priv);
187 }
188
fluke_return_to_local(struct gpib_board * board)189 static void fluke_return_to_local(struct gpib_board *board)
190 {
191 struct fluke_priv *priv = board->private_data;
192 struct nec7210_priv *nec_priv = &priv->nec7210_priv;
193
194 write_byte(nec_priv, AUX_RTL2, AUXMR);
195 udelay(1);
196 write_byte(nec_priv, AUX_RTL, AUXMR);
197 }
198
fluke_line_status(const struct gpib_board * board)199 static int fluke_line_status(const struct gpib_board *board)
200 {
201 int status = VALID_ALL;
202 int bsr_bits;
203 struct fluke_priv *e_priv;
204
205 e_priv = board->private_data;
206
207 bsr_bits = fluke_paged_read_byte(e_priv, BUS_STATUS, BUS_STATUS_PAGE);
208
209 if ((bsr_bits & BSR_REN_BIT) == 0)
210 status |= BUS_REN;
211 if ((bsr_bits & BSR_IFC_BIT) == 0)
212 status |= BUS_IFC;
213 if ((bsr_bits & BSR_SRQ_BIT) == 0)
214 status |= BUS_SRQ;
215 if ((bsr_bits & BSR_EOI_BIT) == 0)
216 status |= BUS_EOI;
217 if ((bsr_bits & BSR_NRFD_BIT) == 0)
218 status |= BUS_NRFD;
219 if ((bsr_bits & BSR_NDAC_BIT) == 0)
220 status |= BUS_NDAC;
221 if ((bsr_bits & BSR_DAV_BIT) == 0)
222 status |= BUS_DAV;
223 if ((bsr_bits & BSR_ATN_BIT) == 0)
224 status |= BUS_ATN;
225
226 return status;
227 }
228
fluke_t1_delay(struct gpib_board * board,unsigned int nano_sec)229 static int fluke_t1_delay(struct gpib_board *board, unsigned int nano_sec)
230 {
231 struct fluke_priv *e_priv = board->private_data;
232 struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
233 unsigned int retval;
234
235 retval = nec7210_t1_delay(board, nec_priv, nano_sec);
236
237 if (nano_sec <= 350) {
238 write_byte(nec_priv, AUX_HI_SPEED, AUXMR);
239 retval = 350;
240 } else {
241 write_byte(nec_priv, AUX_LO_SPEED, AUXMR);
242 }
243 return retval;
244 }
245
lacs_or_read_ready(struct gpib_board * board)246 static int lacs_or_read_ready(struct gpib_board *board)
247 {
248 const struct fluke_priv *e_priv = board->private_data;
249 const struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
250 unsigned long flags;
251 int retval;
252
253 spin_lock_irqsave(&board->spinlock, flags);
254 retval = test_bit(LACS_NUM, &board->status) || test_bit(READ_READY_BN, &nec_priv->state);
255 spin_unlock_irqrestore(&board->spinlock, flags);
256 return retval;
257 }
258
259 /*
260 * Wait until it is possible for a read to do something useful. This
261 * is not essential, it only exists to prevent RFD holdoff from being released pointlessly.
262 */
wait_for_read(struct gpib_board * board)263 static int wait_for_read(struct gpib_board *board)
264 {
265 struct fluke_priv *e_priv = board->private_data;
266 struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
267 int retval = 0;
268
269 if (wait_event_interruptible(board->wait,
270 lacs_or_read_ready(board) ||
271 test_bit(DEV_CLEAR_BN, &nec_priv->state) ||
272 test_bit(TIMO_NUM, &board->status)))
273 retval = -ERESTARTSYS;
274
275 if (test_bit(TIMO_NUM, &board->status))
276 retval = -ETIMEDOUT;
277 if (test_and_clear_bit(DEV_CLEAR_BN, &nec_priv->state))
278 retval = -EINTR;
279 return retval;
280 }
281
282 /*
283 * Check if the SH state machine is in SGNS. We check twice since there is a very small chance
284 * we could be blowing through SGNS from SIDS to SDYS if there is already a
285 * byte available in the handshake state machine. We are interested
286 * in the case where the handshake is stuck in SGNS due to no byte being
287 * available to the chip (and thus we can be confident a dma transfer will
288 * result in at least one byte making it into the chip). This matters
289 * because we want to be confident before sending a "send eoi" auxilary
290 * command that we will be able to also put the associated data byte
291 * in the chip before any potential timeout.
292 */
source_handshake_is_sgns(struct fluke_priv * e_priv)293 static int source_handshake_is_sgns(struct fluke_priv *e_priv)
294 {
295 int i;
296
297 for (i = 0; i < 2; ++i) {
298 if ((fluke_paged_read_byte(e_priv, STATE1_REG, STATE1_PAGE) &
299 SOURCE_HANDSHAKE_MASK) != SOURCE_HANDSHAKE_SGNS_BITS) {
300 return 0;
301 }
302 }
303 return 1;
304 }
305
source_handshake_is_sids_or_sgns(struct fluke_priv * e_priv)306 static int source_handshake_is_sids_or_sgns(struct fluke_priv *e_priv)
307 {
308 unsigned int source_handshake_bits;
309
310 source_handshake_bits = fluke_paged_read_byte(e_priv, STATE1_REG, STATE1_PAGE) &
311 SOURCE_HANDSHAKE_MASK;
312
313 return (source_handshake_bits == SOURCE_HANDSHAKE_SGNS_BITS) ||
314 (source_handshake_bits == SOURCE_HANDSHAKE_SIDS_BITS);
315 }
316
317 /*
318 * Wait until the gpib chip is ready to accept a data out byte.
319 * If the chip is SGNS it is probably waiting for a a byte to
320 * be written to it.
321 */
wait_for_data_out_ready(struct gpib_board * board)322 static int wait_for_data_out_ready(struct gpib_board *board)
323 {
324 struct fluke_priv *e_priv = board->private_data;
325 struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
326 int retval = 0;
327
328 if (wait_event_interruptible(board->wait,
329 (test_bit(TACS_NUM, &board->status) &&
330 source_handshake_is_sgns(e_priv)) ||
331 test_bit(DEV_CLEAR_BN, &nec_priv->state) ||
332 test_bit(TIMO_NUM, &board->status)))
333 retval = -ERESTARTSYS;
334 if (test_bit(TIMO_NUM, &board->status))
335 retval = -ETIMEDOUT;
336 if (test_and_clear_bit(DEV_CLEAR_BN, &nec_priv->state))
337 retval = -EINTR;
338 return retval;
339 }
340
wait_for_sids_or_sgns(struct gpib_board * board)341 static int wait_for_sids_or_sgns(struct gpib_board *board)
342 {
343 struct fluke_priv *e_priv = board->private_data;
344 struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
345 int retval = 0;
346
347 if (wait_event_interruptible(board->wait,
348 source_handshake_is_sids_or_sgns(e_priv) ||
349 test_bit(DEV_CLEAR_BN, &nec_priv->state) ||
350 test_bit(TIMO_NUM, &board->status)))
351 retval = -ERESTARTSYS;
352
353 if (test_bit(TIMO_NUM, &board->status))
354 retval = -ETIMEDOUT;
355 if (test_and_clear_bit(DEV_CLEAR_BN, &nec_priv->state))
356 retval = -EINTR;
357 return retval;
358 }
359
fluke_dma_callback(void * arg)360 static void fluke_dma_callback(void *arg)
361 {
362 struct gpib_board *board = arg;
363 struct fluke_priv *e_priv = board->private_data;
364 struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
365 unsigned long flags;
366
367 spin_lock_irqsave(&board->spinlock, flags);
368
369 nec7210_set_reg_bits(nec_priv, IMR1, HR_DOIE | HR_DIIE, HR_DOIE | HR_DIIE);
370 wake_up_interruptible(&board->wait);
371
372 fluke_gpib_internal_interrupt(board);
373 clear_bit(DMA_WRITE_IN_PROGRESS_BN, &nec_priv->state);
374 clear_bit(DMA_READ_IN_PROGRESS_BN, &nec_priv->state);
375
376 spin_unlock_irqrestore(&board->spinlock, flags);
377 }
378
fluke_dma_write(struct gpib_board * board,u8 * buffer,size_t length,size_t * bytes_written)379 static int fluke_dma_write(struct gpib_board *board, u8 *buffer, size_t length,
380 size_t *bytes_written)
381 {
382 struct fluke_priv *e_priv = board->private_data;
383 struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
384 unsigned long flags;
385 int retval = 0;
386 dma_addr_t address;
387 struct dma_async_tx_descriptor *tx_desc;
388
389 *bytes_written = 0;
390
391 if (WARN_ON_ONCE(length > e_priv->dma_buffer_size))
392 return -EFAULT;
393 dmaengine_terminate_all(e_priv->dma_channel);
394 // write-clear counter
395 writel(0x0, e_priv->write_transfer_counter);
396
397 memcpy(e_priv->dma_buffer, buffer, length);
398 address = dma_map_single(board->dev, e_priv->dma_buffer,
399 length, DMA_TO_DEVICE);
400 /* program dma controller */
401 retval = fluke_config_dma(board, 1);
402 if (retval)
403 goto cleanup;
404
405 tx_desc = dmaengine_prep_slave_single(e_priv->dma_channel, address, length, DMA_MEM_TO_DEV,
406 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
407 if (!tx_desc) {
408 dev_err(board->gpib_dev, "failed to allocate dma transmit descriptor\n");
409 retval = -ENOMEM;
410 goto cleanup;
411 }
412 tx_desc->callback = fluke_dma_callback;
413 tx_desc->callback_param = board;
414
415 spin_lock_irqsave(&board->spinlock, flags);
416 nec7210_set_reg_bits(nec_priv, IMR1, HR_DOIE, 0);
417 nec7210_set_reg_bits(nec_priv, IMR2, HR_DMAO, HR_DMAO);
418 dmaengine_submit(tx_desc);
419 dma_async_issue_pending(e_priv->dma_channel);
420
421 clear_bit(WRITE_READY_BN, &nec_priv->state);
422 set_bit(DMA_WRITE_IN_PROGRESS_BN, &nec_priv->state);
423
424 spin_unlock_irqrestore(&board->spinlock, flags);
425
426 // suspend until message is sent
427 if (wait_event_interruptible(board->wait,
428 ((readl(e_priv->write_transfer_counter) &
429 write_transfer_counter_mask) == length) ||
430 test_bit(BUS_ERROR_BN, &nec_priv->state) ||
431 test_bit(DEV_CLEAR_BN, &nec_priv->state) ||
432 test_bit(TIMO_NUM, &board->status))) {
433 retval = -ERESTARTSYS;
434 }
435 if (test_bit(TIMO_NUM, &board->status))
436 retval = -ETIMEDOUT;
437 if (test_and_clear_bit(DEV_CLEAR_BN, &nec_priv->state))
438 retval = -EINTR;
439 if (test_and_clear_bit(BUS_ERROR_BN, &nec_priv->state))
440 retval = -EIO;
441 // disable board's dma
442 nec7210_set_reg_bits(nec_priv, IMR2, HR_DMAO, 0);
443
444 dmaengine_terminate_all(e_priv->dma_channel);
445 // make sure fluke_dma_callback got called
446 if (test_bit(DMA_WRITE_IN_PROGRESS_BN, &nec_priv->state))
447 fluke_dma_callback(board);
448
449 /*
450 * if everything went fine, try to wait until last byte is actually
451 * transmitted across gpib (but don't try _too_ hard)
452 */
453 if (retval == 0)
454 retval = wait_for_sids_or_sgns(board);
455
456 *bytes_written = readl(e_priv->write_transfer_counter) & write_transfer_counter_mask;
457 if (WARN_ON_ONCE(*bytes_written > length))
458 return -EFAULT;
459
460 cleanup:
461 dma_unmap_single(board->dev, address, length, DMA_TO_DEVICE);
462 return retval;
463 }
464
fluke_accel_write(struct gpib_board * board,u8 * buffer,size_t length,int send_eoi,size_t * bytes_written)465 static int fluke_accel_write(struct gpib_board *board, u8 *buffer, size_t length,
466 int send_eoi, size_t *bytes_written)
467 {
468 struct fluke_priv *e_priv = board->private_data;
469 struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
470 size_t remainder = length;
471 size_t transfer_size;
472 ssize_t retval = 0;
473 size_t dma_remainder = remainder;
474
475 if (!e_priv->dma_channel) {
476 dev_err(board->gpib_dev, "No dma channel available, cannot do accel write.");
477 return -ENXIO;
478 }
479
480 *bytes_written = 0;
481 if (length < 1)
482 return 0;
483
484 clear_bit(DEV_CLEAR_BN, &nec_priv->state); // XXX FIXME
485
486 if (send_eoi)
487 --dma_remainder;
488
489 while (dma_remainder > 0) {
490 size_t num_bytes;
491
492 retval = wait_for_data_out_ready(board);
493 if (retval < 0)
494 break;
495
496 transfer_size = (e_priv->dma_buffer_size < dma_remainder) ?
497 e_priv->dma_buffer_size : dma_remainder;
498 retval = fluke_dma_write(board, buffer, transfer_size, &num_bytes);
499 *bytes_written += num_bytes;
500 if (retval < 0)
501 break;
502 dma_remainder -= num_bytes;
503 remainder -= num_bytes;
504 buffer += num_bytes;
505 if (need_resched())
506 schedule();
507 }
508 if (retval < 0)
509 return retval;
510 // handle sending of last byte with eoi
511 if (send_eoi) {
512 size_t num_bytes;
513
514 if (WARN_ON_ONCE(remainder != 1))
515 return -EFAULT;
516
517 /*
518 * wait until we are sure we will be able to write the data byte
519 * into the chip before we send AUX_SEOI. This prevents a timeout
520 * scenerio where we send AUX_SEOI but then timeout without getting
521 * any bytes into the gpib chip. This will result in the first byte
522 * of the next write having a spurious EOI set on the first byte.
523 */
524 retval = wait_for_data_out_ready(board);
525 if (retval < 0)
526 return retval;
527
528 write_byte(nec_priv, AUX_SEOI, AUXMR);
529 retval = fluke_dma_write(board, buffer, remainder, &num_bytes);
530 *bytes_written += num_bytes;
531 if (retval < 0)
532 return retval;
533 remainder -= num_bytes;
534 }
535 return 0;
536 }
537
fluke_get_dma_residue(struct dma_chan * chan,dma_cookie_t cookie)538 static int fluke_get_dma_residue(struct dma_chan *chan, dma_cookie_t cookie)
539 {
540 struct dma_tx_state state;
541 int result;
542
543 result = dmaengine_pause(chan);
544 if (result < 0) {
545 pr_err("dma pause failed?\n");
546 return result;
547 }
548 dmaengine_tx_status(chan, cookie, &state);
549 /*
550 * hardware doesn't support resume, so dont call this
551 * method unless the dma transfer is done.
552 */
553 return state.residue;
554 }
555
fluke_dma_read(struct gpib_board * board,u8 * buffer,size_t length,int * end,size_t * bytes_read)556 static int fluke_dma_read(struct gpib_board *board, u8 *buffer,
557 size_t length, int *end, size_t *bytes_read)
558 {
559 struct fluke_priv *e_priv = board->private_data;
560 struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
561 int retval = 0;
562 unsigned long flags;
563 int residue;
564 dma_addr_t bus_address;
565 struct dma_async_tx_descriptor *tx_desc;
566 dma_cookie_t dma_cookie;
567 int i;
568 static const int timeout = 10;
569
570 *bytes_read = 0;
571 *end = 0;
572 if (length == 0)
573 return 0;
574
575 bus_address = dma_map_single(board->dev, e_priv->dma_buffer,
576 length, DMA_FROM_DEVICE);
577
578 /* program dma controller */
579 retval = fluke_config_dma(board, 0);
580 if (retval) {
581 dma_unmap_single(board->dev, bus_address, length, DMA_FROM_DEVICE);
582 return retval;
583 }
584 tx_desc = dmaengine_prep_slave_single(e_priv->dma_channel,
585 bus_address, length, DMA_DEV_TO_MEM,
586 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
587 if (!tx_desc) {
588 dev_err(board->gpib_dev, "failed to allocate dma transmit descriptor\n");
589 dma_unmap_single(NULL, bus_address, length, DMA_FROM_DEVICE);
590 return -EIO;
591 }
592 tx_desc->callback = fluke_dma_callback;
593 tx_desc->callback_param = board;
594
595 spin_lock_irqsave(&board->spinlock, flags);
596 // enable nec7210 dma
597 nec7210_set_reg_bits(nec_priv, IMR1, HR_DIIE, 0);
598 nec7210_set_reg_bits(nec_priv, IMR2, HR_DMAI, HR_DMAI);
599
600 dma_cookie = dmaengine_submit(tx_desc);
601 dma_async_issue_pending(e_priv->dma_channel);
602
603 set_bit(DMA_READ_IN_PROGRESS_BN, &nec_priv->state);
604 clear_bit(READ_READY_BN, &nec_priv->state);
605
606 spin_unlock_irqrestore(&board->spinlock, flags);
607 // wait for data to transfer
608 if (wait_event_interruptible(board->wait,
609 test_bit(DMA_READ_IN_PROGRESS_BN, &nec_priv->state) == 0 ||
610 test_bit(RECEIVED_END_BN, &nec_priv->state) ||
611 test_bit(DEV_CLEAR_BN, &nec_priv->state) ||
612 test_bit(TIMO_NUM, &board->status))) {
613 retval = -ERESTARTSYS;
614 }
615 if (test_bit(TIMO_NUM, &board->status))
616 retval = -ETIMEDOUT;
617 if (test_bit(DEV_CLEAR_BN, &nec_priv->state))
618 retval = -EINTR;
619
620 /*
621 * If we woke up because of end, wait until the dma transfer has pulled
622 * the data byte associated with the end before we cancel the dma transfer.
623 */
624 if (test_bit(RECEIVED_END_BN, &nec_priv->state)) {
625 for (i = 0; i < timeout; ++i) {
626 if (test_bit(DMA_READ_IN_PROGRESS_BN, &nec_priv->state) == 0)
627 break;
628 if ((read_byte(nec_priv, ADR0) & DATA_IN_STATUS) == 0)
629 break;
630 usleep_range(10, 15);
631 }
632 if (i == timeout)
633 pr_warn("fluke_gpib: timeout waiting for dma to transfer end data byte.\n");
634 }
635
636 // stop the dma transfer
637 nec7210_set_reg_bits(nec_priv, IMR2, HR_DMAI, 0);
638 /*
639 * delay a little just to make sure any bytes in dma controller's fifo get
640 * written to memory before we disable it
641 */
642 usleep_range(10, 15);
643 residue = fluke_get_dma_residue(e_priv->dma_channel, dma_cookie);
644 if (WARN_ON_ONCE(residue > length || residue < 0))
645 return -EFAULT;
646 *bytes_read += length - residue;
647 dmaengine_terminate_all(e_priv->dma_channel);
648 // make sure fluke_dma_callback got called
649 if (test_bit(DMA_READ_IN_PROGRESS_BN, &nec_priv->state))
650 fluke_dma_callback(board);
651
652 dma_unmap_single(board->dev, bus_address, length, DMA_FROM_DEVICE);
653 memcpy(buffer, e_priv->dma_buffer, *bytes_read);
654
655 /*
656 * If we got an end interrupt, figure out if it was
657 * associated with the last byte we dma'd or with a
658 * byte still sitting on the cb7210.
659 */
660 spin_lock_irqsave(&board->spinlock, flags);
661 if (test_bit(READ_READY_BN, &nec_priv->state) == 0) {
662 /*
663 * There is no byte sitting on the cb7210. If we
664 * saw an end interrupt, we need to deal with it now
665 */
666 if (test_and_clear_bit(RECEIVED_END_BN, &nec_priv->state))
667 *end = 1;
668 }
669 spin_unlock_irqrestore(&board->spinlock, flags);
670
671 return retval;
672 }
673
fluke_accel_read(struct gpib_board * board,u8 * buffer,size_t length,int * end,size_t * bytes_read)674 static int fluke_accel_read(struct gpib_board *board, u8 *buffer, size_t length,
675 int *end, size_t *bytes_read)
676 {
677 struct fluke_priv *e_priv = board->private_data;
678 struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
679 size_t remain = length;
680 size_t transfer_size;
681 int retval = 0;
682 size_t dma_nbytes;
683
684 *end = 0;
685 *bytes_read = 0;
686
687 smp_mb__before_atomic();
688 clear_bit(DEV_CLEAR_BN, &nec_priv->state); // XXX FIXME
689 smp_mb__after_atomic();
690
691 retval = wait_for_read(board);
692 if (retval < 0)
693 return retval;
694
695 nec7210_release_rfd_holdoff(board, nec_priv);
696
697 while (remain > 0) {
698 transfer_size = (e_priv->dma_buffer_size < remain) ?
699 e_priv->dma_buffer_size : remain;
700 retval = fluke_dma_read(board, buffer, transfer_size, end, &dma_nbytes);
701 remain -= dma_nbytes;
702 buffer += dma_nbytes;
703 *bytes_read += dma_nbytes;
704 if (*end)
705 break;
706 if (retval < 0)
707 return retval;
708 if (need_resched())
709 schedule();
710 }
711
712 return retval;
713 }
714
715 static struct gpib_interface fluke_unaccel_interface = {
716 .name = "fluke_unaccel",
717 .attach = fluke_attach_holdoff_all,
718 .detach = fluke_detach,
719 .read = fluke_read,
720 .write = fluke_write,
721 .command = fluke_command,
722 .take_control = fluke_take_control,
723 .go_to_standby = fluke_go_to_standby,
724 .request_system_control = fluke_request_system_control,
725 .interface_clear = fluke_interface_clear,
726 .remote_enable = fluke_remote_enable,
727 .enable_eos = fluke_enable_eos,
728 .disable_eos = fluke_disable_eos,
729 .parallel_poll = fluke_parallel_poll,
730 .parallel_poll_configure = fluke_parallel_poll_configure,
731 .parallel_poll_response = fluke_parallel_poll_response,
732 .line_status = fluke_line_status,
733 .update_status = fluke_update_status,
734 .primary_address = fluke_primary_address,
735 .secondary_address = fluke_secondary_address,
736 .serial_poll_response = fluke_serial_poll_response,
737 .serial_poll_status = fluke_serial_poll_status,
738 .t1_delay = fluke_t1_delay,
739 .return_to_local = fluke_return_to_local,
740 };
741
742 /*
743 * fluke_hybrid uses dma for writes but not for reads. Added
744 * to deal with occasional corruption of bytes seen when doing dma
745 * reads. From looking at the cb7210 vhdl, I believe the corruption
746 * is due to a hardware bug triggered by the cpu reading a cb7210
747 * }
748 * register just as the dma controller is also doing a read.
749 */
750
751 static struct gpib_interface fluke_hybrid_interface = {
752 .name = "fluke_hybrid",
753 .attach = fluke_attach_holdoff_all,
754 .detach = fluke_detach,
755 .read = fluke_read,
756 .write = fluke_accel_write,
757 .command = fluke_command,
758 .take_control = fluke_take_control,
759 .go_to_standby = fluke_go_to_standby,
760 .request_system_control = fluke_request_system_control,
761 .interface_clear = fluke_interface_clear,
762 .remote_enable = fluke_remote_enable,
763 .enable_eos = fluke_enable_eos,
764 .disable_eos = fluke_disable_eos,
765 .parallel_poll = fluke_parallel_poll,
766 .parallel_poll_configure = fluke_parallel_poll_configure,
767 .parallel_poll_response = fluke_parallel_poll_response,
768 .line_status = fluke_line_status,
769 .update_status = fluke_update_status,
770 .primary_address = fluke_primary_address,
771 .secondary_address = fluke_secondary_address,
772 .serial_poll_response = fluke_serial_poll_response,
773 .serial_poll_status = fluke_serial_poll_status,
774 .t1_delay = fluke_t1_delay,
775 .return_to_local = fluke_return_to_local,
776 };
777
778 static struct gpib_interface fluke_interface = {
779 .name = "fluke",
780 .attach = fluke_attach_holdoff_end,
781 .detach = fluke_detach,
782 .read = fluke_accel_read,
783 .write = fluke_accel_write,
784 .command = fluke_command,
785 .take_control = fluke_take_control,
786 .go_to_standby = fluke_go_to_standby,
787 .request_system_control = fluke_request_system_control,
788 .interface_clear = fluke_interface_clear,
789 .remote_enable = fluke_remote_enable,
790 .enable_eos = fluke_enable_eos,
791 .disable_eos = fluke_disable_eos,
792 .parallel_poll = fluke_parallel_poll,
793 .parallel_poll_configure = fluke_parallel_poll_configure,
794 .parallel_poll_response = fluke_parallel_poll_response,
795 .line_status = fluke_line_status,
796 .update_status = fluke_update_status,
797 .primary_address = fluke_primary_address,
798 .secondary_address = fluke_secondary_address,
799 .serial_poll_response = fluke_serial_poll_response,
800 .serial_poll_status = fluke_serial_poll_status,
801 .t1_delay = fluke_t1_delay,
802 .return_to_local = fluke_return_to_local,
803 };
804
fluke_gpib_internal_interrupt(struct gpib_board * board)805 irqreturn_t fluke_gpib_internal_interrupt(struct gpib_board *board)
806 {
807 int status0, status1, status2;
808 struct fluke_priv *priv = board->private_data;
809 struct nec7210_priv *nec_priv = &priv->nec7210_priv;
810 int retval = IRQ_NONE;
811
812 if (read_byte(nec_priv, ADR0) & DATA_IN_STATUS)
813 set_bit(READ_READY_BN, &nec_priv->state);
814
815 status0 = fluke_paged_read_byte(priv, ISR0_IMR0, ISR0_IMR0_PAGE);
816 status1 = read_byte(nec_priv, ISR1);
817 status2 = read_byte(nec_priv, ISR2);
818
819 if (status0 & FLUKE_IFCI_BIT) {
820 push_gpib_event(board, EVENT_IFC);
821 retval = IRQ_HANDLED;
822 }
823
824 if (nec7210_interrupt_have_status(board, nec_priv, status1, status2) == IRQ_HANDLED)
825 retval = IRQ_HANDLED;
826
827 if (read_byte(nec_priv, ADR0) & DATA_IN_STATUS) {
828 if (test_bit(RFD_HOLDOFF_BN, &nec_priv->state))
829 set_bit(READ_READY_BN, &nec_priv->state);
830 else
831 clear_bit(READ_READY_BN, &nec_priv->state);
832 }
833
834 if (retval == IRQ_HANDLED)
835 wake_up_interruptible(&board->wait);
836
837 return retval;
838 }
839
fluke_gpib_interrupt(int irq,void * arg)840 static irqreturn_t fluke_gpib_interrupt(int irq, void *arg)
841 {
842 struct gpib_board *board = arg;
843 unsigned long flags;
844 irqreturn_t retval;
845
846 spin_lock_irqsave(&board->spinlock, flags);
847 retval = fluke_gpib_internal_interrupt(board);
848 spin_unlock_irqrestore(&board->spinlock, flags);
849 return retval;
850 }
851
fluke_allocate_private(struct gpib_board * board)852 static int fluke_allocate_private(struct gpib_board *board)
853 {
854 struct fluke_priv *priv;
855
856 board->private_data = kzalloc_obj(struct fluke_priv);
857 if (!board->private_data)
858 return -ENOMEM;
859 priv = board->private_data;
860 init_nec7210_private(&priv->nec7210_priv);
861 priv->dma_buffer_size = 0x7ff;
862 priv->dma_buffer = kmalloc(priv->dma_buffer_size, GFP_KERNEL);
863 if (!priv->dma_buffer)
864 return -ENOMEM;
865 return 0;
866 }
867
fluke_generic_detach(struct gpib_board * board)868 static void fluke_generic_detach(struct gpib_board *board)
869 {
870 if (board->private_data) {
871 struct fluke_priv *e_priv = board->private_data;
872
873 kfree(e_priv->dma_buffer);
874 kfree(board->private_data);
875 board->private_data = NULL;
876 }
877 }
878
879 // generic part of attach functions shared by all cb7210 boards
fluke_generic_attach(struct gpib_board * board)880 static int fluke_generic_attach(struct gpib_board *board)
881 {
882 struct fluke_priv *e_priv;
883 struct nec7210_priv *nec_priv;
884 int retval;
885
886 board->status = 0;
887
888 retval = fluke_allocate_private(board);
889 if (retval)
890 return retval;
891 e_priv = board->private_data;
892 nec_priv = &e_priv->nec7210_priv;
893 nec_priv->read_byte = fluke_locking_read_byte;
894 nec_priv->write_byte = fluke_locking_write_byte;
895 nec_priv->offset = fluke_reg_offset;
896 nec_priv->type = CB7210;
897 return 0;
898 }
899
fluke_config_dma(struct gpib_board * board,int output)900 static int fluke_config_dma(struct gpib_board *board, int output)
901 {
902 struct fluke_priv *e_priv = board->private_data;
903 struct dma_slave_config config;
904
905 config.src_maxburst = 1;
906 config.dst_maxburst = 1;
907 config.device_fc = true;
908
909 if (output) {
910 config.direction = DMA_MEM_TO_DEV;
911 config.src_addr = 0;
912 config.dst_addr = e_priv->dma_port_res->start;
913 config.src_addr_width = 1;
914 config.dst_addr_width = 1;
915 } else {
916 config.direction = DMA_DEV_TO_MEM;
917 config.src_addr = e_priv->dma_port_res->start;
918 config.dst_addr = 0;
919 config.src_addr_width = 1;
920 config.dst_addr_width = 1;
921 }
922 return dmaengine_slave_config(e_priv->dma_channel, &config);
923 }
924
fluke_init(struct fluke_priv * e_priv,struct gpib_board * board,int handshake_mode)925 static int fluke_init(struct fluke_priv *e_priv, struct gpib_board *board, int handshake_mode)
926 {
927 struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
928
929 nec7210_board_reset(nec_priv, board);
930 write_byte(nec_priv, AUX_LO_SPEED, AUXMR);
931 /*
932 * set clock register for driving frequency
933 * ICR should be set to clock in megahertz (1-15) and to zero
934 * for clocks faster than 15 MHz (max 20MHz)
935 */
936 write_byte(nec_priv, ICR | 10, AUXMR);
937 nec7210_set_handshake_mode(board, nec_priv, handshake_mode);
938
939 nec7210_board_online(nec_priv, board);
940
941 /* poll so we can detect ATN changes */
942 if (gpib_request_pseudo_irq(board, fluke_gpib_interrupt)) {
943 dev_err(board->gpib_dev, "failed to allocate pseudo_irq\n");
944 return -EINVAL;
945 }
946
947 fluke_paged_write_byte(e_priv, FLUKE_IFCIE_BIT, ISR0_IMR0, ISR0_IMR0_PAGE);
948 return 0;
949 }
950
951 /*
952 * This function is passed to dma_request_channel() in order to
953 * select the pl330 dma channel which has been hardwired to
954 * the gpib controller.
955 */
gpib_dma_channel_filter(struct dma_chan * chan,void * filter_param)956 static bool gpib_dma_channel_filter(struct dma_chan *chan, void *filter_param)
957 {
958 // select the channel which is wired to the gpib chip
959 return chan->chan_id == 0;
960 }
961
fluke_attach_impl(struct gpib_board * board,const struct gpib_board_config * config,unsigned int handshake_mode)962 static int fluke_attach_impl(struct gpib_board *board, const struct gpib_board_config *config,
963 unsigned int handshake_mode)
964 {
965 struct fluke_priv *e_priv;
966 struct nec7210_priv *nec_priv;
967 int isr_flags = 0;
968 int retval;
969 int irq;
970 struct resource *res;
971 dma_cap_mask_t dma_cap;
972
973 if (!fluke_gpib_pdev) {
974 dev_err(board->gpib_dev, "No fluke device was found, attach failed.\n");
975 return -ENODEV;
976 }
977
978 retval = fluke_generic_attach(board);
979 if (retval)
980 return retval;
981
982 e_priv = board->private_data;
983 nec_priv = &e_priv->nec7210_priv;
984 nec_priv->offset = fluke_reg_offset;
985 board->dev = &fluke_gpib_pdev->dev;
986
987 res = platform_get_resource(fluke_gpib_pdev, IORESOURCE_MEM, 0);
988 if (!res) {
989 dev_err(&fluke_gpib_pdev->dev, "Unable to locate mmio resource\n");
990 return -ENODEV;
991 }
992
993 if (request_mem_region(res->start,
994 resource_size(res),
995 fluke_gpib_pdev->name) == NULL) {
996 dev_err(&fluke_gpib_pdev->dev, "cannot claim registers\n");
997 return -ENXIO;
998 }
999 e_priv->gpib_iomem_res = res;
1000
1001 nec_priv->mmiobase = ioremap(e_priv->gpib_iomem_res->start,
1002 resource_size(e_priv->gpib_iomem_res));
1003 if (!nec_priv->mmiobase) {
1004 dev_err(&fluke_gpib_pdev->dev, "Could not map I/O memory\n");
1005 return -ENOMEM;
1006 }
1007
1008 res = platform_get_resource(fluke_gpib_pdev, IORESOURCE_MEM, 1);
1009 if (!res) {
1010 dev_err(&fluke_gpib_pdev->dev, "Unable to locate mmio resource for gpib dma port\n");
1011 return -ENODEV;
1012 }
1013 if (request_mem_region(res->start,
1014 resource_size(res),
1015 fluke_gpib_pdev->name) == NULL) {
1016 dev_err(&fluke_gpib_pdev->dev, "cannot claim registers\n");
1017 return -ENXIO;
1018 }
1019 e_priv->dma_port_res = res;
1020
1021 res = platform_get_resource(fluke_gpib_pdev, IORESOURCE_MEM, 2);
1022 if (!res) {
1023 dev_err(&fluke_gpib_pdev->dev, "Unable to locate mmio resource for write transfer counter\n");
1024 return -ENODEV;
1025 }
1026
1027 if (request_mem_region(res->start,
1028 resource_size(res),
1029 fluke_gpib_pdev->name) == NULL) {
1030 dev_err(&fluke_gpib_pdev->dev, "cannot claim registers\n");
1031 return -ENXIO;
1032 }
1033 e_priv->write_transfer_counter_res = res;
1034
1035 e_priv->write_transfer_counter = ioremap(e_priv->write_transfer_counter_res->start,
1036 resource_size(e_priv->write_transfer_counter_res));
1037 if (!e_priv->write_transfer_counter) {
1038 dev_err(&fluke_gpib_pdev->dev, "Could not map I/O memory\n");
1039 return -ENOMEM;
1040 }
1041
1042 irq = platform_get_irq(fluke_gpib_pdev, 0);
1043 if (irq < 0)
1044 return -EBUSY;
1045 retval = request_irq(irq, fluke_gpib_interrupt, isr_flags, fluke_gpib_pdev->name, board);
1046 if (retval) {
1047 dev_err(&fluke_gpib_pdev->dev,
1048 "cannot register interrupt handler err=%d\n",
1049 retval);
1050 return retval;
1051 }
1052 e_priv->irq = irq;
1053
1054 dma_cap_zero(dma_cap);
1055 dma_cap_set(DMA_SLAVE, dma_cap);
1056 e_priv->dma_channel = dma_request_channel(dma_cap, gpib_dma_channel_filter, NULL);
1057 if (!e_priv->dma_channel) {
1058 dev_err(board->gpib_dev, "failed to allocate a dma channel.\n");
1059 /*
1060 * we don't error out here because unaccel interface will still
1061 * work without dma
1062 */
1063 }
1064
1065 return fluke_init(e_priv, board, handshake_mode);
1066 }
1067
fluke_attach_holdoff_all(struct gpib_board * board,const struct gpib_board_config * config)1068 int fluke_attach_holdoff_all(struct gpib_board *board, const struct gpib_board_config *config)
1069 {
1070 return fluke_attach_impl(board, config, HR_HLDA);
1071 }
1072
fluke_attach_holdoff_end(struct gpib_board * board,const struct gpib_board_config * config)1073 int fluke_attach_holdoff_end(struct gpib_board *board, const struct gpib_board_config *config)
1074 {
1075 return fluke_attach_impl(board, config, HR_HLDE);
1076 }
1077
fluke_detach(struct gpib_board * board)1078 void fluke_detach(struct gpib_board *board)
1079 {
1080 struct fluke_priv *e_priv = board->private_data;
1081 struct nec7210_priv *nec_priv;
1082
1083 if (e_priv) {
1084 if (e_priv->dma_channel)
1085 dma_release_channel(e_priv->dma_channel);
1086 gpib_free_pseudo_irq(board);
1087 nec_priv = &e_priv->nec7210_priv;
1088
1089 if (nec_priv->mmiobase) {
1090 fluke_paged_write_byte(e_priv, 0, ISR0_IMR0, ISR0_IMR0_PAGE);
1091 nec7210_board_reset(nec_priv, board);
1092 }
1093 if (e_priv->irq)
1094 free_irq(e_priv->irq, board);
1095 if (e_priv->write_transfer_counter_res) {
1096 release_mem_region(e_priv->write_transfer_counter_res->start,
1097 resource_size(e_priv->write_transfer_counter_res));
1098 }
1099 if (e_priv->dma_port_res) {
1100 release_mem_region(e_priv->dma_port_res->start,
1101 resource_size(e_priv->dma_port_res));
1102 }
1103 if (e_priv->gpib_iomem_res)
1104 release_mem_region(e_priv->gpib_iomem_res->start,
1105 resource_size(e_priv->gpib_iomem_res));
1106 }
1107 fluke_generic_detach(board);
1108 }
1109
fluke_gpib_probe(struct platform_device * pdev)1110 static int fluke_gpib_probe(struct platform_device *pdev)
1111 {
1112 fluke_gpib_pdev = pdev;
1113 return 0;
1114 }
1115
1116 static const struct of_device_id fluke_gpib_of_match[] = {
1117 { .compatible = "flk,fgpib-4.0"},
1118 { {0} }
1119 };
1120 MODULE_DEVICE_TABLE(of, fluke_gpib_of_match);
1121
1122 static struct platform_driver fluke_gpib_platform_driver = {
1123 .driver = {
1124 .name = DRV_NAME,
1125 .of_match_table = fluke_gpib_of_match,
1126 },
1127 .probe = &fluke_gpib_probe
1128 };
1129
fluke_init_module(void)1130 static int __init fluke_init_module(void)
1131 {
1132 int result;
1133
1134 result = platform_driver_register(&fluke_gpib_platform_driver);
1135 if (result) {
1136 pr_err("platform_driver_register failed: error = %d\n", result);
1137 return result;
1138 }
1139
1140 result = gpib_register_driver(&fluke_unaccel_interface, THIS_MODULE);
1141 if (result) {
1142 pr_err("gpib_register_driver failed: error = %d\n", result);
1143 goto err_unaccel;
1144 }
1145
1146 result = gpib_register_driver(&fluke_hybrid_interface, THIS_MODULE);
1147 if (result) {
1148 pr_err("gpib_register_driver failed: error = %d\n", result);
1149 goto err_hybrid;
1150 }
1151
1152 result = gpib_register_driver(&fluke_interface, THIS_MODULE);
1153 if (result) {
1154 pr_err("gpib_register_driver failed: error = %d\n", result);
1155 goto err_interface;
1156 }
1157
1158 return 0;
1159
1160 err_interface:
1161 gpib_unregister_driver(&fluke_hybrid_interface);
1162 err_hybrid:
1163 gpib_unregister_driver(&fluke_unaccel_interface);
1164 err_unaccel:
1165 platform_driver_unregister(&fluke_gpib_platform_driver);
1166
1167 return result;
1168 }
1169
fluke_exit_module(void)1170 static void __exit fluke_exit_module(void)
1171 {
1172 gpib_unregister_driver(&fluke_unaccel_interface);
1173 gpib_unregister_driver(&fluke_hybrid_interface);
1174 gpib_unregister_driver(&fluke_interface);
1175 platform_driver_unregister(&fluke_gpib_platform_driver);
1176 }
1177
1178 module_init(fluke_init_module);
1179 module_exit(fluke_exit_module);
1180