1 // SPDX-License-Identifier: GPL-2.0
2
3 /***************************************************************************
4 * copyright : (C) 2002 by Frank Mori Hess *
5 ***************************************************************************/
6
7 /*should enable ATN interrupts (and update board->status on occurrence),
8 * implement recovery from bus errors (if necessary)
9 */
10
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #define dev_fmt pr_fmt
13 #define DRV_NAME KBUILD_MODNAME
14
15 #include "hp82335.h"
16 #include <linux/io.h>
17 #include <linux/ioport.h>
18 #include <linux/sched.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/string.h>
22 #include <linux/init.h>
23
24 MODULE_LICENSE("GPL");
25 MODULE_DESCRIPTION("GPIB driver for HP 82335 interface cards");
26
27 static int hp82335_attach(struct gpib_board *board, const gpib_board_config_t *config);
28 static void hp82335_detach(struct gpib_board *board);
29 static irqreturn_t hp82335_interrupt(int irq, void *arg);
30
31 // wrappers for interface functions
hp82335_read(struct gpib_board * board,uint8_t * buffer,size_t length,int * end,size_t * bytes_read)32 static int hp82335_read(struct gpib_board *board, uint8_t *buffer, size_t length,
33 int *end, size_t *bytes_read)
34 {
35 struct hp82335_priv *priv = board->private_data;
36
37 return tms9914_read(board, &priv->tms9914_priv, buffer, length, end, bytes_read);
38 }
39
hp82335_write(struct gpib_board * board,uint8_t * buffer,size_t length,int send_eoi,size_t * bytes_written)40 static int hp82335_write(struct gpib_board *board, uint8_t *buffer, size_t length, int send_eoi,
41 size_t *bytes_written)
42 {
43 struct hp82335_priv *priv = board->private_data;
44
45 return tms9914_write(board, &priv->tms9914_priv, buffer, length, send_eoi, bytes_written);
46 }
47
hp82335_command(struct gpib_board * board,uint8_t * buffer,size_t length,size_t * bytes_written)48 static int hp82335_command(struct gpib_board *board, uint8_t *buffer, size_t length,
49 size_t *bytes_written)
50 {
51 struct hp82335_priv *priv = board->private_data;
52
53 return tms9914_command(board, &priv->tms9914_priv, buffer, length, bytes_written);
54 }
55
hp82335_take_control(struct gpib_board * board,int synchronous)56 static int hp82335_take_control(struct gpib_board *board, int synchronous)
57 {
58 struct hp82335_priv *priv = board->private_data;
59
60 return tms9914_take_control(board, &priv->tms9914_priv, synchronous);
61 }
62
hp82335_go_to_standby(struct gpib_board * board)63 static int hp82335_go_to_standby(struct gpib_board *board)
64 {
65 struct hp82335_priv *priv = board->private_data;
66
67 return tms9914_go_to_standby(board, &priv->tms9914_priv);
68 }
69
hp82335_request_system_control(struct gpib_board * board,int request_control)70 static void hp82335_request_system_control(struct gpib_board *board, int request_control)
71 {
72 struct hp82335_priv *priv = board->private_data;
73
74 tms9914_request_system_control(board, &priv->tms9914_priv, request_control);
75 }
76
hp82335_interface_clear(struct gpib_board * board,int assert)77 static void hp82335_interface_clear(struct gpib_board *board, int assert)
78 {
79 struct hp82335_priv *priv = board->private_data;
80
81 tms9914_interface_clear(board, &priv->tms9914_priv, assert);
82 }
83
hp82335_remote_enable(struct gpib_board * board,int enable)84 static void hp82335_remote_enable(struct gpib_board *board, int enable)
85 {
86 struct hp82335_priv *priv = board->private_data;
87
88 tms9914_remote_enable(board, &priv->tms9914_priv, enable);
89 }
90
hp82335_enable_eos(struct gpib_board * board,uint8_t eos_byte,int compare_8_bits)91 static int hp82335_enable_eos(struct gpib_board *board, uint8_t eos_byte, int compare_8_bits)
92 {
93 struct hp82335_priv *priv = board->private_data;
94
95 return tms9914_enable_eos(board, &priv->tms9914_priv, eos_byte, compare_8_bits);
96 }
97
hp82335_disable_eos(struct gpib_board * board)98 static void hp82335_disable_eos(struct gpib_board *board)
99 {
100 struct hp82335_priv *priv = board->private_data;
101
102 tms9914_disable_eos(board, &priv->tms9914_priv);
103 }
104
hp82335_update_status(struct gpib_board * board,unsigned int clear_mask)105 static unsigned int hp82335_update_status(struct gpib_board *board, unsigned int clear_mask)
106 {
107 struct hp82335_priv *priv = board->private_data;
108
109 return tms9914_update_status(board, &priv->tms9914_priv, clear_mask);
110 }
111
hp82335_primary_address(struct gpib_board * board,unsigned int address)112 static int hp82335_primary_address(struct gpib_board *board, unsigned int address)
113 {
114 struct hp82335_priv *priv = board->private_data;
115
116 return tms9914_primary_address(board, &priv->tms9914_priv, address);
117 }
118
hp82335_secondary_address(struct gpib_board * board,unsigned int address,int enable)119 static int hp82335_secondary_address(struct gpib_board *board, unsigned int address, int enable)
120 {
121 struct hp82335_priv *priv = board->private_data;
122
123 return tms9914_secondary_address(board, &priv->tms9914_priv, address, enable);
124 }
125
hp82335_parallel_poll(struct gpib_board * board,uint8_t * result)126 static int hp82335_parallel_poll(struct gpib_board *board, uint8_t *result)
127 {
128 struct hp82335_priv *priv = board->private_data;
129
130 return tms9914_parallel_poll(board, &priv->tms9914_priv, result);
131 }
132
hp82335_parallel_poll_configure(struct gpib_board * board,uint8_t config)133 static void hp82335_parallel_poll_configure(struct gpib_board *board, uint8_t config)
134 {
135 struct hp82335_priv *priv = board->private_data;
136
137 tms9914_parallel_poll_configure(board, &priv->tms9914_priv, config);
138 }
139
hp82335_parallel_poll_response(struct gpib_board * board,int ist)140 static void hp82335_parallel_poll_response(struct gpib_board *board, int ist)
141 {
142 struct hp82335_priv *priv = board->private_data;
143
144 tms9914_parallel_poll_response(board, &priv->tms9914_priv, ist);
145 }
146
hp82335_serial_poll_response(struct gpib_board * board,uint8_t status)147 static void hp82335_serial_poll_response(struct gpib_board *board, uint8_t status)
148 {
149 struct hp82335_priv *priv = board->private_data;
150
151 tms9914_serial_poll_response(board, &priv->tms9914_priv, status);
152 }
153
hp82335_serial_poll_status(struct gpib_board * board)154 static uint8_t hp82335_serial_poll_status(struct gpib_board *board)
155 {
156 struct hp82335_priv *priv = board->private_data;
157
158 return tms9914_serial_poll_status(board, &priv->tms9914_priv);
159 }
160
hp82335_line_status(const struct gpib_board * board)161 static int hp82335_line_status(const struct gpib_board *board)
162 {
163 struct hp82335_priv *priv = board->private_data;
164
165 return tms9914_line_status(board, &priv->tms9914_priv);
166 }
167
hp82335_t1_delay(struct gpib_board * board,unsigned int nano_sec)168 static int hp82335_t1_delay(struct gpib_board *board, unsigned int nano_sec)
169 {
170 struct hp82335_priv *priv = board->private_data;
171
172 return tms9914_t1_delay(board, &priv->tms9914_priv, nano_sec);
173 }
174
hp82335_return_to_local(struct gpib_board * board)175 static void hp82335_return_to_local(struct gpib_board *board)
176 {
177 struct hp82335_priv *priv = board->private_data;
178
179 tms9914_return_to_local(board, &priv->tms9914_priv);
180 }
181
182 static gpib_interface_t hp82335_interface = {
183 .name = "hp82335",
184 .attach = hp82335_attach,
185 .detach = hp82335_detach,
186 .read = hp82335_read,
187 .write = hp82335_write,
188 .command = hp82335_command,
189 .request_system_control = hp82335_request_system_control,
190 .take_control = hp82335_take_control,
191 .go_to_standby = hp82335_go_to_standby,
192 .interface_clear = hp82335_interface_clear,
193 .remote_enable = hp82335_remote_enable,
194 .enable_eos = hp82335_enable_eos,
195 .disable_eos = hp82335_disable_eos,
196 .parallel_poll = hp82335_parallel_poll,
197 .parallel_poll_configure = hp82335_parallel_poll_configure,
198 .parallel_poll_response = hp82335_parallel_poll_response,
199 .local_parallel_poll_mode = NULL, // XXX
200 .line_status = hp82335_line_status,
201 .update_status = hp82335_update_status,
202 .primary_address = hp82335_primary_address,
203 .secondary_address = hp82335_secondary_address,
204 .serial_poll_response = hp82335_serial_poll_response,
205 .serial_poll_status = hp82335_serial_poll_status,
206 .t1_delay = hp82335_t1_delay,
207 .return_to_local = hp82335_return_to_local,
208 };
209
hp82335_allocate_private(struct gpib_board * board)210 static int hp82335_allocate_private(struct gpib_board *board)
211 {
212 board->private_data = kzalloc(sizeof(struct hp82335_priv), GFP_KERNEL);
213 if (!board->private_data)
214 return -1;
215 return 0;
216 }
217
hp82335_free_private(struct gpib_board * board)218 static void hp82335_free_private(struct gpib_board *board)
219 {
220 kfree(board->private_data);
221 board->private_data = NULL;
222 }
223
tms9914_to_hp82335_offset(unsigned int register_num)224 static inline unsigned int tms9914_to_hp82335_offset(unsigned int register_num)
225 {
226 return 0x1ff8 + register_num;
227 }
228
hp82335_read_byte(struct tms9914_priv * priv,unsigned int register_num)229 static uint8_t hp82335_read_byte(struct tms9914_priv *priv, unsigned int register_num)
230 {
231 return tms9914_iomem_read_byte(priv, tms9914_to_hp82335_offset(register_num));
232 }
233
hp82335_write_byte(struct tms9914_priv * priv,uint8_t data,unsigned int register_num)234 static void hp82335_write_byte(struct tms9914_priv *priv, uint8_t data, unsigned int register_num)
235 {
236 tms9914_iomem_write_byte(priv, data, tms9914_to_hp82335_offset(register_num));
237 }
238
hp82335_clear_interrupt(struct hp82335_priv * hp_priv)239 static void hp82335_clear_interrupt(struct hp82335_priv *hp_priv)
240 {
241 struct tms9914_priv *tms_priv = &hp_priv->tms9914_priv;
242
243 writeb(0, tms_priv->mmiobase + HPREG_INTR_CLEAR);
244 }
245
hp82335_attach(struct gpib_board * board,const gpib_board_config_t * config)246 static int hp82335_attach(struct gpib_board *board, const gpib_board_config_t *config)
247 {
248 struct hp82335_priv *hp_priv;
249 struct tms9914_priv *tms_priv;
250 int retval;
251 const unsigned long upper_iomem_base = config->ibbase + hp82335_rom_size;
252
253 board->status = 0;
254
255 if (hp82335_allocate_private(board))
256 return -ENOMEM;
257 hp_priv = board->private_data;
258 tms_priv = &hp_priv->tms9914_priv;
259 tms_priv->read_byte = hp82335_read_byte;
260 tms_priv->write_byte = hp82335_write_byte;
261 tms_priv->offset = 1;
262
263 switch (config->ibbase) {
264 case 0xc4000:
265 case 0xc8000:
266 case 0xcc000:
267 case 0xd0000:
268 case 0xd4000:
269 case 0xd8000:
270 case 0xdc000:
271 case 0xe0000:
272 case 0xe4000:
273 case 0xe8000:
274 case 0xec000:
275 case 0xf0000:
276 case 0xf4000:
277 case 0xf8000:
278 case 0xfc000:
279 break;
280 default:
281 dev_err(board->gpib_dev, "invalid base io address 0x%x\n", config->ibbase);
282 return -EINVAL;
283 }
284 if (!request_mem_region(upper_iomem_base, hp82335_upper_iomem_size, "hp82335")) {
285 dev_err(board->gpib_dev, "failed to allocate io memory region 0x%lx-0x%lx\n",
286 upper_iomem_base, upper_iomem_base + hp82335_upper_iomem_size - 1);
287 return -EBUSY;
288 }
289 hp_priv->raw_iobase = upper_iomem_base;
290 tms_priv->mmiobase = ioremap(upper_iomem_base, hp82335_upper_iomem_size);
291
292 retval = request_irq(config->ibirq, hp82335_interrupt, 0, DRV_NAME, board);
293 if (retval) {
294 dev_err(board->gpib_dev, "can't request IRQ %d\n", config->ibirq);
295 return retval;
296 }
297 hp_priv->irq = config->ibirq;
298
299 tms9914_board_reset(tms_priv);
300
301 hp82335_clear_interrupt(hp_priv);
302
303 writeb(INTR_ENABLE, tms_priv->mmiobase + HPREG_CCR);
304
305 tms9914_online(board, tms_priv);
306
307 return 0;
308 }
309
hp82335_detach(struct gpib_board * board)310 static void hp82335_detach(struct gpib_board *board)
311 {
312 struct hp82335_priv *hp_priv = board->private_data;
313 struct tms9914_priv *tms_priv;
314
315 if (hp_priv) {
316 tms_priv = &hp_priv->tms9914_priv;
317 if (hp_priv->irq)
318 free_irq(hp_priv->irq, board);
319 if (tms_priv->mmiobase) {
320 writeb(0, tms_priv->mmiobase + HPREG_CCR);
321 tms9914_board_reset(tms_priv);
322 iounmap(tms_priv->mmiobase);
323 }
324 if (hp_priv->raw_iobase)
325 release_mem_region(hp_priv->raw_iobase, hp82335_upper_iomem_size);
326 }
327 hp82335_free_private(board);
328 }
329
hp82335_init_module(void)330 static int __init hp82335_init_module(void)
331 {
332 int result = gpib_register_driver(&hp82335_interface, THIS_MODULE);
333
334 if (result) {
335 pr_err("gpib_register_driver failed: error = %d\n", result);
336 return result;
337 }
338
339 return 0;
340 }
341
hp82335_exit_module(void)342 static void __exit hp82335_exit_module(void)
343 {
344 gpib_unregister_driver(&hp82335_interface);
345 }
346
347 module_init(hp82335_init_module);
348 module_exit(hp82335_exit_module);
349
350 /*
351 * GPIB interrupt service routines
352 */
353
hp82335_interrupt(int irq,void * arg)354 static irqreturn_t hp82335_interrupt(int irq, void *arg)
355 {
356 int status1, status2;
357 struct gpib_board *board = arg;
358 struct hp82335_priv *priv = board->private_data;
359 unsigned long flags;
360 irqreturn_t retval;
361
362 spin_lock_irqsave(&board->spinlock, flags);
363 status1 = read_byte(&priv->tms9914_priv, ISR0);
364 status2 = read_byte(&priv->tms9914_priv, ISR1);
365 hp82335_clear_interrupt(priv);
366 retval = tms9914_interrupt_have_status(board, &priv->tms9914_priv, status1, status2);
367 spin_unlock_irqrestore(&board->spinlock, flags);
368 return retval;
369 }
370
371