File Index
Files and directories
Sign in
FILE

fluke_gpib.c

File preview with syntax highlighting.

linux-6.19.14/drivers/gpib/eastwood/fluke_gpib.c C
   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
  24MODULE_LICENSE("GPL");
  25MODULE_DESCRIPTION("GPIB Driver for Fluke cda devices");
  26
  27static int fluke_attach_holdoff_all(struct gpib_board *board,
  28				    const struct gpib_board_config *config);
  29static int fluke_attach_holdoff_end(struct gpib_board *board,
  30				    const struct gpib_board_config *config);
  31static void fluke_detach(struct gpib_board *board);
  32static int fluke_config_dma(struct gpib_board *board, int output);
  33static irqreturn_t fluke_gpib_internal_interrupt(struct gpib_board *board);
  34
  35static struct platform_device *fluke_gpib_pdev;
  36
  37static 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
  48static 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
  59static 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
  67static 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
  75static 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
  83static 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
  90static 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
  97static 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
 105static 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
 112static 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
 119static 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
 126static 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
 133static 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
 140static 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
 147static 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
 154static 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
 161static 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
 168static 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
 175static 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
 182static 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
 189static 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
 199static 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
 229static 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
 246static 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 */
 263static 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 */
 293static 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
 306static 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 */
 322static 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
 341static 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
 360static 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
 379static 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
 460cleanup:
 461	dma_unmap_single(board->dev, address, length, DMA_TO_DEVICE);
 462	return retval;
 463}
 464
 465static 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
 538static 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
 556static 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
 674static 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
 715static 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
 751static 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
 778static 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
 805irqreturn_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
 840static 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
 852static int fluke_allocate_private(struct gpib_board *board)
 853{
 854	struct fluke_priv *priv;
 855
 856	board->private_data = kmalloc(sizeof(struct fluke_priv), GFP_KERNEL);
 857	if (!board->private_data)
 858		return -ENOMEM;
 859	priv = board->private_data;
 860	memset(priv, 0, sizeof(struct fluke_priv));
 861	init_nec7210_private(&priv->nec7210_priv);
 862	priv->dma_buffer_size = 0x7ff;
 863	priv->dma_buffer = kmalloc(priv->dma_buffer_size, GFP_KERNEL);
 864	if (!priv->dma_buffer)
 865		return -ENOMEM;
 866	return 0;
 867}
 868
 869static void fluke_generic_detach(struct gpib_board *board)
 870{
 871	if (board->private_data) {
 872		struct fluke_priv *e_priv = board->private_data;
 873
 874		kfree(e_priv->dma_buffer);
 875		kfree(board->private_data);
 876		board->private_data = NULL;
 877	}
 878}
 879
 880// generic part of attach functions shared by all cb7210 boards
 881static int fluke_generic_attach(struct gpib_board *board)
 882{
 883	struct fluke_priv *e_priv;
 884	struct nec7210_priv *nec_priv;
 885	int retval;
 886
 887	board->status = 0;
 888
 889	retval = fluke_allocate_private(board);
 890	if (retval < 0)
 891		return retval;
 892	e_priv = board->private_data;
 893	nec_priv = &e_priv->nec7210_priv;
 894	nec_priv->read_byte = fluke_locking_read_byte;
 895	nec_priv->write_byte = fluke_locking_write_byte;
 896	nec_priv->offset = fluke_reg_offset;
 897	nec_priv->type = CB7210;
 898	return 0;
 899}
 900
 901static int fluke_config_dma(struct gpib_board *board, int output)
 902{
 903	struct fluke_priv *e_priv = board->private_data;
 904	struct dma_slave_config config;
 905
 906	config.src_maxburst = 1;
 907	config.dst_maxburst = 1;
 908	config.device_fc = true;
 909
 910	if (output) {
 911		config.direction = DMA_MEM_TO_DEV;
 912		config.src_addr = 0;
 913		config.dst_addr = e_priv->dma_port_res->start;
 914		config.src_addr_width = 1;
 915		config.dst_addr_width = 1;
 916	} else {
 917		config.direction = DMA_DEV_TO_MEM;
 918		config.src_addr = e_priv->dma_port_res->start;
 919		config.dst_addr = 0;
 920		config.src_addr_width = 1;
 921		config.dst_addr_width = 1;
 922	}
 923	return dmaengine_slave_config(e_priv->dma_channel, &config);
 924}
 925
 926static int fluke_init(struct fluke_priv *e_priv, struct gpib_board *board, int handshake_mode)
 927{
 928	struct nec7210_priv *nec_priv = &e_priv->nec7210_priv;
 929
 930	nec7210_board_reset(nec_priv, board);
 931	write_byte(nec_priv, AUX_LO_SPEED, AUXMR);
 932	/*
 933	 * set clock register for driving frequency
 934	 * ICR should be set to clock in megahertz (1-15) and to zero
 935	 * for clocks faster than 15 MHz (max 20MHz)
 936	 */
 937	write_byte(nec_priv, ICR | 10, AUXMR);
 938	nec7210_set_handshake_mode(board, nec_priv, handshake_mode);
 939
 940	nec7210_board_online(nec_priv, board);
 941
 942	/* poll so we can detect ATN changes */
 943	if (gpib_request_pseudo_irq(board, fluke_gpib_interrupt)) {
 944		dev_err(board->gpib_dev, "failed to allocate pseudo_irq\n");
 945		return -EINVAL;
 946	}
 947
 948	fluke_paged_write_byte(e_priv, FLUKE_IFCIE_BIT, ISR0_IMR0, ISR0_IMR0_PAGE);
 949	return 0;
 950}
 951
 952/*
 953 * This function is passed to dma_request_channel() in order to
 954 * select the pl330 dma channel which has been hardwired to
 955 * the gpib controller.
 956 */
 957static bool gpib_dma_channel_filter(struct dma_chan *chan, void *filter_param)
 958{
 959	// select the channel which is wired to the gpib chip
 960	return chan->chan_id == 0;
 961}
 962
 963static int fluke_attach_impl(struct gpib_board *board, const struct gpib_board_config *config,
 964			     unsigned int handshake_mode)
 965{
 966	struct fluke_priv *e_priv;
 967	struct nec7210_priv *nec_priv;
 968	int isr_flags = 0;
 969	int retval;
 970	int irq;
 971	struct resource *res;
 972	dma_cap_mask_t dma_cap;
 973
 974	if (!fluke_gpib_pdev) {
 975		dev_err(board->gpib_dev, "No fluke device was found, attach failed.\n");
 976		return -ENODEV;
 977	}
 978
 979	retval = fluke_generic_attach(board);
 980	if (retval)
 981		return retval;
 982
 983	e_priv = board->private_data;
 984	nec_priv = &e_priv->nec7210_priv;
 985	nec_priv->offset = fluke_reg_offset;
 986	board->dev = &fluke_gpib_pdev->dev;
 987
 988	res = platform_get_resource(fluke_gpib_pdev, IORESOURCE_MEM, 0);
 989	if (!res) {
 990		dev_err(&fluke_gpib_pdev->dev, "Unable to locate mmio resource\n");
 991		return -ENODEV;
 992	}
 993
 994	if (request_mem_region(res->start,
 995			       resource_size(res),
 996			       fluke_gpib_pdev->name) == NULL) {
 997		dev_err(&fluke_gpib_pdev->dev, "cannot claim registers\n");
 998		return -ENXIO;
 999	}
1000	e_priv->gpib_iomem_res = res;
1001
1002	nec_priv->mmiobase = ioremap(e_priv->gpib_iomem_res->start,
1003				     resource_size(e_priv->gpib_iomem_res));
1004	if (!nec_priv->mmiobase) {
1005		dev_err(&fluke_gpib_pdev->dev, "Could not map I/O memory\n");
1006		return -ENOMEM;
1007	}
1008
1009	res = platform_get_resource(fluke_gpib_pdev, IORESOURCE_MEM, 1);
1010	if (!res) {
1011		dev_err(&fluke_gpib_pdev->dev, "Unable to locate mmio resource for gpib dma port\n");
1012		return -ENODEV;
1013	}
1014	if (request_mem_region(res->start,
1015			       resource_size(res),
1016			       fluke_gpib_pdev->name) == NULL) {
1017		dev_err(&fluke_gpib_pdev->dev, "cannot claim registers\n");
1018		return -ENXIO;
1019	}
1020	e_priv->dma_port_res = res;
1021
1022	res = platform_get_resource(fluke_gpib_pdev, IORESOURCE_MEM, 2);
1023	if (!res) {
1024		dev_err(&fluke_gpib_pdev->dev, "Unable to locate mmio resource for write transfer counter\n");
1025		return -ENODEV;
1026	}
1027
1028	if (request_mem_region(res->start,
1029			       resource_size(res),
1030			       fluke_gpib_pdev->name) == NULL) {
1031		dev_err(&fluke_gpib_pdev->dev, "cannot claim registers\n");
1032		return -ENXIO;
1033	}
1034	e_priv->write_transfer_counter_res = res;
1035
1036	e_priv->write_transfer_counter = ioremap(e_priv->write_transfer_counter_res->start,
1037						 resource_size(e_priv->write_transfer_counter_res));
1038	if (!e_priv->write_transfer_counter) {
1039		dev_err(&fluke_gpib_pdev->dev, "Could not map I/O memory\n");
1040		return -ENOMEM;
1041	}
1042
1043	irq = platform_get_irq(fluke_gpib_pdev, 0);
1044	if (irq < 0)
1045		return -EBUSY;
1046	retval = request_irq(irq, fluke_gpib_interrupt, isr_flags, fluke_gpib_pdev->name, board);
1047	if (retval) {
1048		dev_err(&fluke_gpib_pdev->dev,
1049			"cannot register interrupt handler err=%d\n",
1050			retval);
1051		return retval;
1052	}
1053	e_priv->irq = irq;
1054
1055	dma_cap_zero(dma_cap);
1056	dma_cap_set(DMA_SLAVE, dma_cap);
1057	e_priv->dma_channel = dma_request_channel(dma_cap, gpib_dma_channel_filter, NULL);
1058	if (!e_priv->dma_channel) {
1059		dev_err(board->gpib_dev, "failed to allocate a dma channel.\n");
1060		/*
1061		 * we don't error out here because unaccel interface will still
1062		 * work without dma
1063		 */
1064	}
1065
1066	return fluke_init(e_priv, board, handshake_mode);
1067}
1068
1069int fluke_attach_holdoff_all(struct gpib_board *board, const struct gpib_board_config *config)
1070{
1071	return fluke_attach_impl(board, config, HR_HLDA);
1072}
1073
1074int fluke_attach_holdoff_end(struct gpib_board *board, const struct gpib_board_config *config)
1075{
1076	return fluke_attach_impl(board, config, HR_HLDE);
1077}
1078
1079void fluke_detach(struct gpib_board *board)
1080{
1081	struct fluke_priv *e_priv = board->private_data;
1082	struct nec7210_priv *nec_priv;
1083
1084	if (e_priv) {
1085		if (e_priv->dma_channel)
1086			dma_release_channel(e_priv->dma_channel);
1087		gpib_free_pseudo_irq(board);
1088		nec_priv = &e_priv->nec7210_priv;
1089
1090		if (nec_priv->mmiobase) {
1091			fluke_paged_write_byte(e_priv, 0, ISR0_IMR0, ISR0_IMR0_PAGE);
1092			nec7210_board_reset(nec_priv, board);
1093		}
1094		if (e_priv->irq)
1095			free_irq(e_priv->irq, board);
1096		if (e_priv->write_transfer_counter_res) {
1097			release_mem_region(e_priv->write_transfer_counter_res->start,
1098					   resource_size(e_priv->write_transfer_counter_res));
1099		}
1100		if (e_priv->dma_port_res) {
1101			release_mem_region(e_priv->dma_port_res->start,
1102					   resource_size(e_priv->dma_port_res));
1103		}
1104		if (e_priv->gpib_iomem_res)
1105			release_mem_region(e_priv->gpib_iomem_res->start,
1106					   resource_size(e_priv->gpib_iomem_res));
1107	}
1108	fluke_generic_detach(board);
1109}
1110
1111static int fluke_gpib_probe(struct platform_device *pdev)
1112{
1113	fluke_gpib_pdev = pdev;
1114	return 0;
1115}
1116
1117static const struct of_device_id fluke_gpib_of_match[] = {
1118	{ .compatible = "flk,fgpib-4.0"},
1119	{ {0} }
1120};
1121MODULE_DEVICE_TABLE(of, fluke_gpib_of_match);
1122
1123static struct platform_driver fluke_gpib_platform_driver = {
1124	.driver = {
1125		.name = DRV_NAME,
1126		.of_match_table = fluke_gpib_of_match,
1127	},
1128	.probe = &fluke_gpib_probe
1129};
1130
1131static int __init fluke_init_module(void)
1132{
1133	int result;
1134
1135	result = platform_driver_register(&fluke_gpib_platform_driver);
1136	if (result) {
1137		pr_err("platform_driver_register failed: error = %d\n", result);
1138		return result;
1139	}
1140
1141	result = gpib_register_driver(&fluke_unaccel_interface, THIS_MODULE);
1142	if (result) {
1143		pr_err("gpib_register_driver failed: error = %d\n", result);
1144		goto err_unaccel;
1145	}
1146
1147	result = gpib_register_driver(&fluke_hybrid_interface, THIS_MODULE);
1148	if (result) {
1149		pr_err("gpib_register_driver failed: error = %d\n", result);
1150		goto err_hybrid;
1151	}
1152
1153	result = gpib_register_driver(&fluke_interface, THIS_MODULE);
1154	if (result) {
1155		pr_err("gpib_register_driver failed: error = %d\n", result);
1156		goto err_interface;
1157	}
1158
1159	return 0;
1160
1161err_interface:
1162	gpib_unregister_driver(&fluke_hybrid_interface);
1163err_hybrid:
1164	gpib_unregister_driver(&fluke_unaccel_interface);
1165err_unaccel:
1166	platform_driver_unregister(&fluke_gpib_platform_driver);
1167
1168	return result;
1169}
1170
1171static void __exit fluke_exit_module(void)
1172{
1173	gpib_unregister_driver(&fluke_unaccel_interface);
1174	gpib_unregister_driver(&fluke_hybrid_interface);
1175	gpib_unregister_driver(&fluke_interface);
1176	platform_driver_unregister(&fluke_gpib_platform_driver);
1177}
1178
1179module_init(fluke_init_module);
1180module_exit(fluke_exit_module);
~/ Read-only file preview.