hsq.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* $OpenBSD: hsq.c,v 1.5 2008/11/22 10:33:33 deraadt Exp $ */
  2. /*-
  3. * Copyright (c) 1999 Denis A. Doroshenko. All rights reserved.
  4. *
  5. * This code is derived from BSD-licensed software written by
  6. * Christopher G. Demetriou and Charles Hannum, which was
  7. * derived from public-domain software written by Roland McGrath.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer in the documentation and/or other materials provided
  17. * with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  20. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  27. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. * Note: this copyright notice is a version of Berkeley-style license
  32. * derived from the original by Regents of the University of California.
  33. */
  34. /*
  35. * This is OpenBSD driver for Hostess 4-channel serial mux -- hsq
  36. * ("hsq" stands for "HoStess Quad channel mux"; funny enough).
  37. *
  38. * Hostess may be the registered trademark of Comtrol Corp.
  39. * WARNING! The information that is below about Hostess family
  40. * serial cards is not based on any information from Comtrol,
  41. * and I DON'T guarantee this information is authentic or right.
  42. * For authentic information on Hostess serial cards visit
  43. * http://www.comtrol.com
  44. *
  45. * Hostess mux is a very simple thing -- it's 4/8/16(?) UARTs one
  46. * after another, that share one IRQ. Under linux they may be used
  47. * with setserial, BSD systems are not so flexible with serial ports,
  48. * so i took ast driver and changed it, in the way i think Hostess
  49. * muxes should work. It works ifine with my mux (it has ti16c750
  50. * UARTs on it, and current com driver detects them as 550A, so i
  51. * changed it a bit, to use the power of 750).
  52. *
  53. * Hostess cards use scratch register of lead UART to control the mux.
  54. * When a byte is written to the register it is meant as mask, which
  55. * enables/disables interrupts from 1-8 UARTs by setting 0-7 bits to
  56. * 1/0. Let us call this register UER -- UARTs Enable Register.
  57. * When a byte is read it is mask of bits for UARTs, that have some
  58. * event(s), which are analyzed using that UART's IIR. Let us call
  59. * this register UIR -- UARTs Interrrupt Register.
  60. *
  61. * Note: four higher bits of the UIR are alway 1 for 4-channel mux,
  62. * I have no idea what could that mean, it would be better to have
  63. * them zeroed.
  64. *
  65. * Shitty feature: UER's value upon power up is absolutely random,
  66. * so that UARTs can work and can not and you don't uderstand what's up...
  67. * Thus, we have to set its value to 0x0f to get all four UARTs
  68. * interrupting, just after we've attached the mux...
  69. *
  70. * Use it and share my fun!
  71. * --
  72. * Denis A. Doroshenko <cyxob@isl.vtu.lt>
  73. */
  74. #include <sys/param.h>
  75. #include <sys/systm.h>
  76. #include <sys/device.h>
  77. #include <sys/termios.h>
  78. #include <machine/bus.h>
  79. #include <machine/intr.h>
  80. #include <dev/isa/isavar.h>
  81. #include <dev/ic/comreg.h>
  82. #include <dev/ic/comvar.h>
  83. /*
  84. * For 8-channel Hostess serial card change: NSLAVES -> 8, UART_MASK -> 0xff
  85. */
  86. #define NSLAVES (4)
  87. #define UART_MASK (0x0f)
  88. #define com_uer (com_scratch)
  89. #define com_uir (com_scratch)
  90. struct hsq_softc {
  91. struct device sc_dev;
  92. void *sc_ih;
  93. bus_space_tag_t sc_iot;
  94. int sc_iobase;
  95. void *sc_slaves[NSLAVES]; /* com device unit numbers */
  96. bus_space_handle_t sc_slaveioh[NSLAVES];
  97. };
  98. int hsqprobe(struct device *, void *, void *);
  99. void hsqattach(struct device *, struct device *, void *);
  100. int hsqintr(void *);
  101. int hsqprint(void *, const char *);
  102. struct cfattach hsq_ca = {
  103. sizeof(struct hsq_softc), hsqprobe, hsqattach
  104. };
  105. struct cfdriver hsq_cd = {
  106. NULL, "hsq", DV_TTY
  107. };
  108. int
  109. hsqprobe(parent, self, aux)
  110. struct device *parent;
  111. void *self;
  112. void *aux;
  113. {
  114. struct isa_attach_args *ia = aux;
  115. int iobase = ia->ia_iobase;
  116. bus_space_tag_t iot = ia->ia_iot;
  117. bus_space_handle_t ioh;
  118. int i, rv = 1;
  119. /*
  120. * Do the normal com probe for the first UART and assume
  121. * its presence, and the ability to map the other UARTs,
  122. * means there is a multiport board there.
  123. * XXX Needs more robustness.
  124. */
  125. /* if the first port is in use as console, then it. */
  126. if (iobase == comconsaddr && !comconsattached)
  127. goto checkmappings;
  128. /* map UART ports (COM_NPORTS == 8) */
  129. if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) {
  130. rv = 0;
  131. goto out;
  132. }
  133. rv = comprobe1(iot, ioh);
  134. bus_space_unmap(iot, ioh, COM_NPORTS);
  135. if (rv == 0)
  136. goto out;
  137. checkmappings:
  138. for (i = 1; i < NSLAVES; i++) {
  139. iobase += COM_NPORTS;
  140. if (iobase == comconsaddr && !comconsattached)
  141. continue;
  142. if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) {
  143. rv = 0;
  144. goto out;
  145. }
  146. bus_space_unmap(iot, ioh, COM_NPORTS);
  147. }
  148. out:
  149. if (rv)
  150. ia->ia_iosize = NSLAVES * COM_NPORTS;
  151. return (rv);
  152. }
  153. int
  154. hsqprint(aux, pnp)
  155. void *aux;
  156. const char *pnp;
  157. {
  158. struct commulti_attach_args *ca = aux;
  159. if (pnp)
  160. printf("com at %s", pnp);
  161. printf(" slave %d", ca->ca_slave);
  162. return (UNCONF);
  163. }
  164. void
  165. hsqattach(parent, self, aux)
  166. struct device *parent, *self;
  167. void *aux;
  168. {
  169. struct hsq_softc *sc = (void *)self;
  170. struct isa_attach_args *ia = aux;
  171. struct commulti_attach_args ca;
  172. int i;
  173. sc->sc_iot = ia->ia_iot;
  174. sc->sc_iobase = ia->ia_iobase;
  175. for (i = 0; i < NSLAVES; i++)
  176. if (bus_space_map(sc->sc_iot, sc->sc_iobase + i * COM_NPORTS,
  177. COM_NPORTS, 0, &sc->sc_slaveioh[i]))
  178. panic("hsqattach: couldn't map slave %d", i);
  179. printf("\n");
  180. for (i = 0; i < NSLAVES; i++) {
  181. ca.ca_slave = i;
  182. ca.ca_iot = sc->sc_iot;
  183. ca.ca_ioh = sc->sc_slaveioh[i];
  184. ca.ca_iobase = sc->sc_iobase + i * COM_NPORTS;
  185. ca.ca_noien = 1;
  186. sc->sc_slaves[i] = config_found(self, &ca, hsqprint);
  187. }
  188. /* allow interrupts from all four UARTs */
  189. bus_space_write_1(sc->sc_iot, sc->sc_slaveioh[0], com_uer, UART_MASK);
  190. sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
  191. IPL_TTY, hsqintr, sc, sc->sc_dev.dv_xname);
  192. }
  193. int
  194. hsqintr(arg)
  195. void *arg;
  196. {
  197. struct hsq_softc *sc = arg;
  198. bus_space_tag_t iot = sc->sc_iot;
  199. int bits;
  200. bits = bus_space_read_1(iot, sc->sc_slaveioh[0], com_uir) & UART_MASK;
  201. if ( !bits )
  202. return (0); /* no interrupts pending */
  203. for (;;) {
  204. #define TRY(n) \
  205. if ( sc->sc_slaves[n] && bits & (1 << (n)) ) \
  206. comintr(sc->sc_slaves[n]);
  207. TRY(0);
  208. TRY(1);
  209. TRY(2);
  210. TRY(3);
  211. #undef TRY
  212. bits = bus_space_read_1(iot, sc->sc_slaveioh[0],
  213. com_uir) & UART_MASK;
  214. if ( !bits )
  215. return (1); /* no interrupts pending */
  216. }
  217. }