xref: /linux/drivers/staging/gpib/agilent_82350b/agilent_82350b.c (revision a100922a3855eb35ecd465f1d558546b1e144445)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 /***************************************************************************
4  *   copyright            : (C) 2002, 2004 by Frank Mori Hess              *
5  ***************************************************************************/
6 
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #define dev_fmt pr_fmt
9 #define DRV_NAME KBUILD_MODNAME
10 
11 #include "agilent_82350b.h"
12 #include <linux/delay.h>
13 #include <linux/ioport.h>
14 #include <linux/sched.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <asm/dma.h>
18 #include <linux/pci.h>
19 #include <linux/pci_ids.h>
20 #include <linux/string.h>
21 #include <linux/init.h>
22 #include <linux/wait.h>
23 
24 MODULE_LICENSE("GPL");
25 MODULE_DESCRIPTION("GPIB driver for Agilent 82350b");
26 
27 static int read_transfer_counter(struct agilent_82350b_priv *a_priv);
28 static unsigned short read_and_clear_event_status(struct gpib_board *board);
29 static void set_transfer_counter(struct agilent_82350b_priv *a_priv, int count);
30 static int agilent_82350b_write(struct gpib_board *board, u8 *buffer,
31 				size_t length, int send_eoi, size_t *bytes_written);
32 
agilent_82350b_accel_read(struct gpib_board * board,u8 * buffer,size_t length,int * end,size_t * bytes_read)33 static int agilent_82350b_accel_read(struct gpib_board *board, u8 *buffer,
34 				     size_t length, int *end, size_t *bytes_read)
35 
36 {
37 	struct agilent_82350b_priv *a_priv = board->private_data;
38 	struct tms9914_priv *tms_priv = &a_priv->tms9914_priv;
39 	int retval = 0;
40 	unsigned short event_status;
41 	int i, num_fifo_bytes;
42 	/* hardware doesn't support checking for end-of-string character when using fifo */
43 	if (tms_priv->eos_flags & REOS)
44 		return tms9914_read(board, tms_priv, buffer, length, end, bytes_read);
45 
46 	clear_bit(DEV_CLEAR_BN, &tms_priv->state);
47 
48 	read_and_clear_event_status(board);
49 	*end = 0;
50 	*bytes_read = 0;
51 	if (length == 0)
52 		return 0;
53 	/* disable fifo for the moment */
54 	writeb(DIRECTION_GPIB_TO_HOST, a_priv->gpib_base + SRAM_ACCESS_CONTROL_REG);
55 	/* handle corner case of board not in holdoff and one byte might slip in early */
56 	if (tms_priv->holdoff_active == 0 && length > 1) {
57 		size_t num_bytes;
58 
59 		retval = tms9914_read(board, tms_priv, buffer, 1, end, &num_bytes);
60 		*bytes_read += num_bytes;
61 		if (retval < 0 || *end)
62 			return retval;
63 		++buffer;
64 		--length;
65 	}
66 	tms9914_set_holdoff_mode(tms_priv, TMS9914_HOLDOFF_EOI);
67 	tms9914_release_holdoff(tms_priv);
68 	i = 0;
69 	num_fifo_bytes = length - 1;
70 	/* disable BI interrupts */
71 	write_byte(tms_priv, tms_priv->imr0_bits & ~HR_BIIE, IMR0);
72 	while (i < num_fifo_bytes && *end == 0) {
73 		int block_size;
74 		int j;
75 		int count;
76 
77 		block_size = min(num_fifo_bytes - i, agilent_82350b_fifo_size);
78 		set_transfer_counter(a_priv, block_size);
79 		writeb(ENABLE_TI_TO_SRAM | DIRECTION_GPIB_TO_HOST,
80 		       a_priv->gpib_base + SRAM_ACCESS_CONTROL_REG);
81 		if (agilent_82350b_fifo_is_halted(a_priv))
82 			writeb(RESTART_STREAM_BIT, a_priv->gpib_base + STREAM_STATUS_REG);
83 
84 		clear_bit(READ_READY_BN, &tms_priv->state);
85 
86 		retval = wait_event_interruptible(board->wait,
87 						  ((event_status =
88 						    read_and_clear_event_status(board)) &
89 						   (TERM_COUNT_STATUS_BIT |
90 						    BUFFER_END_STATUS_BIT)) ||
91 						  test_bit(DEV_CLEAR_BN, &tms_priv->state) ||
92 						  test_bit(TIMO_NUM, &board->status));
93 		if (retval) {
94 			retval = -ERESTARTSYS;
95 			break;
96 		}
97 		count = block_size - read_transfer_counter(a_priv);
98 		for (j = 0; j < count && i < num_fifo_bytes; ++j)
99 			buffer[i++] = readb(a_priv->sram_base + j);
100 		if (event_status & BUFFER_END_STATUS_BIT) {
101 			clear_bit(RECEIVED_END_BN, &tms_priv->state);
102 
103 			tms_priv->holdoff_active = 1;
104 			*end = 1;
105 		}
106 		if (test_bit(TIMO_NUM, &board->status)) {
107 			retval = -ETIMEDOUT;
108 			break;
109 		}
110 		if (test_bit(DEV_CLEAR_BN, &tms_priv->state)) {
111 			retval = -EINTR;
112 			break;
113 		}
114 	}
115 	/* re-enable BI interrupts */
116 	write_byte(tms_priv, tms_priv->imr0_bits, IMR0);
117 	*bytes_read += i;
118 	buffer += i;
119 	length -= i;
120 	writeb(DIRECTION_GPIB_TO_HOST, a_priv->gpib_base + SRAM_ACCESS_CONTROL_REG);
121 	if (retval < 0)
122 		return retval;
123 	/* read last bytes if we havn't received an END yet */
124 	if (*end == 0) {
125 		size_t num_bytes;
126 		/* try to make sure we holdoff after last byte read */
127 		retval = tms9914_read(board, tms_priv, buffer, length, end, &num_bytes);
128 		*bytes_read += num_bytes;
129 		if (retval < 0)
130 			return retval;
131 	}
132 	return 0;
133 }
134 
translate_wait_return_value(struct gpib_board * board,int retval)135 static int translate_wait_return_value(struct gpib_board *board, int retval)
136 
137 {
138 	struct agilent_82350b_priv *a_priv = board->private_data;
139 	struct tms9914_priv *tms_priv = &a_priv->tms9914_priv;
140 
141 	if (retval)
142 		return -ERESTARTSYS;
143 	if (test_bit(TIMO_NUM, &board->status))
144 		return -ETIMEDOUT;
145 	if (test_bit(DEV_CLEAR_BN, &tms_priv->state))
146 		return -EINTR;
147 	return 0;
148 }
149 
agilent_82350b_accel_write(struct gpib_board * board,u8 * buffer,size_t length,int send_eoi,size_t * bytes_written)150 static int agilent_82350b_accel_write(struct gpib_board *board, u8 *buffer,
151 				      size_t length, int send_eoi,
152 				      size_t *bytes_written)
153 {
154 	struct agilent_82350b_priv *a_priv = board->private_data;
155 	struct tms9914_priv *tms_priv = &a_priv->tms9914_priv;
156 	int i, j;
157 	unsigned short event_status;
158 	int retval = 0;
159 	int fifotransferlength = length;
160 	int block_size = 0;
161 	size_t num_bytes;
162 
163 	*bytes_written = 0;
164 	if (send_eoi)
165 		--fifotransferlength;
166 
167 	clear_bit(DEV_CLEAR_BN, &tms_priv->state);
168 
169 	writeb(0, a_priv->gpib_base + SRAM_ACCESS_CONTROL_REG);
170 
171 	event_status = read_and_clear_event_status(board);
172 
173 #ifdef EXPERIMENTAL
174 	/* wait for previous BO to complete if any */
175 	retval = wait_event_interruptible(board->wait,
176 					  test_bit(DEV_CLEAR_BN, &tms_priv->state) ||
177 					  test_bit(WRITE_READY_BN, &tms_priv->state) ||
178 					  test_bit(TIMO_NUM, &board->status));
179 	retval = translate_wait_return_value(board, retval);
180 
181 	if (retval)
182 		return retval;
183 #endif
184 
185 	retval = agilent_82350b_write(board, buffer, 1, 0, &num_bytes);
186 	*bytes_written += num_bytes;
187 	if (retval < 0)
188 		return retval;
189 
190 	write_byte(tms_priv, tms_priv->imr0_bits & ~HR_BOIE, IMR0);
191 	for (i = 1; i < fifotransferlength;) {
192 		clear_bit(WRITE_READY_BN, &tms_priv->state);
193 
194 		block_size = min(fifotransferlength - i, agilent_82350b_fifo_size);
195 		set_transfer_counter(a_priv, block_size);
196 		for (j = 0; j < block_size; ++j, ++i) {
197 			/* load data into board's sram */
198 			writeb(buffer[i], a_priv->sram_base + j);
199 		}
200 		writeb(ENABLE_TI_TO_SRAM, a_priv->gpib_base + SRAM_ACCESS_CONTROL_REG);
201 
202 		if (agilent_82350b_fifo_is_halted(a_priv))
203 			writeb(RESTART_STREAM_BIT, a_priv->gpib_base + STREAM_STATUS_REG);
204 
205 		retval = wait_event_interruptible(board->wait,
206 						  ((event_status =
207 						    read_and_clear_event_status(board)) &
208 						   TERM_COUNT_STATUS_BIT) ||
209 						  test_bit(DEV_CLEAR_BN, &tms_priv->state) ||
210 						  test_bit(TIMO_NUM, &board->status));
211 		writeb(0, a_priv->gpib_base + SRAM_ACCESS_CONTROL_REG);
212 		num_bytes = block_size - read_transfer_counter(a_priv);
213 
214 		*bytes_written += num_bytes;
215 		retval = translate_wait_return_value(board, retval);
216 		if (retval)
217 			break;
218 	}
219 	write_byte(tms_priv, tms_priv->imr0_bits, IMR0);
220 	if (retval)
221 		return retval;
222 
223 	if (send_eoi) {
224 		retval = agilent_82350b_write(board, buffer + fifotransferlength, 1, send_eoi,
225 					      &num_bytes);
226 		*bytes_written += num_bytes;
227 		if (retval < 0)
228 			return retval;
229 	}
230 	return 0;
231 }
232 
read_and_clear_event_status(struct gpib_board * board)233 static unsigned short read_and_clear_event_status(struct gpib_board *board)
234 {
235 	struct agilent_82350b_priv *a_priv = board->private_data;
236 	unsigned long flags;
237 	unsigned short status;
238 
239 	spin_lock_irqsave(&board->spinlock, flags);
240 	status = a_priv->event_status_bits;
241 	a_priv->event_status_bits = 0;
242 	spin_unlock_irqrestore(&board->spinlock, flags);
243 	return status;
244 }
245 
agilent_82350b_interrupt(int irq,void * arg)246 static irqreturn_t agilent_82350b_interrupt(int irq, void *arg)
247 
248 {
249 	int tms9914_status1 = 0, tms9914_status2 = 0;
250 	int event_status;
251 	struct gpib_board *board = arg;
252 	struct agilent_82350b_priv *a_priv = board->private_data;
253 	unsigned long flags;
254 	irqreturn_t retval = IRQ_NONE;
255 
256 	spin_lock_irqsave(&board->spinlock, flags);
257 	event_status = readb(a_priv->gpib_base + EVENT_STATUS_REG);
258 	if (event_status & IRQ_STATUS_BIT)
259 		retval = IRQ_HANDLED;
260 
261 	if (event_status & TMS9914_IRQ_STATUS_BIT) {
262 		tms9914_status1 = read_byte(&a_priv->tms9914_priv, ISR0);
263 		tms9914_status2 = read_byte(&a_priv->tms9914_priv, ISR1);
264 		tms9914_interrupt_have_status(board, &a_priv->tms9914_priv, tms9914_status1,
265 					      tms9914_status2);
266 	}
267 	/* write-clear status bits */
268 	if (event_status & (BUFFER_END_STATUS_BIT | TERM_COUNT_STATUS_BIT)) {
269 		writeb(event_status & (BUFFER_END_STATUS_BIT | TERM_COUNT_STATUS_BIT),
270 		       a_priv->gpib_base + EVENT_STATUS_REG);
271 		a_priv->event_status_bits |= event_status;
272 		wake_up_interruptible(&board->wait);
273 	}
274 	spin_unlock_irqrestore(&board->spinlock, flags);
275 	return retval;
276 }
277 
278 static void agilent_82350b_detach(struct gpib_board *board);
279 
read_transfer_counter(struct agilent_82350b_priv * a_priv)280 static int read_transfer_counter(struct agilent_82350b_priv *a_priv)
281 {
282 	int lo, mid, value;
283 
284 	lo = readb(a_priv->gpib_base + XFER_COUNT_LO_REG);
285 	mid = readb(a_priv->gpib_base + XFER_COUNT_MID_REG);
286 	value = (lo & 0xff) | ((mid << 8) & 0x7f00);
287 	value = ~(value - 1) & 0x7fff;
288 	return value;
289 }
290 
set_transfer_counter(struct agilent_82350b_priv * a_priv,int count)291 static void set_transfer_counter(struct agilent_82350b_priv *a_priv, int count)
292 {
293 	int complement = -count;
294 
295 	writeb(complement & 0xff, a_priv->gpib_base + XFER_COUNT_LO_REG);
296 	writeb((complement >> 8) & 0xff, a_priv->gpib_base + XFER_COUNT_MID_REG);
297 	/* I don't think the hi count reg is even used, but oh well */
298 	writeb((complement >> 16) & 0xf, a_priv->gpib_base + XFER_COUNT_HI_REG);
299 }
300 
301 /* wrappers for interface functions */
agilent_82350b_read(struct gpib_board * board,u8 * buffer,size_t length,int * end,size_t * bytes_read)302 static int agilent_82350b_read(struct gpib_board *board, u8 *buffer,
303 			       size_t length, int *end, size_t *bytes_read)
304 {
305 	struct agilent_82350b_priv *priv = board->private_data;
306 
307 	return tms9914_read(board, &priv->tms9914_priv, buffer, length, end, bytes_read);
308 }
309 
agilent_82350b_write(struct gpib_board * board,u8 * buffer,size_t length,int send_eoi,size_t * bytes_written)310 static int agilent_82350b_write(struct gpib_board *board, u8 *buffer,
311 				size_t length, int send_eoi, size_t *bytes_written)
312 
313 {
314 	struct agilent_82350b_priv *priv = board->private_data;
315 
316 	return tms9914_write(board, &priv->tms9914_priv, buffer, length, send_eoi, bytes_written);
317 }
318 
agilent_82350b_command(struct gpib_board * board,u8 * buffer,size_t length,size_t * bytes_written)319 static int agilent_82350b_command(struct gpib_board *board, u8 *buffer,
320 				  size_t length, size_t *bytes_written)
321 
322 {
323 	struct agilent_82350b_priv *priv = board->private_data;
324 
325 	return tms9914_command(board, &priv->tms9914_priv, buffer, length, bytes_written);
326 }
327 
agilent_82350b_take_control(struct gpib_board * board,int synchronous)328 static int agilent_82350b_take_control(struct gpib_board *board, int synchronous)
329 
330 {
331 	struct agilent_82350b_priv *priv = board->private_data;
332 
333 	return tms9914_take_control_workaround(board, &priv->tms9914_priv, synchronous);
334 }
335 
agilent_82350b_go_to_standby(struct gpib_board * board)336 static int agilent_82350b_go_to_standby(struct gpib_board *board)
337 
338 {
339 	struct agilent_82350b_priv *priv = board->private_data;
340 
341 	return tms9914_go_to_standby(board, &priv->tms9914_priv);
342 }
343 
agilent_82350b_request_system_control(struct gpib_board * board,int request_control)344 static int agilent_82350b_request_system_control(struct gpib_board *board, int request_control)
345 {
346 	struct agilent_82350b_priv *a_priv = board->private_data;
347 
348 	if (request_control) {
349 		a_priv->card_mode_bits |= CM_SYSTEM_CONTROLLER_BIT;
350 		if (a_priv->model != MODEL_82350A)
351 			writeb(IC_SYSTEM_CONTROLLER_BIT, a_priv->gpib_base + INTERNAL_CONFIG_REG);
352 	} else {
353 		a_priv->card_mode_bits &= ~CM_SYSTEM_CONTROLLER_BIT;
354 		if (a_priv->model != MODEL_82350A)
355 			writeb(0, a_priv->gpib_base + INTERNAL_CONFIG_REG);
356 	}
357 	writeb(a_priv->card_mode_bits, a_priv->gpib_base + CARD_MODE_REG);
358 	return tms9914_request_system_control(board, &a_priv->tms9914_priv, request_control);
359 }
360 
agilent_82350b_interface_clear(struct gpib_board * board,int assert)361 static void agilent_82350b_interface_clear(struct gpib_board *board, int assert)
362 
363 {
364 	struct agilent_82350b_priv *priv = board->private_data;
365 
366 	tms9914_interface_clear(board, &priv->tms9914_priv, assert);
367 }
368 
agilent_82350b_remote_enable(struct gpib_board * board,int enable)369 static void agilent_82350b_remote_enable(struct gpib_board *board, int enable)
370 {
371 	struct agilent_82350b_priv *priv = board->private_data;
372 
373 	tms9914_remote_enable(board, &priv->tms9914_priv, enable);
374 }
375 
agilent_82350b_enable_eos(struct gpib_board * board,u8 eos_byte,int compare_8_bits)376 static int agilent_82350b_enable_eos(struct gpib_board *board, u8 eos_byte,
377 				     int compare_8_bits)
378 {
379 	struct agilent_82350b_priv *priv = board->private_data;
380 
381 	return tms9914_enable_eos(board, &priv->tms9914_priv, eos_byte, compare_8_bits);
382 }
383 
agilent_82350b_disable_eos(struct gpib_board * board)384 static void agilent_82350b_disable_eos(struct gpib_board *board)
385 {
386 	struct agilent_82350b_priv *priv = board->private_data;
387 
388 	tms9914_disable_eos(board, &priv->tms9914_priv);
389 }
390 
agilent_82350b_update_status(struct gpib_board * board,unsigned int clear_mask)391 static unsigned int agilent_82350b_update_status(struct gpib_board *board,
392 						 unsigned int clear_mask)
393 {
394 	struct agilent_82350b_priv *priv = board->private_data;
395 
396 	return tms9914_update_status(board, &priv->tms9914_priv, clear_mask);
397 }
398 
agilent_82350b_primary_address(struct gpib_board * board,unsigned int address)399 static int agilent_82350b_primary_address(struct gpib_board *board,
400 					  unsigned int address)
401 {
402 	struct agilent_82350b_priv *priv = board->private_data;
403 
404 	return tms9914_primary_address(board, &priv->tms9914_priv, address);
405 }
406 
agilent_82350b_secondary_address(struct gpib_board * board,unsigned int address,int enable)407 static int agilent_82350b_secondary_address(struct gpib_board *board,
408 					    unsigned int address, int enable)
409 {
410 	struct agilent_82350b_priv *priv = board->private_data;
411 
412 	return tms9914_secondary_address(board, &priv->tms9914_priv, address, enable);
413 }
414 
agilent_82350b_parallel_poll(struct gpib_board * board,u8 * result)415 static int agilent_82350b_parallel_poll(struct gpib_board *board, u8 *result)
416 {
417 	struct agilent_82350b_priv *priv = board->private_data;
418 
419 	return tms9914_parallel_poll(board, &priv->tms9914_priv, result);
420 }
421 
agilent_82350b_parallel_poll_configure(struct gpib_board * board,u8 config)422 static void agilent_82350b_parallel_poll_configure(struct gpib_board *board,
423 						   u8 config)
424 {
425 	struct agilent_82350b_priv *priv = board->private_data;
426 
427 	tms9914_parallel_poll_configure(board, &priv->tms9914_priv, config);
428 }
429 
agilent_82350b_parallel_poll_response(struct gpib_board * board,int ist)430 static void agilent_82350b_parallel_poll_response(struct gpib_board *board, int ist)
431 {
432 	struct agilent_82350b_priv *priv = board->private_data;
433 
434 	tms9914_parallel_poll_response(board, &priv->tms9914_priv, ist);
435 }
436 
agilent_82350b_serial_poll_response(struct gpib_board * board,u8 status)437 static void agilent_82350b_serial_poll_response(struct gpib_board *board, u8 status)
438 {
439 	struct agilent_82350b_priv *priv = board->private_data;
440 
441 	tms9914_serial_poll_response(board, &priv->tms9914_priv, status);
442 }
443 
agilent_82350b_serial_poll_status(struct gpib_board * board)444 static u8 agilent_82350b_serial_poll_status(struct gpib_board *board)
445 {
446 	struct agilent_82350b_priv *priv = board->private_data;
447 
448 	return tms9914_serial_poll_status(board, &priv->tms9914_priv);
449 }
450 
agilent_82350b_line_status(const struct gpib_board * board)451 static int agilent_82350b_line_status(const struct gpib_board *board)
452 {
453 	struct agilent_82350b_priv *priv = board->private_data;
454 
455 	return tms9914_line_status(board, &priv->tms9914_priv);
456 }
457 
agilent_82350b_t1_delay(struct gpib_board * board,unsigned int nanosec)458 static int agilent_82350b_t1_delay(struct gpib_board *board, unsigned int nanosec)
459 {
460 	struct agilent_82350b_priv *a_priv = board->private_data;
461 	static const int nanosec_per_clock = 30;
462 	unsigned int value;
463 
464 	tms9914_t1_delay(board, &a_priv->tms9914_priv, nanosec);
465 
466 	value = (nanosec + nanosec_per_clock - 1) / nanosec_per_clock;
467 	if (value > 0xff)
468 		value = 0xff;
469 	writeb(value, a_priv->gpib_base + T1_DELAY_REG);
470 	return value * nanosec_per_clock;
471 }
472 
agilent_82350b_return_to_local(struct gpib_board * board)473 static void agilent_82350b_return_to_local(struct gpib_board *board)
474 {
475 	struct agilent_82350b_priv *priv = board->private_data;
476 
477 	tms9914_return_to_local(board, &priv->tms9914_priv);
478 }
479 
agilent_82350b_allocate_private(struct gpib_board * board)480 static int agilent_82350b_allocate_private(struct gpib_board *board)
481 {
482 	board->private_data = kzalloc(sizeof(struct agilent_82350b_priv), GFP_KERNEL);
483 	if (!board->private_data)
484 		return -ENOMEM;
485 	return 0;
486 }
487 
agilent_82350b_free_private(struct gpib_board * board)488 static void agilent_82350b_free_private(struct gpib_board *board)
489 {
490 	kfree(board->private_data);
491 	board->private_data = NULL;
492 }
493 
init_82350a_hardware(struct gpib_board * board,const struct gpib_board_config * config)494 static int init_82350a_hardware(struct gpib_board *board,
495 				const struct gpib_board_config *config)
496 {
497 	struct agilent_82350b_priv *a_priv = board->private_data;
498 	static const unsigned int firmware_length = 5302;
499 	unsigned int borg_status;
500 	static const unsigned int timeout = 1000;
501 	int i, j;
502 	const char *firmware_data = config->init_data;
503 	const unsigned int plx_cntrl_static_bits = PLX9050_WAITO_NOT_USER0_SELECT_BIT |
504 		PLX9050_USER0_OUTPUT_BIT |
505 		PLX9050_LLOCK_NOT_USER1_SELECT_BIT |
506 		PLX9050_USER1_OUTPUT_BIT |
507 		PLX9050_USER2_OUTPUT_BIT |
508 		PLX9050_USER3_OUTPUT_BIT |
509 		PLX9050_PCI_READ_MODE_BIT |
510 		PLX9050_PCI_WRITE_MODE_BIT |
511 		PLX9050_PCI_RETRY_DELAY_BITS(64) |
512 		PLX9050_DIRECT_SLAVE_LOCK_ENABLE_BIT;
513 
514 	/* load borg data */
515 	borg_status = readb(a_priv->borg_base);
516 	if ((borg_status & BORG_DONE_BIT))
517 		return 0;
518 	/* need to programme borg */
519 	if (!config->init_data || config->init_data_length != firmware_length) {
520 		dev_err(board->gpib_dev, "the 82350A board requires firmware after powering on.\n");
521 		return -EIO;
522 	}
523 	dev_dbg(board->gpib_dev, "Loading firmware...\n");
524 
525 	/* tickle the borg */
526 	writel(plx_cntrl_static_bits | PLX9050_USER3_DATA_BIT,
527 	       a_priv->plx_base + PLX9050_CNTRL_REG);
528 	usleep_range(1000, 2000);
529 	writel(plx_cntrl_static_bits, a_priv->plx_base + PLX9050_CNTRL_REG);
530 	usleep_range(1000, 2000);
531 	writel(plx_cntrl_static_bits | PLX9050_USER3_DATA_BIT,
532 	       a_priv->plx_base + PLX9050_CNTRL_REG);
533 	usleep_range(1000, 2000);
534 
535 	for (i = 0; i < config->init_data_length; ++i) {
536 		for (j = 0; j < timeout && (readb(a_priv->borg_base) & BORG_READY_BIT) == 0; ++j) {
537 			if (need_resched())
538 				schedule();
539 			usleep_range(10, 20);
540 		}
541 		if (j == timeout) {
542 			dev_err(board->gpib_dev, "timed out loading firmware.\n");
543 			return -ETIMEDOUT;
544 		}
545 		writeb(firmware_data[i], a_priv->gpib_base + CONFIG_DATA_REG);
546 	}
547 	for (j = 0; j < timeout && (readb(a_priv->borg_base) & BORG_DONE_BIT) == 0; ++j) {
548 		if (need_resched())
549 			schedule();
550 		usleep_range(10, 20);
551 	}
552 	if (j == timeout) {
553 		dev_err(board->gpib_dev, "timed out waiting for firmware load to complete.\n");
554 		return -ETIMEDOUT;
555 	}
556 	dev_dbg(board->gpib_dev, " ...done.\n");
557 	return 0;
558 }
559 
test_sram(struct gpib_board * board)560 static int test_sram(struct gpib_board *board)
561 
562 {
563 	struct agilent_82350b_priv *a_priv = board->private_data;
564 	unsigned int i;
565 	const unsigned int sram_length = pci_resource_len(a_priv->pci_device, SRAM_82350A_REGION);
566 	/* test SRAM */
567 	const unsigned int byte_mask = 0xff;
568 
569 	for (i = 0; i < sram_length; ++i) {
570 		writeb(i & byte_mask, a_priv->sram_base + i);
571 		if (need_resched())
572 			schedule();
573 	}
574 	for (i = 0; i < sram_length; ++i) {
575 		unsigned int read_value = readb(a_priv->sram_base + i);
576 
577 		if ((i & byte_mask) != read_value) {
578 			dev_err(board->gpib_dev, "SRAM test failed at %d wanted %d got %d\n",
579 				i, (i & byte_mask), read_value);
580 			return -EIO;
581 		}
582 		if (need_resched())
583 			schedule();
584 	}
585 	dev_dbg(board->gpib_dev, "SRAM test passed 0x%x bytes checked\n", sram_length);
586 	return 0;
587 }
588 
agilent_82350b_generic_attach(struct gpib_board * board,const struct gpib_board_config * config,int use_fifos)589 static int agilent_82350b_generic_attach(struct gpib_board *board,
590 					 const struct gpib_board_config *config,
591 					 int use_fifos)
592 
593 {
594 	struct agilent_82350b_priv *a_priv;
595 	struct tms9914_priv *tms_priv;
596 	int retval;
597 
598 	board->status = 0;
599 
600 	if (agilent_82350b_allocate_private(board))
601 		return -ENOMEM;
602 	a_priv = board->private_data;
603 	a_priv->using_fifos = use_fifos;
604 	tms_priv = &a_priv->tms9914_priv;
605 	tms_priv->read_byte = tms9914_iomem_read_byte;
606 	tms_priv->write_byte = tms9914_iomem_write_byte;
607 	tms_priv->offset = 1;
608 
609 	/* find board */
610 	a_priv->pci_device = gpib_pci_get_device(config, PCI_VENDOR_ID_AGILENT,
611 						 PCI_DEVICE_ID_82350B, NULL);
612 	if (a_priv->pci_device) {
613 		a_priv->model = MODEL_82350B;
614 		dev_dbg(board->gpib_dev, "Agilent 82350B board found\n");
615 
616 	} else	{
617 		a_priv->pci_device = gpib_pci_get_device(config, PCI_VENDOR_ID_AGILENT,
618 							 PCI_DEVICE_ID_82351A, NULL);
619 		if (a_priv->pci_device)	{
620 			a_priv->model = MODEL_82351A;
621 			dev_dbg(board->gpib_dev, "Agilent 82351B board found\n");
622 
623 		} else {
624 			a_priv->pci_device = gpib_pci_get_subsys(config, PCI_VENDOR_ID_PLX,
625 								 PCI_DEVICE_ID_PLX_9050,
626 								 PCI_VENDOR_ID_HP,
627 								 PCI_SUBDEVICE_ID_82350A,
628 								 a_priv->pci_device);
629 			if (a_priv->pci_device) {
630 				a_priv->model = MODEL_82350A;
631 				dev_dbg(board->gpib_dev, "HP/Agilent 82350A board found\n");
632 			} else {
633 				dev_err(board->gpib_dev, "no 82350/82351 board found\n");
634 				return -ENODEV;
635 			}
636 		}
637 	}
638 	if (pci_enable_device(a_priv->pci_device)) {
639 		dev_err(board->gpib_dev, "error enabling pci device\n");
640 		return -EIO;
641 	}
642 	if (pci_request_regions(a_priv->pci_device, DRV_NAME))
643 		return -ENOMEM;
644 	switch (a_priv->model) {
645 	case MODEL_82350A:
646 		a_priv->plx_base = ioremap(pci_resource_start(a_priv->pci_device, PLX_MEM_REGION),
647 					   pci_resource_len(a_priv->pci_device, PLX_MEM_REGION));
648 		dev_dbg(board->gpib_dev, "plx base address remapped to 0x%p\n", a_priv->plx_base);
649 		a_priv->gpib_base = ioremap(pci_resource_start(a_priv->pci_device,
650 							       GPIB_82350A_REGION),
651 					    pci_resource_len(a_priv->pci_device,
652 							     GPIB_82350A_REGION));
653 		dev_dbg(board->gpib_dev, "chip base address remapped to 0x%p\n", a_priv->gpib_base);
654 		tms_priv->mmiobase = a_priv->gpib_base + TMS9914_BASE_REG;
655 		a_priv->sram_base = ioremap(pci_resource_start(a_priv->pci_device,
656 							       SRAM_82350A_REGION),
657 					    pci_resource_len(a_priv->pci_device,
658 							     SRAM_82350A_REGION));
659 		dev_dbg(board->gpib_dev, "sram base address remapped to 0x%p\n", a_priv->sram_base);
660 		a_priv->borg_base = ioremap(pci_resource_start(a_priv->pci_device,
661 							       BORG_82350A_REGION),
662 					    pci_resource_len(a_priv->pci_device,
663 							     BORG_82350A_REGION));
664 		dev_dbg(board->gpib_dev, "borg base address remapped to 0x%p\n", a_priv->borg_base);
665 
666 		retval = init_82350a_hardware(board, config);
667 		if (retval < 0)
668 			return retval;
669 		break;
670 	case MODEL_82350B:
671 	case MODEL_82351A:
672 		a_priv->gpib_base = ioremap(pci_resource_start(a_priv->pci_device, GPIB_REGION),
673 					    pci_resource_len(a_priv->pci_device, GPIB_REGION));
674 		dev_dbg(board->gpib_dev, "chip base address remapped to 0x%p\n", a_priv->gpib_base);
675 		tms_priv->mmiobase = a_priv->gpib_base + TMS9914_BASE_REG;
676 		a_priv->sram_base = ioremap(pci_resource_start(a_priv->pci_device, SRAM_REGION),
677 					    pci_resource_len(a_priv->pci_device, SRAM_REGION));
678 		dev_dbg(board->gpib_dev, "sram base address remapped to 0x%p\n", a_priv->sram_base);
679 		a_priv->misc_base = ioremap(pci_resource_start(a_priv->pci_device, MISC_REGION),
680 					    pci_resource_len(a_priv->pci_device, MISC_REGION));
681 		dev_dbg(board->gpib_dev, "misc base address remapped to 0x%p\n", a_priv->misc_base);
682 		break;
683 	default:
684 		dev_err(board->gpib_dev, "invalid board\n");
685 		return -ENODEV;
686 	}
687 
688 	retval = test_sram(board);
689 	if (retval < 0)
690 		return retval;
691 
692 	if (request_irq(a_priv->pci_device->irq, agilent_82350b_interrupt,
693 			IRQF_SHARED, DRV_NAME, board)) {
694 		dev_err(board->gpib_dev, "failed to obtain irq %d\n", a_priv->pci_device->irq);
695 		return -EIO;
696 	}
697 	a_priv->irq = a_priv->pci_device->irq;
698 	dev_dbg(board->gpib_dev, " IRQ %d\n", a_priv->irq);
699 
700 	writeb(0, a_priv->gpib_base + SRAM_ACCESS_CONTROL_REG);
701 	a_priv->card_mode_bits = ENABLE_PCI_IRQ_BIT;
702 	writeb(a_priv->card_mode_bits, a_priv->gpib_base + CARD_MODE_REG);
703 
704 	if (a_priv->model == MODEL_82350A) {
705 		/* enable PCI interrupts for 82350a */
706 		writel(PLX9050_LINTR1_EN_BIT | PLX9050_LINTR2_POLARITY_BIT |
707 		       PLX9050_PCI_INTR_EN_BIT,
708 		       a_priv->plx_base + PLX9050_INTCSR_REG);
709 	}
710 
711 	if (use_fifos) {
712 		writeb(ENABLE_BUFFER_END_EVENTS_BIT | ENABLE_TERM_COUNT_EVENTS_BIT,
713 		       a_priv->gpib_base + EVENT_ENABLE_REG);
714 		writeb(ENABLE_TERM_COUNT_INTERRUPT_BIT | ENABLE_BUFFER_END_INTERRUPT_BIT |
715 		       ENABLE_TMS9914_INTERRUPTS_BIT, a_priv->gpib_base + INTERRUPT_ENABLE_REG);
716 		/* write-clear event status bits */
717 		writeb(BUFFER_END_STATUS_BIT | TERM_COUNT_STATUS_BIT,
718 		       a_priv->gpib_base + EVENT_STATUS_REG);
719 	} else {
720 		writeb(0, a_priv->gpib_base + EVENT_ENABLE_REG);
721 		writeb(ENABLE_TMS9914_INTERRUPTS_BIT,
722 		       a_priv->gpib_base + INTERRUPT_ENABLE_REG);
723 	}
724 	board->t1_nano_sec = agilent_82350b_t1_delay(board, 2000);
725 	tms9914_board_reset(tms_priv);
726 
727 	tms9914_online(board, tms_priv);
728 
729 	return 0;
730 }
731 
agilent_82350b_unaccel_attach(struct gpib_board * board,const struct gpib_board_config * config)732 static int agilent_82350b_unaccel_attach(struct gpib_board *board,
733 					 const struct gpib_board_config *config)
734 {
735 	return agilent_82350b_generic_attach(board, config, 0);
736 }
737 
agilent_82350b_accel_attach(struct gpib_board * board,const struct gpib_board_config * config)738 static int agilent_82350b_accel_attach(struct gpib_board *board,
739 				       const struct gpib_board_config *config)
740 {
741 	return agilent_82350b_generic_attach(board, config, 1);
742 }
743 
agilent_82350b_detach(struct gpib_board * board)744 static void agilent_82350b_detach(struct gpib_board *board)
745 {
746 	struct agilent_82350b_priv *a_priv = board->private_data;
747 	struct tms9914_priv *tms_priv;
748 
749 	if (a_priv) {
750 		if (a_priv->plx_base) /* disable interrupts */
751 			writel(0, a_priv->plx_base + PLX9050_INTCSR_REG);
752 
753 		tms_priv = &a_priv->tms9914_priv;
754 		if (a_priv->irq)
755 			free_irq(a_priv->irq, board);
756 		if (a_priv->gpib_base) {
757 			tms9914_board_reset(tms_priv);
758 			if (a_priv->misc_base)
759 				iounmap(a_priv->misc_base);
760 			if (a_priv->borg_base)
761 				iounmap(a_priv->borg_base);
762 			if (a_priv->sram_base)
763 				iounmap(a_priv->sram_base);
764 			if (a_priv->gpib_base)
765 				iounmap(a_priv->gpib_base);
766 			if (a_priv->plx_base)
767 				iounmap(a_priv->plx_base);
768 			pci_release_regions(a_priv->pci_device);
769 		}
770 		if (a_priv->pci_device)
771 			pci_dev_put(a_priv->pci_device);
772 	}
773 	agilent_82350b_free_private(board);
774 }
775 
776 static struct gpib_interface agilent_82350b_unaccel_interface = {
777 	.name = "agilent_82350b_unaccel",
778 	.attach = agilent_82350b_unaccel_attach,
779 	.detach = agilent_82350b_detach,
780 	.read = agilent_82350b_read,
781 	.write = agilent_82350b_write,
782 	.command = agilent_82350b_command,
783 	.request_system_control = agilent_82350b_request_system_control,
784 	.take_control = agilent_82350b_take_control,
785 	.go_to_standby = agilent_82350b_go_to_standby,
786 	.interface_clear = agilent_82350b_interface_clear,
787 	.remote_enable = agilent_82350b_remote_enable,
788 	.enable_eos = agilent_82350b_enable_eos,
789 	.disable_eos = agilent_82350b_disable_eos,
790 	.parallel_poll = agilent_82350b_parallel_poll,
791 	.parallel_poll_configure = agilent_82350b_parallel_poll_configure,
792 	.parallel_poll_response = agilent_82350b_parallel_poll_response,
793 	.local_parallel_poll_mode = NULL, /* XXX */
794 	.line_status = agilent_82350b_line_status,
795 	.update_status = agilent_82350b_update_status,
796 	.primary_address = agilent_82350b_primary_address,
797 	.secondary_address = agilent_82350b_secondary_address,
798 	.serial_poll_response = agilent_82350b_serial_poll_response,
799 	.serial_poll_status = agilent_82350b_serial_poll_status,
800 	.t1_delay = agilent_82350b_t1_delay,
801 	.return_to_local = agilent_82350b_return_to_local,
802 };
803 
804 static struct gpib_interface agilent_82350b_interface = {
805 	.name = "agilent_82350b",
806 	.attach = agilent_82350b_accel_attach,
807 	.detach = agilent_82350b_detach,
808 	.read = agilent_82350b_accel_read,
809 	.write = agilent_82350b_accel_write,
810 	.command = agilent_82350b_command,
811 	.request_system_control = agilent_82350b_request_system_control,
812 	.take_control = agilent_82350b_take_control,
813 	.go_to_standby = agilent_82350b_go_to_standby,
814 	.interface_clear = agilent_82350b_interface_clear,
815 	.remote_enable = agilent_82350b_remote_enable,
816 	.enable_eos = agilent_82350b_enable_eos,
817 	.disable_eos = agilent_82350b_disable_eos,
818 	.parallel_poll = agilent_82350b_parallel_poll,
819 	.parallel_poll_configure = agilent_82350b_parallel_poll_configure,
820 	.parallel_poll_response = agilent_82350b_parallel_poll_response,
821 	.local_parallel_poll_mode = NULL, /* XXX */
822 	.line_status = agilent_82350b_line_status,
823 	.update_status = agilent_82350b_update_status,
824 	.primary_address = agilent_82350b_primary_address,
825 	.secondary_address = agilent_82350b_secondary_address,
826 	.serial_poll_response = agilent_82350b_serial_poll_response,
827 	.serial_poll_status = agilent_82350b_serial_poll_status,
828 	.t1_delay = agilent_82350b_t1_delay,
829 	.return_to_local = agilent_82350b_return_to_local,
830 };
831 
agilent_82350b_pci_probe(struct pci_dev * dev,const struct pci_device_id * id)832 static int agilent_82350b_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
833 
834 {
835 	return 0;
836 }
837 
838 static const struct pci_device_id agilent_82350b_pci_table[] = {
839 	{ PCI_VENDOR_ID_PLX,     PCI_DEVICE_ID_PLX_9050, PCI_VENDOR_ID_HP,
840 	  PCI_SUBDEVICE_ID_82350A, 0, 0, 0 },
841 	{ PCI_VENDOR_ID_AGILENT, PCI_DEVICE_ID_82350B, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
842 	{ PCI_VENDOR_ID_AGILENT, PCI_DEVICE_ID_82351A, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
843 	{ 0 }
844 };
845 MODULE_DEVICE_TABLE(pci, agilent_82350b_pci_table);
846 
847 static struct pci_driver agilent_82350b_pci_driver = {
848 	.name = DRV_NAME,
849 	.id_table = agilent_82350b_pci_table,
850 	.probe = &agilent_82350b_pci_probe
851 };
852 
agilent_82350b_init_module(void)853 static int __init agilent_82350b_init_module(void)
854 {
855 	int result;
856 
857 	result = pci_register_driver(&agilent_82350b_pci_driver);
858 	if (result) {
859 		pr_err("pci_register_driver failed: error = %d\n", result);
860 		return result;
861 	}
862 
863 	result = gpib_register_driver(&agilent_82350b_unaccel_interface, THIS_MODULE);
864 	if (result) {
865 		pr_err("gpib_register_driver failed: error = %d\n", result);
866 		goto err_unaccel;
867 	}
868 
869 	result = gpib_register_driver(&agilent_82350b_interface, THIS_MODULE);
870 	if (result) {
871 		pr_err("gpib_register_driver failed: error = %d\n", result);
872 		goto err_interface;
873 	}
874 
875 	return 0;
876 
877 err_interface:
878 	gpib_unregister_driver(&agilent_82350b_unaccel_interface);
879 err_unaccel:
880 	pci_unregister_driver(&agilent_82350b_pci_driver);
881 
882 	return result;
883 }
884 
agilent_82350b_exit_module(void)885 static void __exit agilent_82350b_exit_module(void)
886 {
887 	gpib_unregister_driver(&agilent_82350b_interface);
888 	gpib_unregister_driver(&agilent_82350b_unaccel_interface);
889 
890 	pci_unregister_driver(&agilent_82350b_pci_driver);
891 }
892 
893 module_init(agilent_82350b_init_module);
894 module_exit(agilent_82350b_exit_module);
895