hp82335.c 10 KB

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