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